GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 90.6% 433 / 0 / 478
Functions: 96.2% 25 / 0 / 26
Branches: 49.3% 541 / 0 / 1098

src/irgenerator/GenImplicit.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "IRGenerator.h"
4
5 #include <SourceFile.h>
6 #include <ast/ASTNodes.h>
7 #include <ast/Attributes.h>
8 #include <driver/Driver.h>
9 #include <global/GlobalResourceManager.h>
10 #include <model/Function.h>
11 #include <symboltablebuilder/SymbolTableBuilder.h>
12 #include <typechecker/FunctionManager.h>
13
14 #include <llvm/IR/Module.h>
15
16 namespace spice::compiler {
17
18 // String placeholders for builtin testing output
19 static const char *const TEST_ALL_START_MSG = "[==========] Running %d test(s) from %d source file(s)\n";
20 static const char *const TEST_ALL_END_MSG = "[==========] Ran %d test(s) from %d source file(s)\n";
21 static const char *const TEST_FILE_START_MSG = "[----------] Running %d test(s) from %s\n";
22 static const char *const TEST_FILE_END_MSG = "[----------] Ran %d test(s) from %s\n\n";
23 static const char *const TEST_CASE_RUN_MSG = "[ RUN ] %s\n";
24 static const char *const TEST_CASE_SUCCESS_MSG = "\033[1m\033[32m[ PASSED ]\033[0m\033[22m %s\n";
25 static const char *const TEST_CASE_FAILED_MSG = "\033[1m\033[31m[ FAILED ]\033[0m\033[22m %s\n";
26 static const char *const TEST_CASE_SKIPPED_MSG = "\033[1m\033[33m[ SKIPPED ]\033[0m\033[22m %s\n";
27
28 35 llvm::Value *IRGenerator::doImplicitCast(llvm::Value *src, QualType dstSTy, QualType srcSTy) {
29
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 35 times.
35 assert(srcSTy != dstSTy); // We only need to cast implicitly, if the types do not match exactly
30
31 // Unpack the pointers until a pointer of another type is met
32 35 size_t loadCounter = 0;
33
1/2
✗ Branch 16 → 6 not taken.
✓ Branch 16 → 17 taken 35 times.
35 while (srcSTy.isPtr()) {
34 src = insertLoad(srcSTy, src);
35 srcSTy = srcSTy.getContained();
36 dstSTy = dstSTy.getContained();
37 loadCounter++;
38 }
39 // GEP or bit-cast
40
3/6
✓ Branch 18 → 19 taken 35 times.
✗ Branch 18 → 22 not taken.
✓ Branch 20 → 21 taken 35 times.
✗ Branch 20 → 22 not taken.
✓ Branch 23 → 24 taken 35 times.
✗ Branch 23 → 35 not taken.
35 if (dstSTy.isArray() && srcSTy.isArray()) { // Special case that is used for passing arrays as pointer to functions
41
2/4
✓ Branch 24 → 25 taken 35 times.
✗ Branch 24 → 72 not taken.
✓ Branch 25 → 26 taken 35 times.
✗ Branch 25 → 72 not taken.
35 llvm::Value *indices[2] = {builder.getInt64(0), builder.getInt32(0)};
42
2/4
✓ Branch 30 → 31 taken 35 times.
✗ Branch 30 → 65 not taken.
✓ Branch 31 → 32 taken 35 times.
✗ Branch 31 → 65 not taken.
35 src = insertInBoundsGEP(srcSTy.toLLVMType(sourceFile), src, indices);
43 } else {
44 src = insertLoad(srcSTy, src);
45 src = builder.CreateBitCast(src, dstSTy.toLLVMType(sourceFile));
46 }
47 // Pack the pointers together again
48
1/2
✗ Branch 54 → 46 not taken.
✓ Branch 54 → 55 taken 35 times.
35 for (; loadCounter > 0; loadCounter--) {
49 llvm::Value *newActualArg = insertAlloca(srcSTy);
50 insertStore(src, newActualArg);
51 src = newActualArg;
52 }
53 35 return src;
54 }
55
56 53595 void IRGenerator::generateScopeCleanup(const StmtLstNode *node) const {
57 // Do not clean up if the block is already terminated
58
2/2
✓ Branch 2 → 3 taken 19010 times.
✓ Branch 2 → 4 taken 34585 times.
53595 if (blockAlreadyTerminated)
59 19010 return;
60
61 // Call all dtor functions
62 34585 const auto &[dtorFunctionsToCall, heapVarsToFree] = node->resourcesToCleanup.at(manIdx);
63
2/2
✓ Branch 23 → 7 taken 2015 times.
✓ Branch 23 → 24 taken 34585 times.
73200 for (auto [entry, dtor] : dtorFunctionsToCall)
64
1/2
✓ Branch 12 → 13 taken 2015 times.
✗ Branch 12 → 62 not taken.
2015 generateCtorOrDtorCall(entry, dtor, {});
65
66 // Deallocate all heap variables that go out of scope and are currently owned
67
2/2
✓ Branch 39 → 26 taken 19 times.
✓ Branch 39 → 40 taken 34585 times.
69189 for (const SymbolTableEntry *entry : heapVarsToFree)
68
2/4
✓ Branch 28 → 29 taken 19 times.
✗ Branch 28 → 66 not taken.
✓ Branch 29 → 30 taken 19 times.
✗ Branch 29 → 66 not taken.
19 generateDeallocCall(entry->getAddress());
69
70 // Generate lifetime end markers
71
2/2
✓ Branch 40 → 41 taken 28 times.
✓ Branch 40 → 61 taken 34557 times.
34585 if (cliOptions.useLifetimeMarkers) {
72
3/4
✓ Branch 41 → 42 taken 28 times.
✗ Branch 41 → 69 not taken.
✓ Branch 58 → 44 taken 8 times.
✓ Branch 58 → 59 taken 28 times.
64 for (const SymbolTableEntry *var : currentScope->getVarsGoingOutOfScope()) {
73
1/2
✓ Branch 46 → 47 taken 8 times.
✗ Branch 46 → 67 not taken.
8 llvm::Value *address = var->getAddress();
74
1/2
✓ Branch 47 → 48 taken 8 times.
✗ Branch 47 → 49 not taken.
8 if (address != nullptr)
75
1/2
✓ Branch 48 → 49 taken 8 times.
✗ Branch 48 → 67 not taken.
8 builder.CreateLifetimeEnd(address);
76 28 }
77 }
78 }
79
80 3665 void IRGenerator::generateFctDecl(const Function *fct, const std::vector<llvm::Value *> &args) const {
81 // Retrieve metadata for the function
82
1/2
✓ Branch 2 → 3 taken 3665 times.
✗ Branch 2 → 90 not taken.
3665 const std::string mangledName = fct->getMangledName();
83
84 // Function is not defined in the current module -> declare it
85
3/4
✓ Branch 4 → 5 taken 3665 times.
✗ Branch 4 → 71 not taken.
✓ Branch 5 → 6 taken 1002 times.
✓ Branch 5 → 69 taken 2663 times.
3665 if (!module->getFunction(mangledName)) {
86 1002 std::vector<llvm::Type *> paramTypes;
87
2/2
✓ Branch 21 → 8 taken 1305 times.
✓ Branch 21 → 22 taken 1002 times.
3309 for (const llvm::Value *argValue : args)
88
1/2
✓ Branch 11 → 12 taken 1305 times.
✗ Branch 11 → 72 not taken.
1305 paramTypes.push_back(argValue->getType());
89
2/6
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 28 taken 1002 times.
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 85 not taken.
✓ Branch 28 → 29 taken 1002 times.
✗ Branch 28 → 85 not taken.
1002 llvm::Type *returnType = fct->isFunction() ? fct->returnType.toLLVMType(sourceFile) : builder.getVoidTy();
90
1/2
✓ Branch 31 → 32 taken 1002 times.
✗ Branch 31 → 74 not taken.
1002 llvm::FunctionType *fctType = llvm::FunctionType::get(returnType, paramTypes, false);
91
2/4
✓ Branch 33 → 34 taken 1002 times.
✗ Branch 33 → 75 not taken.
✓ Branch 34 → 35 taken 1002 times.
✗ Branch 34 → 85 not taken.
1002 module->getOrInsertFunction(mangledName, fctType);
92
93
1/2
✓ Branch 37 → 38 taken 1002 times.
✗ Branch 37 → 67 not taken.
1002 if (fct->isMethod()) {
94 // Get callee function
95
1/2
✓ Branch 39 → 40 taken 1002 times.
✗ Branch 39 → 76 not taken.
1002 llvm::Function *callee = module->getFunction(mangledName);
96
1/2
✗ Branch 40 → 41 not taken.
✓ Branch 40 → 42 taken 1002 times.
1002 assert(callee != nullptr);
97
98 // Set attributes to 'this' param
99 // Get 'this' entry
100
1/2
✓ Branch 44 → 45 taken 1002 times.
✗ Branch 44 → 79 not taken.
3006 const SymbolTableEntry *thisEntry = fct->bodyScope->lookupStrict(THIS_VARIABLE_NAME);
101
1/2
✗ Branch 50 → 51 not taken.
✓ Branch 50 → 52 taken 1002 times.
1002 assert(thisEntry != nullptr);
102
3/6
✓ Branch 52 → 53 taken 1002 times.
✗ Branch 52 → 83 not taken.
✓ Branch 53 → 54 taken 1002 times.
✗ Branch 53 → 83 not taken.
✓ Branch 54 → 55 taken 1002 times.
✗ Branch 54 → 83 not taken.
1002 llvm::Type *structType = thisEntry->getQualType().getContained().toLLVMType(sourceFile);
103
1/2
✗ Branch 55 → 56 not taken.
✓ Branch 55 → 57 taken 1002 times.
1002 assert(structType != nullptr);
104
1/2
✓ Branch 57 → 58 taken 1002 times.
✗ Branch 57 → 85 not taken.
1002 callee->addParamAttr(0, llvm::Attribute::NoUndef);
105
1/2
✓ Branch 58 → 59 taken 1002 times.
✗ Branch 58 → 85 not taken.
1002 callee->addParamAttr(0, llvm::Attribute::NonNull);
106
3/6
✓ Branch 60 → 61 taken 1002 times.
✗ Branch 60 → 84 not taken.
✓ Branch 61 → 62 taken 1002 times.
✗ Branch 61 → 84 not taken.
✓ Branch 62 → 63 taken 1002 times.
✗ Branch 62 → 84 not taken.
1002 callee->addDereferenceableParamAttr(0, module->getDataLayout().getTypeStoreSize(structType));
107
3/6
✓ Branch 64 → 65 taken 1002 times.
✗ Branch 64 → 85 not taken.
✓ Branch 65 → 66 taken 1002 times.
✗ Branch 65 → 85 not taken.
✓ Branch 66 → 67 taken 1002 times.
✗ Branch 66 → 85 not taken.
1002 callee->addParamAttr(0, llvm::Attribute::getWithAlignment(context, module->getDataLayout().getABITypeAlign(structType)));
108 }
109 1002 }
110 3665 }
111
112 3665 llvm::CallInst *IRGenerator::generateFctCall(const Function *fct, const std::vector<llvm::Value *> &args) const {
113 // Retrieve metadata for the function
114
1/2
✓ Branch 2 → 3 taken 3665 times.
✗ Branch 2 → 57 not taken.
3665 const std::string mangledName = fct->getMangledName();
115
116 // Get callee function
117
1/2
✓ Branch 4 → 5 taken 3665 times.
✗ Branch 4 → 43 not taken.
3665 llvm::Function *callee = module->getFunction(mangledName);
118
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 3665 times.
3665 assert(callee != nullptr);
119
120 // Generate function call
121
4/8
✓ Branch 7 → 8 taken 3665 times.
✗ Branch 7 → 46 not taken.
✓ Branch 9 → 10 taken 3665 times.
✗ Branch 9 → 44 not taken.
✓ Branch 10 → 11 taken 3665 times.
✗ Branch 10 → 44 not taken.
✓ Branch 11 → 12 taken 3665 times.
✗ Branch 11 → 55 not taken.
3665 llvm::CallInst *callInst = builder.CreateCall(callee, args);
122
123 // Set attributes to 'this' param
124
1/2
✓ Branch 14 → 15 taken 3665 times.
✗ Branch 14 → 40 not taken.
3665 if (fct->isMethod()) {
125 // Get 'this' entry
126
1/2
✓ Branch 17 → 18 taken 3665 times.
✗ Branch 17 → 49 not taken.
10995 const SymbolTableEntry *thisEntry = fct->bodyScope->lookupStrict(THIS_VARIABLE_NAME);
127
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 3665 times.
3665 assert(thisEntry != nullptr);
128
3/6
✓ Branch 25 → 26 taken 3665 times.
✗ Branch 25 → 53 not taken.
✓ Branch 26 → 27 taken 3665 times.
✗ Branch 26 → 53 not taken.
✓ Branch 27 → 28 taken 3665 times.
✗ Branch 27 → 53 not taken.
3665 llvm::Type *structType = thisEntry->getQualType().getContained().toLLVMType(sourceFile);
129
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 3665 times.
3665 assert(structType != nullptr);
130
1/2
✓ Branch 30 → 31 taken 3665 times.
✗ Branch 30 → 55 not taken.
3665 callInst->addParamAttr(0, llvm::Attribute::NoUndef);
131
1/2
✓ Branch 31 → 32 taken 3665 times.
✗ Branch 31 → 55 not taken.
3665 callInst->addParamAttr(0, llvm::Attribute::NonNull);
132
3/6
✓ Branch 33 → 34 taken 3665 times.
✗ Branch 33 → 54 not taken.
✓ Branch 34 → 35 taken 3665 times.
✗ Branch 34 → 54 not taken.
✓ Branch 35 → 36 taken 3665 times.
✗ Branch 35 → 54 not taken.
3665 callInst->addDereferenceableParamAttr(0, module->getDataLayout().getTypeStoreSize(structType));
133
3/6
✓ Branch 37 → 38 taken 3665 times.
✗ Branch 37 → 55 not taken.
✓ Branch 38 → 39 taken 3665 times.
✗ Branch 38 → 55 not taken.
✓ Branch 39 → 40 taken 3665 times.
✗ Branch 39 → 55 not taken.
3665 callInst->addParamAttr(0, llvm::Attribute::getWithAlignment(context, module->getDataLayout().getABITypeAlign(structType)));
134 }
135
136 3665 return callInst;
137 3665 }
138
139 llvm::Value *IRGenerator::generateFctDeclAndCall(const Function *fct, const std::vector<llvm::Value *> &args) const {
140 generateFctDecl(fct, args);
141 return generateFctCall(fct, args);
142 }
143
144 3665 void IRGenerator::generateProcDeclAndCall(const Function *proc, const std::vector<llvm::Value *> &args) const {
145 3665 generateFctDecl(proc, args);
146 3665 (void)generateFctCall(proc, args);
147 3665 }
148
149 2656 void IRGenerator::generateCtorOrDtorCall(const SymbolTableEntry *entry, const Function *ctorOrDtor,
150 const std::vector<llvm::Value *> &args) const {
151 // Retrieve address of the struct variable. For fields this is the 'this' variable, otherwise use the normal address
152 llvm::Value *structAddr;
153
2/2
✓ Branch 3 → 4 taken 285 times.
✓ Branch 3 → 41 taken 2371 times.
2656 if (entry->isField()) {
154 // Take 'this' var as base pointer
155
1/2
✓ Branch 6 → 7 taken 285 times.
✗ Branch 6 → 50 not taken.
855 const SymbolTableEntry *thisVar = currentScope->lookupStrict(THIS_VARIABLE_NAME);
156
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 285 times.
285 assert(thisVar != nullptr);
157
7/14
✓ Branch 14 → 15 taken 285 times.
✗ Branch 14 → 54 not taken.
✓ Branch 15 → 16 taken 285 times.
✗ Branch 15 → 54 not taken.
✓ Branch 16 → 17 taken 285 times.
✗ Branch 16 → 22 not taken.
✓ Branch 17 → 18 taken 285 times.
✗ Branch 17 → 54 not taken.
✓ Branch 18 → 19 taken 285 times.
✗ Branch 18 → 54 not taken.
✓ Branch 19 → 20 taken 285 times.
✗ Branch 19 → 54 not taken.
✓ Branch 20 → 21 taken 285 times.
✗ Branch 20 → 22 not taken.
285 assert(thisVar->getQualType().isPtr() && thisVar->getQualType().getContained().is(TY_STRUCT));
158
3/6
✓ Branch 23 → 24 taken 285 times.
✗ Branch 23 → 55 not taken.
✓ Branch 24 → 25 taken 285 times.
✗ Branch 24 → 55 not taken.
✓ Branch 25 → 26 taken 285 times.
✗ Branch 25 → 55 not taken.
285 llvm::Type *thisType = thisVar->getQualType().getContained().toLLVMType(sourceFile);
159
3/6
✓ Branch 29 → 30 taken 285 times.
✗ Branch 29 → 56 not taken.
✓ Branch 30 → 31 taken 285 times.
✗ Branch 30 → 56 not taken.
✓ Branch 31 → 32 taken 285 times.
✗ Branch 31 → 56 not taken.
285 llvm::Value *thisPtr = insertLoad(builder.getPtrTy(), thisVar->getAddress());
160 // Add field offset
161
1/2
✓ Branch 37 → 38 taken 285 times.
✗ Branch 37 → 62 not taken.
285 structAddr = insertStructGEP(thisType, thisPtr, entry->orderIndex);
162 } else {
163 2371 structAddr = entry->getAddress();
164 // For optional parameter initializers we need this exception
165
2/2
✓ Branch 42 → 43 taken 7 times.
✓ Branch 42 → 44 taken 2364 times.
2371 if (!structAddr)
166 7 return;
167 }
168
1/2
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 46 taken 2649 times.
2649 assert(structAddr != nullptr);
169 2649 generateCtorOrDtorCall(structAddr, ctorOrDtor, args);
170 }
171
172 3665 void IRGenerator::generateCtorOrDtorCall(llvm::Value *structAddr, const Function *ctorOrDtor,
173 const std::vector<llvm::Value *> &args) const {
174 // Build parameter list
175
1/2
✓ Branch 4 → 5 taken 3665 times.
✗ Branch 4 → 16 not taken.
7330 std::vector argValues = {structAddr};
176
1/2
✓ Branch 12 → 13 taken 3665 times.
✗ Branch 12 → 20 not taken.
7330 argValues.insert(argValues.end(), args.begin(), args.end());
177
178 // Generate function call
179
1/2
✓ Branch 13 → 14 taken 3665 times.
✗ Branch 13 → 22 not taken.
3665 generateProcDeclAndCall(ctorOrDtor, argValues);
180 3665 }
181
182 171 void IRGenerator::generateDeallocCall(llvm::Value *variableAddress) const {
183 // Abort if the address is not set. This can happen when leaving the scope of a dtor, which already freed the heap memory
184
2/2
✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 4 taken 169 times.
171 if (!variableAddress)
185 2 return;
186
187 // In case of string runtime, call free manually. Otherwise, use the memory_rt implementation of sDealloc()
188
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 14 taken 169 times.
338 if (sourceFile->isStringRT()) {
189 llvm::Function *freeFct = stdFunctionManager.getFreeFct();
190 builder.CreateCall(freeFct, variableAddress);
191 } else {
192 169 llvm::Function *deallocFct = stdFunctionManager.getDeallocBytePtrRefFct();
193
3/6
✓ Branch 15 → 16 taken 169 times.
✗ Branch 15 → 26 not taken.
✓ Branch 17 → 18 taken 169 times.
✗ Branch 17 → 24 not taken.
✓ Branch 18 → 19 taken 169 times.
✗ Branch 18 → 24 not taken.
169 builder.CreateCall(deallocFct, variableAddress);
194 }
195 }
196
197 4 llvm::Function *IRGenerator::generateImplicitFunction(const std::function<void()> &generateBody, const Function *spiceFunc) {
198 // Only focus on method procedures
199
1/2
✓ Branch 2 → 3 taken 4 times.
✗ Branch 2 → 168 not taken.
4 const ASTNode *node = spiceFunc->entry->declNode;
200
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 4 times.
4 assert(spiceFunc->isFunction());
201
202 // Only generate if used
203
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 4 times.
4 if (!spiceFunc->used)
204 return nullptr;
205
206 // Retrieve return type
207
1/2
✓ Branch 9 → 10 taken 4 times.
✗ Branch 9 → 168 not taken.
4 llvm::Type *returnType = spiceFunc->returnType.toLLVMType(sourceFile);
208
209 // Get 'this' entry
210 4 std::vector<llvm::Type *> paramTypes;
211
1/2
✓ Branch 10 → 11 taken 4 times.
✗ Branch 10 → 166 not taken.
4 SymbolTableEntry *thisEntry = nullptr;
212
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 27 taken 4 times.
4 if (spiceFunc->isMethod()) {
213 thisEntry = spiceFunc->bodyScope->lookupStrict(THIS_VARIABLE_NAME);
214 assert(thisEntry != nullptr);
215 paramTypes.push_back(builder.getPtrTy());
216 }
217
218 // Get parameter types
219
1/2
✗ Branch 44 → 29 not taken.
✓ Branch 44 → 45 taken 4 times.
8 for (const auto &[qualType, isOptional] : spiceFunc->paramList) {
220 assert(!isOptional);
221 paramTypes.push_back(qualType.toLLVMType(sourceFile));
222 }
223
224 // Get function linkage
225
2/4
✓ Branch 45 → 46 taken 4 times.
✗ Branch 45 → 166 not taken.
✓ Branch 46 → 47 taken 4 times.
✗ Branch 46 → 166 not taken.
4 const bool isPublic = spiceFunc->entry->getQualType().isPublic();
226
227 // Create function
228
1/2
✓ Branch 47 → 48 taken 4 times.
✗ Branch 47 → 166 not taken.
4 const std::string mangledName = spiceFunc->getMangledName();
229
1/2
✓ Branch 49 → 50 taken 4 times.
✗ Branch 49 → 139 not taken.
4 llvm::FunctionType *fctType = llvm::FunctionType::get(returnType, paramTypes, false);
230
1/2
✓ Branch 50 → 51 taken 4 times.
✗ Branch 50 → 164 not taken.
4 const llvm::GlobalObject::LinkageTypes linkage = getSymbolLinkageType(isPublic);
231
2/4
✓ Branch 51 → 52 taken 4 times.
✗ Branch 51 → 140 not taken.
✓ Branch 52 → 53 taken 4 times.
✗ Branch 52 → 140 not taken.
4 llvm::Function *fct = llvm::Function::Create(fctType, linkage, mangledName, module);
232
1/2
✓ Branch 53 → 54 taken 4 times.
✗ Branch 53 → 164 not taken.
4 fct->addFnAttr(llvm::Attribute::MustProgress);
233
1/2
✓ Branch 54 → 55 taken 4 times.
✗ Branch 54 → 164 not taken.
4 fct->addFnAttr(llvm::Attribute::NoUnwind);
234
1/2
✓ Branch 55 → 56 taken 4 times.
✗ Branch 55 → 58 not taken.
4 if (cliOptions.optLevel == OptLevel::O0) {
235
1/2
✓ Branch 56 → 57 taken 4 times.
✗ Branch 56 → 164 not taken.
4 fct->addFnAttr(llvm::Attribute::OptimizeNone);
236
1/2
✓ Branch 57 → 60 taken 4 times.
✗ Branch 57 → 164 not taken.
4 fct->addFnAttr(llvm::Attribute::NoInline);
237 } else if (cliOptions.optLevel >= OptLevel::Os) {
238 fct->addFnAttr(llvm::Attribute::OptimizeForSize);
239 }
240
2/4
✓ Branch 60 → 61 taken 4 times.
✗ Branch 60 → 164 not taken.
✓ Branch 61 → 62 taken 4 times.
✗ Branch 61 → 164 not taken.
4 fct->addFnAttr(llvm::Attribute::getWithUWTableKind(context, llvm::UWTableKind::Default));
241
242 // Set attributes to 'this' param
243
1/2
✗ Branch 65 → 66 not taken.
✓ Branch 65 → 83 taken 4 times.
4 if (spiceFunc->isMethod()) {
244 fct->addParamAttr(0, llvm::Attribute::NoUndef);
245 fct->addParamAttr(0, llvm::Attribute::NonNull);
246 assert(thisEntry != nullptr);
247 llvm::Type *structType = thisEntry->getQualType().getContained().toLLVMType(sourceFile);
248 assert(structType != nullptr);
249 fct->addDereferenceableParamAttr(0, module->getDataLayout().getTypeStoreSize(structType));
250 fct->addParamAttr(0, llvm::Attribute::getWithAlignment(context, module->getDataLayout().getABITypeAlign(structType)));
251 }
252
253 // Add debug info
254
1/2
✓ Branch 83 → 84 taken 4 times.
✗ Branch 83 → 164 not taken.
4 diGenerator.generateFunctionDebugInfo(fct, spiceFunc);
255
1/2
✗ Branch 84 → 85 not taken.
✓ Branch 84 → 86 taken 4 times.
4 if (node != nullptr)
256 diGenerator.setSourceLocation(node);
257
258 // Change to body scope
259
2/4
✓ Branch 86 → 87 taken 4 times.
✗ Branch 86 → 145 not taken.
✓ Branch 87 → 88 taken 4 times.
✗ Branch 87 → 143 not taken.
4 changeToScope(spiceFunc->getScopeName(), ScopeType::FUNC_PROC_BODY);
260
261 // Create entry block
262
1/2
✓ Branch 92 → 93 taken 4 times.
✗ Branch 92 → 146 not taken.
4 llvm::BasicBlock *bEntry = createBlock();
263
1/2
✓ Branch 95 → 96 taken 4 times.
✗ Branch 95 → 164 not taken.
4 switchToBlock(bEntry, fct);
264
265 // Reset alloca insert markers to this block
266 4 allocaInsertBlock = bEntry;
267
1/2
✓ Branch 96 → 97 taken 4 times.
✗ Branch 96 → 164 not taken.
4 allocaInsertInst = nullptr;
268
269 // Store first argument to 'this' symbol
270
1/2
✗ Branch 99 → 100 not taken.
✓ Branch 99 → 119 taken 4 times.
4 if (spiceFunc->isMethod()) {
271 assert(thisEntry != nullptr);
272 // Allocate space for the parameter
273 llvm::Value *thisAddress = insertAlloca(paramTypes.front(), THIS_VARIABLE_NAME);
274 // Update the symbol table entry
275 thisEntry->updateAddress(thisAddress);
276 // Store the value at the new address
277 insertStore(fct->arg_begin(), thisAddress);
278 // Generate debug info
279 diGenerator.generateLocalVarDebugInfo(THIS_VARIABLE_NAME, thisAddress, 1);
280 }
281
282 // Generate body
283
1/2
✓ Branch 119 → 120 taken 4 times.
✗ Branch 119 → 164 not taken.
4 generateBody();
284
285 // Conclude debug info for function
286
1/2
✓ Branch 120 → 121 taken 4 times.
✗ Branch 120 → 164 not taken.
4 diGenerator.concludeFunctionDebugInfo();
287
288 // Verify function
289 // Use the code location of the declaration node if available. Otherwise, (e.g. in case of test main) use an artificial code loc
290
2/4
✗ Branch 121 → 122 not taken.
✓ Branch 121 → 123 taken 4 times.
✓ Branch 123 → 124 taken 4 times.
✗ Branch 123 → 164 not taken.
4 const CodeLoc codeLoc = node != nullptr ? node->codeLoc : CodeLoc(1, 1, sourceFile);
291
1/2
✓ Branch 124 → 125 taken 4 times.
✗ Branch 124 → 164 not taken.
4 verifyFunction(fct, codeLoc);
292
293 // Change to parent scope
294
1/2
✓ Branch 125 → 126 taken 4 times.
✗ Branch 125 → 164 not taken.
4 changeToParentScope(ScopeType::FUNC_PROC_BODY);
295
296 4 return fct;
297 4 }
298
299 832 llvm::Function *IRGenerator::generateImplicitProcedure(const std::function<void()> &generateBody, const Function *spiceProc) {
300 // Only focus on method procedures
301 832 const ASTNode *node = spiceProc->entry->declNode;
302
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 832 times.
832 assert(node != nullptr);
303
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 832 times.
832 assert(spiceProc->isProcedure());
304
305 // Only generate if used
306
2/2
✓ Branch 9 → 10 taken 429 times.
✓ Branch 9 → 11 taken 403 times.
832 if (!spiceProc->used)
307 429 return nullptr;
308
309 // Get 'this' entry
310 403 std::vector<llvm::Type *> paramTypes;
311
1/2
✓ Branch 11 → 12 taken 403 times.
✗ Branch 11 → 165 not taken.
403 SymbolTableEntry *thisEntry = nullptr;
312
1/2
✓ Branch 14 → 15 taken 403 times.
✗ Branch 14 → 28 not taken.
403 if (spiceProc->isMethod()) {
313
1/2
✓ Branch 17 → 18 taken 403 times.
✗ Branch 17 → 131 not taken.
1209 thisEntry = spiceProc->bodyScope->lookupStrict(THIS_VARIABLE_NAME);
314
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 403 times.
403 assert(thisEntry != nullptr);
315
2/4
✓ Branch 25 → 26 taken 403 times.
✗ Branch 25 → 135 not taken.
✓ Branch 26 → 27 taken 403 times.
✗ Branch 26 → 135 not taken.
403 paramTypes.push_back(builder.getPtrTy());
316 }
317
318 // Get parameter types
319
2/2
✓ Branch 45 → 30 taken 51 times.
✓ Branch 45 → 46 taken 403 times.
857 for (const auto &[qualType, isOptional] : spiceProc->paramList) {
320
1/2
✗ Branch 32 → 33 not taken.
✓ Branch 32 → 34 taken 51 times.
51 assert(!isOptional);
321
2/4
✓ Branch 34 → 35 taken 51 times.
✗ Branch 34 → 136 not taken.
✓ Branch 35 → 36 taken 51 times.
✗ Branch 35 → 136 not taken.
51 paramTypes.push_back(qualType.toLLVMType(sourceFile));
322 }
323
324 // Get function linkage
325
2/4
✓ Branch 46 → 47 taken 403 times.
✗ Branch 46 → 165 not taken.
✓ Branch 47 → 48 taken 403 times.
✗ Branch 47 → 165 not taken.
403 const bool isPublic = spiceProc->entry->getQualType().isPublic();
326
327 // Create function
328
1/2
✓ Branch 48 → 49 taken 403 times.
✗ Branch 48 → 165 not taken.
403 const std::string mangledName = spiceProc->getMangledName();
329
2/4
✓ Branch 50 → 51 taken 403 times.
✗ Branch 50 → 138 not taken.
✓ Branch 51 → 52 taken 403 times.
✗ Branch 51 → 138 not taken.
403 llvm::FunctionType *fctType = llvm::FunctionType::get(builder.getVoidTy(), paramTypes, false);
330
1/2
✓ Branch 52 → 53 taken 403 times.
✗ Branch 52 → 163 not taken.
403 const llvm::GlobalObject::LinkageTypes linkage = getSymbolLinkageType(isPublic);
331
2/4
✓ Branch 53 → 54 taken 403 times.
✗ Branch 53 → 139 not taken.
✓ Branch 54 → 55 taken 403 times.
✗ Branch 54 → 139 not taken.
403 llvm::Function *fct = llvm::Function::Create(fctType, linkage, mangledName, module);
332
1/2
✓ Branch 55 → 56 taken 403 times.
✗ Branch 55 → 163 not taken.
403 fct->addFnAttr(llvm::Attribute::MustProgress);
333
1/2
✓ Branch 56 → 57 taken 403 times.
✗ Branch 56 → 163 not taken.
403 fct->addFnAttr(llvm::Attribute::NoUnwind);
334
1/2
✓ Branch 57 → 58 taken 403 times.
✗ Branch 57 → 60 not taken.
403 if (cliOptions.optLevel == OptLevel::O0) {
335
1/2
✓ Branch 58 → 59 taken 403 times.
✗ Branch 58 → 163 not taken.
403 fct->addFnAttr(llvm::Attribute::OptimizeNone);
336
1/2
✓ Branch 59 → 62 taken 403 times.
✗ Branch 59 → 163 not taken.
403 fct->addFnAttr(llvm::Attribute::NoInline);
337 } else if (cliOptions.optLevel >= OptLevel::Os) {
338 fct->addFnAttr(llvm::Attribute::OptimizeForSize);
339 }
340
2/4
✓ Branch 62 → 63 taken 403 times.
✗ Branch 62 → 163 not taken.
✓ Branch 63 → 64 taken 403 times.
✗ Branch 63 → 163 not taken.
403 fct->addFnAttr(llvm::Attribute::getWithUWTableKind(context, llvm::UWTableKind::Default));
341
342 // Set attributes to 'this' param
343
1/2
✓ Branch 67 → 68 taken 403 times.
✗ Branch 67 → 85 not taken.
403 if (spiceProc->isMethod()) {
344
1/2
✓ Branch 68 → 69 taken 403 times.
✗ Branch 68 → 163 not taken.
403 fct->addParamAttr(0, llvm::Attribute::NoUndef);
345
1/2
✓ Branch 69 → 70 taken 403 times.
✗ Branch 69 → 163 not taken.
403 fct->addParamAttr(0, llvm::Attribute::NonNull);
346
1/2
✗ Branch 70 → 71 not taken.
✓ Branch 70 → 72 taken 403 times.
403 assert(thisEntry != nullptr);
347
3/6
✓ Branch 72 → 73 taken 403 times.
✗ Branch 72 → 140 not taken.
✓ Branch 73 → 74 taken 403 times.
✗ Branch 73 → 140 not taken.
✓ Branch 74 → 75 taken 403 times.
✗ Branch 74 → 140 not taken.
403 llvm::Type *structType = thisEntry->getQualType().getContained().toLLVMType(sourceFile);
348
1/2
✗ Branch 75 → 76 not taken.
✓ Branch 75 → 77 taken 403 times.
403 assert(structType != nullptr);
349
3/6
✓ Branch 78 → 79 taken 403 times.
✗ Branch 78 → 141 not taken.
✓ Branch 79 → 80 taken 403 times.
✗ Branch 79 → 141 not taken.
✓ Branch 80 → 81 taken 403 times.
✗ Branch 80 → 141 not taken.
403 fct->addDereferenceableParamAttr(0, module->getDataLayout().getTypeStoreSize(structType));
350
3/6
✓ Branch 82 → 83 taken 403 times.
✗ Branch 82 → 163 not taken.
✓ Branch 83 → 84 taken 403 times.
✗ Branch 83 → 163 not taken.
✓ Branch 84 → 85 taken 403 times.
✗ Branch 84 → 163 not taken.
403 fct->addParamAttr(0, llvm::Attribute::getWithAlignment(context, module->getDataLayout().getABITypeAlign(structType)));
351 }
352
353 // Add debug info
354
1/2
✓ Branch 85 → 86 taken 403 times.
✗ Branch 85 → 163 not taken.
403 diGenerator.generateFunctionDebugInfo(fct, spiceProc);
355
1/2
✓ Branch 86 → 87 taken 403 times.
✗ Branch 86 → 163 not taken.
403 diGenerator.setSourceLocation(node);
356
357 // Change to body scope
358
2/4
✓ Branch 87 → 88 taken 403 times.
✗ Branch 87 → 144 not taken.
✓ Branch 88 → 89 taken 403 times.
✗ Branch 88 → 142 not taken.
403 changeToScope(spiceProc->getScopeName(), ScopeType::FUNC_PROC_BODY);
359
360 // Create entry block
361
1/2
✓ Branch 93 → 94 taken 403 times.
✗ Branch 93 → 145 not taken.
403 llvm::BasicBlock *bEntry = createBlock();
362
1/2
✓ Branch 96 → 97 taken 403 times.
✗ Branch 96 → 163 not taken.
403 switchToBlock(bEntry, fct);
363
364 // Reset alloca insert markers to this block
365 403 allocaInsertBlock = bEntry;
366
1/2
✓ Branch 97 → 98 taken 403 times.
✗ Branch 97 → 163 not taken.
403 allocaInsertInst = nullptr;
367
368 // Store first argument to 'this' symbol
369
1/2
✓ Branch 100 → 101 taken 403 times.
✗ Branch 100 → 120 not taken.
403 if (spiceProc->isMethod()) {
370
1/2
✗ Branch 101 → 102 not taken.
✓ Branch 101 → 103 taken 403 times.
403 assert(thisEntry != nullptr);
371 // Allocate space for the parameter
372
2/4
✓ Branch 105 → 106 taken 403 times.
✗ Branch 105 → 153 not taken.
✓ Branch 107 → 108 taken 403 times.
✗ Branch 107 → 151 not taken.
403 llvm::Value *thisAddress = insertAlloca(paramTypes.front(), THIS_VARIABLE_NAME);
373 // Update the symbol table entry
374
1/2
✓ Branch 110 → 111 taken 403 times.
✗ Branch 110 → 163 not taken.
403 thisEntry->updateAddress(thisAddress);
375 // Store the value at the new address
376
2/4
✓ Branch 111 → 112 taken 403 times.
✗ Branch 111 → 163 not taken.
✓ Branch 112 → 113 taken 403 times.
✗ Branch 112 → 163 not taken.
403 insertStore(fct->arg_begin(), thisAddress);
377 // Generate debug info
378
2/4
✓ Branch 115 → 116 taken 403 times.
✗ Branch 115 → 159 not taken.
✓ Branch 116 → 117 taken 403 times.
✗ Branch 116 → 157 not taken.
1209 diGenerator.generateLocalVarDebugInfo(THIS_VARIABLE_NAME, thisAddress, 1);
379 }
380
381 // Generate body
382
1/2
✓ Branch 120 → 121 taken 403 times.
✗ Branch 120 → 163 not taken.
403 generateBody();
383
384 // Create return instruction
385
1/2
✓ Branch 121 → 122 taken 403 times.
✗ Branch 121 → 163 not taken.
403 builder.CreateRetVoid();
386
387 // Conclude debug info for function
388
1/2
✓ Branch 122 → 123 taken 403 times.
✗ Branch 122 → 163 not taken.
403 diGenerator.concludeFunctionDebugInfo();
389
390 // Verify function
391
1/2
✓ Branch 123 → 124 taken 403 times.
✗ Branch 123 → 163 not taken.
403 verifyFunction(fct, node->codeLoc);
392
393 // Change to parent scope
394
1/2
✓ Branch 124 → 125 taken 403 times.
✗ Branch 124 → 163 not taken.
403 changeToParentScope(ScopeType::FUNC_PROC_BODY);
395
396 403 return fct;
397 403 }
398
399 2692 void IRGenerator::generateCtorBodyPreamble(Scope *bodyScope) {
400 // Retrieve struct scope
401 2692 Scope *structScope = bodyScope->parent;
402
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2692 times.
2692 assert(structScope != nullptr);
403
404 // Get struct address
405
1/2
✓ Branch 6 → 7 taken 2692 times.
✗ Branch 6 → 130 not taken.
8076 const SymbolTableEntry *thisEntry = bodyScope->lookupStrict(THIS_VARIABLE_NAME);
406
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 2692 times.
2692 assert(thisEntry != nullptr);
407
1/2
✓ Branch 14 → 15 taken 2692 times.
✗ Branch 14 → 184 not taken.
2692 llvm::Value *thisPtrPtr = thisEntry->getAddress();
408
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 2692 times.
2692 assert(thisPtrPtr != nullptr);
409 2692 llvm::Value *thisPtr = nullptr;
410
2/4
✓ Branch 17 → 18 taken 2692 times.
✗ Branch 17 → 184 not taken.
✓ Branch 18 → 19 taken 2692 times.
✗ Branch 18 → 184 not taken.
2692 const QualType structSymbolType = thisEntry->getQualType().getBase();
411
1/2
✓ Branch 19 → 20 taken 2692 times.
✗ Branch 19 → 184 not taken.
2692 llvm::Type *structType = structSymbolType.toLLVMType(sourceFile);
412
413 // Store VTable to first struct field if required
414
1/2
✓ Branch 20 → 21 taken 2692 times.
✗ Branch 20 → 184 not taken.
2692 const Struct *spiceStruct = structSymbolType.getStruct(nullptr);
415
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 2692 times.
2692 assert(spiceStruct != nullptr);
416
2/2
✓ Branch 23 → 24 taken 526 times.
✓ Branch 23 → 45 taken 2166 times.
2692 if (spiceStruct->vTableData.vtable != nullptr) {
417
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 526 times.
526 assert(spiceStruct->vTableData.vtableType != nullptr);
418 // Store VTable to field address at index 0
419
2/4
✓ Branch 29 → 30 taken 526 times.
✗ Branch 29 → 134 not taken.
✓ Branch 30 → 31 taken 526 times.
✗ Branch 30 → 134 not taken.
526 thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr);
420
3/6
✓ Branch 33 → 34 taken 526 times.
✗ Branch 33 → 147 not taken.
✓ Branch 34 → 35 taken 526 times.
✗ Branch 34 → 147 not taken.
✓ Branch 35 → 36 taken 526 times.
✗ Branch 35 → 147 not taken.
526 llvm::Value *indices[3] = {builder.getInt64(0), builder.getInt32(0), builder.getInt32(2)};
421
1/2
✓ Branch 40 → 41 taken 526 times.
✗ Branch 40 → 140 not taken.
526 llvm::Value *gepResult = insertInBoundsGEP(spiceStruct->vTableData.vtableType, spiceStruct->vTableData.vtable, indices);
422
1/2
✓ Branch 43 → 44 taken 526 times.
✗ Branch 43 → 147 not taken.
526 insertStore(gepResult, thisPtr);
423 }
424
425
1/2
✓ Branch 45 → 46 taken 2692 times.
✗ Branch 45 → 184 not taken.
2692 const size_t fieldCount = structScope->getFieldCount();
426
2/2
✓ Branch 126 → 47 taken 6423 times.
✓ Branch 126 → 127 taken 2692 times.
9115 for (size_t fieldIdx = 0; fieldIdx < fieldCount; fieldIdx++) {
427
1/2
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 6423 times.
6423 const SymbolTableEntry *fieldSymbol = structScope->lookupField(fieldIdx);
428
3/6
✓ Branch 52 → 53 taken 6423 times.
✗ Branch 52 → 56 not taken.
✓ Branch 53 → 54 taken 6423 times.
✗ Branch 53 → 184 not taken.
✓ Branch 54 → 55 taken 6423 times.
✗ Branch 54 → 56 not taken.
6423 assert(fieldSymbol != nullptr && fieldSymbol->isField());
429
2/2
✓ Branch 57 → 58 taken 456 times.
✓ Branch 57 → 59 taken 5967 times.
6423 if (fieldSymbol->isImplicitField)
430 456 continue;
431
432 // Call ctor for struct fields
433
1/2
✓ Branch 59 → 60 taken 5967 times.
✗ Branch 59 → 184 not taken.
5967 const QualType &fieldType = fieldSymbol->getQualType();
434
1/2
✓ Branch 60 → 61 taken 5967 times.
✗ Branch 60 → 62 not taken.
5967 const auto fieldNode = spice_pointer_cast<FieldNode *>(fieldSymbol->declNode);
435
3/4
✓ Branch 67 → 68 taken 5967 times.
✗ Branch 67 → 184 not taken.
✓ Branch 68 → 69 taken 1128 times.
✓ Branch 68 → 99 taken 4839 times.
5967 if (fieldType.is(TY_STRUCT)) {
436 // Lookup ctor function and call if available
437
1/2
✓ Branch 69 → 70 taken 1128 times.
✗ Branch 69 → 184 not taken.
1128 Scope *matchScope = fieldType.getBodyScope();
438
4/6
✓ Branch 73 → 74 taken 1128 times.
✗ Branch 73 → 150 not taken.
✓ Branch 74 → 75 taken 1128 times.
✗ Branch 74 → 148 not taken.
✓ Branch 78 → 79 taken 655 times.
✓ Branch 78 → 98 taken 473 times.
3384 if (const Function *ctorFunction = FunctionManager::lookup(matchScope, CTOR_FUNCTION_NAME, fieldType, {}, false)) {
439
2/2
✓ Branch 79 → 80 taken 175 times.
✓ Branch 79 → 88 taken 480 times.
655 if (!thisPtr)
440
2/4
✓ Branch 83 → 84 taken 175 times.
✗ Branch 83 → 157 not taken.
✓ Branch 84 → 85 taken 175 times.
✗ Branch 84 → 157 not taken.
175 thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr);
441
1/2
✓ Branch 91 → 92 taken 655 times.
✗ Branch 91 → 163 not taken.
655 llvm::Value *fieldAddress = insertStructGEP(structType, thisPtr, fieldIdx);
442
1/2
✓ Branch 95 → 96 taken 655 times.
✗ Branch 95 → 169 not taken.
655 generateCtorOrDtorCall(fieldAddress, ctorFunction, {});
443 }
444 1128 continue;
445 1128 }
446
447 // Store default field values
448
3/4
✓ Branch 99 → 100 taken 4170 times.
✓ Branch 99 → 101 taken 669 times.
✓ Branch 100 → 101 taken 4170 times.
✗ Branch 100 → 125 not taken.
4839 if (fieldNode->defaultValue != nullptr || cliOptions.buildMode != BuildMode::RELEASE) {
449 // Retrieve field address
450
2/2
✓ Branch 101 → 102 taken 1653 times.
✓ Branch 101 → 110 taken 3186 times.
4839 if (!thisPtr)
451
2/4
✓ Branch 105 → 106 taken 1653 times.
✗ Branch 105 → 172 not taken.
✓ Branch 106 → 107 taken 1653 times.
✗ Branch 106 → 172 not taken.
1653 thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr);
452
1/2
✓ Branch 113 → 114 taken 4839 times.
✗ Branch 113 → 178 not taken.
4839 llvm::Value *fieldAddress = insertStructGEP(structType, thisPtr, fieldIdx);
453 // Retrieve default value
454 llvm::Value *value;
455
2/2
✓ Branch 116 → 117 taken 669 times.
✓ Branch 116 → 119 taken 4170 times.
4839 if (fieldNode->defaultValue != nullptr) {
456 // To resolve the default value, we need to temporarily change to the manifestation of the current struct instantiation
457 669 const size_t oldManIdx = manIdx; // Save manifestation index
458 669 manIdx = spiceStruct->manifestationIndex;
459
1/2
✓ Branch 117 → 118 taken 669 times.
✗ Branch 117 → 184 not taken.
669 value = resolveValue(fieldNode->defaultValue);
460 669 manIdx = oldManIdx; // Restore manifestation index
461 } else {
462
1/4
✗ Branch 119 → 120 not taken.
✓ Branch 119 → 122 taken 4170 times.
✗ Branch 120 → 121 not taken.
✗ Branch 120 → 122 not taken.
4170 assert(cliOptions.buildMode == BuildMode::DEBUG || cliOptions.buildMode == BuildMode::TEST);
463
1/2
✓ Branch 122 → 123 taken 4170 times.
✗ Branch 122 → 184 not taken.
4170 value = getDefaultValueForSymbolType(fieldType);
464 }
465 // Store default value
466
1/2
✓ Branch 124 → 125 taken 4839 times.
✗ Branch 124 → 184 not taken.
4839 insertStore(value, fieldAddress);
467 }
468 }
469 2692 }
470
471 72 void IRGenerator::generateDefaultCtor(const Function *ctorFunction) {
472
3/6
✓ Branch 2 → 3 taken 72 times.
✗ Branch 2 → 6 not taken.
✓ Branch 3 → 4 taken 72 times.
✗ Branch 3 → 13 not taken.
✓ Branch 4 → 5 taken 72 times.
✗ Branch 4 → 6 not taken.
72 assert(ctorFunction->implicitDefault && ctorFunction->name == CTOR_FUNCTION_NAME);
473 139 const std::function<void()> generateBody = [&] { generateCtorBodyPreamble(ctorFunction->bodyScope); };
474
1/2
✓ Branch 8 → 9 taken 72 times.
✗ Branch 8 → 11 not taken.
72 generateImplicitProcedure(generateBody, ctorFunction);
475 72 }
476
477 46 void IRGenerator::generateCopyCtorBodyPreamble(const Function *copyCtorFunction) {
478 // Retrieve struct scope
479 46 Scope *structScope = copyCtorFunction->bodyScope->parent;
480
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 46 times.
46 assert(structScope != nullptr);
481
482 // Get struct address
483
1/2
✓ Branch 6 → 7 taken 46 times.
✗ Branch 6 → 141 not taken.
138 const SymbolTableEntry *thisEntry = copyCtorFunction->bodyScope->lookupStrict(THIS_VARIABLE_NAME);
484
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 46 times.
46 assert(thisEntry != nullptr);
485 46 llvm::Value *thisPtrPtr = thisEntry->getAddress();
486
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 46 times.
46 assert(thisPtrPtr != nullptr);
487 46 llvm::Value *thisPtr = nullptr;
488
3/6
✓ Branch 17 → 18 taken 46 times.
✗ Branch 17 → 145 not taken.
✓ Branch 18 → 19 taken 46 times.
✗ Branch 18 → 145 not taken.
✓ Branch 19 → 20 taken 46 times.
✗ Branch 19 → 145 not taken.
46 llvm::Type *structType = thisEntry->getQualType().getBase().toLLVMType(sourceFile);
489
490 // Retrieve the value of the original struct, which is the only function parameter
491 46 llvm::Value *originalThisPtr = builder.GetInsertBlock()->getParent()->getArg(1);
492
493 46 const size_t fieldCount = structScope->getFieldCount();
494
2/2
✓ Branch 137 → 25 taken 164 times.
✓ Branch 137 → 138 taken 46 times.
210 for (size_t fieldIdx = 0; fieldIdx < fieldCount; fieldIdx++) {
495
1/2
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 164 times.
164 const SymbolTableEntry *fieldSymbol = structScope->lookupField(fieldIdx);
496
2/4
✓ Branch 30 → 31 taken 164 times.
✗ Branch 30 → 34 not taken.
✓ Branch 32 → 33 taken 164 times.
✗ Branch 32 → 34 not taken.
164 assert(fieldSymbol != nullptr && fieldSymbol->isField());
497
498 // Retrieve the address of the original field (copy source)
499
1/2
✓ Branch 38 → 39 taken 164 times.
✗ Branch 38 → 146 not taken.
164 llvm::Value *originalFieldAddress = insertStructGEP(structType, originalThisPtr, fieldIdx);
500
501 164 const QualType &fieldType = fieldSymbol->getQualType();
502
503 // Call copy ctor for struct fields
504
5/6
✓ Branch 43 → 44 taken 69 times.
✓ Branch 43 → 47 taken 95 times.
✓ Branch 45 → 46 taken 69 times.
✗ Branch 45 → 47 not taken.
✓ Branch 48 → 49 taken 69 times.
✓ Branch 48 → 73 taken 95 times.
164 if (fieldType.is(TY_STRUCT) && !fieldType.isTriviallyCopyable(nullptr)) {
505 // Lookup copy ctor function and call if available
506
1/2
✓ Branch 49 → 50 taken 69 times.
✗ Branch 49 → 173 not taken.
69 Scope *matchScope = fieldType.getBodyScope();
507
2/4
✓ Branch 50 → 51 taken 69 times.
✗ Branch 50 → 156 not taken.
✓ Branch 54 → 55 taken 69 times.
✗ Branch 54 → 152 not taken.
207 const ArgList args = {{fieldType.toConstRef(nullptr), false /* we have the field as storage */}};
508
2/4
✓ Branch 58 → 59 taken 69 times.
✗ Branch 58 → 160 not taken.
✓ Branch 59 → 60 taken 69 times.
✗ Branch 59 → 158 not taken.
69 const Function *copyCtor = FunctionManager::lookup(matchScope, CTOR_FUNCTION_NAME, fieldType, args, false);
509
1/2
✗ Branch 62 → 63 not taken.
✓ Branch 62 → 64 taken 69 times.
69 assert(copyCtor != nullptr);
510
2/4
✓ Branch 66 → 67 taken 69 times.
✗ Branch 66 → 166 not taken.
✓ Branch 67 → 68 taken 69 times.
✗ Branch 67 → 164 not taken.
138 generateCtorOrDtorCall(fieldSymbol, copyCtor, {originalFieldAddress});
511 69 continue;
512 69 }
513
514 // Retrieve the address of the new field (copy dest)
515
2/2
✓ Branch 73 → 74 taken 31 times.
✓ Branch 73 → 82 taken 64 times.
95 if (!thisPtr)
516
2/4
✓ Branch 77 → 78 taken 31 times.
✗ Branch 77 → 174 not taken.
✓ Branch 78 → 79 taken 31 times.
✗ Branch 78 → 174 not taken.
31 thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr);
517
1/2
✓ Branch 85 → 86 taken 95 times.
✗ Branch 85 → 180 not taken.
95 llvm::Value *fieldAddress = insertStructGEP(structType, thisPtr, fieldIdx);
518
519 // For owning heap fields, copy the underlying heap storage
520
2/2
✓ Branch 89 → 90 taken 18 times.
✓ Branch 89 → 134 taken 77 times.
95 if (fieldType.isHeap()) {
521
1/2
✗ Branch 91 → 92 not taken.
✓ Branch 91 → 93 taken 18 times.
18 assert(fieldType.isPtr());
522
2/4
✓ Branch 93 → 94 taken 18 times.
✗ Branch 93 → 186 not taken.
✓ Branch 94 → 95 taken 18 times.
✗ Branch 94 → 186 not taken.
18 llvm::Type *pointeeType = fieldType.getContained().toLLVMType(sourceFile);
523
524 // Retrieve original heap address
525
2/4
✓ Branch 98 → 99 taken 18 times.
✗ Branch 98 → 187 not taken.
✓ Branch 99 → 100 taken 18 times.
✗ Branch 99 → 187 not taken.
18 llvm::Value *originalHeapAddress = insertLoad(builder.getPtrTy(), originalFieldAddress);
526
527 // Insert check for nullptr
528
2/4
✓ Branch 104 → 105 taken 18 times.
✗ Branch 104 → 195 not taken.
✓ Branch 105 → 106 taken 18 times.
✗ Branch 105 → 193 not taken.
36 llvm::BasicBlock *bThen = createBlock("nullptrcheck.then");
529
2/4
✓ Branch 110 → 111 taken 18 times.
✗ Branch 110 → 201 not taken.
✓ Branch 111 → 112 taken 18 times.
✗ Branch 111 → 199 not taken.
18 llvm::BasicBlock *bExit = createBlock("nullptrcheck.exit");
530
4/8
✓ Branch 114 → 115 taken 18 times.
✗ Branch 114 → 205 not taken.
✓ Branch 115 → 116 taken 18 times.
✗ Branch 115 → 205 not taken.
✓ Branch 116 → 117 taken 18 times.
✗ Branch 116 → 205 not taken.
✓ Branch 117 → 118 taken 18 times.
✗ Branch 117 → 205 not taken.
18 llvm::Value *condValue = builder.CreateICmpNE(originalHeapAddress, llvm::Constant::getNullValue(builder.getPtrTy()));
531 18 insertCondJump(condValue, bThen, bExit);
532
533 // Fill then block
534 18 switchToBlock(bThen);
535
536 // Allocate new space on the heap
537 18 llvm::Function *unsafeAllocFct = stdFunctionManager.getAllocUnsafeLongFct();
538
2/4
✓ Branch 122 → 123 taken 18 times.
✗ Branch 122 → 206 not taken.
✓ Branch 123 → 124 taken 18 times.
✗ Branch 123 → 206 not taken.
18 const size_t typeSizeInBytes = module->getDataLayout().getTypeSizeInBits(pointeeType) / 8;
539 18 llvm::ConstantInt *typeSize = builder.getInt64(typeSizeInBytes);
540
3/6
✓ Branch 125 → 126 taken 18 times.
✗ Branch 125 → 210 not taken.
✓ Branch 127 → 128 taken 18 times.
✗ Branch 127 → 207 not taken.
✓ Branch 128 → 129 taken 18 times.
✗ Branch 128 → 207 not taken.
18 llvm::Value *newHeapAddress = builder.CreateCall(unsafeAllocFct, {typeSize});
541 18 insertStore(newHeapAddress, fieldAddress);
542
543 // Copy data from the old heap storage to the new one
544 18 generateShallowCopy(originalHeapAddress, pointeeType, newHeapAddress, false);
545 18 insertJump(bExit);
546
547 // Switch to exit block
548 18 switchToBlock(bExit);
549
550 18 continue;
551 18 }
552
553 // Shallow copy
554 77 llvm::Type *type = fieldType.toLLVMType(sourceFile);
555 77 generateShallowCopy(originalFieldAddress, type, fieldAddress, false);
556 }
557 46 }
558
559 165 void IRGenerator::generateDefaultCopyCtor(const Function *copyCtorFunction) {
560
3/6
✓ Branch 2 → 3 taken 165 times.
✗ Branch 2 → 6 not taken.
✓ Branch 3 → 4 taken 165 times.
✗ Branch 3 → 13 not taken.
✓ Branch 4 → 5 taken 165 times.
✗ Branch 4 → 6 not taken.
165 assert(copyCtorFunction->implicitDefault && copyCtorFunction->name == CTOR_FUNCTION_NAME);
561 211 const std::function<void()> generateBody = [&] { generateCopyCtorBodyPreamble(copyCtorFunction); };
562
1/2
✓ Branch 8 → 9 taken 165 times.
✗ Branch 8 → 11 not taken.
165 generateImplicitProcedure(generateBody, copyCtorFunction);
563 165 }
564
565 5 void IRGenerator::generateMoveCtorBodyPreamble(const Function *moveCtorFunction) {
566 // Retrieve struct scope
567 5 Scope *structScope = moveCtorFunction->bodyScope->parent;
568
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 5 times.
5 assert(structScope != nullptr);
569
570 // Get struct address
571
1/2
✓ Branch 6 → 7 taken 5 times.
✗ Branch 6 → 118 not taken.
15 const SymbolTableEntry *thisEntry = moveCtorFunction->bodyScope->lookupStrict(THIS_VARIABLE_NAME);
572
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 5 times.
5 assert(thisEntry != nullptr);
573 5 llvm::Value *thisPtrPtr = thisEntry->getAddress();
574
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 5 times.
5 assert(thisPtrPtr != nullptr);
575 5 llvm::Value *thisPtr = nullptr;
576
3/6
✓ Branch 17 → 18 taken 5 times.
✗ Branch 17 → 122 not taken.
✓ Branch 18 → 19 taken 5 times.
✗ Branch 18 → 122 not taken.
✓ Branch 19 → 20 taken 5 times.
✗ Branch 19 → 122 not taken.
5 llvm::Type *structType = thisEntry->getQualType().getBase().toLLVMType(sourceFile);
577
578 // Retrieve the value of the original (source) struct, which is the only function parameter
579 5 llvm::Value *originalThisPtr = builder.GetInsertBlock()->getParent()->getArg(1);
580
581 5 const size_t fieldCount = structScope->getFieldCount();
582
2/2
✓ Branch 114 → 25 taken 7 times.
✓ Branch 114 → 115 taken 5 times.
12 for (size_t fieldIdx = 0; fieldIdx < fieldCount; fieldIdx++) {
583
1/2
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 7 times.
7 const SymbolTableEntry *fieldSymbol = structScope->lookupField(fieldIdx);
584
2/4
✓ Branch 30 → 31 taken 7 times.
✗ Branch 30 → 34 not taken.
✓ Branch 32 → 33 taken 7 times.
✗ Branch 32 → 34 not taken.
7 assert(fieldSymbol != nullptr && fieldSymbol->isField());
585
586 // Retrieve the address of the original field (move source)
587
1/2
✓ Branch 38 → 39 taken 7 times.
✗ Branch 38 → 123 not taken.
7 llvm::Value *originalFieldAddress = insertStructGEP(structType, originalThisPtr, fieldIdx);
588
589 7 const QualType &fieldType = fieldSymbol->getQualType();
590
591 // Call move ctor for struct fields if available, otherwise fall back to copy or shallow copy
592
2/2
✓ Branch 43 → 44 taken 2 times.
✓ Branch 43 → 79 taken 5 times.
7 if (fieldType.is(TY_STRUCT)) {
593 2 Scope *matchScope = fieldType.getBodyScope();
594 // First try to find a move ctor (non-const ref param). We scan the manifestations directly via
595 // findMoveCtor rather than FunctionManager::lookup with a non-const ref arg, because lookup permits
596 // const-param-to-non-const-arg "constify" matching and may return the copy ctor as a false positive.
597
1/2
✓ Branch 46 → 47 taken 2 times.
✗ Branch 46 → 54 not taken.
2 if (const Function *moveCtor = FunctionManager::findMoveCtor(matchScope)) {
598
2/4
✓ Branch 49 → 50 taken 2 times.
✗ Branch 49 → 131 not taken.
✓ Branch 50 → 51 taken 2 times.
✗ Branch 50 → 129 not taken.
4 generateCtorOrDtorCall(fieldSymbol, moveCtor, {originalFieldAddress});
599 2 continue;
600 }
601 // No move ctor: fall back to copy ctor for non-trivially copyable types
602 if (!fieldType.isTriviallyCopyable(nullptr)) {
603 const ArgList copyArgs = {{fieldType.toConstRef(nullptr), false}};
604 const Function *copyCtor = FunctionManager::lookup(matchScope, CTOR_FUNCTION_NAME, fieldType, copyArgs, false);
605 assert(copyCtor != nullptr);
606 generateCtorOrDtorCall(fieldSymbol, copyCtor, {originalFieldAddress});
607 continue;
608 }
609 }
610
611 // Retrieve the address of the new field (move dest)
612
2/2
✓ Branch 79 → 80 taken 4 times.
✓ Branch 79 → 88 taken 1 time.
5 if (!thisPtr)
613
2/4
✓ Branch 83 → 84 taken 4 times.
✗ Branch 83 → 158 not taken.
✓ Branch 84 → 85 taken 4 times.
✗ Branch 84 → 158 not taken.
4 thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr);
614
1/2
✓ Branch 91 → 92 taken 5 times.
✗ Branch 91 → 164 not taken.
5 llvm::Value *fieldAddress = insertStructGEP(structType, thisPtr, fieldIdx);
615
616 // For owning heap fields, transfer ownership: copy the pointer to the destination, and null out the source
617
2/2
✓ Branch 95 → 96 taken 3 times.
✓ Branch 95 → 111 taken 2 times.
5 if (fieldType.isHeap()) {
618
1/2
✗ Branch 97 → 98 not taken.
✓ Branch 97 → 99 taken 3 times.
3 assert(fieldType.isPtr());
619
620 // Load original heap address
621
2/4
✓ Branch 102 → 103 taken 3 times.
✗ Branch 102 → 170 not taken.
✓ Branch 103 → 104 taken 3 times.
✗ Branch 103 → 170 not taken.
3 llvm::Value *originalHeapAddress = insertLoad(builder.getPtrTy(), originalFieldAddress);
622 // Store it in the destination field
623 3 insertStore(originalHeapAddress, fieldAddress);
624 // Null out the source field so its dtor does not free the storage
625 3 insertStore(llvm::Constant::getNullValue(builder.getPtrTy()), originalFieldAddress);
626
627 3 continue;
628 3 }
629
630 // Shallow copy non-heap, non-struct (or trivially copyable struct) fields
631 2 llvm::Type *type = fieldType.toLLVMType(sourceFile);
632 2 generateShallowCopy(originalFieldAddress, type, fieldAddress, false);
633 }
634 5 }
635
636 10 void IRGenerator::generateDefaultMoveCtor(const Function *moveCtorFunction) {
637
3/6
✓ Branch 2 → 3 taken 10 times.
✗ Branch 2 → 6 not taken.
✓ Branch 3 → 4 taken 10 times.
✗ Branch 3 → 13 not taken.
✓ Branch 4 → 5 taken 10 times.
✗ Branch 4 → 6 not taken.
10 assert(moveCtorFunction->implicitDefault && moveCtorFunction->name == CTOR_FUNCTION_NAME);
638 15 const std::function<void()> generateBody = [&] { generateMoveCtorBodyPreamble(moveCtorFunction); };
639
1/2
✓ Branch 8 → 9 taken 10 times.
✗ Branch 8 → 11 not taken.
10 generateImplicitProcedure(generateBody, moveCtorFunction);
640 10 }
641
642 285 void IRGenerator::generateDtorBodyPreamble(const Function *dtorFunction) const {
643 // Retrieve struct scope
644 285 Scope *structScope = dtorFunction->bodyScope->parent;
645
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 285 times.
285 assert(structScope != nullptr);
646
647 // Get struct address
648
1/2
✓ Branch 6 → 7 taken 285 times.
✗ Branch 6 → 73 not taken.
855 const SymbolTableEntry *thisEntry = dtorFunction->bodyScope->lookupStrict(THIS_VARIABLE_NAME);
649
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 285 times.
285 assert(thisEntry != nullptr);
650 285 llvm::Value *thisPtrPtr = thisEntry->getAddress();
651
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 285 times.
285 assert(thisPtrPtr != nullptr);
652 285 llvm::Value *thisPtr = nullptr;
653
3/6
✓ Branch 17 → 18 taken 285 times.
✗ Branch 17 → 77 not taken.
✓ Branch 18 → 19 taken 285 times.
✗ Branch 18 → 77 not taken.
✓ Branch 19 → 20 taken 285 times.
✗ Branch 19 → 77 not taken.
285 llvm::Type *structType = thisEntry->getQualType().getBase().toLLVMType(sourceFile);
654
655 285 const size_t fieldCount = structScope->getFieldCount();
656
2/2
✓ Branch 69 → 22 taken 885 times.
✓ Branch 69 → 70 taken 285 times.
1170 for (size_t i = 0; i < fieldCount; i++) {
657 885 const size_t fieldIdx = fieldCount - 1 - i; // Destruct fields in reverse order
658
1/2
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 885 times.
885 const SymbolTableEntry *fieldSymbol = structScope->lookupField(fieldIdx);
659
2/4
✓ Branch 27 → 28 taken 885 times.
✗ Branch 27 → 31 not taken.
✓ Branch 29 → 30 taken 885 times.
✗ Branch 29 → 31 not taken.
885 assert(fieldSymbol != nullptr && fieldSymbol->isField());
660
661 // Call dtor for struct fields
662 885 const QualType &fieldType = fieldSymbol->getQualType();
663
2/2
✓ Branch 34 → 35 taken 230 times.
✓ Branch 34 → 50 taken 655 times.
885 if (fieldType.is(TY_STRUCT)) {
664 // Lookup dtor function and generate call if found
665
5/8
✓ Branch 38 → 39 taken 230 times.
✗ Branch 38 → 80 not taken.
✓ Branch 39 → 40 taken 230 times.
✗ Branch 39 → 78 not taken.
✓ Branch 40 → 41 taken 230 times.
✗ Branch 40 → 78 not taken.
✓ Branch 44 → 45 taken 205 times.
✓ Branch 44 → 49 taken 25 times.
690 if (const Function *dtorFct = FunctionManager::lookup(fieldType.getBodyScope(), DTOR_FUNCTION_NAME, fieldType, {}, false))
666
1/2
✓ Branch 46 → 47 taken 205 times.
✗ Branch 46 → 87 not taken.
205 generateCtorOrDtorCall(fieldSymbol, dtorFct, {});
667 230 continue;
668 230 }
669
670 // Deallocate fields, that are stored on the heap
671
2/2
✓ Branch 51 → 52 taken 152 times.
✓ Branch 51 → 68 taken 503 times.
655 if (fieldType.isHeap()) {
672 // Retrieve field address
673
2/2
✓ Branch 52 → 53 taken 147 times.
✓ Branch 52 → 61 taken 5 times.
152 if (!thisPtr)
674
2/4
✓ Branch 56 → 57 taken 147 times.
✗ Branch 56 → 90 not taken.
✓ Branch 57 → 58 taken 147 times.
✗ Branch 57 → 90 not taken.
147 thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr);
675
1/2
✓ Branch 64 → 65 taken 152 times.
✗ Branch 64 → 96 not taken.
152 llvm::Value *fieldAddress = insertStructGEP(structType, thisPtr, fieldIdx);
676 // Call dealloc function
677 152 generateDeallocCall(fieldAddress);
678 }
679 }
680 285 }
681
682 585 void IRGenerator::generateDefaultDtor(const Function *dtorFunction) {
683
3/6
✓ Branch 2 → 3 taken 585 times.
✗ Branch 2 → 6 not taken.
✓ Branch 3 → 4 taken 585 times.
✗ Branch 3 → 13 not taken.
✓ Branch 4 → 5 taken 585 times.
✗ Branch 4 → 6 not taken.
585 assert(dtorFunction->implicitDefault && dtorFunction->name == DTOR_FUNCTION_NAME);
684 870 const std::function<void()> generateBody = [&] { generateDtorBodyPreamble(dtorFunction); };
685
1/2
✓ Branch 8 → 9 taken 585 times.
✗ Branch 8 → 11 not taken.
585 generateImplicitProcedure(generateBody, dtorFunction);
686 585 }
687
688 4 void IRGenerator::generateTestMain() {
689 // Collect all test functions
690 4 std::vector<const std::vector<const Function *> *> tests;
691
5/8
✓ Branch 2 → 3 taken 4 times.
✗ Branch 2 → 138 not taken.
✓ Branch 3 → 4 taken 4 times.
✗ Branch 3 → 138 not taken.
✓ Branch 4 → 5 taken 4 times.
✗ Branch 4 → 138 not taken.
✓ Branch 15 → 6 taken 5 times.
✓ Branch 15 → 16 taken 4 times.
9 for (const auto &sourceFile : resourceManager.sourceFiles | std::views::values)
692
1/2
✓ Branch 9 → 10 taken 5 times.
✗ Branch 9 → 13 not taken.
5 if (!sourceFile->testFunctions.empty())
693
1/2
✓ Branch 11 → 12 taken 5 times.
✗ Branch 11 → 137 not taken.
5 tests.push_back(&sourceFile->testFunctions);
694
695 // Prepare printf function
696
1/2
✓ Branch 16 → 17 taken 4 times.
✗ Branch 16 → 265 not taken.
4 llvm::Function *printfFct = stdFunctionManager.getPrintfFct();
697
698 // Prepare success and error messages
699
3/6
✓ Branch 19 → 20 taken 4 times.
✗ Branch 19 → 147 not taken.
✓ Branch 22 → 23 taken 4 times.
✗ Branch 22 → 141 not taken.
✓ Branch 23 → 24 taken 4 times.
✗ Branch 23 → 139 not taken.
16 llvm::Constant *allStartMsg = createGlobalStringConst("allStartMsg", TEST_ALL_START_MSG, *rootScope->codeLoc);
700
3/6
✓ Branch 30 → 31 taken 4 times.
✗ Branch 30 → 159 not taken.
✓ Branch 33 → 34 taken 4 times.
✗ Branch 33 → 153 not taken.
✓ Branch 34 → 35 taken 4 times.
✗ Branch 34 → 151 not taken.
16 llvm::Constant *allEndMsg = createGlobalStringConst("allEndMsg", TEST_ALL_END_MSG, *rootScope->codeLoc);
701
3/6
✓ Branch 41 → 42 taken 4 times.
✗ Branch 41 → 171 not taken.
✓ Branch 44 → 45 taken 4 times.
✗ Branch 44 → 165 not taken.
✓ Branch 45 → 46 taken 4 times.
✗ Branch 45 → 163 not taken.
16 llvm::Constant *fileStartMsg = createGlobalStringConst("fileStartMsg", TEST_FILE_START_MSG, *rootScope->codeLoc);
702
3/6
✓ Branch 52 → 53 taken 4 times.
✗ Branch 52 → 183 not taken.
✓ Branch 55 → 56 taken 4 times.
✗ Branch 55 → 177 not taken.
✓ Branch 56 → 57 taken 4 times.
✗ Branch 56 → 175 not taken.
16 llvm::Constant *fileEndMsg = createGlobalStringConst("fileEndMsg", TEST_FILE_END_MSG, *rootScope->codeLoc);
703
3/6
✓ Branch 63 → 64 taken 4 times.
✗ Branch 63 → 195 not taken.
✓ Branch 66 → 67 taken 4 times.
✗ Branch 66 → 189 not taken.
✓ Branch 67 → 68 taken 4 times.
✗ Branch 67 → 187 not taken.
16 llvm::Constant *runMsg = createGlobalStringConst("runMsg", TEST_CASE_RUN_MSG, *rootScope->codeLoc);
704
3/6
✓ Branch 74 → 75 taken 4 times.
✗ Branch 74 → 207 not taken.
✓ Branch 77 → 78 taken 4 times.
✗ Branch 77 → 201 not taken.
✓ Branch 78 → 79 taken 4 times.
✗ Branch 78 → 199 not taken.
16 llvm::Constant *successMsg = createGlobalStringConst("successMsg", TEST_CASE_SUCCESS_MSG, *rootScope->codeLoc);
705
3/6
✓ Branch 85 → 86 taken 4 times.
✗ Branch 85 → 219 not taken.
✓ Branch 88 → 89 taken 4 times.
✗ Branch 88 → 213 not taken.
✓ Branch 89 → 90 taken 4 times.
✗ Branch 89 → 211 not taken.
16 llvm::Constant *errorMsg = createGlobalStringConst("errorMsg", TEST_CASE_FAILED_MSG, *rootScope->codeLoc);
706
3/6
✓ Branch 96 → 97 taken 4 times.
✗ Branch 96 → 231 not taken.
✓ Branch 99 → 100 taken 4 times.
✗ Branch 99 → 225 not taken.
✓ Branch 100 → 101 taken 4 times.
✗ Branch 100 → 223 not taken.
16 llvm::Constant *skippedMsg = createGlobalStringConst("skippedMsg", TEST_CASE_SKIPPED_MSG, *rootScope->codeLoc);
707
708 // Prepare entry for test main
709
1/2
✓ Branch 105 → 106 taken 4 times.
✗ Branch 105 → 265 not taken.
4 QualType functionType(TY_FUNCTION);
710
1/2
✓ Branch 106 → 107 taken 4 times.
✗ Branch 106 → 265 not taken.
4 functionType.setQualifiers(TypeQualifiers::of(TY_FUNCTION));
711
1/2
✓ Branch 108 → 109 taken 4 times.
✗ Branch 108 → 265 not taken.
4 functionType.makePublic();
712
2/4
✓ Branch 111 → 112 taken 4 times.
✗ Branch 111 → 237 not taken.
✓ Branch 112 → 113 taken 4 times.
✗ Branch 112 → 235 not taken.
8 SymbolTableEntry entry(MAIN_FUNCTION_NAME, functionType, rootScope, nullptr, 0, false);
713
714 // Prepare test main function
715
4/8
✓ Branch 117 → 118 taken 4 times.
✗ Branch 117 → 248 not taken.
✓ Branch 118 → 119 taken 4 times.
✗ Branch 118 → 247 not taken.
✓ Branch 121 → 122 taken 4 times.
✗ Branch 121 → 243 not taken.
✓ Branch 122 → 123 taken 4 times.
✗ Branch 122 → 241 not taken.
12 Function testMain(MAIN_FUNCTION_NAME, &entry, QualType(TY_DYN), QualType(TY_INT), {}, {}, nullptr);
716 4 testMain.used = true; // Mark as used to prevent removal
717 4 testMain.implicitDefault = true;
718 4 testMain.mangleFunctionName = false;
719
720 // Prepare scope
721
2/4
✓ Branch 127 → 128 taken 4 times.
✗ Branch 127 → 257 not taken.
✓ Branch 128 → 129 taken 4 times.
✗ Branch 128 → 255 not taken.
4 rootScope->createChildScope(testMain.getScopeName(), ScopeType::FUNC_PROC_BODY, nullptr);
722
723 // Generate
724 const std::function<void()> generateBody = [&] {
725 // Prepare result variable
726 4 std::vector<llvm::Value *> testCaseResults;
727
1/2
✓ Branch 3 → 4 taken 4 times.
✗ Branch 3 → 297 not taken.
4 testCaseResults.reserve(tests.size());
728
729 // Print start message
730 5 const auto accFct = [&](size_t sum, const std::vector<const Function *> *innerVector) { return sum + innerVector->size(); };
731 4 const size_t totalTestCount = std::accumulate(tests.begin(), tests.end(), 0, accFct);
732
5/10
✓ Branch 7 → 8 taken 4 times.
✗ Branch 7 → 217 not taken.
✓ Branch 8 → 9 taken 4 times.
✗ Branch 8 → 215 not taken.
✓ Branch 10 → 11 taken 4 times.
✗ Branch 10 → 215 not taken.
✓ Branch 12 → 13 taken 4 times.
✗ Branch 12 → 214 not taken.
✓ Branch 13 → 14 taken 4 times.
✗ Branch 13 → 214 not taken.
4 builder.CreateCall(printfFct, {allStartMsg, builder.getInt32(totalTestCount), builder.getInt32(tests.size())});
733
734 // Generate a call to each test function
735
2/2
✓ Branch 181 → 16 taken 5 times.
✓ Branch 181 → 182 taken 4 times.
13 for (const std::vector<const Function *> *testSuite : tests) {
736 // Print test suite prologue
737
1/2
✓ Branch 19 → 20 taken 5 times.
✗ Branch 19 → 287 not taken.
5 const std::string fileName = testSuite->front()->bodyScope->sourceFile->fileName;
738
3/6
✓ Branch 21 → 22 taken 5 times.
✗ Branch 21 → 285 not taken.
✓ Branch 24 → 25 taken 5 times.
✗ Branch 24 → 220 not taken.
✓ Branch 25 → 26 taken 5 times.
✗ Branch 25 → 218 not taken.
10 llvm::Constant *fileNameValue = createGlobalStringConst("fileName", fileName, testSuite->front()->getDeclCodeLoc());
739
4/8
✓ Branch 28 → 29 taken 5 times.
✗ Branch 28 → 227 not taken.
✓ Branch 30 → 31 taken 5 times.
✗ Branch 30 → 225 not taken.
✓ Branch 32 → 33 taken 5 times.
✗ Branch 32 → 224 not taken.
✓ Branch 33 → 34 taken 5 times.
✗ Branch 33 → 224 not taken.
5 builder.CreateCall(printfFct, {fileStartMsg, builder.getInt32(testSuite->size()), fileNameValue});
740
741
3/4
✓ Branch 38 → 39 taken 10 times.
✗ Branch 38 → 279 not taken.
✓ Branch 164 → 36 taken 10 times.
✓ Branch 164 → 165 taken 5 times.
30 for (const Function *testFunction : *testSuite) {
742
1/2
✗ Branch 50 → 51 not taken.
✓ Branch 50 → 52 taken 10 times.
10 assert(testFunction->isNormalFunction());
743
1/2
✗ Branch 53 → 54 not taken.
✓ Branch 53 → 55 taken 10 times.
10 assert(testFunction->paramList.empty());
744
745 // Retrieve attribute list for the test function
746
2/4
✓ Branch 55 → 56 taken 10 times.
✗ Branch 55 → 279 not taken.
✗ Branch 56 → 57 not taken.
✓ Branch 56 → 58 taken 10 times.
10 assert(testFunction->declNode->isFctOrProcDef());
747
1/2
✓ Branch 58 → 59 taken 10 times.
✗ Branch 58 → 60 not taken.
10 const auto fctDefNode = spice_pointer_cast<FctDefBaseNode *>(testFunction->declNode);
748
1/2
✗ Branch 65 → 66 not taken.
✓ Branch 65 → 67 taken 10 times.
10 assert(fctDefNode->attrs != nullptr);
749 10 const AttrLstNode *attrs = fctDefNode->attrs->attrLst;
750
3/6
✓ Branch 69 → 70 taken 10 times.
✗ Branch 69 → 230 not taken.
✓ Branch 70 → 71 taken 10 times.
✗ Branch 70 → 228 not taken.
✗ Branch 71 → 72 not taken.
✓ Branch 71 → 73 taken 10 times.
20 assert(attrs->getAttrValueByName(ATTR_TEST)->boolValue); // The test attribute must be present
751
2/4
✓ Branch 77 → 78 taken 10 times.
✗ Branch 77 → 236 not taken.
✓ Branch 78 → 79 taken 10 times.
✗ Branch 78 → 234 not taken.
20 const CompileTimeValue *testSkipAttr = attrs->getAttrValueByName(ATTR_TEST_SKIP);
752
4/4
✓ Branch 81 → 82 taken 3 times.
✓ Branch 81 → 84 taken 7 times.
✓ Branch 82 → 83 taken 1 time.
✓ Branch 82 → 84 taken 2 times.
10 const bool skipTest = testSkipAttr && testSkipAttr->boolValue;
753
2/4
✓ Branch 87 → 88 taken 10 times.
✗ Branch 87 → 242 not taken.
✓ Branch 88 → 89 taken 10 times.
✗ Branch 88 → 240 not taken.
20 const CompileTimeValue *testNameAttr = attrs->getAttrValueByName(ATTR_TEST_NAME);
754
755 // Prepare test name
756
1/2
✓ Branch 91 → 92 taken 10 times.
✗ Branch 91 → 279 not taken.
10 std::stringstream testName;
757
1/2
✓ Branch 92 → 93 taken 10 times.
✗ Branch 92 → 277 not taken.
10 testName << testFunction->name;
758
2/2
✓ Branch 93 → 94 taken 1 time.
✓ Branch 93 → 98 taken 9 times.
10 if (testNameAttr)
759
4/8
✓ Branch 94 → 95 taken 1 time.
✗ Branch 94 → 277 not taken.
✓ Branch 95 → 96 taken 1 time.
✗ Branch 95 → 277 not taken.
✓ Branch 96 → 97 taken 1 time.
✗ Branch 96 → 277 not taken.
✓ Branch 97 → 98 taken 1 time.
✗ Branch 97 → 277 not taken.
1 testName << " (" << resourceManager.compileTimeStringValues.at(testNameAttr->stringValueOffset) << ")";
760
761 // Print test case run message
762
4/8
✓ Branch 98 → 99 taken 10 times.
✗ Branch 98 → 277 not taken.
✓ Branch 99 → 100 taken 10 times.
✗ Branch 99 → 254 not taken.
✓ Branch 102 → 103 taken 10 times.
✗ Branch 102 → 248 not taken.
✓ Branch 103 → 104 taken 10 times.
✗ Branch 103 → 246 not taken.
30 llvm::Constant *testNameValue = createGlobalStringConst("testName", testName.str(), testFunction->getDeclCodeLoc());
763
5/8
✓ Branch 107 → 108 taken 10 times.
✗ Branch 107 → 258 not taken.
✓ Branch 109 → 110 taken 10 times.
✗ Branch 109 → 255 not taken.
✓ Branch 110 → 111 taken 10 times.
✗ Branch 110 → 255 not taken.
✓ Branch 111 → 112 taken 1 time.
✓ Branch 111 → 117 taken 9 times.
10 builder.CreateCall(printfFct, {runMsg, testNameValue});
764
765
2/2
✓ Branch 111 → 112 taken 1 time.
✓ Branch 111 → 117 taken 9 times.
10 if (skipTest) {
766 // Print test case skip message
767
3/6
✓ Branch 112 → 113 taken 1 time.
✗ Branch 112 → 262 not taken.
✓ Branch 114 → 115 taken 1 time.
✗ Branch 114 → 259 not taken.
✓ Branch 115 → 116 taken 1 time.
✗ Branch 115 → 259 not taken.
1 builder.CreateCall(printfFct, {skippedMsg, testNameValue});
768 1 continue;
769 }
770
771 // Test function is not defined in the current module -> declare it
772
1/2
✓ Branch 117 → 118 taken 9 times.
✗ Branch 117 → 277 not taken.
9 const std::string mangledName = testFunction->getMangledName();
773
3/4
✓ Branch 119 → 120 taken 9 times.
✗ Branch 119 → 263 not taken.
✓ Branch 120 → 121 taken 2 times.
✓ Branch 120 → 133 taken 7 times.
9 if (!module->getFunction(mangledName)) {
774
2/4
✓ Branch 121 → 122 taken 2 times.
✗ Branch 121 → 275 not taken.
✗ Branch 122 → 123 not taken.
✓ Branch 122 → 124 taken 2 times.
2 assert(testFunction->returnType.is(TY_BOOL));
775
1/2
✗ Branch 125 → 126 not taken.
✓ Branch 125 → 127 taken 2 times.
2 assert(testFunction->paramList.empty());
776
2/4
✓ Branch 128 → 129 taken 2 times.
✗ Branch 128 → 264 not taken.
✓ Branch 129 → 130 taken 2 times.
✗ Branch 129 → 264 not taken.
2 llvm::FunctionType *fctType = llvm::FunctionType::get(builder.getInt1Ty(), {}, false);
777
1/2
✓ Branch 131 → 132 taken 2 times.
✗ Branch 131 → 265 not taken.
2 module->getOrInsertFunction(mangledName, fctType);
778 }
779
780 // Call test function
781
1/2
✓ Branch 134 → 135 taken 9 times.
✗ Branch 134 → 266 not taken.
9 llvm::Function *callee = module->getFunction(mangledName);
782
1/2
✗ Branch 135 → 136 not taken.
✓ Branch 135 → 137 taken 9 times.
9 assert(callee != nullptr);
783
4/8
✓ Branch 137 → 138 taken 9 times.
✗ Branch 137 → 269 not taken.
✓ Branch 139 → 140 taken 9 times.
✗ Branch 139 → 267 not taken.
✓ Branch 140 → 141 taken 9 times.
✗ Branch 140 → 267 not taken.
✓ Branch 141 → 142 taken 9 times.
✗ Branch 141 → 275 not taken.
9 llvm::Value *testCaseResult = builder.CreateCall(callee);
784
1/2
✓ Branch 141 → 142 taken 9 times.
✗ Branch 141 → 275 not taken.
9 testCaseResults.push_back(testCaseResult);
785
786 // Print test case result message
787
3/6
✓ Branch 142 → 143 taken 9 times.
✗ Branch 142 → 270 not taken.
✓ Branch 143 → 144 taken 9 times.
✗ Branch 143 → 270 not taken.
✓ Branch 144 → 145 taken 9 times.
✗ Branch 144 → 274 not taken.
9 llvm::Value *message = builder.CreateSelect(testCaseResult, successMsg, errorMsg);
788
3/6
✓ Branch 144 → 145 taken 9 times.
✗ Branch 144 → 274 not taken.
✓ Branch 146 → 147 taken 9 times.
✗ Branch 146 → 271 not taken.
✓ Branch 147 → 148 taken 9 times.
✗ Branch 147 → 271 not taken.
9 builder.CreateCall(printfFct, {message, testNameValue});
789
2/2
✓ Branch 151 → 152 taken 9 times.
✓ Branch 151 → 154 taken 1 time.
10 }
790
791 // Print test suite epilogue
792
4/8
✓ Branch 165 → 166 taken 5 times.
✗ Branch 165 → 284 not taken.
✓ Branch 167 → 168 taken 5 times.
✗ Branch 167 → 282 not taken.
✓ Branch 169 → 170 taken 5 times.
✗ Branch 169 → 281 not taken.
✓ Branch 170 → 171 taken 5 times.
✗ Branch 170 → 281 not taken.
5 builder.CreateCall(printfFct, {fileEndMsg, builder.getInt32(testSuite->size()), fileNameValue});
793 5 }
794
795 // Print end message
796
6/12
✓ Branch 182 → 183 taken 4 times.
✗ Branch 182 → 292 not taken.
✓ Branch 183 → 184 taken 4 times.
✗ Branch 183 → 290 not taken.
✓ Branch 185 → 186 taken 4 times.
✗ Branch 185 → 290 not taken.
✓ Branch 187 → 188 taken 4 times.
✗ Branch 187 → 289 not taken.
✓ Branch 188 → 189 taken 4 times.
✗ Branch 188 → 289 not taken.
✓ Branch 189 → 190 taken 4 times.
✗ Branch 189 → 297 not taken.
4 builder.CreateCall(printfFct, {allEndMsg, builder.getInt32(totalTestCount), builder.getInt32(tests.size())});
797
798 // Compute overall result
799
1/2
✓ Branch 189 → 190 taken 4 times.
✗ Branch 189 → 297 not taken.
4 llvm::Value *overallResult = builder.getTrue();
800
2/2
✓ Branch 205 → 192 taken 9 times.
✓ Branch 205 → 206 taken 4 times.
17 for (llvm::Value *testCaseResult : testCaseResults)
801
2/4
✓ Branch 194 → 195 taken 9 times.
✗ Branch 194 → 293 not taken.
✓ Branch 195 → 196 taken 9 times.
✗ Branch 195 → 293 not taken.
9 overallResult = builder.CreateAnd(overallResult, testCaseResult);
802
803 // Return code must be 0 for success and 1 for failure, so we need to invert the result and zero extend to 32 bit
804
3/6
✓ Branch 206 → 207 taken 4 times.
✗ Branch 206 → 295 not taken.
✓ Branch 207 → 208 taken 4 times.
✗ Branch 207 → 295 not taken.
✓ Branch 208 → 209 taken 4 times.
✗ Branch 208 → 296 not taken.
4 llvm::Value *overallResultNegated = builder.CreateNot(overallResult);
805
4/8
✓ Branch 208 → 209 taken 4 times.
✗ Branch 208 → 296 not taken.
✓ Branch 209 → 210 taken 4 times.
✗ Branch 209 → 296 not taken.
✓ Branch 210 → 211 taken 4 times.
✗ Branch 210 → 296 not taken.
✓ Branch 211 → 212 taken 4 times.
✗ Branch 211 → 297 not taken.
4 llvm::Value *exitCode = builder.CreateZExt(overallResultNegated, builder.getInt32Ty());
806
1/2
✓ Branch 211 → 212 taken 4 times.
✗ Branch 211 → 297 not taken.
4 builder.CreateRet(exitCode);
807
1/2
✓ Branch 130 → 131 taken 4 times.
✗ Branch 130 → 258 not taken.
8 };
808
1/2
✓ Branch 131 → 132 taken 4 times.
✗ Branch 131 → 259 not taken.
4 generateImplicitFunction(generateBody, &testMain);
809 4 }
810
811 } // namespace spice::compiler
812