Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright (c) 2021-2024 ChilliBits. All rights reserved. | ||
2 | |||
3 | #include "ObjectEmitter.h" | ||
4 | |||
5 | #include <global/GlobalResourceManager.h> | ||
6 | #include <util/FileUtil.h> | ||
7 | #include <util/RawStringOStream.h> | ||
8 | |||
9 | #include <llvm/IR/LegacyPassManager.h> | ||
10 | #include <llvm/Support/FileSystem.h> | ||
11 | |||
12 | namespace spice::compiler { | ||
13 | |||
14 | 641 | ObjectEmitter::ObjectEmitter(GlobalResourceManager &resourceManager, SourceFile *sourceFile) | |
15 | : CompilerPass(resourceManager, sourceFile), | ||
16 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 640 times.
|
641 | module(cliOptions.useLTO ? *resourceManager.ltoModule : *sourceFile->llvmModule) {} |
17 | |||
18 | 641 | void ObjectEmitter::emit(const std::filesystem::path &objectPath) const { | |
19 |
1/2✓ Branch 1 taken 641 times.
✗ Branch 2 not taken.
|
641 | const std::string objectPathString = objectPath.string(); |
20 | |||
21 | // Open file output stream | ||
22 | 641 | std::error_code errorCode; | |
23 |
1/2✓ Branch 2 taken 641 times.
✗ Branch 3 not taken.
|
641 | llvm::raw_fd_ostream stream(objectPathString, errorCode, llvm::sys::fs::OF_None); |
24 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 641 times.
|
641 | if (errorCode) |
25 | − | throw CompilerError(CANT_OPEN_OUTPUT_FILE, "File '" + objectPathString + "' could not be opened"); // GCOV_EXCL_LINE | |
26 | |||
27 |
1/2✓ Branch 1 taken 641 times.
✗ Branch 2 not taken.
|
641 | llvm::legacy::PassManager passManager; |
28 | 641 | constexpr auto fileType = llvm::CodeGenFileType::ObjectFile; | |
29 |
2/4✓ Branch 2 taken 641 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 641 times.
|
641 | if (sourceFile->targetMachine->addPassesToEmitFile(passManager, stream, nullptr, fileType, cliOptions.disableVerifier)) |
30 | − | throw CompilerError(WRONG_OUTPUT_TYPE, "Target machine can't emit a file of this type"); // GCOV_EXCL_LINE | |
31 | |||
32 | // Emit object file | ||
33 |
1/2✓ Branch 1 taken 641 times.
✗ Branch 2 not taken.
|
641 | passManager.run(module); |
34 |
1/2✓ Branch 1 taken 641 times.
✗ Branch 2 not taken.
|
641 | stream.flush(); |
35 | 641 | } | |
36 | |||
37 | 641 | void ObjectEmitter::getASMString(std::string &output) const { | |
38 |
1/2✓ Branch 1 taken 641 times.
✗ Branch 2 not taken.
|
641 | RawStringOStream ostream(output); |
39 |
1/2✓ Branch 1 taken 641 times.
✗ Branch 2 not taken.
|
641 | llvm::legacy::PassManager passManager; |
40 | 641 | constexpr auto fileType = llvm::CodeGenFileType::AssemblyFile; | |
41 |
2/4✓ Branch 2 taken 641 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 641 times.
|
641 | if (sourceFile->targetMachine->addPassesToEmitFile(passManager, ostream, nullptr, fileType, cliOptions.disableVerifier)) |
42 | − | throw CompilerError(WRONG_OUTPUT_TYPE, "Target machine can't emit a file of this type"); // GCOV_EXCL_LINE | |
43 | |||
44 | // Emit object file | ||
45 |
1/2✓ Branch 1 taken 641 times.
✗ Branch 2 not taken.
|
641 | passManager.run(module); |
46 |
1/2✓ Branch 1 taken 641 times.
✗ Branch 2 not taken.
|
641 | ostream.flush(); |
47 | 641 | } | |
48 | |||
49 | } // namespace spice::compiler | ||
50 |