From 1e1ff638a3c920654c5f05c83b05e1e3b76e9782 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Mon, 21 Mar 2011 16:21:54 -0700 Subject: [PATCH] Make floating-point operations work (neg, add, sub, mul, div, rem, and comparison ops.) --- src/comp/middle/trans.rs | 50 ++++++++++++++++++++++++++++++++++++---- src/comp/middle/ty.rs | 5 ++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index 7be12f910be..d4cd3992289 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 bf525679e2b..6eb3d8b53c1 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); }