FieldCodeGenerator.h 14 KB

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