summaryrefslogtreecommitdiff
path: root/tnslc
diff options
context:
space:
mode:
authorCircleShift <kgunger12@gmail.com>2025-12-09 02:25:30 -0500
committerCircleShift <kgunger12@gmail.com>2025-12-09 02:25:30 -0500
commit0587f854d80a9ad95c459b45509fcda7926cde20 (patch)
tree2b3e0d2f2cbfa541bb4d9bf2e2c7f5af5bcaafdc /tnslc
parentde71ecd4d3f52deb6f854540f42f931b2e55055c (diff)
begin on the actual codegenorigin
Diffstat (limited to 'tnslc')
-rw-r--r--tnslc/compile/function.tnsl30
-rw-r--r--tnslc/compile/module.tnsl2
-rw-r--r--tnslc/compile/scope.tnsl13
-rw-r--r--tnslc/compile/var.tnsl114
4 files changed, 129 insertions, 30 deletions
diff --git a/tnslc/compile/function.tnsl b/tnslc/compile/function.tnsl
index d55579e..eec65ed 100644
--- a/tnslc/compile/function.tnsl
+++ b/tnslc/compile/function.tnsl
@@ -161,7 +161,35 @@ struct Function {
self._end_func(~fscope, cb)
;/
- /; _compile_statements(~Scope s, int off)
+ #
+ # Compiling individual statements
+ #
+
+ /; _compile_statements (~Scope s, int off)
+ ~parse.Node _up = self._up
+ ~parse.Node n = NULL
+ /; loop (off < _up`.sub.count) [off++]
+ n = _up`.sub.get(off)
+ /; if (n`._type == parse.NTYPE_FLOW_CONTROL)
+ self._compile_flow_control(s, n)
+ ;; else if (n`._type == parse.NTYPE_ASM)
+ s`.cb`.add_c(" \0")
+ s`.cb`.add_c(n`.data)
+ s`.cb`.add_c(" ; User defined asm\n\0")
+ ;/
+ ;/
+ ;/
+
+ # Should handle break, continue, and return
+ /; _compile_flow_control (~Scope s, ~parse.Node n)
+ /; if (utils.strcmp(n`.data, "return\0") == true)
+ # Compute value and return
+ self._compile_value(s, n)
+ ;/
+ ;/
+
+ # Should handle computing a value, delegate to other funcs when needed
+ /; _compile_value (~Scope s, ~parse.Node n)
;/
/; _print (int idt)
diff --git a/tnslc/compile/module.tnsl b/tnslc/compile/module.tnsl
index a60533a..e46c219 100644
--- a/tnslc/compile/module.tnsl
+++ b/tnslc/compile/module.tnsl
@@ -187,7 +187,7 @@ struct Module {
~Function f
/; loop (int i = 0; i < self.funcs.count) [i++]
f = self.funcs.get(i)
- # f`._compile(~self, cb)
+ f`._compile(~self, cb)
;/
~Module m
diff --git a/tnslc/compile/scope.tnsl b/tnslc/compile/scope.tnsl
index 6e4d44d..1afaeab 100644
--- a/tnslc/compile/scope.tnsl
+++ b/tnslc/compile/scope.tnsl
@@ -106,8 +106,8 @@ struct Scope {
;/
/; mk_set_var (~Var src)
- Var out
-
+ Var out = src`.copy()
+
/; if (src`.is_reg_passed() == true)
out.loc = self._next_reg_slot()
/; if (out.loc == 0)
@@ -117,6 +117,15 @@ struct Scope {
out.loc = self._next_stack_slot()
;/
+ ~int32 p = out.top_ptrc()
+ /; if (p == NULL)
+ out.set(self.cb, src)
+ ;; else if (p` == 0)
+ out.set_ref(self.cb, src)
+ ;; else
+ out.set(self.cb, src)
+ ;/
+
self.vars.push(~out)
;/
diff --git a/tnslc/compile/var.tnsl b/tnslc/compile/var.tnsl
index 69108ce..9748ed4 100644
--- a/tnslc/compile/var.tnsl
+++ b/tnslc/compile/var.tnsl
@@ -113,6 +113,29 @@ struct Var {
self._tn = tn
self._id = id
;/
+
+ /; copy [Var]
+ Var out
+ out.init(self._tn, self._id)
+ out._type = self._type
+ out.loc = self.loc
+
+ /; loop (int i = 0; i < self.ptrc.count) [i++]
+ ~int32 p = self.ptrc.get(i)
+ out.ptrc.push(p)
+ ;/
+
+ return out
+ ;/
+
+ /; top_ptrc [~int32]
+ # Sanity
+ /; if (self.ptrc.count < 1)
+ return NULL
+ ;/
+ ~int32 out = self.ptrc.get(self.ptrc.count - 1)
+ return out
+ ;/
/; _print (int idt)
_indent(idt)
@@ -171,6 +194,18 @@ struct Var {
return self._type`.size
;/
+ /; _reverse_ptrc
+ int max = self.ptrc.count / 2
+ ~int32 l, r
+ /; loop (int i = 0; i < max) [i++]
+ l = self.ptrc.get(i)
+ r = self.ptrc.get(self.ptrc.count - (i + 1))
+ int32 tmp = l`
+ l` = r`
+ r` = tmp
+ ;/
+ ;/
+
# Sets up both the ptrc and the _type members, requires
# parent module for resolution of types
/; _resolve_type (~Module parent)
@@ -203,6 +238,8 @@ struct Var {
;/
;/
+ self._reverse_ptrc()
+
# After pre-ops comes id
utils.Vector strv
strv.init(8)
@@ -290,29 +327,54 @@ struct Var {
# Variable manipulation functions #
###################################
- # Generate a string which represents where the variable is in memory,
- # this string may be used to set the value of the variable with operations like "mov"
- # if "maybe_mem" is true, this might be an address like "[rsi]"
- /; gen_to (bool maybe_mem) [~uint8]
+ /; gen_loc [~uint8]
utils.Vector out
out.init(1)
- return out.as_cstr()
- ;/
- # Generate a string which represents where the variable is in memory,
- # this string may be used to read the value of the variable with operations like "mov"
- # if "maybe_mem" is true, this might be an address like "[rsi]"
- /; gen_from (bool maybe_mem) [~uint8]
- utils.Vector out
- out.init(1)
+ /; if (self.in_mem() == true)
+ out.push_char('[')
+ ;/
+
+ ~uint8 str
+ /; if (self.loc < 1)
+ str = reg_string(7, 8)
+ ;; else
+ str = reg_string(self.loc, self.actual_size())
+ ;/
+ out.push_cstr(str)
+ _delete(str)
+
+
+ /; if (self.in_mem() == true)
+ /; if (self.loc < 0)
+ int stk = 0
+ stk = stk - self.loc
+ out.push_cstr(" + \0")
+ str = utils.int_to_str(stk)
+ out.push_cstr(str)
+ _delete(str)
+ ;/
+ out.push_char(']')
+ ;/
+
return out.as_cstr()
;/
- # Returns true if the variable is stored in memory
+ # Returns true if the variable is known to be stored in memory
/; in_mem [bool]
- /; if (self.loc !> 0)
+ /; if (self.loc < 1)
return true
;/
+
+ ~int32 ptr = self.top_ptrc()
+ /; if (ptr !== NULL)
+ /; if (ptr` == 0)
+ return true
+ ;; else if (ptr` > 1)
+ return true
+ ;/
+ ;/
+
return false
;/
@@ -320,20 +382,24 @@ struct Var {
/; set_literal (~CompBuf buf, ~Var other)
;/
+ /; _set_mem(~CompBuf buf, ~Var other)
+
+ ;/
+
# Set this Variable to the value of other
/; set (~CompBuf buf, ~Var other)
+ self.standard_op(buf, other, "mov\0")
;/
- /; standard_op (~CompBuf buf, ~Var other, ~uint8 op_str)
- ~uint8 from_str
- ~uint8 to_str = self.gen_to(true)
+ # Set the address which this reference points to
+ /; set_ref(~CompBuf buf, ~Var other)
+ ;/
- /; if (self.in_mem())
- from_str = other`.gen_from(false)
- ;; else
- from_str = other`.gen_from(true)
- ;/
+ /; standard_op (~CompBuf buf, ~Var other, ~uint8 op_str)
+ ~uint8 from_str = other`.gen_loc()
+ ~uint8 to_str = self.gen_loc()
+ buf`.add_c(" \0")
buf`.add_c(op_str)
buf`.add_c(" \0")
buf`.add_c(to_str)
@@ -391,10 +457,6 @@ struct Var {
self.standard_op(buf, other, "xor")
;/
- /; not (~CompBuf buf, ~Var other)
- self.standard_op(buf, other, "xor")
- ;/
-
/; member (~CompBuf buf, ~uint8 name) [Var]
Var out
return out