diff --git a/src/ir/BBlock.hpp b/src/ir/BBlock.hpp index bf15414..b8a21a1 100644 --- a/src/ir/BBlock.hpp +++ b/src/ir/BBlock.hpp @@ -42,9 +42,11 @@ class BBlock { [[nodiscard]] bool isVisited() const { return visited; } void markVisited() { visited = true; }; + void resetVisited() { visited = false; }; [[nodiscard]] bool isGenerated() const { return generated; } void markGenerated() { generated = true; }; + void resetGenerated() { generated = false; }; void generateBytecode(BytecodeMethod &method); }; diff --git a/src/ir/CFG.cpp b/src/ir/CFG.cpp index 09c62be..c183ab2 100644 --- a/src/ir/CFG.cpp +++ b/src/ir/CFG.cpp @@ -51,29 +51,50 @@ std::string CFG::getBlockName() { } void CFG::printGraphviz(std::ostream &os) const { + resetVisitedFlags(); os << "digraph {\n"; os << "graph [splines=ortho]\n"; os << "node [shape=box]\n"; - for (const auto &el : methodBlocks) { + for (auto *el : methodRoots) { el->printBlockGraphviz(os); } os << "}\n"; } -BBlock *CFG::newBlock() { return new BBlock(getBlockName()); } +BBlock *CFG::ownBlock(std::unique_ptr block) { + auto *ptr = block.get(); + allBlocks.push_back(std::move(block)); + return ptr; +} + +void CFG::resetVisitedFlags() const { + for (const auto &block : allBlocks) { + block->resetVisited(); + } +} + +void CFG::resetGeneratedFlags() const { + for (const auto &block : allBlocks) { + block->resetGenerated(); + } +} + +BBlock *CFG::newBlock() { + return ownBlock(std::make_unique(getBlockName())); +} void CFG::addInstruction(Tac *ptr) { currentBlock->addInstruction(ptr); } BBlock *CFG::addMethodBlock() { - auto *ptr = new BBlock(getBlockName()); - methodBlocks.push_back(ptr); + auto *ptr = ownBlock(std::make_unique(getBlockName())); + methodRoots.push_back(ptr); return ptr; } BBlock *CFG::addMethodRootBlock(const std::string &className, const std::string &methodName) { - auto *ptr = new BBlock(className, methodName); - methodBlocks.push_back(ptr); + auto *ptr = ownBlock(std::make_unique(className, methodName)); + methodRoots.push_back(ptr); return ptr; } @@ -85,9 +106,10 @@ const std::string *CFG::typeOf(const Node &node) const { } void CFG::generateBytecode(BytecodeProgram &program, SymbolTable &st) { + resetGeneratedFlags(); BBlock *mainRoot = nullptr; - for (auto *basicBlock : methodBlocks) { + for (auto *basicBlock : methodRoots) { if (mainRoot == nullptr) { mainRoot = basicBlock; } diff --git a/src/ir/CFG.hpp b/src/ir/CFG.hpp index 8d08c68..5827234 100644 --- a/src/ir/CFG.hpp +++ b/src/ir/CFG.hpp @@ -1,6 +1,7 @@ #ifndef CFG_HPP #define CFG_HPP +#include #include #include "bytecode/BytecodeProgram.hpp" @@ -13,11 +14,16 @@ class TypeInfo; class CFG { private: BBlock *currentBlock = nullptr; - std::vector methodBlocks; + std::vector> allBlocks; + std::vector methodRoots; int temporaryIndex = 0; int blockIndex = 0; const TypeInfo *type_info_ = nullptr; + [[nodiscard]] BBlock *ownBlock(std::unique_ptr block); + void resetVisitedFlags() const; + void resetGeneratedFlags() const; + public: std::string getTemporaryName(); std::string getBlockName();