diff options
| author | Kai Gunger <kgunger12@gmail.com> | 2025-12-07 18:32:35 -0500 |
|---|---|---|
| committer | Kai Gunger <kgunger12@gmail.com> | 2025-12-07 18:32:35 -0500 |
| commit | daedfe9e1684ae855e2a4cf21249e1274ff8484d (patch) | |
| tree | 6cacc8e364efb9b1472dfeb48a02bd37e1866634 /tnslc | |
| parent | 9e8e52bf43b325ab636fc4de81acc89eb17a2c1d (diff) | |
| parent | 17620a6aae801ff22e8eb6a95b30f6e830742542 (diff) | |
Merge branch 'origin' of cshift.net:git/tnsl-lang into origin
Diffstat (limited to 'tnslc')
| -rw-r--r-- | tnslc/compile/function.tnsl | 10 | ||||
| -rw-r--r-- | tnslc/compile/module.tnsl | 15 | ||||
| -rw-r--r-- | tnslc/compile/scope.tnsl | 63 | ||||
| -rw-r--r-- | tnslc/compile/struct.tnsl | 23 | ||||
| -rw-r--r-- | tnslc/compile/var.tnsl | 23 | ||||
| -rw-r--r-- | tnslc/test.tnsl | 22 |
6 files changed, 131 insertions, 25 deletions
diff --git a/tnslc/compile/function.tnsl b/tnslc/compile/function.tnsl index eaf1da5..73188b8 100644 --- a/tnslc/compile/function.tnsl +++ b/tnslc/compile/function.tnsl @@ -20,6 +20,7 @@ struct Function { ~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) @@ -32,10 +33,13 @@ struct Function { Var p p.init(tn, n) p._resolve_type(parent) - /; if (reg < 7 && p.is_reg_passed()) + /; 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) ;/ @@ -102,10 +106,10 @@ struct Function { cb`.add_c(" push r15 ; scope init\n\n\0") # Add all params to the scope - ~Var inp + ~Var in /; loop (int i = 0; i < self.inputs.count) [i++] inp = self.inputs.get(i) - out.mk_set_var(inp) + out.mk_set_var(in) ;/ return out diff --git a/tnslc/compile/module.tnsl b/tnslc/compile/module.tnsl index 38653da..c7ceab6 100644 --- a/tnslc/compile/module.tnsl +++ b/tnslc/compile/module.tnsl @@ -183,29 +183,28 @@ struct Module { /; if ((lvl + 1) < key`.count) ~Module m - ~uint8 str = key`.get(lvl) + ~~uint8 str = key`.get(lvl) /; 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/scope.tnsl b/tnslc/compile/scope.tnsl index 69e445c..7dcf0ea 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 } @@ -62,6 +61,66 @@ struct Scope { ;/ # + # 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/struct.tnsl b/tnslc/compile/struct.tnsl index 7c18585..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 ;/ @@ -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 @@ -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 diff --git a/tnslc/compile/var.tnsl b/tnslc/compile/var.tnsl index 4425cf7..33c3973 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 ;/ @@ -153,6 +155,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) @@ -195,8 +214,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 ;/ |