lowering_context.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. // Returns a local (versus global) value for the given node.
  23. auto GetLocal(SemanticsNodeId node_id) -> llvm::Value* {
  24. auto it = locals_.find(node_id);
  25. CARBON_CHECK(it != locals_.end()) << "Missing local: " << node_id;
  26. return it->second;
  27. }
  28. // Returns a local (versus global) value for the given node in loaded state.
  29. // Loads will only be inserted on an as-needed basis.
  30. auto GetLocalLoaded(SemanticsNodeId node_id) -> llvm::Value*;
  31. // Sets the value for the given node.
  32. auto SetLocal(SemanticsNodeId node_id, llvm::Value* value) {
  33. bool added = locals_.insert({node_id, value}).second;
  34. CARBON_CHECK(added) << "Duplicate local insert: " << node_id;
  35. }
  36. // Gets a callable's function.
  37. auto GetFunction(SemanticsFunctionId function_id) -> llvm::Function* {
  38. CARBON_CHECK(functions_[function_id.index] != nullptr) << function_id;
  39. return functions_[function_id.index];
  40. }
  41. // Returns a lowered type for the given type_id.
  42. auto GetType(SemanticsTypeId type_id) -> llvm::Type* {
  43. // Neither TypeType nor InvalidType should be passed in.
  44. CARBON_CHECK(type_id.index >= 0) << type_id;
  45. return types_[type_id.index];
  46. }
  47. auto llvm_context() -> llvm::LLVMContext& { return *llvm_context_; }
  48. auto llvm_module() -> llvm::Module& { return *llvm_module_; }
  49. auto builder() -> llvm::IRBuilder<>& { return builder_; }
  50. auto semantics_ir() -> const SemanticsIR& { return *semantics_ir_; }
  51. private:
  52. // Builds the declaration for the given function, which should then be cached
  53. // by the caller.
  54. auto BuildFunctionDeclaration(SemanticsFunctionId function_id)
  55. -> llvm::Function*;
  56. // Builds the definition for the given function. If the function is only a
  57. // declaration with no definition, does nothing.
  58. auto BuildFunctionDefinition(SemanticsFunctionId function_id) -> void;
  59. // Builds the type for the given node, which should then be cached by the
  60. // caller.
  61. auto BuildType(SemanticsNodeId node_id) -> llvm::Type*;
  62. // State for building the LLVM IR.
  63. llvm::LLVMContext* llvm_context_;
  64. std::unique_ptr<llvm::Module> llvm_module_;
  65. llvm::IRBuilder<> builder_;
  66. // The input Semantics IR.
  67. const SemanticsIR* const semantics_ir_;
  68. // The optional vlog stream.
  69. llvm::raw_ostream* vlog_stream_;
  70. // Maps a function's SemanticsIR nodes to lowered values.
  71. // TODO: Handle nested scopes. Right now this is just cleared at the end of
  72. // every block.
  73. llvm::DenseMap<SemanticsNodeId, llvm::Value*> locals_;
  74. // Maps callables to lowered functions. Semantics treats callables as the
  75. // canonical form of a function, so lowering needs to do the same.
  76. llvm::SmallVector<llvm::Function*> functions_;
  77. // Provides lowered versions of types.
  78. llvm::SmallVector<llvm::Type*> types_;
  79. };
  80. // Declare handlers for each SemanticsIR node.
  81. #define CARBON_SEMANTICS_NODE_KIND(Name) \
  82. auto LoweringHandle##Name(LoweringContext& context, SemanticsNodeId node_id, \
  83. SemanticsNode node) \
  84. ->void;
  85. #include "toolchain/semantics/semantics_node_kind.def"
  86. } // namespace Carbon
  87. #endif // CARBON_TOOLCHAIN_LOWERING_LOWERING_CONTEXT_H_