Write parts of generated code directly to objects
This commit is contained in:
@ -76,6 +76,12 @@ namespace elna::riscv
|
||||
return reinterpret_cast<const std::byte *>(&this->representation) + sizeof(this->representation);
|
||||
}
|
||||
|
||||
visitor::visitor(std::function<void(const std::string&, const std::byte *, std::size_t)> write_text,
|
||||
std::function<std::string_view(const std::byte *, std::size_t)> write_read_only)
|
||||
: write_text(write_text), write_read_only(write_read_only)
|
||||
{
|
||||
}
|
||||
|
||||
void visitor::visit(source::declaration *declaration)
|
||||
{
|
||||
}
|
||||
@ -119,6 +125,13 @@ namespace elna::riscv
|
||||
.i(x_register::zero, funct3_t::jalr, x_register::ra, 0));
|
||||
}
|
||||
|
||||
void visitor::visit(source::program *program)
|
||||
{
|
||||
visit(dynamic_cast<source::block *>(program));
|
||||
write_text("main", reinterpret_cast<const std::byte *>(this->instructions.data()),
|
||||
this->instructions.size() * sizeof(instruction));
|
||||
}
|
||||
|
||||
void visitor::visit(source::bang_statement *statement)
|
||||
{
|
||||
statement->body().accept(this);
|
||||
@ -127,15 +140,15 @@ namespace elna::riscv
|
||||
this->instructions.push_back(instruction(base_opcode::opImm)
|
||||
.i(x_register::a1, funct3_t::addi, x_register::a0, 0));
|
||||
|
||||
auto format_string = this->read_only.label("%d\n");
|
||||
auto format_string = write_read_only(reinterpret_cast<const std::byte *>("%d\n\0"), 4);
|
||||
|
||||
this->references.push_back(reference());
|
||||
this->references.back().name = format_string->first;
|
||||
this->references.back().name = format_string;
|
||||
this->references.back().offset = instructions.size() * 4;
|
||||
this->references.back().target = address_t::high20;
|
||||
this->instructions.push_back(instruction(base_opcode::lui).u(x_register::a5, 0));
|
||||
this->references.push_back(reference());
|
||||
this->references.back().name = format_string->first;
|
||||
this->references.back().name = format_string;
|
||||
this->references.back().offset = instructions.size() * 4;
|
||||
this->references.back().target = address_t::lower12i;
|
||||
|
||||
@ -158,15 +171,15 @@ namespace elna::riscv
|
||||
this->instructions.push_back(instruction(base_opcode::opImm)
|
||||
.i(x_register::a1, funct3_t::addi, x_register::a0, 0));
|
||||
|
||||
auto format_string = this->read_only.label("%d\n");
|
||||
auto format_string = write_read_only(reinterpret_cast<const std::byte *>("%d\n\0"), 4);
|
||||
|
||||
this->references.push_back(reference());
|
||||
this->references.back().name = format_string->first;
|
||||
this->references.back().name = format_string;
|
||||
this->references.back().offset = instructions.size() * 4;
|
||||
this->references.back().target = address_t::high20;
|
||||
this->instructions.push_back(instruction(base_opcode::lui).u(x_register::a5, 0));
|
||||
this->references.push_back(reference());
|
||||
this->references.back().name = format_string->first;
|
||||
this->references.back().name = format_string;
|
||||
this->references.back().offset = instructions.size() * 4;
|
||||
this->references.back().target = address_t::lower12i;
|
||||
|
||||
|
Reference in New Issue
Block a user