icode.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. //reserve(512);
  14. }
  15. CIcodeRec::~CIcodeRec()
  16. {
  17. }
  18. /* Copies the icode that is pointed to by pIcode to the icode array.
  19. * If there is need to allocate extra memory, it is done so, and
  20. * the alloc variable is adjusted. */
  21. ICODE * CIcodeRec::addIcode(ICODE *pIcode)
  22. {
  23. push_back(*pIcode);
  24. back().loc_ip = size()-1;
  25. return &back();
  26. }
  27. void CIcodeRec::SetInBB(int start, int end, BB *pnewBB)
  28. {
  29. for (int i = start; i <= end; i++)
  30. at(i).inBB = pnewBB;
  31. }
  32. void CIcodeRec::SetImmediateOp(int ip, dword dw)
  33. {
  34. at(ip).ic.ll.immed.op = dw;
  35. }
  36. dword CIcodeRec::GetLlLabel(int ip)
  37. {
  38. return at(ip).ic.ll.label;
  39. }
  40. llIcode CIcodeRec::GetLlOpcode(int ip)
  41. {
  42. return at(ip).ic.ll.opcode;
  43. }
  44. /* labelSrchRepl - Searches the icodes for instruction with label = target, and
  45. replaces *pIndex with an icode index */
  46. bool CIcodeRec::labelSrch(dword target, dword &pIndex)
  47. {
  48. Int i;
  49. for (i = 0; i < size(); i++)
  50. {
  51. if (at(i).ic.ll.label == target)
  52. {
  53. pIndex = i;
  54. return TRUE;
  55. }
  56. }
  57. return FALSE;
  58. }
  59. ICODE * CIcodeRec::GetIcode(int ip)
  60. {
  61. return &at(ip);
  62. }
  63. extern char *indent(int level);
  64. extern Int getNextLabel();
  65. extern bundle cCode;
  66. /* Checks the given icode to determine whether it has a label associated
  67. * to it. If so, a goto is emitted to this label; otherwise, a new label
  68. * is created and a goto is also emitted.
  69. * Note: this procedure is to be used when the label is to be backpatched
  70. * onto code in cCode.code */
  71. void ICODE::emitGotoLabel (Int indLevel)
  72. {
  73. if (! (ic.ll.flg & HLL_LABEL)) /* node hasn't got a lab */
  74. {
  75. /* Generate new label */
  76. ic.ll.hllLabNum = getNextLabel();
  77. ic.ll.flg |= HLL_LABEL;
  78. /* Node has been traversed already, so backpatch this label into
  79. * the code */
  80. addLabelBundle (cCode.code, codeIdx, ic.ll.hllLabNum);
  81. }
  82. cCode.appendCode( "%sgoto L%ld;\n", indent(indLevel), ic.ll.hllLabNum);
  83. stats.numHLIcode++;
  84. }