diff options
| author | Kai Gunger <kgunger12@gmail.com> | 2026-01-05 02:09:37 -0500 |
|---|---|---|
| committer | Kai Gunger <kgunger12@gmail.com> | 2026-01-05 02:09:37 -0500 |
| commit | 3b0dd26823b62e13bf38376f0a0211ce7861c150 (patch) | |
| tree | 8b241fd16c58511330e980ce9a56020a47befe50 /tnslc | |
| parent | 4289c90cc90a9798091a741c3e18861b47836402 (diff) | |
fix some improper memory accessesorigin
Diffstat (limited to 'tnslc')
| -rw-r--r-- | tnslc/compile/function.tnsl | 7 | ||||
| -rw-r--r-- | tnslc/compile/scope.tnsl | 103 |
2 files changed, 106 insertions, 4 deletions
diff --git a/tnslc/compile/function.tnsl b/tnslc/compile/function.tnsl index bd3438e..6f7fceb 100644 --- a/tnslc/compile/function.tnsl +++ b/tnslc/compile/function.tnsl @@ -289,7 +289,7 @@ struct Function { tmp = s`.mk_tmp(out) cmp = self._compile_value(s, val_node, out) - tmp.set(s, ~cmp) + tmp.set(s`.cb, ~cmp) cmp.end() tmps.push(~tmp) ;/ @@ -299,7 +299,7 @@ struct Function { /; loop (int i = 0; i < self.outputs.count) [i++] out = self.outputs.get(i) tt = tmps.get(i) - out`.set(s`.cb, tmp) + out`.set(s`.cb, tt) tt`.end() ;/ @@ -325,6 +325,7 @@ struct Function { s`.cb`.add_c(" jmp \0") s`.cb`.add_c(lab) s`.cb`.add_c("\n\0") + _delete(lab) ;/ # Should handle break, continue, and return @@ -342,6 +343,7 @@ struct Function { s`.cb`.add_c(" jmp \0") s`.cb`.add_c(lab) s`.cb`.add_c("\n\0") + _delete(lab) ;; else if (utils.strcmp(n`.data, "break\0") == true) ~Scope br = s`.closest_break() /; if (br == NULL) @@ -352,6 +354,7 @@ struct Function { s`.cb`.add_c(" jmp \0") s`.cb`.add_c(lab) s`.cb`.add_c("\n\0") + _delete(lab) ;; else _printf("COMPILER ERROR: The following was detected as flow control, but we do not handle it: '\0") _printf(n`.data) diff --git a/tnslc/compile/scope.tnsl b/tnslc/compile/scope.tnsl index b199abe..d3f86a9 100644 --- a/tnslc/compile/scope.tnsl +++ b/tnslc/compile/scope.tnsl @@ -97,6 +97,32 @@ struct Scope { return out ;/ + /; _next_tmp_slot [int] + int out = 2 + /; if (self.parent !== NULL) + out = self.parent`._next_tmp_slot() + ;/ + + /; if (out < 0) + return out + ;/ + + ~Var v + /; loop (int i = 0; i < self.tmps.count) [i++] + v = self.vars.get(i) + /; if (v`.loc > 1) + out++ + /; if (out > 4) + out = 0 + out = out - 1 + return out + ;/ + ;/ + ;/ + + return out + ;/ + /; _next_stack_slot [int] int out = 0 out = out - 56 @@ -108,7 +134,20 @@ struct Scope { /; loop (int i = 0; i < self.vars.count) [i++] v = self.vars.get(i) /; if (v`.loc < 0 && v`.offset !== 0) - out = out - v`.actual_size() + int off = v`.offset - v`.actual_size() + /; if (off < out) + out = off + ;/ + ;/ + ;/ + + /; loop (int i = 0; i < self.tmps.count) [i++] + v = self.tmps.get(i) + /; if (v`.loc < 0 && v`.offset !== 0) + int off = v`.offset - v`.actual_size() + /; if (off < out) + out = off + ;/ ;/ ;/ @@ -240,19 +279,79 @@ struct Scope { 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 ;/ # Make a temp variable in register or stack for use in computations /; mk_tmp (~Var base) [Var] Var v = base`.copy() - return v + v.offset = 0 + + int tmp = 0 + /; if (v.regable() == true) + int tmp = self._next_tmp_slot() + v.loc = tmp + /; if (v.loc + 1 == 0) + tmp = self._next_stack_slot() + v.offset = tmp + ;; else if (v.loc > 2) + v.loc = v.loc + 6 + ;/ + ;; else + tmp = self._next_stack_slot() + v.offset = tmp + ;/ + + /; 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) + ;/ + + self.tmps.push(~v) + + return v.copy() ;/ # Free n tmp variables (starts from most recently created) # If you want to generate code to set the stack pointer then # set code to true /; free_tmp (int tmps, bool code) + /; if (self.tmps.count < tmps) + tmps = self.tmps.count + ;/ + + int stk_mv = 0 + + ~Var tmp + /; loop (int i = 1; i !> tmps) [i++] + tmp = self.tmps.get(self.tmps.count - i) + /; if (tmp`.loc < 0) + stk_mv = stk_mv + tmp`.actual_size() + ;/ + tmp`.end() + self.tmps.pop() + ;/ + + /; if (stk_mv > 0) + ~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") + _delete(stk_mv_str) + ;/ ;/ # |