function_context.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_LOWER_FUNCTION_CONTEXT_H_
  5. #define CARBON_TOOLCHAIN_LOWER_FUNCTION_CONTEXT_H_
  6. #include "llvm/IR/IRBuilder.h"
  7. #include "llvm/IR/LLVMContext.h"
  8. #include "llvm/IR/Module.h"
  9. #include "toolchain/lower/file_context.h"
  10. #include "toolchain/sem_ir/file.h"
  11. #include "toolchain/sem_ir/node.h"
  12. namespace Carbon::Lower {
  13. // Context and shared functionality for lowering handlers that produce an
  14. // `llvm::Function` definition.
  15. class FunctionContext {
  16. public:
  17. explicit FunctionContext(FileContext& file_context, llvm::Function* function,
  18. llvm::raw_ostream* vlog_stream);
  19. // Returns a basic block corresponding to the start of the given semantics
  20. // block, and enqueues it for emission.
  21. auto GetBlock(SemIR::NodeBlockId block_id) -> llvm::BasicBlock*;
  22. // If we have not yet allocated a `BasicBlock` for this `block_id`, set it to
  23. // `block`, and enqueue `block_id` for emission. Returns whether we set the
  24. // block.
  25. auto TryToReuseBlock(SemIR::NodeBlockId block_id, llvm::BasicBlock* block)
  26. -> bool;
  27. // Builds LLVM IR for the sequence of instructions in `block_id`.
  28. auto LowerBlock(SemIR::NodeBlockId block_id) -> void;
  29. // Returns a phi node corresponding to the block argument of the given basic
  30. // block.
  31. auto GetBlockArg(SemIR::NodeBlockId block_id, SemIR::TypeId type_id)
  32. -> llvm::PHINode*;
  33. // Returns a local (versus global) value for the given node.
  34. auto GetLocal(SemIR::NodeId node_id) -> llvm::Value* {
  35. // All builtins are types, with the same empty lowered value.
  36. if (node_id.index < SemIR::BuiltinKind::ValidCount) {
  37. return GetTypeAsValue();
  38. }
  39. auto it = locals_.find(node_id);
  40. CARBON_CHECK(it != locals_.end()) << "Missing local: " << node_id;
  41. return it->second;
  42. }
  43. // Sets the value for the given node.
  44. auto SetLocal(SemIR::NodeId node_id, llvm::Value* value) {
  45. bool added = locals_.insert({node_id, value}).second;
  46. CARBON_CHECK(added) << "Duplicate local insert: " << node_id;
  47. }
  48. // Gets a callable's function.
  49. auto GetFunction(SemIR::FunctionId function_id) -> llvm::Function* {
  50. return file_context_->GetFunction(function_id);
  51. }
  52. // Returns a lowered type for the given type_id.
  53. auto GetType(SemIR::TypeId type_id) -> llvm::Type* {
  54. return file_context_->GetType(type_id);
  55. }
  56. // Returns a lowered value to use for a value of type `type`.
  57. auto GetTypeAsValue() -> llvm::Value* {
  58. return file_context_->GetTypeAsValue();
  59. }
  60. // Create a synthetic block that corresponds to no SemIR::NodeBlockId. Such
  61. // a block should only ever have a single predecessor, and is used when we
  62. // need multiple `llvm::BasicBlock`s to model the linear control flow in a
  63. // single SemIR::File block.
  64. auto CreateSyntheticBlock() -> llvm::BasicBlock*;
  65. // Determine whether block is the most recently created synthetic block.
  66. auto IsCurrentSyntheticBlock(llvm::BasicBlock* block) -> bool {
  67. return synthetic_block_ == block;
  68. }
  69. // After emitting an initializer `init_id`, finishes performing the
  70. // initialization of `dest_id` from that initializer. This is a no-op if the
  71. // initialization was performed in-place, and otherwise performs a store or a
  72. // copy.
  73. auto FinishInitialization(SemIR::TypeId type_id, SemIR::NodeId dest_id,
  74. SemIR::NodeId init_id) -> void;
  75. auto llvm_context() -> llvm::LLVMContext& {
  76. return file_context_->llvm_context();
  77. }
  78. auto llvm_module() -> llvm::Module& { return file_context_->llvm_module(); }
  79. auto builder() -> llvm::IRBuilder<>& { return builder_; }
  80. auto semantics_ir() -> const SemIR::File& {
  81. return file_context_->semantics_ir();
  82. }
  83. private:
  84. // Emits a value copy for type `type_id` from `source_id` to `dest_id`.
  85. // `source_id` must produce a value representation for `type_id`, and
  86. // `dest_id` must be a pointer to a `type_id` object.
  87. auto CopyValue(SemIR::TypeId type_id, SemIR::NodeId source_id,
  88. SemIR::NodeId dest_id) -> void;
  89. // Context for the overall lowering process.
  90. FileContext* file_context_;
  91. // The IR function we're generating.
  92. llvm::Function* function_;
  93. llvm::IRBuilder<> builder_;
  94. // The optional vlog stream.
  95. llvm::raw_ostream* vlog_stream_;
  96. // Maps a function's SemIR::File blocks to lowered blocks.
  97. llvm::DenseMap<SemIR::NodeBlockId, llvm::BasicBlock*> blocks_;
  98. // The synthetic block we most recently created. May be null if there is no
  99. // such block.
  100. llvm::BasicBlock* synthetic_block_ = nullptr;
  101. // Maps a function's SemIR::File nodes to lowered values.
  102. // TODO: Handle nested scopes. Right now this is just cleared at the end of
  103. // every block.
  104. llvm::DenseMap<SemIR::NodeId, llvm::Value*> locals_;
  105. };
  106. // Declare handlers for each SemIR::File node.
  107. #define CARBON_SEMANTICS_NODE_KIND(Name) \
  108. auto Handle##Name(FunctionContext& context, SemIR::NodeId node_id, \
  109. SemIR::Name node) \
  110. ->void;
  111. #include "toolchain/sem_ir/node_kind.def"
  112. } // namespace Carbon::Lower
  113. #endif // CARBON_TOOLCHAIN_LOWER_FUNCTION_CONTEXT_H_