dcc.cpp 5.3 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 <string.h>
  8. #ifdef __UNIX__
  9. //#include <unistd.h>
  10. #else
  11. #include <stdio.h>
  12. #include <io.h> /* For unlink() */
  13. #endif
  14. /* Global variables - extern to other modules */
  15. char *progname; /* argv[0] - for error msgs */
  16. char *asm1_name, *asm2_name; /* Assembler output filenames */
  17. SYMTAB symtab; /* Global symbol table */
  18. STATS stats; /* cfg statistics */
  19. PROG prog; /* programs fields */
  20. OPTION option; /* Command line options */
  21. //Function * pProcList; /* List of procedures, topologically sort */
  22. //Function * pLastProc; /* Pointer to last node in procedure list */
  23. FunctionListType pProcList;
  24. CALL_GRAPH *callGraph; /* Call graph of the program */
  25. static char *initargs(int argc, char *argv[]);
  26. static void displayTotalStats();
  27. /****************************************************************************
  28. * main
  29. ***************************************************************************/
  30. int main(int argc, char *argv[])
  31. {
  32. /* Extract switches and filename */
  33. strcpy(option.filename, initargs(argc, argv));
  34. /* Front end reads in EXE or COM file, parses it into I-code while
  35. * building the call graph and attaching appropriate bits of code for
  36. * each procedure.
  37. */
  38. FrontEnd (option.filename, &callGraph);
  39. /* In the middle is a so called Universal Decompiling Machine.
  40. * It processes the procedure list and I-code and attaches where it can
  41. * to each procedure an optimised cfg and ud lists
  42. */
  43. udm();
  44. /* Back end converts each procedure into C using I-code, interval
  45. * analysis, data flow etc. and outputs it to output file ready for
  46. * re-compilation.
  47. */
  48. BackEnd(option.filename, callGraph);
  49. callGraph->write();
  50. if (option.Stats)
  51. displayTotalStats();
  52. /*
  53. freeDataStructures(pProcList);
  54. */
  55. return 0;
  56. }
  57. /****************************************************************************
  58. * initargs - Extract command line arguments
  59. ***************************************************************************/
  60. static char *initargs(int argc, char *argv[])
  61. {
  62. char *pc;
  63. progname = *argv; /* Save invocation name for error messages */
  64. while (--argc > 0 && (*++argv)[0] == '-') {
  65. for (pc = argv[0]+1; *pc; pc++)
  66. switch (*pc) {
  67. case 'a': /* Print assembler listing */
  68. if (*(pc+1) == '2')
  69. option.asm2 = TRUE;
  70. else
  71. option.asm1 = TRUE;
  72. if (*(pc+1) == '1' || *(pc+1) == '2')
  73. pc++;
  74. break;
  75. case 'c':
  76. option.Calls = TRUE;
  77. break;
  78. case 'i':
  79. option.Interact = TRUE;
  80. break;
  81. case 'm': /* Print memory map */
  82. option.Map = TRUE;
  83. break;
  84. case 's': /* Print Stats */
  85. option.Stats = TRUE;
  86. break;
  87. case 'V': /* Very verbose => verbose */
  88. option.VeryVerbose = TRUE;
  89. case 'v': /* Make everything verbose */
  90. option.verbose = TRUE;
  91. break;
  92. case 'o': /* assembler output file */
  93. if (*(pc+1)) {
  94. asm1_name = asm2_name = pc+1;
  95. goto NextArg;
  96. }
  97. else if (--argc > 0) {
  98. asm1_name = asm2_name = *++argv;
  99. goto NextArg;
  100. }
  101. default:
  102. fatalError(INVALID_ARG, *pc);
  103. return *argv;
  104. }
  105. NextArg:;
  106. }
  107. if (argc == 1)
  108. {
  109. if (option.asm1 || option.asm2)
  110. {
  111. if (! asm1_name)
  112. {
  113. asm1_name = strcpy((char*)allocMem(strlen(*argv)+4), *argv);
  114. pc = strrchr(asm1_name, '.');
  115. if (pc > strrchr(asm1_name, '/'))
  116. {
  117. *pc = '\0';
  118. }
  119. asm2_name = (char*)allocMem(strlen(asm1_name)+4) ;
  120. strcat(strcpy(asm2_name, asm1_name), ".a2");
  121. unlink(asm2_name);
  122. strcat(asm1_name, ".a1");
  123. }
  124. unlink(asm1_name); /* Remove asm output files */
  125. }
  126. return *argv; /* filename of the program to decompile */
  127. }
  128. fatalError(USAGE);
  129. return *argv;
  130. }
  131. static void
  132. displayTotalStats ()
  133. /* Displays final statistics for the complete program */
  134. {
  135. printf ("\nFinal Program Statistics\n");
  136. printf (" Total number of low-level Icodes : %ld\n", stats.totalLL);
  137. printf (" Total number of high-level Icodes: %ld\n", stats.totalHL);
  138. printf (" Total reduction of instructions : %2.2f%%\n", 100.0 -
  139. (stats.totalHL * 100.0) / stats.totalLL);
  140. }