locident.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * File: locIdent.h
  3. * Purpose: High-level local identifier definitions
  4. * Date: October 1993
  5. * (C) Cristina Cifuentes
  6. */
  7. #pragma once
  8. #include "msvc_fixes.h"
  9. #include "types.h"
  10. #include "Enums.h"
  11. #include "machine_x86.h"
  12. #include <stdint.h>
  13. #include <vector>
  14. #include <list>
  15. #include <set>
  16. #include <algorithm>
  17. /* Type definition */
  18. // this array has to stay in-order of addition i.e. not std::set<iICODE,std::less<iICODE> >
  19. // TODO: why ?
  20. struct Expr;
  21. struct AstIdent;
  22. struct ICODE;
  23. struct LLInst;
  24. typedef std::list<ICODE>::iterator iICODE;
  25. struct IDX_ARRAY : public std::vector<iICODE>
  26. {
  27. bool inList(iICODE idx)
  28. {
  29. return std::find(begin(),end(),idx)!=end();
  30. }
  31. };
  32. enum frameType
  33. {
  34. STK_FRAME, /* For stack vars */
  35. REG_FRAME, /* For register variables */
  36. GLB_FRAME /* For globals */
  37. };
  38. struct BWGLB_TYPE
  39. {
  40. int16_t seg; /* segment value */
  41. int16_t off; /* offset */
  42. eReg regi; /* optional indexed register */
  43. } ;
  44. /* For TYPE_LONG_(UN)SIGN on the stack */
  45. struct LONG_STKID_TYPE
  46. {
  47. int offH; /* high offset from BP */
  48. int offL; /* low offset from BP */
  49. LONG_STKID_TYPE(int h,int l) : offH(h),offL(l) {}
  50. };
  51. /* For TYPE_LONG_(UN)SIGN registers */
  52. struct LONGID_TYPE
  53. {
  54. protected:
  55. eReg m_h; /* high register */
  56. eReg m_l; /* low register */
  57. public:
  58. void set(eReg highpart,eReg lowpart)
  59. {
  60. m_h = highpart;
  61. m_l = lowpart;
  62. }
  63. eReg l() const { return m_l; }
  64. eReg h() const { return m_h; }
  65. bool srcDstRegMatch(iICODE a,iICODE b) const;
  66. LONGID_TYPE() {} // uninitializing constructor to help valgrind catch uninit accesses
  67. LONGID_TYPE(eReg h,eReg l) : m_h(h),m_l(l) {}
  68. };
  69. struct LONGGLB_TYPE /* For TYPE_LONG_(UN)SIGN globals */
  70. {
  71. int16_t seg; /* segment value */
  72. int16_t offH; /* offset high */
  73. int16_t offL; /* offset low */
  74. uint8_t regi; /* optional indexed register */
  75. LONGGLB_TYPE(int16_t _seg,int16_t _H,int16_t _L,int8_t _reg=0)
  76. {
  77. seg=_seg;
  78. offH=_H;
  79. offL=_L;
  80. regi=_reg;
  81. }
  82. };
  83. /* ID, LOCAL_ID */
  84. struct ID
  85. {
  86. protected:
  87. LONGID_TYPE m_longId; /* For TYPE_LONG_(UN)SIGN registers */
  88. public:
  89. hlType type; /* Probable type */
  90. bool illegal; /* Boolean: not a valid field any more */
  91. //std::vector<iICODE> idx;
  92. IDX_ARRAY idx; /* Index into icode array (REG_FRAME only) */
  93. frameType loc; /* Frame location */
  94. bool hasMacro; /* Identifier requires a macro */
  95. char macro[10]; /* Macro for this identifier */
  96. std::string name; /* Identifier's name */
  97. union ID_UNION { /* Different types of identifiers */
  98. friend struct ID;
  99. protected:
  100. LONG_STKID_TYPE longStkId; /* For TYPE_LONG_(UN)SIGN on the stack */
  101. public:
  102. eReg regi; /* For TYPE_BYTE(WORD)_(UN)SIGN registers */
  103. struct { /* For TYPE_BYTE(WORD)_(UN)SIGN on the stack */
  104. uint8_t regOff; /* register offset (if any) */
  105. int off; /* offset from BP */
  106. } bwId;
  107. BWGLB_TYPE bwGlb; /* For TYPE_BYTE(uint16_t)_(UN)SIGN globals */
  108. LONGGLB_TYPE longGlb;
  109. struct { /* For TYPE_LONG_(UN)SIGN constants */
  110. uint32_t h; /* high uint16_t */
  111. uint32_t l; /* low uint16_t */
  112. } longKte;
  113. ID_UNION() { /*new (&longStkId) LONG_STKID_TYPE();*/}
  114. } id;
  115. LONGID_TYPE & longId() {assert(isLong() and loc==REG_FRAME); return m_longId;}
  116. const LONGID_TYPE & longId() const {assert(isLong() and loc==REG_FRAME); return m_longId;}
  117. LONG_STKID_TYPE & longStkId() {assert(isLong() and loc==STK_FRAME); return id.longStkId;}
  118. const LONG_STKID_TYPE & longStkId() const {assert(isLong() and loc==STK_FRAME); return id.longStkId;}
  119. ID();
  120. ID(hlType t, frameType f);
  121. ID(hlType t, const LONGID_TYPE &s);
  122. ID(hlType t, const LONG_STKID_TYPE &s);
  123. ID(hlType t, const LONGGLB_TYPE &s);
  124. bool isSigned() const { return (type==TYPE_BYTE_SIGN) or (type==TYPE_WORD_SIGN) or (type==TYPE_LONG_SIGN);}
  125. uint16_t typeBitsize() const
  126. {
  127. return TypeContainer::typeSize(type)*8;
  128. }
  129. bool isLong() const { return (type==TYPE_LONG_UNSIGN) or (type==TYPE_LONG_SIGN); }
  130. void setLocalName(int i)
  131. {
  132. char buf[32];
  133. sprintf (buf, "loc%d", i);
  134. name=buf;
  135. }
  136. };
  137. struct LOCAL_ID
  138. {
  139. std::vector<ID> id_arr;
  140. protected:
  141. int newLongIdx(int16_t seg, int16_t offH, int16_t offL, uint8_t regi, hlType t);
  142. int newLongGlb(int16_t seg, int16_t offH, int16_t offL, hlType t);
  143. int newLongStk(hlType t, int offH, int offL);
  144. public:
  145. LOCAL_ID()
  146. {
  147. id_arr.reserve(256);
  148. }
  149. // interface to allow range based iteration
  150. std::vector<ID>::iterator begin() {return id_arr.begin();}
  151. std::vector<ID>::iterator end() {return id_arr.end();}
  152. int newByteWordReg(hlType t, eReg regi);
  153. int newByteWordStk(hlType t, int off, uint8_t regOff);
  154. int newIntIdx(int16_t seg, int16_t off, eReg regi, hlType t);
  155. int newLongReg(hlType t, const LONGID_TYPE &longT, iICODE ix_);
  156. int newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, int off);
  157. int newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, LLInst &atOffset);
  158. void newIdent(hlType t, frameType f);
  159. void flagByteWordId(int off);
  160. void propLongId(uint8_t regL, uint8_t regH, const char *name);
  161. size_t csym() const {return id_arr.size();}
  162. void newRegArg(iICODE picode, iICODE ticode) const;
  163. void processTargetIcode(iICODE picode, int &numHlIcodes, iICODE ticode, bool isLong) const;
  164. void forwardSubs(Expr *lhs, Expr *rhs, iICODE picode, iICODE ticode, int &numHlIcodes) const;
  165. AstIdent *createId(const ID *retVal, iICODE ix_);
  166. };