Skip to content

Commit

Permalink
VM: add JumpIfLessOrEquals and JumpIfGreaterOrEquals instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Jul 5, 2024
1 parent cf04da0 commit 85b0b76
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
2 changes: 2 additions & 0 deletions include/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ struct Instruction {
Return,
Call,
JumpIfFalse,
JumpIfLessOrEqualsI32,
JumpIfGreaterOrEqualsI32,
Jump,
} type{};
union {
Expand Down
37 changes: 32 additions & 5 deletions src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,18 +388,45 @@ bool IfStatement::operator==(const AbstractSyntaxTree &other) const {
elseBody.has_value() == otherIfStatement.elseBody.has_value();
}
void IfStatement::compile(Program &program, Segment &segment) const {
condition->compile(program, segment);
size_t jumpIndex = segment.instructions.size();
segment.instructions.push_back(
Instruction{.type = Instruction::InstructionType::JumpIfFalse});
size_t jumpIndex;
bool unOptimized = false;
if (condition->nodeType == AbstractSyntaxTree::Type::BinaryExpression &&
dynamic_cast<BinaryExpression *>(condition)->op.type == Less) {
auto binaryExpression = dynamic_cast<BinaryExpression *>(condition);
binaryExpression->left->compile(program, segment);
binaryExpression->right->compile(program, segment);
jumpIndex = segment.instructions.size();
segment.instructions.push_back(
Instruction{.type = Instruction::InstructionType::JumpIfGreaterOrEqualsI32});
} else if (condition->nodeType == AbstractSyntaxTree::Type::BinaryExpression &&
dynamic_cast<BinaryExpression *>(condition)->op.type == Greater) {
auto binaryExpression = dynamic_cast<BinaryExpression *>(condition);
binaryExpression->left->compile(program, segment);
binaryExpression->right->compile(program, segment);
jumpIndex = segment.instructions.size();
segment.instructions.push_back(
Instruction{.type = Instruction::InstructionType::JumpIfLessOrEqualsI32});
} else if (condition->nodeType == AbstractSyntaxTree::Type::BinaryExpression &&
dynamic_cast<BinaryExpression *>(condition)->op.type == Equal) {
auto binaryExpression = dynamic_cast<BinaryExpression *>(condition);
binaryExpression->left->compile(program, segment);
binaryExpression->right->compile(program, segment);
jumpIndex = segment.instructions.size();
segment.instructions.push_back(
Instruction{.type = Instruction::InstructionType::JumpIfFalse});
} else {
condition->compile(program, segment);
jumpIndex = segment.instructions.size();
unOptimized = true;
}
thenBody->compile(program, segment);
segment.instructions[jumpIndex].params.index = segment.instructions.size() + elseBody.has_value();
if (elseBody.has_value()) {
jumpIndex = segment.instructions.size();
segment.instructions.push_back(
Instruction{.type = Instruction::InstructionType::Jump});
elseBody.value()->compile(program, segment);
segment.instructions[jumpIndex].params.index = segment.instructions.size() + 1;
segment.instructions[jumpIndex].params.index = segment.instructions.size() + unOptimized;
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,30 @@ void VM::run(const Program &program) {
}
free(val);
} break;
case Instruction::InstructionType::JumpIfLessOrEqualsI32: {
auto b = static_cast<int32_t *>(popStack(sizeof(int32_t)));
auto a = static_cast<int32_t *>(popStack(sizeof(int32_t)));
if (*a <= *b) {
callStack.back().currentInstruction = instruction.params.index;
free(a);
free(b);
continue;
}
free(a);
free(b);
} break;
case Instruction::InstructionType::JumpIfGreaterOrEqualsI32: {
auto b = static_cast<int32_t *>(popStack(sizeof(int32_t)));
auto a = static_cast<int32_t *>(popStack(sizeof(int32_t)));
if (*a >= *b) {
callStack.back().currentInstruction = instruction.params.index;
free(a);
free(b);
continue;
}
free(a);
free(b);
} break;
case Instruction::InstructionType::Jump:
callStack.back().currentInstruction = instruction.params.index;
continue;
Expand Down

0 comments on commit 85b0b76

Please sign in to comment.