GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 93.2% 110 / 0 / 118
Functions: 76.9% 10 / 0 / 13
Branches: 52.9% 128 / 0 / 242

src/irgenerator/GenStatements.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "IRGenerator.h"
4
5 #include <ast/ASTNodes.h>
6 #include <driver/Driver.h>
7 #include <symboltablebuilder/SymbolTableBuilder.h>
8 #include <util/CommonUtil.h>
9
10 #include <llvm/IR/Module.h>
11
12 namespace spice::compiler {
13
14 121694 std::any IRGenerator::visitStmtLst(const StmtLstNode *node) {
15 // Generate instructions in the scope
16
2/2
✓ Branch 20 → 4 taken 244998 times.
✓ Branch 20 → 21 taken 121684 times.
488376 for (const StmtNode *stmt : node->statements) {
17 // Check if we can cancel generating instructions for this code branch
18
4/4
✓ Branch 6 → 7 taken 244992 times.
✓ Branch 6 → 21 taken 6 times.
✓ Branch 7 → 8 taken 244988 times.
✓ Branch 7 → 21 taken 4 times.
244998 if (blockAlreadyTerminated || stmt->unreachable)
19 break;
20
21 // Set source location for debug info
22
1/2
✓ Branch 8 → 9 taken 244988 times.
✗ Branch 8 → 27 not taken.
244988 diGenerator.setSourceLocation(stmt);
23
24 // Visit child
25
1/2
✓ Branch 9 → 10 taken 244988 times.
✗ Branch 9 → 26 not taken.
244988 visit(stmt);
26 }
27
28 // Generate cleanup code of this scope, e.g. dtor calls for struct instances
29 121694 generateScopeCleanup(node);
30
31
1/2
✓ Branch 22 → 23 taken 121694 times.
✗ Branch 22 → 28 not taken.
243388 return nullptr;
32 }
33
34 ✗ std::any IRGenerator::visitTypeAltsLst(const TypeAltsLstNode *node) {
35 ✗ return nullptr; // Noop
36 }
37
38 47835 std::any IRGenerator::visitDeclStmt(const DeclStmtNode *node) {
39 // Get variable entry
40
1/2
✓ Branch 2 → 3 taken 47835 times.
✗ Branch 2 → 90 not taken.
47835 const SymbolTableEntry *varEntry = node->entries.at(manIdx);
41
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 47835 times.
47835 assert(varEntry != nullptr);
42
1/2
✓ Branch 5 → 6 taken 47835 times.
✗ Branch 5 → 90 not taken.
47835 const QualType varSymbolType = varEntry->getQualType();
43
44 // Get LLVM type of variable
45
1/2
✓ Branch 6 → 7 taken 47835 times.
✗ Branch 6 → 90 not taken.
47835 llvm::Type *varTy = varSymbolType.toLLVMType(sourceFile);
46
47 // Check if the declaration is with an assignment or the default value
48 47835 llvm::Value *varAddress = nullptr;
49
2/2
✓ Branch 7 → 8 taken 45426 times.
✓ Branch 7 → 33 taken 2409 times.
47835 if (node->hasAssignment) { // Assignment
50
2/2
✓ Branch 8 → 9 taken 116 times.
✓ Branch 8 → 27 taken 45310 times.
45426 if (node->calledCopyCtor) {
51 // Allocate memory
52
1/2
✓ Branch 12 → 13 taken 116 times.
✗ Branch 12 → 65 not taken.
116 varAddress = insertAlloca(varTy);
53
1/2
✓ Branch 15 → 16 taken 116 times.
✗ Branch 15 → 90 not taken.
116 updateAddress(varEntry, varAddress);
54 // Generate debug info for variable declaration
55
1/2
✓ Branch 16 → 17 taken 116 times.
✗ Branch 16 → 90 not taken.
116 diGenerator.generateLocalVarDebugInfo(node->varName, varAddress);
56 // Call copy ctor
57
1/2
✓ Branch 17 → 18 taken 116 times.
✗ Branch 17 → 90 not taken.
116 llvm::Value *rhsAddress = resolveAddress(node->assignExpr);
58
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 116 times.
116 assert(rhsAddress != nullptr);
59
2/4
✓ Branch 22 → 23 taken 116 times.
✗ Branch 22 → 73 not taken.
✓ Branch 23 → 24 taken 116 times.
✗ Branch 23 → 71 not taken.
348 generateCtorOrDtorCall(varEntry, node->calledCopyCtor, {rhsAddress});
60 } else {
61 // Assign rhs to lhs
62
1/2
✓ Branch 27 → 28 taken 45310 times.
✗ Branch 27 → 78 not taken.
45310 [[maybe_unused]] const LLVMExprResult assignResult = doAssignment(varAddress, varEntry, node->assignExpr, node, true);
63
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 45310 times.
45310 assert(assignResult.entry == varEntry);
64
1/2
✓ Branch 30 → 31 taken 45310 times.
✗ Branch 30 → 78 not taken.
45310 varAddress = getAddress(varEntry);
65
1/2
✓ Branch 31 → 32 taken 45310 times.
✗ Branch 31 → 78 not taken.
45310 updateAddress(varEntry, varAddress);
66 }
67 } else { // Default value
68 // Allocate memory
69
1/2
✓ Branch 36 → 37 taken 2409 times.
✗ Branch 36 → 79 not taken.
2409 varAddress = insertAlloca(varTy);
70
1/2
✓ Branch 39 → 40 taken 2409 times.
✗ Branch 39 → 90 not taken.
2409 updateAddress(varEntry, varAddress);
71
72 // Generate debug info for variable declaration
73
1/2
✓ Branch 40 → 41 taken 2409 times.
✗ Branch 40 → 90 not taken.
2409 diGenerator.generateLocalVarDebugInfo(node->varName, varAddress);
74
75
2/2
✓ Branch 41 → 42 taken 1242 times.
✓ Branch 41 → 46 taken 1167 times.
2409 if (node->calledInitCtor) {
76 // Call no-args constructor
77
1/2
✓ Branch 43 → 44 taken 1242 times.
✗ Branch 43 → 85 not taken.
1242 generateCtorOrDtorCall(varEntry, node->calledInitCtor, {});
78 // A union with no default field must start in the "unset" tag state, and a union with a default field must
79 // start with that field active. Either way, the tag bits carry real runtime safety meaning (unlike a struct's
80 // all-zero debug-only default), so they must be initialized unconditionally, in every build mode.
81
8/10
✓ Branch 46 → 47 taken 545 times.
✓ Branch 46 → 51 taken 622 times.
✓ Branch 47 → 48 taken 2 times.
✓ Branch 47 → 50 taken 543 times.
✓ Branch 48 → 49 taken 2 times.
✗ Branch 48 → 90 not taken.
✗ Branch 49 → 50 not taken.
✓ Branch 49 → 51 taken 2 times.
✓ Branch 52 → 53 taken 543 times.
✓ Branch 52 → 57 taken 624 times.
1167 } else if (!node->isForEachItem && (cliOptions.buildMode != BuildMode::RELEASE || varSymbolType.is(TY_UNION))) {
82
1/2
✗ Branch 53 → 54 not taken.
✓ Branch 53 → 55 taken 543 times.
543 assert(!node->isCtorCallRequired);
83 // Retrieve default value for lhs symbol type and store it
84
1/2
✓ Branch 55 → 56 taken 543 times.
✗ Branch 55 → 90 not taken.
543 llvm::Constant *defaultValue = getDefaultValueForSymbolType(varSymbolType);
85
1/2
✓ Branch 56 → 57 taken 543 times.
✗ Branch 56 → 90 not taken.
543 insertStore(defaultValue, varAddress);
86 }
87 }
88
1/2
✗ Branch 57 → 58 not taken.
✓ Branch 57 → 59 taken 47835 times.
47835 assert(varAddress != nullptr);
89
90 // Attach the variable name to the LLVM value.
91
2/4
✓ Branch 59 → 60 taken 47835 times.
✗ Branch 59 → 88 not taken.
✓ Branch 60 → 61 taken 47835 times.
✗ Branch 60 → 88 not taken.
47835 varAddress->setName(varEntry->name);
92
93
1/2
✓ Branch 61 → 62 taken 47835 times.
✗ Branch 61 → 89 not taken.
95670 return nullptr;
94 }
95
96 ✗ std::any IRGenerator::visitQualifierLst(const QualifierLstNode *node) {
97 ✗ return nullptr; // Noop
98 }
99
100 1905 std::any IRGenerator::visitModAttr(const ModAttrNode *node) {
101
1/2
✓ Branch 2 → 3 taken 1905 times.
✗ Branch 2 → 6 not taken.
3810 return nullptr; // Noop
102 }
103
104 ✗ std::any IRGenerator::visitTopLevelDefinitionAttr(const TopLevelDefAttrNode *node) {
105 ✗ return nullptr; // Noop
106 }
107
108 303 std::any IRGenerator::visitCaseConstant(const CaseConstantNode *node) {
109
2/2
✓ Branch 2 → 3 taken 147 times.
✓ Branch 2 → 4 taken 156 times.
303 if (node->constant)
110 147 return visit(node->constant);
111
112 156 const SymbolTableEntry *constantEntry = node->entry;
113
4/8
✓ Branch 4 → 5 taken 156 times.
✗ Branch 4 → 12 not taken.
✓ Branch 5 → 6 taken 156 times.
✗ Branch 5 → 11 not taken.
✓ Branch 6 → 7 taken 156 times.
✗ Branch 6 → 11 not taken.
✓ Branch 7 → 8 taken 156 times.
✗ Branch 7 → 11 not taken.
312 return getConst(constantEntry->declNode->getCompileTimeValue(manIdx), node->getEvaluatedSymbolType(manIdx), node);
114 }
115
116 54444 std::any IRGenerator::visitReturnStmt(const ReturnStmtNode *node) {
117 54444 llvm::Value *returnValue = nullptr;
118
2/2
✓ Branch 2 → 3 taken 48834 times.
✓ Branch 2 → 31 taken 5610 times.
54444 if (node->hasReturnValue) { // Return value is attached to the return statement
119 48834 const ExprNode *returnExpr = node->assignExpr;
120
2/2
✓ Branch 3 → 4 taken 501 times.
✓ Branch 3 → 25 taken 48333 times.
48834 if (node->calledCopyCtor) {
121 // Perform a copy
122 501 llvm::Value *originalAddress = resolveAddress(returnExpr);
123 501 llvm::Type *returnTy = node->returnType.toLLVMType(sourceFile);
124
1/2
✓ Branch 9 → 10 taken 501 times.
✗ Branch 9 → 59 not taken.
501 llvm::Value *newAddress = insertAlloca(returnTy);
125
2/4
✓ Branch 14 → 15 taken 501 times.
✗ Branch 14 → 67 not taken.
✓ Branch 15 → 16 taken 501 times.
✗ Branch 15 → 65 not taken.
1503 generateCtorOrDtorCall(newAddress, node->calledCopyCtor, {originalAddress});
126
1/2
✓ Branch 21 → 22 taken 501 times.
✗ Branch 21 → 72 not taken.
501 returnValue = insertLoad(returnTy, newAddress);
127 } else {
128
2/2
✓ Branch 26 → 27 taken 2890 times.
✓ Branch 26 → 29 taken 45443 times.
48333 returnValue = node->returnType.isRef() ? resolveAddress(returnExpr) : resolveValue(returnExpr);
129 }
130 } else { // Try to load result variable value
131
1/2
✓ Branch 33 → 34 taken 5610 times.
✗ Branch 33 → 80 not taken.
16830 const SymbolTableEntry *resultEntry = currentScope->lookup(RETURN_VARIABLE_NAME);
132
2/2
✓ Branch 39 → 40 taken 8 times.
✓ Branch 39 → 50 taken 5602 times.
5610 if (resultEntry != nullptr) {
133 8 llvm::Type *resultSTy = resultEntry->getQualType().toLLVMType(sourceFile);
134 8 llvm::Value *returnValueAddr = getAddress(resultEntry);
135
1/2
✓ Branch 46 → 47 taken 8 times.
✗ Branch 46 → 84 not taken.
8 returnValue = insertLoad(resultSTy, returnValueAddr);
136 }
137 }
138
139 // Clean up all scopes between here and the enclosing function/procedure/lambda body, then terminate the block
140 54444 generateScopeCleanupUpTo(node, currentScope->getFunctionScope());
141 54444 blockAlreadyTerminated = true;
142
143 // Create return instruction
144
2/2
✓ Branch 52 → 53 taken 48842 times.
✓ Branch 52 → 54 taken 5602 times.
54444 if (returnValue != nullptr) {
145 // Return with value
146 48842 builder.CreateRet(returnValue);
147 } else {
148 // Return without value
149 5602 builder.CreateRetVoid();
150 }
151
152
1/2
✓ Branch 55 → 56 taken 54444 times.
✗ Branch 55 → 90 not taken.
108888 return nullptr;
153 }
154
155 972 std::any IRGenerator::visitBreakStmt(const BreakStmtNode *node) {
156
1/2
✓ Branch 3 → 4 taken 972 times.
✗ Branch 3 → 11 not taken.
972 const auto [targetScope, targetBlock] = breakTargets.at(breakTargets.size() - node->breakTimes);
157
158 // Clean up all scopes between here and the loop/switch statement we are breaking out of
159
1/2
✓ Branch 4 → 5 taken 972 times.
✗ Branch 4 → 11 not taken.
972 generateScopeCleanupUpTo(node, targetScope);
160
161 // Jump to destination block
162
1/2
✓ Branch 5 → 6 taken 972 times.
✗ Branch 5 → 11 not taken.
972 insertJump(targetBlock);
163
164
1/2
✓ Branch 6 → 7 taken 972 times.
✗ Branch 6 → 10 not taken.
1944 return nullptr;
165 }
166
167 1836 std::any IRGenerator::visitContinueStmt(const ContinueStmtNode *node) {
168
1/2
✓ Branch 3 → 4 taken 1836 times.
✗ Branch 3 → 11 not taken.
1836 const auto [targetScope, targetBlock] = continueTargets.at(continueTargets.size() - node->continueTimes);
169
170 // Clean up all scopes between here and the loop statement we are continuing
171
1/2
✓ Branch 4 → 5 taken 1836 times.
✗ Branch 4 → 11 not taken.
1836 generateScopeCleanupUpTo(node, targetScope);
172
173 // Jump to destination block
174
1/2
✓ Branch 5 → 6 taken 1836 times.
✗ Branch 5 → 11 not taken.
1836 insertJump(targetBlock);
175
176
1/2
✓ Branch 6 → 7 taken 1836 times.
✗ Branch 6 → 10 not taken.
3672 return nullptr;
177 }
178
179 8 std::any IRGenerator::visitFallthroughStmt(const FallthroughStmtNode *node) {
180 // Jump to destination block
181 8 insertJump(fallthroughBlocks.top());
182
183
1/2
✓ Branch 4 → 5 taken 8 times.
✗ Branch 4 → 8 not taken.
16 return nullptr;
184 }
185
186 7593 std::any IRGenerator::visitAssertStmt(const AssertStmtNode *node) {
187 // Do not generate assertions in release mode
188
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 6 taken 7593 times.
7593 if (cliOptions.buildMode == BuildMode::RELEASE)
189 ✗ return nullptr;
190
191 15168 const auto generateBody = [&] {
192 // Create constant for error message. It is the format string of the fprintf call below, so the condition's source text
193 // must not be mistaken for conversion specifiers
194
1/2
✓ Branch 2 → 3 taken 7575 times.
✗ Branch 2 → 100 not taken.
7575 std::string expressionString = node->expressionString;
195
3/6
✓ Branch 5 → 6 taken 7575 times.
✗ Branch 5 → 65 not taken.
✓ Branch 8 → 9 taken 7575 times.
✗ Branch 8 → 59 not taken.
✓ Branch 9 → 10 taken 7575 times.
✗ Branch 9 → 57 not taken.
22725 CommonUtil::replaceAll(expressionString, "%", "%%");
196
2/4
✓ Branch 14 → 15 taken 7575 times.
✗ Branch 14 → 71 not taken.
✓ Branch 15 → 16 taken 7575 times.
✗ Branch 15 → 69 not taken.
7575 const std::string errorMsg = "Assertion failed: Condition '" + expressionString + "' evaluated to false.\n";
197
4/8
✓ Branch 19 → 20 taken 7575 times.
✗ Branch 19 → 77 not taken.
✓ Branch 20 → 21 taken 7575 times.
✗ Branch 20 → 75 not taken.
✓ Branch 21 → 22 taken 7575 times.
✗ Branch 21 → 73 not taken.
✓ Branch 23 → 24 taken 7575 times.
✗ Branch 23 → 72 not taken.
15150 llvm::GlobalVariable *globalString = builder.CreateGlobalString(errorMsg, getUnusedGlobalName(ANON_GLOBAL_STRING_NAME));
198 // If the output should be comparable, fix alignment to 4 bytes
199
1/2
✓ Branch 27 → 28 taken 7575 times.
✗ Branch 27 → 31 not taken.
7575 if (cliOptions.comparableOutput)
200
2/4
✓ Branch 28 → 29 taken 7575 times.
✗ Branch 28 → 83 not taken.
✓ Branch 29 → 30 taken 7575 times.
✗ Branch 29 → 83 not taken.
7575 globalString->setAlignment(llvm::Align(4));
201 // Print the error message to stderr, like panic does
202
1/2
✓ Branch 31 → 32 taken 7575 times.
✗ Branch 31 → 96 not taken.
7575 llvm::Function *fprintfFct = stdFunctionManager.getFPrintfFct();
203
4/8
✓ Branch 32 → 33 taken 7575 times.
✗ Branch 32 → 87 not taken.
✓ Branch 33 → 34 taken 7575 times.
✗ Branch 33 → 85 not taken.
✓ Branch 35 → 36 taken 7575 times.
✗ Branch 35 → 84 not taken.
✓ Branch 36 → 37 taken 7575 times.
✗ Branch 36 → 84 not taken.
7575 builder.CreateCall(fprintfFct, {getStdErrValue(), globalString});
204 // Print the stack trace of the failed assertion
205
1/2
✗ Branch 38 → 39 not taken.
✓ Branch 38 → 47 taken 7575 times.
7575 if (cliOptions.printsStackTraceOnAbort())
206 ✗ builder.CreateCall(stdFunctionManager.getDumpStacktraceFct(), {builder.getTrue(), builder.getFalse()});
207 // Generate call to exit()
208
1/2
✓ Branch 47 → 48 taken 7575 times.
✗ Branch 47 → 96 not taken.
7575 llvm::Function *exitFct = stdFunctionManager.getExitFct();
209
5/10
✓ Branch 48 → 49 taken 7575 times.
✗ Branch 48 → 95 not taken.
✓ Branch 49 → 50 taken 7575 times.
✗ Branch 49 → 93 not taken.
✓ Branch 51 → 52 taken 7575 times.
✗ Branch 51 → 92 not taken.
✓ Branch 52 → 53 taken 7575 times.
✗ Branch 52 → 92 not taken.
✓ Branch 53 → 54 taken 7575 times.
✗ Branch 53 → 96 not taken.
7575 builder.CreateCall(exitFct, builder.getInt32(EXIT_FAILURE));
210 // Create unreachable instruction
211
1/2
✓ Branch 53 → 54 taken 7575 times.
✗ Branch 53 → 96 not taken.
7575 builder.CreateUnreachable();
212 7575 blockAlreadyTerminated = true;
213 7575 };
214
215 // If we have a compile time decision, only evaluate the respective branch
216
3/4
✓ Branch 6 → 7 taken 7593 times.
✗ Branch 6 → 42 not taken.
✓ Branch 7 → 8 taken 22 times.
✓ Branch 7 → 14 taken 7571 times.
7593 if (node->assignExpr->hasCompileTimeValue(manIdx)) {
217
3/4
✓ Branch 8 → 9 taken 22 times.
✗ Branch 8 → 42 not taken.
✓ Branch 9 → 10 taken 4 times.
✓ Branch 9 → 11 taken 18 times.
22 if (!node->assignExpr->getCompileTimeValue(manIdx).boolValue)
218
1/2
✓ Branch 10 → 11 taken 4 times.
✗ Branch 10 → 42 not taken.
4 generateBody();
219
1/2
✓ Branch 11 → 12 taken 22 times.
✗ Branch 11 → 32 not taken.
44 return nullptr;
220 }
221
222 // Create blocks
223
1/2
✓ Branch 14 → 15 taken 7571 times.
✗ Branch 14 → 42 not taken.
7571 const std::string &codeLine = node->codeLoc.toPrettyLine();
224
2/4
✓ Branch 15 → 16 taken 7571 times.
✗ Branch 15 → 35 not taken.
✓ Branch 16 → 17 taken 7571 times.
✗ Branch 16 → 33 not taken.
7571 llvm::BasicBlock *bThen = createBlock("assert.then." + codeLine);
225
2/4
✓ Branch 18 → 19 taken 7571 times.
✗ Branch 18 → 38 not taken.
✓ Branch 19 → 20 taken 7571 times.
✗ Branch 19 → 36 not taken.
7571 llvm::BasicBlock *bExit = createBlock("assert.exit." + codeLine);
226
227 // Visit the assignExpr
228
1/2
✓ Branch 21 → 22 taken 7571 times.
✗ Branch 21 → 40 not taken.
7571 llvm::Value *condValue = resolveValue(node->assignExpr);
229
230 // Create condition check
231
1/2
✓ Branch 22 → 23 taken 7571 times.
✗ Branch 22 → 40 not taken.
7571 insertCondJump(condValue, bExit, bThen, Likelihood::LIKELY);
232
233 // Switch to then block
234
1/2
✓ Branch 23 → 24 taken 7571 times.
✗ Branch 23 → 40 not taken.
7571 switchToBlock(bThen);
235
1/2
✓ Branch 24 → 25 taken 7571 times.
✗ Branch 24 → 40 not taken.
7571 generateBody();
236
237 // Switch to exit block
238
1/2
✓ Branch 25 → 26 taken 7571 times.
✗ Branch 25 → 40 not taken.
7571 switchToBlock(bExit);
239
240
1/2
✓ Branch 26 → 27 taken 7571 times.
✗ Branch 26 → 39 not taken.
7571 return nullptr;
241 7571 }
242
243 } // namespace spice::compiler
244