Skip to content

Commit

Permalink
#87 some code investigation
Browse files Browse the repository at this point in the history
  • Loading branch information
c71n93 committed Jun 8, 2024
1 parent 61f28ac commit 5b8f3b0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 33 deletions.
2 changes: 1 addition & 1 deletion include/ChaiVM/interpreter/code-manager/code-manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CodeManager final {

chsize_t getCnst(Immidiate id);

const std::string &getCnstString(Immidiate id) { return stringPool_[id]; }
const std::string &getCnstString(Immidiate id);

Immidiate addCnstString(std::string &&str) {
stringPool_.emplace_back(str);
Expand Down
26 changes: 11 additions & 15 deletions include/frontend/assembler/assembler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ class Assembler final {
std::ifstream inputFile_;
std::filesystem::path outPath_;

/*
* @todo #41:90min Refactor this function. Or maybe it is better to kill
* myself?
*/
void processMain() {
lex_.nextLexem();
checkError();
Expand All @@ -66,11 +62,11 @@ class Assembler final {
void processFunction() {
expectCurrentLexem(AsmLex::FUNC, "Expected function declaration");
expectNextLexem(AsmLex::IDENTIFIER, "Expected function name");
std::string func_name = static_cast<AsmLex::Identifier *>(lex_.currentLexem().get())->value;
std::string func_name = dynamic_cast<AsmLex::Identifier *>(lex_.currentLexem().get())->value;
expectNextLexem(AsmLex::INTEGER, "Expected number of registers in function frame");
auto num_regs = static_cast<uint8_t>(static_cast<AsmLex::Int *>(lex_.currentLexem().get())->value);
auto num_regs = static_cast<uint8_t>(dynamic_cast<AsmLex::Int *>(lex_.currentLexem().get())->value);
expectNextLexem(AsmLex::INTEGER, "Expected number of function arguments");
auto num_args = static_cast<uint8_t>(static_cast<AsmLex::Int *>(lex_.currentLexem().get())->value);
auto num_args = static_cast<uint8_t>(dynamic_cast<AsmLex::Int *>(lex_.currentLexem().get())->value);
expectNextLexem(AsmLex::OP_CURLY_BRACKET, "Expected opening curly bracket");
std::vector<chai::bytecode_t> instrs;
lex_.nextLexem();
Expand All @@ -83,7 +79,7 @@ class Assembler final {
}
chai::bytecode_t processInstruction() {
SmartOperation op = SmartOperation(
static_cast<AsmLex::Identifier *>(lex_.currentLexem().get())
dynamic_cast<AsmLex::Identifier *>(lex_.currentLexem().get())
->value);
if (op == chai::interpreter::Inv) {
throw AssembleError("Invalid instruction", lex_.lineno());
Expand Down Expand Up @@ -133,19 +129,19 @@ class Assembler final {
lex_.nextLexem();
if (lex_.currentLexem()->type == AsmLex::INTEGER) {
auto val = static_cast<int64_t>(
static_cast<AsmLex::Int *>(lex_.currentLexem().get())->value);
dynamic_cast<AsmLex::Int *>(lex_.currentLexem().get())->value);
auto imm = chaiFile_.addConst(
std::make_unique<chai::utils::fileformat::ConstI64>(val));
return chai::utils::instr2Raw(op, imm);
} else if (lex_.currentLexem()->type == AsmLex::FLOAT) {
auto val =
static_cast<AsmLex::Float *>(lex_.currentLexem().get())->value;
dynamic_cast<AsmLex::Float *>(lex_.currentLexem().get())->value;
auto imm = chaiFile_.addConst(
std::make_unique<chai::utils::fileformat::ConstI64>(val));
return chai::utils::instr2Raw(op, imm);
} else if (lex_.currentLexem()->type == AsmLex::STRING) {
std::string str =
static_cast<AsmLex::String *>(lex_.currentLexem().get())->value;
dynamic_cast<AsmLex::String *>(lex_.currentLexem().get())->value;
auto imm = chaiFile_.addConst(
std::make_unique<chai::utils::fileformat::ConstRawStr>(str));
return chai::utils::instr2Raw(op, imm);
Expand All @@ -160,19 +156,19 @@ class Assembler final {
lex_.nextLexem();
if (lex_.currentLexem()->type == AsmLex::INTEGER) {
auto val = static_cast<int64_t>(
static_cast<AsmLex::Int *>(lex_.currentLexem().get())->value);
dynamic_cast<AsmLex::Int *>(lex_.currentLexem().get())->value);
auto imm = chaiFile_.addConst(
std::make_unique<chai::utils::fileformat::ConstI64>(val));
return chai::utils::instr2RawRI(op, regId, imm);
} else if (lex_.currentLexem()->type == AsmLex::FLOAT) {
auto val =
static_cast<AsmLex::Float *>(lex_.currentLexem().get())->value;
dynamic_cast<AsmLex::Float *>(lex_.currentLexem().get())->value;
auto imm = chaiFile_.addConst(
std::make_unique<chai::utils::fileformat::ConstI64>(val));
return chai::utils::instr2RawRI(op, regId, imm);
} else if (lex_.currentLexem()->type == AsmLex::STRING) {
std::string str =
static_cast<AsmLex::String *>(lex_.currentLexem().get())->value;
dynamic_cast<AsmLex::String *>(lex_.currentLexem().get())->value;
auto imm = chaiFile_.addConst(
std::make_unique<chai::utils::fileformat::ConstRawStr>(str));
return chai::utils::instr2RawRI(op, regId, imm);
Expand All @@ -185,7 +181,7 @@ class Assembler final {
chai::interpreter::RegisterId processReg() {
expectNextLexem(AsmLex::IDENTIFIER, "Expected register name");
return regNameToRegId(
static_cast<AsmLex::Identifier *>(lex_.currentLexem().get())
dynamic_cast<AsmLex::Identifier *>(lex_.currentLexem().get())
->value);
}

Expand Down
22 changes: 12 additions & 10 deletions src/ChaiVM/interpreter/code-manager/code-manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ void CodeManager::loadPool(std::istream &istream) {
ConstantTag type;
istream.read(reinterpret_cast<char *>(&type), sizeof type);
switch (type) {
case CNST_I64:
case CNST_I64: {
int64_t next_long;
istream.read(reinterpret_cast<char *>(&next_long),
sizeof next_long);
constantPool_.push_back(next_long);
break;
case CNST_F64:
} case CNST_F64: {
double next_d;
istream.read(reinterpret_cast<char *>(&next_d), sizeof next_d);
constantPool_.push_back(std::bit_cast<chsize_t>(next_d));
break;
case CNST_FUNC_NAME_AND_TYPE:
} case CNST_FUNC_NAME_AND_TYPE: {
Immidiate name_index;
istream.read(reinterpret_cast<char *>(&name_index),
sizeof name_index);
Expand All @@ -57,18 +57,16 @@ void CodeManager::loadPool(std::istream &istream) {
(static_cast<chsize_t>(name_index) << 16) |
(static_cast<chsize_t>(descriptor_index) << 0));
break;
case CNST_RAW_STR:
} case CNST_RAW_STR: {
uint16_t len;
istream.read(reinterpret_cast<char *>(&len), sizeof len);
char *buf;
buf = new char[len + 1];
istream.read(buf, len);
std::unique_ptr<char[]> buf{new char[len + 1]};
istream.read(buf.get(), len);
buf[len] = 0;
stringPool_.emplace_back(buf);
stringPool_.emplace_back(buf.get());
constantPool_.push_back(stringPool_.size() - 1);
delete[] buf;
break;
default:
} default:
throw std::invalid_argument(std::string{"Type cannot be "} +
std::to_string(type));
break;
Expand Down Expand Up @@ -123,6 +121,10 @@ chsize_t CodeManager::getCnst(Immidiate id) {
return constantPool_[id];
}

const std::string &CodeManager::getCnstString(Immidiate id) {
return stringPool_[id];
}

bytecode_t CodeManager::getBytecode(size_t func, chsize_t pc) {
if (pc / sizeof(bytecode_t) >= funcs_[func].code.size() ||
pc % sizeof(bytecode_t) != 0) {
Expand Down
12 changes: 6 additions & 6 deletions src/ChaiVM/interpreter/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ void Executor::call(Instruction ins) {
DO_NEXT_INS();
}
void Executor::newi64array(Instruction ins) {
int n = static_cast<int64_t>(acc());
auto n = static_cast<int64_t>(acc());
memory::LinearAllocator<int64_t> allocator{buffer_};
assert(n >= 0);
auto *arr = new (allocator.allocate(n)) int64_t[n]();
Expand All @@ -323,23 +323,23 @@ void Executor::newi64array(Instruction ins) {
DO_NEXT_INS();
}
void Executor::get_i64from_arr(Instruction ins) {
int i = static_cast<int64_t>((*currentFrame_)[ins.r1]);
auto i = static_cast<int64_t>((*currentFrame_)[ins.r1]);
auto *arr = reinterpret_cast<int64_t *>(acc());
acc() = arr[i];
advancePc();
DO_NEXT_INS();
}

void Executor::set_i64in_arr(Instruction ins) {
int i = static_cast<int64_t>((*currentFrame_)[ins.r1]);
auto i = static_cast<int64_t>((*currentFrame_)[ins.r1]);
auto *arr = reinterpret_cast<int64_t *>(acc());
arr[i] = static_cast<int64_t>((*currentFrame_)[ins.r2]);
advancePc();
DO_NEXT_INS();
}

void Executor::newf64array(Instruction ins) {
int n = static_cast<int64_t>(acc());
auto n = static_cast<int64_t>(acc());
memory::LinearAllocator<double> allocator{buffer_};
assert(n >= 0);
auto *arr = new (allocator.allocate(n)) double[n]();
Expand All @@ -348,15 +348,15 @@ void Executor::newf64array(Instruction ins) {
DO_NEXT_INS();
}
void Executor::get_f64from_arr(Instruction ins) {
int i = static_cast<int64_t>((*currentFrame_)[ins.r1]);
auto i = static_cast<int64_t>((*currentFrame_)[ins.r1]);
auto *arr = reinterpret_cast<double *>(acc());
acc() = std::bit_cast<int64_t>(arr[i]);
advancePc();
DO_NEXT_INS();
}

void Executor::set_f64in_arr(Instruction ins) {
int i = static_cast<int64_t>((*currentFrame_)[ins.r1]);
auto i = static_cast<int64_t>((*currentFrame_)[ins.r1]);
auto *arr = reinterpret_cast<double *>(acc());
arr[i] = std::bit_cast<double>((*currentFrame_)[ins.r2]);
advancePc();
Expand Down
3 changes: 2 additions & 1 deletion test/frontend/assembler/assembler-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ TEST_F(AssemblerTest, runWithStrings) {

TEST_F(AssemblerTest, runWithFunctions) {
write_input_ << "Ldia 271\n"
<< "Call 8\n"
<< "Ldia 228\n"
<< "Call 9\n"
<< "Ret\n"
<< "fn aboba_func 50 2 {\n"
<< "Ldia 125\n"
Expand Down

0 comments on commit 5b8f3b0

Please sign in to comment.