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