machine_x86.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <cassert>
  2. #include "machine_x86.h"
  3. // Index registers **** temp solution
  4. static const std::string regNames[] = {
  5. "undef",
  6. "ax", "cx", "dx", "bx",
  7. "sp", "bp", "si", "di",
  8. "es", "cs", "ss", "ds",
  9. "al", "cl", "dl", "bl",
  10. "ah", "ch", "dh", "bh",
  11. "tmp",
  12. "bx+si", "bx+di", "bp+si", "bp+di",
  13. "si", "di", "bp", "bx"
  14. };
  15. /* uint8_t and uint16_t registers */
  16. Machine_X86::Machine_X86()
  17. {
  18. static_assert((sizeof(regNames)/sizeof(std::string))==LAST_REG,
  19. "Reg count not equal number of strings");
  20. }
  21. const std::string &Machine_X86::regName(eReg r)
  22. {
  23. assert(r<(sizeof(regNames)/sizeof(std::string)));
  24. return regNames[r];
  25. }
  26. static const std::string szOps[] =
  27. {
  28. "CBW", "AAA", "AAD", "AAM", "AAS", "ADC", "ADD", "AND",
  29. "BOUND","CALL", "CALL", "CLC", "CLD", "CLI", "CMC", "CMP",
  30. "CMPS", "REPNE CMPS","REPE CMPS","DAA", "DAS", "DEC", "DIV", "ENTER",
  31. "ESC", "HLT", "IDIV", "IMUL", "IN", "INC", "INS", "REP INS",
  32. "INT", "IRET", "JB", "JBE", "JAE", "JA", "JE", "JNE",
  33. "JL", "JGE", "JLE", "JG", "JS", "JNS", "JO", "JNO",
  34. "JP", "JNP", "JCXZ", "JMP", "JMP", "LAHF", "LDS", "LEA",
  35. "LEAVE","LES", "LOCK", "LODS", "REP LODS", "LOOP", "LOOPE","LOOPNE",
  36. "MOV", "MOVS", "REP MOVS", "MUL", "NEG", "NOT", "OR", "OUT",
  37. "OUTS", "REP OUTS", "POP", "POPA", "POPF", "PUSH", "PUSHA","PUSHF",
  38. "RCL", "RCR", "ROL", "ROR", "RET", "RETF", "SAHF", "SAR",
  39. "SHL", "SHR", "SBB", "SCAS", "REPNE SCAS","REPE SCAS", "CWD", "STC",
  40. "STD", "STI", "STOS", "REP STOS", "SUB", "TEST", "WAIT", "XCHG",
  41. "XLAT", "XOR", "INTO", "NOP", "REPNE", "REPE", "MOD"
  42. };
  43. /* The following opcodes are for mod != 3 */
  44. static std::string szFlops1[] =
  45. {
  46. /* 0 1 2 3 4 5 6 7 */
  47. "FADD", "FMUL", "FCOM", "FCOMP", "FSUB", "FSUBR", "FDIV", "FDIVR", /* 00 */
  48. "FLD", "???", "FST", "???", "FLDENV","FLDCW", "FSTENV","FSTSW", /* 08 */
  49. "FIADD", "FIMUL", "FICOM","FICOMP","FISUB", "FISUBR","FIDIV", "FIDIVR", /* 10 */
  50. "FILD", "???", "FIST", "FISTP", "???", "???", "???", "FSTP", /* 18 */
  51. "FADD", "FMUL", "FCOM", "FCOMP", "FSUB", "FSUBR", "FDIV", "FDIVR", /* 20 */
  52. "FLD", "FLD", "FST", "FSTP", "FRESTOR","???", "FSAVE", "FSTSW", /* 28 */
  53. "FIADD", "FIMUL", "FICOM","FICOMP","FISUB", "FISUBR","FIDIV", "FIDIVR", /* 30 */
  54. "FILD", "???", "FIST", "FISTP", "FBLD", "???", "FBSTP", "FISTP" /* 38 */
  55. };
  56. /* The following opcodes are for mod == 3 */
  57. static std::string szFlops2[] =
  58. {
  59. /* 0 1 2 3 4 5 6 7 */
  60. "FADD", "FMUL", "FCOM", "FCOMP", "FSUB", "FSUBR", "FDIV", "FDIVR", /* 00 */
  61. "FLD", "FXCH", "FNOP", "???", "", "", "", "", /* 08 */
  62. "FIADD", "FIMUL", "FICOM","FICOMP","FISUB", "", "FIDIV", "FIDIVR", /* 10 */
  63. "FILD", "???", "FIST", "FISTP", "???", "???", "???", "FSTP", /* 18 */
  64. "FADD", "FMUL", "FCOM", "FCOMP", "FSUB", "FSUBR", "FDIV", "FDIVR", /* 20 */
  65. "FFREE", "FSTP", "FST", "???", "FUCOM", "FUCOMP","???", "???", /* 28 */
  66. "FADDP", "FMULP", "FICOM","", "FSUBRP","FISUBR","FDIVRP","FDIVP", /* 30 */
  67. "FILD", "???", "FIST", "FISTP", "", "???", "FBSTP", "FISTP" /* 38 */
  68. };
  69. const std::string &Machine_X86::opcodeName(unsigned r)
  70. {
  71. assert(r<(sizeof(szOps)/sizeof(std::string)));
  72. return szOps[r];
  73. }
  74. const std::string &Machine_X86::floatOpName(unsigned r)
  75. {
  76. if(r>=(sizeof(szFlops1)/sizeof(std::string)))
  77. {
  78. r-= (sizeof(szFlops1)/sizeof(std::string));
  79. assert(r<(sizeof(szFlops2)/sizeof(std::string)));
  80. return szFlops2[r];
  81. }
  82. return szFlops1[r];
  83. }
  84. bool Machine_X86::physicalReg(eReg r)
  85. {
  86. return (r>=rAX) && (r<rTMP);
  87. }
  88. bool Machine_X86::isMemOff(eReg r)
  89. {
  90. return r == 0 || r >= INDEX_BX_SI;
  91. }
  92. //TODO: Move these to Machine_X86
  93. eReg Machine_X86::subRegH(eReg reg)
  94. {
  95. return eReg((int)reg + (int)rAH-(int)rAX);
  96. }
  97. eReg Machine_X86::subRegL(eReg reg)
  98. {
  99. return eReg((int)reg + (int)rAL-(int)rAX);
  100. }
  101. bool Machine_X86::isSubRegisterOf(eReg reg,eReg parent)
  102. {
  103. if ((parent < rAX) || (parent > rBX))
  104. return false; // only AX -> BX are coverede by subregisters
  105. return ((reg==subRegH(parent)) || (reg == subRegL(parent)));
  106. }