Skip to content

Commit db3b9a4

Browse files
committed
Stop stringifying integers to get integral constants.
1 parent a71fda4 commit db3b9a4

File tree

4 files changed

+43
-32
lines changed

4 files changed

+43
-32
lines changed

src/comp/lib/llvm.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,10 @@ native mod llvm = llvm_lib {
864864
/** Parses the bitcode in the given memory buffer. */
865865
fn LLVMRustParseBitcode(MemoryBufferRef MemBuf) -> ModuleRef;
866866

867+
/** FiXME: Hacky adaptor for lack of ULongLong in FFI. */
868+
fn LLVMRustConstSmallInt(TypeRef IntTy, uint N,
869+
Bool SignExtend) -> ValueRef;
870+
867871
/** Links LLVM modules together. `Src` is destroyed by this call and
868872
must never be referenced again. */
869873
fn LLVMLinkModules(ModuleRef Dest, ModuleRef Src) -> Bool;

src/comp/middle/trans.rs

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import lib.llvm.llvm.BasicBlockRef;
4242

4343
import lib.llvm.False;
4444
import lib.llvm.True;
45+
import lib.llvm.Bool;
4546

4647
state obj namegen(mutable int i) {
4748
fn next(str prefix) -> str {
@@ -887,14 +888,13 @@ fn C_null(TypeRef t) -> ValueRef {
887888
ret llvm.LLVMConstNull(t);
888889
}
889890

890-
fn C_integral(int i, TypeRef t) -> ValueRef {
891+
fn C_integral(TypeRef t, uint u, Bool sign_extend) -> ValueRef {
891892
// FIXME. We can't use LLVM.ULongLong with our existing minimal native
892-
// API, which only knows word-sized args. Lucky for us LLVM has a "take a
893-
// string encoding" version. Hilarious. Please fix to handle:
893+
// API, which only knows word-sized args.
894894
//
895895
// ret llvm.LLVMConstInt(T_int(), t as LLVM.ULongLong, False);
896896
//
897-
ret llvm.LLVMConstIntOfString(t, Str.buf(istr(i)), 10);
897+
ret llvm.LLVMRustConstSmallInt(t, u, sign_extend);
898898
}
899899

900900
fn C_float(str s) -> ValueRef {
@@ -907,23 +907,23 @@ fn C_floating(str s, TypeRef t) -> ValueRef {
907907

908908
fn C_nil() -> ValueRef {
909909
// NB: See comment above in T_void().
910-
ret C_integral(0, T_i1());
910+
ret C_integral(T_i1(), 0u, False);
911911
}
912912

913913
fn C_bool(bool b) -> ValueRef {
914914
if (b) {
915-
ret C_integral(1, T_bool());
915+
ret C_integral(T_bool(), 1u, False);
916916
} else {
917-
ret C_integral(0, T_bool());
917+
ret C_integral(T_bool(), 0u, False);
918918
}
919919
}
920920

921921
fn C_int(int i) -> ValueRef {
922-
ret C_integral(i, T_int());
922+
ret C_integral(T_int(), i as uint, True);
923923
}
924924

925-
fn C_i8(uint i) -> ValueRef {
926-
ret C_integral(i as int, T_i8());
925+
fn C_u8(uint i) -> ValueRef {
926+
ret C_integral(T_i8(), i, False);
927927
}
928928

929929
// This is a 'c-like' raw string, which differs from
@@ -961,7 +961,7 @@ fn C_zero_byte_arr(uint size) -> ValueRef {
961961
auto i = 0u;
962962
let vec[ValueRef] elts = vec();
963963
while (i < size) {
964-
elts += vec(C_integral(0, T_i8()));
964+
elts += vec(C_u8(0u));
965965
i += 1u;
966966
}
967967
ret llvm.LLVMConstArray(T_i8(), Vec.buf[ValueRef](elts),
@@ -2175,7 +2175,7 @@ fn make_cmp_glue(@block_ctxt cx,
21752175
// == and <= default to true if they find == all the way. <
21762176
// defaults to false if it finds == all the way.
21772177
auto result_if_equal = scx.build.ICmp(lib.llvm.LLVMIntNE, llop,
2178-
C_i8(abi.cmp_glue_op_lt));
2178+
C_u8(abi.cmp_glue_op_lt));
21792179
scx.build.Store(result_if_equal, flag);
21802180
r = res(scx, C_nil());
21812181
}
@@ -2211,7 +2211,7 @@ fn make_cmp_glue(@block_ctxt cx,
22112211

22122212
// First 'eq' comparison: if so, continue to next elts.
22132213
auto eq_r = call_cmp_glue(cx, av, bv, t,
2214-
C_i8(abi.cmp_glue_op_eq));
2214+
C_u8(abi.cmp_glue_op_eq));
22152215
eq_r.bcx.build.CondBr(eq_r.val, cnt_cx.llbb, stop_cx.llbb);
22162216

22172217
// Second 'op' comparison: find out how this elt-pair decides.
@@ -2299,9 +2299,9 @@ fn make_fp_cmp_glue(@block_ctxt cx, ValueRef lhs, ValueRef rhs, ty.t fptype,
22992299
unreach_cx.build.Unreachable();
23002300

23012301
auto llswitch = cx.build.Switch(llop, unreach_cx.llbb, 3u);
2302-
llvm.LLVMAddCase(llswitch, C_i8(abi.cmp_glue_op_eq), eq_cx.llbb);
2303-
llvm.LLVMAddCase(llswitch, C_i8(abi.cmp_glue_op_lt), lt_cx.llbb);
2304-
llvm.LLVMAddCase(llswitch, C_i8(abi.cmp_glue_op_le), le_cx.llbb);
2302+
llvm.LLVMAddCase(llswitch, C_u8(abi.cmp_glue_op_eq), eq_cx.llbb);
2303+
llvm.LLVMAddCase(llswitch, C_u8(abi.cmp_glue_op_lt), lt_cx.llbb);
2304+
llvm.LLVMAddCase(llswitch, C_u8(abi.cmp_glue_op_le), le_cx.llbb);
23052305

23062306
auto last_result =
23072307
last_cx.build.Phi(T_i1(), vec(eq_result, lt_result, le_result),
@@ -2341,9 +2341,9 @@ fn compare_integral_values(@block_ctxt cx, ValueRef lhs, ValueRef rhs,
23412341
unreach_cx.build.Unreachable();
23422342

23432343
auto llswitch = cx.build.Switch(llop, unreach_cx.llbb, 3u);
2344-
llvm.LLVMAddCase(llswitch, C_i8(abi.cmp_glue_op_eq), eq_cx.llbb);
2345-
llvm.LLVMAddCase(llswitch, C_i8(abi.cmp_glue_op_lt), lt_cx.llbb);
2346-
llvm.LLVMAddCase(llswitch, C_i8(abi.cmp_glue_op_le), le_cx.llbb);
2344+
llvm.LLVMAddCase(llswitch, C_u8(abi.cmp_glue_op_eq), eq_cx.llbb);
2345+
llvm.LLVMAddCase(llswitch, C_u8(abi.cmp_glue_op_lt), lt_cx.llbb);
2346+
llvm.LLVMAddCase(llswitch, C_u8(abi.cmp_glue_op_le), le_cx.llbb);
23472347

23482348
auto last_result =
23492349
last_cx.build.Phi(T_i1(), vec(eq_result, lt_result, le_result),
@@ -2949,18 +2949,19 @@ fn trans_lit(@crate_ctxt cx, &ast.lit lit, &ast.ann ann) -> ValueRef {
29492949
// if target int width is larger than host, at the moment;
29502950
// re-do the mach-int types using 'big' when that works.
29512951
auto t = T_int();
2952+
auto s = True;
29522953
alt (tm) {
2953-
case (common.ty_u8) { t = T_i8(); }
2954-
case (common.ty_u16) { t = T_i16(); }
2955-
case (common.ty_u32) { t = T_i32(); }
2956-
case (common.ty_u64) { t = T_i64(); }
2954+
case (common.ty_u8) { t = T_i8(); s = False; }
2955+
case (common.ty_u16) { t = T_i16(); s = False; }
2956+
case (common.ty_u32) { t = T_i32(); s = False; }
2957+
case (common.ty_u64) { t = T_i64(); s = False; }
29572958

29582959
case (common.ty_i8) { t = T_i8(); }
29592960
case (common.ty_i16) { t = T_i16(); }
29602961
case (common.ty_i32) { t = T_i32(); }
29612962
case (common.ty_i64) { t = T_i64(); }
29622963
}
2963-
ret C_integral(i, t);
2964+
ret C_integral(t, i as uint, s);
29642965
}
29652966
case(ast.lit_float(?fs)) {
29662967
ret C_float(fs);
@@ -2974,7 +2975,7 @@ fn trans_lit(@crate_ctxt cx, &ast.lit lit, &ast.ann ann) -> ValueRef {
29742975
ret C_floating(s, t);
29752976
}
29762977
case (ast.lit_char(?c)) {
2977-
ret C_integral(c as int, T_char());
2978+
ret C_integral(T_char(), c as uint, False);
29782979
}
29792980
case (ast.lit_bool(?b)) {
29802981
ret C_bool(b);
@@ -3116,12 +3117,12 @@ fn trans_compare(@block_ctxt cx0, ast.binop op, ty.t t0,
31163117
// FIXME: Use or-patterns when we have them.
31173118
auto llop;
31183119
alt (op) {
3119-
case (ast.eq) { llop = C_i8(abi.cmp_glue_op_eq); }
3120-
case (ast.lt) { llop = C_i8(abi.cmp_glue_op_lt); }
3121-
case (ast.le) { llop = C_i8(abi.cmp_glue_op_le); }
3122-
case (ast.ne) { llop = C_i8(abi.cmp_glue_op_eq); }
3123-
case (ast.ge) { llop = C_i8(abi.cmp_glue_op_lt); }
3124-
case (ast.gt) { llop = C_i8(abi.cmp_glue_op_le); }
3120+
case (ast.eq) { llop = C_u8(abi.cmp_glue_op_eq); }
3121+
case (ast.lt) { llop = C_u8(abi.cmp_glue_op_lt); }
3122+
case (ast.le) { llop = C_u8(abi.cmp_glue_op_le); }
3123+
case (ast.ne) { llop = C_u8(abi.cmp_glue_op_eq); }
3124+
case (ast.ge) { llop = C_u8(abi.cmp_glue_op_lt); }
3125+
case (ast.gt) { llop = C_u8(abi.cmp_glue_op_le); }
31253126
}
31263127

31273128
auto rslt = call_cmp_glue(cx, lhs, rhs, t, llop);
@@ -7347,7 +7348,7 @@ fn make_bzero_glue(ValueRef fun) -> ValueRef {
73477348
// Loop-body block
73487349
auto lb = new_builder(loopbb);
73497350
i = lb.Load(ip);
7350-
lb.Store(C_integral(0, T_i8()), lb.GEP(dst, vec(i)));
7351+
lb.Store(C_u8(0u), lb.GEP(dst, vec(i)));
73517352
lb.Store(lb.Add(i, C_int(1)), ip);
73527353
lb.Br(hdrbb);
73537354

src/rustllvm/RustWrapper.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,8 @@ extern "C" const char *LLVMRustGetHostTriple(void)
116116
static std::string str = llvm::sys::getHostTriple();
117117
return str.c_str();
118118
}
119+
120+
extern "C" LLVMValueRef LLVMRustConstSmallInt(LLVMTypeRef IntTy, unsigned N,
121+
LLVMBool SignExtend) {
122+
return LLVMConstInt(IntTy, (unsigned long long)N, SignExtend);
123+
}

src/rustllvm/rustllvm.def.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ LLVMRustCreateMemoryBufferWithContentsOfFile
22
LLVMRustWriteOutputFile
33
LLVMRustGetLastError
44
LLVMRustGetHostTriple
5+
LLVMRustConstSmallInt
56
LLVMRustParseBitcode
67
LLVMLinkModules
78
LLVMCreateObjectFile

0 commit comments

Comments
 (0)