authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-10 18:34:27-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-10 18:34:27-04:00
log65f6ea66f4a86c45004547bb5ac079b8286f980e
treebfdf39c9185e30be44ad262ea78e9c78386e25b3
parentee3f7e20f64d715ff22eeff0b7b355bca1981ea1
signaturelock-open Commit is signed but in an unrecognized format.

result loc semantics for `@sliceToBytes` and `@bytesToSlice`


4 files changed, 39 insertions(+), 23 deletions(-)

src/all_types.hpp+3-1
...@@ -2635,7 +2635,7 @@ struct IrInstructionResizeSlice {...@@ -2635,7 +2635,7 @@ struct IrInstructionResizeSlice {
2635 IrInstruction base;2635 IrInstruction base;
26362636
2637 IrInstruction *operand;2637 IrInstruction *operand;
2638 LLVMValueRef tmp_ptr;2638 IrInstruction *result_loc;
2639};2639};
26402640
2641struct IrInstructionContainerInitList {2641struct IrInstructionContainerInitList {
...@@ -2925,6 +2925,7 @@ struct IrInstructionToBytes {...@@ -2925,6 +2925,7 @@ struct IrInstructionToBytes {
2925 IrInstruction base;2925 IrInstruction base;
29262926
2927 IrInstruction *target;2927 IrInstruction *target;
2928 ResultLoc *result_loc;
2928};2929};
29292930
2930struct IrInstructionFromBytes {2931struct IrInstructionFromBytes {
...@@ -2932,6 +2933,7 @@ struct IrInstructionFromBytes {...@@ -2932,6 +2933,7 @@ struct IrInstructionFromBytes {
29322933
2933 IrInstruction *dest_child_type;2934 IrInstruction *dest_child_type;
2934 IrInstruction *target;2935 IrInstruction *target;
2936 ResultLoc *result_loc;
2935};2937};
29362938
2937struct IrInstructionIntToFloat {2939struct IrInstructionIntToFloat {
src/codegen.cpp+4-9
...@@ -2943,7 +2943,7 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,...@@ -2943,7 +2943,7 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,
2943 LLVMValueRef expr_val = ir_llvm_value(g, instruction->operand);2943 LLVMValueRef expr_val = ir_llvm_value(g, instruction->operand);
2944 assert(expr_val);2944 assert(expr_val);
29452945
2946 assert(instruction->tmp_ptr);2946 LLVMValueRef result_loc = ir_llvm_value(g, instruction->result_loc);
2947 assert(wanted_type->id == ZigTypeIdStruct);2947 assert(wanted_type->id == ZigTypeIdStruct);
2948 assert(wanted_type->data.structure.is_slice);2948 assert(wanted_type->data.structure.is_slice);
2949 assert(actual_type->id == ZigTypeIdStruct);2949 assert(actual_type->id == ZigTypeIdStruct);
...@@ -2964,7 +2964,7 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,...@@ -2964,7 +2964,7 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,
2964 LLVMValueRef src_ptr = gen_load_untyped(g, src_ptr_ptr, 0, false, "");2964 LLVMValueRef src_ptr = gen_load_untyped(g, src_ptr_ptr, 0, false, "");
2965 LLVMValueRef src_ptr_casted = LLVMBuildBitCast(g->builder, src_ptr,2965 LLVMValueRef src_ptr_casted = LLVMBuildBitCast(g->builder, src_ptr,
2966 get_llvm_type(g, wanted_type->data.structure.fields[0].type_entry), "");2966 get_llvm_type(g, wanted_type->data.structure.fields[0].type_entry), "");
2967 LLVMValueRef dest_ptr_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,2967 LLVMValueRef dest_ptr_ptr = LLVMBuildStructGEP(g->builder, result_loc,
2968 (unsigned)wanted_ptr_index, "");2968 (unsigned)wanted_ptr_index, "");
2969 gen_store_untyped(g, src_ptr_casted, dest_ptr_ptr, 0, false);2969 gen_store_untyped(g, src_ptr_casted, dest_ptr_ptr, 0, false);
29702970
...@@ -2997,12 +2997,10 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,...@@ -2997,12 +2997,10 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,
2997 zig_unreachable();2997 zig_unreachable();
2998 }2998 }
29992999
3000 LLVMValueRef dest_len_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,3000 LLVMValueRef dest_len_ptr = LLVMBuildStructGEP(g->builder, result_loc, (unsigned)wanted_len_index, "");
3001 (unsigned)wanted_len_index, "");
3002 gen_store_untyped(g, new_len, dest_len_ptr, 0, false);3001 gen_store_untyped(g, new_len, dest_len_ptr, 0, false);
30033002
30043003 return result_loc;
3005 return instruction->tmp_ptr;
3006}3004}
30073005
3008static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,3006static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
...@@ -6840,9 +6838,6 @@ static void do_code_gen(CodeGen *g) {...@@ -6840,9 +6838,6 @@ static void do_code_gen(CodeGen *g) {
6840 slot = &ref_instruction->tmp_ptr;6838 slot = &ref_instruction->tmp_ptr;
6841 assert(instruction->value.type->id == ZigTypeIdPointer);6839 assert(instruction->value.type->id == ZigTypeIdPointer);
6842 slot_type = instruction->value.type->data.pointer.child_type;6840 slot_type = instruction->value.type->data.pointer.child_type;
6843 } else if (instruction->id == IrInstructionIdResizeSlice) {
6844 IrInstructionResizeSlice *resize_slice_instruction = (IrInstructionResizeSlice *)instruction;
6845 slot = &resize_slice_instruction->tmp_ptr;
6846 } else if (instruction->id == IrInstructionIdLoadPtrGen) {6841 } else if (instruction->id == IrInstructionIdLoadPtrGen) {
6847 IrInstructionLoadPtrGen *load_ptr_inst = (IrInstructionLoadPtrGen *)instruction;6842 IrInstructionLoadPtrGen *load_ptr_inst = (IrInstructionLoadPtrGen *)instruction;
6848 slot = &load_ptr_inst->tmp_ptr;6843 slot = &load_ptr_inst->tmp_ptr;
src/ir.cpp+30-12
...@@ -1574,14 +1574,16 @@ static IrInstruction *ir_build_var_decl_gen(IrAnalyze *ira, IrInstruction *sourc...@@ -1574,14 +1574,16 @@ static IrInstruction *ir_build_var_decl_gen(IrAnalyze *ira, IrInstruction *sourc
1574}1574}
15751575
1576static IrInstruction *ir_build_resize_slice(IrAnalyze *ira, IrInstruction *source_instruction,1576static IrInstruction *ir_build_resize_slice(IrAnalyze *ira, IrInstruction *source_instruction,
1577 IrInstruction *operand, ZigType *ty)1577 IrInstruction *operand, ZigType *ty, IrInstruction *result_loc)
1578{1578{
1579 IrInstructionResizeSlice *instruction = ir_build_instruction<IrInstructionResizeSlice>(&ira->new_irb,1579 IrInstructionResizeSlice *instruction = ir_build_instruction<IrInstructionResizeSlice>(&ira->new_irb,
1580 source_instruction->scope, source_instruction->source_node);1580 source_instruction->scope, source_instruction->source_node);
1581 instruction->base.value.type = ty;1581 instruction->base.value.type = ty;
1582 instruction->operand = operand;1582 instruction->operand = operand;
1583 instruction->result_loc = result_loc;
15831584
1584 ir_ref_instruction(operand, ira->new_irb.current_basic_block);1585 ir_ref_instruction(operand, ira->new_irb.current_basic_block);
1586 ir_ref_instruction(result_loc, ira->new_irb.current_basic_block);
15851587
1586 return &instruction->base;1588 return &instruction->base;
1587}1589}
...@@ -2131,19 +2133,25 @@ static IrInstruction *ir_build_err_set_cast(IrBuilder *irb, Scope *scope, AstNod...@@ -2131,19 +2133,25 @@ static IrInstruction *ir_build_err_set_cast(IrBuilder *irb, Scope *scope, AstNod
2131 return &instruction->base;2133 return &instruction->base;
2132}2134}
21332135
2134static IrInstruction *ir_build_to_bytes(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *target) {2136static IrInstruction *ir_build_to_bytes(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *target,
2137 ResultLoc *result_loc)
2138{
2135 IrInstructionToBytes *instruction = ir_build_instruction<IrInstructionToBytes>(irb, scope, source_node);2139 IrInstructionToBytes *instruction = ir_build_instruction<IrInstructionToBytes>(irb, scope, source_node);
2136 instruction->target = target;2140 instruction->target = target;
2141 instruction->result_loc = result_loc;
21372142
2138 ir_ref_instruction(target, irb->current_basic_block);2143 ir_ref_instruction(target, irb->current_basic_block);
21392144
2140 return &instruction->base;2145 return &instruction->base;
2141}2146}
21422147
2143static IrInstruction *ir_build_from_bytes(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *dest_child_type, IrInstruction *target) {2148static IrInstruction *ir_build_from_bytes(IrBuilder *irb, Scope *scope, AstNode *source_node,
2149 IrInstruction *dest_child_type, IrInstruction *target, ResultLoc *result_loc)
2150{
2144 IrInstructionFromBytes *instruction = ir_build_instruction<IrInstructionFromBytes>(irb, scope, source_node);2151 IrInstructionFromBytes *instruction = ir_build_instruction<IrInstructionFromBytes>(irb, scope, source_node);
2145 instruction->dest_child_type = dest_child_type;2152 instruction->dest_child_type = dest_child_type;
2146 instruction->target = target;2153 instruction->target = target;
2154 instruction->result_loc = result_loc;
21472155
2148 ir_ref_instruction(dest_child_type, irb->current_basic_block);2156 ir_ref_instruction(dest_child_type, irb->current_basic_block);
2149 ir_ref_instruction(target, irb->current_basic_block);2157 ir_ref_instruction(target, irb->current_basic_block);
...@@ -4599,7 +4607,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo...@@ -4599,7 +4607,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
4599 if (arg1_value == irb->codegen->invalid_instruction)4607 if (arg1_value == irb->codegen->invalid_instruction)
4600 return arg1_value;4608 return arg1_value;
46014609
4602 IrInstruction *result = ir_build_from_bytes(irb, scope, node, arg0_value, arg1_value);4610 IrInstruction *result = ir_build_from_bytes(irb, scope, node, arg0_value, arg1_value, result_loc);
4603 return ir_lval_wrap(irb, scope, result, lval, result_loc);4611 return ir_lval_wrap(irb, scope, result, lval, result_loc);
4604 }4612 }
4605 case BuiltinFnIdToBytes:4613 case BuiltinFnIdToBytes:
...@@ -4609,7 +4617,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo...@@ -4609,7 +4617,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
4609 if (arg0_value == irb->codegen->invalid_instruction)4617 if (arg0_value == irb->codegen->invalid_instruction)
4610 return arg0_value;4618 return arg0_value;
46114619
4612 IrInstruction *result = ir_build_to_bytes(irb, scope, node, arg0_value);4620 IrInstruction *result = ir_build_to_bytes(irb, scope, node, arg0_value, result_loc);
4613 return ir_lval_wrap(irb, scope, result, lval, result_loc);4621 return ir_lval_wrap(irb, scope, result, lval, result_loc);
4614 }4622 }
4615 case BuiltinFnIdIntToFloat:4623 case BuiltinFnIdIntToFloat:
...@@ -5681,7 +5689,8 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A...@@ -5681,7 +5689,8 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
5681 ir_ref_instruction(elem_ptr, irb->current_basic_block);5689 ir_ref_instruction(elem_ptr, irb->current_basic_block);
5682 ResultLoc *child_result_loc = &result_loc_inst->base;5690 ResultLoc *child_result_loc = &result_loc_inst->base;
56835691
5684 IrInstruction *expr_value = ir_gen_node_extra(irb, expr_node, scope, LValNone, child_result_loc);5692 IrInstruction *expr_value = ir_gen_node_extra(irb, expr_node, &result_loc->scope_elide->base,
5693 LValNone, child_result_loc);
5685 if (expr_value == irb->codegen->invalid_instruction)5694 if (expr_value == irb->codegen->invalid_instruction)
5686 return expr_value;5695 return expr_value;
56875696
...@@ -14687,6 +14696,7 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s...@@ -14687,6 +14696,7 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s
14687 if (fn_entry != nullptr) {14696 if (fn_entry != nullptr) {
14688 fn_entry->alloca_gen_list.append(alloca_gen);14697 fn_entry->alloca_gen_list.append(alloca_gen);
14689 }14698 }
14699 result_loc->written = true;
14690 result_loc->resolved_loc = &alloca_gen->base;14700 result_loc->resolved_loc = &alloca_gen->base;
14691 return result_loc->resolved_loc;14701 return result_loc->resolved_loc;
14692 }14702 }
...@@ -20766,6 +20776,12 @@ static IrInstruction *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstru...@@ -20766,6 +20776,12 @@ static IrInstruction *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstru
20766 }20776 }
20767 }20777 }
2076820778
20779 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
20780 dest_slice_type, nullptr);
20781 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {
20782 return result_loc;
20783 }
20784
20769 if (casted_value->value.data.rh_slice.id == RuntimeHintSliceIdLen) {20785 if (casted_value->value.data.rh_slice.id == RuntimeHintSliceIdLen) {
20770 known_len = casted_value->value.data.rh_slice.len;20786 known_len = casted_value->value.data.rh_slice.len;
20771 have_known_len = true;20787 have_known_len = true;
...@@ -20785,9 +20801,7 @@ static IrInstruction *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstru...@@ -20785,9 +20801,7 @@ static IrInstruction *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstru
20785 }20801 }
20786 }20802 }
2078720803
20788 IrInstruction *result = ir_build_resize_slice(ira, &instruction->base, casted_value, dest_slice_type);20804 return ir_build_resize_slice(ira, &instruction->base, casted_value, dest_slice_type, result_loc);
20789 ir_add_alloca(ira, result, dest_slice_type);
20790 return result;
20791}20805}
2079220806
20793static IrInstruction *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstructionToBytes *instruction) {20807static IrInstruction *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstructionToBytes *instruction) {
...@@ -20839,9 +20853,13 @@ static IrInstruction *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstruct...@@ -20839,9 +20853,13 @@ static IrInstruction *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstruct
20839 return result;20853 return result;
20840 }20854 }
2084120855
20842 IrInstruction *result = ir_build_resize_slice(ira, &instruction->base, target, dest_slice_type);20856 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
20843 ir_add_alloca(ira, result, dest_slice_type);20857 dest_slice_type, nullptr);
20844 return result;20858 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {
20859 return result_loc;
20860 }
20861
20862 return ir_build_resize_slice(ira, &instruction->base, target, dest_slice_type, result_loc);
20845}20863}
2084620864
20847static Error resolve_ptr_align(IrAnalyze *ira, ZigType *ty, uint32_t *result_align) {20865static Error resolve_ptr_align(IrAnalyze *ira, ZigType *ty, uint32_t *result_align) {
src/ir_print.cpp+2-1
...@@ -1125,7 +1125,8 @@ static void ir_print_assert_non_null(IrPrint *irp, IrInstructionAssertNonNull *i...@@ -1125,7 +1125,8 @@ static void ir_print_assert_non_null(IrPrint *irp, IrInstructionAssertNonNull *i
1125static void ir_print_resize_slice(IrPrint *irp, IrInstructionResizeSlice *instruction) {1125static void ir_print_resize_slice(IrPrint *irp, IrInstructionResizeSlice *instruction) {
1126 fprintf(irp->f, "@resizeSlice(");1126 fprintf(irp->f, "@resizeSlice(");
1127 ir_print_other_instruction(irp, instruction->operand);1127 ir_print_other_instruction(irp, instruction->operand);
1128 fprintf(irp->f, ")");1128 fprintf(irp->f, ")result=");
1129 ir_print_other_instruction(irp, instruction->result_loc);
1129}1130}
11301131
1131static void ir_print_alloca_src(IrPrint *irp, IrInstructionAllocaSrc *instruction) {1132static void ir_print_alloca_src(IrPrint *irp, IrInstructionAllocaSrc *instruction) {