locident.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <vector>
  9. #include <set>
  10. #include <algorithm>
  11. #include "icode.h"
  12. /* Type definition */
  13. // this array has to stay in-order of addition i.e. not std::set<iICODE,std::less<iICODE> >
  14. // TODO: why ?
  15. struct IDX_ARRAY : public std::vector<iICODE>
  16. {
  17. bool inList(iICODE idx)
  18. {
  19. return std::find(begin(),end(),idx)!=end();
  20. }
  21. };
  22. /* Type definitions used in the decompiled program */
  23. typedef enum {
  24. TYPE_UNKNOWN = 0, /* unknown so far */
  25. TYPE_BYTE_SIGN, /* signed byte (8 bits) */
  26. TYPE_BYTE_UNSIGN, /* unsigned byte */
  27. TYPE_WORD_SIGN, /* signed word (16 bits) */
  28. TYPE_WORD_UNSIGN, /* unsigned word (16 bits) */
  29. TYPE_LONG_SIGN, /* signed long (32 bits) */
  30. TYPE_LONG_UNSIGN, /* unsigned long (32 bits) */
  31. TYPE_RECORD, /* record structure */
  32. TYPE_PTR, /* pointer (32 bit ptr) */
  33. TYPE_STR, /* string */
  34. TYPE_CONST, /* constant (any type) */
  35. TYPE_FLOAT, /* floating point */
  36. TYPE_DOUBLE /* double precision float */
  37. } hlType;
  38. static constexpr const char *hlTypes[13] = {"", "char", "unsigned char", "int", "unsigned int",
  39. "long", "unsigned long", "record", "int *", "char *",
  40. "", "float", "double"};
  41. typedef enum
  42. {
  43. STK_FRAME, /* For stack vars */
  44. REG_FRAME, /* For register variables */
  45. GLB_FRAME /* For globals */
  46. } frameType;
  47. typedef struct
  48. {
  49. int16 seg; /* segment value */
  50. int16 off; /* offset */
  51. byte regi; /* optional indexed register */
  52. } BWGLB_TYPE;
  53. typedef struct
  54. { /* For TYPE_LONG_(UN)SIGN on the stack */
  55. Int offH; /* high offset from BP */
  56. Int offL; /* low offset from BP */
  57. } LONG_STKID_TYPE;
  58. typedef struct
  59. { /* For TYPE_LONG_(UN)SIGN registers */
  60. byte h; /* high register */
  61. byte l; /* low register */
  62. } LONGID_TYPE;
  63. /* ID, LOCAL_ID */
  64. struct ID
  65. {
  66. hlType type; /* Probable type */
  67. boolT illegal; /* Boolean: not a valid field any more */
  68. //std::vector<iICODE> idx;
  69. IDX_ARRAY idx; /* Index into icode array (REG_FRAME only) */
  70. frameType loc; /* Frame location */
  71. boolT hasMacro; /* Identifier requires a macro */
  72. char macro[10]; /* Macro for this identifier */
  73. char name[20]; /* Identifier's name */
  74. union { /* Different types of identifiers */
  75. byte regi; /* For TYPE_BYTE(WORD)_(UN)SIGN registers */
  76. struct { /* For TYPE_BYTE(WORD)_(UN)SIGN on the stack */
  77. byte regOff; /* register offset (if any) */
  78. Int off; /* offset from BP */
  79. } bwId;
  80. BWGLB_TYPE bwGlb; /* For TYPE_BYTE(WORD)_(UN)SIGN globals */
  81. LONGID_TYPE longId; /* For TYPE_LONG_(UN)SIGN registers */
  82. LONG_STKID_TYPE longStkId; /* For TYPE_LONG_(UN)SIGN on the stack */
  83. struct { /* For TYPE_LONG_(UN)SIGN globals */
  84. int16 seg; /* segment value */
  85. int16 offH; /* offset high */
  86. int16 offL; /* offset low */
  87. byte regi; /* optional indexed register */
  88. } longGlb;
  89. struct { /* For TYPE_LONG_(UN)SIGN constants */
  90. dword h; /* high word */
  91. dword l; /* low word */
  92. } longKte;
  93. } id;
  94. ID()
  95. {
  96. memset(this,0,sizeof(ID));
  97. }
  98. ID(hlType t, frameType f)
  99. {
  100. memset(this,0,sizeof(ID));
  101. type=t;
  102. loc=f;
  103. }
  104. bool isSigned() const { return (type==TYPE_BYTE_SIGN)||(type==TYPE_WORD_SIGN)||(type==TYPE_LONG_SIGN);}
  105. uint16_t typeBitsize() const
  106. {
  107. switch(type)
  108. {
  109. case TYPE_WORD_SIGN: case TYPE_WORD_UNSIGN:
  110. return 16;
  111. }
  112. return ~0;
  113. }
  114. };
  115. struct LOCAL_ID
  116. {
  117. std::vector<ID> id_arr;
  118. public:
  119. LOCAL_ID()
  120. {
  121. id_arr.reserve(256);
  122. }
  123. Int newByteWordReg(hlType t, byte regi);
  124. Int newByteWordStk(hlType t, Int off, byte regOff);
  125. Int newIntIdx(int16 seg, int16 off, byte regi, Int ix, hlType t);
  126. Int newLongReg(hlType t, byte regH, byte regL, iICODE ix_);
  127. Int newLong(opLoc sd, ICODE *pIcode, hlFirst f, iICODE ix, operDu du, Int off);
  128. void newIdent(hlType t, frameType f);
  129. void flagByteWordId(Int off);
  130. void propLongId(byte regL, byte regH, const char *name);
  131. size_t csym() const {return id_arr.size();}
  132. protected:
  133. Int newLongIdx(int16 seg, int16 offH, int16 offL, byte regi, hlType t);
  134. Int newLongGlb(int16 seg, int16 offH, int16 offL, hlType t);
  135. Int newLongStk(hlType t, Int offH, Int offL);
  136. };