fix various bugs related to guess number example

This commit is contained in:
Andrew Kelley 2016-01-02 21:56:33 -07:00
parent fb1e3a5be9
commit 1abb4e59be
5 changed files with 25 additions and 17 deletions

View File

@ -10,7 +10,9 @@ pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
print_str("Welcome to the Guess Number Game in Zig.\n");
var seed : u32;
if (os_get_random_bytes(&seed as &u8, 4) != 0) {
var err : isize;
// TODO #sizeof(u32) instead of 4
if ({err = os_get_random_bytes(&seed as &u8, 4); err != 4}) {
// TODO full error message
fprint_str(stderr_fileno, "unable to get random bytes");
return 1;

View File

@ -196,6 +196,7 @@ static TypeTableEntry *get_array_type(CodeGen *g, TypeTableEntry *child_type, ui
entry->size_in_bits = child_type->size_in_bits * array_size;
entry->align_in_bits = child_type->align_in_bits;
entry->di_type = LLVMZigCreateDebugArrayType(g->dbuilder, entry->size_in_bits,
entry->align_in_bits, child_type->di_type, array_size);
entry->data.array.child_type = child_type;

View File

@ -1570,6 +1570,7 @@ static void do_code_gen(CodeGen *g) {
add_debug_source_node(g, var->decl_node);
var->value_ref = LLVMBuildAlloca(g->builder, var->type->type_ref, buf_ptr(&var->name));
LLVMSetAlignment(var->value_ref, var->type->align_in_bits / 8);
}
var->di_loc_var = LLVMZigCreateLocalVariable(g->dbuilder, tag,

View File

@ -628,7 +628,7 @@ static uint8_t parse_char_literal(ParseContext *pc, Token *token) {
} else if (return_count > 1) {
ast_error(pc, token, "character literal too long");
}
return return_count;
return return_value;
}
static void parse_string_literal(ParseContext *pc, Token *token, Buf *buf, bool *out_c_str,

View File

@ -1,9 +1,6 @@
const SYS_write : usize = 1;
const SYS_exit : usize = 60;
const SYS_getrandom : usize = 278;
const stdout_fileno : isize = 1;
const stderr_fileno : isize = 2;
const SYS_getrandom : usize = 318;
fn syscall1(number: usize, arg1: usize) -> usize {
asm volatile ("syscall"
@ -19,32 +16,37 @@ fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) -> usize {
: "rcx", "r11")
}
pub fn getrandom(buf: &u8, count: usize, flags: u32) -> i32 {
return syscall3(SYS_getrandom, buf as usize, count, flags as usize) as i32;
}
pub fn write(fd: isize, buf: &const u8, count: usize) -> isize {
return syscall3(SYS_write, fd as usize, buf as usize, count) as isize;
syscall3(SYS_write, fd as usize, buf as usize, count) as isize
}
pub fn exit(status: i32) -> unreachable {
syscall1(SYS_exit, status as usize);
unreachable;
unreachable
}
pub fn getrandom(buf: &u8, count: usize, flags: u32) -> isize {
syscall3(SYS_getrandom, buf as usize, count, flags as usize) as isize
}
const stdout_fileno : isize = 1;
const stderr_fileno : isize = 2;
// TODO error handling
pub fn os_get_random_bytes(buf: &u8, count: usize) -> i32 {
return getrandom(buf, count, 0);
pub fn os_get_random_bytes(buf: &u8, count: usize) -> isize {
getrandom(buf, count, 0)
}
// TODO error handling
// TODO handle buffering and flushing (mutex protected)
pub fn print_str(str: string) -> isize { fprint_str(stdout_fileno, str) }
pub fn print_str(str: string) -> isize {
fprint_str(stdout_fileno, str)
}
// TODO error handling
// TODO handle buffering and flushing (mutex protected)
pub fn fprint_str(fd: isize, str: string) -> isize {
return write(fd, str.ptr, str.len);
write(fd, str.ptr, str.len)
}
// TODO handle buffering and flushing (mutex protected)
@ -56,7 +58,9 @@ pub fn print_u64(x: u64) -> isize {
return write(stdout_fileno, buf.ptr, len);
}
fn digit_to_char(digit: u64) -> u8 { '0' + (digit as u8) }
fn digit_to_char(digit: u64) -> u8 {
'0' + (digit as u8)
}
const max_u64_base10_digits: usize = 20;