From 9e9606681f568fbf89f95f29a589bc9932a12c90 Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Sat, 10 May 2025 13:39:25 +0100 Subject: [PATCH] Removed how variables were handled --- IR-Generator/inc/Parser.h | 25 ------------------ Parser/Parser.vcxproj | 1 - Parser/Parser.vcxproj.filters | 3 --- Parser/inc/AST.h | 3 --- Parser/src/AST/AST-Constructors.cpp | 2 +- Parser/src/AST/AST-LLVM.cpp | 17 +++---------- Parser/src/GenIR.cpp | 3 +-- Parser/src/Scope.cpp | 39 ----------------------------- 8 files changed, 5 insertions(+), 88 deletions(-) delete mode 100644 Parser/src/Scope.cpp diff --git a/IR-Generator/inc/Parser.h b/IR-Generator/inc/Parser.h index f1842bb..b58ddf6 100644 --- a/IR-Generator/inc/Parser.h +++ b/IR-Generator/inc/Parser.h @@ -63,28 +63,6 @@ namespace LX::AST namespace LX { - class Scope - { - public: - // Default constructor // - Scope() - : m_LocalVariables{}, m_Child(nullptr) - {} - - // Gets a variable from the scope by it's name // - llvm::AllocaInst* GetVar(const std::string& name); - - // Creates a variable of the given name // - llvm::AllocaInst* CreateVar(const std::string& name, InfoLLVM& LLVM); - - private: - // Holds all the variables in the scope (excluding ones owned by the children // - std::unordered_map m_LocalVariables; - - // Holds a section of the scope, for example the variables created in a while loop // - std::unique_ptr m_Child; - }; - // Holds all needed info about a function // // Currently only holds the body but in the future will hold: params, namespace/class-member // struct FunctionDefinition @@ -97,9 +75,6 @@ namespace LX // The parameters of the function // std::vector params; - - // The scope off the function // - Scope scope; // The instructions of the body of the function // std::vector> body; diff --git a/Parser/Parser.vcxproj b/Parser/Parser.vcxproj index 3d6a285..7bd6eb5 100644 --- a/Parser/Parser.vcxproj +++ b/Parser/Parser.vcxproj @@ -153,7 +153,6 @@ - diff --git a/Parser/Parser.vcxproj.filters b/Parser/Parser.vcxproj.filters index c979894..a80584e 100644 --- a/Parser/Parser.vcxproj.filters +++ b/Parser/Parser.vcxproj.filters @@ -20,9 +20,6 @@ Source Files - - Source Files - Source Files diff --git a/Parser/inc/AST.h b/Parser/inc/AST.h index 778f038..b86a32b 100644 --- a/Parser/inc/AST.h +++ b/Parser/inc/AST.h @@ -13,9 +13,6 @@ namespace LX llvm::LLVMContext context; llvm::Module module; llvm::IRBuilder<> builder; - - // Not LLVM I just cba to add this parameter to the functions that needed it // - Scope* scope; }; } diff --git a/Parser/src/AST/AST-Constructors.cpp b/Parser/src/AST/AST-Constructors.cpp index 7972a08..e01f6db 100644 --- a/Parser/src/AST/AST-Constructors.cpp +++ b/Parser/src/AST/AST-Constructors.cpp @@ -8,7 +8,7 @@ namespace LX { // Default constructor that just initalises LLVM variables that it holds // InfoLLVM::InfoLLVM(std::string name) - : context{}, builder(context), module(name, context), scope(nullptr) + : context{}, builder(context), module(name, context) {} // Reserves space for nodes (stops excess allocations) // diff --git a/Parser/src/AST/AST-LLVM.cpp b/Parser/src/AST/AST-LLVM.cpp index 429ce06..435eed1 100644 --- a/Parser/src/AST/AST-LLVM.cpp +++ b/Parser/src/AST/AST-LLVM.cpp @@ -101,27 +101,16 @@ namespace LX::AST // Function for generating LLVM IR (Intermediate representation) // llvm::Value* VariableDeclaration::GenIR(InfoLLVM& LLVM) { - // Creates the variable within the scope // - return LLVM.scope->CreateVar(m_Name, LLVM); + return nullptr; } llvm::Value* VariableAssignment::GenIR(InfoLLVM& LLVM) { - // Gets the variable from the current scope // - llvm::AllocaInst* asignee = LLVM.scope->GetVar(m_Name); - ThrowIf(asignee == nullptr); - - // Creates the assignment // - return LLVM.builder.CreateStore(m_Value->GenIR(LLVM), asignee); + return nullptr; } llvm::Value* VariableAccess::GenIR(InfoLLVM& LLVM) { - // Loads the variable from the current scope // - llvm::AllocaInst* var = LLVM.scope->GetVar(m_Name); - ThrowIf(var == nullptr); - - // Creates the load within the AST // - return LLVM.builder.CreateLoad(llvm::Type::getInt32Ty(LLVM.context), var, m_Name + "_val"); + return nullptr; } } diff --git a/Parser/src/GenIR.cpp b/Parser/src/GenIR.cpp index ac55dbe..2664d01 100644 --- a/Parser/src/GenIR.cpp +++ b/Parser/src/GenIR.cpp @@ -40,7 +40,7 @@ namespace LX // Adds the function's parameters to the scope // for (std::string& param : funcAST.params) { - LLVM.scope->CreateVar(param, LLVM); + //LLVM.scope->CreateVar(param, LLVM); } // Generates the IR within the function by looping over the nodes // @@ -73,7 +73,6 @@ namespace LX // Loops over the functions to generate their LLVM IR // for (auto& func : ast.functions) { - LLVM.scope = &func.scope; // Sets the current scope for the builder GenerateFunctionIR(func, LLVM); } diff --git a/Parser/src/Scope.cpp b/Parser/src/Scope.cpp deleted file mode 100644 index 25e4f5e..0000000 --- a/Parser/src/Scope.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include - -#include - -#include -#include - -namespace LX -{ - llvm::AllocaInst* Scope::GetVar(const std::string& name) - { - // Stores a pointer to the current scope // - Scope* current = this; - - do - { - // Checks if the variable exists in the current scope // - bool exists = current->m_LocalVariables.contains(name); - if (exists) { return m_LocalVariables[name]; } - - // Travels to the next scope // - current = current->m_Child.get(); - - } while (current != nullptr); - - // If it gets here it means it couldnt find the variable so it doesnt exist in the current context // - return nullptr; - } - - llvm::AllocaInst* Scope::CreateVar(const std::string& name, InfoLLVM& LLVM) - { - // Checks variable of the same name doesn't exist // - ThrowIf(GetVar(name) != nullptr); - - // Else inserts it into the local set // - m_LocalVariables[name] = LLVM.builder.CreateAlloca(LLVM.builder.getInt32Ty(), nullptr, name); - return m_LocalVariables[name]; - } -}