1 //! Expanding instructions as runtime library calls.
2
3 use crate::ir;
4 use crate::ir::{get_libcall_funcref, InstBuilder};
5 use crate::isa::{CallConv, TargetIsa};
6 use crate::legalizer::boundary::legalize_libcall_signature;
7 use std::vec::Vec;
8
9 /// Try to expand `inst` as a library call, returning true is successful.
expand_as_libcall(inst: ir::Inst, func: &mut ir::Function, isa: &dyn TargetIsa) -> bool10 pub fn expand_as_libcall(inst: ir::Inst, func: &mut ir::Function, isa: &dyn TargetIsa) -> bool {
11 // Does the opcode/ctrl_type combo even have a well-known runtime library name.
12 let libcall = match ir::LibCall::for_inst(func.dfg[inst].opcode(), func.dfg.ctrl_typevar(inst))
13 {
14 Some(lc) => lc,
15 None => return false,
16 };
17
18 // Now we convert `inst` to a call. First save the arguments.
19 let mut args = Vec::new();
20 args.extend_from_slice(func.dfg.inst_args(inst));
21
22 let call_conv = CallConv::for_libcall(isa);
23 if call_conv.extends_baldrdash() {
24 let vmctx = func
25 .special_param(ir::ArgumentPurpose::VMContext)
26 .expect("Missing vmctx parameter for baldrdash libcall");
27 args.push(vmctx);
28 }
29
30 // The replace builder will preserve the instruction result values.
31 let funcref = get_libcall_funcref(libcall, call_conv, func, inst, isa);
32 func.dfg.replace(inst).call(funcref, &args);
33
34 // Ask the ISA to legalize the signature.
35 let fn_data = &func.dfg.ext_funcs[funcref];
36 let sig_data = &mut func.dfg.signatures[fn_data.signature];
37 legalize_libcall_signature(sig_data, isa);
38
39 true
40 }
41