From 5b81a4819a249921085f6596927f9939dffc46e7 Mon Sep 17 00:00:00 2001 From: CircleShift Date: Fri, 5 Dec 2025 01:03:25 -0500 Subject: [tnslc] Fix pointer issue with find func --- tnslc/compile/module.tnsl | 21 +++++++++++++-------- tnslc/compile/struct.tnsl | 2 +- tnslc/compile/var.tnsl | 4 ++-- tnslc/test.tnsl | 22 ++++++++++++++++++++++ 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/tnslc/compile/module.tnsl b/tnslc/compile/module.tnsl index 38653da..d2f1525 100644 --- a/tnslc/compile/module.tnsl +++ b/tnslc/compile/module.tnsl @@ -183,29 +183,34 @@ struct Module { /; if ((lvl + 1) < key`.count) ~Module m - ~uint8 str = key`.get(lvl) + ~~uint8 str = key`.get(lvl) + _printf("Trying to find sub '\0") + _printf(str`) + _printf("' in module '\0") + _printf(self.name) + _printf("'\n\0") + /; loop (int i = 0; i < self.subs.count) [i++] m = self.subs.get(i) - /; if (utils.strcmp(str, m`.name) == true) + /; if (utils.strcmp(str`, m`.name) == true) ~void v = m._find(stype, key, lvl + 1) /; if (v != NULL) return v ;/ - return NULL ;/ ;/ ;; else - ~uint8 str = key`.get(key`.count - 1) + ~~uint8 str = key`.get(key`.count - 1) ~void out = NULL /; if (stype == SEARCH_VAR) - out = self._find_var(str) + out = self._find_var(str`) ;; else if (stype == SEARCH_STRUCT) - out = self._find_struct(str) + out = self._find_struct(str`) ;; else if (stype == SEARCH_FUNC) - out = self._find_func(str) + out = self._find_func(str`) ;; else if (stype == SEARCH_SUB) - out = self._find_sub(str) + out = self._find_sub(str`) ;/ /; if (out !== NULL) diff --git a/tnslc/compile/struct.tnsl b/tnslc/compile/struct.tnsl index 7c18585..45f85d3 100644 --- a/tnslc/compile/struct.tnsl +++ b/tnslc/compile/struct.tnsl @@ -189,7 +189,7 @@ struct Struct { n = tn`.sub.get(i) /; if (n`._type == parse.NTYPE_ID) str = n`.data - sv.push(str) + sv.push(~str) seen_id = true ;; else if (seen_id == true) i = tn`.sub.count diff --git a/tnslc/compile/var.tnsl b/tnslc/compile/var.tnsl index c26d227..df62d66 100644 --- a/tnslc/compile/var.tnsl +++ b/tnslc/compile/var.tnsl @@ -190,8 +190,8 @@ struct Var { ;; else t = _tn`.sub.get(idx) /; if (t`._type == parse.NTYPE_ID) - ~void str = t`.data - strv.push(str) + ~uint8 str = t`.data + strv.push(~str) ;; else running = false ;/ diff --git a/tnslc/test.tnsl b/tnslc/test.tnsl index 6163318..fd29a23 100644 --- a/tnslc/test.tnsl +++ b/tnslc/test.tnsl @@ -1,5 +1,27 @@ +/; module Container + /; module m1 + struct Box { + ~uint8 data + } + ;/ + + /; module m2 + struct Crate { + {}m1.Box boxes + } + ;/ + +;/ + +struct Option { + bool has_crate, + Container.m2.Crate crate +} + + /; main (int argc, ~~uint8 argv) [int] + return 0 ;/ -- cgit v1.2.3 From 062aefd6aa7cda6a30ae557f4813a2a967b49596 Mon Sep 17 00:00:00 2001 From: CircleShift Date: Fri, 5 Dec 2025 01:58:03 -0500 Subject: [tnslc] Fix struct resolution --- tnslc/compile/module.tnsl | 2 +- tnslc/compile/struct.tnsl | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/tnslc/compile/module.tnsl b/tnslc/compile/module.tnsl index d2f1525..99d42f1 100644 --- a/tnslc/compile/module.tnsl +++ b/tnslc/compile/module.tnsl @@ -188,7 +188,7 @@ struct Module { _printf(str`) _printf("' in module '\0") _printf(self.name) - _printf("'\n\0") + _print_num("' at level %d\n\0", lvl) /; loop (int i = 0; i < self.subs.count) [i++] diff --git a/tnslc/compile/struct.tnsl b/tnslc/compile/struct.tnsl index 45f85d3..780e8ad 100644 --- a/tnslc/compile/struct.tnsl +++ b/tnslc/compile/struct.tnsl @@ -149,13 +149,13 @@ struct Struct { return ;/ - /; if (ft`.size == 0) - # Recurse - ft`._compute_size(parent) - ;/ - /; if (ft`.size < 0) # Cyclical dependency + _printf("ERROR: Cyclic struct definition: '\0") + _printf(ft`.name) + _printf("' imported from '\0") + _printf(self.name) + _printf("'\n\0") return ;/ @@ -198,10 +198,13 @@ struct Struct { # Find struct and compute its size ~Struct out = parent`.find(SEARCH_STRUCT, ~sv) - sv.pop() - ~Module outp = parent - /; if (sv.count !== 0) - outp = parent`.find(SEARCH_SUB, ~sv) + /; if (out !== NULL && out`.size == 0) + sv.pop() + ~Module outp = parent + /; if (sv.count !== 0) + outp = parent`.find(SEARCH_SUB, ~sv) + ;/ + out`._compute_size(outp) ;/ sv.end() return out -- cgit v1.2.3 From 17620a6aae801ff22e8eb6a95b30f6e830742542 Mon Sep 17 00:00:00 2001 From: CircleShift Date: Sun, 7 Dec 2025 02:08:21 -0500 Subject: in progress reg gen --- tnslc/compile/function.tnsl | 15 ++++++++++ tnslc/compile/module.tnsl | 6 ---- tnslc/compile/scope.tnsl | 63 ++++++++++++++++++++++++++++++++++++++-- tnslc/compile/var.tnsl | 19 ++++++++++++ tnslc/eee | 70 --------------------------------------------- 5 files changed, 95 insertions(+), 78 deletions(-) delete mode 100644 tnslc/eee diff --git a/tnslc/compile/function.tnsl b/tnslc/compile/function.tnsl index bf06f80..f0aac81 100644 --- a/tnslc/compile/function.tnsl +++ b/tnslc/compile/function.tnsl @@ -19,6 +19,8 @@ struct Function { /; _resolve_dlist (~Module parent, ~parse.Node dl) ~parse.Node tn = NULL ~parse.Node n + int reg = 1 + int stack_down = 0 /; loop (int i = 0; i < dl`.sub.count) [i++] n = dl`.sub.get(i) /; if (n`._type == parse.NTYPE_TYPE) @@ -31,6 +33,14 @@ struct Function { Var p p.init(tn, n) p._resolve_type(parent) + /; if (p.is_reg_passed() == true && reg < 7) + p.loc = reg + reg++ + ;; else + p.loc = 0 + p.loc = p.loc - stack_down + stack_down = stack_down - p.actual_size() + ;/ self.inputs.push(~p) ;/ ;/ @@ -96,6 +106,11 @@ struct Function { cb`.add_c(" push r15 ; scope init\n\n\0") # TODO: Add all params to the scope + ~Var in + /; loop (int i = 0; i < self.inputs.count) [i++] + in = self.inputs.get(i) + out.mk_set_var(in) + ;/ return out ;/ diff --git a/tnslc/compile/module.tnsl b/tnslc/compile/module.tnsl index 99d42f1..c7ceab6 100644 --- a/tnslc/compile/module.tnsl +++ b/tnslc/compile/module.tnsl @@ -184,12 +184,6 @@ struct Module { /; if ((lvl + 1) < key`.count) ~Module m ~~uint8 str = key`.get(lvl) - _printf("Trying to find sub '\0") - _printf(str`) - _printf("' in module '\0") - _printf(self.name) - _print_num("' at level %d\n\0", lvl) - /; loop (int i = 0; i < self.subs.count) [i++] m = self.subs.get(i) diff --git a/tnslc/compile/scope.tnsl b/tnslc/compile/scope.tnsl index c152d00..24fbed1 100644 --- a/tnslc/compile/scope.tnsl +++ b/tnslc/compile/scope.tnsl @@ -5,8 +5,7 @@ struct Scope { ~Scope parent, - utils.Vector - vars, + utils.Vector vars, int unique } @@ -61,6 +60,66 @@ struct Scope { self.vars.end() ;/ + # + # Make variables + # + + /; _next_reg_slot [uint] + uint out = 9 + /; if (self.parent !== NULL) + out = self.parent`._next_reg_slot() + ;/ + + /; if (out == 0) + return out + ;/ + + ~Var v + /; loop (int i = 0; i < self.vars.count) [i++] + v = self.vars.get(i) + /; if (v`.loc > 0) + out++ + /; if (out > 16) + return 0 + ;/ + ;/ + ;/ + + return out + ;/ + + /; _next_stack_slot [uint] + uint out = 0 + /; if (self.parent !== NULL) + out = self.parent`._next_stack_slot() + ;/ + + ~Var v + /; loop (int i = 0; i < self.vars.count) [i++] + v = self.vars.get(i) + /; if (v`.loc < 0) + out = out - v`.actual_size() + ;/ + ;/ + + return out + ;/ + + /; mk_set_var (~Var src) + Var out + + /; if (src`.is_reg_passed() == true) + out.loc = self._next_reg_slot() + /; if (out.loc == 0) + out.loc = self._next_stack_slot() + ;/ + ;; else + out.loc = self._next_stack_slot() + ;/ + + self.vars.push(~out) + ;/ + # # Sub scope # diff --git a/tnslc/compile/var.tnsl b/tnslc/compile/var.tnsl index df62d66..34c101a 100644 --- a/tnslc/compile/var.tnsl +++ b/tnslc/compile/var.tnsl @@ -108,6 +108,8 @@ struct Var { /; init (~parse.Node tn, id) self.name = utils.strcpy(id`.data) self.ptrc.init(4) + self.loc = 0 + self._tn = tn self._id = id ;/ @@ -148,6 +150,23 @@ struct Var { self.ptrc.push(~ptr) ;/ + /; is_reg_passed [bool] + /; if (self.ptrc.count > 0) + return true + ;/ + return false + ;/ + + /; actual_size [uint] + /; if (self.ptrc.count > 0) + return 8 + ;; else if (self._type == NULL) + return 0 + ;/ + + return self._type`.size + ;/ + # Sets up both the ptrc and the _type members, requires # parent module for resolution of types /; _resolve_type (~Module parent) diff --git a/tnslc/eee b/tnslc/eee deleted file mode 100644 index 150f6e9..0000000 --- a/tnslc/eee +++ /dev/null @@ -1,70 +0,0 @@ -==== STARTING FIND WITH KEY ==== -key parts 1 -==== STARTING FIND WITH KEY ==== -key parts 1 -{ Module : - { Struct : Box - size: 8 - members: - } - { Struct : bool - size: 1 - members: - } - { Struct : uint8 - size: 1 - members: - } - { Struct : int8 - size: 1 - members: - } - { Struct : uint16 - size: 2 - members: - } - { Struct : int16 - size: 2 - members: - } - { Struct : uint32 - size: 4 - members: - } - { Struct : int32 - size: 4 - members: - } - { Struct : float32 - size: 4 - members: - } - { Struct : uint64 - size: 8 - members: - } - { Struct : int64 - size: 8 - members: - } - { Struct : float64 - size: 8 - members: - } - { Struct : uint - size: 8 - members: - } - { Struct : int - size: 8 - members: - } - { Struct : float - size: 8 - members: - } - { Struct : void - size: 8 - members: - } -} -- cgit v1.2.3