OpenCores
URL https://opencores.org/ocsvn/lxp32/lxp32/trunk

Subversion Repositories lxp32

[/] [lxp32/] [trunk/] [tools/] [src/] [lxp32dump/] [main.cpp] - Diff between revs 2 and 6

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 2 Rev 6
/*
/*
 * Copyright (c) 2016 by Alex I. Kuznetsov.
 * Copyright (c) 2016 by Alex I. Kuznetsov.
 *
 *
 * Part of the LXP32 CPU IP core.
 * Part of the LXP32 CPU IP core.
 *
 *
 * Main translation unit for the LXP32 disassembler.
 * Main translation unit for the LXP32 disassembler.
 */
 */
 
 
#ifdef _MSC_VER
#ifdef _MSC_VER
        #define _CRT_SECURE_NO_WARNINGS
        #define _CRT_SECURE_NO_WARNINGS
#endif
#endif
 
 
#include "disassembler.h"
#include "disassembler.h"
 
 
#include <iostream>
#include <iostream>
#include <fstream>
#include <fstream>
#include <string>
#include <string>
#include <stdexcept>
#include <stdexcept>
#include <cstring>
#include <cstring>
#include <cstdlib>
#include <cstdlib>
#include <cstdio>
#include <cstdio>
#include <ctime>
#include <ctime>
 
 
static void displayUsage(std::ostream &os,const char *program) {
static void displayUsage(std::ostream &os,const char *program) {
        os<<std::endl;
        os<<std::endl;
        os<<"Usage:"<<std::endl;
        os<<"Usage:"<<std::endl;
        os<<"    "<<program<<" [ option(s) | input file ]"<<std::endl<<std::endl;
        os<<"    "<<program<<" [ option(s) | input file ]"<<std::endl<<std::endl;
 
 
        os<<"Options:"<<std::endl;
        os<<"Options:"<<std::endl;
        os<<"    -b <addr>    Base address (for comments only)"<<std::endl;
        os<<"    -b <addr>    Base address (for comments only)"<<std::endl;
        os<<"    -f <fmt>     Input format (bin, textio, dec, hex), default: autodetect"<<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<<"    -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<<"    -o <file>    Output file name, default: standard output"<<std::endl;
        os<<"    --           Do not interpret subsequent arguments as options"<<std::endl;
        os<<"    --           Do not interpret subsequent arguments as options"<<std::endl;
}
}
 
 
static Disassembler::Format detectInputFormat(std::istream &in) {
static Disassembler::Format detectInputFormat(std::istream &in) {
        static const std::size_t Size=256;
        static const std::size_t Size=256;
        static const char *textio="01\r\n \t";
        static const char *textio="01\r\n \t";
        static const char *dec="0123456789\r\n \t";
        static const char *dec="0123456789\r\n \t";
        static const char *hex="0123456789ABCDEFabcdef\r\n \t";
        static const char *hex="0123456789ABCDEFabcdef\r\n \t";
 
 
        char buf[Size];
        char buf[Size];
        in.read(buf,Size);
        in.read(buf,Size);
        auto s=static_cast<std::size_t>(in.gcount());
        auto s=static_cast<std::size_t>(in.gcount());
        in.clear();
        in.clear();
        in.seekg(0);
        in.seekg(0);
 
 
        Disassembler::Format fmt=Disassembler::Textio;
        Disassembler::Format fmt=Disassembler::Textio;
 
 
        for(std::size_t i=0;i<s;i++) {
        for(std::size_t i=0;i<s;i++) {
                if(fmt==Disassembler::Textio&&!strchr(textio,buf[i])) fmt=Disassembler::Dec;
                if(fmt==Disassembler::Textio&&!strchr(textio,buf[i])) fmt=Disassembler::Dec;
                if(fmt==Disassembler::Dec&&!strchr(dec,buf[i])) fmt=Disassembler::Hex;
                if(fmt==Disassembler::Dec&&!strchr(dec,buf[i])) fmt=Disassembler::Hex;
                if(fmt==Disassembler::Hex&&!strchr(hex,buf[i])) {
                if(fmt==Disassembler::Hex&&!strchr(hex,buf[i])) {
                        fmt=Disassembler::Bin;
                        fmt=Disassembler::Bin;
                        break;
                        break;
                }
                }
        }
        }
 
 
        return fmt;
        return fmt;
}
}
 
 
int main(int argc,char *argv[]) try {
int main(int argc,char *argv[]) try {
        std::string inputFileName,outputFileName;
        std::string inputFileName,outputFileName;
 
 
        std::cerr<<"LXP32 Platform Disassembler"<<std::endl;
        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;
        Disassembler::Format fmt=Disassembler::Bin;
        bool noMoreOptions=false;
        bool noMoreOptions=false;
        bool formatSpecified=false;
        bool formatSpecified=false;
        Disassembler::Word base=0;
        Disassembler::Word base=0;
 
        bool noAliases=false;
 
 
        if(argc<=1) {
        if(argc<=1) {
                displayUsage(std::cout,argv[0]);
                displayUsage(std::cout,argv[0]);
                return 0;
                return 0;
        }
        }
 
 
        for(int i=1;i<argc;i++) {
        for(int i=1;i<argc;i++) {
                if(argv[i][0]!='-'||noMoreOptions) {
                if(argv[i][0]!='-'||noMoreOptions) {
                        if(inputFileName.empty()) inputFileName=argv[i];
                        if(inputFileName.empty()) inputFileName=argv[i];
                        else throw std::runtime_error("Only one input file name can be specified");
                        else throw std::runtime_error("Only one input file name can be specified");
                }
                }
                else if(!strcmp(argv[i],"--")) noMoreOptions=true;
                else if(!strcmp(argv[i],"--")) noMoreOptions=true;
                else if(!strcmp(argv[i],"-b")) {
                else if(!strcmp(argv[i],"-b")) {
                        if(++i==argc) {
                        if(++i==argc) {
                                displayUsage(std::cerr,argv[0]);
                                displayUsage(std::cerr,argv[0]);
                                return EXIT_FAILURE;
                                return EXIT_FAILURE;
                        }
                        }
                        try {
                        try {
                                base=std::stoul(argv[i],nullptr,0);
                                base=std::stoul(argv[i],nullptr,0);
                                if(base%4!=0) throw std::exception();
                                if(base%4!=0) throw std::exception();
                        }
                        }
                        catch(std::exception &) {
                        catch(std::exception &) {
                                throw std::runtime_error("Invalid base address");
                                throw std::runtime_error("Invalid base address");
                        }
                        }
                }
                }
                else if(!strcmp(argv[i],"-f")) {
                else if(!strcmp(argv[i],"-f")) {
                        if(++i==argc) {
                        if(++i==argc) {
                                displayUsage(std::cerr,argv[0]);
                                displayUsage(std::cerr,argv[0]);
                                return EXIT_FAILURE;
                                return EXIT_FAILURE;
                        }
                        }
                        if(!strcmp(argv[i],"bin")) fmt=Disassembler::Bin;
                        if(!strcmp(argv[i],"bin")) fmt=Disassembler::Bin;
                        else if(!strcmp(argv[i],"textio")) fmt=Disassembler::Textio;
                        else if(!strcmp(argv[i],"textio")) fmt=Disassembler::Textio;
                        else if(!strcmp(argv[i],"dec")) fmt=Disassembler::Dec;
                        else if(!strcmp(argv[i],"dec")) fmt=Disassembler::Dec;
                        else if(!strcmp(argv[i],"hex")) fmt=Disassembler::Hex;
                        else if(!strcmp(argv[i],"hex")) fmt=Disassembler::Hex;
                        else throw std::runtime_error("Unrecognized input format");
                        else throw std::runtime_error("Unrecognized input format");
                        formatSpecified=true;
                        formatSpecified=true;
                }
                }
                else if(!strcmp(argv[i],"-h")||!strcmp(argv[i],"--help")) {
                else if(!strcmp(argv[i],"-h")||!strcmp(argv[i],"--help")) {
                        displayUsage(std::cout,argv[0]);
                        displayUsage(std::cout,argv[0]);
                        return 0;
                        return 0;
                }
                }
 
                else if(!strcmp(argv[i],"-na")) {
 
                        noAliases=true;
 
                }
                else if(!strcmp(argv[i],"-o")) {
                else if(!strcmp(argv[i],"-o")) {
                        if(++i==argc) {
                        if(++i==argc) {
                                displayUsage(std::cerr,argv[0]);
                                displayUsage(std::cerr,argv[0]);
                                return EXIT_FAILURE;
                                return EXIT_FAILURE;
                        }
                        }
                        outputFileName=argv[i];
                        outputFileName=argv[i];
                }
                }
                else throw std::runtime_error(std::string("Unrecognized option: \"")+argv[i]+"\"");
                else throw std::runtime_error(std::string("Unrecognized option: \"")+argv[i]+"\"");
        }
        }
 
 
        if(!formatSpecified) { // auto-detect input file format
        if(!formatSpecified) { // auto-detect input file format
                std::ifstream in(inputFileName,std::ios_base::in|std::ios_base::binary);
                std::ifstream in(inputFileName,std::ios_base::in|std::ios_base::binary);
                fmt=detectInputFormat(in);
                fmt=detectInputFormat(in);
        }
        }
 
 
        std::ifstream in;
        std::ifstream in;
 
 
        if(fmt==Disassembler::Bin) in.open(inputFileName,std::ios_base::in|std::ios_base::binary);
        if(fmt==Disassembler::Bin) in.open(inputFileName,std::ios_base::in|std::ios_base::binary);
        else in.open(inputFileName,std::ios_base::in);
        else in.open(inputFileName,std::ios_base::in);
        if(!in) throw std::runtime_error("Cannot open \""+inputFileName+"\"");
        if(!in) throw std::runtime_error("Cannot open \""+inputFileName+"\"");
 
 
        std::ofstream out;
        std::ofstream out;
        std::ostream *os=&std::cout;
        std::ostream *os=&std::cout;
        if(!outputFileName.empty()) {
        if(!outputFileName.empty()) {
                out.open(outputFileName,std::ios_base::out);
                out.open(outputFileName,std::ios_base::out);
                if(!out) throw std::runtime_error("Cannot open \""+outputFileName+"\"");
                if(!out) throw std::runtime_error("Cannot open \""+outputFileName+"\"");
                os=&out;
                os=&out;
        }
        }
 
 
        auto t=std::time(NULL);
        auto t=std::time(NULL);
        char szTime[256];
        char szTime[256];
        auto r=std::strftime(szTime,256,"%c",std::localtime(&t));
        auto r=std::strftime(szTime,256,"%c",std::localtime(&t));
        if(r==0) szTime[0]='\0';
        if(r==0) szTime[0]='\0';
 
 
        *os<<"/*"<<std::endl;
        *os<<"/*"<<std::endl;
        *os<<" * Input file: "<<inputFileName<<std::endl;
        *os<<" * Input file: "<<inputFileName<<std::endl;
        *os<<" * Input format: ";
        *os<<" * Input format: ";
 
 
        switch(fmt) {
        switch(fmt) {
        case Disassembler::Bin:
        case Disassembler::Bin:
                *os<<"bin";
                *os<<"bin";
                break;
                break;
        case Disassembler::Textio:
        case Disassembler::Textio:
                *os<<"textio";
                *os<<"textio";
                break;
                break;
        case Disassembler::Dec:
        case Disassembler::Dec:
                *os<<"dec";
                *os<<"dec";
                break;
                break;
        case Disassembler::Hex:
        case Disassembler::Hex:
                *os<<"hex";
                *os<<"hex";
                break;
                break;
        default:
        default:
                break;
                break;
        }
        }
 
 
        if(!formatSpecified) *os<<" (autodetected)";
        if(!formatSpecified) *os<<" (autodetected)";
        *os<<std::endl;
        *os<<std::endl;
        *os<<" * Base address: 0x"<<Disassembler::hex(base)<<std::endl;
        *os<<" * Base address: 0x"<<Disassembler::hex(base)<<std::endl;
        *os<<" * Disassembled by lxp32dump at "<<szTime<<std::endl;
        *os<<" * Disassembled by lxp32dump at "<<szTime<<std::endl;
        *os<<" */"<<std::endl<<std::endl;
        *os<<" */"<<std::endl<<std::endl;
 
 
        Disassembler disasm(in,*os);
        Disassembler disasm(in,*os);
        disasm.setFormat(fmt);
        disasm.setFormat(fmt);
        disasm.setBase(base);
        disasm.setBase(base);
 
        disasm.setPreferAliases(!noAliases);
 
 
        try {
        try {
                disasm.dump();
                disasm.dump();
        }
        }
        catch(std::exception &) {
        catch(std::exception &) {
                if(!outputFileName.empty()) {
                if(!outputFileName.empty()) {
                        out.close();
                        out.close();
                        std::remove(outputFileName.c_str());
                        std::remove(outputFileName.c_str());
                }
                }
                throw;
                throw;
        }
        }
}
}
catch(std::exception &ex) {
catch(std::exception &ex) {
        std::cerr<<"Error: "<<ex.what()<<std::endl;
        std::cerr<<"Error: "<<ex.what()<<std::endl;
        return EXIT_FAILURE;
        return EXIT_FAILURE;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.