locident.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "Enums.h"
  14. #include "machine_x86.h"
  15. /* Type definition */
  16. // this array has to stay in-order of addition i.e. not std::set<iICODE,std::less<iICODE> >
  17. // TODO: why ?
  18. struct ICODE;
  19. typedef std::list<ICODE>::iterator iICODE;
  20. struct IDX_ARRAY : public std::vector<iICODE>
  21. {
  22. bool inList(iICODE idx)
  23. {
  24. return std::find(begin(),end(),idx)!=end();
  25. }
  26. };
  27. static constexpr const char * hlTypes[13] = {
  28. "", "char", "unsigned char", "int", "unsigned int",
  29. "long", "unsigned long", "record", "int *", "char *",
  30. "", "float", "double"
  31. };
  32. typedef enum
  33. {
  34. STK_FRAME, /* For stack vars */
  35. REG_FRAME, /* For register variables */
  36. GLB_FRAME /* For globals */
  37. } frameType;
  38. typedef struct
  39. {
  40. int16_t seg; /* segment value */
  41. int16_t off; /* offset */
  42. eReg regi; /* optional indexed register */
  43. } BWGLB_TYPE;
  44. typedef struct
  45. { /* For TYPE_LONG_(UN)SIGN on the stack */
  46. int offH; /* high offset from BP */
  47. int offL; /* low offset from BP */
  48. } LONG_STKID_TYPE;
  49. typedef struct
  50. { /* For TYPE_LONG_(UN)SIGN registers */
  51. eReg h; /* high register */
  52. eReg l; /* low register */
  53. } LONGID_TYPE;
  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. char name[20]; /* 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. switch(type)
  91. {
  92. case TYPE_WORD_SIGN: case TYPE_WORD_UNSIGN:
  93. return 16;
  94. case TYPE_BYTE_SIGN: case TYPE_BYTE_UNSIGN:
  95. return 8;
  96. }
  97. return ~0;
  98. }
  99. };
  100. struct LOCAL_ID
  101. {
  102. std::vector<ID> id_arr;
  103. public:
  104. LOCAL_ID()
  105. {
  106. id_arr.reserve(256);
  107. }
  108. // interface to allow range based iteration
  109. std::vector<ID>::iterator begin() {return id_arr.begin();}
  110. std::vector<ID>::iterator end() {return id_arr.end();}
  111. int newByteWordReg(hlType t, eReg regi);
  112. int newByteWordStk(hlType t, int off, uint8_t regOff);
  113. int newIntIdx(int16_t seg, int16_t off, eReg regi, int ix, hlType t);
  114. int newLongReg(hlType t, eReg regH, eReg regL, iICODE ix_);
  115. int newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, int off);
  116. int newLong(opLoc sd, iICODE pIcode, hlFirst f, iICODE ix, operDu du, iICODE atOffset);
  117. void newIdent(hlType t, frameType f);
  118. void flagByteWordId(int off);
  119. void propLongId(uint8_t regL, uint8_t regH, const char *name);
  120. size_t csym() const {return id_arr.size();}
  121. protected:
  122. int newLongIdx(int16_t seg, int16_t offH, int16_t offL, uint8_t regi, hlType t);
  123. int newLongGlb(int16_t seg, int16_t offH, int16_t offL, hlType t);
  124. int newLongStk(hlType t, int offH, int offL);
  125. };