| author | |
| committer | |
| log | d14a31100f3f4e7b8d43c8ad794a82da36532aa7 |
| tree | 9cb70f9785427c24499dff91bc3383837c7de78d |
| parent | 2a8d6af7ba9dcea5f13e306f2d032f3f344950af |
5 files changed, 59 insertions(+), 23 deletions(-)
src/analyze.cpp+10-6| ... | @@ -1373,7 +1373,7 @@ static TypeTableEntry *analyze_slice_expr(CodeGen *g, ImportTableEntry *import, | ... | @@ -1373,7 +1373,7 @@ static TypeTableEntry *analyze_slice_expr(CodeGen *g, ImportTableEntry *import, |
| 1373 | array_type->data.structure.is_unknown_size_array) | 1373 | array_type->data.structure.is_unknown_size_array) |
| 1374 | { | 1374 | { |
| 1375 | return_type = get_unknown_size_array_type(g, import, | 1375 | return_type = get_unknown_size_array_type(g, import, |
| 1376 | array_type->data.structure.fields[0].type_entry, | 1376 | array_type->data.structure.fields[0].type_entry->data.pointer.child_type, |
| 1377 | node->data.slice_expr.is_const); | 1377 | node->data.slice_expr.is_const); |
| 1378 | } else { | 1378 | } else { |
| 1379 | add_node_error(g, node, | 1379 | add_node_error(g, node, |
| ... | @@ -1405,15 +1405,19 @@ static TypeTableEntry *analyze_array_access_expr(CodeGen *g, ImportTableEntry *i | ... | @@ -1405,15 +1405,19 @@ static TypeTableEntry *analyze_array_access_expr(CodeGen *g, ImportTableEntry *i |
| 1405 | 1405 | ||
| 1406 | TypeTableEntry *return_type; | 1406 | TypeTableEntry *return_type; |
| 1407 | 1407 | ||
| 1408 | if (array_type->id == TypeTableEntryIdArray) { | 1408 | if (array_type->id == TypeTableEntryIdInvalid) { |
| 1409 | return_type = g->builtin_types.entry_invalid; | ||
| 1410 | } else if (array_type->id == TypeTableEntryIdArray) { | ||
| 1409 | return_type = array_type->data.array.child_type; | 1411 | return_type = array_type->data.array.child_type; |
| 1410 | } else if (array_type->id == TypeTableEntryIdPointer) { | 1412 | } else if (array_type->id == TypeTableEntryIdPointer) { |
| 1411 | return_type = array_type->data.pointer.child_type; | 1413 | return_type = array_type->data.pointer.child_type; |
| 1414 | } else if (array_type->id == TypeTableEntryIdStruct && | ||
| 1415 | array_type->data.structure.is_unknown_size_array) | ||
| 1416 | { | ||
| 1417 | return_type = array_type->data.structure.fields[0].type_entry->data.pointer.child_type; | ||
| 1412 | } else { | 1418 | } else { |
| 1413 | if (array_type->id != TypeTableEntryIdInvalid) { | 1419 | add_node_error(g, node, |
| 1414 | add_node_error(g, node, | 1420 | buf_sprintf("array access of non-array type '%s'", buf_ptr(&array_type->name))); |
| 1415 | buf_sprintf("array access of non-array type '%s'", buf_ptr(&array_type->name))); | ||
| 1416 | } | ||
| 1417 | return_type = g->builtin_types.entry_invalid; | 1421 | return_type = g->builtin_types.entry_invalid; |
| 1418 | } | 1422 | } |
| 1419 | 1423 |
src/codegen.cpp+39-2| ... | @@ -260,6 +260,15 @@ static LLVMValueRef gen_array_ptr(CodeGen *g, AstNode *node) { | ... | @@ -260,6 +260,15 @@ static LLVMValueRef gen_array_ptr(CodeGen *g, AstNode *node) { |
| 260 | }; | 260 | }; |
| 261 | add_debug_source_node(g, node); | 261 | add_debug_source_node(g, node); |
| 262 | return LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 1, ""); | 262 | return LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 1, ""); |
| 263 | } else if (type_entry->id == TypeTableEntryIdStruct) { | ||
| 264 | assert(type_entry->data.structure.is_unknown_size_array); | ||
| 265 | assert(LLVMGetTypeKind(LLVMTypeOf(array_ptr)) == LLVMPointerTypeKind); | ||
| 266 | assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(array_ptr))) == LLVMStructTypeKind); | ||
| 267 | |||
| 268 | add_debug_source_node(g, node); | ||
| 269 | LLVMValueRef ptr_ptr = LLVMBuildStructGEP(g->builder, array_ptr, 0, ""); | ||
| 270 | LLVMValueRef ptr = LLVMBuildLoad(g->builder, ptr_ptr, ""); | ||
| 271 | return LLVMBuildInBoundsGEP(g->builder, ptr, &subscript_value, 1, ""); | ||
| 263 | } else { | 272 | } else { |
| 264 | zig_unreachable(); | 273 | zig_unreachable(); |
| 265 | } | 274 | } |
| ... | @@ -314,9 +323,9 @@ static LLVMValueRef gen_slice_expr(CodeGen *g, AstNode *node) { | ... | @@ -314,9 +323,9 @@ static LLVMValueRef gen_slice_expr(CodeGen *g, AstNode *node) { |
| 314 | TypeTableEntry *array_type = get_expr_type(array_ref_node); | 323 | TypeTableEntry *array_type = get_expr_type(array_ref_node); |
| 315 | 324 | ||
| 316 | LLVMValueRef tmp_struct_ptr = node->codegen_node->data.struct_val_expr_node.ptr; | 325 | LLVMValueRef tmp_struct_ptr = node->codegen_node->data.struct_val_expr_node.ptr; |
| 326 | LLVMValueRef array_ptr = gen_array_base_ptr(g, array_ref_node); | ||
| 317 | 327 | ||
| 318 | if (array_type->id == TypeTableEntryIdArray) { | 328 | if (array_type->id == TypeTableEntryIdArray) { |
| 319 | LLVMValueRef array_ptr = gen_array_base_ptr(g, array_ref_node); | ||
| 320 | LLVMValueRef start_val = gen_expr(g, node->data.slice_expr.start); | 329 | LLVMValueRef start_val = gen_expr(g, node->data.slice_expr.start); |
| 321 | LLVMValueRef end_val; | 330 | LLVMValueRef end_val; |
| 322 | if (node->data.slice_expr.end) { | 331 | if (node->data.slice_expr.end) { |
| ... | @@ -343,7 +352,31 @@ static LLVMValueRef gen_slice_expr(CodeGen *g, AstNode *node) { | ... | @@ -343,7 +352,31 @@ static LLVMValueRef gen_slice_expr(CodeGen *g, AstNode *node) { |
| 343 | zig_panic("TODO gen_slice_expr pointer"); | 352 | zig_panic("TODO gen_slice_expr pointer"); |
| 344 | } else if (array_type->id == TypeTableEntryIdStruct) { | 353 | } else if (array_type->id == TypeTableEntryIdStruct) { |
| 345 | assert(array_type->data.structure.is_unknown_size_array); | 354 | assert(array_type->data.structure.is_unknown_size_array); |
| 346 | zig_panic("TODO gen_slice_expr unknown size array"); | 355 | assert(LLVMGetTypeKind(LLVMTypeOf(array_ptr)) == LLVMPointerTypeKind); |
| 356 | assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(array_ptr))) == LLVMStructTypeKind); | ||
| 357 | |||
| 358 | LLVMValueRef start_val = gen_expr(g, node->data.slice_expr.start); | ||
| 359 | LLVMValueRef end_val; | ||
| 360 | if (node->data.slice_expr.end) { | ||
| 361 | end_val = gen_expr(g, node->data.slice_expr.end); | ||
| 362 | } else { | ||
| 363 | add_debug_source_node(g, node); | ||
| 364 | LLVMValueRef src_len_ptr = LLVMBuildStructGEP(g->builder, array_ptr, 1, ""); | ||
| 365 | end_val = LLVMBuildLoad(g->builder, src_len_ptr, ""); | ||
| 366 | } | ||
| 367 | |||
| 368 | add_debug_source_node(g, node); | ||
| 369 | LLVMValueRef src_ptr_ptr = LLVMBuildStructGEP(g->builder, array_ptr, 0, ""); | ||
| 370 | LLVMValueRef src_ptr = LLVMBuildLoad(g->builder, src_ptr_ptr, ""); | ||
| 371 | LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, 0, ""); | ||
| 372 | LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, src_ptr, &start_val, 1, ""); | ||
| 373 | LLVMBuildStore(g->builder, slice_start_ptr, ptr_field_ptr); | ||
| 374 | |||
| 375 | LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, 1, ""); | ||
| 376 | LLVMValueRef len_value = LLVMBuildSub(g->builder, end_val, start_val, ""); | ||
| 377 | LLVMBuildStore(g->builder, len_value, len_field_ptr); | ||
| 378 | |||
| 379 | return tmp_struct_ptr; | ||
| 347 | } else { | 380 | } else { |
| 348 | zig_unreachable(); | 381 | zig_unreachable(); |
| 349 | } | 382 | } |
| ... | @@ -421,6 +454,10 @@ static LLVMValueRef gen_lvalue(CodeGen *g, AstNode *expr_node, AstNode *node, | ... | @@ -421,6 +454,10 @@ static LLVMValueRef gen_lvalue(CodeGen *g, AstNode *expr_node, AstNode *node, |
| 421 | } else if (array_type->id == TypeTableEntryIdPointer) { | 454 | } else if (array_type->id == TypeTableEntryIdPointer) { |
| 422 | *out_type_entry = array_type->data.pointer.child_type; | 455 | *out_type_entry = array_type->data.pointer.child_type; |
| 423 | target_ref = gen_array_ptr(g, node); | 456 | target_ref = gen_array_ptr(g, node); |
| 457 | } else if (array_type->id == TypeTableEntryIdStruct) { | ||
| 458 | assert(array_type->data.structure.is_unknown_size_array); | ||
| 459 | *out_type_entry = array_type->data.structure.fields[0].type_entry->data.pointer.child_type; | ||
| 460 | target_ref = gen_array_ptr(g, node); | ||
| 424 | } else { | 461 | } else { |
| 425 | zig_unreachable(); | 462 | zig_unreachable(); |
| 426 | } | 463 | } |
std/builtin.zig+1| ... | @@ -10,6 +10,7 @@ export fn memset(dest: &u8, c: u8, n: usize) -> &u8 { | ... | @@ -10,6 +10,7 @@ export fn memset(dest: &u8, c: u8, n: usize) -> &u8 { |
| 10 | return dest; | 10 | return dest; |
| 11 | } | 11 | } |
| 12 | 12 | ||
| 13 | // TODO annotate parameters with noalias | ||
| 13 | export fn memcpy(dest: &u8, src: &const u8, n: usize) -> &u8 { | 14 | export fn memcpy(dest: &u8, src: &const u8, n: usize) -> &u8 { |
| 14 | var index : #typeof(n) = 0; | 15 | var index : #typeof(n) = 0; |
| 15 | while (index != n) { | 16 | while (index != n) { |
std/rand.zig+2-4| ... | @@ -45,8 +45,7 @@ pub struct Rand { | ... | @@ -45,8 +45,7 @@ pub struct Rand { |
| 45 | var rand_val_array : [#sizeof(u32)]u8; | 45 | var rand_val_array : [#sizeof(u32)]u8; |
| 46 | *(rand_val_array.ptr as &u32) = r.get_u32(); | 46 | *(rand_val_array.ptr as &u32) = r.get_u32(); |
| 47 | while (bytes_left > 0) { | 47 | while (bytes_left > 0) { |
| 48 | // TODO array index operator so we can remove the .ptr | 48 | buf[buf.len - bytes_left] = rand_val_array[#sizeof(u32) - bytes_left]; |
| 49 | buf.ptr[buf.len - bytes_left] = rand_val_array[#sizeof(u32) - bytes_left]; | ||
| 50 | bytes_left -= 1; | 49 | bytes_left -= 1; |
| 51 | } | 50 | } |
| 52 | } | 51 | } |
| ... | @@ -88,8 +87,7 @@ pub struct Rand { | ... | @@ -88,8 +87,7 @@ pub struct Rand { |
| 88 | fn get_bytes_aligned(r: &Rand, buf: []u8) -> usize { | 87 | fn get_bytes_aligned(r: &Rand, buf: []u8) -> usize { |
| 89 | var bytes_left = buf.len; | 88 | var bytes_left = buf.len; |
| 90 | while (bytes_left >= 4) { | 89 | while (bytes_left >= 4) { |
| 91 | // TODO: array access so we can remove .ptr | 90 | *(&buf[buf.len - bytes_left] as &u32) = r.get_u32(); |
| 92 | *(&buf.ptr[buf.len - bytes_left] as &u32) = r.get_u32(); | ||
| 93 | bytes_left -= #sizeof(u32); | 91 | bytes_left -= #sizeof(u32); |
| 94 | } | 92 | } |
| 95 | return bytes_left; | 93 | return bytes_left; |
std/std.zig+7-11| ... | @@ -26,7 +26,7 @@ pub fn fprint_str(fd: isize, str: []const u8) -> isize { | ... | @@ -26,7 +26,7 @@ pub fn fprint_str(fd: isize, str: []const u8) -> isize { |
| 26 | pub fn print_u64(x: u64) -> isize { | 26 | pub fn print_u64(x: u64) -> isize { |
| 27 | // TODO use max_u64_base10_digits instead of hardcoding 20 | 27 | // TODO use max_u64_base10_digits instead of hardcoding 20 |
| 28 | var buf: [20]u8; | 28 | var buf: [20]u8; |
| 29 | const len = buf_print_u64(buf.ptr, x); | 29 | const len = buf_print_u64(buf, x); |
| 30 | return write(stdout_fileno, buf.ptr, len); | 30 | return write(stdout_fileno, buf.ptr, len); |
| 31 | } | 31 | } |
| 32 | 32 | ||
| ... | @@ -35,7 +35,7 @@ pub fn print_u64(x: u64) -> isize { | ... | @@ -35,7 +35,7 @@ pub fn print_u64(x: u64) -> isize { |
| 35 | pub fn print_i64(x: i64) -> isize { | 35 | pub fn print_i64(x: i64) -> isize { |
| 36 | // TODO use max_u64_base10_digits instead of hardcoding 20 | 36 | // TODO use max_u64_base10_digits instead of hardcoding 20 |
| 37 | var buf: [20]u8; | 37 | var buf: [20]u8; |
| 38 | const len = buf_print_i64(buf.ptr, x); | 38 | const len = buf_print_i64(buf, x); |
| 39 | return write(stdout_fileno, buf.ptr, len); | 39 | return write(stdout_fileno, buf.ptr, len); |
| 40 | } | 40 | } |
| 41 | 41 | ||
| ... | @@ -56,8 +56,7 @@ pub fn parse_u64(buf: []u8, radix: u8, result: &u64) -> bool { | ... | @@ -56,8 +56,7 @@ pub fn parse_u64(buf: []u8, radix: u8, result: &u64) -> bool { |
| 56 | 56 | ||
| 57 | var i : #typeof(buf.len) = 0; | 57 | var i : #typeof(buf.len) = 0; |
| 58 | while (i < buf.len) { | 58 | while (i < buf.len) { |
| 59 | // TODO array indexing operator | 59 | const c = buf[i]; |
| 60 | const c = buf.ptr[i]; | ||
| 61 | const digit = char_to_digit(c); | 60 | const digit = char_to_digit(c); |
| 62 | 61 | ||
| 63 | if (digit > radix) { | 62 | if (digit > radix) { |
| ... | @@ -100,20 +99,16 @@ fn char_to_digit(c: u8) -> u8 { | ... | @@ -100,20 +99,16 @@ fn char_to_digit(c: u8) -> u8 { |
| 100 | 99 | ||
| 101 | const max_u64_base10_digits: usize = 20; | 100 | const max_u64_base10_digits: usize = 20; |
| 102 | 101 | ||
| 103 | // TODO use an array for out_buf instead of pointer. this should give bounds checking in | 102 | fn buf_print_i64(out_buf: []u8, x: i64) -> usize { |
| 104 | // debug mode and length can get optimized out in release mode. requires array slicing syntax | ||
| 105 | // for the buf_print_u64 call. | ||
| 106 | fn buf_print_i64(out_buf: &u8, x: i64) -> usize { | ||
| 107 | if (x < 0) { | 103 | if (x < 0) { |
| 108 | out_buf[0] = '-'; | 104 | out_buf[0] = '-'; |
| 109 | return 1 + buf_print_u64(&out_buf[1], ((-(x + 1)) as u64) + 1); | 105 | return 1 + buf_print_u64(out_buf[1...], ((-(x + 1)) as u64) + 1); |
| 110 | } else { | 106 | } else { |
| 111 | return buf_print_u64(out_buf, x as u64); | 107 | return buf_print_u64(out_buf, x as u64); |
| 112 | } | 108 | } |
| 113 | } | 109 | } |
| 114 | 110 | ||
| 115 | // TODO use an array for out_buf instead of pointer. | 111 | fn buf_print_u64(out_buf: []u8, x: u64) -> usize { |
| 116 | fn buf_print_u64(out_buf: &u8, x: u64) -> usize { | ||
| 117 | var buf: [max_u64_base10_digits]u8; | 112 | var buf: [max_u64_base10_digits]u8; |
| 118 | var a = x; | 113 | var a = x; |
| 119 | var index = buf.len; | 114 | var index = buf.len; |
| ... | @@ -130,6 +125,7 @@ fn buf_print_u64(out_buf: &u8, x: u64) -> usize { | ... | @@ -130,6 +125,7 @@ fn buf_print_u64(out_buf: &u8, x: u64) -> usize { |
| 130 | const len = buf.len - index; | 125 | const len = buf.len - index; |
| 131 | 126 | ||
| 132 | // TODO memcpy intrinsic | 127 | // TODO memcpy intrinsic |
| 128 | // @memcpy(out_buf, buf, len); | ||
| 133 | var i: usize = 0; | 129 | var i: usize = 0; |
| 134 | while (i < len) { | 130 | while (i < len) { |
| 135 | out_buf[i] = buf[index + i]; | 131 | out_buf[i] = buf[index + i]; |