Index: src/sksl/SkSLCFGGenerator.h |
diff --git a/src/sksl/SkSLCFGGenerator.h b/src/sksl/SkSLCFGGenerator.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..aa21ba634f8902bb7724ed4376b6dd941917b45e |
--- /dev/null |
+++ b/src/sksl/SkSLCFGGenerator.h |
@@ -0,0 +1,84 @@ |
+/* |
+ * Copyright 2016 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#ifndef SKSL_CFGGENERATOR |
+#define SKSL_CFGGENERATOR |
+ |
+#include "ir/SkSLExpression.h" |
+#include "ir/SkSLFunctionDefinition.h" |
+ |
+#include <set> |
+#include <stack> |
+ |
+namespace SkSL { |
+ |
+typedef size_t BlockId; |
dogben
2016/10/13 03:55:43
nit: document this is index in CFG::fBlocks.
|
+ |
+struct BasicBlock { |
+ struct Node { |
+ enum Kind { |
+ kStatement_Kind, |
+ kExpression_Kind |
+ }; |
+ |
+ Kind fKind; |
+ const IRNode* fNode; |
+ }; |
+ |
+ std::vector<Node> fNodes; |
+ std::set<BlockId> fEntrances; |
+ std::set<BlockId> fExits; |
+ // variable definitions upon entering this basic block (null expression = undefined) |
+ std::unordered_map<const Variable*, const Expression*> fBefore; |
+}; |
+ |
+struct CFG { |
+ BlockId fStart; |
+ BlockId fExit; |
+ std::vector<BasicBlock> fBlocks; |
+ |
+ void dump(); |
+ |
+private: |
+ BlockId fCurrent; |
+ |
+ // Adds a new block, adds an exit from the current block to the new block, then marks the new |
+ // block as the current block |
+ BlockId newBlock(); |
+ |
+ // Adds a new block, but does not mark it current or add an exit from the current block |
+ BlockId newIsolatedBlock(); |
+ |
+ // Adds an exit from the from block to the to block |
+ void addExit(BlockId from, BlockId to); |
+ |
+ friend class CFGGenerator; |
+}; |
+ |
+/** |
+ * Converts functions into control flow graphs. |
+ */ |
+class CFGGenerator { |
+public: |
+ CFGGenerator() {} |
+ |
+ CFG getCFG(const FunctionDefinition& f); |
dogben
2016/10/13 03:55:43
nit: mention getCFG does not set BasicBlock::fBefo
|
+ |
+private: |
+ void addStatement(CFG& cfg, const Statement* s); |
+ |
+ void addExpression(CFG& cfg, const Expression* e); |
+ |
+ void addLValue(CFG& cfg, const Expression* e); |
+ |
+ std::stack<BlockId> fLoopContinues; |
+ std::stack<BlockId> fLoopExits; |
+}; |
+ |
+} |
+ |
+#endif |