From d850bd17e2533179bfbc2dd6cd8d0c11a43786ba Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Sun, 14 May 2023 01:43:58 -0400 Subject: Windows C wrapping --- tnslc/c_wrap.tnsl | 45 +++++++++++++++++++++++++++-------------- tnslc/proper_calling.tnsl | 21 +++++++++++++++++++ tnslc/simpler.tnsl | 39 ----------------------------------- tnslc/tests/proper_calling.tnsl | 25 ----------------------- tnslc/tnslc.tnsl | 4 ++++ tnslc/tnslc_wrapped.tnsl | 17 +++++++++++----- 6 files changed, 67 insertions(+), 84 deletions(-) create mode 100644 tnslc/proper_calling.tnsl delete mode 100644 tnslc/simpler.tnsl delete mode 100644 tnslc/tests/proper_calling.tnsl diff --git a/tnslc/c_wrap.tnsl b/tnslc/c_wrap.tnsl index 9c0faf6..eead086 100644 --- a/tnslc/c_wrap.tnsl +++ b/tnslc/c_wrap.tnsl @@ -3,10 +3,15 @@ asm "extern realloc" asm "extern free" asm "extern printf" +{}uint8 _alert = "Alert!" +uint _stop = 0 + /; _alloc (uint size) [~void] ~void out # Mov size into proper register, and set all extras to zero - asm "mov rcx, rax" + asm "mov rax, 0" + asm "mov rbx, 0" + asm "mov rcx, r8" asm "mov rdx, 0" asm "mov rdi, 0" asm "mov rsi, 0" @@ -23,8 +28,10 @@ asm "extern printf" /; _realloc (~void ptr, uint new_size) [~void] ~void out # Mov ptr and new size into proper registers, and set all extras to zero - asm "mov rcx, rax" - asm "mov rdx, rbx" + asm "mov rax, 0" + asm "mov rbx, 0" + asm "mov rcx, r8" + asm "mov rdx, r9" asm "mov rdi, 0" asm "mov rsi, 0" asm "mov r8, 0" @@ -40,6 +47,8 @@ asm "extern printf" /; _delete (~void ptr) # setup call by clearing most values + asm "mov rax, 0" + asm "mov rbx, 0" asm "mov rcx, rax" asm "mov rdx, 0" asm "mov rdi, 0" @@ -66,45 +75,51 @@ asm "extern printf" # there's no more to do 'cause free returns nothing ;/ +/; print_alert + _printf(~_alert{0}) +;/ + struct Vector { uint el_size, num_el, dat_size, - ~void dat + ~uint8 dat } /; method Vector /; resize (uint num_el) self.dat_size = num_el - _realloc(self.dat, num_el * self.el_size) + self.dat = _realloc(self.dat, num_el * self.el_size) ;/ - /; get (uint i) [~void] + /; get (uint i) [~uint8] /; if (i !< self.num_el) return self.dat ;/ return self.dat + (i * self.el_size) ;/ - /; set (uint i, ~void data) - ~void index = self.get(i) - /; loop (i = 0; i < self.el_size) [index = index + 1; data = data + 1; i++] - index = data + /; set (uint i, ~uint8 data) + ~uint8 index = self.get(i) + /; loop (i = 0; i < self.el_size) [i++] + index` = data` + index++ + data++ ;/ ;/ - /; push (~void data) - self.num_el++ - self.set(self.num_el - 1, data) + /; push (~uint8 data) + self.set(self.num_el, data) + self.num_el = self.num_el + 1 /; if (self.num_el !< self.dat_size) - self.resize(2*self.dat_size) + self.resize(2 * self.dat_size) ;/ ;/ /; pop - self.num_el-- + self.num_el = self.num_el - 1 /; if (self.num_el !== 0 && self.num_el < self.dat_size / 4) self.resize(self.dat_size / 2) ;/ diff --git a/tnslc/proper_calling.tnsl b/tnslc/proper_calling.tnsl new file mode 100644 index 0000000..6fd5e69 --- /dev/null +++ b/tnslc/proper_calling.tnsl @@ -0,0 +1,21 @@ +struct CallMe { + int a, b +} + +/; method CallMe + /; call_two (int a, b) [int] + return a + b + self.a + self.b + ;/ + + /; call_four (int a, b, c, d) [int] + return self.call_two(a + b, b * c + d) + self.call_two(a * b + c, c + d) + ;/ + +;/ + +/; main [int] + CallMe cm + cm.a = 0 + cm.b = 0 + return cm.call_four(0, 2, 2, 0) +;/ \ No newline at end of file diff --git a/tnslc/simpler.tnsl b/tnslc/simpler.tnsl deleted file mode 100644 index 847e316..0000000 --- a/tnslc/simpler.tnsl +++ /dev/null @@ -1,39 +0,0 @@ - -{}uint8 str1 = "abcd" -{}uint8 str2 = "abcd" - -/; method Test - /; mamba [int] - return 1 - ;/ - - /; wamba [int] - return self.i + self.j + self.mamba() - ;/ -;/ - -struct Test { - int i, j, k -} - -/; 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" - - # 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" - - - /; if (argc > 8) - argc = 90 - ;/ - - - # return 3 - return str1{1} -;/ - diff --git a/tnslc/tests/proper_calling.tnsl b/tnslc/tests/proper_calling.tnsl deleted file mode 100644 index 4480b31..0000000 --- a/tnslc/tests/proper_calling.tnsl +++ /dev/null @@ -1,25 +0,0 @@ -struct CallMe { - int a, b -} - -/; method CallMe - /; call_two (int a, b) [int] - return a + b + self.a + self.b - ;/ - - /; call_three (int a, b, c) [int] - return self.call_two(a, b) + self.call_two(a, b) + c - ;/ - - /; call_four (int a, b, c, d) [int] - return self.call_three(a, b, c) + self.call_three(b, c, d) - ;/ - -;/ - -/; main [int] - CallMe cm - cm.a = 0 - cm.b = 0 - return cm.call_four(2, 0, 0, 1) -;/ \ No newline at end of file diff --git a/tnslc/tnslc.tnsl b/tnslc/tnslc.tnsl index 4804be9..1ad73f6 100644 --- a/tnslc/tnslc.tnsl +++ b/tnslc/tnslc.tnsl @@ -3510,6 +3510,10 @@ ;l = string_add(l, root.name) ;root.name = l + ;log_debug("----------------------------") + ;log_debug(string_add("START OF BLOCK: ", l)) + ;log_debug("----------------------------") + /; if (current`.exp) ;out`.hsec = string_add(out`.hsec, "global ") ;out`.hsec = string_add(out`.hsec, l) diff --git a/tnslc/tnslc_wrapped.tnsl b/tnslc/tnslc_wrapped.tnsl index 8643a01..f93c3c4 100644 --- a/tnslc/tnslc_wrapped.tnsl +++ b/tnslc/tnslc_wrapped.tnsl @@ -1,8 +1,5 @@ :include "c_wrap.tnsl" -{}uint8 _name = "Hello World!" -int _stop = 0 - /; 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 @@ -14,6 +11,16 @@ int _stop = 0 # asm "mov r8, rdi" # asm "mov r9, rsi" - _printf(~_name{0}) - return 0 + Vector vec + vec.start(1) + uint8 a = 97 + vec.push(~a) + vec.push(~a) + vec.push(~a) + vec.push(~a) + a = 0 + vec.push(~a) + _printf(vec.dat) + vec.clean() + return a ;/ \ No newline at end of file -- cgit v1.2.3