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