Replace ! with a function call writei
This commit is contained in:
@ -76,9 +76,8 @@ 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)
|
||||
visitor::visitor(std::shared_ptr<source::writer> writer)
|
||||
: writer(writer)
|
||||
{
|
||||
}
|
||||
|
||||
@ -127,40 +126,67 @@ namespace elna::riscv
|
||||
|
||||
void visitor::visit(source::program *program)
|
||||
{
|
||||
this->writer->sink("printf");
|
||||
auto format_string = this->writer->sink(reinterpret_cast<const std::byte *>("%d\n\0"), 4);
|
||||
|
||||
this->instructions.push_back(instruction(base_opcode::opImm)
|
||||
.i(x_register::sp, funct3_t::addi, x_register::sp, -8));
|
||||
this->instructions.push_back(instruction(base_opcode::store)
|
||||
.s(4, funct3_t::sw, x_register::sp, x_register::s0));
|
||||
this->instructions.push_back(instruction(base_opcode::store)
|
||||
.s(0, funct3_t::sw, x_register::sp, x_register::ra));
|
||||
this->instructions.push_back(instruction(base_opcode::opImm)
|
||||
.i(x_register::s0, funct3_t::addi, x_register::sp, -8));
|
||||
|
||||
this->references.push_back(reference());
|
||||
this->references.back().name = format_string;
|
||||
this->references.back().offset = writer->size() + 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;
|
||||
this->references.back().offset = writer->size() + instructions.size() * 4;
|
||||
this->references.back().target = address_t::lower12i;
|
||||
this->instructions.push_back(instruction(base_opcode::opImm)
|
||||
.i(x_register::a0, funct3_t::addi, x_register::a5, 0));
|
||||
|
||||
this->references.push_back(reference());
|
||||
this->references.back().name = "printf";
|
||||
this->references.back().offset = writer->size() + instructions.size() * 4;
|
||||
this->references.back().target = address_t::text;
|
||||
this->instructions.push_back(instruction(base_opcode::auipc).u(x_register::ra, 0));
|
||||
this->instructions.push_back(instruction(base_opcode::jalr)
|
||||
.i(x_register::ra, funct3_t::jalr, x_register::ra, 0));
|
||||
|
||||
this->instructions.push_back(instruction(base_opcode::load)
|
||||
.i(x_register::s0, funct3_t::lw, x_register::sp, 4));
|
||||
this->instructions.push_back(instruction(base_opcode::load)
|
||||
.i(x_register::ra, funct3_t::lw, x_register::sp, 0));
|
||||
this->instructions.push_back(instruction(base_opcode::opImm)
|
||||
.i(x_register::sp, funct3_t::addi, x_register::sp, 8));
|
||||
this->instructions.push_back(instruction(base_opcode::jalr)
|
||||
.i(x_register::zero, funct3_t::jalr, x_register::ra, 0));
|
||||
|
||||
this->writer->sink("writei", reinterpret_cast<const std::byte *>(this->instructions.data()),
|
||||
this->instructions.size() * sizeof(instruction));
|
||||
|
||||
this->instructions.clear();
|
||||
visit(dynamic_cast<source::block *>(program));
|
||||
write_text("main", reinterpret_cast<const std::byte *>(this->instructions.data()),
|
||||
this->writer->sink("main", reinterpret_cast<const std::byte *>(this->instructions.data()),
|
||||
this->instructions.size() * sizeof(instruction));
|
||||
}
|
||||
|
||||
void visitor::visit(source::bang_statement *statement)
|
||||
void visitor::visit(source::call_statement *statement)
|
||||
{
|
||||
statement->body().accept(this);
|
||||
statement->arguments().accept(this);
|
||||
|
||||
// Print the result.
|
||||
this->instructions.push_back(instruction(base_opcode::opImm)
|
||||
.i(x_register::a1, funct3_t::addi, x_register::a0, 0));
|
||||
|
||||
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;
|
||||
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;
|
||||
this->references.back().offset = instructions.size() * 4;
|
||||
this->references.back().target = address_t::lower12i;
|
||||
|
||||
this->instructions.push_back(instruction(base_opcode::opImm)
|
||||
.i(x_register::a0, funct3_t::addi, x_register::a5, 0));
|
||||
this->references.push_back(reference());
|
||||
this->references.back().name = "printf";
|
||||
this->references.back().offset = instructions.size() * 4;
|
||||
this->references.back().target = address_t::text;
|
||||
this->instructions.push_back(instruction(base_opcode::auipc).u(x_register::ra, 0));
|
||||
this->instructions.push_back(instruction(base_opcode::jalr)
|
||||
.i(x_register::ra, funct3_t::jalr, x_register::ra, 0));
|
||||
.i(x_register::ra, funct3_t::jalr, x_register::ra, -instructions.size() * 4 - 40));
|
||||
}
|
||||
|
||||
void visitor::visit(source::question_mark_statement *statement)
|
||||
@ -171,23 +197,23 @@ 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 = write_read_only(reinterpret_cast<const std::byte *>("%d\n\0"), 4);
|
||||
auto format_string = this->writer->sink(reinterpret_cast<const std::byte *>("%d\n\0"), 4);
|
||||
|
||||
this->references.push_back(reference());
|
||||
this->references.back().name = format_string;
|
||||
this->references.back().offset = instructions.size() * 4;
|
||||
this->references.back().offset = writer->size() + 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;
|
||||
this->references.back().offset = instructions.size() * 4;
|
||||
this->references.back().offset = writer->size() + instructions.size() * 4;
|
||||
this->references.back().target = address_t::lower12i;
|
||||
|
||||
this->instructions.push_back(instruction(base_opcode::opImm)
|
||||
.i(x_register::a0, funct3_t::addi, x_register::a5, 0));
|
||||
this->references.push_back(reference());
|
||||
this->references.back().name = "printf";
|
||||
this->references.back().offset = instructions.size() * 4;
|
||||
this->references.back().offset = writer->size() + instructions.size() * 4;
|
||||
this->references.back().target = address_t::text;
|
||||
this->instructions.push_back(instruction(base_opcode::auipc).u(x_register::ra, 0));
|
||||
this->instructions.push_back(instruction(base_opcode::jalr)
|
||||
@ -224,7 +250,7 @@ namespace elna::riscv
|
||||
instructions.push_back(instruction(base_opcode::branch));
|
||||
statement->body().accept(this);
|
||||
instructions[before_branch]
|
||||
.b((instructions.size() - before_branch) * 4 - 3, funct3_t::beq, x_register::zero, free_register);
|
||||
.b((instructions.size() - before_branch) * 4, funct3_t::beq, x_register::zero, free_register);
|
||||
}
|
||||
|
||||
void visitor::visit(source::while_statement *statement)
|
||||
|
@ -4,17 +4,17 @@
|
||||
|
||||
namespace elna::riscv
|
||||
{
|
||||
elfio_writer::iterator::reference elfio_writer::iterator::operator*() const noexcept
|
||||
elfio_section_writer::iterator::reference elfio_section_writer::iterator::operator*() const noexcept
|
||||
{
|
||||
return payload;
|
||||
}
|
||||
|
||||
elfio_writer::iterator::pointer elfio_writer::iterator::operator->() const noexcept
|
||||
elfio_section_writer::iterator::pointer elfio_section_writer::iterator::operator->() const noexcept
|
||||
{
|
||||
return &payload;
|
||||
}
|
||||
|
||||
elfio_writer::iterator& elfio_writer::iterator::operator++()
|
||||
elfio_section_writer::iterator& elfio_section_writer::iterator::operator++()
|
||||
{
|
||||
this->payload.data += *this->sizes;
|
||||
this->payload.label = *(++this->labels);
|
||||
@ -23,70 +23,127 @@ namespace elna::riscv
|
||||
return *this;
|
||||
}
|
||||
|
||||
elfio_writer::iterator& elfio_writer::iterator::operator++(int)
|
||||
elfio_section_writer::iterator& elfio_section_writer::iterator::operator++(int)
|
||||
{
|
||||
auto tmp = *this;
|
||||
++(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool elfio_writer::iterator::operator==(const iterator& that) const
|
||||
bool elfio_section_writer::iterator::operator==(const iterator& that) const
|
||||
{
|
||||
return this->labels == that.labels;
|
||||
}
|
||||
|
||||
bool elfio_writer::iterator::operator!=(const iterator& that) const
|
||||
bool elfio_section_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>>())
|
||||
elfio_section_writer::elfio_section_writer(ELFIO::section *section)
|
||||
: m_section(section)
|
||||
{
|
||||
}
|
||||
|
||||
void elfio_writer::operator()(const std::string& label, const std::byte *data, std::size_t size)
|
||||
void elfio_section_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);
|
||||
labels.push_back(label);
|
||||
sizes.push_back(size);
|
||||
m_section->append_data(reinterpret_cast<const char *>(data), size);
|
||||
}
|
||||
|
||||
std::string_view elfio_writer::operator()(const std::byte *data, std::size_t size)
|
||||
std::pair<std::string_view, bool> elfio_section_writer::operator()(const std::byte *data, std::size_t size)
|
||||
{
|
||||
auto found = std::find_if(begin(), end(),
|
||||
[data, size](elfio_writer::entry entry) {
|
||||
[data, size](elfio_section_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();
|
||||
(*this)(".CL" + std::to_string(labels.size()), data, size);
|
||||
return std::pair<std::string_view, bool>(labels.back(), true);
|
||||
}
|
||||
return found->label;
|
||||
return std::pair<std::string_view, bool>(found->label, false);
|
||||
}
|
||||
|
||||
elfio_writer::iterator elfio_writer::begin() const
|
||||
elfio_section_writer::iterator elfio_section_writer::begin() const
|
||||
{
|
||||
return elfio_writer::iterator(labels->cbegin(), sizes->cbegin(),
|
||||
reinterpret_cast<const std::byte *>(text->get_data()));
|
||||
return elfio_section_writer::iterator(labels.cbegin(), sizes.cbegin(),
|
||||
reinterpret_cast<const std::byte *>(m_section->get_data()));
|
||||
}
|
||||
|
||||
elfio_writer::iterator elfio_writer::end() const
|
||||
elfio_section_writer::iterator elfio_section_writer::end() const
|
||||
{
|
||||
return elfio_writer::iterator(labels->cend(), sizes->cend());
|
||||
return elfio_section_writer::iterator(labels.cend(), sizes.cend());
|
||||
}
|
||||
|
||||
std::ptrdiff_t elfio_writer::lookup(const std::string& label)
|
||||
ELFIO::section *elfio_section_writer::section() noexcept
|
||||
{
|
||||
auto found = std::find(labels->cbegin(), labels->cend(), label);
|
||||
return m_section;
|
||||
}
|
||||
|
||||
if (found == labels->cend())
|
||||
elfio_writer::elfio_writer(ELFIO::section *text, ELFIO::section *read_only,
|
||||
ELFIO::symbol_section_accessor symbol_accessor, ELFIO::string_section_accessor string_accessor)
|
||||
: text(text), read_only(elfio_section_writer(read_only)),
|
||||
symbol_accessor(symbol_accessor), string_accessor(string_accessor)
|
||||
{
|
||||
}
|
||||
|
||||
std::size_t elfio_writer::sink(const std::string& label, const std::byte *data, std::size_t size)
|
||||
{
|
||||
auto offset = text->get_size();
|
||||
text->append_data(reinterpret_cast<const char *>(data), size);
|
||||
|
||||
this->symbol_accessor.add_symbol(this->string_accessor, label.data(), offset, label.size(),
|
||||
ELFIO::STB_GLOBAL, ELFIO::STT_FUNC, 0, text->get_index());
|
||||
|
||||
return text->get_size();
|
||||
}
|
||||
|
||||
std::string_view elfio_writer::sink(const std::byte *data, std::size_t size)
|
||||
{
|
||||
auto offset = read_only.section()->get_size();
|
||||
auto [result, inserted] = read_only(data, size);
|
||||
|
||||
if (inserted)
|
||||
{
|
||||
return -1;
|
||||
this->symbol_accessor.add_symbol(this->string_accessor, result.data(), offset,
|
||||
result.size(), ELFIO::STB_LOCAL, ELFIO::STT_NOTYPE, 0,
|
||||
read_only.section()->get_index());
|
||||
}
|
||||
return std::distance(labels->cbegin(), found);
|
||||
return result;
|
||||
}
|
||||
|
||||
void elfio_writer::sink(const std::string& label)
|
||||
{
|
||||
this->symbol_accessor.add_symbol(this->string_accessor, "printf", 0x00000000, 0,
|
||||
ELFIO::STB_GLOBAL, ELFIO::STT_NOTYPE, 0, ELFIO::SHN_UNDEF);
|
||||
}
|
||||
|
||||
std::size_t elfio_writer::size() const
|
||||
{
|
||||
return this->text->get_size();
|
||||
}
|
||||
|
||||
std::ptrdiff_t lookup(ELFIO::symbol_section_accessor symbol_accessor, const std::string& label)
|
||||
{
|
||||
for (ptrdiff_t j = 0; j < symbol_accessor.get_symbols_num(); ++j)
|
||||
{
|
||||
std::string name;
|
||||
ELFIO::Elf64_Addr value;
|
||||
ELFIO::Elf_Xword size;
|
||||
unsigned char bind;
|
||||
unsigned char type;
|
||||
ELFIO::Elf_Half section_index;
|
||||
unsigned char other;
|
||||
|
||||
symbol_accessor.get_symbol(j, name, value, size, bind, type, section_index, other);
|
||||
if (name == label)
|
||||
{
|
||||
return j;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void riscv32_elf(source::program *ast, const std::filesystem::path& out_file)
|
||||
@ -135,46 +192,36 @@ 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;
|
||||
auto _writer = std::make_shared<elfio_writer>(text_sec, ro_sec, syma, stra);
|
||||
|
||||
for (auto symbol : read_only_writer)
|
||||
{
|
||||
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);
|
||||
visitor _visitor{ _writer };
|
||||
_visitor.visit(ast);
|
||||
|
||||
syma.arrange_local_symbols();
|
||||
|
||||
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)
|
||||
{
|
||||
ELFIO::Elf_Word relocated_symbol{ 0 };
|
||||
|
||||
switch (reference.target)
|
||||
{
|
||||
case address_t::high20:
|
||||
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 */);
|
||||
relocated_symbol = lookup(syma, reference.name);
|
||||
rela.add_entry(reference.offset, relocated_symbol, 26 /* ELFIO::R_RISCV_HI20 */);
|
||||
rela.add_entry(reference.offset, relocated_symbol, 51 /* ELFIO::R_RISCV_RELAX */);
|
||||
break;
|
||||
case address_t::lower12i:
|
||||
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 */);
|
||||
relocated_symbol = lookup(syma, reference.name);
|
||||
rela.add_entry(reference.offset, relocated_symbol, 27 /* ELFIO::R_RISCV_LO12_I */);
|
||||
rela.add_entry(reference.offset, relocated_symbol, 51 /* ELFIO::R_RISCV_RELAX */);
|
||||
break;
|
||||
case address_t::text:
|
||||
rela.add_entry(reference.offset, printf_symbol, 18 /* ELFIO::R_RISCV_CALL */);
|
||||
rela.add_entry(reference.offset, printf_symbol, 51 /* ELFIO::R_RISCV_RELAX */);
|
||||
relocated_symbol = lookup(syma, "printf");
|
||||
rela.add_entry(reference.offset, relocated_symbol, 18 /* ELFIO::R_RISCV_CALL */);
|
||||
rela.add_entry(reference.offset, relocated_symbol, 51 /* ELFIO::R_RISCV_RELAX */);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user