mov_idioms.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "dcc.h"
  2. #include "mov_idioms.h"
  3. using namespace std;
  4. /*****************************************************************************
  5. * idiom 14 - Long uint16_t assign
  6. * MOV regL, mem/reg
  7. * XOR regH, regH
  8. * Eg: MOV ax, di
  9. * XOR dx, dx
  10. * => MOV dx:ax, di
  11. * Note: only the following combinations are allowed:
  12. * dx:ax
  13. * cx:bx
  14. * this is to remove the possibility of making errors in situations
  15. * like this:
  16. * MOV dx, offH
  17. * MOV ax, offL
  18. * XOR cx, cx
  19. * Found in Borland Turbo C, used for division of unsigned integer
  20. * operands.
  21. ****************************************************************************/
  22. bool Idiom14::match(iICODE pIcode)
  23. {
  24. if(distance(pIcode,m_end)<2)
  25. return false;
  26. m_icodes[0]=pIcode++;
  27. m_icodes[1]=pIcode++;
  28. LLInst * matched [] {m_icodes[0]->ll(),m_icodes[1]->ll()};
  29. /* Check for regL */
  30. m_regL = matched[0]->m_dst.regi;
  31. if (not matched[0]->testFlags(I) && ((m_regL == rAX) || (m_regL ==rBX)))
  32. {
  33. /* Check for XOR regH, regH */
  34. if (matched[1]->match(iXOR) && not matched[1]->testFlags(I))
  35. {
  36. m_regH = matched[1]->m_dst.regi;
  37. if (m_regH == matched[1]->src().getReg2())
  38. {
  39. if ((m_regL == rAX) && (m_regH == rDX))
  40. return true;
  41. if ((m_regL == rBX) && (m_regH == rCX))
  42. return true;
  43. }
  44. }
  45. }
  46. return false;
  47. }
  48. int Idiom14::action()
  49. {
  50. int idx = m_func->localId.newLongReg (TYPE_LONG_SIGN, LONGID_TYPE(m_regH,m_regL), m_icodes[0]);
  51. AstIdent *lhs = AstIdent::LongIdx (idx);
  52. m_icodes[0]->setRegDU( m_regH, eDEF);
  53. Expr *rhs = AstIdent::id (*m_icodes[0]->ll(), SRC, m_func, m_icodes[0], *m_icodes[0], NONE);
  54. m_icodes[0]->setAsgn(lhs, rhs);
  55. m_icodes[1]->invalidate();
  56. return 2;
  57. }
  58. /*****************************************************************************
  59. * idiom 13 - uint16_t assign
  60. * MOV regL, mem
  61. * MOV regH, 0
  62. * Eg: MOV al, [bp-2]
  63. * MOV ah, 0
  64. * => MOV ax, [bp-2]
  65. * Found in Borland Turbo C, used for multiplication and division of
  66. * uint8_t operands (ie. they need to be extended to words).
  67. ****************************************************************************/
  68. bool Idiom13::match(iICODE pIcode)
  69. {
  70. if(distance(pIcode,m_end)<2)
  71. return false;
  72. m_icodes[0]=pIcode++;
  73. m_icodes[1]=pIcode++;
  74. m_loaded_reg = rUNDEF;
  75. eReg regi;
  76. /* Check for regL */
  77. regi = m_icodes[0]->ll()->m_dst.regi;
  78. if (not m_icodes[0]->ll()->testFlags(I) && (regi >= rAL) && (regi <= rBH))
  79. {
  80. /* Check for MOV regH, 0 */
  81. if (m_icodes[1]->ll()->match(iMOV,I) && (m_icodes[1]->ll()->src().getImm2() == 0))
  82. {
  83. if (m_icodes[1]->ll()->m_dst.regi == (regi + 4)) //WARNING: based on distance between AH-AL,BH-BL etc.
  84. {
  85. m_loaded_reg=(eReg)(regi - rAL + rAX);
  86. return true;
  87. }
  88. }
  89. }
  90. return false;
  91. }
  92. int Idiom13::action()
  93. {
  94. AstIdent *lhs;
  95. Expr *rhs;
  96. eReg regi = m_icodes[0]->ll()->m_dst.regi;
  97. m_icodes[0]->du1.removeDef(regi);
  98. //m_icodes[0]->du1.numRegsDef--; /* prev uint8_t reg def */
  99. lhs = new RegisterNode(LLOperand(m_loaded_reg, 0), &m_func->localId);
  100. m_icodes[0]->setRegDU( m_loaded_reg, eDEF);
  101. rhs = AstIdent::id (*m_icodes[0]->ll(), SRC, m_func, m_icodes[0], *m_icodes[0], NONE);
  102. m_icodes[0]->setAsgn(lhs, rhs);
  103. m_icodes[1]->invalidate();
  104. return 2;
  105. }