summaryrefslogtreecommitdiff
path: root/tnslc
diff options
context:
space:
mode:
authorKyle Gunger <kgunger12@gmail.com>2023-07-26 00:48:43 -0400
committerKyle Gunger <kgunger12@gmail.com>2023-07-26 00:48:43 -0400
commit7f0e6ea654037eda11c2e84946f99bad3f1ab6e4 (patch)
tree88aae0b23c8cdcaaa4a80daf44f87e1969b71de4 /tnslc
parentceaeb8df4d9bf9b518239ea623d813add5a71072 (diff)
Division fixes
Diffstat (limited to 'tnslc')
-rw-r--r--tnslc/c_wrap.tnsl82
-rw-r--r--tnslc/simple.tnsl28
-rw-r--r--tnslc/tnslc.tnsl9
-rw-r--r--tnslc/tokenizer.tnsl72
-rw-r--r--tnslc/vector.tnsl8
-rw-r--r--tnslc/vector_test.tnsl25
6 files changed, 170 insertions, 54 deletions
diff --git a/tnslc/c_wrap.tnsl b/tnslc/c_wrap.tnsl
index c0fa91c..d44e948 100644
--- a/tnslc/c_wrap.tnsl
+++ b/tnslc/c_wrap.tnsl
@@ -12,17 +12,24 @@ asm "extern CloseHandle"
{}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, 32"
+ asm "div rcx"
+ asm "sub rsp, rdx"
+ asm "sub rsp, 32"
+
# Mov size into proper register, and set all extras to zero
asm "mov rcx, r8"
asm "mov rdx, 0"
asm "mov r8, 0"
asm "mov r9, 0"
-
- asm "sub rsp, 32"
+
asm "call malloc"
# Set out to the returned value
# (The compiler assignes spaces sequentially, and we have a uint in r8)
@@ -33,13 +40,19 @@ asm "extern CloseHandle"
/; _realloc (~void ptr, uint new_size) [~void]
~void out
+ asm "mov rax, rsp"
+ asm "xor rdx, rdx"
+ asm "mov rcx, 32"
+ asm "div rcx"
+ asm "sub rsp, rdx"
+ asm "sub rsp, 32"
+
# Mov ptr and new size into proper registers, and set all extras to zero
asm "mov rcx, r8"
asm "mov rdx, r9"
asm "mov r8, 0"
asm "mov r9, 0"
# Do call
- asm "sub rsp, 32"
asm "call realloc"
# Set out to the returned value
# (The compiler assignes spaces sequentially. We have a ptr in r8, and a uint in r9)
@@ -49,35 +62,57 @@ asm "extern CloseHandle"
/; _delete (~void ptr)
+ asm "mov rax, rsp"
+ asm "xor rdx, rdx"
+ asm "mov rcx, 32"
+ asm "div rcx"
+ asm "sub rsp, rdx"
+ asm "sub rsp, 32"
+
# setup call by clearing most values
asm "mov rcx, r8"
asm "mov rdx, 0"
asm "mov r8, 0"
asm "mov r9, 0"
# do call
- asm "sub rsp, 32"
asm "call free"
# there's no more to do 'cause free returns nothing
;/
/; _printf (~void str)
+ asm "mov rax, rsp"
+ asm "xor rdx, rdx"
+ asm "mov rcx, 32"
+ asm "div rcx"
+ asm "sub rsp, rdx"
+ asm "push qword 0"
+ asm "push qword 0"
+ asm "push qword 0"
+ asm "push qword 0"
+
# setup call by clearing most values
asm "mov rcx, r8"
asm "mov rdx, 0"
asm "mov r8, 0"
asm "mov r9, 0"
# do call
- asm "push qword 0"
- asm "push qword 0"
- asm "push qword 0"
- asm "push qword 0"
asm "call printf"
# 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, 32"
+ asm "div rcx"
+ asm "sub rsp, rdx"
+ asm "push qword 0"
+ asm "push qword 0"
+ asm "push qword 0"
+ asm "push qword 0"
+
# setup call by clearing most values
asm "mov rcx, r8"
asm "mov rdx, r9"
@@ -86,7 +121,6 @@ asm "extern CloseHandle"
asm "mov r8, 0"
asm "mov r9, 0"
# do call
- asm "sub rsp, 32"
asm "call printf"
# there's no more to do 'cause printf returns nothing
;/
@@ -96,6 +130,12 @@ asm "extern CloseHandle"
/; _create_file (~void name) [~void]
~void out
+ asm "mov rax, rsp"
+ asm "xor rdx, rdx"
+ asm "mov rcx, 32"
+ asm "div rcx"
+ asm "sub rsp, rdx"
+
asm "mov rcx, r8" # lpFileName
asm "mov rdx, 1" # dwDesiredAccess
asm "shl rdx, 28" # (GENERIC_READ 1 << 31 | GENERIC_WRITE 1 << 30)
@@ -126,6 +166,12 @@ asm "extern CloseHandle"
/; _open_file (~void name) [~void]
~void out
+ asm "mov rax, rsp"
+ asm "xor rdx, rdx"
+ asm "mov rcx, 32"
+ asm "div rcx"
+ asm "sub rsp, rdx"
+
asm "mov rcx, r8" # lpFileName
asm "mov rdx, 1" # dwDesiredAccess
asm "shl rdx, 28" # (GENERIC_READ 1 << 31 | GENERIC_WRITE 1 << 30)
@@ -154,6 +200,12 @@ asm "extern CloseHandle"
/; _close_file (~void handle)
+ asm "mov rax, rsp"
+ asm "xor rdx, rdx"
+ asm "mov rcx, 32"
+ asm "div rcx"
+ asm "sub rsp, rdx"
+
asm "mov rcx, r8" # handle
asm "mov rdx, 0"
asm "mov r8, 0"
@@ -173,6 +225,12 @@ asm "extern CloseHandle"
/; _read_byte (~void handle, ~uint8 byte, ~int read) [int]
int out
+ asm "mov rax, rsp"
+ asm "xor rdx, rdx"
+ asm "mov rcx, 32"
+ asm "div rcx"
+ asm "sub rsp, rdx"
+
asm "mov rcx, r8" # handle
asm "mov rdx, r9" # buffer
asm "mov r8, 1" # one byte
@@ -195,6 +253,12 @@ asm "extern CloseHandle"
/; _write_byte (~void handle, ~uint8 byte) [int]
int out
+ asm "mov rax, rsp"
+ asm "xor rdx, rdx"
+ asm "mov rcx, 32"
+ asm "div rcx"
+ asm "sub rsp, rdx"
+
asm "mov rcx, r8" # handle
asm "mov rdx, r9" # buffer
asm "mov r8, 1" # one byte
diff --git a/tnslc/simple.tnsl b/tnslc/simple.tnsl
index 1541df2..c0c1821 100644
--- a/tnslc/simple.tnsl
+++ b/tnslc/simple.tnsl
@@ -1,7 +1,20 @@
-:include "c_wrap.tnsl"
-:include "logging.tnsl"
+# testing file to try to reproduce bugs as best as possible
-{}uint8 eee = "eee\0"
+# 8x4 bytes
+struct AddressError {
+ uint a, b, c, d
+}
+
+/; method AddressError
+ /; if_check [bool]
+ /; if (self.a < 0 && self.b !< self.d / 4)
+ return true
+ ;; else if (self.c == 0)
+ return false
+ ;/
+ return self.d != 1
+ ;/
+;/
/; main (int argc, ~~uint argv) [int]
# On windows, the first two arguments are passed in RCX and RDX, so we need to
@@ -9,9 +22,12 @@
asm "mov r8, rcx"
asm "mov r9, rdx"
- log_err(~eee{0})
- log_info(~eee{0})
- log_debug(~eee{0})
+ AddressError ae
+ ae.a = ae.b = ae.c = ae.d = 0
+
+ /; if (ae.if_check())
+ return 1
+ ;/
return 0
;/
diff --git a/tnslc/tnslc.tnsl b/tnslc/tnslc.tnsl
index 5c1e6f9..772897e 100644
--- a/tnslc/tnslc.tnsl
+++ b/tnslc/tnslc.tnsl
@@ -1258,8 +1258,11 @@
;data`.csec = string_join( {
data`.csec,
"\tmov ", get_reg(0, sz), ", ", self.norm_loc(sz), "\n",
- "\tmov ", get_reg(3, sz), ", ", v.norm_loc(sz), "\n",
- "\t", op, " ", get_reg(3, sz), "\n",
+ "\txor rdx, rdx\n",
+ "\tpush rsi\n",
+ "\tmov ", get_reg(4, sz), ", ", v.norm_loc(sz), "\n",
+ "\t", op, " ", get_reg(4, sz), "\n",
+ "\tpop rsi\n",
"\tmov ", self.norm_loc(sz), ", ", get_reg(keep, sz), "\n"
}, "")
;/
@@ -1784,7 +1787,7 @@
/; new_tmp_var (Type t, ~CompData out) [Variable]
;Variable new = {"##tmp", t, 0, 0}
;new.loc_type = LOCATION.STACK
- ;new.location = self.get_full_tmp_stack() + self.tmp + new.norm_size()
+ ;new.location = self.get_full_tmp_stack() + new.norm_size()
;self.tmp = self.tmp + new.norm_size()
;out`.csec = string_join( {
out`.csec,
diff --git a/tnslc/tokenizer.tnsl b/tnslc/tokenizer.tnsl
index ada1b8e..ae52468 100644
--- a/tnslc/tokenizer.tnsl
+++ b/tnslc/tokenizer.tnsl
@@ -141,6 +141,10 @@ struct Token {
;; else if (contains_char(~AUGMENTS, tok.data{0}))
return TOKEN_TYPE.AUGMENT
;/
+ ;; else if (in_csv(~CSV_KEYWORDS, tok.data))
+ return TOKEN_TYPE.KEYWORD
+ ;; else if (in_csv(~CSV_KEYTYPES, tok.data))
+ return TOKEN_TYPE.KEYTYPE
;; else if (tok_literal(tok) == true)
return TOKEN_TYPE.LITERAL
;/
@@ -158,9 +162,17 @@ struct Token {
tok.append(c)
uint type_after = get_tok_type(tok)
tok.pop()
- log_one_nl('g')
- bool a = type_before !== TOKEN_TYPE.DEFWORD && type_after == TOKEN_TYPE.DEFWORD
+ log_one('g')
+ bool a = type_before !== TOKEN_TYPE.DEFWORD && type_before != TOKEN_TYPE.KEYTYPE && type_before != TOKEN_TYPE.KEYWORD && type_after == TOKEN_TYPE.DEFWORD
+ log_one(' ')
+ log_one('[')
+ log_one(a)
+ log_one(']')
bool b = type_after !== TOKEN_TYPE.LITERAL && is_whitespace(c) == true
+ log_one(' ')
+ log_one('[')
+ log_one(b)
+ log_one_nl(']')
return a || b
;/
@@ -178,31 +190,41 @@ struct Token {
/; if (read_count == 0)
break
;/
+
log_one_nl('b')
- # /; if (break_token(tmp, buf) == true)
- # log_one_nl('c')
- # /; if (tmp._len() > 0)
- # log_one_nl('d')
- # print_token(tmp, file_out)
- # ;/
- #
- # tmp._del()
- # tmp.start()
-#
- # /; if (is_whitespace(buf) == false)
- # log_one_nl('e')
- # tmp.append(buf)
- # ;; else if (buf == WHITESPACE{2})
- # log_one_nl('f')
- # tmp.append(WHITESPACE{2})
- # print_token(tmp, file_out)
- # tmp._del()
- # tmp.start()
- # ;/
- # log_one_nl('c')
- # ;; else
+
+ /; if (buf == '#')
+ /; loop (_read_byte(file_in, ~buf, ~read_count))
+ /; if (buf == '\n' || read_count == 0)
+ break
+ ;/
+ ;/
+
+ ;; else if (break_token(tmp, buf) == true)
+ log_one_nl('c')
+ /; if (tmp._len() > 0)
+ log_one_nl('d')
+ print_token(tmp, file_out)
+ ;/
+ tmp._del()
+ tmp.start()
+ /; if (is_whitespace(buf) == false)
+ log_one_nl('e')
+ tmp.append(buf)
+ ;; else if (buf == WHITESPACE{2})
+ log_one_nl('f')
+ tmp.append(WHITESPACE{2})
+ print_token(tmp, file_out)
+ tmp._del()
+ tmp.start()
+ ;/
+ log_one_nl('c')
+
+ ;; else
tmp.append(buf)
- # ;/
+
+ ;/
+
log_one_nl('b')
read_count = 0
;/
diff --git a/tnslc/vector.tnsl b/tnslc/vector.tnsl
index ecb52e1..f7ca9bc 100644
--- a/tnslc/vector.tnsl
+++ b/tnslc/vector.tnsl
@@ -10,7 +10,9 @@ struct Vector {
/; resize (uint num_el)
self.dat_size = num_el
+ _print_num(~_ptr{0}, ~self.dat)
self.dat = _realloc(self.dat, num_el * self.el_size)
+ _print_num(~_ptr{0}, ~self.dat)
;/
/; get (uint i) [~uint8]
@@ -39,9 +41,11 @@ struct Vector {
/; pop
self.num_el--
- /; if (self.num_el !== 0 && self.num_el < self.dat_size / 4)
+ /; if (self.num_el > 0 && self.num_el < self.dat_size / 4)
self.resize(self.dat_size / 2)
- ;/
+ ;; else if (self.num_el < 0)
+ self.num_el = 0
+ ;/
;/
/; remove (int index)
diff --git a/tnslc/vector_test.tnsl b/tnslc/vector_test.tnsl
index bfb41cc..ee4a25c 100644
--- a/tnslc/vector_test.tnsl
+++ b/tnslc/vector_test.tnsl
@@ -14,15 +14,22 @@
Vector vec
vec.start(1)
- uint8 a = _alert{0}
- vec.push(~a)
- vec.push(~a)
- a = _alert{len _alert - 2}
- vec.push(~a)
- a = 0
- vec.push(~a)
- _printf(vec.get(0))
- _printf(vec.get(0))
+
+ uint8 buf = 0
+ /; loop (int i = 0; i < 20) [i++]
+ buf = 'A' + i
+ vec.push(~buf)
+ ;/
+
+ buf = 0
+ vec.push(~buf)
+
+ _printf(vec.dat)
+
+ /; loop (int i = 0; i < 21) [i++]
+ vec.pop()
+ ;/
+
vec.clean()
return 0
;/ \ No newline at end of file