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