Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright (c) 2021-2024 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 | |||
16 | namespace spice::compiler { | ||
17 | |||
18 | 390 | GlobalResourceManager::GlobalResourceManager(const CliOptions &cliOptions) | |
19 |
5/10✓ Branch 3 taken 390 times.
✗ Branch 4 not taken.
✓ Branch 9 taken 390 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 390 times.
✗ Branch 13 not taken.
✓ Branch 16 taken 390 times.
✗ Branch 17 not taken.
✓ Branch 21 taken 390 times.
✗ Branch 22 not taken.
|
390 | : cliOptions(cliOptions), linker(cliOptions), cacheManager(cliOptions.cacheDir), runtimeModuleManager(*this) { |
20 | // Initialize the required LLVM targets | ||
21 |
1/2✓ Branch 0 taken 390 times.
✗ Branch 1 not taken.
|
390 | if (cliOptions.isNativeTarget) { |
22 |
1/2✓ Branch 1 taken 390 times.
✗ Branch 2 not taken.
|
390 | llvm::InitializeNativeTarget(); |
23 |
1/2✓ Branch 1 taken 390 times.
✗ Branch 2 not taken.
|
390 | llvm::InitializeNativeTargetAsmPrinter(); |
24 |
1/2✓ Branch 1 taken 390 times.
✗ Branch 2 not taken.
|
390 | llvm::InitializeNativeTargetAsmParser(); |
25 | } else { // GCOV_EXCL_START | ||
26 | − | llvm::InitializeAllTargets(); | |
27 | − | llvm::InitializeAllTargetMCs(); | |
28 | − | llvm::InitializeAllAsmPrinters(); | |
29 | − | llvm::InitializeAllAsmParsers(); | |
30 | } // GCOV_EXCL_STOP | ||
31 | |||
32 | // Create cpu name and features strings | ||
33 |
1/2✓ Branch 1 taken 390 times.
✗ Branch 2 not taken.
|
390 | cpuName = "generic"; |
34 |
1/2✓ Branch 1 taken 390 times.
✗ Branch 2 not taken.
|
390 | std::stringstream featureString; |
35 |
2/4✓ Branch 0 taken 390 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 390 times.
|
390 | if (cliOptions.isNativeTarget && cliOptions.useCPUFeatures) { |
36 | // Retrieve native CPU name and the supported CPU features | ||
37 | ✗ | cpuName = llvm::sys::getHostCPUName(); | |
38 | ✗ | for (const auto &[name, enabled] : llvm::sys::getHostCPUFeatures()) { | |
39 | ✗ | if (featureString.rdbuf()->in_avail() > 0) | |
40 | ✗ | featureString << ','; | |
41 | ✗ | featureString << (enabled ? '+' : '-') << name.str(); | |
42 | ✗ | } | |
43 | } | ||
44 |
1/2✓ Branch 1 taken 390 times.
✗ Branch 2 not taken.
|
390 | cpuFeatures = featureString.str(); |
45 | |||
46 | // Create lto module | ||
47 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 389 times.
|
390 | if (cliOptions.useLTO) |
48 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | ltoModule = std::make_unique<llvm::Module>(LTO_FILE_NAME, ltoContext); |
49 | 390 | } | |
50 | |||
51 | 390 | GlobalResourceManager::~GlobalResourceManager() { | |
52 | // Cleanup all statics | ||
53 | 390 | TypeRegistry::clear(); | |
54 | 390 | FunctionManager::clear(); | |
55 | 390 | StructManager::clear(); | |
56 | 390 | InterfaceManager::clear(); | |
57 | // Cleanup all LLVM statics | ||
58 | 390 | llvm::llvm_shutdown(); | |
59 | 390 | } | |
60 | |||
61 | 1028 | SourceFile *GlobalResourceManager::createSourceFile(SourceFile *parent, const std::string &depName, | |
62 | const std::filesystem::path &path, bool isStdFile) { | ||
63 | // Check if the source file was already added (e.g. by another source file that imports it) | ||
64 |
3/6✓ Branch 1 taken 1028 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1028 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1028 times.
✗ Branch 8 not taken.
|
1028 | const std::string filePathStr = weakly_canonical(absolute(path)).string(); |
65 | |||
66 | // Create the new source file if it does not exist yet | ||
67 |
3/4✓ Branch 1 taken 1028 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 905 times.
✓ Branch 4 taken 123 times.
|
1028 | if (!sourceFiles.contains(filePathStr)) |
68 |
3/6✓ Branch 1 taken 905 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 905 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 905 times.
✗ Branch 8 not taken.
|
905 | sourceFiles.insert({filePathStr, std::make_unique<SourceFile>(*this, parent, depName, path, isStdFile)}); |
69 | |||
70 |
1/2✓ Branch 1 taken 1028 times.
✗ Branch 2 not taken.
|
2056 | return sourceFiles.at(filePathStr).get(); |
71 | 1028 | } | |
72 | |||
73 | 697 | uint64_t GlobalResourceManager::getNextCustomTypeId() { return nextCustomTypeId++; } | |
74 | |||
75 | ✗ | size_t GlobalResourceManager::getTotalLineCount() const { | |
76 | ✗ | const auto acc = [](size_t sum, const auto &sourceFile) { return sum + FileUtil::getLineCount(sourceFile.second->filePath); }; | |
77 | ✗ | return std::accumulate(sourceFiles.begin(), sourceFiles.end(), 0, acc); | |
78 | } | ||
79 | |||
80 | } // namespace spice::compiler | ||
81 |