|
| 1 | +//! A `Void` parameter keeps the parameters after it where the other |
| 2 | +//! tier looks for them. |
| 3 | +//! |
| 4 | +//! Cranelift passes a `Void` as a byte in its own register. An LLVM |
| 5 | +//! entry standing in a call cell that Cranelift callers read, and an |
| 6 | +//! LLVM call through a cell holding Cranelift's code, must pass it the |
| 7 | +//! same way, or every later argument arrives in the register of the |
| 8 | +//! one before it. |
| 9 | +
|
| 10 | +#![cfg(all(feature = "cranelift-backend", feature = "llvm-backend"))] |
| 11 | + |
| 12 | +use std::collections::HashSet; |
| 13 | +use std::sync::Arc; |
| 14 | +use zyntax_compiler::hir::{ |
| 15 | + BinaryOp, HirBlock, HirCallable, HirConstant, HirFunction, HirFunctionSignature, HirId, |
| 16 | + HirInstruction, HirModule, HirParam, HirTerminator, HirType, HirValue, HirValueKind, |
| 17 | + ParamAttributes, |
| 18 | +}; |
| 19 | +use zyntax_typed_ast::InternedString; |
| 20 | + |
| 21 | +fn sig(params: Vec<HirType>, returns: Vec<HirType>) -> HirFunctionSignature { |
| 22 | + HirFunctionSignature { |
| 23 | + params: params |
| 24 | + .into_iter() |
| 25 | + .enumerate() |
| 26 | + .map(|(i, ty)| HirParam { |
| 27 | + id: HirId::new(), |
| 28 | + name: InternedString::new_global(&format!("p{i}")), |
| 29 | + ty, |
| 30 | + attributes: ParamAttributes::default(), |
| 31 | + ownership: Default::default(), |
| 32 | + }) |
| 33 | + .collect(), |
| 34 | + returns, |
| 35 | + type_params: vec![], |
| 36 | + const_params: vec![], |
| 37 | + lifetime_params: vec![], |
| 38 | + is_variadic: false, |
| 39 | + is_async: false, |
| 40 | + is_fiber: false, |
| 41 | + effects: vec![], |
| 42 | + is_pure: false, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +fn add_value(func: &mut HirFunction, ty: HirType, kind: HirValueKind) -> HirId { |
| 47 | + let id = HirId::new(); |
| 48 | + func.values.insert( |
| 49 | + id, |
| 50 | + HirValue { |
| 51 | + id, |
| 52 | + ty, |
| 53 | + kind, |
| 54 | + uses: HashSet::new(), |
| 55 | + span: None, |
| 56 | + }, |
| 57 | + ); |
| 58 | + id |
| 59 | +} |
| 60 | + |
| 61 | +fn konst(func: &mut HirFunction, v: i64) -> HirId { |
| 62 | + add_value( |
| 63 | + func, |
| 64 | + HirType::I64, |
| 65 | + HirValueKind::Constant(HirConstant::I64(v)), |
| 66 | + ) |
| 67 | +} |
| 68 | + |
| 69 | +fn body(func: &mut HirFunction) -> &mut HirBlock { |
| 70 | + let entry = func.entry_block; |
| 71 | + func.blocks.get_mut(&entry).unwrap() |
| 72 | +} |
| 73 | + |
| 74 | +/// `def pick(a: i64, u: Void, b: i64, c: i64): i64 { return a*100 + b*10 + c }` |
| 75 | +fn build_pick() -> HirFunction { |
| 76 | + let mut f = HirFunction::new( |
| 77 | + InternedString::new_global("pick"), |
| 78 | + sig( |
| 79 | + vec![HirType::I64, HirType::Void, HirType::I64, HirType::I64], |
| 80 | + vec![HirType::I64], |
| 81 | + ), |
| 82 | + ); |
| 83 | + let a = add_value(&mut f, HirType::I64, HirValueKind::Parameter(0)); |
| 84 | + let b = add_value(&mut f, HirType::I64, HirValueKind::Parameter(2)); |
| 85 | + let c = add_value(&mut f, HirType::I64, HirValueKind::Parameter(3)); |
| 86 | + let hundred = konst(&mut f, 100); |
| 87 | + let ten = konst(&mut f, 10); |
| 88 | + let v: Vec<HirId> = (0..4) |
| 89 | + .map(|_| add_value(&mut f, HirType::I64, HirValueKind::Instruction)) |
| 90 | + .collect(); |
| 91 | + let arith = |op, result, left, right| HirInstruction::Binary { |
| 92 | + op, |
| 93 | + result, |
| 94 | + ty: HirType::I64, |
| 95 | + left, |
| 96 | + right, |
| 97 | + }; |
| 98 | + let blk = body(&mut f); |
| 99 | + blk.instructions.extend([ |
| 100 | + arith(BinaryOp::Mul, v[0], a, hundred), |
| 101 | + arith(BinaryOp::Mul, v[1], b, ten), |
| 102 | + arith(BinaryOp::Add, v[2], v[0], v[1]), |
| 103 | + arith(BinaryOp::Add, v[3], v[2], c), |
| 104 | + ]); |
| 105 | + blk.terminator = HirTerminator::Return { values: vec![v[3]] }; |
| 106 | + f |
| 107 | +} |
| 108 | + |
| 109 | +/// `def pick_main(): i64 { return pick(1, (), 2, 3) }` |
| 110 | +fn build_pick_main(pick_id: HirId) -> HirFunction { |
| 111 | + let mut f = HirFunction::new( |
| 112 | + InternedString::new_global("pick_main"), |
| 113 | + sig(vec![], vec![HirType::I64]), |
| 114 | + ); |
| 115 | + let args: Vec<HirId> = [1, 2, 3].into_iter().map(|v| konst(&mut f, v)).collect(); |
| 116 | + let unit = add_value(&mut f, HirType::Void, HirValueKind::Undef); |
| 117 | + let r = add_value(&mut f, HirType::I64, HirValueKind::Instruction); |
| 118 | + let blk = body(&mut f); |
| 119 | + blk.instructions.push(HirInstruction::Call { |
| 120 | + result: Some(r), |
| 121 | + callee: HirCallable::Function(pick_id), |
| 122 | + args: vec![args[0], unit, args[1], args[2]], |
| 123 | + type_args: vec![], |
| 124 | + const_args: vec![], |
| 125 | + is_tail: false, |
| 126 | + }); |
| 127 | + blk.terminator = HirTerminator::Return { values: vec![r] }; |
| 128 | + f |
| 129 | +} |
| 130 | + |
| 131 | +const PICK_EXPECTED: i64 = 123; |
| 132 | + |
| 133 | +fn pick_module() -> (HirModule, HirFunction, HirFunction) { |
| 134 | + let pick = build_pick(); |
| 135 | + let main = build_pick_main(pick.id); |
| 136 | + let mut module = HirModule::new(InternedString::new_global("void_param")); |
| 137 | + module.functions.insert(pick.id, pick.clone()); |
| 138 | + module.functions.insert(main.id, main.clone()); |
| 139 | + (module, pick, main) |
| 140 | +} |
| 141 | + |
| 142 | +/// One call cell, entered first by Cranelift's code for `pick` and then |
| 143 | +/// by LLVM's, from the same Cranelift caller. |
| 144 | +#[test] |
| 145 | +fn llvm_entry_takes_the_parameters_after_a_void_where_cranelift_passes_them() { |
| 146 | + use inkwell::context::Context; |
| 147 | + use zyntax_compiler::cranelift_backend::CraneliftBackend; |
| 148 | + use zyntax_compiler::llvm_jit_backend::LLVMJitBackend; |
| 149 | + |
| 150 | + let (module, pick, main) = pick_module(); |
| 151 | + let mut cranelift = CraneliftBackend::new().expect("backend"); |
| 152 | + cranelift.set_reloadable_calls(true); |
| 153 | + cranelift.compile_module(&module).expect("compile"); |
| 154 | + cranelift.finalize_definitions().expect("finalize"); |
| 155 | + let ptr = cranelift.get_function_ptr(main.id).expect("main compiled"); |
| 156 | + let run: unsafe extern "C" fn() -> i64 = unsafe { std::mem::transmute(ptr) }; |
| 157 | + assert_eq!(unsafe { run() }, PICK_EXPECTED); |
| 158 | + |
| 159 | + let context = Context::create(); |
| 160 | + let mut llvm = LLVMJitBackend::new(&context).expect("backend"); |
| 161 | + llvm.set_use_mcjit(true); |
| 162 | + llvm.set_module_context(Arc::new(module.clone())); |
| 163 | + llvm.set_cross_tier_links(cranelift.reload_key(), Arc::new(|_| None)); |
| 164 | + llvm.compile_function(pick.id, &pick) |
| 165 | + .expect("LLVM compiles the entry"); |
| 166 | + let entry = llvm.get_function_pointer(pick.id).expect("pick compiled"); |
| 167 | + cranelift.publish_call_target(pick.id, entry as usize); |
| 168 | + assert_eq!(unsafe { run() }, PICK_EXPECTED); |
| 169 | +} |
| 170 | + |
| 171 | +/// An LLVM entry calling Cranelift's `pick` through its cell. |
| 172 | +#[test] |
| 173 | +fn llvm_passes_the_parameters_after_a_void_where_cranelift_takes_them() { |
| 174 | + use inkwell::context::Context; |
| 175 | + use zyntax_compiler::cranelift_backend::CraneliftBackend; |
| 176 | + use zyntax_compiler::llvm_jit_backend::LLVMJitBackend; |
| 177 | + |
| 178 | + let (module, _pick, main) = pick_module(); |
| 179 | + let mut cranelift = CraneliftBackend::new().expect("backend"); |
| 180 | + cranelift.set_reloadable_calls(true); |
| 181 | + cranelift.compile_module(&module).expect("compile"); |
| 182 | + cranelift.finalize_definitions().expect("finalize"); |
| 183 | + |
| 184 | + let context = Context::create(); |
| 185 | + let mut llvm = LLVMJitBackend::new(&context).expect("backend"); |
| 186 | + llvm.set_use_mcjit(true); |
| 187 | + llvm.set_module_context(Arc::new(module.clone())); |
| 188 | + llvm.set_cross_tier_links(cranelift.reload_key(), Arc::new(|_| None)); |
| 189 | + llvm.compile_function(main.id, &main) |
| 190 | + .expect("LLVM compiles the caller"); |
| 191 | + let entry = llvm.get_function_pointer(main.id).expect("main compiled"); |
| 192 | + let run: unsafe extern "C" fn() -> i64 = unsafe { std::mem::transmute(entry) }; |
| 193 | + assert_eq!(unsafe { run() }, PICK_EXPECTED); |
| 194 | +} |
0 commit comments