1use rucc_base::Interner;
27use rucc_ir as ir;
28use rucc_mir as mir;
29use rucc_regalloc::assign::Env;
30use rucc_target::{Arch, BranchInsts, CallRegs, FrameInsts, PhysReg, RegFile, TargetInfo, x86_64};
31
32use crate::coverage::Fired;
33use crate::elsewhere::Elsewhere;
34use crate::expand;
35use crate::finish::finish;
36use crate::frame::{Frame, Layout};
37use crate::layout;
38use crate::lower::{self, Unsupported};
39use crate::split;
40use crate::switch;
41use crate::varargs;
42use crate::widths;
43
44#[derive(Debug)]
53pub struct Machine {
54 pub conv: &'static CallRegs,
56 pub file: RegFile,
58 pub insts: &'static FrameInsts,
60 pub branch: &'static BranchInsts,
62 pub env: Env,
64}
65
66const SCRATCH: [PhysReg; 2] = [x86_64::R10, x86_64::R11];
73
74const SCRATCH_COUNT: usize = SCRATCH.len();
76
77impl Machine {
78 #[must_use]
85 pub fn x86_64(conv: &'static CallRegs) -> Self {
86 let order: Vec<PhysReg> =
87 conv.int_order.iter().copied().filter(|reg| !SCRATCH.contains(reg)).collect();
88 let free: Vec<PhysReg> =
95 conv.sse_order.iter().copied().filter(|®| !conv.preserves_sse(reg)).collect();
96 let at = free.len().saturating_sub(SCRATCH_COUNT);
97 let sse_scratch: Vec<PhysReg> = free[at..].to_vec();
98 let sse_order: Vec<PhysReg> =
99 conv.sse_order.iter().copied().filter(|reg| !sse_scratch.contains(reg)).collect();
100 Self {
101 conv,
102 file: x86_64::REGS,
103 insts: &x86_64::FRAME,
104 branch: &x86_64::BRANCH,
105 env: Env::new().with(x86_64::GPR, &order, &SCRATCH).with(
106 x86_64::XMM,
107 &sse_order,
108 &sse_scratch,
109 ),
110 }
111 }
112
113 #[must_use]
120 pub fn for_target(target: &TargetInfo) -> Option<Self> {
121 let conv = target.call_regs?;
122 match target.triple.arch {
123 Arch::X86_64 => Some(Self::x86_64(conv)),
124 Arch::Aarch64 | Arch::Riscv64 => None,
125 }
126 }
127}
128
129#[derive(Debug, Clone, Copy, PartialEq, Eq)]
131pub struct Flags {
132 pub frame_pointer: bool,
134 pub red_zone: bool,
136}
137
138impl Default for Flags {
139 fn default() -> Self {
142 Self { frame_pointer: false, red_zone: true }
143 }
144}
145
146pub fn compile(
165 source: &mut ir::Func,
166 names: &mut Interner,
167 machine: &Machine,
168 elsewhere: &Elsewhere,
169 flags: Flags,
170) -> Result<mir::Func, Unsupported> {
171 compile_recording(source, names, machine, elsewhere, flags, &mut Fired::new())
172}
173
174pub fn compile_recording(
188 source: &mut ir::Func,
189 names: &mut Interner,
190 machine: &Machine,
191 elsewhere: &Elsewhere,
192 flags: Flags,
193 fired: &mut Fired,
194) -> Result<mir::Func, Unsupported> {
195 switch::switches(source);
196 expand::orderings(source, machine.conv.word);
199 widths::integers(source);
202 expand::bytes(source);
203 expand::counts(source);
204 expand::overflows(source);
205 expand::floats(source);
206 expand::bulk(source, names, machine.conv.word);
207 varargs::lists(source, machine.conv);
208 let lowered = lower::func(source, names, machine.conv, elsewhere)?;
209 fired.merge(&lowered.fired);
210 let lower::Lowered { mut func, stack, .. } = lowered;
211 let layout = Layout {
212 frame_pointer: flags.frame_pointer,
213 red_zone: flags.red_zone,
214 ..stack.layout(Layout::new(machine.conv, machine.file))
215 };
216
217 split::critical(&mut func);
221 let called = names.resolve(func.name).to_owned();
222 let allocation = rucc_regalloc::run(&mut func, &machine.env, &called);
223
224 let frame = Frame::of(&func, &allocation, &layout);
227 finish(&mut func, &allocation, &frame, &stack, machine.conv, machine.insts, names);
228
229 layout::blocks(&mut func, machine.branch, names);
232 Ok(func)
233}
234
235#[cfg(test)]
236mod tests {
237 use rucc_ir::{Builder, Flags as IrFlags, Func, Opcode, Restrict, Signature, Type};
238 use rucc_target::x86_64::{REGS, SYSV, WIN64};
239
240 use super::*;
241
242 fn blank(params: &[Type]) -> (Interner, Func, ir::Block, Vec<ir::Value>) {
244 let mut names = Interner::new();
245 let mut func = Func::new(names.intern("f"), Signature::new());
246 let block = func.create_block();
247 let values = params.iter().map(|&ty| func.append_param(block, ty)).collect();
248 (names, func, block, values)
249 }
250
251 #[test]
252 fn a_function_comes_out_with_no_virtual_register_left_in_it() {
253 let i32 = Type::int(32);
254 let (mut names, mut source, block, args) = blank(&[i32, i32]);
255 let mut build = Builder::new(&mut source, block);
256 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
257 build.ret(&[sum]);
258
259 let machine = Machine::x86_64(&SYSV);
260 let out =
261 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
262 .expect("every instruction has a rule");
263
264 assert_eq!(
269 mir::print_func(&out, &names, ®S),
270 "mfunc @f {\n\
271 block0:\n \
272 $rdi($rdi) = x64.arg_val_32\n \
273 $rsi($rsi) = x64.arg_val_32\n \
274 $rdi(reuse 1) = x64.add_rr_32 $rdi, $rsi\n \
275 $rax = x64.mov_rr_64 $rdi\n \
276 x64.ret_val_32 $rax($rax)\n \
277 x64.ret\n\
278 }\n"
279 );
280 }
281
282 #[test]
286 fn which_rules_lowered_a_function_is_something_the_compilation_can_be_asked_for() {
287 let i32 = Type::int(32);
288 let (mut names, mut source, block, args) = blank(&[i32, i32]);
289 let mut build = Builder::new(&mut source, block);
290 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
291 build.ret(&[sum]);
292
293 let machine = Machine::x86_64(&SYSV);
294 let mut fired = Fired::new();
295 compile_recording(
296 &mut source,
297 &mut names,
298 &machine,
299 &Elsewhere::default(),
300 Flags::default(),
301 &mut fired,
302 )
303 .expect("every instruction has a rule");
304 let one = fired.count();
305 assert!(one > 0, "an add and a return went through the table and nothing was recorded");
306
307 let listing = fired.listing(&crate::select::x86_64::TABLE);
308 assert_eq!(listing.lines().filter(|line| line.starts_with("fired ")).count(), one);
309 assert!(
310 listing.contains(&format!("{one} of ")),
311 "{}",
312 listing.lines().next().unwrap_or("")
313 );
314
315 let (mut names, mut source, block, args) = blank(&[i32, i32]);
317 let mut build = Builder::new(&mut source, block);
318 let difference = build.binary(Opcode::Sub, args[0], args[1], IrFlags::default());
319 build.ret(&[difference]);
320 compile_recording(
321 &mut source,
322 &mut names,
323 &machine,
324 &Elsewhere::default(),
325 Flags::default(),
326 &mut fired,
327 )
328 .expect("every instruction has a rule");
329 assert!(fired.count() > one, "a subtraction is not an addition");
330 }
331
332 #[test]
333 fn a_function_that_calls_takes_a_frame_and_gives_it_back() {
334 let i32 = Type::int(32);
335 let (mut names, mut source, block, args) = blank(&[i32]);
336 let sig = source.add_signature(Signature::new().with_params(&[i32]).with_returns(&[i32]));
337 let callee = names.intern("g");
338 let call = Builder::new(&mut source, block).call(callee, sig, &[args[0]]);
339 let got = source[call].first_result.expect("an integer comes back");
340 let mut build = Builder::new(&mut source, block);
341 let sum = build.binary(Opcode::Add, got, args[0], IrFlags::default());
342 build.ret(&[sum]);
343
344 let machine = Machine::x86_64(&SYSV);
345 let out =
346 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
347 .expect("every instruction has a rule");
348
349 let text = mir::print_func(&out, &names, ®S);
352 assert!(text.contains("x64.push_64 $rbx"), "{text}");
353 assert!(text.contains("$rbx = x64.pop_64"), "{text}");
354 assert!(text.contains("x64.call $rdi($rdi), @g"), "{text}");
355 assert!(!text.contains('%'), "{text}");
356 }
357
358 #[test]
359 fn the_other_convention_is_the_same_function_somewhere_else() {
360 let i32 = Type::int(32);
361 let (mut names, mut source, block, args) = blank(&[i32, i32]);
362 let mut build = Builder::new(&mut source, block);
363 let sum = build.binary(Opcode::Add, args[0], args[1], IrFlags::default());
364 build.ret(&[sum]);
365
366 let machine = Machine::x86_64(&WIN64);
367 let out =
368 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
369 .expect("every instruction has a rule");
370
371 let text = mir::print_func(&out, &names, ®S);
374 assert!(text.contains("$rcx($rcx) = x64.arg_val_32"), "{text}");
375 assert!(text.contains("$rdx($rdx) = x64.arg_val_32"), "{text}");
376 assert!(!text.contains("$rdi"), "{text}");
377 }
378
379 #[test]
380 fn a_function_with_a_branch_in_it_goes_through_every_pass() {
381 let i32 = Type::int(32);
382 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
383 let then = source.create_block();
384 let join = source.create_block();
385 let got = source.append_param(join, i32);
386 let mut build = Builder::new(&mut source, entry);
387 let cond = build.icmp(rucc_ir::IntPred::Slt, args[0], args[1]);
388 build.br_if(cond, then, &[], join, &[args[1]]);
389 Builder::new(&mut source, then).jump(join, &[args[0]]);
390 Builder::new(&mut source, join).ret(&[got]);
391
392 let machine = Machine::x86_64(&SYSV);
393 let out =
394 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
395 .expect("every instruction has a rule");
396
397 assert_eq!(out.block_count(), 4);
401
402 let text = mir::print_func(&out, &names, ®S);
411 assert_eq!(
412 text,
413 "mfunc @f {\n\
414 block0:\n \
415 $rdi($rdi) = x64.arg_val_32\n \
416 $rsi($rsi) = x64.arg_val_32\n \
417 $rax = x64.cmp_set_l_32 $rdi, $rsi\n \
418 x64.test_rr_8 $rax\n \
419 x64.jcc_e block2, block1\n\
420 \nblock1:\n \
421 $rax = x64.mov_rr_64 $rdi\n \
422 x64.jmp block3\n\
423 \nblock2:\n \
424 $rax = x64.mov_rr_64 $rsi, block3\n\
425 \nblock3:\n \
426 x64.ret_val_32 $rax($rax)\n \
427 x64.ret\n\
428 }\n"
429 );
430 }
431
432 #[test]
438 fn a_loop_that_carries_its_values_round_keeps_all_of_them() {
439 let i32 = Type::int(32);
440 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
441 let head = source.create_block();
442 let body = source.create_block();
443 let exit = source.create_block();
444 let left = source.append_param(head, i32);
445 let right = source.append_param(head, i32);
446 Builder::new(&mut source, entry).jump(head, &[args[0], args[1]]);
447 let mut build = Builder::new(&mut source, head);
448 let zero = build.iconst(i32, 0);
449 let more = build.icmp(rucc_ir::IntPred::Ne, right, zero);
450 build.br_if(more, body, &[], exit, &[left]);
451 let mut build = Builder::new(&mut source, body);
452 let rest = build.binary(Opcode::SRem, left, right, IrFlags::default());
453 build.jump(head, &[right, rest]);
454 let result = source.append_param(exit, i32);
455 Builder::new(&mut source, exit).ret(&[result]);
456
457 let machine = Machine::x86_64(&SYSV);
458 let out =
459 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
460 .expect("every instruction has a rule");
461
462 assert_eq!(
478 mir::print_func(&out, &names, ®S),
479 "mfunc @f {\n\
480 block0:\n \
481 $rdi($rdi) = x64.arg_val_32\n \
482 $rsi($rsi) = x64.arg_val_32\n \
483 $rcx = x64.mov_rr_64 $rdi, block1\n\
484 \nblock1:\n \
485 $rax = x64.mov_ri_32 0\n \
486 $rax = x64.cmp_set_ne_32 $rsi, $rax\n \
487 x64.test_rr_8 $rax\n \
488 x64.jcc_e block3, block2\n\
489 \nblock2:\n \
490 $rax = x64.mov_rr_64 $rcx\n \
491 $rdx($rdx), early $rax($rax) = x64.idiv_rem_32 $rax($rax), $rsi\n \
492 $rdi = x64.mov_rr_64 $rax\n \
493 $rcx = x64.mov_rr_64 $rsi\n \
494 $rsi = x64.mov_rr_64 $rdx\n \
495 x64.jmp block1\n\
496 \nblock3:\n \
497 $rax = x64.mov_rr_64 $rcx\n \
498 x64.ret_val_32 $rax($rax)\n \
499 x64.ret\n\
500 }\n"
501 );
502 }
503
504 #[test]
509 fn a_function_that_has_been_laid_out_reads_back_as_the_same_function() {
510 let i32 = Type::int(32);
511 let (mut names, mut source, entry, args) = blank(&[i32, i32]);
512 let then = source.create_block();
513 let join = source.create_block();
514 let got = source.append_param(join, i32);
515 let mut build = Builder::new(&mut source, entry);
516 let cond = build.icmp(rucc_ir::IntPred::Slt, args[0], args[1]);
517 build.br_if(cond, then, &[], join, &[args[1]]);
518 Builder::new(&mut source, then).jump(join, &[args[0]]);
519 Builder::new(&mut source, join).ret(&[got]);
520
521 let machine = Machine::x86_64(&SYSV);
522 let out =
523 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
524 .expect("every instruction has a rule");
525
526 let text = mir::print_func(&out, &names, ®S);
527 let read = rucc_mir::parse(&text, &mut names, ®S).expect("what the printer wrote");
528 assert_eq!(mir::print(&read, &names, ®S), text);
529 }
530
531 #[test]
532 fn a_function_this_cannot_lower_is_reported_rather_than_compiled() {
533 let f80 = Type::float(rucc_ir::Float::F80);
534 let (mut names, mut source, block, args) = blank(&[f80, Type::int(64)]);
535 Builder::new(&mut source, block).ret(&args);
536
537 let machine = Machine::x86_64(&SYSV);
541 let failed =
542 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
543 .expect_err("a long double cannot come back beside another value");
544 assert_eq!(failed.to_string(), "what this function gives back is on the x87 stack");
545 }
546
547 #[test]
555 fn a_long_double_arrives_in_memory_and_goes_back_on_the_x87_stack() {
556 let f80 = Type::float(rucc_ir::Float::F80);
557 let (mut names, mut source, block, args) = blank(&[f80, f80]);
558 let mut build = Builder::new(&mut source, block);
559 let sum = build.binary(Opcode::FAdd, args[0], args[1], IrFlags::default());
560 build.ret(&[sum]);
561
562 let machine = Machine::x86_64(&SYSV);
563 let out =
564 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
565 .expect("every instruction has a rule");
566
567 let text = mir::print_func(&out, &names, ®S);
568 assert!(text.contains("x64.lea_64 [$rsp + 32]"), "{text}");
571 assert!(text.contains("x64.lea_64 [$rsp + 48]"), "{text}");
572 assert!(!text.contains("x64.ret_val"), "nothing comes back in a register: {text}");
573 let end: Vec<&str> = text.lines().rev().skip(1).take(3).map(str::trim).collect();
576 assert_eq!(end, ["x64.ret", "$rsp = x64.add_ri_64 $rsp, 24", "x64.fld_t [$rax]"], "{text}");
577 }
578
579 #[test]
583 fn a_float_is_added_in_the_register_file_it_arrives_in() {
584 let f32 = Type::float(rucc_ir::Float::F32);
585 let (mut names, mut source, block, args) = blank(&[f32, f32]);
586 let mut build = Builder::new(&mut source, block);
587 let sum = build.binary(Opcode::FAdd, args[0], args[1], ir::Flags::default());
588 build.ret(&[sum]);
589
590 let machine = Machine::x86_64(&SYSV);
591 let out =
592 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
593 .expect("every instruction has a rule");
594
595 let text = mir::print_func(&out, &names, ®S);
596 assert!(text.contains("x64.addss_rr"), "{text}");
597 assert!(text.contains("$xmm0"), "{text}");
598 assert!(!text.contains("$rax"), "{text}");
599 }
600
601 #[test]
604 fn a_float_read_from_memory_and_written_back_uses_the_scalar_moves() {
605 let f64 = Type::float(rucc_ir::Float::F64);
606 let (mut names, mut source, block, args) = blank(&[Type::PTR, f64]);
607 let mut build = Builder::new(&mut source, block);
608 let info = rucc_ir::MemInfo {
609 size: 8,
610 align: 8,
611 order: rucc_ir::MemOrder::NotAtomic,
612 tbaa: None,
613 restrict: Restrict::NONE,
614 };
615 let read = build.load(f64, args[0], info, ir::Flags::default());
616 let sum = build.binary(Opcode::FAdd, read, args[1], ir::Flags::default());
617 build.store(sum, args[0], info, ir::Flags::default());
618 build.ret(&[sum]);
619
620 let machine = Machine::x86_64(&SYSV);
621 let out =
622 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
623 .expect("every instruction has a rule");
624
625 let text = mir::print_func(&out, &names, ®S);
626 assert!(text.contains("x64.movsd_rm"), "{text}");
627 assert!(text.contains("x64.movsd_mr"), "{text}");
628 assert!(!text.contains("x64.movaps_rm"), "{text}");
631 assert!(!text.contains("x64.movaps_mr"), "{text}");
632 }
633
634 #[test]
642 fn an_unsigned_word_and_a_long_double_convert_into_each_other() {
643 let f80 = Type::float(rucc_ir::Float::F80);
644 let (mut names, mut source, block, args) = blank(&[Type::PTR, Type::int(64)]);
645 let mut build = Builder::new(&mut source, block);
646 let info = rucc_ir::MemInfo {
647 size: 16,
648 align: 16,
649 order: rucc_ir::MemOrder::NotAtomic,
650 tbaa: None,
651 restrict: Restrict::NONE,
652 };
653 let wide = build.unary(Opcode::UIToFP, args[1], f80);
654 build.store(wide, args[0], info, ir::Flags::default());
655 let read = build.load(f80, args[0], info, ir::Flags::default());
656 let back = build.unary(Opcode::FPToUI, read, Type::int(64));
657 build.ret(&[back]);
658
659 let machine = Machine::x86_64(&SYSV);
660 let out =
661 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
662 .expect("every instruction has a rule");
663
664 let text = mir::print_func(&out, &names, ®S);
665 assert!(text.contains("x64.fild_ll"), "the integer goes in as a signed one: {text}");
668 assert!(text.contains("x64.fistp_ll"), "and comes back out as one: {text}");
669 assert!(text.contains("x64.fmul_p"), "the correction is taken or not: {text}");
670 assert!(text.contains("x64.fadd_p"), "and applied one way: {text}");
671 assert!(text.contains("x64.fsubr_p"), "and the other: {text}");
672 assert!(!text.contains("xmm"), "no part of this is in a vector register: {text}");
673 }
674
675 #[test]
680 fn a_conversion_carries_the_value_into_the_other_register_file() {
681 let f64 = Type::float(rucc_ir::Float::F64);
682 let (mut names, mut source, block, args) = blank(&[f64]);
683 let mut build = Builder::new(&mut source, block);
684 let whole = build.unary(Opcode::FPToSI, args[0], Type::int(32));
685 let back = build.unary(Opcode::SIToFP, whole, f64);
686 build.ret(&[back]);
687
688 let machine = Machine::x86_64(&SYSV);
689 let out =
690 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
691 .expect("every instruction has a rule");
692
693 let text = mir::print_func(&out, &names, ®S);
696 assert!(text.contains("x64.cvttsd2si_32"), "{text}");
697 assert!(text.contains("x64.cvtsi2sd_32"), "{text}");
698 assert!(text.contains("$xmm0"), "{text}");
699 }
700
701 #[test]
704 fn a_bitcast_between_the_files_is_the_move_that_changes_no_bit() {
705 let f64 = Type::float(rucc_ir::Float::F64);
706 let (mut names, mut source, block, args) = blank(&[f64]);
707 let mut build = Builder::new(&mut source, block);
708 let bits = build.unary(Opcode::Bitcast, args[0], Type::int(64));
709 build.ret(&[bits]);
710
711 let machine = Machine::x86_64(&SYSV);
712 let out =
713 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
714 .expect("every instruction has a rule");
715
716 let text = mir::print_func(&out, &names, ®S);
717 assert!(text.contains("x64.movq_from_xmm"), "{text}");
718 assert!(!text.contains("cvt"), "{text}");
719 }
720
721 #[test]
723 fn a_float_comparison_is_the_compare_and_the_byte_a_condition_sets() {
724 let f64 = Type::float(rucc_ir::Float::F64);
725 let (mut names, mut source, block, args) = blank(&[f64, f64]);
726 let mut build = Builder::new(&mut source, block);
727 let less = build.fcmp(rucc_ir::FloatPred::Olt, args[0], args[1], ir::Flags::default());
728 let wide = build.unary(Opcode::ZExt, less, Type::int(32));
729 build.ret(&[wide]);
730
731 let machine = Machine::x86_64(&SYSV);
732 let out =
733 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
734 .expect("every instruction has a rule");
735
736 let text = mir::print_func(&out, &names, ®S);
739 assert!(text.contains("x64.ucomisd_set_a"), "{text}");
740 }
741
742 #[test]
747 fn an_equality_between_floats_gets_a_register_for_the_byte_it_needs_twice() {
748 let f64 = Type::float(rucc_ir::Float::F64);
749 let (mut names, mut source, block, args) = blank(&[f64, f64]);
750 let mut build = Builder::new(&mut source, block);
751 let same = build.fcmp(rucc_ir::FloatPred::Oeq, args[0], args[1], ir::Flags::default());
752 let wide = build.unary(Opcode::ZExt, same, Type::int(32));
753 build.ret(&[wide]);
754
755 let machine = Machine::x86_64(&SYSV);
756 let out =
757 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
758 .expect("every instruction has a rule");
759
760 let text = mir::print_func(&out, &names, ®S);
761 let line = text
762 .lines()
763 .find(|line| line.contains("x64.ucomisd_set_e_and_np"))
764 .expect("the rule for an ordered equality fired");
765 let written: Vec<&str> = line
766 .split_once('=')
767 .expect("the instruction writes something")
768 .0
769 .split(',')
770 .map(str::trim)
771 .collect();
772 assert_eq!(written.len(), 2, "{line}");
773 assert_ne!(written[0], written[1], "{line}");
774 }
775
776 #[test]
780 fn a_float_constant_is_the_bits_in_a_register_and_the_move_that_carries_them_over() {
781 let f64 = Type::float(rucc_ir::Float::F64);
782 let (mut names, mut source, block, _) = blank(&[]);
783 let mut build = Builder::new(&mut source, block);
784 let half = build.fconst(f64, 0x3fe0_0000_0000_0000);
785 build.ret(&[half]);
786
787 let machine = Machine::x86_64(&SYSV);
788 let out =
789 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
790 .expect("every instruction has a rule");
791
792 let text = mir::print_func(&out, &names, ®S);
793 assert!(text.contains("x64.mov_ri_64"), "{text}");
794 assert!(text.contains("x64.movq_to_xmm"), "{text}");
795 }
796
797 #[test]
800 fn a_negation_is_the_sign_bit_flipped_and_no_float_instruction_at_all() {
801 let f64 = Type::float(rucc_ir::Float::F64);
802 let (mut names, mut source, block, args) = blank(&[f64]);
803 let mut build = Builder::new(&mut source, block);
804 let less = build.unary(Opcode::FNeg, args[0], f64);
805 build.ret(&[less]);
806
807 let machine = Machine::x86_64(&SYSV);
808 let out =
809 compile(&mut source, &mut names, &machine, &Elsewhere::default(), Flags::default())
810 .expect("every instruction has a rule");
811
812 let text = mir::print_func(&out, &names, ®S);
813 assert!(text.contains("x64.xor_rr_64"), "{text}");
814 assert!(!text.contains("sub"), "a negation is not a subtraction: {text}");
815 }
816
817 #[test]
818 fn the_flags_reach_the_frame() {
819 let i32 = Type::int(32);
820 let (mut names, mut source, block, args) = blank(&[i32]);
821 Builder::new(&mut source, block).ret(&[args[0]]);
822
823 let machine = Machine::x86_64(&SYSV);
824 let flags = Flags { frame_pointer: true, red_zone: true };
825 let out = compile(&mut source, &mut names, &machine, &Elsewhere::default(), flags)
826 .expect("every instruction has a rule");
827
828 let text = mir::print_func(&out, &names, ®S);
831 assert!(text.contains("x64.push_64 $rbp"), "{text}");
832 assert!(text.contains("$rbp = x64.mov_rr_64 $rsp"), "{text}");
833 }
834
835 #[test]
836 fn a_target_says_which_machine_it_is_and_which_convention_it_uses() {
837 let triple = |text: &str| text.parse::<rucc_target::Triple>().expect("a triple");
838 let info = TargetInfo::new(triple("x86_64-unknown-linux-gnu"));
839 let machine = Machine::for_target(&info).expect("x86-64 is the target this crate covers");
840 assert!(std::ptr::eq(machine.conv, &SYSV));
841
842 let info = TargetInfo::new(triple("x86_64-pc-windows-msvc"));
843 let machine = Machine::for_target(&info).expect("x86-64 is the target this crate covers");
844 assert!(std::ptr::eq(machine.conv, &WIN64));
845
846 let info = TargetInfo::new(triple("aarch64-unknown-linux-gnu"));
849 assert!(Machine::for_target(&info).is_none());
850 }
851}