FieldControlFlowInstruction.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * V-Gears
  3. * Copyright (C) 2022 V-Gears Team
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <iostream>
  19. #include <sstream>
  20. #include <boost/format.hpp>
  21. #include "decompiler/field/instruction/FieldControlFlowInstruction.h"
  22. #include "decompiler/field/FieldEngine.h"
  23. #include "decompiler/field/FieldCodeGenerator.h"
  24. #include "decompiler/field/FieldDisassembler.h"
  25. void FF7::FF7ControlFlowInstruction::ProcessInst(
  26. Function& func, ValueStack&, Engine* engine, CodeGenerator *code_gen
  27. ){
  28. FF7::FieldEngine& eng = static_cast<FF7::FieldEngine&>(*engine);
  29. FunctionMetaData md(func.metadata);
  30. switch (opcode_){
  31. case OPCODES::RET:
  32. // A few notes in RET.
  33. // - Lua requires all functions to end with a return.
  34. // - Lua doesn's like returns if not followed by an end.
  35. // - Don't add a return statement to on_start functions.
  36. // Game 'init' and 'main' code is concatenated in V-Gears. If the
  37. // 'main' return is kept, the code of 'main' is never executed.
  38. // There are safeguards forfunctions without a return in the end,
  39. // so it's OK not to include it here.
  40. if (func.name != "on_start") code_gen->AddOutputLine("do return 0 end");
  41. break;
  42. case OPCODES::REQ: ProcessREQ(code_gen, eng); break;
  43. case OPCODES::REQSW: ProcessREQSW(code_gen, eng); break;
  44. case OPCODES::REQEW: ProcessREQEW(code_gen, eng); break;
  45. case OPCODES::PREQ: code_gen->WriteTodo(md.GetEntityName(), "PREQ"); break;
  46. case OPCODES::PRQSW: code_gen->WriteTodo(md.GetEntityName(), "PRQSW"); break;
  47. case OPCODES::PRQEW: code_gen->WriteTodo(md.GetEntityName(), "PRQEW"); break;
  48. case OPCODES::RETTO: ProcessRETTO(code_gen); break;
  49. case OPCODES::WAIT: ProcessWAIT(code_gen); break;
  50. default:
  51. code_gen->AddOutputLine(FF7::FieldCodeGenerator::FormatInstructionNotImplemented(
  52. md.GetEntityName(), address_, opcode_
  53. ));
  54. }
  55. }
  56. void FF7::FF7ControlFlowInstruction::ProcessREQ(
  57. CodeGenerator* code_gen, const FieldEngine& engine
  58. ){
  59. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  60. const auto& entity = engine.EntityByIndex(params_[0]->getSigned());
  61. const auto& script_name = cg->GetFormatter().FunctionName(
  62. entity.GetName(), entity.FunctionByIndex(params_[2]->getUnsigned())
  63. );
  64. auto priority = params_[1]->getUnsigned();
  65. code_gen->AddOutputLine((
  66. boost::format("script:request(Script.ENTITY, \"%1%\", \"%2%\", %3%)")
  67. % entity.GetName() % script_name % priority
  68. ).str());
  69. }
  70. void FF7::FF7ControlFlowInstruction::ProcessREQSW(
  71. CodeGenerator* code_gen, const FieldEngine& engine
  72. ){
  73. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  74. const auto& entity = engine.EntityByIndex(params_[0]->getSigned());
  75. const auto& script_name = cg->GetFormatter().FunctionName(
  76. entity.GetName(), entity.FunctionByIndex(params_[2]->getUnsigned())
  77. );
  78. auto priority = params_[1]->getUnsigned();
  79. code_gen->AddOutputLine((
  80. boost::format("script:request_start_sync(Script.ENTITY, \"%1%\", \"%2%\", %3%)")
  81. % entity.GetName() % script_name % priority
  82. ).str());
  83. }
  84. void FF7::FF7ControlFlowInstruction::ProcessREQEW(
  85. CodeGenerator* code_gen, const FieldEngine& engine
  86. ){
  87. try{
  88. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  89. const auto& entity = engine.EntityByIndex(params_[0]->getSigned());
  90. const auto& script_name = cg->GetFormatter().FunctionName(
  91. entity.GetName(), entity.FunctionByIndex(params_[2]->getUnsigned())
  92. );
  93. auto priority = params_[1]->getUnsigned();
  94. code_gen->AddOutputLine((
  95. boost::format("script:request_end_sync(Script.ENTITY, \"%1%\", \"%2%\", %3%)")
  96. % entity.GetName() % script_name % priority
  97. ).str());
  98. }
  99. catch (const InternalDecompilerError&){
  100. code_gen->AddOutputLine((
  101. boost::format("-- ERROR call to non existing function index %1%")
  102. % params_[2]->getUnsigned()
  103. ).str());
  104. }
  105. }
  106. void FF7::FF7ControlFlowInstruction::ProcessRETTO(CodeGenerator* code_gen){
  107. auto entity_index = params_[0]->getUnsigned();
  108. auto priority = params_[1]->getUnsigned();
  109. code_gen->AddOutputLine((
  110. boost::format("-- return_to(script_id_in_current_entity=%2%, priority=%1%)")
  111. % entity_index % priority
  112. ).str());
  113. }
  114. void FF7::FF7ControlFlowInstruction::ProcessWAIT(CodeGenerator* code_gen){
  115. code_gen->AddOutputLine((
  116. boost::format("script:wait(%1%)") % (params_[0]->getUnsigned() / 30.0f)
  117. ).str());
  118. }