dcc.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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();
  21. #include <llvm/Support/raw_os_ostream.h>
  22. #include <llvm/Support/raw_ostream.h>
  23. /****************************************************************************
  24. * main
  25. ***************************************************************************/
  26. #include <iostream>
  27. int main(int argc, char *argv[])
  28. {
  29. // llvm::MCOperand op=llvm::MCOperand::CreateImm(11);
  30. // llvm::MCAsmInfo info;
  31. // llvm::raw_os_ostream wrap(std::cerr);
  32. // op.print(wrap,&info);
  33. // wrap.flush();
  34. /* Extract switches and filename */
  35. strcpy(option.filename, initargs(argc, argv));
  36. /* Front end reads in EXE or COM file, parses it into I-code while
  37. * building the call graph and attaching appropriate bits of code for
  38. * each procedure.
  39. */
  40. FrontEnd (option.filename, &callGraph);
  41. /* In the middle is a so called Universal Decompiling Machine.
  42. * It processes the procedure list and I-code and attaches where it can
  43. * to each procedure an optimised cfg and ud lists
  44. */
  45. udm();
  46. /* Back end converts each procedure into C using I-code, interval
  47. * analysis, data flow etc. and outputs it to output file ready for
  48. * re-compilation.
  49. */
  50. BackEnd(option.filename, callGraph);
  51. callGraph->write();
  52. if (option.Stats)
  53. displayTotalStats();
  54. /*
  55. freeDataStructures(pProcList);
  56. */
  57. return 0;
  58. }
  59. /****************************************************************************
  60. * initargs - Extract command line arguments
  61. ***************************************************************************/
  62. static char *initargs(int argc, char *argv[])
  63. {
  64. char *pc;
  65. progname = *argv; /* Save invocation name for error messages */
  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': /* Make everything verbose */
  94. option.verbose = TRUE;
  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;
  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 : %ld\n", stats.totalLL);
  141. printf (" Total number of high-level Icodes: %ld\n", stats.totalHL);
  142. printf (" Total reduction of instructions : %2.2f%%\n", 100.0 -
  143. (stats.totalHL * 100.0) / stats.totalLL);
  144. }