1- use libc:: c_uint;
2- use rustc_ast:: expand:: allocator:: {
3- AllocatorKind , NO_ALLOC_SHIM_IS_UNSTABLE , alloc_error_handler_name,
4- } ;
5- use rustc_middle:: bug;
1+ use rustc_ast:: expand:: allocator:: NO_ALLOC_SHIM_IS_UNSTABLE ;
62use rustc_middle:: ty:: TyCtxt ;
73use rustc_session:: config:: { DebugInfo , OomStrategy } ;
84
95use crate :: common:: AsCCharPtr ;
10- use crate :: llvm:: { self , Context , False , Module , True , Type } ;
11- use crate :: { ModuleLlvm , attributes , debuginfo} ;
6+ use crate :: llvm:: { self , False } ;
7+ use crate :: { ModuleLlvm , debuginfo} ;
128
13- pub ( crate ) unsafe fn codegen (
14- tcx : TyCtxt < ' _ > ,
15- module_llvm : & mut ModuleLlvm ,
16- module_name : & str ,
17- alloc_error_handler_kind : AllocatorKind ,
18- ) {
9+ pub ( crate ) unsafe fn codegen ( tcx : TyCtxt < ' _ > , module_llvm : & mut ModuleLlvm , module_name : & str ) {
1910 let llcx = & * module_llvm. llcx ;
2011 let llmod = module_llvm. llmod ( ) ;
21- let usize = unsafe {
22- match tcx. sess . target . pointer_width {
23- 16 => llvm:: LLVMInt16TypeInContext ( llcx) ,
24- 32 => llvm:: LLVMInt32TypeInContext ( llcx) ,
25- 64 => llvm:: LLVMInt64TypeInContext ( llcx) ,
26- tws => bug ! ( "Unsupported target word size for int: {}" , tws) ,
27- }
28- } ;
2912 let i8 = unsafe { llvm:: LLVMInt8TypeInContext ( llcx) } ;
3013
31- // rust alloc error handler
32- create_wrapper_function (
33- tcx,
34- llcx,
35- llmod,
36- "__rust_alloc_error_handler" ,
37- alloc_error_handler_name ( alloc_error_handler_kind) ,
38- & [ usize, usize] , // size, align
39- None ,
40- true ,
41- ) ;
42-
4314 unsafe {
4415 // __rust_alloc_error_handler_should_panic
4516 let name = OomStrategy :: SYMBOL ;
@@ -62,80 +33,3 @@ pub(crate) unsafe fn codegen(
6233 dbg_cx. finalize ( tcx. sess ) ;
6334 }
6435}
65-
66- fn create_wrapper_function (
67- tcx : TyCtxt < ' _ > ,
68- llcx : & Context ,
69- llmod : & Module ,
70- from_name : & str ,
71- to_name : & str ,
72- args : & [ & Type ] ,
73- output : Option < & Type > ,
74- no_return : bool ,
75- ) {
76- unsafe {
77- let ty = llvm:: LLVMFunctionType (
78- output. unwrap_or_else ( || llvm:: LLVMVoidTypeInContext ( llcx) ) ,
79- args. as_ptr ( ) ,
80- args. len ( ) as c_uint ,
81- False ,
82- ) ;
83- let llfn = llvm:: LLVMRustGetOrInsertFunction (
84- llmod,
85- from_name. as_c_char_ptr ( ) ,
86- from_name. len ( ) ,
87- ty,
88- ) ;
89- let no_return = if no_return {
90- // -> ! DIFlagNoReturn
91- let no_return = llvm:: AttributeKind :: NoReturn . create_attr ( llcx) ;
92- attributes:: apply_to_llfn ( llfn, llvm:: AttributePlace :: Function , & [ no_return] ) ;
93- Some ( no_return)
94- } else {
95- None
96- } ;
97-
98- llvm:: set_visibility ( llfn, llvm:: Visibility :: from_generic ( tcx. sess . default_visibility ( ) ) ) ;
99-
100- if tcx. sess . must_emit_unwind_tables ( ) {
101- let uwtable =
102- attributes:: uwtable_attr ( llcx, tcx. sess . opts . unstable_opts . use_sync_unwind ) ;
103- attributes:: apply_to_llfn ( llfn, llvm:: AttributePlace :: Function , & [ uwtable] ) ;
104- }
105-
106- let callee =
107- llvm:: LLVMRustGetOrInsertFunction ( llmod, to_name. as_c_char_ptr ( ) , to_name. len ( ) , ty) ;
108- if let Some ( no_return) = no_return {
109- // -> ! DIFlagNoReturn
110- attributes:: apply_to_llfn ( callee, llvm:: AttributePlace :: Function , & [ no_return] ) ;
111- }
112- llvm:: set_visibility ( callee, llvm:: Visibility :: Hidden ) ;
113-
114- let llbb = llvm:: LLVMAppendBasicBlockInContext ( llcx, llfn, c"entry" . as_ptr ( ) ) ;
115-
116- let llbuilder = llvm:: LLVMCreateBuilderInContext ( llcx) ;
117- llvm:: LLVMPositionBuilderAtEnd ( llbuilder, llbb) ;
118- let args = args
119- . iter ( )
120- . enumerate ( )
121- . map ( |( i, _) | llvm:: LLVMGetParam ( llfn, i as c_uint ) )
122- . collect :: < Vec < _ > > ( ) ;
123- let ret = llvm:: LLVMBuildCallWithOperandBundles (
124- llbuilder,
125- ty,
126- callee,
127- args. as_ptr ( ) ,
128- args. len ( ) as c_uint ,
129- [ ] . as_ptr ( ) ,
130- 0 as c_uint ,
131- c"" . as_ptr ( ) ,
132- ) ;
133- llvm:: LLVMSetTailCall ( ret, True ) ;
134- if output. is_some ( ) {
135- llvm:: LLVMBuildRet ( llbuilder, ret) ;
136- } else {
137- llvm:: LLVMBuildRetVoid ( llbuilder) ;
138- }
139- llvm:: LLVMDisposeBuilder ( llbuilder) ;
140- }
141- }
0 commit comments