remove intermediate enum / directly encode assembly

This commit is contained in:
2026-06-11 21:49:04 -04:00
parent 91f5db6950
commit b03f755252
11 changed files with 414 additions and 668 deletions
+40 -43
View File
@@ -18,34 +18,32 @@ fn linux() -> Result<(), CompilerMsg> {
let text_sym2 = program.ro_data("hello_jp", text2);
let hello2 = program.func(
"hello2",
[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,
],
})],
[BInstr::Asm(encode(|c| {
c.mov(ax, 1)?;
c.mov(di, 1)?;
c.lea(rsi, text_sym2);
c.mov(dx, text2.len() as u64)?;
c.syscall();
c.ret();
Ok(())
})?)],
);
let entry = program.func(
"main",
[BInstr::Asm(Asm {
instrs: vec![
mov(di, 39)?,
push(rdi)?,
mov(ax, 1)?,
mov(di, 1)?,
lea(rsi, text_sym),
mov(dx, text.len() as u64)?,
Instr::Syscall,
Instr::Call(hello2),
mov(ax, 0x3c)?,
pop(rdi),
Instr::Syscall,
],
})],
[BInstr::Asm(encode(|c| {
c.mov(rdi, 39)?;
c.push(rdi)?;
c.mov(ax, 1)?;
c.mov(di, 1)?;
c.lea(rsi, text_sym);
c.mov(dx, text.len() as u64)?;
c.syscall();
c.call(hello2);
c.mov(ax, 0x3c)?;
c.pop(rdi)?;
c.syscall();
Ok(())
})?)],
);
program.entry = Some(entry);
let linked = program.compile().expect("failed to compile");
@@ -86,24 +84,23 @@ fn windows() -> Result<(), CompilerMsg> {
let written = program.ro_data("written", [0; 4]);
let entry = program.func(
"main",
[BInstr::Asm(Asm {
instrs: vec![
Instr::Sub,
// stdout
mov(ecx, -11)?,
Instr::CallM(get_std_handle),
// write
mov(rcx, rax)?,
lea(rdx, text_sym),
mov(r8d, text.len() as u64)?,
lea(r9, written),
mov(mem(rsp, 0x20)?, 0)?,
Instr::CallM(write_file),
// exit
mov(ecx, 39)?,
Instr::CallM(exit_process),
],
})],
[BInstr::Asm(encode(|c| {
c.sub();
// stdout
c.mov(ecx, -11)?;
c.call_mem(get_std_handle);
// write
c.mov(rcx, rax)?;
c.lea(rdx, text_sym);
c.mov(r8d, text.len() as u64)?;
c.lea(r9, written);
c.mov(mem(rsp, 0x20), 0)?;
c.call_mem(write_file);
// exit
c.mov(ecx, 39)?;
c.call_mem(exit_process);
Ok(())
})?)],
);
program.entry = Some(entry);
let linked = program.compile().expect("failed to compile");