icode.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. ICODE::TypeFilter<HIGH_LEVEL> ICODE::select_high_level;
  12. ICODE::TypeAndValidFilter<HIGH_LEVEL> ICODE::select_valid_high_level;
  13. CIcodeRec::CIcodeRec()
  14. {
  15. }
  16. /* Copies the icode that is pointed to by pIcode to the icode array.
  17. * If there is need to allocate extra memory, it is done so, and
  18. * the alloc variable is adjusted. */
  19. ICODE * CIcodeRec::addIcode(ICODE *pIcode)
  20. {
  21. push_back(*pIcode);
  22. back().loc_ip = size()-1;
  23. return &back();
  24. }
  25. void CIcodeRec::SetInBB(rCODE &rang, BB *pnewBB)
  26. {
  27. for(ICODE &ic : rang)
  28. {
  29. ic.setParent(pnewBB);
  30. }
  31. }
  32. /* labelSrchRepl - Searches the icodes for instruction with label = target, and
  33. replaces *pIndex with an icode index */
  34. bool CIcodeRec::labelSrch(uint32_t target, uint32_t &pIndex)
  35. {
  36. iICODE location=labelSrch(target);
  37. if(end()==location)
  38. return false;
  39. pIndex=location->loc_ip;
  40. return true;
  41. }
  42. bool CIcodeRec::alreadyDecoded(uint32_t target)
  43. {
  44. iICODE location=labelSrch(target);
  45. if(end()==location)
  46. return false;
  47. return true;
  48. }
  49. CIcodeRec::iterator CIcodeRec::labelSrch(uint32_t target)
  50. {
  51. return find_if(begin(),end(),[target](ICODE &l) -> bool {return l.ll()->label==target;});
  52. }
  53. ICODE * CIcodeRec::GetIcode(size_t ip)
  54. {
  55. assert(ip<size());
  56. iICODE res=begin();
  57. advance(res,ip);
  58. return &(*res);
  59. }
  60. extern int getNextLabel();
  61. extern bundle cCode;
  62. /* Checks the given icode to determine whether it has a label associated
  63. * to it. If so, a goto is emitted to this label; otherwise, a new label
  64. * is created and a goto is also emitted.
  65. * Note: this procedure is to be used when the label is to be backpatched
  66. * onto code in cCode.code */
  67. void LLInst::emitGotoLabel (int indLevel)
  68. {
  69. if ( not testFlags(HLL_LABEL) ) /* node hasn't got a lab */
  70. {
  71. /* Generate new label */
  72. hllLabNum = getNextLabel();
  73. setFlags(HLL_LABEL);
  74. /* Node has been traversed already, so backpatch this label into
  75. * the code */
  76. cCode.code.addLabelBundle (codeIdx, hllLabNum);
  77. }
  78. cCode.appendCode( "%sgoto L%ld;\n", indentStr(indLevel), hllLabNum);
  79. stats.numHLIcode++;
  80. }
  81. bool LLOperand::isReg() const
  82. {
  83. return (regi>=rAX) && (regi<=rTMP);
  84. }
  85. void LLOperand::addProcInformation(int param_count, uint32_t call_conv)
  86. {
  87. proc.proc->cbParam = (int16_t)param_count;
  88. proc.cb = param_count;
  89. proc.proc->flg |= call_conv;
  90. }