| 1 |
9 |
ring0_mipt |
/*
|
| 2 |
|
|
* Copyright (c) 2016 by Alex I. Kuznetsov.
|
| 3 |
|
|
*
|
| 4 |
|
|
* Part of the LXP32 CPU IP core.
|
| 5 |
|
|
*
|
| 6 |
|
|
* Main translation unit for the LXP32 disassembler.
|
| 7 |
|
|
*/
|
| 8 |
|
|
|
| 9 |
|
|
#ifdef _MSC_VER
|
| 10 |
|
|
#define _CRT_SECURE_NO_WARNINGS
|
| 11 |
|
|
#endif
|
| 12 |
|
|
|
| 13 |
|
|
#include "disassembler.h"
|
| 14 |
|
|
|
| 15 |
|
|
#include <iostream>
|
| 16 |
|
|
#include <fstream>
|
| 17 |
|
|
#include <string>
|
| 18 |
|
|
#include <stdexcept>
|
| 19 |
|
|
#include <cstring>
|
| 20 |
|
|
#include <cstdlib>
|
| 21 |
|
|
#include <cstdio>
|
| 22 |
|
|
#include <ctime>
|
| 23 |
|
|
|
| 24 |
|
|
static void displayUsage(std::ostream &os,const char *program) {
|
| 25 |
|
|
os<<std::endl;
|
| 26 |
|
|
os<<"Usage:"<<std::endl;
|
| 27 |
|
|
os<<" "<<program<<" [ option(s) | input file ]"<<std::endl<<std::endl;
|
| 28 |
|
|
|
| 29 |
|
|
os<<"Options:"<<std::endl;
|
| 30 |
|
|
os<<" -b <addr> Base address (for comments only)"<<std::endl;
|
| 31 |
|
|
os<<" -f <fmt> Input format (bin, textio, dec, hex), default: autodetect"<<std::endl;
|
| 32 |
|
|
os<<" -h, --help Display a short help message"<<std::endl;
|
| 33 |
|
|
os<<" -na Do not use instruction and register aliases"<<std::endl;
|
| 34 |
|
|
os<<" -o <file> Output file name, default: standard output"<<std::endl;
|
| 35 |
|
|
os<<" -- Do not interpret subsequent arguments as options"<<std::endl;
|
| 36 |
|
|
}
|
| 37 |
|
|
|
| 38 |
|
|
static Disassembler::Format detectInputFormat(std::istream &in) {
|
| 39 |
|
|
static const std::size_t Size=256;
|
| 40 |
|
|
static const char *textio="01\r\n \t";
|
| 41 |
|
|
static const char *dec="0123456789\r\n \t";
|
| 42 |
|
|
static const char *hex="0123456789ABCDEFabcdef\r\n \t";
|
| 43 |
|
|
|
| 44 |
|
|
char buf[Size];
|
| 45 |
|
|
in.read(buf,Size);
|
| 46 |
|
|
auto s=static_cast<std::size_t>(in.gcount());
|
| 47 |
|
|
in.clear();
|
| 48 |
|
|
in.seekg(0);
|
| 49 |
|
|
|
| 50 |
|
|
Disassembler::Format fmt=Disassembler::Textio;
|
| 51 |
|
|
|
| 52 |
|
|
for(std::size_t i=0;i<s;i++) {
|
| 53 |
|
|
if(fmt==Disassembler::Textio&&!strchr(textio,buf[i])) fmt=Disassembler::Dec;
|
| 54 |
|
|
if(fmt==Disassembler::Dec&&!strchr(dec,buf[i])) fmt=Disassembler::Hex;
|
| 55 |
|
|
if(fmt==Disassembler::Hex&&!strchr(hex,buf[i])) {
|
| 56 |
|
|
fmt=Disassembler::Bin;
|
| 57 |
|
|
break;
|
| 58 |
|
|
}
|
| 59 |
|
|
}
|
| 60 |
|
|
|
| 61 |
|
|
return fmt;
|
| 62 |
|
|
}
|
| 63 |
|
|
|
| 64 |
|
|
int main(int argc,char *argv[]) try {
|
| 65 |
|
|
std::string inputFileName,outputFileName;
|
| 66 |
|
|
|
| 67 |
|
|
std::cerr<<"LXP32 Platform Disassembler"<<std::endl;
|
| 68 |
|
|
std::cerr<<"Copyright (c) 2016-2019 by Alex I. Kuznetsov"<<std::endl;
|
| 69 |
|
|
|
| 70 |
|
|
Disassembler::Format fmt=Disassembler::Bin;
|
| 71 |
|
|
bool noMoreOptions=false;
|
| 72 |
|
|
bool formatSpecified=false;
|
| 73 |
|
|
Disassembler::Word base=0;
|
| 74 |
|
|
bool noAliases=false;
|
| 75 |
|
|
|
| 76 |
|
|
if(argc<=1) {
|
| 77 |
|
|
displayUsage(std::cout,argv[0]);
|
| 78 |
|
|
return 0;
|
| 79 |
|
|
}
|
| 80 |
|
|
|
| 81 |
|
|
for(int i=1;i<argc;i++) {
|
| 82 |
|
|
if(argv[i][0]!='-'||noMoreOptions) {
|
| 83 |
|
|
if(inputFileName.empty()) inputFileName=argv[i];
|
| 84 |
|
|
else throw std::runtime_error("Only one input file name can be specified");
|
| 85 |
|
|
}
|
| 86 |
|
|
else if(!strcmp(argv[i],"--")) noMoreOptions=true;
|
| 87 |
|
|
else if(!strcmp(argv[i],"-b")) {
|
| 88 |
|
|
if(++i==argc) {
|
| 89 |
|
|
displayUsage(std::cerr,argv[0]);
|
| 90 |
|
|
return EXIT_FAILURE;
|
| 91 |
|
|
}
|
| 92 |
|
|
try {
|
| 93 |
|
|
base=std::stoul(argv[i],nullptr,0);
|
| 94 |
|
|
if(base%4!=0) throw std::exception();
|
| 95 |
|
|
}
|
| 96 |
|
|
catch(std::exception &) {
|
| 97 |
|
|
throw std::runtime_error("Invalid base address");
|
| 98 |
|
|
}
|
| 99 |
|
|
}
|
| 100 |
|
|
else if(!strcmp(argv[i],"-f")) {
|
| 101 |
|
|
if(++i==argc) {
|
| 102 |
|
|
displayUsage(std::cerr,argv[0]);
|
| 103 |
|
|
return EXIT_FAILURE;
|
| 104 |
|
|
}
|
| 105 |
|
|
if(!strcmp(argv[i],"bin")) fmt=Disassembler::Bin;
|
| 106 |
|
|
else if(!strcmp(argv[i],"textio")) fmt=Disassembler::Textio;
|
| 107 |
|
|
else if(!strcmp(argv[i],"dec")) fmt=Disassembler::Dec;
|
| 108 |
|
|
else if(!strcmp(argv[i],"hex")) fmt=Disassembler::Hex;
|
| 109 |
|
|
else throw std::runtime_error("Unrecognized input format");
|
| 110 |
|
|
formatSpecified=true;
|
| 111 |
|
|
}
|
| 112 |
|
|
else if(!strcmp(argv[i],"-h")||!strcmp(argv[i],"--help")) {
|
| 113 |
|
|
displayUsage(std::cout,argv[0]);
|
| 114 |
|
|
return 0;
|
| 115 |
|
|
}
|
| 116 |
|
|
else if(!strcmp(argv[i],"-na")) {
|
| 117 |
|
|
noAliases=true;
|
| 118 |
|
|
}
|
| 119 |
|
|
else if(!strcmp(argv[i],"-o")) {
|
| 120 |
|
|
if(++i==argc) {
|
| 121 |
|
|
displayUsage(std::cerr,argv[0]);
|
| 122 |
|
|
return EXIT_FAILURE;
|
| 123 |
|
|
}
|
| 124 |
|
|
outputFileName=argv[i];
|
| 125 |
|
|
}
|
| 126 |
|
|
else throw std::runtime_error(std::string("Unrecognized option: \"")+argv[i]+"\"");
|
| 127 |
|
|
}
|
| 128 |
|
|
|
| 129 |
|
|
if(!formatSpecified) { // auto-detect input file format
|
| 130 |
|
|
std::ifstream in(inputFileName,std::ios_base::in|std::ios_base::binary);
|
| 131 |
|
|
fmt=detectInputFormat(in);
|
| 132 |
|
|
}
|
| 133 |
|
|
|
| 134 |
|
|
std::ifstream in;
|
| 135 |
|
|
|
| 136 |
|
|
if(fmt==Disassembler::Bin) in.open(inputFileName,std::ios_base::in|std::ios_base::binary);
|
| 137 |
|
|
else in.open(inputFileName,std::ios_base::in);
|
| 138 |
|
|
if(!in) throw std::runtime_error("Cannot open \""+inputFileName+"\"");
|
| 139 |
|
|
|
| 140 |
|
|
std::ofstream out;
|
| 141 |
|
|
std::ostream *os=&std::cout;
|
| 142 |
|
|
if(!outputFileName.empty()) {
|
| 143 |
|
|
out.open(outputFileName,std::ios_base::out);
|
| 144 |
|
|
if(!out) throw std::runtime_error("Cannot open \""+outputFileName+"\"");
|
| 145 |
|
|
os=&out;
|
| 146 |
|
|
}
|
| 147 |
|
|
|
| 148 |
|
|
auto t=std::time(NULL);
|
| 149 |
|
|
char szTime[256];
|
| 150 |
|
|
auto r=std::strftime(szTime,256,"%c",std::localtime(&t));
|
| 151 |
|
|
if(r==0) szTime[0]='\0';
|
| 152 |
|
|
|
| 153 |
|
|
*os<<"/*"<<std::endl;
|
| 154 |
|
|
*os<<" * Input file: "<<inputFileName<<std::endl;
|
| 155 |
|
|
*os<<" * Input format: ";
|
| 156 |
|
|
|
| 157 |
|
|
switch(fmt) {
|
| 158 |
|
|
case Disassembler::Bin:
|
| 159 |
|
|
*os<<"bin";
|
| 160 |
|
|
break;
|
| 161 |
|
|
case Disassembler::Textio:
|
| 162 |
|
|
*os<<"textio";
|
| 163 |
|
|
break;
|
| 164 |
|
|
case Disassembler::Dec:
|
| 165 |
|
|
*os<<"dec";
|
| 166 |
|
|
break;
|
| 167 |
|
|
case Disassembler::Hex:
|
| 168 |
|
|
*os<<"hex";
|
| 169 |
|
|
break;
|
| 170 |
|
|
default:
|
| 171 |
|
|
break;
|
| 172 |
|
|
}
|
| 173 |
|
|
|
| 174 |
|
|
if(!formatSpecified) *os<<" (autodetected)";
|
| 175 |
|
|
*os<<std::endl;
|
| 176 |
|
|
*os<<" * Base address: 0x"<<Disassembler::hex(base)<<std::endl;
|
| 177 |
|
|
*os<<" * Disassembled by lxp32dump at "<<szTime<<std::endl;
|
| 178 |
|
|
*os<<" */"<<std::endl<<std::endl;
|
| 179 |
|
|
|
| 180 |
|
|
Disassembler disasm(in,*os);
|
| 181 |
|
|
disasm.setFormat(fmt);
|
| 182 |
|
|
disasm.setBase(base);
|
| 183 |
|
|
disasm.setPreferAliases(!noAliases);
|
| 184 |
|
|
|
| 185 |
|
|
try {
|
| 186 |
|
|
disasm.dump();
|
| 187 |
|
|
}
|
| 188 |
|
|
catch(std::exception &) {
|
| 189 |
|
|
if(!outputFileName.empty()) {
|
| 190 |
|
|
out.close();
|
| 191 |
|
|
std::remove(outputFileName.c_str());
|
| 192 |
|
|
}
|
| 193 |
|
|
throw;
|
| 194 |
|
|
}
|
| 195 |
|
|
}
|
| 196 |
|
|
catch(std::exception &ex) {
|
| 197 |
|
|
std::cerr<<"Error: "<<ex.what()<<std::endl;
|
| 198 |
|
|
return EXIT_FAILURE;
|
| 199 |
|
|
}
|