diff options
author | Kyle Gunger <kgunger12@gmail.com> | 2022-12-23 03:33:56 -0500 |
---|---|---|
committer | Kyle Gunger <kgunger12@gmail.com> | 2022-12-23 03:33:56 -0500 |
commit | 3b0ad0c84111be433de39b8a74c03db7e99cb686 (patch) | |
tree | b7f7149953d1523a5824b0c04e4d31591e2bc0bd /tnslc/compile | |
parent | 2ec32dac56b544bc4851a3efddf75ee5fd9314ed (diff) |
Add initial value functions
Diffstat (limited to 'tnslc/compile')
-rw-r--r-- | tnslc/compile/compile.tnsl | 49 | ||||
-rw-r--r-- | tnslc/compile/isa_x86.tnsl | 24 | ||||
-rw-r--r-- | tnslc/compile/value.tnsl | 225 |
3 files changed, 213 insertions, 85 deletions
diff --git a/tnslc/compile/compile.tnsl b/tnslc/compile/compile.tnsl index b8c6cd3..b7bdb3e 100644 --- a/tnslc/compile/compile.tnsl +++ b/tnslc/compile/compile.tnsl @@ -69,12 +69,9 @@ ;struct VTrack { {}{}uint8 sym_names, - - {}bool - on_stack, - {}VType - sym_types + {}Value + sym_vals } /; method VTrack @@ -88,20 +85,13 @@ ;count++ ;/ - /; if (count > 7) + /; if (count > 8) ;to_stack = true ;/ ;/ ;self.sym_names.append(name) - ;self.sym_types.append(_type) - ;self.on_stack.append(to_stack) - - /; if (count > 7) - ;return -1 - ;/ - - ;return count + ;self.sym_vals.append(_type) ;/ # Returns true if the variable is being tracked @@ -196,6 +186,29 @@ ;return out ;/ +# Bool to j +/; cmp_to_jxt ({}uint8 c) [{}uint8] + /; if (string_equate(c, "<")) + ;return "l" + ;; else if (string_equate(c, ">")) + ;return "g" + ;; else if (string_equate(c, "!<") || string_equate(c, ">==")) + ;return "nl" + ;; else if (string_equate(c, "!>") || string_equate(c, "<==")) + ;return "ng" + ;; else if (string_equate(c, "==")) + ;return "e" + ;; else if (string_equate(c, "!==")) + ;return "ne" + ;; + ;/ + ;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) @@ -701,14 +714,14 @@ ;out.write('\n') - /; loop (int i = 0; i < len csec) [i++] - ;out.write(csec{i}) + /; loop (int i = 0; i < len dsec) [i++] + ;out.write(dsec{i}) ;/ ;out.write('\n') - /; loop (int i = 0; i < len dsec) [i++] - ;out.write(dsec{i}) + /; loop (int i = 0; i < len csec) [i++] + ;out.write(csec{i}) ;/ ;out.write('\n') diff --git a/tnslc/compile/isa_x86.tnsl b/tnslc/compile/isa_x86.tnsl index 02ba899..3fda486 100644 --- a/tnslc/compile/isa_x86.tnsl +++ b/tnslc/compile/isa_x86.tnsl @@ -131,31 +131,23 @@ # - bx # - cx # - dx -# - sp -# - bp # - si # - di +# - bp +# - sp # - 8-15 #/ /; get_reg (uint size, {}uint8 common) [{}uint8] ;{}uint8 out = "%" - /; if (string_equate(common, "ax") || string_equate(common, "bx") || string_equate(common, "cx") || string_equate(common, "dx")) - - /; if (size == 1) - ;common{1} = 'l' - ;; else if (size == 4) - ;out.append('e') - ;; else if (size == 8) - ;out.append('r') - ;/ - - ;add_strings(~out, ~common) + /; if (common{0} !< 'a') - ;; else if (string_equate(common, "sp") || string_equate(common, "bp") || string_equate(common, "si") || string_equate(common, "di")) - /; if (size == 1) - ;common.append('l') + /; if(common{1} == 'x') + ;common{1} = 'l' + ;; else + ;common.append('l') + ;/ ;; else if (size == 4) ;out.append('e') ;; else if (size == 8) diff --git a/tnslc/compile/value.tnsl b/tnslc/compile/value.tnsl index 701d4b9..28586f4 100644 --- a/tnslc/compile/value.tnsl +++ b/tnslc/compile/value.tnsl @@ -1,7 +1,6 @@ ;struct Value { bool on_stack, - temp, literal, int @@ -12,17 +11,16 @@ _type } -;Value NV = {false, false, false, 0, 0, 0, {0, 0, "null", {}, {}}} +;Value NV = {false, false, 0, 0, 0, {0, 0, "null", {}, {}}} -/; setup_stack_offset (int offset) [{}uint8] - ;{}uint8 out = "$" - ;{}uint8 tmp = string_from_int(offset) - ;add_strings(~out, ~tmp) - ;return mov_asm(out, "%rax") +/; val_from_address (int offset, {}uint8 reg) [{}uint8] + ;{}uint8 out = string_from_int(offset) + ;out.append('(') + ;add_strings(~out, ~reg) + ;out.append(')') + ;return out ;/ -;{}charp general_stack_offset = "(%rsp, %rax, 1)" - # The temp registers in order /; reg_by_num(int r) [{}uint8] @@ -49,33 +47,58 @@ /; get_norm_loc [{}uint8] /; if (self.on_stack) - ;return general_stack_offset + ;return val_from_address(self.loc, "%rsp") ;/ - /; if (self.temp) - ;return get_reg(self._type._size, reg_by_num(self.loc)) - ;; else - ;return get_reg(self._type._size, reg_by_num(self.loc + 8)) + /; if (self.literal) + ;{}uint8 out = "$" + ;{}uint8 num = string_from_int(self.val) + ;add_strings(~out, ~num) + ;return out ;/ - ;return "" + ;int sz = self._type._size + /; if (self._type.ptr !== 0) + ;sz = 8 + ;/ + + ;return get_reg(sz, reg_by_num(self.loc)) ;/ - /; add_literal (int i) [{}uint8] - /; if (self.literal) - ;self.val = self.val + i - ;return "" - ;; else if (!self.on_stack) - ;{}uint8 reg = self.get_norm_loc() - ;{}uint8 out = "\tadd $" - ;{}uint8 tmp = string_from_int(i) + /; init_val [{}uint8] + /; if (!self.on_stack) + ;{}uint8 out = "\tmov $0, " + ;{}uint8 reg = get_reg(8, reg_by_num(self.loc)) + ;add_strings(~out, ~reg) + ;return out + ;; else if (!self.literal) + ;{}uint8 out = "\tsub $" + ;{}uint8 tmp = string_from_int(self._type._size) ;add_strings(~out, ~tmp) ;out.append(',') ;out.append(' ') - ;add_strings(~out, ~reg) + ;tmp = "%rsp\n" + ;add_strings(~out, ~tmp) ;return out ;/ - + ;return "" + ;/ + + /; 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 + ;{}uint8 out = "\t" + ;add_strings(~out, ~op) + ;out.append(' ') + ;add_strings(~out, ~tmp) + ;out.append(',') + ;out.append(' ') + ;tmp = self.get_norm_loc() + ;add_strings(~out, ~tmp) + ;out.append('\n') + ;return out ;/ /; add_value (Value v) [{}uint8] @@ -83,66 +106,166 @@ ;self.val = self.val + v.val ;return "" ;; else if (!self.on_stack) - ;{}uint8 tmp = self.get_norm_loc() - ;{}uint8 out = "\tadd " - ;add_strings(~out, ~tmp) - ;out.append(',') - ;out.append(' ') - ;tmp = v.get_norm_loc() - ;add_strings(~out, ~tmp) - ;return out + ;return self.standard_op(v, "add") ;/ ;/ /; sub_value (Value v) [{}uint8] /; if (self.literal) - ;self.val = self.val - v.val + ;self.val = self.val + v.val ;return "" + ;; else if (!self.on_stack) + ;return self.standard_op(v, "sub") ;/ ;/ - /; mul_literal (int i) [{}uint8] - /; if (self.literal) - ;return "" - ;/ - ;/ + /; ax_compute(Value v, {}uint8 op, bool save_dx) + ;{}uint8 out = "\tpush %rax\n\tpush %rdx\n" - /; div_literal (int i) [{}uint8] + ;{}uint8 tmp = "\tmov " + ;{}uint8 t2 = v.get_norm_loc() + ;add_strings(~tmp, ~t2) + ;t2 = ", %rax\n" + ;add_strings(~tmp, ~t2) + ;add_strings(~out, ~tmp) + + ;tmp = "\txor %rdx, %rdx\n" + ;add_strings(~out, ~tmp) + + ;tmp = "\t" + ;add_strings(~tmp, ~op) + ;tmp.append(' ') + ;{}uint8 t2 = self.get_norm_loc() + ;add_strings(~tmp, ~t2) + ;tmp.append('\n') + ;add_strings(~out, ~tmp) + + /; if (save_dx) + ;tmp = "\tmov %rdx, " + ;; else + ;tmp = "\tmov %rax, " + ;/ + ;add_strings(~tmp, ~t2) + ;tmp.append('\n') + ;add_strings(~out, ~tmp) + ;tmp = "\tpop %rdx\n\tpop %rax" + ;add_strings(~out, ~tmp) + ;return out ;/ /; mul_value (Value v) [{}uint8] - + /; if (self.literal) + ;self.val = self.val + v.val + ;return "" + ;; else if (!self.on_stack) + /; if (self._type.name{0} !== 'u') + ;return self.standard_op(v, "imul") + ;/ + + ;return self.ax_compute(v, "mul", false) + ;/ ;/ - /; div_value (int i) [{}uint8] + /; div_value (Value v) [{}uint8] + /; if (self.literal) + ;self.val = self.val + v.val + ;return "" + ;; else if (!self.on_stack) + /; if (self._type.name{0} !== 'u') + ;return self.ax_compute(v, "idiv", false) + ;/ + + ;return self.ax_compute(v, "div", false) + ;/ + ;/ + /; mod_value (Value v) [{}uint8] + /; if (self.literal) + ;self.val = self.val + v.val + ;return "" + ;; else if (!self.on_stack) + /; if (self._type.name{0} !== 'u') + ;return self.ax_compute(v, "idiv", true) + ;/ + + ;return self.ax_compute(v, "div", true) + ;/ ;/ - /; deref_value [Value] + /; deref_value [{}uint8] /; if (self._type.ptr == 0) - + ;return "" ;/ + + ;Value new = self + ;new._type.ptr = new._type.ptr - 1 + ;{}uint8 out = "\tmov (" + ;{}uint8 tmp = self.get_norm_loc() + ;add_strings(~out, ~tmp) + ;out.append(')') + ;out.append(',') + ;out.append(' ') + ;tmp = new.get_norm_loc() + ;add_strings(~out, ~tmp) + + ;self = new + ;return out ;/ - /; arr_value (int i) [Value] + /; arr_value (Value index, Value store) /; if (self._type.ptr == 0) - + ;return ;/ ;/ - /; copy_from_value (Value v) - + /; set_value (Value v) [{}uint8] + /; if (self.literal) + ;self.val = v.val + ;return "" + ;; else if (!self.on_stack) + ;return self.standard_op(v, "mov") + ;/ ;/ /; get_member_value ({}uint8 name) [Value] ;Value out = self + ;out.loc = out.loc + self._type.get_offset(name) + ;out._type = self._type.get_sub_type(name) + ;return out ;/ - /; update_loc(int offset) + /; update_loc(int loc) [{}uint8] /; if (self.on_stack) - ;self.loc = self.loc + offset + ;self.loc = self.loc + loc + ;; else if (!self.literal) + ;{}uint8 out = "\tmov " + ;int tsz = self._type._size + ;self._type._size = 8 + + ;{}uint8 tmp = self.get_norm_loc() + ;add_strings(~out, ~tmp) + ;out.append(',') + ;out.append(' ') + + ;self.loc = loc + + ;{}uint8 tmp = self.get_norm_loc() + ;add_strings(~out, ~tmp) + ;out.append('\n') + + ;self._type._size = tsz + ;return out ;/ + ;return "" ;/ -;/
\ No newline at end of file +;/ + +/; literal_val (int l, VType t) [Value] + ;return {false, true, 0, l, t} +;/ + +/; track_val(VType ty, bool on_stack, int loc) [Value] + ;return {on_stack, false, loc, 0, ty} +;/ |