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 2297 StdFunctionManager::StdFunctionManager(SourceFile *sourceFile, GlobalResourceManager &resourceManager, llvm::Module *module)
16
2/2
✓ Branch 2 → 3 taken 4 times.
✓ Branch 2 → 4 taken 2293 times.
2297 : sourceFile(sourceFile), context(resourceManager.cliOptions.useLTO ? resourceManager.ltoContext : sourceFile->context),
17 2297 builder(sourceFile->builder), module(module) {}
18
19 6136 llvm::Function *StdFunctionManager::getPrintfFct() const {
20
3/6
✓ Branch 2 → 3 taken 6136 times.
✗ Branch 2 → 17 not taken.
✓ Branch 4 → 5 taken 6136 times.
✗ Branch 4 → 17 not taken.
✓ Branch 5 → 6 taken 6136 times.
✗ Branch 5 → 17 not taken.
6136 llvm::Function *printfFct = getFunction("printf", builder.getInt32Ty(), builder.getPtrTy(), true);
21 // Set attributes
22 6136 printfFct->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Local);
23 6136 printfFct->addFnAttr(llvm::Attribute::NoFree);
24 6136 printfFct->addFnAttr(llvm::Attribute::NoUnwind);
25 6136 printfFct->addParamAttr(0, llvm::Attribute::getWithCaptureInfo(context, llvm::CaptureInfo::none()));
26 6136 printfFct->addParamAttr(0, llvm::Attribute::NoUndef);
27 6136 printfFct->addParamAttr(0, llvm::Attribute::ReadOnly);
28 6136 printfFct->addRetAttr(llvm::Attribute::NoUndef);
29 6136 return printfFct;
30 }
31
32 2336 llvm::Function *StdFunctionManager::getFPrintfFct() const {
33
4/8
✓ Branch 2 → 3 taken 2336 times.
✗ Branch 2 → 21 not taken.
✓ Branch 3 → 4 taken 2336 times.
✗ Branch 3 → 21 not taken.
✓ Branch 5 → 6 taken 2336 times.
✗ Branch 5 → 21 not taken.
✓ Branch 6 → 7 taken 2336 times.
✗ Branch 6 → 21 not taken.
2336 llvm::Function *fprintfFct = getFunction("fprintf", builder.getInt32Ty(), {builder.getPtrTy(), builder.getPtrTy()}, true);
34 // Set attributes
35 2336 fprintfFct->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Local);
36 2336 fprintfFct->addFnAttr(llvm::Attribute::NoFree);
37 2336 fprintfFct->addParamAttr(0, llvm::Attribute::getWithCaptureInfo(context, llvm::CaptureInfo::none()));
38 2336 fprintfFct->addParamAttr(0, llvm::Attribute::NoUndef);
39 2336 fprintfFct->addParamAttr(1, llvm::Attribute::getWithCaptureInfo(context, llvm::CaptureInfo::none()));
40 2336 fprintfFct->addParamAttr(1, llvm::Attribute::NoUndef);
41 2336 fprintfFct->addParamAttr(1, llvm::Attribute::ReadOnly);
42 2336 fprintfFct->addRetAttr(llvm::Attribute::NoUndef);
43 2336 return fprintfFct;
44 }
45
46 7084 llvm::Function *StdFunctionManager::getExitFct() const {
47
2/4
✓ Branch 2 → 3 taken 7084 times.
✗ Branch 2 → 10 not taken.
✓ Branch 4 → 5 taken 7084 times.
✗ Branch 4 → 10 not taken.
7084 llvm::Function *exitFct = getProcedure("exit", builder.getInt32Ty());
48 // Set attributes
49 7084 exitFct->addFnAttr(llvm::Attribute::Cold);
50 7084 exitFct->addFnAttr(llvm::Attribute::NoReturn);
51 7084 exitFct->addFnAttr(llvm::Attribute::NoUnwind);
52 7084 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 293 llvm::Function *StdFunctionManager::getMemcmpFct() const {
66 293 llvm::Type *ptrTy = builder.getPtrTy();
67
3/6
✓ Branch 3 → 4 taken 293 times.
✗ Branch 3 → 10 not taken.
✓ Branch 5 → 6 taken 293 times.
✗ Branch 5 → 10 not taken.
✓ Branch 6 → 7 taken 293 times.
✗ Branch 6 → 10 not taken.
293 llvm::Function *memcmpFct = getFunction("memcmp", builder.getInt32Ty(), {ptrTy, ptrTy, builder.getInt64Ty()});
68 // Set attributes
69 293 memcmpFct->addFnAttr(llvm::Attribute::NoUnwind);
70 293 return memcmpFct;
71 }
72
73 2485 llvm::Function *StdFunctionManager::getMemcpyIntrinsic() const {
74 2485 llvm::Type *ptrTy = builder.getPtrTy();
75
3/6
✓ Branch 3 → 4 taken 2485 times.
✗ Branch 3 → 13 not taken.
✓ Branch 4 → 5 taken 2485 times.
✗ Branch 4 → 13 not taken.
✓ Branch 6 → 7 taken 2485 times.
✗ Branch 6 → 13 not taken.
2485 llvm::Function *memcpyFct = getProcedure("llvm.memcpy.p0.p0.i64", {ptrTy, ptrTy, builder.getInt64Ty(), builder.getInt1Ty()});
76 // Set attributes
77 2485 memcpyFct->addFnAttr(llvm::Attribute::NoCallback);
78 2485 memcpyFct->addFnAttr(llvm::Attribute::NoFree);
79 2485 memcpyFct->addFnAttr(llvm::Attribute::NoUnwind);
80 2485 memcpyFct->addFnAttr(llvm::Attribute::WillReturn);
81 2485 return memcpyFct;
82 }
83
84 77 llvm::Function *StdFunctionManager::getStringGetRawLengthStringFct() const {
85
2/4
✓ Branch 2 → 3 taken 77 times.
✗ Branch 2 → 34 not taken.
✓ Branch 5 → 6 taken 77 times.
✗ Branch 5 → 31 not taken.
154 const ParamList paramLst = {{QualType(TY_STRING), false}};
86
5/10
✓ Branch 8 → 9 taken 77 times.
✗ Branch 8 → 45 not taken.
✓ Branch 9 → 10 taken 77 times.
✗ Branch 9 → 42 not taken.
✓ Branch 10 → 11 taken 77 times.
✗ Branch 10 → 41 not taken.
✓ Branch 13 → 14 taken 77 times.
✗ Branch 13 → 37 not taken.
✓ Branch 14 → 15 taken 77 times.
✗ Branch 14 → 35 not taken.
231 const Function function("getRawLength", nullptr, QualType(TY_DYN), QualType(TY_LONG), paramLst, {}, nullptr);
87
1/2
✓ Branch 19 → 20 taken 77 times.
✗ Branch 19 → 53 not taken.
77 const std::string mangledName = NameMangling::mangleFunction(function);
88
3/6
✓ Branch 20 → 21 taken 77 times.
✗ Branch 20 → 49 not taken.
✓ Branch 22 → 23 taken 77 times.
✗ Branch 22 → 49 not taken.
✓ Branch 24 → 25 taken 77 times.
✗ Branch 24 → 49 not taken.
154 return getFunction(mangledName.c_str(), builder.getInt64Ty(), {builder.getPtrTy()});
89 77 }
90
91 381 llvm::Function *StdFunctionManager::getStringIsRawEqualStringStringFct() const {
92
3/6
✓ Branch 2 → 3 taken 381 times.
✗ Branch 2 → 36 not taken.
✓ Branch 3 → 4 taken 381 times.
✗ Branch 3 → 36 not taken.
✓ Branch 6 → 7 taken 381 times.
✗ Branch 6 → 33 not taken.
762 const ParamList paramLst = {{QualType(TY_STRING), false}, {QualType(TY_STRING), false}};
93
5/10
✓ Branch 9 → 10 taken 381 times.
✗ Branch 9 → 47 not taken.
✓ Branch 10 → 11 taken 381 times.
✗ Branch 10 → 44 not taken.
✓ Branch 11 → 12 taken 381 times.
✗ Branch 11 → 43 not taken.
✓ Branch 14 → 15 taken 381 times.
✗ Branch 14 → 39 not taken.
✓ Branch 15 → 16 taken 381 times.
✗ Branch 15 → 37 not taken.
1143 const Function function("isRawEqual", nullptr, QualType(TY_DYN), QualType(TY_BOOL), paramLst, {}, nullptr);
94
1/2
✓ Branch 20 → 21 taken 381 times.
✗ Branch 20 → 55 not taken.
381 const std::string mangledName = NameMangling::mangleFunction(function);
95
4/8
✓ Branch 21 → 22 taken 381 times.
✗ Branch 21 → 51 not taken.
✓ Branch 22 → 23 taken 381 times.
✗ Branch 22 → 51 not taken.
✓ Branch 24 → 25 taken 381 times.
✗ Branch 24 → 51 not taken.
✓ Branch 26 → 27 taken 381 times.
✗ Branch 26 → 51 not taken.
762 return getFunction(mangledName.c_str(), builder.getInt1Ty(), {builder.getPtrTy(), builder.getPtrTy()});
96 381 }
97
98 62 llvm::Function *StdFunctionManager::getAllocUnsafeLongFct() const {
99
1/2
✓ Branch 2 → 3 taken 62 times.
✗ Branch 2 → 60 not taken.
62 QualType unsignedLong(TY_LONG);
100
1/2
✓ Branch 3 → 4 taken 62 times.
✗ Branch 3 → 60 not taken.
62 unsignedLong.makeUnsigned();
101
1/2
✓ Branch 6 → 7 taken 62 times.
✗ Branch 6 → 33 not taken.
124 const ParamList paramLst = {{unsignedLong, false}};
102
6/12
✓ Branch 9 → 10 taken 62 times.
✗ Branch 9 → 48 not taken.
✓ Branch 10 → 11 taken 62 times.
✗ Branch 10 → 44 not taken.
✓ Branch 11 → 12 taken 62 times.
✗ Branch 11 → 44 not taken.
✓ Branch 12 → 13 taken 62 times.
✗ Branch 12 → 43 not taken.
✓ Branch 15 → 16 taken 62 times.
✗ Branch 15 → 39 not taken.
✓ Branch 16 → 17 taken 62 times.
✗ Branch 16 → 37 not taken.
186 const Function function("sAllocUnsafe", nullptr, QualType(TY_DYN), QualType(TY_BYTE).toPtr(nullptr), paramLst, {}, nullptr);
103
1/2
✓ Branch 21 → 22 taken 62 times.
✗ Branch 21 → 56 not taken.
62 const std::string mangledName = NameMangling::mangleFunction(function);
104
3/6
✓ Branch 22 → 23 taken 62 times.
✗ Branch 22 → 52 not taken.
✓ Branch 24 → 25 taken 62 times.
✗ Branch 24 → 52 not taken.
✓ Branch 26 → 27 taken 62 times.
✗ Branch 26 → 52 not taken.
124 return getFunction(mangledName.c_str(), builder.getPtrTy(), {builder.getInt64Ty()});
105 62 }
106
107 141 llvm::Function *StdFunctionManager::getDeallocBytePtrRefFct() const {
108
2/4
✓ Branch 2 → 3 taken 141 times.
✗ Branch 2 → 33 not taken.
✓ Branch 3 → 4 taken 141 times.
✗ Branch 3 → 33 not taken.
141 QualType heapBytePtrType = QualType(TY_BYTE).toPtr(nullptr);
109
1/2
✓ Branch 4 → 5 taken 141 times.
✗ Branch 4 → 60 not taken.
141 heapBytePtrType.makeHeap();
110
2/4
✓ Branch 5 → 6 taken 141 times.
✗ Branch 5 → 37 not taken.
✓ Branch 8 → 9 taken 141 times.
✗ Branch 8 → 34 not taken.
282 const ParamList paramLst = {{heapBytePtrType.toRef(nullptr), false}};
111
5/10
✓ Branch 11 → 12 taken 141 times.
✗ Branch 11 → 48 not taken.
✓ Branch 12 → 13 taken 141 times.
✗ Branch 12 → 45 not taken.
✓ Branch 13 → 14 taken 141 times.
✗ Branch 13 → 44 not taken.
✓ Branch 16 → 17 taken 141 times.
✗ Branch 16 → 40 not taken.
✓ Branch 17 → 18 taken 141 times.
✗ Branch 17 → 38 not taken.
423 const Function function("sDealloc", nullptr, QualType(TY_DYN), QualType(TY_DYN), paramLst, {}, nullptr);
112
1/2
✓ Branch 22 → 23 taken 141 times.
✗ Branch 22 → 56 not taken.
141 const std::string mangledName = NameMangling::mangleFunction(function);
113
2/4
✓ Branch 23 → 24 taken 141 times.
✗ Branch 23 → 52 not taken.
✓ Branch 26 → 27 taken 141 times.
✗ Branch 26 → 52 not taken.
282 return getProcedure(mangledName.c_str(), {builder.getPtrTy()});
114 141 }
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 547 llvm::Function *StdFunctionManager::getIteratorFct(const Function *spiceFunc) const {
123
1/2
✓ Branch 2 → 3 taken 547 times.
✗ Branch 2 → 16 not taken.
547 const std::string functionName = NameMangling::mangleFunction(*spiceFunc);
124
1/2
✓ Branch 3 → 4 taken 547 times.
✗ Branch 3 → 14 not taken.
547 llvm::Type *iteratorType = spiceFunc->returnType.toLLVMType(sourceFile);
125
2/4
✓ Branch 4 → 5 taken 547 times.
✗ Branch 4 → 12 not taken.
✓ Branch 7 → 8 taken 547 times.
✗ Branch 7 → 12 not taken.
1094 return getFunction(functionName.c_str(), iteratorType, builder.getPtrTy());
126 547 }
127
128 481 llvm::Function *StdFunctionManager::getIteratorGetFct(const Function *spiceFunc) const {
129
1/2
✓ Branch 2 → 3 taken 481 times.
✗ Branch 2 → 16 not taken.
481 const std::string functionName = NameMangling::mangleFunction(*spiceFunc);
130
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.getPtrTy(), builder.getPtrTy());
131 481 }
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 598 llvm::Function *StdFunctionManager::getIteratorIsValidFct(const Function *spiceFunc) const {
140
1/2
✓ Branch 2 → 3 taken 598 times.
✗ Branch 2 → 16 not taken.
598 const std::string functionName = NameMangling::mangleFunction(*spiceFunc);
141
3/6
✓ Branch 3 → 4 taken 598 times.
✗ Branch 3 → 12 not taken.
✓ Branch 5 → 6 taken 598 times.
✗ Branch 5 → 12 not taken.
✓ Branch 7 → 8 taken 598 times.
✗ Branch 7 → 12 not taken.
1196 return getFunction(functionName.c_str(), builder.getInt1Ty(), builder.getPtrTy());
142 598 }
143
144 598 llvm::Function *StdFunctionManager::getIteratorNextFct(const Function *spiceFunc) const {
145
1/2
✓ Branch 2 → 3 taken 598 times.
✗ Branch 2 → 15 not taken.
598 const std::string functionName = NameMangling::mangleFunction(*spiceFunc);
146
2/4
✓ Branch 3 → 4 taken 598 times.
✗ Branch 3 → 11 not taken.
✓ Branch 6 → 7 taken 598 times.
✗ Branch 6 → 11 not taken.
1196 return getProcedure(functionName.c_str(), builder.getPtrTy());
147 598 }
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 21364 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 21364 times.
✗ Branch 2 → 14 not taken.
✓ Branch 3 → 4 taken 21364 times.
✗ Branch 3 → 14 not taken.
21364 llvm::Function *opFct = module->getFunction(funcName);
159
2/2
✓ Branch 4 → 5 taken 16879 times.
✓ Branch 4 → 6 taken 4485 times.
21364 if (opFct != nullptr)
160 16879 return opFct;
161
162 // Add function to the current module
163 4485 llvm::FunctionType *opFctTy = llvm::FunctionType::get(returnType, args, varArg);
164
2/4
✓ Branch 7 → 8 taken 4485 times.
✗ Branch 7 → 15 not taken.
✓ Branch 8 → 9 taken 4485 times.
✗ Branch 8 → 15 not taken.
4485 module->getOrInsertFunction(funcName, opFctTy);
165
2/4
✓ Branch 9 → 10 taken 4485 times.
✗ Branch 9 → 16 not taken.
✓ Branch 10 → 11 taken 4485 times.
✗ Branch 10 → 16 not taken.
4485 return module->getFunction(funcName);
166 }
167
168 10308 llvm::Function *StdFunctionManager::getProcedure(const char *procName, llvm::ArrayRef<llvm::Type *> args) const {
169 10308 return getFunction(procName, builder.getVoidTy(), args, false);
170 }
171
172 } // namespace spice::compiler
173