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

@@ -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("...");