Skip to content

Commit

Permalink
perf: Add DivideRI and DivideRR instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Apr 24, 2024
1 parent 0b0bc06 commit f509e02
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/bytecode/builtin_functions/DivideFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,30 @@ namespace Bytecode::BuiltinFunctions {
instructions.push_back(new (Instruction){Instruction::Divide});
return;
}
if (args[0]->type == SyntaxTreeNode::TokenNode &&
args[1]->type == SyntaxTreeNode::TokenNode &&
segment->find_variable(((TokenNode *) args[0])->getName()) != -1) {
if (((TokenNode *) args[0])->token.type == Token::Symbol &&
((TokenNode *) args[1])->token.type == Token::Number) {
instructions.push_back(new (Instruction){
Instruction::DivideRI,
{.ri_params = {
segment->find_variable(((TokenNode *) args[0])->getName()),
StackObject(((TokenNode *) args[1])->token)}},
});
return;
}
if (((TokenNode *) args[0])->token.type == Token::Symbol &&
((TokenNode *) args[1])->token.type == Token::Symbol) {
instructions.push_back(new (Instruction){
Instruction::DivideRR,
{.rr_params = {
segment->find_variable(((TokenNode *) args[0])->getName()),
segment->find_variable(((TokenNode *) args[1])->getName())}},
});
return;
}
}
args[0]->compile(segment, program, instructions);
args[1]->compile(segment, program, instructions);
instructions.push_back(new (Instruction){Instruction::Divide});
Expand Down
2 changes: 2 additions & 0 deletions src/bytecode/compiler/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace Bytecode {
MultiplyRI,
MultiplyRR,
Divide,
DivideRI,
DivideRR,
Increment,
Decrement,
DecrementR,
Expand Down
23 changes: 23 additions & 0 deletions src/bytecode/vm/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,29 @@ void Bytecode::Interpreter::execute(const Program &program) {
}
vm.program_stack.push(object1.data.number / object2.data.number);
} break;
case Instruction::DivideRI: {
const auto object2 = currentInstruction->params.ri_params.intermediate;
const auto object1 = vm.call_stack.getLocal(currentInstruction->params.ri_params.reg);
if (object1.type != ObjectType::Number) {
throw SyntaxError("Invalid argument type for function \"/\", Expected number, got string");
}
if (object2.data.number == 0) {
throw SyntaxError("Division by zero", 0, 0);
}
vm.program_stack.push(object1.data.number / object2.data.number);
} break;
case Instruction::DivideRR: {
const auto object1 = vm.call_stack.getLocal(currentInstruction->params.rr_params.reg1);
const auto object2 = vm.call_stack.getLocal(currentInstruction->params.rr_params.reg2);
if (object1.type != ObjectType::Number ||
object2.type != ObjectType::Number) {
throw SyntaxError("Invalid argument type for function \"/\", Expected number, got string");
}
if (object2.data.number == 0) {
throw SyntaxError("Division by zero", 0, 0);
}
vm.program_stack.push(object1.data.number / object2.data.number);
} break;
case Instruction::Equals: {
const auto object2 = vm.program_stack.pop();
const auto object1 = vm.program_stack.pop();
Expand Down
6 changes: 6 additions & 0 deletions src/utils/dump_bytecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ void print_instruction(const Bytecode::Instruction &instruction) {
case Bytecode::Instruction::Divide:
std::cout << "Divide\n";
break;
case Bytecode::Instruction::DivideRI:
std::cout << "DivideRI $r" << instruction.params.ri_params.reg << " " << instruction.params.ri_params.intermediate.toString() << '\n';
break;
case Bytecode::Instruction::DivideRR:
std::cout << "DivideRR $r" << instruction.params.rr_params.reg1 << " $r" << instruction.params.rr_params.reg2 << '\n';
break;
case Bytecode::Instruction::Increment:
std::cout << "Increment\n";
break;
Expand Down

0 comments on commit f509e02

Please sign in to comment.