Instruction.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 value[in] 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 output[out] The stream to output to.
  99. * @param inst[in] 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 output[out] 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 stack[in] The current stack.
  185. * @param engine[in] The Engine used for code generation.
  186. * @param code_gen[in] The CodeGenerator used for code generation.
  187. */
  188. virtual void ProcessInst(
  189. Function& function, ValueStack& stack, Engine* engine, CodeGenerator* code_gen
  190. ) = 0;
  191. /**
  192. * Retrieves the instruction opcode.
  193. *
  194. * @return The opcode.
  195. */
  196. uint32 GetOpcode() const;
  197. /**
  198. * Sets the instruction opcode.
  199. *
  200. * @param opcode[in] The opcode.
  201. */
  202. void SetOpcode(uint32 opcode);
  203. /**
  204. * Retrieves the instruction address.
  205. *
  206. * @return The address.
  207. */
  208. uint32 GetAddress() const;
  209. /**
  210. * Sets the instruction address.
  211. *
  212. * @param address[in] The address.
  213. */
  214. void SetAddress(uint32 address);
  215. /**
  216. * Retrieves the instruction name (the opcode name).
  217. *
  218. * @return The name.
  219. */
  220. std::string GetName() const;
  221. void SetName(std::string name);
  222. /**
  223. * Checks how much the instruction changes the stack pointer.
  224. *
  225. * @return The stack pointer change, in bytes.
  226. */
  227. int16 GetStackChange() const;
  228. /**
  229. * Defines how much the instruction changes the stack pointer.
  230. *
  231. * @param stack_change[in] Bytes the instruction changes the stack
  232. * pointer by.
  233. */
  234. void SetStackChange(int16 stack_change);
  235. /**
  236. * Retrieves the list of instruction parameters.
  237. *
  238. * @return The list of parameters.
  239. */
  240. std::vector<ValuePtr> GetParams() const;
  241. /**
  242. * Retrieves a instruction parameter.
  243. *
  244. * @param index[in] Index of the parameter to retrieve.
  245. * @return The parameter at the specified index, or nullptr if there
  246. * is not that many parameters.
  247. */
  248. ValuePtr GetParam(uint32 index) const;
  249. /**
  250. * Sets the instructions parameters.
  251. *
  252. * @param params[in] The list of instruction parameters.
  253. */
  254. void SetParams(std::vector<ValuePtr> params);
  255. /**
  256. * Adds a parameter to the instructions.
  257. *
  258. * @param value[in] The parameter to add.
  259. */
  260. void AddParam(ValuePtr value);
  261. /**
  262. * Retrieves metadata for code generation.
  263. *
  264. * See the extended documentation for details.
  265. *
  266. * @return Code generator metadata.
  267. */
  268. std::string GetCodeGenData() const;
  269. /**
  270. * Sets metadata for code generation.
  271. *
  272. * See the extended documentation for details.
  273. *
  274. * @params code_gen_data[in] Code generator metadata.
  275. */
  276. void SetCodeGenData(std::string code_gen_data);
  277. /**
  278. * Checks if the instruction requires a label.
  279. */
  280. bool LabelRequired() const;
  281. /**
  282. * Indicates if the instruction needs a label.
  283. */
  284. void SetLabelRequired(bool required);
  285. protected:
  286. /**
  287. * The instruction opcode.
  288. */
  289. uint32 opcode_;
  290. /**
  291. * The instruction address.
  292. */
  293. uint32 address_;
  294. /**
  295. * The instruction name (opcode name).
  296. */
  297. std::string name_;
  298. /**
  299. * How much this instruction changes the stack pointer by.
  300. */
  301. int16 stack_change_;
  302. /**
  303. * Array of parameters used for the instruction.
  304. */
  305. std::vector<ValuePtr> params_;
  306. /**
  307. * String containing metadata for code generation.
  308. *
  309. * See the extended documentation for details.
  310. */
  311. std::string code_gen_data_;
  312. /**
  313. * Indicates if a label is required.
  314. */
  315. bool label_required_ = false;
  316. };
  317. /**
  318. * Type representing a vector of InstPtrs.
  319. */
  320. typedef std::vector<InstPtr> InstVec;
  321. /**
  322. * Type representing an iterator over InstPtrs.
  323. */
  324. typedef InstVec::iterator InstIterator;
  325. /**
  326. * Type representing a const_iterator over InstPtrs.
  327. */
  328. typedef InstVec::const_iterator ConstInstIterator;