2018-05-08 08:10:16 -05:00
|
|
|
//! Code that is useful in various codegen modules.
|
2013-01-07 16:16:52 -06:00
|
|
|
|
2020-03-31 11:16:47 -05:00
|
|
|
use crate::consts::{self, const_alloc_to_llvm};
|
|
|
|
pub use crate::context::CodegenCx;
|
2019-12-22 16:42:04 -06:00
|
|
|
use crate::llvm::{self, BasicBlock, Bool, ConstantInt, False, OperandBundleDef, True};
|
2019-02-17 12:58:58 -06:00
|
|
|
use crate::type_::Type;
|
|
|
|
use crate::type_of::LayoutLlvmExt;
|
|
|
|
use crate::value::Value;
|
2018-07-10 05:28:39 -05:00
|
|
|
|
2020-04-27 12:56:11 -05:00
|
|
|
use rustc_ast::Mutability;
|
2018-10-03 09:56:24 -05:00
|
|
|
use rustc_codegen_ssa::mir::place::PlaceRef;
|
2020-03-31 11:16:47 -05:00
|
|
|
use rustc_codegen_ssa::traits::*;
|
|
|
|
use rustc_middle::bug;
|
2020-03-29 09:41:09 -05:00
|
|
|
use rustc_middle::mir::interpret::{Allocation, GlobalAlloc, Scalar};
|
2021-08-30 09:38:27 -05:00
|
|
|
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
|
|
|
|
use rustc_middle::ty::ScalarInt;
|
2020-01-01 12:30:57 -06:00
|
|
|
use rustc_span::symbol::Symbol;
|
2021-08-30 09:38:27 -05:00
|
|
|
use rustc_target::abi::{self, AddressSpace, HasDataLayout, Pointer, Size};
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2020-03-31 11:16:47 -05:00
|
|
|
use libc::{c_char, c_uint};
|
2020-08-05 06:35:53 -05:00
|
|
|
use tracing::debug;
|
2013-06-12 21:02:33 -05:00
|
|
|
|
2011-07-21 19:27:34 -05:00
|
|
|
/*
|
2015-01-06 17:22:24 -06:00
|
|
|
* A note on nomenclature of linking: "extern", "foreign", and "upcall".
|
|
|
|
*
|
|
|
|
* An "extern" is an LLVM symbol we wind up emitting an undefined external
|
|
|
|
* reference to. This means "we don't have the thing in this compilation unit,
|
|
|
|
* please make sure you link it in at runtime". This could be a reference to
|
|
|
|
* C code found in a C library, or rust code found in a rust crate.
|
|
|
|
*
|
|
|
|
* Most "externs" are implicitly declared (automatically) as a result of a
|
|
|
|
* user declaring an extern _module_ dependency; this causes the rust driver
|
|
|
|
* to locate an extern crate, scan its compilation metadata, and emit extern
|
|
|
|
* declarations for any symbols used by the declaring crate.
|
|
|
|
*
|
|
|
|
* A "foreign" is an extern that references C (or other non-rust ABI) code.
|
|
|
|
* There is no metadata to scan for extern references so in these cases either
|
|
|
|
* a header-digester like bindgen, or manual function prototypes, have to
|
|
|
|
* serve as declarators. So these are usually given explicitly as prototype
|
|
|
|
* declarations, in rust code, with ABI attributes on them noting which ABI to
|
|
|
|
* link via.
|
|
|
|
*
|
|
|
|
* An "upcall" is a foreign call generated by the compiler (not corresponding
|
|
|
|
* to any user-written call in the code) into the runtime library, to perform
|
|
|
|
* some helper task such as bringing a task to life, allocating memory, etc.
|
|
|
|
*
|
|
|
|
*/
|
2012-03-22 15:44:20 -05:00
|
|
|
|
2015-10-23 20:18:44 -05:00
|
|
|
/// A structure representing an active landing pad for the duration of a basic
|
|
|
|
/// block.
|
|
|
|
///
|
|
|
|
/// Each `Block` may contain an instance of this, indicating whether the block
|
|
|
|
/// is part of a landing pad or not. This is used to make decision about whether
|
2018-11-26 20:59:49 -06:00
|
|
|
/// to emit `invoke` instructions (e.g., in a landing pad we don't continue to
|
2015-10-23 20:18:44 -05:00
|
|
|
/// use `invoke`) and also about various function call metadata.
|
|
|
|
///
|
|
|
|
/// For GNU exceptions (`landingpad` + `resume` instructions) this structure is
|
|
|
|
/// just a bunch of `None` instances (not too interesting), but for MSVC
|
|
|
|
/// exceptions (`cleanuppad` + `cleanupret` instructions) this contains data.
|
|
|
|
/// When inside of a landing pad, each function call in LLVM IR needs to be
|
|
|
|
/// annotated with which landing pad it's a part of. This is accomplished via
|
|
|
|
/// the `OperandBundleDef` value created for MSVC landing pads.
|
2018-11-13 04:51:42 -06:00
|
|
|
pub struct Funclet<'ll> {
|
|
|
|
cleanuppad: &'ll Value,
|
|
|
|
operand: OperandBundleDef<'ll>,
|
2015-10-23 20:18:44 -05:00
|
|
|
}
|
|
|
|
|
2018-11-13 04:51:42 -06:00
|
|
|
impl Funclet<'ll> {
|
|
|
|
pub fn new(cleanuppad: &'ll Value) -> Self {
|
2019-12-22 16:42:04 -06:00
|
|
|
Funclet { cleanuppad, operand: OperandBundleDef::new("funclet", &[cleanuppad]) }
|
2015-10-23 20:18:44 -05:00
|
|
|
}
|
|
|
|
|
2018-11-13 04:51:42 -06:00
|
|
|
pub fn cleanuppad(&self) -> &'ll Value {
|
2016-12-12 07:48:39 -06:00
|
|
|
self.cleanuppad
|
2015-10-23 20:18:44 -05:00
|
|
|
}
|
2016-05-29 14:01:06 -05:00
|
|
|
|
2018-11-13 04:51:42 -06:00
|
|
|
pub fn bundle(&self) -> &OperandBundleDef<'ll> {
|
2016-12-12 07:48:39 -06:00
|
|
|
&self.operand
|
2016-05-29 14:01:06 -05:00
|
|
|
}
|
2015-10-23 20:18:44 -05:00
|
|
|
}
|
|
|
|
|
2018-09-13 07:58:19 -05:00
|
|
|
impl BackendTypes for CodegenCx<'ll, 'tcx> {
|
2018-08-28 10:03:46 -05:00
|
|
|
type Value = &'ll Value;
|
2020-02-10 14:30:51 -06:00
|
|
|
// FIXME(eddyb) replace this with a `Function` "subclass" of `Value`.
|
2019-10-13 04:28:19 -05:00
|
|
|
type Function = &'ll Value;
|
2019-08-27 04:45:03 -05:00
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
type BasicBlock = &'ll BasicBlock;
|
|
|
|
type Type = &'ll Type;
|
2018-11-13 04:51:42 -06:00
|
|
|
type Funclet = Funclet<'ll>;
|
2018-09-20 08:47:22 -05:00
|
|
|
|
|
|
|
type DIScope = &'ll llvm::debuginfo::DIScope;
|
2020-02-10 14:52:30 -06:00
|
|
|
type DILocation = &'ll llvm::debuginfo::DILocation;
|
2020-01-26 10:50:13 -06:00
|
|
|
type DIVariable = &'ll llvm::debuginfo::DIVariable;
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2011-07-21 19:27:34 -05:00
|
|
|
|
2018-12-02 08:58:40 -06:00
|
|
|
impl CodegenCx<'ll, 'tcx> {
|
|
|
|
pub fn const_array(&self, ty: &'ll Type, elts: &[&'ll Value]) -> &'ll Value {
|
2020-03-20 09:03:11 -05:00
|
|
|
unsafe { llvm::LLVMConstArray(ty, elts.as_ptr(), elts.len() as c_uint) }
|
2018-12-02 08:58:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn const_vector(&self, elts: &[&'ll Value]) -> &'ll Value {
|
2020-03-20 09:03:11 -05:00
|
|
|
unsafe { llvm::LLVMConstVector(elts.as_ptr(), elts.len() as c_uint) }
|
2018-12-02 08:58:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn const_bytes(&self, bytes: &[u8]) -> &'ll Value {
|
|
|
|
bytes_in_context(self.llcx, bytes)
|
|
|
|
}
|
2018-12-02 09:53:39 -06:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
fn const_cstr(&self, s: Symbol, null_terminated: bool) -> &'ll Value {
|
2018-12-02 09:53:39 -06:00
|
|
|
unsafe {
|
|
|
|
if let Some(&llval) = self.const_cstr_cache.borrow().get(&s) {
|
|
|
|
return llval;
|
|
|
|
}
|
|
|
|
|
2019-09-03 01:23:21 -05:00
|
|
|
let s_str = s.as_str();
|
2019-12-22 16:42:04 -06:00
|
|
|
let sc = llvm::LLVMConstStringInContext(
|
|
|
|
self.llcx,
|
|
|
|
s_str.as_ptr() as *const c_char,
|
|
|
|
s_str.len() as c_uint,
|
|
|
|
!null_terminated as Bool,
|
|
|
|
);
|
2018-12-02 09:53:39 -06:00
|
|
|
let sym = self.generate_local_symbol_name("str");
|
2019-12-22 16:42:04 -06:00
|
|
|
let g = self.define_global(&sym[..], self.val_ty(sc)).unwrap_or_else(|| {
|
2018-12-02 09:53:39 -06:00
|
|
|
bug!("symbol `{}` is already defined", sym);
|
|
|
|
});
|
|
|
|
llvm::LLVMSetInitializer(g, sc);
|
|
|
|
llvm::LLVMSetGlobalConstant(g, True);
|
|
|
|
llvm::LLVMRustSetLinkage(g, llvm::Linkage::InternalLinkage);
|
|
|
|
|
|
|
|
self.const_cstr_cache.borrow_mut().insert(s, g);
|
|
|
|
g
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn const_get_elt(&self, v: &'ll Value, idx: u64) -> &'ll Value {
|
|
|
|
unsafe {
|
|
|
|
assert_eq!(idx as c_uint as u64, idx);
|
|
|
|
let us = &[idx as c_uint];
|
|
|
|
let r = llvm::LLVMConstExtractValue(v, us.as_ptr(), us.len() as c_uint);
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
debug!("const_get_elt(v={:?}, idx={}, r={:?})", v, idx, r);
|
2018-12-02 09:53:39 -06:00
|
|
|
|
|
|
|
r
|
|
|
|
}
|
|
|
|
}
|
2018-12-02 08:58:40 -06:00
|
|
|
}
|
|
|
|
|
2018-09-13 07:58:19 -05:00
|
|
|
impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
|
2018-09-06 13:57:42 -05:00
|
|
|
fn const_null(&self, t: &'ll Type) -> &'ll Value {
|
2019-12-22 16:42:04 -06:00
|
|
|
unsafe { llvm::LLVMConstNull(t) }
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
|
2018-09-06 13:57:42 -05:00
|
|
|
fn const_undef(&self, t: &'ll Type) -> &'ll Value {
|
2019-12-22 16:42:04 -06:00
|
|
|
unsafe { llvm::LLVMGetUndef(t) }
|
2017-08-05 04:27:28 -05:00
|
|
|
}
|
|
|
|
|
2018-09-06 13:57:42 -05:00
|
|
|
fn const_int(&self, t: &'ll Type, i: i64) -> &'ll Value {
|
2019-12-22 16:42:04 -06:00
|
|
|
unsafe { llvm::LLVMConstInt(t, i as u64, True) }
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2018-09-06 13:57:42 -05:00
|
|
|
fn const_uint(&self, t: &'ll Type, i: u64) -> &'ll Value {
|
2019-12-22 16:42:04 -06:00
|
|
|
unsafe { llvm::LLVMConstInt(t, i, False) }
|
2016-08-22 19:56:52 -05:00
|
|
|
}
|
|
|
|
|
2018-09-06 13:57:42 -05:00
|
|
|
fn const_uint_big(&self, t: &'ll Type, u: u128) -> &'ll Value {
|
2018-08-28 10:03:46 -05:00
|
|
|
unsafe {
|
|
|
|
let words = [u as u64, (u >> 64) as u64];
|
|
|
|
llvm::LLVMConstIntOfArbitraryPrecision(t, 2, words.as_ptr())
|
|
|
|
}
|
|
|
|
}
|
2013-09-29 04:20:11 -05:00
|
|
|
|
2018-09-06 13:57:42 -05:00
|
|
|
fn const_bool(&self, val: bool) -> &'ll Value {
|
2018-11-07 04:08:41 -06:00
|
|
|
self.const_uint(self.type_i1(), val as u64)
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2014-10-15 12:26:43 -05:00
|
|
|
|
2018-09-06 13:57:42 -05:00
|
|
|
fn const_i32(&self, i: i32) -> &'ll Value {
|
2018-11-07 04:08:41 -06:00
|
|
|
self.const_int(self.type_i32(), i as i64)
|
2018-08-28 10:03:46 -05:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2018-09-06 13:57:42 -05:00
|
|
|
fn const_u32(&self, i: u32) -> &'ll Value {
|
2018-11-07 04:08:41 -06:00
|
|
|
self.const_uint(self.type_i32(), i as u64)
|
2018-08-28 10:03:46 -05:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2018-09-06 13:57:42 -05:00
|
|
|
fn const_u64(&self, i: u64) -> &'ll Value {
|
2018-11-07 04:08:41 -06:00
|
|
|
self.const_uint(self.type_i64(), i)
|
2018-08-28 10:03:46 -05:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2018-09-06 13:57:42 -05:00
|
|
|
fn const_usize(&self, i: u64) -> &'ll Value {
|
2018-08-28 10:03:46 -05:00
|
|
|
let bit_size = self.data_layout().pointer_size.bits();
|
|
|
|
if bit_size < 64 {
|
|
|
|
// make sure it doesn't overflow
|
2019-12-22 16:42:04 -06:00
|
|
|
assert!(i < (1 << bit_size));
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2012-04-21 15:23:25 -05:00
|
|
|
|
2018-11-07 04:08:41 -06:00
|
|
|
self.const_uint(self.isize_ty, i)
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2018-09-06 13:57:42 -05:00
|
|
|
fn const_u8(&self, i: u8) -> &'ll Value {
|
2018-11-07 04:08:41 -06:00
|
|
|
self.const_uint(self.type_i8(), i as u64)
|
2018-08-28 10:03:46 -05:00
|
|
|
}
|
2017-06-25 04:42:55 -05:00
|
|
|
|
2019-07-07 12:08:40 -05:00
|
|
|
fn const_real(&self, t: &'ll Type, val: f64) -> &'ll Value {
|
|
|
|
unsafe { llvm::LLVMConstReal(t, val) }
|
|
|
|
}
|
|
|
|
|
2019-10-09 10:25:41 -05:00
|
|
|
fn const_str(&self, s: Symbol) -> (&'ll Value, &'ll Value) {
|
|
|
|
let len = s.as_str().len();
|
2019-12-22 16:42:04 -06:00
|
|
|
let cs = consts::ptrcast(
|
|
|
|
self.const_cstr(s, false),
|
2020-05-28 06:02:02 -05:00
|
|
|
self.type_ptr_to(self.layout_of(self.tcx.types.str_).llvm_type(self)),
|
2019-12-22 16:42:04 -06:00
|
|
|
);
|
2019-10-09 10:25:41 -05:00
|
|
|
(cs, self.const_usize(len as u64))
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
fn const_struct(&self, elts: &[&'ll Value], packed: bool) -> &'ll Value {
|
2018-11-07 04:08:41 -06:00
|
|
|
struct_in_context(self.llcx, elts, packed)
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2012-09-05 17:27:22 -05:00
|
|
|
|
2019-08-27 04:51:53 -05:00
|
|
|
fn const_to_opt_uint(&self, v: &'ll Value) -> Option<u64> {
|
2019-12-22 16:42:04 -06:00
|
|
|
try_as_const_integral(v).map(|v| unsafe { llvm::LLVMConstIntGetZExtValue(v) })
|
2018-08-28 10:03:46 -05:00
|
|
|
}
|
2018-03-15 10:36:02 -05:00
|
|
|
|
2018-08-30 07:58:15 -05:00
|
|
|
fn const_to_opt_u128(&self, v: &'ll Value, sign_ext: bool) -> Option<u128> {
|
2019-10-13 05:19:14 -05:00
|
|
|
try_as_const_integral(v).and_then(|v| unsafe {
|
|
|
|
let (mut lo, mut hi) = (0u64, 0u64);
|
2019-12-22 16:42:04 -06:00
|
|
|
let success = llvm::LLVMRustConstInt128Get(v, sign_ext, &mut hi, &mut lo);
|
2019-12-06 06:18:32 -06:00
|
|
|
success.then_some(hi_lo_to_u128(lo, hi))
|
2019-10-13 05:19:14 -05:00
|
|
|
})
|
2015-03-26 19:37:10 -05:00
|
|
|
}
|
2018-09-20 08:47:22 -05:00
|
|
|
|
2020-03-31 11:16:47 -05:00
|
|
|
fn scalar_to_backend(&self, cv: Scalar, layout: &abi::Scalar, llty: &'ll Type) -> &'ll Value {
|
2018-09-20 08:47:22 -05:00
|
|
|
let bitsize = if layout.is_bool() { 1 } else { layout.value.size(self).bits() };
|
|
|
|
match cv {
|
2020-11-01 10:57:03 -06:00
|
|
|
Scalar::Int(ScalarInt::ZST) => {
|
2018-09-20 08:47:22 -05:00
|
|
|
assert_eq!(0, layout.value.size(self).bytes());
|
|
|
|
self.const_undef(self.type_ix(0))
|
2019-12-22 16:42:04 -06:00
|
|
|
}
|
2020-11-01 10:57:03 -06:00
|
|
|
Scalar::Int(int) => {
|
2020-09-26 08:15:35 -05:00
|
|
|
let data = int.assert_bits(layout.value.size(self));
|
2019-05-25 03:59:09 -05:00
|
|
|
let llval = self.const_uint_big(self.type_ix(bitsize), data);
|
2020-03-31 11:16:47 -05:00
|
|
|
if layout.value == Pointer {
|
2018-09-20 08:47:22 -05:00
|
|
|
unsafe { llvm::LLVMConstIntToPtr(llval, llty) }
|
|
|
|
} else {
|
2018-11-24 10:45:05 -06:00
|
|
|
self.const_bitcast(llval, llty)
|
2018-09-20 08:47:22 -05:00
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
}
|
2021-07-12 13:29:05 -05:00
|
|
|
Scalar::Ptr(ptr, _size) => {
|
2021-07-12 11:22:15 -05:00
|
|
|
let (alloc_id, offset) = ptr.into_parts();
|
|
|
|
let (base_addr, base_addr_space) = match self.tcx.global_alloc(alloc_id) {
|
2020-05-08 03:58:53 -05:00
|
|
|
GlobalAlloc::Memory(alloc) => {
|
2018-09-20 08:47:22 -05:00
|
|
|
let init = const_alloc_to_llvm(self, alloc);
|
2020-03-12 08:53:49 -05:00
|
|
|
let value = match alloc.mutability {
|
|
|
|
Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None),
|
|
|
|
_ => self.static_addr_of(init, alloc.align, None),
|
|
|
|
};
|
|
|
|
if !self.sess().fewer_names() {
|
2021-07-12 11:22:15 -05:00
|
|
|
llvm::set_value_name(value, format!("{:?}", alloc_id).as_bytes());
|
2018-09-20 08:47:22 -05:00
|
|
|
}
|
2020-06-11 00:52:09 -05:00
|
|
|
(value, AddressSpace::DATA)
|
2018-09-20 08:47:22 -05:00
|
|
|
}
|
2020-06-11 00:52:09 -05:00
|
|
|
GlobalAlloc::Function(fn_instance) => (
|
2020-07-22 05:50:26 -05:00
|
|
|
self.get_fn_addr(fn_instance.polymorphize(self.tcx)),
|
2020-06-11 00:52:09 -05:00
|
|
|
self.data_layout().instruction_address_space,
|
|
|
|
),
|
2020-05-08 03:58:53 -05:00
|
|
|
GlobalAlloc::Static(def_id) => {
|
2019-04-21 06:41:51 -05:00
|
|
|
assert!(self.tcx.is_static(def_id));
|
2020-05-02 14:44:25 -05:00
|
|
|
assert!(!self.tcx.is_thread_local_static(def_id));
|
2020-06-11 00:52:09 -05:00
|
|
|
(self.get_static(def_id), AddressSpace::DATA)
|
2018-09-20 08:47:22 -05:00
|
|
|
}
|
|
|
|
};
|
2019-12-22 16:42:04 -06:00
|
|
|
let llval = unsafe {
|
2021-07-30 19:00:00 -05:00
|
|
|
llvm::LLVMRustConstInBoundsGEP2(
|
|
|
|
self.type_i8(),
|
2020-06-11 00:52:09 -05:00
|
|
|
self.const_bitcast(base_addr, self.type_i8p_ext(base_addr_space)),
|
2021-07-12 11:22:15 -05:00
|
|
|
&self.const_usize(offset.bytes()),
|
2019-12-22 16:42:04 -06:00
|
|
|
1,
|
|
|
|
)
|
|
|
|
};
|
2020-03-31 11:16:47 -05:00
|
|
|
if layout.value != Pointer {
|
2018-09-20 08:47:22 -05:00
|
|
|
unsafe { llvm::LLVMConstPtrToInt(llval, llty) }
|
|
|
|
} else {
|
2018-11-24 10:45:05 -06:00
|
|
|
self.const_bitcast(llval, llty)
|
2018-09-20 08:47:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-20 04:43:25 -05:00
|
|
|
fn const_data_from_alloc(&self, alloc: &Allocation) -> Self::Value {
|
|
|
|
const_alloc_to_llvm(self, alloc)
|
|
|
|
}
|
|
|
|
|
2018-09-20 08:47:22 -05:00
|
|
|
fn from_const_alloc(
|
|
|
|
&self,
|
2020-03-04 08:50:21 -06:00
|
|
|
layout: TyAndLayout<'tcx>,
|
2018-09-20 08:47:22 -05:00
|
|
|
alloc: &Allocation,
|
|
|
|
offset: Size,
|
|
|
|
) -> PlaceRef<'tcx, &'ll Value> {
|
2019-07-28 11:17:58 -05:00
|
|
|
assert_eq!(alloc.align, layout.align.abi);
|
2019-08-17 04:29:17 -05:00
|
|
|
let llty = self.type_ptr_to(layout.llvm_type(self));
|
2019-08-16 09:05:36 -05:00
|
|
|
let llval = if layout.size == Size::ZERO {
|
|
|
|
let llval = self.const_usize(alloc.align.bytes());
|
2019-08-17 04:29:17 -05:00
|
|
|
unsafe { llvm::LLVMConstIntToPtr(llval, llty) }
|
2019-08-16 09:05:36 -05:00
|
|
|
} else {
|
|
|
|
let init = const_alloc_to_llvm(self, alloc);
|
|
|
|
let base_addr = self.static_addr_of(init, alloc.align, None);
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
let llval = unsafe {
|
2021-07-30 19:00:00 -05:00
|
|
|
llvm::LLVMRustConstInBoundsGEP2(
|
|
|
|
self.type_i8(),
|
2019-12-22 16:42:04 -06:00
|
|
|
self.const_bitcast(base_addr, self.type_i8p()),
|
|
|
|
&self.const_usize(offset.bytes()),
|
|
|
|
1,
|
|
|
|
)
|
|
|
|
};
|
2019-08-17 04:29:17 -05:00
|
|
|
self.const_bitcast(llval, llty)
|
2019-08-16 09:05:36 -05:00
|
|
|
};
|
2019-08-29 13:24:50 -05:00
|
|
|
PlaceRef::new_sized(llval, layout)
|
2018-09-20 08:47:22 -05:00
|
|
|
}
|
2018-11-24 10:23:22 -06:00
|
|
|
|
|
|
|
fn const_ptrcast(&self, val: &'ll Value, ty: &'ll Type) -> &'ll Value {
|
|
|
|
consts::ptrcast(val, ty)
|
|
|
|
}
|
2015-03-26 19:37:10 -05:00
|
|
|
}
|
|
|
|
|
2020-12-22 21:37:23 -06:00
|
|
|
/// Get the [LLVM type][Type] of a [`Value`].
|
2020-03-06 17:56:32 -06:00
|
|
|
pub fn val_ty(v: &Value) -> &Type {
|
2019-12-22 16:42:04 -06:00
|
|
|
unsafe { llvm::LLVMTypeOf(v) }
|
2018-08-30 07:24:41 -05:00
|
|
|
}
|
|
|
|
|
2018-09-06 16:44:51 -05:00
|
|
|
pub fn bytes_in_context(llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
|
2018-08-30 07:24:41 -05:00
|
|
|
unsafe {
|
|
|
|
let ptr = bytes.as_ptr() as *const c_char;
|
2020-03-20 09:03:11 -05:00
|
|
|
llvm::LLVMConstStringInContext(llcx, ptr, bytes.len() as c_uint, True)
|
2018-08-30 07:24:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
pub fn struct_in_context(llcx: &'a llvm::Context, elts: &[&'a Value], packed: bool) -> &'a Value {
|
2018-08-30 07:24:41 -05:00
|
|
|
unsafe {
|
2019-12-22 16:42:04 -06:00
|
|
|
llvm::LLVMConstStructInContext(llcx, elts.as_ptr(), elts.len() as c_uint, packed as Bool)
|
2018-08-30 07:24:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
#[inline]
|
|
|
|
fn hi_lo_to_u128(lo: u64, hi: u64) -> u128 {
|
|
|
|
((hi as u128) << 64) | (lo as u128)
|
|
|
|
}
|
2019-08-27 04:51:53 -05:00
|
|
|
|
2020-03-06 17:56:32 -06:00
|
|
|
fn try_as_const_integral(v: &Value) -> Option<&ConstantInt> {
|
2019-12-22 16:42:04 -06:00
|
|
|
unsafe { llvm::LLVMIsAConstantInt(v) }
|
2019-08-27 04:51:53 -05:00
|
|
|
}
|