symtab.h 3.5 KB

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