GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 94.0% 109 / 0 / 116
Functions: 95.0% 19 / 0 / 20
Branches: 50.0% 101 / 0 / 202

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 2153 StdFunctionManager::StdFunctionManager(SourceFile *sourceFile, GlobalResourceManager &resourceManager, llvm::Module *module)
16
2/2
✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 4 taken 2151 times.
2153 : sourceFile(sourceFile), context(resourceManager.cliOptions.useLTO ? resourceManager.ltoContext : sourceFile->context),
17 2153 builder(sourceFile->builder), module(module) {}
18
19 5859 llvm::Function *StdFunctionManager::getPrintfFct() const {
20
3/6
✓ Branch 2 → 3 taken 5859 times.
✗ Branch 2 → 17 not taken.
✓ Branch 4 → 5 taken 5859 times.
✗ Branch 4 → 17 not taken.
✓ Branch 5 → 6 taken 5859 times.
✗ Branch 5 → 17 not taken.
5859 llvm::Function *printfFct = getFunction("printf", builder.getInt32Ty(), builder.getPtrTy(), true);
21 // Set attributes
22 5859 printfFct->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Local);
23 5859 printfFct->addFnAttr(llvm::Attribute::NoFree);
24 5859 printfFct->addFnAttr(llvm::Attribute::NoUnwind);
25 5859 printfFct->addParamAttr(0, llvm::Attribute::getWithCaptureInfo(context, llvm::CaptureInfo::none()));
26 5859 printfFct->addParamAttr(0, llvm::Attribute::NoUndef);
27 5859 printfFct->addParamAttr(0, llvm::Attribute::ReadOnly);
28 5859 printfFct->addRetAttr(llvm::Attribute::NoUndef);
29 5859 return printfFct;
30 }
31
32 2207 llvm::Function *StdFunctionManager::getFPrintfFct() const {
33
4/8
✓ Branch 2 → 3 taken 2207 times.
✗ Branch 2 → 21 not taken.
✓ Branch 3 → 4 taken 2207 times.
✗ Branch 3 → 21 not taken.
✓ Branch 5 → 6 taken 2207 times.
✗ Branch 5 → 21 not taken.
✓ Branch 6 → 7 taken 2207 times.
✗ Branch 6 → 21 not taken.
2207 llvm::Function *fprintfFct = getFunction("fprintf", builder.getInt32Ty(), {builder.getPtrTy(), builder.getPtrTy()}, true);
34 // Set attributes
35 2207 fprintfFct->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Local);
36 2207 fprintfFct->addFnAttr(llvm::Attribute::NoFree);
37 2207 fprintfFct->addParamAttr(0, llvm::Attribute::getWithCaptureInfo(context, llvm::CaptureInfo::none()));
38 2207 fprintfFct->addParamAttr(0, llvm::Attribute::NoUndef);
39 2207 fprintfFct->addParamAttr(1, llvm::Attribute::getWithCaptureInfo(context, llvm::CaptureInfo::none()));
40 2207 fprintfFct->addParamAttr(1, llvm::Attribute::NoUndef);
41 2207 fprintfFct->addParamAttr(1, llvm::Attribute::ReadOnly);
42 2207 fprintfFct->addRetAttr(llvm::Attribute::NoUndef);
43 2207 return fprintfFct;
44 }
45
46 6869 llvm::Function *StdFunctionManager::getExitFct() const {
47
2/4
✓ Branch 2 → 3 taken 6869 times.
✗ Branch 2 → 10 not taken.
✓ Branch 4 → 5 taken 6869 times.
✗ Branch 4 → 10 not taken.
6869 llvm::Function *exitFct = getProcedure("exit", builder.getInt32Ty());
48 // Set attributes
49 6869 exitFct->addFnAttr(llvm::Attribute::Cold);
50 6869 exitFct->addFnAttr(llvm::Attribute::NoReturn);
51 6869 exitFct->addFnAttr(llvm::Attribute::NoUnwind);
52 6869 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 2440 llvm::Function *StdFunctionManager::getMemcpyIntrinsic() const {
74 2440 llvm::Type *ptrTy = builder.getPtrTy();
75
3/6
✓ Branch 3 → 4 taken 2440 times.
✗ Branch 3 → 13 not taken.
✓ Branch 4 → 5 taken 2440 times.
✗ Branch 4 → 13 not taken.
✓ Branch 6 → 7 taken 2440 times.
✗ Branch 6 → 13 not taken.
2440 llvm::Function *memcpyFct = getProcedure("llvm.memcpy.p0.p0.i64", {ptrTy, ptrTy, builder.getInt64Ty(), builder.getInt1Ty()});
76 // Set attributes
77 2440 memcpyFct->addFnAttr(llvm::Attribute::NoCallback);
78 2440 memcpyFct->addFnAttr(llvm::Attribute::NoFree);
79 2440 memcpyFct->addFnAttr(llvm::Attribute::NoUnwind);
80 2440 memcpyFct->addFnAttr(llvm::Attribute::WillReturn);
81 2440 return memcpyFct;
82 }
83
84 74 llvm::Function *StdFunctionManager::getStringGetRawLengthStringFct() const {
85
2/4
✓ Branch 2 → 3 taken 74 times.
✗ Branch 2 → 34 not taken.
✓ Branch 5 → 6 taken 74 times.
✗ Branch 5 → 31 not taken.
148 const ParamList paramLst = {{QualType(TY_STRING), false}};
86
5/10
✓ Branch 8 → 9 taken 74 times.
✗ Branch 8 → 45 not taken.
✓ Branch 9 → 10 taken 74 times.
✗ Branch 9 → 42 not taken.
✓ Branch 10 → 11 taken 74 times.
✗ Branch 10 → 41 not taken.
✓ Branch 13 → 14 taken 74 times.
✗ Branch 13 → 37 not taken.
✓ Branch 14 → 15 taken 74 times.
✗ Branch 14 → 35 not taken.
222 const Function function("getRawLength", nullptr, QualType(TY_DYN), QualType(TY_LONG), paramLst, {}, nullptr);
87
1/2
✓ Branch 19 → 20 taken 74 times.
✗ Branch 19 → 53 not taken.
74 const std::string mangledName = NameMangling::mangleFunction(function);
88
3/6
✓ Branch 20 → 21 taken 74 times.
✗ Branch 20 → 49 not taken.
✓ Branch 22 → 23 taken 74 times.
✗ Branch 22 → 49 not taken.
✓ Branch 24 → 25 taken 74 times.
✗ Branch 24 → 49 not taken.
148 return getFunction(mangledName.c_str(), builder.getInt64Ty(), {builder.getPtrTy()});
89 74 }
90
91 373 llvm::Function *StdFunctionManager::getStringIsRawEqualStringStringFct() const {
92
3/6
✓ Branch 2 → 3 taken 373 times.
✗ Branch 2 → 36 not taken.
✓ Branch 3 → 4 taken 373 times.
✗ Branch 3 → 36 not taken.
✓ Branch 6 → 7 taken 373 times.
✗ Branch 6 → 33 not taken.
746 const ParamList paramLst = {{QualType(TY_STRING), false}, {QualType(TY_STRING), false}};
93
5/10
✓ Branch 9 → 10 taken 373 times.
✗ Branch 9 → 47 not taken.
✓ Branch 10 → 11 taken 373 times.
✗ Branch 10 → 44 not taken.
✓ Branch 11 → 12 taken 373 times.
✗ Branch 11 → 43 not taken.
✓ Branch 14 → 15 taken 373 times.
✗ Branch 14 → 39 not taken.
✓ Branch 15 → 16 taken 373 times.
✗ Branch 15 → 37 not taken.
1119 const Function function("isRawEqual", nullptr, QualType(TY_DYN), QualType(TY_BOOL), paramLst, {}, nullptr);
94
1/2
✓ Branch 20 → 21 taken 373 times.
✗ Branch 20 → 55 not taken.
373 const std::string mangledName = NameMangling::mangleFunction(function);
95
4/8
✓ Branch 21 → 22 taken 373 times.
✗ Branch 21 → 51 not taken.
✓ Branch 22 → 23 taken 373 times.
✗ Branch 22 → 51 not taken.
✓ Branch 24 → 25 taken 373 times.
✗ Branch 24 → 51 not taken.
✓ Branch 26 → 27 taken 373 times.
✗ Branch 26 → 51 not taken.
746 return getFunction(mangledName.c_str(), builder.getInt1Ty(), {builder.getPtrTy(), builder.getPtrTy()});
96 373 }
97
98 89 llvm::Function *StdFunctionManager::getAllocUnsafeLongFct() const {
99
1/2
✓ Branch 2 → 3 taken 89 times.
✗ Branch 2 → 60 not taken.
89 QualType unsignedLong(TY_LONG);
100
1/2
✓ Branch 3 → 4 taken 89 times.
✗ Branch 3 → 60 not taken.
89 unsignedLong.makeUnsigned();
101
1/2
✓ Branch 6 → 7 taken 89 times.
✗ Branch 6 → 33 not taken.
178 const ParamList paramLst = {{unsignedLong, false}};
102
6/12
✓ Branch 9 → 10 taken 89 times.
✗ Branch 9 → 48 not taken.
✓ Branch 10 → 11 taken 89 times.
✗ Branch 10 → 44 not taken.
✓ Branch 11 → 12 taken 89 times.
✗ Branch 11 → 44 not taken.
✓ Branch 12 → 13 taken 89 times.
✗ Branch 12 → 43 not taken.
✓ Branch 15 → 16 taken 89 times.
✗ Branch 15 → 39 not taken.
✓ Branch 16 → 17 taken 89 times.
✗ Branch 16 → 37 not taken.
267 const Function function("sAllocUnsafe", nullptr, QualType(TY_DYN), QualType(TY_BYTE).toPtr(nullptr), paramLst, {}, nullptr);
103
1/2
✓ Branch 21 → 22 taken 89 times.
✗ Branch 21 → 56 not taken.
89 const std::string mangledName = NameMangling::mangleFunction(function);
104
3/6
✓ Branch 22 → 23 taken 89 times.
✗ Branch 22 → 52 not taken.
✓ Branch 24 → 25 taken 89 times.
✗ Branch 24 → 52 not taken.
✓ Branch 26 → 27 taken 89 times.
✗ Branch 26 → 52 not taken.
178 return getFunction(mangledName.c_str(), builder.getPtrTy(), {builder.getInt64Ty()});
105 89 }
106
107 325 llvm::Function *StdFunctionManager::getDeallocBytePtrRefFct() const {
108
4/8
✓ Branch 2 → 3 taken 325 times.
✗ Branch 2 → 35 not taken.
✓ Branch 3 → 4 taken 325 times.
✗ Branch 3 → 35 not taken.
✓ Branch 4 → 5 taken 325 times.
✗ Branch 4 → 35 not taken.
✓ Branch 7 → 8 taken 325 times.
✗ Branch 7 → 32 not taken.
650 const ParamList paramLst = {{QualType(TY_BYTE).toPtr(nullptr).toRef(nullptr), false}};
109
5/10
✓ Branch 10 → 11 taken 325 times.
✗ Branch 10 → 48 not taken.
✓ Branch 11 → 12 taken 325 times.
✗ Branch 11 → 45 not taken.
✓ Branch 12 → 13 taken 325 times.
✗ Branch 12 → 44 not taken.
✓ Branch 15 → 16 taken 325 times.
✗ Branch 15 → 40 not taken.
✓ Branch 16 → 17 taken 325 times.
✗ Branch 16 → 38 not taken.
975 const Function function("sDealloc", nullptr, QualType(TY_DYN), QualType(TY_DYN), paramLst, {}, nullptr);
110
1/2
✓ Branch 21 → 22 taken 325 times.
✗ Branch 21 → 56 not taken.
325 const std::string mangledName = NameMangling::mangleFunction(function);
111
2/4
✓ Branch 22 → 23 taken 325 times.
✗ Branch 22 → 52 not taken.
✓ Branch 25 → 26 taken 325 times.
✗ Branch 25 → 52 not taken.
650 return getProcedure(mangledName.c_str(), {builder.getPtrTy()});
112 325 }
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 539 llvm::Function *StdFunctionManager::getIteratorFct(const Function *spiceFunc) const {
121
1/2
✓ Branch 2 → 3 taken 539 times.
✗ Branch 2 → 16 not taken.
539 const std::string functionName = NameMangling::mangleFunction(*spiceFunc);
122
1/2
✓ Branch 3 → 4 taken 539 times.
✗ Branch 3 → 14 not taken.
539 llvm::Type *iteratorType = spiceFunc->returnType.toLLVMType(sourceFile);
123
2/4
✓ Branch 4 → 5 taken 539 times.
✗ Branch 4 → 12 not taken.
✓ Branch 7 → 8 taken 539 times.
✗ Branch 7 → 12 not taken.
1078 return getFunction(functionName.c_str(), iteratorType, builder.getPtrTy());
124 539 }
125
126 469 llvm::Function *StdFunctionManager::getIteratorGetFct(const Function *spiceFunc) const {
127
1/2
✓ Branch 2 → 3 taken 469 times.
✗ Branch 2 → 16 not taken.
469 const std::string functionName = NameMangling::mangleFunction(*spiceFunc);
128
3/6
✓ Branch 3 → 4 taken 469 times.
✗ Branch 3 → 12 not taken.
✓ Branch 5 → 6 taken 469 times.
✗ Branch 5 → 12 not taken.
✓ Branch 7 → 8 taken 469 times.
✗ Branch 7 → 12 not taken.
938 return getFunction(functionName.c_str(), builder.getPtrTy(), builder.getPtrTy());
129 469 }
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 586 llvm::Function *StdFunctionManager::getIteratorIsValidFct(const Function *spiceFunc) const {
138
1/2
✓ Branch 2 → 3 taken 586 times.
✗ Branch 2 → 16 not taken.
586 const std::string functionName = NameMangling::mangleFunction(*spiceFunc);
139
3/6
✓ Branch 3 → 4 taken 586 times.
✗ Branch 3 → 12 not taken.
✓ Branch 5 → 6 taken 586 times.
✗ Branch 5 → 12 not taken.
✓ Branch 7 → 8 taken 586 times.
✗ Branch 7 → 12 not taken.
1172 return getFunction(functionName.c_str(), builder.getInt1Ty(), builder.getPtrTy());
140 586 }
141
142 586 llvm::Function *StdFunctionManager::getIteratorNextFct(const Function *spiceFunc) const {
143
1/2
✓ Branch 2 → 3 taken 586 times.
✗ Branch 2 → 15 not taken.
586 const std::string functionName = NameMangling::mangleFunction(*spiceFunc);
144
2/4
✓ Branch 3 → 4 taken 586 times.
✗ Branch 3 → 11 not taken.
✓ Branch 6 → 7 taken 586 times.
✗ Branch 6 → 11 not taken.
1172 return getProcedure(functionName.c_str(), builder.getPtrTy());
145 586 }
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 20854 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 20854 times.
✗ Branch 2 → 14 not taken.
✓ Branch 3 → 4 taken 20854 times.
✗ Branch 3 → 14 not taken.
20854 llvm::Function *opFct = module->getFunction(funcName);
157
2/2
✓ Branch 4 → 5 taken 16504 times.
✓ Branch 4 → 6 taken 4350 times.
20854 if (opFct != nullptr)
158 16504 return opFct;
159
160 // Add function to the current module
161 4350 llvm::FunctionType *opFctTy = llvm::FunctionType::get(returnType, args, varArg);
162
2/4
✓ Branch 7 → 8 taken 4350 times.
✗ Branch 7 → 15 not taken.
✓ Branch 8 → 9 taken 4350 times.
✗ Branch 8 → 15 not taken.
4350 module->getOrInsertFunction(funcName, opFctTy);
163
2/4
✓ Branch 9 → 10 taken 4350 times.
✗ Branch 9 → 16 not taken.
✓ Branch 10 → 11 taken 4350 times.
✗ Branch 10 → 16 not taken.
4350 return module->getFunction(funcName);
164 }
165
166 10220 llvm::Function *StdFunctionManager::getProcedure(const char *procName, llvm::ArrayRef<llvm::Type *> args) const {
167 10220 return getFunction(procName, builder.getVoidTy(), args, false);
168 }
169
170 } // namespace spice::compiler
171