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_ECX, (uint32_t)ptr);
37 ADD_imm8s_r32( adj, R_ESP );
41 static inline void call_func1( void *ptr, int arg1 )
43 int adj = (-sh4_x86.stack_posn)&0x0F;
44 SUB_imm8s_r32( adj, R_ESP );
46 MOV_r32_r32( arg1, R_EAX );
48 load_imm32(R_ECX, (uint32_t)ptr);
50 ADD_imm8s_r32( adj, R_ESP );
53 static inline void call_func2( void *ptr, int arg1, int arg2 )
55 int adj = (-sh4_x86.stack_posn)&0x0F;
56 SUB_imm8s_r32( adj, R_ESP );
58 MOV_r32_r32( arg2, R_EDX );
61 MOV_r32_r32( arg1, R_EAX );
63 load_imm32(R_ECX, (uint32_t)ptr);
65 ADD_imm8s_r32( adj, R_ESP );
69 * Write a double (64-bit) value into memory, with the first word in arg2a, and
72 static inline void MEM_WRITE_DOUBLE( int addr, int arg2a, int arg2b )
76 call_func2(sh4_write_long, addr, arg2a);
79 ADD_imm8s_r32(4, R_EAX);
80 call_func0(sh4_write_long);
84 * Read a double (64-bit) value from memory, writing the first word into arg2a
85 * and the second into arg2b. The addr must not be in EAX
87 static inline void MEM_READ_DOUBLE( int addr, int arg2a, int arg2b )
90 call_func1(sh4_read_long, addr);
93 MOV_r32_r32(R_ECX, R_EAX);
94 ADD_imm8s_r32(4, R_EAX);
95 call_func0(sh4_read_long);
96 if( arg2b != R_EAX ) {
97 MOV_r32_r32(R_EAX, arg2b);
102 static inline void call_func1( void *ptr, int arg1 )
104 int adj = (-4-sh4_x86.stack_posn)&0x0F;
105 SUB_imm8s_r32( adj, R_ESP );
107 load_imm32(R_EAX, (uint32_t)ptr);
109 ADD_imm8s_r32( adj+4, R_ESP );
110 sh4_x86.stack_posn -= 4;
113 #define CALL_FUNC2_SIZE 15
114 static inline void call_func2( void *ptr, int arg1, int arg2 )
116 int adj = (-8-sh4_x86.stack_posn)&0x0F;
117 SUB_imm8s_r32( adj, R_ESP );
120 load_imm32(R_EAX, (uint32_t)ptr);
122 ADD_imm8s_r32( adj+8, R_ESP );
123 sh4_x86.stack_posn -= 8;
127 * Write a double (64-bit) value into memory, with the first word in arg2a, and
128 * the second in arg2b
130 static inline void MEM_WRITE_DOUBLE( int addr, int arg2a, int arg2b )
132 int adj = (-8-sh4_x86.stack_posn)&0x0F;
133 SUB_imm8s_r32( adj, R_ESP );
134 ADD_imm8s_r32( 4, addr );
137 ADD_imm8s_r32( -4, addr );
138 SUB_imm8s_r32( 8, R_ESP );
141 load_imm32(R_EAX, (uint32_t)sh4_write_long);
143 ADD_imm8s_r32( 16, R_ESP );
144 load_imm32(R_EAX, (uint32_t)sh4_write_long);
146 ADD_imm8s_r32( adj+8, R_ESP );
147 sh4_x86.stack_posn -= 16;
151 * Read a double (64-bit) value from memory, writing the first word into arg2a
152 * and the second into arg2b. The addr must not be in EAX
154 static inline void MEM_READ_DOUBLE( int addr, int arg2a, int arg2b )
156 int adj = (-4-sh4_x86.stack_posn)&0x0F;
157 int adj2 = (-8-sh4_x86.stack_posn)&0x0F;
158 SUB_imm8s_r32( adj, R_ESP );
160 load_imm32(R_EAX, (uint32_t)sh4_read_long);
163 SUB_imm8s_r32( adj2-adj, R_ESP );
165 ADD_imm8s_r32( 4, R_ECX );
167 load_imm32(R_EAX, (uint32_t)sh4_read_long);
169 ADD_imm8s_r32( 4, R_ESP );
170 MOV_r32_r32( R_EAX, arg2b );
172 ADD_imm8s_r32( adj2, R_ESP );
173 sh4_x86.stack_posn -= 4;
179 * Emit the 'start of block' assembly. Sets up the stack frame and save
186 load_ptr( R_EBP, ((uint8_t *)&sh4r) + 128 );
187 sh4_x86.stack_posn = 8;
191 * Exit the block with sh4r.new_pc written with the target pc
193 void exit_block_pcset( sh4addr_t pc )
195 load_imm32( R_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
196 ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) ); // 6
197 load_spreg( R_EAX, R_PC );
198 if( sh4_x86.tlb_on ) {
199 call_func1(xlat_get_code_by_vma,R_EAX);
201 call_func1(xlat_get_code,R_EAX);
208 * Exit the block with sh4r.new_pc written with the target pc
210 void exit_block_newpcset( sh4addr_t pc )
212 load_imm32( R_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
213 ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) ); // 6
214 load_spreg( R_EAX, R_NEW_PC );
215 store_spreg( R_EAX, R_PC );
216 if( sh4_x86.tlb_on ) {
217 call_func1(xlat_get_code_by_vma,R_EAX);
219 call_func1(xlat_get_code,R_EAX);
227 * Exit the block to an absolute PC
229 void exit_block( sh4addr_t pc, sh4addr_t endpc )
231 load_imm32( R_ECX, pc ); // 5
232 store_spreg( R_ECX, REG_OFFSET(pc) ); // 3
233 if( IS_IN_ICACHE(pc) ) {
234 MOV_moff32_EAX( xlat_get_lut_entry(GET_ICACHE_PHYS(pc)) ); // 5
235 } else if( sh4_x86.tlb_on ) {
236 call_func1(xlat_get_code_by_vma,R_ECX);
238 call_func1(xlat_get_code,R_ECX);
240 AND_imm8s_r32( 0xFC, R_EAX ); // 3
241 load_imm32( R_ECX, ((endpc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
242 ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) ); // 6
248 * Exit the block to a relative PC
250 void exit_block_rel( sh4addr_t pc, sh4addr_t endpc )
252 load_imm32( R_ECX, pc - sh4_x86.block_start_pc ); // 5
253 ADD_sh4r_r32( R_PC, R_ECX );
254 store_spreg( R_ECX, REG_OFFSET(pc) ); // 3
255 if( IS_IN_ICACHE(pc) ) {
256 MOV_moff32_EAX( xlat_get_lut_entry(GET_ICACHE_PHYS(pc)) ); // 5
257 } else if( sh4_x86.tlb_on ) {
258 call_func1(xlat_get_code_by_vma,R_ECX);
260 call_func1(xlat_get_code,R_ECX);
262 AND_imm8s_r32( 0xFC, R_EAX ); // 3
263 load_imm32( R_ECX, ((endpc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
264 ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) ); // 6
270 * Write the block trailer (exception handling block)
272 void sh4_translate_end_block( sh4addr_t pc ) {
273 if( sh4_x86.branch_taken == FALSE ) {
274 // Didn't exit unconditionally already, so write the termination here
275 exit_block_rel( pc, pc );
277 if( sh4_x86.backpatch_posn != 0 ) {
280 uint8_t *end_ptr = xlat_output;
281 MOV_r32_r32( R_EDX, R_ECX );
282 ADD_r32_r32( R_EDX, R_ECX );
283 ADD_r32_sh4r( R_ECX, R_PC );
284 MOV_moff32_EAX( &sh4_cpu_period );
286 ADD_r32_sh4r( R_EAX, REG_OFFSET(slice_cycle) );
289 call_func1( sh4_raise_exception, R_EDX );
290 load_spreg( R_EAX, R_PC );
291 if( sh4_x86.tlb_on ) {
292 call_func1(xlat_get_code_by_vma,R_EAX);
294 call_func1(xlat_get_code,R_EAX);
299 // Exception already raised - just cleanup
300 uint8_t *preexc_ptr = xlat_output;
301 MOV_r32_r32( R_EDX, R_ECX );
302 ADD_r32_r32( R_EDX, R_ECX );
303 ADD_r32_sh4r( R_ECX, R_SPC );
304 MOV_moff32_EAX( &sh4_cpu_period );
306 ADD_r32_sh4r( R_EAX, REG_OFFSET(slice_cycle) );
307 load_spreg( R_EAX, R_PC );
308 if( sh4_x86.tlb_on ) {
309 call_func1(xlat_get_code_by_vma,R_EAX);
311 call_func1(xlat_get_code,R_EAX);
316 for( i=0; i< sh4_x86.backpatch_posn; i++ ) {
317 uint32_t *fixup_addr = (uint32_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset];
318 *fixup_addr = xlat_output - (uint8_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset] - 4;
319 if( sh4_x86.backpatch_list[i].exc_code < 0 ) {
320 load_imm32( R_EDX, sh4_x86.backpatch_list[i].fixup_icount );
321 int stack_adj = -1 - sh4_x86.backpatch_list[i].exc_code;
322 if( stack_adj > 0 ) {
323 ADD_imm8s_r32( stack_adj, R_ESP );
325 int rel = preexc_ptr - xlat_output;
328 PUSH_imm32( sh4_x86.backpatch_list[i].exc_code );
329 load_imm32( R_EDX, sh4_x86.backpatch_list[i].fixup_icount );
330 int rel = end_ptr - xlat_output;
339 * The unwind methods only work if we compiled with DWARF2 frame information
340 * (ie -fexceptions), otherwise we have to use the direct frame scan.
342 #ifdef HAVE_EXCEPTIONS
346 uintptr_t block_start;
351 _Unwind_Reason_Code xlat_check_frame( struct _Unwind_Context *context, void *arg )
353 struct UnwindInfo *info = arg;
354 void *pc = (void *)_Unwind_GetIP(context);
355 if( ((uintptr_t)pc) >= info->block_start && ((uintptr_t)pc) < info->block_end ) {
357 return _URC_NORMAL_STOP;
360 return _URC_NO_REASON;
363 void *xlat_get_native_pc( void *code, uint32_t code_size )
365 struct _Unwind_Exception exc;
366 struct UnwindInfo info;
369 info.block_start = (uintptr_t)code;
370 info.block_end = info.block_start + code_size;
372 _Unwind_Backtrace( xlat_check_frame, &info );
376 void *xlat_get_native_pc( void *code, uint32_t code_size )
380 "mov %%ebp, %%eax\n\t"
381 "mov $0x8, %%ecx\n\t"
383 "frame_loop: test %%eax, %%eax\n\t"
384 "je frame_not_found\n\t"
385 "cmp (%%eax), %%edx\n\t"
387 "sub $0x1, %%ecx\n\t"
388 "je frame_not_found\n\t"
389 "movl (%%eax), %%eax\n\t"
391 "frame_found: movl 0x4(%%eax), %0\n"
394 : "r" (((uint8_t *)&sh4r) + 128 )
395 : "eax", "ecx", "edx" );
400 #endif /* !lxdream_ia32mac.h */
.