Skip to content

Commit

Permalink
Add more for op
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeRoggenbuck committed Jul 5, 2024
1 parent 8b4bb01 commit ffa3e4c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/codegen/x86/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "codegen.h"

#include <lexer/token.h>
#include <testing/tassert.h> // tassert

struct GenState {
Expand All @@ -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\
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/codegen/x86/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

#include <testing/test_utils.h>

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();

Expand Down

0 comments on commit ffa3e4c

Please sign in to comment.