authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-09 12:03:15-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-09 12:03:15-04:00
log59fe13772f63838a84ac1786c0dc8361cd14b99d
treed005cca602d469ac080d68e9d6807c85d08ec9df
parent3ec766abe38a892346e56a47ec0fa4c27eda2995
signature Commit is signed but in an unrecognized format.

result loc semantics for array initialization

```zig export fn entry() void { var x = [3]Bar{ bar(), bar(), Bar{ .y = 12 } }; } ``` ```llvm define void @entry() #2 !dbg !35 { Entry: %x = alloca [3 x %Bar], align 4 %0 = getelementptr inbounds [3 x %Bar], [3 x %Bar]* %x, i64 0, i64 0, !dbg !48 call fastcc void @bar(%Bar* sret %0), !dbg !48 %1 = getelementptr inbounds [3 x %Bar], [3 x %Bar]* %x, i64 0, i64 1, !dbg !49 call fastcc void @bar(%Bar* sret %1), !dbg !49 %2 = getelementptr inbounds [3 x %Bar], [3 x %Bar]* %x, i64 0, i64 2, !dbg !50 %3 = bitcast %Bar* %2 to i8*, !dbg !50 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %3, i8* align 4 bitcast (%Bar* @0 to i8*), i64 4, i1 false), !dbg !50 call void @llvm.dbg.declare(metadata [3 x %Bar]* %x, metadata !39, metadata !DIExpression()), !dbg !51 ret void, !dbg !52 } ```

2 files changed, 34 insertions(+), 19 deletions(-)

BRANCH_TODO-1
...@@ -1,7 +1,6 @@...@@ -1,7 +1,6 @@
1Scratch pad for stuff to do before merging master1Scratch pad for stuff to do before merging master
2=================================================2=================================================
33
4 * array initializations
5 * union initializations4 * union initializations
6 * bitCast5 * bitCast
76
src/ir.cpp+34-18
...@@ -5565,6 +5565,11 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A...@@ -5565,6 +5565,11 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
5565 case ContainerInitKindStruct: {5565 case ContainerInitKindStruct: {
5566 src_assert(result_loc->scope_elide == nullptr, node);5566 src_assert(result_loc->scope_elide == nullptr, node);
5567 result_loc->scope_elide = create_elide_scope(irb->codegen, node, scope);5567 result_loc->scope_elide = create_elide_scope(irb->codegen, node, scope);
5568
5569 src_assert(result_loc != nullptr, node);
5570 IrInstruction *container_ptr = ir_build_resolve_result(irb, &result_loc->scope_elide->base,
5571 node, result_loc, container_type);
5572
5568 size_t field_count = container_init_expr->entries.length;5573 size_t field_count = container_init_expr->entries.length;
5569 IrInstructionContainerInitFieldsField *fields = allocate<IrInstructionContainerInitFieldsField>(field_count);5574 IrInstructionContainerInitFieldsField *fields = allocate<IrInstructionContainerInitFieldsField>(field_count);
5570 for (size_t i = 0; i < field_count; i += 1) {5575 for (size_t i = 0; i < field_count; i += 1) {
...@@ -5574,18 +5579,13 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A...@@ -5574,18 +5579,13 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
5574 Buf *name = entry_node->data.struct_val_field.name;5579 Buf *name = entry_node->data.struct_val_field.name;
5575 AstNode *expr_node = entry_node->data.struct_val_field.expr;5580 AstNode *expr_node = entry_node->data.struct_val_field.expr;
55765581
5577 ResultLoc *child_result_loc = nullptr;5582 IrInstruction *field_ptr = ir_build_field_ptr(irb, &result_loc->scope_elide->base, expr_node,
5578 if (result_loc != nullptr) {5583 container_ptr, name);
5579 IrInstruction *container_ptr = ir_build_resolve_result(irb, &result_loc->scope_elide->base,5584 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1);
5580 expr_node, result_loc, container_type);5585 result_loc_inst->base.id = ResultLocIdInstruction;
5581 IrInstruction *field_ptr = ir_build_field_ptr(irb, &result_loc->scope_elide->base, expr_node,5586 result_loc_inst->base.source_instruction = field_ptr;
5582 container_ptr, name);5587 ir_ref_instruction(field_ptr, irb->current_basic_block);
5583 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1);5588 ResultLoc *child_result_loc = &result_loc_inst->base;
5584 result_loc_inst->base.id = ResultLocIdInstruction;
5585 result_loc_inst->base.source_instruction = field_ptr;
5586 ir_ref_instruction(field_ptr, irb->current_basic_block);
5587 child_result_loc = &result_loc_inst->base;
5588 }
55895589
5590 IrInstruction *expr_value = ir_gen_node_extra(irb, expr_node, &result_loc->scope_elide->base,5590 IrInstruction *expr_value = ir_gen_node_extra(irb, expr_node, &result_loc->scope_elide->base,
5591 LValNone, child_result_loc);5591 LValNone, child_result_loc);
...@@ -5601,11 +5601,28 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A...@@ -5601,11 +5601,28 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
5601 return ir_lval_wrap(irb, scope, init_fields, lval, result_loc);5601 return ir_lval_wrap(irb, scope, init_fields, lval, result_loc);
5602 }5602 }
5603 case ContainerInitKindArray: {5603 case ContainerInitKindArray: {
5604 src_assert(result_loc->scope_elide == nullptr, node);
5605 result_loc->scope_elide = create_elide_scope(irb->codegen, node, scope);
5606
5607 src_assert(result_loc != nullptr, node);
5608 IrInstruction *container_ptr = ir_build_resolve_result(irb, &result_loc->scope_elide->base,
5609 node, result_loc, container_type);
5610
5604 size_t item_count = container_init_expr->entries.length;5611 size_t item_count = container_init_expr->entries.length;
5605 IrInstruction **values = allocate<IrInstruction *>(item_count);5612 IrInstruction **values = allocate<IrInstruction *>(item_count);
5606 for (size_t i = 0; i < item_count; i += 1) {5613 for (size_t i = 0; i < item_count; i += 1) {
5607 AstNode *expr_node = container_init_expr->entries.at(i);5614 AstNode *expr_node = container_init_expr->entries.at(i);
5608 IrInstruction *expr_value = ir_gen_node(irb, expr_node, scope);5615
5616 IrInstruction *elem_index = ir_build_const_usize(irb, &result_loc->scope_elide->base, expr_node, i);
5617 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, &result_loc->scope_elide->base, expr_node,
5618 container_ptr, elem_index, false, PtrLenSingle);
5619 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1);
5620 result_loc_inst->base.id = ResultLocIdInstruction;
5621 result_loc_inst->base.source_instruction = elem_ptr;
5622 ir_ref_instruction(elem_ptr, irb->current_basic_block);
5623 ResultLoc *child_result_loc = &result_loc_inst->base;
5624
5625 IrInstruction *expr_value = ir_gen_node_extra(irb, expr_node, scope, LValNone, child_result_loc);
5609 if (expr_value == irb->codegen->invalid_instruction)5626 if (expr_value == irb->codegen->invalid_instruction)
5610 return expr_value;5627 return expr_value;
56115628
...@@ -18594,11 +18611,10 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,...@@ -18594,11 +18611,10 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
18594 return ira->codegen->invalid_instruction;18611 return ira->codegen->invalid_instruction;
18595 }18612 }
1859618613
18597 IrInstruction *new_instruction = ir_build_container_init_list(&ira->new_irb,18614 // this instruction should not get to codegen
18598 instruction->base.scope, instruction->base.source_node,18615 IrInstruction *new_instruction = ir_const(ira, &instruction->base, fixed_size_array_type);
18599 nullptr, elem_count, new_items);18616 // this is how we signal to EndExpr the value is not comptime known
18600 new_instruction->value.type = fixed_size_array_type;18617 new_instruction->value.special = ConstValSpecialRuntime;
18601 ir_add_alloca(ira, new_instruction, fixed_size_array_type);
18602 return new_instruction;18618 return new_instruction;
18603 } else if (container_type->id == ZigTypeIdVoid) {18619 } else if (container_type->id == ZigTypeIdVoid) {
18604 if (elem_count != 0) {18620 if (elem_count != 0) {