From 943ed04b775949e8bc4948bcb8d762a32ab14f76 Mon Sep 17 00:00:00 2001 From: ov-4 Date: Thu, 29 Jan 2026 14:24:05 +0000 Subject: Added array --- include/cps/ArrayHandler.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 include/cps/ArrayHandler.h (limited to 'include/cps/ArrayHandler.h') diff --git a/include/cps/ArrayHandler.h b/include/cps/ArrayHandler.h new file mode 100644 index 0000000..5052de4 --- /dev/null +++ b/include/cps/ArrayHandler.h @@ -0,0 +1,50 @@ +#pragma once +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" +#include "cps/AST.h" +#include +#include +#include + +namespace cps { + +struct ArrayMetadata { + int Rank; + llvm::Type* ElementType; + std::vector LowerBounds; + std::vector UpperBounds; + std::vector Multipliers; +}; + +class CodeGen; + +class ArrayHandler { + llvm::LLVMContext *TheContext; + llvm::IRBuilder<> *Builder; + llvm::Module *TheModule; + std::map *NamedValues; + + std::map ArrayTable; + + llvm::FunctionCallee MallocFunc; + llvm::FunctionCallee FreeFunc; + + llvm::Value* computeFlatIndex(const std::string &Name, const std::vector &Indices); + + void emitPrintLoop(const std::string &Name, int CurrentDim, std::vector CurrentIndices, llvm::FunctionCallee PrintfFunc, llvm::Value* FmtStr); + +public: + ArrayHandler(llvm::LLVMContext &C, llvm::IRBuilder<> &B, llvm::Module &M, std::map &NV); + + void setupExternalFunctions(); + + void emitArrayDeclare(ArrayDeclareStmtAST *Stmt, CodeGen &CG); + void emitArrayAssign(ArrayAssignStmtAST *Stmt, CodeGen &CG); + + llvm::Value* emitArrayAccess(ArrayAccessExprAST *Expr, CodeGen &CG); + + bool tryEmitArrayOutput(ExprAST *Expr, CodeGen &CG, llvm::FunctionCallee PrintfFunc, llvm::Value* FmtStr); +}; + +} -- cgit v1.3.1-4-g156e