]>
Commit | Line | Data |
---|---|---|
a2a8927a | 1 | use cranelift_codegen::isa::TargetFrontendConfig; |
f2b60f7d FG |
2 | use gimli::write::FileId; |
3 | ||
4 | use rustc_data_structures::sync::Lrc; | |
29967ef6 | 5 | use rustc_index::vec::IndexVec; |
c295e0f8 XL |
6 | use rustc_middle::ty::layout::{ |
7 | FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers, | |
8 | }; | |
f2b60f7d | 9 | use rustc_span::SourceFile; |
5869c6ff | 10 | use rustc_target::abi::call::FnAbi; |
29967ef6 XL |
11 | use rustc_target::abi::{Integer, Primitive}; |
12 | use rustc_target::spec::{HasTargetSpec, Target}; | |
13 | ||
17df50a5 | 14 | use crate::constant::ConstantCx; |
f2b60f7d | 15 | use crate::debuginfo::FunctionDebugContext; |
29967ef6 XL |
16 | use crate::prelude::*; |
17 | ||
18 | pub(crate) fn pointer_ty(tcx: TyCtxt<'_>) -> types::Type { | |
19 | match tcx.data_layout.pointer_size.bits() { | |
20 | 16 => types::I16, | |
21 | 32 => types::I32, | |
22 | 64 => types::I64, | |
23 | bits => bug!("ptr_sized_integer: unknown pointer bit size {}", bits), | |
24 | } | |
25 | } | |
26 | ||
27 | pub(crate) fn scalar_to_clif_type(tcx: TyCtxt<'_>, scalar: Scalar) -> Type { | |
04454e1e | 28 | match scalar.primitive() { |
29967ef6 XL |
29 | Primitive::Int(int, _sign) => match int { |
30 | Integer::I8 => types::I8, | |
31 | Integer::I16 => types::I16, | |
32 | Integer::I32 => types::I32, | |
33 | Integer::I64 => types::I64, | |
34 | Integer::I128 => types::I128, | |
35 | }, | |
36 | Primitive::F32 => types::F32, | |
37 | Primitive::F64 => types::F64, | |
9ffffee4 FG |
38 | // FIXME(erikdesjardins): handle non-default addrspace ptr sizes |
39 | Primitive::Pointer(_) => pointer_ty(tcx), | |
29967ef6 XL |
40 | } |
41 | } | |
42 | ||
43 | fn clif_type_from_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<types::Type> { | |
44 | Some(match ty.kind() { | |
45 | ty::Bool => types::I8, | |
46 | ty::Uint(size) => match size { | |
47 | UintTy::U8 => types::I8, | |
48 | UintTy::U16 => types::I16, | |
49 | UintTy::U32 => types::I32, | |
50 | UintTy::U64 => types::I64, | |
51 | UintTy::U128 => types::I128, | |
52 | UintTy::Usize => pointer_ty(tcx), | |
53 | }, | |
54 | ty::Int(size) => match size { | |
55 | IntTy::I8 => types::I8, | |
56 | IntTy::I16 => types::I16, | |
57 | IntTy::I32 => types::I32, | |
58 | IntTy::I64 => types::I64, | |
59 | IntTy::I128 => types::I128, | |
60 | IntTy::Isize => pointer_ty(tcx), | |
61 | }, | |
62 | ty::Char => types::I32, | |
63 | ty::Float(size) => match size { | |
64 | FloatTy::F32 => types::F32, | |
65 | FloatTy::F64 => types::F64, | |
66 | }, | |
67 | ty::FnPtr(_) => pointer_ty(tcx), | |
6a06907d | 68 | ty::RawPtr(TypeAndMut { ty: pointee_ty, mutbl: _ }) | ty::Ref(_, pointee_ty, _) => { |
5099ac24 | 69 | if has_ptr_meta(tcx, *pointee_ty) { |
29967ef6 XL |
70 | return None; |
71 | } else { | |
72 | pointer_ty(tcx) | |
73 | } | |
74 | } | |
5e7ed085 | 75 | ty::Adt(adt_def, _) if adt_def.repr().simd() => { |
29967ef6 XL |
76 | let (element, count) = match &tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap().abi |
77 | { | |
353b0b11 | 78 | Abi::Vector { element, count } => (*element, *count), |
29967ef6 XL |
79 | _ => unreachable!(), |
80 | }; | |
81 | ||
f2b60f7d | 82 | match scalar_to_clif_type(tcx, element).by(u32::try_from(count).unwrap()) { |
29967ef6 XL |
83 | // Cranelift currently only implements icmp for 128bit vectors. |
84 | Some(vector_ty) if vector_ty.bits() == 128 => vector_ty, | |
85 | _ => return None, | |
86 | } | |
87 | } | |
88 | ty::Param(_) => bug!("ty param {:?}", ty), | |
89 | _ => return None, | |
90 | }) | |
91 | } | |
92 | ||
93 | fn clif_pair_type_from_ty<'tcx>( | |
94 | tcx: TyCtxt<'tcx>, | |
95 | ty: Ty<'tcx>, | |
96 | ) -> Option<(types::Type, types::Type)> { | |
97 | Some(match ty.kind() { | |
5e7ed085 FG |
98 | ty::Tuple(types) if types.len() == 2 => { |
99 | let a = clif_type_from_ty(tcx, types[0])?; | |
100 | let b = clif_type_from_ty(tcx, types[1])?; | |
29967ef6 XL |
101 | if a.is_vector() || b.is_vector() { |
102 | return None; | |
103 | } | |
104 | (a, b) | |
105 | } | |
6a06907d | 106 | ty::RawPtr(TypeAndMut { ty: pointee_ty, mutbl: _ }) | ty::Ref(_, pointee_ty, _) => { |
5099ac24 | 107 | if has_ptr_meta(tcx, *pointee_ty) { |
29967ef6 XL |
108 | (pointer_ty(tcx), pointer_ty(tcx)) |
109 | } else { | |
110 | return None; | |
111 | } | |
112 | } | |
113 | _ => return None, | |
114 | }) | |
115 | } | |
116 | ||
117 | /// Is a pointer to this type a fat ptr? | |
118 | pub(crate) fn has_ptr_meta<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool { | |
6a06907d XL |
119 | let ptr_ty = tcx.mk_ptr(TypeAndMut { ty, mutbl: rustc_hir::Mutability::Not }); |
120 | match &tcx.layout_of(ParamEnv::reveal_all().and(ptr_ty)).unwrap().abi { | |
29967ef6 XL |
121 | Abi::Scalar(_) => false, |
122 | Abi::ScalarPair(_, _) => true, | |
123 | abi => unreachable!("Abi of ptr to {:?} is {:?}???", ty, abi), | |
124 | } | |
125 | } | |
126 | ||
127 | pub(crate) fn codegen_icmp_imm( | |
6a06907d | 128 | fx: &mut FunctionCx<'_, '_, '_>, |
29967ef6 XL |
129 | intcc: IntCC, |
130 | lhs: Value, | |
131 | rhs: i128, | |
132 | ) -> Value { | |
133 | let lhs_ty = fx.bcx.func.dfg.value_type(lhs); | |
134 | if lhs_ty == types::I128 { | |
135 | // FIXME legalize `icmp_imm.i128` in Cranelift | |
136 | ||
137 | let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs); | |
138 | let (rhs_lsb, rhs_msb) = (rhs as u128 as u64 as i64, (rhs as u128 >> 64) as u64 as i64); | |
139 | ||
140 | match intcc { | |
141 | IntCC::Equal => { | |
142 | let lsb_eq = fx.bcx.ins().icmp_imm(IntCC::Equal, lhs_lsb, rhs_lsb); | |
143 | let msb_eq = fx.bcx.ins().icmp_imm(IntCC::Equal, lhs_msb, rhs_msb); | |
144 | fx.bcx.ins().band(lsb_eq, msb_eq) | |
145 | } | |
146 | IntCC::NotEqual => { | |
147 | let lsb_ne = fx.bcx.ins().icmp_imm(IntCC::NotEqual, lhs_lsb, rhs_lsb); | |
148 | let msb_ne = fx.bcx.ins().icmp_imm(IntCC::NotEqual, lhs_msb, rhs_msb); | |
149 | fx.bcx.ins().bor(lsb_ne, msb_ne) | |
150 | } | |
151 | _ => { | |
152 | // if msb_eq { | |
153 | // lsb_cc | |
154 | // } else { | |
155 | // msb_cc | |
156 | // } | |
157 | ||
158 | let msb_eq = fx.bcx.ins().icmp_imm(IntCC::Equal, lhs_msb, rhs_msb); | |
159 | let lsb_cc = fx.bcx.ins().icmp_imm(intcc, lhs_lsb, rhs_lsb); | |
160 | let msb_cc = fx.bcx.ins().icmp_imm(intcc, lhs_msb, rhs_msb); | |
161 | ||
162 | fx.bcx.ins().select(msb_eq, lsb_cc, msb_cc) | |
163 | } | |
164 | } | |
165 | } else { | |
9c376795 | 166 | let rhs = rhs as i64; // Truncates on purpose in case rhs is actually an unsigned value |
29967ef6 XL |
167 | fx.bcx.ins().icmp_imm(intcc, lhs, rhs) |
168 | } | |
169 | } | |
170 | ||
9ffffee4 FG |
171 | pub(crate) fn codegen_bitcast(fx: &mut FunctionCx<'_, '_, '_>, dst_ty: Type, val: Value) -> Value { |
172 | let mut flags = MemFlags::new(); | |
173 | flags.set_endianness(match fx.tcx.data_layout.endian { | |
174 | rustc_target::abi::Endian::Big => cranelift_codegen::ir::Endianness::Big, | |
175 | rustc_target::abi::Endian::Little => cranelift_codegen::ir::Endianness::Little, | |
176 | }); | |
177 | fx.bcx.ins().bitcast(dst_ty, flags, val) | |
178 | } | |
179 | ||
9c376795 FG |
180 | pub(crate) fn type_zero_value(bcx: &mut FunctionBuilder<'_>, ty: Type) -> Value { |
181 | if ty == types::I128 { | |
182 | let zero = bcx.ins().iconst(types::I64, 0); | |
183 | bcx.ins().iconcat(zero, zero) | |
184 | } else { | |
185 | bcx.ins().iconst(ty, 0) | |
186 | } | |
187 | } | |
188 | ||
29967ef6 XL |
189 | pub(crate) fn type_min_max_value( |
190 | bcx: &mut FunctionBuilder<'_>, | |
191 | ty: Type, | |
192 | signed: bool, | |
193 | ) -> (Value, Value) { | |
194 | assert!(ty.is_int()); | |
195 | ||
196 | if ty == types::I128 { | |
197 | if signed { | |
198 | let min = i128::MIN as u128; | |
199 | let min_lsb = bcx.ins().iconst(types::I64, min as u64 as i64); | |
200 | let min_msb = bcx.ins().iconst(types::I64, (min >> 64) as u64 as i64); | |
201 | let min = bcx.ins().iconcat(min_lsb, min_msb); | |
202 | ||
fc512014 | 203 | let max = i128::MAX as u128; |
29967ef6 XL |
204 | let max_lsb = bcx.ins().iconst(types::I64, max as u64 as i64); |
205 | let max_msb = bcx.ins().iconst(types::I64, (max >> 64) as u64 as i64); | |
206 | let max = bcx.ins().iconcat(max_lsb, max_msb); | |
207 | ||
208 | return (min, max); | |
209 | } else { | |
210 | let min_half = bcx.ins().iconst(types::I64, 0); | |
211 | let min = bcx.ins().iconcat(min_half, min_half); | |
212 | ||
213 | let max_half = bcx.ins().iconst(types::I64, u64::MAX as i64); | |
214 | let max = bcx.ins().iconcat(max_half, max_half); | |
215 | ||
216 | return (min, max); | |
217 | } | |
218 | } | |
219 | ||
220 | let min = match (ty, signed) { | |
221 | (types::I8, false) | (types::I16, false) | (types::I32, false) | (types::I64, false) => { | |
222 | 0i64 | |
223 | } | |
224 | (types::I8, true) => i64::from(i8::MIN), | |
225 | (types::I16, true) => i64::from(i16::MIN), | |
226 | (types::I32, true) => i64::from(i32::MIN), | |
227 | (types::I64, true) => i64::MIN, | |
228 | _ => unreachable!(), | |
229 | }; | |
230 | ||
231 | let max = match (ty, signed) { | |
232 | (types::I8, false) => i64::from(u8::MAX), | |
233 | (types::I16, false) => i64::from(u16::MAX), | |
234 | (types::I32, false) => i64::from(u32::MAX), | |
235 | (types::I64, false) => u64::MAX as i64, | |
236 | (types::I8, true) => i64::from(i8::MAX), | |
237 | (types::I16, true) => i64::from(i16::MAX), | |
238 | (types::I32, true) => i64::from(i32::MAX), | |
239 | (types::I64, true) => i64::MAX, | |
240 | _ => unreachable!(), | |
241 | }; | |
242 | ||
243 | let (min, max) = (bcx.ins().iconst(ty, min), bcx.ins().iconst(ty, max)); | |
244 | ||
245 | (min, max) | |
246 | } | |
247 | ||
248 | pub(crate) fn type_sign(ty: Ty<'_>) -> bool { | |
249 | match ty.kind() { | |
250 | ty::Ref(..) | ty::RawPtr(..) | ty::FnPtr(..) | ty::Char | ty::Uint(..) | ty::Bool => false, | |
251 | ty::Int(..) => true, | |
252 | ty::Float(..) => false, // `signed` is unused for floats | |
253 | _ => panic!("{}", ty), | |
254 | } | |
255 | } | |
256 | ||
9ffffee4 FG |
257 | pub(crate) fn create_wrapper_function( |
258 | module: &mut dyn Module, | |
259 | unwind_context: &mut UnwindContext, | |
260 | sig: Signature, | |
261 | wrapper_name: &str, | |
262 | callee_name: &str, | |
263 | ) { | |
264 | let wrapper_func_id = module.declare_function(wrapper_name, Linkage::Export, &sig).unwrap(); | |
265 | let callee_func_id = module.declare_function(callee_name, Linkage::Import, &sig).unwrap(); | |
266 | ||
267 | let mut ctx = Context::new(); | |
268 | ctx.func.signature = sig; | |
269 | { | |
270 | let mut func_ctx = FunctionBuilderContext::new(); | |
271 | let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx); | |
272 | ||
273 | let block = bcx.create_block(); | |
274 | bcx.switch_to_block(block); | |
275 | let func = &mut bcx.func.stencil; | |
276 | let args = func | |
277 | .signature | |
278 | .params | |
279 | .iter() | |
280 | .map(|param| func.dfg.append_block_param(block, param.value_type)) | |
281 | .collect::<Vec<Value>>(); | |
282 | ||
283 | let callee_func_ref = module.declare_func_in_func(callee_func_id, &mut bcx.func); | |
284 | let call_inst = bcx.ins().call(callee_func_ref, &args); | |
285 | let results = bcx.inst_results(call_inst).to_vec(); // Clone to prevent borrow error | |
286 | ||
287 | bcx.ins().return_(&results); | |
288 | bcx.seal_all_blocks(); | |
289 | bcx.finalize(); | |
290 | } | |
291 | module.define_function(wrapper_func_id, &mut ctx).unwrap(); | |
292 | unwind_context.add_function(wrapper_func_id, &ctx, module.isa()); | |
293 | } | |
294 | ||
17df50a5 | 295 | pub(crate) struct FunctionCx<'m, 'clif, 'tcx: 'm> { |
f2b60f7d | 296 | pub(crate) cx: &'clif mut crate::CodegenCx, |
17df50a5 | 297 | pub(crate) module: &'m mut dyn Module, |
29967ef6 | 298 | pub(crate) tcx: TyCtxt<'tcx>, |
a2a8927a XL |
299 | pub(crate) target_config: TargetFrontendConfig, // Cached from module |
300 | pub(crate) pointer_type: Type, // Cached from module | |
17df50a5 | 301 | pub(crate) constants_cx: ConstantCx, |
f2b60f7d | 302 | pub(crate) func_debug_cx: Option<FunctionDebugContext>, |
29967ef6 XL |
303 | |
304 | pub(crate) instance: Instance<'tcx>, | |
f2b60f7d | 305 | pub(crate) symbol_name: String, |
29967ef6 | 306 | pub(crate) mir: &'tcx Body<'tcx>, |
c295e0f8 | 307 | pub(crate) fn_abi: Option<&'tcx FnAbi<'tcx, Ty<'tcx>>>, |
29967ef6 XL |
308 | |
309 | pub(crate) bcx: FunctionBuilder<'clif>, | |
310 | pub(crate) block_map: IndexVec<BasicBlock, Block>, | |
311 | pub(crate) local_map: IndexVec<Local, CPlace<'tcx>>, | |
312 | ||
313 | /// When `#[track_caller]` is used, the implicit caller location is stored in this variable. | |
314 | pub(crate) caller_location: Option<CValue<'tcx>>, | |
315 | ||
29967ef6 | 316 | pub(crate) clif_comments: crate::pretty_clif::CommentWriter, |
f2b60f7d FG |
317 | |
318 | /// Last accessed source file and it's debuginfo file id. | |
319 | /// | |
320 | /// For optimization purposes only | |
321 | pub(crate) last_source_file: Option<(Lrc<SourceFile>, FileId)>, | |
29967ef6 XL |
322 | |
323 | /// This should only be accessed by `CPlace::new_var`. | |
324 | pub(crate) next_ssa_var: u32, | |
29967ef6 XL |
325 | } |
326 | ||
c295e0f8 XL |
327 | impl<'tcx> LayoutOfHelpers<'tcx> for FunctionCx<'_, '_, 'tcx> { |
328 | type LayoutOfResult = TyAndLayout<'tcx>; | |
29967ef6 | 329 | |
c295e0f8 XL |
330 | #[inline] |
331 | fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! { | |
332 | RevealAllLayoutCx(self.tcx).handle_layout_err(err, span, ty) | |
333 | } | |
334 | } | |
335 | ||
336 | impl<'tcx> FnAbiOfHelpers<'tcx> for FunctionCx<'_, '_, 'tcx> { | |
337 | type FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>; | |
338 | ||
339 | #[inline] | |
340 | fn handle_fn_abi_err( | |
341 | &self, | |
342 | err: FnAbiError<'tcx>, | |
343 | span: Span, | |
344 | fn_abi_request: FnAbiRequest<'tcx>, | |
345 | ) -> ! { | |
346 | RevealAllLayoutCx(self.tcx).handle_fn_abi_err(err, span, fn_abi_request) | |
29967ef6 XL |
347 | } |
348 | } | |
349 | ||
6a06907d | 350 | impl<'tcx> layout::HasTyCtxt<'tcx> for FunctionCx<'_, '_, 'tcx> { |
29967ef6 XL |
351 | fn tcx<'b>(&'b self) -> TyCtxt<'tcx> { |
352 | self.tcx | |
353 | } | |
354 | } | |
355 | ||
6a06907d | 356 | impl<'tcx> rustc_target::abi::HasDataLayout for FunctionCx<'_, '_, 'tcx> { |
29967ef6 XL |
357 | fn data_layout(&self) -> &rustc_target::abi::TargetDataLayout { |
358 | &self.tcx.data_layout | |
359 | } | |
360 | } | |
361 | ||
6a06907d | 362 | impl<'tcx> layout::HasParamEnv<'tcx> for FunctionCx<'_, '_, 'tcx> { |
29967ef6 XL |
363 | fn param_env(&self) -> ParamEnv<'tcx> { |
364 | ParamEnv::reveal_all() | |
365 | } | |
366 | } | |
367 | ||
6a06907d | 368 | impl<'tcx> HasTargetSpec for FunctionCx<'_, '_, 'tcx> { |
29967ef6 XL |
369 | fn target_spec(&self) -> &Target { |
370 | &self.tcx.sess.target | |
371 | } | |
372 | } | |
373 | ||
6a06907d | 374 | impl<'tcx> FunctionCx<'_, '_, 'tcx> { |
fc512014 | 375 | pub(crate) fn monomorphize<T>(&self, value: T) -> T |
29967ef6 | 376 | where |
9ffffee4 | 377 | T: TypeFoldable<TyCtxt<'tcx>> + Copy, |
29967ef6 XL |
378 | { |
379 | self.instance.subst_mir_and_normalize_erasing_regions( | |
380 | self.tcx, | |
381 | ty::ParamEnv::reveal_all(), | |
fc512014 | 382 | value, |
29967ef6 XL |
383 | ) |
384 | } | |
385 | ||
386 | pub(crate) fn clif_type(&self, ty: Ty<'tcx>) -> Option<Type> { | |
387 | clif_type_from_ty(self.tcx, ty) | |
388 | } | |
389 | ||
390 | pub(crate) fn clif_pair_type(&self, ty: Ty<'tcx>) -> Option<(Type, Type)> { | |
391 | clif_pair_type_from_ty(self.tcx, ty) | |
392 | } | |
393 | ||
394 | pub(crate) fn get_block(&self, bb: BasicBlock) -> Block { | |
395 | *self.block_map.get(bb).unwrap() | |
396 | } | |
397 | ||
398 | pub(crate) fn get_local_place(&mut self, local: Local) -> CPlace<'tcx> { | |
399 | *self.local_map.get(local).unwrap_or_else(|| { | |
400 | panic!("Local {:?} doesn't exist", local); | |
401 | }) | |
402 | } | |
403 | ||
404 | pub(crate) fn set_debug_loc(&mut self, source_info: mir::SourceInfo) { | |
f2b60f7d FG |
405 | if let Some(debug_context) = &mut self.cx.debug_context { |
406 | let (file, line, column) = | |
407 | DebugContext::get_span_loc(self.tcx, self.mir.span, source_info.span); | |
408 | ||
409 | // add_source_file is very slow. | |
410 | // Optimize for the common case of the current file not being changed. | |
411 | let mut cached_file_id = None; | |
412 | if let Some((ref last_source_file, last_file_id)) = self.last_source_file { | |
413 | // If the allocations are not equal, the files may still be equal, but that | |
414 | // doesn't matter, as this is just an optimization. | |
415 | if rustc_data_structures::sync::Lrc::ptr_eq(last_source_file, &file) { | |
416 | cached_file_id = Some(last_file_id); | |
417 | } | |
418 | } | |
419 | ||
420 | let file_id = if let Some(file_id) = cached_file_id { | |
421 | file_id | |
422 | } else { | |
423 | debug_context.add_source_file(&file) | |
424 | }; | |
425 | ||
426 | let source_loc = | |
427 | self.func_debug_cx.as_mut().unwrap().add_dbg_loc(file_id, line, column); | |
428 | self.bcx.set_srcloc(source_loc); | |
429 | } | |
29967ef6 XL |
430 | } |
431 | ||
923072b8 FG |
432 | // Note: must be kept in sync with get_caller_location from cg_ssa |
433 | pub(crate) fn get_caller_location(&mut self, mut source_info: mir::SourceInfo) -> CValue<'tcx> { | |
434 | let span_to_caller_location = |fx: &mut FunctionCx<'_, '_, 'tcx>, span: Span| { | |
435 | let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span); | |
436 | let caller = fx.tcx.sess.source_map().lookup_char_pos(topmost.lo()); | |
437 | let const_loc = fx.tcx.const_caller_location(( | |
438 | rustc_span::symbol::Symbol::intern( | |
439 | &caller.file.name.prefer_remapped().to_string_lossy(), | |
440 | ), | |
441 | caller.line as u32, | |
442 | caller.col_display as u32 + 1, | |
443 | )); | |
444 | crate::constant::codegen_const_value(fx, const_loc, fx.tcx.caller_location_ty()) | |
445 | }; | |
446 | ||
447 | // Walk up the `SourceScope`s, in case some of them are from MIR inlining. | |
448 | // If so, the starting `source_info.span` is in the innermost inlined | |
449 | // function, and will be replaced with outer callsite spans as long | |
450 | // as the inlined functions were `#[track_caller]`. | |
451 | loop { | |
452 | let scope_data = &self.mir.source_scopes[source_info.scope]; | |
453 | ||
454 | if let Some((callee, callsite_span)) = scope_data.inlined { | |
455 | // Stop inside the most nested non-`#[track_caller]` function, | |
456 | // before ever reaching its caller (which is irrelevant). | |
457 | if !callee.def.requires_caller_location(self.tcx) { | |
458 | return span_to_caller_location(self, source_info.span); | |
459 | } | |
460 | source_info.span = callsite_span; | |
461 | } | |
462 | ||
463 | // Skip past all of the parents with `inlined: None`. | |
464 | match scope_data.inlined_parent_scope { | |
465 | Some(parent) => source_info.scope = parent, | |
466 | None => break, | |
467 | } | |
29967ef6 XL |
468 | } |
469 | ||
923072b8 FG |
470 | // No inlined `SourceScope`s, or all of them were `#[track_caller]`. |
471 | self.caller_location.unwrap_or_else(|| span_to_caller_location(self, source_info.span)) | |
29967ef6 XL |
472 | } |
473 | ||
17df50a5 | 474 | pub(crate) fn anonymous_str(&mut self, msg: &str) -> Value { |
29967ef6 XL |
475 | let mut data_ctx = DataContext::new(); |
476 | data_ctx.define(msg.as_bytes().to_vec().into_boxed_slice()); | |
17df50a5 | 477 | let msg_id = self.module.declare_anonymous_data(false, false).unwrap(); |
29967ef6 XL |
478 | |
479 | // Ignore DuplicateDefinition error, as the data will be the same | |
17df50a5 | 480 | let _ = self.module.define_data(msg_id, &data_ctx); |
29967ef6 | 481 | |
17df50a5 | 482 | let local_msg_id = self.module.declare_data_in_func(msg_id, self.bcx.func); |
cdc7bbd5 | 483 | if self.clif_comments.enabled() { |
29967ef6 XL |
484 | self.add_comment(local_msg_id, msg); |
485 | } | |
486 | self.bcx.ins().global_value(self.pointer_type, local_msg_id) | |
487 | } | |
488 | } | |
5869c6ff XL |
489 | |
490 | pub(crate) struct RevealAllLayoutCx<'tcx>(pub(crate) TyCtxt<'tcx>); | |
491 | ||
c295e0f8 XL |
492 | impl<'tcx> LayoutOfHelpers<'tcx> for RevealAllLayoutCx<'tcx> { |
493 | type LayoutOfResult = TyAndLayout<'tcx>; | |
5869c6ff | 494 | |
c295e0f8 XL |
495 | #[inline] |
496 | fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! { | |
497 | if let layout::LayoutError::SizeOverflow(_) = err { | |
498 | self.0.sess.span_fatal(span, &err.to_string()) | |
499 | } else { | |
500 | span_bug!(span, "failed to get layout for `{}`: {}", ty, err) | |
501 | } | |
502 | } | |
503 | } | |
504 | ||
505 | impl<'tcx> FnAbiOfHelpers<'tcx> for RevealAllLayoutCx<'tcx> { | |
506 | type FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>; | |
507 | ||
508 | #[inline] | |
509 | fn handle_fn_abi_err( | |
510 | &self, | |
511 | err: FnAbiError<'tcx>, | |
512 | span: Span, | |
513 | fn_abi_request: FnAbiRequest<'tcx>, | |
514 | ) -> ! { | |
515 | if let FnAbiError::Layout(LayoutError::SizeOverflow(_)) = err { | |
516 | self.0.sess.span_fatal(span, &err.to_string()) | |
517 | } else { | |
518 | match fn_abi_request { | |
519 | FnAbiRequest::OfFnPtr { sig, extra_args } => { | |
520 | span_bug!( | |
521 | span, | |
522 | "`fn_abi_of_fn_ptr({}, {:?})` failed: {}", | |
523 | sig, | |
524 | extra_args, | |
525 | err | |
526 | ); | |
527 | } | |
528 | FnAbiRequest::OfInstance { instance, extra_args } => { | |
529 | span_bug!( | |
530 | span, | |
531 | "`fn_abi_of_instance({}, {:?})` failed: {}", | |
532 | instance, | |
533 | extra_args, | |
534 | err | |
535 | ); | |
536 | } | |
6a06907d | 537 | } |
c295e0f8 | 538 | } |
5869c6ff XL |
539 | } |
540 | } | |
541 | ||
542 | impl<'tcx> layout::HasTyCtxt<'tcx> for RevealAllLayoutCx<'tcx> { | |
543 | fn tcx<'b>(&'b self) -> TyCtxt<'tcx> { | |
544 | self.0 | |
545 | } | |
546 | } | |
547 | ||
548 | impl<'tcx> rustc_target::abi::HasDataLayout for RevealAllLayoutCx<'tcx> { | |
549 | fn data_layout(&self) -> &rustc_target::abi::TargetDataLayout { | |
550 | &self.0.data_layout | |
551 | } | |
552 | } | |
553 | ||
554 | impl<'tcx> layout::HasParamEnv<'tcx> for RevealAllLayoutCx<'tcx> { | |
555 | fn param_env(&self) -> ParamEnv<'tcx> { | |
556 | ParamEnv::reveal_all() | |
557 | } | |
558 | } | |
559 | ||
560 | impl<'tcx> HasTargetSpec for RevealAllLayoutCx<'tcx> { | |
561 | fn target_spec(&self) -> &Target { | |
562 | &self.0.sess.target | |
563 | } | |
564 | } |