From a3efa633617f7b02a91348110457755de281c863 Mon Sep 17 00:00:00 2001 From: Matthew Laux Date: Wed, 18 Oct 2023 17:52:24 -0500 Subject: [PATCH] no idea how to write a JIT compiler but this seems like an ok start --- src/compiler68k.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/compiler68k.c diff --git a/src/compiler68k.c b/src/compiler68k.c new file mode 100644 index 0000000..1f19c97 --- /dev/null +++ b/src/compiler68k.c @@ -0,0 +1,66 @@ +#include +#include +#include + + +uint8_t out_code[1024]; +uint8_t memory[1024]; // ??? +uint32_t out_ptr; + +struct basic_block { + // in 68k space? + void *start_address; + size_t length; +}; + +// need some kind of map from gb address to struct basic_block? + +uint8_t test_code[] = { + 0 +}; + +struct basic_block *compile_block(uint16_t src_address, uint8_t *gb_code) +{ + uint32_t start = out_ptr; + struct basic_block *bblock; + + bblock = malloc(sizeof *bblock); + bblock->start_address = out_code + start; + + return bblock; +} + +void run_block(struct basic_block *bblock) +{ + // calling convention? do i need to do this from asm? + uint16_t jump_target = ((uint16_t (*)()) bblock->start_address)(); +} + +// TODO +void block_cache_add(uint16_t src_address, struct basic_block *bblock); +struct basic_block *block_cache_get(uint16_t src_address); + +// 1. compile each block ending in a jump +// 2. turn the jump into a return +// 3. add the compiled code to some kind of cache +// 3. return back to check the cache and maybe compile the next block + +int main(int argc, char *argv[]) +{ + struct basic_block *bblock; + + bblock = compile_block(0, test_code); + block_cache_add(0, bblock); + + while (1) { + uint16_t jump_target; + + jump_target = ((uint16_t (*)()) bblock->start_address)(); + + bblock = block_cache_get(jump_target); + if (!bblock) { + bblock = compile_block(jump_target, test_code + jump_target); + } + } + return 0; +} \ No newline at end of file