GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 94.1% 111 / 0 / 118
Functions: 95.0% 19 / 0 / 20
Branches: 50.0% 102 / 0 / 204

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 2486 StdFunctionManager::StdFunctionManager(SourceFile *sourceFile, GlobalResourceManager &resourceManager, llvm::Module *module)
16
2/2
✓ Branch 2 → 3 taken 4 times.
✓ Branch 2 → 4 taken 2482 times.
2486 : sourceFile(sourceFile), context(resourceManager.cliOptions.useLTO ? resourceManager.ltoContext : sourceFile->context),
17 2486 builder(sourceFile->builder), module(module) {}
18
19 6914 llvm::Function *StdFunctionManager::getPrintfFct() const {
20
3/6
✓ Branch 2 → 3 taken 6914 times.
✗ Branch 2 → 17 not taken.
✓ Branch 4 → 5 taken 6914 times.
✗ Branch 4 → 17 not taken.
✓ Branch 5 → 6 taken 6914 times.
✗ Branch 5 → 17 not taken.
6914 llvm::Function *printfFct = getFunction("printf", builder.getInt32Ty(), builder.getPtrTy(), true);
21 // Set attributes
22 6914 printfFct->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Local);
23 6914 printfFct->addFnAttr(llvm::Attribute::NoFree);
24 6914 printfFct->addFnAttr(llvm::Attribute::NoUnwind);
25 6914 printfFct->addParamAttr(0, llvm::Attribute::getWithCaptureInfo(context, llvm::CaptureInfo::none()));
26 6914 printfFct->addParamAttr(0, llvm::Attribute::NoUndef);
27 6914 printfFct->addParamAttr(0, llvm::Attribute::ReadOnly);
28 6914 printfFct->addRetAttr(llvm::Attribute::NoUndef);
29 6914 return printfFct;
30 }
31
32 2415 llvm::Function *StdFunctionManager::getFPrintfFct() const {
33
4/8
✓ Branch 2 → 3 taken 2415 times.
✗ Branch 2 → 21 not taken.
✓ Branch 3 → 4 taken 2415 times.
✗ Branch 3 → 21 not taken.
✓ Branch 5 → 6 taken 2415 times.
✗ Branch 5 → 21 not taken.
✓ Branch 6 → 7 taken 2415 times.
✗ Branch 6 → 21 not taken.
2415 llvm::Function *fprintfFct = getFunction("fprintf", builder.getInt32Ty(), {builder.getPtrTy(), builder.getPtrTy()}, true);
34 // Set attributes
35 2415 fprintfFct->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Local);
36 2415 fprintfFct->addFnAttr(llvm::Attribute::NoFree);
37 2415 fprintfFct->addParamAttr(0, llvm::Attribute::getWithCaptureInfo(context, llvm::CaptureInfo::none()));
38 2415 fprintfFct->addParamAttr(0, llvm::Attribute::NoUndef);
39 2415 fprintfFct->addParamAttr(1, llvm::Attribute::getWithCaptureInfo(context, llvm::CaptureInfo::none()));
40 2415 fprintfFct->addParamAttr(1, llvm::Attribute::NoUndef);
41 2415 fprintfFct->addParamAttr(1, llvm::Attribute::ReadOnly);
42 2415 fprintfFct->addRetAttr(llvm::Attribute::NoUndef);
43 2415 return fprintfFct;
44 }
45
46 7873 llvm::Function *StdFunctionManager::getExitFct() const {
47
2/4
✓ Branch 2 → 3 taken 7873 times.
✗ Branch 2 → 10 not taken.
✓ Branch 4 → 5 taken 7873 times.
✗ Branch 4 → 10 not taken.
7873 llvm::Function *exitFct = getProcedure("exit", builder.getInt32Ty());
48 // Set attributes
49 7873 exitFct->addFnAttr(llvm::Attribute::Cold);
50 7873 exitFct->addFnAttr(llvm::Attribute::NoReturn);
51 7873 exitFct->addFnAttr(llvm::Attribute::NoUnwind);
52 7873 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 296 llvm::Function *StdFunctionManager::getMemcmpFct() const {
66 296 llvm::Type *ptrTy = builder.getPtrTy();
67
3/6
✓ Branch 3 → 4 taken 296 times.
✗ Branch 3 → 10 not taken.
✓ Branch 5 → 6 taken 296 times.
✗ Branch 5 → 10 not taken.
✓ Branch 6 → 7 taken 296 times.
✗ Branch 6 → 10 not taken.
296 llvm::Function *memcmpFct = getFunction("memcmp", builder.getInt32Ty(), {ptrTy, ptrTy, builder.getInt64Ty()});
68 // Set attributes
69 296 memcmpFct->addFnAttr(llvm::Attribute::NoUnwind);
70 296 return memcmpFct;
71 }
72
73 2691 llvm::Function *StdFunctionManager::getMemcpyIntrinsic() const {
74 2691 llvm::Type *ptrTy = builder.getPtrTy();
75
3/6
✓ Branch 3 → 4 taken 2691 times.
✗ Branch 3 → 13 not taken.
✓ Branch 4 → 5 taken 2691 times.
✗ Branch 4 → 13 not taken.
✓ Branch 6 → 7 taken 2691 times.
✗ Branch 6 → 13 not taken.
2691 llvm::Function *memcpyFct = getProcedure("llvm.memcpy.p0.p0.i64", {ptrTy, ptrTy, builder.getInt64Ty(), builder.getInt1Ty()});
76 // Set attributes
77 2691 memcpyFct->addFnAttr(llvm::Attribute::NoCallback);
78 2691 memcpyFct->addFnAttr(llvm::Attribute::NoFree);
79 2691 memcpyFct->addFnAttr(llvm::Attribute::NoUnwind);
80 2691 memcpyFct->addFnAttr(llvm::Attribute::WillReturn);
81 2691 return memcpyFct;
82 }
83
84 78 llvm::Function *StdFunctionManager::getStringGetRawLengthStringFct() const {
85
2/4
✓ Branch 2 → 3 taken 78 times.
✗ Branch 2 → 34 not taken.
✓ Branch 5 → 6 taken 78 times.
✗ Branch 5 → 31 not taken.
156 const ParamList paramLst = {{QualType(TY_STRING), false}};
86
5/10
✓ Branch 8 → 9 taken 78 times.
✗ Branch 8 → 45 not taken.
✓ Branch 9 → 10 taken 78 times.
✗ Branch 9 → 42 not taken.
✓ Branch 10 → 11 taken 78 times.
✗ Branch 10 → 41 not taken.
✓ Branch 13 → 14 taken 78 times.
✗ Branch 13 → 37 not taken.
✓ Branch 14 → 15 taken 78 times.
✗ Branch 14 → 35 not taken.
234 const Function function("getRawLength", nullptr, QualType(TY_DYN), QualType(TY_LONG), paramLst, {}, nullptr);
87
1/2
✓ Branch 19 → 20 taken 78 times.
✗ Branch 19 → 53 not taken.
78 const std::string mangledName = NameMangling::mangleFunction(function);
88
3/6
✓ Branch 20 → 21 taken 78 times.
✗ Branch 20 → 49 not taken.
✓ Branch 22 → 23 taken 78 times.
✗ Branch 22 → 49 not taken.
✓ Branch 24 → 25 taken 78 times.
✗ Branch 24 → 49 not taken.
156 return getFunction(mangledName.c_str(), builder.getInt64Ty(), {builder.getPtrTy()});
89 78 }
90
91 419 llvm::Function *StdFunctionManager::getStringIsRawEqualStringStringFct() const {
92
3/6
✓ Branch 2 → 3 taken 419 times.
✗ Branch 2 → 36 not taken.
✓ Branch 3 → 4 taken 419 times.
✗ Branch 3 → 36 not taken.
✓ Branch 6 → 7 taken 419 times.
✗ Branch 6 → 33 not taken.
838 const ParamList paramLst = {{QualType(TY_STRING), false}, {QualType(TY_STRING), false}};
93
5/10
✓ Branch 9 → 10 taken 419 times.
✗ Branch 9 → 47 not taken.
✓ Branch 10 → 11 taken 419 times.
✗ Branch 10 → 44 not taken.
✓ Branch 11 → 12 taken 419 times.
✗ Branch 11 → 43 not taken.
✓ Branch 14 → 15 taken 419 times.
✗ Branch 14 → 39 not taken.
✓ Branch 15 → 16 taken 419 times.
✗ Branch 15 → 37 not taken.
1257 const Function function("isRawEqual", nullptr, QualType(TY_DYN), QualType(TY_BOOL), paramLst, {}, nullptr);
94
1/2
✓ Branch 20 → 21 taken 419 times.
✗ Branch 20 → 55 not taken.
419 const std::string mangledName = NameMangling::mangleFunction(function);
95
4/8
✓ Branch 21 → 22 taken 419 times.
✗ Branch 21 → 51 not taken.
✓ Branch 22 → 23 taken 419 times.
✗ Branch 22 → 51 not taken.
✓ Branch 24 → 25 taken 419 times.
✗ Branch 24 → 51 not taken.
✓ Branch 26 → 27 taken 419 times.
✗ Branch 26 → 51 not taken.
838 return getFunction(mangledName.c_str(), builder.getInt1Ty(), {builder.getPtrTy(), builder.getPtrTy()});
96 419 }
97
98 239 llvm::Function *StdFunctionManager::getAllocUnsafeLongFct() const {
99
1/2
✓ Branch 2 → 3 taken 239 times.
✗ Branch 2 → 60 not taken.
239 QualType unsignedLong(TY_LONG);
100
1/2
✓ Branch 3 → 4 taken 239 times.
✗ Branch 3 → 60 not taken.
239 unsignedLong.makeUnsigned();
101
1/2
✓ Branch 6 → 7 taken 239 times.
✗ Branch 6 → 33 not taken.
478 const ParamList paramLst = {{unsignedLong, false}};
102
6/12
✓ Branch 9 → 10 taken 239 times.
✗ Branch 9 → 48 not taken.
✓ Branch 10 → 11 taken 239 times.
✗ Branch 10 → 44 not taken.
✓ Branch 11 → 12 taken 239 times.
✗ Branch 11 → 44 not taken.
✓ Branch 12 → 13 taken 239 times.
✗ Branch 12 → 43 not taken.
✓ Branch 15 → 16 taken 239 times.
✗ Branch 15 → 39 not taken.
✓ Branch 16 → 17 taken 239 times.
✗ Branch 16 → 37 not taken.
717 const Function function("sAllocUnsafe", nullptr, QualType(TY_DYN), QualType(TY_BYTE).toPtr(nullptr), paramLst, {}, nullptr);
103
1/2
✓ Branch 21 → 22 taken 239 times.
✗ Branch 21 → 56 not taken.
239 const std::string mangledName = NameMangling::mangleFunction(function);
104
3/6
✓ Branch 22 → 23 taken 239 times.
✗ Branch 22 → 52 not taken.
✓ Branch 24 → 25 taken 239 times.
✗ Branch 24 → 52 not taken.
✓ Branch 26 → 27 taken 239 times.
✗ Branch 26 → 52 not taken.
478 return getFunction(mangledName.c_str(), builder.getPtrTy(), {builder.getInt64Ty()});
105 239 }
106
107 319 llvm::Function *StdFunctionManager::getDeallocBytePtrRefFct() const {
108
2/4
✓ Branch 2 → 3 taken 319 times.
✗ Branch 2 → 33 not taken.
✓ Branch 3 → 4 taken 319 times.
✗ Branch 3 → 33 not taken.
319 QualType heapBytePtrType = QualType(TY_BYTE).toPtr(nullptr);
109
1/2
✓ Branch 4 → 5 taken 319 times.
✗ Branch 4 → 60 not taken.
319 heapBytePtrType.makeHeap();
110
2/4
✓ Branch 5 → 6 taken 319 times.
✗ Branch 5 → 37 not taken.
✓ Branch 8 → 9 taken 319 times.
✗ Branch 8 → 34 not taken.
638 const ParamList paramLst = {{heapBytePtrType.toRef(nullptr), false}};
111
5/10
✓ Branch 11 → 12 taken 319 times.
✗ Branch 11 → 48 not taken.
✓ Branch 12 → 13 taken 319 times.
✗ Branch 12 → 45 not taken.
✓ Branch 13 → 14 taken 319 times.
✗ Branch 13 → 44 not taken.
✓ Branch 16 → 17 taken 319 times.
✗ Branch 16 → 40 not taken.
✓ Branch 17 → 18 taken 319 times.
✗ Branch 17 → 38 not taken.
957 const Function function("sDealloc", nullptr, QualType(TY_DYN), QualType(TY_DYN), paramLst, {}, nullptr);
112
1/2
✓ Branch 22 → 23 taken 319 times.
✗ Branch 22 → 56 not taken.
319 const std::string mangledName = NameMangling::mangleFunction(function);
113
2/4
✓ Branch 23 → 24 taken 319 times.
✗ Branch 23 → 52 not taken.
✓ Branch 26 → 27 taken 319 times.
✗ Branch 26 → 52 not taken.
638 return getProcedure(mangledName.c_str(), {builder.getPtrTy()});
114 319 }
115
116 18 llvm::Function *StdFunctionManager::getIterateFct(const Function *spiceFunc) const {
117
1/2
✓ Branch 2 → 3 taken 18 times.
✗ Branch 2 → 17 not taken.
18 const std::string functionName = NameMangling::mangleFunction(*spiceFunc);
118
1/2
✓ Branch 3 → 4 taken 18 times.
✗ Branch 3 → 15 not taken.
18 llvm::Type *iteratorType = spiceFunc->returnType.toLLVMType(sourceFile);
119
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()});
120 18 }
121
122 430 llvm::Function *StdFunctionManager::getIteratorFct(const Function *spiceFunc) const {
123
1/2
✓ Branch 2 → 3 taken 430 times.
✗ Branch 2 → 16 not taken.
430 const std::string functionName = NameMangling::mangleFunction(*spiceFunc);
124
1/2
✓ Branch 3 → 4 taken 430 times.
✗ Branch 3 → 14 not taken.
430 llvm::Type *iteratorType = spiceFunc->returnType.toLLVMType(sourceFile);
125
2/4
✓ Branch 4 → 5 taken 430 times.
✗ Branch 4 → 12 not taken.
✓ Branch 7 → 8 taken 430 times.
✗ Branch 7 → 12 not taken.
860 return getFunction(functionName.c_str(), iteratorType, builder.getPtrTy());
126 430 }
127
128 364 llvm::Function *StdFunctionManager::getIteratorGetFct(const Function *spiceFunc) const {
129
1/2
✓ Branch 2 → 3 taken 364 times.
✗ Branch 2 → 16 not taken.
364 const std::string functionName = NameMangling::mangleFunction(*spiceFunc);
130
3/6
✓ Branch 3 → 4 taken 364 times.
✗ Branch 3 → 12 not taken.
✓ Branch 5 → 6 taken 364 times.
✗ Branch 5 → 12 not taken.
✓ Branch 7 → 8 taken 364 times.
✗ Branch 7 → 12 not taken.
728 return getFunction(functionName.c_str(), builder.getPtrTy(), builder.getPtrTy());
131 364 }
132
133 117 llvm::Function *StdFunctionManager::getIteratorGetIdxFct(const Function *spiceFunc) const {
134
1/2
✓ Branch 2 → 3 taken 117 times.
✗ Branch 2 → 16 not taken.
117 const std::string functionName = NameMangling::mangleFunction(*spiceFunc);
135
1/2
✓ Branch 3 → 4 taken 117 times.
✗ Branch 3 → 14 not taken.
117 llvm::Type *pairTy = spiceFunc->returnType.toLLVMType(sourceFile);
136
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());
137 117 }
138
139 481 llvm::Function *StdFunctionManager::getIteratorIsValidFct(const Function *spiceFunc) const {
140
1/2
✓ Branch 2 → 3 taken 481 times.
✗ Branch 2 → 16 not taken.
481 const std::string functionName = NameMangling::mangleFunction(*spiceFunc);
141
3/6
✓ Branch 3 → 4 taken 481 times.
✗ Branch 3 → 12 not taken.
✓ Branch 5 → 6 taken 481 times.
✗ Branch 5 → 12 not taken.
✓ Branch 7 → 8 taken 481 times.
✗ Branch 7 → 12 not taken.
962 return getFunction(functionName.c_str(), builder.getInt1Ty(), builder.getPtrTy());
142 481 }
143
144 481 llvm::Function *StdFunctionManager::getIteratorNextFct(const Function *spiceFunc) const {
145
1/2
✓ Branch 2 → 3 taken 481 times.
✗ Branch 2 → 15 not taken.
481 const std::string functionName = NameMangling::mangleFunction(*spiceFunc);
146
2/4
✓ Branch 3 → 4 taken 481 times.
✗ Branch 3 → 11 not taken.
✓ Branch 6 → 7 taken 481 times.
✗ Branch 6 → 11 not taken.
962 return getProcedure(functionName.c_str(), builder.getPtrTy());
147 481 }
148
149 10 llvm::Function *StdFunctionManager::getAcrtIOFuncFct() const {
150
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());
151 10 stdErrPFct->setDSOLocal(true);
152 10 return stdErrPFct;
153 }
154
155 23145 llvm::Function *StdFunctionManager::getFunction(const char *funcName, llvm::Type *returnType, llvm::ArrayRef<llvm::Type *> args,
156 bool varArg /*=false*/) const {
157 // Check if function already exists in the current module
158
2/4
✓ Branch 2 → 3 taken 23145 times.
✗ Branch 2 → 14 not taken.
✓ Branch 3 → 4 taken 23145 times.
✗ Branch 3 → 14 not taken.
23145 llvm::Function *opFct = module->getFunction(funcName);
159
2/2
✓ Branch 4 → 5 taken 18518 times.
✓ Branch 4 → 6 taken 4627 times.
23145 if (opFct != nullptr)
160 18518 return opFct;
161
162 // Add function to the current module
163 4627 llvm::FunctionType *opFctTy = llvm::FunctionType::get(returnType, args, varArg);
164
2/4
✓ Branch 7 → 8 taken 4627 times.
✗ Branch 7 → 15 not taken.
✓ Branch 8 → 9 taken 4627 times.
✗ Branch 8 → 15 not taken.
4627 module->getOrInsertFunction(funcName, opFctTy);
165
2/4
✓ Branch 9 → 10 taken 4627 times.
✗ Branch 9 → 16 not taken.
✓ Branch 10 → 11 taken 4627 times.
✗ Branch 10 → 16 not taken.
4627 return module->getFunction(funcName);
166 }
167
168 11364 llvm::Function *StdFunctionManager::getProcedure(const char *procName, llvm::ArrayRef<llvm::Type *> args) const {
169 11364 return getFunction(procName, builder.getVoidTy(), args, false);
170 }
171
172 } // namespace spice::compiler
173