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