Assign variables
This commit is contained in:
@ -63,7 +63,6 @@ namespace elna::riscv
|
||||
|
||||
void visitor::visit(source::definition *definition)
|
||||
{
|
||||
constants[definition->identifier()] = definition->body().number();
|
||||
}
|
||||
|
||||
void visitor::visit(source::block *block)
|
||||
@ -73,14 +72,7 @@ namespace elna::riscv
|
||||
this->instructions.push_back(instruction(base_opcode::store));
|
||||
this->instructions.push_back(instruction(base_opcode::opImm));
|
||||
|
||||
for (const auto& block_definition : block->definitions())
|
||||
{
|
||||
block_definition->accept(this);
|
||||
}
|
||||
for (const auto& block_declaration : block->declarations())
|
||||
{
|
||||
block_declaration->accept(this);
|
||||
}
|
||||
table = block->table();
|
||||
block->body().accept(this);
|
||||
|
||||
// Set the return value (0).
|
||||
@ -88,7 +80,9 @@ namespace elna::riscv
|
||||
.r(x_register::a0, funct3_t::_and, x_register::zero, x_register::zero));
|
||||
|
||||
// Prologue.
|
||||
const uint stack_size = static_cast<std::uint32_t>(variable_counter * 4 + 12);
|
||||
auto main_symbol =
|
||||
std::dynamic_pointer_cast<source::procedure_info>(table->lookup("main"));
|
||||
const uint stack_size = static_cast<std::uint32_t>(variable_counter * 4 + 8 + main_symbol->stack_size());
|
||||
|
||||
this->instructions[0].i(x_register::sp, funct3_t::addi, x_register::sp, -stack_size);
|
||||
this->instructions[1].s(stack_size - 4, funct3_t::sw, x_register::sp, x_register::s0);
|
||||
@ -143,18 +137,37 @@ namespace elna::riscv
|
||||
}
|
||||
}
|
||||
|
||||
void visitor::visit(source::assignment_statement *statement)
|
||||
void visitor::visit(source::assign_statement *statement)
|
||||
{
|
||||
const auto free_register = this->register_in_use ? x_register::a0 : x_register::t0;
|
||||
auto symbol = table->lookup(statement->lvalue());
|
||||
auto variable_symbol = std::dynamic_pointer_cast<source::variable_info>(symbol);
|
||||
|
||||
statement->rvalue().accept(this);
|
||||
|
||||
this->instructions.push_back(instruction(base_opcode::store)
|
||||
.s(variable_symbol->offset, funct3_t::sw, x_register::s0, x_register::a0));
|
||||
}
|
||||
|
||||
void visitor::visit(source::variable_expression *variable)
|
||||
{
|
||||
const auto free_register = this->register_in_use ? x_register::a0 : x_register::t0;
|
||||
|
||||
this->instructions.push_back(
|
||||
instruction(base_opcode::opImm) // movl $x, %eax; where $x is a number.
|
||||
.i(free_register, funct3_t::addi, x_register::zero, constants[variable->name()])
|
||||
);
|
||||
auto symbol = table->lookup(variable->name());
|
||||
if (auto constant_symbol = std::dynamic_pointer_cast<source::constant_info>(symbol))
|
||||
{
|
||||
this->instructions.push_back(
|
||||
instruction(base_opcode::opImm) // movl $x, %eax; where $x is a number.
|
||||
.i(free_register, funct3_t::addi, x_register::zero, constant_symbol->value())
|
||||
);
|
||||
}
|
||||
else if (auto variable_symbol = std::dynamic_pointer_cast<source::variable_info>(symbol))
|
||||
{
|
||||
this->instructions.push_back(
|
||||
instruction(base_opcode::store)
|
||||
.i(free_register, funct3_t::lw, x_register::s0, variable_symbol->offset)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void visitor::visit(source::integer_literal *number)
|
||||
|
Reference in New Issue
Block a user