lowering_context.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. nodes_(semantics_ir_->nodes_size(), nullptr),
  19. 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. types_.resize_for_overwrite(types.size());
  24. for (int i = 0; i < static_cast<int>(types.size()); ++i) {
  25. types_[i] = BuildType(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_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_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::BuildType(SemanticsNodeId node_id) -> llvm::Type* {
  53. switch (node_id.index) {
  54. case SemanticsBuiltinKind::EmptyTupleType.AsInt():
  55. // Represent empty types as empty structs.
  56. // TODO: Investigate special-casing handling of these so that they can be
  57. // collectively replaced with LLVM's void, particularly around function
  58. // returns. LLVM doesn't allow declaring variables with a void type, so
  59. // that may require significant special casing.
  60. return llvm::StructType::create(
  61. *llvm_context_, llvm::ArrayRef<llvm::Type*>(),
  62. SemanticsBuiltinKind::FromInt(node_id.index).name());
  63. case SemanticsBuiltinKind::FloatingPointType.AsInt():
  64. // TODO: Handle different sizes.
  65. return builder_.getDoubleTy();
  66. case SemanticsBuiltinKind::IntegerType.AsInt():
  67. // TODO: Handle different sizes.
  68. return builder_.getInt32Ty();
  69. }
  70. auto node = semantics_ir_->GetNode(node_id);
  71. switch (node.kind()) {
  72. case SemanticsNodeKind::StructType: {
  73. auto refs = semantics_ir_->GetNodeBlock(node.GetAsStructType());
  74. llvm::SmallVector<llvm::Type*> subtypes;
  75. subtypes.reserve(refs.size());
  76. for (auto ref_id : refs) {
  77. auto type_id = semantics_ir_->GetNode(ref_id).type_id();
  78. // TODO: Handle recursive types. The restriction for builtins prevents
  79. // recursion while still letting them cache.
  80. CARBON_CHECK(type_id.index < SemanticsBuiltinKind::ValidCount)
  81. << type_id;
  82. subtypes.push_back(GetType(type_id));
  83. }
  84. return llvm::StructType::create(*llvm_context_, subtypes,
  85. "StructLiteralType");
  86. }
  87. default: {
  88. CARBON_FATAL() << "Cannot use node as type: " << node_id;
  89. }
  90. }
  91. }
  92. auto LoweringContext::GetNodeLoaded(SemanticsNodeId node_id) -> llvm::Value* {
  93. auto* value = GetNode(node_id);
  94. if (llvm::isa<llvm::AllocaInst, llvm::GetElementPtrInst>(value)) {
  95. auto* load_type = GetType(semantics_ir().GetNode(node_id).type_id());
  96. return builder().CreateLoad(load_type, value);
  97. } else {
  98. // No load is needed.
  99. return value;
  100. }
  101. }
  102. } // namespace Carbon