FieldControlFlowInstruction.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. InstPtr FieldControlFlowInstruction::Create(){return new FieldControlFlowInstruction();}
  26. void FieldControlFlowInstruction::ProcessInst(
  27. Function& func, ValueStack&, Engine* engine, CodeGenerator *code_gen
  28. ){
  29. FieldEngine& eng = static_cast<FieldEngine&>(*engine);
  30. FunctionMetaData md(func.metadata);
  31. switch (opcode_){
  32. case OPCODES::RET:
  33. // A few notes in RET.
  34. // - Lua requires all functions to end with a return.
  35. // - Lua doesn's like returns if not followed by an end.
  36. // - Don't add a return statement to on_start functions.
  37. // Game 'init' and 'main' code is concatenated in V-Gears. If the
  38. // 'main' return is kept, the code of 'main' is never executed.
  39. // There are safeguards forfunctions without a return in the end,
  40. // so it's OK not to include it here.
  41. // - The engine runs on_update again every time it finishes. In the original game a
  42. // script that returns is over for good, so on_update is marked as finished.
  43. if (func.name == "on_update")
  44. code_gen->AddOutputLine(FieldCodeGenerator::ON_UPDATE_FINISHED_STATEMENT);
  45. if (func.name != "on_start") code_gen->AddOutputLine("do return 0 end");
  46. break;
  47. case OPCODES::REQ: ProcessREQ(code_gen, eng); break;
  48. case OPCODES::REQSW: ProcessREQSW(code_gen, eng); break;
  49. case OPCODES::REQEW: ProcessREQEW(code_gen, eng); break;
  50. case OPCODES::PREQ: code_gen->WriteTodo(md.GetEntityName(), "PREQ"); break;
  51. case OPCODES::PRQSW: code_gen->WriteTodo(md.GetEntityName(), "PRQSW"); break;
  52. case OPCODES::PRQEW: code_gen->WriteTodo(md.GetEntityName(), "PRQEW"); break;
  53. case OPCODES::RETTO: ProcessRETTO(code_gen); break;
  54. case OPCODES::WAIT: ProcessWAIT(code_gen); break;
  55. default:
  56. code_gen->AddOutputLine(FieldCodeGenerator::FormatInstructionNotImplemented(
  57. md.GetEntityName(), address_, opcode_
  58. ));
  59. }
  60. }
  61. void FieldControlFlowInstruction::ProcessREQ(
  62. CodeGenerator* code_gen, const FieldEngine& engine
  63. ){
  64. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  65. const auto& entity = engine.EntityByIndex(params_[0]->GetSigned());
  66. const auto& script_name = cg->GetFormatter().GetFriendlyFunctionName(
  67. entity.GetName(), entity.FunctionByIndex(params_[2]->GetUnsigned())
  68. );
  69. auto priority = params_[1]->GetUnsigned();
  70. code_gen->AddOutputLine((
  71. boost::format("script:request(Script.ENTITY, \"%1%\", \"%2%\", %3%)")
  72. % entity.GetName() % script_name % priority
  73. ).str());
  74. }
  75. void FieldControlFlowInstruction::ProcessREQSW(
  76. CodeGenerator* code_gen, const FieldEngine& engine
  77. ){
  78. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  79. const auto& entity = engine.EntityByIndex(params_[0]->GetSigned());
  80. const auto& script_name = cg->GetFormatter().GetFriendlyFunctionName(
  81. entity.GetName(), entity.FunctionByIndex(params_[2]->GetUnsigned())
  82. );
  83. auto priority = params_[1]->GetUnsigned();
  84. code_gen->AddOutputLine((
  85. boost::format("script:request_start_sync(Script.ENTITY, \"%1%\", \"%2%\", %3%)")
  86. % entity.GetName() % script_name % priority
  87. ).str());
  88. }
  89. void FieldControlFlowInstruction::ProcessREQEW(
  90. CodeGenerator* code_gen, const FieldEngine& engine
  91. ){
  92. try{
  93. FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
  94. const auto& entity = engine.EntityByIndex(params_[0]->GetSigned());
  95. const auto& script_name = cg->GetFormatter().GetFriendlyFunctionName(
  96. entity.GetName(), entity.FunctionByIndex(params_[2]->GetUnsigned())
  97. );
  98. auto priority = params_[1]->GetUnsigned();
  99. code_gen->AddOutputLine((
  100. boost::format("script:request_end_sync(Script.ENTITY, \"%1%\", \"%2%\", %3%)")
  101. % entity.GetName() % script_name % priority
  102. ).str());
  103. }
  104. catch (const DecompilerException&){
  105. code_gen->AddOutputLine((
  106. boost::format("-- ERROR call to non existing function index %1%")
  107. % params_[2]->GetUnsigned()
  108. ).str());
  109. }
  110. }
  111. void FieldControlFlowInstruction::ProcessRETTO(CodeGenerator* code_gen){
  112. auto entity_index = params_[0]->GetUnsigned();
  113. auto priority = params_[1]->GetUnsigned();
  114. code_gen->AddOutputLine((
  115. boost::format("-- return_to(script_id_in_current_entity=%2%, priority=%1%)")
  116. % entity_index % priority
  117. ).str());
  118. }
  119. void FieldControlFlowInstruction::ProcessWAIT(CodeGenerator* code_gen){
  120. code_gen->AddOutputLine((
  121. boost::format("script:wait(%1%)") % (params_[0]->GetUnsigned() / 30.0f)
  122. ).str());
  123. }