project.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <utility>
  2. #include "dcc.h"
  3. #include "project.h"
  4. #include "Procedure.h"
  5. using namespace std;
  6. //Project g_proj;
  7. char *asm1_name, *asm2_name; /* Assembler output filenames */
  8. SYMTAB symtab; /* Global symbol table */
  9. STATS stats; /* cfg statistics */
  10. //PROG prog; /* programs fields */
  11. OPTION option; /* Command line options */
  12. Project *Project::s_instance = 0;
  13. Project::Project() : callGraph(nullptr)
  14. {
  15. memset(&prog,0,sizeof(prog));
  16. }
  17. void Project::initialize()
  18. {
  19. delete callGraph;
  20. callGraph = nullptr;
  21. }
  22. void Project::create(const string &a)
  23. {
  24. m_fname=a;
  25. string::size_type ext_loc=a.find_last_of('.');
  26. string::size_type slash_loc=a.find_last_of('/',ext_loc);
  27. if(slash_loc==string::npos)
  28. slash_loc=0;
  29. else
  30. slash_loc++;
  31. if(ext_loc!=string::npos)
  32. m_project_name = a.substr(slash_loc,(ext_loc-slash_loc));
  33. else
  34. m_project_name = a.substr(slash_loc);
  35. }
  36. bool Project::valid(ilFunction iter)
  37. {
  38. return iter!=pProcList.end();
  39. }
  40. ilFunction Project::funcIter(Function *to_find)
  41. {
  42. auto iter=std::find_if(pProcList.begin(),pProcList.end(),
  43. [to_find](const Function &f)->bool {return to_find==&f;});
  44. assert(iter!=pProcList.end());
  45. return iter;
  46. }
  47. ilFunction Project::findByEntry(uint32_t entry)
  48. {
  49. /* Search procedure list for one with appropriate entry point */
  50. ilFunction iter= std::find_if(pProcList.begin(),pProcList.end(),
  51. [entry](const Function &f) { return f.procEntry==entry; });
  52. return iter;
  53. }
  54. ilFunction Project::createFunction()
  55. {
  56. pProcList.push_back(Function::Create());
  57. return (++pProcList.rbegin()).base();
  58. }
  59. int Project::getSymIdxByAdd(uint32_t adr)
  60. {
  61. size_t i;
  62. for (i = 0; i < symtab.size(); i++)
  63. if (symtab[i].label == adr)
  64. break;
  65. return i;
  66. }
  67. bool Project::validSymIdx(size_t idx)
  68. {
  69. return idx<symtab.size();
  70. }
  71. const SYM &Project::getSymByIdx(size_t idx) const
  72. {
  73. return symtab[idx];
  74. }
  75. size_t Project::symbolSize(size_t idx)
  76. {
  77. assert(validSymIdx(idx));
  78. return symtab[idx].size;
  79. }
  80. hlType Project::symbolType(size_t idx)
  81. {
  82. assert(validSymIdx(idx));
  83. return symtab[idx].type;
  84. }
  85. const std::string &Project::symbolName(size_t idx)
  86. {
  87. assert(validSymIdx(idx));
  88. return symtab[idx].name;
  89. }
  90. Project *Project::get()
  91. {
  92. //WARNING: poor man's singleton, not thread safe
  93. if(s_instance==0)
  94. s_instance=new Project;
  95. return s_instance;
  96. }
  97. SourceMachine *Project::machine()
  98. {
  99. return nullptr;
  100. }