elna/backend/riscv.cpp

186 lines
7.6 KiB
C++
Raw Normal View History

2024-03-07 09:15:11 +01:00
#include "elna/backend/riscv.hpp"
2024-03-01 10:13:55 +01:00
#include <memory>
2024-02-28 16:18:39 +01:00
2024-03-07 09:15:11 +01:00
namespace elna::backend
2024-02-28 16:18:39 +01:00
{
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-07 09:15:11 +01:00
const std::byte *Instruction::cbegin() const
2024-02-28 16:18:39 +01:00
{
2024-03-07 09:15:11 +01:00
return reinterpret_cast<const std::byte *>(&this->instruction);
2024-03-03 13:11:39 +01:00
}
2024-03-07 09:15:11 +01:00
const std::byte *Instruction::cend() const
2024-03-03 13:11:39 +01:00
{
2024-03-07 09:15:11 +01:00
return reinterpret_cast<const std::byte *>(&this->instruction) + sizeof(this->instruction);
2024-02-28 16:18:39 +01:00
}
void RiscVVisitor::visit(source::definition *definition)
2024-02-28 16:18:39 +01:00
{
constants[definition->identifier()] = definition->body().number();
2024-02-28 16:18:39 +01:00
}
void RiscVVisitor::visit(source::block *block)
2024-02-28 16:18:39 +01:00
{
for (const auto& block_definition : block->definitions())
2024-03-01 10:13:55 +01:00
{
block_definition->accept(this);
2024-03-01 10:13:55 +01:00
}
block->body().accept(this);
2024-03-01 10:13:55 +01:00
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);
2024-03-07 09:15:11 +01:00
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));
2024-02-28 16:18:39 +01:00
// Print the result.
2024-03-07 09:15:11 +01:00
this->instructions.push_back(Instruction(BaseOpcode::opImm)
.i(XRegister::a1, Funct3::addi, XRegister::a0, 0));
2024-02-28 16:18:39 +01:00
this->references[0] = Reference();
this->references[0].name = ".CL0";
2024-03-07 09:15:11 +01:00
this->references[0].offset = instructions.size() * 4;
2024-02-28 16:18:39 +01:00
this->references[0].target = Target::high20;
2024-03-07 09:15:11 +01:00
this->instructions.push_back(Instruction(BaseOpcode::lui).u(XRegister::a5, 0));
2024-02-28 16:18:39 +01:00
this->references[1] = Reference();
this->references[1].name = ".CL0";
2024-03-07 09:15:11 +01:00
this->references[1].offset = instructions.size() * 4;
2024-02-28 16:18:39 +01:00
this->references[1].target = Target::lower12i;
2024-03-07 09:15:11 +01:00
this->instructions.push_back(Instruction(BaseOpcode::opImm)
.i(XRegister::a0, Funct3::addi, XRegister::a5, 0));
2024-02-28 16:18:39 +01:00
this->references[2] = Reference();
this->references[2].name = "printf";
2024-03-07 09:15:11 +01:00
this->references[2].offset = instructions.size() * 4;
2024-02-28 16:18:39 +01:00
this->references[2].target = Target::text;
2024-03-07 09:15:11 +01:00
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));
2024-02-28 16:18:39 +01:00
// Set the return value (0).
2024-03-07 09:15:11 +01:00
this->instructions.push_back(Instruction(BaseOpcode::op)
.r(XRegister::a0, Funct3::_and, XRegister::zero, XRegister::zero));
2024-02-28 16:18:39 +01:00
// Epilogue.
2024-03-07 09:15:11 +01:00
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));
2024-02-28 16:18:39 +01:00
}
void RiscVVisitor::visit(source::bang_statement *statement)
2024-03-01 10:13:55 +01:00
{
statement->body().accept(this);
2024-02-28 16:18:39 +01:00
}
2024-03-07 09:15:11 +01:00
void RiscVVisitor::visit(source::variable_expression *variable)
2024-02-28 16:18:39 +01:00
{
const auto freeRegister = this->registerInUse ? XRegister::a0 : XRegister::t0;
2024-03-07 09:15:11 +01:00
this->instructions.push_back(
2024-03-01 10:13:55 +01:00
Instruction(BaseOpcode::opImm) // movl $x, %eax; where $x is a number.
2024-03-07 09:15:11 +01:00
.i(freeRegister, Funct3::addi, XRegister::zero, constants[variable->name()])
);
2024-02-28 16:18:39 +01:00
}
void RiscVVisitor::visit(source::integer_literal *number)
2024-02-28 16:18:39 +01:00
{
const auto freeRegister = this->registerInUse ? XRegister::a0 : XRegister::t0;
2024-03-07 09:15:11 +01:00
this->instructions.push_back(
2024-02-28 16:18:39 +01:00
Instruction(BaseOpcode::opImm) // movl $x, %eax; where $x is a number.
2024-03-07 09:15:11 +01:00
.i(freeRegister, Funct3::addi, XRegister::zero, number->number())
);
2024-02-28 16:18:39 +01:00
}
void RiscVVisitor::visit(source::binary_expression *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
2024-03-07 09:15:11 +01:00
this->instructions.push_back( // movl %eax, -x(%rbp); where x is a number.
2024-03-01 10:13:55 +01:00
Instruction(BaseOpcode::store)
2024-03-07 09:15:11 +01:00
.s(static_cast<std::uint32_t>(this->variableCounter * 4), Funct3::sw, XRegister::sp, XRegister::a0)
);
2024-03-01 10:13:55 +01:00
auto lhs_stack_position = ++this->variableCounter;
2024-02-28 16:18:39 +01:00
this->registerInUse = false;
expression->rhs().accept(this);
2024-02-28 16:18:39 +01:00
2024-03-07 09:15:11 +01:00
this->instructions.push_back(Instruction(BaseOpcode::load)
2024-03-01 10:13:55 +01:00
.i(XRegister::a0, Funct3::lw, XRegister::sp,
2024-03-07 09:15:11 +01:00
static_cast<std::int8_t>((lhs_stack_position - 1) * 4))
);
2024-03-01 10:13:55 +01:00
2024-02-28 16:18:39 +01:00
// Calculate the result and assign it to a variable on the stack.
switch (expression->operation())
2024-02-28 16:18:39 +01:00
{
case source::binary_operator::sum:
2024-03-07 09:15:11 +01:00
this->instructions.push_back(Instruction(BaseOpcode::op)
.r(lhs_register, Funct3::add, XRegister::a0, XRegister::t0));
2024-02-28 16:18:39 +01:00
break;
case source::binary_operator::subtraction:
2024-03-07 09:15:11 +01:00
this->instructions.push_back(Instruction(BaseOpcode::op)
.r(lhs_register, Funct3::sub, XRegister::a0, XRegister::t0, Funct7::sub));
2024-02-28 16:18:39 +01:00
break;
case source::binary_operator::multiplication:
2024-03-07 09:15:11 +01:00
this->instructions.push_back(Instruction(BaseOpcode::op)
.r(lhs_register, Funct3::mul, XRegister::a0, XRegister::t0, Funct7::muldiv));
2024-03-03 13:11:39 +01:00
break;
2024-02-28 16:18:39 +01:00
}
2024-03-01 10:13:55 +01:00
}
2024-02-28 16:18:39 +01:00
}