icode.cpp 2.4 KB

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