inst_block_stack.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #ifndef CARBON_TOOLCHAIN_CHECK_INST_BLOCK_STACK_H_
  5. #define CARBON_TOOLCHAIN_CHECK_INST_BLOCK_STACK_H_
  6. #include "common/array_stack.h"
  7. #include "llvm/ADT/SmallVector.h"
  8. #include "toolchain/sem_ir/file.h"
  9. #include "toolchain/sem_ir/formatter.h"
  10. namespace Carbon::Check {
  11. // A stack of instruction blocks that are currently being constructed in a
  12. // Context. The contents of the instruction blocks are stored here until the
  13. // instruction block is popped from the stack, at which point they are
  14. // transferred into the SemIR::File for long-term storage.
  15. //
  16. // All pushes and pops will be vlogged.
  17. class InstBlockStack {
  18. public:
  19. explicit InstBlockStack(llvm::StringLiteral name, SemIR::File& sem_ir,
  20. llvm::raw_ostream* vlog_stream)
  21. : name_(name), sem_ir_(&sem_ir), vlog_stream_(vlog_stream) {}
  22. // Pushes an existing instruction block.
  23. auto Push(SemIR::InstBlockId id) -> void;
  24. // Pushes an existing instruction block with a set of instructions.
  25. auto Push(SemIR::InstBlockId id, llvm::ArrayRef<SemIR::InstId> inst_ids)
  26. -> void;
  27. // Pushes a new instruction block. It will be `None` unless PeekOrAdd is
  28. // called in order to support lazy allocation.
  29. auto Push() -> void { Push(SemIR::InstBlockId::None); }
  30. // Pushes a new unreachable code block.
  31. auto PushUnreachable() -> void { Push(SemIR::InstBlockId::Unreachable); }
  32. // Returns the ID of the top instruction block, allocating one if necessary.
  33. // If `depth` is specified, returns the instruction at `depth` levels from the
  34. // top of the stack instead of the top block, where the top block is at depth
  35. // 0.
  36. auto PeekOrAdd(int depth = 0) -> SemIR::InstBlockId;
  37. // Pops the top instruction block. This will never return `None`; `Empty` is
  38. // returned if one wasn't allocated.
  39. auto Pop() -> SemIR::InstBlockId;
  40. // Pops the top instruction block, and discards it if it hasn't had an ID
  41. // allocated.
  42. auto PopAndDiscard() -> void;
  43. // Adds the given instruction ID to the block at the top of the stack.
  44. auto AddInstId(SemIR::InstId inst_id) -> void {
  45. CARBON_CHECK(!empty(), "{0} has no current block", name_);
  46. insts_stack_.AppendToTop(inst_id);
  47. }
  48. // Returns whether the current block is statically reachable.
  49. auto is_current_block_reachable() -> bool {
  50. return id_stack_.back() != SemIR::InstBlockId::Unreachable;
  51. }
  52. // Returns a view of the contents of the top instruction block on the stack.
  53. auto PeekCurrentBlockContents() const -> llvm::ArrayRef<SemIR::InstId> {
  54. CARBON_CHECK(!empty(), "no current block");
  55. return insts_stack_.PeekArray();
  56. }
  57. // Prints the stack for a stack dump.
  58. auto PrintForStackDump(int indent, llvm::raw_ostream& output) const -> void;
  59. // Runs verification that the processing cleanly finished.
  60. auto VerifyOnFinish() const -> void {
  61. CARBON_CHECK(empty(), "{0} still has {1} entries", name_, id_stack_.size());
  62. }
  63. auto empty() const -> bool { return id_stack_.empty(); }
  64. private:
  65. // A name for debugging.
  66. llvm::StringLiteral name_;
  67. // The underlying SemIR::File instance. Always non-null.
  68. SemIR::File* sem_ir_;
  69. // Whether to print verbose output.
  70. llvm::raw_ostream* vlog_stream_;
  71. // The stack of block IDs. A value if allocated, `None` if no block has been
  72. // allocated, or `Unreachable` if this block is known to be unreachable.
  73. llvm::SmallVector<SemIR::InstBlockId> id_stack_;
  74. // The stack of insts in each block.
  75. ArrayStack<SemIR::InstId> insts_stack_;
  76. };
  77. } // namespace Carbon::Check
  78. #endif // CARBON_TOOLCHAIN_CHECK_INST_BLOCK_STACK_H_