udm.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*****************************************************************************
  2. * dcc project Universal Decompilation Module
  3. * This is supposedly a machine independant and language independant module
  4. * that just plays with abstract cfg's and intervals and such like.
  5. * (C) Cristina Cifuentes
  6. ****************************************************************************/
  7. #include <list>
  8. #include <cassert>
  9. #include <stdio.h>
  10. #include "dcc.h"
  11. #include "disassem.h"
  12. static void displayCFG(Function * pProc);
  13. static void displayDfs(BB * pBB);
  14. /****************************************************************************
  15. * udm
  16. ****************************************************************************/
  17. void Function::buildCFG(Disassembler &ds)
  18. {
  19. if(flg & PROC_ISLIB)
  20. return; // Ignore library functions
  21. createCFG();
  22. if (option.VeryVerbose)
  23. displayCFG();
  24. compressCFG(); // Remove redundancies and add in-edge information
  25. if (option.asm2)
  26. {
  27. ds.disassem(this); // Print 2nd pass assembler listing
  28. }
  29. /* Idiom analysis and propagation of long type */
  30. lowLevelAnalysis();
  31. /* Generate HIGH_LEVEL icodes whenever possible */
  32. highLevelGen();
  33. }
  34. void Function::controlFlowAnalysis()
  35. {
  36. if (flg & PROC_ISLIB)
  37. return; /* Ignore library functions */
  38. derSeq *derivedG=0;
  39. /* Make cfg reducible and build derived sequences */
  40. derivedG=checkReducibility();
  41. if (option.VeryVerbose)
  42. derivedG->display();
  43. /* Structure the graph */
  44. structure(derivedG);
  45. /* Check for compound conditions */
  46. compoundCond ();
  47. if (option.verbose)
  48. {
  49. printf("\nDepth first traversal - Proc %s\n", name.c_str());
  50. m_cfg.front()->displayDfs();
  51. }
  52. /* Free storage occupied by this procedure */
  53. freeDerivedSeq(*derivedG);
  54. }
  55. void udm(void)
  56. {
  57. /* Build the control flow graph, find idioms, and convert low-level
  58. * icodes to high-level ones */
  59. Disassembler ds(2);
  60. for (auto iter = pProcList.rbegin(); iter!=pProcList.rend(); ++iter)
  61. {
  62. iter->buildCFG(ds);
  63. }
  64. /* Data flow analysis - eliminate condition codes, extraneous registers
  65. * and intermediate instructions. Find expressions by forward
  66. * substitution algorithm */
  67. std::bitset<32> live_regs;
  68. pProcList.front().dataFlow (live_regs);
  69. /* Control flow analysis - structuring algorithm */
  70. for (auto iter = pProcList.rbegin(); iter!=pProcList.rend(); ++iter)
  71. {
  72. iter->controlFlowAnalysis();
  73. }
  74. }
  75. /****************************************************************************
  76. * displayCFG - Displays the Basic Block list
  77. ***************************************************************************/
  78. void Function::displayCFG()
  79. {
  80. printf("\nBasic Block List - Proc %s", name.c_str());
  81. for (BB *pBB : m_cfg)
  82. {
  83. pBB->display();
  84. }
  85. }