From 3c6707a09a90f461fe3c002ef4f55a0d072e7541 Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Tue, 21 Feb 2023 19:03:00 -0500 Subject: Basic return values (globals and constants) --- tnslc/tnslc.tnsl | 193 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 145 insertions(+), 48 deletions(-) (limited to 'tnslc/tnslc.tnsl') diff --git a/tnslc/tnslc.tnsl b/tnslc/tnslc.tnsl index d65ddb9..6345bc7 100644 --- a/tnslc/tnslc.tnsl +++ b/tnslc/tnslc.tnsl @@ -409,8 +409,7 @@ # Get common register by common name and size /; reg_by_name_size ({}uint8 common, uint sz) [{}uint8] - ;{}uint8 out = "%" - + ;{}uint8 out = "" /; if (common{0} !< 'a') /; if (sz == 1) @@ -425,12 +424,12 @@ ;out.append('r') ;/ - ;string_add(out, common) + ;out = string_add(out, common) ;; else ;out.append('r') - ;string_add(out, common) + ;out = string_add(out, common) /; if (sz == 1) ;out.append('b') ;; else if (sz == 2) @@ -438,7 +437,6 @@ ;; else if (sz == 4) ;out.append('d') ;/ - ;return out ;/ ;return out @@ -463,7 +461,7 @@ /; label_to_loc({}uint8 l, int sz, offset) [{}uint8] ;{}uint8 off = "" - /; if (off !== 0) + /; if (offset !== 0) ;off = string_add(" + ", int_to_string(offset)) ;/ ;return string_join( { "[rel ", l, off, "]" }, "") @@ -480,6 +478,8 @@ ;return get_reg(self.location, sz) ;; else if (self.loc_type == LOCATION.STACK) ;return string_join( { "[ rbp - ", int_to_string(self.location), " ]" }, "") + ;; else if (self.loc_type == LOCATION.LITERAL) + ;return int_to_string(self.location) ;/ ;/ @@ -515,7 +515,7 @@ "\tmov ", get_reg(0, self.norm_size()), ", ", v.norm_loc(self.norm_size()), "\n", "\tadd ", self.norm_loc(self.norm_size()), ", ", get_reg(0, self.norm_size()), "\n" }, "") - ;; + ;; else ;dat`.csec = string_add(dat`.csec, self.norm_op("add", { self.norm_loc(self.norm_size()), get_reg(tr, self.norm_size()) })) ;/ ;/ @@ -541,26 +541,43 @@ /; div (Variable v, ~CompData dat) /; if (self.loc_type == LOCATION.LITERAL) /; if (v.loc_type !== LOCATION.LITERAL) - ;return v.div(self) + ;v.div(self) + ;; else + ;self.location = self.location / v.location ;/ - ;self.location = self.location + v.location - ;return "" - ;; if (self.loc_type == LOCATION.STACK && v.loc_type == LOCATION.STACK) + ;; else if (self.loc_type == LOCATION.STACK && v.loc_type == LOCATION.STACK) + ;{}uint8 out = "" + # TODO + ;return out + ;; else + ;return self.norm_op("div", { self.norm_loc(self.norm_size()), v.norm_loc(self.norm_size()) }) + ;/ + ;/ + + /; mod (Variable v, ~CompData dat) + /; if (self.loc_type == LOCATION.LITERAL) + /; if (v.loc_type !== LOCATION.LITERAL) + ;v.div(self) + ;; else + ;self.location = self.location % v.location + ;/ + ;; else if (self.loc_type == LOCATION.STACK && v.loc_type == LOCATION.STACK) ;{}uint8 out = "" # TODO ;return out + ;; else + ;return self.norm_op("div", { self.norm_loc(self.norm_size()), v.norm_loc(self.norm_size()) }) ;/ - ;return self.norm_op("div", { self.norm_loc(self.norm_size()), v.norm_loc(self.norm_size) }) ;/ /; mul (Variable v, ~CompData dat) /; if (self.loc_type == LOCATION.LITERAL) /; if (v.loc_type !== LOCATION.LITERAL) - ;return v.mul(self) + ;v.mul(self) + ;; else + ;self.location = self.location * v.location ;/ - ;self.location = self.location * v.location - ;return "" - ;; if (self.loc_type == LOCATION.STACK && v.loc_type == LOCATION.STACK) + ;; else if (self.loc_type == LOCATION.STACK && v.loc_type == LOCATION.STACK) ;{}uint8 out = "" # TODO ;return out @@ -571,9 +588,10 @@ /; set (Variable v, ~CompData dat) /; if (self.loc_type == LOCATION.LITERAL) /; if (v.loc_type !== LOCATION.LITERAL) - ;return v.set(self) + ;v.set(self, dat) + ;; else + ;self.location = v.location ;/ - ;self.location = v.location ;; else if (self.loc_type == LOCATION.STACK && v.loc_type == LOCATION.STACK) ;dat`.csec = string_join( { dat`.csec, @@ -583,7 +601,7 @@ "\trep movsb\n" }, "") ;; else - ;dat`.csec = string_add(dat`.csec, self.norm_op("mov", { self.norm_loc(self.norm_size()), v.norm_loc(self.norm_size) })) + ;dat`.csec = string_add(dat`.csec, self.norm_op("mov", { self.norm_loc(self.norm_size()), v.norm_loc(self.norm_size()) })) ;/ ;/ @@ -607,7 +625,7 @@ ;struct Scope { {}int level, - int num, + int num, c, ~Scope parent, {}uint8 name, {}Variable vars @@ -762,7 +780,7 @@ ;cf = string_add(cf, int_to_string(s{i})) ;/ - ;return {s, 0, ~self, cf, {}} + ;return {s, 0, 0, ~self, cf, {}} ;/ /; get_continue (uint i) [{}uint8] @@ -812,11 +830,20 @@ ;/ ;/ - /; if (!self.is_cf()) + /; if (!(self.is_cf())) ;return current`.find_def(artifact) ;/ ;return self.parent`.find_var(artifact, current) ;/ + + /; next_const [{}uint8] + /; if (self.is_cf()) + ;return self.parent`.next_const() + ;/ + ;{}uint8 out = string_add(self.name, string_add("_#const_", int_to_string(self.c))) + ;self.c++ + ;return out + ;/ ;/ ;struct Function { @@ -919,7 +946,7 @@ ;/ /; find_def ({}{}uint8 artifact) [Variable] - ;return _find_def(artifact, 0) + ;return self._find_def(artifact, 0) ;/ /; _find_function ({}{}uint8 artifact, int r) [Variable] @@ -1027,10 +1054,9 @@ ;return out ;/ -/; is_call(~{}Token tok, ~int cur) [bool] - ;int i = cur` - ;get_artifact(tok, cur) - ;return tok`{cur`}.cmp("(") +/; is_call(~{}Token tok, int i) [bool] + ;get_artifact(tok, ~i) + ;return tok`{i}.cmp("(") ;/ /; get_type (~{}Token tok, ~int cur, ~Module current) [Type] @@ -1397,6 +1423,22 @@ ;/ ;/ +# Counts number of components in a composite value +/; count_comp_el (~{}Token tok, int start) [int] + ;int max = find_closing(tok, ~start) + ;int out = 0 + + /; loop (start++; start < max) [start++] + /; if (tok`{start}.type_is(TOKEN.DELIMITER)) + ;start = find_closing(tok, ~start) + ;; else if (tok`{start}.cmp(",")) + ;out++ + ;/ + ;/ + + ;return out +;/ + # Priority # 0 - deref # 1 - get @@ -1448,9 +1490,16 @@ ;return 999 ;/ -/; _eval_dot (~{}Token tok, int start, ~CompData out, ~Module current, ~Scope scope, Type t, int loc) [Variable] +/; _eval_dot (~{}Token tok, int start, ~CompData out, ~Module current, ~Scope scope, Type t, bool alt) [Variable] ;Variable wk = scope.find_var(get_artifact(tok, ~start), current) + + ;int loc = 1 + /; if (alt) + ;loc = 2 + ;/ + ;Variable refer = {"#tmp", wk.data_type, loc, LOCATION.REGISTER} + ;refer.set(wk, out) /; loop (start < len tok`) [start++] /; if (tok`{start}.cmp("`")) @@ -1466,15 +1515,44 @@ ;/ +/; literal_variable ({}uint8 data, l, ~CompData out) [Variable] + ;Variable v = {"#literal", NO_TYPE, 0, LOCATION.LITERAL} + /; if (data{0} == '"') + # TODO: String literals + ;out`.dsec = string_join({ + out`.dsec, + l, ":\n", + declare_size(8), int_to_string(len unquote_str(data)), "\n", + declare_size(1), data, "\n"} + , "") + ;v.data_type = {1, "uint8", "", {POINTER.ARRAY}, {}} + ;v.loc_type = LOCATION.LABEL + ;v.name = l + ;; else if (data{0} == '\'') + ;v.data_type = {1, "uint8", "", {}, {}} + ;v.location = unqote_char(tok`{start}.data) + ;; else if (string_equate(data, "true")) + ;v.data_type = {1, "bool", "", {}, {}} + ;v.location = 1 + ;; else if (string_equate(data, "false")) + ;v.data_type = {1, "bool", "", {}, {}} + ;v.location = 0 + ;; else + ;v.data_type = {8, "int", "", {}, {}} + ;v.location = string_to_int(data) + ;/ + ;return v +;/ + # FIXME: # Need to impliment in place solving # Need to impliment auto typing -/; _eval_value(~{}Token tok, int start, max, ~CompData out, ~Module current, ~Scope scope, Type t) [Variable] +/; _eval_value(~{}Token tok, int start, max, ~CompData out, ~Module current, ~Scope scope, Type t, bool alt) [Variable] /; if (start == max - 1) /; if (tok`{start}.type_is(TOKEN.LITERAL)) - /; if (tok`{start}.data{0} == '"') - - ;; else if (tok`{start}.data{0} == '"') + ;{}uint8 l = string_add(current`.full_path(), ".") + ;l = string_add(l, scope`.next_const()) + ;return literal_variable(tok`{start}.data, l, out) ;/ ;/ @@ -1489,27 +1567,44 @@ ;/ ;/ - ;return {"", NO_TYPE, 0, 0} - # This is all kinda garbage, to fix. - /; if (pr == 0 || pr == 1) - /; if (pa > 0 && tok`{max - 1}.cmp(")")) - ;return _eval_call(tok, start, max, out, current, scope, t, loc) - ;/ + /; if (pr == -1) + ;return scope`.find_var( { tok`{start}.data }, current ) + ;; else if (is_call(tok, start)) + ;return _eval_call(tok, start, max, out, current, scope, t, loc) + ;; else if (pr == 0 || pr == 1) ;return _eval_dot(tok, start, out, current, scope, t, loc) ;/ - ;loc = loc % 8 - - ;Type s1, s2 + ;Variable s1, s2 /; if (first == start) - ;s1 = _eval_value(tok, first + 1, max, out, current, scope, NO_TYPE, loc) + ;s1 = _eval_value(tok, first + 1, max, out, current, scope, NO_TYPE, alt) ;; else if (first == max - 1) - ;s1 = _eval_value(tok, start, first, out, current, scope, NO_TYPE, loc) + ;s1 = _eval_value(tok, start, first, out, current, scope, NO_TYPE, alt) ;; else - ;s1 = _eval_value(tok, start, first, out, current, scope, NO_TYPE, loc) - ;s1 = _eval_value(tok, first + 1, max, out, current, scope, NO_TYPE, loc + 1) + ;s1 = _eval_value(tok, start, first, out, current, scope, NO_TYPE, !alt) + ;s2 = _eval_value(tok, first + 1, max, out, current, scope, NO_TYPE, alt) + ;/ + + /; if (tok`{first}.cmp("=")) + ;s1.set(s2, out) + ;; else if (tok`{first}.cmp("+")) + ;s1.add(s2, out) + ;; else if (tok`{first}.cmp("-")) + ;s1.sub(s2, out) + ;; else if (tok`{first}.cmp("*")) + ;s1.mul(s2, out) + ;; else if (tok`{first}.cmp("/")) + ;s1.div(s2, out) + ;; else if (tok`{first}.cmp("%")) + ;s1.mod(s2, out) + ;; else if (tok`{first}.cmp("`")) + ;s1.deref(out) + ;; else if (tok`{first}.cmp("~")) + ;s1.ref(out) ;/ + + ;return s1 ;/ # ALWAYS put the value in rax @@ -1522,8 +1617,9 @@ ;end = find_closing(tok, ~end) ;/ ;/ - ;_eval_value(tok, cur`, end, out, current, scope, t) - ;Variable set = {"#tmp", } + ;Variable val = _eval_value(tok, cur`, end, out, current, scope, t, false) + ;Variable set = {"#tmp", t, 0, LOCATION.REGISTER} + ;set.set(val, out) ;cur` = end ;/ @@ -1569,7 +1665,7 @@ ;/ /; compile_block (~{}Token tok, ~int cur, ~Module current, ~CompData out) - ;Scope root = {{}, 0, 0, "", {}} + ;Scope root = {{}, 0, 0, 0, "", {}} ;int max = find_closing(tok, cur) ;bool r = false, m = false, returned = false ;Type ret = NO_TYPE @@ -1593,6 +1689,7 @@ ;; if (tok`{cur`}.cmp("(")) ;root.vars = parse_param_list(tok, cur, current) ;; if (tok`{cur`}.cmp("[")) + ;cur`++ ;ret = get_type(tok, cur, current) ;; if (tok`{cur`}.cmp("\n")) ;break -- cgit v1.2.3