diff options
author | Kyle Gunger <kgunger12@gmail.com> | 2022-10-09 15:15:39 -0400 |
---|---|---|
committer | Kyle Gunger <kgunger12@gmail.com> | 2022-10-09 15:15:39 -0400 |
commit | 51494729997b457251a62506b87af1635cd19802 (patch) | |
tree | 37d9a669363bda813d56b93497e53c20cb17dc50 | |
parent | b819cbb3b0ff492024b72d9ea9c9e28e1023b9c3 (diff) |
Basic asm helpers
-rw-r--r-- | CONTRIBUTING.md | 5 | ||||
-rw-r--r-- | tnslc/compile/compile.tnsl | 28 | ||||
-rw-r--r-- | tnslc/compile/isa_x86.tnsl | 67 | ||||
-rw-r--r-- | tnslc/dummy.tnsl | 21 | ||||
-rw-r--r-- | tnslc/tnslc.tnsl | 1 |
5 files changed, 60 insertions, 62 deletions
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..115dac1 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,5 @@ +To contribute to this repository, the following terms must be met: + +- The contributing party must have enough rights to the code they are providing to make said code available under the license +- The contributing party agrees to allow their contributed code to be governed by the license in this repository +- The contributing party agrees to field any legal issues from their contribution, and give other contributers indemnity diff --git a/tnslc/compile/compile.tnsl b/tnslc/compile/compile.tnsl index 520ea1f..7d8f012 100644 --- a/tnslc/compile/compile.tnsl +++ b/tnslc/compile/compile.tnsl @@ -14,13 +14,6 @@ EXPRESS OR IMPLIED #/ -/; module compile - - # :include "compile/arch/arch.tnsl" - # :include "compile/comp.tnsl" -;/ - -:include "isa_x86.tnsl" ;{}{}charp COMMON_ASM = { "\tret", @@ -88,18 +81,6 @@ ;/ ;/ -/; header_guard (~VTrack tab, ~{}charp csec) - -;/ - -/; ret_guard (~VTrack tab, {}charp to_ret, ~{}charp csec) - ;tail_guard(csec) - -;/ - -/; tail_guard (~{}charp csec) -;/ - /; compile_statement (~int cur, ~{}Token data, ~{}charp hsec, csec, dsec) ;cur`++ /; if (cur` < len data`) @@ -115,8 +96,8 @@ /; compile_block (~int cur, ~{}Token data, ~{}charp hsec, csec, dsec) ;VTrack tab = { - {"", "", "", "", "", "", "", ""}, - {NT, NT, NT, NT, NT, NT, NT, NT} + {}, + {} } ;{}charp name = {} @@ -182,7 +163,7 @@ ;{}charp dsec = ".data\n" ;int j = len data` - + /# /; loop (int i = 0; i < j) [i++] /; if (string_equate(data`{i}.data`, "/;")) ;compile_block(~i, data, ~hsec, ~csec, ~dsec) @@ -192,6 +173,9 @@ ;break ;/ ;/ + #/ + ;{}charp tmp = add_asm("%eax", "%edx") + ;add_strings(~csec, ~tmp) ;tnsl.io.File out = tnsl.io.writeFile(file) diff --git a/tnslc/compile/isa_x86.tnsl b/tnslc/compile/isa_x86.tnsl index 1dff3f7..0ab8910 100644 --- a/tnslc/compile/isa_x86.tnsl +++ b/tnslc/compile/isa_x86.tnsl @@ -14,18 +14,52 @@ EXPRESS OR IMPLIED #/ -/; push_asm ({}charp reg) [{}charp] - ;{}charp out = "\tpush " - ;add_strings(~out, ~reg) - ;out.append('\n') +/; construct_statement({}charp base, {}{}charp args) [{}charp] + /; loop (int i = 0; i < len args) [i++] + ;add_strings(~base, ~(args{i})) + /; if (i < len args - 1) + ;base.append(',') + ;base.append(' ') + ;/ + ;/ + ;base.append('\n') + ;return base +;/ + +/; literal_num ({}charp num) [{}charp] + ;{}charp out = "$" + ;add_strings(~out, ~num) ;return out ;/ +/; add_asm ({}charp from, to) [{}charp] + ;return construct_statement("\tadd ", {from, to}) +;/ + +/; sub_asm({}charp from, to) [{}charp] + ;return construct_statement("\tsub ", {from, to}) +;/ + +/; push_asm ({}charp reg) [{}charp] + ;return construct_statement("\tpush ", {reg}) +;/ + /; pop_asm ({}charp reg) [{}charp] - ;{}charp out = "\tpop " - ;add_strings(~out, ~reg) - ;out.append('\n') - ;return out + ;return construct_statement("\tpop ", {reg}) +;/ + +/; cmp_asm ({}charp a, b) [{}charp] + ;return construct_statement("\tcmp ", {a, b}) +;/ + +/; jmp_asm ({}charp pos) [{}charp] + ;return construct_statement("\tjmp ", {pos}) +;/ + +/; mem_offset ({}charp pos, offset, scale) [{}charp] + ;{}charp tmp = construct_statement("(", {pos, offset, scale}) + ;tmp{len tmp - 1} = ')' + ;return tmp ;/ /; header_guard [{}charp] @@ -87,7 +121,7 @@ /; if (string_equate(common, "ax") || string_equate(common, "bx") || string_equate(common, "cx") || string_equate(common, "dx")) /; if (size == 1) - common{1} = 'l' + ;common{1} = 'l' ;; else if (size == 4) ;out.append('e') ;; else if (size == 8) @@ -124,18 +158,3 @@ ;return out ;/ - -/; literal_num ({}charp num) [{}charp] - ;{}charp out = "$" - ;add_strings(~out, ~num) - ;return out -;/ - -/; add_asm ({}charp from, to) [{}charp] - ;{}charp out = "\tadd " - ;add_strings(~out, ~from) - ;out.append(',') - ;out.append(' ') - ;add_strings(~out, ~to) - ; -;/
\ No newline at end of file diff --git a/tnslc/dummy.tnsl b/tnslc/dummy.tnsl index 003e428..60fe913 100644 --- a/tnslc/dummy.tnsl +++ b/tnslc/dummy.tnsl @@ -1,28 +1,17 @@ -/; raw _alloc +/; _alloc (uint bytes) [~void] ;asm "mov %rax, %rdi" - ;asm "push %r8" - ;asm "push %r9" - ;asm "push %r10" ;asm "mov $0, %r10" ;asm "call malloc" - ;asm "pop %r10" - ;asm "pop %r9" - ;asm "pop %r8" ;/ -/; raw _delete +/; _delete (~void ptr) ;asm "mov %rax, %rdi" - ;asm "push %r9" - ;asm "push %r10" ;asm "mov $0, %r10" ;asm "call free" - ;asm "pop %r10" - ;asm "pop %r9" ;/ /; main [int] - ;asm "mov $10, %rax" - ;asm "call _alloc" - ;asm "call _delete" - ;asm "mov $0, %rax" + ;~void ptr = _alloc(10) + ;_delete(ptr) + ;return 0 ;/ diff --git a/tnslc/tnslc.tnsl b/tnslc/tnslc.tnsl index 7aad7b5..b8cfb92 100644 --- a/tnslc/tnslc.tnsl +++ b/tnslc/tnslc.tnsl @@ -19,6 +19,7 @@ /; export module tnslc :include "parse/parse.tnsl" :include "compile/compile.tnsl" + :include "compile/isa_x86.tnsl" ;/ |