elna/source/riscv.cpp

252 lines
10 KiB
C++
Raw Normal View History

2024-02-28 16:18:39 +01:00
#include "elna/parser.hpp"
#include "elna/riscv.hpp"
2024-03-01 10:13:55 +01:00
#include <memory>
2024-03-03 13:11:39 +01:00
#include <cstring>
2024-02-28 16:18:39 +01:00
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;
}
2024-03-03 13:11:39 +01:00
const std::byte *Instruction::cbegin()
2024-02-28 16:18:39 +01:00
{
2024-03-03 13:11:39 +01:00
return reinterpret_cast<std::byte *>(&this->instruction);
}
const std::byte *Instruction::cend()
{
return reinterpret_cast<std::byte *>(&this->instruction) + sizeof(this->instruction);
2024-02-28 16:18:39 +01:00
}
2024-03-01 10:13:55 +01:00
void RiscVVisitor::visit(Node *)
2024-02-28 16:18:39 +01:00
{
}
2024-03-01 10:13:55 +01:00
void RiscVVisitor::visit(Definition *definition)
2024-02-28 16:18:39 +01:00
{
2024-03-01 10:13:55 +01:00
++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));
2024-02-28 16:18:39 +01:00
2024-03-01 10:13:55 +01:00
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);
}
2024-02-28 16:18:39 +01:00
this->instructionsLength += 4;
this->instructions = reinterpret_cast<Instruction *>(
realloc(this->instructions, this->instructionsLength * sizeof(Instruction)));
2024-03-01 10:13:55 +01:00
block->statement->accept(this);
2024-02-28 16:18:39 +01:00
// Prologue.
2024-03-01 10:13:55 +01:00
const uint stackSize = static_cast<std::uint32_t>(variableCounter * 4 + 12);
this->instructions[0] = Instruction(BaseOpcode::opImm)
2024-02-28 16:18:39 +01:00
.i(XRegister::sp, Funct3::addi, XRegister::sp, -stackSize);
2024-03-01 10:13:55 +01:00
this->instructions[1] = Instruction(BaseOpcode::store)
2024-02-28 16:18:39 +01:00
.s(stackSize - 4, Funct3::sw, XRegister::sp, XRegister::s0);
2024-03-01 10:13:55 +01:00
this->instructions[2] = Instruction(BaseOpcode::store)
2024-02-28 16:18:39 +01:00
.s(stackSize - 8, Funct3::sw, XRegister::sp, XRegister::ra);
2024-03-01 10:13:55 +01:00
this->instructions[3] = Instruction(BaseOpcode::opImm)
2024-02-28 16:18:39 +01:00
.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);
}
2024-03-01 10:13:55 +01:00
void RiscVVisitor::visit(BangStatement *statement)
2024-02-28 16:18:39 +01:00
{
2024-03-01 10:13:55 +01:00
statement->expression->accept(this);
}
void RiscVVisitor::visit(Expression *operand)
{
if (dynamic_cast<Variable *>(operand) != nullptr)
2024-02-28 16:18:39 +01:00
{
2024-03-01 10:13:55 +01:00
return dynamic_cast<Variable *>(operand)->accept(this);
2024-02-28 16:18:39 +01:00
}
2024-03-01 10:13:55 +01:00
if (dynamic_cast<Number *>(operand) != nullptr)
2024-02-28 16:18:39 +01:00
{
2024-03-01 10:13:55 +01:00
return dynamic_cast<Number *>(operand)->accept(this);
2024-02-28 16:18:39 +01:00
}
}
2024-03-01 10:13:55 +01:00
void RiscVVisitor::visit(Variable *variable)
2024-02-28 16:18:39 +01:00
{
2024-03-01 10:13:55 +01:00
std::size_t i = 0;
for (; i < constCount; ++i)
{
2024-03-03 13:11:39 +01:00
if (std::strcmp(variable->identifier, constNames[i]) == 0)
2024-03-01 10:13:55 +01:00
{
break;
}
}
2024-02-28 16:18:39 +01:00
const auto freeRegister = this->registerInUse ? XRegister::a0 : XRegister::t0;
++this->instructionsLength;
this->instructions = reinterpret_cast<Instruction *>(
realloc(this->instructions, this->instructionsLength * sizeof(Instruction)));
2024-03-01 10:13:55 +01:00
this->instructions[this->instructionsLength - 1] =
Instruction(BaseOpcode::opImm) // movl $x, %eax; where $x is a number.
.i(freeRegister, Funct3::addi, XRegister::zero, constValues[i]);
2024-02-28 16:18:39 +01:00
}
2024-03-01 10:13:55 +01:00
void RiscVVisitor::visit(Number *number)
2024-02-28 16:18:39 +01:00
{
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->value);
}
2024-03-01 10:13:55 +01:00
void RiscVVisitor::visit(BinaryExpression *expression)
2024-02-28 16:18:39 +01:00
{
2024-03-01 10:13:55 +01:00
const auto lhs_register = this->registerInUse ? XRegister::a0 : XRegister::t0;
2024-02-28 16:18:39 +01:00
this->registerInUse = true;
expression->lhs->accept(this);
2024-03-01 10:13:55 +01:00
++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;
2024-02-28 16:18:39 +01:00
this->registerInUse = false;
expression->rhs->accept(this);
this->instructionsLength += 2;
this->instructions = reinterpret_cast<Instruction *>(
realloc(this->instructions, this->instructionsLength * sizeof(Instruction)));
2024-03-01 10:13:55 +01:00
this->instructions[instructionsLength - 2] = Instruction(BaseOpcode::load)
.i(XRegister::a0, Funct3::lw, XRegister::sp,
static_cast<std::int8_t>((lhs_stack_position - 1) * 4));
2024-02-28 16:18:39 +01:00
// Calculate the result and assign it to a variable on the stack.
switch (expression->_operator)
{
case BinaryOperator::sum:
2024-03-01 10:13:55 +01:00
this->instructions[instructionsLength - 1] = Instruction(BaseOpcode::op)
.r(lhs_register, Funct3::add, XRegister::a0, XRegister::t0);
2024-02-28 16:18:39 +01:00
break;
case BinaryOperator::subtraction:
2024-03-01 10:13:55 +01:00
this->instructions[instructionsLength - 1] = Instruction(BaseOpcode::op)
.r(lhs_register, Funct3::sub, XRegister::a0, XRegister::t0, Funct7::sub);
2024-02-28 16:18:39 +01:00
break;
2024-03-03 13:11:39 +01:00
case BinaryOperator::multiplication:
this->instructions[instructionsLength - 1] = Instruction(BaseOpcode::op)
.r(lhs_register, Funct3::mul, XRegister::a0, XRegister::t0, Funct7::muldiv);
break;
2024-02-28 16:18:39 +01:00
}
2024-03-01 10:13:55 +01:00
}
Symbol writeNext(Block *ast)
{
auto visitor = std::make_unique<RiscVVisitor>();
visitor->visit(ast);
2024-02-28 16:18:39 +01:00
2024-03-01 10:13:55 +01:00
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)
{
2024-03-03 13:11:39 +01:00
program.text.insert(program.text.cend(),
visitor->instructions[i].cbegin(), visitor->instructions[i].cend());
2024-03-01 10:13:55 +01:00
}
return program;
2024-02-28 16:18:39 +01:00
}
}