Skip to content

Commit

Permalink
Merge pull request #49 from JakeRoggenbuck/more-codegen
Browse files Browse the repository at this point in the history
More codegen
  • Loading branch information
JakeRoggenbuck authored Jul 13, 2024
2 parents 3056da3 + 2a45ad9 commit bf1e0f8
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
26 changes: 25 additions & 1 deletion src/codegen/RISCV/codegen.c
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
// Possibility later
#include <stdint.h>

#include "codegen.h"

#include <testing/tassert.h> // 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;
}
4 changes: 2 additions & 2 deletions src/codegen/RISCV/codegen.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Possibility later
void r_code_gen_init();

#pragma once
char *r_start_main();
37 changes: 37 additions & 0 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 @@ -46,6 +58,19 @@ char *end_main_custom_return(int val) {
return end;
}

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

char *op_str = op_strs[op];

sprintf(end, " %s rax, rdi\n", op_str);

return end;
}

char *start_func() {
static char start[256] = "\
sub rsp, 32\
Expand Down Expand Up @@ -86,3 +111,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(OP_ADD);
tassert(strcmp(out, " add rax, rdi\n") == 0);

char *out2 = op_on_rax_with_rdi(OP_MOV);
tassert(strcmp(out2, " mov rax, rdi\n") == 0);

return 0;
}
7 changes: 7 additions & 0 deletions src/codegen/x86/codegen.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#pragma once

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

enum Op { OP_ADD, OP_SUB, OP_MOV, OP_NOP };

enum Op ttype_to_op(TokenType t);

char *start_main();

char *end_main();
Expand All @@ -15,3 +20,5 @@ char *end_func();
char *init_int_literal(int val);

int test_init_int_literal();

int test_op_on_rax_with_rdi();
1 change: 1 addition & 0 deletions src/codegen/x86/test_x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit bf1e0f8

Please sign in to comment.