Use static allocator

This commit is contained in:
Sofia 2026-04-27 23:11:35 +03:00
parent 8530e1162d
commit d3d487159a
2 changed files with 7 additions and 9 deletions

View File

@ -20,7 +20,7 @@ namespace AST {
return this->m_ty; return this->m_ty;
} }
codegen::StackValue IntLiteralExpression::codegen(codegen::Builder& builder, codegen::Scope& scope, codegen::StackAllocator& allocator) { codegen::StackValue IntLiteralExpression::codegen(codegen::Builder& builder, codegen::Scope& scope, codegen::StackAllocator&) {
auto ty = this->m_ty->codegen(builder, scope.structs); auto ty = this->m_ty->codegen(builder, scope.structs);
return codegen::StackValue{ return codegen::StackValue{
@ -38,7 +38,7 @@ namespace AST {
return std::shared_ptr<types::Type> {stack_type}; return std::shared_ptr<types::Type> {stack_type};
} }
codegen::StackValue StringLiteralExpression::codegen(codegen::Builder& builder, codegen::Scope& scope, codegen::StackAllocator& allocator) { codegen::StackValue StringLiteralExpression::codegen(codegen::Builder& builder, codegen::Scope& scope, codegen::StackAllocator&) {
auto stack_type = new types::ArrayType{ auto stack_type = new types::ArrayType{
std::make_shared<types::FundamentalType>(true, types::FundamentalTypeKind::Char), std::make_shared<types::FundamentalType>(true, types::FundamentalTypeKind::Char),
static_cast<uint32_t>(this->m_value.size()) + 1, static_cast<uint32_t>(this->m_value.size()) + 1,
@ -78,7 +78,7 @@ namespace AST {
} }
} }
codegen::StackValue ValueReferenceExpression::codegen(codegen::Builder& builder, codegen::Scope& scope, codegen::StackAllocator& allocator) { codegen::StackValue ValueReferenceExpression::codegen(codegen::Builder& builder, codegen::Scope& scope, codegen::StackAllocator&) {
auto value = scope.values.find(this->m_name); auto value = scope.values.find(this->m_name);
if (value != scope.values.end()) { if (value != scope.values.end()) {
if (scope.is_lvalue) { if (scope.is_lvalue) {
@ -393,7 +393,7 @@ namespace AST {
} }
codegen::StackValue ListInitializerExpression::codegen(codegen::Builder& builder, codegen::Scope& scope, codegen::StackAllocator& allocator) { codegen::StackValue ListInitializerExpression::codegen(codegen::Builder& builder, codegen::Scope& scope, codegen::StackAllocator& allocator) {
auto value_ptr = builder.builder->CreateAlloca(this->m_ty->codegen(builder, scope.structs)); auto value_ptr = allocator.pop_alloca(this->m_ty);
if (this->m_ty->m_kind == types::TypeKind::Array) { if (this->m_ty->m_kind == types::TypeKind::Array) {
auto array_ty = dynamic_cast<types::ArrayType*>(this->m_ty.get()); auto array_ty = dynamic_cast<types::ArrayType*>(this->m_ty.get());
@ -475,8 +475,7 @@ namespace AST {
builder.builder->SetInsertPoint(builder.block); builder.builder->SetInsertPoint(builder.block);
if (this->m_type->m_kind == types::TypeKind::Array) { if (this->m_type->m_kind == types::TypeKind::Array) {
auto raw_llvm_ty = this->m_type->codegen(builder, scope.structs); auto ptr = allocator.pop_alloca(this->m_type);
auto ptr = builder.builder->CreateAlloca(raw_llvm_ty);
if (this->m_expr.has_value()) { if (this->m_expr.has_value()) {
auto value = this->m_expr->get()->codegen(builder, scope, allocator); auto value = this->m_expr->get()->codegen(builder, scope, allocator);
builder.builder->CreateStore(value.value, ptr, false); builder.builder->CreateStore(value.value, ptr, false);
@ -486,8 +485,7 @@ namespace AST {
return; return;
} }
auto ty = this->m_type->codegen(builder, scope.structs); auto ptr = allocator.pop_alloca(this->m_type);
auto ptr = builder.builder->CreateAlloca(ty);
if (this->m_expr.has_value()) { if (this->m_expr.has_value()) {
auto value = this->m_expr->get()->codegen(builder, scope, allocator); auto value = this->m_expr->get()->codegen(builder, scope, allocator);
builder.builder->CreateStore(value.value, ptr, false); builder.builder->CreateStore(value.value, ptr, false);

View File

@ -72,10 +72,10 @@ namespace AST {
} }
void InitializationStatement::codegen_alloca(codegen::StackAllocator& allocator) { void InitializationStatement::codegen_alloca(codegen::StackAllocator& allocator) {
allocator.push_alloca(this->m_type, this->m_name);
if (this->m_expr) { if (this->m_expr) {
(*this->m_expr)->codegen_alloca(allocator); (*this->m_expr)->codegen_alloca(allocator);
} }
allocator.push_alloca(this->m_type, this->m_name);
} }
void ExpressionStatement::codegen_alloca(codegen::StackAllocator& allocator) { void ExpressionStatement::codegen_alloca(codegen::StackAllocator& allocator) {