src/irgenerator/GenTopLevelDefinitions.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 <symboltablebuilder/SymbolTableBuilder.h> | ||
| 10 | #include <typechecker/FunctionManager.h> | ||
| 11 | |||
| 12 | #include <llvm/IR/Module.h> | ||
| 13 | #include <llvm/Target/TargetLoweringObjectFile.h> | ||
| 14 | |||
| 15 | namespace spice::compiler { | ||
| 16 | |||
| 17 | 437 | std::any IRGenerator::visitMainFctDef(const MainFctDefNode *node) { | |
| 18 | // Ignore main function definitions if this is not the main source file | ||
| 19 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 6 taken 437 times.
|
437 | if (!sourceFile->isMainFile) |
| 20 | ✗ | return nullptr; | |
| 21 | |||
| 22 | // Do not generate main function if it is explicitly specified | ||
| 23 |
2/2✓ Branch 6 → 7 taken 1 time.
✓ Branch 6 → 10 taken 436 times.
|
437 | if (cliOptions.noEntryFct) |
| 24 |
1/2✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 144 not taken.
|
2 | return nullptr; |
| 25 | |||
| 26 | // Change scope to function scope | ||
| 27 | 436 | currentScope = node->bodyScope; | |
| 28 |
1/2✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 436 times.
|
436 | assert(currentScope != nullptr); |
| 29 | |||
| 30 | // Visit parameters | ||
| 31 | 436 | std::vector<std::pair<std::string, const SymbolTableEntry *>> paramInfoList; | |
| 32 | 436 | QualTypeList paramSymbolTypes; | |
| 33 | 436 | std::vector<llvm::Type *> paramTypes; | |
| 34 |
2/2✓ Branch 12 → 13 taken 7 times.
✓ Branch 12 → 42 taken 429 times.
|
436 | if (node->takesArgs) { |
| 35 | 7 | const size_t numOfParams = node->paramLst->params.size(); | |
| 36 |
1/2✓ Branch 14 → 15 taken 7 times.
✗ Branch 14 → 195 not taken.
|
7 | paramInfoList.reserve(numOfParams); |
| 37 |
1/2✓ Branch 15 → 16 taken 7 times.
✗ Branch 15 → 195 not taken.
|
7 | paramSymbolTypes.reserve(numOfParams); |
| 38 |
1/2✓ Branch 16 → 17 taken 7 times.
✗ Branch 16 → 195 not taken.
|
7 | paramTypes.reserve(numOfParams); |
| 39 |
2/2✓ Branch 40 → 19 taken 14 times.
✓ Branch 40 → 41 taken 7 times.
|
28 | for (DeclStmtNode *param : node->paramLst->params) { |
| 40 | // Get symbol table entry of param | ||
| 41 |
1/2✓ Branch 21 → 22 taken 14 times.
✗ Branch 21 → 146 not taken.
|
14 | const SymbolTableEntry *paramSymbol = node->bodyScope->lookupStrict(param->varName); |
| 42 |
1/2✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 14 times.
|
14 | assert(paramSymbol != nullptr); |
| 43 | // Retrieve type of param | ||
| 44 |
1/2✓ Branch 26 → 27 taken 14 times.
✗ Branch 26 → 146 not taken.
|
14 | const QualType paramSymbolType = paramSymbol->getQualType(); |
| 45 | // Add it to the lists | ||
| 46 |
1/2✓ Branch 27 → 28 taken 14 times.
✗ Branch 27 → 146 not taken.
|
14 | paramInfoList.emplace_back(param->varName, paramSymbol); |
| 47 |
1/2✓ Branch 28 → 29 taken 14 times.
✗ Branch 28 → 146 not taken.
|
14 | paramSymbolTypes.push_back(paramSymbolType); |
| 48 |
2/4✓ Branch 29 → 30 taken 14 times.
✗ Branch 29 → 145 not taken.
✓ Branch 30 → 31 taken 14 times.
✗ Branch 30 → 145 not taken.
|
14 | paramTypes.push_back(paramSymbolType.getParamLLVMType(sourceFile)); |
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | // Build the function | ||
| 53 |
1/2✓ Branch 42 → 43 taken 436 times.
✗ Branch 42 → 195 not taken.
|
436 | llvm::Type *returnType = builder.getInt32Ty(); |
| 54 |
1/2✓ Branch 44 → 45 taken 436 times.
✗ Branch 44 → 148 not taken.
|
436 | llvm::FunctionType *fctType = llvm::FunctionType::get(returnType, paramTypes, false); |
| 55 |
2/4✓ Branch 45 → 46 taken 436 times.
✗ Branch 45 → 149 not taken.
✓ Branch 46 → 47 taken 436 times.
✗ Branch 46 → 149 not taken.
|
436 | llvm::Function *fct = llvm::Function::Create(fctType, llvm::Function::ExternalLinkage, MAIN_FUNCTION_NAME, module); |
| 56 | 436 | fct->setDSOLocal(true); | |
| 57 | |||
| 58 | // Add function attributes. The main function is the entry point, so nobody can call or inline it. | ||
| 59 |
1/2✓ Branch 48 → 49 taken 436 times.
✗ Branch 48 → 195 not taken.
|
436 | fct->addFnAttr(llvm::Attribute::MustProgress); |
| 60 |
1/2✓ Branch 49 → 50 taken 436 times.
✗ Branch 49 → 195 not taken.
|
436 | fct->addFnAttr(llvm::Attribute::NoInline); |
| 61 |
1/2✓ Branch 50 → 51 taken 436 times.
✗ Branch 50 → 195 not taken.
|
436 | fct->addFnAttr(llvm::Attribute::NoRecurse); |
| 62 |
1/2✓ Branch 51 → 52 taken 436 times.
✗ Branch 51 → 195 not taken.
|
436 | addCommonFctAttrs(fct); |
| 63 |
1/2✓ Branch 52 → 53 taken 436 times.
✗ Branch 52 → 195 not taken.
|
436 | enableFunctionInstrumentation(fct); |
| 64 | |||
| 65 | // Add return value attributes | ||
| 66 |
1/2✓ Branch 53 → 54 taken 436 times.
✗ Branch 53 → 195 not taken.
|
436 | fct->addRetAttr(llvm::Attribute::NoUndef); |
| 67 | |||
| 68 | // Add debug info | ||
| 69 |
2/2✓ Branch 54 → 55 taken 7 times.
✓ Branch 54 → 60 taken 429 times.
|
436 | if (cliOptions.instrumentation.generateDebugInfo) { |
| 70 | 7 | const auto nonConstNode = const_cast<MainFctDefNode *>(node); | |
| 71 |
1/2✓ Branch 55 → 56 taken 7 times.
✗ Branch 55 → 152 not taken.
|
7 | const Function spiceFunc = FunctionManager::createMainFunction(node->entry, paramSymbolTypes, nonConstNode); |
| 72 |
1/2✓ Branch 56 → 57 taken 7 times.
✗ Branch 56 → 150 not taken.
|
7 | diGenerator.generateFunctionDebugInfo(fct, &spiceFunc); |
| 73 |
1/2✓ Branch 57 → 58 taken 7 times.
✗ Branch 57 → 150 not taken.
|
7 | diGenerator.setSourceLocation(node); |
| 74 | 7 | } | |
| 75 | |||
| 76 | // Create entry block | ||
| 77 |
1/2✓ Branch 63 → 64 taken 436 times.
✗ Branch 63 → 153 not taken.
|
436 | llvm::BasicBlock *bEntry = createBlock(); |
| 78 |
1/2✓ Branch 66 → 67 taken 436 times.
✗ Branch 66 → 195 not taken.
|
436 | switchToBlock(bEntry, fct); |
| 79 | |||
| 80 | // Reset alloca insert markers to this block | ||
| 81 | 436 | allocaInsertBlock = bEntry; | |
| 82 | 436 | allocaInsertInst = nullptr; | |
| 83 | |||
| 84 | // Allocate result variable | ||
| 85 |
3/6✓ Branch 69 → 70 taken 436 times.
✗ Branch 69 → 162 not taken.
✓ Branch 70 → 71 taken 436 times.
✗ Branch 70 → 159 not taken.
✓ Branch 71 → 72 taken 436 times.
✗ Branch 71 → 159 not taken.
|
436 | llvm::Value *resultAddress = insertAlloca(QualType(TY_INT), RETURN_VARIABLE_NAME); |
| 86 | // Update the symbol table entry | ||
| 87 |
1/2✓ Branch 76 → 77 taken 436 times.
✗ Branch 76 → 168 not taken.
|
1308 | const SymbolTableEntry *resultEntry = currentScope->lookupStrict(RETURN_VARIABLE_NAME); |
| 88 |
1/2✗ Branch 82 → 83 not taken.
✓ Branch 82 → 84 taken 436 times.
|
436 | assert(resultEntry != nullptr); |
| 89 |
1/2✓ Branch 84 → 85 taken 436 times.
✗ Branch 84 → 195 not taken.
|
436 | updateAddress(resultEntry, resultAddress); |
| 90 | // Generate debug info | ||
| 91 |
2/4✓ Branch 87 → 88 taken 436 times.
✗ Branch 87 → 174 not taken.
✓ Branch 88 → 89 taken 436 times.
✗ Branch 88 → 172 not taken.
|
872 | diGenerator.generateLocalVarDebugInfo(RETURN_VARIABLE_NAME, resultAddress); |
| 92 | // Store the default result value | ||
| 93 |
3/6✓ Branch 91 → 92 taken 436 times.
✗ Branch 91 → 178 not taken.
✓ Branch 92 → 93 taken 436 times.
✗ Branch 92 → 178 not taken.
✓ Branch 93 → 94 taken 436 times.
✗ Branch 93 → 178 not taken.
|
436 | insertStore(builder.getInt32(0), resultAddress, QualType(TY_INT)); |
| 94 | |||
| 95 | // Store function argument values | ||
| 96 |
3/4✓ Branch 94 → 95 taken 436 times.
✗ Branch 94 → 186 not taken.
✓ Branch 119 → 97 taken 14 times.
✓ Branch 119 → 120 taken 436 times.
|
450 | for (auto &arg : fct->args()) { |
| 97 | // Get information about the parameter | ||
| 98 | 14 | const size_t argNumber = arg.getArgNo(); | |
| 99 |
2/4✓ Branch 98 → 99 taken 14 times.
✗ Branch 98 → 179 not taken.
✓ Branch 99 → 100 taken 14 times.
✗ Branch 99 → 179 not taken.
|
14 | auto [paramName, paramSymbol] = paramInfoList.at(argNumber); |
| 100 |
1/2✗ Branch 102 → 103 not taken.
✓ Branch 102 → 104 taken 14 times.
|
14 | assert(paramSymbol != nullptr); |
| 101 | // Decayed array params already carry the address of the array, so they do not need a local copy | ||
| 102 |
2/4✓ Branch 104 → 105 taken 14 times.
✗ Branch 104 → 179 not taken.
✗ Branch 105 → 106 not taken.
✓ Branch 105 → 107 taken 14 times.
|
14 | if (bindDecayedArrayParam(arg, paramName, paramSymbol)) |
| 103 | ✗ | continue; | |
| 104 | // Allocate space for it | ||
| 105 |
2/4✓ Branch 107 → 108 taken 14 times.
✗ Branch 107 → 179 not taken.
✓ Branch 108 → 109 taken 14 times.
✗ Branch 108 → 179 not taken.
|
14 | llvm::Value *paramAddress = insertAlloca(paramSymbol->getQualType(), paramName); |
| 106 | // Update the symbol table entry | ||
| 107 |
1/2✓ Branch 109 → 110 taken 14 times.
✗ Branch 109 → 179 not taken.
|
14 | updateAddress(paramSymbol, paramAddress); |
| 108 | // Generate debug info | ||
| 109 |
1/2✓ Branch 110 → 111 taken 14 times.
✗ Branch 110 → 179 not taken.
|
14 | diGenerator.generateLocalVarDebugInfo(paramName, paramAddress, argNumber + 1); |
| 110 | // Store the value at the new address | ||
| 111 |
1/2✓ Branch 111 → 112 taken 14 times.
✗ Branch 111 → 179 not taken.
|
14 | insertStore(&arg, paramAddress); |
| 112 |
1/4✓ Branch 113 → 114 taken 14 times.
✗ Branch 113 → 115 not taken.
✗ Branch 179 → 180 not taken.
✗ Branch 179 → 181 not taken.
|
14 | } |
| 113 | |||
| 114 | // Visit function body | ||
| 115 |
1/2✓ Branch 120 → 121 taken 436 times.
✗ Branch 120 → 187 not taken.
|
436 | visit(node->body); |
| 116 | |||
| 117 | // Create return statement if the block is not terminated yet | ||
| 118 |
2/2✓ Branch 122 → 123 taken 398 times.
✓ Branch 122 → 132 taken 38 times.
|
436 | if (!blockAlreadyTerminated) { |
| 119 |
3/6✓ Branch 126 → 127 taken 398 times.
✗ Branch 126 → 188 not taken.
✓ Branch 127 → 128 taken 398 times.
✗ Branch 127 → 188 not taken.
✓ Branch 128 → 129 taken 398 times.
✗ Branch 128 → 188 not taken.
|
398 | llvm::Value *result = insertLoad(fct->getReturnType(), getAddress(resultEntry)); |
| 120 |
1/2✓ Branch 131 → 132 taken 398 times.
✗ Branch 131 → 195 not taken.
|
398 | builder.CreateRet(result); |
| 121 | } | ||
| 122 | |||
| 123 | // Conclude debug info for function | ||
| 124 |
1/2✓ Branch 132 → 133 taken 436 times.
✗ Branch 132 → 195 not taken.
|
436 | diGenerator.concludeFunctionDebugInfo(); |
| 125 | |||
| 126 | // Verify function | ||
| 127 |
1/2✓ Branch 133 → 134 taken 436 times.
✗ Branch 133 → 195 not taken.
|
436 | verifyFunction(fct, node->codeLoc); |
| 128 | |||
| 129 | // Change back to root scope | ||
| 130 | 436 | currentScope = rootScope; | |
| 131 |
1/2✗ Branch 134 → 135 not taken.
✓ Branch 134 → 136 taken 436 times.
|
436 | assert(currentScope != nullptr); |
| 132 | |||
| 133 |
1/2✓ Branch 136 → 137 taken 436 times.
✗ Branch 136 → 194 not taken.
|
436 | return nullptr; |
| 134 | 436 | } | |
| 135 | |||
| 136 | 22651 | std::any IRGenerator::visitFctDef(const FctDefNode *node) { | |
| 137 | // Loop through manifestations | ||
| 138 | 22651 | manIdx = 0; // Reset the symbolTypeIndex | |
| 139 |
2/2✓ Branch 213 → 4 taken 30448 times.
✓ Branch 213 → 214 taken 22651 times.
|
75750 | for (const Function *manifestation : node->manifestations) { |
| 140 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 30448 times.
|
30448 | assert(manifestation->entry != nullptr); |
| 141 | |||
| 142 | // Check if the manifestation is substantiated or not public and not used by anybody | ||
| 143 |
2/4✓ Branch 8 → 9 taken 30448 times.
✗ Branch 8 → 305 not taken.
✓ Branch 9 → 10 taken 30448 times.
✗ Branch 9 → 305 not taken.
|
30448 | bool isPublic = manifestation->entry->getQualType().isPublic(); |
| 144 |
9/10✓ Branch 10 → 11 taken 30448 times.
✗ Branch 10 → 305 not taken.
✓ Branch 11 → 12 taken 24978 times.
✓ Branch 11 → 14 taken 5470 times.
✓ Branch 12 → 13 taken 1587 times.
✓ Branch 12 → 15 taken 23391 times.
✓ Branch 13 → 14 taken 24 times.
✓ Branch 13 → 15 taken 1563 times.
✓ Branch 16 → 17 taken 5494 times.
✓ Branch 16 → 18 taken 24954 times.
|
30448 | if (!manifestation->isFullySubstantiated() || (!isPublic && !manifestation->used)) { |
| 145 | 5494 | manIdx++; // Increment symbolTypeIndex | |
| 146 | 5494 | continue; | |
| 147 | } | ||
| 148 | |||
| 149 | // Change to struct scope | ||
| 150 |
2/2✓ Branch 21 → 22 taken 14026 times.
✓ Branch 21 → 30 taken 10928 times.
|
24954 | if (manifestation->isMethod()) { |
| 151 | 14026 | const QualType &thisType = manifestation->thisType; | |
| 152 |
3/6✓ Branch 22 → 23 taken 14026 times.
✗ Branch 22 → 222 not taken.
✓ Branch 23 → 24 taken 14026 times.
✗ Branch 23 → 222 not taken.
✓ Branch 24 → 25 taken 14026 times.
✗ Branch 24 → 222 not taken.
|
14026 | const std::string scopeName = Struct::getScopeName(thisType.getSubType(), thisType.getTemplateTypes()); |
| 153 |
1/2✓ Branch 25 → 26 taken 14026 times.
✗ Branch 25 → 220 not taken.
|
14026 | currentScope = currentScope->getChildScope(scopeName); |
| 154 |
1/2✗ Branch 26 → 27 not taken.
✓ Branch 26 → 28 taken 14026 times.
|
14026 | assert(currentScope != nullptr); |
| 155 | 14026 | } | |
| 156 | |||
| 157 | // Change scope | ||
| 158 |
2/4✓ Branch 30 → 31 taken 24954 times.
✗ Branch 30 → 225 not taken.
✓ Branch 31 → 32 taken 24954 times.
✗ Branch 31 → 223 not taken.
|
24954 | currentScope = currentScope->getChildScope(manifestation->getScopeName()); |
| 159 |
1/2✗ Branch 33 → 34 not taken.
✓ Branch 33 → 35 taken 24954 times.
|
24954 | assert(currentScope != nullptr); |
| 160 | |||
| 161 | // Get 'this' entry | ||
| 162 | 24954 | std::vector<std::pair<std::string, const SymbolTableEntry *>> paramInfoList; | |
| 163 |
1/2✓ Branch 35 → 36 taken 24954 times.
✗ Branch 35 → 301 not taken.
|
24954 | std::vector<llvm::Type *> paramTypes; |
| 164 |
2/2✓ Branch 38 → 39 taken 14026 times.
✓ Branch 38 → 53 taken 10928 times.
|
24954 | if (manifestation->isMethod()) { |
| 165 |
1/2✓ Branch 41 → 42 taken 14026 times.
✗ Branch 41 → 228 not taken.
|
42078 | const SymbolTableEntry *thisEntry = currentScope->lookupStrict(THIS_VARIABLE_NAME); |
| 166 |
1/2✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 14026 times.
|
14026 | assert(thisEntry != nullptr); |
| 167 |
1/2✓ Branch 49 → 50 taken 14026 times.
✗ Branch 49 → 233 not taken.
|
14026 | paramInfoList.emplace_back(THIS_VARIABLE_NAME, thisEntry); |
| 168 |
2/4✓ Branch 50 → 51 taken 14026 times.
✗ Branch 50 → 232 not taken.
✓ Branch 51 → 52 taken 14026 times.
✗ Branch 51 → 232 not taken.
|
14026 | paramTypes.push_back(builder.getPtrTy()); |
| 169 | } | ||
| 170 | |||
| 171 | // Visit parameters | ||
| 172 | 24954 | size_t argIdx = 0; | |
| 173 |
2/2✓ Branch 53 → 54 taken 18113 times.
✓ Branch 53 → 72 taken 6841 times.
|
24954 | if (node->hasParams) { |
| 174 | 18113 | const size_t numOfParams = manifestation->paramList.size(); | |
| 175 |
1/2✓ Branch 55 → 56 taken 18113 times.
✗ Branch 55 → 301 not taken.
|
18113 | paramInfoList.reserve(numOfParams); |
| 176 |
1/2✓ Branch 56 → 57 taken 18113 times.
✗ Branch 56 → 301 not taken.
|
18113 | paramTypes.reserve(numOfParams); |
| 177 |
2/2✓ Branch 71 → 58 taken 26269 times.
✓ Branch 71 → 72 taken 18113 times.
|
44382 | for (; argIdx < numOfParams; argIdx++) { |
| 178 |
1/2✓ Branch 58 → 59 taken 26269 times.
✗ Branch 58 → 237 not taken.
|
26269 | const DeclStmtNode *param = node->paramLst->params.at(argIdx); |
| 179 | // Get symbol table entry of param | ||
| 180 |
1/2✓ Branch 59 → 60 taken 26269 times.
✗ Branch 59 → 237 not taken.
|
26269 | const SymbolTableEntry *paramSymbol = currentScope->lookupStrict(param->varName); |
| 181 |
1/2✗ Branch 62 → 63 not taken.
✓ Branch 62 → 64 taken 26269 times.
|
26269 | assert(paramSymbol != nullptr); |
| 182 |
2/4✓ Branch 64 → 65 taken 26269 times.
✗ Branch 64 → 236 not taken.
✓ Branch 65 → 66 taken 26269 times.
✗ Branch 65 → 234 not taken.
|
26269 | const QualType paramSymbolType = manifestation->getParamTypes().at(argIdx); |
| 183 | // Retrieve type of param | ||
| 184 |
1/2✓ Branch 67 → 68 taken 26269 times.
✗ Branch 67 → 237 not taken.
|
26269 | llvm::Type *paramType = paramSymbolType.getParamLLVMType(sourceFile); |
| 185 | // Add it to the lists | ||
| 186 |
1/2✓ Branch 68 → 69 taken 26269 times.
✗ Branch 68 → 237 not taken.
|
26269 | paramInfoList.emplace_back(param->varName, paramSymbol); |
| 187 |
1/2✓ Branch 69 → 70 taken 26269 times.
✗ Branch 69 → 237 not taken.
|
26269 | paramTypes.push_back(paramType); |
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | // Get return type | ||
| 192 |
1/2✓ Branch 72 → 73 taken 24954 times.
✗ Branch 72 → 301 not taken.
|
24954 | llvm::Type *returnType = manifestation->returnType.toLLVMType(sourceFile); |
| 193 | |||
| 194 | // Get function linkage | ||
| 195 |
12/18✓ Branch 73 → 74 taken 908 times.
✓ Branch 73 → 80 taken 24046 times.
✓ Branch 76 → 77 taken 908 times.
✗ Branch 76 → 238 not taken.
✓ Branch 77 → 78 taken 908 times.
✗ Branch 77 → 238 not taken.
✓ Branch 78 → 79 taken 109 times.
✓ Branch 78 → 80 taken 799 times.
✓ Branch 81 → 82 taken 908 times.
✓ Branch 81 → 83 taken 24046 times.
✓ Branch 83 → 84 taken 908 times.
✓ Branch 83 → 86 taken 24046 times.
✓ Branch 86 → 87 taken 109 times.
✓ Branch 86 → 94 taken 24845 times.
✗ Branch 238 → 239 not taken.
✗ Branch 238 → 240 not taken.
✗ Branch 242 → 243 not taken.
✗ Branch 242 → 245 not taken.
|
26770 | if (node->attrs && node->attrs->attrLst->hasAttr(ATTR_TEST)) |
| 196 |
2/4✓ Branch 89 → 90 taken 109 times.
✗ Branch 89 → 249 not taken.
✓ Branch 90 → 91 taken 109 times.
✗ Branch 90 → 247 not taken.
|
327 | isPublic |= node->attrs->attrLst->getAttrValueByName(ATTR_TEST)->boolValue; |
| 197 | |||
| 198 | // Create function or implement declared function | ||
| 199 |
1/2✓ Branch 94 → 95 taken 24954 times.
✗ Branch 94 → 301 not taken.
|
24954 | const std::string mangledName = manifestation->getMangledName(); |
| 200 |
1/2✓ Branch 96 → 97 taken 24954 times.
✗ Branch 96 → 253 not taken.
|
24954 | llvm::FunctionType *funcType = llvm::FunctionType::get(returnType, paramTypes, false); |
| 201 |
1/2✓ Branch 98 → 99 taken 24954 times.
✗ Branch 98 → 254 not taken.
|
24954 | module->getOrInsertFunction(mangledName, funcType); |
| 202 |
1/2✓ Branch 100 → 101 taken 24954 times.
✗ Branch 100 → 255 not taken.
|
24954 | llvm::Function *func = module->getFunction(mangledName); |
| 203 |
1/2✓ Branch 101 → 102 taken 24954 times.
✗ Branch 101 → 299 not taken.
|
24954 | updateAddress(node->entry, func); |
| 204 |
1/2✓ Branch 102 → 103 taken 24954 times.
✗ Branch 102 → 299 not taken.
|
24954 | setLLVMFunction(manifestation, func); |
| 205 |
2/4✓ Branch 103 → 104 taken 24954 times.
✗ Branch 103 → 299 not taken.
✗ Branch 104 → 105 not taken.
✓ Branch 104 → 106 taken 24954 times.
|
24954 | assert(func->empty()); |
| 206 | |||
| 207 | // Set attributes to function | ||
| 208 |
1/2✓ Branch 106 → 107 taken 24954 times.
✗ Branch 106 → 299 not taken.
|
24954 | func->setDSOLocal(isSymbolDSOLocal(isPublic)); |
| 209 |
2/4✓ Branch 108 → 109 taken 24954 times.
✗ Branch 108 → 299 not taken.
✓ Branch 109 → 110 taken 24954 times.
✗ Branch 109 → 299 not taken.
|
24954 | func->setLinkage(getSymbolLinkageType(isPublic)); |
| 210 |
3/6✓ Branch 110 → 111 taken 24954 times.
✗ Branch 110 → 299 not taken.
✓ Branch 111 → 112 taken 24954 times.
✗ Branch 111 → 299 not taken.
✓ Branch 112 → 113 taken 24954 times.
✗ Branch 112 → 299 not taken.
|
24954 | addCommonFctAttrs(func, manifestation->entry->getQualType().isInline()); |
| 211 |
1/2✓ Branch 113 → 114 taken 24954 times.
✗ Branch 113 → 299 not taken.
|
24954 | enableFunctionInstrumentation(func); |
| 212 | // Set attributes to function parameters and return value | ||
| 213 |
1/2✓ Branch 114 → 115 taken 24954 times.
✗ Branch 114 → 299 not taken.
|
24954 | setParamAttrs(func, paramInfoList); |
| 214 |
1/2✓ Branch 115 → 116 taken 24954 times.
✗ Branch 115 → 299 not taken.
|
24954 | setFunctionReturnValAttrs(func, manifestation->returnType); |
| 215 | |||
| 216 | // Add debug info | ||
| 217 |
1/2✓ Branch 116 → 117 taken 24954 times.
✗ Branch 116 → 299 not taken.
|
24954 | diGenerator.generateFunctionDebugInfo(func, manifestation); |
| 218 |
1/2✓ Branch 117 → 118 taken 24954 times.
✗ Branch 117 → 299 not taken.
|
24954 | diGenerator.setSourceLocation(node); |
| 219 | |||
| 220 | // Create entry block | ||
| 221 |
1/2✓ Branch 121 → 122 taken 24954 times.
✗ Branch 121 → 256 not taken.
|
24954 | llvm::BasicBlock *bEntry = createBlock(); |
| 222 |
1/2✓ Branch 124 → 125 taken 24954 times.
✗ Branch 124 → 299 not taken.
|
24954 | switchToBlock(bEntry, func); |
| 223 | |||
| 224 | // Reset alloca insert markers to this block | ||
| 225 | 24954 | allocaInsertBlock = bEntry; | |
| 226 | 24954 | allocaInsertInst = nullptr; | |
| 227 | |||
| 228 | // Declare result variable | ||
| 229 |
2/4✓ Branch 127 → 128 taken 24954 times.
✗ Branch 127 → 264 not taken.
✓ Branch 128 → 129 taken 24954 times.
✗ Branch 128 → 262 not taken.
|
24954 | llvm::Value *resultAddr = insertAlloca(manifestation->returnType, RETURN_VARIABLE_NAME); |
| 230 |
1/2✓ Branch 133 → 134 taken 24954 times.
✗ Branch 133 → 270 not taken.
|
74862 | const SymbolTableEntry *resultEntry = currentScope->lookupStrict(RETURN_VARIABLE_NAME); |
| 231 |
1/2✗ Branch 139 → 140 not taken.
✓ Branch 139 → 141 taken 24954 times.
|
24954 | assert(resultEntry != nullptr); |
| 232 |
1/2✓ Branch 141 → 142 taken 24954 times.
✗ Branch 141 → 299 not taken.
|
24954 | updateAddress(resultEntry, resultAddr); |
| 233 | // Generate debug info | ||
| 234 |
2/4✓ Branch 144 → 145 taken 24954 times.
✗ Branch 144 → 276 not taken.
✓ Branch 145 → 146 taken 24954 times.
✗ Branch 145 → 274 not taken.
|
49908 | diGenerator.generateLocalVarDebugInfo(RETURN_VARIABLE_NAME, resultAddr); |
| 235 | |||
| 236 | // Store function argument values | ||
| 237 |
3/4✓ Branch 148 → 149 taken 24954 times.
✗ Branch 148 → 287 not taken.
✓ Branch 174 → 151 taken 40295 times.
✓ Branch 174 → 175 taken 24954 times.
|
65249 | for (auto &arg : func->args()) { |
| 238 | // Get information about the parameter | ||
| 239 | 40295 | const size_t argNumber = arg.getArgNo(); | |
| 240 |
2/4✓ Branch 152 → 153 taken 40295 times.
✗ Branch 152 → 280 not taken.
✓ Branch 153 → 154 taken 40295 times.
✗ Branch 153 → 280 not taken.
|
40295 | auto [paramName, paramSymbol] = paramInfoList.at(argNumber); |
| 241 |
1/2✗ Branch 156 → 157 not taken.
✓ Branch 156 → 158 taken 40295 times.
|
40295 | assert(paramSymbol != nullptr); |
| 242 | // Decayed array params already carry the address of the array, so they do not need a local copy | ||
| 243 |
3/4✓ Branch 158 → 159 taken 40295 times.
✗ Branch 158 → 280 not taken.
✓ Branch 159 → 160 taken 9 times.
✓ Branch 159 → 161 taken 40286 times.
|
40295 | if (bindDecayedArrayParam(arg, paramName, paramSymbol)) |
| 244 | 9 | continue; | |
| 245 | // Allocate space for it | ||
| 246 |
2/4✓ Branch 161 → 162 taken 40286 times.
✗ Branch 161 → 280 not taken.
✓ Branch 162 → 163 taken 40286 times.
✗ Branch 162 → 280 not taken.
|
40286 | llvm::Value *paramAddress = insertAlloca(paramSymbol->getQualType(), paramName); |
| 247 | // Update the symbol table entry | ||
| 248 |
1/2✓ Branch 163 → 164 taken 40286 times.
✗ Branch 163 → 280 not taken.
|
40286 | updateAddress(paramSymbol, paramAddress); |
| 249 | // Set source location | ||
| 250 |
1/2✓ Branch 164 → 165 taken 40286 times.
✗ Branch 164 → 280 not taken.
|
40286 | diGenerator.setSourceLocation(paramSymbol->declNode); |
| 251 | // Generate debug info to declare variable | ||
| 252 |
1/2✓ Branch 165 → 166 taken 40286 times.
✗ Branch 165 → 280 not taken.
|
40286 | diGenerator.generateLocalVarDebugInfo(paramName, paramAddress, argNumber + 1); |
| 253 | // Store the value at the new address | ||
| 254 |
1/2✓ Branch 166 → 167 taken 40286 times.
✗ Branch 166 → 280 not taken.
|
40286 | insertStore(&arg, paramAddress); |
| 255 |
1/4✓ Branch 168 → 169 taken 40295 times.
✗ Branch 168 → 170 not taken.
✗ Branch 280 → 281 not taken.
✗ Branch 280 → 282 not taken.
|
40295 | } |
| 256 | |||
| 257 | // Store the default values for optional function args | ||
| 258 |
2/2✓ Branch 175 → 176 taken 18113 times.
✓ Branch 175 → 186 taken 6841 times.
|
24954 | if (node->paramLst) { |
| 259 |
1/2✓ Branch 176 → 177 taken 18113 times.
✗ Branch 176 → 291 not taken.
|
18113 | const std::vector<DeclStmtNode *> params = node->paramLst->params; |
| 260 |
2/2✓ Branch 183 → 178 taken 1779 times.
✓ Branch 183 → 184 taken 18113 times.
|
19892 | for (; argIdx < params.size(); argIdx++) |
| 261 |
2/4✓ Branch 178 → 179 taken 1779 times.
✗ Branch 178 → 288 not taken.
✓ Branch 179 → 180 taken 1779 times.
✗ Branch 179 → 288 not taken.
|
1779 | visit(params.at(argIdx)); |
| 262 | 18113 | } | |
| 263 | |||
| 264 | // Visit function body | ||
| 265 |
1/2✓ Branch 186 → 187 taken 24954 times.
✗ Branch 186 → 292 not taken.
|
24954 | visit(node->body); |
| 266 | |||
| 267 | // Create return statement if the block is not terminated yet | ||
| 268 |
2/2✓ Branch 188 → 189 taken 1207 times.
✓ Branch 188 → 197 taken 23747 times.
|
24954 | if (!blockAlreadyTerminated) { |
| 269 |
2/4✓ Branch 192 → 193 taken 1207 times.
✗ Branch 192 → 293 not taken.
✓ Branch 193 → 194 taken 1207 times.
✗ Branch 193 → 293 not taken.
|
1207 | llvm::Value *result = insertLoad(returnType, getAddress(resultEntry)); |
| 270 |
1/2✓ Branch 196 → 197 taken 1207 times.
✗ Branch 196 → 299 not taken.
|
1207 | builder.CreateRet(result); |
| 271 | } | ||
| 272 | |||
| 273 | // Conclude debug info for function | ||
| 274 |
1/2✓ Branch 197 → 198 taken 24954 times.
✗ Branch 197 → 299 not taken.
|
24954 | diGenerator.concludeFunctionDebugInfo(); |
| 275 | |||
| 276 | // Verify function | ||
| 277 |
1/2✓ Branch 198 → 199 taken 24954 times.
✗ Branch 198 → 299 not taken.
|
24954 | verifyFunction(func, node->codeLoc); |
| 278 | |||
| 279 | // Change to root scope | ||
| 280 | 24954 | currentScope = rootScope; | |
| 281 | |||
| 282 | 24954 | manIdx++; // Increment symbolTypeIndex | |
| 283 | 24954 | } | |
| 284 | 22651 | manIdx = 0; // Reset the symbolTypeIndex | |
| 285 | |||
| 286 | // Ensure that we are at the root scope again | ||
| 287 |
1/2✗ Branch 214 → 215 not taken.
✓ Branch 214 → 216 taken 22651 times.
|
22651 | assert(currentScope == rootScope); |
| 288 | |||
| 289 |
1/2✓ Branch 216 → 217 taken 22651 times.
✗ Branch 216 → 307 not taken.
|
45302 | return nullptr; |
| 290 | } | ||
| 291 | |||
| 292 | 12447 | std::any IRGenerator::visitProcDef(const ProcDefNode *node) { | |
| 293 | // Loop through manifestations | ||
| 294 | 12447 | manIdx = 0; // Reset the symbolTypeIndex | |
| 295 |
2/2✓ Branch 167 → 4 taken 19447 times.
✓ Branch 167 → 168 taken 12447 times.
|
44341 | for (const Function *manifestation : node->manifestations) { |
| 296 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 19447 times.
|
19447 | assert(manifestation->entry != nullptr); |
| 297 | |||
| 298 | // Check if the manifestation is substantiated or not public and not used by anybody | ||
| 299 |
2/4✓ Branch 8 → 9 taken 19447 times.
✗ Branch 8 → 220 not taken.
✓ Branch 9 → 10 taken 19447 times.
✗ Branch 9 → 220 not taken.
|
19447 | const bool isPublic = manifestation->entry->getQualType().isPublic(); |
| 300 |
9/10✓ Branch 10 → 11 taken 19447 times.
✗ Branch 10 → 220 not taken.
✓ Branch 11 → 12 taken 14095 times.
✓ Branch 11 → 14 taken 5352 times.
✓ Branch 12 → 13 taken 2274 times.
✓ Branch 12 → 15 taken 11821 times.
✓ Branch 13 → 14 taken 18 times.
✓ Branch 13 → 15 taken 2256 times.
✓ Branch 16 → 17 taken 5370 times.
✓ Branch 16 → 18 taken 14077 times.
|
19447 | if (!manifestation->isFullySubstantiated() || (!isPublic && !manifestation->used)) { |
| 301 | 5370 | manIdx++; // Increment symbolTypeIndex | |
| 302 | 5370 | continue; | |
| 303 | } | ||
| 304 |
1/2✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 14077 times.
|
14077 | assert(manifestation->alreadyTypeChecked); |
| 305 | |||
| 306 | // Change to struct scope | ||
| 307 |
2/2✓ Branch 23 → 24 taken 12002 times.
✓ Branch 23 → 32 taken 2075 times.
|
14077 | if (manifestation->isMethod()) { |
| 308 | 12002 | const QualType &thisType = manifestation->thisType; | |
| 309 |
3/6✓ Branch 24 → 25 taken 12002 times.
✗ Branch 24 → 176 not taken.
✓ Branch 25 → 26 taken 12002 times.
✗ Branch 25 → 176 not taken.
✓ Branch 26 → 27 taken 12002 times.
✗ Branch 26 → 176 not taken.
|
12002 | const std::string scopeName = Struct::getScopeName(thisType.getSubType(), thisType.getTemplateTypes()); |
| 310 |
1/2✓ Branch 27 → 28 taken 12002 times.
✗ Branch 27 → 174 not taken.
|
12002 | currentScope = currentScope->getChildScope(scopeName); |
| 311 |
1/2✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 12002 times.
|
12002 | assert(currentScope != nullptr); |
| 312 | 12002 | } | |
| 313 | |||
| 314 | // Change scope | ||
| 315 |
2/4✓ Branch 32 → 33 taken 14077 times.
✗ Branch 32 → 179 not taken.
✓ Branch 33 → 34 taken 14077 times.
✗ Branch 33 → 177 not taken.
|
14077 | currentScope = currentScope->getChildScope(manifestation->getScopeName()); |
| 316 |
1/2✗ Branch 35 → 36 not taken.
✓ Branch 35 → 37 taken 14077 times.
|
14077 | assert(currentScope != nullptr); |
| 317 | |||
| 318 | // Get 'this' entry | ||
| 319 | 14077 | std::vector<std::pair<std::string, const SymbolTableEntry *>> paramInfoList; | |
| 320 |
1/2✓ Branch 37 → 38 taken 14077 times.
✗ Branch 37 → 216 not taken.
|
14077 | std::vector<llvm::Type *> paramTypes; |
| 321 |
2/2✓ Branch 40 → 41 taken 12002 times.
✓ Branch 40 → 55 taken 2075 times.
|
14077 | if (manifestation->isMethod()) { |
| 322 |
1/2✓ Branch 43 → 44 taken 12002 times.
✗ Branch 43 → 182 not taken.
|
36006 | const SymbolTableEntry *thisEntry = currentScope->lookupStrict(THIS_VARIABLE_NAME); |
| 323 |
1/2✗ Branch 49 → 50 not taken.
✓ Branch 49 → 51 taken 12002 times.
|
12002 | assert(thisEntry != nullptr); |
| 324 |
1/2✓ Branch 51 → 52 taken 12002 times.
✗ Branch 51 → 187 not taken.
|
12002 | paramInfoList.emplace_back(THIS_VARIABLE_NAME, thisEntry); |
| 325 |
2/4✓ Branch 52 → 53 taken 12002 times.
✗ Branch 52 → 186 not taken.
✓ Branch 53 → 54 taken 12002 times.
✗ Branch 53 → 186 not taken.
|
12002 | paramTypes.push_back(builder.getPtrTy()); |
| 326 | } | ||
| 327 | |||
| 328 | // Visit parameters | ||
| 329 | 14077 | size_t argIdx = 0; | |
| 330 |
2/2✓ Branch 55 → 56 taken 10602 times.
✓ Branch 55 → 74 taken 3475 times.
|
14077 | if (node->hasParams) { |
| 331 | 10602 | const size_t numOfParams = manifestation->paramList.size(); | |
| 332 |
1/2✓ Branch 57 → 58 taken 10602 times.
✗ Branch 57 → 216 not taken.
|
10602 | paramInfoList.reserve(numOfParams); |
| 333 |
1/2✓ Branch 58 → 59 taken 10602 times.
✗ Branch 58 → 216 not taken.
|
10602 | paramTypes.reserve(numOfParams); |
| 334 |
2/2✓ Branch 73 → 60 taken 14638 times.
✓ Branch 73 → 74 taken 10602 times.
|
25240 | for (; argIdx < numOfParams; argIdx++) { |
| 335 |
1/2✓ Branch 60 → 61 taken 14638 times.
✗ Branch 60 → 191 not taken.
|
14638 | const DeclStmtNode *param = node->paramLst->params.at(argIdx); |
| 336 | // Get symbol table entry of param | ||
| 337 |
1/2✓ Branch 61 → 62 taken 14638 times.
✗ Branch 61 → 191 not taken.
|
14638 | const SymbolTableEntry *paramSymbol = currentScope->lookupStrict(param->varName); |
| 338 |
1/2✗ Branch 64 → 65 not taken.
✓ Branch 64 → 66 taken 14638 times.
|
14638 | assert(paramSymbol != nullptr); |
| 339 |
2/4✓ Branch 66 → 67 taken 14638 times.
✗ Branch 66 → 190 not taken.
✓ Branch 67 → 68 taken 14638 times.
✗ Branch 67 → 188 not taken.
|
14638 | const QualType paramSymbolType = manifestation->getParamTypes().at(argIdx); |
| 340 | // Retrieve type of param | ||
| 341 |
1/2✓ Branch 69 → 70 taken 14638 times.
✗ Branch 69 → 191 not taken.
|
14638 | llvm::Type *paramType = paramSymbolType.getParamLLVMType(sourceFile); |
| 342 | // Add it to the lists | ||
| 343 |
1/2✓ Branch 70 → 71 taken 14638 times.
✗ Branch 70 → 191 not taken.
|
14638 | paramInfoList.emplace_back(param->varName, paramSymbol); |
| 344 |
1/2✓ Branch 71 → 72 taken 14638 times.
✗ Branch 71 → 191 not taken.
|
14638 | paramTypes.push_back(paramType); |
| 345 | } | ||
| 346 | } | ||
| 347 | |||
| 348 | // Get return type | ||
| 349 |
1/2✓ Branch 74 → 75 taken 14077 times.
✗ Branch 74 → 216 not taken.
|
14077 | llvm::Type *returnType = builder.getVoidTy(); |
| 350 | |||
| 351 | // Create procedure or implement declared procedure | ||
| 352 |
1/2✓ Branch 75 → 76 taken 14077 times.
✗ Branch 75 → 216 not taken.
|
14077 | const std::string mangledName = manifestation->getMangledName(); |
| 353 |
1/2✓ Branch 77 → 78 taken 14077 times.
✗ Branch 77 → 192 not taken.
|
14077 | llvm::FunctionType *procType = llvm::FunctionType::get(returnType, paramTypes, false); |
| 354 |
1/2✓ Branch 79 → 80 taken 14077 times.
✗ Branch 79 → 193 not taken.
|
14077 | module->getOrInsertFunction(mangledName, procType); |
| 355 |
1/2✓ Branch 81 → 82 taken 14077 times.
✗ Branch 81 → 194 not taken.
|
14077 | llvm::Function *proc = module->getFunction(mangledName); |
| 356 |
1/2✓ Branch 82 → 83 taken 14077 times.
✗ Branch 82 → 214 not taken.
|
14077 | updateAddress(node->entry, proc); |
| 357 |
1/2✓ Branch 83 → 84 taken 14077 times.
✗ Branch 83 → 214 not taken.
|
14077 | setLLVMFunction(manifestation, proc); |
| 358 |
2/4✓ Branch 84 → 85 taken 14077 times.
✗ Branch 84 → 214 not taken.
✗ Branch 85 → 86 not taken.
✓ Branch 85 → 87 taken 14077 times.
|
14077 | assert(proc->empty()); |
| 359 | |||
| 360 | // Set attributes to procedure | ||
| 361 |
2/4✓ Branch 87 → 88 taken 14077 times.
✗ Branch 87 → 214 not taken.
✓ Branch 88 → 89 taken 14077 times.
✗ Branch 88 → 214 not taken.
|
14077 | proc->setLinkage(getSymbolLinkageType(isPublic)); |
| 362 |
1/2✓ Branch 89 → 90 taken 14077 times.
✗ Branch 89 → 214 not taken.
|
14077 | proc->setDSOLocal(isSymbolDSOLocal(isPublic)); |
| 363 |
3/6✓ Branch 91 → 92 taken 14077 times.
✗ Branch 91 → 214 not taken.
✓ Branch 92 → 93 taken 14077 times.
✗ Branch 92 → 214 not taken.
✓ Branch 93 → 94 taken 14077 times.
✗ Branch 93 → 214 not taken.
|
14077 | addCommonFctAttrs(proc, manifestation->entry->getQualType().isInline()); |
| 364 |
1/2✓ Branch 94 → 95 taken 14077 times.
✗ Branch 94 → 214 not taken.
|
14077 | enableFunctionInstrumentation(proc); |
| 365 | |||
| 366 | // Set attributes to function parameters | ||
| 367 |
1/2✓ Branch 95 → 96 taken 14077 times.
✗ Branch 95 → 214 not taken.
|
14077 | setParamAttrs(proc, paramInfoList); |
| 368 | |||
| 369 | // Add debug info | ||
| 370 |
1/2✓ Branch 96 → 97 taken 14077 times.
✗ Branch 96 → 214 not taken.
|
14077 | diGenerator.generateFunctionDebugInfo(proc, manifestation); |
| 371 |
1/2✓ Branch 97 → 98 taken 14077 times.
✗ Branch 97 → 214 not taken.
|
14077 | diGenerator.setSourceLocation(node); |
| 372 | |||
| 373 | // Create entry block | ||
| 374 |
1/2✓ Branch 101 → 102 taken 14077 times.
✗ Branch 101 → 195 not taken.
|
14077 | llvm::BasicBlock *bEntry = createBlock(); |
| 375 |
1/2✓ Branch 104 → 105 taken 14077 times.
✗ Branch 104 → 214 not taken.
|
14077 | switchToBlock(bEntry, proc); |
| 376 | |||
| 377 | // Reset alloca insert markers to this block | ||
| 378 | 14077 | allocaInsertBlock = bEntry; | |
| 379 | 14077 | allocaInsertInst = nullptr; | |
| 380 | |||
| 381 | // Store procedure argument values | ||
| 382 |
3/4✓ Branch 105 → 106 taken 14077 times.
✗ Branch 105 → 208 not taken.
✓ Branch 131 → 108 taken 26640 times.
✓ Branch 131 → 132 taken 14077 times.
|
40717 | for (auto &arg : proc->args()) { |
| 383 | // Get information about the parameter | ||
| 384 | 26640 | const size_t argNumber = arg.getArgNo(); | |
| 385 |
2/4✓ Branch 109 → 110 taken 26640 times.
✗ Branch 109 → 201 not taken.
✓ Branch 110 → 111 taken 26640 times.
✗ Branch 110 → 201 not taken.
|
26640 | auto [paramName, paramSymbol] = paramInfoList.at(argNumber); |
| 386 |
1/2✗ Branch 113 → 114 not taken.
✓ Branch 113 → 115 taken 26640 times.
|
26640 | assert(paramSymbol != nullptr); |
| 387 | // Decayed array params already carry the address of the array, so they do not need a local copy | ||
| 388 |
3/4✓ Branch 115 → 116 taken 26640 times.
✗ Branch 115 → 201 not taken.
✓ Branch 116 → 117 taken 3 times.
✓ Branch 116 → 118 taken 26637 times.
|
26640 | if (bindDecayedArrayParam(arg, paramName, paramSymbol)) |
| 389 | 3 | continue; | |
| 390 | // Allocate space for it | ||
| 391 |
2/4✓ Branch 118 → 119 taken 26637 times.
✗ Branch 118 → 201 not taken.
✓ Branch 119 → 120 taken 26637 times.
✗ Branch 119 → 201 not taken.
|
26637 | llvm::Value *paramAddress = insertAlloca(paramSymbol->getQualType(), paramName); |
| 392 | // Update the symbol table entry | ||
| 393 |
1/2✓ Branch 120 → 121 taken 26637 times.
✗ Branch 120 → 201 not taken.
|
26637 | updateAddress(paramSymbol, paramAddress); |
| 394 | // Set source location | ||
| 395 |
1/2✓ Branch 121 → 122 taken 26637 times.
✗ Branch 121 → 201 not taken.
|
26637 | diGenerator.setSourceLocation(paramSymbol->declNode); |
| 396 | // Generate debug info to declare variable | ||
| 397 |
1/2✓ Branch 122 → 123 taken 26637 times.
✗ Branch 122 → 201 not taken.
|
26637 | diGenerator.generateLocalVarDebugInfo(paramName, paramAddress, argNumber + 1); |
| 398 | // Store the value at the new address | ||
| 399 |
1/2✓ Branch 123 → 124 taken 26637 times.
✗ Branch 123 → 201 not taken.
|
26637 | insertStore(&arg, paramAddress); |
| 400 |
1/4✓ Branch 125 → 126 taken 26640 times.
✗ Branch 125 → 127 not taken.
✗ Branch 201 → 202 not taken.
✗ Branch 201 → 203 not taken.
|
26640 | } |
| 401 | |||
| 402 | // Store the default values for optional procedure args | ||
| 403 |
2/2✓ Branch 132 → 133 taken 10602 times.
✓ Branch 132 → 143 taken 3475 times.
|
14077 | if (node->paramLst) { |
| 404 |
1/2✓ Branch 133 → 134 taken 10602 times.
✗ Branch 133 → 212 not taken.
|
10602 | const std::vector<DeclStmtNode *> params = node->paramLst->params; |
| 405 |
2/2✓ Branch 140 → 135 taken 792 times.
✓ Branch 140 → 141 taken 10602 times.
|
11394 | for (; argIdx < params.size(); argIdx++) |
| 406 |
2/4✓ Branch 135 → 136 taken 792 times.
✗ Branch 135 → 209 not taken.
✓ Branch 136 → 137 taken 792 times.
✗ Branch 136 → 209 not taken.
|
792 | visit(params.at(argIdx)); |
| 407 | 10602 | } | |
| 408 | |||
| 409 | // Generate special ctor preamble before generating the body to store VTable, default field values, etc. if required | ||
| 410 |
2/2✓ Branch 143 → 144 taken 6222 times.
✓ Branch 143 → 145 taken 7855 times.
|
14077 | if (node->isCtor) { |
| 411 | 6222 | isInCtorBody = true; | |
| 412 |
1/2✓ Branch 144 → 145 taken 6222 times.
✗ Branch 144 → 214 not taken.
|
6222 | generateCtorBodyPreamble(currentScope); |
| 413 | } | ||
| 414 | |||
| 415 | // Visit procedure body | ||
| 416 |
1/2✓ Branch 145 → 146 taken 14077 times.
✗ Branch 145 → 213 not taken.
|
14077 | visit(node->body); |
| 417 | |||
| 418 |
2/2✓ Branch 147 → 148 taken 6222 times.
✓ Branch 147 → 149 taken 7855 times.
|
14077 | if (node->isCtor) |
| 419 | 6222 | isInCtorBody = false; | |
| 420 | |||
| 421 | // Create return statement if the block is not terminated yet | ||
| 422 |
2/2✓ Branch 149 → 150 taken 13866 times.
✓ Branch 149 → 151 taken 211 times.
|
14077 | if (!blockAlreadyTerminated) |
| 423 |
1/2✓ Branch 150 → 151 taken 13866 times.
✗ Branch 150 → 214 not taken.
|
13866 | builder.CreateRetVoid(); |
| 424 | |||
| 425 | // Conclude debug info for procedure | ||
| 426 |
1/2✓ Branch 151 → 152 taken 14077 times.
✗ Branch 151 → 214 not taken.
|
14077 | diGenerator.concludeFunctionDebugInfo(); |
| 427 | |||
| 428 | // Verify procedure | ||
| 429 |
1/2✓ Branch 152 → 153 taken 14077 times.
✗ Branch 152 → 214 not taken.
|
14077 | verifyFunction(proc, node->codeLoc); |
| 430 | |||
| 431 | // Change to root scope | ||
| 432 | 14077 | currentScope = rootScope; | |
| 433 | |||
| 434 | 14077 | manIdx++; // Increment symbolTypeIndex | |
| 435 | 14077 | } | |
| 436 | 12447 | manIdx = 0; // Reset the symbolTypeIndex | |
| 437 | |||
| 438 | // Ensure that we are at the root scope again | ||
| 439 |
1/2✗ Branch 168 → 169 not taken.
✓ Branch 168 → 170 taken 12447 times.
|
12447 | assert(currentScope == rootScope); |
| 440 | |||
| 441 |
1/2✓ Branch 170 → 171 taken 12447 times.
✗ Branch 170 → 222 not taken.
|
24894 | return nullptr; |
| 442 | } | ||
| 443 | |||
| 444 | /** | ||
| 445 | * Bind a function argument that carries a decayed array to its parameter symbol. Since the argument already is the | ||
| 446 | * address of the array, it can be used as the address of the parameter directly, without allocating a local copy. | ||
| 447 | * | ||
| 448 | * @param arg Argument of the LLVM function | ||
| 449 | * @param paramName Name of the parameter | ||
| 450 | * @param paramSymbol Symbol table entry of the parameter | ||
| 451 | * @return Whether the argument was bound (false if the parameter does not carry a decayed array) | ||
| 452 | */ | ||
| 453 | 67132 | bool IRGenerator::bindDecayedArrayParam(llvm::Argument &arg, const std::string ¶mName, | |
| 454 | const SymbolTableEntry *paramSymbol) { | ||
| 455 |
6/6✓ Branch 2 → 3 taken 67042 times.
✓ Branch 2 → 6 taken 90 times.
✓ Branch 5 → 6 taken 67029 times.
✓ Branch 5 → 7 taken 13 times.
✓ Branch 8 → 9 taken 67119 times.
✓ Branch 8 → 10 taken 13 times.
|
67132 | if (paramSymbol == nullptr || !paramSymbol->getQualType().isDecayedArray()) |
| 456 | 67119 | return false; | |
| 457 | |||
| 458 |
2/4✓ Branch 10 → 11 taken 13 times.
✗ Branch 10 → 18 not taken.
✓ Branch 11 → 12 taken 13 times.
✗ Branch 11 → 18 not taken.
|
13 | arg.setName(paramName); |
| 459 | 13 | updateAddress(paramSymbol, &arg); | |
| 460 | // Set source location and generate debug info to declare the variable | ||
| 461 | 13 | diGenerator.setSourceLocation(paramSymbol->declNode); | |
| 462 | 13 | diGenerator.generateLocalVarDebugInfo(paramName, &arg, arg.getArgNo() + 1); | |
| 463 | 13 | return true; | |
| 464 | } | ||
| 465 | |||
| 466 | /** | ||
| 467 | * Materialize an argument that is passed to a decayed array parameter, so that the callee receives the address of a | ||
| 468 | * writable array. | ||
| 469 | * | ||
| 470 | * Two cases need fixing up: if the argument was produced as the array itself instead of its address, it has to be put | ||
| 471 | * into memory. And if it was produced as the address of a read-only constant - array literals are emitted as global | ||
| 472 | * constants - it has to be copied into a mutable stack slot, because the callee may assign through the parameter. The | ||
| 473 | * latter matches how C frontends materialize array compound literals. | ||
| 474 | * | ||
| 475 | * @param argValue Value that was produced for the argument | ||
| 476 | * @param paramType Type of the parameter | ||
| 477 | * @return Address of the array to hand to the callee | ||
| 478 | */ | ||
| 479 | 17 | llvm::Value *IRGenerator::materializeDecayedArrayArg(llvm::Value *argValue, const QualType ¶mType) { | |
| 480 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 17 times.
|
17 | assert(paramType.isDecayedArray()); |
| 481 | 17 | llvm::Type *paramArrayType = paramType.toLLVMType(sourceFile); | |
| 482 | |||
| 483 | // The array itself was produced (e.g. as the result of an implicit cast) -> put it into memory | ||
| 484 |
1/2✗ Branch 8 → 9 not taken.
✓ Branch 8 → 17 taken 17 times.
|
17 | if (!argValue->getType()->isPointerTy()) { |
| 485 | ✗ | llvm::Value *argAddress = insertAlloca(paramArrayType, "arg.decay"); | |
| 486 | ✗ | insertStore(argValue, argAddress); | |
| 487 | ✗ | return argAddress; | |
| 488 | } | ||
| 489 | |||
| 490 | // Look through constant offsets, because the implicit array-to-array cast GEPs into the global constant | ||
| 491 | 17 | const auto *global = llvm::dyn_cast<llvm::GlobalVariable>(argValue->stripInBoundsConstantOffsets()); | |
| 492 |
5/6✓ Branch 19 → 20 taken 6 times.
✓ Branch 19 → 22 taken 11 times.
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 6 times.
✓ Branch 24 → 25 taken 11 times.
✓ Branch 24 → 26 taken 6 times.
|
17 | if (global == nullptr || !global->isConstant()) |
| 493 | 11 | return argValue; | |
| 494 | |||
| 495 | // Copy the constant into a mutable stack slot. Function matching does not enforce that the argument array has the | ||
| 496 | // same size as the parameter array, so copy the smaller of the two to never read past the end of the global. | ||
| 497 | 6 | const llvm::DataLayout &dataLayout = module->getDataLayout(); | |
| 498 | 6 | llvm::Type *globalType = global->getValueType(); | |
| 499 |
4/8✓ Branch 28 → 29 taken 6 times.
✗ Branch 28 → 51 not taken.
✓ Branch 29 → 30 taken 6 times.
✗ Branch 29 → 51 not taken.
✓ Branch 30 → 31 taken 6 times.
✗ Branch 30 → 50 not taken.
✓ Branch 31 → 32 taken 6 times.
✗ Branch 31 → 50 not taken.
|
6 | const bool globalIsSmaller = dataLayout.getTypeAllocSize(globalType) < dataLayout.getTypeAllocSize(paramArrayType); |
| 500 |
2/4✓ Branch 34 → 35 taken 6 times.
✗ Branch 34 → 54 not taken.
✓ Branch 35 → 36 taken 6 times.
✗ Branch 35 → 52 not taken.
|
6 | llvm::Value *argAddress = insertAlloca(paramArrayType, "arg.decay"); |
| 501 |
1/2✗ Branch 38 → 39 not taken.
✓ Branch 38 → 40 taken 6 times.
|
6 | generateShallowCopy(argValue, globalIsSmaller ? globalType : paramArrayType, argAddress, false); |
| 502 | 6 | return argAddress; | |
| 503 | } | ||
| 504 | |||
| 505 | 39031 | void IRGenerator::setParamAttrs(llvm::Function *function, const ParamInfoList ¶mInfo) const { | |
| 506 |
1/2✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 39031 times.
|
39031 | assert(function->arg_size() == paramInfo.size()); |
| 507 |
2/2✓ Branch 30 → 7 taken 66935 times.
✓ Branch 30 → 31 taken 39031 times.
|
105966 | for (size_t i = 0; i < paramInfo.size(); i++) { |
| 508 | 66935 | const QualType ¶mType = paramInfo.at(i).second->getQualType(); | |
| 509 | |||
| 510 | // NoUndef attribute | ||
| 511 | 66935 | function->addParamAttr(i, llvm::Attribute::NoUndef); | |
| 512 | |||
| 513 |
2/2✓ Branch 11 → 12 taken 29109 times.
✓ Branch 11 → 25 taken 37826 times.
|
66935 | if (paramType.isPtr()) { |
| 514 | // NonNull attribute | ||
| 515 | 29109 | function->addParamAttr(i, llvm::Attribute::NonNull); | |
| 516 | // Dereferenceable attribute | ||
| 517 |
2/4✓ Branch 13 → 14 taken 29109 times.
✗ Branch 13 → 32 not taken.
✓ Branch 14 → 15 taken 29109 times.
✗ Branch 14 → 32 not taken.
|
29109 | llvm::Type *pointeeType = paramType.getContained().toLLVMType(sourceFile); |
| 518 |
1/2✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 29109 times.
|
29109 | assert(pointeeType != nullptr); |
| 519 |
3/6✓ Branch 18 → 19 taken 29109 times.
✗ Branch 18 → 33 not taken.
✓ Branch 19 → 20 taken 29109 times.
✗ Branch 19 → 33 not taken.
✓ Branch 20 → 21 taken 29109 times.
✗ Branch 20 → 33 not taken.
|
29109 | function->addDereferenceableParamAttr(i, module->getDataLayout().getTypeStoreSize(pointeeType)); |
| 520 | // Alignment attribute | ||
| 521 | 29109 | function->addParamAttr(i, llvm::Attribute::getWithAlignment(context, module->getDataLayout().getABITypeAlign(pointeeType))); | |
| 522 | } | ||
| 523 | |||
| 524 | // ZExt or SExt attribute | ||
| 525 |
2/2✓ Branch 26 → 27 taken 7766 times.
✓ Branch 26 → 28 taken 59169 times.
|
66935 | if (const llvm::Attribute::AttrKind extAttrKind = getExtAttrKindForType(paramType); extAttrKind != llvm::Attribute::None) |
| 526 | 7766 | function->addParamAttr(i, extAttrKind); | |
| 527 | } | ||
| 528 | 39031 | } | |
| 529 | |||
| 530 | 24954 | void IRGenerator::setFunctionReturnValAttrs(llvm::Function *function, const QualType &returnType) const { | |
| 531 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 24954 times.
|
24954 | if (returnType.is(TY_DYN)) |
| 532 | ✗ | return; | |
| 533 | |||
| 534 | // NoUndef attribute | ||
| 535 | 24954 | function->addRetAttr(llvm::Attribute::NoUndef); | |
| 536 | // ZExt or SExt attribute | ||
| 537 |
2/2✓ Branch 7 → 8 taken 8855 times.
✓ Branch 7 → 9 taken 16099 times.
|
24954 | if (const llvm::Attribute::AttrKind extAttrKind = getExtAttrKindForType(returnType); extAttrKind != llvm::Attribute::None) |
| 538 | 8855 | function->addRetAttr(extAttrKind); | |
| 539 | } | ||
| 540 | |||
| 541 | 246113 | llvm::Attribute::AttrKind IRGenerator::getExtAttrKindForType(const QualType &type) const { | |
| 542 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 246113 times.
|
246113 | if (type.is(TY_DYN)) |
| 543 | ✗ | return llvm::Attribute::None; | |
| 544 | |||
| 545 | 246113 | const llvm::Type *llvmType = type.toLLVMType(sourceFile); | |
| 546 |
1/2✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 246113 times.
|
246113 | const unsigned int maxExtWidth = cliOptions.targetTriple.isPPC() ? 64 : 32; |
| 547 |
6/6✓ Branch 11 → 12 taken 80307 times.
✓ Branch 11 → 15 taken 165806 times.
✓ Branch 13 → 14 taken 37174 times.
✓ Branch 13 → 15 taken 43133 times.
✓ Branch 16 → 17 taken 37174 times.
✓ Branch 16 → 22 taken 208939 times.
|
246113 | if (llvmType->isIntegerTy() && llvmType->getIntegerBitWidth() < maxExtWidth) |
| 548 |
2/2✓ Branch 18 → 19 taken 2341 times.
✓ Branch 18 → 20 taken 34833 times.
|
37174 | return type.isSigned() ? llvm::Attribute::SExt : llvm::Attribute::ZExt; |
| 549 | 208939 | return llvm::Attribute::None; | |
| 550 | } | ||
| 551 | |||
| 552 | 3146 | std::any IRGenerator::visitStructDef(const StructDefNode *node) { | |
| 553 | // Get all substantiated structs which result from this struct def | ||
| 554 |
1/2✓ Branch 2 → 3 taken 3146 times.
✗ Branch 2 → 128 not taken.
|
3146 | std::vector<Struct *> manifestations = node->structManifestations; |
| 555 | |||
| 556 | // Sort the manifestations to prevent generating the struct types in the wrong order (in case of dependencies between structs) | ||
| 557 | 5466 | const auto comp = [](const Struct *lhs, const Struct *rhs) { return lhs->manifestationIndex < rhs->manifestationIndex; }; | |
| 558 |
1/2✓ Branch 3 → 4 taken 3146 times.
✗ Branch 3 → 126 not taken.
|
3146 | std::ranges::sort(manifestations, comp); |
| 559 | |||
| 560 |
2/2✓ Branch 83 → 6 taken 5465 times.
✓ Branch 83 → 84 taken 3146 times.
|
11757 | for (Struct *spiceStruct : manifestations) { |
| 561 | // Skip structs, that are not fully substantiated | ||
| 562 |
3/4✓ Branch 8 → 9 taken 5465 times.
✗ Branch 8 → 123 not taken.
✓ Branch 9 → 10 taken 861 times.
✓ Branch 9 → 11 taken 4604 times.
|
5465 | if (!spiceStruct->isFullySubstantiated()) |
| 563 | 864 | continue; | |
| 564 | |||
| 565 | // Do not generate this struct if it is private and used by nobody | ||
| 566 |
8/10✓ Branch 11 → 12 taken 849 times.
✓ Branch 11 → 16 taken 3755 times.
✓ Branch 12 → 13 taken 849 times.
✗ Branch 12 → 123 not taken.
✓ Branch 13 → 14 taken 849 times.
✗ Branch 13 → 123 not taken.
✓ Branch 14 → 15 taken 3 times.
✓ Branch 14 → 16 taken 846 times.
✓ Branch 17 → 18 taken 3 times.
✓ Branch 17 → 19 taken 4601 times.
|
4604 | if (!spiceStruct->used && !spiceStruct->entry->getQualType().isPublic()) |
| 567 | 3 | continue; | |
| 568 | |||
| 569 | // Change scope to struct scope, specific to substantiation | ||
| 570 | 4601 | currentScope = spiceStruct->scope; | |
| 571 |
1/2✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 4601 times.
|
4601 | assert(currentScope); |
| 572 | |||
| 573 | // Set LLVM type to the struct entry | ||
| 574 | 4601 | const SymbolTableEntry *structEntry = spiceStruct->entry; | |
| 575 |
1/2✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 4601 times.
|
4601 | assert(structEntry != nullptr); |
| 576 | |||
| 577 | // Generate VTable if required | ||
| 578 |
2/2✓ Branch 23 → 24 taken 1137 times.
✓ Branch 23 → 27 taken 3464 times.
|
4601 | if (node->emitVTable) { |
| 579 |
1/2✓ Branch 24 → 25 taken 1137 times.
✗ Branch 24 → 123 not taken.
|
1137 | generateVTable(spiceStruct); |
| 580 |
1/2✓ Branch 25 → 26 taken 1137 times.
✗ Branch 25 → 89 not taken.
|
2274 | deferredVTableInitializations.emplace_back([=, this]() { generateVTableInitializer(spiceStruct); }, false); |
| 581 | } | ||
| 582 | |||
| 583 | // Generate default ctor if required | ||
| 584 |
1/2✓ Branch 27 → 28 taken 4601 times.
✗ Branch 27 → 123 not taken.
|
4601 | const QualType &thisType = structEntry->getQualType(); |
| 585 |
2/4✓ Branch 31 → 32 taken 4601 times.
✗ Branch 31 → 93 not taken.
✓ Branch 32 → 33 taken 4601 times.
✗ Branch 32 → 91 not taken.
|
13803 | const Function *ctorFunc = FunctionManager::lookup(currentScope, CTOR_FUNCTION_NAME, thisType, {}, true); |
| 586 |
4/4✓ Branch 36 → 37 taken 1097 times.
✓ Branch 36 → 39 taken 3504 times.
✓ Branch 37 → 38 taken 155 times.
✓ Branch 37 → 39 taken 942 times.
|
4601 | if (ctorFunc != nullptr && ctorFunc->implicitDefault) |
| 587 |
1/2✓ Branch 38 → 39 taken 155 times.
✗ Branch 38 → 123 not taken.
|
155 | generateDefaultCtor(ctorFunc); |
| 588 | |||
| 589 | // Generate default copy ctor if required | ||
| 590 |
2/4✓ Branch 39 → 40 taken 4601 times.
✗ Branch 39 → 104 not taken.
✓ Branch 43 → 44 taken 4601 times.
✗ Branch 43 → 100 not taken.
|
13803 | const ArgList args = {{thisType.toConstRef(node), false /* always non-temporary */}}; |
| 591 |
2/4✓ Branch 47 → 48 taken 4601 times.
✗ Branch 47 → 108 not taken.
✓ Branch 48 → 49 taken 4601 times.
✗ Branch 48 → 106 not taken.
|
4601 | const Function *copyCtorFunc = FunctionManager::lookup(currentScope, CTOR_FUNCTION_NAME, thisType, args, true); |
| 592 |
4/4✓ Branch 51 → 52 taken 2355 times.
✓ Branch 51 → 54 taken 2246 times.
✓ Branch 52 → 53 taken 1873 times.
✓ Branch 52 → 54 taken 482 times.
|
4601 | if (copyCtorFunc != nullptr && copyCtorFunc->implicitDefault) |
| 593 |
1/2✓ Branch 53 → 54 taken 1873 times.
✗ Branch 53 → 121 not taken.
|
1873 | generateDefaultCopyCtor(copyCtorFunc); |
| 594 | |||
| 595 | // Generate default move ctor if required. We can't use FunctionManager::lookup with a non-const ref arg | ||
| 596 | // here, because the lookup permits const-param-to-non-const-arg matching ("constify") and may return the | ||
| 597 | // copy ctor as a false positive. findMoveCtor scans the manifestations directly and only matches the | ||
| 598 | // strict move ctor signature. | ||
| 599 |
5/6✓ Branch 54 → 55 taken 4601 times.
✗ Branch 54 → 121 not taken.
✓ Branch 55 → 56 taken 44 times.
✓ Branch 55 → 58 taken 4557 times.
✓ Branch 56 → 57 taken 10 times.
✓ Branch 56 → 58 taken 34 times.
|
4601 | if (const Function *moveCtorFunc = FunctionManager::findMoveCtor(currentScope); moveCtorFunc && moveCtorFunc->implicitDefault) |
| 600 |
1/2✓ Branch 57 → 58 taken 10 times.
✗ Branch 57 → 121 not taken.
|
10 | generateDefaultMoveCtor(moveCtorFunc); |
| 601 | |||
| 602 | // Generate default dtor if required | ||
| 603 |
2/4✓ Branch 61 → 62 taken 4601 times.
✗ Branch 61 → 114 not taken.
✓ Branch 62 → 63 taken 4601 times.
✗ Branch 62 → 112 not taken.
|
13803 | const Function *dtorFunc = FunctionManager::lookup(currentScope, DTOR_FUNCTION_NAME, thisType, {}, true); |
| 604 |
4/4✓ Branch 66 → 67 taken 2457 times.
✓ Branch 66 → 69 taken 2144 times.
✓ Branch 67 → 68 taken 1779 times.
✓ Branch 67 → 69 taken 678 times.
|
4601 | if (dtorFunc != nullptr && dtorFunc->implicitDefault) |
| 605 |
1/2✓ Branch 68 → 69 taken 1779 times.
✗ Branch 68 → 121 not taken.
|
1779 | generateDefaultDtor(dtorFunc); |
| 606 | |||
| 607 | // Return to root scope | ||
| 608 | 4601 | currentScope = rootScope; | |
| 609 |
1/2✗ Branch 69 → 70 not taken.
✓ Branch 69 → 71 taken 4601 times.
|
4601 | assert(currentScope); |
| 610 | 4601 | } | |
| 611 | |||
| 612 |
1/2✓ Branch 84 → 85 taken 3146 times.
✗ Branch 84 → 125 not taken.
|
6292 | return nullptr; |
| 613 | 3146 | } | |
| 614 | |||
| 615 | 388 | std::any IRGenerator::visitInterfaceDef(const InterfaceDefNode *node) { | |
| 616 | // Get all substantiated structs which result from this struct def | ||
| 617 |
1/2✓ Branch 2 → 3 taken 388 times.
✗ Branch 2 → 43 not taken.
|
388 | std::vector<Interface *> manifestations = node->interfaceManifestations; |
| 618 | |||
| 619 | // Sort the manifestations to prevent generating the struct types in the wrong order (in case of dependencies between structs) | ||
| 620 | 2514 | const auto comp = [](const Interface *lhs, const Interface *rhs) { return lhs->manifestationIndex < rhs->manifestationIndex; }; | |
| 621 |
1/2✓ Branch 3 → 4 taken 388 times.
✗ Branch 3 → 41 not taken.
|
388 | std::ranges::sort(manifestations, comp); |
| 622 | |||
| 623 |
2/2✓ Branch 31 → 6 taken 1318 times.
✓ Branch 31 → 32 taken 388 times.
|
2094 | for (Interface *spiceInterface : manifestations) { |
| 624 | // Skip interfaces, that are not fully substantiated | ||
| 625 |
3/4✓ Branch 8 → 9 taken 1318 times.
✗ Branch 8 → 39 not taken.
✓ Branch 9 → 10 taken 182 times.
✓ Branch 9 → 11 taken 1136 times.
|
1318 | if (!spiceInterface->isFullySubstantiated()) |
| 626 | 182 | continue; | |
| 627 | |||
| 628 | // Do not generate this interface if it is private and used by nobody | ||
| 629 |
8/10✓ Branch 11 → 12 taken 21 times.
✓ Branch 11 → 16 taken 1115 times.
✓ Branch 12 → 13 taken 21 times.
✗ Branch 12 → 39 not taken.
✓ Branch 13 → 14 taken 21 times.
✗ Branch 13 → 39 not taken.
✓ Branch 14 → 15 taken 1 time.
✓ Branch 14 → 16 taken 20 times.
✓ Branch 17 → 18 taken 1 time.
✓ Branch 17 → 19 taken 1135 times.
|
1136 | if (!spiceInterface->used && !spiceInterface->entry->getQualType().isPublic()) |
| 630 | 1 | continue; | |
| 631 | |||
| 632 | // Generate VTable information | ||
| 633 |
1/2✓ Branch 19 → 20 taken 1135 times.
✗ Branch 19 → 39 not taken.
|
1135 | generateVTable(spiceInterface); |
| 634 |
1/2✓ Branch 20 → 21 taken 1135 times.
✗ Branch 20 → 37 not taken.
|
2270 | deferredVTableInitializations.emplace_back([=, this]() { generateVTableInitializer(spiceInterface); }, false); |
| 635 | } | ||
| 636 | |||
| 637 |
1/2✓ Branch 32 → 33 taken 388 times.
✗ Branch 32 → 40 not taken.
|
776 | return nullptr; |
| 638 | 388 | } | |
| 639 | |||
| 640 | 381 | std::any IRGenerator::visitEnumDef(const EnumDefNode *node) { | |
| 641 |
1/2✓ Branch 2 → 3 taken 381 times.
✗ Branch 2 → 6 not taken.
|
762 | return nullptr; // Noop (enums are high-level semantic-only structures) |
| 642 | } | ||
| 643 | |||
| 644 | 2153 | std::any IRGenerator::visitGenericTypeDef(const GenericTypeDefNode *node) { | |
| 645 |
1/2✓ Branch 2 → 3 taken 2153 times.
✗ Branch 2 → 6 not taken.
|
4306 | return nullptr; // Noop (generic types are high-level semantic-only structures) |
| 646 | } | ||
| 647 | |||
| 648 | 348 | std::any IRGenerator::visitAliasDef(const AliasDefNode *node) { | |
| 649 |
1/2✓ Branch 2 → 3 taken 348 times.
✗ Branch 2 → 6 not taken.
|
696 | return nullptr; // Noop (alias definitions are high-level semantic-only structures) |
| 650 | } | ||
| 651 | |||
| 652 | 2701 | std::any IRGenerator::visitGlobalVarDef(const GlobalVarDefNode *node) { | |
| 653 | // Retrieve some information about the variable | ||
| 654 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2701 times.
|
2701 | assert(node->entry != nullptr); |
| 655 | 2701 | const QualType &entryType = node->entry->getQualType(); | |
| 656 | 2701 | const bool isPublic = entryType.isPublic(); | |
| 657 | 2701 | const bool isConst = entryType.isConst(); | |
| 658 | |||
| 659 | // Get correct type and linkage type | ||
| 660 |
2/4✓ Branch 7 → 8 taken 2701 times.
✗ Branch 7 → 39 not taken.
✓ Branch 8 → 9 taken 2701 times.
✗ Branch 8 → 37 not taken.
|
2701 | const auto varType = std::any_cast<llvm::Type *>(visit(node->dataType)); |
| 661 | |||
| 662 | // Create global var | ||
| 663 |
1/2✓ Branch 11 → 12 taken 2701 times.
✗ Branch 11 → 40 not taken.
|
2701 | llvm::Value *varAddress = module->getOrInsertGlobal(node->varName, varType); |
| 664 |
1/2✓ Branch 13 → 14 taken 2701 times.
✗ Branch 13 → 41 not taken.
|
2701 | llvm::GlobalVariable *var = module->getNamedGlobal(node->varName); |
| 665 | // Set some attributes, based on the given information | ||
| 666 | 2701 | var->setConstant(isConst); | |
| 667 | 2701 | var->setLinkage(getSymbolLinkageType(isPublic)); | |
| 668 | 2701 | var->setDSOLocal(isSymbolDSOLocal(isPublic)); | |
| 669 | |||
| 670 | // Set initializer | ||
| 671 |
1/2✓ Branch 19 → 20 taken 2701 times.
✗ Branch 19 → 24 not taken.
|
2701 | if (node->hasValue) { // Set the constant value as variable initializer |
| 672 |
2/4✓ Branch 20 → 21 taken 2701 times.
✗ Branch 20 → 44 not taken.
✓ Branch 21 → 22 taken 2701 times.
✗ Branch 21 → 42 not taken.
|
2701 | const auto constantValue = std::any_cast<llvm::Constant *>(visit(node->constant)); |
| 673 | 2701 | var->setInitializer(constantValue); | |
| 674 | ✗ | } else if (cliOptions.buildMode != BuildMode::RELEASE) { // Set the default value as variable initializer | |
| 675 | ✗ | assert(cliOptions.buildMode == BuildMode::DEBUG || cliOptions.buildMode == BuildMode::TEST); | |
| 676 | ✗ | llvm::Constant *constantValue = getDefaultValueForSymbolType(node->entry->getQualType()); | |
| 677 | ✗ | var->setInitializer(constantValue); | |
| 678 | } | ||
| 679 | |||
| 680 | 2701 | updateAddress(node->entry, varAddress); | |
| 681 | |||
| 682 | // Add debug info | ||
| 683 | 2701 | diGenerator.generateGlobalVarDebugInfo(var, node->entry); | |
| 684 | |||
| 685 |
1/2✓ Branch 33 → 34 taken 2701 times.
✗ Branch 33 → 45 not taken.
|
5402 | return nullptr; |
| 686 | } | ||
| 687 | |||
| 688 | 3877 | std::any IRGenerator::visitExtDecl(const ExtDeclNode *node) { | |
| 689 | // Get return type | ||
| 690 | 3877 | const Function *spiceFunc = node->extFunction; | |
| 691 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 3877 times.
|
3877 | assert(spiceFunc != nullptr); |
| 692 |
1/2✓ Branch 4 → 5 taken 3877 times.
✗ Branch 4 → 100 not taken.
|
3877 | llvm::Type *returnType = builder.getVoidTy(); |
| 693 |
3/4✓ Branch 5 → 6 taken 3877 times.
✗ Branch 5 → 100 not taken.
✓ Branch 6 → 7 taken 3079 times.
✓ Branch 6 → 9 taken 798 times.
|
3877 | if (!spiceFunc->returnType.is(TY_DYN)) |
| 694 |
1/2✓ Branch 7 → 8 taken 3079 times.
✗ Branch 7 → 100 not taken.
|
3079 | returnType = spiceFunc->returnType.toLLVMType(sourceFile); |
| 695 | |||
| 696 | // Get arg types | ||
| 697 | 3877 | std::vector<llvm::Type *> argTypes; | |
| 698 |
1/2✓ Branch 10 → 11 taken 3877 times.
✗ Branch 10 → 98 not taken.
|
3877 | argTypes.reserve(spiceFunc->paramList.size()); |
| 699 |
3/4✓ Branch 11 → 12 taken 3877 times.
✗ Branch 11 → 76 not taken.
✓ Branch 27 → 14 taken 8102 times.
✓ Branch 27 → 28 taken 3877 times.
|
15856 | for (const QualType ¶mType : spiceFunc->getParamTypes()) |
| 700 |
2/4✓ Branch 16 → 17 taken 8102 times.
✗ Branch 16 → 73 not taken.
✓ Branch 17 → 18 taken 8102 times.
✗ Branch 17 → 73 not taken.
|
11979 | argTypes.push_back(paramType.getParamLLVMType(sourceFile)); |
| 701 | |||
| 702 | // Declare function | ||
| 703 |
4/4✓ Branch 29 → 30 taken 3671 times.
✓ Branch 29 → 32 taken 206 times.
✓ Branch 30 → 31 taken 66 times.
✓ Branch 30 → 32 taken 3605 times.
|
3877 | const bool isVarArg = node->argTypeLst && node->argTypeLst->hasEllipsis; |
| 704 |
1/2✓ Branch 34 → 35 taken 3877 times.
✗ Branch 34 → 77 not taken.
|
3877 | llvm::FunctionType *functionType = llvm::FunctionType::get(returnType, argTypes, isVarArg); |
| 705 |
1/2✓ Branch 35 → 36 taken 3877 times.
✗ Branch 35 → 98 not taken.
|
3877 | const std::string mangledName = spiceFunc->getMangledName(); |
| 706 |
1/2✓ Branch 37 → 38 taken 3877 times.
✗ Branch 37 → 78 not taken.
|
3877 | module->getOrInsertFunction(mangledName, functionType); |
| 707 |
1/2✓ Branch 39 → 40 taken 3877 times.
✗ Branch 39 → 79 not taken.
|
3877 | llvm::Function *fct = module->getFunction(mangledName); |
| 708 | |||
| 709 | // Add noundef attribute to all parameters | ||
| 710 |
2/2✓ Branch 44 → 41 taken 8102 times.
✓ Branch 44 → 45 taken 3877 times.
|
11979 | for (size_t i = 0; i < argTypes.size(); i++) |
| 711 |
1/2✓ Branch 41 → 42 taken 8102 times.
✗ Branch 41 → 96 not taken.
|
8102 | fct->addParamAttr(i, llvm::Attribute::NoUndef); |
| 712 | |||
| 713 | // If the function should be imported as dll, add the dll attribute | ||
| 714 |
10/18✓ Branch 45 → 46 taken 1 time.
✓ Branch 45 → 52 taken 3876 times.
✓ Branch 48 → 49 taken 1 time.
✗ Branch 48 → 80 not taken.
✓ Branch 49 → 50 taken 1 time.
✗ Branch 49 → 80 not taken.
✗ Branch 50 → 51 not taken.
✓ Branch 50 → 52 taken 1 time.
✓ Branch 53 → 54 taken 1 time.
✓ Branch 53 → 55 taken 3876 times.
✓ Branch 55 → 56 taken 1 time.
✓ Branch 55 → 58 taken 3876 times.
✗ Branch 58 → 59 not taken.
✓ Branch 58 → 67 taken 3877 times.
✗ Branch 80 → 81 not taken.
✗ Branch 80 → 82 not taken.
✗ Branch 84 → 85 not taken.
✗ Branch 84 → 87 not taken.
|
3879 | if (node->attrs && node->attrs->attrLst->hasAttr(ATTR_CORE_LINKER_DLL)) |
| 715 | ✗ | if (node->attrs->attrLst->getAttrValueByName(ATTR_CORE_LINKER_DLL)->boolValue) | |
| 716 | ✗ | fct->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); | |
| 717 | |||
| 718 |
1/2✓ Branch 67 → 68 taken 3877 times.
✗ Branch 67 → 95 not taken.
|
7754 | return nullptr; |
| 719 | 3877 | } | |
| 720 | |||
| 721 | } // namespace spice::compiler | ||
| 722 |