replace array with an actual struct now that we have them
This commit is contained in:
@@ -70,7 +70,7 @@ macro_rules! emit {
|
||||
static REGISTERS: [&str; 6] = ["rdi", "rsi", "rdx", "rcx", "r8", "r9"];
|
||||
|
||||
// TODO: currently they are all just 64 bit values
|
||||
static BUILTIN_TYPES: [&str; 7] = ["void", "u8", "i64", "str", "bool", "ptr", "array"];
|
||||
static BUILTIN_TYPES: [&str; 6] = ["void", "u8", "i64", "str", "bool", "ptr"];
|
||||
|
||||
pub struct CodegenX86_64<'a> {
|
||||
output: String,
|
||||
@@ -226,7 +226,7 @@ _builtin_environ:
|
||||
Stmt::Function {
|
||||
name,
|
||||
params,
|
||||
return_type: _,
|
||||
return_type,
|
||||
body,
|
||||
exported,
|
||||
} => {
|
||||
@@ -239,10 +239,17 @@ _builtin_environ:
|
||||
emit!(&mut self.output, " mov rbp, rsp");
|
||||
emit!(&mut self.output, " sub rsp, 256"); // TODO
|
||||
|
||||
if !self.is_valid_type_name(&return_type.lexeme) {
|
||||
return error!(
|
||||
&return_type.loc,
|
||||
"unrecognized type: ".to_owned() + &return_type.lexeme
|
||||
);
|
||||
}
|
||||
|
||||
for (i, param) in params.iter().enumerate() {
|
||||
if !self.is_valid_type_name(¶m.var_type.lexeme) {
|
||||
return error!(
|
||||
&name.loc,
|
||||
¶m.var_name.loc,
|
||||
"unrecognized type: ".to_owned() + ¶m.var_type.lexeme
|
||||
);
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ func crypto.blake2b._G[v: ptr, a: i64, b: i64, c: i64, d: i64, x: i64, y: i64] :
|
||||
mem.write64(v + c * 8, mem.read64(v + c * 8) + mem.read64(v + d * 8))
|
||||
mem.write64(v + b * 8, crypto.rotr64(mem.read64(v + b * 8) ^ mem.read64(v + c * 8), 63))
|
||||
|
||||
func crypto.blake2b._compress[h: ptr, block: ptr, t0: i64, t1: i64, last: i64, iv: array, sigma: array] : void
|
||||
func crypto.blake2b._compress[h: ptr, block: ptr, t0: i64, t1: i64, last: i64, iv: Array, sigma: Array] : void
|
||||
let v: ptr = mem.alloc(16 * 8)
|
||||
let m: ptr = mem.alloc(16 * 8)
|
||||
|
||||
@@ -60,17 +60,17 @@ func crypto.blake2b._compress[h: ptr, block: ptr, t0: i64, t1: i64, last: i64, i
|
||||
|
||||
func crypto.blake2b.hash[outlen: i64, key: ptr, keylen: i64, input: ptr, inputlen: i64] : ptr
|
||||
if outlen == 0 || outlen > 64 || keylen > 64
|
||||
dbg.panic("invalid length passed to crypto.blake2b.hash")
|
||||
panic("invalid length passed to crypto.blake2b.hash")
|
||||
let out: ptr = mem.alloc(outlen)
|
||||
|
||||
// IV[i] = floor(2**w * frac(sqrt(prime(i+1)))), where prime(i)
|
||||
// is the i:th prime number ( 2, 3, 5, 7, 11, 13, 17, 19 )
|
||||
// and sqrt(x) is the square root of x.
|
||||
// https://www.ietf.org/rfc/rfc7693#section-2.6
|
||||
let iv: array = [0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179]
|
||||
let iv: Array = [0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179]
|
||||
|
||||
// https://crypto.stackexchange.com/questions/108987/rationale-for-blake2-message-schedule
|
||||
let sigma: array = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3,11,8,12,0,5,2,15,13,10,14,3,6,7,1,9,4,7,9,3,1,13,12,11,14,2,6,5,10,4,0,15,8,9,0,5,7,2,4,10,15,14,1,11,12,6,8,3,13,2,12,6,10,0,11,8,3,4,13,7,5,15,14,1,9,12,5,1,15,14,13,4,10,0,7,6,3,9,2,8,11,13,11,7,14,12,1,3,9,5,0,15,4,8,6,2,10,6,15,14,9,11,3,0,8,12,2,13,7,1,4,10,5,10,2,8,4,7,6,1,5,15,11,9,14,3,12,13,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3]
|
||||
let sigma: Array = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3,11,8,12,0,5,2,15,13,10,14,3,6,7,1,9,4,7,9,3,1,13,12,11,14,2,6,5,10,4,0,15,8,9,0,5,7,2,4,10,15,14,1,11,12,6,8,3,13,2,12,6,10,0,11,8,3,4,13,7,5,15,14,1,9,12,5,1,15,14,13,4,10,0,7,6,3,9,2,8,11,13,11,7,14,12,1,3,9,5,0,15,4,8,6,2,10,6,15,14,9,11,3,0,8,12,2,13,7,1,4,10,5,10,2,8,4,7,6,1,5,15,11,9,14,3,12,13,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3]
|
||||
|
||||
let h: ptr = mem.alloc(8 * 8)
|
||||
let t0 = 0
|
||||
|
||||
138
src/std/std.zr
138
src/std/std.zr
@@ -3,7 +3,7 @@ extern realloc
|
||||
extern free
|
||||
extern gethostbyname
|
||||
|
||||
func dbg.panic[msg: str] : void
|
||||
func panic[msg: str] : void
|
||||
io.print("PANIC: ")
|
||||
io.println(msg)
|
||||
(0 / 0) // crashes program which is kinda better since you get a gdb backtrace
|
||||
@@ -90,7 +90,7 @@ func io.read_line[]: 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")
|
||||
panic("failed to open file")
|
||||
|
||||
let size: i64 = _builtin_syscall(SYS_lseek, fd, 0, 2)
|
||||
_builtin_syscall(SYS_lseek, fd, 0, 0)
|
||||
@@ -104,7 +104,7 @@ func io.read_file[path: str] : str
|
||||
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")
|
||||
panic("failed to open file")
|
||||
|
||||
let size: i64 = _builtin_syscall(SYS_lseek, fd, 0, 2)
|
||||
_builtin_syscall(SYS_lseek, fd, 0, 0)
|
||||
@@ -118,7 +118,7 @@ func io.read_binary_file[path: str, len_ptr: ptr] : ptr
|
||||
func io.write_file[path: str, content: str] : void
|
||||
let fd: ptr = _builtin_syscall(SYS_openat, -100, path, 0x241, 0o644)
|
||||
if fd < 0
|
||||
dbg.panic("failed to open file")
|
||||
panic("failed to open file")
|
||||
|
||||
_builtin_syscall(SYS_write, fd, content, str.len(content))
|
||||
_builtin_syscall(SYS_close, fd)
|
||||
@@ -195,7 +195,7 @@ func str.find[haystack: str, needle: str] : i64
|
||||
|
||||
func str.substr[s: str, start: i64, length: i64] : str
|
||||
if start < 0 || length < 0 || start + length > str.len(s)
|
||||
dbg.panic("str.substr out of bounds")
|
||||
panic("str.substr out of bounds")
|
||||
|
||||
let out: str = mem.alloc(length + 1)
|
||||
for i in 0..length
|
||||
@@ -219,10 +219,10 @@ func str.trim[s: str] : str
|
||||
|
||||
return str.substr(s, start, end - start + 1)
|
||||
|
||||
func str.split[haystack: str, needle: str]: array
|
||||
func str.split[haystack: str, needle: str]: Array
|
||||
let haystack_len: i64 = str.len(haystack)
|
||||
let needle_len: i64 = str.len(needle)
|
||||
let result: array = []
|
||||
let result: Array = []
|
||||
|
||||
if !needle_len
|
||||
if !haystack_len
|
||||
@@ -410,7 +410,7 @@ func math.lcm[a: i64, b: i64] : i64
|
||||
|
||||
func math.isqrt[n: i64] : i64
|
||||
if n < 0
|
||||
dbg.panic("negative number passed to math.isqrt")
|
||||
panic("negative number passed to math.isqrt")
|
||||
if n == 0 || n == 1
|
||||
return n
|
||||
|
||||
@@ -438,85 +438,74 @@ func math.is_prime[n: i64]: bool
|
||||
i = i + 6
|
||||
return true
|
||||
|
||||
func array.new[] : array
|
||||
// [ 8 bytes - ptr to data ] [ 8 bytes - size ] [ 8 bytes - capacity ]
|
||||
let arr: ptr = mem.alloc(24)
|
||||
mem.zero(arr, 24)
|
||||
return arr
|
||||
struct Array
|
||||
data: ptr
|
||||
size: i64
|
||||
capacity: i64
|
||||
|
||||
func array.nth[xs: array, n: i64] : i64
|
||||
if n < 0 || n >= array.size(xs)
|
||||
dbg.panic("array.nth out of bounds")
|
||||
let data: ptr = mem.read64(xs)
|
||||
return mem.read64(data + n * 8)
|
||||
func array.new[] : Array
|
||||
return new Array
|
||||
|
||||
func array.set[xs: array, n: i64, x: i64] : void
|
||||
if n < 0 || n >= array.size(xs)
|
||||
dbg.panic("array.set out of bounds")
|
||||
let data: ptr = mem.read64(xs)
|
||||
mem.write64(data + n * 8, x)
|
||||
func array.nth[xs: Array, n: i64] : i64
|
||||
if n < 0 || n >= xs->size
|
||||
panic("array.nth out of bounds")
|
||||
return mem.read64(xs->data + n * 8)
|
||||
|
||||
func array.push[xs: array, x: i64] : void
|
||||
let data: ptr = mem.read64(xs)
|
||||
let capacity: i64 = mem.read64(xs + 8)
|
||||
let size: i64 = mem.read64(xs + 16)
|
||||
func array.set[xs: Array, n: i64, x: i64] : void
|
||||
if n < 0 || n >= xs->size
|
||||
panic("array.set out of bounds")
|
||||
mem.write64(xs->data + n * 8, x)
|
||||
|
||||
if size == capacity
|
||||
func array.push[xs: Array, x: i64] : void
|
||||
if xs->size == xs->capacity
|
||||
let new_capacity = 4
|
||||
if capacity != 0
|
||||
new_capacity = capacity * 2
|
||||
let new_data: ptr = realloc(data, new_capacity * 8)
|
||||
mem.write64(xs, new_data)
|
||||
mem.write64(xs + 8, new_capacity)
|
||||
data = new_data
|
||||
if xs->capacity != 0
|
||||
new_capacity = xs->capacity * 2
|
||||
xs->data = realloc(xs->data, new_capacity * 8)
|
||||
xs->capacity = new_capacity
|
||||
|
||||
mem.write64(data + size * 8, x)
|
||||
mem.write64(xs + 16, size + 1)
|
||||
mem.write64(xs->data + xs->size * 8, x)
|
||||
xs->size = xs->size + 1
|
||||
|
||||
func array.size[xs: array] : i64
|
||||
return mem.read64(xs + 16)
|
||||
|
||||
func array.free[xs: array] : void
|
||||
let data: ptr = mem.read64(xs)
|
||||
if data != 0
|
||||
mem.free(data)
|
||||
func array.free[xs: Array] : void
|
||||
if xs->data != 0
|
||||
mem.free(xs->data)
|
||||
mem.free(xs)
|
||||
|
||||
func array.pop[xs: array] : i64
|
||||
let size: i64 = array.size(xs)
|
||||
if size == 0
|
||||
dbg.panic("array.pop on empty array")
|
||||
let x: i64 = array.nth(xs, size - 1)
|
||||
mem.write64(xs + 16, size - 1)
|
||||
func array.pop[xs: Array] : i64
|
||||
if xs->size == 0
|
||||
panic("array.pop on empty array")
|
||||
let x: i64 = array.nth(xs, xs->size - 1)
|
||||
xs->size = xs->size - 1
|
||||
return x
|
||||
|
||||
func array.slice[xs: array, start: i64, length: i64] : array
|
||||
if start < 0 || length < 0 || start + length > array.size(xs)
|
||||
dbg.panic("array.slice out of bounds")
|
||||
func array.slice[xs: Array, start: i64, length: i64] : Array
|
||||
if start < 0 || length < 0 || start + length > xs->size
|
||||
panic("array.slice out of bounds")
|
||||
|
||||
let new_array: array = []
|
||||
let new_array: Array = []
|
||||
for i in 0..length
|
||||
array.push(new_array, array.nth(xs, start + i))
|
||||
return new_array
|
||||
|
||||
func array.concat[a: array, b: array] : array
|
||||
let new_array: array = []
|
||||
for i in 0..array.size(a)
|
||||
func array.concat[a: Array, b: Array] : Array
|
||||
let new_array: Array = []
|
||||
for i in 0..a->size
|
||||
array.push(new_array, array.nth(a, i))
|
||||
for i in 0..array.size(b)
|
||||
for i in 0..b->size
|
||||
array.push(new_array, array.nth(b, i))
|
||||
return new_array
|
||||
|
||||
func alg.quicksort[arr: array] : void
|
||||
alg._do_quicksort(arr, 0, array.size(arr) - 1)
|
||||
func alg.quicksort[arr: Array] : void
|
||||
alg._do_quicksort(arr, 0, arr->size - 1)
|
||||
|
||||
func alg._do_quicksort[arr: array, low: i64, high: i64] : void
|
||||
func alg._do_quicksort[arr: Array, low: i64, high: i64] : void
|
||||
if low < high
|
||||
let i: i64 = alg._partition(arr, low, high)
|
||||
alg._do_quicksort(arr, low, i - 1)
|
||||
alg._do_quicksort(arr, i + 1, high)
|
||||
|
||||
func alg._partition[arr: array, low: i64, high: i64] : i64
|
||||
func alg._partition[arr: Array, low: i64, high: i64] : i64
|
||||
let pivot: i64 = array.nth(arr, high)
|
||||
let i: i64 = low - 1
|
||||
for j in (low)..high
|
||||
@@ -530,29 +519,28 @@ func alg._partition[arr: array, low: i64, high: i64] : i64
|
||||
array.set(arr, high, temp)
|
||||
return i + 1
|
||||
|
||||
func alg.count[arr: array, item: i64] : i64
|
||||
func alg.count[arr: Array, item: i64] : i64
|
||||
let count = 0
|
||||
let size: i64 = array.size(arr)
|
||||
for i in 0..size
|
||||
for i in 0..arr->size
|
||||
if array.nth(arr, i) == item
|
||||
count = count + 1
|
||||
return count
|
||||
|
||||
func alg.map[arr: array, fn: ptr] : array
|
||||
let out: array = []
|
||||
for i in 0..array.size(arr)
|
||||
func alg.map[arr: Array, fn: ptr] : Array
|
||||
let out: Array = []
|
||||
for i in 0..arr->size
|
||||
array.push(out, fn(array.nth(arr, i)))
|
||||
return out
|
||||
|
||||
func alg.filter[arr: array, fn: ptr] : array
|
||||
let out: array = []
|
||||
for i in 0..array.size(arr)
|
||||
func alg.filter[arr: Array, fn: ptr] : Array
|
||||
let out: Array = []
|
||||
for i in 0..arr->size
|
||||
if fn(array.nth(arr, i))
|
||||
array.push(out, array.nth(arr, i))
|
||||
return out
|
||||
|
||||
func alg.reduce[arr: array, fn: ptr, acc: i64] : i64
|
||||
for i in 0..array.size(arr)
|
||||
func alg.reduce[arr: Array, fn: ptr, acc: i64] : i64
|
||||
for i in 0..arr->size
|
||||
acc = fn(acc, array.nth(arr, i))
|
||||
return acc
|
||||
|
||||
@@ -585,7 +573,7 @@ func os.shell[command: str] : i64
|
||||
let pid: i64 = _builtin_syscall(SYS_fork)
|
||||
if pid == 0
|
||||
// leaky but not sure where can i free it
|
||||
let argv: array = ["sh", "-c", command, 0]
|
||||
let argv: Array = ["sh", "-c", command, 0]
|
||||
_builtin_syscall(SYS_execve, "/bin/sh", mem.read64(argv), _builtin_environ())
|
||||
_builtin_syscall(SYS_exit, 1)
|
||||
else
|
||||
@@ -600,12 +588,12 @@ func os.shell[command: str] : i64
|
||||
else
|
||||
return -(st & 0x7f)
|
||||
|
||||
func os.listdir[path: str] : array
|
||||
func os.listdir[path: str] : Array
|
||||
let fd: i64 = _builtin_syscall(SYS_openat, -100, path, 0, 0)
|
||||
if fd < 0
|
||||
return []
|
||||
|
||||
let files: array = []
|
||||
let files: Array = []
|
||||
let buf: ptr = mem.alloc(1024)
|
||||
while true
|
||||
let n: i64 = _builtin_syscall(SYS_getdents64, fd, buf, 1024)
|
||||
|
||||
Reference in New Issue
Block a user