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