udm.cpp 3.1 KB

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