aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen/MemoryGen.cc
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-10-27 13:27:14 +0000
committer_ov4 <ov4@ov4.org>2026-10-27 13:27:14 +0000
commit0dd0b0908300eee08a06ee51d9eb48b85fd97d74 (patch)
tree8e18df810a1d88f686f2b85ed2f05e3aa0985119 /lib/CodeGen/MemoryGen.cc
parent790e0fd7c5a81b9a27a6d84e0f3f4c7a0b17d04b (diff)
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 <noreply@anthropic.com>
Diffstat (limited to 'lib/CodeGen/MemoryGen.cc')
-rw-r--r--lib/CodeGen/MemoryGen.cc30
1 files changed, 30 insertions, 0 deletions
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<int> 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});
+}