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 227903 std::any IRGenerator::visitStmtLst(const StmtLstNode *node) {
15 // Generate instructions in the scope
16
2/2
✓ Branch 20 → 4 taken 447896 times.
✓ Branch 20 → 21 taken 227853 times.
903652 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 447850 times.
✓ Branch 6 → 21 taken 46 times.
✓ Branch 7 → 8 taken 447846 times.
✓ Branch 7 → 21 taken 4 times.
447896 if (blockAlreadyTerminated || stmt->unreachable)
19 break;
20
21 // Set source location for debug info
22
1/2
✓ Branch 8 → 9 taken 447846 times.
✗ Branch 8 → 27 not taken.
447846 diGenerator.setSourceLocation(stmt);
23
24 // Visit child
25
1/2
✓ Branch 9 → 10 taken 447846 times.
✗ Branch 9 → 26 not taken.
447846 visit(stmt);
26 }
27
28 // Generate cleanup code of this scope, e.g. dtor calls for struct instances
29 227903 generateScopeCleanup(node);
30
31
1/2
✓ Branch 22 → 23 taken 227903 times.
✗ Branch 22 → 28 not taken.
455806 return nullptr;
32 }
33
34 ✗ std::any IRGenerator::visitTypeAltsLst(const TypeAltsLstNode *node) {
35 ✗ return nullptr; // Noop
36 }
37
38 90258 std::any IRGenerator::visitDeclStmt(const DeclStmtNode *node) {
39 // Get variable entry
40
1/2
✓ Branch 2 → 3 taken 90258 times.
✗ Branch 2 → 90 not taken.
90258 const SymbolTableEntry *varEntry = node->entries.at(manIdx);
41
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 90258 times.
90258 assert(varEntry != nullptr);
42
1/2
✓ Branch 5 → 6 taken 90258 times.
✗ Branch 5 → 90 not taken.
90258 const QualType varSymbolType = varEntry->getQualType();
43
44 // Get LLVM type of variable
45
1/2
✓ Branch 6 → 7 taken 90258 times.
✗ Branch 6 → 90 not taken.
90258 llvm::Type *varTy = varSymbolType.toLLVMType(sourceFile);
46
47 // Check if the declaration is with an assignment or the default value
48 90258 llvm::Value *varAddress = nullptr;
49
2/2
✓ Branch 7 → 8 taken 83616 times.
✓ Branch 7 → 33 taken 6642 times.
90258 if (node->hasAssignment) { // Assignment
50
2/2
✓ Branch 8 → 9 taken 912 times.
✓ Branch 8 → 27 taken 82704 times.
83616 if (node->calledCopyCtor) {
51 // Allocate memory
52
1/2
✓ Branch 12 → 13 taken 912 times.
✗ Branch 12 → 65 not taken.
912 varAddress = insertAlloca(varTy);
53
1/2
✓ Branch 15 → 16 taken 912 times.
✗ Branch 15 → 90 not taken.
912 updateAddress(varEntry, varAddress);
54 // Generate debug info for variable declaration
55
1/2
✓ Branch 16 → 17 taken 912 times.
✗ Branch 16 → 90 not taken.
912 diGenerator.generateLocalVarDebugInfo(node->varName, varAddress);
56 // Call copy ctor
57
1/2
✓ Branch 17 → 18 taken 912 times.
✗ Branch 17 → 90 not taken.
912 llvm::Value *rhsAddress = resolveAddress(node->assignExpr);
58
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 912 times.
912 assert(rhsAddress != nullptr);
59
2/4
✓ Branch 22 → 23 taken 912 times.
✗ Branch 22 → 73 not taken.
✓ Branch 23 → 24 taken 912 times.
✗ Branch 23 → 71 not taken.
2736 generateCtorOrDtorCall(varEntry, node->calledCopyCtor, {rhsAddress});
60 } else {
61 // Assign rhs to lhs
62
1/2
✓ Branch 27 → 28 taken 82704 times.
✗ Branch 27 → 78 not taken.
82704 [[maybe_unused]] const LLVMExprResult assignResult = doAssignment(varAddress, varEntry, node->assignExpr, node, true);
63
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 82704 times.
82704 assert(assignResult.entry == varEntry);
64
1/2
✓ Branch 30 → 31 taken 82704 times.
✗ Branch 30 → 78 not taken.
82704 varAddress = getAddress(varEntry);
65
1/2
✓ Branch 31 → 32 taken 82704 times.
✗ Branch 31 → 78 not taken.
82704 updateAddress(varEntry, varAddress);
66 }
67 } else { // Default value
68 // Allocate memory
69
1/2
✓ Branch 36 → 37 taken 6642 times.
✗ Branch 36 → 79 not taken.
6642 varAddress = insertAlloca(varTy);
70
1/2
✓ Branch 39 → 40 taken 6642 times.
✗ Branch 39 → 90 not taken.
6642 updateAddress(varEntry, varAddress);
71
72 // Generate debug info for variable declaration
73
1/2
✓ Branch 40 → 41 taken 6642 times.
✗ Branch 40 → 90 not taken.
6642 diGenerator.generateLocalVarDebugInfo(node->varName, varAddress);
74
75
2/2
✓ Branch 41 → 42 taken 2632 times.
✓ Branch 41 → 46 taken 4010 times.
6642 if (node->calledInitCtor) {
76 // Call no-args constructor
77
1/2
✓ Branch 43 → 44 taken 2632 times.
✗ Branch 43 → 85 not taken.
2632 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 2180 times.
✓ Branch 46 → 51 taken 1830 times.
✓ Branch 47 → 48 taken 2 times.
✓ Branch 47 → 50 taken 2178 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 2178 times.
✓ Branch 52 → 57 taken 1832 times.
4010 } else if (!node->isForEachItem && (cliOptions.buildMode != BuildMode::RELEASE || varSymbolType.is(TY_UNION))) {
82
1/2
✗ Branch 53 → 54 not taken.
✓ Branch 53 → 55 taken 2178 times.
2178 assert(!node->isCtorCallRequired);
83 // Retrieve default value for lhs symbol type and store it
84
1/2
✓ Branch 55 → 56 taken 2178 times.
✗ Branch 55 → 90 not taken.
2178 llvm::Constant *defaultValue = getDefaultValueForSymbolType(varSymbolType);
85
1/2
✓ Branch 56 → 57 taken 2178 times.
✗ Branch 56 → 90 not taken.
2178 insertStore(defaultValue, varAddress);
86 }
87 }
88
1/2
✗ Branch 57 → 58 not taken.
✓ Branch 57 → 59 taken 90258 times.
90258 assert(varAddress != nullptr);
89
90 // Attach the variable name to the LLVM value.
91
2/4
✓ Branch 59 → 60 taken 90258 times.
✗ Branch 59 → 88 not taken.
✓ Branch 60 → 61 taken 90258 times.
✗ Branch 60 → 88 not taken.
90258 varAddress->setName(varEntry->name);
92
93
1/2
✓ Branch 61 → 62 taken 90258 times.
✗ Branch 61 → 89 not taken.
180516 return nullptr;
94 }
95
96 ✗ std::any IRGenerator::visitQualifierLst(const QualifierLstNode *node) {
97 ✗ return nullptr; // Noop
98 }
99
100 2218 std::any IRGenerator::visitModAttr(const ModAttrNode *node) {
101
1/2
✓ Branch 2 → 3 taken 2218 times.
✗ Branch 2 → 6 not taken.
4436 return nullptr; // Noop
102 }
103
104 ✗ std::any IRGenerator::visitTopLevelDefinitionAttr(const TopLevelDefAttrNode *node) {
105 ✗ return nullptr; // Noop
106 }
107
108 4055 std::any IRGenerator::visitCaseConstant(const CaseConstantNode *node) {
109
2/2
✓ Branch 2 → 3 taken 131 times.
✓ Branch 2 → 4 taken 3924 times.
4055 if (node->constant)
110 131 return visit(node->constant);
111
112 3924 const SymbolTableEntry *constantEntry = node->entry;
113
4/8
✓ Branch 4 → 5 taken 3924 times.
✗ Branch 4 → 12 not taken.
✓ Branch 5 → 6 taken 3924 times.
✗ Branch 5 → 11 not taken.
✓ Branch 6 → 7 taken 3924 times.
✗ Branch 6 → 11 not taken.
✓ Branch 7 → 8 taken 3924 times.
✗ Branch 7 → 11 not taken.
7848 return getConst(constantEntry->declNode->getCompileTimeValue(manIdx), node->getEvaluatedSymbolType(manIdx), node);
114 }
115
116 104629 std::any IRGenerator::visitReturnStmt(const ReturnStmtNode *node) {
117 104629 llvm::Value *returnValue = nullptr;
118
2/2
✓ Branch 2 → 3 taken 95222 times.
✓ Branch 2 → 31 taken 9407 times.
104629 if (node->hasReturnValue) { // Return value is attached to the return statement
119 95222 const ExprNode *returnExpr = node->assignExpr;
120
2/2
✓ Branch 3 → 4 taken 560 times.
✓ Branch 3 → 25 taken 94662 times.
95222 if (node->calledCopyCtor) {
121 // Perform a copy
122 560 llvm::Value *originalAddress = resolveAddress(returnExpr);
123 560 llvm::Type *returnTy = node->returnType.toLLVMType(sourceFile);
124
1/2
✓ Branch 9 → 10 taken 560 times.
✗ Branch 9 → 59 not taken.
560 llvm::Value *newAddress = insertAlloca(returnTy);
125
2/4
✓ Branch 14 → 15 taken 560 times.
✗ Branch 14 → 67 not taken.
✓ Branch 15 → 16 taken 560 times.
✗ Branch 15 → 65 not taken.
1680 generateCtorOrDtorCall(newAddress, node->calledCopyCtor, {originalAddress});
126
1/2
✓ Branch 21 → 22 taken 560 times.
✗ Branch 21 → 72 not taken.
560 returnValue = insertLoad(returnTy, newAddress);
127 } else {
128
2/2
✓ Branch 26 → 27 taken 9640 times.
✓ Branch 26 → 29 taken 85022 times.
94662 returnValue = node->returnType.isRef() ? resolveAddress(returnExpr) : resolveValue(returnExpr);
129 }
130 } else { // Try to load result variable value
131
1/2
✓ Branch 33 → 34 taken 9407 times.
✗ Branch 33 → 80 not taken.
28221 const SymbolTableEntry *resultEntry = currentScope->lookup(RETURN_VARIABLE_NAME);
132
2/2
✓ Branch 39 → 40 taken 20 times.
✓ Branch 39 → 50 taken 9387 times.
9407 if (resultEntry != nullptr) {
133 20 llvm::Type *resultSTy = resultEntry->getQualType().toLLVMType(sourceFile);
134 20 llvm::Value *returnValueAddr = getAddress(resultEntry);
135
1/2
✓ Branch 46 → 47 taken 20 times.
✗ Branch 46 → 84 not taken.
20 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 104629 generateScopeCleanupUpTo(node, currentScope->getFunctionScope());
141 104629 blockAlreadyTerminated = true;
142
143 // Create return instruction
144
2/2
✓ Branch 52 → 53 taken 95242 times.
✓ Branch 52 → 54 taken 9387 times.
104629 if (returnValue != nullptr) {
145 // Return with value
146 95242 builder.CreateRet(returnValue);
147 } else {
148 // Return without value
149 9387 builder.CreateRetVoid();
150 }
151
152
1/2
✓ Branch 55 → 56 taken 104629 times.
✗ Branch 55 → 90 not taken.
209258 return nullptr;
153 }
154
155 1328 std::any IRGenerator::visitBreakStmt(const BreakStmtNode *node) {
156
1/2
✓ Branch 3 → 4 taken 1328 times.
✗ Branch 3 → 11 not taken.
1328 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 1328 times.
✗ Branch 4 → 11 not taken.
1328 generateScopeCleanupUpTo(node, targetScope);
160
161 // Jump to destination block
162
1/2
✓ Branch 5 → 6 taken 1328 times.
✗ Branch 5 → 11 not taken.
1328 insertJump(targetBlock);
163
164
1/2
✓ Branch 6 → 7 taken 1328 times.
✗ Branch 6 → 10 not taken.
2656 return nullptr;
165 }
166
167 2110 std::any IRGenerator::visitContinueStmt(const ContinueStmtNode *node) {
168
1/2
✓ Branch 3 → 4 taken 2110 times.
✗ Branch 3 → 11 not taken.
2110 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 2110 times.
✗ Branch 4 → 11 not taken.
2110 generateScopeCleanupUpTo(node, targetScope);
172
173 // Jump to destination block
174
1/2
✓ Branch 5 → 6 taken 2110 times.
✗ Branch 5 → 11 not taken.
2110 insertJump(targetBlock);
175
176
1/2
✓ Branch 6 → 7 taken 2110 times.
✗ Branch 6 → 10 not taken.
4220 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 17771 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 17771 times.
17771 if (cliOptions.buildMode == BuildMode::RELEASE)
189 ✗ return nullptr;
190
191 35504 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 17733 times.
✗ Branch 2 → 100 not taken.
17733 std::string expressionString = node->expressionString;
195
3/6
✓ Branch 5 → 6 taken 17733 times.
✗ Branch 5 → 65 not taken.
✓ Branch 8 → 9 taken 17733 times.
✗ Branch 8 → 59 not taken.
✓ Branch 9 → 10 taken 17733 times.
✗ Branch 9 → 57 not taken.
53199 CommonUtil::replaceAll(expressionString, "%", "%%");
196
2/4
✓ Branch 14 → 15 taken 17733 times.
✗ Branch 14 → 71 not taken.
✓ Branch 15 → 16 taken 17733 times.
✗ Branch 15 → 69 not taken.
17733 const std::string errorMsg = "Assertion failed: Condition '" + expressionString + "' evaluated to false.\n";
197
4/8
✓ Branch 19 → 20 taken 17733 times.
✗ Branch 19 → 77 not taken.
✓ Branch 20 → 21 taken 17733 times.
✗ Branch 20 → 75 not taken.
✓ Branch 21 → 22 taken 17733 times.
✗ Branch 21 → 73 not taken.
✓ Branch 23 → 24 taken 17733 times.
✗ Branch 23 → 72 not taken.
35466 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 17733 times.
✗ Branch 27 → 31 not taken.
17733 if (cliOptions.comparableOutput)
200
2/4
✓ Branch 28 → 29 taken 17733 times.
✗ Branch 28 → 83 not taken.
✓ Branch 29 → 30 taken 17733 times.
✗ Branch 29 → 83 not taken.
17733 globalString->setAlignment(llvm::Align(4));
201 // Print the error message to stderr, like panic does
202
1/2
✓ Branch 31 → 32 taken 17733 times.
✗ Branch 31 → 96 not taken.
17733 llvm::Function *fprintfFct = stdFunctionManager.getFPrintfFct();
203
4/8
✓ Branch 32 → 33 taken 17733 times.
✗ Branch 32 → 87 not taken.
✓ Branch 33 → 34 taken 17733 times.
✗ Branch 33 → 85 not taken.
✓ Branch 35 → 36 taken 17733 times.
✗ Branch 35 → 84 not taken.
✓ Branch 36 → 37 taken 17733 times.
✗ Branch 36 → 84 not taken.
17733 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 17733 times.
17733 if (cliOptions.printsStackTraceOnAbort())
206 ✗ builder.CreateCall(stdFunctionManager.getDumpStacktraceFct(), {builder.getTrue(), builder.getFalse()});
207 // Generate call to exit()
208
1/2
✓ Branch 47 → 48 taken 17733 times.
✗ Branch 47 → 96 not taken.
17733 llvm::Function *exitFct = stdFunctionManager.getExitFct();
209
5/10
✓ Branch 48 → 49 taken 17733 times.
✗ Branch 48 → 95 not taken.
✓ Branch 49 → 50 taken 17733 times.
✗ Branch 49 → 93 not taken.
✓ Branch 51 → 52 taken 17733 times.
✗ Branch 51 → 92 not taken.
✓ Branch 52 → 53 taken 17733 times.
✗ Branch 52 → 92 not taken.
✓ Branch 53 → 54 taken 17733 times.
✗ Branch 53 → 96 not taken.
17733 builder.CreateCall(exitFct, builder.getInt32(EXIT_FAILURE));
210 // Create unreachable instruction
211
1/2
✓ Branch 53 → 54 taken 17733 times.
✗ Branch 53 → 96 not taken.
17733 builder.CreateUnreachable();
212 17733 blockAlreadyTerminated = true;
213 17733 };
214
215 // If we have a compile time decision, only evaluate the respective branch
216
3/4
✓ Branch 6 → 7 taken 17771 times.
✗ Branch 6 → 42 not taken.
✓ Branch 7 → 8 taken 88 times.
✓ Branch 7 → 14 taken 17683 times.
17771 if (node->assignExpr->hasCompileTimeValue(manIdx)) {
217
3/4
✓ Branch 8 → 9 taken 88 times.
✗ Branch 8 → 42 not taken.
✓ Branch 9 → 10 taken 50 times.
✓ Branch 9 → 11 taken 38 times.
88 if (!node->assignExpr->getCompileTimeValue(manIdx).boolValue)
218
1/2
✓ Branch 10 → 11 taken 50 times.
✗ Branch 10 → 42 not taken.
50 generateBody();
219
1/2
✓ Branch 11 → 12 taken 88 times.
✗ Branch 11 → 32 not taken.
176 return nullptr;
220 }
221
222 // Create blocks
223
1/2
✓ Branch 14 → 15 taken 17683 times.
✗ Branch 14 → 42 not taken.
17683 const std::string &codeLine = node->codeLoc.toPrettyLine();
224
2/4
✓ Branch 15 → 16 taken 17683 times.
✗ Branch 15 → 35 not taken.
✓ Branch 16 → 17 taken 17683 times.
✗ Branch 16 → 33 not taken.
17683 llvm::BasicBlock *bThen = createBlock("assert.then." + codeLine);
225
2/4
✓ Branch 18 → 19 taken 17683 times.
✗ Branch 18 → 38 not taken.
✓ Branch 19 → 20 taken 17683 times.
✗ Branch 19 → 36 not taken.
17683 llvm::BasicBlock *bExit = createBlock("assert.exit." + codeLine);
226
227 // Visit the assignExpr
228
1/2
✓ Branch 21 → 22 taken 17683 times.
✗ Branch 21 → 40 not taken.
17683 llvm::Value *condValue = resolveValue(node->assignExpr);
229
230 // Create condition check
231
1/2
✓ Branch 22 → 23 taken 17683 times.
✗ Branch 22 → 40 not taken.
17683 insertCondJump(condValue, bExit, bThen, Likelihood::LIKELY);
232
233 // Switch to then block
234
1/2
✓ Branch 23 → 24 taken 17683 times.
✗ Branch 23 → 40 not taken.
17683 switchToBlock(bThen);
235
1/2
✓ Branch 24 → 25 taken 17683 times.
✗ Branch 24 → 40 not taken.
17683 generateBody();
236
237 // Switch to exit block
238
1/2
✓ Branch 25 → 26 taken 17683 times.
✗ Branch 25 → 40 not taken.
17683 switchToBlock(bExit);
239
240
1/2
✓ Branch 26 → 27 taken 17683 times.
✗ Branch 26 → 39 not taken.
17683 return nullptr;
241 17683 }
242
243 } // namespace spice::compiler
244