Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frame allocator patch #7

Merged
merged 2 commits into from
Dec 17, 2024
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ add_subdirectory(${TESTS_ROOT_DIR})

add_custom_target(clang-force-format
COMMAND find "${ISA_ROOT_DIR}" "${RUNTIME_CORE_ROOT_DIR}" "${TESTS_ROOT_DIR}"
-name "*.cpp" -exec clang-format -style='{ColumnLimit: 120, IndentWidth: 4}' -i {} +
-name "*.h" -exec clang-format -style='{ColumnLimit: 120, IndentWidth: 4}' -i {} +
)
66 changes: 56 additions & 10 deletions runtime_core/frame.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#pragma once
#include <iostream>
#include "function.h"
#include "isa.h"
#include <iostream>
#include <stack>

class Interpretator;

class Frame {
public:
Frame(const Function *func): _n_regs(func->_n_regs), _regs(new int64_t[func->_n_regs]) {
alexzhelyapov1 marked this conversation as resolved.
Show resolved Hide resolved
SetPtr(func);
}
~Frame() {delete[] _regs;}
alexzhelyapov1 marked this conversation as resolved.
Show resolved Hide resolved
// Frame(const Function *func) : _n_regs(func->_n_regs) { SetPtr(func); }
Frame(const Function *func, int64_t *const regs) : _n_regs(func->_n_regs), _regs(regs) { SetPtr(func); }
~Frame() {}

void DumpRegs() const {
std::cout << "Frame dump (" << _n_regs << "):\n";
Expand All @@ -36,15 +36,61 @@ class Frame {
return _return_value;
}

inline void Dump(Interpretator *i) {
ISA::dispatch_dump[*_ptr](i, _ptr, true);
}
inline void Dump(Interpretator *i) { ISA::dispatch_dump[*_ptr](i, _ptr, true); }

int64_t *const _regs = nullptr; // owner
int64_t *const _regs = nullptr; // owner
const int64_t _n_regs = 0;
int64_t *_bytecode = nullptr;
int64_t *_ptr = nullptr;
int64_t _bytecode_len = 0;
int64_t _return_value = 0;
Frame *previous_frame = nullptr;
};
};

template <uint64_t memorySize = 100'000> class FrameAllocator {
private:
char *_memory;
char *_availablePtr;
std::stack<char *> _framesStart;
uint64_t _framesAllocated = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If think it can be removed


public:
FrameAllocator() {
_memory = new char[memorySize];
if (_memory == nullptr) {
throw std::runtime_error("Can't create allocator.");
}
_availablePtr = _memory;
}

Frame *allocate(Function *function) {
// check availability to allocate
if (_availablePtr + sizeof(Frame) + function->_n_args * sizeof(int64_t) > _memory + memorySize) {
throw std::runtime_error(
"Impossible to allocate frame. Total allocated: " + std::to_string(_framesAllocated) +
". Memory used(bytes): " + std::to_string(_availablePtr - _memory));
}

_framesAllocated++;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove


// save current pointer
_framesStart.push(_availablePtr);

// allocate regs
int64_t *regs = new (_availablePtr) int64_t[function->_n_regs];
_availablePtr += function->_n_regs * sizeof(int64_t);

// creating frame header
Frame *new_frame = new (_availablePtr) Frame(function, regs);
_availablePtr += sizeof(Frame);

return new_frame;
}

void deallocate() {
_availablePtr = _framesStart.top();
_framesStart.pop();
}

~FrameAllocator() { delete[] _memory; }
};
18 changes: 7 additions & 11 deletions runtime_core/function.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
#pragma once
#include <string>
#include <cstdint>
#include <iostream>
#include <string>

class Function {
public:
// TODO: Calloc -> new
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this comment pls

Function(const std::string &name, int64_t n_args, int64_t n_regs, int64_t bytecode_len_in_int64):
_name(name),
_n_args(n_args),
_n_regs(n_regs),
_bytecode_len(bytecode_len_in_int64),
_bytecode(new int64_t[bytecode_len_in_int64]) {}
Function(const std::string &name, int64_t n_args, int64_t n_regs, int64_t bytecode_len_in_int64)
: _name(name), _n_args(n_args), _n_regs(n_regs), _bytecode_len(bytecode_len_in_int64),
_bytecode(new int64_t[bytecode_len_in_int64]) {}

~Function() {
delete[] _bytecode;
}
~Function() { delete[] _bytecode; }

// TODO: dump bytecode
void Dump() {
Expand All @@ -27,7 +23,7 @@ class Function {
// std::cout << "Bytecode: " << std::endl;
}

int64_t *const _bytecode = nullptr; // owner
int64_t *const _bytecode = nullptr; // owner
const int64_t _bytecode_len = 0;
const std::string _name = "";
const int64_t _n_args = 0;
Expand Down
38 changes: 18 additions & 20 deletions runtime_core/interpretator.h
Original file line number Diff line number Diff line change
@@ -1,65 +1,63 @@
#pragma once
#include "frame.h"
#include "isa.h"
#include <cstdint>
#include <cstring>
#include <vector>
#include <iostream>
#include <cstdint>
#include "isa.h"
#include "frame.h"

#include <vector>

class Function;

class Interpretator {
public:
FrameAllocator<> frame_allocator;

Interpretator(Function *start_func) {
if (start_func->_n_args != 0) {
throw std::runtime_error("Start func should be without arguments!");
}
_functions.push_back(start_func);
_current_frame = new Frame(start_func); // start frame
}

~Interpretator() {
delete _current_frame;
// start frame
_current_frame = frame_allocator.allocate(start_func);
}

~Interpretator() { frame_allocator.deallocate(); }

int64_t Run() {
_current_frame->Run(this);
return _return_code;
}


void Dump() {
_current_frame->Dump(this);
}

void Dump() { _current_frame->Dump(this); }

int64_t AppendFunction(Function *func) {
_functions.push_back(func);
return _functions.size() - 1;
}


inline void CallFuncById(int64_t func_id) {
if (func_id < 0 || func_id > _functions.size()) {
throw std::runtime_error("Out of range in _functions.");
}

Function *function = _functions[func_id];
Frame *new_frame = new Frame(function);

// allocate new frame with function regs
Frame *new_frame = frame_allocator.allocate(function);
memcpy(new_frame->_regs, _current_frame->_regs, function->_n_args * sizeof(_current_frame->_regs[0]));
new_frame->previous_frame = _current_frame;
_current_frame = new_frame;

_current_frame = new_frame;
_current_frame->Run(this);

_current_frame = new_frame->previous_frame;

// propagate return value
_current_frame->_return_value = new_frame->_return_value;
delete new_frame;
}

// delete new_frame;
frame_allocator.deallocate();
}

Frame *_current_frame;
std::vector<Function *> _functions;
Expand Down