00001 #ifndef _Instruction_h_ 00002 #define _Instruction_h_ 00003 00004 #include <Base.h> 00005 #include <PowerPCInstruct.h> 00006 #include <BitSet.h> 00007 00008 class Operand { 00009 protected: 00010 const static uint32_t IntegerType = 0x1; 00011 const static uint32_t GPRegisterType = 0x2; 00012 00013 uint32_t flags; 00014 union { 00015 int32_t iconst; 00016 uint32_t regist; 00017 } value; 00018 00019 Operand(uint32_t flgs) : flags(flgs) {} 00020 Operand() : flags(0) {} 00021 00022 public: 00023 ~Operand() {} 00024 bool isInteger() { return (flags & IntegerType); } 00025 bool isGPRegister() { return (flags & GPRegisterType); } 00026 void print() { 00027 if(isInteger()){ 00028 PRINT_INFOR("\t\t(INT %d)",value.iconst); 00029 } else if(isGPRegister()){ 00030 PRINT_INFOR("\t\t(GPR %2d)",value.regist); 00031 } 00032 } 00033 int32_t getIconst() { return value.iconst; } 00034 uint32_t getRegister() { return value.regist; } 00035 00036 static Operand* GPRegisterOperands; 00037 static Operand IntegerOperand0; 00038 00039 static Operand* initGPRegisterOperands(); 00040 }; 00041 00042 class IntegerOperand : public Operand { 00043 protected: 00044 public: 00045 IntegerOperand(int32_t val) : Operand(Operand::IntegerType){ 00046 value.iconst = val; 00047 } 00048 ~IntegerOperand() {} 00049 }; 00050 00051 class GPRegisterOperand : public Operand { 00052 protected: 00053 public: 00054 GPRegisterOperand(uint32_t val) : Operand(Operand::GPRegisterType){ 00055 value.regist = val; 00056 } 00057 ~GPRegisterOperand() {} 00058 }; 00059 00060 class Instruction { 00061 protected: 00062 00063 static BitSet<>* memoryOperationXops; 00064 PowerPCInstruction ppci; 00065 public: 00066 00067 Instruction() { ppci.bits = 0; } 00068 Instruction(uint32_t bits) { ppci.bits = bits; } 00069 ~Instruction() {} 00070 00071 bool isZero() { return !(ppci.bits); } 00072 uint32_t bits() { return ppci.bits; } 00073 00074 bool isCondBranch(); 00075 bool isJump(); 00076 bool isReturn(); 00077 bool isCondReturn(); 00078 bool isCall(); 00079 bool isIndirectJump(); 00080 bool isIndirectJumpCtr(); 00081 bool isIndirectJumpLnk(); 00082 bool isOtherBranch(); 00083 bool isAddBeforeJump(); 00084 bool isLoadBeforeJump(); 00085 00086 bool definesLeaders(){ 00087 return (isCondBranch() || isJump() || isReturn() || isCall() || isIndirectJump()); 00088 } 00089 bool hasTargetAddress() { 00090 return (isCondBranch() || isJump()); 00091 } 00092 00093 uint64_t getTargetAddress(uint64_t insnAddr); 00094 00095 bool definesJTEntryCount(); 00096 uint32_t getJTEntryCount(); 00097 00098 bool definesJTBaseAddress(); 00099 int32_t getJTBaseOffsetTOC(); 00100 00101 bool definesJTBaseAddrIndir(); 00102 int32_t getJTBaseAddrIndirOffset(); 00103 00104 void print(uint64_t insnAddr,bool is64Bit); 00105 static void print(char* buffer,uint64_t baseAddress,uint32_t sizeInBytes,bool is64Bit); 00106 00107 uint32_t getJTBaseAddrTarget(); 00108 uint32_t getJTBaseAddrIndirSrc(); 00109 00110 uint32_t getLoadBeforeJumpSrc1(); 00111 00112 bool isMemoryDForm(); 00113 bool isMemoryDsForm(); 00114 bool isMemoryXForm(); 00115 bool isMemoryOperation() { return (isMemoryDForm() || isMemoryDsForm() || isMemoryXForm()); } 00116 bool isUnhandledMemoryOp(); 00117 00118 bool isFloatAForm(); 00119 bool isFloatXForm(); 00120 bool isFloatPOperation() { return (isFloatAForm() || isFloatXForm()); } 00121 00122 static BitSet<>* initMemoryOperationXops(); 00123 00124 uint32_t getDFormSrc1(); 00125 uint32_t getDFormTgt(); 00126 int32_t getDFormImmediate(); 00127 int32_t getDsFormImmediate(); 00128 uint32_t getXFormSrc1(); 00129 uint32_t getXFormSrc2(); 00130 bool isMemoryXFormButNoSrc2(); 00131 00132 static bool isInJumpInsnRange(uint64_t from,uint64_t to); 00133 static bool isInLoadOffsetInsnRange(int32_t value); 00134 00135 static Instruction generateCompare(uint32_t reg1,uint32_t reg2,uint32_t field); 00136 00137 static Instruction generateJumpInsn(uint64_t from,uint64_t to); 00138 static Instruction generateCallToCTR(); 00139 static Instruction generateReturnToLnk(); 00140 static Instruction generateCallToImmediate(uint64_t from,uint64_t to); 00141 static Instruction generateCondBranch(uint32_t field,uint32_t op,uint32_t tf,int32_t distance); 00142 00143 static Instruction generateMoveReg(uint32_t from,uint32_t to); 00144 00145 static Instruction multiplyImmediate(uint32_t tgt,uint32_t src,int32_t imm); 00146 00147 static Instruction generateAdd(uint32_t tgt,uint32_t src1,uint32_t src2); 00148 static Instruction generateAddImm(uint32_t tgt,uint32_t src,int32_t value); 00149 static Instruction generateIncrement(uint32_t reg,int32_t value); 00150 static Instruction generateLoadImmediate(uint32_t reg,int32_t value); 00151 static Instruction generateAddImmShifted(uint32_t tgt,uint32_t src,int32_t imm); 00152 static Instruction generateLoad32BitHigh(uint32_t reg,int32_t value); 00153 00154 static Instruction generateOrImm(uint32_t tgt,uint32_t src,int32_t imm); 00155 static Instruction generateLoad32BitLow(uint32_t reg,int32_t value); 00156 00157 static Instruction generateXorImm(uint32_t tgt,uint32_t src,int32_t imm); 00158 static Instruction generateXorImmShifted(uint32_t tgt,uint32_t src,int32_t imm); 00159 00160 static Instruction generateStoreDouble(uint32_t src,uint32_t base,int32_t offset); 00161 static Instruction generateLoadDouble(uint32_t tgt,uint32_t base,int32_t offset); 00162 static Instruction generateLoadWord(uint32_t tgt,uint32_t base,int32_t offset); 00163 static Instruction generateStoreWord(uint32_t src,uint32_t base,int32_t offset); 00164 static Instruction generateLoadWordIndx(uint32_t tgt,uint32_t base1,uint32_t base2); 00165 static Instruction generateStoreWordIndx(uint32_t src,uint32_t base1,uint32_t base2); 00166 static Instruction generateLoadDoubleIndx(uint32_t tgt,uint32_t base1,uint32_t base2); 00167 static Instruction generateStoreDoubleIndx(uint32_t src,uint32_t base1,uint32_t base2); 00168 00169 static Instruction generateStoreDoubleFloat(uint32_t src,uint32_t base,int32_t offset); 00170 static Instruction generateLoadDoubleFloat(uint32_t tgt,uint32_t base,int32_t offset); 00171 static Instruction generateStoreWordFloat(uint32_t src,uint32_t base,int32_t offset); 00172 static Instruction generateLoadWordFloat(uint32_t tgt,uint32_t base,int32_t offset); 00173 00174 static Instruction generateMoveToSPR(uint32_t tgt,uint32_t regcode); 00175 static Instruction generateMoveFromSPR(uint32_t tgt,uint32_t regcode); 00176 static Instruction generateMoveFromCR(uint32_t tgt); 00177 static Instruction generateMoveToCR(uint32_t src); 00178 static Instruction generateMoveFromFPSCR(uint32_t tgt); 00179 static Instruction generateMoveToFPSCR(uint32_t src); 00180 00181 static Instruction generateSPIncrementWord(int32_t offset); 00182 static Instruction generateSPIncrementDouble(int32_t offset); 00183 00184 static Instruction generateAnd(uint32_t tgt,uint32_t src1,uint32_t src2); 00185 00186 bool isMemoryDFormFloat(); 00187 }; 00188 00189 #endif