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 {...@@ -3095,8 +3095,8 @@ struct IrInstructionUnwrapErrPayload {
3095struct IrInstructionOptionalWrap {3095struct IrInstructionOptionalWrap {
3096 IrInstruction base;3096 IrInstruction base;
30973097
3098 IrInstruction *value;3098 IrInstruction *operand;
3099 LLVMValueRef tmp_ptr;3099 IrInstruction *result_loc;
3100};3100};
31013101
3102struct IrInstructionErrWrapPayload {3102struct IrInstructionErrWrapPayload {
src/codegen.cpp+5-8
...@@ -4956,20 +4956,20 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I...@@ -4956,20 +4956,20 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I
4956 return LLVMConstInt(LLVMInt1Type(), 1, false);4956 return LLVMConstInt(LLVMInt1Type(), 1, false);
4957 }4957 }
49584958
4959 LLVMValueRef payload_val = ir_llvm_value(g, instruction->value);4959 LLVMValueRef payload_val = ir_llvm_value(g, instruction->operand);
4960 if (!handle_is_ptr(wanted_type)) {4960 if (!handle_is_ptr(wanted_type)) {
4961 return payload_val;4961 return payload_val;
4962 }4962 }
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, "");
4967 // child_type and instruction->value->value.type may differ by constness4967 // child_type and instruction->value->value.type may differ by constness
4968 gen_assign_raw(g, val_ptr, get_pointer_to_type(g, child_type, false), payload_val);4968 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, "");
4970 gen_store_untyped(g, LLVMConstAllOnes(LLVMInt1Type()), maybe_ptr, 0, false);4970 gen_store_untyped(g, LLVMConstAllOnes(LLVMInt1Type()), maybe_ptr, 0, false);
49714971
4972 return instruction->tmp_ptr;4972 return result_loc;
4973}4973}
49744974
4975static LLVMValueRef ir_render_err_wrap_code(CodeGen *g, IrExecutable *executable, IrInstructionErrWrapCode *instruction) {4975static LLVMValueRef ir_render_err_wrap_code(CodeGen *g, IrExecutable *executable, IrInstructionErrWrapCode *instruction) {
...@@ -6840,9 +6840,6 @@ static void do_code_gen(CodeGen *g) {...@@ -6840,9 +6840,6 @@ static void do_code_gen(CodeGen *g) {
6840 slot = &ref_instruction->tmp_ptr;6840 slot = &ref_instruction->tmp_ptr;
6841 assert(instruction->value.type->id == ZigTypeIdPointer);6841 assert(instruction->value.type->id == ZigTypeIdPointer);
6842 slot_type = instruction->value.type->data.pointer.child_type;6842 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;
6846 } else if (instruction->id == IrInstructionIdErrWrapPayload) {6843 } else if (instruction->id == IrInstructionIdErrWrapPayload) {
6847 IrInstructionErrWrapPayload *err_wrap_payload_instruction = (IrInstructionErrWrapPayload *)instruction;6844 IrInstructionErrWrapPayload *err_wrap_payload_instruction = (IrInstructionErrWrapPayload *)instruction;
6848 slot = &err_wrap_payload_instruction->tmp_ptr;6845 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,...@@ -1758,11 +1758,17 @@ static IrInstruction *ir_build_optional_unwrap_ptr(IrBuilder *irb, Scope *scope,
1758 return &instruction->base;1758 return &instruction->base;
1759}1759}
17601760
1761static IrInstruction *ir_build_maybe_wrap(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) {1761static IrInstruction *ir_build_optional_wrap(IrAnalyze *ira, IrInstruction *source_instruction, ZigType *result_ty,
1762 IrInstructionOptionalWrap *instruction = ir_build_instruction<IrInstructionOptionalWrap>(irb, scope, source_node);1762 IrInstruction *operand, IrInstruction *result_loc)
1763 instruction->value = value;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
1767 return &instruction->base;1773 return &instruction->base;
1768}1774}
...@@ -11035,7 +11041,7 @@ static ZigFn *ir_resolve_fn(IrAnalyze *ira, IrInstruction *fn_value) {...@@ -11035,7 +11041,7 @@ static ZigFn *ir_resolve_fn(IrAnalyze *ira, IrInstruction *fn_value) {
11035}11041}
1103611042
11037static IrInstruction *ir_analyze_optional_wrap(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value,11043static IrInstruction *ir_analyze_optional_wrap(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value,
11038 ZigType *wanted_type)11044 ZigType *wanted_type, ResultLoc *result_loc)
11039{11045{
11040 assert(wanted_type->id == ZigTypeIdOptional);11046 assert(wanted_type->id == ZigTypeIdOptional);
1104111047
...@@ -11061,10 +11067,13 @@ static IrInstruction *ir_analyze_optional_wrap(IrAnalyze *ira, IrInstruction *so...@@ -11061,10 +11067,13 @@ static IrInstruction *ir_analyze_optional_wrap(IrAnalyze *ira, IrInstruction *so
11061 return &const_instruction->base;11067 return &const_instruction->base;
11062 }11068 }
1106311069
11064 IrInstruction *result = ir_build_maybe_wrap(&ira->new_irb, source_instr->scope, source_instr->source_node, value);11070 if (result_loc == nullptr) result_loc = no_result_loc();
11065 result->value.type = wanted_type;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);
11066 result->value.data.rh_maybe = RuntimeHintOptionalNonNull;11076 result->value.data.rh_maybe = RuntimeHintOptionalNonNull;
11067 ir_add_alloca(ira, result, wanted_type);
11068 return result;11077 return result;
11069}11078}
1107011079
...@@ -12009,12 +12018,12 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -12009,12 +12018,12 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
12009 if (types_match_const_cast_only(ira, wanted_child_type, actual_type, source_node,12018 if (types_match_const_cast_only(ira, wanted_child_type, actual_type, source_node,
12010 false).id == ConstCastResultIdOk)12019 false).id == ConstCastResultIdOk)
12011 {12020 {
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);
12013 } else if (actual_type->id == ZigTypeIdComptimeInt ||12022 } else if (actual_type->id == ZigTypeIdComptimeInt ||
12014 actual_type->id == ZigTypeIdComptimeFloat)12023 actual_type->id == ZigTypeIdComptimeFloat)
12015 {12024 {
12016 if (ir_num_lit_fits_in_other_type(ira, value, wanted_child_type, true)) {12025 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);
12018 } else {12027 } else {
12019 return ira->codegen->invalid_instruction;12028 return ira->codegen->invalid_instruction;
12020 }12029 }
...@@ -12038,7 +12047,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -12038,7 +12047,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
12038 wanted_child_type);12047 wanted_child_type);
12039 if (type_is_invalid(cast1->value.type))12048 if (type_is_invalid(cast1->value.type))
12040 return ira->codegen->invalid_instruction;12049 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);
12042 }12051 }
12043 }12052 }
12044 }12053 }
...@@ -24387,6 +24396,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {...@@ -24387,6 +24396,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
24387 case IrInstructionIdEndExpr:24396 case IrInstructionIdEndExpr:
24388 case IrInstructionIdPtrOfArrayToSlice:24397 case IrInstructionIdPtrOfArrayToSlice:
24389 case IrInstructionIdSliceGen:24398 case IrInstructionIdSliceGen:
24399 case IrInstructionIdOptionalWrap:
24390 return true;24400 return true;
2439124401
24392 case IrInstructionIdPhi:24402 case IrInstructionIdPhi:
...@@ -24436,7 +24446,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {...@@ -24436,7 +24446,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
24436 case IrInstructionIdHandle:24446 case IrInstructionIdHandle:
24437 case IrInstructionIdTestErr:24447 case IrInstructionIdTestErr:
24438 case IrInstructionIdUnwrapErrCode:24448 case IrInstructionIdUnwrapErrCode:
24439 case IrInstructionIdOptionalWrap:
24440 case IrInstructionIdErrWrapCode:24449 case IrInstructionIdErrWrapCode:
24441 case IrInstructionIdErrWrapPayload:24450 case IrInstructionIdErrWrapPayload:
24442 case IrInstructionIdFnProto:24451 case IrInstructionIdFnProto:
src/ir_print.cpp+6-5
...@@ -970,10 +970,11 @@ static void ir_print_unwrap_err_payload(IrPrint *irp, IrInstructionUnwrapErrPayl...@@ -970,10 +970,11 @@ static void ir_print_unwrap_err_payload(IrPrint *irp, IrInstructionUnwrapErrPayl
970 }970 }
971}971}
972972
973static void ir_print_maybe_wrap(IrPrint *irp, IrInstructionOptionalWrap *instruction) {973static void ir_print_optional_wrap(IrPrint *irp, IrInstructionOptionalWrap *instruction) {
974 fprintf(irp->f, "@maybeWrap(");974 fprintf(irp->f, "@optionalWrap(");
975 ir_print_other_instruction(irp, instruction->value);975 ir_print_other_instruction(irp, instruction->operand);
976 fprintf(irp->f, ")");976 fprintf(irp->f, ")result=");
977 ir_print_other_instruction(irp, instruction->result_loc);
977}978}
978979
979static void ir_print_err_wrap_code(IrPrint *irp, IrInstructionErrWrapCode *instruction) {980static void ir_print_err_wrap_code(IrPrint *irp, IrInstructionErrWrapCode *instruction) {
...@@ -1820,7 +1821,7 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {...@@ -1820,7 +1821,7 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
1820 ir_print_unwrap_err_payload(irp, (IrInstructionUnwrapErrPayload *)instruction);1821 ir_print_unwrap_err_payload(irp, (IrInstructionUnwrapErrPayload *)instruction);
1821 break;1822 break;
1822 case IrInstructionIdOptionalWrap:1823 case IrInstructionIdOptionalWrap:
1823 ir_print_maybe_wrap(irp, (IrInstructionOptionalWrap *)instruction);1824 ir_print_optional_wrap(irp, (IrInstructionOptionalWrap *)instruction);
1824 break;1825 break;
1825 case IrInstructionIdErrWrapCode:1826 case IrInstructionIdErrWrapCode:
1826 ir_print_err_wrap_code(irp, (IrInstructionErrWrapCode *)instruction);1827 ir_print_err_wrap_code(irp, (IrInstructionErrWrapCode *)instruction);