dcc.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*****************************************************************************
  2. * dcc decompiler
  3. * Reads the command line switches and then executes each major section in turn
  4. * (C) Cristina Cifuentes
  5. ****************************************************************************/
  6. #include <cstring>
  7. #include "dcc.h"
  8. #include "project.h"
  9. /* Global variables - extern to other modules */
  10. extern char *asm1_name, *asm2_name; /* Assembler output filenames */
  11. extern SYMTAB symtab; /* Global symbol table */
  12. extern STATS stats; /* cfg statistics */
  13. //PROG prog; /* programs fields */
  14. extern OPTION option; /* Command line options */
  15. //Function * pProcList; /* List of procedures, topologically sort */
  16. //Function * pLastProc; /* Pointer to last node in procedure list */
  17. //FunctionListType pProcList;
  18. //CALL_GRAPH *callGraph; /* Call graph of the program */
  19. static char *initargs(int argc, char *argv[]);
  20. static void displayTotalStats(void);
  21. #include <llvm/Support/raw_os_ostream.h>
  22. #include <llvm/Support/CommandLine.h>
  23. #include <llvm/Support/TargetSelect.h>
  24. #include <llvm/Support/TargetRegistry.h>
  25. #include <llvm/Support/PrettyStackTrace.h>
  26. #include <llvm/Support/Signals.h>
  27. #include <llvm/Support/Host.h>
  28. #include <llvm/Target/TargetMachine.h>
  29. #include <llvm/Target/TargetInstrInfo.h>
  30. #include <llvm/MC/MCAsmInfo.h>
  31. #include <llvm/CodeGen/MachineInstrBuilder.h>
  32. #include <llvm/TableGen/Main.h>
  33. #include <llvm/TableGen/TableGenAction.h>
  34. #include <llvm/TableGen/Record.h>
  35. /****************************************************************************
  36. * main
  37. ***************************************************************************/
  38. #include <iostream>
  39. using namespace llvm;
  40. class TVisitor : public TableGenAction {
  41. public:
  42. virtual bool operator()(raw_ostream &OS, RecordKeeper &Records)
  43. {
  44. Record *rec = Records.getDef("ADD8i8");
  45. if(rec)
  46. {
  47. if(not rec->getTemplateArgs().empty())
  48. std::cout << "Has template args\n";
  49. auto classes(rec->getSuperClasses());
  50. for(auto val : rec->getSuperClasses())
  51. std::cout << "Super "<<val->getName()<<"\n";
  52. // DagInit * in = rec->getValueAsDag(val.getName());
  53. // in->dump();
  54. for(const RecordVal &val : rec->getValues())
  55. {
  56. // val.dump();
  57. }
  58. rec->dump();
  59. }
  60. // rec = Records.getDef("CCR");
  61. // if(rec)
  62. // rec->dump();
  63. for(auto val : Records.getDefs())
  64. {
  65. //std::cout<< "Def "<<val.first<<"\n";
  66. }
  67. return false;
  68. }
  69. };
  70. int testTblGen(int argc, char **argv)
  71. {
  72. using namespace llvm;
  73. sys::PrintStackTraceOnErrorSignal();
  74. PrettyStackTraceProgram(argc,argv);
  75. cl::ParseCommandLineOptions(argc,argv);
  76. TVisitor tz;
  77. return llvm::TableGenMain(argv[0],tz);
  78. InitializeNativeTarget();
  79. Triple TheTriple;
  80. std::string def = sys::getDefaultTargetTriple();
  81. std::string MCPU="i386";
  82. std::string MARCH="x86";
  83. InitializeAllTargetInfos();
  84. InitializeAllTargetMCs();
  85. InitializeAllAsmPrinters();
  86. InitializeAllAsmParsers();
  87. InitializeAllDisassemblers();
  88. std::string TargetTriple("i386-pc-linux-gnu");
  89. TheTriple = Triple(Triple::normalize(TargetTriple));
  90. MCOperand op=llvm::MCOperand::CreateImm(11);
  91. MCAsmInfo info;
  92. raw_os_ostream wrap(std::cerr);
  93. op.print(wrap,&info);
  94. wrap.flush();
  95. std::cerr<<"\n";
  96. std::string lookuperr;
  97. TargetRegistry::printRegisteredTargetsForVersion();
  98. const Target *t = TargetRegistry::lookupTarget(MARCH,TheTriple,lookuperr);
  99. TargetOptions opts;
  100. std::string Features;
  101. opts.PrintMachineCode=1;
  102. TargetMachine *tm = t->createTargetMachine(TheTriple.getTriple(),MCPU,Features,opts);
  103. std::cerr<<tm->getInstrInfo()->getName(97)<<"\n";
  104. const MCInstrDesc &ds(tm->getInstrInfo()->get(97));
  105. const MCOperandInfo *op1=ds.OpInfo;
  106. uint16_t impl_def = ds.getImplicitDefs()[0];
  107. std::cerr<<lookuperr<<"\n";
  108. exit(0);
  109. }
  110. int main(int argc, char **argv)
  111. {
  112. /* Extract switches and filename */
  113. strcpy(option.filename, initargs(argc, argv));
  114. /* Front end reads in EXE or COM file, parses it into I-code while
  115. * building the call graph and attaching appropriate bits of code for
  116. * each procedure.
  117. */
  118. DccFrontend fe(option.filename);
  119. if(false==fe.FrontEnd ())
  120. return -1;
  121. if(option.asm1)
  122. return 0;
  123. /* In the middle is a so called Universal Decompiling Machine.
  124. * It processes the procedure list and I-code and attaches where it can
  125. * to each procedure an optimised cfg and ud lists
  126. */
  127. udm();
  128. if(option.asm2)
  129. return 0;
  130. /* Back end converts each procedure into C using I-code, interval
  131. * analysis, data flow etc. and outputs it to output file ready for
  132. * re-compilation.
  133. */
  134. BackEnd(asm1_name ? asm1_name:option.filename, Project::get()->callGraph);
  135. Project::get()->callGraph->write();
  136. if (option.Stats)
  137. displayTotalStats();
  138. /*
  139. freeDataStructures(pProcList);
  140. */
  141. return 0;
  142. }
  143. /****************************************************************************
  144. * initargs - Extract command line arguments
  145. ***************************************************************************/
  146. static char *initargs(int argc, char *argv[])
  147. {
  148. char *pc;
  149. while (--argc > 0 && (*++argv)[0] == '-')
  150. {
  151. for (pc = argv[0]+1; *pc; pc++)
  152. switch (*pc)
  153. {
  154. case 'a': /* Print assembler listing */
  155. if (*(pc+1) == '2')
  156. option.asm2 = true;
  157. else
  158. option.asm1 = true;
  159. if (*(pc+1) == '1' || *(pc+1) == '2')
  160. pc++;
  161. break;
  162. case 'c':
  163. option.Calls = true;
  164. break;
  165. case 'i':
  166. option.Interact = true;
  167. break;
  168. case 'm': /* Print memory map */
  169. option.Map = true;
  170. break;
  171. case 's': /* Print Stats */
  172. option.Stats = true;
  173. break;
  174. case 'V': /* Very verbose => verbose */
  175. option.VeryVerbose = true;
  176. case 'v':
  177. option.verbose = true; /* Make everything verbose */
  178. break;
  179. case 'o': /* assembler output file */
  180. if (*(pc+1)) {
  181. asm1_name = asm2_name = pc+1;
  182. goto NextArg;
  183. }
  184. else if (--argc > 0) {
  185. asm1_name = asm2_name = *++argv;
  186. goto NextArg;
  187. }
  188. default:
  189. fatalError(INVALID_ARG, *pc);
  190. return *argv;
  191. }
  192. NextArg:;
  193. }
  194. if (argc == 1)
  195. {
  196. if (option.asm1 || option.asm2)
  197. {
  198. if (! asm1_name)
  199. {
  200. asm1_name = strcpy((char*)malloc(strlen(*argv)+4), *argv);
  201. pc = strrchr(asm1_name, '.');
  202. if (pc > strrchr(asm1_name, '/'))
  203. {
  204. *pc = '\0';
  205. }
  206. asm2_name = (char*)malloc(strlen(asm1_name)+4) ;
  207. strcat(strcpy(asm2_name, asm1_name), ".a2");
  208. unlink(asm2_name);
  209. strcat(asm1_name, ".a1");
  210. }
  211. unlink(asm1_name); /* Remove asm output files */
  212. }
  213. return *argv; /* filename of the program to decompile */
  214. }
  215. fatalError(USAGE);
  216. return *argv; // does not reach this.
  217. }
  218. static void
  219. displayTotalStats ()
  220. /* Displays final statistics for the complete program */
  221. {
  222. printf ("\nFinal Program Statistics\n");
  223. printf (" Total number of low-level Icodes : %d\n", stats.totalLL);
  224. printf (" Total number of high-level Icodes: %d\n", stats.totalHL);
  225. printf (" Total reduction of instructions : %2.2f%%\n", 100.0 -
  226. (stats.totalHL * 100.0) / stats.totalLL);
  227. }