use std::collections::HashSet;
use super::{HELD, decode, function_starts, text};
use vlk_base::target::Isa;
use vlk_ir::ir::{FunctionIr, Stmt};
use vlk_loader::loader::Image;
const NOP: u32 = 0;
const RETURN: u32 = 0x03e0_0008;
fn code(words: &[u32]) -> Vec<u8> {
words.iter().flat_map(|word| word.to_le_bytes()).collect()
}
fn lifted(bytes: Vec<u8>) -> FunctionIr {
let size = bytes.len() as u64;
let program = vlk_program::program::Program::new(Image::from_code(Isa::Mips32, 0x1000, bytes));
let mut ir = FunctionIr::single_block(Isa::Mips32, Vec::new());
ir.blocks = crate::lift::lift(&program, 0x1000, size, &HashSet::new()).unwrap();
ir
}
#[test]
fn instructions_decode_to_their_usual_text() {
for (word, expected) in [
(0x0085_1021u32, "addu $v0, $a0, $a1"),
(0x27bd_ffe8, "addiu $sp, $sp, -0x18"),
(0xafbf_0014, "sw $ra, 0x14($sp)"),
(0x8f99_8018, "lw $t9, -0x7fe8($gp)"),
(0x0320_f809, "jalr $t9"),
(0x03e0_0008, "jr $ra"),
(0x0000_0000, "nop"),
(0x00a0_2025, "move $a0, $a1"),
(0x1000_0003, "b 0x1010"),
(0x0411_ffff, "bal 0x1000"),
(0x1440_0002, "bnez $v0, 0x100c"),
(0x0081_0018, "mult $a0, $at"),
(0x7c82_0900, "ext $v0, $a0, 0x4, 0x2"),
(0x3c02_0012, "lui $v0, 0x12"),
(0x0000_000c, "syscall"),
] {
let instruction = decode::decode_word(word).unwrap();
assert_eq!(text::text(&instruction, 0x1000), expected, "{word:#x}");
}
assert_eq!(decode::decode_word(0x0000_0005), None);
assert_eq!(decode::decode(&[0, 0], false), None);
}
#[test]
fn a_delay_slot_runs_before_the_branch_that_read_its_operands() {
let ir = lifted(code(&[0x1085_0002, 0x2484_0001, RETURN, NOP, RETURN, NOP]));
let first = &ir.blocks[0].stmts;
assert!(
matches!(&first[0], Stmt::Assign { dest, .. } if dest.index == HELD[0]),
"{}",
ir.render()
);
assert!(matches!(&first[1], Stmt::Assign { dest, .. } if dest.index == 2));
assert!(matches!(
first.last(),
Some(Stmt::Brif { target: 0x100c, .. })
));
}
#[test]
fn a_branch_that_may_skip_its_slot_splits_in_two() {
let ir = lifted(code(&[0x5085_0002, 0x2484_0005, RETURN, NOP, RETURN, NOP]));
assert!(matches!(
ir.blocks[0].stmts.last(),
Some(Stmt::Brif { target: 0x1008, .. })
));
let slot = ir
.blocks
.iter()
.find(|block| block.start == 0x1004)
.unwrap();
assert!(matches!(
slot.stmts.last(),
Some(Stmt::Br {
target: Some(0x100c),
..
})
));
}
#[test]
fn the_two_halves_of_an_unaligned_word_are_one_access() {
let ir = lifted(code(&[0x9882_0000, 0x8882_0003, RETURN, NOP]));
assert!(
!ir.blocks[0]
.stmts
.iter()
.any(|stmt| matches!(stmt, Stmt::Unsupported { .. })),
"{}",
ir.render()
);
assert!(ir.render().contains("load32"), "{}", ir.render());
let ir = lifted(code(&[0x9882_0000, RETURN, NOP]));
assert!(
!ir.blocks[0]
.stmts
.iter()
.any(|stmt| matches!(stmt, Stmt::Unsupported { .. }))
);
assert!(ir.render().contains("#0xfffffffc"), "{}", ir.render());
}
#[test]
fn halves_a_few_instructions_apart_are_one_access() {
let whole = |words: &[u32]| !lifted(code(words)).render().contains("#0xfffffffc");
assert!(whole(&[0x8882_0003, 0x8fa9_0000, 0x9882_0000, RETURN, NOP]));
assert!(!whole(&[
0x8882_0003,
0xaca9_0000,
0x9882_0000,
RETURN,
NOP
]));
assert!(!whole(&[
0x8882_0003,
0x2484_0004,
0x9882_0000,
RETURN,
NOP
]));
assert!(!whole(&[
0x8882_0003,
0x0042_4821,
0x9882_0000,
RETURN,
NOP
]));
assert!(whole(&[0xa882_0003, 0xac89_0008, 0xb882_0000, RETURN, NOP]));
assert!(!whole(&[
0xa882_0003,
0xac89_0002,
0xb882_0000,
RETURN,
NOP
]));
assert!(!whole(&[
0xa882_0003,
0x8fa9_0000,
0xb882_0000,
RETURN,
NOP
]));
assert!(!whole(&[
0x10a0_0003,
NOP,
0x8882_0003,
NOP,
0x9882_0000,
RETURN,
NOP
]));
assert!(!whole(&[
0x10a0_0002,
NOP,
0x8882_0003,
0x9882_0000,
RETURN,
NOP
]));
}
#[test]
fn gp_set_from_a_constant_stays_known_when_read_back_from_the_frame() {
let ir = lifted(code(&[
0x3c1c_005a,
0x279c_4c90,
0x27bd_ffe0,
0xafbc_0010,
0x8fbc_0010,
0x2782_0004,
RETURN,
NOP,
]));
assert!(ir.render().contains("v0 = #0x5a4c94"), "{}", ir.render());
}
#[test]
fn gp_set_from_a_constant_and_t9_is_their_sum() {
let ir = lifted(code(&[
0x3c1c_001a,
0x279c_2a60,
0x0399_e021,
0x2782_0004,
RETURN,
NOP,
]));
assert!(ir.render().contains("v0 = #0x1a3a64"), "{}", ir.render());
}
#[test]
fn a_jump_through_t9_is_a_call_that_does_not_return() {
let ir = lifted(code(&[0x8c99_000c, 0x0320_0008, NOP]));
let stmts = &ir.blocks[0].stmts;
assert!(
matches!(
stmts[stmts.len() - 2],
Stmt::Call {
kind: vlk_types::types::CallKind::Tail,
..
}
),
"{}",
ir.render()
);
assert!(matches!(stmts.last(), Some(Stmt::Ret { .. })));
let ir = lifted(code(&[0x3c19_0040, 0x2739_46a4, 0x0320_0008, NOP]));
let stmts = &ir.blocks[0].stmts;
assert!(
matches!(
stmts[stmts.len() - 2],
Stmt::Call {
kind: vlk_types::types::CallKind::Tail,
target: Some(0x4046a4),
..
}
),
"{}",
ir.render()
);
assert!(matches!(stmts.last(), Some(Stmt::Ret { .. })));
let ir = lifted(code(&[0x8c82_000c, 0x0040_0008, NOP]));
assert!(matches!(
ir.blocks[0].stmts.last(),
Some(Stmt::BrIndirect { .. })
));
}
#[test]
fn code_that_read_only_data_points_at_is_a_case_unless_it_begins_a_function() {
let bytes = code(&[
0x0411_0006,
NOP,
RETURN,
NOP,
0x0080_1025,
RETURN,
NOP,
RETURN,
NOP,
]);
let tabled = HashSet::from([0x1010]);
assert_eq!(function_starts(&bytes, 0x1000, false, &tabled), [0x101c]);
let mut framed = bytes;
framed[16..20].copy_from_slice(&0x27bd_ffe8u32.to_le_bytes());
assert_eq!(
function_starts(&framed, 0x1000, false, &tabled),
[0x1010, 0x101c]
);
let padded = code(&[
0x0411_0007,
NOP,
RETURN,
NOP,
NOP,
0x0080_1025,
RETURN,
NOP,
RETURN,
NOP,
]);
let tabled = HashSet::from([0x1014]);
assert_eq!(
function_starts(&padded, 0x1000, false, &tabled),
[0x1014, 0x1020]
);
}
#[test]
fn an_argument_the_callee_does_not_read_keeps_its_place() {
let words = [0x0c00_0404, NOP, RETURN, NOP, RETURN, NOP];
let arguments = |floats: usize| {
let mut program =
vlk_program::program::Program::new(Image::from_code(Isa::Mips32, 0x1000, code(&words)));
program.image.functions.insert(
0x1010,
vlk_loader::loader::Function {
addr: 0x1010,
size: None,
size_estimated: false,
name: None,
aliases: Vec::new(),
library: None,
sources: Vec::new(),
},
);
let blocks =
crate::lift::lift_shared(&program, 0x1000, 0x10, &|_| vlk_loader::loader::Summary {
arity: Some(4),
floats: Some(floats),
inputs: Some([2u16, 5].into_iter().collect()),
..Default::default()
})
.unwrap();
blocks
.iter()
.flat_map(|block| &block.stmts)
.find_map(|stmt| match stmt {
Stmt::Call {
target: Some(0x1010),
args: Some(args),
..
} => Some(args.clone()),
_ => None,
})
.unwrap()
};
let plain = arguments(0);
assert_eq!(plain.len(), 4, "{plain:?}");
assert!(matches!(plain[1], vlk_ir::ir::Expr::Const(0)));
assert!(matches!(plain[2], vlk_ir::ir::Expr::Const(0)));
assert_eq!(
arguments(1)
.iter()
.filter(|arg| matches!(arg, vlk_ir::ir::Expr::Reg { index: 2..=5, .. }))
.count(),
2
);
}
#[test]
fn a_format_set_across_a_block_start_counts_the_arguments() {
let mut words = [0u32; 18];
words[0] = 0x10a0_0003;
words[1] = 0x3c04_0000;
words[3] = 0x3c04_0000;
words[4] = 0x2484_1030;
words[5] = 0x0c00_0410;
words[7] = RETURN;
words[12] = u32::from_le_bytes(*b"none");
words[16] = RETURN;
let arguments = |words: &[u32]| {
let mut program =
vlk_program::program::Program::new(Image::from_code(Isa::Mips32, 0x1000, code(words)));
program.image.functions.insert(
0x1040,
vlk_loader::loader::Function {
addr: 0x1040,
size: None,
size_estimated: false,
name: None,
aliases: Vec::new(),
library: None,
sources: Vec::new(),
},
);
let blocks =
crate::lift::lift_shared(&program, 0x1000, 0x24, &|_| vlk_loader::loader::Summary {
arity: Some(4),
further_from: 1,
..Default::default()
})
.unwrap();
blocks
.iter()
.flat_map(|block| &block.stmts)
.find_map(|stmt| match stmt {
Stmt::Call {
target: Some(0x1040),
args: Some(args),
..
} => Some(args.len()),
_ => None,
})
};
assert_eq!(arguments(&words), Some(1));
words[1] = 0x3c04_0001;
assert_eq!(arguments(&words), Some(4));
}
#[test]
fn a_call_whose_address_is_set_across_a_block_start_names_its_callee() {
let words = [
0x1080_0003,
0x3c19_0000,
NOP,
0x3c19_0000,
0x2739_1028,
0x0320_f809,
NOP,
RETURN,
NOP,
];
let lift = |words: &[u32], noreturn: bool| {
let bytes = code(words);
let size = bytes.len() as u64;
let mut program =
vlk_program::program::Program::new(Image::from_code(Isa::Mips32, 0x1000, bytes));
program.image.functions.insert(
0x1028,
vlk_loader::loader::Function {
addr: 0x1028,
size: None,
size_estimated: false,
name: None,
aliases: Vec::new(),
library: None,
sources: Vec::new(),
},
);
let blocks =
crate::lift::lift_shared(&program, 0x1000, size, &|_| vlk_loader::loader::Summary {
noreturn,
..Default::default()
})
.unwrap();
blocks
.iter()
.flat_map(|block| block.stmts.iter().cloned())
.collect::<Vec<Stmt>>()
};
let direct = |stmts: &[Stmt]| {
stmts.iter().any(|stmt| {
matches!(
stmt,
Stmt::Call {
target: Some(0x1028),
..
}
)
})
};
let returning = lift(&words, false);
assert!(direct(&returning), "{returning:?}");
assert!(
returning
.iter()
.any(|stmt| matches!(stmt, Stmt::Ret { .. }))
);
let ending = lift(&words, true);
assert!(direct(&ending), "{ending:?}");
let call = ending
.iter()
.position(|stmt| matches!(stmt, Stmt::Call { .. }))
.expect("call");
assert!(matches!(ending.get(call + 1), Some(Stmt::Trap { .. })));
let mut other = words;
other[1] = 0x3c19_0001;
assert!(!direct(&lift(&other, false)));
other[1] = NOP;
assert!(!direct(&lift(&other, false)));
}
#[test]
fn a_table_address_kept_in_a_frame_slot_is_read_where_the_jump_is() {
use vlk_arch::arch::mips32::SP;
use vlk_ir::ir::{BinOp, Block, Cond, Expr};
let mut image = Image::from_code(Isa::Mips32, 0x1000, vec![0; 0x40]);
image.sections.push(vlk_loader::loader::Section {
name: ".rodata".to_owned(),
vaddr: 0x2000,
size: 0x100,
file_offset: None,
file_size: 0,
executable: false,
loaded: true,
read_only: true,
});
let slot = |offset: u64| Expr::bin(BinOp::Add, Expr::reg(SP, 64), Expr::Const(offset));
let assign = |register: u16, value: Expr| Stmt::Assign {
dest: vlk_ir::ir::RegRef {
index: register,
width: 64,
},
value,
at: None,
};
let store = |offset: u64, value: Expr| Stmt::Store {
addr: slot(offset),
value,
width: 4,
at: None,
};
let load = |offset: u64| Expr::Load {
addr: Box::new(slot(offset)),
width: 4,
signed: false,
};
let function = |table: u64, second: Vec<Stmt>| {
let mut blocks = vec![
Block {
start: 0x1000,
stmts: vec![
assign(
SP,
Expr::bin(BinOp::Sub, Expr::reg(SP, 64), Expr::Const(0x40)),
),
assign(0, Expr::Const(table)),
store(0x20, Expr::reg(0, 32)),
Stmt::Brif {
cond: Cond::Eq,
lhs: Expr::reg(2, 32),
rhs: Expr::Const(0),
target: 0x1020,
at: None,
},
],
},
Block {
start: 0x1010,
stmts: second,
},
Block {
start: 0x1020,
stmts: vec![
assign(1, load(0x20)),
Stmt::BrIndirect {
target: Expr::Load {
addr: Box::new(Expr::bin(
BinOp::Add,
Expr::reg(1, 32),
Expr::reg(3, 32),
)),
width: 4,
signed: false,
},
at: None,
},
],
},
];
super::fold_table_slots(&image, &mut blocks, &HashSet::new(), &[]);
blocks[2].stmts[0].clone()
};
let folded = |stmt: &Stmt| {
matches!(
stmt,
Stmt::Assign {
value: Expr::Const(0x2000),
..
}
)
};
assert!(folded(&function(0x2000, vec![assign(1, Expr::Const(1))])));
assert!(!folded(&function(0x1008, vec![assign(1, Expr::Const(1))])));
assert!(!folded(&function(0x5000, vec![assign(1, Expr::Const(1))])));
assert!(!folded(&function(
0x2000,
vec![store(0x20, Expr::reg(4, 32))]
)));
assert!(!folded(&function(
0x2000,
vec![Stmt::Store {
addr: slot(0x22),
value: Expr::reg(4, 32),
width: 1,
at: None,
}]
)));
assert!(!folded(&function(
0x2000,
vec![assign(
SP,
Expr::bin(BinOp::Sub, Expr::reg(SP, 64), Expr::Const(8))
)]
)));
assert!(!folded(&function(
0x2000,
vec![assign(vlk_arch::arch::mips32::FP, Expr::reg(SP, 64))]
)));
}
#[test]
fn a_table_address_set_across_a_block_start_is_still_one_stored() {
use vlk_arch::arch::mips32::SP;
use vlk_ir::ir::{BinOp, Block, Cond, Expr};
let mut image = Image::from_code(Isa::Mips32, 0x1000, vec![0; 0x40]);
image.sections.push(vlk_loader::loader::Section {
name: ".rodata".to_owned(),
vaddr: 0x2000,
size: 0x100,
file_offset: None,
file_size: 0,
executable: false,
loaded: true,
read_only: true,
});
let slot = Expr::bin(BinOp::Add, Expr::reg(SP, 64), Expr::Const(0x20));
let assign = |register: u16, value: Expr| Stmt::Assign {
dest: vlk_ir::ir::RegRef {
index: register,
width: 64,
},
value,
at: None,
};
let read = Expr::Load {
addr: Box::new(slot.clone()),
width: 4,
signed: false,
};
let folded = |other: u64| {
let mut blocks = vec![
Block {
start: 0x1000,
stmts: vec![
assign(
SP,
Expr::bin(BinOp::Sub, Expr::reg(SP, 64), Expr::Const(0x40)),
),
assign(0, Expr::Const(0x2000)),
Stmt::Brif {
cond: Cond::Eq,
lhs: Expr::reg(2, 32),
rhs: Expr::Const(0),
target: 0x1020,
at: None,
},
],
},
Block {
start: 0x1010,
stmts: vec![assign(0, Expr::Const(other))],
},
Block {
start: 0x1020,
stmts: vec![
assign(
0,
Expr::bin(
BinOp::And,
Expr::bin(BinOp::Add, Expr::reg(0, 32), Expr::Const(0x10)),
Expr::Const(0xffff_ffff),
),
),
Stmt::Store {
addr: slot.clone(),
value: Expr::reg(0, 32),
width: 4,
at: None,
},
Stmt::BrIndirect {
target: Expr::bin(BinOp::Add, read.clone(), Expr::reg(3, 32)),
at: None,
},
],
},
];
super::fold_table_slots(&image, &mut blocks, &HashSet::new(), &[]);
matches!(&blocks[2].stmts[2], Stmt::BrIndirect { target: Expr::Bin { lhs, .. }, .. }
if **lhs == Expr::Const(0x2010))
};
assert!(folded(0x2000));
assert!(!folded(0x2040));
}
#[test]
fn another_jump_through_a_register_may_go_round_the_store() {
use vlk_arch::arch::mips32::SP;
use vlk_ir::ir::{BinOp, Block, Expr};
let mut image = Image::from_code(Isa::Mips32, 0x1000, vec![0; 0x40]);
image.sections.push(vlk_loader::loader::Section {
name: ".rodata".to_owned(),
vaddr: 0x2000,
size: 0x100,
file_offset: None,
file_size: 0,
executable: false,
loaded: true,
read_only: true,
});
let slot = Expr::bin(BinOp::Add, Expr::reg(SP, 64), Expr::Const(0x20));
let read = Expr::Load {
addr: Box::new(slot.clone()),
width: 4,
signed: false,
};
let through = |target: Expr| Stmt::BrIndirect { target, at: None };
let folded = |first: Stmt, tables: &[(u64, Vec<u64>)]| {
let mut blocks = vec![
Block {
start: 0x1000,
stmts: vec![
Stmt::Assign {
dest: vlk_ir::ir::RegRef {
index: SP,
width: 64,
},
value: Expr::bin(BinOp::Sub, Expr::reg(SP, 64), Expr::Const(0x40)),
at: None,
},
first,
],
},
Block {
start: 0x1010,
stmts: vec![Stmt::Store {
addr: slot.clone(),
value: Expr::Const(0x2000),
width: 4,
at: None,
}],
},
Block {
start: 0x1020,
stmts: vec![through(Expr::bin(
BinOp::Add,
read.clone(),
Expr::reg(3, 32),
))],
},
Block {
start: 0x1030,
stmts: vec![Stmt::Br {
target: Some(0x1020),
at: None,
}],
},
];
super::fold_table_slots(&image, &mut blocks, &HashSet::new(), tables);
!matches!(&blocks[2].stmts[0], Stmt::BrIndirect { target: Expr::Bin { lhs, .. }, .. } if **lhs == read)
};
assert!(folded(
Stmt::Br {
target: Some(0x1010),
at: None,
},
&[]
));
assert!(!folded(through(Expr::reg(3, 32)), &[]));
assert!(folded(through(Expr::reg(3, 32)), &[(0x1000, vec![0x1010])]));
}
#[test]
fn a_frame_closed_before_a_block_that_only_returns_is_closed_there() {
use vlk_arch::arch::mips32::SP;
use vlk_ir::ir::{BinOp, Block, Expr};
let mut image = Image::from_code(Isa::Mips32, 0x1000, vec![0; 0x40]);
image.sections.push(vlk_loader::loader::Section {
name: ".rodata".to_owned(),
vaddr: 0x2000,
size: 0x100,
file_offset: None,
file_size: 0,
executable: false,
loaded: true,
read_only: true,
});
let slot = Expr::bin(BinOp::Add, Expr::reg(SP, 64), Expr::Const(0x20));
let moved = |op: BinOp| Stmt::Assign {
dest: vlk_ir::ir::RegRef {
index: SP,
width: 64,
},
value: Expr::bin(op, Expr::reg(SP, 64), Expr::Const(0x40)),
at: None,
};
let read = Expr::Load {
addr: Box::new(slot.clone()),
width: 4,
signed: false,
};
let ret = Stmt::Ret {
value: Expr::reg(0, 32),
at: None,
};
let folded = |last: Vec<Stmt>| {
let mut blocks = vec![
Block {
start: 0x1000,
stmts: vec![
moved(BinOp::Sub),
Stmt::Store {
addr: slot.clone(),
value: Expr::Const(0x2000),
width: 4,
at: None,
},
Stmt::BrIndirect {
target: Expr::bin(BinOp::Add, read.clone(), Expr::reg(3, 32)),
at: None,
},
],
},
Block {
start: 0x1010,
stmts: vec![moved(BinOp::Add)],
},
Block {
start: 0x1020,
stmts: last,
},
];
super::fold_table_slots(&image, &mut blocks, &HashSet::new(), &[]);
!matches!(&blocks[0].stmts[2], Stmt::BrIndirect { target: Expr::Bin { lhs, .. }, .. } if **lhs == read)
};
assert!(folded(vec![ret.clone()]));
assert!(!folded(vec![
Stmt::Assign {
dest: vlk_ir::ir::RegRef {
index: 0,
width: 64
},
value: Expr::Const(1),
at: None,
},
ret,
]));
}
#[test]
fn a_store_on_one_path_only_does_not_give_the_slot_its_value() {
use vlk_arch::arch::mips32::SP;
use vlk_ir::ir::{BinOp, Block, Cond, Expr};
let mut image = Image::from_code(Isa::Mips32, 0x1000, vec![0; 0x40]);
image.sections.push(vlk_loader::loader::Section {
name: ".rodata".to_owned(),
vaddr: 0x2000,
size: 0x100,
file_offset: None,
file_size: 0,
executable: false,
loaded: true,
read_only: true,
});
let slot = Expr::bin(BinOp::Add, Expr::reg(SP, 64), Expr::Const(0x20));
let open = Stmt::Assign {
dest: vlk_ir::ir::RegRef {
index: SP,
width: 64,
},
value: Expr::bin(BinOp::Sub, Expr::reg(SP, 64), Expr::Const(0x40)),
at: None,
};
let read = Expr::Load {
addr: Box::new(slot.clone()),
width: 4,
signed: false,
};
let mut blocks = vec![
Block {
start: 0x1000,
stmts: vec![
open,
Stmt::Brif {
cond: Cond::Eq,
lhs: Expr::reg(2, 32),
rhs: Expr::Const(0),
target: 0x1020,
at: None,
},
],
},
Block {
start: 0x1010,
stmts: vec![Stmt::Store {
addr: slot,
value: Expr::Const(0x2000),
width: 4,
at: None,
}],
},
Block {
start: 0x1020,
stmts: vec![Stmt::BrIndirect {
target: Expr::bin(BinOp::Add, read.clone(), Expr::reg(3, 32)),
at: None,
}],
},
];
super::fold_table_slots(&image, &mut blocks, &HashSet::new(), &[]);
assert!(
matches!(&blocks[2].stmts[0], Stmt::BrIndirect { target: Expr::Bin { lhs, .. }, .. } if **lhs == read)
);
}
#[test]
fn code_that_sets_gp_after_a_call_or_a_jump_to_it_begins_a_function() {
let none = HashSet::new();
let after_call = code(&[
0x0c00_0408,
NOP,
0x3c1c_005a,
0x279c_4c90,
RETURN,
NOP,
NOP,
NOP,
RETURN,
NOP,
]);
assert_eq!(
function_starts(&after_call, 0x1000, false, &none),
[0x1008, 0x1020]
);
let reads_address = code(&[0x0411_0001, NOP, 0x3c1c_005a, 0x279c_4c90, RETURN, NOP]);
assert_eq!(
function_starts(&reads_address, 0x1000, false, &none),
[0u64; 0]
);
let mut goes_on = reads_address;
goes_on[0..4].copy_from_slice(&0x1000_0001u32.to_le_bytes());
assert_eq!(function_starts(&goes_on, 0x1000, false, &none), [0x1008]);
goes_on[8..12].copy_from_slice(&0x0080_1025u32.to_le_bytes());
goes_on[12..16].copy_from_slice(&NOP.to_le_bytes());
assert_eq!(function_starts(&goes_on, 0x1000, false, &none), [0u64; 0]);
let framed = code(&[
0x1000_0001,
NOP,
0x27bd_ffc8,
0x3c1c_005a,
0x279c_4c90,
RETURN,
NOP,
]);
assert_eq!(function_starts(&framed, 0x1000, false, &none), [0x1008]);
}
#[test]
fn a_jump_back_to_the_code_past_a_return_is_that_code_s_own() {
let bytes = code(&[RETURN, NOP, 0x0080_1025, 0x1000_fffe, NOP]);
assert_eq!(
function_starts(&bytes, 0x1000, false, &HashSet::new()),
[0x1008]
);
}
#[test]
fn code_the_function_before_it_jumps_into_is_a_part_of_that_function() {
let bytes = code(&[
0x1000_0003,
NOP,
0x9043_0000,
NOP,
0x14c4_fffd,
NOP,
RETURN,
NOP,
]);
assert_eq!(
function_starts(&bytes, 0x1000, false, &HashSet::new()),
[0u64; 0]
);
let mut apart = bytes;
apart[0..4].copy_from_slice(&RETURN.to_le_bytes());
assert_eq!(
function_starts(&apart, 0x1000, false, &HashSet::new()),
[0x1008]
);
}
#[test]
fn a_system_call_gives_its_error_flag_and_second_value() {
let ir = lifted(code(&[0x2402_0fa4, 0x0000_000c, RETURN, NOP]));
let outputs: Vec<u16> = ir.blocks[0]
.stmts
.iter()
.flat_map(Stmt::call_outputs)
.map(|output| output.register)
.collect();
assert_eq!(outputs, [0, 1, 5]);
}
#[test]
fn float_instructions_decode_to_their_usual_text() {
for (word, expected) in [
(0x4602_0100u32, "add.s $f4, $f0, $f2"),
(0x4622_0103, "div.d $f4, $f0, $f2"),
(0x4620_0104, "sqrt.d $f4, $f0"),
(0x4600_0106, "mov.s $f4, $f0"),
(0x4600_0121, "cvt.d.s $f4, $f0"),
(0x4620_0120, "cvt.s.d $f4, $f0"),
(0x4620_010d, "trunc.w.d $f4, $f0"),
(0x4600_0124, "cvt.w.s $f4, $f0"),
(0x4622_0036, "c.ole.d $f0, $f2"),
(0x4602_0035, "c.ult.s $f0, $f2"),
(0x4500_0002, "bc1f 0x100c"),
(0x0081_1001, "movt $v0, $a0, $fcc0"),
(0x00a0_1001, "movf $v0, $a1, $fcc0"),
(0x4484_0000, "mtc1 $a0, $f0"),
(0x4402_2000, "mfc1 $v0, $f4"),
(0x4c42_0121, "madd.d $f4, $f2, $f0, $f2"),
(0x4c00_1138, "nmsub.s $f4, $f0, $f2, $f0"),
] {
let instruction = decode::decode_word(word).unwrap();
assert_eq!(text::text(&instruction, 0x1000), expected, "{word:#x}");
}
assert_eq!(decode::decode_word(0x4600_0120), None);
assert_eq!(
decode::decode_word(0x4080_6000).unwrap().op,
decode::Op::Other
);
}
#[test]
fn a_conversion_rounds_as_the_control_register_was_just_told_to() {
let mut words = [
0x4442_f800,
0x3441_0003,
0x3821_0002,
0x44c1_f800,
0x4620_0024,
0x44c2_f800,
0x4402_0000,
RETURN,
NOP,
];
let shown = |words: &[u32]| format!("{:?}", lifted(code(words)).blocks);
let text = shown(&words);
assert!(text.contains("TowardZero"), "{text}");
assert!(!text.contains("Unsupported"), "{text}");
assert_eq!(text.matches("MipsFloatControl").count(), 3, "{text}");
words[2] = 0x3821_0001;
let text = shown(&words);
assert!(text.contains("rounding: Up"), "{text}");
words[3] = 0x44c4_f800;
let text = shown(&words);
assert!(text.contains("Unsupported"), "{text}");
words[3] = 0x44c1_e000;
let text = shown(&words);
assert!(text.contains("Unsupported"), "{text}");
words[3] = NOP;
words[5] = NOP;
let text = shown(&words);
assert!(text.contains("NearestEven"), "{text}");
assert!(!text.contains("Unsupported"), "{text}");
}
#[test]
fn a_jump_to_a_function_that_takes_a_format_counts_its_arguments() {
let mut words = [0u32; 18];
words[0] = 0x3c04_0000;
words[1] = 0x2484_1030;
words[2] = 0x0800_0410;
words[12] = u32::from_le_bytes(*b"none");
words[16] = RETURN;
let arguments = |words: &[u32]| {
let mut program =
vlk_program::program::Program::new(Image::from_code(Isa::Mips32, 0x1000, code(words)));
program.image.functions.insert(
0x1040,
vlk_loader::loader::Function {
addr: 0x1040,
size: None,
size_estimated: false,
name: None,
aliases: Vec::new(),
library: None,
sources: Vec::new(),
},
);
let blocks =
crate::lift::lift_shared(&program, 0x1000, 0x10, &|_| vlk_loader::loader::Summary {
arity: Some(4),
further_from: 1,
..Default::default()
})
.unwrap();
blocks
.iter()
.flat_map(|block| &block.stmts)
.find_map(|stmt| match stmt {
Stmt::Call {
kind: vlk_types::types::CallKind::Tail,
target: Some(0x1040),
args: Some(args),
..
} => Some(args.len()),
_ => None,
})
};
assert_eq!(arguments(&words), Some(1));
words[12] = u32::from_le_bytes(*b"%d%s");
assert_eq!(arguments(&words), Some(3));
words[1] = 0x2484_2000;
assert_eq!(arguments(&words), Some(4));
}
#[test]
fn a_single_fills_its_pair_unless_the_code_uses_the_other_half() {
let ir = lifted(code(&[0x4484_0000, 0x4402_0000, RETURN, NOP]));
assert!(ir.render().contains("f0 = a0"), "{}", ir.render());
let ir = lifted(code(&[0x4484_0000, 0x4485_0800, 0x4402_0000, RETURN, NOP]));
assert!(
ir.render()
.contains("f0 = ((f0 & #0xffffffff00000000) | a0)"),
"{}",
ir.render()
);
assert!(
ir.render()
.contains("f0 = ((f0 & #0xffffffff) | (a1 << #0x20))"),
"{}",
ir.render()
);
}
#[test]
fn a_float_branch_reads_its_flag_before_a_comparison_in_its_slot() {
let ir = lifted(code(&[
0x4602_0034,
0x4500_0002,
0x4600_1034,
RETURN,
NOP,
RETURN,
NOP,
]));
let first = &ir.blocks[0].stmts;
assert!(
matches!(&first[1], Stmt::Assign { dest, .. } if dest.index == HELD[0]),
"{}",
ir.render()
);
assert!(matches!(
first.last(),
Some(Stmt::Brif {
lhs: vlk_ir::ir::Expr::Reg { index, .. },
target: 0x1010,
..
}) if *index == HELD[0]
));
}
#[test]
fn position_independent_code_reads_its_own_address_from_t9() {
let ir = lifted(code(&[0x3c02_0002, 0x2442_0010, 0x0059_e021, RETURN, NOP]));
assert!(ir.render().contains("gp = #0x21010"), "{}", ir.render());
}
#[test]
fn a_function_starts_past_the_delay_slot_of_a_return() {
let bytes = code(&[
0x0411_0006,
NOP,
RETURN,
NOP,
0x2484_0001,
RETURN,
NOP,
RETURN,
NOP,
]);
assert_eq!(
function_starts(&bytes, 0x1000, false, &HashSet::new()),
[0x1010, 0x101c]
);
let bytes = code(&[0x1080_0003, NOP, RETURN, NOP, 0x2484_0001, RETURN, NOP]);
assert_eq!(
function_starts(&bytes, 0x1000, false, &HashSet::new()),
[0u64; 0]
);
let bytes = code(&[
0x14a0_0002,
0x0085_001b,
0x0007_000d,
0x0000_1012,
RETURN,
NOP,
]);
assert_eq!(
function_starts(&bytes, 0x1000, false, &HashSet::new()),
[0u64; 0]
);
let bytes = code(&[
0x0411_0006,
NOP,
0x0080_1025,
RETURN,
NOP,
0x1000_fffc,
NOP,
RETURN,
NOP,
]);
assert_eq!(
function_starts(&bytes, 0x1000, false, &HashSet::new()),
[0x101c]
);
}