static_inst.hh revision 7219:0c995c5f8245
111807Snikos.nikoleris@arm.com/* 211247Sradhika.jagtap@ARM.com * Copyright (c) 2010 ARM Limited 311247Sradhika.jagtap@ARM.com * All rights reserved 411247Sradhika.jagtap@ARM.com * 511247Sradhika.jagtap@ARM.com * The license below extends only to copyright in the software and shall 611247Sradhika.jagtap@ARM.com * not be construed as granting a license to any other intellectual 711247Sradhika.jagtap@ARM.com * property including but not limited to intellectual property relating 811247Sradhika.jagtap@ARM.com * to a hardware implementation of the functionality of the software 911247Sradhika.jagtap@ARM.com * licensed hereunder. You may use the software subject to the license 1011247Sradhika.jagtap@ARM.com * terms below provided that you ensure that this notice is replicated 1111247Sradhika.jagtap@ARM.com * unmodified and in its entirety in all distributions of the software, 1211247Sradhika.jagtap@ARM.com * modified or unmodified, in source code or in binary form. 1311247Sradhika.jagtap@ARM.com * 1411247Sradhika.jagtap@ARM.com * Copyright (c) 2007-2008 The Florida State University 1511247Sradhika.jagtap@ARM.com * All rights reserved. 1611247Sradhika.jagtap@ARM.com * 1711247Sradhika.jagtap@ARM.com * Redistribution and use in source and binary forms, with or without 1811247Sradhika.jagtap@ARM.com * modification, are permitted provided that the following conditions are 1911247Sradhika.jagtap@ARM.com * met: redistributions of source code must retain the above copyright 2011247Sradhika.jagtap@ARM.com * notice, this list of conditions and the following disclaimer; 2111247Sradhika.jagtap@ARM.com * redistributions in binary form must reproduce the above copyright 2211247Sradhika.jagtap@ARM.com * notice, this list of conditions and the following disclaimer in the 2311247Sradhika.jagtap@ARM.com * documentation and/or other materials provided with the distribution; 2411247Sradhika.jagtap@ARM.com * neither the name of the copyright holders nor the names of its 2511247Sradhika.jagtap@ARM.com * contributors may be used to endorse or promote products derived from 2611247Sradhika.jagtap@ARM.com * this software without specific prior written permission. 2711247Sradhika.jagtap@ARM.com * 2811247Sradhika.jagtap@ARM.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2911247Sradhika.jagtap@ARM.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 3011247Sradhika.jagtap@ARM.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 3111247Sradhika.jagtap@ARM.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 3211247Sradhika.jagtap@ARM.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3311247Sradhika.jagtap@ARM.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 3411247Sradhika.jagtap@ARM.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3511247Sradhika.jagtap@ARM.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3611247Sradhika.jagtap@ARM.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3711247Sradhika.jagtap@ARM.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3811807Snikos.nikoleris@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3911807Snikos.nikoleris@arm.com * 4011247Sradhika.jagtap@ARM.com * Authors: Stephen Hines 4111247Sradhika.jagtap@ARM.com */ 4211247Sradhika.jagtap@ARM.com#ifndef __ARCH_ARM_INSTS_STATICINST_HH__ 4311247Sradhika.jagtap@ARM.com#define __ARCH_ARM_INSTS_STATICINST_HH__ 4411247Sradhika.jagtap@ARM.com 4511247Sradhika.jagtap@ARM.com#include "base/trace.hh" 4611247Sradhika.jagtap@ARM.com#include "cpu/static_inst.hh" 4711247Sradhika.jagtap@ARM.com 4811247Sradhika.jagtap@ARM.comnamespace ArmISA 4911247Sradhika.jagtap@ARM.com{ 5011247Sradhika.jagtap@ARM.comclass ArmStaticInst : public StaticInst 5111247Sradhika.jagtap@ARM.com{ 5211247Sradhika.jagtap@ARM.com protected: 5311247Sradhika.jagtap@ARM.com int32_t shift_rm_imm(uint32_t base, uint32_t shamt, 5411247Sradhika.jagtap@ARM.com uint32_t type, uint32_t cfval) const; 5511252Sradhika.jagtap@ARM.com int32_t shift_rm_rs(uint32_t base, uint32_t shamt, 5611252Sradhika.jagtap@ARM.com uint32_t type, uint32_t cfval) const; 5711252Sradhika.jagtap@ARM.com 5811252Sradhika.jagtap@ARM.com bool shift_carry_imm(uint32_t base, uint32_t shamt, 5911252Sradhika.jagtap@ARM.com uint32_t type, uint32_t cfval) const; 6011252Sradhika.jagtap@ARM.com bool shift_carry_rs(uint32_t base, uint32_t shamt, 6111252Sradhika.jagtap@ARM.com uint32_t type, uint32_t cfval) const; 6211252Sradhika.jagtap@ARM.com 6311252Sradhika.jagtap@ARM.com template<int width> 6411252Sradhika.jagtap@ARM.com static bool 6511247Sradhika.jagtap@ARM.com saturateOp(int32_t &res, int64_t op1, int64_t op2, bool sub=false) 6611252Sradhika.jagtap@ARM.com { 6711252Sradhika.jagtap@ARM.com int64_t midRes = sub ? (op1 - op2) : (op1 + op2); 6811252Sradhika.jagtap@ARM.com if (bits(midRes, width) != bits(midRes, width - 1)) { 6911252Sradhika.jagtap@ARM.com if (midRes > 0) 7011252Sradhika.jagtap@ARM.com res = (1LL << (width - 1)) - 1; 7111252Sradhika.jagtap@ARM.com else 7211247Sradhika.jagtap@ARM.com res = -(1LL << (width - 1)); 7311252Sradhika.jagtap@ARM.com return true; 7411253Sradhika.jagtap@ARM.com } else { 7511252Sradhika.jagtap@ARM.com res = midRes; 7611252Sradhika.jagtap@ARM.com return false; 7711252Sradhika.jagtap@ARM.com } 7811252Sradhika.jagtap@ARM.com } 7911252Sradhika.jagtap@ARM.com 8011252Sradhika.jagtap@ARM.com template<int width> 8111252Sradhika.jagtap@ARM.com static bool 8211253Sradhika.jagtap@ARM.com uSaturateOp(uint32_t &res, int64_t op1, int64_t op2, bool sub=false) 8311253Sradhika.jagtap@ARM.com { 8411252Sradhika.jagtap@ARM.com int64_t midRes = sub ? (op1 - op2) : (op1 + op2); 85 if (midRes >= (1 << width)) { 86 res = (1 << width) - 1; 87 return true; 88 } else if (midRes < 0) { 89 res = 0; 90 return true; 91 } else { 92 res = midRes; 93 return false; 94 } 95 } 96 97 // Constructor 98 ArmStaticInst(const char *mnem, ExtMachInst _machInst, 99 OpClass __opClass) 100 : StaticInst(mnem, _machInst, __opClass) 101 { 102 } 103 104 /// Print a register name for disassembly given the unique 105 /// dependence tag number (FP or int). 106 void printReg(std::ostream &os, int reg) const; 107 void printMnemonic(std::ostream &os, 108 const std::string &suffix = "", 109 bool withPred = true) const; 110 void printMemSymbol(std::ostream &os, const SymbolTable *symtab, 111 const std::string &prefix, const Addr addr, 112 const std::string &suffix) const; 113 void printShiftOperand(std::ostream &os, IntRegIndex rm, 114 bool immShift, uint32_t shiftAmt, 115 IntRegIndex rs, ArmShiftType type) const; 116 117 118 void printDataInst(std::ostream &os, bool withImm) const; 119 void printDataInst(std::ostream &os, bool withImm, bool immShift, bool s, 120 IntRegIndex rd, IntRegIndex rn, IntRegIndex rm, 121 IntRegIndex rs, uint32_t shiftAmt, ArmShiftType type, 122 uint32_t imm) const; 123 124 std::string generateDisassembly(Addr pc, const SymbolTable *symtab) const; 125 126 static uint32_t 127 cpsrWriteByInstr(CPSR cpsr, uint32_t val, 128 uint8_t byteMask, bool affectState) 129 { 130 bool privileged = (cpsr.mode != MODE_USER); 131 132 uint32_t bitMask = 0; 133 134 if (bits(byteMask, 3)) { 135 unsigned lowIdx = affectState ? 24 : 27; 136 bitMask = bitMask | mask(31, lowIdx); 137 } 138 if (bits(byteMask, 2)) { 139 bitMask = bitMask | mask(19, 16); 140 } 141 if (bits(byteMask, 1)) { 142 unsigned highIdx = affectState ? 15 : 9; 143 unsigned lowIdx = privileged ? 8 : 9; 144 bitMask = bitMask | mask(highIdx, lowIdx); 145 } 146 if (bits(byteMask, 0)) { 147 if (privileged) { 148 bitMask = bitMask | mask(7, 6); 149 bitMask = bitMask | mask(5); 150 } 151 if (affectState) 152 bitMask = bitMask | (1 << 5); 153 } 154 155 return ((uint32_t)cpsr & ~bitMask) | (val & bitMask); 156 } 157 158 static uint32_t 159 spsrWriteByInstr(uint32_t spsr, uint32_t val, 160 uint8_t byteMask, bool affectState) 161 { 162 uint32_t bitMask = 0; 163 164 if (bits(byteMask, 3)) 165 bitMask = bitMask | mask(31, 24); 166 if (bits(byteMask, 2)) 167 bitMask = bitMask | mask(19, 16); 168 if (bits(byteMask, 1)) 169 bitMask = bitMask | mask(15, 8); 170 if (bits(byteMask, 0)) 171 bitMask = bitMask | mask(7, 0); 172 173 return ((spsr & ~bitMask) | (val & bitMask)); 174 } 175 176 template<class XC> 177 static Addr 178 readPC(XC *xc) 179 { 180 Addr pc = xc->readPC(); 181 Addr tBit = pc & (ULL(1) << PcTBitShift); 182 if (tBit) 183 return pc + 4; 184 else 185 return pc + 8; 186 } 187 188 // Perform an regular branch. 189 template<class XC> 190 static void 191 setNextPC(XC *xc, Addr val) 192 { 193 xc->setNextPC((xc->readNextPC() & PcModeMask) | 194 (val & ~PcModeMask)); 195 } 196 197 // Perform an interworking branch. 198 template<class XC> 199 static void 200 setIWNextPC(XC *xc, Addr val) 201 { 202 Addr stateBits = xc->readPC() & PcModeMask; 203 Addr jBit = (ULL(1) << PcJBitShift); 204 Addr tBit = (ULL(1) << PcTBitShift); 205 bool thumbEE = (stateBits == (tBit | jBit)); 206 207 Addr newPc = (val & ~PcModeMask); 208 if (thumbEE) { 209 if (bits(newPc, 0)) { 210 warn("Bad thumbEE interworking branch address %#x.\n", newPc); 211 } else { 212 newPc = newPc & ~mask(1); 213 } 214 } else { 215 if (bits(newPc, 0)) { 216 stateBits = tBit; 217 newPc = newPc & ~mask(1); 218 } else if (!bits(newPc, 1)) { 219 stateBits = 0; 220 } else { 221 warn("Bad interworking branch address %#x.\n", newPc); 222 } 223 } 224 newPc = newPc | stateBits; 225 xc->setNextPC(newPc); 226 } 227 228 // Perform an interworking branch in ARM mode, a regular branch 229 // otherwise. 230 template<class XC> 231 static void 232 setAIWNextPC(XC *xc, Addr val) 233 { 234 Addr stateBits = xc->readPC() & PcModeMask; 235 Addr jBit = (ULL(1) << PcJBitShift); 236 Addr tBit = (ULL(1) << PcTBitShift); 237 if (!jBit && !tBit) { 238 setIWNextPC(xc, val); 239 } else { 240 setNextPC(xc, val); 241 } 242 } 243}; 244} 245 246#endif //__ARCH_ARM_INSTS_STATICINST_HH__ 247