UnivesalDisassembler(2003)

instrset.h

Go to the documentation of this file.
00001 // instrset.h
00002 //
00003 // uda - universal disassembler
00004 // Date: 2003-05-04
00005 //
00006 // Copyright (c) 2002-2003 Ales Smrcka 
00007 //
00008 // This program is licensed under GNU Library GPL. See the file COPYING.
00009 //
00010 // An interface of loading instruction set from file and creating tables and
00011 // variables. This file including builtin tables in namespace BuiltinTables.
00012 // Tables, Table, TableItem include core of decoding.
00013 
00014 #ifndef instrsetH
00015 #define instrsetH
00016 
00017 #include <iomanip>
00018 #include <iostream>
00019 #include <fstream>
00020 #include <map>
00021 #include <string>
00022 #include "shared.h"
00023 #include "udaclasses.h"
00024 #include "sections.h"
00025 using namespace std;
00026 
00027 class InstrSetException;
00028 class  ScannerException;
00029 class  ParserException;
00030 class  InstructionException;
00031 
00032 class InstructionSet;
00033 // Scanner
00034 class Scanner;
00035 // Parser ist. map
00036 class Parser;
00037 // Tables of instructions map. Map of Table
00038 class Tables;
00039 // Abstract table, parent of BuildinTable and Table
00040 class AbstractTable;
00041 // Builtin table
00042 class  BuiltinTable;
00043 // One table of Tables including TableItem
00044 class  Table;
00045 // Item of Table
00046 class TableItem;
00047 // Map of Variable
00048 class Variables;
00049 // Item of Variables
00050 class Variable;
00051 
00052 extern const string instrsetdirectory;
00053 extern const string defaultinstrset;
00055 extern InstructionSet instructionset;
00056 
00057 class InstrSetException : public Exception {
00058 protected:
00059     int line;
00060 public:
00061     InstrSetException(string s, int l=0) : Exception(s) { line=l; }
00062     virtual ~InstrSetException() {}
00063     virtual void Print() { cerr<<"Instruction set error at line "<<line<<" : "<<value<<endl; }
00064 };
00065 
00066 class ScannerException : public InstrSetException {
00067 public:
00068     ScannerException(string s, int l) : InstrSetException(s, l) {}
00069     virtual ~ScannerException() {}
00070     void Print() { cerr<<"Scanner error at line "<<line<<" : "<<value<<endl; }
00071 };
00072 
00073 class ParserException : public InstrSetException {
00074 public:
00075     ParserException(string s, int l) : InstrSetException(s, l) {}
00076     virtual ~ParserException() {}
00077     void Print() { cerr<<"Parser error at line "<<line<<" : "<<value<<endl; }
00078 };
00079 
00080 class InstructionException : public InstrSetException {
00081 public:
00082     string text;
00083     InstructionException(string s, string t) : InstrSetException(s) { text=t; }
00084     virtual ~InstructionException() {}
00085     void Print() { cerr<<"Instruction decoder error: "<<value<<": "<<text<<endl; }
00086 };
00087 
00089 class InstructionSet {
00090     InstructionSet(const InstructionSet&) {}
00091 public:
00092     Tables *tables;
00093     Variables *variables;
00095     InstructionSet();
00097     ~InstructionSet();
00099     int LoadFile(const string filename);
00101     void Load(const string processor);
00103     void Clear();
00104 };
00105 
00107 enum tk_type {
00109     tk_end=0,
00111     tk_number,
00113     tk_text,
00115     tk_ident,
00117     tk_equal,
00119     tk_plus,
00121     tk_comma,
00123     tk_semicol,
00125     tk_parop,
00127     tk_parcl
00128 };
00129 
00131 class Scanner {
00132     Scanner();
00133     int unget; //unget char
00134     int _line;
00135 protected:
00136     const char *_filename;
00137     ifstream input; //input stream from file with instruction set
00138     int getc();
00139     void ungetc(int c);
00140 public:
00142     class Token {
00143     public:
00145         tk_type type;
00147         string str_value;
00149         unsigned int_value;
00150         Token(tk_type t=tk_end, unsigned i=0) { type=t; int_value=i; }
00151         operator tk_type() { return type; }
00152     };
00154     int line() { return _line; }
00156     Scanner(const char *filename);
00158     Token Scan();
00159 };
00160 
00162 class Parser {
00163     Scanner &scanner;
00164     Tables &tables;
00165     Variables &variables;
00166     Parser();
00167     void Error();
00168 public:
00170     Parser(Scanner &sc, Tables &t, Variables &v) : scanner(sc), tables(t), variables(v) {}
00172     void Parse();
00173 };
00174 
00175 typedef map<string, AbstractTable*> MTables;
00176 
00178 class Tables {
00179     MTables _tables;
00180 public:
00181     Variables *variables;
00182     Tables(Variables *v) { variables=v; }
00183     ~Tables();
00185     Table *Add(string name);
00187     AbstractTable *&Add(string name, AbstractTable *table);
00189     AbstractTable *&operator [](string index);
00191     void Reset();
00193     void Clear();
00194 };
00195 
00197 class AbstractTable {
00198 public:
00199     virtual ~AbstractTable() {}
00201     virtual string Decode(Section &data, Address addr, unsigned &size);
00203     virtual void Reset() {}
00204 };
00205 
00207 class BuiltinTable : public AbstractTable {};
00208 
00210 class Table : public AbstractTable {
00211     TableItem *_items[256];
00212 public:
00213     Tables *tables;
00214     Variables *variables;
00215     Table(Tables *t=NULL, Variables *v=NULL); //for all items: item=NULL
00216     virtual ~Table(); //free all items
00217     TableItem *Add(unsigned mask, unsigned tag, string value, string length);
00219     string Decode(Section &data, Address addr, unsigned &size);
00220 //    TableItem &operator [](unsigned index) { return *_items[index]; }
00222     void Reset();
00223 };
00224 
00226 class TableItem {
00227     void Error(const char *c=NULL);
00229     int CountLength(unsigned lens[]);
00231     int CountLength(unsigned lens[], string len);
00232     // Decodes the text (c) with others tables and variables. Fills the length array.
00233     // lindex is actual index to length array. c is the text. is terminated character.
00234     // Returns substituted string.
00235     string Norm(Section &data, Address addr, unsigned lens[], unsigned &lindex, const char *&c, char t=0);
00236 public:
00237     Tables &tables;
00238     Variables &variables;
00239     string value;
00240     struct Lengths {
00241         unsigned first_value;
00242         bool valid[26];
00243     } lengths;
00244     TableItem(Tables &t, Variables &v, string value, string lengthstr);
00246     string Decode(Section &data, Address addr, unsigned &size);
00247 };
00248 
00249 typedef map<string, Variable> MVariables;
00250 
00252 class Variables {
00253     MVariables _variables;
00254 public:
00256     Variable &Add(string name, string defaultvalue="");
00258     Variable &operator [](string index);
00260     void Reset();
00262     void Clear();
00263 };
00264 
00266 class Variable {
00267 public:
00268     string value;
00269     string defaultvalue;
00270     Variable(string defaultvalue="") { Variable::value=defaultvalue; Variable::defaultvalue=defaultvalue; }
00271     void Reset() { value=defaultvalue; }
00272     operator string() { return value; }
00273 };
00274 
00275 namespace BuiltinTables {
00276 
00278 //
00279 // Little Endian - Immediate data
00280 //
00282 
00284 class Ib : public BuiltinTable {
00285 public:
00286     virtual string Decode(Section &data, Address addr, unsigned &size);
00287 };
00289 class Iw : public BuiltinTable {
00290 public:
00291     virtual string Decode(Section &data, Address addr, unsigned &size);
00292 };
00294 class Id : public BuiltinTable {
00295 public:
00296     virtual string Decode(Section &data, Address addr, unsigned &size);
00297 };
00299 class Ibw : public BuiltinTable {
00300 public:
00301     virtual string Decode(Section &data, Address addr, unsigned &size);
00302 };
00304 class Ibd : public BuiltinTable {
00305 public:
00306     virtual string Decode(Section &data, Address addr, unsigned &size);
00307 };
00308 
00310 //
00311 // Little Endian - Signed Immediate daat
00312 //
00314 
00316 class SIb : public BuiltinTable {
00317 public:
00318     virtual string Decode(Section &data, Address addr, unsigned &size);
00319 };
00321 class SIw : public BuiltinTable {
00322 public:
00323     virtual string Decode(Section &data, Address addr, unsigned &size);
00324 };
00326 class SId : public BuiltinTable {
00327 public:
00328     virtual string Decode(Section &data, Address addr, unsigned &size);
00329 };
00331 class SIbw : public BuiltinTable {
00332 public:
00333     virtual string Decode(Section &data, Address addr, unsigned &size);
00334 };
00336 class SIbd : public BuiltinTable {
00337 public:
00338     virtual string Decode(Section &data, Address addr, unsigned &size);
00339 };
00340 
00342 //
00343 // Little Endian - Relative address
00344 //
00346 
00348 class Jb : public BuiltinTable {
00349 public:
00350     virtual string Decode(Section &data, Address addr, unsigned &size);
00351 };
00353 class Jw : public BuiltinTable {
00354 public:
00355     virtual string Decode(Section &data, Address addr, unsigned &size);
00356 };
00358 class Jd : public BuiltinTable {
00359 public:
00360     virtual string Decode(Section &data, Address addr, unsigned &size);
00361 };
00362 
00364 //
00365 // Little Endian - Absolute address
00366 //
00368 
00370 class Apw : public BuiltinTable {
00371 public:
00372     virtual string Decode(Section &data, Address addr, unsigned &size);
00373 };
00375 class Apd : public BuiltinTable {
00376 public:
00377     virtual string Decode(Section &data, Address addr, unsigned &size);
00378 };
00380 class Aw : public BuiltinTable {
00381 public:
00382     virtual string Decode(Section &data, Address addr, unsigned &size);
00383 };
00385 class Ad : public BuiltinTable {
00386 public:
00387     virtual string Decode(Section &data, Address addr, unsigned &size);
00388 };
00389 
00391 //
00392 // Big Endian - Immediate data
00393 //
00395 
00397 class BIb : public BuiltinTable {
00398 public:
00399     virtual string Decode(Section &data, Address addr, unsigned &size);
00400 };
00402 class BIw : public BuiltinTable {
00403 public:
00404     virtual string Decode(Section &data, Address addr, unsigned &size);
00405 };
00407 class BId : public BuiltinTable {
00408 public:
00409     virtual string Decode(Section &data, Address addr, unsigned &size);
00410 };
00412 class BIbw : public BuiltinTable {
00413 public:
00414     virtual string Decode(Section &data, Address addr, unsigned &size);
00415 };
00417 class BIbd : public BuiltinTable {
00418 public:
00419     virtual string Decode(Section &data, Address addr, unsigned &size);
00420 };
00421 
00423 //
00424 // Big Endian - Signed Immediate daat
00425 //
00427 
00429 class SBIb : public BuiltinTable {
00430 public:
00431     virtual string Decode(Section &data, Address addr, unsigned &size);
00432 };
00434 class SBIw : public BuiltinTable {
00435 public:
00436     virtual string Decode(Section &data, Address addr, unsigned &size);
00437 };
00439 class SBId : public BuiltinTable {
00440 public:
00441     virtual string Decode(Section &data, Address addr, unsigned &size);
00442 };
00444 class SBIbw : public BuiltinTable {
00445 public:
00446     virtual string Decode(Section &data, Address addr, unsigned &size);
00447 };
00449 class SBIbd : public BuiltinTable {
00450 public:
00451     virtual string Decode(Section &data, Address addr, unsigned &size);
00452 };
00453 
00455 //
00456 // Big Endian - Relative address
00457 //
00459 
00461 class BJb : public BuiltinTable {
00462 public:
00463     virtual string Decode(Section &data, Address addr, unsigned &size);
00464 };
00466 class BJw : public BuiltinTable {
00467 public:
00468     virtual string Decode(Section &data, Address addr, unsigned &size);
00469 };
00471 class BJd : public BuiltinTable {
00472 public:
00473     virtual string Decode(Section &data, Address addr, unsigned &size);
00474 };
00475 
00477 //
00478 // Big Endian - Absolute address
00479 //
00481 
00483 class BApw : public BuiltinTable {
00484 public:
00485     virtual string Decode(Section &data, Address addr, unsigned &size);
00486 };
00488 class BApd : public BuiltinTable {
00489 public:
00490     virtual string Decode(Section &data, Address addr, unsigned &size);
00491 };
00493 class BAw : public BuiltinTable {
00494 public:
00495     virtual string Decode(Section &data, Address addr, unsigned &size);
00496 };
00498 class BAd : public BuiltinTable {
00499 public:
00500     virtual string Decode(Section &data, Address addr, unsigned &size);
00501 };
00502 
00503 } //namespace BuiltinTables
00504 #endif