zero initialize structs, begin porting my chip8 emulator
This commit is contained in:
@@ -111,11 +111,6 @@ _builtin_read64:
|
||||
mov rax, qword [rdi]
|
||||
ret
|
||||
|
||||
section .text._builtin_set8
|
||||
_builtin_set8:
|
||||
mov [rdi], sil
|
||||
ret
|
||||
|
||||
section .text._builtin_set64
|
||||
_builtin_set64:
|
||||
mov [rdi], rsi
|
||||
@@ -671,8 +666,14 @@ _builtin_environ:
|
||||
Expr::New(struct_name) => {
|
||||
let struct_fields = &self.analyzer.structs[&struct_name.lexeme];
|
||||
|
||||
emit!(&mut self.output, " mov rdi, {}", struct_fields.len() * 8);
|
||||
let memory_size = struct_fields.len() * 8;
|
||||
emit!(&mut self.output, " mov rdi, {}", memory_size);
|
||||
emit!(&mut self.output, " call mem.alloc");
|
||||
emit!(&mut self.output, " push rax");
|
||||
emit!(&mut self.output, " mov rdi, rax");
|
||||
emit!(&mut self.output, " mov rsi, {}", memory_size);
|
||||
emit!(&mut self.output, " call mem.zero");
|
||||
emit!(&mut self.output, " pop rax");
|
||||
}
|
||||
Expr::MemberAccess { left, field } => {
|
||||
let offset = self.get_field_offset(env, left, field)?;
|
||||
|
||||
@@ -77,11 +77,12 @@ func io.read_line[]: str
|
||||
let buffer: str = mem.alloc(MAX_SIZE + 1)
|
||||
let n: i64 = _builtin_syscall(SYS_read, 0, buffer, MAX_SIZE)
|
||||
if n < 0
|
||||
mem.free(buffer)
|
||||
return ""
|
||||
buffer[n] = 0
|
||||
return buffer
|
||||
|
||||
func io.read_file[path: str]: str
|
||||
func io.read_file[path: str] : str
|
||||
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||
if fd <= 0
|
||||
dbg.panic("failed to open file")
|
||||
@@ -91,8 +92,22 @@ func io.read_file[path: str]: str
|
||||
|
||||
let buffer: str = mem.alloc(size + 1)
|
||||
let n: i64 = _builtin_syscall(SYS_read, fd, buffer, size)
|
||||
buffer[n] = 0
|
||||
_builtin_syscall(SYS_close, fd)
|
||||
buffer[n] = 0
|
||||
return buffer
|
||||
|
||||
func io.read_binary_file[path: str, len_ptr: ptr] : ptr
|
||||
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||
if fd < 0
|
||||
dbg.panic("failed to open file")
|
||||
|
||||
let size: i64 = _builtin_syscall(SYS_lseek, fd, 0, 2)
|
||||
_builtin_syscall(SYS_lseek, fd, 0, 0)
|
||||
|
||||
let buffer: ptr = mem.alloc(size)
|
||||
_builtin_syscall(SYS_read, fd, buffer, size)
|
||||
_builtin_syscall(SYS_close, fd)
|
||||
mem.write64(len_ptr, size)
|
||||
return buffer
|
||||
|
||||
func io.write_file[path: str, content: str] : void
|
||||
|
||||
Reference in New Issue
Block a user