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;
|
||||
|
||||
|
@ -1,16 +1,97 @@
|
||||
#include "elna/backend/target.hpp"
|
||||
#include "elna/backend/riscv.hpp"
|
||||
#include <elfio/elfio.hpp>
|
||||
#include <cstring>
|
||||
|
||||
namespace elna::riscv
|
||||
{
|
||||
void riscv32_elf(source::block *ast, const std::filesystem::path& out_file)
|
||||
elfio_writer::iterator::reference elfio_writer::iterator::operator*() const noexcept
|
||||
{
|
||||
auto _visitor = std::make_unique<visitor>();
|
||||
_visitor->visit(ast);
|
||||
return payload;
|
||||
}
|
||||
|
||||
elfio_writer::iterator::pointer elfio_writer::iterator::operator->() const noexcept
|
||||
{
|
||||
return &payload;
|
||||
}
|
||||
|
||||
elfio_writer::iterator& elfio_writer::iterator::operator++()
|
||||
{
|
||||
this->payload.data += *this->sizes;
|
||||
this->payload.label = *(++this->labels);
|
||||
this->payload.size = *(++this->sizes);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
elfio_writer::iterator& elfio_writer::iterator::operator++(int)
|
||||
{
|
||||
auto tmp = *this;
|
||||
++(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool elfio_writer::iterator::operator==(const iterator& that) const
|
||||
{
|
||||
return this->labels == that.labels;
|
||||
}
|
||||
|
||||
bool elfio_writer::iterator::operator!=(const iterator& that) const
|
||||
{
|
||||
return !(*this == that);
|
||||
}
|
||||
|
||||
elfio_writer::elfio_writer(ELFIO::section *text)
|
||||
: text(text), labels(std::make_shared<std::vector<std::string>>()),
|
||||
sizes(std::make_shared<std::vector<std::size_t>>())
|
||||
{
|
||||
}
|
||||
|
||||
void elfio_writer::operator()(const std::string& label, const std::byte *data, std::size_t size)
|
||||
{
|
||||
labels->push_back(label + '\0');
|
||||
sizes->push_back(size);
|
||||
text->append_data(reinterpret_cast<const char *>(data), size);
|
||||
}
|
||||
|
||||
std::string_view elfio_writer::operator()(const std::byte *data, std::size_t size)
|
||||
{
|
||||
auto found = std::find_if(begin(), end(),
|
||||
[data, size](elfio_writer::entry entry) {
|
||||
return size == entry.size && std::memcmp(entry.data, data, size) == 0;
|
||||
});
|
||||
if (found == end())
|
||||
{
|
||||
(*this)(".CL" + std::to_string(labels->size()), data, size);
|
||||
return labels->back();
|
||||
}
|
||||
return found->label;
|
||||
}
|
||||
|
||||
elfio_writer::iterator elfio_writer::begin() const
|
||||
{
|
||||
return elfio_writer::iterator(labels->cbegin(), sizes->cbegin(),
|
||||
reinterpret_cast<const std::byte *>(text->get_data()));
|
||||
}
|
||||
|
||||
elfio_writer::iterator elfio_writer::end() const
|
||||
{
|
||||
return elfio_writer::iterator(labels->cend(), sizes->cend());
|
||||
}
|
||||
|
||||
std::ptrdiff_t elfio_writer::lookup(const std::string& label)
|
||||
{
|
||||
auto found = std::find(labels->cbegin(), labels->cend(), label);
|
||||
|
||||
if (found == labels->cend())
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return std::distance(labels->cbegin(), found);
|
||||
}
|
||||
|
||||
void riscv32_elf(source::program *ast, const std::filesystem::path& out_file)
|
||||
{
|
||||
ELFIO::elfio writer;
|
||||
const ELFIO::Elf_Word instructions_size = _visitor->instructions.size() * sizeof(instruction);
|
||||
|
||||
writer.create(ELFIO::ELFCLASS32, ELFIO::ELFDATA2LSB);
|
||||
|
||||
@ -23,8 +104,6 @@ namespace elna::riscv
|
||||
text_sec->set_type(ELFIO::SHT_PROGBITS);
|
||||
text_sec->set_flags(ELFIO::SHF_ALLOC | ELFIO::SHF_EXECINSTR);
|
||||
text_sec->set_addr_align(0x1);
|
||||
text_sec->set_data(reinterpret_cast<const char *>(_visitor->instructions.data()),
|
||||
instructions_size);
|
||||
|
||||
// Create string table section
|
||||
ELFIO::section* str_sec = writer.sections.add(".strtab");
|
||||
@ -56,32 +135,40 @@ namespace elna::riscv
|
||||
ro_sec->set_flags(ELFIO::SHF_ALLOC);
|
||||
ro_sec->set_addr_align(0x4);
|
||||
|
||||
elfio_writer text_writer{ text_sec };
|
||||
elfio_writer read_only_writer{ ro_sec };
|
||||
visitor _visitor{ text_writer, read_only_writer };
|
||||
_visitor.visit(ast);
|
||||
|
||||
// Create symbol relocation table writers
|
||||
ELFIO::symbol_section_accessor syma(writer, sym_sec);
|
||||
ELFIO::relocation_section_accessor rela(writer, rel_sec);
|
||||
ELFIO::Elf_Word digit_symbol;
|
||||
|
||||
for (auto read_only_text : _visitor->read_only)
|
||||
for (auto symbol : read_only_writer)
|
||||
{
|
||||
ro_sec->append_data(read_only_text.second.data(), read_only_text.second.size());
|
||||
|
||||
syma.add_symbol(stra, read_only_text.first.c_str(), 0x00000000,
|
||||
read_only_text.first.size() + 1, ELFIO::STB_LOCAL, ELFIO::STT_NOTYPE, 0, ro_sec->get_index());
|
||||
syma.add_symbol(stra, symbol.label.data(), 0x00000000,
|
||||
symbol.label.size(), ELFIO::STB_LOCAL, ELFIO::STT_NOTYPE, 0, ro_sec->get_index());
|
||||
}
|
||||
ELFIO::Elf_Word printf_symbol = syma.add_symbol(stra, "printf", 0x00000000, 0,
|
||||
ELFIO::STB_GLOBAL, ELFIO::STT_NOTYPE, 0, ELFIO::SHN_UNDEF);
|
||||
|
||||
for (auto& reference : _visitor->references)
|
||||
for (auto symbol : text_writer)
|
||||
{
|
||||
syma.add_symbol(stra, symbol.label.data(), 0x00000000, symbol.label.size(),
|
||||
ELFIO::STB_GLOBAL, ELFIO::STT_FUNC, 0, text_sec->get_index());
|
||||
}
|
||||
for (auto& reference : _visitor.references)
|
||||
{
|
||||
switch (reference.target)
|
||||
{
|
||||
case address_t::high20:
|
||||
digit_symbol = _visitor->read_only.lookup(reference.name) + 1;
|
||||
digit_symbol = read_only_writer.lookup(reference.name) + 1;
|
||||
rela.add_entry(reference.offset, digit_symbol, 26 /* ELFIO::R_RISCV_HI20 */);
|
||||
rela.add_entry(reference.offset, digit_symbol, 51 /* ELFIO::R_RISCV_RELAX */);
|
||||
break;
|
||||
case address_t::lower12i:
|
||||
digit_symbol = _visitor->read_only.lookup(reference.name) + 1;
|
||||
digit_symbol = read_only_writer.lookup(reference.name) + 1;
|
||||
rela.add_entry(reference.offset, digit_symbol, 27 /* ELFIO::R_RISCV_LO12_I */);
|
||||
rela.add_entry(reference.offset, digit_symbol, 51 /* ELFIO::R_RISCV_RELAX */);
|
||||
break;
|
||||
@ -91,8 +178,6 @@ namespace elna::riscv
|
||||
break;
|
||||
}
|
||||
}
|
||||
syma.add_symbol(stra, "main", 0x00000000, instructions_size,
|
||||
ELFIO::STB_GLOBAL, ELFIO::STT_FUNC, 0, text_sec->get_index());
|
||||
|
||||
// Create ELF object file
|
||||
writer.save(out_file);
|
||||
|
Reference in New Issue
Block a user