From 98ce28ba0adb770e0ac29e1f9822b675ef914769 Mon Sep 17 00:00:00 2001 From: Michael Pavone Date: Mon, 24 Feb 2014 09:55:24 -0800 Subject: Moved some generic stuff from backend.h gen_arm.h and gen_arm.c into gen.h and gen.c. Added a couple fields to cpu_options so that gen_mem_fun can be made guest CPU generic --- Makefile | 12 +++++++----- backend.h | 9 +++------ gen.c | 15 +++++++++++++++ gen.h | 22 ++++++++++++++++++++++ gen_arm.c | 21 +++------------------ gen_arm.h | 8 +------- 6 files changed, 51 insertions(+), 36 deletions(-) create mode 100644 gen.c create mode 100644 gen.h diff --git a/Makefile b/Makefile index a20ae8f..8d4b079 100644 --- a/Makefile +++ b/Makefile @@ -24,13 +24,15 @@ endif -TRANSOBJS=gen_x86.o backend.o mem.o +TRANSOBJS=gen.o backend.o mem.o M68KOBJS=68kinst.o m68k_to_x86.o ifeq ($(CPU),x86_64) M68KOBJS+= runtime.o +TRANSOBJS+= gen_x86.o else ifeq ($(CPU),i686) M68KOBJS+= runtime_32.o +TRANSOBJS+= gen_x86.o endif endif @@ -85,11 +87,11 @@ vgmplay : vgmplay.o render_sdl.o $(CONFIGOBJS) $(AUDIOOBJS) testgst : testgst.o gst.o $(CC) -o testgst testgst.o gst.o -test_x86 : test_x86.o gen_x86.o - $(CC) -o test_x86 test_x86.o gen_x86.o +test_x86 : test_x86.o gen_x86.o gen.o + $(CC) -o test_x86 test_x86.o gen_x86.o gen.o -test_arm : test_arm.o gen_arm.o mem.o - $(CC) -o test_arm test_arm.o gen_arm.o mem.o +test_arm : test_arm.o gen_arm.o mem.o gen.o + $(CC) -o test_arm test_arm.o gen_arm.o mem.o gen.o gen_fib : gen_fib.o gen_x86.o mem.o $(CC) -o gen_fib gen_fib.o gen_x86.o mem.o diff --git a/backend.h b/backend.h index d35a1ec..697ea7a 100644 --- a/backend.h +++ b/backend.h @@ -8,6 +8,7 @@ #include #include +#include "gen.h" #define INVALID_OFFSET 0xFFFFFFFF #define EXTENSION_WORD 0xFFFFFFFE @@ -20,12 +21,6 @@ typedef struct { uint8_t cycles; } x86_ea; -#if defined(X86_64) || defined(X86_32) -typedef uint8_t* code_ptr; -#else -typedef uint32_t* code_ptr; -#endif - typedef struct { uint8_t *base; int32_t *offsets; @@ -56,6 +51,8 @@ typedef struct { code_ptr handle_cycle_limit; code_ptr handle_cycle_limit_int; uint8_t context_reg; + uint8_t cycles; + uint8_t limit; uint8_t scratch1; uint8_t scratch2; } cpu_options; diff --git a/gen.c b/gen.c new file mode 100644 index 0000000..8f1b5f5 --- /dev/null +++ b/gen.c @@ -0,0 +1,15 @@ +#include +#include +#include "gen.h" +#include "mem.h" + +void init_code_info(code_info *code) +{ + size_t size = CODE_ALLOC_SIZE; + code->cur = alloc_code(&size); + if (!code->cur) { + fputs("Failed to allocate memory for generated code\n", stderr); + exit(1); + } + code->last = code->cur + size/sizeof(code_word) - RESERVE_WORDS; +} diff --git a/gen.h b/gen.h new file mode 100644 index 0000000..cd6933e --- /dev/null +++ b/gen.h @@ -0,0 +1,22 @@ +#ifndef GEN_H_ +#define GEN_H_ +#include + +#if defined(X86_64) || defined(X86_32) +typedef uint8_t code_word; +#define RESERVE_WORDS 5 //opcode + 4-byte displacement +#else +typedef uint32_t code_word; +#define RESERVE_WORDS 4 //1 push + 1 ldr + 1bx + 1 constant +#endif +typedef code_word * code_ptr; +#define CODE_ALLOC_SIZE (1024*1024) + +typedef struct { + code_ptr cur; + code_ptr last; +} code_info; + +void init_code_info(code_info *code); + +#endif //GEN_H_ diff --git a/gen_arm.c b/gen_arm.c index 2693fc3..5d9fab6 100644 --- a/gen_arm.c +++ b/gen_arm.c @@ -57,10 +57,6 @@ #define REG 0u - -#define RESERVE_INSTRUCTIONS 4 //1 ldr + 1bx + 1 constant -#define CODE_ALLOC_SIZE (1024*1024) - uint32_t make_immed(uint32_t val) { uint32_t rot_amount = 0; @@ -74,17 +70,6 @@ uint32_t make_immed(uint32_t val) return INVALID_IMMED; } -void init_code_info(code_info *code) -{ - size_t size = CODE_ALLOC_SIZE; - code->cur = alloc_code(&size); - if (!code->cur) { - fputs("Failed to allocate memory for generated code\n", stderr); - exit(1); - } - code->last = code->cur + size/sizeof(uint32_t) - RESERVE_INSTRUCTIONS; -} - void check_alloc_code(code_info *code) { if (code->cur == code->last) { @@ -94,9 +79,9 @@ void check_alloc_code(code_info *code) fputs("Failed to allocate memory for generated code\n", stderr); exit(1); } - if (next_code = code->last + RESERVE_INSTRUCTIONS) { + if (next_code = code->last + RESERVE_WORDS) { //new chunk is contiguous with the current one - code->last = next_code + size/sizeof(uint32_t) - RESERVE_INSTRUCTIONS; + code->last = next_code + size/sizeof(code_word) - RESERVE_WORDS; } else { uint32_t * from = code->cur + 2; if (next_code - from < 0x400000 || from - next_code <= 0x400000) { @@ -115,7 +100,7 @@ void check_alloc_code(code_info *code) } //branch to address in r0 *from = CC_AL | OP_BX; - code->last = next_code + size/sizeof(uint32_t) - RESERVE_INSTRUCTIONS; + code->last = next_code + size/sizeof(code_word) - RESERVE_WORDS; //pop r0 *(next_code++) = CC_AL | POP; code->cur = next_code; diff --git a/gen_arm.h b/gen_arm.h index cd351fc..749f78c 100644 --- a/gen_arm.h +++ b/gen_arm.h @@ -7,11 +7,7 @@ #define GEN_ARM_H_ #include - -typedef struct { - uint32_t *cur; - uint32_t *last; -} code_info; +#include "gen.h" #define SET_COND 0x100000u #define NO_COND 0u @@ -73,8 +69,6 @@ enum { #define LR 0x4000 #define PC 0x8000 -void init_code_info(code_info *code); - uint32_t and(code_info *code, uint32_t dst, uint32_t src1, uint32_t src2, uint32_t set_cond); uint32_t andi(code_info *code, uint32_t dst, uint32_t src1, uint32_t immed, uint32_t set_cond); uint32_t and_cc(code_info *code, uint32_t dst, uint32_t src1, uint32_t src2, uint32_t cc, uint32_t set_cond); -- cgit v1.2.3