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];
83
84const SCRATCH_COUNT: usize = SCRATCH.len();
86
87impl Machine {
88 #[must_use]
95 pub fn x86_64(conv: &'static CallRegs) -> Self {
96 let order: Vec<PhysReg> =
97 conv.int_order.iter().copied().filter(|reg| !SCRATCH.contains(reg)).collect();
98 let free: Vec<PhysReg> =
105 conv.sse_order.iter().copied().filter(|®| !conv.preserves_sse(reg)).collect();
106 let at = free.len().saturating_sub(SCRATCH_COUNT);
107 let sse_scratch: Vec<PhysReg> = free[at..].to_vec();
108 let sse_order: Vec<PhysReg> =
109 conv.sse_order.iter().copied().filter(|reg| !sse_scratch.contains(reg)).collect();
110 Self {
111 conv,
112 file: x86_64::REGS,
113 insts: &x86_64::FRAME,
114 branch: &x86_64::BRANCH,
115 env: Env::new().with(x86_64::GPR, &order, &SCRATCH).with(
116 x86_64::XMM,
117 &sse_order,
118 &sse_scratch,
119 ),
120 }
121 }
122
123 #[must_use]
130 pub fn for_target(target: &TargetInfo) -> Option<Self> {
131 let conv = target.call_regs?;
132 match target.tuple.arch() {
133 Arch::X86_64 => Some(Self::x86_64(conv)),
134 _ => None,
135 }
136 }
137}
138
139#[derive(Debug, Clone, Copy, PartialEq, Eq)]
141pub struct Flags {
142 pub frame_pointer: bool,
144 pub red_zone: bool,
146}
147
148impl Default for Flags {
149 fn default() -> Self {
152 Self { frame_pointer: false, red_zone: true }
153 }
154}
155
156pub fn compile(
175 source: &mut ir::Func,
176 names: &mut Interner,
177 machine: &Machine,
178 elsewhere: &Elsewhere,
179 flags: Flags,
180) -> Result<mir::Func, Unsupported> {
181 compile_recording(source, names, machine, elsewhere, flags, &mut Fired::new())
182}
183
184pub fn compile_recording(
198 source: &mut ir::Func,
199 names: &mut Interner,
200 machine: &Machine,
201 elsewhere: &Elsewhere,
202 flags: Flags,
203 fired: &mut Fired,
204) -> Result<mir::Func, Unsupported> {
205 switch::switches(source);
206 retry::loops(source);
211 expand::orderings(source, machine.conv.word);
214 widths::integers(source);
217 expand::bytes(source);
218 expand::counts(source);
219 expand::overflows(source);
220 expand::floats(source);
221 expand::bulk(source, names, machine.conv.word);
222 varargs::lists(source, machine.conv);
223 let lowered = lower::func(source, names, machine.conv, elsewhere)?;
224 fired.merge(&lowered.fired);
225 let lower::Lowered { mut func, stack, .. } = lowered;
226 let layout = Layout {
227 frame_pointer: flags.frame_pointer,
228 red_zone: flags.red_zone,
229 ..stack.layout(Layout::new(machine.conv, machine.file))
230 };
231
232 split::critical(&mut func);
236 let called = names.resolve(func.name).to_owned();
237 let allocation = rucc_regalloc::run(&mut func, &machine.env, &called);
238
239 let frame = Frame::of(&func, &allocation, &layout);
242 finish(&mut func, &allocation, &frame, &stack, machine.conv, machine.insts, names);
243
244 layout::blocks(&mut func, machine.branch, names);
247 Ok(func)
248}
249
250#[cfg(test)]
251mod tests {
252 use rucc_ir::{Builder, Flags as IrFlags, Func, Opcode, Restrict, Signature, Type};
253 use rucc_target::x86_64::{REGS, SYSV, WIN64};
254
255 use super::*;
256
257 fn blank(params: &[Type]) -> (Interner, Func, ir::Block, Vec<ir::Value>) {
259 let mut names = Interner::new();
260 let mut func = Func::new(names.intern("f"), Signature::new());
261 let block = func.create_block();
262 let values = params.iter().map(|&ty| func.append_param(block, ty)).collect();
263 (names, func, block, values)
264 }
265
266 #[test]
267 fn a_function_comes_out_with_no_virtual_register_left_in_it() {
268 let i32 = Type::int(32);
269 let (mut names, mut source, block, args) = blank(&[i32, i32]);
270 let mut build = Builder::new(&mut source, block);
271 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
272 build.ret(&[sum]);
273
274 let machine = Machine::x86_64(&SYSV);
275 let out =
276 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
277 .expect("every instruction has a rule");
278
279 assert_eq!(
284 mir::print_func(&out, &names, ®S),
285 "mfunc @f {\n\
286 block0:\n \
287 $rdi($rdi) = x64.arg_val_32\n \
288 $rsi($rsi) = x64.arg_val_32\n \
289 $rdi(reuse 1) = x64.add_rr_32 $rdi, $rsi\n \
290 $rax = x64.mov_rr_64 $rdi\n \
291 x64.ret_val_32 $rax($rax)\n \
292 x64.ret\n\
293 }\n"
294 );
295 }
296
297 #[test]
301 fn which_rules_lowered_a_function_is_something_the_compilation_can_be_asked_for() {
302 let i32 = Type::int(32);
303 let (mut names, mut source, block, args) = blank(&[i32, i32]);
304 let mut build = Builder::new(&mut source, block);
305 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
306 build.ret(&[sum]);
307
308 let machine = Machine::x86_64(&SYSV);
309 let mut fired = Fired::new();
310 compile_recording(
311 &mut source,
312 &mut names,
313 &machine,
314 &Elsewhere::default(),
315 Flags::default(),
316 &mut fired,
317 )
318 .expect("every instruction has a rule");
319 let one = fired.count();
320 assert!(one > 0, "an add and a return went through the table and nothing was recorded");
321
322 let listing = fired.listing(&crate::select::x86_64::TABLE);
323 assert_eq!(listing.lines().filter(|line| line.starts_with("fired ")).count(), one);
324 assert!(
325 listing.contains(&format!("{one} of ")),
326 "{}",
327 listing.lines().next().unwrap_or("")
328 );
329
330 let (mut names, mut source, block, args) = blank(&[i32, i32]);
332 let mut build = Builder::new(&mut source, block);
333 let difference = build.binary(Opcode::Sub, args[0], args[1], IrFlags::default());
334 build.ret(&[difference]);
335 compile_recording(
336 &mut source,
337 &mut names,
338 &machine,
339 &Elsewhere::default(),
340 Flags::default(),
341 &mut fired,
342 )
343 .expect("every instruction has a rule");
344 assert!(fired.count() > one, "a subtraction is not an addition");
345 }
346
347 #[test]
348 fn a_function_that_calls_takes_a_frame_and_gives_it_back() {
349 let i32 = Type::int(32);
350 let (mut names, mut source, block, args) = blank(&[i32]);
351 let sig = source.add_signature(Signature::new().with_params(&[i32]).with_returns(&[i32]));
352 let callee = names.intern("g");
353 let call = Builder::new(&mut source, block).call(callee, sig, &[args[0]]);
354 let got = source[call].first_result.expect("an integer comes back");
355 let mut build = Builder::new(&mut source, block);
356 let sum = build.binary(Opcode::Add, got, args[0], IrFlags::default());
357 build.ret(&[sum]);
358
359 let machine = Machine::x86_64(&SYSV);
360 let out =
361 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
362 .expect("every instruction has a rule");
363
364 let text = mir::print_func(&out, &names, ®S);
367 assert!(text.contains("x64.push_64 $rbx"), "{text}");
368 assert!(text.contains("$rbx = x64.pop_64"), "{text}");
369 assert!(text.contains("x64.call $rdi($rdi), @g"), "{text}");
370 assert!(!text.contains('%'), "{text}");
371 }
372
373 #[test]
374 fn the_other_convention_is_the_same_function_somewhere_else() {
375 let i32 = Type::int(32);
376 let (mut names, mut source, block, args) = blank(&[i32, i32]);
377 let mut build = Builder::new(&mut source, block);
378 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
379 build.ret(&[sum]);
380
381 let machine = Machine::x86_64(&WIN64);
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("$rcx($rcx) = x64.arg_val_32"), "{text}");
390 assert!(text.contains("$rdx($rdx) = x64.arg_val_32"), "{text}");
391 assert!(!text.contains("$rdi"), "{text}");
392 }
393
394 #[test]
395 fn a_function_with_a_branch_in_it_goes_through_every_pass() {
396 let i32 = Type::int(32);
397 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
398 let then = source.create_block();
399 let join = source.create_block();
400 let got = source.append_param(join, i32);
401 let mut build = Builder::new(&mut source, entry);
402 let cond = build.icmp(rucc_ir::IntPred::Slt, args[0], args[1]);
403 build.br_if(cond, then, &[], join, &[args[1]]);
404 Builder::new(&mut source, then).jump(join, &[args[0]]);
405 Builder::new(&mut source, join).ret(&[got]);
406
407 let machine = Machine::x86_64(&SYSV);
408 let out =
409 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
410 .expect("every instruction has a rule");
411
412 assert_eq!(out.block_count(), 4);
416
417 let text = mir::print_func(&out, &names, ®S);
426 assert_eq!(
427 text,
428 "mfunc @f {\n\
429 block0:\n \
430 $rdi($rdi) = x64.arg_val_32\n \
431 $rsi($rsi) = x64.arg_val_32\n \
432 $rax = x64.cmp_set_l_32 $rdi, $rsi\n \
433 x64.test_rr_8 $rax\n \
434 x64.jcc_e block2, block1\n\
435 \nblock1:\n \
436 $rax = x64.mov_rr_64 $rdi\n \
437 x64.jmp block3\n\
438 \nblock2:\n \
439 $rax = x64.mov_rr_64 $rsi, block3\n\
440 \nblock3:\n \
441 x64.ret_val_32 $rax($rax)\n \
442 x64.ret\n\
443 }\n"
444 );
445 }
446
447 #[test]
453 fn a_loop_that_carries_its_values_round_keeps_all_of_them() {
454 let i32 = Type::int(32);
455 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
456 let head = source.create_block();
457 let body = source.create_block();
458 let exit = source.create_block();
459 let left = source.append_param(head, i32);
460 let right = source.append_param(head, i32);
461 Builder::new(&mut source, entry).jump(head, &[args[0], args[1]]);
462 let mut build = Builder::new(&mut source, head);
463 let zero = build.iconst(i32, 0);
464 let more = build.icmp(rucc_ir::IntPred::Ne, right, zero);
465 build.br_if(more, body, &[], exit, &[left]);
466 let mut build = Builder::new(&mut source, body);
467 let rest = build.binary(Opcode::SRem, left, right, IrFlags::default());
468 build.jump(head, &[right, rest]);
469 let result = source.append_param(exit, i32);
470 Builder::new(&mut source, exit).ret(&[result]);
471
472 let machine = Machine::x86_64(&SYSV);
473 let out =
474 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
475 .expect("every instruction has a rule");
476
477 assert_eq!(
493 mir::print_func(&out, &names, ®S),
494 "mfunc @f {\n\
495 block0:\n \
496 $rdi($rdi) = x64.arg_val_32\n \
497 $rsi($rsi) = x64.arg_val_32\n \
498 $rcx = x64.mov_rr_64 $rdi, block1\n\
499 \nblock1:\n \
500 $rax = x64.mov_ri_32 0\n \
501 $rax = x64.cmp_set_ne_32 $rsi, $rax\n \
502 x64.test_rr_8 $rax\n \
503 x64.jcc_e block3, block2\n\
504 \nblock2:\n \
505 $rax = x64.mov_rr_64 $rcx\n \
506 $rdx($rdx), early $rax($rax) = x64.idiv_rem_32 $rax($rax), $rsi\n \
507 $rdi = x64.mov_rr_64 $rax\n \
508 $rcx = x64.mov_rr_64 $rsi\n \
509 $rsi = x64.mov_rr_64 $rdx\n \
510 x64.jmp block1\n\
511 \nblock3:\n \
512 $rax = x64.mov_rr_64 $rcx\n \
513 x64.ret_val_32 $rax($rax)\n \
514 x64.ret\n\
515 }\n"
516 );
517 }
518
519 #[test]
524 fn a_function_that_has_been_laid_out_reads_back_as_the_same_function() {
525 let i32 = Type::int(32);
526 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
527 let then = source.create_block();
528 let join = source.create_block();
529 let got = source.append_param(join, i32);
530 let mut build = Builder::new(&mut source, entry);
531 let cond = build.icmp(rucc_ir::IntPred::Slt, args[0], args[1]);
532 build.br_if(cond, then, &[], join, &[args[1]]);
533 Builder::new(&mut source, then).jump(join, &[args[0]]);
534 Builder::new(&mut source, join).ret(&[got]);
535
536 let machine = Machine::x86_64(&SYSV);
537 let out =
538 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
539 .expect("every instruction has a rule");
540
541 let text = mir::print_func(&out, &names, ®S);
542 let read = rucc_mir::parse(&text, &mut names, ®S).expect("what the printer wrote");
543 assert_eq!(mir::print(&read, &names, ®S), text);
544 }
545
546 #[test]
547 fn a_function_this_cannot_lower_is_reported_rather_than_compiled() {
548 let f80 = Type::float(rucc_ir::Float::F80);
549 let (mut names, mut source, block, args) = blank(&[f80, Type::int(64)]);
550 Builder::new(&mut source, block).ret(&args);
551
552 let machine = Machine::x86_64(&SYSV);
556 let failed =
557 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
558 .expect_err("a long double cannot come back beside another value");
559 assert_eq!(failed.to_string(), "what this function gives back is on the x87 stack");
560 }
561
562 #[test]
570 fn a_long_double_arrives_in_memory_and_goes_back_on_the_x87_stack() {
571 let f80 = Type::float(rucc_ir::Float::F80);
572 let (mut names, mut source, block, args) = blank(&[f80, f80]);
573 let mut build = Builder::new(&mut source, block);
574 let sum = build.binary(Opcode::FAdd, args[0], args[1], IrFlags::default());
575 build.ret(&[sum]);
576
577 let machine = Machine::x86_64(&SYSV);
578 let out =
579 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
580 .expect("every instruction has a rule");
581
582 let text = mir::print_func(&out, &names, ®S);
583 assert!(text.contains("x64.lea_64 [$rsp + 32]"), "{text}");
586 assert!(text.contains("x64.lea_64 [$rsp + 48]"), "{text}");
587 assert!(!text.contains("x64.ret_val"), "nothing comes back in a register: {text}");
588 let end: Vec<&str> = text.lines().rev().skip(1).take(3).map(str::trim).collect();
591 assert_eq!(end, ["x64.ret", "$rsp = x64.add_ri_64 $rsp, 24", "x64.fld_t [$rax]"], "{text}");
592 }
593
594 #[test]
598 fn a_float_is_added_in_the_register_file_it_arrives_in() {
599 let f32 = Type::float(rucc_ir::Float::F32);
600 let (mut names, mut source, block, args) = blank(&[f32, f32]);
601 let mut build = Builder::new(&mut source, block);
602 let sum = build.binary(Opcode::FAdd, args[0], args[1], ir::Flags::default());
603 build.ret(&[sum]);
604
605 let machine = Machine::x86_64(&SYSV);
606 let out =
607 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
608 .expect("every instruction has a rule");
609
610 let text = mir::print_func(&out, &names, ®S);
611 assert!(text.contains("x64.addss_rr"), "{text}");
612 assert!(text.contains("$xmm0"), "{text}");
613 assert!(!text.contains("$rax"), "{text}");
614 }
615
616 #[test]
619 fn a_float_read_from_memory_and_written_back_uses_the_scalar_moves() {
620 let f64 = Type::float(rucc_ir::Float::F64);
621 let (mut names, mut source, block, args) = blank(&[Type::PTR, f64]);
622 let mut build = Builder::new(&mut source, block);
623 let info = rucc_ir::MemInfo {
624 size: 8,
625 align: 8,
626 order: rucc_ir::MemOrder::NotAtomic,
627 tbaa: None,
628 restrict: Restrict::NONE,
629 };
630 let read = build.load(f64, args[0], info, ir::Flags::default());
631 let sum = build.binary(Opcode::FAdd, read, args[1], ir::Flags::default());
632 build.store(sum, args[0], info, ir::Flags::default());
633 build.ret(&[sum]);
634
635 let machine = Machine::x86_64(&SYSV);
636 let out =
637 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
638 .expect("every instruction has a rule");
639
640 let text = mir::print_func(&out, &names, ®S);
641 assert!(text.contains("x64.movsd_rm"), "{text}");
642 assert!(text.contains("x64.movsd_mr"), "{text}");
643 assert!(!text.contains("x64.movaps_rm"), "{text}");
646 assert!(!text.contains("x64.movaps_mr"), "{text}");
647 }
648
649 #[test]
657 fn an_unsigned_word_and_a_long_double_convert_into_each_other() {
658 let f80 = Type::float(rucc_ir::Float::F80);
659 let (mut names, mut source, block, args) = blank(&[Type::PTR, Type::int(64)]);
660 let mut build = Builder::new(&mut source, block);
661 let info = rucc_ir::MemInfo {
662 size: 16,
663 align: 16,
664 order: rucc_ir::MemOrder::NotAtomic,
665 tbaa: None,
666 restrict: Restrict::NONE,
667 };
668 let wide = build.unary(Opcode::UIToFP, args[1], f80);
669 build.store(wide, args[0], info, ir::Flags::default());
670 let read = build.load(f80, args[0], info, ir::Flags::default());
671 let back = build.unary(Opcode::FPToUI, read, Type::int(64));
672 build.ret(&[back]);
673
674 let machine = Machine::x86_64(&SYSV);
675 let out =
676 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
677 .expect("every instruction has a rule");
678
679 let text = mir::print_func(&out, &names, ®S);
680 assert!(text.contains("x64.fild_ll"), "the integer goes in as a signed one: {text}");
683 assert!(text.contains("x64.fistp_ll"), "and comes back out as one: {text}");
684 assert!(text.contains("x64.fmul_p"), "the correction is taken or not: {text}");
685 assert!(text.contains("x64.fadd_p"), "and applied one way: {text}");
686 assert!(text.contains("x64.fsubr_p"), "and the other: {text}");
687 assert!(!text.contains("xmm"), "no part of this is in a vector register: {text}");
688 }
689
690 #[test]
695 fn a_conversion_carries_the_value_into_the_other_register_file() {
696 let f64 = Type::float(rucc_ir::Float::F64);
697 let (mut names, mut source, block, args) = blank(&[f64]);
698 let mut build = Builder::new(&mut source, block);
699 let whole = build.unary(Opcode::FPToSI, args[0], Type::int(32));
700 let back = build.unary(Opcode::SIToFP, whole, f64);
701 build.ret(&[back]);
702
703 let machine = Machine::x86_64(&SYSV);
704 let out =
705 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
706 .expect("every instruction has a rule");
707
708 let text = mir::print_func(&out, &names, ®S);
711 assert!(text.contains("x64.cvttsd2si_32"), "{text}");
712 assert!(text.contains("x64.cvtsi2sd_32"), "{text}");
713 assert!(text.contains("$xmm0"), "{text}");
714 }
715
716 #[test]
719 fn a_bitcast_between_the_files_is_the_move_that_changes_no_bit() {
720 let f64 = Type::float(rucc_ir::Float::F64);
721 let (mut names, mut source, block, args) = blank(&[f64]);
722 let mut build = Builder::new(&mut source, block);
723 let bits = build.unary(Opcode::Bitcast, args[0], Type::int(64));
724 build.ret(&[bits]);
725
726 let machine = Machine::x86_64(&SYSV);
727 let out =
728 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
729 .expect("every instruction has a rule");
730
731 let text = mir::print_func(&out, &names, ®S);
732 assert!(text.contains("x64.movq_from_xmm"), "{text}");
733 assert!(!text.contains("cvt"), "{text}");
734 }
735
736 #[test]
738 fn a_float_comparison_is_the_compare_and_the_byte_a_condition_sets() {
739 let f64 = Type::float(rucc_ir::Float::F64);
740 let (mut names, mut source, block, args) = blank(&[f64, f64]);
741 let mut build = Builder::new(&mut source, block);
742 let less = build.fcmp(rucc_ir::FloatPred::Olt, args[0], args[1], ir::Flags::default());
743 let wide = build.unary(Opcode::ZExt, less, Type::int(32));
744 build.ret(&[wide]);
745
746 let machine = Machine::x86_64(&SYSV);
747 let out =
748 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
749 .expect("every instruction has a rule");
750
751 let text = mir::print_func(&out, &names, ®S);
754 assert!(text.contains("x64.ucomisd_set_a"), "{text}");
755 }
756
757 #[test]
762 fn an_equality_between_floats_gets_a_register_for_the_byte_it_needs_twice() {
763 let f64 = Type::float(rucc_ir::Float::F64);
764 let (mut names, mut source, block, args) = blank(&[f64, f64]);
765 let mut build = Builder::new(&mut source, block);
766 let same = build.fcmp(rucc_ir::FloatPred::Oeq, args[0], args[1], ir::Flags::default());
767 let wide = build.unary(Opcode::ZExt, same, Type::int(32));
768 build.ret(&[wide]);
769
770 let machine = Machine::x86_64(&SYSV);
771 let out =
772 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
773 .expect("every instruction has a rule");
774
775 let text = mir::print_func(&out, &names, ®S);
776 let line = text
777 .lines()
778 .find(|line| line.contains("x64.ucomisd_set_e_and_np"))
779 .expect("the rule for an ordered equality fired");
780 let written: Vec<&str> = line
781 .split_once('=')
782 .expect("the instruction writes something")
783 .0
784 .split(',')
785 .map(str::trim)
786 .collect();
787 assert_eq!(written.len(), 2, "{line}");
788 assert_ne!(written[0], written[1], "{line}");
789 }
790
791 #[test]
795 fn a_float_constant_is_the_bits_in_a_register_and_the_move_that_carries_them_over() {
796 let f64 = Type::float(rucc_ir::Float::F64);
797 let (mut names, mut source, block, _) = blank(&[]);
798 let mut build = Builder::new(&mut source, block);
799 let half = build.fconst(f64, 0x3fe0_0000_0000_0000);
800 build.ret(&[half]);
801
802 let machine = Machine::x86_64(&SYSV);
803 let out =
804 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
805 .expect("every instruction has a rule");
806
807 let text = mir::print_func(&out, &names, ®S);
808 assert!(text.contains("x64.mov_ri_64"), "{text}");
809 assert!(text.contains("x64.movq_to_xmm"), "{text}");
810 }
811
812 #[test]
815 fn a_negation_is_the_sign_bit_flipped_and_no_float_instruction_at_all() {
816 let f64 = Type::float(rucc_ir::Float::F64);
817 let (mut names, mut source, block, args) = blank(&[f64]);
818 let mut build = Builder::new(&mut source, block);
819 let less = build.unary(Opcode::FNeg, args[0], f64);
820 build.ret(&[less]);
821
822 let machine = Machine::x86_64(&SYSV);
823 let out =
824 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
825 .expect("every instruction has a rule");
826
827 let text = mir::print_func(&out, &names, ®S);
828 assert!(text.contains("x64.xor_rr_64"), "{text}");
829 assert!(!text.contains("sub"), "a negation is not a subtraction: {text}");
830 }
831
832 #[test]
833 fn the_flags_reach_the_frame() {
834 let i32 = Type::int(32);
835 let (mut names, mut source, block, args) = blank(&[i32]);
836 Builder::new(&mut source, block).ret(&[args[0]]);
837
838 let machine = Machine::x86_64(&SYSV);
839 let flags = Flags { frame_pointer: true, red_zone: true };
840 let out = compile(&mut source, &mut names, &machine, &Elsewhere::default(), flags)
841 .expect("every instruction has a rule");
842
843 let text = mir::print_func(&out, &names, ®S);
846 assert!(text.contains("x64.push_64 $rbp"), "{text}");
847 assert!(text.contains("$rbp = x64.mov_rr_64 $rsp"), "{text}");
848 }
849
850 #[test]
851 fn a_target_says_which_machine_it_is_and_which_convention_it_uses() {
852 let triple = |text: &str| text.parse::<rucc_target::Triple>().expect("a triple");
853 let info = TargetInfo::new(triple("x86_64-unknown-linux-gnu"));
854 let machine = Machine::for_target(&info).expect("x86-64 is the target this crate covers");
855 assert!(std::ptr::eq(machine.conv, &SYSV));
856
857 let info = TargetInfo::new(triple("x86_64-pc-windows-msvc"));
858 let machine = Machine::for_target(&info).expect("x86-64 is the target this crate covers");
859 assert!(std::ptr::eq(machine.conv, &WIN64));
860
861 let info = TargetInfo::new(triple("aarch64-unknown-linux-gnu"));
864 assert!(Machine::for_target(&info).is_none());
865 }
866}