3 // Copyright (c) 2003-2006 The Regents of The University of Michigan
4 // All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met: redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer;
10 // redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution;
13 // neither the name of the copyright holders nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // Authors: Korey Sewell
31 ////////////////////////////////////////////////////////////////////
33 // Control transfer instructions
42 * Base class for instructions whose disassembly is not purely a
43 * function of the machine instruction (i.e., it depends on the
44 * PC). This class overrides the disassemble() method to check
45 * the PC and symbol table values before re-using a cached
46 * disassembly string. This is necessary for branches and jumps,
47 * where the disassembly string includes the target address (which
48 * may depend on the PC and/or symbol table).
50 class PCDependentDisassembly : public MipsStaticInst
53 /// Cached program counter from last disassembly
54 mutable Addr cachedPC;
56 /// Cached symbol table pointer from last disassembly
57 mutable const SymbolTable *cachedSymtab;
60 PCDependentDisassembly(const char *mnem, MachInst _machInst,
62 : MipsStaticInst(mnem, _machInst, __opClass),
63 cachedPC(0), cachedSymtab(0)
68 disassemble(Addr pc, const SymbolTable *symtab) const;
72 * Base class for branches (PC-relative control transfers),
73 * conditional or unconditional.
75 class Branch : public PCDependentDisassembly
78 /// target address (signed) Displacement .
82 Branch(const char *mnem, MachInst _machInst, OpClass __opClass)
83 : PCDependentDisassembly(mnem, _machInst, __opClass),
86 //If Bit 17 is 1 then Sign Extend
87 if ( (disp & 0x00020000) > 0 ) {
92 Addr branchTarget(Addr branchPC) const;
95 generateDisassembly(Addr pc, const SymbolTable *symtab) const;
99 * Base class for jumps (register-indirect control transfers). In
100 * the Mips ISA, these are always unconditional.
102 class Jump : public PCDependentDisassembly
106 /// Displacement to target address (signed).
113 Jump(const char *mnem, MachInst _machInst, OpClass __opClass)
114 : PCDependentDisassembly(mnem, _machInst, __opClass),
119 Addr branchTarget(ThreadContext *tc) const;
122 generateDisassembly(Addr pc, const SymbolTable *symtab) const;
128 Branch::branchTarget(Addr branchPC) const
130 return branchPC + 4 + disp;
134 Jump::branchTarget(ThreadContext *tc) const
136 Addr NPC = tc->readPC() + 4;
137 uint64_t Rb = tc->readIntReg(_srcRegIdx[0]);
138 return (Rb & ~3) | (NPC & 1);
142 PCDependentDisassembly::disassemble(Addr pc,
143 const SymbolTable *symtab) const
145 if (!cachedDisassembly ||
146 pc != cachedPC || symtab != cachedSymtab)
148 if (cachedDisassembly)
149 delete cachedDisassembly;
152 new std::string(generateDisassembly(pc, symtab));
154 cachedSymtab = symtab;
157 return *cachedDisassembly;
161 Branch::generateDisassembly(Addr pc, const SymbolTable *symtab) const
163 std::stringstream ss;
165 ccprintf(ss, "%-10s ", mnemonic);
167 // There's only one register arg (RA), but it could be
168 // either a source (the condition for conditional
169 // branches) or a destination (the link reg for
170 // unconditional branches)
171 if (_numSrcRegs == 1) {
172 printReg(ss, _srcRegIdx[0]);
174 } else if(_numSrcRegs == 2) {
175 printReg(ss, _srcRegIdx[0]);
177 printReg(ss, _srcRegIdx[1]);
181 Addr target = pc + 4 + disp;
184 if (symtab && symtab->findSymbol(target, str))
187 ccprintf(ss, "0x%x", target);
193 Jump::generateDisassembly(Addr pc, const SymbolTable *symtab) const
195 std::stringstream ss;
197 ccprintf(ss, "%-10s ", mnemonic);
199 if ( mnemonic == "jal" ) {
201 ccprintf(ss,"0x%x",(npc & 0xF0000000) | disp);
202 } else if (_numSrcRegs == 0) {
204 if (symtab && symtab->findSymbol(disp, str))
207 ccprintf(ss, "0x%x", disp);
208 } else if (_numSrcRegs == 1) {
209 printReg(ss, _srcRegIdx[0]);
210 } else if(_numSrcRegs == 2) {
211 printReg(ss, _srcRegIdx[0]);
213 printReg(ss, _srcRegIdx[1]);
220 def format Branch(code,*opt_flags) {{
221 not_taken_code = ' NNPC = NNPC;\n'
222 not_taken_code += '} \n'
224 #Build Instruction Flags
225 #Use Link & Likely Flags to Add Link/Condition Code
226 inst_flags = ('IsDirectControl', )
229 code += 'R31 = NNPC;\n'
231 not_taken_code = ' NPC = NNPC;\n'
232 not_taken_code += ' NNPC = NNPC + 4;\n'
233 not_taken_code += '} \n'
234 inst_flags = ('IsCondDelaySlot', )
238 if 'cond == 1' in code:
239 inst_flags += ('IsCondControl', )
241 inst_flags += ('IsUncondControl', )
244 code = 'bool cond;\n' + code
245 code += 'if (cond) {\n'
246 code += ' NNPC = NPC + disp;\n'
248 code += not_taken_code
250 iop = InstObjParams(name, Name, 'Branch', CodeBlock(code), inst_flags)
251 header_output = BasicDeclare.subst(iop)
252 decoder_output = BasicConstructor.subst(iop)
253 decode_block = BasicDecode.subst(iop)
254 exec_output = BasicExecute.subst(iop)
257 def format Jump(code, *opt_flags) {{
258 #Build Instruction Flags
259 #Use Link Flag to Add Link Code
260 inst_flags = ('IsIndirectControl', 'IsUncondControl')
263 code = 'R31 = NNPC;\n' + code
264 elif x == 'ClearHazards':
265 code += '/* Code Needed to Clear Execute & Inst Hazards */\n'
269 iop = InstObjParams(name, Name, 'Jump', CodeBlock(code), inst_flags)
270 header_output = BasicDeclare.subst(iop)
271 decoder_output = BasicConstructor.subst(iop)
272 decode_block = BasicDecode.subst(iop)
273 exec_output = BasicExecute.subst(iop)