authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-15 20:55:07-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-19 09:53:51-04:00
log2182d28cb0917b8d869d13802a5955ee35b4537a
treeefa890da6ed6c0f549635c3c4589570394dcdb34
parente3c92d05328d7b40927bed66e7c2500a7853cdc8
signaturelock-open Commit is signed but in an unrecognized format.

slicing with comptime start and end results in array

implements #863

3 files changed, 119 insertions(+), 37 deletions(-)

src/analyze.cpp+4-1
...@@ -791,7 +791,10 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size, Zi...@@ -791,7 +791,10 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size, Zi
791 return existing_entry->value;791 return existing_entry->value;
792 }792 }
793793
794 assert(type_is_resolved(child_type, ResolveStatusSizeKnown));794 Error err;
795 if ((err = type_resolve(g, child_type, ResolveStatusSizeKnown))) {
796 codegen_report_errors_and_exit(g);
797 }
795798
796 ZigType *entry = new_type_table_entry(ZigTypeIdArray);799 ZigType *entry = new_type_table_entry(ZigTypeIdArray);
797800
src/codegen.cpp+54-18
...@@ -5422,8 +5422,22 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrI...@@ -5422,8 +5422,22 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrI
54225422
5423 bool want_runtime_safety = instruction->safety_check_on && ir_want_runtime_safety(g, &instruction->base);5423 bool want_runtime_safety = instruction->safety_check_on && ir_want_runtime_safety(g, &instruction->base);
54245424
5425 ZigType *res_slice_ptr_type = instruction->base.value->type->data.structure.fields[slice_ptr_index]->type_entry;5425 ZigType *result_type = instruction->base.value->type;
5426 ZigValue *sentinel = res_slice_ptr_type->data.pointer.sentinel;5426 if (!type_has_bits(g, result_type)) {
5427 return nullptr;
5428 }
5429
5430 ZigValue *sentinel = nullptr;
5431 if (result_type->id == ZigTypeIdPointer) {
5432 ZigType *result_array_type = result_type->data.pointer.child_type;
5433 ir_assert(result_array_type->id == ZigTypeIdArray, &instruction->base);
5434 sentinel = result_array_type->data.array.sentinel;
5435 } else if (result_type->id == ZigTypeIdStruct) {
5436 ZigType *res_slice_ptr_type = result_type->data.structure.fields[slice_ptr_index]->type_entry;
5437 sentinel = res_slice_ptr_type->data.pointer.sentinel;
5438 } else {
5439 zig_unreachable();
5440 }
54275441
5428 if (array_type->id == ZigTypeIdArray ||5442 if (array_type->id == ZigTypeIdArray ||
5429 (array_type->id == ZigTypeIdPointer && array_type->data.pointer.ptr_len == PtrLenSingle))5443 (array_type->id == ZigTypeIdPointer && array_type->data.pointer.ptr_len == PtrLenSingle))
...@@ -5466,18 +5480,24 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrI...@@ -5466,18 +5480,24 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrI
5466 return tmp_struct_ptr;5480 return tmp_struct_ptr;
5467 }5481 }
54685482
5469
5470 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_ptr_index, "");
5471 LLVMValueRef indices[] = {5483 LLVMValueRef indices[] = {
5472 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),5484 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
5473 start_val,5485 start_val,
5474 };5486 };
5475 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");5487 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");
5476 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);5488 if (result_type->id == ZigTypeIdPointer) {
5489 LLVMTypeRef result_ptr_type = get_llvm_type(g, result_type);
5490 LLVMValueRef bitcasted = LLVMBuildBitCast(g->builder, slice_start_ptr, result_ptr_type, "");
5491 gen_store_untyped(g, bitcasted, tmp_struct_ptr, 0, false);
5492 return slice_start_ptr;
5493 } else {
5494 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_ptr_index, "");
5495 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
54775496
5478 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_len_index, "");5497 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_len_index, "");
5479 LLVMValueRef len_value = LLVMBuildNSWSub(g->builder, end_val, start_val, "");5498 LLVMValueRef len_value = LLVMBuildNSWSub(g->builder, end_val, start_val, "");
5480 gen_store_untyped(g, len_value, len_field_ptr, 0, false);5499 gen_store_untyped(g, len_value, len_field_ptr, 0, false);
5500 }
54815501
5482 return tmp_struct_ptr;5502 return tmp_struct_ptr;
5483 } else if (array_type->id == ZigTypeIdPointer) {5503 } else if (array_type->id == ZigTypeIdPointer) {
...@@ -5493,14 +5513,21 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrI...@@ -5493,14 +5513,21 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrI
5493 }5513 }
5494 }5514 }
54955515
5516 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, &start_val, 1, "");
5517 if (result_type->id == ZigTypeIdPointer) {
5518 LLVMTypeRef result_ptr_type = get_llvm_type(g, result_type);
5519 LLVMValueRef bitcasted = LLVMBuildBitCast(g->builder, slice_start_ptr, result_ptr_type, "");
5520 gen_store_untyped(g, bitcasted, tmp_struct_ptr, 0, false);
5521 return bitcasted;
5522 }
5523
5496 if (type_has_bits(g, array_type)) {5524 if (type_has_bits(g, array_type)) {
5497 size_t gen_ptr_index = instruction->base.value->type->data.structure.fields[slice_ptr_index]->gen_index;5525 size_t gen_ptr_index = result_type->data.structure.fields[slice_ptr_index]->gen_index;
5498 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, gen_ptr_index, "");5526 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, gen_ptr_index, "");
5499 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, &start_val, 1, "");
5500 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);5527 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
5501 }5528 }
55025529
5503 size_t gen_len_index = instruction->base.value->type->data.structure.fields[slice_len_index]->gen_index;5530 size_t gen_len_index = result_type->data.structure.fields[slice_len_index]->gen_index;
5504 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, gen_len_index, "");5531 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, gen_len_index, "");
5505 LLVMValueRef len_value = LLVMBuildNSWSub(g->builder, end_val, start_val, "");5532 LLVMValueRef len_value = LLVMBuildNSWSub(g->builder, end_val, start_val, "");
5506 gen_store_untyped(g, len_value, len_field_ptr, 0, false);5533 gen_store_untyped(g, len_value, len_field_ptr, 0, false);
...@@ -5510,7 +5537,9 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrI...@@ -5510,7 +5537,9 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrI
5510 assert(array_type->data.structure.special == StructSpecialSlice);5537 assert(array_type->data.structure.special == StructSpecialSlice);
5511 assert(LLVMGetTypeKind(LLVMTypeOf(array_ptr)) == LLVMPointerTypeKind);5538 assert(LLVMGetTypeKind(LLVMTypeOf(array_ptr)) == LLVMPointerTypeKind);
5512 assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(array_ptr))) == LLVMStructTypeKind);5539 assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(array_ptr))) == LLVMStructTypeKind);
5513 assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(tmp_struct_ptr))) == LLVMStructTypeKind);5540 if (result_type->id != ZigTypeIdPointer) {
5541 assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(tmp_struct_ptr))) == LLVMStructTypeKind);
5542 }
55145543
5515 size_t ptr_index = array_type->data.structure.fields[slice_ptr_index]->gen_index;5544 size_t ptr_index = array_type->data.structure.fields[slice_ptr_index]->gen_index;
5516 assert(ptr_index != SIZE_MAX);5545 assert(ptr_index != SIZE_MAX);
...@@ -5547,15 +5576,22 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrI...@@ -5547,15 +5576,22 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutableGen *executable, IrI
5547 }5576 }
5548 }5577 }
55495578
5550 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, (unsigned)ptr_index, "");
5551 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, src_ptr, &start_val, 1, "");5579 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, src_ptr, &start_val, 1, "");
5552 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);5580 if (result_type->id == ZigTypeIdPointer) {
5581 LLVMTypeRef result_ptr_type = get_llvm_type(g, result_type);
5582 LLVMValueRef bitcasted = LLVMBuildBitCast(g->builder, slice_start_ptr, result_ptr_type, "");
5583 gen_store_untyped(g, bitcasted, tmp_struct_ptr, 0, false);
5584 return bitcasted;
5585 } else {
5586 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, (unsigned)ptr_index, "");
5587 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
55535588
5554 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, (unsigned)len_index, "");5589 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, (unsigned)len_index, "");
5555 LLVMValueRef len_value = LLVMBuildNSWSub(g->builder, end_val, start_val, "");5590 LLVMValueRef len_value = LLVMBuildNSWSub(g->builder, end_val, start_val, "");
5556 gen_store_untyped(g, len_value, len_field_ptr, 0, false);5591 gen_store_untyped(g, len_value, len_field_ptr, 0, false);
55575592
5558 return tmp_struct_ptr;5593 return tmp_struct_ptr;
5594 }
5559 } else {5595 } else {
5560 zig_unreachable();5596 zig_unreachable();
5561 }5597 }
src/ir.cpp+61-18
...@@ -849,11 +849,6 @@ static bool is_slice(ZigType *type) {...@@ -849,11 +849,6 @@ static bool is_slice(ZigType *type) {
849 return type->id == ZigTypeIdStruct && type->data.structure.special == StructSpecialSlice;849 return type->id == ZigTypeIdStruct && type->data.structure.special == StructSpecialSlice;
850}850}
851851
852static bool slice_is_const(ZigType *type) {
853 assert(is_slice(type));
854 return type->data.structure.fields[slice_ptr_index]->type_entry->data.pointer.is_const;
855}
856
857// This function returns true when you can change the type of a ZigValue and the852// This function returns true when you can change the type of a ZigValue and the
858// value remains meaningful.853// value remains meaningful.
859static bool types_have_same_zig_comptime_repr(CodeGen *codegen, ZigType *expected, ZigType *actual) {854static bool types_have_same_zig_comptime_repr(CodeGen *codegen, ZigType *expected, ZigType *actual) {
...@@ -26206,7 +26201,6 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i...@@ -26206,7 +26201,6 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
26206 return ira->codegen->invalid_inst_gen;26201 return ira->codegen->invalid_inst_gen;
26207 }26202 }
2620826203
26209 ZigType *return_type;
26210 ZigValue *sentinel_val = nullptr;26204 ZigValue *sentinel_val = nullptr;
26211 if (instruction->sentinel) {26205 if (instruction->sentinel) {
26212 IrInstGen *uncasted_sentinel = instruction->sentinel->child;26206 IrInstGen *uncasted_sentinel = instruction->sentinel->child;
...@@ -26218,6 +26212,46 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i...@@ -26218,6 +26212,46 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
26218 sentinel_val = ir_resolve_const(ira, sentinel, UndefBad);26212 sentinel_val = ir_resolve_const(ira, sentinel, UndefBad);
26219 if (sentinel_val == nullptr)26213 if (sentinel_val == nullptr)
26220 return ira->codegen->invalid_inst_gen;26214 return ira->codegen->invalid_inst_gen;
26215 }
26216
26217 // If start index and end index are both comptime known, then the result type is a pointer to array
26218 // not a slice.
26219 ZigType *return_type;
26220
26221 if (value_is_comptime(casted_start->value) &&
26222 ((end != nullptr && value_is_comptime(end->value)) || array_type->id == ZigTypeIdArray ))
26223 {
26224 ZigValue *start_val = ir_resolve_const(ira, casted_start, UndefBad);
26225 if (!start_val)
26226 return ira->codegen->invalid_inst_gen;
26227
26228 uint64_t start_scalar = bigint_as_u64(&start_val->data.x_bigint);
26229
26230 uint64_t end_scalar;
26231 if (end != nullptr) {
26232 ZigValue *end_val = ir_resolve_const(ira, end, UndefBad);
26233 if (!end_val)
26234 return ira->codegen->invalid_inst_gen;
26235 end_scalar = bigint_as_u64(&end_val->data.x_bigint);
26236 } else {
26237 end_scalar = array_type->data.array.len;
26238 }
26239 ZigValue *array_sentinel = (array_type->id == ZigTypeIdArray && end_scalar == array_type->data.array.len)
26240 ? sentinel_val : nullptr;
26241
26242 if (start_scalar > end_scalar) {
26243 ir_add_error(ira, &instruction->base.base, buf_sprintf("out of bounds slice"));
26244 return ira->codegen->invalid_inst_gen;
26245 }
26246
26247 ZigType *return_array_type = get_array_type(ira->codegen, elem_type, end_scalar - start_scalar,
26248 array_sentinel);
26249 return_type = get_pointer_to_type_extra(ira->codegen, return_array_type,
26250 non_sentinel_slice_ptr_type->data.pointer.is_const,
26251 non_sentinel_slice_ptr_type->data.pointer.is_volatile,
26252 PtrLenSingle,
26253 0, 0, 0, false);
26254 } else if (sentinel_val != nullptr) {
26221 ZigType *slice_ptr_type = adjust_ptr_sentinel(ira->codegen, non_sentinel_slice_ptr_type, sentinel_val);26255 ZigType *slice_ptr_type = adjust_ptr_sentinel(ira->codegen, non_sentinel_slice_ptr_type, sentinel_val);
26222 return_type = get_slice_type(ira->codegen, slice_ptr_type);26256 return_type = get_slice_type(ira->codegen, slice_ptr_type);
26223 } else {26257 } else {
...@@ -26401,15 +26435,25 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i...@@ -26401,15 +26435,25 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
26401 }26435 }
2640226436
26403 IrInstGen *result = ir_const(ira, &instruction->base.base, return_type);26437 IrInstGen *result = ir_const(ira, &instruction->base.base, return_type);
26404 ZigValue *out_val = result->value;
26405 out_val->data.x_struct.fields = alloc_const_vals_ptrs(ira->codegen, 2);
2640626438
26407 ZigValue *ptr_val = out_val->data.x_struct.fields[slice_ptr_index];26439 ZigValue *ptr_val;
26440 if (return_type->id == ZigTypeIdPointer) {
26441 // pointer to array
26442 ptr_val = result->value;
26443 } else {
26444 // slice
26445 result->value->data.x_struct.fields = alloc_const_vals_ptrs(ira->codegen, 2);
26446
26447 ptr_val = result->value->data.x_struct.fields[slice_ptr_index];
26448
26449 ZigValue *len_val = result->value->data.x_struct.fields[slice_len_index];
26450 init_const_usize(ira->codegen, len_val, end_scalar - start_scalar);
26451 }
2640826452
26453 bool return_type_is_const = non_sentinel_slice_ptr_type->data.pointer.is_const;
26409 if (array_val) {26454 if (array_val) {
26410 size_t index = abs_offset + start_scalar;26455 size_t index = abs_offset + start_scalar;
26411 bool is_const = slice_is_const(return_type);26456 init_const_ptr_array(ira->codegen, ptr_val, array_val, index, return_type_is_const, PtrLenUnknown);
26412 init_const_ptr_array(ira->codegen, ptr_val, array_val, index, is_const, PtrLenUnknown);
26413 if (array_type->id == ZigTypeIdArray) {26457 if (array_type->id == ZigTypeIdArray) {
26414 ptr_val->data.x_ptr.mut = ptr_ptr->value->data.x_ptr.mut;26458 ptr_val->data.x_ptr.mut = ptr_ptr->value->data.x_ptr.mut;
26415 } else if (is_slice(array_type)) {26459 } else if (is_slice(array_type)) {
...@@ -26419,15 +26463,15 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i...@@ -26419,15 +26463,15 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
26419 }26463 }
26420 } else if (ptr_is_undef) {26464 } else if (ptr_is_undef) {
26421 ptr_val->type = get_pointer_to_type(ira->codegen, parent_ptr->type->data.pointer.child_type,26465 ptr_val->type = get_pointer_to_type(ira->codegen, parent_ptr->type->data.pointer.child_type,
26422 slice_is_const(return_type));26466 return_type_is_const);
26423 ptr_val->special = ConstValSpecialUndef;26467 ptr_val->special = ConstValSpecialUndef;
26424 } else switch (parent_ptr->data.x_ptr.special) {26468 } else switch (parent_ptr->data.x_ptr.special) {
26425 case ConstPtrSpecialInvalid:26469 case ConstPtrSpecialInvalid:
26426 case ConstPtrSpecialDiscard:26470 case ConstPtrSpecialDiscard:
26427 zig_unreachable();26471 zig_unreachable();
26428 case ConstPtrSpecialRef:26472 case ConstPtrSpecialRef:
26429 init_const_ptr_ref(ira->codegen, ptr_val,26473 init_const_ptr_ref(ira->codegen, ptr_val, parent_ptr->data.x_ptr.data.ref.pointee,
26430 parent_ptr->data.x_ptr.data.ref.pointee, slice_is_const(return_type));26474 return_type_is_const);
26431 break;26475 break;
26432 case ConstPtrSpecialBaseArray:26476 case ConstPtrSpecialBaseArray:
26433 zig_unreachable();26477 zig_unreachable();
...@@ -26443,7 +26487,7 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i...@@ -26443,7 +26487,7 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
26443 init_const_ptr_hard_coded_addr(ira->codegen, ptr_val,26487 init_const_ptr_hard_coded_addr(ira->codegen, ptr_val,
26444 parent_ptr->type->data.pointer.child_type,26488 parent_ptr->type->data.pointer.child_type,
26445 parent_ptr->data.x_ptr.data.hard_coded_addr.addr + start_scalar,26489 parent_ptr->data.x_ptr.data.hard_coded_addr.addr + start_scalar,
26446 slice_is_const(return_type));26490 return_type_is_const);
26447 break;26491 break;
26448 case ConstPtrSpecialFunction:26492 case ConstPtrSpecialFunction:
26449 zig_panic("TODO");26493 zig_panic("TODO");
...@@ -26451,9 +26495,8 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i...@@ -26451,9 +26495,8 @@ static IrInstGen *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstSrcSlice *i
26451 zig_panic("TODO");26495 zig_panic("TODO");
26452 }26496 }
2645326497
26454 ZigValue *len_val = out_val->data.x_struct.fields[slice_len_index];26498 // In the case of pointer-to-array, we must restore this because above it overwrites ptr_val->type
26455 init_const_usize(ira->codegen, len_val, end_scalar - start_scalar);26499 result->value->type = return_type;
26456
26457 return result;26500 return result;
26458 }26501 }
2645926502