GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 96.8% 368 / 0 / 380
Functions: 100.0% 17 / 0 / 17
Branches: 57.8% 454 / 0 / 786

src/irgenerator/GenTopLevelDefinitions.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "IRGenerator.h"
4
5 #include <SourceFile.h>
6 #include <ast/ASTNodes.h>
7 #include <ast/Attributes.h>
8 #include <driver/Driver.h>
9 #include <symboltablebuilder/SymbolTableBuilder.h>
10 #include <typechecker/FunctionManager.h>
11
12 #include <llvm/IR/Module.h>
13 #include <llvm/Target/TargetLoweringObjectFile.h>
14
15 namespace spice::compiler {
16
17 257 std::any IRGenerator::visitMainFctDef(const MainFctDefNode *node) {
18 // Ignore main function definitions if this is not the main source file
19
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 257 times.
257 if (!sourceFile->isMainFile)
20 return nullptr;
21
22 // Do not generate main function if it is explicitly specified
23
2/2
✓ Branch 5 → 6 taken 1 time.
✓ Branch 5 → 8 taken 256 times.
257 if (cliOptions.noEntryFct)
24
1/2
✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 134 not taken.
1 return nullptr;
25
26 // Change scope to function scope
27 256 currentScope = node->bodyScope;
28
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 256 times.
256 assert(currentScope != nullptr);
29
30 // Visit parameters
31 256 std::vector<std::pair<std::string, SymbolTableEntry *>> paramInfoList;
32 256 QualTypeList paramSymbolTypes;
33 256 std::vector<llvm::Type *> paramTypes;
34
2/2
✓ Branch 10 → 11 taken 4 times.
✓ Branch 10 → 34 taken 252 times.
256 if (node->takesArgs) {
35 4 const size_t numOfParams = node->paramLst->params.size();
36
1/2
✓ Branch 12 → 13 taken 4 times.
✗ Branch 12 → 183 not taken.
4 paramInfoList.reserve(numOfParams);
37
1/2
✓ Branch 13 → 14 taken 4 times.
✗ Branch 13 → 183 not taken.
4 paramSymbolTypes.reserve(numOfParams);
38
1/2
✓ Branch 14 → 15 taken 4 times.
✗ Branch 14 → 183 not taken.
4 paramTypes.reserve(numOfParams);
39
2/2
✓ Branch 32 → 17 taken 8 times.
✓ Branch 32 → 33 taken 4 times.
12 for (DeclStmtNode *param : node->paramLst->params) {
40 // Get symbol table entry of param
41
1/2
✓ Branch 18 → 19 taken 8 times.
✗ Branch 18 → 138 not taken.
8 SymbolTableEntry *paramSymbol = node->bodyScope->lookupStrict(param->varName);
42
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 8 times.
8 assert(paramSymbol != nullptr);
43 // Retrieve type of param
44
2/4
✓ Branch 23 → 24 taken 8 times.
✗ Branch 23 → 137 not taken.
✓ Branch 24 → 25 taken 8 times.
✗ Branch 24 → 135 not taken.
8 auto paramType = any_cast<llvm::Type *>(visit(param->dataType));
45 // Add it to the lists
46
1/2
✓ Branch 26 → 27 taken 8 times.
✗ Branch 26 → 138 not taken.
8 paramInfoList.emplace_back(param->varName, paramSymbol);
47
2/4
✓ Branch 27 → 28 taken 8 times.
✗ Branch 27 → 138 not taken.
✓ Branch 28 → 29 taken 8 times.
✗ Branch 28 → 138 not taken.
8 paramSymbolTypes.push_back(paramSymbol->getQualType());
48
1/2
✓ Branch 29 → 30 taken 8 times.
✗ Branch 29 → 138 not taken.
8 paramTypes.push_back(paramType);
49 }
50 }
51
52 // Build the function
53
1/2
✓ Branch 34 → 35 taken 256 times.
✗ Branch 34 → 183 not taken.
256 llvm::Type *returnType = builder.getInt32Ty();
54
1/2
✓ Branch 36 → 37 taken 256 times.
✗ Branch 36 → 140 not taken.
256 llvm::FunctionType *fctType = llvm::FunctionType::get(returnType, paramTypes, false);
55
2/4
✓ Branch 37 → 38 taken 256 times.
✗ Branch 37 → 141 not taken.
✓ Branch 38 → 39 taken 256 times.
✗ Branch 38 → 141 not taken.
256 llvm::Function *fct = llvm::Function::Create(fctType, llvm::Function::ExternalLinkage, MAIN_FUNCTION_NAME, module);
56 256 fct->setDSOLocal(true);
57
58 // Add function attributes
59
1/2
✓ Branch 40 → 41 taken 256 times.
✗ Branch 40 → 183 not taken.
256 fct->addFnAttr(llvm::Attribute::MustProgress);
60
1/2
✓ Branch 41 → 42 taken 256 times.
✗ Branch 41 → 183 not taken.
256 fct->addFnAttr(llvm::Attribute::NoInline);
61
1/2
✓ Branch 42 → 43 taken 256 times.
✗ Branch 42 → 183 not taken.
256 fct->addFnAttr(llvm::Attribute::NoRecurse);
62
1/2
✓ Branch 43 → 44 taken 256 times.
✗ Branch 43 → 183 not taken.
256 fct->addFnAttr(llvm::Attribute::NoUnwind);
63
1/2
✓ Branch 44 → 45 taken 256 times.
✗ Branch 44 → 46 not taken.
256 if (cliOptions.optLevel == OptLevel::O0)
64
1/2
✓ Branch 45 → 48 taken 256 times.
✗ Branch 45 → 183 not taken.
256 fct->addFnAttr(llvm::Attribute::OptimizeNone);
65 else if (cliOptions.optLevel >= OptLevel::Os)
66 fct->addFnAttr(llvm::Attribute::OptimizeForSize);
67
2/4
✓ Branch 48 → 49 taken 256 times.
✗ Branch 48 → 183 not taken.
✓ Branch 49 → 50 taken 256 times.
✗ Branch 49 → 183 not taken.
256 fct->addFnAttr(llvm::Attribute::getWithUWTableKind(context, llvm::UWTableKind::Default));
68
1/2
✓ Branch 50 → 51 taken 256 times.
✗ Branch 50 → 183 not taken.
256 enableFunctionInstrumentation(fct);
69
70 // Add return value attributes
71
1/2
✓ Branch 51 → 52 taken 256 times.
✗ Branch 51 → 183 not taken.
256 fct->addRetAttr(llvm::Attribute::NoUndef);
72
73 // Add return value attributes
74
1/2
✓ Branch 52 → 53 taken 256 times.
✗ Branch 52 → 183 not taken.
256 fct->addRetAttr(llvm::Attribute::NoUndef);
75
76 // Add debug info
77
2/2
✓ Branch 53 → 54 taken 6 times.
✓ Branch 53 → 59 taken 250 times.
256 if (cliOptions.instrumentation.generateDebugInfo) {
78 6 const auto nonConstNode = const_cast<MainFctDefNode *>(node);
79
1/2
✓ Branch 54 → 55 taken 6 times.
✗ Branch 54 → 144 not taken.
6 const Function spiceFunc = FunctionManager::createMainFunction(node->entry, paramSymbolTypes, nonConstNode);
80
1/2
✓ Branch 55 → 56 taken 6 times.
✗ Branch 55 → 142 not taken.
6 diGenerator.generateFunctionDebugInfo(fct, &spiceFunc);
81
1/2
✓ Branch 56 → 57 taken 6 times.
✗ Branch 56 → 142 not taken.
6 diGenerator.setSourceLocation(node);
82 6 }
83
84 // Create entry block
85
2/4
✓ Branch 61 → 62 taken 256 times.
✗ Branch 61 → 147 not taken.
✓ Branch 62 → 63 taken 256 times.
✗ Branch 62 → 145 not taken.
256 llvm::BasicBlock *bEntry = createBlock();
86
1/2
✓ Branch 65 → 66 taken 256 times.
✗ Branch 65 → 183 not taken.
256 switchToBlock(bEntry, fct);
87
88 // Reset alloca insert markers to this block
89 256 allocaInsertBlock = bEntry;
90 256 allocaInsertInst = nullptr;
91
92 // Allocate result variable
93
3/6
✓ Branch 68 → 69 taken 256 times.
✗ Branch 68 → 154 not taken.
✓ Branch 69 → 70 taken 256 times.
✗ Branch 69 → 151 not taken.
✓ Branch 70 → 71 taken 256 times.
✗ Branch 70 → 151 not taken.
256 llvm::Value *resultAddress = insertAlloca(QualType(TY_INT), RETURN_VARIABLE_NAME);
94 // Update the symbol table entry
95
1/2
✓ Branch 75 → 76 taken 256 times.
✗ Branch 75 → 160 not taken.
768 SymbolTableEntry *resultEntry = currentScope->lookupStrict(RETURN_VARIABLE_NAME);
96
1/2
✗ Branch 81 → 82 not taken.
✓ Branch 81 → 83 taken 256 times.
256 assert(resultEntry != nullptr);
97
1/2
✓ Branch 83 → 84 taken 256 times.
✗ Branch 83 → 183 not taken.
256 resultEntry->updateAddress(resultAddress);
98 // Generate debug info
99
2/4
✓ Branch 86 → 87 taken 256 times.
✗ Branch 86 → 166 not taken.
✓ Branch 87 → 88 taken 256 times.
✗ Branch 87 → 164 not taken.
512 diGenerator.generateLocalVarDebugInfo(RETURN_VARIABLE_NAME, resultAddress);
100 // Store the default result value
101
3/6
✓ Branch 90 → 91 taken 256 times.
✗ Branch 90 → 170 not taken.
✓ Branch 91 → 92 taken 256 times.
✗ Branch 91 → 170 not taken.
✓ Branch 92 → 93 taken 256 times.
✗ Branch 92 → 170 not taken.
256 insertStore(builder.getInt32(0), resultAddress, QualType(TY_INT));
102
103 // Store function argument values
104
3/4
✓ Branch 93 → 94 taken 256 times.
✗ Branch 93 → 174 not taken.
✓ Branch 110 → 96 taken 8 times.
✓ Branch 110 → 111 taken 256 times.
264 for (auto &arg : fct->args()) {
105 // Get information about the parameter
106 8 const size_t argNumber = arg.getArgNo();
107
2/4
✓ Branch 97 → 98 taken 8 times.
✗ Branch 97 → 173 not taken.
✓ Branch 98 → 99 taken 8 times.
✗ Branch 98 → 173 not taken.
8 auto [paramName, paramSymbol] = paramInfoList.at(argNumber);
108
1/2
✗ Branch 101 → 102 not taken.
✓ Branch 101 → 103 taken 8 times.
8 assert(paramSymbol != nullptr);
109 // Allocate space for it
110
2/4
✓ Branch 103 → 104 taken 8 times.
✗ Branch 103 → 171 not taken.
✓ Branch 104 → 105 taken 8 times.
✗ Branch 104 → 171 not taken.
8 llvm::Value *paramAddress = insertAlloca(paramSymbol->getQualType(), paramName);
111 // Update the symbol table entry
112
1/2
✓ Branch 105 → 106 taken 8 times.
✗ Branch 105 → 171 not taken.
8 paramSymbol->updateAddress(paramAddress);
113 // Store the value at the new address
114
1/2
✓ Branch 106 → 107 taken 8 times.
✗ Branch 106 → 171 not taken.
8 insertStore(&arg, paramAddress);
115 // Generate debug info
116
1/2
✓ Branch 107 → 108 taken 8 times.
✗ Branch 107 → 171 not taken.
8 diGenerator.generateLocalVarDebugInfo(paramName, paramAddress, argNumber + 1);
117 8 }
118
119 // Visit function body
120
1/2
✓ Branch 111 → 112 taken 256 times.
✗ Branch 111 → 175 not taken.
256 visit(node->body);
121
122 // Create return statement if the block is not terminated yet
123
2/2
✓ Branch 113 → 114 taken 252 times.
✓ Branch 113 → 123 taken 4 times.
256 if (!blockAlreadyTerminated) {
124
4/8
✓ Branch 116 → 117 taken 252 times.
✗ Branch 116 → 178 not taken.
✓ Branch 117 → 118 taken 252 times.
✗ Branch 117 → 176 not taken.
✓ Branch 118 → 119 taken 252 times.
✗ Branch 118 → 176 not taken.
✓ Branch 119 → 120 taken 252 times.
✗ Branch 119 → 176 not taken.
252 llvm::Value *result = insertLoad(fct->getReturnType(), resultEntry->getAddress());
125
1/2
✓ Branch 122 → 123 taken 252 times.
✗ Branch 122 → 183 not taken.
252 builder.CreateRet(result);
126 }
127
128 // Conclude debug info for function
129
1/2
✓ Branch 123 → 124 taken 256 times.
✗ Branch 123 → 183 not taken.
256 diGenerator.concludeFunctionDebugInfo();
130
131 // Verify function
132
1/2
✓ Branch 124 → 125 taken 256 times.
✗ Branch 124 → 183 not taken.
256 verifyFunction(fct, node->codeLoc);
133
134 // Change back to root scope
135 256 currentScope = rootScope;
136
1/2
✗ Branch 125 → 126 not taken.
✓ Branch 125 → 127 taken 256 times.
256 assert(currentScope != nullptr);
137
138
1/2
✓ Branch 127 → 128 taken 256 times.
✗ Branch 127 → 182 not taken.
256 return nullptr;
139 256 }
140
141 7775 std::any IRGenerator::visitFctDef(const FctDefNode *node) {
142 // Loop through manifestations
143 7775 manIdx = 0; // Reset the symbolTypeIndex
144
2/2
✓ Branch 209 → 4 taken 9624 times.
✓ Branch 209 → 210 taken 7775 times.
17399 for (Function *manifestation : node->manifestations) {
145
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 9624 times.
9624 assert(manifestation->entry != nullptr);
146
147 // Check if the manifestation is substantiated or not public and not used by anybody
148
2/4
✓ Branch 7 → 8 taken 9624 times.
✗ Branch 7 → 298 not taken.
✓ Branch 8 → 9 taken 9624 times.
✗ Branch 8 → 298 not taken.
9624 const bool isPublic = manifestation->entry->getQualType().isPublic();
149
9/10
✓ Branch 9 → 10 taken 9624 times.
✗ Branch 9 → 298 not taken.
✓ Branch 10 → 11 taken 7453 times.
✓ Branch 10 → 13 taken 2171 times.
✓ Branch 11 → 12 taken 201 times.
✓ Branch 11 → 14 taken 7252 times.
✓ Branch 12 → 13 taken 10 times.
✓ Branch 12 → 14 taken 191 times.
✓ Branch 15 → 16 taken 2181 times.
✓ Branch 15 → 17 taken 7443 times.
9624 if (!manifestation->isFullySubstantiated() || (!isPublic && !manifestation->used)) {
150 2181 manIdx++; // Increment symbolTypeIndex
151 2181 continue;
152 }
153
154 // Change to struct scope
155
2/2
✓ Branch 20 → 21 taken 3395 times.
✓ Branch 20 → 29 taken 4048 times.
7443 if (manifestation->isMethod()) {
156 3395 const QualType &thisType = manifestation->thisType;
157
3/6
✓ Branch 21 → 22 taken 3395 times.
✗ Branch 21 → 217 not taken.
✓ Branch 22 → 23 taken 3395 times.
✗ Branch 22 → 217 not taken.
✓ Branch 23 → 24 taken 3395 times.
✗ Branch 23 → 217 not taken.
3395 const std::string scopeName = Struct::getScopeName(thisType.getSubType(), thisType.getTemplateTypes());
158
1/2
✓ Branch 24 → 25 taken 3395 times.
✗ Branch 24 → 215 not taken.
3395 currentScope = currentScope->getChildScope(scopeName);
159
1/2
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 3395 times.
3395 assert(currentScope != nullptr);
160 3395 }
161
162 // Change scope
163
2/4
✓ Branch 29 → 30 taken 7443 times.
✗ Branch 29 → 220 not taken.
✓ Branch 30 → 31 taken 7443 times.
✗ Branch 30 → 218 not taken.
7443 currentScope = currentScope->getChildScope(manifestation->getScopeName());
164
1/2
✗ Branch 32 → 33 not taken.
✓ Branch 32 → 34 taken 7443 times.
7443 assert(currentScope != nullptr);
165
166 // Get 'this' entry
167 7443 std::vector<std::pair<std::string, SymbolTableEntry *>> paramInfoList;
168
1/2
✓ Branch 34 → 35 taken 7443 times.
✗ Branch 34 → 294 not taken.
7443 std::vector<llvm::Type *> paramTypes;
169
2/2
✓ Branch 37 → 38 taken 3395 times.
✓ Branch 37 → 52 taken 4048 times.
7443 if (manifestation->isMethod()) {
170
1/2
✓ Branch 40 → 41 taken 3395 times.
✗ Branch 40 → 223 not taken.
10185 SymbolTableEntry *thisEntry = currentScope->lookupStrict(THIS_VARIABLE_NAME);
171
1/2
✗ Branch 46 → 47 not taken.
✓ Branch 46 → 48 taken 3395 times.
3395 assert(thisEntry != nullptr);
172
1/2
✓ Branch 48 → 49 taken 3395 times.
✗ Branch 48 → 228 not taken.
3395 paramInfoList.emplace_back(THIS_VARIABLE_NAME, thisEntry);
173
2/4
✓ Branch 49 → 50 taken 3395 times.
✗ Branch 49 → 227 not taken.
✓ Branch 50 → 51 taken 3395 times.
✗ Branch 50 → 227 not taken.
3395 paramTypes.push_back(builder.getPtrTy());
174 }
175
176 // Visit parameters
177 7443 size_t argIdx = 0;
178
2/2
✓ Branch 52 → 53 taken 5884 times.
✓ Branch 52 → 82 taken 1559 times.
7443 if (node->hasParams) {
179 5884 const size_t numOfParams = manifestation->paramList.size();
180
1/2
✓ Branch 54 → 55 taken 5884 times.
✗ Branch 54 → 294 not taken.
5884 paramInfoList.reserve(numOfParams);
181
1/2
✓ Branch 55 → 56 taken 5884 times.
✗ Branch 55 → 294 not taken.
5884 paramTypes.reserve(numOfParams);
182
2/2
✓ Branch 81 → 57 taken 8891 times.
✓ Branch 81 → 82 taken 5884 times.
14775 for (; argIdx < numOfParams; argIdx++) {
183
1/2
✓ Branch 57 → 58 taken 8891 times.
✗ Branch 57 → 234 not taken.
8891 const DeclStmtNode *param = node->paramLst->params.at(argIdx);
184 // Get symbol table entry of param
185
1/2
✓ Branch 58 → 59 taken 8891 times.
✗ Branch 58 → 234 not taken.
8891 SymbolTableEntry *paramSymbol = currentScope->lookupStrict(param->varName);
186
1/2
✗ Branch 61 → 62 not taken.
✓ Branch 61 → 63 taken 8891 times.
8891 assert(paramSymbol != nullptr);
187
2/4
✓ Branch 63 → 64 taken 8891 times.
✗ Branch 63 → 231 not taken.
✓ Branch 64 → 65 taken 8891 times.
✗ Branch 64 → 229 not taken.
8891 const QualType paramSymbolType = manifestation->getParamTypes().at(argIdx);
188 // Pass the information if captures are taken for function/procedure types
189
6/10
✓ Branch 66 → 67 taken 8891 times.
✗ Branch 66 → 232 not taken.
✓ Branch 67 → 68 taken 3 times.
✓ Branch 67 → 71 taken 8888 times.
✓ Branch 68 → 69 taken 3 times.
✗ Branch 68 → 232 not taken.
✗ Branch 69 → 70 not taken.
✓ Branch 69 → 71 taken 3 times.
✗ Branch 72 → 73 not taken.
✓ Branch 72 → 77 taken 8891 times.
8891 if (paramSymbolType.isOneOf({TY_FUNCTION, TY_PROCEDURE}) && paramSymbolType.hasLambdaCaptures())
190 paramSymbol->updateType(paramSymbol->getQualType().getWithLambdaCaptures(), true);
191 // Retrieve type of param
192
1/2
✓ Branch 77 → 78 taken 8891 times.
✗ Branch 77 → 234 not taken.
8891 llvm::Type *paramType = paramSymbolType.toLLVMType(sourceFile);
193 // Add it to the lists
194
1/2
✓ Branch 78 → 79 taken 8891 times.
✗ Branch 78 → 234 not taken.
8891 paramInfoList.emplace_back(param->varName, paramSymbol);
195
1/2
✓ Branch 79 → 80 taken 8891 times.
✗ Branch 79 → 234 not taken.
8891 paramTypes.push_back(paramType);
196 }
197 }
198
199 // Get return type
200
1/2
✓ Branch 82 → 83 taken 7443 times.
✗ Branch 82 → 294 not taken.
7443 llvm::Type *returnType = manifestation->returnType.toLLVMType(sourceFile);
201
202 // Get function linkage
203 7443 bool externalLinkage = isPublic;
204
12/18
✓ Branch 83 → 84 taken 386 times.
✓ Branch 83 → 90 taken 7057 times.
✓ Branch 86 → 87 taken 386 times.
✗ Branch 86 → 235 not taken.
✓ Branch 87 → 88 taken 386 times.
✗ Branch 87 → 235 not taken.
✓ Branch 88 → 89 taken 12 times.
✓ Branch 88 → 90 taken 374 times.
✓ Branch 91 → 92 taken 386 times.
✓ Branch 91 → 93 taken 7057 times.
✓ Branch 93 → 94 taken 386 times.
✓ Branch 93 → 96 taken 7057 times.
✓ Branch 96 → 97 taken 12 times.
✓ Branch 96 → 104 taken 7431 times.
✗ Branch 235 → 236 not taken.
✗ Branch 235 → 237 not taken.
✗ Branch 239 → 240 not taken.
✗ Branch 239 → 242 not taken.
8215 if (node->attrs && node->attrs->attrLst->hasAttr(ATTR_TEST))
205
2/4
✓ Branch 99 → 100 taken 12 times.
✗ Branch 99 → 246 not taken.
✓ Branch 100 → 101 taken 12 times.
✗ Branch 100 → 244 not taken.
36 externalLinkage |= node->attrs->attrLst->getAttrValueByName(ATTR_TEST)->boolValue;
206
2/2
✓ Branch 104 → 105 taken 7262 times.
✓ Branch 104 → 106 taken 181 times.
7443 const auto linkage = externalLinkage ? llvm::Function::ExternalLinkage : llvm::Function::PrivateLinkage;
207
208 // Create function or implement declared function
209
1/2
✓ Branch 107 → 108 taken 7443 times.
✗ Branch 107 → 294 not taken.
7443 const std::string mangledName = manifestation->getMangledName();
210
1/2
✓ Branch 109 → 110 taken 7443 times.
✗ Branch 109 → 250 not taken.
7443 llvm::FunctionType *funcType = llvm::FunctionType::get(returnType, paramTypes, false);
211
1/2
✓ Branch 111 → 112 taken 7443 times.
✗ Branch 111 → 251 not taken.
7443 module->getOrInsertFunction(mangledName, funcType);
212
1/2
✓ Branch 113 → 114 taken 7443 times.
✗ Branch 113 → 252 not taken.
7443 llvm::Function *func = module->getFunction(mangledName);
213
1/2
✓ Branch 114 → 115 taken 7443 times.
✗ Branch 114 → 292 not taken.
7443 node->entry->updateAddress(func);
214 7443 manifestation->llvmFunction = func;
215
2/4
✓ Branch 115 → 116 taken 7443 times.
✗ Branch 115 → 292 not taken.
✗ Branch 116 → 117 not taken.
✓ Branch 116 → 118 taken 7443 times.
7443 assert(func->empty());
216
217 // Set attributes to function
218
1/2
✓ Branch 118 → 119 taken 7443 times.
✗ Branch 118 → 292 not taken.
7443 func->setDSOLocal(isSymbolDSOLocal(isPublic));
219
1/2
✓ Branch 120 → 121 taken 7443 times.
✗ Branch 120 → 292 not taken.
7443 func->setLinkage(linkage);
220
4/6
✓ Branch 121 → 122 taken 7443 times.
✗ Branch 121 → 292 not taken.
✓ Branch 122 → 123 taken 7443 times.
✗ Branch 122 → 292 not taken.
✓ Branch 123 → 124 taken 2525 times.
✓ Branch 123 → 125 taken 4918 times.
7443 if (manifestation->entry->getQualType().isInline())
221
1/2
✓ Branch 124 → 125 taken 2525 times.
✗ Branch 124 → 292 not taken.
2525 func->addFnAttr(llvm::Attribute::AlwaysInline);
222
1/2
✓ Branch 125 → 126 taken 7443 times.
✗ Branch 125 → 292 not taken.
7443 enableFunctionInstrumentation(func);
223 // Set attributes to function parameters and return value
224
1/2
✓ Branch 126 → 127 taken 7443 times.
✗ Branch 126 → 292 not taken.
7443 setParamAttrs(func, paramInfoList);
225
1/2
✓ Branch 127 → 128 taken 7443 times.
✗ Branch 127 → 292 not taken.
7443 setFunctionReturnValAttrs(func, manifestation->returnType);
226
227 // Add debug info
228
1/2
✓ Branch 128 → 129 taken 7443 times.
✗ Branch 128 → 292 not taken.
7443 diGenerator.generateFunctionDebugInfo(func, manifestation);
229
1/2
✓ Branch 129 → 130 taken 7443 times.
✗ Branch 129 → 292 not taken.
7443 diGenerator.setSourceLocation(node);
230
231 // Create entry block
232
2/4
✓ Branch 132 → 133 taken 7443 times.
✗ Branch 132 → 255 not taken.
✓ Branch 133 → 134 taken 7443 times.
✗ Branch 133 → 253 not taken.
7443 llvm::BasicBlock *bEntry = createBlock();
233
1/2
✓ Branch 136 → 137 taken 7443 times.
✗ Branch 136 → 292 not taken.
7443 switchToBlock(bEntry, func);
234
235 // Reset alloca insert markers to this block
236 7443 allocaInsertBlock = bEntry;
237 7443 allocaInsertInst = nullptr;
238
239 // Declare result variable
240
2/4
✓ Branch 139 → 140 taken 7443 times.
✗ Branch 139 → 261 not taken.
✓ Branch 140 → 141 taken 7443 times.
✗ Branch 140 → 259 not taken.
7443 llvm::Value *resultAddr = insertAlloca(manifestation->returnType, RETURN_VARIABLE_NAME);
241
1/2
✓ Branch 145 → 146 taken 7443 times.
✗ Branch 145 → 267 not taken.
22329 SymbolTableEntry *resultEntry = currentScope->lookupStrict(RETURN_VARIABLE_NAME);
242
1/2
✗ Branch 151 → 152 not taken.
✓ Branch 151 → 153 taken 7443 times.
7443 assert(resultEntry != nullptr);
243
1/2
✓ Branch 153 → 154 taken 7443 times.
✗ Branch 153 → 292 not taken.
7443 resultEntry->updateAddress(resultAddr);
244 // Generate debug info
245
2/4
✓ Branch 156 → 157 taken 7443 times.
✗ Branch 156 → 273 not taken.
✓ Branch 157 → 158 taken 7443 times.
✗ Branch 157 → 271 not taken.
14886 diGenerator.generateLocalVarDebugInfo(RETURN_VARIABLE_NAME, resultAddr);
246
247 // Store function argument values
248
3/4
✓ Branch 160 → 161 taken 7443 times.
✗ Branch 160 → 280 not taken.
✓ Branch 178 → 163 taken 12286 times.
✓ Branch 178 → 179 taken 7443 times.
19729 for (auto &arg : func->args()) {
249 // Get information about the parameter
250 12286 const size_t argNumber = arg.getArgNo();
251
2/4
✓ Branch 164 → 165 taken 12286 times.
✗ Branch 164 → 279 not taken.
✓ Branch 165 → 166 taken 12286 times.
✗ Branch 165 → 279 not taken.
12286 auto [paramName, paramSymbol] = paramInfoList.at(argNumber);
252
1/2
✗ Branch 168 → 169 not taken.
✓ Branch 168 → 170 taken 12286 times.
12286 assert(paramSymbol != nullptr);
253 // Allocate space for it
254
2/4
✓ Branch 170 → 171 taken 12286 times.
✗ Branch 170 → 277 not taken.
✓ Branch 171 → 172 taken 12286 times.
✗ Branch 171 → 277 not taken.
12286 llvm::Value *paramAddress = insertAlloca(paramSymbol->getQualType(), paramName);
255 // Update the symbol table entry
256
1/2
✓ Branch 172 → 173 taken 12286 times.
✗ Branch 172 → 277 not taken.
12286 paramSymbol->updateAddress(paramAddress);
257 // Set source location
258
1/2
✓ Branch 173 → 174 taken 12286 times.
✗ Branch 173 → 277 not taken.
12286 diGenerator.setSourceLocation(paramSymbol->declNode);
259 // Store the value at the new address
260
1/2
✓ Branch 174 → 175 taken 12286 times.
✗ Branch 174 → 277 not taken.
12286 insertStore(&arg, paramAddress);
261 // Generate debug info to declare variable
262
1/2
✓ Branch 175 → 176 taken 12286 times.
✗ Branch 175 → 277 not taken.
12286 diGenerator.generateLocalVarDebugInfo(paramName, paramAddress, argNumber + 1);
263 12286 }
264
265 // Store the default values for optional function args
266
2/2
✓ Branch 179 → 180 taken 5884 times.
✓ Branch 179 → 190 taken 1559 times.
7443 if (node->paramLst) {
267
1/2
✓ Branch 180 → 181 taken 5884 times.
✗ Branch 180 → 284 not taken.
5884 const std::vector<DeclStmtNode *> params = node->paramLst->params;
268
2/2
✓ Branch 187 → 182 taken 619 times.
✓ Branch 187 → 188 taken 5884 times.
6503 for (; argIdx < params.size(); argIdx++)
269
2/4
✓ Branch 182 → 183 taken 619 times.
✗ Branch 182 → 281 not taken.
✓ Branch 183 → 184 taken 619 times.
✗ Branch 183 → 281 not taken.
619 visit(params.at(argIdx));
270 5884 }
271
272 // Visit function body
273
1/2
✓ Branch 190 → 191 taken 7443 times.
✗ Branch 190 → 285 not taken.
7443 visit(node->body);
274
275 // Create return statement if the block is not terminated yet
276
2/2
✓ Branch 192 → 193 taken 336 times.
✓ Branch 192 → 201 taken 7107 times.
7443 if (!blockAlreadyTerminated) {
277
3/6
✓ Branch 195 → 196 taken 336 times.
✗ Branch 195 → 288 not taken.
✓ Branch 196 → 197 taken 336 times.
✗ Branch 196 → 286 not taken.
✓ Branch 197 → 198 taken 336 times.
✗ Branch 197 → 286 not taken.
336 llvm::Value *result = insertLoad(returnType, resultEntry->getAddress());
278
1/2
✓ Branch 200 → 201 taken 336 times.
✗ Branch 200 → 292 not taken.
336 builder.CreateRet(result);
279 }
280
281 // Conclude debug info for function
282
1/2
✓ Branch 201 → 202 taken 7443 times.
✗ Branch 201 → 292 not taken.
7443 diGenerator.concludeFunctionDebugInfo();
283
284 // Verify function
285
1/2
✓ Branch 202 → 203 taken 7443 times.
✗ Branch 202 → 292 not taken.
7443 verifyFunction(func, node->codeLoc);
286
287 // Change to root scope
288 7443 currentScope = rootScope;
289
290 7443 manIdx++; // Increment symbolTypeIndex
291 7443 }
292 7775 manIdx = 0; // Reset the symbolTypeIndex
293
294 // Ensure that we are at the root scope again
295
1/2
✗ Branch 210 → 211 not taken.
✓ Branch 210 → 212 taken 7775 times.
7775 assert(currentScope == rootScope);
296
297
1/2
✓ Branch 212 → 213 taken 7775 times.
✗ Branch 212 → 300 not taken.
7775 return nullptr;
298 }
299
300 3995 std::any IRGenerator::visitProcDef(const ProcDefNode *node) {
301 // Loop through manifestations
302 3995 manIdx = 0; // Reset the symbolTypeIndex
303
2/2
✓ Branch 161 → 4 taken 5828 times.
✓ Branch 161 → 162 taken 3995 times.
9823 for (Function *manifestation : node->manifestations) {
304
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 5828 times.
5828 assert(manifestation->entry != nullptr);
305
306 // Check if the manifestation is substantiated or not public and not used by anybody
307
2/4
✓ Branch 7 → 8 taken 5828 times.
✗ Branch 7 → 211 not taken.
✓ Branch 8 → 9 taken 5828 times.
✗ Branch 8 → 211 not taken.
5828 const bool isPublic = manifestation->entry->getQualType().isPublic();
308
9/10
✓ Branch 9 → 10 taken 5828 times.
✗ Branch 9 → 211 not taken.
✓ Branch 10 → 11 taken 3825 times.
✓ Branch 10 → 13 taken 2003 times.
✓ Branch 11 → 12 taken 1061 times.
✓ Branch 11 → 14 taken 2764 times.
✓ Branch 12 → 13 taken 7 times.
✓ Branch 12 → 14 taken 1054 times.
✓ Branch 15 → 16 taken 2010 times.
✓ Branch 15 → 17 taken 3818 times.
5828 if (!manifestation->isFullySubstantiated() || (!isPublic && !manifestation->used)) {
309 2010 manIdx++; // Increment symbolTypeIndex
310 2010 continue;
311 }
312
1/2
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 3818 times.
3818 assert(manifestation->alreadyTypeChecked);
313
314 // Change to struct scope
315
2/2
✓ Branch 22 → 23 taken 2798 times.
✓ Branch 22 → 31 taken 1020 times.
3818 if (manifestation->isMethod()) {
316 2798 const QualType &thisType = manifestation->thisType;
317
3/6
✓ Branch 23 → 24 taken 2798 times.
✗ Branch 23 → 169 not taken.
✓ Branch 24 → 25 taken 2798 times.
✗ Branch 24 → 169 not taken.
✓ Branch 25 → 26 taken 2798 times.
✗ Branch 25 → 169 not taken.
2798 const std::string scopeName = Struct::getScopeName(thisType.getSubType(), thisType.getTemplateTypes());
318
1/2
✓ Branch 26 → 27 taken 2798 times.
✗ Branch 26 → 167 not taken.
2798 currentScope = currentScope->getChildScope(scopeName);
319
1/2
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 29 taken 2798 times.
2798 assert(currentScope != nullptr);
320 2798 }
321
322 // Change scope
323
2/4
✓ Branch 31 → 32 taken 3818 times.
✗ Branch 31 → 172 not taken.
✓ Branch 32 → 33 taken 3818 times.
✗ Branch 32 → 170 not taken.
3818 currentScope = currentScope->getChildScope(manifestation->getScopeName());
324
1/2
✗ Branch 34 → 35 not taken.
✓ Branch 34 → 36 taken 3818 times.
3818 assert(currentScope != nullptr);
325
326 // Get 'this' entry
327 3818 std::vector<std::pair<std::string, SymbolTableEntry *>> paramInfoList;
328
1/2
✓ Branch 36 → 37 taken 3818 times.
✗ Branch 36 → 207 not taken.
3818 std::vector<llvm::Type *> paramTypes;
329
2/2
✓ Branch 39 → 40 taken 2798 times.
✓ Branch 39 → 54 taken 1020 times.
3818 if (manifestation->isMethod()) {
330
1/2
✓ Branch 42 → 43 taken 2798 times.
✗ Branch 42 → 175 not taken.
8394 SymbolTableEntry *thisEntry = currentScope->lookupStrict(THIS_VARIABLE_NAME);
331
1/2
✗ Branch 48 → 49 not taken.
✓ Branch 48 → 50 taken 2798 times.
2798 assert(thisEntry != nullptr);
332
1/2
✓ Branch 50 → 51 taken 2798 times.
✗ Branch 50 → 180 not taken.
2798 paramInfoList.emplace_back(THIS_VARIABLE_NAME, thisEntry);
333
2/4
✓ Branch 51 → 52 taken 2798 times.
✗ Branch 51 → 179 not taken.
✓ Branch 52 → 53 taken 2798 times.
✗ Branch 52 → 179 not taken.
2798 paramTypes.push_back(builder.getPtrTy());
334 }
335
336 // Visit parameters
337 3818 size_t argIdx = 0;
338
2/2
✓ Branch 54 → 55 taken 2849 times.
✓ Branch 54 → 84 taken 969 times.
3818 if (node->hasParams) {
339 2849 const size_t numOfParams = manifestation->paramList.size();
340
1/2
✓ Branch 56 → 57 taken 2849 times.
✗ Branch 56 → 207 not taken.
2849 paramInfoList.reserve(numOfParams);
341
1/2
✓ Branch 57 → 58 taken 2849 times.
✗ Branch 57 → 207 not taken.
2849 paramTypes.reserve(numOfParams);
342
2/2
✓ Branch 83 → 59 taken 5155 times.
✓ Branch 83 → 84 taken 2849 times.
8004 for (; argIdx < numOfParams; argIdx++) {
343
1/2
✓ Branch 59 → 60 taken 5155 times.
✗ Branch 59 → 186 not taken.
5155 const DeclStmtNode *param = node->paramLst->params.at(argIdx);
344 // Get symbol table entry of param
345
1/2
✓ Branch 60 → 61 taken 5155 times.
✗ Branch 60 → 186 not taken.
5155 SymbolTableEntry *paramSymbol = currentScope->lookupStrict(param->varName);
346
1/2
✗ Branch 63 → 64 not taken.
✓ Branch 63 → 65 taken 5155 times.
5155 assert(paramSymbol != nullptr);
347
2/4
✓ Branch 65 → 66 taken 5155 times.
✗ Branch 65 → 183 not taken.
✓ Branch 66 → 67 taken 5155 times.
✗ Branch 66 → 181 not taken.
5155 const QualType paramSymbolType = manifestation->getParamTypes().at(argIdx);
348 // Pass the information if captures are taken for function/procedure types
349
8/10
✓ Branch 68 → 69 taken 5155 times.
✗ Branch 68 → 184 not taken.
✓ Branch 69 → 70 taken 21 times.
✓ Branch 69 → 73 taken 5134 times.
✓ Branch 70 → 71 taken 21 times.
✗ Branch 70 → 184 not taken.
✓ Branch 71 → 72 taken 3 times.
✓ Branch 71 → 73 taken 18 times.
✓ Branch 74 → 75 taken 3 times.
✓ Branch 74 → 79 taken 5152 times.
5155 if (paramSymbolType.isOneOf({TY_FUNCTION, TY_PROCEDURE}) && paramSymbolType.hasLambdaCaptures())
350
3/6
✓ Branch 75 → 76 taken 3 times.
✗ Branch 75 → 185 not taken.
✓ Branch 76 → 77 taken 3 times.
✗ Branch 76 → 185 not taken.
✓ Branch 77 → 78 taken 3 times.
✗ Branch 77 → 185 not taken.
3 paramSymbol->updateType(paramSymbol->getQualType().getWithLambdaCaptures(), true);
351 // Retrieve type of param
352
1/2
✓ Branch 79 → 80 taken 5155 times.
✗ Branch 79 → 186 not taken.
5155 llvm::Type *paramType = paramSymbolType.toLLVMType(sourceFile);
353 // Add it to the lists
354
1/2
✓ Branch 80 → 81 taken 5155 times.
✗ Branch 80 → 186 not taken.
5155 paramInfoList.emplace_back(param->varName, paramSymbol);
355
1/2
✓ Branch 81 → 82 taken 5155 times.
✗ Branch 81 → 186 not taken.
5155 paramTypes.push_back(paramType);
356 }
357 }
358
359 // Get return type
360
1/2
✓ Branch 84 → 85 taken 3818 times.
✗ Branch 84 → 207 not taken.
3818 llvm::Type *returnType = builder.getVoidTy();
361
362 // Create procedure or implement declared procedure
363
1/2
✓ Branch 85 → 86 taken 3818 times.
✗ Branch 85 → 207 not taken.
3818 const std::string mangledName = manifestation->getMangledName();
364
1/2
✓ Branch 87 → 88 taken 3818 times.
✗ Branch 87 → 187 not taken.
3818 llvm::FunctionType *procType = llvm::FunctionType::get(returnType, paramTypes, false);
365
1/2
✓ Branch 89 → 90 taken 3818 times.
✗ Branch 89 → 188 not taken.
3818 module->getOrInsertFunction(mangledName, procType);
366
1/2
✓ Branch 91 → 92 taken 3818 times.
✗ Branch 91 → 189 not taken.
3818 llvm::Function *proc = module->getFunction(mangledName);
367
1/2
✓ Branch 92 → 93 taken 3818 times.
✗ Branch 92 → 205 not taken.
3818 node->entry->updateAddress(proc);
368 3818 manifestation->llvmFunction = proc;
369
2/4
✓ Branch 93 → 94 taken 3818 times.
✗ Branch 93 → 205 not taken.
✗ Branch 94 → 95 not taken.
✓ Branch 94 → 96 taken 3818 times.
3818 assert(proc->empty());
370
371 // Set attributes to procedure
372
2/4
✓ Branch 96 → 97 taken 3818 times.
✗ Branch 96 → 205 not taken.
✓ Branch 97 → 98 taken 3818 times.
✗ Branch 97 → 205 not taken.
3818 proc->setLinkage(getSymbolLinkageType(isPublic));
373
1/2
✓ Branch 98 → 99 taken 3818 times.
✗ Branch 98 → 205 not taken.
3818 proc->setDSOLocal(isSymbolDSOLocal(isPublic));
374
4/6
✓ Branch 100 → 101 taken 3818 times.
✗ Branch 100 → 205 not taken.
✓ Branch 101 → 102 taken 3818 times.
✗ Branch 101 → 205 not taken.
✓ Branch 102 → 103 taken 308 times.
✓ Branch 102 → 104 taken 3510 times.
3818 if (manifestation->entry->getQualType().isInline())
375
1/2
✓ Branch 103 → 104 taken 308 times.
✗ Branch 103 → 205 not taken.
308 proc->addFnAttr(llvm::Attribute::AlwaysInline);
376
1/2
✓ Branch 104 → 105 taken 3818 times.
✗ Branch 104 → 205 not taken.
3818 enableFunctionInstrumentation(proc);
377
378 // Set attributes to function parameters
379
1/2
✓ Branch 105 → 106 taken 3818 times.
✗ Branch 105 → 205 not taken.
3818 setParamAttrs(proc, paramInfoList);
380
381 // Add debug info
382
1/2
✓ Branch 106 → 107 taken 3818 times.
✗ Branch 106 → 205 not taken.
3818 diGenerator.generateFunctionDebugInfo(proc, manifestation);
383
1/2
✓ Branch 107 → 108 taken 3818 times.
✗ Branch 107 → 205 not taken.
3818 diGenerator.setSourceLocation(node);
384
385 // Create entry block
386
2/4
✓ Branch 110 → 111 taken 3818 times.
✗ Branch 110 → 192 not taken.
✓ Branch 111 → 112 taken 3818 times.
✗ Branch 111 → 190 not taken.
3818 llvm::BasicBlock *bEntry = createBlock();
387
1/2
✓ Branch 114 → 115 taken 3818 times.
✗ Branch 114 → 205 not taken.
3818 switchToBlock(bEntry, proc);
388
389 // Reset alloca insert markers to this block
390 3818 allocaInsertBlock = bEntry;
391 3818 allocaInsertInst = nullptr;
392
393 // Store procedure argument values
394
3/4
✓ Branch 115 → 116 taken 3818 times.
✗ Branch 115 → 199 not taken.
✓ Branch 133 → 118 taken 7953 times.
✓ Branch 133 → 134 taken 3818 times.
11771 for (auto &arg : proc->args()) {
395 // Get information about the parameter
396 7953 const size_t argNumber = arg.getArgNo();
397
2/4
✓ Branch 119 → 120 taken 7953 times.
✗ Branch 119 → 198 not taken.
✓ Branch 120 → 121 taken 7953 times.
✗ Branch 120 → 198 not taken.
7953 auto [paramName, paramSymbol] = paramInfoList.at(argNumber);
398
1/2
✗ Branch 123 → 124 not taken.
✓ Branch 123 → 125 taken 7953 times.
7953 assert(paramSymbol != nullptr);
399 // Allocate space for it
400
2/4
✓ Branch 125 → 126 taken 7953 times.
✗ Branch 125 → 196 not taken.
✓ Branch 126 → 127 taken 7953 times.
✗ Branch 126 → 196 not taken.
7953 llvm::Value *paramAddress = insertAlloca(paramSymbol->getQualType(), paramName);
401 // Update the symbol table entry
402
1/2
✓ Branch 127 → 128 taken 7953 times.
✗ Branch 127 → 196 not taken.
7953 paramSymbol->updateAddress(paramAddress);
403 // Set source location
404
1/2
✓ Branch 128 → 129 taken 7953 times.
✗ Branch 128 → 196 not taken.
7953 diGenerator.setSourceLocation(paramSymbol->declNode);
405 // Store the value at the new address
406
1/2
✓ Branch 129 → 130 taken 7953 times.
✗ Branch 129 → 196 not taken.
7953 insertStore(&arg, paramAddress);
407 // Generate debug info to declare variable
408
1/2
✓ Branch 130 → 131 taken 7953 times.
✗ Branch 130 → 196 not taken.
7953 diGenerator.generateLocalVarDebugInfo(paramName, paramAddress, argNumber + 1);
409 7953 }
410
411 // Store the default values for optional procedure args
412
2/2
✓ Branch 134 → 135 taken 2849 times.
✓ Branch 134 → 145 taken 969 times.
3818 if (node->paramLst) {
413
1/2
✓ Branch 135 → 136 taken 2849 times.
✗ Branch 135 → 203 not taken.
2849 const std::vector<DeclStmtNode *> params = node->paramLst->params;
414
2/2
✓ Branch 142 → 137 taken 196 times.
✓ Branch 142 → 143 taken 2849 times.
3045 for (; argIdx < params.size(); argIdx++)
415
2/4
✓ Branch 137 → 138 taken 196 times.
✗ Branch 137 → 200 not taken.
✓ Branch 138 → 139 taken 196 times.
✗ Branch 138 → 200 not taken.
196 visit(params.at(argIdx));
416 2849 }
417
418 // Generate special ctor preamble before generating the body to store VTable, default field values, etc. if required
419
2/2
✓ Branch 145 → 146 taken 1313 times.
✓ Branch 145 → 147 taken 2505 times.
3818 if (node->isCtor) {
420 1313 isInCtorBody = true;
421
1/2
✓ Branch 146 → 147 taken 1313 times.
✗ Branch 146 → 205 not taken.
1313 generateCtorBodyPreamble(currentScope);
422 }
423
424 // Visit procedure body
425
1/2
✓ Branch 147 → 148 taken 3818 times.
✗ Branch 147 → 204 not taken.
3818 visit(node->body);
426
427
2/2
✓ Branch 149 → 150 taken 1313 times.
✓ Branch 149 → 151 taken 2505 times.
3818 if (node->isCtor)
428 1313 isInCtorBody = false;
429
430 // Create return statement if the block is not terminated yet
431
2/2
✓ Branch 151 → 152 taken 3715 times.
✓ Branch 151 → 153 taken 103 times.
3818 if (!blockAlreadyTerminated)
432
1/2
✓ Branch 152 → 153 taken 3715 times.
✗ Branch 152 → 205 not taken.
3715 builder.CreateRetVoid();
433
434 // Conclude debug info for procedure
435
1/2
✓ Branch 153 → 154 taken 3818 times.
✗ Branch 153 → 205 not taken.
3818 diGenerator.concludeFunctionDebugInfo();
436
437 // Verify procedure
438
1/2
✓ Branch 154 → 155 taken 3818 times.
✗ Branch 154 → 205 not taken.
3818 verifyFunction(proc, node->codeLoc);
439
440 // Change to root scope
441 3818 currentScope = rootScope;
442
443 3818 manIdx++; // Increment symbolTypeIndex
444 3818 }
445 3995 manIdx = 0; // Reset the symbolTypeIndex
446
447 // Ensure that we are at the root scope again
448
1/2
✗ Branch 162 → 163 not taken.
✓ Branch 162 → 164 taken 3995 times.
3995 assert(currentScope == rootScope);
449
450
1/2
✓ Branch 164 → 165 taken 3995 times.
✗ Branch 164 → 213 not taken.
3995 return nullptr;
451 }
452
453 11261 void IRGenerator::setParamAttrs(llvm::Function *function, const ParamInfoList &paramInfo) const {
454
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 11261 times.
11261 assert(function->arg_size() == paramInfo.size());
455
2/2
✓ Branch 30 → 7 taken 20239 times.
✓ Branch 30 → 31 taken 11261 times.
31500 for (size_t i = 0; i < paramInfo.size(); i++) {
456 20239 const QualType &paramType = paramInfo.at(i).second->getQualType();
457
458 // NoUndef attribute
459 20239 function->addParamAttr(i, llvm::Attribute::NoUndef);
460
461
2/2
✓ Branch 11 → 12 taken 6564 times.
✓ Branch 11 → 25 taken 13675 times.
20239 if (paramType.isPtr()) {
462 // NonNull attribute
463 6564 function->addParamAttr(i, llvm::Attribute::NonNull);
464 // Dereferenceable attribute
465
2/4
✓ Branch 13 → 14 taken 6564 times.
✗ Branch 13 → 32 not taken.
✓ Branch 14 → 15 taken 6564 times.
✗ Branch 14 → 32 not taken.
6564 llvm::Type *pointeeType = paramType.getContained().toLLVMType(sourceFile);
466
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 6564 times.
6564 assert(pointeeType != nullptr);
467
3/6
✓ Branch 18 → 19 taken 6564 times.
✗ Branch 18 → 33 not taken.
✓ Branch 19 → 20 taken 6564 times.
✗ Branch 19 → 33 not taken.
✓ Branch 20 → 21 taken 6564 times.
✗ Branch 20 → 33 not taken.
6564 function->addDereferenceableParamAttr(i, module->getDataLayout().getTypeStoreSize(pointeeType));
468 // Alignment attribute
469 6564 function->addParamAttr(i, llvm::Attribute::getWithAlignment(context, module->getDataLayout().getABITypeAlign(pointeeType)));
470 }
471
472 // ZExt or SExt attribute
473
2/2
✓ Branch 26 → 27 taken 3889 times.
✓ Branch 26 → 28 taken 16350 times.
20239 if (const llvm::Attribute::AttrKind extAttrKind = getExtAttrKindForType(paramType); extAttrKind != llvm::Attribute::None)
474 3889 function->addParamAttr(i, extAttrKind);
475 }
476 11261 }
477
478 7443 void IRGenerator::setFunctionReturnValAttrs(llvm::Function *function, const QualType &returnType) const {
479
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 7443 times.
7443 if (returnType.is(TY_DYN))
480 return;
481
482 // NoUndef attribute
483 7443 function->addRetAttr(llvm::Attribute::NoUndef);
484 // ZExt or SExt attribute
485
2/2
✓ Branch 7 → 8 taken 3438 times.
✓ Branch 7 → 9 taken 4005 times.
7443 if (const llvm::Attribute::AttrKind extAttrKind = getExtAttrKindForType(returnType); extAttrKind != llvm::Attribute::None)
486 3438 function->addRetAttr(extAttrKind);
487 }
488
489 69884 llvm::Attribute::AttrKind IRGenerator::getExtAttrKindForType(const QualType &type) const {
490
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 69884 times.
69884 if (type.is(TY_DYN))
491 return llvm::Attribute::None;
492
493 69884 const llvm::Type *llvmType = type.toLLVMType(sourceFile);
494
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 69884 times.
69884 const unsigned int maxExtWidth = cliOptions.targetTriple.isPPC() ? 64 : 32;
495
6/6
✓ Branch 11 → 12 taken 31600 times.
✓ Branch 11 → 15 taken 38284 times.
✓ Branch 13 → 14 taken 16230 times.
✓ Branch 13 → 15 taken 15370 times.
✓ Branch 16 → 17 taken 16230 times.
✓ Branch 16 → 22 taken 53654 times.
69884 if (llvmType->isIntegerTy() && llvmType->getIntegerBitWidth() < maxExtWidth)
496
2/2
✓ Branch 18 → 19 taken 1708 times.
✓ Branch 18 → 20 taken 14522 times.
16230 return type.isSigned() ? llvm::Attribute::SExt : llvm::Attribute::ZExt;
497 53654 return llvm::Attribute::None;
498 }
499
500 641 std::any IRGenerator::visitStructDef(const StructDefNode *node) {
501 // Get all substantiated structs which result from this struct def
502
1/2
✓ Branch 2 → 3 taken 641 times.
✗ Branch 2 → 115 not taken.
641 std::vector<Struct *> manifestations = node->structManifestations;
503
504 // Sort the manifestations to prevent generating the struct types in the wrong order (in case of dependencies between structs)
505 920 const auto comp = [](const Struct *lhs, const Struct *rhs) { return lhs->manifestationIndex < rhs->manifestationIndex; };
506
1/2
✓ Branch 3 → 4 taken 641 times.
✗ Branch 3 → 113 not taken.
641 std::ranges::sort(manifestations, comp);
507
508
2/2
✓ Branch 71 → 6 taken 1101 times.
✓ Branch 71 → 72 taken 641 times.
1742 for (Struct *spiceStruct : manifestations) {
509 // Skip structs, that are not fully substantiated
510
3/4
✓ Branch 7 → 8 taken 1101 times.
✗ Branch 7 → 110 not taken.
✓ Branch 8 → 9 taken 310 times.
✓ Branch 8 → 10 taken 791 times.
1101 if (!spiceStruct->isFullySubstantiated())
511 311 continue;
512
513 // Do not generate this struct if it is private and used by nobody
514
8/10
✓ Branch 10 → 11 taken 5 times.
✓ Branch 10 → 15 taken 786 times.
✓ Branch 11 → 12 taken 5 times.
✗ Branch 11 → 110 not taken.
✓ Branch 12 → 13 taken 5 times.
✗ Branch 12 → 110 not taken.
✓ Branch 13 → 14 taken 1 time.
✓ Branch 13 → 15 taken 4 times.
✓ Branch 16 → 17 taken 1 time.
✓ Branch 16 → 18 taken 790 times.
791 if (!spiceStruct->used && !spiceStruct->entry->getQualType().isPublic())
515 1 continue;
516
517 // Change scope to struct scope, specific to substantiation
518 790 currentScope = spiceStruct->scope;
519
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 790 times.
790 assert(currentScope);
520
521 // Set LLVM type to the struct entry
522 790 const SymbolTableEntry *structEntry = spiceStruct->entry;
523
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 790 times.
790 assert(structEntry != nullptr);
524
525 // Generate VTable if required
526
2/2
✓ Branch 22 → 23 taken 217 times.
✓ Branch 22 → 26 taken 573 times.
790 if (node->emitVTable) {
527
1/2
✓ Branch 23 → 24 taken 217 times.
✗ Branch 23 → 110 not taken.
217 generateVTable(spiceStruct);
528
1/2
✓ Branch 24 → 25 taken 217 times.
✗ Branch 24 → 76 not taken.
434 deferredVTableInitializations.emplace_back([=, this]() { generateVTableInitializer(spiceStruct); }, false);
529 }
530
531 // Generate default ctor if required
532
1/2
✓ Branch 26 → 27 taken 790 times.
✗ Branch 26 → 110 not taken.
790 const QualType &thisType = structEntry->getQualType();
533
2/4
✓ Branch 30 → 31 taken 790 times.
✗ Branch 30 → 80 not taken.
✓ Branch 31 → 32 taken 790 times.
✗ Branch 31 → 78 not taken.
2370 const Function *ctorFunc = FunctionManager::lookup(currentScope, CTOR_FUNCTION_NAME, thisType, {}, true);
534
4/4
✓ Branch 35 → 36 taken 325 times.
✓ Branch 35 → 38 taken 465 times.
✓ Branch 36 → 37 taken 45 times.
✓ Branch 36 → 38 taken 280 times.
790 if (ctorFunc != nullptr && ctorFunc->implicitDefault)
535
1/2
✓ Branch 37 → 38 taken 45 times.
✗ Branch 37 → 110 not taken.
45 generateDefaultCtor(ctorFunc);
536
537 // Generate default copy ctor if required
538
2/4
✓ Branch 38 → 39 taken 790 times.
✗ Branch 38 → 91 not taken.
✓ Branch 42 → 43 taken 790 times.
✗ Branch 42 → 87 not taken.
2370 const ArgList args = {{thisType.toConstRef(node), false /* always non-temporary */}};
539
2/4
✓ Branch 46 → 47 taken 790 times.
✗ Branch 46 → 95 not taken.
✓ Branch 47 → 48 taken 790 times.
✗ Branch 47 → 93 not taken.
790 const Function *copyCtorFunc = FunctionManager::lookup(currentScope, CTOR_FUNCTION_NAME, thisType, args, true);
540
4/4
✓ Branch 50 → 51 taken 216 times.
✓ Branch 50 → 53 taken 574 times.
✓ Branch 51 → 52 taken 98 times.
✓ Branch 51 → 53 taken 118 times.
790 if (copyCtorFunc != nullptr && copyCtorFunc->implicitDefault)
541
1/2
✓ Branch 52 → 53 taken 98 times.
✗ Branch 52 → 108 not taken.
98 generateDefaultCopyCtor(copyCtorFunc);
542
543 // Generate default dtor if required
544
2/4
✓ Branch 56 → 57 taken 790 times.
✗ Branch 56 → 101 not taken.
✓ Branch 57 → 58 taken 790 times.
✗ Branch 57 → 99 not taken.
2370 const Function *dtorFunc = FunctionManager::lookup(currentScope, DTOR_FUNCTION_NAME, thisType, {}, true);
545
4/4
✓ Branch 61 → 62 taken 224 times.
✓ Branch 61 → 64 taken 566 times.
✓ Branch 62 → 63 taken 113 times.
✓ Branch 62 → 64 taken 111 times.
790 if (dtorFunc != nullptr && dtorFunc->implicitDefault)
546
1/2
✓ Branch 63 → 64 taken 113 times.
✗ Branch 63 → 108 not taken.
113 generateDefaultDtor(dtorFunc);
547
548 // Return to root scope
549 790 currentScope = rootScope;
550
1/2
✗ Branch 64 → 65 not taken.
✓ Branch 64 → 66 taken 790 times.
790 assert(currentScope);
551 790 }
552
553
1/2
✓ Branch 72 → 73 taken 641 times.
✗ Branch 72 → 112 not taken.
1282 return nullptr;
554 641 }
555
556 92 std::any IRGenerator::visitInterfaceDef(const InterfaceDefNode *node) {
557 // Get all substantiated structs which result from this struct def
558
1/2
✓ Branch 2 → 3 taken 92 times.
✗ Branch 2 → 34 not taken.
92 std::vector<Interface *> manifestations = node->interfaceManifestations;
559
560 // Sort the manifestations to prevent generating the struct types in the wrong order (in case of dependencies between structs)
561 326 const auto comp = [](const Interface *lhs, const Interface *rhs) { return lhs->manifestationIndex < rhs->manifestationIndex; };
562
1/2
✓ Branch 3 → 4 taken 92 times.
✗ Branch 3 → 32 not taken.
92 std::ranges::sort(manifestations, comp);
563
564
2/2
✓ Branch 23 → 6 taken 255 times.
✓ Branch 23 → 24 taken 92 times.
347 for (Interface *spiceInterface : manifestations) {
565 // Skip interfaces, that are not fully substantiated
566
3/4
✓ Branch 7 → 8 taken 255 times.
✗ Branch 7 → 30 not taken.
✓ Branch 8 → 9 taken 75 times.
✓ Branch 8 → 10 taken 180 times.
255 if (!spiceInterface->isFullySubstantiated())
567 75 continue;
568
569 // Do not generate this interface if it is private and used by nobody
570
7/10
✓ Branch 10 → 11 taken 1 time.
✓ Branch 10 → 15 taken 179 times.
✓ Branch 11 → 12 taken 1 time.
✗ Branch 11 → 30 not taken.
✓ Branch 12 → 13 taken 1 time.
✗ Branch 12 → 30 not taken.
✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 15 not taken.
✓ Branch 16 → 17 taken 1 time.
✓ Branch 16 → 18 taken 179 times.
180 if (!spiceInterface->used && !spiceInterface->entry->getQualType().isPublic())
571 1 continue;
572
573 // Generate VTable information
574
1/2
✓ Branch 18 → 19 taken 179 times.
✗ Branch 18 → 30 not taken.
179 generateVTable(spiceInterface);
575
1/2
✓ Branch 19 → 20 taken 179 times.
✗ Branch 19 → 28 not taken.
358 deferredVTableInitializations.emplace_back([=, this]() { generateVTableInitializer(spiceInterface); }, false);
576 }
577
578
1/2
✓ Branch 24 → 25 taken 92 times.
✗ Branch 24 → 31 not taken.
184 return nullptr;
579 92 }
580
581 64 std::any IRGenerator::visitEnumDef(const EnumDefNode *node) {
582
1/2
✓ Branch 2 → 3 taken 64 times.
✗ Branch 2 → 5 not taken.
64 return nullptr; // Noop (enums are high-level semantic-only structures)
583 }
584
585 920 std::any IRGenerator::visitGenericTypeDef(const GenericTypeDefNode *node) {
586
1/2
✓ Branch 2 → 3 taken 920 times.
✗ Branch 2 → 5 not taken.
920 return nullptr; // Noop (generic types are high-level semantic-only structures)
587 }
588
589 68 std::any IRGenerator::visitAliasDef(const AliasDefNode *node) {
590
1/2
✓ Branch 2 → 3 taken 68 times.
✗ Branch 2 → 5 not taken.
68 return nullptr; // Noop (alias definitions are high-level semantic-only structures)
591 }
592
593 1171 std::any IRGenerator::visitGlobalVarDef(const GlobalVarDefNode *node) {
594 // Retrieve some information about the variable
595
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1171 times.
1171 assert(node->entry != nullptr);
596 1171 const QualType &entryType = node->entry->getQualType();
597 1171 const bool isPublic = entryType.isPublic();
598 1171 const bool isConst = entryType.isConst();
599
600 // Get correct type and linkage type
601
2/4
✓ Branch 7 → 8 taken 1171 times.
✗ Branch 7 → 38 not taken.
✓ Branch 8 → 9 taken 1171 times.
✗ Branch 8 → 36 not taken.
1171 const auto varType = std::any_cast<llvm::Type *>(visit(node->dataType));
602
603 // Create global var
604
1/2
✓ Branch 11 → 12 taken 1171 times.
✗ Branch 11 → 39 not taken.
1171 llvm::Value *varAddress = module->getOrInsertGlobal(node->varName, varType);
605
1/2
✓ Branch 13 → 14 taken 1171 times.
✗ Branch 13 → 40 not taken.
1171 llvm::GlobalVariable *var = module->getNamedGlobal(node->varName);
606 // Set some attributes, based on the given information
607 1171 var->setConstant(isConst);
608 1171 var->setLinkage(getSymbolLinkageType(isPublic));
609 1171 var->setDSOLocal(isSymbolDSOLocal(isPublic));
610
611 // Set initializer
612
1/2
✓ Branch 19 → 20 taken 1171 times.
✗ Branch 19 → 24 not taken.
1171 if (node->hasValue) { // Set the constant value as variable initializer
613
2/4
✓ Branch 20 → 21 taken 1171 times.
✗ Branch 20 → 43 not taken.
✓ Branch 21 → 22 taken 1171 times.
✗ Branch 21 → 41 not taken.
1171 const auto constantValue = std::any_cast<llvm::Constant *>(visit(node->constant));
614 1171 var->setInitializer(constantValue);
615 } else if (cliOptions.buildMode != BuildMode::RELEASE) { // Set the default value as variable initializer
616 assert(cliOptions.buildMode == BuildMode::DEBUG || cliOptions.buildMode == BuildMode::TEST);
617 llvm::Constant *constantValue = getDefaultValueForSymbolType(node->entry->getQualType());
618 var->setInitializer(constantValue);
619 }
620
621 1171 node->entry->updateAddress(varAddress);
622
623 // Add debug info
624 1171 diGenerator.generateGlobalVarDebugInfo(var, node->entry);
625
626
1/2
✓ Branch 33 → 34 taken 1171 times.
✗ Branch 33 → 44 not taken.
1171 return nullptr;
627 }
628
629 1004 std::any IRGenerator::visitExtDecl(const ExtDeclNode *node) {
630 // Get return type
631 1004 const Function *spiceFunc = node->extFunction;
632
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1004 times.
1004 assert(spiceFunc != nullptr);
633
1/2
✓ Branch 4 → 5 taken 1004 times.
✗ Branch 4 → 91 not taken.
1004 llvm::Type *returnType = builder.getVoidTy();
634
3/4
✓ Branch 5 → 6 taken 1004 times.
✗ Branch 5 → 91 not taken.
✓ Branch 6 → 7 taken 660 times.
✓ Branch 6 → 9 taken 344 times.
1004 if (!spiceFunc->returnType.is(TY_DYN))
635
1/2
✓ Branch 7 → 8 taken 660 times.
✗ Branch 7 → 91 not taken.
660 returnType = spiceFunc->returnType.toLLVMType(sourceFile);
636
637 // Get arg types
638 1004 std::vector<llvm::Type *> argTypes;
639
1/2
✓ Branch 10 → 11 taken 1004 times.
✗ Branch 10 → 89 not taken.
1004 argTypes.reserve(spiceFunc->paramList.size());
640
3/4
✓ Branch 11 → 12 taken 1004 times.
✗ Branch 11 → 67 not taken.
✓ Branch 19 → 14 taken 1969 times.
✓ Branch 19 → 20 taken 1004 times.
2973 for (const QualType &paramType : spiceFunc->getParamTypes())
641
2/4
✓ Branch 15 → 16 taken 1969 times.
✗ Branch 15 → 64 not taken.
✓ Branch 16 → 17 taken 1969 times.
✗ Branch 16 → 64 not taken.
2973 argTypes.push_back(paramType.toLLVMType(sourceFile));
642
643 // Declare function
644
4/4
✓ Branch 21 → 22 taken 963 times.
✓ Branch 21 → 24 taken 41 times.
✓ Branch 22 → 23 taken 17 times.
✓ Branch 22 → 24 taken 946 times.
1004 const bool isVarArg = node->argTypeLst && node->argTypeLst->hasEllipsis;
645
1/2
✓ Branch 26 → 27 taken 1004 times.
✗ Branch 26 → 68 not taken.
1004 llvm::FunctionType *functionType = llvm::FunctionType::get(returnType, argTypes, isVarArg);
646
1/2
✓ Branch 27 → 28 taken 1004 times.
✗ Branch 27 → 89 not taken.
1004 const std::string mangledName = spiceFunc->getMangledName();
647
1/2
✓ Branch 29 → 30 taken 1004 times.
✗ Branch 29 → 69 not taken.
1004 module->getOrInsertFunction(mangledName, functionType);
648
1/2
✓ Branch 31 → 32 taken 1004 times.
✗ Branch 31 → 70 not taken.
1004 llvm::Function *fct = module->getFunction(mangledName);
649
650 // Add noundef attribute to all parameters
651
2/2
✓ Branch 36 → 33 taken 1969 times.
✓ Branch 36 → 37 taken 1004 times.
2973 for (size_t i = 0; i < argTypes.size(); i++)
652
1/2
✓ Branch 33 → 34 taken 1969 times.
✗ Branch 33 → 87 not taken.
1969 fct->addParamAttr(i, llvm::Attribute::NoUndef);
653
654 // If the function should be imported as dll, add the dll attribute
655
10/18
✓ Branch 37 → 38 taken 1 time.
✓ Branch 37 → 44 taken 1003 times.
✓ Branch 40 → 41 taken 1 time.
✗ Branch 40 → 71 not taken.
✓ Branch 41 → 42 taken 1 time.
✗ Branch 41 → 71 not taken.
✗ Branch 42 → 43 not taken.
✓ Branch 42 → 44 taken 1 time.
✓ Branch 45 → 46 taken 1 time.
✓ Branch 45 → 47 taken 1003 times.
✓ Branch 47 → 48 taken 1 time.
✓ Branch 47 → 50 taken 1003 times.
✗ Branch 50 → 51 not taken.
✓ Branch 50 → 59 taken 1004 times.
✗ Branch 71 → 72 not taken.
✗ Branch 71 → 73 not taken.
✗ Branch 75 → 76 not taken.
✗ Branch 75 → 78 not taken.
1006 if (node->attrs && node->attrs->attrLst->hasAttr(ATTR_CORE_LINKER_DLL))
656 if (node->attrs->attrLst->getAttrValueByName(ATTR_CORE_LINKER_DLL)->boolValue)
657 fct->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
658
659
1/2
✓ Branch 59 → 60 taken 1004 times.
✗ Branch 59 → 86 not taken.
2008 return nullptr;
660 1004 }
661
662 } // namespace spice::compiler
663