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 
codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_name: &str, kind: AllocatorKind, has_alloc_error_handler: bool)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;
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         // 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
83     }
84 
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)))
89         .collect();
90     let func = context.new_function(None, FunctionType::Exported, void, &args, name, false);
91 
92     let kind =
93         if has_alloc_error_handler {
94             AllocatorKind::Global
95         }
96         else {
97             AllocatorKind::Default
98         };
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)))
102         .collect();
103     let callee = context.new_function(None, FunctionType::Extern, void, &args, callee, false);
104     //llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
105 
106     let block = func.new_block("entry");
107 
108     let args = args
109         .iter()
110         .enumerate()
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);
116 }
117