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