2021-04-30 12:49:58 +00:00
|
|
|
//! The JIT driver uses [`cranelift_jit`] to JIT execute programs without writing any object
|
2020-09-23 13:13:49 +00:00
|
|
|
//! files.
|
|
|
|
|
2020-12-27 09:30:38 +00:00
|
|
|
use std::cell::RefCell;
|
2020-03-12 10:44:27 +00:00
|
|
|
use std::ffi::CString;
|
|
|
|
use std::os::raw::{c_char, c_int};
|
2021-07-07 09:14:20 +00:00
|
|
|
use std::sync::{mpsc, Mutex};
|
2020-03-12 10:44:27 +00:00
|
|
|
|
|
|
|
use rustc_codegen_ssa::CrateInfo;
|
2020-12-27 09:30:38 +00:00
|
|
|
use rustc_middle::mir::mono::MonoItem;
|
2021-07-07 09:14:20 +00:00
|
|
|
use rustc_session::Session;
|
2021-12-20 17:56:35 +00:00
|
|
|
use rustc_span::Symbol;
|
2020-03-12 10:44:27 +00:00
|
|
|
|
2020-12-27 09:30:38 +00:00
|
|
|
use cranelift_jit::{JITBuilder, JITModule};
|
2020-10-11 09:31:36 +00:00
|
|
|
|
2022-06-16 15:39:39 +00:00
|
|
|
// FIXME use std::sync::OnceLock once it stabilizes
|
2022-03-20 15:55:21 +00:00
|
|
|
use once_cell::sync::OnceCell;
|
|
|
|
|
2021-03-05 18:12:59 +00:00
|
|
|
use crate::{prelude::*, BackendConfig};
|
2020-12-27 09:30:38 +00:00
|
|
|
use crate::{CodegenCx, CodegenMode};
|
2020-03-12 10:44:27 +00:00
|
|
|
|
2021-04-30 12:49:58 +00:00
|
|
|
struct JitState {
|
|
|
|
backend_config: BackendConfig,
|
|
|
|
jit_module: JITModule,
|
2020-12-27 09:30:38 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 12:49:58 +00:00
|
|
|
thread_local! {
|
2021-12-20 17:56:35 +00:00
|
|
|
static LAZY_JIT_STATE: RefCell<Option<JitState>> = const { RefCell::new(None) };
|
2021-04-30 12:49:58 +00:00
|
|
|
}
|
2020-03-12 10:44:27 +00:00
|
|
|
|
2021-07-07 09:14:20 +00:00
|
|
|
/// The Sender owned by the rustc thread
|
2022-03-20 15:55:21 +00:00
|
|
|
static GLOBAL_MESSAGE_SENDER: OnceCell<Mutex<mpsc::Sender<UnsafeMessage>>> = OnceCell::new();
|
2021-07-07 09:14:20 +00:00
|
|
|
|
|
|
|
/// A message that is sent from the jitted runtime to the rustc thread.
|
|
|
|
/// Senders are responsible for upholding `Send` semantics.
|
|
|
|
enum UnsafeMessage {
|
|
|
|
/// Request that the specified `Instance` be lazily jitted.
|
|
|
|
///
|
|
|
|
/// Nothing accessible through `instance_ptr` may be moved or mutated by the sender after
|
|
|
|
/// this message is sent.
|
|
|
|
JitFn {
|
|
|
|
instance_ptr: *const Instance<'static>,
|
|
|
|
trampoline_ptr: *const u8,
|
|
|
|
tx: mpsc::Sender<*const u8>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
unsafe impl Send for UnsafeMessage {}
|
|
|
|
|
|
|
|
impl UnsafeMessage {
|
|
|
|
/// Send the message.
|
|
|
|
fn send(self) -> Result<(), mpsc::SendError<UnsafeMessage>> {
|
|
|
|
thread_local! {
|
|
|
|
/// The Sender owned by the local thread
|
2021-12-20 17:56:35 +00:00
|
|
|
static LOCAL_MESSAGE_SENDER: mpsc::Sender<UnsafeMessage> =
|
2021-07-07 09:14:20 +00:00
|
|
|
GLOBAL_MESSAGE_SENDER
|
|
|
|
.get().unwrap()
|
|
|
|
.lock().unwrap()
|
2021-12-20 17:56:35 +00:00
|
|
|
.clone();
|
2021-07-07 09:14:20 +00:00
|
|
|
}
|
|
|
|
LOCAL_MESSAGE_SENDER.with(|sender| sender.send(self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 15:25:26 +00:00
|
|
|
fn create_jit_module(
|
|
|
|
tcx: TyCtxt<'_>,
|
2021-04-30 12:49:58 +00:00
|
|
|
backend_config: &BackendConfig,
|
|
|
|
hotswap: bool,
|
2022-08-18 15:25:26 +00:00
|
|
|
) -> (JITModule, CodegenCx) {
|
2021-07-07 09:14:20 +00:00
|
|
|
let crate_info = CrateInfo::new(tcx, "dummy_target_cpu".to_string());
|
2020-03-12 10:44:27 +00:00
|
|
|
|
2021-04-30 12:49:58 +00:00
|
|
|
let isa = crate::build_isa(tcx.sess, backend_config);
|
|
|
|
let mut jit_builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names());
|
|
|
|
jit_builder.hotswap(hotswap);
|
2021-03-29 08:45:09 +00:00
|
|
|
crate::compiler_builtins::register_functions_for_jit(&mut jit_builder);
|
2022-08-28 08:43:19 +00:00
|
|
|
jit_builder.symbol_lookup_fn(dep_symbol_lookup_fn(tcx.sess, crate_info));
|
2022-05-15 10:32:19 +00:00
|
|
|
jit_builder.symbol("__clif_jit_fn", clif_jit_fn as *const u8);
|
2020-12-27 09:30:38 +00:00
|
|
|
let mut jit_module = JITModule::new(jit_builder);
|
2021-04-30 12:49:58 +00:00
|
|
|
|
2021-12-20 17:56:35 +00:00
|
|
|
let mut cx = crate::CodegenCx::new(
|
|
|
|
tcx,
|
|
|
|
backend_config.clone(),
|
|
|
|
jit_module.isa(),
|
|
|
|
false,
|
|
|
|
Symbol::intern("dummy_cgu_name"),
|
|
|
|
);
|
2021-04-30 12:49:58 +00:00
|
|
|
|
|
|
|
crate::allocator::codegen(tcx, &mut jit_module, &mut cx.unwind_context);
|
|
|
|
crate::main_shim::maybe_create_entry_wrapper(
|
|
|
|
tcx,
|
|
|
|
&mut jit_module,
|
|
|
|
&mut cx.unwind_context,
|
|
|
|
true,
|
2021-05-27 11:08:14 +00:00
|
|
|
true,
|
2021-04-30 12:49:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
(jit_module, cx)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
|
|
|
|
if !tcx.sess.opts.output_types.should_codegen() {
|
|
|
|
tcx.sess.fatal("JIT mode doesn't work with `cargo check`");
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tcx.sess.crate_types().contains(&rustc_session::config::CrateType::Executable) {
|
|
|
|
tcx.sess.fatal("can't jit non-executable crate");
|
|
|
|
}
|
|
|
|
|
|
|
|
let (mut jit_module, mut cx) = create_jit_module(
|
|
|
|
tcx,
|
|
|
|
&backend_config,
|
|
|
|
matches!(backend_config.codegen_mode, CodegenMode::JitLazy),
|
|
|
|
);
|
2022-08-10 18:47:05 +00:00
|
|
|
let mut cached_context = Context::new();
|
2020-03-12 10:44:27 +00:00
|
|
|
|
2021-05-11 12:39:04 +00:00
|
|
|
let (_, cgus) = tcx.collect_and_partition_mono_items(());
|
2020-03-12 10:44:27 +00:00
|
|
|
let mono_items = cgus
|
|
|
|
.iter()
|
|
|
|
.map(|cgu| cgu.items_in_deterministic_order(tcx).into_iter())
|
|
|
|
.flatten()
|
|
|
|
.collect::<FxHashMap<_, (_, _)>>()
|
|
|
|
.into_iter()
|
|
|
|
.collect::<Vec<(_, (_, _))>>();
|
|
|
|
|
2021-04-30 12:49:58 +00:00
|
|
|
super::time(tcx, backend_config.display_cg_time, "codegen mono items", || {
|
|
|
|
super::predefine_mono_items(tcx, &mut jit_module, &mono_items);
|
2021-03-29 08:45:09 +00:00
|
|
|
for (mono_item, _) in mono_items {
|
2020-12-27 09:30:38 +00:00
|
|
|
match mono_item {
|
2021-03-05 18:12:59 +00:00
|
|
|
MonoItem::Fn(inst) => match backend_config.codegen_mode {
|
2020-12-27 09:30:38 +00:00
|
|
|
CodegenMode::Aot => unreachable!(),
|
|
|
|
CodegenMode::Jit => {
|
2023-02-03 16:48:35 +00:00
|
|
|
codegen_and_compile_fn(
|
|
|
|
tcx,
|
|
|
|
&mut cx,
|
|
|
|
&mut cached_context,
|
|
|
|
&mut jit_module,
|
|
|
|
inst,
|
|
|
|
);
|
2020-12-27 09:30:38 +00:00
|
|
|
}
|
2022-08-10 18:47:05 +00:00
|
|
|
CodegenMode::JitLazy => {
|
2022-08-18 12:55:44 +00:00
|
|
|
codegen_shim(tcx, &mut cx, &mut cached_context, &mut jit_module, inst)
|
2022-08-10 18:47:05 +00:00
|
|
|
}
|
2020-12-27 09:30:38 +00:00
|
|
|
},
|
|
|
|
MonoItem::Static(def_id) => {
|
2021-04-30 12:49:58 +00:00
|
|
|
crate::constant::codegen_static(tcx, &mut jit_module, def_id);
|
2020-12-27 09:30:38 +00:00
|
|
|
}
|
2021-01-30 18:18:48 +00:00
|
|
|
MonoItem::GlobalAsm(item_id) => {
|
2021-04-30 12:49:58 +00:00
|
|
|
let item = tcx.hir().item(item_id);
|
2021-01-30 18:18:48 +00:00
|
|
|
tcx.sess.span_fatal(item.span, "Global asm is not supported in JIT mode");
|
2020-12-27 09:30:38 +00:00
|
|
|
}
|
2020-11-27 19:48:53 +00:00
|
|
|
}
|
2020-12-27 09:30:38 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-30 12:49:58 +00:00
|
|
|
if !cx.global_asm.is_empty() {
|
2020-12-27 09:30:38 +00:00
|
|
|
tcx.sess.fatal("Inline asm is not supported in JIT mode");
|
2020-07-09 17:24:53 +00:00
|
|
|
}
|
2020-12-27 09:30:38 +00:00
|
|
|
|
2020-10-11 09:31:36 +00:00
|
|
|
tcx.sess.abort_if_errors();
|
|
|
|
|
2022-11-10 10:47:43 +00:00
|
|
|
jit_module.finalize_definitions().unwrap();
|
2021-04-30 12:49:58 +00:00
|
|
|
unsafe { cx.unwind_context.register_jit(&jit_module) };
|
2020-05-01 18:57:51 +00:00
|
|
|
|
2021-03-05 18:12:59 +00:00
|
|
|
println!(
|
|
|
|
"Rustc codegen cranelift will JIT run the executable, because -Cllvm-args=mode=jit was passed"
|
|
|
|
);
|
2020-03-12 10:44:27 +00:00
|
|
|
|
2020-06-20 10:01:24 +00:00
|
|
|
let args = std::iter::once(&*tcx.crate_name(LOCAL_CRATE).as_str().to_string())
|
2021-04-30 12:49:58 +00:00
|
|
|
.chain(backend_config.jit_args.iter().map(|arg| &**arg))
|
2020-03-12 10:44:27 +00:00
|
|
|
.map(|arg| CString::new(arg).unwrap())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2021-04-30 12:49:58 +00:00
|
|
|
let start_sig = Signature {
|
|
|
|
params: vec![
|
|
|
|
AbiParam::new(jit_module.target_config().pointer_type()),
|
|
|
|
AbiParam::new(jit_module.target_config().pointer_type()),
|
|
|
|
],
|
|
|
|
returns: vec![AbiParam::new(jit_module.target_config().pointer_type() /*isize*/)],
|
2021-07-07 09:14:20 +00:00
|
|
|
call_conv: jit_module.target_config().default_call_conv,
|
2021-04-30 12:49:58 +00:00
|
|
|
};
|
|
|
|
let start_func_id = jit_module.declare_function("main", Linkage::Import, &start_sig).unwrap();
|
|
|
|
let finalized_start: *const u8 = jit_module.get_finalized_function(start_func_id);
|
|
|
|
|
|
|
|
LAZY_JIT_STATE.with(|lazy_jit_state| {
|
|
|
|
let mut lazy_jit_state = lazy_jit_state.borrow_mut();
|
|
|
|
assert!(lazy_jit_state.is_none());
|
|
|
|
*lazy_jit_state = Some(JitState { backend_config, jit_module });
|
2021-03-05 18:12:59 +00:00
|
|
|
});
|
2020-12-27 09:30:38 +00:00
|
|
|
|
2021-04-30 13:27:05 +00:00
|
|
|
let f: extern "C" fn(c_int, *const *const c_char) -> c_int =
|
|
|
|
unsafe { ::std::mem::transmute(finalized_start) };
|
2021-07-07 09:14:20 +00:00
|
|
|
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
GLOBAL_MESSAGE_SENDER.set(Mutex::new(tx)).unwrap();
|
|
|
|
|
|
|
|
// Spawn the jitted runtime in a new thread so that this rustc thread can handle messages
|
|
|
|
// (eg to lazily JIT further functions as required)
|
|
|
|
std::thread::spawn(move || {
|
|
|
|
let mut argv = args.iter().map(|arg| arg.as_ptr()).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// Push a null pointer as a terminating argument. This is required by POSIX and
|
|
|
|
// useful as some dynamic linkers use it as a marker to jump over.
|
|
|
|
argv.push(std::ptr::null());
|
|
|
|
|
|
|
|
let ret = f(args.len() as c_int, argv.as_ptr());
|
|
|
|
std::process::exit(ret);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handle messages
|
|
|
|
loop {
|
|
|
|
match rx.recv().unwrap() {
|
|
|
|
// lazy JIT compilation request - compile requested instance and return pointer to result
|
|
|
|
UnsafeMessage::JitFn { instance_ptr, trampoline_ptr, tx } => {
|
|
|
|
tx.send(jit_fn(instance_ptr, trampoline_ptr))
|
|
|
|
.expect("jitted runtime hung up before response to lazy JIT request was sent");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-12 10:44:27 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 16:48:35 +00:00
|
|
|
pub(crate) fn codegen_and_compile_fn<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
cx: &mut crate::CodegenCx,
|
|
|
|
cached_context: &mut Context,
|
|
|
|
module: &mut dyn Module,
|
|
|
|
instance: Instance<'tcx>,
|
|
|
|
) {
|
|
|
|
tcx.sess.time("codegen and compile fn", || {
|
|
|
|
let _inst_guard =
|
|
|
|
crate::PrintOnPanic(|| format!("{:?} {}", instance, tcx.symbol_name(instance).name));
|
|
|
|
|
|
|
|
let cached_func = std::mem::replace(&mut cached_context.func, Function::new());
|
|
|
|
let codegened_func = crate::base::codegen_fn(tcx, cx, cached_func, module, instance);
|
|
|
|
|
|
|
|
crate::base::compile_fn(cx, cached_context, module, codegened_func);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-15 10:32:19 +00:00
|
|
|
extern "C" fn clif_jit_fn(
|
2021-07-07 09:14:20 +00:00
|
|
|
instance_ptr: *const Instance<'static>,
|
|
|
|
trampoline_ptr: *const u8,
|
|
|
|
) -> *const u8 {
|
|
|
|
// send the JIT request to the rustc thread, with a channel for the response
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
UnsafeMessage::JitFn { instance_ptr, trampoline_ptr, tx }
|
|
|
|
.send()
|
|
|
|
.expect("rustc thread hung up before lazy JIT request was sent");
|
|
|
|
|
|
|
|
// block on JIT compilation result
|
|
|
|
rx.recv().expect("rustc thread hung up before responding to sent lazy JIT request")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn jit_fn(instance_ptr: *const Instance<'static>, trampoline_ptr: *const u8) -> *const u8 {
|
2020-12-27 09:30:38 +00:00
|
|
|
rustc_middle::ty::tls::with(|tcx| {
|
|
|
|
// lift is used to ensure the correct lifetime for instance.
|
|
|
|
let instance = tcx.lift(unsafe { *instance_ptr }).unwrap();
|
|
|
|
|
2021-04-30 12:49:58 +00:00
|
|
|
LAZY_JIT_STATE.with(|lazy_jit_state| {
|
|
|
|
let mut lazy_jit_state = lazy_jit_state.borrow_mut();
|
|
|
|
let lazy_jit_state = lazy_jit_state.as_mut().unwrap();
|
|
|
|
let jit_module = &mut lazy_jit_state.jit_module;
|
|
|
|
let backend_config = lazy_jit_state.backend_config.clone();
|
2020-12-27 09:30:38 +00:00
|
|
|
|
2021-04-30 12:49:58 +00:00
|
|
|
let name = tcx.symbol_name(instance).name;
|
2022-12-14 12:25:53 +00:00
|
|
|
let sig = crate::abi::get_function_sig(
|
|
|
|
tcx,
|
|
|
|
jit_module.target_config().default_call_conv,
|
|
|
|
instance,
|
|
|
|
);
|
2021-04-30 12:49:58 +00:00
|
|
|
let func_id = jit_module.declare_function(name, Linkage::Export, &sig).unwrap();
|
2021-07-07 09:14:20 +00:00
|
|
|
|
|
|
|
let current_ptr = jit_module.read_got_entry(func_id);
|
|
|
|
|
|
|
|
// If the function's GOT entry has already been updated to point at something other
|
|
|
|
// than the shim trampoline, don't re-jit but just return the new pointer instead.
|
|
|
|
// This does not need synchronization as this code is executed only by a sole rustc
|
|
|
|
// thread.
|
|
|
|
if current_ptr != trampoline_ptr {
|
|
|
|
return current_ptr;
|
|
|
|
}
|
|
|
|
|
2021-03-05 18:12:59 +00:00
|
|
|
jit_module.prepare_for_function_redefine(func_id).unwrap();
|
|
|
|
|
2021-12-20 17:56:35 +00:00
|
|
|
let mut cx = crate::CodegenCx::new(
|
|
|
|
tcx,
|
|
|
|
backend_config,
|
|
|
|
jit_module.isa(),
|
|
|
|
false,
|
|
|
|
Symbol::intern("dummy_cgu_name"),
|
|
|
|
);
|
2023-02-03 16:48:35 +00:00
|
|
|
codegen_and_compile_fn(tcx, &mut cx, &mut Context::new(), jit_module, instance);
|
2021-03-05 18:12:59 +00:00
|
|
|
|
2021-04-30 12:49:58 +00:00
|
|
|
assert!(cx.global_asm.is_empty());
|
2022-11-10 10:47:43 +00:00
|
|
|
jit_module.finalize_definitions().unwrap();
|
2021-04-30 12:49:58 +00:00
|
|
|
unsafe { cx.unwind_context.register_jit(&jit_module) };
|
2020-12-27 09:30:38 +00:00
|
|
|
jit_module.get_finalized_function(func_id)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-28 08:43:19 +00:00
|
|
|
fn dep_symbol_lookup_fn(
|
2021-07-07 09:14:20 +00:00
|
|
|
sess: &Session,
|
|
|
|
crate_info: CrateInfo,
|
2022-08-28 08:43:19 +00:00
|
|
|
) -> Box<dyn Fn(&str) -> Option<*const u8>> {
|
2020-03-31 11:20:19 +00:00
|
|
|
use rustc_middle::middle::dependency_format::Linkage;
|
2020-03-12 10:44:27 +00:00
|
|
|
|
|
|
|
let mut dylib_paths = Vec::new();
|
|
|
|
|
2021-07-07 09:14:20 +00:00
|
|
|
let data = &crate_info
|
|
|
|
.dependency_formats
|
2020-03-12 10:44:27 +00:00
|
|
|
.iter()
|
2020-03-27 11:14:45 +00:00
|
|
|
.find(|(crate_type, _data)| *crate_type == rustc_session::config::CrateType::Executable)
|
2020-03-12 10:44:27 +00:00
|
|
|
.unwrap()
|
|
|
|
.1;
|
2021-06-07 10:18:28 +00:00
|
|
|
for &cnum in &crate_info.used_crates {
|
2020-03-12 10:44:27 +00:00
|
|
|
let src = &crate_info.used_crate_source[&cnum];
|
|
|
|
match data[cnum.as_usize() - 1] {
|
|
|
|
Linkage::NotLinked | Linkage::IncludedFromDylib => {}
|
|
|
|
Linkage::Static => {
|
2022-04-05 12:52:53 +00:00
|
|
|
let name = crate_info.crate_name[&cnum];
|
|
|
|
let mut err = sess.struct_err(&format!("Can't load static lib {}", name));
|
2020-03-12 10:44:27 +00:00
|
|
|
err.note("rustc_codegen_cranelift can only load dylibs in JIT mode.");
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
Linkage::Dynamic => {
|
|
|
|
dylib_paths.push(src.dylib.as_ref().unwrap().0.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-28 08:43:19 +00:00
|
|
|
let imported_dylibs = Box::leak(
|
|
|
|
dylib_paths
|
|
|
|
.into_iter()
|
2022-09-01 15:36:12 +00:00
|
|
|
.map(|path| unsafe { libloading::Library::new(&path).unwrap() })
|
2022-08-28 08:43:19 +00:00
|
|
|
.collect::<Box<[_]>>(),
|
|
|
|
);
|
2020-03-12 10:44:27 +00:00
|
|
|
|
2021-07-07 09:14:20 +00:00
|
|
|
sess.abort_if_errors();
|
2020-03-12 10:44:27 +00:00
|
|
|
|
2022-08-28 08:43:19 +00:00
|
|
|
Box::new(move |sym_name| {
|
|
|
|
for dylib in &*imported_dylibs {
|
|
|
|
if let Ok(sym) = unsafe { dylib.get::<*const u8>(sym_name.as_bytes()) } {
|
|
|
|
return Some(*sym);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
})
|
2020-03-12 10:44:27 +00:00
|
|
|
}
|
2020-12-27 09:30:38 +00:00
|
|
|
|
2022-08-10 18:47:05 +00:00
|
|
|
fn codegen_shim<'tcx>(
|
2022-08-17 13:43:32 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2022-08-18 15:25:26 +00:00
|
|
|
cx: &mut CodegenCx,
|
2022-08-10 18:47:05 +00:00
|
|
|
cached_context: &mut Context,
|
|
|
|
module: &mut JITModule,
|
|
|
|
inst: Instance<'tcx>,
|
|
|
|
) {
|
2021-04-30 12:49:58 +00:00
|
|
|
let pointer_type = module.target_config().pointer_type();
|
2020-12-27 09:30:38 +00:00
|
|
|
|
2021-04-30 12:49:58 +00:00
|
|
|
let name = tcx.symbol_name(inst).name;
|
2022-12-14 12:25:53 +00:00
|
|
|
let sig = crate::abi::get_function_sig(tcx, module.target_config().default_call_conv, inst);
|
2021-04-30 12:49:58 +00:00
|
|
|
let func_id = module.declare_function(name, Linkage::Export, &sig).unwrap();
|
2020-12-27 09:30:38 +00:00
|
|
|
|
|
|
|
let instance_ptr = Box::into_raw(Box::new(inst));
|
|
|
|
|
2021-04-30 12:49:58 +00:00
|
|
|
let jit_fn = module
|
2020-12-27 09:30:38 +00:00
|
|
|
.declare_function(
|
|
|
|
"__clif_jit_fn",
|
|
|
|
Linkage::Import,
|
|
|
|
&Signature {
|
2021-04-30 12:49:58 +00:00
|
|
|
call_conv: module.target_config().default_call_conv,
|
2021-07-07 09:14:20 +00:00
|
|
|
params: vec![AbiParam::new(pointer_type), AbiParam::new(pointer_type)],
|
2020-12-27 09:30:38 +00:00
|
|
|
returns: vec![AbiParam::new(pointer_type)],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2022-08-10 18:47:05 +00:00
|
|
|
let context = cached_context;
|
|
|
|
context.clear();
|
|
|
|
let trampoline = &mut context.func;
|
2021-04-30 12:49:58 +00:00
|
|
|
trampoline.signature = sig.clone();
|
|
|
|
|
2020-12-27 09:30:38 +00:00
|
|
|
let mut builder_ctx = FunctionBuilderContext::new();
|
2021-04-30 12:49:58 +00:00
|
|
|
let mut trampoline_builder = FunctionBuilder::new(trampoline, &mut builder_ctx);
|
2020-12-27 09:30:38 +00:00
|
|
|
|
2021-07-07 09:14:20 +00:00
|
|
|
let trampoline_fn = module.declare_func_in_func(func_id, trampoline_builder.func);
|
2021-04-30 12:49:58 +00:00
|
|
|
let jit_fn = module.declare_func_in_func(jit_fn, trampoline_builder.func);
|
2020-12-27 09:30:38 +00:00
|
|
|
let sig_ref = trampoline_builder.func.import_signature(sig);
|
|
|
|
|
|
|
|
let entry_block = trampoline_builder.create_block();
|
|
|
|
trampoline_builder.append_block_params_for_function_params(entry_block);
|
2021-03-05 18:12:59 +00:00
|
|
|
let fn_args = trampoline_builder.func.dfg.block_params(entry_block).to_vec();
|
2020-12-27 09:30:38 +00:00
|
|
|
|
|
|
|
trampoline_builder.switch_to_block(entry_block);
|
2021-03-05 18:12:59 +00:00
|
|
|
let instance_ptr = trampoline_builder.ins().iconst(pointer_type, instance_ptr as u64 as i64);
|
2021-07-07 09:14:20 +00:00
|
|
|
let trampoline_ptr = trampoline_builder.ins().func_addr(pointer_type, trampoline_fn);
|
|
|
|
let jitted_fn = trampoline_builder.ins().call(jit_fn, &[instance_ptr, trampoline_ptr]);
|
2020-12-27 09:30:38 +00:00
|
|
|
let jitted_fn = trampoline_builder.func.dfg.inst_results(jitted_fn)[0];
|
2021-03-05 18:12:59 +00:00
|
|
|
let call_inst = trampoline_builder.ins().call_indirect(sig_ref, jitted_fn, &fn_args);
|
2020-12-27 09:30:38 +00:00
|
|
|
let ret_vals = trampoline_builder.func.dfg.inst_results(call_inst).to_vec();
|
|
|
|
trampoline_builder.ins().return_(&ret_vals);
|
|
|
|
|
2022-08-10 18:47:05 +00:00
|
|
|
module.define_function(func_id, context).unwrap();
|
2022-08-18 12:55:44 +00:00
|
|
|
cx.unwind_context.add_function(func_id, context, module.isa());
|
2020-12-27 09:30:38 +00:00
|
|
|
}
|