lowering.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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_LOWERING_LOWERING_H_
  5. #define CARBON_TOOLCHAIN_LOWERING_LOWERING_H_
  6. #include "llvm/IR/IRBuilder.h"
  7. #include "llvm/IR/LLVMContext.h"
  8. #include "llvm/IR/Module.h"
  9. #include "toolchain/semantics/semantics_ir.h"
  10. #include "toolchain/semantics/semantics_node.h"
  11. namespace Carbon {
  12. // Use LowerToLLVM rather than calling this directly.
  13. //
  14. // This carries state for lowering. `Run()` should only be called once, and
  15. // handles the main execution.
  16. class Lowering {
  17. public:
  18. explicit Lowering(llvm::LLVMContext& llvm_context,
  19. llvm::StringRef module_name,
  20. const SemanticsIR& semantics_ir);
  21. // Lowers the SemanticsIR to LLVM IR.
  22. auto Run() -> std::unique_ptr<llvm::Module>;
  23. private:
  24. // Provides DenseMapInfo for SemanticsNodeId.
  25. struct SemanticsNodeIdMapInfo {
  26. static inline auto getEmptyKey() -> SemanticsNodeId {
  27. return SemanticsNodeId(llvm::DenseMapInfo<int32_t>::getEmptyKey());
  28. }
  29. static inline auto getTombstoneKey() -> SemanticsNodeId {
  30. return SemanticsNodeId(llvm::DenseMapInfo<int32_t>::getTombstoneKey());
  31. }
  32. static auto getHashValue(const SemanticsNodeId& val) -> unsigned {
  33. return llvm::DenseMapInfo<int32_t>::getHashValue(val.index);
  34. }
  35. static auto isEqual(const SemanticsNodeId& lhs, const SemanticsNodeId& rhs)
  36. -> bool {
  37. return lhs == rhs;
  38. }
  39. };
  40. // Declare handlers for each SemanticsIR node.
  41. #define CARBON_SEMANTICS_NODE_KIND(Name) \
  42. auto Handle##Name##Node(SemanticsNodeId node_id, SemanticsNode node)->void;
  43. #include "toolchain/semantics/semantics_node_kind.def"
  44. // Runs lowering for a block.
  45. auto LowerBlock(SemanticsNodeBlockId block_id) -> void;
  46. // State for building the LLVM IR.
  47. llvm::LLVMContext* llvm_context_;
  48. std::unique_ptr<llvm::Module> llvm_module_;
  49. llvm::IRBuilder<> builder_;
  50. // The input Semantics IR.
  51. const SemanticsIR* const semantics_ir_;
  52. // Blocks which we've observed and need to lower.
  53. llvm::SmallVector<std::pair<llvm::BasicBlock*, SemanticsNodeBlockId>>
  54. todo_blocks_;
  55. // A mapping of nodes to designated values.
  56. // TODO: It might be worth considering other approaches, or at least if we
  57. // stick with this we'll probably want to clean up nodes as they leave scope.
  58. // However, for now, it's handy to make things work.
  59. llvm::DenseMap<SemanticsNodeId, llvm::Value*, SemanticsNodeIdMapInfo>
  60. node_values_;
  61. };
  62. } // namespace Carbon
  63. #endif // CARBON_TOOLCHAIN_LOWERING_LOWERING_H_