lowering_function_context.h 4.7 KB

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