FieldCodeGenerator.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 "FieldScriptFormatter.h"
  20. #include "decompiler/CodeGenerator.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 character 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. FieldScriptFormatter& 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.GetFriendlyVarName(
  239. bank, value_or_address
  240. );
  241. if (friendly_name.empty())
  242. return (boost::format("FFVII.Banks[%1%][%2%]") % bank % address).str();
  243. return (boost::format("FFVII.Data.%1%") % friendly_name).str();
  244. }
  245. case 5:
  246. case 6:
  247. {
  248. const auto address = static_cast<uint32>(value_or_address)& 0xFF;
  249. const auto friendly_name = formatter.GetFriendlyVarName(bank, address);
  250. if (friendly_name.empty())
  251. return (boost::format("FFVII.Banks[%1%][%2%]") % bank % address).str();
  252. return "FFVII.Data." + friendly_name;
  253. }
  254. default:
  255. return (
  256. boost::format(
  257. "FFVII.Banks[%1%][%2%]"
  258. ) % bank % (static_cast<uint32>(value_or_address) & 0xFF)
  259. ).str();
  260. }
  261. }
  262. /**
  263. * Constructor.
  264. *
  265. * @param engine[in] The engine.
  266. * @param insts[in] The list of instructions to parse.
  267. * @param output[out] The generated script.
  268. * @param formatter[in] The code formatter.
  269. */
  270. FieldCodeGenerator(
  271. Engine *engine, const InstVec& insts, std::ostream &output,
  272. FieldScriptFormatter& formatter
  273. ) :
  274. CodeGenerator(engine, output, FIFO_ARGUMENT_ORDER, LIFO_ARGUMENT_ORDER),
  275. insts_(insts), formatter_(formatter)
  276. {target_lang_ = std::make_unique<LuaLanguage>();}
  277. /**
  278. * Generates the script from the instructions.
  279. *
  280. * @param insts[in] The list of instructions.
  281. * @param graph[in] Code graph, unused.
  282. */
  283. virtual void Generate(InstVec& insts, const Graph &graph) override;
  284. /**
  285. * Adds a line to the script.
  286. *
  287. * @param line[in] The line to add.
  288. * @param unindent_before[in] If true, the current script indentation
  289. * will be moved back one position before the line.
  290. * @param indent_after[in] If true, the current script indentation
  291. * will be moved forward one position after the line.
  292. */
  293. virtual void AddOutputLine(
  294. std::string line, bool unindent_before = false, bool indent_after = false
  295. ) override;
  296. /**
  297. * Retrieves the engine scale factor.
  298. *
  299. * @return The scale factor
  300. */
  301. float GetScaleFactor() const;
  302. /**
  303. * Retrieves the formatter.
  304. */
  305. FieldScriptFormatter& GetFormatter();
  306. protected:
  307. /**
  308. * Constructs the function signature.
  309. *
  310. * The function signature is the LUA function declaration, and it
  311. * looks like this:
  312. *
  313. * <function_name> = function(self)
  314. *
  315. * @param function[in] The function to construct the signature for.
  316. * @return The function signature.
  317. */
  318. virtual std::string ConstructFuncSignature(const Function &function) override;
  319. /**
  320. * Finalizes a function.
  321. *
  322. * It appends an "end" to finalize the function. If the function is
  323. * the last of a class, it also closes the class braces.
  324. *
  325. * @param function[in] The function to end.
  326. */
  327. virtual void OnEndFunction(const Function& function) override;
  328. /**
  329. * Adds lines to the script before a function.
  330. *
  331. * If the function is the first in a class, it initializes the class
  332. * and, if the function belongs to a character, creates the class
  333. * variable for the character. If the function has a comment
  334. * associated, it will also be added before the function.
  335. *
  336. * @param function[in] The function about to start.
  337. */
  338. virtual void OnBeforeStartFunction(const Function& function) override;
  339. /**
  340. * Initializes a function.
  341. *
  342. * It adds a comment with the function opcodes.
  343. *
  344. * @param function[in] The function to start.
  345. */
  346. virtual void OnStartFunction(const Function& function) override;
  347. /**
  348. * Checks if only required labels are to be written.
  349. *
  350. * @return Always true.
  351. */
  352. virtual bool OutputOnlyRequiredLabels() const override;
  353. private:
  354. /**
  355. * The list of instructions in the function to generate.
  356. */
  357. const InstVec& insts_;
  358. /**
  359. * Generated lines.
  360. */
  361. std::vector<CodeLine> lines_;
  362. /**
  363. * The formatter.
  364. */
  365. FieldScriptFormatter& formatter_;
  366. };