lowering_handle.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. namespace Carbon {
  6. auto LoweringHandleInvalid(LoweringContext& /*context*/,
  7. SemanticsNodeId /*node_id*/, SemanticsNode /*node*/)
  8. -> void {
  9. llvm_unreachable("never in actual IR");
  10. }
  11. auto LoweringHandleCrossReference(LoweringContext& /*context*/,
  12. SemanticsNodeId /*node_id*/,
  13. SemanticsNode node) -> void {
  14. CARBON_FATAL() << "TODO: Add support: " << node;
  15. }
  16. auto LoweringHandleAssign(LoweringContext& context, SemanticsNodeId /*node_id*/,
  17. SemanticsNode node) -> void {
  18. auto [storage_id, value_id] = node.GetAsAssign();
  19. if (value_id == SemanticsNodeId::BuiltinEmptyStruct ||
  20. value_id == SemanticsNodeId::BuiltinEmptyTuple) {
  21. // Elide the 0-length store; these have no value assigned and it should have
  22. // no effect.
  23. return;
  24. }
  25. context.builder().CreateStore(context.GetLoweredNodeAsValue(value_id),
  26. context.GetLoweredNodeAsValue(storage_id));
  27. }
  28. auto LoweringHandleBinaryOperatorAdd(LoweringContext& /*context*/,
  29. SemanticsNodeId /*node_id*/,
  30. SemanticsNode node) -> void {
  31. CARBON_FATAL() << "TODO: Add support: " << node;
  32. }
  33. auto LoweringHandleBindName(LoweringContext& /*context*/,
  34. SemanticsNodeId /*node_id*/, SemanticsNode /*node*/)
  35. -> void {
  36. // Probably need to do something here, but not necessary for now.
  37. }
  38. auto LoweringHandleBuiltin(LoweringContext& /*context*/,
  39. SemanticsNodeId /*node_id*/, SemanticsNode node)
  40. -> void {
  41. CARBON_FATAL() << "TODO: Add support: " << node;
  42. }
  43. auto LoweringHandleCall(LoweringContext& /*context*/,
  44. SemanticsNodeId /*node_id*/, SemanticsNode node)
  45. -> void {
  46. CARBON_FATAL() << "TODO: Add support: " << node;
  47. }
  48. auto LoweringHandleCodeBlock(LoweringContext& /*context*/,
  49. SemanticsNodeId /*node_id*/, SemanticsNode node)
  50. -> void {
  51. CARBON_FATAL() << "TODO: Add support: " << node;
  52. }
  53. auto LoweringHandleFunctionDeclaration(LoweringContext& context,
  54. SemanticsNodeId /*node_id*/,
  55. SemanticsNode node) -> void {
  56. auto [name_id, callable_id] = node.GetAsFunctionDeclaration();
  57. auto callable = context.semantics_ir().GetCallable(callable_id);
  58. // TODO: Lower type information for the arguments prior to building args.
  59. auto param_refs = context.semantics_ir().GetNodeBlock(callable.param_refs_id);
  60. llvm::SmallVector<llvm::Type*> args;
  61. args.resize_for_overwrite(param_refs.size());
  62. for (int i = 0; i < static_cast<int>(param_refs.size()); ++i) {
  63. args[i] = context.GetLoweredNodeAsType(
  64. context.semantics_ir().GetNode(param_refs[i]).type_id());
  65. }
  66. llvm::Type* return_type = context.GetLoweredNodeAsType(
  67. callable.return_type_id.is_valid() ? callable.return_type_id
  68. : SemanticsNodeId::BuiltinEmptyTuple);
  69. llvm::FunctionType* function_type =
  70. llvm::FunctionType::get(return_type, args, /*isVarArg=*/false);
  71. auto* function = llvm::Function::Create(
  72. function_type, llvm::Function::ExternalLinkage,
  73. context.semantics_ir().GetString(name_id), context.llvm_module());
  74. // Set parameter names.
  75. for (int i = 0; i < static_cast<int>(param_refs.size()); ++i) {
  76. auto [param_name_id, _] =
  77. context.semantics_ir().GetNode(param_refs[i]).GetAsBindName();
  78. function->getArg(i)->setName(
  79. context.semantics_ir().GetString(param_name_id));
  80. }
  81. }
  82. auto LoweringHandleFunctionDefinition(LoweringContext& context,
  83. SemanticsNodeId /*node_id*/,
  84. SemanticsNode node) -> void {
  85. auto [declaration_id, body_block_id] = node.GetAsFunctionDefinition();
  86. auto [name_id, callable_id] =
  87. context.semantics_ir().GetNode(declaration_id).GetAsFunctionDeclaration();
  88. llvm::Function* function = context.llvm_module().getFunction(
  89. context.semantics_ir().GetString(name_id));
  90. // Create a new basic block to start insertion into.
  91. llvm::BasicBlock* body =
  92. llvm::BasicBlock::Create(context.llvm_context(), "entry", function);
  93. context.todo_blocks().push_back({body, body_block_id});
  94. }
  95. auto LoweringHandleIntegerLiteral(LoweringContext& context,
  96. SemanticsNodeId node_id, SemanticsNode node)
  97. -> void {
  98. SemanticsIntegerLiteralId int_id = node.GetAsIntegerLiteral();
  99. llvm::APInt i = context.semantics_ir().GetIntegerLiteral(int_id);
  100. llvm::Value* v = context.builder().getInt32(i.getLimitedValue());
  101. context.SetLoweredNodeAsValue(node_id, v);
  102. }
  103. auto LoweringHandleRealLiteral(LoweringContext& /*context*/,
  104. SemanticsNodeId /*node_id*/, SemanticsNode node)
  105. -> void {
  106. CARBON_FATAL() << "TODO: Add support: " << node;
  107. }
  108. auto LoweringHandleReturn(LoweringContext& context, SemanticsNodeId /*node_id*/,
  109. SemanticsNode /*node*/) -> void {
  110. context.builder().CreateRetVoid();
  111. }
  112. auto LoweringHandleReturnExpression(LoweringContext& context,
  113. SemanticsNodeId /*node_id*/,
  114. SemanticsNode node) -> void {
  115. SemanticsNodeId expr_id = node.GetAsReturnExpression();
  116. context.builder().CreateRet(context.GetLoweredNodeAsValue(expr_id));
  117. }
  118. auto LoweringHandleStringLiteral(LoweringContext& /*context*/,
  119. SemanticsNodeId /*node_id*/,
  120. SemanticsNode node) -> void {
  121. CARBON_FATAL() << "TODO: Add support: " << node;
  122. }
  123. auto LoweringHandleStructMemberAccess(LoweringContext& context,
  124. SemanticsNodeId node_id,
  125. SemanticsNode node) -> void {
  126. auto [struct_id, member_index] = node.GetAsStructMemberAccess();
  127. auto* struct_type = context.GetLoweredNodeAsType(
  128. context.semantics_ir().GetNode(struct_id).type_id());
  129. auto* gep = context.builder().CreateStructGEP(
  130. struct_type, context.GetLoweredNodeAsValue(struct_id),
  131. member_index.index);
  132. context.SetLoweredNodeAsValue(node_id, gep);
  133. }
  134. auto LoweringHandleStructType(LoweringContext& /*context*/,
  135. SemanticsNodeId /*node_id*/,
  136. SemanticsNode /*node*/) -> void {
  137. // No action to take.
  138. }
  139. auto LoweringHandleStructTypeField(LoweringContext& /*context*/,
  140. SemanticsNodeId /*node_id*/,
  141. SemanticsNode node) -> void {
  142. CARBON_FATAL() << "TODO: Add support: " << node;
  143. }
  144. auto LoweringHandleStructValue(LoweringContext& context,
  145. SemanticsNodeId node_id, SemanticsNode node)
  146. -> void {
  147. auto* type = context.GetLoweredNodeAsType(node.type_id());
  148. auto* alloca = context.builder().CreateAlloca(type);
  149. context.SetLoweredNodeAsValue(node_id, alloca);
  150. // TODO: Figure out changes to flow so that values are calculated for store.
  151. // Right now, the struct value IR is unevaluated.
  152. // auto refs =
  153. // context.semantics_ir().GetNodeBlock(node.GetAsStructValue().second);
  154. // for (int i = 0; i < static_cast<int>(refs.size()); ++i) {
  155. // auto* gep = context.builder().CreateStructGEP(type, alloca, i);
  156. // context.builder().CreateStore(context.GetLoweredNodeAsValue(refs[i]),
  157. // gep);
  158. // }
  159. }
  160. auto LoweringHandleStubReference(LoweringContext& /*context*/,
  161. SemanticsNodeId /*node_id*/,
  162. SemanticsNode node) -> void {
  163. CARBON_FATAL() << "TODO: Add support: " << node;
  164. }
  165. auto LoweringHandleVarStorage(LoweringContext& context, SemanticsNodeId node_id,
  166. SemanticsNode node) -> void {
  167. // TODO: This doesn't handle globals. Also, LLVM requires globals to have a
  168. // name. Do we want to generate a name, which would need to be consistent
  169. // across translation units, or use the given name, which requires either
  170. // looking ahead for BindName or restructuring semantics, either of which
  171. // affects the destructuring due to the difference in storage?
  172. auto* alloca = context.builder().CreateAlloca(
  173. context.GetLoweredNodeAsType(node.type_id()));
  174. context.SetLoweredNodeAsValue(node_id, alloca);
  175. }
  176. } // namespace Carbon