unsigned integers for sizes of things

Closes #62.
This commit is contained in:
Andrew Kelley 2016-07-26 20:40:11 -07:00
parent 76f87cdd96
commit bc81ddfea6
19 changed files with 264 additions and 394 deletions

View File

@ -386,7 +386,7 @@ Function Operation
@shl_with_overflow(inline T: type, a: T, b: T, result: &T) -> bool *x = a << b
```
### @memset(dest, c: u8, byte_count: isize)
### @memset(dest, c: u8, byte_count: usize)
This function sets a region of memory to `c`. `dest` is a pointer.
@ -398,7 +398,7 @@ level code will not use this function, instead using something like this:
for (dest) |*b| *b = c;
```
### @memcpy(dest, source, byte_count: isize)
### @memcpy(dest, source, byte_count: usize)
This function copies bytes from one region of memory to another. `dest` and
`source` are both pointers and must not overlap.

View File

@ -502,7 +502,7 @@ static void slice_type_common_init(CodeGen *g, TypeTableEntry *child_type,
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_isize;
entry->data.structure.fields[1].type_entry = g->builtin_types.entry_usize;
entry->data.structure.fields[1].src_index = 1;
entry->data.structure.fields[1].gen_index = 1;
}
@ -546,7 +546,7 @@ TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_c
if (child_type->zero_bits) {
LLVMTypeRef element_types[] = {
g->builtin_types.entry_isize->type_ref,
g->builtin_types.entry_usize->type_ref,
};
LLVMStructSetBody(entry->type_ref, element_types, 1, false);
@ -556,9 +556,9 @@ TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_c
entry->data.structure.fields[0].gen_index = -1;
entry->data.structure.fields[1].gen_index = 0;
TypeTableEntry *isize_type = g->builtin_types.entry_isize;
uint64_t len_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, isize_type->type_ref);
uint64_t len_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, isize_type->type_ref);
TypeTableEntry *usize_type = g->builtin_types.entry_usize;
uint64_t len_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, usize_type->type_ref);
uint64_t len_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, usize_type->type_ref);
uint64_t len_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, 0);
uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);
@ -570,7 +570,7 @@ TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_c
len_debug_size_in_bits,
len_debug_align_in_bits,
len_offset_in_bits,
0, isize_type->di_type),
0, usize_type->di_type),
};
LLVMZigDIType *replacement_di_type = LLVMZigCreateDebugStructType(g->dbuilder,
compile_unit_scope,
@ -586,7 +586,7 @@ TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_c
unsigned element_count = 2;
LLVMTypeRef element_types[] = {
pointer_type->type_ref,
g->builtin_types.entry_isize->type_ref,
g->builtin_types.entry_usize->type_ref,
};
LLVMStructSetBody(entry->type_ref, element_types, element_count, false);
@ -597,9 +597,9 @@ TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_c
uint64_t ptr_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, pointer_type->type_ref);
uint64_t ptr_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, 0);
TypeTableEntry *isize_type = g->builtin_types.entry_isize;
uint64_t len_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, isize_type->type_ref);
uint64_t len_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, isize_type->type_ref);
TypeTableEntry *usize_type = g->builtin_types.entry_usize;
uint64_t len_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, usize_type->type_ref);
uint64_t len_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, usize_type->type_ref);
uint64_t len_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, 1);
uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);
@ -617,7 +617,7 @@ TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_c
len_debug_size_in_bits,
len_debug_align_in_bits,
len_offset_in_bits,
0, isize_type->di_type),
0, usize_type->di_type),
};
LLVMZigDIType *replacement_di_type = LLVMZigCreateDebugStructType(g->dbuilder,
compile_unit_scope,
@ -2817,10 +2817,10 @@ static TypeTableEntry *analyze_slice_expr(CodeGen *g, ImportTableEntry *import,
context->fn_entry->struct_val_expr_alloca_list.append(&node->data.slice_expr.resolved_struct_val_expr);
}
analyze_expression(g, import, context, g->builtin_types.entry_isize, node->data.slice_expr.start);
analyze_expression(g, import, context, g->builtin_types.entry_usize, node->data.slice_expr.start);
if (node->data.slice_expr.end) {
analyze_expression(g, import, context, g->builtin_types.entry_isize, node->data.slice_expr.end);
analyze_expression(g, import, context, g->builtin_types.entry_usize, node->data.slice_expr.end);
}
return return_type;
@ -2853,7 +2853,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_isize, node->data.array_access_expr.subscript);
analyze_expression(g, import, context, g->builtin_types.entry_usize, node->data.array_access_expr.subscript);
return return_type;
}
@ -3912,7 +3912,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_isize, size_node);
g->builtin_types.entry_usize, size_node);
if (size_type->id == TypeTableEntryIdInvalid) {
return g->builtin_types.entry_invalid;
}
@ -4042,10 +4042,10 @@ static TypeTableEntry *analyze_for_expr(CodeGen *g, ImportTableEntry *import, Bl
Buf *index_var_name = &index_var_node->data.symbol_expr.symbol;
index_var_node->block_context = child_context;
node->data.for_expr.index_var = add_local_var(g, index_var_node, import, child_context, index_var_name,
g->builtin_types.entry_isize, true, nullptr);
g->builtin_types.entry_usize, true, nullptr);
} else {
node->data.for_expr.index_var = add_local_var(g, node, import, child_context, nullptr,
g->builtin_types.entry_isize, true, nullptr);
g->builtin_types.entry_usize, true, nullptr);
}
AstNode *for_body_node = node->data.for_expr.body;

View File

@ -413,7 +413,7 @@ static LLVMValueRef gen_err_name(CodeGen *g, AstNode *node) {
}
LLVMValueRef indices[] = {
LLVMConstNull(g->builtin_types.entry_isize->type_ref),
LLVMConstNull(g->builtin_types.entry_usize->type_ref),
err_val,
};
return LLVMBuildInBoundsGEP(g->builder, g->err_name_table, indices, 2, "");
@ -937,7 +937,7 @@ static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) {
int len_index = wanted_type->data.structure.fields[1].gen_index;
LLVMValueRef len_ptr = LLVMBuildStructGEP(g->builder, cast_expr->tmp_ptr, len_index, "");
LLVMValueRef len_val = LLVMConstInt(g->builtin_types.entry_isize->type_ref,
LLVMValueRef len_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
actual_type->data.array.len, false);
LLVMBuildStore(g->builder, len_val, len_ptr);
@ -978,13 +978,13 @@ static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) {
LLVMValueRef new_len;
if (dest_size == 1) {
LLVMValueRef src_size_val = LLVMConstInt(g->builtin_types.entry_isize->type_ref, src_size, false);
LLVMValueRef src_size_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, src_size, false);
new_len = LLVMBuildMul(g->builder, src_len, src_size_val, "");
} else if (src_size == 1) {
LLVMValueRef dest_size_val = LLVMConstInt(g->builtin_types.entry_isize->type_ref, dest_size, false);
LLVMValueRef dest_size_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, dest_size, false);
if (want_debug_safety(g, node)) {
LLVMValueRef remainder_val = LLVMBuildURem(g->builder, src_len, dest_size_val, "");
LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_isize->type_ref);
LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_usize->type_ref);
LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, remainder_val, zero, "");
LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "SliceWidenOk");
LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "SliceWidenFail");
@ -1159,12 +1159,12 @@ static LLVMValueRef gen_array_elem_ptr(CodeGen *g, AstNode *source_node, LLVMVal
if (array_type->id == TypeTableEntryIdArray) {
if (want_debug_safety(g, source_node)) {
LLVMValueRef end = LLVMConstInt(g->builtin_types.entry_isize->type_ref,
LLVMValueRef end = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
array_type->data.array.len, false);
add_bounds_check(g, source_node, subscript_value, LLVMIntEQ, nullptr, LLVMIntULT, end);
}
LLVMValueRef indices[] = {
LLVMConstNull(g->builtin_types.entry_isize->type_ref),
LLVMConstNull(g->builtin_types.entry_usize->type_ref),
subscript_value
};
set_debug_source_node(g, source_node);
@ -1268,13 +1268,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_isize->type_ref, array_type->data.array.len, false);
end_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, array_type->data.array.len, false);
}
if (want_debug_safety(g, node)) {
add_bounds_check(g, node, start_val, LLVMIntEQ, nullptr, LLVMIntULE, end_val);
if (node->data.slice_expr.end) {
LLVMValueRef array_end = LLVMConstInt(g->builtin_types.entry_isize->type_ref,
LLVMValueRef array_end = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
array_type->data.array.len, false);
add_bounds_check(g, node, end_val, LLVMIntEQ, nullptr, LLVMIntULE, array_end);
}
@ -1283,7 +1283,7 @@ static LLVMValueRef gen_slice_expr(CodeGen *g, AstNode *node) {
set_debug_source_node(g, node);
LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, 0, "");
LLVMValueRef indices[] = {
LLVMConstNull(g->builtin_types.entry_isize->type_ref),
LLVMConstNull(g->builtin_types.entry_usize->type_ref),
start_val,
};
LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");
@ -1408,7 +1408,7 @@ static LLVMValueRef gen_field_access_expr(CodeGen *g, AstNode *node, bool is_lva
if (struct_type->id == TypeTableEntryIdArray) {
Buf *name = &node->data.field_access_expr.field_name;
assert(buf_eql_str(name, "len"));
return LLVMConstInt(g->builtin_types.entry_isize->type_ref,
return LLVMConstInt(g->builtin_types.entry_usize->type_ref,
struct_type->data.array.len, false);
} else if (struct_type->id == TypeTableEntryIdStruct || (struct_type->id == TypeTableEntryIdPointer &&
struct_type->data.pointer.child_type->id == TypeTableEntryIdStruct))
@ -2043,7 +2043,7 @@ static LLVMValueRef gen_struct_memcpy(CodeGen *g, AstNode *source_node, LLVMValu
LLVMValueRef src_ptr = LLVMBuildBitCast(g->builder, src, ptr_u8, "");
LLVMValueRef dest_ptr = LLVMBuildBitCast(g->builder, dest, ptr_u8, "");
TypeTableEntry *isize = g->builtin_types.entry_isize;
TypeTableEntry *usize = g->builtin_types.entry_usize;
uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, type_entry->type_ref);
uint64_t align_bytes = get_memcpy_align(g, type_entry);
assert(size_bytes > 0);
@ -2052,7 +2052,7 @@ static LLVMValueRef gen_struct_memcpy(CodeGen *g, AstNode *source_node, LLVMValu
LLVMValueRef params[] = {
dest_ptr, // dest pointer
src_ptr, // source pointer
LLVMConstInt(isize->type_ref, size_bytes, false),
LLVMConstInt(usize->type_ref, size_bytes, false),
LLVMConstInt(LLVMInt32Type(), align_bytes, false),
LLVMConstNull(LLVMInt1Type()), // is volatile
};
@ -2871,8 +2871,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_isize->type_ref),
LLVMConstInt(g->builtin_types.entry_isize->type_ref, i, false),
LLVMConstNull(g->builtin_types.entry_usize->type_ref),
LLVMConstInt(g->builtin_types.entry_usize->type_ref, i, false),
};
set_debug_source_node(g, field_node);
LLVMValueRef elem_ptr = LLVMBuildInBoundsGEP(g->builder, tmp_array_ptr, indices, 2, "");
@ -2989,7 +2989,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_isize->type_ref, 1, false);
LLVMValueRef one_const = LLVMConstInt(g->builtin_types.entry_usize->type_ref, 1, false);
LLVMBasicBlockRef cond_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForCond");
LLVMBasicBlockRef body_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForBody");
@ -3005,7 +3005,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_isize->type_ref,
len_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
array_type->data.array.len, false);
child_type = array_type->data.array.child_type;
} else if (array_type->id == TypeTableEntryIdStruct) {
@ -3154,7 +3154,7 @@ static LLVMValueRef gen_var_decl_raw(CodeGen *g, AstNode *source_node, AstNodeVa
}
}
if (!ignore_uninit && want_debug_safety(g, source_node)) {
TypeTableEntry *isize = g->builtin_types.entry_isize;
TypeTableEntry *usize = g->builtin_types.entry_usize;
uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, variable->type->type_ref);
uint64_t align_bytes = get_memcpy_align(g, variable->type);
@ -3163,7 +3163,7 @@ static LLVMValueRef gen_var_decl_raw(CodeGen *g, AstNode *source_node, AstNodeVa
LLVMTypeRef ptr_u8 = LLVMPointerType(LLVMInt8Type(), 0);
LLVMValueRef fill_char = LLVMConstInt(LLVMInt8Type(), 0xaa, false);
LLVMValueRef dest_ptr = LLVMBuildBitCast(g->builder, variable->value_ref, ptr_u8, "");
LLVMValueRef byte_count = LLVMConstInt(isize->type_ref, size_bytes, false);
LLVMValueRef byte_count = LLVMConstInt(usize->type_ref, size_bytes, false);
LLVMValueRef align_in_bytes = LLVMConstInt(LLVMInt32Type(), align_bytes, false);
LLVMValueRef params[] = {
dest_ptr,
@ -3771,7 +3771,7 @@ static LLVMValueRef gen_test_fn_val(CodeGen *g, FnTableEntry *fn_entry) {
LLVMSetGlobalConstant(str_global_val, true);
LLVMSetUnnamedAddr(str_global_val, true);
LLVMValueRef len_val = LLVMConstInt(g->builtin_types.entry_isize->type_ref, buf_len(fn_name), false);
LLVMValueRef len_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, buf_len(fn_name), false);
LLVMTypeRef ptr_type = LLVMPointerType(g->builtin_types.entry_u8->type_ref, 0);
LLVMValueRef name_fields[] = {
@ -3813,7 +3813,7 @@ static void generate_error_name_table(CodeGen *g) {
LLVMValueRef fields[] = {
LLVMConstBitCast(str_global, u8_ptr_type->type_ref),
LLVMConstInt(g->builtin_types.entry_isize->type_ref, buf_len(name), false),
LLVMConstInt(g->builtin_types.entry_usize->type_ref, buf_len(name), false),
};
values[i] = LLVMConstNamedStruct(str_type->type_ref, fields, 2);
}
@ -3986,7 +3986,7 @@ static void do_code_gen(CodeGen *g) {
LLVMSetGlobalConstant(test_fn_array_val, true);
LLVMSetUnnamedAddr(test_fn_array_val, true);
LLVMValueRef len_val = LLVMConstInt(g->builtin_types.entry_isize->type_ref, g->test_fn_count, false);
LLVMValueRef len_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, g->test_fn_count, false);
LLVMTypeRef ptr_type = LLVMPointerType(LLVMTypeOf(test_fn_vals[0]), 0);
LLVMValueRef fields[] = {
LLVMConstBitCast(test_fn_array_val, ptr_type),
@ -4611,7 +4611,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_isize;
builtin_fn->param_types[2] = g->builtin_types.entry_usize;
builtin_fn->ref_count = 1;
LLVMTypeRef param_types[] = {
@ -4635,7 +4635,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_isize;
builtin_fn->param_types[2] = g->builtin_types.entry_usize;
builtin_fn->ref_count = 1;
LLVMTypeRef param_types[] = {

View File

@ -10,7 +10,7 @@ const want_start_symbol = switch(@compile_var("os")) {
};
const want_main_symbol = !want_start_symbol;
var argc: isize = undefined;
var argc: usize = undefined;
var argv: &&u8 = undefined;
#attribute("naked")
@ -18,11 +18,11 @@ var argv: &&u8 = undefined;
export fn _start() -> unreachable {
switch (@compile_var("arch")) {
x86_64 => {
argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> isize));
argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> usize));
argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8));
},
i386 => {
argc = asm("mov (%%esp), %[argc]": [argc] "=r" (-> isize));
argc = asm("mov (%%esp), %[argc]": [argc] "=r" (-> usize));
argv = asm("lea 0x4(%%esp), %[argv]": [argv] "=r" (-> &&u8));
},
else => @compile_err("unsupported arch"),
@ -46,7 +46,7 @@ fn call_main_and_exit() -> unreachable {
#condition(want_main_symbol)
export fn main(c_argc: i32, c_argv: &&u8) -> i32 {
argc = c_argc;
argc = usize(c_argc);
argv = c_argv;
call_main() %% return 1;
return 0;

View File

@ -2,8 +2,8 @@
// sometimes generates code that calls them.
#debug_safety(false)
export fn memset(dest: &u8, c: u8, n: isize) -> &u8 {
var index: isize = 0;
export fn memset(dest: &u8, c: u8, n: usize) -> &u8 {
var index: usize = 0;
while (index != n) {
dest[index] = c;
index += 1;
@ -12,8 +12,8 @@ export fn memset(dest: &u8, c: u8, n: isize) -> &u8 {
}
#debug_safety(false)
export fn memcpy(noalias dest: &u8, noalias src: &const u8, n: isize) -> &u8 {
var index: isize = 0;
export fn memcpy(noalias dest: &u8, noalias src: &const u8, n: usize) -> &u8 {
var index: usize = 0;
while (index != n) {
dest[index] = src[index];
index += 1;

View File

@ -1,8 +1,8 @@
// TODO fix https://github.com/andrewrk/zig/issues/140
// and then make this able to run at compile time
#static_eval_enable(false)
pub fn len(ptr: &const u8) -> isize {
var count: isize = 0;
pub fn len(ptr: &const u8) -> usize {
var count: usize = 0;
while (ptr[count] != 0; count += 1) {}
return count;
}
@ -11,7 +11,7 @@ pub fn len(ptr: &const u8) -> isize {
// and then make this able to run at compile time
#static_eval_enable(false)
pub fn cmp(a: &const u8, b: &const u8) -> i32 {
var index: isize = 0;
var index: usize = 0;
while (a[index] == b[index] && a[index] != 0; index += 1) {}
return a[index] - b[index];
}

View File

@ -12,10 +12,10 @@ pub inline fn HashMap(inline K: type, inline V: type, inline hash: fn(key: K)->u
}
*/
pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b: K)->bool, STATIC_SIZE: isize) {
pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b: K)->bool, STATIC_SIZE: usize) {
entries: []Entry,
size: isize,
max_distance_from_start_index: isize,
size: usize,
max_distance_from_start_index: usize,
allocator: &Allocator,
// if the hash map is small enough, we use linear search through these
// entries instead of allocating memory
@ -27,7 +27,7 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
pub struct Entry {
used: bool,
distance_from_start_index: isize,
distance_from_start_index: usize,
key: K,
value: V,
}
@ -35,9 +35,9 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
pub struct Iterator {
hm: &Self,
// how many items have we returned
count: isize,
count: usize,
// iterator through the entry array
index: isize,
index: usize,
// used to detect concurrent modification
initial_modification_count: debug_u32,
@ -117,7 +117,7 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
pub fn remove(hm: &Self, key: K) {
hm.increment_modification_count();
const start_index = hm.key_to_index(key);
{var roll_over: isize = 0; while (roll_over <= hm.max_distance_from_start_index; roll_over += 1) {
{var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index; roll_over += 1) {
const index = (start_index + roll_over) % hm.entries.len;
var entry = &hm.entries[index];
@ -151,7 +151,7 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
};
}
fn init_capacity(hm: &Self, capacity: isize) -> %void {
fn init_capacity(hm: &Self, capacity: usize) -> %void {
hm.entries = %return hm.allocator.alloc(Entry, capacity);
hm.size = 0;
hm.max_distance_from_start_index = 0;
@ -170,8 +170,8 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
var key = orig_key;
var value = orig_value;
const start_index = hm.key_to_index(key);
var roll_over: isize = 0;
var distance_from_start_index: isize = 0;
var roll_over: usize = 0;
var distance_from_start_index: usize = 0;
while (roll_over < hm.entries.len; {roll_over += 1; distance_from_start_index += 1}) {
const index = (start_index + roll_over) % hm.entries.len;
const entry = &hm.entries[index];
@ -180,7 +180,7 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
if (entry.distance_from_start_index < distance_from_start_index) {
// robin hood to the rescue
const tmp = *entry;
hm.max_distance_from_start_index = math.max(isize,
hm.max_distance_from_start_index = math.max(usize,
hm.max_distance_from_start_index, distance_from_start_index);
*entry = Entry {
.used = true,
@ -201,7 +201,7 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
hm.size += 1;
}
hm.max_distance_from_start_index = math.max(isize, distance_from_start_index,
hm.max_distance_from_start_index = math.max(usize, distance_from_start_index,
hm.max_distance_from_start_index);
*entry = Entry {
.used = true,
@ -216,7 +216,7 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
fn internal_get(hm: &Self, key: K) -> ?&Entry {
const start_index = hm.key_to_index(key);
{var roll_over: isize = 0; while (roll_over <= hm.max_distance_from_start_index; roll_over += 1) {
{var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index; roll_over += 1) {
const index = (start_index + roll_over) % hm.entries.len;
const entry = &hm.entries[index];
@ -226,8 +226,8 @@ pub struct SmallHashMap(K: type, V: type, hash: fn(key: K)->u32, eql: fn(a: K, b
return null;
}
fn key_to_index(hm: &Self, key: K) -> isize {
return isize(hash(key)) % hm.entries.len;
fn key_to_index(hm: &Self, key: K) -> usize {
return usize(hash(key)) % hm.entries.len;
}
}
@ -239,15 +239,15 @@ var global_allocator = Allocator {
};
var some_mem: [200]u8 = undefined;
var some_mem_index: isize = 0;
var some_mem_index: usize = 0;
fn global_alloc(self: &Allocator, n: isize) -> %[]u8 {
fn global_alloc(self: &Allocator, n: usize) -> %[]u8 {
const result = some_mem[some_mem_index ... some_mem_index + n];
some_mem_index += n;
return result;
}
fn global_realloc(self: &Allocator, old_mem: []u8, new_size: isize) -> %[]u8 {
fn global_realloc(self: &Allocator, old_mem: []u8, new_size: usize) -> %[]u8 {
const result = %return global_alloc(self, new_size);
@memcpy(result.ptr, old_mem.ptr, old_mem.len);
return result;

View File

@ -59,9 +59,9 @@ pub const OpenCreate = 0b0100;
pub const OpenTruncate = 0b1000;
pub struct OutStream {
fd: isize,
fd: i32,
buffer: [buffer_size]u8,
index: isize,
index: usize,
pub fn write_byte(os: &OutStream, b: u8) -> %void {
if (os.buffer.len == os.index) %return os.flush();
@ -69,13 +69,13 @@ pub struct OutStream {
os.index += 1;
}
pub fn write(os: &OutStream, bytes: []const u8) -> %isize {
pub fn write(os: &OutStream, bytes: []const u8) -> %usize {
var src_bytes_left = bytes.len;
var src_index: @typeof(bytes.len) = 0;
const dest_space_left = os.buffer.len - os.index;
while (src_bytes_left > 0) {
const copy_amt = math.min(isize, dest_space_left, src_bytes_left);
const copy_amt = math.min(usize, dest_space_left, src_bytes_left);
@memcpy(&os.buffer[os.index], &bytes[src_index], copy_amt);
os.index += copy_amt;
if (os.index == os.buffer.len) {
@ -88,13 +88,13 @@ pub struct OutStream {
/// Prints a byte buffer, flushes the buffer, then returns the number of
/// bytes printed. The "f" is for "flush".
pub fn printf(os: &OutStream, str: []const u8) -> %isize {
pub fn printf(os: &OutStream, str: []const u8) -> %usize {
const byte_count = %return os.write(str);
%return os.flush();
return byte_count;
}
pub fn print_u64(os: &OutStream, x: u64) -> %isize {
pub fn print_u64(os: &OutStream, x: u64) -> %usize {
if (os.index + max_u64_base10_digits >= os.buffer.len) {
%return os.flush();
}
@ -104,7 +104,7 @@ pub struct OutStream {
return amt_printed;
}
pub fn print_i64(os: &OutStream, x: i64) -> %isize {
pub fn print_i64(os: &OutStream, x: i64) -> %usize {
if (os.index + max_u64_base10_digits >= os.buffer.len) {
%return os.flush();
}
@ -114,16 +114,6 @@ pub struct OutStream {
return amt_printed;
}
pub fn print_f64(os: &OutStream, x: f64) -> %isize {
if (os.index + max_f64_digits >= os.buffer.len) {
%return os.flush();
}
const amt_printed = buf_print_f64(os.buffer[os.index...], x, 4);
os.index += amt_printed;
return amt_printed;
}
pub fn flush(os: &OutStream) -> %void {
const write_ret = linux.write(os.fd, &os.buffer[0], os.index);
const write_err = linux.get_errno(write_ret);
@ -144,25 +134,27 @@ pub struct OutStream {
}
pub fn close(os: &OutStream) -> %void {
const closed = linux.close(os.fd);
if (closed < 0) {
return switch (-closed) {
errno.EIO => error.Io,
const close_ret = linux.close(os.fd);
const close_err = linux.get_errno(close_ret);
if (close_err > 0) {
return switch (close_err) {
errno.EIO => error.Io,
errno.EBADF => error.BadFd,
errno.EINTR => error.SigInterrupt,
else => error.Unexpected,
else => error.Unexpected,
}
}
}
}
pub struct InStream {
fd: isize,
fd: i32,
pub fn open(path: []u8) -> %InStream {
const fd = linux.open(path, linux.O_LARGEFILE|linux.O_RDONLY, 0);
if (fd < 0) {
return switch (-fd) {
const fd_err = linux.get_errno(fd);
if (fd_err > 0) {
return switch (fd_err) {
errno.EFAULT => unreachable{},
errno.EINVAL => unreachable{},
errno.EACCES => error.BadPerm,
@ -183,13 +175,14 @@ pub struct InStream {
}
}
return InStream { .fd = fd, };
return InStream { .fd = i32(fd), };
}
pub fn read(is: &InStream, buf: []u8) -> %isize {
pub fn read(is: &InStream, buf: []u8) -> %usize {
const amt_read = linux.read(is.fd, &buf[0], buf.len);
if (amt_read < 0) {
return switch (-amt_read) {
const read_err = linux.get_errno(amt_read);
if (read_err > 0) {
return switch (read_err) {
errno.EINVAL => unreachable{},
errno.EFAULT => unreachable{},
errno.EBADF => error.BadFd,
@ -202,9 +195,10 @@ pub struct InStream {
}
pub fn close(is: &InStream) -> %void {
const closed = linux.close(is.fd);
if (closed < 0) {
return switch (-closed) {
const close_ret = linux.close(is.fd);
const close_err = linux.get_errno(close_ret);
if (close_err > 0) {
return switch (close_err) {
errno.EIO => error.Io,
errno.EBADF => error.BadFd,
errno.EINTR => error.SigInterrupt,
@ -240,7 +234,7 @@ fn char_to_digit(c: u8, radix: u8) -> %u8 {
return if (value >= radix) error.InvalidChar else value;
}
pub fn buf_print_signed(inline T: type, out_buf: []u8, x: T) -> isize {
pub fn buf_print_signed(inline T: type, out_buf: []u8, x: T) -> usize {
const uint = @int_type(false, T.bit_count, false);
if (x < 0) {
out_buf[0] = '-';
@ -250,14 +244,14 @@ pub fn buf_print_signed(inline T: type, out_buf: []u8, x: T) -> isize {
}
}
pub fn buf_print_i64(out_buf: []u8, x: i64) -> isize {
pub fn buf_print_i64(out_buf: []u8, x: i64) -> usize {
buf_print_signed(i64, out_buf, x)
}
pub fn buf_print_unsigned(inline T: type, out_buf: []u8, x: T) -> isize {
pub fn buf_print_unsigned(inline T: type, out_buf: []u8, x: T) -> usize {
var buf: [max_u64_base10_digits]u8 = undefined;
var a = x;
var index: isize = buf.len;
var index: usize = buf.len;
while (true) {
const digit = a % 10;
@ -275,134 +269,10 @@ pub fn buf_print_unsigned(inline T: type, out_buf: []u8, x: T) -> isize {
return len;
}
pub fn buf_print_u64(out_buf: []u8, x: u64) -> isize {
pub fn buf_print_u64(out_buf: []u8, x: u64) -> usize {
buf_print_unsigned(u64, out_buf, x)
}
pub fn buf_print_f64(out_buf: []u8, x: f64, decimals: isize) -> isize {
const numExpBits = 11;
const numRawSigBits = 52; // not including implicit 1 bit
const expBias = 1023;
var decs = decimals;
if (decs >= max_u64_base10_digits) {
decs = max_u64_base10_digits - 1;
}
if (x == math.f64_get_pos_inf()) {
const buf2 = "+Inf";
@memcpy(&out_buf[0], &buf2[0], buf2.len);
return 4;
} else if (x == math.f64_get_neg_inf()) {
const buf2 = "-Inf";
@memcpy(&out_buf[0], &buf2[0], buf2.len);
return 4;
} else if (math.f64_is_nan(x)) {
const buf2 = "NaN";
@memcpy(&out_buf[0], &buf2[0], buf2.len);
return 3;
}
var buf: [max_f64_digits]u8 = undefined;
var len: isize = 0;
// 1 sign bit
// 11 exponent bits
// 52 significand bits (+ 1 implicit always non-zero bit)
const bits = math.f64_to_bits(x);
if (bits & (1 << 63) != 0) {
buf[0] = '-';
len += 1;
}
const rexponent: i64 = i64((bits >> numRawSigBits) & ((1 << numExpBits) - 1));
const exponent = rexponent - expBias - numRawSigBits;
if (rexponent == 0) {
buf[len] = '0';
len += 1;
@memcpy(&out_buf[0], &buf[0], len);
return len;
}
const sig = (bits & ((1 << numRawSigBits) - 1)) | (1 << numRawSigBits);
if (exponent >= 0) {
// number is an integer
if (exponent >= 64 - 53) {
// use XeX form
// TODO support printing large floats
//len += buf_print_u64(buf[len...], sig << 10);
const str = "LARGEF64";
@memcpy(&buf[len], &str[0], str.len);
len += str.len;
} else {
// use typical form
len += buf_print_u64(buf[len...], sig << u64(exponent));
buf[len] = '.';
len += 1;
var i: isize = 0;
while (i < decs) {
buf[len] = '0';
len += 1;
i += 1;
}
}
} else {
// number is not an integer
// print out whole part
len += buf_print_u64(buf[len...], sig >> u64(-exponent));
buf[len] = '.';
len += 1;
// print out fractional part
// dec_num holds: fractional part * 10 ^ decs
var dec_num: u64 = 0;
var a: isize = 1;
var i: isize = 0;
while (i < decs + 5) {
a *= 10;
i += 1;
}
// create a mask: 1's for the fractional part, 0's for whole part
var masked_sig = sig & ((1 << u64(-exponent)) - 1);
i = -1;
while (i >= exponent) {
var bit_set = ((1 << u64(i-exponent)) & masked_sig) != 0;
if (bit_set) {
dec_num += usize(a) >> usize(-i);
}
i -= 1;
}
dec_num /= 100000;
len += decs;
i = len - 1;
while (i >= len - decs) {
buf[i] = '0' + u8(dec_num % 10);
dec_num /= 10;
i -= 1;
}
}
@memcpy(&out_buf[0], &buf[0], len);
len
}
#attribute("test")
fn parse_u64_digit_too_big() {
parse_unsigned(u64, "123a", 10) %% |err| {

View File

@ -221,66 +221,67 @@ pub const AF_VSOCK = PF_VSOCK;
pub const AF_MAX = PF_MAX;
/// Get the errno from a syscall return value, or 0 for no error.
pub fn get_errno(r: isize) -> isize {
if (r > -4096 && r < 0) -r else 0
pub fn get_errno(r: usize) -> usize {
const signed_r = *(&isize)(&r);
if (signed_r > -4096 && signed_r < 0) usize(-signed_r) else 0
}
pub fn mmap(address: ?&u8, length: isize, prot: isize, flags: isize, fd: isize, offset: isize) -> isize {
arch.syscall6(arch.SYS_mmap, isize(address), length, prot, flags, fd, offset)
pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: usize, fd: i32, offset: usize) -> usize {
arch.syscall6(arch.SYS_mmap, usize(address), length, prot, flags, usize(fd), offset)
}
pub fn munmap(address: &u8, length: isize) -> isize {
arch.syscall2(arch.SYS_munmap, isize(address), length)
pub fn munmap(address: &u8, length: usize) -> usize {
arch.syscall2(arch.SYS_munmap, usize(address), length)
}
pub fn read(fd: isize, buf: &u8, count: isize) -> isize {
arch.syscall3(arch.SYS_read, isize(fd), isize(buf), count)
pub fn read(fd: i32, buf: &u8, count: usize) -> usize {
arch.syscall3(arch.SYS_read, usize(fd), usize(buf), count)
}
pub fn write(fd: isize, buf: &const u8, count: isize) -> isize {
arch.syscall3(arch.SYS_write, isize(fd), isize(buf), count)
pub fn write(fd: i32, buf: &const u8, count: usize) -> usize {
arch.syscall3(arch.SYS_write, usize(fd), usize(buf), count)
}
pub fn open(path: []u8, flags: isize, perm: isize) -> isize {
pub fn open(path: []u8, flags: usize, perm: usize) -> usize {
var buf: [path.len + 1]u8 = undefined;
@memcpy(&buf[0], &path[0], path.len);
buf[path.len] = 0;
arch.syscall3(arch.SYS_open, isize(&buf[0]), flags, perm)
arch.syscall3(arch.SYS_open, usize(&buf[0]), flags, perm)
}
pub fn create(path: []u8, perm: isize) -> isize {
pub fn create(path: []u8, perm: usize) -> usize {
var buf: [path.len + 1]u8 = undefined;
@memcpy(&buf[0], &path[0], path.len);
buf[path.len] = 0;
arch.syscall2(arch.SYS_creat, isize(&buf[0]), perm)
arch.syscall2(arch.SYS_creat, usize(&buf[0]), perm)
}
pub fn openat(dirfd: isize, path: []u8, flags: isize, mode: isize) -> isize {
pub fn openat(dirfd: i32, path: []u8, flags: usize, mode: usize) -> usize {
var buf: [path.len + 1]u8 = undefined;
@memcpy(&buf[0], &path[0], path.len);
buf[path.len] = 0;
arch.syscall4(arch.SYS_openat, dirfd, isize(&buf[0]), flags, mode)
arch.syscall4(arch.SYS_openat, usize(dirfd), usize(&buf[0]), flags, mode)
}
pub fn close(fd: isize) -> isize {
arch.syscall1(arch.SYS_close, fd)
pub fn close(fd: i32) -> usize {
arch.syscall1(arch.SYS_close, usize(fd))
}
pub fn lseek(fd: isize, offset: isize, ref_pos: isize) -> isize {
arch.syscall3(arch.SYS_lseek, fd, offset, ref_pos)
pub fn lseek(fd: i32, offset: usize, ref_pos: usize) -> usize {
arch.syscall3(arch.SYS_lseek, usize(fd), offset, ref_pos)
}
pub fn exit(status: i32) -> unreachable {
arch.syscall1(arch.SYS_exit, isize(status));
arch.syscall1(arch.SYS_exit, usize(status));
unreachable{}
}
pub fn getrandom(buf: &u8, count: isize, flags: u32) -> isize {
arch.syscall3(arch.SYS_getrandom, isize(buf), count, isize(flags))
pub fn getrandom(buf: &u8, count: usize, flags: u32) -> usize {
arch.syscall3(arch.SYS_getrandom, usize(buf), count, usize(flags))
}
pub fn kill(pid: i32, sig: i32) -> i32 {
i32(arch.syscall2(arch.SYS_kill, pid, sig))
i32(arch.syscall2(arch.SYS_kill, usize(pid), usize(sig)))
}
const NSIG = 65;
@ -292,21 +293,21 @@ pub fn raise(sig: i32) -> i32 {
var set: sigset_t = undefined;
block_app_signals(&set);
const tid = i32(arch.syscall0(arch.SYS_gettid));
const ret = i32(arch.syscall2(arch.SYS_tkill, tid, sig));
const ret = i32(arch.syscall2(arch.SYS_tkill, usize(tid), usize(sig)));
restore_signals(&set);
return ret;
}
fn block_all_signals(set: &sigset_t) {
arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, isize(&all_mask), isize(set), NSIG/8);
arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, usize(&all_mask), usize(set), NSIG/8);
}
fn block_app_signals(set: &sigset_t) {
arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, isize(&app_mask), isize(set), NSIG/8);
arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, usize(&app_mask), usize(set), NSIG/8);
}
fn restore_signals(set: &sigset_t) {
arch.syscall4(arch.SYS_rt_sigprocmask, SIG_SETMASK, isize(set), 0, NSIG/8);
arch.syscall4(arch.SYS_rt_sigprocmask, SIG_SETMASK, usize(set), 0, NSIG/8);
}
@ -363,98 +364,96 @@ export struct ifreq {
}
*/
pub fn getsockname(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> isize {
arch.syscall3(arch.SYS_getsockname, fd, isize(addr), isize(len))
pub fn getsockname(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> usize {
arch.syscall3(arch.SYS_getsockname, usize(fd), usize(addr), usize(len))
}
pub fn getpeername(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> isize {
arch.syscall3(arch.SYS_getpeername, fd, isize(addr), isize(len))
pub fn getpeername(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> usize {
arch.syscall3(arch.SYS_getpeername, usize(fd), usize(addr), usize(len))
}
pub fn socket(domain: i32, socket_type: i32, protocol: i32) -> isize {
arch.syscall3(arch.SYS_socket, domain, socket_type, protocol)
pub fn socket(domain: i32, socket_type: i32, protocol: i32) -> usize {
arch.syscall3(arch.SYS_socket, usize(domain), usize(socket_type), usize(protocol))
}
pub fn setsockopt(fd: i32, level: i32, optname: i32, optval: &const u8, optlen: socklen_t) -> isize {
arch.syscall5(arch.SYS_setsockopt, fd, level, optname, isize(optval), isize(optlen))
pub fn setsockopt(fd: i32, level: i32, optname: i32, optval: &const u8, optlen: socklen_t) -> usize {
arch.syscall5(arch.SYS_setsockopt, usize(fd), usize(level), usize(optname), usize(optval), usize(optlen))
}
pub fn getsockopt(fd: i32, level: i32, optname: i32, noalias optval: &u8, noalias optlen: &socklen_t) -> isize {
arch.syscall5(arch.SYS_getsockopt, fd, level, optname, isize(optval), isize(optlen))
pub fn getsockopt(fd: i32, level: i32, optname: i32, noalias optval: &u8, noalias optlen: &socklen_t) -> usize {
arch.syscall5(arch.SYS_getsockopt, usize(fd), usize(level), usize(optname), usize(optval), usize(optlen))
}
pub fn sendmsg(fd: i32, msg: &const arch.msghdr, flags: i32) -> isize {
arch.syscall3(arch.SYS_sendmsg, fd, isize(msg), flags)
pub fn sendmsg(fd: i32, msg: &const arch.msghdr, flags: u32) -> usize {
arch.syscall3(arch.SYS_sendmsg, usize(fd), usize(msg), flags)
}
pub fn connect(fd: i32, addr: &const sockaddr, len: socklen_t) -> isize {
arch.syscall3(arch.SYS_connect, fd, isize(addr), isize(len))
pub fn connect(fd: i32, addr: &const sockaddr, len: socklen_t) -> usize {
arch.syscall3(arch.SYS_connect, usize(fd), usize(addr), usize(len))
}
pub fn recvmsg(fd: i32, msg: &arch.msghdr, flags: i32) -> isize {
arch.syscall3(arch.SYS_recvmsg, fd, isize(msg), flags)
pub fn recvmsg(fd: i32, msg: &arch.msghdr, flags: u32) -> usize {
arch.syscall3(arch.SYS_recvmsg, usize(fd), usize(msg), flags)
}
pub fn recvfrom(fd: i32, noalias buf: &u8, len: isize, flags: i32,
noalias addr: ?&sockaddr, noalias alen: ?&socklen_t) -> isize
pub fn recvfrom(fd: i32, noalias buf: &u8, len: usize, flags: u32,
noalias addr: ?&sockaddr, noalias alen: ?&socklen_t) -> usize
{
arch.syscall6(arch.SYS_recvfrom, fd, isize(buf), len, flags, isize(addr), isize(alen))
arch.syscall6(arch.SYS_recvfrom, usize(fd), usize(buf), len, flags, usize(addr), usize(alen))
}
pub fn shutdown(fd: i32, how: i32) -> isize {
arch.syscall2(arch.SYS_shutdown, fd, how)
pub fn shutdown(fd: i32, how: i32) -> usize {
arch.syscall2(arch.SYS_shutdown, usize(fd), usize(how))
}
pub fn bind(fd: i32, addr: &const sockaddr, len: socklen_t) {
arch.syscall3(arch.SYS_bind, fd, isize(addr), isize(len));
arch.syscall3(arch.SYS_bind, usize(fd), usize(addr), usize(len));
}
pub fn listen(fd: i32, backlog: i32) -> isize {
arch.syscall2(arch.SYS_listen, fd, backlog)
pub fn listen(fd: i32, backlog: i32) -> usize {
arch.syscall2(arch.SYS_listen, usize(fd), usize(backlog))
}
pub fn sendto(fd: i32, buf: &const u8, len: isize, flags: i32, addr: ?&const sockaddr, alen: socklen_t) -> isize {
arch.syscall6(arch.SYS_sendto, fd, isize(buf), len, flags, isize(addr), isize(alen))
pub fn sendto(fd: i32, buf: &const u8, len: usize, flags: u32, addr: ?&const sockaddr, alen: socklen_t) -> usize {
arch.syscall6(arch.SYS_sendto, usize(fd), usize(buf), len, flags, usize(addr), usize(alen))
}
pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) -> isize {
arch.syscall4(arch.SYS_socketpair, domain, socket_type, protocol, isize(&fd[0]))
pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) -> usize {
arch.syscall4(arch.SYS_socketpair, usize(domain), usize(socket_type), usize(protocol), usize(&fd[0]))
}
pub fn accept(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> isize {
pub fn accept(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> usize {
accept4(fd, addr, len, 0)
}
pub fn accept4(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t, flags: i32) -> isize {
arch.syscall4(arch.SYS_accept4, fd, isize(addr), isize(len), flags)
pub fn accept4(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t, flags: u32) -> usize {
arch.syscall4(arch.SYS_accept4, usize(fd), usize(addr), usize(len), flags)
}
/*
pub error NameTooLong;
pub error SystemResources;
pub error Io;
pub fn if_nametoindex(name: []u8) -> %u32 {
var ifr: ifreq = undefined;
if (name.len >= ifr.ifr_name.len) {
return error.NameTooLong;
}
const socket_ret = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
const socket_err = get_errno(socket_ret);
if (socket_err > 0) {
return error.SystemResources;
}
const socket_fd = i32(socket_ret);
@memcpy(&ifr.ifr_name[0], &name[0], name.len);
ifr.ifr_name[name.len] = 0;
const ioctl_ret = ioctl(socket_fd, SIOCGIFINDEX, &ifr);
close(socket_fd);
const ioctl_err = get_errno(ioctl_ret);
if (ioctl_err > 0) {
return error.Io;
}
return ifr.ifr_ifindex;
}
*/
// pub error NameTooLong;
// pub error SystemResources;
// pub error Io;
//
// pub fn if_nametoindex(name: []u8) -> %u32 {
// var ifr: ifreq = undefined;
//
// if (name.len >= ifr.ifr_name.len) {
// return error.NameTooLong;
// }
//
// const socket_ret = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
// const socket_err = get_errno(socket_ret);
// if (socket_err > 0) {
// return error.SystemResources;
// }
// const socket_fd = i32(socket_ret);
// @memcpy(&ifr.ifr_name[0], &name[0], name.len);
// ifr.ifr_name[name.len] = 0;
// const ioctl_ret = ioctl(socket_fd, SIOCGIFINDEX, &ifr);
// close(socket_fd);
// const ioctl_err = get_errno(ioctl_ret);
// if (ioctl_err > 0) {
// return error.Io;
// }
// return ifr.ifr_ifindex;
// }

View File

@ -419,39 +419,39 @@ pub const F_GETOWN_EX = 16;
pub const F_GETOWNER_UIDS = 17;
pub inline fn syscall0(number: isize) -> isize {
pub inline fn syscall0(number: usize) -> usize {
asm volatile ("int $0x80"
: [ret] "={eax}" (-> isize)
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number))
}
pub inline fn syscall1(number: isize, arg1: isize) -> isize {
pub inline fn syscall1(number: usize, arg1: usize) -> usize {
asm volatile ("int $0x80"
: [ret] "={eax}" (-> isize)
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number),
[arg1] "{ebx}" (arg1))
}
pub inline fn syscall2(number: isize, arg1: isize, arg2: isize) -> isize {
pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) -> usize {
asm volatile ("int $0x80"
: [ret] "={eax}" (-> isize)
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number),
[arg1] "{ebx}" (arg1),
[arg2] "{ecx}" (arg2))
}
pub inline fn syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) -> isize {
pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) -> usize {
asm volatile ("int $0x80"
: [ret] "={eax}" (-> isize)
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number),
[arg1] "{ebx}" (arg1),
[arg2] "{ecx}" (arg2),
[arg3] "{edx}" (arg3))
}
pub inline fn syscall4(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize) -> isize {
pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) -> usize {
asm volatile ("int $0x80"
: [ret] "={eax}" (-> isize)
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number),
[arg1] "{ebx}" (arg1),
[arg2] "{ecx}" (arg2),
@ -459,11 +459,11 @@ pub inline fn syscall4(number: isize, arg1: isize, arg2: isize, arg3: isize, arg
[arg4] "{esi}" (arg4))
}
pub inline fn syscall5(number: isize, arg1: isize, arg2: isize, arg3: isize,
arg4: isize, arg5: isize) -> isize
pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize,
arg4: usize, arg5: usize) -> usize
{
asm volatile ("int $0x80"
: [ret] "={eax}" (-> isize)
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number),
[arg1] "{ebx}" (arg1),
[arg2] "{ecx}" (arg2),
@ -472,11 +472,11 @@ pub inline fn syscall5(number: isize, arg1: isize, arg2: isize, arg3: isize,
[arg5] "{edi}" (arg5))
}
pub inline fn syscall6(number: isize, arg1: isize, arg2: isize, arg3: isize,
arg4: isize, arg5: isize, arg6: isize) -> isize
pub inline fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize,
arg4: usize, arg5: usize, arg6: usize) -> usize
{
asm volatile ("int $0x80"
: [ret] "={eax}" (-> isize)
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number),
[arg1] "{ebx}" (arg1),
[arg2] "{ecx}" (arg2),

View File

@ -370,33 +370,33 @@ pub const F_GETOWN_EX = 16;
pub const F_GETOWNER_UIDS = 17;
pub inline fn syscall0(number: isize) -> isize {
pub inline fn syscall0(number: usize) -> usize {
asm volatile ("syscall"
: [ret] "={rax}" (-> isize)
: [ret] "={rax}" (-> usize)
: [number] "{rax}" (number)
: "rcx", "r11")
}
pub inline fn syscall1(number: isize, arg1: isize) -> isize {
pub inline fn syscall1(number: usize, arg1: usize) -> usize {
asm volatile ("syscall"
: [ret] "={rax}" (-> isize)
: [ret] "={rax}" (-> usize)
: [number] "{rax}" (number),
[arg1] "{rdi}" (arg1)
: "rcx", "r11")
}
pub inline fn syscall2(number: isize, arg1: isize, arg2: isize) -> isize {
pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) -> usize {
asm volatile ("syscall"
: [ret] "={rax}" (-> isize)
: [ret] "={rax}" (-> usize)
: [number] "{rax}" (number),
[arg1] "{rdi}" (arg1),
[arg2] "{rsi}" (arg2)
: "rcx", "r11")
}
pub inline fn syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) -> isize {
pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) -> usize {
asm volatile ("syscall"
: [ret] "={rax}" (-> isize)
: [ret] "={rax}" (-> usize)
: [number] "{rax}" (number),
[arg1] "{rdi}" (arg1),
[arg2] "{rsi}" (arg2),
@ -404,9 +404,9 @@ pub inline fn syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) ->
: "rcx", "r11")
}
pub inline fn syscall4(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize) -> isize {
pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) -> usize {
asm volatile ("syscall"
: [ret] "={rax}" (-> isize)
: [ret] "={rax}" (-> usize)
: [number] "{rax}" (number),
[arg1] "{rdi}" (arg1),
[arg2] "{rsi}" (arg2),
@ -415,9 +415,9 @@ pub inline fn syscall4(number: isize, arg1: isize, arg2: isize, arg3: isize, arg
: "rcx", "r11")
}
pub inline fn syscall5(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize, arg5: isize) -> isize {
pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) -> usize {
asm volatile ("syscall"
: [ret] "={rax}" (-> isize)
: [ret] "={rax}" (-> usize)
: [number] "{rax}" (number),
[arg1] "{rdi}" (arg1),
[arg2] "{rsi}" (arg2),
@ -427,11 +427,11 @@ pub inline fn syscall5(number: isize, arg1: isize, arg2: isize, arg3: isize, arg
: "rcx", "r11")
}
pub inline fn syscall6(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize,
arg5: isize, arg6: isize) -> isize
pub inline fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize,
arg5: usize, arg6: usize) -> usize
{
asm volatile ("syscall"
: [ret] "={rax}" (-> isize)
: [ret] "={rax}" (-> usize)
: [number] "{rax}" (number),
[arg1] "{rdi}" (arg1),
[arg2] "{rsi}" (arg2),

View File

@ -6,11 +6,11 @@ pub inline fn List(inline T: type) -> type {
SmallList(T, 8)
}
pub struct SmallList(T: type, STATIC_SIZE: isize) {
pub struct SmallList(T: type, STATIC_SIZE: usize) {
const Self = SmallList(T, STATIC_SIZE);
items: []T,
length: isize,
length: usize,
prealloc_items: [STATIC_SIZE]T,
allocator: &Allocator,
@ -33,7 +33,7 @@ pub struct SmallList(T: type, STATIC_SIZE: isize) {
l.length = new_length;
}
pub fn ensure_capacity(l: &Self, new_capacity: isize) -> %void {
pub fn ensure_capacity(l: &Self, new_capacity: usize) -> %void {
const old_capacity = l.items.len;
var better_capacity = old_capacity;
while (better_capacity < new_capacity) {
@ -58,15 +58,15 @@ var global_allocator = Allocator {
};
var some_mem: [200]u8 = undefined;
var some_mem_index: isize = 0;
var some_mem_index: usize = 0;
fn global_alloc(self: &Allocator, n: isize) -> %[]u8 {
fn global_alloc(self: &Allocator, n: usize) -> %[]u8 {
const result = some_mem[some_mem_index ... some_mem_index + n];
some_mem_index += n;
return result;
}
fn global_realloc(self: &Allocator, old_mem: []u8, new_size: isize) -> %[]u8 {
fn global_realloc(self: &Allocator, old_mem: []u8, new_size: usize) -> %[]u8 {
const result = %return global_alloc(self, new_size);
@memcpy(result.ptr, old_mem.ptr, old_mem.len);
return result;
@ -81,11 +81,11 @@ fn basic_list_test() {
list.init(&global_allocator);
defer list.deinit();
{var i: isize = 0; while (i < 10; i += 1) {
{var i: usize = 0; while (i < 10; i += 1) {
%%list.append(i32(i + 1));
}}
{var i: isize = 0; while (i < 10; i += 1) {
{var i: usize = 0; while (i < 10; i += 1) {
assert(list.items[i] == i32(i + 1));
}}
}

View File

@ -7,13 +7,13 @@ pub error NoMem;
pub type Context = u8;
pub struct Allocator {
alloc_fn: fn (self: &Allocator, n: isize) -> %[]u8,
realloc_fn: fn (self: &Allocator, old_mem: []u8, new_size: isize) -> %[]u8,
alloc_fn: fn (self: &Allocator, n: usize) -> %[]u8,
realloc_fn: fn (self: &Allocator, old_mem: []u8, new_size: usize) -> %[]u8,
free_fn: fn (self: &Allocator, mem: []u8),
context: ?&Context,
/// Aborts the program if an allocation fails.
fn checked_alloc(self: &Allocator, inline T: type, n: isize) -> []T {
fn checked_alloc(self: &Allocator, inline T: type, n: usize) -> []T {
alloc(self, T, n) %% |err| {
// TODO var args printf
%%io.stderr.write("allocation failure: ");
@ -23,13 +23,13 @@ pub struct Allocator {
}
}
fn alloc(self: &Allocator, inline T: type, n: isize) -> %[]T {
const byte_count = %return math.mul_overflow(isize, @sizeof(T), n);
fn alloc(self: &Allocator, inline T: type, n: usize) -> %[]T {
const byte_count = %return math.mul_overflow(usize, @sizeof(T), n);
([]T)(%return self.alloc_fn(self, byte_count))
}
fn realloc(self: &Allocator, inline T: type, old_mem: []T, n: isize) -> %[]T {
const byte_count = %return math.mul_overflow(isize, @sizeof(T), n);
fn realloc(self: &Allocator, inline T: type, old_mem: []T, n: usize) -> %[]T {
const byte_count = %return math.mul_overflow(usize, @sizeof(T), n);
([]T)(%return self.realloc_fn(self, ([]u8)(old_mem), byte_count))
}

View File

@ -15,7 +15,7 @@ pub error BadFd;
struct Connection {
socket_fd: i32,
pub fn send(c: Connection, buf: []const u8) -> %isize {
pub fn send(c: Connection, buf: []const u8) -> %usize {
const send_ret = linux.sendto(c.socket_fd, buf.ptr, buf.len, 0, null, 0);
const send_err = linux.get_errno(send_ret);
switch (send_err) {

View File

@ -7,9 +7,10 @@ pub error Unexpected;
pub fn get_random_bytes(buf: []u8) -> %void {
switch (@compile_var("os")) {
linux => {
const amt_got = linux.getrandom(buf.ptr, buf.len, 0);
if (amt_got < 0) {
return switch (-amt_got) {
const ret = linux.getrandom(buf.ptr, buf.len, 0);
const err = linux.get_errno(ret);
if (err > 0) {
return switch (err) {
errno.EINVAL => unreachable{},
errno.EFAULT => unreachable{},
errno.EINTR => error.SigInterrupt,

View File

@ -4,7 +4,7 @@ const ARRAY_SIZE = 624;
/// Use `init` to initialize this state.
pub struct Rand {
array: [ARRAY_SIZE]u32,
index: isize,
index: usize,
/// Initialize random state with the given seed.
#static_eval_enable(false)
@ -12,7 +12,7 @@ pub struct Rand {
var r: Rand = undefined;
r.index = 0;
r.array[0] = seed;
var i : isize = 1;
var i : usize = 1;
var prev_value: u64w = seed;
while (i < ARRAY_SIZE; i += 1) {
r.array[i] = @truncate(u32, (prev_value ^ (prev_value << 30)) * 0x6c078965 + u64w(i));
@ -91,7 +91,7 @@ pub struct Rand {
}
// does not populate the remaining (buf.len % 4) bytes
fn get_bytes_aligned(r: &Rand, buf: []u8) -> isize {
fn get_bytes_aligned(r: &Rand, buf: []u8) -> usize {
var bytes_left = buf.len;
while (bytes_left >= 4) {
*((&u32)(&buf[buf.len - bytes_left])) = r.get_u32();

View File

@ -11,9 +11,9 @@ pub fn run_tests() -> %void {
for (zig_test_fn_list) |test_fn, i| {
// TODO: print var args
%%io.stderr.write("Test ");
%%io.stderr.print_i64(i + 1);
%%io.stderr.print_u64(i + 1);
%%io.stderr.write("/");
%%io.stderr.print_i64(zig_test_fn_list.len);
%%io.stderr.print_u64(zig_test_fn_list.len);
%%io.stderr.write(" ");
%%io.stderr.write(test_fn.name);
%%io.stderr.write("...");

View File

@ -466,7 +466,7 @@ pub fn main(args: [][]u8) -> %void {
%%io.stdout.printf("\n");
}
for (array) |item, index| {
%%io.stdout.print_i64(index);
%%io.stdout.print_u64(index);
%%io.stdout.printf("\n");
}
const unknown_size: []u8 = array;
@ -475,7 +475,7 @@ pub fn main(args: [][]u8) -> %void {
%%io.stdout.printf("\n");
}
for (unknown_size) |item, index| {
%%io.stdout.print_i64(index);
%%io.stdout.print_u64(index);
%%io.stdout.printf("\n");
}
}
@ -497,7 +497,7 @@ export fn compare_fn(a: ?&const c_void, b: ?&const c_void) -> c_int {
}
export fn main(args: c_int, argv: &&u8) -> c_int {
var array = []i32 { 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 };
var array = []u32 { 1, 7, 3, 2, 0, 9, 4, 8, 6, 5 };
c.qsort((&c_void)(&array[0]), c_ulong(array.len), @sizeof(i32), compare_fn);
@ -816,7 +816,7 @@ fn f() {
)SOURCE", 4, ".tmp_source.zig:4:5: error: use of undeclared identifier 'i'",
".tmp_source.zig:4:7: 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 'isize', got 'bool'");
".tmp_source.zig:5:9: error: expected type 'usize', got 'bool'");
add_compile_fail_case("variadic functions only allowed in extern", R"SOURCE(
fn f(...) {}
@ -1115,8 +1115,8 @@ fn a(x: i32) {
struct Foo {
y: [get()]u8,
}
var global_var: isize = 1;
fn get() -> isize { global_var }
var global_var: usize = 1;
fn get() -> usize { global_var }
)SOURCE", 1, ".tmp_source.zig:3:9: error: unable to evaluate constant expression");
@ -1397,7 +1397,7 @@ pub fn main(args: [][]u8) { }
add_compile_fail_case("invalid pointer for var type", R"SOURCE(
extern fn ext() -> isize;
extern fn ext() -> usize;
var bytes: [ext()]u8 = undefined;
fn f() {
for (bytes) |*b, i| {

View File

@ -96,16 +96,16 @@ fn mutable_local_variables() {
#attribute("test")
fn arrays() {
var array : [5]i32 = undefined;
var array : [5]u32 = undefined;
var i : i32 = 0;
var i : u32 = 0;
while (i < 5) {
array[i] = i + 1;
i = array[i];
}
i = 0;
var accumulator = i32(0);
var accumulator = u32(0);
while (i < 5) {
accumulator += array[i];
@ -115,7 +115,7 @@ fn arrays() {
assert(accumulator == 15);
assert(get_array_len(array) == 5);
}
fn get_array_len(a: []i32) -> isize {
fn get_array_len(a: []u32) -> usize {
a.len
}
@ -742,7 +742,7 @@ fn generic_malloc_free() {
}
const some_mem : [100]u8 = undefined;
#static_eval_enable(false)
fn mem_alloc(inline T: type, n: isize) -> %[]T {
fn mem_alloc(inline T: type, n: usize) -> %[]T {
return (&T)(&some_mem[0])[0...n];
}
fn mem_free(inline T: type, mem: []T) { }
@ -823,10 +823,10 @@ fn test_cast_undefined(x: []u8) {}
#attribute("test")
fn cast_small_unsigned_to_larger_signed() {
assert(cast_small_unsigned_to_larger_signed_1(200) == i16(200));
assert(cast_small_unsigned_to_larger_signed_2(9999) == isize(9999));
assert(cast_small_unsigned_to_larger_signed_2(9999) == i64(9999));
}
fn cast_small_unsigned_to_larger_signed_1(x: u8) -> i16 { x }
fn cast_small_unsigned_to_larger_signed_2(x: u16) -> isize { x }
fn cast_small_unsigned_to_larger_signed_2(x: u16) -> i64 { x }
#attribute("test")
@ -834,7 +834,7 @@ fn implicit_cast_after_unreachable() {
assert(outer() == 1234);
}
fn inner() -> i32 { 1234 }
fn outer() -> isize {
fn outer() -> i64 {
return inner();
}
@ -1029,7 +1029,7 @@ fn constant_expressions() {
var array : [ARRAY_SIZE]u8 = undefined;
assert(@sizeof(@typeof(array)) == 20);
}
const ARRAY_SIZE : i8 = 20;
const ARRAY_SIZE : u8 = 20;
#attribute("test")
@ -1315,7 +1315,7 @@ fn test_return_empty_struct_from_fn_noeval() -> EmptyStruct2 {
fn pass_slice_of_empty_struct_to_fn() {
assert(test_pass_slice_of_empty_struct_to_fn([]EmptyStruct2{ EmptyStruct2{} }) == 1);
}
fn test_pass_slice_of_empty_struct_to_fn(slice: []EmptyStruct2) -> isize {
fn test_pass_slice_of_empty_struct_to_fn(slice: []EmptyStruct2) -> usize {
slice.len
}
@ -1555,7 +1555,7 @@ fn c_string_concatenation() {
const len = cstr.len(b);
const len_with_null = len + 1;
{var i: i32 = 0; while (i < len_with_null; i += 1) {
{var i: u32 = 0; while (i < len_with_null; i += 1) {
assert(a[i] == b[i]);
}}
assert(a[len] == 0);