symtab.h 3.5 KB

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