-
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.
- Loading branch information
1 parent
942e6fd
commit a3d8a67
Showing
15 changed files
with
541 additions
and
367 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
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
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
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,61 @@ | ||
#pragma once | ||
|
||
#include "constant.hpp" | ||
#include "instr2Raw.hpp" | ||
|
||
class ChaiFile { | ||
public: | ||
ChaiFile(std::vector<chai::bytecode_t> &&instrs, | ||
std::vector<std::unique_ptr<Constant>> &&pool) | ||
: rawInstrs_(instrs), pool_(std::move(pool)) {} | ||
|
||
ChaiFile() | ||
: ChaiFile(std::vector<chai::bytecode_t>{}, | ||
std::vector<std::unique_ptr<Constant>>{}) {} | ||
|
||
chai::interpreter::Immidiate addInstr(chai::bytecode_t raw) { | ||
rawInstrs_.push_back(raw); | ||
return rawInstrs_.size() - 1; | ||
} | ||
|
||
chai::interpreter::Immidiate | ||
addConst(std::unique_ptr<Constant> &&constant) { | ||
pool_.push_back(std::move(constant)); | ||
return pool_.size() - 1; | ||
} | ||
|
||
void addWithConst(chai::interpreter::Operation op, int64_t data) { | ||
chai::chsize_t id = addConst(std::make_unique<ConstI64>(data)); | ||
addInstr(chai::utils::instr2Raw(op, id)); | ||
} | ||
|
||
void addWithConst(Operation op, double data) { | ||
chai::chsize_t id = addConst(std::make_unique<ConstF64>(data)); | ||
addInstr(chai::utils::instr2Raw(op, id)); | ||
} | ||
|
||
void toFile(const std::filesystem::path &path) { | ||
std::ofstream ofs(path, std::ios::binary | std::ios::out); | ||
if (ofs.good() && ofs.is_open()) { | ||
Immidiate constants = pool_.size(); | ||
ofs.write(reinterpret_cast<const char *>(&constants), | ||
sizeof(constants)); | ||
for (const std::unique_ptr<Constant> &cnst : pool_) { | ||
cnst->putType(ofs); | ||
cnst->write(ofs); | ||
} | ||
for (const auto &ins : rawInstrs_) { | ||
ofs.write(reinterpret_cast<const char *>(&ins), | ||
sizeof(chai::bytecode_t)); | ||
} | ||
ofs.close(); | ||
} else { | ||
throw std::invalid_argument(std::string{"Invalid path "} + | ||
path.string()); | ||
} | ||
} | ||
|
||
private: | ||
std::vector<chai::bytecode_t> rawInstrs_; | ||
std::vector<std::unique_ptr<Constant>> pool_; | ||
}; |
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,40 @@ | ||
#pragma once | ||
|
||
#include "ChaiVM/interpreter/code-manager.hpp" | ||
#include "ChaiVM/interpreter/instruction.hpp" | ||
#include "ChaiVM/types.hpp" | ||
|
||
struct Constant { | ||
void putType(std::ofstream &ofs) { ofs.put(this->getType()); } | ||
|
||
virtual void write(std::ofstream &ofs) = 0; | ||
virtual int8_t getType() = 0; | ||
virtual ~Constant() = default; | ||
; | ||
}; | ||
|
||
struct ConstI64 : public Constant { | ||
int64_t data_; | ||
|
||
ConstI64(int64_t data) : data_(data){}; | ||
|
||
void write(std::ofstream &ofs) override { | ||
ofs.write(reinterpret_cast<const char *>(&data_), sizeof(int64_t)); | ||
} | ||
int8_t getType() override { | ||
return chai::interpreter::CodeManager::CNST_I64; | ||
} | ||
~ConstI64() override = default; | ||
}; | ||
|
||
struct ConstF64 : public Constant { | ||
double data_; | ||
ConstF64(double data) : data_(data){}; | ||
void write(std::ofstream &ofs) override { | ||
ofs.write(reinterpret_cast<const char *>(&data_), sizeof(double)); | ||
} | ||
int8_t getType() override { | ||
return chai::interpreter::CodeManager::CNST_F64; | ||
} | ||
~ConstF64() override = default; | ||
}; |
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
Oops, something went wrong.