diff --git a/compiler/gen/src/llvm/build.rs b/compiler/gen/src/llvm/build.rs index 0218d8d584..e0c59b6c0c 100644 --- a/compiler/gen/src/llvm/build.rs +++ b/compiler/gen/src/llvm/build.rs @@ -24,6 +24,10 @@ const PRINT_FN_VERIFICATION_OUTPUT: bool = true; #[cfg(not(debug_assertions))] const PRINT_FN_VERIFICATION_OUTPUT: bool = false; +// 0 is the C calling convention - see https://llvm.org/doxygen/namespacellvm_1_1CallingConv.html +// TODO: experiment with different internal calling conventions, e.g. "fast" +const DEFAULT_CALLING_CONVENTION: u32 = 0; + type Scope<'a, 'ctx> = ImMap, PointerValue<'ctx>)>; pub struct Env<'a, 'ctx, 'env> { @@ -211,6 +215,8 @@ pub fn build_expr<'a, 'ctx, 'env>( } }; + call.set_call_convention(DEFAULT_CALLING_CONVENTION); + call.try_as_basic_value() .left() .unwrap_or_else(|| panic!("LLVM error: Invalid call by pointer.")) @@ -865,6 +871,8 @@ pub fn build_proc_header<'a, 'ctx, 'env>( Some(Linkage::Private), ); + fn_val.set_call_conventions(DEFAULT_CALLING_CONVENTION); + (fn_val, arg_basic_types) } @@ -1103,6 +1111,8 @@ fn call_with_args<'a, 'ctx, 'env>( .builder .build_call(fn_val, arg_vals.into_bump_slice(), "call"); + call.set_call_convention(DEFAULT_CALLING_CONVENTION); + call.try_as_basic_value() .left() .unwrap_or_else(|| panic!("LLVM error: Invalid call by name for name {:?}", symbol)) diff --git a/compiler/gen/tests/test_gen.rs b/compiler/gen/tests/test_gen.rs index a7f5fdb3f5..1aaf262f2b 100644 --- a/compiler/gen/tests/test_gen.rs +++ b/compiler/gen/tests/test_gen.rs @@ -31,6 +31,9 @@ mod test_gen { // Pointer size on current system const POINTER_SIZE: u32 = std::mem::size_of::() as u32; + // 0 is the C calling convention - see https://llvm.org/doxygen/namespacellvm_1_1CallingConv.html + const MAIN_CALLING_CONVENTION: u32 = 0; + macro_rules! assert_llvm_evals_to { ($src:expr, $expected:expr, $ty:ty, $transform:expr) => { let arena = Bump::new(); @@ -128,6 +131,8 @@ mod test_gen { // Add main to the module. let main_fn = env.module.add_function(main_fn_name, main_fn_type, None); + main_fn.set_call_conventions(MAIN_CALLING_CONVENTION); + // Add main's body let basic_block = context.append_basic_block(main_fn, "entry"); @@ -272,6 +277,8 @@ mod test_gen { // Add main to the module. let main_fn = env.module.add_function(main_fn_name, main_fn_type, None); + main_fn.set_call_conventions(MAIN_CALLING_CONVENTION); + // Add main's body let basic_block = context.append_basic_block(main_fn, "entry");