GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 98.2% 428 / 6 / 442
Functions: 96.2% 50 / 0 / 52
Branches: 63.3% 470 / 20 / 762

src/irgenerator/IRGenerator.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 <driver/Driver.h>
7 #include <global/GlobalResourceManager.h>
8 #include <model/Function.h>
9 #include <symboltablebuilder/SymbolTableBuilder.h>
10 #include <typechecker/FunctionManager.h>
11
12 #include <llvm/IR/Module.h>
13 #include <llvm/IR/Verifier.h>
14
15 namespace spice::compiler {
16
17 const std::string PRODUCER_STRING = "spice version " + std::string(SPICE_VERSION) + " (https://github.com/spicelang/spice)";
18
19 2220 IRGenerator::IRGenerator(GlobalResourceManager &resourceManager, SourceFile *sourceFile)
20 2220 : CompilerPass(resourceManager, sourceFile), context(cliOptions.useLTO ? resourceManager.ltoContext : sourceFile->context),
21
1/2
✓ Branch 8 → 9 taken 2220 times.
✗ Branch 8 → 78 not taken.
2220 builder(sourceFile->builder), module(sourceFile->llvmModule.get()), conversionManager(sourceFile, this),
22
6/10
✓ Branch 4 → 5 taken 4 times.
✓ Branch 4 → 6 taken 2216 times.
✓ Branch 9 → 10 taken 2220 times.
✗ Branch 9 → 78 not taken.
✓ Branch 10 → 11 taken 2220 times.
✗ Branch 10 → 78 not taken.
✓ Branch 11 → 12 taken 2220 times.
✗ Branch 11 → 76 not taken.
✓ Branch 15 → 16 taken 2220 times.
✗ Branch 15 → 72 not taken.
4440 stdFunctionManager(sourceFile, resourceManager, module) {
23 // Attach information to the module
24
1/2
✓ Branch 19 → 20 taken 2220 times.
✗ Branch 19 → 51 not taken.
2220 module->setTargetTriple(cliOptions.targetTriple);
25
2/4
✓ Branch 23 → 24 taken 2220 times.
✗ Branch 23 → 54 not taken.
✓ Branch 24 → 25 taken 2220 times.
✗ Branch 24 → 52 not taken.
2220 module->setDataLayout(sourceFile->targetMachine->createDataLayout());
26
2/2
✓ Branch 26 → 27 taken 1 time.
✓ Branch 26 → 29 taken 2219 times.
2220 if (cliOptions.outputContainer == OutputContainer::SHARED_LIBRARY) {
27
1/2
✓ Branch 27 → 28 taken 1 time.
✗ Branch 27 → 64 not taken.
1 module->setPICLevel(llvm::PICLevel::SmallPIC);
28
1/2
✓ Branch 28 → 31 taken 1 time.
✗ Branch 28 → 64 not taken.
1 module->setPIELevel(llvm::PIELevel::Default);
29 } else {
30
1/2
✓ Branch 29 → 30 taken 2219 times.
✗ Branch 29 → 64 not taken.
2219 module->setPICLevel(llvm::PICLevel::BigPIC);
31
1/2
✓ Branch 30 → 31 taken 2219 times.
✗ Branch 30 → 64 not taken.
2219 module->setPIELevel(llvm::PIELevel::Large);
32 }
33
1/2
✓ Branch 31 → 32 taken 2220 times.
✗ Branch 31 → 64 not taken.
2220 module->setUwtable(llvm::UWTableKind::Default);
34
1/2
✓ Branch 32 → 33 taken 2220 times.
✗ Branch 32 → 64 not taken.
2220 module->setFramePointer(llvm::FramePointerKind::All);
35
36 // Add module identifier metadata
37
2/4
✓ Branch 33 → 34 taken 2220 times.
✗ Branch 33 → 55 not taken.
✓ Branch 34 → 35 taken 2220 times.
✗ Branch 34 → 55 not taken.
2220 llvm::NamedMDNode *identifierMetadata = module->getOrInsertNamedMetadata("llvm.ident");
38
3/6
✓ Branch 36 → 37 taken 2220 times.
✗ Branch 36 → 56 not taken.
✓ Branch 38 → 39 taken 2220 times.
✗ Branch 38 → 56 not taken.
✓ Branch 39 → 40 taken 2220 times.
✗ Branch 39 → 56 not taken.
2220 identifierMetadata->addOperand(llvm::MDNode::get(context, llvm::MDString::get(context, PRODUCER_STRING)));
39
40 // Initialize common LLVM types
41
4/8
✓ Branch 40 → 41 taken 2220 times.
✗ Branch 40 → 59 not taken.
✓ Branch 41 → 42 taken 2220 times.
✗ Branch 41 → 59 not taken.
✓ Branch 42 → 43 taken 2220 times.
✗ Branch 42 → 59 not taken.
✓ Branch 44 → 45 taken 2220 times.
✗ Branch 44 → 59 not taken.
2220 llvmTypes.lambdaFatPtrType = llvm::StructType::get(context, {builder.getPtrTy(), builder.getPtrTy(), builder.getInt64Ty()});
42
43 // Initialize debug info generator
44
2/2
✓ Branch 45 → 46 taken 27 times.
✓ Branch 45 → 50 taken 2193 times.
2220 if (cliOptions.instrumentation.generateDebugInfo)
45
2/4
✓ Branch 46 → 47 taken 27 times.
✗ Branch 46 → 63 not taken.
✓ Branch 47 → 48 taken 27 times.
✗ Branch 47 → 61 not taken.
27 diGenerator.initialize(sourceFile->fileName, sourceFile->fileDir);
46 2220 }
47
48 2220 std::any IRGenerator::visitEntry(const EntryNode *node) {
49 // Generate IR
50
1/2
✓ Branch 2 → 3 taken 2220 times.
✗ Branch 2 → 28 not taken.
2220 visitChildren(node);
51
52 // Generate test main if required
53
4/4
✓ Branch 4 → 5 taken 408 times.
✓ Branch 4 → 7 taken 1812 times.
✓ Branch 5 → 6 taken 4 times.
✓ Branch 5 → 7 taken 404 times.
2220 if (sourceFile->isMainFile && cliOptions.generateTestMain)
54 4 generateTestMain();
55
56 // Execute deferred VTable initializations
57
2/2
✓ Branch 21 → 9 taken 2253 times.
✓ Branch 21 → 22 taken 2220 times.
6693 for (DeferredLogic &deferredVTableInit : deferredVTableInitializations)
58
1/2
✓ Branch 11 → 12 taken 2253 times.
✗ Branch 11 → 29 not taken.
2253 deferredVTableInit.execute();
59
60 // Finalize debug info generator
61 2220 diGenerator.finalize();
62
63 // Verify module
64 2220 verifyModule(node->codeLoc);
65
66
1/2
✓ Branch 24 → 25 taken 2220 times.
✗ Branch 24 → 30 not taken.
4440 return nullptr;
67 }
68
69 120147 llvm::AllocaInst *IRGenerator::insertAlloca(llvm::Type *llvmType, const std::string &varName) {
70
2/2
✓ Branch 2 → 3 taken 85510 times.
✓ Branch 2 → 8 taken 34637 times.
120147 if (allocaInsertInst != nullptr) { // If there is already an alloca inst, insert right after that
71
2/4
✓ Branch 3 → 4 taken 85510 times.
✗ Branch 3 → 19 not taken.
✓ Branch 4 → 5 taken 85510 times.
✗ Branch 4 → 19 not taken.
85510 llvm::AllocaInst *allocaInst = builder.CreateAlloca(llvmType, nullptr, varName);
72 85510 allocaInst->dropLocation(); // Part of prologue
73 85510 allocaInst->moveAfter(allocaInsertInst);
74 85510 allocaInsertInst = allocaInst;
75 } else { // This is the first alloca inst in the current function -> insert at the entry block
76 // Save current basic block and move insert cursor to entry block of the current function
77 34637 llvm::BasicBlock *currentBlock = builder.GetInsertBlock();
78 34637 builder.SetInsertPoint(allocaInsertBlock, allocaInsertBlock->begin());
79
80 // Allocate the size of the given LLVM type
81
2/4
✓ Branch 11 → 12 taken 34637 times.
✗ Branch 11 → 20 not taken.
✓ Branch 12 → 13 taken 34637 times.
✗ Branch 12 → 20 not taken.
34637 allocaInsertInst = builder.CreateAlloca(llvmType, nullptr, varName);
82 34637 allocaInsertInst->dropLocation(); // Part of prologue
83
84 // Restore old basic block
85 34637 builder.SetInsertPoint(currentBlock);
86 }
87
88 // Insert lifetime start marker
89
2/2
✓ Branch 15 → 16 taken 54 times.
✓ Branch 15 → 17 taken 120093 times.
120147 if (cliOptions.useLifetimeMarkers)
90 54 builder.CreateLifetimeStart(allocaInsertInst);
91
92 120147 return allocaInsertInst;
93 }
94
95 98993 llvm::AllocaInst *IRGenerator::insertAlloca(const QualType &qualType, const std::string &varName) {
96 98993 llvm::Type *llvmType = qualType.toLLVMType(sourceFile);
97 98993 llvm::AllocaInst *alloca = insertAlloca(llvmType, varName);
98
99 // Insert type metadata
100
2/2
✓ Branch 4 → 5 taken 9 times.
✓ Branch 4 → 6 taken 98984 times.
98993 if (cliOptions.useTBAAMetadata)
101 9 mdGenerator.generateTypeMetadata(allocaInsertInst, qualType);
102
103 98993 return alloca;
104 }
105
106 224308 llvm::LoadInst *IRGenerator::insertLoad(llvm::Type *llvmType, llvm::Value *ptr, bool isVolatile,
107 const std::string &varName) const {
108
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 224308 times.
224308 assert(ptr->getType()->isPointerTy());
109
2/4
✓ Branch 6 → 7 taken 224308 times.
✗ Branch 6 → 11 not taken.
✓ Branch 7 → 8 taken 224308 times.
✗ Branch 7 → 11 not taken.
224308 return builder.CreateLoad(llvmType, ptr, isVolatile, varName);
110 }
111
112 191548 llvm::LoadInst *IRGenerator::insertLoad(const QualType &qualType, llvm::Value *ptr, bool isVolatile, const std::string &varName) {
113 191548 llvm::Type *llvmType = qualType.toLLVMType(sourceFile);
114 191548 llvm::LoadInst *load = insertLoad(llvmType, ptr, isVolatile, varName);
115
2/2
✓ Branch 4 → 5 taken 6 times.
✓ Branch 4 → 6 taken 191542 times.
191548 if (cliOptions.useTBAAMetadata)
116 6 mdGenerator.generateTBAAMetadata(load, qualType);
117 191548 return load;
118 }
119
120 120420 llvm::StoreInst *IRGenerator::insertStore(llvm::Value *val, llvm::Value *ptr, bool isVolatile) const {
121
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 120420 times.
120420 assert(ptr->getType()->isPointerTy());
122 120420 return builder.CreateStore(val, ptr, isVolatile);
123 }
124
125 33624 void IRGenerator::insertStore(llvm::Value *val, llvm::Value *ptr, const QualType &qualType, bool isVolatile) {
126 33624 llvm::StoreInst *store = insertStore(val, ptr, isVolatile);
127
2/2
✓ Branch 3 → 4 taken 9 times.
✓ Branch 3 → 5 taken 33615 times.
33624 if (cliOptions.useTBAAMetadata)
128 9 mdGenerator.generateTBAAMetadata(store, qualType);
129 33624 }
130
131 62774 llvm::Value *IRGenerator::insertInBoundsGEP(llvm::Type *type, llvm::Value *basePtr, llvm::ArrayRef<llvm::Value *> indices,
132 const std::string &varName) const {
133
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 62774 times.
62774 assert(basePtr->getType()->isPointerTy());
134
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 62774 times.
62774 assert(!indices.empty());
135
4/6
✓ Branch 4 → 5 taken 62086 times.
✓ Branch 4 → 7 taken 56942 times.
✓ Branch 6 → 7 taken 62086 times.
✗ Branch 6 → 8 not taken.
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 62774 times.
181802 assert(std::ranges::all_of(indices, [](const llvm::Value *index) {
136 const llvm::Type *indexType = index->getType();
137 return indexType->isIntegerTy(32) || indexType->isIntegerTy(64);
138 }));
139
140 // Insert GEP
141
2/4
✓ Branch 12 → 13 taken 62774 times.
✗ Branch 12 → 17 not taken.
✓ Branch 13 → 14 taken 62774 times.
✗ Branch 13 → 17 not taken.
62774 return builder.CreateInBoundsGEP(type, basePtr, indices, varName);
142 }
143
144 25826 llvm::Value *IRGenerator::insertStructGEP(llvm::Type *type, llvm::Value *basePtr, unsigned int index,
145 const std::string &varName) const {
146
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 25826 times.
25826 assert(basePtr->getType()->isPointerTy());
147
148 // If we use index 0 we can use the base pointer directly
149
2/2
✓ Branch 6 → 7 taken 8061 times.
✓ Branch 6 → 8 taken 17765 times.
25826 if (index == 0)
150 8061 return basePtr;
151
152 // Insert GEP
153
2/4
✓ Branch 8 → 9 taken 17765 times.
✗ Branch 8 → 13 not taken.
✓ Branch 9 → 10 taken 17765 times.
✗ Branch 9 → 13 not taken.
17765 return builder.CreateStructGEP(type, basePtr, index, varName);
154 }
155
156 129003 llvm::Value *IRGenerator::resolveValue(const ExprNode *node) {
157 // Visit the given AST node
158
2/4
✓ Branch 2 → 3 taken 129003 times.
✗ Branch 2 → 11 not taken.
✓ Branch 3 → 4 taken 129003 times.
✗ Branch 3 → 9 not taken.
129003 auto exprResult = any_cast<LLVMExprResult>(visit(node));
159
1/2
✓ Branch 5 → 6 taken 129003 times.
✗ Branch 5 → 12 not taken.
258006 return resolveValue(node, exprResult);
160 }
161
162 141302 llvm::Value *IRGenerator::resolveValue(const ExprNode *node, LLVMExprResult &exprResult) {
163 141302 return resolveValue(node->getEvaluatedSymbolType(manIdx), exprResult);
164 }
165
166 270288 llvm::Value *IRGenerator::resolveValue(const QualType &qualType, LLVMExprResult &exprResult) {
167 // Check if the value is already present
168
2/2
✓ Branch 2 → 3 taken 86173 times.
✓ Branch 2 → 4 taken 184115 times.
270288 if (exprResult.value != nullptr)
169 86173 return exprResult.value;
170
171 // Check if a constant is present
172
2/2
✓ Branch 4 → 5 taken 56377 times.
✓ Branch 4 → 7 taken 127738 times.
184115 if (exprResult.constant != nullptr) {
173 56377 materializeConstant(exprResult);
174 56377 return exprResult.value;
175 }
176
177
3/4
✓ Branch 7 → 8 taken 1623 times.
✓ Branch 7 → 10 taken 126115 times.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 1623 times.
127738 assert(exprResult.ptr != nullptr || exprResult.refPtr != nullptr);
178
179 // De-reference if reference type
180
4/4
✓ Branch 10 → 11 taken 118123 times.
✓ Branch 10 → 13 taken 9615 times.
✓ Branch 11 → 12 taken 7 times.
✓ Branch 11 → 13 taken 118116 times.
127738 const bool isVolatile = exprResult.entry && exprResult.entry->isVolatile;
181
4/4
✓ Branch 14 → 15 taken 1633 times.
✓ Branch 14 → 24 taken 126105 times.
✓ Branch 15 → 16 taken 1623 times.
✓ Branch 15 → 24 taken 10 times.
127738 if (exprResult.refPtr != nullptr && exprResult.ptr == nullptr)
182
2/4
✓ Branch 19 → 20 taken 1623 times.
✗ Branch 19 → 34 not taken.
✓ Branch 20 → 21 taken 1623 times.
✗ Branch 20 → 34 not taken.
1623 exprResult.ptr = insertLoad(builder.getPtrTy(), exprResult.refPtr, isVolatile);
183
184 // Load the value from the pointer
185
1/2
✓ Branch 24 → 25 taken 127738 times.
✗ Branch 24 → 46 not taken.
127738 const QualType referencedType = qualType.removeReferenceWrapper();
186
1/2
✓ Branch 28 → 29 taken 127738 times.
✗ Branch 28 → 40 not taken.
127738 exprResult.value = insertLoad(referencedType, exprResult.ptr, isVolatile);
187
188 127738 return exprResult.value;
189 }
190
191 13502 llvm::Value *IRGenerator::resolveAddress(const ASTNode *node) {
192 // Visit the given AST node
193
2/4
✓ Branch 2 → 3 taken 13502 times.
✗ Branch 2 → 11 not taken.
✓ Branch 3 → 4 taken 13502 times.
✗ Branch 3 → 9 not taken.
13502 auto exprResult = any_cast<LLVMExprResult>(visit(node));
194
1/2
✓ Branch 5 → 6 taken 13502 times.
✗ Branch 5 → 12 not taken.
27004 return resolveAddress(exprResult);
195 }
196
197 100277 llvm::Value *IRGenerator::resolveAddress(LLVMExprResult &exprResult) {
198 // Check if an address is already present
199
2/2
✓ Branch 2 → 3 taken 82184 times.
✓ Branch 2 → 4 taken 18093 times.
100277 if (exprResult.ptr != nullptr)
200 82184 return exprResult.ptr;
201
202 // Check if the reference address is already present
203
3/4
✓ Branch 4 → 5 taken 13743 times.
✓ Branch 4 → 7 taken 4350 times.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 13743 times.
18093 const bool isVolatile = exprResult.entry && exprResult.entry->isVolatile;
204
3/4
✓ Branch 8 → 9 taken 14667 times.
✓ Branch 8 → 18 taken 3426 times.
✓ Branch 9 → 10 taken 14667 times.
✗ Branch 9 → 18 not taken.
18093 if (exprResult.refPtr != nullptr && exprResult.ptr == nullptr) {
205
2/4
✓ Branch 13 → 14 taken 14667 times.
✗ Branch 13 → 35 not taken.
✓ Branch 14 → 15 taken 14667 times.
✗ Branch 14 → 35 not taken.
14667 exprResult.ptr = insertLoad(builder.getPtrTy(), exprResult.refPtr, isVolatile);
206 14667 return exprResult.ptr;
207 }
208
209 // If not, store the value or constant
210 3426 materializeConstant(exprResult);
211
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 3426 times.
3426 assert(exprResult.value != nullptr);
212
7/12
✓ Branch 21 → 22 taken 6 times.
✓ Branch 21 → 23 taken 3420 times.
✓ Branch 22 → 26 taken 6 times.
✗ Branch 22 → 43 not taken.
✓ Branch 25 → 26 taken 3420 times.
✗ Branch 25 → 43 not taken.
✓ Branch 27 → 28 taken 3426 times.
✗ Branch 27 → 41 not taken.
✓ Branch 29 → 30 taken 3420 times.
✓ Branch 29 → 32 taken 6 times.
✗ Branch 43 → 44 not taken.
✗ Branch 43 → 46 not taken.
6846 exprResult.ptr = insertAlloca(exprResult.value->getType(), exprResult.entry ? exprResult.entry->name : "");
213 3426 insertStore(exprResult.value, exprResult.ptr, isVolatile);
214
215 3426 return exprResult.ptr;
216 }
217
218 4601 llvm::Constant *IRGenerator::getDefaultValueForSymbolType(const QualType &symbolType) { // NOLINT(misc-no-recursion)
219 // Double
220
2/2
✓ Branch 3 → 4 taken 18 times.
✓ Branch 3 → 9 taken 4583 times.
4601 if (symbolType.is(TY_DOUBLE))
221
2/4
✓ Branch 4 → 5 taken 18 times.
✗ Branch 4 → 129 not taken.
✓ Branch 5 → 6 taken 18 times.
✗ Branch 5 → 127 not taken.
18 return llvm::ConstantFP::get(context, llvm::APFloat(0.0));
222
223 // Int
224
2/2
✓ Branch 10 → 11 taken 749 times.
✓ Branch 10 → 13 taken 3834 times.
4583 if (symbolType.is(TY_INT))
225 749 return builder.getInt32(0);
226
227 // Short
228
2/2
✓ Branch 14 → 15 taken 9 times.
✓ Branch 14 → 17 taken 3825 times.
3834 if (symbolType.is(TY_SHORT))
229 9 return builder.getInt16(0);
230
231 // Long
232
2/2
✓ Branch 18 → 19 taken 845 times.
✓ Branch 18 → 21 taken 2980 times.
3825 if (symbolType.is(TY_LONG))
233 845 return builder.getInt64(0);
234
235 // Byte or char
236
3/4
✓ Branch 21 → 22 taken 2980 times.
✗ Branch 21 → 130 not taken.
✓ Branch 22 → 23 taken 90 times.
✓ Branch 22 → 25 taken 2890 times.
2980 if (symbolType.isOneOf({TY_BYTE, TY_CHAR}))
237 90 return builder.getInt8(0);
238
239 // String
240
2/2
✓ Branch 26 → 27 taken 756 times.
✓ Branch 26 → 35 taken 2134 times.
2890 if (symbolType.is(TY_STRING)) {
241
3/6
✓ Branch 27 → 28 taken 756 times.
✗ Branch 27 → 132 not taken.
✓ Branch 28 → 29 taken 756 times.
✗ Branch 28 → 131 not taken.
✓ Branch 29 → 30 taken 756 times.
✗ Branch 29 → 131 not taken.
756 llvm::GlobalVariable *globalString = builder.CreateGlobalString("", "");
242
1/2
✓ Branch 30 → 31 taken 756 times.
✗ Branch 30 → 34 not taken.
756 if (cliOptions.comparableOutput)
243
2/4
✓ Branch 31 → 32 taken 756 times.
✗ Branch 31 → 133 not taken.
✓ Branch 32 → 33 taken 756 times.
✗ Branch 32 → 133 not taken.
756 globalString->setAlignment(llvm::Align(4));
244 756 return globalString;
245 }
246
247 // Bool
248
2/2
✓ Branch 36 → 37 taken 90 times.
✓ Branch 36 → 39 taken 2044 times.
2134 if (symbolType.is(TY_BOOL))
249 90 return builder.getFalse();
250
251 // Pointer or reference
252
3/4
✓ Branch 39 → 40 taken 2044 times.
✗ Branch 39 → 134 not taken.
✓ Branch 40 → 41 taken 1878 times.
✓ Branch 40 → 44 taken 166 times.
2044 if (symbolType.isOneOf({TY_PTR, TY_REF}))
253 1878 return llvm::Constant::getNullValue(builder.getPtrTy());
254
255 // Array
256
2/2
✓ Branch 45 → 46 taken 54 times.
✓ Branch 45 → 61 taken 112 times.
166 if (symbolType.isArray()) {
257 // Get array size
258
1/2
✓ Branch 46 → 47 taken 54 times.
✗ Branch 46 → 143 not taken.
54 const size_t arraySize = symbolType.getArraySize();
259
260 // Get default value for item
261
2/4
✓ Branch 47 → 48 taken 54 times.
✗ Branch 47 → 135 not taken.
✓ Branch 48 → 49 taken 54 times.
✗ Branch 48 → 135 not taken.
54 llvm::Constant *defaultItemValue = getDefaultValueForSymbolType(symbolType.getContained());
262
263 // Retrieve array and item type
264
2/4
✓ Branch 49 → 50 taken 54 times.
✗ Branch 49 → 136 not taken.
✓ Branch 50 → 51 taken 54 times.
✗ Branch 50 → 136 not taken.
54 llvm::Type *itemType = symbolType.getContained().toLLVMType(sourceFile);
265
1/2
✓ Branch 51 → 52 taken 54 times.
✗ Branch 51 → 143 not taken.
54 llvm::ArrayType *arrayType = llvm::ArrayType::get(itemType, arraySize);
266
267 // Create a constant array with n times the default value
268
1/2
✓ Branch 54 → 55 taken 54 times.
✗ Branch 54 → 137 not taken.
108 const std::vector itemConstants(arraySize, defaultItemValue);
269
1/2
✓ Branch 57 → 58 taken 54 times.
✗ Branch 57 → 140 not taken.
54 return llvm::ConstantArray::get(arrayType, itemConstants);
270 54 }
271
272 // Function or procedure
273
3/4
✓ Branch 61 → 62 taken 112 times.
✗ Branch 61 → 144 not taken.
✓ Branch 62 → 63 taken 58 times.
✓ Branch 62 → 70 taken 54 times.
112 if (symbolType.isOneOf({TY_FUNCTION, TY_PROCEDURE})) {
274
2/4
✓ Branch 63 → 64 taken 58 times.
✗ Branch 63 → 145 not taken.
✓ Branch 64 → 65 taken 58 times.
✗ Branch 64 → 145 not taken.
58 llvm::Constant *ptrDefaultValue = getDefaultValueForSymbolType(QualType(TY_PTR));
275 58 llvm::Constant *sizeDefaultValue = builder.getInt64(0);
276
1/2
✓ Branch 67 → 68 taken 58 times.
✗ Branch 67 → 146 not taken.
58 return llvm::ConstantStruct::get(llvmTypes.lambdaFatPtrType, {ptrDefaultValue, ptrDefaultValue, sizeDefaultValue});
277 }
278
279 // Struct
280
2/2
✓ Branch 71 → 72 taken 53 times.
✓ Branch 71 → 110 taken 1 time.
54 if (symbolType.is(TY_STRUCT)) {
281 // Retrieve field count
282
1/2
✓ Branch 72 → 73 taken 53 times.
✗ Branch 72 → 153 not taken.
53 Scope *structScope = symbolType.getBodyScope();
283
1/2
✗ Branch 73 → 74 not taken.
✓ Branch 73 → 75 taken 53 times.
53 assert(structScope != nullptr);
284
1/2
✓ Branch 75 → 76 taken 53 times.
✗ Branch 75 → 153 not taken.
53 const size_t fieldCount = structScope->getFieldCount();
285
286 // Get default values for all fields of the struct
287 53 std::vector<llvm::Constant *> fieldConstants;
288
1/2
✓ Branch 76 → 77 taken 53 times.
✗ Branch 76 → 151 not taken.
53 fieldConstants.reserve(fieldCount);
289
290 // Add default value for each struct field
291
2/2
✓ Branch 102 → 78 taken 108 times.
✓ Branch 102 → 103 taken 53 times.
161 for (size_t i = 0; i < fieldCount; i++) {
292 // Get entry of the field
293
1/2
✗ Branch 78 → 79 not taken.
✓ Branch 78 → 80 taken 108 times.
108 const SymbolTableEntry *fieldEntry = structScope->lookupField(i);
294
3/6
✓ Branch 83 → 84 taken 108 times.
✗ Branch 83 → 87 not taken.
✓ Branch 84 → 85 taken 108 times.
✗ Branch 84 → 149 not taken.
✓ Branch 85 → 86 taken 108 times.
✗ Branch 85 → 87 not taken.
108 assert(fieldEntry != nullptr && fieldEntry->isField());
295
296 // Retrieve default field value
297 llvm::Constant *defaultFieldValue;
298
4/6
✓ Branch 88 → 89 taken 108 times.
✗ Branch 88 → 90 not taken.
✓ Branch 91 → 92 taken 108 times.
✗ Branch 91 → 97 not taken.
✓ Branch 92 → 93 taken 8 times.
✓ Branch 92 → 97 taken 100 times.
108 if (const auto fieldNode = dynamic_cast<FieldNode *>(fieldEntry->declNode); fieldNode && fieldNode->defaultValue)
299
3/6
✓ Branch 93 → 94 taken 8 times.
✗ Branch 93 → 149 not taken.
✓ Branch 94 → 95 taken 8 times.
✗ Branch 94 → 148 not taken.
✓ Branch 95 → 96 taken 8 times.
✗ Branch 95 → 148 not taken.
8 defaultFieldValue = getConst(fieldNode->defaultValue->getCompileTimeValue(manIdx), fieldEntry->getQualType(), fieldNode);
300 else
301
2/4
✓ Branch 97 → 98 taken 100 times.
✗ Branch 97 → 149 not taken.
✓ Branch 98 → 99 taken 100 times.
✗ Branch 98 → 149 not taken.
100 defaultFieldValue = getDefaultValueForSymbolType(fieldEntry->getQualType());
302
303
1/2
✓ Branch 100 → 101 taken 108 times.
✗ Branch 100 → 149 not taken.
108 fieldConstants.push_back(defaultFieldValue);
304 }
305
306
2/4
✓ Branch 103 → 104 taken 53 times.
✗ Branch 103 → 151 not taken.
✓ Branch 104 → 105 taken 53 times.
✗ Branch 104 → 151 not taken.
53 const auto structType = llvm::cast<llvm::StructType>(symbolType.toLLVMType(sourceFile));
307
1/2
✓ Branch 106 → 107 taken 53 times.
✗ Branch 106 → 150 not taken.
53 return llvm::ConstantStruct::get(structType, fieldConstants);
308 53 }
309
310 // Interface
311
1/2
✓ Branch 111 → 112 taken 1 time.
✗ Branch 111 → 118 not taken.
1 if (symbolType.is(TY_INTERFACE)) {
312 1 const auto structType = llvm::cast<llvm::StructType>(symbolType.toLLVMType(sourceFile));
313 1 return llvm::ConstantStruct::get(structType, llvm::Constant::getNullValue(builder.getPtrTy()));
314 }
315
316 throw CompilerError(INTERNAL_ERROR, "Cannot determine default value for symbol type"); // GCOV_EXCL_LINE
317 }
318
319 51683 llvm::Constant *IRGenerator::getConst(const CompileTimeValue &compileTimeValue, const QualType &type, const ASTNode *node) const {
320
2/2
✓ Branch 3 → 4 taken 1773 times.
✓ Branch 3 → 9 taken 49910 times.
51683 if (type.is(TY_DOUBLE))
321
2/4
✓ Branch 4 → 5 taken 1773 times.
✗ Branch 4 → 56 not taken.
✓ Branch 5 → 6 taken 1773 times.
✗ Branch 5 → 54 not taken.
1773 return llvm::ConstantFP::get(context, llvm::APFloat(compileTimeValue.doubleValue));
322
323
2/2
✓ Branch 10 → 11 taken 8211 times.
✓ Branch 10 → 13 taken 41699 times.
49910 if (type.is(TY_INT))
324 8211 return builder.getInt32(compileTimeValue.intValue);
325
326
2/2
✓ Branch 14 → 15 taken 1231 times.
✓ Branch 14 → 17 taken 40468 times.
41699 if (type.is(TY_SHORT))
327 1231 return builder.getInt16(compileTimeValue.shortValue);
328
329
2/2
✓ Branch 18 → 19 taken 21630 times.
✓ Branch 18 → 21 taken 18838 times.
40468 if (type.is(TY_LONG))
330 21630 return builder.getInt64(compileTimeValue.longValue);
331
332
3/4
✓ Branch 21 → 22 taken 18838 times.
✗ Branch 21 → 57 not taken.
✓ Branch 22 → 23 taken 4730 times.
✓ Branch 22 → 25 taken 14108 times.
18838 if (type.isOneOf({TY_BYTE, TY_CHAR}))
333 4730 return builder.getInt8(compileTimeValue.charValue);
334
335
2/2
✓ Branch 26 → 27 taken 9116 times.
✓ Branch 26 → 36 taken 4992 times.
14108 if (type.is(TY_STRING)) {
336 9116 const std::string &stringValue = resourceManager.compileTimeStringValues.at(compileTimeValue.stringValueOffset);
337
2/4
✓ Branch 30 → 31 taken 9116 times.
✗ Branch 30 → 60 not taken.
✓ Branch 31 → 32 taken 9116 times.
✗ Branch 31 → 58 not taken.
27348 return createGlobalStringConst(ANON_GLOBAL_STRING_NAME, stringValue, node->codeLoc);
338 }
339
340
2/2
✓ Branch 37 → 38 taken 4990 times.
✓ Branch 37 → 40 taken 2 times.
4992 if (type.is(TY_BOOL))
341 4990 return builder.getInt1(compileTimeValue.boolValue);
342
343
1/2
✓ Branch 41 → 42 taken 2 times.
✗ Branch 41 → 45 not taken.
2 if (type.is(TY_PTR))
344 2 return llvm::Constant::getNullValue(builder.getPtrTy());
345
346 throw CompilerError(UNHANDLED_BRANCH, "Constant fall-through"); // GCOV_EXCL_LINE
347 }
348
349 104766 llvm::BasicBlock *IRGenerator::createBlock(const std::string &blockName /*=""*/) const {
350
2/4
✓ Branch 2 → 3 taken 104766 times.
✗ Branch 2 → 7 not taken.
✓ Branch 3 → 4 taken 104766 times.
✗ Branch 3 → 7 not taken.
104766 return llvm::BasicBlock::Create(context, blockName);
351 }
352
353 104766 void IRGenerator::switchToBlock(llvm::BasicBlock *block, llvm::Function *parentFct /*=nullptr*/) {
354
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 104766 times.
104766 assert(block->getParent() == nullptr); // Ensure that the block was not added to a function already
355 // If no parent function were passed, use the current function
356
2/2
✓ Branch 5 → 6 taken 69954 times.
✓ Branch 5 → 8 taken 34812 times.
104766 if (!parentFct)
357 69954 parentFct = builder.GetInsertBlock()->getParent();
358 // Append block to current function
359 104766 parentFct->insert(parentFct->end(), block);
360 // Set insert point to the block
361 104766 builder.SetInsertPoint(block);
362 104766 blockAlreadyTerminated = false;
363 104766 }
364
365 31279 void IRGenerator::terminateBlock(const StmtLstNode *stmtLstNode) {
366 31279 generateScopeCleanup(stmtLstNode);
367 31279 blockAlreadyTerminated = true;
368 31279 }
369
370 36400 void IRGenerator::insertJump(llvm::BasicBlock *targetBlock) {
371
2/2
✓ Branch 2 → 3 taken 10997 times.
✓ Branch 2 → 4 taken 25403 times.
36400 if (blockAlreadyTerminated)
372 10997 return;
373 25403 builder.CreateBr(targetBlock);
374 25403 blockAlreadyTerminated = true;
375 }
376
377 29700 void IRGenerator::insertCondJump(llvm::Value *condition, llvm::BasicBlock *trueBlock, llvm::BasicBlock *falseBlock,
378 Likelihood likelihood /*=UNSPECIFIED*/) {
379
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 29700 times.
29700 if (blockAlreadyTerminated)
380 return;
381 29700 llvm::CondBrInst *jumpInst = builder.CreateCondBr(condition, trueBlock, falseBlock);
382 29700 blockAlreadyTerminated = true;
383
384
2/2
✓ Branch 5 → 6 taken 4732 times.
✓ Branch 5 → 7 taken 24968 times.
29700 if (likelihood != Likelihood::UNSPECIFIED)
385 4732 mdGenerator.generateBranchWeightsMetadata(jumpInst, likelihood);
386 }
387
388 34799 void IRGenerator::verifyFunction(const llvm::Function *fct, const CodeLoc &codeLoc) const {
389 // Skip the verifying step if the verifier was disabled manually or debug info is emitted
390
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 34799 times.
34799 if (cliOptions.disableVerifier)
391 return;
392
393 // Verify function
394 34799 std::string output;
395
1/2
✓ Branch 5 → 6 taken 34799 times.
✗ Branch 5 → 20 not taken.
34799 llvm::raw_string_ostream oss(output);
396 if (llvm::verifyFunction(*fct, &oss)) // LCOV_EXCL_LINE
397 throw CompilerError(codeLoc, INVALID_FUNCTION, output); // LCOV_EXCL_LINE
398 34799 }
399
400 2220 void IRGenerator::verifyModule(const CodeLoc &codeLoc) const {
401 // Skip the verifying step if the verifier was disabled manually or debug info is emitted
402
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2220 times.
2220 if (cliOptions.disableVerifier)
403 return;
404
405 // Verify module
406 2220 std::string output;
407
1/2
✓ Branch 5 → 6 taken 2220 times.
✗ Branch 5 → 20 not taken.
2220 llvm::raw_string_ostream oss(output);
408 if (llvm::verifyModule(*module, &oss)) // LCOV_EXCL_LINE
409 throw CompilerError(codeLoc, INVALID_MODULE, output); // LCOV_EXCL_LINE
410 2220 }
411
412 17783 LLVMExprResult IRGenerator::doAssignment(const ASTNode *lhsNode, const ExprNode *rhsNode, const ASTNode *node) {
413 // Get entry of left side
414
2/4
✓ Branch 2 → 3 taken 17783 times.
✗ Branch 2 → 18 not taken.
✓ Branch 3 → 4 taken 17783 times.
✗ Branch 3 → 16 not taken.
17783 auto exprResult = std::any_cast<LLVMExprResult>(visit(lhsNode));
415 17783 const SymbolTableEntry *entry = exprResult.entry;
416
7/10
✓ Branch 5 → 6 taken 16325 times.
✓ Branch 5 → 10 taken 1458 times.
✓ Branch 6 → 7 taken 16325 times.
✗ Branch 6 → 19 not taken.
✓ Branch 7 → 8 taken 16325 times.
✗ Branch 7 → 19 not taken.
✓ Branch 8 → 9 taken 1321 times.
✓ Branch 8 → 10 taken 15004 times.
✓ Branch 10 → 11 taken 16462 times.
✗ Branch 10 → 19 not taken.
17783 llvm::Value *lhsAddress = entry != nullptr && entry->getQualType().isRef() ? exprResult.refPtr : resolveAddress(exprResult);
417
1/2
✓ Branch 12 → 13 taken 17783 times.
✗ Branch 12 → 19 not taken.
35566 return doAssignment(lhsAddress, entry, rhsNode, node);
418 }
419
420 41526 LLVMExprResult IRGenerator::doAssignment(llvm::Value *lhsAddress, const SymbolTableEntry *lhsEntry, const ExprNode *rhsNode,
421 const ASTNode *node, bool isDecl) {
422 // Get symbol type of right side
423
1/2
✓ Branch 2 → 3 taken 41526 times.
✗ Branch 2 → 13 not taken.
41526 const QualType &rhsSType = rhsNode->getEvaluatedSymbolType(manIdx);
424
2/4
✓ Branch 3 → 4 taken 41526 times.
✗ Branch 3 → 12 not taken.
✓ Branch 4 → 5 taken 41526 times.
✗ Branch 4 → 10 not taken.
41526 auto rhs = std::any_cast<LLVMExprResult>(visit(rhsNode));
425
1/2
✓ Branch 6 → 7 taken 41526 times.
✗ Branch 6 → 13 not taken.
83052 return doAssignment(lhsAddress, lhsEntry, rhs, rhsSType, node, isDecl);
426 }
427
428 42233 LLVMExprResult IRGenerator::doAssignment(llvm::Value *lhsAddress, const SymbolTableEntry *lhsEntry, LLVMExprResult &rhs,
429 const QualType &rhsSType, const ASTNode *node, bool isDecl) {
430 // Deduce some information about the assignment
431
4/4
✓ Branch 2 → 3 taken 40775 times.
✓ Branch 2 → 7 taken 1458 times.
✓ Branch 5 → 6 taken 3213 times.
✓ Branch 5 → 7 taken 37562 times.
42233 const bool isRefAssign = lhsEntry != nullptr && lhsEntry->getQualType().isRef();
432 // A non-temporary struct value assigned by value needs a deep copy. This holds whether the destination is a direct
433 // lvalue or the value behind an already-bound reference (assign-through): the binding cases of a reference assignment
434 // (declaration/initial field ref/return value) all return early above before this is consumed, so the remaining
435 // reference assignments are assign-throughs that must copy into the referent instead of shallow-copying (which would
436 // alias the rhs' owned members and double-free).
437
6/8
✓ Branch 8 → 9 taken 42233 times.
✗ Branch 8 → 249 not taken.
✓ Branch 9 → 10 taken 42233 times.
✗ Branch 9 → 249 not taken.
✓ Branch 10 → 11 taken 8380 times.
✓ Branch 10 → 14 taken 33853 times.
✓ Branch 12 → 13 taken 1446 times.
✓ Branch 12 → 14 taken 6934 times.
42233 const bool needsCopy = rhsSType.removeReferenceWrapper().is(TY_STRUCT) && !rhs.isTemporary();
438
439
2/2
✓ Branch 15 → 16 taken 3213 times.
✓ Branch 15 → 57 taken 39020 times.
42233 if (isRefAssign) {
440
1/2
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 3213 times.
3213 assert(lhsEntry != nullptr);
441
2/2
✓ Branch 18 → 19 taken 1892 times.
✓ Branch 18 → 33 taken 1321 times.
3213 if (isDecl) { // Reference gets initially assigned
442 // Store lhs pointer to rhs
443
2/4
✓ Branch 22 → 23 taken 1892 times.
✗ Branch 22 → 250 not taken.
✓ Branch 23 → 24 taken 1892 times.
✗ Branch 23 → 250 not taken.
1892 llvm::Value *refAddress = insertAlloca(builder.getPtrTy());
444 1892 updateAddress(lhsEntry, refAddress);
445
446 // Generate debug info for variable declaration
447 1892 diGenerator.generateLocalVarDebugInfo(lhsEntry->name, refAddress);
448
449 // Get address of right side
450 1892 llvm::Value *rhsAddress = resolveAddress(rhs);
451
1/2
✗ Branch 29 → 30 not taken.
✓ Branch 29 → 31 taken 1892 times.
1892 assert(rhsAddress != nullptr);
452 1892 insertStore(rhsAddress, refAddress);
453
454 1892 return LLVMExprResult{.value = rhsAddress, .ptr = refAddress, .entry = lhsEntry};
455 }
456
457 // Reference to reference assignment (only for struct fields that are not initialized yet)
458 // These are only allowed inside a ctor body. In other cases, the value of the reference gets assigned, not the ref itself.
459
7/8
✓ Branch 33 → 34 taken 1048 times.
✓ Branch 33 → 40 taken 273 times.
✓ Branch 35 → 36 taken 1044 times.
✓ Branch 35 → 40 taken 4 times.
✓ Branch 36 → 37 taken 1007 times.
✓ Branch 36 → 40 taken 37 times.
✓ Branch 38 → 39 taken 1007 times.
✗ Branch 38 → 40 not taken.
1321 const bool isInitialFieldRefAssign = isInCtorBody && rhsSType.isRef() && rhs.entry && lhsEntry->isField();
460 // Assigning the result variable
461 1321 const bool isReturnValAssign = lhsEntry->name == RETURN_VARIABLE_NAME;
462
4/4
✓ Branch 42 → 43 taken 314 times.
✓ Branch 42 → 44 taken 1007 times.
✓ Branch 43 → 44 taken 11 times.
✓ Branch 43 → 49 taken 303 times.
1321 if (isInitialFieldRefAssign || isReturnValAssign) {
463 // Get address of right side
464 1018 llvm::Value *referencedAddress = resolveAddress(rhs);
465
1/2
✗ Branch 45 → 46 not taken.
✓ Branch 45 → 47 taken 1018 times.
1018 assert(referencedAddress != nullptr);
466
467 // Store the rhs* to the lhs**
468 1018 insertStore(referencedAddress, lhsAddress);
469
470 1018 return LLVMExprResult{.value = referencedAddress, .ptr = lhsAddress, .entry = lhsEntry};
471 }
472
473 // Load referenced address
474
2/4
✓ Branch 52 → 53 taken 303 times.
✗ Branch 52 → 256 not taken.
✓ Branch 53 → 54 taken 303 times.
✗ Branch 53 → 256 not taken.
303 lhsAddress = insertLoad(builder.getPtrTy(), lhsAddress);
475 }
476
477
8/8
✓ Branch 57 → 58 taken 22558 times.
✓ Branch 57 → 63 taken 16765 times.
✓ Branch 59 → 60 taken 3403 times.
✓ Branch 59 → 63 taken 19155 times.
✓ Branch 61 → 62 taken 3392 times.
✓ Branch 61 → 63 taken 11 times.
✓ Branch 64 → 65 taken 3392 times.
✓ Branch 64 → 71 taken 35931 times.
39323 if (isDecl && rhsSType.is(TY_STRUCT) && rhs.isTemporary()) {
478
1/2
✗ Branch 65 → 66 not taken.
✓ Branch 65 → 67 taken 3392 times.
3392 assert(lhsEntry != nullptr);
479 // Directly set the address to the lhs entry (temp stealing)
480 3392 llvm::Value *rhsAddress = resolveAddress(rhs);
481 3392 updateAddress(lhsEntry, rhsAddress);
482 3392 rhs.entry = lhsEntry;
483 // Generate debug info for variable declaration
484 3392 diGenerator.generateLocalVarDebugInfo(lhsEntry->name, rhsAddress);
485 3392 return rhs;
486 }
487
488 // Allocate new memory if the lhs address does not exist
489
2/2
✓ Branch 71 → 72 taken 18944 times.
✓ Branch 71 → 83 taken 16987 times.
35931 if (!lhsAddress) {
490
1/2
✗ Branch 72 → 73 not taken.
✓ Branch 72 → 74 taken 18944 times.
18944 assert(lhsEntry != nullptr);
491
2/4
✓ Branch 77 → 78 taken 18944 times.
✗ Branch 77 → 262 not taken.
✓ Branch 78 → 79 taken 18944 times.
✗ Branch 78 → 262 not taken.
18944 lhsAddress = insertAlloca(lhsEntry->getQualType());
492 18944 updateAddress(lhsEntry, lhsAddress);
493 // Generate debug info for variable declaration
494 18944 diGenerator.generateLocalVarDebugInfo(lhsEntry->name, lhsAddress);
495 }
496
497 // Check if we try to assign an array by value to a pointer. Here we have to store the address of the first element to the lhs
498
10/10
✓ Branch 83 → 84 taken 34473 times.
✓ Branch 83 → 92 taken 1458 times.
✓ Branch 86 → 87 taken 8084 times.
✓ Branch 86 → 92 taken 26389 times.
✓ Branch 88 → 89 taken 7 times.
✓ Branch 88 → 92 taken 8077 times.
✓ Branch 90 → 91 taken 2 times.
✓ Branch 90 → 92 taken 5 times.
✓ Branch 93 → 94 taken 2 times.
✓ Branch 93 → 109 taken 35929 times.
35931 if (lhsEntry && lhsEntry->getQualType().isPtr() && rhsSType.isArray() && rhsSType.getArraySize() != ARRAY_SIZE_UNKNOWN) {
499 // Get address of right side
500
1/2
✓ Branch 94 → 95 taken 2 times.
✗ Branch 94 → 275 not taken.
2 llvm::Value *rhsAddress = resolveAddress(rhs);
501
1/2
✗ Branch 95 → 96 not taken.
✓ Branch 95 → 97 taken 2 times.
2 assert(rhsAddress != nullptr);
502
1/2
✓ Branch 97 → 98 taken 2 times.
✗ Branch 97 → 275 not taken.
2 llvm::Type *elementTy = rhsSType.toLLVMType(sourceFile);
503
2/4
✓ Branch 98 → 99 taken 2 times.
✗ Branch 98 → 275 not taken.
✓ Branch 99 → 100 taken 2 times.
✗ Branch 99 → 275 not taken.
2 llvm::Value *indices[2] = {builder.getInt64(0), builder.getInt32(0)};
504
1/2
✓ Branch 104 → 105 taken 2 times.
✗ Branch 104 → 268 not taken.
2 llvm::Value *firstItemAddress = insertInBoundsGEP(elementTy, rhsAddress, indices);
505
1/2
✓ Branch 107 → 108 taken 2 times.
✗ Branch 107 → 275 not taken.
2 insertStore(firstItemAddress, lhsAddress);
506 2 return LLVMExprResult{.value = rhsAddress, .ptr = lhsAddress, .entry = lhsEntry};
507 }
508
509 // Handle operator overloads
510
6/6
✓ Branch 109 → 110 taken 16765 times.
✓ Branch 109 → 113 taken 19164 times.
✓ Branch 111 → 112 taken 396 times.
✓ Branch 111 → 113 taken 16369 times.
✓ Branch 114 → 115 taken 396 times.
✓ Branch 114 → 131 taken 35533 times.
35929 if (!isDecl && conversionManager.callsOverloadedOpFct(node, DEFAULT_OP_IDX)) {
511 396 ResolverFct lhsV = [&] { return static_cast<llvm::Value *>(nullptr); };
512
1/2
✓ Branch 116 → 117 taken 396 times.
✗ Branch 116 → 276 not taken.
396 ResolverFct rhsV = [&] { return resolveValue(rhsSType, rhs); };
513 792 ResolverFct lhsP = [&] { return lhsAddress; };
514 792 ResolverFct rhsP = [&] { return resolveAddress(rhs); };
515 396 return conversionManager.callOperatorOverloadFct<2>(node, {lhsV, lhsP, rhsV, rhsP}, DEFAULT_OP_IDX);
516 396 }
517
518 // Check if we need to copy the rhs to the lhs. This happens for structs
519
2/2
✓ Branch 131 → 132 taken 463 times.
✓ Branch 131 → 217 taken 35070 times.
35533 if (needsCopy) {
520 // Get address of right side
521
1/2
✓ Branch 132 → 133 taken 463 times.
✗ Branch 132 → 352 not taken.
463 llvm::Value *rhsAddress = resolveAddress(rhs);
522
1/2
✗ Branch 133 → 134 not taken.
✓ Branch 133 → 135 taken 463 times.
463 assert(rhsAddress != nullptr);
523
524 // If the lhs already holds an initialized, non-trivially-destructible struct, its old value must be
525 // destructed before the copy overwrites it, otherwise its owning members (heap pointers, strings, ...)
526 // would leak. The typechecker only sets a dtor in exactly those cases. To stay correct for a self-
527 // assignment like 'a = a', the destruct + copy are skipped entirely when both sides share the address
528 // (the assignment is a no-op in that case, and destructing first would corrupt the value to copy from).
529
1/2
✓ Branch 135 → 136 taken 463 times.
✗ Branch 135 → 137 not taken.
463 const auto *assignNode = dynamic_cast<const AssignExprNode *>(node);
530
3/4
✓ Branch 138 → 139 taken 452 times.
✓ Branch 138 → 141 taken 11 times.
✓ Branch 139 → 140 taken 452 times.
✗ Branch 139 → 352 not taken.
463 const Function *lhsDtor = assignNode ? assignNode->lhsDtorFct.at(manIdx) : nullptr;
531 463 llvm::BasicBlock *bCopyEnd = nullptr;
532
2/2
✓ Branch 142 → 143 taken 11 times.
✓ Branch 142 → 163 taken 452 times.
463 if (lhsDtor != nullptr) {
533
2/4
✓ Branch 145 → 146 taken 11 times.
✗ Branch 145 → 296 not taken.
✓ Branch 146 → 147 taken 11 times.
✗ Branch 146 → 294 not taken.
22 llvm::BasicBlock *bCopy = createBlock("assign.copy");
534
2/4
✓ Branch 151 → 152 taken 11 times.
✗ Branch 151 → 302 not taken.
✓ Branch 152 → 153 taken 11 times.
✗ Branch 152 → 300 not taken.
11 bCopyEnd = createBlock("assign.copy.end");
535
3/6
✓ Branch 155 → 156 taken 11 times.
✗ Branch 155 → 306 not taken.
✓ Branch 156 → 157 taken 11 times.
✗ Branch 156 → 306 not taken.
✓ Branch 157 → 158 taken 11 times.
✗ Branch 157 → 306 not taken.
11 insertCondJump(builder.CreateICmpEQ(lhsAddress, rhsAddress), bCopyEnd, bCopy);
536
1/2
✓ Branch 158 → 159 taken 11 times.
✗ Branch 158 → 352 not taken.
11 switchToBlock(bCopy);
537
1/2
✓ Branch 160 → 161 taken 11 times.
✗ Branch 160 → 307 not taken.
11 generateCtorOrDtorCall(lhsAddress, lhsDtor, {});
538 }
539
540
2/4
✓ Branch 163 → 164 taken 463 times.
✗ Branch 163 → 310 not taken.
✓ Branch 164 → 165 taken 463 times.
✗ Branch 164 → 310 not taken.
463 const QualType rhsSTypeNonRef = rhsSType.removeReferenceWrapper().toNonConst();
541
3/4
✓ Branch 165 → 166 taken 463 times.
✗ Branch 165 → 352 not taken.
✓ Branch 166 → 167 taken 321 times.
✓ Branch 166 → 183 taken 142 times.
463 if (rhsSTypeNonRef.isTriviallyCopyable(node)) {
542 // Create shallow copy
543
1/2
✓ Branch 167 → 168 taken 321 times.
✗ Branch 167 → 318 not taken.
321 llvm::Type *rhsType = rhsSTypeNonRef.toLLVMType(sourceFile);
544
3/10
✓ Branch 168 → 169 taken 321 times.
✗ Branch 168 → 170 not taken.
✓ Branch 169 → 173 taken 321 times.
✗ Branch 169 → 311 not taken.
✗ Branch 172 → 173 not taken.
✗ Branch 172 → 311 not taken.
✗ Branch 173 → 174 not taken.
✓ Branch 173 → 176 taken 321 times.
✗ Branch 311 → 312 not taken.
✗ Branch 311 → 314 not taken.
321 const std::string copyName = lhsEntry ? lhsEntry->name : "";
545
3/6
✓ Branch 176 → 177 taken 321 times.
✗ Branch 176 → 179 not taken.
✗ Branch 177 → 178 not taken.
✓ Branch 177 → 179 taken 321 times.
✓ Branch 180 → 181 taken 321 times.
✗ Branch 180 → 316 not taken.
321 generateShallowCopy(rhsAddress, rhsType, lhsAddress, lhsEntry && lhsEntry->isVolatile);
546 321 } else {
547 // Check if we have a copy ctor
548
1/2
✓ Branch 183 → 184 taken 142 times.
✗ Branch 183 → 351 not taken.
142 Scope *structScope = rhsSTypeNonRef.getBodyScope();
549
2/4
✓ Branch 184 → 185 taken 142 times.
✗ Branch 184 → 323 not taken.
✓ Branch 189 → 190 taken 142 times.
✗ Branch 189 → 319 not taken.
426 const ArgList args = {{rhsSTypeNonRef.toConstRef(node), rhs.isTemporary()}};
550
2/4
✓ Branch 193 → 194 taken 142 times.
✗ Branch 193 → 327 not taken.
✓ Branch 194 → 195 taken 142 times.
✗ Branch 194 → 325 not taken.
142 const Function *copyCtor = FunctionManager::lookup(structScope, CTOR_FUNCTION_NAME, rhsSTypeNonRef, args, true);
551
1/2
✓ Branch 197 → 198 taken 142 times.
✗ Branch 197 → 206 not taken.
142 if (copyCtor != nullptr) {
552 // Call copy ctor
553
2/4
✓ Branch 200 → 201 taken 142 times.
✗ Branch 200 → 333 not taken.
✓ Branch 201 → 202 taken 142 times.
✗ Branch 201 → 331 not taken.
284 generateCtorOrDtorCall(lhsAddress, copyCtor, {rhsAddress});
554 } else {
555 const std::string structName = rhsSTypeNonRef.getName();
556 const std::string msg = "Cannot copy struct '" + structName + "', as it is not trivially copyable and has no copy ctor";
557 throw SemanticError(node, COPY_CTOR_REQUIRED, msg);
558 }
559 142 }
560
561 // Close the self-assignment guard
562
2/2
✓ Branch 213 → 214 taken 11 times.
✓ Branch 213 → 216 taken 452 times.
463 if (bCopyEnd != nullptr) {
563
1/2
✓ Branch 214 → 215 taken 11 times.
✗ Branch 214 → 352 not taken.
11 insertJump(bCopyEnd);
564
1/2
✓ Branch 215 → 216 taken 11 times.
✗ Branch 215 → 352 not taken.
11 switchToBlock(bCopyEnd);
565 }
566 463 return LLVMExprResult{.ptr = lhsAddress, .entry = lhsEntry};
567 }
568
569 // Optimization: If we have the address of both sides, we can do a memcpy instead of loading and storing the value
570 35070 llvm::Value *rhsValue = nullptr;
571
8/8
✓ Branch 218 → 219 taken 1915 times.
✓ Branch 218 → 222 taken 33155 times.
✓ Branch 219 → 220 taken 1854 times.
✓ Branch 219 → 222 taken 61 times.
✓ Branch 220 → 221 taken 1847 times.
✓ Branch 220 → 222 taken 7 times.
✓ Branch 223 → 224 taken 1847 times.
✓ Branch 223 → 245 taken 33223 times.
35070 if (rhsSType.is(TY_STRUCT) && rhs.value == nullptr && rhs.constant == nullptr) {
572 // Create shallow copy
573
2/4
✓ Branch 224 → 225 taken 1847 times.
✗ Branch 224 → 353 not taken.
✓ Branch 225 → 226 taken 1847 times.
✗ Branch 225 → 353 not taken.
1847 const QualType rhsSTypeNonRef = rhsSType.removeReferenceWrapper().toNonConst();
574
1/2
✓ Branch 226 → 227 taken 1847 times.
✗ Branch 226 → 361 not taken.
1847 llvm::Type *rhsType = rhsSTypeNonRef.toLLVMType(sourceFile);
575
1/2
✓ Branch 227 → 228 taken 1847 times.
✗ Branch 227 → 361 not taken.
1847 llvm::Value *rhsAddress = resolveAddress(rhs);
576
1/2
✗ Branch 228 → 229 not taken.
✓ Branch 228 → 230 taken 1847 times.
1847 assert(rhsAddress != nullptr);
577
6/10
✓ Branch 230 → 231 taken 1839 times.
✓ Branch 230 → 232 taken 8 times.
✓ Branch 231 → 235 taken 1839 times.
✗ Branch 231 → 354 not taken.
✓ Branch 234 → 235 taken 8 times.
✗ Branch 234 → 354 not taken.
✓ Branch 235 → 236 taken 8 times.
✓ Branch 235 → 238 taken 1839 times.
✗ Branch 354 → 355 not taken.
✗ Branch 354 → 357 not taken.
1855 const std::string copyName = lhsEntry ? lhsEntry->name : "";
578
4/6
✓ Branch 238 → 239 taken 1839 times.
✓ Branch 238 → 241 taken 8 times.
✗ Branch 239 → 240 not taken.
✓ Branch 239 → 241 taken 1839 times.
✓ Branch 242 → 243 taken 1847 times.
✗ Branch 242 → 359 not taken.
1847 generateShallowCopy(rhsAddress, rhsType, lhsAddress, lhsEntry && lhsEntry->isVolatile);
579 1847 } else {
580 // We can load the value from the right side and store it to the left side
581 // Retrieve value of the right side
582 33223 rhsValue = resolveValue(rhsSType, rhs);
583 // Store the value to the address
584 33223 insertStore(rhsValue, lhsAddress, rhsSType);
585 }
586
587 35070 return LLVMExprResult{.value = rhsValue, .ptr = lhsAddress, .entry = lhsEntry};
588
5/14
✓ Branch 119 → 120 taken 396 times.
✗ Branch 119 → 279 not taken.
✓ Branch 120 → 121 taken 396 times.
✗ Branch 120 → 279 not taken.
✓ Branch 121 → 122 taken 396 times.
✗ Branch 121 → 279 not taken.
✓ Branch 122 → 123 taken 396 times.
✗ Branch 122 → 279 not taken.
✓ Branch 123 → 124 taken 396 times.
✗ Branch 123 → 277 not taken.
✗ Branch 279 → 280 not taken.
✗ Branch 279 → 283 not taken.
✗ Branch 281 → 282 not taken.
✗ Branch 281 → 283 not taken.
396 }
589
590 2559 void IRGenerator::generateShallowCopy(llvm::Value *oldAddress, llvm::Type *varType, llvm::Value *targetAddress,
591 bool isVolatile) const {
592 // Retrieve size to copy
593
1/2
✓ Branch 3 → 4 taken 2559 times.
✗ Branch 3 → 19 not taken.
2559 const llvm::TypeSize typeSize = module->getDataLayout().getTypeAllocSize(varType);
594
595 // Create values for memcpy intrinsic
596
2/4
✓ Branch 4 → 5 taken 2559 times.
✗ Branch 4 → 19 not taken.
✓ Branch 5 → 6 taken 2559 times.
✗ Branch 5 → 19 not taken.
2559 llvm::Value *structSize = builder.getInt64(typeSize);
597
1/2
✓ Branch 6 → 7 taken 2559 times.
✗ Branch 6 → 19 not taken.
2559 llvm::Value *copyVolatile = builder.getInt1(isVolatile);
598
599 // Call memcpy intrinsic to execute the shallow copy
600
1/2
✓ Branch 7 → 8 taken 2559 times.
✗ Branch 7 → 19 not taken.
2559 llvm::Function *memcpyFct = stdFunctionManager.getMemcpyIntrinsic();
601
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 2559 times.
2559 assert(targetAddress != nullptr);
602
3/6
✓ Branch 10 → 11 taken 2559 times.
✗ Branch 10 → 18 not taken.
✓ Branch 12 → 13 taken 2559 times.
✗ Branch 12 → 15 not taken.
✓ Branch 13 → 14 taken 2559 times.
✗ Branch 13 → 15 not taken.
2559 builder.CreateCall(memcpyFct, {targetAddress, oldAddress, structSize, copyVolatile});
603 2559 }
604
605 88292 void IRGenerator::autoDeReferencePtr(llvm::Value *&ptr, QualType &symbolType) {
606
6/6
✓ Branch 12 → 13 taken 93140 times.
✓ Branch 12 → 15 taken 58962 times.
✓ Branch 14 → 15 taken 4848 times.
✓ Branch 14 → 16 taken 88292 times.
✓ Branch 17 → 3 taken 63810 times.
✓ Branch 17 → 18 taken 88292 times.
152102 while (symbolType.isPtr() || symbolType.isRef()) {
607
1/2
✓ Branch 6 → 7 taken 63810 times.
✗ Branch 6 → 19 not taken.
63810 ptr = insertLoad(symbolType, ptr);
608
1/2
✓ Branch 9 → 10 taken 63810 times.
✗ Branch 9 → 25 not taken.
63810 symbolType = symbolType.getContained();
609 }
610 88292 }
611
612 302 llvm::GlobalVariable *IRGenerator::createGlobalConst(const std::string &baseName, llvm::Constant *constant) const {
613 // Get unused name
614
1/2
✓ Branch 2 → 3 taken 302 times.
✗ Branch 2 → 19 not taken.
302 const std::string globalName = getUnusedGlobalName(baseName);
615 // Create global
616
1/2
✓ Branch 5 → 6 taken 302 times.
✗ Branch 5 → 15 not taken.
302 module->getOrInsertGlobal(globalName, constant->getType());
617
1/2
✓ Branch 7 → 8 taken 302 times.
✗ Branch 7 → 16 not taken.
302 llvm::GlobalVariable *global = module->getNamedGlobal(globalName);
618 // Set initializer to the given constant
619
1/2
✓ Branch 8 → 9 taken 302 times.
✗ Branch 8 → 17 not taken.
302 global->setInitializer(constant);
620 302 global->setConstant(true);
621
1/2
✓ Branch 10 → 11 taken 302 times.
✗ Branch 10 → 17 not taken.
302 global->setLinkage(llvm::GlobalValue::PrivateLinkage);
622 302 global->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
623 302 return global;
624 302 }
625
626 10478 llvm::GlobalVariable *IRGenerator::createGlobalStringConst(const std::string &baseName, const std::string &value) const {
627 // Get unused name
628
1/2
✓ Branch 2 → 3 taken 10478 times.
✗ Branch 2 → 21 not taken.
10478 const std::string globalName = getUnusedGlobalName(baseName);
629 // Create global
630
2/4
✓ Branch 3 → 4 taken 10478 times.
✗ Branch 3 → 16 not taken.
✓ Branch 5 → 6 taken 10478 times.
✗ Branch 5 → 15 not taken.
10478 builder.CreateGlobalString(value, globalName, 0, module);
631
1/2
✓ Branch 7 → 8 taken 10478 times.
✗ Branch 7 → 17 not taken.
10478 llvm::GlobalVariable *global = module->getNamedGlobal(globalName);
632 // If the output should be comparable, fix alignment to 4 bytes
633
1/2
✓ Branch 8 → 9 taken 10478 times.
✗ Branch 8 → 12 not taken.
10478 if (cliOptions.comparableOutput)
634
2/4
✓ Branch 9 → 10 taken 10478 times.
✗ Branch 9 → 18 not taken.
✓ Branch 10 → 11 taken 10478 times.
✗ Branch 10 → 18 not taken.
10478 global->setAlignment(llvm::Align(4));
635 10478 return global;
636 10478 }
637
638 10478 llvm::GlobalVariable *IRGenerator::createGlobalStringConst(const std::string &baseName, const std::string &value,
639 const CodeLoc &codeLoc) const {
640 10478 llvm::GlobalVariable *global = createGlobalStringConst(baseName, value);
641 // Create debug info
642
2/2
✓ Branch 3 → 4 taken 54 times.
✓ Branch 3 → 10 taken 10424 times.
10478 if (cliOptions.instrumentation.generateDebugInfo)
643
3/6
✓ Branch 5 → 6 taken 54 times.
✗ Branch 5 → 14 not taken.
✓ Branch 6 → 7 taken 54 times.
✗ Branch 6 → 14 not taken.
✓ Branch 7 → 8 taken 54 times.
✗ Branch 7 → 12 not taken.
54 diGenerator.generateGlobalStringDebugInfo(global, global->getName().str(), value.length(), codeLoc);
644 10478 return global;
645 }
646
647 17750 std::string IRGenerator::getUnusedGlobalName(const std::string &baseName) const {
648 // Find an unused global name
649 17750 std::string globalName;
650 17750 unsigned int suffixNumber = 0;
651 do {
652
1/2
✓ Branch 5 → 6 taken 678213 times.
✗ Branch 5 → 15 not taken.
678213 globalName = baseName + std::to_string(suffixNumber);
653 678213 suffixNumber++;
654
3/4
✓ Branch 10 → 11 taken 678213 times.
✗ Branch 10 → 19 not taken.
✓ Branch 11 → 12 taken 660463 times.
✓ Branch 11 → 13 taken 17750 times.
678213 } while (module->getNamedGlobal(globalName) != nullptr);
655 17750 return globalName;
656 }
657
658 59803 void IRGenerator::materializeConstant(LLVMExprResult &exprResult) {
659 // Skip results, that do not contain a constant or already have a value
660
3/4
✓ Branch 2 → 3 taken 57492 times.
✓ Branch 2 → 4 taken 2311 times.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 57492 times.
59803 if (exprResult.value != nullptr || exprResult.constant == nullptr)
661 2311 return;
662
663 // Default case: the value to the constant
664 57492 exprResult.value = exprResult.constant;
665 }
666
667 42388 bool IRGenerator::isSymbolDSOLocal(bool isPublic) const {
668 // If we are compiling a shared library and export the global symbol, we need to drop dso_local
669 // because it may be interposed by other shared objects by the dynamic linker.
670
3/4
✓ Branch 2 → 3 taken 38449 times.
✓ Branch 2 → 4 taken 3939 times.
✓ Branch 3 → 4 taken 38449 times.
✗ Branch 3 → 5 not taken.
42388 return !(isPublic && cliOptions.outputContainer == OutputContainer::SHARED_LIBRARY);
671 }
672
673 14745 llvm::GlobalValue::LinkageTypes IRGenerator::getSymbolLinkageType(bool isPublic) const {
674
2/2
✓ Branch 2 → 3 taken 11999 times.
✓ Branch 2 → 4 taken 2746 times.
14745 return isPublic ? llvm::GlobalValue::ExternalLinkage : llvm::GlobalValue::PrivateLinkage;
675 }
676
677 6759 llvm::GlobalValue::LinkageTypes IRGenerator::getVTableLinkageType(bool isPublic) const {
678 // VTables, type infos and type info names are ODR entities that may legitimately be emitted in more than one
679 // translation unit (e.g. an interface that is defined in one file and used from several importing files). Giving
680 // them weak ODR linkage lets the linker coalesce the duplicates. On ELF this pairs with the comdat group below; on
681 // MachO, which has no comdat support, the weak/coalesced linkage is what prevents a duplicate-symbol error.
682
2/2
✓ Branch 2 → 3 taken 6672 times.
✓ Branch 2 → 4 taken 87 times.
6759 return isPublic ? llvm::GlobalValue::WeakODRLinkage : llvm::GlobalValue::PrivateLinkage;
683 }
684
685 6759 void IRGenerator::attachComdatToSymbol(llvm::GlobalVariable *global, const std::string &comdatName, bool isPublic) const {
686 // MachO does not support comdat annotations
687
6/6
✓ Branch 2 → 3 taken 6672 times.
✓ Branch 2 → 6 taken 87 times.
✓ Branch 4 → 5 taken 6657 times.
✓ Branch 4 → 6 taken 15 times.
✓ Branch 7 → 8 taken 6657 times.
✓ Branch 7 → 12 taken 102 times.
6759 if (isPublic && cliOptions.targetTriple.getObjectFormat() != llvm::Triple::MachO)
688
2/4
✓ Branch 9 → 10 taken 6657 times.
✗ Branch 9 → 13 not taken.
✓ Branch 10 → 11 taken 6657 times.
✗ Branch 10 → 13 not taken.
6657 global->setComdat(module->getOrInsertComdat(comdatName));
689 6759 }
690
691 /**
692 * Attach the function attributes that all functions we emit have in common.
693 *
694 * Spice does not know exceptions and we never emit landing pads, so none of our functions can unwind. We still request
695 * an unwind table, so that debuggers and profilers are able to produce correct stack traces.
696 *
697 * The size levels are not communicated to LLVM by the pass pipeline alone - since Os and Oz both select the O2
698 * pipeline, 'optsize' and 'minsize' on the individual function are what actually distinguishes them. Without 'minsize',
699 * Oz is indistinguishable from Os.
700 *
701 * @param fct Function to attach the attributes to
702 * @param isAlwaysInline Whether the function was declared as inline
703 */
704 34812 void IRGenerator::addCommonFctAttrs(llvm::Function *fct, bool isAlwaysInline) const {
705 34812 fct->addFnAttr(llvm::Attribute::NoUnwind);
706 34812 fct->addFnAttr(llvm::Attribute::getWithUWTableKind(context, llvm::UWTableKind::Default));
707
708 // Explicitly inlined functions must not be marked as 'optnone', because that is incompatible with 'alwaysinline'.
709 // This matches the behavior of other frontends: an inline request is honored, even at O0.
710
2/2
✓ Branch 5 → 6 taken 7018 times.
✓ Branch 5 → 7 taken 27794 times.
34812 if (isAlwaysInline) {
711 7018 fct->addFnAttr(llvm::Attribute::AlwaysInline);
712
2/2
✓ Branch 7 → 8 taken 27746 times.
✓ Branch 7 → 10 taken 48 times.
27794 } else if (cliOptions.optLevel == OptLevel::O0) {
713 27746 fct->addFnAttr(llvm::Attribute::OptimizeNone);
714 27746 fct->addFnAttr(llvm::Attribute::NoInline); // 'optnone' requires 'noinline'
715 }
716
717
2/2
✓ Branch 10 → 11 taken 2 times.
✓ Branch 10 → 14 taken 34810 times.
34812 if (cliOptions.optLevel >= OptLevel::Os) {
718 2 fct->addFnAttr(llvm::Attribute::OptimizeForSize);
719
2/2
✓ Branch 12 → 13 taken 1 time.
✓ Branch 12 → 14 taken 1 time.
2 if (cliOptions.optLevel == OptLevel::Oz)
720 1 fct->addFnAttr(llvm::Attribute::MinSize);
721 }
722 34812 }
723
724 216389 llvm::Value *IRGenerator::getAddress(const SymbolTableEntry *entry) {
725
1/2
✓ Branch 2 → 3 taken 216389 times.
✗ Branch 2 → 18 not taken.
216389 const auto it = addressMap.find(entry);
726
5/6
✓ Branch 5 → 6 taken 216339 times.
✓ Branch 5 → 9 taken 50 times.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 216339 times.
✓ Branch 11 → 12 taken 50 times.
✓ Branch 11 → 13 taken 216339 times.
216389 if (it == addressMap.end() || it->second.empty())
727 50 return nullptr;
728 216339 return it->second.top();
729 }
730
731 169030 void IRGenerator::updateAddress(const SymbolTableEntry *entry, llvm::Value *address) {
732
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 169030 times.
169030 assert(address != nullptr);
733
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 169030 times.
169030 assert(address->getType()->isPointerTy());
734 169030 auto &stack = addressMap[entry];
735
2/2
✓ Branch 10 → 11 taken 136641 times.
✓ Branch 10 → 12 taken 32389 times.
169030 if (stack.empty())
736 136641 stack.push(address);
737 else
738 32389 stack.top() = address;
739 169030 }
740
741 54 void IRGenerator::pushAddress(const SymbolTableEntry *entry, llvm::Value *address) {
742
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 54 times.
54 assert(address != nullptr);
743 54 addressMap[entry].push(address);
744 54 }
745
746 54 void IRGenerator::popAddress(const SymbolTableEntry *entry) {
747
1/2
✓ Branch 2 → 3 taken 54 times.
✗ Branch 2 → 14 not taken.
54 auto it = addressMap.find(entry);
748
2/4
✓ Branch 5 → 6 taken 54 times.
✗ Branch 5 → 10 not taken.
✓ Branch 8 → 9 taken 54 times.
✗ Branch 8 → 10 not taken.
54 assert(it != addressMap.end() && !it->second.empty());
749 54 it->second.pop();
750 54 }
751
752 7332 llvm::Function *IRGenerator::getLLVMFunction(const Function *spiceFunc) {
753
1/2
✓ Branch 2 → 3 taken 7332 times.
✗ Branch 2 → 12 not taken.
7332 const auto it = llvmFunctions.find(spiceFunc);
754
2/2
✓ Branch 5 → 6 taken 2954 times.
✓ Branch 5 → 8 taken 4378 times.
14664 return it != llvmFunctions.end() ? it->second : nullptr;
755 }
756
757 33259 void IRGenerator::setLLVMFunction(const Function *spiceFunc, llvm::Function *llvmFunction) {
758
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 33259 times.
33259 assert(llvmFunction != nullptr);
759 33259 llvmFunctions[spiceFunc] = llvmFunction;
760 33259 }
761
762 4234 std::string IRGenerator::getIRString(llvm::Module *llvmModule, const CliOptions &cliOptions) {
763
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 4234 times.
4234 assert(llvmModule != nullptr); // Make sure the module hasn't been moved away
764
3/4
✓ Branch 4 → 5 taken 4234 times.
✗ Branch 4 → 7 not taken.
✓ Branch 5 → 6 taken 4136 times.
✓ Branch 5 → 7 taken 98 times.
4234 const bool eliminateTarget = cliOptions.comparableOutput && cliOptions.isNativeTarget;
765
766 // Backup target triple and data layout
767
1/2
✓ Branch 9 → 10 taken 4234 times.
✗ Branch 9 → 45 not taken.
4234 const llvm::Triple targetTriple = llvmModule->getTargetTriple();
768
1/2
✓ Branch 11 → 12 taken 4234 times.
✗ Branch 11 → 43 not taken.
4234 const std::string targetDataLayout = llvmModule->getDataLayoutStr();
769 // Remove target triple and data layout
770
2/2
✓ Branch 12 → 13 taken 4136 times.
✓ Branch 12 → 19 taken 98 times.
4234 if (eliminateTarget) {
771 4136 llvmModule->setTargetTriple(llvm::Triple());
772
2/4
✓ Branch 16 → 17 taken 4136 times.
✗ Branch 16 → 34 not taken.
✓ Branch 17 → 18 taken 4136 times.
✗ Branch 17 → 34 not taken.
4136 llvmModule->setDataLayout("");
773 }
774
775 // Get IR string
776 4234 std::string output;
777
1/2
✓ Branch 20 → 21 taken 4234 times.
✗ Branch 20 → 39 not taken.
4234 llvm::raw_string_ostream oss(output);
778
1/2
✓ Branch 21 → 22 taken 4234 times.
✗ Branch 21 → 37 not taken.
4234 llvmModule->print(oss, nullptr);
779
780 // Restore target triple and data layout
781
2/2
✓ Branch 22 → 23 taken 4136 times.
✓ Branch 22 → 29 taken 98 times.
4234 if (eliminateTarget) {
782
1/2
✓ Branch 23 → 24 taken 4136 times.
✗ Branch 23 → 35 not taken.
4136 llvmModule->setTargetTriple(targetTriple);
783
1/2
✓ Branch 27 → 28 taken 4136 times.
✗ Branch 27 → 36 not taken.
4136 llvmModule->setDataLayout(targetDataLayout);
784 }
785
786 4234 return output;
787 4234 }
788
789 /**
790 * Returns the operator function list for the current manifestation and the given node
791 *
792 * @param node Node to retrieve the op fct pointer list from
793 * @return Op fct pointer list
794 */
795 67417 const std::vector<const Function *> &IRGenerator::getOpFctPointers(const ASTNode *node) const {
796
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 67417 times.
67417 assert(node->getOpFctPointers()->size() > manIdx);
797 67417 return node->getOpFctPointers()->at(manIdx);
798 }
799
800 } // namespace spice::compiler
801