GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 93.9% 108 / 0 / 115
Functions: 76.9% 10 / 0 / 13
Branches: 55.0% 120 / 0 / 218

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
9 #include <llvm/IR/Module.h>
10
11 namespace spice::compiler {
12
13 162841 std::any IRGenerator::visitStmtLst(const StmtLstNode *node) {
14 // Generate instructions in the scope
15
2/2
✓ Branch 20 → 4 taken 321141 times.
✓ Branch 20 → 21 taken 162801 times.
646783 for (const StmtNode *stmt : node->statements) {
16 // Check if we can cancel generating instructions for this code branch
17
4/4
✓ Branch 6 → 7 taken 321105 times.
✓ Branch 6 → 21 taken 36 times.
✓ Branch 7 → 8 taken 321101 times.
✓ Branch 7 → 21 taken 4 times.
321141 if (blockAlreadyTerminated || stmt->unreachable)
18 break;
19
20 // Set source location for debug info
21
1/2
✓ Branch 8 → 9 taken 321101 times.
✗ Branch 8 → 27 not taken.
321101 diGenerator.setSourceLocation(stmt);
22
23 // Visit child
24
1/2
✓ Branch 9 → 10 taken 321101 times.
✗ Branch 9 → 26 not taken.
321101 visit(stmt);
25 }
26
27 // Generate cleanup code of this scope, e.g. dtor calls for struct instances
28 162841 generateScopeCleanup(node);
29
30
1/2
✓ Branch 22 → 23 taken 162841 times.
✗ Branch 22 → 28 not taken.
325682 return nullptr;
31 }
32
33 std::any IRGenerator::visitTypeAltsLst(const TypeAltsLstNode *node) {
34 return nullptr; // Noop
35 }
36
37 64024 std::any IRGenerator::visitDeclStmt(const DeclStmtNode *node) {
38 // Get variable entry
39
1/2
✓ Branch 2 → 3 taken 64024 times.
✗ Branch 2 → 88 not taken.
64024 const SymbolTableEntry *varEntry = node->entries.at(manIdx);
40
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 64024 times.
64024 assert(varEntry != nullptr);
41
1/2
✓ Branch 5 → 6 taken 64024 times.
✗ Branch 5 → 88 not taken.
64024 const QualType varSymbolType = varEntry->getQualType();
42
43 // Get LLVM type of variable
44
1/2
✓ Branch 6 → 7 taken 64024 times.
✗ Branch 6 → 88 not taken.
64024 llvm::Type *varTy = varSymbolType.toLLVMType(sourceFile);
45
46 // Check if the declaration is with an assignment or the default value
47 64024 llvm::Value *varAddress = nullptr;
48
2/2
✓ Branch 7 → 8 taken 60012 times.
✓ Branch 7 → 33 taken 4012 times.
64024 if (node->hasAssignment) { // Assignment
49
2/2
✓ Branch 8 → 9 taken 480 times.
✓ Branch 8 → 27 taken 59532 times.
60012 if (node->calledCopyCtor) {
50 // Allocate memory
51
1/2
✓ Branch 12 → 13 taken 480 times.
✗ Branch 12 → 63 not taken.
480 varAddress = insertAlloca(varTy);
52
1/2
✓ Branch 15 → 16 taken 480 times.
✗ Branch 15 → 88 not taken.
480 updateAddress(varEntry, varAddress);
53 // Generate debug info for variable declaration
54
1/2
✓ Branch 16 → 17 taken 480 times.
✗ Branch 16 → 88 not taken.
480 diGenerator.generateLocalVarDebugInfo(node->varName, varAddress);
55 // Call copy ctor
56
1/2
✓ Branch 17 → 18 taken 480 times.
✗ Branch 17 → 88 not taken.
480 llvm::Value *rhsAddress = resolveAddress(node->assignExpr);
57
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 480 times.
480 assert(rhsAddress != nullptr);
58
2/4
✓ Branch 22 → 23 taken 480 times.
✗ Branch 22 → 71 not taken.
✓ Branch 23 → 24 taken 480 times.
✗ Branch 23 → 69 not taken.
1440 generateCtorOrDtorCall(varEntry, node->calledCopyCtor, {rhsAddress});
59 } else {
60 // Assign rhs to lhs
61
1/2
✓ Branch 27 → 28 taken 59532 times.
✗ Branch 27 → 76 not taken.
59532 [[maybe_unused]] const LLVMExprResult assignResult = doAssignment(varAddress, varEntry, node->assignExpr, node, true);
62
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 59532 times.
59532 assert(assignResult.entry == varEntry);
63
1/2
✓ Branch 30 → 31 taken 59532 times.
✗ Branch 30 → 76 not taken.
59532 varAddress = getAddress(varEntry);
64
1/2
✓ Branch 31 → 32 taken 59532 times.
✗ Branch 31 → 76 not taken.
59532 updateAddress(varEntry, varAddress);
65 }
66 } else { // Default value
67 // Allocate memory
68
1/2
✓ Branch 36 → 37 taken 4012 times.
✗ Branch 36 → 77 not taken.
4012 varAddress = insertAlloca(varTy);
69
1/2
✓ Branch 39 → 40 taken 4012 times.
✗ Branch 39 → 88 not taken.
4012 updateAddress(varEntry, varAddress);
70
71 // Generate debug info for variable declaration
72
1/2
✓ Branch 40 → 41 taken 4012 times.
✗ Branch 40 → 88 not taken.
4012 diGenerator.generateLocalVarDebugInfo(node->varName, varAddress);
73
74
2/2
✓ Branch 41 → 42 taken 1842 times.
✓ Branch 41 → 46 taken 2170 times.
4012 if (node->calledInitCtor) {
75 // Call no-args constructor
76
1/2
✓ Branch 43 → 44 taken 1842 times.
✗ Branch 43 → 83 not taken.
1842 generateCtorOrDtorCall(varEntry, node->calledInitCtor, {});
77
4/4
✓ Branch 46 → 47 taken 1206 times.
✓ Branch 46 → 55 taken 964 times.
✓ Branch 47 → 48 taken 1204 times.
✓ Branch 47 → 55 taken 2 times.
2170 } else if (!node->isForEachItem && cliOptions.buildMode != BuildMode::RELEASE) {
78
1/2
✗ Branch 48 → 49 not taken.
✓ Branch 48 → 50 taken 1204 times.
1204 assert(!node->isCtorCallRequired);
79
1/4
✗ Branch 50 → 51 not taken.
✓ Branch 50 → 53 taken 1204 times.
✗ Branch 51 → 52 not taken.
✗ Branch 51 → 53 not taken.
1204 assert(cliOptions.buildMode == BuildMode::DEBUG || cliOptions.buildMode == BuildMode::TEST);
80 // Retrieve default value for lhs symbol type and store it
81
1/2
✓ Branch 53 → 54 taken 1204 times.
✗ Branch 53 → 88 not taken.
1204 llvm::Constant *defaultValue = getDefaultValueForSymbolType(varSymbolType);
82
1/2
✓ Branch 54 → 55 taken 1204 times.
✗ Branch 54 → 88 not taken.
1204 insertStore(defaultValue, varAddress);
83 }
84 }
85
1/2
✗ Branch 55 → 56 not taken.
✓ Branch 55 → 57 taken 64024 times.
64024 assert(varAddress != nullptr);
86
87 // Attach the variable name to the LLVM value.
88
2/4
✓ Branch 57 → 58 taken 64024 times.
✗ Branch 57 → 86 not taken.
✓ Branch 58 → 59 taken 64024 times.
✗ Branch 58 → 86 not taken.
64024 varAddress->setName(varEntry->name);
89
90
1/2
✓ Branch 59 → 60 taken 64024 times.
✗ Branch 59 → 87 not taken.
128048 return nullptr;
91 }
92
93 std::any IRGenerator::visitQualifierLst(const QualifierLstNode *node) {
94 return nullptr; // Noop
95 }
96
97 2034 std::any IRGenerator::visitModAttr(const ModAttrNode *node) {
98
1/2
✓ Branch 2 → 3 taken 2034 times.
✗ Branch 2 → 6 not taken.
4068 return nullptr; // Noop
99 }
100
101 std::any IRGenerator::visitTopLevelDefinitionAttr(const TopLevelDefAttrNode *node) {
102 return nullptr; // Noop
103 }
104
105 1907 std::any IRGenerator::visitCaseConstant(const CaseConstantNode *node) {
106
2/2
✓ Branch 2 → 3 taken 131 times.
✓ Branch 2 → 4 taken 1776 times.
1907 if (node->constant)
107 131 return visit(node->constant);
108
109 1776 const SymbolTableEntry *constantEntry = node->entry;
110
4/8
✓ Branch 4 → 5 taken 1776 times.
✗ Branch 4 → 12 not taken.
✓ Branch 5 → 6 taken 1776 times.
✗ Branch 5 → 11 not taken.
✓ Branch 6 → 7 taken 1776 times.
✗ Branch 6 → 11 not taken.
✓ Branch 7 → 8 taken 1776 times.
✗ Branch 7 → 11 not taken.
3552 return getConst(constantEntry->declNode->getCompileTimeValue(manIdx), node->getEvaluatedSymbolType(manIdx), node);
111 }
112
113 74289 std::any IRGenerator::visitReturnStmt(const ReturnStmtNode *node) {
114 74289 llvm::Value *returnValue = nullptr;
115
2/2
✓ Branch 2 → 3 taken 67594 times.
✓ Branch 2 → 31 taken 6695 times.
74289 if (node->hasReturnValue) { // Return value is attached to the return statement
116 67594 const ExprNode *returnExpr = node->assignExpr;
117
2/2
✓ Branch 3 → 4 taken 306 times.
✓ Branch 3 → 25 taken 67288 times.
67594 if (node->calledCopyCtor) {
118 // Perform a copy
119 306 llvm::Value *originalAddress = resolveAddress(returnExpr);
120 306 llvm::Type *returnTy = node->returnType.toLLVMType(sourceFile);
121
1/2
✓ Branch 9 → 10 taken 306 times.
✗ Branch 9 → 59 not taken.
306 llvm::Value *newAddress = insertAlloca(returnTy);
122
2/4
✓ Branch 14 → 15 taken 306 times.
✗ Branch 14 → 67 not taken.
✓ Branch 15 → 16 taken 306 times.
✗ Branch 15 → 65 not taken.
918 generateCtorOrDtorCall(newAddress, node->calledCopyCtor, {originalAddress});
123
1/2
✓ Branch 21 → 22 taken 306 times.
✗ Branch 21 → 72 not taken.
306 returnValue = insertLoad(returnTy, newAddress);
124 } else {
125
2/2
✓ Branch 26 → 27 taken 4144 times.
✓ Branch 26 → 29 taken 63144 times.
67288 returnValue = node->returnType.isRef() ? resolveAddress(returnExpr) : resolveValue(returnExpr);
126 }
127 } else { // Try to load result variable value
128
1/2
✓ Branch 33 → 34 taken 6695 times.
✗ Branch 33 → 80 not taken.
20085 const SymbolTableEntry *resultEntry = currentScope->lookup(RETURN_VARIABLE_NAME);
129
2/2
✓ Branch 39 → 40 taken 14 times.
✓ Branch 39 → 50 taken 6681 times.
6695 if (resultEntry != nullptr) {
130 14 llvm::Type *resultSTy = resultEntry->getQualType().toLLVMType(sourceFile);
131 14 llvm::Value *returnValueAddr = getAddress(resultEntry);
132
1/2
✓ Branch 46 → 47 taken 14 times.
✗ Branch 46 → 84 not taken.
14 returnValue = insertLoad(resultSTy, returnValueAddr);
133 }
134 }
135
136 // Clean up all scopes between here and the enclosing function/procedure/lambda body, then terminate the block
137 74289 generateScopeCleanupUpTo(node, currentScope->getFunctionScope());
138 74289 blockAlreadyTerminated = true;
139
140 // Create return instruction
141
2/2
✓ Branch 52 → 53 taken 67608 times.
✓ Branch 52 → 54 taken 6681 times.
74289 if (returnValue != nullptr) {
142 // Return with value
143 67608 builder.CreateRet(returnValue);
144 } else {
145 // Return without value
146 6681 builder.CreateRetVoid();
147 }
148
149
1/2
✓ Branch 55 → 56 taken 74289 times.
✗ Branch 55 → 90 not taken.
148578 return nullptr;
150 }
151
152 1044 std::any IRGenerator::visitBreakStmt(const BreakStmtNode *node) {
153
1/2
✓ Branch 3 → 4 taken 1044 times.
✗ Branch 3 → 11 not taken.
1044 const auto [targetScope, targetBlock] = breakTargets.at(breakTargets.size() - node->breakTimes);
154
155 // Clean up all scopes between here and the loop/switch statement we are breaking out of
156
1/2
✓ Branch 4 → 5 taken 1044 times.
✗ Branch 4 → 11 not taken.
1044 generateScopeCleanupUpTo(node, targetScope);
157
158 // Jump to destination block
159
1/2
✓ Branch 5 → 6 taken 1044 times.
✗ Branch 5 → 11 not taken.
1044 insertJump(targetBlock);
160
161
1/2
✓ Branch 6 → 7 taken 1044 times.
✗ Branch 6 → 10 not taken.
2088 return nullptr;
162 }
163
164 1698 std::any IRGenerator::visitContinueStmt(const ContinueStmtNode *node) {
165
1/2
✓ Branch 3 → 4 taken 1698 times.
✗ Branch 3 → 11 not taken.
1698 const auto [targetScope, targetBlock] = continueTargets.at(continueTargets.size() - node->continueTimes);
166
167 // Clean up all scopes between here and the loop statement we are continuing
168
1/2
✓ Branch 4 → 5 taken 1698 times.
✗ Branch 4 → 11 not taken.
1698 generateScopeCleanupUpTo(node, targetScope);
169
170 // Jump to destination block
171
1/2
✓ Branch 5 → 6 taken 1698 times.
✗ Branch 5 → 11 not taken.
1698 insertJump(targetBlock);
172
173
1/2
✓ Branch 6 → 7 taken 1698 times.
✗ Branch 6 → 10 not taken.
3396 return nullptr;
174 }
175
176 8 std::any IRGenerator::visitFallthroughStmt(const FallthroughStmtNode *node) {
177 // Jump to destination block
178 8 insertJump(fallthroughBlocks.top());
179
180
1/2
✓ Branch 4 → 5 taken 8 times.
✗ Branch 4 → 8 not taken.
16 return nullptr;
181 }
182
183 11043 std::any IRGenerator::visitAssertStmt(const AssertStmtNode *node) {
184 // Do not generate assertions in release mode
185
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 6 taken 11043 times.
11043 if (cliOptions.buildMode == BuildMode::RELEASE)
186 return nullptr;
187
188 22052 const auto generateBody = [&] {
189 // Create constant for error message
190
2/4
✓ Branch 2 → 3 taken 11009 times.
✗ Branch 2 → 35 not taken.
✓ Branch 3 → 4 taken 11009 times.
✗ Branch 3 → 33 not taken.
11009 const std::string errorMsg = "Assertion failed: Condition '" + node->expressionString + "' evaluated to false.\n";
191
4/8
✓ Branch 7 → 8 taken 11009 times.
✗ Branch 7 → 41 not taken.
✓ Branch 8 → 9 taken 11009 times.
✗ Branch 8 → 39 not taken.
✓ Branch 9 → 10 taken 11009 times.
✗ Branch 9 → 37 not taken.
✓ Branch 11 → 12 taken 11009 times.
✗ Branch 11 → 36 not taken.
22018 llvm::GlobalVariable *globalString = builder.CreateGlobalString(errorMsg, getUnusedGlobalName(ANON_GLOBAL_STRING_NAME));
192 // If the output should be comparable, fix alignment to 4 bytes
193
1/2
✓ Branch 15 → 16 taken 11009 times.
✗ Branch 15 → 19 not taken.
11009 if (cliOptions.comparableOutput)
194
2/4
✓ Branch 16 → 17 taken 11009 times.
✗ Branch 16 → 47 not taken.
✓ Branch 17 → 18 taken 11009 times.
✗ Branch 17 → 47 not taken.
11009 globalString->setAlignment(llvm::Align(4));
195 // Print the error message
196
1/2
✓ Branch 19 → 20 taken 11009 times.
✗ Branch 19 → 56 not taken.
11009 llvm::Function *printfFct = stdFunctionManager.getPrintfFct();
197
4/8
✓ Branch 20 → 21 taken 11009 times.
✗ Branch 20 → 51 not taken.
✓ Branch 22 → 23 taken 11009 times.
✗ Branch 22 → 48 not taken.
✓ Branch 23 → 24 taken 11009 times.
✗ Branch 23 → 48 not taken.
✓ Branch 24 → 25 taken 11009 times.
✗ Branch 24 → 56 not taken.
11009 builder.CreateCall(printfFct, globalString);
198 // Generate call to exit()
199
1/2
✓ Branch 24 → 25 taken 11009 times.
✗ Branch 24 → 56 not taken.
11009 llvm::Function *exitFct = stdFunctionManager.getExitFct();
200
5/10
✓ Branch 25 → 26 taken 11009 times.
✗ Branch 25 → 55 not taken.
✓ Branch 26 → 27 taken 11009 times.
✗ Branch 26 → 53 not taken.
✓ Branch 28 → 29 taken 11009 times.
✗ Branch 28 → 52 not taken.
✓ Branch 29 → 30 taken 11009 times.
✗ Branch 29 → 52 not taken.
✓ Branch 30 → 31 taken 11009 times.
✗ Branch 30 → 56 not taken.
11009 builder.CreateCall(exitFct, builder.getInt32(EXIT_FAILURE));
201 // Create unreachable instruction
202
1/2
✓ Branch 30 → 31 taken 11009 times.
✗ Branch 30 → 56 not taken.
11009 builder.CreateUnreachable();
203 11009 blockAlreadyTerminated = true;
204 11009 };
205
206 // If we have a compile time decision, only evaluate the respective branch
207
3/4
✓ Branch 6 → 7 taken 11043 times.
✗ Branch 6 → 42 not taken.
✓ Branch 7 → 8 taken 72 times.
✓ Branch 7 → 14 taken 10971 times.
11043 if (node->assignExpr->hasCompileTimeValue(manIdx)) {
208
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)
209
1/2
✓ Branch 10 → 11 taken 38 times.
✗ Branch 10 → 42 not taken.
38 generateBody();
210
1/2
✓ Branch 11 → 12 taken 72 times.
✗ Branch 11 → 32 not taken.
144 return nullptr;
211 }
212
213 // Create blocks
214
1/2
✓ Branch 14 → 15 taken 10971 times.
✗ Branch 14 → 42 not taken.
10971 const std::string &codeLine = node->codeLoc.toPrettyLine();
215
2/4
✓ Branch 15 → 16 taken 10971 times.
✗ Branch 15 → 35 not taken.
✓ Branch 16 → 17 taken 10971 times.
✗ Branch 16 → 33 not taken.
10971 llvm::BasicBlock *bThen = createBlock("assert.then." + codeLine);
216
2/4
✓ Branch 18 → 19 taken 10971 times.
✗ Branch 18 → 38 not taken.
✓ Branch 19 → 20 taken 10971 times.
✗ Branch 19 → 36 not taken.
10971 llvm::BasicBlock *bExit = createBlock("assert.exit." + codeLine);
217
218 // Visit the assignExpr
219
1/2
✓ Branch 21 → 22 taken 10971 times.
✗ Branch 21 → 40 not taken.
10971 llvm::Value *condValue = resolveValue(node->assignExpr);
220
221 // Create condition check
222
1/2
✓ Branch 22 → 23 taken 10971 times.
✗ Branch 22 → 40 not taken.
10971 insertCondJump(condValue, bExit, bThen, Likelihood::LIKELY);
223
224 // Switch to then block
225
1/2
✓ Branch 23 → 24 taken 10971 times.
✗ Branch 23 → 40 not taken.
10971 switchToBlock(bThen);
226
1/2
✓ Branch 24 → 25 taken 10971 times.
✗ Branch 24 → 40 not taken.
10971 generateBody();
227
228 // Switch to exit block
229
1/2
✓ Branch 25 → 26 taken 10971 times.
✗ Branch 25 → 40 not taken.
10971 switchToBlock(bExit);
230
231
1/2
✓ Branch 26 → 27 taken 10971 times.
✗ Branch 26 → 39 not taken.
10971 return nullptr;
232 10971 }
233
234 } // namespace spice::compiler
235