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 | 65478 | std::any IRGenerator::visitStmtLst(const StmtLstNode *node) { | |
| 14 | // Generate instructions in the scope | ||
| 15 |
2/2✓ Branch 20 → 4 taken 129223 times.
✓ Branch 20 → 21 taken 65459 times.
|
260160 | 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 129222 times.
✓ Branch 6 → 21 taken 1 time.
✓ Branch 7 → 8 taken 129204 times.
✓ Branch 7 → 21 taken 18 times.
|
129223 | if (blockAlreadyTerminated || stmt->unreachable) |
| 18 | break; | ||
| 19 | |||
| 20 | // Set source location for debug info | ||
| 21 |
1/2✓ Branch 8 → 9 taken 129204 times.
✗ Branch 8 → 27 not taken.
|
129204 | diGenerator.setSourceLocation(stmt); |
| 22 | |||
| 23 | // Visit child | ||
| 24 |
1/2✓ Branch 9 → 10 taken 129204 times.
✗ Branch 9 → 26 not taken.
|
129204 | visit(stmt); |
| 25 | } | ||
| 26 | |||
| 27 | // Generate cleanup code of this scope, e.g. dtor calls for struct instances | ||
| 28 | 65478 | generateScopeCleanup(node); | |
| 29 | |||
| 30 |
1/2✓ Branch 22 → 23 taken 65478 times.
✗ Branch 22 → 28 not taken.
|
130956 | return nullptr; |
| 31 | } | ||
| 32 | |||
| 33 | ✗ | std::any IRGenerator::visitTypeAltsLst(const TypeAltsLstNode *node) { | |
| 34 | ✗ | return nullptr; // Noop | |
| 35 | } | ||
| 36 | |||
| 37 | 27673 | std::any IRGenerator::visitDeclStmt(const DeclStmtNode *node) { | |
| 38 | // Get variable entry | ||
| 39 |
1/2✓ Branch 2 → 3 taken 27673 times.
✗ Branch 2 → 88 not taken.
|
27673 | const SymbolTableEntry *varEntry = node->entries.at(manIdx); |
| 40 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 27673 times.
|
27673 | assert(varEntry != nullptr); |
| 41 |
1/2✓ Branch 5 → 6 taken 27673 times.
✗ Branch 5 → 88 not taken.
|
27673 | const QualType varSymbolType = varEntry->getQualType(); |
| 42 | |||
| 43 | // Get LLVM type of variable | ||
| 44 |
1/2✓ Branch 6 → 7 taken 27673 times.
✗ Branch 6 → 88 not taken.
|
27673 | llvm::Type *varTy = varSymbolType.toLLVMType(sourceFile); |
| 45 | |||
| 46 | // Check if the declaration is with an assignment or the default value | ||
| 47 | 27673 | llvm::Value *varAddress = nullptr; | |
| 48 |
2/2✓ Branch 7 → 8 taken 25783 times.
✓ Branch 7 → 33 taken 1890 times.
|
27673 | if (node->hasAssignment) { // Assignment |
| 49 |
2/2✓ Branch 8 → 9 taken 223 times.
✓ Branch 8 → 27 taken 25560 times.
|
25783 | if (node->calledCopyCtor) { |
| 50 | // Allocate memory | ||
| 51 |
1/2✓ Branch 12 → 13 taken 223 times.
✗ Branch 12 → 63 not taken.
|
223 | varAddress = insertAlloca(varTy); |
| 52 |
1/2✓ Branch 15 → 16 taken 223 times.
✗ Branch 15 → 88 not taken.
|
223 | updateAddress(varEntry, varAddress); |
| 53 | // Generate debug info for variable declaration | ||
| 54 |
1/2✓ Branch 16 → 17 taken 223 times.
✗ Branch 16 → 88 not taken.
|
223 | diGenerator.generateLocalVarDebugInfo(node->varName, varAddress); |
| 55 | // Call copy ctor | ||
| 56 |
1/2✓ Branch 17 → 18 taken 223 times.
✗ Branch 17 → 88 not taken.
|
223 | llvm::Value *rhsAddress = resolveAddress(node->assignExpr); |
| 57 |
1/2✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 223 times.
|
223 | assert(rhsAddress != nullptr); |
| 58 |
2/4✓ Branch 22 → 23 taken 223 times.
✗ Branch 22 → 71 not taken.
✓ Branch 23 → 24 taken 223 times.
✗ Branch 23 → 69 not taken.
|
669 | generateCtorOrDtorCall(varEntry, node->calledCopyCtor, {rhsAddress}); |
| 59 | } else { | ||
| 60 | // Assign rhs to lhs | ||
| 61 |
1/2✓ Branch 27 → 28 taken 25560 times.
✗ Branch 27 → 76 not taken.
|
25560 | [[maybe_unused]] const LLVMExprResult assignResult = doAssignment(varAddress, varEntry, node->assignExpr, node, true); |
| 62 |
1/2✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 25560 times.
|
25560 | assert(assignResult.entry == varEntry); |
| 63 |
1/2✓ Branch 30 → 31 taken 25560 times.
✗ Branch 30 → 76 not taken.
|
25560 | varAddress = getAddress(varEntry); |
| 64 |
1/2✓ Branch 31 → 32 taken 25560 times.
✗ Branch 31 → 76 not taken.
|
25560 | updateAddress(varEntry, varAddress); |
| 65 | } | ||
| 66 | } else { // Default value | ||
| 67 | // Allocate memory | ||
| 68 |
1/2✓ Branch 36 → 37 taken 1890 times.
✗ Branch 36 → 77 not taken.
|
1890 | varAddress = insertAlloca(varTy); |
| 69 |
1/2✓ Branch 39 → 40 taken 1890 times.
✗ Branch 39 → 88 not taken.
|
1890 | updateAddress(varEntry, varAddress); |
| 70 | |||
| 71 | // Generate debug info for variable declaration | ||
| 72 |
1/2✓ Branch 40 → 41 taken 1890 times.
✗ Branch 40 → 88 not taken.
|
1890 | diGenerator.generateLocalVarDebugInfo(node->varName, varAddress); |
| 73 | |||
| 74 |
2/2✓ Branch 41 → 42 taken 772 times.
✓ Branch 41 → 46 taken 1118 times.
|
1890 | if (node->calledInitCtor) { |
| 75 | // Call no-args constructor | ||
| 76 |
1/2✓ Branch 43 → 44 taken 772 times.
✗ Branch 43 → 83 not taken.
|
772 | generateCtorOrDtorCall(varEntry, node->calledInitCtor, {}); |
| 77 |
4/4✓ Branch 46 → 47 taken 520 times.
✓ Branch 46 → 55 taken 598 times.
✓ Branch 47 → 48 taken 518 times.
✓ Branch 47 → 55 taken 2 times.
|
1118 | } else if (!node->isForEachItem && cliOptions.buildMode != BuildMode::RELEASE) { |
| 78 |
1/2✗ Branch 48 → 49 not taken.
✓ Branch 48 → 50 taken 518 times.
|
518 | assert(!node->isCtorCallRequired); |
| 79 |
1/4✗ Branch 50 → 51 not taken.
✓ Branch 50 → 53 taken 518 times.
✗ Branch 51 → 52 not taken.
✗ Branch 51 → 53 not taken.
|
518 | 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 518 times.
✗ Branch 53 → 88 not taken.
|
518 | llvm::Constant *defaultValue = getDefaultValueForSymbolType(varSymbolType); |
| 82 |
1/2✓ Branch 54 → 55 taken 518 times.
✗ Branch 54 → 88 not taken.
|
518 | insertStore(defaultValue, varAddress); |
| 83 | } | ||
| 84 | } | ||
| 85 |
1/2✗ Branch 55 → 56 not taken.
✓ Branch 55 → 57 taken 27673 times.
|
27673 | assert(varAddress != nullptr); |
| 86 | |||
| 87 | // Attach the variable name to the LLVM value. | ||
| 88 |
2/4✓ Branch 57 → 58 taken 27673 times.
✗ Branch 57 → 86 not taken.
✓ Branch 58 → 59 taken 27673 times.
✗ Branch 58 → 86 not taken.
|
27673 | varAddress->setName(varEntry->name); |
| 89 | |||
| 90 |
1/2✓ Branch 59 → 60 taken 27673 times.
✗ Branch 59 → 87 not taken.
|
55346 | return nullptr; |
| 91 | } | ||
| 92 | |||
| 93 | ✗ | std::any IRGenerator::visitQualifierLst(const QualifierLstNode *node) { | |
| 94 | ✗ | return nullptr; // Noop | |
| 95 | } | ||
| 96 | |||
| 97 | 696 | std::any IRGenerator::visitModAttr(const ModAttrNode *node) { | |
| 98 |
1/2✓ Branch 2 → 3 taken 696 times.
✗ Branch 2 → 6 not taken.
|
1392 | return nullptr; // Noop |
| 99 | } | ||
| 100 | |||
| 101 | ✗ | std::any IRGenerator::visitTopLevelDefinitionAttr(const TopLevelDefAttrNode *node) { | |
| 102 | ✗ | return nullptr; // Noop | |
| 103 | } | ||
| 104 | |||
| 105 | 844 | std::any IRGenerator::visitCaseConstant(const CaseConstantNode *node) { | |
| 106 |
2/2✓ Branch 2 → 3 taken 16 times.
✓ Branch 2 → 4 taken 828 times.
|
844 | if (node->constant) |
| 107 | 16 | return visit(node->constant); | |
| 108 | |||
| 109 | 828 | const SymbolTableEntry *constantEntry = node->entry; | |
| 110 |
4/8✓ Branch 4 → 5 taken 828 times.
✗ Branch 4 → 12 not taken.
✓ Branch 5 → 6 taken 828 times.
✗ Branch 5 → 11 not taken.
✓ Branch 6 → 7 taken 828 times.
✗ Branch 6 → 11 not taken.
✓ Branch 7 → 8 taken 828 times.
✗ Branch 7 → 11 not taken.
|
1656 | return getConst(constantEntry->declNode->getCompileTimeValue(manIdx), node->getEvaluatedSymbolType(manIdx), node); |
| 111 | } | ||
| 112 | |||
| 113 | 30108 | std::any IRGenerator::visitReturnStmt(const ReturnStmtNode *node) { | |
| 114 | 30108 | llvm::Value *returnValue = nullptr; | |
| 115 |
2/2✓ Branch 2 → 3 taken 27862 times.
✓ Branch 2 → 31 taken 2246 times.
|
30108 | if (node->hasReturnValue) { // Return value is attached to the return statement |
| 116 | 27862 | const ExprNode *returnExpr = node->assignExpr; | |
| 117 |
2/2✓ Branch 3 → 4 taken 133 times.
✓ Branch 3 → 25 taken 27729 times.
|
27862 | if (node->calledCopyCtor) { |
| 118 | // Perform a copy | ||
| 119 | 133 | llvm::Value *originalAddress = resolveAddress(returnExpr); | |
| 120 | 133 | llvm::Type *returnTy = node->returnType.toLLVMType(sourceFile); | |
| 121 |
1/2✓ Branch 9 → 10 taken 133 times.
✗ Branch 9 → 59 not taken.
|
133 | llvm::Value *newAddress = insertAlloca(returnTy); |
| 122 |
2/4✓ Branch 14 → 15 taken 133 times.
✗ Branch 14 → 67 not taken.
✓ Branch 15 → 16 taken 133 times.
✗ Branch 15 → 65 not taken.
|
399 | generateCtorOrDtorCall(newAddress, node->calledCopyCtor, {originalAddress}); |
| 123 |
1/2✓ Branch 21 → 22 taken 133 times.
✗ Branch 21 → 72 not taken.
|
133 | returnValue = insertLoad(returnTy, newAddress); |
| 124 | } else { | ||
| 125 |
2/2✓ Branch 26 → 27 taken 1959 times.
✓ Branch 26 → 29 taken 25770 times.
|
27729 | returnValue = node->returnType.isRef() ? resolveAddress(returnExpr) : resolveValue(returnExpr); |
| 126 | } | ||
| 127 | } else { // Try to load result variable value | ||
| 128 |
1/2✓ Branch 33 → 34 taken 2246 times.
✗ Branch 33 → 80 not taken.
|
6738 | const SymbolTableEntry *resultEntry = currentScope->lookup(RETURN_VARIABLE_NAME); |
| 129 |
2/2✓ Branch 39 → 40 taken 7 times.
✓ Branch 39 → 50 taken 2239 times.
|
2246 | if (resultEntry != nullptr) { |
| 130 | 7 | llvm::Type *resultSTy = resultEntry->getQualType().toLLVMType(sourceFile); | |
| 131 | 7 | llvm::Value *returnValueAddr = getAddress(resultEntry); | |
| 132 |
1/2✓ Branch 46 → 47 taken 7 times.
✗ Branch 46 → 84 not taken.
|
7 | 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 | 30108 | generateScopeCleanupUpTo(node, currentScope->getFunctionScope()); | |
| 138 | 30108 | blockAlreadyTerminated = true; | |
| 139 | |||
| 140 | // Create return instruction | ||
| 141 |
2/2✓ Branch 52 → 53 taken 27869 times.
✓ Branch 52 → 54 taken 2239 times.
|
30108 | if (returnValue != nullptr) { |
| 142 | // Return with value | ||
| 143 | 27869 | builder.CreateRet(returnValue); | |
| 144 | } else { | ||
| 145 | // Return without value | ||
| 146 | 2239 | builder.CreateRetVoid(); | |
| 147 | } | ||
| 148 | |||
| 149 |
1/2✓ Branch 55 → 56 taken 30108 times.
✗ Branch 55 → 90 not taken.
|
60216 | return nullptr; |
| 150 | } | ||
| 151 | |||
| 152 | 247 | std::any IRGenerator::visitBreakStmt(const BreakStmtNode *node) { | |
| 153 |
1/2✓ Branch 3 → 4 taken 247 times.
✗ Branch 3 → 11 not taken.
|
247 | 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 247 times.
✗ Branch 4 → 11 not taken.
|
247 | generateScopeCleanupUpTo(node, targetScope); |
| 157 | |||
| 158 | // Jump to destination block | ||
| 159 |
1/2✓ Branch 5 → 6 taken 247 times.
✗ Branch 5 → 11 not taken.
|
247 | insertJump(targetBlock); |
| 160 | |||
| 161 |
1/2✓ Branch 6 → 7 taken 247 times.
✗ Branch 6 → 10 not taken.
|
494 | return nullptr; |
| 162 | } | ||
| 163 | |||
| 164 | 743 | std::any IRGenerator::visitContinueStmt(const ContinueStmtNode *node) { | |
| 165 |
1/2✓ Branch 3 → 4 taken 743 times.
✗ Branch 3 → 11 not taken.
|
743 | 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 743 times.
✗ Branch 4 → 11 not taken.
|
743 | generateScopeCleanupUpTo(node, targetScope); |
| 169 | |||
| 170 | // Jump to destination block | ||
| 171 |
1/2✓ Branch 5 → 6 taken 743 times.
✗ Branch 5 → 11 not taken.
|
743 | insertJump(targetBlock); |
| 172 | |||
| 173 |
1/2✓ Branch 6 → 7 taken 743 times.
✗ Branch 6 → 10 not taken.
|
1486 | return nullptr; |
| 174 | } | ||
| 175 | |||
| 176 | 4 | std::any IRGenerator::visitFallthroughStmt(const FallthroughStmtNode *node) { | |
| 177 | // Jump to destination block | ||
| 178 | 4 | insertJump(fallthroughBlocks.top()); | |
| 179 | |||
| 180 |
1/2✓ Branch 4 → 5 taken 4 times.
✗ Branch 4 → 8 not taken.
|
8 | return nullptr; |
| 181 | } | ||
| 182 | |||
| 183 | 4748 | 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 4748 times.
|
4748 | if (cliOptions.buildMode == BuildMode::RELEASE) |
| 186 | ✗ | return nullptr; | |
| 187 | |||
| 188 | // Create blocks | ||
| 189 |
1/2✓ Branch 6 → 7 taken 4748 times.
✗ Branch 6 → 87 not taken.
|
4748 | const std::string &codeLine = node->codeLoc.toPrettyLine(); |
| 190 |
2/4✓ Branch 7 → 8 taken 4748 times.
✗ Branch 7 → 55 not taken.
✓ Branch 8 → 9 taken 4748 times.
✗ Branch 8 → 53 not taken.
|
4748 | llvm::BasicBlock *bThen = createBlock("assert.then." + codeLine); |
| 191 |
2/4✓ Branch 10 → 11 taken 4748 times.
✗ Branch 10 → 58 not taken.
✓ Branch 11 → 12 taken 4748 times.
✗ Branch 11 → 56 not taken.
|
4748 | llvm::BasicBlock *bExit = createBlock("assert.exit." + codeLine); |
| 192 | |||
| 193 | // Visit the assignExpr | ||
| 194 |
1/2✓ Branch 13 → 14 taken 4748 times.
✗ Branch 13 → 85 not taken.
|
4748 | llvm::Value *condValue = resolveValue(node->assignExpr); |
| 195 | |||
| 196 | // Create condition check | ||
| 197 |
1/2✓ Branch 14 → 15 taken 4748 times.
✗ Branch 14 → 85 not taken.
|
4748 | insertCondJump(condValue, bExit, bThen, Likelihood::LIKELY); |
| 198 | |||
| 199 | // Switch to then block | ||
| 200 |
1/2✓ Branch 15 → 16 taken 4748 times.
✗ Branch 15 → 85 not taken.
|
4748 | switchToBlock(bThen); |
| 201 | // Create constant for error message | ||
| 202 |
2/4✓ Branch 16 → 17 taken 4748 times.
✗ Branch 16 → 61 not taken.
✓ Branch 17 → 18 taken 4748 times.
✗ Branch 17 → 59 not taken.
|
4748 | const std::string errorMsg = "Assertion failed: Condition '" + node->expressionString + "' evaluated to false.\n"; |
| 203 |
4/8✓ Branch 21 → 22 taken 4748 times.
✗ Branch 21 → 67 not taken.
✓ Branch 22 → 23 taken 4748 times.
✗ Branch 22 → 65 not taken.
✓ Branch 23 → 24 taken 4748 times.
✗ Branch 23 → 63 not taken.
✓ Branch 25 → 26 taken 4748 times.
✗ Branch 25 → 62 not taken.
|
9496 | llvm::GlobalVariable *globalString = builder.CreateGlobalString(errorMsg, getUnusedGlobalName(ANON_GLOBAL_STRING_NAME)); |
| 204 | // If the output should be comparable, fix alignment to 4 bytes | ||
| 205 |
1/2✓ Branch 29 → 30 taken 4748 times.
✗ Branch 29 → 33 not taken.
|
4748 | if (cliOptions.comparableOutput) |
| 206 |
2/4✓ Branch 30 → 31 taken 4748 times.
✗ Branch 30 → 73 not taken.
✓ Branch 31 → 32 taken 4748 times.
✗ Branch 31 → 73 not taken.
|
4748 | globalString->setAlignment(llvm::Align(4)); |
| 207 | // Print the error message | ||
| 208 |
1/2✓ Branch 33 → 34 taken 4748 times.
✗ Branch 33 → 83 not taken.
|
4748 | llvm::Function *printfFct = stdFunctionManager.getPrintfFct(); |
| 209 |
4/8✓ Branch 34 → 35 taken 4748 times.
✗ Branch 34 → 77 not taken.
✓ Branch 36 → 37 taken 4748 times.
✗ Branch 36 → 74 not taken.
✓ Branch 37 → 38 taken 4748 times.
✗ Branch 37 → 74 not taken.
✓ Branch 38 → 39 taken 4748 times.
✗ Branch 38 → 83 not taken.
|
4748 | builder.CreateCall(printfFct, globalString); |
| 210 | // Generate call to exit() | ||
| 211 |
1/2✓ Branch 38 → 39 taken 4748 times.
✗ Branch 38 → 83 not taken.
|
4748 | llvm::Function *exitFct = stdFunctionManager.getExitFct(); |
| 212 |
5/10✓ Branch 39 → 40 taken 4748 times.
✗ Branch 39 → 81 not taken.
✓ Branch 40 → 41 taken 4748 times.
✗ Branch 40 → 79 not taken.
✓ Branch 42 → 43 taken 4748 times.
✗ Branch 42 → 78 not taken.
✓ Branch 43 → 44 taken 4748 times.
✗ Branch 43 → 78 not taken.
✓ Branch 44 → 45 taken 4748 times.
✗ Branch 44 → 83 not taken.
|
4748 | builder.CreateCall(exitFct, builder.getInt32(EXIT_FAILURE)); |
| 213 | // Create unreachable instruction | ||
| 214 |
1/2✓ Branch 44 → 45 taken 4748 times.
✗ Branch 44 → 83 not taken.
|
4748 | builder.CreateUnreachable(); |
| 215 | |||
| 216 | // Switch to exit block | ||
| 217 |
1/2✓ Branch 45 → 46 taken 4748 times.
✗ Branch 45 → 83 not taken.
|
4748 | switchToBlock(bExit); |
| 218 | |||
| 219 |
1/2✓ Branch 46 → 47 taken 4748 times.
✗ Branch 46 → 82 not taken.
|
4748 | return nullptr; |
| 220 | 4748 | } | |
| 221 | |||
| 222 | } // namespace spice::compiler | ||
| 223 |