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