Skip to content

Commit 1752615

Browse files
committed
MSVC SEH in MIR is implemented here
1 parent 5ad4673 commit 1752615

File tree

8 files changed

+80
-32
lines changed

8 files changed

+80
-32
lines changed

src/librustc_mir/mir_map.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,6 @@ impl<'a, 'm, 'tcx> Visitor<'tcx> for InnerDump<'a,'m,'tcx> {
148148

149149
match build_mir(Cx::new(&infcx), implicit_arg_tys, id, span, decl, body) {
150150
Ok(mut mir) => {
151-
// FIXME: This should run later rather than earlier (since this is supposed to be a
152-
// codegen option), but we do not want to re-run the whole simplify_cfg pass all
153-
// over again after this pass.
154151
no_landing_pads::NoLandingPads.run_on_mir(&mut mir, self.tcx);
155152
simplify_cfg::SimplifyCfg::new().run_on_mir(&mut mir, self.tcx);
156153

src/librustc_mir/transform/no_landing_pads.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ impl<'tcx> MutVisitor<'tcx> for NoLandingPads {
2626
Terminator::Return |
2727
Terminator::If { .. } |
2828
Terminator::Switch { .. } |
29-
Terminator::SwitchInt { .. } => { /* nothing to do */ },
29+
Terminator::SwitchInt { .. } => {
30+
/* nothing to do */
31+
},
3032
Terminator::Drop { ref mut unwind, .. } => {
3133
unwind.take();
3234
},

src/librustc_trans/trans/common.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,10 @@ impl<'blk, 'tcx> BlockAndBuilder<'blk, 'tcx> {
767767
{
768768
self.bcx.monomorphize(value)
769769
}
770+
771+
pub fn set_lpad(&self, lpad: Option<LandingPad>) {
772+
self.bcx.lpad.set(lpad.map(|p| &*self.fcx().lpad_arena.alloc(p)))
773+
}
770774
}
771775

772776
impl<'blk, 'tcx> Deref for BlockAndBuilder<'blk, 'tcx> {

src/librustc_trans/trans/mir/block.rs

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use llvm::{BasicBlockRef, ValueRef};
11+
use llvm::{BasicBlockRef, ValueRef, OperandBundleDef};
1212
use rustc::middle::ty;
1313
use rustc::mir::repr as mir;
1414
use syntax::abi::Abi;
@@ -34,15 +34,40 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
3434
let mut bcx = self.bcx(bb);
3535
let data = self.mir.basic_block_data(bb);
3636

37+
// MSVC SEH bits
38+
let (cleanup_pad, cleanup_bundle) = if let Some((cp, cb)) = self.make_cleanup_pad(bb) {
39+
(Some(cp), Some(cb))
40+
} else {
41+
(None, None)
42+
};
43+
let funclet_br = |bcx: BlockAndBuilder, llbb: BasicBlockRef| if let Some(cp) = cleanup_pad {
44+
bcx.cleanup_ret(cp, Some(llbb));
45+
} else {
46+
bcx.br(llbb);
47+
};
48+
3749
for statement in &data.statements {
3850
bcx = self.trans_statement(bcx, statement);
3951
}
4052

4153
debug!("trans_block: terminator: {:?}", data.terminator());
4254

4355
match *data.terminator() {
56+
mir::Terminator::Resume => {
57+
if let Some(cleanup_pad) = cleanup_pad {
58+
bcx.cleanup_ret(cleanup_pad, None);
59+
} else {
60+
let ps = self.get_personality_slot(&bcx);
61+
let lp = bcx.load(ps);
62+
bcx.with_block(|bcx| {
63+
base::call_lifetime_end(bcx, ps);
64+
base::trans_unwind_resume(bcx, lp);
65+
});
66+
}
67+
}
68+
4469
mir::Terminator::Goto { target } => {
45-
bcx.br(self.llblock(target));
70+
funclet_br(bcx, self.llblock(target));
4671
}
4772

4873
mir::Terminator::If { ref cond, targets: (true_bb, false_bb) } => {
@@ -85,19 +110,10 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
85110
}
86111
}
87112

88-
mir::Terminator::Resume => {
89-
let ps = self.get_personality_slot(&bcx);
90-
let lp = bcx.load(ps);
91-
bcx.with_block(|bcx| {
92-
base::call_lifetime_end(bcx, ps);
93-
base::trans_unwind_resume(bcx, lp);
94-
});
95-
}
96-
97113
mir::Terminator::Return => {
98114
let return_ty = bcx.monomorphize(&self.mir.return_ty);
99115
bcx.with_block(|bcx| {
100-
base::build_return_block(bcx.fcx, bcx, return_ty, DebugLoc::None);
116+
base::build_return_block(self.fcx, bcx, return_ty, DebugLoc::None);
101117
})
102118
}
103119

@@ -106,7 +122,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
106122
let ty = lvalue.ty.to_ty(bcx.tcx());
107123
// Double check for necessity to drop
108124
if !glue::type_needs_drop(bcx.tcx(), ty) {
109-
bcx.br(self.llblock(target));
125+
funclet_br(bcx, self.llblock(target));
110126
return;
111127
}
112128
let drop_fn = glue::get_drop_glue(bcx.ccx(), ty);
@@ -123,11 +139,11 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
123139
&[llvalue],
124140
self.llblock(target),
125141
unwind.llbb(),
126-
None,
142+
cleanup_bundle.as_ref(),
127143
None);
128144
} else {
129-
bcx.call(drop_fn, &[llvalue], None, None);
130-
bcx.br(self.llblock(target));
145+
bcx.call(drop_fn, &[llvalue], cleanup_bundle.as_ref(), None);
146+
funclet_br(bcx, self.llblock(target));
131147
}
132148
}
133149

@@ -191,22 +207,22 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
191207
&llargs[..],
192208
unreachable_blk.llbb,
193209
landingpad.llbb(),
194-
None,
210+
cleanup_bundle.as_ref(),
195211
Some(attrs));
196212
},
197213
(false, &Some(cleanup), &Some((_, success))) => {
198214
let cleanup = self.bcx(cleanup);
199215
let landingpad = self.make_landing_pad(cleanup);
200216
let (target, postinvoke) = if must_copy_dest {
201-
(bcx.fcx().new_block("", None).build(), Some(self.bcx(success)))
217+
(self.fcx.new_block("", None).build(), Some(self.bcx(success)))
202218
} else {
203219
(self.bcx(success), None)
204220
};
205221
let invokeret = bcx.invoke(callee.immediate(),
206222
&llargs[..],
207223
target.llbb(),
208224
landingpad.llbb(),
209-
None,
225+
cleanup_bundle.as_ref(),
210226
Some(attrs));
211227
if let Some(postinvoketarget) = postinvoke {
212228
// We translate the copy into a temporary block. The temporary block is
@@ -242,13 +258,16 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
242258
}
243259
},
244260
(false, _, &None) => {
245-
bcx.call(callee.immediate(), &llargs[..], None, Some(attrs));
261+
bcx.call(callee.immediate(),
262+
&llargs[..],
263+
cleanup_bundle.as_ref(),
264+
Some(attrs));
246265
bcx.unreachable();
247266
}
248267
(false, _, &Some((_, target))) => {
249268
let llret = bcx.call(callee.immediate(),
250269
&llargs[..],
251-
None,
270+
cleanup_bundle.as_ref(),
252271
Some(attrs));
253272
if must_copy_dest {
254273
let (ret_dest, ret_ty) = ret_dest_ty
@@ -257,7 +276,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
257276
base::store_ty(bcx, llret, ret_dest.llval, ret_ty);
258277
});
259278
}
260-
bcx.br(self.llblock(target));
279+
funclet_br(bcx, self.llblock(target));
261280
}
262281
// Foreign functions
263282
(true, _, destination) => {
@@ -273,7 +292,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
273292
debugloc)
274293
});
275294
if let Some((_, target)) = *destination {
276-
bcx.br(self.llblock(target));
295+
funclet_br(bcx, self.llblock(target));
277296
}
278297
},
279298
}
@@ -296,11 +315,16 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
296315
}
297316
}
298317

318+
/// Create a landingpad wrapper around the given Block.
319+
///
320+
/// No-op in MSVC SEH scheme.
299321
fn make_landing_pad(&mut self,
300322
cleanup: BlockAndBuilder<'bcx, 'tcx>)
301323
-> BlockAndBuilder<'bcx, 'tcx>
302324
{
303-
// FIXME(#30941) this doesn't handle msvc-style exceptions
325+
if base::wants_msvc_seh(cleanup.sess()) {
326+
return cleanup;
327+
}
304328
let bcx = self.fcx.new_block("cleanup", None).build();
305329
let ccx = bcx.ccx();
306330
let llpersonality = self.fcx.eh_personality();
@@ -313,6 +337,31 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
313337
bcx
314338
}
315339

340+
/// Create prologue cleanuppad instruction under MSVC SEH handling scheme.
341+
///
342+
/// Also handles setting some state for the original trans and creating an operand bundle for
343+
/// function calls.
344+
fn make_cleanup_pad(&mut self, bb: mir::BasicBlock) -> Option<(ValueRef, OperandBundleDef)> {
345+
let bcx = self.bcx(bb);
346+
let data = self.mir.basic_block_data(bb);
347+
let use_funclets = base::wants_msvc_seh(bcx.sess()) && data.is_cleanup;
348+
let cleanup_pad = if use_funclets {
349+
bcx.set_personality_fn(self.fcx.eh_personality());
350+
Some(bcx.cleanup_pad(None, &[]))
351+
} else {
352+
None
353+
};
354+
// Set the landingpad global-state for old translator, so it knows about the SEH used.
355+
bcx.set_lpad(if let Some(cleanup_pad) = cleanup_pad {
356+
Some(common::LandingPad::msvc(cleanup_pad))
357+
} else if data.is_cleanup {
358+
Some(common::LandingPad::gnu())
359+
} else {
360+
None
361+
});
362+
cleanup_pad.map(|f| (f, OperandBundleDef::new("funclet", &[f])))
363+
}
364+
316365
fn unreachable_block(&mut self) -> Block<'bcx, 'tcx> {
317366
self.unreachable_block.unwrap_or_else(|| {
318367
let bl = self.fcx.new_block("unreachable", None);

src/test/run-fail/mir_drop_panics.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010
#![feature(rustc_attrs)]
1111

12-
// ignore-msvc: FIXME(#30941)
1312
// error-pattern:panic 1
1413
// error-pattern:drop 2
1514
use std::io::{self, Write};

src/test/run-fail/mir_trans_calls_converging_drops.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#![feature(rustc_attrs)]
1212

13-
// ignore-msvc: FIXME(#30941)
1413
// error-pattern:converging_fn called
1514
// error-pattern:0 dropped
1615
// error-pattern:exit

src/test/run-fail/mir_trans_calls_converging_drops_2.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#![feature(rustc_attrs)]
1212

13-
// ignore-msvc: FIXME(#30941)
1413
// error-pattern:complex called
1514
// error-pattern:dropped
1615
// error-pattern:exit

src/test/run-fail/mir_trans_calls_diverging_drops.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#![feature(rustc_attrs)]
1212

13-
// ignore-msvc: FIXME(#30941)
1413
// error-pattern:diverging_fn called
1514
// error-pattern:0 dropped
1615

0 commit comments

Comments
 (0)