diff options
Diffstat (limited to 'tnslc')
| -rw-r--r-- | tnslc/compile/codegen.tnsl | 4 | ||||
| -rw-r--r-- | tnslc/compile/compile.tnsl | 1 | ||||
| -rw-r--r-- | tnslc/compile/tests_var.tnsl | 248 | ||||
| -rw-r--r-- | tnslc/compile/var.tnsl | 38 |
4 files changed, 286 insertions, 5 deletions
diff --git a/tnslc/compile/codegen.tnsl b/tnslc/compile/codegen.tnsl index 05d0fb7..636f16b 100644 --- a/tnslc/compile/codegen.tnsl +++ b/tnslc/compile/codegen.tnsl @@ -41,6 +41,7 @@ ast.end() ;/ + /; generate (~utils.File fin, fout) # Parse files into AST parse.Node ast = parse.generate_ast(fin) @@ -60,6 +61,9 @@ # Compile code mod.compile(~buffer) + # Tests + # var_tests(~buffer) + # Write assembly to output file fout.create() buffer.write_to(fout) diff --git a/tnslc/compile/compile.tnsl b/tnslc/compile/compile.tnsl index fbb9670..709dbce 100644 --- a/tnslc/compile/compile.tnsl +++ b/tnslc/compile/compile.tnsl @@ -4,6 +4,7 @@ :import "var.tnsl" :import "function.tnsl" :import "module.tnsl" + :import "tests_var.tnsl" :import "codegen.tnsl" :import "scope.tnsl" ;/ diff --git a/tnslc/compile/tests_var.tnsl b/tnslc/compile/tests_var.tnsl new file mode 100644 index 0000000..a8fd573 --- /dev/null +++ b/tnslc/compile/tests_var.tnsl @@ -0,0 +1,248 @@ + +/; var_test_mov (~Module root, ~CompBuf buf) + ~Struct _int = root`._find_struct("int\0") + + Var a + Var b + + a._init(_int) + b._init(_int) + + buf`.add_c("; test: register to register move, both int\n\0") + + a.loc = 1 + b.loc = 2 + b.set(buf, ~a) + + buf`.add_c("; test: register to stack move\n\0") + + b.loc = b.loc - 3 + b.offset = b.offset - 56 + b.set(buf, ~a) + + buf`.add_c("; test: stack to register move\n\0") + + a.set(buf, ~b) + + buf`.add_c("; test: stack to stack move\n\0") + + a.loc = a.loc - 2 + a.offset = 56 + b.set(buf, ~a) + + buf`.add_c("\n\0") + + a.end() + b.end() +;/ + +/; var_test_movsx (~Module root, ~CompBuf buf) + ~Struct _int = root`._find_struct("int\0") + ~Struct _int32 = root`._find_struct("int32\0") + ~Struct _int8 = root`._find_struct("int8\0") + + Var a + Var b + Var c + + a._init(_int) + b._init(_int32) + c._init(_int8) + + a.loc = 1 + b.loc = 2 + c.loc = 3 + + buf`.add_c("; test: mov with extension - int8 to int32\n\0") + b.set(buf, ~c) + buf`.add_c("; test: mov with extension - int8 to int\n\0") + a.set(buf, ~c) + buf`.add_c("; test: mov with extension - int32 to int\n\0") + a.set(buf, ~b) + + buf`.add_c("; test: mov with extension - int8 to int32 (stack to reg)\n\0") + c.loc = c.loc - 4 + c.offset = 4 + b.set(buf, ~c) + buf`.add_c("; test: mov with extension - int8 to int32 (reg to stack)\n\0") + c.loc = 3 + c.offset = 0 + b.loc = b.loc - 3 + b.offset = 5 + b.set(buf, ~c) + buf`.add_c("; test: mov with extension - int8 to int32 (stack to stack)\n\0") + c.loc = c.loc - 4 + c.offset = 4 + b.set(buf, ~c) + + b.loc = 2 + b.offset = 0 + c.loc = 3 + c.offset = 0 + + buf`.add_c("; test: mov with extension - int8 to int (stack to reg)\n\0") + c.loc = c.loc - 4 + c.offset = 4 + a.set(buf, ~c) + buf`.add_c("; test: mov with extension - int8 to int (reg to stack)\n\0") + c.loc = 3 + c.offset = 0 + a.loc = a.loc - 2 + a.offset = 5 + a.set(buf, ~c) + buf`.add_c("; test: mov with extension - int8 to int (stack to stack)\n\0") + c.loc = c.loc - 4 + c.offset = 4 + a.set(buf, ~c) + + a.loc = 1 + a.offset = 0 + c.loc = 3 + c.offset = 0 + + buf`.add_c("; test: mov with extension - int32 to int (stack to reg)\n\0") + b.loc = b.loc - 3 + b.offset = 4 + a.set(buf, ~b) + buf`.add_c("; test: mov with extension - int32 to int (reg to stack)\n\0") + b.loc = 2 + b.offset = 0 + a.loc = a.loc - 2 + a.offset = 5 + a.set(buf, ~b) + buf`.add_c("; test: mov with extension - int32 to int (stack to stack)\n\0") + b.loc = b.loc - 3 + b.offset = 4 + a.set(buf, ~b) + + + buf`.add_c("\n\0") + a.end() + b.end() + c.end() +;/ + +/; var_test_movzx (~Module root, ~CompBuf buf) + ~Struct _uint = root`._find_struct("uint\0") + ~Struct _uint32 = root`._find_struct("uint32\0") + ~Struct _uint8 = root`._find_struct("uint8\0") + + Var a + Var b + Var c + + a._init(_uint) + b._init(_uint32) + c._init(_uint8) + + a.loc = 1 + b.loc = 2 + c.loc = 3 + + buf`.add_c("; test: mov with extension - uint8 to uint32\n\0") + b.set(buf, ~c) + buf`.add_c("; test: mov with extension - uint8 to uint\n\0") + a.set(buf, ~c) + buf`.add_c("; test: mov with extension - uint32 to uint\n\0") + a.set(buf, ~b) + + buf`.add_c("; test: mov with extension - uint8 to uint32 (stack to reg)\n\0") + c.loc = c.loc - 4 + c.offset = 4 + b.set(buf, ~c) + buf`.add_c("; test: mov with extension - uint8 to uint32 (reg to stack)\n\0") + c.loc = 3 + c.offset = 0 + b.loc = b.loc - 3 + b.offset = 5 + b.set(buf, ~c) + buf`.add_c("; test: mov with extension - uint8 to uint32 (stack to stack)\n\0") + c.loc = c.loc - 4 + c.offset = 4 + b.set(buf, ~c) + + b.loc = 2 + b.offset = 0 + c.loc = 3 + c.offset = 0 + + buf`.add_c("; test: mov with extension - uint8 to uint (stack to reg)\n\0") + c.loc = c.loc - 4 + c.offset = 4 + a.set(buf, ~c) + buf`.add_c("; test: mov with extension - uint8 to uint (reg to stack)\n\0") + c.loc = 3 + c.offset = 0 + a.loc = a.loc - 2 + a.offset = 5 + a.set(buf, ~c) + buf`.add_c("; test: mov with extension - uint8 to uint (stack to stack)\n\0") + c.loc = c.loc - 4 + c.offset = 4 + a.set(buf, ~c) + + a.loc = 1 + a.offset = 0 + c.loc = 3 + c.offset = 0 + + buf`.add_c("; test: mov with extension - uint32 to uint (stack to reg)\n\0") + b.loc = b.loc - 3 + b.offset = 4 + a.set(buf, ~b) + buf`.add_c("; test: mov with extension - uint32 to uint (reg to stack)\n\0") + b.loc = 2 + b.offset = 0 + a.loc = a.loc - 2 + a.offset = 5 + a.set(buf, ~b) + buf`.add_c("; test: mov with extension - uint32 to uint (stack to stack)\n\0") + b.loc = b.loc - 3 + b.offset = 4 + a.set(buf, ~b) + + + buf`.add_c("\n\0") + a.end() + b.end() + c.end() +;/ + +# Test moving references +/; var_test_movref (~Module root, ~CompBuf buf) + ~Struct _int = root`._find_struct("int\0") + ~Struct _int32 = root`._find_struct("int32\0") +;/ + +# Generate a test_struct type for use in testing struct moves and sub-refs +/; _gen_test_struct(~Module m) + ~Module mds = m`._create_methods("test\0") + + Var t + + Struct s + s.size = 9 + s.methods = NULL + s.members.init(len t) + s.name = utils.strcpy("test\0") + s._up = NULL + + m`.structs.push(~s) +;/ + +/; var_tests (~CompBuf buf) + # Setup dummy root + Module mod + mod._init() + mod.name = utils.strcpy("\0") + mod.e = true + _gen_prims(~mod) + _gen_test_struct(~mod) + + var_test_mov(~mod, buf) + var_test_movsx(~mod, buf) + var_test_movzx(~mod, buf) + var_test_movref(~mod, buf) + + mod.end() +;/ diff --git a/tnslc/compile/var.tnsl b/tnslc/compile/var.tnsl index 8c6a532..aa14f2f 100644 --- a/tnslc/compile/var.tnsl +++ b/tnslc/compile/var.tnsl @@ -124,6 +124,15 @@ struct Var { # Init and copy functions # ########################### + # Dummy init + /; _init (~Struct _type) + self.name = utils.strcpy("dummy\0") + self.ptrc.init(4) + self.loc = 0 + self.offset = 0 + self._type = _type + ;/ + # Initial init function, requires type node and # identifier node /; init (~parse.Node tn, id) @@ -447,7 +456,7 @@ struct Var { # 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 ;/ @@ -527,6 +536,9 @@ struct Var { ;; else buf`.add_c(" + \0") ;/ + ~uint8 its = utils.int_to_str(o) + buf`.add_c(its) + _delete(its) ;/ buf`.add_c("] ; initial struct addr move\n\0") @@ -656,6 +668,24 @@ struct Var { /; _set_prim_r (~CompBuf buf, ~Var lhs) [~uint8] ~uint8 out = self._set_prim_l(buf) + /; if (self.in_mem() == true) + utils.Vector vout + uint ts = self.type_size() + /; if (ts == 1) + vout.from_cstr("byte \0") + ;; else if (ts == 2) + vout.from_cstr("word \0") + ;; else if (ts == 4) + vout.from_cstr("dword \0") + ;; else if (ts == 8) + vout.from_cstr("qword \0") + ;/ + vout.push_cstr(out) + + _delete(out) + out = vout.as_cstr() + ;/ + # Sign extend if required bool ext = false uint R = self.type_size() @@ -677,9 +707,7 @@ struct Var { buf`.add_c(vout) buf`.add_c(", \0") ;; else - buf`.add_c(" mov \0") - buf`.add_c(vout) - buf`.add_c(", \0") + buf`.add_c(" mov esi, \0") ;/ buf`.add_c(out) @@ -712,7 +740,7 @@ struct Var { # - If pointer then move qword # - If struct then move via rep movsb - /; if (self.is_struct() == false) + /; if (self.is_struct() == true) # Struct set self._set_struct(buf, other) return |