1use rucc_base::Interner;
27use rucc_ir as ir;
28use rucc_mir as mir;
29use rucc_regalloc::assign::Env;
30use rucc_target::{Arch, BranchInsts, CallRegs, FrameInsts, PhysReg, RegFile, TargetInfo, x86_64};
31
32use crate::expand;
33use crate::finish::finish;
34use crate::frame::{Frame, Layout};
35use crate::layout;
36use crate::lower::{self, Unsupported};
37use crate::split;
38use crate::varargs;
39
40#[derive(Debug)]
49pub struct Machine {
50 pub conv: &'static CallRegs,
52 pub file: RegFile,
54 pub insts: &'static FrameInsts,
56 pub branch: &'static BranchInsts,
58 pub env: Env,
60}
61
62const SCRATCH: [PhysReg; 2] = [x86_64::R10, x86_64::R11];
69
70const SCRATCH_COUNT: usize = SCRATCH.len();
72
73impl Machine {
74 #[must_use]
81 pub fn x86_64(conv: &'static CallRegs) -> Self {
82 let order: Vec<PhysReg> =
83 conv.int_order.iter().copied().filter(|reg| !SCRATCH.contains(reg)).collect();
84 let free: Vec<PhysReg> =
91 conv.sse_order.iter().copied().filter(|®| !conv.preserves_sse(reg)).collect();
92 let at = free.len().saturating_sub(SCRATCH_COUNT);
93 let sse_scratch: Vec<PhysReg> = free[at..].to_vec();
94 let sse_order: Vec<PhysReg> =
95 conv.sse_order.iter().copied().filter(|reg| !sse_scratch.contains(reg)).collect();
96 Self {
97 conv,
98 file: x86_64::REGS,
99 insts: &x86_64::FRAME,
100 branch: &x86_64::BRANCH,
101 env: Env::new().with(x86_64::GPR, &order, &SCRATCH).with(
102 x86_64::XMM,
103 &sse_order,
104 &sse_scratch,
105 ),
106 }
107 }
108
109 #[must_use]
116 pub fn for_target(target: &TargetInfo) -> Option<Self> {
117 let conv = target.call_regs?;
118 match target.triple.arch {
119 Arch::X86_64 => Some(Self::x86_64(conv)),
120 Arch::Aarch64 | Arch::Riscv64 => None,
121 }
122 }
123}
124
125#[derive(Debug, Clone, Copy, PartialEq, Eq)]
127pub struct Flags {
128 pub frame_pointer: bool,
130 pub red_zone: bool,
132}
133
134impl Default for Flags {
135 fn default() -> Self {
138 Self { frame_pointer: false, red_zone: true }
139 }
140}
141
142pub fn compile(
156 source: &mut ir::Func,
157 names: &mut Interner,
158 machine: &Machine,
159 flags: Flags,
160) -> Result<mir::Func, Unsupported> {
161 expand::switches(source);
162 expand::floats(source);
163 expand::bulk(source, names, machine.conv.word);
164 varargs::lists(source, machine.conv);
165 let lower::Lowered { mut func, stack } = lower::func(source, names, machine.conv)?;
166 let layout = Layout {
167 frame_pointer: flags.frame_pointer,
168 red_zone: flags.red_zone,
169 ..stack.layout(Layout::new(machine.conv, machine.file))
170 };
171
172 split::critical(&mut func);
176 let allocation = rucc_regalloc::run(&mut func, &machine.env);
177
178 let frame = Frame::of(&func, &allocation, &layout);
181 finish(&mut func, &allocation, &frame, &stack, machine.conv, machine.insts, names);
182
183 layout::blocks(&mut func, machine.branch, names);
186 Ok(func)
187}
188
189#[cfg(test)]
190mod tests {
191 use rucc_ir::{Builder, Flags as IrFlags, Func, Opcode, Signature, Type};
192 use rucc_target::x86_64::{REGS, SYSV, WIN64};
193
194 use super::*;
195
196 fn blank(params: &[Type]) -> (Interner, Func, ir::Block, Vec<ir::Value>) {
198 let mut names = Interner::new();
199 let mut func = Func::new(names.intern("f"), Signature::new());
200 let block = func.create_block();
201 let values = params.iter().map(|&ty| func.append_param(block, ty)).collect();
202 (names, func, block, values)
203 }
204
205 #[test]
206 fn a_function_comes_out_with_no_virtual_register_left_in_it() {
207 let i32 = Type::int(32);
208 let (mut names, mut source, block, args) = blank(&[i32, i32]);
209 let mut build = Builder::new(&mut source, block);
210 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
211 build.ret(&[sum]);
212
213 let machine = Machine::x86_64(&SYSV);
214 let out = compile(&mut source, &mut names, &machine, Flags::default())
215 .expect("every instruction has a rule");
216
217 assert_eq!(
222 mir::print_func(&out, &names, ®S),
223 "mfunc @f {\n\
224 block0:\n \
225 $rdi($rdi) = x64.arg_val_32\n \
226 $rax = x64.mov_rr_64 $rdi\n \
227 $rsi($rsi) = x64.arg_val_32\n \
228 $rcx = x64.mov_rr_64 $rsi\n \
229 $rdx = x64.mov_rr_64 $rax\n \
230 $rdx(reuse 1) = x64.add_rr_32 $rax, $rcx\n \
231 $rax = x64.mov_rr_64 $rdx\n \
232 x64.ret_val_32 $rax($rax)\n \
233 x64.ret\n\
234 }\n"
235 );
236 }
237
238 #[test]
239 fn a_function_that_calls_takes_a_frame_and_gives_it_back() {
240 let i32 = Type::int(32);
241 let (mut names, mut source, block, args) = blank(&[i32]);
242 let sig = source.add_signature(Signature::new().with_params(&[i32]).with_returns(&[i32]));
243 let callee = names.intern("g");
244 let call = Builder::new(&mut source, block).call(callee, sig, &[args[0]]);
245 let got = source[call].first_result.expect("an integer comes back");
246 let mut build = Builder::new(&mut source, block);
247 let sum = build.binary(Opcode::Add, got, args[0], IrFlags::default());
248 build.ret(&[sum]);
249
250 let machine = Machine::x86_64(&SYSV);
251 let out = compile(&mut source, &mut names, &machine, Flags::default())
252 .expect("every instruction has a rule");
253
254 let text = mir::print_func(&out, &names, ®S);
257 assert!(text.contains("x64.push_64 $rbx"), "{text}");
258 assert!(text.contains("$rbx = x64.pop_64"), "{text}");
259 assert!(text.contains("x64.call $rdi($rdi), @g"), "{text}");
260 assert!(!text.contains('%'), "{text}");
261 }
262
263 #[test]
264 fn the_other_convention_is_the_same_function_somewhere_else() {
265 let i32 = Type::int(32);
266 let (mut names, mut source, block, args) = blank(&[i32, i32]);
267 let mut build = Builder::new(&mut source, block);
268 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
269 build.ret(&[sum]);
270
271 let machine = Machine::x86_64(&WIN64);
272 let out = compile(&mut source, &mut names, &machine, Flags::default())
273 .expect("every instruction has a rule");
274
275 let text = mir::print_func(&out, &names, ®S);
278 assert!(text.contains("$rcx($rcx) = x64.arg_val_32"), "{text}");
279 assert!(text.contains("$rdx($rdx) = x64.arg_val_32"), "{text}");
280 assert!(!text.contains("$rdi"), "{text}");
281 }
282
283 #[test]
284 fn a_function_with_a_branch_in_it_goes_through_every_pass() {
285 let i32 = Type::int(32);
286 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
287 let then = source.create_block();
288 let join = source.create_block();
289 let got = source.append_param(join, i32);
290 let mut build = Builder::new(&mut source, entry);
291 let cond = build.icmp(rucc_ir::IntPred::Slt, args[0], args[1]);
292 build.br_if(cond, then, &[], join, &[args[1]]);
293 Builder::new(&mut source, then).jump(join, &[args[0]]);
294 Builder::new(&mut source, join).ret(&[got]);
295
296 let machine = Machine::x86_64(&SYSV);
297 let out = compile(&mut source, &mut names, &machine, Flags::default())
298 .expect("every instruction has a rule");
299
300 assert_eq!(out.block_count(), 4);
304
305 let text = mir::print_func(&out, &names, ®S);
312 assert_eq!(
313 text,
314 "mfunc @f {\n\
315 block0:\n \
316 $rdi($rdi) = x64.arg_val_32\n \
317 $rax = x64.mov_rr_64 $rdi\n \
318 $rsi($rsi) = x64.arg_val_32\n \
319 $rcx = x64.mov_rr_64 $rsi\n \
320 $rdx = x64.cmp_set_l_32 $rax, $rcx\n \
321 x64.test_rr_8 $rdx\n \
322 x64.jcc_e block2, block1\n\
323 \nblock1:\n \
324 $rdx = x64.mov_rr_64 $rax\n \
325 x64.jmp block3\n\
326 \nblock2:\n \
327 $rdx = x64.mov_rr_64 $rcx, block3\n\
328 \nblock3:\n \
329 $rax = x64.mov_rr_64 $rdx\n \
330 x64.ret_val_32 $rax($rax)\n \
331 x64.ret\n\
332 }\n"
333 );
334 }
335
336 #[test]
342 fn a_loop_that_carries_its_values_round_keeps_all_of_them() {
343 let i32 = Type::int(32);
344 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
345 let head = source.create_block();
346 let body = source.create_block();
347 let exit = source.create_block();
348 let left = source.append_param(head, i32);
349 let right = source.append_param(head, i32);
350 Builder::new(&mut source, entry).jump(head, &[args[0], args[1]]);
351 let mut build = Builder::new(&mut source, head);
352 let zero = build.iconst(i32, 0);
353 let more = build.icmp(rucc_ir::IntPred::Ne, right, zero);
354 build.br_if(more, body, &[], exit, &[left]);
355 let mut build = Builder::new(&mut source, body);
356 let rest = build.binary(Opcode::SRem, left, right, IrFlags::default());
357 build.jump(head, &[right, rest]);
358 let result = source.append_param(exit, i32);
359 Builder::new(&mut source, exit).ret(&[result]);
360
361 let machine = Machine::x86_64(&SYSV);
362 let out = compile(&mut source, &mut names, &machine, Flags::default())
363 .expect("every instruction has a rule");
364
365 assert_eq!(
381 mir::print_func(&out, &names, ®S),
382 "mfunc @f {\n\
383 block0:\n \
384 $rdi($rdi) = x64.arg_val_32\n \
385 $rax = x64.mov_rr_64 $rdi\n \
386 $rsi($rsi) = x64.arg_val_32\n \
387 $rcx = x64.mov_rr_64 $rsi\n \
388 $rsi = x64.mov_rr_64 $rcx\n \
389 $rcx = x64.mov_rr_64 $rax, block1\n\
390 \nblock1:\n \
391 $rax = x64.mov_ri_32 0\n \
392 $rax = x64.cmp_set_ne_32 $rsi, $rax\n \
393 x64.test_rr_8 $rax\n \
394 x64.jcc_e block3, block2\n\
395 \nblock2:\n \
396 $rax = x64.mov_rr_64 $rcx\n \
397 $rdx($rdx), early $rax($rax) = x64.idiv_rem_32 $rax($rax), $rsi\n \
398 $rcx = x64.mov_rr_64 $rdx\n \
399 $rdi = x64.mov_rr_64 $rax\n \
400 $r10 = x64.mov_rr_64 $rsi\n \
401 $rsi = x64.mov_rr_64 $rcx\n \
402 $rcx = x64.mov_rr_64 $r10\n \
403 x64.jmp block1\n\
404 \nblock3:\n \
405 $rax = x64.mov_rr_64 $rcx\n \
406 x64.ret_val_32 $rax($rax)\n \
407 x64.ret\n\
408 }\n"
409 );
410 }
411
412 #[test]
417 fn a_function_that_has_been_laid_out_reads_back_as_the_same_function() {
418 let i32 = Type::int(32);
419 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
420 let then = source.create_block();
421 let join = source.create_block();
422 let got = source.append_param(join, i32);
423 let mut build = Builder::new(&mut source, entry);
424 let cond = build.icmp(rucc_ir::IntPred::Slt, args[0], args[1]);
425 build.br_if(cond, then, &[], join, &[args[1]]);
426 Builder::new(&mut source, then).jump(join, &[args[0]]);
427 Builder::new(&mut source, join).ret(&[got]);
428
429 let machine = Machine::x86_64(&SYSV);
430 let out = compile(&mut source, &mut names, &machine, Flags::default())
431 .expect("every instruction has a rule");
432
433 let text = mir::print_func(&out, &names, ®S);
434 let read = rucc_mir::parse(&text, &mut names, ®S).expect("what the printer wrote");
435 assert_eq!(mir::print(&read, &names, ®S), text);
436 }
437
438 #[test]
439 fn a_function_this_cannot_lower_is_reported_rather_than_compiled() {
440 let f80 = Type::float(rucc_ir::Float::F80);
441 let (mut names, mut source, block, args) = blank(&[f80]);
442 Builder::new(&mut source, block).ret(&[args[0]]);
443
444 let machine = Machine::x86_64(&SYSV);
445 let failed = compile(&mut source, &mut names, &machine, Flags::default())
446 .expect_err("a long double arrives on the x87 stack");
447 assert_eq!(failed.to_string(), "parameter 0 is on the x87 stack");
448 }
449
450 #[test]
454 fn a_float_is_added_in_the_register_file_it_arrives_in() {
455 let f32 = Type::float(rucc_ir::Float::F32);
456 let (mut names, mut source, block, args) = blank(&[f32, f32]);
457 let mut build = Builder::new(&mut source, block);
458 let sum = build.binary(Opcode::FAdd, args[0], args[1], ir::Flags::default());
459 build.ret(&[sum]);
460
461 let machine = Machine::x86_64(&SYSV);
462 let out = compile(&mut source, &mut names, &machine, Flags::default())
463 .expect("every instruction has a rule");
464
465 let text = mir::print_func(&out, &names, ®S);
466 assert!(text.contains("x64.addss_rr"), "{text}");
467 assert!(text.contains("$xmm0"), "{text}");
468 assert!(!text.contains("$rax"), "{text}");
469 }
470
471 #[test]
474 fn a_float_read_from_memory_and_written_back_uses_the_scalar_moves() {
475 let f64 = Type::float(rucc_ir::Float::F64);
476 let (mut names, mut source, block, args) = blank(&[Type::PTR, f64]);
477 let mut build = Builder::new(&mut source, block);
478 let info =
479 rucc_ir::MemInfo { size: 8, align: 8, order: rucc_ir::MemOrder::NotAtomic, tbaa: None };
480 let read = build.load(f64, args[0], info, ir::Flags::default());
481 let sum = build.binary(Opcode::FAdd, read, args[1], ir::Flags::default());
482 build.store(sum, args[0], info, ir::Flags::default());
483 build.ret(&[sum]);
484
485 let machine = Machine::x86_64(&SYSV);
486 let out = compile(&mut source, &mut names, &machine, Flags::default())
487 .expect("every instruction has a rule");
488
489 let text = mir::print_func(&out, &names, ®S);
490 assert!(text.contains("x64.movsd_rm"), "{text}");
491 assert!(text.contains("x64.movsd_mr"), "{text}");
492 assert!(!text.contains("x64.movaps_rm"), "{text}");
495 assert!(!text.contains("x64.movaps_mr"), "{text}");
496 }
497
498 #[test]
503 fn a_conversion_carries_the_value_into_the_other_register_file() {
504 let f64 = Type::float(rucc_ir::Float::F64);
505 let (mut names, mut source, block, args) = blank(&[f64]);
506 let mut build = Builder::new(&mut source, block);
507 let whole = build.unary(Opcode::FPToSI, args[0], Type::int(32));
508 let back = build.unary(Opcode::SIToFP, whole, f64);
509 build.ret(&[back]);
510
511 let machine = Machine::x86_64(&SYSV);
512 let out = compile(&mut source, &mut names, &machine, Flags::default())
513 .expect("every instruction has a rule");
514
515 let text = mir::print_func(&out, &names, ®S);
518 assert!(text.contains("x64.cvttsd2si_32"), "{text}");
519 assert!(text.contains("x64.cvtsi2sd_32"), "{text}");
520 assert!(text.contains("$xmm0"), "{text}");
521 }
522
523 #[test]
526 fn a_bitcast_between_the_files_is_the_move_that_changes_no_bit() {
527 let f64 = Type::float(rucc_ir::Float::F64);
528 let (mut names, mut source, block, args) = blank(&[f64]);
529 let mut build = Builder::new(&mut source, block);
530 let bits = build.unary(Opcode::Bitcast, args[0], Type::int(64));
531 build.ret(&[bits]);
532
533 let machine = Machine::x86_64(&SYSV);
534 let out = compile(&mut source, &mut names, &machine, Flags::default())
535 .expect("every instruction has a rule");
536
537 let text = mir::print_func(&out, &names, ®S);
538 assert!(text.contains("x64.movq_from_xmm"), "{text}");
539 assert!(!text.contains("cvt"), "{text}");
540 }
541
542 #[test]
544 fn a_float_comparison_is_the_compare_and_the_byte_a_condition_sets() {
545 let f64 = Type::float(rucc_ir::Float::F64);
546 let (mut names, mut source, block, args) = blank(&[f64, f64]);
547 let mut build = Builder::new(&mut source, block);
548 let less = build.fcmp(rucc_ir::FloatPred::Olt, args[0], args[1], ir::Flags::default());
549 let wide = build.unary(Opcode::ZExt, less, Type::int(32));
550 build.ret(&[wide]);
551
552 let machine = Machine::x86_64(&SYSV);
553 let out = compile(&mut source, &mut names, &machine, Flags::default())
554 .expect("every instruction has a rule");
555
556 let text = mir::print_func(&out, &names, ®S);
559 assert!(text.contains("x64.ucomisd_set_a"), "{text}");
560 }
561
562 #[test]
567 fn an_equality_between_floats_gets_a_register_for_the_byte_it_needs_twice() {
568 let f64 = Type::float(rucc_ir::Float::F64);
569 let (mut names, mut source, block, args) = blank(&[f64, f64]);
570 let mut build = Builder::new(&mut source, block);
571 let same = build.fcmp(rucc_ir::FloatPred::Oeq, args[0], args[1], ir::Flags::default());
572 let wide = build.unary(Opcode::ZExt, same, Type::int(32));
573 build.ret(&[wide]);
574
575 let machine = Machine::x86_64(&SYSV);
576 let out = compile(&mut source, &mut names, &machine, Flags::default())
577 .expect("every instruction has a rule");
578
579 let text = mir::print_func(&out, &names, ®S);
580 let line = text
581 .lines()
582 .find(|line| line.contains("x64.ucomisd_set_e_and_np"))
583 .expect("the rule for an ordered equality fired");
584 let written: Vec<&str> = line
585 .split_once('=')
586 .expect("the instruction writes something")
587 .0
588 .split(',')
589 .map(str::trim)
590 .collect();
591 assert_eq!(written.len(), 2, "{line}");
592 assert_ne!(written[0], written[1], "{line}");
593 }
594
595 #[test]
599 fn a_float_constant_is_the_bits_in_a_register_and_the_move_that_carries_them_over() {
600 let f64 = Type::float(rucc_ir::Float::F64);
601 let (mut names, mut source, block, _) = blank(&[]);
602 let mut build = Builder::new(&mut source, block);
603 let half = build.fconst(f64, 0x3fe0_0000_0000_0000);
604 build.ret(&[half]);
605
606 let machine = Machine::x86_64(&SYSV);
607 let out = compile(&mut source, &mut names, &machine, Flags::default())
608 .expect("every instruction has a rule");
609
610 let text = mir::print_func(&out, &names, ®S);
611 assert!(text.contains("x64.mov_ri_64"), "{text}");
612 assert!(text.contains("x64.movq_to_xmm"), "{text}");
613 }
614
615 #[test]
618 fn a_negation_is_the_sign_bit_flipped_and_no_float_instruction_at_all() {
619 let f64 = Type::float(rucc_ir::Float::F64);
620 let (mut names, mut source, block, args) = blank(&[f64]);
621 let mut build = Builder::new(&mut source, block);
622 let less = build.unary(Opcode::FNeg, args[0], f64);
623 build.ret(&[less]);
624
625 let machine = Machine::x86_64(&SYSV);
626 let out = compile(&mut source, &mut names, &machine, Flags::default())
627 .expect("every instruction has a rule");
628
629 let text = mir::print_func(&out, &names, ®S);
630 assert!(text.contains("x64.xor_rr_64"), "{text}");
631 assert!(!text.contains("sub"), "a negation is not a subtraction: {text}");
632 }
633
634 #[test]
635 fn the_flags_reach_the_frame() {
636 let i32 = Type::int(32);
637 let (mut names, mut source, block, args) = blank(&[i32]);
638 Builder::new(&mut source, block).ret(&[args[0]]);
639
640 let machine = Machine::x86_64(&SYSV);
641 let flags = Flags { frame_pointer: true, red_zone: true };
642 let out = compile(&mut source, &mut names, &machine, flags)
643 .expect("every instruction has a rule");
644
645 let text = mir::print_func(&out, &names, ®S);
648 assert!(text.contains("x64.push_64 $rbp"), "{text}");
649 assert!(text.contains("$rbp = x64.mov_rr_64 $rsp"), "{text}");
650 }
651
652 #[test]
653 fn a_target_says_which_machine_it_is_and_which_convention_it_uses() {
654 let triple = |text: &str| text.parse::<rucc_target::Triple>().expect("a triple");
655 let info = TargetInfo::new(triple("x86_64-unknown-linux-gnu"));
656 let machine = Machine::for_target(&info).expect("x86-64 is the target this crate covers");
657 assert!(std::ptr::eq(machine.conv, &SYSV));
658
659 let info = TargetInfo::new(triple("x86_64-pc-windows-msvc"));
660 let machine = Machine::for_target(&info).expect("x86-64 is the target this crate covers");
661 assert!(std::ptr::eq(machine.conv, &WIN64));
662
663 let info = TargetInfo::new(triple("aarch64-unknown-linux-gnu"));
666 assert!(Machine::for_target(&info).is_none());
667 }
668}