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 => {...@@ -7,7 +7,8 @@ pub fn main(args: [][]u8) i32 => {
7 print_str("Welcome to the Guess Number Game in Zig.\n");7 print_str("Welcome to the Guess Number Game in Zig.\n");
88
9 var seed : u32;9 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);
11 if (err != @sizeof(u32)) {12 if (err != @sizeof(u32)) {
12 // TODO full error message13 // TODO full error message
13 fprint_str(stderr_fileno, "unable to get random bytes\n");14 fprint_str(stderr_fileno, "unable to get random bytes\n");
...@@ -22,7 +23,7 @@ pub fn main(args: [][]u8) i32 => {...@@ -22,7 +23,7 @@ pub fn main(args: [][]u8) i32 => {
22 while (true) {23 while (true) {
23 print_str("\nGuess a number between 1 and 100: ");24 print_str("\nGuess a number between 1 and 100: ");
24 var line_buf : [20]u8;25 var line_buf : [20]u8;
25 var line_len : usize;26 var line_len : isize;
26 // TODO fix this awkward error handling27 // TODO fix this awkward error handling
27 if (readline(line_buf, &line_len) || line_len == line_buf.len) {28 if (readline(line_buf, &line_len) || line_len == line_buf.len) {
28 // TODO full error message29 // 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...@@ -253,7 +253,7 @@ static void unknown_size_array_type_common_init(CodeGen *g, TypeTableEntry *chil
253 entry->data.structure.fields[0].src_index = 0;253 entry->data.structure.fields[0].src_index = 0;
254 entry->data.structure.fields[0].gen_index = 0;254 entry->data.structure.fields[0].gen_index = 0;
255 entry->data.structure.fields[1].name = buf_create_from_str("len");255 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;
257 entry->data.structure.fields[1].src_index = 1;257 entry->data.structure.fields[1].src_index = 1;
258 entry->data.structure.fields[1].gen_index = 1;258 entry->data.structure.fields[1].gen_index = 1;
259}259}
...@@ -290,7 +290,7 @@ static TypeTableEntry *get_unknown_size_array_type(CodeGen *g, TypeTableEntry *c...@@ -290,7 +290,7 @@ static TypeTableEntry *get_unknown_size_array_type(CodeGen *g, TypeTableEntry *c
290 unsigned element_count = 2;290 unsigned element_count = 2;
291 LLVMTypeRef element_types[] = {291 LLVMTypeRef element_types[] = {
292 pointer_type->type_ref,292 pointer_type->type_ref,
293 g->builtin_types.entry_usize->type_ref,293 g->builtin_types.entry_isize->type_ref,
294 };294 };
295 LLVMStructSetBody(entry->type_ref, element_types, element_count, false);295 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...@@ -298,7 +298,7 @@ static TypeTableEntry *get_unknown_size_array_type(CodeGen *g, TypeTableEntry *c
298298
299 LLVMZigDIType *di_element_types[] = {299 LLVMZigDIType *di_element_types[] = {
300 pointer_type->di_type,300 pointer_type->di_type,
301 g->builtin_types.entry_usize->di_type,301 g->builtin_types.entry_isize->di_type,
302 };302 };
303 LLVMZigDIScope *compile_unit_scope = LLVMZigCompileUnitToScope(g->compile_unit);303 LLVMZigDIScope *compile_unit_scope = LLVMZigCompileUnitToScope(g->compile_unit);
304 entry->di_type = LLVMZigCreateDebugStructType(g->dbuilder, compile_unit_scope,304 entry->di_type = LLVMZigCreateDebugStructType(g->dbuilder, compile_unit_scope,
...@@ -1513,7 +1513,7 @@ static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *i...@@ -1513,7 +1513,7 @@ static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *i
1513 } else if (struct_type->id == TypeTableEntryIdArray) {1513 } else if (struct_type->id == TypeTableEntryIdArray) {
1514 Buf *name = &node->data.field_access_expr.field_name;1514 Buf *name = &node->data.field_access_expr.field_name;
1515 if (buf_eql_str(name, "len")) {1515 if (buf_eql_str(name, "len")) {
1516 return g->builtin_types.entry_usize;1516 return g->builtin_types.entry_isize;
1517 } else if (buf_eql_str(name, "ptr")) {1517 } else if (buf_eql_str(name, "ptr")) {
1518 // TODO determine whether the pointer should be const1518 // TODO determine whether the pointer should be const
1519 return get_pointer_to_type(g, struct_type->data.array.child_type, false);1519 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,...@@ -1581,10 +1581,10 @@ static TypeTableEntry *analyze_slice_expr(CodeGen *g, ImportTableEntry *import,
1581 context->struct_val_expr_alloca_list.append(&node->data.slice_expr.resolved_struct_val_expr);1581 context->struct_val_expr_alloca_list.append(&node->data.slice_expr.resolved_struct_val_expr);
1582 }1582 }
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
1586 if (node->data.slice_expr.end) {1586 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);
1588 }1588 }
15891589
1590 return return_type;1590 return return_type;
...@@ -1614,7 +1614,7 @@ static TypeTableEntry *analyze_array_access_expr(CodeGen *g, ImportTableEntry *i...@@ -1614,7 +1614,7 @@ static TypeTableEntry *analyze_array_access_expr(CodeGen *g, ImportTableEntry *i
1614 return_type = g->builtin_types.entry_invalid;1614 return_type = g->builtin_types.entry_invalid;
1615 }1615 }
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
1619 return return_type;1619 return return_type;
1620}1620}
...@@ -2248,7 +2248,7 @@ static TypeTableEntry *analyze_array_type(CodeGen *g, ImportTableEntry *import,...@@ -2248,7 +2248,7 @@ static TypeTableEntry *analyze_array_type(CodeGen *g, ImportTableEntry *import,
22482248
2249 if (size_node) {2249 if (size_node) {
2250 TypeTableEntry *size_type = analyze_expression(g, import, context,2250 TypeTableEntry *size_type = analyze_expression(g, import, context,
2251 g->builtin_types.entry_usize, size_node);2251 g->builtin_types.entry_isize, size_node);
2252 if (size_type->id == TypeTableEntryIdInvalid) {2252 if (size_type->id == TypeTableEntryIdInvalid) {
2253 return g->builtin_types.entry_invalid;2253 return g->builtin_types.entry_invalid;
2254 }2254 }
...@@ -2341,10 +2341,10 @@ static TypeTableEntry *analyze_for_expr(CodeGen *g, ImportTableEntry *import, Bl...@@ -2341,10 +2341,10 @@ static TypeTableEntry *analyze_for_expr(CodeGen *g, ImportTableEntry *import, Bl
2341 if (index_var_node) {2341 if (index_var_node) {
2342 Buf *index_var_name = &index_var_node->data.symbol_expr.symbol;2342 Buf *index_var_name = &index_var_node->data.symbol_expr.symbol;
2343 node->data.for_expr.index_var = add_local_var(g, index_var_node, child_context, index_var_name,2343 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);
2345 } else {2345 } else {
2346 node->data.for_expr.index_var = add_local_var(g, node, child_context, nullptr,2346 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);
2348 }2348 }
23492349
2350 AstNode *for_body_node = node->data.for_expr.body;2350 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...@@ -511,7 +511,7 @@ static LLVMValueRef gen_array_elem_ptr(CodeGen *g, AstNode *source_node, LLVMVal
511511
512 if (array_type->id == TypeTableEntryIdArray) {512 if (array_type->id == TypeTableEntryIdArray) {
513 LLVMValueRef indices[] = {513 LLVMValueRef indices[] = {
514 LLVMConstNull(g->builtin_types.entry_usize->type_ref),514 LLVMConstNull(g->builtin_types.entry_isize->type_ref),
515 subscript_value515 subscript_value
516 };516 };
517 add_debug_source_node(g, source_node);517 add_debug_source_node(g, source_node);
...@@ -606,13 +606,13 @@ static LLVMValueRef gen_slice_expr(CodeGen *g, AstNode *node) {...@@ -606,13 +606,13 @@ static LLVMValueRef gen_slice_expr(CodeGen *g, AstNode *node) {
606 if (node->data.slice_expr.end) {606 if (node->data.slice_expr.end) {
607 end_val = gen_expr(g, node->data.slice_expr.end);607 end_val = gen_expr(g, node->data.slice_expr.end);
608 } else {608 } 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);
610 }610 }
611611
612 add_debug_source_node(g, node);612 add_debug_source_node(g, node);
613 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, 0, "");613 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, 0, "");
614 LLVMValueRef indices[] = {614 LLVMValueRef indices[] = {
615 LLVMConstNull(g->builtin_types.entry_usize->type_ref),615 LLVMConstNull(g->builtin_types.entry_isize->type_ref),
616 start_val,616 start_val,
617 };617 };
618 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");618 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...@@ -706,13 +706,13 @@ static LLVMValueRef gen_field_access_expr(CodeGen *g, AstNode *node, bool is_lva
706706
707 if (struct_type->id == TypeTableEntryIdArray) {707 if (struct_type->id == TypeTableEntryIdArray) {
708 if (buf_eql_str(name, "len")) {708 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,
710 struct_type->data.array.len, false);710 struct_type->data.array.len, false);
711 } else if (buf_eql_str(name, "ptr")) {711 } else if (buf_eql_str(name, "ptr")) {
712 LLVMValueRef array_val = gen_expr(g, node->data.field_access_expr.struct_expr);712 LLVMValueRef array_val = gen_expr(g, node->data.field_access_expr.struct_expr);
713 LLVMValueRef indices[] = {713 LLVMValueRef indices[] = {
714 LLVMConstNull(g->builtin_types.entry_usize->type_ref),714 LLVMConstNull(g->builtin_types.entry_isize->type_ref),
715 LLVMConstNull(g->builtin_types.entry_usize->type_ref),715 LLVMConstNull(g->builtin_types.entry_isize->type_ref),
716 };716 };
717 add_debug_source_node(g, node);717 add_debug_source_node(g, node);
718 return LLVMBuildInBoundsGEP(g->builder, array_val, indices, 2, "");718 return LLVMBuildInBoundsGEP(g->builder, array_val, indices, 2, "");
...@@ -891,7 +891,7 @@ static LLVMValueRef gen_bare_cast(CodeGen *g, AstNode *node, LLVMValueRef expr_v...@@ -891,7 +891,7 @@ static LLVMValueRef gen_bare_cast(CodeGen *g, AstNode *node, LLVMValueRef expr_v
891 LLVMBuildStore(g->builder, expr_bitcast, ptr_ptr);891 LLVMBuildStore(g->builder, expr_bitcast, ptr_ptr);
892892
893 LLVMValueRef len_ptr = LLVMBuildStructGEP(g->builder, cast_node->ptr, 1, "");893 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,
895 actual_type->data.array.len, false);895 actual_type->data.array.len, false);
896 LLVMBuildStore(g->builder, len_val, len_ptr);896 LLVMBuildStore(g->builder, len_val, len_ptr);
897897
...@@ -1656,8 +1656,8 @@ static LLVMValueRef gen_container_init_expr(CodeGen *g, AstNode *node) {...@@ -1656,8 +1656,8 @@ static LLVMValueRef gen_container_init_expr(CodeGen *g, AstNode *node) {
1656 LLVMValueRef elem_val = gen_expr(g, field_node);1656 LLVMValueRef elem_val = gen_expr(g, field_node);
16571657
1658 LLVMValueRef indices[] = {1658 LLVMValueRef indices[] = {
1659 LLVMConstNull(g->builtin_types.entry_usize->type_ref),1659 LLVMConstNull(g->builtin_types.entry_isize->type_ref),
1660 LLVMConstInt(g->builtin_types.entry_usize->type_ref, i, false),1660 LLVMConstInt(g->builtin_types.entry_isize->type_ref, i, false),
1661 };1661 };
1662 add_debug_source_node(g, field_node);1662 add_debug_source_node(g, field_node);
1663 LLVMValueRef elem_ptr = LLVMBuildInBoundsGEP(g->builder, tmp_array_ptr, indices, 2, "");1663 LLVMValueRef elem_ptr = LLVMBuildInBoundsGEP(g->builder, tmp_array_ptr, indices, 2, "");
...@@ -1756,7 +1756,7 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {...@@ -1756,7 +1756,7 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {
1756 VariableTableEntry *index_var = node->data.for_expr.index_var;1756 VariableTableEntry *index_var = node->data.for_expr.index_var;
1757 assert(index_var);1757 assert(index_var);
1758 LLVMValueRef index_ptr = index_var->value_ref;1758 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
1761 BlockContext *old_block_context = g->cur_block_context;1761 BlockContext *old_block_context = g->cur_block_context;
17621762
...@@ -1770,7 +1770,7 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {...@@ -1770,7 +1770,7 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {
1770 LLVMValueRef len_val;1770 LLVMValueRef len_val;
1771 TypeTableEntry *child_type;1771 TypeTableEntry *child_type;
1772 if (array_type->id == TypeTableEntryIdArray) {1772 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,
1774 array_type->data.array.len, false);1774 array_type->data.array.len, false);
1775 child_type = array_type->data.array.child_type;1775 child_type = array_type->data.array.child_type;
1776 } else if (array_type->id == TypeTableEntryIdStruct) {1776 } else if (array_type->id == TypeTableEntryIdStruct) {
...@@ -2007,8 +2007,8 @@ static LLVMValueRef gen_expr_no_cast(CodeGen *g, AstNode *node) {...@@ -2007,8 +2007,8 @@ static LLVMValueRef gen_expr_no_cast(CodeGen *g, AstNode *node) {
2007 Buf *str = &node->data.string_literal.buf;2007 Buf *str = &node->data.string_literal.buf;
2008 LLVMValueRef str_val = find_or_create_string(g, str, node->data.string_literal.c);2008 LLVMValueRef str_val = find_or_create_string(g, str, node->data.string_literal.c);
2009 LLVMValueRef indices[] = {2009 LLVMValueRef indices[] = {
2010 LLVMConstNull(g->builtin_types.entry_usize->type_ref),2010 LLVMConstNull(g->builtin_types.entry_isize->type_ref),
2011 LLVMConstNull(g->builtin_types.entry_usize->type_ref),2011 LLVMConstNull(g->builtin_types.entry_isize->type_ref),
2012 };2012 };
2013 LLVMValueRef ptr_val = LLVMBuildInBoundsGEP(g->builder, str_val, indices, 2, "");2013 LLVMValueRef ptr_val = LLVMBuildInBoundsGEP(g->builder, str_val, indices, 2, "");
2014 return ptr_val;2014 return ptr_val;
...@@ -2506,7 +2506,7 @@ static void define_builtin_fns(CodeGen *g) {...@@ -2506,7 +2506,7 @@ static void define_builtin_fns(CodeGen *g) {
2506 builtin_fn->param_types = allocate<TypeTableEntry *>(builtin_fn->param_count);2506 builtin_fn->param_types = allocate<TypeTableEntry *>(builtin_fn->param_count);
2507 builtin_fn->param_types[0] = nullptr; // manually checked later2507 builtin_fn->param_types[0] = nullptr; // manually checked later
2508 builtin_fn->param_types[1] = nullptr; // manually checked later2508 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
2511 LLVMTypeRef param_types[] = {2511 LLVMTypeRef param_types[] = {
2512 LLVMPointerType(LLVMInt8Type(), 0),2512 LLVMPointerType(LLVMInt8Type(), 0),
...@@ -2529,7 +2529,7 @@ static void define_builtin_fns(CodeGen *g) {...@@ -2529,7 +2529,7 @@ static void define_builtin_fns(CodeGen *g) {
2529 builtin_fn->param_types = allocate<TypeTableEntry *>(builtin_fn->param_count);2529 builtin_fn->param_types = allocate<TypeTableEntry *>(builtin_fn->param_count);
2530 builtin_fn->param_types[0] = nullptr; // manually checked later2530 builtin_fn->param_types[0] = nullptr; // manually checked later
2531 builtin_fn->param_types[1] = g->builtin_types.entry_u8;2531 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
2534 LLVMTypeRef param_types[] = {2534 LLVMTypeRef param_types[] = {
2535 LLVMPointerType(LLVMInt8Type(), 0),2535 LLVMPointerType(LLVMInt8Type(), 0),
std/bootstrap.zig+4-4
...@@ -3,20 +3,20 @@ import "syscall.zig";...@@ -3,20 +3,20 @@ import "syscall.zig";
3// The compiler treats this file special by implicitly importing the function `main`3// The compiler treats this file special by implicitly importing the function `main`
4// from the root source file.4// from the root source file.
55
6var argc: usize;6var argc: isize;
7var argv: &&u8;7var argv: &&u8;
8var env: &&u8;8var env: &&u8;
99
10#attribute("naked")10#attribute("naked")
11export fn _start() unreachable => {11export fn _start() unreachable => {
12 argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> usize));12 argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> isize));
13 argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8));13 argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8));
14 env = asm("lea 0x10(%%rsp,%%rdi,8), %[env]": [env] "=r" (-> &&u8));14 env = asm("lea 0x10(%%rsp,%%rdi,8), %[env]": [env] "=r" (-> &&u8));
15 call_main()15 call_main()
16}16}
1717
18fn strlen(ptr: &const u8) usize => {18fn strlen(ptr: &const u8) isize => {
19 var count: usize = 0;19 var count: isize = 0;
20 while (ptr[count] != 0) {20 while (ptr[count] != 0) {
21 count += 1;21 count += 1;
22 }22 }
std/builtin.zig+2-2
...@@ -1,7 +1,7 @@...@@ -1,7 +1,7 @@
1// These functions are provided when not linking against libc because LLVM1// These functions are provided when not linking against libc because LLVM
2// sometimes generates code that calls them.2// 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 => {
5 var index : @typeof(n) = 0;5 var index : @typeof(n) = 0;
6 while (index != n) {6 while (index != n) {
7 dest[index] = c;7 dest[index] = c;
...@@ -10,7 +10,7 @@ export fn memset(dest: &u8, c: u8, n: usize) &u8 => {...@@ -10,7 +10,7 @@ export fn memset(dest: &u8, c: u8, n: usize) &u8 => {
10 return dest;10 return dest;
11}11}
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 => {
14 var index : @typeof(n) = 0;14 var index : @typeof(n) = 0;
15 while (index != n) {15 while (index != n) {
16 dest[index] = src[index];16 dest[index] = src[index];
std/rand.zig+3-3
...@@ -1,5 +1,5 @@...@@ -1,5 +1,5 @@
1// Mersenne Twister1// Mersenne Twister
2const ARRAY_SIZE : u16 = 624;2const ARRAY_SIZE : i16 = 624;
33
4/// Use `rand_init` to initialize this state.4/// Use `rand_init` to initialize this state.
5pub struct Rand {5pub struct Rand {
...@@ -13,7 +13,7 @@ pub struct Rand {...@@ -13,7 +13,7 @@ pub struct Rand {
13 var i : @typeof(ARRAY_SIZE) = 1;13 var i : @typeof(ARRAY_SIZE) = 1;
14 var prev_value: u64 = seed;14 var prev_value: u64 = seed;
15 while (i < ARRAY_SIZE) {15 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));
17 prev_value = r.array[i];17 prev_value = r.array[i];
18 i += 1;18 i += 1;
19 }19 }
...@@ -81,7 +81,7 @@ pub struct Rand {...@@ -81,7 +81,7 @@ pub struct Rand {
81 }81 }
8282
83 // does not populate the remaining (buf.len % 4) bytes83 // 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 => {
85 var bytes_left = buf.len;85 var bytes_left = buf.len;
86 while (bytes_left >= 4) {86 while (bytes_left >= 4) {
87 *((&u32)(&buf[buf.len - bytes_left])) = r.get_u32();87 *((&u32)(&buf[buf.len - bytes_left])) = r.get_u32();
std/std.zig+7-7
...@@ -5,8 +5,8 @@ pub const stdout_fileno : isize = 1;...@@ -5,8 +5,8 @@ pub const stdout_fileno : isize = 1;
5pub const stderr_fileno : isize = 2;5pub const stderr_fileno : isize = 2;
66
7// TODO error handling7// TODO error handling
8pub fn os_get_random_bytes(buf: &u8, count: usize) isize => {8pub fn os_get_random_bytes(buf: []u8) isize => {
9 getrandom(buf, count, 0)9 getrandom(buf.ptr, buf.len, 0)
10}10}
1111
12// TODO error handling12// TODO error handling
...@@ -38,12 +38,12 @@ pub fn print_i64(x: i64) isize => {...@@ -38,12 +38,12 @@ pub fn print_i64(x: i64) isize => {
38}38}
3939
40// TODO error handling40// TODO error handling
41pub fn readline(buf: []u8, out_len: &usize) bool => {41pub fn readline(buf: []u8, out_len: &isize) bool => {
42 const amt_read = read(stdin_fileno, buf.ptr, buf.len);42 const amt_read = read(stdin_fileno, buf.ptr, buf.len);
43 if (amt_read < 0) {43 if (amt_read < 0) {
44 return true;44 return true;
45 }45 }
46 *out_len = usize(amt_read);46 *out_len = isize(amt_read);
47 return false;47 return false;
48}48}
4949
...@@ -85,9 +85,9 @@ fn char_to_digit(c: u8) u8 => {...@@ -85,9 +85,9 @@ fn char_to_digit(c: u8) u8 => {
85 }85 }
86}86}
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 => {
91 if (x < 0) {91 if (x < 0) {
92 out_buf[0] = '-';92 out_buf[0] = '-';
93 return 1 + buf_print_u64(out_buf[1...], u64(-(x + 1)) + 1);93 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 => {...@@ -96,7 +96,7 @@ fn buf_print_i64(out_buf: []u8, x: i64) usize => {
96 }96 }
97}97}
9898
99fn buf_print_u64(out_buf: []u8, x: u64) usize => {99fn buf_print_u64(out_buf: []u8, x: u64) isize => {
100 var buf: [max_u64_base10_digits]u8;100 var buf: [max_u64_base10_digits]u8;
101 var a = x;101 var a = x;
102 var index = buf.len;102 var index = buf.len;
std/syscall.zig+15-15
...@@ -1,35 +1,35 @@...@@ -1,35 +1,35 @@
1const SYS_read : usize = 0;1const SYS_read : isize = 0;
2const SYS_write : usize = 1;2const SYS_write : isize = 1;
3const SYS_exit : usize = 60;3const SYS_exit : isize = 60;
4const SYS_getrandom : usize = 318;4const SYS_getrandom : isize = 318;
55
6fn syscall1(number: usize, arg1: usize) usize => {6fn syscall1(number: isize, arg1: isize) isize => {
7 asm volatile ("syscall"7 asm volatile ("syscall"
8 : [ret] "={rax}" (-> usize)8 : [ret] "={rax}" (-> isize)
9 : [number] "{rax}" (number), [arg1] "{rdi}" (arg1)9 : [number] "{rax}" (number), [arg1] "{rdi}" (arg1)
10 : "rcx", "r11")10 : "rcx", "r11")
11}11}
1212
13fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize => {13fn syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) isize => {
14 asm volatile ("syscall"14 asm volatile ("syscall"
15 : [ret] "={rax}" (-> usize)15 : [ret] "={rax}" (-> isize)
16 : [number] "{rax}" (number), [arg1] "{rdi}" (arg1), [arg2] "{rsi}" (arg2), [arg3] "{rdx}" (arg3)16 : [number] "{rax}" (number), [arg1] "{rdi}" (arg1), [arg2] "{rsi}" (arg2), [arg3] "{rdx}" (arg3)
17 : "rcx", "r11")17 : "rcx", "r11")
18}18}
1919
20pub fn read(fd: isize, buf: &u8, count: usize) isize => {20pub fn read(fd: isize, buf: &u8, count: isize) isize => {
21 isize(syscall3(SYS_read, usize(fd), usize(buf), count))21 isize(syscall3(SYS_read, isize(fd), isize(buf), count))
22}22}
2323
24pub fn write(fd: isize, buf: &const u8, count: usize) isize => {24pub fn write(fd: isize, buf: &const u8, count: isize) isize => {
25 isize(syscall3(SYS_write, usize(fd), usize(buf), count))25 isize(syscall3(SYS_write, isize(fd), isize(buf), count))
26}26}
2727
28pub fn exit(status: i32) unreachable => {28pub fn exit(status: i32) unreachable => {
29 syscall1(SYS_exit, usize(status));29 syscall1(SYS_exit, isize(status));
30 unreachable{}30 unreachable{}
31}31}
3232
33pub fn getrandom(buf: &u8, count: usize, flags: u32) isize => {33pub fn getrandom(buf: &u8, count: isize, flags: u32) isize => {
34 isize(syscall3(SYS_getrandom, usize(buf), count, usize(flags)))34 isize(syscall3(SYS_getrandom, isize(buf), count, isize(flags)))
35}35}
test/run_tests.cpp+9-9
...@@ -394,16 +394,16 @@ done:...@@ -394,16 +394,16 @@ done:
394import "std.zig";394import "std.zig";
395395
396pub fn main(args: [][]u8) i32 => {396pub 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;
400 while (i < 5) {400 while (i < 5) {
401 array[i] = i + 1;401 array[i] = i + 1;
402 i = array[i];402 i = array[i];
403 }403 }
404404
405 i = 0;405 i = 0;
406 var accumulator = u32(0);406 var accumulator = i32(0);
407 while (i < 5) {407 while (i < 5) {
408 accumulator += array[i];408 accumulator += array[i];
409409
...@@ -420,7 +420,7 @@ pub fn main(args: [][]u8) i32 => {...@@ -420,7 +420,7 @@ pub fn main(args: [][]u8) i32 => {
420420
421 return 0;421 return 0;
422}422}
423fn get_array_len(a: []u32) usize => {423fn get_array_len(a: []i32) isize => {
424 a.len424 a.len
425}425}
426 )SOURCE", "OK\n");426 )SOURCE", "OK\n");
...@@ -856,7 +856,7 @@ pub fn main(args: [][]u8) i32 => {...@@ -856,7 +856,7 @@ pub fn main(args: [][]u8) i32 => {
856 add_simple_case("constant expressions", R"SOURCE(856 add_simple_case("constant expressions", R"SOURCE(
857import "std.zig";857import "std.zig";
858858
859const ARRAY_SIZE : u8 = 20;859const ARRAY_SIZE : i8 = 20;
860860
861pub fn main(args: [][]u8) i32 => {861pub fn main(args: [][]u8) i32 => {
862 var array : [ARRAY_SIZE]u8;862 var array : [ARRAY_SIZE]u8;
...@@ -1147,7 +1147,7 @@ pub fn main(args: [][]u8) i32 => {...@@ -1147,7 +1147,7 @@ pub fn main(args: [][]u8) i32 => {
1147 print_str("\n");1147 print_str("\n");
1148 }1148 }
1149 for (item, array, index) {1149 for (item, array, index) {
1150 print_u64(index);1150 print_i64(index);
1151 print_str("\n");1151 print_str("\n");
1152 }1152 }
1153 const unknown_size: []u8 = array;1153 const unknown_size: []u8 = array;
...@@ -1156,7 +1156,7 @@ pub fn main(args: [][]u8) i32 => {...@@ -1156,7 +1156,7 @@ pub fn main(args: [][]u8) i32 => {
1156 print_str("\n");1156 print_str("\n");
1157 }1157 }
1158 for (item, unknown_size, index) {1158 for (item, unknown_size, index) {
1159 print_u64(index);1159 print_i64(index);
1160 print_str("\n");1160 print_str("\n");
1161 }1161 }
1162 return 0;1162 return 0;
...@@ -1356,9 +1356,9 @@ fn f() => {...@@ -1356,9 +1356,9 @@ fn f() => {
1356 ".tmp_source.zig:4:12: error: use of undeclared identifier 'i'",1356 ".tmp_source.zig:4:12: error: use of undeclared identifier 'i'",
1357 ".tmp_source.zig:4:14: error: use of undeclared identifier 'i'",1357 ".tmp_source.zig:4:14: error: use of undeclared identifier 'i'",
1358 ".tmp_source.zig:5:8: error: array access of non-array",1358 ".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'",
1360 ".tmp_source.zig:5:19: error: array access of non-array",1360 ".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
1363 add_compile_fail_case("variadic functions only allowed in extern", R"SOURCE(1363 add_compile_fail_case("variadic functions only allowed in extern", R"SOURCE(
1364fn f(...) => {}1364fn f(...) => {}