1use rucc_base::Interner;
27use rucc_ir as ir;
28use rucc_mir as mir;
29use rucc_regalloc::assign::Env;
30use rucc_target::{BranchInsts, CallRegs, FrameInsts, PhysReg, RegFile, TargetInfo, x86_64};
31use rucc_tuple::Arch;
32
33use crate::coverage::Fired;
34use crate::elsewhere::Elsewhere;
35use crate::expand;
36use crate::finish::finish;
37use crate::frame::{Frame, Layout};
38use crate::layout;
39use crate::lower::{self, Unsupported};
40use crate::retry;
41use crate::split;
42use crate::switch;
43use crate::varargs;
44use crate::widths;
45
46#[derive(Debug)]
55pub struct Machine {
56 pub conv: &'static CallRegs,
58 pub file: RegFile,
60 pub insts: &'static FrameInsts,
62 pub branch: &'static BranchInsts,
64 pub env: Env,
66}
67
68const SCRATCH: [PhysReg; 2] = [x86_64::R10, x86_64::R11];
75
76const SCRATCH_COUNT: usize = SCRATCH.len();
78
79impl Machine {
80 #[must_use]
87 pub fn x86_64(conv: &'static CallRegs) -> Self {
88 let order: Vec<PhysReg> =
89 conv.int_order.iter().copied().filter(|reg| !SCRATCH.contains(reg)).collect();
90 let free: Vec<PhysReg> =
97 conv.sse_order.iter().copied().filter(|®| !conv.preserves_sse(reg)).collect();
98 let at = free.len().saturating_sub(SCRATCH_COUNT);
99 let sse_scratch: Vec<PhysReg> = free[at..].to_vec();
100 let sse_order: Vec<PhysReg> =
101 conv.sse_order.iter().copied().filter(|reg| !sse_scratch.contains(reg)).collect();
102 Self {
103 conv,
104 file: x86_64::REGS,
105 insts: &x86_64::FRAME,
106 branch: &x86_64::BRANCH,
107 env: Env::new().with(x86_64::GPR, &order, &SCRATCH).with(
108 x86_64::XMM,
109 &sse_order,
110 &sse_scratch,
111 ),
112 }
113 }
114
115 #[must_use]
122 pub fn for_target(target: &TargetInfo) -> Option<Self> {
123 let conv = target.call_regs?;
124 match target.tuple.arch() {
125 Arch::X86_64 => Some(Self::x86_64(conv)),
126 _ => None,
127 }
128 }
129}
130
131#[derive(Debug, Clone, Copy, PartialEq, Eq)]
133pub struct Flags {
134 pub frame_pointer: bool,
136 pub red_zone: bool,
138}
139
140impl Default for Flags {
141 fn default() -> Self {
144 Self { frame_pointer: false, red_zone: true }
145 }
146}
147
148pub fn compile(
167 source: &mut ir::Func,
168 names: &mut Interner,
169 machine: &Machine,
170 elsewhere: &Elsewhere,
171 flags: Flags,
172) -> Result<mir::Func, Unsupported> {
173 compile_recording(source, names, machine, elsewhere, flags, &mut Fired::new())
174}
175
176pub fn compile_recording(
190 source: &mut ir::Func,
191 names: &mut Interner,
192 machine: &Machine,
193 elsewhere: &Elsewhere,
194 flags: Flags,
195 fired: &mut Fired,
196) -> Result<mir::Func, Unsupported> {
197 switch::switches(source);
198 retry::loops(source);
203 expand::orderings(source, machine.conv.word);
206 widths::integers(source);
209 expand::bytes(source);
210 expand::counts(source);
211 expand::overflows(source);
212 expand::floats(source);
213 expand::bulk(source, names, machine.conv.word);
214 varargs::lists(source, machine.conv);
215 let lowered = lower::func(source, names, machine.conv, elsewhere)?;
216 fired.merge(&lowered.fired);
217 let lower::Lowered { mut func, stack, .. } = lowered;
218 let layout = Layout {
219 frame_pointer: flags.frame_pointer,
220 red_zone: flags.red_zone,
221 ..stack.layout(Layout::new(machine.conv, machine.file))
222 };
223
224 split::critical(&mut func);
228 let called = names.resolve(func.name).to_owned();
229 let allocation = rucc_regalloc::run(&mut func, &machine.env, &called);
230
231 let frame = Frame::of(&func, &allocation, &layout);
234 finish(&mut func, &allocation, &frame, &stack, machine.conv, machine.insts, names);
235
236 layout::blocks(&mut func, machine.branch, names);
239 Ok(func)
240}
241
242#[cfg(test)]
243mod tests {
244 use rucc_ir::{Builder, Flags as IrFlags, Func, Opcode, Restrict, Signature, Type};
245 use rucc_target::x86_64::{REGS, SYSV, WIN64};
246
247 use super::*;
248
249 fn blank(params: &[Type]) -> (Interner, Func, ir::Block, Vec<ir::Value>) {
251 let mut names = Interner::new();
252 let mut func = Func::new(names.intern("f"), Signature::new());
253 let block = func.create_block();
254 let values = params.iter().map(|&ty| func.append_param(block, ty)).collect();
255 (names, func, block, values)
256 }
257
258 #[test]
259 fn a_function_comes_out_with_no_virtual_register_left_in_it() {
260 let i32 = Type::int(32);
261 let (mut names, mut source, block, args) = blank(&[i32, i32]);
262 let mut build = Builder::new(&mut source, block);
263 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
264 build.ret(&[sum]);
265
266 let machine = Machine::x86_64(&SYSV);
267 let out =
268 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
269 .expect("every instruction has a rule");
270
271 assert_eq!(
276 mir::print_func(&out, &names, ®S),
277 "mfunc @f {\n\
278 block0:\n \
279 $rdi($rdi) = x64.arg_val_32\n \
280 $rsi($rsi) = x64.arg_val_32\n \
281 $rdi(reuse 1) = x64.add_rr_32 $rdi, $rsi\n \
282 $rax = x64.mov_rr_64 $rdi\n \
283 x64.ret_val_32 $rax($rax)\n \
284 x64.ret\n\
285 }\n"
286 );
287 }
288
289 #[test]
293 fn which_rules_lowered_a_function_is_something_the_compilation_can_be_asked_for() {
294 let i32 = Type::int(32);
295 let (mut names, mut source, block, args) = blank(&[i32, i32]);
296 let mut build = Builder::new(&mut source, block);
297 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
298 build.ret(&[sum]);
299
300 let machine = Machine::x86_64(&SYSV);
301 let mut fired = Fired::new();
302 compile_recording(
303 &mut source,
304 &mut names,
305 &machine,
306 &Elsewhere::default(),
307 Flags::default(),
308 &mut fired,
309 )
310 .expect("every instruction has a rule");
311 let one = fired.count();
312 assert!(one > 0, "an add and a return went through the table and nothing was recorded");
313
314 let listing = fired.listing(&crate::select::x86_64::TABLE);
315 assert_eq!(listing.lines().filter(|line| line.starts_with("fired ")).count(), one);
316 assert!(
317 listing.contains(&format!("{one} of ")),
318 "{}",
319 listing.lines().next().unwrap_or("")
320 );
321
322 let (mut names, mut source, block, args) = blank(&[i32, i32]);
324 let mut build = Builder::new(&mut source, block);
325 let difference = build.binary(Opcode::Sub, args[0], args[1], IrFlags::default());
326 build.ret(&[difference]);
327 compile_recording(
328 &mut source,
329 &mut names,
330 &machine,
331 &Elsewhere::default(),
332 Flags::default(),
333 &mut fired,
334 )
335 .expect("every instruction has a rule");
336 assert!(fired.count() > one, "a subtraction is not an addition");
337 }
338
339 #[test]
340 fn a_function_that_calls_takes_a_frame_and_gives_it_back() {
341 let i32 = Type::int(32);
342 let (mut names, mut source, block, args) = blank(&[i32]);
343 let sig = source.add_signature(Signature::new().with_params(&[i32]).with_returns(&[i32]));
344 let callee = names.intern("g");
345 let call = Builder::new(&mut source, block).call(callee, sig, &[args[0]]);
346 let got = source[call].first_result.expect("an integer comes back");
347 let mut build = Builder::new(&mut source, block);
348 let sum = build.binary(Opcode::Add, got, args[0], IrFlags::default());
349 build.ret(&[sum]);
350
351 let machine = Machine::x86_64(&SYSV);
352 let out =
353 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
354 .expect("every instruction has a rule");
355
356 let text = mir::print_func(&out, &names, ®S);
359 assert!(text.contains("x64.push_64 $rbx"), "{text}");
360 assert!(text.contains("$rbx = x64.pop_64"), "{text}");
361 assert!(text.contains("x64.call $rdi($rdi), @g"), "{text}");
362 assert!(!text.contains('%'), "{text}");
363 }
364
365 #[test]
366 fn the_other_convention_is_the_same_function_somewhere_else() {
367 let i32 = Type::int(32);
368 let (mut names, mut source, block, args) = blank(&[i32, i32]);
369 let mut build = Builder::new(&mut source, block);
370 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
371 build.ret(&[sum]);
372
373 let machine = Machine::x86_64(&WIN64);
374 let out =
375 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
376 .expect("every instruction has a rule");
377
378 let text = mir::print_func(&out, &names, ®S);
381 assert!(text.contains("$rcx($rcx) = x64.arg_val_32"), "{text}");
382 assert!(text.contains("$rdx($rdx) = x64.arg_val_32"), "{text}");
383 assert!(!text.contains("$rdi"), "{text}");
384 }
385
386 #[test]
387 fn a_function_with_a_branch_in_it_goes_through_every_pass() {
388 let i32 = Type::int(32);
389 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
390 let then = source.create_block();
391 let join = source.create_block();
392 let got = source.append_param(join, i32);
393 let mut build = Builder::new(&mut source, entry);
394 let cond = build.icmp(rucc_ir::IntPred::Slt, args[0], args[1]);
395 build.br_if(cond, then, &[], join, &[args[1]]);
396 Builder::new(&mut source, then).jump(join, &[args[0]]);
397 Builder::new(&mut source, join).ret(&[got]);
398
399 let machine = Machine::x86_64(&SYSV);
400 let out =
401 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
402 .expect("every instruction has a rule");
403
404 assert_eq!(out.block_count(), 4);
408
409 let text = mir::print_func(&out, &names, ®S);
418 assert_eq!(
419 text,
420 "mfunc @f {\n\
421 block0:\n \
422 $rdi($rdi) = x64.arg_val_32\n \
423 $rsi($rsi) = x64.arg_val_32\n \
424 $rax = x64.cmp_set_l_32 $rdi, $rsi\n \
425 x64.test_rr_8 $rax\n \
426 x64.jcc_e block2, block1\n\
427 \nblock1:\n \
428 $rax = x64.mov_rr_64 $rdi\n \
429 x64.jmp block3\n\
430 \nblock2:\n \
431 $rax = x64.mov_rr_64 $rsi, block3\n\
432 \nblock3:\n \
433 x64.ret_val_32 $rax($rax)\n \
434 x64.ret\n\
435 }\n"
436 );
437 }
438
439 #[test]
445 fn a_loop_that_carries_its_values_round_keeps_all_of_them() {
446 let i32 = Type::int(32);
447 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
448 let head = source.create_block();
449 let body = source.create_block();
450 let exit = source.create_block();
451 let left = source.append_param(head, i32);
452 let right = source.append_param(head, i32);
453 Builder::new(&mut source, entry).jump(head, &[args[0], args[1]]);
454 let mut build = Builder::new(&mut source, head);
455 let zero = build.iconst(i32, 0);
456 let more = build.icmp(rucc_ir::IntPred::Ne, right, zero);
457 build.br_if(more, body, &[], exit, &[left]);
458 let mut build = Builder::new(&mut source, body);
459 let rest = build.binary(Opcode::SRem, left, right, IrFlags::default());
460 build.jump(head, &[right, rest]);
461 let result = source.append_param(exit, i32);
462 Builder::new(&mut source, exit).ret(&[result]);
463
464 let machine = Machine::x86_64(&SYSV);
465 let out =
466 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
467 .expect("every instruction has a rule");
468
469 assert_eq!(
485 mir::print_func(&out, &names, ®S),
486 "mfunc @f {\n\
487 block0:\n \
488 $rdi($rdi) = x64.arg_val_32\n \
489 $rsi($rsi) = x64.arg_val_32\n \
490 $rcx = x64.mov_rr_64 $rdi, block1\n\
491 \nblock1:\n \
492 $rax = x64.mov_ri_32 0\n \
493 $rax = x64.cmp_set_ne_32 $rsi, $rax\n \
494 x64.test_rr_8 $rax\n \
495 x64.jcc_e block3, block2\n\
496 \nblock2:\n \
497 $rax = x64.mov_rr_64 $rcx\n \
498 $rdx($rdx), early $rax($rax) = x64.idiv_rem_32 $rax($rax), $rsi\n \
499 $rdi = x64.mov_rr_64 $rax\n \
500 $rcx = x64.mov_rr_64 $rsi\n \
501 $rsi = x64.mov_rr_64 $rdx\n \
502 x64.jmp block1\n\
503 \nblock3:\n \
504 $rax = x64.mov_rr_64 $rcx\n \
505 x64.ret_val_32 $rax($rax)\n \
506 x64.ret\n\
507 }\n"
508 );
509 }
510
511 #[test]
516 fn a_function_that_has_been_laid_out_reads_back_as_the_same_function() {
517 let i32 = Type::int(32);
518 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
519 let then = source.create_block();
520 let join = source.create_block();
521 let got = source.append_param(join, i32);
522 let mut build = Builder::new(&mut source, entry);
523 let cond = build.icmp(rucc_ir::IntPred::Slt, args[0], args[1]);
524 build.br_if(cond, then, &[], join, &[args[1]]);
525 Builder::new(&mut source, then).jump(join, &[args[0]]);
526 Builder::new(&mut source, join).ret(&[got]);
527
528 let machine = Machine::x86_64(&SYSV);
529 let out =
530 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
531 .expect("every instruction has a rule");
532
533 let text = mir::print_func(&out, &names, ®S);
534 let read = rucc_mir::parse(&text, &mut names, ®S).expect("what the printer wrote");
535 assert_eq!(mir::print(&read, &names, ®S), text);
536 }
537
538 #[test]
539 fn a_function_this_cannot_lower_is_reported_rather_than_compiled() {
540 let f80 = Type::float(rucc_ir::Float::F80);
541 let (mut names, mut source, block, args) = blank(&[f80, Type::int(64)]);
542 Builder::new(&mut source, block).ret(&args);
543
544 let machine = Machine::x86_64(&SYSV);
548 let failed =
549 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
550 .expect_err("a long double cannot come back beside another value");
551 assert_eq!(failed.to_string(), "what this function gives back is on the x87 stack");
552 }
553
554 #[test]
562 fn a_long_double_arrives_in_memory_and_goes_back_on_the_x87_stack() {
563 let f80 = Type::float(rucc_ir::Float::F80);
564 let (mut names, mut source, block, args) = blank(&[f80, f80]);
565 let mut build = Builder::new(&mut source, block);
566 let sum = build.binary(Opcode::FAdd, args[0], args[1], IrFlags::default());
567 build.ret(&[sum]);
568
569 let machine = Machine::x86_64(&SYSV);
570 let out =
571 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
572 .expect("every instruction has a rule");
573
574 let text = mir::print_func(&out, &names, ®S);
575 assert!(text.contains("x64.lea_64 [$rsp + 32]"), "{text}");
578 assert!(text.contains("x64.lea_64 [$rsp + 48]"), "{text}");
579 assert!(!text.contains("x64.ret_val"), "nothing comes back in a register: {text}");
580 let end: Vec<&str> = text.lines().rev().skip(1).take(3).map(str::trim).collect();
583 assert_eq!(end, ["x64.ret", "$rsp = x64.add_ri_64 $rsp, 24", "x64.fld_t [$rax]"], "{text}");
584 }
585
586 #[test]
590 fn a_float_is_added_in_the_register_file_it_arrives_in() {
591 let f32 = Type::float(rucc_ir::Float::F32);
592 let (mut names, mut source, block, args) = blank(&[f32, f32]);
593 let mut build = Builder::new(&mut source, block);
594 let sum = build.binary(Opcode::FAdd, args[0], args[1], ir::Flags::default());
595 build.ret(&[sum]);
596
597 let machine = Machine::x86_64(&SYSV);
598 let out =
599 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
600 .expect("every instruction has a rule");
601
602 let text = mir::print_func(&out, &names, ®S);
603 assert!(text.contains("x64.addss_rr"), "{text}");
604 assert!(text.contains("$xmm0"), "{text}");
605 assert!(!text.contains("$rax"), "{text}");
606 }
607
608 #[test]
611 fn a_float_read_from_memory_and_written_back_uses_the_scalar_moves() {
612 let f64 = Type::float(rucc_ir::Float::F64);
613 let (mut names, mut source, block, args) = blank(&[Type::PTR, f64]);
614 let mut build = Builder::new(&mut source, block);
615 let info = rucc_ir::MemInfo {
616 size: 8,
617 align: 8,
618 order: rucc_ir::MemOrder::NotAtomic,
619 tbaa: None,
620 restrict: Restrict::NONE,
621 };
622 let read = build.load(f64, args[0], info, ir::Flags::default());
623 let sum = build.binary(Opcode::FAdd, read, args[1], ir::Flags::default());
624 build.store(sum, args[0], info, ir::Flags::default());
625 build.ret(&[sum]);
626
627 let machine = Machine::x86_64(&SYSV);
628 let out =
629 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
630 .expect("every instruction has a rule");
631
632 let text = mir::print_func(&out, &names, ®S);
633 assert!(text.contains("x64.movsd_rm"), "{text}");
634 assert!(text.contains("x64.movsd_mr"), "{text}");
635 assert!(!text.contains("x64.movaps_rm"), "{text}");
638 assert!(!text.contains("x64.movaps_mr"), "{text}");
639 }
640
641 #[test]
649 fn an_unsigned_word_and_a_long_double_convert_into_each_other() {
650 let f80 = Type::float(rucc_ir::Float::F80);
651 let (mut names, mut source, block, args) = blank(&[Type::PTR, Type::int(64)]);
652 let mut build = Builder::new(&mut source, block);
653 let info = rucc_ir::MemInfo {
654 size: 16,
655 align: 16,
656 order: rucc_ir::MemOrder::NotAtomic,
657 tbaa: None,
658 restrict: Restrict::NONE,
659 };
660 let wide = build.unary(Opcode::UIToFP, args[1], f80);
661 build.store(wide, args[0], info, ir::Flags::default());
662 let read = build.load(f80, args[0], info, ir::Flags::default());
663 let back = build.unary(Opcode::FPToUI, read, Type::int(64));
664 build.ret(&[back]);
665
666 let machine = Machine::x86_64(&SYSV);
667 let out =
668 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
669 .expect("every instruction has a rule");
670
671 let text = mir::print_func(&out, &names, ®S);
672 assert!(text.contains("x64.fild_ll"), "the integer goes in as a signed one: {text}");
675 assert!(text.contains("x64.fistp_ll"), "and comes back out as one: {text}");
676 assert!(text.contains("x64.fmul_p"), "the correction is taken or not: {text}");
677 assert!(text.contains("x64.fadd_p"), "and applied one way: {text}");
678 assert!(text.contains("x64.fsubr_p"), "and the other: {text}");
679 assert!(!text.contains("xmm"), "no part of this is in a vector register: {text}");
680 }
681
682 #[test]
687 fn a_conversion_carries_the_value_into_the_other_register_file() {
688 let f64 = Type::float(rucc_ir::Float::F64);
689 let (mut names, mut source, block, args) = blank(&[f64]);
690 let mut build = Builder::new(&mut source, block);
691 let whole = build.unary(Opcode::FPToSI, args[0], Type::int(32));
692 let back = build.unary(Opcode::SIToFP, whole, f64);
693 build.ret(&[back]);
694
695 let machine = Machine::x86_64(&SYSV);
696 let out =
697 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
698 .expect("every instruction has a rule");
699
700 let text = mir::print_func(&out, &names, ®S);
703 assert!(text.contains("x64.cvttsd2si_32"), "{text}");
704 assert!(text.contains("x64.cvtsi2sd_32"), "{text}");
705 assert!(text.contains("$xmm0"), "{text}");
706 }
707
708 #[test]
711 fn a_bitcast_between_the_files_is_the_move_that_changes_no_bit() {
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 bits = build.unary(Opcode::Bitcast, args[0], Type::int(64));
716 build.ret(&[bits]);
717
718 let machine = Machine::x86_64(&SYSV);
719 let out =
720 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
721 .expect("every instruction has a rule");
722
723 let text = mir::print_func(&out, &names, ®S);
724 assert!(text.contains("x64.movq_from_xmm"), "{text}");
725 assert!(!text.contains("cvt"), "{text}");
726 }
727
728 #[test]
730 fn a_float_comparison_is_the_compare_and_the_byte_a_condition_sets() {
731 let f64 = Type::float(rucc_ir::Float::F64);
732 let (mut names, mut source, block, args) = blank(&[f64, f64]);
733 let mut build = Builder::new(&mut source, block);
734 let less = build.fcmp(rucc_ir::FloatPred::Olt, args[0], args[1], ir::Flags::default());
735 let wide = build.unary(Opcode::ZExt, less, Type::int(32));
736 build.ret(&[wide]);
737
738 let machine = Machine::x86_64(&SYSV);
739 let out =
740 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
741 .expect("every instruction has a rule");
742
743 let text = mir::print_func(&out, &names, ®S);
746 assert!(text.contains("x64.ucomisd_set_a"), "{text}");
747 }
748
749 #[test]
754 fn an_equality_between_floats_gets_a_register_for_the_byte_it_needs_twice() {
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 same = build.fcmp(rucc_ir::FloatPred::Oeq, args[0], args[1], ir::Flags::default());
759 let wide = build.unary(Opcode::ZExt, same, 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);
768 let line = text
769 .lines()
770 .find(|line| line.contains("x64.ucomisd_set_e_and_np"))
771 .expect("the rule for an ordered equality fired");
772 let written: Vec<&str> = line
773 .split_once('=')
774 .expect("the instruction writes something")
775 .0
776 .split(',')
777 .map(str::trim)
778 .collect();
779 assert_eq!(written.len(), 2, "{line}");
780 assert_ne!(written[0], written[1], "{line}");
781 }
782
783 #[test]
787 fn a_float_constant_is_the_bits_in_a_register_and_the_move_that_carries_them_over() {
788 let f64 = Type::float(rucc_ir::Float::F64);
789 let (mut names, mut source, block, _) = blank(&[]);
790 let mut build = Builder::new(&mut source, block);
791 let half = build.fconst(f64, 0x3fe0_0000_0000_0000);
792 build.ret(&[half]);
793
794 let machine = Machine::x86_64(&SYSV);
795 let out =
796 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
797 .expect("every instruction has a rule");
798
799 let text = mir::print_func(&out, &names, ®S);
800 assert!(text.contains("x64.mov_ri_64"), "{text}");
801 assert!(text.contains("x64.movq_to_xmm"), "{text}");
802 }
803
804 #[test]
807 fn a_negation_is_the_sign_bit_flipped_and_no_float_instruction_at_all() {
808 let f64 = Type::float(rucc_ir::Float::F64);
809 let (mut names, mut source, block, args) = blank(&[f64]);
810 let mut build = Builder::new(&mut source, block);
811 let less = build.unary(Opcode::FNeg, args[0], f64);
812 build.ret(&[less]);
813
814 let machine = Machine::x86_64(&SYSV);
815 let out =
816 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
817 .expect("every instruction has a rule");
818
819 let text = mir::print_func(&out, &names, ®S);
820 assert!(text.contains("x64.xor_rr_64"), "{text}");
821 assert!(!text.contains("sub"), "a negation is not a subtraction: {text}");
822 }
823
824 #[test]
825 fn the_flags_reach_the_frame() {
826 let i32 = Type::int(32);
827 let (mut names, mut source, block, args) = blank(&[i32]);
828 Builder::new(&mut source, block).ret(&[args[0]]);
829
830 let machine = Machine::x86_64(&SYSV);
831 let flags = Flags { frame_pointer: true, red_zone: true };
832 let out = compile(&mut source, &mut names, &machine, &Elsewhere::default(), flags)
833 .expect("every instruction has a rule");
834
835 let text = mir::print_func(&out, &names, ®S);
838 assert!(text.contains("x64.push_64 $rbp"), "{text}");
839 assert!(text.contains("$rbp = x64.mov_rr_64 $rsp"), "{text}");
840 }
841
842 #[test]
843 fn a_target_says_which_machine_it_is_and_which_convention_it_uses() {
844 let triple = |text: &str| text.parse::<rucc_target::Triple>().expect("a triple");
845 let info = TargetInfo::new(triple("x86_64-unknown-linux-gnu"));
846 let machine = Machine::for_target(&info).expect("x86-64 is the target this crate covers");
847 assert!(std::ptr::eq(machine.conv, &SYSV));
848
849 let info = TargetInfo::new(triple("x86_64-pc-windows-msvc"));
850 let machine = Machine::for_target(&info).expect("x86-64 is the target this crate covers");
851 assert!(std::ptr::eq(machine.conv, &WIN64));
852
853 let info = TargetInfo::new(triple("aarch64-unknown-linux-gnu"));
856 assert!(Machine::for_target(&info).is_none());
857 }
858}