| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | // Copyright (c) 2021-2025 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "GlobalResourceManager.h" | ||
| 4 | |||
| 5 | #include <SourceFile.h> | ||
| 6 | #include <global/TypeRegistry.h> | ||
| 7 | #include <typechecker/FunctionManager.h> | ||
| 8 | #include <typechecker/StructManager.h> | ||
| 9 | #include <util/FileUtil.h> | ||
| 10 | |||
| 11 | #include <llvm/IR/Module.h> | ||
| 12 | #include <llvm/Support/ManagedStatic.h> | ||
| 13 | #include <llvm/Support/TargetSelect.h> | ||
| 14 | #include <llvm/TargetParser/Host.h> | ||
| 15 | #include <llvm/TargetParser/SubtargetFeature.h> | ||
| 16 | |||
| 17 | namespace spice::compiler { | ||
| 18 | |||
| 19 | 420 | GlobalResourceManager::GlobalResourceManager(const CliOptions &cliOptions) | |
| 20 | 5/10✓ Branch 4 → 5 taken 420 times. ✗ Branch 4 → 83 not taken. ✓ Branch 8 → 9 taken 420 times. ✗ Branch 8 → 77 not taken. ✓ Branch 9 → 10 taken 420 times. ✗ Branch 9 → 77 not taken. ✓ Branch 11 → 12 taken 420 times. ✗ Branch 11 → 73 not taken. ✓ Branch 14 → 15 taken 420 times. ✗ Branch 14 → 69 not taken. | 420 | : cliOptions(cliOptions), linker(cliOptions), cacheManager(cliOptions.cacheDir), runtimeModuleManager(*this) { | 
| 21 | // Initialize the required LLVM targets | ||
| 22 | 1/2✓ Branch 17 → 18 taken 420 times. ✗ Branch 17 → 21 not taken. | 420 | if (cliOptions.isNativeTarget) { | 
| 23 | 1/2✓ Branch 18 → 19 taken 420 times. ✗ Branch 18 → 67 not taken. | 420 | llvm::InitializeNativeTarget(); | 
| 24 | 1/2✓ Branch 19 → 20 taken 420 times. ✗ Branch 19 → 67 not taken. | 420 | llvm::InitializeNativeTargetAsmPrinter(); | 
| 25 | 1/2✓ Branch 20 → 25 taken 420 times. ✗ Branch 20 → 67 not taken. | 420 | llvm::InitializeNativeTargetAsmParser(); | 
| 26 | } else { // GCOV_EXCL_START | ||
| 27 | − | llvm::InitializeAllTargets(); | |
| 28 | − | llvm::InitializeAllTargetMCs(); | |
| 29 | − | llvm::InitializeAllAsmPrinters(); | |
| 30 | − | llvm::InitializeAllAsmParsers(); | |
| 31 | } // GCOV_EXCL_STOP | ||
| 32 | |||
| 33 | // Create cpu name and features strings | ||
| 34 | 1/2✓ Branch 25 → 26 taken 420 times. ✗ Branch 25 → 67 not taken. | 420 | cpuName = "generic"; | 
| 35 | 2/4✓ Branch 26 → 27 taken 420 times. ✗ Branch 26 → 50 not taken. ✗ Branch 27 → 28 not taken. ✓ Branch 27 → 50 taken 420 times. | 420 | if (cliOptions.isNativeTarget && cliOptions.useCPUFeatures) { | 
| 36 | // Retrieve native CPU name and the supported CPU features | ||
| 37 | ✗ | cpuName = llvm::sys::getHostCPUName(); | |
| 38 | ✗ | llvm::SubtargetFeatures features; | |
| 39 | ✗ | for (const auto &[feature, isEnabled] : llvm::sys::getHostCPUFeatures()) | |
| 40 | ✗ | features.AddFeature(feature, isEnabled); | |
| 41 | ✗ | cpuFeatures = features.getString(); | |
| 42 | ✗ | } | |
| 43 | |||
| 44 | // Create lto module | ||
| 45 | 2/2✓ Branch 50 → 51 taken 1 time. ✓ Branch 50 → 55 taken 419 times. | 420 | if (cliOptions.useLTO) | 
| 46 | 1/2✓ Branch 51 → 52 taken 1 time. ✗ Branch 51 → 66 not taken. | 1 | ltoModule = std::make_unique<llvm::Module>(LTO_FILE_NAME, ltoContext); | 
| 47 | 420 | } | |
| 48 | |||
| 49 | 420 | GlobalResourceManager::~GlobalResourceManager() { | |
| 50 | // Notify all global components to prepare to destroy | ||
| 51 | 420 | TypeRegistry::clear(); | |
| 52 | 420 | FunctionManager::cleanup(); | |
| 53 | 420 | StructManager::cleanup(); | |
| 54 | 420 | InterfaceManager::cleanup(); | |
| 55 | // Cleanup all LLVM statics | ||
| 56 | 420 | llvm::llvm_shutdown(); | |
| 57 | 420 | } | |
| 58 | |||
| 59 | 1345 | SourceFile *GlobalResourceManager::createSourceFile(SourceFile *parent, const std::string &depName, | |
| 60 | const std::filesystem::path &path, bool isStdFile) { | ||
| 61 | // Check if the source file was already added (e.g. by another source file that imports it) | ||
| 62 | 3/6✓ Branch 2 → 3 taken 1345 times. ✗ Branch 2 → 24 not taken. ✓ Branch 3 → 4 taken 1345 times. ✗ Branch 3 → 22 not taken. ✓ Branch 4 → 5 taken 1345 times. ✗ Branch 4 → 20 not taken. | 1345 | const std::string filePathStr = weakly_canonical(absolute(path)).string(); | 
| 63 | |||
| 64 | // Create the new source file if it does not exist yet | ||
| 65 | 3/4✓ Branch 7 → 8 taken 1345 times. ✗ Branch 7 → 32 not taken. ✓ Branch 8 → 9 taken 1145 times. ✓ Branch 8 → 15 taken 200 times. | 1345 | if (!sourceFiles.contains(filePathStr)) | 
| 66 | 3/6✓ Branch 9 → 10 taken 1145 times. ✗ Branch 9 → 30 not taken. ✓ Branch 10 → 11 taken 1145 times. ✗ Branch 10 → 28 not taken. ✓ Branch 11 → 12 taken 1145 times. ✗ Branch 11 → 26 not taken. | 1145 | sourceFiles.insert({filePathStr, std::make_unique<SourceFile>(*this, parent, depName, path, isStdFile)}); | 
| 67 | |||
| 68 | 1/2✓ Branch 15 → 16 taken 1345 times. ✗ Branch 15 → 32 not taken. | 2690 | return sourceFiles.at(filePathStr).get(); | 
| 69 | 1345 | } | |
| 70 | |||
| 71 | 917 | uint64_t GlobalResourceManager::getNextCustomTypeId() { return nextCustomTypeId++; } | |
| 72 | |||
| 73 | ✗ | size_t GlobalResourceManager::getTotalLineCount() const { | |
| 74 | ✗ | const auto acc = [](size_t sum, const auto &sourceFile) { return sum + FileUtil::getLineCount(sourceFile.second->filePath); }; | |
| 75 | ✗ | return std::accumulate(sourceFiles.begin(), sourceFiles.end(), 0, acc); | |
| 76 | } | ||
| 77 | |||
| 78 | } // namespace spice::compiler | ||
| 79 |