locident.h 6.6 KB

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