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 @@
11Scratch pad for stuff to do before merging master
22=================================================
33
4 * array initializations
54 * union initializations
65 * bitCast
76
src/ir.cpp+34-18
......@@ -5565,6 +5565,11 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
55655565 case ContainerInitKindStruct: {
55665566 src_assert(result_loc->scope_elide == nullptr, node);
55675567 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
55685573 size_t field_count = container_init_expr->entries.length;
55695574 IrInstructionContainerInitFieldsField *fields = allocate<IrInstructionContainerInitFieldsField>(field_count);
55705575 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
55745579 Buf *name = entry_node->data.struct_val_field.name;
55755580 AstNode *expr_node = entry_node->data.struct_val_field.expr;
55765581
5577 ResultLoc *child_result_loc = nullptr;
5578 if (result_loc != nullptr) {
5579 IrInstruction *container_ptr = ir_build_resolve_result(irb, &result_loc->scope_elide->base,
5580 expr_node, result_loc, container_type);
5581 IrInstruction *field_ptr = ir_build_field_ptr(irb, &result_loc->scope_elide->base, expr_node,
5582 container_ptr, name);
5583 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1);
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 }
5582 IrInstruction *field_ptr = ir_build_field_ptr(irb, &result_loc->scope_elide->base, expr_node,
5583 container_ptr, name);
5584 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1);
5585 result_loc_inst->base.id = ResultLocIdInstruction;
5586 result_loc_inst->base.source_instruction = field_ptr;
5587 ir_ref_instruction(field_ptr, irb->current_basic_block);
5588 ResultLoc *child_result_loc = &result_loc_inst->base;
55895589
55905590 IrInstruction *expr_value = ir_gen_node_extra(irb, expr_node, &result_loc->scope_elide->base,
55915591 LValNone, child_result_loc);
......@@ -5601,11 +5601,28 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
56015601 return ir_lval_wrap(irb, scope, init_fields, lval, result_loc);
56025602 }
56035603 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
56045611 size_t item_count = container_init_expr->entries.length;
56055612 IrInstruction **values = allocate<IrInstruction *>(item_count);
56065613 for (size_t i = 0; i < item_count; i += 1) {
56075614 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);
56095626 if (expr_value == irb->codegen->invalid_instruction)
56105627 return expr_value;
56115628
......@@ -18594,11 +18611,10 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1859418611 return ira->codegen->invalid_instruction;
1859518612 }
1859618613
18597 IrInstruction *new_instruction = ir_build_container_init_list(&ira->new_irb,
18598 instruction->base.scope, instruction->base.source_node,
18599 nullptr, elem_count, new_items);
18600 new_instruction->value.type = fixed_size_array_type;
18601 ir_add_alloca(ira, new_instruction, fixed_size_array_type);
18614 // this instruction should not get to codegen
18615 IrInstruction *new_instruction = ir_const(ira, &instruction->base, fixed_size_array_type);
18616 // this is how we signal to EndExpr the value is not comptime known
18617 new_instruction->value.special = ConstValSpecialRuntime;
1860218618 return new_instruction;
1860318619 } else if (container_type->id == ZigTypeIdVoid) {
1860418620 if (elem_count != 0) {