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 125046 std::any IRGenerator::visitStmtLst(const StmtLstNode *node) {
15 // Generate instructions in the scope
16
2/2
✓ Branch 20 → 4 taken 251108 times.
✓ Branch 20 → 21 taken 125036 times.
501190 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 251102 times.
✓ Branch 6 → 21 taken 6 times.
✓ Branch 7 → 8 taken 251098 times.
✓ Branch 7 → 21 taken 4 times.
251108 if (blockAlreadyTerminated || stmt->unreachable)
19 break;
20
21 // Set source location for debug info
22
1/2
✓ Branch 8 → 9 taken 251098 times.
✗ Branch 8 → 27 not taken.
251098 diGenerator.setSourceLocation(stmt);
23
24 // Visit child
25
1/2
✓ Branch 9 → 10 taken 251098 times.
✗ Branch 9 → 26 not taken.
251098 visit(stmt);
26 }
27
28 // Generate cleanup code of this scope, e.g. dtor calls for struct instances
29 125046 generateScopeCleanup(node);
30
31
1/2
✓ Branch 22 → 23 taken 125046 times.
✗ Branch 22 → 28 not taken.
250092 return nullptr;
32 }
33
34 ✗ std::any IRGenerator::visitTypeAltsLst(const TypeAltsLstNode *node) {
35 ✗ return nullptr; // Noop
36 }
37
38 49175 std::any IRGenerator::visitDeclStmt(const DeclStmtNode *node) {
39 // Get variable entry
40
1/2
✓ Branch 2 → 3 taken 49175 times.
✗ Branch 2 → 90 not taken.
49175 const SymbolTableEntry *varEntry = node->entries.at(manIdx);
41
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 49175 times.
49175 assert(varEntry != nullptr);
42
1/2
✓ Branch 5 → 6 taken 49175 times.
✗ Branch 5 → 90 not taken.
49175 const QualType varSymbolType = varEntry->getQualType();
43
44 // Get LLVM type of variable
45
1/2
✓ Branch 6 → 7 taken 49175 times.
✗ Branch 6 → 90 not taken.
49175 llvm::Type *varTy = varSymbolType.toLLVMType(sourceFile);
46
47 // Check if the declaration is with an assignment or the default value
48 49175 llvm::Value *varAddress = nullptr;
49
2/2
✓ Branch 7 → 8 taken 46732 times.
✓ Branch 7 → 33 taken 2443 times.
49175 if (node->hasAssignment) { // Assignment
50
2/2
✓ Branch 8 → 9 taken 118 times.
✓ Branch 8 → 27 taken 46614 times.
46732 if (node->calledCopyCtor) {
51 // Allocate memory
52
1/2
✓ Branch 12 → 13 taken 118 times.
✗ Branch 12 → 65 not taken.
118 varAddress = insertAlloca(varTy);
53
1/2
✓ Branch 15 → 16 taken 118 times.
✗ Branch 15 → 90 not taken.
118 updateAddress(varEntry, varAddress);
54 // Generate debug info for variable declaration
55
1/2
✓ Branch 16 → 17 taken 118 times.
✗ Branch 16 → 90 not taken.
118 diGenerator.generateLocalVarDebugInfo(node->varName, varAddress);
56 // Call copy ctor
57
1/2
✓ Branch 17 → 18 taken 118 times.
✗ Branch 17 → 90 not taken.
118 llvm::Value *rhsAddress = resolveAddress(node->assignExpr);
58
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 118 times.
118 assert(rhsAddress != nullptr);
59
2/4
✓ Branch 22 → 23 taken 118 times.
✗ Branch 22 → 73 not taken.
✓ Branch 23 → 24 taken 118 times.
✗ Branch 23 → 71 not taken.
354 generateCtorOrDtorCall(varEntry, node->calledCopyCtor, {rhsAddress});
60 } else {
61 // Assign rhs to lhs
62
1/2
✓ Branch 27 → 28 taken 46614 times.
✗ Branch 27 → 78 not taken.
46614 [[maybe_unused]] const LLVMExprResult assignResult = doAssignment(varAddress, varEntry, node->assignExpr, node, true);
63
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 46614 times.
46614 assert(assignResult.entry == varEntry);
64
1/2
✓ Branch 30 → 31 taken 46614 times.
✗ Branch 30 → 78 not taken.
46614 varAddress = getAddress(varEntry);
65
1/2
✓ Branch 31 → 32 taken 46614 times.
✗ Branch 31 → 78 not taken.
46614 updateAddress(varEntry, varAddress);
66 }
67 } else { // Default value
68 // Allocate memory
69
1/2
✓ Branch 36 → 37 taken 2443 times.
✗ Branch 36 → 79 not taken.
2443 varAddress = insertAlloca(varTy);
70
1/2
✓ Branch 39 → 40 taken 2443 times.
✗ Branch 39 → 90 not taken.
2443 updateAddress(varEntry, varAddress);
71
72 // Generate debug info for variable declaration
73
1/2
✓ Branch 40 → 41 taken 2443 times.
✗ Branch 40 → 90 not taken.
2443 diGenerator.generateLocalVarDebugInfo(node->varName, varAddress);
74
75
2/2
✓ Branch 41 → 42 taken 1264 times.
✓ Branch 41 → 46 taken 1179 times.
2443 if (node->calledInitCtor) {
76 // Call no-args constructor
77
1/2
✓ Branch 43 → 44 taken 1264 times.
✗ Branch 43 → 85 not taken.
1264 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 557 times.
✓ Branch 46 → 51 taken 622 times.
✓ Branch 47 → 48 taken 2 times.
✓ Branch 47 → 50 taken 555 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 555 times.
✓ Branch 52 → 57 taken 624 times.
1179 } else if (!node->isForEachItem && (cliOptions.buildMode != BuildMode::RELEASE || varSymbolType.is(TY_UNION))) {
82
1/2
✗ Branch 53 → 54 not taken.
✓ Branch 53 → 55 taken 555 times.
555 assert(!node->isCtorCallRequired);
83 // Retrieve default value for lhs symbol type and store it
84
1/2
✓ Branch 55 → 56 taken 555 times.
✗ Branch 55 → 90 not taken.
555 llvm::Constant *defaultValue = getDefaultValueForSymbolType(varSymbolType);
85
1/2
✓ Branch 56 → 57 taken 555 times.
✗ Branch 56 → 90 not taken.
555 insertStore(defaultValue, varAddress);
86 }
87 }
88
1/2
✗ Branch 57 → 58 not taken.
✓ Branch 57 → 59 taken 49175 times.
49175 assert(varAddress != nullptr);
89
90 // Attach the variable name to the LLVM value.
91
2/4
✓ Branch 59 → 60 taken 49175 times.
✗ Branch 59 → 88 not taken.
✓ Branch 60 → 61 taken 49175 times.
✗ Branch 60 → 88 not taken.
49175 varAddress->setName(varEntry->name);
92
93
1/2
✓ Branch 61 → 62 taken 49175 times.
✗ Branch 61 → 89 not taken.
98350 return nullptr;
94 }
95
96 ✗ std::any IRGenerator::visitQualifierLst(const QualifierLstNode *node) {
97 ✗ return nullptr; // Noop
98 }
99
100 1941 std::any IRGenerator::visitModAttr(const ModAttrNode *node) {
101
1/2
✓ Branch 2 → 3 taken 1941 times.
✗ Branch 2 → 6 not taken.
3882 return nullptr; // Noop
102 }
103
104 ✗ std::any IRGenerator::visitTopLevelDefinitionAttr(const TopLevelDefAttrNode *node) {
105 ✗ return nullptr; // Noop
106 }
107
108 315 std::any IRGenerator::visitCaseConstant(const CaseConstantNode *node) {
109
2/2
✓ Branch 2 → 3 taken 147 times.
✓ Branch 2 → 4 taken 168 times.
315 if (node->constant)
110 147 return visit(node->constant);
111
112 168 const SymbolTableEntry *constantEntry = node->entry;
113
4/8
✓ Branch 4 → 5 taken 168 times.
✗ Branch 4 → 12 not taken.
✓ Branch 5 → 6 taken 168 times.
✗ Branch 5 → 11 not taken.
✓ Branch 6 → 7 taken 168 times.
✗ Branch 6 → 11 not taken.
✓ Branch 7 → 8 taken 168 times.
✗ Branch 7 → 11 not taken.
336 return getConst(constantEntry->declNode->getCompileTimeValue(manIdx), node->getEvaluatedSymbolType(manIdx), node);
114 }
115
116 56322 std::any IRGenerator::visitReturnStmt(const ReturnStmtNode *node) {
117 56322 llvm::Value *returnValue = nullptr;
118
2/2
✓ Branch 2 → 3 taken 50628 times.
✓ Branch 2 → 31 taken 5694 times.
56322 if (node->hasReturnValue) { // Return value is attached to the return statement
119 50628 const ExprNode *returnExpr = node->assignExpr;
120
2/2
✓ Branch 3 → 4 taken 515 times.
✓ Branch 3 → 25 taken 50113 times.
50628 if (node->calledCopyCtor) {
121 // Perform a copy
122 515 llvm::Value *originalAddress = resolveAddress(returnExpr);
123 515 llvm::Type *returnTy = node->returnType.toLLVMType(sourceFile);
124
1/2
✓ Branch 9 → 10 taken 515 times.
✗ Branch 9 → 60 not taken.
515 llvm::Value *newAddress = insertAlloca(returnTy);
125
2/4
✓ Branch 14 → 15 taken 515 times.
✗ Branch 14 → 68 not taken.
✓ Branch 15 → 16 taken 515 times.
✗ Branch 15 → 66 not taken.
1545 generateCtorOrDtorCall(newAddress, node->calledCopyCtor, {originalAddress});
126
1/2
✓ Branch 21 → 22 taken 515 times.
✗ Branch 21 → 73 not taken.
515 returnValue = insertLoad(returnTy, newAddress);
127 } else {
128
2/2
✓ Branch 26 → 27 taken 2948 times.
✓ Branch 26 → 29 taken 47165 times.
50113 returnValue = node->returnType.isRef() ? resolveAddress(returnExpr) : resolveValue(returnExpr);
129 }
130 } else { // Try to load result variable value
131
1/2
✓ Branch 33 → 34 taken 5694 times.
✗ Branch 33 → 81 not taken.
17082 const SymbolTableEntry *resultEntry = currentScope->lookup(RETURN_VARIABLE_NAME);
132
2/2
✓ Branch 39 → 40 taken 8 times.
✓ Branch 39 → 50 taken 5686 times.
5694 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 → 85 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. A local
140 // variable, that is returned without a copy, is destructed by the caller
141 56322 generateScopeCleanupUpTo(node, currentScope->getFunctionScope(), node->returnedLocals.at(manIdx));
142 56322 blockAlreadyTerminated = true;
143
144 // Create return instruction
145
2/2
✓ Branch 53 → 54 taken 50636 times.
✓ Branch 53 → 55 taken 5686 times.
56322 if (returnValue != nullptr) {
146 // Return with value
147 50636 builder.CreateRet(returnValue);
148 } else {
149 // Return without value
150 5686 builder.CreateRetVoid();
151 }
152
153
1/2
✓ Branch 56 → 57 taken 56322 times.
✗ Branch 56 → 91 not taken.
112644 return nullptr;
154 }
155
156 994 std::any IRGenerator::visitBreakStmt(const BreakStmtNode *node) {
157
1/2
✓ Branch 3 → 4 taken 994 times.
✗ Branch 3 → 11 not taken.
994 const auto [targetScope, targetBlock] = breakTargets.at(breakTargets.size() - node->breakTimes);
158
159 // Clean up all scopes between here and the loop/switch statement we are breaking out of
160
1/2
✓ Branch 4 → 5 taken 994 times.
✗ Branch 4 → 11 not taken.
994 generateScopeCleanupUpTo(node, targetScope);
161
162 // Jump to destination block
163
1/2
✓ Branch 5 → 6 taken 994 times.
✗ Branch 5 → 11 not taken.
994 insertJump(targetBlock);
164
165
1/2
✓ Branch 6 → 7 taken 994 times.
✗ Branch 6 → 10 not taken.
1988 return nullptr;
166 }
167
168 1862 std::any IRGenerator::visitContinueStmt(const ContinueStmtNode *node) {
169
1/2
✓ Branch 3 → 4 taken 1862 times.
✗ Branch 3 → 11 not taken.
1862 const auto [targetScope, targetBlock] = continueTargets.at(continueTargets.size() - node->continueTimes);
170
171 // Clean up all scopes between here and the loop statement we are continuing
172
1/2
✓ Branch 4 → 5 taken 1862 times.
✗ Branch 4 → 11 not taken.
1862 generateScopeCleanupUpTo(node, targetScope);
173
174 // Jump to destination block
175
1/2
✓ Branch 5 → 6 taken 1862 times.
✗ Branch 5 → 11 not taken.
1862 insertJump(targetBlock);
176
177
1/2
✓ Branch 6 → 7 taken 1862 times.
✗ Branch 6 → 10 not taken.
3724 return nullptr;
178 }
179
180 8 std::any IRGenerator::visitFallthroughStmt(const FallthroughStmtNode *node) {
181 // Jump to destination block
182 8 insertJump(fallthroughBlocks.top());
183
184
1/2
✓ Branch 4 → 5 taken 8 times.
✗ Branch 4 → 8 not taken.
16 return nullptr;
185 }
186
187 7753 std::any IRGenerator::visitAssertStmt(const AssertStmtNode *node) {
188 // Do not generate assertions in release mode
189
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 6 taken 7753 times.
7753 if (cliOptions.buildMode == BuildMode::RELEASE)
190 ✗ return nullptr;
191
192 15488 const auto generateBody = [&] {
193 // Create constant for error message. It is the format string of the fprintf call below, so the condition's source text
194 // must not be mistaken for conversion specifiers
195
1/2
✓ Branch 2 → 3 taken 7735 times.
✗ Branch 2 → 100 not taken.
7735 std::string expressionString = node->expressionString;
196
3/6
✓ Branch 5 → 6 taken 7735 times.
✗ Branch 5 → 65 not taken.
✓ Branch 8 → 9 taken 7735 times.
✗ Branch 8 → 59 not taken.
✓ Branch 9 → 10 taken 7735 times.
✗ Branch 9 → 57 not taken.
23205 CommonUtil::replaceAll(expressionString, "%", "%%");
197
2/4
✓ Branch 14 → 15 taken 7735 times.
✗ Branch 14 → 71 not taken.
✓ Branch 15 → 16 taken 7735 times.
✗ Branch 15 → 69 not taken.
7735 const std::string errorMsg = "Assertion failed: Condition '" + expressionString + "' evaluated to false.\n";
198
4/8
✓ Branch 19 → 20 taken 7735 times.
✗ Branch 19 → 77 not taken.
✓ Branch 20 → 21 taken 7735 times.
✗ Branch 20 → 75 not taken.
✓ Branch 21 → 22 taken 7735 times.
✗ Branch 21 → 73 not taken.
✓ Branch 23 → 24 taken 7735 times.
✗ Branch 23 → 72 not taken.
15470 llvm::GlobalVariable *globalString = builder.CreateGlobalString(errorMsg, getUnusedGlobalName(ANON_GLOBAL_STRING_NAME));
199 // If the output should be comparable, fix alignment to 4 bytes
200
1/2
✓ Branch 27 → 28 taken 7735 times.
✗ Branch 27 → 31 not taken.
7735 if (cliOptions.comparableOutput)
201
2/4
✓ Branch 28 → 29 taken 7735 times.
✗ Branch 28 → 83 not taken.
✓ Branch 29 → 30 taken 7735 times.
✗ Branch 29 → 83 not taken.
7735 globalString->setAlignment(llvm::Align(4));
202 // Print the error message to stderr, like panic does
203
1/2
✓ Branch 31 → 32 taken 7735 times.
✗ Branch 31 → 96 not taken.
7735 llvm::Function *fprintfFct = stdFunctionManager.getFPrintfFct();
204
4/8
✓ Branch 32 → 33 taken 7735 times.
✗ Branch 32 → 87 not taken.
✓ Branch 33 → 34 taken 7735 times.
✗ Branch 33 → 85 not taken.
✓ Branch 35 → 36 taken 7735 times.
✗ Branch 35 → 84 not taken.
✓ Branch 36 → 37 taken 7735 times.
✗ Branch 36 → 84 not taken.
7735 builder.CreateCall(fprintfFct, {getStdErrValue(), globalString});
205 // Print the stack trace of the failed assertion
206
1/2
✗ Branch 38 → 39 not taken.
✓ Branch 38 → 47 taken 7735 times.
7735 if (cliOptions.printsStackTraceOnAbort())
207 ✗ builder.CreateCall(stdFunctionManager.getDumpStacktraceFct(), {builder.getTrue(), builder.getFalse()});
208 // Generate call to exit()
209
1/2
✓ Branch 47 → 48 taken 7735 times.
✗ Branch 47 → 96 not taken.
7735 llvm::Function *exitFct = stdFunctionManager.getExitFct();
210
5/10
✓ Branch 48 → 49 taken 7735 times.
✗ Branch 48 → 95 not taken.
✓ Branch 49 → 50 taken 7735 times.
✗ Branch 49 → 93 not taken.
✓ Branch 51 → 52 taken 7735 times.
✗ Branch 51 → 92 not taken.
✓ Branch 52 → 53 taken 7735 times.
✗ Branch 52 → 92 not taken.
✓ Branch 53 → 54 taken 7735 times.
✗ Branch 53 → 96 not taken.
7735 builder.CreateCall(exitFct, builder.getInt32(EXIT_FAILURE));
211 // Create unreachable instruction
212
1/2
✓ Branch 53 → 54 taken 7735 times.
✗ Branch 53 → 96 not taken.
7735 builder.CreateUnreachable();
213 7735 blockAlreadyTerminated = true;
214 7735 };
215
216 // If we have a compile time decision, only evaluate the respective branch
217
3/4
✓ Branch 6 → 7 taken 7753 times.
✗ Branch 6 → 42 not taken.
✓ Branch 7 → 8 taken 22 times.
✓ Branch 7 → 14 taken 7731 times.
7753 if (node->assignExpr->hasCompileTimeValue(manIdx)) {
218
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)
219
1/2
✓ Branch 10 → 11 taken 4 times.
✗ Branch 10 → 42 not taken.
4 generateBody();
220
1/2
✓ Branch 11 → 12 taken 22 times.
✗ Branch 11 → 32 not taken.
44 return nullptr;
221 }
222
223 // Create blocks
224
1/2
✓ Branch 14 → 15 taken 7731 times.
✗ Branch 14 → 42 not taken.
7731 const std::string &codeLine = node->codeLoc.toPrettyLine();
225
2/4
✓ Branch 15 → 16 taken 7731 times.
✗ Branch 15 → 35 not taken.
✓ Branch 16 → 17 taken 7731 times.
✗ Branch 16 → 33 not taken.
7731 llvm::BasicBlock *bThen = createBlock("assert.then." + codeLine);
226
2/4
✓ Branch 18 → 19 taken 7731 times.
✗ Branch 18 → 38 not taken.
✓ Branch 19 → 20 taken 7731 times.
✗ Branch 19 → 36 not taken.
7731 llvm::BasicBlock *bExit = createBlock("assert.exit." + codeLine);
227
228 // Visit the assignExpr
229
1/2
✓ Branch 21 → 22 taken 7731 times.
✗ Branch 21 → 40 not taken.
7731 llvm::Value *condValue = resolveValue(node->assignExpr);
230
231 // Create condition check
232
1/2
✓ Branch 22 → 23 taken 7731 times.
✗ Branch 22 → 40 not taken.
7731 insertCondJump(condValue, bExit, bThen, Likelihood::LIKELY);
233
234 // Switch to then block
235
1/2
✓ Branch 23 → 24 taken 7731 times.
✗ Branch 23 → 40 not taken.
7731 switchToBlock(bThen);
236
1/2
✓ Branch 24 → 25 taken 7731 times.
✗ Branch 24 → 40 not taken.
7731 generateBody();
237
238 // Switch to exit block
239
1/2
✓ Branch 25 → 26 taken 7731 times.
✗ Branch 25 → 40 not taken.
7731 switchToBlock(bExit);
240
241
1/2
✓ Branch 26 → 27 taken 7731 times.
✗ Branch 26 → 39 not taken.
7731 return nullptr;
242 7731 }
243
244 } // namespace spice::compiler
245