From 8ffb50a4bc61b51952aa78da0c6e1f002f09e1be Mon Sep 17 00:00:00 2001 From: Kai Gunger Date: Wed, 6 May 2026 04:52:02 -0400 Subject: [tnslc] better calling, issue with member setting on refs --- tnslc/compile/function.tnsl | 128 ++++++++++++++++++++++++++++++++------------ tnslc/compile/module.tnsl | 1 + tnslc/compile/var.tnsl | 6 ++- tnslc/test.tnsl | 21 +++----- 4 files changed, 108 insertions(+), 48 deletions(-) diff --git a/tnslc/compile/function.tnsl b/tnslc/compile/function.tnsl index 90eef37..56fb712 100644 --- a/tnslc/compile/function.tnsl +++ b/tnslc/compile/function.tnsl @@ -37,12 +37,6 @@ struct Function { p.loc = reg reg++ self.inputs.push(~p) - _printf("Generated self:\n\0") - p._print(0) - ;; else - _printf("Did not generate self for \"\0") - _printf(self.name) - _printf("\"\n\0") ;/ /; if (dl == NULL) @@ -77,8 +71,8 @@ struct Function { /; _resolve_tlist (~Module parent, ~parse.Node tl) ~parse.Node n - parse.Node dummy - dummy.data = "### OUTPUT ###\0" + parse.Node out_id + out_id.data = "# OUTPUT #\0" int reg = 1 int stack = 0 @@ -87,8 +81,10 @@ struct Function { /; if (n`._type == parse.NTYPE_TYPE) # TODO: also wrong Var r - r.init(n, ~dummy) + r.init(n, ~out_id) r._resolve_type(parent) + r._id = NULL + r._tn = NULL /; if (r.regable() == true && reg < 7) r.loc = reg @@ -191,6 +187,7 @@ struct Function { /; _call_label [~uint8] Scope tmp + self.mod`._print(0) tmp.init(self.mod, NULL, self.name) ~uint8 out = tmp.base_label() tmp.end() @@ -489,12 +486,16 @@ struct Function { ;/ /; _compile_call(~Scope s, ~parse.Node params, ~Function f, ~Var _self, bool mth) [Var] - # Generate result tmp if required Var result ~Var out /; if (f`.outputs.count > 0) + # Generate result tmp if required out = f`.outputs.get(0) - result = s`.mk_tmp(out) + /; if (out`.loc < 0) + result = s`.mk_tmp_stack(out) + ;; else + result = out`.copy() + ;/ ;; else ~Struct t = self._find_literal_type(s, "void\0") result._init(t) @@ -509,6 +510,14 @@ struct Function { param_count++ ;/ + /; if (params`.sub.count == 1) + ~parse.Node nn + nn = params`.sub.get(0) + /; if (nn`._type == parse.NTYPE_VALUE && nn`.sub.count == 0) + param_count = param_count - 1 + ;/ + ;/ + /; if (param_count !== f`.inputs.count) ~uint8 blab = s`.base_label() _printf("ERROR: Failed to call function \"\0") @@ -528,6 +537,7 @@ struct Function { utils.Vector tmps tmps.init(len result) tmps._grow(param_count) + tmps.count = param_count # Create all stack based tmps ~Var inp @@ -548,8 +558,7 @@ struct Function { to_set.ptr_push(ptrc) ;/ - ~Var tmp = tmps.get(pidx) - tmp` = to_set + tmps.replace(pidx, ~to_set) last_stack = to_set ;/ ;/ @@ -571,8 +580,7 @@ struct Function { to_set.ptr_push(ptrc) ;/ - ~Var tmp = tmps.get(i) - tmp` = to_set + tmps.replace(i, ~to_set) last_reg = to_set ;/ ;/ @@ -581,7 +589,10 @@ struct Function { int use_self = 0 /; if (mth == true) inp = tmps.get(0) - inp`.set(s`.cb, _self) + + Var _sptr = _self`.take_ptr(s`.cb, 1) + inp`.set(s`.cb, ~_sptr) + _sptr.end() use_self++ ;/ @@ -598,17 +609,26 @@ struct Function { ;/ param.end() ;/ - + # Move all register parameters into registers and free the stack space ~Var ptmp /; loop (int i = 0; i < f`.inputs.count) [i++] inp = f`.inputs.get(i) /; if (inp`.loc > 0) ptmp = tmps.get(i) - inp`.set(s`.cb, ptmp) + /; if (inp`.is_ref() == true) + Var to_set = inp`.take_ptr(s`.cb, 1) + to_set.set(s`.cb, ptmp) + to_set.end() + ;; else + inp`.set(s`.cb, ptmp) + ;/ ;/ ;/ - s`.free_after(~last_stack, true) + + /; if (last_stack.loc !== 0) + s`.free_after(~last_stack, true) + ;/ # Pad out the stack for return if required ~CompBuf buf = s`.cb @@ -629,16 +649,18 @@ struct Function { # Generate result variable and move into the result if required /; if (f`.outputs.count > 0) - Var out_pos = out`.copy() - out_pos.loc = last_stack.loc - out_pos.offset = last_stack.offset - /; if (f`.call_padding > 0) - int offset = out_pos.offset - offset = offset - f`.call_padding - out_pos.offset = offset + /; if (out`.loc < 0) + Var out_pos = out`.copy() + out_pos.loc = last_stack.loc + out_pos.offset = last_stack.offset + /; if (f`.call_padding > 0) + int offset = out_pos.offset + offset = offset - f`.call_padding + out_pos.offset = offset + ;/ + result.set(s`.cb, ~out_pos) + out_pos.end() ;/ - result.set(s`.cb, ~out_pos) - out_pos.end() ;/ # Free all in tmp vector @@ -651,7 +673,10 @@ struct Function { # Restore tmps in regs s`.restore_caller_tmp(~handle) - s`.free_after(~result, true) + /; if (result.loc < 0) + s`.free_after(~result, true) + ;/ + return result ;/ @@ -798,6 +823,8 @@ struct Function { # Do call result = self._compile_call(s, sub, to_call, NULL, false) + v`.end() + v`.count = 0 ;; else if (result.loc == 0) # Exit @@ -830,7 +857,7 @@ struct Function { ;/ self._compile_chain_r_post(s, post, ~result) ;/ - + return result ;/ @@ -1017,11 +1044,46 @@ struct Function { # If we are setting /; if (utils.strcmp(n`.data, "=\0") == true) + Var lhs = self._compile_value(s, lhn) + + /; if (s`.is_tmp(~lhs) == true) + s`.free_after(~lhs, true) + ;/ + + # TODO: move lhs maybe if it is in rdi + /; if (lhs.loc > 0 && lhs.loc < 11) + Var tmp = s`.mk_tmp(~lhs) + + bool was_ref = false + /; if (lhs.is_ref() == true) + was_ref = true + int32 ptr = 0 + ptr = ptr - 1 + tmp.ptr_pop() + lhs.ptr_pop() + tmp.ptr_push(ptr) + lhs.ptr_push(ptr) + ;/ + + tmp.set(s`.cb, ~lhs) + lhs.end() + lhs = tmp + + /; if (was_ref == true) + lhs.ptr_pop() + lhs.ptr_push(0) + ;/ + ;/ + Var rhs = self._compile_value(s, rhn) - # TODO: move rhs maybe if it is in rdi - Var lhs = self._compile_set(s, lhn) lhs.set(s`.cb, ~rhs) + + # Cleanup rhs + /; if (s`.is_tmp(~rhs) == true) + s`.free_to(~rhs, true) + ;/ rhs.end() + return lhs ;/ @@ -1158,7 +1220,7 @@ struct Function { Var out = self._compile_chain_r_base(s, n, ~tmp) /; if (tmp.count !== 0) tmp.end() - ;; else if (out.loc !== 0) + ;; else return out ;/ diff --git a/tnslc/compile/module.tnsl b/tnslc/compile/module.tnsl index 9de8041..86ede96 100644 --- a/tnslc/compile/module.tnsl +++ b/tnslc/compile/module.tnsl @@ -214,6 +214,7 @@ struct Module { f.init(sub) # Make function aware that it is in a method block f.m = true + f.mod = ~self self.funcs.push(~f) ;/ ;/ diff --git a/tnslc/compile/var.tnsl b/tnslc/compile/var.tnsl index 477a4fb..9c7c042 100644 --- a/tnslc/compile/var.tnsl +++ b/tnslc/compile/var.tnsl @@ -1183,7 +1183,7 @@ struct Var { # - If builtin then move based on size (byte, word, dword, qword) # - If pointer then move qword # - If struct then move via rep movsb - + /; if (self.is_struct() == true) # Struct set self._set_struct(buf, other) @@ -1252,7 +1252,7 @@ struct Var { /; if (vcpy.is_ptrc(i, 0) == false) i = vcpy.ptrc.count ;; else - set++ + idx++ ;/ ;/ @@ -1276,6 +1276,8 @@ struct Var { vcpy.loc = reg vcpy.offset = 0 + vcpy.ptrc.end() + vcpy.ptrc.init(4) vcpy.ptr_push(set) ;/ diff --git a/tnslc/test.tnsl b/tnslc/test.tnsl index 66091f7..58ba226 100644 --- a/tnslc/test.tnsl +++ b/tnslc/test.tnsl @@ -4,21 +4,16 @@ struct A { } /; method A - /; get_a [int] - return self.a + /; add_one + int a = self.a + 1 ;/ ;/ -/; module mod - /; func1 [int] - return 4 - ;/ -;/ - -/; main (int argc, ~~uint8 argv) [int] - asm "mov r10, rdi" - asm "mov r11, rsi" - - return argc +/; main (uint argc, ~~uint8 argv) [uint] + A a + a.a = 1 + a.b = 2 + a.add_one() + return a.a + a.b ;/ -- cgit v1.2.3