From 66710370bf1ef52933b48734fa43c690216070a3 Mon Sep 17 00:00:00 2001 From: Shadow Cat Date: Sat, 6 Jun 2026 23:26:17 -0400 Subject: [PATCH] x86_64 call & ret --- src/arch/x86_64/asm.rs | 2 ++ src/arch/x86_64/encode.rs | 11 +++++++++++ src/arch/x86_64/test/bin.rs | 13 +++++++++++++ src/backend/ir/mod.rs | 1 - x86_64_test | Bin 166 -> 222 bytes 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/arch/x86_64/asm.rs b/src/arch/x86_64/asm.rs index 1e9101c..976a4b6 100644 --- a/src/arch/x86_64/asm.rs +++ b/src/arch/x86_64/asm.rs @@ -8,6 +8,8 @@ pub struct Asm { pub enum Instr { Mov { dst: RegMode, src: RegImm }, Int { code: u8 }, + Call(Symbol), + Ret, Syscall, Lea { dst: RegMode, sym: Symbol }, } diff --git a/src/arch/x86_64/encode.rs b/src/arch/x86_64/encode.rs index a80d4e3..67af13f 100644 --- a/src/arch/x86_64/encode.rs +++ b/src/arch/x86_64/encode.rs @@ -106,6 +106,15 @@ impl Encoder { self.data.extend([0x0f, 0x05]) } + pub fn call(&mut self, sym: Symbol) { + self.data.push(0xe8); + self.sym_offset4(sym); + } + + pub fn ret(&mut self) { + self.data.push(0xc3); + } + /// inserts a 32 bit offset from a symbol pub fn sym_offset4(&mut self, sym: Symbol) { let Some(addr) = self.sym_tab.get(sym) else { @@ -123,6 +132,8 @@ impl Encoder { Instr::Int { code } => self.int(code), Instr::Syscall => self.syscall(), Instr::Lea { dst, sym } => self.lea(dst, sym), + Instr::Call(sym) => self.call(sym), + Instr::Ret => self.ret(), } Ok(()) } diff --git a/src/arch/x86_64/test/bin.rs b/src/arch/x86_64/test/bin.rs index 8596c9d..bfc5000 100644 --- a/src/arch/x86_64/test/bin.rs +++ b/src/arch/x86_64/test/bin.rs @@ -8,6 +8,18 @@ pub fn run() { let mut program = Program::::default(); let text = b"Hello world!\n"; let text_sym = program.ro_data(text); + let text2 = "世界、こんにちは!\n"; + let text_sym2 = program.ro_data(text2); + let hello2 = program.func([BInstr::Asm(Asm { + instrs: vec![ + mov(ax, 1), + mov(di, 1), + lea(rsi, text_sym2), + mov(dx, text2.len() as u64), + Instr::Syscall, + Instr::Ret, + ], + })]); let entry = program.func([BInstr::Asm(Asm { instrs: vec![ mov(ax, 1), @@ -15,6 +27,7 @@ pub fn run() { lea(rsi, text_sym), mov(dx, text.len() as u64), Instr::Syscall, + Instr::Call(hello2), mov(ax, 0x3c), mov(di, 39), Instr::Syscall, diff --git a/src/backend/ir/mod.rs b/src/backend/ir/mod.rs index cc2b2ce..7daad48 100644 --- a/src/backend/ir/mod.rs +++ b/src/backend/ir/mod.rs @@ -27,7 +27,6 @@ pub enum Instr { Set { dst: VarId, src: Vec }, Call { dst: FnId, args: Vec }, Copy { dst: VarId, src: VarId }, - Free(VarId), Asm(A::Asm), } diff --git a/x86_64_test b/x86_64_test index 2d5f1f72b2d13107bd99ab6d4e742a531c0ba942..92c5c4137a465a86ce278ad6cf49d85acc6eaa05 100755 GIT binary patch delta 114 zcmZ3+c#m;{1mlj0lJ1;o3=ohuF;gb;$&P8yr}jKc~ xMy|9Sj0|ZG`xzNLdQGqV|NlR2mka|x>tST!4Paqjpzw=xAm$DmkPdYq9{>ktG9&;1 delta 58 zcmcb|xQuav1Y_$&Nq0^?1_;obm?@)}wu6x&&0#+ygGaCF