]>
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; |
1b1a35ee | 15 | use rustc_middle::mir::interpret::ConstValue; |
ba9703b0 | 16 | use rustc_middle::mir::AssertKind; |
29967ef6 | 17 | use rustc_middle::mir::{self, SwitchTargets}; |
ba9703b0 | 18 | use rustc_middle::ty::layout::{FnAbiExt, HasTyCtxt}; |
1b1a35ee | 19 | use rustc_middle::ty::print::with_no_trimmed_paths; |
ba9703b0 | 20 | use rustc_middle::ty::{self, Instance, Ty, TypeFoldable}; |
3dfed10e XL |
21 | use rustc_span::source_map::Span; |
22 | use rustc_span::{sym, Symbol}; | |
dfeec247 | 23 | use rustc_target::abi::call::{ArgAbi, FnAbi, PassMode}; |
ba9703b0 | 24 | use rustc_target::abi::{self, LayoutOf}; |
dfeec247 | 25 | use rustc_target::spec::abi::Abi; |
54a0048b | 26 | |
532ac7d7 XL |
27 | /// Used by `FunctionCx::codegen_terminator` for emitting common patterns |
28 | /// e.g., creating a basic block, calling a function, etc. | |
60c5eb7d XL |
29 | struct TerminatorCodegenHelper<'tcx> { |
30 | bb: mir::BasicBlock, | |
31 | terminator: &'tcx mir::Terminator<'tcx>, | |
532ac7d7 XL |
32 | funclet_bb: Option<mir::BasicBlock>, |
33 | } | |
34 | ||
60c5eb7d | 35 | impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> { |
532ac7d7 XL |
36 | /// Returns the associated funclet from `FunctionCx::funclets` for the |
37 | /// `funclet_bb` member if it is not `None`. | |
60c5eb7d | 38 | fn funclet<'b, Bx: BuilderMethods<'a, 'tcx>>( |
532ac7d7 | 39 | &self, |
fc512014 | 40 | fx: &'b FunctionCx<'a, 'tcx, Bx>, |
60c5eb7d | 41 | ) -> Option<&'b Bx::Funclet> { |
fc512014 | 42 | self.funclet_bb.and_then(|funcl| fx.funclets[funcl].as_ref()) |
532ac7d7 | 43 | } |
3157f602 | 44 | |
60c5eb7d | 45 | fn lltarget<Bx: BuilderMethods<'a, 'tcx>>( |
532ac7d7 | 46 | &self, |
60c5eb7d | 47 | fx: &mut FunctionCx<'a, 'tcx, Bx>, |
532ac7d7 XL |
48 | target: mir::BasicBlock, |
49 | ) -> (Bx::BasicBlock, bool) { | |
50 | let span = self.terminator.source_info.span; | |
51 | let lltarget = fx.blocks[target]; | |
52 | let target_funclet = fx.cleanup_kinds[target].funclet_bb(target); | |
53 | match (self.funclet_bb, target_funclet) { | |
54 | (None, None) => (lltarget, false), | |
dfeec247 XL |
55 | (Some(f), Some(t_f)) if f == t_f || !base::wants_msvc_seh(fx.cx.tcx().sess) => { |
56 | (lltarget, false) | |
57 | } | |
532ac7d7 XL |
58 | // jump *into* cleanup - need a landing pad if GNU |
59 | (None, Some(_)) => (fx.landing_pad_to(target), false), | |
60 | (Some(_), None) => span_bug!(span, "{:?} - jump out of cleanup?", self.terminator), | |
61 | (Some(_), Some(_)) => (fx.landing_pad_to(target), true), | |
7cac9316 | 62 | } |
532ac7d7 | 63 | } |
7cac9316 | 64 | |
532ac7d7 | 65 | /// Create a basic block. |
60c5eb7d | 66 | fn llblock<Bx: BuilderMethods<'a, 'tcx>>( |
532ac7d7 | 67 | &self, |
60c5eb7d | 68 | fx: &mut FunctionCx<'a, 'tcx, Bx>, |
532ac7d7 XL |
69 | target: mir::BasicBlock, |
70 | ) -> Bx::BasicBlock { | |
71 | let (lltarget, is_cleanupret) = self.lltarget(fx, target); | |
72 | if is_cleanupret { | |
73 | // MSVC cross-funclet jump - need a trampoline | |
74 | ||
75 | debug!("llblock: creating cleanup trampoline for {:?}", target); | |
76 | let name = &format!("{:?}_cleanup_trampoline_{:?}", self.bb, target); | |
77 | let mut trampoline = fx.new_block(name); | |
dfeec247 | 78 | trampoline.cleanup_ret(self.funclet(fx).unwrap(), Some(lltarget)); |
532ac7d7 XL |
79 | trampoline.llbb() |
80 | } else { | |
81 | lltarget | |
82 | } | |
7cac9316 XL |
83 | } |
84 | ||
60c5eb7d | 85 | fn funclet_br<Bx: BuilderMethods<'a, 'tcx>>( |
532ac7d7 | 86 | &self, |
60c5eb7d | 87 | fx: &mut FunctionCx<'a, 'tcx, Bx>, |
532ac7d7 XL |
88 | bx: &mut Bx, |
89 | target: mir::BasicBlock, | |
a1dfa0c6 | 90 | ) { |
532ac7d7 XL |
91 | let (lltarget, is_cleanupret) = self.lltarget(fx, target); |
92 | if is_cleanupret { | |
93 | // micro-optimization: generate a `ret` rather than a jump | |
94 | // to a trampoline. | |
95 | bx.cleanup_ret(self.funclet(fx).unwrap(), Some(lltarget)); | |
96 | } else { | |
97 | bx.br(lltarget); | |
98 | } | |
99 | } | |
7cac9316 | 100 | |
60c5eb7d | 101 | /// Call `fn_ptr` of `fn_abi` with the arguments `llargs`, the optional |
532ac7d7 | 102 | /// return destination `destination` and the cleanup function `cleanup`. |
60c5eb7d | 103 | fn do_call<Bx: BuilderMethods<'a, 'tcx>>( |
532ac7d7 | 104 | &self, |
60c5eb7d | 105 | fx: &mut FunctionCx<'a, 'tcx, Bx>, |
532ac7d7 | 106 | bx: &mut Bx, |
60c5eb7d | 107 | fn_abi: FnAbi<'tcx, Ty<'tcx>>, |
532ac7d7 XL |
108 | fn_ptr: Bx::Value, |
109 | llargs: &[Bx::Value], | |
110 | destination: Option<(ReturnDest<'tcx, Bx::Value>, mir::BasicBlock)>, | |
111 | cleanup: Option<mir::BasicBlock>, | |
112 | ) { | |
ba9703b0 XL |
113 | // If there is a cleanup block and the function we're calling can unwind, then |
114 | // do an invoke, otherwise do a call. | |
115 | if let Some(cleanup) = cleanup.filter(|_| fn_abi.can_unwind) { | |
532ac7d7 XL |
116 | let ret_bx = if let Some((_, target)) = destination { |
117 | fx.blocks[target] | |
118 | } else { | |
119 | fx.unreachable_block() | |
120 | }; | |
dfeec247 XL |
121 | let invokeret = |
122 | bx.invoke(fn_ptr, &llargs, ret_bx, self.llblock(fx, cleanup), self.funclet(fx)); | |
60c5eb7d | 123 | bx.apply_attrs_callsite(&fn_abi, invokeret); |
532ac7d7 XL |
124 | |
125 | if let Some((ret_dest, target)) = destination { | |
126 | let mut ret_bx = fx.build_block(target); | |
127 | fx.set_debug_loc(&mut ret_bx, self.terminator.source_info); | |
60c5eb7d | 128 | fx.store_return(&mut ret_bx, ret_dest, &fn_abi.ret, invokeret); |
a1dfa0c6 | 129 | } |
532ac7d7 XL |
130 | } else { |
131 | let llret = bx.call(fn_ptr, &llargs, self.funclet(fx)); | |
60c5eb7d XL |
132 | bx.apply_attrs_callsite(&fn_abi, llret); |
133 | if fx.mir[self.bb].is_cleanup { | |
532ac7d7 XL |
134 | // Cleanup is always the cold path. Don't inline |
135 | // drop glue. Also, when there is a deeply-nested | |
136 | // struct, there are "symmetry" issues that cause | |
137 | // exponential inlining - see issue #41696. | |
138 | bx.do_not_inline(llret); | |
3157f602 | 139 | } |
7cac9316 | 140 | |
532ac7d7 | 141 | if let Some((ret_dest, target)) = destination { |
60c5eb7d | 142 | fx.store_return(bx, ret_dest, &fn_abi.ret, llret); |
532ac7d7 | 143 | self.funclet_br(fx, bx, target); |
7cac9316 | 144 | } else { |
532ac7d7 | 145 | bx.unreachable(); |
7cac9316 | 146 | } |
532ac7d7 XL |
147 | } |
148 | } | |
149 | } | |
3157f602 | 150 | |
532ac7d7 | 151 | /// Codegen implementations for some terminator variants. |
dc9dc135 | 152 | impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { |
532ac7d7 | 153 | /// Generates code for a `Resume` terminator. |
dfeec247 | 154 | fn codegen_resume_terminator(&mut self, helper: TerminatorCodegenHelper<'tcx>, mut bx: Bx) { |
532ac7d7 XL |
155 | if let Some(funclet) = helper.funclet(self) { |
156 | bx.cleanup_ret(funclet, None); | |
157 | } else { | |
158 | let slot = self.get_personality_slot(&mut bx); | |
159 | let lp0 = slot.project_field(&mut bx, 0); | |
160 | let lp0 = bx.load_operand(lp0).immediate(); | |
161 | let lp1 = slot.project_field(&mut bx, 1); | |
162 | let lp1 = bx.load_operand(lp1).immediate(); | |
163 | slot.storage_dead(&mut bx); | |
164 | ||
ba9703b0 XL |
165 | let mut lp = bx.const_undef(self.landing_pad_type()); |
166 | lp = bx.insert_value(lp, lp0, 0); | |
167 | lp = bx.insert_value(lp, lp1, 1); | |
168 | bx.resume(lp); | |
532ac7d7 XL |
169 | } |
170 | } | |
7cac9316 | 171 | |
60c5eb7d | 172 | fn codegen_switchint_terminator( |
532ac7d7 | 173 | &mut self, |
60c5eb7d | 174 | helper: TerminatorCodegenHelper<'tcx>, |
532ac7d7 XL |
175 | mut bx: Bx, |
176 | discr: &mir::Operand<'tcx>, | |
177 | switch_ty: Ty<'tcx>, | |
29967ef6 | 178 | targets: &SwitchTargets, |
532ac7d7 XL |
179 | ) { |
180 | let discr = self.codegen_operand(&mut bx, &discr); | |
f035d41b XL |
181 | // `switch_ty` is redundant, sanity-check that. |
182 | assert_eq!(discr.layout.ty, switch_ty); | |
29967ef6 XL |
183 | let mut target_iter = targets.iter(); |
184 | if target_iter.len() == 1 { | |
185 | // If there are two targets (one conditional, one fallback), emit br instead of switch | |
186 | let (test_value, target) = target_iter.next().unwrap(); | |
187 | let lltrue = helper.llblock(self, target); | |
188 | let llfalse = helper.llblock(self, targets.otherwise()); | |
532ac7d7 XL |
189 | if switch_ty == bx.tcx().types.bool { |
190 | // Don't generate trivial icmps when switching on bool | |
29967ef6 XL |
191 | match test_value { |
192 | 0 => bx.cond_br(discr.immediate(), llfalse, lltrue), | |
193 | 1 => bx.cond_br(discr.immediate(), lltrue, llfalse), | |
194 | _ => bug!(), | |
3157f602 | 195 | } |
532ac7d7 | 196 | } else { |
dfeec247 | 197 | let switch_llty = bx.immediate_backend_type(bx.layout_of(switch_ty)); |
29967ef6 | 198 | let llval = bx.const_uint_big(switch_llty, test_value); |
532ac7d7 XL |
199 | let cmp = bx.icmp(IntPredicate::IntEQ, discr.immediate(), llval); |
200 | bx.cond_br(cmp, lltrue, llfalse); | |
3157f602 | 201 | } |
532ac7d7 | 202 | } else { |
532ac7d7 XL |
203 | bx.switch( |
204 | discr.immediate(), | |
29967ef6 XL |
205 | helper.llblock(self, targets.otherwise()), |
206 | target_iter.map(|(value, target)| (value, helper.llblock(self, target))), | |
532ac7d7 XL |
207 | ); |
208 | } | |
209 | } | |
54a0048b | 210 | |
dc9dc135 | 211 | fn codegen_return_terminator(&mut self, mut bx: Bx) { |
e74abb32 | 212 | // Call `va_end` if this is the definition of a C-variadic function. |
60c5eb7d | 213 | if self.fn_abi.c_variadic { |
e74abb32 | 214 | // The `VaList` "spoofed" argument is just after all the real arguments. |
60c5eb7d | 215 | let va_list_arg_idx = self.fn_abi.args.len(); |
e74abb32 XL |
216 | match self.locals[mir::Local::new(1 + va_list_arg_idx)] { |
217 | LocalRef::Place(va_list) => { | |
532ac7d7 XL |
218 | bx.va_end(va_list.llval); |
219 | } | |
e74abb32 | 220 | _ => bug!("C-variadic function must have a `VaList` place"), |
54a0048b | 221 | } |
532ac7d7 | 222 | } |
60c5eb7d | 223 | if self.fn_abi.ret.layout.abi.is_uninhabited() { |
532ac7d7 XL |
224 | // Functions with uninhabited return values are marked `noreturn`, |
225 | // so we should make sure that we never actually do. | |
60c5eb7d XL |
226 | // We play it safe by using a well-defined `abort`, but we could go for immediate UB |
227 | // if that turns out to be helpful. | |
532ac7d7 | 228 | bx.abort(); |
60c5eb7d XL |
229 | // `abort` does not terminate the block, so we still need to generate |
230 | // an `unreachable` terminator after it. | |
532ac7d7 XL |
231 | bx.unreachable(); |
232 | return; | |
233 | } | |
60c5eb7d | 234 | let llval = match self.fn_abi.ret.mode { |
fc512014 | 235 | PassMode::Ignore | PassMode::Indirect { .. } => { |
532ac7d7 XL |
236 | bx.ret_void(); |
237 | return; | |
ff7c6d11 XL |
238 | } |
239 | ||
532ac7d7 | 240 | PassMode::Direct(_) | PassMode::Pair(..) => { |
74b04a01 | 241 | let op = self.codegen_consume(&mut bx, mir::Place::return_place().as_ref()); |
532ac7d7 XL |
242 | if let Ref(llval, _, align) = op.val { |
243 | bx.load(llval, align) | |
8bb4bdeb | 244 | } else { |
532ac7d7 | 245 | op.immediate_or_packed_pair(&mut bx) |
54a0048b SL |
246 | } |
247 | } | |
248 | ||
532ac7d7 XL |
249 | PassMode::Cast(cast_ty) => { |
250 | let op = match self.locals[mir::RETURN_PLACE] { | |
251 | LocalRef::Operand(Some(op)) => op, | |
252 | LocalRef::Operand(None) => bug!("use of return before def"), | |
dfeec247 XL |
253 | LocalRef::Place(cg_place) => OperandRef { |
254 | val: Ref(cg_place.llval, None, cg_place.align), | |
255 | layout: cg_place.layout, | |
256 | }, | |
532ac7d7 XL |
257 | LocalRef::UnsizedPlace(_) => bug!("return type must be sized"), |
258 | }; | |
259 | let llslot = match op.val { | |
260 | Immediate(_) | Pair(..) => { | |
dfeec247 | 261 | let scratch = PlaceRef::alloca(&mut bx, self.fn_abi.ret.layout); |
532ac7d7 XL |
262 | op.val.store(&mut bx, scratch); |
263 | scratch.llval | |
264 | } | |
265 | Ref(llval, _, align) => { | |
dfeec247 | 266 | assert_eq!(align, op.layout.align.abi, "return place is unaligned!"); |
532ac7d7 | 267 | llval |
476ff2be | 268 | } |
3157f602 | 269 | }; |
dfeec247 | 270 | let addr = bx.pointercast(llslot, bx.type_ptr_to(bx.cast_backend_type(&cast_ty))); |
60c5eb7d | 271 | bx.load(addr, self.fn_abi.ret.layout.align.abi) |
3157f602 | 272 | } |
532ac7d7 XL |
273 | }; |
274 | bx.ret(llval); | |
275 | } | |
3157f602 | 276 | |
60c5eb7d | 277 | fn codegen_drop_terminator( |
532ac7d7 | 278 | &mut self, |
60c5eb7d | 279 | helper: TerminatorCodegenHelper<'tcx>, |
532ac7d7 | 280 | mut bx: Bx, |
ba9703b0 | 281 | location: mir::Place<'tcx>, |
532ac7d7 XL |
282 | target: mir::BasicBlock, |
283 | unwind: Option<mir::BasicBlock>, | |
284 | ) { | |
f9f354fc | 285 | let ty = location.ty(self.mir, bx.tcx()).ty; |
fc512014 | 286 | let ty = self.monomorphize(ty); |
dc9dc135 | 287 | let drop_fn = Instance::resolve_drop_in_place(bx.tcx(), ty); |
532ac7d7 XL |
288 | |
289 | if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def { | |
290 | // we don't actually need to drop anything. | |
291 | helper.funclet_br(self, &mut bx, target); | |
dfeec247 | 292 | return; |
532ac7d7 XL |
293 | } |
294 | ||
74b04a01 | 295 | let place = self.codegen_place(&mut bx, location.as_ref()); |
532ac7d7 XL |
296 | let (args1, args2); |
297 | let mut args = if let Some(llextra) = place.llextra { | |
298 | args2 = [place.llval, llextra]; | |
299 | &args2[..] | |
300 | } else { | |
301 | args1 = [place.llval]; | |
302 | &args1[..] | |
303 | }; | |
1b1a35ee | 304 | let (drop_fn, fn_abi) = match ty.kind() { |
60c5eb7d XL |
305 | // FIXME(eddyb) perhaps move some of this logic into |
306 | // `Instance::resolve_drop_in_place`? | |
532ac7d7 | 307 | ty::Dynamic(..) => { |
60c5eb7d XL |
308 | let virtual_drop = Instance { |
309 | def: ty::InstanceDef::Virtual(drop_fn.def_id(), 0), | |
310 | substs: drop_fn.substs, | |
311 | }; | |
312 | let fn_abi = FnAbi::of_instance(&bx, virtual_drop, &[]); | |
532ac7d7 XL |
313 | let vtable = args[1]; |
314 | args = &args[..1]; | |
60c5eb7d | 315 | (meth::DESTRUCTOR.get_fn(&mut bx, vtable, &fn_abi), fn_abi) |
54a0048b | 316 | } |
dfeec247 | 317 | _ => (bx.get_fn_addr(drop_fn), FnAbi::of_instance(&bx, drop_fn, &[])), |
532ac7d7 | 318 | }; |
dfeec247 XL |
319 | helper.do_call( |
320 | self, | |
321 | &mut bx, | |
322 | fn_abi, | |
323 | drop_fn, | |
324 | args, | |
325 | Some((ReturnDest::Nothing, target)), | |
326 | unwind, | |
327 | ); | |
532ac7d7 | 328 | } |
54a0048b | 329 | |
60c5eb7d | 330 | fn codegen_assert_terminator( |
532ac7d7 | 331 | &mut self, |
60c5eb7d | 332 | helper: TerminatorCodegenHelper<'tcx>, |
532ac7d7 XL |
333 | mut bx: Bx, |
334 | terminator: &mir::Terminator<'tcx>, | |
335 | cond: &mir::Operand<'tcx>, | |
336 | expected: bool, | |
337 | msg: &mir::AssertMessage<'tcx>, | |
338 | target: mir::BasicBlock, | |
339 | cleanup: Option<mir::BasicBlock>, | |
340 | ) { | |
341 | let span = terminator.source_info.span; | |
342 | let cond = self.codegen_operand(&mut bx, cond).immediate(); | |
343 | let mut const_cond = bx.const_to_opt_u128(cond, false).map(|c| c == 1); | |
344 | ||
345 | // This case can currently arise only from functions marked | |
346 | // with #[rustc_inherit_overflow_checks] and inlined from | |
347 | // another crate (mostly core::num generic/#[inline] fns), | |
348 | // while the current crate doesn't use overflow checks. | |
349 | // NOTE: Unlike binops, negation doesn't have its own | |
350 | // checked operation, just a comparison with the minimum | |
351 | // value, so we have to check for the assert message. | |
352 | if !bx.check_overflow() { | |
f035d41b | 353 | if let AssertKind::OverflowNeg(_) = *msg { |
532ac7d7 XL |
354 | const_cond = Some(expected); |
355 | } | |
356 | } | |
3157f602 | 357 | |
532ac7d7 XL |
358 | // Don't codegen the panic block if success if known. |
359 | if const_cond == Some(expected) { | |
360 | helper.funclet_br(self, &mut bx, target); | |
361 | return; | |
362 | } | |
363 | ||
364 | // Pass the condition through llvm.expect for branch hinting. | |
365 | let cond = bx.expect(cond, expected); | |
366 | ||
367 | // Create the failure block and the conditional branch to it. | |
368 | let lltarget = helper.llblock(self, target); | |
369 | let panic_block = self.new_block("panic"); | |
370 | if expected { | |
371 | bx.cond_br(cond, lltarget, panic_block.llbb()); | |
372 | } else { | |
373 | bx.cond_br(cond, panic_block.llbb(), lltarget); | |
374 | } | |
375 | ||
376 | // After this point, bx is the block for the call to panic. | |
377 | bx = panic_block; | |
378 | self.set_debug_loc(&mut bx, terminator.source_info); | |
379 | ||
380 | // Get the location information. | |
29967ef6 | 381 | let location = self.get_caller_location(&mut bx, terminator.source_info).immediate(); |
532ac7d7 XL |
382 | |
383 | // Put together the arguments to the panic entry point. | |
416331ca | 384 | let (lang_item, args) = match msg { |
74b04a01 | 385 | AssertKind::BoundsCheck { ref len, ref index } => { |
532ac7d7 XL |
386 | let len = self.codegen_operand(&mut bx, len).immediate(); |
387 | let index = self.codegen_operand(&mut bx, index).immediate(); | |
ba9703b0 XL |
388 | // It's `fn panic_bounds_check(index: usize, len: usize)`, |
389 | // and `#[track_caller]` adds an implicit third argument. | |
3dfed10e | 390 | (LangItem::PanicBoundsCheck, vec![index, len, location]) |
532ac7d7 XL |
391 | } |
392 | _ => { | |
e1599b0c | 393 | let msg_str = Symbol::intern(msg.description()); |
e74abb32 | 394 | let msg = bx.const_str(msg_str); |
ba9703b0 XL |
395 | // It's `pub fn panic(expr: &str)`, with the wide reference being passed |
396 | // as two arguments, and `#[track_caller]` adds an implicit third argument. | |
3dfed10e | 397 | (LangItem::Panic, vec![msg.0, msg.1, location]) |
3157f602 | 398 | } |
532ac7d7 | 399 | }; |
3157f602 | 400 | |
532ac7d7 XL |
401 | // Obtain the panic entry point. |
402 | let def_id = common::langcall(bx.tcx(), Some(span), "", lang_item); | |
403 | let instance = ty::Instance::mono(bx.tcx(), def_id); | |
60c5eb7d | 404 | let fn_abi = FnAbi::of_instance(&bx, instance, &[]); |
e74abb32 | 405 | let llfn = bx.get_fn_addr(instance); |
3157f602 | 406 | |
532ac7d7 | 407 | // Codegen the actual panic invoke/call. |
60c5eb7d | 408 | helper.do_call(self, &mut bx, fn_abi, llfn, &args, None, cleanup); |
532ac7d7 | 409 | } |
3157f602 | 410 | |
ba9703b0 XL |
411 | /// Returns `true` if this is indeed a panic intrinsic and codegen is done. |
412 | fn codegen_panic_intrinsic( | |
413 | &mut self, | |
414 | helper: &TerminatorCodegenHelper<'tcx>, | |
415 | bx: &mut Bx, | |
3dfed10e | 416 | intrinsic: Option<Symbol>, |
ba9703b0 | 417 | instance: Option<Instance<'tcx>>, |
29967ef6 | 418 | source_info: mir::SourceInfo, |
ba9703b0 XL |
419 | destination: &Option<(mir::Place<'tcx>, mir::BasicBlock)>, |
420 | cleanup: Option<mir::BasicBlock>, | |
421 | ) -> bool { | |
422 | // Emit a panic or a no-op for `assert_*` intrinsics. | |
423 | // These are intrinsics that compile to panics so that we can get a message | |
424 | // which mentions the offending type, even from a const context. | |
425 | #[derive(Debug, PartialEq)] | |
426 | enum AssertIntrinsic { | |
427 | Inhabited, | |
428 | ZeroValid, | |
429 | UninitValid, | |
fc512014 | 430 | } |
ba9703b0 | 431 | let panic_intrinsic = intrinsic.and_then(|i| match i { |
3dfed10e XL |
432 | sym::assert_inhabited => Some(AssertIntrinsic::Inhabited), |
433 | sym::assert_zero_valid => Some(AssertIntrinsic::ZeroValid), | |
434 | sym::assert_uninit_valid => Some(AssertIntrinsic::UninitValid), | |
ba9703b0 XL |
435 | _ => None, |
436 | }); | |
437 | if let Some(intrinsic) = panic_intrinsic { | |
438 | use AssertIntrinsic::*; | |
439 | let ty = instance.unwrap().substs.type_at(0); | |
440 | let layout = bx.layout_of(ty); | |
441 | let do_panic = match intrinsic { | |
442 | Inhabited => layout.abi.is_uninhabited(), | |
443 | // We unwrap as the error type is `!`. | |
444 | ZeroValid => !layout.might_permit_raw_init(bx, /*zero:*/ true).unwrap(), | |
445 | // We unwrap as the error type is `!`. | |
446 | UninitValid => !layout.might_permit_raw_init(bx, /*zero:*/ false).unwrap(), | |
447 | }; | |
448 | if do_panic { | |
1b1a35ee XL |
449 | let msg_str = with_no_trimmed_paths(|| { |
450 | if layout.abi.is_uninhabited() { | |
451 | // Use this error even for the other intrinsics as it is more precise. | |
452 | format!("attempted to instantiate uninhabited type `{}`", ty) | |
453 | } else if intrinsic == ZeroValid { | |
454 | format!("attempted to zero-initialize type `{}`, which is invalid", ty) | |
455 | } else { | |
456 | format!("attempted to leave type `{}` uninitialized, which is invalid", ty) | |
457 | } | |
458 | }); | |
ba9703b0 | 459 | let msg = bx.const_str(Symbol::intern(&msg_str)); |
29967ef6 | 460 | let location = self.get_caller_location(bx, source_info).immediate(); |
ba9703b0 XL |
461 | |
462 | // Obtain the panic entry point. | |
463 | // FIXME: dedup this with `codegen_assert_terminator` above. | |
29967ef6 XL |
464 | let def_id = |
465 | common::langcall(bx.tcx(), Some(source_info.span), "", LangItem::Panic); | |
ba9703b0 XL |
466 | let instance = ty::Instance::mono(bx.tcx(), def_id); |
467 | let fn_abi = FnAbi::of_instance(bx, instance, &[]); | |
468 | let llfn = bx.get_fn_addr(instance); | |
469 | ||
ba9703b0 XL |
470 | // Codegen the actual panic invoke/call. |
471 | helper.do_call( | |
472 | self, | |
473 | bx, | |
474 | fn_abi, | |
475 | llfn, | |
476 | &[msg.0, msg.1, location], | |
477 | destination.as_ref().map(|(_, bb)| (ReturnDest::Nothing, *bb)), | |
478 | cleanup, | |
479 | ); | |
480 | } else { | |
481 | // a NOP | |
482 | let target = destination.as_ref().unwrap().1; | |
ba9703b0 XL |
483 | helper.funclet_br(self, bx, target) |
484 | } | |
485 | true | |
486 | } else { | |
487 | false | |
488 | } | |
489 | } | |
490 | ||
60c5eb7d | 491 | fn codegen_call_terminator( |
532ac7d7 | 492 | &mut self, |
60c5eb7d | 493 | helper: TerminatorCodegenHelper<'tcx>, |
532ac7d7 XL |
494 | mut bx: Bx, |
495 | terminator: &mir::Terminator<'tcx>, | |
496 | func: &mir::Operand<'tcx>, | |
5869c6ff | 497 | args: &[mir::Operand<'tcx>], |
532ac7d7 XL |
498 | destination: &Option<(mir::Place<'tcx>, mir::BasicBlock)>, |
499 | cleanup: Option<mir::BasicBlock>, | |
f035d41b | 500 | fn_span: Span, |
532ac7d7 | 501 | ) { |
29967ef6 XL |
502 | let source_info = terminator.source_info; |
503 | let span = source_info.span; | |
504 | ||
532ac7d7 XL |
505 | // Create the callee. This is a fn ptr or zero-sized and hence a kind of scalar. |
506 | let callee = self.codegen_operand(&mut bx, func); | |
507 | ||
1b1a35ee | 508 | let (instance, mut llfn) = match *callee.layout.ty.kind() { |
dfeec247 XL |
509 | ty::FnDef(def_id, substs) => ( |
510 | Some( | |
511 | ty::Instance::resolve(bx.tcx(), ty::ParamEnv::reveal_all(), def_id, substs) | |
f9f354fc | 512 | .unwrap() |
3dfed10e XL |
513 | .unwrap() |
514 | .polymorphize(bx.tcx()), | |
dfeec247 XL |
515 | ), |
516 | None, | |
517 | ), | |
518 | ty::FnPtr(_) => (None, Some(callee.immediate())), | |
532ac7d7 XL |
519 | _ => bug!("{} is not callable", callee.layout.ty), |
520 | }; | |
521 | let def = instance.map(|i| i.def); | |
60c5eb7d XL |
522 | |
523 | if let Some(ty::InstanceDef::DropGlue(_, None)) = def { | |
524 | // Empty drop glue; a no-op. | |
525 | let &(_, target) = destination.as_ref().unwrap(); | |
60c5eb7d XL |
526 | helper.funclet_br(self, &mut bx, target); |
527 | return; | |
528 | } | |
529 | ||
530 | // FIXME(eddyb) avoid computing this if possible, when `instance` is | |
531 | // available - right now `sig` is only needed for getting the `abi` | |
532 | // and figuring out how many extra args were passed to a C-variadic `fn`. | |
532ac7d7 | 533 | let sig = callee.layout.ty.fn_sig(bx.tcx()); |
60c5eb7d | 534 | let abi = sig.abi(); |
532ac7d7 XL |
535 | |
536 | // Handle intrinsics old codegen wants Expr's for, ourselves. | |
537 | let intrinsic = match def { | |
3dfed10e | 538 | Some(ty::InstanceDef::Intrinsic(def_id)) => Some(bx.tcx().item_name(def_id)), |
dfeec247 | 539 | _ => None, |
532ac7d7 | 540 | }; |
3157f602 | 541 | |
60c5eb7d | 542 | let extra_args = &args[sig.inputs().skip_binder().len()..]; |
dfeec247 XL |
543 | let extra_args = extra_args |
544 | .iter() | |
545 | .map(|op_arg| { | |
f9f354fc | 546 | let op_ty = op_arg.ty(self.mir, bx.tcx()); |
fc512014 | 547 | self.monomorphize(op_ty) |
dfeec247 XL |
548 | }) |
549 | .collect::<Vec<_>>(); | |
60c5eb7d XL |
550 | |
551 | let fn_abi = match instance { | |
552 | Some(instance) => FnAbi::of_instance(&bx, instance, &extra_args), | |
dfeec247 | 553 | None => FnAbi::of_fn_ptr(&bx, sig, &extra_args), |
60c5eb7d XL |
554 | }; |
555 | ||
3dfed10e | 556 | if intrinsic == Some(sym::transmute) { |
532ac7d7 | 557 | if let Some(destination_ref) = destination.as_ref() { |
ba9703b0 | 558 | let &(dest, target) = destination_ref; |
532ac7d7 XL |
559 | self.codegen_transmute(&mut bx, &args[0], dest); |
560 | helper.funclet_br(self, &mut bx, target); | |
561 | } else { | |
562 | // If we are trying to transmute to an uninhabited type, | |
563 | // it is likely there is no allotted destination. In fact, | |
564 | // transmuting to an uninhabited type is UB, which means | |
565 | // we can do what we like. Here, we declare that transmuting | |
566 | // into an uninhabited type is impossible, so anything following | |
567 | // it must be unreachable. | |
ba9703b0 | 568 | assert_eq!(fn_abi.ret.layout.abi, abi::Abi::Uninhabited); |
532ac7d7 XL |
569 | bx.unreachable(); |
570 | } | |
571 | return; | |
572 | } | |
3157f602 | 573 | |
ba9703b0 XL |
574 | if self.codegen_panic_intrinsic( |
575 | &helper, | |
576 | &mut bx, | |
577 | intrinsic, | |
578 | instance, | |
29967ef6 | 579 | source_info, |
ba9703b0 XL |
580 | destination, |
581 | cleanup, | |
582 | ) { | |
532ac7d7 XL |
583 | return; |
584 | } | |
54a0048b | 585 | |
532ac7d7 | 586 | // The arguments we'll be passing. Plus one to account for outptr, if used. |
60c5eb7d | 587 | let arg_count = fn_abi.args.len() + fn_abi.ret.is_indirect() as usize; |
532ac7d7 | 588 | let mut llargs = Vec::with_capacity(arg_count); |
3157f602 | 589 | |
532ac7d7 | 590 | // Prepare the return value destination |
ba9703b0 | 591 | let ret_dest = if let Some((dest, _)) = *destination { |
532ac7d7 | 592 | let is_intrinsic = intrinsic.is_some(); |
dfeec247 | 593 | self.make_return_dest(&mut bx, dest, &fn_abi.ret, &mut llargs, is_intrinsic) |
532ac7d7 XL |
594 | } else { |
595 | ReturnDest::Nothing | |
596 | }; | |
54a0048b | 597 | |
3dfed10e | 598 | if intrinsic == Some(sym::caller_location) { |
e74abb32 | 599 | if let Some((_, target)) = destination.as_ref() { |
29967ef6 XL |
600 | let location = self |
601 | .get_caller_location(&mut bx, mir::SourceInfo { span: fn_span, ..source_info }); | |
e74abb32 XL |
602 | |
603 | if let ReturnDest::IndirectOperand(tmp, _) = ret_dest { | |
604 | location.val.store(&mut bx, tmp); | |
605 | } | |
60c5eb7d | 606 | self.store_return(&mut bx, ret_dest, &fn_abi.ret, location.immediate()); |
e74abb32 XL |
607 | helper.funclet_br(self, &mut bx, *target); |
608 | } | |
609 | return; | |
610 | } | |
611 | ||
6a06907d XL |
612 | match intrinsic { |
613 | None | Some(sym::drop_in_place) => {} | |
614 | Some(sym::copy_nonoverlapping) => unreachable!(), | |
615 | Some(intrinsic) => { | |
616 | let dest = match ret_dest { | |
617 | _ if fn_abi.ret.is_indirect() => llargs[0], | |
618 | ReturnDest::Nothing => { | |
619 | bx.const_undef(bx.type_ptr_to(bx.arg_memory_ty(&fn_abi.ret))) | |
620 | } | |
621 | ReturnDest::IndirectOperand(dst, _) | ReturnDest::Store(dst) => dst.llval, | |
622 | ReturnDest::DirectOperand(_) => { | |
623 | bug!("Cannot use direct operand with an intrinsic call") | |
624 | } | |
625 | }; | |
54a0048b | 626 | |
6a06907d XL |
627 | let args: Vec<_> = args |
628 | .iter() | |
629 | .enumerate() | |
630 | .map(|(i, arg)| { | |
631 | // The indices passed to simd_shuffle* in the | |
632 | // third argument must be constant. This is | |
633 | // checked by const-qualification, which also | |
634 | // promotes any complex rvalues to constants. | |
635 | if i == 2 && intrinsic.as_str().starts_with("simd_shuffle") { | |
636 | if let mir::Operand::Constant(constant) = arg { | |
637 | let c = self.eval_mir_constant(constant); | |
638 | let (llval, ty) = | |
639 | self.simd_shuffle_indices(&bx, constant.span, constant.ty(), c); | |
640 | return OperandRef { | |
641 | val: Immediate(llval), | |
642 | layout: bx.layout_of(ty), | |
643 | }; | |
644 | } else { | |
645 | span_bug!(span, "shuffle indices must be constant"); | |
646 | } | |
532ac7d7 | 647 | } |
54a0048b | 648 | |
6a06907d XL |
649 | self.codegen_operand(&mut bx, arg) |
650 | }) | |
651 | .collect(); | |
652 | ||
653 | Self::codegen_intrinsic_call( | |
654 | &mut bx, | |
655 | *instance.as_ref().unwrap(), | |
656 | &fn_abi, | |
657 | &args, | |
658 | dest, | |
659 | span, | |
660 | ); | |
dfeec247 | 661 | |
6a06907d XL |
662 | if let ReturnDest::IndirectOperand(dst, _) = ret_dest { |
663 | self.store_return(&mut bx, ret_dest, &fn_abi.ret, dst.llval); | |
664 | } | |
532ac7d7 | 665 | |
6a06907d XL |
666 | if let Some((_, target)) = *destination { |
667 | helper.funclet_br(self, &mut bx, target); | |
668 | } else { | |
669 | bx.unreachable(); | |
670 | } | |
532ac7d7 | 671 | |
6a06907d | 672 | return; |
532ac7d7 | 673 | } |
532ac7d7 XL |
674 | } |
675 | ||
676 | // Split the rust-call tupled arguments off. | |
677 | let (first_args, untuple) = if abi == Abi::RustCall && !args.is_empty() { | |
678 | let (tup, args) = args.split_last().unwrap(); | |
679 | (args, Some(tup)) | |
680 | } else { | |
6a06907d | 681 | (args, None) |
532ac7d7 XL |
682 | }; |
683 | ||
532ac7d7 | 684 | 'make_args: for (i, arg) in first_args.iter().enumerate() { |
532ac7d7 XL |
685 | let mut op = self.codegen_operand(&mut bx, arg); |
686 | ||
687 | if let (0, Some(ty::InstanceDef::Virtual(_, idx))) = (i, def) { | |
688 | if let Pair(..) = op.val { | |
689 | // In the case of Rc<Self>, we need to explicitly pass a | |
690 | // *mut RcBox<Self> with a Scalar (not ScalarPair) ABI. This is a hack | |
691 | // that is understood elsewhere in the compiler as a method on | |
692 | // `dyn Trait`. | |
693 | // To get a `*mut RcBox<Self>`, we just keep unwrapping newtypes until | |
694 | // we get a value of a built-in pointer type | |
695 | 'descend_newtypes: while !op.layout.ty.is_unsafe_ptr() | |
dfeec247 | 696 | && !op.layout.ty.is_region_ptr() |
532ac7d7 | 697 | { |
60c5eb7d | 698 | for i in 0..op.layout.fields.count() { |
532ac7d7 XL |
699 | let field = op.extract_field(&mut bx, i); |
700 | if !field.layout.is_zst() { | |
701 | // we found the one non-zero-sized field that is allowed | |
702 | // now find *its* non-zero-sized field, or stop if it's a | |
703 | // pointer | |
704 | op = field; | |
dfeec247 | 705 | continue 'descend_newtypes; |
532ac7d7 XL |
706 | } |
707 | } | |
708 | ||
709 | span_bug!(span, "receiver has no non-zero-sized fields {:?}", op); | |
32a655c1 | 710 | } |
54a0048b | 711 | |
532ac7d7 XL |
712 | // now that we have `*dyn Trait` or `&dyn Trait`, split it up into its |
713 | // data pointer and vtable. Look up the method in the vtable, and pass | |
714 | // the data pointer as the first argument | |
715 | match op.val { | |
716 | Pair(data_ptr, meta) => { | |
dfeec247 XL |
717 | llfn = Some( |
718 | meth::VirtualIndex::from_index(idx).get_fn(&mut bx, meta, &fn_abi), | |
719 | ); | |
532ac7d7 | 720 | llargs.push(data_ptr); |
dfeec247 | 721 | continue 'make_args; |
532ac7d7 XL |
722 | } |
723 | other => bug!("expected a Pair, got {:?}", other), | |
0731742a | 724 | } |
532ac7d7 XL |
725 | } else if let Ref(data_ptr, Some(meta), _) = op.val { |
726 | // by-value dynamic dispatch | |
dfeec247 | 727 | llfn = Some(meth::VirtualIndex::from_index(idx).get_fn(&mut bx, meta, &fn_abi)); |
532ac7d7 XL |
728 | llargs.push(data_ptr); |
729 | continue; | |
730 | } else { | |
731 | span_bug!(span, "can't codegen a virtual call on {:?}", op); | |
0bf4aa26 | 732 | } |
532ac7d7 | 733 | } |
0bf4aa26 | 734 | |
532ac7d7 XL |
735 | // The callee needs to own the argument memory if we pass it |
736 | // by-ref, so make a local copy of non-immediate constants. | |
737 | match (arg, op.val) { | |
dfeec247 XL |
738 | (&mir::Operand::Copy(_), Ref(_, None, _)) |
739 | | (&mir::Operand::Constant(_), Ref(_, None, _)) => { | |
e1599b0c | 740 | let tmp = PlaceRef::alloca(&mut bx, op.layout); |
532ac7d7 XL |
741 | op.val.store(&mut bx, tmp); |
742 | op.val = Ref(tmp.llval, None, tmp.align); | |
743 | } | |
744 | _ => {} | |
745 | } | |
54a0048b | 746 | |
60c5eb7d | 747 | self.codegen_argument(&mut bx, op, &mut llargs, &fn_abi.args[i]); |
532ac7d7 XL |
748 | } |
749 | if let Some(tup) = untuple { | |
dfeec247 XL |
750 | self.codegen_arguments_untupled( |
751 | &mut bx, | |
752 | tup, | |
753 | &mut llargs, | |
754 | &fn_abi.args[first_args.len()..], | |
755 | ) | |
60c5eb7d XL |
756 | } |
757 | ||
758 | let needs_location = | |
759 | instance.map_or(false, |i| i.def.requires_caller_location(self.cx.tcx())); | |
760 | if needs_location { | |
761 | assert_eq!( | |
dfeec247 XL |
762 | fn_abi.args.len(), |
763 | args.len() + 1, | |
60c5eb7d XL |
764 | "#[track_caller] fn's must have 1 more argument in their ABI than in their MIR", |
765 | ); | |
29967ef6 XL |
766 | let location = |
767 | self.get_caller_location(&mut bx, mir::SourceInfo { span: fn_span, ..source_info }); | |
f035d41b XL |
768 | debug!( |
769 | "codegen_call_terminator({:?}): location={:?} (fn_span {:?})", | |
770 | terminator, location, fn_span | |
771 | ); | |
772 | ||
60c5eb7d XL |
773 | let last_arg = fn_abi.args.last().unwrap(); |
774 | self.codegen_argument(&mut bx, location, &mut llargs, last_arg); | |
532ac7d7 | 775 | } |
54a0048b | 776 | |
532ac7d7 XL |
777 | let fn_ptr = match (llfn, instance) { |
778 | (Some(llfn), _) => llfn, | |
e74abb32 | 779 | (None, Some(instance)) => bx.get_fn_addr(instance), |
532ac7d7 XL |
780 | _ => span_bug!(span, "no llfn for call"), |
781 | }; | |
54a0048b | 782 | |
dfeec247 XL |
783 | helper.do_call( |
784 | self, | |
785 | &mut bx, | |
786 | fn_abi, | |
787 | fn_ptr, | |
788 | &llargs, | |
789 | destination.as_ref().map(|&(_, target)| (ret_dest, target)), | |
790 | cleanup, | |
791 | ); | |
532ac7d7 | 792 | } |
f9f354fc XL |
793 | |
794 | fn codegen_asm_terminator( | |
795 | &mut self, | |
796 | helper: TerminatorCodegenHelper<'tcx>, | |
797 | mut bx: Bx, | |
798 | terminator: &mir::Terminator<'tcx>, | |
799 | template: &[ast::InlineAsmTemplatePiece], | |
800 | operands: &[mir::InlineAsmOperand<'tcx>], | |
801 | options: ast::InlineAsmOptions, | |
802 | line_spans: &[Span], | |
803 | destination: Option<mir::BasicBlock>, | |
804 | ) { | |
805 | let span = terminator.source_info.span; | |
806 | ||
807 | let operands: Vec<_> = operands | |
808 | .iter() | |
809 | .map(|op| match *op { | |
810 | mir::InlineAsmOperand::In { reg, ref value } => { | |
811 | let value = self.codegen_operand(&mut bx, value); | |
812 | InlineAsmOperandRef::In { reg, value } | |
813 | } | |
814 | mir::InlineAsmOperand::Out { reg, late, ref place } => { | |
815 | let place = place.map(|place| self.codegen_place(&mut bx, place.as_ref())); | |
816 | InlineAsmOperandRef::Out { reg, late, place } | |
817 | } | |
818 | mir::InlineAsmOperand::InOut { reg, late, ref in_value, ref out_place } => { | |
819 | let in_value = self.codegen_operand(&mut bx, in_value); | |
820 | let out_place = | |
821 | out_place.map(|out_place| self.codegen_place(&mut bx, out_place.as_ref())); | |
822 | InlineAsmOperandRef::InOut { reg, late, in_value, out_place } | |
823 | } | |
824 | mir::InlineAsmOperand::Const { ref value } => { | |
825 | if let mir::Operand::Constant(constant) = value { | |
826 | let const_value = self | |
827 | .eval_mir_constant(constant) | |
828 | .unwrap_or_else(|_| span_bug!(span, "asm const cannot be resolved")); | |
6a06907d | 829 | let ty = constant.ty(); |
f9f354fc XL |
830 | let size = bx.layout_of(ty).size; |
831 | let scalar = match const_value { | |
1b1a35ee XL |
832 | ConstValue::Scalar(s) => s, |
833 | _ => span_bug!( | |
834 | span, | |
835 | "expected Scalar for promoted asm const, but got {:#?}", | |
836 | const_value | |
837 | ), | |
f9f354fc XL |
838 | }; |
839 | let value = scalar.assert_bits(size); | |
1b1a35ee | 840 | let string = match ty.kind() { |
f9f354fc XL |
841 | ty::Uint(_) => value.to_string(), |
842 | ty::Int(int_ty) => { | |
29967ef6 | 843 | match int_ty.normalize(bx.tcx().sess.target.pointer_width) { |
5869c6ff XL |
844 | ty::IntTy::I8 => (value as i8).to_string(), |
845 | ty::IntTy::I16 => (value as i16).to_string(), | |
846 | ty::IntTy::I32 => (value as i32).to_string(), | |
847 | ty::IntTy::I64 => (value as i64).to_string(), | |
848 | ty::IntTy::I128 => (value as i128).to_string(), | |
849 | ty::IntTy::Isize => unreachable!(), | |
f9f354fc XL |
850 | } |
851 | } | |
5869c6ff XL |
852 | ty::Float(ty::FloatTy::F32) => f32::from_bits(value as u32).to_string(), |
853 | ty::Float(ty::FloatTy::F64) => f64::from_bits(value as u64).to_string(), | |
f9f354fc XL |
854 | _ => span_bug!(span, "asm const has bad type {}", ty), |
855 | }; | |
856 | InlineAsmOperandRef::Const { string } | |
857 | } else { | |
858 | span_bug!(span, "asm const is not a constant"); | |
859 | } | |
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 { |
dfeec247 | 1090 | let addr = bx.pointercast(llval, bx.type_ptr_to(bx.cast_backend_type(&ty))); |
a1dfa0c6 | 1091 | llval = bx.load(addr, align.min(arg.layout.align.abi)); |
54a0048b | 1092 | } else { |
ff7c6d11 XL |
1093 | // We can't use `PlaceRef::load` here because the argument |
1094 | // may have a type we don't treat as immediate, but the ABI | |
1095 | // used for this call is passing it by-value. In that case, | |
1096 | // the load would just produce `OperandValue::Ref` instead | |
1097 | // of the `OperandValue::Immediate` we need for the call. | |
2c00a5a8 | 1098 | llval = bx.load(llval, align); |
ba9703b0 | 1099 | if let abi::Abi::Scalar(ref scalar) = arg.layout.abi { |
ff7c6d11 | 1100 | if scalar.is_bool() { |
2c00a5a8 | 1101 | bx.range_metadata(llval, 0..2); |
ff7c6d11 XL |
1102 | } |
1103 | } | |
dc9dc135 | 1104 | // We store bools as `i8` so we need to truncate to `i1`. |
1b1a35ee | 1105 | llval = bx.to_immediate(llval, arg.layout); |
54a0048b SL |
1106 | } |
1107 | } | |
1108 | ||
1109 | llargs.push(llval); | |
1110 | } | |
1111 | ||
a1dfa0c6 XL |
1112 | fn codegen_arguments_untupled( |
1113 | &mut self, | |
1114 | bx: &mut Bx, | |
1115 | operand: &mir::Operand<'tcx>, | |
1116 | llargs: &mut Vec<Bx::Value>, | |
dfeec247 | 1117 | args: &[ArgAbi<'tcx, Ty<'tcx>>], |
a1dfa0c6 | 1118 | ) { |
94b46f34 | 1119 | let tuple = self.codegen_operand(bx, operand); |
54a0048b | 1120 | |
a7813a04 | 1121 | // Handle both by-ref and immediate tuples. |
b7449926 | 1122 | if let Ref(llval, None, align) = tuple.val { |
e1599b0c | 1123 | let tuple_ptr = PlaceRef::new_sized_aligned(llval, tuple.layout, align); |
ff7c6d11 | 1124 | for i in 0..tuple.layout.fields.count() { |
2c00a5a8 | 1125 | let field_ptr = tuple_ptr.project_field(bx, i); |
a1dfa0c6 XL |
1126 | let field = bx.load_operand(field_ptr); |
1127 | self.codegen_argument(bx, field, llargs, &args[i]); | |
3157f602 | 1128 | } |
b7449926 XL |
1129 | } else if let Ref(_, Some(_), _) = tuple.val { |
1130 | bug!("closure arguments must be sized") | |
ff7c6d11 XL |
1131 | } else { |
1132 | // If the tuple is immediate, the elements are as well. | |
1133 | for i in 0..tuple.layout.fields.count() { | |
2c00a5a8 | 1134 | let op = tuple.extract_field(bx, i); |
94b46f34 | 1135 | self.codegen_argument(bx, op, llargs, &args[i]); |
a7813a04 | 1136 | } |
54a0048b SL |
1137 | } |
1138 | } | |
1139 | ||
29967ef6 XL |
1140 | fn get_caller_location( |
1141 | &mut self, | |
1142 | bx: &mut Bx, | |
1143 | mut source_info: mir::SourceInfo, | |
1144 | ) -> OperandRef<'tcx, Bx::Value> { | |
1145 | let tcx = bx.tcx(); | |
1146 | ||
1147 | let mut span_to_caller_location = |span: Span| { | |
60c5eb7d | 1148 | let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span); |
29967ef6 XL |
1149 | let caller = tcx.sess.source_map().lookup_char_pos(topmost.lo()); |
1150 | let const_loc = tcx.const_caller_location(( | |
60c5eb7d XL |
1151 | Symbol::intern(&caller.file.name.to_string()), |
1152 | caller.line as u32, | |
1153 | caller.col_display as u32 + 1, | |
1154 | )); | |
74b04a01 | 1155 | OperandRef::from_const(bx, const_loc, bx.tcx().caller_location_ty()) |
29967ef6 XL |
1156 | }; |
1157 | ||
1158 | // Walk up the `SourceScope`s, in case some of them are from MIR inlining. | |
1159 | // If so, the starting `source_info.span` is in the innermost inlined | |
1160 | // function, and will be replaced with outer callsite spans as long | |
1161 | // as the inlined functions were `#[track_caller]`. | |
1162 | loop { | |
1163 | let scope_data = &self.mir.source_scopes[source_info.scope]; | |
1164 | ||
1165 | if let Some((callee, callsite_span)) = scope_data.inlined { | |
1166 | // Stop inside the most nested non-`#[track_caller]` function, | |
1167 | // before ever reaching its caller (which is irrelevant). | |
1168 | if !callee.def.requires_caller_location(tcx) { | |
1169 | return span_to_caller_location(source_info.span); | |
1170 | } | |
1171 | source_info.span = callsite_span; | |
1172 | } | |
1173 | ||
1174 | // Skip past all of the parents with `inlined: None`. | |
1175 | match scope_data.inlined_parent_scope { | |
1176 | Some(parent) => source_info.scope = parent, | |
1177 | None => break, | |
1178 | } | |
1179 | } | |
1180 | ||
1181 | // No inlined `SourceScope`s, or all of them were `#[track_caller]`. | |
1182 | self.caller_location.unwrap_or_else(|| span_to_caller_location(source_info.span)) | |
e74abb32 XL |
1183 | } |
1184 | ||
dfeec247 | 1185 | fn get_personality_slot(&mut self, bx: &mut Bx) -> PlaceRef<'tcx, Bx::Value> { |
a1dfa0c6 | 1186 | let cx = bx.cx(); |
ff7c6d11 | 1187 | if let Some(slot) = self.personality_slot { |
54a0048b SL |
1188 | slot |
1189 | } else { | |
dfeec247 XL |
1190 | let layout = cx.layout_of( |
1191 | cx.tcx().intern_tup(&[cx.tcx().mk_mut_ptr(cx.tcx().types.u8), cx.tcx().types.i32]), | |
1192 | ); | |
e1599b0c | 1193 | let slot = PlaceRef::alloca(bx, layout); |
ff7c6d11 | 1194 | self.personality_slot = Some(slot); |
32a655c1 | 1195 | slot |
54a0048b SL |
1196 | } |
1197 | } | |
1198 | ||
9fa01778 | 1199 | /// Returns the landing-pad wrapper around the given basic block. |
54a0048b SL |
1200 | /// |
1201 | /// No-op in MSVC SEH scheme. | |
dfeec247 | 1202 | fn landing_pad_to(&mut self, target_bb: mir::BasicBlock) -> Bx::BasicBlock { |
3157f602 XL |
1203 | if let Some(block) = self.landing_pads[target_bb] { |
1204 | return block; | |
1205 | } | |
1206 | ||
cc61c64b XL |
1207 | let block = self.blocks[target_bb]; |
1208 | let landing_pad = self.landing_pad_uncached(block); | |
1209 | self.landing_pads[target_bb] = Some(landing_pad); | |
1210 | landing_pad | |
1211 | } | |
1212 | ||
dfeec247 | 1213 | fn landing_pad_uncached(&mut self, target_bb: Bx::BasicBlock) -> Bx::BasicBlock { |
2c00a5a8 | 1214 | if base::wants_msvc_seh(self.cx.sess()) { |
7cac9316 | 1215 | span_bug!(self.mir.span, "landing pad was not inserted?") |
54a0048b | 1216 | } |
3157f602 | 1217 | |
a1dfa0c6 | 1218 | let mut bx = self.new_block("cleanup"); |
3157f602 | 1219 | |
2c00a5a8 | 1220 | let llpersonality = self.cx.eh_personality(); |
ff7c6d11 | 1221 | let llretty = self.landing_pad_type(); |
2c00a5a8 XL |
1222 | let lp = bx.landing_pad(llretty, llpersonality, 1); |
1223 | bx.set_cleanup(lp); | |
ff7c6d11 | 1224 | |
a1dfa0c6 XL |
1225 | let slot = self.get_personality_slot(&mut bx); |
1226 | slot.storage_live(&mut bx); | |
1227 | Pair(bx.extract_value(lp, 0), bx.extract_value(lp, 1)).store(&mut bx, slot); | |
ff7c6d11 | 1228 | |
2c00a5a8 XL |
1229 | bx.br(target_bb); |
1230 | bx.llbb() | |
54a0048b SL |
1231 | } |
1232 | ||
a1dfa0c6 | 1233 | fn landing_pad_type(&self) -> Bx::Type { |
2c00a5a8 | 1234 | let cx = self.cx; |
a1dfa0c6 | 1235 | cx.type_struct(&[cx.type_i8p(), cx.type_i32()], false) |
ff7c6d11 XL |
1236 | } |
1237 | ||
dfeec247 | 1238 | fn unreachable_block(&mut self) -> Bx::BasicBlock { |
54a0048b | 1239 | self.unreachable_block.unwrap_or_else(|| { |
a1dfa0c6 XL |
1240 | let mut bx = self.new_block("unreachable"); |
1241 | bx.unreachable(); | |
1242 | self.unreachable_block = Some(bx.llbb()); | |
1243 | bx.llbb() | |
54a0048b SL |
1244 | }) |
1245 | } | |
1246 | ||
a1dfa0c6 XL |
1247 | pub fn new_block(&self, name: &str) -> Bx { |
1248 | Bx::new_block(self.cx, self.llfn, name) | |
32a655c1 SL |
1249 | } |
1250 | ||
dfeec247 | 1251 | pub fn build_block(&self, bb: mir::BasicBlock) -> Bx { |
a1dfa0c6 | 1252 | let mut bx = Bx::with_cx(self.cx); |
2c00a5a8 XL |
1253 | bx.position_at_end(self.blocks[bb]); |
1254 | bx | |
54a0048b SL |
1255 | } |
1256 | ||
a1dfa0c6 XL |
1257 | fn make_return_dest( |
1258 | &mut self, | |
1259 | bx: &mut Bx, | |
ba9703b0 | 1260 | dest: mir::Place<'tcx>, |
60c5eb7d | 1261 | fn_ret: &ArgAbi<'tcx, Ty<'tcx>>, |
dfeec247 XL |
1262 | llargs: &mut Vec<Bx::Value>, |
1263 | is_intrinsic: bool, | |
a1dfa0c6 | 1264 | ) -> ReturnDest<'tcx, Bx::Value> { |
dc9dc135 | 1265 | // If the return is ignored, we can just return a do-nothing `ReturnDest`. |
ff7c6d11 | 1266 | if fn_ret.is_ignore() { |
54a0048b SL |
1267 | return ReturnDest::Nothing; |
1268 | } | |
e74abb32 | 1269 | let dest = if let Some(index) = dest.as_local() { |
3157f602 | 1270 | match self.locals[index] { |
ff7c6d11 | 1271 | LocalRef::Place(dest) => dest, |
b7449926 | 1272 | LocalRef::UnsizedPlace(_) => bug!("return type must be sized"), |
3157f602 | 1273 | LocalRef::Operand(None) => { |
dc9dc135 XL |
1274 | // Handle temporary places, specifically `Operand` ones, as |
1275 | // they don't have `alloca`s. | |
ff7c6d11 | 1276 | return if fn_ret.is_indirect() { |
3157f602 XL |
1277 | // Odd, but possible, case, we have an operand temporary, |
1278 | // but the calling convention has an indirect return. | |
e1599b0c | 1279 | let tmp = PlaceRef::alloca(bx, fn_ret.layout); |
2c00a5a8 | 1280 | tmp.storage_live(bx); |
32a655c1 | 1281 | llargs.push(tmp.llval); |
ff7c6d11 | 1282 | ReturnDest::IndirectOperand(tmp, index) |
3157f602 XL |
1283 | } else if is_intrinsic { |
1284 | // Currently, intrinsics always need a location to store | |
dc9dc135 XL |
1285 | // the result, so we create a temporary `alloca` for the |
1286 | // result. | |
e1599b0c | 1287 | let tmp = PlaceRef::alloca(bx, fn_ret.layout); |
2c00a5a8 | 1288 | tmp.storage_live(bx); |
ff7c6d11 | 1289 | ReturnDest::IndirectOperand(tmp, index) |
3157f602 XL |
1290 | } else { |
1291 | ReturnDest::DirectOperand(index) | |
1292 | }; | |
1293 | } | |
1294 | LocalRef::Operand(Some(_)) => { | |
ff7c6d11 | 1295 | bug!("place local already assigned to"); |
54a0048b SL |
1296 | } |
1297 | } | |
3157f602 | 1298 | } else { |
dfeec247 XL |
1299 | self.codegen_place( |
1300 | bx, | |
74b04a01 | 1301 | mir::PlaceRef { local: dest.local, projection: &dest.projection }, |
dfeec247 | 1302 | ) |
54a0048b | 1303 | }; |
ff7c6d11 | 1304 | if fn_ret.is_indirect() { |
a1dfa0c6 | 1305 | if dest.align < dest.layout.align.abi { |
ff7c6d11 XL |
1306 | // Currently, MIR code generation does not create calls |
1307 | // that store directly to fields of packed structs (in | |
dc9dc135 | 1308 | // fact, the calls it creates write only to temps). |
ff7c6d11 XL |
1309 | // |
1310 | // If someone changes that, please update this code path | |
1311 | // to create a temporary. | |
1312 | span_bug!(self.mir.span, "can't directly store to unaligned value"); | |
8bb4bdeb | 1313 | } |
ff7c6d11 XL |
1314 | llargs.push(dest.llval); |
1315 | ReturnDest::Nothing | |
54a0048b | 1316 | } else { |
ff7c6d11 | 1317 | ReturnDest::Store(dest) |
54a0048b SL |
1318 | } |
1319 | } | |
1320 | ||
ba9703b0 | 1321 | fn codegen_transmute(&mut self, bx: &mut Bx, src: &mir::Operand<'tcx>, dst: mir::Place<'tcx>) { |
e74abb32 | 1322 | if let Some(index) = dst.as_local() { |
32a655c1 | 1323 | match self.locals[index] { |
94b46f34 | 1324 | LocalRef::Place(place) => self.codegen_transmute_into(bx, src, place), |
b7449926 | 1325 | LocalRef::UnsizedPlace(_) => bug!("transmute must not involve unsized locals"), |
32a655c1 | 1326 | LocalRef::Operand(None) => { |
74b04a01 | 1327 | let dst_layout = bx.layout_of(self.monomorphized_place_ty(dst.as_ref())); |
ff7c6d11 | 1328 | assert!(!dst_layout.ty.has_erasable_regions()); |
e1599b0c | 1329 | let place = PlaceRef::alloca(bx, dst_layout); |
2c00a5a8 | 1330 | place.storage_live(bx); |
94b46f34 | 1331 | self.codegen_transmute_into(bx, src, place); |
a1dfa0c6 | 1332 | let op = bx.load_operand(place); |
2c00a5a8 | 1333 | place.storage_dead(bx); |
32a655c1 | 1334 | self.locals[index] = LocalRef::Operand(Some(op)); |
74b04a01 | 1335 | self.debug_introduce_local(bx, index); |
32a655c1 | 1336 | } |
ff7c6d11 | 1337 | LocalRef::Operand(Some(op)) => { |
dfeec247 | 1338 | assert!(op.layout.is_zst(), "assigning to initialized SSAtemp"); |
32a655c1 SL |
1339 | } |
1340 | } | |
1341 | } else { | |
74b04a01 | 1342 | let dst = self.codegen_place(bx, dst.as_ref()); |
94b46f34 | 1343 | self.codegen_transmute_into(bx, src, dst); |
32a655c1 SL |
1344 | } |
1345 | } | |
1346 | ||
a1dfa0c6 XL |
1347 | fn codegen_transmute_into( |
1348 | &mut self, | |
1349 | bx: &mut Bx, | |
1350 | src: &mir::Operand<'tcx>, | |
dfeec247 | 1351 | dst: PlaceRef<'tcx, Bx::Value>, |
a1dfa0c6 | 1352 | ) { |
94b46f34 | 1353 | let src = self.codegen_operand(bx, src); |
fc512014 XL |
1354 | |
1355 | // Special-case transmutes between scalars as simple bitcasts. | |
1356 | match (&src.layout.abi, &dst.layout.abi) { | |
1357 | (abi::Abi::Scalar(src_scalar), abi::Abi::Scalar(dst_scalar)) => { | |
1358 | // HACK(eddyb) LLVM doesn't like `bitcast`s between pointers and non-pointers. | |
1359 | if (src_scalar.value == abi::Pointer) == (dst_scalar.value == abi::Pointer) { | |
1360 | assert_eq!(src.layout.size, dst.layout.size); | |
1361 | ||
1362 | // NOTE(eddyb) the `from_immediate` and `to_immediate_scalar` | |
1363 | // conversions allow handling `bool`s the same as `u8`s. | |
1364 | let src = bx.from_immediate(src.immediate()); | |
1365 | let src_as_dst = bx.bitcast(src, bx.backend_type(dst.layout)); | |
1366 | Immediate(bx.to_immediate_scalar(src_as_dst, dst_scalar)).store(bx, dst); | |
1367 | return; | |
1368 | } | |
1369 | } | |
1370 | _ => {} | |
1371 | } | |
1372 | ||
a1dfa0c6 XL |
1373 | let llty = bx.backend_type(src.layout); |
1374 | let cast_ptr = bx.pointercast(dst.llval, bx.type_ptr_to(llty)); | |
1375 | let align = src.layout.align.abi.min(dst.align); | |
e1599b0c | 1376 | src.val.store(bx, PlaceRef::new_sized_aligned(cast_ptr, src.layout, align)); |
54a0048b SL |
1377 | } |
1378 | ||
1379 | // Stores the return value of a function call into it's final location. | |
a1dfa0c6 XL |
1380 | fn store_return( |
1381 | &mut self, | |
1382 | bx: &mut Bx, | |
1383 | dest: ReturnDest<'tcx, Bx::Value>, | |
60c5eb7d | 1384 | ret_abi: &ArgAbi<'tcx, Ty<'tcx>>, |
dfeec247 | 1385 | llval: Bx::Value, |
a1dfa0c6 | 1386 | ) { |
54a0048b SL |
1387 | use self::ReturnDest::*; |
1388 | ||
1389 | match dest { | |
1390 | Nothing => (), | |
60c5eb7d | 1391 | Store(dst) => bx.store_arg(&ret_abi, llval, dst), |
3157f602 | 1392 | IndirectOperand(tmp, index) => { |
a1dfa0c6 | 1393 | let op = bx.load_operand(tmp); |
2c00a5a8 | 1394 | tmp.storage_dead(bx); |
3157f602 | 1395 | self.locals[index] = LocalRef::Operand(Some(op)); |
74b04a01 | 1396 | self.debug_introduce_local(bx, index); |
54a0048b | 1397 | } |
3157f602 XL |
1398 | DirectOperand(index) => { |
1399 | // If there is a cast, we have to store and reload. | |
60c5eb7d XL |
1400 | let op = if let PassMode::Cast(_) = ret_abi.mode { |
1401 | let tmp = PlaceRef::alloca(bx, ret_abi.layout); | |
2c00a5a8 | 1402 | tmp.storage_live(bx); |
60c5eb7d | 1403 | bx.store_arg(&ret_abi, llval, tmp); |
a1dfa0c6 | 1404 | let op = bx.load_operand(tmp); |
2c00a5a8 | 1405 | tmp.storage_dead(bx); |
ff7c6d11 | 1406 | op |
54a0048b | 1407 | } else { |
60c5eb7d | 1408 | OperandRef::from_immediate_or_packed_pair(bx, llval, ret_abi.layout) |
54a0048b | 1409 | }; |
3157f602 | 1410 | self.locals[index] = LocalRef::Operand(Some(op)); |
74b04a01 | 1411 | self.debug_introduce_local(bx, index); |
54a0048b SL |
1412 | } |
1413 | } | |
1414 | } | |
1415 | } | |
1416 | ||
a1dfa0c6 | 1417 | enum ReturnDest<'tcx, V> { |
dc9dc135 | 1418 | // Do nothing; the return value is indirect or ignored. |
54a0048b | 1419 | Nothing, |
dc9dc135 | 1420 | // Store the return value to the pointer. |
a1dfa0c6 | 1421 | Store(PlaceRef<'tcx, V>), |
dc9dc135 | 1422 | // Store an indirect return value to an operand local place. |
a1dfa0c6 | 1423 | IndirectOperand(PlaceRef<'tcx, V>, mir::Local), |
dc9dc135 | 1424 | // Store a direct return value to an operand local place. |
dfeec247 | 1425 | DirectOperand(mir::Local), |
54a0048b | 1426 | } |