1use std::collections::HashSet;
27
28use rucc_base::Interner;
29use rucc_ir as ir;
30use rucc_mir as mir;
31use rucc_regalloc::assign::Env;
32use rucc_target::{BranchInsts, CallRegs, FrameInsts, PhysReg, RegFile, TargetInfo, x86_64};
33use rucc_tuple::Arch;
34
35use crate::coverage::Fired;
36use crate::elsewhere::Elsewhere;
37use crate::expand;
38use crate::finish::finish;
39use crate::fold;
40use crate::frame::{Frame, Layout};
41use crate::layout;
42use crate::lower::{self, Unsupported};
43use crate::retry;
44use crate::split;
45use crate::switch;
46use crate::varargs;
47use crate::widths;
48
49#[derive(Debug)]
58pub struct Machine {
59 pub conv: &'static CallRegs,
61 pub file: RegFile,
63 pub insts: &'static FrameInsts,
65 pub branch: &'static BranchInsts,
67 pub env: Env,
69}
70
71const SCRATCH: [PhysReg; 2] = [x86_64::R10, x86_64::R11];
86
87const SCRATCH_COUNT: usize = SCRATCH.len();
89
90impl Machine {
91 #[must_use]
98 pub fn x86_64(conv: &'static CallRegs) -> Self {
99 let order: Vec<PhysReg> =
100 conv.int_order.iter().copied().filter(|reg| !SCRATCH.contains(reg)).collect();
101 let free: Vec<PhysReg> =
108 conv.sse_order.iter().copied().filter(|®| !conv.preserves_sse(reg)).collect();
109 let at = free.len().saturating_sub(SCRATCH_COUNT);
110 let sse_scratch: Vec<PhysReg> = free[at..].to_vec();
111 let sse_order: Vec<PhysReg> =
112 conv.sse_order.iter().copied().filter(|reg| !sse_scratch.contains(reg)).collect();
113 Self {
114 conv,
115 file: x86_64::REGS,
116 insts: &x86_64::FRAME,
117 branch: &x86_64::BRANCH,
118 env: Env::new().with(x86_64::GPR, &order, &SCRATCH).with(
119 x86_64::XMM,
120 &sse_order,
121 &sse_scratch,
122 ),
123 }
124 }
125
126 #[must_use]
133 pub fn for_target(target: &TargetInfo) -> Option<Self> {
134 let conv = target.call_regs?;
135 match target.tuple.arch() {
136 Arch::X86_64 => Some(Self::x86_64(conv)),
137 _ => None,
138 }
139 }
140}
141
142#[derive(Debug, Clone, Copy, PartialEq, Eq)]
144pub struct Flags {
145 pub frame_pointer: bool,
147 pub red_zone: bool,
149}
150
151impl Default for Flags {
152 fn default() -> Self {
155 Self { frame_pointer: false, red_zone: true }
156 }
157}
158
159pub fn compile(
178 source: &mut ir::Func,
179 names: &mut Interner,
180 machine: &Machine,
181 elsewhere: &Elsewhere,
182 flags: Flags,
183) -> Result<mir::Func, Unsupported> {
184 compile_recording(source, names, machine, elsewhere, flags, &mut Fired::new())
185}
186
187pub fn compile_recording(
201 source: &mut ir::Func,
202 names: &mut Interner,
203 machine: &Machine,
204 elsewhere: &Elsewhere,
205 flags: Flags,
206 fired: &mut Fired,
207) -> Result<mir::Func, Unsupported> {
208 switch::switches(source);
209 retry::loops(source);
214 expand::orderings(source, machine.conv.word);
217 widths::integers(source);
220 expand::bytes(source);
221 expand::counts(source);
222 expand::overflows(source);
223 expand::floats(source);
224 expand::bulk(source, names, machine.conv.word);
225 varargs::lists(source, machine.conv);
226 let lowered = lower::func(source, names, machine.conv, elsewhere)?;
227 fired.merge(&lowered.fired);
228 let lower::Lowered { mut func, stack, .. } = lowered;
229 let layout = Layout {
230 frame_pointer: flags.frame_pointer,
231 red_zone: flags.red_zone,
232 ..stack.layout(Layout::new(machine.conv, machine.file))
233 };
234
235 let waiting: HashSet<mir::Inst> = stack
241 .addresses
242 .iter()
243 .map(|&(inst, _)| inst)
244 .chain(stack.arguments.iter().map(|&(inst, _)| inst))
245 .collect();
246 fold::addresses(&mut func, machine.insts, names, &waiting);
247
248 split::critical(&mut func);
252 let called = names.resolve(func.name).to_owned();
253 let allocation = rucc_regalloc::run(&mut func, &machine.env, &called);
254
255 let frame = Frame::of(&func, &allocation, &layout);
258 finish(&mut func, &allocation, &frame, &stack, machine.conv, machine.insts, names);
259
260 layout::blocks(&mut func, machine.branch, names);
263 Ok(func)
264}
265
266#[cfg(test)]
267mod tests {
268 use rucc_ir::{Builder, Flags as IrFlags, Func, Opcode, Restrict, Signature, Type};
269 use rucc_target::x86_64::{REGS, SYSV, WIN64};
270
271 use super::*;
272
273 fn blank(params: &[Type]) -> (Interner, Func, ir::Block, Vec<ir::Value>) {
275 let mut names = Interner::new();
276 let mut func = Func::new(names.intern("f"), Signature::new());
277 let block = func.create_block();
278 let values = params.iter().map(|&ty| func.append_param(block, ty)).collect();
279 (names, func, block, values)
280 }
281
282 #[test]
283 fn a_function_comes_out_with_no_virtual_register_left_in_it() {
284 let i32 = Type::int(32);
285 let (mut names, mut source, block, args) = blank(&[i32, i32]);
286 let mut build = Builder::new(&mut source, block);
287 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
288 build.ret(&[sum]);
289
290 let machine = Machine::x86_64(&SYSV);
291 let out =
292 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
293 .expect("every instruction has a rule");
294
295 assert_eq!(
300 mir::print_func(&out, &names, ®S),
301 "mfunc @f {\n\
302 block0:\n \
303 $rdi($rdi) = x64.arg_val_32\n \
304 $rsi($rsi) = x64.arg_val_32\n \
305 $rdi(reuse 1) = x64.add_rr_32 $rdi, $rsi\n \
306 $rax = x64.mov_rr_64 $rdi\n \
307 x64.ret_val_32 $rax($rax)\n \
308 x64.ret\n\
309 }\n"
310 );
311 }
312
313 #[test]
317 fn which_rules_lowered_a_function_is_something_the_compilation_can_be_asked_for() {
318 let i32 = Type::int(32);
319 let (mut names, mut source, block, args) = blank(&[i32, i32]);
320 let mut build = Builder::new(&mut source, block);
321 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
322 build.ret(&[sum]);
323
324 let machine = Machine::x86_64(&SYSV);
325 let mut fired = Fired::new();
326 compile_recording(
327 &mut source,
328 &mut names,
329 &machine,
330 &Elsewhere::default(),
331 Flags::default(),
332 &mut fired,
333 )
334 .expect("every instruction has a rule");
335 let one = fired.count();
336 assert!(one > 0, "an add and a return went through the table and nothing was recorded");
337
338 let listing = fired.listing(&crate::select::x86_64::TABLE);
339 assert_eq!(listing.lines().filter(|line| line.starts_with("fired ")).count(), one);
340 assert!(
341 listing.contains(&format!("{one} of ")),
342 "{}",
343 listing.lines().next().unwrap_or("")
344 );
345
346 let (mut names, mut source, block, args) = blank(&[i32, i32]);
348 let mut build = Builder::new(&mut source, block);
349 let difference = build.binary(Opcode::Sub, args[0], args[1], IrFlags::default());
350 build.ret(&[difference]);
351 compile_recording(
352 &mut source,
353 &mut names,
354 &machine,
355 &Elsewhere::default(),
356 Flags::default(),
357 &mut fired,
358 )
359 .expect("every instruction has a rule");
360 assert!(fired.count() > one, "a subtraction is not an addition");
361 }
362
363 #[test]
364 fn a_function_that_calls_takes_a_frame_and_gives_it_back() {
365 let i32 = Type::int(32);
366 let (mut names, mut source, block, args) = blank(&[i32]);
367 let sig = source.add_signature(Signature::new().with_params(&[i32]).with_returns(&[i32]));
368 let callee = names.intern("g");
369 let call = Builder::new(&mut source, block).call(callee, sig, &[args[0]]);
370 let got = source[call].first_result.expect("an integer comes back");
371 let mut build = Builder::new(&mut source, block);
372 let sum = build.binary(Opcode::Add, got, args[0], IrFlags::default());
373 build.ret(&[sum]);
374
375 let machine = Machine::x86_64(&SYSV);
376 let out =
377 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
378 .expect("every instruction has a rule");
379
380 let text = mir::print_func(&out, &names, ®S);
383 assert!(text.contains("x64.push_64 $rbx"), "{text}");
384 assert!(text.contains("$rbx = x64.pop_64"), "{text}");
385 assert!(text.contains("x64.call $rdi($rdi), @g"), "{text}");
386 assert!(!text.contains('%'), "{text}");
387 }
388
389 #[test]
390 fn the_other_convention_is_the_same_function_somewhere_else() {
391 let i32 = Type::int(32);
392 let (mut names, mut source, block, args) = blank(&[i32, i32]);
393 let mut build = Builder::new(&mut source, block);
394 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
395 build.ret(&[sum]);
396
397 let machine = Machine::x86_64(&WIN64);
398 let out =
399 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
400 .expect("every instruction has a rule");
401
402 let text = mir::print_func(&out, &names, ®S);
405 assert!(text.contains("$rcx($rcx) = x64.arg_val_32"), "{text}");
406 assert!(text.contains("$rdx($rdx) = x64.arg_val_32"), "{text}");
407 assert!(!text.contains("$rdi"), "{text}");
408 }
409
410 #[test]
411 fn a_function_with_a_branch_in_it_goes_through_every_pass() {
412 let i32 = Type::int(32);
413 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
414 let then = source.create_block();
415 let join = source.create_block();
416 let got = source.append_param(join, i32);
417 let mut build = Builder::new(&mut source, entry);
418 let cond = build.icmp(rucc_ir::IntPred::Slt, args[0], args[1]);
419 build.br_if(cond, then, &[], join, &[args[1]]);
420 Builder::new(&mut source, then).jump(join, &[args[0]]);
421 Builder::new(&mut source, join).ret(&[got]);
422
423 let machine = Machine::x86_64(&SYSV);
424 let out =
425 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
426 .expect("every instruction has a rule");
427
428 assert_eq!(out.block_count(), 4);
432
433 let text = mir::print_func(&out, &names, ®S);
442 assert_eq!(
443 text,
444 "mfunc @f {\n\
445 block0:\n \
446 $rdi($rdi) = x64.arg_val_32\n \
447 $rsi($rsi) = x64.arg_val_32\n \
448 $rax = x64.cmp_set_l_32 $rdi, $rsi\n \
449 x64.test_rr_8 $rax\n \
450 x64.jcc_e block2, block1\n\
451 \nblock1:\n \
452 $rax = x64.mov_rr_64 $rdi\n \
453 x64.jmp block3\n\
454 \nblock2:\n \
455 $rax = x64.mov_rr_64 $rsi, block3\n\
456 \nblock3:\n \
457 x64.ret_val_32 $rax($rax)\n \
458 x64.ret\n\
459 }\n"
460 );
461 }
462
463 #[test]
469 fn a_loop_that_carries_its_values_round_keeps_all_of_them() {
470 let i32 = Type::int(32);
471 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
472 let head = source.create_block();
473 let body = source.create_block();
474 let exit = source.create_block();
475 let left = source.append_param(head, i32);
476 let right = source.append_param(head, i32);
477 Builder::new(&mut source, entry).jump(head, &[args[0], args[1]]);
478 let mut build = Builder::new(&mut source, head);
479 let zero = build.iconst(i32, 0);
480 let more = build.icmp(rucc_ir::IntPred::Ne, right, zero);
481 build.br_if(more, body, &[], exit, &[left]);
482 let mut build = Builder::new(&mut source, body);
483 let rest = build.binary(Opcode::SRem, left, right, IrFlags::default());
484 build.jump(head, &[right, rest]);
485 let result = source.append_param(exit, i32);
486 Builder::new(&mut source, exit).ret(&[result]);
487
488 let machine = Machine::x86_64(&SYSV);
489 let out =
490 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
491 .expect("every instruction has a rule");
492
493 assert_eq!(
509 mir::print_func(&out, &names, ®S),
510 "mfunc @f {\n\
511 block0:\n \
512 $rdi($rdi) = x64.arg_val_32\n \
513 $rsi($rsi) = x64.arg_val_32\n \
514 $rcx = x64.mov_rr_64 $rdi, block1\n\
515 \nblock1:\n \
516 $rax = x64.mov_ri_32 0\n \
517 $rax = x64.cmp_set_ne_32 $rsi, $rax\n \
518 x64.test_rr_8 $rax\n \
519 x64.jcc_e block3, block2\n\
520 \nblock2:\n \
521 $rax = x64.mov_rr_64 $rcx\n \
522 $rdx($rdx), early $rax($rax) = x64.idiv_rem_32 $rax($rax), $rsi\n \
523 $rdi = x64.mov_rr_64 $rax\n \
524 $rcx = x64.mov_rr_64 $rsi\n \
525 $rsi = x64.mov_rr_64 $rdx\n \
526 x64.jmp block1\n\
527 \nblock3:\n \
528 $rax = x64.mov_rr_64 $rcx\n \
529 x64.ret_val_32 $rax($rax)\n \
530 x64.ret\n\
531 }\n"
532 );
533 }
534
535 #[test]
540 fn a_function_that_has_been_laid_out_reads_back_as_the_same_function() {
541 let i32 = Type::int(32);
542 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
543 let then = source.create_block();
544 let join = source.create_block();
545 let got = source.append_param(join, i32);
546 let mut build = Builder::new(&mut source, entry);
547 let cond = build.icmp(rucc_ir::IntPred::Slt, args[0], args[1]);
548 build.br_if(cond, then, &[], join, &[args[1]]);
549 Builder::new(&mut source, then).jump(join, &[args[0]]);
550 Builder::new(&mut source, join).ret(&[got]);
551
552 let machine = Machine::x86_64(&SYSV);
553 let out =
554 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
555 .expect("every instruction has a rule");
556
557 let text = mir::print_func(&out, &names, ®S);
558 let read = rucc_mir::parse(&text, &mut names, ®S).expect("what the printer wrote");
559 assert_eq!(mir::print(&read, &names, ®S), text);
560 }
561
562 #[test]
563 fn a_function_this_cannot_lower_is_reported_rather_than_compiled() {
564 let f80 = Type::float(rucc_ir::Float::F80);
565 let (mut names, mut source, block, args) = blank(&[f80, Type::int(64)]);
566 Builder::new(&mut source, block).ret(&args);
567
568 let machine = Machine::x86_64(&SYSV);
572 let failed =
573 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
574 .expect_err("a long double cannot come back beside another value");
575 assert_eq!(failed.to_string(), "what this function gives back is on the x87 stack");
576 }
577
578 #[test]
586 fn a_long_double_arrives_in_memory_and_goes_back_on_the_x87_stack() {
587 let f80 = Type::float(rucc_ir::Float::F80);
588 let (mut names, mut source, block, args) = blank(&[f80, f80]);
589 let mut build = Builder::new(&mut source, block);
590 let sum = build.binary(Opcode::FAdd, args[0], args[1], IrFlags::default());
591 build.ret(&[sum]);
592
593 let machine = Machine::x86_64(&SYSV);
594 let out =
595 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
596 .expect("every instruction has a rule");
597
598 let text = mir::print_func(&out, &names, ®S);
599 assert!(text.contains("x64.lea_64 [$rsp + 32]"), "{text}");
602 assert!(text.contains("x64.lea_64 [$rsp + 48]"), "{text}");
603 assert!(!text.contains("x64.ret_val"), "nothing comes back in a register: {text}");
604 let end: Vec<&str> = text.lines().rev().skip(1).take(3).map(str::trim).collect();
607 assert_eq!(end, ["x64.ret", "$rsp = x64.add_ri_64 $rsp, 24", "x64.fld_t [$rax]"], "{text}");
608 }
609
610 #[test]
614 fn a_float_is_added_in_the_register_file_it_arrives_in() {
615 let f32 = Type::float(rucc_ir::Float::F32);
616 let (mut names, mut source, block, args) = blank(&[f32, f32]);
617 let mut build = Builder::new(&mut source, block);
618 let sum = build.binary(Opcode::FAdd, args[0], args[1], ir::Flags::default());
619 build.ret(&[sum]);
620
621 let machine = Machine::x86_64(&SYSV);
622 let out =
623 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
624 .expect("every instruction has a rule");
625
626 let text = mir::print_func(&out, &names, ®S);
627 assert!(text.contains("x64.addss_rr"), "{text}");
628 assert!(text.contains("$xmm0"), "{text}");
629 assert!(!text.contains("$rax"), "{text}");
630 }
631
632 #[test]
635 fn a_float_read_from_memory_and_written_back_uses_the_scalar_moves() {
636 let f64 = Type::float(rucc_ir::Float::F64);
637 let (mut names, mut source, block, args) = blank(&[Type::PTR, f64]);
638 let mut build = Builder::new(&mut source, block);
639 let info = rucc_ir::MemInfo {
640 size: 8,
641 align: 8,
642 order: rucc_ir::MemOrder::NotAtomic,
643 tbaa: None,
644 restrict: Restrict::NONE,
645 };
646 let read = build.load(f64, args[0], info, ir::Flags::default());
647 let sum = build.binary(Opcode::FAdd, read, args[1], ir::Flags::default());
648 build.store(sum, args[0], info, ir::Flags::default());
649 build.ret(&[sum]);
650
651 let machine = Machine::x86_64(&SYSV);
652 let out =
653 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
654 .expect("every instruction has a rule");
655
656 let text = mir::print_func(&out, &names, ®S);
657 assert!(text.contains("x64.movsd_rm"), "{text}");
658 assert!(text.contains("x64.movsd_mr"), "{text}");
659 assert!(!text.contains("x64.movaps_rm"), "{text}");
662 assert!(!text.contains("x64.movaps_mr"), "{text}");
663 }
664
665 #[test]
673 fn an_unsigned_word_and_a_long_double_convert_into_each_other() {
674 let f80 = Type::float(rucc_ir::Float::F80);
675 let (mut names, mut source, block, args) = blank(&[Type::PTR, Type::int(64)]);
676 let mut build = Builder::new(&mut source, block);
677 let info = rucc_ir::MemInfo {
678 size: 16,
679 align: 16,
680 order: rucc_ir::MemOrder::NotAtomic,
681 tbaa: None,
682 restrict: Restrict::NONE,
683 };
684 let wide = build.unary(Opcode::UIToFP, args[1], f80);
685 build.store(wide, args[0], info, ir::Flags::default());
686 let read = build.load(f80, args[0], info, ir::Flags::default());
687 let back = build.unary(Opcode::FPToUI, read, Type::int(64));
688 build.ret(&[back]);
689
690 let machine = Machine::x86_64(&SYSV);
691 let out =
692 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
693 .expect("every instruction has a rule");
694
695 let text = mir::print_func(&out, &names, ®S);
696 assert!(text.contains("x64.fild_ll"), "the integer goes in as a signed one: {text}");
699 assert!(text.contains("x64.fistp_ll"), "and comes back out as one: {text}");
700 assert!(text.contains("x64.fmul_p"), "the correction is taken or not: {text}");
701 assert!(text.contains("x64.fadd_p"), "and applied one way: {text}");
702 assert!(text.contains("x64.fsubr_p"), "and the other: {text}");
703 assert!(!text.contains("xmm"), "no part of this is in a vector register: {text}");
704 }
705
706 #[test]
711 fn a_conversion_carries_the_value_into_the_other_register_file() {
712 let f64 = Type::float(rucc_ir::Float::F64);
713 let (mut names, mut source, block, args) = blank(&[f64]);
714 let mut build = Builder::new(&mut source, block);
715 let whole = build.unary(Opcode::FPToSI, args[0], Type::int(32));
716 let back = build.unary(Opcode::SIToFP, whole, f64);
717 build.ret(&[back]);
718
719 let machine = Machine::x86_64(&SYSV);
720 let out =
721 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
722 .expect("every instruction has a rule");
723
724 let text = mir::print_func(&out, &names, ®S);
727 assert!(text.contains("x64.cvttsd2si_32"), "{text}");
728 assert!(text.contains("x64.cvtsi2sd_32"), "{text}");
729 assert!(text.contains("$xmm0"), "{text}");
730 }
731
732 #[test]
735 fn a_bitcast_between_the_files_is_the_move_that_changes_no_bit() {
736 let f64 = Type::float(rucc_ir::Float::F64);
737 let (mut names, mut source, block, args) = blank(&[f64]);
738 let mut build = Builder::new(&mut source, block);
739 let bits = build.unary(Opcode::Bitcast, args[0], Type::int(64));
740 build.ret(&[bits]);
741
742 let machine = Machine::x86_64(&SYSV);
743 let out =
744 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
745 .expect("every instruction has a rule");
746
747 let text = mir::print_func(&out, &names, ®S);
748 assert!(text.contains("x64.movq_from_xmm"), "{text}");
749 assert!(!text.contains("cvt"), "{text}");
750 }
751
752 #[test]
754 fn a_float_comparison_is_the_compare_and_the_byte_a_condition_sets() {
755 let f64 = Type::float(rucc_ir::Float::F64);
756 let (mut names, mut source, block, args) = blank(&[f64, f64]);
757 let mut build = Builder::new(&mut source, block);
758 let less = build.fcmp(rucc_ir::FloatPred::Olt, args[0], args[1], ir::Flags::default());
759 let wide = build.unary(Opcode::ZExt, less, Type::int(32));
760 build.ret(&[wide]);
761
762 let machine = Machine::x86_64(&SYSV);
763 let out =
764 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
765 .expect("every instruction has a rule");
766
767 let text = mir::print_func(&out, &names, ®S);
770 assert!(text.contains("x64.ucomisd_set_a"), "{text}");
771 }
772
773 #[test]
778 fn an_equality_between_floats_gets_a_register_for_the_byte_it_needs_twice() {
779 let f64 = Type::float(rucc_ir::Float::F64);
780 let (mut names, mut source, block, args) = blank(&[f64, f64]);
781 let mut build = Builder::new(&mut source, block);
782 let same = build.fcmp(rucc_ir::FloatPred::Oeq, args[0], args[1], ir::Flags::default());
783 let wide = build.unary(Opcode::ZExt, same, Type::int(32));
784 build.ret(&[wide]);
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 let line = text
793 .lines()
794 .find(|line| line.contains("x64.ucomisd_set_e_and_np"))
795 .expect("the rule for an ordered equality fired");
796 let written: Vec<&str> = line
797 .split_once('=')
798 .expect("the instruction writes something")
799 .0
800 .split(',')
801 .map(str::trim)
802 .collect();
803 assert_eq!(written.len(), 2, "{line}");
804 assert_ne!(written[0], written[1], "{line}");
805 }
806
807 #[test]
811 fn a_float_constant_is_the_bits_in_a_register_and_the_move_that_carries_them_over() {
812 let f64 = Type::float(rucc_ir::Float::F64);
813 let (mut names, mut source, block, _) = blank(&[]);
814 let mut build = Builder::new(&mut source, block);
815 let half = build.fconst(f64, 0x3fe0_0000_0000_0000);
816 build.ret(&[half]);
817
818 let machine = Machine::x86_64(&SYSV);
819 let out =
820 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
821 .expect("every instruction has a rule");
822
823 let text = mir::print_func(&out, &names, ®S);
824 assert!(text.contains("x64.mov_ri_64"), "{text}");
825 assert!(text.contains("x64.movq_to_xmm"), "{text}");
826 }
827
828 #[test]
831 fn a_negation_is_the_sign_bit_flipped_and_no_float_instruction_at_all() {
832 let f64 = Type::float(rucc_ir::Float::F64);
833 let (mut names, mut source, block, args) = blank(&[f64]);
834 let mut build = Builder::new(&mut source, block);
835 let less = build.unary(Opcode::FNeg, args[0], f64);
836 build.ret(&[less]);
837
838 let machine = Machine::x86_64(&SYSV);
839 let out =
840 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
841 .expect("every instruction has a rule");
842
843 let text = mir::print_func(&out, &names, ®S);
844 assert!(text.contains("x64.xor_rr_64"), "{text}");
845 assert!(!text.contains("sub"), "a negation is not a subtraction: {text}");
846 }
847
848 #[test]
849 fn the_flags_reach_the_frame() {
850 let i32 = Type::int(32);
851 let (mut names, mut source, block, args) = blank(&[i32]);
852 Builder::new(&mut source, block).ret(&[args[0]]);
853
854 let machine = Machine::x86_64(&SYSV);
855 let flags = Flags { frame_pointer: true, red_zone: true };
856 let out = compile(&mut source, &mut names, &machine, &Elsewhere::default(), flags)
857 .expect("every instruction has a rule");
858
859 let text = mir::print_func(&out, &names, ®S);
862 assert!(text.contains("x64.push_64 $rbp"), "{text}");
863 assert!(text.contains("$rbp = x64.mov_rr_64 $rsp"), "{text}");
864 }
865
866 #[test]
867 fn a_target_says_which_machine_it_is_and_which_convention_it_uses() {
868 let triple = |text: &str| text.parse::<rucc_target::Triple>().expect("a triple");
869 let info = TargetInfo::new(triple("x86_64-unknown-linux-gnu"));
870 let machine = Machine::for_target(&info).expect("x86-64 is the target this crate covers");
871 assert!(std::ptr::eq(machine.conv, &SYSV));
872
873 let info = TargetInfo::new(triple("x86_64-pc-windows-msvc"));
874 let machine = Machine::for_target(&info).expect("x86-64 is the target this crate covers");
875 assert!(std::ptr::eq(machine.conv, &WIN64));
876
877 let info = TargetInfo::new(triple("aarch64-unknown-linux-gnu"));
880 assert!(Machine::for_target(&info).is_none());
881 }
882}