@@ -338,6 +338,9 @@ fn get_type_sha1(&@crate_ctxt ccx, &ty::t t) -> str {
338
338
339
339
fn mangle ( & vec[ str] ss ) -> str {
340
340
341
+ if ( vec:: len ( ss) > 0 u && str:: eq ( vec:: top ( ss) , "main" ) ) {
342
+ ret "_rust_main" ;
343
+ }
341
344
// Follow C++ namespace-mangling style
342
345
343
346
auto n = "_ZN" ; // Begin name-sequence.
@@ -369,7 +372,7 @@ fn mangle_name_by_path_and_seq(&@crate_ctxt ccx, &vec[str] path,
369
372
ret mangle ( path + [ ccx. names . next ( flav) ] ) ;
370
373
}
371
374
372
- fn mangle_name_by_path ( & @crate_ctxt ccx , & vec[ str] path ) -> str {
375
+ fn mangle_name_by_path ( & vec[ str] path ) -> str {
373
376
ret mangle ( path) ;
374
377
}
375
378
@@ -7095,7 +7098,7 @@ fn create_vtbl(@local_ctxt cx,
7095
7098
7096
7099
let @local_ctxt mcx = @rec( path = cx. path + [ "method" ,
7097
7100
m. node. ident] with * cx) ;
7098
- let str s = mangle_name_by_path( mcx. ccx , mcx . path) ;
7101
+ let str s = mangle_name_by_path( mcx. path) ;
7099
7102
let ValueRef llfn = decl_internal_fastcall_fn( cx. ccx. llmod, s,
7100
7103
llfnty) ;
7101
7104
cx. ccx. item_ids. insert( m. node. id, llfn) ;
@@ -7107,7 +7110,7 @@ fn create_vtbl(@local_ctxt cx,
7107
7110
methods += [ llfn] ;
7108
7111
}
7109
7112
auto vtbl = C_struct ( methods) ;
7110
- auto vtbl_name = mangle_name_by_path( cx. ccx , cx . path + [ "vtbl" ] ) ;
7113
+ auto vtbl_name = mangle_name_by_path( cx. path + [ "vtbl" ] ) ;
7111
7114
auto gvar = llvm:: LLVMAddGlobal ( cx. ccx. llmod, val_ty( vtbl) ,
7112
7115
str:: buf( vtbl_name) ) ;
7113
7116
llvm:: LLVMSetInitializer ( gvar, vtbl) ;
@@ -7124,7 +7127,7 @@ fn trans_dtor(@local_ctxt cx,
7124
7127
& @ast:: method dtor) -> ValueRef {
7125
7128
7126
7129
auto llfnty = T_dtor ( cx. ccx, dtor. span, llself_ty) ;
7127
- let str s = mangle_name_by_path( cx. ccx , cx . path + [ "drop"] ) ;
7130
+ let str s = mangle_name_by_path( cx. path + [ "drop"] ) ;
7128
7131
let ValueRef llfn = decl_internal_fastcall_fn( cx. ccx. llmod, s, llfnty) ;
7129
7132
cx. ccx. item_ids. insert( dtor. node. id, llfn) ;
7130
7133
cx. ccx. item_symbols. insert( dtor. node. id, s) ;
@@ -7518,7 +7521,7 @@ fn decl_fn_and_pair(&@crate_ctxt ccx, &span sp,
7518
7521
}
7519
7522
7520
7523
// Declare the function itself.
7521
- let str s = mangle_name_by_path( ccx , path) ;
7524
+ let str s = mangle_name_by_path( path) ;
7522
7525
let ValueRef llfn = decl_internal_fastcall_fn( ccx. llmod, s, llfty) ;
7523
7526
7524
7527
// Declare the global constant pair that points to it.
@@ -7532,6 +7535,8 @@ fn decl_fn_and_pair(&@crate_ctxt ccx, &span sp,
7532
7535
ccx. sess. span_err( sp, "multiple 'main' functions" ) ;
7533
7536
}
7534
7537
log #fmt( "registering %s as main function for crate" , ps) ;
7538
+ llvm:: LLVMSetLinkage ( llfn, lib:: llvm:: LLVMExternalLinkage
7539
+ as llvm:: Linkage ) ;
7535
7540
ccx. main_fn = some( llfn) ;
7536
7541
}
7537
7542
}
@@ -7593,7 +7598,7 @@ fn decl_native_fn_and_pair(&@crate_ctxt ccx,
7593
7598
// Declare the wrapper.
7594
7599
auto t = node_ann_type( ccx, ann) ;
7595
7600
auto wrapper_type = native_fn_wrapper_type( ccx, sp, num_ty_param, t) ;
7596
- let str s = mangle_name_by_path( ccx , path) ;
7601
+ let str s = mangle_name_by_path( path) ;
7597
7602
let ValueRef wrapper_fn = decl_internal_fastcall_fn( ccx. llmod, s,
7598
7603
wrapper_type) ;
7599
7604
@@ -7996,51 +8001,6 @@ fn create_typedefs(&@crate_ctxt cx) {
7996
8001
llvm:: LLVMAddTypeName ( cx. llmod , str:: buf ( "tydesc" ) , T_tydesc ( cx. tn ) ) ;
7997
8002
}
7998
8003
7999
- fn trans_main_fn ( @local_ctxt cx , ValueRef crate_map ) {
8000
- auto T_main_args = [ T_int ( ) , T_int ( ) ] ;
8001
- auto T_rust_start_args = [ T_int ( ) , T_int ( ) , T_int ( ) , T_int ( ) ] ;
8002
-
8003
- auto main_name;
8004
- if ( str:: eq ( std:: os:: target_os ( ) , "win32" ) ) {
8005
- main_name = "WinMain@16" ;
8006
- } else {
8007
- main_name = "main" ;
8008
- }
8009
-
8010
- auto llmain =
8011
- decl_cdecl_fn ( cx. ccx . llmod , main_name, T_fn ( T_main_args , T_int ( ) ) ) ;
8012
-
8013
- auto llrust_start = decl_cdecl_fn ( cx. ccx . llmod , "rust_start" ,
8014
- T_fn ( T_rust_start_args , T_int ( ) ) ) ;
8015
-
8016
- auto llargc = llvm:: LLVMGetParam ( llmain, 0 u) ;
8017
- auto llargv = llvm:: LLVMGetParam ( llmain, 1 u) ;
8018
- auto llrust_main = alt ( cx. ccx . main_fn ) {
8019
- case ( none) {
8020
- cx. ccx . sess . err ( "missing 'main' function" ) ;
8021
- // FIXME: shouldn't sess.err's ! result unify with f?
8022
- C_nil ( )
8023
- }
8024
- case ( some ( ?f) ) { f }
8025
- } ;
8026
-
8027
- //
8028
- // Emit the moral equivalent of:
8029
- //
8030
- // main(int argc, char **argv) {
8031
- // rust_start(&_rust.main, argc, argv);
8032
- // }
8033
- //
8034
-
8035
- let BasicBlockRef llbb =
8036
- llvm:: LLVMAppendBasicBlock ( llmain, str:: buf ( "" ) ) ;
8037
- auto b = new_builder ( llbb) ;
8038
-
8039
- auto start_args = [ p2i ( llrust_main) , llargc, llargv, p2i ( crate_map) ] ;
8040
-
8041
- b. Ret ( b. Call ( llrust_start, start_args) ) ;
8042
- }
8043
-
8044
8004
fn declare_intrinsics ( ModuleRef llmod) -> hashmap [ str, ValueRef ] {
8045
8005
8046
8006
let vec[ TypeRef ] T_memmove32_args = [ T_ptr ( T_i8 ( ) ) , T_ptr ( T_i8 ( ) ) ,
@@ -8211,8 +8171,6 @@ fn create_module_map(&@crate_ctxt ccx) -> ValueRef {
8211
8171
auto maptype = T_array ( elttype, ccx. module_data . size ( ) + 1 u) ;
8212
8172
auto map = llvm:: LLVMAddGlobal ( ccx. llmod , maptype,
8213
8173
str:: buf ( "_rust_mod_map" ) ) ;
8214
- llvm:: LLVMSetLinkage ( map, lib:: llvm:: LLVMInternalLinkage
8215
- as llvm:: Linkage ) ;
8216
8174
let vec[ ValueRef ] elts = [ ] ;
8217
8175
for each ( @tup( str, ValueRef ) item in ccx. module_data . items ( ) ) {
8218
8176
auto elt = C_struct ( [ p2i ( C_cstr ( ccx, item. _0 ) ) , p2i ( item. _1 ) ] ) ;
@@ -8245,7 +8203,14 @@ fn create_crate_map(&@crate_ctxt ccx) -> ValueRef {
8245
8203
i += 1 ;
8246
8204
}
8247
8205
vec:: push[ ValueRef ] ( subcrates, C_int ( 0 ) ) ;
8248
- auto sym_name = "_rust_crate_map_" + crate_name ( ccx, "__none__" ) ;
8206
+ auto cname = crate_name ( ccx, "__none__" ) ;
8207
+ auto mapname;
8208
+ if ( ccx. sess . get_opts ( ) . shared ) {
8209
+ mapname = cname;
8210
+ } else {
8211
+ mapname = "toplevel" ;
8212
+ }
8213
+ auto sym_name = "_rust_crate_map_" + mapname;
8249
8214
auto arrtype = T_array ( T_int ( ) , vec:: len[ ValueRef ] ( subcrates) ) ;
8250
8215
auto maptype = T_struct ( [ T_int ( ) , arrtype] ) ;
8251
8216
auto map = llvm:: LLVMAddGlobal ( ccx. llmod , maptype, str:: buf ( sym_name) ) ;
@@ -8322,9 +8287,17 @@ fn trans_crate(&session::session sess, &@ast::crate crate,
8322
8287
trans_constants ( ccx, crate ) ;
8323
8288
trans_mod ( cx, crate . node. module ) ;
8324
8289
auto crate_map = create_crate_map ( ccx) ;
8290
+
8325
8291
if ( !sess. get_opts ( ) . shared ) {
8326
- trans_main_fn ( cx, crate_map) ;
8327
- }
8292
+ auto gvar = llvm:: LLVMAddGlobal ( cx. ccx . llmod , T_ptr ( T_int ( ) ) ,
8293
+ str:: buf ( "_rust_fetch_this_object_hack" ) ) ;
8294
+
8295
+ auto gvar2 = llvm:: LLVMAddGlobal ( cx. ccx . llmod , T_ptr ( T_ptr ( T_int ( ) ) ) ,
8296
+ str:: buf ( "_rust_fetch_this_object_hack2" ) ) ;
8297
+ llvm:: LLVMSetInitializer ( gvar2, gvar) ;
8298
+ llvm:: LLVMSetGlobalConstant ( gvar, True ) ;
8299
+ llvm:: LLVMSetGlobalConstant ( gvar2, True ) ;
8300
+ }
8328
8301
8329
8302
emit_tydescs ( ccx) ;
8330
8303
0 commit comments