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