Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ir/BBlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
36 changes: 29 additions & 7 deletions src/ir/CFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<BBlock> 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<BBlock>(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<BBlock>(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<BBlock>(className, methodName));
methodRoots.push_back(ptr);
return ptr;
}

Expand All @@ -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;
}
Expand Down
8 changes: 7 additions & 1 deletion src/ir/CFG.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef CFG_HPP
#define CFG_HPP

#include <memory>
#include <vector>

#include "bytecode/BytecodeProgram.hpp"
Expand All @@ -13,11 +14,16 @@ class TypeInfo;
class CFG {
private:
BBlock *currentBlock = nullptr;
std::vector<BBlock *> methodBlocks;
std::vector<std::unique_ptr<BBlock>> allBlocks;
std::vector<BBlock *> methodRoots;
int temporaryIndex = 0;
int blockIndex = 0;
const TypeInfo *type_info_ = nullptr;

[[nodiscard]] BBlock *ownBlock(std::unique_ptr<BBlock> block);
void resetVisitedFlags() const;
void resetGeneratedFlags() const;

public:
std::string getTemporaryName();
std::string getBlockName();
Expand Down