From e25b738656928b38781458ed85880e8ae7bdd5a6 Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Thu, 14 Mar 2024 01:11:23 -0400 Subject: Test vector impl --- compiler.c | 16 ++++++++-------- tests/test_funcall_5.tnsl | 12 ++++++++++++ tnslc/build.sh | 2 +- tnslc/main.tnsl | 13 +++++-------- tnslc/vector.tnsl | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 tests/test_funcall_5.tnsl create mode 100644 tnslc/vector.tnsl diff --git a/compiler.c b/compiler.c index 9901e2e..9d66258 100644 --- a/compiler.c +++ b/compiler.c @@ -1846,7 +1846,7 @@ void *mod_find_rec(Module *mod, Artifact *art, size_t sub, int find_type) { char **to_check = vect_get(art, sub); Vector e_check = vect_from_string("@@"); // In case it is a variable inside an enum - Vector t_check = vect_from_string("@"); // In case it is a function inside a method block + Vector t_check = vect_from_string("_#"); // In case it is a function inside a method block vect_push_string(&e_check, *to_check); vect_as_string(&e_check); vect_push_string(&t_check, *to_check); @@ -1939,7 +1939,7 @@ void mod_full_path_rec(Module *m, Vector *v) { mod_full_path_rec(m->parent, v); char dot = '.'; - if(v->count > 0 || (v->count == 0 && m->name[0] == '@')) + if(v->count > 0) vect_push(v, &dot); vect_push_string(v, m->name); } @@ -3094,7 +3094,7 @@ void p1_parse_method(Module *root, Vector *tokens, size_t *pos) { return; } - Vector mod_name = vect_from_string("@"); + Vector mod_name = vect_from_string("_#"); vect_push_string(&mod_name, t->data); Module out = mod_init(vect_as_string(&mod_name), root, root->exported); vect_end(&mod_name); @@ -3290,7 +3290,7 @@ void p1_size_type (Module *root, Type *t) { int sum = 0; t->size = -1; - Vector tmp = vect_from_string("@"); + Vector tmp = vect_from_string("_#"); vect_push_string(&tmp, t->name); t->module = mod_find_sub(root, vect_as_string(&tmp)); vect_end(&tmp); @@ -3349,7 +3349,7 @@ void p1_size_type (Module *root, Type *t) { void p1_resolve_func_types(Module *root, Function *func) { - bool method = root->name != NULL && strlen(root->name) > 0 && root->name[0] == '@'; + bool method = root->name != NULL && strlen(root->name) > 1 && root->name[0] == '_' && root->name[1] == '#'; int reg = 1; if (method) @@ -5036,7 +5036,7 @@ void p2_compile_control(Scope *s, CompData *out, Vector *tokens, size_t *pos, Ve void _p2_handle_method_scope(Module *root, CompData *out, Scope *fs, Function *f) { // load type for method - Artifact t_art = art_from_str((root->name + 1), '.'); + Artifact t_art = art_from_str((root->name + 2), '.'); Type *t = mod_find_type(root, &t_art); art_end(&t_art); @@ -5148,7 +5148,7 @@ void p2_compile_function(Module *root, CompData *out, Vector *tokens, size_t *po // Scope init Scope fs = scope_init(t->data, root); _p2_func_scope_init(root, out, &fs, f, &p_list); - if(root->name != NULL && strlen(root->name) > 0 && root->name[0] == '@') { + if(root->name != NULL && strlen(root->name) > 1 && root->name[0] == '_' && root->name[1] == '#') { _p2_handle_method_scope(root, out, &fs, f); } @@ -5253,7 +5253,7 @@ void p2_compile_method(Module *root, CompData *out, Vector *tokens, size_t *pos) return; } - Vector sub_name = vect_from_string("@"); + Vector sub_name = vect_from_string("_#"); vect_push_string(&sub_name, t->data); // TODO: method loop diff --git a/tests/test_funcall_5.tnsl b/tests/test_funcall_5.tnsl new file mode 100644 index 0000000..48bd4fe --- /dev/null +++ b/tests/test_funcall_5.tnsl @@ -0,0 +1,12 @@ +/; f_a (int a) [int] + return a +;/ + +/; f_b (int a, ~int b) [int] + return a + b` +;/ + +/; main [int] + int a = 60 + return f_b(f_a(1) * 9, ~a) +;/ diff --git a/tnslc/build.sh b/tnslc/build.sh index b492dcd..798e77d 100755 --- a/tnslc/build.sh +++ b/tnslc/build.sh @@ -3,5 +3,5 @@ ../ctc main.tnsl tnslc.asm nasm -f elf64 -o tnslc.o tnslc.asm gcc -o tnslc tnslc.o -rm tnslc.asm tnslc.o +# rm tnslc.asm tnslc.o diff --git a/tnslc/main.tnsl b/tnslc/main.tnsl index 1abedb3..ac56836 100644 --- a/tnslc/main.tnsl +++ b/tnslc/main.tnsl @@ -1,14 +1,11 @@ :import "c_wrap_linux.tnsl" +:import "vector.tnsl" -~uint8 str_a = "Hello World\n\0" -~uint8 str_b = "!\n\0" +/; main [int] -/; main (int argc, ~~uint8 argv) [int] - _printf(str_a) + Vector a + a.init(1) - /; if (argc > 2) - _printf(str_b) - ;/ - return 0 ;/ + diff --git a/tnslc/vector.tnsl b/tnslc/vector.tnsl new file mode 100644 index 0000000..1377938 --- /dev/null +++ b/tnslc/vector.tnsl @@ -0,0 +1,32 @@ +struct Vector { + ~void data, + int + size, + count, + _elsz +} + +int VECT_DEFAULT_SIZE = 4 + +/; method Vector + + /; init (int elsz) + self._elsz = elsz + self.size = VECT_DEFAULT_SIZE + self.data = _alloc(elsz * self.size) + self.count = 0 + ;/ + + /; get (int index) [~void] + return self.data + ;/ + + /; end + _delete(self.data) + self.size = 0 + self._elsz = 0 + self.count = 0 + ;/ + +;/ + -- cgit v1.2.3 From 7b009880ad7a4182d3fa8d84468526d0d30d8844 Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Fri, 15 Mar 2024 17:27:21 -0400 Subject: Slightly better function call --- compiler.c | 19 +++++++++------ tests/run.sh | 9 ++++++++ tests/test_controll.tnsl | 12 ---------- tests/test_method_2.tnsl | 2 +- tnslc/main.tnsl | 4 ++++ tnslc/tnslc | Bin 0 -> 21440 bytes tnslc/vector.tnsl | 59 +++++++++++++++++++++++++++++++++++++++++++++-- 7 files changed, 83 insertions(+), 22 deletions(-) create mode 100755 tests/run.sh delete mode 100644 tests/test_controll.tnsl create mode 100755 tnslc/tnslc diff --git a/compiler.c b/compiler.c index 9d66258..8b67a1e 100644 --- a/compiler.c +++ b/compiler.c @@ -1027,7 +1027,7 @@ void _var_op_set_ptr(CompData *out, Variable *store, Variable *from) { int *cur; for (size_t i = store->ptr_chain.count - 1; i > 0; i--) { cur = vect_get(&store->ptr_chain, i - 1); - if (cur == PTYPE_REF) { + if (*cur == PTYPE_REF) { vect_push_string(&out->text, "\tmov rdi, [rdi]\n"); } else { break; @@ -1054,7 +1054,7 @@ void _var_op_set_ptr(CompData *out, Variable *store, Variable *from) { int *cur; for (size_t i = from->ptr_chain.count - 1; i > 0; i--) { cur = vect_get(&store->ptr_chain, i - 1); - if (cur == PTYPE_REF) { + if (*cur == PTYPE_REF) { vect_push_string(&out->text, "\tmov rsi, [rsi]\n"); } else { break; @@ -1544,18 +1544,23 @@ void var_op_mul(CompData *out, Variable *base, Variable *mul) { if(base->type->name[0] == 'i') { // Integer multiplication if (base->location > 0 && mul->location != LOC_LITL) { + char *store = _var_get_store(out, base); + char *from = _var_get_from(out, base, mul); + vect_push_string(&out->text, "\timul "); - vect_push_free_string(&out->text, _var_get_store(out, base)); + vect_push_free_string(&out->text, store); vect_push_string(&out->text, ", "); - vect_push_free_string(&out->text, _var_get_from(out, base, mul)); + vect_push_free_string(&out->text, from); vect_push_string(&out->text, "; complete mul\n\n"); } else if (base->location > 0) { vect_push_string(&out->text, "\tmov rcx, "); vect_push_free_string(&out->text, int_to_str(mul->offset)); vect_push_string(&out->text, "; literal load\n"); + char *store = _var_get_store(out, base); + vect_push_string(&out->text, "\timul "); - vect_push_free_string(&out->text, _var_get_store(out, base)); + vect_push_free_string(&out->text, store); vect_push_string(&out->text, ", "); vect_push_free_string(&out->text, _op_get_register(3, _var_size(base))); vect_push_string(&out->text, "; complete mul\n\n"); @@ -4134,7 +4139,7 @@ Variable _eval_call(Scope *s, CompData *data, Vector *tokens, Function *f, Varia Variable set = scope_mk_stmp(s, data, cur); Variable from = _eval(s, data, tokens, pstart, pend); // eval and set - var_op_pure_set(data, &set, &from); + var_op_set(data, &set, &from); scope_free_to(s, data, &set); var_end(&from); var_end(&set); @@ -4186,7 +4191,7 @@ Variable _eval_call(Scope *s, CompData *data, Vector *tokens, Function *f, Varia Variable set = scope_mk_stmp(s, data, cur); // eval and set Variable from = _eval(s, data, tokens, pstart, pend); - var_op_pure_set(data, &set, &from); + var_op_set(data, &set, &from); // cleanup scope_free_to(s, data, &set); var_end(&from); diff --git a/tests/run.sh b/tests/run.sh new file mode 100755 index 0000000..136dac1 --- /dev/null +++ b/tests/run.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +mkdir -p ./out/artifact +filename=$1 +filename="${filename%.*}" +../ctc $1 out/artifact/$filename.asm +nasm -f elf64 -o ./out/artifact/$filename.o ./out/artifact/$filename.asm +gcc -o ./out/$filename ./out/artifact/$filename.o + diff --git a/tests/test_controll.tnsl b/tests/test_controll.tnsl deleted file mode 100644 index 47ff241..0000000 --- a/tests/test_controll.tnsl +++ /dev/null @@ -1,12 +0,0 @@ -/; main [int] - - /; if (false) - return 1 - ;; else if (1 == 0) - return 2 - ;; else - return 69 - ;/ - - return 0 -;/ diff --git a/tests/test_method_2.tnsl b/tests/test_method_2.tnsl index 899cdcb..56b3419 100644 --- a/tests/test_method_2.tnsl +++ b/tests/test_method_2.tnsl @@ -12,6 +12,6 @@ struct Vector { /; main [int] Vector a - a.init(1) + a.init(69) return a._elsz ;/ diff --git a/tnslc/main.tnsl b/tnslc/main.tnsl index ac56836..c7e8c95 100644 --- a/tnslc/main.tnsl +++ b/tnslc/main.tnsl @@ -5,6 +5,10 @@ Vector a a.init(1) + + + + a.end() return 0 ;/ diff --git a/tnslc/tnslc b/tnslc/tnslc new file mode 100755 index 0000000..e8d4c46 Binary files /dev/null and b/tnslc/tnslc differ diff --git a/tnslc/vector.tnsl b/tnslc/vector.tnsl index 1377938..9b89081 100644 --- a/tnslc/vector.tnsl +++ b/tnslc/vector.tnsl @@ -1,3 +1,4 @@ +# Define vector struct struct Vector { ~void data, int @@ -6,21 +7,75 @@ struct Vector { _elsz } +# Consts used in impl int VECT_DEFAULT_SIZE = 4 +int VECT_MAX_GROW = 128 +# Methods on the struct /; method Vector + # Initialize a new vector with elements + # 'elsz' bytes long /; init (int elsz) - self._elsz = elsz self.size = VECT_DEFAULT_SIZE - self.data = _alloc(elsz * self.size) + self._elsz = elsz self.count = 0 + self.data = _alloc(elsz * VECT_DEFAULT_SIZE) + ;/ + + # Grow the size of the vector by 'size' elements + /; _grow (int size) + /; if (size > VECT_MAX_GROW) + size = VECT_MAX_GROW + ;/ + + self.size = self.size + size + self.data = _realloc(self.data, self.size * self._elsz) + ;/ + + # Shrink the size of the vector by 'size' elements + /; _shrink (int size) + /; if (self.size - size < 0) + self.size = 1 + ;; else + self.size = self.size - size + ;/ + + /; if (self.count < self.size) + self.count = self.size + ;/ + + self.data = _realloc(self.data, self.size * self._elsz) + ;/ + + # Push an element onto the end of the vector + /; push (~void data) + /; if (count == size - 1) + self._grow(self.size) + ;/ + + /; loop (int i = 0; i < self._elsz) [i++] + (self.data + i)` = (data + i)` + ;/ + + self.count++ + ;/ + + # Pop an element off of the end of the vector + /; pop + self.count-- + + /; if (self.count < self.size / 2) + self._shrink(self.size / 3) + ;/ ;/ + # Get a pointer to the start of an element in the vector /; get (int index) [~void] return self.data ;/ + # Free all memory associated with the vector /; end _delete(self.data) self.size = 0 -- cgit v1.2.3 From f80bb4fd79f27210b606966c421ff3104ad0c959 Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Fri, 15 Mar 2024 17:28:48 -0400 Subject: Build script --- tnslc/build.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tnslc/build.sh b/tnslc/build.sh index 798e77d..a12ad5b 100755 --- a/tnslc/build.sh +++ b/tnslc/build.sh @@ -1,7 +1,8 @@ #!/bin/bash -../ctc main.tnsl tnslc.asm -nasm -f elf64 -o tnslc.o tnslc.asm -gcc -o tnslc tnslc.o +mkdir -p out +../ctc main.tnsl out/tnslc.asm +nasm -f elf64 -o out/tnslc.o out/tnslc.asm +gcc -o out/tnslc out/tnslc.o # rm tnslc.asm tnslc.o -- cgit v1.2.3