2008-01-29 20:37:37 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2008-10-11 00:49:16 +04:00
|
|
|
// $Id: instr.h,v 1.18 2008-10-10 20:49:16 sshwarts Exp $
|
2008-01-29 20:37:37 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright (c) 2008 Stanislav Shwartsman
|
|
|
|
// Written by Stanislav Shwartsman [sshwarts at sourceforge net]
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef BX_INSTR_H
|
|
|
|
# define BX_INSTR_H 1
|
|
|
|
|
2008-03-23 00:29:41 +03:00
|
|
|
class bxInstruction_c;
|
|
|
|
|
|
|
|
// <TAG-TYPE-EXECUTEPTR-START>
|
|
|
|
#if BX_USE_CPU_SMF
|
|
|
|
typedef void (BX_CPP_AttrRegparmN(1) *BxExecutePtr_tR)(bxInstruction_c *);
|
2008-08-08 13:22:49 +04:00
|
|
|
typedef bx_address (BX_CPP_AttrRegparmN(1) *BxResolvePtr_tR)(bxInstruction_c *);
|
2008-03-23 00:29:41 +03:00
|
|
|
#else
|
|
|
|
typedef void (BX_CPU_C::*BxExecutePtr_tR)(bxInstruction_c *) BX_CPP_AttrRegparmN(1);
|
2008-08-08 13:22:49 +04:00
|
|
|
typedef bx_address (BX_CPU_C::*BxResolvePtr_tR)(bxInstruction_c *) BX_CPP_AttrRegparmN(1);
|
2008-03-23 00:29:41 +03:00
|
|
|
#endif
|
|
|
|
// <TAG-TYPE-EXECUTEPTR-END>
|
|
|
|
|
2008-01-29 20:37:37 +03:00
|
|
|
// <TAG-CLASS-INSTRUCTION-START>
|
|
|
|
class bxInstruction_c {
|
|
|
|
public:
|
|
|
|
// Function pointers; a function to resolve the modRM address
|
|
|
|
// given the current state of the CPU and the instruction data,
|
|
|
|
// and a function to execute the instruction after resolving
|
|
|
|
// the memory address (if any).
|
2008-03-23 00:29:41 +03:00
|
|
|
BxExecutePtr_tR execute;
|
2008-08-10 01:05:07 +04:00
|
|
|
BxExecutePtr_tR execute2;
|
2008-10-11 00:49:16 +04:00
|
|
|
BxResolvePtr_tR ResolveModrm;
|
2008-06-25 14:34:21 +04:00
|
|
|
#if BX_INSTRUMENTATION
|
|
|
|
Bit16u ia_opcode;
|
|
|
|
#endif
|
2008-01-29 20:37:37 +03:00
|
|
|
|
|
|
|
struct {
|
2008-08-09 23:18:09 +04:00
|
|
|
// 7...1 (unused)
|
|
|
|
// 0...0 stop trace (used with trace cache)
|
2008-04-05 21:51:55 +04:00
|
|
|
Bit8u metaInfo4;
|
|
|
|
|
|
|
|
// 7...0 b1 - opcode byte
|
|
|
|
Bit8u metaInfo3;
|
2008-01-29 20:37:37 +03:00
|
|
|
|
2008-04-05 21:51:55 +04:00
|
|
|
// 7...4 (unused)
|
|
|
|
// 3...0 ilen (0..15)
|
|
|
|
Bit8u metaInfo2;
|
2008-01-29 20:37:37 +03:00
|
|
|
|
2008-04-05 21:51:55 +04:00
|
|
|
// 7...7 extend8bit
|
|
|
|
// 6...6 as64
|
|
|
|
// 5...5 os64
|
|
|
|
// 4...4 as32
|
|
|
|
// 3...3 os32
|
|
|
|
// 2...2 mod==c0 (modrm)
|
|
|
|
// 1...0 repUsed (0=none, 2=0xF2, 3=0xF3)
|
|
|
|
Bit8u metaInfo1;
|
2008-01-29 20:37:37 +03:00
|
|
|
} metaInfo;
|
|
|
|
|
2008-03-31 21:33:34 +04:00
|
|
|
#define BX_INSTR_METADATA_SEG 0
|
|
|
|
#define BX_INSTR_METADATA_DEST 1
|
|
|
|
#define BX_INSTR_METADATA_NNN 2
|
|
|
|
#define BX_INSTR_METADATA_RM 3
|
|
|
|
#define BX_INSTR_METADATA_BASE 4
|
|
|
|
#define BX_INSTR_METADATA_INDEX 5
|
|
|
|
#define BX_INSTR_METADATA_SCALE 6
|
|
|
|
#define BX_INSTR_METADATA_MODRM 7
|
2008-02-05 00:28:53 +03:00
|
|
|
|
2008-03-31 21:33:34 +04:00
|
|
|
// using 5-bit field for registers (16 regs in 64-bit, RIP, NIL)
|
|
|
|
Bit8u metaData[8];
|
2008-01-29 20:37:37 +03:00
|
|
|
|
|
|
|
union {
|
|
|
|
// Form (longest case): [opcode+modrm+sib/displacement32/immediate32]
|
|
|
|
struct {
|
|
|
|
union {
|
|
|
|
Bit32u Id;
|
|
|
|
Bit16u Iw;
|
|
|
|
Bit8u Ib;
|
|
|
|
};
|
|
|
|
union {
|
|
|
|
Bit16u displ16u; // for 16-bit modrm forms
|
|
|
|
Bit32u displ32u; // for 32-bit modrm forms
|
|
|
|
};
|
|
|
|
} modRMForm;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
union {
|
|
|
|
Bit32u Id;
|
|
|
|
Bit16u Iw;
|
|
|
|
Bit8u Ib;
|
|
|
|
};
|
|
|
|
union {
|
|
|
|
Bit32u Id2; // Not used (for alignment)
|
|
|
|
Bit16u Iw2;
|
|
|
|
Bit8u Ib2;
|
|
|
|
};
|
|
|
|
} IxIxForm;
|
|
|
|
|
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
struct {
|
|
|
|
Bit64u Iq; // for MOV Rx,imm64
|
|
|
|
} IqForm;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned modC0() const
|
2008-01-29 20:37:37 +03:00
|
|
|
{
|
|
|
|
// This is a cheaper way to test for modRM instructions where
|
|
|
|
// the mod field is 0xc0. FetchDecode flags this condition since
|
|
|
|
// it is quite common to be tested for.
|
2008-02-05 00:28:53 +03:00
|
|
|
return metaInfo.metaInfo1 & (1<<2);
|
2008-01-29 20:37:37 +03:00
|
|
|
}
|
|
|
|
BX_CPP_INLINE unsigned assertModC0()
|
|
|
|
{
|
2008-02-05 00:28:53 +03:00
|
|
|
return metaInfo.metaInfo1 |= (1<<2);
|
2008-01-29 20:37:37 +03:00
|
|
|
}
|
2008-03-31 21:33:34 +04:00
|
|
|
BX_CPP_INLINE void setOpcodeReg(unsigned opreg) {
|
|
|
|
// The opcodeReg form (low 3 bits of the opcode byte (extended
|
|
|
|
// by REX.B on x86-64) to be used with IxIxForm or IqForm.
|
|
|
|
metaData[BX_INSTR_METADATA_RM] = opreg;
|
|
|
|
}
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned opcodeReg() const {
|
2008-03-31 21:33:34 +04:00
|
|
|
return metaData[BX_INSTR_METADATA_RM];
|
|
|
|
}
|
|
|
|
BX_CPP_INLINE void setModRM(unsigned modrm) {
|
|
|
|
metaData[BX_INSTR_METADATA_MODRM] = modrm;
|
|
|
|
}
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned modrm() const {
|
2008-03-31 21:33:34 +04:00
|
|
|
return metaData[BX_INSTR_METADATA_MODRM];
|
|
|
|
}
|
|
|
|
BX_CPP_INLINE void setNnn(unsigned nnn) {
|
|
|
|
metaData[BX_INSTR_METADATA_NNN] = nnn;
|
|
|
|
}
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned nnn() const {
|
2008-03-31 21:33:34 +04:00
|
|
|
return metaData[BX_INSTR_METADATA_NNN];
|
|
|
|
}
|
|
|
|
BX_CPP_INLINE void setRm(unsigned rm) {
|
|
|
|
metaData[BX_INSTR_METADATA_RM] = rm;
|
2008-01-29 20:37:37 +03:00
|
|
|
}
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned rm() const {
|
2008-03-31 21:33:34 +04:00
|
|
|
return metaData[BX_INSTR_METADATA_RM];
|
2008-02-05 00:28:53 +03:00
|
|
|
}
|
|
|
|
BX_CPP_INLINE void setSibScale(unsigned scale) {
|
2008-03-31 21:33:34 +04:00
|
|
|
metaData[BX_INSTR_METADATA_SCALE] = scale;
|
2008-01-29 20:37:37 +03:00
|
|
|
}
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned sibScale() const {
|
2008-03-31 21:33:34 +04:00
|
|
|
return metaData[BX_INSTR_METADATA_SCALE];
|
2008-02-05 00:28:53 +03:00
|
|
|
}
|
|
|
|
BX_CPP_INLINE void setSibIndex(unsigned index) {
|
2008-03-31 21:33:34 +04:00
|
|
|
metaData[BX_INSTR_METADATA_INDEX] = index;
|
2008-01-29 20:37:37 +03:00
|
|
|
}
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned sibIndex() const {
|
2008-03-31 21:33:34 +04:00
|
|
|
return metaData[BX_INSTR_METADATA_INDEX];
|
2008-01-29 20:37:37 +03:00
|
|
|
}
|
2008-01-30 01:26:29 +03:00
|
|
|
BX_CPP_INLINE void setSibBase(unsigned base) {
|
2008-03-31 21:33:34 +04:00
|
|
|
metaData[BX_INSTR_METADATA_BASE] = base;
|
2008-01-29 20:37:37 +03:00
|
|
|
}
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned sibBase() const {
|
2008-03-31 21:33:34 +04:00
|
|
|
return metaData[BX_INSTR_METADATA_BASE];
|
2008-01-29 20:37:37 +03:00
|
|
|
}
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE Bit32u displ32u() const { return modRMForm.displ32u; }
|
|
|
|
BX_CPP_INLINE Bit16u displ16u() const { return modRMForm.displ16u; }
|
|
|
|
BX_CPP_INLINE Bit32u Id() const { return modRMForm.Id; }
|
|
|
|
BX_CPP_INLINE Bit16u Iw() const { return modRMForm.Iw; }
|
|
|
|
BX_CPP_INLINE Bit8u Ib() const { return modRMForm.Ib; }
|
|
|
|
BX_CPP_INLINE Bit16u Iw2() const { return IxIxForm.Iw2; } // Legacy
|
|
|
|
BX_CPP_INLINE Bit8u Ib2() const { return IxIxForm.Ib2; } // Legacy
|
2008-01-29 20:37:37 +03:00
|
|
|
#if BX_SUPPORT_X86_64
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE Bit64u Iq() const { return IqForm.Iq; }
|
2008-01-29 20:37:37 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Info in the metaInfo field.
|
|
|
|
// Note: the 'L' at the end of certain flags, means the value returned
|
|
|
|
// is for Logical comparisons, eg if (i->os32L() && i->as32L()). If you
|
|
|
|
// want a bx_bool value, use os32B() etc. This makes for smaller
|
|
|
|
// code, when a strict 0 or 1 is not necessary.
|
2008-02-05 00:28:53 +03:00
|
|
|
BX_CPP_INLINE void init(unsigned os32, unsigned as32, unsigned os64, unsigned as64)
|
2008-01-29 20:37:37 +03:00
|
|
|
{
|
2008-02-05 00:28:53 +03:00
|
|
|
metaInfo.metaInfo1 = (os32<<3) | (as32<<4) | (os64<<5) | (as64<<6);
|
2008-04-05 21:51:55 +04:00
|
|
|
metaInfo.metaInfo4 = 0;
|
2008-01-29 20:37:37 +03:00
|
|
|
}
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned seg(void) const {
|
2008-03-31 21:33:34 +04:00
|
|
|
return metaData[BX_INSTR_METADATA_SEG];
|
2008-01-29 20:37:37 +03:00
|
|
|
}
|
|
|
|
BX_CPP_INLINE void setSeg(unsigned val) {
|
2008-03-31 21:33:34 +04:00
|
|
|
metaData[BX_INSTR_METADATA_SEG] = val;
|
2008-01-29 20:37:37 +03:00
|
|
|
}
|
|
|
|
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned os32L(void) const {
|
2008-01-29 20:37:37 +03:00
|
|
|
return metaInfo.metaInfo1 & (1<<3);
|
|
|
|
}
|
|
|
|
BX_CPP_INLINE void setOs32B(unsigned bit) {
|
|
|
|
metaInfo.metaInfo1 = (metaInfo.metaInfo1 & ~(1<<3)) | (bit<<3);
|
|
|
|
}
|
|
|
|
BX_CPP_INLINE void assertOs32(void) {
|
|
|
|
metaInfo.metaInfo1 |= (1<<3);
|
|
|
|
}
|
|
|
|
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned as32L(void) const {
|
2008-01-29 20:37:37 +03:00
|
|
|
return metaInfo.metaInfo1 & (1<<4);
|
|
|
|
}
|
|
|
|
BX_CPP_INLINE void setAs32B(unsigned bit) {
|
|
|
|
metaInfo.metaInfo1 = (metaInfo.metaInfo1 & ~(1<<4)) | (bit<<4);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if BX_SUPPORT_X86_64
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned os64L(void) const {
|
2008-01-29 20:37:37 +03:00
|
|
|
return metaInfo.metaInfo1 & (1<<5);
|
|
|
|
}
|
|
|
|
BX_CPP_INLINE void assertOs64(void) {
|
|
|
|
metaInfo.metaInfo1 |= (1<<5);
|
|
|
|
}
|
|
|
|
#else
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned os64L(void) const { return 0; }
|
2008-01-29 20:37:37 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if BX_SUPPORT_X86_64
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned as64L(void) const {
|
2008-01-29 20:37:37 +03:00
|
|
|
return metaInfo.metaInfo1 & (1<<6);
|
|
|
|
}
|
|
|
|
BX_CPP_INLINE void setAs64B(unsigned bit) {
|
|
|
|
metaInfo.metaInfo1 = (metaInfo.metaInfo1 & ~(1<<6)) | (bit<<6);
|
|
|
|
}
|
|
|
|
#else
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned as64L(void) const { return 0; }
|
2008-01-29 20:37:37 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if BX_SUPPORT_X86_64
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned extend8bitL(void) const {
|
2008-01-29 20:37:37 +03:00
|
|
|
return metaInfo.metaInfo1 & (1<<7);
|
|
|
|
}
|
|
|
|
BX_CPP_INLINE void assertExtend8bit(void) {
|
|
|
|
metaInfo.metaInfo1 |= (1<<7);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned ilen(void) const {
|
2008-01-29 20:37:37 +03:00
|
|
|
return metaInfo.metaInfo2;
|
|
|
|
}
|
|
|
|
BX_CPP_INLINE void setILen(unsigned ilen) {
|
|
|
|
metaInfo.metaInfo2 = ilen;
|
|
|
|
}
|
|
|
|
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned repUsedL(void) const {
|
2008-02-05 00:28:53 +03:00
|
|
|
return metaInfo.metaInfo1 & 3;
|
2008-01-29 20:37:37 +03:00
|
|
|
}
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned repUsedValue(void) const {
|
2008-02-05 00:28:53 +03:00
|
|
|
return metaInfo.metaInfo1 & 3;
|
2008-01-29 20:37:37 +03:00
|
|
|
}
|
|
|
|
BX_CPP_INLINE void setRepUsed(unsigned value) {
|
2008-02-05 00:28:53 +03:00
|
|
|
metaInfo.metaInfo1 = (metaInfo.metaInfo1 & ~3) | (value);
|
2008-01-29 20:37:37 +03:00
|
|
|
}
|
|
|
|
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned b1(void) const {
|
2008-04-05 21:51:55 +04:00
|
|
|
return metaInfo.metaInfo3;
|
|
|
|
}
|
|
|
|
BX_CPP_INLINE void setB1(unsigned b1) {
|
|
|
|
metaInfo.metaInfo3 = b1 & 0xff;
|
|
|
|
}
|
|
|
|
|
2008-01-29 20:37:37 +03:00
|
|
|
#if BX_SUPPORT_TRACE_CACHE
|
|
|
|
BX_CPP_INLINE void setStopTraceAttr(void) {
|
2008-08-09 23:18:09 +04:00
|
|
|
metaInfo.metaInfo4 |= 1;
|
2008-01-29 20:37:37 +03:00
|
|
|
}
|
2008-07-13 13:59:59 +04:00
|
|
|
BX_CPP_INLINE unsigned getStopTraceAttr(void) const {
|
2008-08-09 23:18:09 +04:00
|
|
|
return metaInfo.metaInfo4 & 1;
|
2008-01-29 20:37:37 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
// <TAG-CLASS-INSTRUCTION-END>
|
|
|
|
|
|
|
|
#endif
|