Search
lxdream.org :: lxdream/src/sh4/x86op.h
lxdream 0.9.1
released Jun 29
Download Now
filename src/sh4/x86op.h
changeset 374:8f80a795513e
prev368:36fac4c42322
next375:4627600f7f8e
author nkeynes
date Tue Sep 11 02:14:46 2007 +0000 (13 years ago)
permissions -rw-r--r--
last change Cache the pointer to the last FR bank (speeds fp ops up by about 10%)
Implement experimental fix for FLOAT/FTRC
Make read/write sr functions non-static (share with translator)
Much more translator WIP
file annotate diff log raw
nkeynes@359
     1
/**
nkeynes@374
     2
 * $Id: x86op.h,v 1.4 2007-09-11 02:14:46 nkeynes Exp $
nkeynes@359
     3
 * 
nkeynes@359
     4
 * Definitions of x86 opcodes for use by the translator.
nkeynes@359
     5
 *
nkeynes@359
     6
 * Copyright (c) 2007 Nathan Keynes.
nkeynes@359
     7
 *
nkeynes@359
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@359
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@359
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@359
    11
 * (at your option) any later version.
nkeynes@359
    12
 *
nkeynes@359
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@359
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@359
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@359
    16
 * GNU General Public License for more details.
nkeynes@359
    17
 */
nkeynes@359
    18
nkeynes@359
    19
#ifndef __lxdream_x86op_H
nkeynes@359
    20
#define __lxdream_x86op_H
nkeynes@359
    21
nkeynes@359
    22
#define R_NONE -1
nkeynes@359
    23
#define R_EAX 0
nkeynes@359
    24
#define R_ECX 1
nkeynes@359
    25
#define R_EDX 2
nkeynes@359
    26
#define R_EBX 3
nkeynes@359
    27
#define R_ESP 4
nkeynes@359
    28
#define R_EBP 5
nkeynes@359
    29
#define R_ESI 6 
nkeynes@359
    30
#define R_EDI 7 
nkeynes@359
    31
nkeynes@359
    32
#define R_AL 0
nkeynes@359
    33
#define R_CL 1
nkeynes@359
    34
#define R_DL 2
nkeynes@359
    35
#define R_BL 3
nkeynes@359
    36
#define R_AH 4
nkeynes@359
    37
#define R_CH 5
nkeynes@359
    38
#define R_DH 6
nkeynes@359
    39
#define R_BH 7
nkeynes@359
    40
nkeynes@359
    41
nkeynes@361
    42
#define OP(x) *xlat_output++ = (x)
nkeynes@361
    43
#define OP32(x) *((uint32_t *)xlat_output) = (x); xlat_output+=4
nkeynes@359
    44
nkeynes@359
    45
/* Offset of a reg relative to the sh4r structure */
nkeynes@359
    46
#define REG_OFFSET(reg)  (((char *)&sh4r.reg) - ((char *)&sh4r))
nkeynes@359
    47
nkeynes@359
    48
#define R_T   REG_OFFSET(t)
nkeynes@361
    49
#define R_Q   REG_OFFSET(q)
nkeynes@361
    50
#define R_S   REG_OFFSET(s)
nkeynes@361
    51
#define R_M   REG_OFFSET(m)
nkeynes@368
    52
#define R_SR  REG_OFFSET(sr)
nkeynes@359
    53
#define R_GBR REG_OFFSET(gbr)
nkeynes@359
    54
#define R_SSR REG_OFFSET(ssr)
nkeynes@359
    55
#define R_SPC REG_OFFSET(spc)
nkeynes@359
    56
#define R_VBR REG_OFFSET(vbr)
nkeynes@359
    57
#define R_MACH REG_OFFSET(mac)+4
nkeynes@359
    58
#define R_MACL REG_OFFSET(mac)
nkeynes@359
    59
#define R_PR REG_OFFSET(pr)
nkeynes@359
    60
#define R_SGR REG_OFFSET(sgr)
nkeynes@359
    61
#define R_FPUL REG_OFFSET(fpul)
nkeynes@359
    62
#define R_FPSCR REG_OFFSET(fpscr)
nkeynes@359
    63
#define R_DBR REG_OFFSET(dbr)
nkeynes@359
    64
nkeynes@359
    65
/**************** Basic X86 operations *********************/
nkeynes@359
    66
/* Note: operands follow SH4 convention (source, dest) rather than x86 
nkeynes@359
    67
 * conventions (dest, source)
nkeynes@359
    68
 */
nkeynes@359
    69
nkeynes@359
    70
/* Two-reg modrm form - first arg is the r32 reg, second arg is the r/m32 reg */
nkeynes@359
    71
#define MODRM_r32_rm32(r1,r2) OP(0xC0 | (r1<<3) | r2)
nkeynes@359
    72
#define MODRM_rm32_r32(r1,r2) OP(0xC0 | (r2<<3) | r1)
nkeynes@359
    73
nkeynes@359
    74
/* ebp+disp8 modrm form */
nkeynes@359
    75
#define MODRM_r32_ebp8(r1,disp) OP(0x45 | (r1<<3)); OP(disp)
nkeynes@359
    76
nkeynes@359
    77
/* ebp+disp32 modrm form */
nkeynes@359
    78
#define MODRM_r32_ebp32(r1,disp) OP(0x85 | (r1<<3)); OP32(disp)
nkeynes@359
    79
nkeynes@374
    80
#define MODRM_r32_sh4r(r1,disp) if(disp>127){ MODRM_r32_ebp32(r1,disp);}else{ MODRM_r32_ebp8(r1,(unsigned char)disp); }
nkeynes@368
    81
nkeynes@359
    82
/* Major opcodes */
nkeynes@374
    83
#define ADD_sh4r_r32(disp,r1) OP(0x03); MODRM_r32_sh4r(r1,disp)
nkeynes@359
    84
#define ADD_r32_r32(r1,r2) OP(0x03); MODRM_rm32_r32(r1,r2)
nkeynes@359
    85
#define ADD_imm8s_r32(imm,r1) OP(0x83); MODRM_rm32_r32(r1, 0); OP(imm)
nkeynes@368
    86
#define ADD_imm32_r32(imm32,r1) OP(0x81); MODRM_rm32_r32(r1,0); OP32(imm32)
nkeynes@359
    87
#define ADC_r32_r32(r1,r2)    OP(0x13); MODRM_rm32_r32(r1,r2)
nkeynes@359
    88
#define AND_r32_r32(r1,r2)    OP(0x23); MODRM_rm32_r32(r1,r2)
nkeynes@368
    89
#define AND_imm8_r8(imm8, r1) OP(0x80); MODRM_rm32_r32(r1,4); OP(imm8)
nkeynes@374
    90
#define AND_imm8s_r32(imm8,r1) OP(0x83); MODRM_rm32_r32(r1,4); OP(imm8)
nkeynes@359
    91
#define AND_imm32_r32(imm,r1) OP(0x81); MODRM_rm32_r32(r1,4); OP32(imm)
nkeynes@368
    92
#define CALL_r32(r1)          OP(0xFF); MODRM_rm32_r32(r1,2)
nkeynes@374
    93
#define CLC()                 OP(0xF8)
nkeynes@359
    94
#define CMC()                 OP(0xF5)
nkeynes@374
    95
#define CMP_sh4r_r32(disp,r1)  OP(0x3B); MODRM_r32_sh4r(r1,disp)
nkeynes@359
    96
#define CMP_r32_r32(r1,r2)    OP(0x3B); MODRM_rm32_r32(r1,r2)
nkeynes@368
    97
#define CMP_imm32_r32(imm32, r1) OP(0x81); MODRM_rm32_r32(r1,7); OP32(imm32)
nkeynes@359
    98
#define CMP_imm8s_r32(imm,r1) OP(0x83); MODRM_rm32_r32(r1,7); OP(imm)
nkeynes@374
    99
#define CMP_imm8s_sh4r(imm,disp) OP(0x83); MODRM_r32_sh4r(7,disp) OP(imm)
nkeynes@368
   100
#define DEC_r32(r1)           OP(0x48+r1)
nkeynes@368
   101
#define IMUL_r32(r1)          OP(0xF7); MODRM_rm32_r32(r1,5)
nkeynes@368
   102
#define INC_r32(r1)           OP(0x40+r1)
nkeynes@361
   103
#define JMP_rel8(rel)  OP(0xEB); OP(rel)
nkeynes@368
   104
#define MOV_r32_r32(r1,r2)    OP(0x89); MODRM_r32_rm32(r1,r2)
nkeynes@374
   105
#define MOV_r32_sh4r(r1,disp) OP(0x89); MODRM_r32_sh4r(r1,disp)
nkeynes@368
   106
#define MOV_moff32_EAX(off)   OP(0xA1); OP32(off)
nkeynes@374
   107
#define MOV_sh4r_r32(disp, r1)  OP(0x8B); MODRM_r32_sh4r(r1,disp)
nkeynes@359
   108
#define MOVSX_r8_r32(r1,r2)   OP(0x0F); OP(0xBE); MODRM_rm32_r32(r1,r2)
nkeynes@359
   109
#define MOVSX_r16_r32(r1,r2)  OP(0x0F); OP(0xBF); MODRM_rm32_r32(r1,r2)
nkeynes@359
   110
#define MOVZX_r8_r32(r1,r2)   OP(0x0F); OP(0xB6); MODRM_rm32_r32(r1,r2)
nkeynes@359
   111
#define MOVZX_r16_r32(r1,r2)  OP(0x0F); OP(0xB7); MODRM_rm32_r32(r1,r2)
nkeynes@368
   112
#define MUL_r32(r1)           OP(0xF7); MODRM_rm32_r32(r1,4)
nkeynes@359
   113
#define NEG_r32(r1)           OP(0xF7); MODRM_rm32_r32(r1,3)
nkeynes@359
   114
#define NOT_r32(r1)           OP(0xF7); MODRM_rm32_r32(r1,2)
nkeynes@359
   115
#define OR_r32_r32(r1,r2)     OP(0x0B); MODRM_rm32_r32(r1,r2)
nkeynes@368
   116
#define OR_imm8_r8(imm,r1)    OP(0x80); MODRM_rm32_r32(r1,1)
nkeynes@359
   117
#define OR_imm32_r32(imm,r1)  OP(0x81); MODRM_rm32_r32(r1,1); OP32(imm)
nkeynes@374
   118
#define OR_sh4r_r32(disp,r1)  OP(0x0B); MODRM_r32_sh4r(r1,disp)
nkeynes@368
   119
#define POP_r32(r1)           OP(0x58 + r1)
nkeynes@361
   120
#define PUSH_r32(r1)          OP(0x50 + r1)
nkeynes@368
   121
#define PUSH_imm32(imm)       OP(0x68); OP32(imm)
nkeynes@359
   122
#define RCL1_r32(r1)          OP(0xD1); MODRM_rm32_r32(r1,2)
nkeynes@359
   123
#define RCR1_r32(r1)          OP(0xD1); MODRM_rm32_r32(r1,3)
nkeynes@359
   124
#define RET()                 OP(0xC3)
nkeynes@359
   125
#define ROL1_r32(r1)          OP(0xD1); MODRM_rm32_r32(r1,0)
nkeynes@359
   126
#define ROR1_r32(r1)          OP(0xD1); MODRM_rm32_r32(r1,1)
nkeynes@359
   127
#define SAR1_r32(r1)          OP(0xD1); MODRM_rm32_r32(r1,7)
nkeynes@359
   128
#define SAR_imm8_r32(imm,r1)  OP(0xC1); MODRM_rm32_r32(r1,7); OP(imm)
nkeynes@368
   129
#define SAR_r32_CL(r1)        OP(0xD3); MODRM_rm32_r32(r1,7)
nkeynes@359
   130
#define SBB_r32_r32(r1,r2)    OP(0x1B); MODRM_rm32_r32(r1,r2)
nkeynes@359
   131
#define SHL1_r32(r1)          OP(0xD1); MODRM_rm32_r32(r1,4)
nkeynes@368
   132
#define SHL_r32_CL(r1)        OP(0xD3); MODRM_rm32_r32(r1,4)
nkeynes@359
   133
#define SHL_imm8_r32(imm,r1)  OP(0xC1); MODRM_rm32_r32(r1,4); OP(imm)
nkeynes@359
   134
#define SHR1_r32(r1)          OP(0xD1); MODRM_rm32_r32(r1,5)
nkeynes@368
   135
#define SHR_r32_CL(r1)        OP(0xD3); MODRM_rm32_r32(r1,5)
nkeynes@359
   136
#define SHR_imm8_r32(imm,r1)  OP(0xC1); MODRM_rm32_r32(r1,5); OP(imm)
nkeynes@374
   137
#define STC()                 OP(0xF9)
nkeynes@359
   138
#define SUB_r32_r32(r1,r2)    OP(0x2B); MODRM_rm32_r32(r1,r2)
nkeynes@374
   139
#define SUB_sh4r_r32(disp,r1)  OP(0x2B); MODRM_r32_sh4r(r1, disp)
nkeynes@368
   140
#define TEST_r8_r8(r1,r2)     OP(0x84); MODRM_r32_rm32(r1,r2)
nkeynes@359
   141
#define TEST_r32_r32(r1,r2)   OP(0x85); MODRM_rm32_r32(r1,r2)
nkeynes@368
   142
#define TEST_imm8_r8(imm8,r1) OP(0xF6); MODRM_rm32_r32(r1,0); OP(imm8)
nkeynes@359
   143
#define TEST_imm32_r32(imm,r1) OP(0xF7); MODRM_rm32_r32(r1,0); OP32(imm)
nkeynes@368
   144
#define XCHG_r8_r8(r1,r2)     OP(0x86); MODRM_rm32_r32(r1,r2)
nkeynes@359
   145
#define XOR_r32_r32(r1,r2)    OP(0x33); MODRM_rm32_r32(r1,r2)
nkeynes@374
   146
#define XOR_sh4r_r32(disp,r1)    OP(0x33); MODRM_r32_sh4r(r1,disp)
nkeynes@359
   147
#define XOR_imm32_r32(imm,r1) OP(0x81); MODRM_rm32_r32(r1,6); OP32(imm)
nkeynes@359
   148
nkeynes@359
   149
nkeynes@374
   150
/* Floating point ops */
nkeynes@374
   151
#define FABS_st0() OP(0xD9); OP(0xE1)
nkeynes@374
   152
#define FADDP_st(st) OP(0xDE); OP(0xC0+st)
nkeynes@374
   153
#define FCHS_st0() OP(0xD9); OP(0xE0)
nkeynes@374
   154
#define FDIVP_st(st) OP(0xDE); OP(0xF8+st)
nkeynes@374
   155
#define FMULP_st(st) OP(0xDE); OP(0xC8+st)
nkeynes@374
   156
#define FSUB_st(st) OP(0xDE); OP(0xE8+st)
nkeynes@374
   157
#define FSQRT_st0() OP(0xD9); OP(0xFA)
nkeynes@374
   158
nkeynes@359
   159
/* Conditional branches */
nkeynes@359
   160
#define JE_rel8(rel)   OP(0x74); OP(rel)
nkeynes@359
   161
#define JA_rel8(rel)   OP(0x77); OP(rel)
nkeynes@359
   162
#define JAE_rel8(rel)  OP(0x73); OP(rel)
nkeynes@359
   163
#define JG_rel8(rel)   OP(0x7F); OP(rel)
nkeynes@359
   164
#define JGE_rel8(rel)  OP(0x7D); OP(rel)
nkeynes@359
   165
#define JC_rel8(rel)   OP(0x72); OP(rel)
nkeynes@359
   166
#define JO_rel8(rel)   OP(0x70); OP(rel)
nkeynes@359
   167
#define JNE_rel8(rel)  OP(0x75); OP(rel)
nkeynes@359
   168
#define JNA_rel8(rel)  OP(0x76); OP(rel)
nkeynes@359
   169
#define JNAE_rel8(rel) OP(0x72); OP(rel)
nkeynes@359
   170
#define JNG_rel8(rel)  OP(0x7E); OP(rel)
nkeynes@359
   171
#define JNGE_rel8(rel) OP(0x7C); OP(rel)
nkeynes@359
   172
#define JNC_rel8(rel)  OP(0x73); OP(rel)
nkeynes@359
   173
#define JNO_rel8(rel)  OP(0x71); OP(rel)
nkeynes@359
   174
nkeynes@368
   175
/* 32-bit long forms w/ backpatching to an exit routine */
nkeynes@368
   176
#define JE_exit(rel)  OP(0x0F); OP(0x84); sh4_x86_add_backpatch(xlat_output); OP32(rel)
nkeynes@368
   177
#define JA_exit(rel)  OP(0x0F); OP(0x87); sh4_x86_add_backpatch(xlat_output); OP32(rel)
nkeynes@368
   178
#define JAE_exit(rel) OP(0x0F); OP(0x83); sh4_x86_add_backpatch(xlat_output); OP32(rel)
nkeynes@368
   179
#define JG_exit(rel)  OP(0x0F); OP(0x8F); sh4_x86_add_backpatch(xlat_output); OP32(rel)
nkeynes@368
   180
#define JGE_exit(rel) OP(0x0F); OP(0x8D); sh4_x86_add_backpatch(xlat_output); OP32(rel)
nkeynes@368
   181
#define JC_exit(rel)  OP(0x0F); OP(0x82); sh4_x86_add_backpatch(xlat_output); OP32(rel)
nkeynes@368
   182
#define JO_exit(rel)  OP(0x0F); OP(0x80); sh4_x86_add_backpatch(xlat_output); OP32(rel)
nkeynes@368
   183
#define JNE_exit(rel) OP(0x0F); OP(0x85); sh4_x86_add_backpatch(xlat_output); OP32(rel)
nkeynes@368
   184
#define JNA_exit(rel) OP(0x0F); OP(0x86); sh4_x86_add_backpatch(xlat_output); OP32(rel)
nkeynes@368
   185
#define JNAE_exit(rel) OP(0x0F);OP(0x82); sh4_x86_add_backpatch(xlat_output); OP32(rel)
nkeynes@368
   186
#define JNG_exit(rel) OP(0x0F); OP(0x8E); sh4_x86_add_backpatch(xlat_output); OP32(rel)
nkeynes@368
   187
#define JNGE_exit(rel) OP(0x0F);OP(0x8C); sh4_x86_add_backpatch(xlat_output); OP32(rel)
nkeynes@368
   188
#define JNC_exit(rel) OP(0x0F); OP(0x83); sh4_x86_add_backpatch(xlat_output); OP32(rel)
nkeynes@368
   189
#define JNO_exit(rel) OP(0x0F); OP(0x81); sh4_x86_add_backpatch(xlat_output); OP32(rel)
nkeynes@368
   190
nkeynes@368
   191
nkeynes@368
   192
/* Conditional moves ebp-rel */
nkeynes@368
   193
#define CMOVE_r32_r32(r1,r2)  OP(0x0F); OP(0x44); MODRM_rm32_r32(r1,r2)
nkeynes@368
   194
#define CMOVA_r32_r32(r1,r2)  OP(0x0F); OP(0x47); MODRM_rm32_r32(r1,r2)
nkeynes@368
   195
#define CMOVAE_r32_r32(r1,r2) OP(0x0F); OP(0x43); MODRM_rm32_r32(r1,r2)
nkeynes@368
   196
#define CMOVG_r32_r32(r1,r2)  OP(0x0F); OP(0x4F); MODRM_rm32_r32(r1,r2)
nkeynes@368
   197
#define CMOVGE_r32_r32(r1,r2)  OP(0x0F); OP(0x4D); MODRM_rm32_r32(r1,r2)
nkeynes@368
   198
#define CMOVC_r32_r32(r1,r2)  OP(0x0F); OP(0x42); MODRM_rm32_r32(r1,r2)
nkeynes@368
   199
#define CMOVO_r32_r32(r1,r2)  OP(0x0F); OP(0x40); MODRM_rm32_r32(r1,r2)
nkeynes@368
   200
nkeynes@368
   201
nkeynes@359
   202
/* Conditional setcc - writeback to sh4r.t */
nkeynes@374
   203
#define SETE_sh4r(disp)    OP(0x0F); OP(0x94); MODRM_r32_sh4r(0, disp);
nkeynes@374
   204
#define SETA_sh4r(disp)    OP(0x0F); OP(0x97); MODRM_r32_sh4r(0, disp);
nkeynes@374
   205
#define SETAE_sh4r(disp)   OP(0x0F); OP(0x93); MODRM_r32_sh4r(0, disp);
nkeynes@374
   206
#define SETG_sh4r(disp)    OP(0x0F); OP(0x9F); MODRM_r32_sh4r(0, disp);
nkeynes@374
   207
#define SETGE_sh4r(disp)   OP(0x0F); OP(0x9D); MODRM_r32_sh4r(0, disp);
nkeynes@374
   208
#define SETC_sh4r(disp)    OP(0x0F); OP(0x92); MODRM_r32_sh4r(0, disp);
nkeynes@374
   209
#define SETO_sh4r(disp)    OP(0x0F); OP(0x90); MODRM_r32_sh4r(0, disp);
nkeynes@359
   210
nkeynes@374
   211
#define SETNE_sh4r(disp)   OP(0x0F); OP(0x95); MODRM_r32_sh4r(0, disp);
nkeynes@374
   212
#define SETNA_sh4r(disp)   OP(0x0F); OP(0x96); MODRM_r32_sh4r(0, disp);
nkeynes@374
   213
#define SETNAE_sh4r(disp)  OP(0x0F); OP(0x92); MODRM_r32_sh4r(0, disp);
nkeynes@374
   214
#define SETNG_sh4r(disp)   OP(0x0F); OP(0x9E); MODRM_r32_sh4r(0, disp);
nkeynes@374
   215
#define SETNGE_sh4r(disp)  OP(0x0F); OP(0x9C); MODRM_r32_sh4r(0, disp);
nkeynes@374
   216
#define SETNC_sh4r(disp)   OP(0x0F); OP(0x93); MODRM_r32_sh4r(0, disp);
nkeynes@374
   217
#define SETNO_sh4r(disp)   OP(0x0F); OP(0x91); MODRM_r32_sh4r(0, disp);
nkeynes@368
   218
nkeynes@374
   219
#define SETE_t() SETE_sh4r(R_T)
nkeynes@374
   220
#define SETA_t() SETA_sh4r(R_T)
nkeynes@374
   221
#define SETAE_t() SETAE_sh4r(R_T)
nkeynes@374
   222
#define SETG_t() SETG_sh4r(R_T)
nkeynes@374
   223
#define SETGE_t() SETGE_sh4r(R_T)
nkeynes@374
   224
#define SETC_t() SETC_sh4r(R_T)
nkeynes@374
   225
#define SETO_t() SETO_sh4r(R_T)
nkeynes@374
   226
nkeynes@374
   227
#define SETC_r32(r1)      OP(0x0F); OP(0x92); MODRM_rm32_r32(r1, 0)
nkeynes@359
   228
nkeynes@359
   229
/* Pseudo-op Load carry from T: CMP [EBP+t], #01 ; CMC */
nkeynes@374
   230
#define LDC_t()     OP(0x83); MODRM_r32_sh4r(7,R_T); OP(0x01); CMC()
nkeynes@359
   231
nkeynes@359
   232
#endif /* !__lxdream_x86op_H */
.