diff options
| author | _ov4 <ov4@ov4.org> | 2026-07-26 12:12:59 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-07-26 12:12:59 +0000 |
| commit | b9b7f048b0874df0f034f6b1e15e1a4ee2131475 (patch) | |
| tree | ccbe2f45205c6e05ac536e6ac73e1d5830769787 /lib/CodeGen | |
| parent | fdef2814d0d7e65573d1b666ae9bc561fa35a9f9 (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')
| -rw-r--r-- | lib/CodeGen/ArithmeticHandler.cc | 26 | ||||
| -rw-r--r-- | lib/CodeGen/ArrayHandler.cc | 5 | ||||
| -rw-r--r-- | lib/CodeGen/CodeGen.cc | 32 | ||||
| -rw-r--r-- | lib/CodeGen/StringHandler.cc | 25 |
4 files changed, 33 insertions, 55 deletions
diff --git a/lib/CodeGen/ArithmeticHandler.cc b/lib/CodeGen/ArithmeticHandler.cc index b909abc..bfee395 100644 --- a/lib/CodeGen/ArithmeticHandler.cc +++ b/lib/CodeGen/ArithmeticHandler.cc @@ -11,32 +11,6 @@ Value *ArithmeticHandler::emitBinaryOp(int Op, Value *LHS, Value *RHS, int Line) bool LIsDouble = LHS->getType()->isDoubleTy(); bool RIsDouble = RHS->getType()->isDoubleTy(); - if (Op == '&') { - Module *M = Builder.GetInsertBlock()->getParent()->getParent(); - - FunctionType *StrLenTy = FunctionType::get(Type::getInt64Ty(Context), {PointerType::getUnqual(Context)}, false); - FunctionCallee StrLenF = M->getOrInsertFunction("strlen", StrLenTy); - - FunctionType *MallocTy = FunctionType::get(PointerType::getUnqual(Context), {Type::getInt64Ty(Context)}, false); - FunctionCallee MallocF = M->getOrInsertFunction("malloc", MallocTy); - - FunctionType *StrCpyTy = FunctionType::get(PointerType::getUnqual(Context), {PointerType::getUnqual(Context), PointerType::getUnqual(Context)}, false); - FunctionCallee StrCpyF = M->getOrInsertFunction("strcpy", StrCpyTy); - FunctionCallee StrCatF = M->getOrInsertFunction("strcat", StrCpyTy); - - Value *LLen = Builder.CreateCall(StrLenF, {LHS}, "llen"); - Value *RLen = Builder.CreateCall(StrLenF, {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(MallocF, {AllocSize}, "concat_str"); - - Builder.CreateCall(StrCpyF, {NewStr, LHS}); - Builder.CreateCall(StrCatF, {NewStr, RHS}); - - return NewStr; - } - if (Op == '/') { Value *LVal = LHS; Value *RVal = RHS; diff --git a/lib/CodeGen/ArrayHandler.cc b/lib/CodeGen/ArrayHandler.cc index aa7230a..af16692 100644 --- a/lib/CodeGen/ArrayHandler.cc +++ b/lib/CodeGen/ArrayHandler.cc @@ -32,11 +32,6 @@ void ArrayHandler::setupExternalFunctions() { FunctionType *MallocType = FunctionType::get(PointerType::getUnqual(*TheContext), MallocArgs, false); MallocFunc = TheModule->getOrInsertFunction("malloc", MallocType); - - std::vector<Type*> FreeArgs; - FreeArgs.push_back(PointerType::getUnqual(*TheContext)); - FunctionType *FreeType = FunctionType::get(Type::getVoidTy(*TheContext), FreeArgs, false); - FreeFunc = TheModule->getOrInsertFunction("free", FreeType); } const ArrayMetadata *ArrayHandler::getMetadata(const std::string &Name) const { diff --git a/lib/CodeGen/CodeGen.cc b/lib/CodeGen/CodeGen.cc index 1c019d9..de64a15 100644 --- a/lib/CodeGen/CodeGen.cc +++ b/lib/CodeGen/CodeGen.cc @@ -99,6 +99,11 @@ void CodeGen::SetupExternalFunctions() { FunctionType *ScanfType = FunctionType::get(Type::getInt32Ty(*TheContext), PrintfArgs, true); ScanfFunc = TheModule->getOrInsertFunction("scanf", ScanfType); + FunctionType *MallocType = FunctionType::get(PointerType::getUnqual(*TheContext), + {Type::getInt64Ty(*TheContext)}, + false); + MallocFunc = TheModule->getOrInsertFunction("malloc", MallocType); + PrintfFormatStr = Builder->CreateGlobalStringPtr("%lld\n", "fmt_nl", 0, TheModule.get()); PrintfFloatFormatStr = Builder->CreateGlobalStringPtr("%f\n", "fmt_flt", 0, TheModule.get()); PrintfStringFormatStr = Builder->CreateGlobalStringPtr("%s\n", "fmt_str", 0, TheModule.get()); @@ -291,17 +296,7 @@ Value *CodeGen::coerceValueToType(Value *Val, const TypeInfo *TargetInfo) { return StrConvHandler->emitNumToStr(Val, true); } if (Val->getType()->isIntegerTy(8)) { - Function *MallocF = TheModule->getFunction("malloc"); - if (!MallocF) { - FunctionType *MallocTy = FunctionType::get(PointerType::getUnqual(*TheContext), - {Type::getInt64Ty(*TheContext)}, - false); - MallocF = Function::Create(MallocTy, - Function::ExternalLinkage, - "malloc", - TheModule.get()); - } - Value *Mem = Builder->CreateCall(MallocF, + Value *Mem = Builder->CreateCall(MallocFunc, ConstantInt::get(*TheContext, APInt(64, 2)), "char_str"); Builder->CreateStore(Val, Mem); @@ -511,6 +506,9 @@ Value *CodeGen::emitExpr(ExprAST *Expr) { Value *L = emitExpr(Bin->getLHS()); Value *R = emitExpr(Bin->getRHS()); if (!L || !R) return nullptr; + if (Bin->getOp() == '&') { + return StrHandler->emitConcat(L, R); + } return ArithHandler->emitBinaryOp(Bin->getOp(), L, R, Bin->getLine()); } @@ -565,14 +563,7 @@ Value *CodeGen::emitExpr(ExprAST *Expr) { if (Name == "CHR") { Value *IntVal = coerceValueToType(emitExpr(Call->getArgs()[0].get()), resolveType("INTEGER")); Value *CharVal = ChrHandler->emitChr(IntVal); - Function *MallocF = TheModule->getFunction("malloc"); - Value *Mem = Builder->CreateCall(MallocF, ConstantInt::get(*TheContext, APInt(64, 2))); - Builder->CreateStore(CharVal, Mem); - Value *NullPtr = Builder->CreateInBoundsGEP(Type::getInt8Ty(*TheContext), - Mem, - ConstantInt::get(*TheContext, APInt(64, 1))); - Builder->CreateStore(ConstantInt::get(Type::getInt8Ty(*TheContext), 0), NullPtr); - return Mem; + return coerceValueToType(CharVal, resolveType("STRING")); } if (Name == "IS_NUM") { return StrConvHandler->emitIsNum(emitExpr(Call->getArgs()[0].get())); @@ -873,8 +864,7 @@ void CodeGen::emitStmt(StmtAST *Stmt) { Value *BoolVal = Builder->CreateICmpNE(Val, ConstantInt::get(*TheContext, APInt(64, 0)), "bool_cast"); Builder->CreateStore(BoolVal, Info->Storage); } else if (TypeInfo->isString()) { - Function *MallocF = TheModule->getFunction("malloc"); - Value *Mem = Builder->CreateCall(MallocF, ConstantInt::get(*TheContext, APInt(64, 1024)), "input_str"); + Value *Mem = Builder->CreateCall(MallocFunc, ConstantInt::get(*TheContext, APInt(64, 1024)), "input_str"); Args.push_back(ScanfStringFormatStr); Args.push_back(Mem); Builder->CreateCall(ScanfFunc, Args); 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); |
