Engine.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <set>
  17. #include <string>
  18. #include <vector>
  19. #include "CodeGenerator.h"
  20. #include "Disassembler.h"
  21. #include "Function.h"
  22. /**
  23. * Type representing a map of functions, indexed by starting address.
  24. */
  25. typedef std::map<uint32, Function> FuncMap;
  26. /**
  27. * Base class for engines.
  28. */
  29. class Engine {
  30. public:
  31. virtual ~Engine() = default;
  32. virtual std::unique_ptr<Disassembler> GetDisassembler(InstVec &, const std::vector<unsigned char>& )
  33. {
  34. throw NotImplementedException();
  35. }
  36. /**
  37. * Retrieve the disassembler for the engine.
  38. *
  39. * @param insts Reference to the std::vector to place the Instructions in.
  40. * @return Pointer to a Disassembler for the engine.
  41. */
  42. virtual std::unique_ptr<Disassembler> GetDisassembler(InstVec &insts) = 0;
  43. /**
  44. * Retrieve the code generator for the engine.
  45. *
  46. * @param output The std::ostream to output the code to.
  47. * @return Pointer to a CodeGenerator for the engine.
  48. */
  49. virtual std::unique_ptr<CodeGenerator> GetCodeGenerator(const InstVec& insts, std::ostream &output) = 0;
  50. /**
  51. * Post-processing step after CFG analysis.
  52. * @param insts Reference to the std::vector to place the Instructions in.
  53. * @param g Graph generated from the CFG analysis.
  54. */
  55. virtual void PostCFG(InstVec&, Graph) { }
  56. /**
  57. * Whether or not code flow analysis is supported for this engine.
  58. *
  59. * @return True if supported, false if not. If false is returned, code flow analysis should not take place, and -D should be implied.
  60. */
  61. virtual bool SupportsCodeFlow() const { return true; }
  62. /**
  63. * Whether or not code generation is supported for this engine.
  64. *
  65. * @return True if supported, false if not. If false is returned, code generation should not take place, and -G should be implied.
  66. */
  67. virtual bool SupportsCodeGen() const { return true; }
  68. /**
  69. * Fill a vector with the names of all variants supported for this engine.
  70. * If variants are not used by this engine, leave the vector empty (default implementation).
  71. *
  72. * @param variants Vector to add the supported variants to.
  73. */
  74. virtual void GetVariants(std::vector<std::string>&) const { };
  75. /**
  76. * Whether or not to use "pure" grouping during code flow analysis.
  77. * With pure grouping, code flow analysis only looks at branches when merging.
  78. * This method may be more appropriate for non-stack-based engines.
  79. *
  80. * @return True if pure grouping should be used, false if not.
  81. */
  82. virtual bool UsePureGrouping() const { return false; }
  83. virtual FuncMap GetFunctions() const{return _functions;}
  84. virtual void SetFunction(uint32 index, Function function){
  85. _functions[index] = function;
  86. }
  87. FuncMap _functions;
  88. protected:
  89. //FuncMap _functions; ///< Map to functions in the current script, indexed by starting address.
  90. std::string _variant; ///< Engine variant to use for the script.
  91. };