aboutsummaryrefslogtreecommitdiff
path: root/src/ir.cpp
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2019-06-07 15:58:18 -0400
committerAndrew Kelley <andrew@ziglang.org>2019-06-07 15:58:18 -0400
commit2b0a1b7b1438cfc561276fa56356e5dbaa359c89 (patch)
tree9ef27675444af88677ba3abcdb0be8357de65bb6 /src/ir.cpp
parentede3436b087223f88c65124a12b7d477d792748d (diff)
downloadzig-2b0a1b7b1438cfc561276fa56356e5dbaa359c89.tar.gz
zig-2b0a1b7b1438cfc561276fa56356e5dbaa359c89.zip
hook up result locs to for loops
```zig export fn entry() void { var buf: [10]u8 = undefined; var x = for (buf) |x| break foo() else bar(); } ``` ```llvm define void @entry() #2 !dbg !35 { Entry: %buf = alloca [10 x i8], align 1 %i = alloca i64, align 8 %x = alloca %Foo, align 4 %0 = bitcast [10 x i8]* %buf to i8*, !dbg !51 call void @llvm.memset.p0i8.i64(i8* align 1 %0, i8 -86, i64 10, i1 false), !dbg !51 call void @llvm.dbg.declare(metadata [10 x i8]* %buf, metadata !39, metadata !DIExpression()), !dbg !51 store i64 0, i64* %i, align 8, !dbg !52 call void @llvm.dbg.declare(metadata i64* %i, metadata !44, metadata !DIExpression()), !dbg !52 br label %ForCond, !dbg !52 ForCond: ; preds = %Entry %1 = load i64, i64* %i, align 8, !dbg !52 %2 = icmp ult i64 %1, 10, !dbg !52 br i1 %2, label %ForBody, label %ForElse, !dbg !52 ForBody: ; preds = %ForCond %3 = getelementptr inbounds [10 x i8], [10 x i8]* %buf, i64 0, i64 %1, !dbg !52 call void @llvm.dbg.declare(metadata i8* %3, metadata !45, metadata !DIExpression()), !dbg !53 call fastcc void @foo(%Foo* sret %x), !dbg !54 br label %ForEnd, !dbg !55 ForElse: ; preds = %ForCond call fastcc void @bar(%Foo* sret %x), !dbg !56 br label %ForEnd, !dbg !52 ForEnd: ; preds = %ForElse, %ForBody call void @llvm.dbg.declare(metadata %Foo* %x, metadata !46, metadata !DIExpression()), !dbg !57 ret void, !dbg !58 } ```
Diffstat (limited to 'src/ir.cpp')
-rw-r--r--src/ir.cpp21
1 files changed, 16 insertions, 5 deletions
diff --git a/src/ir.cpp b/src/ir.cpp
index 5b7e4516e2..e0cd7194e5 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -5983,7 +5983,9 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
}
}
-static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNode *node) {
+static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNode *node, LVal lval,
+ ResultLoc *result_loc)
+{
assert(node->type == NodeTypeForExpr);
AstNode *array_node = node->data.for_expr.array_expr;
@@ -6043,7 +6045,10 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
IrInstruction *cond = ir_build_bin_op(irb, parent_scope, node, IrBinOpCmpLessThan, index_val, len_val, false);
IrBasicBlock *after_cond_block = irb->current_basic_block;
IrInstruction *void_else_value = else_node ? nullptr : ir_mark_gen(ir_build_const_void(irb, parent_scope, node));
- ir_mark_gen(ir_build_cond_br(irb, parent_scope, node, cond, body_block, else_block, is_comptime));
+ IrInstruction *cond_br_inst = ir_mark_gen(ir_build_cond_br(irb, parent_scope, node, cond,
+ body_block, else_block, is_comptime));
+
+ ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, else_block, end_block, result_loc);
ir_set_cursor_at_end_and_append_block(irb, body_block);
IrInstruction *elem_ptr = ir_build_elem_ptr(irb, parent_scope, node, array_val_ptr, index_val, false, PtrLenSingle);
@@ -6064,7 +6069,12 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
loop_scope->is_comptime = is_comptime;
loop_scope->incoming_blocks = &incoming_blocks;
loop_scope->incoming_values = &incoming_values;
+ loop_scope->lval = lval;
+ loop_scope->result_loc = &peer_parent->peers[0].base;
+ // Note the body block of the loop is not the place that lval and result_loc are used -
+ // it's actually in break statements, handled similarly to return statements.
+ // That is why we set those values in loop_scope above and not in this ir_gen_node call.
IrInstruction *body_result = ir_gen_node(irb, body_node, &loop_scope->base);
if (!instr_is_unreachable(body_result)) {
@@ -6081,7 +6091,7 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
if (else_node) {
ir_set_cursor_at_end_and_append_block(irb, else_block);
- else_result = ir_gen_node(irb, else_node, parent_scope);
+ else_result = ir_gen_node_extra(irb, else_node, parent_scope, lval, &peer_parent->peers[1].base);
if (else_result == irb->codegen->invalid_instruction)
return else_result;
if (!instr_is_unreachable(else_result))
@@ -6098,7 +6108,8 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
incoming_values.append(void_else_value);
}
- return ir_build_phi(irb, parent_scope, node, incoming_blocks.length, incoming_blocks.items, incoming_values.items);
+ IrInstruction *phi = ir_build_phi(irb, parent_scope, node, incoming_blocks.length, incoming_blocks.items, incoming_values.items);
+ return ir_expr_wrap(irb, parent_scope, phi, result_loc);
}
static IrInstruction *ir_gen_bool_literal(IrBuilder *irb, Scope *scope, AstNode *node) {
@@ -7866,7 +7877,7 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
case NodeTypeWhileExpr:
return ir_gen_while_expr(irb, scope, node, lval, result_loc);
case NodeTypeForExpr:
- return ir_lval_wrap(irb, scope, ir_gen_for_expr(irb, scope, node), lval, result_loc);
+ return ir_gen_for_expr(irb, scope, node, lval, result_loc);
case NodeTypeArrayAccessExpr:
return ir_gen_array_access(irb, scope, node, lval, result_loc);
case NodeTypeReturnExpr: