-
Notifications
You must be signed in to change notification settings - Fork 4
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
Start codegen #30
Start codegen #30
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// Possibility later |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Possibility later | ||
|
||
#pragma once |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "codegen.h" | ||
|
||
#include <testing/tassert.h> // tassert | ||
|
||
struct GenState { | ||
// Each bit corresponds with a registers 0-31 where the LSB is 0 | ||
uint32_t registers_in_use; | ||
|
||
unsigned int rsp_offset; | ||
} GEN_STATE; | ||
|
||
void code_gen_init() { | ||
GEN_STATE.registers_in_use = 0; | ||
GEN_STATE.rsp_offset = 0; | ||
} | ||
|
||
char *start_main() { | ||
static char start[256] = "\ | ||
global _start\ | ||
section .text\ | ||
\ | ||
_start:"; | ||
|
||
return start; | ||
} | ||
|
||
char *end_main() { | ||
static char end[256] = "\ | ||
mov rax, 60\ | ||
mov rdi, 0\ | ||
syscall"; | ||
|
||
return end; | ||
} | ||
|
||
char *start_func() { | ||
static char start[256] = "\ | ||
sub rsp, 32\ | ||
mov [rsp], r12\ | ||
mov [rsp+8], r13\ | ||
mov [rsp+16], r14\ | ||
mov [rsp+24], r15"; | ||
|
||
return start; | ||
} | ||
|
||
char *end_func() { | ||
static char end[256] = "\ | ||
mov r12, [rsp]\ | ||
mov r13, [rsp+8]\ | ||
mov r14, [rsp+16]\ | ||
mov r15, [rsp+24]\ | ||
add rsp, 32"; | ||
|
||
return end; | ||
} | ||
|
||
char *init_int_literal(int val) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For my understanding, what exactly does this do? Does it just push a new int with the specified value to the stack? Still getting there with the assembly. Also, this is all fine for now, but in the future maybe we should have all of these functions also accept a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup |
||
GEN_STATE.rsp_offset += 8; | ||
|
||
char *init; | ||
init = (char *)malloc(256 * sizeof(char)); | ||
sprintf(init, "mov [rsp+%d], %d", GEN_STATE.rsp_offset, val); | ||
|
||
return init; | ||
} | ||
|
||
int test_init_int_literal() { | ||
testing_func_setup(); | ||
code_gen_init(); | ||
|
||
tassert(strcmp(init_int_literal(100), "mov [rsp+8], 100") == 0); | ||
|
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
|
||
#include <testing/test_utils.h> | ||
|
||
char *start_main(); | ||
|
||
char *end_main(); | ||
|
||
char *start_func(); | ||
|
||
char *end_func(); | ||
|
||
char *init_int_literal(int val); | ||
|
||
int test_init_int_literal(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Run all of the tests at the end of each C file in the lexer module | ||
*/ | ||
|
||
#include "codegen.h" | ||
#include <testing/test_utils.h> | ||
|
||
int test_x86() { | ||
testing_module_setup(); | ||
|
||
test_init_int_literal(); | ||
|
||
testing_module_cleanup(); | ||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Run all of the tests at the end of each C file in the lexer module | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "codegen.h" | ||
#include <testing/test_utils.h> | ||
|
||
int test_x86(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
/* | ||
* Run all of the tests at the end of each C file in the lexer module */ | ||
|
||
#pragma once | ||
|
||
#include "lex.h" | ||
|
||
int test_lexer(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
#include "lexer/test_lexer.h" | ||
#include "codegen/x86/test_x86.h" | ||
|
||
int main() { | ||
test_lexer(); | ||
test_x86(); | ||
|
||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#pragma once | ||
|
||
#include <assert.h> | ||
|
||
#define tassert(e) ((void)((e) ? 0 : __assert(#e, __FILE__, __LINE__))) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is perhaps silly, but are all of these no-input functions not behaving as immutable global variables you are asking for copies of?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the conversation above, it will make sense once we reorganize this to write into files instead