URL
https://opencores.org/ocsvn/lxp32/lxp32/trunk
Subversion Repositories lxp32
Compare Revisions
- This comparison shows the changes necessary to convert path
/lxp32/trunk/tools/src/lxp32dump
- from Rev 2 to Rev 6
- ↔ Reverse comparison
Rev 2 → Rev 6
/disassembler.cpp
26,24 → 26,12
return _value; |
} |
|
std::string Disassembler::Operand::str() const { |
if(_type==Register) { |
if(_value>=240&&_value<=247) return "iv"+std::to_string(_value-240); |
else if(_value==252) return "cr"; |
else if(_value==253) return "irp"; |
else if(_value==254) return "rp"; |
else if(_value==255) return "sp"; |
else return "r"+std::to_string(_value); |
} |
else return std::to_string(_value); |
} |
|
/* |
* Disassembler class members |
*/ |
|
Disassembler::Disassembler(std::istream &is,std::ostream &os): |
_is(is),_os(os),_fmt(Bin),_lineNumber(0),_pos(0) {} |
_is(is),_os(os),_fmt(Bin),_preferAliases(true),_lineNumber(0),_pos(0) {} |
|
void Disassembler::setFormat(Format fmt) { |
_fmt=fmt; |
53,6 → 41,10
_pos=base; |
} |
|
void Disassembler::setPreferAliases(bool b) { |
_preferAliases=b; |
} |
|
void Disassembler::dump() { |
Word word; |
|
138,6 → 130,7
break; |
default: |
if((opcode>>4)==0x03) instruction=decodeCjmpxx(word); |
else if((opcode>>3)==0x05) instruction=decodeLcs(word); |
else instruction=decodeWord(word); |
} |
|
183,6 → 176,19
return true; |
} |
|
std::string Disassembler::str(const Operand &op) { |
if(op.type()==Operand::Register) { |
if(!_preferAliases) return "r"+std::to_string(op.value()); |
else if(op.value()>=240&&op.value()<=247) return "iv"+std::to_string(op.value()-240); |
else if(op.value()==252) return "cr"; |
else if(op.value()==253) return "irp"; |
else if(op.value()==254) return "rp"; |
else if(op.value()==255) return "sp"; |
else return "r"+std::to_string(op.value()); |
} |
else return std::to_string(op.value()); |
} |
|
Disassembler::Operand Disassembler::decodeRd1Operand(Word w) { |
int value=(w>>8)&0xFF; |
if(w&0x02000000) return Operand(Operand::Register,value); |
211,7 → 217,7
auto dst=decodeDstOperand(w); |
auto rd1=decodeRd1Operand(w); |
auto rd2=decodeRd2Operand(w); |
oss<<op<<' '<<dst.str()<<", "<<rd1.str()<<", "<<rd2.str(); |
oss<<op<<' '<<str(dst)<<", "<<str(rd1)<<", "<<str(rd2); |
return oss.str(); |
} |
|
222,10 → 228,10
auto rd1=decodeRd1Operand(w); |
auto rd2=decodeRd2Operand(w); |
|
if(rd2.type()==Operand::Direct&&rd2.value()==0) |
oss<<"mov "<<dst.str()<<", "<<rd1.str(); |
if(rd2.type()==Operand::Direct&&rd2.value()==0&&_preferAliases) |
oss<<"mov "<<str(dst)<<", "<<str(rd1); |
else |
oss<<"add "<<dst.str()<<", "<<rd1.str()<<", "<<rd2.str(); |
oss<<"add "<<str(dst)<<", "<<str(rd1)<<", "<<str(rd2); |
|
return oss.str(); |
} |
243,7 → 249,7
if(rd1.type()!=Operand::Register) return decodeWord(w); |
if(rd2.type()!=Operand::Direct||rd2.value()!=0) return decodeWord(w); |
|
return "call "+rd1.str(); |
return "call "+str(rd1); |
} |
|
std::string Disassembler::decodeCjmpxx(Word w) { |
302,9 → 308,9
if(rd1.type()!=Operand::Register) return decodeWord(w); |
if(rd2.type()!=Operand::Direct||rd2.value()!=0) return decodeWord(w); |
|
if(rd1.value()==253) return "iret"; |
if(rd1.value()==254) return "ret"; |
return "jmp "+rd1.str(); |
if(rd1.value()==253&&_preferAliases) return "iret"; |
if(rd1.value()==254&&_preferAliases) return "ret"; |
return "jmp "+str(rd1); |
} |
|
std::string Disassembler::decodeLc(Word w,bool &valid,Word &operand) { |
321,9 → 327,17
if(!b) return decodeWord(w); |
|
valid=true; |
return "lc "+dst.str()+", 0x"+hex(operand); |
return "lc "+str(dst)+", 0x"+hex(operand); |
} |
|
std::string Disassembler::decodeLcs(Word w) { |
auto dst=decodeDstOperand(w); |
auto operand=w&0xFFFF; |
operand|=(w>>8)&0x001F0000; |
if(operand&0x00100000) operand|=0xFFE00000; |
return "lcs "+str(dst)+", 0x"+hex(operand); |
} |
|
std::string Disassembler::decodeLsb(Word w) { |
std::ostringstream oss; |
|
334,7 → 348,7
if(rd1.type()!=Operand::Register) return decodeWord(w); |
if(rd2.type()!=Operand::Direct||rd2.value()!=0) return decodeWord(w); |
|
return "lsb "+dst.str()+", "+rd1.str(); |
return "lsb "+str(dst)+", "+str(rd1); |
} |
|
std::string Disassembler::decodeLub(Word w) { |
347,7 → 361,7
if(rd1.type()!=Operand::Register) return decodeWord(w); |
if(rd2.type()!=Operand::Direct||rd2.value()!=0) return decodeWord(w); |
|
return "lub "+dst.str()+", "+rd1.str(); |
return "lub "+str(dst)+", "+str(rd1); |
} |
|
std::string Disassembler::decodeLw(Word w) { |
360,7 → 374,7
if(rd1.type()!=Operand::Register) return decodeWord(w); |
if(rd2.type()!=Operand::Direct||rd2.value()!=0) return decodeWord(w); |
|
return "lw "+dst.str()+", "+rd1.str(); |
return "lw "+str(dst)+", "+str(rd1); |
} |
|
std::string Disassembler::decodeMods(Word w) { |
398,7 → 412,7
if(dst.value()!=0) return decodeWord(w); |
if(rd1.type()!=Operand::Register) return decodeWord(w); |
|
return "sb "+rd1.str()+", "+rd2.str(); |
return "sb "+str(rd1)+", "+str(rd2); |
} |
|
std::string Disassembler::decodeSl(Word w) { |
420,7 → 434,18
} |
|
std::string Disassembler::decodeSub(Word w) { |
return decodeSimpleInstruction("sub",w); |
std::ostringstream oss; |
|
auto dst=decodeDstOperand(w); |
auto rd1=decodeRd1Operand(w); |
auto rd2=decodeRd2Operand(w); |
|
if(rd1.type()==Operand::Direct&&rd1.value()==0&&_preferAliases) |
oss<<"neg "<<str(dst)<<", "<<str(rd2); |
else |
oss<<"sub "<<str(dst)<<", "<<str(rd1)<<", "<<str(rd2); |
|
return oss.str(); |
} |
|
std::string Disassembler::decodeSw(Word w) { |
433,7 → 458,7
if(dst.value()!=0) return decodeWord(w); |
if(rd1.type()!=Operand::Register) return decodeWord(w); |
|
return "sw "+rd1.str()+", "+rd2.str(); |
return "sw "+str(rd1)+", "+str(rd2); |
} |
|
std::string Disassembler::decodeXor(Word w) { |
443,10 → 468,10
auto rd1=decodeRd1Operand(w); |
auto rd2=decodeRd2Operand(w); |
|
if(rd2.type()==Operand::Direct&&rd2.value()==-1) |
oss<<"not "<<dst.str()<<", "<<rd1.str(); |
if(rd2.type()==Operand::Direct&&rd2.value()==-1&&_preferAliases) |
oss<<"not "<<str(dst)<<", "<<str(rd1); |
else |
oss<<"xor "<<dst.str()<<", "<<rd1.str()<<", "<<rd2.str(); |
oss<<"xor "<<str(dst)<<", "<<str(rd1)<<", "<<str(rd2); |
|
return oss.str(); |
} |
/disassembler.h
27,15 → 27,14
int _value; |
public: |
Operand(Type t,int value); |
|
Type type() const; |
int value() const; |
std::string str() const; |
}; |
|
std::istream &_is; |
std::ostream &_os; |
Format _fmt; |
bool _preferAliases; |
int _lineNumber; |
Word _pos; |
public: |
42,6 → 41,7
Disassembler(std::istream &is,std::ostream &os); |
void setFormat(Format fmt); |
void setBase(Word base); |
void setPreferAliases(bool b); |
void dump(); |
|
template <typename T> static std::string hex(const T &w) { |
58,11 → 58,12
} |
private: |
bool getWord(Word &w); |
std::string str(const Operand &op); |
static Operand decodeRd1Operand(Word w); |
static Operand decodeRd2Operand(Word w); |
static Operand decodeDstOperand(Word w); |
static std::string decodeSimpleInstruction(const std::string &op,Word w); |
|
std::string decodeSimpleInstruction(const std::string &op,Word w); |
std::string decodeAdd(Word w); |
std::string decodeAnd(Word w); |
std::string decodeCall(Word w); |
72,6 → 73,7
std::string decodeHlt(Word w); |
std::string decodeJmp(Word w); |
std::string decodeLc(Word w,bool &valid,Word &operand); |
std::string decodeLcs(Word w); |
std::string decodeLsb(Word w); |
std::string decodeLub(Word w); |
std::string decodeLw(Word w); |
/main.cpp
30,6 → 30,7
os<<" -b <addr> Base address (for comments only)"<<std::endl; |
os<<" -f <fmt> Input format (bin, textio, dec, hex), default: autodetect"<<std::endl; |
os<<" -h, --help Display a short help message"<<std::endl; |
os<<" -na Do not use instruction and register aliases"<<std::endl; |
os<<" -o <file> Output file name, default: standard output"<<std::endl; |
os<<" -- Do not interpret subsequent arguments as options"<<std::endl; |
} |
64,12 → 65,13
std::string inputFileName,outputFileName; |
|
std::cerr<<"LXP32 Platform Disassembler"<<std::endl; |
std::cerr<<"Copyright (c) 2016 by Alex I. Kuznetsov"<<std::endl; |
std::cerr<<"Copyright (c) 2016-2019 by Alex I. Kuznetsov"<<std::endl; |
|
Disassembler::Format fmt=Disassembler::Bin; |
bool noMoreOptions=false; |
bool formatSpecified=false; |
Disassembler::Word base=0; |
bool noAliases=false; |
|
if(argc<=1) { |
displayUsage(std::cout,argv[0]); |
111,6 → 113,9
displayUsage(std::cout,argv[0]); |
return 0; |
} |
else if(!strcmp(argv[i],"-na")) { |
noAliases=true; |
} |
else if(!strcmp(argv[i],"-o")) { |
if(++i==argc) { |
displayUsage(std::cerr,argv[0]); |
175,6 → 180,7
Disassembler disasm(in,*os); |
disasm.setFormat(fmt); |
disasm.setBase(base); |
disasm.setPreferAliases(!noAliases); |
|
try { |
disasm.dump(); |