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
34 changes: 0 additions & 34 deletions src/ast/ArithmeticExpressionNode.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,2 @@
#include "ast/ArithmeticExpressionNode.hpp"

#include "ir/ArithmeticTac.hpp"

Operand PlusNode::generateIR(CFG &graph, SymbolTable &st) {
auto lhs_name = left->generateIR(graph, st);
auto rhs_name = right->generateIR(graph, st);
auto name = graph.getTemporaryName();
st.addIntegerVariable(name);
graph.addInstruction(new AddTac(name, lhs_name, rhs_name));
return name;
}
Operand MinusNode::generateIR(CFG &graph, SymbolTable &st) {
auto lhs_name = left->generateIR(graph, st);
auto rhs_name = right->generateIR(graph, st);
auto name = graph.getTemporaryName();
st.addIntegerVariable(name);
graph.addInstruction(new SubtractTac(name, lhs_name, rhs_name));
return name;
}
Operand MultiplicationNode::generateIR(CFG &graph, SymbolTable &st) {
auto lhs_name = left->generateIR(graph, st);
auto rhs_name = right->generateIR(graph, st);
auto name = graph.getTemporaryName();
st.addIntegerVariable(name);
graph.addInstruction(new MultiplyTac(name, lhs_name, rhs_name));
return name;
}
Operand DivisionNode::generateIR(CFG &graph, SymbolTable &st) {
auto lhs_name = left->generateIR(graph, st);
auto rhs_name = right->generateIR(graph, st);
auto name = graph.getTemporaryName();
st.addIntegerVariable(name);
graph.addInstruction(new DivideTac(name, lhs_name, rhs_name));
return name;
}
4 changes: 0 additions & 4 deletions src/ast/ArithmeticExpressionNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class PlusNode : public ArithmeticExpressionNode {
PlusNode(std::unique_ptr<Node> left, std::unique_ptr<Node> right, int l)
: ArithmeticExpressionNode("Plus", std::move(left), std::move(right),
l) {}
Operand generateIR(CFG &graph, SymbolTable &st) override;
};

class MinusNode : public ArithmeticExpressionNode {
Expand All @@ -31,7 +30,6 @@ class MinusNode : public ArithmeticExpressionNode {
MinusNode(std::unique_ptr<Node> left, std::unique_ptr<Node> right, int l)
: ArithmeticExpressionNode("Minus", std::move(left), std::move(right),
l) {}
Operand generateIR(CFG &graph, SymbolTable &st) override;
};

class MultiplicationNode : public ArithmeticExpressionNode {
Expand All @@ -41,7 +39,6 @@ class MultiplicationNode : public ArithmeticExpressionNode {
int l)
: ArithmeticExpressionNode("Multiplication", std::move(left),
std::move(right), l) {}
Operand generateIR(CFG &graph, SymbolTable &st) override;
};

class DivisionNode : public ArithmeticExpressionNode {
Expand All @@ -50,7 +47,6 @@ class DivisionNode : public ArithmeticExpressionNode {
DivisionNode(std::unique_ptr<Node> left, std::unique_ptr<Node> right, int l)
: ArithmeticExpressionNode("Division", std::move(left),
std::move(right), l) {}
Operand generateIR(CFG &graph, SymbolTable &st) override;
};

#endif
9 changes: 0 additions & 9 deletions src/ast/ArrayAccessNode.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
#include "ast/ArrayAccessNode.hpp"
#include "ir/Tac.hpp"

Operand ArrayAccessNode::generateIR(CFG &graph, SymbolTable &st) {
auto arrayName = array->generateIR(graph, st);
auto indexName = index->generateIR(graph, st);
auto name = graph.getTemporaryName();
st.addIntegerVariable(name);
graph.addInstruction(new ArrayAccessTac(name, arrayName, indexName));
return name;
}
1 change: 0 additions & 1 deletion src/ast/ArrayAccessNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class ArrayAccessNode : public Node {
array = append_child(std::move(array_));
index = append_child(std::move(index_));
}
Operand generateIR(CFG &graph, SymbolTable &st) override;
};

#endif // ARRAYACCESSNODE_HPP
8 changes: 0 additions & 8 deletions src/ast/ArrayLengthNode.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
#include "ast/ArrayLengthNode.hpp"
#include "ir/Tac.hpp"

Operand ArrayLengthNode::generateIR(CFG &graph, SymbolTable &st) {
auto name = graph.getTemporaryName();
st.addIntegerVariable(name);
auto arrayName = array->generateIR(graph, st);
graph.addInstruction(new ArrayLengthTac(name, arrayName));
return name;
}
1 change: 0 additions & 1 deletion src/ast/ArrayLengthNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class ArrayLengthNode : public Node {
ArrayLengthNode(std::unique_ptr<Node> array_, int l)
: Node("Array length", l), array(std::move(array_)) {}
[[nodiscard]] const Node &getArrayNode() const { return *array; }
Operand generateIR(CFG &graph, SymbolTable &st) override;
};

#endif // ARRAYLENGTHNODE_HPP
74 changes: 0 additions & 74 deletions src/ast/BooleanExpressionNode.cpp
Original file line number Diff line number Diff line change
@@ -1,76 +1,2 @@
#include "ast/BooleanExpressionNode.hpp"
#include "ir/Tac.hpp"

Operand AndNode::generateIR(CFG &graph, SymbolTable &st) {
auto lhsName = left->generateIR(graph, st);
auto name = graph.getTemporaryName();
st.addBooleanVariable(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 lhsName = left->generateIR(graph, st);
auto name = graph.getTemporaryName();
st.addBooleanVariable(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;
}
2 changes: 0 additions & 2 deletions src/ast/BooleanExpressionNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ class AndNode : public BooleanExpressionNode {
public:
AndNode(std::unique_ptr<Node> left, std::unique_ptr<Node> right, int l)
: BooleanExpressionNode("AND", std::move(left), std::move(right), l) {}
Operand generateIR(CFG &graph, SymbolTable &st) override;
};

class OrNode : public BooleanExpressionNode {
public:
OrNode(std::unique_ptr<Node> left, std::unique_ptr<Node> right, int l)
: BooleanExpressionNode("OR", std::move(left), std::move(right), l) {}
Operand generateIR(CFG &graph, SymbolTable &st) override;
};

#endif
4 changes: 1 addition & 3 deletions src/ast/BooleanNode.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
#include "ast/BooleanNode.hpp"
#include "ir/Tac.hpp"
Operand TrueNode::generateIR(CFG &graph, SymbolTable &st) { return true; }
Operand FalseNode::generateIR(CFG &graph, SymbolTable &st) { return false; }

2 changes: 0 additions & 2 deletions src/ast/BooleanNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
class TrueNode : public Node {
public:
TrueNode(int l) : Node("TRUE", l) {};
Operand generateIR(CFG &graph, SymbolTable &st) override;
};
class FalseNode : public Node {
public:
FalseNode(int l) : Node("FALSE", l) {};
Operand generateIR(CFG &graph, SymbolTable &st) override;
};

#endif // BOOLEANNODE_HPP
7 changes: 0 additions & 7 deletions src/ast/ClassAllocationNode.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
#include "ast/ClassAllocationNode.hpp"
#include "ir/Tac.hpp"

Operand ClassAllocationNode::generateIR(CFG &graph, SymbolTable &st) {
auto name = graph.getTemporaryName();
st.addVariable(id, name);
graph.addInstruction(new NewTac(name, id));
return name;
}
1 change: 0 additions & 1 deletion src/ast/ClassAllocationNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class ClassAllocationNode : public Node {
public:
ClassAllocationNode(std::unique_ptr<Node> object, int l)
: Node("Class allocation", object->value, l), id{object->value} {}
Operand generateIR(CFG &graph, SymbolTable &st) override;
};

#endif // CLASSALLOCATIONNODE_HPP
8 changes: 0 additions & 8 deletions src/ast/ClassNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,3 @@
#include "ast/AstVisitor.hpp"

void ClassNode::accept(AstVisitor &visitor) const { visitor.visit(*this); }

Operand ClassNode::generateIR(CFG &graph, SymbolTable &st) {
auto *currentClass = st.lookupClass(className);
st.enterClassScope(currentClass);
body->generateIR(graph, st);
st.exitScope();
return "";
}
2 changes: 0 additions & 2 deletions src/ast/ClassNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class ClassNode : public Node {

[[nodiscard]] const std::string &getClassName() const { return className; }
[[nodiscard]] const Node &getBodyNode() const { return *body; }

Operand generateIR(CFG &graph, SymbolTable &st) override;
};

#endif
85 changes: 0 additions & 85 deletions src/ast/ControlStatementNode.cpp
Original file line number Diff line number Diff line change
@@ -1,87 +1,2 @@
#include "ast/ControlStatementNode.hpp"
#include "ir/Tac.hpp"

Operand IfNode::generateIR(CFG &graph, SymbolTable &st) {
auto *trueBlock = graph.newBlock();
auto *joinBlock = graph.newBlock();
const auto &joinLabel = joinBlock->getName();
const auto &trueLabel = trueBlock->getName();

const auto &condName = cond->generateIR(graph, st);
graph.addInstruction(new CondJumpTac(joinLabel, condName));
graph.addInstruction(new JumpTac(trueLabel));

auto *currentBlock = graph.getCurrentBlock();
currentBlock->setTrueBlock(trueBlock);
currentBlock->setFalseBlock(joinBlock);

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

graph.setCurrentBlock(joinBlock);

return "placeholder ifnode::generateir";
}

Operand IfElseNode::generateIR(CFG &graph, SymbolTable &st) {
auto *trueBlock = graph.newBlock();
auto *falseBlock = graph.newBlock();
auto *joinBlock = graph.newBlock();

const auto &falseLabel = falseBlock->getName();
const auto &joinLabel = joinBlock->getName();

const auto &condName = cond->generateIR(graph, st);
graph.addInstruction(new CondJumpTac(falseLabel, condName));
graph.addInstruction(new JumpTac(trueBlock->getName()));

auto *currentBlock = graph.getCurrentBlock();
currentBlock->setTrueBlock(trueBlock);
currentBlock->setFalseBlock(falseBlock);

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);

return "placeholder ifelsenode::generateir";
}

Operand WhileNode::generateIR(CFG &graph, SymbolTable &st) {
auto *headerBlock = graph.newBlock();
const auto &headerLabel = headerBlock->getName();
auto *bodyBlock = graph.newBlock();
bodyBlock->setTrueBlock(headerBlock);
auto *joinBlock = graph.newBlock();

auto *currentBlock = graph.getCurrentBlock();
currentBlock->setTrueBlock(headerBlock);
graph.addInstruction(new JumpTac(headerLabel));

graph.setCurrentBlock(headerBlock);
const auto &condName = cond->generateIR(graph, st);
auto *conditionBlock = graph.getCurrentBlock();
graph.addInstruction(new CondJumpTac(joinBlock->getName(), condName));
graph.addInstruction(new JumpTac(bodyBlock->getName()));

graph.setCurrentBlock(bodyBlock);
stmt->generateIR(graph, st);
graph.getCurrentBlock()->setTrueBlock(headerBlock);
graph.addInstruction(new JumpTac(headerLabel));

conditionBlock->setTrueBlock(bodyBlock);
conditionBlock->setFalseBlock(joinBlock);

graph.setCurrentBlock(joinBlock);

return "placeholder whilenode::generateir";
}
3 changes: 0 additions & 3 deletions src/ast/ControlStatementNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class IfNode : public ControlStatementNode {
++it;
stmt = it->get();
}
Operand generateIR(CFG &graph, SymbolTable &st) override;
};
class IfElseNode : public ControlStatementNode {
Node *cond, *stmt, *elseStmt;
Expand All @@ -41,7 +40,6 @@ class IfElseNode : public ControlStatementNode {
stmt = it->get();
elseStmt = append_child(std::move(elseStmt_));
}
Operand generateIR(CFG &graph, SymbolTable &st) override;
};
class WhileNode : public ControlStatementNode {
Node *cond, *stmt;
Expand All @@ -54,7 +52,6 @@ class WhileNode : public ControlStatementNode {
++it;
stmt = it->get();
}
Operand generateIR(CFG &graph, SymbolTable &st) override;
};

#endif
3 changes: 0 additions & 3 deletions src/ast/IdentifierNode.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
#include "ast/IdentifierNode.hpp"

Operand IdentifierNode::generateIR(CFG &graph, SymbolTable &st) {
return value;
}
2 changes: 0 additions & 2 deletions src/ast/IdentifierNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class IdentifierNode : public Node {
public:
IdentifierNode(const std::string &value_, int l)
: Node("Identifier", value_, l), value{value_} {}

Operand generateIR(CFG &graph, SymbolTable &st) override;
};

#endif
8 changes: 0 additions & 8 deletions src/ast/IntegerArrayAllocationNode.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
#include "ast/IntegerArrayAllocationNode.hpp"
#include "ir/Tac.hpp"

Operand IntegerArrayAllocationNode::generateIR(CFG &graph, SymbolTable &st) {
auto name = graph.getTemporaryName();
st.addVariable("int[]", name);
auto lengthName = length->generateIR(graph, st);
graph.addInstruction(new NewArrayTac(name, lengthName));
return name;
}
Loading