WorldSubStackInstruction.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/world/instruction/WorldSubStackInstruction.h"
  22. #include "decompiler/world/WorldEngine.h"
  23. #include "decompiler/world/WorldCodeGenerator.h"
  24. #include "decompiler/world/WorldDisassembler.h"
  25. void WorldSubStackInstruction::ProcessInst(
  26. Function& function, ValueStack &stack, Engine *engine, CodeGenerator *code_gen
  27. ){
  28. // The operands are pushed in order, so the first one is the second from the top.
  29. if (opcode_ == 0x15){
  30. // Negative number.
  31. const std::string operand = stack.Pop()->GetString();
  32. stack.Push(new VarValue("(-" + operand + ")"));
  33. return;
  34. }
  35. if (opcode_ == 0x1b){
  36. // A function of one value that is not known (the emerald weapon uses it on a variable).
  37. const std::string operand = stack.Pop()->GetString();
  38. stack.Push(new VarValue("UnknownFunction1B(" + operand + ")"));
  39. return;
  40. }
  41. if (opcode_ == 0x17){
  42. // Logic not.
  43. const std::string condition = WorldEngine::AsCondition(stack.Pop());
  44. // not not x is x.
  45. if (condition.compare(0, 4, "not ") == 0) stack.Push(new WorldEngine::BoolValue(condition.substr(4)));
  46. else stack.Push(new WorldEngine::BoolValue("not " + condition));
  47. return;
  48. }
  49. const ValuePtr right = stack.Pop();
  50. const ValuePtr left = stack.Pop();
  51. const std::string l = left->GetString();
  52. const std::string r = right->GetString();
  53. switch (opcode_){
  54. case 0x40: stack.Push(new VarValue("(" + l + " + " + r + ")")); break;
  55. case 0x41: stack.Push(new VarValue("(" + l + " - " + r + ")")); break;
  56. case 0x30: stack.Push(new VarValue("(" + l + " * " + r + ")")); break;
  57. // Shift right.
  58. case 0x51: stack.Push(new VarValue("math.floor(" + l + " / 2 ^ " + r + ")")); break;
  59. // Bitwise and, or (see script_stubs.lua).
  60. case 0x80: stack.Push(new VarValue("BitAnd(" + l + ", " + r + ")")); break;
  61. case 0xa0: stack.Push(new VarValue("BitOr(" + l + ", " + r + ")")); break;
  62. case 0x60: stack.Push(new WorldEngine::BoolValue("(" + l + " < " + r + ")")); break;
  63. case 0x61: stack.Push(new WorldEngine::BoolValue("(" + l + " > " + r + ")")); break;
  64. case 0x62: stack.Push(new WorldEngine::BoolValue("(" + l + " <= " + r + ")")); break;
  65. case 0x63: stack.Push(new WorldEngine::BoolValue("(" + l + " >= " + r + ")")); break;
  66. case 0xb0:
  67. stack.Push(new WorldEngine::BoolValue(
  68. "(" + WorldEngine::AsCondition(left) + " and " + WorldEngine::AsCondition(right) + ")"
  69. ));
  70. break;
  71. case 0xc0:
  72. stack.Push(new WorldEngine::BoolValue(
  73. "(" + WorldEngine::AsCondition(left) + " or " + WorldEngine::AsCondition(right) + ")"
  74. ));
  75. break;
  76. default: stack.Push(new VarValue("unknown_operation(" + l + ", " + r + ")")); break;
  77. }
  78. }