authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-11-26 04:03:39-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-11-26 04:03:39-05:00
log697c768730ad4c095c376079adbb97854db84cb9
treeaee759024d7e31fce2c70b076b0574848d605b95
parentbbf785bc1d5740488521b0cb90eeae31090e67ae

IR: support switch with range


2 files changed, 34 insertions(+), 2 deletions(-)

src/ir.cpp+3-2
......@@ -630,6 +630,7 @@ static IrInstruction *ir_build_phi(IrBuilder *irb, AstNode *source_node,
630630 phi_instruction->incoming_values = incoming_values;
631631
632632 for (size_t i = 0; i < incoming_count; i += 1) {
633 ir_ref_bb(incoming_blocks[i]);
633634 ir_ref_instruction(incoming_values[i]);
634635 }
635636
......@@ -2784,8 +2785,8 @@ static void ir_start_bb(IrAnalyze *ira, IrBasicBlock *old_bb, IrBasicBlock *cons
27842785 ira->old_irb.current_basic_block = old_bb;
27852786 ira->const_predecessor_bb = const_predecessor_bb;
27862787
2787 assert(old_bb->other);
2788 ira->new_irb.exec->basic_block_list.append(old_bb->other);
2788 if (old_bb->other)
2789 ira->new_irb.exec->basic_block_list.append(old_bb->other);
27892790}
27902791
27912792static void ir_finish_bb(IrAnalyze *ira) {
test/self_hosted2.zig+31
......@@ -20,6 +20,35 @@ fn inlinedLoop() {
2020 assert(sum == 15);
2121}
2222
23fn switchWithNumbers() {
24 testSwitchWithNumbers(13);
25}
26
27fn testSwitchWithNumbers(x: u32) {
28 const result = switch (x) {
29 1, 2, 3, 4 ... 8 => false,
30 13 => true,
31 else => false,
32 };
33 assert(result);
34}
35
36fn switchWithAllRanges() {
37 assert(testSwitchWithAllRanges(50, 3) == 1);
38 assert(testSwitchWithAllRanges(101, 0) == 2);
39 assert(testSwitchWithAllRanges(300, 5) == 3);
40 assert(testSwitchWithAllRanges(301, 6) == 6);
41}
42
43fn testSwitchWithAllRanges(x: u32, y: u32) -> u32 {
44 switch (x) {
45 0 ... 100 => 1,
46 101 ... 200 => 2,
47 201 ... 300 => 3,
48 else => y,
49 }
50}
51
2352fn assert(ok: bool) {
2453 if (!ok)
2554 @unreachable();
......@@ -29,6 +58,8 @@ fn runAllTests() {
2958 emptyFunctionWithComments();
3059 disabledExternFn();
3160 inlinedLoop();
61 switchWithNumbers();
62 switchWithAllRanges();
3263}
3364
3465export nakedcc fn _start() -> unreachable {