instruction.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. #ifndef DEC_INSTRUCTION_H
  22. #define DEC_INSTRUCTION_H
  23. #include <string>
  24. #include <vector>
  25. #include <boost/format.hpp>
  26. #include <boost/intrusive_ptr.hpp>
  27. #include "common/scummsys.h"
  28. #include "refcounted.h"
  29. #include "value.h"
  30. #include "wrongtype.h"
  31. class CodeGenerator;
  32. class Engine;
  33. /**
  34. * Changes whether or not to output the stack effect for an instruction.
  35. */
  36. void setOutputStackEffect(bool value);
  37. /**
  38. * Constants for categorizing the different kinds of instructions.
  39. */
  40. const int kBinaryOpInst = 0; ///< Binary operation (e.g. +, &&, etc.), including comparisons.
  41. const int kBoolNegateInst = 1; ///< Boolean negation.
  42. const int kCallInst = 2; ///< Regular function call.
  43. const int kCondJumpInst = 3; ///< Conditional jump.
  44. const int kDupInst = 4; ///< Instruction duplicates the most recent stack entry.
  45. const int kJumpInst = 5; ///< Unconditional jump.
  46. const int kKernelCallInst = 6; ///< Kernel functions.
  47. const int kLoadInst = 7; ///< Load value from memory.
  48. const int kReturnInst = 8; ///< Return from regular function call.
  49. const int kStackInst = 9; ///< Stack allocation or deallocation (altering stack pointer)
  50. const int kStoreInst = 10; ///< Store value in memory.
  51. const int kUnaryOpPreInst = 11; ///< Unary operation (e.g. !) with operator placed before the operator.
  52. const int kUnaryOpPostInst = 12; ///< Unary operation with operator placed after the operator.
  53. const int kFirstCustomInst = kUnaryOpPostInst + 1; ///< First unused key. Add your custom type keys starting with this value.
  54. struct Instruction;
  55. /**
  56. * Pointer to an Instruction.
  57. */
  58. typedef boost::intrusive_ptr<Instruction> InstPtr;
  59. class Function;
  60. /**
  61. * Structure for representing an instruction.
  62. */
  63. struct Instruction : public RefCounted {
  64. public:
  65. uint32 _opcode; ///< The instruction opcode.
  66. uint32 _address; ///< The instruction address.
  67. std::string _name; ///< The instruction name (opcode name).
  68. int16 _stackChange; ///< How much this instruction changes the stack pointer by.
  69. std::vector<ValuePtr> _params; ///< Array of parameters used for the instruction.
  70. std::string _codeGenData; ///< String containing metadata for code generation. See the extended documentation for details.
  71. bool mLabelRequired = false;
  72. /**
  73. * Operator overload to output an Instruction to a std::ostream.
  74. *
  75. * @param output The std::ostream to output to.
  76. * @param inst The Instruction to output.
  77. * @return The std::ostream used for output.
  78. */
  79. friend std::ostream &operator<<(std::ostream &output, const Instruction *inst) {
  80. return inst->Print(output);
  81. }
  82. /**
  83. * Print the instruction to an std::ostream.
  84. *
  85. * @param output The std::ostream to write to.
  86. * @return The std::ostream used for output.
  87. */
  88. virtual std::ostream &Print(std::ostream &output) const;
  89. /**
  90. * Returns whether or not the instruction is a jump of some sort.
  91. *
  92. * @return True if the instruction is a jump, otherwise false.
  93. */
  94. virtual bool isJump() const;
  95. /**
  96. * Returns whether or not the instruction is a conditional jump.
  97. *
  98. * @return True if the instruction is a conditional jump, otherwise false.
  99. */
  100. virtual bool isCondJump() const;
  101. /**
  102. * Returns whether or not the instruction is an unconditional jump.
  103. *
  104. * @return True if the instruction is an unconditional jump, otherwise false.
  105. */
  106. virtual bool IsUncondJump() const;
  107. /**
  108. * Returns whether or not the instruction is a stack operation.
  109. *
  110. * @return True if the instruction is a stack operation, otherwise false.
  111. */
  112. virtual bool isStackOp() const;
  113. /**
  114. * Returns whether or not the instruction is a call to a script function.
  115. *
  116. * @return True if the instruction is a script function call, otherwise false.
  117. */
  118. virtual bool IsFuncCall() const;
  119. /**
  120. * Returns whether or not the instruction is a return statement.
  121. *
  122. * @return True if the instruction is a return statement, otherwise false.
  123. */
  124. virtual bool isReturn() const;
  125. /**
  126. * Returns whether or not the instruction is a call to a kernel function.
  127. *
  128. * @return True if the instruction is a kernel function call, otherwise false.
  129. */
  130. virtual bool isKernelCall() const;
  131. /**
  132. * Returns whether or not the instruction is a load operation.
  133. *
  134. * @return True if the instruction is a load operation, otherwise false.
  135. */
  136. virtual bool isLoad() const;
  137. /**
  138. * Returns whether or not the instruction is a store operation.
  139. *
  140. * @return True if the instruction is a store operation, otherwise false.
  141. */
  142. virtual bool isStore() const;
  143. /**
  144. * Returns the destination address of a jump instruction.
  145. *
  146. * @return Destination address of a jump instruction.
  147. * @throws WrongTypeException if instruction is not a jump.
  148. */
  149. virtual uint32 GetDestAddress() const;
  150. /**
  151. * Process an instruction for code generation.
  152. *
  153. * @param stack The current stack.
  154. * @param engine Pointer to the Engine used for code generation.
  155. * @param codeGen Pointer to the CodeGenerator used for code generation.
  156. */
  157. virtual void ProcessInst(Function& func, ValueStack &stack, Engine *engine, CodeGenerator *codeGen) = 0;
  158. };
  159. /**
  160. * Instruction performing a jump.
  161. */
  162. struct JumpInstruction : public Instruction {
  163. };
  164. /**
  165. * Instruction performing a conditional jump.
  166. */
  167. struct CondJumpInstruction : public JumpInstruction {
  168. public:
  169. virtual bool isCondJump() const;
  170. };
  171. /**
  172. * Instruction performing an unconditional jump.
  173. */
  174. struct UncondJumpInstruction : public JumpInstruction {
  175. public:
  176. virtual bool IsUncondJump() const override;
  177. virtual void ProcessInst(Function& func, ValueStack &stack, Engine *engine, CodeGenerator *codeGen) override;
  178. };
  179. /**
  180. * Instruction performing a stack operation.
  181. */
  182. struct StackInstruction : public Instruction {
  183. public:
  184. virtual bool isStackOp() const;
  185. };
  186. /**
  187. * Instruction performing a script function call.
  188. */
  189. struct CallInstruction : public Instruction {
  190. public:
  191. virtual bool IsFuncCall() const;
  192. };
  193. /**
  194. * Instruction which loads data from memory.
  195. */
  196. struct LoadInstruction : public Instruction {
  197. public:
  198. virtual bool isLoad() const;
  199. };
  200. /**
  201. * Instruction which stores data to memory.
  202. */
  203. struct StoreInstruction : public Instruction {
  204. public:
  205. virtual bool isStore() const;
  206. };
  207. /**
  208. * Instruction duplicating the topmost stack value.
  209. */
  210. struct DupInstruction : public Instruction {
  211. };
  212. /**
  213. * Instruction performing boolean negation.
  214. */
  215. struct BoolNegateInstruction : public Instruction {
  216. };
  217. /**
  218. * Instruction performing a binary operation.
  219. */
  220. struct BinaryOpInstruction : public Instruction {
  221. };
  222. /**
  223. * Instruction performing a unary operation.
  224. */
  225. struct UnaryOpInstruction : public Instruction {
  226. };
  227. /**
  228. * Instruction performing a kernel function call.
  229. */
  230. struct KernelCallInstruction : public Instruction {
  231. public:
  232. virtual bool isKernelCall() const;
  233. };
  234. /**
  235. * Default implementation for stack-based instruction duplicating the topmost stack value.
  236. */
  237. struct DupStackInstruction : public DupInstruction {
  238. public:
  239. virtual void ProcessInst(Function& func, ValueStack &stack, Engine *engine, CodeGenerator *codeGen) override;
  240. };
  241. /**
  242. * Default implementation for stack-based instruction performing boolean negation.
  243. */
  244. struct BoolNegateStackInstruction : public BoolNegateInstruction {
  245. public:
  246. virtual void ProcessInst(Function& func, ValueStack &stack, Engine *engine, CodeGenerator *codeGen) override;
  247. };
  248. /**
  249. * Default implementation for stack-based instruction performing a binary operation.
  250. */
  251. struct BinaryOpStackInstruction : public BinaryOpInstruction {
  252. public:
  253. virtual void ProcessInst(Function& func, ValueStack &stack, Engine *engine, CodeGenerator *codeGen) override;
  254. };
  255. /**
  256. * Instruction which returns from a function.
  257. */
  258. struct ReturnInstruction : public Instruction {
  259. public:
  260. virtual void ProcessInst(Function& func, ValueStack &stack, Engine *engine, CodeGenerator *codeGen) override;
  261. virtual bool isReturn() const;
  262. };
  263. /**
  264. * Default implementation for stack-based instruction performing a unary operation, with a prefixed operator.
  265. */
  266. struct UnaryOpPrefixStackInstruction : public UnaryOpInstruction {
  267. public:
  268. virtual void ProcessInst(Function& func, ValueStack &stack, Engine *engine, CodeGenerator *codeGen) override;
  269. };
  270. /**
  271. * Default implementation for stack-based instruction performing a unary operation, with a postfixed operator.
  272. */
  273. struct UnaryOpPostfixStackInstruction : public UnaryOpInstruction {
  274. public:
  275. virtual void ProcessInst(Function& func, ValueStack &stack, Engine *engine, CodeGenerator *codeGen) override;
  276. };
  277. /**
  278. * Default implementation for stack-based instruction performing a kernel function call.
  279. */
  280. struct KernelCallStackInstruction : public Instruction {
  281. public:
  282. virtual void ProcessInst(Function& func, ValueStack &stack, Engine *engine, CodeGenerator *codeGen) override;
  283. };
  284. /**
  285. * Type representing a vector of InstPtrs.
  286. */
  287. typedef std::vector<InstPtr> InstVec;
  288. /**
  289. * Type representing an iterator over InstPtrs.
  290. */
  291. typedef InstVec::iterator InstIterator;
  292. /**
  293. * Type representing a const_iterator over InstPtrs.
  294. */
  295. typedef InstVec::const_iterator ConstInstIterator;
  296. #endif