Skip to content

Commit ef63f09

Browse files
committed
Don't rely on main.ll anymore
I'll remove the actual file after I register a snapshot. Issue #992
1 parent cff9c67 commit ef63f09

File tree

5 files changed

+63
-47
lines changed

5 files changed

+63
-47
lines changed

src/comp/back/link.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -503,12 +503,6 @@ fn mangle_internal_name_by_seq(ccx: @crate_ctxt, flav: str) -> str {
503503
// gcc to link the object file with some libs
504504
fn link_binary(sess: session::session,
505505
saved_out_filename: str) {
506-
let main: str = alt filesearch::search(
507-
sess.filesearch(), bind filesearch::pick_file("main.o", _)) {
508-
option::some(f) { f }
509-
option::none. { sess.fatal("can't find main.o") }
510-
};
511-
512506
// The default library location, we need this to find the runtime.
513507
// The location of crates will be determined as needed.
514508
let stage: str = "-L" + sess.filesearch().get_target_lib_path();
@@ -579,7 +573,7 @@ fn link_binary(sess: session::session,
579573
}
580574
} else {
581575
// FIXME: why do we hardcode -lm?
582-
gcc_args += ["-lm", main];
576+
gcc_args += ["-lm"];
583577
}
584578

585579

src/comp/middle/trans.rs

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5632,6 +5632,7 @@ fn create_main_wrapper(ccx: @crate_ctxt, sp: span, main_llfn: ValueRef,
56325632

56335633
let llfn = create_main(ccx, sp, main_llfn, main_takes_argv);
56345634
ccx.main_fn = some(llfn);
5635+
create_entry_fn(ccx, llfn);
56355636

56365637
fn create_main(ccx: @crate_ctxt, sp: span, main_llfn: ValueRef,
56375638
takes_argv: bool) -> ValueRef {
@@ -5664,6 +5665,35 @@ fn create_main_wrapper(ccx: @crate_ctxt, sp: span, main_llfn: ValueRef,
56645665

56655666
ret llfdecl;
56665667
}
5668+
5669+
fn create_entry_fn(ccx: @crate_ctxt, rust_main: ValueRef) {
5670+
#[cfg(target_os = "win32")]
5671+
fn main_name() -> str { ret "WinMain@16"; }
5672+
#[cfg(target_os = "macos")]
5673+
fn main_name() -> str { ret "main"; }
5674+
#[cfg(target_os = "linux")]
5675+
fn main_name() -> str { ret "main"; }
5676+
let llfty = T_fn([T_int(), T_int()], T_int());
5677+
let llfn = decl_cdecl_fn(ccx.llmod, main_name(), llfty);
5678+
let llbb = str::as_buf("top", {|buf|
5679+
llvm::LLVMAppendBasicBlock(llfn, buf)
5680+
});
5681+
let bld = *ccx.builder;
5682+
llvm::LLVMPositionBuilderAtEnd(bld, llbb);
5683+
let crate_map = ccx.crate_map;
5684+
let start_ty = T_fn([val_ty(rust_main), T_int(), T_int(),
5685+
val_ty(crate_map)], T_int());
5686+
let start = str::as_buf("rust_start", {|buf|
5687+
llvm::LLVMAddGlobal(ccx.llmod, start_ty, buf)
5688+
});
5689+
let args = [rust_main, llvm::LLVMGetParam(llfn, 0u),
5690+
llvm::LLVMGetParam(llfn, 1u), crate_map];
5691+
let result = unsafe {
5692+
llvm::LLVMBuildCall(bld, start, vec::to_ptr(args),
5693+
vec::len(args), noname())
5694+
};
5695+
llvm::LLVMBuildRet(bld, result);
5696+
}
56675697
}
56685698

56695699
// Create a /real/ closure: this is like create_fn_pair, but creates a
@@ -6146,38 +6176,39 @@ fn create_module_map(ccx: @crate_ctxt) -> ValueRef {
61466176
}
61476177

61486178

6179+
fn decl_crate_map(sess: session::session, mapname: str,
6180+
llmod: ModuleRef) -> ValueRef {
6181+
let n_subcrates = 1;
6182+
let cstore = sess.get_cstore();
6183+
while cstore::have_crate_data(cstore, n_subcrates) { n_subcrates += 1; }
6184+
if !sess.get_opts().library { mapname = "toplevel"; }
6185+
let sym_name = "_rust_crate_map_" + mapname;
6186+
let arrtype = T_array(T_int(), n_subcrates as uint);
6187+
let maptype = T_struct([T_int(), arrtype]);
6188+
let map = str::as_buf(sym_name, {|buf|
6189+
llvm::LLVMAddGlobal(llmod, maptype, buf)
6190+
});
6191+
llvm::LLVMSetLinkage(map, lib::llvm::LLVMExternalLinkage
6192+
as llvm::Linkage);
6193+
ret map;
6194+
}
6195+
61496196
// FIXME use hashed metadata instead of crate names once we have that
6150-
fn create_crate_map(ccx: @crate_ctxt) -> ValueRef {
6197+
fn fill_crate_map(ccx: @crate_ctxt, map: ValueRef) {
61516198
let subcrates: [ValueRef] = [];
61526199
let i = 1;
61536200
let cstore = ccx.sess.get_cstore();
61546201
while cstore::have_crate_data(cstore, i) {
61556202
let nm = "_rust_crate_map_" + cstore::get_crate_data(cstore, i).name;
6156-
let cr =
6157-
str::as_buf(nm,
6158-
{|buf|
6159-
llvm::LLVMAddGlobal(ccx.llmod, T_int(), buf)
6160-
});
6203+
let cr = str::as_buf(nm, {|buf|
6204+
llvm::LLVMAddGlobal(ccx.llmod, T_int(), buf)
6205+
});
61616206
subcrates += [p2i(cr)];
61626207
i += 1;
61636208
}
61646209
subcrates += [C_int(0)];
6165-
let mapname;
6166-
if ccx.sess.get_opts().library {
6167-
mapname = ccx.link_meta.name;
6168-
} else { mapname = "toplevel"; }
6169-
let sym_name = "_rust_crate_map_" + mapname;
6170-
let arrtype = T_array(T_int(), std::vec::len::<ValueRef>(subcrates));
6171-
let maptype = T_struct([T_int(), arrtype]);
6172-
let map =
6173-
str::as_buf(sym_name,
6174-
{|buf| llvm::LLVMAddGlobal(ccx.llmod, maptype, buf) });
6175-
llvm::LLVMSetLinkage(map,
6176-
lib::llvm::LLVMExternalLinkage as llvm::Linkage);
6177-
llvm::LLVMSetInitializer(map,
6178-
C_struct([p2i(create_module_map(ccx)),
6179-
C_array(T_int(), subcrates)]));
6180-
ret map;
6210+
llvm::LLVMSetInitializer(map, C_struct([p2i(create_module_map(ccx)),
6211+
C_array(T_int(), subcrates)]));
61816212
}
61826213

61836214
fn write_metadata(cx: @crate_ctxt, crate: @ast::crate) {
@@ -6242,6 +6273,8 @@ fn trans_crate(sess: session::session, crate: @ast::crate, tcx: ty::ctxt,
62426273
let sha1s = map::mk_hashmap::<ty::t, str>(hasher, eqer);
62436274
let short_names = map::mk_hashmap::<ty::t, str>(hasher, eqer);
62446275
let sha = std::sha1::mk_sha1();
6276+
let link_meta = link::build_link_meta(sess, *crate, output, sha);
6277+
let crate_map = decl_crate_map(sess, link_meta.name, llmod);
62456278
let ccx =
62466279
@{sess: sess,
62476280
llmod: llmod,
@@ -6253,7 +6286,7 @@ fn trans_crate(sess: session::session, crate: @ast::crate, tcx: ty::ctxt,
62536286
ast_map: amap,
62546287
item_symbols: new_int_hash::<str>(),
62556288
mutable main_fn: none::<ValueRef>,
6256-
link_meta: link::build_link_meta(sess, *crate, output, sha),
6289+
link_meta: link_meta,
62576290
tag_sizes: tag_sizes,
62586291
discrims: new_int_hash::<ValueRef>(),
62596292
discrim_symbols: new_int_hash::<str>(),
@@ -6283,13 +6316,14 @@ fn trans_crate(sess: session::session, crate: @ast::crate, tcx: ty::ctxt,
62836316
task_type: task_type,
62846317
builder: BuilderRef_res(llvm::LLVMCreateBuilder()),
62856318
shape_cx: shape::mk_ctxt(llmod),
6286-
gc_cx: gc::mk_ctxt()};
6319+
gc_cx: gc::mk_ctxt(),
6320+
crate_map: crate_map};
62876321
let cx = new_local_ctxt(ccx);
62886322
collect_items(ccx, crate);
62896323
collect_tag_ctors(ccx, crate);
62906324
trans_constants(ccx, crate);
62916325
trans_mod(cx, crate.node.module);
6292-
create_crate_map(ccx);
6326+
fill_crate_map(ccx, crate_map);
62936327
emit_tydescs(ccx);
62946328
shape::gen_shape_tables(ccx);
62956329
write_abi_version(ccx);

src/comp/middle/trans_common.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ type crate_ctxt =
123123
task_type: TypeRef,
124124
builder: BuilderRef_res,
125125
shape_cx: shape::ctxt,
126-
gc_cx: gc::ctxt};
126+
gc_cx: gc::ctxt,
127+
crate_map: ValueRef};
127128

128129
type local_ctxt =
129130
{path: [str],

src/rt/main.ll.in

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,3 @@
1111
%task = type { i32, i32, i32, i32, i32, i32, i32, i32 }
1212

1313
%vec = type { i32, i32, [0 x i8] }
14-
15-
@_rust_crate_map_toplevel = external global %0
16-
17-
declare i32 @rust_start(i32, i32, i32, i32)
18-
19-
declare external void @_rust_main(i1* nocapture, %task*, %2* nocapture, %vec*)
20-
21-
define i32 @"MAIN"(i32, i32) {
22-
%result = tail call i32 @rust_start(i32 ptrtoint (void (i1*, %task*, %2*, %vec*)* @_rust_main to i32), i32 %0, i32 %1, i32 ptrtoint (%0* @_rust_crate_map_toplevel to i32))
23-
ret i32 %result
24-
}

src/rt/rust.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ command_line_args : public kernel_owned<command_line_args>
7474
int check_claims = 0;
7575

7676
extern "C" CDECL int
77-
rust_start(uintptr_t main_fn, int argc, char **argv,
78-
void* crate_map) {
79-
77+
rust_start(uintptr_t main_fn, int argc, char **argv, void* crate_map) {
8078
rust_env *env = load_env();
8179

8280
update_log_settings(crate_map, env->logspec);

0 commit comments

Comments
 (0)