Engine.h 3.6 KB

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