FieldCodeGenerator.h 13 KB

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