fix tests
This commit is contained in:
@@ -63,39 +63,58 @@ pub struct TestCtx {
|
|||||||
pub fn eq(
|
pub fn eq(
|
||||||
ctx: &mut TestCtx,
|
ctx: &mut TestCtx,
|
||||||
asm: impl AsRef<str>,
|
asm: impl AsRef<str>,
|
||||||
instr: impl FnOnce(&mut Code) -> Result<(), CompilerMsg>,
|
instr: impl Fn(&mut Code) -> Result<(), CompilerMsg>,
|
||||||
) {
|
) {
|
||||||
let asm = asm.as_ref();
|
let asm = asm.as_ref();
|
||||||
let expected = if let Some(val) = ctx.cache.get(asm) {
|
let (mut res, cache) = eq_(ctx, asm, &instr);
|
||||||
val
|
if res.is_err() && cache {
|
||||||
|
ctx.cache.remove(asm);
|
||||||
|
res = eq_(ctx, asm, &instr).0;
|
||||||
|
}
|
||||||
|
if let Err(err) = res {
|
||||||
|
panic!("{err}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[track_caller]
|
||||||
|
pub fn eq_(
|
||||||
|
ctx: &mut TestCtx,
|
||||||
|
asm: &str,
|
||||||
|
instr: impl FnOnce(&mut Code) -> Result<(), CompilerMsg>,
|
||||||
|
) -> (Result<(), String>, bool) {
|
||||||
|
let (expected, cache) = if let Some(val) = ctx.cache.get(asm) {
|
||||||
|
(val, true)
|
||||||
} else {
|
} else {
|
||||||
ctx.changed = true;
|
ctx.changed = true;
|
||||||
let res = nasm(asm);
|
let res = nasm(asm);
|
||||||
ctx.cache.insert(asm.to_string(), res);
|
ctx.cache.insert(asm.to_string(), res);
|
||||||
ctx.cache.get(asm).unwrap()
|
(ctx.cache.get(asm).unwrap(), false)
|
||||||
};
|
};
|
||||||
let code = &mut ctx.code;
|
let code = &mut ctx.code;
|
||||||
let res = instr(code);
|
let res = instr(code);
|
||||||
match (expected, res) {
|
let res = match (expected, res) {
|
||||||
(Ok(expected), Err(e)) => {
|
(Ok(expected), Err(e)) => Err(format!(
|
||||||
panic!(
|
"{asm}: failed to compile: {}\nexpected: {expected:x?}",
|
||||||
"{asm}: failed to compile: {}\nexpected: {expected:x?}",
|
e.msg
|
||||||
e.msg
|
)),
|
||||||
);
|
|
||||||
}
|
|
||||||
(Err(e), Ok(_)) => {
|
(Err(e), Ok(_)) => {
|
||||||
let res = &code.bytes[..];
|
let res = &code.bytes[..];
|
||||||
panic!("{asm}: should not have compiled:\n{e}\ngot: {res:x?}");
|
Err(format!(
|
||||||
|
"{asm}: should not have compiled:\n{e}\ngot: {res:x?}"
|
||||||
|
))
|
||||||
}
|
}
|
||||||
(Err(_), Err(_)) => (),
|
(Err(_), Err(_)) => Ok(()),
|
||||||
(Ok(expected), Ok(_)) => {
|
(Ok(expected), Ok(_)) => {
|
||||||
let res = &code.bytes[..];
|
let res = &code.bytes[..];
|
||||||
if expected != res {
|
if expected != res {
|
||||||
panic!("{asm}: expected {expected:x?}, got {res:x?}")
|
Err(format!("{asm}: expected {expected:x?}, got {res:x?}"))
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
code.bytes.clear();
|
ctx.code.bytes.clear();
|
||||||
|
(res, cache)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn nasm(input: &str) -> Result<Vec<u8>, String> {
|
fn nasm(input: &str) -> Result<Vec<u8>, String> {
|
||||||
@@ -103,7 +122,7 @@ fn nasm(input: &str) -> Result<Vec<u8>, String> {
|
|||||||
let fout = "/tmp/69420nasm_out.o";
|
let fout = "/tmp/69420nasm_out.o";
|
||||||
let input = "result:".to_string() + input;
|
let input = "result:".to_string() + input;
|
||||||
write(fin, input.as_bytes());
|
write(fin, input.as_bytes());
|
||||||
run(["nasm", "-O1", "-w+error", "-felf64", fin, &format!("-o{fout}")])?;
|
run(["nasm", "-w+error", "-felf64", fin, &format!("-o{fout}")])?;
|
||||||
let output = run(["objdump", "--no-addresses", "-dw", "-Mintel", fout])?;
|
let output = run(["objdump", "--no-addresses", "-dw", "-Mintel", fout])?;
|
||||||
let mut iter = output.lines().skip_while(|l| !l.contains("result")).skip(1);
|
let mut iter = output.lines().skip_while(|l| !l.contains("result")).skip(1);
|
||||||
let res_line = iter.next().unwrap().trim();
|
let res_line = iter.next().unwrap().trim();
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ def_regs! {
|
|||||||
0b0011 : rbx ebx bx bl,
|
0b0011 : rbx ebx bx bl,
|
||||||
|
|
||||||
0b0100 : rsp esp sp spl norex=ah !_,
|
0b0100 : rsp esp sp spl norex=ah !_,
|
||||||
0b0101 : rbp ebp bp bpl norex=ch,
|
0b0101 : rbp ebp bp bpl norex=ch !_,
|
||||||
0b0110 : rsi esi si sil norex=dh !_,
|
0b0110 : rsi esi si sil norex=dh !_,
|
||||||
0b0111 : rdi edi di dil norex=bh,
|
0b0111 : rdi edi di dil norex=bh,
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user