-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: Add support for tail call optimization
- Loading branch information
Showing
5 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include "Optimizer.h" | ||
#include "bytecode/instructions/Jump.h" | ||
#include "bytecode/instructions/Return.h" | ||
#include "bytecode/instructions/StoreLocal.h" | ||
|
||
bool Bytecode::Optimizer::is_tail_recursive(const Bytecode::Segment &segment, size_t id) { | ||
const auto &instructions = segment.instructions; | ||
return instructions.back()->type == Instruction::Return && | ||
instructions[instructions.size() - 2]->type == Instruction::Call && | ||
instructions[instructions.size() - 2]->params.ri_params.reg == id; | ||
} | ||
|
||
void Bytecode::Optimizer::optimize_tail_calls(Segment &segment) { | ||
auto &instructions = segment.instructions; | ||
const size_t number_of_args = instructions[instructions.size() - 2] | ||
->params.ri_params.intermediate.asLambda(); | ||
const size_t old_number_of_instructions = instructions.size() - 1; | ||
instructions.pop_back();// Remove the Return instruction | ||
instructions.pop_back();// Remove the Call instruction | ||
for (size_t i = number_of_args - 1; i != -1; i--) | ||
instructions.push_back(new Bytecode::StoreLocal(i)); | ||
instructions.push_back(new Bytecode::Jump(0)); | ||
instructions.push_back(new Bytecode::Return()); | ||
|
||
// Update jump locations | ||
for (auto &instruction: instructions) { | ||
if ((instruction->type == Instruction::Jump || | ||
instruction->type == Instruction::CondJumpIfNot) && | ||
instruction->params.r_param.reg == old_number_of_instructions) { | ||
instruction->params.r_param.reg = instructions.size() - 1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#pragma once | ||
|
||
#include "Segment.h" | ||
namespace Bytecode::Optimizer { | ||
bool is_tail_recursive(const Segment &segment, size_t id); | ||
void optimize_tail_calls(Segment &segment); | ||
}// namespace Bytecode::Optimizer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#pragma once | ||
|
||
#include "Instruction.h" | ||
#include <string> | ||
|
||
namespace Bytecode { | ||
class StoreLocal final : public Instruction { | ||
public: | ||
explicit StoreLocal(const size_t reg) { | ||
params.r_param = {reg}; | ||
type = InstructionType::StoreLocal; | ||
} | ||
void execute(Bytecode::VM &vm) override { | ||
vm.call_stack.setLocal(params.r_param.reg, vm.program_stack.pop()); | ||
} | ||
[[nodiscard]] std::string toString() const override { | ||
return "StoreLocal $r" + std::to_string(params.r_param.reg); | ||
} | ||
}; | ||
}// namespace Bytecode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters