GCC Code Coverage Report


Directory: ../
File: src/irgenerator/GenControlStructures.cpp
Date: 2024-12-24 01:17:15
Exec Total Coverage
Lines: 247 247 100.0%
Functions: 11 11 100.0%
Branches: 287 538 53.3%

Line Branch Exec Source
1 // Copyright (c) 2021-2024 ChilliBits. All rights reserved.
2
3 #include "IRGenerator.h"
4
5 #include <ast/ASTNodes.h>
6 #include <symboltablebuilder/ScopeHandle.h>
7
8 namespace spice::compiler {
9
10 1517 std::any IRGenerator::visitUnsafeBlockDef(const UnsafeBlockNode *node) {
11
1/2
✓ Branch 1 taken 1517 times.
✗ Branch 2 not taken.
1517 diGenerator.setSourceLocation(node);
12
13 // Change scope
14
2/4
✓ Branch 1 taken 1517 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1517 times.
✗ Branch 5 not taken.
1517 ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::UNSAFE_BODY, node);
15
16 // Visit instructions in the block
17
1/2
✓ Branch 1 taken 1517 times.
✗ Branch 2 not taken.
1517 visit(node->body);
18
19
1/2
✓ Branch 1 taken 1517 times.
✗ Branch 2 not taken.
3034 return nullptr;
20 1517 }
21
22 950 std::any IRGenerator::visitForLoop(const ForLoopNode *node) {
23
1/2
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
950 diGenerator.setSourceLocation(node);
24
25 // Create blocks
26
1/2
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
950 const std::string codeLine = node->codeLoc.toPrettyLine();
27
2/4
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 950 times.
✗ Branch 5 not taken.
950 llvm::BasicBlock *bHead = createBlock("for.head." + codeLine);
28
2/4
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 950 times.
✗ Branch 5 not taken.
950 llvm::BasicBlock *bBody = createBlock("for.body." + codeLine);
29
2/4
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 950 times.
✗ Branch 5 not taken.
950 llvm::BasicBlock *bTail = createBlock("for.tail." + codeLine);
30
2/4
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 950 times.
✗ Branch 5 not taken.
950 llvm::BasicBlock *bExit = createBlock("for.exit." + codeLine);
31
32 // Change scope
33
2/4
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 950 times.
✗ Branch 5 not taken.
950 ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::FOR_BODY, node);
34
35 // Save the blocks for break and continue
36
1/2
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
950 breakBlocks.push_back(bExit);
37
1/2
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
950 continueBlocks.push_back(bTail);
38
39 // Init statement
40
1/2
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
950 visit(node->initDecl);
41 // Create jump from original to head block
42
1/2
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
950 insertJump(bHead);
43
44 // Switch to head block
45
1/2
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
950 switchToBlock(bHead);
46 // Condition evaluation
47
1/2
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
950 llvm::Value *condValue = resolveValue(node->condAssign);
48 // Create conditional jump from head to body or exit block
49
1/2
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
950 insertCondJump(condValue, bBody, bExit);
50
51 // Switch to body block
52
1/2
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
950 switchToBlock(bBody);
53 // Visit body
54
1/2
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
950 visit(node->body);
55 // Create jump from body to tail block
56
1/2
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
950 insertJump(bTail);
57
58 // Switch to tail block
59
1/2
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
950 switchToBlock(bTail);
60 // Inc statement
61
1/2
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
950 visit(node->incAssign);
62 // Create jump from tail to head
63
1/2
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
950 insertJump(bHead);
64
65 // Switch to exit block
66
1/2
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
950 switchToBlock(bExit);
67
68 // Pop basic blocks from break and continue stacks
69
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 950 times.
950 assert(breakBlocks.back() == bExit);
70 950 breakBlocks.pop_back();
71
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 950 times.
950 assert(continueBlocks.back() == bTail);
72 950 continueBlocks.pop_back();
73
74
1/2
✓ Branch 1 taken 950 times.
✗ Branch 2 not taken.
1900 return nullptr;
75 950 }
76
77 85 std::any IRGenerator::visitForeachLoop(const ForeachLoopNode *node) {
78
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
85 diGenerator.setSourceLocation(node);
79
80 // Create blocks
81
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
85 const std::string codeLine = node->codeLoc.toPrettyLine();
82
2/4
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 85 times.
✗ Branch 5 not taken.
85 llvm::BasicBlock *bHead = createBlock("foreach.head." + codeLine);
83
2/4
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 85 times.
✗ Branch 5 not taken.
85 llvm::BasicBlock *bBody = createBlock("foreach.body." + codeLine);
84
2/4
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 85 times.
✗ Branch 5 not taken.
85 llvm::BasicBlock *bTail = createBlock("foreach.tail." + codeLine);
85
2/4
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 85 times.
✗ Branch 5 not taken.
85 llvm::BasicBlock *bExit = createBlock("foreach.exit." + codeLine);
86
87 // Change scope
88
2/4
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 85 times.
✗ Branch 5 not taken.
85 ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::FOREACH_BODY, node);
89
90 // Save the blocks for break and continue
91
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
85 breakBlocks.push_back(bExit);
92
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
85 continueBlocks.push_back(bTail);
93
94 // Resolve iterator
95 85 AssignExprNode *iteratorAssignNode = node->iteratorAssign;
96 llvm::Value *iteratorPtr;
97
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 15 times.
85 if (node->getIteratorFct != nullptr) { // The iteratorAssignExpr is of type Iterable
98
1/2
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
70 llvm::Value *iterablePtr = resolveAddress(iteratorAssignNode);
99
100 llvm::Value *iterator;
101
10/16
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 63 times.
✓ Branch 6 taken 7 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 7 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 7 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 7 times.
✓ Branch 15 taken 63 times.
✓ Branch 17 taken 7 times.
✓ Branch 18 taken 63 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
140 if (!node->getIteratorFct->isMethod() && node->getIteratorFct->getParamTypes().front().isArray()) { // Array as iterable
102 // Call iterate() function from std/iterator/array-iterator
103
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 llvm::Function *iterateFct = stdFunctionManager.getIterateFct(node->getIteratorFct);
104
2/4
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
7 const size_t arraySize = iteratorAssignNode->getEvaluatedSymbolType(manIdx).getArraySize();
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 assert(arraySize > 0);
106
4/8
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 7 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 7 times.
✗ Branch 12 not taken.
7 iterator = builder.CreateCall(iterateFct, {iterablePtr, builder.getInt64(arraySize)});
107 } else { // Struct as iterable
108 // Call .getIterator() on iterable
109
1/2
✓ Branch 1 taken 63 times.
✗ Branch 2 not taken.
63 llvm::Function *getIteratorFct = stdFunctionManager.getIteratorFct(node->getIteratorFct);
110
3/6
✓ Branch 1 taken 63 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 63 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 63 times.
✗ Branch 9 not taken.
63 iterator = builder.CreateCall(getIteratorFct, iterablePtr);
111 }
112
113 // Resolve address of iterator
114 70 LLVMExprResult callResult = {.value = iterator, .node = iteratorAssignNode};
115
1/2
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
70 iteratorPtr = resolveAddress(callResult);
116
117 // Attach address to anonymous symbol to keep track of de-allocation
118
1/2
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
70 SymbolTableEntry *returnSymbol = currentScope->symbolTable.lookupAnonymous(iteratorAssignNode->codeLoc);
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 assert(returnSymbol != nullptr);
120
1/2
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
70 returnSymbol->updateAddress(iteratorPtr);
121 } else { // The iteratorAssignExpr is of type Iterator
122
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
15 iteratorPtr = resolveAddress(iteratorAssignNode);
123 }
124
125 // Check we have an idx
126 85 const DeclStmtNode *idxDeclNode = node->idxVarDecl;
127 85 const bool hasIdx = idxDeclNode != nullptr;
128 // Retrieve item ref type
129
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 85 times.
85 assert(hasIdx ? node->getIdxFct != nullptr : node->getFct != nullptr);
130
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 80 times.
85 const QualType itemRefSTy = hasIdx ? node->getIdxFct->returnType : node->getFct->returnType;
131
132 // Visit idx variable declaration if required
133 85 SymbolTableEntry *idxEntry = nullptr;
134 85 llvm::Value *idxAddress = nullptr;
135
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 80 times.
85 if (hasIdx) {
136
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 visit(idxDeclNode);
137 // Get address of idx variable
138
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 idxEntry = idxDeclNode->entries.at(manIdx);
139
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 idxAddress = idxEntry->getAddress();
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 assert(idxAddress != nullptr);
141 }
142
143 // Visit item variable declaration
144 85 const DeclStmtNode *itemDeclNode = node->itemVarDecl;
145
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
85 visit(itemDeclNode);
146 // Get address of item variable
147
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
85 SymbolTableEntry *itemEntry = itemDeclNode->entries.at(manIdx);
148
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
85 llvm::Value *itemAddress = itemEntry->getAddress();
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
85 assert(itemAddress != nullptr);
150
151 // Create jump from original to head block
152
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
85 insertJump(bHead);
153
154 // Switch to head block
155
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
85 switchToBlock(bHead);
156 // Call .isValid() on iterator
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
85 assert(node->isValidFct);
158
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
85 llvm::Function *isValidFct = stdFunctionManager.getIteratorIsValidFct(node->isValidFct);
159
3/6
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 85 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 85 times.
✗ Branch 9 not taken.
85 llvm::Value *isValid = builder.CreateCall(isValidFct, iteratorPtr);
160 // Create conditional jump from head to body or exit block
161
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
85 insertCondJump(isValid, bBody, bExit);
162
163 // Switch to body block
164
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
85 switchToBlock(bBody);
165 // Get the current iterator values
166
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 80 times.
85 if (hasIdx) {
167 // Allocate space to save pair
168
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 llvm::Type *pairTy = node->getIdxFct->returnType.toLLVMType(sourceFile);
169
2/4
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
5 llvm::Value *pairPtr = insertAlloca(pairTy, "pair_addr");
170 // Call .getIdx() on iterator
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 assert(node->getIdxFct);
172
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 llvm::Function *getIdxFct = stdFunctionManager.getIteratorGetIdxFct(node->getIdxFct);
173
3/6
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 5 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 5 times.
✗ Branch 9 not taken.
5 llvm::Value *pair = builder.CreateCall(getIdxFct, iteratorPtr);
174
2/4
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
5 pair->setName("pair");
175
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 insertStore(pair, pairPtr);
176 // Store idx to idx var
177
2/4
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
5 llvm::Value *idxAddrInPair = insertStructGEP(pairTy, pairPtr, 0, "idx_addr");
178 5 LLVMExprResult idxResult = {.ptr = idxAddrInPair};
179
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 assert(idxAddress != nullptr && idxEntry != nullptr);
180
2/4
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
5 doAssignment(idxAddress, idxEntry, idxResult, QualType(TY_LONG), node, true);
181 // Store item to item var
182
2/4
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
5 llvm::Value *itemAddrInPair = insertStructGEP(pairTy, pairPtr, 1, "item_addr");
183 5 LLVMExprResult itemResult = {.refPtr = itemAddrInPair};
184
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 doAssignment(itemAddress, itemEntry, itemResult, itemRefSTy, node, true);
185 } else {
186 // Call .get() on iterator
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 assert(node->getFct);
188
1/2
✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
80 llvm::Function *getFct = stdFunctionManager.getIteratorGetFct(node->getFct);
189
3/6
✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 80 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 80 times.
✗ Branch 9 not taken.
80 llvm::Value *getItemPtr = builder.CreateCall(getFct, iteratorPtr);
190 80 LLVMExprResult getResult = {.ptr = getItemPtr};
191
1/2
✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
80 doAssignment(itemAddress, itemEntry, getResult, itemRefSTy, node, true);
192 }
193 // Visit body
194
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
85 visit(node->body);
195 // Create jump from body to tail block
196
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
85 insertJump(bTail);
197
198 // Switch to tail block
199
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
85 switchToBlock(bTail);
200 // Call .next() on iterator
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
85 assert(node->nextFct);
202
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
85 llvm::Function *nextFct = stdFunctionManager.getIteratorNextFct(node->nextFct);
203
3/6
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 85 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 85 times.
✗ Branch 9 not taken.
85 builder.CreateCall(nextFct, iteratorPtr);
204 // Create jump from tail to head block
205
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
85 insertJump(bHead);
206
207 // Switch to exit block
208
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
85 switchToBlock(bExit);
209
210 // Pop basic blocks from break and continue stacks
211
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 85 times.
85 assert(breakBlocks.back() == bExit);
212 85 breakBlocks.pop_back();
213
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 85 times.
85 assert(continueBlocks.back() == bTail);
214 85 continueBlocks.pop_back();
215
216
1/2
✓ Branch 1 taken 85 times.
✗ Branch 2 not taken.
170 return nullptr;
217 85 }
218
219 422 std::any IRGenerator::visitWhileLoop(const WhileLoopNode *node) {
220
1/2
✓ Branch 1 taken 422 times.
✗ Branch 2 not taken.
422 diGenerator.setSourceLocation(node);
221
222 // Create blocks
223
1/2
✓ Branch 1 taken 422 times.
✗ Branch 2 not taken.
422 const std::string codeLine = node->codeLoc.toPrettyLine();
224
2/4
✓ Branch 1 taken 422 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 422 times.
✗ Branch 5 not taken.
422 llvm::BasicBlock *bHead = createBlock("while.head." + codeLine);
225
2/4
✓ Branch 1 taken 422 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 422 times.
✗ Branch 5 not taken.
422 llvm::BasicBlock *bBody = createBlock("while.body." + codeLine);
226
2/4
✓ Branch 1 taken 422 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 422 times.
✗ Branch 5 not taken.
422 llvm::BasicBlock *bExit = createBlock("while.exit." + codeLine);
227
228 // Change scope
229
2/4
✓ Branch 1 taken 422 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 422 times.
✗ Branch 5 not taken.
422 ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::WHILE_BODY, node);
230
231 // Save the blocks for break and continue
232
1/2
✓ Branch 1 taken 422 times.
✗ Branch 2 not taken.
422 breakBlocks.push_back(bExit);
233
1/2
✓ Branch 1 taken 422 times.
✗ Branch 2 not taken.
422 continueBlocks.push_back(bHead);
234
235 // Jump to head block
236
1/2
✓ Branch 1 taken 422 times.
✗ Branch 2 not taken.
422 insertJump(bHead);
237
238 // Switch to head block
239
1/2
✓ Branch 1 taken 422 times.
✗ Branch 2 not taken.
422 switchToBlock(bHead);
240 // Evaluate condition
241
1/2
✓ Branch 1 taken 422 times.
✗ Branch 2 not taken.
422 llvm::Value *condValue = resolveValue(node->condition);
242 // Jump to body or exit block, depending on the condition
243
1/2
✓ Branch 1 taken 422 times.
✗ Branch 2 not taken.
422 insertCondJump(condValue, bBody, bExit);
244
245 // Switch to body block
246
1/2
✓ Branch 1 taken 422 times.
✗ Branch 2 not taken.
422 switchToBlock(bBody);
247 // Visit body
248
1/2
✓ Branch 1 taken 422 times.
✗ Branch 2 not taken.
422 visit(node->body);
249 // Create jump to head block
250
1/2
✓ Branch 1 taken 422 times.
✗ Branch 2 not taken.
422 insertJump(bHead);
251
252 // Switch to exit block
253
1/2
✓ Branch 1 taken 422 times.
✗ Branch 2 not taken.
422 switchToBlock(bExit);
254
255 // Pop basic blocks from break and continue stacks
256
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 422 times.
422 assert(breakBlocks.back() == bExit);
257 422 breakBlocks.pop_back();
258
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 422 times.
422 assert(continueBlocks.back() == bHead);
259 422 continueBlocks.pop_back();
260
261
1/2
✓ Branch 1 taken 422 times.
✗ Branch 2 not taken.
844 return nullptr;
262 422 }
263
264 8 std::any IRGenerator::visitDoWhileLoop(const DoWhileLoopNode *node) {
265
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 diGenerator.setSourceLocation(node);
266
267 // Create blocks
268
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 const std::string codeLine = node->codeLoc.toPrettyLine();
269
2/4
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 llvm::BasicBlock *bBody = createBlock("dowhile.body." + codeLine);
270
2/4
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 llvm::BasicBlock *bFoot = createBlock("dowhile.foot." + codeLine);
271
2/4
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 llvm::BasicBlock *bExit = createBlock("dowhile.exit." + codeLine);
272
273 // Change scope
274
2/4
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::WHILE_BODY, node);
275
276 // Save the blocks for break and continue
277
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 breakBlocks.push_back(bExit);
278
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 continueBlocks.push_back(bFoot);
279
280 // Jump to body block
281
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 insertJump(bBody);
282
283 // Switch to body block
284
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 switchToBlock(bBody);
285 // Visit body
286
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 visit(node->body);
287 // Create jump to foot block
288
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 insertJump(bFoot);
289
290 // Switch to head block
291
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 switchToBlock(bFoot);
292 // Evaluate condition
293
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 llvm::Value *condValue = resolveValue(node->condition);
294 // Jump to body or exit block, depending on the condition
295
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 insertCondJump(condValue, bBody, bExit);
296
297 // Switch to exit block
298
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 switchToBlock(bExit);
299
300 // Pop basic blocks from break and continue stacks
301
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 assert(breakBlocks.back() == bExit);
302 8 breakBlocks.pop_back();
303
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 assert(continueBlocks.back() == bFoot);
304 8 continueBlocks.pop_back();
305
306
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
16 return nullptr;
307 8 }
308
309 2795 std::any IRGenerator::visitIfStmt(const IfStmtNode *node) {
310
1/2
✓ Branch 1 taken 2795 times.
✗ Branch 2 not taken.
2795 diGenerator.setSourceLocation(node);
311
312 // Create blocks
313
1/2
✓ Branch 1 taken 2795 times.
✗ Branch 2 not taken.
2795 const std::string codeLine = node->codeLoc.toPrettyLine();
314
2/4
✓ Branch 1 taken 2795 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2795 times.
✗ Branch 5 not taken.
2795 llvm::BasicBlock *bThen = createBlock("if.then." + codeLine);
315
6/10
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 2655 times.
✓ Branch 3 taken 140 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 140 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 140 times.
✓ Branch 9 taken 2655 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
2795 llvm::BasicBlock *bElse = node->elseStmt ? createBlock("if.else." + codeLine) : nullptr;
316
2/4
✓ Branch 1 taken 2795 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2795 times.
✗ Branch 5 not taken.
2795 llvm::BasicBlock *bExit = createBlock("if.exit." + codeLine);
317
318 // Change scope
319
2/4
✓ Branch 1 taken 2795 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2795 times.
✗ Branch 5 not taken.
2795 ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::IF_ELSE_BODY, node);
320
321 // Retrieve condition value
322
1/2
✓ Branch 1 taken 2795 times.
✗ Branch 2 not taken.
2795 llvm::Value *condValue = resolveValue(node->condition);
323 // Check if condition is fulfilled
324
3/4
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 2655 times.
✓ Branch 3 taken 2795 times.
✗ Branch 4 not taken.
2795 insertCondJump(condValue, bThen, node->elseStmt ? bElse : bExit);
325
326 // Switch to then block
327
1/2
✓ Branch 1 taken 2795 times.
✗ Branch 2 not taken.
2795 switchToBlock(bThen);
328 // Visit then body
329
1/2
✓ Branch 1 taken 2795 times.
✗ Branch 2 not taken.
2795 visit(node->thenBody);
330 // Create jump from then to end block
331
1/2
✓ Branch 1 taken 2795 times.
✗ Branch 2 not taken.
2795 insertJump(bExit);
332
333 // Change scope back
334
1/2
✓ Branch 1 taken 2795 times.
✗ Branch 2 not taken.
2795 scopeHandle.leaveScopeEarly();
335
336
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 2655 times.
2795 if (node->elseStmt) {
337 // Switch to else block
338
1/2
✓ Branch 1 taken 140 times.
✗ Branch 2 not taken.
140 switchToBlock(bElse);
339 // Visit else block
340
1/2
✓ Branch 1 taken 140 times.
✗ Branch 2 not taken.
140 visit(node->elseStmt);
341 // Create jump from else to end block
342
1/2
✓ Branch 1 taken 140 times.
✗ Branch 2 not taken.
140 insertJump(bExit);
343 }
344
345 // Switch to exit block
346
1/2
✓ Branch 1 taken 2795 times.
✗ Branch 2 not taken.
2795 switchToBlock(bExit);
347
348 // Return conditional value as result for the 'if' stmt
349
1/2
✓ Branch 1 taken 2795 times.
✗ Branch 2 not taken.
5590 return condValue;
350 2795 }
351
352 140 std::any IRGenerator::visitElseStmt(const ElseStmtNode *node) {
353 140 diGenerator.setSourceLocation(node);
354
355
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 99 times.
140 if (node->ifStmt) { // It is an else if branch
356
1/2
✓ Branch 1 taken 41 times.
✗ Branch 2 not taken.
41 visit(node->ifStmt);
357 } else { // It is an else branch
358 // Change scope
359
2/4
✓ Branch 1 taken 99 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 99 times.
✗ Branch 5 not taken.
99 ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::IF_ELSE_BODY, node);
360
361 // Generate IR for nested statements
362
1/2
✓ Branch 1 taken 99 times.
✗ Branch 2 not taken.
99 visit(node->body);
363 99 }
364
365
1/2
✓ Branch 1 taken 140 times.
✗ Branch 2 not taken.
140 return nullptr;
366 }
367
368 4 std::any IRGenerator::visitSwitchStmt(const SwitchStmtNode *node) {
369
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 diGenerator.setSourceLocation(node);
370
371 // Create blocks
372 4 std::vector<llvm::BasicBlock *> bCases;
373
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 bCases.reserve(node->caseBranches.size());
374
2/2
✓ Branch 4 taken 24 times.
✓ Branch 5 taken 4 times.
28 for (const auto caseBranch : node->caseBranches)
375
4/8
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 24 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 24 times.
✗ Branch 11 not taken.
24 bCases.push_back(createBlock("switch.case." + caseBranch->codeLoc.toPrettyLine()));
376 4 llvm::BasicBlock *bDefault = nullptr;
377
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (node->hasDefaultBranch)
378
3/6
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
1 bDefault = createBlock("switch.default." + node->defaultBranch->codeLoc.toPrettyLine());
379
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 const std::string codeLine = node->codeLoc.toPrettyLine();
380
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 llvm::BasicBlock *bExit = createBlock("switch.exit." + codeLine);
381
382 // Save the blocks for break and continue
383
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 breakBlocks.push_back(bExit);
384
385 // Visit switch expression
386
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 llvm::Value *exprValue = resolveValue(node->assignExpr);
387
388 // Generate switch instruction
389
3/4
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 llvm::SwitchInst *switchInst = builder.CreateSwitch(exprValue, bDefault ? bDefault : bExit, node->caseBranches.size());
390
391 // Generate case branches
392
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 4 times.
28 for (size_t i = 0; i < node->caseBranches.size(); i++) {
393
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 const CaseBranchNode *caseBranch = node->caseBranches.at(i);
394
395 // Push fallthrough block
396 24 llvm::BasicBlock *bFallthrough = bDefault;
397
2/2
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 4 times.
24 if (i + 1 < node->caseBranches.size())
398
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 bFallthrough = bCases.at(i + 1);
399
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 fallthroughBlocks.push(bFallthrough);
400
401 // Switch to case block
402
2/4
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
24 switchToBlock(bCases.at(i));
403
404 // Visit case body
405
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 visit(caseBranch);
406
407 // Create jump from case to exit block
408
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 insertJump(bExit);
409
410 // Pop fallthrough block
411 24 fallthroughBlocks.pop();
412
413 // Add case to switch instruction
414
2/2
✓ Branch 5 taken 24 times.
✓ Branch 6 taken 24 times.
48 for (const CaseConstantNode *caseConstantNode : caseBranch->caseConstants) {
415
2/4
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
24 const auto caseValue = std::any_cast<llvm::Constant *>(visit(caseConstantNode));
416
3/6
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 24 times.
✗ Branch 8 not taken.
24 switchInst->addCase(llvm::cast<llvm::ConstantInt>(caseValue), bCases.at(i));
417 }
418 }
419
420 // Generate default branch
421
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (node->hasDefaultBranch) {
422 // Switch to default block
423
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 switchToBlock(bDefault);
424
425 // Visit default body
426
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 visit(node->defaultBranch);
427
428 // Create jump from default to exit block
429
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 insertJump(bExit);
430 }
431
432 // Switch to exit block
433
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 switchToBlock(bExit);
434
435 // Pop basic blocks from break stack
436
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 assert(breakBlocks.back() == bExit);
437 4 breakBlocks.pop_back();
438
439
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
8 return nullptr;
440 4 }
441
442 24 std::any IRGenerator::visitCaseBranch(const CaseBranchNode *node) {
443
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 diGenerator.setSourceLocation(node);
444
445 // Change to case body scope
446
2/4
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
24 ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::CASE_BODY);
447
448 // Visit case body
449
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 visit(node->body);
450
451
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
48 return nullptr;
452 24 }
453
454 1 std::any IRGenerator::visitDefaultBranch(const DefaultBranchNode *node) {
455
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 diGenerator.setSourceLocation(node);
456
457 // Change to default body scope
458
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::DEFAULT_BODY);
459
460 // Visit case body
461
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 visit(node->body);
462
463
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return nullptr;
464 1 }
465
466 13 std::any IRGenerator::visitAnonymousBlockStmt(const AnonymousBlockStmtNode *node) {
467
1/2
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
13 diGenerator.setSourceLocation(node);
468
469 // Change scope
470 13 node->bodyScope->parent = currentScope; // Needed for nested scopes in generic functions
471 13 node->bodyScope->symbolTable.parent = &currentScope->symbolTable; // Needed for nested scopes in generic functions
472
2/4
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
13 ScopeHandle scopeHandle(this, node->getScopeId(), ScopeType::ANONYMOUS_BLOCK_BODY, node);
473
474 // Visit instructions in the block
475
1/2
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
13 visit(node->body);
476
477
1/2
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
26 return nullptr;
478 13 }
479
480 } // namespace spice::compiler
481