inst.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/check/inst.h"
  5. #include "common/vlog.h"
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/check/eval.h"
  8. #include "toolchain/check/generic_region_stack.h"
  9. #include "toolchain/sem_ir/constant.h"
  10. #include "toolchain/sem_ir/ids.h"
  11. namespace Carbon::Check {
  12. // Finish producing an instruction. Set its constant value, and register it in
  13. // any applicable instruction lists.
  14. static auto FinishInst(Context& context, SemIR::InstId inst_id,
  15. SemIR::Inst inst) -> void {
  16. GenericRegionStack::DependencyKind dep_kind =
  17. GenericRegionStack::DependencyKind::None;
  18. // If the instruction has a symbolic constant type, track that we need to
  19. // substitute into it.
  20. if (context.constant_values().DependsOnGenericParameter(
  21. context.types().GetConstantId(inst.type_id()))) {
  22. dep_kind |= GenericRegionStack::DependencyKind::SymbolicType;
  23. }
  24. // If the instruction has a constant value, compute it.
  25. auto const_id = TryEvalInst(context, inst_id, inst);
  26. context.constant_values().Set(inst_id, const_id);
  27. if (const_id.is_constant()) {
  28. CARBON_VLOG_TO(context.vlog_stream(), "Constant: {0} -> {1}\n", inst,
  29. context.constant_values().GetInstId(const_id));
  30. // If the constant value is symbolic, track that we need to substitute into
  31. // it.
  32. if (context.constant_values().DependsOnGenericParameter(const_id)) {
  33. dep_kind |= GenericRegionStack::DependencyKind::SymbolicConstant;
  34. }
  35. }
  36. // Template-dependent instructions are handled separately by
  37. // `AddDependentActionInst`.
  38. CARBON_CHECK(
  39. inst.kind().constant_kind() != SemIR::InstConstantKind::InstAction,
  40. "Use AddDependentActionInst to add an action instruction");
  41. // Keep track of dependent instructions.
  42. if (dep_kind != GenericRegionStack::DependencyKind::None) {
  43. context.generic_region_stack().AddDependentInst(
  44. {.inst_id = inst_id, .kind = dep_kind});
  45. }
  46. }
  47. auto AddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  48. -> SemIR::InstId {
  49. auto inst_id = AddInstInNoBlock(context, loc_id_and_inst);
  50. context.inst_block_stack().AddInstId(inst_id);
  51. return inst_id;
  52. }
  53. auto AddInstInNoBlock(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  54. -> SemIR::InstId {
  55. auto inst_id = context.sem_ir().insts().AddInNoBlock(loc_id_and_inst);
  56. CARBON_VLOG_TO(context.vlog_stream(), "AddInst: {0}\n", loc_id_and_inst.inst);
  57. FinishInst(context, inst_id, loc_id_and_inst.inst);
  58. return inst_id;
  59. }
  60. auto AddDependentActionInst(Context& context,
  61. SemIR::LocIdAndInst loc_id_and_inst)
  62. -> SemIR::InstId {
  63. auto inst_id = context.sem_ir().insts().AddInNoBlock(loc_id_and_inst);
  64. CARBON_VLOG_TO(context.vlog_stream(), "AddDependentActionInst: {0}\n",
  65. loc_id_and_inst.inst);
  66. // Set the constant value of this instruction to point back to itself.
  67. auto const_id = context.constant_values().AddSymbolicConstant(
  68. {.inst_id = inst_id,
  69. .generic_id = SemIR::GenericId::None,
  70. .index = SemIR::GenericInstIndex::None,
  71. .dependence = SemIR::ConstantDependence::Template});
  72. context.constant_values().Set(inst_id, const_id);
  73. // Register the instruction to be added to the eval block.
  74. context.generic_region_stack().AddDependentInst(
  75. {.inst_id = inst_id,
  76. .kind = GenericRegionStack::DependencyKind::Template});
  77. return inst_id;
  78. }
  79. auto AddPatternInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  80. -> SemIR::InstId {
  81. auto inst_id = AddInstInNoBlock(context, loc_id_and_inst);
  82. context.pattern_block_stack().AddInstId(inst_id);
  83. return inst_id;
  84. }
  85. auto GetOrAddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  86. -> SemIR::InstId {
  87. if (loc_id_and_inst.loc_id.is_implicit()) {
  88. auto const_id =
  89. TryEvalInst(context, SemIR::InstId::None, loc_id_and_inst.inst);
  90. if (const_id.has_value()) {
  91. CARBON_VLOG_TO(context.vlog_stream(), "GetOrAddInst: constant: {0}\n",
  92. loc_id_and_inst.inst);
  93. return context.constant_values().GetInstId(const_id);
  94. }
  95. }
  96. // TODO: For an implicit instruction, this reattempts evaluation.
  97. return AddInst(context, loc_id_and_inst);
  98. }
  99. auto AddPlaceholderInstInNoBlock(Context& context,
  100. SemIR::LocIdAndInst loc_id_and_inst)
  101. -> SemIR::InstId {
  102. auto inst_id = context.sem_ir().insts().AddInNoBlock(loc_id_and_inst);
  103. CARBON_VLOG_TO(context.vlog_stream(), "AddPlaceholderInst: {0}\n",
  104. loc_id_and_inst.inst);
  105. context.constant_values().Set(inst_id, SemIR::ConstantId::None);
  106. return inst_id;
  107. }
  108. auto AddPlaceholderInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  109. -> SemIR::InstId {
  110. auto inst_id = AddPlaceholderInstInNoBlock(context, loc_id_and_inst);
  111. context.inst_block_stack().AddInstId(inst_id);
  112. return inst_id;
  113. }
  114. auto ReplaceLocIdAndInstBeforeConstantUse(Context& context,
  115. SemIR::InstId inst_id,
  116. SemIR::LocIdAndInst loc_id_and_inst)
  117. -> void {
  118. context.sem_ir().insts().SetLocIdAndInst(inst_id, loc_id_and_inst);
  119. CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
  120. loc_id_and_inst.inst);
  121. FinishInst(context, inst_id, loc_id_and_inst.inst);
  122. }
  123. auto ReplaceInstBeforeConstantUse(Context& context, SemIR::InstId inst_id,
  124. SemIR::Inst inst) -> void {
  125. context.sem_ir().insts().Set(inst_id, inst);
  126. CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
  127. inst);
  128. FinishInst(context, inst_id, inst);
  129. }
  130. auto ReplaceInstPreservingConstantValue(Context& context, SemIR::InstId inst_id,
  131. SemIR::Inst inst) -> void {
  132. auto old_const_id = context.constant_values().Get(inst_id);
  133. context.sem_ir().insts().Set(inst_id, inst);
  134. CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
  135. inst);
  136. auto new_const_id = TryEvalInst(context, inst_id, inst);
  137. CARBON_CHECK(old_const_id == new_const_id);
  138. }
  139. auto SetNamespaceNodeId(Context& context, SemIR::InstId inst_id,
  140. Parse::NodeId node_id) -> void {
  141. context.sem_ir().insts().SetLocId(inst_id, SemIR::LocId(node_id));
  142. }
  143. } // namespace Carbon::Check