Store contents of incomplete node blocks on the node block stack. (#3223)

`SemIR` now only stores complete blocks, and placeholders for blocks
where we need a block ID before we know the block contents.

This is a preparation step towards the new node block allocation design.
This commit is contained in:
Richard Smith authored and GitHub committed 2023-09-13 23:28:13 +00:00
1 parent 9ba1669472
commit a66f35e92c
9 files changed
+123 -84

No files matched your search

+37 -16
View File
@@ -6,43 +6,64 @@
#include "common/vlog.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "toolchain/sem_ir/node.h"
namespace Carbon::Check {
auto NodeBlockStack::Push(SemIR::NodeBlockId id) -> void {
CARBON_VLOG() << name_ << " Push " << stack_.size() << "\n";
CARBON_CHECK(stack_.size() < (1 << 20))
CARBON_VLOG() << name_ << " Push " << size_ << "\n";
CARBON_CHECK(size_ < (1 << 20))
<< "Excessive stack size: likely infinite loop";
stack_.push_back(id);
if (size_ == static_cast<int>(stack_.size())) {
stack_.emplace_back();
}
stack_[size_].Reset(id);
++size_;
}
auto NodeBlockStack::PeekForAdd(int depth) -> SemIR::NodeBlockId {
CARBON_CHECK(static_cast<int>(stack_.size()) > depth) << "no such block";
int index = stack_.size() - depth - 1;
CARBON_CHECK(size() > depth) << "no such block";
int index = size() - depth - 1;
auto& slot = stack_[index];
if (!slot.is_valid()) {
slot = semantics_ir_->AddNodeBlock();
CARBON_VLOG() << name_ << " Add " << index << ": " << slot << "\n";
if (!slot.id.is_valid()) {
slot.id = semantics_ir_->AddNodeBlockId();
CARBON_VLOG() << name_ << " Add " << index << ": " << slot.id << "\n";
}
return slot;
return slot.id;
}
auto NodeBlockStack::Pop() -> SemIR::NodeBlockId {
CARBON_CHECK(!stack_.empty()) << "no current block";
auto back = stack_.pop_back_val();
CARBON_VLOG() << name_ << " Pop " << stack_.size() << ": " << back << "\n";
if (!back.is_valid()) {
CARBON_CHECK(!empty()) << "no current block";
--size_;
auto& back = stack_[size_];
// Finalize the block.
if (!back.content.empty() && back.id != SemIR::NodeBlockId::Unreachable) {
if (back.id.is_valid()) {
semantics_ir_->SetNodeBlock(back.id, back.content);
} else {
back.id = semantics_ir_->AddNodeBlock(back.content);
}
}
CARBON_VLOG() << name_ << " Pop " << size_ << ": " << back.id << "\n";
if (!back.id.is_valid()) {
return SemIR::NodeBlockId::Empty;
}
return back;
return back.id;
}
auto NodeBlockStack::PrintForStackDump(llvm::raw_ostream& output) const
-> void {
output << name_ << ":\n";
for (auto [i, entry] : llvm::enumerate(stack_)) {
output << "\t" << i << ".\t" << entry << "\n";
for (const auto& [i, entry] : llvm::enumerate(stack_)) {
output << "\t" << i << ".\t" << entry.id << "\t{";
llvm::ListSeparator sep;
for (auto id : entry.content) {
output << sep << id;
}
output << "}\n";
}
}