locident.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. typedef enum
  32. {
  33. STK_FRAME, /* For stack vars */
  34. REG_FRAME, /* For register variables */
  35. GLB_FRAME /* For globals */
  36. } frameType;
  37. typedef struct
  38. {
  39. int16_t seg; /* segment value */
  40. int16_t off; /* offset */
  41. eReg regi; /* optional indexed register */
  42. } BWGLB_TYPE;
  43. typedef struct
  44. { /* For TYPE_LONG_(UN)SIGN on the stack */
  45. int offH; /* high offset from BP */
  46. int offL; /* low offset from BP */
  47. } LONG_STKID_TYPE;
  48. struct LONGID_TYPE
  49. { /* For TYPE_LONG_(UN)SIGN registers */
  50. eReg h; /* high register */
  51. eReg l; /* low register */
  52. bool srcDstRegMatch(iICODE a,iICODE b) const;
  53. };
  54. /* ID, LOCAL_ID */
  55. struct ID
  56. {
  57. hlType type; /* Probable type */
  58. bool illegal; /* Boolean: not a valid field any more */
  59. //std::vector<iICODE> idx;
  60. IDX_ARRAY idx; /* Index into icode array (REG_FRAME only) */
  61. frameType loc; /* Frame location */
  62. bool hasMacro; /* Identifier requires a macro */
  63. char macro[10]; /* Macro for this identifier */
  64. std::string name; /* Identifier's name */
  65. union { /* Different types of identifiers */
  66. eReg regi; /* For TYPE_BYTE(uint16_t)_(UN)SIGN registers */
  67. struct { /* For TYPE_BYTE(uint16_t)_(UN)SIGN on the stack */
  68. uint8_t regOff; /* register offset (if any) */
  69. int off; /* offset from BP */
  70. } bwId;
  71. BWGLB_TYPE bwGlb; /* For TYPE_BYTE(uint16_t)_(UN)SIGN globals */
  72. LONGID_TYPE longId; /* For TYPE_LONG_(UN)SIGN registers */
  73. LONG_STKID_TYPE longStkId; /* For TYPE_LONG_(UN)SIGN on the stack */
  74. struct { /* For TYPE_LONG_(UN)SIGN globals */
  75. int16_t seg; /* segment value */
  76. int16_t offH; /* offset high */
  77. int16_t offL; /* offset low */
  78. uint8_t regi; /* optional indexed register */
  79. } longGlb;
  80. struct { /* For TYPE_LONG_(UN)SIGN constants */
  81. uint32_t h; /* high uint16_t */
  82. uint32_t l; /* low uint16_t */
  83. } longKte;
  84. } id;
  85. ID();
  86. ID(hlType t, frameType f);
  87. bool isSigned() const { return (type==TYPE_BYTE_SIGN)||(type==TYPE_WORD_SIGN)||(type==TYPE_LONG_SIGN);}
  88. uint16_t typeBitsize() const
  89. {
  90. return TypeContainer::typeSize(type)*8;
  91. }
  92. void setLocalName(int i)
  93. {
  94. char buf[32];
  95. sprintf (buf, "loc%d", i);
  96. name=buf;
  97. }
  98. };
  99. struct LOCAL_ID
  100. {
  101. std::vector<ID> id_arr;
  102. protected:
  103. int newLongIdx(int16_t seg, int16_t offH, int16_t offL, uint8_t regi, hlType t);
  104. int newLongGlb(int16_t seg, int16_t offH, int16_t offL, hlType t);
  105. int newLongStk(hlType t, int offH, int offL);
  106. public:
  107. LOCAL_ID()
  108. {
  109. id_arr.reserve(256);
  110. }
  111. // interface to allow range based iteration
  112. std::vector<ID>::iterator begin() {return id_arr.begin();}
  113. std::vector<ID>::iterator end() {return id_arr.end();}
  114. int newByteWordReg(hlType t, eReg regi);
  115. int newByteWordStk(hlType t, int off, uint8_t regOff);
  116. int newIntIdx(int16_t seg, int16_t off, eReg regi, hlType t);
  117. int newLongReg(hlType t, eReg regH, eReg regL, iICODE ix_);
  118. int newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, int off);
  119. int newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, LLInst &atOffset);
  120. void newIdent(hlType t, frameType f);
  121. void flagByteWordId(int off);
  122. void propLongId(uint8_t regL, uint8_t regH, const char *name);
  123. size_t csym() const {return id_arr.size();}
  124. void newRegArg(iICODE picode, iICODE ticode) const;
  125. void processTargetIcode(iICODE picode, int &numHlIcodes, iICODE ticode, bool isLong) const;
  126. void forwardSubs(Expr *lhs, Expr *rhs, iICODE picode, iICODE ticode, int &numHlIcodes) const;
  127. AstIdent *createId(const ID *retVal, iICODE ix_);
  128. };