branch.cc revision 6691
1/* 2 * Copyright (c) 2009 The University of Edinburgh 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Timothy M. Jones 29 */ 30 31#include "arch/power/insts/branch.hh" 32#include "base/loader/symtab.hh" 33 34using namespace PowerISA; 35 36const std::string & 37PCDependentDisassembly::disassemble(Addr pc, const SymbolTable *symtab) const 38{ 39 if (!cachedDisassembly || 40 pc != cachedPC || symtab != cachedSymtab) 41 { 42 if (cachedDisassembly) 43 delete cachedDisassembly; 44 45 cachedDisassembly = 46 new std::string(generateDisassembly(pc, symtab)); 47 cachedPC = pc; 48 cachedSymtab = symtab; 49 } 50 51 return *cachedDisassembly; 52} 53 54Addr 55BranchPCRel::branchTarget(Addr pc) const 56{ 57 return (uint32_t)(pc + disp); 58} 59 60std::string 61BranchPCRel::generateDisassembly(Addr pc, const SymbolTable *symtab) const 62{ 63 std::stringstream ss; 64 65 ccprintf(ss, "%-10s ", mnemonic); 66 67 Addr target = pc + disp; 68 69 std::string str; 70 if (symtab && symtab->findSymbol(target, str)) 71 ss << str; 72 else 73 ccprintf(ss, "0x%x", target); 74 75 return ss.str(); 76} 77 78Addr 79BranchNonPCRel::branchTarget(Addr pc) const 80{ 81 return targetAddr; 82} 83 84std::string 85BranchNonPCRel::generateDisassembly(Addr pc, const SymbolTable *symtab) const 86{ 87 std::stringstream ss; 88 89 ccprintf(ss, "%-10s ", mnemonic); 90 91 std::string str; 92 if (symtab && symtab->findSymbol(targetAddr, str)) 93 ss << str; 94 else 95 ccprintf(ss, "0x%x", targetAddr); 96 97 return ss.str(); 98} 99 100Addr 101BranchPCRelCond::branchTarget(Addr pc) const 102{ 103 return (uint32_t)(pc + disp); 104} 105 106std::string 107BranchPCRelCond::generateDisassembly(Addr pc, const SymbolTable *symtab) const 108{ 109 std::stringstream ss; 110 111 ccprintf(ss, "%-10s ", mnemonic); 112 113 ss << bo << ", " << bi << ", "; 114 115 Addr target = pc + disp; 116 117 std::string str; 118 if (symtab && symtab->findSymbol(target, str)) 119 ss << str; 120 else 121 ccprintf(ss, "0x%x", target); 122 123 return ss.str(); 124} 125 126Addr 127BranchNonPCRelCond::branchTarget(Addr pc) const 128{ 129 return targetAddr; 130} 131 132std::string 133BranchNonPCRelCond::generateDisassembly(Addr pc, 134 const SymbolTable *symtab) const 135{ 136 std::stringstream ss; 137 138 ccprintf(ss, "%-10s ", mnemonic); 139 140 ss << bo << ", " << bi << ", "; 141 142 std::string str; 143 if (symtab && symtab->findSymbol(targetAddr, str)) 144 ss << str; 145 else 146 ccprintf(ss, "0x%x", targetAddr); 147 148 return ss.str(); 149} 150 151Addr 152BranchRegCond::branchTarget(ThreadContext *tc) const 153{ 154 uint32_t regVal = tc->readIntReg(_srcRegIdx[_numSrcRegs - 1]); 155 return (regVal & 0xfffffffc); 156} 157 158std::string 159BranchRegCond::generateDisassembly(Addr pc, 160 const SymbolTable *symtab) const 161{ 162 std::stringstream ss; 163 164 ccprintf(ss, "%-10s ", mnemonic); 165 166 ss << bo << ", " << bi << ", "; 167 168 return ss.str(); 169} 170