icode.cpp 2.4 KB

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