3 // Copyright (c) 2007 MIPS Technologies, Inc.
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 ////////////////////////////////////////////////////////////////////
31 // Control transfer instructions
40 * Base class for instructions whose disassembly is not purely a
41 * function of the machine instruction (i.e., it depends on the
42 * PC). This class overrides the disassemble() method to check
43 * the PC and symbol table values before re-using a cached
44 * disassembly string. This is necessary for branches and jumps,
45 * where the disassembly string includes the target address (which
46 * may depend on the PC and/or symbol table).
48 class PCDependentDisassembly : public MipsStaticInst
51 /// Cached program counter from last disassembly
52 mutable Addr cachedPC;
54 /// Cached symbol table pointer from last disassembly
55 mutable const Loader::SymbolTable *cachedSymtab;
58 PCDependentDisassembly(const char *mnem, MachInst _machInst,
60 : MipsStaticInst(mnem, _machInst, __opClass),
61 cachedPC(0), cachedSymtab(0)
66 disassemble(Addr pc, const Loader::SymbolTable *symtab) const;
70 * Base class for branches (PC-relative control transfers),
71 * conditional or unconditional.
73 class Branch : public PCDependentDisassembly
76 /// target address (signed) Displacement .
80 Branch(const char *mnem, MachInst _machInst, OpClass __opClass)
81 : PCDependentDisassembly(mnem, _machInst, __opClass),
84 //If Bit 17 is 1 then Sign Extend
85 if ( (disp & 0x00020000) > 0 ) {
90 MipsISA::PCState branchTarget(
91 const MipsISA::PCState &branchPC) const override;
93 /// Explicitly import the otherwise hidden branchTarget
94 using StaticInst::branchTarget;
96 std::string generateDisassembly(
97 Addr pc, const Loader::SymbolTable *symtab) const override;
101 * Base class for jumps (register-indirect control transfers). In
102 * the Mips ISA, these are always unconditional.
104 class Jump : public PCDependentDisassembly
108 /// Displacement to target address (signed).
115 Jump(const char *mnem, MachInst _machInst, OpClass __opClass)
116 : PCDependentDisassembly(mnem, _machInst, __opClass),
121 MipsISA::PCState branchTarget(ThreadContext *tc) const override;
123 /// Explicitly import the otherwise hidden branchTarget
124 using StaticInst::branchTarget;
126 std::string generateDisassembly(
127 Addr pc, const Loader::SymbolTable *symtab) const override;
133 Branch::branchTarget(const MipsISA::PCState &branchPC) const
135 MipsISA::PCState target = branchPC;
137 target.npc(branchPC.pc() + sizeof(MachInst) + disp);
138 target.nnpc(target.npc() + sizeof(MachInst));
143 Jump::branchTarget(ThreadContext *tc) const
145 MipsISA::PCState target = tc->pcState();
146 Addr pc = target.pc();
148 target.npc((pc & 0xF0000000) | disp);
149 target.nnpc(target.npc() + sizeof(MachInst));
154 PCDependentDisassembly::disassemble(
155 Addr pc, const Loader::SymbolTable *symtab) const
157 if (!cachedDisassembly ||
158 pc != cachedPC || symtab != cachedSymtab)
160 if (cachedDisassembly)
161 delete cachedDisassembly;
164 new std::string(generateDisassembly(pc, symtab));
166 cachedSymtab = symtab;
169 return *cachedDisassembly;
173 Branch::generateDisassembly(
174 Addr pc, const Loader::SymbolTable *symtab) const
176 std::stringstream ss;
178 ccprintf(ss, "%-10s ", mnemonic);
180 // There's only one register arg (RA), but it could be
181 // either a source (the condition for conditional
182 // branches) or a destination (the link reg for
183 // unconditional branches)
184 if (_numSrcRegs == 1) {
185 printReg(ss, _srcRegIdx[0]);
187 } else if(_numSrcRegs == 2) {
188 printReg(ss, _srcRegIdx[0]);
190 printReg(ss, _srcRegIdx[1]);
194 Addr target = pc + 4 + disp;
197 if (symtab && symtab->findSymbol(target, str))
200 ccprintf(ss, "0x%x", target);
206 Jump::generateDisassembly(Addr pc, const Loader::SymbolTable *symtab) const
208 std::stringstream ss;
210 ccprintf(ss, "%-10s ", mnemonic);
212 if ( strcmp(mnemonic,"jal") == 0 ) {
214 ccprintf(ss,"0x%x",(npc & 0xF0000000) | disp);
215 } else if (_numSrcRegs == 0) {
217 if (symtab && symtab->findSymbol(disp, str))
220 ccprintf(ss, "0x%x", disp);
221 } else if (_numSrcRegs == 1) {
222 printReg(ss, _srcRegIdx[0]);
223 } else if(_numSrcRegs == 2) {
224 printReg(ss, _srcRegIdx[0]);
226 printReg(ss, _srcRegIdx[1]);
233 def format Branch(code, *opt_flags) {{
234 not_taken_code = 'NNPC = NNPC; NPC = NPC;'
236 #Build Instruction Flags
237 #Use Link & Likely Flags to Add Link/Condition Code
238 inst_flags = ('IsDirectControl', )
241 code += 'R31 = NNPC;\n'
243 not_taken_code = 'NNPC = NPC; NPC = PC;'
244 inst_flags += ('IsCondDelaySlot', )
248 #Take into account uncond. branch instruction
249 if 'cond = 1' in code:
250 inst_flags += ('IsUncondControl', )
252 inst_flags += ('IsCondControl', )
263 ''' % { "code" : code, "not_taken_code" : not_taken_code }
265 iop = InstObjParams(name, Name, 'Branch', code, inst_flags)
266 header_output = BasicDeclare.subst(iop)
267 decoder_output = BasicConstructor.subst(iop)
268 decode_block = BasicDecode.subst(iop)
269 exec_output = BasicExecute.subst(iop)
272 def format DspBranch(code, *opt_flags) {{
273 not_taken_code = 'NNPC = NNPC; NPC = NPC;'
275 #Build Instruction Flags
276 #Use Link & Likely Flags to Add Link/Condition Code
277 inst_flags = ('IsDirectControl', )
280 code += 'R32 = NNPC;'
282 not_taken_code = 'NNPC = NPC, NPC = PC;'
283 inst_flags += ('IsCondDelaySlot', )
287 #Take into account uncond. branch instruction
288 if 'cond = 1' in code:
289 inst_flags += ('IsUncondControl', )
291 inst_flags += ('IsCondControl', )
296 uint32_t dspctl = DSPControl;
303 ''' % { "code" : code, "not_taken_code" : not_taken_code }
305 iop = InstObjParams(name, Name, 'Branch', code, inst_flags)
306 header_output = BasicDeclare.subst(iop)
307 decoder_output = BasicConstructor.subst(iop)
308 decode_block = BasicDecode.subst(iop)
309 exec_output = BasicExecute.subst(iop)
312 def format Jump(code, *opt_flags) {{
313 #Build Instruction Flags
314 #Use Link Flag to Add Link Code
315 inst_flags = ('IsIndirectControl', 'IsUncondControl')
321 elif x == 'ClearHazards':
322 code += '/* Code Needed to Clear Execute & Inst Hazards */\n'
326 iop = InstObjParams(name, Name, 'Jump', code, inst_flags)
327 header_output = BasicDeclare.subst(iop)
328 decoder_output = BasicConstructor.subst(iop)
329 decode_block = BasicDecode.subst(iop)
330 exec_output = BasicExecute.subst(iop)