1 use gccjit::{FunctionType, GlobalKind, ToRValue};
2 use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
4 use rustc_middle::ty::TyCtxt;
5 use rustc_session::config::OomStrategy;
6 use rustc_span::symbol::sym;
10 pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_name: &str, kind: AllocatorKind, alloc_error_handler_kind: AllocatorKind) {
11 let context = &mods.context;
13 match tcx.sess.target.pointer_width {
14 16 => context.new_type::<u16>(),
15 32 => context.new_type::<u32>(),
16 64 => context.new_type::<u64>(),
17 tws => bug!("Unsupported target word size for int: {}", tws),
19 let i8 = context.new_type::<i8>();
20 let i8p = i8.make_pointer();
21 let void = context.new_type::<()>();
23 for method in ALLOCATOR_METHODS {
24 let mut types = Vec::with_capacity(method.inputs.len());
25 for ty in method.inputs.iter() {
27 AllocatorTy::Layout => {
31 AllocatorTy::Ptr => types.push(i8p),
32 AllocatorTy::Usize => types.push(usize),
34 AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
37 let output = match method.output {
38 AllocatorTy::ResultPtr => Some(i8p),
39 AllocatorTy::Unit => None,
41 AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
42 panic!("invalid allocator output")
45 let name = format!("__rust_{}", method.name);
47 let args: Vec<_> = types.iter().enumerate()
48 .map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
50 let func = context.new_function(None, FunctionType::Exported, output.unwrap_or(void), &args, name, false);
52 if tcx.sess.target.options.default_hidden_visibility {
53 // TODO(antoyo): set visibility.
55 if tcx.sess.must_emit_unwind_tables() {
56 // TODO(antoyo): emit unwind tables.
59 let callee = kind.fn_name(method.name);
60 let args: Vec<_> = types.iter().enumerate()
61 .map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
63 let callee = context.new_function(None, FunctionType::Extern, output.unwrap_or(void), &args, callee, false);
64 // TODO(antoyo): set visibility.
66 let block = func.new_block("entry");
71 .map(|(i, _)| func.get_param(i as i32).to_rvalue())
73 let ret = context.new_call(None, callee, &args);
74 //llvm::LLVMSetTailCall(ret, True);
76 block.end_with_return(None, ret);
79 block.end_with_void_return(None);
82 // TODO(@Commeownist): Check if we need to emit some extra debugging info in certain circumstances
83 // as described in https://github.com/rust-lang/rust/commit/77a96ed5646f7c3ee8897693decc4626fe380643
86 let types = [usize, usize];
87 let name = "__rust_alloc_error_handler".to_string();
88 let args: Vec<_> = types.iter().enumerate()
89 .map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
91 let func = context.new_function(None, FunctionType::Exported, void, &args, name, false);
93 let callee = alloc_error_handler_kind.fn_name(sym::oom);
94 let args: Vec<_> = types.iter().enumerate()
95 .map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
97 let callee = context.new_function(None, FunctionType::Extern, void, &args, callee, false);
98 //llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
100 let block = func.new_block("entry");
105 .map(|(i, _)| func.get_param(i as i32).to_rvalue())
106 .collect::<Vec<_>>();
107 let _ret = context.new_call(None, callee, &args);
108 //llvm::LLVMSetTailCall(ret, True);
109 block.end_with_void_return(None);
111 let name = OomStrategy::SYMBOL.to_string();
112 let global = context.new_global(None, GlobalKind::Exported, i8, name);
113 let value = tcx.sess.opts.unstable_opts.oom.should_panic();
114 let value = context.new_rvalue_from_int(i8, value as i32);
115 global.global_set_initializer_rvalue(value);