codegen.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_CODEGEN_CODEGEN_H_
  5. #define CARBON_TOOLCHAIN_CODEGEN_CODEGEN_H_
  6. #include "llvm/IR/Module.h"
  7. #include "llvm/Target/TargetMachine.h"
  8. #include "toolchain/diagnostics/consumer.h"
  9. #include "toolchain/diagnostics/file_diagnostics.h"
  10. namespace Carbon {
  11. class CodeGen {
  12. public:
  13. // `module`, `target_machine`, and `consumer` must not be null.
  14. explicit CodeGen(llvm::Module* module, llvm::TargetMachine* target_machine,
  15. Diagnostics::Consumer* consumer)
  16. : module_(module), target_machine_(target_machine), emitter_(consumer) {}
  17. // Generates the object code file.
  18. // Returns false in case of failure, and any information about the failure is
  19. // printed to the error stream.
  20. //
  21. // Note this requires a `pwrite` stream to allow patching the output.
  22. auto EmitObject(llvm::raw_pwrite_stream& out) -> bool;
  23. // Prints the assembly to stdout.
  24. // Returns false in case of failure, and any information about the failure is
  25. // printed to the error stream.
  26. //
  27. // Note this requires a `pwrite` stream to allow patching the output.
  28. auto EmitAssembly(llvm::raw_pwrite_stream& out) -> bool;
  29. private:
  30. // Using the llvm pass emits either assembly or object code to dest.
  31. // Returns false in case of failure, and any information about the failure is
  32. // printed to the error stream.
  33. auto EmitCode(llvm::raw_pwrite_stream& out, llvm::CodeGenFileType file_type)
  34. -> bool;
  35. llvm::Module* module_;
  36. llvm::TargetMachine* target_machine_;
  37. // The emitter for diagnostics.
  38. Diagnostics::FileEmitter emitter_;
  39. };
  40. } // namespace Carbon
  41. #endif // CARBON_TOOLCHAIN_CODEGEN_CODEGEN_H_