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 | |||
| 13 | #include <llvm/IR/Module.h> | ||
| 14 | |||
| 15 | namespace spice::compiler { | ||
| 16 | |||
| 17 | // String placeholders for builtin testing output | ||
| 18 | static const char *const TEST_ALL_START_MSG = "[==========] Running %d test(s) from %d source file(s)\n"; | ||
| 19 | static const char *const TEST_ALL_END_MSG = "[==========] Ran %d test(s) from %d source file(s)\n"; | ||
| 20 | static const char *const TEST_FILE_START_MSG = "[----------] Running %d test(s) from %s\n"; | ||
| 21 | static const char *const TEST_FILE_END_MSG = "[----------] Ran %d test(s) from %s\n\n"; | ||
| 22 | static const char *const TEST_CASE_RUN_MSG = "[ RUN ] %s\n"; | ||
| 23 | static const char *const TEST_CASE_SUCCESS_MSG = "\033[1m\033[32m[ PASSED ]\033[0m\033[22m %s\n"; | ||
| 24 | static const char *const TEST_CASE_FAILED_MSG = "\033[1m\033[31m[ FAILED ]\033[0m\033[22m %s\n"; | ||
| 25 | static const char *const TEST_CASE_SKIPPED_MSG = "\033[1m\033[33m[ SKIPPED ]\033[0m\033[22m %s\n"; | ||
| 26 | |||
| 27 | 13 | llvm::Value *IRGenerator::doImplicitCast(llvm::Value *src, QualType dstSTy, QualType srcSTy) { | |
| 28 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 13 times.
|
13 | assert(srcSTy != dstSTy); // We only need to cast implicitly, if the types do not match exactly |
| 29 | |||
| 30 | // Unpack the pointers until a pointer of another type is met | ||
| 31 | 13 | size_t loadCounter = 0; | |
| 32 |
1/2✗ Branch 16 → 6 not taken.
✓ Branch 16 → 17 taken 13 times.
|
13 | while (srcSTy.isPtr()) { |
| 33 | ✗ | src = insertLoad(srcSTy, src); | |
| 34 | ✗ | srcSTy = srcSTy.getContained(); | |
| 35 | ✗ | dstSTy = dstSTy.getContained(); | |
| 36 | ✗ | loadCounter++; | |
| 37 | } | ||
| 38 | // GEP or bit-cast | ||
| 39 |
3/6✓ Branch 18 → 19 taken 13 times.
✗ Branch 18 → 22 not taken.
✓ Branch 20 → 21 taken 13 times.
✗ Branch 20 → 22 not taken.
✓ Branch 23 → 24 taken 13 times.
✗ Branch 23 → 35 not taken.
|
13 | if (dstSTy.isArray() && srcSTy.isArray()) { // Special case that is used for passing arrays as pointer to functions |
| 40 |
2/4✓ Branch 24 → 25 taken 13 times.
✗ Branch 24 → 72 not taken.
✓ Branch 25 → 26 taken 13 times.
✗ Branch 25 → 72 not taken.
|
13 | llvm::Value *indices[2] = {builder.getInt64(0), builder.getInt32(0)}; |
| 41 |
2/4✓ Branch 30 → 31 taken 13 times.
✗ Branch 30 → 65 not taken.
✓ Branch 31 → 32 taken 13 times.
✗ Branch 31 → 65 not taken.
|
13 | src = insertInBoundsGEP(srcSTy.toLLVMType(sourceFile), src, indices); |
| 42 | } else { | ||
| 43 | ✗ | src = insertLoad(srcSTy, src); | |
| 44 | ✗ | src = builder.CreateBitCast(src, dstSTy.toLLVMType(sourceFile)); | |
| 45 | } | ||
| 46 | // Pack the pointers together again | ||
| 47 |
1/2✗ Branch 54 → 46 not taken.
✓ Branch 54 → 55 taken 13 times.
|
13 | for (; loadCounter > 0; loadCounter--) { |
| 48 | ✗ | llvm::Value *newActualArg = insertAlloca(srcSTy); | |
| 49 | ✗ | insertStore(src, newActualArg); | |
| 50 | ✗ | src = newActualArg; | |
| 51 | } | ||
| 52 | 13 | return src; | |
| 53 | } | ||
| 54 | |||
| 55 | 30966 | void IRGenerator::generateScopeCleanup(const StmtLstNode *node) const { | |
| 56 | // Do not clean up if the block is already terminated | ||
| 57 |
2/2✓ Branch 2 → 3 taken 11153 times.
✓ Branch 2 → 4 taken 19813 times.
|
30966 | if (blockAlreadyTerminated) |
| 58 | 11153 | return; | |
| 59 | |||
| 60 | // Call all dtor functions | ||
| 61 | 19813 | const auto &[dtorFunctionsToCall, heapVarsToFree] = node->resourcesToCleanup.at(manIdx); | |
| 62 |
2/2✓ Branch 15 → 7 taken 1019 times.
✓ Branch 15 → 16 taken 19813 times.
|
20832 | for (auto [entry, dtor] : dtorFunctionsToCall) |
| 63 |
1/2✓ Branch 11 → 12 taken 1019 times.
✗ Branch 11 → 38 not taken.
|
1019 | generateCtorOrDtorCall(entry, dtor, {}); |
| 64 | |||
| 65 | // Deallocate all heap variables that go out of scope and are currently owned | ||
| 66 |
2/2✓ Branch 23 → 18 taken 4 times.
✓ Branch 23 → 24 taken 19813 times.
|
19817 | for (const SymbolTableEntry *entry : heapVarsToFree) |
| 67 |
2/4✓ Branch 19 → 20 taken 4 times.
✗ Branch 19 → 42 not taken.
✓ Branch 20 → 21 taken 4 times.
✗ Branch 20 → 42 not taken.
|
4 | generateDeallocCall(entry->getAddress()); |
| 68 | |||
| 69 | // Generate lifetime end markers | ||
| 70 |
2/2✓ Branch 24 → 25 taken 33 times.
✓ Branch 24 → 37 taken 19780 times.
|
19813 | if (cliOptions.useLifetimeMarkers) { |
| 71 |
3/4✓ Branch 25 → 26 taken 33 times.
✗ Branch 25 → 45 not taken.
✓ Branch 34 → 28 taken 9 times.
✓ Branch 34 → 35 taken 33 times.
|
42 | for (const SymbolTableEntry *var : currentScope->getVarsGoingOutOfScope()) { |
| 72 |
1/2✓ Branch 29 → 30 taken 9 times.
✗ Branch 29 → 43 not taken.
|
9 | llvm::Value *address = var->getAddress(); |
| 73 |
1/2✓ Branch 30 → 31 taken 9 times.
✗ Branch 30 → 32 not taken.
|
9 | if (address != nullptr) |
| 74 |
1/2✓ Branch 31 → 32 taken 9 times.
✗ Branch 31 → 43 not taken.
|
9 | builder.CreateLifetimeEnd(address); |
| 75 | 33 | } | |
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | 1739 | void IRGenerator::generateFctDecl(const Function *fct, const std::vector<llvm::Value *> &args) const { | |
| 80 | // Retrieve metadata for the function | ||
| 81 |
1/2✓ Branch 2 → 3 taken 1739 times.
✗ Branch 2 → 82 not taken.
|
1739 | const std::string mangledName = fct->getMangledName(); |
| 82 | |||
| 83 | // Function is not defined in the current module -> declare it | ||
| 84 |
3/4✓ Branch 4 → 5 taken 1739 times.
✗ Branch 4 → 63 not taken.
✓ Branch 5 → 6 taken 429 times.
✓ Branch 5 → 61 taken 1310 times.
|
1739 | if (!module->getFunction(mangledName)) { |
| 85 | 429 | std::vector<llvm::Type *> paramTypes; | |
| 86 |
2/2✓ Branch 13 → 8 taken 514 times.
✓ Branch 13 → 14 taken 429 times.
|
943 | for (const llvm::Value *argValue : args) |
| 87 |
1/2✓ Branch 10 → 11 taken 514 times.
✗ Branch 10 → 64 not taken.
|
514 | paramTypes.push_back(argValue->getType()); |
| 88 |
2/6✗ Branch 17 → 18 not taken.
✓ Branch 17 → 20 taken 429 times.
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 77 not taken.
✓ Branch 20 → 21 taken 429 times.
✗ Branch 20 → 77 not taken.
|
429 | llvm::Type *returnType = fct->isFunction() ? fct->returnType.toLLVMType(sourceFile) : builder.getVoidTy(); |
| 89 |
1/2✓ Branch 23 → 24 taken 429 times.
✗ Branch 23 → 66 not taken.
|
429 | llvm::FunctionType *fctType = llvm::FunctionType::get(returnType, paramTypes, false); |
| 90 |
2/4✓ Branch 25 → 26 taken 429 times.
✗ Branch 25 → 67 not taken.
✓ Branch 26 → 27 taken 429 times.
✗ Branch 26 → 77 not taken.
|
429 | module->getOrInsertFunction(mangledName, fctType); |
| 91 | |||
| 92 |
1/2✓ Branch 29 → 30 taken 429 times.
✗ Branch 29 → 59 not taken.
|
429 | if (fct->isMethod()) { |
| 93 | // Get callee function | ||
| 94 |
1/2✓ Branch 31 → 32 taken 429 times.
✗ Branch 31 → 68 not taken.
|
429 | llvm::Function *callee = module->getFunction(mangledName); |
| 95 |
1/2✗ Branch 32 → 33 not taken.
✓ Branch 32 → 34 taken 429 times.
|
429 | assert(callee != nullptr); |
| 96 | |||
| 97 | // Set attributes to 'this' param | ||
| 98 | // Get 'this' entry | ||
| 99 |
1/2✓ Branch 36 → 37 taken 429 times.
✗ Branch 36 → 71 not taken.
|
1287 | const SymbolTableEntry *thisEntry = fct->bodyScope->lookupStrict(THIS_VARIABLE_NAME); |
| 100 |
1/2✗ Branch 42 → 43 not taken.
✓ Branch 42 → 44 taken 429 times.
|
429 | assert(thisEntry != nullptr); |
| 101 |
3/6✓ Branch 44 → 45 taken 429 times.
✗ Branch 44 → 75 not taken.
✓ Branch 45 → 46 taken 429 times.
✗ Branch 45 → 75 not taken.
✓ Branch 46 → 47 taken 429 times.
✗ Branch 46 → 75 not taken.
|
429 | llvm::Type *structType = thisEntry->getQualType().getContained().toLLVMType(sourceFile); |
| 102 |
1/2✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 429 times.
|
429 | assert(structType != nullptr); |
| 103 |
1/2✓ Branch 49 → 50 taken 429 times.
✗ Branch 49 → 77 not taken.
|
429 | callee->addParamAttr(0, llvm::Attribute::NoUndef); |
| 104 |
1/2✓ Branch 50 → 51 taken 429 times.
✗ Branch 50 → 77 not taken.
|
429 | callee->addParamAttr(0, llvm::Attribute::NonNull); |
| 105 |
3/6✓ Branch 52 → 53 taken 429 times.
✗ Branch 52 → 76 not taken.
✓ Branch 53 → 54 taken 429 times.
✗ Branch 53 → 76 not taken.
✓ Branch 54 → 55 taken 429 times.
✗ Branch 54 → 76 not taken.
|
429 | callee->addDereferenceableParamAttr(0, module->getDataLayout().getTypeStoreSize(structType)); |
| 106 |
3/6✓ Branch 56 → 57 taken 429 times.
✗ Branch 56 → 77 not taken.
✓ Branch 57 → 58 taken 429 times.
✗ Branch 57 → 77 not taken.
✓ Branch 58 → 59 taken 429 times.
✗ Branch 58 → 77 not taken.
|
429 | callee->addParamAttr(0, llvm::Attribute::getWithAlignment(context, module->getDataLayout().getABITypeAlign(structType))); |
| 107 | } | ||
| 108 | 429 | } | |
| 109 | 1739 | } | |
| 110 | |||
| 111 | 1739 | llvm::CallInst *IRGenerator::generateFctCall(const Function *fct, const std::vector<llvm::Value *> &args) const { | |
| 112 | // Retrieve metadata for the function | ||
| 113 |
1/2✓ Branch 2 → 3 taken 1739 times.
✗ Branch 2 → 57 not taken.
|
1739 | const std::string mangledName = fct->getMangledName(); |
| 114 | |||
| 115 | // Get callee function | ||
| 116 |
1/2✓ Branch 4 → 5 taken 1739 times.
✗ Branch 4 → 43 not taken.
|
1739 | llvm::Function *callee = module->getFunction(mangledName); |
| 117 |
1/2✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 1739 times.
|
1739 | assert(callee != nullptr); |
| 118 | |||
| 119 | // Generate function call | ||
| 120 |
4/8✓ Branch 7 → 8 taken 1739 times.
✗ Branch 7 → 46 not taken.
✓ Branch 9 → 10 taken 1739 times.
✗ Branch 9 → 44 not taken.
✓ Branch 10 → 11 taken 1739 times.
✗ Branch 10 → 44 not taken.
✓ Branch 11 → 12 taken 1739 times.
✗ Branch 11 → 55 not taken.
|
1739 | llvm::CallInst *callInst = builder.CreateCall(callee, args); |
| 121 | |||
| 122 | // Set attributes to 'this' param | ||
| 123 |
1/2✓ Branch 14 → 15 taken 1739 times.
✗ Branch 14 → 40 not taken.
|
1739 | if (fct->isMethod()) { |
| 124 | // Get 'this' entry | ||
| 125 |
1/2✓ Branch 17 → 18 taken 1739 times.
✗ Branch 17 → 49 not taken.
|
5217 | const SymbolTableEntry *thisEntry = fct->bodyScope->lookupStrict(THIS_VARIABLE_NAME); |
| 126 |
1/2✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 1739 times.
|
1739 | assert(thisEntry != nullptr); |
| 127 |
3/6✓ Branch 25 → 26 taken 1739 times.
✗ Branch 25 → 53 not taken.
✓ Branch 26 → 27 taken 1739 times.
✗ Branch 26 → 53 not taken.
✓ Branch 27 → 28 taken 1739 times.
✗ Branch 27 → 53 not taken.
|
1739 | llvm::Type *structType = thisEntry->getQualType().getContained().toLLVMType(sourceFile); |
| 128 |
1/2✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 1739 times.
|
1739 | assert(structType != nullptr); |
| 129 |
1/2✓ Branch 30 → 31 taken 1739 times.
✗ Branch 30 → 55 not taken.
|
1739 | callInst->addParamAttr(0, llvm::Attribute::NoUndef); |
| 130 |
1/2✓ Branch 31 → 32 taken 1739 times.
✗ Branch 31 → 55 not taken.
|
1739 | callInst->addParamAttr(0, llvm::Attribute::NonNull); |
| 131 |
3/6✓ Branch 33 → 34 taken 1739 times.
✗ Branch 33 → 54 not taken.
✓ Branch 34 → 35 taken 1739 times.
✗ Branch 34 → 54 not taken.
✓ Branch 35 → 36 taken 1739 times.
✗ Branch 35 → 54 not taken.
|
1739 | callInst->addDereferenceableParamAttr(0, module->getDataLayout().getTypeStoreSize(structType)); |
| 132 |
3/6✓ Branch 37 → 38 taken 1739 times.
✗ Branch 37 → 55 not taken.
✓ Branch 38 → 39 taken 1739 times.
✗ Branch 38 → 55 not taken.
✓ Branch 39 → 40 taken 1739 times.
✗ Branch 39 → 55 not taken.
|
1739 | callInst->addParamAttr(0, llvm::Attribute::getWithAlignment(context, module->getDataLayout().getABITypeAlign(structType))); |
| 133 | } | ||
| 134 | |||
| 135 | 1739 | return callInst; | |
| 136 | 1739 | } | |
| 137 | |||
| 138 | ✗ | llvm::Value *IRGenerator::generateFctDeclAndCall(const Function *fct, const std::vector<llvm::Value *> &args) const { | |
| 139 | ✗ | generateFctDecl(fct, args); | |
| 140 | ✗ | return generateFctCall(fct, args); | |
| 141 | } | ||
| 142 | |||
| 143 | 1739 | void IRGenerator::generateProcDeclAndCall(const Function *proc, const std::vector<llvm::Value *> &args) const { | |
| 144 | 1739 | generateFctDecl(proc, args); | |
| 145 | 1739 | (void)generateFctCall(proc, args); | |
| 146 | 1739 | } | |
| 147 | |||
| 148 | 1304 | void IRGenerator::generateCtorOrDtorCall(const SymbolTableEntry *entry, const Function *ctorOrDtor, | |
| 149 | const std::vector<llvm::Value *> &args) const { | ||
| 150 | // Retrieve address of the struct variable. For fields this is the 'this' variable, otherwise use the normal address | ||
| 151 | llvm::Value *structAddr; | ||
| 152 |
2/2✓ Branch 3 → 4 taken 111 times.
✓ Branch 3 → 41 taken 1193 times.
|
1304 | if (entry->isField()) { |
| 153 | // Take 'this' var as base pointer | ||
| 154 |
1/2✓ Branch 6 → 7 taken 111 times.
✗ Branch 6 → 50 not taken.
|
333 | const SymbolTableEntry *thisVar = currentScope->lookupStrict(THIS_VARIABLE_NAME); |
| 155 |
1/2✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 111 times.
|
111 | assert(thisVar != nullptr); |
| 156 |
7/14✓ Branch 14 → 15 taken 111 times.
✗ Branch 14 → 54 not taken.
✓ Branch 15 → 16 taken 111 times.
✗ Branch 15 → 54 not taken.
✓ Branch 16 → 17 taken 111 times.
✗ Branch 16 → 22 not taken.
✓ Branch 17 → 18 taken 111 times.
✗ Branch 17 → 54 not taken.
✓ Branch 18 → 19 taken 111 times.
✗ Branch 18 → 54 not taken.
✓ Branch 19 → 20 taken 111 times.
✗ Branch 19 → 54 not taken.
✓ Branch 20 → 21 taken 111 times.
✗ Branch 20 → 22 not taken.
|
111 | assert(thisVar->getQualType().isPtr() && thisVar->getQualType().getContained().is(TY_STRUCT)); |
| 157 |
3/6✓ Branch 23 → 24 taken 111 times.
✗ Branch 23 → 55 not taken.
✓ Branch 24 → 25 taken 111 times.
✗ Branch 24 → 55 not taken.
✓ Branch 25 → 26 taken 111 times.
✗ Branch 25 → 55 not taken.
|
111 | llvm::Type *thisType = thisVar->getQualType().getContained().toLLVMType(sourceFile); |
| 158 |
4/8✓ Branch 28 → 29 taken 111 times.
✗ Branch 28 → 58 not taken.
✓ Branch 29 → 30 taken 111 times.
✗ Branch 29 → 56 not taken.
✓ Branch 30 → 31 taken 111 times.
✗ Branch 30 → 56 not taken.
✓ Branch 31 → 32 taken 111 times.
✗ Branch 31 → 56 not taken.
|
222 | llvm::Value *thisPtr = insertLoad(builder.getPtrTy(), thisVar->getAddress()); |
| 159 | // Add field offset | ||
| 160 |
1/2✓ Branch 37 → 38 taken 111 times.
✗ Branch 37 → 62 not taken.
|
111 | structAddr = insertStructGEP(thisType, thisPtr, entry->orderIndex); |
| 161 | } else { | ||
| 162 | 1193 | structAddr = entry->getAddress(); | |
| 163 | // For optional parameter initializers we need this exception | ||
| 164 |
2/2✓ Branch 42 → 43 taken 5 times.
✓ Branch 42 → 44 taken 1188 times.
|
1193 | if (!structAddr) |
| 165 | 5 | return; | |
| 166 | } | ||
| 167 |
1/2✗ Branch 44 → 45 not taken.
✓ Branch 44 → 46 taken 1299 times.
|
1299 | assert(structAddr != nullptr); |
| 168 | 1299 | generateCtorOrDtorCall(structAddr, ctorOrDtor, args); | |
| 169 | } | ||
| 170 | |||
| 171 | 1739 | void IRGenerator::generateCtorOrDtorCall(llvm::Value *structAddr, const Function *ctorOrDtor, | |
| 172 | const std::vector<llvm::Value *> &args) const { | ||
| 173 | // Build parameter list | ||
| 174 |
1/2✓ Branch 4 → 5 taken 1739 times.
✗ Branch 4 → 14 not taken.
|
3478 | std::vector argValues = {structAddr}; |
| 175 |
1/2✓ Branch 10 → 11 taken 1739 times.
✗ Branch 10 → 18 not taken.
|
1739 | argValues.insert(argValues.end(), args.begin(), args.end()); |
| 176 | |||
| 177 | // Generate function call | ||
| 178 |
1/2✓ Branch 11 → 12 taken 1739 times.
✗ Branch 11 → 20 not taken.
|
1739 | generateProcDeclAndCall(ctorOrDtor, argValues); |
| 179 | 1739 | } | |
| 180 | |||
| 181 | 78 | void IRGenerator::generateDeallocCall(llvm::Value *variableAddress) const { | |
| 182 | // Abort if the address is not set. This can happen when leaving the scope of a dtor, which already freed the heap memory | ||
| 183 |
2/2✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 77 times.
|
78 | if (!variableAddress) |
| 184 | 1 | return; | |
| 185 | |||
| 186 | // In case of string runtime, call free manually. Otherwise, use the memory_rt implementation of sDealloc() | ||
| 187 |
1/2✗ Branch 7 → 8 not taken.
✓ Branch 7 → 14 taken 77 times.
|
154 | if (sourceFile->isStringRT()) { |
| 188 | ✗ | llvm::Function *freeFct = stdFunctionManager.getFreeFct(); | |
| 189 | ✗ | builder.CreateCall(freeFct, variableAddress); | |
| 190 | } else { | ||
| 191 | 77 | llvm::Function *deallocFct = stdFunctionManager.getDeallocBytePtrRefFct(); | |
| 192 |
3/6✓ Branch 15 → 16 taken 77 times.
✗ Branch 15 → 26 not taken.
✓ Branch 17 → 18 taken 77 times.
✗ Branch 17 → 24 not taken.
✓ Branch 18 → 19 taken 77 times.
✗ Branch 18 → 24 not taken.
|
77 | builder.CreateCall(deallocFct, variableAddress); |
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | 4 | llvm::Function *IRGenerator::generateImplicitFunction(const std::function<void()> &generateBody, const Function *spiceFunc) { | |
| 197 | // Only focus on method procedures | ||
| 198 |
1/2✓ Branch 2 → 3 taken 4 times.
✗ Branch 2 → 163 not taken.
|
4 | const ASTNode *node = spiceFunc->entry->declNode; |
| 199 |
1/2✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 4 times.
|
4 | assert(spiceFunc->isFunction()); |
| 200 | |||
| 201 | // Only generate if used | ||
| 202 |
1/2✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 4 times.
|
4 | if (!spiceFunc->used) |
| 203 | ✗ | return nullptr; | |
| 204 | |||
| 205 | // Retrieve return type | ||
| 206 |
1/2✓ Branch 9 → 10 taken 4 times.
✗ Branch 9 → 163 not taken.
|
4 | llvm::Type *returnType = spiceFunc->returnType.toLLVMType(sourceFile); |
| 207 | |||
| 208 | // Get 'this' entry | ||
| 209 | 4 | std::vector<llvm::Type *> paramTypes; | |
| 210 |
1/2✓ Branch 10 → 11 taken 4 times.
✗ Branch 10 → 161 not taken.
|
4 | SymbolTableEntry *thisEntry = nullptr; |
| 211 |
1/2✗ Branch 13 → 14 not taken.
✓ Branch 13 → 27 taken 4 times.
|
4 | if (spiceFunc->isMethod()) { |
| 212 | ✗ | thisEntry = spiceFunc->bodyScope->lookupStrict(THIS_VARIABLE_NAME); | |
| 213 | ✗ | assert(thisEntry != nullptr); | |
| 214 | ✗ | paramTypes.push_back(builder.getPtrTy()); | |
| 215 | } | ||
| 216 | |||
| 217 | // Get parameter types | ||
| 218 |
1/2✗ Branch 36 → 29 not taken.
✓ Branch 36 → 37 taken 4 times.
|
4 | for (const auto &[qualType, isOptional] : spiceFunc->paramList) { |
| 219 | ✗ | assert(!isOptional); | |
| 220 | ✗ | paramTypes.push_back(qualType.toLLVMType(sourceFile)); | |
| 221 | } | ||
| 222 | |||
| 223 | // Get function linkage | ||
| 224 |
2/4✓ Branch 37 → 38 taken 4 times.
✗ Branch 37 → 161 not taken.
✓ Branch 38 → 39 taken 4 times.
✗ Branch 38 → 161 not taken.
|
4 | const bool isPublic = spiceFunc->entry->getQualType().isPublic(); |
| 225 |
1/2✓ Branch 39 → 40 taken 4 times.
✗ Branch 39 → 41 not taken.
|
4 | const llvm::GlobalValue::LinkageTypes linkage = isPublic ? llvm::Function::ExternalLinkage : llvm::Function::PrivateLinkage; |
| 226 | |||
| 227 | // Create function | ||
| 228 |
1/2✓ Branch 42 → 43 taken 4 times.
✗ Branch 42 → 161 not taken.
|
4 | const std::string mangledName = spiceFunc->getMangledName(); |
| 229 |
1/2✓ Branch 44 → 45 taken 4 times.
✗ Branch 44 → 134 not taken.
|
4 | llvm::FunctionType *fctType = llvm::FunctionType::get(returnType, paramTypes, false); |
| 230 |
2/4✓ Branch 45 → 46 taken 4 times.
✗ Branch 45 → 135 not taken.
✓ Branch 46 → 47 taken 4 times.
✗ Branch 46 → 135 not taken.
|
4 | llvm::Function *fct = llvm::Function::Create(fctType, llvm::Function::ExternalLinkage, mangledName, module); |
| 231 |
1/2✓ Branch 47 → 48 taken 4 times.
✗ Branch 47 → 159 not taken.
|
4 | fct->setLinkage(linkage); |
| 232 |
1/2✓ Branch 48 → 49 taken 4 times.
✗ Branch 48 → 159 not taken.
|
4 | fct->addFnAttr(llvm::Attribute::MustProgress); |
| 233 |
1/2✓ Branch 49 → 50 taken 4 times.
✗ Branch 49 → 159 not taken.
|
4 | fct->addFnAttr(llvm::Attribute::NoUnwind); |
| 234 |
1/2✓ Branch 50 → 51 taken 4 times.
✗ Branch 50 → 53 not taken.
|
4 | if (cliOptions.optLevel == OptLevel::O0) { |
| 235 |
1/2✓ Branch 51 → 52 taken 4 times.
✗ Branch 51 → 159 not taken.
|
4 | fct->addFnAttr(llvm::Attribute::OptimizeNone); |
| 236 |
1/2✓ Branch 52 → 55 taken 4 times.
✗ Branch 52 → 159 not taken.
|
4 | fct->addFnAttr(llvm::Attribute::NoInline); |
| 237 | ✗ | } else if (cliOptions.optLevel >= OptLevel::Os) { | |
| 238 | ✗ | fct->addFnAttr(llvm::Attribute::OptimizeForSize); | |
| 239 | } | ||
| 240 |
2/4✓ Branch 55 → 56 taken 4 times.
✗ Branch 55 → 159 not taken.
✓ Branch 56 → 57 taken 4 times.
✗ Branch 56 → 159 not taken.
|
4 | fct->addFnAttr(llvm::Attribute::getWithUWTableKind(context, llvm::UWTableKind::Default)); |
| 241 | |||
| 242 | // Set attributes to 'this' param | ||
| 243 |
1/2✗ Branch 60 → 61 not taken.
✓ Branch 60 → 78 taken 4 times.
|
4 | if (spiceFunc->isMethod()) { |
| 244 | ✗ | fct->addParamAttr(0, llvm::Attribute::NoUndef); | |
| 245 | ✗ | fct->addParamAttr(0, llvm::Attribute::NonNull); | |
| 246 | ✗ | assert(thisEntry != nullptr); | |
| 247 | ✗ | llvm::Type *structType = thisEntry->getQualType().getContained().toLLVMType(sourceFile); | |
| 248 | ✗ | assert(structType != nullptr); | |
| 249 | ✗ | fct->addDereferenceableParamAttr(0, module->getDataLayout().getTypeStoreSize(structType)); | |
| 250 | ✗ | fct->addParamAttr(0, llvm::Attribute::getWithAlignment(context, module->getDataLayout().getABITypeAlign(structType))); | |
| 251 | } | ||
| 252 | |||
| 253 | // Add debug info | ||
| 254 |
1/2✓ Branch 78 → 79 taken 4 times.
✗ Branch 78 → 159 not taken.
|
4 | diGenerator.generateFunctionDebugInfo(fct, spiceFunc); |
| 255 |
1/2✗ Branch 79 → 80 not taken.
✓ Branch 79 → 81 taken 4 times.
|
4 | if (node != nullptr) |
| 256 | ✗ | diGenerator.setSourceLocation(node); | |
| 257 | |||
| 258 | // Change to body scope | ||
| 259 |
2/4✓ Branch 81 → 82 taken 4 times.
✗ Branch 81 → 140 not taken.
✓ Branch 82 → 83 taken 4 times.
✗ Branch 82 → 138 not taken.
|
4 | changeToScope(spiceFunc->getScopeName(), ScopeType::FUNC_PROC_BODY); |
| 260 | |||
| 261 | // Create entry block | ||
| 262 |
2/4✓ Branch 86 → 87 taken 4 times.
✗ Branch 86 → 143 not taken.
✓ Branch 87 → 88 taken 4 times.
✗ Branch 87 → 141 not taken.
|
4 | llvm::BasicBlock *bEntry = createBlock(); |
| 263 |
1/2✓ Branch 90 → 91 taken 4 times.
✗ Branch 90 → 159 not taken.
|
4 | switchToBlock(bEntry, fct); |
| 264 | |||
| 265 | // Reset alloca insert markers to this block | ||
| 266 | 4 | allocaInsertBlock = bEntry; | |
| 267 |
1/2✓ Branch 91 → 92 taken 4 times.
✗ Branch 91 → 159 not taken.
|
4 | allocaInsertInst = nullptr; |
| 268 | |||
| 269 | // Store first argument to 'this' symbol | ||
| 270 |
1/2✗ Branch 94 → 95 not taken.
✓ Branch 94 → 114 taken 4 times.
|
4 | if (spiceFunc->isMethod()) { |
| 271 | ✗ | assert(thisEntry != nullptr); | |
| 272 | // Allocate space for the parameter | ||
| 273 | ✗ | llvm::Value *thisAddress = insertAlloca(paramTypes.front(), THIS_VARIABLE_NAME); | |
| 274 | // Update the symbol table entry | ||
| 275 | ✗ | thisEntry->updateAddress(thisAddress); | |
| 276 | // Store the value at the new address | ||
| 277 | ✗ | insertStore(fct->arg_begin(), thisAddress); | |
| 278 | // Generate debug info | ||
| 279 | ✗ | diGenerator.generateLocalVarDebugInfo(THIS_VARIABLE_NAME, thisAddress, 1); | |
| 280 | } | ||
| 281 | |||
| 282 | // Generate body | ||
| 283 |
1/2✓ Branch 114 → 115 taken 4 times.
✗ Branch 114 → 159 not taken.
|
4 | generateBody(); |
| 284 | |||
| 285 | // Conclude debug info for function | ||
| 286 |
1/2✓ Branch 115 → 116 taken 4 times.
✗ Branch 115 → 159 not taken.
|
4 | diGenerator.concludeFunctionDebugInfo(); |
| 287 | |||
| 288 | // Verify function | ||
| 289 | // Use the code location of the declaration node if available. Otherwise, (e.g. in case of test main) use an artificial code loc | ||
| 290 |
2/4✗ Branch 116 → 117 not taken.
✓ Branch 116 → 118 taken 4 times.
✓ Branch 118 → 119 taken 4 times.
✗ Branch 118 → 159 not taken.
|
4 | const CodeLoc codeLoc = node != nullptr ? node->codeLoc : CodeLoc(1, 1, sourceFile); |
| 291 |
1/2✓ Branch 119 → 120 taken 4 times.
✗ Branch 119 → 159 not taken.
|
4 | verifyFunction(fct, codeLoc); |
| 292 | |||
| 293 | // Change to parent scope | ||
| 294 |
1/2✓ Branch 120 → 121 taken 4 times.
✗ Branch 120 → 159 not taken.
|
4 | changeToParentScope(ScopeType::FUNC_PROC_BODY); |
| 295 | |||
| 296 | 4 | return fct; | |
| 297 | 4 | } | |
| 298 | |||
| 299 | 256 | llvm::Function *IRGenerator::generateImplicitProcedure(const std::function<void()> &generateBody, const Function *spiceProc) { | |
| 300 | // Only focus on method procedures | ||
| 301 | 256 | const ASTNode *node = spiceProc->entry->declNode; | |
| 302 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 256 times.
|
256 | assert(node != nullptr); |
| 303 |
1/2✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 256 times.
|
256 | assert(spiceProc->isProcedure()); |
| 304 | |||
| 305 | // Only generate if used | ||
| 306 |
2/2✓ Branch 9 → 10 taken 82 times.
✓ Branch 9 → 11 taken 174 times.
|
256 | if (!spiceProc->used) |
| 307 | 82 | return nullptr; | |
| 308 | |||
| 309 | // Get 'this' entry | ||
| 310 | 174 | std::vector<llvm::Type *> paramTypes; | |
| 311 |
1/2✓ Branch 11 → 12 taken 174 times.
✗ Branch 11 → 160 not taken.
|
174 | SymbolTableEntry *thisEntry = nullptr; |
| 312 |
1/2✓ Branch 14 → 15 taken 174 times.
✗ Branch 14 → 28 not taken.
|
174 | if (spiceProc->isMethod()) { |
| 313 |
1/2✓ Branch 17 → 18 taken 174 times.
✗ Branch 17 → 126 not taken.
|
522 | thisEntry = spiceProc->bodyScope->lookupStrict(THIS_VARIABLE_NAME); |
| 314 |
1/2✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 174 times.
|
174 | assert(thisEntry != nullptr); |
| 315 |
2/4✓ Branch 25 → 26 taken 174 times.
✗ Branch 25 → 130 not taken.
✓ Branch 26 → 27 taken 174 times.
✗ Branch 26 → 130 not taken.
|
174 | paramTypes.push_back(builder.getPtrTy()); |
| 316 | } | ||
| 317 | |||
| 318 | // Get parameter types | ||
| 319 |
2/2✓ Branch 37 → 30 taken 22 times.
✓ Branch 37 → 38 taken 174 times.
|
196 | for (const auto &[qualType, isOptional] : spiceProc->paramList) { |
| 320 |
1/2✗ Branch 31 → 32 not taken.
✓ Branch 31 → 33 taken 22 times.
|
22 | assert(!isOptional); |
| 321 |
2/4✓ Branch 33 → 34 taken 22 times.
✗ Branch 33 → 131 not taken.
✓ Branch 34 → 35 taken 22 times.
✗ Branch 34 → 131 not taken.
|
22 | paramTypes.push_back(qualType.toLLVMType(sourceFile)); |
| 322 | } | ||
| 323 | |||
| 324 | // Get function linkage | ||
| 325 |
2/4✓ Branch 38 → 39 taken 174 times.
✗ Branch 38 → 160 not taken.
✓ Branch 39 → 40 taken 174 times.
✗ Branch 39 → 160 not taken.
|
174 | const bool isPublic = spiceProc->entry->getQualType().isPublic(); |
| 326 |
1/2✓ Branch 40 → 41 taken 174 times.
✗ Branch 40 → 42 not taken.
|
174 | const llvm::GlobalValue::LinkageTypes linkage = isPublic ? llvm::Function::ExternalLinkage : llvm::Function::PrivateLinkage; |
| 327 | |||
| 328 | // Create function | ||
| 329 |
1/2✓ Branch 43 → 44 taken 174 times.
✗ Branch 43 → 160 not taken.
|
174 | const std::string mangledName = spiceProc->getMangledName(); |
| 330 |
2/4✓ Branch 45 → 46 taken 174 times.
✗ Branch 45 → 133 not taken.
✓ Branch 46 → 47 taken 174 times.
✗ Branch 46 → 133 not taken.
|
174 | llvm::FunctionType *fctType = llvm::FunctionType::get(builder.getVoidTy(), paramTypes, false); |
| 331 |
2/4✓ Branch 47 → 48 taken 174 times.
✗ Branch 47 → 134 not taken.
✓ Branch 48 → 49 taken 174 times.
✗ Branch 48 → 134 not taken.
|
174 | llvm::Function *fct = llvm::Function::Create(fctType, llvm::Function::ExternalLinkage, mangledName, module); |
| 332 |
1/2✓ Branch 49 → 50 taken 174 times.
✗ Branch 49 → 158 not taken.
|
174 | fct->setLinkage(linkage); |
| 333 |
1/2✓ Branch 50 → 51 taken 174 times.
✗ Branch 50 → 158 not taken.
|
174 | fct->addFnAttr(llvm::Attribute::MustProgress); |
| 334 |
1/2✓ Branch 51 → 52 taken 174 times.
✗ Branch 51 → 158 not taken.
|
174 | fct->addFnAttr(llvm::Attribute::NoUnwind); |
| 335 |
1/2✓ Branch 52 → 53 taken 174 times.
✗ Branch 52 → 55 not taken.
|
174 | if (cliOptions.optLevel == OptLevel::O0) { |
| 336 |
1/2✓ Branch 53 → 54 taken 174 times.
✗ Branch 53 → 158 not taken.
|
174 | fct->addFnAttr(llvm::Attribute::OptimizeNone); |
| 337 |
1/2✓ Branch 54 → 57 taken 174 times.
✗ Branch 54 → 158 not taken.
|
174 | fct->addFnAttr(llvm::Attribute::NoInline); |
| 338 | ✗ | } else if (cliOptions.optLevel >= OptLevel::Os) { | |
| 339 | ✗ | fct->addFnAttr(llvm::Attribute::OptimizeForSize); | |
| 340 | } | ||
| 341 |
2/4✓ Branch 57 → 58 taken 174 times.
✗ Branch 57 → 158 not taken.
✓ Branch 58 → 59 taken 174 times.
✗ Branch 58 → 158 not taken.
|
174 | fct->addFnAttr(llvm::Attribute::getWithUWTableKind(context, llvm::UWTableKind::Default)); |
| 342 | |||
| 343 | // Set attributes to 'this' param | ||
| 344 |
1/2✓ Branch 62 → 63 taken 174 times.
✗ Branch 62 → 80 not taken.
|
174 | if (spiceProc->isMethod()) { |
| 345 |
1/2✓ Branch 63 → 64 taken 174 times.
✗ Branch 63 → 158 not taken.
|
174 | fct->addParamAttr(0, llvm::Attribute::NoUndef); |
| 346 |
1/2✓ Branch 64 → 65 taken 174 times.
✗ Branch 64 → 158 not taken.
|
174 | fct->addParamAttr(0, llvm::Attribute::NonNull); |
| 347 |
1/2✗ Branch 65 → 66 not taken.
✓ Branch 65 → 67 taken 174 times.
|
174 | assert(thisEntry != nullptr); |
| 348 |
3/6✓ Branch 67 → 68 taken 174 times.
✗ Branch 67 → 135 not taken.
✓ Branch 68 → 69 taken 174 times.
✗ Branch 68 → 135 not taken.
✓ Branch 69 → 70 taken 174 times.
✗ Branch 69 → 135 not taken.
|
174 | llvm::Type *structType = thisEntry->getQualType().getContained().toLLVMType(sourceFile); |
| 349 |
1/2✗ Branch 70 → 71 not taken.
✓ Branch 70 → 72 taken 174 times.
|
174 | assert(structType != nullptr); |
| 350 |
3/6✓ Branch 73 → 74 taken 174 times.
✗ Branch 73 → 136 not taken.
✓ Branch 74 → 75 taken 174 times.
✗ Branch 74 → 136 not taken.
✓ Branch 75 → 76 taken 174 times.
✗ Branch 75 → 136 not taken.
|
174 | fct->addDereferenceableParamAttr(0, module->getDataLayout().getTypeStoreSize(structType)); |
| 351 |
3/6✓ Branch 77 → 78 taken 174 times.
✗ Branch 77 → 158 not taken.
✓ Branch 78 → 79 taken 174 times.
✗ Branch 78 → 158 not taken.
✓ Branch 79 → 80 taken 174 times.
✗ Branch 79 → 158 not taken.
|
174 | fct->addParamAttr(0, llvm::Attribute::getWithAlignment(context, module->getDataLayout().getABITypeAlign(structType))); |
| 352 | } | ||
| 353 | |||
| 354 | // Add debug info | ||
| 355 |
1/2✓ Branch 80 → 81 taken 174 times.
✗ Branch 80 → 158 not taken.
|
174 | diGenerator.generateFunctionDebugInfo(fct, spiceProc); |
| 356 |
1/2✓ Branch 81 → 82 taken 174 times.
✗ Branch 81 → 158 not taken.
|
174 | diGenerator.setSourceLocation(node); |
| 357 | |||
| 358 | // Change to body scope | ||
| 359 |
2/4✓ Branch 82 → 83 taken 174 times.
✗ Branch 82 → 139 not taken.
✓ Branch 83 → 84 taken 174 times.
✗ Branch 83 → 137 not taken.
|
174 | changeToScope(spiceProc->getScopeName(), ScopeType::FUNC_PROC_BODY); |
| 360 | |||
| 361 | // Create entry block | ||
| 362 |
2/4✓ Branch 87 → 88 taken 174 times.
✗ Branch 87 → 142 not taken.
✓ Branch 88 → 89 taken 174 times.
✗ Branch 88 → 140 not taken.
|
174 | llvm::BasicBlock *bEntry = createBlock(); |
| 363 |
1/2✓ Branch 91 → 92 taken 174 times.
✗ Branch 91 → 158 not taken.
|
174 | switchToBlock(bEntry, fct); |
| 364 | |||
| 365 | // Reset alloca insert markers to this block | ||
| 366 | 174 | allocaInsertBlock = bEntry; | |
| 367 |
1/2✓ Branch 92 → 93 taken 174 times.
✗ Branch 92 → 158 not taken.
|
174 | allocaInsertInst = nullptr; |
| 368 | |||
| 369 | // Store first argument to 'this' symbol | ||
| 370 |
1/2✓ Branch 95 → 96 taken 174 times.
✗ Branch 95 → 115 not taken.
|
174 | if (spiceProc->isMethod()) { |
| 371 |
1/2✗ Branch 96 → 97 not taken.
✓ Branch 96 → 98 taken 174 times.
|
174 | assert(thisEntry != nullptr); |
| 372 | // Allocate space for the parameter | ||
| 373 |
2/4✓ Branch 100 → 101 taken 174 times.
✗ Branch 100 → 148 not taken.
✓ Branch 102 → 103 taken 174 times.
✗ Branch 102 → 146 not taken.
|
174 | llvm::Value *thisAddress = insertAlloca(paramTypes.front(), THIS_VARIABLE_NAME); |
| 374 | // Update the symbol table entry | ||
| 375 |
1/2✓ Branch 105 → 106 taken 174 times.
✗ Branch 105 → 158 not taken.
|
174 | thisEntry->updateAddress(thisAddress); |
| 376 | // Store the value at the new address | ||
| 377 |
2/4✓ Branch 106 → 107 taken 174 times.
✗ Branch 106 → 158 not taken.
✓ Branch 107 → 108 taken 174 times.
✗ Branch 107 → 158 not taken.
|
174 | insertStore(fct->arg_begin(), thisAddress); |
| 378 | // Generate debug info | ||
| 379 |
2/4✓ Branch 110 → 111 taken 174 times.
✗ Branch 110 → 154 not taken.
✓ Branch 111 → 112 taken 174 times.
✗ Branch 111 → 152 not taken.
|
522 | diGenerator.generateLocalVarDebugInfo(THIS_VARIABLE_NAME, thisAddress, 1); |
| 380 | } | ||
| 381 | |||
| 382 | // Generate body | ||
| 383 |
1/2✓ Branch 115 → 116 taken 174 times.
✗ Branch 115 → 158 not taken.
|
174 | generateBody(); |
| 384 | |||
| 385 | // Create return instruction | ||
| 386 |
1/2✓ Branch 116 → 117 taken 174 times.
✗ Branch 116 → 158 not taken.
|
174 | builder.CreateRetVoid(); |
| 387 | |||
| 388 | // Conclude debug info for function | ||
| 389 |
1/2✓ Branch 117 → 118 taken 174 times.
✗ Branch 117 → 158 not taken.
|
174 | diGenerator.concludeFunctionDebugInfo(); |
| 390 | |||
| 391 | // Verify function | ||
| 392 |
1/2✓ Branch 118 → 119 taken 174 times.
✗ Branch 118 → 158 not taken.
|
174 | verifyFunction(fct, node->codeLoc); |
| 393 | |||
| 394 | // Change to parent scope | ||
| 395 |
1/2✓ Branch 119 → 120 taken 174 times.
✗ Branch 119 → 158 not taken.
|
174 | changeToParentScope(ScopeType::FUNC_PROC_BODY); |
| 396 | |||
| 397 | 174 | return fct; | |
| 398 | 174 | } | |
| 399 | |||
| 400 | 1352 | void IRGenerator::generateCtorBodyPreamble(Scope *bodyScope) { | |
| 401 | // Retrieve struct scope | ||
| 402 | 1352 | Scope *structScope = bodyScope->parent; | |
| 403 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1352 times.
|
1352 | assert(structScope != nullptr); |
| 404 | |||
| 405 | // Get struct address | ||
| 406 |
1/2✓ Branch 6 → 7 taken 1352 times.
✗ Branch 6 → 130 not taken.
|
4056 | const SymbolTableEntry *thisEntry = bodyScope->lookupStrict(THIS_VARIABLE_NAME); |
| 407 |
1/2✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 1352 times.
|
1352 | assert(thisEntry != nullptr); |
| 408 |
1/2✓ Branch 14 → 15 taken 1352 times.
✗ Branch 14 → 184 not taken.
|
1352 | llvm::Value *thisPtrPtr = thisEntry->getAddress(); |
| 409 |
1/2✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 1352 times.
|
1352 | assert(thisPtrPtr != nullptr); |
| 410 | 1352 | llvm::Value *thisPtr = nullptr; | |
| 411 |
2/4✓ Branch 17 → 18 taken 1352 times.
✗ Branch 17 → 184 not taken.
✓ Branch 18 → 19 taken 1352 times.
✗ Branch 18 → 184 not taken.
|
1352 | const QualType structSymbolType = thisEntry->getQualType().getBase(); |
| 412 |
1/2✓ Branch 19 → 20 taken 1352 times.
✗ Branch 19 → 184 not taken.
|
1352 | llvm::Type *structType = structSymbolType.toLLVMType(sourceFile); |
| 413 | |||
| 414 | // Store VTable to first struct field if required | ||
| 415 |
1/2✓ Branch 20 → 21 taken 1352 times.
✗ Branch 20 → 184 not taken.
|
1352 | const Struct *spiceStruct = structSymbolType.getStruct(nullptr); |
| 416 |
1/2✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 1352 times.
|
1352 | assert(spiceStruct != nullptr); |
| 417 |
2/2✓ Branch 23 → 24 taken 258 times.
✓ Branch 23 → 45 taken 1094 times.
|
1352 | if (spiceStruct->vTableData.vtable != nullptr) { |
| 418 |
1/2✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 258 times.
|
258 | assert(spiceStruct->vTableData.vtableType != nullptr); |
| 419 | // Store VTable to field address at index 0 | ||
| 420 |
3/6✓ Branch 28 → 29 taken 258 times.
✗ Branch 28 → 136 not taken.
✓ Branch 29 → 30 taken 258 times.
✗ Branch 29 → 134 not taken.
✓ Branch 30 → 31 taken 258 times.
✗ Branch 30 → 134 not taken.
|
258 | thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr); |
| 421 |
3/6✓ Branch 33 → 34 taken 258 times.
✗ Branch 33 → 147 not taken.
✓ Branch 34 → 35 taken 258 times.
✗ Branch 34 → 147 not taken.
✓ Branch 35 → 36 taken 258 times.
✗ Branch 35 → 147 not taken.
|
258 | llvm::Value *indices[3] = {builder.getInt64(0), builder.getInt32(0), builder.getInt32(2)}; |
| 422 |
1/2✓ Branch 40 → 41 taken 258 times.
✗ Branch 40 → 140 not taken.
|
258 | llvm::Value *gepResult = insertInBoundsGEP(spiceStruct->vTableData.vtableType, spiceStruct->vTableData.vtable, indices); |
| 423 |
1/2✓ Branch 43 → 44 taken 258 times.
✗ Branch 43 → 147 not taken.
|
258 | insertStore(gepResult, thisPtr); |
| 424 | } | ||
| 425 | |||
| 426 |
1/2✓ Branch 45 → 46 taken 1352 times.
✗ Branch 45 → 184 not taken.
|
1352 | const size_t fieldCount = structScope->getFieldCount(); |
| 427 |
2/2✓ Branch 126 → 47 taken 3375 times.
✓ Branch 126 → 127 taken 1352 times.
|
4727 | for (size_t fieldIdx = 0; fieldIdx < fieldCount; fieldIdx++) { |
| 428 |
1/2✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 3375 times.
|
3375 | const SymbolTableEntry *fieldSymbol = structScope->lookupField(fieldIdx); |
| 429 |
3/6✓ Branch 52 → 53 taken 3375 times.
✗ Branch 52 → 56 not taken.
✓ Branch 53 → 54 taken 3375 times.
✗ Branch 53 → 184 not taken.
✓ Branch 54 → 55 taken 3375 times.
✗ Branch 54 → 56 not taken.
|
3375 | assert(fieldSymbol != nullptr && fieldSymbol->isField()); |
| 430 |
2/2✓ Branch 57 → 58 taken 208 times.
✓ Branch 57 → 59 taken 3167 times.
|
3375 | if (fieldSymbol->isImplicitField) |
| 431 | 208 | continue; | |
| 432 | |||
| 433 | // Call ctor for struct fields | ||
| 434 |
1/2✓ Branch 59 → 60 taken 3167 times.
✗ Branch 59 → 184 not taken.
|
3167 | const QualType &fieldType = fieldSymbol->getQualType(); |
| 435 |
1/2✓ Branch 60 → 61 taken 3167 times.
✗ Branch 60 → 62 not taken.
|
3167 | const auto fieldNode = spice_pointer_cast<FieldNode *>(fieldSymbol->declNode); |
| 436 |
3/4✓ Branch 67 → 68 taken 3167 times.
✗ Branch 67 → 184 not taken.
✓ Branch 68 → 69 taken 390 times.
✓ Branch 68 → 99 taken 2777 times.
|
3167 | if (fieldType.is(TY_STRUCT)) { |
| 437 | // Lookup ctor function and call if available | ||
| 438 |
1/2✓ Branch 69 → 70 taken 390 times.
✗ Branch 69 → 184 not taken.
|
390 | Scope *matchScope = fieldType.getBodyScope(); |
| 439 |
4/6✓ Branch 73 → 74 taken 390 times.
✗ Branch 73 → 150 not taken.
✓ Branch 74 → 75 taken 390 times.
✗ Branch 74 → 148 not taken.
✓ Branch 78 → 79 taken 330 times.
✓ Branch 78 → 98 taken 60 times.
|
1170 | if (const Function *ctorFunction = FunctionManager::lookup(matchScope, CTOR_FUNCTION_NAME, fieldType, {}, false)) { |
| 440 |
2/2✓ Branch 79 → 80 taken 92 times.
✓ Branch 79 → 88 taken 238 times.
|
330 | if (!thisPtr) |
| 441 |
3/6✓ Branch 82 → 83 taken 92 times.
✗ Branch 82 → 159 not taken.
✓ Branch 83 → 84 taken 92 times.
✗ Branch 83 → 157 not taken.
✓ Branch 84 → 85 taken 92 times.
✗ Branch 84 → 157 not taken.
|
184 | thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr); |
| 442 |
1/2✓ Branch 91 → 92 taken 330 times.
✗ Branch 91 → 163 not taken.
|
330 | llvm::Value *fieldAddress = insertStructGEP(structType, thisPtr, fieldIdx); |
| 443 |
1/2✓ Branch 95 → 96 taken 330 times.
✗ Branch 95 → 169 not taken.
|
330 | generateCtorOrDtorCall(fieldAddress, ctorFunction, {}); |
| 444 | } | ||
| 445 | 390 | continue; | |
| 446 | 390 | } | |
| 447 | |||
| 448 | // Store default field values | ||
| 449 |
3/4✓ Branch 99 → 100 taken 2478 times.
✓ Branch 99 → 101 taken 299 times.
✓ Branch 100 → 101 taken 2478 times.
✗ Branch 100 → 125 not taken.
|
2777 | if (fieldNode->defaultValue != nullptr || cliOptions.buildMode != BuildMode::RELEASE) { |
| 450 | // Retrieve field address | ||
| 451 |
2/2✓ Branch 101 → 102 taken 992 times.
✓ Branch 101 → 110 taken 1785 times.
|
2777 | if (!thisPtr) |
| 452 |
3/6✓ Branch 104 → 105 taken 992 times.
✗ Branch 104 → 174 not taken.
✓ Branch 105 → 106 taken 992 times.
✗ Branch 105 → 172 not taken.
✓ Branch 106 → 107 taken 992 times.
✗ Branch 106 → 172 not taken.
|
1984 | thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr); |
| 453 |
1/2✓ Branch 113 → 114 taken 2777 times.
✗ Branch 113 → 178 not taken.
|
2777 | llvm::Value *fieldAddress = insertStructGEP(structType, thisPtr, fieldIdx); |
| 454 | // Retrieve default value | ||
| 455 | llvm::Value *value; | ||
| 456 |
2/2✓ Branch 116 → 117 taken 299 times.
✓ Branch 116 → 119 taken 2478 times.
|
2777 | if (fieldNode->defaultValue != nullptr) { |
| 457 | // To resolve the default value, we need to temporarily change to the manifestation of the current struct instantiation | ||
| 458 | 299 | const size_t oldManIdx = manIdx; // Save manifestation index | |
| 459 | 299 | manIdx = spiceStruct->manifestationIndex; | |
| 460 |
1/2✓ Branch 117 → 118 taken 299 times.
✗ Branch 117 → 184 not taken.
|
299 | value = resolveValue(fieldNode->defaultValue); |
| 461 | 299 | manIdx = oldManIdx; // Restore manifestation index | |
| 462 | } else { | ||
| 463 |
1/4✗ Branch 119 → 120 not taken.
✓ Branch 119 → 122 taken 2478 times.
✗ Branch 120 → 121 not taken.
✗ Branch 120 → 122 not taken.
|
2478 | assert(cliOptions.buildMode == BuildMode::DEBUG || cliOptions.buildMode == BuildMode::TEST); |
| 464 |
1/2✓ Branch 122 → 123 taken 2478 times.
✗ Branch 122 → 184 not taken.
|
2478 | value = getDefaultValueForSymbolType(fieldType); |
| 465 | } | ||
| 466 | // Store default value | ||
| 467 |
1/2✓ Branch 124 → 125 taken 2777 times.
✗ Branch 124 → 184 not taken.
|
2777 | insertStore(value, fieldAddress); |
| 468 | } | ||
| 469 | } | ||
| 470 | 1352 | } | |
| 471 | |||
| 472 | 45 | void IRGenerator::generateDefaultCtor(const Function *ctorFunction) { | |
| 473 |
3/6✓ Branch 2 → 3 taken 45 times.
✗ Branch 2 → 6 not taken.
✓ Branch 3 → 4 taken 45 times.
✗ Branch 3 → 13 not taken.
✓ Branch 4 → 5 taken 45 times.
✗ Branch 4 → 6 not taken.
|
45 | assert(ctorFunction->implicitDefault && ctorFunction->name == CTOR_FUNCTION_NAME); |
| 474 | 86 | const std::function<void()> generateBody = [&] { generateCtorBodyPreamble(ctorFunction->bodyScope); }; | |
| 475 |
1/2✓ Branch 8 → 9 taken 45 times.
✗ Branch 8 → 11 not taken.
|
45 | generateImplicitProcedure(generateBody, ctorFunction); |
| 476 | 45 | } | |
| 477 | |||
| 478 | 22 | void IRGenerator::generateCopyCtorBodyPreamble(const Function *copyCtorFunction) { | |
| 479 | // Retrieve struct scope | ||
| 480 | 22 | Scope *structScope = copyCtorFunction->bodyScope->parent; | |
| 481 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 22 times.
|
22 | assert(structScope != nullptr); |
| 482 | |||
| 483 | // Get struct address | ||
| 484 |
1/2✓ Branch 6 → 7 taken 22 times.
✗ Branch 6 → 141 not taken.
|
66 | const SymbolTableEntry *thisEntry = copyCtorFunction->bodyScope->lookupStrict(THIS_VARIABLE_NAME); |
| 485 |
1/2✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 22 times.
|
22 | assert(thisEntry != nullptr); |
| 486 | 22 | llvm::Value *thisPtrPtr = thisEntry->getAddress(); | |
| 487 |
1/2✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 22 times.
|
22 | assert(thisPtrPtr != nullptr); |
| 488 | 22 | llvm::Value *thisPtr = nullptr; | |
| 489 |
3/6✓ Branch 17 → 18 taken 22 times.
✗ Branch 17 → 145 not taken.
✓ Branch 18 → 19 taken 22 times.
✗ Branch 18 → 145 not taken.
✓ Branch 19 → 20 taken 22 times.
✗ Branch 19 → 145 not taken.
|
22 | llvm::Type *structType = thisEntry->getQualType().getBase().toLLVMType(sourceFile); |
| 490 | |||
| 491 | // Retrieve the value of the original struct, which is the only function parameter | ||
| 492 | 22 | llvm::Value *originalThisPtr = builder.GetInsertBlock()->getParent()->getArg(1); | |
| 493 | |||
| 494 | 22 | const size_t fieldCount = structScope->getFieldCount(); | |
| 495 |
2/2✓ Branch 137 → 25 taken 77 times.
✓ Branch 137 → 138 taken 22 times.
|
99 | for (size_t fieldIdx = 0; fieldIdx < fieldCount; fieldIdx++) { |
| 496 |
1/2✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 77 times.
|
77 | const SymbolTableEntry *fieldSymbol = structScope->lookupField(fieldIdx); |
| 497 |
2/4✓ Branch 30 → 31 taken 77 times.
✗ Branch 30 → 34 not taken.
✓ Branch 32 → 33 taken 77 times.
✗ Branch 32 → 34 not taken.
|
77 | assert(fieldSymbol != nullptr && fieldSymbol->isField()); |
| 498 | |||
| 499 | // Retrieve the address of the original field (copy source) | ||
| 500 |
1/2✓ Branch 38 → 39 taken 77 times.
✗ Branch 38 → 146 not taken.
|
77 | llvm::Value *originalFieldAddress = insertStructGEP(structType, originalThisPtr, fieldIdx); |
| 501 | |||
| 502 | 77 | const QualType &fieldType = fieldSymbol->getQualType(); | |
| 503 | |||
| 504 | // Call copy ctor for struct fields | ||
| 505 |
5/6✓ Branch 43 → 44 taken 38 times.
✓ Branch 43 → 47 taken 39 times.
✓ Branch 45 → 46 taken 38 times.
✗ Branch 45 → 47 not taken.
✓ Branch 48 → 49 taken 38 times.
✓ Branch 48 → 73 taken 39 times.
|
77 | if (fieldType.is(TY_STRUCT) && !fieldType.isTriviallyCopyable(nullptr)) { |
| 506 | // Lookup copy ctor function and call if available | ||
| 507 |
1/2✓ Branch 49 → 50 taken 38 times.
✗ Branch 49 → 173 not taken.
|
38 | Scope *matchScope = fieldType.getBodyScope(); |
| 508 |
2/4✓ Branch 50 → 51 taken 38 times.
✗ Branch 50 → 156 not taken.
✓ Branch 54 → 55 taken 38 times.
✗ Branch 54 → 152 not taken.
|
114 | const ArgList args = {{fieldType.toConstRef(nullptr), false /* we have the field as storage */}}; |
| 509 |
2/4✓ Branch 58 → 59 taken 38 times.
✗ Branch 58 → 160 not taken.
✓ Branch 59 → 60 taken 38 times.
✗ Branch 59 → 158 not taken.
|
38 | const Function *copyCtor = FunctionManager::lookup(matchScope, CTOR_FUNCTION_NAME, fieldType, args, false); |
| 510 |
1/2✗ Branch 62 → 63 not taken.
✓ Branch 62 → 64 taken 38 times.
|
38 | assert(copyCtor != nullptr); |
| 511 |
2/4✓ Branch 66 → 67 taken 38 times.
✗ Branch 66 → 166 not taken.
✓ Branch 67 → 68 taken 38 times.
✗ Branch 67 → 164 not taken.
|
76 | generateCtorOrDtorCall(fieldSymbol, copyCtor, {originalFieldAddress}); |
| 512 | 38 | continue; | |
| 513 | 38 | } | |
| 514 | |||
| 515 | // Retrieve the address of the new field (copy dest) | ||
| 516 |
2/2✓ Branch 73 → 74 taken 12 times.
✓ Branch 73 → 82 taken 27 times.
|
39 | if (!thisPtr) |
| 517 |
3/6✓ Branch 76 → 77 taken 12 times.
✗ Branch 76 → 176 not taken.
✓ Branch 77 → 78 taken 12 times.
✗ Branch 77 → 174 not taken.
✓ Branch 78 → 79 taken 12 times.
✗ Branch 78 → 174 not taken.
|
24 | thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr); |
| 518 |
1/2✓ Branch 85 → 86 taken 39 times.
✗ Branch 85 → 180 not taken.
|
39 | llvm::Value *fieldAddress = insertStructGEP(structType, thisPtr, fieldIdx); |
| 519 | |||
| 520 | // For owning heap fields, copy the underlying heap storage | ||
| 521 |
2/2✓ Branch 89 → 90 taken 7 times.
✓ Branch 89 → 134 taken 32 times.
|
39 | if (fieldType.isHeap()) { |
| 522 |
1/2✗ Branch 91 → 92 not taken.
✓ Branch 91 → 93 taken 7 times.
|
7 | assert(fieldType.isPtr()); |
| 523 |
2/4✓ Branch 93 → 94 taken 7 times.
✗ Branch 93 → 186 not taken.
✓ Branch 94 → 95 taken 7 times.
✗ Branch 94 → 186 not taken.
|
7 | llvm::Type *pointeeType = fieldType.getContained().toLLVMType(sourceFile); |
| 524 | |||
| 525 | // Retrieve original heap address | ||
| 526 |
3/6✓ Branch 97 → 98 taken 7 times.
✗ Branch 97 → 189 not taken.
✓ Branch 98 → 99 taken 7 times.
✗ Branch 98 → 187 not taken.
✓ Branch 99 → 100 taken 7 times.
✗ Branch 99 → 187 not taken.
|
14 | llvm::Value *originalHeapAddress = insertLoad(builder.getPtrTy(), originalFieldAddress); |
| 527 | |||
| 528 | // Insert check for nullptr | ||
| 529 |
2/4✓ Branch 104 → 105 taken 7 times.
✗ Branch 104 → 195 not taken.
✓ Branch 105 → 106 taken 7 times.
✗ Branch 105 → 193 not taken.
|
14 | llvm::BasicBlock *bThen = createBlock("nullptrcheck.then"); |
| 530 |
2/4✓ Branch 110 → 111 taken 7 times.
✗ Branch 110 → 201 not taken.
✓ Branch 111 → 112 taken 7 times.
✗ Branch 111 → 199 not taken.
|
7 | llvm::BasicBlock *bExit = createBlock("nullptrcheck.exit"); |
| 531 |
4/8✓ Branch 114 → 115 taken 7 times.
✗ Branch 114 → 205 not taken.
✓ Branch 115 → 116 taken 7 times.
✗ Branch 115 → 205 not taken.
✓ Branch 116 → 117 taken 7 times.
✗ Branch 116 → 205 not taken.
✓ Branch 117 → 118 taken 7 times.
✗ Branch 117 → 205 not taken.
|
7 | llvm::Value *condValue = builder.CreateICmpNE(originalHeapAddress, llvm::Constant::getNullValue(builder.getPtrTy())); |
| 532 | 7 | insertCondJump(condValue, bThen, bExit); | |
| 533 | |||
| 534 | // Fill then block | ||
| 535 | 7 | switchToBlock(bThen); | |
| 536 | |||
| 537 | // Allocate new space on the heap | ||
| 538 | 7 | llvm::Function *unsafeAllocFct = stdFunctionManager.getAllocUnsafeLongFct(); | |
| 539 |
2/4✓ Branch 122 → 123 taken 7 times.
✗ Branch 122 → 206 not taken.
✓ Branch 123 → 124 taken 7 times.
✗ Branch 123 → 206 not taken.
|
7 | const size_t typeSizeInBytes = module->getDataLayout().getTypeSizeInBits(pointeeType) / 8; |
| 540 | 7 | llvm::ConstantInt *typeSize = builder.getInt64(typeSizeInBytes); | |
| 541 |
3/6✓ Branch 125 → 126 taken 7 times.
✗ Branch 125 → 210 not taken.
✓ Branch 127 → 128 taken 7 times.
✗ Branch 127 → 207 not taken.
✓ Branch 128 → 129 taken 7 times.
✗ Branch 128 → 207 not taken.
|
7 | llvm::Value *newHeapAddress = builder.CreateCall(unsafeAllocFct, {typeSize}); |
| 542 | 7 | insertStore(newHeapAddress, fieldAddress); | |
| 543 | |||
| 544 | // Copy data from the old heap storage to the new one | ||
| 545 | 7 | generateShallowCopy(originalHeapAddress, pointeeType, newHeapAddress, false); | |
| 546 | 7 | insertJump(bExit); | |
| 547 | |||
| 548 | // Switch to exit block | ||
| 549 | 7 | switchToBlock(bExit); | |
| 550 | |||
| 551 | 7 | continue; | |
| 552 | 7 | } | |
| 553 | |||
| 554 | // Shallow copy | ||
| 555 | 32 | llvm::Type *type = fieldType.toLLVMType(sourceFile); | |
| 556 | 32 | generateShallowCopy(originalFieldAddress, type, fieldAddress, false); | |
| 557 | } | ||
| 558 | 22 | } | |
| 559 | |||
| 560 | 98 | void IRGenerator::generateDefaultCopyCtor(const Function *copyCtorFunction) { | |
| 561 |
3/6✓ Branch 2 → 3 taken 98 times.
✗ Branch 2 → 6 not taken.
✓ Branch 3 → 4 taken 98 times.
✗ Branch 3 → 13 not taken.
✓ Branch 4 → 5 taken 98 times.
✗ Branch 4 → 6 not taken.
|
98 | assert(copyCtorFunction->implicitDefault && copyCtorFunction->name == CTOR_FUNCTION_NAME); |
| 562 | 120 | const std::function<void()> generateBody = [&] { generateCopyCtorBodyPreamble(copyCtorFunction); }; | |
| 563 |
1/2✓ Branch 8 → 9 taken 98 times.
✗ Branch 8 → 11 not taken.
|
98 | generateImplicitProcedure(generateBody, copyCtorFunction); |
| 564 | 98 | } | |
| 565 | |||
| 566 | 111 | void IRGenerator::generateDtorBodyPreamble(const Function *dtorFunction) const { | |
| 567 | // Retrieve struct scope | ||
| 568 | 111 | Scope *structScope = dtorFunction->bodyScope->parent; | |
| 569 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 111 times.
|
111 | assert(structScope != nullptr); |
| 570 | |||
| 571 | // Get struct address | ||
| 572 |
1/2✓ Branch 6 → 7 taken 111 times.
✗ Branch 6 → 73 not taken.
|
333 | const SymbolTableEntry *thisEntry = dtorFunction->bodyScope->lookupStrict(THIS_VARIABLE_NAME); |
| 573 |
1/2✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 111 times.
|
111 | assert(thisEntry != nullptr); |
| 574 | 111 | llvm::Value *thisPtrPtr = thisEntry->getAddress(); | |
| 575 |
1/2✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 111 times.
|
111 | assert(thisPtrPtr != nullptr); |
| 576 | 111 | llvm::Value *thisPtr = nullptr; | |
| 577 |
3/6✓ Branch 17 → 18 taken 111 times.
✗ Branch 17 → 77 not taken.
✓ Branch 18 → 19 taken 111 times.
✗ Branch 18 → 77 not taken.
✓ Branch 19 → 20 taken 111 times.
✗ Branch 19 → 77 not taken.
|
111 | llvm::Type *structType = thisEntry->getQualType().getBase().toLLVMType(sourceFile); |
| 578 | |||
| 579 | 111 | const size_t fieldCount = structScope->getFieldCount(); | |
| 580 |
2/2✓ Branch 69 → 22 taken 382 times.
✓ Branch 69 → 70 taken 111 times.
|
493 | for (size_t i = 0; i < fieldCount; i++) { |
| 581 | 382 | const size_t fieldIdx = fieldCount - 1 - i; // Destruct fields in reverse order | |
| 582 |
1/2✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 382 times.
|
382 | const SymbolTableEntry *fieldSymbol = structScope->lookupField(fieldIdx); |
| 583 |
2/4✓ Branch 27 → 28 taken 382 times.
✗ Branch 27 → 31 not taken.
✓ Branch 29 → 30 taken 382 times.
✗ Branch 29 → 31 not taken.
|
382 | assert(fieldSymbol != nullptr && fieldSymbol->isField()); |
| 584 | |||
| 585 | // Call dtor for struct fields | ||
| 586 | 382 | const QualType &fieldType = fieldSymbol->getQualType(); | |
| 587 |
2/2✓ Branch 34 → 35 taken 82 times.
✓ Branch 34 → 50 taken 300 times.
|
382 | if (fieldType.is(TY_STRUCT)) { |
| 588 | // Lookup dtor function and generate call if found | ||
| 589 |
5/8✓ Branch 38 → 39 taken 82 times.
✗ Branch 38 → 80 not taken.
✓ Branch 39 → 40 taken 82 times.
✗ Branch 39 → 78 not taken.
✓ Branch 40 → 41 taken 82 times.
✗ Branch 40 → 78 not taken.
✓ Branch 44 → 45 taken 73 times.
✓ Branch 44 → 49 taken 9 times.
|
246 | if (const Function *dtorFct = FunctionManager::lookup(fieldType.getBodyScope(), DTOR_FUNCTION_NAME, fieldType, {}, false)) |
| 590 |
1/2✓ Branch 46 → 47 taken 73 times.
✗ Branch 46 → 87 not taken.
|
73 | generateCtorOrDtorCall(fieldSymbol, dtorFct, {}); |
| 591 | 82 | continue; | |
| 592 | 82 | } | |
| 593 | |||
| 594 | // Deallocate fields, that are stored on the heap | ||
| 595 |
2/2✓ Branch 51 → 52 taken 74 times.
✓ Branch 51 → 68 taken 226 times.
|
300 | if (fieldType.isHeap()) { |
| 596 | // Retrieve field address | ||
| 597 |
2/2✓ Branch 52 → 53 taken 68 times.
✓ Branch 52 → 61 taken 6 times.
|
74 | if (!thisPtr) |
| 598 |
3/6✓ Branch 55 → 56 taken 68 times.
✗ Branch 55 → 92 not taken.
✓ Branch 56 → 57 taken 68 times.
✗ Branch 56 → 90 not taken.
✓ Branch 57 → 58 taken 68 times.
✗ Branch 57 → 90 not taken.
|
136 | thisPtr = insertLoad(builder.getPtrTy(), thisPtrPtr); |
| 599 |
1/2✓ Branch 64 → 65 taken 74 times.
✗ Branch 64 → 96 not taken.
|
74 | llvm::Value *fieldAddress = insertStructGEP(structType, thisPtr, fieldIdx); |
| 600 | // Call dealloc function | ||
| 601 | 74 | generateDeallocCall(fieldAddress); | |
| 602 | } | ||
| 603 | } | ||
| 604 | 111 | } | |
| 605 | |||
| 606 | 113 | void IRGenerator::generateDefaultDtor(const Function *dtorFunction) { | |
| 607 |
3/6✓ Branch 2 → 3 taken 113 times.
✗ Branch 2 → 6 not taken.
✓ Branch 3 → 4 taken 113 times.
✗ Branch 3 → 13 not taken.
✓ Branch 4 → 5 taken 113 times.
✗ Branch 4 → 6 not taken.
|
113 | assert(dtorFunction->implicitDefault && dtorFunction->name == DTOR_FUNCTION_NAME); |
| 608 | 224 | const std::function<void()> generateBody = [&] { generateDtorBodyPreamble(dtorFunction); }; | |
| 609 |
1/2✓ Branch 8 → 9 taken 113 times.
✗ Branch 8 → 11 not taken.
|
113 | generateImplicitProcedure(generateBody, dtorFunction); |
| 610 | 113 | } | |
| 611 | |||
| 612 | 4 | void IRGenerator::generateTestMain() { | |
| 613 | // Collect all test functions | ||
| 614 | 4 | std::vector<const std::vector<const Function *> *> tests; | |
| 615 |
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) |
| 616 |
1/2✓ Branch 9 → 10 taken 5 times.
✗ Branch 9 → 13 not taken.
|
5 | if (!sourceFile->testFunctions.empty()) |
| 617 |
1/2✓ Branch 11 → 12 taken 5 times.
✗ Branch 11 → 137 not taken.
|
5 | tests.push_back(&sourceFile->testFunctions); |
| 618 | |||
| 619 | // Prepare printf function | ||
| 620 |
1/2✓ Branch 16 → 17 taken 4 times.
✗ Branch 16 → 263 not taken.
|
4 | llvm::Function *printfFct = stdFunctionManager.getPrintfFct(); |
| 621 | |||
| 622 | // Prepare success and error messages | ||
| 623 |
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); |
| 624 |
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); |
| 625 |
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); |
| 626 |
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); |
| 627 |
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); |
| 628 |
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); |
| 629 |
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); |
| 630 |
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); |
| 631 | |||
| 632 | // Prepare entry for test main | ||
| 633 |
1/2✓ Branch 105 → 106 taken 4 times.
✗ Branch 105 → 263 not taken.
|
4 | QualType functionType(TY_FUNCTION); |
| 634 |
1/2✓ Branch 106 → 107 taken 4 times.
✗ Branch 106 → 263 not taken.
|
4 | functionType.setQualifiers(TypeQualifiers::of(TY_FUNCTION)); |
| 635 |
1/2✓ Branch 108 → 109 taken 4 times.
✗ Branch 108 → 263 not taken.
|
4 | functionType.makePublic(); |
| 636 |
2/4✓ Branch 111 → 112 taken 4 times.
✗ Branch 111 → 237 not taken.
✓ Branch 112 → 113 taken 4 times.
✗ Branch 112 → 235 not taken.
|
8 | SymbolTableEntry entry(MAIN_FUNCTION_NAME, functionType, rootScope, nullptr, 0, false); |
| 637 | |||
| 638 | // Prepare test main function | ||
| 639 |
3/6✓ 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.
|
12 | Function testMain(MAIN_FUNCTION_NAME, &entry, QualType(TY_DYN), QualType(TY_INT), {}, {}, nullptr); |
| 640 | 4 | testMain.used = true; // Mark as used to prevent removal | |
| 641 | 4 | testMain.implicitDefault = true; | |
| 642 | 4 | testMain.mangleFunctionName = false; | |
| 643 | |||
| 644 | // Prepare scope | ||
| 645 |
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); |
| 646 | |||
| 647 | // Generate | ||
| 648 | ✗ | const std::function<void()> generateBody = [&] { | |
| 649 | // Prepare result variable | ||
| 650 | 4 | std::vector<llvm::Value *> testCaseResults; | |
| 651 |
1/2✓ Branch 3 → 4 taken 4 times.
✗ Branch 3 → 273 not taken.
|
4 | testCaseResults.reserve(tests.size()); |
| 652 | |||
| 653 | // Print start message | ||
| 654 | 5 | const auto accFct = [&](size_t sum, const std::vector<const Function *> *innerVector) { return sum + innerVector->size(); }; | |
| 655 | 4 | const size_t totalTestCount = std::accumulate(tests.begin(), tests.end(), 0, accFct); | |
| 656 |
5/10✓ Branch 7 → 8 taken 4 times.
✗ Branch 7 → 193 not taken.
✓ Branch 8 → 9 taken 4 times.
✗ Branch 8 → 191 not taken.
✓ Branch 10 → 11 taken 4 times.
✗ Branch 10 → 191 not taken.
✓ Branch 12 → 13 taken 4 times.
✗ Branch 12 → 190 not taken.
✓ Branch 13 → 14 taken 4 times.
✗ Branch 13 → 190 not taken.
|
4 | builder.CreateCall(printfFct, {allStartMsg, builder.getInt32(totalTestCount), builder.getInt32(tests.size())}); |
| 657 | |||
| 658 | // Generate a call to each test function | ||
| 659 |
2/2✓ Branch 165 → 16 taken 5 times.
✓ Branch 165 → 166 taken 4 times.
|
9 | for (const std::vector<const Function *> *testSuite : tests) { |
| 660 | // Print test suite prologue | ||
| 661 |
1/2✓ Branch 18 → 19 taken 5 times.
✗ Branch 18 → 263 not taken.
|
5 | const std::string fileName = testSuite->front()->bodyScope->sourceFile->fileName; |
| 662 |
3/6✓ Branch 20 → 21 taken 5 times.
✗ Branch 20 → 261 not taken.
✓ Branch 23 → 24 taken 5 times.
✗ Branch 23 → 196 not taken.
✓ Branch 24 → 25 taken 5 times.
✗ Branch 24 → 194 not taken.
|
10 | llvm::Constant *fileNameValue = createGlobalStringConst("fileName", fileName, testSuite->front()->getDeclCodeLoc()); |
| 663 |
4/8✓ Branch 27 → 28 taken 5 times.
✗ Branch 27 → 203 not taken.
✓ Branch 29 → 30 taken 5 times.
✗ Branch 29 → 201 not taken.
✓ Branch 31 → 32 taken 5 times.
✗ Branch 31 → 200 not taken.
✓ Branch 32 → 33 taken 5 times.
✗ Branch 32 → 200 not taken.
|
5 | builder.CreateCall(printfFct, {fileStartMsg, builder.getInt32(testSuite->size()), fileNameValue}); |
| 664 | |||
| 665 |
3/4✓ Branch 36 → 37 taken 10 times.
✗ Branch 36 → 255 not taken.
✓ Branch 155 → 35 taken 10 times.
✓ Branch 155 → 156 taken 5 times.
|
15 | for (const Function *testFunction : *testSuite) { |
| 666 |
1/2✗ Branch 48 → 49 not taken.
✓ Branch 48 → 50 taken 10 times.
|
10 | assert(testFunction->isNormalFunction()); |
| 667 |
1/2✗ Branch 51 → 52 not taken.
✓ Branch 51 → 53 taken 10 times.
|
10 | assert(testFunction->paramList.empty()); |
| 668 | |||
| 669 | // Retrieve attribute list for the test function | ||
| 670 |
2/4✓ Branch 53 → 54 taken 10 times.
✗ Branch 53 → 255 not taken.
✗ Branch 54 → 55 not taken.
✓ Branch 54 → 56 taken 10 times.
|
10 | assert(testFunction->declNode->isFctOrProcDef()); |
| 671 |
1/2✓ Branch 56 → 57 taken 10 times.
✗ Branch 56 → 58 not taken.
|
10 | const auto fctDefNode = spice_pointer_cast<FctDefBaseNode *>(testFunction->declNode); |
| 672 |
1/2✗ Branch 63 → 64 not taken.
✓ Branch 63 → 65 taken 10 times.
|
10 | assert(fctDefNode->attrs != nullptr); |
| 673 | 10 | const AttrLstNode *attrs = fctDefNode->attrs->attrLst; | |
| 674 |
3/6✓ Branch 67 → 68 taken 10 times.
✗ Branch 67 → 206 not taken.
✓ Branch 68 → 69 taken 10 times.
✗ Branch 68 → 204 not taken.
✗ Branch 69 → 70 not taken.
✓ Branch 69 → 71 taken 10 times.
|
20 | assert(attrs->getAttrValueByName(ATTR_TEST)->boolValue); // The test attribute must be present |
| 675 |
2/4✓ Branch 75 → 76 taken 10 times.
✗ Branch 75 → 212 not taken.
✓ Branch 76 → 77 taken 10 times.
✗ Branch 76 → 210 not taken.
|
20 | const CompileTimeValue *testSkipAttr = attrs->getAttrValueByName(ATTR_TEST_SKIP); |
| 676 |
4/4✓ Branch 79 → 80 taken 3 times.
✓ Branch 79 → 82 taken 7 times.
✓ Branch 80 → 81 taken 1 time.
✓ Branch 80 → 82 taken 2 times.
|
10 | const bool skipTest = testSkipAttr && testSkipAttr->boolValue; |
| 677 |
2/4✓ Branch 85 → 86 taken 10 times.
✗ Branch 85 → 218 not taken.
✓ Branch 86 → 87 taken 10 times.
✗ Branch 86 → 216 not taken.
|
20 | const CompileTimeValue *testNameAttr = attrs->getAttrValueByName(ATTR_TEST_NAME); |
| 678 | |||
| 679 | // Prepare test name | ||
| 680 |
1/2✓ Branch 89 → 90 taken 10 times.
✗ Branch 89 → 255 not taken.
|
10 | std::stringstream testName; |
| 681 |
1/2✓ Branch 90 → 91 taken 10 times.
✗ Branch 90 → 253 not taken.
|
10 | testName << testFunction->name; |
| 682 |
2/2✓ Branch 91 → 92 taken 1 time.
✓ Branch 91 → 96 taken 9 times.
|
10 | if (testNameAttr) |
| 683 |
4/8✓ Branch 92 → 93 taken 1 time.
✗ Branch 92 → 253 not taken.
✓ Branch 93 → 94 taken 1 time.
✗ Branch 93 → 253 not taken.
✓ Branch 94 → 95 taken 1 time.
✗ Branch 94 → 253 not taken.
✓ Branch 95 → 96 taken 1 time.
✗ Branch 95 → 253 not taken.
|
1 | testName << " (" << resourceManager.compileTimeStringValues.at(testNameAttr->stringValueOffset) << ")"; |
| 684 | |||
| 685 | // Print test case run message | ||
| 686 |
4/8✓ Branch 96 → 97 taken 10 times.
✗ Branch 96 → 253 not taken.
✓ Branch 97 → 98 taken 10 times.
✗ Branch 97 → 230 not taken.
✓ Branch 100 → 101 taken 10 times.
✗ Branch 100 → 224 not taken.
✓ Branch 101 → 102 taken 10 times.
✗ Branch 101 → 222 not taken.
|
30 | llvm::Constant *testNameValue = createGlobalStringConst("testName", testName.str(), testFunction->getDeclCodeLoc()); |
| 687 |
3/6✓ Branch 105 → 106 taken 10 times.
✗ Branch 105 → 234 not taken.
✓ Branch 107 → 108 taken 10 times.
✗ Branch 107 → 231 not taken.
✓ Branch 108 → 109 taken 10 times.
✗ Branch 108 → 231 not taken.
|
10 | builder.CreateCall(printfFct, {runMsg, testNameValue}); |
| 688 | |||
| 689 |
2/2✓ Branch 109 → 110 taken 1 time.
✓ Branch 109 → 115 taken 9 times.
|
10 | if (skipTest) { |
| 690 | // Print test case skip message | ||
| 691 |
3/6✓ Branch 110 → 111 taken 1 time.
✗ Branch 110 → 238 not taken.
✓ Branch 112 → 113 taken 1 time.
✗ Branch 112 → 235 not taken.
✓ Branch 113 → 114 taken 1 time.
✗ Branch 113 → 235 not taken.
|
1 | builder.CreateCall(printfFct, {skippedMsg, testNameValue}); |
| 692 | 1 | continue; | |
| 693 | } | ||
| 694 | |||
| 695 | // Test function is not defined in the current module -> declare it | ||
| 696 |
1/2✓ Branch 115 → 116 taken 9 times.
✗ Branch 115 → 253 not taken.
|
9 | const std::string mangledName = testFunction->getMangledName(); |
| 697 |
3/4✓ Branch 117 → 118 taken 9 times.
✗ Branch 117 → 239 not taken.
✓ Branch 118 → 119 taken 2 times.
✓ Branch 118 → 131 taken 7 times.
|
9 | if (!module->getFunction(mangledName)) { |
| 698 |
2/4✓ Branch 119 → 120 taken 2 times.
✗ Branch 119 → 251 not taken.
✗ Branch 120 → 121 not taken.
✓ Branch 120 → 122 taken 2 times.
|
2 | assert(testFunction->returnType.is(TY_BOOL)); |
| 699 |
1/2✗ Branch 123 → 124 not taken.
✓ Branch 123 → 125 taken 2 times.
|
2 | assert(testFunction->paramList.empty()); |
| 700 |
2/4✓ Branch 126 → 127 taken 2 times.
✗ Branch 126 → 240 not taken.
✓ Branch 127 → 128 taken 2 times.
✗ Branch 127 → 240 not taken.
|
2 | llvm::FunctionType *fctType = llvm::FunctionType::get(builder.getInt1Ty(), {}, false); |
| 701 |
1/2✓ Branch 129 → 130 taken 2 times.
✗ Branch 129 → 241 not taken.
|
2 | module->getOrInsertFunction(mangledName, fctType); |
| 702 | } | ||
| 703 | |||
| 704 | // Call test function | ||
| 705 |
1/2✓ Branch 132 → 133 taken 9 times.
✗ Branch 132 → 242 not taken.
|
9 | llvm::Function *callee = module->getFunction(mangledName); |
| 706 |
1/2✗ Branch 133 → 134 not taken.
✓ Branch 133 → 135 taken 9 times.
|
9 | assert(callee != nullptr); |
| 707 |
3/6✓ Branch 135 → 136 taken 9 times.
✗ Branch 135 → 245 not taken.
✓ Branch 137 → 138 taken 9 times.
✗ Branch 137 → 243 not taken.
✓ Branch 138 → 139 taken 9 times.
✗ Branch 138 → 243 not taken.
|
9 | llvm::Value *testCaseResult = builder.CreateCall(callee); |
| 708 |
1/2✓ Branch 139 → 140 taken 9 times.
✗ Branch 139 → 251 not taken.
|
9 | testCaseResults.push_back(testCaseResult); |
| 709 | |||
| 710 | // Print test case result message | ||
| 711 |
2/4✓ Branch 140 → 141 taken 9 times.
✗ Branch 140 → 246 not taken.
✓ Branch 141 → 142 taken 9 times.
✗ Branch 141 → 246 not taken.
|
9 | llvm::Value *message = builder.CreateSelect(testCaseResult, successMsg, errorMsg); |
| 712 |
3/6✓ Branch 142 → 143 taken 9 times.
✗ Branch 142 → 250 not taken.
✓ Branch 144 → 145 taken 9 times.
✗ Branch 144 → 247 not taken.
✓ Branch 145 → 146 taken 9 times.
✗ Branch 145 → 247 not taken.
|
9 | builder.CreateCall(printfFct, {message, testNameValue}); |
| 713 |
2/2✓ Branch 149 → 150 taken 9 times.
✓ Branch 149 → 152 taken 1 time.
|
10 | } |
| 714 | |||
| 715 | // Print test suite epilogue | ||
| 716 |
4/8✓ Branch 156 → 157 taken 5 times.
✗ Branch 156 → 260 not taken.
✓ Branch 158 → 159 taken 5 times.
✗ Branch 158 → 258 not taken.
✓ Branch 160 → 161 taken 5 times.
✗ Branch 160 → 257 not taken.
✓ Branch 161 → 162 taken 5 times.
✗ Branch 161 → 257 not taken.
|
5 | builder.CreateCall(printfFct, {fileEndMsg, builder.getInt32(testSuite->size()), fileNameValue}); |
| 717 | 5 | } | |
| 718 | |||
| 719 | // Print end message | ||
| 720 |
5/10✓ Branch 166 → 167 taken 4 times.
✗ Branch 166 → 268 not taken.
✓ Branch 167 → 168 taken 4 times.
✗ Branch 167 → 266 not taken.
✓ Branch 169 → 170 taken 4 times.
✗ Branch 169 → 266 not taken.
✓ Branch 171 → 172 taken 4 times.
✗ Branch 171 → 265 not taken.
✓ Branch 172 → 173 taken 4 times.
✗ Branch 172 → 265 not taken.
|
4 | builder.CreateCall(printfFct, {allEndMsg, builder.getInt32(totalTestCount), builder.getInt32(tests.size())}); |
| 721 | |||
| 722 | // Compute overall result | ||
| 723 |
1/2✓ Branch 173 → 174 taken 4 times.
✗ Branch 173 → 273 not taken.
|
4 | llvm::Value *overallResult = builder.getTrue(); |
| 724 |
2/2✓ Branch 181 → 176 taken 9 times.
✓ Branch 181 → 182 taken 4 times.
|
13 | for (llvm::Value *testCaseResult : testCaseResults) |
| 725 |
2/4✓ Branch 177 → 178 taken 9 times.
✗ Branch 177 → 269 not taken.
✓ Branch 178 → 179 taken 9 times.
✗ Branch 178 → 269 not taken.
|
9 | overallResult = builder.CreateAnd(overallResult, testCaseResult); |
| 726 | |||
| 727 | // Return code must be 0 for success and 1 for failure, so we need to invert the result and zero extend to 32 bit | ||
| 728 |
2/4✓ Branch 182 → 183 taken 4 times.
✗ Branch 182 → 271 not taken.
✓ Branch 183 → 184 taken 4 times.
✗ Branch 183 → 271 not taken.
|
4 | llvm::Value *overallResultNegated = builder.CreateNot(overallResult); |
| 729 |
3/6✓ Branch 184 → 185 taken 4 times.
✗ Branch 184 → 272 not taken.
✓ Branch 185 → 186 taken 4 times.
✗ Branch 185 → 272 not taken.
✓ Branch 186 → 187 taken 4 times.
✗ Branch 186 → 272 not taken.
|
4 | llvm::Value *exitCode = builder.CreateZExt(overallResultNegated, builder.getInt32Ty()); |
| 730 |
1/2✓ Branch 187 → 188 taken 4 times.
✗ Branch 187 → 273 not taken.
|
4 | builder.CreateRet(exitCode); |
| 731 |
1/2✓ Branch 130 → 131 taken 4 times.
✗ Branch 130 → 256 not taken.
|
8 | }; |
| 732 |
1/2✓ Branch 131 → 132 taken 4 times.
✗ Branch 131 → 257 not taken.
|
4 | generateImplicitFunction(generateBody, &testMain); |
| 733 | 4 | } | |
| 734 | |||
| 735 | } // namespace spice::compiler | ||
| 736 |