From f2b051ae3e28135cc02589f3f698c43743cca48e Mon Sep 17 00:00:00 2001 From: _ov4 Date: Fri, 27 Mar 2026 01:50:32 +0000 Subject: feat: generalize typed array support --- include/cps/ArrayHandler.h | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) (limited to 'include/cps/ArrayHandler.h') diff --git a/include/cps/ArrayHandler.h b/include/cps/ArrayHandler.h index 1ee91db..f15004f 100644 --- a/include/cps/ArrayHandler.h +++ b/include/cps/ArrayHandler.h @@ -4,6 +4,8 @@ #include "llvm/IR/Module.h" #include "cps/AST.h" #include "cps/RuntimeCheck.h" +#include "cps/TypeSystem.h" +#include #include #include #include @@ -11,11 +13,13 @@ namespace cps { struct ArrayMetadata { - int Rank; - llvm::Type* ElementType; + int Rank = 0; + std::string ElementTypeName; + llvm::Type *ElementType = nullptr; + uint64_t ElementSize = 0; std::vector LowerBounds; std::vector UpperBounds; - std::vector Multipliers; + std::vector Multipliers; }; class CodeGen; @@ -25,6 +29,8 @@ class ArrayHandler { llvm::IRBuilder<> *Builder; llvm::Module *TheModule; std::map *NamedValues; + std::map *Symbols; + TypeSystem &Types; std::map ArrayTable; RuntimeCheck &RuntimeChecker; @@ -32,22 +38,31 @@ class ArrayHandler { 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); + llvm::Value *computeFlatIndex(const std::string &Name, const std::vector &Indices); + llvm::Value *getArrayBasePointer(const std::string &Name); + llvm::Value *getElementPointer(const std::string &Name, llvm::Value *Offset); + const ArrayMetadata *getMetadata(const std::string &Name) const; + void emitPrintLoop(const std::string &Name, + int CurrentDim, + std::vector CurrentIndices, + CodeGen &CG); public: - ArrayHandler(llvm::LLVMContext &C, llvm::IRBuilder<> &B, llvm::Module &M, std::map &NV); + ArrayHandler(llvm::LLVMContext &C, + llvm::IRBuilder<> &B, + llvm::Module &M, + std::map &NV, + std::map &Sym, + TypeSystem &TS, + RuntimeCheck &RC); void setupExternalFunctions(); - + void emitArrayDeclare(ArrayDeclareStmtAST *Stmt, CodeGen &CG); void emitArrayAssign(ArrayAssignStmtAST *Stmt, CodeGen &CG); - ArrayHandler(llvm::LLVMContext &C, llvm::IRBuilder<> &B, llvm::Module &M, std::map &NV, RuntimeCheck &RC); - - llvm::Value* emitArrayAccess(ArrayAccessExprAST *Expr, CodeGen &CG); + llvm::Value *emitArrayAccess(ArrayAccessExprAST *Expr, CodeGen &CG); - bool tryEmitArrayOutput(ExprAST *Expr, CodeGen &CG, llvm::FunctionCallee PrintfFunc, llvm::Value* FmtStr); + bool tryEmitArrayOutput(ExprAST *Expr, CodeGen &CG); }; -} +} // namespace cps -- cgit v1.3.1-4-g156e