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: 1 addition & 1 deletion src/ast/ArrayAccessNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include "ir/Tac.hpp"

Operand ArrayAccessNode::generateIR(CFG &graph, SymbolTable &st) {
auto arrayName = array->generateIR(graph, st);
auto indexName = index->generateIR(graph, st);
auto arrayName = array->value;
auto name = graph.getTemporaryName();
st.addIntegerVariable(name);
graph.addInstruction(new ArrayAccessTac(name, arrayName, indexName));
Expand Down
2 changes: 1 addition & 1 deletion src/ast/ArrayLengthNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Operand ArrayLengthNode::generateIR(CFG &graph, SymbolTable &st) {
auto name = graph.getTemporaryName();
st.addIntegerVariable(name);
auto arrayName = array->value;
auto arrayName = array->generateIR(graph, st);
graph.addInstruction(new ArrayLengthTac(name, arrayName));
return name;
}
70 changes: 63 additions & 7 deletions src/ast/BooleanExpressionNode.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,76 @@
#include "ast/BooleanExpressionNode.hpp"
#include "ir/BooleanTac.hpp"
#include "ir/Tac.hpp"

Operand AndNode::generateIR(CFG &graph, SymbolTable &st) {
auto lhs_name = left->generateIR(graph, st);
auto rhs_name = right->generateIR(graph, st);
auto lhsName = left->generateIR(graph, st);
auto name = graph.getTemporaryName();
st.addBooleanVariable(name);
graph.addInstruction(new AndTac(name, lhs_name, rhs_name));

auto *rhsEvalBlock = graph.newBlock();
auto *trueBlock = graph.newBlock();
auto *falseBlock = graph.newBlock();
auto *joinBlock = graph.newBlock();

auto *entryBlock = graph.getCurrentBlock();
graph.addInstruction(new CondJumpTac(falseBlock->getName(), lhsName));
graph.addInstruction(new JumpTac(rhsEvalBlock->getName()));
entryBlock->setTrueBlock(rhsEvalBlock);
entryBlock->setFalseBlock(falseBlock);

graph.setCurrentBlock(rhsEvalBlock);
auto rhsName = right->generateIR(graph, st);
graph.addInstruction(new CondJumpTac(falseBlock->getName(), rhsName));
graph.addInstruction(new JumpTac(trueBlock->getName()));
rhsEvalBlock->setTrueBlock(trueBlock);
rhsEvalBlock->setFalseBlock(falseBlock);

graph.setCurrentBlock(trueBlock);
graph.addInstruction(new CopyTac(1, name));
graph.addInstruction(new JumpTac(joinBlock->getName()));
trueBlock->setTrueBlock(joinBlock);

graph.setCurrentBlock(falseBlock);
graph.addInstruction(new CopyTac(0, name));
graph.addInstruction(new JumpTac(joinBlock->getName()));
falseBlock->setTrueBlock(joinBlock);

graph.setCurrentBlock(joinBlock);
return name;
}

Operand OrNode::generateIR(CFG &graph, SymbolTable &st) {
auto lhs_name = left->generateIR(graph, st);
auto rhs_name = right->generateIR(graph, st);
auto lhsName = left->generateIR(graph, st);
auto name = graph.getTemporaryName();
st.addBooleanVariable(name);
graph.addInstruction(new OrTac(name, lhs_name, rhs_name));

auto *rhsEvalBlock = graph.newBlock();
auto *trueBlock = graph.newBlock();
auto *falseBlock = graph.newBlock();
auto *joinBlock = graph.newBlock();

auto *entryBlock = graph.getCurrentBlock();
graph.addInstruction(new CondJumpTac(rhsEvalBlock->getName(), lhsName));
graph.addInstruction(new JumpTac(trueBlock->getName()));
entryBlock->setTrueBlock(trueBlock);
entryBlock->setFalseBlock(rhsEvalBlock);

graph.setCurrentBlock(rhsEvalBlock);
auto rhsName = right->generateIR(graph, st);
graph.addInstruction(new CondJumpTac(falseBlock->getName(), rhsName));
graph.addInstruction(new JumpTac(trueBlock->getName()));
rhsEvalBlock->setTrueBlock(trueBlock);
rhsEvalBlock->setFalseBlock(falseBlock);

graph.setCurrentBlock(trueBlock);
graph.addInstruction(new CopyTac(1, name));
graph.addInstruction(new JumpTac(joinBlock->getName()));
trueBlock->setTrueBlock(joinBlock);

graph.setCurrentBlock(falseBlock);
graph.addInstruction(new CopyTac(0, name));
graph.addInstruction(new JumpTac(joinBlock->getName()));
falseBlock->setTrueBlock(joinBlock);

graph.setCurrentBlock(joinBlock);
return name;
}
8 changes: 2 additions & 6 deletions src/ast/ControlStatementNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ Operand IfNode::generateIR(CFG &graph, SymbolTable &st) {
currentBlock->setTrueBlock(trueBlock);
currentBlock->setFalseBlock(joinBlock);

trueBlock->setTrueBlock(joinBlock);

graph.setCurrentBlock(trueBlock);
stmt->generateIR(graph, st);
graph.addInstruction(new JumpTac(joinLabel));
Expand All @@ -43,16 +41,15 @@ Operand IfElseNode::generateIR(CFG &graph, SymbolTable &st) {
currentBlock->setTrueBlock(trueBlock);
currentBlock->setFalseBlock(falseBlock);

trueBlock->setTrueBlock(joinBlock);
falseBlock->setTrueBlock(joinBlock);

graph.setCurrentBlock(trueBlock);
stmt->generateIR(graph, st);
graph.addInstruction(new JumpTac(joinLabel));
graph.getCurrentBlock()->setTrueBlock(joinBlock);

graph.setCurrentBlock(falseBlock);
elseStmt->generateIR(graph, st);
graph.addInstruction(new JumpTac(joinLabel));
graph.getCurrentBlock()->setTrueBlock(joinBlock);

graph.setCurrentBlock(joinBlock);

Expand All @@ -76,7 +73,6 @@ Operand WhileNode::generateIR(CFG &graph, SymbolTable &st) {
graph.addInstruction(new JumpTac(bodyBlock->getName()));

graph.setCurrentBlock(bodyBlock);
currentBlock->setTrueBlock(headerBlock);
stmt->generateIR(graph, st);
graph.getCurrentBlock()->setTrueBlock(headerBlock);
graph.addInstruction(new JumpTac(headerLabel));
Expand Down
3 changes: 2 additions & 1 deletion src/ast/MethodCallNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Operand MethodCallNode::generateIR(CFG &graph, SymbolTable &st) {
}

const auto &methodType = method->getType();
object->generateIR(graph, st);

for (const auto &arg : exprList->children) {
const auto &argName = arg->generateIR(graph, st);
Expand All @@ -33,7 +34,7 @@ Operand MethodCallNode::generateIR(CFG &graph, SymbolTable &st) {
st.addVariable(methodType, name);

const auto &methodName = id->value;
const auto argCount = std::to_string(exprList->children.size() + 1);
const auto argCount = std::to_string(exprList->children.size());
graph.addInstruction(
new MethodCallTac(name, methodName, *caller_type, argCount));
return name;
Expand Down
3 changes: 2 additions & 1 deletion src/ast/MethodCallWithoutArgumentsNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ Operand MethodCallWithoutArgumentsNode::generateIR(CFG &graph,
}

const auto &methodType = method->getType();
object->generateIR(graph, st);

const auto &name = graph.getTemporaryName();
st.addVariable(methodType, name);
const auto &methodName = id->value;
graph.addInstruction(
new MethodCallTac(name, methodName, *caller_type, "1"));
new MethodCallTac(name, methodName, *caller_type, "0"));
return name;
}
46 changes: 42 additions & 4 deletions src/ir/CFG.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>

#include "ir/CFG.hpp"
#include "semantic/TypeCheckVisitor.hpp"

namespace {
void appendStopToLeafBlocks(BBlock *root, BytecodeMethod &method) {
std::vector<BBlock *> stack{root};
std::unordered_set<BBlock *> visited;

while (!stack.empty()) {
auto *block = stack.back();
stack.pop_back();

if (!visited.insert(block).second) {
continue;
}

const auto hasTrueBlock = block->hasTrueBlock();
const auto hasFalseBlock = block->hasFalseBlock();
if (!hasTrueBlock && !hasFalseBlock) {
method.getBytecodeMethodBlock(block->getName()).stop();
continue;
}

if (hasTrueBlock) {
stack.push_back(block->getTrueBlock());
}
if (hasFalseBlock) {
stack.push_back(block->getFalseBlock());
}
}
}
} // namespace

std::string CFG::getTemporaryName() {
auto name = "_t" + std::to_string(temporaryIndex);
temporaryIndex++;
Expand Down Expand Up @@ -53,7 +84,13 @@ const std::string *CFG::typeOf(const Node &node) const {
}

void CFG::generateBytecode(BytecodeProgram &program, SymbolTable &st) {
BBlock *mainRoot = nullptr;

for (auto *basicBlock : methodBlocks) {
if (mainRoot == nullptr) {
mainRoot = basicBlock;
}

const auto &className = basicBlock->getClassName();
const auto &methodName = basicBlock->getMethodName();

Expand All @@ -76,8 +113,9 @@ void CFG::generateBytecode(BytecodeProgram &program, SymbolTable &st) {

basicBlock->generateBytecode(bytecodeMethod);
}
const auto mainName = methodBlocks.front()->getName();
auto &mainMethod = program.getBytecodeMethod(mainName);
auto &mainBlock = mainMethod.getBytecodeMethodBlock(mainName);
mainBlock.stop();

if (mainRoot != nullptr) {
auto &mainMethod = program.getBytecodeMethod(mainRoot->getName());
appendStopToLeafBlocks(mainRoot, mainMethod);
}
}
2 changes: 1 addition & 1 deletion src/ir/Tac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void CondJumpTac::print(std::ostream &os) const {
os << "iffalse " << lhs << " goto " << rhs << "\n";
}
void CondJumpTac::generateBytecode(BytecodeMethodBlock &block) {
block.push(lhs).cjump(rhs);
block.push(lhsOp).cjump(rhs);
}

void MethodCallTac::print(std::ostream &os) const {
Expand Down