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 <cstdint>
  7. #include "llvm/IR/Module.h"
  8. #include "llvm/Target/TargetMachine.h"
  9. namespace Carbon {
  10. class CodeGen {
  11. public:
  12. static auto Create(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. explicit CodeGen(llvm::Module& module, llvm::raw_pwrite_stream& errors)
  30. : module_(module), errors_(errors) {}
  31. // Using the llvm pass emits either assembly or object code to dest.
  32. // Returns false in case of failure, and any information about the failure is
  33. // printed to the error stream.
  34. auto EmitCode(llvm::raw_pwrite_stream& out, llvm::CodeGenFileType file_type)
  35. -> bool;
  36. llvm::Module& module_;
  37. llvm::raw_pwrite_stream& errors_;
  38. std::unique_ptr<llvm::TargetMachine> target_machine_;
  39. };
  40. } // namespace Carbon
  41. #endif // CARBON_TOOLCHAIN_CODEGEN_CODEGEN_H_