mov_idioms.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. /* Check for regL */
  29. m_regL = m_icodes[0]->ll()->dst.regi;
  30. if (not m_icodes[0]->ll()->isLlFlag(I) && ((m_regL == rAX) || (m_regL ==rBX)))
  31. {
  32. /* Check for XOR regH, regH */
  33. if (m_icodes[1]->ll()->match(iXOR) && not m_icodes[1]->ll()->isLlFlag(I))
  34. {
  35. m_regH = m_icodes[1]->ll()->dst.regi;
  36. if (m_regH == m_icodes[1]->ll()->src.regi)
  37. {
  38. if ((m_regL == rAX) && (m_regH == rDX))
  39. return true;
  40. if ((m_regL == rBX) && (m_regH == rCX))
  41. return true;
  42. }
  43. }
  44. }
  45. return false;
  46. }
  47. int Idiom14::action()
  48. {
  49. int idx;
  50. COND_EXPR *lhs,*rhs;
  51. idx = m_func->localId.newLongReg (TYPE_LONG_SIGN, m_regH, m_regL, m_icodes[0]);
  52. lhs = COND_EXPR::idLongIdx (idx);
  53. m_icodes[0]->setRegDU( m_regH, eDEF);
  54. rhs = COND_EXPR::id (*m_icodes[0], SRC, m_func, m_icodes[0], *m_icodes[0], NONE);
  55. m_icodes[0]->setAsgn(lhs, rhs);
  56. m_icodes[1]->invalidate();
  57. return 2;
  58. }
  59. /*****************************************************************************
  60. * idiom 13 - uint16_t assign
  61. * MOV regL, mem
  62. * MOV regH, 0
  63. * Eg: MOV al, [bp-2]
  64. * MOV ah, 0
  65. * => MOV ax, [bp-2]
  66. * Found in Borland Turbo C, used for multiplication and division of
  67. * uint8_t operands (ie. they need to be extended to words).
  68. ****************************************************************************/
  69. bool Idiom13::match(iICODE pIcode)
  70. {
  71. if(distance(pIcode,m_end)<2)
  72. return false;
  73. m_icodes[0]=pIcode++;
  74. m_icodes[1]=pIcode++;
  75. m_loaded_reg = 0;
  76. uint8_t regi;
  77. /* Check for regL */
  78. regi = m_icodes[0]->ll()->dst.regi;
  79. if (not m_icodes[0]->ll()->isLlFlag(I) && (regi >= rAL) && (regi <= rBH))
  80. {
  81. /* Check for MOV regH, 0 */
  82. if (m_icodes[1]->ll()->match(iMOV) && m_icodes[1]->ll()->isLlFlag(I) && (m_icodes[1]->ll()->src.op() == 0))
  83. {
  84. if (m_icodes[1]->ll()->dst.regi == (regi + 4)) //TODO: based on distance between AH-AL,BH-BL etc.
  85. {
  86. m_loaded_reg=(regi - rAL + rAX);
  87. return true;
  88. }
  89. }
  90. }
  91. return false;
  92. }
  93. int Idiom13::action()
  94. {
  95. COND_EXPR *lhs,*rhs;
  96. lhs = COND_EXPR::idReg (m_loaded_reg, 0, &m_func->localId);
  97. m_icodes[0]->setRegDU( m_loaded_reg, eDEF);
  98. m_icodes[0]->du1.numRegsDef--; /* prev uint8_t reg def */
  99. rhs = COND_EXPR::id (*m_icodes[0], SRC, m_func, m_icodes[0], *m_icodes[0], NONE);
  100. m_icodes[0]->setAsgn(lhs, rhs);
  101. m_icodes[1]->invalidate();
  102. return 2;
  103. }