symtab.h 3.6 KB

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