Search
lxdream.org :: lxdream/src/sh4/ia64abi.h
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/ia64abi.h
changeset 926:68f3e0fe02f1
prev908:a00debcf2600
next927:17b6b9e245d8
author nkeynes
date Sun Dec 14 07:50:48 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Setup a 'proper' stackframe in translated blocks. This doesn't affect performance noticeably,
but does ensure that
a) The stack is aligned correctly on OS X with no extra effort, and
b) We can't mess up the stack and crash that way anymore.
Replace all PUSH/POP instructions (outside of prologue/epilogue) with ESP-rel moves to stack
local variables.
Finally merge ia32mac and ia32abi together, since they're pretty much the same now anyway (and
thereby simplifying maintenance a good deal)
view annotate diff log raw
     1 /**
     2  * $Id$
     3  * 
     4  * Provides the implementation for the AMD64 ABI (eg prologue, epilogue, and
     5  * calling conventions)
     6  *
     7  * Copyright (c) 2007 Nathan Keynes.
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  */
    20 #ifndef lxdream_ia64abi_H
    21 #define lxdream_ia64abi_H 1
    23 #include <unwind.h>
    25 #define load_ptr( reg, ptr ) load_imm64( reg, (uint64_t)ptr );
    27 /**
    28  * Note: clobbers EAX to make the indirect call - this isn't usually
    29  * a problem since the callee will usually clobber it anyway.
    30  * Size: 12 bytes
    31  */
    32 #define CALL_FUNC0_SIZE 12
    33 static inline void call_func0( void *ptr )
    34 {
    35     load_imm64(R_EAX, (uint64_t)ptr);
    36     CALL_r32(R_EAX);
    37 }
    39 #define CALL_FUNC1_SIZE 14
    40 static inline void call_func1( void *ptr, int arg1 )
    41 {
    42     REXW(); MOV_r32_r32(arg1, R_EDI);
    43     call_func0(ptr);
    44 }
    46 #define CALL_FUNC2_SIZE 16
    47 static inline void call_func2( void *ptr, int arg1, int arg2 )
    48 {
    49     REXW(); MOV_r32_r32(arg1, R_EDI);
    50     REXW(); MOV_r32_r32(arg2, R_ESI);
    51     call_func0(ptr);
    52 }
    54 #define MEM_WRITE_DOUBLE_SIZE 35
    55 /**
    56  * Write a double (64-bit) value into memory, with the first word in arg2a, and
    57  * the second in arg2b
    58  */
    59 static inline void MEM_WRITE_DOUBLE( int addr, int arg2a, int arg2b )
    60 {
    61     PUSH_r32(arg2b);
    62     PUSH_r32(addr);
    63     call_func2(sh4_write_long, addr, arg2a);
    64     POP_r32(R_EDI);
    65     POP_r32(R_ESI);
    66     ADD_imm8s_r32(4, R_EDI);
    67     call_func0(sh4_write_long);
    68 }
    70 #define MEM_READ_DOUBLE_SIZE 43
    71 /**
    72  * Read a double (64-bit) value from memory, writing the first word into arg2a
    73  * and the second into arg2b. The addr must not be in EAX
    74  */
    75 static inline void MEM_READ_DOUBLE( int addr, int arg2a, int arg2b )
    76 {
    77     REXW(); SUB_imm8s_r32( 8, R_ESP );
    78     PUSH_r32(addr);
    79     call_func1(sh4_read_long, addr);
    80     POP_r32(R_EDI);
    81     PUSH_r32(R_EAX);
    82     ADD_imm8s_r32(4, R_EDI);
    83     call_func0(sh4_read_long);
    84     MOV_r32_r32(R_EAX, arg2b);
    85     POP_r32(arg2a);
    86     REXW(); ADD_imm8s_r32( 8, R_ESP );
    87 }
    90 /**
    91  * Emit the 'start of block' assembly. Sets up the stack frame and save
    92  * SI/DI as required
    93  */
    94 void enter_block( ) 
    95 {
    96     PUSH_r32(R_EBP);
    97     load_ptr( R_EBP, ((uint8_t *)&sh4r) + 128 );
    98     // Minimum aligned allocation is 16 bytes
    99     REXW(); SUB_imm8s_r32( 16, R_ESP );
   100 }
   102 static inline void exit_block( )
   103 {
   104     REXW(); ADD_imm8s_r32( 16, R_ESP );
   105     POP_r32(R_EBP);
   106     RET();
   107 }
   109 /**
   110  * Exit the block with sh4r.pc already written
   111  */
   112 void exit_block_pcset( sh4addr_t pc )
   113 {
   114     load_imm32( R_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
   115     ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );    // 6
   116     load_spreg( R_EAX, R_PC );
   117     if( sh4_x86.tlb_on ) {
   118         call_func1(xlat_get_code_by_vma,R_EAX);
   119     } else {
   120         call_func1(xlat_get_code,R_EAX);
   121     }
   122     exit_block();
   123 }
   125 /**
   126  * Exit the block with sh4r.new_pc written with the target address
   127  */
   128 void exit_block_newpcset( sh4addr_t pc )
   129 {
   130     load_imm32( R_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
   131     ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );    // 6
   132     load_spreg( R_EAX, R_NEW_PC );
   133     store_spreg( R_EAX, R_PC );
   134     if( sh4_x86.tlb_on ) {
   135         call_func1(xlat_get_code_by_vma,R_EAX);
   136     } else {
   137         call_func1(xlat_get_code,R_EAX);
   138     }
   139     exit_block();
   140 }
   142 #define EXIT_BLOCK_SIZE(pc) (25 + (IS_IN_ICACHE(pc)?10:CALL_FUNC1_SIZE))
   143 /**
   144  * Exit the block to an absolute PC
   145  */
   146 void exit_block_abs( sh4addr_t pc, sh4addr_t endpc )
   147 {
   148     load_imm32( R_ECX, pc );                            // 5
   149     store_spreg( R_ECX, REG_OFFSET(pc) );               // 3
   150     if( IS_IN_ICACHE(pc) ) {
   151         REXW(); MOV_moff32_EAX( xlat_get_lut_entry(pc) );
   152         REXW(); AND_imm8s_r32( 0xFC, R_EAX ); // 4
   153     } else if( sh4_x86.tlb_on ) {
   154         call_func1(xlat_get_code_by_vma, R_ECX);
   155     } else {
   156         call_func1(xlat_get_code,R_ECX);
   157     }
   158     load_imm32( R_ECX, ((endpc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
   159     ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );     // 6
   160     exit_block();
   161 }
   164 #define EXIT_BLOCK_REL_SIZE(pc)  (28 + (IS_IN_ICACHE(pc)?10:CALL_FUNC1_SIZE))
   166 /**
   167  * Exit the block to a relative PC
   168  */
   169 void exit_block_rel( sh4addr_t pc, sh4addr_t endpc )
   170 {
   171     load_imm32( R_ECX, pc - sh4_x86.block_start_pc );   // 5
   172     ADD_sh4r_r32( R_PC, R_ECX );
   173     store_spreg( R_ECX, REG_OFFSET(pc) );               // 3
   174     if( IS_IN_ICACHE(pc) ) {
   175         REXW(); MOV_moff32_EAX( xlat_get_lut_entry(GET_ICACHE_PHYS(pc)) ); // 5
   176         REXW(); AND_imm8s_r32( 0xFC, R_EAX ); // 4
   177     } else if( sh4_x86.tlb_on ) {
   178         call_func1(xlat_get_code_by_vma,R_ECX);
   179     } else {
   180         call_func1(xlat_get_code,R_ECX);
   181     }
   182     load_imm32( R_ECX, ((endpc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
   183     ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );     // 6
   184     exit_block();
   185 }
   187 /**
   188  * Write the block trailer (exception handling block)
   189  */
   190 void sh4_translate_end_block( sh4addr_t pc ) {
   191     if( sh4_x86.branch_taken == FALSE ) {
   192         // Didn't exit unconditionally already, so write the termination here
   193         exit_block_rel( pc, pc );
   194     }
   195     if( sh4_x86.backpatch_posn != 0 ) {
   196         unsigned int i;
   197         // Raise exception
   198         uint8_t *end_ptr = xlat_output;
   199         MOV_r32_r32( R_EDX, R_ECX );
   200         ADD_r32_r32( R_EDX, R_ECX );
   201         ADD_r32_sh4r( R_ECX, R_PC );
   202         MOV_moff32_EAX( &sh4_cpu_period );
   203         MUL_r32( R_EDX );
   204         ADD_r32_sh4r( R_EAX, REG_OFFSET(slice_cycle) );
   206         call_func0( sh4_raise_exception );
   207         load_spreg( R_EAX, R_PC );
   208         if( sh4_x86.tlb_on ) {
   209             call_func1(xlat_get_code_by_vma,R_EAX);
   210         } else {
   211             call_func1(xlat_get_code,R_EAX);
   212         }
   213         exit_block();
   215         // Exception already raised - just cleanup
   216         uint8_t *preexc_ptr = xlat_output;
   217         MOV_r32_r32( R_EDX, R_ECX );
   218         ADD_r32_r32( R_EDX, R_ECX );
   219         ADD_r32_sh4r( R_ECX, R_SPC );
   220         MOV_moff32_EAX( &sh4_cpu_period );
   221         MUL_r32( R_EDX );
   222         ADD_r32_sh4r( R_EAX, REG_OFFSET(slice_cycle) );
   223         load_spreg( R_EDI, R_PC );
   224         if( sh4_x86.tlb_on ) {
   225             call_func0(xlat_get_code_by_vma);
   226         } else {
   227             call_func0(xlat_get_code);
   228         }
   229         exit_block();
   231         for( i=0; i< sh4_x86.backpatch_posn; i++ ) {
   232             uint32_t *fixup_addr = (uint32_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset];
   233             *fixup_addr = xlat_output - (uint8_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset] - 4;
   234             if( sh4_x86.backpatch_list[i].exc_code < 0 ) {
   235                 load_imm32( R_EDX, sh4_x86.backpatch_list[i].fixup_icount );
   236                 int stack_adj = -1 - sh4_x86.backpatch_list[i].exc_code;
   237                 if( stack_adj > 0 ) { 
   238                     REXW(); ADD_imm8s_r32( stack_adj*4, R_ESP );
   239                 }
   240                 int rel = preexc_ptr - xlat_output;
   241                 JMP_rel(rel);
   242             } else {
   243                 load_imm32( R_EDI, sh4_x86.backpatch_list[i].exc_code );
   244                 load_imm32( R_EDX, sh4_x86.backpatch_list[i].fixup_icount );
   245                 int rel = end_ptr - xlat_output;
   246                 JMP_rel(rel);
   247             }
   248         }
   249     }
   250 }
   252 struct UnwindInfo {
   253     uintptr_t block_start;
   254     uintptr_t block_end;
   255     void *pc;
   256 };
   258 _Unwind_Reason_Code xlat_check_frame( struct _Unwind_Context *context, void *arg )
   259 {
   260     struct UnwindInfo *info = arg;
   261     void *pc = (void *)_Unwind_GetIP(context);
   262     if( ((uintptr_t)pc) >= info->block_start && ((uintptr_t)pc) < info->block_end ) {
   263         info->pc = pc;
   264         return _URC_NORMAL_STOP;
   265     }
   267     return _URC_NO_REASON;
   268 }
   270 void *xlat_get_native_pc( void *code, uint32_t code_size )
   271 {
   272     struct _Unwind_Exception exc;
   273     struct UnwindInfo info;
   275     info.pc = NULL;
   276     info.block_start = (uintptr_t)code;
   277     info.block_end = info.block_start + code_size;
   278     void *result = NULL;
   279     _Unwind_Backtrace( xlat_check_frame, &info );
   280     return info.pc;
   281 }
   283 #endif /* !lxdream_ia64abi_H */
.