From 0d0eea0f227be681ccd235fba045a8048047fb4f Mon Sep 17 00:00:00 2001 From: Jake Date: Sat, 29 Jun 2024 11:18:54 -0700 Subject: [PATCH 1/5] Add risc-v init --- src/codegen/RISCV/codegen.c | 26 +++++++++++++++++++++++++- src/codegen/RISCV/codegen.h | 4 ++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/codegen/RISCV/codegen.c b/src/codegen/RISCV/codegen.c index b95b9c3..e456037 100644 --- a/src/codegen/RISCV/codegen.c +++ b/src/codegen/RISCV/codegen.c @@ -1 +1,25 @@ -// Possibility later +#include + +#include "codegen.h" + +#include // tassert + +struct RGenState { + // Each bit corresponds with a registers 0-31 where the LSB is 0 + uint32_t registers_in_use; + + unsigned int sp_offset; +} RGEN_STATE; + +void r_code_gen_init() { + RGEN_STATE.registers_in_use = 0; + RGEN_STATE.sp_offset = 0; +} + +char *r_start_main() { + static char start[256] = "\ +.global _start\n\ +_start:\n"; + + return start; +} diff --git a/src/codegen/RISCV/codegen.h b/src/codegen/RISCV/codegen.h index 616671c..b622c27 100644 --- a/src/codegen/RISCV/codegen.h +++ b/src/codegen/RISCV/codegen.h @@ -1,3 +1,3 @@ -// Possibility later +void r_code_gen_init(); -#pragma once +char *r_start_main(); From 0419771024eb07625bedf860f41d894e6790be38 Mon Sep 17 00:00:00 2001 From: Jake Date: Sat, 29 Jun 2024 11:55:00 -0700 Subject: [PATCH 2/5] Add operation --- src/codegen/x86/codegen.c | 8 ++++++++ src/codegen/x86/codegen.h | 2 ++ 2 files changed, 10 insertions(+) diff --git a/src/codegen/x86/codegen.c b/src/codegen/x86/codegen.c index fae49b3..9ac57c8 100644 --- a/src/codegen/x86/codegen.c +++ b/src/codegen/x86/codegen.c @@ -46,6 +46,14 @@ char *end_main_custom_return(int val) { return end; } +char* op_on_n(int n, enum Op op) { + char *end; + end = (char *)malloc(256 * sizeof(char)); + sprintf(end, " mov rax, 60\n mov rdi, %d\n syscall\n", n); + + return end; +} + char *start_func() { static char start[256] = "\ sub rsp, 32\ diff --git a/src/codegen/x86/codegen.h b/src/codegen/x86/codegen.h index 5e3a489..f81da28 100644 --- a/src/codegen/x86/codegen.h +++ b/src/codegen/x86/codegen.h @@ -2,6 +2,8 @@ #include +enum Op { ADD }; + char *start_main(); char *end_main(); From 8b4bb01ec0037218875887ee89d481df8789ff6b Mon Sep 17 00:00:00 2001 From: Jake Date: Thu, 4 Jul 2024 07:53:27 -0700 Subject: [PATCH 3/5] Add more codegen --- src/codegen/x86/codegen.c | 21 +++++++++++++++++++-- src/codegen/x86/codegen.h | 4 +++- src/codegen/x86/test_x86.c | 1 + 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/codegen/x86/codegen.c b/src/codegen/x86/codegen.c index 9ac57c8..cf6dc68 100644 --- a/src/codegen/x86/codegen.c +++ b/src/codegen/x86/codegen.c @@ -46,10 +46,15 @@ char *end_main_custom_return(int val) { return end; } -char* op_on_n(int n, enum Op op) { +static char *op_strs[4] = {"add", "sub", "mov"}; + +char *op_on_rax_with_rdi(enum Op op) { char *end; end = (char *)malloc(256 * sizeof(char)); - sprintf(end, " mov rax, 60\n mov rdi, %d\n syscall\n", n); + + char *op_str = op_strs[op]; + + sprintf(end, " %s rax, rdi\n", op_str); return end; } @@ -94,3 +99,15 @@ int test_init_int_literal() { return 0; } + +int test_op_on_rax_with_rdi() { + testing_func_setup(); + + char *out = op_on_rax_with_rdi(ADD); + tassert(strcmp(out, " add rax, rdi\n") == 0); + + char *out2 = op_on_rax_with_rdi(MOV); + tassert(strcmp(out2, " mov rax, rdi\n") == 0); + + return 0; +} diff --git a/src/codegen/x86/codegen.h b/src/codegen/x86/codegen.h index f81da28..b1ef00b 100644 --- a/src/codegen/x86/codegen.h +++ b/src/codegen/x86/codegen.h @@ -2,7 +2,7 @@ #include -enum Op { ADD }; +enum Op { ADD, SUB, MOV }; char *start_main(); @@ -17,3 +17,5 @@ char *end_func(); char *init_int_literal(int val); int test_init_int_literal(); + +int test_op_on_rax_with_rdi(); diff --git a/src/codegen/x86/test_x86.c b/src/codegen/x86/test_x86.c index 50b4948..bafacf4 100644 --- a/src/codegen/x86/test_x86.c +++ b/src/codegen/x86/test_x86.c @@ -9,6 +9,7 @@ int test_x86() { testing_module_setup(); test_init_int_literal(); + test_op_on_rax_with_rdi(); testing_module_cleanup(); return 0; From ffa3e4c1b1997218080cca44d99a2031a19da5e0 Mon Sep 17 00:00:00 2001 From: Jake Date: Thu, 4 Jul 2024 22:21:26 -0700 Subject: [PATCH 4/5] Add more for op --- src/codegen/x86/codegen.c | 16 ++++++++++++++-- src/codegen/x86/codegen.h | 4 +++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/codegen/x86/codegen.c b/src/codegen/x86/codegen.c index cf6dc68..d6189d8 100644 --- a/src/codegen/x86/codegen.c +++ b/src/codegen/x86/codegen.c @@ -5,6 +5,7 @@ #include "codegen.h" +#include #include // tassert struct GenState { @@ -19,6 +20,17 @@ void code_gen_init() { GEN_STATE.rsp_offset = 0; } +enum Op ttype_to_op(TokenType t) { + switch (t) { + case TT_PLUS: + return OP_ADD; + case TT_MINUS: + return OP_SUB; + default: + return OP_NOP; + } +} + char *start_main() { static char start[256] = "\ global _start\n\ @@ -103,10 +115,10 @@ int test_init_int_literal() { int test_op_on_rax_with_rdi() { testing_func_setup(); - char *out = op_on_rax_with_rdi(ADD); + char *out = op_on_rax_with_rdi(OP_ADD); tassert(strcmp(out, " add rax, rdi\n") == 0); - char *out2 = op_on_rax_with_rdi(MOV); + char *out2 = op_on_rax_with_rdi(OP_MOV); tassert(strcmp(out2, " mov rax, rdi\n") == 0); return 0; diff --git a/src/codegen/x86/codegen.h b/src/codegen/x86/codegen.h index b1ef00b..068002c 100644 --- a/src/codegen/x86/codegen.h +++ b/src/codegen/x86/codegen.h @@ -2,7 +2,9 @@ #include -enum Op { ADD, SUB, MOV }; +enum Op { OP_ADD, OP_SUB, OP_MOV, OP_NOP }; + +enum Op ttype_to_op(TokenType t); char *start_main(); From 2a45ad9083cb966e94c19fba5f793dc301d46681 Mon Sep 17 00:00:00 2001 From: Jake Date: Sun, 7 Jul 2024 00:20:40 -0700 Subject: [PATCH 5/5] Add include --- src/codegen/x86/codegen.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/codegen/x86/codegen.h b/src/codegen/x86/codegen.h index 068002c..43f82d6 100644 --- a/src/codegen/x86/codegen.h +++ b/src/codegen/x86/codegen.h @@ -1,5 +1,6 @@ #pragma once +#include #include enum Op { OP_ADD, OP_SUB, OP_MOV, OP_NOP };