engine.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* ScummVM Tools
  2. *
  3. * ScummVM Tools is the legal property of its developers, whose
  4. * names are too numerous to list here. Please refer to the
  5. * COPYRIGHT file distributed with this source distribution.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. */
  21. #include "engine.h"
  22. #include "disassembler.h"
  23. #include "codegen.h"
  24. #include "make_unique.h"
  25. std::ostream &Scumm::v6::Scummv6StringValue::print(std::ostream &output) const {
  26. return output << _str;
  27. }
  28. std::unique_ptr<Disassembler> Scumm::v6::Scummv6Engine::getDisassembler(InstVec &insts)
  29. {
  30. return std::make_unique<Scummv6Disassembler>(insts);
  31. }
  32. std::unique_ptr<CodeGenerator> Scumm::v6::Scummv6Engine::getCodeGenerator(const InstVec&, std::ostream &output)
  33. {
  34. return std::make_unique<Scummv6CodeGenerator>(this, output);
  35. }
  36. void Scumm::v6::Scummv6LoadInstruction::processInst(Function&, ValueStack &stack, Engine*, CodeGenerator *codeGen) {
  37. Scummv6CodeGenerator *cg = (Scummv6CodeGenerator *)codeGen;
  38. switch (_opcode) {
  39. case 0x00: // pushByte
  40. case 0x01: // pushWord
  41. stack.push(_params[0]);
  42. break;
  43. case 0x02: // pushByteVar
  44. case 0x03: // pushWordVar
  45. stack.push(new VarValue(cg->decodeVarName(static_cast<uint16>(_params[0]->getUnsigned()))));
  46. break;
  47. case 0x06: // byteArrayRead
  48. case 0x07: // wordArrayRead
  49. {
  50. ValueList idxs;
  51. idxs.push_front(stack.pop());
  52. stack.push(new ArrayValue(cg->decodeArrayName(static_cast<uint16>(_params[0]->getUnsigned())), idxs));
  53. break;
  54. }
  55. case 0x0A: // byteArrayIndexedRead
  56. case 0x0B: // wordArrayIndexedRead
  57. {
  58. ValueList idxs;
  59. idxs.push_front(stack.pop());
  60. idxs.push_front(stack.pop());
  61. stack.push(new ArrayValue(cg->decodeArrayName(static_cast<uint16>(_params[0]->getUnsigned())), idxs));
  62. break;
  63. }
  64. }
  65. }
  66. void Scumm::v6::Scummv6StoreInstruction::processInst(Function&, ValueStack &stack, Engine*, CodeGenerator *codeGen) {
  67. Scummv6CodeGenerator *cg = (Scummv6CodeGenerator *)codeGen;
  68. switch (_opcode) {
  69. case 0x42: // writeByteVar
  70. case 0x43: // writeWordVar
  71. {
  72. ValuePtr p = new VarValue(cg->decodeVarName(static_cast<uint16>(_params[0]->getUnsigned())));
  73. cg->writeAssignment(p, stack.pop());
  74. }
  75. break;
  76. case 0x46: // byteArrayWrite
  77. case 0x47: // wordArrayWrite
  78. {
  79. ValuePtr value = stack.pop();
  80. ValueList idxs;
  81. idxs.push_back(stack.pop());
  82. ValuePtr p = new ArrayValue(cg->decodeArrayName(static_cast<uint16>(_params[0]->getUnsigned())), idxs);
  83. cg->writeAssignment(p, value);
  84. }
  85. break;
  86. case 0x4A: // byteArrayIndexedWrite
  87. case 0x4B: // wordArrayIndexedWrite
  88. {
  89. ValuePtr value = stack.pop();
  90. ValueList idxs;
  91. idxs.push_front(stack.pop());
  92. idxs.push_front(stack.pop());
  93. ValuePtr p = new ArrayValue(cg->decodeArrayName(static_cast<uint16>(_params[0]->getUnsigned())), idxs);
  94. cg->writeAssignment(p, value);
  95. }
  96. break;
  97. }
  98. }
  99. void Scumm::v6::Scummv6StackInstruction::processInst(Function&, ValueStack &stack, Engine*, CodeGenerator*) {
  100. stack.pop();
  101. }
  102. void Scumm::v6::Scummv6CondJumpInstruction::processInst(Function&, ValueStack &stack, Engine*, CodeGenerator*) {
  103. if (_opcode == 0x5D) // jumpFalse
  104. stack.push(stack.pop()->negate());
  105. }
  106. uint32 Scumm::v6::Scummv6CondJumpInstruction::getDestAddress() const {
  107. return _params[0]->getUnsigned();
  108. }
  109. uint32 Scumm::v6::Scummv6JumpInstruction::getDestAddress() const {
  110. return _params[0]->getUnsigned();
  111. }
  112. void Scumm::v6::Scummv6IncDecInstruction::processInst(Function&, ValueStack &stack, Engine*, CodeGenerator *codeGen) {
  113. Scummv6CodeGenerator *cg = (Scummv6CodeGenerator *)codeGen;
  114. switch (_opcode) {
  115. case 0x4E: // byteVarInc
  116. case 0x4F: // wordVarInc
  117. case 0x56: // byteVarDec
  118. case 0x57: // wordVarDec
  119. {
  120. std::stringstream s;
  121. ValuePtr p = new UnaryOpValue(new VarValue(cg->decodeVarName(static_cast<uint16>(_params[0]->getUnsigned()))), _codeGenData, true);
  122. s << p << ";";
  123. cg->addOutputLine(s.str());
  124. }
  125. break;
  126. case 0x52: // byteArrayInc
  127. case 0x53: // wordArrayInc
  128. case 0x5A: // byteArrayDec
  129. case 0x5B: // wordArrayDec
  130. {
  131. std::stringstream s;
  132. ValueList idxs;
  133. idxs.push_front(stack.pop());
  134. ValuePtr p = new UnaryOpValue(new ArrayValue(cg->decodeVarName(static_cast<uint16>(_params[0]->getUnsigned())), idxs), _codeGenData, true);
  135. s << p << ";";
  136. cg->addOutputLine(s.str());
  137. }
  138. break;
  139. }
  140. }
  141. void Scumm::v6::Scummv6ArrayOpInstruction::processInst(Function&, ValueStack &stack, Engine*, CodeGenerator *codeGen) {
  142. Scummv6CodeGenerator *cg = (Scummv6CodeGenerator *)codeGen;
  143. switch (_opcode) {
  144. case 0xA4CD: // arrayOp_assignString
  145. {
  146. ValuePtr value = _params[1];
  147. ValueList idxs;
  148. idxs.push_front(stack.pop());
  149. ValuePtr p = new ArrayValue(cg->decodeArrayName(static_cast<uint16>(_params[0]->getUnsigned())), idxs);
  150. cg->writeAssignment(p, value);
  151. }
  152. break;
  153. case 0xA4D0: // arrayOp_assignIntList
  154. {
  155. ValueList idxs;
  156. idxs.push_front(stack.pop());
  157. ValuePtr value = cg->createListValue();
  158. ValuePtr p = new ArrayValue(cg->decodeArrayName(static_cast<uint16>(_params[0]->getUnsigned())), idxs);
  159. cg->writeAssignment(p, value);
  160. }
  161. break;
  162. case 0xA4D4: // arrayOp_assign2DimList
  163. {
  164. ValueList idxs;
  165. idxs.push_front(stack.pop());
  166. ValuePtr value = cg->createListValue();
  167. idxs.push_front(stack.pop());
  168. ValuePtr p = new ArrayValue(cg->decodeArrayName(static_cast<uint16>(_params[0]->getUnsigned())), idxs);
  169. cg->writeAssignment(p, value);
  170. }
  171. break;
  172. }
  173. }