diff options
Diffstat (limited to 'tnslc/compile/scope.tnsl')
| -rw-r--r-- | tnslc/compile/scope.tnsl | 104 |
1 files changed, 97 insertions, 7 deletions
diff --git a/tnslc/compile/scope.tnsl b/tnslc/compile/scope.tnsl index ebfc671..2461abb 100644 --- a/tnslc/compile/scope.tnsl +++ b/tnslc/compile/scope.tnsl @@ -369,6 +369,9 @@ struct Scope { v.loc = v.loc + 6 ;/ ;; else + int loc = 0 + loc = loc - 1 + v.loc = loc tmp = self._next_stack_slot() int sz = v.actual_size() v.offset = tmp - sz @@ -388,6 +391,58 @@ struct Scope { return v.copy() ;/ + # Make a temp variable on the stack for use in computations + /; mk_tmp_stack (~Var base) [Var] + Var v = base`.copy() + v.offset = 0 + + int loc = 0 + loc = loc - 1 + v.loc = loc + int tmp = self._next_stack_slot() + int sz = v.actual_size() + v.offset = tmp - sz + + ~uint8 stk_str = utils.int_to_str(sz) + self.cb`.add_c(" sub rsp, \0") + self.cb`.add_c(stk_str) + self.cb`.add_c(" ; Create stack space for tmp variable\n\0") + _delete(stk_str) + + self.tmps.push(~v) + + return v.copy() + ;/ + + /; _save_caller_tmp (~utils.Vector vec) + # TODO + ;/ + + /; save_caller_tmp [Var] + Var tmp + utils.Vector tmps + tmps.init(len tmp) + + self._save_caller_tmp(~tmps) + + ~Var to_save + /; loop (int i = 0; i < tmps.count) [i++] + to_save = tmps.get(i) + tmp = self.mk_tmp_stack(to_save) + tmp.set(self.cb, to_save) + /; if (i < tmps.count - 1) + tmp.end() + ;/ + ;/ + tmps.end() + + return tmp + ;/ + + /; restore_caller_tmp (~Var handle) + # TODO + ;/ + # 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 @@ -419,33 +474,68 @@ struct Scope { # Returns true if the variable is in a temp spot /; is_tmp (~Var v) [bool] + ~Var tmp + /; loop (int i = 0; i < self.tmps.count) [i++] + tmp = self.tmps.get(i) + /; if (tmp`.loc == v`.loc) + /; if (tmp`.offset == v`.offset) + return true + ;/ + ;/ + ;/ return false ;/ + # Free all tmp variables created after and including the one provided /; free_to (~Var tmp, bool code) # TODO:DETERMINE HOW MANY TMPS int tmps = 0 + tmps = tmps - 1 ~Var v - /; loop (int i - 1; i !> self.tmps.count) [i++] + /; loop (int i = 1; i !> self.tmps.count) [i++] v = self.tmps.get(self.tmps.count - i) /; if (v`.offset == tmp`.offset) - tmps = self.tmps.count - tmps = tmps - i - tmps = tmps + 1 + tmps = i + i = self.tmps.count + 1 ;/ ;/ - /; if (tmps !== 0) - self.free_tmp(tmps, code) - ;; else + /; if (tmps < 0) ~uint8 scope_name = self.base_label() int off = tmp`.offset _printf("COMPILER ERROR: Unable to find temp to free to in scope \"\0") _printf(scope_name) _printf("\"\n\0") _print_num(" Was looking for tmp with offset %d but could not find one.\n\0", off) + ;; else if (tmps > 0) + self.free_tmp(tmps, code) ;/ + ;/ + # Free all tmp variables created after the one provided + /; free_after (~Var tmp, bool code) + # TODO:DETERMINE HOW MANY TMPS + int tmps = 0 + tmps = tmps - 1 + ~Var v + /; loop (int i = 1; i !> self.tmps.count) [i++] + v = self.tmps.get(self.tmps.count - i) + /; if (v`.offset == tmp`.offset) + tmps = i - 1 + i = self.tmps.count + 1 + ;/ + ;/ + + /; if (tmps < 0) + ~uint8 scope_name = self.base_label() + int off = tmp`.offset + _printf("COMPILER ERROR: Unable to find temp to free to in scope \"\0") + _printf(scope_name) + _printf("\"\n\0") + _print_num(" Was looking for tmp with offset %d but could not find one.\n\0", off) + ;; else if (tmps > 0) + self.free_tmp(tmps, code) + ;/ ;/ # |