nasm test cache

This commit is contained in:
2026-06-12 17:38:22 -04:00
parent e2ebf5c681
commit 51bdc5c684
8 changed files with 157 additions and 6 deletions
+2 -2
View File
@@ -48,7 +48,7 @@ fn linux() -> Result<(), CompilerMsg> {
program.entry = Some(entry);
let linked = program.compile().expect("failed to compile");
let binary = linked.to_elf();
let path = "./x86_64_test";
let path = "./test/x86_64_test";
write(path, &binary);
println!("running...");
let gdb = false;
@@ -106,7 +106,7 @@ fn windows() -> Result<(), CompilerMsg> {
let linked = program.compile().expect("failed to compile");
let binary = linked.to_pe();
let path = "./x86_64_test.exe";
let path = "./test/x86_64_test.exe";
write(path, &binary);
let mut cmd = Command::new("wine");
+43 -4
View File
@@ -1,5 +1,5 @@
use crate::arch::x86_64::*;
use std::{fs::OpenOptions, io::Write, process::Command};
use std::{collections::HashMap, fs::OpenOptions, io::Write, process::Command};
const DISPS: &[i32] = &[
0x0,
@@ -34,7 +34,8 @@ const WIDTHS: &[Width] = &[Width::B8, Width::B16, Width::B32, Width::B64];
#[test]
fn mov() {
let c = &mut Code::default();
let c = &mut TestCtx::new();
for &r1 in Reg::IMPORTANT {
for &r2 in Reg::IMPORTANT {
eq(c, format!("mov {r1}, {r2}"), |c| c.mov(r1, r2));
@@ -59,13 +60,27 @@ fn mov() {
}
}
struct TestCtx {
code: Code,
cache: HashMap<String, Result<Vec<u8>, String>>,
changed: bool,
}
fn eq(
code: &mut Code,
ctx: &mut TestCtx,
asm: impl AsRef<str>,
instr: impl FnOnce(&mut Code) -> Result<(), CompilerMsg>,
) {
let asm = asm.as_ref();
let expected = nasm(asm);
let expected = if let Some(val) = ctx.cache.get(asm) {
val
} else {
ctx.changed = true;
let res = nasm(asm);
ctx.cache.insert(asm.to_string(), res);
ctx.cache.get(asm).unwrap()
};
let code = &mut ctx.code;
let res = instr(code);
match (expected, res) {
(Ok(expected), Err(e)) => {
@@ -129,3 +144,27 @@ fn write(path: &str, binary: &[u8]) {
file.write_all(binary).expect("Failed to write to file");
file.sync_all().expect("Failed to sync file");
}
const CACHE_PATH: &str = "test/nasm_test_cache";
impl TestCtx {
fn new() -> Self {
let cache = match std::fs::read(CACHE_PATH) {
Ok(bytes) => bitcode::decode(&bytes).unwrap_or_default(),
Err(_) => Default::default(),
};
Self {
code: Default::default(),
cache,
changed: Default::default(),
}
}
}
impl Drop for TestCtx {
fn drop(&mut self) {
if self.changed {
write(CACHE_PATH, &bitcode::encode(&self.cache));
}
}
}