Skip to content

Commit b34f871

Browse files
committed
librustc: Change i1 to i8 for bools. Attempts to put out burning tree. rs=burningtree
1 parent 801f322 commit b34f871

File tree

9 files changed

+86
-34
lines changed

9 files changed

+86
-34
lines changed

src/librustc/middle/trans/_match.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,8 @@ pub fn pick_col(m: &[@Match]) -> uint {
10281028
pub enum branch_kind { no_branch, single, switch, compare, compare_vec_len, }
10291029
10301030
// Compiles a comparison between two things.
1031+
//
1032+
// NB: This must produce an i1, not a Rust bool (i8).
10311033
pub fn compare_values(cx: block,
10321034
lhs: ValueRef,
10331035
rhs: ValueRef,
@@ -1053,7 +1055,11 @@ pub fn compare_values(cx: block,
10531055
scratch_rhs],
10541056
expr::SaveIn(
10551057
scratch_result.val));
1056-
return scratch_result.to_result(bcx);
1058+
let result = scratch_result.to_result(bcx);
1059+
Result {
1060+
bcx: result.bcx,
1061+
val: bool_to_i1(result.bcx, result.val)
1062+
}
10571063
}
10581064
ty::ty_estr(_) => {
10591065
let scratch_result = scratch_datum(cx, ty::mk_bool(cx.tcx()),
@@ -1063,7 +1069,11 @@ pub fn compare_values(cx: block,
10631069
~[lhs, rhs],
10641070
expr::SaveIn(
10651071
scratch_result.val));
1066-
return scratch_result.to_result(bcx);
1072+
let result = scratch_result.to_result(bcx);
1073+
Result {
1074+
bcx: result.bcx,
1075+
val: bool_to_i1(result.bcx, result.val)
1076+
}
10671077
}
10681078
_ => {
10691079
cx.tcx().sess.bug(~"only scalars and strings supported in \
@@ -1176,6 +1186,7 @@ pub fn compile_guard(bcx: block,
11761186
expr::trans_to_datum(bcx, guard_expr).to_result()
11771187
}
11781188
});
1189+
let val = bool_to_i1(bcx, val);
11791190
11801191
// Revoke the temp cleanups now that the guard successfully executed.
11811192
for temp_cleanups.each |llval| {

src/librustc/middle/trans/base.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,13 @@ pub fn maybe_name_value(cx: @crate_ctxt, v: ValueRef, s: ~str) {
494494
// Used only for creating scalar comparison glue.
495495
pub enum scalar_type { nil_type, signed_int, unsigned_int, floating_point, }
496496

497-
pub fn compare_scalar_types(cx: block, lhs: ValueRef, rhs: ValueRef,
498-
t: ty::t, op: ast::binop) -> Result {
497+
// NB: This produces an i1, not a Rust bool (i8).
498+
pub fn compare_scalar_types(cx: block,
499+
lhs: ValueRef,
500+
rhs: ValueRef,
501+
t: ty::t,
502+
op: ast::binop)
503+
-> Result {
499504
let f = |a| compare_scalar_values(cx, lhs, rhs, a, op);
500505

501506
match ty::get(t).sty {
@@ -521,8 +526,12 @@ pub fn compare_scalar_types(cx: block, lhs: ValueRef, rhs: ValueRef,
521526

522527

523528
// A helper function to do the actual comparison of scalar values.
524-
pub fn compare_scalar_values(cx: block, lhs: ValueRef, rhs: ValueRef,
525-
nt: scalar_type, op: ast::binop) -> ValueRef {
529+
pub fn compare_scalar_values(cx: block,
530+
lhs: ValueRef,
531+
rhs: ValueRef,
532+
nt: scalar_type,
533+
op: ast::binop)
534+
-> ValueRef {
526535
let _icx = cx.insn_ctxt("compare_scalar_values");
527536
fn die(cx: block) -> ! {
528537
cx.tcx().sess.bug(~"compare_scalar_values: must be a\
@@ -533,8 +542,8 @@ pub fn compare_scalar_values(cx: block, lhs: ValueRef, rhs: ValueRef,
533542
// We don't need to do actual comparisons for nil.
534543
// () == () holds but () < () does not.
535544
match op {
536-
ast::eq | ast::le | ast::ge => return C_bool(true),
537-
ast::ne | ast::lt | ast::gt => return C_bool(false),
545+
ast::eq | ast::le | ast::ge => return C_i1(true),
546+
ast::ne | ast::lt | ast::gt => return C_i1(false),
538547
// refinements would be nice
539548
_ => die(cx)
540549
}
@@ -1442,7 +1451,7 @@ pub fn call_memcpy(cx: block, dst: ValueRef, src: ValueRef,
14421451
let dst_ptr = PointerCast(cx, dst, T_ptr(T_i8()));
14431452
let size = IntCast(cx, n_bytes, ccx.int_type);
14441453
let align = C_i32(1i32);
1445-
let volatile = C_bool(false);
1454+
let volatile = C_i1(false);
14461455
Call(cx, memcpy, ~[dst_ptr, src_ptr, size, align, volatile]);
14471456
}
14481457

@@ -1489,7 +1498,7 @@ pub fn memzero(cx: block, llptr: ValueRef, llty: TypeRef) {
14891498
let llzeroval = C_u8(0);
14901499
let size = IntCast(cx, machine::llsize_of(ccx, llty), ccx.int_type);
14911500
let align = C_i32(1i32);
1492-
let volatile = C_bool(false);
1501+
let volatile = C_i1(false);
14931502
Call(cx, llintrinsicfn, ~[llptr, llzeroval, size, align, volatile]);
14941503
}
14951504

src/librustc/middle/trans/callee.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,9 @@ pub fn trans_call_inner(
438438
let flag = alloca(bcx, T_bool());
439439
Store(bcx, C_bool(false), flag);
440440
Some(flag)
441-
} else { None };
441+
} else {
442+
None
443+
};
442444

443445
let (llfn, llenv) = unsafe {
444446
match callee.data {
@@ -506,7 +508,8 @@ pub fn trans_call_inner(
506508
if ty::type_is_bot(ret_ty) {
507509
Unreachable(bcx);
508510
} else if ret_in_loop {
509-
bcx = do with_cond(bcx, Load(bcx, ret_flag.get())) |bcx| {
511+
let ret_flag_result = bool_to_i1(bcx, Load(bcx, ret_flag.get()));
512+
bcx = do with_cond(bcx, ret_flag_result) |bcx| {
510513
do option::iter(&copy bcx.fcx.loop_ret) |lret| {
511514
Store(bcx, C_bool(true), lret.flagptr);
512515
Store(bcx, C_bool(false), bcx.fcx.llretptr);

src/librustc/middle/trans/common.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ pub fn T_f32() -> TypeRef { unsafe { return llvm::LLVMFloatType(); } }
759759

760760
pub fn T_f64() -> TypeRef { unsafe { return llvm::LLVMDoubleType(); } }
761761

762-
pub fn T_bool() -> TypeRef { return T_i1(); }
762+
pub fn T_bool() -> TypeRef { return T_i8(); }
763763

764764
pub fn T_int(targ_cfg: @session::config) -> TypeRef {
765765
return match targ_cfg.arch {
@@ -1109,6 +1109,10 @@ pub fn C_bool(b: bool) -> ValueRef {
11091109
C_integral(T_bool(), if b { 1u64 } else { 0u64 }, False)
11101110
}
11111111

1112+
pub fn C_i1(b: bool) -> ValueRef {
1113+
return C_integral(T_i1(), if b { 1 } else { 0 }, False);
1114+
}
1115+
11121116
pub fn C_i32(i: i32) -> ValueRef {
11131117
return C_integral(T_i32(), i as u64, True);
11141118
}
@@ -1435,6 +1439,11 @@ pub fn struct_dtor() -> [uint * 2] {
14351439
[0, 1]
14361440
}
14371441

1442+
// Casts a Rust bool value to an i1.
1443+
pub fn bool_to_i1(bcx: block, llval: ValueRef) -> ValueRef {
1444+
build::ICmp(bcx, lib::llvm::IntNE, llval, C_bool(false))
1445+
}
1446+
14381447
//
14391448
// Local Variables:
14401449
// mode: rust

src/librustc/middle/trans/controlflow.rs

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ pub fn trans_if(bcx: block,
6262

6363
let then_bcx_in = scope_block(bcx, thn.info(), ~"then");
6464
let else_bcx_in = scope_block(bcx, els.info(), ~"else");
65+
66+
let cond_val = bool_to_i1(bcx, cond_val);
6567
CondBr(bcx, cond_val, then_bcx_in.llbb, else_bcx_in.llbb);
6668

6769
debug!("then_bcx_in=%s, else_bcx_in=%s",
@@ -139,6 +141,7 @@ pub fn trans_while(bcx: block, cond: @ast::expr, body: ast::blk) -> block {
139141
// compile the condition
140142
let Result {bcx: cond_bcx_out, val: cond_val} =
141143
expr::trans_to_datum(cond_bcx_in, cond).to_result();
144+
let cond_val = bool_to_i1(cond_bcx_out, cond_val);
142145
let cond_bcx_out =
143146
trans_block_cleanups(cond_bcx_out, block_cleanups(cond_bcx_in));
144147
CondBr(cond_bcx_out, cond_val, body_bcx_in.llbb, next_bcx.llbb);
@@ -324,6 +327,7 @@ pub fn trans_check_expr(bcx: block,
324327
expr::trans_to_datum(bcx, pred_expr).to_result()
325328
}
326329
};
330+
let val = bool_to_i1(bcx, val);
327331
do with_cond(bcx, Not(bcx, val)) |bcx| {
328332
trans_fail(bcx, Some(pred_expr.span), /*bad*/copy expr_str)
329333
}

src/librustc/middle/trans/debuginfo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ fn lli64(val: int) -> ValueRef {
7777
C_i64(val as i64)
7878
}
7979
fn lli1(bval: bool) -> ValueRef {
80-
C_bool(bval)
80+
C_i1(bval)
8181
}
8282
fn llmdnode(elems: ~[ValueRef]) -> ValueRef {
8383
unsafe {

src/librustc/middle/trans/expr.rs

+23-9
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,6 @@ fn trans_unary_datum(bcx: block,
12361236
un_expr: @ast::expr,
12371237
op: ast::unop,
12381238
sub_expr: @ast::expr) -> DatumBlock {
1239-
12401239
let _icx = bcx.insn_ctxt("trans_unary_datum");
12411240

12421241
// if deref, would be LvalueExpr
@@ -1251,7 +1250,21 @@ fn trans_unary_datum(bcx: block,
12511250
return match op {
12521251
ast::not => {
12531252
let Result {bcx, val} = trans_to_datum(bcx, sub_expr).to_result();
1254-
immediate_rvalue_bcx(bcx, Not(bcx, val), un_ty)
1253+
1254+
// If this is a boolean type, we must not use the LLVM Not
1255+
// instruction, as that is a *bitwise* not and we want *logical*
1256+
// not on our 8-bit boolean values.
1257+
let llresult = match ty::get(un_ty).sty {
1258+
ty::ty_bool => {
1259+
let llcond = ICmp(bcx,
1260+
lib::llvm::IntEQ,
1261+
val,
1262+
C_bool(false));
1263+
Select(bcx, llcond, C_bool(true), C_bool(false))
1264+
}
1265+
_ => Not(bcx, val)
1266+
};
1267+
immediate_rvalue_bcx(bcx, llresult, un_ty)
12551268
}
12561269
ast::neg => {
12571270
let Result {bcx, val} = trans_to_datum(bcx, sub_expr).to_result();
@@ -1308,8 +1321,8 @@ fn trans_eager_binop(bcx: block,
13081321
binop_ty: ty::t,
13091322
op: ast::binop,
13101323
lhs_datum: &Datum,
1311-
rhs_datum: &Datum) -> DatumBlock
1312-
{
1324+
rhs_datum: &Datum)
1325+
-> DatumBlock {
13131326
let mut bcx = bcx;
13141327
let _icx = bcx.insn_ctxt("trans_eager_binop");
13151328

@@ -1388,7 +1401,7 @@ fn trans_eager_binop(bcx: block,
13881401
}
13891402
let cmpr = base::compare_scalar_types(bcx, lhs, rhs, rhs_t, op);
13901403
bcx = cmpr.bcx;
1391-
cmpr.val
1404+
ZExt(bcx, cmpr.val, T_i8())
13921405
}
13931406
}
13941407
_ => {
@@ -1406,8 +1419,7 @@ fn trans_lazy_binop(bcx: block,
14061419
binop_expr: @ast::expr,
14071420
op: lazy_binop_ty,
14081421
a: @ast::expr,
1409-
b: @ast::expr) -> DatumBlock
1410-
{
1422+
b: @ast::expr) -> DatumBlock {
14111423
let _icx = bcx.insn_ctxt("trans_lazy_binop");
14121424
let binop_ty = expr_ty(bcx, binop_expr);
14131425
let mut bcx = bcx;
@@ -1425,10 +1437,12 @@ fn trans_lazy_binop(bcx: block,
14251437
let join = base::sub_block(bcx, ~"join");
14261438
let before_rhs = base::sub_block(bcx, ~"rhs");
14271439

1440+
let lhs_i1 = bool_to_i1(past_lhs, lhs);
14281441
match op {
1429-
lazy_and => CondBr(past_lhs, lhs, before_rhs.llbb, join.llbb),
1430-
lazy_or => CondBr(past_lhs, lhs, join.llbb, before_rhs.llbb)
1442+
lazy_and => CondBr(past_lhs, lhs_i1, before_rhs.llbb, join.llbb),
1443+
lazy_or => CondBr(past_lhs, lhs_i1, join.llbb, before_rhs.llbb)
14311444
}
1445+
14321446
let Result {bcx: past_rhs, val: rhs} = {
14331447
do base::with_scope_result(before_rhs, b.info(), ~"rhs") |bcx| {
14341448
trans_to_datum(bcx, b).to_result()

src/librustc/middle/trans/foreign.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,8 @@ pub fn trans_intrinsic(ccx: @crate_ctxt,
524524
}
525525
~"needs_drop" => {
526526
let tp_ty = substs.tys[0];
527-
Store(bcx, C_bool(ty::type_needs_drop(ccx.tcx, tp_ty)),
527+
Store(bcx,
528+
C_bool(ty::type_needs_drop(ccx.tcx, tp_ty)),
528529
fcx.llretptr);
529530
}
530531
~"visit_tydesc" => {
@@ -574,7 +575,7 @@ pub fn trans_intrinsic(ccx: @crate_ctxt,
574575
let src_ptr = get_param(decl, first_real_arg + 1);
575576
let size = get_param(decl, first_real_arg + 2);
576577
let align = C_i32(1);
577-
let volatile = C_bool(false);
578+
let volatile = C_i1(false);
578579
let llfn = bcx.ccx().intrinsics.get(
579580
&~"llvm.memmove.p0i8.p0i8.i32");
580581
Call(bcx, llfn, ~[dst_ptr, src_ptr, size, align, volatile]);
@@ -584,7 +585,7 @@ pub fn trans_intrinsic(ccx: @crate_ctxt,
584585
let src_ptr = get_param(decl, first_real_arg + 1);
585586
let size = get_param(decl, first_real_arg + 2);
586587
let align = C_i32(1);
587-
let volatile = C_bool(false);
588+
let volatile = C_i1(false);
588589
let llfn = bcx.ccx().intrinsics.get(
589590
&~"llvm.memmove.p0i8.p0i8.i64");
590591
Call(bcx, llfn, ~[dst_ptr, src_ptr, size, align, volatile]);
@@ -769,49 +770,49 @@ pub fn trans_intrinsic(ccx: @crate_ctxt,
769770
}
770771
~"ctlz8" => {
771772
let x = get_param(decl, first_real_arg);
772-
let y = C_bool(false);
773+
let y = C_i1(false);
773774
let ctlz = ccx.intrinsics.get(&~"llvm.ctlz.i8");
774775
Store(bcx, Call(bcx, ctlz, ~[x, y]), fcx.llretptr)
775776
}
776777
~"ctlz16" => {
777778
let x = get_param(decl, first_real_arg);
778-
let y = C_bool(false);
779+
let y = C_i1(false);
779780
let ctlz = ccx.intrinsics.get(&~"llvm.ctlz.i16");
780781
Store(bcx, Call(bcx, ctlz, ~[x, y]), fcx.llretptr)
781782
}
782783
~"ctlz32" => {
783784
let x = get_param(decl, first_real_arg);
784-
let y = C_bool(false);
785+
let y = C_i1(false);
785786
let ctlz = ccx.intrinsics.get(&~"llvm.ctlz.i32");
786787
Store(bcx, Call(bcx, ctlz, ~[x, y]), fcx.llretptr)
787788
}
788789
~"ctlz64" => {
789790
let x = get_param(decl, first_real_arg);
790-
let y = C_bool(false);
791+
let y = C_i1(false);
791792
let ctlz = ccx.intrinsics.get(&~"llvm.ctlz.i64");
792793
Store(bcx, Call(bcx, ctlz, ~[x, y]), fcx.llretptr)
793794
}
794795
~"cttz8" => {
795796
let x = get_param(decl, first_real_arg);
796-
let y = C_bool(false);
797+
let y = C_i1(false);
797798
let cttz = ccx.intrinsics.get(&~"llvm.cttz.i8");
798799
Store(bcx, Call(bcx, cttz, ~[x, y]), fcx.llretptr)
799800
}
800801
~"cttz16" => {
801802
let x = get_param(decl, first_real_arg);
802-
let y = C_bool(false);
803+
let y = C_i1(false);
803804
let cttz = ccx.intrinsics.get(&~"llvm.cttz.i16");
804805
Store(bcx, Call(bcx, cttz, ~[x, y]), fcx.llretptr)
805806
}
806807
~"cttz32" => {
807808
let x = get_param(decl, first_real_arg);
808-
let y = C_bool(false);
809+
let y = C_i1(false);
809810
let cttz = ccx.intrinsics.get(&~"llvm.cttz.i32");
810811
Store(bcx, Call(bcx, cttz, ~[x, y]), fcx.llretptr)
811812
}
812813
~"cttz64" => {
813814
let x = get_param(decl, first_real_arg);
814-
let y = C_bool(false);
815+
let y = C_i1(false);
815816
let cttz = ccx.intrinsics.get(&~"llvm.cttz.i64");
816817
Store(bcx, Call(bcx, cttz, ~[x, y]), fcx.llretptr)
817818
}

src/librustc/middle/trans/reflect.rs

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ pub impl reflector {
109109
ast::m_imm)),
110110
ArgVals(args), SaveIn(scratch.val), DontAutorefArg);
111111
let result = scratch.to_value_llval(bcx);
112+
let result = bool_to_i1(bcx, result);
112113
let next_bcx = sub_block(bcx, ~"next");
113114
CondBr(bcx, result, next_bcx.llbb, self.final_bcx.llbb);
114115
self.bcx = next_bcx

0 commit comments

Comments
 (0)