This commit is contained in:
2026-06-07 21:22:32 -04:00
parent a086fa6590
commit c9add923be
14 changed files with 413 additions and 46 deletions
+19 -5
View File
@@ -50,7 +50,7 @@ fn compile_instr(encoder: &mut Encoder, instr: &BInstr) -> Result<(), CompilerMs
impl Encoder {
// assembly
pub fn mov(&mut self, dst: RegMode, src: impl Into<RegImm>) -> Result<(), CompilerMsg> {
pub fn mov(&mut self, dst: RegMode, src: impl Into<RegModeImm>) -> Result<(), CompilerMsg> {
let src = src.into();
let width = dst.width;
if width == BitWidth::B16 {
@@ -59,7 +59,7 @@ impl Encoder {
let dst8 = dst.gt8();
let b64 = width == BitWidth::B64;
let b8 = width == BitWidth::B8;
let src8 = if let RegImm::Reg(src) = src {
let src8 = if let RegModeImm::Reg(src) = src {
src.gt8()
} else {
false
@@ -70,7 +70,7 @@ impl Encoder {
.push(0x40 | dst8 as u8 | ((b64 as u8) << 3) | ((src8 as u8) << 2));
}
match src {
RegImm::Reg(src) => {
RegModeImm::Reg(src) => {
if dst.width != src.width {
return Err("src and dst are not the same size".into());
}
@@ -78,7 +78,7 @@ impl Encoder {
let modrm = 0b11_000_000 | (src.base() << 3) | dst.base();
self.data.push(modrm);
}
RegImm::Imm(imm) => {
RegModeImm::Imm(imm) => {
if imm > width.max() {
return Err("immediate cannot fit in register".into());
}
@@ -122,6 +122,17 @@ impl Encoder {
self.data.push(0x50 | reg.base());
}
pub fn push_imm(&mut self, imm: u64) {
const U8: u64 = 2 << 8;
if let 0..U8 = imm {
self.data.push(0x6a);
self.data.push(imm as u8);
} else {
self.data.push(0x68);
self.data.extend(imm.to_le_bytes());
}
}
pub fn pop(&mut self, reg: Reg) {
if reg.gt8() {
self.data.push(0x41);
@@ -148,7 +159,10 @@ impl Encoder {
Instr::Lea { dst, sym } => self.lea(dst, sym),
Instr::Call(sym) => self.call(sym),
Instr::Ret => self.ret(),
Instr::Push(reg) => self.push(reg),
Instr::Push(val) => match val {
RegImm::Reg(reg) => self.push(reg),
RegImm::Imm(imm) => self.push_imm(imm),
},
Instr::Pop(reg) => self.pop(reg),
}
Ok(())