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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
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-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;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Constructor
|
|
|
|
Jump(const char *mnem, MachInst _machInst, OpClass __opClass)
|
|
|
|
: PCDependentDisassembly(mnem, _machInst, __opClass),
|
2006-02-16 08:39:46 +01:00
|
|
|
disp(OFFSET)
|
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)
|
|
|
|
if (_numSrcRegs > 0) {
|
|
|
|
printReg(ss, _srcRegIdx[0]);
|
|
|
|
ss << ",";
|
|
|
|
}
|
|
|
|
else if (_numDestRegs > 0) {
|
|
|
|
printReg(ss, _destRegIdx[0]);
|
|
|
|
ss << ",";
|
2006-01-10 20:57:37 +01:00
|
|
|
}
|
2006-02-15 03:26:01 +01:00
|
|
|
|
2006-02-18 20:38:23 +01:00
|
|
|
#ifdef SS_COMPATIBLE_DISASSEMBLY
|
|
|
|
if (_numSrcRegs == 0 && _numDestRegs == 0) {
|
|
|
|
printReg(ss, 31);
|
|
|
|
ss << ",";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
#ifdef SS_COMPATIBLE_DISASSEMBLY
|
|
|
|
if (_numSrcRegs == 0 && _numDestRegs == 0) {
|
|
|
|
printReg(ss, 31);
|
|
|
|
ss << ",";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
#ifdef SS_COMPATIBLE_DISASSEMBLY
|
|
|
|
if (_numDestRegs == 0) {
|
|
|
|
printReg(ss, 31);
|
|
|
|
ss << ",";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (_numDestRegs > 0) {
|
|
|
|
printReg(ss, _destRegIdx[0]);
|
|
|
|
ss << ",";
|
|
|
|
}
|
|
|
|
|
|
|
|
ccprintf(ss, "(r%d)", RB);
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
2006-02-18 09:12:04 +01:00
|
|
|
def format Branch(code,*flags) {{
|
2006-02-18 10:17:11 +01:00
|
|
|
code = 'bool cond;\n\t' + code + '\n'
|
2006-02-18 09:12:04 +01:00
|
|
|
|
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-02-19 05:17:45 +01:00
|
|
|
code += 'R31 = NPC + 4;\n'
|
2006-02-18 09:12:04 +01:00
|
|
|
|
2006-02-19 05:17:45 +01:00
|
|
|
# condition code
|
|
|
|
code += 'if (cond) {'
|
|
|
|
code += ' NPC = NPC + disp;\n'
|
|
|
|
code += ' NNPC = NNPC + disp;\n } \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'))
|
|
|
|
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
|
|
|
code = 'bool cond;\n\t\t\t' + code
|
2006-02-18 09:12:04 +01:00
|
|
|
|
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-02-19 05:17:45 +01:00
|
|
|
code += 'R31 = NPC + 4;\n'
|
|
|
|
|
|
|
|
#condition code
|
|
|
|
code += 'if (cond) {'
|
|
|
|
code += ' NPC = NPC + disp;\n'
|
|
|
|
code += ' NNPC = NNPC + disp;\n } \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-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-18 09:12:04 +01:00
|
|
|
def format Unconditional(code,*flags) {{
|
|
|
|
iop = InstObjParams(name, Name, 'Jump', CodeBlock(code),
|
|
|
|
('IsIndirectControl', 'IsUncondControl'))
|
|
|
|
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
|
|
|
|
|
|
|
|