1 //use crate::attributes;
2 use gccjit::{FunctionType, ToRValue};
3 use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
5 use rustc_middle::ty::TyCtxt;
6 use rustc_span::symbol::sym;
10 pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, kind: AllocatorKind, has_alloc_error_handler: bool) {
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 //llvm::LLVMRustSetVisibility(func, llvm::Visibility::Hidden);
55 if tcx.sess.must_emit_unwind_tables() {
57 //attributes::emit_uwtable(func, true);
60 let callee = kind.fn_name(method.name);
61 let args: Vec<_> = types.iter().enumerate()
62 .map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
64 let callee = context.new_function(None, FunctionType::Extern, output.unwrap_or(void), &args, callee, false);
65 //llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
67 let block = func.new_block("entry");
72 .map(|(i, _)| func.get_param(i as i32).to_rvalue())
74 let ret = context.new_call(None, callee, &args);
75 //llvm::LLVMSetTailCall(ret, True);
77 block.end_with_return(None, ret);
80 block.end_with_void_return(None);
84 let types = [usize, usize];
85 let name = "__rust_alloc_error_handler".to_string();
86 let args: Vec<_> = types.iter().enumerate()
87 .map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
89 let func = context.new_function(None, FunctionType::Exported, void, &args, name, false);
92 if has_alloc_error_handler {
96 AllocatorKind::Default
98 let callee = kind.fn_name(sym::oom);
99 let args: Vec<_> = types.iter().enumerate()
100 .map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
102 let callee = context.new_function(None, FunctionType::Extern, void, &args, callee, false);
103 //llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
105 let block = func.new_block("entry");
110 .map(|(i, _)| func.get_param(i as i32).to_rvalue())
111 .collect::<Vec<_>>();
112 let _ret = context.new_call(None, callee, &args);
113 //llvm::LLVMSetTailCall(ret, True);
114 block.end_with_void_return(None);