From ea9bb1fcb96137f3006fe3d253c6552b5950b08a Mon Sep 17 00:00:00 2001 From: Sofia Date: Thu, 9 Apr 2026 18:38:22 +0300 Subject: [PATCH] Move types to it's own file --- CMakeLists.txt | 2 +- src/ast.cpp | 10 ---------- src/ast.h | 34 +++++++--------------------------- src/builder.h | 16 ++++++++++++++++ src/codegen.cpp | 21 +++++++++++++++------ src/codegen.h | 12 ++++-------- src/parsing.cpp | 7 ++++--- src/types.cpp | 15 +++++++++++++++ src/types.h | 32 ++++++++++++++++++++++++++++++++ 9 files changed, 94 insertions(+), 55 deletions(-) create mode 100644 src/builder.h create mode 100644 src/types.cpp create mode 100644 src/types.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a3b1a08..b5171df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,7 @@ separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS}) add_definitions(${LLVM_DEFINITIONS_LIST}) # Executable -add_executable(${PROJECT_NAME} src/main.cpp src/tokens.cpp src/parsing.cpp src/ast.cpp src/codegen.cpp) +add_executable(${PROJECT_NAME} src/main.cpp src/tokens.cpp src/parsing.cpp src/ast.cpp src/codegen.cpp src/types.cpp) target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20) # Find the libraries that correspond to the LLVM components diff --git a/src/ast.cpp b/src/ast.cpp index 3c2147e..9c8e880 100644 --- a/src/ast.cpp +++ b/src/ast.cpp @@ -81,14 +81,4 @@ namespace AST { out << "}"; return out.str(); } - - std::string FundamentalType::formatted() { - switch (this->m_ty) { - case FundamentalTypeKind::Int: - return "Int"; - default: - return "Unknown"; - } - - } } \ No newline at end of file diff --git a/src/ast.h b/src/ast.h index 6ff69cd..89cb4e9 100644 --- a/src/ast.h +++ b/src/ast.h @@ -2,6 +2,7 @@ #define AST_H #include "codegen.h" +#include "types.h" #include #include @@ -27,13 +28,6 @@ namespace AST { virtual codegen::StackValue codegen(codegen::Builder& builder, codegen::Scope& scope) = 0; }; - class Type { - public: - virtual ~Type() = default; - virtual std::string formatted() = 0; - virtual llvm::Type* codegen(codegen::Builder& builder, codegen::Scope& scope) = 0; - }; - class Statement : public Node { public: virtual void codegen(codegen::Builder& builder, codegen::Scope& scope) = 0; @@ -86,11 +80,11 @@ namespace AST { class InitializationStatement : public Statement { private: - std::unique_ptr m_type; + std::unique_ptr m_type; std::string m_name; std::optional> m_expr; public: - InitializationStatement(std::unique_ptr ty, std::string name, std::optional> expr) + InitializationStatement(std::unique_ptr ty, std::string name, std::optional> expr) : m_type{ std::move(ty) }, m_name{ name }, m_expr{ std::move(expr) } { } virtual ~InitializationStatement() override = default; @@ -117,14 +111,14 @@ namespace AST { class Function : public TopLevelStatement { private: - std::unique_ptr m_return_ty; - std::vector>> m_params; + std::unique_ptr m_return_ty; + std::vector>> m_params; std::string m_name; std::vector> m_statements; public: Function( - std::unique_ptr return_ty, - std::vector>> params, + std::unique_ptr return_ty, + std::vector>> params, std::string name, std::vector> statements) : m_return_ty{ std::move(return_ty) } @@ -136,20 +130,6 @@ namespace AST { virtual std::string formatted() override; virtual void codegen(codegen::Builder& builder, codegen::Scope& scope) override; }; - - enum class FundamentalTypeKind { - Int, - }; - - class FundamentalType : public Type { - private: - FundamentalTypeKind m_ty; - public: - FundamentalType(FundamentalTypeKind kind) : m_ty{ kind } {} - virtual ~FundamentalType() override = default; - virtual std::string formatted() override; - virtual llvm::Type* codegen(codegen::Builder& builder, codegen::Scope& scope) override; - }; } #endif \ No newline at end of file diff --git a/src/builder.h b/src/builder.h new file mode 100644 index 0000000..dc68830 --- /dev/null +++ b/src/builder.h @@ -0,0 +1,16 @@ +#ifndef BUILDER_H +#define BUILDER_H + +#include +#include + +namespace codegen { + struct Builder { + std::unique_ptr context; + std::unique_ptr mod; + std::unique_ptr> builder; + llvm::BasicBlock* block; + }; +} + +#endif \ No newline at end of file diff --git a/src/codegen.cpp b/src/codegen.cpp index ecc23b1..d5b61d3 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -1,5 +1,6 @@ #include "codegen.h" #include "ast.h" +#include "types.h" #include #include @@ -15,7 +16,13 @@ namespace codegen { namespace AST { codegen::StackValue IntLiteralExpression::codegen(codegen::Builder& builder, codegen::Scope& scope) { auto ty = builder.builder->getInt32Ty(); - return codegen::StackValue{ llvm::ConstantInt::get(ty, this->m_value), ty }; + + auto stack_type = new types::FundamentalType{ types::FundamentalTypeKind::Int }; + + return codegen::StackValue{ + llvm::ConstantInt::get(ty, this->m_value), + std::unique_ptr{stack_type} + }; } codegen::StackValue ValueReferenceExpression::codegen(codegen::Builder& builder, codegen::Scope& scope) { @@ -26,7 +33,7 @@ namespace AST { } else { return codegen::StackValue{ - builder.builder->CreateLoad(value->second.ty, value->second.value), + builder.builder->CreateLoad(value->second.ty->codegen(builder), value->second.value), value->second.ty }; } @@ -78,20 +85,20 @@ namespace AST { builder.builder->SetInsertPoint(builder.block); - auto ty = this->m_type->codegen(builder, scope); + auto ty = this->m_type->codegen(builder); auto ptr = builder.builder->CreateAlloca(ty); if (this->m_expr.has_value()) { auto value = this->m_expr->get()->codegen(builder, scope); builder.builder->CreateStore(value.value, ptr, false); } - scope.values[this->m_name] = codegen::StackValue{ ptr, ty }; + scope.values[this->m_name] = codegen::StackValue{ ptr, std::move(this->m_type) }; } void Function::codegen(codegen::Builder& builder, codegen::Scope& scope) { codegen::Scope inner_scope{ scope }; - auto ret_ty = this->m_return_ty->codegen(builder, inner_scope); + auto ret_ty = this->m_return_ty->codegen(builder); std::vector params{}; @@ -113,8 +120,10 @@ namespace AST { builder.block = nullptr; } +} - llvm::Type* FundamentalType::codegen(codegen::Builder& builder, codegen::Scope& scope) { +namespace types { + llvm::Type* FundamentalType::codegen(codegen::Builder& builder) { switch (this->m_ty) { case FundamentalTypeKind::Int: return builder.builder->getInt32Ty(); diff --git a/src/codegen.h b/src/codegen.h index 14846be..90fa67c 100644 --- a/src/codegen.h +++ b/src/codegen.h @@ -6,17 +6,13 @@ #include #include -namespace codegen { - struct Builder { - std::unique_ptr context; - std::unique_ptr mod; - std::unique_ptr> builder; - llvm::BasicBlock* block; - }; +#include "builder.h" +#include "types.h" +namespace codegen { struct StackValue { llvm::Value* value; - llvm::Type* ty; + std::shared_ptr ty; }; struct Scope { diff --git a/src/parsing.cpp b/src/parsing.cpp index 171e7fb..6faeecf 100644 --- a/src/parsing.cpp +++ b/src/parsing.cpp @@ -1,17 +1,18 @@ +#include "types.h" #include "parsing.h" namespace parsing { namespace { - Result, std::string> parse_type(token::TokenStream& stream) { + Result, std::string> parse_type(token::TokenStream& stream) { token::TokenStream inner{ stream }; try { auto token = inner.expect(token::Type::Ident); stream.m_position = inner.m_position; - auto ty = new AST::FundamentalType{ AST::FundamentalTypeKind::Int }; - return new std::unique_ptr{ ty }; + auto ty = new types::FundamentalType{ types::FundamentalTypeKind::Int }; + return new std::unique_ptr{ ty }; } catch (std::runtime_error error) { return new std::string{ error.what() }; diff --git a/src/types.cpp b/src/types.cpp new file mode 100644 index 0000000..41e1f7b --- /dev/null +++ b/src/types.cpp @@ -0,0 +1,15 @@ +#include "types.h" + +#include + +namespace types { + std::string FundamentalType::formatted() { + switch (this->m_ty) { + case FundamentalTypeKind::Int: + return "Int"; + default: + return "Unknown"; + } + + } +} \ No newline at end of file diff --git a/src/types.h b/src/types.h new file mode 100644 index 0000000..af0ca05 --- /dev/null +++ b/src/types.h @@ -0,0 +1,32 @@ +#ifndef TYPES_H +#define TYPES_H + +#include "builder.h" + +#include +#include + +namespace types { + enum FundamentalTypeKind { + Int, + }; + + class Type { + public: + virtual ~Type() = default; + virtual std::string formatted() = 0; + virtual llvm::Type* codegen(codegen::Builder& builder) = 0; + }; + + class FundamentalType : public Type { + private: + FundamentalTypeKind m_ty; + public: + FundamentalType(FundamentalTypeKind kind) : m_ty{ kind } {} + virtual ~FundamentalType() override = default; + virtual std::string formatted() override; + virtual llvm::Type* codegen(codegen::Builder& builder) override; + }; +} + +#endif \ No newline at end of file