icode.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Object oriented icode code for dcc
  2. // (C) 1997 Mike Van Emmerik
  3. #include <stdlib.h>
  4. #include <malloc.h>
  5. #include <memory.h>
  6. #include "dcc.h"
  7. #include "types.h" // Common types like byte, etc
  8. #include "ast.h" // Some icode types depend on these
  9. #include "icode.h"
  10. #define ICODE_DELTA 25 // Amount to allocate for new chunk
  11. CIcodeRec::CIcodeRec()
  12. {
  13. }
  14. CIcodeRec::~CIcodeRec()
  15. {
  16. }
  17. /* Copies the icode that is pointed to by pIcode to the icode array.
  18. * If there is need to allocate extra memory, it is done so, and
  19. * the alloc variable is adjusted. */
  20. ICODE * CIcodeRec::addIcode(ICODE *pIcode)
  21. {
  22. push_back(*pIcode);
  23. return &back();
  24. }
  25. ICODE * CIcodeRec::GetFirstIcode()
  26. {
  27. return &front();
  28. }
  29. /* Don't need this; just pIcode++ since array is guaranteed to be contiguous
  30. ICODE * CIcodeRec::GetNextIcode(ICODE * pCurIcode)
  31. {
  32. int idx = pCurIcode - icode; // Current index
  33. ASSERT(idx+1 < numIcode);
  34. return &icode[idx+1];
  35. }
  36. */
  37. boolT CIcodeRec::IsValid(ICODE *pCurIcode)
  38. {
  39. ptrdiff_t idx = pCurIcode - &this->front(); // Current index
  40. return (idx>=0) && (idx < size());
  41. }
  42. int CIcodeRec::GetNumIcodes()
  43. {
  44. return size();
  45. }
  46. void CIcodeRec::SetInBB(int start, int end, BB *pnewBB)
  47. {
  48. for (int i = start; i <= end; i++)
  49. at(i).inBB = pnewBB;
  50. }
  51. void CIcodeRec::SetImmediateOp(int ip, dword dw)
  52. {
  53. at(ip).ic.ll.immed.op = dw;
  54. }
  55. void CIcodeRec::SetLlFlag(int ip, dword flag)
  56. {
  57. at(ip).ic.ll.flg |= flag;
  58. }
  59. dword CIcodeRec::GetLlFlag(int ip)
  60. {
  61. return at(ip).ic.ll.flg;
  62. }
  63. void CIcodeRec::ClearLlFlag(int ip, dword flag)
  64. {
  65. at(ip).ic.ll.flg &= (~flag);
  66. }
  67. void CIcodeRec::SetLlInvalid(int ip, boolT fInv)
  68. {
  69. at(ip).invalid = fInv;
  70. }
  71. dword CIcodeRec::GetLlLabel(int ip)
  72. {
  73. return at(ip).ic.ll.label;
  74. }
  75. llIcode CIcodeRec::GetLlOpcode(int ip)
  76. {
  77. return at(ip).ic.ll.opcode;
  78. }
  79. /* labelSrchRepl - Searches the icodes for instruction with label = target, and
  80. replaces *pIndex with an icode index */
  81. boolT CIcodeRec::labelSrch(dword target, Int *pIndex)
  82. {
  83. Int i;
  84. for (i = 0; i < size(); i++)
  85. {
  86. if (at(i).ic.ll.label == target)
  87. {
  88. *pIndex = i;
  89. return TRUE;
  90. }
  91. }
  92. return FALSE;
  93. }
  94. ICODE * CIcodeRec::GetIcode(int ip)
  95. {
  96. return &at(ip);
  97. }
  98. extern char *indent(int level);
  99. extern Int getNextLabel();
  100. extern bundle cCode;
  101. /* Checks the given icode to determine whether it has a label associated
  102. * to it. If so, a goto is emitted to this label; otherwise, a new label
  103. * is created and a goto is also emitted.
  104. * Note: this procedure is to be used when the label is to be backpatched
  105. * onto code in cCode.code */
  106. void ICODE::emitGotoLabel (Int indLevel)
  107. {
  108. if (! (ic.ll.flg & HLL_LABEL)) /* node hasn't got a lab */
  109. {
  110. /* Generate new label */
  111. ic.ll.hllLabNum = getNextLabel();
  112. ic.ll.flg |= HLL_LABEL;
  113. /* Node has been traversed already, so backpatch this label into
  114. * the code */
  115. addLabelBundle (cCode.code, codeIdx, ic.ll.hllLabNum);
  116. }
  117. cCode.appendCode( "%sgoto L%ld;\n", indent(indLevel), ic.ll.hllLabNum);
  118. stats.numHLIcode++;
  119. }