lowering_context.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #include "toolchain/lowering/lowering_context.h"
  5. #include "common/vlog.h"
  6. #include "toolchain/semantics/semantics_ir.h"
  7. #include "toolchain/semantics/semantics_node_kind.h"
  8. namespace Carbon {
  9. LoweringContext::LoweringContext(llvm::LLVMContext& llvm_context,
  10. llvm::StringRef module_name,
  11. const SemanticsIR& semantics_ir,
  12. llvm::raw_ostream* vlog_stream)
  13. : llvm_context_(&llvm_context),
  14. llvm_module_(std::make_unique<llvm::Module>(module_name, llvm_context)),
  15. builder_(llvm_context),
  16. semantics_ir_(&semantics_ir),
  17. vlog_stream_(vlog_stream),
  18. lowered_nodes_(semantics_ir_->nodes_size(), nullptr),
  19. lowered_callables_(semantics_ir_->callables_size(), nullptr) {
  20. CARBON_CHECK(!semantics_ir.has_errors())
  21. << "Generating LLVM IR from invalid SemanticsIR is unsupported.";
  22. }
  23. auto LoweringContext::Run() -> std::unique_ptr<llvm::Module> {
  24. CARBON_CHECK(llvm_module_) << "Run can only be called once.";
  25. LowerBlock(semantics_ir_->top_node_block_id());
  26. while (!todo_blocks_.empty()) {
  27. auto [llvm_block, block_id] = todo_blocks_.pop_back_val();
  28. builder_.SetInsertPoint(llvm_block);
  29. LowerBlock(block_id);
  30. }
  31. return std::move(llvm_module_);
  32. }
  33. auto LoweringContext::LowerBlock(SemanticsNodeBlockId block_id) -> void {
  34. CARBON_VLOG() << "Lowering block " << block_id << "\n";
  35. for (const auto& node_id : semantics_ir_->GetNodeBlock(block_id)) {
  36. auto node = semantics_ir_->GetNode(node_id);
  37. CARBON_VLOG() << "Lowering node" << node_id << ": " << node << "\n";
  38. switch (node.kind()) {
  39. #define CARBON_SEMANTICS_NODE_KIND(Name) \
  40. case SemanticsNodeKind::Name: \
  41. LoweringHandle##Name(*this, node_id, node); \
  42. break;
  43. #include "toolchain/semantics/semantics_node_kind.def"
  44. }
  45. }
  46. }
  47. auto LoweringContext::BuildLoweredNodeAsType(SemanticsNodeId node_id)
  48. -> llvm::Type* {
  49. switch (node_id.index) {
  50. case SemanticsBuiltinKind::EmptyTupleType.AsInt():
  51. // Represent empty types as empty structs.
  52. // TODO: Investigate special-casing handling of these so that they can be
  53. // collectively replaced with LLVM's void, particularly around function
  54. // returns. LLVM doesn't allow declaring variables with a void type, so
  55. // that may require significant special casing.
  56. return llvm::StructType::create(
  57. *llvm_context_, llvm::ArrayRef<llvm::Type*>(),
  58. SemanticsBuiltinKind::FromInt(node_id.index).name());
  59. case SemanticsBuiltinKind::FloatingPointType.AsInt():
  60. // TODO: Handle different sizes.
  61. return builder_.getDoubleTy();
  62. case SemanticsBuiltinKind::IntegerType.AsInt():
  63. // TODO: Handle different sizes.
  64. return builder_.getInt32Ty();
  65. }
  66. auto node = semantics_ir_->GetNode(node_id);
  67. switch (node.kind()) {
  68. case SemanticsNodeKind::StructType: {
  69. auto refs = semantics_ir_->GetNodeBlock(node.GetAsStructType());
  70. llvm::SmallVector<llvm::Type*> subtypes;
  71. subtypes.reserve(refs.size());
  72. for (auto ref_id : refs) {
  73. auto type_id = semantics_ir_->GetNode(ref_id).type_id();
  74. // TODO: Handle recursive types. The restriction for builtins prevents
  75. // recursion while still letting them cache.
  76. CARBON_CHECK(type_id.index < SemanticsBuiltinKind::ValidCount)
  77. << type_id;
  78. subtypes.push_back(GetLoweredNodeAsType(type_id));
  79. }
  80. return llvm::StructType::create(*llvm_context_, subtypes,
  81. "StructLiteralType");
  82. }
  83. default: {
  84. CARBON_FATAL() << "Cannot use node as type: " << node_id;
  85. }
  86. }
  87. }
  88. auto LoweringContext::GetLoweredNodeAsType(SemanticsNodeId node_id)
  89. -> llvm::Type* {
  90. if (lowered_nodes_[node_id.index]) {
  91. return lowered_nodes_[node_id.index].get<llvm::Type*>();
  92. }
  93. auto* type = BuildLoweredNodeAsType(node_id);
  94. lowered_nodes_[node_id.index] = type;
  95. return type;
  96. }
  97. auto LoweringContext::GetLoweredNodeAsValue(SemanticsNodeId node_id)
  98. -> llvm::Value* {
  99. auto& node = lowered_nodes_[node_id.index];
  100. if (node.is<llvm::Value*>()) {
  101. return node.get<llvm::Value*>();
  102. }
  103. CARBON_CHECK(node.isNull())
  104. << node_id << " is already set as a type, not a value";
  105. // Empty values are built lazily.
  106. // TODO: It might be better to built them at initialization, putting them in
  107. // every IR even if not used. This is probably a performance decision since it
  108. // would simplify this function.
  109. if (node_id == SemanticsNodeId::BuiltinEmptyTuple) {
  110. auto* type = GetLoweredNodeAsType(SemanticsNodeId::BuiltinEmptyTupleType);
  111. auto* value = llvm::ConstantStruct::get(llvm::cast<llvm::StructType>(type),
  112. llvm::ArrayRef<llvm::Constant*>());
  113. node = value;
  114. return value;
  115. }
  116. CARBON_FATAL() << node_id << " is null, cannot be initialized";
  117. }
  118. } // namespace Carbon