lowering_handle.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. #include "toolchain/semantics/semantics_node_kind.h"
  6. namespace Carbon {
  7. auto LoweringHandleInvalid(LoweringFunctionContext& /*context*/,
  8. SemanticsNodeId /*node_id*/, SemanticsNode /*node*/)
  9. -> void {
  10. llvm_unreachable("never in actual IR");
  11. }
  12. auto LoweringHandleCrossReference(LoweringFunctionContext& /*context*/,
  13. SemanticsNodeId /*node_id*/,
  14. SemanticsNode node) -> void {
  15. CARBON_FATAL() << "TODO: Add support: " << node;
  16. }
  17. auto LoweringHandleAddressOf(LoweringFunctionContext& context,
  18. SemanticsNodeId node_id, SemanticsNode node)
  19. -> void {
  20. context.SetLocal(node_id, context.GetLocal(node.GetAsAddressOf()));
  21. }
  22. auto LoweringHandleArrayIndex(LoweringFunctionContext& context,
  23. SemanticsNodeId node_id, SemanticsNode node)
  24. -> void {
  25. auto [array_node_id, index_node_id] = node.GetAsArrayIndex();
  26. auto* array_value = context.GetLocal(array_node_id);
  27. auto* llvm_type =
  28. context.GetType(context.semantics_ir().GetNode(array_node_id).type_id());
  29. auto index_node = context.semantics_ir().GetNode(index_node_id);
  30. llvm::Value* array_element_value;
  31. if (index_node.kind() == SemanticsNodeKind::IntegerLiteral) {
  32. const auto index = context.semantics_ir()
  33. .GetIntegerLiteral(index_node.GetAsIntegerLiteral())
  34. .getZExtValue();
  35. array_element_value = context.GetIndexFromStructOrArray(
  36. llvm_type, array_value, index, "array.index");
  37. } else {
  38. auto* index = context.builder().CreateLoad(llvm_type->getArrayElementType(),
  39. context.GetLocal(index_node_id));
  40. // TODO: Handle return value or call such as `F()[a]`.
  41. array_element_value = context.builder().CreateInBoundsGEP(
  42. llvm_type, array_value, index, "array.index");
  43. }
  44. context.SetLocal(node_id, array_element_value);
  45. }
  46. auto LoweringHandleArrayValue(LoweringFunctionContext& context,
  47. SemanticsNodeId node_id, SemanticsNode node)
  48. -> void {
  49. auto* llvm_type = context.GetType(node.type_id());
  50. auto* alloca =
  51. context.builder().CreateAlloca(llvm_type, /*ArraySize=*/nullptr, "array");
  52. context.SetLocal(node_id, alloca);
  53. auto tuple_node_id = node.GetAsArrayValue();
  54. auto* tuple_value = context.GetLocal(tuple_node_id);
  55. auto* tuple_type =
  56. context.GetType(context.semantics_ir().GetNode(tuple_node_id).type_id());
  57. for (int i = 0; i < static_cast<int>(llvm_type->getArrayNumElements()); ++i) {
  58. llvm::Value* array_element_value = context.GetIndexFromStructOrArray(
  59. tuple_type, tuple_value, i, "array.element");
  60. if (tuple_value->getType()->isPointerTy()) {
  61. array_element_value = context.builder().CreateLoad(
  62. llvm_type->getArrayElementType(), array_element_value);
  63. }
  64. // Initializing the array with values.
  65. context.builder().CreateStore(
  66. array_element_value,
  67. context.builder().CreateStructGEP(llvm_type, alloca, i));
  68. }
  69. }
  70. auto LoweringHandleAssign(LoweringFunctionContext& context,
  71. SemanticsNodeId /*node_id*/, SemanticsNode node)
  72. -> void {
  73. auto [storage_id, value_id] = node.GetAsAssign();
  74. context.builder().CreateStore(context.GetLocalLoaded(value_id),
  75. context.GetLocal(storage_id));
  76. }
  77. auto LoweringHandleBinaryOperatorAdd(LoweringFunctionContext& /*context*/,
  78. SemanticsNodeId /*node_id*/,
  79. SemanticsNode node) -> void {
  80. CARBON_FATAL() << "TODO: Add support: " << node;
  81. }
  82. auto LoweringHandleBindName(LoweringFunctionContext& /*context*/,
  83. SemanticsNodeId /*node_id*/, SemanticsNode /*node*/)
  84. -> void {
  85. // Probably need to do something here, but not necessary for now.
  86. }
  87. auto LoweringHandleBlockArg(LoweringFunctionContext& context,
  88. SemanticsNodeId node_id, SemanticsNode node)
  89. -> void {
  90. SemanticsNodeBlockId block_id = node.GetAsBlockArg();
  91. context.SetLocal(node_id, context.GetBlockArg(block_id, node.type_id()));
  92. }
  93. auto LoweringHandleBoolLiteral(LoweringFunctionContext& context,
  94. SemanticsNodeId node_id, SemanticsNode node)
  95. -> void {
  96. llvm::Value* v = llvm::ConstantInt::get(context.builder().getInt1Ty(),
  97. node.GetAsBoolLiteral().index);
  98. context.SetLocal(node_id, v);
  99. }
  100. auto LoweringHandleBranch(LoweringFunctionContext& context,
  101. SemanticsNodeId /*node_id*/, SemanticsNode node)
  102. -> void {
  103. SemanticsNodeBlockId target_block_id = node.GetAsBranch();
  104. // Opportunistically avoid creating a BasicBlock that contains just a branch.
  105. llvm::BasicBlock* block = context.builder().GetInsertBlock();
  106. if (block->empty() && context.TryToReuseBlock(target_block_id, block)) {
  107. // Reuse this block as the branch target.
  108. } else {
  109. context.builder().CreateBr(context.GetBlock(target_block_id));
  110. }
  111. context.builder().ClearInsertionPoint();
  112. }
  113. auto LoweringHandleBranchIf(LoweringFunctionContext& context,
  114. SemanticsNodeId /*node_id*/, SemanticsNode node)
  115. -> void {
  116. auto [target_block_id, cond_id] = node.GetAsBranchIf();
  117. llvm::Value* cond = context.GetLocalLoaded(cond_id);
  118. llvm::BasicBlock* then_block = context.GetBlock(target_block_id);
  119. llvm::BasicBlock* else_block = context.CreateSyntheticBlock();
  120. context.builder().CreateCondBr(cond, then_block, else_block);
  121. context.builder().SetInsertPoint(else_block);
  122. }
  123. auto LoweringHandleBranchWithArg(LoweringFunctionContext& context,
  124. SemanticsNodeId /*node_id*/,
  125. SemanticsNode node) -> void {
  126. auto [target_block_id, arg_id] = node.GetAsBranchWithArg();
  127. llvm::Value* arg = context.GetLocalLoaded(arg_id);
  128. SemanticsTypeId arg_type_id =
  129. context.semantics_ir().GetNode(arg_id).type_id();
  130. // Opportunistically avoid creating a BasicBlock that contains just a branch.
  131. // We only do this for a block that we know will only have a single
  132. // predecessor, so that we can correctly populate the predecessors of the
  133. // PHINode.
  134. llvm::BasicBlock* block = context.builder().GetInsertBlock();
  135. llvm::BasicBlock* phi_predecessor = block;
  136. if (block->empty() && context.IsCurrentSyntheticBlock(block) &&
  137. context.TryToReuseBlock(target_block_id, block)) {
  138. // Reuse this block as the branch target.
  139. phi_predecessor = block->getSinglePredecessor();
  140. CARBON_CHECK(phi_predecessor)
  141. << "Synthetic block did not have a single predecessor";
  142. } else {
  143. context.builder().CreateBr(context.GetBlock(target_block_id));
  144. }
  145. context.GetBlockArg(target_block_id, arg_type_id)
  146. ->addIncoming(arg, phi_predecessor);
  147. context.builder().ClearInsertionPoint();
  148. }
  149. auto LoweringHandleBuiltin(LoweringFunctionContext& /*context*/,
  150. SemanticsNodeId /*node_id*/, SemanticsNode node)
  151. -> void {
  152. CARBON_FATAL() << "TODO: Add support: " << node;
  153. }
  154. auto LoweringHandleCall(LoweringFunctionContext& context,
  155. SemanticsNodeId node_id, SemanticsNode node) -> void {
  156. auto [refs_id, function_id] = node.GetAsCall();
  157. auto* function = context.GetFunction(function_id);
  158. std::vector<llvm::Value*> args;
  159. for (auto ref_id : context.semantics_ir().GetNodeBlock(refs_id)) {
  160. args.push_back(context.GetLocalLoaded(ref_id));
  161. }
  162. if (function->getReturnType()->isVoidTy()) {
  163. context.builder().CreateCall(function, args);
  164. // TODO: use empty tuple type.
  165. // TODO: don't create the empty tuple if the call does not get assigned.
  166. context.SetLocal(node_id, context.builder().CreateAlloca(
  167. llvm::StructType::get(context.llvm_context()),
  168. /*ArraySize=*/nullptr, "call.result"));
  169. } else {
  170. context.SetLocal(node_id, context.builder().CreateCall(
  171. function, args, function->getName()));
  172. }
  173. }
  174. auto LoweringHandleDereference(LoweringFunctionContext& context,
  175. SemanticsNodeId node_id, SemanticsNode node)
  176. -> void {
  177. context.SetLocal(node_id, context.GetLocal(node.GetAsDereference()));
  178. }
  179. auto LoweringHandleFunctionDeclaration(LoweringFunctionContext& /*context*/,
  180. SemanticsNodeId /*node_id*/,
  181. SemanticsNode node) -> void {
  182. CARBON_FATAL()
  183. << "Should not be encountered. If that changes, we may want to change "
  184. "higher-level logic to skip them rather than calling this. "
  185. << node;
  186. }
  187. auto LoweringHandleIntegerLiteral(LoweringFunctionContext& context,
  188. SemanticsNodeId node_id, SemanticsNode node)
  189. -> void {
  190. llvm::APInt i =
  191. context.semantics_ir().GetIntegerLiteral(node.GetAsIntegerLiteral());
  192. // TODO: This won't offer correct semantics, but seems close enough for now.
  193. llvm::Value* v =
  194. llvm::ConstantInt::get(context.builder().getInt32Ty(), i.getSExtValue());
  195. context.SetLocal(node_id, v);
  196. }
  197. auto LoweringHandleNamespace(LoweringFunctionContext& /*context*/,
  198. SemanticsNodeId /*node_id*/,
  199. SemanticsNode /*node*/) -> void {
  200. // No action to take.
  201. }
  202. auto LoweringHandleRealLiteral(LoweringFunctionContext& context,
  203. SemanticsNodeId node_id, SemanticsNode node)
  204. -> void {
  205. SemanticsRealLiteral real =
  206. context.semantics_ir().GetRealLiteral(node.GetAsRealLiteral());
  207. // TODO: This will probably have overflow issues, and should be fixed.
  208. double val =
  209. real.mantissa.getSExtValue() *
  210. std::pow((real.is_decimal ? 10 : 2), real.exponent.getSExtValue());
  211. llvm::APFloat llvm_val(val);
  212. context.SetLocal(node_id, llvm::ConstantFP::get(
  213. context.builder().getDoubleTy(), llvm_val));
  214. }
  215. auto LoweringHandleReturn(LoweringFunctionContext& context,
  216. SemanticsNodeId /*node_id*/, SemanticsNode /*node*/)
  217. -> void {
  218. context.builder().CreateRetVoid();
  219. }
  220. auto LoweringHandleReturnExpression(LoweringFunctionContext& context,
  221. SemanticsNodeId /*node_id*/,
  222. SemanticsNode node) -> void {
  223. SemanticsNodeId expr_id = node.GetAsReturnExpression();
  224. context.builder().CreateRet(context.GetLocalLoaded(expr_id));
  225. }
  226. auto LoweringHandleStringLiteral(LoweringFunctionContext& /*context*/,
  227. SemanticsNodeId /*node_id*/,
  228. SemanticsNode node) -> void {
  229. CARBON_FATAL() << "TODO: Add support: " << node;
  230. }
  231. auto LoweringHandleStructAccess(LoweringFunctionContext& context,
  232. SemanticsNodeId node_id, SemanticsNode node)
  233. -> void {
  234. auto [struct_id, member_index] = node.GetAsStructAccess();
  235. auto struct_type_id = context.semantics_ir().GetNode(struct_id).type_id();
  236. auto* llvm_type = context.GetType(struct_type_id);
  237. // Get type information for member names.
  238. auto type_refs = context.semantics_ir().GetNodeBlock(
  239. context.semantics_ir()
  240. .GetNode(context.semantics_ir().GetType(struct_type_id))
  241. .GetAsStructType());
  242. auto [field_name_id, field_type_id] =
  243. context.semantics_ir()
  244. .GetNode(type_refs[member_index.index])
  245. .GetAsStructTypeField();
  246. auto member_name = context.semantics_ir().GetString(field_name_id);
  247. auto* gep = context.builder().CreateStructGEP(
  248. llvm_type, context.GetLocal(struct_id), member_index.index, member_name);
  249. context.SetLocal(node_id, gep);
  250. }
  251. auto LoweringHandleTupleIndex(LoweringFunctionContext& context,
  252. SemanticsNodeId node_id, SemanticsNode node)
  253. -> void {
  254. auto [tuple_node_id, index_node_id] = node.GetAsTupleIndex();
  255. auto* tuple_value = context.GetLocal(tuple_node_id);
  256. auto index_node = context.semantics_ir().GetNode(index_node_id);
  257. const auto index = context.semantics_ir()
  258. .GetIntegerLiteral(index_node.GetAsIntegerLiteral())
  259. .getZExtValue();
  260. auto* llvm_type =
  261. context.GetType(context.semantics_ir().GetNode(tuple_node_id).type_id());
  262. context.SetLocal(node_id, context.GetIndexFromStructOrArray(
  263. llvm_type, tuple_value, index, "tuple.index"));
  264. }
  265. auto LoweringHandleTupleValue(LoweringFunctionContext& context,
  266. SemanticsNodeId node_id, SemanticsNode node)
  267. -> void {
  268. auto* llvm_type = context.GetType(node.type_id());
  269. auto* alloca =
  270. context.builder().CreateAlloca(llvm_type, /*ArraySize=*/nullptr, "tuple");
  271. context.SetLocal(node_id, alloca);
  272. auto refs = context.semantics_ir().GetNodeBlock(node.GetAsTupleValue());
  273. auto type_refs = context.semantics_ir().GetTypeBlock(
  274. context.semantics_ir()
  275. .GetNode(context.semantics_ir().GetType(node.type_id()))
  276. .GetAsTupleType());
  277. for (int i = 0; i < static_cast<int>(type_refs.size()); ++i) {
  278. auto* gep = context.builder().CreateStructGEP(llvm_type, alloca, i);
  279. context.builder().CreateStore(context.GetLocal(refs[i]), gep);
  280. }
  281. }
  282. auto LoweringHandleStructTypeField(LoweringFunctionContext& /*context*/,
  283. SemanticsNodeId /*node_id*/,
  284. SemanticsNode /*node*/) -> void {
  285. // No action to take.
  286. }
  287. auto LoweringHandleStructValue(LoweringFunctionContext& context,
  288. SemanticsNodeId node_id, SemanticsNode node)
  289. -> void {
  290. auto* llvm_type = context.GetType(node.type_id());
  291. auto* alloca = context.builder().CreateAlloca(
  292. llvm_type, /*ArraySize=*/nullptr, "struct");
  293. context.SetLocal(node_id, alloca);
  294. auto refs = context.semantics_ir().GetNodeBlock(node.GetAsStructValue());
  295. // Get type information for member names.
  296. auto type_refs = context.semantics_ir().GetNodeBlock(
  297. context.semantics_ir()
  298. .GetNode(context.semantics_ir().GetType(node.type_id()))
  299. .GetAsStructType());
  300. for (int i = 0; i < static_cast<int>(refs.size()); ++i) {
  301. auto [field_name_id, field_type_id] =
  302. context.semantics_ir().GetNode(type_refs[i]).GetAsStructTypeField();
  303. auto member_name = context.semantics_ir().GetString(field_name_id);
  304. auto* gep =
  305. context.builder().CreateStructGEP(llvm_type, alloca, i, member_name);
  306. context.builder().CreateStore(context.GetLocal(refs[i]), gep);
  307. }
  308. }
  309. auto LoweringHandleStubReference(LoweringFunctionContext& context,
  310. SemanticsNodeId node_id, SemanticsNode node)
  311. -> void {
  312. context.SetLocal(node_id, context.GetLocal(node.GetAsStubReference()));
  313. }
  314. auto LoweringHandleUnaryOperatorNot(LoweringFunctionContext& context,
  315. SemanticsNodeId node_id, SemanticsNode node)
  316. -> void {
  317. context.SetLocal(node_id, context.builder().CreateNot(context.GetLocal(
  318. node.GetAsUnaryOperatorNot())));
  319. }
  320. auto LoweringHandleVarStorage(LoweringFunctionContext& context,
  321. SemanticsNodeId node_id, SemanticsNode node)
  322. -> void {
  323. // TODO: Eventually this name will be optional, and we'll want to provide
  324. // something like `var` as a default. However, that's not possible right now
  325. // so cannot be tested.
  326. auto name = context.semantics_ir().GetString(node.GetAsVarStorage());
  327. auto* alloca = context.builder().CreateAlloca(context.GetType(node.type_id()),
  328. /*ArraySize=*/nullptr, name);
  329. context.SetLocal(node_id, alloca);
  330. }
  331. } // namespace Carbon