Write parts of generated code directly to objects
This commit is contained in:
@ -1,8 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <charconv>
|
||||
#include <cstdint>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include "elna/source/parser.hpp"
|
||||
|
||||
namespace elna::riscv
|
||||
@ -159,169 +158,20 @@ namespace elna::riscv
|
||||
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
|
||||
{
|
||||
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:
|
||||
std::vector<instruction> instructions;
|
||||
bool register_in_use{ true };
|
||||
std::uint32_t variable_counter = 1;
|
||||
std::vector<reference> references;
|
||||
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::definition *definition) override;
|
||||
@ -332,6 +182,7 @@ namespace elna::riscv
|
||||
virtual void visit(source::if_statement *statement) override;
|
||||
virtual void visit(source::while_statement *statement) 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::binary_expression *expression) override;
|
||||
virtual void visit(source::integer_literal *number) override;
|
||||
|
@ -1,7 +1,80 @@
|
||||
#include "elna/source/parser.hpp"
|
||||
#include <filesystem>
|
||||
#include <elfio/elfio.hpp>
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ namespace elna::source
|
||||
class if_statement;
|
||||
class while_statement;
|
||||
class block;
|
||||
class program;
|
||||
class binary_expression;
|
||||
class variable_expression;
|
||||
class integer_literal;
|
||||
@ -39,6 +40,7 @@ namespace elna::source
|
||||
virtual void visit(if_statement *) = 0;
|
||||
virtual void visit(while_statement *) = 0;
|
||||
virtual void visit(block *) = 0;
|
||||
virtual void visit(program *) = 0;
|
||||
virtual void visit(binary_expression *) = 0;
|
||||
virtual void visit(variable_expression *) = 0;
|
||||
virtual void visit(integer_literal *) = 0;
|
||||
@ -56,6 +58,7 @@ namespace elna::source
|
||||
virtual void visit(if_statement *) override;
|
||||
virtual void visit(while_statement *) override;
|
||||
virtual void visit(block *block) override;
|
||||
virtual void visit(program *program) override;
|
||||
virtual void visit(binary_expression *expression) override;
|
||||
virtual void visit(variable_expression *variable) override;
|
||||
virtual void visit(integer_literal *number) override;
|
||||
@ -186,9 +189,6 @@ namespace elna::source
|
||||
statement& body();
|
||||
};
|
||||
|
||||
/**
|
||||
* Block.
|
||||
*/
|
||||
class block : public node
|
||||
{
|
||||
std::unique_ptr<statement> m_body;
|
||||
@ -208,6 +208,15 @@ namespace elna::source
|
||||
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
|
||||
{
|
||||
std::int32_t m_number;
|
||||
@ -286,7 +295,7 @@ namespace elna::source
|
||||
*
|
||||
* \return Parsed program or nothing if an error occurred.
|
||||
*/
|
||||
std::unique_ptr<block> parse();
|
||||
std::unique_ptr<program> parse();
|
||||
|
||||
/**
|
||||
* Gets produced errors.
|
||||
|
Reference in New Issue
Block a user