254644106ce3da0cde6287054ba376d7383c5d7b
[calu.git] / 3c_disasm / dasm.cpp
1 #include <iostream>
2 #include <fstream>
3 #include <boost/tokenizer.hpp>
4 #include <boost/program_options.hpp>
5 #include <boost/lexical_cast.hpp>
6 #include <string>
7 #include <map>
8 #include "disasm.h"
9
10
11 #include "CInstrFactory.hpp"
12
13 using boost::lexical_cast;
14 using boost::bad_lexical_cast;
15
16 using namespace std;
17
18 using namespace boost::program_options;
19 namespace po = boost::program_options;
20
21 std::string progName;
22
23
24 CCpu* Iinstr::m_cpu;
25 disasm* Iinstr::m_disasm;
26
27 int main(int argc, char* argv[])
28 {
29         progName = argv[0];
30         ifstream inFile;
31         try {
32                 po::options_description desc("Allowed options");
33                 desc.add_options()
34                 ("help,h","produce help message")
35                 ("file,f",value<string>(), "input file")
36                 ;
37
38                 po::positional_options_description p;
39                 p.add("file",1);
40
41                 po::variables_map vm;
42                 po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
43                 po::notify(vm);
44
45                 if(vm.count("help")) {
46                         cout << desc << endl;
47                         return EXIT_FAILURE;
48                 }
49
50                 if(vm.count("file")) {
51 #ifdef DEBUG 
52                         cout << "going to open file " << vm["file"].as<string>() << endl;
53 #endif
54                         inFile.open(vm["file"].as<string>(), ios::in);
55                         if(!inFile) {
56                                 cerr << "Error opening file " << vm["file"].as<string>() << endl;
57                                 return EXIT_FAILURE;
58                         }
59                 }
60                 else {
61                         cout << "not input file given!" << endl << endl;
62                         cout << desc << endl;
63                         return EXIT_FAILURE;
64                 }
65         }
66         catch(std::exception& ex) {
67                 cout << ex.what() << endl;
68         }
69
70
71         string dir = "./instr/";
72
73         map<short, Iinstr*> instr;
74
75         CInstrFactory instrFab;
76         try {
77                 instrFab.loadLibsIntoMap(instr, dir);
78         }
79         catch(std::bad_alloc& e) {
80                 cerr << progName << ": bad_alloc caught " << e.what() << endl;
81                 exit(EXIT_FAILURE); 
82         }
83
84
85
86
87         std::string str = "";
88         boost::char_separator<char> sep(";", "", boost::keep_empty_tokens);
89         boost::tokenizer<boost::char_separator<char> > tokens(str, sep);
90         disasm disasm(instr);
91         while(getline(inFile, str)) {
92                 int count = 0;
93                 tokens.assign(str);
94                 stringstream out;
95                 int type = 0;
96                 for(auto tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) {
97                         if(tok_iter == tokens.begin()) {
98                                 try {
99                                         type = lexical_cast<int>(*tok_iter);
100                                         count++;
101                                         continue;
102                                 }
103                                 catch(bad_lexical_cast &) {
104                                         break;
105                                 }
106                                 cout << endl;
107                         }
108                         switch(type) {
109                                 case 2:
110                                         if(count == 1) {
111                                                 cout << "; ";
112                                         }
113                                         cout << *tok_iter;
114                                         break;
115                                 case 3:
116                                         if((*tok_iter).size() > 0) {
117                                                 if(count > 1) {
118                                                         cout << endl;
119                                                 }
120                                                 cout << *tok_iter << ":";
121                                         }
122                                         break;
123                                 case 1:
124                                         if(count == 1) {
125                                                 out << "[0x" << *tok_iter << "]: ";
126                                         }
127                                         else if(count == 2) {
128                                                 out << disasm.decodeToString(*tok_iter);
129                                         }
130                                         else if(count == 3) {
131                                                 //code saved in hex-file
132                                                 //cout << *tok_iter ;
133                                         }
134                                         else if(count == 4) {
135                                                 /* label */
136                                                 if((*tok_iter).size() > 0) {
137                                                         cout << *tok_iter << ":" << endl;
138                                                 }
139                                                 cout << out.str();
140                                         }
141                                         else if(count == 5) {
142                                                 if((*tok_iter).size() > 0) {
143                                                         cout << " ;";
144                                                 }
145                                         }
146
147                                         if(count >= 5) {
148                                                 cout << *tok_iter;
149                                         }
150                                         break;
151                                 default:
152                                         cerr << "i was to lazy to implement the other format types for now" << endl;
153                         }
154                         count++;
155                 }
156                 if(type == 1 && count <= 4) {
157                         cout << out.str();
158                 }
159                 cout << endl;
160         }
161         inFile.close();
162
163         cout << endl;
164         return EXIT_SUCCESS;
165 }