codegen.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. CodeGen(llvm::Module& module, llvm::StringRef triple,
  13. llvm::raw_pwrite_stream& error_stream,
  14. llvm::raw_pwrite_stream& output_stream)
  15. : Module(module),
  16. output_stream_(output_stream),
  17. error_stream_(error_stream),
  18. target_triple(triple){};
  19. // Generates the object code file.
  20. // Returns false in case of failure, and any information about the failure is
  21. // printed to the error stream.
  22. auto GenerateObjectCode() -> 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. auto PrintAssembly() -> bool;
  27. private:
  28. llvm::Module& Module;
  29. llvm::raw_pwrite_stream& output_stream_;
  30. llvm::raw_pwrite_stream& error_stream_;
  31. llvm::StringRef target_triple;
  32. // Creates the target machine for triple.
  33. // Returns nullptr in case of failure, and any information about the failure
  34. // is printed to the error stream.
  35. auto CreateTargetMachine() -> std::unique_ptr<llvm::TargetMachine>;
  36. // Using the llvm pass emits either assembly or object code to dest.
  37. // Returns false in case of failure, and any information about the failure is
  38. // printed to the error stream.
  39. auto EmitCode(llvm::raw_pwrite_stream& dest,
  40. llvm::TargetMachine* target_machine,
  41. llvm::CodeGenFileType file_type) -> bool;
  42. };
  43. } // namespace Carbon
  44. #endif // CARBON_TOOLCHAIN_CODEGEN_CODEGEN_H_