icode.cpp 2.3 KB

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