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