fix tests
This commit is contained in:
@@ -63,39 +63,58 @@ pub struct TestCtx {
|
||||
pub fn eq(
|
||||
ctx: &mut TestCtx,
|
||||
asm: impl AsRef<str>,
|
||||
instr: impl FnOnce(&mut Code) -> Result<(), CompilerMsg>,
|
||||
instr: impl Fn(&mut Code) -> Result<(), CompilerMsg>,
|
||||
) {
|
||||
let asm = asm.as_ref();
|
||||
let expected = if let Some(val) = ctx.cache.get(asm) {
|
||||
val
|
||||
let (mut res, cache) = eq_(ctx, asm, &instr);
|
||||
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 {
|
||||
ctx.changed = true;
|
||||
let res = nasm(asm);
|
||||
ctx.cache.insert(asm.to_string(), res);
|
||||
ctx.cache.get(asm).unwrap()
|
||||
(ctx.cache.get(asm).unwrap(), false)
|
||||
};
|
||||
let code = &mut ctx.code;
|
||||
let res = instr(code);
|
||||
match (expected, res) {
|
||||
(Ok(expected), Err(e)) => {
|
||||
panic!(
|
||||
"{asm}: failed to compile: {}\nexpected: {expected:x?}",
|
||||
e.msg
|
||||
);
|
||||
}
|
||||
let res = match (expected, res) {
|
||||
(Ok(expected), Err(e)) => Err(format!(
|
||||
"{asm}: failed to compile: {}\nexpected: {expected:x?}",
|
||||
e.msg
|
||||
)),
|
||||
(Err(e), Ok(_)) => {
|
||||
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(_)) => {
|
||||
let res = &code.bytes[..];
|
||||
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> {
|
||||
@@ -103,7 +122,7 @@ fn nasm(input: &str) -> Result<Vec<u8>, String> {
|
||||
let fout = "/tmp/69420nasm_out.o";
|
||||
let input = "result:".to_string() + input;
|
||||
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 mut iter = output.lines().skip_while(|l| !l.contains("result")).skip(1);
|
||||
let res_line = iter.next().unwrap().trim();
|
||||
|
||||
@@ -14,7 +14,7 @@ def_regs! {
|
||||
0b0011 : rbx ebx bx bl,
|
||||
|
||||
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 !_,
|
||||
0b0111 : rdi edi di dil norex=bh,
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user