From d3d487159a56d9ce11174a624df0b7af9ca52130 Mon Sep 17 00:00:00 2001 From: Sofia Date: Mon, 27 Apr 2026 23:11:35 +0300 Subject: [PATCH] Use static allocator --- src/codegen.cpp | 14 ++++++-------- src/stack_allocator.cpp | 2 +- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/codegen.cpp b/src/codegen.cpp index 38347a4..560eb16 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -20,7 +20,7 @@ namespace AST { 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); return codegen::StackValue{ @@ -38,7 +38,7 @@ namespace AST { return std::shared_ptr {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{ std::make_shared(true, types::FundamentalTypeKind::Char), static_cast(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); if (value != scope.values.end()) { if (scope.is_lvalue) { @@ -393,7 +393,7 @@ namespace AST { } 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) { auto array_ty = dynamic_cast(this->m_ty.get()); @@ -475,8 +475,7 @@ namespace AST { builder.builder->SetInsertPoint(builder.block); if (this->m_type->m_kind == types::TypeKind::Array) { - auto raw_llvm_ty = this->m_type->codegen(builder, scope.structs); - auto ptr = builder.builder->CreateAlloca(raw_llvm_ty); + auto ptr = allocator.pop_alloca(this->m_type); if (this->m_expr.has_value()) { auto value = this->m_expr->get()->codegen(builder, scope, allocator); builder.builder->CreateStore(value.value, ptr, false); @@ -486,8 +485,7 @@ namespace AST { return; } - auto ty = this->m_type->codegen(builder, scope.structs); - auto ptr = builder.builder->CreateAlloca(ty); + auto ptr = allocator.pop_alloca(this->m_type); if (this->m_expr.has_value()) { auto value = this->m_expr->get()->codegen(builder, scope, allocator); builder.builder->CreateStore(value.value, ptr, false); diff --git a/src/stack_allocator.cpp b/src/stack_allocator.cpp index c11a06a..302a370 100644 --- a/src/stack_allocator.cpp +++ b/src/stack_allocator.cpp @@ -72,10 +72,10 @@ namespace AST { } void InitializationStatement::codegen_alloca(codegen::StackAllocator& allocator) { + allocator.push_alloca(this->m_type, this->m_name); if (this->m_expr) { (*this->m_expr)->codegen_alloca(allocator); } - allocator.push_alloca(this->m_type, this->m_name); } void ExpressionStatement::codegen_alloca(codegen::StackAllocator& allocator) {