Engine.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "Function.h"
  20. #include "Graph.h"
  21. #include "CodeGenerator.h"
  22. #include "Disassembler.h"
  23. /**
  24. * Type representing a map of functions, indexed by starting address.
  25. */
  26. typedef std::map<uint32, Function> FuncMap;
  27. /**
  28. * Base class for engines.
  29. */
  30. class Engine {
  31. public:
  32. virtual ~Engine() = default;
  33. virtual std::unique_ptr<Disassembler> GetDisassembler(
  34. InstVec& insts, const std::vector<unsigned char>& c
  35. );
  36. /**
  37. * Retrieve the disassembler for the engine.
  38. *
  39. * @param[out] insts 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[in] insts The list of instructions.
  47. * @param[out] output Stream to output the code to.
  48. * @return Pointer to a CodeGenerator for the engine.
  49. */
  50. virtual std::unique_ptr<CodeGenerator> GetCodeGenerator(
  51. const InstVec& insts, std::ostream &output
  52. ) = 0;
  53. /**
  54. * Post-processing step after CFG analysis.
  55. * @param[out] insts Vector to place the Instructions in.
  56. * @param[in] graph Graph generated from the CFG analysis.
  57. */
  58. virtual void PostCFG(InstVec& insts, Graph graph);
  59. /**
  60. * Whether or not code flow analysis is supported for this engine.
  61. *
  62. * @return True if supported, false if not. If false is returned, code
  63. * flow analysis should not take place.
  64. */
  65. virtual bool SupportsCodeFlow() const;
  66. /**
  67. * Whether or not code generation is supported for this engine.
  68. *
  69. * @return True if supported, false if not. If false is returned, code
  70. * generation should not take place.
  71. */
  72. virtual bool SupportsCodeGen() const;
  73. /**
  74. * Retrieves the names of all variants supported for this engine.
  75. *
  76. * If variants are not used by this engine, it will be empty (default
  77. * implementation).
  78. *
  79. * @param[out] variants Vector with the supported variants.
  80. */
  81. virtual void GetVariants(std::vector<std::string>& variants) const;
  82. /**
  83. * Whether or not to use "pure" grouping during code flow analysis.
  84. *
  85. * With pure grouping, code flow analysis only looks at branches when
  86. * merging. This method may be more appropriate for non-stack-based
  87. * engines.
  88. *
  89. * @return True if pure grouping should be used, false if not.
  90. */
  91. virtual bool UsePureGrouping() const;
  92. // TODO: functions must probably be set to private, and have accessors, but...
  93. // too much depends on it being public right now.
  94. // Maybe at some point in the future...
  95. /**
  96. * Map to functions in the current script, indexed by start address.
  97. */
  98. FuncMap functions;
  99. protected:
  100. /**
  101. * Engine variant to use for the script.
  102. */
  103. std::string variant_;
  104. };