1use libc::{c_char, c_uint};
4use rustc_abi as abi;
5use rustc_abi::Primitive::Pointer;
6use rustc_abi::{AddressSpace, HasDataLayout};
7use rustc_ast::Mutability;
8use rustc_codegen_ssa::common::TypeKind;
9use rustc_codegen_ssa::traits::*;
10use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
11use rustc_hashes::Hash128;
12use rustc_hir::def_id::DefId;
13use rustc_middle::bug;
14use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
15use rustc_middle::ty::TyCtxt;
16use rustc_session::cstore::DllImport;
17use tracing::debug;
18
19use crate::consts::const_alloc_to_llvm;
20pub(crate) use crate::context::CodegenCx;
21use crate::llvm::{self, BasicBlock, Bool, ConstantInt, False, Metadata, True};
22use crate::type_::Type;
23use crate::value::Value;
24
25pub(crate) struct Funclet<'ll> {
66 cleanuppad: &'ll Value,
67 operand: llvm::OperandBundleOwned<'ll>,
68}
69
70impl<'ll> Funclet<'ll> {
71 pub(crate) fn new(cleanuppad: &'ll Value) -> Self {
72 Funclet { cleanuppad, operand: llvm::OperandBundleOwned::new("funclet", &[cleanuppad]) }
73 }
74
75 pub(crate) fn cleanuppad(&self) -> &'ll Value {
76 self.cleanuppad
77 }
78
79 pub(crate) fn bundle(&self) -> &llvm::OperandBundle<'ll> {
80 &self.operand
81 }
82}
83
84impl<'ll> BackendTypes for CodegenCx<'ll, '_> {
85 type Value = &'ll Value;
86 type Metadata = &'ll Metadata;
87 type Function = &'ll Value;
89
90 type BasicBlock = &'ll BasicBlock;
91 type Type = &'ll Type;
92 type Funclet = Funclet<'ll>;
93
94 type DIScope = &'ll llvm::debuginfo::DIScope;
95 type DILocation = &'ll llvm::debuginfo::DILocation;
96 type DIVariable = &'ll llvm::debuginfo::DIVariable;
97}
98
99impl<'ll> CodegenCx<'ll, '_> {
100 pub(crate) fn const_array(&self, ty: &'ll Type, elts: &[&'ll Value]) -> &'ll Value {
101 let len = u64::try_from(elts.len()).expect("LLVMConstArray2 elements len overflow");
102 unsafe { llvm::LLVMConstArray2(ty, elts.as_ptr(), len) }
103 }
104
105 pub(crate) fn const_bytes(&self, bytes: &[u8]) -> &'ll Value {
106 bytes_in_context(self.llcx, bytes)
107 }
108
109 pub(crate) fn const_get_elt(&self, v: &'ll Value, idx: u64) -> &'ll Value {
110 unsafe {
111 let idx = c_uint::try_from(idx).expect("LLVMGetAggregateElement index overflow");
112 let r = llvm::LLVMGetAggregateElement(v, idx).unwrap();
113
114 debug!("const_get_elt(v={:?}, idx={}, r={:?})", v, idx, r);
115
116 r
117 }
118 }
119}
120
121impl<'ll, 'tcx> ConstCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
122 fn const_null(&self, t: &'ll Type) -> &'ll Value {
123 unsafe { llvm::LLVMConstNull(t) }
124 }
125
126 fn const_undef(&self, t: &'ll Type) -> &'ll Value {
127 unsafe { llvm::LLVMGetUndef(t) }
128 }
129
130 fn is_undef(&self, v: &'ll Value) -> bool {
131 unsafe { llvm::LLVMIsUndef(v) == True }
132 }
133
134 fn const_poison(&self, t: &'ll Type) -> &'ll Value {
135 unsafe { llvm::LLVMGetPoison(t) }
136 }
137
138 fn const_bool(&self, val: bool) -> &'ll Value {
139 self.const_uint(self.type_i1(), val as u64)
140 }
141
142 fn const_i8(&self, i: i8) -> &'ll Value {
143 self.const_int(self.type_i8(), i as i64)
144 }
145
146 fn const_i16(&self, i: i16) -> &'ll Value {
147 self.const_int(self.type_i16(), i as i64)
148 }
149
150 fn const_i32(&self, i: i32) -> &'ll Value {
151 self.const_int(self.type_i32(), i as i64)
152 }
153
154 fn const_int(&self, t: &'ll Type, i: i64) -> &'ll Value {
155 debug_assert!(
156 self.type_kind(t) == TypeKind::Integer,
157 "only allows integer types in const_int"
158 );
159 unsafe { llvm::LLVMConstInt(t, i as u64, True) }
160 }
161
162 fn const_u8(&self, i: u8) -> &'ll Value {
163 self.const_uint(self.type_i8(), i as u64)
164 }
165
166 fn const_u32(&self, i: u32) -> &'ll Value {
167 self.const_uint(self.type_i32(), i as u64)
168 }
169
170 fn const_u64(&self, i: u64) -> &'ll Value {
171 self.const_uint(self.type_i64(), i)
172 }
173
174 fn const_u128(&self, i: u128) -> &'ll Value {
175 self.const_uint_big(self.type_i128(), i)
176 }
177
178 fn const_usize(&self, i: u64) -> &'ll Value {
179 let bit_size = self.data_layout().pointer_size.bits();
180 if bit_size < 64 {
181 assert!(i < (1 << bit_size));
183 }
184
185 self.const_uint(self.isize_ty, i)
186 }
187
188 fn const_uint(&self, t: &'ll Type, i: u64) -> &'ll Value {
189 debug_assert!(
190 self.type_kind(t) == TypeKind::Integer,
191 "only allows integer types in const_uint"
192 );
193 unsafe { llvm::LLVMConstInt(t, i, False) }
194 }
195
196 fn const_uint_big(&self, t: &'ll Type, u: u128) -> &'ll Value {
197 debug_assert!(
198 self.type_kind(t) == TypeKind::Integer,
199 "only allows integer types in const_uint_big"
200 );
201 unsafe {
202 let words = [u as u64, (u >> 64) as u64];
203 llvm::LLVMConstIntOfArbitraryPrecision(t, 2, words.as_ptr())
204 }
205 }
206
207 fn const_real(&self, t: &'ll Type, val: f64) -> &'ll Value {
208 unsafe { llvm::LLVMConstReal(t, val) }
209 }
210
211 fn const_str(&self, s: &str) -> (&'ll Value, &'ll Value) {
212 let str_global = *self
213 .const_str_cache
214 .borrow_mut()
215 .raw_entry_mut()
216 .from_key(s)
217 .or_insert_with(|| {
218 let sc = self.const_bytes(s.as_bytes());
219 let sym = self.generate_local_symbol_name("str");
220 let g = self.define_global(&sym, self.val_ty(sc)).unwrap_or_else(|| {
221 bug!("symbol `{}` is already defined", sym);
222 });
223 llvm::set_initializer(g, sc);
224 unsafe {
225 llvm::LLVMSetGlobalConstant(g, True);
226 llvm::LLVMSetUnnamedAddress(g, llvm::UnnamedAddr::Global);
227 }
228 llvm::set_linkage(g, llvm::Linkage::InternalLinkage);
229 let g = self.const_pointercast(g, self.type_ptr());
231 (s.to_owned(), g)
232 })
233 .1;
234 let len = s.len();
235 (str_global, self.const_usize(len as u64))
236 }
237
238 fn const_struct(&self, elts: &[&'ll Value], packed: bool) -> &'ll Value {
239 struct_in_context(self.llcx, elts, packed)
240 }
241
242 fn const_vector(&self, elts: &[&'ll Value]) -> &'ll Value {
243 let len = c_uint::try_from(elts.len()).expect("LLVMConstVector elements len overflow");
244 unsafe { llvm::LLVMConstVector(elts.as_ptr(), len) }
245 }
246
247 fn const_to_opt_uint(&self, v: &'ll Value) -> Option<u64> {
248 try_as_const_integral(v).and_then(|v| unsafe {
249 let mut i = 0u64;
250 let success = llvm::LLVMRustConstIntGetZExtValue(v, &mut i);
251 success.then_some(i)
252 })
253 }
254
255 fn const_to_opt_u128(&self, v: &'ll Value, sign_ext: bool) -> Option<u128> {
256 try_as_const_integral(v).and_then(|v| unsafe {
257 let (mut lo, mut hi) = (0u64, 0u64);
258 let success = llvm::LLVMRustConstInt128Get(v, sign_ext, &mut hi, &mut lo);
259 success.then_some(hi_lo_to_u128(lo, hi))
260 })
261 }
262
263 fn scalar_to_backend(&self, cv: Scalar, layout: abi::Scalar, llty: &'ll Type) -> &'ll Value {
264 let bitsize = if layout.is_bool() { 1 } else { layout.size(self).bits() };
265 match cv {
266 Scalar::Int(int) => {
267 let data = int.to_bits(layout.size(self));
268 let llval = self.const_uint_big(self.type_ix(bitsize), data);
269 if matches!(layout.primitive(), Pointer(_)) {
270 unsafe { llvm::LLVMConstIntToPtr(llval, llty) }
271 } else {
272 self.const_bitcast(llval, llty)
273 }
274 }
275 Scalar::Ptr(ptr, _size) => {
276 let (prov, offset) = ptr.into_parts();
277 let (base_addr, base_addr_space) = match self.tcx.global_alloc(prov.alloc_id()) {
278 GlobalAlloc::Memory(alloc) => {
279 if alloc.inner().len() == 0 {
283 assert_eq!(offset.bytes(), 0);
284 let llval = self.const_usize(alloc.inner().align.bytes());
285 return if matches!(layout.primitive(), Pointer(_)) {
286 unsafe { llvm::LLVMConstIntToPtr(llval, llty) }
287 } else {
288 self.const_bitcast(llval, llty)
289 };
290 } else {
291 let init = const_alloc_to_llvm(self, alloc, false);
292 let alloc = alloc.inner();
293 let value = match alloc.mutability {
294 Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None),
295 _ => self.static_addr_of_impl(init, alloc.align, None),
296 };
297 if !self.sess().fewer_names() && llvm::get_value_name(value).is_empty()
298 {
299 let hash = self.tcx.with_stable_hashing_context(|mut hcx| {
300 let mut hasher = StableHasher::new();
301 alloc.hash_stable(&mut hcx, &mut hasher);
302 hasher.finish::<Hash128>()
303 });
304 llvm::set_value_name(
305 value,
306 format!("alloc_{hash:032x}").as_bytes(),
307 );
308 }
309 (value, AddressSpace::DATA)
310 }
311 }
312 GlobalAlloc::Function { instance, .. } => {
313 (self.get_fn_addr(instance), self.data_layout().instruction_address_space)
314 }
315 GlobalAlloc::VTable(ty, dyn_ty) => {
316 let alloc = self
317 .tcx
318 .global_alloc(self.tcx.vtable_allocation((
319 ty,
320 dyn_ty.principal().map(|principal| {
321 self.tcx.instantiate_bound_regions_with_erased(principal)
322 }),
323 )))
324 .unwrap_memory();
325 let init = const_alloc_to_llvm(self, alloc, false);
326 let value = self.static_addr_of_impl(init, alloc.inner().align, None);
327 (value, AddressSpace::DATA)
328 }
329 GlobalAlloc::Static(def_id) => {
330 assert!(self.tcx.is_static(def_id));
331 assert!(!self.tcx.is_thread_local_static(def_id));
332 (self.get_static(def_id), AddressSpace::DATA)
333 }
334 };
335 let llval = unsafe {
336 llvm::LLVMConstInBoundsGEP2(
337 self.type_i8(),
338 self.const_pointercast(base_addr, self.type_ptr_ext(base_addr_space)),
340 &self.const_usize(offset.bytes()),
341 1,
342 )
343 };
344 if !matches!(layout.primitive(), Pointer(_)) {
345 unsafe { llvm::LLVMConstPtrToInt(llval, llty) }
346 } else {
347 self.const_bitcast(llval, llty)
348 }
349 }
350 }
351 }
352
353 fn const_data_from_alloc(&self, alloc: ConstAllocation<'tcx>) -> Self::Value {
354 const_alloc_to_llvm(self, alloc, false)
355 }
356
357 fn const_ptr_byte_offset(&self, base_addr: Self::Value, offset: abi::Size) -> Self::Value {
358 unsafe {
359 llvm::LLVMConstInBoundsGEP2(
360 self.type_i8(),
361 base_addr,
362 &self.const_usize(offset.bytes()),
363 1,
364 )
365 }
366 }
367}
368
369pub(crate) fn val_ty(v: &Value) -> &Type {
371 unsafe { llvm::LLVMTypeOf(v) }
372}
373
374pub(crate) fn bytes_in_context<'ll>(llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
375 unsafe {
376 let ptr = bytes.as_ptr() as *const c_char;
377 llvm::LLVMConstStringInContext2(llcx, ptr, bytes.len(), True)
378 }
379}
380
381fn struct_in_context<'ll>(
382 llcx: &'ll llvm::Context,
383 elts: &[&'ll Value],
384 packed: bool,
385) -> &'ll Value {
386 let len = c_uint::try_from(elts.len()).expect("LLVMConstStructInContext elements len overflow");
387 unsafe { llvm::LLVMConstStructInContext(llcx, elts.as_ptr(), len, packed as Bool) }
388}
389
390#[inline]
391fn hi_lo_to_u128(lo: u64, hi: u64) -> u128 {
392 ((hi as u128) << 64) | (lo as u128)
393}
394
395fn try_as_const_integral(v: &Value) -> Option<&ConstantInt> {
396 unsafe { llvm::LLVMIsAConstantInt(v) }
397}
398
399pub(crate) fn get_dllimport<'tcx>(
400 tcx: TyCtxt<'tcx>,
401 id: DefId,
402 name: &str,
403) -> Option<&'tcx DllImport> {
404 tcx.native_library(id)
405 .and_then(|lib| lib.dll_imports.iter().find(|di| di.name.as_str() == name))
406}
407
408pub(crate) trait AsCCharPtr {
410 fn as_c_char_ptr(&self) -> *const c_char;
412}
413
414impl AsCCharPtr for str {
415 fn as_c_char_ptr(&self) -> *const c_char {
416 self.as_ptr().cast()
417 }
418}
419
420impl AsCCharPtr for [u8] {
421 fn as_c_char_ptr(&self) -> *const c_char {
422 self.as_ptr().cast()
423 }
424}