-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/NicoBliss/jccc
- Loading branch information
Showing
15 changed files
with
280 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// Possibility later |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Possibility later | ||
|
||
#pragma once |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.