codegen.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. namespace Carbon {
  9. class CodeGen {
  10. public:
  11. static auto Create(llvm::Module& module, llvm::StringRef target_triple,
  12. llvm::raw_pwrite_stream& errors) -> std::optional<CodeGen>;
  13. // Generates the object code file.
  14. // Returns false in case of failure, and any information about the failure is
  15. // printed to the error stream.
  16. //
  17. // Note that unlike the error stream, this requires a `pwrite` stream to allow
  18. // patching the output.
  19. auto EmitObject(llvm::raw_pwrite_stream& out) -> bool;
  20. // Prints the assembly to stdout.
  21. // Returns false in case of failure, and any information about the failure is
  22. // printed to the error stream.
  23. //
  24. // Note that unlike the error stream, this requires a `pwrite` stream to allow
  25. // patching the output.
  26. auto EmitAssembly(llvm::raw_pwrite_stream& out) -> bool;
  27. private:
  28. explicit CodeGen(llvm::Module& module, llvm::raw_pwrite_stream& errors)
  29. : module_(module), errors_(errors) {}
  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::raw_pwrite_stream& errors_;
  37. std::unique_ptr<llvm::TargetMachine> target_machine_;
  38. };
  39. } // namespace Carbon
  40. #endif // CARBON_TOOLCHAIN_CODEGEN_CODEGEN_H_