Remove the IR for know for simplicity

This commit is contained in:
2024-03-01 10:13:55 +01:00
parent f37700a02d
commit 3a6d89767b
36 changed files with 7547 additions and 1079 deletions

View File

@@ -1,5 +1,6 @@
#include "elna/parser.hpp"
#include "elna/riscv.hpp"
#include <memory>
#include <type_traits>
namespace elna
@@ -53,35 +54,43 @@ namespace elna
return reinterpret_cast<std::uint8_t *>(&this->instruction);
}
void RiscVVisitor::visit(ir::Node *)
void RiscVVisitor::visit(Node *)
{
}
void RiscVVisitor::visit(ir::Definition *definition)
void RiscVVisitor::visit(Definition *definition)
{
const uint stackSize = static_cast<std::uint32_t>(definition->statementsLength * 4 + 12);
++constCount;
constNames = reinterpret_cast<const char **>(realloc(constNames, sizeof(const char *) * constCount));
constValues = reinterpret_cast<std::int32_t *>(realloc(constValues, sizeof(std::int32_t) * constCount));
constNames[constCount - 1] = definition->identifier;
constValues[constCount - 1] = definition->number->value;
}
void RiscVVisitor::visit(Block *block)
{
for (std::size_t i = 0; i < block->definitionsLength; ++i)
{
block->definitions[i]->accept(this);
}
this->instructionsLength += 4;
this->instructions = reinterpret_cast<Instruction *>(
realloc(this->instructions, this->instructionsLength * sizeof(Instruction)));
// Prologue.
this->instructions[instructionsLength - 4] = Instruction(BaseOpcode::opImm)
.i(XRegister::sp, Funct3::addi, XRegister::sp, -stackSize);
this->instructions[instructionsLength - 3] = Instruction(BaseOpcode::store)
.s(stackSize - 4, Funct3::sw, XRegister::sp, XRegister::s0);
this->instructions[instructionsLength - 2] = Instruction(BaseOpcode::store)
.s(stackSize - 8, Funct3::sw, XRegister::sp, XRegister::ra);
this->instructions[instructionsLength - 1] = Instruction(BaseOpcode::opImm)
.i(XRegister::s0, Funct3::addi, XRegister::sp, stackSize);
block->statement->accept(this);
for (std::size_t i = 0; i < definition->statementsLength; ++i)
{
definition->statements[i]->accept(this);
}
this->registerInUse = true;
definition->result->accept(this);
this->registerInUse = false;
// 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)));
@@ -123,32 +132,44 @@ namespace elna
.i(XRegister::zero, Funct3::jalr, XRegister::ra, 0);
}
void RiscVVisitor::visit(ir::Operand *operand)
void RiscVVisitor::visit(BangStatement *statement)
{
if (dynamic_cast<ir::Variable *>(operand) != nullptr)
statement->expression->accept(this);
}
void RiscVVisitor::visit(Expression *operand)
{
if (dynamic_cast<Variable *>(operand) != nullptr)
{
return dynamic_cast<ir::Variable *>(operand)->accept(this);
return dynamic_cast<Variable *>(operand)->accept(this);
}
if (dynamic_cast<ir::Number *>(operand) != nullptr)
if (dynamic_cast<Number *>(operand) != nullptr)
{
return dynamic_cast<ir::Number *>(operand)->accept(this);
return dynamic_cast<Number *>(operand)->accept(this);
}
}
void RiscVVisitor::visit(ir::Variable *variable)
void RiscVVisitor::visit(Variable *variable)
{
std::size_t i = 0;
for (; i < constCount; ++i)
{
if (strcmp(variable->identifier, constNames[i]) == 0)
{
break;
}
}
const auto freeRegister = this->registerInUse ? XRegister::a0 : XRegister::t0;
++this->instructionsLength;
this->instructions = reinterpret_cast<Instruction *>(
realloc(this->instructions, this->instructionsLength * sizeof(Instruction)));
// movl -x(%rbp), %eax; where x is a number.
this->instructions[instructionsLength - 1] = Instruction(BaseOpcode::load)
.i(freeRegister, Funct3::lw, XRegister::sp,
static_cast<std::int8_t>(variable->counter * 4));
this->instructions[this->instructionsLength - 1] =
Instruction(BaseOpcode::opImm) // movl $x, %eax; where $x is a number.
.i(freeRegister, Funct3::addi, XRegister::zero, constValues[i]);
}
void RiscVVisitor::visit(ir::Number *number)
void RiscVVisitor::visit(Number *number)
{
const auto freeRegister = this->registerInUse ? XRegister::a0 : XRegister::t0;
@@ -160,32 +181,63 @@ namespace elna
.i(freeRegister, Funct3::addi, XRegister::zero, number->value);
}
void RiscVVisitor::visit(ir::BinaryExpression *expression)
void RiscVVisitor::visit(BinaryExpression *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->_operator)
{
case BinaryOperator::sum:
this->instructions[instructionsLength - 2] = Instruction(BaseOpcode::op)
.r(XRegister::a0, Funct3::add, XRegister::a0, XRegister::t0);
this->instructions[instructionsLength - 1] = Instruction(BaseOpcode::op)
.r(lhs_register, Funct3::add, XRegister::a0, XRegister::t0);
break;
case BinaryOperator::subtraction:
this->instructions[instructionsLength - 2] = Instruction(BaseOpcode::op)
.r(XRegister::a0, Funct3::sub, XRegister::a0, XRegister::t0, Funct7::sub);
this->instructions[instructionsLength - 1] = Instruction(BaseOpcode::op)
.r(lhs_register, Funct3::sub, XRegister::a0, XRegister::t0, Funct7::sub);
break;
}
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);
}
++this->variableCounter;
Symbol writeNext(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];
}
program.text = reinterpret_cast<unsigned char *>(malloc(sizeof(std::uint32_t) * visitor->instructionsLength));
for (std::size_t i = 0; i < visitor->instructionsLength; ++i)
{
memcpy(program.text + program.length, visitor->instructions[i].encode(), sizeof(std::uint32_t));
program.length += sizeof(std::uint32_t);
}
return program;
}
}