@@ -7,6 +7,7 @@ use std::os::raw::{c_char, c_int};
7
7
8
8
use rustc_codegen_ssa:: CrateInfo ;
9
9
use rustc_middle:: mir:: mono:: MonoItem ;
10
+ use rustc_session:: config:: EntryFnType ;
10
11
11
12
use cranelift_jit:: { JITBuilder , JITModule } ;
12
13
@@ -32,16 +33,6 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
32
33
let mut jit_module = JITModule :: new ( jit_builder) ;
33
34
assert_eq ! ( pointer_ty( tcx) , jit_module. target_config( ) . pointer_type( ) ) ;
34
35
35
- let sig = Signature {
36
- params : vec ! [
37
- AbiParam :: new( jit_module. target_config( ) . pointer_type( ) ) ,
38
- AbiParam :: new( jit_module. target_config( ) . pointer_type( ) ) ,
39
- ] ,
40
- returns : vec ! [ AbiParam :: new( jit_module. target_config( ) . pointer_type( ) /*isize*/ ) ] ,
41
- call_conv : CallConv :: triple_default ( & crate :: target_triple ( tcx. sess ) ) ,
42
- } ;
43
- let main_func_id = jit_module. declare_function ( "main" , Linkage :: Import , & sig) . unwrap ( ) ;
44
-
45
36
let ( _, cgus) = tcx. collect_and_partition_mono_items ( LOCAL_CRATE ) ;
46
37
let mono_items = cgus
47
38
. iter ( )
@@ -86,24 +77,17 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
86
77
tcx. sess . fatal ( "Inline asm is not supported in JIT mode" ) ;
87
78
}
88
79
89
- crate :: main_shim:: maybe_create_entry_wrapper ( tcx, & mut jit_module, & mut unwind_context) ;
90
80
crate :: allocator:: codegen ( tcx, & mut jit_module, & mut unwind_context) ;
91
81
92
82
tcx. sess . abort_if_errors ( ) ;
93
83
94
84
jit_module. finalize_definitions ( ) ;
95
-
96
85
let _unwind_register_guard = unsafe { unwind_context. register_jit ( & jit_module) } ;
97
86
98
- let finalized_main: * const u8 = jit_module. get_finalized_function ( main_func_id) ;
99
-
100
87
println ! (
101
88
"Rustc codegen cranelift will JIT run the executable, because -Cllvm-args=mode=jit was passed"
102
89
) ;
103
90
104
- let f: extern "C" fn ( c_int , * const * const c_char ) -> c_int =
105
- unsafe { :: std:: mem:: transmute ( finalized_main) } ;
106
-
107
91
let args = :: std:: env:: var ( "CG_CLIF_JIT_ARGS" ) . unwrap_or_else ( |_| String :: new ( ) ) ;
108
92
let args = std:: iter:: once ( & * tcx. crate_name ( LOCAL_CRATE ) . as_str ( ) . to_string ( ) )
109
93
. chain ( args. split ( ' ' ) )
@@ -118,12 +102,58 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
118
102
BACKEND_CONFIG . with ( |tls_backend_config| {
119
103
assert ! ( tls_backend_config. borrow_mut( ) . replace( backend_config) . is_none( ) )
120
104
} ) ;
121
- CURRENT_MODULE
122
- . with ( |current_module| assert ! ( current_module. borrow_mut( ) . replace( jit_module) . is_none( ) ) ) ;
123
105
124
- let ret = f ( args. len ( ) as c_int , argv. as_ptr ( ) ) ;
106
+ let ( main_def_id, entry_ty) = tcx. entry_fn ( LOCAL_CRATE ) . unwrap ( ) ;
107
+ let instance = Instance :: mono ( tcx, main_def_id. to_def_id ( ) ) . polymorphize ( tcx) ;
108
+
109
+ match entry_ty {
110
+ EntryFnType :: Main => {
111
+ // FIXME set program arguments somehow
125
112
126
- std:: process:: exit ( ret) ;
113
+ let main_sig = Signature {
114
+ params : vec ! [ ] ,
115
+ returns : vec ! [ ] ,
116
+ call_conv : CallConv :: triple_default ( & crate :: target_triple ( tcx. sess ) ) ,
117
+ } ;
118
+ let main_func_id = jit_module
119
+ . declare_function ( tcx. symbol_name ( instance) . name , Linkage :: Import , & main_sig)
120
+ . unwrap ( ) ;
121
+ let finalized_main: * const u8 = jit_module. get_finalized_function ( main_func_id) ;
122
+
123
+ CURRENT_MODULE . with ( |current_module| {
124
+ assert ! ( current_module. borrow_mut( ) . replace( jit_module) . is_none( ) )
125
+ } ) ;
126
+
127
+ let f: extern "C" fn ( ) = unsafe { :: std:: mem:: transmute ( finalized_main) } ;
128
+ f ( ) ;
129
+ std:: process:: exit ( 0 ) ;
130
+ }
131
+ EntryFnType :: Start => {
132
+ let start_sig = Signature {
133
+ params : vec ! [
134
+ AbiParam :: new( jit_module. target_config( ) . pointer_type( ) ) ,
135
+ AbiParam :: new( jit_module. target_config( ) . pointer_type( ) ) ,
136
+ ] ,
137
+ returns : vec ! [ AbiParam :: new(
138
+ jit_module. target_config( ) . pointer_type( ) , /*isize*/
139
+ ) ] ,
140
+ call_conv : CallConv :: triple_default ( & crate :: target_triple ( tcx. sess ) ) ,
141
+ } ;
142
+ let start_func_id = jit_module
143
+ . declare_function ( tcx. symbol_name ( instance) . name , Linkage :: Import , & start_sig)
144
+ . unwrap ( ) ;
145
+ let finalized_start: * const u8 = jit_module. get_finalized_function ( start_func_id) ;
146
+
147
+ CURRENT_MODULE . with ( |current_module| {
148
+ assert ! ( current_module. borrow_mut( ) . replace( jit_module) . is_none( ) )
149
+ } ) ;
150
+
151
+ let f: extern "C" fn ( c_int , * const * const c_char ) -> c_int =
152
+ unsafe { :: std:: mem:: transmute ( finalized_start) } ;
153
+ let ret = f ( args. len ( ) as c_int , argv. as_ptr ( ) ) ;
154
+ std:: process:: exit ( ret) ;
155
+ }
156
+ }
127
157
}
128
158
129
159
#[ no_mangle]
@@ -220,7 +250,7 @@ fn load_imported_symbols_for_jit(tcx: TyCtxt<'_>) -> Vec<(String, *const u8)> {
220
250
imported_symbols
221
251
}
222
252
223
- pub ( super ) fn codegen_shim < ' tcx > ( cx : & mut CodegenCx < ' _ , ' tcx > , inst : Instance < ' tcx > ) {
253
+ fn codegen_shim < ' tcx > ( cx : & mut CodegenCx < ' _ , ' tcx > , inst : Instance < ' tcx > ) {
224
254
let tcx = cx. tcx ;
225
255
226
256
let pointer_type = cx. module . target_config ( ) . pointer_type ( ) ;
0 commit comments