Skip to content

Commit

Permalink
Improve type safety
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Oct 13, 2019
1 parent ea60335 commit f0e2fc7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
34 changes: 13 additions & 21 deletions src/librustc_codegen_llvm/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//! Code that is useful in various codegen modules.
use crate::llvm::{self, True, False, Bool, BasicBlock, OperandBundleDef};
use crate::llvm::{self, True, False, Bool, BasicBlock, OperandBundleDef, ConstantInt};
use crate::abi;
use crate::consts;
use crate::type_::Type;
Expand Down Expand Up @@ -246,30 +246,22 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
}

fn const_to_opt_uint(&self, v: &'ll Value) -> Option<u64> {
if is_const_integral(v) {
unsafe {
Some(llvm::LLVMConstIntGetZExtValue(v))
}
} else {
None
}
try_as_const_integral(v).map(|v| unsafe {
llvm::LLVMConstIntGetZExtValue(v)
})
}

fn const_to_opt_u128(&self, v: &'ll Value, sign_ext: bool) -> Option<u128> {
unsafe {
if 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
}
try_as_const_integral(v).and_then(|v| unsafe {
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
}
}
})
}

fn scalar_to_backend(
Expand Down Expand Up @@ -387,8 +379,8 @@ fn hi_lo_to_u128(lo: u64, hi: u64) -> u128 {
((hi as u128) << 64) | (lo as u128)
}

fn is_const_integral(v: &'ll Value) -> bool {
fn try_as_const_integral(v: &'ll Value) -> Option<&'ll ConstantInt> {
unsafe {
llvm::LLVMIsAConstantInt(v).is_some()
llvm::LLVMIsAConstantInt(v)
}
}
7 changes: 4 additions & 3 deletions src/librustc_codegen_llvm/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ extern { pub type Module; }
extern { pub type Context; }
extern { pub type Type; }
extern { pub type Value; }
extern { pub type ConstantInt; }
extern { pub type Metadata; }
extern { pub type BasicBlock; }
#[repr(C)]
Expand Down Expand Up @@ -719,8 +720,8 @@ extern "C" {
pub fn LLVMConstInt(IntTy: &Type, N: c_ulonglong, SignExtend: Bool) -> &Value;
pub fn LLVMConstIntOfArbitraryPrecision(IntTy: &Type, Wn: c_uint, Ws: *const u64) -> &Value;
pub fn LLVMConstReal(RealTy: &Type, N: f64) -> &Value;
pub fn LLVMConstIntGetZExtValue(ConstantVal: &Value) -> c_ulonglong;
pub fn LLVMRustConstInt128Get(ConstantVal: &Value, SExt: bool,
pub fn LLVMConstIntGetZExtValue(ConstantVal: &ConstantInt) -> c_ulonglong;
pub fn LLVMRustConstInt128Get(ConstantVal: &ConstantInt, SExt: bool,
high: &mut u64, low: &mut u64) -> bool;


Expand Down Expand Up @@ -1666,7 +1667,7 @@ extern "C" {
#[allow(improper_ctypes)]
pub fn LLVMRustWriteValueToString(value_ref: &Value, s: &RustString);

pub fn LLVMIsAConstantInt(value_ref: &Value) -> Option<&Value>;
pub fn LLVMIsAConstantInt(value_ref: &Value) -> Option<&ConstantInt>;

pub fn LLVMRustPassKind(Pass: &Pass) -> PassKind;
pub fn LLVMRustFindAndCreatePass(Pass: *const c_char) -> Option<&'static mut Pass>;
Expand Down

0 comments on commit f0e2fc7

Please sign in to comment.