lowering_context.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_CONTEXT_H_
  5. #define CARBON_TOOLCHAIN_LOWERING_LOWERING_CONTEXT_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. // Context and shared functionality for lowering handlers.
  13. class LoweringContext {
  14. public:
  15. explicit LoweringContext(llvm::LLVMContext& llvm_context,
  16. llvm::StringRef module_name,
  17. const SemanticsIR& semantics_ir,
  18. llvm::raw_ostream* vlog_stream);
  19. // Lowers the SemanticsIR to LLVM IR. Should only be called once, and handles
  20. // the main execution loop.
  21. auto Run() -> std::unique_ptr<llvm::Module>;
  22. auto HasLoweredNode(SemanticsNodeId node_id) -> bool {
  23. return lowered_nodes_[node_id.index];
  24. }
  25. // Returns a lowered type for the given type_id.
  26. auto GetType(SemanticsTypeId type_id) -> llvm::Type* {
  27. // Neither TypeType nor InvalidType should be passed in.
  28. CARBON_CHECK(type_id.index >= 0) << type_id;
  29. return lowered_types_[type_id.index];
  30. }
  31. // Returns a value for the given node.
  32. auto GetLoweredNodeAsValue(SemanticsNodeId node_id) -> llvm::Value* {
  33. CARBON_CHECK(lowered_nodes_[node_id.index]) << node_id;
  34. return lowered_nodes_[node_id.index];
  35. }
  36. // Sets the value for the given node.
  37. auto SetLoweredNodeAsValue(SemanticsNodeId node_id, llvm::Value* value) {
  38. CARBON_CHECK(!lowered_nodes_[node_id.index]) << node_id;
  39. lowered_nodes_[node_id.index] = value;
  40. }
  41. // Gets a callable's function.
  42. auto GetLoweredCallable(SemanticsCallableId callable_id) -> llvm::Function* {
  43. CARBON_CHECK(lowered_callables_[callable_id.index] != nullptr)
  44. << callable_id;
  45. return lowered_callables_[callable_id.index];
  46. }
  47. // Sets a callable's function.
  48. auto SetLoweredCallable(SemanticsCallableId callable_id,
  49. llvm::Function* function) {
  50. CARBON_CHECK(lowered_callables_[callable_id.index] == nullptr)
  51. << callable_id;
  52. lowered_callables_[callable_id.index] = function;
  53. }
  54. auto llvm_context() -> llvm::LLVMContext& { return *llvm_context_; }
  55. auto llvm_module() -> llvm::Module& { return *llvm_module_; }
  56. auto builder() -> llvm::IRBuilder<>& { return builder_; }
  57. auto semantics_ir() -> const SemanticsIR& { return *semantics_ir_; }
  58. auto todo_blocks() -> llvm::SmallVector<
  59. std::pair<llvm::BasicBlock*, SemanticsNodeBlockId>>& {
  60. return todo_blocks_;
  61. }
  62. private:
  63. // Runs lowering for a block.
  64. auto LowerBlock(SemanticsNodeBlockId block_id) -> void;
  65. // Builds the type for the given node, which should then be cached by the
  66. // caller.
  67. auto BuildLoweredNodeAsType(SemanticsNodeId node_id) -> llvm::Type*;
  68. // State for building the LLVM IR.
  69. llvm::LLVMContext* llvm_context_;
  70. std::unique_ptr<llvm::Module> llvm_module_;
  71. llvm::IRBuilder<> builder_;
  72. // The input Semantics IR.
  73. const SemanticsIR* const semantics_ir_;
  74. // The optional vlog stream.
  75. llvm::raw_ostream* vlog_stream_;
  76. // Blocks which we've observed and need to lower.
  77. llvm::SmallVector<std::pair<llvm::BasicBlock*, SemanticsNodeBlockId>>
  78. todo_blocks_;
  79. // Maps nodes in SemanticsIR to a lowered value. This will have one entry per
  80. // node, and will be non-null when lowered. It's expected to be sparse during
  81. // execution because while expressions will have entries, statements won't.
  82. // TODO: This is transitioning to only track global and local values, rather
  83. // than one large map for all nodes.
  84. llvm::SmallVector<llvm::Value*> lowered_nodes_;
  85. // Maps callables to lowered functions. Semantics treats callables as the
  86. // canonical form of a function, so lowering needs to do the same.
  87. llvm::SmallVector<llvm::Function*> lowered_callables_;
  88. // Provides lowered versions of types.
  89. llvm::SmallVector<llvm::Type*> lowered_types_;
  90. };
  91. // Declare handlers for each SemanticsIR node.
  92. #define CARBON_SEMANTICS_NODE_KIND(Name) \
  93. auto LoweringHandle##Name(LoweringContext& context, SemanticsNodeId node_id, \
  94. SemanticsNode node) \
  95. ->void;
  96. #include "toolchain/semantics/semantics_node_kind.def"
  97. } // namespace Carbon
  98. #endif // CARBON_TOOLCHAIN_LOWERING_LOWERING_CONTEXT_H_