Skip to content

Commit 1e1ff63

Browse files
catamorphismgraydon
authored andcommitted
Make floating-point operations work (neg, add, sub, mul, div, rem,
and comparison ops.)
1 parent caa22c9 commit 1e1ff63

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/comp/middle/trans.rs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,6 +2367,7 @@ fn trans_unary(@block_ctxt cx, ast.unop op,
23672367
@ast.expr e, &ast.ann a) -> result {
23682368

23692369
auto sub = trans_expr(cx, e);
2370+
auto e_ty = ty.expr_ty(e);
23702371

23712372
alt (op) {
23722373
case (ast.bitnot) {
@@ -2379,7 +2380,12 @@ fn trans_unary(@block_ctxt cx, ast.unop op,
23792380
}
23802381
case (ast.neg) {
23812382
sub = autoderef(sub.bcx, sub.val, ty.expr_ty(e));
2382-
ret res(sub.bcx, sub.bcx.build.Neg(sub.val));
2383+
if(e_ty.struct == ty.ty_float) {
2384+
ret res(sub.bcx, sub.bcx.build.FNeg(sub.val));
2385+
}
2386+
else {
2387+
ret res(sub.bcx, sub.bcx.build.Neg(sub.val));
2388+
}
23832389
}
23842390
case (ast.box) {
23852391
auto e_ty = ty.expr_ty(e);
@@ -2674,24 +2680,60 @@ fn trans_vec_add(@block_ctxt cx, @ty.t t,
26742680
fn trans_eager_binop(@block_ctxt cx, ast.binop op, @ty.t intype,
26752681
ValueRef lhs, ValueRef rhs) -> result {
26762682

2683+
auto is_float = false;
2684+
alt(intype.struct) {
2685+
case (ty.ty_float) {
2686+
is_float = true;
2687+
}
2688+
case (_) {
2689+
is_float = false;
2690+
}
2691+
}
2692+
26772693
alt (op) {
26782694
case (ast.add) {
26792695
if (ty.type_is_sequence(intype)) {
26802696
ret trans_vec_add(cx, intype, lhs, rhs);
26812697
}
2682-
ret res(cx, cx.build.Add(lhs, rhs));
2698+
if (is_float) {
2699+
ret res(cx, cx.build.FAdd(lhs, rhs));
2700+
}
2701+
else {
2702+
ret res(cx, cx.build.Add(lhs, rhs));
2703+
}
2704+
}
2705+
case (ast.sub) {
2706+
if (is_float) {
2707+
ret res(cx, cx.build.FSub(lhs, rhs));
2708+
}
2709+
else {
2710+
ret res(cx, cx.build.Sub(lhs, rhs));
2711+
}
2712+
}
2713+
2714+
case (ast.mul) {
2715+
if (is_float) {
2716+
ret res(cx, cx.build.FMul(lhs, rhs));
2717+
}
2718+
else {
2719+
ret res(cx, cx.build.Mul(lhs, rhs));
2720+
}
26832721
}
2684-
case (ast.sub) { ret res(cx, cx.build.Sub(lhs, rhs)); }
26852722

2686-
case (ast.mul) { ret res(cx, cx.build.Mul(lhs, rhs)); }
26872723
case (ast.div) {
2724+
if (is_float) {
2725+
ret res(cx, cx.build.FDiv(lhs, rhs));
2726+
}
26882727
if (ty.type_is_signed(intype)) {
26892728
ret res(cx, cx.build.SDiv(lhs, rhs));
26902729
} else {
26912730
ret res(cx, cx.build.UDiv(lhs, rhs));
26922731
}
26932732
}
26942733
case (ast.rem) {
2734+
if (is_float) {
2735+
ret res(cx, cx.build.FRem(lhs, rhs));
2736+
}
26952737
if (ty.type_is_signed(intype)) {
26962738
ret res(cx, cx.build.SRem(lhs, rhs));
26972739
} else {

src/comp/middle/ty.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ fn fold_ty(ty_fold fld, @t ty) -> @t {
245245
case (ty_bool) { ret fld.fold_simple_ty(ty); }
246246
case (ty_int) { ret fld.fold_simple_ty(ty); }
247247
case (ty_uint) { ret fld.fold_simple_ty(ty); }
248+
case (ty_float) { ret fld.fold_simple_ty(ty); }
248249
case (ty_machine(_)) { ret fld.fold_simple_ty(ty); }
249250
case (ty_char) { ret fld.fold_simple_ty(ty); }
250251
case (ty_str) { ret fld.fold_simple_ty(ty); }
@@ -503,6 +504,9 @@ fn type_is_fp(@t ty) -> bool {
503504
case (_) { ret false; }
504505
}
505506
}
507+
case (ty_float) {
508+
ret true;
509+
}
506510
case (_) { ret false; }
507511
}
508512
fail;
@@ -1126,6 +1130,7 @@ fn unify(@ty.t expected, @ty.t actual, &unify_handler handler)
11261130
case (ty.ty_int) { ret struct_cmp(expected, actual); }
11271131
case (ty.ty_uint) { ret struct_cmp(expected, actual); }
11281132
case (ty.ty_machine(_)) { ret struct_cmp(expected, actual); }
1133+
case (ty.ty_float) { ret struct_cmp(expected, actual); }
11291134
case (ty.ty_char) { ret struct_cmp(expected, actual); }
11301135
case (ty.ty_str) { ret struct_cmp(expected, actual); }
11311136
case (ty.ty_type) { ret struct_cmp(expected, actual); }

0 commit comments

Comments
 (0)