📄 isel.c
字号:
/*---------------------------------------------------------------*//*--- ---*//*--- This file (host-ppc/isel.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_ir.h"#include "libvex.h"#include "ir/irmatch.h"#include "main/vex_util.h"#include "main/vex_globals.h"#include "host-generic/h_generic_regs.h"#include "host-ppc/hdefs.h"/* GPR register class for ppc32/64 */#define HRcGPR(__mode64) (__mode64 ? HRcInt64 : HRcInt32)/*---------------------------------------------------------*//*--- Register Usage Conventions ---*//*---------------------------------------------------------*//* Integer Regs ------------ GPR0 Reserved GPR1 Stack Pointer GPR2 not used - TOC pointer GPR3:10 Allocateable GPR11 if mode64: not used - calls by ptr / env ptr for some langs GPR12 if mode64: not used - exceptions / global linkage code GPR13 not used - Thread-specific pointer GPR14:28 Allocateable GPR29 Unused by us (reserved for the dispatcher) GPR30 AltiVec temp spill register GPR31 GuestStatePointer Of Allocateable regs: if (mode64) GPR3:10 Caller-saved regs else GPR3:12 Caller-saved regs GPR14:29 Callee-saved regs GPR3 [Return | Parameter] - carrying reg GPR4:10 Parameter-carrying regs Floating Point Regs ------------------- FPR0:31 Allocateable FPR0 Caller-saved - scratch reg if (mode64) FPR1:13 Caller-saved - param & return regs else FPR1:8 Caller-saved - param & return regs FPR9:13 Caller-saved regs FPR14:31 Callee-saved regs Vector Regs (on processors with the VMX feature) ----------- VR0-VR1 Volatile scratch registers VR2-VR13 Volatile vector parameters registers VR14-VR19 Volatile scratch registers VR20-VR31 Non-volatile registers VRSAVE Non-volatile 32-bit register*//*---------------------------------------------------------*//*--- PPC FP Status & Control Register Conventions ---*//*---------------------------------------------------------*//* Vex-generated code expects to run with the FPU set as follows: all exceptions masked. The rounding mode is set appropriately before each floating point insn emitted (or left unchanged if known to be correct already). There are a few fp insns (fmr,fneg,fabs,fnabs), which are unaffected by the rm and so the rounding mode is not set prior to them. At least on MPC7447A (Mac Mini), frsqrte is also not affected by rounding mode. At some point the ppc docs get sufficiently vague that the only way to find out is to write test programs.*//* Notes on the FP instruction set, 6 Feb 06.What exns -> CR1 ? Sets FPRF ? Observes RM ?-------------------------------------------------------------fmr[.] if . n nfneg[.] if . n nfabs[.] if . n nfnabs[.] if . n nfadd[.] if . y yfadds[.] if . y yfcfid[.] (i64->dbl) if . y yfcmpo (cmp, result n n nfcmpu to crfD) n n nfctid[.] (dbl->i64) if . ->undef yfctidz[.] (dbl->i64) if . ->undef rounds-to-zerofctiw[.] (dbl->i32) if . ->undef yfctiwz[.] (dbl->i32) if . ->undef rounds-to-zerofdiv[.] if . y yfdivs[.] if . y yfmadd[.] if . y yfmadds[.] if . y yfmsub[.] if . y yfmsubs[.] if . y yfmul[.] if . y yfmuls[.] if . y y(note: for fnm*, rounding happens before final negation)fnmadd[.] if . y yfnmadds[.] if . y yfnmsub[.] if . y yfnmsubs[.] if . y yfre[.] if . y yfres[.] if . y yfrsqrte[.] if . y apparently notfsqrt[.] if . y yfsqrts[.] if . y yfsub[.] if . y yfsubs[.] if . y yfpscr: bits 30-31 (ibm) is RM 24-29 (ibm) are exnmasks/non-IEEE bit, all zero 15-19 (ibm) is FPRF: class, <, =, >, UNordppc fe(guest) makes fpscr read as all zeros except RM (and maybe FPRFin future) mcrfs - move fpscr field to CR fieldmtfsfi[.] - 4 bit imm moved to fpscr fieldmtfsf[.] - move frS[low 1/2] to fpscr but using 8-bit field maskmtfsb1[.] - set given fpscr bitmtfsb0[.] - clear given fpscr bitmffs[.] - move all fpscr to frD[low 1/2]For [.] presumably cr1 is set with exn summary bits, as per main FP insnsA single precision store truncates/denormalises the in-register value,but does not round it. This is so that flds followed by fsts isalways the identity.*//*---------------------------------------------------------*//*--- misc helpers ---*//*---------------------------------------------------------*//* These are duplicated in guest-ppc/toIR.c */static IRExpr* unop ( IROp op, IRExpr* a ){ return IRExpr_Unop(op, a);}static IRExpr* mkU32 ( UInt i ){ return IRExpr_Const(IRConst_U32(i));}static IRExpr* bind ( Int binder ){ return IRExpr_Binder(binder);}/*---------------------------------------------------------*//*--- ISelEnv ---*//*---------------------------------------------------------*//* This carries around: - A mapping from IRTemp to IRType, giving the type of any IRTemp we might encounter. This is computed before insn selection starts, and does not change. - A mapping from IRTemp to HReg. This tells the insn selector which virtual register(s) are associated with each IRTemp temporary. This is computed before insn selection starts, and does not change. We expect this mapping to map precisely the same set of IRTemps as the type mapping does. - vregmap holds the primary register for the IRTemp. - vregmapHI holds the secondary register for the IRTemp, if any is needed. That's only for Ity_I64 temps in 32 bit mode or Ity_I128 temps in 64-bit mode. - The name of the vreg in which we stash a copy of the link reg, so helper functions don't kill it. - The code array, that is, the insns selected so far. - A counter, for generating new virtual registers. - The host subarchitecture we are selecting insns for. This is set at the start and does not change. - A Bool to tell us if the host is 32 or 64bit. This is set at the start and does not change. - An IRExpr*, which may be NULL, holding the IR expression (an IRRoundingMode-encoded value) to which the FPU's rounding mode was most recently set. Setting to NULL is always safe. Used to avoid redundant settings of the FPU's rounding mode, as described in set_FPU_rounding_mode below.*/ typedef struct { IRTypeEnv* type_env; HReg* vregmap; HReg* vregmapHI; Int n_vregmap; HReg savedLR; HInstrArray* code; Int vreg_ctr; /* 27 Jan 06: Not currently used, but should be */ UInt hwcaps; Bool mode64; IRExpr* previous_rm; } ISelEnv; static HReg lookupIRTemp ( ISelEnv* env, IRTemp tmp ){ vassert(tmp >= 0); vassert(tmp < env->n_vregmap); return env->vregmap[tmp];}static void lookupIRTempPair ( HReg* vrHI, HReg* vrLO, ISelEnv* env, IRTemp tmp ){ vassert(!env->mode64); vassert(tmp >= 0); vassert(tmp < env->n_vregmap); vassert(env->vregmapHI[tmp] != INVALID_HREG); *vrLO = env->vregmap[tmp]; *vrHI = env->vregmapHI[tmp];}static void addInstr ( ISelEnv* env, PPCInstr* instr ){ addHInstr(env->code, instr); if (vex_traceflags & VEX_TRACE_VCODE) { ppPPCInstr(instr, env->mode64); vex_printf("\n"); }}static HReg newVRegI ( ISelEnv* env ){ HReg reg = mkHReg(env->vreg_ctr, HRcGPR(env->mode64), True/*virtual reg*/); env->vreg_ctr++; return reg;}static HReg newVRegF ( ISelEnv* env ){ HReg reg = mkHReg(env->vreg_ctr, HRcFlt64, True/*virtual reg*/); env->vreg_ctr++; return reg;}static HReg newVRegV ( ISelEnv* env ){ HReg reg = mkHReg(env->vreg_ctr, HRcVec128, True/*virtual reg*/); env->vreg_ctr++; return reg;}/*---------------------------------------------------------*//*--- ISEL: Forward declarations ---*//*---------------------------------------------------------*//* These are organised as iselXXX and iselXXX_wrk pairs. The iselXXX_wrk do the real work, but are not to be called directly. For each XXX, iselXXX calls its iselXXX_wrk counterpart, then checks that all returned registers are virtual. You should not call the _wrk version directly. 'Word' refers to the size of the native machine word, that is, 32-bit int in 32-bit mode and 64-bit int in 64-bit mode. '2Word' therefore refers to a double-width (64/128-bit) quantity in two integer registers.*//* 32-bit mode: compute an I8/I16/I32 into a GPR. 64-bit mode: compute an I8/I16/I32/I64 into a GPR. */static HReg iselWordExpr_R_wrk ( ISelEnv* env, IRExpr* e );static HReg iselWordExpr_R ( ISelEnv* env, IRExpr* e );/* 32-bit mode: Compute an I8/I16/I32 into a RH (reg-or-halfword-immediate). 64-bit mode: Compute an I8/I16/I32/I64 into a RH (reg-or-halfword-immediate). It's important to specify whether the immediate is to be regarded
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -