exec-i386.h 8.1 KB
Newer Older
B
bellard 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *  i386 execution defines 
 *
 *  Copyright (c) 2003 Fabrice Bellard
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
20 21 22 23 24 25 26 27 28 29 30 31 32
#include "dyngen-exec.h"

/* at least 4 register variables are defines */
register struct CPUX86State *env asm(AREG0);
register uint32_t T0 asm(AREG1);
register uint32_t T1 asm(AREG2);
register uint32_t T2 asm(AREG3);

#define A0 T2

/* if more registers are available, we define some registers too */
#ifdef AREG4
register uint32_t EAX asm(AREG4);
B
bellard 已提交
33
#define reg_EAX
B
bellard 已提交
34
#endif
35 36 37

#ifdef AREG5
register uint32_t ESP asm(AREG5);
B
bellard 已提交
38
#define reg_ESP
39 40 41 42
#endif

#ifdef AREG6
register uint32_t EBP asm(AREG6);
B
bellard 已提交
43
#define reg_EBP
B
bellard 已提交
44
#endif
45 46 47 48

#ifdef AREG7
register uint32_t ECX asm(AREG7);
#define reg_ECX
B
bellard 已提交
49
#endif
50 51 52 53

#ifdef AREG8
register uint32_t EDX asm(AREG8);
#define reg_EDX
B
bellard 已提交
54
#endif
55 56 57 58

#ifdef AREG9
register uint32_t EBX asm(AREG9);
#define reg_EBX
B
bellard 已提交
59
#endif
B
bellard 已提交
60

61 62 63 64
#ifdef AREG10
register uint32_t ESI asm(AREG10);
#define reg_ESI
#endif
B
bellard 已提交
65

66 67 68
#ifdef AREG11
register uint32_t EDI asm(AREG11);
#define reg_EDI
B
bellard 已提交
69 70
#endif

71 72
extern FILE *logfile;
extern int loglevel;
B
bellard 已提交
73

B
bellard 已提交
74
#ifndef reg_EAX
B
bellard 已提交
75
#define EAX (env->regs[R_EAX])
B
bellard 已提交
76 77
#endif
#ifndef reg_ECX
B
bellard 已提交
78
#define ECX (env->regs[R_ECX])
B
bellard 已提交
79 80
#endif
#ifndef reg_EDX
B
bellard 已提交
81
#define EDX (env->regs[R_EDX])
B
bellard 已提交
82 83
#endif
#ifndef reg_EBX
B
bellard 已提交
84
#define EBX (env->regs[R_EBX])
B
bellard 已提交
85 86
#endif
#ifndef reg_ESP
B
bellard 已提交
87
#define ESP (env->regs[R_ESP])
B
bellard 已提交
88 89
#endif
#ifndef reg_EBP
B
bellard 已提交
90
#define EBP (env->regs[R_EBP])
B
bellard 已提交
91 92
#endif
#ifndef reg_ESI
B
bellard 已提交
93
#define ESI (env->regs[R_ESI])
B
bellard 已提交
94 95
#endif
#ifndef reg_EDI
B
bellard 已提交
96
#define EDI (env->regs[R_EDI])
B
bellard 已提交
97
#endif
B
bellard 已提交
98
#define EIP  (env->eip)
B
bellard 已提交
99 100 101 102 103 104 105 106 107 108 109 110
#define DF  (env->df)

#define CC_SRC (env->cc_src)
#define CC_DST (env->cc_dst)
#define CC_OP  (env->cc_op)

/* float macros */
#define FT0    (env->ft0)
#define ST0    (env->fpregs[env->fpstt])
#define ST(n)  (env->fpregs[(env->fpstt + (n)) & 7])
#define ST1    ST(1)

B
bellard 已提交
111 112 113 114
#ifdef USE_FP_CONVERT
#define FP_CONVERT  (env->fp_convert)
#endif

B
bellard 已提交
115
#include "cpu-i386.h"
B
bellard 已提交
116
#include "exec.h"
B
bellard 已提交
117 118 119 120 121 122 123

typedef struct CCTable {
    int (*compute_all)(void); /* return all the flags */
    int (*compute_c)(void);  /* return the C flag */
} CCTable;

extern CCTable cc_table[];
B
bellard 已提交
124

B
bellard 已提交
125
void load_seg(int seg_reg, int selector, unsigned cur_eip);
B
bellard 已提交
126 127 128 129 130
void jmp_seg(int selector, unsigned int new_eip);
void helper_lldt_T0(void);
void helper_ltr_T0(void);
void helper_movl_crN_T0(int reg);
void helper_movl_drN_T0(int reg);
131 132
void __hidden cpu_lock(void);
void __hidden cpu_unlock(void);
B
bellard 已提交
133 134
void raise_interrupt(int intno, int is_int, int error_code, 
                     unsigned int next_eip);
B
bellard 已提交
135
void raise_exception_err(int exception_index, int error_code);
B
bellard 已提交
136
void raise_exception(int exception_index);
137
void __hidden cpu_loop_exit(void);
138 139
void helper_fsave(uint8_t *ptr, int data32);
void helper_frstor(uint8_t *ptr, int data32);
B
bellard 已提交
140 141 142

void OPPROTO op_movl_eflags_T0(void);
void OPPROTO op_movl_T0_eflags(void);
143 144 145 146
void raise_interrupt(int intno, int is_int, int error_code, 
                     unsigned int next_eip);
void raise_exception_err(int exception_index, int error_code);
void raise_exception(int exception_index);
B
bellard 已提交
147 148 149
void helper_divl_EAX_T0(uint32_t eip);
void helper_idivl_EAX_T0(uint32_t eip);
void helper_cmpxchg8b(void);
150
void helper_cpuid(void);
B
bellard 已提交
151
void helper_rdtsc(void);
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
void helper_lsl(void);
void helper_lar(void);

#ifdef USE_X86LDOUBLE
/* use long double functions */
#define lrint lrintl
#define llrint llrintl
#define fabs fabsl
#define sin sinl
#define cos cosl
#define sqrt sqrtl
#define pow powl
#define log logl
#define tan tanl
#define atan2 atan2l
#define floor floorl
#define ceil ceill
#define rint rintl
#endif

extern int lrint(CPU86_LDouble x);
extern int64_t llrint(CPU86_LDouble x);
extern CPU86_LDouble fabs(CPU86_LDouble x);
extern CPU86_LDouble sin(CPU86_LDouble x);
extern CPU86_LDouble cos(CPU86_LDouble x);
extern CPU86_LDouble sqrt(CPU86_LDouble x);
extern CPU86_LDouble pow(CPU86_LDouble, CPU86_LDouble);
extern CPU86_LDouble log(CPU86_LDouble x);
extern CPU86_LDouble tan(CPU86_LDouble x);
extern CPU86_LDouble atan2(CPU86_LDouble, CPU86_LDouble);
extern CPU86_LDouble floor(CPU86_LDouble x);
extern CPU86_LDouble ceil(CPU86_LDouble x);
extern CPU86_LDouble rint(CPU86_LDouble x);

#define RC_MASK         0xc00
#define RC_NEAR		0x000
#define RC_DOWN		0x400
#define RC_UP		0x800
#define RC_CHOP		0xc00

#define MAXTAN 9223372036854775808.0

B
bellard 已提交
194 195 196 197 198 199 200
#ifdef __arm__
/* we have no way to do correct rounding - a FPU emulator is needed */
#define FE_DOWNWARD   FE_TONEAREST
#define FE_UPWARD     FE_TONEAREST
#define FE_TOWARDZERO FE_TONEAREST
#endif

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
#ifdef USE_X86LDOUBLE

/* only for x86 */
typedef union {
    long double d;
    struct {
        unsigned long long lower;
        unsigned short upper;
    } l;
} CPU86_LDoubleU;

/* the following deal with x86 long double-precision numbers */
#define MAXEXPD 0x7fff
#define EXPBIAS 16383
#define EXPD(fp)	(fp.l.upper & 0x7fff)
#define SIGND(fp)	((fp.l.upper) & 0x8000)
#define MANTD(fp)       (fp.l.lower)
#define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS

#else

B
bellard 已提交
222
/* NOTE: arm is horrible as double 32 bit words are stored in big endian ! */
223 224
typedef union {
    double d;
B
bellard 已提交
225
#if !defined(WORDS_BIGENDIAN) && !defined(__arm__)
226 227 228 229 230 231 232 233 234 235
    struct {
        uint32_t lower;
        int32_t upper;
    } l;
#else
    struct {
        int32_t upper;
        uint32_t lower;
    } l;
#endif
B
bellard 已提交
236
#ifndef __arm__
237
    int64_t ll;
B
bellard 已提交
238
#endif
239 240 241 242 243 244 245
} CPU86_LDoubleU;

/* the following deal with IEEE double-precision numbers */
#define MAXEXPD 0x7ff
#define EXPBIAS 1023
#define EXPD(fp)	(((fp.l.upper) >> 20) & 0x7FF)
#define SIGND(fp)	((fp.l.upper) & 0x80000000)
B
bellard 已提交
246 247 248
#ifdef __arm__
#define MANTD(fp)	(fp.l.lower | ((uint64_t)(fp.l.upper & ((1 << 20) - 1)) << 32))
#else
249
#define MANTD(fp)	(fp.ll & ((1LL << 52) - 1))
B
bellard 已提交
250
#endif
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
#define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7ff << 20)) | (EXPBIAS << 20)
#endif

static inline void fpush(void)
{
    env->fpstt = (env->fpstt - 1) & 7;
    env->fptags[env->fpstt] = 0; /* validate stack entry */
}

static inline void fpop(void)
{
    env->fptags[env->fpstt] = 1; /* invvalidate stack entry */
    env->fpstt = (env->fpstt + 1) & 7;
}

#ifndef USE_X86LDOUBLE
static inline CPU86_LDouble helper_fldt(uint8_t *ptr)
{
    CPU86_LDoubleU temp;
    int upper, e;
B
bellard 已提交
271 272
    uint64_t ll;

273 274 275 276 277
    /* mantissa */
    upper = lduw(ptr + 8);
    /* XXX: handle overflow ? */
    e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
    e |= (upper >> 4) & 0x800; /* sign */
B
bellard 已提交
278 279 280 281 282 283 284
    ll = (ldq(ptr) >> 11) & ((1LL << 52) - 1);
#ifdef __arm__
    temp.l.upper = (e << 20) | (ll >> 32);
    temp.l.lower = ll;
#else
    temp.ll = ll | ((uint64_t)e << 52);
#endif
285 286 287 288 289 290 291
    return temp.d;
}

static inline void helper_fstt(CPU86_LDouble f, uint8_t *ptr)
{
    CPU86_LDoubleU temp;
    int e;
B
bellard 已提交
292

293 294 295 296 297 298 299 300 301 302
    temp.d = f;
    /* mantissa */
    stq(ptr, (MANTD(temp) << 11) | (1LL << 63));
    /* exponent + sign */
    e = EXPD(temp) - EXPBIAS + 16383;
    e |= SIGND(temp) >> 16;
    stw(ptr + 8, e);
}
#endif

B
bellard 已提交
303 304
const CPU86_LDouble f15rk[7];

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
void helper_fldt_ST0_A0(void);
void helper_fstt_ST0_A0(void);
void helper_fbld_ST0_A0(void);
void helper_fbst_ST0_A0(void);
void helper_f2xm1(void);
void helper_fyl2x(void);
void helper_fptan(void);
void helper_fpatan(void);
void helper_fxtract(void);
void helper_fprem1(void);
void helper_fprem(void);
void helper_fyl2xp1(void);
void helper_fsqrt(void);
void helper_fsincos(void);
void helper_frndint(void);
void helper_fscale(void);
void helper_fsin(void);
void helper_fcos(void);
void helper_fxam_ST0(void);
void helper_fstenv(uint8_t *ptr, int data32);
void helper_fldenv(uint8_t *ptr, int data32);
void helper_fsave(uint8_t *ptr, int data32);
void helper_frstor(uint8_t *ptr, int data32);

329 330 331
const uint8_t parity_table[256];
const uint8_t rclw_table[32];
const uint8_t rclb_table[32];