rv64: Require Zba, Zbb, and Zbs instructions
1f0c6397f452d2632206bd810675bd6987814afbbc11a3280ea948e8b87ee4dc
Support all RV64 instructions in the three extensions in the encoder, assembler, decoder, and printer. Select them for integer extension, scaled addresses, rotations, bit operations, and constant arithmetic. Combine adjacent expressions with single-use intermediates to preserve register allocation and narrow integer semantics. Assisted-by: Codex:gpt-6 Change-Id: Ie283135fe008eb47efd3fee3d1b62beffff83e86
1 parent
9839c5be
lib/std/arch/rv64.rad
+5 -5
| 20 | 20 | export mod asm; |
|
| 21 | 21 | export mod shared; |
|
| 22 | 22 | ||
| 23 | 23 | @test mod tests; |
|
| 24 | 24 | ||
| 25 | - | use std::mem; |
|
| 26 | 25 | use std::collections::dict; |
|
| 27 | - | use std::lang::il; |
|
| 28 | - | use std::lang::parser; |
|
| 29 | - | use std::mem::heap; |
|
| 30 | 26 | use std::lang::gen; |
|
| 27 | + | use std::lang::gen::data; |
|
| 31 | 28 | use std::lang::gen::labels; |
|
| 32 | 29 | use std::lang::gen::regalloc; |
|
| 33 | - | use std::lang::gen::data; |
|
| 34 | 30 | use std::lang::gen::types; |
|
| 31 | + | use std::lang::il; |
|
| 32 | + | use std::lang::parser; |
|
| 33 | + | use std::mem; |
|
| 34 | + | use std::mem::heap; |
|
| 35 | 35 | ||
| 36 | 36 | /// Interrupt flag in the RV64 mcause register. |
|
| 37 | 37 | export constant MCAUSE_INTERRUPT: u64 = 1 << 63; |
|
| 38 | 38 | /// Instruction address misalignment exception code. |
|
| 39 | 39 | export constant MCAUSE_INSTRUCTION_MISALIGNED: u64 = 0; |
lib/std/arch/rv64/asm.rad
+145 -104
| 34 | 34 | //! input's emitted data to that prefix. Global text symbols are exported for |
|
| 35 | 35 | //! call resolution when the assembled text is appended to the RV64 generator, |
|
| 36 | 36 | //! shifted by the generator's current code length so disassembly/debug output |
|
| 37 | 37 | //! can name those instruction addresses correctly. Non-global text labels |
|
| 38 | 38 | //! remain local to their assembly fragment. |
|
| 39 | - | use std::io; |
|
| 39 | + | use std::arch::rv64; |
|
| 40 | + | use std::arch::rv64::encode; |
|
| 41 | + | use std::collections::dict; |
|
| 40 | 42 | use std::fmt; |
|
| 43 | + | use std::io; |
|
| 41 | 44 | use std::lang::alloc; |
|
| 42 | - | use std::mem::heap; |
|
| 43 | - | use std::lang::strings; |
|
| 44 | 45 | use std::lang::gen; |
|
| 45 | - | use std::collections::dict; |
|
| 46 | - | use std::arch::rv64::encode; |
|
| 47 | - | use std::arch::rv64; |
|
| 46 | + | use std::lang::strings; |
|
| 48 | 47 | use std::mem; |
|
| 48 | + | use std::mem::heap; |
|
| 49 | 49 | ||
| 50 | 50 | /// Assembler scanner module. |
|
| 51 | 51 | export mod scanner; |
|
| 52 | 52 | /// Assembler parser module. |
|
| 53 | 53 | export mod parser; |
| 326 | 326 | export constant SHIFT_LIMIT: i32 = 64; |
|
| 327 | 327 | /// Largest `lui` or `auipc` immediate. |
|
| 328 | 328 | export constant UPPER_IMM_MAX_VALUE: i64 = 0xFFFFF; |
|
| 329 | 329 | ||
| 330 | 330 | /// Sorted instruction descriptor table used by the assembler parser. |
|
| 331 | - | export constant INSTRUCTIONS: [InstructionEntry; 98] = [ |
|
| 332 | - | { name: "add", encoder: InstructionEncoder::RRR { enc: encode::add } }, |
|
| 333 | - | { name: "addi", encoder: InstructionEncoder::RRI { enc: encode::addi } }, |
|
| 334 | - | { name: "addiw", encoder: InstructionEncoder::RRI { enc: encode::addiw } }, |
|
| 335 | - | { name: "addw", encoder: InstructionEncoder::RRR { enc: encode::addw } }, |
|
| 336 | - | { name: "and", encoder: InstructionEncoder::RRR { enc: encode::and_ } }, |
|
| 337 | - | { name: "andi", encoder: InstructionEncoder::RRI { enc: encode::andi } }, |
|
| 338 | - | { name: "auipc", encoder: InstructionEncoder::Upper { enc: encode::auipc } }, |
|
| 339 | - | { name: "beq", encoder: InstructionEncoder::Branch { op: BranchOp::Beq } }, |
|
| 340 | - | { name: "beqz", encoder: InstructionEncoder::BranchZero { op: BranchOp::Beq } }, |
|
| 341 | - | { name: "bge", encoder: InstructionEncoder::Branch { op: BranchOp::Bge } }, |
|
| 342 | - | { name: "bgeu", encoder: InstructionEncoder::Branch { op: BranchOp::Bgeu } }, |
|
| 343 | - | { name: "bgt", encoder: InstructionEncoder::Branch { op: BranchOp::Bgt } }, |
|
| 344 | - | { name: "bgtu", encoder: InstructionEncoder::Branch { op: BranchOp::Bgtu } }, |
|
| 345 | - | { name: "ble", encoder: InstructionEncoder::Branch { op: BranchOp::Ble } }, |
|
| 346 | - | { name: "bleu", encoder: InstructionEncoder::Branch { op: BranchOp::Bleu } }, |
|
| 347 | - | { name: "blt", encoder: InstructionEncoder::Branch { op: BranchOp::Blt } }, |
|
| 348 | - | { name: "bltu", encoder: InstructionEncoder::Branch { op: BranchOp::Bltu } }, |
|
| 349 | - | { name: "bne", encoder: InstructionEncoder::Branch { op: BranchOp::Bne } }, |
|
| 350 | - | { name: "bnez", encoder: InstructionEncoder::BranchZero { op: BranchOp::Bne } }, |
|
| 351 | - | { name: "call", encoder: InstructionEncoder::Call { link: rv64::RA, scratch: rv64::RA } }, |
|
| 352 | - | { name: "csrc", encoder: InstructionEncoder::CsrRs1 { enc: encode::csrc } }, |
|
| 353 | - | { name: "csrci", encoder: InstructionEncoder::CsrImmediate { enc: encode::csrci } }, |
|
| 354 | - | { name: "csrr", encoder: InstructionEncoder::RdCsr { enc: encode::csrr } }, |
|
| 355 | - | { name: "csrrc", encoder: InstructionEncoder::CsrRegister { enc: encode::csrrc } }, |
|
| 356 | - | { name: "csrrci", encoder: InstructionEncoder::CsrReadImmediate { enc: encode::csrrci } }, |
|
| 357 | - | { name: "csrrs", encoder: InstructionEncoder::CsrRegister { enc: encode::csrrs } }, |
|
| 358 | - | { name: "csrrsi", encoder: InstructionEncoder::CsrReadImmediate { enc: encode::csrrsi } }, |
|
| 359 | - | { name: "csrrw", encoder: InstructionEncoder::CsrRegister { enc: encode::csrrw } }, |
|
| 360 | - | { name: "csrrwi", encoder: InstructionEncoder::CsrReadImmediate { enc: encode::csrrwi } }, |
|
| 361 | - | { name: "csrs", encoder: InstructionEncoder::CsrRs1 { enc: encode::csrs } }, |
|
| 362 | - | { name: "csrsi", encoder: InstructionEncoder::CsrImmediate { enc: encode::csrsi } }, |
|
| 363 | - | { name: "csrw", encoder: InstructionEncoder::CsrRs1 { enc: encode::csrw } }, |
|
| 364 | - | { name: "div", encoder: InstructionEncoder::RRR { enc: encode::div } }, |
|
| 365 | - | { name: "divu", encoder: InstructionEncoder::RRR { enc: encode::divu } }, |
|
| 366 | - | { name: "divuw", encoder: InstructionEncoder::RRR { enc: encode::divuw } }, |
|
| 367 | - | { name: "divw", encoder: InstructionEncoder::RRR { enc: encode::divw } }, |
|
| 368 | - | { name: "ebreak", encoder: InstructionEncoder::NoOperand { enc: encode::ebreak } }, |
|
| 369 | - | { name: "ecall", encoder: InstructionEncoder::NoOperand { enc: encode::ecall } }, |
|
| 370 | - | { name: "fence", encoder: InstructionEncoder::Fence }, |
|
| 331 | + | export constant INSTRUCTIONS: [InstructionEntry; 139] = [ |
|
| 332 | + | { name: "add", encoder: InstructionEncoder::RRR { enc: encode::add } }, |
|
| 333 | + | { name: "add.uw", encoder: InstructionEncoder::RRR { enc: encode::addUw } }, |
|
| 334 | + | { name: "addi", encoder: InstructionEncoder::RRI { enc: encode::addi } }, |
|
| 335 | + | { name: "addiw", encoder: InstructionEncoder::RRI { enc: encode::addiw } }, |
|
| 336 | + | { name: "addw", encoder: InstructionEncoder::RRR { enc: encode::addw } }, |
|
| 337 | + | { name: "and", encoder: InstructionEncoder::RRR { enc: encode::and_ } }, |
|
| 338 | + | { name: "andi", encoder: InstructionEncoder::RRI { enc: encode::andi } }, |
|
| 339 | + | { name: "andn", encoder: InstructionEncoder::RRR { enc: encode::andn } }, |
|
| 340 | + | { name: "auipc", encoder: InstructionEncoder::Upper { enc: encode::auipc } }, |
|
| 341 | + | { name: "bclr", encoder: InstructionEncoder::RRR { enc: encode::bclr } }, |
|
| 342 | + | { name: "bclri", encoder: InstructionEncoder::Shift { enc: encode::bclri } }, |
|
| 343 | + | { name: "beq", encoder: InstructionEncoder::Branch { op: BranchOp::Beq } }, |
|
| 344 | + | { name: "beqz", encoder: InstructionEncoder::BranchZero { op: BranchOp::Beq } }, |
|
| 345 | + | { name: "bext", encoder: InstructionEncoder::RRR { enc: encode::bext } }, |
|
| 346 | + | { name: "bexti", encoder: InstructionEncoder::Shift { enc: encode::bexti } }, |
|
| 347 | + | { name: "bge", encoder: InstructionEncoder::Branch { op: BranchOp::Bge } }, |
|
| 348 | + | { name: "bgeu", encoder: InstructionEncoder::Branch { op: BranchOp::Bgeu } }, |
|
| 349 | + | { name: "bgt", encoder: InstructionEncoder::Branch { op: BranchOp::Bgt } }, |
|
| 350 | + | { name: "bgtu", encoder: InstructionEncoder::Branch { op: BranchOp::Bgtu } }, |
|
| 351 | + | { name: "binv", encoder: InstructionEncoder::RRR { enc: encode::binv } }, |
|
| 352 | + | { name: "binvi", encoder: InstructionEncoder::Shift { enc: encode::binvi } }, |
|
| 353 | + | { name: "ble", encoder: InstructionEncoder::Branch { op: BranchOp::Ble } }, |
|
| 354 | + | { name: "bleu", encoder: InstructionEncoder::Branch { op: BranchOp::Bleu } }, |
|
| 355 | + | { name: "blt", encoder: InstructionEncoder::Branch { op: BranchOp::Blt } }, |
|
| 356 | + | { name: "bltu", encoder: InstructionEncoder::Branch { op: BranchOp::Bltu } }, |
|
| 357 | + | { name: "bne", encoder: InstructionEncoder::Branch { op: BranchOp::Bne } }, |
|
| 358 | + | { name: "bnez", encoder: InstructionEncoder::BranchZero { op: BranchOp::Bne } }, |
|
| 359 | + | { name: "bset", encoder: InstructionEncoder::RRR { enc: encode::bset } }, |
|
| 360 | + | { name: "bseti", encoder: InstructionEncoder::Shift { enc: encode::bseti } }, |
|
| 361 | + | { name: "call", encoder: InstructionEncoder::Call { link: rv64::RA, scratch: rv64::RA } }, |
|
| 362 | + | { name: "clz", encoder: InstructionEncoder::RR { enc: encode::clz } }, |
|
| 363 | + | { name: "clzw", encoder: InstructionEncoder::RR { enc: encode::clzw } }, |
|
| 364 | + | { name: "cpop", encoder: InstructionEncoder::RR { enc: encode::cpop } }, |
|
| 365 | + | { name: "cpopw", encoder: InstructionEncoder::RR { enc: encode::cpopw } }, |
|
| 366 | + | { name: "csrc", encoder: InstructionEncoder::CsrRs1 { enc: encode::csrc } }, |
|
| 367 | + | { name: "csrci", encoder: InstructionEncoder::CsrImmediate { enc: encode::csrci } }, |
|
| 368 | + | { name: "csrr", encoder: InstructionEncoder::RdCsr { enc: encode::csrr } }, |
|
| 369 | + | { name: "csrrc", encoder: InstructionEncoder::CsrRegister { enc: encode::csrrc } }, |
|
| 370 | + | { name: "csrrci", encoder: InstructionEncoder::CsrReadImmediate { enc: encode::csrrci } }, |
|
| 371 | + | { name: "csrrs", encoder: InstructionEncoder::CsrRegister { enc: encode::csrrs } }, |
|
| 372 | + | { name: "csrrsi", encoder: InstructionEncoder::CsrReadImmediate { enc: encode::csrrsi } }, |
|
| 373 | + | { name: "csrrw", encoder: InstructionEncoder::CsrRegister { enc: encode::csrrw } }, |
|
| 374 | + | { name: "csrrwi", encoder: InstructionEncoder::CsrReadImmediate { enc: encode::csrrwi } }, |
|
| 375 | + | { name: "csrs", encoder: InstructionEncoder::CsrRs1 { enc: encode::csrs } }, |
|
| 376 | + | { name: "csrsi", encoder: InstructionEncoder::CsrImmediate { enc: encode::csrsi } }, |
|
| 377 | + | { name: "csrw", encoder: InstructionEncoder::CsrRs1 { enc: encode::csrw } }, |
|
| 378 | + | { name: "ctz", encoder: InstructionEncoder::RR { enc: encode::ctz } }, |
|
| 379 | + | { name: "ctzw", encoder: InstructionEncoder::RR { enc: encode::ctzw } }, |
|
| 380 | + | { name: "div", encoder: InstructionEncoder::RRR { enc: encode::div } }, |
|
| 381 | + | { name: "divu", encoder: InstructionEncoder::RRR { enc: encode::divu } }, |
|
| 382 | + | { name: "divuw", encoder: InstructionEncoder::RRR { enc: encode::divuw } }, |
|
| 383 | + | { name: "divw", encoder: InstructionEncoder::RRR { enc: encode::divw } }, |
|
| 384 | + | { name: "ebreak", encoder: InstructionEncoder::NoOperand { enc: encode::ebreak } }, |
|
| 385 | + | { name: "ecall", encoder: InstructionEncoder::NoOperand { enc: encode::ecall } }, |
|
| 386 | + | { name: "fence", encoder: InstructionEncoder::Fence }, |
|
| 371 | 387 | { name: "fence.i", encoder: InstructionEncoder::NoOperand { enc: encode::fenceI } }, |
|
| 372 | - | { name: "j", encoder: InstructionEncoder::Jump { rd: rv64::ZERO } }, |
|
| 373 | - | { name: "jal", encoder: InstructionEncoder::Jal }, |
|
| 374 | - | { name: "jalr", encoder: InstructionEncoder::Jalr }, |
|
| 375 | - | { name: "la", encoder: InstructionEncoder::La }, |
|
| 376 | - | { name: "lb", encoder: InstructionEncoder::Load { enc: encode::lb } }, |
|
| 377 | - | { name: "lbu", encoder: InstructionEncoder::Load { enc: encode::lbu } }, |
|
| 378 | - | { name: "ld", encoder: InstructionEncoder::Load { enc: encode::ld } }, |
|
| 379 | - | { name: "lh", encoder: InstructionEncoder::Load { enc: encode::lh } }, |
|
| 380 | - | { name: "lhu", encoder: InstructionEncoder::Load { enc: encode::lhu } }, |
|
| 381 | - | { name: "li", encoder: InstructionEncoder::Li }, |
|
| 382 | - | { name: "lui", encoder: InstructionEncoder::Upper { enc: encode::lui } }, |
|
| 383 | - | { name: "lw", encoder: InstructionEncoder::Load { enc: encode::lw } }, |
|
| 384 | - | { name: "lwu", encoder: InstructionEncoder::Load { enc: encode::lwu } }, |
|
| 385 | - | { name: "mret", encoder: InstructionEncoder::NoOperand { enc: encode::mret } }, |
|
| 386 | - | { name: "mul", encoder: InstructionEncoder::RRR { enc: encode::mul } }, |
|
| 387 | - | { name: "mulh", encoder: InstructionEncoder::RRR { enc: encode::mulh } }, |
|
| 388 | - | { name: "mulhsu", encoder: InstructionEncoder::RRR { enc: encode::mulhsu } }, |
|
| 389 | - | { name: "mulhu", encoder: InstructionEncoder::RRR { enc: encode::mulhu } }, |
|
| 390 | - | { name: "mulw", encoder: InstructionEncoder::RRR { enc: encode::mulw } }, |
|
| 391 | - | { name: "mv", encoder: InstructionEncoder::RR { enc: encode::mv } }, |
|
| 392 | - | { name: "neg", encoder: InstructionEncoder::RR { enc: encode::neg } }, |
|
| 393 | - | { name: "nop", encoder: InstructionEncoder::NoOperand { enc: encode::nop } }, |
|
| 394 | - | { name: "not", encoder: InstructionEncoder::RR { enc: encode::not_ } }, |
|
| 395 | - | { name: "or", encoder: InstructionEncoder::RRR { enc: encode::or_ } }, |
|
| 396 | - | { name: "ori", encoder: InstructionEncoder::RRI { enc: encode::ori } }, |
|
| 397 | - | { name: "rem", encoder: InstructionEncoder::RRR { enc: encode::rem } }, |
|
| 398 | - | { name: "remu", encoder: InstructionEncoder::RRR { enc: encode::remu } }, |
|
| 399 | - | { name: "remuw", encoder: InstructionEncoder::RRR { enc: encode::remuw } }, |
|
| 400 | - | { name: "remw", encoder: InstructionEncoder::RRR { enc: encode::remw } }, |
|
| 401 | - | { name: "ret", encoder: InstructionEncoder::NoOperand { enc: encode::ret } }, |
|
| 402 | - | { name: "sb", encoder: InstructionEncoder::Store { enc: encode::sb } }, |
|
| 403 | - | { name: "sd", encoder: InstructionEncoder::Store { enc: encode::sd } }, |
|
| 404 | - | { name: "seqz", encoder: InstructionEncoder::RR { enc: encode::seqz } }, |
|
| 405 | - | { name: "sh", encoder: InstructionEncoder::Store { enc: encode::sh } }, |
|
| 406 | - | { name: "sll", encoder: InstructionEncoder::RRR { enc: encode::sll } }, |
|
| 407 | - | { name: "slli", encoder: InstructionEncoder::Shift { enc: encode::slli } }, |
|
| 408 | - | { name: "slliw", encoder: InstructionEncoder::WordShift { enc: encode::slliw } }, |
|
| 409 | - | { name: "sllw", encoder: InstructionEncoder::RRR { enc: encode::sllw } }, |
|
| 410 | - | { name: "slt", encoder: InstructionEncoder::RRR { enc: encode::slt } }, |
|
| 411 | - | { name: "slti", encoder: InstructionEncoder::RRI { enc: encode::slti } }, |
|
| 412 | - | { name: "sltiu", encoder: InstructionEncoder::RRI { enc: encode::sltiu } }, |
|
| 413 | - | { name: "sltu", encoder: InstructionEncoder::RRR { enc: encode::sltu } }, |
|
| 414 | - | { name: "snez", encoder: InstructionEncoder::RR { enc: encode::snez } }, |
|
| 415 | - | { name: "sra", encoder: InstructionEncoder::RRR { enc: encode::sra } }, |
|
| 416 | - | { name: "srai", encoder: InstructionEncoder::Shift { enc: encode::srai } }, |
|
| 417 | - | { name: "sraiw", encoder: InstructionEncoder::WordShift { enc: encode::sraiw } }, |
|
| 418 | - | { name: "sraw", encoder: InstructionEncoder::RRR { enc: encode::sraw } }, |
|
| 419 | - | { name: "srl", encoder: InstructionEncoder::RRR { enc: encode::srl } }, |
|
| 420 | - | { name: "srli", encoder: InstructionEncoder::Shift { enc: encode::srli } }, |
|
| 421 | - | { name: "srliw", encoder: InstructionEncoder::WordShift { enc: encode::srliw } }, |
|
| 422 | - | { name: "srlw", encoder: InstructionEncoder::RRR { enc: encode::srlw } }, |
|
| 423 | - | { name: "sub", encoder: InstructionEncoder::RRR { enc: encode::sub } }, |
|
| 424 | - | { name: "subw", encoder: InstructionEncoder::RRR { enc: encode::subw } }, |
|
| 425 | - | { name: "sw", encoder: InstructionEncoder::Store { enc: encode::sw } }, |
|
| 426 | - | { name: "tail", encoder: InstructionEncoder::Call { link: rv64::ZERO, scratch: rv64::T1 } }, |
|
| 427 | - | { name: "wfi", encoder: InstructionEncoder::NoOperand { enc: encode::wfi } }, |
|
| 428 | - | { name: "xor", encoder: InstructionEncoder::RRR { enc: encode::xor } }, |
|
| 429 | - | { name: "xori", encoder: InstructionEncoder::RRI { enc: encode::xori } }, |
|
| 388 | + | { name: "j", encoder: InstructionEncoder::Jump { rd: rv64::ZERO } }, |
|
| 389 | + | { name: "jal", encoder: InstructionEncoder::Jal }, |
|
| 390 | + | { name: "jalr", encoder: InstructionEncoder::Jalr }, |
|
| 391 | + | { name: "la", encoder: InstructionEncoder::La }, |
|
| 392 | + | { name: "lb", encoder: InstructionEncoder::Load { enc: encode::lb } }, |
|
| 393 | + | { name: "lbu", encoder: InstructionEncoder::Load { enc: encode::lbu } }, |
|
| 394 | + | { name: "ld", encoder: InstructionEncoder::Load { enc: encode::ld } }, |
|
| 395 | + | { name: "lh", encoder: InstructionEncoder::Load { enc: encode::lh } }, |
|
| 396 | + | { name: "lhu", encoder: InstructionEncoder::Load { enc: encode::lhu } }, |
|
| 397 | + | { name: "li", encoder: InstructionEncoder::Li }, |
|
| 398 | + | { name: "lui", encoder: InstructionEncoder::Upper { enc: encode::lui } }, |
|
| 399 | + | { name: "lw", encoder: InstructionEncoder::Load { enc: encode::lw } }, |
|
| 400 | + | { name: "lwu", encoder: InstructionEncoder::Load { enc: encode::lwu } }, |
|
| 401 | + | { name: "max", encoder: InstructionEncoder::RRR { enc: encode::max } }, |
|
| 402 | + | { name: "maxu", encoder: InstructionEncoder::RRR { enc: encode::maxu } }, |
|
| 403 | + | { name: "min", encoder: InstructionEncoder::RRR { enc: encode::min } }, |
|
| 404 | + | { name: "minu", encoder: InstructionEncoder::RRR { enc: encode::minu } }, |
|
| 405 | + | { name: "mret", encoder: InstructionEncoder::NoOperand { enc: encode::mret } }, |
|
| 406 | + | { name: "mul", encoder: InstructionEncoder::RRR { enc: encode::mul } }, |
|
| 407 | + | { name: "mulh", encoder: InstructionEncoder::RRR { enc: encode::mulh } }, |
|
| 408 | + | { name: "mulhsu", encoder: InstructionEncoder::RRR { enc: encode::mulhsu } }, |
|
| 409 | + | { name: "mulhu", encoder: InstructionEncoder::RRR { enc: encode::mulhu } }, |
|
| 410 | + | { name: "mulw", encoder: InstructionEncoder::RRR { enc: encode::mulw } }, |
|
| 411 | + | { name: "mv", encoder: InstructionEncoder::RR { enc: encode::mv } }, |
|
| 412 | + | { name: "neg", encoder: InstructionEncoder::RR { enc: encode::neg } }, |
|
| 413 | + | { name: "nop", encoder: InstructionEncoder::NoOperand { enc: encode::nop } }, |
|
| 414 | + | { name: "not", encoder: InstructionEncoder::RR { enc: encode::not_ } }, |
|
| 415 | + | { name: "or", encoder: InstructionEncoder::RRR { enc: encode::or_ } }, |
|
| 416 | + | { name: "orc.b", encoder: InstructionEncoder::RR { enc: encode::orcB } }, |
|
| 417 | + | { name: "ori", encoder: InstructionEncoder::RRI { enc: encode::ori } }, |
|
| 418 | + | { name: "orn", encoder: InstructionEncoder::RRR { enc: encode::orn } }, |
|
| 419 | + | { name: "rem", encoder: InstructionEncoder::RRR { enc: encode::rem } }, |
|
| 420 | + | { name: "remu", encoder: InstructionEncoder::RRR { enc: encode::remu } }, |
|
| 421 | + | { name: "remuw", encoder: InstructionEncoder::RRR { enc: encode::remuw } }, |
|
| 422 | + | { name: "remw", encoder: InstructionEncoder::RRR { enc: encode::remw } }, |
|
| 423 | + | { name: "ret", encoder: InstructionEncoder::NoOperand { enc: encode::ret } }, |
|
| 424 | + | { name: "rev8", encoder: InstructionEncoder::RR { enc: encode::rev8 } }, |
|
| 425 | + | { name: "rol", encoder: InstructionEncoder::RRR { enc: encode::rol } }, |
|
| 426 | + | { name: "rolw", encoder: InstructionEncoder::RRR { enc: encode::rolw } }, |
|
| 427 | + | { name: "ror", encoder: InstructionEncoder::RRR { enc: encode::ror } }, |
|
| 428 | + | { name: "rori", encoder: InstructionEncoder::Shift { enc: encode::rori } }, |
|
| 429 | + | { name: "roriw", encoder: InstructionEncoder::WordShift { enc: encode::roriw } }, |
|
| 430 | + | { name: "rorw", encoder: InstructionEncoder::RRR { enc: encode::rorw } }, |
|
| 431 | + | { name: "sb", encoder: InstructionEncoder::Store { enc: encode::sb } }, |
|
| 432 | + | { name: "sd", encoder: InstructionEncoder::Store { enc: encode::sd } }, |
|
| 433 | + | { name: "seqz", encoder: InstructionEncoder::RR { enc: encode::seqz } }, |
|
| 434 | + | { name: "sext.b", encoder: InstructionEncoder::RR { enc: encode::sextB } }, |
|
| 435 | + | { name: "sext.h", encoder: InstructionEncoder::RR { enc: encode::sextH } }, |
|
| 436 | + | { name: "sh", encoder: InstructionEncoder::Store { enc: encode::sh } }, |
|
| 437 | + | { name: "sh1add", encoder: InstructionEncoder::RRR { enc: encode::sh1add } }, |
|
| 438 | + | { name: "sh1add.uw", encoder: InstructionEncoder::RRR { enc: encode::sh1addUw } }, |
|
| 439 | + | { name: "sh2add", encoder: InstructionEncoder::RRR { enc: encode::sh2add } }, |
|
| 440 | + | { name: "sh2add.uw", encoder: InstructionEncoder::RRR { enc: encode::sh2addUw } }, |
|
| 441 | + | { name: "sh3add", encoder: InstructionEncoder::RRR { enc: encode::sh3add } }, |
|
| 442 | + | { name: "sh3add.uw", encoder: InstructionEncoder::RRR { enc: encode::sh3addUw } }, |
|
| 443 | + | { name: "sll", encoder: InstructionEncoder::RRR { enc: encode::sll } }, |
|
| 444 | + | { name: "slli", encoder: InstructionEncoder::Shift { enc: encode::slli } }, |
|
| 445 | + | { name: "slli.uw", encoder: InstructionEncoder::Shift { enc: encode::slliUw } }, |
|
| 446 | + | { name: "slliw", encoder: InstructionEncoder::WordShift { enc: encode::slliw } }, |
|
| 447 | + | { name: "sllw", encoder: InstructionEncoder::RRR { enc: encode::sllw } }, |
|
| 448 | + | { name: "slt", encoder: InstructionEncoder::RRR { enc: encode::slt } }, |
|
| 449 | + | { name: "slti", encoder: InstructionEncoder::RRI { enc: encode::slti } }, |
|
| 450 | + | { name: "sltiu", encoder: InstructionEncoder::RRI { enc: encode::sltiu } }, |
|
| 451 | + | { name: "sltu", encoder: InstructionEncoder::RRR { enc: encode::sltu } }, |
|
| 452 | + | { name: "snez", encoder: InstructionEncoder::RR { enc: encode::snez } }, |
|
| 453 | + | { name: "sra", encoder: InstructionEncoder::RRR { enc: encode::sra } }, |
|
| 454 | + | { name: "srai", encoder: InstructionEncoder::Shift { enc: encode::srai } }, |
|
| 455 | + | { name: "sraiw", encoder: InstructionEncoder::WordShift { enc: encode::sraiw } }, |
|
| 456 | + | { name: "sraw", encoder: InstructionEncoder::RRR { enc: encode::sraw } }, |
|
| 457 | + | { name: "srl", encoder: InstructionEncoder::RRR { enc: encode::srl } }, |
|
| 458 | + | { name: "srli", encoder: InstructionEncoder::Shift { enc: encode::srli } }, |
|
| 459 | + | { name: "srliw", encoder: InstructionEncoder::WordShift { enc: encode::srliw } }, |
|
| 460 | + | { name: "srlw", encoder: InstructionEncoder::RRR { enc: encode::srlw } }, |
|
| 461 | + | { name: "sub", encoder: InstructionEncoder::RRR { enc: encode::sub } }, |
|
| 462 | + | { name: "subw", encoder: InstructionEncoder::RRR { enc: encode::subw } }, |
|
| 463 | + | { name: "sw", encoder: InstructionEncoder::Store { enc: encode::sw } }, |
|
| 464 | + | { name: "tail", encoder: InstructionEncoder::Call { link: rv64::ZERO, scratch: rv64::T1 } }, |
|
| 465 | + | { name: "wfi", encoder: InstructionEncoder::NoOperand { enc: encode::wfi } }, |
|
| 466 | + | { name: "xnor", encoder: InstructionEncoder::RRR { enc: encode::xnor } }, |
|
| 467 | + | { name: "xor", encoder: InstructionEncoder::RRR { enc: encode::xor } }, |
|
| 468 | + | { name: "xori", encoder: InstructionEncoder::RRI { enc: encode::xori } }, |
|
| 469 | + | { name: "zext.h", encoder: InstructionEncoder::RR { enc: encode::zextH } }, |
|
| 470 | + | { name: "zext.w", encoder: InstructionEncoder::RR { enc: encode::zextW } }, |
|
| 430 | 471 | ]; |
|
| 431 | 472 | ||
| 432 | 473 | /// Sorted directive lookup table used by the assembler parser. |
|
| 433 | 474 | export constant DIRECTIVES: [DirectiveEntry; 10] = [ |
|
| 434 | 475 | { name: "align", kind: DirectiveKind::Align }, |
lib/std/arch/rv64/asm/tests.rad
+153 -0
| 566 | 566 | ); |
|
| 567 | 567 | try expectInvalidAt( |
|
| 568 | 568 | ".text; @code ret;\n.data; .dword @code;", 32, "data address target must be in data section" |
|
| 569 | 569 | ); |
|
| 570 | 570 | } |
|
| 571 | + | ||
| 572 | + | /// Required bit manipulation instructions use their architectural encodings. |
|
| 573 | + | @test unsafe fn bitManipulation() throws (testing::TestError) { |
|
| 574 | + | let program = try assembleSource( |
|
| 575 | + | "sh1add %a0 %a1 %a2;\nsext.b %a0 %a1;\nbseti %a0 %a1 63;\n" |
|
| 576 | + | ); |
|
| 577 | + | assert program.text.len == 3; |
|
| 578 | + | assert program.text[0] == 0x20c5a533; |
|
| 579 | + | assert program.text[1] == 0x60459513; |
|
| 580 | + | assert program.text[2] == 0x2bf59513; |
|
| 581 | + | } |
|
| 582 | + | ||
| 583 | + | /// All required bit manipulation forms assemble and disassemble. |
|
| 584 | + | @test unsafe fn bitManipulationForms() throws (testing::TestError) { |
|
| 585 | + | assert encode::addUw(rv64::A0, rv64::A1, rv64::A2) == 0x08c5853b; |
|
| 586 | + | assert (try assembleSource("add.uw %a0 %a1 %a2;")).text[0] == 0x08c5853b; |
|
| 587 | + | assert mem::eq(printInstrText(0x08c5853b), "add.uw %a0, %a1, %a2"); |
|
| 588 | + | assert encode::sh1add(rv64::A0, rv64::A1, rv64::A2) == 0x20c5a533; |
|
| 589 | + | assert (try assembleSource("sh1add %a0 %a1 %a2;")).text[0] == 0x20c5a533; |
|
| 590 | + | assert mem::eq(printInstrText(0x20c5a533), "sh1add %a0, %a1, %a2"); |
|
| 591 | + | assert encode::sh2add(rv64::A0, rv64::A1, rv64::A2) == 0x20c5c533; |
|
| 592 | + | assert (try assembleSource("sh2add %a0 %a1 %a2;")).text[0] == 0x20c5c533; |
|
| 593 | + | assert mem::eq(printInstrText(0x20c5c533), "sh2add %a0, %a1, %a2"); |
|
| 594 | + | assert encode::sh3add(rv64::A0, rv64::A1, rv64::A2) == 0x20c5e533; |
|
| 595 | + | assert (try assembleSource("sh3add %a0 %a1 %a2;")).text[0] == 0x20c5e533; |
|
| 596 | + | assert mem::eq(printInstrText(0x20c5e533), "sh3add %a0, %a1, %a2"); |
|
| 597 | + | assert encode::sh1addUw(rv64::A0, rv64::A1, rv64::A2) == 0x20c5a53b; |
|
| 598 | + | assert (try assembleSource("sh1add.uw %a0 %a1 %a2;")).text[0] == 0x20c5a53b; |
|
| 599 | + | assert mem::eq(printInstrText(0x20c5a53b), "sh1add.uw %a0, %a1, %a2"); |
|
| 600 | + | assert encode::sh2addUw(rv64::A0, rv64::A1, rv64::A2) == 0x20c5c53b; |
|
| 601 | + | assert (try assembleSource("sh2add.uw %a0 %a1 %a2;")).text[0] == 0x20c5c53b; |
|
| 602 | + | assert mem::eq(printInstrText(0x20c5c53b), "sh2add.uw %a0, %a1, %a2"); |
|
| 603 | + | assert encode::sh3addUw(rv64::A0, rv64::A1, rv64::A2) == 0x20c5e53b; |
|
| 604 | + | assert (try assembleSource("sh3add.uw %a0 %a1 %a2;")).text[0] == 0x20c5e53b; |
|
| 605 | + | assert mem::eq(printInstrText(0x20c5e53b), "sh3add.uw %a0, %a1, %a2"); |
|
| 606 | + | assert encode::andn(rv64::A0, rv64::A1, rv64::A2) == 0x40c5f533; |
|
| 607 | + | assert (try assembleSource("andn %a0 %a1 %a2;")).text[0] == 0x40c5f533; |
|
| 608 | + | assert mem::eq(printInstrText(0x40c5f533), "andn %a0, %a1, %a2"); |
|
| 609 | + | assert encode::orn(rv64::A0, rv64::A1, rv64::A2) == 0x40c5e533; |
|
| 610 | + | assert (try assembleSource("orn %a0 %a1 %a2;")).text[0] == 0x40c5e533; |
|
| 611 | + | assert mem::eq(printInstrText(0x40c5e533), "orn %a0, %a1, %a2"); |
|
| 612 | + | assert encode::xnor(rv64::A0, rv64::A1, rv64::A2) == 0x40c5c533; |
|
| 613 | + | assert (try assembleSource("xnor %a0 %a1 %a2;")).text[0] == 0x40c5c533; |
|
| 614 | + | assert mem::eq(printInstrText(0x40c5c533), "xnor %a0, %a1, %a2"); |
|
| 615 | + | assert encode::min(rv64::A0, rv64::A1, rv64::A2) == 0x0ac5c533; |
|
| 616 | + | assert (try assembleSource("min %a0 %a1 %a2;")).text[0] == 0x0ac5c533; |
|
| 617 | + | assert mem::eq(printInstrText(0x0ac5c533), "min %a0, %a1, %a2"); |
|
| 618 | + | assert encode::minu(rv64::A0, rv64::A1, rv64::A2) == 0x0ac5d533; |
|
| 619 | + | assert (try assembleSource("minu %a0 %a1 %a2;")).text[0] == 0x0ac5d533; |
|
| 620 | + | assert mem::eq(printInstrText(0x0ac5d533), "minu %a0, %a1, %a2"); |
|
| 621 | + | assert encode::max(rv64::A0, rv64::A1, rv64::A2) == 0x0ac5e533; |
|
| 622 | + | assert (try assembleSource("max %a0 %a1 %a2;")).text[0] == 0x0ac5e533; |
|
| 623 | + | assert mem::eq(printInstrText(0x0ac5e533), "max %a0, %a1, %a2"); |
|
| 624 | + | assert encode::maxu(rv64::A0, rv64::A1, rv64::A2) == 0x0ac5f533; |
|
| 625 | + | assert (try assembleSource("maxu %a0 %a1 %a2;")).text[0] == 0x0ac5f533; |
|
| 626 | + | assert mem::eq(printInstrText(0x0ac5f533), "maxu %a0, %a1, %a2"); |
|
| 627 | + | assert encode::rol(rv64::A0, rv64::A1, rv64::A2) == 0x60c59533; |
|
| 628 | + | assert (try assembleSource("rol %a0 %a1 %a2;")).text[0] == 0x60c59533; |
|
| 629 | + | assert mem::eq(printInstrText(0x60c59533), "rol %a0, %a1, %a2"); |
|
| 630 | + | assert encode::ror(rv64::A0, rv64::A1, rv64::A2) == 0x60c5d533; |
|
| 631 | + | assert (try assembleSource("ror %a0 %a1 %a2;")).text[0] == 0x60c5d533; |
|
| 632 | + | assert mem::eq(printInstrText(0x60c5d533), "ror %a0, %a1, %a2"); |
|
| 633 | + | assert encode::rolw(rv64::A0, rv64::A1, rv64::A2) == 0x60c5953b; |
|
| 634 | + | assert (try assembleSource("rolw %a0 %a1 %a2;")).text[0] == 0x60c5953b; |
|
| 635 | + | assert mem::eq(printInstrText(0x60c5953b), "rolw %a0, %a1, %a2"); |
|
| 636 | + | assert encode::rorw(rv64::A0, rv64::A1, rv64::A2) == 0x60c5d53b; |
|
| 637 | + | assert (try assembleSource("rorw %a0 %a1 %a2;")).text[0] == 0x60c5d53b; |
|
| 638 | + | assert mem::eq(printInstrText(0x60c5d53b), "rorw %a0, %a1, %a2"); |
|
| 639 | + | assert encode::bclr(rv64::A0, rv64::A1, rv64::A2) == 0x48c59533; |
|
| 640 | + | assert (try assembleSource("bclr %a0 %a1 %a2;")).text[0] == 0x48c59533; |
|
| 641 | + | assert mem::eq(printInstrText(0x48c59533), "bclr %a0, %a1, %a2"); |
|
| 642 | + | assert encode::bext(rv64::A0, rv64::A1, rv64::A2) == 0x48c5d533; |
|
| 643 | + | assert (try assembleSource("bext %a0 %a1 %a2;")).text[0] == 0x48c5d533; |
|
| 644 | + | assert mem::eq(printInstrText(0x48c5d533), "bext %a0, %a1, %a2"); |
|
| 645 | + | assert encode::binv(rv64::A0, rv64::A1, rv64::A2) == 0x68c59533; |
|
| 646 | + | assert (try assembleSource("binv %a0 %a1 %a2;")).text[0] == 0x68c59533; |
|
| 647 | + | assert mem::eq(printInstrText(0x68c59533), "binv %a0, %a1, %a2"); |
|
| 648 | + | assert encode::bset(rv64::A0, rv64::A1, rv64::A2) == 0x28c59533; |
|
| 649 | + | assert (try assembleSource("bset %a0 %a1 %a2;")).text[0] == 0x28c59533; |
|
| 650 | + | assert mem::eq(printInstrText(0x28c59533), "bset %a0, %a1, %a2"); |
|
| 651 | + | assert encode::clz(rv64::A0, rv64::A1) == 0x60059513; |
|
| 652 | + | assert (try assembleSource("clz %a0 %a1;")).text[0] == 0x60059513; |
|
| 653 | + | assert mem::eq(printInstrText(0x60059513), "clz %a0, %a1"); |
|
| 654 | + | assert encode::ctz(rv64::A0, rv64::A1) == 0x60159513; |
|
| 655 | + | assert (try assembleSource("ctz %a0 %a1;")).text[0] == 0x60159513; |
|
| 656 | + | assert mem::eq(printInstrText(0x60159513), "ctz %a0, %a1"); |
|
| 657 | + | assert encode::cpop(rv64::A0, rv64::A1) == 0x60259513; |
|
| 658 | + | assert (try assembleSource("cpop %a0 %a1;")).text[0] == 0x60259513; |
|
| 659 | + | assert mem::eq(printInstrText(0x60259513), "cpop %a0, %a1"); |
|
| 660 | + | assert encode::clzw(rv64::A0, rv64::A1) == 0x6005951b; |
|
| 661 | + | assert (try assembleSource("clzw %a0 %a1;")).text[0] == 0x6005951b; |
|
| 662 | + | assert mem::eq(printInstrText(0x6005951b), "clzw %a0, %a1"); |
|
| 663 | + | assert encode::ctzw(rv64::A0, rv64::A1) == 0x6015951b; |
|
| 664 | + | assert (try assembleSource("ctzw %a0 %a1;")).text[0] == 0x6015951b; |
|
| 665 | + | assert mem::eq(printInstrText(0x6015951b), "ctzw %a0, %a1"); |
|
| 666 | + | assert encode::cpopw(rv64::A0, rv64::A1) == 0x6025951b; |
|
| 667 | + | assert (try assembleSource("cpopw %a0 %a1;")).text[0] == 0x6025951b; |
|
| 668 | + | assert mem::eq(printInstrText(0x6025951b), "cpopw %a0, %a1"); |
|
| 669 | + | assert encode::sextB(rv64::A0, rv64::A1) == 0x60459513; |
|
| 670 | + | assert (try assembleSource("sext.b %a0 %a1;")).text[0] == 0x60459513; |
|
| 671 | + | assert mem::eq(printInstrText(0x60459513), "sext.b %a0, %a1"); |
|
| 672 | + | assert encode::sextH(rv64::A0, rv64::A1) == 0x60559513; |
|
| 673 | + | assert (try assembleSource("sext.h %a0 %a1;")).text[0] == 0x60559513; |
|
| 674 | + | assert mem::eq(printInstrText(0x60559513), "sext.h %a0, %a1"); |
|
| 675 | + | assert encode::orcB(rv64::A0, rv64::A1) == 0x2875d513; |
|
| 676 | + | assert (try assembleSource("orc.b %a0 %a1;")).text[0] == 0x2875d513; |
|
| 677 | + | assert mem::eq(printInstrText(0x2875d513), "orc.b %a0, %a1"); |
|
| 678 | + | assert encode::rev8(rv64::A0, rv64::A1) == 0x6b85d513; |
|
| 679 | + | assert (try assembleSource("rev8 %a0 %a1;")).text[0] == 0x6b85d513; |
|
| 680 | + | assert mem::eq(printInstrText(0x6b85d513), "rev8 %a0, %a1"); |
|
| 681 | + | assert encode::zextH(rv64::A0, rv64::A1) == 0x0805c53b; |
|
| 682 | + | assert (try assembleSource("zext.h %a0 %a1;")).text[0] == 0x0805c53b; |
|
| 683 | + | assert mem::eq(printInstrText(0x0805c53b), "zext.h %a0, %a1"); |
|
| 684 | + | assert encode::slliUw(rv64::A0, rv64::A1, 63) == 0x0bf5951b; |
|
| 685 | + | assert (try assembleSource("slli.uw %a0 %a1 63;")).text[0] == 0x0bf5951b; |
|
| 686 | + | assert mem::eq(printInstrText(0x0bf5951b), "slli.uw %a0, %a1, 63"); |
|
| 687 | + | assert encode::rori(rv64::A0, rv64::A1, 63) == 0x63f5d513; |
|
| 688 | + | assert (try assembleSource("rori %a0 %a1 63;")).text[0] == 0x63f5d513; |
|
| 689 | + | assert mem::eq(printInstrText(0x63f5d513), "rori %a0, %a1, 63"); |
|
| 690 | + | assert encode::roriw(rv64::A0, rv64::A1, 31) == 0x61f5d51b; |
|
| 691 | + | assert (try assembleSource("roriw %a0 %a1 31;")).text[0] == 0x61f5d51b; |
|
| 692 | + | assert mem::eq(printInstrText(0x61f5d51b), "roriw %a0, %a1, 31"); |
|
| 693 | + | assert encode::bclri(rv64::A0, rv64::A1, 63) == 0x4bf59513; |
|
| 694 | + | assert (try assembleSource("bclri %a0 %a1 63;")).text[0] == 0x4bf59513; |
|
| 695 | + | assert mem::eq(printInstrText(0x4bf59513), "bclri %a0, %a1, 63"); |
|
| 696 | + | assert encode::bexti(rv64::A0, rv64::A1, 63) == 0x4bf5d513; |
|
| 697 | + | assert (try assembleSource("bexti %a0 %a1 63;")).text[0] == 0x4bf5d513; |
|
| 698 | + | assert mem::eq(printInstrText(0x4bf5d513), "bexti %a0, %a1, 63"); |
|
| 699 | + | assert encode::binvi(rv64::A0, rv64::A1, 63) == 0x6bf59513; |
|
| 700 | + | assert (try assembleSource("binvi %a0 %a1 63;")).text[0] == 0x6bf59513; |
|
| 701 | + | assert mem::eq(printInstrText(0x6bf59513), "binvi %a0, %a1, 63"); |
|
| 702 | + | assert encode::bseti(rv64::A0, rv64::A1, 63) == 0x2bf59513; |
|
| 703 | + | assert (try assembleSource("bseti %a0 %a1 63;")).text[0] == 0x2bf59513; |
|
| 704 | + | assert mem::eq(printInstrText(0x2bf59513), "bseti %a0, %a1, 63"); |
|
| 705 | + | } |
|
| 706 | + | ||
| 707 | + | /// Immediate bit indices and word rotations enforce their operand bounds. |
|
| 708 | + | @test unsafe fn bitManipulationBounds() throws (testing::TestError) { |
|
| 709 | + | try expectAssembleFail("slli.uw %a0 %a1 -1;"); |
|
| 710 | + | try expectAssembleFail("slli.uw %a0 %a1 64;"); |
|
| 711 | + | try expectAssembleFail("rori %a0 %a1 -1;"); |
|
| 712 | + | try expectAssembleFail("rori %a0 %a1 64;"); |
|
| 713 | + | try expectAssembleFail("roriw %a0 %a1 -1;"); |
|
| 714 | + | try expectAssembleFail("roriw %a0 %a1 32;"); |
|
| 715 | + | try expectAssembleFail("bclri %a0 %a1 -1;"); |
|
| 716 | + | try expectAssembleFail("bclri %a0 %a1 64;"); |
|
| 717 | + | try expectAssembleFail("bexti %a0 %a1 -1;"); |
|
| 718 | + | try expectAssembleFail("bexti %a0 %a1 64;"); |
|
| 719 | + | try expectAssembleFail("binvi %a0 %a1 -1;"); |
|
| 720 | + | try expectAssembleFail("binvi %a0 %a1 64;"); |
|
| 721 | + | try expectAssembleFail("bseti %a0 %a1 -1;"); |
|
| 722 | + | try expectAssembleFail("bseti %a0 %a1 64;"); |
|
| 723 | + | } |
lib/std/arch/rv64/decode.rad
+389 -0
| 103 | 103 | // Decoded Instruction // |
|
| 104 | 104 | ///////////////////////// |
|
| 105 | 105 | ||
| 106 | 106 | /// Decoded RV64 instruction. |
|
| 107 | 107 | export union Instr: Copy { |
|
| 108 | + | /// `add.uw` bit manipulation instruction. |
|
| 109 | + | AddUw { |
|
| 110 | + | /// Destination register. |
|
| 111 | + | rd: gen::Reg, |
|
| 112 | + | /// First source register. |
|
| 113 | + | rs1: gen::Reg, |
|
| 114 | + | /// Second source register. |
|
| 115 | + | rs2: gen::Reg, |
|
| 116 | + | }, |
|
| 117 | + | /// `sh1add` bit manipulation instruction. |
|
| 118 | + | Sh1add { |
|
| 119 | + | /// Destination register. |
|
| 120 | + | rd: gen::Reg, |
|
| 121 | + | /// First source register. |
|
| 122 | + | rs1: gen::Reg, |
|
| 123 | + | /// Second source register. |
|
| 124 | + | rs2: gen::Reg, |
|
| 125 | + | }, |
|
| 126 | + | /// `sh2add` bit manipulation instruction. |
|
| 127 | + | Sh2add { |
|
| 128 | + | /// Destination register. |
|
| 129 | + | rd: gen::Reg, |
|
| 130 | + | /// First source register. |
|
| 131 | + | rs1: gen::Reg, |
|
| 132 | + | /// Second source register. |
|
| 133 | + | rs2: gen::Reg, |
|
| 134 | + | }, |
|
| 135 | + | /// `sh3add` bit manipulation instruction. |
|
| 136 | + | Sh3add { |
|
| 137 | + | /// Destination register. |
|
| 138 | + | rd: gen::Reg, |
|
| 139 | + | /// First source register. |
|
| 140 | + | rs1: gen::Reg, |
|
| 141 | + | /// Second source register. |
|
| 142 | + | rs2: gen::Reg, |
|
| 143 | + | }, |
|
| 144 | + | /// `sh1add.uw` bit manipulation instruction. |
|
| 145 | + | Sh1addUw { |
|
| 146 | + | /// Destination register. |
|
| 147 | + | rd: gen::Reg, |
|
| 148 | + | /// First source register. |
|
| 149 | + | rs1: gen::Reg, |
|
| 150 | + | /// Second source register. |
|
| 151 | + | rs2: gen::Reg, |
|
| 152 | + | }, |
|
| 153 | + | /// `sh2add.uw` bit manipulation instruction. |
|
| 154 | + | Sh2addUw { |
|
| 155 | + | /// Destination register. |
|
| 156 | + | rd: gen::Reg, |
|
| 157 | + | /// First source register. |
|
| 158 | + | rs1: gen::Reg, |
|
| 159 | + | /// Second source register. |
|
| 160 | + | rs2: gen::Reg, |
|
| 161 | + | }, |
|
| 162 | + | /// `sh3add.uw` bit manipulation instruction. |
|
| 163 | + | Sh3addUw { |
|
| 164 | + | /// Destination register. |
|
| 165 | + | rd: gen::Reg, |
|
| 166 | + | /// First source register. |
|
| 167 | + | rs1: gen::Reg, |
|
| 168 | + | /// Second source register. |
|
| 169 | + | rs2: gen::Reg, |
|
| 170 | + | }, |
|
| 171 | + | /// `andn` bit manipulation instruction. |
|
| 172 | + | Andn { |
|
| 173 | + | /// Destination register. |
|
| 174 | + | rd: gen::Reg, |
|
| 175 | + | /// First source register. |
|
| 176 | + | rs1: gen::Reg, |
|
| 177 | + | /// Second source register. |
|
| 178 | + | rs2: gen::Reg, |
|
| 179 | + | }, |
|
| 180 | + | /// `orn` bit manipulation instruction. |
|
| 181 | + | Orn { |
|
| 182 | + | /// Destination register. |
|
| 183 | + | rd: gen::Reg, |
|
| 184 | + | /// First source register. |
|
| 185 | + | rs1: gen::Reg, |
|
| 186 | + | /// Second source register. |
|
| 187 | + | rs2: gen::Reg, |
|
| 188 | + | }, |
|
| 189 | + | /// `xnor` bit manipulation instruction. |
|
| 190 | + | Xnor { |
|
| 191 | + | /// Destination register. |
|
| 192 | + | rd: gen::Reg, |
|
| 193 | + | /// First source register. |
|
| 194 | + | rs1: gen::Reg, |
|
| 195 | + | /// Second source register. |
|
| 196 | + | rs2: gen::Reg, |
|
| 197 | + | }, |
|
| 198 | + | /// `min` bit manipulation instruction. |
|
| 199 | + | Min { |
|
| 200 | + | /// Destination register. |
|
| 201 | + | rd: gen::Reg, |
|
| 202 | + | /// First source register. |
|
| 203 | + | rs1: gen::Reg, |
|
| 204 | + | /// Second source register. |
|
| 205 | + | rs2: gen::Reg, |
|
| 206 | + | }, |
|
| 207 | + | /// `minu` bit manipulation instruction. |
|
| 208 | + | Minu { |
|
| 209 | + | /// Destination register. |
|
| 210 | + | rd: gen::Reg, |
|
| 211 | + | /// First source register. |
|
| 212 | + | rs1: gen::Reg, |
|
| 213 | + | /// Second source register. |
|
| 214 | + | rs2: gen::Reg, |
|
| 215 | + | }, |
|
| 216 | + | /// `max` bit manipulation instruction. |
|
| 217 | + | Max { |
|
| 218 | + | /// Destination register. |
|
| 219 | + | rd: gen::Reg, |
|
| 220 | + | /// First source register. |
|
| 221 | + | rs1: gen::Reg, |
|
| 222 | + | /// Second source register. |
|
| 223 | + | rs2: gen::Reg, |
|
| 224 | + | }, |
|
| 225 | + | /// `maxu` bit manipulation instruction. |
|
| 226 | + | Maxu { |
|
| 227 | + | /// Destination register. |
|
| 228 | + | rd: gen::Reg, |
|
| 229 | + | /// First source register. |
|
| 230 | + | rs1: gen::Reg, |
|
| 231 | + | /// Second source register. |
|
| 232 | + | rs2: gen::Reg, |
|
| 233 | + | }, |
|
| 234 | + | /// `rol` bit manipulation instruction. |
|
| 235 | + | Rol { |
|
| 236 | + | /// Destination register. |
|
| 237 | + | rd: gen::Reg, |
|
| 238 | + | /// First source register. |
|
| 239 | + | rs1: gen::Reg, |
|
| 240 | + | /// Second source register. |
|
| 241 | + | rs2: gen::Reg, |
|
| 242 | + | }, |
|
| 243 | + | /// `ror` bit manipulation instruction. |
|
| 244 | + | Ror { |
|
| 245 | + | /// Destination register. |
|
| 246 | + | rd: gen::Reg, |
|
| 247 | + | /// First source register. |
|
| 248 | + | rs1: gen::Reg, |
|
| 249 | + | /// Second source register. |
|
| 250 | + | rs2: gen::Reg, |
|
| 251 | + | }, |
|
| 252 | + | /// `rolw` bit manipulation instruction. |
|
| 253 | + | Rolw { |
|
| 254 | + | /// Destination register. |
|
| 255 | + | rd: gen::Reg, |
|
| 256 | + | /// First source register. |
|
| 257 | + | rs1: gen::Reg, |
|
| 258 | + | /// Second source register. |
|
| 259 | + | rs2: gen::Reg, |
|
| 260 | + | }, |
|
| 261 | + | /// `rorw` bit manipulation instruction. |
|
| 262 | + | Rorw { |
|
| 263 | + | /// Destination register. |
|
| 264 | + | rd: gen::Reg, |
|
| 265 | + | /// First source register. |
|
| 266 | + | rs1: gen::Reg, |
|
| 267 | + | /// Second source register. |
|
| 268 | + | rs2: gen::Reg, |
|
| 269 | + | }, |
|
| 270 | + | /// `bclr` bit manipulation instruction. |
|
| 271 | + | Bclr { |
|
| 272 | + | /// Destination register. |
|
| 273 | + | rd: gen::Reg, |
|
| 274 | + | /// First source register. |
|
| 275 | + | rs1: gen::Reg, |
|
| 276 | + | /// Second source register. |
|
| 277 | + | rs2: gen::Reg, |
|
| 278 | + | }, |
|
| 279 | + | /// `bext` bit manipulation instruction. |
|
| 280 | + | Bext { |
|
| 281 | + | /// Destination register. |
|
| 282 | + | rd: gen::Reg, |
|
| 283 | + | /// First source register. |
|
| 284 | + | rs1: gen::Reg, |
|
| 285 | + | /// Second source register. |
|
| 286 | + | rs2: gen::Reg, |
|
| 287 | + | }, |
|
| 288 | + | /// `binv` bit manipulation instruction. |
|
| 289 | + | Binv { |
|
| 290 | + | /// Destination register. |
|
| 291 | + | rd: gen::Reg, |
|
| 292 | + | /// First source register. |
|
| 293 | + | rs1: gen::Reg, |
|
| 294 | + | /// Second source register. |
|
| 295 | + | rs2: gen::Reg, |
|
| 296 | + | }, |
|
| 297 | + | /// `bset` bit manipulation instruction. |
|
| 298 | + | Bset { |
|
| 299 | + | /// Destination register. |
|
| 300 | + | rd: gen::Reg, |
|
| 301 | + | /// First source register. |
|
| 302 | + | rs1: gen::Reg, |
|
| 303 | + | /// Second source register. |
|
| 304 | + | rs2: gen::Reg, |
|
| 305 | + | }, |
|
| 306 | + | /// `clz` bit manipulation instruction. |
|
| 307 | + | Clz { |
|
| 308 | + | /// Destination register. |
|
| 309 | + | rd: gen::Reg, |
|
| 310 | + | /// First source register. |
|
| 311 | + | rs1: gen::Reg, |
|
| 312 | + | }, |
|
| 313 | + | /// `ctz` bit manipulation instruction. |
|
| 314 | + | Ctz { |
|
| 315 | + | /// Destination register. |
|
| 316 | + | rd: gen::Reg, |
|
| 317 | + | /// First source register. |
|
| 318 | + | rs1: gen::Reg, |
|
| 319 | + | }, |
|
| 320 | + | /// `cpop` bit manipulation instruction. |
|
| 321 | + | Cpop { |
|
| 322 | + | /// Destination register. |
|
| 323 | + | rd: gen::Reg, |
|
| 324 | + | /// First source register. |
|
| 325 | + | rs1: gen::Reg, |
|
| 326 | + | }, |
|
| 327 | + | /// `clzw` bit manipulation instruction. |
|
| 328 | + | Clzw { |
|
| 329 | + | /// Destination register. |
|
| 330 | + | rd: gen::Reg, |
|
| 331 | + | /// First source register. |
|
| 332 | + | rs1: gen::Reg, |
|
| 333 | + | }, |
|
| 334 | + | /// `ctzw` bit manipulation instruction. |
|
| 335 | + | Ctzw { |
|
| 336 | + | /// Destination register. |
|
| 337 | + | rd: gen::Reg, |
|
| 338 | + | /// First source register. |
|
| 339 | + | rs1: gen::Reg, |
|
| 340 | + | }, |
|
| 341 | + | /// `cpopw` bit manipulation instruction. |
|
| 342 | + | Cpopw { |
|
| 343 | + | /// Destination register. |
|
| 344 | + | rd: gen::Reg, |
|
| 345 | + | /// First source register. |
|
| 346 | + | rs1: gen::Reg, |
|
| 347 | + | }, |
|
| 348 | + | /// `sext.b` bit manipulation instruction. |
|
| 349 | + | SextB { |
|
| 350 | + | /// Destination register. |
|
| 351 | + | rd: gen::Reg, |
|
| 352 | + | /// First source register. |
|
| 353 | + | rs1: gen::Reg, |
|
| 354 | + | }, |
|
| 355 | + | /// `sext.h` bit manipulation instruction. |
|
| 356 | + | SextH { |
|
| 357 | + | /// Destination register. |
|
| 358 | + | rd: gen::Reg, |
|
| 359 | + | /// First source register. |
|
| 360 | + | rs1: gen::Reg, |
|
| 361 | + | }, |
|
| 362 | + | /// `orc.b` bit manipulation instruction. |
|
| 363 | + | OrcB { |
|
| 364 | + | /// Destination register. |
|
| 365 | + | rd: gen::Reg, |
|
| 366 | + | /// First source register. |
|
| 367 | + | rs1: gen::Reg, |
|
| 368 | + | }, |
|
| 369 | + | /// `rev8` bit manipulation instruction. |
|
| 370 | + | Rev8 { |
|
| 371 | + | /// Destination register. |
|
| 372 | + | rd: gen::Reg, |
|
| 373 | + | /// First source register. |
|
| 374 | + | rs1: gen::Reg, |
|
| 375 | + | }, |
|
| 376 | + | /// `zext.h` bit manipulation instruction. |
|
| 377 | + | ZextH { |
|
| 378 | + | /// Destination register. |
|
| 379 | + | rd: gen::Reg, |
|
| 380 | + | /// First source register. |
|
| 381 | + | rs1: gen::Reg, |
|
| 382 | + | }, |
|
| 383 | + | /// `slli.uw` bit manipulation instruction. |
|
| 384 | + | SlliUw { |
|
| 385 | + | /// Destination register. |
|
| 386 | + | rd: gen::Reg, |
|
| 387 | + | /// First source register. |
|
| 388 | + | rs1: gen::Reg, |
|
| 389 | + | /// Shift amount or bit index. |
|
| 390 | + | shamt: i32, |
|
| 391 | + | }, |
|
| 392 | + | /// `rori` bit manipulation instruction. |
|
| 393 | + | Rori { |
|
| 394 | + | /// Destination register. |
|
| 395 | + | rd: gen::Reg, |
|
| 396 | + | /// First source register. |
|
| 397 | + | rs1: gen::Reg, |
|
| 398 | + | /// Shift amount or bit index. |
|
| 399 | + | shamt: i32, |
|
| 400 | + | }, |
|
| 401 | + | /// `roriw` bit manipulation instruction. |
|
| 402 | + | Roriw { |
|
| 403 | + | /// Destination register. |
|
| 404 | + | rd: gen::Reg, |
|
| 405 | + | /// First source register. |
|
| 406 | + | rs1: gen::Reg, |
|
| 407 | + | /// Shift amount or bit index. |
|
| 408 | + | shamt: i32, |
|
| 409 | + | }, |
|
| 410 | + | /// `bclri` bit manipulation instruction. |
|
| 411 | + | Bclri { |
|
| 412 | + | /// Destination register. |
|
| 413 | + | rd: gen::Reg, |
|
| 414 | + | /// First source register. |
|
| 415 | + | rs1: gen::Reg, |
|
| 416 | + | /// Shift amount or bit index. |
|
| 417 | + | shamt: i32, |
|
| 418 | + | }, |
|
| 419 | + | /// `bexti` bit manipulation instruction. |
|
| 420 | + | Bexti { |
|
| 421 | + | /// Destination register. |
|
| 422 | + | rd: gen::Reg, |
|
| 423 | + | /// First source register. |
|
| 424 | + | rs1: gen::Reg, |
|
| 425 | + | /// Shift amount or bit index. |
|
| 426 | + | shamt: i32, |
|
| 427 | + | }, |
|
| 428 | + | /// `binvi` bit manipulation instruction. |
|
| 429 | + | Binvi { |
|
| 430 | + | /// Destination register. |
|
| 431 | + | rd: gen::Reg, |
|
| 432 | + | /// First source register. |
|
| 433 | + | rs1: gen::Reg, |
|
| 434 | + | /// Shift amount or bit index. |
|
| 435 | + | shamt: i32, |
|
| 436 | + | }, |
|
| 437 | + | /// `bseti` bit manipulation instruction. |
|
| 438 | + | Bseti { |
|
| 439 | + | /// Destination register. |
|
| 440 | + | rd: gen::Reg, |
|
| 441 | + | /// First source register. |
|
| 442 | + | rs1: gen::Reg, |
|
| 443 | + | /// Shift amount or bit index. |
|
| 444 | + | shamt: i32, |
|
| 445 | + | }, |
|
| 446 | + | ||
| 108 | 447 | // R-type ALU. |
|
| 109 | 448 | Add { rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg }, |
|
| 110 | 449 | Sub { rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg }, |
|
| 111 | 450 | Sll { rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg }, |
|
| 112 | 451 | Slt { rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg }, |
| 214 | 553 | let f7 = funct7(instr); |
|
| 215 | 554 | let rd = super::reg(rd(instr)); |
|
| 216 | 555 | let rs1 = super::reg(rs1(instr)); |
|
| 217 | 556 | let rs2 = super::reg(rs2(instr)); |
|
| 218 | 557 | ||
| 558 | + | // Match the fixed bits before the base ALU instruction forms. |
|
| 559 | + | match instr & 0xfe00707f { |
|
| 560 | + | case 0x0800003b => return Instr::AddUw { rd, rs1, rs2 }, |
|
| 561 | + | case 0x20002033 => return Instr::Sh1add { rd, rs1, rs2 }, |
|
| 562 | + | case 0x20004033 => return Instr::Sh2add { rd, rs1, rs2 }, |
|
| 563 | + | case 0x20006033 => return Instr::Sh3add { rd, rs1, rs2 }, |
|
| 564 | + | case 0x2000203b => return Instr::Sh1addUw { rd, rs1, rs2 }, |
|
| 565 | + | case 0x2000403b => return Instr::Sh2addUw { rd, rs1, rs2 }, |
|
| 566 | + | case 0x2000603b => return Instr::Sh3addUw { rd, rs1, rs2 }, |
|
| 567 | + | case 0x40007033 => return Instr::Andn { rd, rs1, rs2 }, |
|
| 568 | + | case 0x40006033 => return Instr::Orn { rd, rs1, rs2 }, |
|
| 569 | + | case 0x40004033 => return Instr::Xnor { rd, rs1, rs2 }, |
|
| 570 | + | case 0x0a004033 => return Instr::Min { rd, rs1, rs2 }, |
|
| 571 | + | case 0x0a005033 => return Instr::Minu { rd, rs1, rs2 }, |
|
| 572 | + | case 0x0a006033 => return Instr::Max { rd, rs1, rs2 }, |
|
| 573 | + | case 0x0a007033 => return Instr::Maxu { rd, rs1, rs2 }, |
|
| 574 | + | case 0x60001033 => return Instr::Rol { rd, rs1, rs2 }, |
|
| 575 | + | case 0x60005033 => return Instr::Ror { rd, rs1, rs2 }, |
|
| 576 | + | case 0x6000103b => return Instr::Rolw { rd, rs1, rs2 }, |
|
| 577 | + | case 0x6000503b => return Instr::Rorw { rd, rs1, rs2 }, |
|
| 578 | + | case 0x48001033 => return Instr::Bclr { rd, rs1, rs2 }, |
|
| 579 | + | case 0x48005033 => return Instr::Bext { rd, rs1, rs2 }, |
|
| 580 | + | case 0x68001033 => return Instr::Binv { rd, rs1, rs2 }, |
|
| 581 | + | case 0x28001033 => return Instr::Bset { rd, rs1, rs2 }, |
|
| 582 | + | case 0x6000501b => return Instr::Roriw { rd, rs1, shamt: ((instr >> 20) & 31) as i32 }, |
|
| 583 | + | else => {} |
|
| 584 | + | } |
|
| 585 | + | match instr & 0xfff0707f { |
|
| 586 | + | case 0x60001013 => return Instr::Clz { rd, rs1 }, |
|
| 587 | + | case 0x60101013 => return Instr::Ctz { rd, rs1 }, |
|
| 588 | + | case 0x60201013 => return Instr::Cpop { rd, rs1 }, |
|
| 589 | + | case 0x6000101b => return Instr::Clzw { rd, rs1 }, |
|
| 590 | + | case 0x6010101b => return Instr::Ctzw { rd, rs1 }, |
|
| 591 | + | case 0x6020101b => return Instr::Cpopw { rd, rs1 }, |
|
| 592 | + | case 0x60401013 => return Instr::SextB { rd, rs1 }, |
|
| 593 | + | case 0x60501013 => return Instr::SextH { rd, rs1 }, |
|
| 594 | + | case 0x28705013 => return Instr::OrcB { rd, rs1 }, |
|
| 595 | + | case 0x6b805013 => return Instr::Rev8 { rd, rs1 }, |
|
| 596 | + | case 0x0800403b => return Instr::ZextH { rd, rs1 }, |
|
| 597 | + | else => {} |
|
| 598 | + | } |
|
| 599 | + | match instr & 0xfc00707f { |
|
| 600 | + | case 0x0800101b => return Instr::SlliUw { rd, rs1, shamt: ((instr >> 20) & 63) as i32 }, |
|
| 601 | + | case 0x60005013 => return Instr::Rori { rd, rs1, shamt: ((instr >> 20) & 63) as i32 }, |
|
| 602 | + | case 0x48001013 => return Instr::Bclri { rd, rs1, shamt: ((instr >> 20) & 63) as i32 }, |
|
| 603 | + | case 0x48005013 => return Instr::Bexti { rd, rs1, shamt: ((instr >> 20) & 63) as i32 }, |
|
| 604 | + | case 0x68001013 => return Instr::Binvi { rd, rs1, shamt: ((instr >> 20) & 63) as i32 }, |
|
| 605 | + | case 0x28001013 => return Instr::Bseti { rd, rs1, shamt: ((instr >> 20) & 63) as i32 }, |
|
| 606 | + | else => {} |
|
| 607 | + | } |
|
| 219 | 608 | match op { |
|
| 220 | 609 | case encode::OP_AMO => { |
|
| 221 | 610 | if let atomic = atomics::decode(instr) { |
|
| 222 | 611 | return Instr::Atomic(atomic); |
|
| 223 | 612 | } |
lib/std/arch/rv64/encode.rad
+215 -1
| 1 | - | //! RISC-V RV64I+M instruction encoding. |
|
| 1 | + | //! RISC-V RV64I+M+Zba+Zbb+Zbs instruction encoding. |
|
| 2 | 2 | //! |
|
| 3 | 3 | //! Provides type-safe functions for encoding RV64 instructions. |
|
| 4 | 4 | ||
| 5 | 5 | use std::lang::gen; |
|
| 6 | 6 |
| 742 | 742 | ||
| 743 | 743 | /// Synchronize subsequent instruction fetch on the executing hart. |
|
| 744 | 744 | export fn fenceI() -> u32 { |
|
| 745 | 745 | return FENCE_I; |
|
| 746 | 746 | } |
|
| 747 | + | ||
| 748 | + | // Bit manipulation instructions. |
|
| 749 | + | ||
| 750 | + | /// Encode `add.uw` for RV64. |
|
| 751 | + | export fn addUw(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 752 | + | return encodeR(OP_OP32, rd, rs1, rs2, 0, 0x04); |
|
| 753 | + | } |
|
| 754 | + | ||
| 755 | + | /// Encode `sh1add` for RV64. |
|
| 756 | + | export fn sh1add(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 757 | + | return encodeR(OP_OP, rd, rs1, rs2, 2, 0x10); |
|
| 758 | + | } |
|
| 759 | + | ||
| 760 | + | /// Encode `sh2add` for RV64. |
|
| 761 | + | export fn sh2add(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 762 | + | return encodeR(OP_OP, rd, rs1, rs2, 4, 0x10); |
|
| 763 | + | } |
|
| 764 | + | ||
| 765 | + | /// Encode `sh3add` for RV64. |
|
| 766 | + | export fn sh3add(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 767 | + | return encodeR(OP_OP, rd, rs1, rs2, 6, 0x10); |
|
| 768 | + | } |
|
| 769 | + | ||
| 770 | + | /// Encode `sh1add.uw` for RV64. |
|
| 771 | + | export fn sh1addUw(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 772 | + | return encodeR(OP_OP32, rd, rs1, rs2, 2, 0x10); |
|
| 773 | + | } |
|
| 774 | + | ||
| 775 | + | /// Encode `sh2add.uw` for RV64. |
|
| 776 | + | export fn sh2addUw(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 777 | + | return encodeR(OP_OP32, rd, rs1, rs2, 4, 0x10); |
|
| 778 | + | } |
|
| 779 | + | ||
| 780 | + | /// Encode `sh3add.uw` for RV64. |
|
| 781 | + | export fn sh3addUw(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 782 | + | return encodeR(OP_OP32, rd, rs1, rs2, 6, 0x10); |
|
| 783 | + | } |
|
| 784 | + | ||
| 785 | + | /// Encode `andn` for RV64. |
|
| 786 | + | export fn andn(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 787 | + | return encodeR(OP_OP, rd, rs1, rs2, 7, 0x20); |
|
| 788 | + | } |
|
| 789 | + | ||
| 790 | + | /// Encode `orn` for RV64. |
|
| 791 | + | export fn orn(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 792 | + | return encodeR(OP_OP, rd, rs1, rs2, 6, 0x20); |
|
| 793 | + | } |
|
| 794 | + | ||
| 795 | + | /// Encode `xnor` for RV64. |
|
| 796 | + | export fn xnor(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 797 | + | return encodeR(OP_OP, rd, rs1, rs2, 4, 0x20); |
|
| 798 | + | } |
|
| 799 | + | ||
| 800 | + | /// Encode `min` for RV64. |
|
| 801 | + | export fn min(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 802 | + | return encodeR(OP_OP, rd, rs1, rs2, 4, 0x05); |
|
| 803 | + | } |
|
| 804 | + | ||
| 805 | + | /// Encode `minu` for RV64. |
|
| 806 | + | export fn minu(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 807 | + | return encodeR(OP_OP, rd, rs1, rs2, 5, 0x05); |
|
| 808 | + | } |
|
| 809 | + | ||
| 810 | + | /// Encode `max` for RV64. |
|
| 811 | + | export fn max(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 812 | + | return encodeR(OP_OP, rd, rs1, rs2, 6, 0x05); |
|
| 813 | + | } |
|
| 814 | + | ||
| 815 | + | /// Encode `maxu` for RV64. |
|
| 816 | + | export fn maxu(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 817 | + | return encodeR(OP_OP, rd, rs1, rs2, 7, 0x05); |
|
| 818 | + | } |
|
| 819 | + | ||
| 820 | + | /// Encode `rol` for RV64. |
|
| 821 | + | export fn rol(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 822 | + | return encodeR(OP_OP, rd, rs1, rs2, 1, 0x30); |
|
| 823 | + | } |
|
| 824 | + | ||
| 825 | + | /// Encode `ror` for RV64. |
|
| 826 | + | export fn ror(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 827 | + | return encodeR(OP_OP, rd, rs1, rs2, 5, 0x30); |
|
| 828 | + | } |
|
| 829 | + | ||
| 830 | + | /// Encode `rolw` for RV64. |
|
| 831 | + | export fn rolw(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 832 | + | return encodeR(OP_OP32, rd, rs1, rs2, 1, 0x30); |
|
| 833 | + | } |
|
| 834 | + | ||
| 835 | + | /// Encode `rorw` for RV64. |
|
| 836 | + | export fn rorw(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 837 | + | return encodeR(OP_OP32, rd, rs1, rs2, 5, 0x30); |
|
| 838 | + | } |
|
| 839 | + | ||
| 840 | + | /// Encode `bclr` for RV64. |
|
| 841 | + | export fn bclr(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 842 | + | return encodeR(OP_OP, rd, rs1, rs2, 1, 0x24); |
|
| 843 | + | } |
|
| 844 | + | ||
| 845 | + | /// Encode `bext` for RV64. |
|
| 846 | + | export fn bext(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 847 | + | return encodeR(OP_OP, rd, rs1, rs2, 5, 0x24); |
|
| 848 | + | } |
|
| 849 | + | ||
| 850 | + | /// Encode `binv` for RV64. |
|
| 851 | + | export fn binv(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 852 | + | return encodeR(OP_OP, rd, rs1, rs2, 1, 0x34); |
|
| 853 | + | } |
|
| 854 | + | ||
| 855 | + | /// Encode `bset` for RV64. |
|
| 856 | + | export fn bset(rd: gen::Reg, rs1: gen::Reg, rs2: gen::Reg) -> u32 { |
|
| 857 | + | return encodeR(OP_OP, rd, rs1, rs2, 1, 0x14); |
|
| 858 | + | } |
|
| 859 | + | ||
| 860 | + | /// Encode `clz` for RV64. |
|
| 861 | + | export fn clz(rd: gen::Reg, rs1: gen::Reg) -> u32 { |
|
| 862 | + | return encodeI(OP_IMM, rd, rs1, 1, 0x600); |
|
| 863 | + | } |
|
| 864 | + | ||
| 865 | + | /// Encode `ctz` for RV64. |
|
| 866 | + | export fn ctz(rd: gen::Reg, rs1: gen::Reg) -> u32 { |
|
| 867 | + | return encodeI(OP_IMM, rd, rs1, 1, 0x601); |
|
| 868 | + | } |
|
| 869 | + | ||
| 870 | + | /// Encode `cpop` for RV64. |
|
| 871 | + | export fn cpop(rd: gen::Reg, rs1: gen::Reg) -> u32 { |
|
| 872 | + | return encodeI(OP_IMM, rd, rs1, 1, 0x602); |
|
| 873 | + | } |
|
| 874 | + | ||
| 875 | + | /// Encode `clzw` for RV64. |
|
| 876 | + | export fn clzw(rd: gen::Reg, rs1: gen::Reg) -> u32 { |
|
| 877 | + | return encodeI(OP_IMM32, rd, rs1, 1, 0x600); |
|
| 878 | + | } |
|
| 879 | + | ||
| 880 | + | /// Encode `ctzw` for RV64. |
|
| 881 | + | export fn ctzw(rd: gen::Reg, rs1: gen::Reg) -> u32 { |
|
| 882 | + | return encodeI(OP_IMM32, rd, rs1, 1, 0x601); |
|
| 883 | + | } |
|
| 884 | + | ||
| 885 | + | /// Encode `cpopw` for RV64. |
|
| 886 | + | export fn cpopw(rd: gen::Reg, rs1: gen::Reg) -> u32 { |
|
| 887 | + | return encodeI(OP_IMM32, rd, rs1, 1, 0x602); |
|
| 888 | + | } |
|
| 889 | + | ||
| 890 | + | /// Encode `sext.b` for RV64. |
|
| 891 | + | export fn sextB(rd: gen::Reg, rs1: gen::Reg) -> u32 { |
|
| 892 | + | return encodeI(OP_IMM, rd, rs1, 1, 0x604); |
|
| 893 | + | } |
|
| 894 | + | ||
| 895 | + | /// Encode `sext.h` for RV64. |
|
| 896 | + | export fn sextH(rd: gen::Reg, rs1: gen::Reg) -> u32 { |
|
| 897 | + | return encodeI(OP_IMM, rd, rs1, 1, 0x605); |
|
| 898 | + | } |
|
| 899 | + | ||
| 900 | + | /// Encode `orc.b` for RV64. |
|
| 901 | + | export fn orcB(rd: gen::Reg, rs1: gen::Reg) -> u32 { |
|
| 902 | + | return encodeI(OP_IMM, rd, rs1, 5, 0x287); |
|
| 903 | + | } |
|
| 904 | + | ||
| 905 | + | /// Encode `rev8` for RV64. |
|
| 906 | + | export fn rev8(rd: gen::Reg, rs1: gen::Reg) -> u32 { |
|
| 907 | + | return encodeI(OP_IMM, rd, rs1, 5, 0x6b8); |
|
| 908 | + | } |
|
| 909 | + | ||
| 910 | + | /// Encode `zext.h` for RV64. |
|
| 911 | + | export fn zextH(rd: gen::Reg, rs1: gen::Reg) -> u32 { |
|
| 912 | + | return encodeR(OP_OP32, rd, rs1, super::ZERO, 4, 0x04); |
|
| 913 | + | } |
|
| 914 | + | ||
| 915 | + | /// Encode `slli.uw` for RV64. |
|
| 916 | + | export fn slliUw(rd: gen::Reg, rs1: gen::Reg, shamt: i32) -> u32 { |
|
| 917 | + | assert shamt >= 0 and shamt < 64; |
|
| 918 | + | return encodeI(OP_IMM32, rd, rs1, 1, 0x080 | shamt); |
|
| 919 | + | } |
|
| 920 | + | ||
| 921 | + | /// Encode `rori` for RV64. |
|
| 922 | + | export fn rori(rd: gen::Reg, rs1: gen::Reg, shamt: i32) -> u32 { |
|
| 923 | + | assert shamt >= 0 and shamt < 64; |
|
| 924 | + | return encodeI(OP_IMM, rd, rs1, 5, 0x600 | shamt); |
|
| 925 | + | } |
|
| 926 | + | ||
| 927 | + | /// Encode `roriw` for RV64. |
|
| 928 | + | export fn roriw(rd: gen::Reg, rs1: gen::Reg, shamt: i32) -> u32 { |
|
| 929 | + | assert shamt >= 0 and shamt < 32; |
|
| 930 | + | return encodeI(OP_IMM32, rd, rs1, 5, 0x600 | shamt); |
|
| 931 | + | } |
|
| 932 | + | ||
| 933 | + | /// Encode `bclri` for RV64. |
|
| 934 | + | export fn bclri(rd: gen::Reg, rs1: gen::Reg, shamt: i32) -> u32 { |
|
| 935 | + | assert shamt >= 0 and shamt < 64; |
|
| 936 | + | return encodeI(OP_IMM, rd, rs1, 1, 0x480 | shamt); |
|
| 937 | + | } |
|
| 938 | + | ||
| 939 | + | /// Encode `bexti` for RV64. |
|
| 940 | + | export fn bexti(rd: gen::Reg, rs1: gen::Reg, shamt: i32) -> u32 { |
|
| 941 | + | assert shamt >= 0 and shamt < 64; |
|
| 942 | + | return encodeI(OP_IMM, rd, rs1, 5, 0x480 | shamt); |
|
| 943 | + | } |
|
| 944 | + | ||
| 945 | + | /// Encode `binvi` for RV64. |
|
| 946 | + | export fn binvi(rd: gen::Reg, rs1: gen::Reg, shamt: i32) -> u32 { |
|
| 947 | + | assert shamt >= 0 and shamt < 64; |
|
| 948 | + | return encodeI(OP_IMM, rd, rs1, 1, 0x680 | shamt); |
|
| 949 | + | } |
|
| 950 | + | ||
| 951 | + | /// Encode `bseti` for RV64. |
|
| 952 | + | export fn bseti(rd: gen::Reg, rs1: gen::Reg, shamt: i32) -> u32 { |
|
| 953 | + | assert shamt >= 0 and shamt < 64; |
|
| 954 | + | return encodeI(OP_IMM, rd, rs1, 1, 0x280 | shamt); |
|
| 955 | + | } |
|
| 956 | + | ||
| 957 | + | /// Zero-extend the low word of `rs1` to 64 bits. |
|
| 958 | + | export fn zextW(rd: gen::Reg, rs1: gen::Reg) -> u32 { |
|
| 959 | + | return addUw(rd, rs1, super::ZERO); |
|
| 960 | + | } |
lib/std/arch/rv64/isel.rad
+486 -43
| 45 | 45 | ||
| 46 | 46 | /////////////// |
|
| 47 | 47 | // Constants // |
|
| 48 | 48 | /////////////// |
|
| 49 | 49 | ||
| 50 | - | /// Shift amount for byte sign/zero extension. |
|
| 51 | - | constant SHIFT_W8: i32 = 64 - 8; |
|
| 52 | - | /// Shift amount for halfword sign/zero extension. |
|
| 53 | - | constant SHIFT_W16: i32 = 64 - 16; |
|
| 54 | - | /// Shift amount for word sign/zero extension. |
|
| 55 | - | constant SHIFT_W32: i32 = 64 - 32; |
|
| 56 | 50 | /// Mask for extracting byte value. |
|
| 57 | 51 | constant MASK_W8: i32 = 0xFF; |
|
| 58 | 52 | /// Maximum number of block arguments supported. |
|
| 59 | 53 | constant MAX_BLOCK_ARGS: u32 = 16; |
|
| 60 | 54 |
| 703 | 697 | throws (super::Error) |
|
| 704 | 698 | { |
|
| 705 | 699 | match typ { |
|
| 706 | 700 | case il::Type::W8 => try emit::emit(e, encode::andi(rd, rs, MASK_W8)), |
|
| 707 | 701 | case il::Type::W16 => { |
|
| 708 | - | try emit::emit(e, encode::slli(rd, rs, SHIFT_W16)); |
|
| 709 | - | try emit::emit(e, encode::srli(rd, rd, SHIFT_W16)); |
|
| 702 | + | try emit::emit(e, encode::zextH(rd, rs)); |
|
| 710 | 703 | } |
|
| 711 | 704 | case il::Type::W32 => { |
|
| 712 | - | try emit::emit(e, encode::slli(rd, rs, SHIFT_W32)); |
|
| 713 | - | try emit::emit(e, encode::srli(rd, rd, SHIFT_W32)); |
|
| 705 | + | try emit::emit(e, encode::zextW(rd, rs)); |
|
| 714 | 706 | } |
|
| 715 | 707 | case il::Type::W64 => {} |
|
| 716 | 708 | } |
|
| 717 | 709 | } |
|
| 718 | 710 |
| 720 | 712 | fn emitSext(e: &mut emit::Emitter, rd: gen::Reg, rs: gen::Reg, typ: il::Type) |
|
| 721 | 713 | throws (super::Error) |
|
| 722 | 714 | { |
|
| 723 | 715 | match typ { |
|
| 724 | 716 | case il::Type::W8 => { |
|
| 725 | - | try emit::emit(e, encode::slli(rd, rs, SHIFT_W8)); |
|
| 726 | - | try emit::emit(e, encode::srai(rd, rd, SHIFT_W8)); |
|
| 717 | + | try emit::emit(e, encode::sextB(rd, rs)); |
|
| 727 | 718 | } |
|
| 728 | 719 | case il::Type::W16 => { |
|
| 729 | - | try emit::emit(e, encode::slli(rd, rs, SHIFT_W16)); |
|
| 730 | - | try emit::emit(e, encode::srai(rd, rd, SHIFT_W16)); |
|
| 720 | + | try emit::emit(e, encode::sextH(rd, rs)); |
|
| 731 | 721 | } |
|
| 732 | 722 | case il::Type::W32 => { |
|
| 733 | 723 | try emit::emit(e, encode::addiw(rd, rs, 0)); |
|
| 734 | 724 | } |
|
| 735 | 725 | case il::Type::W64 => {} |
| 796 | 786 | let rs = try resolveVal(s, e, scratch, val); |
|
| 797 | 787 | if isExtended(s, val, il::Type::W32, false) { |
|
| 798 | 788 | try emitMv(s, e, rd, rs); |
|
| 799 | 789 | return; |
|
| 800 | 790 | } |
|
| 801 | - | try emit::emit(e, encode::slli(rd, rs, SHIFT_W32)); |
|
| 802 | - | try emit::emit(e, encode::srli(rd, rd, SHIFT_W32)); |
|
| 791 | + | try emit::emit(e, encode::zextW(rd, rs)); |
|
| 803 | 792 | } |
|
| 804 | 793 | ||
| 805 | 794 | /// Resolve the word `index` of a bounds check into a register. Arrays and slices are |
|
| 806 | 795 | /// shorter than 2^31 elements, so a register that holds the sign or zero extension of |
|
| 807 | 796 | /// the index is used as it is. Other indices are zero-extended into [`super::SCRATCH1`]. |
| 931 | 920 | try emit::emit(e, encode::mv(rd, rs)); |
|
| 932 | 921 | } |
|
| 933 | 922 | } else { |
|
| 934 | 923 | try emit::emit(e, encode::slli(rd, rs, log2(size))); |
|
| 935 | 924 | } |
|
| 925 | + | } else if size == 3 { |
|
| 926 | + | try emit::emit(e, encode::sh1add(rd, rs, rs)); |
|
| 927 | + | } else if size == 5 { |
|
| 928 | + | try emit::emit(e, encode::sh2add(rd, rs, rs)); |
|
| 929 | + | } else if size == 9 { |
|
| 930 | + | try emit::emit(e, encode::sh3add(rd, rs, rs)); |
|
| 936 | 931 | } else { |
|
| 937 | 932 | try emit::loadImm(e, tmp, size as i64); |
|
| 938 | 933 | try emit::emit(e, encode::mul(rd, rs, tmp)); |
|
| 939 | 934 | } |
|
| 940 | 935 | } |
|
| 941 | 936 | ||
| 937 | + | /// Add an element offset to a base address. `tmp` must differ from `base`. |
|
| 938 | + | /// `scaleTmp` must differ from both inputs when the size is not a power of two. |
|
| 939 | + | fn emitAddress( |
|
| 940 | + | e: &mut emit::Emitter, |
|
| 941 | + | rd: gen::Reg, |
|
| 942 | + | base: gen::Reg, |
|
| 943 | + | index: gen::Reg, |
|
| 944 | + | size: u32, |
|
| 945 | + | tmp: gen::Reg, |
|
| 946 | + | scaleTmp: gen::Reg |
|
| 947 | + | ) throws (super::Error) { |
|
| 948 | + | match size { |
|
| 949 | + | case 1 => try emit::emit(e, encode::add(rd, index, base)), |
|
| 950 | + | case 2 => try emit::emit(e, encode::sh1add(rd, index, base)), |
|
| 951 | + | case 4 => try emit::emit(e, encode::sh2add(rd, index, base)), |
|
| 952 | + | case 8 => try emit::emit(e, encode::sh3add(rd, index, base)), |
|
| 953 | + | else => { |
|
| 954 | + | try emitScale(e, tmp, index, size, scaleTmp); |
|
| 955 | + | try emit::emit(e, encode::add(rd, base, tmp)); |
|
| 956 | + | } |
|
| 957 | + | } |
|
| 958 | + | } |
|
| 959 | + | ||
| 942 | 960 | /// Number of trailing zero bits of the power of two `n`. |
|
| 943 | 961 | fn log2(n: u32) -> i32 { |
|
| 944 | 962 | let mut shift: i32 = 0; |
|
| 945 | 963 | while (n >> shift as u32) > 1 { |
|
| 946 | 964 | set shift += 1; |
| 1333 | 1351 | } |
|
| 1334 | 1352 | } |
|
| 1335 | 1353 | return offset; |
|
| 1336 | 1354 | } |
|
| 1337 | 1355 | ||
| 1356 | + | /// Number of uses of each SSA register. |
|
| 1357 | + | record UseCounts { |
|
| 1358 | + | /// Counts indexed by SSA register number. |
|
| 1359 | + | counts: *mut [u32], |
|
| 1360 | + | } |
|
| 1361 | + | ||
| 1362 | + | instance il::RegVisitor for UseCounts { |
|
| 1363 | + | /// Count one operand occurrence. |
|
| 1364 | + | fn (c: &mut UseCounts) visit(reg: il::Reg) { |
|
| 1365 | + | set c.counts[reg.n] += 1; |
|
| 1366 | + | } |
|
| 1367 | + | } |
|
| 1368 | + | ||
| 1369 | + | /// Machine operand form of a combined expression. |
|
| 1370 | + | union CombinedEncoder: Copy { |
|
| 1371 | + | /// Two source registers. |
|
| 1372 | + | Registers { |
|
| 1373 | + | /// Encoder for the destination and both sources. |
|
| 1374 | + | enc: fn(gen::Reg, gen::Reg, gen::Reg) -> u32, |
|
| 1375 | + | /// Second machine operand. |
|
| 1376 | + | b: il::Val, |
|
| 1377 | + | }, |
|
| 1378 | + | /// Source register and immediate bit index or shift. |
|
| 1379 | + | Immediate { |
|
| 1380 | + | /// Encoder for the destination, source, and immediate. |
|
| 1381 | + | enc: fn(gen::Reg, gen::Reg, i32) -> u32, |
|
| 1382 | + | /// Immediate bit index or shift. |
|
| 1383 | + | imm: i32, |
|
| 1384 | + | }, |
|
| 1385 | + | } |
|
| 1386 | + | ||
| 1387 | + | /// Adjacent IL instructions with one equivalent machine instruction. |
|
| 1388 | + | record Combined: Copy { |
|
| 1389 | + | /// Number of IL instructions consumed. |
|
| 1390 | + | count: u32, |
|
| 1391 | + | /// Result register of the last instruction. |
|
| 1392 | + | dst: il::Reg, |
|
| 1393 | + | /// First machine operand. |
|
| 1394 | + | a: il::Val, |
|
| 1395 | + | /// Machine instruction encoder. |
|
| 1396 | + | encoder: CombinedEncoder, |
|
| 1397 | + | } |
|
| 1398 | + | ||
| 1399 | + | /// Return the other operand when `value` occurs in a commutative operation. |
|
| 1400 | + | fn otherOperand(a: il::Val, b: il::Val, value: il::Reg) -> ?il::Val { |
|
| 1401 | + | if a == il::Val::Reg(value) { |
|
| 1402 | + | return b; |
|
| 1403 | + | } |
|
| 1404 | + | if b == il::Val::Reg(value) { |
|
| 1405 | + | return a; |
|
| 1406 | + | } |
|
| 1407 | + | return nil; |
|
| 1408 | + | } |
|
| 1409 | + | ||
| 1410 | + | /// Match two shifts whose counts are complementary in the register width. |
|
| 1411 | + | fn rotation(instrs: &[il::Instr], uses: &[u32]) -> ?Combined { |
|
| 1412 | + | if instrs.len < 4 { |
|
| 1413 | + | return nil; |
|
| 1414 | + | } |
|
| 1415 | + | for subIndex in 0 .. 2 { |
|
| 1416 | + | let case il::Instr::BinOp { |
|
| 1417 | + | op: il::BinOp::Sub, typ, dst: complement, a: il::Val::Imm(width), b: count |
|
| 1418 | + | } = |
|
| 1419 | + | instrs[subIndex] else { |
|
| 1420 | + | continue; |
|
| 1421 | + | }; |
|
| 1422 | + | if uses[complement.n] <> 1 or not ((typ == il::Type::W64 and width == 64) |
|
| 1423 | + | or (typ == il::Type::W32 and width == 32)) |
|
| 1424 | + | { |
|
| 1425 | + | continue; |
|
| 1426 | + | } |
|
| 1427 | + | let shiftIndex: u32 = 1 if subIndex == 0 else 0; |
|
| 1428 | + | let case il::Instr::BinOp { |
|
| 1429 | + | op: firstOp, typ: firstType, dst: first, a: source, b: firstCount |
|
| 1430 | + | } = |
|
| 1431 | + | instrs[shiftIndex] else { |
|
| 1432 | + | continue; |
|
| 1433 | + | }; |
|
| 1434 | + | let case il::Instr::BinOp { |
|
| 1435 | + | op: secondOp, typ: secondType, dst: second, a: otherSource, b: secondCount |
|
| 1436 | + | } = |
|
| 1437 | + | instrs[2] else { |
|
| 1438 | + | continue; |
|
| 1439 | + | }; |
|
| 1440 | + | if firstType <> typ |
|
| 1441 | + | or secondType <> typ |
|
| 1442 | + | or source <> otherSource |
|
| 1443 | + | or uses[first.n] <> 1 |
|
| 1444 | + | or uses[second.n] <> 1 |
|
| 1445 | + | { |
|
| 1446 | + | continue; |
|
| 1447 | + | } |
|
| 1448 | + | let opposite = (firstOp == il::BinOp::Shl and secondOp == il::BinOp::Ushr) |
|
| 1449 | + | or (firstOp == il::BinOp::Ushr and secondOp == il::BinOp::Shl); |
|
| 1450 | + | let counts = (firstCount == count and secondCount == il::Val::Reg(complement)) |
|
| 1451 | + | or (secondCount == count and firstCount == il::Val::Reg(complement)); |
|
| 1452 | + | if not opposite or not counts { |
|
| 1453 | + | continue; |
|
| 1454 | + | } |
|
| 1455 | + | let case il::Instr::BinOp { |
|
| 1456 | + | op: il::BinOp::Or, typ: outType, dst, a, b |
|
| 1457 | + | } = |
|
| 1458 | + | instrs[3] else { |
|
| 1459 | + | continue; |
|
| 1460 | + | }; |
|
| 1461 | + | let other = otherOperand(a, b, first) else { |
|
| 1462 | + | continue; |
|
| 1463 | + | }; |
|
| 1464 | + | if outType <> typ or other <> il::Val::Reg(second) { |
|
| 1465 | + | continue; |
|
| 1466 | + | } |
|
| 1467 | + | let left = firstOp == il::BinOp::Shl == (firstCount == count); |
|
| 1468 | + | let enc = (encode::rolw if left else encode::rorw) |
|
| 1469 | + | if typ == il::Type::W32 |
|
| 1470 | + | else encode::rol if left else encode::ror; |
|
| 1471 | + | return Combined { |
|
| 1472 | + | count: 4, dst, a: source, encoder: CombinedEncoder::Registers { enc, b: count } |
|
| 1473 | + | }; |
|
| 1474 | + | } |
|
| 1475 | + | return nil; |
|
| 1476 | + | } |
|
| 1477 | + | ||
| 1478 | + | /// Match adjacent expressions whose intermediate values each have one use. |
|
| 1479 | + | /// The combined instruction is emitted at the first definition. All source |
|
| 1480 | + | /// registers are still live there, and the final destination can be written. |
|
| 1481 | + | fn combination(instrs: &[il::Instr], uses: &[u32]) -> ?Combined { |
|
| 1482 | + | if let combined = rotation(instrs, uses) { |
|
| 1483 | + | return combined; |
|
| 1484 | + | } |
|
| 1485 | + | if instrs.len < 2 { |
|
| 1486 | + | return nil; |
|
| 1487 | + | } |
|
| 1488 | + | let first = instrs[0]; |
|
| 1489 | + | let second = instrs[1]; |
|
| 1490 | + | if let case il::Instr::UnOp { op: il::UnOp::Not, typ, dst: temp, a: inverted } = first { |
|
| 1491 | + | if uses[temp.n] <> 1 { |
|
| 1492 | + | return nil; |
|
| 1493 | + | } |
|
| 1494 | + | if let case il::Instr::BinOp { op, typ: outType, dst, a, b } = second { |
|
| 1495 | + | let other = otherOperand(a, b, temp) else { |
|
| 1496 | + | return nil; |
|
| 1497 | + | }; |
|
| 1498 | + | if typ <> outType { |
|
| 1499 | + | return nil; |
|
| 1500 | + | } |
|
| 1501 | + | let mut enc = encode::andn; |
|
| 1502 | + | match op { |
|
| 1503 | + | case il::BinOp::And => {} |
|
| 1504 | + | case il::BinOp::Or => set enc = encode::orn, |
|
| 1505 | + | case il::BinOp::Xor => set enc = encode::xnor, |
|
| 1506 | + | else => return nil, |
|
| 1507 | + | } |
|
| 1508 | + | return Combined { |
|
| 1509 | + | count: 2, dst, a: other, encoder: CombinedEncoder::Registers { enc, b: inverted } |
|
| 1510 | + | }; |
|
| 1511 | + | } |
|
| 1512 | + | } |
|
| 1513 | + | if let case il::Instr::Zext { typ: il::Type::W32, dst: temp, val } = first { |
|
| 1514 | + | let case il::Instr::BinOp { op, typ: il::Type::W64, dst, a, b } = second else { |
|
| 1515 | + | return nil; |
|
| 1516 | + | }; |
|
| 1517 | + | if uses[temp.n] <> 1 { |
|
| 1518 | + | return nil; |
|
| 1519 | + | } |
|
| 1520 | + | if op == il::BinOp::Add { |
|
| 1521 | + | let other = otherOperand(a, b, temp) else { |
|
| 1522 | + | return nil; |
|
| 1523 | + | }; |
|
| 1524 | + | return Combined { |
|
| 1525 | + | count: 2, |
|
| 1526 | + | dst, |
|
| 1527 | + | a: val, |
|
| 1528 | + | encoder: CombinedEncoder::Registers { enc: encode::addUw, b: other }, |
|
| 1529 | + | }; |
|
| 1530 | + | } |
|
| 1531 | + | let case il::Val::Imm(shift) = b else { |
|
| 1532 | + | return nil; |
|
| 1533 | + | }; |
|
| 1534 | + | if op <> il::BinOp::Shl or a <> il::Val::Reg(temp) or shift < 0 or shift >= 64 { |
|
| 1535 | + | return nil; |
|
| 1536 | + | } |
|
| 1537 | + | if instrs.len >= 3 and uses[dst.n] == 1 and shift >= 1 and shift <= 3 { |
|
| 1538 | + | if let case il::Instr::BinOp { |
|
| 1539 | + | op: il::BinOp::Add, typ: il::Type::W64, dst: result, a: left, b: right |
|
| 1540 | + | } = |
|
| 1541 | + | instrs[2] |
|
| 1542 | + | { |
|
| 1543 | + | let other = otherOperand(left, right, dst) else { |
|
| 1544 | + | return nil; |
|
| 1545 | + | }; |
|
| 1546 | + | let enc = encode::sh1addUw |
|
| 1547 | + | if shift == 1 |
|
| 1548 | + | else encode::sh2addUw if shift == 2 else encode::sh3addUw; |
|
| 1549 | + | return Combined { |
|
| 1550 | + | count: 3, |
|
| 1551 | + | dst: result, |
|
| 1552 | + | a: val, |
|
| 1553 | + | encoder: CombinedEncoder::Registers { enc, b: other }, |
|
| 1554 | + | }; |
|
| 1555 | + | } |
|
| 1556 | + | } |
|
| 1557 | + | return Combined { |
|
| 1558 | + | count: 2, |
|
| 1559 | + | dst, |
|
| 1560 | + | a: val, |
|
| 1561 | + | encoder: CombinedEncoder::Immediate { enc: encode::slliUw, imm: shift as i32 }, |
|
| 1562 | + | }; |
|
| 1563 | + | } |
|
| 1564 | + | let case il::Instr::BinOp { op, typ, dst: temp, a, b } = first else { |
|
| 1565 | + | return nil; |
|
| 1566 | + | }; |
|
| 1567 | + | if uses[temp.n] <> 1 { |
|
| 1568 | + | return nil; |
|
| 1569 | + | } |
|
| 1570 | + | if op == il::BinOp::Shl and a == il::Val::Imm(1) and typ == il::Type::W64 and instrs.len >= 3 { |
|
| 1571 | + | if let case il::Instr::UnOp { |
|
| 1572 | + | op: il::UnOp::Not, typ: il::Type::W64, dst: mask, a: input |
|
| 1573 | + | } = |
|
| 1574 | + | second |
|
| 1575 | + | { |
|
| 1576 | + | if input == il::Val::Reg(temp) and uses[mask.n] == 1 { |
|
| 1577 | + | if let case il::Instr::BinOp { |
|
| 1578 | + | op: il::BinOp::And, typ: il::Type::W64, dst, a: left, b: right |
|
| 1579 | + | } = |
|
| 1580 | + | instrs[2] |
|
| 1581 | + | { |
|
| 1582 | + | let other = otherOperand(left, right, mask) else { |
|
| 1583 | + | return nil; |
|
| 1584 | + | }; |
|
| 1585 | + | return Combined { |
|
| 1586 | + | count: 3, |
|
| 1587 | + | dst, |
|
| 1588 | + | a: other, |
|
| 1589 | + | encoder: CombinedEncoder::Registers { enc: encode::bclr, b }, |
|
| 1590 | + | }; |
|
| 1591 | + | } |
|
| 1592 | + | } |
|
| 1593 | + | } |
|
| 1594 | + | } |
|
| 1595 | + | if op == il::BinOp::Xor { |
|
| 1596 | + | if let case il::Instr::UnOp { op: il::UnOp::Not, typ: outType, dst, a: input } = second { |
|
| 1597 | + | if typ == outType and input == il::Val::Reg(temp) { |
|
| 1598 | + | return Combined { |
|
| 1599 | + | count: 2, dst, a, encoder: CombinedEncoder::Registers { enc: encode::xnor, b } |
|
| 1600 | + | }; |
|
| 1601 | + | } |
|
| 1602 | + | } |
|
| 1603 | + | } |
|
| 1604 | + | if let case il::Instr::BinOp { op: next, typ: outType, dst, a: x, b: y } = second { |
|
| 1605 | + | if typ == outType { |
|
| 1606 | + | if next == il::BinOp::Add and typ == il::Type::W64 { |
|
| 1607 | + | if let case il::Val::Imm(shift) = b { |
|
| 1608 | + | let scale = shift |
|
| 1609 | + | if op == il::BinOp::Shl |
|
| 1610 | + | else 1 if shift == 2 else 2 if shift == 4 else 3 if shift == 8 else 0; |
|
| 1611 | + | if (op == il::BinOp::Shl or op == il::BinOp::Mul) and scale >= 1 and scale <= 3 |
|
| 1612 | + | { |
|
| 1613 | + | let other = otherOperand(x, y, temp) else { |
|
| 1614 | + | return nil; |
|
| 1615 | + | }; |
|
| 1616 | + | let enc = encode::sh1add |
|
| 1617 | + | if scale == 1 |
|
| 1618 | + | else encode::sh2add if scale == 2 else encode::sh3add; |
|
| 1619 | + | return Combined { |
|
| 1620 | + | count: 2, dst, a, encoder: CombinedEncoder::Registers { enc, b: other } |
|
| 1621 | + | }; |
|
| 1622 | + | } |
|
| 1623 | + | } |
|
| 1624 | + | } |
|
| 1625 | + | if op == il::BinOp::Ushr and next == il::BinOp::And { |
|
| 1626 | + | let other = otherOperand(x, y, temp) else { |
|
| 1627 | + | return nil; |
|
| 1628 | + | }; |
|
| 1629 | + | if other == il::Val::Imm(1) { |
|
| 1630 | + | if let case il::Val::Imm(bit) = b { |
|
| 1631 | + | let width: i64 = 32 if typ == il::Type::W32 else 64; |
|
| 1632 | + | return Combined { |
|
| 1633 | + | count: 2, |
|
| 1634 | + | dst, |
|
| 1635 | + | a, |
|
| 1636 | + | encoder: CombinedEncoder::Immediate { |
|
| 1637 | + | enc: encode::bexti, imm: (bit & (width - 1)) as i32 |
|
| 1638 | + | }, |
|
| 1639 | + | }; |
|
| 1640 | + | } |
|
| 1641 | + | if typ == il::Type::W64 { |
|
| 1642 | + | return Combined { |
|
| 1643 | + | count: 2, |
|
| 1644 | + | dst, |
|
| 1645 | + | a, |
|
| 1646 | + | encoder: CombinedEncoder::Registers { enc: encode::bext, b }, |
|
| 1647 | + | }; |
|
| 1648 | + | } |
|
| 1649 | + | } |
|
| 1650 | + | } |
|
| 1651 | + | if op == il::BinOp::Shl and a == il::Val::Imm(1) and typ == il::Type::W64 { |
|
| 1652 | + | if next == il::BinOp::Or or next == il::BinOp::Xor { |
|
| 1653 | + | let other = otherOperand(x, y, temp) else { |
|
| 1654 | + | return nil; |
|
| 1655 | + | }; |
|
| 1656 | + | let enc = encode::bset if next == il::BinOp::Or else encode::binv; |
|
| 1657 | + | return Combined { |
|
| 1658 | + | count: 2, dst, a: other, encoder: CombinedEncoder::Registers { enc, b } |
|
| 1659 | + | }; |
|
| 1660 | + | } |
|
| 1661 | + | } |
|
| 1662 | + | } |
|
| 1663 | + | // Complementary constant shifts form a rotation of one value. |
|
| 1664 | + | if instrs.len >= 3 and typ == outType and (typ == il::Type::W32 or typ == il::Type::W64) { |
|
| 1665 | + | if (op == il::BinOp::Shl and next == il::BinOp::Ushr) |
|
| 1666 | + | or (op == il::BinOp::Ushr and next == il::BinOp::Shl) |
|
| 1667 | + | { |
|
| 1668 | + | if a == x and uses[dst.n] == 1 { |
|
| 1669 | + | if let case il::Instr::BinOp { |
|
| 1670 | + | op: il::BinOp::Or, typ: resultType, dst: result, a: left, b: right |
|
| 1671 | + | } = |
|
| 1672 | + | instrs[2] |
|
| 1673 | + | { |
|
| 1674 | + | let other = otherOperand(left, right, temp) else { |
|
| 1675 | + | return nil; |
|
| 1676 | + | }; |
|
| 1677 | + | if resultType == typ and other == il::Val::Reg(dst) { |
|
| 1678 | + | if let case il::Val::Imm(firstShift) = b { |
|
| 1679 | + | if let case il::Val::Imm(secondShift) = y { |
|
| 1680 | + | let mask: i64 = 31 if typ == il::Type::W32 else 63; |
|
| 1681 | + | let lo = firstShift & mask; |
|
| 1682 | + | let hi = secondShift & mask; |
|
| 1683 | + | if ((lo + hi) & mask) == 0 { |
|
| 1684 | + | let enc = |
|
| 1685 | + | encode::roriw if typ == il::Type::W32 else encode::rori; |
|
| 1686 | + | return Combined { |
|
| 1687 | + | count: 3, |
|
| 1688 | + | dst: result, |
|
| 1689 | + | a, |
|
| 1690 | + | encoder: CombinedEncoder::Immediate { |
|
| 1691 | + | enc, |
|
| 1692 | + | imm: (lo if op == il::BinOp::Ushr else hi) as i32, |
|
| 1693 | + | }, |
|
| 1694 | + | }; |
|
| 1695 | + | } |
|
| 1696 | + | } |
|
| 1697 | + | } |
|
| 1698 | + | } |
|
| 1699 | + | } |
|
| 1700 | + | } |
|
| 1701 | + | } |
|
| 1702 | + | } |
|
| 1703 | + | } |
|
| 1704 | + | return nil; |
|
| 1705 | + | } |
|
| 1706 | + | ||
| 1707 | + | /// Emit a combined expression. Resolve both sources before writing its result. |
|
| 1708 | + | fn selectCombined(s: &mut Selector, e: &mut emit::Emitter, c: &Combined) |
|
| 1709 | + | throws (super::Error) |
|
| 1710 | + | { |
|
| 1711 | + | let rd = getDstReg(s, c.dst, super::SCRATCH1); |
|
| 1712 | + | let rs1 = try resolveVal(s, e, super::SCRATCH1, c.a); |
|
| 1713 | + | match c.encoder { |
|
| 1714 | + | case CombinedEncoder::Registers { enc, b } => { |
|
| 1715 | + | let rs2 = try resolveVal(s, e, super::SCRATCH2, b); |
|
| 1716 | + | try emit::emit(e, enc(rd, rs1, rs2)); |
|
| 1717 | + | } |
|
| 1718 | + | case CombinedEncoder::Immediate { enc, imm } => { |
|
| 1719 | + | try emit::emit(e, enc(rd, rs1, imm)); |
|
| 1720 | + | } |
|
| 1721 | + | } |
|
| 1722 | + | } |
|
| 1723 | + | ||
| 1338 | 1724 | /// Select instructions for a function. |
|
| 1339 | 1725 | export fn selectFn( |
|
| 1340 | 1726 | e: &mut emit::Emitter, |
|
| 1341 | 1727 | ralloc: regalloc::AllocResult, |
|
| 1342 | 1728 | func: *il::Fn, |
| 1366 | 1752 | throw super::Error::Allocation; |
|
| 1367 | 1753 | }; |
|
| 1368 | 1754 | let exts = try extensions(func, observed, count, arena) catch { |
|
| 1369 | 1755 | throw super::Error::Allocation; |
|
| 1370 | 1756 | }; |
|
| 1757 | + | let mut uses = UseCounts { counts: &mut [] }; |
|
| 1758 | + | try uses.counts.resize(count, 0, arena) catch { |
|
| 1759 | + | throw super::Error::Allocation; |
|
| 1760 | + | }; |
|
| 1761 | + | for block in func.blocks { |
|
| 1762 | + | for instr in block.instrs { |
|
| 1763 | + | il::forEachReg(func, instr, &mut uses); |
|
| 1764 | + | } |
|
| 1765 | + | } |
|
| 1371 | 1766 | // Conditional branches start in their short form. When one does not reach |
|
| 1372 | 1767 | // its target, the body is emitted again with that branch in its long form. |
|
| 1373 | 1768 | let start = emit::beginBody(e); |
|
| 1374 | 1769 | loop { |
|
| 1375 | - | try selectBody(e, ralloc, exts, observed, func, layouts, &frame); |
|
| 1770 | + | try selectBody(e, ralloc, exts, observed, &uses.counts[..], func, layouts, &frame); |
|
| 1376 | 1771 | // Patch local branches now that all blocks are emitted. |
|
| 1377 | 1772 | if try emit::patchLocalBranches(e) { |
|
| 1378 | 1773 | return; |
|
| 1379 | 1774 | } |
|
| 1380 | 1775 | emit::rewind(e, start); |
| 1385 | 1780 | fn selectBody( |
|
| 1386 | 1781 | e: &mut emit::Emitter, |
|
| 1387 | 1782 | ralloc: regalloc::AllocResult, |
|
| 1388 | 1783 | exts: *[Extension], |
|
| 1389 | 1784 | observed: *[bool], |
|
| 1785 | + | uses: &[u32], |
|
| 1390 | 1786 | func: *il::Fn, |
|
| 1391 | 1787 | layouts: &[il::Layout], |
|
| 1392 | 1788 | frame: &emit::Frame |
|
| 1393 | 1789 | ) throws (super::Error) { |
|
| 1394 | 1790 | // Reset block offsets for this function. |
| 1436 | 1832 | } |
|
| 1437 | 1833 | } |
|
| 1438 | 1834 | } |
|
| 1439 | 1835 | // Emit each block. |
|
| 1440 | 1836 | for i in 0 .. func.blocks.len { |
|
| 1441 | - | try selectBlock(&mut s, e, i, &func.blocks[i], frame, func, layouts); |
|
| 1837 | + | try selectBlock(&mut s, e, i, &func.blocks[i], uses, frame, func, layouts); |
|
| 1442 | 1838 | } |
|
| 1443 | 1839 | // Emit epilogue. |
|
| 1444 | 1840 | try emit::emitEpilogue(e, frame); |
|
| 1445 | 1841 | } |
|
| 1446 | 1842 |
| 1448 | 1844 | fn selectBlock( |
|
| 1449 | 1845 | s: &mut Selector, |
|
| 1450 | 1846 | e: &mut emit::Emitter, |
|
| 1451 | 1847 | blockIdx: u32, |
|
| 1452 | 1848 | block: *il::Block, |
|
| 1849 | + | uses: &[u32], |
|
| 1453 | 1850 | frame: &emit::Frame, |
|
| 1454 | 1851 | func: *il::Fn, |
|
| 1455 | 1852 | layouts: &[il::Layout] |
|
| 1456 | 1853 | ) throws (super::Error) { |
|
| 1457 | 1854 | // Record block address for branch patching. |
| 1461 | 1858 | // By the time we enter the block, the arguments have already been |
|
| 1462 | 1859 | // moved to the parameter registers by the predecessor's terminator. |
|
| 1463 | 1860 | ||
| 1464 | 1861 | // Process each instruction, auto-committing any pending spill after each. |
|
| 1465 | 1862 | let hasLocs = block.locs.len > 0; |
|
| 1863 | + | let mut skip: u32 = 0; |
|
| 1466 | 1864 | for instr, i in block.instrs { |
|
| 1865 | + | if skip > 0 { |
|
| 1866 | + | set skip -= 1; |
|
| 1867 | + | continue; |
|
| 1868 | + | } |
|
| 1467 | 1869 | // Record debug location before emitting machine instructions. |
|
| 1468 | 1870 | if hasLocs { |
|
| 1469 | 1871 | try emit::recordSrcLoc(e, block.locs[i]); |
|
| 1470 | 1872 | } |
|
| 1471 | 1873 | set s.pendingSpill = nil; |
|
| 1472 | 1874 | set s.instrIndex = i; |
|
| 1473 | - | try selectInstr(s, e, blockIdx, instr, frame, func, layouts); |
|
| 1474 | - | ||
| 1875 | + | if let combined = combination(&block.instrs[i ..], uses) { |
|
| 1876 | + | try selectCombined(s, e, &combined); |
|
| 1877 | + | set skip = combined.count - 1; |
|
| 1878 | + | } else { |
|
| 1879 | + | try selectInstr(s, e, blockIdx, instr, frame, func, layouts); |
|
| 1880 | + | } |
|
| 1475 | 1881 | // Flush the pending spill store, if any. |
|
| 1476 | 1882 | if let p = s.pendingSpill { |
|
| 1477 | 1883 | if let slot = regalloc::spill::spillSlot(&s.ralloc.spill, p.ssa) { |
|
| 1478 | 1884 | try emit::emitSd(e, p.rd, super::SP, spillOffset(s, slot)); |
|
| 1479 | 1885 | } |
| 1577 | 1983 | // A sign-extended index above the largest one fails the check, so the |
|
| 1578 | 1984 | // check proves that it equals its zero extension. |
|
| 1579 | 1985 | let ri = try resolveIndex(s, e, index); |
|
| 1580 | 1986 | try emit::loadImm(e, super::ADDR_SCRATCH, count as i64); |
|
| 1581 | 1987 | try emitTrapUnlessLtu(e, ri, super::ADDR_SCRATCH); |
|
| 1582 | - | if size == 1 { |
|
| 1583 | - | try emit::emit(e, encode::add(rd, rbase, ri)); |
|
| 1584 | - | } else { |
|
| 1585 | - | try emitScale(e, super::ADDR_SCRATCH, ri, size, super::SCRATCH3); |
|
| 1586 | - | try emit::emit(e, encode::add(rd, rbase, super::ADDR_SCRATCH)); |
|
| 1587 | - | } |
|
| 1988 | + | try emitAddress(e, rd, rbase, ri, size, super::ADDR_SCRATCH, super::SCRATCH3); |
|
| 1588 | 1989 | } |
|
| 1589 | 1990 | case il::Instr::SliceElem { dst, slice, index, layout } => { |
|
| 1590 | 1991 | let size = layouts[layout].size; |
|
| 1591 | 1992 | let rd = getDstReg(s, dst, super::SCRATCH1); |
|
| 1592 | 1993 | let rslice = try getSrcReg(s, e, slice, super::SCRATCH2); |
| 1594 | 1995 | // passes the check equals its zero extension. A longer slice, which |
|
| 1595 | 1996 | // only hostile RIL can build, traps for every index of 2^31 or more. |
|
| 1596 | 1997 | let ri = try resolveIndex(s, e, index); |
|
| 1597 | 1998 | try emit::emitLoad(e, super::ADDR_SCRATCH, rslice, SLICE_LEN_OFFSET, il::Type::W32); |
|
| 1598 | 1999 | try emitTrapUnlessLtu(e, ri, super::ADDR_SCRATCH); |
|
| 1599 | - | let mut offset = ri; |
|
| 1600 | - | if size <> 1 { |
|
| 1601 | - | try emitScale(e, super::SCRATCH1, ri, size, super::SCRATCH3); |
|
| 1602 | - | set offset = super::SCRATCH1; |
|
| 1603 | - | } |
|
| 1604 | - | try emit::emitLd(e, super::ADDR_SCRATCH, rslice, SLICE_PTR_OFFSET); |
|
| 1605 | - | try emit::emit(e, encode::add(rd, super::ADDR_SCRATCH, offset)); |
|
| 2000 | + | try emit::emitLd(e, super::SCRATCH3, rslice, SLICE_PTR_OFFSET); |
|
| 2001 | + | try emitAddress( |
|
| 2002 | + | e, rd, super::SCRATCH3, ri, size, super::SCRATCH1, super::ADDR_SCRATCH |
|
| 2003 | + | ); |
|
| 1606 | 2004 | } |
|
| 1607 | 2005 | case il::Instr::SliceNew { dst, base, layout, count, start, end } => { |
|
| 1608 | 2006 | let size = layouts[layout].size; |
|
| 1609 | 2007 | // T6 holds the slot and T3 the start throughout. T5 holds the end, and then the |
|
| 1610 | 2008 | // length. T4 holds a value only between operand resolutions, since resolving an |
| 1640 | 2038 | try emit::emitSw(e, super::ADDR_SCRATCH, rslot, SLICE_CAP_OFFSET); |
|
| 1641 | 2039 | let rbase = try resolveVal(s, e, super::SCRATCH1, base); |
|
| 1642 | 2040 | if startIsZero { |
|
| 1643 | 2041 | try emit::emitSd(e, rbase, rslot, SLICE_PTR_OFFSET); |
|
| 1644 | 2042 | } else { |
|
| 1645 | - | try emitScale(e, super::SCRATCH3, super::SCRATCH3, size, super::ADDR_SCRATCH); |
|
| 1646 | - | try emit::emit(e, encode::add(super::SCRATCH3, rbase, super::SCRATCH3)); |
|
| 2043 | + | try emitAddress( |
|
| 2044 | + | e, |
|
| 2045 | + | super::SCRATCH3, |
|
| 2046 | + | rbase, |
|
| 2047 | + | super::SCRATCH3, |
|
| 2048 | + | size, |
|
| 2049 | + | super::ADDR_SCRATCH, |
|
| 2050 | + | super::SCRATCH2 |
|
| 2051 | + | ); |
|
| 1647 | 2052 | try emit::emitSd(e, super::SCRATCH3, rslot, SLICE_PTR_OFFSET); |
|
| 1648 | 2053 | } |
|
| 1649 | 2054 | } |
|
| 1650 | 2055 | case il::Instr::SliceSub { dst, src, layout, start, end } => { |
|
| 1651 | 2056 | let size = layouts[layout].size; |
| 1676 | 2081 | )); |
|
| 1677 | 2082 | } |
|
| 1678 | 2083 | try emit::emitSw(e, super::ADDR_SCRATCH, rslot, SLICE_CAP_OFFSET); |
|
| 1679 | 2084 | try emit::emitLd(e, super::ADDR_SCRATCH, rsrc, SLICE_PTR_OFFSET); |
|
| 1680 | 2085 | if not startIsZero { |
|
| 1681 | - | try emitScale(e, super::SCRATCH3, super::SCRATCH3, size, super::SCRATCH1); |
|
| 1682 | - | try emit::emit(e, encode::add( |
|
| 1683 | - | super::ADDR_SCRATCH, super::ADDR_SCRATCH, super::SCRATCH3 |
|
| 1684 | - | )); |
|
| 2086 | + | try emitAddress( |
|
| 2087 | + | e, |
|
| 2088 | + | super::ADDR_SCRATCH, |
|
| 2089 | + | super::ADDR_SCRATCH, |
|
| 2090 | + | super::SCRATCH3, |
|
| 2091 | + | size, |
|
| 2092 | + | super::SCRATCH1, |
|
| 2093 | + | super::SCRATCH2 |
|
| 2094 | + | ); |
|
| 1685 | 2095 | } |
|
| 1686 | 2096 | try emit::emitSd(e, super::ADDR_SCRATCH, rslot, SLICE_PTR_OFFSET); |
|
| 1687 | 2097 | } |
|
| 1688 | 2098 | case il::Instr::SliceLen { dst, length, .. } => try emitSliceLen(s, e, dst, length), |
|
| 1689 | 2099 | case il::Instr::SliceCopy { dst, src, layout } => |
| 2171 | 2581 | return; |
|
| 2172 | 2582 | } else if imm == 8 { |
|
| 2173 | 2583 | try emitShiftLeft(e, typ, rd, rs1, 3); |
|
| 2174 | 2584 | return; |
|
| 2175 | 2585 | } |
|
| 2586 | + | if typ == il::Type::W64 and (imm == 3 or imm == 5 or imm == 9) { |
|
| 2587 | + | let enc = encode::sh1add |
|
| 2588 | + | if imm == 3 |
|
| 2589 | + | else encode::sh2add if imm == 5 else encode::sh3add; |
|
| 2590 | + | try emit::emit(e, enc(rd, rs1, rs1)); |
|
| 2591 | + | return; |
|
| 2592 | + | } |
|
| 2176 | 2593 | } |
|
| 2177 | 2594 | let rs2 = try resolveVal(s, e, super::SCRATCH2, b); |
|
| 2178 | 2595 | try emit::emit( |
|
| 2179 | 2596 | e, |
|
| 2180 | 2597 | encode::mulw(rd, rs1, rs2) |
| 2308 | 2725 | case BinOp::Xor => try emit::emit(e, encode::xori(rd, rs1, simm)), |
|
| 2309 | 2726 | } |
|
| 2310 | 2727 | return; |
|
| 2311 | 2728 | } |
|
| 2312 | 2729 | } |
|
| 2730 | + | if let case il::Val::Imm(imm) = b { |
|
| 2731 | + | if op == BinOp::And { |
|
| 2732 | + | if imm == U16_MAX { |
|
| 2733 | + | try emit::emit(e, encode::zextH(rd, rs1)); |
|
| 2734 | + | return; |
|
| 2735 | + | } |
|
| 2736 | + | if imm == U32_MAX { |
|
| 2737 | + | try emit::emit(e, encode::zextW(rd, rs1)); |
|
| 2738 | + | return; |
|
| 2739 | + | } |
|
| 2740 | + | } |
|
| 2741 | + | let mask = (~imm if op == BinOp::And else imm) as u64; |
|
| 2742 | + | if op <> BinOp::Add and mask <> 0 and (mask & (mask - 1)) == 0 { |
|
| 2743 | + | let mut bit: i32 = 0; |
|
| 2744 | + | let mut remaining = mask; |
|
| 2745 | + | while remaining > 1 { |
|
| 2746 | + | set remaining >>= 1; |
|
| 2747 | + | set bit += 1; |
|
| 2748 | + | } |
|
| 2749 | + | match op { |
|
| 2750 | + | case BinOp::And => try emit::emit(e, encode::bclri(rd, rs1, bit)), |
|
| 2751 | + | case BinOp::Or => try emit::emit(e, encode::bseti(rd, rs1, bit)), |
|
| 2752 | + | case BinOp::Xor => try emit::emit(e, encode::binvi(rd, rs1, bit)), |
|
| 2753 | + | case BinOp::Add => {} |
|
| 2754 | + | } |
|
| 2755 | + | return; |
|
| 2756 | + | } |
|
| 2757 | + | } |
|
| 2313 | 2758 | // Fallback: load into register. |
|
| 2314 | 2759 | let rs2 = try resolveVal(s, e, super::SCRATCH2, b); |
|
| 2315 | 2760 | match op { |
|
| 2316 | 2761 | case BinOp::Add => try emit::emit(e, encode::add(rd, rs1, rs2)), |
|
| 2317 | 2762 | case BinOp::And => try emit::emit(e, encode::and_(rd, rs1, rs2)), |
| 2754 | 3199 | } else { |
|
| 2755 | 3200 | try emit::loadImm(e, super::ADDR_SCRATCH, elementSize as i64); |
|
| 2756 | 3201 | try emit::emit(e, encode::divu(super::A1, super::A1, super::ADDR_SCRATCH)); |
|
| 2757 | 3202 | } |
|
| 2758 | 3203 | // Clamp the length to `types::MAX_LENGTH`, which is 31 bits. |
|
| 2759 | - | try emit::emit(e, encode::srli(super::ADDR_SCRATCH, super::A1, 31)); |
|
| 2760 | - | try emit::emit(e, encode::beq(super::ADDR_SCRATCH, super::ZERO, super::INSTR_SIZE * 3)); |
|
| 2761 | - | try emit::emit(e, encode::addi(super::A1, super::ZERO, -1)); |
|
| 2762 | - | try emit::emit(e, encode::srli(super::A1, super::A1, 33)); |
|
| 3204 | + | try emit::loadImm(e, super::ADDR_SCRATCH, MAX_OBJECT_SIZE as i64); |
|
| 3205 | + | try emit::emit(e, encode::minu(super::A1, super::A1, super::ADDR_SCRATCH)); |
|
| 2763 | 3206 | try emit::emit(e, encode::sd(super::A0, super::SCRATCH3, SLICE_PTR_OFFSET)); |
|
| 2764 | 3207 | try emit::emit(e, encode::sw(super::A1, super::SCRATCH3, SLICE_LEN_OFFSET)); |
|
| 2765 | 3208 | try emit::emit(e, encode::sw(super::A1, super::SCRATCH3, SLICE_CAP_OFFSET)); |
|
| 2766 | 3209 | try emit::emit(e, encode::mv(super::SCRATCH1, super::ZERO)); |
|
| 2767 | 3210 | let skip = (e.code.len - branch) as i32 * super::INSTR_SIZE; |
lib/std/arch/rv64/printer.rad
+83 -3
| 72 | 72 | } |
|
| 73 | 73 | ||
| 74 | 74 | /// Write mnemonic with padding for alignment. |
|
| 75 | 75 | fn writeMnem(out: &mut opaque sexpr::Output, m: *[u8]) { |
|
| 76 | 76 | out.write(m); |
|
| 77 | - | let mut i = m.len; |
|
| 78 | - | while i < MNEMONIC_WIDTH { |
|
| 77 | + | let padding = MNEMONIC_WIDTH - m.len if m.len < MNEMONIC_WIDTH else 1; |
|
| 78 | + | for i in 0 .. padding { |
|
| 79 | 79 | out.write(" "); |
|
| 80 | - | set i += 1; |
|
| 81 | 80 | } |
|
| 82 | 81 | } |
|
| 83 | 82 | ||
| 84 | 83 | //////////////////////////////// |
|
| 85 | 84 | // Instruction Format Helpers // |
| 150 | 149 | /// Print a single instruction to output buffer. |
|
| 151 | 150 | export fn printInstr(out: &mut opaque sexpr::Output, instr: u32) { |
|
| 152 | 151 | let decoded = decode::decode(instr); |
|
| 153 | 152 | ||
| 154 | 153 | match decoded { |
|
| 154 | + | case decode::Instr::AddUw { rd, rs1, rs2 } => |
|
| 155 | + | fmtR(out, "add.uw", rd, rs1, rs2), |
|
| 156 | + | case decode::Instr::Sh1add { rd, rs1, rs2 } => |
|
| 157 | + | fmtR(out, "sh1add", rd, rs1, rs2), |
|
| 158 | + | case decode::Instr::Sh2add { rd, rs1, rs2 } => |
|
| 159 | + | fmtR(out, "sh2add", rd, rs1, rs2), |
|
| 160 | + | case decode::Instr::Sh3add { rd, rs1, rs2 } => |
|
| 161 | + | fmtR(out, "sh3add", rd, rs1, rs2), |
|
| 162 | + | case decode::Instr::Sh1addUw { rd, rs1, rs2 } => |
|
| 163 | + | fmtR(out, "sh1add.uw", rd, rs1, rs2), |
|
| 164 | + | case decode::Instr::Sh2addUw { rd, rs1, rs2 } => |
|
| 165 | + | fmtR(out, "sh2add.uw", rd, rs1, rs2), |
|
| 166 | + | case decode::Instr::Sh3addUw { rd, rs1, rs2 } => |
|
| 167 | + | fmtR(out, "sh3add.uw", rd, rs1, rs2), |
|
| 168 | + | case decode::Instr::Andn { rd, rs1, rs2 } => |
|
| 169 | + | fmtR(out, "andn", rd, rs1, rs2), |
|
| 170 | + | case decode::Instr::Orn { rd, rs1, rs2 } => |
|
| 171 | + | fmtR(out, "orn", rd, rs1, rs2), |
|
| 172 | + | case decode::Instr::Xnor { rd, rs1, rs2 } => |
|
| 173 | + | fmtR(out, "xnor", rd, rs1, rs2), |
|
| 174 | + | case decode::Instr::Min { rd, rs1, rs2 } => |
|
| 175 | + | fmtR(out, "min", rd, rs1, rs2), |
|
| 176 | + | case decode::Instr::Minu { rd, rs1, rs2 } => |
|
| 177 | + | fmtR(out, "minu", rd, rs1, rs2), |
|
| 178 | + | case decode::Instr::Max { rd, rs1, rs2 } => |
|
| 179 | + | fmtR(out, "max", rd, rs1, rs2), |
|
| 180 | + | case decode::Instr::Maxu { rd, rs1, rs2 } => |
|
| 181 | + | fmtR(out, "maxu", rd, rs1, rs2), |
|
| 182 | + | case decode::Instr::Rol { rd, rs1, rs2 } => |
|
| 183 | + | fmtR(out, "rol", rd, rs1, rs2), |
|
| 184 | + | case decode::Instr::Ror { rd, rs1, rs2 } => |
|
| 185 | + | fmtR(out, "ror", rd, rs1, rs2), |
|
| 186 | + | case decode::Instr::Rolw { rd, rs1, rs2 } => |
|
| 187 | + | fmtR(out, "rolw", rd, rs1, rs2), |
|
| 188 | + | case decode::Instr::Rorw { rd, rs1, rs2 } => |
|
| 189 | + | fmtR(out, "rorw", rd, rs1, rs2), |
|
| 190 | + | case decode::Instr::Bclr { rd, rs1, rs2 } => |
|
| 191 | + | fmtR(out, "bclr", rd, rs1, rs2), |
|
| 192 | + | case decode::Instr::Bext { rd, rs1, rs2 } => |
|
| 193 | + | fmtR(out, "bext", rd, rs1, rs2), |
|
| 194 | + | case decode::Instr::Binv { rd, rs1, rs2 } => |
|
| 195 | + | fmtR(out, "binv", rd, rs1, rs2), |
|
| 196 | + | case decode::Instr::Bset { rd, rs1, rs2 } => |
|
| 197 | + | fmtR(out, "bset", rd, rs1, rs2), |
|
| 198 | + | case decode::Instr::Clz { rd, rs1 } => |
|
| 199 | + | fmt2R(out, "clz", rd, rs1), |
|
| 200 | + | case decode::Instr::Ctz { rd, rs1 } => |
|
| 201 | + | fmt2R(out, "ctz", rd, rs1), |
|
| 202 | + | case decode::Instr::Cpop { rd, rs1 } => |
|
| 203 | + | fmt2R(out, "cpop", rd, rs1), |
|
| 204 | + | case decode::Instr::Clzw { rd, rs1 } => |
|
| 205 | + | fmt2R(out, "clzw", rd, rs1), |
|
| 206 | + | case decode::Instr::Ctzw { rd, rs1 } => |
|
| 207 | + | fmt2R(out, "ctzw", rd, rs1), |
|
| 208 | + | case decode::Instr::Cpopw { rd, rs1 } => |
|
| 209 | + | fmt2R(out, "cpopw", rd, rs1), |
|
| 210 | + | case decode::Instr::SextB { rd, rs1 } => |
|
| 211 | + | fmt2R(out, "sext.b", rd, rs1), |
|
| 212 | + | case decode::Instr::SextH { rd, rs1 } => |
|
| 213 | + | fmt2R(out, "sext.h", rd, rs1), |
|
| 214 | + | case decode::Instr::OrcB { rd, rs1 } => |
|
| 215 | + | fmt2R(out, "orc.b", rd, rs1), |
|
| 216 | + | case decode::Instr::Rev8 { rd, rs1 } => |
|
| 217 | + | fmt2R(out, "rev8", rd, rs1), |
|
| 218 | + | case decode::Instr::ZextH { rd, rs1 } => |
|
| 219 | + | fmt2R(out, "zext.h", rd, rs1), |
|
| 220 | + | case decode::Instr::SlliUw { rd, rs1, shamt } => |
|
| 221 | + | fmtI(out, "slli.uw", rd, rs1, shamt), |
|
| 222 | + | case decode::Instr::Rori { rd, rs1, shamt } => |
|
| 223 | + | fmtI(out, "rori", rd, rs1, shamt), |
|
| 224 | + | case decode::Instr::Roriw { rd, rs1, shamt } => |
|
| 225 | + | fmtI(out, "roriw", rd, rs1, shamt), |
|
| 226 | + | case decode::Instr::Bclri { rd, rs1, shamt } => |
|
| 227 | + | fmtI(out, "bclri", rd, rs1, shamt), |
|
| 228 | + | case decode::Instr::Bexti { rd, rs1, shamt } => |
|
| 229 | + | fmtI(out, "bexti", rd, rs1, shamt), |
|
| 230 | + | case decode::Instr::Binvi { rd, rs1, shamt } => |
|
| 231 | + | fmtI(out, "binvi", rd, rs1, shamt), |
|
| 232 | + | case decode::Instr::Bseti { rd, rs1, shamt } => |
|
| 233 | + | fmtI(out, "bseti", rd, rs1, shamt), |
|
| 234 | + | ||
| 155 | 235 | case decode::Instr::Lui { rd, imm } => fmtRI(out, "lui", rd, imm), |
|
| 156 | 236 | case decode::Instr::Auipc { rd, imm } => fmtRI(out, "auipc", rd, imm), |
|
| 157 | 237 | case decode::Instr::Jal { rd, imm } => { |
|
| 158 | 238 | if *rd == 0 { |
|
| 159 | 239 | fmtImm(out, "j", imm); |
lib/std/arch/rv64/shared/tests/run.rad
+21 -0
| 170 | 170 | return Outcome::Faulted; |
|
| 171 | 171 | } |
|
| 172 | 172 | set m.steps += 1; |
|
| 173 | 173 | let mut next = pc + 1; |
|
| 174 | 174 | match decode::decode(code[pc]) { |
|
| 175 | + | case decode::Instr::AddUw { rd, rs1, rs2 } => { |
|
| 176 | + | write(m, rd, read(m, rs1) as u32 as u64 + read(m, rs2)); |
|
| 177 | + | } |
|
| 178 | + | case decode::Instr::Sh1add { rd, rs1, rs2 } => { |
|
| 179 | + | write(m, rd, (read(m, rs1) << 1) + read(m, rs2)); |
|
| 180 | + | } |
|
| 181 | + | case decode::Instr::Sh2add { rd, rs1, rs2 } => { |
|
| 182 | + | write(m, rd, (read(m, rs1) << 2) + read(m, rs2)); |
|
| 183 | + | } |
|
| 184 | + | case decode::Instr::Sh3add { rd, rs1, rs2 } => { |
|
| 185 | + | write(m, rd, (read(m, rs1) << 3) + read(m, rs2)); |
|
| 186 | + | } |
|
| 187 | + | case decode::Instr::SextB { rd, rs1 } => { |
|
| 188 | + | write(m, rd, sext8(read(m, rs1))); |
|
| 189 | + | } |
|
| 190 | + | case decode::Instr::SextH { rd, rs1 } => { |
|
| 191 | + | write(m, rd, sext16(read(m, rs1))); |
|
| 192 | + | } |
|
| 193 | + | case decode::Instr::ZextH { rd, rs1 } => { |
|
| 194 | + | write(m, rd, read(m, rs1) as u16 as u64); |
|
| 195 | + | } |
|
| 175 | 196 | case decode::Instr::Addi { rd, rs1, imm } => { |
|
| 176 | 197 | write(m, rd, read(m, rs1) + imm as i64 as u64); |
|
| 177 | 198 | } |
|
| 178 | 199 | case decode::Instr::Addiw { rd, rs1, imm } => { |
|
| 179 | 200 | write(m, rd, sext32(read(m, rs1) + imm as i64 as u64)); |
lib/std/arch/rv64/tests.rad
+69 -5
| 1 | - | //! RV64I+M instruction encoding tests. |
|
| 1 | + | //! RV64 instruction encoding and selection tests. |
|
| 2 | 2 | //! |
|
| 3 | 3 | //! These tests verify that instruction encodings match the RISC-V specification |
|
| 4 | 4 | //! by comparing against known-good values. |
|
| 5 | 5 | ||
| 6 | 6 | @test mod bounds; |
| 654 | 654 | }; |
|
| 655 | 655 | try testing::expect(selfMoves(try generateBody(&table[..], nil, &[element])) == 0); |
|
| 656 | 656 | } |
|
| 657 | 657 | ||
| 658 | 658 | /// Number of instructions in `code` that sign- or zero-extend a register from 32 bits: |
|
| 659 | - | /// `addiw` by zero, or `slli` by 32. |
|
| 659 | + | /// `addiw` by zero, or `zext.w`. |
|
| 660 | 660 | fn wordExtensions(code: *[u32]) -> u32 { |
|
| 661 | 661 | let mut count: u32 = 0; |
|
| 662 | 662 | for word in code { |
|
| 663 | 663 | let isAddiw = (word & 0x7f) == 0x1b and ((word >> 12) & 7) == 0 and (word >> 20) == 0; |
|
| 664 | - | let isSlli32 = (word & 0x7f) == 0x13 and ((word >> 12) & 7) == 1 and (word >> 20) == 32; |
|
| 665 | - | if isAddiw or isSlli32 { |
|
| 664 | + | let isZextW = (word & 0xfff0707f) == 0x0800003b; |
|
| 665 | + | if isAddiw or isZextW { |
|
| 666 | 666 | set count += 1; |
|
| 667 | 667 | } |
|
| 668 | 668 | } |
|
| 669 | 669 | return count; |
|
| 670 | 670 | } |
| 766 | 766 | }; |
|
| 767 | 767 | let code = try generateBody(&table[..], nil, &[page]); |
|
| 768 | 768 | let mut clamped = false; |
|
| 769 | 769 | for word in code { |
|
| 770 | 770 | try testing::expect(word <> encode::srli(rv64::A1, rv64::A1, 32)); |
|
| 771 | - | if word == encode::srli(rv64::A1, rv64::A1, 33) { |
|
| 771 | + | if word == encode::minu(rv64::A1, rv64::A1, rv64::ADDR_SCRATCH) { |
|
| 772 | 772 | set clamped = true; |
|
| 773 | 773 | } |
|
| 774 | 774 | } |
|
| 775 | 775 | try testing::expect(clamped); |
|
| 776 | 776 | } |
| 1809 | 1809 | try! rv64::emit::emitEpilogue(&mut e, &frame); |
|
| 1810 | 1810 | assert stores(&e.code[..], 3) == 9; |
|
| 1811 | 1811 | assert loads(&e.code[..], 3) == 9; |
|
| 1812 | 1812 | assert e.code.len <= 32; |
|
| 1813 | 1813 | } |
|
| 1814 | + | ||
| 1815 | + | /// Whether code contains the fixed bits of an instruction encoding. |
|
| 1816 | + | fn containsInstruction(code: *[u32], bits: u32, mask: u32) -> bool { |
|
| 1817 | + | for word in code { |
|
| 1818 | + | if (word & mask) == bits { |
|
| 1819 | + | return true; |
|
| 1820 | + | } |
|
| 1821 | + | } |
|
| 1822 | + | return false; |
|
| 1823 | + | } |
|
| 1824 | + | ||
| 1825 | + | /// Bit manipulation selection preserves single-use expression semantics. |
|
| 1826 | + | @test unsafe fn testBitManipulationSelection() throws (testing::TestError) { |
|
| 1827 | + | let shift = il::Instr::BinOp { |
|
| 1828 | + | op: il::BinOp::Shl, typ: il::Type::W64, dst: il::Reg { n: 2 }, a: reg(0), b: il::Val::Imm(3) |
|
| 1829 | + | }; |
|
| 1830 | + | let sum = il::Instr::BinOp { |
|
| 1831 | + | op: il::BinOp::Add, typ: il::Type::W64, dst: il::Reg { n: 3 }, a: reg(2), b: reg(1) |
|
| 1832 | + | }; |
|
| 1833 | + | assert containsInstruction(try generateBody(&[], nil, &[shift, sum]), 0x20006033, 0xfe00707f); |
|
| 1834 | + | let inverted = il::Instr::UnOp { |
|
| 1835 | + | op: il::UnOp::Not, typ: il::Type::W64, dst: il::Reg { n: 2 }, a: reg(0) |
|
| 1836 | + | }; |
|
| 1837 | + | let conjunction = il::Instr::BinOp { |
|
| 1838 | + | op: il::BinOp::And, typ: il::Type::W64, dst: il::Reg { n: 3 }, a: reg(1), b: reg(2) |
|
| 1839 | + | }; |
|
| 1840 | + | assert containsInstruction( |
|
| 1841 | + | try generateBody(&[], nil, &[inverted, conjunction]), 0x40007033, 0xfe00707f |
|
| 1842 | + | ); |
|
| 1843 | + | // The intermediate has another use, so its definition must remain. |
|
| 1844 | + | let usedAgain = il::Instr::Copy { dst: il::Reg { n: 4 }, val: reg(2) }; |
|
| 1845 | + | assert not containsInstruction( |
|
| 1846 | + | try generateBody(&[], nil, &[inverted, conjunction, usedAgain]), 0x40007033, 0xfe00707f |
|
| 1847 | + | ); |
|
| 1848 | + | let right = il::Instr::BinOp { |
|
| 1849 | + | op: il::BinOp::Ushr, |
|
| 1850 | + | typ: il::Type::W64, |
|
| 1851 | + | dst: il::Reg { n: 3 }, |
|
| 1852 | + | a: reg(0), |
|
| 1853 | + | b: il::Val::Imm(61), |
|
| 1854 | + | }; |
|
| 1855 | + | let disjunction = il::Instr::BinOp { |
|
| 1856 | + | op: il::BinOp::Or, typ: il::Type::W64, dst: il::Reg { n: 4 }, a: reg(2), b: reg(3) |
|
| 1857 | + | }; |
|
| 1858 | + | assert containsInstruction( |
|
| 1859 | + | try generateBody(&[], nil, &[shift, right, disjunction]), 0x63d05013, 0xfff0707f |
|
| 1860 | + | ); |
|
| 1861 | + | let bit = il::Instr::BinOp { |
|
| 1862 | + | op: il::BinOp::And, typ: il::Type::W64, dst: il::Reg { n: 4 }, a: reg(3), b: il::Val::Imm(1) |
|
| 1863 | + | }; |
|
| 1864 | + | assert containsInstruction(try generateBody(&[], nil, &[right, bit]), 0x4bd05013, 0xfff0707f); |
|
| 1865 | + | let setBit = il::Instr::BinOp { |
|
| 1866 | + | op: il::BinOp::Or, |
|
| 1867 | + | typ: il::Type::W64, |
|
| 1868 | + | dst: il::Reg { n: 2 }, |
|
| 1869 | + | a: reg(0), |
|
| 1870 | + | b: il::Val::Imm(0x4000000000000000), |
|
| 1871 | + | }; |
|
| 1872 | + | assert containsInstruction(try generateBody(&[], nil, &[setBit]), 0x2be01013, 0xfff0707f); |
|
| 1873 | + | let extended = il::Instr::Zext { typ: il::Type::W32, dst: il::Reg { n: 2 }, val: reg(0) }; |
|
| 1874 | + | assert containsInstruction( |
|
| 1875 | + | try generateBody(&[], nil, &[extended, sum]), 0x0800003b, 0xfe00707f |
|
| 1876 | + | ); |
|
| 1877 | + | } |
test/tests/bitmanip.rad
added
+166 -0
| 1 | + | //! returns: 0 |
|
| 2 | + | ||
| 3 | + | /// Combine shifts, bitwise expressions, and unsigned word addresses. |
|
| 4 | + | @default fn main() -> i32 { |
|
| 5 | + | for x in [0 as u64, 1, 0x8000000000000000, 0xffffffffffffffff, 0x123456789abcdef0] { |
|
| 6 | + | assert multiplyThree(x) == x + x + x; |
|
| 7 | + | assert multiplyFive(x) == multiplyThree(x) + x + x; |
|
| 8 | + | assert multiplyNine(x) == multiplyFive(x) + multiplyThree(x) + x; |
|
| 9 | + | assert maskHalf(x) == x as u16 as u64; |
|
| 10 | + | assert maskWord(x) == x as u32 as u64; |
|
| 11 | + | assert shiftAdd(x, 9) == (x << 3) + 9; |
|
| 12 | + | assert invertedAnd(x, 0x123456789abcdef0) == (x & ~0x123456789abcdef0); |
|
| 13 | + | assert invertedOr(x, 0x123456789abcdef0) == (x | ~0x123456789abcdef0); |
|
| 14 | + | assert invertedXor(x, 0x123456789abcdef0) == ~(x ^ 0x123456789abcdef0); |
|
| 15 | + | assert rotate(x) == ((x >> 61) | (x << 3)); |
|
| 16 | + | assert rotateWord(x as u32) == ((x as u32 >> 29) | (x as u32 << 3)); |
|
| 17 | + | for bit in [0 as u64, 31, 32, 63, 64, 127] { |
|
| 18 | + | assert rotateVariable(x, bit) == rotateReference(x, bit); |
|
| 19 | + | assert rotateWordVariable(x as u32, bit as u32) |
|
| 20 | + | == rotateWordReference(x as u32, bit as u32); |
|
| 21 | + | let mask = 1 as u64 << (bit & 63); |
|
| 22 | + | assert setBit(x, bit) == (x | mask); |
|
| 23 | + | assert clearBit(x, bit) == (x & ~mask); |
|
| 24 | + | assert invertBit(x, bit) == (x ^ mask); |
|
| 25 | + | assert extractBit(x, bit) == ((x >> (bit & 63)) & 1); |
|
| 26 | + | } |
|
| 27 | + | assert extendAdd(x as u32, 9) == (x & 0xffffffff) + 9; |
|
| 28 | + | assert extendShift(x as u32) == ((x & 0xffffffff) << 3); |
|
| 29 | + | assert extendShiftAdd(x as u32, 9) == ((x & 0xffffffff) << 3) + 9; |
|
| 30 | + | assert extendByte(x as i8) == x as i8 as i64; |
|
| 31 | + | assert extendHalf(x as i16) == x as i16 as i64; |
|
| 32 | + | assert unsignedHalf(x as u16) == (x & 0xffff); |
|
| 33 | + | } |
|
| 34 | + | let words = [11 as u64, 22, 33, 44]; |
|
| 35 | + | for i in 0 .. 4 { |
|
| 36 | + | assert words[i] == (i as u64 + 1) * 11; |
|
| 37 | + | } |
|
| 38 | + | return 0; |
|
| 39 | + | } |
|
| 40 | + | ||
| 41 | + | /// Add a scaled full-width value. |
|
| 42 | + | fn shiftAdd(x: u64, y: u64) -> u64 { |
|
| 43 | + | return (x << 3) + y; |
|
| 44 | + | } |
|
| 45 | + | ||
| 46 | + | /// Clear the bits set in the second operand. |
|
| 47 | + | fn invertedAnd(x: u64, y: u64) -> u64 { |
|
| 48 | + | return x & ~y; |
|
| 49 | + | } |
|
| 50 | + | ||
| 51 | + | /// Set the bits clear in the second operand. |
|
| 52 | + | fn invertedOr(x: u64, y: u64) -> u64 { |
|
| 53 | + | return x | ~y; |
|
| 54 | + | } |
|
| 55 | + | ||
| 56 | + | /// Return the complement of the exclusive-or. |
|
| 57 | + | fn invertedXor(x: u64, y: u64) -> u64 { |
|
| 58 | + | return ~(x ^ y); |
|
| 59 | + | } |
|
| 60 | + | ||
| 61 | + | /// Rotate the full register left by three bits. |
|
| 62 | + | fn rotate(x: u64) -> u64 { |
|
| 63 | + | return (x << 3) | (x >> 61); |
|
| 64 | + | } |
|
| 65 | + | ||
| 66 | + | /// Rotate the low word left by three bits. |
|
| 67 | + | fn rotateWord(x: u32) -> u32 { |
|
| 68 | + | return (x << 3) | (x >> 29); |
|
| 69 | + | } |
|
| 70 | + | ||
| 71 | + | /// Set the selected bit. |
|
| 72 | + | fn setBit(x: u64, n: u64) -> u64 { |
|
| 73 | + | return x | (1 as u64 << n); |
|
| 74 | + | } |
|
| 75 | + | ||
| 76 | + | /// Clear the selected bit. |
|
| 77 | + | fn clearBit(x: u64, n: u64) -> u64 { |
|
| 78 | + | return x & ~(1 as u64 << n); |
|
| 79 | + | } |
|
| 80 | + | ||
| 81 | + | /// Invert the selected bit. |
|
| 82 | + | fn invertBit(x: u64, n: u64) -> u64 { |
|
| 83 | + | return x ^ (1 as u64 << n); |
|
| 84 | + | } |
|
| 85 | + | ||
| 86 | + | /// Extract the selected bit. |
|
| 87 | + | fn extractBit(x: u64, n: u64) -> u64 { |
|
| 88 | + | return (x >> n) & 1; |
|
| 89 | + | } |
|
| 90 | + | ||
| 91 | + | /// Zero-extend a word and add a full-width value. |
|
| 92 | + | fn extendAdd(x: u32, y: u64) -> u64 { |
|
| 93 | + | return x as u64 + y; |
|
| 94 | + | } |
|
| 95 | + | ||
| 96 | + | /// Shift a zero-extended word. |
|
| 97 | + | fn extendShift(x: u32) -> u64 { |
|
| 98 | + | return x as u64 << 3; |
|
| 99 | + | } |
|
| 100 | + | ||
| 101 | + | /// Add a scaled zero-extended word. |
|
| 102 | + | fn extendShiftAdd(x: u32, y: u64) -> u64 { |
|
| 103 | + | return (x as u64 << 3) + y; |
|
| 104 | + | } |
|
| 105 | + | ||
| 106 | + | /// Sign-extend a byte. |
|
| 107 | + | fn extendByte(x: i8) -> i64 { |
|
| 108 | + | return x as i64; |
|
| 109 | + | } |
|
| 110 | + | ||
| 111 | + | /// Sign-extend a halfword. |
|
| 112 | + | fn extendHalf(x: i16) -> i64 { |
|
| 113 | + | return x as i64; |
|
| 114 | + | } |
|
| 115 | + | ||
| 116 | + | /// Zero-extend a halfword. |
|
| 117 | + | fn unsignedHalf(x: u16) -> u64 { |
|
| 118 | + | return x as u64; |
|
| 119 | + | } |
|
| 120 | + | ||
| 121 | + | /// Rotate a register by a masked runtime count. |
|
| 122 | + | fn rotateVariable(x: u64, n: u64) -> u64 { |
|
| 123 | + | return (x << n) | (x >> (64 - n)); |
|
| 124 | + | } |
|
| 125 | + | ||
| 126 | + | /// Compute a rotation with explicitly masked counts. |
|
| 127 | + | fn rotateReference(x: u64, n: u64) -> u64 { |
|
| 128 | + | let k = n & 63; |
|
| 129 | + | return (x << k) | (x >> ((64 - k) & 63)); |
|
| 130 | + | } |
|
| 131 | + | ||
| 132 | + | /// Rotate a word by a masked runtime count. |
|
| 133 | + | fn rotateWordVariable(x: u32, n: u32) -> u32 { |
|
| 134 | + | return (x << n) | (x >> (32 - n)); |
|
| 135 | + | } |
|
| 136 | + | ||
| 137 | + | /// Compute a word rotation with explicitly masked counts. |
|
| 138 | + | fn rotateWordReference(x: u32, n: u32) -> u32 { |
|
| 139 | + | let k = n & 31; |
|
| 140 | + | return (x << k) | (x >> ((32 - k) & 31)); |
|
| 141 | + | } |
|
| 142 | + | ||
| 143 | + | /// Multiply a register by three. |
|
| 144 | + | fn multiplyThree(x: u64) -> u64 { |
|
| 145 | + | return x * 3; |
|
| 146 | + | } |
|
| 147 | + | ||
| 148 | + | /// Multiply a register by five. |
|
| 149 | + | fn multiplyFive(x: u64) -> u64 { |
|
| 150 | + | return x * 5; |
|
| 151 | + | } |
|
| 152 | + | ||
| 153 | + | /// Multiply a register by nine. |
|
| 154 | + | fn multiplyNine(x: u64) -> u64 { |
|
| 155 | + | return x * 9; |
|
| 156 | + | } |
|
| 157 | + | ||
| 158 | + | /// Keep the low halfword. |
|
| 159 | + | fn maskHalf(x: u64) -> u64 { |
|
| 160 | + | return x & 0xffff; |
|
| 161 | + | } |
|
| 162 | + | ||
| 163 | + | /// Keep the low word. |
|
| 164 | + | fn maskWord(x: u64) -> u64 { |
|
| 165 | + | return x & 0xffffffff; |
|
| 166 | + | } |