inst_block_stack.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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_block_stack.h"
  5. #include "common/vlog.h"
  6. #include "llvm/ADT/STLExtras.h"
  7. #include "llvm/ADT/StringExtras.h"
  8. namespace Carbon::Check {
  9. auto InstBlockStack::Push(SemIR::InstBlockId id) -> void {
  10. CARBON_VLOG() << name_ << " Push " << size_ << "\n";
  11. CARBON_CHECK(size_ < (1 << 20))
  12. << "Excessive stack size: likely infinite loop";
  13. if (size_ == static_cast<int>(stack_.size())) {
  14. stack_.emplace_back();
  15. }
  16. stack_[size_].Reset(id);
  17. ++size_;
  18. }
  19. auto InstBlockStack::PushGlobalInit() -> void {
  20. Push(SemIR::InstBlockId::GlobalInit);
  21. stack_[size_ - 1].content = std::move(init_block_);
  22. }
  23. auto InstBlockStack::PeekOrAdd(int depth) -> SemIR::InstBlockId {
  24. CARBON_CHECK(size() > depth) << "no such block";
  25. int index = size() - depth - 1;
  26. auto& slot = stack_[index];
  27. if (!slot.id.is_valid()) {
  28. slot.id = sem_ir_->inst_blocks().AddDefaultValue();
  29. }
  30. return slot.id;
  31. }
  32. auto InstBlockStack::Pop() -> SemIR::InstBlockId {
  33. CARBON_CHECK(!empty()) << "no current block";
  34. --size_;
  35. auto& back = stack_[size_];
  36. // Finalize the block.
  37. if (!back.content.empty() && back.id != SemIR::InstBlockId::Unreachable) {
  38. if (back.id.is_valid()) {
  39. sem_ir_->inst_blocks().Set(back.id, back.content);
  40. } else {
  41. back.id = sem_ir_->inst_blocks().Add(back.content);
  42. }
  43. }
  44. CARBON_VLOG() << name_ << " Pop " << size_ << ": " << back.id << "\n";
  45. if (!back.id.is_valid()) {
  46. return SemIR::InstBlockId::Empty;
  47. }
  48. return back.id;
  49. }
  50. auto InstBlockStack::PopGlobalInit() -> void {
  51. CARBON_CHECK(stack_[size_ - 1].id == SemIR::InstBlockId::GlobalInit)
  52. << "Trying to pop Inits block from " << name_
  53. << " but a different block is present!";
  54. init_block_ = std::move(stack_[size_ - 1].content);
  55. PopAndDiscard();
  56. }
  57. auto InstBlockStack::PopAndDiscard() -> void {
  58. CARBON_CHECK(!empty()) << "no current block";
  59. --size_;
  60. CARBON_VLOG() << name_ << " PopAndDiscard " << size_ << "\n";
  61. }
  62. auto InstBlockStack::PrintForStackDump(llvm::raw_ostream& output) const
  63. -> void {
  64. output << name_ << ":\n";
  65. for (const auto& [i, entry] : llvm::enumerate(stack_)) {
  66. output << "\t" << i << ".\t" << entry.id << "\t{";
  67. llvm::ListSeparator sep;
  68. for (auto id : entry.content) {
  69. output << sep << id;
  70. }
  71. output << "}\n";
  72. }
  73. }
  74. } // namespace Carbon::Check