From 46e23fa81f651961b2388c95e9569d2d39c3cffb Mon Sep 17 00:00:00 2001 From: Kai Gunger Date: Sun, 30 Nov 2025 03:52:24 -0500 Subject: Start work on backend --- tnslc/compile/codegen.tnsl | 71 +++++++++++++++++++++++++++++++++++----- tnslc/compile/function.tnsl | 11 ++++--- tnslc/compile/module.tnsl | 35 +++++++++++++++++--- tnslc/compile/struct.tnsl | 79 +++++++++++++++++++++++++++++---------------- tnslc/compile/var.tnsl | 16 +++++++-- 5 files changed, 164 insertions(+), 48 deletions(-) (limited to 'tnslc/compile') diff --git a/tnslc/compile/codegen.tnsl b/tnslc/compile/codegen.tnsl index ccb6b06..48545f3 100644 --- a/tnslc/compile/codegen.tnsl +++ b/tnslc/compile/codegen.tnsl @@ -10,21 +10,76 @@ buffer.init() # Transform into a module tree - # Module mod - # mod.init(~ast) - # mod.update_children() + Module mod + mod.init(~ast, ~buffer) + mod.update_children() # Compile code - # mod.compile(~buffer) + _gen_prims(~mod) + mod.compile(~buffer) # Write assembly to output file - # fout.create() - # buffer.write_to(fout) - # fout.close() + fout.create() + buffer.write_to(fout) + fout.close() # Free all structs - # mod.end() + mod.end() buffer.end() ast.end() ;/ +/; _gen_prims (~Module m) + Var t + + Struct s + s.members.init(len t) + s.methods = NULL + s._up = NULL + + ~uint8 str + + # One byte prims + s.size = 1 + s.name = utils.strcpy("bool\0") + m`.structs.push(~s) + s.name = utils.strcpy("uint8\0") + m`.structs.push(~s) + s.name = utils.strcpy("int8\0") + m`.structs.push(~s) + + # Two byte prims + s.size = 2 + s.name = utils.strcpy("uint16\0") + m`.structs.push(~s) + s.name = utils.strcpy("int16\0") + m`.structs.push(~s) + + # Four byte prims + s.size = 4 + s.name = utils.strcpy("uint32\0") + m`.structs.push(~s) + s.name = utils.strcpy("int32\0") + m`.structs.push(~s) + s.name = utils.strcpy("float32\0") + m`.structs.push(~s) + + # Eight byte prims + s.size = 8 + s.name = utils.strcpy("uint64\0") + m`.structs.push(~s) + s.name = utils.strcpy("int64\0") + m`.structs.push(~s) + s.name = utils.strcpy("float64\0") + m`.structs.push(~s) + s.name = utils.strcpy("uint\0") + m`.structs.push(~s) + s.name = utils.strcpy("int\0") + m`.structs.push(~s) + s.name = utils.strcpy("float\0") + m`.structs.push(~s) + s.name = utils.strcpy("void\0") + m`.structs.push(~s) + +;/ + diff --git a/tnslc/compile/function.tnsl b/tnslc/compile/function.tnsl index 937e668..4f7615b 100644 --- a/tnslc/compile/function.tnsl +++ b/tnslc/compile/function.tnsl @@ -4,23 +4,26 @@ struct Function { utils.Vector inputs, outputs, - ~parse.Node body + ~parse.Node _up, } /; method Function - /; init (~uint8 name) - self.name = name + /; init (~parse.Node n) + self.name = utils.strcpy(n`.data) + self._up = n Var v self.inputs.init(len v) self.outputs.init(len v) ;/ + /; _resolve_type (~Module parent) + ;/ + /; _compile (~Module parent, ~CompBuf cb) ;/ /; end _delete(self.name) - self.body`.end() ~Var v /; loop (int i = 0; i < self.inputs.count) [i++] diff --git a/tnslc/compile/module.tnsl b/tnslc/compile/module.tnsl index fd44b07..6aa6149 100644 --- a/tnslc/compile/module.tnsl +++ b/tnslc/compile/module.tnsl @@ -24,7 +24,7 @@ struct Module { /; method Module - /; init (~parse.Node mod) + /; init (~parse.Node mod, ~CompBuf buf) Var v Struct s Function f @@ -38,7 +38,7 @@ struct Module { self.name = utils.strcpy(mod`.data) self.e = mod`._type == parse.NTYPE_EXPORT - self._from_tree(mod) + self._from_tree(mod, buf) ~Module sub /; loop (int i = 0; i < self.subs.count) [i++] @@ -55,16 +55,20 @@ struct Module { ;/ ;/ - /; _from_tree (~parse.Node mod) + /; _from_tree (~parse.Node mod, ~CompBuf buf) ~parse.Node sub /; loop (int i = 0; i < mod`.sub.count) [i++] sub = mod`.sub.get(i) # TODO: Vars, Enums, Method blocks - /; if (sub`._type == parse.NTYPE_MOD || sub`._type == parse.NTYPE_EXPORT) + /; if (sub`._type == parse.NTYPE_MODULE) Module m - m.init(sub) + m.init(sub, buf) + self.subs.push(~m) + ;; else if (sub`._type == parse.NTYPE_EXPORT) + Module m + m.init(sub, buf) self.subs.push(~m) ;; else if (sub`._type == parse.NTYPE_STRUCT) Struct s @@ -74,6 +78,9 @@ struct Module { Function f f.init(sub) self.funcs.push(~f) + ;; else if (sub`._type == parse.NTYPE_ASM) + buf`.add_h(sub`.data) + buf`.add_h("\n\0") ;/ ;/ ;/ @@ -95,6 +102,18 @@ struct Module { ;/ ;/ + ~Var v + /; loop (int i = 0; i < self.vars.count) [i++] + v = self.vars.get(i) + v`._resolve_type(~self) + ;/ + + ~Function f + /; loop (int i = 0; i < self.funcs.count) [i++] + f = self.funcs.get(i) + f`._resolve_type(~self) + ;/ + ~Module m /; loop (int i = 0; i < self.subs.count) [i++] m = self.subs.get(i) @@ -103,6 +122,12 @@ struct Module { ;/ /; _compile (~CompBuf cb) + ~Var v + /; loop (int i = 0; i < self.vars.count) [i++] + v = self.vars.get(i) + v`._static_compile(~self, cb) + ;/ + ~Function f /; loop (int i = 0; i < self.funcs.count) [i++] f = self.funcs.get(i) diff --git a/tnslc/compile/struct.tnsl b/tnslc/compile/struct.tnsl index 79d3267..5403553 100644 --- a/tnslc/compile/struct.tnsl +++ b/tnslc/compile/struct.tnsl @@ -8,10 +8,10 @@ struct Struct { ~parse.Node _up } -~uint8 PRIMITIVE_1 = "bool,uint8,int8" -~uint8 PRIMITIVE_2 = "uint16,int16" -~uint8 PRIMITIVE_4 = "uint32,int32,float32" -~uint8 PRIMITIVE_8 = "uint64,int64,float64,int,uint,float,void" +~uint8 PRIMITIVE_1 = "bool,uint8,int8\0" +~uint8 PRIMITIVE_2 = "uint16,int16\0" +~uint8 PRIMITIVE_4 = "uint32,int32,float32\0" +~uint8 PRIMITIVE_8 = "uint64,int64,float64,int,uint,float,void\0" /; is_primitive (~parse.Node tn) [int] /; if (tn`.sub.count < 1) @@ -25,17 +25,6 @@ struct Struct { return 8 ;/ - # Check for prim type - /; if (parse._in_csv(PRIMITIVE_1, n`.data) == true) - return 1 - ;; else if (parse._in_csv(PRIMITIVE_2, n`.data) == true) - return 2 - ;; else if (parse._in_csv(PRIMITIVE_4, n`.data) == true) - return 4 - ;; else if (parse._in_csv(PRIMITIVE_8, n`.data) == true) - return 8 - ;/ - # Check for ref n = tn`.sub.get(tn`.sub.count - 1) /; if (n`._type == parse.NTYPE_POST_OP) @@ -45,6 +34,23 @@ struct Struct { return 0 ;/ +/; _print_type(~parse.Node tn) + ~parse.Node n + /; loop (int i = 0; i < tn`.sub.count) [i++] + /; if (i > 0) + _printf(".\0") + ;/ + + n = tn`.sub.get(i) + + /; if (n`._type == parse.NTYPE_ID) + _printf(n`.data) + ;; else + return + ;/ + ;/ +;/ + /; method Struct /; init (~parse.Node node) @@ -101,17 +107,30 @@ struct Struct { n = up`.sub.get(i) /; if (n`._type == parse.NTYPE_TYPE) - # Check for primitive type - add_size = is_primitive(n) - /; if (add_size == 0) - # Find type, compute size, set add_size to type size - ~Struct ft = self._find_type(parent, n) - /; if (ft`.size < 0) - # Cyclical dependency - return - ;/ - add_size = ft`.size + # Find type, compute size, set add_size to type size + ~Struct ft = self._find_type(parent, n) + + /; if (ft == NULL) + # Type not found + _printf("ERROR: Unable to find type '\0") + _print_type(n) + _printf("' for use in struct '\0") + _printf(self.name) + _printf("'\n\0") + return + ;/ + + /; if (ft`.size == 0) + # Recurse + ft`._compute_size(parent) + ;/ + + /; if (ft`.size < 0) + # Cyclical dependency + return ;/ + + add_size = ft`.size ;; else if (n`._type == parse.NTYPE_ID) total = total + add_size ;/ @@ -121,17 +140,22 @@ struct Struct { ;/ /; _find_type (~Module parent, ~parse.Node tn) [~Struct] - # First loop through all the names and create a vector of strings + + # Init vector of strings utils.Vector sv sv.init(8) + ~void str ~parse.Node n + bool seen_id = false + /; loop (int i = 0; i < tn`.sub.count) [i++] n = tn`.sub.get(i) /; if (n`._type == parse.NTYPE_ID) str = n`.data sv.push(~str) - ;; else + seen_id = true + ;; else if (seen_id == true) i = tn`.sub.count ;/ ;/ @@ -144,7 +168,6 @@ struct Struct { outp = parent.find(SEARCH_SUB, ~sv) ;/ sv.end() - out._compute_size(outp) return out ;/ diff --git a/tnslc/compile/var.tnsl b/tnslc/compile/var.tnsl index 320c3a1..9a07aff 100644 --- a/tnslc/compile/var.tnsl +++ b/tnslc/compile/var.tnsl @@ -94,13 +94,23 @@ struct Var { ~uint8 name, ~Struct _type, utils.Vector ptrc, - int loc + int loc, + + ~parse.Node _up } /; method Var - /; init (~uint8 name) - self.name = name + /; init (~parse.Node n) + self.name = utils.strcpy(n`.data) self.ptrc.init(4) + self._up = n + ;/ + + /; _resolve_type (~Module parent) + ;/ + + /; _static_compile (~Module parent, ~CompBuf buf) + # TODO: ;/ /; ptr [int32] -- cgit v1.2.3