authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-10 16:20:13-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-10 16:20:13-04:00
logeaa9d8bdac7f64e38a39607e5d5574f88f8fe875
tree4359e185df20b115b019693468d6ac34e2994040
parentc36289511629e01bbd32bc5f1133f5ed5997d1e0
signature Commit is signed but in an unrecognized format.

result location semantics for optional wrap


4 files changed, 34 insertions(+), 27 deletions(-)

src/all_types.hpp+2-2
......@@ -3095,8 +3095,8 @@ struct IrInstructionUnwrapErrPayload {
30953095struct IrInstructionOptionalWrap {
30963096 IrInstruction base;
30973097
3098 IrInstruction *value;
3099 LLVMValueRef tmp_ptr;
3098 IrInstruction *operand;
3099 IrInstruction *result_loc;
31003100};
31013101
31023102struct IrInstructionErrWrapPayload {
src/codegen.cpp+5-8
......@@ -4956,20 +4956,20 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I
49564956 return LLVMConstInt(LLVMInt1Type(), 1, false);
49574957 }
49584958
4959 LLVMValueRef payload_val = ir_llvm_value(g, instruction->value);
4959 LLVMValueRef payload_val = ir_llvm_value(g, instruction->operand);
49604960 if (!handle_is_ptr(wanted_type)) {
49614961 return payload_val;
49624962 }
49634963
4964 assert(instruction->tmp_ptr);
4964 LLVMValueRef result_loc = ir_llvm_value(g, instruction->result_loc);
49654965
4966 LLVMValueRef val_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, maybe_child_index, "");
4966 LLVMValueRef val_ptr = LLVMBuildStructGEP(g->builder, result_loc, maybe_child_index, "");
49674967 // child_type and instruction->value->value.type may differ by constness
49684968 gen_assign_raw(g, val_ptr, get_pointer_to_type(g, child_type, false), payload_val);
4969 LLVMValueRef maybe_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, maybe_null_index, "");
4969 LLVMValueRef maybe_ptr = LLVMBuildStructGEP(g->builder, result_loc, maybe_null_index, "");
49704970 gen_store_untyped(g, LLVMConstAllOnes(LLVMInt1Type()), maybe_ptr, 0, false);
49714971
4972 return instruction->tmp_ptr;
4972 return result_loc;
49734973}
49744974
49754975static LLVMValueRef ir_render_err_wrap_code(CodeGen *g, IrExecutable *executable, IrInstructionErrWrapCode *instruction) {
......@@ -6840,9 +6840,6 @@ static void do_code_gen(CodeGen *g) {
68406840 slot = &ref_instruction->tmp_ptr;
68416841 assert(instruction->value.type->id == ZigTypeIdPointer);
68426842 slot_type = instruction->value.type->data.pointer.child_type;
6843 } else if (instruction->id == IrInstructionIdOptionalWrap) {
6844 IrInstructionOptionalWrap *maybe_wrap_instruction = (IrInstructionOptionalWrap *)instruction;
6845 slot = &maybe_wrap_instruction->tmp_ptr;
68466843 } else if (instruction->id == IrInstructionIdErrWrapPayload) {
68476844 IrInstructionErrWrapPayload *err_wrap_payload_instruction = (IrInstructionErrWrapPayload *)instruction;
68486845 slot = &err_wrap_payload_instruction->tmp_ptr;
src/ir.cpp+21-12
......@@ -1758,11 +1758,17 @@ static IrInstruction *ir_build_optional_unwrap_ptr(IrBuilder *irb, Scope *scope,
17581758 return &instruction->base;
17591759}
17601760
1761static IrInstruction *ir_build_maybe_wrap(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {
1762 IrInstructionOptionalWrap *instruction = ir_build_instruction<IrInstructionOptionalWrap>(irb, scope, source_node);
1763 instruction->value = value;
1761static IrInstruction *ir_build_optional_wrap(IrAnalyze *ira, IrInstruction *source_instruction, ZigType *result_ty,
1762 IrInstruction *operand, IrInstruction *result_loc)
1763{
1764 IrInstructionOptionalWrap *instruction = ir_build_instruction<IrInstructionOptionalWrap>(
1765 &ira->new_irb, source_instruction->scope, source_instruction->source_node);
1766 instruction->base.value.type = result_ty;
1767 instruction->operand = operand;
1768 instruction->result_loc = result_loc;
17641769
1765 ir_ref_instruction(value, irb->current_basic_block);
1770 ir_ref_instruction(operand, ira->new_irb.current_basic_block);
1771 ir_ref_instruction(result_loc, ira->new_irb.current_basic_block);
17661772
17671773 return &instruction->base;
17681774}
......@@ -11035,7 +11041,7 @@ static ZigFn *ir_resolve_fn(IrAnalyze *ira, IrInstruction *fn_value) {
1103511041}
1103611042
1103711043static IrInstruction *ir_analyze_optional_wrap(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value,
11038 ZigType *wanted_type)
11044 ZigType *wanted_type, ResultLoc *result_loc)
1103911045{
1104011046 assert(wanted_type->id == ZigTypeIdOptional);
1104111047
......@@ -11061,10 +11067,13 @@ static IrInstruction *ir_analyze_optional_wrap(IrAnalyze *ira, IrInstruction *so
1106111067 return &const_instruction->base;
1106211068 }
1106311069
11064 IrInstruction *result = ir_build_maybe_wrap(&ira->new_irb, source_instr->scope, source_instr->source_node, value);
11065 result->value.type = wanted_type;
11070 if (result_loc == nullptr) result_loc = no_result_loc();
11071 IrInstruction *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr);
11072 if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) {
11073 return result_loc_inst;
11074 }
11075 IrInstruction *result = ir_build_optional_wrap(ira, source_instr, wanted_type, value, result_loc_inst);
1106611076 result->value.data.rh_maybe = RuntimeHintOptionalNonNull;
11067 ir_add_alloca(ira, result, wanted_type);
1106811077 return result;
1106911078}
1107011079
......@@ -12009,12 +12018,12 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1200912018 if (types_match_const_cast_only(ira, wanted_child_type, actual_type, source_node,
1201012019 false).id == ConstCastResultIdOk)
1201112020 {
12012 return ir_analyze_optional_wrap(ira, source_instr, value, wanted_type);
12021 return ir_analyze_optional_wrap(ira, source_instr, value, wanted_type, result_loc);
1201312022 } else if (actual_type->id == ZigTypeIdComptimeInt ||
1201412023 actual_type->id == ZigTypeIdComptimeFloat)
1201512024 {
1201612025 if (ir_num_lit_fits_in_other_type(ira, value, wanted_child_type, true)) {
12017 return ir_analyze_optional_wrap(ira, source_instr, value, wanted_type);
12026 return ir_analyze_optional_wrap(ira, source_instr, value, wanted_type, result_loc);
1201812027 } else {
1201912028 return ira->codegen->invalid_instruction;
1202012029 }
......@@ -12038,7 +12047,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1203812047 wanted_child_type);
1203912048 if (type_is_invalid(cast1->value.type))
1204012049 return ira->codegen->invalid_instruction;
12041 return ir_analyze_optional_wrap(ira, source_instr, cast1, wanted_type);
12050 return ir_analyze_optional_wrap(ira, source_instr, cast1, wanted_type, result_loc);
1204212051 }
1204312052 }
1204412053 }
......@@ -24387,6 +24396,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2438724396 case IrInstructionIdEndExpr:
2438824397 case IrInstructionIdPtrOfArrayToSlice:
2438924398 case IrInstructionIdSliceGen:
24399 case IrInstructionIdOptionalWrap:
2439024400 return true;
2439124401
2439224402 case IrInstructionIdPhi:
......@@ -24436,7 +24446,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2443624446 case IrInstructionIdHandle:
2443724447 case IrInstructionIdTestErr:
2443824448 case IrInstructionIdUnwrapErrCode:
24439 case IrInstructionIdOptionalWrap:
2444024449 case IrInstructionIdErrWrapCode:
2444124450 case IrInstructionIdErrWrapPayload:
2444224451 case IrInstructionIdFnProto:
src/ir_print.cpp+6-5
......@@ -970,10 +970,11 @@ static void ir_print_unwrap_err_payload(IrPrint *irp, IrInstructionUnwrapErrPayl
970970 }
971971}
972972
973static void ir_print_maybe_wrap(IrPrint *irp, IrInstructionOptionalWrap *instruction) {
974 fprintf(irp->f, "@maybeWrap(");
975 ir_print_other_instruction(irp, instruction->value);
976 fprintf(irp->f, ")");
973static void ir_print_optional_wrap(IrPrint *irp, IrInstructionOptionalWrap *instruction) {
974 fprintf(irp->f, "@optionalWrap(");
975 ir_print_other_instruction(irp, instruction->operand);
976 fprintf(irp->f, ")result=");
977 ir_print_other_instruction(irp, instruction->result_loc);
977978}
978979
979980static void ir_print_err_wrap_code(IrPrint *irp, IrInstructionErrWrapCode *instruction) {
......@@ -1820,7 +1821,7 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
18201821 ir_print_unwrap_err_payload(irp, (IrInstructionUnwrapErrPayload *)instruction);
18211822 break;
18221823 case IrInstructionIdOptionalWrap:
1823 ir_print_maybe_wrap(irp, (IrInstructionOptionalWrap *)instruction);
1824 ir_print_optional_wrap(irp, (IrInstructionOptionalWrap *)instruction);
18241825 break;
18251826 case IrInstructionIdErrWrapCode:
18261827 ir_print_err_wrap_code(irp, (IrInstructionErrWrapCode *)instruction);