2006-02-16 08:51:04 +01:00
|
|
|
// -*- mode:c++ -*-
|
2006-02-15 03:26:01 +01:00
|
|
|
|
2006-02-16 08:39:46 +01:00
|
|
|
////////////////////////////////////////////////////////////////////
|
2006-01-10 20:57:37 +01:00
|
|
|
//
|
2006-02-16 08:39:46 +01:00
|
|
|
// Control transfer instructions
|
2006-01-10 20:57:37 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
output header {{
|
2006-02-15 03:26:01 +01:00
|
|
|
|
2006-03-08 08:05:38 +01:00
|
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
|
|
|
|
2006-02-15 03:26:01 +01:00
|
|
|
/**
|
|
|
|
* Base class for instructions whose disassembly is not purely a
|
|
|
|
* function of the machine instruction (i.e., it depends on the
|
|
|
|
* PC). This class overrides the disassemble() method to check
|
|
|
|
* the PC and symbol table values before re-using a cached
|
|
|
|
* disassembly string. This is necessary for branches and jumps,
|
|
|
|
* where the disassembly string includes the target address (which
|
|
|
|
* may depend on the PC and/or symbol table).
|
|
|
|
*/
|
2006-02-16 08:39:46 +01:00
|
|
|
class PCDependentDisassembly : public MipsStaticInst
|
2006-02-15 03:26:01 +01:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
/// Cached program counter from last disassembly
|
|
|
|
mutable Addr cachedPC;
|
2006-02-16 08:39:46 +01:00
|
|
|
|
2006-02-15 03:26:01 +01:00
|
|
|
/// Cached symbol table pointer from last disassembly
|
|
|
|
mutable const SymbolTable *cachedSymtab;
|
|
|
|
|
|
|
|
/// Constructor
|
|
|
|
PCDependentDisassembly(const char *mnem, MachInst _machInst,
|
|
|
|
OpClass __opClass)
|
2006-02-16 08:39:46 +01:00
|
|
|
: MipsStaticInst(mnem, _machInst, __opClass),
|
2006-02-15 03:26:01 +01:00
|
|
|
cachedPC(0), cachedSymtab(0)
|
2006-01-10 20:57:37 +01:00
|
|
|
{
|
2006-02-15 03:26:01 +01:00
|
|
|
}
|
2006-01-10 20:57:37 +01:00
|
|
|
|
2006-02-15 03:26:01 +01:00
|
|
|
const std::string &
|
|
|
|
disassemble(Addr pc, const SymbolTable *symtab) const;
|
|
|
|
};
|
2006-01-10 20:57:37 +01:00
|
|
|
|
2006-02-15 03:26:01 +01:00
|
|
|
/**
|
|
|
|
* Base class for branches (PC-relative control transfers),
|
|
|
|
* conditional or unconditional.
|
|
|
|
*/
|
|
|
|
class Branch : public PCDependentDisassembly
|
|
|
|
{
|
|
|
|
protected:
|
2006-02-16 08:39:46 +01:00
|
|
|
/// target address (signed) Displacement .
|
2006-02-19 05:17:45 +01:00
|
|
|
int32_t disp;
|
2006-01-10 20:57:37 +01:00
|
|
|
|
2006-02-15 03:26:01 +01:00
|
|
|
/// Constructor.
|
|
|
|
Branch(const char *mnem, MachInst _machInst, OpClass __opClass)
|
|
|
|
: PCDependentDisassembly(mnem, _machInst, __opClass),
|
2006-02-19 05:17:45 +01:00
|
|
|
disp(OFFSET << 2)
|
2006-02-16 08:39:46 +01:00
|
|
|
{
|
2006-03-08 08:05:38 +01:00
|
|
|
//If Bit 17 is 1 then Sign Extend
|
|
|
|
if ( (disp & 0x00020000) > 0 ) {
|
|
|
|
disp |= 0xFFFE0000;
|
|
|
|
}
|
2006-02-16 08:39:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Addr branchTarget(Addr branchPC) const;
|
|
|
|
|
|
|
|
std::string
|
|
|
|
generateDisassembly(Addr pc, const SymbolTable *symtab) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2006-02-18 09:12:04 +01:00
|
|
|
* Base class for branch likely branches (PC-relative control transfers),
|
2006-02-16 08:39:46 +01:00
|
|
|
*/
|
|
|
|
class BranchLikely : public PCDependentDisassembly
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
/// target address (signed) Displacement .
|
2006-02-19 05:17:45 +01:00
|
|
|
int32_t disp;
|
2006-02-16 08:39:46 +01:00
|
|
|
|
|
|
|
/// Constructor.
|
2006-02-22 09:33:35 +01:00
|
|
|
BranchLikely(const char *mnem, MachInst _machInst, OpClass __opClass)
|
2006-02-16 08:39:46 +01:00
|
|
|
: PCDependentDisassembly(mnem, _machInst, __opClass),
|
2006-02-19 05:17:45 +01:00
|
|
|
disp(OFFSET << 2)
|
2006-02-15 03:26:01 +01:00
|
|
|
{
|
2006-03-08 08:05:38 +01:00
|
|
|
|
2006-02-15 03:26:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Addr branchTarget(Addr branchPC) const;
|
|
|
|
|
|
|
|
std::string
|
|
|
|
generateDisassembly(Addr pc, const SymbolTable *symtab) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for jumps (register-indirect control transfers). In
|
2006-02-16 08:39:46 +01:00
|
|
|
* the Mips ISA, these are always unconditional.
|
2006-02-15 03:26:01 +01:00
|
|
|
*/
|
|
|
|
class Jump : public PCDependentDisassembly
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
|
|
|
|
/// Displacement to target address (signed).
|
|
|
|
int32_t disp;
|
|
|
|
|
2006-03-08 08:05:38 +01:00
|
|
|
uint32_t target;
|
|
|
|
|
2006-02-15 03:26:01 +01:00
|
|
|
public:
|
|
|
|
/// Constructor
|
|
|
|
Jump(const char *mnem, MachInst _machInst, OpClass __opClass)
|
|
|
|
: PCDependentDisassembly(mnem, _machInst, __opClass),
|
2006-03-08 08:05:38 +01:00
|
|
|
disp(JMPTARG << 2)
|
2006-01-10 20:57:37 +01:00
|
|
|
{
|
|
|
|
}
|
2006-02-15 03:26:01 +01:00
|
|
|
|
|
|
|
Addr branchTarget(ExecContext *xc) const;
|
|
|
|
|
|
|
|
std::string
|
|
|
|
generateDisassembly(Addr pc, const SymbolTable *symtab) const;
|
|
|
|
};
|
2006-01-10 20:57:37 +01:00
|
|
|
}};
|
|
|
|
|
2006-02-15 03:26:01 +01:00
|
|
|
output decoder {{
|
|
|
|
Addr
|
|
|
|
Branch::branchTarget(Addr branchPC) const
|
|
|
|
{
|
|
|
|
return branchPC + 4 + disp;
|
|
|
|
}
|
|
|
|
|
2006-02-18 20:38:23 +01:00
|
|
|
Addr
|
|
|
|
BranchLikely::branchTarget(Addr branchPC) const
|
|
|
|
{
|
|
|
|
return branchPC + 4 + disp;
|
|
|
|
}
|
|
|
|
|
2006-02-15 03:26:01 +01:00
|
|
|
Addr
|
|
|
|
Jump::branchTarget(ExecContext *xc) const
|
|
|
|
{
|
|
|
|
Addr NPC = xc->readPC() + 4;
|
|
|
|
uint64_t Rb = xc->readIntReg(_srcRegIdx[0]);
|
|
|
|
return (Rb & ~3) | (NPC & 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string &
|
|
|
|
PCDependentDisassembly::disassemble(Addr pc,
|
|
|
|
const SymbolTable *symtab) const
|
|
|
|
{
|
|
|
|
if (!cachedDisassembly ||
|
|
|
|
pc != cachedPC || symtab != cachedSymtab)
|
2006-01-10 20:57:37 +01:00
|
|
|
{
|
2006-02-15 03:26:01 +01:00
|
|
|
if (cachedDisassembly)
|
|
|
|
delete cachedDisassembly;
|
|
|
|
|
|
|
|
cachedDisassembly =
|
|
|
|
new std::string(generateDisassembly(pc, symtab));
|
|
|
|
cachedPC = pc;
|
|
|
|
cachedSymtab = symtab;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *cachedDisassembly;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
Branch::generateDisassembly(Addr pc, const SymbolTable *symtab) const
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
|
|
|
|
ccprintf(ss, "%-10s ", mnemonic);
|
|
|
|
|
|
|
|
// There's only one register arg (RA), but it could be
|
|
|
|
// either a source (the condition for conditional
|
|
|
|
// branches) or a destination (the link reg for
|
|
|
|
// unconditional branches)
|
2006-03-08 08:05:38 +01:00
|
|
|
if (_numSrcRegs == 1) {
|
2006-02-15 03:26:01 +01:00
|
|
|
printReg(ss, _srcRegIdx[0]);
|
|
|
|
ss << ",";
|
2006-03-08 08:05:38 +01:00
|
|
|
} else if(_numSrcRegs == 2) {
|
|
|
|
printReg(ss, _srcRegIdx[0]);
|
2006-02-15 03:26:01 +01:00
|
|
|
ss << ",";
|
2006-03-08 08:05:38 +01:00
|
|
|
printReg(ss, _srcRegIdx[1]);
|
2006-02-18 20:38:23 +01:00
|
|
|
ss << ",";
|
|
|
|
}
|
|
|
|
|
2006-03-08 08:05:38 +01:00
|
|
|
Addr target = pc + 8 + disp;
|
2006-02-18 20:38:23 +01:00
|
|
|
|
|
|
|
std::string str;
|
|
|
|
if (symtab && symtab->findSymbol(target, str))
|
|
|
|
ss << str;
|
|
|
|
else
|
|
|
|
ccprintf(ss, "0x%x", target);
|
|
|
|
|
2006-03-15 22:26:40 +01:00
|
|
|
string inst_name = mnemonic;
|
|
|
|
|
|
|
|
if (inst_name.substr(inst_name.length()-2,inst_name.length()) == "al"){
|
|
|
|
ccprintf(ss, " (r31=0x%x)",pc+8);
|
|
|
|
}
|
|
|
|
|
2006-02-18 20:38:23 +01:00
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
BranchLikely::generateDisassembly(Addr pc, const SymbolTable *symtab) const
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
|
|
|
|
ccprintf(ss, "%-10s ", mnemonic);
|
|
|
|
|
|
|
|
// There's only one register arg (RA), but it could be
|
|
|
|
// either a source (the condition for conditional
|
|
|
|
// branches) or a destination (the link reg for
|
|
|
|
// unconditional branches)
|
|
|
|
if (_numSrcRegs > 0) {
|
|
|
|
printReg(ss, _srcRegIdx[0]);
|
|
|
|
ss << ",";
|
|
|
|
}
|
|
|
|
else if (_numDestRegs > 0) {
|
|
|
|
printReg(ss, _destRegIdx[0]);
|
|
|
|
ss << ",";
|
|
|
|
}
|
|
|
|
|
2006-02-15 03:26:01 +01:00
|
|
|
Addr target = pc + 4 + disp;
|
|
|
|
|
|
|
|
std::string str;
|
|
|
|
if (symtab && symtab->findSymbol(target, str))
|
|
|
|
ss << str;
|
|
|
|
else
|
|
|
|
ccprintf(ss, "0x%x", target);
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
Jump::generateDisassembly(Addr pc, const SymbolTable *symtab) const
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
|
|
|
|
ccprintf(ss, "%-10s ", mnemonic);
|
|
|
|
|
2006-03-08 08:05:38 +01:00
|
|
|
if ( mnemonic == "jal" ) {
|
|
|
|
Addr npc = pc + 4;
|
|
|
|
ccprintf(ss,"0x%x",(npc & 0xF0000000) | disp);
|
|
|
|
} else if (_numSrcRegs == 0) {
|
|
|
|
std::string str;
|
|
|
|
if (symtab && symtab->findSymbol(disp, str))
|
|
|
|
ss << str;
|
|
|
|
else
|
|
|
|
ccprintf(ss, "0x%x", disp);
|
|
|
|
} else if (_numSrcRegs == 1) {
|
|
|
|
printReg(ss, _srcRegIdx[0]);
|
|
|
|
} else if(_numSrcRegs == 2) {
|
|
|
|
printReg(ss, _srcRegIdx[0]);
|
2006-02-15 03:26:01 +01:00
|
|
|
ss << ",";
|
2006-03-08 08:05:38 +01:00
|
|
|
printReg(ss, _srcRegIdx[1]);
|
|
|
|
} else {
|
|
|
|
panic(">= 3 Source Registers!!!");
|
2006-02-15 03:26:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
2006-02-18 09:12:04 +01:00
|
|
|
def format Branch(code,*flags) {{
|
2006-02-18 20:38:23 +01:00
|
|
|
#Add Link Code if Link instruction
|
2006-02-18 10:17:11 +01:00
|
|
|
strlen = len(name)
|
|
|
|
if name[strlen-2:] == 'al':
|
2006-03-08 08:05:38 +01:00
|
|
|
code += 'r31 = NNPC;\n'
|
2006-02-18 09:12:04 +01:00
|
|
|
|
2006-02-20 07:49:16 +01:00
|
|
|
#Condition code
|
|
|
|
code = 'bool cond;\n' + code
|
|
|
|
code += 'if (cond) {\n'
|
|
|
|
code += ' NNPC = NPC + disp;\n'
|
2006-03-08 08:05:38 +01:00
|
|
|
code += '} else {\n'
|
|
|
|
code += ' NNPC = NNPC;\n'
|
2006-02-20 07:49:16 +01:00
|
|
|
code += '} \n'
|
2006-02-18 09:12:04 +01:00
|
|
|
|
2006-02-08 22:24:04 +01:00
|
|
|
iop = InstObjParams(name, Name, 'Branch', CodeBlock(code),
|
|
|
|
('IsDirectControl', 'IsCondControl'))
|
2006-02-20 07:49:16 +01:00
|
|
|
|
2006-02-08 22:24:04 +01:00
|
|
|
header_output = BasicDeclare.subst(iop)
|
|
|
|
decoder_output = BasicConstructor.subst(iop)
|
|
|
|
decode_block = BasicDecode.subst(iop)
|
|
|
|
exec_output = BasicExecute.subst(iop)
|
2006-01-10 20:57:37 +01:00
|
|
|
}};
|
2006-02-08 22:24:04 +01:00
|
|
|
|
2006-02-19 05:17:45 +01:00
|
|
|
|
2006-02-18 09:12:04 +01:00
|
|
|
def format BranchLikely(code,*flags) {{
|
2006-02-18 20:38:23 +01:00
|
|
|
#Add Link Code if Link instruction
|
2006-02-18 10:17:11 +01:00
|
|
|
strlen = len(name)
|
|
|
|
if name[strlen-3:] == 'all':
|
2006-03-08 08:05:38 +01:00
|
|
|
code += 'r31 = NNPC;\n'
|
2006-02-19 05:17:45 +01:00
|
|
|
|
2006-02-20 07:49:16 +01:00
|
|
|
#Condition code
|
|
|
|
code = 'bool cond;\n' + code
|
2006-02-19 05:17:45 +01:00
|
|
|
code += 'if (cond) {'
|
2006-02-20 07:49:16 +01:00
|
|
|
code += 'NNPC = NPC + disp;\n'
|
|
|
|
code += '} \n'
|
2006-02-18 09:12:04 +01:00
|
|
|
|
2006-02-18 20:38:23 +01:00
|
|
|
|
2006-02-16 08:39:46 +01:00
|
|
|
iop = InstObjParams(name, Name, 'Branch', CodeBlock(code),
|
2006-02-18 09:12:04 +01:00
|
|
|
('IsDirectControl', 'IsCondControl','IsCondDelaySlot'))
|
2006-02-20 07:49:16 +01:00
|
|
|
|
2006-02-16 08:39:46 +01:00
|
|
|
header_output = BasicDeclare.subst(iop)
|
|
|
|
decoder_output = BasicConstructor.subst(iop)
|
|
|
|
decode_block = BasicDecode.subst(iop)
|
|
|
|
exec_output = BasicExecute.subst(iop)
|
2006-02-15 03:26:01 +01:00
|
|
|
}};
|
|
|
|
|
2006-02-20 07:49:16 +01:00
|
|
|
def format Jump(code,*flags) {{
|
|
|
|
#Add Link Code if Link instruction
|
|
|
|
strlen = len(name)
|
2006-03-08 08:05:38 +01:00
|
|
|
if strlen > 1 and name[1:] == 'al':
|
|
|
|
code = 'r31 = NNPC;\n' + code
|
|
|
|
|
2006-02-20 07:49:16 +01:00
|
|
|
|
|
|
|
iop = InstObjParams(name, Name, 'Jump', CodeBlock(code),\
|
2006-02-18 09:12:04 +01:00
|
|
|
('IsIndirectControl', 'IsUncondControl'))
|
2006-02-20 07:49:16 +01:00
|
|
|
|
2006-02-18 09:12:04 +01:00
|
|
|
header_output = BasicDeclare.subst(iop)
|
|
|
|
decoder_output = BasicConstructor.subst(iop)
|
|
|
|
decode_block = BasicDecode.subst(iop)
|
|
|
|
exec_output = BasicExecute.subst(iop)
|
2006-02-15 03:26:01 +01:00
|
|
|
}};
|
|
|
|
|
2006-02-18 09:12:04 +01:00
|
|
|
|
2006-02-15 03:26:01 +01:00
|
|
|
|
|
|
|
|