Skip to content

Commit 7cce6af

Browse files
committed
Make __rust_alloc_error_handler_should_panic a function
1 parent 44b7484 commit 7cce6af

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

src/allocator.rs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use gccjit::{Context, FunctionType, GlobalKind, ToRValue, Type};
21
#[cfg(feature = "master")]
3-
use gccjit::{FnAttribute, VarAttribute};
2+
use gccjit::FnAttribute;
3+
use gccjit::{Context, FunctionType, RValue, ToRValue, Type};
44
use rustc_ast::expand::allocator::{
55
ALLOCATOR_METHODS, AllocatorKind, AllocatorTy, NO_ALLOC_SHIM_IS_UNSTABLE,
66
alloc_error_handler_name, default_fn_name, global_fn_name,
@@ -71,15 +71,13 @@ pub(crate) unsafe fn codegen(
7171
None,
7272
);
7373

74-
let name = mangle_internal_symbol(tcx, OomStrategy::SYMBOL);
75-
let global = context.new_global(None, GlobalKind::Exported, i8, name);
76-
#[cfg(feature = "master")]
77-
global.add_attribute(VarAttribute::Visibility(symbol_visibility_to_gcc(
78-
tcx.sess.default_visibility(),
79-
)));
80-
let value = tcx.sess.opts.unstable_opts.oom.should_panic();
81-
let value = context.new_rvalue_from_int(i8, value as i32);
82-
global.global_set_initializer_rvalue(value);
74+
create_const_value_function(
75+
tcx,
76+
context,
77+
&mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
78+
i8,
79+
context.new_rvalue_from_int(i8, tcx.sess.opts.unstable_opts.oom.should_panic() as i32),
80+
);
8381

8482
create_wrapper_function(
8583
tcx,
@@ -91,6 +89,30 @@ pub(crate) unsafe fn codegen(
9189
);
9290
}
9391

92+
fn create_const_value_function(
93+
tcx: TyCtxt<'_>,
94+
context: &Context<'_>,
95+
name: &str,
96+
output: Type<'_>,
97+
value: RValue<'_>,
98+
) {
99+
let func = context.new_function(None, FunctionType::Exported, output, &[], name, false);
100+
101+
#[cfg(feature = "master")]
102+
func.add_attribute(FnAttribute::Visibility(symbol_visibility_to_gcc(
103+
tcx.sess.default_visibility(),
104+
)));
105+
106+
func.add_attribute(FnAttribute::AlwaysInline);
107+
108+
if tcx.sess.must_emit_unwind_tables() {
109+
// TODO(antoyo): emit unwind tables.
110+
}
111+
112+
let block = func.new_block("entry");
113+
block.end_with_return(None, value);
114+
}
115+
94116
fn create_wrapper_function(
95117
tcx: TyCtxt<'_>,
96118
context: &Context<'_>,

0 commit comments

Comments
 (0)