diff options
-rw-r--r-- | tnslc/c_wrap_linux.tnsl | 226 | ||||
-rw-r--r-- | tnslc/c_wrap_windows.tnsl (renamed from tnslc/c_wrap.tnsl) | 19 | ||||
-rw-r--r-- | tnslc/copy.tnsl | 9 | ||||
-rw-r--r-- | tnslc/hello.tnsl | 12 | ||||
-rw-r--r-- | tnslc/logging.tnsl | 1 | ||||
-rw-r--r-- | tnslc/path.tnsl | 0 | ||||
-rwxr-xr-x | tnslc/run.sh | 15 | ||||
-rw-r--r-- | tnslc/simple.tnsl | 47 | ||||
-rw-r--r-- | tnslc/tnslc_wrapped.tnsl | 31 | ||||
-rw-r--r-- | tnslc/tokenizer.tnsl | 13 | ||||
-rw-r--r-- | tnslc/vector_test.tnsl | 12 |
11 files changed, 299 insertions, 86 deletions
diff --git a/tnslc/c_wrap_linux.tnsl b/tnslc/c_wrap_linux.tnsl new file mode 100644 index 0000000..31037db --- /dev/null +++ b/tnslc/c_wrap_linux.tnsl @@ -0,0 +1,226 @@ +# Must be included at the top of the file +asm "extern malloc, realloc, free, printf, open, close, read, write" + +{}uint8 _alert = "Alert!\n\0" +{}uint8 _dec = "%d\n\0" +{}uint8 _ptr = "%p\n\0" + +/; _alloc (uint size) [~void] + ~void out + + asm "mov rax, rsp" + asm "xor rdx, rdx" + asm "mov rcx, 16" + asm "div rcx" + asm "sub rsp, rdx" + asm "sub rsp, 128" + + # Mov size into proper register, and set all extras to zero + asm "mov rdi, r8" + asm "mov rsi, 0" + asm "xor rax, rax" + + # Do call + asm "call malloc wrt ..plt" + + # Set out to the returned value + # (The compiler assignes spaces sequentially, and we have a uint in r8) + asm "mov r9, rax" + + return out +;/ + +/; _realloc (~void ptr, uint new_size) [~void] + ~void out + + asm "mov rax, rsp" + asm "xor rdx, rdx" + asm "mov rcx, 16" + asm "div rcx" + asm "sub rsp, rdx" + asm "sub rsp, 128" + + # Mov ptr and new size into proper registers, and set all extras to zero + asm "mov rdi, r8" + asm "mov rsi, r9" + asm "xor rax, rax" + + # Do call + asm "call realloc wrt ..plt" + + # Set out to the returned value + # (The compiler assignes spaces sequentially. We have a ptr in r8, and a uint in r9) + asm "mov r10, rax" + + return out +;/ + +/; _delete (~void ptr) + + asm "mov rax, rsp" + asm "xor rdx, rdx" + asm "mov rcx, 16" + asm "div rcx" + asm "sub rsp, rdx" + asm "sub rsp, 128" + + # setup call by clearing most values + asm "mov rdi, r8" + asm "mov rsi, 0" + asm "xor rax, rax" + + # do call + asm "call free wrt ..plt" + + # there's no more to do 'cause free returns nothing +;/ + +/; _printf (~void str) + + asm "mov rax, rsp" + asm "xor rdx, rdx" + asm "mov rcx, 16" + asm "div rcx" + asm "sub rsp, rdx" + asm "sub rsp, 128" + + # setup call by clearing most values + asm "mov rdi, r8" + asm "xor rsi, rsi" + asm "xor rax, rax" + + # do call + asm "call printf wrt ..plt" + + # there's no more to do 'cause printf returns nothing +;/ + +/; _print_num (~void str, int num) + + asm "mov rax, rsp" + asm "xor rdx, rdx" + asm "mov rcx, 16" + asm "div rcx" + asm "sub rsp, rdx" + asm "sub rsp, 128" + + # setup call by clearing most values + asm "mov rdi, r8" + asm "mov rsi, r9" + asm "xor rax, rax" + + # do call + asm "call printf wrt ..plt" + + # there's no more to do 'cause printf returns nothing +;/ + + +# Create file for writing (overwrite) +/; _create_file (~void name) [~void] + ~void out + + asm "mov rax, rsp" + asm "xor rdx, rdx" + asm "mov rcx, 16" + asm "div rcx" + asm "sub rsp, rdx" + asm "sub rsp, 128" + + # Do call + asm "mov rdi, r8" + asm "mov rsi, 0q1102" + asm "mov rdx, 0q700" + asm "call open wrt ..plt" + + # Set out to the returned value + # (The compiler assignes spaces sequentially. We have a ptr in r8) + asm "mov r9, rax" + + return out +;/ + +# Open file for reading or writing (no overwrite) +/; _open_file (~void name) [~void] + ~void out + + asm "mov rax, rsp" + asm "xor rdx, rdx" + asm "mov rcx, 16" + asm "div rcx" + asm "sub rsp, rdx" + asm "sub rsp, 128" + + # Do syscall + asm "mov rdi, r8" + asm "mov rsi, 2" + asm "mov rdx, 0q700" + asm "call open wrt ..plt" + + # Set out to the returned value + # (The compiler assignes spaces sequentially. We have a ptr in r8, and a uint in r9) + asm "mov r9, rax" + + return out +;/ + +/; _close_file (~void handle) + + asm "mov rax, rsp" + asm "xor rdx, rdx" + asm "mov rcx, 16" + asm "div rcx" + asm "sub rsp, rdx" + asm "sub rsp, 128" + + # Do syscall + asm "mov rdi, r8" + asm "call close wrt ..plt" +;/ + +/; _read_byte (~void handle, ~uint8 byte) [int] + int out + + asm "mov rax, rsp" + asm "xor rdx, rdx" + asm "mov rcx, 16" + asm "div rcx" + asm "sub rsp, rdx" + asm "sub rsp, 128" + + # Do syscall + asm "mov rdi, r8" # handle + asm "mov rsi, r9" # buffer + asm "mov rdx, 1" # one byte + asm "call read wrt ..plt" + + # return number of bytes read + asm "mov r10, rax" + + return out +;/ + +/; _write_byte (~void handle, ~uint8 byte) [int] + int out + + asm "mov rax, rsp" + asm "xor rdx, rdx" + asm "mov rcx, 16" + asm "div rcx" + asm "sub rsp, rdx" + asm "sub rsp, 128" + + # Do syscall + asm "mov rdi, r8" # handle + asm "mov rsi, r9" # buffer + asm "mov rdx, 1" # one byte + asm "call write wrt ..plt" + + asm "mov r10, rax" + + return out +;/ + +/; print_alert + _printf(~_alert{0}) +;/ diff --git a/tnslc/c_wrap.tnsl b/tnslc/c_wrap_windows.tnsl index ac516da..577ca25 100644 --- a/tnslc/c_wrap.tnsl +++ b/tnslc/c_wrap_windows.tnsl @@ -222,8 +222,11 @@ asm "extern CloseHandle" asm "add rsp, 32" ;/ -/; _read_byte (~void handle, ~uint8 byte, ~int read) [bool] - int out +/; _read_byte (~void handle, ~uint8 byte) [int] + ~int out + int i = 0 + out = ~i + asm "mov rax, rsp" asm "xor rdx, rdx" @@ -245,13 +248,13 @@ asm "extern CloseHandle" asm "call ReadFile" - asm "mov r11, rax" - - return out + return out` ;/ /; _write_byte (~void handle, ~uint8 byte) [int] - int out + ~int out + int i = 0 + out = ~i asm "mov rax, rsp" asm "xor rdx, rdx" @@ -273,9 +276,7 @@ asm "extern CloseHandle" asm "call WriteFile" - asm "mov r10, rax" - - return out + return out` ;/ /; print_alert diff --git a/tnslc/copy.tnsl b/tnslc/copy.tnsl index 636a773..0050d1a 100644 --- a/tnslc/copy.tnsl +++ b/tnslc/copy.tnsl @@ -1,4 +1,4 @@ -:include "c_wrap.tnsl" +:include "c_wrap_linux.tnsl" # :include "utils.tnsl" @@ -7,8 +7,8 @@ # Proof of concept copy program /; main (int argc, ~~uint8 argv) [int] - asm "mov r8, rcx" - asm "mov r9, rdx" + asm "mov r8, rdi" + asm "mov r9, rsi" /; if (argc < 3) _printf(~wrong_args{0}) @@ -23,9 +23,8 @@ uint8 buf = 0 int read_count = 0 - int tries = 0 /; loop - _read_byte(read_handle, ~buf, ~read_count) + read_count = _read_byte(read_handle, ~buf) /; if (read_count == 0) break ;/ diff --git a/tnslc/hello.tnsl b/tnslc/hello.tnsl index a2bdc68..6a6313a 100644 --- a/tnslc/hello.tnsl +++ b/tnslc/hello.tnsl @@ -1,4 +1,4 @@ -:include "c_wrap.tnsl" +:include "c_wrap_linux.tnsl" {}uint8 star = "*\0" {}uint8 space = " \0" @@ -19,15 +19,15 @@ /; main (int argc, ~~uint8 argv) [int] # On windows, the first two arguments are passed in RCX and RDX, so we need to # update their positions here or else tnsl will have garbage values in r8 and r9 - asm "mov r8, rcx" - asm "mov r9, rdx" + # asm "mov r8, rcx" + # asm "mov r9, rdx" # If on linux, you would use rdi and rsi instead of rcx and rdx, respectively # simply comment out the bove asm, and uncomment the below lines - # asm "mov r8, rdi" - # asm "mov r9, rsi" + asm "mov r8, rdi" + asm "mov r9, rsi" print_triangle(argc) return 0 -;/
\ No newline at end of file +;/ diff --git a/tnslc/logging.tnsl b/tnslc/logging.tnsl index 238f3be..2de326a 100644 --- a/tnslc/logging.tnsl +++ b/tnslc/logging.tnsl @@ -72,3 +72,4 @@ int log_mode = 1 _print_num(~_dec{0}, i) _printf(~_log_nl{0}) ;/ + diff --git a/tnslc/path.tnsl b/tnslc/path.tnsl deleted file mode 100644 index e69de29..0000000 --- a/tnslc/path.tnsl +++ /dev/null diff --git a/tnslc/run.sh b/tnslc/run.sh index c1b19e5..9957e15 100755 --- a/tnslc/run.sh +++ b/tnslc/run.sh @@ -1,3 +1,18 @@ #!/bin/bash +BUILD_DIR="./build" +ARTIFACTS_DIR="$BUILD_DIR/artifacts" + +if [ ! -d $BUILD_DIR ]; then + mkdir $BUILD_DIR +fi + +if [ ! -d $ARTIFACTS_DIR ]; then + mkdir $ARTIFACTS_DIR +fi + ../tint -in tnslc.tnsl -flags "$1" +mv -f "$1.asm" "$ARTIFACTS_DIR/$1.asm" +nasm -f elf64 -o "$ARTIFACTS_DIR/$1.o" "$ARTIFACTS_DIR/$1.asm" +clang -o "./build/${1%.*}" "$ARTIFACTS_DIR/$1.o" + diff --git a/tnslc/simple.tnsl b/tnslc/simple.tnsl index 90ee758..75b91bd 100644 --- a/tnslc/simple.tnsl +++ b/tnslc/simple.tnsl @@ -1,36 +1,29 @@ # No longer simple -:include "c_wrap.tnsl" +:include "c_wrap_linux.tnsl" -enum LOL [int] { - A = 1, - B = 2, - C = 4 -} - -struct Lolbert { - int a, b, c, +{}uint8 str = "Hello copy!\n\0" - ~uint8 stuff +struct Copier { + ~uint8 h8r } -/; lolbert1 (~Lolbert l, uint8 check) [bool] - return l`.a == check -;/ +/; method Copier + /; copy [Copier] + Copier out + out.h8r = self.h8r + return out + ;/ -/; main [int] - Lolbert lol - lol.a = 1 - lol.b = 4 - lol.c = 3 - lol.stuff = _alloc(2) - lol.stuff{0} = 2 - lol.stuff{1} = 1 +;/ - /; if (lolbert1(~lol, lol.stuff{1}) && lolbert1(~lol, lol.stuff{0})) - _delete(lol.stuff) - return lol.a + lol.b - ;/ - _delete(lol.stuff) +/; main + Copier a + a.h8r = ~str{0} + ~Copier b + b = ~a + Copier c = b`.copy() + _printf(c.h8r) return 0 -;/
\ No newline at end of file +;/ + diff --git a/tnslc/tnslc_wrapped.tnsl b/tnslc/tnslc_wrapped.tnsl index e82581a..5bffcb8 100644 --- a/tnslc/tnslc_wrapped.tnsl +++ b/tnslc/tnslc_wrapped.tnsl @@ -1,34 +1,11 @@ -:include "c_wrap.tnsl" +:include "c_wrap_linux.tnsl" :include "logging.tnsl" -:include "vector.tnsl" :include "utils.tnsl" +:include "vector.tnsl" :include "tokenizer.tnsl" :include "compiler.tnsl" -{}uint8 m_usage = "Usage: tnslc [file to compile] [output asm]\n\0" - /; main (int argc, ~~uint8 argv) [int] - asm "mov r8, rcx" - asm "mov r9, rdx" - - /; if (argc < 3) - _printf(~m_usage{0}) - return 1 - ;/ - - Path in - Path out - - in.start(argv{0}) - out.start(argv{0}) - - in.relative_file(argv{1}) - out.relative_file(argv{2}) - - compile(in, out) - - in._del() - out._del() - - return 0 + + return 0 ;/ diff --git a/tnslc/tokenizer.tnsl b/tnslc/tokenizer.tnsl index 09509f8..7a15ade 100644 --- a/tnslc/tokenizer.tnsl +++ b/tnslc/tokenizer.tnsl @@ -193,7 +193,7 @@ struct Token { uint8 buf = 0 int read_count = 0 /; loop - _read_byte(file_in, ~buf, ~read_count) + read_count = _read_byte(file_in, ~buf) /; if (buf == '\n' || read_count == 0) break ;/ @@ -209,7 +209,7 @@ struct Token { read_count = 0 /; loop - _read_byte(file_in, ~buf, ~read_count) + read_count = _read_byte(file_in, ~buf) /; if (read_count == 0) break @@ -217,10 +217,11 @@ struct Token { /; if (buf == '\\') tmp.append(buf) - read_count = 0 - _read_byte(file_in, ~buf, ~read_count) + read_count = _read_byte(file_in, ~buf) column`++ - tmp.append(buf) + /; if (read_count !== 0) + tmp.append(buf) + ;/ ;; else if (buf == first) tmp.append(buf) break @@ -261,7 +262,7 @@ struct Token { # Read loop. /; loop [column++] - _read_byte(file_in, ~buf, ~read_count) + read_count = _read_byte(file_in, ~buf) /; if (read_count == 0) break ;/ diff --git a/tnslc/vector_test.tnsl b/tnslc/vector_test.tnsl index c74ddf9..5606d80 100644 --- a/tnslc/vector_test.tnsl +++ b/tnslc/vector_test.tnsl @@ -1,16 +1,16 @@ -:include "c_wrap.tnsl" +:include "c_wrap_linux.tnsl" :include "vector.tnsl" /; main (int argc, ~~uint argv) [int] # On windows, the first two arguments are passed in RCX and RDX, so we need to # update their positions here or else tnsl will have garbage values in r8 and r9 - asm "mov r8, rcx" - asm "mov r9, rdx" + # asm "mov r8, rcx" + # asm "mov r9, rdx" # If on linux, you would use rdi and rsi instead of rcx and rdx, respectively # simply comment out the bove asm, and uncomment the below lines - # asm "mov r8, rdi" - # asm "mov r9, rsi" + asm "mov r8, rdi" + asm "mov r9, rsi" Vector vec vec.start(1) @@ -33,4 +33,4 @@ vec._del() return 0 -;/
\ No newline at end of file +;/ |