lowering_context.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. CARBON_CHECK(!semantics_ir.has_errors())
  20. << "Generating LLVM IR from invalid SemanticsIR is unsupported.";
  21. }
  22. auto LoweringContext::Run() -> std::unique_ptr<llvm::Module> {
  23. CARBON_CHECK(llvm_module_) << "Run can only be called once.";
  24. LowerBlock(semantics_ir_->top_node_block_id());
  25. while (!todo_blocks_.empty()) {
  26. auto [llvm_block, block_id] = todo_blocks_.pop_back_val();
  27. builder_.SetInsertPoint(llvm_block);
  28. LowerBlock(block_id);
  29. }
  30. return std::move(llvm_module_);
  31. }
  32. auto LoweringContext::LowerBlock(SemanticsNodeBlockId block_id) -> void {
  33. CARBON_VLOG() << "Lowering block " << block_id << "\n";
  34. for (const auto& node_id : semantics_ir_->GetNodeBlock(block_id)) {
  35. auto node = semantics_ir_->GetNode(node_id);
  36. CARBON_VLOG() << "Lowering node" << node_id << ": " << node << "\n";
  37. switch (node.kind()) {
  38. #define CARBON_SEMANTICS_NODE_KIND(Name) \
  39. case SemanticsNodeKind::Name: \
  40. LoweringHandle##Name(*this, node_id, node); \
  41. break;
  42. #include "toolchain/semantics/semantics_node_kind.def"
  43. }
  44. }
  45. }
  46. auto LoweringContext::BuildLoweredNodeAsType(SemanticsNodeId node_id)
  47. -> llvm::Type* {
  48. switch (node_id.index) {
  49. case SemanticsBuiltinKind::EmptyStructType.AsInt():
  50. case SemanticsBuiltinKind::EmptyTuple.AsInt():
  51. case SemanticsBuiltinKind::EmptyTupleType.AsInt():
  52. // Represent empty types as empty structs.
  53. // TODO: Investigate special-casing handling of these so that they can be
  54. // collectively replaced with LLVM's void, particularly around function
  55. // returns. LLVM doesn't allow declaring variables with a void type, so
  56. // that may require significant special casing.
  57. // TODO: Work to remove EmptyTuple here.
  58. return llvm::StructType::create(
  59. *llvm_context_, llvm::ArrayRef<llvm::Type*>(),
  60. SemanticsBuiltinKind::FromInt(node_id.index).name());
  61. case SemanticsBuiltinKind::FloatingPointType.AsInt():
  62. // TODO: Handle different sizes.
  63. return builder_.getDoubleTy();
  64. case SemanticsBuiltinKind::IntegerType.AsInt():
  65. // TODO: Handle different sizes.
  66. return builder_.getInt32Ty();
  67. }
  68. auto node = semantics_ir_->GetNode(node_id);
  69. switch (node.kind()) {
  70. case SemanticsNodeKind::StructType: {
  71. auto refs = semantics_ir_->GetNodeBlock(node.GetAsStructType());
  72. llvm::SmallVector<llvm::Type*> subtypes;
  73. subtypes.reserve(refs.size());
  74. for (auto ref_id : refs) {
  75. auto type_id = semantics_ir_->GetNode(ref_id).type_id();
  76. // TODO: Handle recursive types. The restriction for builtins prevents
  77. // recursion while still letting them cache.
  78. CARBON_CHECK(type_id.index < SemanticsBuiltinKind::ValidCount)
  79. << type_id;
  80. subtypes.push_back(GetLoweredNodeAsType(type_id));
  81. }
  82. return llvm::StructType::create(*llvm_context_, subtypes,
  83. "StructLiteralType");
  84. }
  85. default: {
  86. CARBON_FATAL() << "Cannot use node as type: " << node_id;
  87. }
  88. }
  89. }
  90. auto LoweringContext::GetLoweredNodeAsType(SemanticsNodeId node_id)
  91. -> llvm::Type* {
  92. if (lowered_nodes_[node_id.index]) {
  93. return lowered_nodes_[node_id.index].get<llvm::Type*>();
  94. }
  95. auto* type = BuildLoweredNodeAsType(node_id);
  96. lowered_nodes_[node_id.index] = type;
  97. return type;
  98. }
  99. } // namespace Carbon