summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKai Gunger <kgunger12@gmail.com>2026-07-08 02:14:13 -0400
committerKai Gunger <kgunger12@gmail.com>2026-07-08 02:14:13 -0400
commitf7ececd879b731194f64123f746ac49b7781fc68 (patch)
treed4f3af89e3a138bbd8a9f65c81d1b7033bf81152
parentf9665d8aa06e11730a9d6e7af3f98ccefb324bbf (diff)
[tnslc] fixes for stack location when returning structs
-rw-r--r--tnslc/compile/function.tnsl22
-rwxr-xr-xtnslc/run.sh25
-rw-r--r--tnslc/test.tnsl21
3 files changed, 55 insertions, 13 deletions
diff --git a/tnslc/compile/function.tnsl b/tnslc/compile/function.tnsl
index b2356c5..19f3566 100644
--- a/tnslc/compile/function.tnsl
+++ b/tnslc/compile/function.tnsl
@@ -78,7 +78,7 @@ struct Function {
out_id.data = "# OUTPUT #\0"
int reg = 1
- int stack = 0
+ int stack = 8
/; loop (int i = 0; i < tl`.sub.count) [i++]
n = tl`.sub.get(i)
/; if (n`._type == parse.NTYPE_TYPE)
@@ -853,17 +853,20 @@ struct Function {
;/
;/
- /; _compile_call(~Scope s, ~parse.Node params, ~Function f, ~Var _self, bool mth) [Var]
+ /; _compile_call (~Scope s, ~parse.Node params, ~Function f, ~Var _self, bool mth) [Var]
Var result
~Var out
/; if (f`.outputs.count > 0)
# Generate result tmp if required
out = f`.outputs.get(0)
/; if (out`.loc < 0)
+ _printf("Making tmp!\n\0")
result = s`.mk_tmp_stack(out)
;; else
+ _printf("Grabbing copy!\n\0")
result = out`.copy()
;/
+ result._print(0)
;; else
~Struct t = self._find_literal_type(s, "void\0")
result._init(t)
@@ -911,6 +914,7 @@ struct Function {
~Var inp
Var last_stack
last_stack.loc = 0
+ last_stack.offset = s`._next_stack_slot()
/; loop (int i = 1; i !> f`.inputs.count) [i++]
int pidx = f`.inputs.count - i
inp = f`.inputs.get(pidx)
@@ -1027,7 +1031,8 @@ struct Function {
out_pos.ptr_push(pp)
;/
- out_pos.loc = last_stack.loc
+ int sloc = 0
+ out_pos.loc = sloc - 1
out_pos.offset = last_stack.offset
/; if (f`.call_padding > 0)
@@ -1045,6 +1050,7 @@ struct Function {
result.ptr_push(pp)
;/
+ ~CompBuf buf = s`.cb
result.set(s`.cb, ~out_pos)
out_pos.end()
@@ -1066,7 +1072,15 @@ struct Function {
s`.restore_caller_tmp(~handle)
/; if (result.loc < 0)
- s`.free_after(~result, true)
+ /; if (last_stack.loc !== 0)
+ s`.free_after(~result, true)
+ ;; else if (f`.call_padding > 0)
+ ~uint8 pad_num = utils.int_to_str(f`.call_padding)
+ buf`.add_c(" add rsp, \0")
+ buf`.add_c(pad_num)
+ buf`.add_c("\n\0")
+ _delete(pad_num)
+ ;/
;/
return result
diff --git a/tnslc/run.sh b/tnslc/run.sh
index 8010ab6..a8cf594 100755
--- a/tnslc/run.sh
+++ b/tnslc/run.sh
@@ -1,5 +1,26 @@
#!/bin/bash
-DIR_L=$(pwd)
+BUILD_DIR=./out/NEXT
+ARTIFACT_DIR=$BUILD_DIR/artifacts
+
+mkdir -p $BUILD_DIR
+mkdir -p $ARTIFACT_DIR
+filename=$1
+filename="${filename%.*}"
+
+./out/tnslc $filename.tnsl
+
+if [ $? -ne 0 ]; then
+ exit $?
+fi
+
+mv out.asm $ARTIFACT_DIR/$filename.asm
+
+nasm -g -f elf64 -o $ARTIFACT_DIR/$filename.o $ARTIFACT_DIR/$filename.asm
+
+if [ $? -ne 0 ]; then
+ exit $?
+fi
+
+gcc -ggdb -o $BUILD_DIR/$filename $ARTIFACT_DIR/$filename.o
-$DIR_L/out/tnslc $1
diff --git a/tnslc/test.tnsl b/tnslc/test.tnsl
index c91eee4..a130386 100644
--- a/tnslc/test.tnsl
+++ b/tnslc/test.tnsl
@@ -1,13 +1,20 @@
-struct Init {
- ~int a, b
+struct TTT {
+ int a, b, c
}
-/; method Init
+/; asdf [TTT]
+ TTT out
+ out.a = 0
+ out.b = 0
+ return out
+;/
+
+/; main (int argc, ~~uint8 argv) [int]
+
+ TTT out
+ out = asdf()
- /; set (int a)
- self.b` = a
- ;/
-
+ return out.a
;/