#![allow(clippy::manual_range_contains)]
include!(concat!(env!("OUT_DIR"), "/x86-64.rs"));
#[cfg(test)]
mod tests {
use rucc_target::x86_64;
use super::TABLE;
use crate::select::Piece;
const PREFIX: &str = "x64.";
const AMODES: &[&str] =
&["amode_base_index_scale", "amode_index_scale", "amode_base", "amode_base_offset"];
fn heads() -> Vec<&'static str> {
let mut found: Vec<&'static str> = TABLE
.rules
.iter()
.flat_map(|rule| rule.replacement.iter())
.filter_map(|piece| match piece {
Piece::App { head, .. } => Some(*head),
_ => None,
})
.collect();
found.sort_unstable();
found.dedup();
found
}
#[test]
fn every_instruction_the_table_writes_is_described() {
for head in heads() {
if AMODES.contains(&head) {
continue;
}
let opcode = head.strip_prefix(PREFIX).unwrap_or_else(|| {
panic!("{head} is neither an x86-64 term nor an addressing mode")
});
assert!(
x86_64::form(opcode).is_some(),
"{head} is selected by a rule and `rucc_target::x86_64` does not say what it \
does with its operands"
);
}
}
#[test]
fn a_store_is_written_with_the_value_first_because_that_is_where_the_ir_keeps_it() {
let mut seen = 0;
for rule in TABLE.rules {
let Some(rest) = rule.pattern.strip_prefix("(store.") else { continue };
let (width, operands) = rest.split_once(' ').expect("a store takes operands");
assert!(
operands.starts_with(&format!("(value.{width} ")),
"line {}: {} binds something other than the value it is storing first",
rule.line,
rule.pattern
);
assert!(
operands.contains("(value.i64 "),
"line {}: {} reaches no address",
rule.line,
rule.pattern
);
seen += 1;
}
assert_eq!(seen, 8, "the store rules moved and this test did not follow them");
}
const CONVENTION: &[&str] = &["arg_val_8", "arg_val_16", "arg_val_32", "arg_val_64", "call"];
const LAYOUT: &[&str] = &["test_rr_8", "jcc_e", "jcc_ne", "jmp"];
const FRAME: &[&str] =
&["push_64", "pop_64", "ret", "mov_rr_64", "movaps_rr", "movaps_rm", "movaps_mr"];
#[test]
fn every_instruction_exempt_from_a_rule_is_one_a_frame_really_writes() {
let frame = &x86_64::FRAME;
let mut written = vec![frame.push, frame.pop, frame.ret];
for class in frame.classes {
written.extend([class.mov, class.load, class.store]);
}
written.retain(|opcode| !heads().contains(&format!("{PREFIX}{opcode}").as_str()));
assert_eq!(written, FRAME);
}
#[test]
fn every_instruction_exempt_from_a_rule_is_one_the_convention_really_writes() {
let strip = |head: &'static str| head.strip_prefix(PREFIX).expect("an x86-64 term");
let written: Vec<&str> = [8, 16, 32, 64]
.into_iter()
.map(|bits| {
strip(
crate::abi::head_of(rucc_ir::Type::int(bits))
.expect("every width the pseudos cover"),
)
})
.chain([strip(crate::abi::CALL)])
.collect();
assert_eq!(written, CONVENTION);
}
#[test]
fn every_described_instruction_is_reachable_from_a_rule() {
let written = heads();
for &(opcode, _) in x86_64::INSTS {
if CONVENTION.contains(&opcode) || LAYOUT.contains(&opcode) || FRAME.contains(&opcode) {
continue;
}
let head = format!("{PREFIX}{opcode}");
assert!(
written.contains(&head.as_str()),
"{opcode} is described and no rule in {} selects it",
TABLE.source
);
}
}
}