GCC Code Coverage Report


Directory: ../
File: src/irgenerator/StdFunctionManager.cpp
Date: 2025-10-09 06:28:01
Coverage Exec Excl Total
Lines: 90.4% 103 0 114
Functions: 90.0% 18 0 20
Branches: 48.5% 94 0 194

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