123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- #ifndef ZP_OPTIMIZER_H
- #define ZP_OPTIMIZER_H
- #include "Parser.h"
- #include <map>
- #include <memory>
- #include <cinttypes>
- namespace zp
- {
- // forward declarations
- enum class Instruction : char;
- struct Block;
- struct InstructionBlock;
- struct UnionBlock;
- struct Loop;
- class AstVisitor;
- struct BlockInstruction;
- struct UnionBlockInstruction;
- struct IOInstruction;
- struct SimpleBlockInstruction;
- struct LinearLoopInstruction;
- struct LoopInstruction;
- class Optimizer;
- using Index = int;
- using Cell = uint8_t;
- // forward declaration
- class IRVisitor;
- }
- class zp::AstVisitor
- {
- public:
- std::unique_ptr<BlockInstruction> visit(Block& block);
- virtual std::unique_ptr<BlockInstruction>
- visitLoop(Loop& loop) = 0;
- virtual std::unique_ptr<BlockInstruction>
- visitInstructionBlock(InstructionBlock& ib) = 0;
- virtual std::unique_ptr<BlockInstruction>
- visitIOBlock(IOBlock& io) = 0;
- virtual std::unique_ptr<BlockInstruction>
- visitUnionBlock(UnionBlock& ub) = 0;
- };
- struct zp::BlockInstruction
- {
- virtual ~BlockInstruction(void) = default;
- virtual void accept(IRVisitor& v) = 0;
- };
- struct zp::UnionBlockInstruction :
- public BlockInstruction
- {
- std::vector<std::unique_ptr<BlockInstruction>> content;
- virtual void accept(IRVisitor& v);
- };
- struct zp::IOInstruction :
- public BlockInstruction
- {
- /// true on "," (reads) false on "." (writes)
- bool isRead;
- inline IOInstruction(bool isRead) : isRead{isRead} {}
- virtual void accept(IRVisitor& v);
- };
- struct zp::SimpleBlockInstruction :
- public BlockInstruction
- {
- Index pointerMoved;
- std::map<Index, Cell> added;
- virtual void accept(IRVisitor& v);
- };
- struct zp::LinearLoopInstruction :
- public BlockInstruction
- {
- Cell increment;
- std::map<Index, Cell> factors;
- inline LinearLoopInstruction(void) : increment{ 0 } {}
- virtual void accept(IRVisitor& v);
- };
- struct zp::LoopInstruction :
- public UnionBlockInstruction
- {
- virtual void accept(IRVisitor& v);
- };
- class zp::Optimizer :
- public AstVisitor
- {
- public:
- std::unique_ptr<BlockInstruction> visitLoop(Loop& loop) override;
- std::unique_ptr<BlockInstruction> visitInstructionBlock(InstructionBlock& ib) override;
- std::unique_ptr<BlockInstruction> visitIOBlock(IOBlock& io) override;
- std::unique_ptr<BlockInstruction> visitUnionBlock(UnionBlock& ub) override;
- };
- #endif /* ZP_OPTIMIZER_H */
|