From 7446af747d83622c849ec9f29c0365aa7bf4e697 Mon Sep 17 00:00:00 2001 From: Graydon Hoare Date: Thu, 10 Feb 2011 19:40:02 -0800 Subject: [PATCH] Translate pat_lit, un-XFAIL alt-pattern-lit.rs for rustc. --- src/Makefile | 1 + src/comp/middle/trans.rs | 42 ++++++++++++++++++++++++++++------------ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/Makefile b/src/Makefile index c1f33b91929..d55a63c2797 100644 --- a/src/Makefile +++ b/src/Makefile @@ -444,6 +444,7 @@ TEST_XFAILS_BOOT := $(TASK_XFAILS) \ TEST_XFAILS_RUSTC := $(filter-out \ $(addprefix test/run-pass/, \ alt-path.rs \ + alt-pattern-lit.rs \ alt-pattern-simple.rs \ alt-tag.rs \ arith-0.rs \ diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index 4135a6e59de..bedfad97980 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -1838,6 +1838,24 @@ fn trans_unary(@block_ctxt cx, ast.unop op, fail; } +// FIXME: implement proper structural comparison. + +fn trans_compare(@block_ctxt cx, ast.binop op, + ValueRef lhs, ValueRef rhs) -> ValueRef { + auto cmp = lib.llvm.LLVMIntEQ; + alt (op) { + case (ast.eq) { cmp = lib.llvm.LLVMIntEQ; } + case (ast.ne) { cmp = lib.llvm.LLVMIntNE; } + + // FIXME (issue #57): switch by signedness. + case (ast.lt) { cmp = lib.llvm.LLVMIntSLT; } + case (ast.le) { cmp = lib.llvm.LLVMIntSLE; } + case (ast.ge) { cmp = lib.llvm.LLVMIntSGE; } + case (ast.gt) { cmp = lib.llvm.LLVMIntSGT; } + } + ret cx.build.ICmp(cmp, lhs, rhs); +} + fn trans_eager_binop(@block_ctxt cx, ast.binop op, ValueRef lhs, ValueRef rhs) -> ValueRef { @@ -1857,18 +1875,7 @@ fn trans_eager_binop(@block_ctxt cx, ast.binop op, case (ast.lsr) { ret cx.build.LShr(lhs, rhs); } case (ast.asr) { ret cx.build.AShr(lhs, rhs); } case (_) { - auto cmp = lib.llvm.LLVMIntEQ; - alt (op) { - case (ast.eq) { cmp = lib.llvm.LLVMIntEQ; } - case (ast.ne) { cmp = lib.llvm.LLVMIntNE; } - - // FIXME (issue #57): switch by signedness. - case (ast.lt) { cmp = lib.llvm.LLVMIntSLT; } - case (ast.le) { cmp = lib.llvm.LLVMIntSLE; } - case (ast.ge) { cmp = lib.llvm.LLVMIntSGE; } - case (ast.gt) { cmp = lib.llvm.LLVMIntSGT; } - } - ret cx.build.ICmp(cmp, lhs, rhs); + ret trans_compare(cx, op, lhs, rhs); } } fail; @@ -2132,6 +2139,16 @@ fn trans_pat_match(@block_ctxt cx, @ast.pat pat, ValueRef llval, alt (pat.node) { case (ast.pat_wild(_)) { ret res(cx, llval); } case (ast.pat_bind(_, _, _)) { ret res(cx, llval); } + + case (ast.pat_lit(?lt, ?ann)) { + auto lllit = trans_lit(cx.fcx.ccx, *lt, ann); + auto lleq = trans_compare(cx, ast.eq, llval, lllit); + + auto matched_cx = new_sub_block_ctxt(cx, "matched_cx"); + cx.build.CondBr(lleq, matched_cx.llbb, next_cx.llbb); + ret res(matched_cx, llval); + } + case (ast.pat_tag(?id, ?subpats, ?vdef_opt, ?ann)) { auto lltagptr = cx.build.GEP(llval, vec(C_int(0), C_int(0))); auto lltag = cx.build.Load(lltagptr); @@ -2184,6 +2201,7 @@ fn trans_pat_binding(@block_ctxt cx, @ast.pat pat, ValueRef llval) -> result { alt (pat.node) { case (ast.pat_wild(_)) { ret res(cx, llval); } + case (ast.pat_lit(_, _)) { ret res(cx, llval); } case (ast.pat_bind(?id, ?def_id, ?ann)) { auto ty = node_ann_type(cx.fcx.ccx, ann); auto llty = type_of(cx.fcx.ccx, ty);