Search
lxdream.org :: lxdream/src/sh4/ia32mac.h
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/ia32mac.h
changeset 736:a02d1475ccfd
prev669:ab344e42bca9
next901:32c5cf5e206f
author nkeynes
date Mon Jul 14 07:44:42 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Re-indent everything consistently
Fix include guards for consistency as well
file annotate diff log raw
nkeynes@539
     1
/**
nkeynes@586
     2
 * $Id$
nkeynes@539
     3
 * 
nkeynes@736
     4
 * Provides the implementation for the ia32 Mac OS X ABI variant 
nkeynes@736
     5
 * (eg prologue, epilogue, and calling conventions). Main difference
nkeynes@736
     6
 * from ia32abi is that stack frames are aligned on 16-byte boundaries.
nkeynes@539
     7
 *
nkeynes@539
     8
 * Copyright (c) 2007 Nathan Keynes.
nkeynes@539
     9
 *
nkeynes@539
    10
 * This program is free software; you can redistribute it and/or modify
nkeynes@539
    11
 * it under the terms of the GNU General Public License as published by
nkeynes@539
    12
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@539
    13
 * (at your option) any later version.
nkeynes@539
    14
 *
nkeynes@539
    15
 * This program is distributed in the hope that it will be useful,
nkeynes@539
    16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@539
    17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@539
    18
 * GNU General Public License for more details.
nkeynes@539
    19
 */
nkeynes@539
    20
nkeynes@736
    21
#ifndef lxdream_ia32mac_H
nkeynes@736
    22
#define lxdream_ia32mac_H 1
nkeynes@539
    23
nkeynes@539
    24
#define load_ptr( reg, ptr ) load_imm32( reg, (uint32_t)ptr );
nkeynes@539
    25
nkeynes@539
    26
/**
nkeynes@539
    27
 * Note: clobbers EAX to make the indirect call - this isn't usually
nkeynes@539
    28
 * a problem since the callee will usually clobber it anyway.
nkeynes@539
    29
 */
nkeynes@539
    30
#define CALL_FUNC0_SIZE 13
nkeynes@539
    31
static inline void call_func0( void *ptr )
nkeynes@539
    32
{
nkeynes@539
    33
    int adj = (-sh4_x86.stack_posn)&0x0F;
nkeynes@539
    34
    SUB_imm8s_r32( adj, R_ESP );
nkeynes@539
    35
    load_imm32(R_EAX, (uint32_t)ptr);
nkeynes@539
    36
    CALL_r32(R_EAX);
nkeynes@539
    37
    ADD_imm8s_r32( adj, R_ESP );
nkeynes@539
    38
}
nkeynes@539
    39
nkeynes@539
    40
#define CALL_FUNC1_SIZE 14
nkeynes@539
    41
static inline void call_func1( void *ptr, int arg1 )
nkeynes@539
    42
{
nkeynes@539
    43
    int adj = (-4-sh4_x86.stack_posn)&0x0F;
nkeynes@539
    44
    SUB_imm8s_r32( adj, R_ESP );
nkeynes@539
    45
    PUSH_r32(arg1);
nkeynes@539
    46
    load_imm32(R_EAX, (uint32_t)ptr);
nkeynes@539
    47
    CALL_r32(R_EAX);
nkeynes@539
    48
    ADD_imm8s_r32( adj+4, R_ESP );
nkeynes@539
    49
    sh4_x86.stack_posn -= 4;
nkeynes@539
    50
}
nkeynes@539
    51
nkeynes@539
    52
#define CALL_FUNC2_SIZE 15
nkeynes@539
    53
static inline void call_func2( void *ptr, int arg1, int arg2 )
nkeynes@539
    54
{
nkeynes@539
    55
    int adj = (-8-sh4_x86.stack_posn)&0x0F;
nkeynes@539
    56
    SUB_imm8s_r32( adj, R_ESP );
nkeynes@539
    57
    PUSH_r32(arg2);
nkeynes@539
    58
    PUSH_r32(arg1);
nkeynes@539
    59
    load_imm32(R_EAX, (uint32_t)ptr);
nkeynes@539
    60
    CALL_r32(R_EAX);
nkeynes@539
    61
    ADD_imm8s_r32( adj+8, R_ESP );
nkeynes@539
    62
    sh4_x86.stack_posn -= 8;
nkeynes@539
    63
}
nkeynes@539
    64
nkeynes@539
    65
/**
nkeynes@539
    66
 * Write a double (64-bit) value into memory, with the first word in arg2a, and
nkeynes@539
    67
 * the second in arg2b
nkeynes@539
    68
 * NB: 30 bytes
nkeynes@539
    69
 */
nkeynes@539
    70
#define MEM_WRITE_DOUBLE_SIZE 36
nkeynes@539
    71
static inline void MEM_WRITE_DOUBLE( int addr, int arg2a, int arg2b )
nkeynes@539
    72
{
nkeynes@539
    73
    int adj = (-8-sh4_x86.stack_posn)&0x0F;
nkeynes@539
    74
    SUB_imm8s_r32( adj, R_ESP );
nkeynes@539
    75
    ADD_imm8s_r32( 4, addr );
nkeynes@539
    76
    PUSH_r32(arg2b);
nkeynes@539
    77
    PUSH_r32(addr);
nkeynes@539
    78
    ADD_imm8s_r32( -4, addr );
nkeynes@539
    79
    SUB_imm8s_r32( 8, R_ESP );
nkeynes@539
    80
    PUSH_r32(arg2a);
nkeynes@539
    81
    PUSH_r32(addr);
nkeynes@539
    82
    load_imm32(R_EAX, (uint32_t)sh4_write_long);
nkeynes@539
    83
    CALL_r32(R_EAX);
nkeynes@539
    84
    ADD_imm8s_r32( 16, R_ESP );
nkeynes@539
    85
    load_imm32(R_EAX, (uint32_t)sh4_write_long);
nkeynes@539
    86
    CALL_r32(R_EAX);
nkeynes@539
    87
    ADD_imm8s_r32( adj+8, R_ESP );
nkeynes@539
    88
    sh4_x86.stack_posn -= 16;
nkeynes@539
    89
}
nkeynes@539
    90
nkeynes@539
    91
/**
nkeynes@539
    92
 * Read a double (64-bit) value from memory, writing the first word into arg2a
nkeynes@539
    93
 * and the second into arg2b. The addr must not be in EAX
nkeynes@539
    94
 * NB: 27 bytes
nkeynes@539
    95
 */
nkeynes@539
    96
#define MEM_READ_DOUBLE_SIZE 36
nkeynes@539
    97
static inline void MEM_READ_DOUBLE( int addr, int arg2a, int arg2b )
nkeynes@539
    98
{
nkeynes@539
    99
    int adj = (-4-sh4_x86.stack_posn)&0x0F;
nkeynes@539
   100
    int adj2 = (-8-sh4_x86.stack_posn)&0x0F;
nkeynes@539
   101
    SUB_imm8s_r32( adj, R_ESP );
nkeynes@539
   102
    PUSH_r32(addr);
nkeynes@539
   103
    load_imm32(R_EAX, (uint32_t)sh4_read_long);
nkeynes@539
   104
    CALL_r32(R_EAX);
nkeynes@586
   105
    POP_r32(R_ECX);
nkeynes@539
   106
    SUB_imm8s_r32( adj2-adj, R_ESP );
nkeynes@539
   107
    PUSH_r32(R_EAX);
nkeynes@586
   108
    ADD_imm8s_r32( 4, R_ECX );
nkeynes@586
   109
    PUSH_r32(R_ECX);
nkeynes@539
   110
    load_imm32(R_EAX, (uint32_t)sh4_read_long);
nkeynes@539
   111
    CALL_r32(R_EAX);
nkeynes@539
   112
    ADD_imm8s_r32( 4, R_ESP );
nkeynes@539
   113
    MOV_r32_r32( R_EAX, arg2b );
nkeynes@539
   114
    POP_r32(arg2a);
nkeynes@539
   115
    ADD_imm8s_r32( adj2, R_ESP );
nkeynes@539
   116
    sh4_x86.stack_posn -= 4;
nkeynes@539
   117
}
nkeynes@539
   118
nkeynes@539
   119
/**
nkeynes@539
   120
 * Emit the 'start of block' assembly. Sets up the stack frame and save
nkeynes@539
   121
 * SI/DI as required
nkeynes@539
   122
 */
nkeynes@539
   123
void sh4_translate_begin_block( sh4addr_t pc ) 
nkeynes@539
   124
{
nkeynes@539
   125
    PUSH_r32(R_EBP);
nkeynes@539
   126
    /* mov &sh4r, ebp */
nkeynes@669
   127
    load_ptr( R_EBP, ((uint8_t *)&sh4r) + 128 );
nkeynes@736
   128
nkeynes@539
   129
    sh4_x86.in_delay_slot = FALSE;
nkeynes@539
   130
    sh4_x86.priv_checked = FALSE;
nkeynes@539
   131
    sh4_x86.fpuen_checked = FALSE;
nkeynes@539
   132
    sh4_x86.branch_taken = FALSE;
nkeynes@539
   133
    sh4_x86.backpatch_posn = 0;
nkeynes@539
   134
    sh4_x86.block_start_pc = pc;
nkeynes@539
   135
    sh4_x86.tstate = TSTATE_NONE;
nkeynes@586
   136
    sh4_x86.tlb_on = IS_MMU_ENABLED();
nkeynes@539
   137
    sh4_x86.stack_posn = 8;
nkeynes@539
   138
}
nkeynes@539
   139
nkeynes@539
   140
/**
nkeynes@590
   141
 * Exit the block with sh4r.new_pc written with the target pc
nkeynes@539
   142
 */
nkeynes@586
   143
void exit_block_pcset( sh4addr_t pc )
nkeynes@539
   144
{
nkeynes@539
   145
    load_imm32( R_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
nkeynes@539
   146
    ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );    // 6
nkeynes@590
   147
    load_spreg( R_EAX, R_PC );
nkeynes@586
   148
    if( sh4_x86.tlb_on ) {
nkeynes@736
   149
        call_func1(xlat_get_code_by_vma,R_EAX);
nkeynes@586
   150
    } else {
nkeynes@736
   151
        call_func1(xlat_get_code,R_EAX);
nkeynes@586
   152
    }
nkeynes@539
   153
    POP_r32(R_EBP);
nkeynes@539
   154
    RET();
nkeynes@539
   155
}
nkeynes@539
   156
nkeynes@590
   157
/**
nkeynes@590
   158
 * Exit the block with sh4r.new_pc written with the target pc
nkeynes@590
   159
 */
nkeynes@590
   160
void exit_block_newpcset( sh4addr_t pc )
nkeynes@590
   161
{
nkeynes@590
   162
    load_imm32( R_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
nkeynes@590
   163
    ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );    // 6
nkeynes@590
   164
    load_spreg( R_EAX, R_NEW_PC );
nkeynes@590
   165
    store_spreg( R_EAX, R_PC );
nkeynes@590
   166
    if( sh4_x86.tlb_on ) {
nkeynes@736
   167
        call_func1(xlat_get_code_by_vma,R_EAX);
nkeynes@590
   168
    } else {
nkeynes@736
   169
        call_func1(xlat_get_code,R_EAX);
nkeynes@590
   170
    }
nkeynes@590
   171
    POP_r32(R_EBP);
nkeynes@590
   172
    RET();
nkeynes@590
   173
}
nkeynes@590
   174
nkeynes@590
   175
nkeynes@586
   176
#define EXIT_BLOCK_SIZE(pc)  (24 + (IS_IN_ICACHE(pc)?5:CALL_FUNC1_SIZE))
nkeynes@586
   177
nkeynes@586
   178
nkeynes@539
   179
/**
nkeynes@539
   180
 * Exit the block to an absolute PC
nkeynes@539
   181
 */
nkeynes@539
   182
void exit_block( sh4addr_t pc, sh4addr_t endpc )
nkeynes@539
   183
{
nkeynes@539
   184
    load_imm32( R_ECX, pc );                            // 5
nkeynes@539
   185
    store_spreg( R_ECX, REG_OFFSET(pc) );               // 3
nkeynes@586
   186
    if( IS_IN_ICACHE(pc) ) {
nkeynes@736
   187
        MOV_moff32_EAX( xlat_get_lut_entry(GET_ICACHE_PHYS(pc)) ); // 5
nkeynes@586
   188
    } else if( sh4_x86.tlb_on ) {
nkeynes@736
   189
        call_func1(xlat_get_code_by_vma,R_ECX);
nkeynes@586
   190
    } else {
nkeynes@736
   191
        call_func1(xlat_get_code,R_ECX);
nkeynes@586
   192
    }
nkeynes@586
   193
    AND_imm8s_r32( 0xFC, R_EAX ); // 3
nkeynes@586
   194
    load_imm32( R_ECX, ((endpc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
nkeynes@586
   195
    ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );     // 6
nkeynes@586
   196
    POP_r32(R_EBP);
nkeynes@586
   197
    RET();
nkeynes@586
   198
}
nkeynes@586
   199
nkeynes@586
   200
#define EXIT_BLOCK_REL_SIZE(pc)  (27 + (IS_IN_ICACHE(pc)?5:CALL_FUNC1_SIZE))
nkeynes@586
   201
nkeynes@586
   202
/**
nkeynes@586
   203
 * Exit the block to a relative PC
nkeynes@586
   204
 */
nkeynes@586
   205
void exit_block_rel( sh4addr_t pc, sh4addr_t endpc )
nkeynes@586
   206
{
nkeynes@586
   207
    load_imm32( R_ECX, pc - sh4_x86.block_start_pc );   // 5
nkeynes@586
   208
    ADD_sh4r_r32( R_PC, R_ECX );
nkeynes@586
   209
    store_spreg( R_ECX, REG_OFFSET(pc) );               // 3
nkeynes@586
   210
    if( IS_IN_ICACHE(pc) ) {
nkeynes@736
   211
        MOV_moff32_EAX( xlat_get_lut_entry(GET_ICACHE_PHYS(pc)) ); // 5
nkeynes@586
   212
    } else if( sh4_x86.tlb_on ) {
nkeynes@736
   213
        call_func1(xlat_get_code_by_vma,R_ECX);
nkeynes@586
   214
    } else {
nkeynes@736
   215
        call_func1(xlat_get_code,R_ECX);
nkeynes@586
   216
    }
nkeynes@539
   217
    AND_imm8s_r32( 0xFC, R_EAX ); // 3
nkeynes@539
   218
    load_imm32( R_ECX, ((endpc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
nkeynes@539
   219
    ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );     // 6
nkeynes@539
   220
    POP_r32(R_EBP);
nkeynes@539
   221
    RET();
nkeynes@539
   222
}
nkeynes@539
   223
nkeynes@539
   224
/**
nkeynes@539
   225
 * Write the block trailer (exception handling block)
nkeynes@539
   226
 */
nkeynes@539
   227
void sh4_translate_end_block( sh4addr_t pc ) {
nkeynes@539
   228
    if( sh4_x86.branch_taken == FALSE ) {
nkeynes@736
   229
        // Didn't exit unconditionally already, so write the termination here
nkeynes@736
   230
        exit_block_rel( pc, pc );
nkeynes@539
   231
    }
nkeynes@539
   232
    if( sh4_x86.backpatch_posn != 0 ) {
nkeynes@736
   233
        unsigned int i;
nkeynes@736
   234
        // Raise exception
nkeynes@736
   235
        uint8_t *end_ptr = xlat_output;
nkeynes@736
   236
        MOV_r32_r32( R_EDX, R_ECX );
nkeynes@736
   237
        ADD_r32_r32( R_EDX, R_ECX );
nkeynes@736
   238
        ADD_r32_sh4r( R_ECX, R_PC );
nkeynes@736
   239
        MOV_moff32_EAX( &sh4_cpu_period );
nkeynes@736
   240
        MUL_r32( R_EDX );
nkeynes@736
   241
        ADD_r32_sh4r( R_EAX, REG_OFFSET(slice_cycle) );
nkeynes@539
   242
nkeynes@586
   243
        POP_r32(R_EDX);
nkeynes@586
   244
        call_func1( sh4_raise_exception, R_EDX );
nkeynes@736
   245
        load_spreg( R_EAX, R_PC );
nkeynes@736
   246
        if( sh4_x86.tlb_on ) {
nkeynes@736
   247
            call_func1(xlat_get_code_by_vma,R_EAX);
nkeynes@736
   248
        } else {
nkeynes@736
   249
            call_func1(xlat_get_code,R_EAX);
nkeynes@736
   250
        }
nkeynes@736
   251
        POP_r32(R_EBP);
nkeynes@736
   252
        RET();
nkeynes@539
   253
nkeynes@736
   254
        // Exception already raised - just cleanup
nkeynes@736
   255
        uint8_t *preexc_ptr = xlat_output;
nkeynes@736
   256
        MOV_r32_r32( R_EDX, R_ECX );
nkeynes@736
   257
        ADD_r32_r32( R_EDX, R_ECX );
nkeynes@736
   258
        ADD_r32_sh4r( R_ECX, R_SPC );
nkeynes@736
   259
        MOV_moff32_EAX( &sh4_cpu_period );
nkeynes@736
   260
        MUL_r32( R_EDX );
nkeynes@736
   261
        ADD_r32_sh4r( R_EAX, REG_OFFSET(slice_cycle) );
nkeynes@736
   262
        load_spreg( R_EAX, R_PC );
nkeynes@736
   263
        if( sh4_x86.tlb_on ) {
nkeynes@736
   264
            call_func1(xlat_get_code_by_vma,R_EAX);
nkeynes@736
   265
        } else {
nkeynes@736
   266
            call_func1(xlat_get_code,R_EAX);
nkeynes@736
   267
        }
nkeynes@736
   268
        POP_r32(R_EBP);
nkeynes@736
   269
        RET();
nkeynes@586
   270
nkeynes@736
   271
        for( i=0; i< sh4_x86.backpatch_posn; i++ ) {
nkeynes@736
   272
            uint32_t *fixup_addr = (uint32_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset];
nkeynes@736
   273
            *fixup_addr = xlat_output - (uint8_t *)&xlat_current_block->code[sh4_x86.backpatch_list[i].fixup_offset] - 4;
nkeynes@736
   274
            if( sh4_x86.backpatch_list[i].exc_code < 0 ) {
nkeynes@736
   275
                load_imm32( R_EDX, sh4_x86.backpatch_list[i].fixup_icount );
nkeynes@736
   276
                int stack_adj = -1 - sh4_x86.backpatch_list[i].exc_code;
nkeynes@736
   277
                if( stack_adj > 0 ) { 
nkeynes@736
   278
                    ADD_imm8s_r32( stack_adj, R_ESP );
nkeynes@736
   279
                }
nkeynes@736
   280
                int rel = preexc_ptr - xlat_output;
nkeynes@736
   281
                JMP_rel(rel);
nkeynes@736
   282
            } else {
nkeynes@736
   283
                PUSH_imm32( sh4_x86.backpatch_list[i].exc_code );
nkeynes@736
   284
                load_imm32( R_EDX, sh4_x86.backpatch_list[i].fixup_icount );
nkeynes@736
   285
                int rel = end_ptr - xlat_output;
nkeynes@736
   286
                JMP_rel(rel);
nkeynes@736
   287
            }
nkeynes@736
   288
        }
nkeynes@539
   289
    }
nkeynes@539
   290
}
nkeynes@539
   291
nkeynes@586
   292
void *xlat_get_native_pc()
nkeynes@586
   293
{
nkeynes@586
   294
    void *result = NULL;
nkeynes@586
   295
    asm(
nkeynes@736
   296
        "mov %%ebp, %%eax\n\t"
nkeynes@736
   297
        "mov $0x8, %%ecx\n\t"
nkeynes@736
   298
        "mov %1, %%edx\n"
nkeynes@736
   299
        "frame_loop: test %%eax, %%eax\n\t"
nkeynes@736
   300
        "je frame_not_found\n\t"
nkeynes@736
   301
        "cmp (%%eax), %%edx\n\t"
nkeynes@736
   302
        "je frame_found\n\t"
nkeynes@736
   303
        "sub $0x1, %%ecx\n\t"
nkeynes@736
   304
        "je frame_not_found\n\t"
nkeynes@736
   305
        "movl (%%eax), %%eax\n\t"
nkeynes@736
   306
        "jmp frame_loop\n"
nkeynes@736
   307
        "frame_found: movl 0x4(%%eax), %0\n"
nkeynes@736
   308
        "frame_not_found:"
nkeynes@736
   309
        : "=r" (result)
nkeynes@736
   310
        : "r" (((uint8_t *)&sh4r) + 128 )
nkeynes@736
   311
        : "eax", "ecx", "edx" );
nkeynes@586
   312
    return result;
nkeynes@586
   313
}
nkeynes@586
   314
nkeynes@586
   315
nkeynes@736
   316
#endif /* !lxdream_ia32mac.h */
nkeynes@539
   317
nkeynes@539
   318
.