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 let fusable = layout::fusable(&func, machine.branch, names);
253
254 split::critical(&mut func);
258 let called = names.resolve(func.name).to_owned();
259 let allocation = rucc_regalloc::run(&mut func, &machine.env, &called);
260
261 let frame = Frame::of(&func, &allocation, &layout);
264 finish(&mut func, &allocation, &frame, &stack, machine.conv, machine.insts, names);
265
266 layout::blocks(&mut func, machine.branch, names, &fusable);
269 Ok(func)
270}
271
272#[cfg(test)]
273mod tests {
274 use rucc_ir::{Builder, Flags as IrFlags, Func, Opcode, Restrict, Signature, Type};
275 use rucc_target::x86_64::{REGS, SYSV, WIN64};
276
277 use super::*;
278
279 fn blank(params: &[Type]) -> (Interner, Func, ir::Block, Vec<ir::Value>) {
281 let mut names = Interner::new();
282 let mut func = Func::new(names.intern("f"), Signature::new());
283 let block = func.create_block();
284 let values = params.iter().map(|&ty| func.append_param(block, ty)).collect();
285 (names, func, block, values)
286 }
287
288 #[test]
289 fn a_function_comes_out_with_no_virtual_register_left_in_it() {
290 let i32 = Type::int(32);
291 let (mut names, mut source, block, args) = blank(&[i32, i32]);
292 let mut build = Builder::new(&mut source, block);
293 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
294 build.ret(&[sum]);
295
296 let machine = Machine::x86_64(&SYSV);
297 let out =
298 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
299 .expect("every instruction has a rule");
300
301 assert_eq!(
306 mir::print_func(&out, &names, ®S),
307 "mfunc @f {\n\
308 block0:\n \
309 $rdi($rdi) = x64.arg_val_32\n \
310 $rsi($rsi) = x64.arg_val_32\n \
311 $rdi(reuse 1) = x64.add_rr_32 $rdi, $rsi\n \
312 $rax = x64.mov_rr_64 $rdi\n \
313 x64.ret_val_32 $rax($rax)\n \
314 x64.ret\n\
315 }\n"
316 );
317 }
318
319 #[test]
323 fn which_rules_lowered_a_function_is_something_the_compilation_can_be_asked_for() {
324 let i32 = Type::int(32);
325 let (mut names, mut source, block, args) = blank(&[i32, i32]);
326 let mut build = Builder::new(&mut source, block);
327 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
328 build.ret(&[sum]);
329
330 let machine = Machine::x86_64(&SYSV);
331 let mut fired = Fired::new();
332 compile_recording(
333 &mut source,
334 &mut names,
335 &machine,
336 &Elsewhere::default(),
337 Flags::default(),
338 &mut fired,
339 )
340 .expect("every instruction has a rule");
341 let one = fired.count();
342 assert!(one > 0, "an add and a return went through the table and nothing was recorded");
343
344 let listing = fired.listing(&crate::select::x86_64::TABLE);
345 assert_eq!(listing.lines().filter(|line| line.starts_with("fired ")).count(), one);
346 assert!(
347 listing.contains(&format!("{one} of ")),
348 "{}",
349 listing.lines().next().unwrap_or("")
350 );
351
352 let (mut names, mut source, block, args) = blank(&[i32, i32]);
354 let mut build = Builder::new(&mut source, block);
355 let difference = build.binary(Opcode::Sub, args[0], args[1], IrFlags::default());
356 build.ret(&[difference]);
357 compile_recording(
358 &mut source,
359 &mut names,
360 &machine,
361 &Elsewhere::default(),
362 Flags::default(),
363 &mut fired,
364 )
365 .expect("every instruction has a rule");
366 assert!(fired.count() > one, "a subtraction is not an addition");
367 }
368
369 #[test]
370 fn a_function_that_calls_takes_a_frame_and_gives_it_back() {
371 let i32 = Type::int(32);
372 let (mut names, mut source, block, args) = blank(&[i32]);
373 let sig = source.add_signature(Signature::new().with_params(&[i32]).with_returns(&[i32]));
374 let callee = names.intern("g");
375 let call = Builder::new(&mut source, block).call(callee, sig, &[args[0]]);
376 let got = source[call].first_result.expect("an integer comes back");
377 let mut build = Builder::new(&mut source, block);
378 let sum = build.binary(Opcode::Add, got, args[0], IrFlags::default());
379 build.ret(&[sum]);
380
381 let machine = Machine::x86_64(&SYSV);
382 let out =
383 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
384 .expect("every instruction has a rule");
385
386 let text = mir::print_func(&out, &names, ®S);
389 assert!(text.contains("x64.push_64 $rbx"), "{text}");
390 assert!(text.contains("$rbx = x64.pop_64"), "{text}");
391 assert!(text.contains("x64.call $rdi($rdi), @g"), "{text}");
392 assert!(!text.contains('%'), "{text}");
393 }
394
395 #[test]
396 fn the_other_convention_is_the_same_function_somewhere_else() {
397 let i32 = Type::int(32);
398 let (mut names, mut source, block, args) = blank(&[i32, i32]);
399 let mut build = Builder::new(&mut source, block);
400 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
401 build.ret(&[sum]);
402
403 let machine = Machine::x86_64(&WIN64);
404 let out =
405 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
406 .expect("every instruction has a rule");
407
408 let text = mir::print_func(&out, &names, ®S);
411 assert!(text.contains("$rcx($rcx) = x64.arg_val_32"), "{text}");
412 assert!(text.contains("$rdx($rdx) = x64.arg_val_32"), "{text}");
413 assert!(!text.contains("$rdi"), "{text}");
414 }
415
416 #[test]
417 fn a_function_with_a_branch_in_it_goes_through_every_pass() {
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 =
431 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
432 .expect("every instruction has a rule");
433
434 assert_eq!(out.block_count(), 4);
438
439 let text = mir::print_func(&out, &names, ®S);
448 assert_eq!(
449 text,
450 "mfunc @f {\n\
451 block0:\n \
452 $rdi($rdi) = x64.arg_val_32\n \
453 $rsi($rsi) = x64.arg_val_32\n \
454 x64.cmp_rr_32 $rdi, $rsi\n \
455 x64.jcc_ge block2, block1\n\
456 \nblock1:\n \
457 $rax = x64.mov_rr_64 $rdi\n \
458 x64.jmp block3\n\
459 \nblock2:\n \
460 $rax = x64.mov_rr_64 $rsi, block3\n\
461 \nblock3:\n \
462 x64.ret_val_32 $rax($rax)\n \
463 x64.ret\n\
464 }\n"
465 );
466 }
467
468 #[test]
474 fn a_loop_that_carries_its_values_round_keeps_all_of_them() {
475 let i32 = Type::int(32);
476 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
477 let head = source.create_block();
478 let body = source.create_block();
479 let exit = source.create_block();
480 let left = source.append_param(head, i32);
481 let right = source.append_param(head, i32);
482 Builder::new(&mut source, entry).jump(head, &[args[0], args[1]]);
483 let mut build = Builder::new(&mut source, head);
484 let zero = build.iconst(i32, 0);
485 let more = build.icmp(rucc_ir::IntPred::Ne, right, zero);
486 build.br_if(more, body, &[], exit, &[left]);
487 let mut build = Builder::new(&mut source, body);
488 let rest = build.binary(Opcode::SRem, left, right, IrFlags::default());
489 build.jump(head, &[right, rest]);
490 let result = source.append_param(exit, i32);
491 Builder::new(&mut source, exit).ret(&[result]);
492
493 let machine = Machine::x86_64(&SYSV);
494 let out =
495 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
496 .expect("every instruction has a rule");
497
498 assert_eq!(
514 mir::print_func(&out, &names, ®S),
515 "mfunc @f {\n\
516 block0:\n \
517 $rdi($rdi) = x64.arg_val_32\n \
518 $rsi($rsi) = x64.arg_val_32\n \
519 $rcx = x64.mov_rr_64 $rdi, block1\n\
520 \nblock1:\n \
521 x64.cmp_ri_32 $rsi, 0\n \
522 x64.jcc_e block3, block2\n\
523 \nblock2:\n \
524 $rax = x64.mov_rr_64 $rcx\n \
525 $rdx($rdx), early $rax($rax) = x64.idiv_rem_32 $rax($rax), $rsi\n \
526 $rdi = x64.mov_rr_64 $rax\n \
527 $rcx = x64.mov_rr_64 $rsi\n \
528 $rsi = x64.mov_rr_64 $rdx\n \
529 x64.jmp block1\n\
530 \nblock3:\n \
531 $rax = x64.mov_rr_64 $rcx\n \
532 x64.ret_val_32 $rax($rax)\n \
533 x64.ret\n\
534 }\n"
535 );
536 }
537
538 #[test]
543 fn a_function_that_has_been_laid_out_reads_back_as_the_same_function() {
544 let i32 = Type::int(32);
545 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
546 let then = source.create_block();
547 let join = source.create_block();
548 let got = source.append_param(join, i32);
549 let mut build = Builder::new(&mut source, entry);
550 let cond = build.icmp(rucc_ir::IntPred::Slt, args[0], args[1]);
551 build.br_if(cond, then, &[], join, &[args[1]]);
552 Builder::new(&mut source, then).jump(join, &[args[0]]);
553 Builder::new(&mut source, join).ret(&[got]);
554
555 let machine = Machine::x86_64(&SYSV);
556 let out =
557 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
558 .expect("every instruction has a rule");
559
560 let text = mir::print_func(&out, &names, ®S);
561 let read = rucc_mir::parse(&text, &mut names, ®S).expect("what the printer wrote");
562 assert_eq!(mir::print(&read, &names, ®S), text);
563 }
564
565 #[test]
566 fn a_function_this_cannot_lower_is_reported_rather_than_compiled() {
567 let f80 = Type::float(rucc_ir::Float::F80);
568 let (mut names, mut source, block, args) = blank(&[f80, Type::int(64)]);
569 Builder::new(&mut source, block).ret(&args);
570
571 let machine = Machine::x86_64(&SYSV);
575 let failed =
576 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
577 .expect_err("a long double cannot come back beside another value");
578 assert_eq!(failed.to_string(), "what this function gives back is on the x87 stack");
579 }
580
581 #[test]
589 fn a_long_double_arrives_in_memory_and_goes_back_on_the_x87_stack() {
590 let f80 = Type::float(rucc_ir::Float::F80);
591 let (mut names, mut source, block, args) = blank(&[f80, f80]);
592 let mut build = Builder::new(&mut source, block);
593 let sum = build.binary(Opcode::FAdd, args[0], args[1], IrFlags::default());
594 build.ret(&[sum]);
595
596 let machine = Machine::x86_64(&SYSV);
597 let out =
598 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
599 .expect("every instruction has a rule");
600
601 let text = mir::print_func(&out, &names, ®S);
602 assert!(text.contains("x64.lea_64 [$rsp + 32]"), "{text}");
605 assert!(text.contains("x64.lea_64 [$rsp + 48]"), "{text}");
606 assert!(!text.contains("x64.ret_val"), "nothing comes back in a register: {text}");
607 let end: Vec<&str> = text.lines().rev().skip(1).take(3).map(str::trim).collect();
610 assert_eq!(end, ["x64.ret", "$rsp = x64.add_ri_64 $rsp, 24", "x64.fld_t [$rax]"], "{text}");
611 }
612
613 #[test]
617 fn a_float_is_added_in_the_register_file_it_arrives_in() {
618 let f32 = Type::float(rucc_ir::Float::F32);
619 let (mut names, mut source, block, args) = blank(&[f32, f32]);
620 let mut build = Builder::new(&mut source, block);
621 let sum = build.binary(Opcode::FAdd, args[0], args[1], ir::Flags::default());
622 build.ret(&[sum]);
623
624 let machine = Machine::x86_64(&SYSV);
625 let out =
626 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
627 .expect("every instruction has a rule");
628
629 let text = mir::print_func(&out, &names, ®S);
630 assert!(text.contains("x64.addss_rr"), "{text}");
631 assert!(text.contains("$xmm0"), "{text}");
632 assert!(!text.contains("$rax"), "{text}");
633 }
634
635 #[test]
638 fn a_float_read_from_memory_and_written_back_uses_the_scalar_moves() {
639 let f64 = Type::float(rucc_ir::Float::F64);
640 let (mut names, mut source, block, args) = blank(&[Type::PTR, f64]);
641 let mut build = Builder::new(&mut source, block);
642 let info = rucc_ir::MemInfo {
643 size: 8,
644 align: 8,
645 order: rucc_ir::MemOrder::NotAtomic,
646 tbaa: None,
647 restrict: Restrict::NONE,
648 };
649 let read = build.load(f64, args[0], info, ir::Flags::default());
650 let sum = build.binary(Opcode::FAdd, read, args[1], ir::Flags::default());
651 build.store(sum, args[0], info, ir::Flags::default());
652 build.ret(&[sum]);
653
654 let machine = Machine::x86_64(&SYSV);
655 let out =
656 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
657 .expect("every instruction has a rule");
658
659 let text = mir::print_func(&out, &names, ®S);
660 assert!(text.contains("x64.movsd_rm"), "{text}");
661 assert!(text.contains("x64.movsd_mr"), "{text}");
662 assert!(!text.contains("x64.movaps_rm"), "{text}");
665 assert!(!text.contains("x64.movaps_mr"), "{text}");
666 }
667
668 #[test]
676 fn an_unsigned_word_and_a_long_double_convert_into_each_other() {
677 let f80 = Type::float(rucc_ir::Float::F80);
678 let (mut names, mut source, block, args) = blank(&[Type::PTR, Type::int(64)]);
679 let mut build = Builder::new(&mut source, block);
680 let info = rucc_ir::MemInfo {
681 size: 16,
682 align: 16,
683 order: rucc_ir::MemOrder::NotAtomic,
684 tbaa: None,
685 restrict: Restrict::NONE,
686 };
687 let wide = build.unary(Opcode::UIToFP, args[1], f80);
688 build.store(wide, args[0], info, ir::Flags::default());
689 let read = build.load(f80, args[0], info, ir::Flags::default());
690 let back = build.unary(Opcode::FPToUI, read, Type::int(64));
691 build.ret(&[back]);
692
693 let machine = Machine::x86_64(&SYSV);
694 let out =
695 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
696 .expect("every instruction has a rule");
697
698 let text = mir::print_func(&out, &names, ®S);
699 assert!(text.contains("x64.fild_ll"), "the integer goes in as a signed one: {text}");
702 assert!(text.contains("x64.fistp_ll"), "and comes back out as one: {text}");
703 assert!(text.contains("x64.fmul_p"), "the correction is taken or not: {text}");
704 assert!(text.contains("x64.fadd_p"), "and applied one way: {text}");
705 assert!(text.contains("x64.fsubr_p"), "and the other: {text}");
706 assert!(!text.contains("xmm"), "no part of this is in a vector register: {text}");
707 }
708
709 #[test]
714 fn a_conversion_carries_the_value_into_the_other_register_file() {
715 let f64 = Type::float(rucc_ir::Float::F64);
716 let (mut names, mut source, block, args) = blank(&[f64]);
717 let mut build = Builder::new(&mut source, block);
718 let whole = build.unary(Opcode::FPToSI, args[0], Type::int(32));
719 let back = build.unary(Opcode::SIToFP, whole, f64);
720 build.ret(&[back]);
721
722 let machine = Machine::x86_64(&SYSV);
723 let out =
724 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
725 .expect("every instruction has a rule");
726
727 let text = mir::print_func(&out, &names, ®S);
730 assert!(text.contains("x64.cvttsd2si_32"), "{text}");
731 assert!(text.contains("x64.cvtsi2sd_32"), "{text}");
732 assert!(text.contains("$xmm0"), "{text}");
733 }
734
735 #[test]
738 fn a_bitcast_between_the_files_is_the_move_that_changes_no_bit() {
739 let f64 = Type::float(rucc_ir::Float::F64);
740 let (mut names, mut source, block, args) = blank(&[f64]);
741 let mut build = Builder::new(&mut source, block);
742 let bits = build.unary(Opcode::Bitcast, args[0], Type::int(64));
743 build.ret(&[bits]);
744
745 let machine = Machine::x86_64(&SYSV);
746 let out =
747 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
748 .expect("every instruction has a rule");
749
750 let text = mir::print_func(&out, &names, ®S);
751 assert!(text.contains("x64.movq_from_xmm"), "{text}");
752 assert!(!text.contains("cvt"), "{text}");
753 }
754
755 #[test]
757 fn a_float_comparison_is_the_compare_and_the_byte_a_condition_sets() {
758 let f64 = Type::float(rucc_ir::Float::F64);
759 let (mut names, mut source, block, args) = blank(&[f64, f64]);
760 let mut build = Builder::new(&mut source, block);
761 let less = build.fcmp(rucc_ir::FloatPred::Olt, args[0], args[1], ir::Flags::default());
762 let wide = build.unary(Opcode::ZExt, less, Type::int(32));
763 build.ret(&[wide]);
764
765 let machine = Machine::x86_64(&SYSV);
766 let out =
767 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
768 .expect("every instruction has a rule");
769
770 let text = mir::print_func(&out, &names, ®S);
773 assert!(text.contains("x64.ucomisd_set_a"), "{text}");
774 }
775
776 #[test]
781 fn an_equality_between_floats_gets_a_register_for_the_byte_it_needs_twice() {
782 let f64 = Type::float(rucc_ir::Float::F64);
783 let (mut names, mut source, block, args) = blank(&[f64, f64]);
784 let mut build = Builder::new(&mut source, block);
785 let same = build.fcmp(rucc_ir::FloatPred::Oeq, args[0], args[1], ir::Flags::default());
786 let wide = build.unary(Opcode::ZExt, same, Type::int(32));
787 build.ret(&[wide]);
788
789 let machine = Machine::x86_64(&SYSV);
790 let out =
791 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
792 .expect("every instruction has a rule");
793
794 let text = mir::print_func(&out, &names, ®S);
795 let line = text
796 .lines()
797 .find(|line| line.contains("x64.ucomisd_set_e_and_np"))
798 .expect("the rule for an ordered equality fired");
799 let written: Vec<&str> = line
800 .split_once('=')
801 .expect("the instruction writes something")
802 .0
803 .split(',')
804 .map(str::trim)
805 .collect();
806 assert_eq!(written.len(), 2, "{line}");
807 assert_ne!(written[0], written[1], "{line}");
808 }
809
810 #[test]
814 fn a_float_constant_is_the_bits_in_a_register_and_the_move_that_carries_them_over() {
815 let f64 = Type::float(rucc_ir::Float::F64);
816 let (mut names, mut source, block, _) = blank(&[]);
817 let mut build = Builder::new(&mut source, block);
818 let half = build.fconst(f64, 0x3fe0_0000_0000_0000);
819 build.ret(&[half]);
820
821 let machine = Machine::x86_64(&SYSV);
822 let out =
823 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
824 .expect("every instruction has a rule");
825
826 let text = mir::print_func(&out, &names, ®S);
827 assert!(text.contains("x64.mov_ri_64"), "{text}");
828 assert!(text.contains("x64.movq_to_xmm"), "{text}");
829 }
830
831 #[test]
834 fn a_negation_is_the_sign_bit_flipped_and_no_float_instruction_at_all() {
835 let f64 = Type::float(rucc_ir::Float::F64);
836 let (mut names, mut source, block, args) = blank(&[f64]);
837 let mut build = Builder::new(&mut source, block);
838 let less = build.unary(Opcode::FNeg, args[0], f64);
839 build.ret(&[less]);
840
841 let machine = Machine::x86_64(&SYSV);
842 let out =
843 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
844 .expect("every instruction has a rule");
845
846 let text = mir::print_func(&out, &names, ®S);
847 assert!(text.contains("x64.xor_rr_64"), "{text}");
848 assert!(!text.contains("sub"), "a negation is not a subtraction: {text}");
849 }
850
851 #[test]
852 fn the_flags_reach_the_frame() {
853 let i32 = Type::int(32);
854 let (mut names, mut source, block, args) = blank(&[i32]);
855 Builder::new(&mut source, block).ret(&[args[0]]);
856
857 let machine = Machine::x86_64(&SYSV);
858 let flags = Flags { frame_pointer: true, red_zone: true };
859 let out = compile(&mut source, &mut names, &machine, &Elsewhere::default(), flags)
860 .expect("every instruction has a rule");
861
862 let text = mir::print_func(&out, &names, ®S);
865 assert!(text.contains("x64.push_64 $rbp"), "{text}");
866 assert!(text.contains("$rbp = x64.mov_rr_64 $rsp"), "{text}");
867 }
868
869 #[test]
870 fn a_target_says_which_machine_it_is_and_which_convention_it_uses() {
871 let triple = |text: &str| text.parse::<rucc_target::Triple>().expect("a triple");
872 let info = TargetInfo::new(triple("x86_64-unknown-linux-gnu"));
873 let machine = Machine::for_target(&info).expect("x86-64 is the target this crate covers");
874 assert!(std::ptr::eq(machine.conv, &SYSV));
875
876 let info = TargetInfo::new(triple("x86_64-pc-windows-msvc"));
877 let machine = Machine::for_target(&info).expect("x86-64 is the target this crate covers");
878 assert!(std::ptr::eq(machine.conv, &WIN64));
879
880 let info = TargetInfo::new(triple("aarch64-unknown-linux-gnu"));
883 assert!(Machine::for_target(&info).is_none());
884 }
885}