icode.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #ifdef _lint
  26. for (auto ik=this->begin(); ik!=this->end(); ++ik)
  27. {
  28. ICODE &icode(*ik);
  29. #else
  30. for(ICODE &icode : *this)
  31. {
  32. #endif
  33. if((icode.loc_ip>=start) and (icode.loc_ip<=_end))
  34. icode.inBB = pnewBB;
  35. }
  36. }
  37. /* labelSrchRepl - Searches the icodes for instruction with label = target, and
  38. replaces *pIndex with an icode index */
  39. bool CIcodeRec::labelSrch(uint32_t target, uint32_t &pIndex)
  40. {
  41. iICODE location=labelSrch(target);
  42. if(end()==location)
  43. return false;
  44. pIndex=location->loc_ip;
  45. return true;
  46. }
  47. CIcodeRec::iterator CIcodeRec::labelSrch(uint32_t target)
  48. {
  49. return find_if(begin(),end(),[target](ICODE &l) -> bool {return l.ll()->label==target;});
  50. }
  51. ICODE * CIcodeRec::GetIcode(int ip)
  52. {
  53. assert(ip>=0 && ip<size());
  54. iICODE res=begin();
  55. advance(res,ip);
  56. return &(*res);
  57. }
  58. extern int getNextLabel();
  59. extern bundle cCode;
  60. /* Checks the given icode to determine whether it has a label associated
  61. * to it. If so, a goto is emitted to this label; otherwise, a new label
  62. * is created and a goto is also emitted.
  63. * Note: this procedure is to be used when the label is to be backpatched
  64. * onto code in cCode.code */
  65. void LLInst::emitGotoLabel (int indLevel)
  66. {
  67. if ( not testFlags(HLL_LABEL) ) /* node hasn't got a lab */
  68. {
  69. /* Generate new label */
  70. hllLabNum = getNextLabel();
  71. setFlags(HLL_LABEL);
  72. /* Node has been traversed already, so backpatch this label into
  73. * the code */
  74. addLabelBundle (cCode.code, codeIdx, hllLabNum);
  75. }
  76. cCode.appendCode( "%sgoto L%ld;\n", indentStr(indLevel), hllLabNum);
  77. stats.numHLIcode++;
  78. }