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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ if(BUILD_TESTING)
${CMAKE_CURRENT_SOURCE_DIR}/tests/lexer_exact_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/parser_exact_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/parser_syntax_error_files_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/ir_constant_folding_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/symbol_table_test.cpp
)
target_link_libraries(minijava_tests PRIVATE GTest::gtest_main minijava_core)
Expand Down
2 changes: 1 addition & 1 deletion src/bytecode/BytecodeInstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void StackParameterInstruction::serialize(Serializer &serializer) const {
};
void IntegerParameterInstruction::serialize(Serializer &serializer) const {
serializer.writeOpcode(opcode);
serializer.writeInteger(param);
serializer.writeSignedInteger(param);
};
void StringParameterInstruction::serialize(Serializer &serializer) const {
serializer.writeOpcode(opcode);
Expand Down
10 changes: 5 additions & 5 deletions src/bytecode/BytecodeInstruction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ class BytecodeInstruction {

public:
BytecodeInstruction(Opcode opcode_)
: opcode(opcode_),
mnemonic(mnemonics.at(
static_cast<size_t>(static_cast<std::uint8_t>(opcode_)))) {};
: opcode(opcode_), mnemonic(mnemonics.at(static_cast<size_t>(
static_cast<std::uint8_t>(opcode_)))) {};
virtual ~BytecodeInstruction() = default;
virtual void print(std::ostream &os) const = 0;

Expand All @@ -39,13 +38,14 @@ class StackParameterInstruction : public BytecodeInstruction {
class IntegerParameterInstruction : public BytecodeInstruction {
// An instruction which takes one integer parameter
// and pushes one result back to the stack
size_t param;
std::int64_t param;

public:
IntegerParameterInstruction(Opcode opcode_, size_t param_)
IntegerParameterInstruction(Opcode opcode_, std::int64_t param_)
: BytecodeInstruction(opcode_), param(param_) {};
void print(std::ostream &os) const override;
void serialize(Serializer &serializer) const override;
[[nodiscard]] std::int64_t getParam() const { return param; }
};

class StringParameterInstruction : public BytecodeInstruction {
Expand Down
1 change: 1 addition & 0 deletions src/bytecode/BytecodeMethod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class BytecodeMethod {

void print(std::ostream &os) const;

[[nodiscard]] const auto &getBlocks() const { return blocks; }
[[nodiscard]] const auto &getVariables() const { return variables; }
[[nodiscard]] const auto &getFieldVariables() const {
return fieldVariables;
Expand Down
3 changes: 1 addition & 2 deletions src/bytecode/BytecodeMethodBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ void BytecodeMethodBlock::addBytecodeInstruction(BytecodeInstruction *instr) {
BytecodeMethodBlock &BytecodeMethodBlock::push(const Operand &operand) {
if (const auto *ptr = std::get_if<int>(&operand)) {
addBytecodeInstruction(
new IntegerParameterInstruction(Opcode::CONST,
static_cast<size_t>(*ptr)));
new IntegerParameterInstruction(Opcode::CONST, *ptr));
} else if (const auto *ptr = std::get_if<std::string>(&operand)) {
addBytecodeInstruction(
new StringParameterInstruction(Opcode::LOAD, *ptr));
Expand Down
1 change: 1 addition & 0 deletions src/bytecode/BytecodeMethodBlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class BytecodeMethodBlock {

BytecodeMethodBlock(const std::string &name_) : name(name_) {};
[[nodiscard]] const std::string &getName() const { return name; }
[[nodiscard]] const auto &getInstructions() const { return instructions; }
void print(std::ostream &os) const;
void addBytecodeInstruction(BytecodeInstruction *instr);

Expand Down
13 changes: 13 additions & 0 deletions src/bytecode/BytecodeProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ BytecodeMethod &BytecodeProgram::getBytecodeMethod(const std::string &name) {
return *it;
}

std::vector<const BytecodeInstruction *>
BytecodeProgram::getInstructions() const {
std::vector<const BytecodeInstruction *> instructions;
for (const auto &method : methods) {
for (const auto &block : method.getBlocks()) {
for (const auto &instruction : block.getInstructions()) {
instructions.push_back(instruction.get());
}
}
}
return instructions;
}

void BytecodeProgram::print(std::ostream &os) const {
for (const auto &method : methods) {
method.print(os);
Expand Down
2 changes: 2 additions & 0 deletions src/bytecode/BytecodeProgram.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class BytecodeProgram {
std::vector<std::string> fieldVariables);

[[nodiscard]] BytecodeMethod &getBytecodeMethod(const std::string &name);
[[nodiscard]] std::vector<const BytecodeInstruction *>
getInstructions() const;

void print(std::ostream &os) const;

Expand Down
2 changes: 2 additions & 0 deletions src/ir/BBlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class BBlock {
BBlock *getFalseBlock() { return falseExit; }

void addInstruction(Tac *ptr);
[[nodiscard]] const auto &getInstructions() const { return instructions; }
[[nodiscard]] auto &getInstructions() { return instructions; }

void printBlockGraphviz(std::ostream &os);

Expand Down
1 change: 1 addition & 0 deletions src/ir/CFG.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class CFG {
[[nodiscard]] BBlock *addMethodBlock();
[[nodiscard]] BBlock *addMethodRootBlock(const std::string &className,
const std::string &methodName);
[[nodiscard]] const auto &getMethodRoots() const { return methodRoots; }

void setTypeInfo(const TypeInfo *info) { type_info_ = info; }
[[nodiscard]] const std::string *typeOf(const Node &node) const;
Expand Down
15 changes: 15 additions & 0 deletions src/ir/Tac.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ class Tac {
virtual void generateBytecode([[maybe_unused]] BytecodeMethodBlock &block) {
};

[[nodiscard]] const std::string &getResult() const { return result; }
[[nodiscard]] const Operand &getLhsOperand() const { return lhsOp; }
[[nodiscard]] const Operand &getRhsOperand() const { return rhsOp; }
[[nodiscard]] const std::string &getOperator() const { return op; }

void setResult(const std::string &value) { result = value; }
void setLhsOperand(const Operand &value) {
lhsOp = value;
lhs = to_string(lhsOp);
}
void setRhsOperand(const Operand &value) {
rhsOp = value;
rhs = to_string(rhsOp);
}

Tac(const std::string &result_) : result{result_} {}
Tac(const std::string &result_, const Operand &lhs_, const std::string &op_,
const Operand &rhs_)
Expand Down
Loading