use rucc_base::{Interner, Symbol};
use rucc_ir::Type;
use rucc_mir as mir;
use rucc_target::{CallRegs, Constraint, PhysReg, Places, RegClass, Where};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Missing {
OnX87,
InBothFiles,
Width,
}
impl Missing {
#[must_use]
pub fn why(self) -> &'static str {
match self {
Missing::OnX87 => "is on the x87 stack",
Missing::InBothFiles => "is a float passed to a variadic callee on this convention",
Missing::Width => "is a width no argument register holds",
}
}
}
fn class_of(ty: Type, conv: &CallRegs) -> RegClass {
if ty.is_float() { conv.sse_class } else { conv.int_class }
}
fn refuses(ty: Type) -> Option<Missing> {
if head_of(ty).is_some() {
return None;
}
if ty.is_float() && ty.bits() == 80 {
return Some(Missing::OnX87);
}
Some(Missing::Width)
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Arrived {
pub regs: Vec<mir::Reg>,
pub stack: Vec<(mir::Inst, u32)>,
}
pub fn entry(
out: &mut mir::Func,
block: mir::Block,
params: &[Type],
conv: &CallRegs,
names: &mut Interner,
) -> Result<Arrived, (usize, Missing)> {
let mut places = Places::new(conv);
let mut arrived = Arrived { regs: Vec::with_capacity(params.len()), stack: Vec::new() };
for (index, &ty) in params.iter().enumerate() {
let at = if ty.is_float() { places.float() } else { places.integer() };
if let Some(missing) = refuses(ty) {
return Err((index, missing));
}
let class = class_of(ty, conv);
let reg = out.new_vreg(class);
match at {
Where::Reg(arrived_in) => {
let head = head_of(ty).ok_or((index, Missing::Width))?;
let opcode = mir::Opcode::new(names.intern(head));
let operand = mir::Operand::write(reg, class).with(Constraint::Fixed(arrived_in));
out.build(block, opcode).operand(operand).finish();
}
Where::Stack(up) => {
let load = load_of(ty).ok_or((index, Missing::Width))?;
let opcode = mir::Opcode::new(names.intern(load));
let sp = mir::Operand::read(mir::Reg::physical(conv.stack_pointer), conv.int_class);
let made = out.build(block, opcode).def(reg, class).mem(mir::Mem::at(sp)).finish();
arrived.stack.push((made, up));
}
}
arrived.regs.push(reg);
}
Ok(arrived)
}
pub const CALL: &str = "x64.call";
pub const CALL_REG: &str = "x64.call_reg";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Made {
pub result: Option<mir::Reg>,
pub outgoing: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Refused {
pub argument: Option<usize>,
pub missing: Missing,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Callee {
Named(Symbol),
Through(mir::Reg),
}
#[derive(Debug, Clone, Copy)]
pub struct Calling<'a> {
pub callee: Callee,
pub args: &'a [(Type, mir::Reg)],
pub returns: Option<Type>,
pub variadic: bool,
}
pub fn call(
out: &mut mir::Func,
block: mir::Block,
made: &Calling<'_>,
conv: &CallRegs,
names: &mut Interner,
) -> Result<Made, Refused> {
let &Calling { callee, args, returns, variadic } = made;
let mut places = Places::new(conv);
let mut passed = Vec::with_capacity(args.len());
let mut on_stack = Vec::new();
let mut vectors = 0u32;
for (index, &(ty, reg)) in args.iter().enumerate() {
let refused = |missing| Refused { argument: Some(index), missing };
let at = if ty.is_float() { places.float() } else { places.integer() };
if let Some(missing) = refuses(ty) {
return Err(refused(missing));
}
if ty.is_float() && variadic && conv.shared_positions {
return Err(refused(Missing::InBothFiles));
}
let class = class_of(ty, conv);
match at {
Where::Reg(at) => {
if class == conv.sse_class {
vectors += 1;
}
passed.push((reg, at, class));
}
Where::Stack(up) => {
let store = store_of(ty).ok_or(refused(Missing::Width))?;
on_stack.push((reg, class, names.intern(store), up));
}
}
}
let comes_back = match returns {
None => None,
Some(ty) if refuses(ty).is_some() => {
return Err(Refused { argument: None, missing: refuses(ty).unwrap_or(Missing::Width) });
}
Some(ty) => {
let class = class_of(ty, conv);
let file = if class == conv.sse_class { conv.sse_returns } else { conv.int_returns };
let at = *file.first().ok_or(Refused { argument: None, missing: Missing::Width })?;
Some((at, class))
}
};
let counted = if variadic { conv.vector_count } else { None };
for (reg, class, store, up) in on_stack {
let sp = mir::Operand::read(mir::Reg::physical(conv.stack_pointer), conv.int_class);
let up = i32::try_from(up).expect("an argument area under two gigabytes");
let build = out.build(block, mir::Opcode::new(store));
build.uses(reg, class).mem(mir::Mem::at(sp).plus(up)).finish();
}
let mut operands = Vec::with_capacity(args.len() + conv.int_order.len() + 2);
let result = comes_back.map(|(at, class)| {
let reg = out.new_vreg(class);
operands.push(mir::Operand::write(reg, class).with(Constraint::Fixed(at)));
reg
});
let spoken_for = |class: RegClass| -> Vec<PhysReg> {
comes_back
.filter(|&(_, at)| at == class)
.map(|(reg, _)| reg)
.into_iter()
.chain(counted.filter(|_| class == conv.int_class))
.chain(passed.iter().filter(|&&(_, _, at)| at == class).map(|&(_, reg, _)| reg))
.collect()
};
let named = spoken_for(conv.int_class);
for ® in conv.int_order {
if !conv.preserves_int(reg) && !named.contains(®) {
operands.push(mir::Operand::write(mir::Reg::physical(reg), conv.int_class));
}
}
let named = spoken_for(conv.sse_class);
for ® in conv.sse_order {
if !conv.preserves_sse(reg) && !named.contains(®) {
operands.push(mir::Operand::write(mir::Reg::physical(reg), conv.sse_class));
}
}
if let Callee::Through(reg) = callee {
operands.push(mir::Operand::read(reg, conv.int_class));
}
for (reg, at, class) in passed {
operands.push(mir::Operand::read(reg, class).with(Constraint::Fixed(at)));
}
if let Some(at) = counted {
let count = out.new_vreg(conv.int_class);
let zero = mir::Opcode::new(names.intern("x64.mov_ri_32"));
out.build(block, zero).def(count, conv.int_class).imm(i64::from(vectors)).finish();
operands.push(mir::Operand::read(count, conv.int_class).with(Constraint::Fixed(at)));
}
let opcode = mir::Opcode::new(names.intern(match callee {
Callee::Named(_) => CALL,
Callee::Through(_) => CALL_REG,
}));
let mut build = out.build(block, opcode);
if let Callee::Named(symbol) = callee {
build = build.symbol(symbol);
}
for operand in operands {
build = build.operand(operand);
}
build.finish();
Ok(Made { result, outgoing: places.size() })
}
#[must_use]
pub fn head_of(ty: Type) -> Option<&'static str> {
if let Some(at) = crate::term::float_slot(ty) {
return Some(["x64.arg_val_f32", "x64.arg_val_f64"][at]);
}
let names = ["x64.arg_val_8", "x64.arg_val_16", "x64.arg_val_32", "x64.arg_val_64"];
Some(names[crate::term::slot(ty)?])
}
#[must_use]
pub fn load_of(ty: Type) -> Option<&'static str> {
if let Some(at) = crate::term::float_slot(ty) {
return Some(["x64.movss_rm", "x64.movsd_rm"][at]);
}
let names = ["x64.mov_rm_8", "x64.mov_rm_16", "x64.mov_rm_32", "x64.mov_rm_64"];
Some(names[crate::term::slot(ty)?])
}
#[must_use]
pub fn store_of(ty: Type) -> Option<&'static str> {
if let Some(at) = crate::term::float_slot(ty) {
return Some(["x64.movss_mr", "x64.movsd_mr"][at]);
}
let names = ["x64.mov_mr_8", "x64.mov_mr_16", "x64.mov_mr_32", "x64.mov_mr_64"];
Some(names[crate::term::slot(ty)?])
}
#[cfg(test)]
mod tests {
use rucc_target::x86_64::{REGS, SYSV, WIN64};
use super::*;
fn bind(params: &[Type], conv: &CallRegs) -> String {
let mut names = Interner::new();
let mut out = mir::Func::new(names.intern("f"));
let block = out.create_block();
entry(&mut out, block, params, conv, &mut names).expect("every parameter arrives");
mir::print_func(&out, &names, ®S)
}
#[test]
fn the_first_arguments_arrive_where_the_convention_puts_them() {
let i32 = Type::int(32);
assert_eq!(
bind(&[i32, i32, Type::int(64)], &SYSV),
"mfunc @f {\nblock0:\n %0:gpr($rdi) = x64.arg_val_32\n \
%1:gpr($rsi) = x64.arg_val_32\n %2:gpr($rdx) = x64.arg_val_64\n}\n"
);
}
#[test]
fn the_other_convention_puts_the_same_arguments_somewhere_else() {
let i64 = Type::int(64);
assert_eq!(
bind(&[i64, i64], &WIN64),
"mfunc @f {\nblock0:\n %0:gpr($rcx) = x64.arg_val_64\n \
%1:gpr($rdx) = x64.arg_val_64\n}\n"
);
}
fn arrive(params: &[Type], conv: &CallRegs) -> (String, Vec<u32>) {
let mut names = Interner::new();
let mut out = mir::Func::new(names.intern("f"));
let block = out.create_block();
let arrived = entry(&mut out, block, params, conv, &mut names).expect("every parameter");
let up = arrived.stack.iter().map(|&(_, up)| up).collect();
(mir::print_func(&out, &names, ®S), up)
}
#[test]
fn an_argument_past_the_last_register_is_read_out_of_the_caller_s_stack() {
let (text, up) = arrive(&[Type::int(64); 7], &SYSV);
assert_eq!(up, [0]);
assert!(text.contains("%6:gpr = x64.mov_rm_64 [$rsp]"), "{text}");
assert_eq!(text.matches("x64.arg_val_64").count(), 6, "{text}");
}
#[test]
fn the_other_convention_runs_out_of_registers_three_arguments_earlier() {
let (text, up) = arrive(&[Type::int(64); 7], &WIN64);
assert_eq!(up, [32, 40, 48]);
assert_eq!(text.matches("x64.arg_val_64").count(), 4, "{text}");
assert!(text.contains("%4:gpr = x64.mov_rm_64 [$rsp]"), "{text}");
}
#[test]
fn what_a_stack_argument_is_read_with_is_its_own_width_and_its_own_file() {
let f32 = Type::float(rucc_ir::Float::F32);
let params = [Type::int(64), Type::int(64), Type::int(64), Type::int(64), Type::int(8)];
let (text, up) = arrive(¶ms, &WIN64);
assert_eq!(up, [32]);
assert!(text.contains("x64.mov_rm_8 [$rsp]"), "{text}");
let floats = [f32; 5];
let (text, up) = arrive(&floats, &WIN64);
assert_eq!(up, [32]);
assert!(text.contains("%4:xmm = x64.movss_rm [$rsp]"), "{text}");
}
#[test]
fn the_two_lists_of_widths_answer_for_the_same_types() {
let types = [
Type::int(1),
Type::int(8),
Type::int(16),
Type::int(32),
Type::int(64),
Type::int(128),
Type::PTR,
Type::float(rucc_ir::Float::F32),
Type::float(rucc_ir::Float::F64),
Type::float(rucc_ir::Float::F80),
];
for ty in types {
assert_eq!(head_of(ty).is_some(), load_of(ty).is_some(), "{ty:?}");
}
}
#[test]
fn a_float_arrives_in_a_vector_register_and_is_counted_apart_from_the_integers() {
let f32 = Type::float(rucc_ir::Float::F32);
let f64 = Type::float(rucc_ir::Float::F64);
assert_eq!(
bind(&[Type::int(32), f64, f32], &SYSV),
"mfunc @f {\nblock0:\n %0:gpr($rdi) = x64.arg_val_32\n \
%1:xmm($xmm0) = x64.arg_val_f64\n %2:xmm($xmm1) = x64.arg_val_f32\n}\n"
);
}
#[test]
fn the_other_convention_counts_the_two_files_as_one_run_of_positions() {
let f64 = Type::float(rucc_ir::Float::F64);
assert_eq!(
bind(&[Type::int(32), f64, Type::int(64)], &WIN64),
"mfunc @f {\nblock0:\n %0:gpr($rcx) = x64.arg_val_32\n \
%1:xmm($xmm1) = x64.arg_val_f64\n %2:gpr($r8) = x64.arg_val_64\n}\n"
);
}
#[test]
fn a_long_double_is_reported_as_the_x87_stack_it_travels_on() {
let mut names = Interner::new();
let mut out = mir::Func::new(names.intern("f"));
let block = out.create_block();
let params = [Type::int(32), Type::float(rucc_ir::Float::F80)];
assert_eq!(entry(&mut out, block, ¶ms, &SYSV, &mut names), Err((1, Missing::OnX87)));
assert_eq!(
make(&[], Some(Type::float(rucc_ir::Float::F80)), false, &SYSV).2,
Err(Refused { argument: None, missing: Missing::OnX87 })
);
}
fn make(
args: &[Type],
returns: Option<Type>,
variadic: bool,
conv: &CallRegs,
) -> (Interner, mir::Func, Result<Made, Refused>) {
let mut names = Interner::new();
let mut out = mir::Func::new(names.intern("f"));
let block = out.create_block();
let passed: Vec<(Type, mir::Reg)> =
args.iter().map(|&ty| (ty, out.append_param(block, class_of(ty, conv)))).collect();
let callee = Callee::Named(names.intern("g"));
let what = Calling { callee, args: &passed, returns, variadic };
let made = call(&mut out, block, &what, conv, &mut names);
(names, out, made)
}
fn operands(func: &mir::Func) -> (Vec<String>, Vec<String>) {
let block = func.entry().expect("a function with a block in it");
let call = func.terminator(block).expect("the call is the last thing in the block");
let name = |operand: &mir::Operand| match (operand.reg.phys(), operand.constraint) {
(Some(reg), _) | (None, Constraint::Fixed(reg)) => {
REGS.name(operand.class, reg).expect("a register the file describes").to_string()
}
_ => format!("{:?}", operand.reg),
};
let mut written = Vec::new();
let mut read = Vec::new();
for operand in &func[func[call].operands] {
let into = if operand.role == mir::Role::Use { &mut read } else { &mut written };
into.push(name(operand));
}
(written, read)
}
#[test]
fn a_call_passes_its_arguments_where_the_convention_puts_them() {
let i32 = Type::int(32);
let (_, func, made) = make(&[i32, i32, i32], None, false, &SYSV);
assert_eq!(made.expect("three integers all fit in registers").result, None);
assert_eq!(operands(&func).1, ["rdi", "rsi", "rdx"]);
}
#[test]
fn the_other_convention_passes_the_same_arguments_somewhere_else() {
let i64 = Type::int(64);
let (_, func, made) = make(&[i64, i64], None, false, &WIN64);
assert_eq!(made.expect("two integers fit in registers").outgoing, 32);
assert_eq!(operands(&func).1, ["rcx", "rdx"]);
}
#[test]
fn what_a_call_gives_back_comes_out_of_the_register_the_convention_returns_in() {
let (names, func, made) = make(&[], Some(Type::int(32)), false, &SYSV);
let result = made.expect("an integer comes back").result.expect("in a register");
assert_eq!(operands(&func).0.first().map(String::as_str), Some("rax"));
assert_eq!(func.class_of(result), Some(SYSV.int_class));
assert!(mir::print_func(&func, &names, ®S).contains("x64.call"));
}
#[test]
fn every_register_the_callee_may_destroy_is_written_by_the_call() {
let (_, func, _) = make(&[Type::int(64)], Some(Type::int(64)), false, &SYSV);
let (written, read) = operands(&func);
for saved in ["rbx", "rbp", "r12", "r13", "r14", "r15"] {
assert!(!written.contains(&saved.to_string()), "{saved} survives a call");
}
for destroyed in ["rcx", "rdx", "rsi", "r8", "r9", "r10", "r11"] {
let count = written.iter().filter(|name| *name == destroyed).count();
assert_eq!(count, 1, "{destroyed} is destroyed by a call and is written {count} times");
}
assert_eq!(written.iter().filter(|name| *name == "rax").count(), 1);
assert_eq!(read, ["rdi"]);
assert!(written.contains(&"xmm0".to_string()));
}
#[test]
fn a_variadic_call_says_how_many_vector_registers_it_passed_arguments_in() {
let (names, func, made) = make(&[Type::int(64)], None, true, &SYSV);
made.expect("an integer argument to a variadic callee");
let (_, read) = operands(&func);
assert_eq!(read, ["rdi", "rax"]);
assert_eq!(
mir::print_func(&func, &names, ®S).lines().nth(2),
Some(" %1:gpr = x64.mov_ri_32 0")
);
let f64 = Type::float(rucc_ir::Float::F64);
let (names, func, made) = make(&[Type::int(64), f64, f64], None, true, &SYSV);
made.expect("one integer and two floats all fit in registers");
assert_eq!(operands(&func).1, ["rdi", "xmm0", "xmm1", "rax"]);
assert!(mir::print_func(&func, &names, ®S).contains("x64.mov_ri_32 2"));
}
#[test]
fn a_float_passed_to_a_variadic_callee_on_windows_is_reported() {
let f64 = Type::float(rucc_ir::Float::F64);
assert_eq!(
make(&[Type::int(32), f64], None, true, &WIN64).2,
Err(Refused { argument: Some(1), missing: Missing::InBothFiles })
);
assert!(make(&[Type::int(32), f64], None, false, &WIN64).2.is_ok());
}
#[test]
fn a_call_through_an_address_reads_it_in_front_of_the_arguments() {
let i32 = Type::int(32);
let mut names = Interner::new();
let mut out = mir::Func::new(names.intern("f"));
let block = out.create_block();
let address = out.append_param(block, SYSV.int_class);
let passed = vec![(i32, out.append_param(block, SYSV.int_class))];
let what = Calling {
callee: Callee::Through(address),
args: &passed,
returns: Some(i32),
variadic: false,
};
call(&mut out, block, &what, &SYSV, &mut names).expect("one integer fits in a register");
let text = mir::print_func(&out, &names, ®S);
assert!(text.contains("= x64.call_reg %0, %1($rdi)\n"), "{text}");
assert!(!text.contains("@g"), "a call through an address names nobody: {text}");
}
#[test]
fn a_call_with_no_register_left_writes_the_argument_into_the_outgoing_area() {
let i64 = Type::int(64);
let (names, func, made) = make(&[i64; 7], None, false, &SYSV);
let made = made.expect("the seventh goes to memory");
let text = mir::print_func(&func, &names, ®S);
assert!(text.contains("x64.mov_mr_64 %6, [$rsp]\n"), "{text}");
let store = text.find("x64.mov_mr_64").expect("the store");
assert!(store < text.find("x64.call").expect("the call"), "{text}");
assert_eq!(made.outgoing, 8);
}
#[test]
fn where_the_outgoing_area_starts_is_the_convention_s_answer() {
let i64 = Type::int(64);
let (names, func, made) = make(&[i64; 7], None, false, &WIN64);
assert_eq!(made.expect("the last three go to memory").outgoing, 56);
let text = mir::print_func(&func, &names, ®S);
assert!(text.contains("x64.mov_mr_64 %4, [$rsp + 32]\n"), "{text}");
assert!(text.contains("x64.mov_mr_64 %5, [$rsp + 40]\n"), "{text}");
assert!(text.contains("x64.mov_mr_64 %6, [$rsp + 48]\n"), "{text}");
}
#[test]
fn a_narrow_or_floating_argument_keeps_its_own_store() {
let i64 = Type::int(64);
let narrow = [i64, i64, i64, i64, i64, i64, Type::int(8)];
let (names, func, made) = make(&narrow, None, false, &SYSV);
made.expect("the seventh goes to memory");
let text = mir::print_func(&func, &names, ®S);
assert!(text.contains("x64.mov_mr_8 %6, [$rsp]\n"), "{text}");
let f32 = Type::float(rucc_ir::Float::F32);
let (names, func, made) = make(&[f32; 9], None, false, &SYSV);
made.expect("the ninth goes to memory");
let text = mir::print_func(&func, &names, ®S);
assert!(text.contains("x64.movss_mr %8, [$rsp]\n"), "{text}");
}
#[test]
fn an_argument_in_memory_is_not_counted_as_a_vector_register() {
let f64 = Type::float(rucc_ir::Float::F64);
let (names, func, made) = make(&[f64; 9], None, true, &SYSV);
made.expect("the ninth goes to memory");
let text = mir::print_func(&func, &names, ®S);
assert!(text.contains("x64.mov_ri_32 8\n"), "eight registers, not nine: {text}");
}
#[test]
fn what_can_be_read_can_be_written() {
let types = [
Type::int(1),
Type::int(8),
Type::int(16),
Type::int(32),
Type::int(64),
Type::int(128),
Type::PTR,
Type::float(rucc_ir::Float::F32),
Type::float(rucc_ir::Float::F64),
Type::float(rucc_ir::Float::F80),
];
for ty in types {
assert_eq!(load_of(ty).is_some(), store_of(ty).is_some(), "{ty:?}");
}
}
#[test]
fn a_call_passes_and_returns_a_float_in_a_vector_register() {
let f64 = Type::float(rucc_ir::Float::F64);
let (_, func, made) = make(&[Type::int(32), f64], Some(f64), false, &SYSV);
let result = made.expect("an integer and a float both fit in registers");
let (written, read) = operands(&func);
assert_eq!(read, ["rdi", "xmm0"]);
assert_eq!(written.first().map(String::as_str), Some("xmm0"));
assert_eq!(func.class_of(result.result.expect("a float comes back")), Some(SYSV.sse_class));
assert_eq!(written.iter().filter(|name| *name == "xmm0").count(), 1);
assert!(written.contains(&"rax".to_string()));
}
#[test]
fn a_call_at_a_width_no_register_holds_is_reported_on_either_side() {
let i128 = Type::int(128);
assert_eq!(
make(&[i128], None, false, &SYSV).2,
Err(Refused { argument: Some(0), missing: Missing::Width })
);
assert_eq!(
make(&[], Some(i128), false, &SYSV).2,
Err(Refused { argument: None, missing: Missing::Width })
);
}
#[test]
fn an_argument_wider_than_a_register_has_no_name() {
assert_eq!(head_of(Type::int(128)), None);
assert_eq!(head_of(Type::int(8)), Some("x64.arg_val_8"));
assert_eq!(head_of(Type::int(64)), Some("x64.arg_val_64"));
}
#[test]
fn an_address_arrives_in_a_register_like_the_integer_it_is() {
assert_eq!(head_of(Type::PTR), Some("x64.arg_val_64"));
assert_eq!(
bind(&[Type::PTR], &SYSV),
"mfunc @f {\nblock0:\n %0:gpr($rdi) = x64.arg_val_64\n}\n"
);
assert!(make(&[Type::PTR], Some(Type::PTR), false, &SYSV).2.is_ok());
}
}