static_inst.cc revision 7094:4d878c4a0c2b
1/* 2 * Copyright (c) 2010 ARM Limited 3 * All rights reserved 4 * 5 * The license below extends only to copyright in the software and shall 6 * not be construed as granting a license to any other intellectual 7 * property including but not limited to intellectual property relating 8 * to a hardware implementation of the functionality of the software 9 * licensed hereunder. You may use the software subject to the license 10 * terms below provided that you ensure that this notice is replicated 11 * unmodified and in its entirety in all distributions of the software, 12 * modified or unmodified, in source code or in binary form. 13 * 14 * Copyright (c) 2007-2008 The Florida State University 15 * All rights reserved. 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions are 19 * met: redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer; 21 * redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution; 24 * neither the name of the copyright holders nor the names of its 25 * contributors may be used to endorse or promote products derived from 26 * this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 39 * 40 * Authors: Stephen Hines 41 */ 42 43#include "arch/arm/faults.hh" 44#include "arch/arm/insts/static_inst.hh" 45#include "base/condcodes.hh" 46#include "base/cprintf.hh" 47#include "base/loader/symtab.hh" 48 49namespace ArmISA 50{ 51// Shift Rm by an immediate value 52int32_t 53ArmStaticInstBase::shift_rm_imm(uint32_t base, uint32_t shamt, 54 uint32_t type, uint32_t cfval) const 55{ 56 assert(shamt < 32); 57 ArmShiftType shiftType; 58 shiftType = (ArmShiftType)type; 59 60 switch (shiftType) 61 { 62 case LSL: 63 return base << shamt; 64 case LSR: 65 if (shamt == 0) 66 return 0; 67 else 68 return base >> shamt; 69 case ASR: 70 if (shamt == 0) 71 return (int32_t)base >> 31; 72 else 73 return (int32_t)base >> shamt; 74 case ROR: 75 if (shamt == 0) 76 return (cfval << 31) | (base >> 1); // RRX 77 else 78 return (base << (32 - shamt)) | (base >> shamt); 79 default: 80 ccprintf(std::cerr, "Unhandled shift type\n"); 81 exit(1); 82 break; 83 } 84 return 0; 85} 86 87// Shift Rm by Rs 88int32_t 89ArmStaticInstBase::shift_rm_rs(uint32_t base, uint32_t shamt, 90 uint32_t type, uint32_t cfval) const 91{ 92 enum ArmShiftType shiftType; 93 shiftType = (enum ArmShiftType) type; 94 95 switch (shiftType) 96 { 97 case LSL: 98 if (shamt >= 32) 99 return 0; 100 else 101 return base << shamt; 102 case LSR: 103 if (shamt >= 32) 104 return 0; 105 else 106 return base >> shamt; 107 case ASR: 108 if (shamt >= 32) 109 return (int32_t)base >> 31; 110 else 111 return (int32_t)base >> shamt; 112 case ROR: 113 shamt = shamt & 0x1f; 114 if (shamt == 0) 115 return base; 116 else 117 return (base << (32 - shamt)) | (base >> shamt); 118 default: 119 ccprintf(std::cerr, "Unhandled shift type\n"); 120 exit(1); 121 break; 122 } 123 return 0; 124} 125 126 127// Generate C for a shift by immediate 128bool 129ArmStaticInstBase::shift_carry_imm(uint32_t base, uint32_t shamt, 130 uint32_t type, uint32_t cfval) const 131{ 132 enum ArmShiftType shiftType; 133 shiftType = (enum ArmShiftType) type; 134 135 switch (shiftType) 136 { 137 case LSL: 138 if (shamt == 0) 139 return cfval; 140 else 141 return (base >> (32 - shamt)) & 1; 142 case LSR: 143 if (shamt == 0) 144 return (base >> 31); 145 else 146 return (base >> (shamt - 1)) & 1; 147 case ASR: 148 if (shamt == 0) 149 return (base >> 31); 150 else 151 return (base >> (shamt - 1)) & 1; 152 case ROR: 153 shamt = shamt & 0x1f; 154 if (shamt == 0) 155 return (base & 1); // RRX 156 else 157 return (base >> (shamt - 1)) & 1; 158 default: 159 ccprintf(std::cerr, "Unhandled shift type\n"); 160 exit(1); 161 break; 162 } 163 return 0; 164} 165 166 167// Generate C for a shift by Rs 168bool 169ArmStaticInstBase::shift_carry_rs(uint32_t base, uint32_t shamt, 170 uint32_t type, uint32_t cfval) const 171{ 172 enum ArmShiftType shiftType; 173 shiftType = (enum ArmShiftType) type; 174 175 if (shamt == 0) 176 return cfval; 177 178 switch (shiftType) 179 { 180 case LSL: 181 if (shamt > 32) 182 return 0; 183 else 184 return (base >> (32 - shamt)) & 1; 185 case LSR: 186 if (shamt > 32) 187 return 0; 188 else 189 return (base >> (shamt - 1)) & 1; 190 case ASR: 191 if (shamt > 32) 192 shamt = 32; 193 return (base >> (shamt - 1)) & 1; 194 case ROR: 195 shamt = shamt & 0x1f; 196 if (shamt == 0) 197 shamt = 32; 198 return (base >> (shamt - 1)) & 1; 199 default: 200 ccprintf(std::cerr, "Unhandled shift type\n"); 201 exit(1); 202 break; 203 } 204 return 0; 205} 206 207 208// Generate the appropriate carry bit for an addition operation 209bool 210ArmStaticInstBase::arm_add_carry(int32_t result, int32_t lhs, int32_t rhs) const 211{ 212 return findCarry(32, result, lhs, rhs); 213} 214 215// Generate the appropriate carry bit for a subtraction operation 216bool 217ArmStaticInstBase::arm_sub_carry(int32_t result, int32_t lhs, int32_t rhs) const 218{ 219 return findCarry(32, result, lhs, ~rhs); 220} 221 222bool 223ArmStaticInstBase::arm_add_overflow(int32_t result, int32_t lhs, int32_t rhs) const 224{ 225 return findOverflow(32, result, lhs, rhs); 226} 227 228bool 229ArmStaticInstBase::arm_sub_overflow(int32_t result, int32_t lhs, int32_t rhs) const 230{ 231 return findOverflow(32, result, lhs, ~rhs); 232} 233 234void 235ArmStaticInstBase::printReg(std::ostream &os, int reg) const 236{ 237 if (reg < FP_Base_DepTag) { 238 switch (reg) { 239 case PCReg: 240 ccprintf(os, "pc"); 241 break; 242 case StackPointerReg: 243 ccprintf(os, "sp"); 244 break; 245 case FramePointerReg: 246 ccprintf(os, "fp"); 247 break; 248 case ReturnAddressReg: 249 ccprintf(os, "lr"); 250 break; 251 default: 252 ccprintf(os, "r%d", reg); 253 break; 254 } 255 } else if (reg < Ctrl_Base_DepTag) { 256 ccprintf(os, "f%d", reg - FP_Base_DepTag); 257 } else { 258 reg -= Ctrl_Base_DepTag; 259 assert(reg < NUM_MISCREGS); 260 ccprintf(os, "%s", ArmISA::miscRegName[reg]); 261 } 262} 263 264void 265ArmStaticInstBase::printMnemonic(std::ostream &os, 266 const std::string &suffix, 267 bool withPred) const 268{ 269 os << " " << mnemonic; 270 if (withPred) { 271 unsigned condCode = machInst.condCode; 272 switch (condCode) { 273 case COND_EQ: 274 os << "eq"; 275 break; 276 case COND_NE: 277 os << "ne"; 278 break; 279 case COND_CS: 280 os << "cs"; 281 break; 282 case COND_CC: 283 os << "cc"; 284 break; 285 case COND_MI: 286 os << "mi"; 287 break; 288 case COND_PL: 289 os << "pl"; 290 break; 291 case COND_VS: 292 os << "vs"; 293 break; 294 case COND_VC: 295 os << "vc"; 296 break; 297 case COND_HI: 298 os << "hi"; 299 break; 300 case COND_LS: 301 os << "ls"; 302 break; 303 case COND_GE: 304 os << "ge"; 305 break; 306 case COND_LT: 307 os << "lt"; 308 break; 309 case COND_GT: 310 os << "gt"; 311 break; 312 case COND_LE: 313 os << "le"; 314 break; 315 case COND_AL: 316 // This one is implicit. 317 break; 318 case COND_NV: 319 os << "nv"; 320 break; 321 default: 322 panic("Unrecognized condition code %d.\n", condCode); 323 } 324 os << suffix << " "; 325 } 326} 327 328void 329ArmStaticInstBase::printMemSymbol(std::ostream &os, 330 const SymbolTable *symtab, 331 const std::string &prefix, 332 const Addr addr, 333 const std::string &suffix) const 334{ 335 Addr symbolAddr; 336 std::string symbol; 337 if (symtab && symtab->findNearestSymbol(addr, symbol, symbolAddr)) { 338 ccprintf(os, "%s%s", prefix, symbol); 339 if (symbolAddr != addr) 340 ccprintf(os, "+%d", addr - symbolAddr); 341 ccprintf(os, suffix); 342 } 343} 344 345void 346ArmStaticInstBase::printShiftOperand(std::ostream &os) const 347{ 348 printReg(os, machInst.rm); 349 350 bool immShift = (machInst.opcode4 == 0); 351 bool done = false; 352 unsigned shiftAmt = (machInst.shiftSize); 353 ArmShiftType type = (ArmShiftType)(uint32_t)machInst.shift; 354 355 if ((type == LSR || type == ASR) && immShift && shiftAmt == 0) 356 shiftAmt = 32; 357 358 switch (type) { 359 case LSL: 360 if (immShift && shiftAmt == 0) { 361 done = true; 362 break; 363 } 364 os << ", LSL"; 365 break; 366 case LSR: 367 os << ", LSR"; 368 break; 369 case ASR: 370 os << ", ASR"; 371 break; 372 case ROR: 373 if (immShift && shiftAmt == 0) { 374 os << ", RRX"; 375 done = true; 376 break; 377 } 378 os << ", ROR"; 379 break; 380 default: 381 panic("Tried to disassemble unrecognized shift type.\n"); 382 } 383 if (!done) { 384 os << " "; 385 if (immShift) 386 os << "#" << shiftAmt; 387 else 388 printReg(os, machInst.rs); 389 } 390} 391 392void 393ArmStaticInstBase::printDataInst(std::ostream &os, bool withImm) const 394{ 395 printMnemonic(os, machInst.sField ? "s" : ""); 396 //XXX It would be nice if the decoder figured this all out for us. 397 unsigned opcode = machInst.opcode; 398 bool firstOp = true; 399 400 // Destination 401 // Cmp, cmn, teq, and tst don't have one. 402 if (opcode < 8 || opcode > 0xb) { 403 firstOp = false; 404 printReg(os, machInst.rd); 405 } 406 407 // Source 1. 408 // Mov and Movn don't have one of these. 409 if (opcode != 0xd && opcode != 0xf) { 410 if (!firstOp) 411 os << ", "; 412 firstOp = false; 413 printReg(os, machInst.rn); 414 } 415 416 if (!firstOp) 417 os << ", "; 418 if (withImm) { 419 unsigned rotate = machInst.rotate * 2; 420 uint32_t imm = machInst.imm; 421 ccprintf(os, "#%#x", (imm << (32 - rotate)) | (imm >> rotate)); 422 } else { 423 printShiftOperand(os); 424 } 425} 426 427std::string 428ArmStaticInstBase::generateDisassembly(Addr pc, 429 const SymbolTable *symtab) const 430{ 431 std::stringstream ss; 432 printMnemonic(ss); 433 return ss.str(); 434} 435} 436