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::coverage::Fired;
33use crate::elsewhere::Elsewhere;
34use crate::expand;
35use crate::finish::finish;
36use crate::frame::{Frame, Layout};
37use crate::layout;
38use crate::lower::{self, Unsupported};
39use crate::split;
40use crate::switch;
41use crate::varargs;
42use crate::widths;
43
44#[derive(Debug)]
53pub struct Machine {
54 pub conv: &'static CallRegs,
56 pub file: RegFile,
58 pub insts: &'static FrameInsts,
60 pub branch: &'static BranchInsts,
62 pub env: Env,
64}
65
66const SCRATCH: [PhysReg; 2] = [x86_64::R10, x86_64::R11];
73
74const SCRATCH_COUNT: usize = SCRATCH.len();
76
77impl Machine {
78 #[must_use]
85 pub fn x86_64(conv: &'static CallRegs) -> Self {
86 let order: Vec<PhysReg> =
87 conv.int_order.iter().copied().filter(|reg| !SCRATCH.contains(reg)).collect();
88 let free: Vec<PhysReg> =
95 conv.sse_order.iter().copied().filter(|®| !conv.preserves_sse(reg)).collect();
96 let at = free.len().saturating_sub(SCRATCH_COUNT);
97 let sse_scratch: Vec<PhysReg> = free[at..].to_vec();
98 let sse_order: Vec<PhysReg> =
99 conv.sse_order.iter().copied().filter(|reg| !sse_scratch.contains(reg)).collect();
100 Self {
101 conv,
102 file: x86_64::REGS,
103 insts: &x86_64::FRAME,
104 branch: &x86_64::BRANCH,
105 env: Env::new().with(x86_64::GPR, &order, &SCRATCH).with(
106 x86_64::XMM,
107 &sse_order,
108 &sse_scratch,
109 ),
110 }
111 }
112
113 #[must_use]
120 pub fn for_target(target: &TargetInfo) -> Option<Self> {
121 let conv = target.call_regs?;
122 match target.triple.arch {
123 Arch::X86_64 => Some(Self::x86_64(conv)),
124 Arch::Aarch64 | Arch::Riscv64 => None,
125 }
126 }
127}
128
129#[derive(Debug, Clone, Copy, PartialEq, Eq)]
131pub struct Flags {
132 pub frame_pointer: bool,
134 pub red_zone: bool,
136}
137
138impl Default for Flags {
139 fn default() -> Self {
142 Self { frame_pointer: false, red_zone: true }
143 }
144}
145
146pub fn compile(
165 source: &mut ir::Func,
166 names: &mut Interner,
167 machine: &Machine,
168 elsewhere: &Elsewhere,
169 flags: Flags,
170) -> Result<mir::Func, Unsupported> {
171 compile_recording(source, names, machine, elsewhere, flags, &mut Fired::new())
172}
173
174pub fn compile_recording(
188 source: &mut ir::Func,
189 names: &mut Interner,
190 machine: &Machine,
191 elsewhere: &Elsewhere,
192 flags: Flags,
193 fired: &mut Fired,
194) -> Result<mir::Func, Unsupported> {
195 switch::switches(source);
196 expand::orderings(source, machine.conv.word);
199 widths::integers(source);
202 expand::bytes(source);
203 expand::counts(source);
204 expand::overflows(source);
205 expand::floats(source);
206 expand::bulk(source, names, machine.conv.word);
207 varargs::lists(source, machine.conv);
208 let lowered = lower::func(source, names, machine.conv, elsewhere)?;
209 fired.merge(&lowered.fired);
210 let lower::Lowered { mut func, stack, .. } = lowered;
211 let layout = Layout {
212 frame_pointer: flags.frame_pointer,
213 red_zone: flags.red_zone,
214 ..stack.layout(Layout::new(machine.conv, machine.file))
215 };
216
217 split::critical(&mut func);
221 let allocation = rucc_regalloc::run(&mut func, &machine.env);
222
223 let frame = Frame::of(&func, &allocation, &layout);
226 finish(&mut func, &allocation, &frame, &stack, machine.conv, machine.insts, names);
227
228 layout::blocks(&mut func, machine.branch, names);
231 Ok(func)
232}
233
234#[cfg(test)]
235mod tests {
236 use rucc_ir::{Builder, Flags as IrFlags, Func, Opcode, Restrict, Signature, Type};
237 use rucc_target::x86_64::{REGS, SYSV, WIN64};
238
239 use super::*;
240
241 fn blank(params: &[Type]) -> (Interner, Func, ir::Block, Vec<ir::Value>) {
243 let mut names = Interner::new();
244 let mut func = Func::new(names.intern("f"), Signature::new());
245 let block = func.create_block();
246 let values = params.iter().map(|&ty| func.append_param(block, ty)).collect();
247 (names, func, block, values)
248 }
249
250 #[test]
251 fn a_function_comes_out_with_no_virtual_register_left_in_it() {
252 let i32 = Type::int(32);
253 let (mut names, mut source, block, args) = blank(&[i32, i32]);
254 let mut build = Builder::new(&mut source, block);
255 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
256 build.ret(&[sum]);
257
258 let machine = Machine::x86_64(&SYSV);
259 let out =
260 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
261 .expect("every instruction has a rule");
262
263 assert_eq!(
268 mir::print_func(&out, &names, ®S),
269 "mfunc @f {\n\
270 block0:\n \
271 $rdi($rdi) = x64.arg_val_32\n \
272 $rsi($rsi) = x64.arg_val_32\n \
273 $rdi(reuse 1) = x64.add_rr_32 $rdi, $rsi\n \
274 $rax = x64.mov_rr_64 $rdi\n \
275 x64.ret_val_32 $rax($rax)\n \
276 x64.ret\n\
277 }\n"
278 );
279 }
280
281 #[test]
285 fn which_rules_lowered_a_function_is_something_the_compilation_can_be_asked_for() {
286 let i32 = Type::int(32);
287 let (mut names, mut source, block, args) = blank(&[i32, i32]);
288 let mut build = Builder::new(&mut source, block);
289 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
290 build.ret(&[sum]);
291
292 let machine = Machine::x86_64(&SYSV);
293 let mut fired = Fired::new();
294 compile_recording(
295 &mut source,
296 &mut names,
297 &machine,
298 &Elsewhere::default(),
299 Flags::default(),
300 &mut fired,
301 )
302 .expect("every instruction has a rule");
303 let one = fired.count();
304 assert!(one > 0, "an add and a return went through the table and nothing was recorded");
305
306 let listing = fired.listing(&crate::select::x86_64::TABLE);
307 assert_eq!(listing.lines().filter(|line| line.starts_with("fired ")).count(), one);
308 assert!(
309 listing.contains(&format!("{one} of ")),
310 "{}",
311 listing.lines().next().unwrap_or("")
312 );
313
314 let (mut names, mut source, block, args) = blank(&[i32, i32]);
316 let mut build = Builder::new(&mut source, block);
317 let difference = build.binary(Opcode::Sub, args[0], args[1], IrFlags::default());
318 build.ret(&[difference]);
319 compile_recording(
320 &mut source,
321 &mut names,
322 &machine,
323 &Elsewhere::default(),
324 Flags::default(),
325 &mut fired,
326 )
327 .expect("every instruction has a rule");
328 assert!(fired.count() > one, "a subtraction is not an addition");
329 }
330
331 #[test]
332 fn a_function_that_calls_takes_a_frame_and_gives_it_back() {
333 let i32 = Type::int(32);
334 let (mut names, mut source, block, args) = blank(&[i32]);
335 let sig = source.add_signature(Signature::new().with_params(&[i32]).with_returns(&[i32]));
336 let callee = names.intern("g");
337 let call = Builder::new(&mut source, block).call(callee, sig, &[args[0]]);
338 let got = source[call].first_result.expect("an integer comes back");
339 let mut build = Builder::new(&mut source, block);
340 let sum = build.binary(Opcode::Add, got, args[0], IrFlags::default());
341 build.ret(&[sum]);
342
343 let machine = Machine::x86_64(&SYSV);
344 let out =
345 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
346 .expect("every instruction has a rule");
347
348 let text = mir::print_func(&out, &names, ®S);
351 assert!(text.contains("x64.push_64 $rbx"), "{text}");
352 assert!(text.contains("$rbx = x64.pop_64"), "{text}");
353 assert!(text.contains("x64.call $rdi($rdi), @g"), "{text}");
354 assert!(!text.contains('%'), "{text}");
355 }
356
357 #[test]
358 fn the_other_convention_is_the_same_function_somewhere_else() {
359 let i32 = Type::int(32);
360 let (mut names, mut source, block, args) = blank(&[i32, i32]);
361 let mut build = Builder::new(&mut source, block);
362 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
363 build.ret(&[sum]);
364
365 let machine = Machine::x86_64(&WIN64);
366 let out =
367 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
368 .expect("every instruction has a rule");
369
370 let text = mir::print_func(&out, &names, ®S);
373 assert!(text.contains("$rcx($rcx) = x64.arg_val_32"), "{text}");
374 assert!(text.contains("$rdx($rdx) = x64.arg_val_32"), "{text}");
375 assert!(!text.contains("$rdi"), "{text}");
376 }
377
378 #[test]
379 fn a_function_with_a_branch_in_it_goes_through_every_pass() {
380 let i32 = Type::int(32);
381 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
382 let then = source.create_block();
383 let join = source.create_block();
384 let got = source.append_param(join, i32);
385 let mut build = Builder::new(&mut source, entry);
386 let cond = build.icmp(rucc_ir::IntPred::Slt, args[0], args[1]);
387 build.br_if(cond, then, &[], join, &[args[1]]);
388 Builder::new(&mut source, then).jump(join, &[args[0]]);
389 Builder::new(&mut source, join).ret(&[got]);
390
391 let machine = Machine::x86_64(&SYSV);
392 let out =
393 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
394 .expect("every instruction has a rule");
395
396 assert_eq!(out.block_count(), 4);
400
401 let text = mir::print_func(&out, &names, ®S);
410 assert_eq!(
411 text,
412 "mfunc @f {\n\
413 block0:\n \
414 $rdi($rdi) = x64.arg_val_32\n \
415 $rsi($rsi) = x64.arg_val_32\n \
416 $rax = x64.cmp_set_l_32 $rdi, $rsi\n \
417 x64.test_rr_8 $rax\n \
418 x64.jcc_e block2, block1\n\
419 \nblock1:\n \
420 $rax = x64.mov_rr_64 $rdi\n \
421 x64.jmp block3\n\
422 \nblock2:\n \
423 $rax = x64.mov_rr_64 $rsi, block3\n\
424 \nblock3:\n \
425 x64.ret_val_32 $rax($rax)\n \
426 x64.ret\n\
427 }\n"
428 );
429 }
430
431 #[test]
437 fn a_loop_that_carries_its_values_round_keeps_all_of_them() {
438 let i32 = Type::int(32);
439 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
440 let head = source.create_block();
441 let body = source.create_block();
442 let exit = source.create_block();
443 let left = source.append_param(head, i32);
444 let right = source.append_param(head, i32);
445 Builder::new(&mut source, entry).jump(head, &[args[0], args[1]]);
446 let mut build = Builder::new(&mut source, head);
447 let zero = build.iconst(i32, 0);
448 let more = build.icmp(rucc_ir::IntPred::Ne, right, zero);
449 build.br_if(more, body, &[], exit, &[left]);
450 let mut build = Builder::new(&mut source, body);
451 let rest = build.binary(Opcode::SRem, left, right, IrFlags::default());
452 build.jump(head, &[right, rest]);
453 let result = source.append_param(exit, i32);
454 Builder::new(&mut source, exit).ret(&[result]);
455
456 let machine = Machine::x86_64(&SYSV);
457 let out =
458 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
459 .expect("every instruction has a rule");
460
461 assert_eq!(
477 mir::print_func(&out, &names, ®S),
478 "mfunc @f {\n\
479 block0:\n \
480 $rdi($rdi) = x64.arg_val_32\n \
481 $rsi($rsi) = x64.arg_val_32\n \
482 $rcx = x64.mov_rr_64 $rdi, block1\n\
483 \nblock1:\n \
484 $rax = x64.mov_ri_32 0\n \
485 $rax = x64.cmp_set_ne_32 $rsi, $rax\n \
486 x64.test_rr_8 $rax\n \
487 x64.jcc_e block3, block2\n\
488 \nblock2:\n \
489 $rax = x64.mov_rr_64 $rcx\n \
490 $rdx($rdx), early $rax($rax) = x64.idiv_rem_32 $rax($rax), $rsi\n \
491 $rdi = x64.mov_rr_64 $rax\n \
492 $rcx = x64.mov_rr_64 $rsi\n \
493 $rsi = x64.mov_rr_64 $rdx\n \
494 x64.jmp block1\n\
495 \nblock3:\n \
496 $rax = x64.mov_rr_64 $rcx\n \
497 x64.ret_val_32 $rax($rax)\n \
498 x64.ret\n\
499 }\n"
500 );
501 }
502
503 #[test]
508 fn a_function_that_has_been_laid_out_reads_back_as_the_same_function() {
509 let i32 = Type::int(32);
510 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
511 let then = source.create_block();
512 let join = source.create_block();
513 let got = source.append_param(join, i32);
514 let mut build = Builder::new(&mut source, entry);
515 let cond = build.icmp(rucc_ir::IntPred::Slt, args[0], args[1]);
516 build.br_if(cond, then, &[], join, &[args[1]]);
517 Builder::new(&mut source, then).jump(join, &[args[0]]);
518 Builder::new(&mut source, join).ret(&[got]);
519
520 let machine = Machine::x86_64(&SYSV);
521 let out =
522 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
523 .expect("every instruction has a rule");
524
525 let text = mir::print_func(&out, &names, ®S);
526 let read = rucc_mir::parse(&text, &mut names, ®S).expect("what the printer wrote");
527 assert_eq!(mir::print(&read, &names, ®S), text);
528 }
529
530 #[test]
531 fn a_function_this_cannot_lower_is_reported_rather_than_compiled() {
532 let f80 = Type::float(rucc_ir::Float::F80);
533 let (mut names, mut source, block, args) = blank(&[f80, Type::int(64)]);
534 Builder::new(&mut source, block).ret(&args);
535
536 let machine = Machine::x86_64(&SYSV);
540 let failed =
541 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
542 .expect_err("a long double cannot come back beside another value");
543 assert_eq!(failed.to_string(), "what this function gives back is on the x87 stack");
544 }
545
546 #[test]
554 fn a_long_double_arrives_in_memory_and_goes_back_on_the_x87_stack() {
555 let f80 = Type::float(rucc_ir::Float::F80);
556 let (mut names, mut source, block, args) = blank(&[f80, f80]);
557 let mut build = Builder::new(&mut source, block);
558 let sum = build.binary(Opcode::FAdd, args[0], args[1], IrFlags::default());
559 build.ret(&[sum]);
560
561 let machine = Machine::x86_64(&SYSV);
562 let out =
563 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
564 .expect("every instruction has a rule");
565
566 let text = mir::print_func(&out, &names, ®S);
567 assert!(text.contains("x64.lea_64 [$rsp + 32]"), "{text}");
570 assert!(text.contains("x64.lea_64 [$rsp + 48]"), "{text}");
571 assert!(!text.contains("x64.ret_val"), "nothing comes back in a register: {text}");
572 let end: Vec<&str> = text.lines().rev().skip(1).take(3).map(str::trim).collect();
575 assert_eq!(end, ["x64.ret", "$rsp = x64.add_ri_64 $rsp, 24", "x64.fld_t [$rax]"], "{text}");
576 }
577
578 #[test]
582 fn a_float_is_added_in_the_register_file_it_arrives_in() {
583 let f32 = Type::float(rucc_ir::Float::F32);
584 let (mut names, mut source, block, args) = blank(&[f32, f32]);
585 let mut build = Builder::new(&mut source, block);
586 let sum = build.binary(Opcode::FAdd, args[0], args[1], ir::Flags::default());
587 build.ret(&[sum]);
588
589 let machine = Machine::x86_64(&SYSV);
590 let out =
591 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
592 .expect("every instruction has a rule");
593
594 let text = mir::print_func(&out, &names, ®S);
595 assert!(text.contains("x64.addss_rr"), "{text}");
596 assert!(text.contains("$xmm0"), "{text}");
597 assert!(!text.contains("$rax"), "{text}");
598 }
599
600 #[test]
603 fn a_float_read_from_memory_and_written_back_uses_the_scalar_moves() {
604 let f64 = Type::float(rucc_ir::Float::F64);
605 let (mut names, mut source, block, args) = blank(&[Type::PTR, f64]);
606 let mut build = Builder::new(&mut source, block);
607 let info = rucc_ir::MemInfo {
608 size: 8,
609 align: 8,
610 order: rucc_ir::MemOrder::NotAtomic,
611 tbaa: None,
612 restrict: Restrict::NONE,
613 };
614 let read = build.load(f64, args[0], info, ir::Flags::default());
615 let sum = build.binary(Opcode::FAdd, read, args[1], ir::Flags::default());
616 build.store(sum, args[0], info, ir::Flags::default());
617 build.ret(&[sum]);
618
619 let machine = Machine::x86_64(&SYSV);
620 let out =
621 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
622 .expect("every instruction has a rule");
623
624 let text = mir::print_func(&out, &names, ®S);
625 assert!(text.contains("x64.movsd_rm"), "{text}");
626 assert!(text.contains("x64.movsd_mr"), "{text}");
627 assert!(!text.contains("x64.movaps_rm"), "{text}");
630 assert!(!text.contains("x64.movaps_mr"), "{text}");
631 }
632
633 #[test]
641 fn an_unsigned_word_and_a_long_double_convert_into_each_other() {
642 let f80 = Type::float(rucc_ir::Float::F80);
643 let (mut names, mut source, block, args) = blank(&[Type::PTR, Type::int(64)]);
644 let mut build = Builder::new(&mut source, block);
645 let info = rucc_ir::MemInfo {
646 size: 16,
647 align: 16,
648 order: rucc_ir::MemOrder::NotAtomic,
649 tbaa: None,
650 restrict: Restrict::NONE,
651 };
652 let wide = build.unary(Opcode::UIToFP, args[1], f80);
653 build.store(wide, args[0], info, ir::Flags::default());
654 let read = build.load(f80, args[0], info, ir::Flags::default());
655 let back = build.unary(Opcode::FPToUI, read, Type::int(64));
656 build.ret(&[back]);
657
658 let machine = Machine::x86_64(&SYSV);
659 let out =
660 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
661 .expect("every instruction has a rule");
662
663 let text = mir::print_func(&out, &names, ®S);
664 assert!(text.contains("x64.fild_ll"), "the integer goes in as a signed one: {text}");
667 assert!(text.contains("x64.fistp_ll"), "and comes back out as one: {text}");
668 assert!(text.contains("x64.fmul_p"), "the correction is taken or not: {text}");
669 assert!(text.contains("x64.fadd_p"), "and applied one way: {text}");
670 assert!(text.contains("x64.fsub_p"), "and the other: {text}");
671 assert!(!text.contains("xmm"), "no part of this is in a vector register: {text}");
672 }
673
674 #[test]
679 fn a_conversion_carries_the_value_into_the_other_register_file() {
680 let f64 = Type::float(rucc_ir::Float::F64);
681 let (mut names, mut source, block, args) = blank(&[f64]);
682 let mut build = Builder::new(&mut source, block);
683 let whole = build.unary(Opcode::FPToSI, args[0], Type::int(32));
684 let back = build.unary(Opcode::SIToFP, whole, f64);
685 build.ret(&[back]);
686
687 let machine = Machine::x86_64(&SYSV);
688 let out =
689 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
690 .expect("every instruction has a rule");
691
692 let text = mir::print_func(&out, &names, ®S);
695 assert!(text.contains("x64.cvttsd2si_32"), "{text}");
696 assert!(text.contains("x64.cvtsi2sd_32"), "{text}");
697 assert!(text.contains("$xmm0"), "{text}");
698 }
699
700 #[test]
703 fn a_bitcast_between_the_files_is_the_move_that_changes_no_bit() {
704 let f64 = Type::float(rucc_ir::Float::F64);
705 let (mut names, mut source, block, args) = blank(&[f64]);
706 let mut build = Builder::new(&mut source, block);
707 let bits = build.unary(Opcode::Bitcast, args[0], Type::int(64));
708 build.ret(&[bits]);
709
710 let machine = Machine::x86_64(&SYSV);
711 let out =
712 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
713 .expect("every instruction has a rule");
714
715 let text = mir::print_func(&out, &names, ®S);
716 assert!(text.contains("x64.movq_from_xmm"), "{text}");
717 assert!(!text.contains("cvt"), "{text}");
718 }
719
720 #[test]
722 fn a_float_comparison_is_the_compare_and_the_byte_a_condition_sets() {
723 let f64 = Type::float(rucc_ir::Float::F64);
724 let (mut names, mut source, block, args) = blank(&[f64, f64]);
725 let mut build = Builder::new(&mut source, block);
726 let less = build.fcmp(rucc_ir::FloatPred::Olt, args[0], args[1], ir::Flags::default());
727 let wide = build.unary(Opcode::ZExt, less, Type::int(32));
728 build.ret(&[wide]);
729
730 let machine = Machine::x86_64(&SYSV);
731 let out =
732 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
733 .expect("every instruction has a rule");
734
735 let text = mir::print_func(&out, &names, ®S);
738 assert!(text.contains("x64.ucomisd_set_a"), "{text}");
739 }
740
741 #[test]
746 fn an_equality_between_floats_gets_a_register_for_the_byte_it_needs_twice() {
747 let f64 = Type::float(rucc_ir::Float::F64);
748 let (mut names, mut source, block, args) = blank(&[f64, f64]);
749 let mut build = Builder::new(&mut source, block);
750 let same = build.fcmp(rucc_ir::FloatPred::Oeq, args[0], args[1], ir::Flags::default());
751 let wide = build.unary(Opcode::ZExt, same, Type::int(32));
752 build.ret(&[wide]);
753
754 let machine = Machine::x86_64(&SYSV);
755 let out =
756 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
757 .expect("every instruction has a rule");
758
759 let text = mir::print_func(&out, &names, ®S);
760 let line = text
761 .lines()
762 .find(|line| line.contains("x64.ucomisd_set_e_and_np"))
763 .expect("the rule for an ordered equality fired");
764 let written: Vec<&str> = line
765 .split_once('=')
766 .expect("the instruction writes something")
767 .0
768 .split(',')
769 .map(str::trim)
770 .collect();
771 assert_eq!(written.len(), 2, "{line}");
772 assert_ne!(written[0], written[1], "{line}");
773 }
774
775 #[test]
779 fn a_float_constant_is_the_bits_in_a_register_and_the_move_that_carries_them_over() {
780 let f64 = Type::float(rucc_ir::Float::F64);
781 let (mut names, mut source, block, _) = blank(&[]);
782 let mut build = Builder::new(&mut source, block);
783 let half = build.fconst(f64, 0x3fe0_0000_0000_0000);
784 build.ret(&[half]);
785
786 let machine = Machine::x86_64(&SYSV);
787 let out =
788 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
789 .expect("every instruction has a rule");
790
791 let text = mir::print_func(&out, &names, ®S);
792 assert!(text.contains("x64.mov_ri_64"), "{text}");
793 assert!(text.contains("x64.movq_to_xmm"), "{text}");
794 }
795
796 #[test]
799 fn a_negation_is_the_sign_bit_flipped_and_no_float_instruction_at_all() {
800 let f64 = Type::float(rucc_ir::Float::F64);
801 let (mut names, mut source, block, args) = blank(&[f64]);
802 let mut build = Builder::new(&mut source, block);
803 let less = build.unary(Opcode::FNeg, args[0], f64);
804 build.ret(&[less]);
805
806 let machine = Machine::x86_64(&SYSV);
807 let out =
808 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
809 .expect("every instruction has a rule");
810
811 let text = mir::print_func(&out, &names, ®S);
812 assert!(text.contains("x64.xor_rr_64"), "{text}");
813 assert!(!text.contains("sub"), "a negation is not a subtraction: {text}");
814 }
815
816 #[test]
817 fn the_flags_reach_the_frame() {
818 let i32 = Type::int(32);
819 let (mut names, mut source, block, args) = blank(&[i32]);
820 Builder::new(&mut source, block).ret(&[args[0]]);
821
822 let machine = Machine::x86_64(&SYSV);
823 let flags = Flags { frame_pointer: true, red_zone: true };
824 let out = compile(&mut source, &mut names, &machine, &Elsewhere::default(), flags)
825 .expect("every instruction has a rule");
826
827 let text = mir::print_func(&out, &names, ®S);
830 assert!(text.contains("x64.push_64 $rbp"), "{text}");
831 assert!(text.contains("$rbp = x64.mov_rr_64 $rsp"), "{text}");
832 }
833
834 #[test]
835 fn a_target_says_which_machine_it_is_and_which_convention_it_uses() {
836 let triple = |text: &str| text.parse::<rucc_target::Triple>().expect("a triple");
837 let info = TargetInfo::new(triple("x86_64-unknown-linux-gnu"));
838 let machine = Machine::for_target(&info).expect("x86-64 is the target this crate covers");
839 assert!(std::ptr::eq(machine.conv, &SYSV));
840
841 let info = TargetInfo::new(triple("x86_64-pc-windows-msvc"));
842 let machine = Machine::for_target(&info).expect("x86-64 is the target this crate covers");
843 assert!(std::ptr::eq(machine.conv, &WIN64));
844
845 let info = TargetInfo::new(triple("aarch64-unknown-linux-gnu"));
848 assert!(Machine::for_target(&info).is_none());
849 }
850}