2014-02-10 08:36:31 -06:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-07-18 07:45:17 -05:00
|
|
|
#![allow(non_camel_case_types, non_snake_case)]
|
2014-02-10 08:36:31 -06:00
|
|
|
|
2018-05-08 08:10:16 -05:00
|
|
|
//! Code that is useful in various codegen modules.
|
2013-01-07 16:16:52 -06:00
|
|
|
|
2018-07-10 05:28:39 -05:00
|
|
|
use llvm::{self, TypeKind};
|
2018-08-28 10:03:46 -05:00
|
|
|
use llvm::{True, False, Bool, BasicBlock};
|
2016-03-29 04:54:26 -05:00
|
|
|
use rustc::hir::def_id::DefId;
|
2017-05-27 13:48:09 -05:00
|
|
|
use rustc::middle::lang_items::LangItem;
|
2017-06-25 04:42:55 -05:00
|
|
|
use abi;
|
2016-03-22 12:23:36 -05:00
|
|
|
use base;
|
|
|
|
use builder::Builder;
|
|
|
|
use consts;
|
|
|
|
use declare;
|
|
|
|
use type_::Type;
|
2017-09-20 15:07:47 -05:00
|
|
|
use type_of::LayoutLlvmExt;
|
2018-08-22 10:48:32 -05:00
|
|
|
use value::Value;
|
2018-08-29 09:40:47 -05:00
|
|
|
use interfaces::{Backend, CommonMethods, CommonWriteMethods};
|
2018-07-10 05:28:39 -05:00
|
|
|
|
2016-03-22 10:30:57 -05:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt};
|
2017-10-04 20:22:23 -05:00
|
|
|
use rustc::ty::layout::{HasDataLayout, LayoutOf};
|
2016-03-29 00:50:44 -05:00
|
|
|
use rustc::hir;
|
2018-08-28 04:11:01 -05:00
|
|
|
use interfaces::BuilderMethods;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2014-10-14 15:36:11 -05:00
|
|
|
use libc::{c_uint, c_char};
|
2016-02-23 13:57:22 -06:00
|
|
|
|
2018-04-11 16:02:41 -05:00
|
|
|
use syntax::symbol::LocalInternedString;
|
2017-09-12 10:28:17 -05:00
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2018-01-04 23:01:54 -06:00
|
|
|
pub use context::CodegenCx;
|
2013-06-12 21:02:33 -05:00
|
|
|
|
2017-09-12 10:28:17 -05:00
|
|
|
pub fn type_needs_drop<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
|
2018-02-10 12:18:02 -06:00
|
|
|
ty.needs_drop(tcx, ty::ParamEnv::reveal_all())
|
2017-09-12 10:28:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn type_is_sized<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
|
2018-02-10 12:18:02 -06:00
|
|
|
ty.is_sized(tcx.at(DUMMY_SP), ty::ParamEnv::reveal_all())
|
2017-09-12 10:28:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn type_is_freeze<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
|
2018-02-10 12:18:02 -06:00
|
|
|
ty.is_freeze(tcx, ty::ParamEnv::reveal_all(), DUMMY_SP)
|
2017-09-12 10:28:17 -05:00
|
|
|
}
|
|
|
|
|
2018-08-28 04:11:01 -05:00
|
|
|
pub struct OperandBundleDef<'a, Value : 'a> {
|
|
|
|
pub name: &'a str,
|
|
|
|
pub val: Value
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OperandBundleDef<'ll, &'ll Value> {
|
|
|
|
pub fn new(name: &'ll str, val: &'ll Value) -> Self {
|
|
|
|
OperandBundleDef {
|
|
|
|
name,
|
|
|
|
val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum IntPredicate {
|
|
|
|
IntEQ,
|
|
|
|
IntNE,
|
|
|
|
IntUGT,
|
|
|
|
IntUGE,
|
|
|
|
IntULT,
|
|
|
|
IntULE,
|
|
|
|
IntSGT,
|
|
|
|
IntSGE,
|
|
|
|
IntSLT,
|
|
|
|
IntSLE
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub enum RealPredicate {
|
|
|
|
RealPredicateFalse,
|
|
|
|
RealOEQ,
|
|
|
|
RealOGT,
|
|
|
|
RealOGE,
|
|
|
|
RealOLT,
|
|
|
|
RealOLE,
|
|
|
|
RealONE,
|
|
|
|
RealORD,
|
|
|
|
RealUNO,
|
|
|
|
RealUEQ,
|
|
|
|
RealUGT,
|
|
|
|
RealUGE,
|
|
|
|
RealULT,
|
|
|
|
RealULE,
|
|
|
|
RealUNE,
|
|
|
|
RealPredicateTrue
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum AtomicRmwBinOp {
|
|
|
|
AtomicXchg,
|
|
|
|
AtomicAdd,
|
|
|
|
AtomicSub,
|
|
|
|
AtomicAnd,
|
|
|
|
AtomicNand,
|
|
|
|
AtomicOr,
|
|
|
|
AtomicXor,
|
|
|
|
AtomicMax,
|
|
|
|
AtomicMin,
|
|
|
|
AtomicUMax,
|
|
|
|
AtomicUMin
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum AtomicOrdering {
|
|
|
|
#[allow(dead_code)]
|
|
|
|
NotAtomic,
|
|
|
|
Unordered,
|
|
|
|
Monotonic,
|
|
|
|
// Consume, // Not specified yet.
|
|
|
|
Acquire,
|
|
|
|
Release,
|
|
|
|
AcquireRelease,
|
|
|
|
SequentiallyConsistent,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum SynchronizationScope {
|
|
|
|
// FIXME: figure out if this variant is needed at all.
|
|
|
|
#[allow(dead_code)]
|
|
|
|
Other,
|
|
|
|
SingleThread,
|
|
|
|
CrossThread,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
/// to emit `invoke` instructions (e.g. in a landing pad we don't continue to
|
|
|
|
/// 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-07-10 05:28:39 -05:00
|
|
|
pub struct Funclet<'ll> {
|
|
|
|
cleanuppad: &'ll Value,
|
2018-08-21 10:39:43 -05:00
|
|
|
operand: OperandBundleDef<'ll, &'ll Value>,
|
2015-10-23 20:18:44 -05:00
|
|
|
}
|
|
|
|
|
2018-07-10 05:28:39 -05:00
|
|
|
impl Funclet<'ll> {
|
|
|
|
pub fn new(cleanuppad: &'ll Value) -> Self {
|
2016-12-19 15:10:48 -06:00
|
|
|
Funclet {
|
2017-08-07 00:54:09 -05:00
|
|
|
cleanuppad,
|
2018-08-21 10:39:43 -05:00
|
|
|
operand: OperandBundleDef::new("funclet", cleanuppad),
|
2016-12-19 15:10:48 -06:00
|
|
|
}
|
2015-10-23 20:18:44 -05:00
|
|
|
}
|
|
|
|
|
2018-07-10 05:28:39 -05: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-08-21 10:39:43 -05:00
|
|
|
pub fn bundle(&self) -> &OperandBundleDef<'ll, &'ll Value> {
|
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-08-28 10:50:57 -05:00
|
|
|
impl Backend for CodegenCx<'ll, 'tcx> {
|
2018-08-28 10:03:46 -05:00
|
|
|
type Value = &'ll Value;
|
|
|
|
type BasicBlock = &'ll BasicBlock;
|
|
|
|
type Type = &'ll Type;
|
|
|
|
type Context = &'ll llvm::Context;
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2011-07-21 19:27:34 -05:00
|
|
|
|
2018-08-28 10:50:57 -05:00
|
|
|
impl<'ll, 'tcx : 'll> CommonMethods for CodegenCx<'ll, 'tcx> {
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
// LLVM constant constructors.
|
2018-08-29 08:56:30 -05:00
|
|
|
fn c_null(&self, t: &'ll Type) -> &'ll Value {
|
2018-08-28 10:03:46 -05:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMConstNull(t)
|
|
|
|
}
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
|
2018-08-29 08:56:30 -05:00
|
|
|
fn c_undef(&self, t: &'ll Type) -> &'ll Value {
|
2018-08-28 10:03:46 -05:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMGetUndef(t)
|
|
|
|
}
|
2017-08-05 04:27:28 -05:00
|
|
|
}
|
|
|
|
|
2018-08-29 08:56:30 -05:00
|
|
|
fn c_int(&self, t: &'ll Type, i: i64) -> &'ll Value {
|
2018-08-28 10:03:46 -05: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-08-29 08:56:30 -05:00
|
|
|
fn c_uint(&self, t: &'ll Type, i: u64) -> &'ll Value {
|
2018-08-28 10:03:46 -05:00
|
|
|
unsafe {
|
|
|
|
llvm::LLVMConstInt(t, i, False)
|
|
|
|
}
|
2016-08-22 19:56:52 -05:00
|
|
|
}
|
|
|
|
|
2018-08-29 08:56:30 -05:00
|
|
|
fn c_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-08-28 10:03:46 -05:00
|
|
|
fn c_bool(&self, val: bool) -> &'ll Value {
|
2018-08-29 08:56:30 -05:00
|
|
|
&self.c_uint(Type::i1(&self), val as u64)
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2014-10-15 12:26:43 -05:00
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
fn c_i32(&self, i: i32) -> &'ll Value {
|
2018-08-29 08:56:30 -05:00
|
|
|
&self.c_int(Type::i32(&self), i as i64)
|
2018-08-28 10:03:46 -05:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
fn c_u32(&self, i: u32) -> &'ll Value {
|
2018-08-29 08:56:30 -05:00
|
|
|
&self.c_uint(Type::i32(&self), i as u64)
|
2018-08-28 10:03:46 -05:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
fn c_u64(&self, i: u64) -> &'ll Value {
|
2018-08-29 08:56:30 -05:00
|
|
|
&self.c_uint(Type::i64(&self), i)
|
2018-08-28 10:03:46 -05:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
fn c_usize(&self, i: u64) -> &'ll Value {
|
|
|
|
let bit_size = self.data_layout().pointer_size.bits();
|
|
|
|
if bit_size < 64 {
|
|
|
|
// make sure it doesn't overflow
|
|
|
|
assert!(i < (1<<bit_size));
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2012-04-21 15:23:25 -05:00
|
|
|
|
2018-08-29 08:56:30 -05:00
|
|
|
&self.c_uint(&self.isize_ty, i)
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2011-07-14 19:08:22 -05:00
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
fn c_u8(&self, i: u8) -> &'ll Value {
|
2018-08-29 08:56:30 -05:00
|
|
|
&self.c_uint(Type::i8(&self), i as u64)
|
2018-08-28 10:03:46 -05:00
|
|
|
}
|
2017-06-25 04:42:55 -05:00
|
|
|
|
2012-06-08 15:26:06 -05:00
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
// This is a 'c-like' raw string, which differs from
|
|
|
|
// our boxed-and-length-annotated strings.
|
|
|
|
fn c_cstr(
|
|
|
|
&self,
|
|
|
|
s: LocalInternedString,
|
|
|
|
null_terminated: bool,
|
|
|
|
) -> &'ll Value {
|
|
|
|
unsafe {
|
|
|
|
if let Some(&llval) = &self.const_cstr_cache.borrow().get(&s) {
|
|
|
|
return llval;
|
|
|
|
}
|
run optimization and codegen on worker threads
Refactor the code in `llvm::back` that invokes LLVM optimization and codegen
passes so that it can be called from worker threads. (Previously, it used
`&Session` extensively, and `Session` is not `Share`.) The new code can handle
multiple compilation units, by compiling each unit to `crate.0.o`, `crate.1.o`,
etc., and linking together all the `crate.N.o` files into a single `crate.o`
using `ld -r`. The later linking steps can then be run unchanged.
The new code preserves the behavior of `--emit`/`-o` when building a single
compilation unit. With multiple compilation units, the `--emit=asm/ir/bc`
options produce multiple files, so combinations like `--emit=ir -o foo.ll` will
not actually produce `foo.ll` (they instead produce several `foo.N.ll` files).
The new code supports `-Z lto` only when using a single compilation unit.
Compiling with multiple compilation units and `-Z lto` will produce an error.
(I can't think of any good reason to do such a thing.) Linking with `-Z lto`
against a library that was built as multiple compilation units will also fail,
because the rlib does not contain a `crate.bytecode.deflate` file. This could
be supported in the future by linking together the `crate.N.bc` files produced
when compiling the library into a single `crate.bc`, or by making the LTO code
support multiple `crate.N.bytecode.deflate` files.
2014-07-17 12:52:52 -05:00
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
let sc = llvm::LLVMConstStringInContext(&self.llcx,
|
|
|
|
s.as_ptr() as *const c_char,
|
|
|
|
s.len() as c_uint,
|
|
|
|
!null_terminated as Bool);
|
|
|
|
let sym = &self.generate_local_symbol_name("str");
|
2018-08-29 11:42:25 -05:00
|
|
|
let g = declare::define_global(&self, &sym[..], &self.val_ty(sc)).unwrap_or_else(||{
|
2018-08-28 10:03:46 -05: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
|
|
|
|
}
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2013-01-23 18:25:47 -06:00
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
// NB: Do not use `do_spill_noroot` to make this into a constant string, or
|
|
|
|
// you will be kicked off fast isel. See issue #4352 for an example of this.
|
|
|
|
fn c_str_slice(&self, s: LocalInternedString) -> &'ll Value {
|
|
|
|
let len = s.len();
|
|
|
|
let cs = consts::ptrcast(&self.c_cstr(s, false),
|
|
|
|
&self.layout_of(&self.tcx.mk_str()).llvm_type(&self).ptr_to());
|
|
|
|
&self.c_fat_ptr(cs, &self.c_usize(len as u64))
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2011-08-04 12:46:10 -05:00
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
fn c_fat_ptr(
|
|
|
|
&self,
|
|
|
|
ptr: &'ll Value,
|
|
|
|
meta: &'ll Value
|
|
|
|
) -> &'ll Value {
|
|
|
|
assert_eq!(abi::FAT_PTR_ADDR, 0);
|
|
|
|
assert_eq!(abi::FAT_PTR_EXTRA, 1);
|
|
|
|
&self.c_struct(&[ptr, meta], false)
|
2015-01-29 06:03:34 -06:00
|
|
|
}
|
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
fn c_struct(
|
|
|
|
&self,
|
|
|
|
elts: &[&'ll Value],
|
|
|
|
packed: bool
|
|
|
|
) -> &'ll Value {
|
2018-08-29 11:42:25 -05:00
|
|
|
&self.c_struct_in_context(&self.llcx, elts, packed)
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2012-09-05 17:27:22 -05:00
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
fn c_array(ty: &'ll Type, elts: &[&'ll Value]) -> &'ll Value {
|
|
|
|
unsafe {
|
|
|
|
return llvm::LLVMConstArray(ty, elts.as_ptr(), elts.len() as c_uint);
|
|
|
|
}
|
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
fn c_vector(elts: &[&'ll Value]) -> &'ll Value {
|
|
|
|
unsafe {
|
|
|
|
return llvm::LLVMConstVector(elts.as_ptr(), elts.len() as c_uint);
|
|
|
|
}
|
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
fn c_bytes(&self, bytes: &[u8]) -> &'ll Value {
|
2018-08-29 11:42:25 -05:00
|
|
|
&self.c_bytes_in_context(&self.llcx, bytes)
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
fn const_get_elt(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);
|
|
|
|
|
|
|
|
debug!("const_get_elt(v={:?}, idx={}, r={:?})",
|
|
|
|
v, idx, r);
|
|
|
|
|
|
|
|
r
|
|
|
|
}
|
2015-01-08 06:14:07 -06:00
|
|
|
}
|
2013-02-18 16:16:21 -06:00
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
fn const_get_real(v: &'ll Value) -> Option<(f64, bool)> {
|
|
|
|
unsafe {
|
|
|
|
if Self::is_const_real(v) {
|
|
|
|
let mut loses_info: llvm::Bool = ::std::mem::uninitialized();
|
|
|
|
let r = llvm::LLVMConstRealGetDouble(v, &mut loses_info);
|
|
|
|
let loses_info = if loses_info == 1 { true } else { false };
|
|
|
|
Some((r, loses_info))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2015-03-26 19:37:10 -05:00
|
|
|
}
|
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
fn const_to_uint(v: &'ll Value) -> u64 {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMConstIntGetZExtValue(v)
|
|
|
|
}
|
2018-03-15 10:36:02 -05:00
|
|
|
}
|
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
fn is_const_integral(v: &'ll Value) -> bool {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMIsAConstantInt(v).is_some()
|
|
|
|
}
|
|
|
|
}
|
2018-03-15 10:36:02 -05:00
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
fn is_const_real(v: &'ll Value) -> bool {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMIsAConstantFP(v).is_some()
|
|
|
|
}
|
|
|
|
}
|
2016-11-19 22:06:53 -06:00
|
|
|
|
2018-08-28 10:03:46 -05:00
|
|
|
fn const_to_opt_u128(v: &'ll Value, sign_ext: bool) -> Option<u128> {
|
|
|
|
unsafe {
|
|
|
|
if Self::is_const_integral(v) {
|
|
|
|
let (mut lo, mut hi) = (0u64, 0u64);
|
|
|
|
let success = llvm::LLVMRustConstInt128Get(v, sign_ext,
|
|
|
|
&mut hi, &mut lo);
|
|
|
|
if success {
|
|
|
|
Some(hi_lo_to_u128(lo, hi))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2016-08-25 17:32:46 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2015-03-26 19:37:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 11:42:25 -05:00
|
|
|
impl<'ll, 'tcx : 'll> CommonWriteMethods for CodegenCx<'ll, 'tcx> {
|
|
|
|
fn val_ty(&self, v: &'ll Value) -> &'ll Type {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMTypeOf(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn c_bytes_in_context(&self, llcx: &'ll llvm::Context, bytes: &[u8]) -> &'ll Value {
|
|
|
|
unsafe {
|
|
|
|
let ptr = bytes.as_ptr() as *const c_char;
|
|
|
|
return llvm::LLVMConstStringInContext(llcx, ptr, bytes.len() as c_uint, True);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn c_struct_in_context(
|
|
|
|
&self,
|
|
|
|
llcx: &'a llvm::Context,
|
|
|
|
elts: &[&'a Value],
|
|
|
|
packed: bool,
|
|
|
|
) -> &'a Value {
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMConstStructInContext(llcx,
|
|
|
|
elts.as_ptr(), elts.len() as c_uint,
|
|
|
|
packed as Bool)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-05-25 00:39:32 -05:00
|
|
|
pub fn langcall(tcx: TyCtxt,
|
2014-01-07 10:54:58 -06:00
|
|
|
span: Option<Span>,
|
|
|
|
msg: &str,
|
|
|
|
li: LangItem)
|
2015-08-16 05:32:28 -05:00
|
|
|
-> DefId {
|
2018-10-08 09:58:26 -05:00
|
|
|
tcx.lang_items().require(li).unwrap_or_else(|s| {
|
|
|
|
let msg = format!("{} {}", msg, s);
|
|
|
|
match span {
|
|
|
|
Some(span) => tcx.sess.span_fatal(span, &msg[..]),
|
|
|
|
None => tcx.sess.fatal(&msg[..]),
|
2013-07-15 22:42:13 -05:00
|
|
|
}
|
2018-10-08 09:58:26 -05:00
|
|
|
})
|
2013-07-15 22:42:13 -05:00
|
|
|
}
|
2015-08-02 14:52:50 -05:00
|
|
|
|
2015-10-21 16:35:15 -05:00
|
|
|
// To avoid UB from LLVM, these two functions mask RHS with an
|
|
|
|
// appropriate mask unconditionally (i.e. the fallback behavior for
|
|
|
|
// all shifts). For 32- and 64-bit types, this matches the semantics
|
|
|
|
// of Java. (See related discussion on #1877 and #10183.)
|
|
|
|
|
2018-07-02 09:52:53 -05:00
|
|
|
pub fn build_unchecked_lshift(
|
|
|
|
bx: &Builder<'a, 'll, 'tcx>,
|
2018-07-10 05:28:39 -05:00
|
|
|
lhs: &'ll Value,
|
|
|
|
rhs: &'ll Value
|
|
|
|
) -> &'ll Value {
|
2018-07-11 05:44:53 -05:00
|
|
|
let rhs = base::cast_shift_expr_rhs(bx, hir::BinOpKind::Shl, lhs, rhs);
|
2015-10-21 16:35:15 -05:00
|
|
|
// #1877, #10183: Ensure that input is always valid
|
2018-01-04 23:12:32 -06:00
|
|
|
let rhs = shift_mask_rhs(bx, rhs);
|
|
|
|
bx.shl(lhs, rhs)
|
2015-10-21 16:35:15 -05:00
|
|
|
}
|
|
|
|
|
2018-07-02 09:52:53 -05:00
|
|
|
pub fn build_unchecked_rshift(
|
2018-07-10 05:28:39 -05:00
|
|
|
bx: &Builder<'a, 'll, 'tcx>, lhs_t: Ty<'tcx>, lhs: &'ll Value, rhs: &'ll Value
|
|
|
|
) -> &'ll Value {
|
2018-07-11 05:44:53 -05:00
|
|
|
let rhs = base::cast_shift_expr_rhs(bx, hir::BinOpKind::Shr, lhs, rhs);
|
2015-10-21 16:35:15 -05:00
|
|
|
// #1877, #10183: Ensure that input is always valid
|
2018-01-04 23:12:32 -06:00
|
|
|
let rhs = shift_mask_rhs(bx, rhs);
|
2015-10-21 16:35:15 -05:00
|
|
|
let is_signed = lhs_t.is_signed();
|
|
|
|
if is_signed {
|
2018-01-04 23:12:32 -06:00
|
|
|
bx.ashr(lhs, rhs)
|
2015-10-21 16:35:15 -05:00
|
|
|
} else {
|
2018-01-04 23:12:32 -06:00
|
|
|
bx.lshr(lhs, rhs)
|
2015-10-21 16:35:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 05:28:39 -05:00
|
|
|
fn shift_mask_rhs(bx: &Builder<'a, 'll, 'tcx>, rhs: &'ll Value) -> &'ll Value {
|
2018-08-29 11:42:25 -05:00
|
|
|
let rhs_llty = bx.cx().val_ty(rhs);
|
2018-01-04 23:12:32 -06:00
|
|
|
bx.and(rhs, shift_mask_val(bx, rhs_llty, rhs_llty, false))
|
2015-10-21 16:35:15 -05:00
|
|
|
}
|
|
|
|
|
2018-07-02 09:52:53 -05:00
|
|
|
pub fn shift_mask_val(
|
|
|
|
bx: &Builder<'a, 'll, 'tcx>,
|
|
|
|
llty: &'ll Type,
|
|
|
|
mask_llty: &'ll Type,
|
2016-12-10 21:32:44 -06:00
|
|
|
invert: bool
|
2018-07-10 05:28:39 -05:00
|
|
|
) -> &'ll Value {
|
2015-10-21 16:35:15 -05:00
|
|
|
let kind = llty.kind();
|
|
|
|
match kind {
|
|
|
|
TypeKind::Integer => {
|
|
|
|
// i8/u8 can shift by at most 7, i16/u16 by at most 15, etc.
|
|
|
|
let val = llty.int_width() - 1;
|
|
|
|
if invert {
|
2018-08-29 08:56:30 -05:00
|
|
|
bx.cx.c_int(mask_llty, !val as i64)
|
2015-10-21 16:35:15 -05:00
|
|
|
} else {
|
2018-08-29 08:56:30 -05:00
|
|
|
bx.cx.c_uint(mask_llty, val)
|
2015-10-21 16:35:15 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
TypeKind::Vector => {
|
2018-01-04 23:12:32 -06:00
|
|
|
let mask = shift_mask_val(bx, llty.element_type(), mask_llty.element_type(), invert);
|
|
|
|
bx.vector_splat(mask_llty.vector_length(), mask)
|
2015-10-21 16:35:15 -05:00
|
|
|
},
|
2016-03-28 18:46:02 -05:00
|
|
|
_ => bug!("shift_mask_val: expected Integer or Vector, found {:?}", kind),
|
2015-10-21 16:35:15 -05:00
|
|
|
}
|
|
|
|
}
|