authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-07 15:58:18-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-07 15:58:18-04:00
log2b0a1b7b1438cfc561276fa56356e5dbaa359c89
tree9ef27675444af88677ba3abcdb0be8357de65bb6
parentede3436b087223f88c65124a12b7d477d792748d
signature Commit is signed but in an unrecognized format.

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 } ```

2 files changed, 16 insertions(+), 6 deletions(-)

BRANCH_TODO-1
......@@ -1,7 +1,6 @@
11Scratch pad for stuff to do before merging master
22=================================================
33
4 * hook up peer result locs to for
54 * hook up peer result locs to catch
65 * struct initializations
76 * function call parameters
src/ir.cpp+16-5
......@@ -5983,7 +5983,9 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
59835983 }
59845984}
59855985
5986static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNode *node) {
5986static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNode *node, LVal lval,
5987 ResultLoc *result_loc)
5988{
59875989 assert(node->type == NodeTypeForExpr);
59885990
59895991 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
60436045 IrInstruction *cond = ir_build_bin_op(irb, parent_scope, node, IrBinOpCmpLessThan, index_val, len_val, false);
60446046 IrBasicBlock *after_cond_block = irb->current_basic_block;
60456047 IrInstruction *void_else_value = else_node ? nullptr : ir_mark_gen(ir_build_const_void(irb, parent_scope, node));
6046 ir_mark_gen(ir_build_cond_br(irb, parent_scope, node, cond, body_block, else_block, is_comptime));
6048 IrInstruction *cond_br_inst = ir_mark_gen(ir_build_cond_br(irb, parent_scope, node, cond,
6049 body_block, else_block, is_comptime));
6050
6051 ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, else_block, end_block, result_loc);
60476052
60486053 ir_set_cursor_at_end_and_append_block(irb, body_block);
60496054 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
60646069 loop_scope->is_comptime = is_comptime;
60656070 loop_scope->incoming_blocks = &incoming_blocks;
60666071 loop_scope->incoming_values = &incoming_values;
6072 loop_scope->lval = lval;
6073 loop_scope->result_loc = &peer_parent->peers[0].base;
60676074
6075 // Note the body block of the loop is not the place that lval and result_loc are used -
6076 // it's actually in break statements, handled similarly to return statements.
6077 // That is why we set those values in loop_scope above and not in this ir_gen_node call.
60686078 IrInstruction *body_result = ir_gen_node(irb, body_node, &loop_scope->base);
60696079
60706080 if (!instr_is_unreachable(body_result)) {
......@@ -6081,7 +6091,7 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
60816091 if (else_node) {
60826092 ir_set_cursor_at_end_and_append_block(irb, else_block);
60836093
6084 else_result = ir_gen_node(irb, else_node, parent_scope);
6094 else_result = ir_gen_node_extra(irb, else_node, parent_scope, lval, &peer_parent->peers[1].base);
60856095 if (else_result == irb->codegen->invalid_instruction)
60866096 return else_result;
60876097 if (!instr_is_unreachable(else_result))
......@@ -6098,7 +6108,8 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
60986108 incoming_values.append(void_else_value);
60996109 }
61006110
6101 return ir_build_phi(irb, parent_scope, node, incoming_blocks.length, incoming_blocks.items, incoming_values.items);
6111 IrInstruction *phi = ir_build_phi(irb, parent_scope, node, incoming_blocks.length, incoming_blocks.items, incoming_values.items);
6112 return ir_expr_wrap(irb, parent_scope, phi, result_loc);
61026113}
61036114
61046115static 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
78667877 case NodeTypeWhileExpr:
78677878 return ir_gen_while_expr(irb, scope, node, lval, result_loc);
78687879 case NodeTypeForExpr:
7869 return ir_lval_wrap(irb, scope, ir_gen_for_expr(irb, scope, node), lval, result_loc);
7880 return ir_gen_for_expr(irb, scope, node, lval, result_loc);
78707881 case NodeTypeArrayAccessExpr:
78717882 return ir_gen_array_access(irb, scope, node, lval, result_loc);
78727883 case NodeTypeReturnExpr: