Remove libc dependency for the generated code

This commit is contained in:
2024-05-23 01:13:16 +02:00
parent 189b2e6054
commit 8ea09ff3b4
14 changed files with 362 additions and 417 deletions

View File

@ -116,12 +116,10 @@ namespace elna::riscv
static void generate_intrinsics(std::shared_ptr<source::writer<std::byte>> writer,
std::vector<reference>& references)
{
writer->sink("printf");
{
std::vector<instruction> instructions;
auto format_string = writer->sink(reinterpret_cast<const std::byte *>("%c\n\0"), 4);
prologue(instructions);
instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a1, funct3_t::addi, x_register::zero, 't'));
instructions.push_back(instruction(base_opcode::branch)
@ -129,42 +127,68 @@ namespace elna::riscv
instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a1, funct3_t::addi, x_register::zero, 'f'));
relocate(format_string, address_t::high20, references, instructions, writer);
instructions.push_back(instruction(base_opcode::lui).u(x_register::a5, 0));
relocate(format_string, address_t::lower12i, references, instructions, writer);
instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a0, funct3_t::addi, x_register::a5, 0));
.i(x_register::t0, funct3_t::addi, x_register::a0, 0));
instructions.push_back(instruction(base_opcode::store)
.s(0, funct3_t::sw, x_register::s0, x_register::a1));
instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a0, funct3_t::addi, x_register::zero, 1));
instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a1, funct3_t::addi, x_register::s0, 0));
instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a2, funct3_t::addi, x_register::zero, 1));
instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a7, funct3_t::addi, x_register::zero, 64));
instructions.push_back(instruction(base_opcode::system)
.i(x_register::zero, funct3_t::priv, x_register::zero, 0));
relocate("printf", address_t::text, references, instructions ,writer);
instructions.push_back(instruction(base_opcode::auipc).u(x_register::ra, 0));
instructions.push_back(instruction(base_opcode::jalr)
.i(x_register::ra, funct3_t::jalr, x_register::ra, 0));
instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::t1, funct3_t::addi, x_register::zero, '\n'));
instructions.push_back(instruction(base_opcode::store)
.s(0, funct3_t::sw, x_register::s0, x_register::t1));
instructions.push_back(instruction(base_opcode::system)
.i(x_register::zero, funct3_t::priv, x_register::zero, 0));
epilogue(8, instructions);
instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a0, funct3_t::addi, x_register::t0, 0));
epilogue(12, instructions);
writer->sink("writeb", reinterpret_cast<const std::byte *>(instructions.data()),
instructions.size() * sizeof(instruction));
}
{
std::vector<instruction> instructions;
auto format_string = writer->sink(reinterpret_cast<const std::byte *>("%d\n\0"), 4);
prologue(instructions);
instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a1, funct3_t::addi, x_register::a0, 0));
relocate(format_string, address_t::high20, references, instructions, writer);
instructions.push_back(instruction(base_opcode::lui).u(x_register::a5, 0));
relocate(format_string, address_t::lower12i, references, instructions, writer);
.i(x_register::t0, funct3_t::addi, x_register::a0, 0));
instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a0, funct3_t::addi, x_register::a5, 0));
.i(x_register::a0, funct3_t::addi, x_register::a0, '0'));
instructions.push_back(instruction(base_opcode::store)
.s(0, funct3_t::sw, x_register::s0, x_register::a0));
instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a0, funct3_t::addi, x_register::zero, 1));
instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a1, funct3_t::addi, x_register::s0, 0));
instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a2, funct3_t::addi, x_register::zero, 1));
instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a7, funct3_t::addi, x_register::zero, 64));
instructions.push_back(instruction(base_opcode::system)
.i(x_register::zero, funct3_t::priv, x_register::zero, 0));
relocate("printf", address_t::text, references, instructions, writer);
instructions.push_back(instruction(base_opcode::auipc).u(x_register::ra, 0));
instructions.push_back(instruction(base_opcode::jalr)
.i(x_register::ra, funct3_t::jalr, x_register::ra, 0));
instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::t1, funct3_t::addi, x_register::zero, '\n'));
instructions.push_back(instruction(base_opcode::store)
.s(0, funct3_t::sw, x_register::s0, x_register::t1));
instructions.push_back(instruction(base_opcode::system)
.i(x_register::zero, funct3_t::priv, x_register::zero, 0));
epilogue(8, instructions);
instructions.push_back(instruction(base_opcode::opImm)
.i(x_register::a0, funct3_t::addi, x_register::t0, 0));
epilogue(12, instructions);
writer->sink("writei", reinterpret_cast<const std::byte *>(instructions.data()),
instructions.size() * sizeof(instruction));