Search
lxdream.org :: lxdream/src/sh4/ia32abi.h
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/ia32abi.h
changeset 968:6fb1481859a4
prev956:4c1ed9e03985
next991:60c7fab9c880
author nkeynes
date Sun Feb 15 21:47:39 2009 +0000 (15 years ago)
permissions -rw-r--r--
last change Compile in C99 standards mode, albeit with -D_GNU_SOURCE. Helps portability slightly as well
view annotate diff log raw
     1 /**
     2  * $Id$
     3  * 
     4  * Provides the implementation for the ia32 ABI variant 
     5  * (eg prologue, epilogue, and calling conventions). Stack frame is
     6  * aligned on 16-byte boundaries for the benefit of OS X (which 
     7  * requires it).
     8  *
     9  * Copyright (c) 2007 Nathan Keynes.
    10  *
    11  * This program is free software; you can redistribute it and/or modify
    12  * it under the terms of the GNU General Public License as published by
    13  * the Free Software Foundation; either version 2 of the License, or
    14  * (at your option) any later version.
    15  *
    16  * This program is distributed in the hope that it will be useful,
    17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19  * GNU General Public License for more details.
    20  */
    22 #ifndef lxdream_ia32mac_H
    23 #define lxdream_ia32mac_H 1
    25 #define load_ptr( reg, ptr ) load_imm32( reg, (uint32_t)ptr );
    27 static inline void decode_address( int addr_reg )
    28 {
    29     uintptr_t base = (sh4r.xlat_sh4_mode&SR_MD) ? (uintptr_t)sh4_address_space : (uintptr_t)sh4_user_address_space;
    30     MOV_r32_r32( addr_reg, R_ECX ); 
    31     SHR_imm8_r32( 12, R_ECX ); 
    32     MOV_r32disp32x4_r32( R_ECX, base, R_ECX );
    33 }
    35 /**
    36  * Note: clobbers EAX to make the indirect call - this isn't usually
    37  * a problem since the callee will usually clobber it anyway.
    38  */
    39 static inline void call_func0( void *ptr )
    40 {
    41     load_imm32(R_ECX, (uint32_t)ptr);
    42     CALL_r32(R_ECX);
    43 }
    45 #ifdef HAVE_FASTCALL
    46 static inline void call_func1( void *ptr, int arg1 )
    47 {
    48     if( arg1 != R_EAX ) {
    49         MOV_r32_r32( arg1, R_EAX );
    50     }
    51     load_imm32(R_ECX, (uint32_t)ptr);
    52     CALL_r32(R_ECX);
    53 }
    55 static inline void call_func1_r32( int addr_reg, int arg1 )
    56 {
    57     if( arg1 != R_EAX ) {
    58         MOV_r32_r32( arg1, R_EAX );
    59     }
    60     CALL_r32(addr_reg);
    61 }
    63 static inline void call_func1_r32disp8( int preg, uint32_t disp8, int arg1 )
    64 {
    65     if( arg1 != R_EAX ) {
    66         MOV_r32_r32( arg1, R_EAX );
    67     }
    68     CALL_r32disp8(preg, disp8);
    69 }
    71 static inline void call_func1_r32disp8_exc( int preg, uint32_t disp8, int arg1, int pc )
    72 {
    73     if( arg1 != R_EAX ) {
    74         MOV_r32_r32( arg1, R_EAX );
    75     }
    76     load_exc_backpatch(R_EDX);
    77     CALL_r32disp8(preg, disp8);
    78 }
    80 static inline void call_func2( void *ptr, int arg1, int arg2 )
    81 {
    82     if( arg2 != R_EDX ) {
    83         MOV_r32_r32( arg2, R_EDX );
    84     }
    85     if( arg1 != R_EAX ) {
    86         MOV_r32_r32( arg1, R_EAX );
    87     }
    88     load_imm32(R_ECX, (uint32_t)ptr);
    89     CALL_r32(R_ECX);
    90 }
    92 static inline void call_func2_r32( int addr_reg, int arg1, int arg2 )
    93 {
    94     if( arg2 != R_EDX ) {
    95         MOV_r32_r32( arg2, R_EDX );
    96     }
    97     if( arg1 != R_EAX ) {
    98         MOV_r32_r32( arg1, R_EAX );
    99     }
   100     CALL_r32(addr_reg);
   101 }
   103 static inline void call_func2_r32disp8( int preg, uint32_t disp8, int arg1, int arg2 )
   104 {
   105     if( arg2 != R_EDX ) {
   106         MOV_r32_r32( arg2, R_EDX );
   107     }
   108     if( arg1 != R_EAX ) {
   109         MOV_r32_r32( arg1, R_EAX );
   110     }
   111     CALL_r32disp8(preg, disp8);
   112 }
   114 static inline void call_func2_r32disp8_exc( int preg, uint32_t disp8, int arg1, int arg2, int pc )
   115 {
   116     if( arg2 != R_EDX ) {
   117         MOV_r32_r32( arg2, R_EDX );
   118     }
   119     if( arg1 != R_EAX ) {
   120         MOV_r32_r32( arg1, R_EAX );
   121     }
   122     MOV_backpatch_esp8( 0 );
   123     CALL_r32disp8(preg, disp8);
   124 }
   128 static inline void call_func1_exc( void *ptr, int arg1, int pc )
   129 {
   130     if( arg1 != R_EAX ) {
   131         MOV_r32_r32( arg1, R_EAX );
   132     }
   133     load_exc_backpatch(R_EDX);
   134     load_imm32(R_ECX, (uint32_t)ptr);
   135     CALL_r32(R_ECX);
   136 }   
   138 static inline void call_func2_exc( void *ptr, int arg1, int arg2, int pc )
   139 {
   140     if( arg2 != R_EDX ) {
   141         MOV_r32_r32( arg2, R_EDX );
   142     }
   143     if( arg1 != R_EAX ) {
   144         MOV_r32_r32( arg1, R_EAX );
   145     }
   146     MOV_backpatch_esp8(0);
   147     load_imm32(R_ECX, (uint32_t)ptr);
   148     CALL_r32(R_ECX);
   149 }
   151 #else
   152 static inline void call_func1( void *ptr, int arg1 )
   153 {
   154     SUB_imm8s_r32( 12, R_ESP );
   155     PUSH_r32(arg1);
   156     load_imm32(R_ECX, (uint32_t)ptr);
   157     CALL_r32(R_ECX);
   158     ADD_imm8s_r32( 16, R_ESP );
   159 }
   161 static inline void call_func2( void *ptr, int arg1, int arg2 )
   162 {
   163     SUB_imm8s_r32( 8, R_ESP );
   164     PUSH_r32(arg2);
   165     PUSH_r32(arg1);
   166     load_imm32(R_ECX, (uint32_t)ptr);
   167     CALL_r32(R_ECX);
   168     ADD_imm8s_r32( 16, R_ESP );
   169 }
   171 #endif
   173 /**
   174  * Emit the 'start of block' assembly. Sets up the stack frame and save
   175  * SI/DI as required
   176  * Allocates 8 bytes for local variables, which also has the convenient
   177  * side-effect of aligning the stack.
   178  */
   179 void enter_block( ) 
   180 {
   181     PUSH_r32(R_EBP);
   182     load_ptr( R_EBP, ((uint8_t *)&sh4r) + 128 );
   183     SUB_imm8s_r32( 8, R_ESP ); 
   184 }
   186 static inline void exit_block( )
   187 {
   188     ADD_imm8s_r32( 8, R_ESP );
   189     POP_r32(R_EBP);
   190     RET();
   191 }
   193 /**
   194  * Exit the block with sh4r.new_pc written with the target pc
   195  */
   196 void exit_block_pcset( sh4addr_t pc )
   197 {
   198     load_imm32( R_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
   199     ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );    // 6
   200     load_spreg( R_EAX, R_PC );
   201     if( sh4_x86.tlb_on ) {
   202         call_func1(xlat_get_code_by_vma,R_EAX);
   203     } else {
   204         call_func1(xlat_get_code,R_EAX);
   205     }
   206     exit_block();
   207 }
   209 /**
   210  * Exit the block with sh4r.new_pc written with the target pc
   211  */
   212 void exit_block_newpcset( sh4addr_t pc )
   213 {
   214     load_imm32( R_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
   215     ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );    // 6
   216     load_spreg( R_EAX, R_NEW_PC );
   217     store_spreg( R_EAX, R_PC );
   218     if( sh4_x86.tlb_on ) {
   219         call_func1(xlat_get_code_by_vma,R_EAX);
   220     } else {
   221         call_func1(xlat_get_code,R_EAX);
   222     }
   223     exit_block();
   224 }
   227 /**
   228  * Exit the block to an absolute PC
   229  */
   230 void exit_block_abs( sh4addr_t pc, sh4addr_t endpc )
   231 {
   232     load_imm32( R_ECX, pc );                            // 5
   233     store_spreg( R_ECX, REG_OFFSET(pc) );               // 3
   234     if( IS_IN_ICACHE(pc) ) {
   235         MOV_moff32_EAX( xlat_get_lut_entry(GET_ICACHE_PHYS(pc)) ); // 5
   236         AND_imm8s_r32( 0xFC, R_EAX ); // 3
   237     } else if( sh4_x86.tlb_on ) {
   238         call_func1(xlat_get_code_by_vma,R_ECX);
   239     } else {
   240         call_func1(xlat_get_code,R_ECX);
   241     }
   242     load_imm32( R_ECX, ((endpc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
   243     ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );     // 6
   244     exit_block();
   245 }
   247 /**
   248  * Exit the block to a relative PC
   249  */
   250 void exit_block_rel( sh4addr_t pc, sh4addr_t endpc )
   251 {
   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         AND_imm8s_r32( 0xFC, R_EAX ); // 3
   258     } else if( sh4_x86.tlb_on ) {
   259         call_func1(xlat_get_code_by_vma,R_ECX);
   260     } else {
   261         call_func1(xlat_get_code,R_ECX);
   262     }
   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
   265     exit_block();
   266 }
   268 /**
   269  * Exit unconditionally with a general exception
   270  */
   271 void exit_block_exc( int code, sh4addr_t pc )
   272 {
   273     load_imm32( R_ECX, pc - sh4_x86.block_start_pc );   // 5
   274     ADD_r32_sh4r( R_ECX, R_PC );
   275     load_imm32( R_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
   276     ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );     // 6
   277     load_imm32( R_EAX, code );
   278     call_func1( sh4_raise_exception, R_EAX );
   280     load_spreg( R_EAX, R_PC );
   281     if( sh4_x86.tlb_on ) {
   282         call_func1(xlat_get_code_by_vma,R_EAX);
   283     } else {
   284         call_func1(xlat_get_code,R_EAX);
   285     }
   287     exit_block();
   288 }    
   290 /**
   291  * Write the block trailer (exception handling block)
   292  */
   293 void sh4_translate_end_block( sh4addr_t pc ) {
   294     if( sh4_x86.branch_taken == FALSE ) {
   295         // Didn't exit unconditionally already, so write the termination here
   296         exit_block_rel( pc, pc );
   297     }
   298     if( sh4_x86.backpatch_posn != 0 ) {
   299         unsigned int i;
   300         // Raise exception
   301         uint8_t *end_ptr = xlat_output;
   302         MOV_r32_r32( R_EDX, R_ECX );
   303         ADD_r32_r32( R_EDX, R_ECX );
   304         ADD_r32_sh4r( R_ECX, R_PC );
   305         MOV_moff32_EAX( &sh4_cpu_period );
   306         MUL_r32( R_EDX );
   307         ADD_r32_sh4r( R_EAX, REG_OFFSET(slice_cycle) );
   309         POP_r32(R_EAX);
   310         call_func1( sh4_raise_exception, R_EAX );
   311         load_spreg( R_EAX, R_PC );
   312         if( sh4_x86.tlb_on ) {
   313             call_func1(xlat_get_code_by_vma,R_EAX);
   314         } else {
   315             call_func1(xlat_get_code,R_EAX);
   316         }
   317         exit_block();
   319         // Exception already raised - just cleanup
   320         uint8_t *preexc_ptr = xlat_output;
   321         MOV_r32_r32( R_EDX, R_ECX );
   322         ADD_r32_r32( R_EDX, R_ECX );
   323         ADD_r32_sh4r( R_ECX, R_SPC );
   324         MOV_moff32_EAX( &sh4_cpu_period );
   325         MUL_r32( R_EDX );
   326         ADD_r32_sh4r( R_EAX, REG_OFFSET(slice_cycle) );
   327         load_spreg( R_EAX, R_PC );
   328         if( sh4_x86.tlb_on ) {
   329             call_func1(xlat_get_code_by_vma,R_EAX);
   330         } else {
   331             call_func1(xlat_get_code,R_EAX);
   332         }
   333         exit_block();
   335         for( i=0; i< sh4_x86.backpatch_posn; i++ ) {
   336             uint32_t *fixup_addr = (uint32_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset];
   337             if( sh4_x86.backpatch_list[i].exc_code < 0 ) {
   338                 if( sh4_x86.backpatch_list[i].exc_code == -2 ) {
   339                     *fixup_addr = (uint32_t)xlat_output;
   340                 } else {
   341                     *fixup_addr += xlat_output - (uint8_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset] - 4;
   342                 }
   343                 load_imm32( R_EDX, sh4_x86.backpatch_list[i].fixup_icount );
   344                 int rel = preexc_ptr - xlat_output;
   345                 JMP_rel(rel);
   346             } else {
   347                 *fixup_addr += xlat_output - (uint8_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset] - 4;
   348                 PUSH_imm32( sh4_x86.backpatch_list[i].exc_code );
   349                 load_imm32( R_EDX, sh4_x86.backpatch_list[i].fixup_icount );
   350                 int rel = end_ptr - xlat_output;
   351                 JMP_rel(rel);
   352             }
   353         }
   354     }
   355 }
   358 /**
   359  * The unwind methods only work if we compiled with DWARF2 frame information
   360  * (ie -fexceptions), otherwise we have to use the direct frame scan.
   361  */
   362 #ifdef HAVE_EXCEPTIONS
   363 #include <unwind.h>
   365 struct UnwindInfo {
   366     uintptr_t block_start;
   367     uintptr_t block_end;
   368     void *pc;
   369 };
   371 _Unwind_Reason_Code xlat_check_frame( struct _Unwind_Context *context, void *arg )
   372 {
   373     struct UnwindInfo *info = arg;
   374     void *pc = (void *)_Unwind_GetIP(context);
   375     if( ((uintptr_t)pc) >= info->block_start && ((uintptr_t)pc) < info->block_end ) {
   376         info->pc = pc;
   377         return _URC_NORMAL_STOP;
   378     }
   380     return _URC_NO_REASON;
   381 }
   383 void *xlat_get_native_pc( void *code, uint32_t code_size )
   384 {
   385     struct _Unwind_Exception exc;
   386     struct UnwindInfo info;
   388     info.pc = NULL;
   389     info.block_start = (uintptr_t)code;
   390     info.block_end = info.block_start + code_size;
   391     void *result = NULL;
   392     _Unwind_Backtrace( xlat_check_frame, &info );
   393     return info.pc;
   394 }
   395 #else 
   396 void *xlat_get_native_pc( void *code, uint32_t code_size )
   397 {
   398     void *result = NULL;
   399     asm(
   400         "mov %%ebp, %%eax\n\t"
   401         "mov $0x8, %%ecx\n\t"
   402         "mov %1, %%edx\n"
   403         "frame_loop: test %%eax, %%eax\n\t"
   404         "je frame_not_found\n\t"
   405         "cmp (%%eax), %%edx\n\t"
   406         "je frame_found\n\t"
   407         "sub $0x1, %%ecx\n\t"
   408         "je frame_not_found\n\t"
   409         "movl (%%eax), %%eax\n\t"
   410         "jmp frame_loop\n"
   411         "frame_found: movl 0x4(%%eax), %0\n"
   412         "frame_not_found:"
   413         : "=r" (result)
   414         : "r" (((uint8_t *)&sh4r) + 128 )
   415         : "eax", "ecx", "edx" );
   416     return result;
   417 }
   418 #endif
   420 #endif /* !lxdream_ia32mac.h */
.