From cc7b9b34e99fa17357e14ac69f5fb2d1535b5512 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Tue, 3 Mar 2026 07:22:19 +0000 Subject: Added all built-in functions --- lib/CodeGen/ArithmeticHandler.cc | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'lib/CodeGen/ArithmeticHandler.cc') diff --git a/lib/CodeGen/ArithmeticHandler.cc b/lib/CodeGen/ArithmeticHandler.cc index df83513..585104d 100644 --- a/lib/CodeGen/ArithmeticHandler.cc +++ b/lib/CodeGen/ArithmeticHandler.cc @@ -11,6 +11,32 @@ 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; -- cgit v1.3.1-4-g156e