GCC Code Coverage Report


Directory: ../
File: src/irgenerator/GenStatements.cpp
Date: 2025-02-05 01:09:36
Exec Total Coverage
Lines: 101 109 92.7%
Functions: 9 12 75.0%
Branches: 102 188 54.3%

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