From 0dd0b0908300eee08a06ee51d9eb48b85fd97d74 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Tue, 27 Oct 2026 13:27:14 +0000 Subject: refactor(memory)!: move allocation functions into memory.cps Route keyword NEW and DELETE through the same library runtime and retain allocation tracking, runtime diagnostics and exception behavior. BREAKING CHANGE: -no-std programs using NEW or DELETE must explicitly import memory.cps. Co-Authored-By: Claude Opus 5.5 --- lib/CodeGen/MemoryGen.cc | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 lib/CodeGen/MemoryGen.cc (limited to 'lib/CodeGen/MemoryGen.cc') diff --git a/lib/CodeGen/MemoryGen.cc b/lib/CodeGen/MemoryGen.cc new file mode 100644 index 0000000..453f137 --- /dev/null +++ b/lib/CodeGen/MemoryGen.cc @@ -0,0 +1,30 @@ +#include "cps/CodeGen.h" + +using namespace llvm; +using namespace cps; + +// Keyword NEW/DELETE use the same visible C helpers as memory.cps. Ordinary +// MALLOC/REALLOC/FREE calls are resolved entirely through the library. +Value *CodeGen::emitMemoryCall(const char *Name, Value *Argument, int Line) { + SaveAndRestore Location(DiagnosticLine, Line); + auto Declarations = FuncGen->getOverloads(Name); + if (Declarations.empty()) { + reportError("NEW/DELETE requires memory.cps; include the memory library"); + return nullptr; + } + const FuncSig &Sig = *Declarations.front(); + bool Allocate = sameIdentifier(Name, "_memory_impl_malloc"); + bool Validate = sameIdentifier(Name, "_memory_impl_validate"); + const TypeInfo *Parameter = resolveType(Allocate ? "C_TYPE(\"unsigned long long\")" + : "C_TYPE(\"void*\")"); + const TypeInfo *Result = resolveType(Validate ? "VOID" : "C_TYPE(\"void*\")"); + if (!Sig.IsExternal || Sig.IsVariadic || Sig.Params.size() != 1 || + Sig.Params[0].IsArray || Sig.Params[0].IsByRef || + !Types->sameType(Sig.Params[0].TypeName, Parameter->Name) || + !Types->sameType(Sig.ReturnTypeName, Result->Name)) { + reportError("Invalid memory runtime declaration for %s", Name); + return nullptr; + } + if (Allocate) Argument = Builder->CreateIntCast(Argument, Parameter->LLVMType, false); + return FuncGen->emitCall(Sig, {Argument}); +} -- cgit v1.3.1-4-g156e