diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-17 05:30:57 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-17 05:30:57 +0000 |
| commit | 4dd85fd6ecd9a67e5c2b68380873e6e114714355 (patch) | |
| tree | b9dad1d0845f4c2ae7e6efed171fbee40ccd1f31 /include/cps | |
| parent | e782898e420c7f2abd83b97c160194d0e70598dd (diff) | |
feat(memory): add tracked MALLOC and FREE builtins
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Diffstat (limited to 'include/cps')
| -rw-r--r-- | include/cps/CodeGen.h | 3 | ||||
| -rw-r--r-- | include/cps/MemoryHandler.h | 42 |
2 files changed, 45 insertions, 0 deletions
diff --git a/include/cps/CodeGen.h b/include/cps/CodeGen.h index 8f84004..94aacb7 100644 --- a/include/cps/CodeGen.h +++ b/include/cps/CodeGen.h @@ -22,6 +22,7 @@ namespace cps { class ArrayHandler; class FileHandler; +class MemoryHandler; class ExceptionHandler; class CastExprAST; @@ -52,6 +53,7 @@ class CodeGen { std::unique_ptr<StringHandler> StrHandler; std::unique_ptr<StringConversionHandler> StrConvHandler; std::unique_ptr<FileHandler> Files; + std::unique_ptr<MemoryHandler> Memory; llvm::FunctionCallee PrintfFunc; llvm::FunctionCallee ScanfFunc; @@ -106,6 +108,7 @@ class CodeGen { bool emitLValue(ExprAST *E, LValueInfo &Out); // The rvalue read: load + the STRING null->"" guard. llvm::Value *loadFromLValue(const LValueInfo &LV); + llvm::Value *emitAllocationSize(ExprAST *Expr); llvm::Value *emitCall(const std::string &Callee, const std::vector<std::unique_ptr<ExprAST>> &ArgExprs, int Line); bool marshalCallArgs(const std::string &Callee, 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<llvm::Type *> 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 |
