Instruction.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <string>
  17. #include <vector>
  18. #include <boost/format.hpp>
  19. #include <boost/intrusive_ptr.hpp>
  20. #include "../RefCounted.h"
  21. #include "../Value.h"
  22. #include "common/scummsys.h"
  23. #include "decompiler/DecompilerException.h"
  24. class CodeGenerator;
  25. class Engine;
  26. /**
  27. * Changes whether or not to output the stack effect for an instruction.
  28. *
  29. * @param[in] value True to output the effect, false not to.
  30. */
  31. void SetOutputStackEffect(bool value);
  32. class Instruction;
  33. /**
  34. * Pointer to an Instruction.
  35. */
  36. typedef boost::intrusive_ptr<Instruction> InstPtr;
  37. class Function;
  38. /**
  39. * Structure for representing an instruction.
  40. */
  41. class Instruction : public RefCounted {
  42. public:
  43. /**
  44. * Binary operation (e.g. +, &&, etc.), including comparisons.
  45. */
  46. static int INST_TYPE_BINARY_OP;
  47. /**
  48. * Boolean negation.
  49. */
  50. static int INST_TYPE_BOOL_NEGATE;
  51. /**
  52. * Regular function call.
  53. */
  54. static int INST_TYPE_CALL;
  55. /**
  56. * Conditional jump.
  57. */
  58. static int INST_TYPE_COND_JUMP;
  59. /**
  60. * Instruction duplicates the most recent stack entry.
  61. */
  62. static int INST_TYPE_DUP;
  63. /**
  64. * Unconditional jump.
  65. */
  66. static int INST_TYPE_JUMP;
  67. /**
  68. * Kernel functions.
  69. */
  70. static int INST_TYPE_KERNEL_CALL;
  71. /**
  72. * Load value from memory.
  73. */
  74. static int INST_TYPE_LOAD;
  75. /**
  76. * Return from regular function call.
  77. */
  78. static int INST_TYPE_RETURN;
  79. /**
  80. * Stack allocation or deallocation (altering stack pointer).
  81. */
  82. static int INST_TYPE_STACK;
  83. /**
  84. * Store value in memory.
  85. */
  86. static int INST_TYPE_STORE;
  87. /**
  88. * Unary operation (e.g. !) with operator placed before the operator.
  89. */
  90. static int INST_TYPE_UNARY_OP_PRE;
  91. /**
  92. * Unary operation with operator placed after the operator.
  93. */
  94. static int INST_TYPE_UNARY_OP_POST;
  95. /**
  96. * Operator overload to output an Instruction to a stream.
  97. *
  98. * @param[out] output The stream to output to.
  99. * @param[in] inst The Instruction to output.
  100. * @return The stream used for output.
  101. */
  102. friend std::ostream& operator<<(std::ostream &output, const Instruction *inst) {
  103. return inst->Print(output);
  104. }
  105. /**
  106. * Print the instruction to a stream.
  107. *
  108. * @param[out] output The stream to write to.
  109. * @return The stream used for output.
  110. */
  111. virtual std::ostream& Print(std::ostream &output) const;
  112. /**
  113. * Checks if the instruction is a jump of some sort.
  114. *
  115. * @return True if the instruction is a jump, otherwise false.
  116. */
  117. virtual bool IsJump() const;
  118. /**
  119. * Checks if the instruction is a conditional jump.
  120. *
  121. * @return True if the instruction is a conditional jump, false
  122. * otherwise.
  123. */
  124. virtual bool IsCondJump() const;
  125. /**
  126. * Checks if the instruction is an unconditional jump.
  127. *
  128. * @return True if the instruction is an unconditional jump, false
  129. * otherwise.
  130. */
  131. virtual bool IsUncondJump() const;
  132. /**
  133. * Checks if the instruction is a stack operation.
  134. *
  135. * @return True if the instruction is a stack operation, false
  136. * otherwise.
  137. */
  138. virtual bool IsStackOp() const;
  139. /**
  140. * Checks if the instruction is a call to a script function.
  141. *
  142. * @return True if the instruction is a script function call, false
  143. * otherwise.
  144. */
  145. virtual bool IsFuncCall() const;
  146. /**
  147. * Checks if the instruction is a return statement.
  148. *
  149. * @return True if the instruction is a return statement, false
  150. * otherwise.
  151. */
  152. virtual bool IsReturn() const;
  153. /**
  154. * Checks if the instruction is a call to a kernel function.
  155. *
  156. * @return True if the instruction is a kernel function call, false
  157. * otherwise.
  158. */
  159. virtual bool IsKernelCall() const;
  160. /**
  161. * Checks if the instruction is a load operation.
  162. *
  163. * @return True if the instruction is a load operation, false
  164. * otherwise.
  165. */
  166. virtual bool IsLoad() const;
  167. /**
  168. * Checks if the instruction is a store operation.
  169. *
  170. * @return True if the instruction is a store operation, false
  171. * otherwise.
  172. */
  173. virtual bool IsStore() const;
  174. /**
  175. * Returns the destination address of a jump instruction.
  176. *
  177. * @return Destination address of a jump instruction.
  178. * @throws WrongTypeException if instruction is not a jump.
  179. */
  180. virtual uint32 GetDestAddress() const;
  181. /**
  182. * Process an instruction for code generation.
  183. *
  184. * @param[in] function The function the instruction is in.
  185. * @param[in] stack The current stack.
  186. * @param[in] engine The Engine used for code generation.
  187. * @param[in] code_gen The CodeGenerator used for code generation.
  188. */
  189. virtual void ProcessInst(
  190. Function& function, ValueStack& stack, Engine* engine, CodeGenerator* code_gen
  191. ) = 0;
  192. /**
  193. * Retrieves the instruction opcode.
  194. *
  195. * @return The opcode.
  196. */
  197. uint32 GetOpcode() const;
  198. /**
  199. * Sets the instruction opcode.
  200. *
  201. * @param[in] opcode The opcode.
  202. */
  203. void SetOpcode(uint32 opcode);
  204. /**
  205. * Retrieves the instruction address.
  206. *
  207. * @return The address.
  208. */
  209. uint32 GetAddress() const;
  210. /**
  211. * Sets the instruction address.
  212. *
  213. * @param[in] address The address.
  214. */
  215. void SetAddress(uint32 address);
  216. /**
  217. * Retrieves the instruction name (the opcode name).
  218. *
  219. * @return The name.
  220. */
  221. std::string GetName() const;
  222. void SetName(std::string name);
  223. /**
  224. * Checks how much the instruction changes the stack pointer.
  225. *
  226. * @return The stack pointer change, in bytes.
  227. */
  228. int16 GetStackChange() const;
  229. /**
  230. * Defines how much the instruction changes the stack pointer.
  231. *
  232. * @param[in] stack_change Bytes the instruction changes the stack
  233. * pointer by.
  234. */
  235. void SetStackChange(int16 stack_change);
  236. /**
  237. * Retrieves the list of instruction parameters.
  238. *
  239. * @return The list of parameters.
  240. */
  241. std::vector<ValuePtr> GetParams() const;
  242. /**
  243. * Retrieves a instruction parameter.
  244. *
  245. * @param[in] index Index of the parameter to retrieve.
  246. * @return The parameter at the specified index, or nullptr if there
  247. * is not that many parameters.
  248. */
  249. ValuePtr GetParam(uint32 index) const;
  250. /**
  251. * Sets the instructions parameters.
  252. *
  253. * @param[in] params The list of instruction parameters.
  254. */
  255. void SetParams(std::vector<ValuePtr> params);
  256. /**
  257. * Adds a parameter to the instructions.
  258. *
  259. * @param[in] value The parameter to add.
  260. */
  261. void AddParam(ValuePtr value);
  262. /**
  263. * Retrieves metadata for code generation.
  264. *
  265. * See the extended documentation for details.
  266. *
  267. * @return Code generator metadata.
  268. */
  269. std::string GetCodeGenData() const;
  270. /**
  271. * Sets metadata for code generation.
  272. *
  273. * See the extended documentation for details.
  274. *
  275. * @param[in] code_gen_data Code generator metadata.
  276. */
  277. void SetCodeGenData(std::string code_gen_data);
  278. /**
  279. * Checks if the instruction requires a label.
  280. */
  281. bool LabelRequired() const;
  282. /**
  283. * Indicates if the instruction needs a label.
  284. */
  285. void SetLabelRequired(bool required);
  286. protected:
  287. /**
  288. * The instruction opcode.
  289. */
  290. uint32 opcode_;
  291. /**
  292. * The instruction address.
  293. */
  294. uint32 address_;
  295. /**
  296. * The instruction name (opcode name).
  297. */
  298. std::string name_;
  299. /**
  300. * How much this instruction changes the stack pointer by.
  301. */
  302. int16 stack_change_;
  303. /**
  304. * Array of parameters used for the instruction.
  305. */
  306. std::vector<ValuePtr> params_;
  307. /**
  308. * String containing metadata for code generation.
  309. *
  310. * See the extended documentation for details.
  311. */
  312. std::string code_gen_data_;
  313. /**
  314. * Indicates if a label is required.
  315. */
  316. bool label_required_ = false;
  317. };
  318. /**
  319. * Type representing a vector of InstPtrs.
  320. */
  321. typedef std::vector<InstPtr> InstVec;
  322. /**
  323. * Type representing an iterator over InstPtrs.
  324. */
  325. typedef InstVec::iterator InstIterator;
  326. /**
  327. * Type representing a const_iterator over InstPtrs.
  328. */
  329. typedef InstVec::const_iterator ConstInstIterator;