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