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