Commit ae2151a751

Andrew Kelley <superjoe30@gmail.com>
2016-01-19 05:13:14
use signed integer for sizes of things
1 parent 92dccde
example/guess_number/main.zig
@@ -7,7 +7,8 @@ pub fn main(args: [][]u8) i32 => {
     print_str("Welcome to the Guess Number Game in Zig.\n");
 
     var seed : u32;
-    const err = os_get_random_bytes((&u8)(&seed), @sizeof(u32));
+    const seed_bytes = (&u8)(&seed)[0...@sizeof(u32)];
+    const err = os_get_random_bytes(seed_bytes);
     if (err != @sizeof(u32)) {
         // TODO full error message
         fprint_str(stderr_fileno, "unable to get random bytes\n");
@@ -22,7 +23,7 @@ pub fn main(args: [][]u8) i32 => {
     while (true) {
         print_str("\nGuess a number between 1 and 100: ");
         var line_buf : [20]u8;
-        var line_len : usize;
+        var line_len : isize;
         // TODO fix this awkward error handling
         if (readline(line_buf, &line_len) || line_len == line_buf.len) {
             // TODO full error message
src/analyze.cpp
@@ -253,7 +253,7 @@ static void unknown_size_array_type_common_init(CodeGen *g, TypeTableEntry *chil
     entry->data.structure.fields[0].src_index = 0;
     entry->data.structure.fields[0].gen_index = 0;
     entry->data.structure.fields[1].name = buf_create_from_str("len");
-    entry->data.structure.fields[1].type_entry = g->builtin_types.entry_usize;
+    entry->data.structure.fields[1].type_entry = g->builtin_types.entry_isize;
     entry->data.structure.fields[1].src_index = 1;
     entry->data.structure.fields[1].gen_index = 1;
 }
@@ -290,7 +290,7 @@ static TypeTableEntry *get_unknown_size_array_type(CodeGen *g, TypeTableEntry *c
         unsigned element_count = 2;
         LLVMTypeRef element_types[] = {
             pointer_type->type_ref,
-            g->builtin_types.entry_usize->type_ref,
+            g->builtin_types.entry_isize->type_ref,
         };
         LLVMStructSetBody(entry->type_ref, element_types, element_count, false);
 
@@ -298,7 +298,7 @@ static TypeTableEntry *get_unknown_size_array_type(CodeGen *g, TypeTableEntry *c
 
         LLVMZigDIType *di_element_types[] = {
             pointer_type->di_type,
-            g->builtin_types.entry_usize->di_type,
+            g->builtin_types.entry_isize->di_type,
         };
         LLVMZigDIScope *compile_unit_scope = LLVMZigCompileUnitToScope(g->compile_unit);
         entry->di_type = LLVMZigCreateDebugStructType(g->dbuilder, compile_unit_scope,
@@ -1513,7 +1513,7 @@ static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *i
     } else if (struct_type->id == TypeTableEntryIdArray) {
         Buf *name = &node->data.field_access_expr.field_name;
         if (buf_eql_str(name, "len")) {
-            return g->builtin_types.entry_usize;
+            return g->builtin_types.entry_isize;
         } else if (buf_eql_str(name, "ptr")) {
             // TODO determine whether the pointer should be const
             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,
         context->struct_val_expr_alloca_list.append(&node->data.slice_expr.resolved_struct_val_expr);
     }
 
-    analyze_expression(g, import, context, g->builtin_types.entry_usize, node->data.slice_expr.start);
+    analyze_expression(g, import, context, g->builtin_types.entry_isize, node->data.slice_expr.start);
 
     if (node->data.slice_expr.end) {
-        analyze_expression(g, import, context, g->builtin_types.entry_usize, node->data.slice_expr.end);
+        analyze_expression(g, import, context, g->builtin_types.entry_isize, node->data.slice_expr.end);
     }
 
     return return_type;
@@ -1614,7 +1614,7 @@ static TypeTableEntry *analyze_array_access_expr(CodeGen *g, ImportTableEntry *i
         return_type = g->builtin_types.entry_invalid;
     }
 
-    analyze_expression(g, import, context, g->builtin_types.entry_usize, node->data.array_access_expr.subscript);
+    analyze_expression(g, import, context, g->builtin_types.entry_isize, node->data.array_access_expr.subscript);
 
     return return_type;
 }
@@ -2248,7 +2248,7 @@ static TypeTableEntry *analyze_array_type(CodeGen *g, ImportTableEntry *import,
 
     if (size_node) {
         TypeTableEntry *size_type = analyze_expression(g, import, context,
-                g->builtin_types.entry_usize, size_node);
+                g->builtin_types.entry_isize, size_node);
         if (size_type->id == TypeTableEntryIdInvalid) {
             return g->builtin_types.entry_invalid;
         }
@@ -2341,10 +2341,10 @@ static TypeTableEntry *analyze_for_expr(CodeGen *g, ImportTableEntry *import, Bl
     if (index_var_node) {
         Buf *index_var_name = &index_var_node->data.symbol_expr.symbol;
         node->data.for_expr.index_var = add_local_var(g, index_var_node, child_context, index_var_name,
-                g->builtin_types.entry_usize, true);
+                g->builtin_types.entry_isize, true);
     } else {
         node->data.for_expr.index_var = add_local_var(g, node, child_context, nullptr,
-                g->builtin_types.entry_usize, true);
+                g->builtin_types.entry_isize, true);
     }
 
     AstNode *for_body_node = node->data.for_expr.body;
src/codegen.cpp
@@ -511,7 +511,7 @@ static LLVMValueRef gen_array_elem_ptr(CodeGen *g, AstNode *source_node, LLVMVal
 
     if (array_type->id == TypeTableEntryIdArray) {
         LLVMValueRef indices[] = {
-            LLVMConstNull(g->builtin_types.entry_usize->type_ref),
+            LLVMConstNull(g->builtin_types.entry_isize->type_ref),
             subscript_value
         };
         add_debug_source_node(g, source_node);
@@ -606,13 +606,13 @@ static LLVMValueRef gen_slice_expr(CodeGen *g, AstNode *node) {
         if (node->data.slice_expr.end) {
             end_val = gen_expr(g, node->data.slice_expr.end);
         } else {
-            end_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, array_type->data.array.len, false);
+            end_val = LLVMConstInt(g->builtin_types.entry_isize->type_ref, array_type->data.array.len, false);
         }
 
         add_debug_source_node(g, node);
         LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, 0, "");
         LLVMValueRef indices[] = {
-            LLVMConstNull(g->builtin_types.entry_usize->type_ref),
+            LLVMConstNull(g->builtin_types.entry_isize->type_ref),
             start_val,
         };
         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
 
     if (struct_type->id == TypeTableEntryIdArray) {
         if (buf_eql_str(name, "len")) {
-            return LLVMConstInt(g->builtin_types.entry_usize->type_ref,
+            return LLVMConstInt(g->builtin_types.entry_isize->type_ref,
                     struct_type->data.array.len, false);
         } else if (buf_eql_str(name, "ptr")) {
             LLVMValueRef array_val = gen_expr(g, node->data.field_access_expr.struct_expr);
             LLVMValueRef indices[] = {
-                LLVMConstNull(g->builtin_types.entry_usize->type_ref),
-                LLVMConstNull(g->builtin_types.entry_usize->type_ref),
+                LLVMConstNull(g->builtin_types.entry_isize->type_ref),
+                LLVMConstNull(g->builtin_types.entry_isize->type_ref),
             };
             add_debug_source_node(g, node);
             return LLVMBuildInBoundsGEP(g->builder, array_val, indices, 2, "");
@@ -891,7 +891,7 @@ static LLVMValueRef gen_bare_cast(CodeGen *g, AstNode *node, LLVMValueRef expr_v
                 LLVMBuildStore(g->builder, expr_bitcast, ptr_ptr);
 
                 LLVMValueRef len_ptr = LLVMBuildStructGEP(g->builder, cast_node->ptr, 1, "");
-                LLVMValueRef len_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
+                LLVMValueRef len_val = LLVMConstInt(g->builtin_types.entry_isize->type_ref,
                         actual_type->data.array.len, false);
                 LLVMBuildStore(g->builder, len_val, len_ptr);
 
@@ -1656,8 +1656,8 @@ static LLVMValueRef gen_container_init_expr(CodeGen *g, AstNode *node) {
             LLVMValueRef elem_val = gen_expr(g, field_node);
 
             LLVMValueRef indices[] = {
-                LLVMConstNull(g->builtin_types.entry_usize->type_ref),
-                LLVMConstInt(g->builtin_types.entry_usize->type_ref, i, false),
+                LLVMConstNull(g->builtin_types.entry_isize->type_ref),
+                LLVMConstInt(g->builtin_types.entry_isize->type_ref, i, false),
             };
             add_debug_source_node(g, field_node);
             LLVMValueRef elem_ptr = LLVMBuildInBoundsGEP(g->builder, tmp_array_ptr, indices, 2, "");
@@ -1756,7 +1756,7 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {
     VariableTableEntry *index_var = node->data.for_expr.index_var;
     assert(index_var);
     LLVMValueRef index_ptr = index_var->value_ref;
-    LLVMValueRef one_const = LLVMConstInt(g->builtin_types.entry_usize->type_ref, 1, false);
+    LLVMValueRef one_const = LLVMConstInt(g->builtin_types.entry_isize->type_ref, 1, false);
 
     BlockContext *old_block_context = g->cur_block_context;
 
@@ -1770,7 +1770,7 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {
     LLVMValueRef len_val;
     TypeTableEntry *child_type;
     if (array_type->id == TypeTableEntryIdArray) {
-        len_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
+        len_val = LLVMConstInt(g->builtin_types.entry_isize->type_ref,
                 array_type->data.array.len, false);
         child_type = array_type->data.array.child_type;
     } else if (array_type->id == TypeTableEntryIdStruct) {
@@ -2007,8 +2007,8 @@ static LLVMValueRef gen_expr_no_cast(CodeGen *g, AstNode *node) {
                 Buf *str = &node->data.string_literal.buf;
                 LLVMValueRef str_val = find_or_create_string(g, str, node->data.string_literal.c);
                 LLVMValueRef indices[] = {
-                    LLVMConstNull(g->builtin_types.entry_usize->type_ref),
-                    LLVMConstNull(g->builtin_types.entry_usize->type_ref),
+                    LLVMConstNull(g->builtin_types.entry_isize->type_ref),
+                    LLVMConstNull(g->builtin_types.entry_isize->type_ref),
                 };
                 LLVMValueRef ptr_val = LLVMBuildInBoundsGEP(g->builder, str_val, indices, 2, "");
                 return ptr_val;
@@ -2506,7 +2506,7 @@ static void define_builtin_fns(CodeGen *g) {
         builtin_fn->param_types = allocate<TypeTableEntry *>(builtin_fn->param_count);
         builtin_fn->param_types[0] = nullptr; // manually checked later
         builtin_fn->param_types[1] = nullptr; // manually checked later
-        builtin_fn->param_types[2] = g->builtin_types.entry_usize;
+        builtin_fn->param_types[2] = g->builtin_types.entry_isize;
 
         LLVMTypeRef param_types[] = {
             LLVMPointerType(LLVMInt8Type(), 0),
@@ -2529,7 +2529,7 @@ static void define_builtin_fns(CodeGen *g) {
         builtin_fn->param_types = allocate<TypeTableEntry *>(builtin_fn->param_count);
         builtin_fn->param_types[0] = nullptr; // manually checked later
         builtin_fn->param_types[1] = g->builtin_types.entry_u8;
-        builtin_fn->param_types[2] = g->builtin_types.entry_usize;
+        builtin_fn->param_types[2] = g->builtin_types.entry_isize;
 
         LLVMTypeRef param_types[] = {
             LLVMPointerType(LLVMInt8Type(), 0),
std/bootstrap.zig
@@ -3,20 +3,20 @@ import "syscall.zig";
 // The compiler treats this file special by implicitly importing the function `main`
 // from the root source file.
 
-var argc: usize;
+var argc: isize;
 var argv: &&u8;
 var env: &&u8;
 
 #attribute("naked")
 export fn _start() unreachable => {
-    argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> usize));
+    argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> isize));
     argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8));
     env = asm("lea 0x10(%%rsp,%%rdi,8), %[env]": [env] "=r" (-> &&u8));
     call_main()
 }
 
-fn strlen(ptr: &const u8) usize => {
-    var count: usize = 0;
+fn strlen(ptr: &const u8) isize => {
+    var count: isize = 0;
     while (ptr[count] != 0) {
         count += 1;
     }
std/builtin.zig
@@ -1,7 +1,7 @@
 // These functions are provided when not linking against libc because LLVM
 // sometimes generates code that calls them.
 
-export fn memset(dest: &u8, c: u8, n: usize) &u8 => {
+export fn memset(dest: &u8, c: u8, n: isize) &u8 => {
     var index : @typeof(n) = 0;
     while (index != n) {
         dest[index] = c;
@@ -10,7 +10,7 @@ export fn memset(dest: &u8, c: u8, n: usize) &u8 => {
     return dest;
 }
 
-export fn memcpy(noalias dest: &u8, noalias src: &const u8, n: usize) &u8 => {
+export fn memcpy(noalias dest: &u8, noalias src: &const u8, n: isize) &u8 => {
     var index : @typeof(n) = 0;
     while (index != n) {
         dest[index] = src[index];
std/rand.zig
@@ -1,5 +1,5 @@
 // Mersenne Twister
-const ARRAY_SIZE : u16 = 624;
+const ARRAY_SIZE : i16 = 624;
 
 /// Use `rand_init` to initialize this state.
 pub struct Rand {
@@ -13,7 +13,7 @@ pub struct Rand {
         var i : @typeof(ARRAY_SIZE) = 1;
         var prev_value: u64 = seed;
         while (i < ARRAY_SIZE) {
-            r.array[i] = u32((prev_value ^ (prev_value << 30)) * 0x6c078965 + i);
+            r.array[i] = u32((prev_value ^ (prev_value << 30)) * 0x6c078965 + u32(i));
             prev_value = r.array[i];
             i += 1;
         }
@@ -81,7 +81,7 @@ pub struct Rand {
     }
 
     // does not populate the remaining (buf.len % 4) bytes
-    fn get_bytes_aligned(r: &Rand, buf: []u8) usize => {
+    fn get_bytes_aligned(r: &Rand, buf: []u8) isize => {
         var bytes_left = buf.len;
         while (bytes_left >= 4) {
             *((&u32)(&buf[buf.len - bytes_left])) = r.get_u32();
std/std.zig
@@ -5,8 +5,8 @@ pub const stdout_fileno : isize = 1;
 pub const stderr_fileno : isize = 2;
 
 // TODO error handling
-pub fn os_get_random_bytes(buf: &u8, count: usize) isize => {
-    getrandom(buf, count, 0)
+pub fn os_get_random_bytes(buf: []u8) isize => {
+    getrandom(buf.ptr, buf.len, 0)
 }
 
 // TODO error handling
@@ -38,12 +38,12 @@ pub fn print_i64(x: i64) isize => {
 }
 
 // TODO error handling
-pub fn readline(buf: []u8, out_len: &usize) bool => {
+pub fn readline(buf: []u8, out_len: &isize) bool => {
     const amt_read = read(stdin_fileno, buf.ptr, buf.len);
     if (amt_read < 0) {
         return true;
     }
-    *out_len = usize(amt_read);
+    *out_len = isize(amt_read);
     return false;
 }
 
@@ -85,9 +85,9 @@ fn char_to_digit(c: u8) u8 => {
     }
 }
 
-const max_u64_base10_digits: usize = 20;
+const max_u64_base10_digits: isize = 20;
 
-fn buf_print_i64(out_buf: []u8, x: i64) usize => {
+fn buf_print_i64(out_buf: []u8, x: i64) isize => {
     if (x < 0) {
         out_buf[0] = '-';
         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 => {
     }
 }
 
-fn buf_print_u64(out_buf: []u8, x: u64) usize => {
+fn buf_print_u64(out_buf: []u8, x: u64) isize => {
     var buf: [max_u64_base10_digits]u8;
     var a = x;
     var index = buf.len;
std/syscall.zig
@@ -1,35 +1,35 @@
-const SYS_read : usize = 0;
-const SYS_write : usize = 1;
-const SYS_exit : usize = 60;
-const SYS_getrandom : usize = 318;
+const SYS_read : isize = 0;
+const SYS_write : isize = 1;
+const SYS_exit : isize = 60;
+const SYS_getrandom : isize = 318;
 
-fn syscall1(number: usize, arg1: usize) usize => {
+fn syscall1(number: isize, arg1: isize) isize => {
     asm volatile ("syscall"
-        : [ret] "={rax}" (-> usize)
+        : [ret] "={rax}" (-> isize)
         : [number] "{rax}" (number), [arg1] "{rdi}" (arg1)
         : "rcx", "r11")
 }
 
-fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize => {
+fn syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) isize => {
     asm volatile ("syscall"
-        : [ret] "={rax}" (-> usize)
+        : [ret] "={rax}" (-> isize)
         : [number] "{rax}" (number), [arg1] "{rdi}" (arg1), [arg2] "{rsi}" (arg2), [arg3] "{rdx}" (arg3)
         : "rcx", "r11")
 }
 
-pub fn read(fd: isize, buf: &u8, count: usize) isize => {
-    isize(syscall3(SYS_read, usize(fd), usize(buf), count))
+pub fn read(fd: isize, buf: &u8, count: isize) isize => {
+    isize(syscall3(SYS_read, isize(fd), isize(buf), count))
 }
 
-pub fn write(fd: isize, buf: &const u8, count: usize) isize => {
-    isize(syscall3(SYS_write, usize(fd), usize(buf), count))
+pub fn write(fd: isize, buf: &const u8, count: isize) isize => {
+    isize(syscall3(SYS_write, isize(fd), isize(buf), count))
 }
 
 pub fn exit(status: i32) unreachable => {
-    syscall1(SYS_exit, usize(status));
+    syscall1(SYS_exit, isize(status));
     unreachable{}
 }
 
-pub fn getrandom(buf: &u8, count: usize, flags: u32) isize => {
-    isize(syscall3(SYS_getrandom, usize(buf), count, usize(flags)))
+pub fn getrandom(buf: &u8, count: isize, flags: u32) isize => {
+    isize(syscall3(SYS_getrandom, isize(buf), count, isize(flags)))
 }
test/run_tests.cpp
@@ -394,16 +394,16 @@ done:
 import "std.zig";
 
 pub fn main(args: [][]u8) i32 => {
-    var array : [5]u32;
+    var array : [5]i32;
 
-    var i : u32 = 0;
+    var i : i32 = 0;
     while (i < 5) {
         array[i] = i + 1;
         i = array[i];
     }
 
     i = 0;
-    var accumulator = u32(0);
+    var accumulator = i32(0);
     while (i < 5) {
         accumulator += array[i];
 
@@ -420,7 +420,7 @@ pub fn main(args: [][]u8) i32 => {
 
     return 0;
 }
-fn get_array_len(a: []u32) usize => {
+fn get_array_len(a: []i32) isize => {
     a.len
 }
     )SOURCE", "OK\n");
@@ -856,7 +856,7 @@ pub fn main(args: [][]u8) i32 => {
     add_simple_case("constant expressions", R"SOURCE(
 import "std.zig";
 
-const ARRAY_SIZE : u8 = 20;
+const ARRAY_SIZE : i8 = 20;
 
 pub fn main(args: [][]u8) i32 => {
     var array : [ARRAY_SIZE]u8;
@@ -1147,7 +1147,7 @@ pub fn main(args: [][]u8) i32 => {
         print_str("\n");
     }
     for (item, array, index) {
-        print_u64(index);
+        print_i64(index);
         print_str("\n");
     }
     const unknown_size: []u8 = array;
@@ -1156,7 +1156,7 @@ pub fn main(args: [][]u8) i32 => {
         print_str("\n");
     }
     for (item, unknown_size, index) {
-        print_u64(index);
+        print_i64(index);
         print_str("\n");
     }
     return 0;
@@ -1356,9 +1356,9 @@ fn f() => {
                  ".tmp_source.zig:4:12: error: use of undeclared identifier 'i'",
                  ".tmp_source.zig:4:14: error: use of undeclared identifier 'i'",
                  ".tmp_source.zig:5:8: error: array access of non-array",
-                 ".tmp_source.zig:5:9: error: expected type 'usize', got 'bool'",
+                 ".tmp_source.zig:5:9: error: expected type 'isize', got 'bool'",
                  ".tmp_source.zig:5:19: error: array access of non-array",
-                 ".tmp_source.zig:5:20: error: expected type 'usize', got 'bool'");
+                 ".tmp_source.zig:5:20: error: expected type 'isize', got 'bool'");
 
     add_compile_fail_case("variadic functions only allowed in extern", R"SOURCE(
 fn f(...) => {}