Split code generation from the ui
This commit is contained in:
parent
f84fd91426
commit
4ac29669ad
@ -8,23 +8,24 @@ set(CMAKE_CXX_STANDARD 17)
|
||||
find_package(Boost COMPONENTS program_options REQUIRED)
|
||||
include_directories(${Boost_INCLUDE_DIR})
|
||||
|
||||
add_executable(tester tests/runner.cpp include/elna/tester.hpp)
|
||||
add_executable(tester tests/tester.cpp include/elna/tester.hpp)
|
||||
target_include_directories(tester PRIVATE include)
|
||||
|
||||
add_executable(elnsh shell/main.cpp
|
||||
shell/interactive.cpp include/elna/interactive.hpp
|
||||
shell/history.cpp include/elna/history.hpp
|
||||
shell/state.cpp include/elna/state.hpp
|
||||
shell/interactive.cpp include/elna/shell/interactive.hpp
|
||||
shell/history.cpp include/elna/shell/history.hpp
|
||||
shell/state.cpp include/elna/shell/state.hpp
|
||||
)
|
||||
target_include_directories(elnsh PRIVATE include)
|
||||
|
||||
add_executable(elna source/main.cpp
|
||||
add_executable(elna cli/main.cpp
|
||||
source/lexer.cpp include/elna/source/lexer.hpp
|
||||
source/parser.cpp include/elna/source/parser.hpp
|
||||
source/result.cpp include/elna/result.hpp
|
||||
source/riscv.cpp include/elna/riscv.hpp
|
||||
source/ir.cpp include/elna/ir.hpp
|
||||
source/cl.cpp include/elna/cl.hpp
|
||||
source/result.cpp include/elna/source/result.hpp
|
||||
source/ir.cpp include/elna/source/ir.hpp
|
||||
backend/riscv.cpp include/elna/backend/riscv.hpp
|
||||
backend/target.cpp include/elna/backend/target.hpp
|
||||
cli/cl.cpp include/elna/cli/cl.hpp
|
||||
)
|
||||
target_include_directories(elna PRIVATE include)
|
||||
target_link_libraries(elna LINK_PUBLIC ${Boost_LIBRARIES})
|
||||
|
14
TODO
14
TODO
@ -4,14 +4,15 @@
|
||||
- Make writing ELF independent from the code generation.
|
||||
- Argument parsing.
|
||||
- Whitespaces are checked twice, in the source class and by lexing.
|
||||
- Replace pointer and length with vectors and strings.
|
||||
- Choose one name for runner.cpp and tester.hpp.
|
||||
- Move argument handling into the cl module.
|
||||
- Catch exceptions thrown by the argument parser and print them normally.
|
||||
- cmake clean target.
|
||||
- Division.
|
||||
- Expressions contain one or more terms. Currently it parses another expression on the right side.
|
||||
- Multiple factors.
|
||||
- Put riscv code into the backend namespace.
|
||||
- Rename riscv backend structs to be snake case.
|
||||
- Make reference and Target be nested types of the visitor.
|
||||
- Rename visitors to snake case.
|
||||
- The backend visitor seems to generate code before the prologue.
|
||||
|
||||
# Shell
|
||||
- Persist the history.
|
||||
@ -30,8 +31,3 @@
|
||||
|
||||
- Add a bar with additional information under the prompt (edit_bar), like the hostname.
|
||||
- Show current time in the prompt.
|
||||
|
||||
## Input
|
||||
|
||||
- DEL handling.
|
||||
- Starting long running process and killing it with Ctrl-C kills the shell.
|
||||
|
@ -1,8 +1,7 @@
|
||||
#include "elna/riscv.hpp"
|
||||
#include "elna/backend/riscv.hpp"
|
||||
#include <memory>
|
||||
#include <cstring>
|
||||
|
||||
namespace elna
|
||||
namespace elna::backend
|
||||
{
|
||||
Instruction::Instruction(BaseOpcode opcode)
|
||||
{
|
||||
@ -48,14 +47,14 @@ namespace elna
|
||||
return *this;
|
||||
}
|
||||
|
||||
const std::byte *Instruction::cbegin()
|
||||
const std::byte *Instruction::cbegin() const
|
||||
{
|
||||
return reinterpret_cast<std::byte *>(&this->instruction);
|
||||
return reinterpret_cast<const std::byte *>(&this->instruction);
|
||||
}
|
||||
|
||||
const std::byte *Instruction::cend()
|
||||
const std::byte *Instruction::cend() const
|
||||
{
|
||||
return reinterpret_cast<std::byte *>(&this->instruction) + sizeof(this->instruction);
|
||||
return reinterpret_cast<const std::byte *>(&this->instruction) + sizeof(this->instruction);
|
||||
}
|
||||
|
||||
void RiscVVisitor::visit(source::definition *definition)
|
||||
@ -69,62 +68,55 @@ namespace elna
|
||||
{
|
||||
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)));
|
||||
this->instructions.push_back(Instruction(BaseOpcode::opImm)
|
||||
.i(XRegister::sp, Funct3::addi, XRegister::sp, -stackSize));
|
||||
this->instructions.push_back(Instruction(BaseOpcode::store)
|
||||
.s(stackSize - 4, Funct3::sw, XRegister::sp, XRegister::s0));
|
||||
this->instructions.push_back(Instruction(BaseOpcode::store)
|
||||
.s(stackSize - 8, Funct3::sw, XRegister::sp, XRegister::ra));
|
||||
this->instructions.push_back(Instruction(BaseOpcode::opImm)
|
||||
.i(XRegister::s0, Funct3::addi, XRegister::sp, stackSize));
|
||||
|
||||
// Print the result.
|
||||
this->instructions[instructionsLength++] = Instruction(BaseOpcode::opImm)
|
||||
.i(XRegister::a1, Funct3::addi, XRegister::a0, 0);
|
||||
this->instructions.push_back(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].offset = instructions.size() * 4;
|
||||
this->references[0].target = Target::high20;
|
||||
this->instructions[instructionsLength++] = Instruction(BaseOpcode::lui).u(XRegister::a5, 0);
|
||||
this->instructions.push_back(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].offset = instructions.size() * 4;
|
||||
this->references[1].target = Target::lower12i;
|
||||
|
||||
this->instructions[instructionsLength++] = Instruction(BaseOpcode::opImm)
|
||||
.i(XRegister::a0, Funct3::addi, XRegister::a5, 0);
|
||||
this->instructions.push_back(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].offset = instructions.size() * 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);
|
||||
this->instructions.push_back(Instruction(BaseOpcode::auipc).u(XRegister::ra, 0));
|
||||
this->instructions.push_back(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);
|
||||
this->instructions.push_back(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);
|
||||
this->instructions.push_back(Instruction(BaseOpcode::load)
|
||||
.i(XRegister::s0, Funct3::lw, XRegister::sp, stackSize - 4));
|
||||
this->instructions.push_back(Instruction(BaseOpcode::load)
|
||||
.i(XRegister::ra, Funct3::lw, XRegister::sp, stackSize - 8));
|
||||
this->instructions.push_back(Instruction(BaseOpcode::opImm)
|
||||
.i(XRegister::sp, Funct3::addi, XRegister::sp, stackSize));
|
||||
this->instructions.push_back(Instruction(BaseOpcode::jalr)
|
||||
.i(XRegister::zero, Funct3::jalr, XRegister::ra, 0));
|
||||
}
|
||||
|
||||
void RiscVVisitor::visit(source::bang_statement *statement)
|
||||
@ -132,28 +124,24 @@ namespace elna
|
||||
statement->body().accept(this);
|
||||
}
|
||||
|
||||
void RiscVVisitor::visit(source::variable *variable)
|
||||
void RiscVVisitor::visit(source::variable_expression *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] =
|
||||
this->instructions.push_back(
|
||||
Instruction(BaseOpcode::opImm) // movl $x, %eax; where $x is a number.
|
||||
.i(freeRegister, Funct3::addi, XRegister::zero, constants[variable->name()]);
|
||||
.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] =
|
||||
this->instructions.push_back(
|
||||
Instruction(BaseOpcode::opImm) // movl $x, %eax; where $x is a number.
|
||||
.i(freeRegister, Funct3::addi, XRegister::zero, number->number());
|
||||
.i(freeRegister, Funct3::addi, XRegister::zero, number->number())
|
||||
);
|
||||
}
|
||||
|
||||
void RiscVVisitor::visit(source::binary_expression *expression)
|
||||
@ -163,59 +151,35 @@ namespace elna
|
||||
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.
|
||||
this->instructions.push_back( // 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);
|
||||
.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)
|
||||
this->instructions.push_back(Instruction(BaseOpcode::load)
|
||||
.i(XRegister::a0, Funct3::lw, XRegister::sp,
|
||||
static_cast<std::int8_t>((lhs_stack_position - 1) * 4));
|
||||
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);
|
||||
this->instructions.push_back(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);
|
||||
this->instructions.push_back(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);
|
||||
this->instructions.push_back(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;
|
||||
}
|
||||
}
|
@ -1,69 +1,30 @@
|
||||
#include "elna/cl.hpp"
|
||||
#include "elna/result.hpp"
|
||||
#include "elna/riscv.hpp"
|
||||
#include <cstddef>
|
||||
#include "elna/backend/target.hpp"
|
||||
#include "elna/backend/riscv.hpp"
|
||||
#include <elfio/elfio.hpp>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
namespace elna
|
||||
namespace elna::backend
|
||||
{
|
||||
char *readSource(const char *source)
|
||||
void riscv32_elf(source::block *ast, const std::filesystem::path& out_file)
|
||||
{
|
||||
const std::size_t bufferSize = 255;
|
||||
auto visitor = std::make_unique<RiscVVisitor>();
|
||||
visitor->visit(ast);
|
||||
|
||||
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;
|
||||
const ELFIO::Elf_Word instructions_size = visitor->instructions.size() * sizeof(Instruction);
|
||||
|
||||
// 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());
|
||||
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");
|
||||
@ -93,7 +54,7 @@ namespace elna
|
||||
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(),
|
||||
syma.add_symbol(stra, "main", 0x00000000, instructions_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);
|
||||
@ -110,16 +71,14 @@ namespace elna
|
||||
// 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 */);
|
||||
rela.add_entry(visitor->references[0].offset, label_sym, 26 /* ELFIO::R_RISCV_HI20 */);
|
||||
rela.add_entry(visitor->references[0].offset, label_sym, 51 /* ELFIO::R_RISCV_RELAX */);
|
||||
rela.add_entry(visitor->references[1].offset, label_sym, 27 /* ELFIO::R_RISCV_LO12_I */);
|
||||
rela.add_entry(visitor->references[1].offset, label_sym, 51 /* ELFIO::R_RISCV_RELAX */);
|
||||
rela.add_entry(visitor->references[2].offset, printf_sym, 18 /* ELFIO::R_RISCV_CALL */);
|
||||
rela.add_entry(visitor->references[2].offset, printf_sym, 51 /* ELFIO::R_RISCV_RELAX */);
|
||||
|
||||
// Create ELF object file
|
||||
writer.save(out_file);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
52
cli/cl.cpp
Normal file
52
cli/cl.cpp
Normal file
@ -0,0 +1,52 @@
|
||||
#include "elna/cli/cl.hpp"
|
||||
#include "elna/backend/target.hpp"
|
||||
#include <cstddef>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
namespace elna::cli
|
||||
{
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
backend::riscv32_elf(ast.get(), out_file);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
#include "elna/cl.hpp"
|
||||
#include "elna/cli/cl.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
@ -43,5 +43,5 @@ int main(int argc, char **argv)
|
||||
{
|
||||
out_file = in_file.filename().replace_extension(".o");
|
||||
}
|
||||
return elna::compile(in_file, out_file);
|
||||
return elna::cli::compile(in_file, out_file);
|
||||
}
|
@ -3,10 +3,23 @@
|
||||
#include <cstdint>
|
||||
#include <unordered_map>
|
||||
#include "elna/source/parser.hpp"
|
||||
#include "elna/result.hpp"
|
||||
|
||||
namespace elna
|
||||
namespace elna::backend
|
||||
{
|
||||
enum class Target
|
||||
{
|
||||
text,
|
||||
high20,
|
||||
lower12i
|
||||
};
|
||||
|
||||
struct Reference
|
||||
{
|
||||
const char* name;
|
||||
std::size_t offset;
|
||||
Target target;
|
||||
};
|
||||
|
||||
enum class XRegister : std::uint8_t
|
||||
{
|
||||
zero = 0,
|
||||
@ -135,8 +148,8 @@ namespace elna
|
||||
Instruction& r(XRegister rd, Funct3 funct3, XRegister rs1, XRegister rs2, Funct7 funct7 = Funct7::none);
|
||||
Instruction& u(XRegister rd, std::uint32_t imm);
|
||||
|
||||
const std::byte *cbegin();
|
||||
const std::byte *cend();
|
||||
const std::byte *cbegin() const;
|
||||
const std::byte *cend() const;
|
||||
|
||||
private:
|
||||
std::uint32_t instruction{ 0 };
|
||||
@ -145,8 +158,7 @@ namespace elna
|
||||
class RiscVVisitor : public source::ParserVisitor
|
||||
{
|
||||
public:
|
||||
Instruction *instructions;
|
||||
std::size_t instructionsLength;
|
||||
std::vector<Instruction> instructions;
|
||||
bool registerInUse{ true };
|
||||
std::uint32_t variableCounter = 1;
|
||||
Reference references[3];
|
||||
@ -155,10 +167,8 @@ namespace elna
|
||||
virtual void visit(source::definition *definition) override;
|
||||
virtual void visit(source::bang_statement *statement) override;
|
||||
virtual void visit(source::block *block) override;
|
||||
virtual void visit(source::variable *variable) override;
|
||||
virtual void visit(source::variable_expression *variable) override;
|
||||
virtual void visit(source::integer_literal *number) override;
|
||||
virtual void visit(source::binary_expression *expression) override;
|
||||
};
|
||||
|
||||
Symbol writeNext(source::block *ast);
|
||||
}
|
7
include/elna/backend/target.hpp
Normal file
7
include/elna/backend/target.hpp
Normal file
@ -0,0 +1,7 @@
|
||||
#include "elna/source/parser.hpp"
|
||||
#include <filesystem>
|
||||
|
||||
namespace elna::backend
|
||||
{
|
||||
void riscv32_elf(source::block *ast, const std::filesystem::path& out_file);
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
namespace elna
|
||||
namespace elna::cli
|
||||
{
|
||||
char *readSource(const char *source);
|
||||
int compile(const std::filesystem::path& in_file, const std::filesystem::path& out_file);
|
@ -1,112 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
#include <forward_list>
|
||||
|
||||
namespace elna
|
||||
{
|
||||
namespace source
|
||||
{
|
||||
/**
|
||||
* Position in the source text.
|
||||
*/
|
||||
struct position
|
||||
{
|
||||
/// Line.
|
||||
std::size_t line = 1;
|
||||
|
||||
/// Column.
|
||||
std::size_t column = 1;
|
||||
};
|
||||
|
||||
/**
|
||||
* A compilation error consists of an error message and position.
|
||||
*/
|
||||
struct error
|
||||
{
|
||||
private:
|
||||
char const *message;
|
||||
source::position position;
|
||||
|
||||
public:
|
||||
/**
|
||||
* \param message Error text.
|
||||
* \param position Error position in the source text.
|
||||
*/
|
||||
error(char const *message, const source::position position) noexcept;
|
||||
|
||||
/// Error text.
|
||||
const char *what() const noexcept;
|
||||
|
||||
/// Error line in the source text.
|
||||
std::size_t line() const noexcept;
|
||||
|
||||
/// Error column in the source text.
|
||||
std::size_t column() const noexcept;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct result
|
||||
{
|
||||
using E = std::forward_list<source::error>;
|
||||
|
||||
template<typename... Args>
|
||||
explicit result(Args&&... arguments)
|
||||
: payload(std::forward<Args>(arguments)...)
|
||||
{
|
||||
}
|
||||
|
||||
explicit result(const char *message, const source::position position)
|
||||
: payload(E{ source::error(message, position) })
|
||||
{
|
||||
}
|
||||
|
||||
bool has_errors() const noexcept
|
||||
{
|
||||
return std::holds_alternative<E>(payload);
|
||||
}
|
||||
|
||||
bool is_success() const noexcept
|
||||
{
|
||||
return std::holds_alternative<T>(payload);
|
||||
}
|
||||
|
||||
T& success()
|
||||
{
|
||||
return std::get<T>(payload);
|
||||
}
|
||||
|
||||
E& errors()
|
||||
{
|
||||
return std::get<E>(payload);
|
||||
}
|
||||
|
||||
private:
|
||||
std::variant<T, E> payload;
|
||||
};
|
||||
}
|
||||
|
||||
enum class Target
|
||||
{
|
||||
text,
|
||||
high20,
|
||||
lower12i
|
||||
};
|
||||
|
||||
struct Reference
|
||||
{
|
||||
const char* name;
|
||||
size_t offset;
|
||||
Target target;
|
||||
};
|
||||
|
||||
struct Symbol
|
||||
{
|
||||
Symbol(const char *name);
|
||||
const char *name;
|
||||
std::vector<std::byte> text;
|
||||
Reference symbols[3];
|
||||
};
|
||||
}
|
@ -5,8 +5,8 @@
|
||||
#include <string>
|
||||
#include <optional>
|
||||
|
||||
#include "elna/state.hpp"
|
||||
#include "elna/history.hpp"
|
||||
#include "elna/shell/state.hpp"
|
||||
#include "elna/shell/history.hpp"
|
||||
|
||||
#define BOOST_PROCESS_USE_STD_FS
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
#include "elna/source/parser.hpp"
|
||||
|
||||
namespace elna
|
||||
namespace elna::source
|
||||
{
|
||||
class TransformVisitor final : public source::ParserVisitor
|
||||
{
|
||||
@ -10,7 +10,7 @@ namespace elna
|
||||
void visit(source::bang_statement *statement) override;
|
||||
void visit(source::block *block) override;
|
||||
void visit(source::integer_literal *number) override;
|
||||
void visit(source::variable *variable) override;
|
||||
void visit(source::variable_expression *variable) override;
|
||||
void visit(source::binary_expression *binaryExpression) override;
|
||||
};
|
||||
}
|
@ -2,12 +2,10 @@
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "elna/source/result.hpp"
|
||||
|
||||
#include "elna/result.hpp"
|
||||
|
||||
namespace elna
|
||||
{
|
||||
namespace source
|
||||
namespace elna::source
|
||||
{
|
||||
/**
|
||||
* Range over the source text that keeps track of the current position.
|
||||
@ -113,4 +111,3 @@ namespace source
|
||||
*/
|
||||
elna::source::result<std::vector<token>> lex(const std::string& buffer);
|
||||
}
|
||||
}
|
||||
|
@ -4,9 +4,7 @@
|
||||
#include <memory>
|
||||
#include <elna/source/lexer.hpp>
|
||||
|
||||
namespace elna
|
||||
{
|
||||
namespace source
|
||||
namespace elna::source
|
||||
{
|
||||
enum class binary_operator
|
||||
{
|
||||
@ -20,7 +18,7 @@ namespace source
|
||||
class bang_statement;
|
||||
class block;
|
||||
class binary_expression;
|
||||
class variable;
|
||||
class variable_expression;
|
||||
class integer_literal;
|
||||
|
||||
struct ParserVisitor
|
||||
@ -29,7 +27,7 @@ namespace source
|
||||
virtual void visit(bang_statement *) = 0;
|
||||
virtual void visit(block *) = 0;
|
||||
virtual void visit(binary_expression *) = 0;
|
||||
virtual void visit(variable *) = 0;
|
||||
virtual void visit(variable_expression *) = 0;
|
||||
virtual void visit(integer_literal *) = 0;
|
||||
};
|
||||
|
||||
@ -104,12 +102,12 @@ namespace source
|
||||
std::int32_t number() const noexcept;
|
||||
};
|
||||
|
||||
class variable : public expression
|
||||
class variable_expression : public expression
|
||||
{
|
||||
std::string m_name;
|
||||
|
||||
public:
|
||||
variable(const std::string& name);
|
||||
variable_expression(const std::string& name);
|
||||
virtual void accept(ParserVisitor *visitor) override;
|
||||
|
||||
const std::string& name() const noexcept;
|
||||
@ -150,4 +148,3 @@ namespace source
|
||||
std::vector<token>::const_iterator end;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
86
include/elna/source/result.hpp
Normal file
86
include/elna/source/result.hpp
Normal file
@ -0,0 +1,86 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <variant>
|
||||
#include <forward_list>
|
||||
|
||||
namespace elna::source
|
||||
{
|
||||
/**
|
||||
* Position in the source text.
|
||||
*/
|
||||
struct position
|
||||
{
|
||||
/// Line.
|
||||
std::size_t line = 1;
|
||||
|
||||
/// Column.
|
||||
std::size_t column = 1;
|
||||
};
|
||||
|
||||
/**
|
||||
* A compilation error consists of an error message and position.
|
||||
*/
|
||||
struct error
|
||||
{
|
||||
private:
|
||||
char const *message;
|
||||
source::position position;
|
||||
|
||||
public:
|
||||
/**
|
||||
* \param message Error text.
|
||||
* \param position Error position in the source text.
|
||||
*/
|
||||
error(char const *message, const source::position position) noexcept;
|
||||
|
||||
/// Error text.
|
||||
const char *what() const noexcept;
|
||||
|
||||
/// Error line in the source text.
|
||||
std::size_t line() const noexcept;
|
||||
|
||||
/// Error column in the source text.
|
||||
std::size_t column() const noexcept;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct result
|
||||
{
|
||||
using E = std::forward_list<source::error>;
|
||||
|
||||
template<typename... Args>
|
||||
explicit result(Args&&... arguments)
|
||||
: payload(std::forward<Args>(arguments)...)
|
||||
{
|
||||
}
|
||||
|
||||
explicit result(const char *message, const source::position position)
|
||||
: payload(E{ source::error(message, position) })
|
||||
{
|
||||
}
|
||||
|
||||
bool has_errors() const noexcept
|
||||
{
|
||||
return std::holds_alternative<E>(payload);
|
||||
}
|
||||
|
||||
bool is_success() const noexcept
|
||||
{
|
||||
return std::holds_alternative<T>(payload);
|
||||
}
|
||||
|
||||
T& success()
|
||||
{
|
||||
return std::get<T>(payload);
|
||||
}
|
||||
|
||||
E& errors()
|
||||
{
|
||||
return std::get<E>(payload);
|
||||
}
|
||||
|
||||
private:
|
||||
std::variant<T, E> payload;
|
||||
};
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
#include "elna/history.hpp"
|
||||
#include "elna/shell/history.hpp"
|
||||
|
||||
namespace elna
|
||||
{
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include "elna/interactive.hpp"
|
||||
#include "elna/shell/interactive.hpp"
|
||||
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/process.hpp>
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include <cstdlib>
|
||||
#include <unistd.h>
|
||||
#include "elna/interactive.hpp"
|
||||
#include "elna/shell/interactive.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include <algorithm>
|
||||
#include <boost/endian.hpp>
|
||||
|
||||
#include "elna/state.hpp"
|
||||
#include "elna/shell/state.hpp"
|
||||
|
||||
namespace elna
|
||||
{
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user