symtab.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Symbol table prototypes
  3. * (C) Mike van Emmerik
  4. */
  5. #pragma once
  6. #include <string>
  7. #include <stdint.h>
  8. #include "Enums.h"
  9. #include "types.h"
  10. struct COND_EXPR;
  11. struct TypeContainer;
  12. /* * * * * * * * * * * * * * * * * */
  13. /* Symbol table structs and protos */
  14. /* * * * * * * * * * * * * * * * * */
  15. struct SymbolCommon
  16. {
  17. std::string name; /* New name for this variable/symbol/argument */
  18. int size; /* Size/maximum size */
  19. hlType type; /* probable type */
  20. eDuVal duVal; /* DEF, USE, VAL */
  21. SymbolCommon() : size(0),type(TYPE_UNKNOWN)
  22. {}
  23. };
  24. struct SYM : public SymbolCommon
  25. {
  26. SYM() : label(0),flg(0)
  27. {
  28. }
  29. uint32_t label; /* physical address (20 bit) */
  30. uint32_t flg; /* SEG_IMMED, IMPURE, WORD_OFF */
  31. };
  32. /* STACK FRAME */
  33. struct STKSYM : public SymbolCommon
  34. {
  35. COND_EXPR *actual; /* Expression tree of actual parameter */
  36. COND_EXPR *regs; /* For register arguments only */
  37. int16_t label; /* Immediate off from BP (+:args, -:params) */
  38. uint8_t regOff; /* Offset is a register (e.g. SI, DI) */
  39. bool hasMacro; /* This type needs a macro */
  40. std::string macro; /* Macro name */
  41. bool invalid; /* Boolean: invalid entry in formal arg list*/
  42. STKSYM()
  43. {
  44. actual=regs=0;
  45. label=0;
  46. regOff=0;
  47. invalid=hasMacro = false;
  48. }
  49. void setArgName(int i)
  50. {
  51. char buf[32];
  52. sprintf (buf, "arg%d", i);
  53. name = buf;
  54. }
  55. };
  56. template<class T>
  57. class SymbolTableCommon : public std::vector<T>
  58. {
  59. public:
  60. typedef typename std::vector<T>::iterator iterator;
  61. typedef typename std::vector<T>::const_iterator const_iterator;
  62. iterator findByLabel(uint32_t lab)
  63. {
  64. auto iter = std::find_if(this->begin(),this->end(),
  65. [lab](T &s)->bool {return s.label==lab;});
  66. return iter;
  67. }
  68. const_iterator findByLabel(uint32_t lab) const
  69. {
  70. auto iter = std::find_if(this->begin(),this->end(),
  71. [lab](const T &s)->bool {return s.label==lab;});
  72. return iter;
  73. }
  74. };
  75. /* SYMBOL TABLE */
  76. class SYMTAB : public SymbolTableCommon<SYM>
  77. {
  78. public:
  79. void updateSymType(uint32_t symbol, const TypeContainer &tc);
  80. SYM *updateGlobSym(uint32_t operand, int size, uint16_t duFlag, bool &inserted_new);
  81. };
  82. struct Function;
  83. struct SYMTABLE
  84. {
  85. std::string pSymName; /* Ptr to symbolic name or comment */
  86. uint32_t symOff; /* Symbol image offset */
  87. Function *symProc; /* Procedure pointer */
  88. SYMTABLE() : symOff(0),symProc(0) {}
  89. SYMTABLE(uint32_t _sym,Function *_proc) : symOff(_sym),symProc(_proc)
  90. {}
  91. bool operator == (const SYMTABLE &other) const
  92. {
  93. // does not yse pSymName, to ease finding by symOff/symProc combo
  94. // in map<SYMTABLE,X>
  95. return (symOff==other.symOff) && symProc==(other.symProc);
  96. }
  97. };
  98. enum tableType /* The table types */
  99. {
  100. Label=0, /* The label table */
  101. Comment /* The comment table */
  102. };
  103. constexpr int NUM_TABLE_TYPES = int(Comment)+1; /* Number of entries: must be last */
  104. void createSymTables(void);
  105. void destroySymTables(void);
  106. boolT readVal (std::ostringstream &symName, uint32_t symOff, Function *symProc);
  107. void selectTable(tableType); /* Select a particular table */