1 use gccjit::{FunctionType, ToRValue};
2 use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
4 use rustc_middle::ty::TyCtxt;
5 use rustc_span::symbol::sym;
9 pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_name: &str, kind: AllocatorKind, has_alloc_error_handler: bool) {
10 let context = &mods.context;
12 match tcx.sess.target.pointer_width {
13 16 => context.new_type::<u16>(),
14 32 => context.new_type::<u32>(),
15 64 => context.new_type::<u64>(),
16 tws => bug!("Unsupported target word size for int: {}", tws),
18 let i8 = context.new_type::<i8>();
19 let i8p = i8.make_pointer();
20 let void = context.new_type::<()>();
22 for method in ALLOCATOR_METHODS {
23 let mut types = Vec::with_capacity(method.inputs.len());
24 for ty in method.inputs.iter() {
26 AllocatorTy::Layout => {
30 AllocatorTy::Ptr => types.push(i8p),
31 AllocatorTy::Usize => types.push(usize),
33 AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
36 let output = match method.output {
37 AllocatorTy::ResultPtr => Some(i8p),
38 AllocatorTy::Unit => None,
40 AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
41 panic!("invalid allocator output")
44 let name = format!("__rust_{}", method.name);
46 let args: Vec<_> = types.iter().enumerate()
47 .map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
49 let func = context.new_function(None, FunctionType::Exported, output.unwrap_or(void), &args, name, false);
51 if tcx.sess.target.options.default_hidden_visibility {
52 // TODO(antoyo): set visibility.
54 if tcx.sess.must_emit_unwind_tables() {
55 // TODO(antoyo): emit unwind tables.
58 let callee = kind.fn_name(method.name);
59 let args: Vec<_> = types.iter().enumerate()
60 .map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
62 let callee = context.new_function(None, FunctionType::Extern, output.unwrap_or(void), &args, callee, false);
63 // TODO(antoyo): set visibility.
65 let block = func.new_block("entry");
70 .map(|(i, _)| func.get_param(i as i32).to_rvalue())
72 let ret = context.new_call(None, callee, &args);
73 //llvm::LLVMSetTailCall(ret, True);
75 block.end_with_return(None, ret);
78 block.end_with_void_return(None);
81 // TODO(@Commeownist): Check if we need to emit some extra debugging info in certain circumstances
82 // as described in https://github.com/rust-lang/rust/commit/77a96ed5646f7c3ee8897693decc4626fe380643
85 let types = [usize, usize];
86 let name = "__rust_alloc_error_handler".to_string();
87 let args: Vec<_> = types.iter().enumerate()
88 .map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
90 let func = context.new_function(None, FunctionType::Exported, void, &args, name, false);
93 if has_alloc_error_handler {
97 AllocatorKind::Default
99 let callee = kind.fn_name(sym::oom);
100 let args: Vec<_> = types.iter().enumerate()
101 .map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
103 let callee = context.new_function(None, FunctionType::Extern, void, &args, callee, false);
104 //llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
106 let block = func.new_block("entry");
111 .map(|(i, _)| func.get_param(i as i32).to_rvalue())
112 .collect::<Vec<_>>();
113 let _ret = context.new_call(None, callee, &args);
114 //llvm::LLVMSetTailCall(ret, True);
115 block.end_with_void_return(None);