GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 97.0% 354 / 6 / 371
Functions: 100.0% 37 / 0 / 37
Branches: 62.7% 420 / 20 / 690

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 <llvm/IR/Module.h>
6 #include <llvm/IR/Verifier.h>
7
8 #include <SourceFile.h>
9 #include <global/GlobalResourceManager.h>
10 #include <irgenerator/NameMangling.h>
11 #include <symboltablebuilder/SymbolTableBuilder.h>
12
13 namespace spice::compiler {
14
15 992 IRGenerator::IRGenerator(GlobalResourceManager &resourceManager, SourceFile *sourceFile)
16 992 : CompilerPass(resourceManager, sourceFile), context(cliOptions.useLTO ? resourceManager.ltoContext : sourceFile->context),
17
1/2
✓ Branch 8 → 9 taken 992 times.
✗ Branch 8 → 62 not taken.
992 builder(sourceFile->builder), module(sourceFile->llvmModule.get()), conversionManager(sourceFile, this),
18
6/10
✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 6 taken 990 times.
✓ Branch 9 → 10 taken 992 times.
✗ Branch 9 → 62 not taken.
✓ Branch 10 → 11 taken 992 times.
✗ Branch 10 → 62 not taken.
✓ Branch 11 → 12 taken 992 times.
✗ Branch 11 → 60 not taken.
✓ Branch 15 → 16 taken 992 times.
✗ Branch 15 → 56 not taken.
1984 stdFunctionManager(sourceFile, resourceManager, module) {
19 // Attach information to the module
20
1/2
✓ Branch 17 → 18 taken 992 times.
✗ Branch 17 → 41 not taken.
992 module->setTargetTriple(cliOptions.targetTriple);
21
2/4
✓ Branch 21 → 22 taken 992 times.
✗ Branch 21 → 44 not taken.
✓ Branch 22 → 23 taken 992 times.
✗ Branch 22 → 42 not taken.
992 module->setDataLayout(sourceFile->targetMachine->createDataLayout());
22
1/2
✓ Branch 24 → 25 taken 992 times.
✗ Branch 24 → 52 not taken.
992 module->setPICLevel(llvm::PICLevel::BigPIC);
23
1/2
✓ Branch 25 → 26 taken 992 times.
✗ Branch 25 → 52 not taken.
992 module->setPIELevel(llvm::PIELevel::Large);
24
1/2
✓ Branch 26 → 27 taken 992 times.
✗ Branch 26 → 52 not taken.
992 module->setUwtable(llvm::UWTableKind::Default);
25
1/2
✓ Branch 27 → 28 taken 992 times.
✗ Branch 27 → 52 not taken.
992 module->setFramePointer(llvm::FramePointerKind::All);
26
27 // Add module identifier metadata
28
2/4
✓ Branch 28 → 29 taken 992 times.
✗ Branch 28 → 45 not taken.
✓ Branch 29 → 30 taken 992 times.
✗ Branch 29 → 45 not taken.
992 llvm::NamedMDNode *identifierMetadata = module->getOrInsertNamedMetadata("llvm.ident");
29
3/6
✓ Branch 31 → 32 taken 992 times.
✗ Branch 31 → 46 not taken.
✓ Branch 33 → 34 taken 992 times.
✗ Branch 33 → 46 not taken.
✓ Branch 34 → 35 taken 992 times.
✗ Branch 34 → 46 not taken.
992 identifierMetadata->addOperand(llvm::MDNode::get(context, llvm::MDString::get(context, PRODUCER_STRING)));
30
31 // Initialize debug info generator
32
2/2
✓ Branch 35 → 36 taken 24 times.
✓ Branch 35 → 40 taken 968 times.
992 if (cliOptions.instrumentation.generateDebugInfo)
33
2/4
✓ Branch 36 → 37 taken 24 times.
✗ Branch 36 → 51 not taken.
✓ Branch 37 → 38 taken 24 times.
✗ Branch 37 → 49 not taken.
24 diGenerator.initialize(sourceFile->fileName, sourceFile->fileDir);
34 992 }
35
36 992 std::any IRGenerator::visitEntry(const EntryNode *node) {
37 // Generate IR
38
1/2
✓ Branch 2 → 3 taken 992 times.
✗ Branch 2 → 19 not taken.
992 visitChildren(node);
39
40 // Generate test main if required
41
4/4
✓ Branch 4 → 5 taken 258 times.
✓ Branch 4 → 7 taken 734 times.
✓ Branch 5 → 6 taken 4 times.
✓ Branch 5 → 7 taken 254 times.
992 if (sourceFile->isMainFile && cliOptions.generateTestMain)
42 4 generateTestMain();
43
44 // Execute deferred VTable initializations
45
2/2
✓ Branch 13 → 9 taken 396 times.
✓ Branch 13 → 14 taken 992 times.
1388 for (DeferredLogic &deferredVTableInit : deferredVTableInitializations)
46
1/2
✓ Branch 10 → 11 taken 396 times.
✗ Branch 10 → 20 not taken.
396 deferredVTableInit.execute();
47
48 // Finalize debug info generator
49 992 diGenerator.finalize();
50
51 // Verify module
52 992 verifyModule(node->codeLoc);
53
54
1/2
✓ Branch 16 → 17 taken 992 times.
✗ Branch 16 → 21 not taken.
992 return nullptr;
55 }
56
57 40378 llvm::AllocaInst *IRGenerator::insertAlloca(llvm::Type *llvmType, std::string varName) {
58
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 40378 times.
40378 if (!cliOptions.namesForIRValues)
59 varName = "";
60
61
2/2
✓ Branch 4 → 5 taken 28718 times.
✓ Branch 4 → 12 taken 11660 times.
40378 if (allocaInsertInst != nullptr) { // If there is already an alloca inst, insert right after that
62
2/4
✓ Branch 5 → 6 taken 28718 times.
✗ Branch 5 → 29 not taken.
✓ Branch 6 → 7 taken 28718 times.
✗ Branch 6 → 29 not taken.
28718 llvm::AllocaInst *allocaInst = builder.CreateAlloca(llvmType, nullptr, varName);
63
1/2
✓ Branch 8 → 9 taken 28718 times.
✗ Branch 8 → 30 not taken.
28718 allocaInst->setDebugLoc(llvm::DebugLoc());
64 28718 allocaInst->moveAfter(allocaInsertInst);
65 28718 allocaInsertInst = allocaInst;
66 } else { // This is the first alloca inst in the current function -> insert at the entry block
67 // Save current basic block and move insert cursor to entry block of the current function
68 11660 llvm::BasicBlock *currentBlock = builder.GetInsertBlock();
69 11660 builder.SetInsertPoint(allocaInsertBlock, allocaInsertBlock->begin());
70
71 // Allocate the size of the given LLVM type
72
2/4
✓ Branch 15 → 16 taken 11660 times.
✗ Branch 15 → 33 not taken.
✓ Branch 16 → 17 taken 11660 times.
✗ Branch 16 → 33 not taken.
11660 allocaInsertInst = builder.CreateAlloca(llvmType, nullptr, varName);
73
1/2
✓ Branch 18 → 19 taken 11660 times.
✗ Branch 18 → 34 not taken.
11660 allocaInsertInst->setDebugLoc(llvm::DebugLoc());
74
75 // Restore old basic block
76 11660 builder.SetInsertPoint(currentBlock);
77 }
78
79 // Insert lifetime start marker
80
2/2
✓ Branch 21 → 22 taken 59 times.
✓ Branch 21 → 27 taken 40319 times.
40378 if (cliOptions.useLifetimeMarkers) {
81
2/4
✓ Branch 23 → 24 taken 59 times.
✗ Branch 23 → 37 not taken.
✓ Branch 24 → 25 taken 59 times.
✗ Branch 24 → 37 not taken.
59 const uint64_t sizeInBytes = module->getDataLayout().getTypeAllocSize(llvmType);
82 59 builder.CreateLifetimeStart(allocaInsertInst, builder.getInt64(sizeInBytes));
83 }
84
85 40378 return allocaInsertInst;
86 }
87
88 35033 llvm::AllocaInst *IRGenerator::insertAlloca(const QualType &qualType, const std::string &varName) {
89 35033 llvm::Type *llvmType = qualType.toLLVMType(sourceFile);
90
2/4
✓ Branch 3 → 4 taken 35033 times.
✗ Branch 3 → 12 not taken.
✓ Branch 4 → 5 taken 35033 times.
✗ Branch 4 → 10 not taken.
35033 llvm::AllocaInst *alloca = insertAlloca(llvmType, varName);
91
92 // Insert type metadata
93
2/2
✓ Branch 6 → 7 taken 9 times.
✓ Branch 6 → 8 taken 35024 times.
35033 if (cliOptions.useTBAAMetadata)
94 9 mdGenerator.generateTypeMetadata(allocaInsertInst, qualType);
95
96 35033 return alloca;
97 }
98
99 77453 llvm::LoadInst *IRGenerator::insertLoad(llvm::Type *llvmType, llvm::Value *ptr, bool isVolatile,
100 const std::string &varName) const {
101
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 77453 times.
77453 assert(ptr->getType()->isPointerTy());
102
5/14
✓ Branch 6 → 7 taken 77453 times.
✗ Branch 6 → 8 not taken.
✓ Branch 7 → 11 taken 77453 times.
✗ Branch 7 → 22 not taken.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 22 not taken.
✓ Branch 11 → 12 taken 77453 times.
✗ Branch 11 → 20 not taken.
✓ Branch 12 → 13 taken 77453 times.
✗ Branch 12 → 20 not taken.
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 18 taken 77453 times.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 25 not taken.
77453 return builder.CreateLoad(llvmType, ptr, isVolatile, cliOptions.namesForIRValues ? varName : "");
103 }
104
105 70173 llvm::LoadInst *IRGenerator::insertLoad(const QualType &qualType, llvm::Value *ptr, bool isVolatile, const std::string &varName) {
106 70173 llvm::Type *llvmType = qualType.toLLVMType(sourceFile);
107 70173 llvm::LoadInst *load = insertLoad(llvmType, ptr, isVolatile, varName);
108
2/2
✓ Branch 4 → 5 taken 8 times.
✓ Branch 4 → 6 taken 70165 times.
70173 if (cliOptions.useTBAAMetadata)
109 8 mdGenerator.generateTBAAMetadata(load, qualType);
110 70173 return load;
111 }
112
113 41691 llvm::StoreInst *IRGenerator::insertStore(llvm::Value *val, llvm::Value *ptr, bool isVolatile) const {
114
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 41691 times.
41691 assert(ptr->getType()->isPointerTy());
115 41691 return builder.CreateStore(val, ptr, isVolatile);
116 }
117
118 12565 void IRGenerator::insertStore(llvm::Value *val, llvm::Value *ptr, const QualType &qualType, bool isVolatile) {
119 12565 llvm::StoreInst *store = insertStore(val, ptr, isVolatile);
120
2/2
✓ Branch 3 → 4 taken 9 times.
✓ Branch 3 → 5 taken 12556 times.
12565 if (cliOptions.useTBAAMetadata)
121 9 mdGenerator.generateTBAAMetadata(store, qualType);
122 12565 }
123
124 19741 llvm::Value *IRGenerator::insertInBoundsGEP(llvm::Type *type, llvm::Value *basePtr, llvm::ArrayRef<llvm::Value *> indices,
125 std::string varName) const {
126
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 19741 times.
19741 assert(basePtr->getType()->isPointerTy());
127
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 19741 times.
19741 assert(!indices.empty());
128
4/6
✓ Branch 4 → 5 taken 19405 times.
✓ Branch 4 → 7 taken 17218 times.
✓ Branch 6 → 7 taken 19405 times.
✗ Branch 6 → 8 not taken.
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 19741 times.
56364 assert(std::ranges::all_of(indices, [](const llvm::Value *index) {
129 const llvm::Type *indexType = index->getType();
130 return indexType->isIntegerTy(32) || indexType->isIntegerTy(64);
131 }));
132
133
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 19741 times.
19741 if (!cliOptions.namesForIRValues)
134 varName.clear();
135
136 // Insert GEP
137
2/4
✓ Branch 14 → 15 taken 19741 times.
✗ Branch 14 → 19 not taken.
✓ Branch 15 → 16 taken 19741 times.
✗ Branch 15 → 19 not taken.
19741 return builder.CreateInBoundsGEP(type, basePtr, indices, varName);
138 }
139
140 5575 llvm::Value *IRGenerator::insertStructGEP(llvm::Type *type, llvm::Value *basePtr, unsigned int index, std::string varName) const {
141
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 5575 times.
5575 assert(basePtr->getType()->isPointerTy());
142
143
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 5575 times.
5575 if (!cliOptions.namesForIRValues)
144 varName.clear();
145
146 // If we use index 0 we can use the base pointer directly
147
2/2
✓ Branch 8 → 9 taken 1946 times.
✓ Branch 8 → 10 taken 3629 times.
5575 if (index == 0)
148 1946 return basePtr;
149
150 // Insert GEP
151
2/4
✓ Branch 10 → 11 taken 3629 times.
✗ Branch 10 → 15 not taken.
✓ Branch 11 → 12 taken 3629 times.
✗ Branch 11 → 15 not taken.
3629 return builder.CreateStructGEP(type, basePtr, index, varName);
152 }
153
154 47104 llvm::Value *IRGenerator::resolveValue(const ExprNode *node) {
155 // Visit the given AST node
156
2/4
✓ Branch 2 → 3 taken 47104 times.
✗ Branch 2 → 11 not taken.
✓ Branch 3 → 4 taken 47104 times.
✗ Branch 3 → 9 not taken.
47104 auto exprResult = any_cast<LLVMExprResult>(visit(node));
157
1/2
✓ Branch 5 → 6 taken 47104 times.
✗ Branch 5 → 12 not taken.
94208 return resolveValue(node, exprResult);
158 }
159
160 53729 llvm::Value *IRGenerator::resolveValue(const ExprNode *node, LLVMExprResult &exprResult) {
161 53729 return resolveValue(node->getEvaluatedSymbolType(manIdx), exprResult);
162 }
163
164 103670 llvm::Value *IRGenerator::resolveValue(const QualType &qualType, LLVMExprResult &exprResult) {
165 // Check if the value is already present
166
2/2
✓ Branch 2 → 3 taken 33548 times.
✓ Branch 2 → 4 taken 70122 times.
103670 if (exprResult.value != nullptr)
167 33548 return exprResult.value;
168
169 // Check if a constant is present
170
2/2
✓ Branch 4 → 5 taken 18362 times.
✓ Branch 4 → 7 taken 51760 times.
70122 if (exprResult.constant != nullptr) {
171 18362 materializeConstant(exprResult);
172 18362 return exprResult.value;
173 }
174
175
3/4
✓ Branch 7 → 8 taken 576 times.
✓ Branch 7 → 10 taken 51184 times.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 576 times.
51760 assert(exprResult.ptr != nullptr || exprResult.refPtr != nullptr);
176
177 // De-reference if reference type
178
4/4
✓ Branch 10 → 11 taken 48293 times.
✓ Branch 10 → 13 taken 3467 times.
✓ Branch 11 → 12 taken 10 times.
✓ Branch 11 → 13 taken 48283 times.
51760 const bool isVolatile = exprResult.entry && exprResult.entry->isVolatile;
179
4/4
✓ Branch 14 → 15 taken 580 times.
✓ Branch 14 → 24 taken 51180 times.
✓ Branch 15 → 16 taken 576 times.
✓ Branch 15 → 24 taken 4 times.
51760 if (exprResult.refPtr != nullptr && exprResult.ptr == nullptr)
180
3/6
✓ Branch 18 → 19 taken 576 times.
✗ Branch 18 → 36 not taken.
✓ Branch 19 → 20 taken 576 times.
✗ Branch 19 → 34 not taken.
✓ Branch 20 → 21 taken 576 times.
✗ Branch 20 → 34 not taken.
1152 exprResult.ptr = insertLoad(builder.getPtrTy(), exprResult.refPtr, isVolatile);
181
182 // Load the value from the pointer
183
1/2
✓ Branch 24 → 25 taken 51760 times.
✗ Branch 24 → 46 not taken.
51760 const QualType referencedType = qualType.removeReferenceWrapper();
184
2/4
✓ Branch 27 → 28 taken 51760 times.
✗ Branch 27 → 42 not taken.
✓ Branch 28 → 29 taken 51760 times.
✗ Branch 28 → 40 not taken.
51760 exprResult.value = insertLoad(referencedType, exprResult.ptr, isVolatile);
185
186 51760 return exprResult.value;
187 }
188
189 2989 llvm::Value *IRGenerator::resolveAddress(const ASTNode *node) {
190 // Visit the given AST node
191
2/4
✓ Branch 2 → 3 taken 2989 times.
✗ Branch 2 → 11 not taken.
✓ Branch 3 → 4 taken 2989 times.
✗ Branch 3 → 9 not taken.
2989 auto exprResult = any_cast<LLVMExprResult>(visit(node));
192
1/2
✓ Branch 5 → 6 taken 2989 times.
✗ Branch 5 → 12 not taken.
5978 return resolveAddress(exprResult);
193 }
194
195 22993 llvm::Value *IRGenerator::resolveAddress(LLVMExprResult &exprResult) {
196 // Check if an address is already present
197
2/2
✓ Branch 2 → 3 taken 19394 times.
✓ Branch 2 → 4 taken 3599 times.
22993 if (exprResult.ptr != nullptr)
198 19394 return exprResult.ptr;
199
200 // Check if the reference address is already present
201
3/4
✓ Branch 4 → 5 taken 2842 times.
✓ Branch 4 → 7 taken 757 times.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 2842 times.
3599 const bool isVolatile = exprResult.entry && exprResult.entry->isVolatile;
202
3/4
✓ Branch 8 → 9 taken 2841 times.
✓ Branch 8 → 18 taken 758 times.
✓ Branch 9 → 10 taken 2841 times.
✗ Branch 9 → 18 not taken.
3599 if (exprResult.refPtr != nullptr && exprResult.ptr == nullptr) {
203
3/6
✓ Branch 12 → 13 taken 2841 times.
✗ Branch 12 → 37 not taken.
✓ Branch 13 → 14 taken 2841 times.
✗ Branch 13 → 35 not taken.
✓ Branch 14 → 15 taken 2841 times.
✗ Branch 14 → 35 not taken.
2841 exprResult.ptr = insertLoad(builder.getPtrTy(), exprResult.refPtr, isVolatile);
204 2841 return exprResult.ptr;
205 }
206
207 // If not, store the value or constant
208 758 materializeConstant(exprResult);
209
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 758 times.
758 assert(exprResult.value != nullptr);
210
7/12
✓ Branch 21 → 22 taken 6 times.
✓ Branch 21 → 23 taken 752 times.
✓ Branch 22 → 26 taken 6 times.
✗ Branch 22 → 43 not taken.
✓ Branch 25 → 26 taken 752 times.
✗ Branch 25 → 43 not taken.
✓ Branch 27 → 28 taken 758 times.
✗ Branch 27 → 41 not taken.
✓ Branch 29 → 30 taken 752 times.
✓ Branch 29 → 32 taken 6 times.
✗ Branch 43 → 44 not taken.
✗ Branch 43 → 46 not taken.
1510 exprResult.ptr = insertAlloca(exprResult.value->getType(), exprResult.entry ? exprResult.entry->name : "");
211 758 insertStore(exprResult.value, exprResult.ptr, isVolatile);
212
213 758 return exprResult.ptr;
214 }
215
216 2659 llvm::Constant *IRGenerator::getDefaultValueForSymbolType(const QualType &symbolType) { // NOLINT(misc-no-recursion)
217 // Double
218
2/2
✓ Branch 3 → 4 taken 4 times.
✓ Branch 3 → 9 taken 2655 times.
2659 if (symbolType.is(TY_DOUBLE))
219
2/4
✓ Branch 4 → 5 taken 4 times.
✗ Branch 4 → 132 not taken.
✓ Branch 5 → 6 taken 4 times.
✗ Branch 5 → 130 not taken.
4 return llvm::ConstantFP::get(context, llvm::APFloat(0.0));
220
221 // Int
222
2/2
✓ Branch 10 → 11 taken 360 times.
✓ Branch 10 → 13 taken 2295 times.
2655 if (symbolType.is(TY_INT))
223 360 return builder.getInt32(0);
224
225 // Short
226
2/2
✓ Branch 14 → 15 taken 12 times.
✓ Branch 14 → 17 taken 2283 times.
2295 if (symbolType.is(TY_SHORT))
227 12 return builder.getInt16(0);
228
229 // Long
230
2/2
✓ Branch 18 → 19 taken 992 times.
✓ Branch 18 → 21 taken 1291 times.
2283 if (symbolType.is(TY_LONG))
231 992 return builder.getInt64(0);
232
233 // Byte or char
234
3/4
✓ Branch 21 → 22 taken 1291 times.
✗ Branch 21 → 133 not taken.
✓ Branch 22 → 23 taken 20 times.
✓ Branch 22 → 25 taken 1271 times.
1291 if (symbolType.isOneOf({TY_BYTE, TY_CHAR}))
235 20 return builder.getInt8(0);
236
237 // String
238
2/2
✓ Branch 26 → 27 taken 392 times.
✓ Branch 26 → 35 taken 879 times.
1271 if (symbolType.is(TY_STRING)) {
239
3/6
✓ Branch 27 → 28 taken 392 times.
✗ Branch 27 → 135 not taken.
✓ Branch 28 → 29 taken 392 times.
✗ Branch 28 → 134 not taken.
✓ Branch 29 → 30 taken 392 times.
✗ Branch 29 → 134 not taken.
392 llvm::GlobalVariable *globalString = builder.CreateGlobalString("", "");
240
1/2
✓ Branch 30 → 31 taken 392 times.
✗ Branch 30 → 34 not taken.
392 if (cliOptions.comparableOutput)
241
2/4
✓ Branch 31 → 32 taken 392 times.
✗ Branch 31 → 136 not taken.
✓ Branch 32 → 33 taken 392 times.
✗ Branch 32 → 136 not taken.
392 globalString->setAlignment(llvm::Align(4));
242 392 return globalString;
243 }
244
245 // Bool
246
2/2
✓ Branch 36 → 37 taken 33 times.
✓ Branch 36 → 39 taken 846 times.
879 if (symbolType.is(TY_BOOL))
247 33 return builder.getFalse();
248
249 // Pointer or reference
250
3/4
✓ Branch 39 → 40 taken 846 times.
✗ Branch 39 → 137 not taken.
✓ Branch 40 → 41 taken 789 times.
✓ Branch 40 → 44 taken 57 times.
846 if (symbolType.isOneOf({TY_PTR, TY_REF}))
251 789 return llvm::Constant::getNullValue(builder.getPtrTy());
252
253 // Array
254
2/2
✓ Branch 45 → 46 taken 14 times.
✓ Branch 45 → 61 taken 43 times.
57 if (symbolType.isArray()) {
255 // Get array size
256
1/2
✓ Branch 46 → 47 taken 14 times.
✗ Branch 46 → 146 not taken.
14 const size_t arraySize = symbolType.getArraySize();
257
258 // Get default value for item
259
2/4
✓ Branch 47 → 48 taken 14 times.
✗ Branch 47 → 138 not taken.
✓ Branch 48 → 49 taken 14 times.
✗ Branch 48 → 138 not taken.
14 llvm::Constant *defaultItemValue = getDefaultValueForSymbolType(symbolType.getContained());
260
261 // Retrieve array and item type
262
2/4
✓ Branch 49 → 50 taken 14 times.
✗ Branch 49 → 139 not taken.
✓ Branch 50 → 51 taken 14 times.
✗ Branch 50 → 139 not taken.
14 llvm::Type *itemType = symbolType.getContained().toLLVMType(sourceFile);
263
1/2
✓ Branch 51 → 52 taken 14 times.
✗ Branch 51 → 146 not taken.
14 llvm::ArrayType *arrayType = llvm::ArrayType::get(itemType, arraySize);
264
265 // Create a constant array with n times the default value
266
1/2
✓ Branch 54 → 55 taken 14 times.
✗ Branch 54 → 140 not taken.
14 const std::vector itemConstants(arraySize, defaultItemValue);
267
1/2
✓ Branch 57 → 58 taken 14 times.
✗ Branch 57 → 143 not taken.
14 return llvm::ConstantArray::get(arrayType, itemConstants);
268 14 }
269
270 // Function or procedure
271
3/4
✓ Branch 61 → 62 taken 43 times.
✗ Branch 61 → 147 not taken.
✓ Branch 62 → 63 taken 16 times.
✓ Branch 62 → 75 taken 27 times.
43 if (symbolType.isOneOf({TY_FUNCTION, TY_PROCEDURE})) {
272
2/2
✓ Branch 63 → 64 taken 8 times.
✓ Branch 63 → 69 taken 8 times.
16 if (!llvmTypes.fatPtrType)
273
3/6
✓ Branch 64 → 65 taken 8 times.
✗ Branch 64 → 148 not taken.
✓ Branch 65 → 66 taken 8 times.
✗ Branch 65 → 148 not taken.
✓ Branch 67 → 68 taken 8 times.
✗ Branch 67 → 148 not taken.
8 llvmTypes.fatPtrType = llvm::StructType::get(context, {builder.getPtrTy(), builder.getPtrTy()});
274
275
2/4
✓ Branch 69 → 70 taken 16 times.
✗ Branch 69 → 150 not taken.
✓ Branch 70 → 71 taken 16 times.
✗ Branch 70 → 150 not taken.
16 llvm::Constant *ptrDefaultValue = getDefaultValueForSymbolType(QualType(TY_PTR));
276
1/2
✓ Branch 72 → 73 taken 16 times.
✗ Branch 72 → 151 not taken.
16 return llvm::ConstantStruct::get(llvmTypes.fatPtrType, {ptrDefaultValue, ptrDefaultValue});
277 }
278
279 // Struct
280
2/2
✓ Branch 76 → 77 taken 26 times.
✓ Branch 76 → 114 taken 1 time.
27 if (symbolType.is(TY_STRUCT)) {
281 // Retrieve field count
282
1/2
✓ Branch 77 → 78 taken 26 times.
✗ Branch 77 → 158 not taken.
26 Scope *structScope = symbolType.getBodyScope();
283
1/2
✗ Branch 78 → 79 not taken.
✓ Branch 78 → 80 taken 26 times.
26 assert(structScope != nullptr);
284
1/2
✓ Branch 80 → 81 taken 26 times.
✗ Branch 80 → 158 not taken.
26 const size_t fieldCount = structScope->getFieldCount();
285
286 // Get default values for all fields of the struct
287 26 std::vector<llvm::Constant *> fieldConstants;
288
1/2
✓ Branch 81 → 82 taken 26 times.
✗ Branch 81 → 156 not taken.
26 fieldConstants.reserve(fieldCount);
289
290 // Add default value for each struct field
291
2/2
✓ Branch 107 → 83 taken 63 times.
✓ Branch 107 → 108 taken 26 times.
89 for (size_t i = 0; i < fieldCount; i++) {
292 // Get entry of the field
293
1/2
✗ Branch 83 → 84 not taken.
✓ Branch 83 → 85 taken 63 times.
63 const SymbolTableEntry *fieldEntry = structScope->lookupField(i);
294
3/6
✓ Branch 88 → 89 taken 63 times.
✗ Branch 88 → 92 not taken.
✓ Branch 89 → 90 taken 63 times.
✗ Branch 89 → 154 not taken.
✓ Branch 90 → 91 taken 63 times.
✗ Branch 90 → 92 not taken.
63 assert(fieldEntry != nullptr && fieldEntry->isField());
295
296 // Retrieve default field value
297 llvm::Constant *defaultFieldValue;
298
4/6
✓ Branch 93 → 94 taken 63 times.
✗ Branch 93 → 95 not taken.
✓ Branch 96 → 97 taken 63 times.
✗ Branch 96 → 102 not taken.
✓ Branch 97 → 98 taken 4 times.
✓ Branch 97 → 102 taken 59 times.
63 if (const auto fieldNode = dynamic_cast<FieldNode *>(fieldEntry->declNode); fieldNode && fieldNode->defaultValue)
299
3/6
✓ Branch 98 → 99 taken 4 times.
✗ Branch 98 → 154 not taken.
✓ Branch 99 → 100 taken 4 times.
✗ Branch 99 → 153 not taken.
✓ Branch 100 → 101 taken 4 times.
✗ Branch 100 → 153 not taken.
4 defaultFieldValue = getConst(fieldNode->defaultValue->getCompileTimeValue(), fieldEntry->getQualType(), fieldNode);
300 else
301
2/4
✓ Branch 102 → 103 taken 59 times.
✗ Branch 102 → 154 not taken.
✓ Branch 103 → 104 taken 59 times.
✗ Branch 103 → 154 not taken.
59 defaultFieldValue = getDefaultValueForSymbolType(fieldEntry->getQualType());
302
303
1/2
✓ Branch 105 → 106 taken 63 times.
✗ Branch 105 → 154 not taken.
63 fieldConstants.push_back(defaultFieldValue);
304 }
305
306
1/2
✓ Branch 108 → 109 taken 26 times.
✗ Branch 108 → 156 not taken.
26 const auto structType = reinterpret_cast<llvm::StructType *>(symbolType.toLLVMType(sourceFile));
307
1/2
✓ Branch 110 → 111 taken 26 times.
✗ Branch 110 → 155 not taken.
26 return llvm::ConstantStruct::get(structType, fieldConstants);
308 26 }
309
310 // Interface
311
1/2
✓ Branch 115 → 116 taken 1 time.
✗ Branch 115 → 121 not taken.
1 if (symbolType.is(TY_INTERFACE)) {
312 1 const auto structType = reinterpret_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 18455 llvm::Constant *IRGenerator::getConst(const CompileTimeValue &compileTimeValue, const QualType &type, const ASTNode *node) const {
320
2/2
✓ Branch 3 → 4 taken 623 times.
✓ Branch 3 → 9 taken 17832 times.
18455 if (type.is(TY_DOUBLE))
321
2/4
✓ Branch 4 → 5 taken 623 times.
✗ Branch 4 → 56 not taken.
✓ Branch 5 → 6 taken 623 times.
✗ Branch 5 → 54 not taken.
623 return llvm::ConstantFP::get(context, llvm::APFloat(compileTimeValue.doubleValue));
322
323
2/2
✓ Branch 10 → 11 taken 3728 times.
✓ Branch 10 → 13 taken 14104 times.
17832 if (type.is(TY_INT))
324 3728 return builder.getInt32(compileTimeValue.intValue);
325
326
2/2
✓ Branch 14 → 15 taken 879 times.
✓ Branch 14 → 17 taken 13225 times.
14104 if (type.is(TY_SHORT))
327 879 return builder.getInt16(compileTimeValue.shortValue);
328
329
2/2
✓ Branch 18 → 19 taken 6586 times.
✓ Branch 18 → 21 taken 6639 times.
13225 if (type.is(TY_LONG))
330 6586 return builder.getInt64(compileTimeValue.longValue);
331
332
3/4
✓ Branch 21 → 22 taken 6639 times.
✗ Branch 21 → 57 not taken.
✓ Branch 22 → 23 taken 2519 times.
✓ Branch 22 → 25 taken 4120 times.
6639 if (type.isOneOf({TY_BYTE, TY_CHAR}))
333 2519 return builder.getInt8(compileTimeValue.charValue);
334
335
2/2
✓ Branch 26 → 27 taken 2137 times.
✓ Branch 26 → 36 taken 1983 times.
4120 if (type.is(TY_STRING)) {
336 2137 const std::string &stringValue = resourceManager.compileTimeStringValues.at(compileTimeValue.stringValueOffset);
337
2/4
✓ Branch 30 → 31 taken 2137 times.
✗ Branch 30 → 60 not taken.
✓ Branch 31 → 32 taken 2137 times.
✗ Branch 31 → 58 not taken.
6411 return createGlobalStringConst(ANON_GLOBAL_STRING_NAME, stringValue, node->codeLoc);
338 }
339
340
1/2
✓ Branch 37 → 38 taken 1983 times.
✗ Branch 37 → 40 not taken.
1983 if (type.is(TY_BOOL))
341 1983 return builder.getInt1(compileTimeValue.boolValue);
342
343 if (type.is(TY_PTR))
344 return llvm::Constant::getNullValue(builder.getPtrTy());
345
346 throw CompilerError(UNHANDLED_BRANCH, "Constant fall-through"); // GCOV_EXCL_LINE
347 }
348
349 34948 llvm::BasicBlock *IRGenerator::createBlock(const std::string &blockName /*=""*/) const {
350
2/4
✓ Branch 2 → 3 taken 34948 times.
✗ Branch 2 → 7 not taken.
✓ Branch 3 → 4 taken 34948 times.
✗ Branch 3 → 7 not taken.
34948 return llvm::BasicBlock::Create(context, blockName);
351 }
352
353 34948 void IRGenerator::switchToBlock(llvm::BasicBlock *block, llvm::Function *parentFct /*=nullptr*/) {
354
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 34948 times.
34948 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 23220 times.
✓ Branch 5 → 8 taken 11728 times.
34948 if (!parentFct)
357 23220 parentFct = builder.GetInsertBlock()->getParent();
358 // Append block to current function
359 34948 parentFct->insert(parentFct->end(), block);
360 // Set insert point to the block
361 34948 builder.SetInsertPoint(block);
362 34948 blockAlreadyTerminated = false;
363 34948 }
364
365 10391 void IRGenerator::terminateBlock(const StmtLstNode *stmtLstNode) {
366 10391 diGenerator.setSourceLocation(stmtLstNode->closingBraceCodeLoc);
367 10391 generateScopeCleanup(stmtLstNode);
368 10391 blockAlreadyTerminated = true;
369 10391 }
370
371 12851 void IRGenerator::insertJump(llvm::BasicBlock *targetBlock) {
372
2/2
✓ Branch 2 → 3 taken 3674 times.
✓ Branch 2 → 4 taken 9177 times.
12851 if (blockAlreadyTerminated)
373 3674 return;
374 9177 builder.CreateBr(targetBlock);
375 9177 blockAlreadyTerminated = true;
376 }
377
378 9642 void IRGenerator::insertCondJump(llvm::Value *condition, llvm::BasicBlock *trueBlock, llvm::BasicBlock *falseBlock,
379 Likelihood likelihood /*=UNSPECIFIED*/) {
380
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 9642 times.
9642 if (blockAlreadyTerminated)
381 return;
382 9642 llvm::BranchInst *jumpInst = builder.CreateCondBr(condition, trueBlock, falseBlock);
383 9642 blockAlreadyTerminated = true;
384
385
2/2
✓ Branch 5 → 6 taken 1222 times.
✓ Branch 5 → 7 taken 8420 times.
9642 if (likelihood != Likelihood::UNSPECIFIED)
386 1222 mdGenerator.generateBranchWeightsMetadata(jumpInst, likelihood);
387 }
388
389 11728 void IRGenerator::verifyFunction(const llvm::Function *fct, const CodeLoc &codeLoc) const {
390 // Skip the verifying step if the verifier was disabled manually or debug info is emitted
391
4/4
✓ Branch 2 → 3 taken 11727 times.
✓ Branch 2 → 4 taken 1 time.
✓ Branch 3 → 4 taken 206 times.
✓ Branch 3 → 5 taken 11521 times.
11728 if (cliOptions.disableVerifier || cliOptions.instrumentation.generateDebugInfo)
392 207 return;
393
394 // Verify function
395 11521 std::string output;
396
1/2
✓ Branch 6 → 7 taken 11521 times.
✗ Branch 6 → 21 not taken.
11521 llvm::raw_string_ostream oss(output);
397 if (llvm::verifyFunction(*fct, &oss)) // LCOV_EXCL_LINE
398 throw CompilerError(codeLoc, INVALID_FUNCTION, output); // LCOV_EXCL_LINE
399 11521 }
400
401 992 void IRGenerator::verifyModule(const CodeLoc &codeLoc) const {
402 // Skip the verifying step if the verifier was disabled manually or debug info is emitted
403
4/4
✓ Branch 2 → 3 taken 991 times.
✓ Branch 2 → 4 taken 1 time.
✓ Branch 3 → 4 taken 24 times.
✓ Branch 3 → 5 taken 967 times.
992 if (cliOptions.disableVerifier || cliOptions.instrumentation.generateDebugInfo)
404 25 return;
405
406 // Verify module
407 967 std::string output;
408
1/2
✓ Branch 6 → 7 taken 967 times.
✗ Branch 6 → 21 not taken.
967 llvm::raw_string_ostream oss(output);
409 if (llvm::verifyModule(*module, &oss)) // LCOV_EXCL_LINE
410 throw CompilerError(codeLoc, INVALID_MODULE, output); // LCOV_EXCL_LINE
411 967 }
412
413 5768 LLVMExprResult IRGenerator::doAssignment(const ASTNode *lhsNode, const ExprNode *rhsNode, const ASTNode *node) {
414 // Get entry of left side
415
2/4
✓ Branch 2 → 3 taken 5768 times.
✗ Branch 2 → 17 not taken.
✓ Branch 3 → 4 taken 5768 times.
✗ Branch 3 → 15 not taken.
5768 auto [value, constant, ptr, refPtr, entry, _] = std::any_cast<LLVMExprResult>(visit(lhsNode));
416
6/8
✓ Branch 5 → 6 taken 4930 times.
✓ Branch 5 → 10 taken 838 times.
✓ Branch 6 → 7 taken 4930 times.
✗ Branch 6 → 18 not taken.
✓ Branch 7 → 8 taken 4930 times.
✗ Branch 7 → 18 not taken.
✓ Branch 8 → 9 taken 250 times.
✓ Branch 8 → 10 taken 4680 times.
5768 llvm::Value *lhsAddress = entry != nullptr && entry->getQualType().isRef() ? refPtr : ptr;
417
1/2
✓ Branch 11 → 12 taken 5768 times.
✗ Branch 11 → 18 not taken.
11536 return doAssignment(lhsAddress, entry, rhsNode, node);
418 }
419
420 13978 LLVMExprResult IRGenerator::doAssignment(llvm::Value *lhsAddress, 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 13978 times.
✗ Branch 2 → 13 not taken.
13978 const QualType &rhsSType = rhsNode->getEvaluatedSymbolType(manIdx);
424
2/4
✓ Branch 3 → 4 taken 13978 times.
✗ Branch 3 → 12 not taken.
✓ Branch 4 → 5 taken 13978 times.
✗ Branch 4 → 10 not taken.
13978 auto rhs = std::any_cast<LLVMExprResult>(visit(rhsNode));
425
1/2
✓ Branch 6 → 7 taken 13978 times.
✗ Branch 6 → 13 not taken.
27956 return doAssignment(lhsAddress, lhsEntry, rhs, rhsSType, node, isDecl);
426 }
427
428 14096 LLVMExprResult IRGenerator::doAssignment(llvm::Value *lhsAddress, 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 13258 times.
✓ Branch 2 → 7 taken 838 times.
✓ Branch 5 → 6 taken 504 times.
✓ Branch 5 → 7 taken 12754 times.
14096 const bool isRefAssign = lhsEntry != nullptr && lhsEntry->getQualType().isRef();
432
8/10
✓ Branch 8 → 9 taken 13592 times.
✓ Branch 8 → 15 taken 504 times.
✓ Branch 9 → 10 taken 13592 times.
✗ Branch 9 → 195 not taken.
✓ Branch 10 → 11 taken 13592 times.
✗ Branch 10 → 195 not taken.
✓ Branch 11 → 12 taken 1440 times.
✓ Branch 11 → 15 taken 12152 times.
✓ Branch 13 → 14 taken 182 times.
✓ Branch 13 → 15 taken 1258 times.
14096 const bool needsCopy = !isRefAssign && rhsSType.removeReferenceWrapper().is(TY_STRUCT) && !rhs.isTemporary();
433
434
2/2
✓ Branch 16 → 17 taken 504 times.
✓ Branch 16 → 57 taken 13592 times.
14096 if (isRefAssign) {
435
1/2
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 504 times.
504 assert(lhsEntry != nullptr);
436
2/2
✓ Branch 19 → 20 taken 254 times.
✓ Branch 19 → 33 taken 250 times.
504 if (isDecl) { // Reference gets initially assigned
437 // Get address of right side
438 254 llvm::Value *rhsAddress = resolveAddress(rhs);
439
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 254 times.
254 assert(rhsAddress != nullptr);
440
441 // Store lhs pointer to rhs
442
2/4
✓ Branch 26 → 27 taken 254 times.
✗ Branch 26 → 196 not taken.
✓ Branch 27 → 28 taken 254 times.
✗ Branch 27 → 196 not taken.
254 llvm::Value *refAddress = insertAlloca(builder.getPtrTy());
443 254 lhsEntry->updateAddress(refAddress);
444 254 insertStore(rhsAddress, refAddress);
445
446 254 return LLVMExprResult{.value = rhsAddress, .ptr = refAddress, .entry = lhsEntry};
447 }
448
449 // Reference to reference assignment (only for struct fields that are not initialized yet)
450 // These are only allowed inside a ctor body. In other cases, the value of the reference gets assigned, not the ref itself.
451
6/8
✓ Branch 33 → 34 taken 173 times.
✓ Branch 33 → 40 taken 77 times.
✓ Branch 35 → 36 taken 173 times.
✗ Branch 35 → 40 not taken.
✓ Branch 36 → 37 taken 167 times.
✓ Branch 36 → 40 taken 6 times.
✓ Branch 38 → 39 taken 167 times.
✗ Branch 38 → 40 not taken.
250 const bool isInitialFieldRefAssign = isInCtorBody && rhsSType.isRef() && rhs.entry && lhsEntry->isField();
452 // Assigning the result variable
453 250 const bool isReturnValAssign = lhsEntry->name == RETURN_VARIABLE_NAME;
454
4/4
✓ Branch 42 → 43 taken 83 times.
✓ Branch 42 → 44 taken 167 times.
✓ Branch 43 → 44 taken 2 times.
✓ Branch 43 → 49 taken 81 times.
250 if (isInitialFieldRefAssign || isReturnValAssign) {
455 // Get address of right side
456 169 llvm::Value *referencedAddress = resolveAddress(rhs);
457
1/2
✗ Branch 45 → 46 not taken.
✓ Branch 45 → 47 taken 169 times.
169 assert(referencedAddress != nullptr);
458
459 // Store the rhs* to the lhs**
460 169 insertStore(referencedAddress, lhsAddress);
461
462 169 return LLVMExprResult{.value = referencedAddress, .ptr = lhsAddress, .entry = lhsEntry};
463 }
464
465 // Load referenced address
466
3/6
✓ Branch 51 → 52 taken 81 times.
✗ Branch 51 → 204 not taken.
✓ Branch 52 → 53 taken 81 times.
✗ Branch 52 → 202 not taken.
✓ Branch 53 → 54 taken 81 times.
✗ Branch 53 → 202 not taken.
162 lhsAddress = insertLoad(builder.getPtrTy(), lhsAddress);
467 }
468
469
8/8
✓ Branch 57 → 58 taken 8074 times.
✓ Branch 57 → 63 taken 5599 times.
✓ Branch 59 → 60 taken 959 times.
✓ Branch 59 → 63 taken 7115 times.
✓ Branch 61 → 62 taken 955 times.
✓ Branch 61 → 63 taken 4 times.
✓ Branch 64 → 65 taken 955 times.
✓ Branch 64 → 70 taken 12718 times.
13673 if (isDecl && rhsSType.is(TY_STRUCT) && rhs.isTemporary()) {
470
1/2
✗ Branch 65 → 66 not taken.
✓ Branch 65 → 67 taken 955 times.
955 assert(lhsEntry != nullptr);
471 // Directly set the address to the lhs entry (temp stealing)
472 955 llvm::Value *rhsAddress = resolveAddress(rhs);
473 955 lhsEntry->updateAddress(rhsAddress);
474 955 rhs.entry = lhsEntry;
475 955 return rhs;
476 }
477
478 // Allocate new memory if the lhs address does not exist
479
2/2
✓ Branch 70 → 71 taken 7089 times.
✓ Branch 70 → 81 taken 5629 times.
12718 if (!lhsAddress) {
480
1/2
✗ Branch 71 → 72 not taken.
✓ Branch 71 → 73 taken 7089 times.
7089 assert(lhsEntry != nullptr);
481
3/6
✓ Branch 75 → 76 taken 7089 times.
✗ Branch 75 → 210 not taken.
✓ Branch 76 → 77 taken 7089 times.
✗ Branch 76 → 208 not taken.
✓ Branch 77 → 78 taken 7089 times.
✗ Branch 77 → 208 not taken.
7089 lhsAddress = insertAlloca(lhsEntry->getQualType());
482 7089 lhsEntry->updateAddress(lhsAddress);
483 }
484
485 // 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
486
10/10
✓ Branch 81 → 82 taken 11880 times.
✓ Branch 81 → 90 taken 838 times.
✓ Branch 84 → 85 taken 2282 times.
✓ Branch 84 → 90 taken 9598 times.
✓ Branch 86 → 87 taken 6 times.
✓ Branch 86 → 90 taken 2276 times.
✓ Branch 88 → 89 taken 2 times.
✓ Branch 88 → 90 taken 4 times.
✓ Branch 91 → 92 taken 2 times.
✓ Branch 91 → 107 taken 12716 times.
12718 if (lhsEntry && lhsEntry->getQualType().isPtr() && rhsSType.isArray() && rhsSType.getArraySize() != ARRAY_SIZE_UNKNOWN) {
487 // Get address of right side
488
1/2
✓ Branch 92 → 93 taken 2 times.
✗ Branch 92 → 221 not taken.
2 llvm::Value *rhsAddress = resolveAddress(rhs);
489
1/2
✗ Branch 93 → 94 not taken.
✓ Branch 93 → 95 taken 2 times.
2 assert(rhsAddress != nullptr);
490
1/2
✓ Branch 95 → 96 taken 2 times.
✗ Branch 95 → 221 not taken.
2 llvm::Type *elementTy = rhsSType.toLLVMType(sourceFile);
491
2/4
✓ Branch 96 → 97 taken 2 times.
✗ Branch 96 → 221 not taken.
✓ Branch 97 → 98 taken 2 times.
✗ Branch 97 → 221 not taken.
2 llvm::Value *indices[2] = {builder.getInt64(0), builder.getInt32(0)};
492
1/2
✓ Branch 102 → 103 taken 2 times.
✗ Branch 102 → 214 not taken.
2 llvm::Value *firstItemAddress = insertInBoundsGEP(elementTy, rhsAddress, indices);
493
1/2
✓ Branch 105 → 106 taken 2 times.
✗ Branch 105 → 221 not taken.
2 insertStore(firstItemAddress, lhsAddress);
494 2 return LLVMExprResult{.value = rhsAddress, .ptr = lhsAddress, .entry = lhsEntry};
495 }
496
497 // Check if we need to copy the rhs to the lhs. This happens for structs
498
2/2
✓ Branch 107 → 108 taken 182 times.
✓ Branch 107 → 163 taken 12534 times.
12716 if (needsCopy) {
499 // Get address of right side
500
1/2
✓ Branch 108 → 109 taken 182 times.
✗ Branch 108 → 264 not taken.
182 llvm::Value *rhsAddress = resolveAddress(rhs);
501
1/2
✗ Branch 109 → 110 not taken.
✓ Branch 109 → 111 taken 182 times.
182 assert(rhsAddress != nullptr);
502
503
2/4
✓ Branch 111 → 112 taken 182 times.
✗ Branch 111 → 222 not taken.
✓ Branch 112 → 113 taken 182 times.
✗ Branch 112 → 222 not taken.
182 const QualType rhsSTypeNonRef = rhsSType.removeReferenceWrapper().toNonConst();
504
3/4
✓ Branch 113 → 114 taken 182 times.
✗ Branch 113 → 264 not taken.
✓ Branch 114 → 115 taken 105 times.
✓ Branch 114 → 131 taken 77 times.
182 if (rhsSTypeNonRef.isTriviallyCopyable(node)) {
505 // Create shallow copy
506
1/2
✓ Branch 115 → 116 taken 105 times.
✗ Branch 115 → 230 not taken.
105 llvm::Type *rhsType = rhsSTypeNonRef.toLLVMType(sourceFile);
507
6/10
✓ Branch 116 → 117 taken 100 times.
✓ Branch 116 → 118 taken 5 times.
✓ Branch 117 → 121 taken 100 times.
✗ Branch 117 → 223 not taken.
✓ Branch 120 → 121 taken 5 times.
✗ Branch 120 → 223 not taken.
✓ Branch 121 → 122 taken 5 times.
✓ Branch 121 → 124 taken 100 times.
✗ Branch 223 → 224 not taken.
✗ Branch 223 → 226 not taken.
110 const std::string copyName = lhsEntry ? lhsEntry->name : "";
508
4/6
✓ Branch 124 → 125 taken 100 times.
✓ Branch 124 → 127 taken 5 times.
✗ Branch 125 → 126 not taken.
✓ Branch 125 → 127 taken 100 times.
✓ Branch 128 → 129 taken 105 times.
✗ Branch 128 → 228 not taken.
105 generateShallowCopy(rhsAddress, rhsType, lhsAddress, lhsEntry && lhsEntry->isVolatile);
509 105 } else {
510 // Check if we have a copy ctor
511
1/2
✓ Branch 131 → 132 taken 77 times.
✗ Branch 131 → 263 not taken.
77 Scope *structScope = rhsSTypeNonRef.getBodyScope();
512
2/4
✓ Branch 132 → 133 taken 77 times.
✗ Branch 132 → 235 not taken.
✓ Branch 137 → 138 taken 77 times.
✗ Branch 137 → 231 not taken.
231 const ArgList args = {{rhsSTypeNonRef.toConstRef(node), rhs.isTemporary()}};
513
2/4
✓ Branch 141 → 142 taken 77 times.
✗ Branch 141 → 239 not taken.
✓ Branch 142 → 143 taken 77 times.
✗ Branch 142 → 237 not taken.
77 const Function *copyCtor = FunctionManager::lookup(structScope, CTOR_FUNCTION_NAME, rhsSTypeNonRef, args, true);
514
1/2
✓ Branch 145 → 146 taken 77 times.
✗ Branch 145 → 153 not taken.
77 if (copyCtor != nullptr) {
515 // Call copy ctor
516
2/4
✓ Branch 148 → 149 taken 77 times.
✗ Branch 148 → 245 not taken.
✓ Branch 149 → 150 taken 77 times.
✗ Branch 149 → 243 not taken.
231 generateCtorOrDtorCall(lhsAddress, copyCtor, {rhsAddress});
517 } else {
518 const std::string structName = rhsSTypeNonRef.getName();
519 const std::string msg = "Cannot copy struct '" + structName + "', as it is not trivially copyable and has no copy ctor";
520 throw SemanticError(node, COPY_CTOR_REQUIRED, msg);
521 }
522 77 }
523 182 return LLVMExprResult{.ptr = lhsAddress, .entry = lhsEntry};
524 }
525
526 // Optimization: If we have the address of both sides, we can do a memcpy instead of loading and storing the value
527 12534 llvm::Value *rhsValue = nullptr;
528
8/8
✓ Branch 164 → 165 taken 253 times.
✓ Branch 164 → 168 taken 12281 times.
✓ Branch 165 → 166 taken 226 times.
✓ Branch 165 → 168 taken 27 times.
✓ Branch 166 → 167 taken 223 times.
✓ Branch 166 → 168 taken 3 times.
✓ Branch 169 → 170 taken 223 times.
✓ Branch 169 → 191 taken 12311 times.
12534 if (rhsSType.is(TY_STRUCT) && rhs.value == nullptr && rhs.constant == nullptr) {
529 // Create shallow copy
530
2/4
✓ Branch 170 → 171 taken 223 times.
✗ Branch 170 → 265 not taken.
✓ Branch 171 → 172 taken 223 times.
✗ Branch 171 → 265 not taken.
223 const QualType rhsSTypeNonRef = rhsSType.removeReferenceWrapper().toNonConst();
531
1/2
✓ Branch 172 → 173 taken 223 times.
✗ Branch 172 → 273 not taken.
223 llvm::Type *rhsType = rhsSTypeNonRef.toLLVMType(sourceFile);
532
1/2
✓ Branch 173 → 174 taken 223 times.
✗ Branch 173 → 273 not taken.
223 llvm::Value *rhsAddress = resolveAddress(rhs);
533
1/2
✗ Branch 174 → 175 not taken.
✓ Branch 174 → 176 taken 223 times.
223 assert(rhsAddress != nullptr);
534
6/10
✓ Branch 176 → 177 taken 222 times.
✓ Branch 176 → 178 taken 1 time.
✓ Branch 177 → 181 taken 222 times.
✗ Branch 177 → 266 not taken.
✓ Branch 180 → 181 taken 1 time.
✗ Branch 180 → 266 not taken.
✓ Branch 181 → 182 taken 1 time.
✓ Branch 181 → 184 taken 222 times.
✗ Branch 266 → 267 not taken.
✗ Branch 266 → 269 not taken.
224 const std::string copyName = lhsEntry ? lhsEntry->name : "";
535
4/6
✓ Branch 184 → 185 taken 222 times.
✓ Branch 184 → 187 taken 1 time.
✗ Branch 185 → 186 not taken.
✓ Branch 185 → 187 taken 222 times.
✓ Branch 188 → 189 taken 223 times.
✗ Branch 188 → 271 not taken.
223 generateShallowCopy(rhsAddress, rhsType, lhsAddress, lhsEntry && lhsEntry->isVolatile);
536 223 } else {
537 // We can load the value from the right side and store it to the left side
538 // Retrieve value of the right side
539 12311 rhsValue = resolveValue(rhsSType, rhs);
540 // Store the value to the address
541 12311 insertStore(rhsValue, lhsAddress, rhsSType);
542 }
543
544 12534 return LLVMExprResult{.value = rhsValue, .ptr = lhsAddress, .entry = lhsEntry};
545 }
546
547 367 void IRGenerator::generateShallowCopy(llvm::Value *oldAddress, llvm::Type *varType, llvm::Value *targetAddress,
548 bool isVolatile) const {
549 // Retrieve size to copy
550
1/2
✓ Branch 3 → 4 taken 367 times.
✗ Branch 3 → 19 not taken.
367 const llvm::TypeSize typeSize = module->getDataLayout().getTypeAllocSize(varType);
551
552 // Create values for memcpy intrinsic
553
2/4
✓ Branch 4 → 5 taken 367 times.
✗ Branch 4 → 19 not taken.
✓ Branch 5 → 6 taken 367 times.
✗ Branch 5 → 19 not taken.
367 llvm::Value *structSize = builder.getInt64(typeSize);
554
1/2
✓ Branch 6 → 7 taken 367 times.
✗ Branch 6 → 19 not taken.
367 llvm::Value *copyVolatile = builder.getInt1(isVolatile);
555
556 // Call memcpy intrinsic to execute the shallow copy
557
1/2
✓ Branch 7 → 8 taken 367 times.
✗ Branch 7 → 19 not taken.
367 llvm::Function *memcpyFct = stdFunctionManager.getMemcpyIntrinsic();
558
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 367 times.
367 assert(targetAddress != nullptr);
559
3/6
✓ Branch 10 → 11 taken 367 times.
✗ Branch 10 → 18 not taken.
✓ Branch 12 → 13 taken 367 times.
✗ Branch 12 → 15 not taken.
✓ Branch 13 → 14 taken 367 times.
✗ Branch 13 → 15 not taken.
367 builder.CreateCall(memcpyFct, {targetAddress, oldAddress, structSize, copyVolatile});
560 367 }
561
562 23291 void IRGenerator::autoDeReferencePtr(llvm::Value *&ptr, QualType &symbolType) {
563
6/6
✓ Branch 12 → 13 taken 24223 times.
✓ Branch 12 → 15 taken 17481 times.
✓ Branch 14 → 15 taken 932 times.
✓ Branch 14 → 16 taken 23291 times.
✓ Branch 17 → 3 taken 18413 times.
✓ Branch 17 → 18 taken 23291 times.
41704 while (symbolType.isPtr() || symbolType.isRef()) {
564
2/4
✓ Branch 5 → 6 taken 18413 times.
✗ Branch 5 → 21 not taken.
✓ Branch 6 → 7 taken 18413 times.
✗ Branch 6 → 19 not taken.
18413 ptr = insertLoad(symbolType, ptr);
565
1/2
✓ Branch 9 → 10 taken 18413 times.
✗ Branch 9 → 25 not taken.
18413 symbolType = symbolType.getContained();
566 }
567 23291 }
568
569 57 llvm::GlobalVariable *IRGenerator::createGlobalConst(const std::string &baseName, llvm::Constant *constant) const {
570 // Get unused name
571
1/2
✓ Branch 2 → 3 taken 57 times.
✗ Branch 2 → 19 not taken.
57 const std::string globalName = getUnusedGlobalName(baseName);
572 // Create global
573
1/2
✓ Branch 5 → 6 taken 57 times.
✗ Branch 5 → 15 not taken.
57 module->getOrInsertGlobal(globalName, constant->getType());
574
1/2
✓ Branch 7 → 8 taken 57 times.
✗ Branch 7 → 16 not taken.
57 llvm::GlobalVariable *global = module->getNamedGlobal(globalName);
575 // Set initializer to the given constant
576
1/2
✓ Branch 8 → 9 taken 57 times.
✗ Branch 8 → 17 not taken.
57 global->setInitializer(constant);
577 57 global->setConstant(true);
578
1/2
✓ Branch 10 → 11 taken 57 times.
✗ Branch 10 → 17 not taken.
57 global->setLinkage(llvm::GlobalValue::PrivateLinkage);
579 57 global->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
580 57 return global;
581 57 }
582
583 2980 llvm::GlobalVariable *IRGenerator::createGlobalStringConst(const std::string &baseName, const std::string &value) const {
584 // Get unused name
585
1/2
✓ Branch 2 → 3 taken 2980 times.
✗ Branch 2 → 21 not taken.
2980 const std::string globalName = getUnusedGlobalName(baseName);
586 // Create global
587
2/4
✓ Branch 3 → 4 taken 2980 times.
✗ Branch 3 → 16 not taken.
✓ Branch 5 → 6 taken 2980 times.
✗ Branch 5 → 15 not taken.
2980 builder.CreateGlobalString(value, globalName, 0, module);
588
1/2
✓ Branch 7 → 8 taken 2980 times.
✗ Branch 7 → 17 not taken.
2980 llvm::GlobalVariable *global = module->getNamedGlobal(globalName);
589 // If the output should be comparable, fix alignment to 4 bytes
590
1/2
✓ Branch 8 → 9 taken 2980 times.
✗ Branch 8 → 12 not taken.
2980 if (cliOptions.comparableOutput)
591
2/4
✓ Branch 9 → 10 taken 2980 times.
✗ Branch 9 → 18 not taken.
✓ Branch 10 → 11 taken 2980 times.
✗ Branch 10 → 18 not taken.
2980 global->setAlignment(llvm::Align(4));
592 2980 return global;
593 2980 }
594
595 2980 llvm::GlobalVariable *IRGenerator::createGlobalStringConst(const std::string &baseName, const std::string &value,
596 const CodeLoc &codeLoc) const {
597 2980 llvm::GlobalVariable *global = createGlobalStringConst(baseName, value);
598 // Create debug info
599
2/2
✓ Branch 3 → 4 taken 48 times.
✓ Branch 3 → 10 taken 2932 times.
2980 if (cliOptions.instrumentation.generateDebugInfo)
600
3/6
✓ Branch 5 → 6 taken 48 times.
✗ Branch 5 → 14 not taken.
✓ Branch 6 → 7 taken 48 times.
✗ Branch 6 → 14 not taken.
✓ Branch 7 → 8 taken 48 times.
✗ Branch 7 → 12 not taken.
48 diGenerator.generateGlobalStringDebugInfo(global, global->getName().str(), value.length(), codeLoc);
601 2980 return global;
602 }
603
604 4987 std::string IRGenerator::getUnusedGlobalName(const std::string &baseName) const {
605 // Find an unused global name
606 4987 std::string globalName;
607 4987 unsigned int suffixNumber = 0;
608 do {
609
1/2
✓ Branch 5 → 6 taken 130560 times.
✗ Branch 5 → 15 not taken.
130560 globalName = baseName + std::to_string(suffixNumber);
610 130560 suffixNumber++;
611
3/4
✓ Branch 10 → 11 taken 130560 times.
✗ Branch 10 → 19 not taken.
✓ Branch 11 → 12 taken 125573 times.
✓ Branch 11 → 13 taken 4987 times.
130560 } while (module->getNamedGlobal(globalName) != nullptr);
612 4987 return globalName;
613 }
614
615 19120 void IRGenerator::materializeConstant(LLVMExprResult &exprResult) {
616 // Skip results, that do not contain a constant or already have a value
617
3/4
✓ Branch 2 → 3 taken 18719 times.
✓ Branch 2 → 4 taken 401 times.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 18719 times.
19120 if (exprResult.value != nullptr || exprResult.constant == nullptr)
618 401 return;
619
620 // Default case: the value to the constant
621 18719 exprResult.value = exprResult.constant;
622 }
623
624 1907 std::string IRGenerator::getIRString(llvm::Module *llvmModule, const CliOptions &cliOptions) {
625
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1907 times.
1907 assert(llvmModule != nullptr); // Make sure the module hasn't been moved away
626
3/4
✓ Branch 4 → 5 taken 1907 times.
✗ Branch 4 → 7 not taken.
✓ Branch 5 → 6 taken 1809 times.
✓ Branch 5 → 7 taken 98 times.
1907 const bool eliminateTarget = cliOptions.comparableOutput && cliOptions.isNativeTarget;
627
628 // Backup target triple and data layout
629
1/2
✓ Branch 9 → 10 taken 1907 times.
✗ Branch 9 → 45 not taken.
1907 const llvm::Triple targetTriple = llvmModule->getTargetTriple();
630
1/2
✓ Branch 11 → 12 taken 1907 times.
✗ Branch 11 → 43 not taken.
1907 const std::string targetDataLayout = llvmModule->getDataLayoutStr();
631 // Remove target triple and data layout
632
2/2
✓ Branch 12 → 13 taken 1809 times.
✓ Branch 12 → 19 taken 98 times.
1907 if (eliminateTarget) {
633 1809 llvmModule->setTargetTriple(llvm::Triple());
634
2/4
✓ Branch 16 → 17 taken 1809 times.
✗ Branch 16 → 34 not taken.
✓ Branch 17 → 18 taken 1809 times.
✗ Branch 17 → 34 not taken.
1809 llvmModule->setDataLayout("");
635 }
636
637 // Get IR string
638 1907 std::string output;
639
1/2
✓ Branch 20 → 21 taken 1907 times.
✗ Branch 20 → 39 not taken.
1907 llvm::raw_string_ostream oss(output);
640
1/2
✓ Branch 21 → 22 taken 1907 times.
✗ Branch 21 → 37 not taken.
1907 llvmModule->print(oss, nullptr);
641
642 // Restore target triple and data layout
643
2/2
✓ Branch 22 → 23 taken 1809 times.
✓ Branch 22 → 29 taken 98 times.
1907 if (eliminateTarget) {
644
1/2
✓ Branch 23 → 24 taken 1809 times.
✗ Branch 23 → 35 not taken.
1809 llvmModule->setTargetTriple(targetTriple);
645
1/2
✓ Branch 27 → 28 taken 1809 times.
✗ Branch 27 → 36 not taken.
1809 llvmModule->setDataLayout(targetDataLayout);
646 }
647
648 1907 return output;
649 1907 }
650
651 /**
652 * Returns the operator function list for the current manifestation and the given node
653 *
654 * @param node Node to retrieve the op fct pointer list from
655 * @return Op fct pointer list
656 */
657 19910 const std::vector<const Function *> &IRGenerator::getOpFctPointers(const ASTNode *node) const {
658
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 19910 times.
19910 assert(node->getOpFctPointers()->size() > manIdx);
659 19910 return node->getOpFctPointers()->at(manIdx);
660 }
661
662 } // namespace spice::compiler
663