authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-11-16 14:07:23+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-11-16 10:15:13-05:00
logbe9bb0a8572577f5e5bbac83ff25b88467906d70
treedda5689eddd5fa857abc70f114d678bb8069c295
parent332fdf33eeeb0560d3f854c816196214aa4e9bc9

Fixed #1663 and removed IrInstructionArrayLen


5 files changed, 20 insertions(+), 74 deletions(-)

src/all_types.hpp-7
...@@ -2071,7 +2071,6 @@ enum IrInstructionId {...@@ -2071,7 +2071,6 @@ enum IrInstructionId {
2071 IrInstructionIdCInclude,2071 IrInstructionIdCInclude,
2072 IrInstructionIdCDefine,2072 IrInstructionIdCDefine,
2073 IrInstructionIdCUndef,2073 IrInstructionIdCUndef,
2074 IrInstructionIdArrayLen,
2075 IrInstructionIdRef,2074 IrInstructionIdRef,
2076 IrInstructionIdCompileErr,2075 IrInstructionIdCompileErr,
2077 IrInstructionIdCompileLog,2076 IrInstructionIdCompileLog,
...@@ -2584,12 +2583,6 @@ struct IrInstructionImport {...@@ -2584,12 +2583,6 @@ struct IrInstructionImport {
2584 IrInstruction *name;2583 IrInstruction *name;
2585};2584};
25862585
2587struct IrInstructionArrayLen {
2588 IrInstruction base;
2589
2590 IrInstruction *array_value;
2591};
2592
2593struct IrInstructionRef {2586struct IrInstructionRef {
2594 IrInstruction base;2587 IrInstruction base;
25952588
src/codegen.cpp-1
...@@ -5102,7 +5102,6 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,...@@ -5102,7 +5102,6 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
5102 case IrInstructionIdContainerInitFields:5102 case IrInstructionIdContainerInitFields:
5103 case IrInstructionIdCompileErr:5103 case IrInstructionIdCompileErr:
5104 case IrInstructionIdCompileLog:5104 case IrInstructionIdCompileLog:
5105 case IrInstructionIdArrayLen:
5106 case IrInstructionIdImport:5105 case IrInstructionIdImport:
5107 case IrInstructionIdCImport:5106 case IrInstructionIdCImport:
5108 case IrInstructionIdCInclude:5107 case IrInstructionIdCInclude:
src/ir.cpp+10-57
...@@ -479,10 +479,6 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionCUndef *) {...@@ -479,10 +479,6 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionCUndef *) {
479 return IrInstructionIdCUndef;479 return IrInstructionIdCUndef;
480}480}
481481
482static constexpr IrInstructionId ir_instruction_id(IrInstructionArrayLen *) {
483 return IrInstructionIdArrayLen;
484}
485
486static constexpr IrInstructionId ir_instruction_id(IrInstructionRef *) {482static constexpr IrInstructionId ir_instruction_id(IrInstructionRef *) {
487 return IrInstructionIdRef;483 return IrInstructionIdRef;
488}484}
...@@ -1663,15 +1659,6 @@ static IrInstruction *ir_build_import(IrBuilder *irb, Scope *scope, AstNode *sou...@@ -1663,15 +1659,6 @@ static IrInstruction *ir_build_import(IrBuilder *irb, Scope *scope, AstNode *sou
1663 return &instruction->base;1659 return &instruction->base;
1664}1660}
16651661
1666static IrInstruction *ir_build_array_len(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *array_value) {
1667 IrInstructionArrayLen *instruction = ir_build_instruction<IrInstructionArrayLen>(irb, scope, source_node);
1668 instruction->array_value = array_value;
1669
1670 ir_ref_instruction(array_value, irb->current_basic_block);
1671
1672 return &instruction->base;
1673}
1674
1675static IrInstruction *ir_build_ref(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value,1662static IrInstruction *ir_build_ref(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value,
1676 bool is_const, bool is_volatile)1663 bool is_const, bool is_volatile)
1677{1664{
...@@ -5351,7 +5338,9 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo...@@ -5351,7 +5338,9 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
5351 IrBasicBlock *else_block = else_node ? ir_create_basic_block(irb, child_scope, "ForElse") : end_block;5338 IrBasicBlock *else_block = else_node ? ir_create_basic_block(irb, child_scope, "ForElse") : end_block;
5352 IrBasicBlock *continue_block = ir_create_basic_block(irb, child_scope, "ForContinue");5339 IrBasicBlock *continue_block = ir_create_basic_block(irb, child_scope, "ForContinue");
53535340
5354 IrInstruction *len_val = ir_build_array_len(irb, child_scope, node, array_val);5341 Buf *len_field_name = buf_create_from_str("len");
5342 IrInstruction *len_ref = ir_build_field_ptr(irb, child_scope, node, array_val_ptr, len_field_name);
5343 IrInstruction *len_val = ir_build_load_ptr(irb, child_scope, node, len_ref);
5355 ir_build_br(irb, child_scope, node, cond_block, is_comptime);5344 ir_build_br(irb, child_scope, node, cond_block, is_comptime);
53565345
5357 ir_set_cursor_at_end_and_append_block(irb, cond_block);5346 ir_set_cursor_at_end_and_append_block(irb, cond_block);
...@@ -9450,12 +9439,6 @@ static IrInstruction *ir_const_unsigned(IrAnalyze *ira, IrInstruction *source_in...@@ -9450,12 +9439,6 @@ static IrInstruction *ir_const_unsigned(IrAnalyze *ira, IrInstruction *source_in
9450 return result;9439 return result;
9451}9440}
94529441
9453static IrInstruction *ir_const_usize(IrAnalyze *ira, IrInstruction *source_instruction, uint64_t value) {
9454 IrInstruction *result = ir_const(ira, source_instruction, ira->codegen->builtin_types.entry_usize);
9455 bigint_init_unsigned(&result->value.data.x_bigint, value);
9456 return result;
9457}
9458
9459static IrInstruction *ir_get_const_ptr(IrAnalyze *ira, IrInstruction *instruction,9442static IrInstruction *ir_get_const_ptr(IrAnalyze *ira, IrInstruction *instruction,
9460 ConstExprValue *pointee, ZigType *pointee_type,9443 ConstExprValue *pointee, ZigType *pointee_type,
9461 ConstPtrMut ptr_mut, bool ptr_is_const, bool ptr_is_volatile, uint32_t ptr_align)9444 ConstPtrMut ptr_mut, bool ptr_is_const, bool ptr_is_volatile, uint32_t ptr_align)
...@@ -14971,8 +14954,14 @@ static IrInstruction *ir_analyze_instruction_to_ptr_type(IrAnalyze *ira,...@@ -14971,8 +14954,14 @@ static IrInstruction *ir_analyze_instruction_to_ptr_type(IrAnalyze *ira,
1497114954
14972 ZigType *ptr_type;14955 ZigType *ptr_type;
14973 if (type_entry->id == ZigTypeIdArray) {14956 if (type_entry->id == ZigTypeIdArray) {
14957 // TODO: Allow capturing pointer to const array.
14958 // const a = "123"; for (a) |*c| continue;
14959 // error: expected type '*u8', found '*const u8'
14974 ptr_type = get_pointer_to_type(ira->codegen, type_entry->data.array.child_type, false);14960 ptr_type = get_pointer_to_type(ira->codegen, type_entry->data.array.child_type, false);
14975 } else if (is_slice(type_entry)) {14961 } else if (is_array_ref(type_entry)) {
14962 ptr_type = get_pointer_to_type(ira->codegen,
14963 type_entry->data.pointer.child_type->data.array.child_type, type_entry->data.pointer.is_const);
14964 } else if (is_slice(type_entry)) {
14976 ptr_type = adjust_ptr_len(ira->codegen, type_entry->data.structure.fields[0].type_entry, PtrLenSingle);14965 ptr_type = adjust_ptr_len(ira->codegen, type_entry->data.structure.fields[0].type_entry, PtrLenSingle);
14977 } else if (type_entry->id == ZigTypeIdArgTuple) {14966 } else if (type_entry->id == ZigTypeIdArgTuple) {
14978 ConstExprValue *arg_tuple_val = ir_resolve_const(ira, value, UndefBad);14967 ConstExprValue *arg_tuple_val = ir_resolve_const(ira, value, UndefBad);
...@@ -15959,39 +15948,6 @@ static IrInstruction *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructio...@@ -15959,39 +15948,6 @@ static IrInstruction *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructio
15959 return result;15948 return result;
15960}15949}
1596115950
15962static IrInstruction *ir_analyze_instruction_array_len(IrAnalyze *ira,
15963 IrInstructionArrayLen *array_len_instruction)
15964{
15965 IrInstruction *array_value = array_len_instruction->array_value->child;
15966 ZigType *type_entry = array_value->value.type;
15967 if (type_is_invalid(type_entry)) {
15968 return ira->codegen->invalid_instruction;
15969 } else if (type_entry->id == ZigTypeIdArray) {
15970 return ir_const_usize(ira, &array_len_instruction->base,
15971 type_entry->data.array.len);
15972 } else if (is_slice(type_entry)) {
15973 if (array_value->value.special != ConstValSpecialRuntime) {
15974 ConstExprValue *len_val = &array_value->value.data.x_struct.fields[slice_len_index];
15975 if (len_val->special != ConstValSpecialRuntime) {
15976 return ir_const_usize(ira, &array_len_instruction->base,
15977 bigint_as_unsigned(&len_val->data.x_bigint));
15978 }
15979 }
15980 TypeStructField *field = &type_entry->data.structure.fields[slice_len_index];
15981 IrInstruction *len_ptr = ir_build_struct_field_ptr(&ira->new_irb, array_len_instruction->base.scope,
15982 array_len_instruction->base.source_node, array_value, field);
15983 len_ptr->value.type = get_pointer_to_type(ira->codegen, ira->codegen->builtin_types.entry_usize, true);
15984 IrInstruction *result = ir_build_load_ptr(&ira->new_irb,
15985 array_len_instruction->base.scope, array_len_instruction->base.source_node, len_ptr);
15986 result->value.type = ira->codegen->builtin_types.entry_usize;
15987 return result;
15988 } else {
15989 ir_add_error_node(ira, array_len_instruction->base.source_node,
15990 buf_sprintf("type '%s' has no field 'len'", buf_ptr(&array_value->value.type->name)));
15991 return ira->codegen->invalid_instruction;
15992 }
15993}
15994
15995static IrInstruction *ir_analyze_instruction_ref(IrAnalyze *ira, IrInstructionRef *ref_instruction) {15951static IrInstruction *ir_analyze_instruction_ref(IrAnalyze *ira, IrInstructionRef *ref_instruction) {
15996 IrInstruction *value = ref_instruction->value->child;15952 IrInstruction *value = ref_instruction->value->child;
15997 if (type_is_invalid(value->value.type))15953 if (type_is_invalid(value->value.type))
...@@ -20893,8 +20849,6 @@ static IrInstruction *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructio...@@ -20893,8 +20849,6 @@ static IrInstruction *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructio
20893 return ir_analyze_instruction_union_tag(ira, (IrInstructionUnionTag *)instruction);20849 return ir_analyze_instruction_union_tag(ira, (IrInstructionUnionTag *)instruction);
20894 case IrInstructionIdImport:20850 case IrInstructionIdImport:
20895 return ir_analyze_instruction_import(ira, (IrInstructionImport *)instruction);20851 return ir_analyze_instruction_import(ira, (IrInstructionImport *)instruction);
20896 case IrInstructionIdArrayLen:
20897 return ir_analyze_instruction_array_len(ira, (IrInstructionArrayLen *)instruction);
20898 case IrInstructionIdRef:20852 case IrInstructionIdRef:
20899 return ir_analyze_instruction_ref(ira, (IrInstructionRef *)instruction);20853 return ir_analyze_instruction_ref(ira, (IrInstructionRef *)instruction);
20900 case IrInstructionIdContainerInitList:20854 case IrInstructionIdContainerInitList:
...@@ -21228,7 +21182,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {...@@ -21228,7 +21182,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
21228 case IrInstructionIdTypeOf:21182 case IrInstructionIdTypeOf:
21229 case IrInstructionIdToPtrType:21183 case IrInstructionIdToPtrType:
21230 case IrInstructionIdPtrTypeChild:21184 case IrInstructionIdPtrTypeChild:
21231 case IrInstructionIdArrayLen:
21232 case IrInstructionIdStructFieldPtr:21185 case IrInstructionIdStructFieldPtr:
21233 case IrInstructionIdUnionFieldPtr:21186 case IrInstructionIdUnionFieldPtr:
21234 case IrInstructionIdArrayType:21187 case IrInstructionIdArrayType:
src/ir_print.cpp-8
...@@ -553,11 +553,6 @@ static void ir_print_import(IrPrint *irp, IrInstructionImport *instruction) {...@@ -553,11 +553,6 @@ static void ir_print_import(IrPrint *irp, IrInstructionImport *instruction) {
553 fprintf(irp->f, ")");553 fprintf(irp->f, ")");
554}554}
555555
556static void ir_print_array_len(IrPrint *irp, IrInstructionArrayLen *instruction) {
557 ir_print_other_instruction(irp, instruction->array_value);
558 fprintf(irp->f, ".len");
559}
560
561static void ir_print_ref(IrPrint *irp, IrInstructionRef *instruction) {556static void ir_print_ref(IrPrint *irp, IrInstructionRef *instruction) {
562 const char *const_str = instruction->is_const ? "const " : "";557 const char *const_str = instruction->is_const ? "const " : "";
563 const char *volatile_str = instruction->is_volatile ? "volatile " : "";558 const char *volatile_str = instruction->is_volatile ? "volatile " : "";
...@@ -1462,9 +1457,6 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {...@@ -1462,9 +1457,6 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
1462 case IrInstructionIdImport:1457 case IrInstructionIdImport:
1463 ir_print_import(irp, (IrInstructionImport *)instruction);1458 ir_print_import(irp, (IrInstructionImport *)instruction);
1464 break;1459 break;
1465 case IrInstructionIdArrayLen:
1466 ir_print_array_len(irp, (IrInstructionArrayLen *)instruction);
1467 break;
1468 case IrInstructionIdRef:1460 case IrInstructionIdRef:
1469 ir_print_ref(irp, (IrInstructionRef *)instruction);1461 ir_print_ref(irp, (IrInstructionRef *)instruction);
1470 break;1462 break;
test/cases/for.zig+10-1
...@@ -35,7 +35,7 @@ fn mangleString(s: []u8) void {...@@ -35,7 +35,7 @@ fn mangleString(s: []u8) void {
35}35}
3636
37test "basic for loop" {37test "basic for loop" {
38 const expected_result = []u8{ 9, 8, 7, 6, 0, 1, 2, 3, 9, 8, 7, 6, 0, 1, 2, 3 };38 const expected_result = []u8{ 9, 8, 7, 6, 0, 1, 2, 3 } ** 3;
3939
40 var buffer: [expected_result.len]u8 = undefined;40 var buffer: [expected_result.len]u8 = undefined;
41 var buf_index: usize = 0;41 var buf_index: usize = 0;
...@@ -49,6 +49,15 @@ test "basic for loop" {...@@ -49,6 +49,15 @@ test "basic for loop" {
49 buffer[buf_index] = @intCast(u8, index);49 buffer[buf_index] = @intCast(u8, index);
50 buf_index += 1;50 buf_index += 1;
51 }51 }
52 const array_ptr = &array;
53 for (array_ptr) |item| {
54 buffer[buf_index] = item;
55 buf_index += 1;
56 }
57 for (array_ptr) |item, index| {
58 buffer[buf_index] = @intCast(u8, index);
59 buf_index += 1;
60 }
52 const unknown_size: []const u8 = array;61 const unknown_size: []const u8 = array;
53 for (unknown_size) |item| {62 for (unknown_size) |item| {
54 buffer[buf_index] = item;63 buffer[buf_index] = item;