project.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. }
  16. void Project::initialize()
  17. {
  18. delete callGraph;
  19. callGraph = nullptr;
  20. }
  21. void Project::create(const string &a)
  22. {
  23. m_fname=a;
  24. string::size_type ext_loc=a.find_last_of('.');
  25. string::size_type slash_loc=a.find_last_of('/',ext_loc);
  26. if(slash_loc==string::npos)
  27. slash_loc=0;
  28. else
  29. slash_loc++;
  30. if(ext_loc!=string::npos)
  31. m_project_name = a.substr(slash_loc,(ext_loc-slash_loc));
  32. else
  33. m_project_name = a.substr(slash_loc);
  34. }
  35. bool Project::valid(ilFunction iter)
  36. {
  37. return iter!=pProcList.end();
  38. }
  39. ilFunction Project::funcIter(Function *to_find)
  40. {
  41. auto iter=std::find_if(pProcList.begin(),pProcList.end(),
  42. [to_find](const Function &f)->bool {return to_find==&f;});
  43. assert(iter!=pProcList.end());
  44. return iter;
  45. }
  46. ilFunction Project::findByEntry(uint32_t entry)
  47. {
  48. /* Search procedure list for one with appropriate entry point */
  49. ilFunction iter= std::find_if(pProcList.begin(),pProcList.end(),
  50. [entry](const Function &f) ->
  51. bool { 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. }