]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/common.rs
Auto merge of #97313 - cjgillot:ast-lifetimes-anon, r=petrochenkov
[rust.git] / compiler / rustc_codegen_llvm / src / common.rs
1 //! Code that is useful in various codegen modules.
2
3 use crate::consts::{self, const_alloc_to_llvm};
4 pub use crate::context::CodegenCx;
5 use crate::llvm::{self, BasicBlock, Bool, ConstantInt, False, OperandBundleDef, True};
6 use crate::type_::Type;
7 use crate::type_of::LayoutLlvmExt;
8 use crate::value::Value;
9
10 use rustc_ast::Mutability;
11 use rustc_codegen_ssa::mir::place::PlaceRef;
12 use rustc_codegen_ssa::traits::*;
13 use rustc_middle::bug;
14 use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
15 use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
16 use rustc_target::abi::{self, AddressSpace, HasDataLayout, Pointer, Size};
17
18 use libc::{c_char, c_uint};
19 use tracing::debug;
20
21 /*
22 * A note on nomenclature of linking: "extern", "foreign", and "upcall".
23 *
24 * An "extern" is an LLVM symbol we wind up emitting an undefined external
25 * reference to. This means "we don't have the thing in this compilation unit,
26 * please make sure you link it in at runtime". This could be a reference to
27 * C code found in a C library, or rust code found in a rust crate.
28 *
29 * Most "externs" are implicitly declared (automatically) as a result of a
30 * user declaring an extern _module_ dependency; this causes the rust driver
31 * to locate an extern crate, scan its compilation metadata, and emit extern
32 * declarations for any symbols used by the declaring crate.
33 *
34 * A "foreign" is an extern that references C (or other non-rust ABI) code.
35 * There is no metadata to scan for extern references so in these cases either
36 * a header-digester like bindgen, or manual function prototypes, have to
37 * serve as declarators. So these are usually given explicitly as prototype
38 * declarations, in rust code, with ABI attributes on them noting which ABI to
39 * link via.
40 *
41 * An "upcall" is a foreign call generated by the compiler (not corresponding
42 * to any user-written call in the code) into the runtime library, to perform
43 * some helper task such as bringing a task to life, allocating memory, etc.
44 *
45 */
46
47 /// A structure representing an active landing pad for the duration of a basic
48 /// block.
49 ///
50 /// Each `Block` may contain an instance of this, indicating whether the block
51 /// is part of a landing pad or not. This is used to make decision about whether
52 /// to emit `invoke` instructions (e.g., in a landing pad we don't continue to
53 /// use `invoke`) and also about various function call metadata.
54 ///
55 /// For GNU exceptions (`landingpad` + `resume` instructions) this structure is
56 /// just a bunch of `None` instances (not too interesting), but for MSVC
57 /// exceptions (`cleanuppad` + `cleanupret` instructions) this contains data.
58 /// When inside of a landing pad, each function call in LLVM IR needs to be
59 /// annotated with which landing pad it's a part of. This is accomplished via
60 /// the `OperandBundleDef` value created for MSVC landing pads.
61 pub struct Funclet<'ll> {
62     cleanuppad: &'ll Value,
63     operand: OperandBundleDef<'ll>,
64 }
65
66 impl<'ll> Funclet<'ll> {
67     pub fn new(cleanuppad: &'ll Value) -> Self {
68         Funclet { cleanuppad, operand: OperandBundleDef::new("funclet", &[cleanuppad]) }
69     }
70
71     pub fn cleanuppad(&self) -> &'ll Value {
72         self.cleanuppad
73     }
74
75     pub fn bundle(&self) -> &OperandBundleDef<'ll> {
76         &self.operand
77     }
78 }
79
80 impl<'ll> BackendTypes for CodegenCx<'ll, '_> {
81     type Value = &'ll Value;
82     // FIXME(eddyb) replace this with a `Function` "subclass" of `Value`.
83     type Function = &'ll Value;
84
85     type BasicBlock = &'ll BasicBlock;
86     type Type = &'ll Type;
87     type Funclet = Funclet<'ll>;
88
89     type DIScope = &'ll llvm::debuginfo::DIScope;
90     type DILocation = &'ll llvm::debuginfo::DILocation;
91     type DIVariable = &'ll llvm::debuginfo::DIVariable;
92 }
93
94 impl<'ll> CodegenCx<'ll, '_> {
95     pub fn const_array(&self, ty: &'ll Type, elts: &[&'ll Value]) -> &'ll Value {
96         unsafe { llvm::LLVMConstArray(ty, elts.as_ptr(), elts.len() as c_uint) }
97     }
98
99     pub fn const_vector(&self, elts: &[&'ll Value]) -> &'ll Value {
100         unsafe { llvm::LLVMConstVector(elts.as_ptr(), elts.len() as c_uint) }
101     }
102
103     pub fn const_bytes(&self, bytes: &[u8]) -> &'ll Value {
104         bytes_in_context(self.llcx, bytes)
105     }
106
107     pub fn const_get_elt(&self, v: &'ll Value, idx: u64) -> &'ll Value {
108         unsafe {
109             assert_eq!(idx as c_uint as u64, idx);
110             let r = llvm::LLVMGetAggregateElement(v, idx as c_uint).unwrap();
111
112             debug!("const_get_elt(v={:?}, idx={}, r={:?})", v, idx, r);
113
114             r
115         }
116     }
117 }
118
119 impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
120     fn const_null(&self, t: &'ll Type) -> &'ll Value {
121         unsafe { llvm::LLVMConstNull(t) }
122     }
123
124     fn const_undef(&self, t: &'ll Type) -> &'ll Value {
125         unsafe { llvm::LLVMGetUndef(t) }
126     }
127
128     fn const_int(&self, t: &'ll Type, i: i64) -> &'ll Value {
129         unsafe { llvm::LLVMConstInt(t, i as u64, True) }
130     }
131
132     fn const_uint(&self, t: &'ll Type, i: u64) -> &'ll Value {
133         unsafe { llvm::LLVMConstInt(t, i, False) }
134     }
135
136     fn const_uint_big(&self, t: &'ll Type, u: u128) -> &'ll Value {
137         unsafe {
138             let words = [u as u64, (u >> 64) as u64];
139             llvm::LLVMConstIntOfArbitraryPrecision(t, 2, words.as_ptr())
140         }
141     }
142
143     fn const_bool(&self, val: bool) -> &'ll Value {
144         self.const_uint(self.type_i1(), val as u64)
145     }
146
147     fn const_i16(&self, i: i16) -> &'ll Value {
148         self.const_int(self.type_i16(), i as i64)
149     }
150
151     fn const_i32(&self, i: i32) -> &'ll Value {
152         self.const_int(self.type_i32(), i as i64)
153     }
154
155     fn const_u32(&self, i: u32) -> &'ll Value {
156         self.const_uint(self.type_i32(), i as u64)
157     }
158
159     fn const_u64(&self, i: u64) -> &'ll Value {
160         self.const_uint(self.type_i64(), i)
161     }
162
163     fn const_usize(&self, i: u64) -> &'ll Value {
164         let bit_size = self.data_layout().pointer_size.bits();
165         if bit_size < 64 {
166             // make sure it doesn't overflow
167             assert!(i < (1 << bit_size));
168         }
169
170         self.const_uint(self.isize_ty, i)
171     }
172
173     fn const_u8(&self, i: u8) -> &'ll Value {
174         self.const_uint(self.type_i8(), i as u64)
175     }
176
177     fn const_real(&self, t: &'ll Type, val: f64) -> &'ll Value {
178         unsafe { llvm::LLVMConstReal(t, val) }
179     }
180
181     fn const_str(&self, s: &str) -> (&'ll Value, &'ll Value) {
182         let str_global = *self
183             .const_str_cache
184             .borrow_mut()
185             .raw_entry_mut()
186             .from_key(s)
187             .or_insert_with(|| {
188                 let sc = self.const_bytes(s.as_bytes());
189                 let sym = self.generate_local_symbol_name("str");
190                 let g = self.define_global(&sym, self.val_ty(sc)).unwrap_or_else(|| {
191                     bug!("symbol `{}` is already defined", sym);
192                 });
193                 unsafe {
194                     llvm::LLVMSetInitializer(g, sc);
195                     llvm::LLVMSetGlobalConstant(g, True);
196                     llvm::LLVMRustSetLinkage(g, llvm::Linkage::InternalLinkage);
197                 }
198                 (s.to_owned(), g)
199             })
200             .1;
201         let len = s.len();
202         let cs = consts::ptrcast(
203             str_global,
204             self.type_ptr_to(self.layout_of(self.tcx.types.str_).llvm_type(self)),
205         );
206         (cs, self.const_usize(len as u64))
207     }
208
209     fn const_struct(&self, elts: &[&'ll Value], packed: bool) -> &'ll Value {
210         struct_in_context(self.llcx, elts, packed)
211     }
212
213     fn const_to_opt_uint(&self, v: &'ll Value) -> Option<u64> {
214         try_as_const_integral(v).map(|v| unsafe { llvm::LLVMConstIntGetZExtValue(v) })
215     }
216
217     fn const_to_opt_u128(&self, v: &'ll Value, sign_ext: bool) -> Option<u128> {
218         try_as_const_integral(v).and_then(|v| unsafe {
219             let (mut lo, mut hi) = (0u64, 0u64);
220             let success = llvm::LLVMRustConstInt128Get(v, sign_ext, &mut hi, &mut lo);
221             success.then_some(hi_lo_to_u128(lo, hi))
222         })
223     }
224
225     fn zst_to_backend(&self, _llty: &'ll Type) -> &'ll Value {
226         self.const_undef(self.type_ix(0))
227     }
228
229     fn scalar_to_backend(&self, cv: Scalar, layout: abi::Scalar, llty: &'ll Type) -> &'ll Value {
230         let bitsize = if layout.is_bool() { 1 } else { layout.size(self).bits() };
231         match cv {
232             Scalar::Int(int) => {
233                 let data = int.assert_bits(layout.size(self));
234                 let llval = self.const_uint_big(self.type_ix(bitsize), data);
235                 if layout.primitive() == Pointer {
236                     unsafe { llvm::LLVMConstIntToPtr(llval, llty) }
237                 } else {
238                     self.const_bitcast(llval, llty)
239                 }
240             }
241             Scalar::Ptr(ptr, _size) => {
242                 let (alloc_id, offset) = ptr.into_parts();
243                 let (base_addr, base_addr_space) = match self.tcx.global_alloc(alloc_id) {
244                     GlobalAlloc::Memory(alloc) => {
245                         let init = const_alloc_to_llvm(self, alloc);
246                         let alloc = alloc.inner();
247                         let value = match alloc.mutability {
248                             Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None),
249                             _ => self.static_addr_of(init, alloc.align, None),
250                         };
251                         if !self.sess().fewer_names() {
252                             llvm::set_value_name(value, format!("{:?}", alloc_id).as_bytes());
253                         }
254                         (value, AddressSpace::DATA)
255                     }
256                     GlobalAlloc::Function(fn_instance) => (
257                         self.get_fn_addr(fn_instance.polymorphize(self.tcx)),
258                         self.data_layout().instruction_address_space,
259                     ),
260                     GlobalAlloc::VTable(ty, trait_ref) => {
261                         let alloc = self
262                             .tcx
263                             .global_alloc(self.tcx.vtable_allocation((ty, trait_ref)))
264                             .unwrap_memory();
265                         let init = const_alloc_to_llvm(self, alloc);
266                         let value = self.static_addr_of(init, alloc.inner().align, None);
267                         (value, AddressSpace::DATA)
268                     }
269                     GlobalAlloc::Static(def_id) => {
270                         assert!(self.tcx.is_static(def_id));
271                         assert!(!self.tcx.is_thread_local_static(def_id));
272                         (self.get_static(def_id), AddressSpace::DATA)
273                     }
274                 };
275                 let llval = unsafe {
276                     llvm::LLVMRustConstInBoundsGEP2(
277                         self.type_i8(),
278                         self.const_bitcast(base_addr, self.type_i8p_ext(base_addr_space)),
279                         &self.const_usize(offset.bytes()),
280                         1,
281                     )
282                 };
283                 if layout.primitive() != Pointer {
284                     unsafe { llvm::LLVMConstPtrToInt(llval, llty) }
285                 } else {
286                     self.const_bitcast(llval, llty)
287                 }
288             }
289         }
290     }
291
292     fn const_data_from_alloc(&self, alloc: ConstAllocation<'tcx>) -> Self::Value {
293         const_alloc_to_llvm(self, alloc)
294     }
295
296     fn from_const_alloc(
297         &self,
298         layout: TyAndLayout<'tcx>,
299         alloc: ConstAllocation<'tcx>,
300         offset: Size,
301     ) -> PlaceRef<'tcx, &'ll Value> {
302         let alloc_align = alloc.inner().align;
303         assert_eq!(alloc_align, layout.align.abi);
304         let llty = self.type_ptr_to(layout.llvm_type(self));
305         let llval = if layout.size == Size::ZERO {
306             let llval = self.const_usize(alloc_align.bytes());
307             unsafe { llvm::LLVMConstIntToPtr(llval, llty) }
308         } else {
309             let init = const_alloc_to_llvm(self, alloc);
310             let base_addr = self.static_addr_of(init, alloc_align, None);
311
312             let llval = unsafe {
313                 llvm::LLVMRustConstInBoundsGEP2(
314                     self.type_i8(),
315                     self.const_bitcast(base_addr, self.type_i8p()),
316                     &self.const_usize(offset.bytes()),
317                     1,
318                 )
319             };
320             self.const_bitcast(llval, llty)
321         };
322         PlaceRef::new_sized(llval, layout)
323     }
324
325     fn const_ptrcast(&self, val: &'ll Value, ty: &'ll Type) -> &'ll Value {
326         consts::ptrcast(val, ty)
327     }
328 }
329
330 /// Get the [LLVM type][Type] of a [`Value`].
331 pub fn val_ty(v: &Value) -> &Type {
332     unsafe { llvm::LLVMTypeOf(v) }
333 }
334
335 pub fn bytes_in_context<'ll>(llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
336     unsafe {
337         let ptr = bytes.as_ptr() as *const c_char;
338         llvm::LLVMConstStringInContext(llcx, ptr, bytes.len() as c_uint, True)
339     }
340 }
341
342 pub fn struct_in_context<'ll>(
343     llcx: &'ll llvm::Context,
344     elts: &[&'ll Value],
345     packed: bool,
346 ) -> &'ll Value {
347     unsafe {
348         llvm::LLVMConstStructInContext(llcx, elts.as_ptr(), elts.len() as c_uint, packed as Bool)
349     }
350 }
351
352 #[inline]
353 fn hi_lo_to_u128(lo: u64, hi: u64) -> u128 {
354     ((hi as u128) << 64) | (lo as u128)
355 }
356
357 fn try_as_const_integral(v: &Value) -> Option<&ConstantInt> {
358     unsafe { llvm::LLVMIsAConstantInt(v) }
359 }