dcc.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "dcc.h"
  7. #include "project.h"
  8. #include <string.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. /****************************************************************************
  23. * main
  24. ***************************************************************************/
  25. #include <iostream>
  26. int main(int argc, char *argv[])
  27. {
  28. // llvm::MCOperand op=llvm::MCOperand::CreateImm(11);
  29. // llvm::MCAsmInfo info;
  30. // llvm::raw_os_ostream wrap(std::cerr);
  31. // op.print(wrap,&info);
  32. // wrap.flush();
  33. /* Extract switches and filename */
  34. strcpy(option.filename, initargs(argc, argv));
  35. /* Front end reads in EXE or COM file, parses it into I-code while
  36. * building the call graph and attaching appropriate bits of code for
  37. * each procedure.
  38. */
  39. DccFrontend fe(option.filename);
  40. if(false==fe.FrontEnd ())
  41. return -1;
  42. /* In the middle is a so called Universal Decompiling Machine.
  43. * It processes the procedure list and I-code and attaches where it can
  44. * to each procedure an optimised cfg and ud lists
  45. */
  46. udm();
  47. /* Back end converts each procedure into C using I-code, interval
  48. * analysis, data flow etc. and outputs it to output file ready for
  49. * re-compilation.
  50. */
  51. BackEnd(option.filename, Project::get()->callGraph);
  52. Project::get()->callGraph->write();
  53. if (option.Stats)
  54. displayTotalStats();
  55. /*
  56. freeDataStructures(pProcList);
  57. */
  58. return 0;
  59. }
  60. /****************************************************************************
  61. * initargs - Extract command line arguments
  62. ***************************************************************************/
  63. static char *initargs(int argc, char *argv[])
  64. {
  65. char *pc;
  66. while (--argc > 0 && (*++argv)[0] == '-')
  67. {
  68. for (pc = argv[0]+1; *pc; pc++)
  69. switch (*pc)
  70. {
  71. case 'a': /* Print assembler listing */
  72. if (*(pc+1) == '2')
  73. option.asm2 = true;
  74. else
  75. option.asm1 = true;
  76. if (*(pc+1) == '1' || *(pc+1) == '2')
  77. pc++;
  78. break;
  79. case 'c':
  80. option.Calls = true;
  81. break;
  82. case 'i':
  83. option.Interact = true;
  84. break;
  85. case 'm': /* Print memory map */
  86. option.Map = true;
  87. break;
  88. case 's': /* Print Stats */
  89. option.Stats = true;
  90. break;
  91. case 'V': /* Very verbose => verbose */
  92. option.VeryVerbose = true;
  93. case 'v':
  94. option.verbose = true; /* Make everything verbose */
  95. break;
  96. case 'o': /* assembler output file */
  97. if (*(pc+1)) {
  98. asm1_name = asm2_name = pc+1;
  99. goto NextArg;
  100. }
  101. else if (--argc > 0) {
  102. asm1_name = asm2_name = *++argv;
  103. goto NextArg;
  104. }
  105. default:
  106. fatalError(INVALID_ARG, *pc);
  107. return *argv;
  108. }
  109. NextArg:;
  110. }
  111. if (argc == 1)
  112. {
  113. if (option.asm1 || option.asm2)
  114. {
  115. if (! asm1_name)
  116. {
  117. asm1_name = strcpy((char*)malloc(strlen(*argv)+4), *argv);
  118. pc = strrchr(asm1_name, '.');
  119. if (pc > strrchr(asm1_name, '/'))
  120. {
  121. *pc = '\0';
  122. }
  123. asm2_name = (char*)malloc(strlen(asm1_name)+4) ;
  124. strcat(strcpy(asm2_name, asm1_name), ".a2");
  125. unlink(asm2_name);
  126. strcat(asm1_name, ".a1");
  127. }
  128. unlink(asm1_name); /* Remove asm output files */
  129. }
  130. return *argv; /* filename of the program to decompile */
  131. }
  132. fatalError(USAGE);
  133. return *argv; // does not reach this.
  134. }
  135. static void
  136. displayTotalStats ()
  137. /* Displays final statistics for the complete program */
  138. {
  139. printf ("\nFinal Program Statistics\n");
  140. printf (" Total number of low-level Icodes : %d\n", stats.totalLL);
  141. printf (" Total number of high-level Icodes: %d\n", stats.totalHL);
  142. printf (" Total reduction of instructions : %2.2f%%\n", 100.0 -
  143. (stats.totalHL * 100.0) / stats.totalLL);
  144. }