objectemitter/llvm-object-emitter.spice
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | // Std imports | ||
| 4 | import "std/io/filepath"; | ||
| 5 | import "std/bindings/llvm/llvm" as llvm; | ||
| 6 | |||
| 7 | // Own imports | ||
| 8 | import "@/exception/fatal-error"; | ||
| 9 | import "@/compiler-pass"; | ||
| 10 | import "@/source-file"; | ||
| 11 | import "@/exception/compiler-error"; | ||
| 12 | import "@/global/global-resource-manager"; | ||
| 13 | import "@/objectemitter/abstract-object-emitter"; | ||
| 14 | |||
| 15 | /** | ||
| 16 | * Emits the object file or the assembly code of a module with the LLVM codegen backend | ||
| 17 | */ | ||
| 18 | public type LLVMObjectEmitter struct : IAbstractObjectEmitter { | ||
| 19 | compose public CompilerPass compilerPass | ||
| 20 | llvm::Module* module | ||
| 21 | } | ||
| 22 | |||
| 23 | 3497 | public p LLVMObjectEmitter.ctor(GlobalResourceManager& resourceManager, SourceFile* sourceFile) { | |
| 24 | 3497 | this.compilerPass = CompilerPass(resourceManager, sourceFile); | |
| 25 | 3497 | this.module = this.cliOptions.useLTO ? &resourceManager.ltoModule : &sourceFile.llvmModule; | |
| 26 | 3497 | } | |
| 27 | |||
| 28 | 3495 | public p LLVMObjectEmitter.emit(const FilePath& objectPath) { | |
| 29 | 3495 | const string objectPathString = objectPath.toString(); | |
| 30 | |||
| 31 | // Emit object file | ||
| 32 | 3495 | String errorMessage; | |
| 33 | 3495 | if this.sourceFile.targetMachine.emitToFileWithError(*this.module, objectPathString, llvm::LLVMCodeGenFileType::ObjectFile, errorMessage) { | |
| 34 | − | const String messageSuffix = "File '" + String(objectPathString) + "' could not be opened"; // LCOV_EXCL_LINE | |
| 35 | − | const CompilerError error = CompilerError(CompilerErrorType::CANT_OPEN_OUTPUT_FILE, messageSuffix.getRaw()); // LCOV_EXCL_LINE | |
| 36 | − | abortWithError(error.getMessage()); // LCOV_EXCL_LINE | |
| 37 | − | } // LCOV_EXCL_LINE | |
| 38 | 3495 | } | |
| 39 | |||
| 40 | 3383 | public p LLVMObjectEmitter.getASMString(String& output) { | |
| 41 | 3383 | if this.sourceFile.targetMachine.emitToString(*this.module, llvm::LLVMCodeGenFileType::AssemblyFile, output) { | |
| 42 | − | const CompilerError error = CompilerError(CompilerErrorType::WRONG_OUTPUT_TYPE, "Target machine can't emit a file of this type"); // LCOV_EXCL_LINE | |
| 43 | − | abortWithError(error.getMessage()); // LCOV_EXCL_LINE | |
| 44 | − | } // LCOV_EXCL_LINE | |
| 45 | 3383 | } | |
| 46 |