static_inst.cc revision 5787:e3a6f53818fe
1/* 2 * Copyright (c) 2007 The Hewlett-Packard Development Company 3 * All rights reserved. 4 * 5 * Redistribution and use of this software in source and binary forms, 6 * with or without modification, are permitted provided that the 7 * following conditions are met: 8 * 9 * The software must be used only for Non-Commercial Use which means any 10 * use which is NOT directed to receiving any direct monetary 11 * compensation for, or commercial advantage from such use. Illustrative 12 * examples of non-commercial use are academic research, personal study, 13 * teaching, education and corporate research & development. 14 * Illustrative examples of commercial use are distributing products for 15 * commercial advantage and providing services using the software for 16 * commercial advantage. 17 * 18 * If you wish to use this software or functionality therein that may be 19 * covered by patents for commercial use, please contact: 20 * Director of Intellectual Property Licensing 21 * Office of Strategy and Technology 22 * Hewlett-Packard Company 23 * 1501 Page Mill Road 24 * Palo Alto, California 94304 25 * 26 * Redistributions of source code must retain the above copyright notice, 27 * this list of conditions and the following disclaimer. Redistributions 28 * in binary form must reproduce the above copyright notice, this list of 29 * conditions and the following disclaimer in the documentation and/or 30 * other materials provided with the distribution. Neither the name of 31 * the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its 32 * contributors may be used to endorse or promote products derived from 33 * this software without specific prior written permission. No right of 34 * sublicense is granted herewith. Derivatives of the software and 35 * output created using the software may be prepared, but only for 36 * Non-Commercial Uses. Derivatives of the software may be shared with 37 * others provided: (i) the others agree to abide by the list of 38 * conditions herein which includes the Non-Commercial Use restrictions; 39 * and (ii) such Derivatives of the software include the above copyright 40 * notice to acknowledge the contribution from this software where 41 * applicable, this list of conditions and the disclaimer below. 42 * 43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 54 * 55 * Authors: Gabe Black 56 */ 57 58#include "arch/x86/insts/static_inst.hh" 59#include "arch/x86/segmentregs.hh" 60 61namespace X86ISA 62{ 63 void X86StaticInst::printMnemonic(std::ostream &os, 64 const char * mnemonic) const 65 { 66 ccprintf(os, " %s ", mnemonic); 67 } 68 69 void X86StaticInst::printMnemonic(std::ostream &os, 70 const char * instMnemonic, const char * mnemonic) const 71 { 72 ccprintf(os, " %s : %s ", instMnemonic, mnemonic); 73 } 74 75 void X86StaticInst::printSegment(std::ostream &os, int segment) const 76 { 77 switch (segment) 78 { 79 case SEGMENT_REG_ES: 80 ccprintf(os, "ES"); 81 break; 82 case SEGMENT_REG_CS: 83 ccprintf(os, "CS"); 84 break; 85 case SEGMENT_REG_SS: 86 ccprintf(os, "SS"); 87 break; 88 case SEGMENT_REG_DS: 89 ccprintf(os, "DS"); 90 break; 91 case SEGMENT_REG_FS: 92 ccprintf(os, "FS"); 93 break; 94 case SEGMENT_REG_GS: 95 ccprintf(os, "GS"); 96 break; 97 case SEGMENT_REG_HS: 98 ccprintf(os, "HS"); 99 break; 100 case SEGMENT_REG_TSL: 101 ccprintf(os, "TSL"); 102 break; 103 case SEGMENT_REG_TSG: 104 ccprintf(os, "TSG"); 105 break; 106 case SEGMENT_REG_LS: 107 ccprintf(os, "LS"); 108 break; 109 case SEGMENT_REG_MS: 110 ccprintf(os, "MS"); 111 break; 112 case SYS_SEGMENT_REG_TR: 113 ccprintf(os, "TR"); 114 break; 115 case SYS_SEGMENT_REG_IDTR: 116 ccprintf(os, "IDTR"); 117 break; 118 default: 119 panic("Unrecognized segment %d\n", segment); 120 } 121 } 122 123 void 124 X86StaticInst::printSrcReg(std::ostream &os, int reg, int size) const 125 { 126 if(_numSrcRegs > reg) 127 printReg(os, _srcRegIdx[reg], size); 128 } 129 130 void 131 X86StaticInst::printDestReg(std::ostream &os, int reg, int size) const 132 { 133 if(_numDestRegs > reg) 134 printReg(os, _destRegIdx[reg], size); 135 } 136 137 void 138 X86StaticInst::printReg(std::ostream &os, int reg, int size) const 139 { 140 assert(size == 1 || size == 2 || size == 4 || size == 8); 141 static const char * abcdFormats[9] = 142 {"", "%s", "%sx", "", "e%sx", "", "", "", "r%sx"}; 143 static const char * piFormats[9] = 144 {"", "%s", "%s", "", "e%s", "", "", "", "r%s"}; 145 static const char * longFormats[9] = 146 {"", "r%sb", "r%sw", "", "r%sd", "", "", "", "r%s"}; 147 static const char * microFormats[9] = 148 {"", "t%db", "t%dw", "", "t%dd", "", "", "", "t%d"}; 149 150 if (reg < FP_Base_DepTag) { 151 const char * suffix = ""; 152 bool fold = reg & (1 << 6); 153 reg &= ~(1 << 6); 154 155 if(fold) 156 { 157 suffix = "h"; 158 reg -= 4; 159 } 160 else if(reg < 8 && size == 1) 161 suffix = "l"; 162 163 switch (reg) { 164 case INTREG_RAX: 165 ccprintf(os, abcdFormats[size], "a"); 166 break; 167 case INTREG_RBX: 168 ccprintf(os, abcdFormats[size], "b"); 169 break; 170 case INTREG_RCX: 171 ccprintf(os, abcdFormats[size], "c"); 172 break; 173 case INTREG_RDX: 174 ccprintf(os, abcdFormats[size], "d"); 175 break; 176 case INTREG_RSP: 177 ccprintf(os, piFormats[size], "sp"); 178 break; 179 case INTREG_RBP: 180 ccprintf(os, piFormats[size], "bp"); 181 break; 182 case INTREG_RSI: 183 ccprintf(os, piFormats[size], "si"); 184 break; 185 case INTREG_RDI: 186 ccprintf(os, piFormats[size], "di"); 187 break; 188 case INTREG_R8W: 189 ccprintf(os, longFormats[size], "8"); 190 break; 191 case INTREG_R9W: 192 ccprintf(os, longFormats[size], "9"); 193 break; 194 case INTREG_R10W: 195 ccprintf(os, longFormats[size], "10"); 196 break; 197 case INTREG_R11W: 198 ccprintf(os, longFormats[size], "11"); 199 break; 200 case INTREG_R12W: 201 ccprintf(os, longFormats[size], "12"); 202 break; 203 case INTREG_R13W: 204 ccprintf(os, longFormats[size], "13"); 205 break; 206 case INTREG_R14W: 207 ccprintf(os, longFormats[size], "14"); 208 break; 209 case INTREG_R15W: 210 ccprintf(os, longFormats[size], "15"); 211 break; 212 default: 213 ccprintf(os, microFormats[size], reg - NUM_INTREGS); 214 } 215 ccprintf(os, suffix); 216 } else if (reg < Ctrl_Base_DepTag) { 217 int fpindex = reg - FP_Base_DepTag; 218 if(fpindex < NumMMXRegs) { 219 ccprintf(os, "%%mmx%d", reg - FP_Base_DepTag); 220 return; 221 } 222 fpindex -= NumMMXRegs; 223 if(fpindex < NumXMMRegs * 2) { 224 ccprintf(os, "%%xmm%d_%s", fpindex / 2, 225 (fpindex % 2) ? "high": "low"); 226 return; 227 } 228 fpindex -= NumXMMRegs * 2; 229 if(fpindex < NumMicroFpRegs) { 230 ccprintf(os, "%%ufp%d", fpindex); 231 return; 232 } 233 fpindex -= NumMicroFpRegs; 234 ccprintf(os, "%%st(%d)", fpindex); 235 } else { 236 switch (reg - Ctrl_Base_DepTag) { 237 default: 238 ccprintf(os, "%%ctrl%d", reg - Ctrl_Base_DepTag); 239 } 240 } 241 } 242 243 void X86StaticInst::printMem(std::ostream &os, uint8_t segment, 244 uint8_t scale, RegIndex index, RegIndex base, 245 uint64_t disp, uint8_t addressSize, bool rip) const 246 { 247 bool someAddr = false; 248 printSegment(os, segment); 249 os << ":["; 250 if (rip) { 251 os << "rip"; 252 someAddr = true; 253 } else { 254 if (scale != 0 && index != ZeroReg) 255 { 256 if(scale != 1) 257 ccprintf(os, "%d*", scale); 258 printReg(os, index, addressSize); 259 someAddr = true; 260 } 261 if (base != ZeroReg) 262 { 263 if(someAddr) 264 os << " + "; 265 printReg(os, base, addressSize); 266 someAddr = true; 267 } 268 } 269 if (disp != 0) 270 { 271 if(someAddr) 272 os << " + "; 273 ccprintf(os, "%#x", disp); 274 someAddr = true; 275 } 276 if (!someAddr) 277 os << "0"; 278 os << "]"; 279 } 280 281 std::string X86StaticInst::generateDisassembly(Addr pc, 282 const SymbolTable *symtab) const 283 { 284 std::stringstream ss; 285 286 printMnemonic(ss, mnemonic); 287 288 return ss.str(); 289 } 290} 291