Skip to content

Commit 32e730f

Browse files
committed
auto merge of #11121 : vadimcn/rust/no-c++2, r=alexcrichton
This PR removes Rust's dependency on C++ for exception handling. Instead, it will use the unwind library API directly. closes #10469
2 parents b8c87fd + e3b3715 commit 32e730f

File tree

16 files changed

+355
-161
lines changed

16 files changed

+355
-161
lines changed

mk/rt.mk

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,16 @@ RUNTIME_CXXFLAGS_$(1)_$(2) = -D_RUST_STAGE1
7272
endif
7373
endif
7474

75-
RUNTIME_CXXS_$(1)_$(2) := \
76-
rt/rust_cxx_glue.cpp
77-
7875
RUNTIME_CS_$(1)_$(2) := \
7976
rt/rust_builtin.c \
8077
rt/rust_upcall.c \
8178
rt/miniz.c \
8279
rt/rust_android_dummy.c \
8380
rt/rust_test_helpers.c
8481

82+
RUNTIME_LL_$(1)_$(2) := \
83+
rt/rust_try.ll
84+
8585
# stage0 remove this after the next snapshot
8686
%.cpp:
8787
@touch tmp/foo.o
@@ -94,19 +94,16 @@ RT_BUILD_DIR_$(1)_$(2) := $$(RT_OUTPUT_DIR_$(1))/stage$(2)
9494
RUNTIME_DEF_$(1)_$(2) := $$(RT_OUTPUT_DIR_$(1))/rustrt$$(CFG_DEF_SUFFIX_$(1))
9595
RUNTIME_INCS_$(1)_$(2) := -I $$(S)src/rt -I $$(S)src/rt/isaac -I $$(S)src/rt/uthash \
9696
-I $$(S)src/rt/arch/$$(HOST_$(1))
97-
RUNTIME_OBJS_$(1)_$(2) := $$(RUNTIME_CXXS_$(1)_$(2):rt/%.cpp=$$(RT_BUILD_DIR_$(1)_$(2))/%.o) \
97+
RUNTIME_OBJS_$(1)_$(2) := \
9898
$$(RUNTIME_CS_$(1)_$(2):rt/%.c=$$(RT_BUILD_DIR_$(1)_$(2))/%.o) \
99-
$$(RUNTIME_S_$(1)_$(2):rt/%.S=$$(RT_BUILD_DIR_$(1)_$(2))/%.o)
99+
$$(RUNTIME_S_$(1)_$(2):rt/%.S=$$(RT_BUILD_DIR_$(1)_$(2))/%.o) \
100+
$$(RUNTIME_LL_$(1)_$(2):rt/%.ll=$$(RT_BUILD_DIR_$(1)_$(2))/%.o)
101+
100102
ALL_OBJ_FILES += $$(RUNTIME_OBJS_$(1)_$(2))
101103

102104
MORESTACK_OBJS_$(1)_$(2) := $$(RT_BUILD_DIR_$(1)_$(2))/arch/$$(HOST_$(1))/morestack.o
103105
ALL_OBJ_FILES += $$(MORESTACK_OBJS_$(1)_$(2))
104106

105-
$$(RT_BUILD_DIR_$(1)_$(2))/rust_cxx_glue.o: rt/rust_cxx_glue.cpp $$(MKFILE_DEPS)
106-
@$$(call E, compile: $$@)
107-
$$(Q)$$(call CFG_COMPILE_CXX_$(1), $$@, $$(RUNTIME_INCS_$(1)_$(2)) \
108-
$$(SNAP_DEFINES) $$(RUNTIME_CXXFLAGS_$(1)_$(2))) $$<
109-
110107
$$(RT_BUILD_DIR_$(1)_$(2))/%.o: rt/%.c $$(MKFILE_DEPS)
111108
@$$(call E, compile: $$@)
112109
$$(Q)$$(call CFG_COMPILE_C_$(1), $$@, $$(RUNTIME_INCS_$(1)_$(2)) \
@@ -117,6 +114,11 @@ $$(RT_BUILD_DIR_$(1)_$(2))/%.o: rt/%.S $$(MKFILE_DEPS) \
117114
@$$(call E, compile: $$@)
118115
$$(Q)$$(call CFG_ASSEMBLE_$(1),$$@,$$<)
119116

117+
$$(RT_BUILD_DIR_$(1)_$(2))/%.o: rt/%.ll $$(MKFILE_DEPS) \
118+
$$(LLVM_CONFIG_$$(CFG_BUILD))
119+
@$$(call E, compile: $$@)
120+
$$(Q)$(LLC_$(CFG_BUILD)) -filetype=obj -mtriple=$(1) -relocation-model=pic -o $$@ $$<
121+
120122
$$(RT_BUILD_DIR_$(1)_$(2))/arch/$$(HOST_$(1))/libmorestack.a: $$(MORESTACK_OBJS_$(1)_$(2))
121123
@$$(call E, link: $$@)
122124
$$(Q)$(AR_$(1)) rcs $$@ $$^

src/etc/mklldeps.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
for lib in out.strip().split(' '):
6060
lib = lib[2:] # chop of the leading '-l'
6161
f.write("#[link(name = \"" + lib + "\", kind = \"static\")]\n")
62+
f.write("#[link(name = \"stdc++\")]\n")
6263
if os == 'win32':
6364
f.write("#[link(name = \"imagehlp\")]\n")
6465
f.write("extern {}\n")

src/librustc/back/link.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -708,10 +708,7 @@ pub fn get_cc_prog(sess: Session) -> ~str {
708708
// In the future, FreeBSD will use clang as default compiler.
709709
// It would be flexible to use cc (system's default C compiler)
710710
// instead of hard-coded gcc.
711-
// For win32, there is no cc command, so we add a condition to make it use
712-
// g++. We use g++ rather than gcc because it automatically adds linker
713-
// options required for generation of dll modules that correctly register
714-
// stack unwind tables.
711+
// For win32, there is no cc command, so we add a condition to make it use gcc.
715712
match sess.targ_cfg.os {
716713
abi::OsAndroid => match sess.opts.android_cross_path {
717714
Some(ref path) => format!("{}/bin/arm-linux-androideabi-gcc", *path),
@@ -720,7 +717,7 @@ pub fn get_cc_prog(sess: Session) -> ~str {
720717
(--android-cross-path)")
721718
}
722719
},
723-
abi::OsWin32 => ~"g++",
720+
abi::OsWin32 => ~"gcc",
724721
_ => ~"cc",
725722
}
726723
}
@@ -1032,6 +1029,13 @@ fn link_args(sess: Session,
10321029
}
10331030
}
10341031

1032+
if sess.targ_cfg.os == abi::OsWin32 {
1033+
// Make sure that we link to the dynamic libgcc, otherwise cross-module
1034+
// DWARF stack unwinding will not work.
1035+
// This behavior may be overriden by --link-args "-static-libgcc"
1036+
args.push(~"-shared-libgcc");
1037+
}
1038+
10351039
add_local_native_libraries(&mut args, sess);
10361040
add_upstream_rust_crates(&mut args, sess, dylib, tmpdir);
10371041
add_upstream_native_libraries(&mut args, sess);

src/librustc/back/upcall.rs

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/librustc/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ pub mod back {
9292
pub mod link;
9393
pub mod manifest;
9494
pub mod abi;
95-
pub mod upcall;
9695
pub mod arm;
9796
pub mod mips;
9897
pub mod x86;

src/librustc/lib/llvm.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,6 @@ pub mod llvm {
319319
// automatically updated whenever LLVM is updated to include an up-to-date
320320
// set of the libraries we need to link to LLVM for.
321321
#[link(name = "rustllvm", kind = "static")]
322-
#[link(name = "stdc++")]
323322
extern {
324323
/* Create and destroy contexts. */
325324
pub fn LLVMContextCreate() -> ContextRef;

src/librustc/middle/lang_items.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ pub fn collect_language_items(crate: &ast::Crate,
208208
}
209209

210210
lets_do_this! {
211-
There are 42 lang items.
211+
There are 43 lang items.
212212

213213
// ID, Variant name, Name, Method name;
214214
0, FreezeTraitLangItem, "freeze", freeze_trait;
@@ -261,5 +261,7 @@ lets_do_this! {
261261
40, EventLoopFactoryLangItem, "event_loop_factory", event_loop_factory;
262262

263263
41, TypeIdLangItem, "type_id", type_id;
264+
265+
42, EhPersonalityLangItem, "eh_personality", eh_personality_fn;
264266
}
265267

src/librustc/middle/trans/base.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use metadata::{csearch, cstore, encoder};
3737
use middle::astencode;
3838
use middle::lang_items::{LangItem, ExchangeMallocFnLangItem, StartFnLangItem};
3939
use middle::lang_items::{MallocFnLangItem, ClosureExchangeMallocFnLangItem};
40+
use middle::lang_items::{EhPersonalityLangItem};
4041
use middle::trans::_match;
4142
use middle::trans::adt;
4243
use middle::trans::base;
@@ -1027,10 +1028,10 @@ pub fn get_landing_pad(bcx: @mut Block) -> BasicBlockRef {
10271028
// this represents but it's determined by the personality function and
10281029
// this is what the EH proposal example uses.
10291030
let llretty = Type::struct_([Type::i8p(), Type::i32()], false);
1030-
// The exception handling personality function. This is the C++
1031-
// personality function __gxx_personality_v0, wrapped in our naming
1032-
// convention.
1033-
let personality = bcx.ccx().upcalls.rust_personality;
1031+
// The exception handling personality function.
1032+
let personality = callee::trans_fn_ref(bcx,
1033+
langcall(bcx, None, "", EhPersonalityLangItem),
1034+
0).llfn;
10341035
// The only landing pad clause will be 'cleanup'
10351036
let llretval = LandingPad(pad_bcx, llretty, personality, 1u);
10361037
// The landing pad block is a cleanup
@@ -3195,6 +3196,8 @@ pub fn trans_crate(sess: session::Session,
31953196
reachable.push(ccx.crate_map_name.to_owned());
31963197
reachable.push(~"main");
31973198
reachable.push(~"rust_stack_exhausted");
3199+
reachable.push(~"rust_eh_personality"); // referenced from .eh_frame section on some platforms
3200+
reachable.push(~"rust_eh_personality_catch"); // referenced from rt/rust_try.ll
31983201

31993202
return CrateTranslation {
32003203
context: llcx,

src/librustc/middle/trans/context.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111

12-
use back::{upcall};
1312
use driver::session;
1413
use lib::llvm::{ContextRef, ModuleRef, ValueRef};
1514
use lib::llvm::{llvm, TargetData, TypeNames};
@@ -105,7 +104,6 @@ pub struct CrateContext {
105104
tcx: ty::ctxt,
106105
maps: astencode::Maps,
107106
stats: @mut Stats,
108-
upcalls: @upcall::Upcalls,
109107
tydesc_type: Type,
110108
int_type: Type,
111109
opaque_vec_type: Type,
@@ -233,7 +231,6 @@ impl CrateContext {
233231
llvm_insns: HashMap::new(),
234232
fn_stats: ~[]
235233
},
236-
upcalls: upcall::declare_upcalls(targ_cfg, llmod),
237234
tydesc_type: tydesc_type,
238235
int_type: int_type,
239236
opaque_vec_type: opaque_vec_type,

src/libstd/rt/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ mod local_ptr;
173173
/// Bindings to pthread/windows thread-local storage.
174174
mod thread_local_storage;
175175

176+
/// Stack unwinding
177+
pub mod unwind;
178+
176179
/// Just stuff
177180
mod util;
178181

src/libstd/rt/task.rs

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ use super::local_heap::LocalHeap;
1818
use prelude::*;
1919

2020
use borrow;
21-
use cast::transmute;
2221
use cleanup;
2322
use io::Writer;
24-
use libc::{c_void, uintptr_t, c_char, size_t};
23+
use libc::{c_char, size_t};
2524
use local_data;
2625
use option::{Option, Some, None};
2726
use rt::borrowck::BorrowRecord;
@@ -33,8 +32,8 @@ use rt::local::Local;
3332
use rt::logging::StdErrLogger;
3433
use rt::sched::{Scheduler, SchedHandle};
3534
use rt::stack::{StackSegment, StackPool};
35+
use rt::unwind::Unwinder;
3636
use send_str::SendStr;
37-
use task::TaskResult;
3837
use unstable::finally::Finally;
3938
use unstable::mutex::Mutex;
4039

@@ -91,21 +90,6 @@ pub enum SchedHome {
9190
pub struct GarbageCollector;
9291
pub struct LocalStorage(Option<local_data::Map>);
9392

94-
pub struct Unwinder {
95-
unwinding: bool,
96-
cause: Option<~Any>
97-
}
98-
99-
impl Unwinder {
100-
fn result(&mut self) -> TaskResult {
101-
if self.unwinding {
102-
Err(self.cause.take().unwrap())
103-
} else {
104-
Ok(())
105-
}
106-
}
107-
}
108-
10993
impl Task {
11094

11195
// A helper to build a new task using the dynamically found
@@ -452,54 +436,6 @@ impl Coroutine {
452436

453437
}
454438

455-
456-
// Just a sanity check to make sure we are catching a Rust-thrown exception
457-
static UNWIND_TOKEN: uintptr_t = 839147;
458-
459-
impl Unwinder {
460-
pub fn try(&mut self, f: ||) {
461-
use unstable::raw::Closure;
462-
463-
unsafe {
464-
let closure: Closure = transmute(f);
465-
let code = transmute(closure.code);
466-
let env = transmute(closure.env);
467-
468-
let token = rust_try(try_fn, code, env);
469-
assert!(token == 0 || token == UNWIND_TOKEN);
470-
}
471-
472-
extern fn try_fn(code: *c_void, env: *c_void) {
473-
unsafe {
474-
let closure: Closure = Closure {
475-
code: transmute(code),
476-
env: transmute(env),
477-
};
478-
let closure: || = transmute(closure);
479-
closure();
480-
}
481-
}
482-
483-
extern {
484-
fn rust_try(f: extern "C" fn(*c_void, *c_void),
485-
code: *c_void,
486-
data: *c_void) -> uintptr_t;
487-
}
488-
}
489-
490-
pub fn begin_unwind(&mut self, cause: ~Any) -> ! {
491-
self.unwinding = true;
492-
self.cause = Some(cause);
493-
unsafe {
494-
rust_begin_unwind(UNWIND_TOKEN);
495-
return transmute(());
496-
}
497-
extern {
498-
fn rust_begin_unwind(token: uintptr_t);
499-
}
500-
}
501-
}
502-
503439
/// This function is invoked from rust's current __morestack function. Segmented
504440
/// stacks are currently not enabled as segmented stacks, but rather one giant
505441
/// stack segment. This means that whenever we run out of stack, we want to

0 commit comments

Comments
 (0)