4 * Provides the implementation for the ia32 Mac OS X ABI variant
5 * (eg prologue, epilogue, and calling conventions). Main difference
6 * from ia32abi is that stack frames are aligned on 16-byte boundaries.
8 * Copyright (c) 2007 Nathan Keynes.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #ifndef lxdream_ia32mac_H
22 #define lxdream_ia32mac_H 1
24 #define load_ptr( reg, ptr ) load_imm32( reg, (uint32_t)ptr );
27 * Note: clobbers EAX to make the indirect call - this isn't usually
28 * a problem since the callee will usually clobber it anyway.
30 #define CALL_FUNC0_SIZE 13
31 static inline void call_func0( void *ptr )
33 int adj = (-sh4_x86.stack_posn)&0x0F;
34 SUB_imm8s_r32( adj, R_ESP );
35 load_imm32(R_EAX, (uint32_t)ptr);
37 ADD_imm8s_r32( adj, R_ESP );
40 #define CALL_FUNC1_SIZE 14
41 static inline void call_func1( void *ptr, int arg1 )
43 int adj = (-4-sh4_x86.stack_posn)&0x0F;
44 SUB_imm8s_r32( adj, R_ESP );
46 load_imm32(R_EAX, (uint32_t)ptr);
48 ADD_imm8s_r32( adj+4, R_ESP );
49 sh4_x86.stack_posn -= 4;
52 #define CALL_FUNC2_SIZE 15
53 static inline void call_func2( void *ptr, int arg1, int arg2 )
55 int adj = (-8-sh4_x86.stack_posn)&0x0F;
56 SUB_imm8s_r32( adj, R_ESP );
59 load_imm32(R_EAX, (uint32_t)ptr);
61 ADD_imm8s_r32( adj+8, R_ESP );
62 sh4_x86.stack_posn -= 8;
66 * Write a double (64-bit) value into memory, with the first word in arg2a, and
70 #define MEM_WRITE_DOUBLE_SIZE 36
71 static inline void MEM_WRITE_DOUBLE( int addr, int arg2a, int arg2b )
73 int adj = (-8-sh4_x86.stack_posn)&0x0F;
74 SUB_imm8s_r32( adj, R_ESP );
75 ADD_imm8s_r32( 4, addr );
78 ADD_imm8s_r32( -4, addr );
79 SUB_imm8s_r32( 8, R_ESP );
82 load_imm32(R_EAX, (uint32_t)sh4_write_long);
84 ADD_imm8s_r32( 16, R_ESP );
85 load_imm32(R_EAX, (uint32_t)sh4_write_long);
87 ADD_imm8s_r32( adj+8, R_ESP );
88 sh4_x86.stack_posn -= 16;
92 * Read a double (64-bit) value from memory, writing the first word into arg2a
93 * and the second into arg2b. The addr must not be in EAX
96 #define MEM_READ_DOUBLE_SIZE 36
97 static inline void MEM_READ_DOUBLE( int addr, int arg2a, int arg2b )
99 int adj = (-4-sh4_x86.stack_posn)&0x0F;
100 int adj2 = (-8-sh4_x86.stack_posn)&0x0F;
101 SUB_imm8s_r32( adj, R_ESP );
103 load_imm32(R_EAX, (uint32_t)sh4_read_long);
106 SUB_imm8s_r32( adj2-adj, R_ESP );
108 ADD_imm8s_r32( 4, R_ECX );
110 load_imm32(R_EAX, (uint32_t)sh4_read_long);
112 ADD_imm8s_r32( 4, R_ESP );
113 MOV_r32_r32( R_EAX, arg2b );
115 ADD_imm8s_r32( adj2, R_ESP );
116 sh4_x86.stack_posn -= 4;
120 * Emit the 'start of block' assembly. Sets up the stack frame and save
127 load_ptr( R_EBP, ((uint8_t *)&sh4r) + 128 );
128 sh4_x86.stack_posn = 8;
132 * Exit the block with sh4r.new_pc written with the target pc
134 void exit_block_pcset( sh4addr_t pc )
136 load_imm32( R_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
137 ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) ); // 6
138 load_spreg( R_EAX, R_PC );
139 if( sh4_x86.tlb_on ) {
140 call_func1(xlat_get_code_by_vma,R_EAX);
142 call_func1(xlat_get_code,R_EAX);
149 * Exit the block with sh4r.new_pc written with the target pc
151 void exit_block_newpcset( sh4addr_t pc )
153 load_imm32( R_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
154 ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) ); // 6
155 load_spreg( R_EAX, R_NEW_PC );
156 store_spreg( R_EAX, R_PC );
157 if( sh4_x86.tlb_on ) {
158 call_func1(xlat_get_code_by_vma,R_EAX);
160 call_func1(xlat_get_code,R_EAX);
167 #define EXIT_BLOCK_SIZE(pc) (24 + (IS_IN_ICACHE(pc)?5:CALL_FUNC1_SIZE))
171 * Exit the block to an absolute PC
173 void exit_block( sh4addr_t pc, sh4addr_t endpc )
175 load_imm32( R_ECX, pc ); // 5
176 store_spreg( R_ECX, REG_OFFSET(pc) ); // 3
177 if( IS_IN_ICACHE(pc) ) {
178 MOV_moff32_EAX( xlat_get_lut_entry(GET_ICACHE_PHYS(pc)) ); // 5
179 } else if( sh4_x86.tlb_on ) {
180 call_func1(xlat_get_code_by_vma,R_ECX);
182 call_func1(xlat_get_code,R_ECX);
184 AND_imm8s_r32( 0xFC, R_EAX ); // 3
185 load_imm32( R_ECX, ((endpc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
186 ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) ); // 6
191 #define EXIT_BLOCK_REL_SIZE(pc) (27 + (IS_IN_ICACHE(pc)?5:CALL_FUNC1_SIZE))
194 * Exit the block to a relative PC
196 void exit_block_rel( sh4addr_t pc, sh4addr_t endpc )
198 load_imm32( R_ECX, pc - sh4_x86.block_start_pc ); // 5
199 ADD_sh4r_r32( R_PC, R_ECX );
200 store_spreg( R_ECX, REG_OFFSET(pc) ); // 3
201 if( IS_IN_ICACHE(pc) ) {
202 MOV_moff32_EAX( xlat_get_lut_entry(GET_ICACHE_PHYS(pc)) ); // 5
203 } else if( sh4_x86.tlb_on ) {
204 call_func1(xlat_get_code_by_vma,R_ECX);
206 call_func1(xlat_get_code,R_ECX);
208 AND_imm8s_r32( 0xFC, R_EAX ); // 3
209 load_imm32( R_ECX, ((endpc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
210 ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) ); // 6
216 * Write the block trailer (exception handling block)
218 void sh4_translate_end_block( sh4addr_t pc ) {
219 if( sh4_x86.branch_taken == FALSE ) {
220 // Didn't exit unconditionally already, so write the termination here
221 exit_block_rel( pc, pc );
223 if( sh4_x86.backpatch_posn != 0 ) {
226 uint8_t *end_ptr = xlat_output;
227 MOV_r32_r32( R_EDX, R_ECX );
228 ADD_r32_r32( R_EDX, R_ECX );
229 ADD_r32_sh4r( R_ECX, R_PC );
230 MOV_moff32_EAX( &sh4_cpu_period );
232 ADD_r32_sh4r( R_EAX, REG_OFFSET(slice_cycle) );
235 call_func1( sh4_raise_exception, R_EDX );
236 load_spreg( R_EAX, R_PC );
237 if( sh4_x86.tlb_on ) {
238 call_func1(xlat_get_code_by_vma,R_EAX);
240 call_func1(xlat_get_code,R_EAX);
245 // Exception already raised - just cleanup
246 uint8_t *preexc_ptr = xlat_output;
247 MOV_r32_r32( R_EDX, R_ECX );
248 ADD_r32_r32( R_EDX, R_ECX );
249 ADD_r32_sh4r( R_ECX, R_SPC );
250 MOV_moff32_EAX( &sh4_cpu_period );
252 ADD_r32_sh4r( R_EAX, REG_OFFSET(slice_cycle) );
253 load_spreg( R_EAX, R_PC );
254 if( sh4_x86.tlb_on ) {
255 call_func1(xlat_get_code_by_vma,R_EAX);
257 call_func1(xlat_get_code,R_EAX);
262 for( i=0; i< sh4_x86.backpatch_posn; i++ ) {
263 uint32_t *fixup_addr = (uint32_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset];
264 *fixup_addr = xlat_output - (uint8_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset] - 4;
265 if( sh4_x86.backpatch_list[i].exc_code < 0 ) {
266 load_imm32( R_EDX, sh4_x86.backpatch_list[i].fixup_icount );
267 int stack_adj = -1 - sh4_x86.backpatch_list[i].exc_code;
268 if( stack_adj > 0 ) {
269 ADD_imm8s_r32( stack_adj, R_ESP );
271 int rel = preexc_ptr - xlat_output;
274 PUSH_imm32( sh4_x86.backpatch_list[i].exc_code );
275 load_imm32( R_EDX, sh4_x86.backpatch_list[i].fixup_icount );
276 int rel = end_ptr - xlat_output;
283 void *xlat_get_native_pc()
287 "mov %%ebp, %%eax\n\t"
288 "mov $0x8, %%ecx\n\t"
290 "frame_loop: test %%eax, %%eax\n\t"
291 "je frame_not_found\n\t"
292 "cmp (%%eax), %%edx\n\t"
294 "sub $0x1, %%ecx\n\t"
295 "je frame_not_found\n\t"
296 "movl (%%eax), %%eax\n\t"
298 "frame_found: movl 0x4(%%eax), %0\n"
301 : "r" (((uint8_t *)&sh4r) + 128 )
302 : "eax", "ecx", "edx" );
307 #endif /* !lxdream_ia32mac.h */
.