From d49d3c9a13aecada7e33df86dde6a232ae285329 Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Fri, 30 Dec 2022 00:59:44 -0500 Subject: Gen asm to fix registers after a call --- tnslc/compile/compile.tnsl | 121 ++++++++++++++++++++++++++++++++------------- tnslc/compile/isa_x86.tnsl | 2 +- tnslc/compile/value.tnsl | 29 ++++++++--- 3 files changed, 109 insertions(+), 43 deletions(-) (limited to 'tnslc/compile') diff --git a/tnslc/compile/compile.tnsl b/tnslc/compile/compile.tnsl index b7bdb3e..9b593b2 100644 --- a/tnslc/compile/compile.tnsl +++ b/tnslc/compile/compile.tnsl @@ -75,23 +75,37 @@ } /; method VTrack - # returns true if the value is allocated to the stack - /; add_track({}uint8 name, VType _type) [int] - ;bool to_stack = is_struct(_type) - ;int count = 0 - /; loop (int i = 0; i < len (self.on_stack) && !to_stack) [i++] - /; if (!self.on_stack{i}) + /; next_loc(VType vt) [int] + /; if (is_struct(vt)) + ;return -1 + ;/ + + ;int count = 0 + /; loop (int i = 0; i < len (self.sym_vals)) [i++] + /; if (!(self.sym_vals{i}.on_stack)) ;count++ ;/ - /; if (count > 8) - ;to_stack = true + /; if (count > 7) + ;return -1 ;/ ;/ + ;return count + 6 + ;/ + + # returns with init commands + /; add_track({}uint8 name, VType _type) [{}uint8] + ;Value v = track_val(_type, to_stack, self.next_loc(_type)) + + /; if (v.loc < 0) + ;v.loc = 0 + ;/ + ;self.sym_names.append(name) - ;self.sym_vals.append(_type) + ;self.sym_vals.append(v) + ;return v.init_val() ;/ # Returns true if the variable is being tracked @@ -109,20 +123,41 @@ ;int out = 0 /; loop (int i = 0; i < len (self.on_stack)) [i++] /; if (self.on_stack{i}) - ;out = out + (self.sym_types{i}._size) + ;out = out + (self.sym_vals{i}._type._size) ;/ ;/ ;return out ;/ # returns the type of the named variable - /; get_vtype ({}uint8 name) [VType] + /; get_val ({}uint8 name) [Value] /; loop (int i = 0; i < len (self.on_stack)) [i++] /; if (string_equate(name, self.sym_names{i})) - ;return (self.sym_types{i}) + ;return (self.sym_vals{i}) ;/ ;/ ;/ + + # push stack updates loc for every value on the stack + /; push_stack (VType vt) [{}uint8] + ;int tsz = vt._size + /; if (vt.ptr !== 0) + ;tsz = 8 + ;/ + + /; loop (int i = 0; i < len (self.sym_vals)) [i++] + ;self.sym_vals{i}.loc = self.sym_vals{i}.loc + tsz + ;/ + + ;{}uint8 out = "\tsub $" + ;{}uint8 tmp = string_from_int(tsz) + ;add_strings(~out, ~tmp) + ;out.append(',') + ;out.append(' ') + ;tmp = "%rsp" + ;add_strings(~out, ~tmp) + ;return out + ;/ ;/ # Sizes of items @@ -205,22 +240,22 @@ ;return "nz" ;/ -/; compute_jmp ({}uint8 jxt, lab) [{}uint8] - -;/ - # Is struct returns true if the type name given is a struct /; is_struct (VType t) [bool] - /; if (t.ptr > 0) + /; if (t.ptr !== 0) ;return false ;/ + ;VType tmp = NT /; loop (int i = 0; i < 15) [i++] - /; if (string_equate(type_table{i}.name, t.name)) + ;tmp = tnslc.type_table{i} + /; if (string_equate(tmp.name, t.name)) ;return false ;/ ;/ + + ;return true ;/ @@ -353,25 +388,41 @@ # Assumes cur points to the beginning of the arguments list # Sets up the VTrack struct that is pointed to. -/; setup_vtrack (~int cur, ~{}Token data, ~VTrack tab) +/; setup_vtrack (~int cur, ~{}Token data, ~VTrack tab, ~{}uint8 csec) ;cur`++ ;VType last = NT + ;Value tmp = {false, false, 0, 0, last} /; loop (cur` < len data`) [cur`++] ;int pre_skip = cur` /; if (token_is(cur, data, ")")) + ;break ;; else if (!token_is(cur, data, ",")) + ;skip_to_name(cur, data) - /; if (pre_skip == cur`) - ;tab`.sym_types.append(last) - ;tab`.sym_names.append(data`{cur`}.data`) - ;; else + + /; if (pre_skip !== cur`) ;last = get_vtype(~pre_skip, data) - ;tab`.sym_types.append(last) - ;tab`.sym_names.append(data`{cur`}.data`) + ;tmp._type = last ;/ + + ;tab`.sym_names.append(data`{cur`}.data`) + ;int nloc = tab`.next_loc(last) + + ;Value store = {false, false, nloc, 0, last} + /; if (store.loc !< 0) + ;{}uint8 tstr = store.set_value(tmp) + ;add_strings(csec, ~tstr) + ;tmp.loc = tmp.loc + 1 + ;; else + ;store.loc = 0 + ;store.on_stack = true + ;tab`.push_stack(last) + ;/ + + ;tab`.sym_vals.append(store) ;/ ;/ ;/ @@ -549,16 +600,19 @@ ;cur`++ /; loop (data`{cur`}.token_type == TOKEN_TYPE.DEFWORD) - ;int stack = tab`.add_track() - + ;{}uint8 init = tab`.add_track(data`{cur`}.data`, def_t) + ;add_strings(~out, ~init) + ;cur`++ /; if (token_is(cur, data, ",")) ;cur`++ ;; else if (token_is(cur, data, "=")) - ;{}uint8 loc = index_to_loc(len tab`.sym_names - 1, tab) + ;{}uint8 set = index_to_loc(len tab`.sym_names - 1, tab) ;eval_value(cur, data, tab, gsc, hsec, csec, dsec, loc) /; if (token_is(cur, data, ",")) ;cur`++ ;/ + ;; else + ;break ;/ ;/ /; if (string_equate(data`{cur`+1}.data`, "=")) @@ -576,7 +630,7 @@ /; compile_block (~int cur, ~{}Token data, ~VTrack gsc, ~{}uint8 hsec, csec, dsec, {}{}uint8 mod_path, Path rel) - ;VTrack tab = { {}, {}, {} } + ;VTrack tab = { {}, {} } ;VType out_type = tnslc.type_table{14} ;{}uint8 name = "" ;bool r = false @@ -599,8 +653,11 @@ ;add_strings(csec, ~name) ;csec`.append(':') ;csec`.append('\n') + /; if (!r) + ;header_guard(csec) + ;/ ;; else if (token_is(cur, data, "(")) - ;setup_vtrack(cur, data, ~tab) + ;setup_vtrack(cur, data, ~tab, csec) ;; else if (token_is(cur, data, "[")) ;cur`++ ;out_type = get_vtype(cur, data) @@ -614,10 +671,6 @@ ;tnsl.io.println(out_type.name) - /; if (!r) - ;header_guard(csec) - ;/ - ;bool ret = false /; loop (cur` < len data` && !ret) /; if (token_is(cur, data, ";/")) diff --git a/tnslc/compile/isa_x86.tnsl b/tnslc/compile/isa_x86.tnsl index 3fda486..1cb09ab 100644 --- a/tnslc/compile/isa_x86.tnsl +++ b/tnslc/compile/isa_x86.tnsl @@ -158,7 +158,7 @@ ;; else - ;{}uint8 out = "r" + ;out.append('r') ;add_strings(~out, ~common) /; if (size == 1) ;out.append('b') diff --git a/tnslc/compile/value.tnsl b/tnslc/compile/value.tnsl index 28586f4..747c172 100644 --- a/tnslc/compile/value.tnsl +++ b/tnslc/compile/value.tnsl @@ -36,15 +36,20 @@ ;return "si" ;; if (r == 5) ;return "di" - ;; if (r == 6) - ;return "bp" ;/ - ;return string_from_int(r) + ;return string_from_int(r + 2) ;/ /; method Value + /; get_norm_sz [int] + /; if (self._type.ptr !== 0) + ;return 8 + ;/ + ;returnself._type._size + ;/ + /; get_norm_loc [{}uint8] /; if (self.on_stack) ;return val_from_address(self.loc, "%rsp") @@ -85,10 +90,10 @@ ;/ /; standard_op(Value other, {}uint8 op) [{}uint8] - ;int tsz = v._type._size - ;v._type._size = self._type._size - ;{}uint8 tmp = v.get_norm_loc() - ;v._type._size = tsz + ;int tsz = other._type._size + ;other._type._size = self._type._size + ;{}uint8 tmp = other.get_norm_loc() + ;other._type._size = tsz ;{}uint8 out = "\t" ;add_strings(~out, ~op) ;out.append(' ') @@ -193,6 +198,14 @@ ;/ ;/ + /; ref_value (Value to_ref) [{}uint8] + /; if (!to_ref.on_stack) + ;return "" + ;/ + + ;return self.standard_op(to_ref, "lea") + ;/ + /; deref_value [{}uint8] /; if (self._type.ptr == 0) ;return "" @@ -223,7 +236,7 @@ /; if (self.literal) ;self.val = v.val ;return "" - ;; else if (!self.on_stack) + ;; else if (!(self.on_stack)) ;return self.standard_op(v, "mov") ;/ ;/ -- cgit v1.2.3