1.1 --- a/src/sh4/sh4x86.in Tue Aug 28 08:46:14 2007 +0000
1.2 +++ b/src/sh4/sh4x86.in Tue Sep 04 08:40:23 2007 +0000
1.5 - * $Id: sh4x86.in,v 1.2 2007-08-28 08:46:14 nkeynes Exp $
1.6 + * $Id: sh4x86.in,v 1.3 2007-09-04 08:40:23 nkeynes Exp $
1.8 * SH4 => x86 translation. This version does no real optimization, it just
1.9 * outputs straight-line x86 code - it mainly exists to provide a baseline
1.11 * GNU General Public License for more details.
1.14 -#include "sh4core.h"
1.15 -#include "sh4trans.h"
1.17 +#include <assert.h>
1.19 +#include "sh4/sh4core.h"
1.20 +#include "sh4/sh4trans.h"
1.21 +#include "sh4/x86op.h"
1.24 +#define DEFAULT_BACKPATCH_SIZE 4096
1.27 + * Struct to manage internal translation state. This state is not saved -
1.28 + * it is only valid between calls to sh4_translate_begin_block() and
1.29 + * sh4_translate_end_block()
1.31 +struct sh4_x86_state {
1.32 + gboolean in_delay_slot;
1.33 + gboolean priv_checked; /* true if we've already checked the cpu mode. */
1.34 + gboolean fpuen_checked; /* true if we've already checked fpu enabled. */
1.36 + /* Allocated memory for the (block-wide) back-patch list */
1.37 + uint32_t **backpatch_list;
1.38 + uint32_t backpatch_posn;
1.39 + uint32_t backpatch_size;
1.42 +#define EXIT_DATA_ADDR_READ 0
1.43 +#define EXIT_DATA_ADDR_WRITE 7
1.44 +#define EXIT_ILLEGAL 14
1.45 +#define EXIT_SLOT_ILLEGAL 21
1.46 +#define EXIT_FPU_DISABLED 28
1.47 +#define EXIT_SLOT_FPU_DISABLED 35
1.49 +static struct sh4_x86_state sh4_x86;
1.51 +void sh4_x86_init()
1.53 + sh4_x86.backpatch_list = malloc(DEFAULT_BACKPATCH_SIZE);
1.54 + sh4_x86.backpatch_size = DEFAULT_BACKPATCH_SIZE / sizeof(uint32_t *);
1.58 +static void sh4_x86_add_backpatch( uint8_t *ptr )
1.60 + if( sh4_x86.backpatch_posn == sh4_x86.backpatch_size ) {
1.61 + sh4_x86.backpatch_size <<= 1;
1.62 + sh4_x86.backpatch_list = realloc( sh4_x86.backpatch_list, sh4_x86.backpatch_size * sizeof(uint32_t *) );
1.63 + assert( sh4_x86.backpatch_list != NULL );
1.65 + sh4_x86.backpatch_list[sh4_x86.backpatch_posn++] = (uint32_t *)ptr;
1.68 +static void sh4_x86_do_backpatch( uint8_t *reloc_base )
1.71 + for( i=0; i<sh4_x86.backpatch_posn; i++ ) {
1.72 + *sh4_x86.backpatch_list[i] += (reloc_base - ((uint8_t *)sh4_x86.backpatch_list[i]));
1.77 +#define MARK_JMP(x,n) uint8_t *_mark_jmp_##x = xlat_output + n
1.78 +#define CHECK_JMP(x) assert( _mark_jmp_##x == xlat_output )
1.80 +#define MARK_JMP(x,n)
1.81 +#define CHECK_JMP(x)
1.86 * Emit an instruction to load an SH4 reg into a real register
1.88 OP(REG_OFFSET(r[sh4reg]));
1.92 + * Load the SR register into an x86 register
1.94 +static inline void read_sr( int x86reg )
1.96 + MOV_ebp_r32( R_M, x86reg );
1.97 + SHL1_r32( x86reg );
1.98 + OR_ebp_r32( R_Q, x86reg );
1.99 + SHL_imm8_r32( 7, x86reg );
1.100 + OR_ebp_r32( R_S, x86reg );
1.101 + SHL1_r32( x86reg );
1.102 + OR_ebp_r32( R_T, x86reg );
1.103 + OR_ebp_r32( R_SR, x86reg );
1.106 +static inline void write_sr( int x86reg )
1.108 + TEST_imm32_r32( SR_M, x86reg );
1.110 + TEST_imm32_r32( SR_Q, x86reg );
1.112 + TEST_imm32_r32( SR_S, x86reg );
1.114 + TEST_imm32_r32( SR_T, x86reg );
1.116 + AND_imm32_r32( SR_MQSTMASK, x86reg );
1.117 + MOV_r32_ebp( x86reg, R_SR );
1.121 static inline void load_spreg( int x86reg, int regoffset )
1.123 /* mov [bp+n], reg */
1.125 static inline void call_func0( void *ptr )
1.127 load_imm32(R_EAX, (uint32_t)ptr);
1.129 - MODRM_rm32_r32(R_EAX, 2);
1.133 static inline void call_func1( void *ptr, int arg1 )
1.134 @@ -92,6 +185,59 @@
1.135 ADD_imm8s_r32( -4, R_ESP );
1.138 +/* Exception checks - Note that all exception checks will clobber EAX */
1.139 +static void check_priv( )
1.141 + if( !sh4_x86.priv_checked ) {
1.142 + sh4_x86.priv_checked = TRUE;
1.143 + load_spreg( R_EAX, R_SR );
1.144 + AND_imm32_r32( SR_MD, R_EAX );
1.145 + if( sh4_x86.in_delay_slot ) {
1.146 + JE_exit( EXIT_SLOT_ILLEGAL );
1.148 + JE_exit( EXIT_ILLEGAL );
1.153 +static void check_fpuen( )
1.155 + if( !sh4_x86.fpuen_checked ) {
1.156 + sh4_x86.fpuen_checked = TRUE;
1.157 + load_spreg( R_EAX, R_SR );
1.158 + AND_imm32_r32( SR_FD, R_EAX );
1.159 + if( sh4_x86.in_delay_slot ) {
1.160 + JNE_exit(EXIT_SLOT_FPU_DISABLED);
1.162 + JNE_exit(EXIT_FPU_DISABLED);
1.167 +static void check_ralign16( int x86reg )
1.169 + TEST_imm32_r32( 0x00000001, x86reg );
1.170 + JNE_exit(EXIT_DATA_ADDR_READ);
1.173 +static void check_walign16( int x86reg )
1.175 + TEST_imm32_r32( 0x00000001, x86reg );
1.176 + JNE_exit(EXIT_DATA_ADDR_WRITE);
1.179 +static void check_ralign32( int x86reg )
1.181 + TEST_imm32_r32( 0x00000003, x86reg );
1.182 + JNE_exit(EXIT_DATA_ADDR_READ);
1.184 +static void check_walign32( int x86reg )
1.186 + TEST_imm32_r32( 0x00000003, x86reg );
1.187 + JNE_exit(EXIT_DATA_ADDR_WRITE);
1.192 #define MEM_RESULT(value_reg) if(value_reg != R_EAX) { MOV_r32_r32(R_EAX,value_reg); }
1.193 #define MEM_READ_BYTE( addr_reg, value_reg ) call_func1(sh4_read_byte, addr_reg ); MEM_RESULT(value_reg)
1.194 @@ -101,30 +247,83 @@
1.195 #define MEM_WRITE_WORD( addr_reg, value_reg ) call_func2(sh4_write_word, addr_reg, value_reg)
1.196 #define MEM_WRITE_LONG( addr_reg, value_reg ) call_func2(sh4_write_long, addr_reg, value_reg)
1.198 +#define RAISE_EXCEPTION( exc ) call_func1(sh4_raise_exception, exc);
1.199 +#define CHECKSLOTILLEGAL() if(sh4_x86.in_delay_slot) RAISE_EXCEPTION(EXC_SLOT_ILLEGAL)
1.204 * Emit the 'start of block' assembly. Sets up the stack frame and save
1.205 * SI/DI as required
1.207 -void sh4_translate_begin_block() {
1.209 - *xlat_output++ = 0x50 + R_EBP;
1.211 +void sh4_translate_begin_block()
1.215 /* mov &sh4r, ebp */
1.216 load_imm32( R_EBP, (uint32_t)&sh4r );
1.219 + sh4_x86.in_delay_slot = FALSE;
1.220 + sh4_x86.priv_checked = FALSE;
1.221 + sh4_x86.fpuen_checked = FALSE;
1.222 + sh4_x86.backpatch_posn = 0;
1.225 - /* load carry from SR */
1.227 + * Exit the block early (ie branch out), conditionally or otherwise
1.229 +void exit_block( uint32_t pc )
1.231 + load_imm32( R_ECX, pc );
1.232 + store_spreg( R_ECX, REG_OFFSET(pc) );
1.233 + MOV_moff32_EAX( (uint32_t)&sh4_cpu_period );
1.234 + load_spreg( R_ECX, REG_OFFSET(slice_cycle) );
1.235 + MUL_r32( R_ESI );
1.236 + ADD_r32_r32( R_EAX, R_ECX );
1.237 + store_spreg( R_ECX, REG_OFFSET(slice_cycle) );
1.238 + XOR_r32_r32( R_EAX, R_EAX );
1.243 * Flush any open regs back to memory, restore SI/DI/, update PC, etc
1.245 void sh4_translate_end_block( sh4addr_t pc ) {
1.247 - *xlat_output++ = 0x58 + R_EBP;
1.248 + assert( !sh4_x86.in_delay_slot ); // should never stop here
1.249 + // Normal termination - save PC, cycle count
1.250 + exit_block( pc );
1.253 - *xlat_output++ = 0xC3;
1.254 + uint8_t *end_ptr = xlat_output;
1.255 + // Exception termination. Jump block for various exception codes:
1.256 + PUSH_imm32( EXC_DATA_ADDR_READ );
1.258 + PUSH_imm32( EXC_DATA_ADDR_WRITE );
1.260 + PUSH_imm32( EXC_ILLEGAL );
1.262 + PUSH_imm32( EXC_SLOT_ILLEGAL );
1.264 + PUSH_imm32( EXC_FPU_DISABLED );
1.266 + PUSH_imm32( EXC_SLOT_FPU_DISABLED );
1.268 + load_spreg( R_ECX, REG_OFFSET(pc) );
1.269 + ADD_r32_r32( R_ESI, R_ECX );
1.270 + ADD_r32_r32( R_ESI, R_ECX );
1.271 + store_spreg( R_ECX, REG_OFFSET(pc) );
1.272 + MOV_moff32_EAX( (uint32_t)&sh4_cpu_period );
1.273 + load_spreg( R_ECX, REG_OFFSET(slice_cycle) );
1.274 + MUL_r32( R_ESI );
1.275 + ADD_r32_r32( R_EAX, R_ECX );
1.276 + store_spreg( R_ECX, REG_OFFSET(slice_cycle) );
1.278 + load_imm32( R_EAX, (uint32_t)sh4_raise_exception ); // 6
1.279 + CALL_r32( R_EAX ); // 2
1.283 + sh4_x86_do_backpatch( end_ptr );
1.287 @@ -138,7 +337,7 @@
1.288 uint32_t sh4_x86_translate_instruction( uint32_t pc )
1.290 uint16_t ir = sh4_read_word( pc );
1.294 /* ALU operations */
1.296 @@ -232,6 +431,18 @@
1.300 + load_reg( R_EAX, Rm );
1.301 + load_reg( R_ECX, Rn );
1.302 + XOR_r32_r32( R_ECX, R_EAX );
1.303 + TEST_r8_r8( R_AL, R_AL );
1.305 + TEST_r8_r8( R_AH, R_AH ); // 2
1.307 + SHR_imm8_r32( 16, R_EAX ); // 3
1.308 + TEST_r8_r8( R_AL, R_AL ); // 2
1.310 + TEST_r8_r8( R_AH, R_AH ); // 2
1.314 load_reg( R_EAX, Rm );
1.315 @@ -379,6 +590,16 @@
1.316 store_reg( R_EAX, Rn );
1.319 + load_reg( R_EAX, Rn );
1.320 + load_reg( R_ECX, Rm );
1.322 + MOV_r32_r32( R_EAX, R_EDX );
1.323 + SHL_r32_CL( R_EAX );
1.324 + NEG_r32( R_ECX );
1.325 + SHR_r32_CL( R_EDX );
1.326 + CMP_imm8s_r32( 0, R_ECX );
1.327 + CMOVAE_r32_r32( R_EDX, R_EAX );
1.328 + store_reg( R_EAX, Rn );
1.331 load_reg( R_EAX, Rn );
1.332 @@ -477,8 +698,19 @@
1.333 TEST_r32_r32( R_EAX, R_ECX );
1.336 -TST #imm, R0 {: :}
1.337 -TST.B #imm, @(R0, GBR) {: :}
1.339 + load_reg( R_EAX, 0 );
1.340 + TEST_imm32_r32( imm, R_EAX );
1.343 +TST.B #imm, @(R0, GBR) {:
1.344 + load_reg( R_EAX, 0);
1.345 + load_reg( R_ECX, R_GBR);
1.346 + ADD_r32_r32( R_EAX, R_ECX );
1.347 + MEM_READ_BYTE( R_ECX, R_EAX );
1.348 + TEST_imm8_r8( imm, R_EAX );
1.352 load_reg( R_EAX, Rm );
1.353 load_reg( R_ECX, Rn );
1.354 @@ -725,9 +957,21 @@
1.357 /* Control transfer instructions */
1.362 + CMP_imm8s_ebp( 0, R_T );
1.364 + exit_block( disp + pc + 4 );
1.368 + CMP_imm8s_ebp( 0, R_T );
1.370 + exit_block( disp + pc + 4 );
1.371 + sh4_x86.in_delay_slot = TRUE;
1.374 + exit_block( disp + pc + 4 );
1.379 @@ -785,7 +1029,10 @@
1.380 FTRV XMTRX, FVn {: :}
1.382 /* Processor control instructions */
1.383 -LDC Rm, SR {: /* We need to be a little careful about SR */ :}
1.385 + load_reg( R_EAX, Rm );
1.386 + write_sr( R_EAX );
1.389 load_reg( R_EAX, Rm );
1.390 store_spreg( R_EAX, R_GBR );
1.391 @@ -819,7 +1066,13 @@
1.392 MEM_READ_LONG( R_ECX, R_EAX );
1.393 store_spreg( R_EAX, R_GBR );
1.397 + load_reg( R_EAX, Rm );
1.398 + MOV_r32_r32( R_EAX, R_ECX );
1.399 + ADD_imm8s_r32( 4, R_EAX );
1.400 + store_reg( R_EAX, Rm );
1.401 + MEM_READ_LONG( R_ECX, R_EAX );
1.402 + write_sr( R_EAX );
1.405 load_reg( R_EAX, Rm );
1.406 @@ -929,7 +1182,9 @@
1.410 - STC SR, Rn {: /* TODO */
1.412 + read_sr( R_EAX );
1.413 + store_reg( R_EAX, Rn );
1.416 load_spreg( R_EAX, R_GBR );
1.417 @@ -955,9 +1210,14 @@
1.418 load_spreg( R_EAX, R_DBR );
1.419 store_reg( R_EAX, Rn );
1.421 - STC Rm_BANK, Rn {: /* TODO */
1.422 +STC Rm_BANK, Rn {: /* TODO */
1.424 - STC.L SR, @-Rn {: /* TODO */
1.425 +STC.L SR, @-Rn {: /* TODO */
1.426 + load_reg( R_ECX, Rn );
1.427 + ADD_imm8s_r32( -4, Rn );
1.428 + store_reg( R_ECX, Rn );
1.429 + read_sr( R_EAX );
1.430 + MEM_WRITE_LONG( R_ECX, R_EAX );
1.433 load_reg( R_ECX, Rn );
1.434 @@ -1060,6 +1320,7 @@
1.436 NOP {: /* Do nothing. Well, we could emit an 0x90, but what would really be the point? */ :}