Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9a3d98d

Browse files
committed
Call Termination::report on main result in jit mode
1 parent f3b5e14 commit 9a3d98d

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/main_shim.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use cranelift_codegen::binemit::{NullStackMapSink, NullTrapSink};
22
use rustc_hir::LangItem;
3+
use rustc_middle::ty::subst::GenericArg;
4+
use rustc_middle::ty::AssocKind;
35
use rustc_session::config::EntryFnType;
6+
use rustc_span::symbol::Ident;
47

58
use crate::prelude::*;
69

@@ -79,8 +82,38 @@ pub(crate) fn maybe_create_entry_wrapper(
7982
let result = if is_main_fn && ignore_lang_start_wrapper {
8083
// regular main fn, but ignoring #[lang = "start"] as we are running in the jit
8184
// FIXME set program arguments somehow
82-
bcx.ins().call(main_func_ref, &[]);
83-
bcx.ins().iconst(m.target_config().pointer_type(), 0)
85+
let call_inst = bcx.ins().call(main_func_ref, &[]);
86+
let call_results = bcx.func.dfg.inst_results(call_inst).to_owned();
87+
88+
let termination_trait = tcx.require_lang_item(LangItem::Termination, None);
89+
let report = tcx
90+
.associated_items(termination_trait)
91+
.find_by_name_and_kind(
92+
tcx,
93+
Ident::from_str("report"),
94+
AssocKind::Fn,
95+
termination_trait,
96+
)
97+
.unwrap();
98+
let report = Instance::resolve(
99+
tcx,
100+
ParamEnv::reveal_all(),
101+
report.def_id,
102+
tcx.mk_substs([GenericArg::from(main_ret_ty)].iter()),
103+
)
104+
.unwrap()
105+
.unwrap();
106+
107+
let report_name = tcx.symbol_name(report).name;
108+
let report_sig = get_function_sig(tcx, m.isa().triple(), report);
109+
let report_func_id =
110+
m.declare_function(report_name, Linkage::Import, &report_sig).unwrap();
111+
let report_func_ref = m.declare_func_in_func(report_func_id, &mut bcx.func);
112+
113+
// FIXME do proper abi handling instead of expecting the pass mode to be identical
114+
// for returns and arguments.
115+
let report_call_inst = bcx.ins().call(report_func_ref, &call_results);
116+
bcx.func.dfg.inst_results(report_call_inst)[0]
84117
} else if is_main_fn {
85118
let start_def_id = tcx.require_lang_item(LangItem::Start, None);
86119
let start_instance = Instance::resolve(

0 commit comments

Comments
 (0)