x86_64 compiler + elf output (can compile code that returns exit code)

This commit is contained in:
2026-06-03 01:50:43 -04:00
parent 473ddab0d4
commit 380a0f977a
11 changed files with 422 additions and 76 deletions
+31
View File
@@ -0,0 +1,31 @@
use crate::backend::{
Addr, elf,
x86_64::{Asm, Instr, mov, reg::*},
};
use std::{fs::OpenOptions, io::Write, os::unix::fs::OpenOptionsExt, process::Command};
pub fn test_x86_64() {
let asm = Asm {
instrs: vec![mov(eax, 1), mov(ebx, 39), Instr::Int { code: 0x80 }],
};
let mut out = Vec::new();
asm.compile(&mut out).expect("failed to compile");
let binary = elf::create(&out, Addr(0));
let path = "./x86_64_test";
let mut file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.mode(0o750)
.open(path)
.expect("Failed to create file");
file.write_all(&binary).expect("Failed to write to file");
file.sync_all().expect("Failed to sync file");
drop(file);
println!("running...");
let mut proc = Command::new(path).spawn().expect("failed to run");
let status = proc.wait().expect("failed to wait");
if let Some(code) = status.code() {
std::process::exit(code);
}
}