Skip to content

Commit

Permalink
rustc_trans: do not pass floating-point values to LLVM through FFI.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Aug 2, 2017
1 parent 9861df4 commit c457b26
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/librustc_llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,6 @@ extern "C" {
// Operations on scalar constants
pub fn LLVMConstInt(IntTy: TypeRef, N: c_ulonglong, SignExtend: Bool) -> ValueRef;
pub fn LLVMConstIntOfArbitraryPrecision(IntTy: TypeRef, Wn: c_uint, Ws: *const u64) -> ValueRef;
pub fn LLVMConstReal(RealTy: TypeRef, N: f64) -> ValueRef;
pub fn LLVMConstIntGetZExtValue(ConstantVal: ValueRef) -> c_ulonglong;
pub fn LLVMConstIntGetSExtValue(ConstantVal: ValueRef) -> c_longlong;
pub fn LLVMRustConstInt128Get(ConstantVal: ValueRef, SExt: bool,
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,8 +844,7 @@ fn create_imps(sess: &Session,
let imp = llvm::LLVMAddGlobal(llvm_module.llmod,
i8p_ty.to_ref(),
imp_name.as_ptr() as *const _);
let init = llvm::LLVMConstBitCast(val, i8p_ty.to_ref());
llvm::LLVMSetInitializer(imp, init);
llvm::LLVMSetInitializer(imp, consts::ptrcast(val, i8p_ty));
llvm::LLVMRustSetLinkage(imp, llvm::Linkage::ExternalLinkage);
}
}
Expand Down
6 changes: 0 additions & 6 deletions src/librustc_trans/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,6 @@ pub fn C_big_integral(t: Type, u: u128) -> ValueRef {
}
}

pub fn C_floating_f64(f: f64, t: Type) -> ValueRef {
unsafe {
llvm::LLVMConstReal(t.to_ref(), f)
}
}

pub fn C_nil(ccx: &CrateContext) -> ValueRef {
C_struct(ccx, &[], false)
}
Expand Down
6 changes: 6 additions & 0 deletions src/librustc_trans/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ pub fn ptrcast(val: ValueRef, ty: Type) -> ValueRef {
}
}

pub fn bitcast(val: ValueRef, ty: Type) -> ValueRef {
unsafe {
llvm::LLVMConstBitCast(val, ty.to_ref())
}
}

pub fn addr_of_mut(ccx: &CrateContext,
cv: ValueRef,
align: machine::llalign,
Expand Down
12 changes: 6 additions & 6 deletions src/librustc_trans/mir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use llvm::{self, ValueRef};
use rustc::middle::const_val::{ConstEvalErr, ConstVal, ErrKind};
use rustc_const_math::ConstInt::*;
use rustc_const_math::ConstFloat;
use rustc_const_math::{ConstInt, ConstMathErr};
use rustc::hir::def_id::DefId;
use rustc::infer::TransNormalize;
Expand All @@ -27,7 +26,7 @@ use abi::{self, Abi};
use callee;
use builder::Builder;
use common::{self, CrateContext, const_get_elt, val_ty};
use common::{C_array, C_bool, C_bytes, C_floating_f64, C_integral, C_big_integral};
use common::{C_array, C_bool, C_bytes, C_integral, C_big_integral, C_u32, C_u64};
use common::{C_null, C_struct, C_str_slice, C_undef, C_uint, C_vector, is_undef};
use common::const_to_opt_u128;
use consts;
Expand All @@ -37,6 +36,7 @@ use type_::Type;
use value::Value;

use syntax_pos::Span;
use syntax::ast;

use std::fmt;
use std::ptr;
Expand Down Expand Up @@ -96,11 +96,11 @@ impl<'tcx> Const<'tcx> {
let llty = type_of::type_of(ccx, ty);
let val = match cv {
ConstVal::Float(v) => {
let v_f64 = match v {
ConstFloat::F32(v) => f32::from_bits(v) as f64,
ConstFloat::F64(v) => f64::from_bits(v)
let bits = match v.ty {
ast::FloatTy::F32 => C_u32(ccx, v.bits as u32),
ast::FloatTy::F64 => C_u64(ccx, v.bits as u64)
};
C_floating_f64(v_f64, llty)
consts::bitcast(bits, llty)
}
ConstVal::Bool(v) => C_bool(ccx, v),
ConstVal::Integral(ref i) => return Const::from_constint(ccx, i),
Expand Down

0 comments on commit c457b26

Please sign in to comment.