📄 ghelpers.c
字号:
/*---------------------------------------------------------------*//*--- ---*//*--- This file (guest-x86/ghelpers.c) is ---*//*--- Copyright (C) OpenWorks LLP. All rights reserved. ---*//*--- ---*//*---------------------------------------------------------------*//* This file is part of LibVEX, a library for dynamic binary instrumentation and translation. Copyright (C) 2004-2006 OpenWorks LLP. All rights reserved. This library is made available under a dual licensing scheme. If you link LibVEX against other code all of which is itself licensed under the GNU General Public License, version 2 dated June 1991 ("GPL v2"), then you may use LibVEX under the terms of the GPL v2, as appearing in the file LICENSE.GPL. If the file LICENSE.GPL is missing, you can obtain a copy of the GPL v2 from the Free Software Foundation Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. For any other uses of LibVEX, you must first obtain a commercial license from OpenWorks LLP. Please contact info@open-works.co.uk for information about commercial licensing. This software is provided by OpenWorks LLP "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall OpenWorks LLP be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage. Neither the names of the U.S. Department of Energy nor the University of California nor the names of its contributors may be used to endorse or promote products derived from this software without prior written permission.*/#include "libvex_basictypes.h"#include "libvex_emwarn.h"#include "libvex_guest_x86.h"#include "libvex_ir.h"#include "libvex.h"#include "main/vex_util.h"#include "guest-generic/bb_to_IR.h"#include "guest-x86/gdefs.h"#include "guest-generic/g_generic_x87.h"/* This file contains helper functions for x86 guest code. Calls to these functions are generated by the back end. These calls are of course in the host machine code and this file will be compiled to host machine code, so that all makes sense. Only change the signatures of these helper functions very carefully. If you change the signature here, you'll have to change the parameters passed to it in the IR calls constructed by guest-x86/toIR.c. The convention used is that all functions called from generated code are named x86g_<something>, and any function whose name lacks that prefix is not called from generated code. Note that some LibVEX_* functions can however be called by VEX's client, but that is not the same as calling them from VEX-generated code.*//* Set to 1 to get detailed profiling info about use of the flag machinery. */#define PROFILE_EFLAGS 0/*---------------------------------------------------------------*//*--- %eflags run-time helpers. ---*//*---------------------------------------------------------------*/static const UChar parity_table[256] = { X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P, X86G_CC_MASK_P, 0, X86G_CC_MASK_P, 0, 0, X86G_CC_MASK_P,};/* generalised left-shifter */inline static Int lshift ( Int x, Int n ){ if (n >= 0) return x << n; else return x >> (-n);}/* identity on ULong */static inline ULong idULong ( ULong x ){ return x;}#define PREAMBLE(__data_bits) \ /* const */ UInt DATA_MASK \ = __data_bits==8 ? 0xFF \ : (__data_bits==16 ? 0xFFFF \ : 0xFFFFFFFF); \ /* const */ UInt SIGN_MASK = 1 << (__data_bits - 1); \ /* const */ UInt CC_DEP1 = cc_dep1_formal; \ /* const */ UInt CC_DEP2 = cc_dep2_formal; \ /* const */ UInt CC_NDEP = cc_ndep_formal; \ /* Four bogus assignments, which hopefully gcc can */ \ /* optimise away, and which stop it complaining about */ \ /* unused variables. */ \ SIGN_MASK = SIGN_MASK; \ DATA_MASK = DATA_MASK; \ CC_DEP2 = CC_DEP2; \ CC_NDEP = CC_NDEP;/*-------------------------------------------------------------*/#define ACTIONS_ADD(DATA_BITS,DATA_UTYPE) \{ \ PREAMBLE(DATA_BITS); \ { Int cf, pf, af, zf, sf, of; \ Int argL, argR, res; \ argL = CC_DEP1; \ argR = CC_DEP2; \ res = argL + argR; \ cf = (DATA_UTYPE)res < (DATA_UTYPE)argL; \ pf = parity_table[(UChar)res]; \ af = (res ^ argL ^ argR) & 0x10; \ zf = ((DATA_UTYPE)res == 0) << 6; \ sf = lshift(res, 8 - DATA_BITS) & 0x80; \ of = lshift((argL ^ argR ^ -1) & (argL ^ res), \ 12 - DATA_BITS) & X86G_CC_MASK_O; \ return cf | pf | af | zf | sf | of; \ } \}/*-------------------------------------------------------------*/#define ACTIONS_SUB(DATA_BITS,DATA_UTYPE) \{ \ PREAMBLE(DATA_BITS); \ { Int cf, pf, af, zf, sf, of; \ Int argL, argR, res; \ argL = CC_DEP1; \ argR = CC_DEP2; \ res = argL - argR; \ cf = (DATA_UTYPE)argL < (DATA_UTYPE)argR; \ pf = parity_table[(UChar)res]; \ af = (res ^ argL ^ argR) & 0x10; \ zf = ((DATA_UTYPE)res == 0) << 6; \ sf = lshift(res, 8 - DATA_BITS) & 0x80; \ of = lshift((argL ^ argR) & (argL ^ res), \ 12 - DATA_BITS) & X86G_CC_MASK_O; \ return cf | pf | af | zf | sf | of; \ } \}/*-------------------------------------------------------------*/#define ACTIONS_ADC(DATA_BITS,DATA_UTYPE) \{ \ PREAMBLE(DATA_BITS); \ { Int cf, pf, af, zf, sf, of; \ Int argL, argR, oldC, res; \ oldC = CC_NDEP & X86G_CC_MASK_C; \ argL = CC_DEP1; \ argR = CC_DEP2 ^ oldC; \ res = (argL + argR) + oldC; \ if (oldC) \ cf = (DATA_UTYPE)res <= (DATA_UTYPE)argL; \ else \ cf = (DATA_UTYPE)res < (DATA_UTYPE)argL; \ pf = parity_table[(UChar)res]; \ af = (res ^ argL ^ argR) & 0x10; \ zf = ((DATA_UTYPE)res == 0) << 6; \ sf = lshift(res, 8 - DATA_BITS) & 0x80; \ of = lshift((argL ^ argR ^ -1) & (argL ^ res), \ 12 - DATA_BITS) & X86G_CC_MASK_O; \ return cf | pf | af | zf | sf | of; \ } \}/*-------------------------------------------------------------*/#define ACTIONS_SBB(DATA_BITS,DATA_UTYPE) \{ \ PREAMBLE(DATA_BITS); \ { Int cf, pf, af, zf, sf, of; \ Int argL, argR, oldC, res; \ oldC = CC_NDEP & X86G_CC_MASK_C; \ argL = CC_DEP1; \ argR = CC_DEP2 ^ oldC; \ res = (argL - argR) - oldC; \ if (oldC) \ cf = (DATA_UTYPE)argL <= (DATA_UTYPE)argR; \ else \ cf = (DATA_UTYPE)argL < (DATA_UTYPE)argR; \ pf = parity_table[(UChar)res]; \ af = (res ^ argL ^ argR) & 0x10; \ zf = ((DATA_UTYPE)res == 0) << 6; \ sf = lshift(res, 8 - DATA_BITS) & 0x80; \ of = lshift((argL ^ argR) & (argL ^ res), \ 12 - DATA_BITS) & X86G_CC_MASK_O; \ return cf | pf | af | zf | sf | of; \ } \}/*-------------------------------------------------------------*/#define ACTIONS_LOGIC(DATA_BITS,DATA_UTYPE) \{ \ PREAMBLE(DATA_BITS); \ { Int cf, pf, af, zf, sf, of; \ cf = 0; \ pf = parity_table[(UChar)CC_DEP1]; \ af = 0; \ zf = ((DATA_UTYPE)CC_DEP1 == 0) << 6; \ sf = lshift(CC_DEP1, 8 - DATA_BITS) & 0x80; \ of = 0; \ return cf | pf | af | zf | sf | of; \ } \}/*-------------------------------------------------------------*/#define ACTIONS_INC(DATA_BITS,DATA_UTYPE) \{ \ PREAMBLE(DATA_BITS); \ { Int cf, pf, af, zf, sf, of; \ Int argL, argR, res; \ res = CC_DEP1; \ argL = res - 1; \ argR = 1; \ cf = CC_NDEP & X86G_CC_MASK_C; \ pf = parity_table[(UChar)res]; \ af = (res ^ argL ^ argR) & 0x10; \ zf = ((DATA_UTYPE)res == 0) << 6; \ sf = lshift(res, 8 - DATA_BITS) & 0x80; \ of = ((res & DATA_MASK) == SIGN_MASK) << 11; \ return cf | pf | af | zf | sf | of; \ } \}/*-------------------------------------------------------------*/#define ACTIONS_DEC(DATA_BITS,DATA_UTYPE) \{ \ PREAMBLE(DATA_BITS); \ { Int cf, pf, af, zf, sf, of; \ Int argL, argR, res; \ res = CC_DEP1; \ argL = res + 1; \ argR = 1; \ cf = CC_NDEP & X86G_CC_MASK_C; \ pf = parity_table[(UChar)res]; \ af = (res ^ argL ^ argR) & 0x10; \ zf = ((DATA_UTYPE)res == 0) << 6; \ sf = lshift(res, 8 - DATA_BITS) & 0x80; \ of = ((res & DATA_MASK) \ == ((UInt)SIGN_MASK - 1)) << 11; \ return cf | pf | af | zf | sf | of; \ } \}/*-------------------------------------------------------------*/#define ACTIONS_SHL(DATA_BITS,DATA_UTYPE) \{ \ PREAMBLE(DATA_BITS); \ { Int cf, pf, af, zf, sf, of; \ cf = (CC_DEP2 >> (DATA_BITS - 1)) & X86G_CC_MASK_C; \ pf = parity_table[(UChar)CC_DEP1]; \ af = 0; /* undefined */ \ zf = ((DATA_UTYPE)CC_DEP1 == 0) << 6; \ sf = lshift(CC_DEP1, 8 - DATA_BITS) & 0x80; \ /* of is defined if shift count == 1 */ \ of = lshift(CC_DEP2 ^ CC_DEP1, 12 - DATA_BITS) \ & X86G_CC_MASK_O; \ return cf | pf | af | zf | sf | of; \ } \}/*-------------------------------------------------------------*/#define ACTIONS_SHR(DATA_BITS,DATA_UTYPE) \{ \ PREAMBLE(DATA_BITS); \ { Int cf, pf, af, zf, sf, of; \ cf = CC_DEP2 & 1; \ pf = parity_table[(UChar)CC_DEP1]; \ af = 0; /* undefined */ \ zf = ((DATA_UTYPE)CC_DEP1 == 0) << 6; \ sf = lshift(CC_DEP1, 8 - DATA_BITS) & 0x80; \ /* of is defined if shift count == 1 */ \ of = lshift(CC_DEP2 ^ CC_DEP1, 12 - DATA_BITS) \ & X86G_CC_MASK_O; \ return cf | pf | af | zf | sf | of; \ } \}/*-------------------------------------------------------------*//* ROL: cf' = lsb(result). of' = msb(result) ^ lsb(result). *//* DEP1 = result, NDEP = old flags */#define ACTIONS_ROL(DATA_BITS,DATA_UTYPE) \{ \ PREAMBLE(DATA_BITS); \ { Int fl \ = (CC_NDEP & ~(X86G_CC_MASK_O | X86G_CC_MASK_C)) \ | (X86G_CC_MASK_C & CC_DEP1) \ | (X86G_CC_MASK_O & (lshift(CC_DEP1, \ 11-(DATA_BITS-1)) \ ^ lshift(CC_DEP1, 11))); \ return fl; \ } \}/*-------------------------------------------------------------*//* ROR: cf' = msb(result). of' = msb(result) ^ msb-1(result). *//* DEP1 = result, NDEP = old flags */#define ACTIONS_ROR(DATA_BITS,DATA_UTYPE) \{ \ PREAMBLE(DATA_BITS); \ { Int fl \ = (CC_NDEP & ~(X86G_CC_MASK_O | X86G_CC_MASK_C)) \ | (X86G_CC_MASK_C & (CC_DEP1 >> (DATA_BITS-1))) \ | (X86G_CC_MASK_O & (lshift(CC_DEP1, \ 11-(DATA_BITS-1)) \ ^ lshift(CC_DEP1, 11-(DATA_BITS-1)+1))); \ return fl; \ } \}/*-------------------------------------------------------------*/#define ACTIONS_UMUL(DATA_BITS, DATA_UTYPE, NARROWtoU, \ DATA_U2TYPE, NARROWto2U) \{ \ PREAMBLE(DATA_BITS); \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -