dcc.cpp 8.1 KB

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