lowering_handle.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. context.builder().CreateStore(context.GetLocalLoaded(value_id),
  20. context.GetLocal(storage_id));
  21. }
  22. auto LoweringHandleBinaryOperatorAdd(LoweringContext& /*context*/,
  23. SemanticsNodeId /*node_id*/,
  24. SemanticsNode node) -> void {
  25. CARBON_FATAL() << "TODO: Add support: " << node;
  26. }
  27. auto LoweringHandleBindName(LoweringContext& /*context*/,
  28. SemanticsNodeId /*node_id*/, SemanticsNode /*node*/)
  29. -> void {
  30. // Probably need to do something here, but not necessary for now.
  31. }
  32. auto LoweringHandleBuiltin(LoweringContext& /*context*/,
  33. SemanticsNodeId /*node_id*/, SemanticsNode node)
  34. -> void {
  35. CARBON_FATAL() << "TODO: Add support: " << node;
  36. }
  37. auto LoweringHandleCall(LoweringContext& context, SemanticsNodeId node_id,
  38. SemanticsNode node) -> void {
  39. auto [refs_id, function_id] = node.GetAsCall();
  40. auto* function = context.GetFunction(function_id);
  41. std::vector<llvm::Value*> args;
  42. for (auto ref_id : context.semantics_ir().GetNodeBlock(refs_id)) {
  43. args.push_back(context.GetLocalLoaded(ref_id));
  44. }
  45. auto* value =
  46. context.builder().CreateCall(function, args, function->getName());
  47. context.SetLocal(node_id, value);
  48. }
  49. auto LoweringHandleCodeBlock(LoweringContext& /*context*/,
  50. SemanticsNodeId /*node_id*/, SemanticsNode node)
  51. -> void {
  52. CARBON_FATAL() << "TODO: Add support: " << node;
  53. }
  54. auto LoweringHandleFunctionDeclaration(LoweringContext& /*context*/,
  55. SemanticsNodeId /*node_id*/,
  56. SemanticsNode node) -> void {
  57. CARBON_FATAL()
  58. << "Should not be encountered. If that changes, we may want to change "
  59. "higher-level logic to skip them rather than calling this. "
  60. << node;
  61. }
  62. auto LoweringHandleIntegerLiteral(LoweringContext& context,
  63. SemanticsNodeId node_id, SemanticsNode node)
  64. -> void {
  65. llvm::APInt i =
  66. context.semantics_ir().GetIntegerLiteral(node.GetAsIntegerLiteral());
  67. // TODO: This won't offer correct semantics, but seems close enough for now.
  68. llvm::Value* v =
  69. llvm::ConstantInt::get(context.builder().getInt32Ty(), i.getSExtValue());
  70. context.SetLocal(node_id, v);
  71. }
  72. auto LoweringHandleRealLiteral(LoweringContext& context,
  73. SemanticsNodeId node_id, SemanticsNode node)
  74. -> void {
  75. SemanticsRealLiteral real =
  76. context.semantics_ir().GetRealLiteral(node.GetAsRealLiteral());
  77. // TODO: This will probably have overflow issues, and should be fixed.
  78. double val =
  79. real.mantissa.getSExtValue() *
  80. std::pow((real.is_decimal ? 10 : 2), real.exponent.getSExtValue());
  81. llvm::APFloat llvm_val(val);
  82. context.SetLocal(node_id, llvm::ConstantFP::get(
  83. context.builder().getDoubleTy(), llvm_val));
  84. }
  85. auto LoweringHandleReturn(LoweringContext& context, SemanticsNodeId /*node_id*/,
  86. SemanticsNode /*node*/) -> void {
  87. context.builder().CreateRetVoid();
  88. }
  89. auto LoweringHandleReturnExpression(LoweringContext& context,
  90. SemanticsNodeId /*node_id*/,
  91. SemanticsNode node) -> void {
  92. SemanticsNodeId expr_id = node.GetAsReturnExpression();
  93. context.builder().CreateRet(context.GetLocalLoaded(expr_id));
  94. }
  95. auto LoweringHandleStringLiteral(LoweringContext& /*context*/,
  96. SemanticsNodeId /*node_id*/,
  97. SemanticsNode node) -> void {
  98. CARBON_FATAL() << "TODO: Add support: " << node;
  99. }
  100. auto LoweringHandleStructMemberAccess(LoweringContext& context,
  101. SemanticsNodeId node_id,
  102. SemanticsNode node) -> void {
  103. auto [struct_id, member_index] = node.GetAsStructMemberAccess();
  104. auto struct_type_id = context.semantics_ir().GetNode(struct_id).type_id();
  105. auto* llvm_type = context.GetType(struct_type_id);
  106. // Get type information for member names.
  107. auto type_refs = context.semantics_ir().GetNodeBlock(
  108. context.semantics_ir()
  109. .GetNode(context.semantics_ir().GetType(struct_type_id))
  110. .GetAsStructType());
  111. auto member_name = context.semantics_ir().GetString(
  112. context.semantics_ir()
  113. .GetNode(type_refs[member_index.index])
  114. .GetAsStructTypeField());
  115. auto* gep = context.builder().CreateStructGEP(
  116. llvm_type, context.GetLocal(struct_id), member_index.index, member_name);
  117. context.SetLocal(node_id, gep);
  118. }
  119. auto LoweringHandleStructType(LoweringContext& /*context*/,
  120. SemanticsNodeId /*node_id*/,
  121. SemanticsNode /*node*/) -> void {
  122. // No action to take.
  123. }
  124. auto LoweringHandleStructTypeField(LoweringContext& /*context*/,
  125. SemanticsNodeId /*node_id*/,
  126. SemanticsNode /*node*/) -> void {
  127. // No action to take.
  128. }
  129. auto LoweringHandleStructValue(LoweringContext& context,
  130. SemanticsNodeId node_id, SemanticsNode node)
  131. -> void {
  132. auto* llvm_type = context.GetType(node.type_id());
  133. auto* alloca = context.builder().CreateAlloca(
  134. llvm_type, /*ArraySize=*/nullptr, "StructLiteralValue");
  135. context.SetLocal(node_id, alloca);
  136. auto refs = context.semantics_ir().GetNodeBlock(node.GetAsStructValue());
  137. // Get type information for member names.
  138. auto type_refs = context.semantics_ir().GetNodeBlock(
  139. context.semantics_ir()
  140. .GetNode(context.semantics_ir().GetType(node.type_id()))
  141. .GetAsStructType());
  142. for (int i = 0; i < static_cast<int>(refs.size()); ++i) {
  143. auto member_name = context.semantics_ir().GetString(
  144. context.semantics_ir().GetNode(type_refs[i]).GetAsStructTypeField());
  145. auto* gep =
  146. context.builder().CreateStructGEP(llvm_type, alloca, i, member_name);
  147. context.builder().CreateStore(context.GetLocal(refs[i]), gep);
  148. }
  149. }
  150. auto LoweringHandleStubReference(LoweringContext& context,
  151. SemanticsNodeId node_id, SemanticsNode node)
  152. -> void {
  153. context.SetLocal(node_id, context.GetLocal(node.GetAsStubReference()));
  154. }
  155. auto LoweringHandleVarStorage(LoweringContext& context, SemanticsNodeId node_id,
  156. SemanticsNode node) -> void {
  157. // TODO: This should provide a name, not just `var`. Also, LLVM requires
  158. // globals to have a name. Do we want to generate a name, which would need to
  159. // be consistent across translation units, or use the given name, which
  160. // requires either looking ahead for BindName or restructuring semantics,
  161. // either of which affects the destructuring due to the difference in
  162. // storage?
  163. auto* alloca = context.builder().CreateAlloca(context.GetType(node.type_id()),
  164. /*ArraySize=*/nullptr, "var");
  165. context.SetLocal(node_id, alloca);
  166. }
  167. } // namespace Carbon