dcc.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /* Global variables - extern to other modules */
  9. char *progname; /* argv[0] - for error msgs */
  10. char *asm1_name, *asm2_name; /* Assembler output filenames */
  11. SYMTAB symtab; /* Global symbol table */
  12. STATS stats; /* cfg statistics */
  13. PROG prog; /* programs fields */
  14. 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. FrontEnd (option.filename, &callGraph);
  40. /* In the middle is a so called Universal Decompiling Machine.
  41. * It processes the procedure list and I-code and attaches where it can
  42. * to each procedure an optimised cfg and ud lists
  43. */
  44. udm();
  45. /* Back end converts each procedure into C using I-code, interval
  46. * analysis, data flow etc. and outputs it to output file ready for
  47. * re-compilation.
  48. */
  49. BackEnd(option.filename, callGraph);
  50. callGraph->write();
  51. if (option.Stats)
  52. displayTotalStats();
  53. /*
  54. freeDataStructures(pProcList);
  55. */
  56. return 0;
  57. }
  58. /****************************************************************************
  59. * initargs - Extract command line arguments
  60. ***************************************************************************/
  61. static char *initargs(int argc, char *argv[])
  62. {
  63. char *pc;
  64. progname = *argv; /* Save invocation name for error messages */
  65. while (--argc > 0 && (*++argv)[0] == '-')
  66. {
  67. for (pc = argv[0]+1; *pc; pc++)
  68. switch (*pc)
  69. {
  70. case 'a': /* Print assembler listing */
  71. if (*(pc+1) == '2')
  72. option.asm2 = true;
  73. else
  74. option.asm1 = true;
  75. if (*(pc+1) == '1' || *(pc+1) == '2')
  76. pc++;
  77. break;
  78. case 'c':
  79. option.Calls = true;
  80. break;
  81. case 'i':
  82. option.Interact = true;
  83. break;
  84. case 'm': /* Print memory map */
  85. option.Map = true;
  86. break;
  87. case 's': /* Print Stats */
  88. option.Stats = true;
  89. break;
  90. case 'V': /* Very verbose => verbose */
  91. option.VeryVerbose = true;
  92. //lint -fallthrough
  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. //lint -fallthrough
  106. default:
  107. fatalError(INVALID_ARG, *pc);
  108. return *argv;
  109. }
  110. NextArg:;
  111. }
  112. if (argc == 1)
  113. {
  114. if (option.asm1 || option.asm2)
  115. {
  116. if (! asm1_name)
  117. {
  118. asm1_name = strcpy((char*)malloc(strlen(*argv)+4), *argv);
  119. pc = strrchr(asm1_name, '.');
  120. if (pc > strrchr(asm1_name, '/'))
  121. {
  122. *pc = '\0';
  123. }
  124. asm2_name = (char*)malloc(strlen(asm1_name)+4) ;
  125. strcat(strcpy(asm2_name, asm1_name), ".a2");
  126. unlink(asm2_name);
  127. strcat(asm1_name, ".a1");
  128. }
  129. unlink(asm1_name); /* Remove asm output files */
  130. }
  131. return *argv; /* filename of the program to decompile */
  132. }
  133. fatalError(USAGE);
  134. return *argv;
  135. }
  136. static void
  137. displayTotalStats ()
  138. /* Displays final statistics for the complete program */
  139. {
  140. printf ("\nFinal Program Statistics\n");
  141. printf (" Total number of low-level Icodes : %ld\n", stats.totalLL);
  142. printf (" Total number of high-level Icodes: %ld\n", stats.totalHL);
  143. printf (" Total reduction of instructions : %2.2f%%\n", 100.0 -
  144. (stats.totalHL * 100.0) / stats.totalLL);
  145. }