src/irgenerator/GenImplicit.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "IRGenerator.h" | ||
| 4 | |||
| 5 | #include <SourceFile.h> | ||
| 6 | #include <ast/ASTNodes.h> | ||
| 7 | #include <ast/Attributes.h> | ||
| 8 | #include <driver/Driver.h> | ||
| 9 | #include <global/GlobalResourceManager.h> | ||
| 10 | #include <model/Function.h> | ||
| 11 | #include <symboltablebuilder/SymbolTableBuilder.h> | ||
| 12 | #include <typechecker/FunctionManager.h> | ||
| 13 | |||
| 14 | #include <llvm/IR/Module.h> | ||
| 15 | |||
| 16 | namespace spice::compiler { | ||
| 17 | |||
| 18 | // String placeholders for builtin testing output | ||
| 19 | static const char *const TEST_ALL_START_MSG = "[==========] Running %d test(s) from %d source file(s)\n"; | ||
| 20 | static const char *const TEST_ALL_END_MSG = "[==========] Ran %d test(s) from %d source file(s)\n"; | ||
| 21 | static const char *const TEST_FILE_START_MSG = "[----------] Running %d test(s) from %s\n"; | ||
| 22 | static const char *const TEST_FILE_END_MSG = "[----------] Ran %d test(s) from %s\n\n"; | ||
| 23 | static const char *const TEST_CASE_RUN_MSG = "[ RUN ] %s\n"; | ||
| 24 | static const char *const TEST_CASE_SUCCESS_MSG = "\033[1m\033[32m[ PASSED ]\033[0m\033[22m %s\n"; | ||
| 25 | static const char *const TEST_CASE_FAILED_MSG = "\033[1m\033[31m[ FAILED ]\033[0m\033[22m %s\n"; | ||
| 26 | static const char *const TEST_CASE_SKIPPED_MSG = "\033[1m\033[33m[ SKIPPED ]\033[0m\033[22m %s\n"; | ||
| 27 | |||
| 28 | 241 | llvm::Value *IRGenerator::doImplicitCast(llvm::Value *src, QualType dstSTy, QualType srcSTy) { | |
| 29 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 241 times.
|
241 | assert(srcSTy != dstSTy); // We only need to cast implicitly, if the types do not match exactly |
| 30 | |||
| 31 | // Unpack the pointers until a pointer of another type is met | ||
| 32 | 241 | size_t loadCounter = 0; | |
| 33 |
1/2✗ Branch 16 → 6 not taken.
✓ Branch 16 → 17 taken 241 times.
|
241 | while (srcSTy.isPtr()) { |
| 34 | ✗ | src = insertLoad(srcSTy, src); | |
| 35 | ✗ | srcSTy = srcSTy.getContained(); | |
| 36 | ✗ | dstSTy = dstSTy.getContained(); | |
| 37 | ✗ | loadCounter++; | |
| 38 | } | ||
| 39 | // GEP or bit-cast | ||
| 40 |
3/6✓ Branch 18 → 19 taken 241 times.
✗ Branch 18 → 22 not taken.
✓ Branch 20 → 21 taken 241 times.
✗ Branch 20 → 22 not taken.
✓ Branch 23 → 24 taken 241 times.
✗ Branch 23 → 35 not taken.
|
241 | if (dstSTy.isArray() && srcSTy.isArray()) { // Special case that is used for passing arrays as pointer to functions |
| 41 |
2/4✓ Branch 24 → 25 taken 241 times.
✗ Branch 24 → 72 not taken.
✓ Branch 25 → 26 taken 241 times.
✗ Branch 25 → 72 not taken.
|
241 | llvm::Value *indices[2] = {builder.getInt64(0), builder.getInt32(0)}; |
| 42 |
2/4✓ Branch 30 → 31 taken 241 times.
✗ Branch 30 → 65 not taken.
✓ Branch 31 → 32 taken 241 times.
✗ Branch 31 → 65 not taken.
|
241 | src = insertInBoundsGEP(srcSTy.toLLVMType(sourceFile), src, indices); |
| 43 | } else { | ||
| 44 | ✗ | src = insertLoad(srcSTy, src); | |
| 45 | ✗ | src = builder.CreateBitCast(src, dstSTy.toLLVMType(sourceFile)); | |
| 46 | } | ||
| 47 | // Pack the pointers together again | ||
| 48 |
1/2✗ Branch 54 → 46 not taken.
✓ Branch 54 → 55 taken 241 times.
|
241 | for (; loadCounter > 0; loadCounter--) { |
| 49 | ✗ | llvm::Value *newActualArg = insertAlloca(srcSTy); | |
| 50 | ✗ | insertStore(src, newActualArg); | |
| 51 | ✗ | src = newActualArg; | |
| 52 | } | ||
| 53 | 241 | return src; | |
| 54 | } | ||
| 55 | |||
| 56 | 469 | llvm::Value *IRGenerator::getUpcastedStructPtr(llvm::Value *structPtr, const QualType &dstType, const QualType &srcType) const { | |
| 57 | // Adjusts a pointer to a struct instance so it points at the embedded subobject the destination type | ||
| 58 | // refers to. This backs the explicit 'cast<Base*>(derived)' / 'cast<Interface*>(struct)' conversions. | ||
| 59 | // When the struct carries a vtable prefix (because it implements interfaces), the composed base no longer | ||
| 60 | // sits at offset 0, so the pointer has to be advanced via a GEP. For subobjects that already sit at offset | ||
| 61 | // 0 (e.g. the leading interface field), insertStructGEP() with index 0 is a no-op and returns the pointer | ||
| 62 | // unchanged, so no superfluous IR is emitted. If the given pair of types is not such an upcast, the | ||
| 63 | // pointer is returned unchanged. | ||
| 64 |
1/2✓ Branch 2 → 3 taken 469 times.
✗ Branch 2 → 99 not taken.
|
469 | QualType dstPointee = dstType.removeReferenceWrapper(); |
| 65 |
1/2✓ Branch 3 → 4 taken 469 times.
✗ Branch 3 → 99 not taken.
|
469 | QualType srcPointee = srcType.removeReferenceWrapper(); |
| 66 | // Both sides are pointers ('structPtr' is the pointer value itself), so look one level deeper | ||
| 67 |
5/10✓ Branch 4 → 5 taken 469 times.
✗ Branch 4 → 99 not taken.
✓ Branch 5 → 6 taken 469 times.
✗ Branch 5 → 9 not taken.
✓ Branch 6 → 7 taken 469 times.
✗ Branch 6 → 99 not taken.
✓ Branch 7 → 8 taken 469 times.
✗ Branch 7 → 9 not taken.
✓ Branch 10 → 11 taken 469 times.
✗ Branch 10 → 14 not taken.
|
469 | if (dstPointee.isPtr() && srcPointee.isPtr()) { |
| 68 |
1/2✓ Branch 11 → 12 taken 469 times.
✗ Branch 11 → 85 not taken.
|
469 | dstPointee = dstPointee.getContained(); |
| 69 |
1/2✓ Branch 12 → 13 taken 469 times.
✗ Branch 12 → 86 not taken.
|
469 | srcPointee = srcPointee.getContained(); |
| 70 | } | ||
| 71 | // Bail out if this is not an interface/composed-base upcast that requires a pointer adjustment | ||
| 72 |
2/4✓ Branch 14 → 15 taken 469 times.
✗ Branch 14 → 99 not taken.
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 469 times.
|
469 | if (!srcPointee.is(TY_STRUCT)) |
| 73 | ✗ | return structPtr; | |
| 74 |
6/10✓ Branch 17 → 18 taken 469 times.
✗ Branch 17 → 99 not taken.
✓ Branch 18 → 19 taken 462 times.
✓ Branch 18 → 22 taken 7 times.
✓ Branch 19 → 20 taken 462 times.
✗ Branch 19 → 99 not taken.
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 462 times.
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 469 times.
|
469 | if (!dstPointee.matchesInterfaceImplementedByStruct(srcPointee) && !dstPointee.matchesComposedBaseOfStruct(srcPointee)) |
| 75 | ✗ | return structPtr; | |
| 76 | |||
| 77 | 469 | QualType walkType = srcPointee; | |
| 78 |
3/4✓ Branch 80 → 81 taken 1292 times.
✗ Branch 80 → 99 not taken.
✓ Branch 81 → 26 taken 830 times.
✓ Branch 81 → 82 taken 462 times.
|
1292 | while (!walkType.matches(dstPointee, false, true, true)) { |
| 79 |
2/4✓ Branch 26 → 27 taken 830 times.
✗ Branch 26 → 99 not taken.
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 29 taken 830 times.
|
830 | assert(walkType.is(TY_STRUCT)); |
| 80 |
1/2✓ Branch 29 → 30 taken 830 times.
✗ Branch 29 → 99 not taken.
|
830 | Scope *structScope = walkType.getBodyScope(); |
| 81 |
1/2✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 830 times.
|
830 | assert(structScope != nullptr); |
| 82 |
1/2✓ Branch 32 → 33 taken 830 times.
✗ Branch 32 → 99 not taken.
|
830 | const Struct *spiceStruct = walkType.getStruct(nullptr); |
| 83 |
1/2✗ Branch 33 → 34 not taken.
✓ Branch 33 → 35 taken 830 times.
|
830 | assert(spiceStruct != nullptr); |
| 84 |
1/2✓ Branch 35 → 36 taken 830 times.
✗ Branch 35 → 99 not taken.
|
830 | llvm::Type *llvmStructType = walkType.toLLVMType(sourceFile); |
| 85 | // Implicit (interface) fields are inserted before the explicit fields, so their count is the difference | ||
| 86 | // between the total field count and the number of explicit fields. | ||
| 87 |
1/2✓ Branch 36 → 37 taken 830 times.
✗ Branch 36 → 99 not taken.
|
830 | const size_t implicitFieldCount = structScope->getFieldCount() - spiceStruct->fieldTypes.size(); |
| 88 | |||
| 89 | // Case 1: the destination is an interface implemented directly by the struct -> step into its implicit field | ||
| 90 |
3/4✓ Branch 38 → 39 taken 830 times.
✗ Branch 38 → 99 not taken.
✓ Branch 39 → 40 taken 7 times.
✓ Branch 39 → 61 taken 823 times.
|
830 | if (dstPointee.is(TY_INTERFACE)) { |
| 91 | 7 | bool found = false; | |
| 92 |
1/2✓ Branch 57 → 41 taken 7 times.
✗ Branch 57 → 58 not taken.
|
7 | for (size_t i = 0; i < implicitFieldCount; i++) { |
| 93 |
1/2✗ Branch 41 → 42 not taken.
✓ Branch 41 → 43 taken 7 times.
|
7 | const SymbolTableEntry *implicitField = structScope->lookupField(i); |
| 94 |
3/6✓ Branch 46 → 47 taken 7 times.
✗ Branch 46 → 99 not taken.
✓ Branch 47 → 48 taken 7 times.
✗ Branch 47 → 99 not taken.
✓ Branch 48 → 49 taken 7 times.
✗ Branch 48 → 56 not taken.
|
7 | if (dstPointee.matches(implicitField->getQualType(), false, true, true)) { |
| 95 |
1/2✓ Branch 52 → 53 taken 7 times.
✗ Branch 52 → 87 not taken.
|
7 | structPtr = insertStructGEP(llvmStructType, structPtr, implicitField->orderIndex); |
| 96 | 7 | found = true; | |
| 97 | 7 | break; | |
| 98 | } | ||
| 99 | } | ||
| 100 |
1/2✗ Branch 58 → 59 not taken.
✓ Branch 58 → 60 taken 7 times.
|
7 | assert(found); |
| 101 | (void)found; | ||
| 102 | 7 | return structPtr; | |
| 103 | } | ||
| 104 | |||
| 105 | // Case 2: the destination is a composed base struct -> step into the first explicit (composed) field | ||
| 106 |
1/2✗ Branch 61 → 62 not taken.
✓ Branch 61 → 63 taken 823 times.
|
823 | const SymbolTableEntry *baseField = structScope->lookupField(implicitFieldCount); |
| 107 |
4/8✓ Branch 66 → 67 taken 823 times.
✗ Branch 66 → 71 not taken.
✓ Branch 67 → 68 taken 823 times.
✗ Branch 67 → 99 not taken.
✓ Branch 68 → 69 taken 823 times.
✗ Branch 68 → 99 not taken.
✓ Branch 69 → 70 taken 823 times.
✗ Branch 69 → 71 not taken.
|
823 | assert(baseField != nullptr && baseField->getQualType().isComposition()); |
| 108 |
1/2✓ Branch 75 → 76 taken 823 times.
✗ Branch 75 → 93 not taken.
|
823 | structPtr = insertStructGEP(llvmStructType, structPtr, baseField->orderIndex); |
| 109 |
1/2✓ Branch 78 → 79 taken 823 times.
✗ Branch 78 → 99 not taken.
|
823 | walkType = baseField->getQualType(); |
| 110 | } | ||
| 111 | 462 | return structPtr; | |
| 112 | } | ||
| 113 | |||
| 114 | 135431 | void IRGenerator::generateScopeCleanup(const StmtLstNode *node) { | |
| 115 | 135431 | diGenerator.setSourceLocation(node->closingBraceCodeLoc); | |
| 116 | |||
| 117 | // Do not clean up if the block is already terminated | ||
| 118 |
2/2✓ Branch 3 → 4 taken 41411 times.
✓ Branch 3 → 5 taken 94020 times.
|
135431 | if (blockAlreadyTerminated) |
| 119 | 41411 | return; | |
| 120 | |||
| 121 | // Call all dtor functions | ||
| 122 | 94020 | const auto &[dtorFunctionsToCall, heapVarsToFree] = node->resourcesToCleanup.at(manIdx); | |
| 123 |
2/2✓ Branch 24 → 8 taken 5868 times.
✓ Branch 24 → 25 taken 94020 times.
|
199776 | for (auto [entry, dtor] : dtorFunctionsToCall) |
| 124 |
1/2✓ Branch 13 → 14 taken 5868 times.
✗ Branch 13 → 63 not taken.
|
5868 | generateCtorOrDtorCall(entry, dtor, {}); |
| 125 | |||
| 126 | // Deallocate all heap variables that go out of scope and are currently owned | ||
| 127 |
2/2✓ Branch 40 → 27 taken 435 times.
✓ Branch 40 → 41 taken 94020 times.
|
188475 | for (const SymbolTableEntry *entry : heapVarsToFree) |
| 128 |
2/4✓ Branch 29 → 30 taken 435 times.
✗ Branch 29 → 67 not taken.
✓ Branch 30 → 31 taken 435 times.
✗ Branch 30 → 67 not taken.
|
435 | generateDeallocCall(getAddress(entry)); |
| 129 | |||
| 130 | // Generate lifetime end markers | ||
| 131 |
2/2✓ Branch 41 → 42 taken 1586 times.
✓ Branch 41 → 62 taken 92434 times.
|
94020 | if (cliOptions.useLifetimeMarkers) { |
| 132 |
3/4✓ Branch 42 → 43 taken 1586 times.
✗ Branch 42 → 70 not taken.
✓ Branch 59 → 45 taken 510 times.
✓ Branch 59 → 60 taken 1586 times.
|
3682 | for (const SymbolTableEntry *var : currentScope->getVarsGoingOutOfScope()) { |
| 133 |
1/2✓ Branch 47 → 48 taken 510 times.
✗ Branch 47 → 68 not taken.
|
510 | llvm::Value *address = getAddress(var); |
| 134 |
2/2✓ Branch 48 → 49 taken 466 times.
✓ Branch 48 → 50 taken 44 times.
|
510 | if (address != nullptr) |
| 135 |
1/2✓ Branch 49 → 50 taken 466 times.
✗ Branch 49 → 68 not taken.
|
466 | builder.CreateLifetimeEnd(address); |
| 136 | 1586 | } | |
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | /** | ||
| 141 | * Generate cleanup code (dtor calls, deallocations) for every scope between the given node (exclusive) and the given | ||
| 142 | * target scope (inclusive). This is required for jumps that leave more than one scope at once (e.g. break/continue/return), | ||
| 143 | * since those skip the normal fall-through cleanup that visitStmtLst() generates for each of the enclosing scopes. | ||
| 144 | * | ||
| 145 | * @param node Node the jump originates from | ||
| 146 | * @param targetScope Outermost scope that is left by the jump; cleanup is generated for this scope as well | ||
| 147 | */ | ||
| 148 | 37015 | void IRGenerator::generateScopeCleanupUpTo(const ASTNode *node, const Scope *targetScope) { | |
| 149 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 37015 times.
|
37015 | assert(targetScope != nullptr); |
| 150 | 37015 | const StmtLstNode *scope = node->getNextOuterStmtLst(); | |
| 151 | 37015 | const Scope *scopeLevel = currentScope; | |
| 152 | while (true) { | ||
| 153 | 56689 | generateScopeCleanup(scope); | |
| 154 |
2/2✓ Branch 7 → 8 taken 37015 times.
✓ Branch 7 → 9 taken 19674 times.
|
56689 | if (scopeLevel == targetScope) |
| 155 | 37015 | break; | |
| 156 |
2/4✓ Branch 9 → 10 taken 19674 times.
✗ Branch 9 → 12 not taken.
✓ Branch 10 → 11 taken 19674 times.
✗ Branch 10 → 12 not taken.
|
19674 | assert(scope->parent != nullptr && scopeLevel->parent != nullptr); |
| 157 | 19674 | scope = scope->parent->getNextOuterStmtLst(); | |
| 158 | 19674 | scopeLevel = scopeLevel->parent; | |
| 159 | } | ||
| 160 | 37015 | } | |
| 161 | |||
| 162 | 10728 | void IRGenerator::generateFctDecl(const Function *fct, const std::vector<llvm::Value *> &args) const { | |
| 163 | // Retrieve metadata for the function | ||
| 164 |
1/2✓ Branch 2 → 3 taken 10728 times.
✗ Branch 2 → 90 not taken.
|
10728 | const std::string mangledName = fct->getMangledName(); |
| 165 | |||
| 166 | // Function is not defined in the current module -> declare it | ||
| 167 |
3/4✓ Branch 4 → 5 taken 10728 times.
✗ Branch 4 → 71 not taken.
✓ Branch 5 → 6 taken 2633 times.
✓ Branch 5 → 69 taken 8095 times.
|
10728 | if (!module->getFunction(mangledName)) { |
| 168 | 2633 | std::vector<llvm::Type *> paramTypes; | |
| 169 |
2/2✓ Branch 21 → 8 taken 3366 times.
✓ Branch 21 → 22 taken 2633 times.
|
8632 | for (const llvm::Value *argValue : args) |
| 170 |
1/2✓ Branch 11 → 12 taken 3366 times.
✗ Branch 11 → 72 not taken.
|
3366 | paramTypes.push_back(argValue->getType()); |
| 171 |
2/6✗ Branch 25 → 26 not taken.
✓ Branch 25 → 28 taken 2633 times.
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 85 not taken.
✓ Branch 28 → 29 taken 2633 times.
✗ Branch 28 → 85 not taken.
|
2633 | llvm::Type *returnType = fct->isFunction() ? fct->returnType.toLLVMType(sourceFile) : builder.getVoidTy(); |
| 172 |
1/2✓ Branch 31 → 32 taken 2633 times.
✗ Branch 31 → 74 not taken.
|
2633 | llvm::FunctionType *fctType = llvm::FunctionType::get(returnType, paramTypes, false); |
| 173 |
2/4✓ Branch 33 → 34 taken 2633 times.
✗ Branch 33 → 75 not taken.
✓ Branch 34 → 35 taken 2633 times.
✗ Branch 34 → 85 not taken.
|
2633 | module->getOrInsertFunction(mangledName, fctType); |
| 174 | |||
| 175 |
1/2✓ Branch 37 → 38 taken 2633 times.
✗ Branch 37 → 67 not taken.
|
2633 | if (fct->isMethod()) { |
| 176 | // Get callee function | ||
| 177 |
1/2✓ Branch 39 → 40 taken 2633 times.
✗ Branch 39 → 76 not taken.
|
2633 | llvm::Function *callee = module->getFunction(mangledName); |
| 178 |
1/2✗ Branch 40 → 41 not taken.
✓ Branch 40 → 42 taken 2633 times.
|
2633 | assert(callee != nullptr); |
| 179 | |||
| 180 | // Set attributes to 'this' param | ||
| 181 | // Get 'this' entry | ||
| 182 |
1/2✓ Branch 44 → 45 taken 2633 times.
✗ Branch 44 → 79 not taken.
|
7899 | const SymbolTableEntry *thisEntry = fct->bodyScope->lookupStrict(THIS_VARIABLE_NAME); |
| 183 |
1/2✗ Branch 50 → 51 not taken.
✓ Branch 50 → 52 taken 2633 times.
|
2633 | assert(thisEntry != nullptr); |
| 184 |
3/6✓ Branch 52 → 53 taken 2633 times.
✗ Branch 52 → 83 not taken.
✓ Branch 53 → 54 taken 2633 times.
✗ Branch 53 → 83 not taken.
✓ Branch 54 → 55 taken 2633 times.
✗ Branch 54 → 83 not taken.
|
2633 | llvm::Type *structType = thisEntry->getQualType().getContained().toLLVMType(sourceFile); |
| 185 |
1/2✗ Branch 55 → 56 not taken.
✓ Branch 55 → 57 taken 2633 times.
|
2633 | assert(structType != nullptr); |
| 186 |
1/2✓ Branch 57 → 58 taken 2633 times.
✗ Branch 57 → 85 not taken.
|
2633 | callee->addParamAttr(0, llvm::Attribute::NoUndef); |
| 187 |
1/2✓ Branch 58 → 59 taken 2633 times.
✗ Branch 58 → 85 not taken.
|
2633 | callee->addParamAttr(0, llvm::Attribute::NonNull); |
| 188 |
3/6✓ Branch 60 → 61 taken 2633 times.
✗ Branch 60 → 84 not taken.
✓ Branch 61 → 62 taken 2633 times.
✗ Branch 61 → 84 not taken.
✓ Branch 62 → 63 taken 2633 times.
✗ Branch 62 → 84 not taken.
|
2633 | callee->addDereferenceableParamAttr(0, module->getDataLayout().getTypeStoreSize(structType)); |
| 189 |
3/6✓ Branch 64 → 65 taken 2633 times.
✗ Branch 64 → 85 not taken.
✓ Branch 65 → 66 taken 2633 times.
✗ Branch 65 → 85 not taken.
✓ Branch 66 → 67 taken 2633 times.
✗ Branch 66 → 85 not taken.
|
2633 | callee->addParamAttr(0, llvm::Attribute::getWithAlignment(context, module->getDataLayout().getABITypeAlign(structType))); |
| 190 | } | ||
| 191 | 2633 | } | |
| 192 | 10728 | } | |
| 193 | |||
| 194 | 10728 | llvm::CallInst *IRGenerator::generateFctCall(const Function *fct, const std::vector<llvm::Value *> &args) const { | |
| 195 | // Retrieve metadata for the function | ||
| 196 |
1/2✓ Branch 2 → 3 taken 10728 times.
✗ Branch 2 → 57 not taken.
|
10728 | const std::string mangledName = fct->getMangledName(); |
| 197 | |||
| 198 | // Get callee function | ||
| 199 |
1/2✓ Branch 4 → 5 taken 10728 times.
✗ Branch 4 → 43 not taken.
|
10728 | llvm::Function *callee = module->getFunction(mangledName); |
| 200 |
1/2✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 10728 times.
|
10728 | assert(callee != nullptr); |
| 201 | |||
| 202 | // Generate function call | ||
| 203 |
4/8✓ Branch 7 → 8 taken 10728 times.
✗ Branch 7 → 46 not taken.
✓ Branch 9 → 10 taken 10728 times.
✗ Branch 9 → 44 not taken.
✓ Branch 10 → 11 taken 10728 times.
✗ Branch 10 → 44 not taken.
✓ Branch 11 → 12 taken 10728 times.
✗ Branch 11 → 55 not taken.
|
10728 | llvm::CallInst *callInst = builder.CreateCall(callee, args); |
| 204 | |||
| 205 | // Set attributes to 'this' param | ||
| 206 |
1/2✓ Branch 14 → 15 taken 10728 times.
✗ Branch 14 → 40 not taken.
|
10728 | if (fct->isMethod()) { |
| 207 | // Get 'this' entry | ||
| 208 |
1/2✓ Branch 17 → 18 taken 10728 times.
✗ Branch 17 → 49 not taken.
|
32184 | const SymbolTableEntry *thisEntry = fct->bodyScope->lookupStrict(THIS_VARIABLE_NAME); |
| 209 |
1/2✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 10728 times.
|
10728 | assert(thisEntry != nullptr); |
| 210 |
3/6✓ Branch 25 → 26 taken 10728 times.
✗ Branch 25 → 53 not taken.
✓ Branch 26 → 27 taken 10728 times.
✗ Branch 26 → 53 not taken.
✓ Branch 27 → 28 taken 10728 times.
✗ Branch 27 → 53 not taken.
|
10728 | llvm::Type *structType = thisEntry->getQualType().getContained().toLLVMType(sourceFile); |
| 211 |
1/2✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 10728 times.
|
10728 | assert(structType != nullptr); |
| 212 |
1/2✓ Branch 30 → 31 taken 10728 times.
✗ Branch 30 → 55 not taken.
|
10728 | callInst->addParamAttr(0, llvm::Attribute::NoUndef); |
| 213 |
1/2✓ Branch 31 → 32 taken 10728 times.
✗ Branch 31 → 55 not taken.
|
10728 | callInst->addParamAttr(0, llvm::Attribute::NonNull); |
| 214 |
3/6✓ Branch 33 → 34 taken 10728 times.
✗ Branch 33 → 54 not taken.
✓ Branch 34 → 35 taken 10728 times.
✗ Branch 34 → 54 not taken.
✓ Branch 35 → 36 taken 10728 times.
✗ Branch 35 → 54 not taken.
|
10728 | callInst->addDereferenceableParamAttr(0, module->getDataLayout().getTypeStoreSize(structType)); |
| 215 |
3/6✓ Branch 37 → 38 taken 10728 times.
✗ Branch 37 → 55 not taken.
✓ Branch 38 → 39 taken 10728 times.
✗ Branch 38 → 55 not taken.
✓ Branch 39 → 40 taken 10728 times.
✗ Branch 39 → 55 not taken.
|
10728 | callInst->addParamAttr(0, llvm::Attribute::getWithAlignment(context, module->getDataLayout().getABITypeAlign(structType))); |
| 216 | } | ||
| 217 | |||
| 218 | 10728 | return callInst; | |
| 219 | 10728 | } | |
| 220 | |||
| 221 | ✗ | llvm::Value *IRGenerator::generateFctDeclAndCall(const Function *fct, const std::vector<llvm::Value *> &args) const { | |
| 222 | ✗ | generateFctDecl(fct, args); | |
| 223 | ✗ | return generateFctCall(fct, args); | |
| 224 | } | ||
| 225 | |||
| 226 | 10728 | void IRGenerator::generateProcDeclAndCall(const Function *proc, const std::vector<llvm::Value *> &args) const { | |
| 227 | 10728 | generateFctDecl(proc, args); | |
| 228 | 10728 | (void)generateFctCall(proc, args); | |
| 229 | 10728 | } | |
| 230 | |||
| 231 | 8064 | void IRGenerator::generateCtorOrDtorCall(const SymbolTableEntry *entry, const Function *ctorOrDtor, | |
| 232 | const std::vector<llvm::Value *> &args) { | ||
| 233 | // Retrieve address of the struct variable. For fields this is the 'this' variable, otherwise use the normal address | ||
| 234 | llvm::Value *structAddr; | ||
| 235 |
2/2✓ Branch 3 → 4 taken 1139 times.
✓ Branch 3 → 41 taken 6925 times.
|
8064 | if (entry->isField()) { |
| 236 | // Take 'this' var as base pointer | ||
| 237 |
1/2✓ Branch 6 → 7 taken 1139 times.
✗ Branch 6 → 50 not taken.
|
3417 | const SymbolTableEntry *thisVar = currentScope->lookupStrict(THIS_VARIABLE_NAME); |
| 238 |
1/2✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 1139 times.
|
1139 | assert(thisVar != nullptr); |
| 239 |
7/14✓ Branch 14 → 15 taken 1139 times.
✗ Branch 14 → 54 not taken.
✓ Branch 15 → 16 taken 1139 times.
✗ Branch 15 → 54 not taken.
✓ Branch 16 → 17 taken 1139 times.
✗ Branch 16 → 22 not taken.
✓ Branch 17 → 18 taken 1139 times.
✗ Branch 17 → 54 not taken.
✓ Branch 18 → 19 taken 1139 times.
✗ Branch 18 → 54 not taken.
✓ Branch 19 → 20 taken 1139 times.
✗ Branch 19 → 54 not taken.
✓ Branch 20 → 21 taken 1139 times.
✗ Branch 20 → 22 not taken.
|
1139 | assert(thisVar->getQualType().isPtr() && thisVar->getQualType().getContained().is(TY_STRUCT)); |
| 240 |
3/6✓ Branch 23 → 24 taken 1139 times.
✗ Branch 23 → 55 not taken.
✓ Branch 24 → 25 taken 1139 times.
✗ Branch 24 → 55 not taken.
✓ Branch 25 → 26 taken 1139 times.
✗ Branch 25 → 55 not taken.
|
1139 | llvm::Type *thisType = thisVar->getQualType().getContained().toLLVMType(sourceFile); |
| 241 |
3/6✓ Branch 29 → 30 taken 1139 times.
✗ Branch 29 → 56 not taken.
✓ Branch 30 → 31 taken 1139 times.
✗ Branch 30 → 56 not taken.
✓ Branch 31 → 32 taken 1139 times.
✗ Branch 31 → 56 not taken.
|
1139 | llvm::Value *thisPtr = insertLoad(builder.getPtrTy(), getAddress(thisVar)); |
| 242 | // Add field offset | ||
| 243 |
1/2✓ Branch 37 → 38 taken 1139 times.
✗ Branch 37 → 62 not taken.
|
1139 | structAddr = insertStructGEP(thisType, thisPtr, entry->orderIndex); |
| 244 | } else { | ||
| 245 | 6925 | structAddr = getAddress(entry); | |
| 246 | // For optional parameter initializers we need this exception | ||
| 247 |
2/2✓ Branch 42 → 43 taken 263 times.
✓ Branch 42 → 44 taken 6662 times.
|
6925 | if (!structAddr) |
| 248 | 263 | return; | |
| 249 | } | ||
| 250 |
1/2✗ Branch 44 → 45 not taken.
✓ Branch 44 → 46 taken 7801 times.
|
7801 | assert(structAddr != nullptr); |
| 251 | 7801 | generateCtorOrDtorCall(structAddr, ctorOrDtor, args); | |
| 252 | } | ||
| 253 | |||
| 254 | 10728 | void IRGenerator::generateCtorOrDtorCall(llvm::Value *structAddr, const Function *ctorOrDtor, | |
| 255 | const std::vector<llvm::Value *> &args) const { | ||
| 256 | // Build parameter list | ||
| 257 |
1/2✓ Branch 4 → 5 taken 10728 times.
✗ Branch 4 → 16 not taken.
|
21456 | std::vector argValues = {structAddr}; |
| 258 |
1/2✓ Branch 12 → 13 taken 10728 times.
✗ Branch 12 → 20 not taken.
|
21456 | argValues.insert(argValues.end(), args.begin(), args.end()); |
| 259 | |||
| 260 | // Generate function call | ||
| 261 |
1/2✓ Branch 13 → 14 taken 10728 times.
✗ Branch 13 → 22 not taken.
|
10728 | generateProcDeclAndCall(ctorOrDtor, argValues); |
| 262 | 10728 | } | |
| 263 | |||
| 264 | 594 | void IRGenerator::generateDeallocCall(llvm::Value *variableAddress) const { | |
| 265 | // Abort if the address is not set. This can happen when leaving the scope of a dtor, which already freed the heap memory | ||
| 266 |
2/2✓ Branch 2 → 3 taken 276 times.
✓ Branch 2 → 4 taken 318 times.
|
594 | if (!variableAddress) |
| 267 | 276 | return; | |
| 268 | |||
| 269 | // In case of string runtime, call free manually. Otherwise, use the memory_rt implementation of sDealloc() | ||
| 270 |
1/2✗ Branch 7 → 8 not taken.
✓ Branch 7 → 14 taken 318 times.
|
636 | if (sourceFile->isStringRT()) { |
| 271 | ✗ | llvm::Function *freeFct = stdFunctionManager.getFreeFct(); | |
| 272 | ✗ | builder.CreateCall(freeFct, variableAddress); | |
| 273 | } else { | ||
| 274 | 318 | llvm::Function *deallocFct = stdFunctionManager.getDeallocBytePtrRefFct(); | |
| 275 |
3/6✓ Branch 15 → 16 taken 318 times.
✗ Branch 15 → 26 not taken.
✓ Branch 17 → 18 taken 318 times.
✗ Branch 17 → 24 not taken.
✓ Branch 18 → 19 taken 318 times.
✗ Branch 18 → 24 not taken.
|
318 | builder.CreateCall(deallocFct, variableAddress); |
| 276 | } | ||
| 277 | } | ||
| 278 | |||
| 279 | 4 | llvm::Function *IRGenerator::generateImplicitFunction(const std::function<void()> &generateBody, const Function *spiceFunc) { | |
| 280 | // Only focus on method procedures | ||
| 281 |
1/2✓ Branch 2 → 3 taken 4 times.
✗ Branch 2 → 161 not taken.
|
4 | const ASTNode *node = spiceFunc->entry->declNode; |
| 282 |
1/2✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 4 times.
|
4 | assert(spiceFunc->isFunction()); |
| 283 | |||
| 284 | // Only generate if used | ||
| 285 |
1/2✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 4 times.
|
4 | if (!spiceFunc->used) |
| 286 | ✗ | return nullptr; | |
| 287 | |||
| 288 | // Retrieve return type | ||
| 289 |
1/2✓ Branch 9 → 10 taken 4 times.
✗ Branch 9 → 161 not taken.
|
4 | llvm::Type *returnType = spiceFunc->returnType.toLLVMType(sourceFile); |
| 290 | |||
| 291 | // Get 'this' entry | ||
| 292 | 4 | std::vector<llvm::Type *> paramTypes; | |
| 293 |
1/2✓ Branch 10 → 11 taken 4 times.
✗ Branch 10 → 159 not taken.
|
4 | const SymbolTableEntry *thisEntry = nullptr; |
| 294 |
1/2✗ Branch 13 → 14 not taken.
✓ Branch 13 → 27 taken 4 times.
|
4 | if (spiceFunc->isMethod()) { |
| 295 | ✗ | thisEntry = spiceFunc->bodyScope->lookupStrict(THIS_VARIABLE_NAME); | |
| 296 | ✗ | assert(thisEntry != nullptr); | |
| 297 | ✗ | paramTypes.push_back(builder.getPtrTy()); | |
| 298 | } | ||
| 299 | |||
| 300 | // Get parameter types | ||
| 301 |
1/2✗ Branch 44 → 29 not taken.
✓ Branch 44 → 45 taken 4 times.
|
8 | for (const auto &[qualType, isOptional] : spiceFunc->paramList) { |
| 302 | ✗ | assert(!isOptional); | |
| 303 | ✗ | paramTypes.push_back(qualType.toLLVMType(sourceFile)); | |
| 304 | } | ||
| 305 | |||
| 306 | // Get function linkage | ||
| 307 |
2/4✓ Branch 45 → 46 taken 4 times.
✗ Branch 45 → 159 not taken.
✓ Branch 46 → 47 taken 4 times.
✗ Branch 46 → 159 not taken.
|
4 | const bool isPublic = spiceFunc->entry->getQualType().isPublic(); |
| 308 | |||
| 309 | // Create function | ||
| 310 |
1/2✓ Branch 47 → 48 taken 4 times.
✗ Branch 47 → 159 not taken.
|
4 | const std::string mangledName = spiceFunc->getMangledName(); |
| 311 |
1/2✓ Branch 49 → 50 taken 4 times.
✗ Branch 49 → 132 not taken.
|
4 | llvm::FunctionType *fctType = llvm::FunctionType::get(returnType, paramTypes, false); |
| 312 |
1/2✓ Branch 50 → 51 taken 4 times.
✗ Branch 50 → 157 not taken.
|
4 | const llvm::GlobalObject::LinkageTypes linkage = getSymbolLinkageType(isPublic); |
| 313 |
2/4✓ Branch 51 → 52 taken 4 times.
✗ Branch 51 → 133 not taken.
✓ Branch 52 → 53 taken 4 times.
✗ Branch 52 → 133 not taken.
|
4 | llvm::Function *fct = llvm::Function::Create(fctType, linkage, mangledName, module); |
| 314 |
1/2✓ Branch 53 → 54 taken 4 times.
✗ Branch 53 → 157 not taken.
|
4 | fct->addFnAttr(llvm::Attribute::MustProgress); |
| 315 |
1/2✓ Branch 54 → 55 taken 4 times.
✗ Branch 54 → 157 not taken.
|
4 | addCommonFctAttrs(fct); |
| 316 | |||
| 317 | // Set attributes to 'this' param | ||
| 318 |
1/2✗ Branch 58 → 59 not taken.
✓ Branch 58 → 76 taken 4 times.
|
4 | if (spiceFunc->isMethod()) { |
| 319 | ✗ | fct->addParamAttr(0, llvm::Attribute::NoUndef); | |
| 320 | ✗ | fct->addParamAttr(0, llvm::Attribute::NonNull); | |
| 321 | ✗ | assert(thisEntry != nullptr); | |
| 322 | ✗ | llvm::Type *structType = thisEntry->getQualType().getContained().toLLVMType(sourceFile); | |
| 323 | ✗ | assert(structType != nullptr); | |
| 324 | ✗ | fct->addDereferenceableParamAttr(0, module->getDataLayout().getTypeStoreSize(structType)); | |
| 325 | ✗ | fct->addParamAttr(0, llvm::Attribute::getWithAlignment(context, module->getDataLayout().getABITypeAlign(structType))); | |
| 326 | } | ||
| 327 | |||
| 328 | // Add debug info | ||
| 329 |
1/2✓ Branch 76 → 77 taken 4 times.
✗ Branch 76 → 157 not taken.
|
4 | diGenerator.generateFunctionDebugInfo(fct, spiceFunc); |
| 330 |
1/2✗ Branch 77 → 78 not taken.
✓ Branch 77 → 80 taken 4 times.
|
4 | if (node != nullptr) |
| 331 | ✗ | diGenerator.setSourceLocation(node); | |
| 332 | |||
| 333 | // Change to body scope | ||
| 334 |
2/4✓ Branch 80 → 81 taken 4 times.
✗ Branch 80 → 138 not taken.
✓ Branch 81 → 82 taken 4 times.
✗ Branch 81 → 136 not taken.
|
4 | changeToScope(spiceFunc->getScopeName(), ScopeType::FUNC_PROC_BODY); |
| 335 | |||
| 336 | // Create entry block | ||
| 337 |
1/2✓ Branch 86 → 87 taken 4 times.
✗ Branch 86 → 139 not taken.
|
4 | llvm::BasicBlock *bEntry = createBlock(); |
| 338 |
1/2✓ Branch 89 → 90 taken 4 times.
✗ Branch 89 → 157 not taken.
|
4 | switchToBlock(bEntry, fct); |
| 339 | |||
| 340 | // Reset alloca insert markers to this block | ||
| 341 | 4 | allocaInsertBlock = bEntry; | |
| 342 |
1/2✓ Branch 90 → 91 taken 4 times.
✗ Branch 90 → 157 not taken.
|
4 | allocaInsertInst = nullptr; |
| 343 | |||
| 344 | // Store first argument to 'this' symbol | ||
| 345 |
1/2✗ Branch 93 → 94 not taken.
✓ Branch 93 → 112 taken 4 times.
|
4 | if (spiceFunc->isMethod()) { |
| 346 | ✗ | assert(thisEntry != nullptr); | |
| 347 | // Allocate space for the parameter | ||
| 348 | ✗ | llvm::Value *thisAddress = insertAlloca(paramTypes.front(), THIS_VARIABLE_NAME); | |
| 349 | // Update the symbol table entry | ||
| 350 | ✗ | updateAddress(thisEntry, thisAddress); | |
| 351 | // Generate debug info | ||
| 352 | ✗ | diGenerator.generateLocalVarDebugInfo(THIS_VARIABLE_NAME, thisAddress, 1); | |
| 353 | // Store the value at the new address | ||
| 354 | ✗ | insertStore(fct->arg_begin(), thisAddress); | |
| 355 | } | ||
| 356 | |||
| 357 | // Generate body | ||
| 358 |
1/2✓ Branch 112 → 113 taken 4 times.
✗ Branch 112 → 157 not taken.
|
4 | generateBody(); |
| 359 | |||
| 360 | // Conclude debug info for function | ||
| 361 |
1/2✓ Branch 113 → 114 taken 4 times.
✗ Branch 113 → 157 not taken.
|
4 | diGenerator.concludeFunctionDebugInfo(); |
| 362 | |||
| 363 | // Verify function | ||
| 364 | // Use the code location of the declaration node if available. Otherwise, (e.g. in case of test main) use an artificial code loc | ||
| 365 |
2/4✗ Branch 114 → 115 not taken.
✓ Branch 114 → 116 taken 4 times.
✓ Branch 116 → 117 taken 4 times.
✗ Branch 116 → 157 not taken.
|
4 | const CodeLoc codeLoc = node != nullptr ? node->codeLoc : CodeLoc(1, 1, sourceFile); |
| 366 |
1/2✓ Branch 117 → 118 taken 4 times.
✗ Branch 117 → 157 not taken.
|
4 | verifyFunction(fct, codeLoc); |
| 367 | |||
| 368 | // Change to parent scope | ||
| 369 |
1/2✓ Branch 118 → 119 taken 4 times.
✗ Branch 118 → 157 not taken.
|
4 | changeToParentScope(ScopeType::FUNC_PROC_BODY); |
| 370 | |||
| 371 | 4 | return fct; | |
| 372 | 4 | } | |
| 373 | |||
| 374 | 3767 | llvm::Function *IRGenerator::generateImplicitProcedure(const std::function<void()> &generateBody, const Function *spiceProc) { | |
| 375 | // Only focus on method procedures | ||
| 376 | 3767 | const ASTNode *node = spiceProc->entry->declNode; | |
| 377 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 3767 times.
|
3767 | assert(node != nullptr); |
| 378 |
1/2✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 3767 times.
|
3767 | assert(spiceProc->isProcedure()); |
| 379 | |||
| 380 | // Only generate if used | ||
| 381 |
2/2✓ Branch 9 → 10 taken 2868 times.
✓ Branch 9 → 11 taken 899 times.
|
3767 | if (!spiceProc->used) |
| 382 | 2868 | return nullptr; | |
| 383 | |||
| 384 | // Get 'this' entry | ||
| 385 | 899 | std::vector<llvm::Type *> paramTypes; | |
| 386 |
1/2✓ Branch 11 → 12 taken 899 times.
✗ Branch 11 → 157 not taken.
|
899 | const SymbolTableEntry *thisEntry = nullptr; |
| 387 |
1/2✓ Branch 14 → 15 taken 899 times.
✗ Branch 14 → 28 not taken.
|
899 | if (spiceProc->isMethod()) { |
| 388 |
1/2✓ Branch 17 → 18 taken 899 times.
✗ Branch 17 → 123 not taken.
|
2697 | thisEntry = spiceProc->bodyScope->lookupStrict(THIS_VARIABLE_NAME); |
| 389 |
1/2✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 899 times.
|
899 | assert(thisEntry != nullptr); |
| 390 |
2/4✓ Branch 25 → 26 taken 899 times.
✗ Branch 25 → 127 not taken.
✓ Branch 26 → 27 taken 899 times.
✗ Branch 26 → 127 not taken.
|
899 | paramTypes.push_back(builder.getPtrTy()); |
| 391 | } | ||
| 392 | |||
| 393 | // Get parameter types | ||
| 394 |
2/2✓ Branch 45 → 30 taken 241 times.
✓ Branch 45 → 46 taken 899 times.
|
2039 | for (const auto &[qualType, isOptional] : spiceProc->paramList) { |
| 395 |
1/2✗ Branch 32 → 33 not taken.
✓ Branch 32 → 34 taken 241 times.
|
241 | assert(!isOptional); |
| 396 |
2/4✓ Branch 34 → 35 taken 241 times.
✗ Branch 34 → 128 not taken.
✓ Branch 35 → 36 taken 241 times.
✗ Branch 35 → 128 not taken.
|
241 | paramTypes.push_back(qualType.toLLVMType(sourceFile)); |
| 397 | } | ||
| 398 | |||
| 399 | // Get function linkage | ||
| 400 |
2/4✓ Branch 46 → 47 taken 899 times.
✗ Branch 46 → 157 not taken.
✓ Branch 47 → 48 taken 899 times.
✗ Branch 47 → 157 not taken.
|
899 | const bool isPublic = spiceProc->entry->getQualType().isPublic(); |
| 401 | |||
| 402 | // Create function | ||
| 403 |
1/2✓ Branch 48 → 49 taken 899 times.
✗ Branch 48 → 157 not taken.
|
899 | const std::string mangledName = spiceProc->getMangledName(); |
| 404 |
2/4✓ Branch 50 → 51 taken 899 times.
✗ Branch 50 → 130 not taken.
✓ Branch 51 → 52 taken 899 times.
✗ Branch 51 → 130 not taken.
|
899 | llvm::FunctionType *fctType = llvm::FunctionType::get(builder.getVoidTy(), paramTypes, false); |
| 405 |
1/2✓ Branch 52 → 53 taken 899 times.
✗ Branch 52 → 155 not taken.
|
899 | const llvm::GlobalObject::LinkageTypes linkage = getSymbolLinkageType(isPublic); |
| 406 |
2/4✓ Branch 53 → 54 taken 899 times.
✗ Branch 53 → 131 not taken.
✓ Branch 54 → 55 taken 899 times.
✗ Branch 54 → 131 not taken.
|
899 | llvm::Function *fct = llvm::Function::Create(fctType, linkage, mangledName, module); |
| 407 |
1/2✓ Branch 55 → 56 taken 899 times.
✗ Branch 55 → 155 not taken.
|
899 | fct->addFnAttr(llvm::Attribute::MustProgress); |
| 408 |
1/2✓ Branch 56 → 57 taken 899 times.
✗ Branch 56 → 155 not taken.
|
899 | addCommonFctAttrs(fct); |
| 409 | |||
| 410 | // Set attributes to 'this' param | ||
| 411 |
1/2✓ Branch 60 → 61 taken 899 times.
✗ Branch 60 → 78 not taken.
|
899 | if (spiceProc->isMethod()) { |
| 412 |
1/2✓ Branch 61 → 62 taken 899 times.
✗ Branch 61 → 155 not taken.
|
899 | fct->addParamAttr(0, llvm::Attribute::NoUndef); |
| 413 |
1/2✓ Branch 62 → 63 taken 899 times.
✗ Branch 62 → 155 not taken.
|
899 | fct->addParamAttr(0, llvm::Attribute::NonNull); |
| 414 |
1/2✗ Branch 63 → 64 not taken.
✓ Branch 63 → 65 taken 899 times.
|
899 | assert(thisEntry != nullptr); |
| 415 |
3/6✓ Branch 65 → 66 taken 899 times.
✗ Branch 65 → 132 not taken.
✓ Branch 66 → 67 taken 899 times.
✗ Branch 66 → 132 not taken.
✓ Branch 67 → 68 taken 899 times.
✗ Branch 67 → 132 not taken.
|
899 | llvm::Type *structType = thisEntry->getQualType().getContained().toLLVMType(sourceFile); |
| 416 |
1/2✗ Branch 68 → 69 not taken.
✓ Branch 68 → 70 taken 899 times.
|
899 | assert(structType != nullptr); |
| 417 |
3/6✓ Branch 71 → 72 taken 899 times.
✗ Branch 71 → 133 not taken.
✓ Branch 72 → 73 taken 899 times.
✗ Branch 72 → 133 not taken.
✓ Branch 73 → 74 taken 899 times.
✗ Branch 73 → 133 not taken.
|
899 | fct->addDereferenceableParamAttr(0, module->getDataLayout().getTypeStoreSize(structType)); |
| 418 |
3/6✓ Branch 75 → 76 taken 899 times.
✗ Branch 75 → 155 not taken.
✓ Branch 76 → 77 taken 899 times.
✗ Branch 76 → 155 not taken.
✓ Branch 77 → 78 taken 899 times.
✗ Branch 77 → 155 not taken.
|
899 | fct->addParamAttr(0, llvm::Attribute::getWithAlignment(context, module->getDataLayout().getABITypeAlign(structType))); |
| 419 | } | ||
| 420 | |||
| 421 | // Add debug info | ||
| 422 |
1/2✓ Branch 78 → 79 taken 899 times.
✗ Branch 78 → 155 not taken.
|
899 | diGenerator.generateFunctionDebugInfo(fct, spiceProc); |
| 423 |
1/2✓ Branch 79 → 80 taken 899 times.
✗ Branch 79 → 155 not taken.
|
899 | diGenerator.setSourceLocation(node); |
| 424 | |||
| 425 | // Change to body scope | ||
| 426 |
2/4✓ Branch 80 → 81 taken 899 times.
✗ Branch 80 → 136 not taken.
✓ Branch 81 → 82 taken 899 times.
✗ Branch 81 → 134 not taken.
|
899 | changeToScope(spiceProc->getScopeName(), ScopeType::FUNC_PROC_BODY); |
| 427 | |||
| 428 | // Create entry block | ||
| 429 |
1/2✓ Branch 86 → 87 taken 899 times.
✗ Branch 86 → 137 not taken.
|
899 | llvm::BasicBlock *bEntry = createBlock(); |
| 430 |
1/2✓ Branch 89 → 90 taken 899 times.
✗ Branch 89 → 155 not taken.
|
899 | switchToBlock(bEntry, fct); |
| 431 | |||
| 432 | // Reset alloca insert markers to this block | ||
| 433 | 899 | allocaInsertBlock = bEntry; | |
| 434 |
1/2✓ Branch 90 → 91 taken 899 times.
✗ Branch 90 → 155 not taken.
|
899 | allocaInsertInst = nullptr; |
| 435 | |||
| 436 | // Store first argument to 'this' symbol | ||
| 437 |
1/2✓ Branch 93 → 94 taken 899 times.
✗ Branch 93 → 112 not taken.
|
899 | if (spiceProc->isMethod()) { |
| 438 |
1/2✗ Branch 94 → 95 not taken.
✓ Branch 94 → 96 taken 899 times.
|
899 | assert(thisEntry != nullptr); |
| 439 | // Allocate space for the parameter | ||
| 440 |
2/4✓ Branch 98 → 99 taken 899 times.
✗ Branch 98 → 145 not taken.
✓ Branch 100 → 101 taken 899 times.
✗ Branch 100 → 143 not taken.
|
899 | llvm::Value *thisAddress = insertAlloca(paramTypes.front(), THIS_VARIABLE_NAME); |
| 441 | // Update the symbol table entry | ||
| 442 |
1/2✓ Branch 103 → 104 taken 899 times.
✗ Branch 103 → 155 not taken.
|
899 | updateAddress(thisEntry, thisAddress); |
| 443 | // Generate debug info | ||
| 444 |
2/4✓ Branch 106 → 107 taken 899 times.
✗ Branch 106 → 151 not taken.
✓ Branch 107 → 108 taken 899 times.
✗ Branch 107 → 149 not taken.
|
1798 | diGenerator.generateLocalVarDebugInfo(THIS_VARIABLE_NAME, thisAddress, 1); |
| 445 | // Store the value at the new address | ||
| 446 |
2/4✓ Branch 110 → 111 taken 899 times.
✗ Branch 110 → 155 not taken.
✓ Branch 111 → 112 taken 899 times.
✗ Branch 111 → 155 not taken.
|
899 | insertStore(fct->arg_begin(), thisAddress); |
| 447 | } | ||
| 448 | |||
| 449 | // Generate body | ||
| 450 |
1/2✓ Branch 112 → 113 taken 899 times.
✗ Branch 112 → 155 not taken.
|
899 | generateBody(); |
| 451 | |||
| 452 | // Create return instruction | ||
| 453 |
1/2✓ Branch 113 → 114 taken 899 times.
✗ Branch 113 → 155 not taken.
|
899 | builder.CreateRetVoid(); |
| 454 | |||
| 455 | // Conclude debug info for function | ||
| 456 |
1/2✓ Branch 114 → 115 taken 899 times.
✗ Branch 114 → 155 not taken.
|
899 | diGenerator.concludeFunctionDebugInfo(); |
| 457 | |||
| 458 | // Verify function | ||
| 459 |
1/2✓ Branch 115 → 116 taken 899 times.
✗ Branch 115 → 155 not taken.
|
899 | verifyFunction(fct, node->codeLoc); |
| 460 | |||
| 461 | // Change to parent scope | ||
| 462 |
1/2✓ Branch 116 → 117 taken 899 times.
✗ Branch 116 → 155 not taken.
|
899 | changeToParentScope(ScopeType::FUNC_PROC_BODY); |
| 463 | |||
| 464 | 899 | return fct; | |
| 465 | 899 | } | |
| 466 | |||
| 467 | 6200 | void IRGenerator::generateCtorBodyPreamble(Scope *bodyScope) { | |
| 468 | // Retrieve struct scope | ||
| 469 | 6200 | Scope *structScope = bodyScope->parent; | |
| 470 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 6200 times.
|
6200 | assert(structScope != nullptr); |
| 471 | |||
| 472 | // Get struct address | ||
| 473 |
1/2✓ Branch 6 → 7 taken 6200 times.
✗ Branch 6 → 130 not taken.
|
18600 | const SymbolTableEntry *thisEntry = bodyScope->lookupStrict(THIS_VARIABLE_NAME); |
| 474 |
1/2✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 6200 times.
|
6200 | assert(thisEntry != nullptr); |
| 475 |
1/2✓ Branch 14 → 15 taken 6200 times.
✗ Branch 14 → 184 not taken.
|
6200 | llvm::Value *thisPtrPtr = getAddress(thisEntry); |
| 476 |
1/2✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 6200 times.
|
6200 | assert(thisPtrPtr != nullptr); |
| 477 | 6200 | llvm::Value *thisPtr = nullptr; | |
| 478 |
2/4✓ Branch 17 → 18 taken 6200 times.
✗ Branch 17 → 184 not taken.
✓ Branch 18 → 19 taken 6200 times.
✗ Branch 18 → 184 not taken.
|
6200 | const QualType structSymbolType = thisEntry->getQualType().getBase(); |
| 479 |
1/2✓ Branch 19 → 20 taken 6200 times.
✗ Branch 19 → 184 not taken.
|
6200 | llvm::Type *structType = structSymbolType.toLLVMType(sourceFile); |
| 480 | |||
| 481 | // Store VTable to first struct field if required | ||
| 482 |
1/2✓ Branch 20 → 21 taken 6200 times.
✗ Branch 20 → 184 not taken.
|
6200 | const Struct *spiceStruct = structSymbolType.getStruct(nullptr); |
| 483 |
1/2✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 6200 times.
|
6200 | assert(spiceStruct != nullptr); |
| 484 |
2/2✓ Branch 23 → 24 taken 1368 times.
✓ Branch 23 → 45 taken 4832 times.
|
6200 | if (spiceStruct->vTableData.vtable != nullptr) { |
| 485 |
1/2✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 1368 times.
|
1368 | assert(spiceStruct->vTableData.vtableType != nullptr); |
| 486 | // Store VTable to field address at index 0 | ||
| 487 |
2/4✓ Branch 29 → 30 taken 1368 times.
✗ Branch 29 → 134 not taken.
✓ Branch 30 → 31 taken 1368 times.
✗ Branch 30 → 134 not taken.
|
1368 | thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr); |
| 488 |
3/6✓ Branch 33 → 34 taken 1368 times.
✗ Branch 33 → 147 not taken.
✓ Branch 34 → 35 taken 1368 times.
✗ Branch 34 → 147 not taken.
✓ Branch 35 → 36 taken 1368 times.
✗ Branch 35 → 147 not taken.
|
1368 | llvm::Value *indices[3] = {builder.getInt64(0), builder.getInt32(0), builder.getInt32(2)}; |
| 489 |
1/2✓ Branch 40 → 41 taken 1368 times.
✗ Branch 40 → 140 not taken.
|
1368 | llvm::Value *gepResult = insertInBoundsGEP(spiceStruct->vTableData.vtableType, spiceStruct->vTableData.vtable, indices); |
| 490 |
1/2✓ Branch 43 → 44 taken 1368 times.
✗ Branch 43 → 147 not taken.
|
1368 | insertStore(gepResult, thisPtr); |
| 491 | } | ||
| 492 | |||
| 493 |
1/2✓ Branch 45 → 46 taken 6200 times.
✗ Branch 45 → 184 not taken.
|
6200 | const size_t fieldCount = structScope->getFieldCount(); |
| 494 |
2/2✓ Branch 126 → 47 taken 15130 times.
✓ Branch 126 → 127 taken 6200 times.
|
21330 | for (size_t fieldIdx = 0; fieldIdx < fieldCount; fieldIdx++) { |
| 495 |
1/2✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 15130 times.
|
15130 | const SymbolTableEntry *fieldSymbol = structScope->lookupField(fieldIdx); |
| 496 |
3/6✓ Branch 52 → 53 taken 15130 times.
✗ Branch 52 → 56 not taken.
✓ Branch 53 → 54 taken 15130 times.
✗ Branch 53 → 184 not taken.
✓ Branch 54 → 55 taken 15130 times.
✗ Branch 54 → 56 not taken.
|
15130 | assert(fieldSymbol != nullptr && fieldSymbol->isField()); |
| 497 |
2/2✓ Branch 57 → 58 taken 1261 times.
✓ Branch 57 → 59 taken 13869 times.
|
15130 | if (fieldSymbol->isImplicitField) |
| 498 | 1261 | continue; | |
| 499 | |||
| 500 | // Call ctor for struct fields | ||
| 501 |
1/2✓ Branch 59 → 60 taken 13869 times.
✗ Branch 59 → 184 not taken.
|
13869 | const QualType &fieldType = fieldSymbol->getQualType(); |
| 502 |
1/2✓ Branch 60 → 61 taken 13869 times.
✗ Branch 60 → 62 not taken.
|
13869 | const auto fieldNode = spice_pointer_cast<FieldNode *>(fieldSymbol->declNode); |
| 503 |
3/4✓ Branch 67 → 68 taken 13869 times.
✗ Branch 67 → 184 not taken.
✓ Branch 68 → 69 taken 3411 times.
✓ Branch 68 → 99 taken 10458 times.
|
13869 | if (fieldType.is(TY_STRUCT)) { |
| 504 | // Lookup ctor function and call if available | ||
| 505 |
1/2✓ Branch 69 → 70 taken 3411 times.
✗ Branch 69 → 184 not taken.
|
3411 | Scope *matchScope = fieldType.getBodyScope(); |
| 506 |
4/6✓ Branch 73 → 74 taken 3411 times.
✗ Branch 73 → 150 not taken.
✓ Branch 74 → 75 taken 3411 times.
✗ Branch 74 → 148 not taken.
✓ Branch 78 → 79 taken 1879 times.
✓ Branch 78 → 98 taken 1532 times.
|
10233 | if (const Function *ctorFunction = FunctionManager::lookup(matchScope, CTOR_FUNCTION_NAME, fieldType, {}, false)) { |
| 507 |
2/2✓ Branch 79 → 80 taken 375 times.
✓ Branch 79 → 88 taken 1504 times.
|
1879 | if (!thisPtr) |
| 508 |
2/4✓ Branch 83 → 84 taken 375 times.
✗ Branch 83 → 157 not taken.
✓ Branch 84 → 85 taken 375 times.
✗ Branch 84 → 157 not taken.
|
375 | thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr); |
| 509 |
1/2✓ Branch 91 → 92 taken 1879 times.
✗ Branch 91 → 163 not taken.
|
1879 | llvm::Value *fieldAddress = insertStructGEP(structType, thisPtr, fieldIdx); |
| 510 |
1/2✓ Branch 95 → 96 taken 1879 times.
✗ Branch 95 → 169 not taken.
|
1879 | generateCtorOrDtorCall(fieldAddress, ctorFunction, {}); |
| 511 | } | ||
| 512 | 3411 | continue; | |
| 513 | 3411 | } | |
| 514 | |||
| 515 | // Store default field values | ||
| 516 |
3/4✓ Branch 99 → 100 taken 4549 times.
✓ Branch 99 → 101 taken 5909 times.
✓ Branch 100 → 101 taken 4549 times.
✗ Branch 100 → 125 not taken.
|
10458 | if (fieldNode->defaultValue != nullptr || cliOptions.buildMode != BuildMode::RELEASE) { |
| 517 | // Retrieve field address | ||
| 518 |
2/2✓ Branch 101 → 102 taken 3237 times.
✓ Branch 101 → 110 taken 7221 times.
|
10458 | if (!thisPtr) |
| 519 |
2/4✓ Branch 105 → 106 taken 3237 times.
✗ Branch 105 → 172 not taken.
✓ Branch 106 → 107 taken 3237 times.
✗ Branch 106 → 172 not taken.
|
3237 | thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr); |
| 520 |
1/2✓ Branch 113 → 114 taken 10458 times.
✗ Branch 113 → 178 not taken.
|
10458 | llvm::Value *fieldAddress = insertStructGEP(structType, thisPtr, fieldIdx); |
| 521 | // Retrieve default value | ||
| 522 | llvm::Value *value; | ||
| 523 |
2/2✓ Branch 116 → 117 taken 5909 times.
✓ Branch 116 → 119 taken 4549 times.
|
10458 | if (fieldNode->defaultValue != nullptr) { |
| 524 | // To resolve the default value, we need to temporarily change to the manifestation of the current struct instantiation | ||
| 525 | 5909 | const size_t oldManIdx = manIdx; // Save manifestation index | |
| 526 | 5909 | manIdx = spiceStruct->manifestationIndex; | |
| 527 |
1/2✓ Branch 117 → 118 taken 5909 times.
✗ Branch 117 → 184 not taken.
|
5909 | value = resolveValue(fieldNode->defaultValue); |
| 528 | 5909 | manIdx = oldManIdx; // Restore manifestation index | |
| 529 | } else { | ||
| 530 |
1/4✗ Branch 119 → 120 not taken.
✓ Branch 119 → 122 taken 4549 times.
✗ Branch 120 → 121 not taken.
✗ Branch 120 → 122 not taken.
|
4549 | assert(cliOptions.buildMode == BuildMode::DEBUG || cliOptions.buildMode == BuildMode::TEST); |
| 531 |
1/2✓ Branch 122 → 123 taken 4549 times.
✗ Branch 122 → 184 not taken.
|
4549 | value = getDefaultValueForSymbolType(fieldType); |
| 532 | } | ||
| 533 | // Store default value | ||
| 534 |
1/2✓ Branch 124 → 125 taken 10458 times.
✗ Branch 124 → 184 not taken.
|
10458 | insertStore(value, fieldAddress); |
| 535 | } | ||
| 536 | } | ||
| 537 | 6200 | } | |
| 538 | |||
| 539 | 146 | void IRGenerator::generateDefaultCtor(const Function *ctorFunction) { | |
| 540 |
3/6✓ Branch 2 → 3 taken 146 times.
✗ Branch 2 → 6 not taken.
✓ Branch 3 → 4 taken 146 times.
✗ Branch 3 → 13 not taken.
✓ Branch 4 → 5 taken 146 times.
✗ Branch 4 → 6 not taken.
|
146 | assert(ctorFunction->implicitDefault && ctorFunction->name == CTOR_FUNCTION_NAME); |
| 541 | 258 | const std::function<void()> generateBody = [&] { generateCtorBodyPreamble(ctorFunction->bodyScope); }; | |
| 542 |
1/2✓ Branch 8 → 9 taken 146 times.
✗ Branch 8 → 11 not taken.
|
146 | generateImplicitProcedure(generateBody, ctorFunction); |
| 543 | 146 | } | |
| 544 | |||
| 545 | 236 | void IRGenerator::generateCopyCtorBodyPreamble(const Function *copyCtorFunction) { | |
| 546 | // Retrieve struct scope | ||
| 547 | 236 | Scope *structScope = copyCtorFunction->bodyScope->parent; | |
| 548 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 236 times.
|
236 | assert(structScope != nullptr); |
| 549 | |||
| 550 | // Get struct address | ||
| 551 |
1/2✓ Branch 6 → 7 taken 236 times.
✗ Branch 6 → 141 not taken.
|
708 | const SymbolTableEntry *thisEntry = copyCtorFunction->bodyScope->lookupStrict(THIS_VARIABLE_NAME); |
| 552 |
1/2✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 236 times.
|
236 | assert(thisEntry != nullptr); |
| 553 | 236 | llvm::Value *thisPtrPtr = getAddress(thisEntry); | |
| 554 |
1/2✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 236 times.
|
236 | assert(thisPtrPtr != nullptr); |
| 555 | 236 | llvm::Value *thisPtr = nullptr; | |
| 556 |
3/6✓ Branch 17 → 18 taken 236 times.
✗ Branch 17 → 145 not taken.
✓ Branch 18 → 19 taken 236 times.
✗ Branch 18 → 145 not taken.
✓ Branch 19 → 20 taken 236 times.
✗ Branch 19 → 145 not taken.
|
236 | llvm::Type *structType = thisEntry->getQualType().getBase().toLLVMType(sourceFile); |
| 557 | |||
| 558 | // Retrieve the value of the original struct, which is the only function parameter | ||
| 559 | 236 | llvm::Value *originalThisPtr = builder.GetInsertBlock()->getParent()->getArg(1); | |
| 560 | |||
| 561 | 236 | const size_t fieldCount = structScope->getFieldCount(); | |
| 562 |
2/2✓ Branch 137 → 25 taken 678 times.
✓ Branch 137 → 138 taken 236 times.
|
914 | for (size_t fieldIdx = 0; fieldIdx < fieldCount; fieldIdx++) { |
| 563 |
1/2✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 678 times.
|
678 | const SymbolTableEntry *fieldSymbol = structScope->lookupField(fieldIdx); |
| 564 |
2/4✓ Branch 30 → 31 taken 678 times.
✗ Branch 30 → 34 not taken.
✓ Branch 32 → 33 taken 678 times.
✗ Branch 32 → 34 not taken.
|
678 | assert(fieldSymbol != nullptr && fieldSymbol->isField()); |
| 565 | |||
| 566 | // Retrieve the address of the original field (copy source) | ||
| 567 |
1/2✓ Branch 38 → 39 taken 678 times.
✗ Branch 38 → 146 not taken.
|
678 | llvm::Value *originalFieldAddress = insertStructGEP(structType, originalThisPtr, fieldIdx); |
| 568 | |||
| 569 | 678 | const QualType &fieldType = fieldSymbol->getQualType(); | |
| 570 | |||
| 571 | // Call copy ctor for struct fields | ||
| 572 |
6/6✓ Branch 43 → 44 taken 422 times.
✓ Branch 43 → 47 taken 256 times.
✓ Branch 45 → 46 taken 392 times.
✓ Branch 45 → 47 taken 30 times.
✓ Branch 48 → 49 taken 392 times.
✓ Branch 48 → 73 taken 286 times.
|
678 | if (fieldType.is(TY_STRUCT) && !fieldType.isTriviallyCopyable(nullptr)) { |
| 573 | // Lookup copy ctor function and call if available | ||
| 574 |
1/2✓ Branch 49 → 50 taken 392 times.
✗ Branch 49 → 173 not taken.
|
392 | Scope *matchScope = fieldType.getBodyScope(); |
| 575 |
2/4✓ Branch 50 → 51 taken 392 times.
✗ Branch 50 → 156 not taken.
✓ Branch 54 → 55 taken 392 times.
✗ Branch 54 → 152 not taken.
|
1176 | const ArgList args = {{fieldType.toConstRef(nullptr), false /* we have the field as storage */}}; |
| 576 |
2/4✓ Branch 58 → 59 taken 392 times.
✗ Branch 58 → 160 not taken.
✓ Branch 59 → 60 taken 392 times.
✗ Branch 59 → 158 not taken.
|
392 | const Function *copyCtor = FunctionManager::lookup(matchScope, CTOR_FUNCTION_NAME, fieldType, args, false); |
| 577 |
1/2✗ Branch 62 → 63 not taken.
✓ Branch 62 → 64 taken 392 times.
|
392 | assert(copyCtor != nullptr); |
| 578 |
2/4✓ Branch 66 → 67 taken 392 times.
✗ Branch 66 → 166 not taken.
✓ Branch 67 → 68 taken 392 times.
✗ Branch 67 → 164 not taken.
|
784 | generateCtorOrDtorCall(fieldSymbol, copyCtor, {originalFieldAddress}); |
| 579 | 392 | continue; | |
| 580 | 392 | } | |
| 581 | |||
| 582 | // Retrieve the address of the new field (copy dest) | ||
| 583 |
2/2✓ Branch 73 → 74 taken 145 times.
✓ Branch 73 → 82 taken 141 times.
|
286 | if (!thisPtr) |
| 584 |
2/4✓ Branch 77 → 78 taken 145 times.
✗ Branch 77 → 174 not taken.
✓ Branch 78 → 79 taken 145 times.
✗ Branch 78 → 174 not taken.
|
145 | thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr); |
| 585 |
1/2✓ Branch 85 → 86 taken 286 times.
✗ Branch 85 → 180 not taken.
|
286 | llvm::Value *fieldAddress = insertStructGEP(structType, thisPtr, fieldIdx); |
| 586 | |||
| 587 | // For owning heap fields, copy the underlying heap storage | ||
| 588 |
2/2✓ Branch 89 → 90 taken 3 times.
✓ Branch 89 → 134 taken 283 times.
|
286 | if (fieldType.isHeap()) { |
| 589 |
1/2✗ Branch 91 → 92 not taken.
✓ Branch 91 → 93 taken 3 times.
|
3 | assert(fieldType.isPtr()); |
| 590 |
2/4✓ Branch 93 → 94 taken 3 times.
✗ Branch 93 → 186 not taken.
✓ Branch 94 → 95 taken 3 times.
✗ Branch 94 → 186 not taken.
|
3 | llvm::Type *pointeeType = fieldType.getContained().toLLVMType(sourceFile); |
| 591 | |||
| 592 | // Retrieve original heap address | ||
| 593 |
2/4✓ Branch 98 → 99 taken 3 times.
✗ Branch 98 → 187 not taken.
✓ Branch 99 → 100 taken 3 times.
✗ Branch 99 → 187 not taken.
|
3 | llvm::Value *originalHeapAddress = insertLoad(builder.getPtrTy(), originalFieldAddress); |
| 594 | |||
| 595 | // Insert check for nullptr | ||
| 596 |
2/4✓ Branch 104 → 105 taken 3 times.
✗ Branch 104 → 195 not taken.
✓ Branch 105 → 106 taken 3 times.
✗ Branch 105 → 193 not taken.
|
6 | llvm::BasicBlock *bThen = createBlock("nullptrcheck.then"); |
| 597 |
2/4✓ Branch 110 → 111 taken 3 times.
✗ Branch 110 → 201 not taken.
✓ Branch 111 → 112 taken 3 times.
✗ Branch 111 → 199 not taken.
|
3 | llvm::BasicBlock *bExit = createBlock("nullptrcheck.exit"); |
| 598 |
4/8✓ Branch 114 → 115 taken 3 times.
✗ Branch 114 → 205 not taken.
✓ Branch 115 → 116 taken 3 times.
✗ Branch 115 → 205 not taken.
✓ Branch 116 → 117 taken 3 times.
✗ Branch 116 → 205 not taken.
✓ Branch 117 → 118 taken 3 times.
✗ Branch 117 → 205 not taken.
|
3 | llvm::Value *condValue = builder.CreateICmpNE(originalHeapAddress, llvm::Constant::getNullValue(builder.getPtrTy())); |
| 599 | 3 | insertCondJump(condValue, bThen, bExit); | |
| 600 | |||
| 601 | // Fill then block | ||
| 602 | 3 | switchToBlock(bThen); | |
| 603 | |||
| 604 | // Allocate new space on the heap | ||
| 605 | 3 | llvm::Function *unsafeAllocFct = stdFunctionManager.getAllocUnsafeLongFct(); | |
| 606 |
2/4✓ Branch 122 → 123 taken 3 times.
✗ Branch 122 → 206 not taken.
✓ Branch 123 → 124 taken 3 times.
✗ Branch 123 → 206 not taken.
|
3 | const size_t typeSizeInBytes = module->getDataLayout().getTypeSizeInBits(pointeeType) / 8; |
| 607 | 3 | llvm::ConstantInt *typeSize = builder.getInt64(typeSizeInBytes); | |
| 608 |
3/6✓ Branch 125 → 126 taken 3 times.
✗ Branch 125 → 210 not taken.
✓ Branch 127 → 128 taken 3 times.
✗ Branch 127 → 207 not taken.
✓ Branch 128 → 129 taken 3 times.
✗ Branch 128 → 207 not taken.
|
3 | llvm::Value *newHeapAddress = builder.CreateCall(unsafeAllocFct, {typeSize}); |
| 609 | 3 | insertStore(newHeapAddress, fieldAddress); | |
| 610 | |||
| 611 | // Copy data from the old heap storage to the new one | ||
| 612 | 3 | generateShallowCopy(originalHeapAddress, pointeeType, newHeapAddress, false); | |
| 613 | 3 | insertJump(bExit); | |
| 614 | |||
| 615 | // Switch to exit block | ||
| 616 | 3 | switchToBlock(bExit); | |
| 617 | |||
| 618 | 3 | continue; | |
| 619 | 3 | } | |
| 620 | |||
| 621 | // Shallow copy | ||
| 622 | 283 | llvm::Type *type = fieldType.toLLVMType(sourceFile); | |
| 623 | 283 | generateShallowCopy(originalFieldAddress, type, fieldAddress, false); | |
| 624 | } | ||
| 625 | 236 | } | |
| 626 | |||
| 627 | 1858 | void IRGenerator::generateDefaultCopyCtor(const Function *copyCtorFunction) { | |
| 628 |
3/6✓ Branch 2 → 3 taken 1858 times.
✗ Branch 2 → 6 not taken.
✓ Branch 3 → 4 taken 1858 times.
✗ Branch 3 → 13 not taken.
✓ Branch 4 → 5 taken 1858 times.
✗ Branch 4 → 6 not taken.
|
1858 | assert(copyCtorFunction->implicitDefault && copyCtorFunction->name == CTOR_FUNCTION_NAME); |
| 629 | 2094 | const std::function<void()> generateBody = [&] { generateCopyCtorBodyPreamble(copyCtorFunction); }; | |
| 630 |
1/2✓ Branch 8 → 9 taken 1858 times.
✗ Branch 8 → 11 not taken.
|
1858 | generateImplicitProcedure(generateBody, copyCtorFunction); |
| 631 | 1858 | } | |
| 632 | |||
| 633 | 5 | void IRGenerator::generateMoveCtorBodyPreamble(const Function *moveCtorFunction) { | |
| 634 | // Retrieve struct scope | ||
| 635 | 5 | Scope *structScope = moveCtorFunction->bodyScope->parent; | |
| 636 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 5 times.
|
5 | assert(structScope != nullptr); |
| 637 | |||
| 638 | // Get struct address | ||
| 639 |
1/2✓ Branch 6 → 7 taken 5 times.
✗ Branch 6 → 118 not taken.
|
15 | const SymbolTableEntry *thisEntry = moveCtorFunction->bodyScope->lookupStrict(THIS_VARIABLE_NAME); |
| 640 |
1/2✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 5 times.
|
5 | assert(thisEntry != nullptr); |
| 641 | 5 | llvm::Value *thisPtrPtr = getAddress(thisEntry); | |
| 642 |
1/2✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 5 times.
|
5 | assert(thisPtrPtr != nullptr); |
| 643 | 5 | llvm::Value *thisPtr = nullptr; | |
| 644 |
3/6✓ Branch 17 → 18 taken 5 times.
✗ Branch 17 → 122 not taken.
✓ Branch 18 → 19 taken 5 times.
✗ Branch 18 → 122 not taken.
✓ Branch 19 → 20 taken 5 times.
✗ Branch 19 → 122 not taken.
|
5 | llvm::Type *structType = thisEntry->getQualType().getBase().toLLVMType(sourceFile); |
| 645 | |||
| 646 | // Retrieve the value of the original (source) struct, which is the only function parameter | ||
| 647 | 5 | llvm::Value *originalThisPtr = builder.GetInsertBlock()->getParent()->getArg(1); | |
| 648 | |||
| 649 | 5 | const size_t fieldCount = structScope->getFieldCount(); | |
| 650 |
2/2✓ Branch 114 → 25 taken 7 times.
✓ Branch 114 → 115 taken 5 times.
|
12 | for (size_t fieldIdx = 0; fieldIdx < fieldCount; fieldIdx++) { |
| 651 |
1/2✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 7 times.
|
7 | const SymbolTableEntry *fieldSymbol = structScope->lookupField(fieldIdx); |
| 652 |
2/4✓ Branch 30 → 31 taken 7 times.
✗ Branch 30 → 34 not taken.
✓ Branch 32 → 33 taken 7 times.
✗ Branch 32 → 34 not taken.
|
7 | assert(fieldSymbol != nullptr && fieldSymbol->isField()); |
| 653 | |||
| 654 | // Retrieve the address of the original field (move source) | ||
| 655 |
1/2✓ Branch 38 → 39 taken 7 times.
✗ Branch 38 → 123 not taken.
|
7 | llvm::Value *originalFieldAddress = insertStructGEP(structType, originalThisPtr, fieldIdx); |
| 656 | |||
| 657 | 7 | const QualType &fieldType = fieldSymbol->getQualType(); | |
| 658 | |||
| 659 | // Call move ctor for struct fields if available, otherwise fall back to copy or shallow copy | ||
| 660 |
2/2✓ Branch 43 → 44 taken 2 times.
✓ Branch 43 → 79 taken 5 times.
|
7 | if (fieldType.is(TY_STRUCT)) { |
| 661 | 2 | Scope *matchScope = fieldType.getBodyScope(); | |
| 662 | // First try to find a move ctor (non-const ref param). We scan the manifestations directly via | ||
| 663 | // findMoveCtor rather than FunctionManager::lookup with a non-const ref arg, because lookup permits | ||
| 664 | // const-param-to-non-const-arg "constify" matching and may return the copy ctor as a false positive. | ||
| 665 |
1/2✓ Branch 46 → 47 taken 2 times.
✗ Branch 46 → 54 not taken.
|
2 | if (const Function *moveCtor = FunctionManager::findMoveCtor(matchScope)) { |
| 666 |
2/4✓ Branch 49 → 50 taken 2 times.
✗ Branch 49 → 131 not taken.
✓ Branch 50 → 51 taken 2 times.
✗ Branch 50 → 129 not taken.
|
4 | generateCtorOrDtorCall(fieldSymbol, moveCtor, {originalFieldAddress}); |
| 667 | 2 | continue; | |
| 668 | } | ||
| 669 | // No move ctor: fall back to copy ctor for non-trivially copyable types | ||
| 670 | ✗ | if (!fieldType.isTriviallyCopyable(nullptr)) { | |
| 671 | ✗ | const ArgList copyArgs = {{fieldType.toConstRef(nullptr), false}}; | |
| 672 | ✗ | const Function *copyCtor = FunctionManager::lookup(matchScope, CTOR_FUNCTION_NAME, fieldType, copyArgs, false); | |
| 673 | ✗ | assert(copyCtor != nullptr); | |
| 674 | ✗ | generateCtorOrDtorCall(fieldSymbol, copyCtor, {originalFieldAddress}); | |
| 675 | ✗ | continue; | |
| 676 | ✗ | } | |
| 677 | } | ||
| 678 | |||
| 679 | // Retrieve the address of the new field (move dest) | ||
| 680 |
2/2✓ Branch 79 → 80 taken 4 times.
✓ Branch 79 → 88 taken 1 time.
|
5 | if (!thisPtr) |
| 681 |
2/4✓ Branch 83 → 84 taken 4 times.
✗ Branch 83 → 158 not taken.
✓ Branch 84 → 85 taken 4 times.
✗ Branch 84 → 158 not taken.
|
4 | thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr); |
| 682 |
1/2✓ Branch 91 → 92 taken 5 times.
✗ Branch 91 → 164 not taken.
|
5 | llvm::Value *fieldAddress = insertStructGEP(structType, thisPtr, fieldIdx); |
| 683 | |||
| 684 | // For owning heap fields, transfer ownership: copy the pointer to the destination, and null out the source | ||
| 685 |
2/2✓ Branch 95 → 96 taken 3 times.
✓ Branch 95 → 111 taken 2 times.
|
5 | if (fieldType.isHeap()) { |
| 686 |
1/2✗ Branch 97 → 98 not taken.
✓ Branch 97 → 99 taken 3 times.
|
3 | assert(fieldType.isPtr()); |
| 687 | |||
| 688 | // Load original heap address | ||
| 689 |
2/4✓ Branch 102 → 103 taken 3 times.
✗ Branch 102 → 170 not taken.
✓ Branch 103 → 104 taken 3 times.
✗ Branch 103 → 170 not taken.
|
3 | llvm::Value *originalHeapAddress = insertLoad(builder.getPtrTy(), originalFieldAddress); |
| 690 | // Store it in the destination field | ||
| 691 | 3 | insertStore(originalHeapAddress, fieldAddress); | |
| 692 | // Null out the source field so its dtor does not free the storage | ||
| 693 | 3 | insertStore(llvm::Constant::getNullValue(builder.getPtrTy()), originalFieldAddress); | |
| 694 | |||
| 695 | 3 | continue; | |
| 696 | 3 | } | |
| 697 | |||
| 698 | // Shallow copy non-heap, non-struct (or trivially copyable struct) fields | ||
| 699 | 2 | llvm::Type *type = fieldType.toLLVMType(sourceFile); | |
| 700 | 2 | generateShallowCopy(originalFieldAddress, type, fieldAddress, false); | |
| 701 | } | ||
| 702 | 5 | } | |
| 703 | |||
| 704 | 10 | void IRGenerator::generateDefaultMoveCtor(const Function *moveCtorFunction) { | |
| 705 |
3/6✓ Branch 2 → 3 taken 10 times.
✗ Branch 2 → 6 not taken.
✓ Branch 3 → 4 taken 10 times.
✗ Branch 3 → 13 not taken.
✓ Branch 4 → 5 taken 10 times.
✗ Branch 4 → 6 not taken.
|
10 | assert(moveCtorFunction->implicitDefault && moveCtorFunction->name == CTOR_FUNCTION_NAME); |
| 706 | 15 | const std::function<void()> generateBody = [&] { generateMoveCtorBodyPreamble(moveCtorFunction); }; | |
| 707 |
1/2✓ Branch 8 → 9 taken 10 times.
✗ Branch 8 → 11 not taken.
|
10 | generateImplicitProcedure(generateBody, moveCtorFunction); |
| 708 | 10 | } | |
| 709 | |||
| 710 | 546 | void IRGenerator::generateDtorBodyPreamble(const Function *dtorFunction) { | |
| 711 | // Retrieve struct scope | ||
| 712 | 546 | Scope *structScope = dtorFunction->bodyScope->parent; | |
| 713 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 546 times.
|
546 | assert(structScope != nullptr); |
| 714 | |||
| 715 | // Get struct address | ||
| 716 |
1/2✓ Branch 6 → 7 taken 546 times.
✗ Branch 6 → 73 not taken.
|
1638 | const SymbolTableEntry *thisEntry = dtorFunction->bodyScope->lookupStrict(THIS_VARIABLE_NAME); |
| 717 |
1/2✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 546 times.
|
546 | assert(thisEntry != nullptr); |
| 718 | 546 | llvm::Value *thisPtrPtr = getAddress(thisEntry); | |
| 719 |
1/2✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 546 times.
|
546 | assert(thisPtrPtr != nullptr); |
| 720 | 546 | llvm::Value *thisPtr = nullptr; | |
| 721 |
3/6✓ Branch 17 → 18 taken 546 times.
✗ Branch 17 → 77 not taken.
✓ Branch 18 → 19 taken 546 times.
✗ Branch 18 → 77 not taken.
✓ Branch 19 → 20 taken 546 times.
✗ Branch 19 → 77 not taken.
|
546 | llvm::Type *structType = thisEntry->getQualType().getBase().toLLVMType(sourceFile); |
| 722 | |||
| 723 | 546 | const size_t fieldCount = structScope->getFieldCount(); | |
| 724 |
2/2✓ Branch 69 → 22 taken 1481 times.
✓ Branch 69 → 70 taken 546 times.
|
2027 | for (size_t i = 0; i < fieldCount; i++) { |
| 725 | 1481 | const size_t fieldIdx = fieldCount - 1 - i; // Destruct fields in reverse order | |
| 726 |
1/2✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 1481 times.
|
1481 | const SymbolTableEntry *fieldSymbol = structScope->lookupField(fieldIdx); |
| 727 |
2/4✓ Branch 27 → 28 taken 1481 times.
✗ Branch 27 → 31 not taken.
✓ Branch 29 → 30 taken 1481 times.
✗ Branch 29 → 31 not taken.
|
1481 | assert(fieldSymbol != nullptr && fieldSymbol->isField()); |
| 728 | |||
| 729 | // Call dtor for struct fields | ||
| 730 | 1481 | const QualType &fieldType = fieldSymbol->getQualType(); | |
| 731 |
2/2✓ Branch 34 → 35 taken 802 times.
✓ Branch 34 → 50 taken 679 times.
|
1481 | if (fieldType.is(TY_STRUCT)) { |
| 732 | // Lookup dtor function and generate call if found | ||
| 733 |
5/8✓ Branch 38 → 39 taken 802 times.
✗ Branch 38 → 80 not taken.
✓ Branch 39 → 40 taken 802 times.
✗ Branch 39 → 78 not taken.
✓ Branch 40 → 41 taken 802 times.
✗ Branch 40 → 78 not taken.
✓ Branch 44 → 45 taken 648 times.
✓ Branch 44 → 49 taken 154 times.
|
2406 | if (const Function *dtorFct = FunctionManager::lookup(fieldType.getBodyScope(), DTOR_FUNCTION_NAME, fieldType, {}, false)) |
| 734 |
1/2✓ Branch 46 → 47 taken 648 times.
✗ Branch 46 → 87 not taken.
|
648 | generateCtorOrDtorCall(fieldSymbol, dtorFct, {}); |
| 735 | 802 | continue; | |
| 736 | 802 | } | |
| 737 | |||
| 738 | // Deallocate fields, that are stored on the heap | ||
| 739 |
2/2✓ Branch 51 → 52 taken 159 times.
✓ Branch 51 → 68 taken 520 times.
|
679 | if (fieldType.isHeap()) { |
| 740 | // Retrieve field address | ||
| 741 |
2/2✓ Branch 52 → 53 taken 144 times.
✓ Branch 52 → 61 taken 15 times.
|
159 | if (!thisPtr) |
| 742 |
2/4✓ Branch 56 → 57 taken 144 times.
✗ Branch 56 → 90 not taken.
✓ Branch 57 → 58 taken 144 times.
✗ Branch 57 → 90 not taken.
|
144 | thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr); |
| 743 |
1/2✓ Branch 64 → 65 taken 159 times.
✗ Branch 64 → 96 not taken.
|
159 | llvm::Value *fieldAddress = insertStructGEP(structType, thisPtr, fieldIdx); |
| 744 | // Call dealloc function | ||
| 745 | 159 | generateDeallocCall(fieldAddress); | |
| 746 | } | ||
| 747 | } | ||
| 748 | 546 | } | |
| 749 | |||
| 750 | 1753 | void IRGenerator::generateDefaultDtor(const Function *dtorFunction) { | |
| 751 |
3/6✓ Branch 2 → 3 taken 1753 times.
✗ Branch 2 → 6 not taken.
✓ Branch 3 → 4 taken 1753 times.
✗ Branch 3 → 13 not taken.
✓ Branch 4 → 5 taken 1753 times.
✗ Branch 4 → 6 not taken.
|
1753 | assert(dtorFunction->implicitDefault && dtorFunction->name == DTOR_FUNCTION_NAME); |
| 752 | 2299 | const std::function<void()> generateBody = [&] { generateDtorBodyPreamble(dtorFunction); }; | |
| 753 |
1/2✓ Branch 8 → 9 taken 1753 times.
✗ Branch 8 → 11 not taken.
|
1753 | generateImplicitProcedure(generateBody, dtorFunction); |
| 754 | 1753 | } | |
| 755 | |||
| 756 | 4 | void IRGenerator::generateTestMain() { | |
| 757 | // Collect all test functions | ||
| 758 | 4 | std::vector<const std::vector<const Function *> *> tests; | |
| 759 |
5/8✓ Branch 2 → 3 taken 4 times.
✗ Branch 2 → 138 not taken.
✓ Branch 3 → 4 taken 4 times.
✗ Branch 3 → 138 not taken.
✓ Branch 4 → 5 taken 4 times.
✗ Branch 4 → 138 not taken.
✓ Branch 15 → 6 taken 5 times.
✓ Branch 15 → 16 taken 4 times.
|
9 | for (const auto &sourceFile : resourceManager.sourceFiles | std::views::values) |
| 760 |
1/2✓ Branch 9 → 10 taken 5 times.
✗ Branch 9 → 13 not taken.
|
5 | if (!sourceFile->testFunctions.empty()) |
| 761 |
1/2✓ Branch 11 → 12 taken 5 times.
✗ Branch 11 → 137 not taken.
|
5 | tests.push_back(&sourceFile->testFunctions); |
| 762 | |||
| 763 | // Prepare printf function | ||
| 764 |
1/2✓ Branch 16 → 17 taken 4 times.
✗ Branch 16 → 263 not taken.
|
4 | llvm::Function *printfFct = stdFunctionManager.getPrintfFct(); |
| 765 | |||
| 766 | // Prepare success and error messages | ||
| 767 |
3/6✓ Branch 19 → 20 taken 4 times.
✗ Branch 19 → 147 not taken.
✓ Branch 22 → 23 taken 4 times.
✗ Branch 22 → 141 not taken.
✓ Branch 23 → 24 taken 4 times.
✗ Branch 23 → 139 not taken.
|
16 | llvm::Constant *allStartMsg = createGlobalStringConst("allStartMsg", TEST_ALL_START_MSG, *rootScope->codeLoc); |
| 768 |
3/6✓ Branch 30 → 31 taken 4 times.
✗ Branch 30 → 159 not taken.
✓ Branch 33 → 34 taken 4 times.
✗ Branch 33 → 153 not taken.
✓ Branch 34 → 35 taken 4 times.
✗ Branch 34 → 151 not taken.
|
16 | llvm::Constant *allEndMsg = createGlobalStringConst("allEndMsg", TEST_ALL_END_MSG, *rootScope->codeLoc); |
| 769 |
3/6✓ Branch 41 → 42 taken 4 times.
✗ Branch 41 → 171 not taken.
✓ Branch 44 → 45 taken 4 times.
✗ Branch 44 → 165 not taken.
✓ Branch 45 → 46 taken 4 times.
✗ Branch 45 → 163 not taken.
|
16 | llvm::Constant *fileStartMsg = createGlobalStringConst("fileStartMsg", TEST_FILE_START_MSG, *rootScope->codeLoc); |
| 770 |
3/6✓ Branch 52 → 53 taken 4 times.
✗ Branch 52 → 183 not taken.
✓ Branch 55 → 56 taken 4 times.
✗ Branch 55 → 177 not taken.
✓ Branch 56 → 57 taken 4 times.
✗ Branch 56 → 175 not taken.
|
16 | llvm::Constant *fileEndMsg = createGlobalStringConst("fileEndMsg", TEST_FILE_END_MSG, *rootScope->codeLoc); |
| 771 |
3/6✓ Branch 63 → 64 taken 4 times.
✗ Branch 63 → 195 not taken.
✓ Branch 66 → 67 taken 4 times.
✗ Branch 66 → 189 not taken.
✓ Branch 67 → 68 taken 4 times.
✗ Branch 67 → 187 not taken.
|
16 | llvm::Constant *runMsg = createGlobalStringConst("runMsg", TEST_CASE_RUN_MSG, *rootScope->codeLoc); |
| 772 |
3/6✓ Branch 74 → 75 taken 4 times.
✗ Branch 74 → 207 not taken.
✓ Branch 77 → 78 taken 4 times.
✗ Branch 77 → 201 not taken.
✓ Branch 78 → 79 taken 4 times.
✗ Branch 78 → 199 not taken.
|
16 | llvm::Constant *successMsg = createGlobalStringConst("successMsg", TEST_CASE_SUCCESS_MSG, *rootScope->codeLoc); |
| 773 |
3/6✓ Branch 85 → 86 taken 4 times.
✗ Branch 85 → 219 not taken.
✓ Branch 88 → 89 taken 4 times.
✗ Branch 88 → 213 not taken.
✓ Branch 89 → 90 taken 4 times.
✗ Branch 89 → 211 not taken.
|
16 | llvm::Constant *errorMsg = createGlobalStringConst("errorMsg", TEST_CASE_FAILED_MSG, *rootScope->codeLoc); |
| 774 |
3/6✓ Branch 96 → 97 taken 4 times.
✗ Branch 96 → 231 not taken.
✓ Branch 99 → 100 taken 4 times.
✗ Branch 99 → 225 not taken.
✓ Branch 100 → 101 taken 4 times.
✗ Branch 100 → 223 not taken.
|
16 | llvm::Constant *skippedMsg = createGlobalStringConst("skippedMsg", TEST_CASE_SKIPPED_MSG, *rootScope->codeLoc); |
| 775 | |||
| 776 | // Prepare entry for test main | ||
| 777 |
1/2✓ Branch 105 → 106 taken 4 times.
✗ Branch 105 → 263 not taken.
|
4 | QualType functionType(TY_FUNCTION); |
| 778 |
1/2✓ Branch 106 → 107 taken 4 times.
✗ Branch 106 → 263 not taken.
|
4 | functionType.setQualifiers(TypeQualifiers::of(TY_FUNCTION)); |
| 779 |
1/2✓ Branch 108 → 109 taken 4 times.
✗ Branch 108 → 263 not taken.
|
4 | functionType.makePublic(); |
| 780 |
1/2✓ Branch 111 → 112 taken 4 times.
✗ Branch 111 → 235 not taken.
|
8 | SymbolTableEntry entry(MAIN_FUNCTION_NAME, functionType, rootScope, nullptr, 0, false); |
| 781 | |||
| 782 | // Prepare test main function | ||
| 783 |
4/8✓ Branch 117 → 118 taken 4 times.
✗ Branch 117 → 246 not taken.
✓ Branch 118 → 119 taken 4 times.
✗ Branch 118 → 245 not taken.
✓ Branch 121 → 122 taken 4 times.
✗ Branch 121 → 241 not taken.
✓ Branch 122 → 123 taken 4 times.
✗ Branch 122 → 239 not taken.
|
12 | Function testMain(MAIN_FUNCTION_NAME, &entry, QualType(TY_DYN), QualType(TY_INT), {}, {}, nullptr); |
| 784 | 4 | testMain.used = true; // Mark as used to prevent removal | |
| 785 | 4 | testMain.implicitDefault = true; | |
| 786 | 4 | testMain.mangleFunctionName = false; | |
| 787 | |||
| 788 | // Prepare scope | ||
| 789 |
2/4✓ Branch 127 → 128 taken 4 times.
✗ Branch 127 → 255 not taken.
✓ Branch 128 → 129 taken 4 times.
✗ Branch 128 → 253 not taken.
|
4 | rootScope->createChildScope(testMain.getScopeName(), ScopeType::FUNC_PROC_BODY, nullptr); |
| 790 | |||
| 791 | // Generate | ||
| 792 | ✗ | const std::function<void()> generateBody = [&] { | |
| 793 | // Prepare result variable | ||
| 794 | 4 | std::vector<llvm::Value *> testCaseResults; | |
| 795 |
1/2✓ Branch 3 → 4 taken 4 times.
✗ Branch 3 → 297 not taken.
|
4 | testCaseResults.reserve(tests.size()); |
| 796 | |||
| 797 | // Print start message | ||
| 798 | 5 | const auto accFct = [&](size_t sum, const std::vector<const Function *> *innerVector) { return sum + innerVector->size(); }; | |
| 799 | 4 | const size_t totalTestCount = std::accumulate(tests.begin(), tests.end(), 0, accFct); | |
| 800 |
5/10✓ Branch 7 → 8 taken 4 times.
✗ Branch 7 → 217 not taken.
✓ Branch 8 → 9 taken 4 times.
✗ Branch 8 → 215 not taken.
✓ Branch 10 → 11 taken 4 times.
✗ Branch 10 → 215 not taken.
✓ Branch 12 → 13 taken 4 times.
✗ Branch 12 → 214 not taken.
✓ Branch 13 → 14 taken 4 times.
✗ Branch 13 → 214 not taken.
|
4 | builder.CreateCall(printfFct, {allStartMsg, builder.getInt32(totalTestCount), builder.getInt32(tests.size())}); |
| 801 | |||
| 802 | // Generate a call to each test function | ||
| 803 |
2/2✓ Branch 181 → 16 taken 5 times.
✓ Branch 181 → 182 taken 4 times.
|
13 | for (const std::vector<const Function *> *testSuite : tests) { |
| 804 | // Print test suite prologue | ||
| 805 |
1/2✓ Branch 19 → 20 taken 5 times.
✗ Branch 19 → 287 not taken.
|
5 | const std::string fileName = testSuite->front()->bodyScope->sourceFile->fileName; |
| 806 |
3/6✓ Branch 21 → 22 taken 5 times.
✗ Branch 21 → 285 not taken.
✓ Branch 24 → 25 taken 5 times.
✗ Branch 24 → 220 not taken.
✓ Branch 25 → 26 taken 5 times.
✗ Branch 25 → 218 not taken.
|
10 | llvm::Constant *fileNameValue = createGlobalStringConst("fileName", fileName, testSuite->front()->getDeclCodeLoc()); |
| 807 |
4/8✓ Branch 28 → 29 taken 5 times.
✗ Branch 28 → 227 not taken.
✓ Branch 30 → 31 taken 5 times.
✗ Branch 30 → 225 not taken.
✓ Branch 32 → 33 taken 5 times.
✗ Branch 32 → 224 not taken.
✓ Branch 33 → 34 taken 5 times.
✗ Branch 33 → 224 not taken.
|
5 | builder.CreateCall(printfFct, {fileStartMsg, builder.getInt32(testSuite->size()), fileNameValue}); |
| 808 | |||
| 809 |
3/4✓ Branch 38 → 39 taken 10 times.
✗ Branch 38 → 279 not taken.
✓ Branch 164 → 36 taken 10 times.
✓ Branch 164 → 165 taken 5 times.
|
30 | for (const Function *testFunction : *testSuite) { |
| 810 |
1/2✗ Branch 50 → 51 not taken.
✓ Branch 50 → 52 taken 10 times.
|
10 | assert(testFunction->isNormalFunction()); |
| 811 |
1/2✗ Branch 53 → 54 not taken.
✓ Branch 53 → 55 taken 10 times.
|
10 | assert(testFunction->paramList.empty()); |
| 812 | |||
| 813 | // Retrieve attribute list for the test function | ||
| 814 |
2/4✓ Branch 55 → 56 taken 10 times.
✗ Branch 55 → 279 not taken.
✗ Branch 56 → 57 not taken.
✓ Branch 56 → 58 taken 10 times.
|
10 | assert(testFunction->declNode->isFctOrProcDef()); |
| 815 |
1/2✓ Branch 58 → 59 taken 10 times.
✗ Branch 58 → 60 not taken.
|
10 | const auto fctDefNode = spice_pointer_cast<FctDefBaseNode *>(testFunction->declNode); |
| 816 |
1/2✗ Branch 65 → 66 not taken.
✓ Branch 65 → 67 taken 10 times.
|
10 | assert(fctDefNode->attrs != nullptr); |
| 817 | 10 | const AttrLstNode *attrs = fctDefNode->attrs->attrLst; | |
| 818 |
3/6✓ Branch 69 → 70 taken 10 times.
✗ Branch 69 → 230 not taken.
✓ Branch 70 → 71 taken 10 times.
✗ Branch 70 → 228 not taken.
✗ Branch 71 → 72 not taken.
✓ Branch 71 → 73 taken 10 times.
|
20 | assert(attrs->getAttrValueByName(ATTR_TEST)->boolValue); // The test attribute must be present |
| 819 |
2/4✓ Branch 77 → 78 taken 10 times.
✗ Branch 77 → 236 not taken.
✓ Branch 78 → 79 taken 10 times.
✗ Branch 78 → 234 not taken.
|
20 | const CompileTimeValue *testSkipAttr = attrs->getAttrValueByName(ATTR_TEST_SKIP); |
| 820 |
4/4✓ Branch 81 → 82 taken 3 times.
✓ Branch 81 → 84 taken 7 times.
✓ Branch 82 → 83 taken 1 time.
✓ Branch 82 → 84 taken 2 times.
|
10 | const bool skipTest = testSkipAttr && testSkipAttr->boolValue; |
| 821 |
2/4✓ Branch 87 → 88 taken 10 times.
✗ Branch 87 → 242 not taken.
✓ Branch 88 → 89 taken 10 times.
✗ Branch 88 → 240 not taken.
|
20 | const CompileTimeValue *testNameAttr = attrs->getAttrValueByName(ATTR_TEST_NAME); |
| 822 | |||
| 823 | // Prepare test name | ||
| 824 |
1/2✓ Branch 91 → 92 taken 10 times.
✗ Branch 91 → 279 not taken.
|
10 | std::stringstream testName; |
| 825 |
1/2✓ Branch 92 → 93 taken 10 times.
✗ Branch 92 → 277 not taken.
|
10 | testName << testFunction->name; |
| 826 |
2/2✓ Branch 93 → 94 taken 1 time.
✓ Branch 93 → 98 taken 9 times.
|
10 | if (testNameAttr) |
| 827 |
4/8✓ Branch 94 → 95 taken 1 time.
✗ Branch 94 → 277 not taken.
✓ Branch 95 → 96 taken 1 time.
✗ Branch 95 → 277 not taken.
✓ Branch 96 → 97 taken 1 time.
✗ Branch 96 → 277 not taken.
✓ Branch 97 → 98 taken 1 time.
✗ Branch 97 → 277 not taken.
|
1 | testName << " (" << resourceManager.compileTimeStringValues.at(testNameAttr->stringValueOffset) << ")"; |
| 828 | |||
| 829 | // Print test case run message | ||
| 830 |
4/8✓ Branch 98 → 99 taken 10 times.
✗ Branch 98 → 277 not taken.
✓ Branch 99 → 100 taken 10 times.
✗ Branch 99 → 254 not taken.
✓ Branch 102 → 103 taken 10 times.
✗ Branch 102 → 248 not taken.
✓ Branch 103 → 104 taken 10 times.
✗ Branch 103 → 246 not taken.
|
30 | llvm::Constant *testNameValue = createGlobalStringConst("testName", testName.str(), testFunction->getDeclCodeLoc()); |
| 831 |
5/8✓ Branch 107 → 108 taken 10 times.
✗ Branch 107 → 258 not taken.
✓ Branch 109 → 110 taken 10 times.
✗ Branch 109 → 255 not taken.
✓ Branch 110 → 111 taken 10 times.
✗ Branch 110 → 255 not taken.
✓ Branch 111 → 112 taken 1 time.
✓ Branch 111 → 117 taken 9 times.
|
10 | builder.CreateCall(printfFct, {runMsg, testNameValue}); |
| 832 | |||
| 833 |
2/2✓ Branch 111 → 112 taken 1 time.
✓ Branch 111 → 117 taken 9 times.
|
10 | if (skipTest) { |
| 834 | // Print test case skip message | ||
| 835 |
3/6✓ Branch 112 → 113 taken 1 time.
✗ Branch 112 → 262 not taken.
✓ Branch 114 → 115 taken 1 time.
✗ Branch 114 → 259 not taken.
✓ Branch 115 → 116 taken 1 time.
✗ Branch 115 → 259 not taken.
|
1 | builder.CreateCall(printfFct, {skippedMsg, testNameValue}); |
| 836 | 1 | continue; | |
| 837 | } | ||
| 838 | |||
| 839 | // Test function is not defined in the current module -> declare it | ||
| 840 |
1/2✓ Branch 117 → 118 taken 9 times.
✗ Branch 117 → 277 not taken.
|
9 | const std::string mangledName = testFunction->getMangledName(); |
| 841 |
3/4✓ Branch 119 → 120 taken 9 times.
✗ Branch 119 → 263 not taken.
✓ Branch 120 → 121 taken 2 times.
✓ Branch 120 → 133 taken 7 times.
|
9 | if (!module->getFunction(mangledName)) { |
| 842 |
2/4✓ Branch 121 → 122 taken 2 times.
✗ Branch 121 → 275 not taken.
✗ Branch 122 → 123 not taken.
✓ Branch 122 → 124 taken 2 times.
|
2 | assert(testFunction->returnType.is(TY_BOOL)); |
| 843 |
1/2✗ Branch 125 → 126 not taken.
✓ Branch 125 → 127 taken 2 times.
|
2 | assert(testFunction->paramList.empty()); |
| 844 |
2/4✓ Branch 128 → 129 taken 2 times.
✗ Branch 128 → 264 not taken.
✓ Branch 129 → 130 taken 2 times.
✗ Branch 129 → 264 not taken.
|
2 | llvm::FunctionType *fctType = llvm::FunctionType::get(builder.getInt1Ty(), {}, false); |
| 845 |
1/2✓ Branch 131 → 132 taken 2 times.
✗ Branch 131 → 265 not taken.
|
2 | module->getOrInsertFunction(mangledName, fctType); |
| 846 | } | ||
| 847 | |||
| 848 | // Call test function | ||
| 849 |
1/2✓ Branch 134 → 135 taken 9 times.
✗ Branch 134 → 266 not taken.
|
9 | llvm::Function *callee = module->getFunction(mangledName); |
| 850 |
1/2✗ Branch 135 → 136 not taken.
✓ Branch 135 → 137 taken 9 times.
|
9 | assert(callee != nullptr); |
| 851 |
4/8✓ Branch 137 → 138 taken 9 times.
✗ Branch 137 → 269 not taken.
✓ Branch 139 → 140 taken 9 times.
✗ Branch 139 → 267 not taken.
✓ Branch 140 → 141 taken 9 times.
✗ Branch 140 → 267 not taken.
✓ Branch 141 → 142 taken 9 times.
✗ Branch 141 → 275 not taken.
|
9 | llvm::Value *testCaseResult = builder.CreateCall(callee); |
| 852 |
1/2✓ Branch 141 → 142 taken 9 times.
✗ Branch 141 → 275 not taken.
|
9 | testCaseResults.push_back(testCaseResult); |
| 853 | |||
| 854 | // Print test case result message | ||
| 855 |
3/6✓ Branch 142 → 143 taken 9 times.
✗ Branch 142 → 270 not taken.
✓ Branch 143 → 144 taken 9 times.
✗ Branch 143 → 270 not taken.
✓ Branch 144 → 145 taken 9 times.
✗ Branch 144 → 274 not taken.
|
9 | llvm::Value *message = builder.CreateSelect(testCaseResult, successMsg, errorMsg); |
| 856 |
3/6✓ Branch 144 → 145 taken 9 times.
✗ Branch 144 → 274 not taken.
✓ Branch 146 → 147 taken 9 times.
✗ Branch 146 → 271 not taken.
✓ Branch 147 → 148 taken 9 times.
✗ Branch 147 → 271 not taken.
|
9 | builder.CreateCall(printfFct, {message, testNameValue}); |
| 857 |
2/2✓ Branch 151 → 152 taken 9 times.
✓ Branch 151 → 154 taken 1 time.
|
10 | } |
| 858 | |||
| 859 | // Print test suite epilogue | ||
| 860 |
4/8✓ Branch 165 → 166 taken 5 times.
✗ Branch 165 → 284 not taken.
✓ Branch 167 → 168 taken 5 times.
✗ Branch 167 → 282 not taken.
✓ Branch 169 → 170 taken 5 times.
✗ Branch 169 → 281 not taken.
✓ Branch 170 → 171 taken 5 times.
✗ Branch 170 → 281 not taken.
|
5 | builder.CreateCall(printfFct, {fileEndMsg, builder.getInt32(testSuite->size()), fileNameValue}); |
| 861 | 5 | } | |
| 862 | |||
| 863 | // Print end message | ||
| 864 |
6/12✓ Branch 182 → 183 taken 4 times.
✗ Branch 182 → 292 not taken.
✓ Branch 183 → 184 taken 4 times.
✗ Branch 183 → 290 not taken.
✓ Branch 185 → 186 taken 4 times.
✗ Branch 185 → 290 not taken.
✓ Branch 187 → 188 taken 4 times.
✗ Branch 187 → 289 not taken.
✓ Branch 188 → 189 taken 4 times.
✗ Branch 188 → 289 not taken.
✓ Branch 189 → 190 taken 4 times.
✗ Branch 189 → 297 not taken.
|
4 | builder.CreateCall(printfFct, {allEndMsg, builder.getInt32(totalTestCount), builder.getInt32(tests.size())}); |
| 865 | |||
| 866 | // Compute overall result | ||
| 867 |
1/2✓ Branch 189 → 190 taken 4 times.
✗ Branch 189 → 297 not taken.
|
4 | llvm::Value *overallResult = builder.getTrue(); |
| 868 |
2/2✓ Branch 205 → 192 taken 9 times.
✓ Branch 205 → 206 taken 4 times.
|
17 | for (llvm::Value *testCaseResult : testCaseResults) |
| 869 |
2/4✓ Branch 194 → 195 taken 9 times.
✗ Branch 194 → 293 not taken.
✓ Branch 195 → 196 taken 9 times.
✗ Branch 195 → 293 not taken.
|
9 | overallResult = builder.CreateAnd(overallResult, testCaseResult); |
| 870 | |||
| 871 | // Return code must be 0 for success and 1 for failure, so we need to invert the result and zero extend to 32 bit | ||
| 872 |
3/6✓ Branch 206 → 207 taken 4 times.
✗ Branch 206 → 295 not taken.
✓ Branch 207 → 208 taken 4 times.
✗ Branch 207 → 295 not taken.
✓ Branch 208 → 209 taken 4 times.
✗ Branch 208 → 296 not taken.
|
4 | llvm::Value *overallResultNegated = builder.CreateNot(overallResult); |
| 873 |
4/8✓ Branch 208 → 209 taken 4 times.
✗ Branch 208 → 296 not taken.
✓ Branch 209 → 210 taken 4 times.
✗ Branch 209 → 296 not taken.
✓ Branch 210 → 211 taken 4 times.
✗ Branch 210 → 296 not taken.
✓ Branch 211 → 212 taken 4 times.
✗ Branch 211 → 297 not taken.
|
4 | llvm::Value *exitCode = builder.CreateZExt(overallResultNegated, builder.getInt32Ty()); |
| 874 |
1/2✓ Branch 211 → 212 taken 4 times.
✗ Branch 211 → 297 not taken.
|
4 | builder.CreateRet(exitCode); |
| 875 |
1/2✓ Branch 130 → 131 taken 4 times.
✗ Branch 130 → 256 not taken.
|
8 | }; |
| 876 |
1/2✓ Branch 131 → 132 taken 4 times.
✗ Branch 131 → 257 not taken.
|
4 | generateImplicitFunction(generateBody, &testMain); |
| 877 | 4 | } | |
| 878 | |||
| 879 | } // namespace spice::compiler | ||
| 880 |