]>
Commit | Line | Data |
---|---|---|
dfeec247 XL |
1 | use super::operand::OperandRef; |
2 | use super::operand::OperandValue::{Immediate, Pair, Ref}; | |
3 | use super::place::PlaceRef; | |
4 | use super::{FunctionCx, LocalRef}; | |
5 | ||
9fa01778 | 6 | use crate::base; |
9fa01778 XL |
7 | use crate::common::{self, IntPredicate}; |
8 | use crate::meth; | |
9fa01778 | 9 | use crate::traits::*; |
dfeec247 | 10 | use crate::MemFlags; |
3157f602 | 11 | |
3dfed10e XL |
12 | use rustc_ast as ast; |
13 | use rustc_hir::lang_items::LangItem; | |
dfeec247 | 14 | use rustc_index::vec::Idx; |
ba9703b0 | 15 | use rustc_middle::mir::AssertKind; |
29967ef6 | 16 | use rustc_middle::mir::{self, SwitchTargets}; |
ba9703b0 | 17 | use rustc_middle::ty::layout::{FnAbiExt, HasTyCtxt}; |
1b1a35ee | 18 | use rustc_middle::ty::print::with_no_trimmed_paths; |
ba9703b0 | 19 | use rustc_middle::ty::{self, Instance, Ty, TypeFoldable}; |
3dfed10e XL |
20 | use rustc_span::source_map::Span; |
21 | use rustc_span::{sym, Symbol}; | |
dfeec247 | 22 | use rustc_target::abi::call::{ArgAbi, FnAbi, PassMode}; |
17df50a5 | 23 | use rustc_target::abi::{self, HasDataLayout, LayoutOf}; |
dfeec247 | 24 | use rustc_target::spec::abi::Abi; |
54a0048b | 25 | |
532ac7d7 XL |
26 | /// Used by `FunctionCx::codegen_terminator` for emitting common patterns |
27 | /// e.g., creating a basic block, calling a function, etc. | |
60c5eb7d XL |
28 | struct TerminatorCodegenHelper<'tcx> { |
29 | bb: mir::BasicBlock, | |
30 | terminator: &'tcx mir::Terminator<'tcx>, | |
532ac7d7 XL |
31 | funclet_bb: Option<mir::BasicBlock>, |
32 | } | |
33 | ||
60c5eb7d | 34 | impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> { |
17df50a5 XL |
35 | /// Returns the appropriate `Funclet` for the current funclet, if on MSVC, |
36 | /// either already previously cached, or newly created, by `landing_pad_for`. | |
60c5eb7d | 37 | fn funclet<'b, Bx: BuilderMethods<'a, 'tcx>>( |
532ac7d7 | 38 | &self, |
17df50a5 | 39 | fx: &'b mut FunctionCx<'a, 'tcx, Bx>, |
60c5eb7d | 40 | ) -> Option<&'b Bx::Funclet> { |
17df50a5 XL |
41 | let funclet_bb = self.funclet_bb?; |
42 | if base::wants_msvc_seh(fx.cx.tcx().sess) { | |
43 | // If `landing_pad_for` hasn't been called yet to create the `Funclet`, | |
44 | // it has to be now. This may not seem necessary, as RPO should lead | |
45 | // to all the unwind edges being visited (and so to `landing_pad_for` | |
46 | // getting called for them), before building any of the blocks inside | |
47 | // the funclet itself - however, if MIR contains edges that end up not | |
48 | // being needed in the LLVM IR after monomorphization, the funclet may | |
49 | // be unreachable, and we don't have yet a way to skip building it in | |
50 | // such an eventuality (which may be a better solution than this). | |
51 | if fx.funclets[funclet_bb].is_none() { | |
52 | fx.landing_pad_for(funclet_bb); | |
53 | } | |
54 | ||
55 | Some( | |
56 | fx.funclets[funclet_bb] | |
57 | .as_ref() | |
58 | .expect("landing_pad_for didn't also create funclets entry"), | |
59 | ) | |
60 | } else { | |
61 | None | |
62 | } | |
532ac7d7 | 63 | } |
3157f602 | 64 | |
60c5eb7d | 65 | fn lltarget<Bx: BuilderMethods<'a, 'tcx>>( |
532ac7d7 | 66 | &self, |
60c5eb7d | 67 | fx: &mut FunctionCx<'a, 'tcx, Bx>, |
532ac7d7 XL |
68 | target: mir::BasicBlock, |
69 | ) -> (Bx::BasicBlock, bool) { | |
70 | let span = self.terminator.source_info.span; | |
17df50a5 | 71 | let lltarget = fx.llbb(target); |
532ac7d7 XL |
72 | let target_funclet = fx.cleanup_kinds[target].funclet_bb(target); |
73 | match (self.funclet_bb, target_funclet) { | |
74 | (None, None) => (lltarget, false), | |
dfeec247 XL |
75 | (Some(f), Some(t_f)) if f == t_f || !base::wants_msvc_seh(fx.cx.tcx().sess) => { |
76 | (lltarget, false) | |
77 | } | |
17df50a5 XL |
78 | // jump *into* cleanup - need a landing pad if GNU, cleanup pad if MSVC |
79 | (None, Some(_)) => (fx.landing_pad_for(target), false), | |
532ac7d7 | 80 | (Some(_), None) => span_bug!(span, "{:?} - jump out of cleanup?", self.terminator), |
17df50a5 | 81 | (Some(_), Some(_)) => (fx.landing_pad_for(target), true), |
7cac9316 | 82 | } |
532ac7d7 | 83 | } |
7cac9316 | 84 | |
532ac7d7 | 85 | /// Create a basic block. |
60c5eb7d | 86 | fn llblock<Bx: BuilderMethods<'a, 'tcx>>( |
532ac7d7 | 87 | &self, |
60c5eb7d | 88 | fx: &mut FunctionCx<'a, 'tcx, Bx>, |
532ac7d7 XL |
89 | target: mir::BasicBlock, |
90 | ) -> Bx::BasicBlock { | |
91 | let (lltarget, is_cleanupret) = self.lltarget(fx, target); | |
92 | if is_cleanupret { | |
93 | // MSVC cross-funclet jump - need a trampoline | |
94 | ||
95 | debug!("llblock: creating cleanup trampoline for {:?}", target); | |
96 | let name = &format!("{:?}_cleanup_trampoline_{:?}", self.bb, target); | |
97 | let mut trampoline = fx.new_block(name); | |
dfeec247 | 98 | trampoline.cleanup_ret(self.funclet(fx).unwrap(), Some(lltarget)); |
532ac7d7 XL |
99 | trampoline.llbb() |
100 | } else { | |
101 | lltarget | |
102 | } | |
7cac9316 XL |
103 | } |
104 | ||
60c5eb7d | 105 | fn funclet_br<Bx: BuilderMethods<'a, 'tcx>>( |
532ac7d7 | 106 | &self, |
60c5eb7d | 107 | fx: &mut FunctionCx<'a, 'tcx, Bx>, |
532ac7d7 XL |
108 | bx: &mut Bx, |
109 | target: mir::BasicBlock, | |
a1dfa0c6 | 110 | ) { |
532ac7d7 XL |
111 | let (lltarget, is_cleanupret) = self.lltarget(fx, target); |
112 | if is_cleanupret { | |
113 | // micro-optimization: generate a `ret` rather than a jump | |
114 | // to a trampoline. | |
115 | bx.cleanup_ret(self.funclet(fx).unwrap(), Some(lltarget)); | |
116 | } else { | |
117 | bx.br(lltarget); | |
118 | } | |
119 | } | |
7cac9316 | 120 | |
60c5eb7d | 121 | /// Call `fn_ptr` of `fn_abi` with the arguments `llargs`, the optional |
532ac7d7 | 122 | /// return destination `destination` and the cleanup function `cleanup`. |
60c5eb7d | 123 | fn do_call<Bx: BuilderMethods<'a, 'tcx>>( |
532ac7d7 | 124 | &self, |
60c5eb7d | 125 | fx: &mut FunctionCx<'a, 'tcx, Bx>, |
532ac7d7 | 126 | bx: &mut Bx, |
60c5eb7d | 127 | fn_abi: FnAbi<'tcx, Ty<'tcx>>, |
532ac7d7 XL |
128 | fn_ptr: Bx::Value, |
129 | llargs: &[Bx::Value], | |
130 | destination: Option<(ReturnDest<'tcx, Bx::Value>, mir::BasicBlock)>, | |
131 | cleanup: Option<mir::BasicBlock>, | |
132 | ) { | |
ba9703b0 XL |
133 | // If there is a cleanup block and the function we're calling can unwind, then |
134 | // do an invoke, otherwise do a call. | |
135 | if let Some(cleanup) = cleanup.filter(|_| fn_abi.can_unwind) { | |
17df50a5 XL |
136 | let ret_llbb = if let Some((_, target)) = destination { |
137 | fx.llbb(target) | |
532ac7d7 XL |
138 | } else { |
139 | fx.unreachable_block() | |
140 | }; | |
dfeec247 | 141 | let invokeret = |
17df50a5 | 142 | bx.invoke(fn_ptr, &llargs, ret_llbb, self.llblock(fx, cleanup), self.funclet(fx)); |
60c5eb7d | 143 | bx.apply_attrs_callsite(&fn_abi, invokeret); |
532ac7d7 XL |
144 | |
145 | if let Some((ret_dest, target)) = destination { | |
146 | let mut ret_bx = fx.build_block(target); | |
147 | fx.set_debug_loc(&mut ret_bx, self.terminator.source_info); | |
60c5eb7d | 148 | fx.store_return(&mut ret_bx, ret_dest, &fn_abi.ret, invokeret); |
a1dfa0c6 | 149 | } |
532ac7d7 XL |
150 | } else { |
151 | let llret = bx.call(fn_ptr, &llargs, self.funclet(fx)); | |
60c5eb7d XL |
152 | bx.apply_attrs_callsite(&fn_abi, llret); |
153 | if fx.mir[self.bb].is_cleanup { | |
532ac7d7 XL |
154 | // Cleanup is always the cold path. Don't inline |
155 | // drop glue. Also, when there is a deeply-nested | |
156 | // struct, there are "symmetry" issues that cause | |
157 | // exponential inlining - see issue #41696. | |
158 | bx.do_not_inline(llret); | |
3157f602 | 159 | } |
7cac9316 | 160 | |
532ac7d7 | 161 | if let Some((ret_dest, target)) = destination { |
60c5eb7d | 162 | fx.store_return(bx, ret_dest, &fn_abi.ret, llret); |
532ac7d7 | 163 | self.funclet_br(fx, bx, target); |
7cac9316 | 164 | } else { |
532ac7d7 | 165 | bx.unreachable(); |
7cac9316 | 166 | } |
532ac7d7 XL |
167 | } |
168 | } | |
169 | } | |
3157f602 | 170 | |
532ac7d7 | 171 | /// Codegen implementations for some terminator variants. |
dc9dc135 | 172 | impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { |
532ac7d7 | 173 | /// Generates code for a `Resume` terminator. |
dfeec247 | 174 | fn codegen_resume_terminator(&mut self, helper: TerminatorCodegenHelper<'tcx>, mut bx: Bx) { |
532ac7d7 XL |
175 | if let Some(funclet) = helper.funclet(self) { |
176 | bx.cleanup_ret(funclet, None); | |
177 | } else { | |
178 | let slot = self.get_personality_slot(&mut bx); | |
179 | let lp0 = slot.project_field(&mut bx, 0); | |
180 | let lp0 = bx.load_operand(lp0).immediate(); | |
181 | let lp1 = slot.project_field(&mut bx, 1); | |
182 | let lp1 = bx.load_operand(lp1).immediate(); | |
183 | slot.storage_dead(&mut bx); | |
184 | ||
ba9703b0 XL |
185 | let mut lp = bx.const_undef(self.landing_pad_type()); |
186 | lp = bx.insert_value(lp, lp0, 0); | |
187 | lp = bx.insert_value(lp, lp1, 1); | |
188 | bx.resume(lp); | |
532ac7d7 XL |
189 | } |
190 | } | |
7cac9316 | 191 | |
60c5eb7d | 192 | fn codegen_switchint_terminator( |
532ac7d7 | 193 | &mut self, |
60c5eb7d | 194 | helper: TerminatorCodegenHelper<'tcx>, |
532ac7d7 XL |
195 | mut bx: Bx, |
196 | discr: &mir::Operand<'tcx>, | |
197 | switch_ty: Ty<'tcx>, | |
29967ef6 | 198 | targets: &SwitchTargets, |
532ac7d7 XL |
199 | ) { |
200 | let discr = self.codegen_operand(&mut bx, &discr); | |
f035d41b XL |
201 | // `switch_ty` is redundant, sanity-check that. |
202 | assert_eq!(discr.layout.ty, switch_ty); | |
29967ef6 XL |
203 | let mut target_iter = targets.iter(); |
204 | if target_iter.len() == 1 { | |
205 | // If there are two targets (one conditional, one fallback), emit br instead of switch | |
206 | let (test_value, target) = target_iter.next().unwrap(); | |
207 | let lltrue = helper.llblock(self, target); | |
208 | let llfalse = helper.llblock(self, targets.otherwise()); | |
532ac7d7 XL |
209 | if switch_ty == bx.tcx().types.bool { |
210 | // Don't generate trivial icmps when switching on bool | |
29967ef6 XL |
211 | match test_value { |
212 | 0 => bx.cond_br(discr.immediate(), llfalse, lltrue), | |
213 | 1 => bx.cond_br(discr.immediate(), lltrue, llfalse), | |
214 | _ => bug!(), | |
3157f602 | 215 | } |
532ac7d7 | 216 | } else { |
dfeec247 | 217 | let switch_llty = bx.immediate_backend_type(bx.layout_of(switch_ty)); |
29967ef6 | 218 | let llval = bx.const_uint_big(switch_llty, test_value); |
532ac7d7 XL |
219 | let cmp = bx.icmp(IntPredicate::IntEQ, discr.immediate(), llval); |
220 | bx.cond_br(cmp, lltrue, llfalse); | |
3157f602 | 221 | } |
532ac7d7 | 222 | } else { |
532ac7d7 XL |
223 | bx.switch( |
224 | discr.immediate(), | |
29967ef6 XL |
225 | helper.llblock(self, targets.otherwise()), |
226 | target_iter.map(|(value, target)| (value, helper.llblock(self, target))), | |
532ac7d7 XL |
227 | ); |
228 | } | |
229 | } | |
54a0048b | 230 | |
dc9dc135 | 231 | fn codegen_return_terminator(&mut self, mut bx: Bx) { |
e74abb32 | 232 | // Call `va_end` if this is the definition of a C-variadic function. |
60c5eb7d | 233 | if self.fn_abi.c_variadic { |
e74abb32 | 234 | // The `VaList` "spoofed" argument is just after all the real arguments. |
60c5eb7d | 235 | let va_list_arg_idx = self.fn_abi.args.len(); |
e74abb32 XL |
236 | match self.locals[mir::Local::new(1 + va_list_arg_idx)] { |
237 | LocalRef::Place(va_list) => { | |
532ac7d7 XL |
238 | bx.va_end(va_list.llval); |
239 | } | |
e74abb32 | 240 | _ => bug!("C-variadic function must have a `VaList` place"), |
54a0048b | 241 | } |
532ac7d7 | 242 | } |
60c5eb7d | 243 | if self.fn_abi.ret.layout.abi.is_uninhabited() { |
532ac7d7 XL |
244 | // Functions with uninhabited return values are marked `noreturn`, |
245 | // so we should make sure that we never actually do. | |
60c5eb7d XL |
246 | // We play it safe by using a well-defined `abort`, but we could go for immediate UB |
247 | // if that turns out to be helpful. | |
532ac7d7 | 248 | bx.abort(); |
60c5eb7d XL |
249 | // `abort` does not terminate the block, so we still need to generate |
250 | // an `unreachable` terminator after it. | |
532ac7d7 XL |
251 | bx.unreachable(); |
252 | return; | |
253 | } | |
60c5eb7d | 254 | let llval = match self.fn_abi.ret.mode { |
fc512014 | 255 | PassMode::Ignore | PassMode::Indirect { .. } => { |
532ac7d7 XL |
256 | bx.ret_void(); |
257 | return; | |
ff7c6d11 XL |
258 | } |
259 | ||
532ac7d7 | 260 | PassMode::Direct(_) | PassMode::Pair(..) => { |
74b04a01 | 261 | let op = self.codegen_consume(&mut bx, mir::Place::return_place().as_ref()); |
532ac7d7 | 262 | if let Ref(llval, _, align) = op.val { |
136023e0 | 263 | bx.load(bx.backend_type(op.layout), llval, align) |
8bb4bdeb | 264 | } else { |
532ac7d7 | 265 | op.immediate_or_packed_pair(&mut bx) |
54a0048b SL |
266 | } |
267 | } | |
268 | ||
532ac7d7 XL |
269 | PassMode::Cast(cast_ty) => { |
270 | let op = match self.locals[mir::RETURN_PLACE] { | |
271 | LocalRef::Operand(Some(op)) => op, | |
272 | LocalRef::Operand(None) => bug!("use of return before def"), | |
dfeec247 XL |
273 | LocalRef::Place(cg_place) => OperandRef { |
274 | val: Ref(cg_place.llval, None, cg_place.align), | |
275 | layout: cg_place.layout, | |
276 | }, | |
532ac7d7 XL |
277 | LocalRef::UnsizedPlace(_) => bug!("return type must be sized"), |
278 | }; | |
279 | let llslot = match op.val { | |
280 | Immediate(_) | Pair(..) => { | |
dfeec247 | 281 | let scratch = PlaceRef::alloca(&mut bx, self.fn_abi.ret.layout); |
532ac7d7 XL |
282 | op.val.store(&mut bx, scratch); |
283 | scratch.llval | |
284 | } | |
285 | Ref(llval, _, align) => { | |
dfeec247 | 286 | assert_eq!(align, op.layout.align.abi, "return place is unaligned!"); |
532ac7d7 | 287 | llval |
476ff2be | 288 | } |
3157f602 | 289 | }; |
136023e0 XL |
290 | let ty = bx.cast_backend_type(&cast_ty); |
291 | let addr = bx.pointercast(llslot, bx.type_ptr_to(ty)); | |
292 | bx.load(ty, addr, self.fn_abi.ret.layout.align.abi) | |
3157f602 | 293 | } |
532ac7d7 XL |
294 | }; |
295 | bx.ret(llval); | |
296 | } | |
3157f602 | 297 | |
60c5eb7d | 298 | fn codegen_drop_terminator( |
532ac7d7 | 299 | &mut self, |
60c5eb7d | 300 | helper: TerminatorCodegenHelper<'tcx>, |
532ac7d7 | 301 | mut bx: Bx, |
ba9703b0 | 302 | location: mir::Place<'tcx>, |
532ac7d7 XL |
303 | target: mir::BasicBlock, |
304 | unwind: Option<mir::BasicBlock>, | |
305 | ) { | |
f9f354fc | 306 | let ty = location.ty(self.mir, bx.tcx()).ty; |
fc512014 | 307 | let ty = self.monomorphize(ty); |
dc9dc135 | 308 | let drop_fn = Instance::resolve_drop_in_place(bx.tcx(), ty); |
532ac7d7 XL |
309 | |
310 | if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def { | |
311 | // we don't actually need to drop anything. | |
312 | helper.funclet_br(self, &mut bx, target); | |
dfeec247 | 313 | return; |
532ac7d7 XL |
314 | } |
315 | ||
74b04a01 | 316 | let place = self.codegen_place(&mut bx, location.as_ref()); |
532ac7d7 XL |
317 | let (args1, args2); |
318 | let mut args = if let Some(llextra) = place.llextra { | |
319 | args2 = [place.llval, llextra]; | |
320 | &args2[..] | |
321 | } else { | |
322 | args1 = [place.llval]; | |
323 | &args1[..] | |
324 | }; | |
1b1a35ee | 325 | let (drop_fn, fn_abi) = match ty.kind() { |
60c5eb7d XL |
326 | // FIXME(eddyb) perhaps move some of this logic into |
327 | // `Instance::resolve_drop_in_place`? | |
532ac7d7 | 328 | ty::Dynamic(..) => { |
60c5eb7d XL |
329 | let virtual_drop = Instance { |
330 | def: ty::InstanceDef::Virtual(drop_fn.def_id(), 0), | |
331 | substs: drop_fn.substs, | |
332 | }; | |
333 | let fn_abi = FnAbi::of_instance(&bx, virtual_drop, &[]); | |
532ac7d7 XL |
334 | let vtable = args[1]; |
335 | args = &args[..1]; | |
136023e0 XL |
336 | ( |
337 | meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_DROPINPLACE) | |
338 | .get_fn(&mut bx, vtable, &fn_abi), | |
339 | fn_abi, | |
340 | ) | |
54a0048b | 341 | } |
dfeec247 | 342 | _ => (bx.get_fn_addr(drop_fn), FnAbi::of_instance(&bx, drop_fn, &[])), |
532ac7d7 | 343 | }; |
dfeec247 XL |
344 | helper.do_call( |
345 | self, | |
346 | &mut bx, | |
347 | fn_abi, | |
348 | drop_fn, | |
349 | args, | |
350 | Some((ReturnDest::Nothing, target)), | |
351 | unwind, | |
352 | ); | |
532ac7d7 | 353 | } |
54a0048b | 354 | |
60c5eb7d | 355 | fn codegen_assert_terminator( |
532ac7d7 | 356 | &mut self, |
60c5eb7d | 357 | helper: TerminatorCodegenHelper<'tcx>, |
532ac7d7 XL |
358 | mut bx: Bx, |
359 | terminator: &mir::Terminator<'tcx>, | |
360 | cond: &mir::Operand<'tcx>, | |
361 | expected: bool, | |
362 | msg: &mir::AssertMessage<'tcx>, | |
363 | target: mir::BasicBlock, | |
364 | cleanup: Option<mir::BasicBlock>, | |
365 | ) { | |
366 | let span = terminator.source_info.span; | |
367 | let cond = self.codegen_operand(&mut bx, cond).immediate(); | |
368 | let mut const_cond = bx.const_to_opt_u128(cond, false).map(|c| c == 1); | |
369 | ||
370 | // This case can currently arise only from functions marked | |
371 | // with #[rustc_inherit_overflow_checks] and inlined from | |
372 | // another crate (mostly core::num generic/#[inline] fns), | |
373 | // while the current crate doesn't use overflow checks. | |
374 | // NOTE: Unlike binops, negation doesn't have its own | |
375 | // checked operation, just a comparison with the minimum | |
376 | // value, so we have to check for the assert message. | |
377 | if !bx.check_overflow() { | |
f035d41b | 378 | if let AssertKind::OverflowNeg(_) = *msg { |
532ac7d7 XL |
379 | const_cond = Some(expected); |
380 | } | |
381 | } | |
3157f602 | 382 | |
532ac7d7 XL |
383 | // Don't codegen the panic block if success if known. |
384 | if const_cond == Some(expected) { | |
385 | helper.funclet_br(self, &mut bx, target); | |
386 | return; | |
387 | } | |
388 | ||
389 | // Pass the condition through llvm.expect for branch hinting. | |
390 | let cond = bx.expect(cond, expected); | |
391 | ||
392 | // Create the failure block and the conditional branch to it. | |
393 | let lltarget = helper.llblock(self, target); | |
17df50a5 | 394 | let panic_block = bx.build_sibling_block("panic"); |
532ac7d7 XL |
395 | if expected { |
396 | bx.cond_br(cond, lltarget, panic_block.llbb()); | |
397 | } else { | |
398 | bx.cond_br(cond, panic_block.llbb(), lltarget); | |
399 | } | |
400 | ||
401 | // After this point, bx is the block for the call to panic. | |
402 | bx = panic_block; | |
403 | self.set_debug_loc(&mut bx, terminator.source_info); | |
404 | ||
405 | // Get the location information. | |
29967ef6 | 406 | let location = self.get_caller_location(&mut bx, terminator.source_info).immediate(); |
532ac7d7 XL |
407 | |
408 | // Put together the arguments to the panic entry point. | |
416331ca | 409 | let (lang_item, args) = match msg { |
74b04a01 | 410 | AssertKind::BoundsCheck { ref len, ref index } => { |
532ac7d7 XL |
411 | let len = self.codegen_operand(&mut bx, len).immediate(); |
412 | let index = self.codegen_operand(&mut bx, index).immediate(); | |
ba9703b0 XL |
413 | // It's `fn panic_bounds_check(index: usize, len: usize)`, |
414 | // and `#[track_caller]` adds an implicit third argument. | |
3dfed10e | 415 | (LangItem::PanicBoundsCheck, vec![index, len, location]) |
532ac7d7 XL |
416 | } |
417 | _ => { | |
e1599b0c | 418 | let msg_str = Symbol::intern(msg.description()); |
e74abb32 | 419 | let msg = bx.const_str(msg_str); |
ba9703b0 XL |
420 | // It's `pub fn panic(expr: &str)`, with the wide reference being passed |
421 | // as two arguments, and `#[track_caller]` adds an implicit third argument. | |
3dfed10e | 422 | (LangItem::Panic, vec![msg.0, msg.1, location]) |
3157f602 | 423 | } |
532ac7d7 | 424 | }; |
3157f602 | 425 | |
532ac7d7 XL |
426 | // Obtain the panic entry point. |
427 | let def_id = common::langcall(bx.tcx(), Some(span), "", lang_item); | |
428 | let instance = ty::Instance::mono(bx.tcx(), def_id); | |
60c5eb7d | 429 | let fn_abi = FnAbi::of_instance(&bx, instance, &[]); |
e74abb32 | 430 | let llfn = bx.get_fn_addr(instance); |
3157f602 | 431 | |
532ac7d7 | 432 | // Codegen the actual panic invoke/call. |
60c5eb7d | 433 | helper.do_call(self, &mut bx, fn_abi, llfn, &args, None, cleanup); |
532ac7d7 | 434 | } |
3157f602 | 435 | |
ba9703b0 XL |
436 | /// Returns `true` if this is indeed a panic intrinsic and codegen is done. |
437 | fn codegen_panic_intrinsic( | |
438 | &mut self, | |
439 | helper: &TerminatorCodegenHelper<'tcx>, | |
440 | bx: &mut Bx, | |
3dfed10e | 441 | intrinsic: Option<Symbol>, |
ba9703b0 | 442 | instance: Option<Instance<'tcx>>, |
29967ef6 | 443 | source_info: mir::SourceInfo, |
ba9703b0 XL |
444 | destination: &Option<(mir::Place<'tcx>, mir::BasicBlock)>, |
445 | cleanup: Option<mir::BasicBlock>, | |
446 | ) -> bool { | |
447 | // Emit a panic or a no-op for `assert_*` intrinsics. | |
448 | // These are intrinsics that compile to panics so that we can get a message | |
449 | // which mentions the offending type, even from a const context. | |
450 | #[derive(Debug, PartialEq)] | |
451 | enum AssertIntrinsic { | |
452 | Inhabited, | |
453 | ZeroValid, | |
454 | UninitValid, | |
fc512014 | 455 | } |
ba9703b0 | 456 | let panic_intrinsic = intrinsic.and_then(|i| match i { |
3dfed10e XL |
457 | sym::assert_inhabited => Some(AssertIntrinsic::Inhabited), |
458 | sym::assert_zero_valid => Some(AssertIntrinsic::ZeroValid), | |
459 | sym::assert_uninit_valid => Some(AssertIntrinsic::UninitValid), | |
ba9703b0 XL |
460 | _ => None, |
461 | }); | |
462 | if let Some(intrinsic) = panic_intrinsic { | |
463 | use AssertIntrinsic::*; | |
464 | let ty = instance.unwrap().substs.type_at(0); | |
465 | let layout = bx.layout_of(ty); | |
466 | let do_panic = match intrinsic { | |
467 | Inhabited => layout.abi.is_uninhabited(), | |
468 | // We unwrap as the error type is `!`. | |
469 | ZeroValid => !layout.might_permit_raw_init(bx, /*zero:*/ true).unwrap(), | |
470 | // We unwrap as the error type is `!`. | |
471 | UninitValid => !layout.might_permit_raw_init(bx, /*zero:*/ false).unwrap(), | |
472 | }; | |
473 | if do_panic { | |
1b1a35ee XL |
474 | let msg_str = with_no_trimmed_paths(|| { |
475 | if layout.abi.is_uninhabited() { | |
476 | // Use this error even for the other intrinsics as it is more precise. | |
477 | format!("attempted to instantiate uninhabited type `{}`", ty) | |
478 | } else if intrinsic == ZeroValid { | |
479 | format!("attempted to zero-initialize type `{}`, which is invalid", ty) | |
480 | } else { | |
481 | format!("attempted to leave type `{}` uninitialized, which is invalid", ty) | |
482 | } | |
483 | }); | |
ba9703b0 | 484 | let msg = bx.const_str(Symbol::intern(&msg_str)); |
29967ef6 | 485 | let location = self.get_caller_location(bx, source_info).immediate(); |
ba9703b0 XL |
486 | |
487 | // Obtain the panic entry point. | |
488 | // FIXME: dedup this with `codegen_assert_terminator` above. | |
29967ef6 XL |
489 | let def_id = |
490 | common::langcall(bx.tcx(), Some(source_info.span), "", LangItem::Panic); | |
ba9703b0 XL |
491 | let instance = ty::Instance::mono(bx.tcx(), def_id); |
492 | let fn_abi = FnAbi::of_instance(bx, instance, &[]); | |
493 | let llfn = bx.get_fn_addr(instance); | |
494 | ||
ba9703b0 XL |
495 | // Codegen the actual panic invoke/call. |
496 | helper.do_call( | |
497 | self, | |
498 | bx, | |
499 | fn_abi, | |
500 | llfn, | |
501 | &[msg.0, msg.1, location], | |
502 | destination.as_ref().map(|(_, bb)| (ReturnDest::Nothing, *bb)), | |
503 | cleanup, | |
504 | ); | |
505 | } else { | |
506 | // a NOP | |
507 | let target = destination.as_ref().unwrap().1; | |
ba9703b0 XL |
508 | helper.funclet_br(self, bx, target) |
509 | } | |
510 | true | |
511 | } else { | |
512 | false | |
513 | } | |
514 | } | |
515 | ||
60c5eb7d | 516 | fn codegen_call_terminator( |
532ac7d7 | 517 | &mut self, |
60c5eb7d | 518 | helper: TerminatorCodegenHelper<'tcx>, |
532ac7d7 XL |
519 | mut bx: Bx, |
520 | terminator: &mir::Terminator<'tcx>, | |
521 | func: &mir::Operand<'tcx>, | |
5869c6ff | 522 | args: &[mir::Operand<'tcx>], |
532ac7d7 XL |
523 | destination: &Option<(mir::Place<'tcx>, mir::BasicBlock)>, |
524 | cleanup: Option<mir::BasicBlock>, | |
f035d41b | 525 | fn_span: Span, |
532ac7d7 | 526 | ) { |
29967ef6 XL |
527 | let source_info = terminator.source_info; |
528 | let span = source_info.span; | |
529 | ||
532ac7d7 XL |
530 | // Create the callee. This is a fn ptr or zero-sized and hence a kind of scalar. |
531 | let callee = self.codegen_operand(&mut bx, func); | |
532 | ||
1b1a35ee | 533 | let (instance, mut llfn) = match *callee.layout.ty.kind() { |
dfeec247 XL |
534 | ty::FnDef(def_id, substs) => ( |
535 | Some( | |
536 | ty::Instance::resolve(bx.tcx(), ty::ParamEnv::reveal_all(), def_id, substs) | |
f9f354fc | 537 | .unwrap() |
3dfed10e XL |
538 | .unwrap() |
539 | .polymorphize(bx.tcx()), | |
dfeec247 XL |
540 | ), |
541 | None, | |
542 | ), | |
543 | ty::FnPtr(_) => (None, Some(callee.immediate())), | |
532ac7d7 XL |
544 | _ => bug!("{} is not callable", callee.layout.ty), |
545 | }; | |
546 | let def = instance.map(|i| i.def); | |
60c5eb7d XL |
547 | |
548 | if let Some(ty::InstanceDef::DropGlue(_, None)) = def { | |
549 | // Empty drop glue; a no-op. | |
550 | let &(_, target) = destination.as_ref().unwrap(); | |
60c5eb7d XL |
551 | helper.funclet_br(self, &mut bx, target); |
552 | return; | |
553 | } | |
554 | ||
555 | // FIXME(eddyb) avoid computing this if possible, when `instance` is | |
556 | // available - right now `sig` is only needed for getting the `abi` | |
557 | // and figuring out how many extra args were passed to a C-variadic `fn`. | |
532ac7d7 | 558 | let sig = callee.layout.ty.fn_sig(bx.tcx()); |
60c5eb7d | 559 | let abi = sig.abi(); |
532ac7d7 XL |
560 | |
561 | // Handle intrinsics old codegen wants Expr's for, ourselves. | |
562 | let intrinsic = match def { | |
3dfed10e | 563 | Some(ty::InstanceDef::Intrinsic(def_id)) => Some(bx.tcx().item_name(def_id)), |
dfeec247 | 564 | _ => None, |
532ac7d7 | 565 | }; |
3157f602 | 566 | |
60c5eb7d | 567 | let extra_args = &args[sig.inputs().skip_binder().len()..]; |
dfeec247 XL |
568 | let extra_args = extra_args |
569 | .iter() | |
570 | .map(|op_arg| { | |
f9f354fc | 571 | let op_ty = op_arg.ty(self.mir, bx.tcx()); |
fc512014 | 572 | self.monomorphize(op_ty) |
dfeec247 XL |
573 | }) |
574 | .collect::<Vec<_>>(); | |
60c5eb7d XL |
575 | |
576 | let fn_abi = match instance { | |
577 | Some(instance) => FnAbi::of_instance(&bx, instance, &extra_args), | |
dfeec247 | 578 | None => FnAbi::of_fn_ptr(&bx, sig, &extra_args), |
60c5eb7d XL |
579 | }; |
580 | ||
3dfed10e | 581 | if intrinsic == Some(sym::transmute) { |
532ac7d7 | 582 | if let Some(destination_ref) = destination.as_ref() { |
ba9703b0 | 583 | let &(dest, target) = destination_ref; |
532ac7d7 XL |
584 | self.codegen_transmute(&mut bx, &args[0], dest); |
585 | helper.funclet_br(self, &mut bx, target); | |
586 | } else { | |
587 | // If we are trying to transmute to an uninhabited type, | |
588 | // it is likely there is no allotted destination. In fact, | |
589 | // transmuting to an uninhabited type is UB, which means | |
590 | // we can do what we like. Here, we declare that transmuting | |
591 | // into an uninhabited type is impossible, so anything following | |
592 | // it must be unreachable. | |
ba9703b0 | 593 | assert_eq!(fn_abi.ret.layout.abi, abi::Abi::Uninhabited); |
532ac7d7 XL |
594 | bx.unreachable(); |
595 | } | |
596 | return; | |
597 | } | |
3157f602 | 598 | |
ba9703b0 XL |
599 | if self.codegen_panic_intrinsic( |
600 | &helper, | |
601 | &mut bx, | |
602 | intrinsic, | |
603 | instance, | |
29967ef6 | 604 | source_info, |
ba9703b0 XL |
605 | destination, |
606 | cleanup, | |
607 | ) { | |
532ac7d7 XL |
608 | return; |
609 | } | |
54a0048b | 610 | |
532ac7d7 | 611 | // The arguments we'll be passing. Plus one to account for outptr, if used. |
60c5eb7d | 612 | let arg_count = fn_abi.args.len() + fn_abi.ret.is_indirect() as usize; |
532ac7d7 | 613 | let mut llargs = Vec::with_capacity(arg_count); |
3157f602 | 614 | |
532ac7d7 | 615 | // Prepare the return value destination |
ba9703b0 | 616 | let ret_dest = if let Some((dest, _)) = *destination { |
532ac7d7 | 617 | let is_intrinsic = intrinsic.is_some(); |
dfeec247 | 618 | self.make_return_dest(&mut bx, dest, &fn_abi.ret, &mut llargs, is_intrinsic) |
532ac7d7 XL |
619 | } else { |
620 | ReturnDest::Nothing | |
621 | }; | |
54a0048b | 622 | |
3dfed10e | 623 | if intrinsic == Some(sym::caller_location) { |
e74abb32 | 624 | if let Some((_, target)) = destination.as_ref() { |
29967ef6 XL |
625 | let location = self |
626 | .get_caller_location(&mut bx, mir::SourceInfo { span: fn_span, ..source_info }); | |
e74abb32 XL |
627 | |
628 | if let ReturnDest::IndirectOperand(tmp, _) = ret_dest { | |
629 | location.val.store(&mut bx, tmp); | |
630 | } | |
60c5eb7d | 631 | self.store_return(&mut bx, ret_dest, &fn_abi.ret, location.immediate()); |
e74abb32 XL |
632 | helper.funclet_br(self, &mut bx, *target); |
633 | } | |
634 | return; | |
635 | } | |
636 | ||
6a06907d XL |
637 | match intrinsic { |
638 | None | Some(sym::drop_in_place) => {} | |
639 | Some(sym::copy_nonoverlapping) => unreachable!(), | |
640 | Some(intrinsic) => { | |
641 | let dest = match ret_dest { | |
642 | _ if fn_abi.ret.is_indirect() => llargs[0], | |
643 | ReturnDest::Nothing => { | |
644 | bx.const_undef(bx.type_ptr_to(bx.arg_memory_ty(&fn_abi.ret))) | |
645 | } | |
646 | ReturnDest::IndirectOperand(dst, _) | ReturnDest::Store(dst) => dst.llval, | |
647 | ReturnDest::DirectOperand(_) => { | |
648 | bug!("Cannot use direct operand with an intrinsic call") | |
649 | } | |
650 | }; | |
54a0048b | 651 | |
6a06907d XL |
652 | let args: Vec<_> = args |
653 | .iter() | |
654 | .enumerate() | |
655 | .map(|(i, arg)| { | |
656 | // The indices passed to simd_shuffle* in the | |
657 | // third argument must be constant. This is | |
658 | // checked by const-qualification, which also | |
659 | // promotes any complex rvalues to constants. | |
660 | if i == 2 && intrinsic.as_str().starts_with("simd_shuffle") { | |
661 | if let mir::Operand::Constant(constant) = arg { | |
662 | let c = self.eval_mir_constant(constant); | |
663 | let (llval, ty) = | |
664 | self.simd_shuffle_indices(&bx, constant.span, constant.ty(), c); | |
665 | return OperandRef { | |
666 | val: Immediate(llval), | |
667 | layout: bx.layout_of(ty), | |
668 | }; | |
669 | } else { | |
670 | span_bug!(span, "shuffle indices must be constant"); | |
671 | } | |
532ac7d7 | 672 | } |
54a0048b | 673 | |
6a06907d XL |
674 | self.codegen_operand(&mut bx, arg) |
675 | }) | |
676 | .collect(); | |
677 | ||
678 | Self::codegen_intrinsic_call( | |
679 | &mut bx, | |
680 | *instance.as_ref().unwrap(), | |
681 | &fn_abi, | |
682 | &args, | |
683 | dest, | |
684 | span, | |
685 | ); | |
dfeec247 | 686 | |
6a06907d XL |
687 | if let ReturnDest::IndirectOperand(dst, _) = ret_dest { |
688 | self.store_return(&mut bx, ret_dest, &fn_abi.ret, dst.llval); | |
689 | } | |
532ac7d7 | 690 | |
6a06907d XL |
691 | if let Some((_, target)) = *destination { |
692 | helper.funclet_br(self, &mut bx, target); | |
693 | } else { | |
694 | bx.unreachable(); | |
695 | } | |
532ac7d7 | 696 | |
6a06907d | 697 | return; |
532ac7d7 | 698 | } |
532ac7d7 XL |
699 | } |
700 | ||
701 | // Split the rust-call tupled arguments off. | |
702 | let (first_args, untuple) = if abi == Abi::RustCall && !args.is_empty() { | |
703 | let (tup, args) = args.split_last().unwrap(); | |
704 | (args, Some(tup)) | |
705 | } else { | |
6a06907d | 706 | (args, None) |
532ac7d7 XL |
707 | }; |
708 | ||
532ac7d7 | 709 | 'make_args: for (i, arg) in first_args.iter().enumerate() { |
532ac7d7 XL |
710 | let mut op = self.codegen_operand(&mut bx, arg); |
711 | ||
712 | if let (0, Some(ty::InstanceDef::Virtual(_, idx))) = (i, def) { | |
713 | if let Pair(..) = op.val { | |
714 | // In the case of Rc<Self>, we need to explicitly pass a | |
715 | // *mut RcBox<Self> with a Scalar (not ScalarPair) ABI. This is a hack | |
716 | // that is understood elsewhere in the compiler as a method on | |
717 | // `dyn Trait`. | |
718 | // To get a `*mut RcBox<Self>`, we just keep unwrapping newtypes until | |
719 | // we get a value of a built-in pointer type | |
720 | 'descend_newtypes: while !op.layout.ty.is_unsafe_ptr() | |
dfeec247 | 721 | && !op.layout.ty.is_region_ptr() |
532ac7d7 | 722 | { |
60c5eb7d | 723 | for i in 0..op.layout.fields.count() { |
532ac7d7 XL |
724 | let field = op.extract_field(&mut bx, i); |
725 | if !field.layout.is_zst() { | |
726 | // we found the one non-zero-sized field that is allowed | |
727 | // now find *its* non-zero-sized field, or stop if it's a | |
728 | // pointer | |
729 | op = field; | |
dfeec247 | 730 | continue 'descend_newtypes; |
532ac7d7 XL |
731 | } |
732 | } | |
733 | ||
734 | span_bug!(span, "receiver has no non-zero-sized fields {:?}", op); | |
32a655c1 | 735 | } |
54a0048b | 736 | |
532ac7d7 XL |
737 | // now that we have `*dyn Trait` or `&dyn Trait`, split it up into its |
738 | // data pointer and vtable. Look up the method in the vtable, and pass | |
739 | // the data pointer as the first argument | |
740 | match op.val { | |
741 | Pair(data_ptr, meta) => { | |
dfeec247 XL |
742 | llfn = Some( |
743 | meth::VirtualIndex::from_index(idx).get_fn(&mut bx, meta, &fn_abi), | |
744 | ); | |
532ac7d7 | 745 | llargs.push(data_ptr); |
dfeec247 | 746 | continue 'make_args; |
532ac7d7 XL |
747 | } |
748 | other => bug!("expected a Pair, got {:?}", other), | |
0731742a | 749 | } |
532ac7d7 XL |
750 | } else if let Ref(data_ptr, Some(meta), _) = op.val { |
751 | // by-value dynamic dispatch | |
dfeec247 | 752 | llfn = Some(meth::VirtualIndex::from_index(idx).get_fn(&mut bx, meta, &fn_abi)); |
532ac7d7 XL |
753 | llargs.push(data_ptr); |
754 | continue; | |
755 | } else { | |
756 | span_bug!(span, "can't codegen a virtual call on {:?}", op); | |
0bf4aa26 | 757 | } |
532ac7d7 | 758 | } |
0bf4aa26 | 759 | |
532ac7d7 XL |
760 | // The callee needs to own the argument memory if we pass it |
761 | // by-ref, so make a local copy of non-immediate constants. | |
762 | match (arg, op.val) { | |
dfeec247 XL |
763 | (&mir::Operand::Copy(_), Ref(_, None, _)) |
764 | | (&mir::Operand::Constant(_), Ref(_, None, _)) => { | |
e1599b0c | 765 | let tmp = PlaceRef::alloca(&mut bx, op.layout); |
532ac7d7 XL |
766 | op.val.store(&mut bx, tmp); |
767 | op.val = Ref(tmp.llval, None, tmp.align); | |
768 | } | |
769 | _ => {} | |
770 | } | |
54a0048b | 771 | |
60c5eb7d | 772 | self.codegen_argument(&mut bx, op, &mut llargs, &fn_abi.args[i]); |
532ac7d7 XL |
773 | } |
774 | if let Some(tup) = untuple { | |
dfeec247 XL |
775 | self.codegen_arguments_untupled( |
776 | &mut bx, | |
777 | tup, | |
778 | &mut llargs, | |
779 | &fn_abi.args[first_args.len()..], | |
780 | ) | |
60c5eb7d XL |
781 | } |
782 | ||
783 | let needs_location = | |
784 | instance.map_or(false, |i| i.def.requires_caller_location(self.cx.tcx())); | |
785 | if needs_location { | |
786 | assert_eq!( | |
dfeec247 XL |
787 | fn_abi.args.len(), |
788 | args.len() + 1, | |
60c5eb7d XL |
789 | "#[track_caller] fn's must have 1 more argument in their ABI than in their MIR", |
790 | ); | |
29967ef6 XL |
791 | let location = |
792 | self.get_caller_location(&mut bx, mir::SourceInfo { span: fn_span, ..source_info }); | |
f035d41b XL |
793 | debug!( |
794 | "codegen_call_terminator({:?}): location={:?} (fn_span {:?})", | |
795 | terminator, location, fn_span | |
796 | ); | |
797 | ||
60c5eb7d XL |
798 | let last_arg = fn_abi.args.last().unwrap(); |
799 | self.codegen_argument(&mut bx, location, &mut llargs, last_arg); | |
532ac7d7 | 800 | } |
54a0048b | 801 | |
532ac7d7 XL |
802 | let fn_ptr = match (llfn, instance) { |
803 | (Some(llfn), _) => llfn, | |
e74abb32 | 804 | (None, Some(instance)) => bx.get_fn_addr(instance), |
532ac7d7 XL |
805 | _ => span_bug!(span, "no llfn for call"), |
806 | }; | |
54a0048b | 807 | |
dfeec247 XL |
808 | helper.do_call( |
809 | self, | |
810 | &mut bx, | |
811 | fn_abi, | |
812 | fn_ptr, | |
813 | &llargs, | |
814 | destination.as_ref().map(|&(_, target)| (ret_dest, target)), | |
815 | cleanup, | |
816 | ); | |
532ac7d7 | 817 | } |
f9f354fc XL |
818 | |
819 | fn codegen_asm_terminator( | |
820 | &mut self, | |
821 | helper: TerminatorCodegenHelper<'tcx>, | |
822 | mut bx: Bx, | |
823 | terminator: &mir::Terminator<'tcx>, | |
824 | template: &[ast::InlineAsmTemplatePiece], | |
825 | operands: &[mir::InlineAsmOperand<'tcx>], | |
826 | options: ast::InlineAsmOptions, | |
827 | line_spans: &[Span], | |
828 | destination: Option<mir::BasicBlock>, | |
829 | ) { | |
830 | let span = terminator.source_info.span; | |
831 | ||
832 | let operands: Vec<_> = operands | |
833 | .iter() | |
834 | .map(|op| match *op { | |
835 | mir::InlineAsmOperand::In { reg, ref value } => { | |
836 | let value = self.codegen_operand(&mut bx, value); | |
837 | InlineAsmOperandRef::In { reg, value } | |
838 | } | |
839 | mir::InlineAsmOperand::Out { reg, late, ref place } => { | |
840 | let place = place.map(|place| self.codegen_place(&mut bx, place.as_ref())); | |
841 | InlineAsmOperandRef::Out { reg, late, place } | |
842 | } | |
843 | mir::InlineAsmOperand::InOut { reg, late, ref in_value, ref out_place } => { | |
844 | let in_value = self.codegen_operand(&mut bx, in_value); | |
845 | let out_place = | |
846 | out_place.map(|out_place| self.codegen_place(&mut bx, out_place.as_ref())); | |
847 | InlineAsmOperandRef::InOut { reg, late, in_value, out_place } | |
848 | } | |
849 | mir::InlineAsmOperand::Const { ref value } => { | |
cdc7bbd5 XL |
850 | let const_value = self |
851 | .eval_mir_constant(value) | |
852 | .unwrap_or_else(|_| span_bug!(span, "asm const cannot be resolved")); | |
17df50a5 XL |
853 | let string = common::asm_const_to_str( |
854 | bx.tcx(), | |
855 | span, | |
856 | const_value, | |
857 | bx.layout_of(value.ty()), | |
858 | ); | |
cdc7bbd5 | 859 | InlineAsmOperandRef::Const { string } |
f9f354fc XL |
860 | } |
861 | mir::InlineAsmOperand::SymFn { ref value } => { | |
fc512014 | 862 | let literal = self.monomorphize(value.literal); |
6a06907d | 863 | if let ty::FnDef(def_id, substs) = *literal.ty().kind() { |
f9f354fc XL |
864 | let instance = ty::Instance::resolve_for_fn_ptr( |
865 | bx.tcx(), | |
866 | ty::ParamEnv::reveal_all(), | |
867 | def_id, | |
868 | substs, | |
869 | ) | |
870 | .unwrap(); | |
871 | InlineAsmOperandRef::SymFn { instance } | |
872 | } else { | |
873 | span_bug!(span, "invalid type for asm sym (fn)"); | |
874 | } | |
875 | } | |
f035d41b XL |
876 | mir::InlineAsmOperand::SymStatic { def_id } => { |
877 | InlineAsmOperandRef::SymStatic { def_id } | |
f9f354fc XL |
878 | } |
879 | }) | |
880 | .collect(); | |
881 | ||
882 | bx.codegen_inline_asm(template, &operands, options, line_spans); | |
883 | ||
884 | if let Some(target) = destination { | |
885 | helper.funclet_br(self, &mut bx, target); | |
886 | } else { | |
887 | bx.unreachable(); | |
888 | } | |
889 | } | |
532ac7d7 | 890 | } |
ff7c6d11 | 891 | |
dc9dc135 | 892 | impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { |
dfeec247 | 893 | pub fn codegen_block(&mut self, bb: mir::BasicBlock) { |
532ac7d7 | 894 | let mut bx = self.build_block(bb); |
60c5eb7d XL |
895 | let mir = self.mir; |
896 | let data = &mir[bb]; | |
ff7c6d11 | 897 | |
532ac7d7 | 898 | debug!("codegen_block({:?}={:?})", bb, data); |
ff7c6d11 | 899 | |
532ac7d7 XL |
900 | for statement in &data.statements { |
901 | bx = self.codegen_statement(bx, statement); | |
902 | } | |
cc61c64b | 903 | |
532ac7d7 XL |
904 | self.codegen_terminator(bx, bb, data.terminator()); |
905 | } | |
cc61c64b | 906 | |
532ac7d7 XL |
907 | fn codegen_terminator( |
908 | &mut self, | |
909 | mut bx: Bx, | |
910 | bb: mir::BasicBlock, | |
dfeec247 | 911 | terminator: &'tcx mir::Terminator<'tcx>, |
532ac7d7 XL |
912 | ) { |
913 | debug!("codegen_terminator: {:?}", terminator); | |
cc61c64b | 914 | |
532ac7d7 XL |
915 | // Create the cleanup bundle, if needed. |
916 | let funclet_bb = self.cleanup_kinds[bb].funclet_bb(bb); | |
dfeec247 | 917 | let helper = TerminatorCodegenHelper { bb, terminator, funclet_bb }; |
cc61c64b | 918 | |
532ac7d7 XL |
919 | self.set_debug_loc(&mut bx, terminator.source_info); |
920 | match terminator.kind { | |
dfeec247 | 921 | mir::TerminatorKind::Resume => self.codegen_resume_terminator(helper, bx), |
ff7c6d11 | 922 | |
532ac7d7 XL |
923 | mir::TerminatorKind::Abort => { |
924 | bx.abort(); | |
60c5eb7d XL |
925 | // `abort` does not terminate the block, so we still need to generate |
926 | // an `unreachable` terminator after it. | |
532ac7d7 XL |
927 | bx.unreachable(); |
928 | } | |
a1dfa0c6 | 929 | |
532ac7d7 | 930 | mir::TerminatorKind::Goto { target } => { |
29967ef6 | 931 | if bb == target { |
6a06907d XL |
932 | // This is an unconditional branch back to this same basic block. That means we |
933 | // have something like a `loop {}` statement. LLVM versions before 12.0 | |
934 | // miscompile this because they assume forward progress. For older versions | |
935 | // try to handle just this specific case which comes up commonly in practice | |
936 | // (e.g., in embedded code). | |
29967ef6 | 937 | // |
6a06907d XL |
938 | // NB: the `sideeffect` currently checks for the LLVM version used internally. |
939 | bx.sideeffect(); | |
29967ef6 | 940 | } |
6a06907d | 941 | |
532ac7d7 XL |
942 | helper.funclet_br(self, &mut bx, target); |
943 | } | |
ff7c6d11 | 944 | |
29967ef6 XL |
945 | mir::TerminatorKind::SwitchInt { ref discr, switch_ty, ref targets } => { |
946 | self.codegen_switchint_terminator(helper, bx, discr, switch_ty, targets); | |
532ac7d7 | 947 | } |
ff7c6d11 | 948 | |
532ac7d7 XL |
949 | mir::TerminatorKind::Return => { |
950 | self.codegen_return_terminator(bx); | |
951 | } | |
ff7c6d11 | 952 | |
532ac7d7 XL |
953 | mir::TerminatorKind::Unreachable => { |
954 | bx.unreachable(); | |
955 | } | |
54a0048b | 956 | |
f035d41b XL |
957 | mir::TerminatorKind::Drop { place, target, unwind } => { |
958 | self.codegen_drop_terminator(helper, bx, place, target, unwind); | |
532ac7d7 XL |
959 | } |
960 | ||
961 | mir::TerminatorKind::Assert { ref cond, expected, ref msg, target, cleanup } => { | |
dfeec247 XL |
962 | self.codegen_assert_terminator( |
963 | helper, bx, terminator, cond, expected, msg, target, cleanup, | |
964 | ); | |
532ac7d7 XL |
965 | } |
966 | ||
967 | mir::TerminatorKind::DropAndReplace { .. } => { | |
968 | bug!("undesugared DropAndReplace in codegen: {:?}", terminator); | |
969 | } | |
970 | ||
971 | mir::TerminatorKind::Call { | |
972 | ref func, | |
973 | ref args, | |
974 | ref destination, | |
975 | cleanup, | |
dfeec247 | 976 | from_hir_call: _, |
f035d41b | 977 | fn_span, |
532ac7d7 | 978 | } => { |
dfeec247 XL |
979 | self.codegen_call_terminator( |
980 | helper, | |
981 | bx, | |
982 | terminator, | |
983 | func, | |
984 | args, | |
985 | destination, | |
986 | cleanup, | |
f035d41b | 987 | fn_span, |
dfeec247 XL |
988 | ); |
989 | } | |
990 | mir::TerminatorKind::GeneratorDrop | mir::TerminatorKind::Yield { .. } => { | |
991 | bug!("generator ops in codegen") | |
992 | } | |
f035d41b | 993 | mir::TerminatorKind::FalseEdge { .. } | mir::TerminatorKind::FalseUnwind { .. } => { |
dfeec247 | 994 | bug!("borrowck false edges in codegen") |
54a0048b | 995 | } |
f9f354fc XL |
996 | |
997 | mir::TerminatorKind::InlineAsm { | |
998 | template, | |
999 | ref operands, | |
1000 | options, | |
1001 | line_spans, | |
1002 | destination, | |
1003 | } => { | |
1004 | self.codegen_asm_terminator( | |
1005 | helper, | |
1006 | bx, | |
1007 | terminator, | |
1008 | template, | |
1009 | operands, | |
1010 | options, | |
1011 | line_spans, | |
1012 | destination, | |
1013 | ); | |
1014 | } | |
54a0048b SL |
1015 | } |
1016 | } | |
1017 | ||
a1dfa0c6 XL |
1018 | fn codegen_argument( |
1019 | &mut self, | |
1020 | bx: &mut Bx, | |
1021 | op: OperandRef<'tcx, Bx::Value>, | |
1022 | llargs: &mut Vec<Bx::Value>, | |
dfeec247 | 1023 | arg: &ArgAbi<'tcx, Ty<'tcx>>, |
a1dfa0c6 | 1024 | ) { |
54a0048b SL |
1025 | // Fill padding with undef value, where applicable. |
1026 | if let Some(ty) = arg.pad { | |
a1dfa0c6 | 1027 | llargs.push(bx.const_undef(bx.reg_backend_type(&ty))) |
54a0048b SL |
1028 | } |
1029 | ||
1030 | if arg.is_ignore() { | |
1031 | return; | |
1032 | } | |
1033 | ||
ff7c6d11 XL |
1034 | if let PassMode::Pair(..) = arg.mode { |
1035 | match op.val { | |
1036 | Pair(a, b) => { | |
1037 | llargs.push(a); | |
1038 | llargs.push(b); | |
1039 | return; | |
1040 | } | |
dfeec247 | 1041 | _ => bug!("codegen_argument: {:?} invalid for pair argument", op), |
b7449926 XL |
1042 | } |
1043 | } else if arg.is_unsized_indirect() { | |
1044 | match op.val { | |
1045 | Ref(a, Some(b), _) => { | |
1046 | llargs.push(a); | |
1047 | llargs.push(b); | |
1048 | return; | |
1049 | } | |
dfeec247 | 1050 | _ => bug!("codegen_argument: {:?} invalid for unsized indirect argument", op), |
ff7c6d11 XL |
1051 | } |
1052 | } | |
1053 | ||
54a0048b | 1054 | // Force by-ref if we have to load through a cast pointer. |
32a655c1 | 1055 | let (mut llval, align, by_ref) = match op.val { |
dfeec247 | 1056 | Immediate(_) | Pair(..) => match arg.mode { |
fc512014 | 1057 | PassMode::Indirect { .. } | PassMode::Cast(_) => { |
dfeec247 XL |
1058 | let scratch = PlaceRef::alloca(bx, arg.layout); |
1059 | op.val.store(bx, scratch); | |
1060 | (scratch.llval, scratch.align, true) | |
3157f602 | 1061 | } |
dfeec247 XL |
1062 | _ => (op.immediate_or_packed_pair(bx), arg.layout.align.abi, false), |
1063 | }, | |
b7449926 | 1064 | Ref(llval, _, align) => { |
a1dfa0c6 | 1065 | if arg.is_indirect() && align < arg.layout.align.abi { |
ff7c6d11 XL |
1066 | // `foo(packed.large_field)`. We can't pass the (unaligned) field directly. I |
1067 | // think that ATM (Rust 1.16) we only pass temporaries, but we shouldn't | |
1068 | // have scary latent bugs around. | |
1069 | ||
e1599b0c | 1070 | let scratch = PlaceRef::alloca(bx, arg.layout); |
dfeec247 XL |
1071 | base::memcpy_ty( |
1072 | bx, | |
1073 | scratch.llval, | |
1074 | scratch.align, | |
1075 | llval, | |
1076 | align, | |
1077 | op.layout, | |
1078 | MemFlags::empty(), | |
1079 | ); | |
ff7c6d11 XL |
1080 | (scratch.llval, scratch.align, true) |
1081 | } else { | |
1082 | (llval, align, true) | |
1083 | } | |
32a655c1 | 1084 | } |
54a0048b SL |
1085 | }; |
1086 | ||
1087 | if by_ref && !arg.is_indirect() { | |
1088 | // Have to load the argument, maybe while casting it. | |
ff7c6d11 | 1089 | if let PassMode::Cast(ty) = arg.mode { |
136023e0 XL |
1090 | let llty = bx.cast_backend_type(&ty); |
1091 | let addr = bx.pointercast(llval, bx.type_ptr_to(llty)); | |
1092 | llval = bx.load(llty, addr, align.min(arg.layout.align.abi)); | |
54a0048b | 1093 | } else { |
ff7c6d11 XL |
1094 | // We can't use `PlaceRef::load` here because the argument |
1095 | // may have a type we don't treat as immediate, but the ABI | |
1096 | // used for this call is passing it by-value. In that case, | |
1097 | // the load would just produce `OperandValue::Ref` instead | |
1098 | // of the `OperandValue::Immediate` we need for the call. | |
136023e0 | 1099 | llval = bx.load(bx.backend_type(arg.layout), llval, align); |
ba9703b0 | 1100 | if let abi::Abi::Scalar(ref scalar) = arg.layout.abi { |
ff7c6d11 | 1101 | if scalar.is_bool() { |
2c00a5a8 | 1102 | bx.range_metadata(llval, 0..2); |
ff7c6d11 XL |
1103 | } |
1104 | } | |
dc9dc135 | 1105 | // We store bools as `i8` so we need to truncate to `i1`. |
1b1a35ee | 1106 | llval = bx.to_immediate(llval, arg.layout); |
54a0048b SL |
1107 | } |
1108 | } | |
1109 | ||
1110 | llargs.push(llval); | |
1111 | } | |
1112 | ||
a1dfa0c6 XL |
1113 | fn codegen_arguments_untupled( |
1114 | &mut self, | |
1115 | bx: &mut Bx, | |
1116 | operand: &mir::Operand<'tcx>, | |
1117 | llargs: &mut Vec<Bx::Value>, | |
dfeec247 | 1118 | args: &[ArgAbi<'tcx, Ty<'tcx>>], |
a1dfa0c6 | 1119 | ) { |
94b46f34 | 1120 | let tuple = self.codegen_operand(bx, operand); |
54a0048b | 1121 | |
a7813a04 | 1122 | // Handle both by-ref and immediate tuples. |
b7449926 | 1123 | if let Ref(llval, None, align) = tuple.val { |
e1599b0c | 1124 | let tuple_ptr = PlaceRef::new_sized_aligned(llval, tuple.layout, align); |
ff7c6d11 | 1125 | for i in 0..tuple.layout.fields.count() { |
2c00a5a8 | 1126 | let field_ptr = tuple_ptr.project_field(bx, i); |
a1dfa0c6 XL |
1127 | let field = bx.load_operand(field_ptr); |
1128 | self.codegen_argument(bx, field, llargs, &args[i]); | |
3157f602 | 1129 | } |
b7449926 XL |
1130 | } else if let Ref(_, Some(_), _) = tuple.val { |
1131 | bug!("closure arguments must be sized") | |
ff7c6d11 XL |
1132 | } else { |
1133 | // If the tuple is immediate, the elements are as well. | |
1134 | for i in 0..tuple.layout.fields.count() { | |
2c00a5a8 | 1135 | let op = tuple.extract_field(bx, i); |
94b46f34 | 1136 | self.codegen_argument(bx, op, llargs, &args[i]); |
a7813a04 | 1137 | } |
54a0048b SL |
1138 | } |
1139 | } | |
1140 | ||
29967ef6 XL |
1141 | fn get_caller_location( |
1142 | &mut self, | |
1143 | bx: &mut Bx, | |
1144 | mut source_info: mir::SourceInfo, | |
1145 | ) -> OperandRef<'tcx, Bx::Value> { | |
1146 | let tcx = bx.tcx(); | |
1147 | ||
1148 | let mut span_to_caller_location = |span: Span| { | |
60c5eb7d | 1149 | let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span); |
29967ef6 XL |
1150 | let caller = tcx.sess.source_map().lookup_char_pos(topmost.lo()); |
1151 | let const_loc = tcx.const_caller_location(( | |
17df50a5 | 1152 | Symbol::intern(&caller.file.name.prefer_remapped().to_string_lossy()), |
60c5eb7d XL |
1153 | caller.line as u32, |
1154 | caller.col_display as u32 + 1, | |
1155 | )); | |
74b04a01 | 1156 | OperandRef::from_const(bx, const_loc, bx.tcx().caller_location_ty()) |
29967ef6 XL |
1157 | }; |
1158 | ||
1159 | // Walk up the `SourceScope`s, in case some of them are from MIR inlining. | |
1160 | // If so, the starting `source_info.span` is in the innermost inlined | |
1161 | // function, and will be replaced with outer callsite spans as long | |
1162 | // as the inlined functions were `#[track_caller]`. | |
1163 | loop { | |
1164 | let scope_data = &self.mir.source_scopes[source_info.scope]; | |
1165 | ||
1166 | if let Some((callee, callsite_span)) = scope_data.inlined { | |
1167 | // Stop inside the most nested non-`#[track_caller]` function, | |
1168 | // before ever reaching its caller (which is irrelevant). | |
1169 | if !callee.def.requires_caller_location(tcx) { | |
1170 | return span_to_caller_location(source_info.span); | |
1171 | } | |
1172 | source_info.span = callsite_span; | |
1173 | } | |
1174 | ||
1175 | // Skip past all of the parents with `inlined: None`. | |
1176 | match scope_data.inlined_parent_scope { | |
1177 | Some(parent) => source_info.scope = parent, | |
1178 | None => break, | |
1179 | } | |
1180 | } | |
1181 | ||
1182 | // No inlined `SourceScope`s, or all of them were `#[track_caller]`. | |
1183 | self.caller_location.unwrap_or_else(|| span_to_caller_location(source_info.span)) | |
e74abb32 XL |
1184 | } |
1185 | ||
dfeec247 | 1186 | fn get_personality_slot(&mut self, bx: &mut Bx) -> PlaceRef<'tcx, Bx::Value> { |
a1dfa0c6 | 1187 | let cx = bx.cx(); |
ff7c6d11 | 1188 | if let Some(slot) = self.personality_slot { |
54a0048b SL |
1189 | slot |
1190 | } else { | |
dfeec247 XL |
1191 | let layout = cx.layout_of( |
1192 | cx.tcx().intern_tup(&[cx.tcx().mk_mut_ptr(cx.tcx().types.u8), cx.tcx().types.i32]), | |
1193 | ); | |
e1599b0c | 1194 | let slot = PlaceRef::alloca(bx, layout); |
ff7c6d11 | 1195 | self.personality_slot = Some(slot); |
32a655c1 | 1196 | slot |
54a0048b SL |
1197 | } |
1198 | } | |
1199 | ||
17df50a5 XL |
1200 | /// Returns the landing/cleanup pad wrapper around the given basic block. |
1201 | // FIXME(eddyb) rename this to `eh_pad_for`. | |
1202 | fn landing_pad_for(&mut self, bb: mir::BasicBlock) -> Bx::BasicBlock { | |
1203 | if let Some(landing_pad) = self.landing_pads[bb] { | |
1204 | return landing_pad; | |
3157f602 XL |
1205 | } |
1206 | ||
17df50a5 XL |
1207 | let landing_pad = self.landing_pad_for_uncached(bb); |
1208 | self.landing_pads[bb] = Some(landing_pad); | |
cc61c64b XL |
1209 | landing_pad |
1210 | } | |
1211 | ||
17df50a5 XL |
1212 | // FIXME(eddyb) rename this to `eh_pad_for_uncached`. |
1213 | fn landing_pad_for_uncached(&mut self, bb: mir::BasicBlock) -> Bx::BasicBlock { | |
1214 | let llbb = self.llbb(bb); | |
2c00a5a8 | 1215 | if base::wants_msvc_seh(self.cx.sess()) { |
17df50a5 XL |
1216 | let funclet; |
1217 | let ret_llbb; | |
1218 | match self.mir[bb].terminator.as_ref().map(|t| &t.kind) { | |
1219 | // This is a basic block that we're aborting the program for, | |
1220 | // notably in an `extern` function. These basic blocks are inserted | |
1221 | // so that we assert that `extern` functions do indeed not panic, | |
1222 | // and if they do we abort the process. | |
1223 | // | |
1224 | // On MSVC these are tricky though (where we're doing funclets). If | |
1225 | // we were to do a cleanuppad (like below) the normal functions like | |
1226 | // `longjmp` would trigger the abort logic, terminating the | |
1227 | // program. Instead we insert the equivalent of `catch(...)` for C++ | |
1228 | // which magically doesn't trigger when `longjmp` files over this | |
1229 | // frame. | |
1230 | // | |
1231 | // Lots more discussion can be found on #48251 but this codegen is | |
1232 | // modeled after clang's for: | |
1233 | // | |
1234 | // try { | |
1235 | // foo(); | |
1236 | // } catch (...) { | |
1237 | // bar(); | |
1238 | // } | |
1239 | Some(&mir::TerminatorKind::Abort) => { | |
1240 | let mut cs_bx = self.new_block(&format!("cs_funclet{:?}", bb)); | |
1241 | let mut cp_bx = self.new_block(&format!("cp_funclet{:?}", bb)); | |
1242 | ret_llbb = cs_bx.llbb(); | |
1243 | ||
1244 | let cs = cs_bx.catch_switch(None, None, 1); | |
1245 | cs_bx.add_handler(cs, cp_bx.llbb()); | |
1246 | ||
1247 | // The "null" here is actually a RTTI type descriptor for the | |
1248 | // C++ personality function, but `catch (...)` has no type so | |
1249 | // it's null. The 64 here is actually a bitfield which | |
1250 | // represents that this is a catch-all block. | |
1251 | let null = cp_bx.const_null( | |
1252 | cp_bx.type_i8p_ext(cp_bx.cx().data_layout().instruction_address_space), | |
1253 | ); | |
1254 | let sixty_four = cp_bx.const_i32(64); | |
1255 | funclet = cp_bx.catch_pad(cs, &[null, sixty_four, null]); | |
1256 | cp_bx.br(llbb); | |
1257 | } | |
1258 | _ => { | |
1259 | let mut cleanup_bx = self.new_block(&format!("funclet_{:?}", bb)); | |
1260 | ret_llbb = cleanup_bx.llbb(); | |
1261 | funclet = cleanup_bx.cleanup_pad(None, &[]); | |
1262 | cleanup_bx.br(llbb); | |
1263 | } | |
1264 | } | |
1265 | self.funclets[bb] = Some(funclet); | |
1266 | ret_llbb | |
1267 | } else { | |
1268 | let mut bx = self.new_block("cleanup"); | |
3157f602 | 1269 | |
17df50a5 XL |
1270 | let llpersonality = self.cx.eh_personality(); |
1271 | let llretty = self.landing_pad_type(); | |
1272 | let lp = bx.landing_pad(llretty, llpersonality, 1); | |
1273 | bx.set_cleanup(lp); | |
ff7c6d11 | 1274 | |
17df50a5 XL |
1275 | let slot = self.get_personality_slot(&mut bx); |
1276 | slot.storage_live(&mut bx); | |
1277 | Pair(bx.extract_value(lp, 0), bx.extract_value(lp, 1)).store(&mut bx, slot); | |
ff7c6d11 | 1278 | |
17df50a5 XL |
1279 | bx.br(llbb); |
1280 | bx.llbb() | |
1281 | } | |
54a0048b SL |
1282 | } |
1283 | ||
a1dfa0c6 | 1284 | fn landing_pad_type(&self) -> Bx::Type { |
2c00a5a8 | 1285 | let cx = self.cx; |
a1dfa0c6 | 1286 | cx.type_struct(&[cx.type_i8p(), cx.type_i32()], false) |
ff7c6d11 XL |
1287 | } |
1288 | ||
dfeec247 | 1289 | fn unreachable_block(&mut self) -> Bx::BasicBlock { |
54a0048b | 1290 | self.unreachable_block.unwrap_or_else(|| { |
a1dfa0c6 XL |
1291 | let mut bx = self.new_block("unreachable"); |
1292 | bx.unreachable(); | |
1293 | self.unreachable_block = Some(bx.llbb()); | |
1294 | bx.llbb() | |
54a0048b SL |
1295 | }) |
1296 | } | |
1297 | ||
17df50a5 XL |
1298 | // FIXME(eddyb) replace with `build_sibling_block`/`append_sibling_block` |
1299 | // (which requires having a `Bx` already, and not all callers do). | |
1300 | fn new_block(&self, name: &str) -> Bx { | |
1301 | let llbb = Bx::append_block(self.cx, self.llfn, name); | |
1302 | Bx::build(self.cx, llbb) | |
1303 | } | |
1304 | ||
1305 | /// Get the backend `BasicBlock` for a MIR `BasicBlock`, either already | |
1306 | /// cached in `self.cached_llbbs`, or created on demand (and cached). | |
1307 | // FIXME(eddyb) rename `llbb` and other `ll`-prefixed things to use a | |
1308 | // more backend-agnostic prefix such as `cg` (i.e. this would be `cgbb`). | |
1309 | pub fn llbb(&mut self, bb: mir::BasicBlock) -> Bx::BasicBlock { | |
1310 | self.cached_llbbs[bb].unwrap_or_else(|| { | |
1311 | // FIXME(eddyb) only name the block if `fewer_names` is `false`. | |
1312 | let llbb = Bx::append_block(self.cx, self.llfn, &format!("{:?}", bb)); | |
1313 | self.cached_llbbs[bb] = Some(llbb); | |
1314 | llbb | |
1315 | }) | |
32a655c1 SL |
1316 | } |
1317 | ||
17df50a5 XL |
1318 | pub fn build_block(&mut self, bb: mir::BasicBlock) -> Bx { |
1319 | let llbb = self.llbb(bb); | |
1320 | Bx::build(self.cx, llbb) | |
54a0048b SL |
1321 | } |
1322 | ||
a1dfa0c6 XL |
1323 | fn make_return_dest( |
1324 | &mut self, | |
1325 | bx: &mut Bx, | |
ba9703b0 | 1326 | dest: mir::Place<'tcx>, |
60c5eb7d | 1327 | fn_ret: &ArgAbi<'tcx, Ty<'tcx>>, |
dfeec247 XL |
1328 | llargs: &mut Vec<Bx::Value>, |
1329 | is_intrinsic: bool, | |
a1dfa0c6 | 1330 | ) -> ReturnDest<'tcx, Bx::Value> { |
dc9dc135 | 1331 | // If the return is ignored, we can just return a do-nothing `ReturnDest`. |
ff7c6d11 | 1332 | if fn_ret.is_ignore() { |
54a0048b SL |
1333 | return ReturnDest::Nothing; |
1334 | } | |
e74abb32 | 1335 | let dest = if let Some(index) = dest.as_local() { |
3157f602 | 1336 | match self.locals[index] { |
ff7c6d11 | 1337 | LocalRef::Place(dest) => dest, |
b7449926 | 1338 | LocalRef::UnsizedPlace(_) => bug!("return type must be sized"), |
3157f602 | 1339 | LocalRef::Operand(None) => { |
dc9dc135 XL |
1340 | // Handle temporary places, specifically `Operand` ones, as |
1341 | // they don't have `alloca`s. | |
ff7c6d11 | 1342 | return if fn_ret.is_indirect() { |
3157f602 XL |
1343 | // Odd, but possible, case, we have an operand temporary, |
1344 | // but the calling convention has an indirect return. | |
e1599b0c | 1345 | let tmp = PlaceRef::alloca(bx, fn_ret.layout); |
2c00a5a8 | 1346 | tmp.storage_live(bx); |
32a655c1 | 1347 | llargs.push(tmp.llval); |
ff7c6d11 | 1348 | ReturnDest::IndirectOperand(tmp, index) |
3157f602 XL |
1349 | } else if is_intrinsic { |
1350 | // Currently, intrinsics always need a location to store | |
dc9dc135 XL |
1351 | // the result, so we create a temporary `alloca` for the |
1352 | // result. | |
e1599b0c | 1353 | let tmp = PlaceRef::alloca(bx, fn_ret.layout); |
2c00a5a8 | 1354 | tmp.storage_live(bx); |
ff7c6d11 | 1355 | ReturnDest::IndirectOperand(tmp, index) |
3157f602 XL |
1356 | } else { |
1357 | ReturnDest::DirectOperand(index) | |
1358 | }; | |
1359 | } | |
1360 | LocalRef::Operand(Some(_)) => { | |
ff7c6d11 | 1361 | bug!("place local already assigned to"); |
54a0048b SL |
1362 | } |
1363 | } | |
3157f602 | 1364 | } else { |
dfeec247 XL |
1365 | self.codegen_place( |
1366 | bx, | |
74b04a01 | 1367 | mir::PlaceRef { local: dest.local, projection: &dest.projection }, |
dfeec247 | 1368 | ) |
54a0048b | 1369 | }; |
ff7c6d11 | 1370 | if fn_ret.is_indirect() { |
a1dfa0c6 | 1371 | if dest.align < dest.layout.align.abi { |
ff7c6d11 XL |
1372 | // Currently, MIR code generation does not create calls |
1373 | // that store directly to fields of packed structs (in | |
dc9dc135 | 1374 | // fact, the calls it creates write only to temps). |
ff7c6d11 XL |
1375 | // |
1376 | // If someone changes that, please update this code path | |
1377 | // to create a temporary. | |
1378 | span_bug!(self.mir.span, "can't directly store to unaligned value"); | |
8bb4bdeb | 1379 | } |
ff7c6d11 XL |
1380 | llargs.push(dest.llval); |
1381 | ReturnDest::Nothing | |
54a0048b | 1382 | } else { |
ff7c6d11 | 1383 | ReturnDest::Store(dest) |
54a0048b SL |
1384 | } |
1385 | } | |
1386 | ||
ba9703b0 | 1387 | fn codegen_transmute(&mut self, bx: &mut Bx, src: &mir::Operand<'tcx>, dst: mir::Place<'tcx>) { |
e74abb32 | 1388 | if let Some(index) = dst.as_local() { |
32a655c1 | 1389 | match self.locals[index] { |
94b46f34 | 1390 | LocalRef::Place(place) => self.codegen_transmute_into(bx, src, place), |
b7449926 | 1391 | LocalRef::UnsizedPlace(_) => bug!("transmute must not involve unsized locals"), |
32a655c1 | 1392 | LocalRef::Operand(None) => { |
74b04a01 | 1393 | let dst_layout = bx.layout_of(self.monomorphized_place_ty(dst.as_ref())); |
ff7c6d11 | 1394 | assert!(!dst_layout.ty.has_erasable_regions()); |
e1599b0c | 1395 | let place = PlaceRef::alloca(bx, dst_layout); |
2c00a5a8 | 1396 | place.storage_live(bx); |
94b46f34 | 1397 | self.codegen_transmute_into(bx, src, place); |
a1dfa0c6 | 1398 | let op = bx.load_operand(place); |
2c00a5a8 | 1399 | place.storage_dead(bx); |
32a655c1 | 1400 | self.locals[index] = LocalRef::Operand(Some(op)); |
74b04a01 | 1401 | self.debug_introduce_local(bx, index); |
32a655c1 | 1402 | } |
ff7c6d11 | 1403 | LocalRef::Operand(Some(op)) => { |
dfeec247 | 1404 | assert!(op.layout.is_zst(), "assigning to initialized SSAtemp"); |
32a655c1 SL |
1405 | } |
1406 | } | |
1407 | } else { | |
74b04a01 | 1408 | let dst = self.codegen_place(bx, dst.as_ref()); |
94b46f34 | 1409 | self.codegen_transmute_into(bx, src, dst); |
32a655c1 SL |
1410 | } |
1411 | } | |
1412 | ||
a1dfa0c6 XL |
1413 | fn codegen_transmute_into( |
1414 | &mut self, | |
1415 | bx: &mut Bx, | |
1416 | src: &mir::Operand<'tcx>, | |
dfeec247 | 1417 | dst: PlaceRef<'tcx, Bx::Value>, |
a1dfa0c6 | 1418 | ) { |
94b46f34 | 1419 | let src = self.codegen_operand(bx, src); |
fc512014 XL |
1420 | |
1421 | // Special-case transmutes between scalars as simple bitcasts. | |
1422 | match (&src.layout.abi, &dst.layout.abi) { | |
1423 | (abi::Abi::Scalar(src_scalar), abi::Abi::Scalar(dst_scalar)) => { | |
1424 | // HACK(eddyb) LLVM doesn't like `bitcast`s between pointers and non-pointers. | |
1425 | if (src_scalar.value == abi::Pointer) == (dst_scalar.value == abi::Pointer) { | |
1426 | assert_eq!(src.layout.size, dst.layout.size); | |
1427 | ||
1428 | // NOTE(eddyb) the `from_immediate` and `to_immediate_scalar` | |
1429 | // conversions allow handling `bool`s the same as `u8`s. | |
1430 | let src = bx.from_immediate(src.immediate()); | |
1431 | let src_as_dst = bx.bitcast(src, bx.backend_type(dst.layout)); | |
1432 | Immediate(bx.to_immediate_scalar(src_as_dst, dst_scalar)).store(bx, dst); | |
1433 | return; | |
1434 | } | |
1435 | } | |
1436 | _ => {} | |
1437 | } | |
1438 | ||
a1dfa0c6 XL |
1439 | let llty = bx.backend_type(src.layout); |
1440 | let cast_ptr = bx.pointercast(dst.llval, bx.type_ptr_to(llty)); | |
1441 | let align = src.layout.align.abi.min(dst.align); | |
e1599b0c | 1442 | src.val.store(bx, PlaceRef::new_sized_aligned(cast_ptr, src.layout, align)); |
54a0048b SL |
1443 | } |
1444 | ||
1445 | // Stores the return value of a function call into it's final location. | |
a1dfa0c6 XL |
1446 | fn store_return( |
1447 | &mut self, | |
1448 | bx: &mut Bx, | |
1449 | dest: ReturnDest<'tcx, Bx::Value>, | |
60c5eb7d | 1450 | ret_abi: &ArgAbi<'tcx, Ty<'tcx>>, |
dfeec247 | 1451 | llval: Bx::Value, |
a1dfa0c6 | 1452 | ) { |
54a0048b SL |
1453 | use self::ReturnDest::*; |
1454 | ||
1455 | match dest { | |
1456 | Nothing => (), | |
60c5eb7d | 1457 | Store(dst) => bx.store_arg(&ret_abi, llval, dst), |
3157f602 | 1458 | IndirectOperand(tmp, index) => { |
a1dfa0c6 | 1459 | let op = bx.load_operand(tmp); |
2c00a5a8 | 1460 | tmp.storage_dead(bx); |
3157f602 | 1461 | self.locals[index] = LocalRef::Operand(Some(op)); |
74b04a01 | 1462 | self.debug_introduce_local(bx, index); |
54a0048b | 1463 | } |
3157f602 XL |
1464 | DirectOperand(index) => { |
1465 | // If there is a cast, we have to store and reload. | |
60c5eb7d XL |
1466 | let op = if let PassMode::Cast(_) = ret_abi.mode { |
1467 | let tmp = PlaceRef::alloca(bx, ret_abi.layout); | |
2c00a5a8 | 1468 | tmp.storage_live(bx); |
60c5eb7d | 1469 | bx.store_arg(&ret_abi, llval, tmp); |
a1dfa0c6 | 1470 | let op = bx.load_operand(tmp); |
2c00a5a8 | 1471 | tmp.storage_dead(bx); |
ff7c6d11 | 1472 | op |
54a0048b | 1473 | } else { |
60c5eb7d | 1474 | OperandRef::from_immediate_or_packed_pair(bx, llval, ret_abi.layout) |
54a0048b | 1475 | }; |
3157f602 | 1476 | self.locals[index] = LocalRef::Operand(Some(op)); |
74b04a01 | 1477 | self.debug_introduce_local(bx, index); |
54a0048b SL |
1478 | } |
1479 | } | |
1480 | } | |
1481 | } | |
1482 | ||
a1dfa0c6 | 1483 | enum ReturnDest<'tcx, V> { |
dc9dc135 | 1484 | // Do nothing; the return value is indirect or ignored. |
54a0048b | 1485 | Nothing, |
dc9dc135 | 1486 | // Store the return value to the pointer. |
a1dfa0c6 | 1487 | Store(PlaceRef<'tcx, V>), |
dc9dc135 | 1488 | // Store an indirect return value to an operand local place. |
a1dfa0c6 | 1489 | IndirectOperand(PlaceRef<'tcx, V>, mir::Local), |
dc9dc135 | 1490 | // Store a direct return value to an operand local place. |
dfeec247 | 1491 | DirectOperand(mir::Local), |
54a0048b | 1492 | } |