From 4dd85fd6ecd9a67e5c2b68380873e6e114714355 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sat, 17 Oct 2026 05:30:57 +0000 Subject: feat(memory): add tracked MALLOC and FREE builtins Co-Authored-By: Claude Fable 5.1 --- include/cps/MemoryHandler.h | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 include/cps/MemoryHandler.h (limited to 'include/cps/MemoryHandler.h') diff --git a/include/cps/MemoryHandler.h b/include/cps/MemoryHandler.h new file mode 100644 index 0000000..fd2c93e --- /dev/null +++ b/include/cps/MemoryHandler.h @@ -0,0 +1,42 @@ +#pragma once +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/Module.h" + +namespace cps { + +class TypeSystem; +class RuntimeCheck; +class ExceptionHandler; + +// Only explicit memory builtins own entries in this runtime's allocation list. +// Internal STRING/ARRAY storage retains its existing lifetime rules. +class MemoryHandler { + llvm::LLVMContext &Context; + llvm::IRBuilder<> &Builder; + llvm::Module &Module; + TypeSystem &Types; + RuntimeCheck &Checker; + ExceptionHandler &Exceptions; + llvm::IntegerType *SizeTy = nullptr; + llvm::StructType *NodeTy = nullptr; + llvm::GlobalVariable *Head = nullptr; + llvm::FunctionCallee Malloc, Free; + llvm::Function *AllocateFn = nullptr, *ReleaseFn = nullptr, *FindFn = nullptr; + llvm::Value *AllocationError = nullptr, *TrackingError = nullptr, *InvalidAddress = nullptr; + + void ensureRuntime(); + llvm::Function *createHelper(const char *Name, llvm::ArrayRef Args); + void buildFind(); + void buildAllocate(); + void buildRelease(); + +public: + MemoryHandler(llvm::LLVMContext &C, llvm::IRBuilder<> &B, llvm::Module &M, + TypeSystem &TS, RuntimeCheck &RC, ExceptionHandler &EH) + : Context(C), Builder(B), Module(M), Types(TS), Checker(RC), Exceptions(EH) {} + + llvm::Value *emitAllocate(llvm::Value *Size, int Line); + llvm::Value *emitRelease(llvm::Value *Address, int Line); +}; + +} // namespace cps -- cgit v1.3.1-4-g156e