Skip to content

Commit e238ea6

Browse files
authored
Merge pull request #1350 from bjorn3/inline_asm_sym
Implement const and sym operands for inline asm
2 parents 8b48138 + a2719a2 commit e238ea6

File tree

3 files changed

+195
-107
lines changed

3 files changed

+195
-107
lines changed

src/allocator.rs

Lines changed: 14 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -70,74 +70,27 @@ fn codegen_inner(
7070
params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
7171
returns: output.into_iter().map(AbiParam::new).collect(),
7272
};
73-
74-
let caller_name = format!("__rust_{}", method.name);
75-
let callee_name = kind.fn_name(method.name);
76-
77-
let func_id = module.declare_function(&caller_name, Linkage::Export, &sig).unwrap();
78-
79-
let callee_func_id = module.declare_function(&callee_name, Linkage::Import, &sig).unwrap();
80-
81-
let mut ctx = Context::new();
82-
ctx.func.signature = sig.clone();
83-
{
84-
let mut func_ctx = FunctionBuilderContext::new();
85-
let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
86-
87-
let block = bcx.create_block();
88-
bcx.switch_to_block(block);
89-
let args = arg_tys
90-
.into_iter()
91-
.map(|ty| bcx.append_block_param(block, ty))
92-
.collect::<Vec<Value>>();
93-
94-
let callee_func_ref = module.declare_func_in_func(callee_func_id, &mut bcx.func);
95-
let call_inst = bcx.ins().call(callee_func_ref, &args);
96-
let results = bcx.inst_results(call_inst).to_vec(); // Clone to prevent borrow error
97-
98-
bcx.ins().return_(&results);
99-
bcx.seal_all_blocks();
100-
bcx.finalize();
101-
}
102-
module.define_function(func_id, &mut ctx).unwrap();
103-
unwind_context.add_function(func_id, &ctx, module.isa());
73+
crate::common::create_wrapper_function(
74+
module,
75+
unwind_context,
76+
sig,
77+
&format!("__rust_{}", method.name),
78+
&kind.fn_name(method.name),
79+
);
10480
}
10581

10682
let sig = Signature {
10783
call_conv: module.target_config().default_call_conv,
10884
params: vec![AbiParam::new(usize_ty), AbiParam::new(usize_ty)],
10985
returns: vec![],
11086
};
111-
112-
let callee_name = alloc_error_handler_kind.fn_name(sym::oom);
113-
114-
let func_id =
115-
module.declare_function("__rust_alloc_error_handler", Linkage::Export, &sig).unwrap();
116-
117-
let callee_func_id = module.declare_function(&callee_name, Linkage::Import, &sig).unwrap();
118-
119-
let mut ctx = Context::new();
120-
ctx.func.signature = sig;
121-
{
122-
let mut func_ctx = FunctionBuilderContext::new();
123-
let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
124-
125-
let block = bcx.create_block();
126-
bcx.switch_to_block(block);
127-
let args = (&[usize_ty, usize_ty])
128-
.iter()
129-
.map(|&ty| bcx.append_block_param(block, ty))
130-
.collect::<Vec<Value>>();
131-
132-
let callee_func_ref = module.declare_func_in_func(callee_func_id, &mut bcx.func);
133-
bcx.ins().call(callee_func_ref, &args);
134-
135-
bcx.ins().trap(TrapCode::UnreachableCodeReached);
136-
bcx.seal_all_blocks();
137-
bcx.finalize();
138-
}
139-
module.define_function(func_id, &mut ctx).unwrap();
140-
unwind_context.add_function(func_id, &ctx, module.isa());
87+
crate::common::create_wrapper_function(
88+
module,
89+
unwind_context,
90+
sig,
91+
"__rust_alloc_error_handler",
92+
&alloc_error_handler_kind.fn_name(sym::oom),
93+
);
14194

14295
let data_id = module.declare_data(OomStrategy::SYMBOL, Linkage::Export, false, false).unwrap();
14396
let mut data_ctx = DataContext::new();

src/common.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,44 @@ pub(crate) fn type_sign(ty: Ty<'_>) -> bool {
254254
}
255255
}
256256

257+
pub(crate) fn create_wrapper_function(
258+
module: &mut dyn Module,
259+
unwind_context: &mut UnwindContext,
260+
sig: Signature,
261+
wrapper_name: &str,
262+
callee_name: &str,
263+
) {
264+
let wrapper_func_id = module.declare_function(wrapper_name, Linkage::Export, &sig).unwrap();
265+
let callee_func_id = module.declare_function(callee_name, Linkage::Import, &sig).unwrap();
266+
267+
let mut ctx = Context::new();
268+
ctx.func.signature = sig;
269+
{
270+
let mut func_ctx = FunctionBuilderContext::new();
271+
let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
272+
273+
let block = bcx.create_block();
274+
bcx.switch_to_block(block);
275+
let func = &mut bcx.func.stencil;
276+
let args = func
277+
.signature
278+
.params
279+
.iter()
280+
.map(|param| func.dfg.append_block_param(block, param.value_type))
281+
.collect::<Vec<Value>>();
282+
283+
let callee_func_ref = module.declare_func_in_func(callee_func_id, &mut bcx.func);
284+
let call_inst = bcx.ins().call(callee_func_ref, &args);
285+
let results = bcx.inst_results(call_inst).to_vec(); // Clone to prevent borrow error
286+
287+
bcx.ins().return_(&results);
288+
bcx.seal_all_blocks();
289+
bcx.finalize();
290+
}
291+
module.define_function(wrapper_func_id, &mut ctx).unwrap();
292+
unwind_context.add_function(wrapper_func_id, &ctx, module.isa());
293+
}
294+
257295
pub(crate) struct FunctionCx<'m, 'clif, 'tcx: 'm> {
258296
pub(crate) cx: &'clif mut crate::CodegenCx,
259297
pub(crate) module: &'m mut dyn Module,

0 commit comments

Comments
 (0)