inst_block_stack.h 3.6 KB

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