elna/source/riscv.cpp
2024-02-28 16:18:39 +01:00

192 lines
8.3 KiB
C++

#include "elna/parser.hpp"
#include "elna/riscv.hpp"
#include <type_traits>
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;
}
std::uint8_t *Instruction::encode()
{
return reinterpret_cast<std::uint8_t *>(&this->instruction);
}
void RiscVVisitor::visit(ir::Node *)
{
}
void RiscVVisitor::visit(ir::Definition *definition)
{
const uint stackSize = static_cast<std::uint32_t>(definition->statementsLength * 4 + 12);
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);
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;
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(ir::Operand *operand)
{
if (dynamic_cast<ir::Variable *>(operand) != nullptr)
{
return dynamic_cast<ir::Variable *>(operand)->accept(this);
}
if (dynamic_cast<ir::Number *>(operand) != nullptr)
{
return dynamic_cast<ir::Number *>(operand)->accept(this);
}
}
void RiscVVisitor::visit(ir::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)));
// 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));
}
void RiscVVisitor::visit(ir::Number *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->value);
}
void RiscVVisitor::visit(ir::BinaryExpression *expression)
{
this->registerInUse = true;
expression->lhs->accept(this);
this->registerInUse = false;
expression->rhs->accept(this);
this->instructionsLength += 2;
this->instructions = reinterpret_cast<Instruction *>(
realloc(this->instructions, this->instructionsLength * sizeof(Instruction)));
// 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);
break;
case BinaryOperator::subtraction:
this->instructions[instructionsLength - 2] = Instruction(BaseOpcode::op)
.r(XRegister::a0, 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;
}
}