mirror of
https://github.com/ziglang/zig.git
synced 2026-01-21 06:45:24 +00:00
implement unknown size array indexing and slicing
This commit is contained in:
parent
2a8d6af7ba
commit
d14a31100f
@ -1373,7 +1373,7 @@ static TypeTableEntry *analyze_slice_expr(CodeGen *g, ImportTableEntry *import,
|
||||
array_type->data.structure.is_unknown_size_array)
|
||||
{
|
||||
return_type = get_unknown_size_array_type(g, import,
|
||||
array_type->data.structure.fields[0].type_entry,
|
||||
array_type->data.structure.fields[0].type_entry->data.pointer.child_type,
|
||||
node->data.slice_expr.is_const);
|
||||
} else {
|
||||
add_node_error(g, node,
|
||||
@ -1405,15 +1405,19 @@ static TypeTableEntry *analyze_array_access_expr(CodeGen *g, ImportTableEntry *i
|
||||
|
||||
TypeTableEntry *return_type;
|
||||
|
||||
if (array_type->id == TypeTableEntryIdArray) {
|
||||
if (array_type->id == TypeTableEntryIdInvalid) {
|
||||
return_type = g->builtin_types.entry_invalid;
|
||||
} else if (array_type->id == TypeTableEntryIdArray) {
|
||||
return_type = array_type->data.array.child_type;
|
||||
} else if (array_type->id == TypeTableEntryIdPointer) {
|
||||
return_type = array_type->data.pointer.child_type;
|
||||
} else if (array_type->id == TypeTableEntryIdStruct &&
|
||||
array_type->data.structure.is_unknown_size_array)
|
||||
{
|
||||
return_type = array_type->data.structure.fields[0].type_entry->data.pointer.child_type;
|
||||
} else {
|
||||
if (array_type->id != TypeTableEntryIdInvalid) {
|
||||
add_node_error(g, node,
|
||||
buf_sprintf("array access of non-array type '%s'", buf_ptr(&array_type->name)));
|
||||
}
|
||||
add_node_error(g, node,
|
||||
buf_sprintf("array access of non-array type '%s'", buf_ptr(&array_type->name)));
|
||||
return_type = g->builtin_types.entry_invalid;
|
||||
}
|
||||
|
||||
|
||||
@ -260,6 +260,15 @@ static LLVMValueRef gen_array_ptr(CodeGen *g, AstNode *node) {
|
||||
};
|
||||
add_debug_source_node(g, node);
|
||||
return LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 1, "");
|
||||
} else if (type_entry->id == TypeTableEntryIdStruct) {
|
||||
assert(type_entry->data.structure.is_unknown_size_array);
|
||||
assert(LLVMGetTypeKind(LLVMTypeOf(array_ptr)) == LLVMPointerTypeKind);
|
||||
assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(array_ptr))) == LLVMStructTypeKind);
|
||||
|
||||
add_debug_source_node(g, node);
|
||||
LLVMValueRef ptr_ptr = LLVMBuildStructGEP(g->builder, array_ptr, 0, "");
|
||||
LLVMValueRef ptr = LLVMBuildLoad(g->builder, ptr_ptr, "");
|
||||
return LLVMBuildInBoundsGEP(g->builder, ptr, &subscript_value, 1, "");
|
||||
} else {
|
||||
zig_unreachable();
|
||||
}
|
||||
@ -314,9 +323,9 @@ static LLVMValueRef gen_slice_expr(CodeGen *g, AstNode *node) {
|
||||
TypeTableEntry *array_type = get_expr_type(array_ref_node);
|
||||
|
||||
LLVMValueRef tmp_struct_ptr = node->codegen_node->data.struct_val_expr_node.ptr;
|
||||
LLVMValueRef array_ptr = gen_array_base_ptr(g, array_ref_node);
|
||||
|
||||
if (array_type->id == TypeTableEntryIdArray) {
|
||||
LLVMValueRef array_ptr = gen_array_base_ptr(g, array_ref_node);
|
||||
LLVMValueRef start_val = gen_expr(g, node->data.slice_expr.start);
|
||||
LLVMValueRef end_val;
|
||||
if (node->data.slice_expr.end) {
|
||||
@ -343,7 +352,31 @@ static LLVMValueRef gen_slice_expr(CodeGen *g, AstNode *node) {
|
||||
zig_panic("TODO gen_slice_expr pointer");
|
||||
} else if (array_type->id == TypeTableEntryIdStruct) {
|
||||
assert(array_type->data.structure.is_unknown_size_array);
|
||||
zig_panic("TODO gen_slice_expr unknown size array");
|
||||
assert(LLVMGetTypeKind(LLVMTypeOf(array_ptr)) == LLVMPointerTypeKind);
|
||||
assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(array_ptr))) == LLVMStructTypeKind);
|
||||
|
||||
LLVMValueRef start_val = gen_expr(g, node->data.slice_expr.start);
|
||||
LLVMValueRef end_val;
|
||||
if (node->data.slice_expr.end) {
|
||||
end_val = gen_expr(g, node->data.slice_expr.end);
|
||||
} else {
|
||||
add_debug_source_node(g, node);
|
||||
LLVMValueRef src_len_ptr = LLVMBuildStructGEP(g->builder, array_ptr, 1, "");
|
||||
end_val = LLVMBuildLoad(g->builder, src_len_ptr, "");
|
||||
}
|
||||
|
||||
add_debug_source_node(g, node);
|
||||
LLVMValueRef src_ptr_ptr = LLVMBuildStructGEP(g->builder, array_ptr, 0, "");
|
||||
LLVMValueRef src_ptr = LLVMBuildLoad(g->builder, src_ptr_ptr, "");
|
||||
LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, 0, "");
|
||||
LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, src_ptr, &start_val, 1, "");
|
||||
LLVMBuildStore(g->builder, slice_start_ptr, ptr_field_ptr);
|
||||
|
||||
LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, 1, "");
|
||||
LLVMValueRef len_value = LLVMBuildSub(g->builder, end_val, start_val, "");
|
||||
LLVMBuildStore(g->builder, len_value, len_field_ptr);
|
||||
|
||||
return tmp_struct_ptr;
|
||||
} else {
|
||||
zig_unreachable();
|
||||
}
|
||||
@ -421,6 +454,10 @@ static LLVMValueRef gen_lvalue(CodeGen *g, AstNode *expr_node, AstNode *node,
|
||||
} else if (array_type->id == TypeTableEntryIdPointer) {
|
||||
*out_type_entry = array_type->data.pointer.child_type;
|
||||
target_ref = gen_array_ptr(g, node);
|
||||
} else if (array_type->id == TypeTableEntryIdStruct) {
|
||||
assert(array_type->data.structure.is_unknown_size_array);
|
||||
*out_type_entry = array_type->data.structure.fields[0].type_entry->data.pointer.child_type;
|
||||
target_ref = gen_array_ptr(g, node);
|
||||
} else {
|
||||
zig_unreachable();
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@ export fn memset(dest: &u8, c: u8, n: usize) -> &u8 {
|
||||
return dest;
|
||||
}
|
||||
|
||||
// TODO annotate parameters with noalias
|
||||
export fn memcpy(dest: &u8, src: &const u8, n: usize) -> &u8 {
|
||||
var index : #typeof(n) = 0;
|
||||
while (index != n) {
|
||||
|
||||
@ -45,8 +45,7 @@ pub struct Rand {
|
||||
var rand_val_array : [#sizeof(u32)]u8;
|
||||
*(rand_val_array.ptr as &u32) = r.get_u32();
|
||||
while (bytes_left > 0) {
|
||||
// TODO array index operator so we can remove the .ptr
|
||||
buf.ptr[buf.len - bytes_left] = rand_val_array[#sizeof(u32) - bytes_left];
|
||||
buf[buf.len - bytes_left] = rand_val_array[#sizeof(u32) - bytes_left];
|
||||
bytes_left -= 1;
|
||||
}
|
||||
}
|
||||
@ -88,8 +87,7 @@ pub struct Rand {
|
||||
fn get_bytes_aligned(r: &Rand, buf: []u8) -> usize {
|
||||
var bytes_left = buf.len;
|
||||
while (bytes_left >= 4) {
|
||||
// TODO: array access so we can remove .ptr
|
||||
*(&buf.ptr[buf.len - bytes_left] as &u32) = r.get_u32();
|
||||
*(&buf[buf.len - bytes_left] as &u32) = r.get_u32();
|
||||
bytes_left -= #sizeof(u32);
|
||||
}
|
||||
return bytes_left;
|
||||
|
||||
18
std/std.zig
18
std/std.zig
@ -26,7 +26,7 @@ pub fn fprint_str(fd: isize, str: []const u8) -> isize {
|
||||
pub fn print_u64(x: u64) -> isize {
|
||||
// TODO use max_u64_base10_digits instead of hardcoding 20
|
||||
var buf: [20]u8;
|
||||
const len = buf_print_u64(buf.ptr, x);
|
||||
const len = buf_print_u64(buf, x);
|
||||
return write(stdout_fileno, buf.ptr, len);
|
||||
}
|
||||
|
||||
@ -35,7 +35,7 @@ pub fn print_u64(x: u64) -> isize {
|
||||
pub fn print_i64(x: i64) -> isize {
|
||||
// TODO use max_u64_base10_digits instead of hardcoding 20
|
||||
var buf: [20]u8;
|
||||
const len = buf_print_i64(buf.ptr, x);
|
||||
const len = buf_print_i64(buf, x);
|
||||
return write(stdout_fileno, buf.ptr, len);
|
||||
}
|
||||
|
||||
@ -56,8 +56,7 @@ pub fn parse_u64(buf: []u8, radix: u8, result: &u64) -> bool {
|
||||
|
||||
var i : #typeof(buf.len) = 0;
|
||||
while (i < buf.len) {
|
||||
// TODO array indexing operator
|
||||
const c = buf.ptr[i];
|
||||
const c = buf[i];
|
||||
const digit = char_to_digit(c);
|
||||
|
||||
if (digit > radix) {
|
||||
@ -100,20 +99,16 @@ fn char_to_digit(c: u8) -> u8 {
|
||||
|
||||
const max_u64_base10_digits: usize = 20;
|
||||
|
||||
// TODO use an array for out_buf instead of pointer. this should give bounds checking in
|
||||
// debug mode and length can get optimized out in release mode. requires array slicing syntax
|
||||
// for the buf_print_u64 call.
|
||||
fn buf_print_i64(out_buf: &u8, x: i64) -> usize {
|
||||
fn buf_print_i64(out_buf: []u8, x: i64) -> usize {
|
||||
if (x < 0) {
|
||||
out_buf[0] = '-';
|
||||
return 1 + buf_print_u64(&out_buf[1], ((-(x + 1)) as u64) + 1);
|
||||
return 1 + buf_print_u64(out_buf[1...], ((-(x + 1)) as u64) + 1);
|
||||
} else {
|
||||
return buf_print_u64(out_buf, x as u64);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO use an array for out_buf instead of pointer.
|
||||
fn buf_print_u64(out_buf: &u8, x: u64) -> usize {
|
||||
fn buf_print_u64(out_buf: []u8, x: u64) -> usize {
|
||||
var buf: [max_u64_base10_digits]u8;
|
||||
var a = x;
|
||||
var index = buf.len;
|
||||
@ -130,6 +125,7 @@ fn buf_print_u64(out_buf: &u8, x: u64) -> usize {
|
||||
const len = buf.len - index;
|
||||
|
||||
// TODO memcpy intrinsic
|
||||
// @memcpy(out_buf, buf, len);
|
||||
var i: usize = 0;
|
||||
while (i < len) {
|
||||
out_buf[i] = buf[index + i];
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user