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