aboutsummaryrefslogtreecommitdiff
path: root/src/comp
diff options
context:
space:
mode:
authorTim Chevalier <[email protected]>2011-03-21 16:21:54 -0700
committerGraydon Hoare <[email protected]>2011-03-21 18:10:34 -0700
commit1e1ff638a3c920654c5f05c83b05e1e3b76e9782 (patch)
treef547938782ca67845b45cb4ea318c95a25b69854 /src/comp
parentStarted adding support for floating-point type, floating-point literals, and ... (diff)
downloadrust-1e1ff638a3c920654c5f05c83b05e1e3b76e9782.tar.xz
rust-1e1ff638a3c920654c5f05c83b05e1e3b76e9782.zip
Make floating-point operations work (neg, add, sub, mul, div, rem,
and comparison ops.)
Diffstat (limited to 'src/comp')
-rw-r--r--src/comp/middle/trans.rs50
-rw-r--r--src/comp/middle/ty.rs5
2 files changed, 51 insertions, 4 deletions
diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs
index 7be12f91..d4cd3992 100644
--- a/src/comp/middle/trans.rs
+++ b/src/comp/middle/trans.rs
@@ -2367,6 +2367,7 @@ fn trans_unary(@block_ctxt cx, ast.unop op,
@ast.expr e, &ast.ann a) -> result {
auto sub = trans_expr(cx, e);
+ auto e_ty = ty.expr_ty(e);
alt (op) {
case (ast.bitnot) {
@@ -2379,7 +2380,12 @@ fn trans_unary(@block_ctxt cx, ast.unop op,
}
case (ast.neg) {
sub = autoderef(sub.bcx, sub.val, ty.expr_ty(e));
- ret res(sub.bcx, sub.bcx.build.Neg(sub.val));
+ if(e_ty.struct == ty.ty_float) {
+ ret res(sub.bcx, sub.bcx.build.FNeg(sub.val));
+ }
+ else {
+ ret res(sub.bcx, sub.bcx.build.Neg(sub.val));
+ }
}
case (ast.box) {
auto e_ty = ty.expr_ty(e);
@@ -2674,17 +2680,50 @@ fn trans_vec_add(@block_ctxt cx, @ty.t t,
fn trans_eager_binop(@block_ctxt cx, ast.binop op, @ty.t intype,
ValueRef lhs, ValueRef rhs) -> result {
+ auto is_float = false;
+ alt(intype.struct) {
+ case (ty.ty_float) {
+ is_float = true;
+ }
+ case (_) {
+ is_float = false;
+ }
+ }
+
alt (op) {
case (ast.add) {
if (ty.type_is_sequence(intype)) {
ret trans_vec_add(cx, intype, lhs, rhs);
}
- ret res(cx, cx.build.Add(lhs, rhs));
+ if (is_float) {
+ ret res(cx, cx.build.FAdd(lhs, rhs));
+ }
+ else {
+ ret res(cx, cx.build.Add(lhs, rhs));
+ }
+ }
+ case (ast.sub) {
+ if (is_float) {
+ ret res(cx, cx.build.FSub(lhs, rhs));
+ }
+ else {
+ ret res(cx, cx.build.Sub(lhs, rhs));
+ }
+ }
+
+ case (ast.mul) {
+ if (is_float) {
+ ret res(cx, cx.build.FMul(lhs, rhs));
+ }
+ else {
+ ret res(cx, cx.build.Mul(lhs, rhs));
+ }
}
- case (ast.sub) { ret res(cx, cx.build.Sub(lhs, rhs)); }
- case (ast.mul) { ret res(cx, cx.build.Mul(lhs, rhs)); }
case (ast.div) {
+ if (is_float) {
+ ret res(cx, cx.build.FDiv(lhs, rhs));
+ }
if (ty.type_is_signed(intype)) {
ret res(cx, cx.build.SDiv(lhs, rhs));
} else {
@@ -2692,6 +2731,9 @@ fn trans_eager_binop(@block_ctxt cx, ast.binop op, @ty.t intype,
}
}
case (ast.rem) {
+ if (is_float) {
+ ret res(cx, cx.build.FRem(lhs, rhs));
+ }
if (ty.type_is_signed(intype)) {
ret res(cx, cx.build.SRem(lhs, rhs));
} else {
diff --git a/src/comp/middle/ty.rs b/src/comp/middle/ty.rs
index bf525679..6eb3d8b5 100644
--- a/src/comp/middle/ty.rs
+++ b/src/comp/middle/ty.rs
@@ -245,6 +245,7 @@ fn fold_ty(ty_fold fld, @t ty) -> @t {
case (ty_bool) { ret fld.fold_simple_ty(ty); }
case (ty_int) { ret fld.fold_simple_ty(ty); }
case (ty_uint) { ret fld.fold_simple_ty(ty); }
+ case (ty_float) { ret fld.fold_simple_ty(ty); }
case (ty_machine(_)) { ret fld.fold_simple_ty(ty); }
case (ty_char) { ret fld.fold_simple_ty(ty); }
case (ty_str) { ret fld.fold_simple_ty(ty); }
@@ -503,6 +504,9 @@ fn type_is_fp(@t ty) -> bool {
case (_) { ret false; }
}
}
+ case (ty_float) {
+ ret true;
+ }
case (_) { ret false; }
}
fail;
@@ -1126,6 +1130,7 @@ fn unify(@ty.t expected, @ty.t actual, &unify_handler handler)
case (ty.ty_int) { ret struct_cmp(expected, actual); }
case (ty.ty_uint) { ret struct_cmp(expected, actual); }
case (ty.ty_machine(_)) { ret struct_cmp(expected, actual); }
+ case (ty.ty_float) { ret struct_cmp(expected, actual); }
case (ty.ty_char) { ret struct_cmp(expected, actual); }
case (ty.ty_str) { ret struct_cmp(expected, actual); }
case (ty.ty_type) { ret struct_cmp(expected, actual); }