Engine.h 3.7 KB

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