Search
lxdream.org :: lxdream/src/sh4/ia64abi.h
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/ia64abi.h
changeset 561:533f6b478071
prev559:06714bc64271
next569:a1c49e1e8776
author nkeynes
date Tue Jan 01 05:08:38 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Enable Id keyword on all source files
file annotate diff log raw
nkeynes@539
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@539
     3
 * 
nkeynes@539
     4
 * Provides the implementation for the ia32 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@539
    20
#ifndef __lxdream_x86_64abi_H
nkeynes@539
    21
#define __lxdream_x86_64abi_H 1
nkeynes@539
    22
nkeynes@539
    23
nkeynes@539
    24
#define load_ptr( reg, ptr ) load_imm64( reg, (uint64_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
 * Size: 12 bytes
nkeynes@539
    30
 */
nkeynes@539
    31
#define CALL_FUNC0_SIZE 12
nkeynes@539
    32
static inline void call_func0( void *ptr )
nkeynes@539
    33
{
nkeynes@539
    34
    load_imm64(R_EAX, (uint64_t)ptr);
nkeynes@539
    35
    CALL_r32(R_EAX);
nkeynes@539
    36
}
nkeynes@539
    37
nkeynes@539
    38
#define CALL_FUNC1_SIZE 14
nkeynes@539
    39
static inline void call_func1( void *ptr, int arg1 )
nkeynes@539
    40
{
nkeynes@539
    41
    MOV_r32_r32(arg1, R_EDI);
nkeynes@539
    42
    call_func0(ptr);
nkeynes@539
    43
}
nkeynes@539
    44
nkeynes@539
    45
#define CALL_FUNC2_SIZE 16
nkeynes@539
    46
static inline void call_func2( void *ptr, int arg1, int arg2 )
nkeynes@539
    47
{
nkeynes@539
    48
    MOV_r32_r32(arg1, R_EDI);
nkeynes@539
    49
    MOV_r32_r32(arg2, R_ESI);
nkeynes@539
    50
    call_func0(ptr);
nkeynes@539
    51
}
nkeynes@539
    52
nkeynes@539
    53
#define MEM_WRITE_DOUBLE_SIZE 39
nkeynes@539
    54
/**
nkeynes@539
    55
 * Write a double (64-bit) value into memory, with the first word in arg2a, and
nkeynes@539
    56
 * the second in arg2b
nkeynes@539
    57
 */
nkeynes@539
    58
static inline void MEM_WRITE_DOUBLE( int addr, int arg2a, int arg2b )
nkeynes@539
    59
{
nkeynes@539
    60
    PUSH_r32(arg2b);
nkeynes@539
    61
    PUSH_r32(addr);
nkeynes@539
    62
    call_func2(sh4_write_long, addr, arg2a);
nkeynes@539
    63
    POP_r32(addr);
nkeynes@539
    64
    POP_r32(arg2b);
nkeynes@539
    65
    ADD_imm8s_r32(4, addr);
nkeynes@539
    66
    call_func2(sh4_write_long, addr, arg2b);
nkeynes@539
    67
}
nkeynes@539
    68
nkeynes@547
    69
#define MEM_READ_DOUBLE_SIZE 43
nkeynes@539
    70
/**
nkeynes@539
    71
 * Read a double (64-bit) value from memory, writing the first word into arg2a
nkeynes@539
    72
 * and the second into arg2b. The addr must not be in EAX
nkeynes@539
    73
 */
nkeynes@539
    74
static inline void MEM_READ_DOUBLE( int addr, int arg2a, int arg2b )
nkeynes@539
    75
{
nkeynes@547
    76
    REXW(); SUB_imm8s_r32( 8, R_ESP );
nkeynes@539
    77
    PUSH_r32(addr);
nkeynes@539
    78
    call_func1(sh4_read_long, addr);
nkeynes@539
    79
    POP_r32(R_EDI);
nkeynes@539
    80
    PUSH_r32(R_EAX);
nkeynes@539
    81
    ADD_imm8s_r32(4, R_EDI);
nkeynes@539
    82
    call_func0(sh4_read_long);
nkeynes@539
    83
    MOV_r32_r32(R_EAX, arg2b);
nkeynes@539
    84
    POP_r32(arg2a);
nkeynes@547
    85
    REXW(); ADD_imm8s_r32( 8, R_ESP );
nkeynes@539
    86
}
nkeynes@539
    87
nkeynes@539
    88
nkeynes@539
    89
/**
nkeynes@539
    90
 * Emit the 'start of block' assembly. Sets up the stack frame and save
nkeynes@539
    91
 * SI/DI as required
nkeynes@539
    92
 */
nkeynes@539
    93
void sh4_translate_begin_block( sh4addr_t pc ) 
nkeynes@539
    94
{
nkeynes@539
    95
    PUSH_r32(R_EBP);
nkeynes@539
    96
    /* mov &sh4r, ebp */
nkeynes@539
    97
    load_ptr( R_EBP, &sh4r );
nkeynes@539
    98
    
nkeynes@539
    99
    sh4_x86.in_delay_slot = FALSE;
nkeynes@539
   100
    sh4_x86.priv_checked = FALSE;
nkeynes@539
   101
    sh4_x86.fpuen_checked = FALSE;
nkeynes@539
   102
    sh4_x86.branch_taken = FALSE;
nkeynes@539
   103
    sh4_x86.backpatch_posn = 0;
nkeynes@539
   104
    sh4_x86.block_start_pc = pc;
nkeynes@539
   105
    sh4_x86.tstate = TSTATE_NONE;
nkeynes@539
   106
}
nkeynes@539
   107
nkeynes@539
   108
/**
nkeynes@539
   109
 * Exit the block with sh4r.pc already written
nkeynes@539
   110
 * Bytes: 15
nkeynes@539
   111
 */
nkeynes@539
   112
void exit_block_pcset( pc )
nkeynes@539
   113
{
nkeynes@539
   114
    load_imm32( R_ECX, ((pc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
nkeynes@539
   115
    ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );    // 6
nkeynes@539
   116
    load_spreg( R_EAX, REG_OFFSET(pc) );
nkeynes@539
   117
    call_func1(xlat_get_code,R_EAX);
nkeynes@539
   118
    POP_r32(R_EBP);
nkeynes@539
   119
    RET();
nkeynes@539
   120
}
nkeynes@539
   121
nkeynes@539
   122
#define EXIT_BLOCK_SIZE 35
nkeynes@539
   123
/**
nkeynes@539
   124
 * Exit the block to an absolute PC
nkeynes@539
   125
 */
nkeynes@539
   126
void exit_block( sh4addr_t pc, sh4addr_t endpc )
nkeynes@539
   127
{
nkeynes@539
   128
    load_imm32( R_ECX, pc );                            // 5
nkeynes@539
   129
    store_spreg( R_ECX, REG_OFFSET(pc) );               // 3
nkeynes@539
   130
    REXW(); MOV_moff32_EAX( xlat_get_lut_entry(pc) );
nkeynes@539
   131
    REXW(); AND_imm8s_r32( 0xFC, R_EAX ); // 3
nkeynes@539
   132
    load_imm32( R_ECX, ((endpc - sh4_x86.block_start_pc)>>1)*sh4_cpu_period ); // 5
nkeynes@539
   133
    ADD_r32_sh4r( R_ECX, REG_OFFSET(slice_cycle) );     // 6
nkeynes@539
   134
    POP_r32(R_EBP);
nkeynes@539
   135
    RET();
nkeynes@539
   136
}
nkeynes@539
   137
nkeynes@539
   138
/**
nkeynes@539
   139
 * Write the block trailer (exception handling block)
nkeynes@539
   140
 */
nkeynes@539
   141
void sh4_translate_end_block( sh4addr_t pc ) {
nkeynes@539
   142
    if( sh4_x86.branch_taken == FALSE ) {
nkeynes@539
   143
	// Didn't exit unconditionally already, so write the termination here
nkeynes@539
   144
	exit_block( pc, pc );
nkeynes@539
   145
    }
nkeynes@539
   146
    if( sh4_x86.backpatch_posn != 0 ) {
nkeynes@559
   147
	unsigned int i;
nkeynes@559
   148
	// Raise exception
nkeynes@539
   149
	uint8_t *end_ptr = xlat_output;
nkeynes@539
   150
	load_spreg( R_ECX, REG_OFFSET(pc) );
nkeynes@539
   151
	ADD_r32_r32( R_EDX, R_ECX );
nkeynes@539
   152
	ADD_r32_r32( R_EDX, R_ECX );
nkeynes@539
   153
	store_spreg( R_ECX, REG_OFFSET(pc) );
nkeynes@539
   154
	MOV_moff32_EAX( &sh4_cpu_period );
nkeynes@539
   155
	MUL_r32( R_EDX );
nkeynes@539
   156
	ADD_r32_sh4r( R_EAX, REG_OFFSET(slice_cycle) );
nkeynes@539
   157
nkeynes@539
   158
	call_func0( sh4_raise_exception );
nkeynes@539
   159
	load_spreg( R_EAX, REG_OFFSET(pc) );
nkeynes@539
   160
	call_func1(xlat_get_code,R_EAX);
nkeynes@539
   161
	POP_r32(R_EBP);
nkeynes@539
   162
	RET();
nkeynes@539
   163
nkeynes@559
   164
	// Exception already raised - just cleanup
nkeynes@559
   165
	uint8_t *preexc_ptr = xlat_output;
nkeynes@559
   166
	load_imm32( R_ECX, sh4_x86.block_start_pc );
nkeynes@559
   167
	ADD_r32_r32( R_EDX, R_ECX );
nkeynes@559
   168
	ADD_r32_r32( R_EDX, R_ECX );
nkeynes@559
   169
	store_spreg( R_ECX, REG_OFFSET(spc) );
nkeynes@559
   170
	MOV_moff32_EAX( &sh4_cpu_period );
nkeynes@559
   171
	MUL_r32( R_EDX );
nkeynes@559
   172
	ADD_r32_sh4r( R_EAX, REG_OFFSET(slice_cycle) );
nkeynes@559
   173
	load_spreg( R_EAX, REG_OFFSET(pc) );
nkeynes@559
   174
	call_func1(xlat_get_code,R_EAX);
nkeynes@559
   175
	POP_r32(R_EBP);
nkeynes@559
   176
	RET();
nkeynes@559
   177
nkeynes@559
   178
	for( i=0; i< sh4_x86.backpatch_posn; i++ ) {
nkeynes@559
   179
	    *sh4_x86.backpatch_list[i].fixup_addr =
nkeynes@559
   180
		xlat_output - ((uint8_t *)sh4_x86.backpatch_list[i].fixup_addr) - 4;
nkeynes@559
   181
	    if( sh4_x86.backpatch_list[i].exc_code == -1 ) {
nkeynes@559
   182
		load_imm32( R_EDX, sh4_x86.backpatch_list[i].fixup_icount );
nkeynes@559
   183
		int rel = preexc_ptr - xlat_output;
nkeynes@559
   184
		JMP_rel(rel);
nkeynes@559
   185
	    } else {
nkeynes@559
   186
		load_imm32( R_EDI, sh4_x86.backpatch_list[i].exc_code );
nkeynes@559
   187
		load_imm32( R_EDX, sh4_x86.backpatch_list[i].fixup_icount );
nkeynes@559
   188
		int rel = end_ptr - xlat_output;
nkeynes@559
   189
		JMP_rel(rel);
nkeynes@559
   190
	    }
nkeynes@559
   191
	}
nkeynes@539
   192
    }
nkeynes@539
   193
}
nkeynes@539
   194
nkeynes@547
   195
#endif
.