Skip to content

Commit

Permalink
Add functions (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
levBagryansky authored Nov 9, 2023
1 parent 2b99f8c commit 80f9e84
Show file tree
Hide file tree
Showing 40 changed files with 1,174 additions and 647 deletions.
1 change: 1 addition & 0 deletions ISA.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ There is ChaiVM's accumulator(acc) based ISA.
| Сmpgf | R | f64 -> i64, compare acc with r1. Acc became 1 i64 if greater than r1, 0 if equal, otherwise -1 |
| Cmplf | R | f64 -> i64, compare acc with r1. Acc became 1 i64 if less than r1, 0 if equal, otherwise -1 |
| Goto | I | Goes to another instruction at branchoffset [imm] |
| Call | I | Calls function [imm]. Imm is reference to function in constant pool named constant_func_name_and_type. |

To generate this file use the following python script:
```shell
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ format:
./tools/clang-format.sh $(PWD)/$(BENCH_DIR)
.PHONY: build
build: init
cmake -S $(PWD) -B $(PWD)/$(BUILD_DIR) -DCMAKE_BUILD_TYPE=DEBUG -DCHAIVM_ADD_SANITIZERS=OFF
cmake -S $(PWD) -B $(PWD)/$(BUILD_DIR) -DCHAIVM_ADD_MEM_SANITIZER=OFF -DCHAIVM_ADD_THREAD_SANITIZER=OFF
cmake --build $(PWD)/$(BUILD_DIR) --parallel $(JOBS)

.PHONY: test
Expand Down
1 change: 1 addition & 0 deletions bench/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ add_library(chai_benchif INTERFACE)
target_link_libraries(chai_benchif INTERFACE
chai_include
chai_interpreter
chai_memory
chai_utils
benchmark
)
Expand Down
4 changes: 2 additions & 2 deletions bench/codeman-wrapper.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "ChaiVM/interpreter/executor.hpp"
#include "ChaiVM/utils/chai-file.hpp"
#include "ChaiVM/utils/file-format/chai-file.hpp"
#include "ChaiVM/utils/instr2Raw.hpp"

using namespace chai::interpreter;
Expand Down Expand Up @@ -35,5 +35,5 @@ class CodeManWrapper {

public:
CodeManager manager_;
ChaiFile chaiFile_;
chai::utils::fileformat::ChaiFile chaiFile_;
};
5 changes: 4 additions & 1 deletion bench/dummy_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ static void BM_SomeFunction(benchmark::State &state) {

// This code gets timed
chai::interpreter::CodeManager codeManager{};
chai::interpreter::Executor executor{&codeManager};
chai::memory::LinearBuffer buffer_ =
chai::memory::LinearBuffer(1024 * 256);
;
chai::interpreter::Executor executor{&codeManager, buffer_};
}
}
// Register the function as a benchmark
Expand Down
6 changes: 4 additions & 2 deletions bench/sin_plus_cos_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ static void BM_SinCos(benchmark::State &state) {
// Perform setup here
CodeManWrapper wrapper{};
initSinPlusCos(wrapper);
Executor executor{&wrapper.manager_};
for (auto _ : state) {
chai::memory::LinearBuffer buffer_ =
chai::memory::LinearBuffer(1024 * 256);
;
Executor executor{&wrapper.manager_, buffer_};
executor.run();
executor.restart();
}
}
BENCHMARK(BM_SinCos);
Expand Down
8 changes: 6 additions & 2 deletions bench/square_equation_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ static void BM_SquareEquation(benchmark::State &state) {
// Perform setup here
CodeManWrapper wrapper{};
initSquareEquatino(wrapper);
Executor executor{&wrapper.manager_};

for (auto _ : state) {
chai::memory::LinearBuffer buffer_ =
chai::memory::LinearBuffer(1024 * 256);
;
Executor executor{&wrapper.manager_, buffer_};
executor.run();
executor.restart();
}
}
// Register the function as a benchmark
Expand All @@ -32,6 +35,7 @@ static void initSquareEquatino(CodeManWrapper &codeman) {
const RegisterId r9 = 9;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-variable"
const RegisterId r10 = 10;
#pragma GCC diagnostic pop
const RegisterId r11 = 11;
Expand Down
64 changes: 0 additions & 64 deletions include/ChaiVM/interpreter/code-manager.hpp

This file was deleted.

82 changes: 82 additions & 0 deletions include/ChaiVM/interpreter/code-manager/code-manager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#pragma once

#include <cassert>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <istream>
#include <vector>

#include "ChaiVM/interpreter/code-manager/func-struct.hpp"
#include "ChaiVM/memory/allocator.hpp"
#include "ChaiVM/memory/linear-allocator.hpp"
#include "ChaiVM/memory/linear-buffer.hpp"

namespace chai::interpreter {

/**
* Class to manage bytecode. Similar to ClassLoader.
*/
class CodeManager final {
public:
enum ConstantTag : uint8_t {
CNST_I64 = 1,
CNST_F64,
CNST_FUNC_NAME_AND_TYPE,
CNST_RAW_STR, // Analogue of CONSTANT_Utf8 in jvm
};

/**
* Parses and loads the full file.
* @param path Path to the .chai file.
*/
void load(const std::filesystem::path &path);

/**
* Loads stream of Constant pool.
* @param istream
*/
void loadPool(std::istream &istream);

void loadFunction(std::istream &istream);

chsize_t getCnst(Immidiate id);

bytecode_t getBytecode(size_t func, chsize_t pc);

const Function &getFunc(Immidiate imm) const;

const Function &getStartFunc() const;

private:
std::vector<Function> funcs_;

/**
* Runtime constant pool. Constants(excepting Strings) with id [imm] can be
* retrieved via this vector. If [imm] is String then the string keeps in
* stringPool_[constantPool_[imm]].
*/
std::vector<chsize_t> constantPool_;

/**
* We cannot contain strings in constantPool_ so we keep constant strings
* here.
*/
std::vector<std::string> stringPool_;

/**
* Id in appropriate collection by immidiate.
* For example, func by imm is found as funcs_[dispatch_[imm]].
* @todo #1:90min We can avoid using dispatch_ anywhere via inheritance.
*/
std::vector<Immidiate> dispatch_;
};

class BeyondCodeBoundaries : public std::runtime_error {
public:
BeyondCodeBoundaries(char const *msg);
BeyondCodeBoundaries(const std::string &msg);
const char *what() const noexcept override;
};

} // namespace chai::interpreter
17 changes: 17 additions & 0 deletions include/ChaiVM/interpreter/code-manager/func-struct.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <vector>

#include "ChaiVM/interpreter/instruction.hpp"
#include "ChaiVM/types.hpp"

namespace chai::interpreter {

struct Function {
uint8_t numRegs;
uint8_t numArgs;
Immidiate constFuncRef;
std::vector<bytecode_t> code = {};
};

} // namespace chai::interpreter
30 changes: 22 additions & 8 deletions include/ChaiVM/interpreter/executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,33 @@

#include <bit>

#include "code-manager.hpp"
#include "ChaiVM/interpreter/code-manager/code-manager.hpp"
#include "decoder.hpp"
#include "reg-file.hpp"
#include "frame.hpp"

namespace chai::interpreter {

class Executor {
public:
using Handler = void (Executor::*)(Instruction);

Executor(CodeManager *manager);
Executor(CodeManager *manager, memory::LinearBuffer &buffer);

/**
* Loads the first frame (public static void main).
*/
void init();

void run();
void restart();
const RegisterFile &getState() const &;

chsize_t &acc();
chsize_t acc() const;

Frame const *getCurrentFrame() const;

private:
chsize_t &pc();
chsize_t pc() const;
inline void advancePc();
void inv(Instruction ins);
void nop(Instruction ins);
Expand Down Expand Up @@ -61,6 +72,7 @@ class Executor {
void cmpgf(Instruction ins);
void cmplf(Instruction ins);
void g0t0(Instruction ins);
void call(Instruction ins);

static constexpr Handler HANDLER_ARR[] = {
&Executor::inv, &Executor::nop, &Executor::ret,
Expand All @@ -76,14 +88,16 @@ class Executor {
&Executor::if_icmpeq, &Executor::if_icmpne, &Executor::if_icmpgt,
&Executor::if_icmpge, &Executor::if_icmplt, &Executor::if_icmple,
&Executor::if_acmpeq, &Executor::if_acmpne, &Executor::cmpgf,
&Executor::cmplf, &Executor::g0t0};
&Executor::cmplf, &Executor::g0t0, &Executor::call};

private:
chsize_t acc_;
CodeManager *codeManager_;
RegisterFile regFile_;
memory::LinearBuffer &buffer_;
Frame *currentFrame_ = nullptr;
};

inline void Executor::advancePc() { regFile_.pc() += sizeof(bytecode_t); }
inline void Executor::advancePc() { pc() += sizeof(bytecode_t); }

/**
* @todo #8:30m>/DEV make this exception to take RegisterFile and return it's
Expand Down
37 changes: 37 additions & 0 deletions include/ChaiVM/interpreter/frame.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include "ChaiVM/interpreter/code-manager/func-struct.hpp"
#include "ChaiVM/memory/linear-allocator.hpp"
#include "ChaiVM/memory/linear-buffer.hpp"

namespace chai::interpreter {

class Frame {

public:
Frame(Frame *prev, const Function &func, memory::LinearBuffer &buffer);

void passArgs();

chai::chsize_t &operator[](size_t n) &;
const chsize_t &operator[](size_t n) const &;

/**
* Get state.
* @return state.
*/
std::vector<chsize_t> copyState();

Frame *back();

public:
Function const &func_;
chsize_t pc_;

private:
Frame *prev_;
size_t regsize_;
std::vector<chsize_t, memory::LinearAllocator<chsize_t>> registers_;
};

} // namespace chai::interpreter
33 changes: 0 additions & 33 deletions include/ChaiVM/interpreter/reg-file.hpp

This file was deleted.

Loading

2 comments on commit 80f9e84

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 80f9e84 Nov 9, 2023

Choose a reason for hiding this comment

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

Puzzle 1-083ca17e discovered in include/ChaiVM/interpreter/code-manager/code-manager.hpp) and submitted as #63. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 80f9e84 Nov 9, 2023

Choose a reason for hiding this comment

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

Puzzle 1-b9389013 discovered in src/ChaiVM/interpreter/code-manager/code-manager.cpp) and submitted as #64. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.