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, 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);
82 let types = [usize, usize];
83 let name = "__rust_alloc_error_handler".to_string();
84 let args: Vec<_> = types.iter().enumerate()
85 .map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
87 let func = context.new_function(None, FunctionType::Exported, void, &args, name, false);
90 if has_alloc_error_handler {
94 AllocatorKind::Default
96 let callee = kind.fn_name(sym::oom);
97 let args: Vec<_> = types.iter().enumerate()
98 .map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
100 let callee = context.new_function(None, FunctionType::Extern, void, &args, callee, false);
101 //llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
103 let block = func.new_block("entry");
108 .map(|(i, _)| func.get_param(i as i32).to_rvalue())
109 .collect::<Vec<_>>();
110 let _ret = context.new_call(None, callee, &args);
111 //llvm::LLVMSetTailCall(ret, True);
112 block.end_with_void_return(None);