Optimizer.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef ZP_OPTIMIZER_H
  2. #define ZP_OPTIMIZER_H
  3. #include "Parser.h"
  4. #include <unordered_map>
  5. #include <memory>
  6. namespace zp
  7. {
  8. // forward declarations
  9. enum class Instruction : char;
  10. struct Block;
  11. struct InstructionBlock;
  12. struct UnionBlock;
  13. struct Loop;
  14. class AstVisitor;
  15. struct BlockInstruction;
  16. struct UnionBlockInstruction;
  17. struct IOInstruction;
  18. struct SimpleBlockInstruction;
  19. struct LinearLoopInstruction;
  20. struct LoopInstruction;
  21. class Optimizer;
  22. using Index = int;
  23. using Cell = unsigned char;
  24. }
  25. class zp::AstVisitor
  26. {
  27. public:
  28. std::unique_ptr<BlockInstruction> visit(Block& block);
  29. virtual std::unique_ptr<BlockInstruction>
  30. visitLoop(Loop& loop) = 0;
  31. virtual std::unique_ptr<BlockInstruction>
  32. visitInstructionBlock(InstructionBlock& ib) = 0;
  33. virtual std::unique_ptr<BlockInstruction>
  34. visitIOBlock(IOBlock& io) = 0;
  35. virtual std::unique_ptr<BlockInstruction>
  36. visitUnionBlock(UnionBlock& ub) = 0;
  37. };
  38. struct zp::BlockInstruction
  39. {
  40. virtual ~BlockInstruction(void) = default;
  41. };
  42. struct zp::UnionBlockInstruction :
  43. public BlockInstruction
  44. {
  45. std::vector<std::unique_ptr<BlockInstruction>> content;
  46. };
  47. struct zp::IOInstruction :
  48. public BlockInstruction
  49. {
  50. /// true on , false on .
  51. bool read;
  52. inline BlockInstruction(bool read) : read{read} {}
  53. };
  54. struct zp::SimpleBlockInstruction :
  55. public BlockInstruction
  56. {
  57. Index pointerMoved;
  58. std::unordered_map<Index, Cell> added;
  59. };
  60. struct zp::LinearLoopInstruction :
  61. public BlockInstruction
  62. {
  63. };
  64. struct zp::LoopInstruction :
  65. public UnionBlockInstruction
  66. {
  67. };
  68. class zp::Optimizer :
  69. public AstVisitor
  70. {
  71. public:
  72. std::unique_ptr<BlockInstruction> visitLoop(Loop& loop) override;
  73. std::unique_ptr<BlockInstruction> visitInstructionBlock(InstructionBlock& ib) override;
  74. std::unique_ptr<BlockInstruction> visitIOBlock(IOBlock& io) override;
  75. std::unique_ptr<BlockInstruction> visitUnionBlock(UnionBlock& ub) override;
  76. };
  77. #endif /* ZP_OPTIMIZER_H */