authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-18 21:13:14-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-18 21:13:14-07:00
logae2151a751f62bd47b0eac37467c2de9f23101a3
tree3dd22c22a6e6ed9eeed46595925b2e46d4261097
parent92dccde2fd7a26ed6b18af4fc4f2dc6b18766a5f

use signed integer for sizes of things


9 files changed, 68 insertions(+), 67 deletions(-)

example/guess_number/main.zig+3-2
......@@ -7,7 +7,8 @@ pub fn main(args: [][]u8) i32 => {
77 print_str("Welcome to the Guess Number Game in Zig.\n");
88
99 var seed : u32;
10 const err = os_get_random_bytes((&u8)(&seed), @sizeof(u32));
10 const seed_bytes = (&u8)(&seed)[0...@sizeof(u32)];
11 const err = os_get_random_bytes(seed_bytes);
1112 if (err != @sizeof(u32)) {
1213 // TODO full error message
1314 fprint_str(stderr_fileno, "unable to get random bytes\n");
......@@ -22,7 +23,7 @@ pub fn main(args: [][]u8) i32 => {
2223 while (true) {
2324 print_str("\nGuess a number between 1 and 100: ");
2425 var line_buf : [20]u8;
25 var line_len : usize;
26 var line_len : isize;
2627 // TODO fix this awkward error handling
2728 if (readline(line_buf, &line_len) || line_len == line_buf.len) {
2829 // TODO full error message
src/analyze.cpp+10-10
......@@ -253,7 +253,7 @@ static void unknown_size_array_type_common_init(CodeGen *g, TypeTableEntry *chil
253253 entry->data.structure.fields[0].src_index = 0;
254254 entry->data.structure.fields[0].gen_index = 0;
255255 entry->data.structure.fields[1].name = buf_create_from_str("len");
256 entry->data.structure.fields[1].type_entry = g->builtin_types.entry_usize;
256 entry->data.structure.fields[1].type_entry = g->builtin_types.entry_isize;
257257 entry->data.structure.fields[1].src_index = 1;
258258 entry->data.structure.fields[1].gen_index = 1;
259259}
......@@ -290,7 +290,7 @@ static TypeTableEntry *get_unknown_size_array_type(CodeGen *g, TypeTableEntry *c
290290 unsigned element_count = 2;
291291 LLVMTypeRef element_types[] = {
292292 pointer_type->type_ref,
293 g->builtin_types.entry_usize->type_ref,
293 g->builtin_types.entry_isize->type_ref,
294294 };
295295 LLVMStructSetBody(entry->type_ref, element_types, element_count, false);
296296
......@@ -298,7 +298,7 @@ static TypeTableEntry *get_unknown_size_array_type(CodeGen *g, TypeTableEntry *c
298298
299299 LLVMZigDIType *di_element_types[] = {
300300 pointer_type->di_type,
301 g->builtin_types.entry_usize->di_type,
301 g->builtin_types.entry_isize->di_type,
302302 };
303303 LLVMZigDIScope *compile_unit_scope = LLVMZigCompileUnitToScope(g->compile_unit);
304304 entry->di_type = LLVMZigCreateDebugStructType(g->dbuilder, compile_unit_scope,
......@@ -1513,7 +1513,7 @@ static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *i
15131513 } else if (struct_type->id == TypeTableEntryIdArray) {
15141514 Buf *name = &node->data.field_access_expr.field_name;
15151515 if (buf_eql_str(name, "len")) {
1516 return g->builtin_types.entry_usize;
1516 return g->builtin_types.entry_isize;
15171517 } else if (buf_eql_str(name, "ptr")) {
15181518 // TODO determine whether the pointer should be const
15191519 return get_pointer_to_type(g, struct_type->data.array.child_type, false);
......@@ -1581,10 +1581,10 @@ static TypeTableEntry *analyze_slice_expr(CodeGen *g, ImportTableEntry *import,
15811581 context->struct_val_expr_alloca_list.append(&node->data.slice_expr.resolved_struct_val_expr);
15821582 }
15831583
1584 analyze_expression(g, import, context, g->builtin_types.entry_usize, node->data.slice_expr.start);
1584 analyze_expression(g, import, context, g->builtin_types.entry_isize, node->data.slice_expr.start);
15851585
15861586 if (node->data.slice_expr.end) {
1587 analyze_expression(g, import, context, g->builtin_types.entry_usize, node->data.slice_expr.end);
1587 analyze_expression(g, import, context, g->builtin_types.entry_isize, node->data.slice_expr.end);
15881588 }
15891589
15901590 return return_type;
......@@ -1614,7 +1614,7 @@ static TypeTableEntry *analyze_array_access_expr(CodeGen *g, ImportTableEntry *i
16141614 return_type = g->builtin_types.entry_invalid;
16151615 }
16161616
1617 analyze_expression(g, import, context, g->builtin_types.entry_usize, node->data.array_access_expr.subscript);
1617 analyze_expression(g, import, context, g->builtin_types.entry_isize, node->data.array_access_expr.subscript);
16181618
16191619 return return_type;
16201620}
......@@ -2248,7 +2248,7 @@ static TypeTableEntry *analyze_array_type(CodeGen *g, ImportTableEntry *import,
22482248
22492249 if (size_node) {
22502250 TypeTableEntry *size_type = analyze_expression(g, import, context,
2251 g->builtin_types.entry_usize, size_node);
2251 g->builtin_types.entry_isize, size_node);
22522252 if (size_type->id == TypeTableEntryIdInvalid) {
22532253 return g->builtin_types.entry_invalid;
22542254 }
......@@ -2341,10 +2341,10 @@ static TypeTableEntry *analyze_for_expr(CodeGen *g, ImportTableEntry *import, Bl
23412341 if (index_var_node) {
23422342 Buf *index_var_name = &index_var_node->data.symbol_expr.symbol;
23432343 node->data.for_expr.index_var = add_local_var(g, index_var_node, child_context, index_var_name,
2344 g->builtin_types.entry_usize, true);
2344 g->builtin_types.entry_isize, true);
23452345 } else {
23462346 node->data.for_expr.index_var = add_local_var(g, node, child_context, nullptr,
2347 g->builtin_types.entry_usize, true);
2347 g->builtin_types.entry_isize, true);
23482348 }
23492349
23502350 AstNode *for_body_node = node->data.for_expr.body;
src/codegen.cpp+15-15
......@@ -511,7 +511,7 @@ static LLVMValueRef gen_array_elem_ptr(CodeGen *g, AstNode *source_node, LLVMVal
511511
512512 if (array_type->id == TypeTableEntryIdArray) {
513513 LLVMValueRef indices[] = {
514 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
514 LLVMConstNull(g->builtin_types.entry_isize->type_ref),
515515 subscript_value
516516 };
517517 add_debug_source_node(g, source_node);
......@@ -606,13 +606,13 @@ static LLVMValueRef gen_slice_expr(CodeGen *g, AstNode *node) {
606606 if (node->data.slice_expr.end) {
607607 end_val = gen_expr(g, node->data.slice_expr.end);
608608 } else {
609 end_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, array_type->data.array.len, false);
609 end_val = LLVMConstInt(g->builtin_types.entry_isize->type_ref, array_type->data.array.len, false);
610610 }
611611
612612 add_debug_source_node(g, node);
613613 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, 0, "");
614614 LLVMValueRef indices[] = {
615 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
615 LLVMConstNull(g->builtin_types.entry_isize->type_ref),
616616 start_val,
617617 };
618618 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");
......@@ -706,13 +706,13 @@ static LLVMValueRef gen_field_access_expr(CodeGen *g, AstNode *node, bool is_lva
706706
707707 if (struct_type->id == TypeTableEntryIdArray) {
708708 if (buf_eql_str(name, "len")) {
709 return LLVMConstInt(g->builtin_types.entry_usize->type_ref,
709 return LLVMConstInt(g->builtin_types.entry_isize->type_ref,
710710 struct_type->data.array.len, false);
711711 } else if (buf_eql_str(name, "ptr")) {
712712 LLVMValueRef array_val = gen_expr(g, node->data.field_access_expr.struct_expr);
713713 LLVMValueRef indices[] = {
714 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
715 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
714 LLVMConstNull(g->builtin_types.entry_isize->type_ref),
715 LLVMConstNull(g->builtin_types.entry_isize->type_ref),
716716 };
717717 add_debug_source_node(g, node);
718718 return LLVMBuildInBoundsGEP(g->builder, array_val, indices, 2, "");
......@@ -891,7 +891,7 @@ static LLVMValueRef gen_bare_cast(CodeGen *g, AstNode *node, LLVMValueRef expr_v
891891 LLVMBuildStore(g->builder, expr_bitcast, ptr_ptr);
892892
893893 LLVMValueRef len_ptr = LLVMBuildStructGEP(g->builder, cast_node->ptr, 1, "");
894 LLVMValueRef len_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
894 LLVMValueRef len_val = LLVMConstInt(g->builtin_types.entry_isize->type_ref,
895895 actual_type->data.array.len, false);
896896 LLVMBuildStore(g->builder, len_val, len_ptr);
897897
......@@ -1656,8 +1656,8 @@ static LLVMValueRef gen_container_init_expr(CodeGen *g, AstNode *node) {
16561656 LLVMValueRef elem_val = gen_expr(g, field_node);
16571657
16581658 LLVMValueRef indices[] = {
1659 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
1660 LLVMConstInt(g->builtin_types.entry_usize->type_ref, i, false),
1659 LLVMConstNull(g->builtin_types.entry_isize->type_ref),
1660 LLVMConstInt(g->builtin_types.entry_isize->type_ref, i, false),
16611661 };
16621662 add_debug_source_node(g, field_node);
16631663 LLVMValueRef elem_ptr = LLVMBuildInBoundsGEP(g->builder, tmp_array_ptr, indices, 2, "");
......@@ -1756,7 +1756,7 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {
17561756 VariableTableEntry *index_var = node->data.for_expr.index_var;
17571757 assert(index_var);
17581758 LLVMValueRef index_ptr = index_var->value_ref;
1759 LLVMValueRef one_const = LLVMConstInt(g->builtin_types.entry_usize->type_ref, 1, false);
1759 LLVMValueRef one_const = LLVMConstInt(g->builtin_types.entry_isize->type_ref, 1, false);
17601760
17611761 BlockContext *old_block_context = g->cur_block_context;
17621762
......@@ -1770,7 +1770,7 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {
17701770 LLVMValueRef len_val;
17711771 TypeTableEntry *child_type;
17721772 if (array_type->id == TypeTableEntryIdArray) {
1773 len_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
1773 len_val = LLVMConstInt(g->builtin_types.entry_isize->type_ref,
17741774 array_type->data.array.len, false);
17751775 child_type = array_type->data.array.child_type;
17761776 } else if (array_type->id == TypeTableEntryIdStruct) {
......@@ -2007,8 +2007,8 @@ static LLVMValueRef gen_expr_no_cast(CodeGen *g, AstNode *node) {
20072007 Buf *str = &node->data.string_literal.buf;
20082008 LLVMValueRef str_val = find_or_create_string(g, str, node->data.string_literal.c);
20092009 LLVMValueRef indices[] = {
2010 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
2011 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
2010 LLVMConstNull(g->builtin_types.entry_isize->type_ref),
2011 LLVMConstNull(g->builtin_types.entry_isize->type_ref),
20122012 };
20132013 LLVMValueRef ptr_val = LLVMBuildInBoundsGEP(g->builder, str_val, indices, 2, "");
20142014 return ptr_val;
......@@ -2506,7 +2506,7 @@ static void define_builtin_fns(CodeGen *g) {
25062506 builtin_fn->param_types = allocate<TypeTableEntry *>(builtin_fn->param_count);
25072507 builtin_fn->param_types[0] = nullptr; // manually checked later
25082508 builtin_fn->param_types[1] = nullptr; // manually checked later
2509 builtin_fn->param_types[2] = g->builtin_types.entry_usize;
2509 builtin_fn->param_types[2] = g->builtin_types.entry_isize;
25102510
25112511 LLVMTypeRef param_types[] = {
25122512 LLVMPointerType(LLVMInt8Type(), 0),
......@@ -2529,7 +2529,7 @@ static void define_builtin_fns(CodeGen *g) {
25292529 builtin_fn->param_types = allocate<TypeTableEntry *>(builtin_fn->param_count);
25302530 builtin_fn->param_types[0] = nullptr; // manually checked later
25312531 builtin_fn->param_types[1] = g->builtin_types.entry_u8;
2532 builtin_fn->param_types[2] = g->builtin_types.entry_usize;
2532 builtin_fn->param_types[2] = g->builtin_types.entry_isize;
25332533
25342534 LLVMTypeRef param_types[] = {
25352535 LLVMPointerType(LLVMInt8Type(), 0),
std/bootstrap.zig+4-4
......@@ -3,20 +3,20 @@ import "syscall.zig";
33// The compiler treats this file special by implicitly importing the function `main`
44// from the root source file.
55
6var argc: usize;
6var argc: isize;
77var argv: &&u8;
88var env: &&u8;
99
1010#attribute("naked")
1111export fn _start() unreachable => {
12 argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> usize));
12 argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> isize));
1313 argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8));
1414 env = asm("lea 0x10(%%rsp,%%rdi,8), %[env]": [env] "=r" (-> &&u8));
1515 call_main()
1616}
1717
18fn strlen(ptr: &const u8) usize => {
19 var count: usize = 0;
18fn strlen(ptr: &const u8) isize => {
19 var count: isize = 0;
2020 while (ptr[count] != 0) {
2121 count += 1;
2222 }
std/builtin.zig+2-2
......@@ -1,7 +1,7 @@
11// These functions are provided when not linking against libc because LLVM
22// sometimes generates code that calls them.
33
4export fn memset(dest: &u8, c: u8, n: usize) &u8 => {
4export fn memset(dest: &u8, c: u8, n: isize) &u8 => {
55 var index : @typeof(n) = 0;
66 while (index != n) {
77 dest[index] = c;
......@@ -10,7 +10,7 @@ export fn memset(dest: &u8, c: u8, n: usize) &u8 => {
1010 return dest;
1111}
1212
13export fn memcpy(noalias dest: &u8, noalias src: &const u8, n: usize) &u8 => {
13export fn memcpy(noalias dest: &u8, noalias src: &const u8, n: isize) &u8 => {
1414 var index : @typeof(n) = 0;
1515 while (index != n) {
1616 dest[index] = src[index];
std/rand.zig+3-3
......@@ -1,5 +1,5 @@
11// Mersenne Twister
2const ARRAY_SIZE : u16 = 624;
2const ARRAY_SIZE : i16 = 624;
33
44/// Use `rand_init` to initialize this state.
55pub struct Rand {
......@@ -13,7 +13,7 @@ pub struct Rand {
1313 var i : @typeof(ARRAY_SIZE) = 1;
1414 var prev_value: u64 = seed;
1515 while (i < ARRAY_SIZE) {
16 r.array[i] = u32((prev_value ^ (prev_value << 30)) * 0x6c078965 + i);
16 r.array[i] = u32((prev_value ^ (prev_value << 30)) * 0x6c078965 + u32(i));
1717 prev_value = r.array[i];
1818 i += 1;
1919 }
......@@ -81,7 +81,7 @@ pub struct Rand {
8181 }
8282
8383 // does not populate the remaining (buf.len % 4) bytes
84 fn get_bytes_aligned(r: &Rand, buf: []u8) usize => {
84 fn get_bytes_aligned(r: &Rand, buf: []u8) isize => {
8585 var bytes_left = buf.len;
8686 while (bytes_left >= 4) {
8787 *((&u32)(&buf[buf.len - bytes_left])) = r.get_u32();
std/std.zig+7-7
......@@ -5,8 +5,8 @@ pub const stdout_fileno : isize = 1;
55pub const stderr_fileno : isize = 2;
66
77// TODO error handling
8pub fn os_get_random_bytes(buf: &u8, count: usize) isize => {
9 getrandom(buf, count, 0)
8pub fn os_get_random_bytes(buf: []u8) isize => {
9 getrandom(buf.ptr, buf.len, 0)
1010}
1111
1212// TODO error handling
......@@ -38,12 +38,12 @@ pub fn print_i64(x: i64) isize => {
3838}
3939
4040// TODO error handling
41pub fn readline(buf: []u8, out_len: &usize) bool => {
41pub fn readline(buf: []u8, out_len: &isize) bool => {
4242 const amt_read = read(stdin_fileno, buf.ptr, buf.len);
4343 if (amt_read < 0) {
4444 return true;
4545 }
46 *out_len = usize(amt_read);
46 *out_len = isize(amt_read);
4747 return false;
4848}
4949
......@@ -85,9 +85,9 @@ fn char_to_digit(c: u8) u8 => {
8585 }
8686}
8787
88const max_u64_base10_digits: usize = 20;
88const max_u64_base10_digits: isize = 20;
8989
90fn buf_print_i64(out_buf: []u8, x: i64) usize => {
90fn buf_print_i64(out_buf: []u8, x: i64) isize => {
9191 if (x < 0) {
9292 out_buf[0] = '-';
9393 return 1 + buf_print_u64(out_buf[1...], u64(-(x + 1)) + 1);
......@@ -96,7 +96,7 @@ fn buf_print_i64(out_buf: []u8, x: i64) usize => {
9696 }
9797}
9898
99fn buf_print_u64(out_buf: []u8, x: u64) usize => {
99fn buf_print_u64(out_buf: []u8, x: u64) isize => {
100100 var buf: [max_u64_base10_digits]u8;
101101 var a = x;
102102 var index = buf.len;
std/syscall.zig+15-15
......@@ -1,35 +1,35 @@
1const SYS_read : usize = 0;
2const SYS_write : usize = 1;
3const SYS_exit : usize = 60;
4const SYS_getrandom : usize = 318;
1const SYS_read : isize = 0;
2const SYS_write : isize = 1;
3const SYS_exit : isize = 60;
4const SYS_getrandom : isize = 318;
55
6fn syscall1(number: usize, arg1: usize) usize => {
6fn syscall1(number: isize, arg1: isize) isize => {
77 asm volatile ("syscall"
8 : [ret] "={rax}" (-> usize)
8 : [ret] "={rax}" (-> isize)
99 : [number] "{rax}" (number), [arg1] "{rdi}" (arg1)
1010 : "rcx", "r11")
1111}
1212
13fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize => {
13fn syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) isize => {
1414 asm volatile ("syscall"
15 : [ret] "={rax}" (-> usize)
15 : [ret] "={rax}" (-> isize)
1616 : [number] "{rax}" (number), [arg1] "{rdi}" (arg1), [arg2] "{rsi}" (arg2), [arg3] "{rdx}" (arg3)
1717 : "rcx", "r11")
1818}
1919
20pub fn read(fd: isize, buf: &u8, count: usize) isize => {
21 isize(syscall3(SYS_read, usize(fd), usize(buf), count))
20pub fn read(fd: isize, buf: &u8, count: isize) isize => {
21 isize(syscall3(SYS_read, isize(fd), isize(buf), count))
2222}
2323
24pub fn write(fd: isize, buf: &const u8, count: usize) isize => {
25 isize(syscall3(SYS_write, usize(fd), usize(buf), count))
24pub fn write(fd: isize, buf: &const u8, count: isize) isize => {
25 isize(syscall3(SYS_write, isize(fd), isize(buf), count))
2626}
2727
2828pub fn exit(status: i32) unreachable => {
29 syscall1(SYS_exit, usize(status));
29 syscall1(SYS_exit, isize(status));
3030 unreachable{}
3131}
3232
33pub fn getrandom(buf: &u8, count: usize, flags: u32) isize => {
34 isize(syscall3(SYS_getrandom, usize(buf), count, usize(flags)))
33pub fn getrandom(buf: &u8, count: isize, flags: u32) isize => {
34 isize(syscall3(SYS_getrandom, isize(buf), count, isize(flags)))
3535}
test/run_tests.cpp+9-9
......@@ -394,16 +394,16 @@ done:
394394import "std.zig";
395395
396396pub fn main(args: [][]u8) i32 => {
397 var array : [5]u32;
397 var array : [5]i32;
398398
399 var i : u32 = 0;
399 var i : i32 = 0;
400400 while (i < 5) {
401401 array[i] = i + 1;
402402 i = array[i];
403403 }
404404
405405 i = 0;
406 var accumulator = u32(0);
406 var accumulator = i32(0);
407407 while (i < 5) {
408408 accumulator += array[i];
409409
......@@ -420,7 +420,7 @@ pub fn main(args: [][]u8) i32 => {
420420
421421 return 0;
422422}
423fn get_array_len(a: []u32) usize => {
423fn get_array_len(a: []i32) isize => {
424424 a.len
425425}
426426 )SOURCE", "OK\n");
......@@ -856,7 +856,7 @@ pub fn main(args: [][]u8) i32 => {
856856 add_simple_case("constant expressions", R"SOURCE(
857857import "std.zig";
858858
859const ARRAY_SIZE : u8 = 20;
859const ARRAY_SIZE : i8 = 20;
860860
861861pub fn main(args: [][]u8) i32 => {
862862 var array : [ARRAY_SIZE]u8;
......@@ -1147,7 +1147,7 @@ pub fn main(args: [][]u8) i32 => {
11471147 print_str("\n");
11481148 }
11491149 for (item, array, index) {
1150 print_u64(index);
1150 print_i64(index);
11511151 print_str("\n");
11521152 }
11531153 const unknown_size: []u8 = array;
......@@ -1156,7 +1156,7 @@ pub fn main(args: [][]u8) i32 => {
11561156 print_str("\n");
11571157 }
11581158 for (item, unknown_size, index) {
1159 print_u64(index);
1159 print_i64(index);
11601160 print_str("\n");
11611161 }
11621162 return 0;
......@@ -1356,9 +1356,9 @@ fn f() => {
13561356 ".tmp_source.zig:4:12: error: use of undeclared identifier 'i'",
13571357 ".tmp_source.zig:4:14: error: use of undeclared identifier 'i'",
13581358 ".tmp_source.zig:5:8: error: array access of non-array",
1359 ".tmp_source.zig:5:9: error: expected type 'usize', got 'bool'",
1359 ".tmp_source.zig:5:9: error: expected type 'isize', got 'bool'",
13601360 ".tmp_source.zig:5:19: error: array access of non-array",
1361 ".tmp_source.zig:5:20: error: expected type 'usize', got 'bool'");
1361 ".tmp_source.zig:5:20: error: expected type 'isize', got 'bool'");
13621362
13631363 add_compile_fail_case("variadic functions only allowed in extern", R"SOURCE(
13641364fn f(...) => {}