diff --git a/src/codegen_x86_64.rs b/src/codegen_x86_64.rs index 4369d88..704daee 100644 --- a/src/codegen_x86_64.rs +++ b/src/codegen_x86_64.rs @@ -125,37 +125,37 @@ extern closedir extern exit extern gettimeofday -section .text.deref -deref: +section .text._builtin_deref +_builtin_deref: mov rax, qword [rdi] ret -section .text.IO.stdin -IO.stdin: +section .text._builtin_stdin +_builtin_stdin: mov rax, [rel stdin] ret -section .text.Bit.lshift -Bit.lshift: +section .text._builtin_lshift +_builtin_lshift: mov rcx, rsi mov rax, rdi shl rax, cl ret -section .text.Bit.rshift -Bit.rshift: +section .text._builtin_rshift +_builtin_rshift: mov rcx, rsi mov rax, rdi sar rax, cl ret -section .text.String.set -String.set: +section .text._builtin_string_set +_builtin_string_set: mov [rdi + rsi], dl ret -section .text.OS.listdir -OS.listdir: +section .text._builtin_listdir +_builtin_listdir: push r14 push rbx push rax @@ -165,29 +165,29 @@ OS.listdir: mov rdi, r14 call opendir mov r14, rax -.OS.listdir.1: +._builtin_listdir.1: mov rdi, r14 call readdir test rax, rax - je .OS.listdir.3 + je ._builtin_listdir.3 cmp byte [rax+19], 46 - jne .OS.listdir.2 + jne ._builtin_listdir.2 movzx ecx, byte [rax+20] test ecx, ecx - je .OS.listdir.1 + je ._builtin_listdir.1 cmp ecx, 46 - jne .OS.listdir.2 + jne ._builtin_listdir.2 cmp byte [rax+21], 0 - je .OS.listdir.1 -.OS.listdir.2: + je ._builtin_listdir.1 +._builtin_listdir.2: add rax, 19 mov rdi, rax call strdup mov rsi, rax mov rdi, rbx call Array.push - jmp .OS.listdir.1 -.OS.listdir.3: + jmp ._builtin_listdir.1 +._builtin_listdir.3: mov rdi, r14 call closedir mov rax, rbx @@ -196,14 +196,14 @@ OS.listdir: pop r14 ret -section .text.Array.set -Array.set: +section .text._builtin_array_set +_builtin_array_set: mov rax, [rdi] mov [rax + rsi*8], rdx ret -section .text.Array.push -Array.push: +section .text._builtin_array_push +_builtin_array_push: push r14 push rbx push rax @@ -212,7 +212,7 @@ Array.push: mov rax, [rdi] mov rcx, [rdi + 16] cmp rcx, [rdi + 8] - jne .Array.push.1 + jne ._builtin_array_push.1 lea rdx, [rcx + rcx] mov rsi, 4 test rcx, rcx @@ -223,7 +223,7 @@ Array.push: call realloc mov [rbx], rax mov rcx, [rbx + 16] -.Array.push.1: +._builtin_array_push.1: mov [rax + rcx*8], r14 inc qword [rbx + 16] add rsp, 8 @@ -231,13 +231,13 @@ Array.push: pop r14 ret -section .text.Array.size -Array.size: +section .text._builtin_array_size +_builtin_array_size: mov rax, [rdi + 16] ret -section .text.Array.free -Array.free: +section .text._builtin_array_free +_builtin_array_free: push rbx mov rbx, rdi mov rdi, [rdi] diff --git a/src/std.zr b/src/std.zr index 3f877c6..df3db50 100644 --- a/src/std.zr +++ b/src/std.zr @@ -9,7 +9,10 @@ func print_i64[x: I64] : I64 printf("%ld\n", x) func String.nth[s: String, n: I64] : U8 - return deref(s + n) + return _builtin_deref(s + n) + +func String.set[s: String, n: I64] : I64 + _builtin_string_set(s, n) func String.is_whitespace[c: U8] : Bool return c == ' ' || c == 10 || c == 13 || c == 9 @@ -20,10 +23,10 @@ func String.concat[a: String, b: String] : String strcat(c, b) return c -func String.find[s: String, needle: U8] : I64 +func String.find[s: String, c: U8] : I64 let s_len: I64 = strlen(s) for i in 0..s_len - if String.nth(s, i) == needle + if String.nth(s, i) == c return i return -1 @@ -57,6 +60,9 @@ func String.rev[s: String] : String String.set(out, len, 0) return out +func IO.stdin[]: Ptr + return _builtin_stdin() + func IO.read_line[]: String let buffer: String = malloc(1024) fgets(buffer, 1024, IO.stdin()) @@ -163,21 +169,39 @@ func Math.urandom[]: I64 let file: Ptr = fopen("/dev/urandom", "rb") fread(buffer, 8, 1, file) fclose(file) - let n: I64 = deref(buffer) + let n: I64 = _builtin_deref(buffer) free(buffer) return n func Array.new[] : Array return calloc(1, 24) +func Array.set[xs: Array, n: I64, x: I64] : I64 + _builtin_array_set(xs, n, x) + +func Array.push[xs: Array, x: I64] : I64 + return _builtin_array_push(xs, x) + +func Array.size[xs: Array] : I64 + return _builtin_array_size(xs) + func OS.time[] : I64 let tv: Ptr = malloc(16) gettimeofday(tv, 0) - let seconds: I64 = deref(tv) - let microseconds: I64 = deref(tv+8) + let seconds: I64 = _builtin_deref(tv) + let microseconds: I64 = _builtin_deref(tv+8) free(tv) return seconds * 1000 + microseconds / 1000 +func OS.listdir[path: String] : Array + return _builtin_listdir(path) + +func Bit.lshift[a: I64, b: I64] : I64 + return _builtin_lshift(a, b) + +func Bit.rshift[a: I64, b: I64] : I64 + return _builtin_rshift(a, b) + func Crypto.hex_encode[s: String] : String let hex_chars: String = "0123456789abcdef" let s_len: I64 = strlen(s) @@ -227,7 +251,7 @@ func Crypto.rc4[key: String, plaintext: String]: String let tmp: U8 = String.nth(S, i) String.set(S, i, String.nth(S, j)) String.set(S, j, tmp) - + let i: I64 = 0 j = 0 let plaintext_len: I64 = strlen(plaintext)