Implement argument passing

This commit is contained in:
2025-01-15 01:48:09 +01:00
parent 5cb0e18a87
commit 9293faea09
6 changed files with 892 additions and 7 deletions

325
source/semantic.cc Normal file
View File

@ -0,0 +1,325 @@
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include "elna/source/semantic.h"
#include "elna/source/result.h"
#include <cstdlib>
namespace elna
{
namespace source
{
name_analysis_visitor::name_analysis_visitor(std::shared_ptr<symbol_table> table,
const char *filename, const std::size_t target_pointer_size)
: table(table), filename(filename), pointer_size(target_pointer_size)
{
}
void name_analysis_visitor::visit(constant_definition *definition)
{
auto constant_type = std::make_shared<const class primitive_type>(int_type);
this->table->enter(definition->identifier(),
std::make_shared<constant_info>(constant_type, definition->body().number()));
}
std::shared_ptr<const type> name_analysis_visitor::convert_declaration_type(const type_expression& ast_type) const
{
auto variable_type = table->lookup(ast_type.base())->is_type_info()->type();
std::shared_ptr<type> declaration_type;
if (ast_type.is_pointer())
{
return std::make_shared<pointer_type>(variable_type, 4);
}
else
{
return variable_type;
}
}
void name_analysis_visitor::visit(declaration *declarationx)
{
std::shared_ptr<const type> declaration_type = convert_declaration_type(declarationx->type());
this->table->enter(declarationx->identifier(),
std::make_shared<variable_info>(declaration_type));
}
void name_analysis_visitor::visit(program *program)
{
class procedure_type main_type{ std::vector<std::shared_ptr<const class type>>(), this->pointer_size };
this->table->enter("_start", std::make_shared<procedure_info>(main_type, this->table));
empty_visitor::visit(program);
}
void name_analysis_visitor::visit(procedure_definition *procedure)
{
std::vector<std::shared_ptr<const type>> arguments;
for (auto& parameter : procedure->parameters())
{
auto declaration_type = convert_declaration_type(parameter->type());
arguments.push_back(declaration_type);
}
procedure_type definition_type{ std::move(arguments), this->pointer_size };
auto info = std::make_shared<procedure_info>(definition_type, this->table);
this->table->enter(procedure->identifier(), info);
this->table = info->scope();
for (std::size_t i = 0; i < procedure->parameters().size(); ++i)
{
this->table->enter(procedure->parameters().at(i)->identifier(),
std::make_shared<parameter_info>(definition_type.arguments.at(i)));
}
procedure->body().accept(this);
this->table = info->scope()->scope();
}
const std::list<std::unique_ptr<error>>& name_analysis_visitor::errors() const noexcept
{
return m_errors;
}
allocator_visitor::allocator_visitor(std::shared_ptr<symbol_table> table)
: table(table)
{
}
void allocator_visitor::visit(declaration *declaration)
{
auto declaration_info = this->table->lookup(declaration->identifier());
if (auto variable = declaration_info->is_variable_info())
{
this->local_offset -= sizeof(std::int32_t);
variable->offset = this->local_offset;
}
else if (auto parameter = declaration_info->is_parameter_info())
{
parameter->offset = this->argument_offset;
this->argument_offset += sizeof(std::int32_t);
}
}
void allocator_visitor::visit(program *program)
{
this->local_offset = 0;
this->argument_offset = 0;
empty_visitor::visit(program);
table->lookup("_start")->is_procedure_info()->local_stack_size =
std::abs(this->local_offset);
}
void allocator_visitor::visit(procedure_definition *procedure)
{
this->local_offset = 0;
this->argument_offset = 0;
auto info = this->table->lookup(procedure->identifier())->is_procedure_info();
this->table = info->scope();
empty_visitor::visit(procedure);
this->table = info->scope()->scope();
info->local_stack_size = std::abs(this->local_offset);
info->argument_stack_size = this->argument_offset;
}
void allocator_visitor::visit(call_statement *statement)
{
auto call_info = this->table->lookup(statement->name())->is_intrinsic_info();
this->argument_offset = std::max(static_cast<std::size_t>(this->argument_offset),
call_info->parameter_stack_size());
}
type_analysis_visitor::type_analysis_visitor(std::shared_ptr<symbol_table> table,
const char *filename, const std::size_t target_pointer_size)
: table(table), filename(filename), pointer_size(target_pointer_size)
{
}
void type_analysis_visitor::visit(program *program)
{
for (auto& definition : program->definitions())
{
definition->accept(this);
}
program->body().accept(this);
}
void type_analysis_visitor::visit(procedure_definition *procedure)
{
auto info = this->table->lookup(procedure->identifier())->is_procedure_info();
this->table = info->scope();
procedure->body().accept(this);
this->table = info->scope()->scope();
}
void type_analysis_visitor::visit(integer_literal *literal)
{
literal->data_type = table->lookup("Int")->is_type_info()->type();
}
void type_analysis_visitor::visit(boolean_literal *literal)
{
literal->data_type = table->lookup("Boolean")->is_type_info()->type();
}
void type_analysis_visitor::visit(variable_expression *expression)
{
expression->data_type = table->lookup(expression->name())->is_typed_info()->type();
}
void type_analysis_visitor::visit(unary_expression *expression)
{
empty_visitor::visit(expression);
switch (expression->operation())
{
case unary_operator::reference:
expression->data_type = std::make_shared<const pointer_type>(expression->operand().data_type,
this->pointer_size);
break;
case unary_operator::dereference:
auto operand_type = expression->operand().data_type;
if (operand_type->is_pointer_type() != nullptr)
{
expression->data_type = operand_type;
}
else if (operand_type != nullptr)
{
auto new_error = std::make_unique<type_mismatch>(operand_type,
type_mismatch::operation::dereference, this->filename, expression->position());
m_errors.push_back(std::move(new_error));
}
break;
}
}
void type_analysis_visitor::visit(binary_expression *expression)
{
empty_visitor::visit(expression);
switch (expression->operation())
{
case binary_operator::sum:
case binary_operator::subtraction:
case binary_operator::multiplication:
case binary_operator::division:
case binary_operator::less:
case binary_operator::greater:
case binary_operator::less_equal:
case binary_operator::greater_equal:
if (expression->lhs().data_type != nullptr && expression->rhs().data_type != nullptr)
{
std::unique_ptr<type_mismatch> new_error;
if (*expression->lhs().data_type != int_type)
{
new_error = std::make_unique<type_mismatch>(expression->lhs().data_type,
type_mismatch::operation::arithmetic, this->filename, expression->lhs().position());
}
if (*expression->rhs().data_type != int_type)
{
new_error = std::make_unique<type_mismatch>(expression->rhs().data_type,
type_mismatch::operation::arithmetic, this->filename, expression->rhs().position());
}
if (new_error != nullptr)
{
m_errors.push_back(std::move(new_error));
}
}
break;
case binary_operator::equals:
case binary_operator::not_equals:
if (expression->lhs().data_type != nullptr && expression->rhs().data_type != nullptr)
{
if (expression->lhs().data_type != expression->rhs().data_type)
{
auto new_error = std::make_unique<type_mismatch>(expression->rhs().data_type,
type_mismatch::operation::comparison, this->filename, expression->rhs().position());
}
}
break;
}
}
void type_analysis_visitor::visit(call_statement *statement)
{
auto call_info = this->table->lookup(statement->name())->is_intrinsic_info();
std::size_t i{ 0 };
for (const auto& argument : statement->arguments())
{
argument->accept(this);
if (argument->data_type != nullptr && i < call_info->type()->arguments.size()
&& call_info->type()->arguments[i] != argument->data_type)
{
auto new_error = std::make_unique<type_mismatch>(argument->data_type,
type_mismatch::operation::argument, this->filename, argument->position());
m_errors.push_back(std::move(new_error));
}
++i;
}
}
void type_analysis_visitor::visit(constant_definition *definition)
{
definition->body().accept(this);
}
void type_analysis_visitor::visit(while_statement *statement)
{
statement->prerequisite().accept(this);
auto condition_type = statement->prerequisite().data_type;
if (condition_type != nullptr && *condition_type != boolean_type)
{
auto new_error = std::make_unique<type_mismatch>(condition_type,
type_mismatch::operation::condition, this->filename, statement->prerequisite().position());
m_errors.push_back(std::move(new_error));
}
statement->body().accept(this);
}
void type_analysis_visitor::visit(if_statement *statement)
{
statement->prerequisite().accept(this);
auto condition_type = statement->prerequisite().data_type;
if (condition_type != nullptr && *condition_type != boolean_type)
{
auto new_error = std::make_unique<type_mismatch>(condition_type,
type_mismatch::operation::condition, this->filename, statement->prerequisite().position());
m_errors.push_back(std::move(new_error));
}
statement->body().accept(this);
}
void type_analysis_visitor::visit(assign_statement *statement)
{
statement->rvalue().accept(this);
auto lvalue_info = this->table->lookup(statement->lvalue())->is_typed_info();
if (statement->rvalue().data_type != nullptr && lvalue_info->type() == statement->rvalue().data_type)
{
auto new_error = std::make_unique<type_mismatch>(statement->rvalue().data_type,
type_mismatch::operation::assignment, this->filename, statement->position());
m_errors.push_back(std::move(new_error));
}
}
const std::list<std::unique_ptr<error>>& type_analysis_visitor::errors() const noexcept
{
return m_errors;
}
}
}

233
source/symbol_table.cc Normal file
View File

@ -0,0 +1,233 @@
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include "elna/source/types.h"
#include "elna/source/symbol_table.h"
namespace elna
{
namespace source
{
symbol_table::symbol_table(std::shared_ptr<symbol_table> scope)
: outer_scope(scope)
{
}
std::shared_ptr<info> symbol_table::lookup(const std::string& name)
{
auto entry = entries.find(name);
if (entry != entries.cend())
{
return entry->second;
}
if (this->outer_scope != nullptr)
{
return this->outer_scope->lookup(name);
}
return nullptr;
}
void symbol_table::enter(const std::string& name, std::shared_ptr<info> entry)
{
entries.insert({ name, entry });
}
std::shared_ptr<symbol_table> symbol_table::scope()
{
return this->outer_scope;
}
info::~info()
{
}
type_info *info::is_type_info() noexcept
{
return nullptr;
}
typed_info *info::is_typed_info() noexcept
{
return nullptr;
}
constant_info *info::is_constant_info() noexcept
{
return nullptr;
}
variable_info *info::is_variable_info() noexcept
{
return nullptr;
}
parameter_info *info::is_parameter_info() noexcept
{
return nullptr;
}
intrinsic_info *info::is_intrinsic_info() noexcept
{
return nullptr;
}
procedure_info *info::is_procedure_info() noexcept
{
return nullptr;
}
info::info()
{
}
type_info::type_info(std::shared_ptr<class type> type)
: info(), m_type(type)
{
}
type_info::~type_info()
{
}
std::shared_ptr<const class type> type_info::type() const noexcept
{
return m_type;
}
type_info *type_info::is_type_info() noexcept
{
return this;
}
typed_info::typed_info(std::shared_ptr<const class type> type)
: m_type(type)
{
}
typed_info::~typed_info()
{
}
typed_info *typed_info::is_typed_info() noexcept
{
return this;
}
std::shared_ptr<const class type> typed_info::type() const noexcept
{
return m_type;
}
constant_info::constant_info(const std::shared_ptr<const class type> type, const std::int32_t value)
: typed_info(type), m_value(value)
{
}
constant_info *constant_info::is_constant_info() noexcept
{
return this;
}
std::int32_t constant_info::value() const noexcept
{
return m_value;
}
variable_info::variable_info(std::shared_ptr<const class type> type)
: typed_info(type)
{
}
variable_info *variable_info::is_variable_info() noexcept
{
return this;
}
parameter_info::parameter_info(std::shared_ptr<const class type> type)
: typed_info(type)
{
}
parameter_info *parameter_info::is_parameter_info() noexcept
{
return this;
}
intrinsic_info::intrinsic_info(const class procedure_type& type)
: m_type(std::make_shared<procedure_type>(type))
{
}
intrinsic_info::~intrinsic_info()
{
}
std::shared_ptr<const class procedure_type> intrinsic_info::type() const noexcept
{
return m_type;
}
std::size_t intrinsic_info::parameter_stack_size() const noexcept
{
return type()->arguments.size() * sizeof(std::int32_t);
}
intrinsic_info *intrinsic_info::is_intrinsic_info() noexcept
{
return this;
}
procedure_info::procedure_info(const class procedure_type& type, std::shared_ptr<symbol_table> outer_scope)
: intrinsic_info(type), local_table(std::make_shared<symbol_table>(outer_scope))
{
}
procedure_info::~procedure_info()
{
}
std::shared_ptr<symbol_table> procedure_info::scope()
{
return local_table;
}
std::size_t procedure_info::stack_size() const noexcept
{
return local_stack_size + argument_stack_size;
}
procedure_info *procedure_info::is_procedure_info() noexcept
{
return this;
}
constexpr std::size_t pointer_size = 4;
std::shared_ptr<source::symbol_table> add_builtin_symbols()
{
source::symbol_table result;
std::vector<std::shared_ptr<const source::type>> intrinsic_arguments;
auto prim = boolean_type;
auto boolean_info = std::make_shared<type_info>(std::make_shared<primitive_type>(boolean_type));
auto int_info = std::make_shared<source::type_info>(std::make_shared<primitive_type>(int_type));
result.enter("Boolean", boolean_info);
result.enter("Int", int_info);
intrinsic_arguments.push_back(int_info->type());
auto writei = std::make_shared<source::intrinsic_info>(
source::procedure_type{ intrinsic_arguments, pointer_size });
result.enter("writei", writei);
intrinsic_arguments.clear();
intrinsic_arguments.push_back(boolean_info->type());
auto writeb = std::make_shared<source::intrinsic_info>(
source::procedure_type{ intrinsic_arguments, pointer_size });
result.enter("writeb", writeb);
intrinsic_arguments.clear();
return std::make_shared<source::symbol_table>(std::move(result));
}
}
}