diff options
| author | Kai Gunger <kgunger12@gmail.com> | 2026-03-20 17:22:32 -0400 |
|---|---|---|
| committer | Kai Gunger <kgunger12@gmail.com> | 2026-03-20 17:22:32 -0400 |
| commit | 34a791931c82d0fc0b0be954b7498e474086c5a9 (patch) | |
| tree | 83f0a86972dccb4e4bb2e7ea8836ec701ff29a8d /tnslc/compile/scope.tnsl | |
| parent | 6fcd9b168c2667c3e757bce7f68377954917224a (diff) | |
[tnslc] fix bug in scope for getting stack locationsorigin
Diffstat (limited to 'tnslc/compile/scope.tnsl')
| -rw-r--r-- | tnslc/compile/scope.tnsl | 84 |
1 files changed, 58 insertions, 26 deletions
diff --git a/tnslc/compile/scope.tnsl b/tnslc/compile/scope.tnsl index d3f86a9..e3348dd 100644 --- a/tnslc/compile/scope.tnsl +++ b/tnslc/compile/scope.tnsl @@ -6,7 +6,8 @@ struct Scope { ~Scope parent, utils.Vector vars, tmps, - + + # Used for generating unique sub-scope labels int unique } @@ -109,7 +110,7 @@ struct Scope { ~Var v /; loop (int i = 0; i < self.tmps.count) [i++] - v = self.vars.get(i) + v = self.tmps.get(i) /; if (v`.loc > 1) out++ /; if (out > 4) @@ -158,7 +159,6 @@ struct Scope { Var mk = v`.copy() mk.offset = 0 - # TODO: Make sure this works properly /; if (mk.loc > 0) mk.loc = 1 ;/ @@ -175,7 +175,7 @@ struct Scope { tp = sub ;; else if (sub`._type == parse.NTYPE_ID) /; if (tp == NULL) - _printf("COMPILER ERROR: Should have type node before first id in decl node\n\0") + _printf("COMPILER ERROR: Should have type node before first id in decl/dlist node\n\0") return ;/ @@ -184,11 +184,12 @@ struct Scope { v.init(tp, sub) v._resolve_type(self.mod) - # TODO: Make sure this works properly + # Compute weather we can put it in a register /; if (v.regable() == true) v.loc = 1 ;; else - v.loc = 0 - 1 + v.loc = 0 + v.loc = v.loc - 1 ;/ v.offset = 0 @@ -198,14 +199,47 @@ struct Scope { /; if (sub`.sub.count > 0) sub = sub`.sub.get(0) /; if (sub`._type == parse.NTYPE_VALUE) - self.precheck_stmt(n) + self.precheck_stmt(sub) ;/ ;/ ;/ ;/ ;/ + /; try_move (~parse.Node n) + /; loop (n`._type == parse.NTYPE_PRE_OP) + /; if (n`.sub.count < 1) + return + ;/ + n = n`.sub.get(0) + ;/ + + /; if (n`._type == parse.NTYPE_ID) + ~Var to_move = self._find_var(n`.data) + /; if (to_move == NULL) + return + ;/ + + /; if (to_move`.loc > 0) + int negative = 0 + to_move`.loc = negative - 1 + ;/ + ;/ + ;/ + /; precheck_stmt (~parse.Node n) + /; if (n`._type == parse.NTYPE_PRE_OP) + /; if (utils.strcmp(n`.data, "~\0") == true) + self.try_move(n) + return + ;/ + ;/ + + ~parse.Node sub + /; loop (int i = 0; i < n`.sub.count) [i++] + sub = n`.sub.get(i) + self.precheck_stmt(sub) + ;/ ;/ /; _find_var (~uint8 name) [~Var] @@ -260,6 +294,17 @@ struct Scope { ;/ ;/ + /; if (v`.loc < 0) + int stk = v`.actual_size() + ~uint8 stk_str = utils.int_to_str(stk) + self.cb`.add_c(" sub rsp, \0") + self.cb`.add_c(stk_str) + self.cb`.add_c(" ; Create stack space for local variable \0") + self.cb`.add_c(v`.name) + self.cb`.add_c("\n\0") + _delete(stk_str) + ;/ + return v ;/ @@ -270,24 +315,12 @@ struct Scope { return NULL ;/ - ~int32 p = v`.top_ptrc() - /; if (p == NULL) - v`.set(self.cb, src) - ;; else if (p` == 0) + /; if (v`.is_ref() == true) v`.set_ref(self.cb, src) ;; else v`.set(self.cb, src) ;/ - /; if (v`.loc < 0) - int stk = v`.actual_size() - ~uint8 stk_str = utils.int_to_str(stk) - self.cb`.add_c(" sub rsp, \0") - self.cb`.add_c(stk_str) - self.cb`.add_c("\n\0") - _delete(stk_str) - ;/ - return v ;/ @@ -316,7 +349,7 @@ struct Scope { ~uint8 stk_str = utils.int_to_str(stk) self.cb`.add_c(" sub rsp, \0") self.cb`.add_c(stk_str) - self.cb`.add_c("\n\0") + self.cb`.add_c(" ; Create stack space for tmp variable\n\0") _delete(stk_str) ;/ @@ -349,7 +382,7 @@ struct Scope { ~uint8 stk_mv_str = utils.int_to_str(stk_mv) self.cb`.add_c(" add rsp, \0") self.cb`.add_c(stk_mv_str) - self.cb`.add_c("\n\0") + self.cb`.add_c(" ; Free stack space (freeing tmp variables)\n\0") _delete(stk_mv_str) ;/ ;/ @@ -378,7 +411,7 @@ struct Scope { true_name.push_char('#') true_name.push_cstr(name) _delete(u) - + Scope out = self.mk_sub(true_name.as_cstr()) true_name.end() @@ -389,14 +422,13 @@ struct Scope { ;/ # Get closest breakable scope - /; _closest_break [~Scope] /; if (utils.ends_with(self.name, "#wrap\0")) return ~self ;; else if (utils.ends_with(self.name, "#loop\0")) return ~self ;/ - + /; if (self.parent == NULL) return NULL ;/ @@ -438,7 +470,7 @@ struct Scope { # Label generation # - + /; _base_label [utils.Vector] utils.Vector out out.init(1) |