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::expand;
34use crate::finish::finish;
35use crate::frame::{Frame, Layout};
36use crate::layout;
37use crate::lower::{self, Unsupported};
38use crate::split;
39use crate::switch;
40use crate::varargs;
41use crate::widths;
42
43#[derive(Debug)]
52pub struct Machine {
53 pub conv: &'static CallRegs,
55 pub file: RegFile,
57 pub insts: &'static FrameInsts,
59 pub branch: &'static BranchInsts,
61 pub env: Env,
63}
64
65const SCRATCH: [PhysReg; 2] = [x86_64::R10, x86_64::R11];
72
73const SCRATCH_COUNT: usize = SCRATCH.len();
75
76impl Machine {
77 #[must_use]
84 pub fn x86_64(conv: &'static CallRegs) -> Self {
85 let order: Vec<PhysReg> =
86 conv.int_order.iter().copied().filter(|reg| !SCRATCH.contains(reg)).collect();
87 let free: Vec<PhysReg> =
94 conv.sse_order.iter().copied().filter(|®| !conv.preserves_sse(reg)).collect();
95 let at = free.len().saturating_sub(SCRATCH_COUNT);
96 let sse_scratch: Vec<PhysReg> = free[at..].to_vec();
97 let sse_order: Vec<PhysReg> =
98 conv.sse_order.iter().copied().filter(|reg| !sse_scratch.contains(reg)).collect();
99 Self {
100 conv,
101 file: x86_64::REGS,
102 insts: &x86_64::FRAME,
103 branch: &x86_64::BRANCH,
104 env: Env::new().with(x86_64::GPR, &order, &SCRATCH).with(
105 x86_64::XMM,
106 &sse_order,
107 &sse_scratch,
108 ),
109 }
110 }
111
112 #[must_use]
119 pub fn for_target(target: &TargetInfo) -> Option<Self> {
120 let conv = target.call_regs?;
121 match target.triple.arch {
122 Arch::X86_64 => Some(Self::x86_64(conv)),
123 Arch::Aarch64 | Arch::Riscv64 => None,
124 }
125 }
126}
127
128#[derive(Debug, Clone, Copy, PartialEq, Eq)]
130pub struct Flags {
131 pub frame_pointer: bool,
133 pub red_zone: bool,
135}
136
137impl Default for Flags {
138 fn default() -> Self {
141 Self { frame_pointer: false, red_zone: true }
142 }
143}
144
145pub fn compile(
159 source: &mut ir::Func,
160 names: &mut Interner,
161 machine: &Machine,
162 flags: Flags,
163) -> Result<mir::Func, Unsupported> {
164 compile_recording(source, names, machine, flags, &mut Fired::new())
165}
166
167pub fn compile_recording(
181 source: &mut ir::Func,
182 names: &mut Interner,
183 machine: &Machine,
184 flags: Flags,
185 fired: &mut Fired,
186) -> Result<mir::Func, Unsupported> {
187 switch::switches(source);
188 expand::orderings(source, machine.conv.word);
191 widths::integers(source);
194 expand::bytes(source);
195 expand::counts(source);
196 expand::overflows(source);
197 expand::floats(source);
198 expand::bulk(source, names, machine.conv.word);
199 varargs::lists(source, machine.conv);
200 let lowered = lower::func(source, names, machine.conv)?;
201 fired.merge(&lowered.fired);
202 let lower::Lowered { mut func, stack, .. } = lowered;
203 let layout = Layout {
204 frame_pointer: flags.frame_pointer,
205 red_zone: flags.red_zone,
206 ..stack.layout(Layout::new(machine.conv, machine.file))
207 };
208
209 split::critical(&mut func);
213 let allocation = rucc_regalloc::run(&mut func, &machine.env);
214
215 let frame = Frame::of(&func, &allocation, &layout);
218 finish(&mut func, &allocation, &frame, &stack, machine.conv, machine.insts, names);
219
220 layout::blocks(&mut func, machine.branch, names);
223 Ok(func)
224}
225
226#[cfg(test)]
227mod tests {
228 use rucc_ir::{Builder, Flags as IrFlags, Func, Opcode, Restrict, Signature, Type};
229 use rucc_target::x86_64::{REGS, SYSV, WIN64};
230
231 use super::*;
232
233 fn blank(params: &[Type]) -> (Interner, Func, ir::Block, Vec<ir::Value>) {
235 let mut names = Interner::new();
236 let mut func = Func::new(names.intern("f"), Signature::new());
237 let block = func.create_block();
238 let values = params.iter().map(|&ty| func.append_param(block, ty)).collect();
239 (names, func, block, values)
240 }
241
242 #[test]
243 fn a_function_comes_out_with_no_virtual_register_left_in_it() {
244 let i32 = Type::int(32);
245 let (mut names, mut source, block, args) = blank(&[i32, i32]);
246 let mut build = Builder::new(&mut source, block);
247 let sum = build.binary(Opcode::Add, args[0], args[1], 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 assert_eq!(
259 mir::print_func(&out, &names, ®S),
260 "mfunc @f {\n\
261 block0:\n \
262 $rdi($rdi) = x64.arg_val_32\n \
263 $rsi($rsi) = x64.arg_val_32\n \
264 $rdi(reuse 1) = x64.add_rr_32 $rdi, $rsi\n \
265 $rax = x64.mov_rr_64 $rdi\n \
266 x64.ret_val_32 $rax($rax)\n \
267 x64.ret\n\
268 }\n"
269 );
270 }
271
272 #[test]
276 fn which_rules_lowered_a_function_is_something_the_compilation_can_be_asked_for() {
277 let i32 = Type::int(32);
278 let (mut names, mut source, block, args) = blank(&[i32, i32]);
279 let mut build = Builder::new(&mut source, block);
280 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
281 build.ret(&[sum]);
282
283 let machine = Machine::x86_64(&SYSV);
284 let mut fired = Fired::new();
285 compile_recording(&mut source, &mut names, &machine, Flags::default(), &mut fired)
286 .expect("every instruction has a rule");
287 let one = fired.count();
288 assert!(one > 0, "an add and a return went through the table and nothing was recorded");
289
290 let listing = fired.listing(&crate::select::x86_64::TABLE);
291 assert_eq!(listing.lines().filter(|line| line.starts_with("fired ")).count(), one);
292 assert!(
293 listing.contains(&format!("{one} of ")),
294 "{}",
295 listing.lines().next().unwrap_or("")
296 );
297
298 let (mut names, mut source, block, args) = blank(&[i32, i32]);
300 let mut build = Builder::new(&mut source, block);
301 let difference = build.binary(Opcode::Sub, args[0], args[1], IrFlags::default());
302 build.ret(&[difference]);
303 compile_recording(&mut source, &mut names, &machine, Flags::default(), &mut fired)
304 .expect("every instruction has a rule");
305 assert!(fired.count() > one, "a subtraction is not an addition");
306 }
307
308 #[test]
309 fn a_function_that_calls_takes_a_frame_and_gives_it_back() {
310 let i32 = Type::int(32);
311 let (mut names, mut source, block, args) = blank(&[i32]);
312 let sig = source.add_signature(Signature::new().with_params(&[i32]).with_returns(&[i32]));
313 let callee = names.intern("g");
314 let call = Builder::new(&mut source, block).call(callee, sig, &[args[0]]);
315 let got = source[call].first_result.expect("an integer comes back");
316 let mut build = Builder::new(&mut source, block);
317 let sum = build.binary(Opcode::Add, got, args[0], IrFlags::default());
318 build.ret(&[sum]);
319
320 let machine = Machine::x86_64(&SYSV);
321 let out = compile(&mut source, &mut names, &machine, Flags::default())
322 .expect("every instruction has a rule");
323
324 let text = mir::print_func(&out, &names, ®S);
327 assert!(text.contains("x64.push_64 $rbx"), "{text}");
328 assert!(text.contains("$rbx = x64.pop_64"), "{text}");
329 assert!(text.contains("x64.call $rdi($rdi), @g"), "{text}");
330 assert!(!text.contains('%'), "{text}");
331 }
332
333 #[test]
334 fn the_other_convention_is_the_same_function_somewhere_else() {
335 let i32 = Type::int(32);
336 let (mut names, mut source, block, args) = blank(&[i32, i32]);
337 let mut build = Builder::new(&mut source, block);
338 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
339 build.ret(&[sum]);
340
341 let machine = Machine::x86_64(&WIN64);
342 let out = compile(&mut source, &mut names, &machine, Flags::default())
343 .expect("every instruction has a rule");
344
345 let text = mir::print_func(&out, &names, ®S);
348 assert!(text.contains("$rcx($rcx) = x64.arg_val_32"), "{text}");
349 assert!(text.contains("$rdx($rdx) = x64.arg_val_32"), "{text}");
350 assert!(!text.contains("$rdi"), "{text}");
351 }
352
353 #[test]
354 fn a_function_with_a_branch_in_it_goes_through_every_pass() {
355 let i32 = Type::int(32);
356 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
357 let then = source.create_block();
358 let join = source.create_block();
359 let got = source.append_param(join, i32);
360 let mut build = Builder::new(&mut source, entry);
361 let cond = build.icmp(rucc_ir::IntPred::Slt, args[0], args[1]);
362 build.br_if(cond, then, &[], join, &[args[1]]);
363 Builder::new(&mut source, then).jump(join, &[args[0]]);
364 Builder::new(&mut source, join).ret(&[got]);
365
366 let machine = Machine::x86_64(&SYSV);
367 let out = compile(&mut source, &mut names, &machine, Flags::default())
368 .expect("every instruction has a rule");
369
370 assert_eq!(out.block_count(), 4);
374
375 let text = mir::print_func(&out, &names, ®S);
384 assert_eq!(
385 text,
386 "mfunc @f {\n\
387 block0:\n \
388 $rdi($rdi) = x64.arg_val_32\n \
389 $rsi($rsi) = x64.arg_val_32\n \
390 $rax = x64.cmp_set_l_32 $rdi, $rsi\n \
391 x64.test_rr_8 $rax\n \
392 x64.jcc_e block2, block1\n\
393 \nblock1:\n \
394 $rax = x64.mov_rr_64 $rdi\n \
395 x64.jmp block3\n\
396 \nblock2:\n \
397 $rax = x64.mov_rr_64 $rsi, block3\n\
398 \nblock3:\n \
399 x64.ret_val_32 $rax($rax)\n \
400 x64.ret\n\
401 }\n"
402 );
403 }
404
405 #[test]
411 fn a_loop_that_carries_its_values_round_keeps_all_of_them() {
412 let i32 = Type::int(32);
413 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
414 let head = source.create_block();
415 let body = source.create_block();
416 let exit = source.create_block();
417 let left = source.append_param(head, i32);
418 let right = source.append_param(head, i32);
419 Builder::new(&mut source, entry).jump(head, &[args[0], args[1]]);
420 let mut build = Builder::new(&mut source, head);
421 let zero = build.iconst(i32, 0);
422 let more = build.icmp(rucc_ir::IntPred::Ne, right, zero);
423 build.br_if(more, body, &[], exit, &[left]);
424 let mut build = Builder::new(&mut source, body);
425 let rest = build.binary(Opcode::SRem, left, right, IrFlags::default());
426 build.jump(head, &[right, rest]);
427 let result = source.append_param(exit, i32);
428 Builder::new(&mut source, exit).ret(&[result]);
429
430 let machine = Machine::x86_64(&SYSV);
431 let out = compile(&mut source, &mut names, &machine, Flags::default())
432 .expect("every instruction has a rule");
433
434 assert_eq!(
450 mir::print_func(&out, &names, ®S),
451 "mfunc @f {\n\
452 block0:\n \
453 $rdi($rdi) = x64.arg_val_32\n \
454 $rsi($rsi) = x64.arg_val_32\n \
455 $rcx = x64.mov_rr_64 $rdi, block1\n\
456 \nblock1:\n \
457 $rax = x64.mov_ri_32 0\n \
458 $rax = x64.cmp_set_ne_32 $rsi, $rax\n \
459 x64.test_rr_8 $rax\n \
460 x64.jcc_e block3, block2\n\
461 \nblock2:\n \
462 $rax = x64.mov_rr_64 $rcx\n \
463 $rdx($rdx), early $rax($rax) = x64.idiv_rem_32 $rax($rax), $rsi\n \
464 $rdi = x64.mov_rr_64 $rax\n \
465 $rcx = x64.mov_rr_64 $rsi\n \
466 $rsi = x64.mov_rr_64 $rdx\n \
467 x64.jmp block1\n\
468 \nblock3:\n \
469 $rax = x64.mov_rr_64 $rcx\n \
470 x64.ret_val_32 $rax($rax)\n \
471 x64.ret\n\
472 }\n"
473 );
474 }
475
476 #[test]
481 fn a_function_that_has_been_laid_out_reads_back_as_the_same_function() {
482 let i32 = Type::int(32);
483 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
484 let then = source.create_block();
485 let join = source.create_block();
486 let got = source.append_param(join, i32);
487 let mut build = Builder::new(&mut source, entry);
488 let cond = build.icmp(rucc_ir::IntPred::Slt, args[0], args[1]);
489 build.br_if(cond, then, &[], join, &[args[1]]);
490 Builder::new(&mut source, then).jump(join, &[args[0]]);
491 Builder::new(&mut source, join).ret(&[got]);
492
493 let machine = Machine::x86_64(&SYSV);
494 let out = compile(&mut source, &mut names, &machine, Flags::default())
495 .expect("every instruction has a rule");
496
497 let text = mir::print_func(&out, &names, ®S);
498 let read = rucc_mir::parse(&text, &mut names, ®S).expect("what the printer wrote");
499 assert_eq!(mir::print(&read, &names, ®S), text);
500 }
501
502 #[test]
503 fn a_function_this_cannot_lower_is_reported_rather_than_compiled() {
504 let f80 = Type::float(rucc_ir::Float::F80);
505 let (mut names, mut source, block, args) = blank(&[f80, Type::int(64)]);
506 Builder::new(&mut source, block).ret(&args);
507
508 let machine = Machine::x86_64(&SYSV);
512 let failed = compile(&mut source, &mut names, &machine, Flags::default())
513 .expect_err("a long double cannot come back beside another value");
514 assert_eq!(failed.to_string(), "what this function gives back is on the x87 stack");
515 }
516
517 #[test]
525 fn a_long_double_arrives_in_memory_and_goes_back_on_the_x87_stack() {
526 let f80 = Type::float(rucc_ir::Float::F80);
527 let (mut names, mut source, block, args) = blank(&[f80, f80]);
528 let mut build = Builder::new(&mut source, block);
529 let sum = build.binary(Opcode::FAdd, args[0], args[1], IrFlags::default());
530 build.ret(&[sum]);
531
532 let machine = Machine::x86_64(&SYSV);
533 let out = compile(&mut source, &mut names, &machine, Flags::default())
534 .expect("every instruction has a rule");
535
536 let text = mir::print_func(&out, &names, ®S);
537 assert!(text.contains("x64.lea_64 [$rsp + 32]"), "{text}");
540 assert!(text.contains("x64.lea_64 [$rsp + 48]"), "{text}");
541 assert!(!text.contains("x64.ret_val"), "nothing comes back in a register: {text}");
542 let end: Vec<&str> = text.lines().rev().skip(1).take(3).map(str::trim).collect();
545 assert_eq!(end, ["x64.ret", "$rsp = x64.add_ri_64 $rsp, 24", "x64.fld_t [$rax]"], "{text}");
546 }
547
548 #[test]
552 fn a_float_is_added_in_the_register_file_it_arrives_in() {
553 let f32 = Type::float(rucc_ir::Float::F32);
554 let (mut names, mut source, block, args) = blank(&[f32, f32]);
555 let mut build = Builder::new(&mut source, block);
556 let sum = build.binary(Opcode::FAdd, args[0], args[1], ir::Flags::default());
557 build.ret(&[sum]);
558
559 let machine = Machine::x86_64(&SYSV);
560 let out = compile(&mut source, &mut names, &machine, Flags::default())
561 .expect("every instruction has a rule");
562
563 let text = mir::print_func(&out, &names, ®S);
564 assert!(text.contains("x64.addss_rr"), "{text}");
565 assert!(text.contains("$xmm0"), "{text}");
566 assert!(!text.contains("$rax"), "{text}");
567 }
568
569 #[test]
572 fn a_float_read_from_memory_and_written_back_uses_the_scalar_moves() {
573 let f64 = Type::float(rucc_ir::Float::F64);
574 let (mut names, mut source, block, args) = blank(&[Type::PTR, f64]);
575 let mut build = Builder::new(&mut source, block);
576 let info = rucc_ir::MemInfo {
577 size: 8,
578 align: 8,
579 order: rucc_ir::MemOrder::NotAtomic,
580 tbaa: None,
581 restrict: Restrict::NONE,
582 };
583 let read = build.load(f64, args[0], info, ir::Flags::default());
584 let sum = build.binary(Opcode::FAdd, read, args[1], ir::Flags::default());
585 build.store(sum, args[0], info, ir::Flags::default());
586 build.ret(&[sum]);
587
588 let machine = Machine::x86_64(&SYSV);
589 let out = compile(&mut source, &mut names, &machine, Flags::default())
590 .expect("every instruction has a rule");
591
592 let text = mir::print_func(&out, &names, ®S);
593 assert!(text.contains("x64.movsd_rm"), "{text}");
594 assert!(text.contains("x64.movsd_mr"), "{text}");
595 assert!(!text.contains("x64.movaps_rm"), "{text}");
598 assert!(!text.contains("x64.movaps_mr"), "{text}");
599 }
600
601 #[test]
609 fn an_unsigned_word_and_a_long_double_convert_into_each_other() {
610 let f80 = Type::float(rucc_ir::Float::F80);
611 let (mut names, mut source, block, args) = blank(&[Type::PTR, Type::int(64)]);
612 let mut build = Builder::new(&mut source, block);
613 let info = rucc_ir::MemInfo {
614 size: 16,
615 align: 16,
616 order: rucc_ir::MemOrder::NotAtomic,
617 tbaa: None,
618 restrict: Restrict::NONE,
619 };
620 let wide = build.unary(Opcode::UIToFP, args[1], f80);
621 build.store(wide, args[0], info, ir::Flags::default());
622 let read = build.load(f80, args[0], info, ir::Flags::default());
623 let back = build.unary(Opcode::FPToUI, read, Type::int(64));
624 build.ret(&[back]);
625
626 let machine = Machine::x86_64(&SYSV);
627 let out = compile(&mut source, &mut names, &machine, Flags::default())
628 .expect("every instruction has a rule");
629
630 let text = mir::print_func(&out, &names, ®S);
631 assert!(text.contains("x64.fild_ll"), "the integer goes in as a signed one: {text}");
634 assert!(text.contains("x64.fistp_ll"), "and comes back out as one: {text}");
635 assert!(text.contains("x64.fmul_p"), "the correction is taken or not: {text}");
636 assert!(text.contains("x64.fadd_p"), "and applied one way: {text}");
637 assert!(text.contains("x64.fsub_p"), "and the other: {text}");
638 assert!(!text.contains("xmm"), "no part of this is in a vector register: {text}");
639 }
640
641 #[test]
646 fn a_conversion_carries_the_value_into_the_other_register_file() {
647 let f64 = Type::float(rucc_ir::Float::F64);
648 let (mut names, mut source, block, args) = blank(&[f64]);
649 let mut build = Builder::new(&mut source, block);
650 let whole = build.unary(Opcode::FPToSI, args[0], Type::int(32));
651 let back = build.unary(Opcode::SIToFP, whole, f64);
652 build.ret(&[back]);
653
654 let machine = Machine::x86_64(&SYSV);
655 let out = compile(&mut source, &mut names, &machine, Flags::default())
656 .expect("every instruction has a rule");
657
658 let text = mir::print_func(&out, &names, ®S);
661 assert!(text.contains("x64.cvttsd2si_32"), "{text}");
662 assert!(text.contains("x64.cvtsi2sd_32"), "{text}");
663 assert!(text.contains("$xmm0"), "{text}");
664 }
665
666 #[test]
669 fn a_bitcast_between_the_files_is_the_move_that_changes_no_bit() {
670 let f64 = Type::float(rucc_ir::Float::F64);
671 let (mut names, mut source, block, args) = blank(&[f64]);
672 let mut build = Builder::new(&mut source, block);
673 let bits = build.unary(Opcode::Bitcast, args[0], Type::int(64));
674 build.ret(&[bits]);
675
676 let machine = Machine::x86_64(&SYSV);
677 let out = compile(&mut source, &mut names, &machine, Flags::default())
678 .expect("every instruction has a rule");
679
680 let text = mir::print_func(&out, &names, ®S);
681 assert!(text.contains("x64.movq_from_xmm"), "{text}");
682 assert!(!text.contains("cvt"), "{text}");
683 }
684
685 #[test]
687 fn a_float_comparison_is_the_compare_and_the_byte_a_condition_sets() {
688 let f64 = Type::float(rucc_ir::Float::F64);
689 let (mut names, mut source, block, args) = blank(&[f64, f64]);
690 let mut build = Builder::new(&mut source, block);
691 let less = build.fcmp(rucc_ir::FloatPred::Olt, args[0], args[1], ir::Flags::default());
692 let wide = build.unary(Opcode::ZExt, less, Type::int(32));
693 build.ret(&[wide]);
694
695 let machine = Machine::x86_64(&SYSV);
696 let out = compile(&mut source, &mut names, &machine, Flags::default())
697 .expect("every instruction has a rule");
698
699 let text = mir::print_func(&out, &names, ®S);
702 assert!(text.contains("x64.ucomisd_set_a"), "{text}");
703 }
704
705 #[test]
710 fn an_equality_between_floats_gets_a_register_for_the_byte_it_needs_twice() {
711 let f64 = Type::float(rucc_ir::Float::F64);
712 let (mut names, mut source, block, args) = blank(&[f64, f64]);
713 let mut build = Builder::new(&mut source, block);
714 let same = build.fcmp(rucc_ir::FloatPred::Oeq, args[0], args[1], ir::Flags::default());
715 let wide = build.unary(Opcode::ZExt, same, Type::int(32));
716 build.ret(&[wide]);
717
718 let machine = Machine::x86_64(&SYSV);
719 let out = compile(&mut source, &mut names, &machine, Flags::default())
720 .expect("every instruction has a rule");
721
722 let text = mir::print_func(&out, &names, ®S);
723 let line = text
724 .lines()
725 .find(|line| line.contains("x64.ucomisd_set_e_and_np"))
726 .expect("the rule for an ordered equality fired");
727 let written: Vec<&str> = line
728 .split_once('=')
729 .expect("the instruction writes something")
730 .0
731 .split(',')
732 .map(str::trim)
733 .collect();
734 assert_eq!(written.len(), 2, "{line}");
735 assert_ne!(written[0], written[1], "{line}");
736 }
737
738 #[test]
742 fn a_float_constant_is_the_bits_in_a_register_and_the_move_that_carries_them_over() {
743 let f64 = Type::float(rucc_ir::Float::F64);
744 let (mut names, mut source, block, _) = blank(&[]);
745 let mut build = Builder::new(&mut source, block);
746 let half = build.fconst(f64, 0x3fe0_0000_0000_0000);
747 build.ret(&[half]);
748
749 let machine = Machine::x86_64(&SYSV);
750 let out = compile(&mut source, &mut names, &machine, Flags::default())
751 .expect("every instruction has a rule");
752
753 let text = mir::print_func(&out, &names, ®S);
754 assert!(text.contains("x64.mov_ri_64"), "{text}");
755 assert!(text.contains("x64.movq_to_xmm"), "{text}");
756 }
757
758 #[test]
761 fn a_negation_is_the_sign_bit_flipped_and_no_float_instruction_at_all() {
762 let f64 = Type::float(rucc_ir::Float::F64);
763 let (mut names, mut source, block, args) = blank(&[f64]);
764 let mut build = Builder::new(&mut source, block);
765 let less = build.unary(Opcode::FNeg, args[0], f64);
766 build.ret(&[less]);
767
768 let machine = Machine::x86_64(&SYSV);
769 let out = compile(&mut source, &mut names, &machine, Flags::default())
770 .expect("every instruction has a rule");
771
772 let text = mir::print_func(&out, &names, ®S);
773 assert!(text.contains("x64.xor_rr_64"), "{text}");
774 assert!(!text.contains("sub"), "a negation is not a subtraction: {text}");
775 }
776
777 #[test]
778 fn the_flags_reach_the_frame() {
779 let i32 = Type::int(32);
780 let (mut names, mut source, block, args) = blank(&[i32]);
781 Builder::new(&mut source, block).ret(&[args[0]]);
782
783 let machine = Machine::x86_64(&SYSV);
784 let flags = Flags { frame_pointer: true, red_zone: true };
785 let out = compile(&mut source, &mut names, &machine, flags)
786 .expect("every instruction has a rule");
787
788 let text = mir::print_func(&out, &names, ®S);
791 assert!(text.contains("x64.push_64 $rbp"), "{text}");
792 assert!(text.contains("$rbp = x64.mov_rr_64 $rsp"), "{text}");
793 }
794
795 #[test]
796 fn a_target_says_which_machine_it_is_and_which_convention_it_uses() {
797 let triple = |text: &str| text.parse::<rucc_target::Triple>().expect("a triple");
798 let info = TargetInfo::new(triple("x86_64-unknown-linux-gnu"));
799 let machine = Machine::for_target(&info).expect("x86-64 is the target this crate covers");
800 assert!(std::ptr::eq(machine.conv, &SYSV));
801
802 let info = TargetInfo::new(triple("x86_64-pc-windows-msvc"));
803 let machine = Machine::for_target(&info).expect("x86-64 is the target this crate covers");
804 assert!(std::ptr::eq(machine.conv, &WIN64));
805
806 let info = TargetInfo::new(triple("aarch64-unknown-linux-gnu"));
809 assert!(Machine::for_target(&info).is_none());
810 }
811}