Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More codegen #49

Merged
merged 6 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading