lowering_function_context.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_FUNCTION_CONTEXT_H_
  5. #define CARBON_TOOLCHAIN_LOWERING_LOWERING_FUNCTION_CONTEXT_H_
  6. #include "llvm/IR/IRBuilder.h"
  7. #include "llvm/IR/LLVMContext.h"
  8. #include "llvm/IR/Module.h"
  9. #include "toolchain/lowering/lowering_context.h"
  10. #include "toolchain/semantics/semantics_ir.h"
  11. #include "toolchain/semantics/semantics_node.h"
  12. namespace Carbon {
  13. // Context and shared functionality for lowering handlers that produce an
  14. // `llvm::Function` definition.
  15. class LoweringFunctionContext {
  16. public:
  17. explicit LoweringFunctionContext(LoweringContext& lowering_context,
  18. llvm::Function* function);
  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. // Returns a phi node corresponding to the block argument of the given basic
  28. // block.
  29. auto GetBlockArg(SemIR::NodeBlockId block_id, SemIR::TypeId type_id)
  30. -> llvm::PHINode*;
  31. // Returns a local (versus global) value for the given node.
  32. auto GetLocal(SemIR::NodeId node_id) -> llvm::Value* {
  33. // All builtins are types, with the same empty lowered value.
  34. if (node_id.index < SemIR::BuiltinKind::ValidCount) {
  35. return GetTypeAsValue();
  36. }
  37. auto it = locals_.find(node_id);
  38. CARBON_CHECK(it != locals_.end()) << "Missing local: " << node_id;
  39. return it->second;
  40. }
  41. // Returns a local (versus global) value for the given node in loaded state.
  42. // Loads will only be inserted on an as-needed basis.
  43. auto GetLocalLoaded(SemIR::NodeId node_id) -> llvm::Value*;
  44. // Sets the value for the given node.
  45. auto SetLocal(SemIR::NodeId node_id, llvm::Value* value) {
  46. bool added = locals_.insert({node_id, value}).second;
  47. CARBON_CHECK(added) << "Duplicate local insert: " << node_id;
  48. }
  49. // Returns the requested index into val based on whether val is a pointer
  50. // type.
  51. auto GetIndexFromStructOrArray(llvm::Type* llvm_type, llvm::Value* val,
  52. unsigned idx, const llvm::Twine& name)
  53. -> llvm::Value* {
  54. return val->getType()->isPointerTy()
  55. ? builder().CreateStructGEP(llvm_type, val, idx, name)
  56. : builder().CreateExtractValue(val, idx, name);
  57. }
  58. // Gets a callable's function.
  59. auto GetFunction(SemIR::FunctionId function_id) -> llvm::Function* {
  60. return lowering_context_->GetFunction(function_id);
  61. }
  62. // Returns a lowered type for the given type_id.
  63. auto GetType(SemIR::TypeId type_id) -> llvm::Type* {
  64. return lowering_context_->GetType(type_id);
  65. }
  66. // Returns a lowered value to use for a value of type `type`.
  67. auto GetTypeAsValue() -> llvm::Value* {
  68. return lowering_context_->GetTypeAsValue();
  69. }
  70. // Create a synthetic block that corresponds to no SemIR::NodeBlockId. Such
  71. // a block should only ever have a single predecessor, and is used when we
  72. // need multiple `llvm::BasicBlock`s to model the linear control flow in a
  73. // single SemIR::File block.
  74. auto CreateSyntheticBlock() -> llvm::BasicBlock*;
  75. // Determine whether block is the most recently created synthetic block.
  76. auto IsCurrentSyntheticBlock(llvm::BasicBlock* block) -> bool {
  77. return synthetic_block_ == block;
  78. }
  79. auto llvm_context() -> llvm::LLVMContext& {
  80. return lowering_context_->llvm_context();
  81. }
  82. auto llvm_module() -> llvm::Module& {
  83. return lowering_context_->llvm_module();
  84. }
  85. auto builder() -> llvm::IRBuilder<>& { return builder_; }
  86. auto semantics_ir() -> const SemIR::File& {
  87. return lowering_context_->semantics_ir();
  88. }
  89. private:
  90. // Context for the overall lowering process.
  91. LoweringContext* lowering_context_;
  92. // The IR function we're generating.
  93. llvm::Function* function_;
  94. llvm::IRBuilder<> builder_;
  95. // Maps a function's SemIR::File blocks to lowered blocks.
  96. llvm::DenseMap<SemIR::NodeBlockId, llvm::BasicBlock*> blocks_;
  97. // The synthetic block we most recently created. May be null if there is no
  98. // such block.
  99. llvm::BasicBlock* synthetic_block_ = nullptr;
  100. // Maps a function's SemIR::File nodes to lowered values.
  101. // TODO: Handle nested scopes. Right now this is just cleared at the end of
  102. // every block.
  103. llvm::DenseMap<SemIR::NodeId, llvm::Value*> locals_;
  104. };
  105. // Declare handlers for each SemIR::File node.
  106. #define CARBON_SEMANTICS_NODE_KIND(Name) \
  107. auto LoweringHandle##Name(LoweringFunctionContext& context, \
  108. SemIR::NodeId node_id, SemIR::Node node) \
  109. ->void;
  110. #include "toolchain/semantics/semantics_node_kind.def"
  111. } // namespace Carbon
  112. #endif // CARBON_TOOLCHAIN_LOWERING_LOWERING_FUNCTION_CONTEXT_H_