symtab.h 3.5 KB

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