lowering_context.cpp 3.9 KB

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