icode.cpp 2.5 KB

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