Write parts of generated code directly to objects

This commit is contained in:
Eugen Wissner 2024-03-18 09:55:25 +01:00
parent 17b0650f77
commit bfabdb1d08
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
7 changed files with 246 additions and 189 deletions

4
TODO
View File

@ -7,8 +7,8 @@
- Move constants to the symbol table, so we can check at parse time for duplicates. - Move constants to the symbol table, so we can check at parse time for duplicates.
- Don't pass raw pointers to the visitor methods. - Don't pass raw pointers to the visitor methods.
- While loop. - While loop.
- If condition. - Comparision operators.
- Introduce program node which contains global state and functions. - Procedures.
- Calculate additional stack space needed for subexpressions in the allocator - Calculate additional stack space needed for subexpressions in the allocator
visitor and not in the backend. visitor and not in the backend.
- Support immediates greater than 12 bits. - Support immediates greater than 12 bits.

View File

@ -76,6 +76,12 @@ namespace elna::riscv
return reinterpret_cast<const std::byte *>(&this->representation) + sizeof(this->representation); 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) void visitor::visit(source::declaration *declaration)
{ {
} }
@ -119,6 +125,13 @@ namespace elna::riscv
.i(x_register::zero, funct3_t::jalr, x_register::ra, 0)); .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) void visitor::visit(source::bang_statement *statement)
{ {
statement->body().accept(this); statement->body().accept(this);
@ -127,15 +140,15 @@ namespace elna::riscv
this->instructions.push_back(instruction(base_opcode::opImm) this->instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a1, funct3_t::addi, x_register::a0, 0)); .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.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().offset = instructions.size() * 4;
this->references.back().target = address_t::high20; this->references.back().target = address_t::high20;
this->instructions.push_back(instruction(base_opcode::lui).u(x_register::a5, 0)); this->instructions.push_back(instruction(base_opcode::lui).u(x_register::a5, 0));
this->references.push_back(reference()); 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().offset = instructions.size() * 4;
this->references.back().target = address_t::lower12i; this->references.back().target = address_t::lower12i;
@ -158,15 +171,15 @@ namespace elna::riscv
this->instructions.push_back(instruction(base_opcode::opImm) this->instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a1, funct3_t::addi, x_register::a0, 0)); .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.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().offset = instructions.size() * 4;
this->references.back().target = address_t::high20; this->references.back().target = address_t::high20;
this->instructions.push_back(instruction(base_opcode::lui).u(x_register::a5, 0)); this->instructions.push_back(instruction(base_opcode::lui).u(x_register::a5, 0));
this->references.push_back(reference()); 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().offset = instructions.size() * 4;
this->references.back().target = address_t::lower12i; this->references.back().target = address_t::lower12i;

View File

@ -1,16 +1,97 @@
#include "elna/backend/target.hpp" #include "elna/backend/target.hpp"
#include "elna/backend/riscv.hpp" #include "elna/backend/riscv.hpp"
#include <elfio/elfio.hpp> #include <cstring>
namespace elna::riscv 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>(); return payload;
_visitor->visit(ast); }
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; ELFIO::elfio writer;
const ELFIO::Elf_Word instructions_size = _visitor->instructions.size() * sizeof(instruction);
writer.create(ELFIO::ELFCLASS32, ELFIO::ELFDATA2LSB); writer.create(ELFIO::ELFCLASS32, ELFIO::ELFDATA2LSB);
@ -23,8 +104,6 @@ namespace elna::riscv
text_sec->set_type(ELFIO::SHT_PROGBITS); text_sec->set_type(ELFIO::SHT_PROGBITS);
text_sec->set_flags(ELFIO::SHF_ALLOC | ELFIO::SHF_EXECINSTR); text_sec->set_flags(ELFIO::SHF_ALLOC | ELFIO::SHF_EXECINSTR);
text_sec->set_addr_align(0x1); text_sec->set_addr_align(0x1);
text_sec->set_data(reinterpret_cast<const char *>(_visitor->instructions.data()),
instructions_size);
// Create string table section // Create string table section
ELFIO::section* str_sec = writer.sections.add(".strtab"); 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_flags(ELFIO::SHF_ALLOC);
ro_sec->set_addr_align(0x4); 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 // Create symbol relocation table writers
ELFIO::symbol_section_accessor syma(writer, sym_sec); ELFIO::symbol_section_accessor syma(writer, sym_sec);
ELFIO::relocation_section_accessor rela(writer, rel_sec); ELFIO::relocation_section_accessor rela(writer, rel_sec);
ELFIO::Elf_Word digit_symbol; 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, symbol.label.data(), 0x00000000,
symbol.label.size(), ELFIO::STB_LOCAL, ELFIO::STT_NOTYPE, 0, ro_sec->get_index());
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());
} }
ELFIO::Elf_Word printf_symbol = syma.add_symbol(stra, "printf", 0x00000000, 0, ELFIO::Elf_Word printf_symbol = syma.add_symbol(stra, "printf", 0x00000000, 0,
ELFIO::STB_GLOBAL, ELFIO::STT_NOTYPE, 0, ELFIO::SHN_UNDEF); 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) switch (reference.target)
{ {
case address_t::high20: 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, 26 /* ELFIO::R_RISCV_HI20 */);
rela.add_entry(reference.offset, digit_symbol, 51 /* ELFIO::R_RISCV_RELAX */); rela.add_entry(reference.offset, digit_symbol, 51 /* ELFIO::R_RISCV_RELAX */);
break; break;
case address_t::lower12i: 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, 27 /* ELFIO::R_RISCV_LO12_I */);
rela.add_entry(reference.offset, digit_symbol, 51 /* ELFIO::R_RISCV_RELAX */); rela.add_entry(reference.offset, digit_symbol, 51 /* ELFIO::R_RISCV_RELAX */);
break; break;
@ -91,8 +178,6 @@ namespace elna::riscv
break; break;
} }
} }
syma.add_symbol(stra, "main", 0x00000000, instructions_size,
ELFIO::STB_GLOBAL, ELFIO::STT_FUNC, 0, text_sec->get_index());
// Create ELF object file // Create ELF object file
writer.save(out_file); writer.save(out_file);

View File

@ -1,8 +1,7 @@
#pragma once #pragma once
#include <charconv>
#include <cstdint> #include <cstdint>
#include <algorithm> #include <functional>
#include "elna/source/parser.hpp" #include "elna/source/parser.hpp"
namespace elna::riscv namespace elna::riscv
@ -159,169 +158,20 @@ namespace elna::riscv
std::uint32_t representation{ 0 }; std::uint32_t representation{ 0 };
}; };
/**
* Assigns sequentially numbered labels to text strings.
*/
template<char... prefix>
struct read_only_table
{
private:
constexpr static const char get_prefix[] = { prefix... };
constexpr static const std::size_t prefix_length = sizeof(get_prefix) / sizeof(char);
public:
/**
* An iterator over label and string pairs.
*/
struct const_iterator
{
using iterator_category = std::forward_iterator_tag;
using difference_type = ptrdiff_t;
using value_type = std::pair<std::string, std::string_view>;
using pointer = const value_type *;
using reference = const value_type&;
reference operator*() const noexcept
{
return payload;
}
pointer operator->() const noexcept
{
return &payload;
}
const_iterator& operator++()
{
++index;
++iterator;
this->payload = std::pair<const std::string, std::string_view>(label(), *iterator);
return *this;
}
const_iterator& operator++(int)
{
auto tmp = *this;
++(*this);
return *this;
}
bool operator==(const const_iterator& that) const
{
return this->index == that.index;
}
bool operator!=(const const_iterator& that) const
{
return !(*this == that);
}
private:
std::vector<std::string>::const_iterator iterator;
std::size_t index;
value_type payload;
const_iterator(std::size_t index, std::vector<std::string>::const_iterator iterator)
: iterator(iterator), index(index), payload({ label(), *iterator })
{
}
const_iterator(std::size_t index, std::vector<std::string>::const_iterator iterator,
std::string_view value)
: iterator(iterator), index(index), payload({ get_prefix, value })
{
}
std::string label() const
{
return get_prefix + std::to_string(this->index);
}
friend read_only_table;
};
const_iterator begin()
{
if (payload.empty())
{
return end();
}
else
{
return read_only_table::const_iterator(0, payload.cbegin());
}
}
const_iterator end() const
{
return read_only_table::const_iterator(size(), payload.cend(), "");
}
std::size_t size() const
{
return payload.size();
}
/**
* Looks up \a needle in the string storage and returns a label for it
* or create a new one.
*
* \param needle A string to search for.
* \return Label name.
*/
const_iterator label(std::string_view needle)
{
auto format_string = std::find(this->payload.cbegin(), this->payload.cend(), needle);
if (format_string == this->payload.cend())
{
format_string = this->payload.emplace(format_string, needle);
}
auto read_only_index = std::distance(this->payload.cbegin(), format_string);
return read_only_table::const_iterator(read_only_index, format_string);
}
/**
* Searches the content by label and returns its index or -1 when the
* label does not exist.
*
* \param needle Label name.
* \return Data index.
*/
std::ptrdiff_t lookup(std::string_view needle)
{
if (needle.size() <= prefix_length)
{
return -1;
}
auto needle_middle = needle.cbegin() + prefix_length;
auto needle_prefix = std::string_view(needle.cbegin(), prefix_length);
std::size_t counter;
auto [position, char_error] = std::from_chars(needle_middle, needle.cend(), counter);
if (char_error != std::errc{} || position != needle.cend()
|| needle_prefix != get_prefix || counter >= size())
{
return -1;
}
return counter;
}
private:
std::vector<std::string> payload;
};
class visitor final : public source::parser_visitor class visitor final : public source::parser_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;
public: public:
std::vector<instruction> instructions; std::vector<instruction> instructions;
bool register_in_use{ true }; bool register_in_use{ true };
std::uint32_t variable_counter = 1; std::uint32_t variable_counter = 1;
std::vector<reference> references; std::vector<reference> references;
std::shared_ptr<source::symbol_table> table; std::shared_ptr<source::symbol_table> table;
read_only_table<'.', 'C', 'L'> read_only;
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);
virtual void visit(source::declaration *declaration) override; virtual void visit(source::declaration *declaration) override;
virtual void visit(source::definition *definition) override; virtual void visit(source::definition *definition) override;
@ -332,6 +182,7 @@ namespace elna::riscv
virtual void visit(source::if_statement *statement) override; virtual void visit(source::if_statement *statement) override;
virtual void visit(source::while_statement *statement) override; virtual void visit(source::while_statement *statement) override;
virtual void visit(source::block *block) override; virtual void visit(source::block *block) override;
virtual void visit(source::program *program) override;
virtual void visit(source::variable_expression *variable) override; virtual void visit(source::variable_expression *variable) override;
virtual void visit(source::binary_expression *expression) override; virtual void visit(source::binary_expression *expression) override;
virtual void visit(source::integer_literal *number) override; virtual void visit(source::integer_literal *number) override;

View File

@ -1,7 +1,80 @@
#include "elna/source/parser.hpp" #include "elna/source/parser.hpp"
#include <filesystem> #include <filesystem>
#include <elfio/elfio.hpp>
namespace elna::riscv namespace elna::riscv
{ {
void riscv32_elf(source::block *ast, const std::filesystem::path& out_file); struct elfio_writer
{
struct entry
{
std::string_view label;
const std::byte *data{ nullptr };
std::size_t size{ 0 };
};
/**
* An iterator over label and string pairs.
*/
struct iterator
{
using iterator_category = std::forward_iterator_tag;
using difference_type = ptrdiff_t;
using value_type = entry;
using pointer = const value_type *;
using reference = const value_type&;
reference operator*() const noexcept;
pointer operator->() const noexcept;
iterator& operator++();
iterator& operator++(int);
bool operator==(const iterator& that) const;
bool operator!=(const iterator& that) const;
private:
std::vector<std::string>::const_iterator labels;
std::vector<std::size_t>::const_iterator sizes;
value_type payload;
iterator(std::vector<std::string>::const_iterator labels, std::vector<std::size_t>::const_iterator sizes,
const std::byte *data)
: labels(labels), sizes(sizes)
{
if (data != nullptr)
{
payload = { *this->labels, data, *this->sizes};
}
}
iterator(std::vector<std::string>::const_iterator labels, std::vector<std::size_t>::const_iterator sizes)
: labels(labels), sizes(sizes), payload{}
{
}
friend elfio_writer;
};
explicit elfio_writer(ELFIO::section *text);
void operator()(const std::string& label, const std::byte *data, std::size_t size);
std::string_view operator()(const std::byte *data, std::size_t size);
iterator begin() const;
iterator end() const;
/**
* Searches the content by label and returns its index or -1 when the
* label does not exist.
*
* \param needle Label name.
* \return Data index.
*/
std::ptrdiff_t lookup(const std::string& label);
private:
std::shared_ptr<std::vector<std::string>> labels;
std::shared_ptr<std::vector<std::size_t>> sizes;
ELFIO::section *text;
};
void riscv32_elf(source::program *ast, const std::filesystem::path& out_file);
} }

View File

@ -23,6 +23,7 @@ namespace elna::source
class if_statement; class if_statement;
class while_statement; class while_statement;
class block; class block;
class program;
class binary_expression; class binary_expression;
class variable_expression; class variable_expression;
class integer_literal; class integer_literal;
@ -39,6 +40,7 @@ namespace elna::source
virtual void visit(if_statement *) = 0; virtual void visit(if_statement *) = 0;
virtual void visit(while_statement *) = 0; virtual void visit(while_statement *) = 0;
virtual void visit(block *) = 0; virtual void visit(block *) = 0;
virtual void visit(program *) = 0;
virtual void visit(binary_expression *) = 0; virtual void visit(binary_expression *) = 0;
virtual void visit(variable_expression *) = 0; virtual void visit(variable_expression *) = 0;
virtual void visit(integer_literal *) = 0; virtual void visit(integer_literal *) = 0;
@ -56,6 +58,7 @@ namespace elna::source
virtual void visit(if_statement *) override; virtual void visit(if_statement *) override;
virtual void visit(while_statement *) override; virtual void visit(while_statement *) override;
virtual void visit(block *block) override; virtual void visit(block *block) override;
virtual void visit(program *program) override;
virtual void visit(binary_expression *expression) override; virtual void visit(binary_expression *expression) override;
virtual void visit(variable_expression *variable) override; virtual void visit(variable_expression *variable) override;
virtual void visit(integer_literal *number) override; virtual void visit(integer_literal *number) override;
@ -186,9 +189,6 @@ namespace elna::source
statement& body(); statement& body();
}; };
/**
* Block.
*/
class block : public node class block : public node
{ {
std::unique_ptr<statement> m_body; std::unique_ptr<statement> m_body;
@ -208,6 +208,15 @@ namespace elna::source
std::shared_ptr<symbol_table> table(); std::shared_ptr<symbol_table> table();
}; };
class program : public block
{
public:
program(std::vector<std::unique_ptr<definition>>&& definitions,
std::vector<std::unique_ptr<declaration>>&& declarations,
std::unique_ptr<statement>&& body);
virtual void accept(parser_visitor *visitor) override;
};
class integer_literal : public expression class integer_literal : public expression
{ {
std::int32_t m_number; std::int32_t m_number;
@ -286,7 +295,7 @@ namespace elna::source
* *
* \return Parsed program or nothing if an error occurred. * \return Parsed program or nothing if an error occurred.
*/ */
std::unique_ptr<block> parse(); std::unique_ptr<program> parse();
/** /**
* Gets produced errors. * Gets produced errors.

View File

@ -60,6 +60,11 @@ namespace elna::source
block->body().accept(this); block->body().accept(this);
} }
void empty_visitor::visit(program *program)
{
visit(dynamic_cast<block *>(program));
}
void empty_visitor::visit(binary_expression *expression) void empty_visitor::visit(binary_expression *expression)
{ {
expression->lhs().accept(this); expression->lhs().accept(this);
@ -154,6 +159,18 @@ namespace elna::source
return m_table; return m_table;
} }
program::program(std::vector<std::unique_ptr<definition>>&& definitions,
std::vector<std::unique_ptr<declaration>>&& declarations,
std::unique_ptr<statement>&& body)
: block(std::move(definitions), std::move(declarations), std::move(body))
{
}
void program::accept(parser_visitor *visitor)
{
visitor->visit(this);
}
integer_literal::integer_literal(const std::int32_t value) integer_literal::integer_literal(const std::int32_t value)
: m_number(value) : m_number(value)
{ {
@ -352,9 +369,18 @@ namespace elna::source
{ {
} }
std::unique_ptr<block> parser::parse() std::unique_ptr<program> parser::parse()
{ {
return parse_block(); auto definitions = parse_definitions();
auto declarations = parse_declarations();
auto parsed_statement = parse_statement();
if (parsed_statement == nullptr)
{
return nullptr;
}
return std::make_unique<program>(std::move(definitions),
std::move(declarations), std::move(parsed_statement));
} }
const std::list<std::unique_ptr<error>>& parser::errors() const noexcept const std::list<std::unique_ptr<error>>& parser::errors() const noexcept