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
signature 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 {
26352635 IrInstruction base;
26362636
26372637 IrInstruction *operand;
2638 LLVMValueRef tmp_ptr;
2638 IrInstruction *result_loc;
26392639};
26402640
26412641struct IrInstructionContainerInitList {
......@@ -2925,6 +2925,7 @@ struct IrInstructionToBytes {
29252925 IrInstruction base;
29262926
29272927 IrInstruction *target;
2928 ResultLoc *result_loc;
29282929};
29292930
29302931struct IrInstructionFromBytes {
......@@ -2932,6 +2933,7 @@ struct IrInstructionFromBytes {
29322933
29332934 IrInstruction *dest_child_type;
29342935 IrInstruction *target;
2936 ResultLoc *result_loc;
29352937};
29362938
29372939struct IrInstructionIntToFloat {
src/codegen.cpp+4-9
......@@ -2943,7 +2943,7 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,
29432943 LLVMValueRef expr_val = ir_llvm_value(g, instruction->operand);
29442944 assert(expr_val);
29452945
2946 assert(instruction->tmp_ptr);
2946 LLVMValueRef result_loc = ir_llvm_value(g, instruction->result_loc);
29472947 assert(wanted_type->id == ZigTypeIdStruct);
29482948 assert(wanted_type->data.structure.is_slice);
29492949 assert(actual_type->id == ZigTypeIdStruct);
......@@ -2964,7 +2964,7 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,
29642964 LLVMValueRef src_ptr = gen_load_untyped(g, src_ptr_ptr, 0, false, "");
29652965 LLVMValueRef src_ptr_casted = LLVMBuildBitCast(g->builder, src_ptr,
29662966 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,
29682968 (unsigned)wanted_ptr_index, "");
29692969 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,
29972997 zig_unreachable();
29982998 }
29992999
3000 LLVMValueRef dest_len_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,
3001 (unsigned)wanted_len_index, "");
3000 LLVMValueRef dest_len_ptr = LLVMBuildStructGEP(g->builder, result_loc, (unsigned)wanted_len_index, "");
30023001 gen_store_untyped(g, new_len, dest_len_ptr, 0, false);
30033002
3004
3005 return instruction->tmp_ptr;
3003 return result_loc;
30063004}
30073005
30083006static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
......@@ -6840,9 +6838,6 @@ static void do_code_gen(CodeGen *g) {
68406838 slot = &ref_instruction->tmp_ptr;
68416839 assert(instruction->value.type->id == ZigTypeIdPointer);
68426840 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;
68466841 } else if (instruction->id == IrInstructionIdLoadPtrGen) {
68476842 IrInstructionLoadPtrGen *load_ptr_inst = (IrInstructionLoadPtrGen *)instruction;
68486843 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
15741574}
15751575
15761576static IrInstruction *ir_build_resize_slice(IrAnalyze *ira, IrInstruction *source_instruction,
1577 IrInstruction *operand, ZigType *ty)
1577 IrInstruction *operand, ZigType *ty, IrInstruction *result_loc)
15781578{
15791579 IrInstructionResizeSlice *instruction = ir_build_instruction<IrInstructionResizeSlice>(&ira->new_irb,
15801580 source_instruction->scope, source_instruction->source_node);
15811581 instruction->base.value.type = ty;
15821582 instruction->operand = operand;
1583 instruction->result_loc = result_loc;
15831584
15841585 ir_ref_instruction(operand, ira->new_irb.current_basic_block);
1586 ir_ref_instruction(result_loc, ira->new_irb.current_basic_block);
15851587
15861588 return &instruction->base;
15871589}
......@@ -2131,19 +2133,25 @@ static IrInstruction *ir_build_err_set_cast(IrBuilder *irb, Scope *scope, AstNod
21312133 return &instruction->base;
21322134}
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{
21352139 IrInstructionToBytes *instruction = ir_build_instruction<IrInstructionToBytes>(irb, scope, source_node);
21362140 instruction->target = target;
2141 instruction->result_loc = result_loc;
21372142
21382143 ir_ref_instruction(target, irb->current_basic_block);
21392144
21402145 return &instruction->base;
21412146}
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{
21442151 IrInstructionFromBytes *instruction = ir_build_instruction<IrInstructionFromBytes>(irb, scope, source_node);
21452152 instruction->dest_child_type = dest_child_type;
21462153 instruction->target = target;
2154 instruction->result_loc = result_loc;
21472155
21482156 ir_ref_instruction(dest_child_type, irb->current_basic_block);
21492157 ir_ref_instruction(target, irb->current_basic_block);
......@@ -4599,7 +4607,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
45994607 if (arg1_value == irb->codegen->invalid_instruction)
46004608 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);
46034611 return ir_lval_wrap(irb, scope, result, lval, result_loc);
46044612 }
46054613 case BuiltinFnIdToBytes:
......@@ -4609,7 +4617,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
46094617 if (arg0_value == irb->codegen->invalid_instruction)
46104618 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);
46134621 return ir_lval_wrap(irb, scope, result, lval, result_loc);
46144622 }
46154623 case BuiltinFnIdIntToFloat:
......@@ -5681,7 +5689,8 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
56815689 ir_ref_instruction(elem_ptr, irb->current_basic_block);
56825690 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);
56855694 if (expr_value == irb->codegen->invalid_instruction)
56865695 return expr_value;
56875696
......@@ -14687,6 +14696,7 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s
1468714696 if (fn_entry != nullptr) {
1468814697 fn_entry->alloca_gen_list.append(alloca_gen);
1468914698 }
14699 result_loc->written = true;
1469014700 result_loc->resolved_loc = &alloca_gen->base;
1469114701 return result_loc->resolved_loc;
1469214702 }
......@@ -20766,6 +20776,12 @@ static IrInstruction *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstru
2076620776 }
2076720777 }
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
2076920785 if (casted_value->value.data.rh_slice.id == RuntimeHintSliceIdLen) {
2077020786 known_len = casted_value->value.data.rh_slice.len;
2077120787 have_known_len = true;
......@@ -20785,9 +20801,7 @@ static IrInstruction *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstru
2078520801 }
2078620802 }
2078720803
20788 IrInstruction *result = ir_build_resize_slice(ira, &instruction->base, casted_value, dest_slice_type);
20789 ir_add_alloca(ira, result, dest_slice_type);
20790 return result;
20804 return ir_build_resize_slice(ira, &instruction->base, casted_value, dest_slice_type, result_loc);
2079120805}
2079220806
2079320807static IrInstruction *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstructionToBytes *instruction) {
......@@ -20839,9 +20853,13 @@ static IrInstruction *ir_analyze_instruction_to_bytes(IrAnalyze *ira, IrInstruct
2083920853 return result;
2084020854 }
2084120855
20842 IrInstruction *result = ir_build_resize_slice(ira, &instruction->base, target, dest_slice_type);
20843 ir_add_alloca(ira, result, dest_slice_type);
20844 return result;
20856 IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc,
20857 dest_slice_type, nullptr);
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);
2084520863}
2084620864
2084720865static 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
11251125static void ir_print_resize_slice(IrPrint *irp, IrInstructionResizeSlice *instruction) {
11261126 fprintf(irp->f, "@resizeSlice(");
11271127 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);
11291130}
11301131
11311132static void ir_print_alloca_src(IrPrint *irp, IrInstructionAllocaSrc *instruction) {