aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen/StringHandler.cc
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-07-26 12:12:59 +0000
committer_ov4 <ov4@ov4.org>2026-07-26 12:12:59 +0000
commitb9b7f048b0874df0f034f6b1e15e1a4ee2131475 (patch)
treeccbe2f45205c6e05ac536e6ac73e1d5830769787 /lib/CodeGen/StringHandler.cc
parentfdef2814d0d7e65573d1b666ae9bc561fa35a9f9 (diff)
refactor: centralize libc declarations, move '&' concat into StringHandler
CodeGen declares its own MallocFunc in SetupExternalFunctions and uses it for char->STRING coercion and INPUT STRING buffers, removing two bare getFunction("malloc") calls that null-deref'd if no handler happened to declare malloc first. The CHR branch reuses the char->STRING coercion path instead of a second hand-rolled heap wrapper. String concatenation moves from ArithmeticHandler (which re-declared four externs per '&' and was the LLVM 19 build-pin root cause) to StringHandler::emitConcat; ArithmeticHandler no longer touches the Module. Dead FreeFunc declarations removed. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Diffstat (limited to 'lib/CodeGen/StringHandler.cc')
-rw-r--r--lib/CodeGen/StringHandler.cc25
1 files changed, 22 insertions, 3 deletions
diff --git a/lib/CodeGen/StringHandler.cc b/lib/CodeGen/StringHandler.cc
index e5971d0..56b5aea 100644
--- a/lib/CodeGen/StringHandler.cc
+++ b/lib/CodeGen/StringHandler.cc
@@ -16,9 +16,6 @@ void StringHandler::setupExternalFunctions() {
FunctionType *MallocType = FunctionType::get(PointerType::getUnqual(Context), {Type::getInt64Ty(Context)}, false);
MallocFunc = Module.getOrInsertFunction("malloc", MallocType);
- FunctionType *FreeType = FunctionType::get(Type::getVoidTy(Context), {PointerType::getUnqual(Context)}, false);
- FreeFunc = Module.getOrInsertFunction("free", FreeType);
-
FunctionType *StrLenType = FunctionType::get(Type::getInt64Ty(Context), {PointerType::getUnqual(Context)}, false);
StrLenFunc = Module.getOrInsertFunction("strlen", StrLenType);
@@ -26,6 +23,12 @@ void StringHandler::setupExternalFunctions() {
FunctionType *MemCpyType = FunctionType::get(PointerType::getUnqual(Context), MemCpyArgs, false);
MemCpyFunc = Module.getOrInsertFunction("memcpy", MemCpyType);
+ FunctionType *StrCpyType = FunctionType::get(PointerType::getUnqual(Context),
+ {PointerType::getUnqual(Context), PointerType::getUnqual(Context)},
+ false);
+ StrCpyFunc = Module.getOrInsertFunction("strcpy", StrCpyType);
+ StrCatFunc = Module.getOrInsertFunction("strcat", StrCpyType);
+
FunctionType *CharCaseType = FunctionType::get(Type::getInt32Ty(Context), {Type::getInt32Ty(Context)}, false);
ToUpperFunc = Module.getOrInsertFunction("toupper", CharCaseType);
ToLowerFunc = Module.getOrInsertFunction("tolower", CharCaseType);
@@ -50,6 +53,22 @@ Value *StringHandler::emitLength(Value *Str) {
return Builder.CreateCall(StrLenFunc, Str, "len");
}
+Value *StringHandler::emitConcat(Value *LHS, Value *RHS) {
+ if (!LHS || !RHS) return nullptr;
+
+ Value *LLen = Builder.CreateCall(StrLenFunc, {LHS}, "llen");
+ Value *RLen = Builder.CreateCall(StrLenFunc, {RHS}, "rlen");
+ Value *TotalLen = Builder.CreateAdd(LLen, RLen, "totallen");
+ Value *AllocSize = Builder.CreateAdd(TotalLen, ConstantInt::get(Type::getInt64Ty(Context), 1), "allocsize");
+
+ Value *NewStr = Builder.CreateCall(MallocFunc, {AllocSize}, "concat_str");
+
+ Builder.CreateCall(StrCpyFunc, {NewStr, LHS});
+ Builder.CreateCall(StrCatFunc, {NewStr, RHS});
+
+ return NewStr;
+}
+
Value *StringHandler::emitMid(Value *Str, Value *Start, Value *Len) {
Value *FullLen = emitLength(Str);