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