Skip to content

Commit 5387788

Browse files
committed
feat: use ZRTL signature return types for extern function calls
Updated Cranelift backend to prefer symbol signature return types over HIR types when calling external functions. This ensures void functions are correctly declared with no return value instead of defaulting to i32. Changes: - Check symbol_signatures for return type before falling back to HIR type - Compare return type against TypeTag::VOID to determine if function returns void - Functions with void signature now generate correct Cranelift IR with no returns Remaining work: - DynamicBox wrapping currently hardcodes TypeCategory::Opaque (0x12) - Need to detect actual type and use correct TypeTag for primitives - For i32: should use TypeCategory::Int with appropriate type_id - For opaque types: current implementation is correct - May need to allocate storage for primitive values and store pointer
1 parent 1ef7881 commit 5387788

1 file changed

Lines changed: 21 additions & 13 deletions

File tree

crates/compiler/src/cranelift_backend.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,21 +1023,29 @@ impl CraneliftBackend {
10231023
sig.params.push(cranelift_codegen::ir::AbiParam::new(arg_ty));
10241024
}
10251025

1026-
// Determine return type from the result value's HIR type
1027-
let return_cranelift_ty = if let Some(result_id) = result {
1028-
// Look up the HIR type for the result value
1026+
// Determine return type - prefer signature info over HIR type
1027+
let return_cranelift_ty = if let Some(sym_sig) = self.symbol_signatures.get(symbol_name) {
1028+
// Use signature return type if available
1029+
if sym_sig.return_type == crate::zrtl::TypeTag::VOID {
1030+
None
1031+
} else {
1032+
// For now, all non-void returns are i64 (pointers/handles)
1033+
Some(types::I64)
1034+
}
1035+
} else if let Some(result_id) = result {
1036+
// Fall back to HIR type
10291037
if let Some(value) = function.values.get(result_id) {
10301038
// Map HIR type to Cranelift type inline
1031-
let ty = match &value.ty {
1032-
HirType::I32 => types::I32,
1033-
HirType::I64 => types::I64,
1034-
HirType::F32 => types::F32,
1035-
HirType::F64 => types::F64,
1036-
HirType::Bool => types::I8,
1037-
HirType::Ptr(_) => types::I64,
1038-
_ => types::I64, // Default to i64 for handles/complex types
1039-
};
1040-
Some(ty)
1039+
match &value.ty {
1040+
HirType::I32 => Some(types::I32),
1041+
HirType::I64 => Some(types::I64),
1042+
HirType::F32 => Some(types::F32),
1043+
HirType::F64 => Some(types::F64),
1044+
HirType::Bool => Some(types::I8),
1045+
HirType::Ptr(_) => Some(types::I64),
1046+
HirType::Void => None,
1047+
_ => Some(types::I64), // Default to i64 for handles/complex types
1048+
}
10411049
} else {
10421050
// Default to i64 for unknown external call results
10431051
Some(types::I64)

0 commit comments

Comments
 (0)