Skip to content

Commit 7bb7345

Browse files
authored
[TableGen][DecoderEmitter] Add helpers for working with scopes (NFC) (#153979)
Part of an effort to simplify DecoderEmitter code.
1 parent f874092 commit 7bb7345

File tree

1 file changed

+18
-29
lines changed

1 file changed

+18
-29
lines changed

llvm/utils/TableGen/DecoderEmitter.cpp

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,17 @@ struct DecoderTableInfo {
192192
DecoderSet Decoders;
193193

194194
bool isOutermostScope() const { return FixupStack.size() == 1; }
195+
196+
void pushScope() { FixupStack.emplace_back(); }
197+
198+
void popScope() {
199+
// Resolve any remaining fixups in the current scope before popping it.
200+
// All fixups resolve to the current location.
201+
uint32_t DestIdx = Table.size();
202+
for (uint32_t FixupIdx : FixupStack.back())
203+
Table.patchNumToSkip(FixupIdx, DestIdx);
204+
FixupStack.pop_back();
205+
}
195206
};
196207

197208
struct EncodingAndInst {
@@ -726,14 +737,6 @@ void Filter::recurse() {
726737
}
727738
}
728739

729-
static void resolveTableFixups(DecoderTable &Table, const FixupList &Fixups,
730-
uint32_t DestIdx) {
731-
// Any NumToSkip fixups in the current scope can resolve to the
732-
// current location.
733-
for (uint32_t FixupIdx : Fixups)
734-
Table.patchNumToSkip(FixupIdx, DestIdx);
735-
}
736-
737740
// Emit table entries to decode instructions given a segment or segments
738741
// of bits.
739742
void Filter::emitTableEntry(DecoderTableInfo &TableInfo) const {
@@ -753,7 +756,7 @@ void Filter::emitTableEntry(DecoderTableInfo &TableInfo) const {
753756
const uint64_t LastFilter = FilterChooserMap.rbegin()->first;
754757
bool HasFallthrough = LastFilter == NO_FIXED_SEGMENTS_SENTINEL;
755758
if (HasFallthrough)
756-
TableInfo.FixupStack.emplace_back();
759+
TableInfo.pushScope();
757760

758761
DecoderTable &Table = TableInfo.Table;
759762

@@ -765,13 +768,7 @@ void Filter::emitTableEntry(DecoderTableInfo &TableInfo) const {
765768
// Each scope should always have at least one filter value to check
766769
// for.
767770
assert(PrevFilter != 0 && "empty filter set!");
768-
FixupList &CurScope = TableInfo.FixupStack.back();
769-
// Resolve any NumToSkip fixups in the current scope.
770-
resolveTableFixups(Table, CurScope, Table.size());
771-
772-
// Delete the scope we have added here.
773-
TableInfo.FixupStack.pop_back();
774-
771+
TableInfo.popScope();
775772
PrevFilter = 0; // Don't re-process the filter's fallthrough.
776773
} else {
777774
// The last filtervalue emitted can be OPC_FilterValue if we are at
@@ -1515,13 +1512,9 @@ void FilterChooser::emitSingletonTableEntry(DecoderTableInfo &TableInfo,
15151512

15161513
// complex singletons need predicate checks from the first singleton
15171514
// to refer forward to the variable filterchooser that follows.
1518-
TableInfo.FixupStack.emplace_back();
1519-
1515+
TableInfo.pushScope();
15201516
emitSingletonTableEntry(TableInfo, Opc);
1521-
1522-
resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(),
1523-
TableInfo.Table.size());
1524-
TableInfo.FixupStack.pop_back();
1517+
TableInfo.popScope();
15251518

15261519
Best.getVariableFC().emitTableEntries(TableInfo);
15271520
}
@@ -2623,16 +2616,12 @@ namespace {
26232616
// predicates and decoders themselves, however, are shared across all
26242617
// decoders to give more opportunities for uniqueing.
26252618
TableInfo.Table.clear();
2626-
TableInfo.FixupStack.clear();
2627-
TableInfo.FixupStack.emplace_back();
2619+
TableInfo.pushScope();
26282620
FC.emitTableEntries(TableInfo);
26292621
// Any NumToSkip fixups in the top level scope can resolve to the
26302622
// OPC_Fail at the end of the table.
2631-
assert(TableInfo.FixupStack.size() == 1 && "fixup stack phasing error!");
2632-
// Resolve any NumToSkip fixups in the current scope.
2633-
resolveTableFixups(TableInfo.Table, TableInfo.FixupStack.back(),
2634-
TableInfo.Table.size());
2635-
TableInfo.FixupStack.clear();
2623+
assert(TableInfo.isOutermostScope() && "fixup stack phasing error!");
2624+
TableInfo.popScope();
26362625

26372626
TableInfo.Table.push_back(MCD::OPC_Fail);
26382627

0 commit comments

Comments
 (0)