Split code generation from the ui

This commit is contained in:
2024-03-07 09:15:11 +01:00
parent f84fd91426
commit 4ac29669ad
26 changed files with 279 additions and 336 deletions

View File

@ -1,125 +0,0 @@
#include "elna/cl.hpp"
#include "elna/result.hpp"
#include "elna/riscv.hpp"
#include <cstddef>
#include <elfio/elfio.hpp>
#include <fstream>
#include <sstream>
namespace elna
{
char *readSource(const char *source)
{
const std::size_t bufferSize = 255;
std::ifstream input_stream{ source };
std::stringstream buffer;
buffer << input_stream.rdbuf();
input_stream.close();
std::string contents = buffer.str();
char *result = reinterpret_cast<char *>(malloc(contents.size() + 1));
std::copy(std::cbegin(contents), std::cend(contents), result);
result[contents.size()] = '\0';
return result;
}
int compile(const std::filesystem::path& in_file, const std::filesystem::path& out_file)
{
ELFIO::elfio writer;
// You can't proceed before this function call!
writer.create(ELFIO::ELFCLASS32, ELFIO::ELFDATA2LSB);
writer.set_os_abi(ELFIO::ELFOSABI_NONE);
writer.set_type(ELFIO::ET_REL);
writer.set_machine(ELFIO::EM_RISCV);
auto sourceText = readSource(in_file.c_str());
if (sourceText == nullptr)
{
return 3;
}
size_t tokensCount{ 0 };
auto lex_result = source::lex(sourceText);
free(sourceText);
if (lex_result.has_errors())
{
for (const auto& compile_error : lex_result.errors())
{
printf("%lu:%lu: %s\n", compile_error.line(), compile_error.column(), compile_error.what());
}
return 1;
}
auto ast = source::parser(lex_result.success()).parse();
if (ast == nullptr)
{
return 2;
}
auto program = writeNext(ast.get());
// Create code section
ELFIO::section* text_sec = writer.sections.add(".text");
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 *>(program.text.data()), program.text.size());
// Create string table section
ELFIO::section* str_sec = writer.sections.add(".strtab");
str_sec->set_type(ELFIO::SHT_STRTAB);
// Create string table writer
ELFIO::string_section_accessor stra(str_sec);
// Add label name
ELFIO::Elf32_Word str_index = stra.add_string("msg");
// Create read only data section
ELFIO::section* ro_sec = writer.sections.add(".rodata");
ro_sec->set_type(ELFIO::SHT_PROGBITS);
ro_sec->set_flags(ELFIO::SHF_ALLOC);
ro_sec->set_addr_align(0x4);
ro_sec->set_data("%d\n");
// Create symbol table section
ELFIO::section* sym_sec = writer.sections.add(".symtab");
sym_sec->set_type(ELFIO::SHT_SYMTAB);
sym_sec->set_info(2);
sym_sec->set_addr_align(0x4);
sym_sec->set_entry_size(writer.get_default_entry_size(ELFIO::SHT_SYMTAB));
sym_sec->set_link(str_sec->get_index());
// Create symbol table writer
ELFIO::symbol_section_accessor syma(writer, sym_sec);
auto label_sym = syma.add_symbol(stra, ".CL0", 0x00000000, strlen("%d\n") + 1,
ELFIO::STB_LOCAL, ELFIO::STT_NOTYPE, 0, ro_sec->get_index());
syma.add_symbol(stra, program.name, 0x00000000, program.text.size(),
ELFIO::STB_GLOBAL, ELFIO::STT_FUNC, 0, text_sec->get_index());
auto printf_sym = syma.add_symbol(stra, "printf", 0x00000000, 0,
ELFIO::STB_GLOBAL, ELFIO::STT_NOTYPE, 0, ELFIO::SHN_UNDEF);
// Create relocation table section
ELFIO::section* rel_sec = writer.sections.add(".rel.text");
rel_sec->set_type(ELFIO::SHT_REL);
rel_sec->set_info(text_sec->get_index());
rel_sec->set_addr_align(0x4);
rel_sec->set_entry_size(writer.get_default_entry_size(ELFIO::SHT_REL));
rel_sec->set_link(sym_sec->get_index());
rel_sec->set_flags(ELFIO::SHF_ALLOC);
// Create relocation table writer
ELFIO::relocation_section_accessor rela(writer, rel_sec);
// Add relocation entry (adjust address at offset 11)
rela.add_entry(program.symbols[0].offset, label_sym, 26 /* ELFIO::R_RISCV_HI20 */);
rela.add_entry(program.symbols[0].offset, label_sym, 51 /* ELFIO::R_RISCV_RELAX */);
rela.add_entry(program.symbols[1].offset, label_sym, 27 /* ELFIO::R_RISCV_LO12_I */);
rela.add_entry(program.symbols[1].offset, label_sym, 51 /* ELFIO::R_RISCV_RELAX */);
rela.add_entry(program.symbols[2].offset, printf_sym, 18 /* ELFIO::R_RISCV_CALL */);
rela.add_entry(program.symbols[2].offset, printf_sym, 51 /* ELFIO::R_RISCV_RELAX */);
// Create ELF object file
writer.save(out_file);
return 0;
}
}

View File

@ -1,8 +1,8 @@
#include "elna/ir.hpp"
#include "elna/source/ir.hpp"
#include <cassert>
namespace elna
namespace elna::source
{
void TransformVisitor::visit(source::definition *definition)
{
@ -24,7 +24,7 @@ namespace elna
assert(false);
}
void TransformVisitor::visit(source::variable *variable)
void TransformVisitor::visit(source::variable_expression *variable)
{
assert(false);
}

View File

@ -1,9 +1,7 @@
#include "elna/source/lexer.hpp"
#include <variant>
namespace elna
{
namespace source
namespace elna::source
{
using source_position = elna::source::position;
using source_error = elna::source::error;
@ -287,4 +285,3 @@ namespace source
return source_result(tokens);
}
}
}

View File

@ -1,47 +0,0 @@
#include "elna/cl.hpp"
#include <filesystem>
#include <iostream>
#include <boost/program_options.hpp>
int main(int argc, char **argv)
{
boost::program_options::options_description visible{ "Allowed options" };
boost::program_options::options_description description;
boost::program_options::positional_options_description positional;
boost::program_options::variables_map variables_map;
visible.add_options()
("help", "Print this help message")
("output,o", boost::program_options::value<std::filesystem::path>(), "Output object file")
;
description.add_options()
("input", boost::program_options::value<std::filesystem::path>()->required())
;
description.add(visible);
positional.add("input", 1);
auto parsed = boost::program_options::command_line_parser(argc, argv)
.options(description).positional(positional)
.run();
boost::program_options::store(parsed, variables_map);
boost::program_options::notify(variables_map);
if (variables_map.count("help"))
{
std::cout << description << std::endl;
return 0;
}
const auto in_file = variables_map["input"].as<std::filesystem::path>();
std::filesystem::path out_file;
if (variables_map.count("output"))
{
out_file = variables_map["output"].as<std::filesystem::path>();
}
else
{
out_file = in_file.filename().replace_extension(".o");
}
return elna::compile(in_file, out_file);
}

View File

@ -1,9 +1,7 @@
#include "elna/source/parser.hpp"
#include <stdexcept>
namespace elna
{
namespace source
namespace elna::source
{
/**
* AST node.
@ -67,17 +65,17 @@ namespace source
return m_number;
}
variable::variable(const std::string& name)
variable_expression::variable_expression(const std::string& name)
: m_name(name)
{
}
void variable::accept(ParserVisitor *visitor)
void variable_expression::accept(ParserVisitor *visitor)
{
visitor->visit(this);
}
const std::string& variable::name() const noexcept
const std::string& variable_expression::name() const noexcept
{
return m_name;
}
@ -154,7 +152,7 @@ namespace source
{
if (tokens->of() == source::token::type::identifier)
{
auto result = std::make_unique<variable>(tokens->identifier());
auto result = std::make_unique<variable_expression>(tokens->identifier());
++tokens;
return result;
}
@ -291,4 +289,3 @@ namespace source
return std::make_unique<block>(std::move(definitions), std::move(parsed_statement));
}
}
}

View File

@ -1,8 +1,6 @@
#include "elna/result.hpp"
#include "elna/source/result.hpp"
namespace elna
{
namespace source
namespace elna::source
{
error::error(const char *message, const source::position position) noexcept
{
@ -25,9 +23,3 @@ namespace source
return this->position.column;
}
}
Symbol::Symbol(const char *name)
{
this->name = name;
}
}

View File

@ -1,221 +0,0 @@
#include "elna/riscv.hpp"
#include <memory>
#include <cstring>
namespace elna
{
Instruction::Instruction(BaseOpcode opcode)
{
this->instruction = static_cast<std::underlying_type<BaseOpcode>::type>(opcode);
}
Instruction& Instruction::i(XRegister rd, Funct3 funct3, XRegister rs1, std::uint32_t immediate)
{
this->instruction |= (static_cast<std::underlying_type<XRegister>::type>(rd) << 7)
| (static_cast<std::underlying_type<Funct3>::type>(funct3) << 12)
| (static_cast<std::underlying_type<XRegister>::type>(rs1) << 15)
| (immediate << 20);
return *this;
}
Instruction& Instruction::s(std::uint32_t imm1, Funct3 funct3, XRegister rs1, XRegister rs2)
{
this->instruction |= ((imm1 & 0b11111) << 7)
| (static_cast<std::underlying_type<Funct3>::type>(funct3) << 12)
| (static_cast<std::underlying_type<XRegister>::type>(rs1) << 15)
| (static_cast<std::underlying_type<XRegister>::type>(rs2) << 20)
| ((imm1 & 0b111111100000) << 20);
return *this;
}
Instruction& Instruction::r(XRegister rd, Funct3 funct3, XRegister rs1, XRegister rs2, Funct7 funct7)
{
this->instruction |= (static_cast<std::underlying_type<XRegister>::type>(rd) << 7)
| (static_cast<std::underlying_type<Funct3>::type>(funct3) << 12)
| (static_cast<std::underlying_type<XRegister>::type>(rs1) << 15)
| (static_cast<std::underlying_type<XRegister>::type>(rs2) << 20)
| (static_cast<std::underlying_type<Funct7>::type>(funct7) << 25);
return *this;
}
Instruction& Instruction::u(XRegister rd, std::uint32_t imm)
{
this->instruction |= (static_cast<std::underlying_type<XRegister>::type>(rd) << 7) | (imm << 12);
return *this;
}
const std::byte *Instruction::cbegin()
{
return reinterpret_cast<std::byte *>(&this->instruction);
}
const std::byte *Instruction::cend()
{
return reinterpret_cast<std::byte *>(&this->instruction) + sizeof(this->instruction);
}
void RiscVVisitor::visit(source::definition *definition)
{
constants[definition->identifier()] = definition->body().number();
}
void RiscVVisitor::visit(source::block *block)
{
for (const auto& block_definition : block->definitions())
{
block_definition->accept(this);
}
this->instructionsLength += 4;
this->instructions = reinterpret_cast<Instruction *>(
realloc(this->instructions, this->instructionsLength * sizeof(Instruction)));
block->body().accept(this);
// Prologue.
const uint stackSize = static_cast<std::uint32_t>(variableCounter * 4 + 12);
this->instructions[0] = Instruction(BaseOpcode::opImm)
.i(XRegister::sp, Funct3::addi, XRegister::sp, -stackSize);
this->instructions[1] = Instruction(BaseOpcode::store)
.s(stackSize - 4, Funct3::sw, XRegister::sp, XRegister::s0);
this->instructions[2] = Instruction(BaseOpcode::store)
.s(stackSize - 8, Funct3::sw, XRegister::sp, XRegister::ra);
this->instructions[3] = Instruction(BaseOpcode::opImm)
.i(XRegister::s0, Funct3::addi, XRegister::sp, stackSize);
this->instructions = reinterpret_cast<Instruction*>(
realloc(this->instructions, (this->instructionsLength + 10) * sizeof(Instruction)));
// Print the result.
this->instructions[instructionsLength++] = Instruction(BaseOpcode::opImm)
.i(XRegister::a1, Funct3::addi, XRegister::a0, 0);
this->references[0] = Reference();
this->references[0].name = ".CL0";
this->references[0].offset = instructionsLength * 4;
this->references[0].target = Target::high20;
this->instructions[instructionsLength++] = Instruction(BaseOpcode::lui).u(XRegister::a5, 0);
this->references[1] = Reference();
this->references[1].name = ".CL0";
this->references[1].offset = instructionsLength * 4;
this->references[1].target = Target::lower12i;
this->instructions[instructionsLength++] = Instruction(BaseOpcode::opImm)
.i(XRegister::a0, Funct3::addi, XRegister::a5, 0);
this->references[2] = Reference();
this->references[2].name = "printf";
this->references[2].offset = instructionsLength * 4;
this->references[2].target = Target::text;
this->instructions[instructionsLength++] = Instruction(BaseOpcode::auipc).u(XRegister::ra, 0);
this->instructions[instructionsLength++] = Instruction(BaseOpcode::jalr)
.i(XRegister::ra, Funct3::jalr, XRegister::ra, 0);
// Set the return value (0).
this->instructions[instructionsLength++] = Instruction(BaseOpcode::op)
.r(XRegister::a0, Funct3::_and, XRegister::zero, XRegister::zero);
// Epilogue.
this->instructions[instructionsLength++] = Instruction(BaseOpcode::load)
.i(XRegister::s0, Funct3::lw, XRegister::sp, stackSize - 4);
this->instructions[instructionsLength++] = Instruction(BaseOpcode::load)
.i(XRegister::ra, Funct3::lw, XRegister::sp, stackSize - 8);
this->instructions[instructionsLength++] = Instruction(BaseOpcode::opImm)
.i(XRegister::sp, Funct3::addi, XRegister::sp, stackSize);
this->instructions[instructionsLength++] = Instruction(BaseOpcode::jalr)
.i(XRegister::zero, Funct3::jalr, XRegister::ra, 0);
}
void RiscVVisitor::visit(source::bang_statement *statement)
{
statement->body().accept(this);
}
void RiscVVisitor::visit(source::variable *variable)
{
const auto freeRegister = this->registerInUse ? XRegister::a0 : XRegister::t0;
++this->instructionsLength;
this->instructions = reinterpret_cast<Instruction *>(
realloc(this->instructions, this->instructionsLength * sizeof(Instruction)));
this->instructions[this->instructionsLength - 1] =
Instruction(BaseOpcode::opImm) // movl $x, %eax; where $x is a number.
.i(freeRegister, Funct3::addi, XRegister::zero, constants[variable->name()]);
}
void RiscVVisitor::visit(source::integer_literal *number)
{
const auto freeRegister = this->registerInUse ? XRegister::a0 : XRegister::t0;
++this->instructionsLength;
this->instructions = reinterpret_cast<Instruction *>(
realloc(this->instructions, this->instructionsLength * sizeof(Instruction)));
this->instructions[this->instructionsLength - 1] =
Instruction(BaseOpcode::opImm) // movl $x, %eax; where $x is a number.
.i(freeRegister, Funct3::addi, XRegister::zero, number->number());
}
void RiscVVisitor::visit(source::binary_expression *expression)
{
const auto lhs_register = this->registerInUse ? XRegister::a0 : XRegister::t0;
this->registerInUse = true;
expression->lhs().accept(this);
++this->instructionsLength;
this->instructions = reinterpret_cast<Instruction *>(
realloc(this->instructions, this->instructionsLength * sizeof(Instruction)));
this->instructions[instructionsLength - 1] = // movl %eax, -x(%rbp); where x is a number.
Instruction(BaseOpcode::store)
.s(static_cast<std::uint32_t>(this->variableCounter * 4), Funct3::sw, XRegister::sp, XRegister::a0);
auto lhs_stack_position = ++this->variableCounter;
this->registerInUse = false;
expression->rhs().accept(this);
this->instructionsLength += 2;
this->instructions = reinterpret_cast<Instruction *>(
realloc(this->instructions, this->instructionsLength * sizeof(Instruction)));
this->instructions[instructionsLength - 2] = Instruction(BaseOpcode::load)
.i(XRegister::a0, Funct3::lw, XRegister::sp,
static_cast<std::int8_t>((lhs_stack_position - 1) * 4));
// Calculate the result and assign it to a variable on the stack.
switch (expression->operation())
{
case source::binary_operator::sum:
this->instructions[instructionsLength - 1] = Instruction(BaseOpcode::op)
.r(lhs_register, Funct3::add, XRegister::a0, XRegister::t0);
break;
case source::binary_operator::subtraction:
this->instructions[instructionsLength - 1] = Instruction(BaseOpcode::op)
.r(lhs_register, Funct3::sub, XRegister::a0, XRegister::t0, Funct7::sub);
break;
case source::binary_operator::multiplication:
this->instructions[instructionsLength - 1] = Instruction(BaseOpcode::op)
.r(lhs_register, Funct3::mul, XRegister::a0, XRegister::t0, Funct7::muldiv);
break;
}
}
Symbol writeNext(source::block *ast)
{
auto visitor = std::make_unique<RiscVVisitor>();
visitor->visit(ast);
Symbol program{ "main" };
for (std::size_t i = 0; i < 3; ++i)
{
program.symbols[i] = visitor->references[i];
}
for (std::size_t i = 0; i < visitor->instructionsLength; ++i)
{
program.text.insert(program.text.cend(),
visitor->instructions[i].cbegin(), visitor->instructions[i].cend());
}
return program;
}
}