icode.cpp 2.1 KB

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