src/irgenerator/StdFunctionManager.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "StdFunctionManager.h" | ||
| 4 | |||
| 5 | #include <SourceFile.h> | ||
| 6 | #include <driver/Driver.h> | ||
| 7 | #include <global/GlobalResourceManager.h> | ||
| 8 | #include <irgenerator/NameMangling.h> | ||
| 9 | #include <model/Function.h> | ||
| 10 | |||
| 11 | #include <llvm/IR/Module.h> | ||
| 12 | |||
| 13 | namespace spice::compiler { | ||
| 14 | |||
| 15 | 2028 | StdFunctionManager::StdFunctionManager(SourceFile *sourceFile, GlobalResourceManager &resourceManager, llvm::Module *module) | |
| 16 |
2/2✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 4 taken 2026 times.
|
2028 | : sourceFile(sourceFile), context(resourceManager.cliOptions.useLTO ? resourceManager.ltoContext : sourceFile->context), |
| 17 | 2028 | builder(sourceFile->builder), module(module) {} | |
| 18 | |||
| 19 | 5706 | llvm::Function *StdFunctionManager::getPrintfFct() const { | |
| 20 |
3/6✓ Branch 2 → 3 taken 5706 times.
✗ Branch 2 → 17 not taken.
✓ Branch 4 → 5 taken 5706 times.
✗ Branch 4 → 17 not taken.
✓ Branch 5 → 6 taken 5706 times.
✗ Branch 5 → 17 not taken.
|
5706 | llvm::Function *printfFct = getFunction("printf", builder.getInt32Ty(), builder.getPtrTy(), true); |
| 21 | // Set attributes | ||
| 22 | 5706 | printfFct->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Local); | |
| 23 | 5706 | printfFct->addFnAttr(llvm::Attribute::NoFree); | |
| 24 | 5706 | printfFct->addFnAttr(llvm::Attribute::NoUnwind); | |
| 25 | 5706 | printfFct->addParamAttr(0, llvm::Attribute::getWithCaptureInfo(context, llvm::CaptureInfo::none())); | |
| 26 | 5706 | printfFct->addParamAttr(0, llvm::Attribute::NoUndef); | |
| 27 | 5706 | printfFct->addParamAttr(0, llvm::Attribute::ReadOnly); | |
| 28 | 5706 | printfFct->addRetAttr(llvm::Attribute::NoUndef); | |
| 29 | 5706 | return printfFct; | |
| 30 | } | ||
| 31 | |||
| 32 | 2127 | llvm::Function *StdFunctionManager::getFPrintfFct() const { | |
| 33 |
4/8✓ Branch 2 → 3 taken 2127 times.
✗ Branch 2 → 21 not taken.
✓ Branch 3 → 4 taken 2127 times.
✗ Branch 3 → 21 not taken.
✓ Branch 5 → 6 taken 2127 times.
✗ Branch 5 → 21 not taken.
✓ Branch 6 → 7 taken 2127 times.
✗ Branch 6 → 21 not taken.
|
2127 | llvm::Function *fprintfFct = getFunction("fprintf", builder.getInt32Ty(), {builder.getPtrTy(), builder.getPtrTy()}, true); |
| 34 | // Set attributes | ||
| 35 | 2127 | fprintfFct->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Local); | |
| 36 | 2127 | fprintfFct->addFnAttr(llvm::Attribute::NoFree); | |
| 37 | 2127 | fprintfFct->addParamAttr(0, llvm::Attribute::getWithCaptureInfo(context, llvm::CaptureInfo::none())); | |
| 38 | 2127 | fprintfFct->addParamAttr(0, llvm::Attribute::NoUndef); | |
| 39 | 2127 | fprintfFct->addParamAttr(1, llvm::Attribute::getWithCaptureInfo(context, llvm::CaptureInfo::none())); | |
| 40 | 2127 | fprintfFct->addParamAttr(1, llvm::Attribute::NoUndef); | |
| 41 | 2127 | fprintfFct->addParamAttr(1, llvm::Attribute::ReadOnly); | |
| 42 | 2127 | fprintfFct->addRetAttr(llvm::Attribute::NoUndef); | |
| 43 | 2127 | return fprintfFct; | |
| 44 | } | ||
| 45 | |||
| 46 | 6660 | llvm::Function *StdFunctionManager::getExitFct() const { | |
| 47 |
2/4✓ Branch 2 → 3 taken 6660 times.
✗ Branch 2 → 10 not taken.
✓ Branch 4 → 5 taken 6660 times.
✗ Branch 4 → 10 not taken.
|
6660 | llvm::Function *exitFct = getProcedure("exit", builder.getInt32Ty()); |
| 48 | // Set attributes | ||
| 49 | 6660 | exitFct->addFnAttr(llvm::Attribute::Cold); | |
| 50 | 6660 | exitFct->addFnAttr(llvm::Attribute::NoReturn); | |
| 51 | 6660 | exitFct->addFnAttr(llvm::Attribute::NoUnwind); | |
| 52 | 6660 | return exitFct; | |
| 53 | } | ||
| 54 | |||
| 55 | ✗ | llvm::Function *StdFunctionManager::getFreeFct() const { | |
| 56 | ✗ | llvm::Function *freeFct = getProcedure("free", builder.getPtrTy()); | |
| 57 | // Set attributes | ||
| 58 | ✗ | freeFct->addFnAttr(llvm::Attribute::NoUnwind); | |
| 59 | ✗ | freeFct->addParamAttr(0, llvm::Attribute::getWithCaptureInfo(context, llvm::CaptureInfo::none())); | |
| 60 | ✗ | freeFct->addParamAttr(0, llvm::Attribute::NoUndef); | |
| 61 | ✗ | freeFct->addParamAttr(0, llvm::Attribute::ReadOnly); | |
| 62 | ✗ | return freeFct; | |
| 63 | } | ||
| 64 | |||
| 65 | 247 | llvm::Function *StdFunctionManager::getMemcmpFct() const { | |
| 66 | 247 | llvm::Type *ptrTy = builder.getPtrTy(); | |
| 67 |
3/6✓ Branch 3 → 4 taken 247 times.
✗ Branch 3 → 10 not taken.
✓ Branch 5 → 6 taken 247 times.
✗ Branch 5 → 10 not taken.
✓ Branch 6 → 7 taken 247 times.
✗ Branch 6 → 10 not taken.
|
247 | llvm::Function *memcmpFct = getFunction("memcmp", builder.getInt32Ty(), {ptrTy, ptrTy, builder.getInt64Ty()}); |
| 68 | // Set attributes | ||
| 69 | 247 | memcmpFct->addFnAttr(llvm::Attribute::NoUnwind); | |
| 70 | 247 | return memcmpFct; | |
| 71 | } | ||
| 72 | |||
| 73 | 2140 | llvm::Function *StdFunctionManager::getMemcpyIntrinsic() const { | |
| 74 | 2140 | llvm::Type *ptrTy = builder.getPtrTy(); | |
| 75 |
3/6✓ Branch 3 → 4 taken 2140 times.
✗ Branch 3 → 13 not taken.
✓ Branch 4 → 5 taken 2140 times.
✗ Branch 4 → 13 not taken.
✓ Branch 6 → 7 taken 2140 times.
✗ Branch 6 → 13 not taken.
|
2140 | llvm::Function *memcpyFct = getProcedure("llvm.memcpy.p0.p0.i64", {ptrTy, ptrTy, builder.getInt64Ty(), builder.getInt1Ty()}); |
| 76 | // Set attributes | ||
| 77 | 2140 | memcpyFct->addFnAttr(llvm::Attribute::NoCallback); | |
| 78 | 2140 | memcpyFct->addFnAttr(llvm::Attribute::NoFree); | |
| 79 | 2140 | memcpyFct->addFnAttr(llvm::Attribute::NoUnwind); | |
| 80 | 2140 | memcpyFct->addFnAttr(llvm::Attribute::WillReturn); | |
| 81 | 2140 | return memcpyFct; | |
| 82 | } | ||
| 83 | |||
| 84 | 73 | llvm::Function *StdFunctionManager::getStringGetRawLengthStringFct() const { | |
| 85 |
2/4✓ Branch 2 → 3 taken 73 times.
✗ Branch 2 → 34 not taken.
✓ Branch 5 → 6 taken 73 times.
✗ Branch 5 → 31 not taken.
|
146 | const ParamList paramLst = {{QualType(TY_STRING), false}}; |
| 86 |
5/10✓ Branch 8 → 9 taken 73 times.
✗ Branch 8 → 45 not taken.
✓ Branch 9 → 10 taken 73 times.
✗ Branch 9 → 42 not taken.
✓ Branch 10 → 11 taken 73 times.
✗ Branch 10 → 41 not taken.
✓ Branch 13 → 14 taken 73 times.
✗ Branch 13 → 37 not taken.
✓ Branch 14 → 15 taken 73 times.
✗ Branch 14 → 35 not taken.
|
219 | const Function function("getRawLength", nullptr, QualType(TY_DYN), QualType(TY_LONG), paramLst, {}, nullptr); |
| 87 |
1/2✓ Branch 19 → 20 taken 73 times.
✗ Branch 19 → 53 not taken.
|
73 | const std::string mangledName = NameMangling::mangleFunction(function); |
| 88 |
3/6✓ Branch 20 → 21 taken 73 times.
✗ Branch 20 → 49 not taken.
✓ Branch 22 → 23 taken 73 times.
✗ Branch 22 → 49 not taken.
✓ Branch 24 → 25 taken 73 times.
✗ Branch 24 → 49 not taken.
|
146 | return getFunction(mangledName.c_str(), builder.getInt64Ty(), {builder.getPtrTy()}); |
| 89 | 73 | } | |
| 90 | |||
| 91 | 362 | llvm::Function *StdFunctionManager::getStringIsRawEqualStringStringFct() const { | |
| 92 |
3/6✓ Branch 2 → 3 taken 362 times.
✗ Branch 2 → 36 not taken.
✓ Branch 3 → 4 taken 362 times.
✗ Branch 3 → 36 not taken.
✓ Branch 6 → 7 taken 362 times.
✗ Branch 6 → 33 not taken.
|
724 | const ParamList paramLst = {{QualType(TY_STRING), false}, {QualType(TY_STRING), false}}; |
| 93 |
5/10✓ Branch 9 → 10 taken 362 times.
✗ Branch 9 → 47 not taken.
✓ Branch 10 → 11 taken 362 times.
✗ Branch 10 → 44 not taken.
✓ Branch 11 → 12 taken 362 times.
✗ Branch 11 → 43 not taken.
✓ Branch 14 → 15 taken 362 times.
✗ Branch 14 → 39 not taken.
✓ Branch 15 → 16 taken 362 times.
✗ Branch 15 → 37 not taken.
|
1086 | const Function function("isRawEqual", nullptr, QualType(TY_DYN), QualType(TY_BOOL), paramLst, {}, nullptr); |
| 94 |
1/2✓ Branch 20 → 21 taken 362 times.
✗ Branch 20 → 55 not taken.
|
362 | const std::string mangledName = NameMangling::mangleFunction(function); |
| 95 |
4/8✓ Branch 21 → 22 taken 362 times.
✗ Branch 21 → 51 not taken.
✓ Branch 22 → 23 taken 362 times.
✗ Branch 22 → 51 not taken.
✓ Branch 24 → 25 taken 362 times.
✗ Branch 24 → 51 not taken.
✓ Branch 26 → 27 taken 362 times.
✗ Branch 26 → 51 not taken.
|
724 | return getFunction(mangledName.c_str(), builder.getInt1Ty(), {builder.getPtrTy(), builder.getPtrTy()}); |
| 96 | 362 | } | |
| 97 | |||
| 98 | 70 | llvm::Function *StdFunctionManager::getAllocUnsafeLongFct() const { | |
| 99 |
1/2✓ Branch 2 → 3 taken 70 times.
✗ Branch 2 → 60 not taken.
|
70 | QualType unsignedLong(TY_LONG); |
| 100 |
1/2✓ Branch 3 → 4 taken 70 times.
✗ Branch 3 → 60 not taken.
|
70 | unsignedLong.makeUnsigned(); |
| 101 |
1/2✓ Branch 6 → 7 taken 70 times.
✗ Branch 6 → 33 not taken.
|
140 | const ParamList paramLst = {{unsignedLong, false}}; |
| 102 |
6/12✓ Branch 9 → 10 taken 70 times.
✗ Branch 9 → 48 not taken.
✓ Branch 10 → 11 taken 70 times.
✗ Branch 10 → 44 not taken.
✓ Branch 11 → 12 taken 70 times.
✗ Branch 11 → 44 not taken.
✓ Branch 12 → 13 taken 70 times.
✗ Branch 12 → 43 not taken.
✓ Branch 15 → 16 taken 70 times.
✗ Branch 15 → 39 not taken.
✓ Branch 16 → 17 taken 70 times.
✗ Branch 16 → 37 not taken.
|
210 | const Function function("sAllocUnsafe", nullptr, QualType(TY_DYN), QualType(TY_BYTE).toPtr(nullptr), paramLst, {}, nullptr); |
| 103 |
1/2✓ Branch 21 → 22 taken 70 times.
✗ Branch 21 → 56 not taken.
|
70 | const std::string mangledName = NameMangling::mangleFunction(function); |
| 104 |
3/6✓ Branch 22 → 23 taken 70 times.
✗ Branch 22 → 52 not taken.
✓ Branch 24 → 25 taken 70 times.
✗ Branch 24 → 52 not taken.
✓ Branch 26 → 27 taken 70 times.
✗ Branch 26 → 52 not taken.
|
140 | return getFunction(mangledName.c_str(), builder.getPtrTy(), {builder.getInt64Ty()}); |
| 105 | 70 | } | |
| 106 | |||
| 107 | 315 | llvm::Function *StdFunctionManager::getDeallocBytePtrRefFct() const { | |
| 108 |
4/8✓ Branch 2 → 3 taken 315 times.
✗ Branch 2 → 35 not taken.
✓ Branch 3 → 4 taken 315 times.
✗ Branch 3 → 35 not taken.
✓ Branch 4 → 5 taken 315 times.
✗ Branch 4 → 35 not taken.
✓ Branch 7 → 8 taken 315 times.
✗ Branch 7 → 32 not taken.
|
630 | const ParamList paramLst = {{QualType(TY_BYTE).toPtr(nullptr).toRef(nullptr), false}}; |
| 109 |
5/10✓ Branch 10 → 11 taken 315 times.
✗ Branch 10 → 48 not taken.
✓ Branch 11 → 12 taken 315 times.
✗ Branch 11 → 45 not taken.
✓ Branch 12 → 13 taken 315 times.
✗ Branch 12 → 44 not taken.
✓ Branch 15 → 16 taken 315 times.
✗ Branch 15 → 40 not taken.
✓ Branch 16 → 17 taken 315 times.
✗ Branch 16 → 38 not taken.
|
945 | const Function function("sDealloc", nullptr, QualType(TY_DYN), QualType(TY_DYN), paramLst, {}, nullptr); |
| 110 |
1/2✓ Branch 21 → 22 taken 315 times.
✗ Branch 21 → 56 not taken.
|
315 | const std::string mangledName = NameMangling::mangleFunction(function); |
| 111 |
2/4✓ Branch 22 → 23 taken 315 times.
✗ Branch 22 → 52 not taken.
✓ Branch 25 → 26 taken 315 times.
✗ Branch 25 → 52 not taken.
|
630 | return getProcedure(mangledName.c_str(), {builder.getPtrTy()}); |
| 112 | 315 | } | |
| 113 | |||
| 114 | 18 | llvm::Function *StdFunctionManager::getIterateFct(const Function *spiceFunc) const { | |
| 115 |
1/2✓ Branch 2 → 3 taken 18 times.
✗ Branch 2 → 17 not taken.
|
18 | const std::string functionName = NameMangling::mangleFunction(*spiceFunc); |
| 116 |
1/2✓ Branch 3 → 4 taken 18 times.
✗ Branch 3 → 15 not taken.
|
18 | llvm::Type *iteratorType = spiceFunc->returnType.toLLVMType(sourceFile); |
| 117 |
3/6✓ Branch 4 → 5 taken 18 times.
✗ Branch 4 → 13 not taken.
✓ Branch 5 → 6 taken 18 times.
✗ Branch 5 → 13 not taken.
✓ Branch 8 → 9 taken 18 times.
✗ Branch 8 → 13 not taken.
|
36 | return getFunction(functionName.c_str(), iteratorType, {builder.getPtrTy(), builder.getInt64Ty()}); |
| 118 | 18 | } | |
| 119 | |||
| 120 | 538 | llvm::Function *StdFunctionManager::getIteratorFct(const Function *spiceFunc) const { | |
| 121 |
1/2✓ Branch 2 → 3 taken 538 times.
✗ Branch 2 → 16 not taken.
|
538 | const std::string functionName = NameMangling::mangleFunction(*spiceFunc); |
| 122 |
1/2✓ Branch 3 → 4 taken 538 times.
✗ Branch 3 → 14 not taken.
|
538 | llvm::Type *iteratorType = spiceFunc->returnType.toLLVMType(sourceFile); |
| 123 |
2/4✓ Branch 4 → 5 taken 538 times.
✗ Branch 4 → 12 not taken.
✓ Branch 7 → 8 taken 538 times.
✗ Branch 7 → 12 not taken.
|
1076 | return getFunction(functionName.c_str(), iteratorType, builder.getPtrTy()); |
| 124 | 538 | } | |
| 125 | |||
| 126 | 468 | llvm::Function *StdFunctionManager::getIteratorGetFct(const Function *spiceFunc) const { | |
| 127 |
1/2✓ Branch 2 → 3 taken 468 times.
✗ Branch 2 → 16 not taken.
|
468 | const std::string functionName = NameMangling::mangleFunction(*spiceFunc); |
| 128 |
3/6✓ Branch 3 → 4 taken 468 times.
✗ Branch 3 → 12 not taken.
✓ Branch 5 → 6 taken 468 times.
✗ Branch 5 → 12 not taken.
✓ Branch 7 → 8 taken 468 times.
✗ Branch 7 → 12 not taken.
|
936 | return getFunction(functionName.c_str(), builder.getPtrTy(), builder.getPtrTy()); |
| 129 | 468 | } | |
| 130 | |||
| 131 | 117 | llvm::Function *StdFunctionManager::getIteratorGetIdxFct(const Function *spiceFunc) const { | |
| 132 |
1/2✓ Branch 2 → 3 taken 117 times.
✗ Branch 2 → 16 not taken.
|
117 | const std::string functionName = NameMangling::mangleFunction(*spiceFunc); |
| 133 |
1/2✓ Branch 3 → 4 taken 117 times.
✗ Branch 3 → 14 not taken.
|
117 | llvm::Type *pairTy = spiceFunc->returnType.toLLVMType(sourceFile); |
| 134 |
2/4✓ Branch 4 → 5 taken 117 times.
✗ Branch 4 → 12 not taken.
✓ Branch 7 → 8 taken 117 times.
✗ Branch 7 → 12 not taken.
|
234 | return getFunction(functionName.c_str(), pairTy, builder.getPtrTy()); |
| 135 | 117 | } | |
| 136 | |||
| 137 | 585 | llvm::Function *StdFunctionManager::getIteratorIsValidFct(const Function *spiceFunc) const { | |
| 138 |
1/2✓ Branch 2 → 3 taken 585 times.
✗ Branch 2 → 16 not taken.
|
585 | const std::string functionName = NameMangling::mangleFunction(*spiceFunc); |
| 139 |
3/6✓ Branch 3 → 4 taken 585 times.
✗ Branch 3 → 12 not taken.
✓ Branch 5 → 6 taken 585 times.
✗ Branch 5 → 12 not taken.
✓ Branch 7 → 8 taken 585 times.
✗ Branch 7 → 12 not taken.
|
1170 | return getFunction(functionName.c_str(), builder.getInt1Ty(), builder.getPtrTy()); |
| 140 | 585 | } | |
| 141 | |||
| 142 | 585 | llvm::Function *StdFunctionManager::getIteratorNextFct(const Function *spiceFunc) const { | |
| 143 |
1/2✓ Branch 2 → 3 taken 585 times.
✗ Branch 2 → 15 not taken.
|
585 | const std::string functionName = NameMangling::mangleFunction(*spiceFunc); |
| 144 |
2/4✓ Branch 3 → 4 taken 585 times.
✗ Branch 3 → 11 not taken.
✓ Branch 6 → 7 taken 585 times.
✗ Branch 6 → 11 not taken.
|
1170 | return getProcedure(functionName.c_str(), builder.getPtrTy()); |
| 145 | 585 | } | |
| 146 | |||
| 147 | 10 | llvm::Function *StdFunctionManager::getAcrtIOFuncFct() const { | |
| 148 |
3/6✓ Branch 2 → 3 taken 10 times.
✗ Branch 2 → 9 not taken.
✓ Branch 4 → 5 taken 10 times.
✗ Branch 4 → 9 not taken.
✓ Branch 5 → 6 taken 10 times.
✗ Branch 5 → 9 not taken.
|
10 | llvm::Function *stdErrPFct = getFunction("__acrt_iob_func", builder.getPtrTy(), builder.getInt32Ty()); |
| 149 | 10 | stdErrPFct->setDSOLocal(true); | |
| 150 | 10 | return stdErrPFct; | |
| 151 | } | ||
| 152 | |||
| 153 | 20021 | llvm::Function *StdFunctionManager::getFunction(const char *funcName, llvm::Type *returnType, llvm::ArrayRef<llvm::Type *> args, | |
| 154 | bool varArg /*=false*/) const { | ||
| 155 | // Check if function already exists in the current module | ||
| 156 |
2/4✓ Branch 2 → 3 taken 20021 times.
✗ Branch 2 → 14 not taken.
✓ Branch 3 → 4 taken 20021 times.
✗ Branch 3 → 14 not taken.
|
20021 | llvm::Function *opFct = module->getFunction(funcName); |
| 157 |
2/2✓ Branch 4 → 5 taken 15832 times.
✓ Branch 4 → 6 taken 4189 times.
|
20021 | if (opFct != nullptr) |
| 158 | 15832 | return opFct; | |
| 159 | |||
| 160 | // Add function to the current module | ||
| 161 | 4189 | llvm::FunctionType *opFctTy = llvm::FunctionType::get(returnType, args, varArg); | |
| 162 |
2/4✓ Branch 7 → 8 taken 4189 times.
✗ Branch 7 → 15 not taken.
✓ Branch 8 → 9 taken 4189 times.
✗ Branch 8 → 15 not taken.
|
4189 | module->getOrInsertFunction(funcName, opFctTy); |
| 163 |
2/4✓ Branch 9 → 10 taken 4189 times.
✗ Branch 9 → 16 not taken.
✓ Branch 10 → 11 taken 4189 times.
✗ Branch 10 → 16 not taken.
|
4189 | return module->getFunction(funcName); |
| 164 | } | ||
| 165 | |||
| 166 | 9700 | llvm::Function *StdFunctionManager::getProcedure(const char *procName, llvm::ArrayRef<llvm::Type *> args) const { | |
| 167 | 9700 | return getFunction(procName, builder.getVoidTy(), args, false); | |
| 168 | } | ||
| 169 | |||
| 170 | } // namespace spice::compiler | ||
| 171 |