Optimizer.h 2.4 KB

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