]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_gcc/src/allocator.rs
Merge commit '0c89065b934397b62838fe3e4ef6f6352fc52daf' into libgccjit-codegen
[rust.git] / compiler / rustc_codegen_gcc / src / allocator.rs
1 //use crate::attributes;
2 use gccjit::{FunctionType, ToRValue};
3 use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
4 use rustc_middle::bug;
5 use rustc_middle::ty::TyCtxt;
6 use rustc_span::symbol::sym;
7
8 use crate::GccContext;
9
10 pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, kind: AllocatorKind, has_alloc_error_handler: bool) {
11     let context = &mods.context;
12     let usize =
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),
18         };
19     let i8 = context.new_type::<i8>();
20     let i8p = i8.make_pointer();
21     let void = context.new_type::<()>();
22
23     for method in ALLOCATOR_METHODS {
24         let mut types = Vec::with_capacity(method.inputs.len());
25         for ty in method.inputs.iter() {
26             match *ty {
27                 AllocatorTy::Layout => {
28                     types.push(usize);
29                     types.push(usize);
30                 }
31                 AllocatorTy::Ptr => types.push(i8p),
32                 AllocatorTy::Usize => types.push(usize),
33
34                 AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
35             }
36         }
37         let output = match method.output {
38             AllocatorTy::ResultPtr => Some(i8p),
39             AllocatorTy::Unit => None,
40
41             AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
42                 panic!("invalid allocator output")
43             }
44         };
45         let name = format!("__rust_{}", method.name);
46
47         let args: Vec<_> = types.iter().enumerate()
48             .map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
49             .collect();
50         let func = context.new_function(None, FunctionType::Exported, output.unwrap_or(void), &args, name, false);
51
52         if tcx.sess.target.options.default_hidden_visibility {
53             //llvm::LLVMRustSetVisibility(func, llvm::Visibility::Hidden);
54         }
55         if tcx.sess.must_emit_unwind_tables() {
56             // TODO
57             //attributes::emit_uwtable(func, true);
58         }
59
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)))
63             .collect();
64         let callee = context.new_function(None, FunctionType::Extern, output.unwrap_or(void), &args, callee, false);
65         //llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
66
67         let block = func.new_block("entry");
68
69         let args = args
70             .iter()
71             .enumerate()
72             .map(|(i, _)| func.get_param(i as i32).to_rvalue())
73             .collect::<Vec<_>>();
74         let ret = context.new_call(None, callee, &args);
75         //llvm::LLVMSetTailCall(ret, True);
76         if output.is_some() {
77             block.end_with_return(None, ret);
78         }
79         else {
80             block.end_with_void_return(None);
81         }
82     }
83
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)))
88         .collect();
89     let func = context.new_function(None, FunctionType::Exported, void, &args, name, false);
90
91     let kind =
92         if has_alloc_error_handler {
93             AllocatorKind::Global
94         }
95         else {
96             AllocatorKind::Default
97         };
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)))
101         .collect();
102     let callee = context.new_function(None, FunctionType::Extern, void, &args, callee, false);
103     //llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
104
105     let block = func.new_block("entry");
106
107     let args = args
108         .iter()
109         .enumerate()
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);
115 }