GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 96.6% 371 / 6 / 390
Functions: 95.5% 42 / 0 / 44
Branches: 61.4% 435 / 20 / 728

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 <symboltablebuilder/SymbolTableBuilder.h>
9 #include <typechecker/FunctionManager.h>
10
11 #include <llvm/IR/Module.h>
12 #include <llvm/IR/Verifier.h>
13
14 namespace spice::compiler {
15
16 1055 IRGenerator::IRGenerator(GlobalResourceManager &resourceManager, SourceFile *sourceFile)
17 1055 : CompilerPass(resourceManager, sourceFile), context(cliOptions.useLTO ? resourceManager.ltoContext : sourceFile->context),
18
1/2
✓ Branch 8 → 9 taken 1055 times.
✗ Branch 8 → 65 not taken.
1055 builder(sourceFile->builder), module(sourceFile->llvmModule.get()), conversionManager(sourceFile, this),
19
6/10
✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 6 taken 1053 times.
✓ Branch 9 → 10 taken 1055 times.
✗ Branch 9 → 65 not taken.
✓ Branch 10 → 11 taken 1055 times.
✗ Branch 10 → 65 not taken.
✓ Branch 11 → 12 taken 1055 times.
✗ Branch 11 → 63 not taken.
✓ Branch 15 → 16 taken 1055 times.
✗ Branch 15 → 59 not taken.
2110 stdFunctionManager(sourceFile, resourceManager, module) {
20 // Attach information to the module
21
1/2
✓ Branch 17 → 18 taken 1055 times.
✗ Branch 17 → 44 not taken.
1055 module->setTargetTriple(cliOptions.targetTriple);
22
2/4
✓ Branch 21 → 22 taken 1055 times.
✗ Branch 21 → 47 not taken.
✓ Branch 22 → 23 taken 1055 times.
✗ Branch 22 → 45 not taken.
1055 module->setDataLayout(sourceFile->targetMachine->createDataLayout());
23
2/2
✓ Branch 24 → 25 taken 1 time.
✓ Branch 24 → 27 taken 1054 times.
1055 if (cliOptions.outputContainer == OutputContainer::SHARED_LIBRARY) {
24
1/2
✓ Branch 25 → 26 taken 1 time.
✗ Branch 25 → 55 not taken.
1 module->setPICLevel(llvm::PICLevel::SmallPIC);
25
1/2
✓ Branch 26 → 29 taken 1 time.
✗ Branch 26 → 55 not taken.
1 module->setPIELevel(llvm::PIELevel::Default);
26 } else {
27
1/2
✓ Branch 27 → 28 taken 1054 times.
✗ Branch 27 → 55 not taken.
1054 module->setPICLevel(llvm::PICLevel::BigPIC);
28
1/2
✓ Branch 28 → 29 taken 1054 times.
✗ Branch 28 → 55 not taken.
1054 module->setPIELevel(llvm::PIELevel::Large);
29 }
30
1/2
✓ Branch 29 → 30 taken 1055 times.
✗ Branch 29 → 55 not taken.
1055 module->setUwtable(llvm::UWTableKind::Default);
31
1/2
✓ Branch 30 → 31 taken 1055 times.
✗ Branch 30 → 55 not taken.
1055 module->setFramePointer(llvm::FramePointerKind::All);
32
33 // Add module identifier metadata
34
2/4
✓ Branch 31 → 32 taken 1055 times.
✗ Branch 31 → 48 not taken.
✓ Branch 32 → 33 taken 1055 times.
✗ Branch 32 → 48 not taken.
1055 llvm::NamedMDNode *identifierMetadata = module->getOrInsertNamedMetadata("llvm.ident");
35
3/6
✓ Branch 34 → 35 taken 1055 times.
✗ Branch 34 → 49 not taken.
✓ Branch 36 → 37 taken 1055 times.
✗ Branch 36 → 49 not taken.
✓ Branch 37 → 38 taken 1055 times.
✗ Branch 37 → 49 not taken.
1055 identifierMetadata->addOperand(llvm::MDNode::get(context, llvm::MDString::get(context, PRODUCER_STRING)));
36
37 // Initialize debug info generator
38
2/2
✓ Branch 38 → 39 taken 24 times.
✓ Branch 38 → 43 taken 1031 times.
1055 if (cliOptions.instrumentation.generateDebugInfo)
39
2/4
✓ Branch 39 → 40 taken 24 times.
✗ Branch 39 → 54 not taken.
✓ Branch 40 → 41 taken 24 times.
✗ Branch 40 → 52 not taken.
24 diGenerator.initialize(sourceFile->fileName, sourceFile->fileDir);
40 1055 }
41
42 1055 std::any IRGenerator::visitEntry(const EntryNode *node) {
43 // Generate IR
44
1/2
✓ Branch 2 → 3 taken 1055 times.
✗ Branch 2 → 19 not taken.
1055 visitChildren(node);
45
46 // Generate test main if required
47
4/4
✓ Branch 4 → 5 taken 277 times.
✓ Branch 4 → 7 taken 778 times.
✓ Branch 5 → 6 taken 4 times.
✓ Branch 5 → 7 taken 273 times.
1055 if (sourceFile->isMainFile && cliOptions.generateTestMain)
48 4 generateTestMain();
49
50 // Execute deferred VTable initializations
51
2/2
✓ Branch 13 → 9 taken 440 times.
✓ Branch 13 → 14 taken 1055 times.
1495 for (DeferredLogic &deferredVTableInit : deferredVTableInitializations)
52
1/2
✓ Branch 10 → 11 taken 440 times.
✗ Branch 10 → 20 not taken.
440 deferredVTableInit.execute();
53
54 // Finalize debug info generator
55 1055 diGenerator.finalize();
56
57 // Verify module
58 1055 verifyModule(node->codeLoc);
59
60
1/2
✓ Branch 16 → 17 taken 1055 times.
✗ Branch 16 → 21 not taken.
1055 return nullptr;
61 }
62
63 43694 llvm::AllocaInst *IRGenerator::insertAlloca(llvm::Type *llvmType, std::string varName) {
64
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 43694 times.
43694 if (!cliOptions.namesForIRValues)
65 varName = "";
66
67
2/2
✓ Branch 4 → 5 taken 30975 times.
✓ Branch 4 → 12 taken 12719 times.
43694 if (allocaInsertInst != nullptr) { // If there is already an alloca inst, insert right after that
68
2/4
✓ Branch 5 → 6 taken 30975 times.
✗ Branch 5 → 25 not taken.
✓ Branch 6 → 7 taken 30975 times.
✗ Branch 6 → 25 not taken.
30975 llvm::AllocaInst *allocaInst = builder.CreateAlloca(llvmType, nullptr, varName);
69
1/2
✓ Branch 8 → 9 taken 30975 times.
✗ Branch 8 → 26 not taken.
30975 allocaInst->setDebugLoc(llvm::DebugLoc());
70 30975 allocaInst->moveAfter(allocaInsertInst);
71 30975 allocaInsertInst = allocaInst;
72 } else { // This is the first alloca inst in the current function -> insert at the entry block
73 // Save current basic block and move insert cursor to entry block of the current function
74 12719 llvm::BasicBlock *currentBlock = builder.GetInsertBlock();
75 12719 builder.SetInsertPoint(allocaInsertBlock, allocaInsertBlock->begin());
76
77 // Allocate the size of the given LLVM type
78
2/4
✓ Branch 15 → 16 taken 12719 times.
✗ Branch 15 → 29 not taken.
✓ Branch 16 → 17 taken 12719 times.
✗ Branch 16 → 29 not taken.
12719 allocaInsertInst = builder.CreateAlloca(llvmType, nullptr, varName);
79
1/2
✓ Branch 18 → 19 taken 12719 times.
✗ Branch 18 → 30 not taken.
12719 allocaInsertInst->setDebugLoc(llvm::DebugLoc());
80
81 // Restore old basic block
82 12719 builder.SetInsertPoint(currentBlock);
83 }
84
85 // Insert lifetime start marker
86
2/2
✓ Branch 21 → 22 taken 54 times.
✓ Branch 21 → 23 taken 43640 times.
43694 if (cliOptions.useLifetimeMarkers)
87 54 builder.CreateLifetimeStart(allocaInsertInst);
88
89 43694 return allocaInsertInst;
90 }
91
92 38011 llvm::AllocaInst *IRGenerator::insertAlloca(const QualType &qualType, const std::string &varName) {
93 38011 llvm::Type *llvmType = qualType.toLLVMType(sourceFile);
94
2/4
✓ Branch 3 → 4 taken 38011 times.
✗ Branch 3 → 12 not taken.
✓ Branch 4 → 5 taken 38011 times.
✗ Branch 4 → 10 not taken.
38011 llvm::AllocaInst *alloca = insertAlloca(llvmType, varName);
95
96 // Insert type metadata
97
2/2
✓ Branch 6 → 7 taken 9 times.
✓ Branch 6 → 8 taken 38002 times.
38011 if (cliOptions.useTBAAMetadata)
98 9 mdGenerator.generateTypeMetadata(allocaInsertInst, qualType);
99
100 38011 return alloca;
101 }
102
103 84819 llvm::LoadInst *IRGenerator::insertLoad(llvm::Type *llvmType, llvm::Value *ptr, bool isVolatile,
104 const std::string &varName) const {
105
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 84819 times.
84819 assert(ptr->getType()->isPointerTy());
106
5/14
✓ Branch 6 → 7 taken 84819 times.
✗ Branch 6 → 8 not taken.
✓ Branch 7 → 11 taken 84819 times.
✗ Branch 7 → 22 not taken.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 22 not taken.
✓ Branch 11 → 12 taken 84819 times.
✗ Branch 11 → 20 not taken.
✓ Branch 12 → 13 taken 84819 times.
✗ Branch 12 → 20 not taken.
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 18 taken 84819 times.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 25 not taken.
84819 return builder.CreateLoad(llvmType, ptr, isVolatile, cliOptions.namesForIRValues ? varName : "");
107 }
108
109 75632 llvm::LoadInst *IRGenerator::insertLoad(const QualType &qualType, llvm::Value *ptr, bool isVolatile, const std::string &varName) {
110 75632 llvm::Type *llvmType = qualType.toLLVMType(sourceFile);
111 75632 llvm::LoadInst *load = insertLoad(llvmType, ptr, isVolatile, varName);
112
2/2
✓ Branch 4 → 5 taken 6 times.
✓ Branch 4 → 6 taken 75626 times.
75632 if (cliOptions.useTBAAMetadata)
113 6 mdGenerator.generateTBAAMetadata(load, qualType);
114 75632 return load;
115 }
116
117 45366 llvm::StoreInst *IRGenerator::insertStore(llvm::Value *val, llvm::Value *ptr, bool isVolatile) const {
118
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 45366 times.
45366 assert(ptr->getType()->isPointerTy());
119 45366 return builder.CreateStore(val, ptr, isVolatile);
120 }
121
122 13865 void IRGenerator::insertStore(llvm::Value *val, llvm::Value *ptr, const QualType &qualType, bool isVolatile) {
123 13865 llvm::StoreInst *store = insertStore(val, ptr, isVolatile);
124
2/2
✓ Branch 3 → 4 taken 9 times.
✓ Branch 3 → 5 taken 13856 times.
13865 if (cliOptions.useTBAAMetadata)
125 9 mdGenerator.generateTBAAMetadata(store, qualType);
126 13865 }
127
128 22174 llvm::Value *IRGenerator::insertInBoundsGEP(llvm::Type *type, llvm::Value *basePtr, llvm::ArrayRef<llvm::Value *> indices,
129 std::string varName) const {
130
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 22174 times.
22174 assert(basePtr->getType()->isPointerTy());
131
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 22174 times.
22174 assert(!indices.empty());
132
4/6
✓ Branch 4 → 5 taken 21816 times.
✓ Branch 4 → 7 taken 19523 times.
✓ Branch 6 → 7 taken 21816 times.
✗ Branch 6 → 8 not taken.
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 22174 times.
63513 assert(std::ranges::all_of(indices, [](const llvm::Value *index) {
133 const llvm::Type *indexType = index->getType();
134 return indexType->isIntegerTy(32) || indexType->isIntegerTy(64);
135 }));
136
137
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 22174 times.
22174 if (!cliOptions.namesForIRValues)
138 varName.clear();
139
140 // Insert GEP
141
2/4
✓ Branch 14 → 15 taken 22174 times.
✗ Branch 14 → 19 not taken.
✓ Branch 15 → 16 taken 22174 times.
✗ Branch 15 → 19 not taken.
22174 return builder.CreateInBoundsGEP(type, basePtr, indices, varName);
142 }
143
144 6014 llvm::Value *IRGenerator::insertStructGEP(llvm::Type *type, llvm::Value *basePtr, unsigned int index, std::string varName) const {
145
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 6014 times.
6014 assert(basePtr->getType()->isPointerTy());
146
147
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 6014 times.
6014 if (!cliOptions.namesForIRValues)
148 varName.clear();
149
150 // If we use index 0 we can use the base pointer directly
151
2/2
✓ Branch 8 → 9 taken 2067 times.
✓ Branch 8 → 10 taken 3947 times.
6014 if (index == 0)
152 2067 return basePtr;
153
154 // Insert GEP
155
2/4
✓ Branch 10 → 11 taken 3947 times.
✗ Branch 10 → 15 not taken.
✓ Branch 11 → 12 taken 3947 times.
✗ Branch 11 → 15 not taken.
3947 return builder.CreateStructGEP(type, basePtr, index, varName);
156 }
157
158 50620 llvm::Value *IRGenerator::resolveValue(const ExprNode *node) {
159 // Visit the given AST node
160
2/4
✓ Branch 2 → 3 taken 50620 times.
✗ Branch 2 → 11 not taken.
✓ Branch 3 → 4 taken 50620 times.
✗ Branch 3 → 9 not taken.
50620 auto exprResult = any_cast<LLVMExprResult>(visit(node));
161
1/2
✓ Branch 5 → 6 taken 50620 times.
✗ Branch 5 → 12 not taken.
101240 return resolveValue(node, exprResult);
162 }
163
164 56597 llvm::Value *IRGenerator::resolveValue(const ExprNode *node, LLVMExprResult &exprResult) {
165 56597 return resolveValue(node->getEvaluatedSymbolType(manIdx), exprResult);
166 }
167
168 110567 llvm::Value *IRGenerator::resolveValue(const QualType &qualType, LLVMExprResult &exprResult) {
169 // Check if the value is already present
170
2/2
✓ Branch 2 → 3 taken 35032 times.
✓ Branch 2 → 4 taken 75535 times.
110567 if (exprResult.value != nullptr)
171 35032 return exprResult.value;
172
173 // Check if a constant is present
174
2/2
✓ Branch 4 → 5 taken 19794 times.
✓ Branch 4 → 7 taken 55741 times.
75535 if (exprResult.constant != nullptr) {
175 19794 materializeConstant(exprResult);
176 19794 return exprResult.value;
177 }
178
179
3/4
✓ Branch 7 → 8 taken 653 times.
✓ Branch 7 → 10 taken 55088 times.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 653 times.
55741 assert(exprResult.ptr != nullptr || exprResult.refPtr != nullptr);
180
181 // De-reference if reference type
182
4/4
✓ Branch 10 → 11 taken 51980 times.
✓ Branch 10 → 13 taken 3761 times.
✓ Branch 11 → 12 taken 7 times.
✓ Branch 11 → 13 taken 51973 times.
55741 const bool isVolatile = exprResult.entry && exprResult.entry->isVolatile;
183
4/4
✓ Branch 14 → 15 taken 663 times.
✓ Branch 14 → 24 taken 55078 times.
✓ Branch 15 → 16 taken 653 times.
✓ Branch 15 → 24 taken 10 times.
55741 if (exprResult.refPtr != nullptr && exprResult.ptr == nullptr)
184
3/6
✓ Branch 18 → 19 taken 653 times.
✗ Branch 18 → 36 not taken.
✓ Branch 19 → 20 taken 653 times.
✗ Branch 19 → 34 not taken.
✓ Branch 20 → 21 taken 653 times.
✗ Branch 20 → 34 not taken.
1306 exprResult.ptr = insertLoad(builder.getPtrTy(), exprResult.refPtr, isVolatile);
185
186 // Load the value from the pointer
187
1/2
✓ Branch 24 → 25 taken 55741 times.
✗ Branch 24 → 46 not taken.
55741 const QualType referencedType = qualType.removeReferenceWrapper();
188
2/4
✓ Branch 27 → 28 taken 55741 times.
✗ Branch 27 → 42 not taken.
✓ Branch 28 → 29 taken 55741 times.
✗ Branch 28 → 40 not taken.
55741 exprResult.value = insertLoad(referencedType, exprResult.ptr, isVolatile);
189
190 55741 return exprResult.value;
191 }
192
193 3274 llvm::Value *IRGenerator::resolveAddress(const ASTNode *node) {
194 // Visit the given AST node
195
2/4
✓ Branch 2 → 3 taken 3274 times.
✗ Branch 2 → 11 not taken.
✓ Branch 3 → 4 taken 3274 times.
✗ Branch 3 → 9 not taken.
3274 auto exprResult = any_cast<LLVMExprResult>(visit(node));
196
1/2
✓ Branch 5 → 6 taken 3274 times.
✗ Branch 5 → 12 not taken.
6548 return resolveAddress(exprResult);
197 }
198
199 31776 llvm::Value *IRGenerator::resolveAddress(LLVMExprResult &exprResult) {
200 // Check if an address is already present
201
2/2
✓ Branch 2 → 3 taken 26534 times.
✓ Branch 2 → 4 taken 5242 times.
31776 if (exprResult.ptr != nullptr)
202 26534 return exprResult.ptr;
203
204 // Check if the reference address is already present
205
3/4
✓ Branch 4 → 5 taken 4200 times.
✓ Branch 4 → 7 taken 1042 times.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 4200 times.
5242 const bool isVolatile = exprResult.entry && exprResult.entry->isVolatile;
206
3/4
✓ Branch 8 → 9 taken 4430 times.
✓ Branch 8 → 18 taken 812 times.
✓ Branch 9 → 10 taken 4430 times.
✗ Branch 9 → 18 not taken.
5242 if (exprResult.refPtr != nullptr && exprResult.ptr == nullptr) {
207
3/6
✓ Branch 12 → 13 taken 4430 times.
✗ Branch 12 → 37 not taken.
✓ Branch 13 → 14 taken 4430 times.
✗ Branch 13 → 35 not taken.
✓ Branch 14 → 15 taken 4430 times.
✗ Branch 14 → 35 not taken.
4430 exprResult.ptr = insertLoad(builder.getPtrTy(), exprResult.refPtr, isVolatile);
208 4430 return exprResult.ptr;
209 }
210
211 // If not, store the value or constant
212 812 materializeConstant(exprResult);
213
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 812 times.
812 assert(exprResult.value != nullptr);
214
4/12
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 812 times.
✗ Branch 22 → 26 not taken.
✗ Branch 22 → 43 not taken.
✓ Branch 25 → 26 taken 812 times.
✗ Branch 25 → 43 not taken.
✓ Branch 27 → 28 taken 812 times.
✗ Branch 27 → 41 not taken.
✓ Branch 29 → 30 taken 812 times.
✗ Branch 29 → 32 not taken.
✗ Branch 43 → 44 not taken.
✗ Branch 43 → 46 not taken.
1624 exprResult.ptr = insertAlloca(exprResult.value->getType(), exprResult.entry ? exprResult.entry->name : "");
215 812 insertStore(exprResult.value, exprResult.ptr, isVolatile);
216
217 812 return exprResult.ptr;
218 }
219
220 2899 llvm::Constant *IRGenerator::getDefaultValueForSymbolType(const QualType &symbolType) { // NOLINT(misc-no-recursion)
221 // Double
222
2/2
✓ Branch 3 → 4 taken 4 times.
✓ Branch 3 → 9 taken 2895 times.
2899 if (symbolType.is(TY_DOUBLE))
223
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));
224
225 // Int
226
2/2
✓ Branch 10 → 11 taken 409 times.
✓ Branch 10 → 13 taken 2486 times.
2895 if (symbolType.is(TY_INT))
227 409 return builder.getInt32(0);
228
229 // Short
230
2/2
✓ Branch 14 → 15 taken 8 times.
✓ Branch 14 → 17 taken 2478 times.
2486 if (symbolType.is(TY_SHORT))
231 8 return builder.getInt16(0);
232
233 // Long
234
2/2
✓ Branch 18 → 19 taken 1058 times.
✓ Branch 18 → 21 taken 1420 times.
2478 if (symbolType.is(TY_LONG))
235 1058 return builder.getInt64(0);
236
237 // Byte or char
238
3/4
✓ Branch 21 → 22 taken 1420 times.
✗ Branch 21 → 133 not taken.
✓ Branch 22 → 23 taken 22 times.
✓ Branch 22 → 25 taken 1398 times.
1420 if (symbolType.isOneOf({TY_BYTE, TY_CHAR}))
239 22 return builder.getInt8(0);
240
241 // String
242
2/2
✓ Branch 26 → 27 taken 425 times.
✓ Branch 26 → 35 taken 973 times.
1398 if (symbolType.is(TY_STRING)) {
243
3/6
✓ Branch 27 → 28 taken 425 times.
✗ Branch 27 → 135 not taken.
✓ Branch 28 → 29 taken 425 times.
✗ Branch 28 → 134 not taken.
✓ Branch 29 → 30 taken 425 times.
✗ Branch 29 → 134 not taken.
425 llvm::GlobalVariable *globalString = builder.CreateGlobalString("", "");
244
1/2
✓ Branch 30 → 31 taken 425 times.
✗ Branch 30 → 34 not taken.
425 if (cliOptions.comparableOutput)
245
2/4
✓ Branch 31 → 32 taken 425 times.
✗ Branch 31 → 136 not taken.
✓ Branch 32 → 33 taken 425 times.
✗ Branch 32 → 136 not taken.
425 globalString->setAlignment(llvm::Align(4));
246 425 return globalString;
247 }
248
249 // Bool
250
2/2
✓ Branch 36 → 37 taken 44 times.
✓ Branch 36 → 39 taken 929 times.
973 if (symbolType.is(TY_BOOL))
251 44 return builder.getFalse();
252
253 // Pointer or reference
254
3/4
✓ Branch 39 → 40 taken 929 times.
✗ Branch 39 → 137 not taken.
✓ Branch 40 → 41 taken 874 times.
✓ Branch 40 → 44 taken 55 times.
929 if (symbolType.isOneOf({TY_PTR, TY_REF}))
255 874 return llvm::Constant::getNullValue(builder.getPtrTy());
256
257 // Array
258
2/2
✓ Branch 45 → 46 taken 14 times.
✓ Branch 45 → 61 taken 41 times.
55 if (symbolType.isArray()) {
259 // Get array size
260
1/2
✓ Branch 46 → 47 taken 14 times.
✗ Branch 46 → 146 not taken.
14 const size_t arraySize = symbolType.getArraySize();
261
262 // Get default value for item
263
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());
264
265 // Retrieve array and item type
266
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);
267
1/2
✓ Branch 51 → 52 taken 14 times.
✗ Branch 51 → 146 not taken.
14 llvm::ArrayType *arrayType = llvm::ArrayType::get(itemType, arraySize);
268
269 // Create a constant array with n times the default value
270
1/2
✓ Branch 54 → 55 taken 14 times.
✗ Branch 54 → 140 not taken.
14 const std::vector itemConstants(arraySize, defaultItemValue);
271
1/2
✓ Branch 57 → 58 taken 14 times.
✗ Branch 57 → 143 not taken.
14 return llvm::ConstantArray::get(arrayType, itemConstants);
272 14 }
273
274 // Function or procedure
275
3/4
✓ Branch 61 → 62 taken 41 times.
✗ Branch 61 → 147 not taken.
✓ Branch 62 → 63 taken 14 times.
✓ Branch 62 → 75 taken 27 times.
41 if (symbolType.isOneOf({TY_FUNCTION, TY_PROCEDURE})) {
276
2/2
✓ Branch 63 → 64 taken 7 times.
✓ Branch 63 → 69 taken 7 times.
14 if (!llvmTypes.fatPtrType)
277
3/6
✓ Branch 64 → 65 taken 7 times.
✗ Branch 64 → 148 not taken.
✓ Branch 65 → 66 taken 7 times.
✗ Branch 65 → 148 not taken.
✓ Branch 67 → 68 taken 7 times.
✗ Branch 67 → 148 not taken.
7 llvmTypes.fatPtrType = llvm::StructType::get(context, {builder.getPtrTy(), builder.getPtrTy()});
278
279
2/4
✓ Branch 69 → 70 taken 14 times.
✗ Branch 69 → 150 not taken.
✓ Branch 70 → 71 taken 14 times.
✗ Branch 70 → 150 not taken.
14 llvm::Constant *ptrDefaultValue = getDefaultValueForSymbolType(QualType(TY_PTR));
280
1/2
✓ Branch 72 → 73 taken 14 times.
✗ Branch 72 → 151 not taken.
14 return llvm::ConstantStruct::get(llvmTypes.fatPtrType, {ptrDefaultValue, ptrDefaultValue});
281 }
282
283 // Struct
284
2/2
✓ Branch 76 → 77 taken 26 times.
✓ Branch 76 → 114 taken 1 time.
27 if (symbolType.is(TY_STRUCT)) {
285 // Retrieve field count
286
1/2
✓ Branch 77 → 78 taken 26 times.
✗ Branch 77 → 158 not taken.
26 Scope *structScope = symbolType.getBodyScope();
287
1/2
✗ Branch 78 → 79 not taken.
✓ Branch 78 → 80 taken 26 times.
26 assert(structScope != nullptr);
288
1/2
✓ Branch 80 → 81 taken 26 times.
✗ Branch 80 → 158 not taken.
26 const size_t fieldCount = structScope->getFieldCount();
289
290 // Get default values for all fields of the struct
291 26 std::vector<llvm::Constant *> fieldConstants;
292
1/2
✓ Branch 81 → 82 taken 26 times.
✗ Branch 81 → 156 not taken.
26 fieldConstants.reserve(fieldCount);
293
294 // Add default value for each struct field
295
2/2
✓ Branch 107 → 83 taken 63 times.
✓ Branch 107 → 108 taken 26 times.
89 for (size_t i = 0; i < fieldCount; i++) {
296 // Get entry of the field
297
1/2
✗ Branch 83 → 84 not taken.
✓ Branch 83 → 85 taken 63 times.
63 const SymbolTableEntry *fieldEntry = structScope->lookupField(i);
298
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());
299
300 // Retrieve default field value
301 llvm::Constant *defaultFieldValue;
302
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)
303
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(manIdx), fieldEntry->getQualType(), fieldNode);
304 else
305
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());
306
307
1/2
✓ Branch 105 → 106 taken 63 times.
✗ Branch 105 → 154 not taken.
63 fieldConstants.push_back(defaultFieldValue);
308 }
309
310
1/2
✓ Branch 108 → 109 taken 26 times.
✗ Branch 108 → 156 not taken.
26 const auto structType = reinterpret_cast<llvm::StructType *>(symbolType.toLLVMType(sourceFile));
311
1/2
✓ Branch 110 → 111 taken 26 times.
✗ Branch 110 → 155 not taken.
26 return llvm::ConstantStruct::get(structType, fieldConstants);
312 26 }
313
314 // Interface
315
1/2
✓ Branch 115 → 116 taken 1 time.
✗ Branch 115 → 121 not taken.
1 if (symbolType.is(TY_INTERFACE)) {
316 1 const auto structType = reinterpret_cast<llvm::StructType *>(symbolType.toLLVMType(sourceFile));
317 1 return llvm::ConstantStruct::get(structType, llvm::Constant::getNullValue(builder.getPtrTy()));
318 }
319
320 throw CompilerError(INTERNAL_ERROR, "Cannot determine default value for symbol type"); // GCOV_EXCL_LINE
321 }
322
323 19900 llvm::Constant *IRGenerator::getConst(const CompileTimeValue &compileTimeValue, const QualType &type, const ASTNode *node) const {
324
2/2
✓ Branch 3 → 4 taken 665 times.
✓ Branch 3 → 9 taken 19235 times.
19900 if (type.is(TY_DOUBLE))
325
2/4
✓ Branch 4 → 5 taken 665 times.
✗ Branch 4 → 56 not taken.
✓ Branch 5 → 6 taken 665 times.
✗ Branch 5 → 54 not taken.
665 return llvm::ConstantFP::get(context, llvm::APFloat(compileTimeValue.doubleValue));
326
327
2/2
✓ Branch 10 → 11 taken 3935 times.
✓ Branch 10 → 13 taken 15300 times.
19235 if (type.is(TY_INT))
328 3935 return builder.getInt32(compileTimeValue.intValue);
329
330
2/2
✓ Branch 14 → 15 taken 886 times.
✓ Branch 14 → 17 taken 14414 times.
15300 if (type.is(TY_SHORT))
331 886 return builder.getInt16(compileTimeValue.shortValue);
332
333
2/2
✓ Branch 18 → 19 taken 7425 times.
✓ Branch 18 → 21 taken 6989 times.
14414 if (type.is(TY_LONG))
334 7425 return builder.getInt64(compileTimeValue.longValue);
335
336
3/4
✓ Branch 21 → 22 taken 6989 times.
✗ Branch 21 → 57 not taken.
✓ Branch 22 → 23 taken 2675 times.
✓ Branch 22 → 25 taken 4314 times.
6989 if (type.isOneOf({TY_BYTE, TY_CHAR}))
337 2675 return builder.getInt8(compileTimeValue.charValue);
338
339
2/2
✓ Branch 26 → 27 taken 2221 times.
✓ Branch 26 → 36 taken 2093 times.
4314 if (type.is(TY_STRING)) {
340 2221 const std::string &stringValue = resourceManager.compileTimeStringValues.at(compileTimeValue.stringValueOffset);
341
2/4
✓ Branch 30 → 31 taken 2221 times.
✗ Branch 30 → 60 not taken.
✓ Branch 31 → 32 taken 2221 times.
✗ Branch 31 → 58 not taken.
6663 return createGlobalStringConst(ANON_GLOBAL_STRING_NAME, stringValue, node->codeLoc);
342 }
343
344
1/2
✓ Branch 37 → 38 taken 2093 times.
✗ Branch 37 → 40 not taken.
2093 if (type.is(TY_BOOL))
345 2093 return builder.getInt1(compileTimeValue.boolValue);
346
347 if (type.is(TY_PTR))
348 return llvm::Constant::getNullValue(builder.getPtrTy());
349
350 throw CompilerError(UNHANDLED_BRANCH, "Constant fall-through"); // GCOV_EXCL_LINE
351 }
352
353 37398 llvm::BasicBlock *IRGenerator::createBlock(const std::string &blockName /*=""*/) const {
354
2/4
✓ Branch 2 → 3 taken 37398 times.
✗ Branch 2 → 7 not taken.
✓ Branch 3 → 4 taken 37398 times.
✗ Branch 3 → 7 not taken.
37398 return llvm::BasicBlock::Create(context, blockName);
355 }
356
357 37398 void IRGenerator::switchToBlock(llvm::BasicBlock *block, llvm::Function *parentFct /*=nullptr*/) {
358
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 37398 times.
37398 assert(block->getParent() == nullptr); // Ensure that the block was not added to a function already
359 // If no parent function were passed, use the current function
360
2/2
✓ Branch 5 → 6 taken 24622 times.
✓ Branch 5 → 8 taken 12776 times.
37398 if (!parentFct)
361 24622 parentFct = builder.GetInsertBlock()->getParent();
362 // Append block to current function
363 37398 parentFct->insert(parentFct->end(), block);
364 // Set insert point to the block
365 37398 builder.SetInsertPoint(block);
366 37398 blockAlreadyTerminated = false;
367 37398 }
368
369 11155 void IRGenerator::terminateBlock(const StmtLstNode *stmtLstNode) {
370 11155 diGenerator.setSourceLocation(stmtLstNode->closingBraceCodeLoc);
371 11155 generateScopeCleanup(stmtLstNode);
372 11155 blockAlreadyTerminated = true;
373 11155 }
374
375 13655 void IRGenerator::insertJump(llvm::BasicBlock *targetBlock) {
376
2/2
✓ Branch 2 → 3 taken 3856 times.
✓ Branch 2 → 4 taken 9799 times.
13655 if (blockAlreadyTerminated)
377 3856 return;
378 9799 builder.CreateBr(targetBlock);
379 9799 blockAlreadyTerminated = true;
380 }
381
382 10208 void IRGenerator::insertCondJump(llvm::Value *condition, llvm::BasicBlock *trueBlock, llvm::BasicBlock *falseBlock,
383 Likelihood likelihood /*=UNSPECIFIED*/) {
384
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 10208 times.
10208 if (blockAlreadyTerminated)
385 return;
386 10208 llvm::BranchInst *jumpInst = builder.CreateCondBr(condition, trueBlock, falseBlock);
387 10208 blockAlreadyTerminated = true;
388
389
2/2
✓ Branch 5 → 6 taken 1288 times.
✓ Branch 5 → 7 taken 8920 times.
10208 if (likelihood != Likelihood::UNSPECIFIED)
390 1288 mdGenerator.generateBranchWeightsMetadata(jumpInst, likelihood);
391 }
392
393 12776 void IRGenerator::verifyFunction(const llvm::Function *fct, const CodeLoc &codeLoc) const {
394 // Skip the verifying step if the verifier was disabled manually or debug info is emitted
395
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 12776 times.
12776 if (cliOptions.disableVerifier)
396 return;
397
398 // Verify function
399 12776 std::string output;
400
1/2
✓ Branch 5 → 6 taken 12776 times.
✗ Branch 5 → 20 not taken.
12776 llvm::raw_string_ostream oss(output);
401 if (llvm::verifyFunction(*fct, &oss)) // LCOV_EXCL_LINE
402 throw CompilerError(codeLoc, INVALID_FUNCTION, output); // LCOV_EXCL_LINE
403 12776 }
404
405 1055 void IRGenerator::verifyModule(const CodeLoc &codeLoc) const {
406 // Skip the verifying step if the verifier was disabled manually or debug info is emitted
407
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1055 times.
1055 if (cliOptions.disableVerifier)
408 return;
409
410 // Verify module
411 1055 std::string output;
412
1/2
✓ Branch 5 → 6 taken 1055 times.
✗ Branch 5 → 20 not taken.
1055 llvm::raw_string_ostream oss(output);
413 if (llvm::verifyModule(*module, &oss)) // LCOV_EXCL_LINE
414 throw CompilerError(codeLoc, INVALID_MODULE, output); // LCOV_EXCL_LINE
415 1055 }
416
417 6487 LLVMExprResult IRGenerator::doAssignment(const ASTNode *lhsNode, const ExprNode *rhsNode, const ASTNode *node) {
418 // Get entry of left side
419
2/4
✓ Branch 2 → 3 taken 6487 times.
✗ Branch 2 → 18 not taken.
✓ Branch 3 → 4 taken 6487 times.
✗ Branch 3 → 16 not taken.
6487 auto exprResult = std::any_cast<LLVMExprResult>(visit(lhsNode));
420 6487 SymbolTableEntry *entry = exprResult.entry;
421
7/10
✓ Branch 5 → 6 taken 5623 times.
✓ Branch 5 → 10 taken 864 times.
✓ Branch 6 → 7 taken 5623 times.
✗ Branch 6 → 19 not taken.
✓ Branch 7 → 8 taken 5623 times.
✗ Branch 7 → 19 not taken.
✓ Branch 8 → 9 taken 281 times.
✓ Branch 8 → 10 taken 5342 times.
✓ Branch 10 → 11 taken 6206 times.
✗ Branch 10 → 19 not taken.
6487 llvm::Value *lhsAddress = entry != nullptr && entry->getQualType().isRef() ? exprResult.refPtr : resolveAddress(exprResult);
422
1/2
✓ Branch 12 → 13 taken 6487 times.
✗ Branch 12 → 19 not taken.
12974 return doAssignment(lhsAddress, entry, rhsNode, node);
423 }
424
425 15413 LLVMExprResult IRGenerator::doAssignment(llvm::Value *lhsAddress, SymbolTableEntry *lhsEntry, const ExprNode *rhsNode,
426 const ASTNode *node, bool isDecl) {
427 // Get symbol type of right side
428
1/2
✓ Branch 2 → 3 taken 15413 times.
✗ Branch 2 → 13 not taken.
15413 const QualType &rhsSType = rhsNode->getEvaluatedSymbolType(manIdx);
429
2/4
✓ Branch 3 → 4 taken 15413 times.
✗ Branch 3 → 12 not taken.
✓ Branch 4 → 5 taken 15413 times.
✗ Branch 4 → 10 not taken.
15413 auto rhs = std::any_cast<LLVMExprResult>(visit(rhsNode));
430
1/2
✓ Branch 6 → 7 taken 15413 times.
✗ Branch 6 → 13 not taken.
30826 return doAssignment(lhsAddress, lhsEntry, rhs, rhsSType, node, isDecl);
431 }
432
433 15548 LLVMExprResult IRGenerator::doAssignment(llvm::Value *lhsAddress, SymbolTableEntry *lhsEntry, LLVMExprResult &rhs,
434 const QualType &rhsSType, const ASTNode *node, bool isDecl) {
435 // Deduce some information about the assignment
436
4/4
✓ Branch 2 → 3 taken 14684 times.
✓ Branch 2 → 7 taken 864 times.
✓ Branch 5 → 6 taken 592 times.
✓ Branch 5 → 7 taken 14092 times.
15548 const bool isRefAssign = lhsEntry != nullptr && lhsEntry->getQualType().isRef();
437
8/10
✓ Branch 8 → 9 taken 14956 times.
✓ Branch 8 → 15 taken 592 times.
✓ Branch 9 → 10 taken 14956 times.
✗ Branch 9 → 217 not taken.
✓ Branch 10 → 11 taken 14956 times.
✗ Branch 10 → 217 not taken.
✓ Branch 11 → 12 taken 1531 times.
✓ Branch 11 → 15 taken 13425 times.
✓ Branch 13 → 14 taken 180 times.
✓ Branch 13 → 15 taken 1351 times.
15548 const bool needsCopy = !isRefAssign && rhsSType.removeReferenceWrapper().is(TY_STRUCT) && !rhs.isTemporary();
438
439
2/2
✓ Branch 16 → 17 taken 592 times.
✓ Branch 16 → 57 taken 14956 times.
15548 if (isRefAssign) {
440
1/2
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 592 times.
592 assert(lhsEntry != nullptr);
441
2/2
✓ Branch 19 → 20 taken 311 times.
✓ Branch 19 → 33 taken 281 times.
592 if (isDecl) { // Reference gets initially assigned
442 // Get address of right side
443 311 llvm::Value *rhsAddress = resolveAddress(rhs);
444
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 311 times.
311 assert(rhsAddress != nullptr);
445
446 // Store lhs pointer to rhs
447
2/4
✓ Branch 26 → 27 taken 311 times.
✗ Branch 26 → 218 not taken.
✓ Branch 27 → 28 taken 311 times.
✗ Branch 27 → 218 not taken.
311 llvm::Value *refAddress = insertAlloca(builder.getPtrTy());
448 311 lhsEntry->updateAddress(refAddress);
449 311 insertStore(rhsAddress, refAddress);
450
451 311 return LLVMExprResult{.value = rhsAddress, .ptr = refAddress, .entry = lhsEntry};
452 }
453
454 // Reference to reference assignment (only for struct fields that are not initialized yet)
455 // These are only allowed inside a ctor body. In other cases, the value of the reference gets assigned, not the ref itself.
456
6/8
✓ Branch 33 → 34 taken 194 times.
✓ Branch 33 → 40 taken 87 times.
✓ Branch 35 → 36 taken 194 times.
✗ Branch 35 → 40 not taken.
✓ Branch 36 → 37 taken 186 times.
✓ Branch 36 → 40 taken 8 times.
✓ Branch 38 → 39 taken 186 times.
✗ Branch 38 → 40 not taken.
281 const bool isInitialFieldRefAssign = isInCtorBody && rhsSType.isRef() && rhs.entry && lhsEntry->isField();
457 // Assigning the result variable
458 281 const bool isReturnValAssign = lhsEntry->name == RETURN_VARIABLE_NAME;
459
4/4
✓ Branch 42 → 43 taken 95 times.
✓ Branch 42 → 44 taken 186 times.
✓ Branch 43 → 44 taken 2 times.
✓ Branch 43 → 49 taken 93 times.
281 if (isInitialFieldRefAssign || isReturnValAssign) {
460 // Get address of right side
461 188 llvm::Value *referencedAddress = resolveAddress(rhs);
462
1/2
✗ Branch 45 → 46 not taken.
✓ Branch 45 → 47 taken 188 times.
188 assert(referencedAddress != nullptr);
463
464 // Store the rhs* to the lhs**
465 188 insertStore(referencedAddress, lhsAddress);
466
467 188 return LLVMExprResult{.value = referencedAddress, .ptr = lhsAddress, .entry = lhsEntry};
468 }
469
470 // Load referenced address
471
3/6
✓ Branch 51 → 52 taken 93 times.
✗ Branch 51 → 226 not taken.
✓ Branch 52 → 53 taken 93 times.
✗ Branch 52 → 224 not taken.
✓ Branch 53 → 54 taken 93 times.
✗ Branch 53 → 224 not taken.
186 lhsAddress = insertLoad(builder.getPtrTy(), lhsAddress);
472 }
473
474
8/8
✓ Branch 57 → 58 taken 8750 times.
✓ Branch 57 → 63 taken 6299 times.
✓ Branch 59 → 60 taken 1030 times.
✓ Branch 59 → 63 taken 7720 times.
✓ Branch 61 → 62 taken 1027 times.
✓ Branch 61 → 63 taken 3 times.
✓ Branch 64 → 65 taken 1027 times.
✓ Branch 64 → 70 taken 14022 times.
15049 if (isDecl && rhsSType.is(TY_STRUCT) && rhs.isTemporary()) {
475
1/2
✗ Branch 65 → 66 not taken.
✓ Branch 65 → 67 taken 1027 times.
1027 assert(lhsEntry != nullptr);
476 // Directly set the address to the lhs entry (temp stealing)
477 1027 llvm::Value *rhsAddress = resolveAddress(rhs);
478 1027 lhsEntry->updateAddress(rhsAddress);
479 1027 rhs.entry = lhsEntry;
480 1027 return rhs;
481 }
482
483 // Allocate new memory if the lhs address does not exist
484
2/2
✓ Branch 70 → 71 taken 7690 times.
✓ Branch 70 → 81 taken 6332 times.
14022 if (!lhsAddress) {
485
1/2
✗ Branch 71 → 72 not taken.
✓ Branch 71 → 73 taken 7690 times.
7690 assert(lhsEntry != nullptr);
486
3/6
✓ Branch 75 → 76 taken 7690 times.
✗ Branch 75 → 232 not taken.
✓ Branch 76 → 77 taken 7690 times.
✗ Branch 76 → 230 not taken.
✓ Branch 77 → 78 taken 7690 times.
✗ Branch 77 → 230 not taken.
7690 lhsAddress = insertAlloca(lhsEntry->getQualType());
487 7690 lhsEntry->updateAddress(lhsAddress);
488 }
489
490 // 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
491
10/10
✓ Branch 81 → 82 taken 13158 times.
✓ Branch 81 → 90 taken 864 times.
✓ Branch 84 → 85 taken 2590 times.
✓ Branch 84 → 90 taken 10568 times.
✓ Branch 86 → 87 taken 6 times.
✓ Branch 86 → 90 taken 2584 times.
✓ Branch 88 → 89 taken 2 times.
✓ Branch 88 → 90 taken 4 times.
✓ Branch 91 → 92 taken 2 times.
✓ Branch 91 → 107 taken 14020 times.
14022 if (lhsEntry && lhsEntry->getQualType().isPtr() && rhsSType.isArray() && rhsSType.getArraySize() != ARRAY_SIZE_UNKNOWN) {
492 // Get address of right side
493
1/2
✓ Branch 92 → 93 taken 2 times.
✗ Branch 92 → 243 not taken.
2 llvm::Value *rhsAddress = resolveAddress(rhs);
494
1/2
✗ Branch 93 → 94 not taken.
✓ Branch 93 → 95 taken 2 times.
2 assert(rhsAddress != nullptr);
495
1/2
✓ Branch 95 → 96 taken 2 times.
✗ Branch 95 → 243 not taken.
2 llvm::Type *elementTy = rhsSType.toLLVMType(sourceFile);
496
2/4
✓ Branch 96 → 97 taken 2 times.
✗ Branch 96 → 243 not taken.
✓ Branch 97 → 98 taken 2 times.
✗ Branch 97 → 243 not taken.
2 llvm::Value *indices[2] = {builder.getInt64(0), builder.getInt32(0)};
497
1/2
✓ Branch 102 → 103 taken 2 times.
✗ Branch 102 → 236 not taken.
2 llvm::Value *firstItemAddress = insertInBoundsGEP(elementTy, rhsAddress, indices);
498
1/2
✓ Branch 105 → 106 taken 2 times.
✗ Branch 105 → 243 not taken.
2 insertStore(firstItemAddress, lhsAddress);
499 2 return LLVMExprResult{.value = rhsAddress, .ptr = lhsAddress, .entry = lhsEntry};
500 }
501
502 // Handle operator overloads
503
6/6
✓ Branch 107 → 108 taken 6299 times.
✓ Branch 107 → 111 taken 7721 times.
✓ Branch 109 → 110 taken 72 times.
✓ Branch 109 → 111 taken 6227 times.
✓ Branch 112 → 113 taken 72 times.
✓ Branch 112 → 129 taken 13948 times.
14020 if (!isDecl && conversionManager.callsOverloadedOpFct(node, DEFAULT_OP_IDX)) {
504 72 ResolverFct lhsV = [&] { return static_cast<llvm::Value *>(nullptr); };
505
1/2
✓ Branch 114 → 115 taken 72 times.
✗ Branch 114 → 244 not taken.
72 ResolverFct rhsV = [&] { return resolveValue(rhsSType, rhs); };
506 144 ResolverFct lhsP = [&] { return lhsAddress; };
507 144 ResolverFct rhsP = [&] { return resolveAddress(rhs); };
508 72 return conversionManager.callOperatorOverloadFct<2>(node, {lhsV, lhsP, rhsV, rhsP}, DEFAULT_OP_IDX);
509 72 }
510
511 // Check if we need to copy the rhs to the lhs. This happens for structs
512
2/2
✓ Branch 129 → 130 taken 151 times.
✓ Branch 129 → 185 taken 13797 times.
13948 if (needsCopy) {
513 // Get address of right side
514
1/2
✓ Branch 130 → 131 taken 151 times.
✗ Branch 130 → 304 not taken.
151 llvm::Value *rhsAddress = resolveAddress(rhs);
515
1/2
✗ Branch 131 → 132 not taken.
✓ Branch 131 → 133 taken 151 times.
151 assert(rhsAddress != nullptr);
516
517
2/4
✓ Branch 133 → 134 taken 151 times.
✗ Branch 133 → 262 not taken.
✓ Branch 134 → 135 taken 151 times.
✗ Branch 134 → 262 not taken.
151 const QualType rhsSTypeNonRef = rhsSType.removeReferenceWrapper().toNonConst();
518
3/4
✓ Branch 135 → 136 taken 151 times.
✗ Branch 135 → 304 not taken.
✓ Branch 136 → 137 taken 113 times.
✓ Branch 136 → 153 taken 38 times.
151 if (rhsSTypeNonRef.isTriviallyCopyable(node)) {
519 // Create shallow copy
520
1/2
✓ Branch 137 → 138 taken 113 times.
✗ Branch 137 → 270 not taken.
113 llvm::Type *rhsType = rhsSTypeNonRef.toLLVMType(sourceFile);
521
3/10
✓ Branch 138 → 139 taken 113 times.
✗ Branch 138 → 140 not taken.
✓ Branch 139 → 143 taken 113 times.
✗ Branch 139 → 263 not taken.
✗ Branch 142 → 143 not taken.
✗ Branch 142 → 263 not taken.
✗ Branch 143 → 144 not taken.
✓ Branch 143 → 146 taken 113 times.
✗ Branch 263 → 264 not taken.
✗ Branch 263 → 266 not taken.
113 const std::string copyName = lhsEntry ? lhsEntry->name : "";
522
3/6
✓ Branch 146 → 147 taken 113 times.
✗ Branch 146 → 149 not taken.
✗ Branch 147 → 148 not taken.
✓ Branch 147 → 149 taken 113 times.
✓ Branch 150 → 151 taken 113 times.
✗ Branch 150 → 268 not taken.
113 generateShallowCopy(rhsAddress, rhsType, lhsAddress, lhsEntry && lhsEntry->isVolatile);
523 113 } else {
524 // Check if we have a copy ctor
525
1/2
✓ Branch 153 → 154 taken 38 times.
✗ Branch 153 → 303 not taken.
38 Scope *structScope = rhsSTypeNonRef.getBodyScope();
526
2/4
✓ Branch 154 → 155 taken 38 times.
✗ Branch 154 → 275 not taken.
✓ Branch 159 → 160 taken 38 times.
✗ Branch 159 → 271 not taken.
114 const ArgList args = {{rhsSTypeNonRef.toConstRef(node), rhs.isTemporary()}};
527
2/4
✓ Branch 163 → 164 taken 38 times.
✗ Branch 163 → 279 not taken.
✓ Branch 164 → 165 taken 38 times.
✗ Branch 164 → 277 not taken.
38 const Function *copyCtor = FunctionManager::lookup(structScope, CTOR_FUNCTION_NAME, rhsSTypeNonRef, args, true);
528
1/2
✓ Branch 167 → 168 taken 38 times.
✗ Branch 167 → 175 not taken.
38 if (copyCtor != nullptr) {
529 // Call copy ctor
530
2/4
✓ Branch 170 → 171 taken 38 times.
✗ Branch 170 → 285 not taken.
✓ Branch 171 → 172 taken 38 times.
✗ Branch 171 → 283 not taken.
114 generateCtorOrDtorCall(lhsAddress, copyCtor, {rhsAddress});
531 } else {
532 const std::string structName = rhsSTypeNonRef.getName();
533 const std::string msg = "Cannot copy struct '" + structName + "', as it is not trivially copyable and has no copy ctor";
534 throw SemanticError(node, COPY_CTOR_REQUIRED, msg);
535 }
536 38 }
537 151 return LLVMExprResult{.ptr = lhsAddress, .entry = lhsEntry};
538 }
539
540 // Optimization: If we have the address of both sides, we can do a memcpy instead of loading and storing the value
541 13797 llvm::Value *rhsValue = nullptr;
542
8/8
✓ Branch 186 → 187 taken 226 times.
✓ Branch 186 → 190 taken 13571 times.
✓ Branch 187 → 188 taken 205 times.
✓ Branch 187 → 190 taken 21 times.
✓ Branch 188 → 189 taken 202 times.
✓ Branch 188 → 190 taken 3 times.
✓ Branch 191 → 192 taken 202 times.
✓ Branch 191 → 213 taken 13595 times.
13797 if (rhsSType.is(TY_STRUCT) && rhs.value == nullptr && rhs.constant == nullptr) {
543 // Create shallow copy
544
2/4
✓ Branch 192 → 193 taken 202 times.
✗ Branch 192 → 305 not taken.
✓ Branch 193 → 194 taken 202 times.
✗ Branch 193 → 305 not taken.
202 const QualType rhsSTypeNonRef = rhsSType.removeReferenceWrapper().toNonConst();
545
1/2
✓ Branch 194 → 195 taken 202 times.
✗ Branch 194 → 313 not taken.
202 llvm::Type *rhsType = rhsSTypeNonRef.toLLVMType(sourceFile);
546
1/2
✓ Branch 195 → 196 taken 202 times.
✗ Branch 195 → 313 not taken.
202 llvm::Value *rhsAddress = resolveAddress(rhs);
547
1/2
✗ Branch 196 → 197 not taken.
✓ Branch 196 → 198 taken 202 times.
202 assert(rhsAddress != nullptr);
548
6/10
✓ Branch 198 → 199 taken 201 times.
✓ Branch 198 → 200 taken 1 time.
✓ Branch 199 → 203 taken 201 times.
✗ Branch 199 → 306 not taken.
✓ Branch 202 → 203 taken 1 time.
✗ Branch 202 → 306 not taken.
✓ Branch 203 → 204 taken 1 time.
✓ Branch 203 → 206 taken 201 times.
✗ Branch 306 → 307 not taken.
✗ Branch 306 → 309 not taken.
203 const std::string copyName = lhsEntry ? lhsEntry->name : "";
549
4/6
✓ Branch 206 → 207 taken 201 times.
✓ Branch 206 → 209 taken 1 time.
✗ Branch 207 → 208 not taken.
✓ Branch 207 → 209 taken 201 times.
✓ Branch 210 → 211 taken 202 times.
✗ Branch 210 → 311 not taken.
202 generateShallowCopy(rhsAddress, rhsType, lhsAddress, lhsEntry && lhsEntry->isVolatile);
550 202 } else {
551 // We can load the value from the right side and store it to the left side
552 // Retrieve value of the right side
553 13595 rhsValue = resolveValue(rhsSType, rhs);
554 // Store the value to the address
555 13595 insertStore(rhsValue, lhsAddress, rhsSType);
556 }
557
558 13797 return LLVMExprResult{.value = rhsValue, .ptr = lhsAddress, .entry = lhsEntry};
559
5/14
✓ Branch 117 → 118 taken 72 times.
✗ Branch 117 → 247 not taken.
✓ Branch 118 → 119 taken 72 times.
✗ Branch 118 → 247 not taken.
✓ Branch 119 → 120 taken 72 times.
✗ Branch 119 → 247 not taken.
✓ Branch 120 → 121 taken 72 times.
✗ Branch 120 → 247 not taken.
✓ Branch 121 → 122 taken 72 times.
✗ Branch 121 → 245 not taken.
✗ Branch 247 → 248 not taken.
✗ Branch 247 → 251 not taken.
✗ Branch 249 → 250 not taken.
✗ Branch 249 → 251 not taken.
72 }
560
561 364 void IRGenerator::generateShallowCopy(llvm::Value *oldAddress, llvm::Type *varType, llvm::Value *targetAddress,
562 bool isVolatile) const {
563 // Retrieve size to copy
564
1/2
✓ Branch 3 → 4 taken 364 times.
✗ Branch 3 → 19 not taken.
364 const llvm::TypeSize typeSize = module->getDataLayout().getTypeAllocSize(varType);
565
566 // Create values for memcpy intrinsic
567
2/4
✓ Branch 4 → 5 taken 364 times.
✗ Branch 4 → 19 not taken.
✓ Branch 5 → 6 taken 364 times.
✗ Branch 5 → 19 not taken.
364 llvm::Value *structSize = builder.getInt64(typeSize);
568
1/2
✓ Branch 6 → 7 taken 364 times.
✗ Branch 6 → 19 not taken.
364 llvm::Value *copyVolatile = builder.getInt1(isVolatile);
569
570 // Call memcpy intrinsic to execute the shallow copy
571
1/2
✓ Branch 7 → 8 taken 364 times.
✗ Branch 7 → 19 not taken.
364 llvm::Function *memcpyFct = stdFunctionManager.getMemcpyIntrinsic();
572
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 364 times.
364 assert(targetAddress != nullptr);
573
3/6
✓ Branch 10 → 11 taken 364 times.
✗ Branch 10 → 18 not taken.
✓ Branch 12 → 13 taken 364 times.
✗ Branch 12 → 15 not taken.
✓ Branch 13 → 14 taken 364 times.
✗ Branch 13 → 15 not taken.
364 builder.CreateCall(memcpyFct, {targetAddress, oldAddress, structSize, copyVolatile});
574 364 }
575
576 26227 void IRGenerator::autoDeReferencePtr(llvm::Value *&ptr, QualType &symbolType) {
577
6/6
✓ Branch 12 → 13 taken 27366 times.
✓ Branch 12 → 15 taken 18752 times.
✓ Branch 14 → 15 taken 1139 times.
✓ Branch 14 → 16 taken 26227 times.
✓ Branch 17 → 3 taken 19891 times.
✓ Branch 17 → 18 taken 26227 times.
46118 while (symbolType.isPtr() || symbolType.isRef()) {
578
2/4
✓ Branch 5 → 6 taken 19891 times.
✗ Branch 5 → 21 not taken.
✓ Branch 6 → 7 taken 19891 times.
✗ Branch 6 → 19 not taken.
19891 ptr = insertLoad(symbolType, ptr);
579
1/2
✓ Branch 9 → 10 taken 19891 times.
✗ Branch 9 → 25 not taken.
19891 symbolType = symbolType.getContained();
580 }
581 26227 }
582
583 59 llvm::GlobalVariable *IRGenerator::createGlobalConst(const std::string &baseName, llvm::Constant *constant) const {
584 // Get unused name
585
1/2
✓ Branch 2 → 3 taken 59 times.
✗ Branch 2 → 19 not taken.
59 const std::string globalName = getUnusedGlobalName(baseName);
586 // Create global
587
1/2
✓ Branch 5 → 6 taken 59 times.
✗ Branch 5 → 15 not taken.
59 module->getOrInsertGlobal(globalName, constant->getType());
588
1/2
✓ Branch 7 → 8 taken 59 times.
✗ Branch 7 → 16 not taken.
59 llvm::GlobalVariable *global = module->getNamedGlobal(globalName);
589 // Set initializer to the given constant
590
1/2
✓ Branch 8 → 9 taken 59 times.
✗ Branch 8 → 17 not taken.
59 global->setInitializer(constant);
591 59 global->setConstant(true);
592
1/2
✓ Branch 10 → 11 taken 59 times.
✗ Branch 10 → 17 not taken.
59 global->setLinkage(llvm::GlobalValue::PrivateLinkage);
593 59 global->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
594 59 return global;
595 59 }
596
597 3099 llvm::GlobalVariable *IRGenerator::createGlobalStringConst(const std::string &baseName, const std::string &value) const {
598 // Get unused name
599
1/2
✓ Branch 2 → 3 taken 3099 times.
✗ Branch 2 → 21 not taken.
3099 const std::string globalName = getUnusedGlobalName(baseName);
600 // Create global
601
2/4
✓ Branch 3 → 4 taken 3099 times.
✗ Branch 3 → 16 not taken.
✓ Branch 5 → 6 taken 3099 times.
✗ Branch 5 → 15 not taken.
3099 builder.CreateGlobalString(value, globalName, 0, module);
602
1/2
✓ Branch 7 → 8 taken 3099 times.
✗ Branch 7 → 17 not taken.
3099 llvm::GlobalVariable *global = module->getNamedGlobal(globalName);
603 // If the output should be comparable, fix alignment to 4 bytes
604
1/2
✓ Branch 8 → 9 taken 3099 times.
✗ Branch 8 → 12 not taken.
3099 if (cliOptions.comparableOutput)
605
2/4
✓ Branch 9 → 10 taken 3099 times.
✗ Branch 9 → 18 not taken.
✓ Branch 10 → 11 taken 3099 times.
✗ Branch 10 → 18 not taken.
3099 global->setAlignment(llvm::Align(4));
606 3099 return global;
607 3099 }
608
609 3099 llvm::GlobalVariable *IRGenerator::createGlobalStringConst(const std::string &baseName, const std::string &value,
610 const CodeLoc &codeLoc) const {
611 3099 llvm::GlobalVariable *global = createGlobalStringConst(baseName, value);
612 // Create debug info
613
2/2
✓ Branch 3 → 4 taken 44 times.
✓ Branch 3 → 10 taken 3055 times.
3099 if (cliOptions.instrumentation.generateDebugInfo)
614
3/6
✓ Branch 5 → 6 taken 44 times.
✗ Branch 5 → 14 not taken.
✓ Branch 6 → 7 taken 44 times.
✗ Branch 6 → 14 not taken.
✓ Branch 7 → 8 taken 44 times.
✗ Branch 7 → 12 not taken.
44 diGenerator.generateGlobalStringDebugInfo(global, global->getName().str(), value.length(), codeLoc);
615 3099 return global;
616 }
617
618 5189 std::string IRGenerator::getUnusedGlobalName(const std::string &baseName) const {
619 // Find an unused global name
620 5189 std::string globalName;
621 5189 unsigned int suffixNumber = 0;
622 do {
623
1/2
✓ Branch 5 → 6 taken 131268 times.
✗ Branch 5 → 15 not taken.
131268 globalName = baseName + std::to_string(suffixNumber);
624 131268 suffixNumber++;
625
3/4
✓ Branch 10 → 11 taken 131268 times.
✗ Branch 10 → 19 not taken.
✓ Branch 11 → 12 taken 126079 times.
✓ Branch 11 → 13 taken 5189 times.
131268 } while (module->getNamedGlobal(globalName) != nullptr);
626 5189 return globalName;
627 }
628
629 20606 void IRGenerator::materializeConstant(LLVMExprResult &exprResult) {
630 // Skip results, that do not contain a constant or already have a value
631
3/4
✓ Branch 2 → 3 taken 20170 times.
✓ Branch 2 → 4 taken 436 times.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 20170 times.
20606 if (exprResult.value != nullptr || exprResult.constant == nullptr)
632 436 return;
633
634 // Default case: the value to the constant
635 20170 exprResult.value = exprResult.constant;
636 }
637
638 14848 bool IRGenerator::isSymbolDSOLocal(bool isPublic) const {
639 // If we are compiling a shared library and export the global symbol, we need to drop dso_local
640 // because it may be interposed by other shared objects by the dynamic linker.
641
3/4
✓ Branch 2 → 3 taken 12879 times.
✓ Branch 2 → 4 taken 1969 times.
✓ Branch 3 → 4 taken 12879 times.
✗ Branch 3 → 5 not taken.
14848 return !(isPublic && cliOptions.outputContainer == OutputContainer::SHARED_LIBRARY);
642 }
643
644 6978 llvm::GlobalValue::LinkageTypes IRGenerator::getSymbolLinkageType(bool isPublic) const {
645
2/2
✓ Branch 2 → 3 taken 5216 times.
✓ Branch 2 → 4 taken 1762 times.
6978 return isPublic ? llvm::GlobalValue::ExternalLinkage : llvm::GlobalValue::PrivateLinkage;
646 }
647
648 1320 void IRGenerator::attachComdatToSymbol(llvm::GlobalVariable *global, const std::string &comdatName, bool isPublic) const {
649 // MachO does not support comdat annotations
650
6/6
✓ Branch 2 → 3 taken 1257 times.
✓ Branch 2 → 6 taken 63 times.
✓ Branch 4 → 5 taken 1242 times.
✓ Branch 4 → 6 taken 15 times.
✓ Branch 7 → 8 taken 1242 times.
✓ Branch 7 → 12 taken 78 times.
1320 if (isPublic && cliOptions.targetTriple.getObjectFormat() != llvm::Triple::MachO)
651
2/4
✓ Branch 9 → 10 taken 1242 times.
✗ Branch 9 → 13 not taken.
✓ Branch 10 → 11 taken 1242 times.
✗ Branch 10 → 13 not taken.
1242 global->setComdat(module->getOrInsertComdat(comdatName));
652 1320 }
653
654 2034 std::string IRGenerator::getIRString(llvm::Module *llvmModule, const CliOptions &cliOptions) {
655
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2034 times.
2034 assert(llvmModule != nullptr); // Make sure the module hasn't been moved away
656
3/4
✓ Branch 4 → 5 taken 2034 times.
✗ Branch 4 → 7 not taken.
✓ Branch 5 → 6 taken 1936 times.
✓ Branch 5 → 7 taken 98 times.
2034 const bool eliminateTarget = cliOptions.comparableOutput && cliOptions.isNativeTarget;
657
658 // Backup target triple and data layout
659
1/2
✓ Branch 9 → 10 taken 2034 times.
✗ Branch 9 → 45 not taken.
2034 const llvm::Triple targetTriple = llvmModule->getTargetTriple();
660
1/2
✓ Branch 11 → 12 taken 2034 times.
✗ Branch 11 → 43 not taken.
2034 const std::string targetDataLayout = llvmModule->getDataLayoutStr();
661 // Remove target triple and data layout
662
2/2
✓ Branch 12 → 13 taken 1936 times.
✓ Branch 12 → 19 taken 98 times.
2034 if (eliminateTarget) {
663 1936 llvmModule->setTargetTriple(llvm::Triple());
664
2/4
✓ Branch 16 → 17 taken 1936 times.
✗ Branch 16 → 34 not taken.
✓ Branch 17 → 18 taken 1936 times.
✗ Branch 17 → 34 not taken.
1936 llvmModule->setDataLayout("");
665 }
666
667 // Get IR string
668 2034 std::string output;
669
1/2
✓ Branch 20 → 21 taken 2034 times.
✗ Branch 20 → 39 not taken.
2034 llvm::raw_string_ostream oss(output);
670
1/2
✓ Branch 21 → 22 taken 2034 times.
✗ Branch 21 → 37 not taken.
2034 llvmModule->print(oss, nullptr);
671
672 // Restore target triple and data layout
673
2/2
✓ Branch 22 → 23 taken 1936 times.
✓ Branch 22 → 29 taken 98 times.
2034 if (eliminateTarget) {
674
1/2
✓ Branch 23 → 24 taken 1936 times.
✗ Branch 23 → 35 not taken.
1936 llvmModule->setTargetTriple(targetTriple);
675
1/2
✓ Branch 27 → 28 taken 1936 times.
✗ Branch 27 → 36 not taken.
1936 llvmModule->setDataLayout(targetDataLayout);
676 }
677
678 2034 return output;
679 2034 }
680
681 /**
682 * Returns the operator function list for the current manifestation and the given node
683 *
684 * @param node Node to retrieve the op fct pointer list from
685 * @return Op fct pointer list
686 */
687 27674 const std::vector<const Function *> &IRGenerator::getOpFctPointers(const ASTNode *node) const {
688
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 27674 times.
27674 assert(node->getOpFctPointers()->size() > manIdx);
689 27674 return node->getOpFctPointers()->at(manIdx);
690 }
691
692 } // namespace spice::compiler
693