From ffa3e4c1b1997218080cca44d99a2031a19da5e0 Mon Sep 17 00:00:00 2001 From: Jake Date: Thu, 4 Jul 2024 22:21:26 -0700 Subject: [PATCH] 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();