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