GCC Code Coverage Report


Directory: ../
File: src/irgenerator/GenStatements.cpp
Date: 2025-08-26 18:26:32
Exec Total Coverage
Lines: 104 112 92.9%
Functions: 9 12 75.0%
Branches: 106 196 54.1%

Line Branch Exec Source
1 // Copyright (c) 2021-2025 ChilliBits. All rights reserved.
2
3 #include "IRGenerator.h"
4
5 #include <llvm/IR/Module.h>
6
7 #include <ast/ASTNodes.h>
8 #include <driver/Driver.h>
9 #include <symboltablebuilder/SymbolTableBuilder.h>
10
11 namespace spice::compiler {
12
13 16897 std::any IRGenerator::visitStmtLst(const StmtLstNode *node) {
14 // Generate instructions in the scope
15
2/2
✓ Branch 0 (14→4) taken 32206 times.
✓ Branch 1 (14→15) taken 16891 times.
49097 for (const StmtNode *stmt : node->statements) {
16
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 32206 times.
32206 if (!stmt)
17 continue;
18 // Check if we can cancel generating instructions for this code branch
19
4/4
✓ Branch 0 (7→8) taken 32205 times.
✓ Branch 1 (7→15) taken 1 times.
✓ Branch 2 (8→9) taken 32200 times.
✓ Branch 3 (8→15) taken 5 times.
32206 if (blockAlreadyTerminated || stmt->unreachable)
20 break;
21 // Visit child
22
1/2
✓ Branch 0 (9→10) taken 32200 times.
✗ Branch 1 (9→21) not taken.
32200 visit(stmt);
23 }
24
25 // Generate cleanup code of this scope, e.g. dtor calls for struct instances
26 16897 diGenerator.setSourceLocation(node->getNextOuterStmtLst()->closingBraceCodeLoc);
27 16897 generateScopeCleanup(node);
28
29
1/2
✓ Branch 0 (18→19) taken 16897 times.
✗ Branch 1 (18→23) not taken.
16897 return nullptr;
30 }
31
32 std::any IRGenerator::visitTypeAltsLst(const TypeAltsLstNode *node) {
33 return nullptr; // Noop
34 }
35
36 7035 std::any IRGenerator::visitDeclStmt(const DeclStmtNode *node) {
37
1/2
✓ Branch 0 (2→3) taken 7035 times.
✗ Branch 1 (2→88) not taken.
7035 diGenerator.setSourceLocation(node);
38
39 // Get variable entry
40
1/2
✓ Branch 0 (3→4) taken 7035 times.
✗ Branch 1 (3→88) not taken.
7035 SymbolTableEntry *varEntry = node->entries.at(manIdx);
41
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 7035 times.
7035 assert(varEntry != nullptr);
42
1/2
✓ Branch 0 (6→7) taken 7035 times.
✗ Branch 1 (6→88) not taken.
7035 const QualType varSymbolType = varEntry->getQualType();
43
44 // Get LLVM type of variable
45
1/2
✓ Branch 0 (7→8) taken 7035 times.
✗ Branch 1 (7→88) not taken.
7035 llvm::Type *varTy = varSymbolType.toLLVMType(sourceFile);
46
47 // Check if the declaration is with an assignment or the default value
48 7035 llvm::Value *varAddress = nullptr;
49
2/2
✓ Branch 0 (8→9) taken 6718 times.
✓ Branch 1 (8→33) taken 317 times.
7035 if (node->hasAssignment) { // Assignment
50
2/2
✓ Branch 0 (9→10) taken 9 times.
✓ Branch 1 (9→27) taken 6709 times.
6718 if (node->calledCopyCtor) {
51 // Allocate memory
52
1/2
✓ Branch 0 (13→14) taken 9 times.
✗ Branch 1 (13→63) not taken.
9 varAddress = insertAlloca(varTy);
53
1/2
✓ Branch 0 (16→17) taken 9 times.
✗ Branch 1 (16→88) not taken.
9 varEntry->updateAddress(varAddress);
54 // Call copy ctor
55
1/2
✓ Branch 0 (17→18) taken 9 times.
✗ Branch 1 (17→88) not taken.
9 llvm::Value *rhsAddress = resolveAddress(node->assignExpr);
56
1/2
✗ Branch 0 (18→19) not taken.
✓ Branch 1 (18→20) taken 9 times.
9 assert(rhsAddress != nullptr);
57
2/4
✓ Branch 0 (22→23) taken 9 times.
✗ Branch 1 (22→71) not taken.
✓ Branch 2 (23→24) taken 9 times.
✗ Branch 3 (23→69) not taken.
27 generateCtorOrDtorCall(varEntry, node->calledCopyCtor, {rhsAddress});
58 } else {
59 // Assign rhs to lhs
60
1/2
✓ Branch 0 (27→28) taken 6709 times.
✗ Branch 1 (27→76) not taken.
6709 [[maybe_unused]] const LLVMExprResult assignResult = doAssignment(varAddress, varEntry, node->assignExpr, node, true);
61
1/2
✗ Branch 0 (28→29) not taken.
✓ Branch 1 (28→30) taken 6709 times.
6709 assert(assignResult.entry == varEntry);
62
1/2
✓ Branch 0 (30→31) taken 6709 times.
✗ Branch 1 (30→76) not taken.
6709 varAddress = varEntry->getAddress();
63
1/2
✓ Branch 0 (31→32) taken 6709 times.
✗ Branch 1 (31→76) not taken.
6709 varEntry->updateAddress(varAddress);
64 }
65 } else { // Default value
66 // Allocate memory
67
1/2
✓ Branch 0 (36→37) taken 317 times.
✗ Branch 1 (36→77) not taken.
317 varAddress = insertAlloca(varTy);
68
1/2
✓ Branch 0 (39→40) taken 317 times.
✗ Branch 1 (39→88) not taken.
317 varEntry->updateAddress(varAddress);
69
70
2/2
✓ Branch 0 (40→41) taken 137 times.
✓ Branch 1 (40→45) taken 180 times.
317 if (node->calledInitCtor) {
71 // Call no-args constructor
72
1/2
✓ Branch 0 (42→43) taken 137 times.
✗ Branch 1 (42→83) not taken.
137 generateCtorOrDtorCall(varEntry, node->calledInitCtor, {});
73
3/4
✓ Branch 0 (45→46) taken 80 times.
✓ Branch 1 (45→54) taken 100 times.
✓ Branch 2 (46→47) taken 80 times.
✗ Branch 3 (46→54) not taken.
180 } else if (!node->isForEachItem && cliOptions.buildMode != RELEASE) {
74
1/2
✗ Branch 0 (47→48) not taken.
✓ Branch 1 (47→49) taken 80 times.
80 assert(!node->isCtorCallRequired);
75
1/4
✗ Branch 0 (49→50) not taken.
✓ Branch 1 (49→52) taken 80 times.
✗ Branch 2 (50→51) not taken.
✗ Branch 3 (50→52) not taken.
80 assert(cliOptions.buildMode == DEBUG || cliOptions.buildMode == TEST);
76 // Retrieve default value for lhs symbol type and store it
77
1/2
✓ Branch 0 (52→53) taken 80 times.
✗ Branch 1 (52→88) not taken.
80 llvm::Constant *defaultValue = getDefaultValueForSymbolType(varSymbolType);
78
1/2
✓ Branch 0 (53→54) taken 80 times.
✗ Branch 1 (53→88) not taken.
80 insertStore(defaultValue, varAddress);
79 }
80 }
81
1/2
✗ Branch 0 (54→55) not taken.
✓ Branch 1 (54→56) taken 7035 times.
7035 assert(varAddress != nullptr);
82
83 // Attach the variable name to the LLVM value.
84
2/4
✓ Branch 0 (56→57) taken 7035 times.
✗ Branch 1 (56→86) not taken.
✓ Branch 2 (57→58) taken 7035 times.
✗ Branch 3 (57→86) not taken.
7035 varAddress->setName(varEntry->name);
85
86 // Generate debug info for variable declaration
87
1/2
✓ Branch 0 (58→59) taken 7035 times.
✗ Branch 1 (58→88) not taken.
7035 diGenerator.setSourceLocation(node);
88
1/2
✓ Branch 0 (59→60) taken 7035 times.
✗ Branch 1 (59→88) not taken.
7035 diGenerator.generateLocalVarDebugInfo(node->varName, varAddress);
89
90
1/2
✓ Branch 0 (60→61) taken 7035 times.
✗ Branch 1 (60→87) not taken.
7035 return nullptr;
91 }
92
93 std::any IRGenerator::visitQualifierLst(const QualifierLstNode *node) {
94 return nullptr; // Noop
95 }
96
97 273 std::any IRGenerator::visitModAttr(const ModAttrNode *node) {
98
1/2
✓ Branch 0 (2→3) taken 273 times.
✗ Branch 1 (2→5) not taken.
273 return nullptr; // Noop
99 }
100
101 std::any IRGenerator::visitTopLevelDefinitionAttr(const TopLevelDefinitionAttrNode *node) {
102 return nullptr; // Noop
103 }
104
105 66 std::any IRGenerator::visitCaseConstant(const CaseConstantNode *node) {
106
2/2
✓ Branch 0 (2→3) taken 15 times.
✓ Branch 1 (2→4) taken 51 times.
66 if (node->constant)
107 15 return visit(node->constant);
108
109 51 const SymbolTableEntry *constantEntry = node->entry;
110
4/8
✓ Branch 0 (4→5) taken 51 times.
✗ Branch 1 (4→12) not taken.
✓ Branch 2 (5→6) taken 51 times.
✗ Branch 3 (5→11) not taken.
✓ Branch 4 (6→7) taken 51 times.
✗ Branch 5 (6→11) not taken.
✓ Branch 6 (7→8) taken 51 times.
✗ Branch 7 (7→11) not taken.
51 return getConst(constantEntry->declNode->getCompileTimeValue(), node->getEvaluatedSymbolType(manIdx), node);
111 }
112
113 8341 std::any IRGenerator::visitReturnStmt(const ReturnStmtNode *node) {
114 8341 diGenerator.setSourceLocation(node);
115
116 8341 llvm::Value *returnValue = nullptr;
117
2/2
✓ Branch 0 (3→4) taken 8287 times.
✓ Branch 1 (3→33) taken 54 times.
8341 if (node->hasReturnValue) { // Return value is attached to the return statement
118 8287 const AssignExprNode *returnExpr = node->assignExpr;
119
2/2
✓ Branch 0 (4→5) taken 3 times.
✓ Branch 1 (4→26) taken 8284 times.
8287 if (node->calledCopyCtor) {
120 // Perform a copy
121 3 llvm::Value *originalAddress = resolveAddress(returnExpr);
122 3 llvm::Type *returnTy = node->returnType.toLLVMType(sourceFile);
123
1/2
✓ Branch 0 (10→11) taken 3 times.
✗ Branch 1 (10→60) not taken.
3 llvm::Value *newAddress = insertAlloca(returnTy);
124
2/4
✓ Branch 0 (15→16) taken 3 times.
✗ Branch 1 (15→68) not taken.
✓ Branch 2 (16→17) taken 3 times.
✗ Branch 3 (16→66) not taken.
9 generateCtorOrDtorCall(newAddress, node->calledCopyCtor, {originalAddress});
125
2/4
✓ Branch 0 (21→22) taken 3 times.
✗ Branch 1 (21→75) not taken.
✓ Branch 2 (22→23) taken 3 times.
✗ Branch 3 (22→73) not taken.
6 returnValue = insertLoad(returnTy, newAddress);
126 } else {
127
2/2
✓ Branch 0 (27→28) taken 430 times.
✓ Branch 1 (27→30) taken 7854 times.
8284 returnValue = node->returnType.isRef() ? resolveAddress(returnExpr) : resolveValue(returnExpr);
128 }
129 } else { // Try to load result variable value
130
1/2
✓ Branch 0 (35→36) taken 54 times.
✗ Branch 1 (35→81) not taken.
162 const SymbolTableEntry *resultEntry = currentScope->lookup(RETURN_VARIABLE_NAME);
131
2/2
✓ Branch 0 (41→42) taken 4 times.
✓ Branch 1 (41→52) taken 50 times.
54 if (resultEntry != nullptr) {
132 4 llvm::Type *resultSTy = resultEntry->getQualType().toLLVMType(sourceFile);
133 4 llvm::Value *returnValueAddr = resultEntry->getAddress();
134
2/4
✓ Branch 0 (47→48) taken 4 times.
✗ Branch 1 (47→87) not taken.
✓ Branch 2 (48→49) taken 4 times.
✗ Branch 3 (48→85) not taken.
8 returnValue = insertLoad(resultSTy, returnValueAddr);
135 }
136 }
137
138 // Terminate the block (with scope cleanup)
139 8341 terminateBlock(node->getParentScopeNode());
140
141 // Create return instruction
142
2/2
✓ Branch 0 (54→55) taken 8291 times.
✓ Branch 1 (54→56) taken 50 times.
8341 if (returnValue != nullptr) {
143 // Return with value
144 8291 builder.CreateRet(returnValue);
145 } else {
146 // Return without value
147 50 builder.CreateRetVoid();
148 }
149
150
1/2
✓ Branch 0 (57→58) taken 8341 times.
✗ Branch 1 (57→91) not taken.
8341 return nullptr;
151 }
152
153 98 std::any IRGenerator::visitBreakStmt(const BreakStmtNode *node) {
154 98 diGenerator.setSourceLocation(node);
155
156 // Jump to destination block
157 98 const size_t blockIdx = breakBlocks.size() - node->breakTimes;
158 98 insertJump(breakBlocks.at(blockIdx));
159
160
1/2
✓ Branch 0 (6→7) taken 98 times.
✗ Branch 1 (6→9) not taken.
98 return nullptr;
161 }
162
163 325 std::any IRGenerator::visitContinueStmt(const ContinueStmtNode *node) {
164 325 diGenerator.setSourceLocation(node);
165
166 // Jump to destination block
167 325 const size_t blockIdx = continueBlocks.size() - node->continueTimes;
168 325 insertJump(continueBlocks.at(blockIdx));
169
170
1/2
✓ Branch 0 (6→7) taken 325 times.
✗ Branch 1 (6→9) not taken.
325 return nullptr;
171 }
172
173 4 std::any IRGenerator::visitFallthroughStmt(const FallthroughStmtNode *node) {
174 4 diGenerator.setSourceLocation(node);
175
176 // Jump to destination block
177 4 insertJump(fallthroughBlocks.top());
178
179
1/2
✓ Branch 0 (5→6) taken 4 times.
✗ Branch 1 (5→8) not taken.
4 return nullptr;
180 }
181
182 739 std::any IRGenerator::visitAssertStmt(const AssertStmtNode *node) {
183 // Do not generate assertions in release mode
184
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 739 times.
739 if (cliOptions.buildMode == RELEASE)
185 return nullptr;
186
187
1/2
✓ Branch 0 (5→6) taken 739 times.
✗ Branch 1 (5→86) not taken.
739 diGenerator.setSourceLocation(node);
188
189 // Create blocks
190
1/2
✓ Branch 0 (6→7) taken 739 times.
✗ Branch 1 (6→86) not taken.
739 const std::string &codeLine = node->codeLoc.toPrettyLine();
191
2/4
✓ Branch 0 (7→8) taken 739 times.
✗ Branch 1 (7→54) not taken.
✓ Branch 2 (8→9) taken 739 times.
✗ Branch 3 (8→52) not taken.
739 llvm::BasicBlock *bThen = createBlock("assert.then." + codeLine);
192
2/4
✓ Branch 0 (10→11) taken 739 times.
✗ Branch 1 (10→57) not taken.
✓ Branch 2 (11→12) taken 739 times.
✗ Branch 3 (11→55) not taken.
739 llvm::BasicBlock *bExit = createBlock("assert.exit." + codeLine);
193
194 // Visit the assignExpr
195
1/2
✓ Branch 0 (13→14) taken 739 times.
✗ Branch 1 (13→84) not taken.
739 llvm::Value *condValue = resolveValue(node->assignExpr);
196
197 // Create condition check
198
1/2
✓ Branch 0 (14→15) taken 739 times.
✗ Branch 1 (14→84) not taken.
739 insertCondJump(condValue, bExit, bThen, LIKELY);
199
200 // Switch to then block
201
1/2
✓ Branch 0 (15→16) taken 739 times.
✗ Branch 1 (15→84) not taken.
739 switchToBlock(bThen);
202 // Create constant for error message
203
2/4
✓ Branch 0 (16→17) taken 739 times.
✗ Branch 1 (16→60) not taken.
✓ Branch 2 (17→18) taken 739 times.
✗ Branch 3 (17→58) not taken.
739 const std::string errorMsg = "Assertion failed: Condition '" + node->expressionString + "' evaluated to false.\n";
204
4/8
✓ Branch 0 (21→22) taken 739 times.
✗ Branch 1 (21→66) not taken.
✓ Branch 2 (22→23) taken 739 times.
✗ Branch 3 (22→64) not taken.
✓ Branch 4 (23→24) taken 739 times.
✗ Branch 5 (23→62) not taken.
✓ Branch 6 (25→26) taken 739 times.
✗ Branch 7 (25→61) not taken.
1478 llvm::GlobalVariable *globalString = builder.CreateGlobalString(errorMsg, getUnusedGlobalName(ANON_GLOBAL_STRING_NAME));
205 // If the output should be comparable, fix alignment to 4 bytes
206
1/2
✓ Branch 0 (29→30) taken 739 times.
✗ Branch 1 (29→33) not taken.
739 if (cliOptions.comparableOutput)
207
2/4
✓ Branch 0 (30→31) taken 739 times.
✗ Branch 1 (30→72) not taken.
✓ Branch 2 (31→32) taken 739 times.
✗ Branch 3 (31→72) not taken.
739 globalString->setAlignment(llvm::Align(4));
208 // Print the error message
209
1/2
✓ Branch 0 (33→34) taken 739 times.
✗ Branch 1 (33→82) not taken.
739 llvm::Function *printfFct = stdFunctionManager.getPrintfFct();
210
3/6
✓ Branch 0 (34→35) taken 739 times.
✗ Branch 1 (34→76) not taken.
✓ Branch 2 (36→37) taken 739 times.
✗ Branch 3 (36→73) not taken.
✓ Branch 4 (37→38) taken 739 times.
✗ Branch 5 (37→73) not taken.
739 builder.CreateCall(printfFct, globalString);
211 // Generate call to exit()
212
1/2
✓ Branch 0 (38→39) taken 739 times.
✗ Branch 1 (38→82) not taken.
739 llvm::Function *exitFct = stdFunctionManager.getExitFct();
213
4/8
✓ Branch 0 (39→40) taken 739 times.
✗ Branch 1 (39→80) not taken.
✓ Branch 2 (40→41) taken 739 times.
✗ Branch 3 (40→78) not taken.
✓ Branch 4 (42→43) taken 739 times.
✗ Branch 5 (42→77) not taken.
✓ Branch 6 (43→44) taken 739 times.
✗ Branch 7 (43→77) not taken.
739 builder.CreateCall(exitFct, builder.getInt32(EXIT_FAILURE));
214 // Create unreachable instruction
215
1/2
✓ Branch 0 (44→45) taken 739 times.
✗ Branch 1 (44→82) not taken.
739 builder.CreateUnreachable();
216
217 // Switch to exit block
218
1/2
✓ Branch 0 (45→46) taken 739 times.
✗ Branch 1 (45→82) not taken.
739 switchToBlock(bExit);
219
220
1/2
✓ Branch 0 (46→47) taken 739 times.
✗ Branch 1 (46→81) not taken.
739 return nullptr;
221 739 }
222
223 } // namespace spice::compiler
224