Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright (c) 2021-2025 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 | 751 | ObjectEmitter::ObjectEmitter(GlobalResourceManager &resourceManager, SourceFile *sourceFile) | |
15 | : CompilerPass(resourceManager, sourceFile), | ||
16 |
2/2✓ Branch 0 (3→4) taken 1 times.
✓ Branch 1 (3→5) taken 750 times.
|
751 | module(cliOptions.useLTO ? *resourceManager.ltoModule : *sourceFile->llvmModule) {} |
17 | |||
18 | 751 | void ObjectEmitter::emit(const std::filesystem::path &objectPath) const { | |
19 |
1/2✓ Branch 0 (2→3) taken 751 times.
✗ Branch 1 (2→58) not taken.
|
751 | const std::string objectPathString = objectPath.string(); |
20 | |||
21 | // Open file output stream | ||
22 | 751 | std::error_code errorCode; | |
23 |
1/2✓ Branch 0 (5→6) taken 751 times.
✗ Branch 1 (5→33) not taken.
|
751 | llvm::raw_fd_ostream stream(objectPathString, errorCode, llvm::sys::fs::OF_None); |
24 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→15) taken 751 times.
|
751 | if (errorCode) |
25 | − | throw CompilerError(CANT_OPEN_OUTPUT_FILE, "File '" + objectPathString + "' could not be opened"); // GCOV_EXCL_LINE | |
26 | |||
27 |
1/2✓ Branch 0 (15→16) taken 751 times.
✗ Branch 1 (15→54) not taken.
|
751 | llvm::legacy::PassManager passManager; |
28 | 751 | constexpr auto fileType = llvm::CodeGenFileType::ObjectFile; | |
29 |
2/4✓ Branch 0 (17→18) taken 751 times.
✗ Branch 1 (17→52) not taken.
✗ Branch 2 (18→19) not taken.
✓ Branch 3 (18→27) taken 751 times.
|
751 | 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 0 (27→28) taken 751 times.
✗ Branch 1 (27→52) not taken.
|
751 | passManager.run(module); |
34 |
1/2✓ Branch 0 (28→29) taken 751 times.
✗ Branch 1 (28→52) not taken.
|
751 | stream.flush(); |
35 | 751 | } | |
36 | |||
37 | 751 | void ObjectEmitter::getASMString(std::string &output) const { | |
38 |
1/2✓ Branch 0 (2→3) taken 751 times.
✗ Branch 1 (2→33) not taken.
|
751 | RawStringOStream ostream(output); |
39 |
1/2✓ Branch 0 (3→4) taken 751 times.
✗ Branch 1 (3→31) not taken.
|
751 | llvm::legacy::PassManager passManager; |
40 | 751 | constexpr auto fileType = llvm::CodeGenFileType::AssemblyFile; | |
41 |
2/4✓ Branch 0 (5→6) taken 751 times.
✗ Branch 1 (5→29) not taken.
✗ Branch 2 (6→7) not taken.
✓ Branch 3 (6→15) taken 751 times.
|
751 | 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 0 (15→16) taken 751 times.
✗ Branch 1 (15→29) not taken.
|
751 | passManager.run(module); |
46 |
1/2✓ Branch 0 (16→17) taken 751 times.
✗ Branch 1 (16→29) not taken.
|
751 | ostream.flush(); |
47 | 751 | } | |
48 | |||
49 | } // namespace spice::compiler | ||
50 |