icode.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. back().loc_ip = size()-1;
  24. return &back();
  25. }
  26. ICODE * CIcodeRec::GetFirstIcode()
  27. {
  28. return &front();
  29. }
  30. /* Don't need this; just pIcode++ since array is guaranteed to be contiguous
  31. ICODE * CIcodeRec::GetNextIcode(ICODE * pCurIcode)
  32. {
  33. int idx = pCurIcode - icode; // Current index
  34. ASSERT(idx+1 < numIcode);
  35. return &icode[idx+1];
  36. }
  37. */
  38. boolT CIcodeRec::IsValid(ICODE *pCurIcode)
  39. {
  40. ptrdiff_t idx = pCurIcode - &this->front(); // Current index
  41. return (idx>=0) && (idx < size());
  42. }
  43. int CIcodeRec::GetNumIcodes()
  44. {
  45. return size();
  46. }
  47. void CIcodeRec::SetInBB(int start, int end, BB *pnewBB)
  48. {
  49. for (int i = start; i <= end; i++)
  50. at(i).inBB = pnewBB;
  51. }
  52. void CIcodeRec::SetImmediateOp(int ip, dword dw)
  53. {
  54. at(ip).ic.ll.immed.op = dw;
  55. }
  56. void CIcodeRec::SetLlFlag(int ip, dword flag)
  57. {
  58. at(ip).ic.ll.flg |= flag;
  59. }
  60. dword CIcodeRec::GetLlFlag(int ip)
  61. {
  62. return at(ip).ic.ll.flg;
  63. }
  64. void CIcodeRec::ClearLlFlag(int ip, dword flag)
  65. {
  66. at(ip).ic.ll.flg &= (~flag);
  67. }
  68. void CIcodeRec::SetLlInvalid(int ip, boolT fInv)
  69. {
  70. at(ip).invalid = fInv;
  71. }
  72. dword CIcodeRec::GetLlLabel(int ip)
  73. {
  74. return at(ip).ic.ll.label;
  75. }
  76. llIcode CIcodeRec::GetLlOpcode(int ip)
  77. {
  78. return at(ip).ic.ll.opcode;
  79. }
  80. /* labelSrchRepl - Searches the icodes for instruction with label = target, and
  81. replaces *pIndex with an icode index */
  82. boolT CIcodeRec::labelSrch(dword target, Int *pIndex)
  83. {
  84. Int i;
  85. for (i = 0; i < size(); i++)
  86. {
  87. if (at(i).ic.ll.label == target)
  88. {
  89. *pIndex = i;
  90. return TRUE;
  91. }
  92. }
  93. return FALSE;
  94. }
  95. ICODE * CIcodeRec::GetIcode(int ip)
  96. {
  97. return &at(ip);
  98. }
  99. extern char *indent(int level);
  100. extern Int getNextLabel();
  101. extern bundle cCode;
  102. /* Checks the given icode to determine whether it has a label associated
  103. * to it. If so, a goto is emitted to this label; otherwise, a new label
  104. * is created and a goto is also emitted.
  105. * Note: this procedure is to be used when the label is to be backpatched
  106. * onto code in cCode.code */
  107. void ICODE::emitGotoLabel (Int indLevel)
  108. {
  109. if (! (ic.ll.flg & HLL_LABEL)) /* node hasn't got a lab */
  110. {
  111. /* Generate new label */
  112. ic.ll.hllLabNum = getNextLabel();
  113. ic.ll.flg |= HLL_LABEL;
  114. /* Node has been traversed already, so backpatch this label into
  115. * the code */
  116. addLabelBundle (cCode.code, codeIdx, ic.ll.hllLabNum);
  117. }
  118. cCode.appendCode( "%sgoto L%ld;\n", indent(indLevel), ic.ll.hllLabNum);
  119. stats.numHLIcode++;
  120. }