symtab.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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=0; /* Expression tree of actual parameter */
  39. AstIdent *regs=0; /* For register arguments only */
  40. tLabel label=0; /* Immediate off from BP (+:args, -:params) */
  41. uint8_t regOff=0; /* Offset is a register (e.g. SI, DI) */
  42. bool hasMacro=false; /* This type needs a macro */
  43. std::string macro; /* Macro name */
  44. bool invalid=false; /* Boolean: invalid entry in formal arg list*/
  45. void setArgName(int i)
  46. {
  47. char buf[32];
  48. sprintf (buf, "arg%d", i);
  49. name = buf;
  50. }
  51. };
  52. template<class T>
  53. class SymbolTableCommon : public std::vector<T>
  54. {
  55. public:
  56. typedef typename std::vector<T>::iterator iterator;
  57. typedef typename std::vector<T>::const_iterator const_iterator;
  58. iterator findByLabel(typename T::tLabel lab)
  59. {
  60. auto iter = std::find_if(this->begin(),this->end(),
  61. [lab](T &s)->bool {return s.label==lab;});
  62. return iter;
  63. }
  64. const_iterator findByLabel(typename T::tLabel lab) const
  65. {
  66. auto iter = std::find_if(this->begin(),this->end(),
  67. [lab](const T &s)->bool {return s.label==lab;});
  68. return iter;
  69. }
  70. };
  71. /* SYMBOL TABLE */
  72. class SYMTAB : public SymbolTableCommon<SYM>
  73. {
  74. public:
  75. void updateSymType(uint32_t symbol, const TypeContainer &tc);
  76. SYM *updateGlobSym(uint32_t operand, int size, uint16_t duFlag, bool &inserted_new);
  77. };
  78. struct Function;
  79. struct SYMTABLE
  80. {
  81. std::string pSymName; /* Ptr to symbolic name or comment */
  82. uint32_t symOff; /* Symbol image offset */
  83. Function *symProc; /* Procedure pointer */
  84. SYMTABLE() : symOff(0),symProc(0) {}
  85. SYMTABLE(uint32_t _sym,Function *_proc) : symOff(_sym),symProc(_proc)
  86. {}
  87. bool operator == (const SYMTABLE &other) const
  88. {
  89. // does not yse pSymName, to ease finding by symOff/symProc combo
  90. // in map<SYMTABLE,X>
  91. return (symOff==other.symOff) && symProc==(other.symProc);
  92. }
  93. };
  94. enum tableType /* The table types */
  95. {
  96. Label=0, /* The label table */
  97. Comment /* The comment table */
  98. };
  99. constexpr int NUM_TABLE_TYPES = int(Comment)+1; /* Number of entries: must be last */
  100. void createSymTables(void);
  101. void destroySymTables(void);
  102. bool readVal (std::ostringstream &symName, uint32_t symOff, Function *symProc);
  103. void selectTable(tableType); /* Select a particular table */