lowering_handle.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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_function_context.h"
  5. namespace Carbon {
  6. auto LoweringHandleInvalid(LoweringFunctionContext& /*context*/,
  7. SemanticsNodeId /*node_id*/, SemanticsNode /*node*/)
  8. -> void {
  9. llvm_unreachable("never in actual IR");
  10. }
  11. auto LoweringHandleCrossReference(LoweringFunctionContext& /*context*/,
  12. SemanticsNodeId /*node_id*/,
  13. SemanticsNode node) -> void {
  14. CARBON_FATAL() << "TODO: Add support: " << node;
  15. }
  16. auto LoweringHandleAssign(LoweringFunctionContext& context,
  17. SemanticsNodeId /*node_id*/, SemanticsNode node)
  18. -> void {
  19. auto [storage_id, value_id] = node.GetAsAssign();
  20. context.builder().CreateStore(context.GetLocalLoaded(value_id),
  21. context.GetLocal(storage_id));
  22. }
  23. auto LoweringHandleBinaryOperatorAdd(LoweringFunctionContext& /*context*/,
  24. SemanticsNodeId /*node_id*/,
  25. SemanticsNode node) -> void {
  26. CARBON_FATAL() << "TODO: Add support: " << node;
  27. }
  28. auto LoweringHandleBindName(LoweringFunctionContext& /*context*/,
  29. SemanticsNodeId /*node_id*/, SemanticsNode /*node*/)
  30. -> void {
  31. // Probably need to do something here, but not necessary for now.
  32. }
  33. auto LoweringHandleBlockArg(LoweringFunctionContext& context,
  34. SemanticsNodeId node_id, SemanticsNode node)
  35. -> void {
  36. SemanticsNodeBlockId block_id = node.GetAsBlockArg();
  37. context.SetLocal(node_id, context.GetBlockArg(block_id, node.type_id()));
  38. }
  39. auto LoweringHandleBoolLiteral(LoweringFunctionContext& context,
  40. SemanticsNodeId node_id, SemanticsNode node)
  41. -> void {
  42. llvm::Value* v = llvm::ConstantInt::get(context.builder().getInt1Ty(),
  43. node.GetAsBoolLiteral().index);
  44. context.SetLocal(node_id, v);
  45. }
  46. auto LoweringHandleBranch(LoweringFunctionContext& context,
  47. SemanticsNodeId /*node_id*/, SemanticsNode node)
  48. -> void {
  49. SemanticsNodeBlockId target_block_id = node.GetAsBranch();
  50. // Opportunistically avoid creating a BasicBlock that contains just a branch.
  51. llvm::BasicBlock* block = context.builder().GetInsertBlock();
  52. if (block->empty() && context.TryToReuseBlock(target_block_id, block)) {
  53. // Reuse this block as the branch target.
  54. } else {
  55. context.builder().CreateBr(context.GetBlock(target_block_id));
  56. }
  57. context.builder().ClearInsertionPoint();
  58. }
  59. auto LoweringHandleBranchIf(LoweringFunctionContext& context,
  60. SemanticsNodeId /*node_id*/, SemanticsNode node)
  61. -> void {
  62. auto [target_block_id, cond_id] = node.GetAsBranchIf();
  63. llvm::Value* cond = context.GetLocalLoaded(cond_id);
  64. llvm::BasicBlock* then_block = context.GetBlock(target_block_id);
  65. llvm::BasicBlock* else_block = context.CreateSyntheticBlock();
  66. context.builder().CreateCondBr(cond, then_block, else_block);
  67. context.builder().SetInsertPoint(else_block);
  68. }
  69. auto LoweringHandleBranchWithArg(LoweringFunctionContext& context,
  70. SemanticsNodeId /*node_id*/,
  71. SemanticsNode node) -> void {
  72. auto [target_block_id, arg_id] = node.GetAsBranchWithArg();
  73. llvm::Value* arg = context.GetLocalLoaded(arg_id);
  74. SemanticsTypeId arg_type_id =
  75. context.semantics_ir().GetNode(arg_id).type_id();
  76. // Opportunistically avoid creating a BasicBlock that contains just a branch.
  77. // We only do this for a block that we know will only have a single
  78. // predecessor, so that we can correctly populate the predecessors of the
  79. // PHINode.
  80. llvm::BasicBlock* block = context.builder().GetInsertBlock();
  81. llvm::BasicBlock* phi_predecessor = block;
  82. if (block->empty() && context.IsCurrentSyntheticBlock(block) &&
  83. context.TryToReuseBlock(target_block_id, block)) {
  84. // Reuse this block as the branch target.
  85. phi_predecessor = block->getSinglePredecessor();
  86. CARBON_CHECK(phi_predecessor)
  87. << "Synthetic block did not have a single predecessor";
  88. } else {
  89. context.builder().CreateBr(context.GetBlock(target_block_id));
  90. }
  91. context.GetBlockArg(target_block_id, arg_type_id)
  92. ->addIncoming(arg, phi_predecessor);
  93. context.builder().ClearInsertionPoint();
  94. }
  95. auto LoweringHandleBuiltin(LoweringFunctionContext& /*context*/,
  96. SemanticsNodeId /*node_id*/, SemanticsNode node)
  97. -> void {
  98. CARBON_FATAL() << "TODO: Add support: " << node;
  99. }
  100. auto LoweringHandleCall(LoweringFunctionContext& context,
  101. SemanticsNodeId node_id, SemanticsNode node) -> void {
  102. auto [refs_id, function_id] = node.GetAsCall();
  103. auto* function = context.GetFunction(function_id);
  104. std::vector<llvm::Value*> args;
  105. for (auto ref_id : context.semantics_ir().GetNodeBlock(refs_id)) {
  106. args.push_back(context.GetLocalLoaded(ref_id));
  107. }
  108. if (function->getReturnType()->isVoidTy()) {
  109. context.builder().CreateCall(function, args);
  110. // TODO: use empty tuple type.
  111. // TODO: don't create the empty tuple if the call does not get assigned.
  112. context.SetLocal(node_id, context.builder().CreateAlloca(
  113. llvm::StructType::get(context.llvm_context()),
  114. /*ArraySize=*/nullptr, "TupleLiteralValue"));
  115. } else {
  116. context.SetLocal(node_id, context.builder().CreateCall(
  117. function, args, function->getName()));
  118. }
  119. }
  120. auto LoweringHandleFunctionDeclaration(LoweringFunctionContext& /*context*/,
  121. SemanticsNodeId /*node_id*/,
  122. SemanticsNode node) -> void {
  123. CARBON_FATAL()
  124. << "Should not be encountered. If that changes, we may want to change "
  125. "higher-level logic to skip them rather than calling this. "
  126. << node;
  127. }
  128. auto LoweringHandleIntegerLiteral(LoweringFunctionContext& context,
  129. SemanticsNodeId node_id, SemanticsNode node)
  130. -> void {
  131. llvm::APInt i =
  132. context.semantics_ir().GetIntegerLiteral(node.GetAsIntegerLiteral());
  133. // TODO: This won't offer correct semantics, but seems close enough for now.
  134. llvm::Value* v =
  135. llvm::ConstantInt::get(context.builder().getInt32Ty(), i.getSExtValue());
  136. context.SetLocal(node_id, v);
  137. }
  138. auto LoweringHandleNamespace(LoweringFunctionContext& /*context*/,
  139. SemanticsNodeId /*node_id*/,
  140. SemanticsNode /*node*/) -> void {
  141. // No action to take.
  142. }
  143. auto LoweringHandleRealLiteral(LoweringFunctionContext& context,
  144. SemanticsNodeId node_id, SemanticsNode node)
  145. -> void {
  146. SemanticsRealLiteral real =
  147. context.semantics_ir().GetRealLiteral(node.GetAsRealLiteral());
  148. // TODO: This will probably have overflow issues, and should be fixed.
  149. double val =
  150. real.mantissa.getSExtValue() *
  151. std::pow((real.is_decimal ? 10 : 2), real.exponent.getSExtValue());
  152. llvm::APFloat llvm_val(val);
  153. context.SetLocal(node_id, llvm::ConstantFP::get(
  154. context.builder().getDoubleTy(), llvm_val));
  155. }
  156. auto LoweringHandleReturn(LoweringFunctionContext& context,
  157. SemanticsNodeId /*node_id*/, SemanticsNode /*node*/)
  158. -> void {
  159. context.builder().CreateRetVoid();
  160. }
  161. auto LoweringHandleReturnExpression(LoweringFunctionContext& context,
  162. SemanticsNodeId /*node_id*/,
  163. SemanticsNode node) -> void {
  164. SemanticsNodeId expr_id = node.GetAsReturnExpression();
  165. context.builder().CreateRet(context.GetLocalLoaded(expr_id));
  166. }
  167. auto LoweringHandleStringLiteral(LoweringFunctionContext& /*context*/,
  168. SemanticsNodeId /*node_id*/,
  169. SemanticsNode node) -> void {
  170. CARBON_FATAL() << "TODO: Add support: " << node;
  171. }
  172. auto LoweringHandleStructMemberAccess(LoweringFunctionContext& context,
  173. SemanticsNodeId node_id,
  174. SemanticsNode node) -> void {
  175. auto [struct_id, member_index] = node.GetAsStructMemberAccess();
  176. auto struct_type_id = context.semantics_ir().GetNode(struct_id).type_id();
  177. auto* llvm_type = context.GetType(struct_type_id);
  178. // Get type information for member names.
  179. auto type_refs = context.semantics_ir().GetNodeBlock(
  180. context.semantics_ir()
  181. .GetNode(context.semantics_ir().GetType(struct_type_id))
  182. .GetAsStructType());
  183. auto [field_name_id, field_type_id] =
  184. context.semantics_ir()
  185. .GetNode(type_refs[member_index.index])
  186. .GetAsStructTypeField();
  187. auto member_name = context.semantics_ir().GetString(field_name_id);
  188. auto* gep = context.builder().CreateStructGEP(
  189. llvm_type, context.GetLocal(struct_id), member_index.index, member_name);
  190. context.SetLocal(node_id, gep);
  191. }
  192. auto LoweringHandleTupleValue(LoweringFunctionContext& context,
  193. SemanticsNodeId node_id, SemanticsNode node)
  194. -> void {
  195. auto* llvm_type = context.GetType(node.type_id());
  196. auto* alloca = context.builder().CreateAlloca(
  197. llvm_type, /*ArraySize=*/nullptr, "TupleLiteralValue");
  198. context.SetLocal(node_id, alloca);
  199. auto refs = context.semantics_ir().GetNodeBlock(node.GetAsTupleValue());
  200. auto type_refs = context.semantics_ir().GetTypeBlock(
  201. context.semantics_ir()
  202. .GetNode(context.semantics_ir().GetType(node.type_id()))
  203. .GetAsTupleType());
  204. for (int i = 0; i < static_cast<int>(type_refs.size()); ++i) {
  205. auto* gep = context.builder().CreateStructGEP(llvm_type, alloca, i);
  206. context.builder().CreateStore(context.GetLocal(refs[i]), gep);
  207. }
  208. }
  209. auto LoweringHandleStructTypeField(LoweringFunctionContext& /*context*/,
  210. SemanticsNodeId /*node_id*/,
  211. SemanticsNode /*node*/) -> void {
  212. // No action to take.
  213. }
  214. auto LoweringHandleStructValue(LoweringFunctionContext& context,
  215. SemanticsNodeId node_id, SemanticsNode node)
  216. -> void {
  217. auto* llvm_type = context.GetType(node.type_id());
  218. auto* alloca = context.builder().CreateAlloca(
  219. llvm_type, /*ArraySize=*/nullptr, "StructLiteralValue");
  220. context.SetLocal(node_id, alloca);
  221. auto refs = context.semantics_ir().GetNodeBlock(node.GetAsStructValue());
  222. // Get type information for member names.
  223. auto type_refs = context.semantics_ir().GetNodeBlock(
  224. context.semantics_ir()
  225. .GetNode(context.semantics_ir().GetType(node.type_id()))
  226. .GetAsStructType());
  227. for (int i = 0; i < static_cast<int>(refs.size()); ++i) {
  228. auto [field_name_id, field_type_id] =
  229. context.semantics_ir().GetNode(type_refs[i]).GetAsStructTypeField();
  230. auto member_name = context.semantics_ir().GetString(field_name_id);
  231. auto* gep =
  232. context.builder().CreateStructGEP(llvm_type, alloca, i, member_name);
  233. context.builder().CreateStore(context.GetLocal(refs[i]), gep);
  234. }
  235. }
  236. auto LoweringHandleStubReference(LoweringFunctionContext& context,
  237. SemanticsNodeId node_id, SemanticsNode node)
  238. -> void {
  239. context.SetLocal(node_id, context.GetLocal(node.GetAsStubReference()));
  240. }
  241. auto LoweringHandleUnaryOperatorNot(LoweringFunctionContext& context,
  242. SemanticsNodeId node_id, SemanticsNode node)
  243. -> void {
  244. context.SetLocal(node_id, context.builder().CreateNot(context.GetLocal(
  245. node.GetAsUnaryOperatorNot())));
  246. }
  247. auto LoweringHandleVarStorage(LoweringFunctionContext& context,
  248. SemanticsNodeId node_id, SemanticsNode node)
  249. -> void {
  250. // TODO: This should provide a name, not just `var`. Also, LLVM requires
  251. // globals to have a name. Do we want to generate a name, which would need to
  252. // be consistent across translation units, or use the given name, which
  253. // requires either looking ahead for BindName or restructuring semantics,
  254. // either of which affects the destructuring due to the difference in
  255. // storage?
  256. auto* alloca = context.builder().CreateAlloca(context.GetType(node.type_id()),
  257. /*ArraySize=*/nullptr, "var");
  258. context.SetLocal(node_id, alloca);
  259. }
  260. } // namespace Carbon