authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-07 17:37:29-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-07 17:37:29-04:00
logb1efba0c7011f8f5acb31ca3c384c3d2750ac669
treea0ff5b9906d743508b950a3f91d2509708a2e269
parent2b0a1b7b1438cfc561276fa56356e5dbaa359c89
signature Commit is signed but in an unrecognized format.

hook up peer result locs to catch

```zig export fn entry() void { var x = crap() catch bar(); } ``` ```llvm define void @entry() #2 !dbg !40 { Entry: %0 = alloca { i16, %Foo }, align 4 %x = alloca %Foo, align 4 call fastcc void @crap({ i16, %Foo }* sret %0), !dbg !50 %1 = getelementptr inbounds { i16, %Foo }, { i16, %Foo }* %0, i32 0, i32 0, !dbg !51 %2 = load i16, i16* %1, align 2, !dbg !51 %3 = icmp ne i16 %2, 0, !dbg !51 br i1 %3, label %UnwrapErrError, label %UnwrapErrOk, !dbg !51 UnwrapErrError: ; preds = %Entry call fastcc void @bar(%Foo* sret %x), !dbg !52 br label %UnwrapErrEnd, !dbg !51 UnwrapErrOk: ; preds = %Entry %4 = getelementptr inbounds { i16, %Foo }, { i16, %Foo }* %0, i32 0, i32 1, !dbg !51 %5 = bitcast %Foo* %4 to i8*, !dbg !51 %6 = bitcast %Foo* %x to i8*, !dbg !51 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %6, i8* align 4 %5, i64 4, i1 false), !dbg !51 br label %UnwrapErrEnd, !dbg !51 UnwrapErrEnd: ; preds = %UnwrapErrOk, %UnwrapErrError ret void, !dbg !53 } ```

2 files changed, 33 insertions(+), 13 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 catch
54 * struct initializations
65 * function call parameters
76 * bitCast
src/ir.cpp+33-12
......@@ -5486,8 +5486,8 @@ static IrInstruction *ir_gen_pointer_type(IrBuilder *irb, Scope *scope, AstNode
54865486 ptr_len, align_value, bit_offset_start, host_int_bytes, is_allow_zero);
54875487}
54885488
5489static IrInstruction *ir_gen_catch_unreachable(IrBuilder *irb, Scope *scope, AstNode *source_node, AstNode *expr_node,
5490 LVal lval)
5489static IrInstruction *ir_gen_catch_unreachable(IrBuilder *irb, Scope *scope, AstNode *source_node,
5490 AstNode *expr_node, LVal lval, ResultLoc *result_loc)
54915491{
54925492 IrInstruction *err_union_ptr = ir_gen_node_extra(irb, expr_node, scope, LValPtr, nullptr);
54935493 if (err_union_ptr == irb->codegen->invalid_instruction)
......@@ -5500,7 +5500,8 @@ static IrInstruction *ir_gen_catch_unreachable(IrBuilder *irb, Scope *scope, Ast
55005500 if (lval == LValPtr)
55015501 return payload_ptr;
55025502
5503 return ir_build_load_ptr(irb, scope, source_node, payload_ptr);
5503 IrInstruction *load_ptr = ir_build_load_ptr(irb, scope, source_node, payload_ptr);
5504 return ir_expr_wrap(irb, scope, load_ptr, result_loc);
55045505}
55055506
55065507static IrInstruction *ir_gen_bool_not(IrBuilder *irb, Scope *scope, AstNode *node) {
......@@ -6872,6 +6873,7 @@ static IrInstruction *ir_gen_comptime(IrBuilder *irb, Scope *parent_scope, AstNo
68726873 assert(node->type == NodeTypeCompTime);
68736874
68746875 Scope *child_scope = create_comptime_scope(irb->codegen, node, parent_scope);
6876 // purposefully pass null for result_loc and let EndExpr handle it
68756877 return ir_gen_node_extra(irb, node->data.comptime_expr.expr, child_scope, lval, nullptr);
68766878}
68776879
......@@ -7072,7 +7074,9 @@ static IrInstruction *ir_gen_slice(IrBuilder *irb, Scope *scope, AstNode *node)
70727074 return ir_build_slice(irb, scope, node, ptr_value, start_value, end_value, true);
70737075}
70747076
7075static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode *node) {
7077static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode *node, LVal lval,
7078 ResultLoc *result_loc)
7079{
70767080 assert(node->type == NodeTypeUnwrapErrorExpr);
70777081
70787082 AstNode *op1_node = node->data.unwrap_err_expr.op1;
......@@ -7086,7 +7090,7 @@ static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode
70867090 add_node_error(irb->codegen, var_node, buf_sprintf("unused variable: '%s'", buf_ptr(var_name)));
70877091 return irb->codegen->invalid_instruction;
70887092 }
7089 return ir_gen_catch_unreachable(irb, parent_scope, node, op1_node, LValNone);
7093 return ir_gen_catch_unreachable(irb, parent_scope, node, op1_node, lval, result_loc);
70907094 }
70917095
70927096
......@@ -7107,7 +7111,9 @@ static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode
71077111 IrBasicBlock *ok_block = ir_create_basic_block(irb, parent_scope, "UnwrapErrOk");
71087112 IrBasicBlock *err_block = ir_create_basic_block(irb, parent_scope, "UnwrapErrError");
71097113 IrBasicBlock *end_block = ir_create_basic_block(irb, parent_scope, "UnwrapErrEnd");
7110 ir_build_cond_br(irb, parent_scope, node, is_err, err_block, ok_block, is_comptime);
7114 IrInstruction *cond_br_inst = ir_build_cond_br(irb, parent_scope, node, is_err, err_block, ok_block, is_comptime);
7115
7116 ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, ok_block, end_block, result_loc);
71117117
71127118 ir_set_cursor_at_end_and_append_block(irb, err_block);
71137119 Scope *err_scope;
......@@ -7124,7 +7130,7 @@ static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode
71247130 } else {
71257131 err_scope = parent_scope;
71267132 }
7127 IrInstruction *err_result = ir_gen_node(irb, op2_node, err_scope);
7133 IrInstruction *err_result = ir_gen_node_extra(irb, op2_node, err_scope, lval, &peer_parent->peers[0].base);
71287134 if (err_result == irb->codegen->invalid_instruction)
71297135 return irb->codegen->invalid_instruction;
71307136 IrBasicBlock *after_err_block = irb->current_basic_block;
......@@ -7134,6 +7140,7 @@ static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode
71347140 ir_set_cursor_at_end_and_append_block(irb, ok_block);
71357141 IrInstruction *unwrapped_ptr = ir_build_unwrap_err_payload(irb, parent_scope, node, err_union_ptr, false);
71367142 IrInstruction *unwrapped_payload = ir_build_load_ptr(irb, parent_scope, node, unwrapped_ptr);
7143 ir_build_end_expr(irb, parent_scope, node, unwrapped_payload, &peer_parent->peers[1].base);
71377144 IrBasicBlock *after_ok_block = irb->current_basic_block;
71387145 ir_build_br(irb, parent_scope, node, end_block, is_comptime);
71397146
......@@ -7144,7 +7151,8 @@ static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode
71447151 IrBasicBlock **incoming_blocks = allocate<IrBasicBlock *>(2);
71457152 incoming_blocks[0] = after_err_block;
71467153 incoming_blocks[1] = after_ok_block;
7147 return ir_build_phi(irb, parent_scope, node, 2, incoming_blocks, incoming_values);
7154 IrInstruction *phi = ir_build_phi(irb, parent_scope, node, 2, incoming_blocks, incoming_values);
7155 return ir_lval_wrap(irb, parent_scope, phi, lval, result_loc);
71487156}
71497157
71507158static bool render_instance_name_recursive(CodeGen *codegen, Buf *name, Scope *outer_scope, Scope *inner_scope) {
......@@ -7941,7 +7949,7 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
79417949 case NodeTypeSwitchExpr:
79427950 return ir_gen_switch_expr(irb, scope, node, lval, result_loc);
79437951 case NodeTypeCompTime:
7944 return ir_gen_comptime(irb, scope, node, lval);
7952 return ir_expr_wrap(irb, scope, ir_gen_comptime(irb, scope, node, lval), result_loc);
79457953 case NodeTypeErrorType:
79467954 return ir_lval_wrap(irb, scope, ir_gen_error_type(irb, scope, node), lval, result_loc);
79477955 case NodeTypeBreak:
......@@ -7955,7 +7963,7 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
79557963 case NodeTypeSliceExpr:
79567964 return ir_lval_wrap(irb, scope, ir_gen_slice(irb, scope, node), lval, result_loc);
79577965 case NodeTypeUnwrapErrorExpr:
7958 return ir_lval_wrap(irb, scope, ir_gen_catch(irb, scope, node), lval, result_loc);
7966 return ir_gen_catch(irb, scope, node, lval, result_loc);
79597967 case NodeTypeContainerDecl:
79607968 return ir_lval_wrap(irb, scope, ir_gen_container_decl(irb, scope, node), lval, result_loc);
79617969 case NodeTypeFnProto:
......@@ -14485,8 +14493,21 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s
1448514493 case ResultLocIdInvalid:
1448614494 case ResultLocIdPeerParent:
1448714495 zig_unreachable();
14488 case ResultLocIdNone:
14489 return nullptr;
14496 case ResultLocIdNone: {
14497 if (value != nullptr) {
14498 return nullptr;
14499 }
14500 // need to return a result location and don't have one. use a stack allocation
14501 IrInstructionAllocaGen *alloca_gen = ir_create_alloca_gen(ira, suspend_source_instr, 0, "");
14502 alloca_gen->base.value.type = get_pointer_to_type_extra(ira->codegen, value_type, false, false,
14503 PtrLenSingle, 0, 0, 0, false);
14504 ZigFn *fn_entry = exec_fn_entry(ira->new_irb.exec);
14505 if (fn_entry != nullptr) {
14506 fn_entry->alloca_gen_list.append(alloca_gen);
14507 }
14508 result_loc->resolved_loc = &alloca_gen->base;
14509 return result_loc->resolved_loc;
14510 }
1449014511 case ResultLocIdVar: {
1449114512 ResultLocVar *result_loc_var = reinterpret_cast<ResultLocVar *>(result_loc);
1449214513 assert(result_loc->source_instruction->id == IrInstructionIdAllocaSrc);