]> git.lizzy.rs Git - rust.git/commitdiff
Implement unimplemented const binops
authorCorey Richardson <corey@octayn.net>
Sun, 19 May 2013 20:03:52 +0000 (16:03 -0400)
committerCorey Richardson <corey@octayn.net>
Mon, 20 May 2013 02:09:32 +0000 (22:09 -0400)
src/librustc/lib/llvm.rs
src/librustc/middle/trans/consts.rs
src/test/run-pass/const-binops.rs [new file with mode: 0644]

index d06bf1480c989b268948c1640644bcafc6082e87..cdc128c6eeb6119e788f936a3438005e8394ec97 100644 (file)
@@ -220,6 +220,7 @@ pub mod llvm {
     use super::{ObjectFileRef, Opcode, PassManagerRef, PassManagerBuilderRef};
     use super::{SectionIteratorRef, TargetDataRef, TypeKind, TypeRef, UseRef};
     use super::{ValueRef};
+    use super::{IntPredicate, RealPredicate};
 
     use core::libc::{c_char, c_int, c_longlong, c_uint, c_ulonglong};
 
@@ -451,6 +452,10 @@ pub unsafe fn LLVMSetOperand(Val: ValueRef,
         /* all zeroes */
         #[fast_ffi]
         pub unsafe fn LLVMConstAllOnes(Ty: TypeRef) -> ValueRef;
+        #[fast_ffi]
+        pub unsafe fn LLVMConstICmp(Pred: IntPredicate, V1: ValueRef, V2: ValueRef) -> ValueRef;
+        #[fast_ffi]
+        pub unsafe fn LLVMConstFCmp(Pred: RealPredicate, V1: ValueRef, V2: ValueRef) -> ValueRef;
         /* only for int/vector */
         #[fast_ffi]
         pub unsafe fn LLVMGetUndef(Ty: TypeRef) -> ValueRef;
index e08cc536a701f6bd99543001d7c32dd01d3bc184..6df523976f2b0a08237e28b658c07010b9ba3356 100644 (file)
@@ -9,8 +9,10 @@
 // except according to those terms.
 
 use back::abi;
-use lib::llvm::{llvm, SetLinkage, PrivateLinkage,
-                ValueRef, TypeRef, Bool, True, False};
+use lib::llvm::{llvm, SetLinkage, PrivateLinkage, ValueRef, TypeRef, Bool, True, False};
+use lib::llvm::{IntEQ, IntNE, IntUGT, IntUGE, IntULT, IntULE, IntSGT, IntSGE, IntSLT, IntSLE,
+    RealOEQ, RealOGT, RealOGE, RealOLT, RealOLE, RealONE};
+
 use metadata::csearch;
 use middle::const_eval;
 use middle::trans::adt;
@@ -280,8 +282,8 @@ fn const_expr_unadjusted(cx: @CrateContext, e: @ast::expr) -> ValueRef {
                 else if signed { llvm::LLVMConstSRem(te1, te2) }
                 else           { llvm::LLVMConstURem(te1, te2) }
               }
-              ast::and    |
-              ast::or     => cx.sess.span_unimpl(e.span, "binop logic"),
+              ast::and    => llvm::LLVMConstAnd(te1, te2),
+              ast::or     => llvm::LLVMConstOr(te1, te2),
               ast::bitxor => llvm::LLVMConstXor(te1, te2),
               ast::bitand => llvm::LLVMConstAnd(te1, te2),
               ast::bitor  => llvm::LLVMConstOr(te1, te2),
@@ -290,14 +292,44 @@ fn const_expr_unadjusted(cx: @CrateContext, e: @ast::expr) -> ValueRef {
                 if signed { llvm::LLVMConstAShr(te1, te2) }
                 else      { llvm::LLVMConstLShr(te1, te2) }
               }
-              ast::eq     |
-              ast::lt     |
-              ast::le     |
-              ast::ne     |
-              ast::ge     |
-              ast::gt     => cx.sess.span_unimpl(e.span, "binop comparator")
-            }
-          }
+              ast::eq     => {
+                  if is_float { llvm::LLVMConstFCmp(RealOEQ, te1, te2) }
+                  else        { llvm::LLVMConstICmp(IntEQ, te1, te2)   }
+              },
+              ast::lt     => {
+                  if is_float { llvm::LLVMConstFCmp(RealOLT, te1, te2) }
+                  else        {
+                      if signed { llvm::LLVMConstICmp(IntSLT, te1, te2) }
+                      else      { llvm::LLVMConstICmp(IntULT, te1, te2) }
+                  }
+              },
+              ast::le     => {
+                  if is_float { llvm::LLVMConstFCmp(RealOLE, te1, te2) }
+                  else        {
+                      if signed { llvm::LLVMConstICmp(IntSLE, te1, te2) }
+                      else      { llvm::LLVMConstICmp(IntULE, te1, te2) }
+                  }
+              },
+              ast::ne     => {
+                  if is_float { llvm::LLVMConstFCmp(RealONE, te1, te2) }
+                  else        { llvm::LLVMConstICmp(IntNE, te1, te2) }
+              },
+              ast::ge     => {
+                  if is_float { llvm::LLVMConstFCmp(RealOGE, te1, te2) }
+                  else        {
+                      if signed { llvm::LLVMConstICmp(IntSGE, te1, te2) }
+                      else      { llvm::LLVMConstICmp(IntUGE, te1, te2) }
+                  }
+              },
+              ast::gt     => {
+                  if is_float { llvm::LLVMConstFCmp(RealOGT, te1, te2) }
+                  else        {
+                      if signed { llvm::LLVMConstICmp(IntSGT, te1, te2) }
+                      else      { llvm::LLVMConstICmp(IntUGT, te1, te2) }
+                  }
+              },
+            };
+          },
           ast::expr_unary(u, e) => {
             let te = const_expr(cx, e);
             let ty = ty::expr_ty(cx.tcx, e);
diff --git a/src/test/run-pass/const-binops.rs b/src/test/run-pass/const-binops.rs
new file mode 100644 (file)
index 0000000..507f68d
--- /dev/null
@@ -0,0 +1,116 @@
+static a: int = -4 + 3;
+static a2: uint = 3 + 3;
+static b: float = 3.0 + 2.7;
+
+static c: int = 3 - 4;
+static d: uint = 3 - 3;
+static e: float = 3.0 - 2.7;
+
+static e2: int = -3 * 3;
+static f: uint = 3 * 3;
+static g: float = 3.3 * 3.3;
+
+static h: int = 3 / -1;
+static i: uint = 3 / 3;
+static j: float = 3.3 / 3.3;
+
+static n: bool = true && false;
+
+static o: bool = true || false;
+
+static p: int = 3 & 1;
+static q: uint = 1 & 3;
+
+static r: int = 3 | 1;
+static s: uint = 1 | 3;
+
+static t: int = 3 ^ 1;
+static u: uint = 1 ^ 3;
+
+static v: int = 1 << 3;
+
+// NOTE: better shr coverage
+static w: int = 1024 >> 4;
+static x: uint = 1024 >> 4;
+
+static y: bool = 1 == 1;
+static z: bool = 1.0 == 1.0;
+
+static aa: bool = 1 <= 2;
+static ab: bool = -1 <= 2;
+static ac: bool = 1.0 <= 2;
+
+static ad: bool = 1 < 2;
+static ae: bool = -1 < 2;
+static af: bool = 1.0 < 2;
+
+static ag: bool = 1 != 2;
+static ah: bool = -1 != 2;
+static ai: bool = 1.0 != 2;
+
+static aj: bool = 2 >= 1;
+static ak: bool = 2 >= -2;
+static al: bool = 1.0 >= -2;
+
+static am: bool = 2 > 1;
+static an: bool = 2 > -2;
+static ao: bool = 1.0 > -2;
+
+fn main() {
+    assert_eq!(a, -1);
+    assert_eq!(a2, 6);
+    assert_approx_eq!(b, 5.7);
+
+    assert_eq!(c, -1);
+    assert_eq!(d, 0);
+    assert_approx_eq!(e, -0.3);
+
+    assert_eq!(e2, -9);
+    assert_eq!(f, 9);
+    assert_approx_eq!(g, 10.89);
+
+    assert_eq!(h, -3);
+    assert_eq!(i, 1);
+    assert_approx_eq!(j, 1.0);
+
+    assert_eq!(n, false);
+
+    assert_eq!(o, true);
+
+    assert_eq!(p, 1);
+    assert_eq!(q, 1);
+
+    assert_eq!(r, 3);
+    assert_eq!(s, 3);
+
+    assert_eq!(t, 2);
+    assert_eq!(u, 2);
+
+    assert_eq!(v, 8);
+
+    assert_eq!(w, 64);
+    assert_eq!(x, 64);
+
+    assert_eq!(y, true);
+    assert_eq!(z, true);
+
+    assert_eq!(aa, true);
+    assert_eq!(ab, true);
+    assert_eq!(ac, true);
+
+    assert_eq!(ad, true);
+    assert_eq!(ae, true);
+    assert_eq!(af, true);
+
+    assert_eq!(ag, true);
+    assert_eq!(ah, true);
+    assert_eq!(ai, true);
+
+    assert_eq!(aj, true);
+    assert_eq!(ak, true);
+    assert_eq!(al, true);
+
+    assert_eq!(am, true);
+    assert_eq!(an, true);
+    assert_eq!(ao, true);
+}