@@ -16,10 +16,10 @@ use midenc_hir::{
16
16
use wasmparser:: { FunctionBody , Operator } ;
17
17
18
18
use crate :: {
19
- intrinsics:: Intrinsic ,
19
+ intrinsics:: { convert_intrinsics_call , Intrinsic } ,
20
20
miden_abi:: {
21
21
is_miden_abi_module, miden_abi_function_type,
22
- transform:: { no_transform , transform_miden_abi_call} ,
22
+ transform:: transform_miden_abi_call,
23
23
} ,
24
24
module:: {
25
25
function_builder_ext:: { FunctionBuilderContext , FunctionBuilderExt , SSABuilderListener } ,
@@ -81,16 +81,19 @@ pub fn maybe_lower_linker_stub(
81
81
return Ok ( false ) ;
82
82
}
83
83
84
- // MASM callee function type
85
- let import_sig = if is_intrinsic {
86
- // For intrinsics, use the same signature as the stub function
87
- function_ref. borrow ( ) . signature ( ) . clone ( )
88
- } else {
89
- let import_ft: FunctionType = miden_abi_function_type ( & import_path) ;
90
- Signature :: new (
91
- import_ft. params . into_iter ( ) . map ( AbiParam :: new) ,
92
- import_ft. results . into_iter ( ) . map ( AbiParam :: new) ,
93
- )
84
+ // Classify intrinsics and obtain signature when needed
85
+ let ( import_sig, intrinsic) : ( Signature , Option < Intrinsic > ) = match Intrinsic :: try_from ( & import_path) {
86
+ Ok ( intr) => ( function_ref. borrow ( ) . signature ( ) . clone ( ) , Some ( intr) ) ,
87
+ Err ( _) => {
88
+ let import_ft: FunctionType = miden_abi_function_type ( & import_path) ;
89
+ (
90
+ Signature :: new (
91
+ import_ft. params . into_iter ( ) . map ( AbiParam :: new) ,
92
+ import_ft. results . into_iter ( ) . map ( AbiParam :: new) ,
93
+ ) ,
94
+ None ,
95
+ )
96
+ }
94
97
} ;
95
98
96
99
// Build the function body for the stub and replace it with an exec to MASM
@@ -112,18 +115,40 @@ pub fn maybe_lower_linker_stub(
112
115
. collect ( ) ;
113
116
114
117
// Declare MASM import callee in world and exec via TransformStrategy
115
- let import_module_ref = module_state
116
- . world_builder
117
- . declare_module_tree ( & import_path. without_leaf ( ) )
118
- . wrap_err ( "failed to create module for MASM imports" ) ?;
119
- let mut import_module_builder = ModuleBuilder :: new ( import_module_ref) ;
120
- let import_func_ref = import_module_builder
121
- . define_function ( import_path. name ( ) . into ( ) , import_sig)
122
- . expect ( "failed to create MASM import function ref" ) ;
123
-
124
- let results = if is_intrinsic {
125
- no_transform ( import_func_ref, & args, & mut fb)
118
+ let results: Vec < ValueRef > = if let Some ( intr) = intrinsic {
119
+ // Decide whether the intrinsic is implemented as a function or an operation
120
+ let conv = intr
121
+ . conversion_result ( )
122
+ . expect ( "unknown intrinsic" ) ;
123
+ if conv. is_function ( ) {
124
+ // Declare callee and call via convert_intrinsics_call with function_ref
125
+ let import_module_ref = module_state
126
+ . world_builder
127
+ . declare_module_tree ( & import_path. without_leaf ( ) )
128
+ . wrap_err ( "failed to create module for intrinsics imports" ) ?;
129
+ let mut import_module_builder = ModuleBuilder :: new ( import_module_ref) ;
130
+ let intrinsic_func_ref = import_module_builder
131
+ . define_function ( import_path. name ( ) . into ( ) , import_sig. clone ( ) )
132
+ . expect ( "failed to create intrinsic function ref" ) ;
133
+ convert_intrinsics_call ( intr, Some ( intrinsic_func_ref) , & args, & mut fb, span)
134
+ . expect ( "convert_intrinsics_call failed" )
135
+ . to_vec ( )
136
+ } else {
137
+ // Inline conversion of intrinsic operation
138
+ convert_intrinsics_call ( intr, None , & args, & mut fb, span)
139
+ . expect ( "convert_intrinsics_call failed" )
140
+ . to_vec ( )
141
+ }
126
142
} else {
143
+ // Miden ABI path: exec import with TransformStrategy
144
+ let import_module_ref = module_state
145
+ . world_builder
146
+ . declare_module_tree ( & import_path. without_leaf ( ) )
147
+ . wrap_err ( "failed to create module for MASM imports" ) ?;
148
+ let mut import_module_builder = ModuleBuilder :: new ( import_module_ref) ;
149
+ let import_func_ref = import_module_builder
150
+ . define_function ( import_path. name ( ) . into ( ) , import_sig)
151
+ . expect ( "failed to create MASM import function ref" ) ;
127
152
transform_miden_abi_call ( import_func_ref, & import_path, & args, & mut fb)
128
153
} ;
129
154
0 commit comments