Skip to content

Commit c457b26

Browse files
committed
rustc_trans: do not pass floating-point values to LLVM through FFI.
1 parent 9861df4 commit c457b26

File tree

5 files changed

+13
-15
lines changed

5 files changed

+13
-15
lines changed

src/librustc_llvm/ffi.rs

-1
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,6 @@ extern "C" {
598598
// Operations on scalar constants
599599
pub fn LLVMConstInt(IntTy: TypeRef, N: c_ulonglong, SignExtend: Bool) -> ValueRef;
600600
pub fn LLVMConstIntOfArbitraryPrecision(IntTy: TypeRef, Wn: c_uint, Ws: *const u64) -> ValueRef;
601-
pub fn LLVMConstReal(RealTy: TypeRef, N: f64) -> ValueRef;
602601
pub fn LLVMConstIntGetZExtValue(ConstantVal: ValueRef) -> c_ulonglong;
603602
pub fn LLVMConstIntGetSExtValue(ConstantVal: ValueRef) -> c_longlong;
604603
pub fn LLVMRustConstInt128Get(ConstantVal: ValueRef, SExt: bool,

src/librustc_trans/base.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -844,8 +844,7 @@ fn create_imps(sess: &Session,
844844
let imp = llvm::LLVMAddGlobal(llvm_module.llmod,
845845
i8p_ty.to_ref(),
846846
imp_name.as_ptr() as *const _);
847-
let init = llvm::LLVMConstBitCast(val, i8p_ty.to_ref());
848-
llvm::LLVMSetInitializer(imp, init);
847+
llvm::LLVMSetInitializer(imp, consts::ptrcast(val, i8p_ty));
849848
llvm::LLVMRustSetLinkage(imp, llvm::Linkage::ExternalLinkage);
850849
}
851850
}

src/librustc_trans/common.rs

-6
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,6 @@ pub fn C_big_integral(t: Type, u: u128) -> ValueRef {
223223
}
224224
}
225225

226-
pub fn C_floating_f64(f: f64, t: Type) -> ValueRef {
227-
unsafe {
228-
llvm::LLVMConstReal(t.to_ref(), f)
229-
}
230-
}
231-
232226
pub fn C_nil(ccx: &CrateContext) -> ValueRef {
233227
C_struct(ccx, &[], false)
234228
}

src/librustc_trans/consts.rs

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ pub fn ptrcast(val: ValueRef, ty: Type) -> ValueRef {
3636
}
3737
}
3838

39+
pub fn bitcast(val: ValueRef, ty: Type) -> ValueRef {
40+
unsafe {
41+
llvm::LLVMConstBitCast(val, ty.to_ref())
42+
}
43+
}
44+
3945
pub fn addr_of_mut(ccx: &CrateContext,
4046
cv: ValueRef,
4147
align: machine::llalign,

src/librustc_trans/mir/constant.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use llvm::{self, ValueRef};
1212
use rustc::middle::const_val::{ConstEvalErr, ConstVal, ErrKind};
1313
use rustc_const_math::ConstInt::*;
14-
use rustc_const_math::ConstFloat;
1514
use rustc_const_math::{ConstInt, ConstMathErr};
1615
use rustc::hir::def_id::DefId;
1716
use rustc::infer::TransNormalize;
@@ -27,7 +26,7 @@ use abi::{self, Abi};
2726
use callee;
2827
use builder::Builder;
2928
use common::{self, CrateContext, const_get_elt, val_ty};
30-
use common::{C_array, C_bool, C_bytes, C_floating_f64, C_integral, C_big_integral};
29+
use common::{C_array, C_bool, C_bytes, C_integral, C_big_integral, C_u32, C_u64};
3130
use common::{C_null, C_struct, C_str_slice, C_undef, C_uint, C_vector, is_undef};
3231
use common::const_to_opt_u128;
3332
use consts;
@@ -37,6 +36,7 @@ use type_::Type;
3736
use value::Value;
3837

3938
use syntax_pos::Span;
39+
use syntax::ast;
4040

4141
use std::fmt;
4242
use std::ptr;
@@ -96,11 +96,11 @@ impl<'tcx> Const<'tcx> {
9696
let llty = type_of::type_of(ccx, ty);
9797
let val = match cv {
9898
ConstVal::Float(v) => {
99-
let v_f64 = match v {
100-
ConstFloat::F32(v) => f32::from_bits(v) as f64,
101-
ConstFloat::F64(v) => f64::from_bits(v)
99+
let bits = match v.ty {
100+
ast::FloatTy::F32 => C_u32(ccx, v.bits as u32),
101+
ast::FloatTy::F64 => C_u64(ccx, v.bits as u64)
102102
};
103-
C_floating_f64(v_f64, llty)
103+
consts::bitcast(bits, llty)
104104
}
105105
ConstVal::Bool(v) => C_bool(ccx, v),
106106
ConstVal::Integral(ref i) => return Const::from_constint(ccx, i),

0 commit comments

Comments
 (0)