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
Generated
+109
View File
@@ -2,6 +2,115 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4 version = 4
[[package]]
name = "arrayvec"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
[[package]]
name = "bitcode"
version = "0.6.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a6ed1b54d8dc333e7be604d00fa9262f4635485ffea923647b6521a5fff045d"
dependencies = [
"arrayvec",
"bitcode_derive",
"bytemuck",
"glam",
"serde",
]
[[package]]
name = "bitcode_derive"
version = "0.6.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "238b90427dfad9da4a9abd60f3ec1cdee6b80454bde49ed37f1781dd8e9dc7f9"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "bytemuck"
version = "1.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8efb64bd706a16a1bdde310ae86b351e4d21550d98d056f22f8a7f7a2183fec"
[[package]]
name = "glam"
version = "0.33.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "898f5a568a84989b6c0f8caa50a93074b97dbdc58fc6d9543157bb4562758933"
[[package]] [[package]]
name = "lang" name = "lang"
version = "0.1.0" version = "0.1.0"
dependencies = [
"bitcode",
]
[[package]]
name = "proc-macro2"
version = "1.0.106"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
dependencies = [
"proc-macro2",
]
[[package]]
name = "serde"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
dependencies = [
"serde_core",
]
[[package]]
name = "serde_core"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "2.0.117"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
+3
View File
@@ -4,3 +4,6 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
[dev-dependencies]
bitcode = "0.6.9"
+2 -2
View File
@@ -48,7 +48,7 @@ fn linux() -> Result<(), CompilerMsg> {
program.entry = Some(entry); program.entry = Some(entry);
let linked = program.compile().expect("failed to compile"); let linked = program.compile().expect("failed to compile");
let binary = linked.to_elf(); let binary = linked.to_elf();
let path = "./x86_64_test"; let path = "./test/x86_64_test";
write(path, &binary); write(path, &binary);
println!("running..."); println!("running...");
let gdb = false; let gdb = false;
@@ -106,7 +106,7 @@ fn windows() -> Result<(), CompilerMsg> {
let linked = program.compile().expect("failed to compile"); let linked = program.compile().expect("failed to compile");
let binary = linked.to_pe(); let binary = linked.to_pe();
let path = "./x86_64_test.exe"; let path = "./test/x86_64_test.exe";
write(path, &binary); write(path, &binary);
let mut cmd = Command::new("wine"); let mut cmd = Command::new("wine");
+43 -4
View File
@@ -1,5 +1,5 @@
use crate::arch::x86_64::*; 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] = &[ const DISPS: &[i32] = &[
0x0, 0x0,
@@ -34,7 +34,8 @@ const WIDTHS: &[Width] = &[Width::B8, Width::B16, Width::B32, Width::B64];
#[test] #[test]
fn mov() { fn mov() {
let c = &mut Code::default(); let c = &mut TestCtx::new();
for &r1 in Reg::IMPORTANT { for &r1 in Reg::IMPORTANT {
for &r2 in Reg::IMPORTANT { for &r2 in Reg::IMPORTANT {
eq(c, format!("mov {r1}, {r2}"), |c| c.mov(r1, r2)); 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( fn eq(
code: &mut Code, ctx: &mut TestCtx,
asm: impl AsRef<str>, asm: impl AsRef<str>,
instr: impl FnOnce(&mut Code) -> Result<(), CompilerMsg>, instr: impl FnOnce(&mut Code) -> Result<(), CompilerMsg>,
) { ) {
let asm = asm.as_ref(); 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); let res = instr(code);
match (expected, res) { match (expected, res) {
(Ok(expected), Err(e)) => { (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.write_all(binary).expect("Failed to write to file");
file.sync_all().expect("Failed to sync 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));
}
}
}
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.