FieldCodeGenerator.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 "decompiler/decompiler_codegen.h"
  17. #include <boost/algorithm/string.hpp>
  18. #include <deque>
  19. #include <unordered_map>
  20. #include "decompiler/sudm.h"
  21. /**
  22. * Parses an stores function metadata.
  23. *
  24. * Function metadata can indicate the entity and character of a function, and
  25. * if the function is the first or last in a class.
  26. */
  27. class FunctionMetaData{
  28. public:
  29. /**
  30. * Constructor. Parses function metadata.
  31. *
  32. * Meta data format can be:
  33. * - start_end_entityname
  34. * - start_entityname
  35. * - end_entity_name
  36. *
  37. * @param meta_data[in] Metadata to parse.
  38. */
  39. FunctionMetaData(std::string meta_data);
  40. /**
  41. * Checks if the function is the first of a class.
  42. *
  43. * @return True if the metadata is the first of a class, false
  44. * otherwise.
  45. */
  46. bool IsStart() const;
  47. /**
  48. * Checks if the function is the last of a class.
  49. *
  50. * @return True if the metadata is the last of a class, false
  51. * otherwise.
  52. */
  53. bool IsEnd() const;
  54. /**
  55. * Retrieves the entity name.
  56. *
  57. * @return The entity name.
  58. */
  59. std::string GetEntityName();
  60. /**
  61. * Retrieves the character ID.
  62. *
  63. * @return The chaacter ID.
  64. */
  65. int GetCharacterId();
  66. private:
  67. /**
  68. * Delimiter for metadata tokens.
  69. */
  70. static const std::string DELIMITER;
  71. /**
  72. * Start tag for metadata.
  73. */
  74. static const std::string START;
  75. /**
  76. * End tag for metadata.
  77. */
  78. static const std::string END;
  79. /**
  80. * Parses metadata.
  81. *
  82. * @param meta_data[in] Metadata to parse.
  83. */
  84. void Parse(std::string meta_data);
  85. /**
  86. * Parses a metadata item, looking for a start mark.
  87. *
  88. * If it finds it, marks the metadata as the first function of a
  89. * class. It also * calls {@see ParseEnd} with the next token.
  90. *
  91. * @param item[in] The item to check for a start mark.
  92. * @param item[in] All the metadata tokens.
  93. */
  94. void ParseStart(const std::string& item, std::deque<std::string>& strs);
  95. /**
  96. * Parses a metadata item, looking for an end mark.
  97. *
  98. * If it finds it, marks the metadata as the last function of a class.
  99. * It also calls {@see ParseCharId}.
  100. *
  101. * @param item[in] The item to check for a start mark.
  102. * @param item[in] Metadata tokens, first removed.
  103. */
  104. void ParseEnd(const std::string& item, std::deque<std::string>& strs);
  105. /**
  106. * Parses a metadata item, looking for a character ID.
  107. *
  108. * If it finds it, sets the character ID. It also calls
  109. * {@see ParseEntity}.
  110. *
  111. * @param item[in] The item to check for a start mark.
  112. * @param item[in] Metadata tokens, first two removed.
  113. */
  114. void ParseCharId(const std::string& item, std::deque<std::string>& strs);
  115. /**
  116. * Parses a metadata item, looking for an entity name.
  117. *
  118. * It sets the entity name.
  119. *
  120. * @param item[in] The item to check for a start mark.
  121. * @param item[in] Metadata tokens, first three removed.
  122. */
  123. void ParseEntity(const std::string& item, std::deque<std::string>& strs);
  124. /**
  125. * Indicates if the function is the last of a class.
  126. */
  127. bool end_ = false;
  128. /**
  129. * Indicates if the function is the first of a class.
  130. */
  131. bool start_ = false;
  132. /**
  133. * The entity name.
  134. */
  135. std::string entity_name_;
  136. /**
  137. * The character ID.
  138. */
  139. int character_id_ = -1;
  140. };
  141. /**
  142. * The code generator.
  143. *
  144. * Generates code for a field map function.
  145. */
  146. class FieldCodeGenerator : public CodeGenerator{
  147. public:
  148. /**
  149. * Type of data.
  150. */
  151. enum struct ValueType : int{
  152. /**
  153. * Integer.
  154. */
  155. Integer = 0,
  156. /**
  157. * Float.
  158. */
  159. Float = 1
  160. };
  161. /**
  162. * Generates code for an unimplemented instruction.
  163. *
  164. * The code will actually be a comment, indicating where the function
  165. * call must have been.
  166. *
  167. * @param entity[in] The entity name.
  168. * @param address[in] The address of the instruction.
  169. * @param opcode[in] The opcode not implemented.
  170. */
  171. static const std::string FormatInstructionNotImplemented(
  172. const std::string& entity, uint32 address, uint32 opcode
  173. );
  174. /**
  175. * Generates code for an unimplemented instruction.
  176. *
  177. * The code will actually be a comment, indicating where the function
  178. * call must have been.
  179. *
  180. * @param entity[in] The entity name.
  181. * @param address[in] The address of the instruction.
  182. * @param opcode[in] The unimplemented instruction.
  183. */
  184. static const std::string FormatInstructionNotImplemented(
  185. const std::string& entity, uint32 address, const Instruction& instruction
  186. );
  187. /**
  188. * Formats a value as a boolean.
  189. *
  190. * @param value[in] The value to format.
  191. * @return "false" if the value is 0, "true" otherwise.
  192. */
  193. static const std::string FormatBool(uint32 value);
  194. /**
  195. * Formats a value as a boolean.
  196. *
  197. * @param value[in] The value to format.
  198. * @return "true" if the value is 0, "false" otherwise.
  199. */
  200. static const std::string FormatInvertedBool(uint32 value);
  201. /**
  202. * Formats a data access for a map script.
  203. *
  204. * If possible, it will look for friendly names for variables.
  205. *
  206. * @param formatter[in] Formatter to look up variable friendly names.
  207. * @param bank[in] The memory bank to use.
  208. * @param value_or_address[in] The value or memory address to use.
  209. * When bank is 0, it will be considered as a value. When bank is non
  210. * 0, it will be considered an address of the bank.
  211. * @param value_type[in] Data type to use. Used only when getting a
  212. * value, not a bank address.
  213. * @param scale[in] Scale to scale values to. Used only when using
  214. * float values, unused when type is integer or when using a bank
  215. * address.
  216. * @return String with the friendly variable name, value, or bank
  217. * address.
  218. */
  219. template<typename TValue>static const std::string FormatValueOrVariable(
  220. SUDM::IScriptFormatter& formatter, uint32 bank, TValue value_or_address,
  221. ValueType type = ValueType::Integer, float scale = 1.0f
  222. ){
  223. switch (bank){
  224. case 0:
  225. switch (type){
  226. // TODO: check for zero
  227. case ValueType::Float: return std::to_string(value_or_address / scale);
  228. case ValueType::Integer: return std::to_string(value_or_address);
  229. default: return std::to_string(value_or_address);
  230. }
  231. case 1:
  232. case 2:
  233. case 3:
  234. case 13:
  235. case 15:
  236. {
  237. const auto address = static_cast<uint32>(value_or_address) & 0xFF;
  238. const auto friendly_name = formatter.VarName(bank, value_or_address);
  239. if (friendly_name.empty())
  240. return (boost::format("FFVII.Banks[%1%][%2%]") % bank % address).str();
  241. return (boost::format("FFVII.Data.%1%") % friendly_name).str();
  242. }
  243. case 5:
  244. case 6:
  245. {
  246. const auto address = static_cast<uint32>(value_or_address)& 0xFF;
  247. const auto friendly_name = formatter.VarName(bank, address);
  248. if (friendly_name.empty())
  249. return (boost::format("FFVII.Banks[%1%][%2%]") % bank % address).str();
  250. return "FFVII.Data." + friendly_name;
  251. }
  252. default:
  253. return (
  254. boost::format(
  255. "FFVII.Banks[%1%][%2%]"
  256. ) % bank % (static_cast<uint32>(value_or_address) & 0xFF)
  257. ).str();
  258. }
  259. }
  260. /**
  261. * Constructor.
  262. *
  263. * @param engine[in] The engine.
  264. * @param insts[in] The list of instructions to parse.
  265. * @param output[out] The generated script.
  266. * @param formatter[in] The code formatter.
  267. */
  268. FieldCodeGenerator(
  269. Engine *engine, const InstVec& insts, std::ostream &output,
  270. SUDM::IScriptFormatter& formatter
  271. ) :
  272. CodeGenerator(engine, output, FIFO_ARGUMENT_ORDER, LIFO_ARGUMENT_ORDER),
  273. insts_(insts), formatter_(formatter)
  274. {target_lang_ = std::make_unique<LuaTargetLanguage>();}
  275. /**
  276. * Generates the script from the instructions.
  277. *
  278. * @param insts[in] The list of instructions.
  279. * @param graph[in] Code graph, unused.
  280. */
  281. virtual void Generate(InstVec& insts, const Graph &g) override;
  282. /**
  283. * Adds a line to the script.
  284. *
  285. * @param line[in] The line to add.
  286. * @param unindent_before[in] If true, the current script indentation
  287. * will be moved back one position before the line.
  288. * @param indent_after[in] If true, the current script indentation
  289. * will be moved forward one position after the line.
  290. */
  291. virtual void AddOutputLine(
  292. std::string line, bool unindent_before = false, bool indent_after = false
  293. ) override;
  294. /**
  295. * Retrieves the engine scale factor.
  296. *
  297. * @return The scale factor
  298. */
  299. float GetScaleFactor() const;
  300. /**
  301. * Retrieves the formatter.
  302. */
  303. SUDM::IScriptFormatter& GetFormatter();
  304. protected:
  305. /**
  306. * Constructs the function signature.
  307. *
  308. * The function signature is the LUA function declaration, and it
  309. * looks like this:
  310. *
  311. * <function_name> = function(self)
  312. *
  313. * @param function[in] The function to construct the signature for.
  314. * @return The function signature.
  315. */
  316. virtual std::string ConstructFuncSignature(const Function &function) override;
  317. /**
  318. * Finalizes a function.
  319. *
  320. * It appends an "end" to finalize the function. If the function is
  321. * the last of a class, it also closes the class braces.
  322. *
  323. * @param function[in] The function to end.
  324. */
  325. virtual void OnEndFunction(const Function& function) override;
  326. /**
  327. * Adds lines to the script before a function.
  328. *
  329. * If the function is the first in a class, it initializes the class
  330. * and, if the function belongs to a character, creates the class
  331. * variable for the character. If the function has a comment
  332. * associated, it will also be added before the function.
  333. *
  334. * @param function[in] The function about to start.
  335. */
  336. virtual void OnBeforeStartFunction(const Function& function) override;
  337. /**
  338. * Initializes a function.
  339. *
  340. * It adds a comment with the function opcodes.
  341. *
  342. * @param function[in] The function to start.
  343. */
  344. virtual void OnStartFunction(const Function& function) override;
  345. /**
  346. * Checks if only required labels are to be written.
  347. *
  348. * @return Always true.
  349. */
  350. virtual bool OutputOnlyRequiredLabels() const override;
  351. private:
  352. /**
  353. * The list of instructions in the function to generate.
  354. */
  355. const InstVec& insts_;
  356. /**
  357. * Generated lines.
  358. */
  359. std::vector<CodeLine> lines_;
  360. /**
  361. * The formatter.
  362. */
  363. SUDM::IScriptFormatter& formatter_;
  364. };