106f32e7eSjoerg //===- DFAEmitter.cpp - Finite state automaton emitter --------------------===//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg //
906f32e7eSjoerg // This class can produce a generic deterministic finite state automaton (DFA),
1006f32e7eSjoerg // given a set of possible states and transitions.
1106f32e7eSjoerg //
1206f32e7eSjoerg // The input transitions can be nondeterministic - this class will produce the
1306f32e7eSjoerg // deterministic equivalent state machine.
1406f32e7eSjoerg //
1506f32e7eSjoerg // The generated code can run the DFA and produce an accepted / not accepted
1606f32e7eSjoerg // state and also produce, given a sequence of transitions that results in an
1706f32e7eSjoerg // accepted state, the sequence of intermediate states. This is useful if the
1806f32e7eSjoerg // initial automaton was nondeterministic - it allows mapping back from the DFA
1906f32e7eSjoerg // to the NFA.
2006f32e7eSjoerg //
2106f32e7eSjoerg //===----------------------------------------------------------------------===//
2206f32e7eSjoerg #define DEBUG_TYPE "dfa-emitter"
2306f32e7eSjoerg 
2406f32e7eSjoerg #include "DFAEmitter.h"
2506f32e7eSjoerg #include "CodeGenTarget.h"
2606f32e7eSjoerg #include "SequenceToOffsetTable.h"
2706f32e7eSjoerg #include "TableGenBackends.h"
2806f32e7eSjoerg #include "llvm/ADT/SmallVector.h"
2906f32e7eSjoerg #include "llvm/ADT/StringExtras.h"
3006f32e7eSjoerg #include "llvm/ADT/UniqueVector.h"
3106f32e7eSjoerg #include "llvm/Support/Debug.h"
3206f32e7eSjoerg #include "llvm/Support/raw_ostream.h"
3306f32e7eSjoerg #include "llvm/TableGen/Record.h"
3406f32e7eSjoerg #include "llvm/TableGen/TableGenBackend.h"
3506f32e7eSjoerg #include <cassert>
3606f32e7eSjoerg #include <cstdint>
3706f32e7eSjoerg #include <map>
3806f32e7eSjoerg #include <set>
3906f32e7eSjoerg #include <string>
4006f32e7eSjoerg #include <vector>
4106f32e7eSjoerg 
4206f32e7eSjoerg using namespace llvm;
4306f32e7eSjoerg 
4406f32e7eSjoerg //===----------------------------------------------------------------------===//
4506f32e7eSjoerg // DfaEmitter implementation. This is independent of the GenAutomaton backend.
4606f32e7eSjoerg //===----------------------------------------------------------------------===//
4706f32e7eSjoerg 
addTransition(state_type From,state_type To,action_type A)4806f32e7eSjoerg void DfaEmitter::addTransition(state_type From, state_type To, action_type A) {
4906f32e7eSjoerg   Actions.insert(A);
5006f32e7eSjoerg   NfaStates.insert(From);
5106f32e7eSjoerg   NfaStates.insert(To);
5206f32e7eSjoerg   NfaTransitions[{From, A}].push_back(To);
5306f32e7eSjoerg   ++NumNfaTransitions;
5406f32e7eSjoerg }
5506f32e7eSjoerg 
visitDfaState(const DfaState & DS)56*da58b97aSjoerg void DfaEmitter::visitDfaState(const DfaState &DS) {
5706f32e7eSjoerg   // For every possible action...
5806f32e7eSjoerg   auto FromId = DfaStates.idFor(DS);
5906f32e7eSjoerg   for (action_type A : Actions) {
6006f32e7eSjoerg     DfaState NewStates;
6106f32e7eSjoerg     DfaTransitionInfo TI;
6206f32e7eSjoerg     // For every represented state, word pair in the original NFA...
63*da58b97aSjoerg     for (state_type FromState : DS) {
6406f32e7eSjoerg       // If this action is possible from this state add the transitioned-to
6506f32e7eSjoerg       // states to NewStates.
6606f32e7eSjoerg       auto I = NfaTransitions.find({FromState, A});
6706f32e7eSjoerg       if (I == NfaTransitions.end())
6806f32e7eSjoerg         continue;
6906f32e7eSjoerg       for (state_type &ToState : I->second) {
7006f32e7eSjoerg         NewStates.push_back(ToState);
7106f32e7eSjoerg         TI.emplace_back(FromState, ToState);
7206f32e7eSjoerg       }
7306f32e7eSjoerg     }
7406f32e7eSjoerg     if (NewStates.empty())
7506f32e7eSjoerg       continue;
7606f32e7eSjoerg     // Sort and unique.
7706f32e7eSjoerg     sort(NewStates);
7806f32e7eSjoerg     NewStates.erase(std::unique(NewStates.begin(), NewStates.end()),
7906f32e7eSjoerg                     NewStates.end());
8006f32e7eSjoerg     sort(TI);
8106f32e7eSjoerg     TI.erase(std::unique(TI.begin(), TI.end()), TI.end());
8206f32e7eSjoerg     unsigned ToId = DfaStates.insert(NewStates);
8306f32e7eSjoerg     DfaTransitions.emplace(std::make_pair(FromId, A), std::make_pair(ToId, TI));
8406f32e7eSjoerg   }
8506f32e7eSjoerg }
8606f32e7eSjoerg 
constructDfa()8706f32e7eSjoerg void DfaEmitter::constructDfa() {
8806f32e7eSjoerg   DfaState Initial(1, /*NFA initial state=*/0);
8906f32e7eSjoerg   DfaStates.insert(Initial);
9006f32e7eSjoerg 
9106f32e7eSjoerg   // Note that UniqueVector starts indices at 1, not zero.
9206f32e7eSjoerg   unsigned DfaStateId = 1;
93*da58b97aSjoerg   while (DfaStateId <= DfaStates.size()) {
94*da58b97aSjoerg     DfaState S = DfaStates[DfaStateId];
95*da58b97aSjoerg     visitDfaState(S);
96*da58b97aSjoerg     DfaStateId++;
97*da58b97aSjoerg   }
9806f32e7eSjoerg }
9906f32e7eSjoerg 
emit(StringRef Name,raw_ostream & OS)10006f32e7eSjoerg void DfaEmitter::emit(StringRef Name, raw_ostream &OS) {
10106f32e7eSjoerg   constructDfa();
10206f32e7eSjoerg 
10306f32e7eSjoerg   OS << "// Input NFA has " << NfaStates.size() << " states with "
10406f32e7eSjoerg      << NumNfaTransitions << " transitions.\n";
10506f32e7eSjoerg   OS << "// Generated DFA has " << DfaStates.size() << " states with "
10606f32e7eSjoerg      << DfaTransitions.size() << " transitions.\n\n";
10706f32e7eSjoerg 
10806f32e7eSjoerg   // Implementation note: We don't bake a simple std::pair<> here as it requires
10906f32e7eSjoerg   // significantly more effort to parse. A simple test with a large array of
11006f32e7eSjoerg   // struct-pairs (N=100000) took clang-10 6s to parse. The same array of
11106f32e7eSjoerg   // std::pair<uint64_t, uint64_t> took 242s. Instead we allow the user to
11206f32e7eSjoerg   // define the pair type.
11306f32e7eSjoerg   //
11406f32e7eSjoerg   // FIXME: It may make sense to emit these as ULEB sequences instead of
11506f32e7eSjoerg   // pairs of uint64_t.
11606f32e7eSjoerg   OS << "// A zero-terminated sequence of NFA state transitions. Every DFA\n";
11706f32e7eSjoerg   OS << "// transition implies a set of NFA transitions. These are referred\n";
11806f32e7eSjoerg   OS << "// to by index in " << Name << "Transitions[].\n";
11906f32e7eSjoerg 
12006f32e7eSjoerg   SequenceToOffsetTable<DfaTransitionInfo> Table;
12106f32e7eSjoerg   std::map<DfaTransitionInfo, unsigned> EmittedIndices;
12206f32e7eSjoerg   for (auto &T : DfaTransitions)
12306f32e7eSjoerg     Table.add(T.second.second);
12406f32e7eSjoerg   Table.layout();
125*da58b97aSjoerg   OS << "const std::array<NfaStatePair, " << Table.size() << "> " << Name
12606f32e7eSjoerg      << "TransitionInfo = {{\n";
12706f32e7eSjoerg   Table.emit(
12806f32e7eSjoerg       OS,
12906f32e7eSjoerg       [](raw_ostream &OS, std::pair<uint64_t, uint64_t> P) {
13006f32e7eSjoerg         OS << "{" << P.first << ", " << P.second << "}";
13106f32e7eSjoerg       },
13206f32e7eSjoerg       "{0ULL, 0ULL}");
13306f32e7eSjoerg 
13406f32e7eSjoerg   OS << "}};\n\n";
13506f32e7eSjoerg 
13606f32e7eSjoerg   OS << "// A transition in the generated " << Name << " DFA.\n";
13706f32e7eSjoerg   OS << "struct " << Name << "Transition {\n";
13806f32e7eSjoerg   OS << "  unsigned FromDfaState; // The transitioned-from DFA state.\n";
13906f32e7eSjoerg   OS << "  ";
14006f32e7eSjoerg   printActionType(OS);
14106f32e7eSjoerg   OS << " Action;       // The input symbol that causes this transition.\n";
14206f32e7eSjoerg   OS << "  unsigned ToDfaState;   // The transitioned-to DFA state.\n";
14306f32e7eSjoerg   OS << "  unsigned InfoIdx;      // Start index into " << Name
14406f32e7eSjoerg      << "TransitionInfo.\n";
14506f32e7eSjoerg   OS << "};\n\n";
14606f32e7eSjoerg 
14706f32e7eSjoerg   OS << "// A table of DFA transitions, ordered by {FromDfaState, Action}.\n";
14806f32e7eSjoerg   OS << "// The initial state is 1, not zero.\n";
149*da58b97aSjoerg   OS << "const std::array<" << Name << "Transition, "
150*da58b97aSjoerg      << DfaTransitions.size() << "> " << Name << "Transitions = {{\n";
15106f32e7eSjoerg   for (auto &KV : DfaTransitions) {
15206f32e7eSjoerg     dfa_state_type From = KV.first.first;
15306f32e7eSjoerg     dfa_state_type To = KV.second.first;
15406f32e7eSjoerg     action_type A = KV.first.second;
15506f32e7eSjoerg     unsigned InfoIdx = Table.get(KV.second.second);
15606f32e7eSjoerg     OS << "  {" << From << ", ";
15706f32e7eSjoerg     printActionValue(A, OS);
15806f32e7eSjoerg     OS << ", " << To << ", " << InfoIdx << "},\n";
15906f32e7eSjoerg   }
16006f32e7eSjoerg   OS << "\n}};\n\n";
16106f32e7eSjoerg }
16206f32e7eSjoerg 
printActionType(raw_ostream & OS)16306f32e7eSjoerg void DfaEmitter::printActionType(raw_ostream &OS) { OS << "uint64_t"; }
16406f32e7eSjoerg 
printActionValue(action_type A,raw_ostream & OS)16506f32e7eSjoerg void DfaEmitter::printActionValue(action_type A, raw_ostream &OS) { OS << A; }
16606f32e7eSjoerg 
16706f32e7eSjoerg //===----------------------------------------------------------------------===//
16806f32e7eSjoerg // AutomatonEmitter implementation
16906f32e7eSjoerg //===----------------------------------------------------------------------===//
17006f32e7eSjoerg 
17106f32e7eSjoerg namespace {
17206f32e7eSjoerg // FIXME: This entire discriminated union could be removed with c++17:
17306f32e7eSjoerg //   using Action = std::variant<Record *, unsigned, std::string>;
17406f32e7eSjoerg struct Action {
17506f32e7eSjoerg   Record *R = nullptr;
17606f32e7eSjoerg   unsigned I = 0;
177*da58b97aSjoerg   std::string S;
17806f32e7eSjoerg 
17906f32e7eSjoerg   Action() = default;
Action__anonaa8d6ae10211::Action18006f32e7eSjoerg   Action(Record *R, unsigned I, std::string S) : R(R), I(I), S(S) {}
18106f32e7eSjoerg 
print__anonaa8d6ae10211::Action18206f32e7eSjoerg   void print(raw_ostream &OS) const {
18306f32e7eSjoerg     if (R)
18406f32e7eSjoerg       OS << R->getName();
18506f32e7eSjoerg     else if (!S.empty())
18606f32e7eSjoerg       OS << '"' << S << '"';
18706f32e7eSjoerg     else
18806f32e7eSjoerg       OS << I;
18906f32e7eSjoerg   }
operator <__anonaa8d6ae10211::Action19006f32e7eSjoerg   bool operator<(const Action &Other) const {
19106f32e7eSjoerg     return std::make_tuple(R, I, S) <
19206f32e7eSjoerg            std::make_tuple(Other.R, Other.I, Other.S);
19306f32e7eSjoerg   }
19406f32e7eSjoerg };
19506f32e7eSjoerg 
19606f32e7eSjoerg using ActionTuple = std::vector<Action>;
19706f32e7eSjoerg class Automaton;
19806f32e7eSjoerg 
19906f32e7eSjoerg class Transition {
20006f32e7eSjoerg   uint64_t NewState;
20106f32e7eSjoerg   // The tuple of actions that causes this transition.
20206f32e7eSjoerg   ActionTuple Actions;
20306f32e7eSjoerg   // The types of the actions; this is the same across all transitions.
20406f32e7eSjoerg   SmallVector<std::string, 4> Types;
20506f32e7eSjoerg 
20606f32e7eSjoerg public:
20706f32e7eSjoerg   Transition(Record *R, Automaton *Parent);
getActions()20806f32e7eSjoerg   const ActionTuple &getActions() { return Actions; }
getTypes()20906f32e7eSjoerg   SmallVector<std::string, 4> getTypes() { return Types; }
21006f32e7eSjoerg 
21106f32e7eSjoerg   bool canTransitionFrom(uint64_t State);
21206f32e7eSjoerg   uint64_t transitionFrom(uint64_t State);
21306f32e7eSjoerg };
21406f32e7eSjoerg 
21506f32e7eSjoerg class Automaton {
21606f32e7eSjoerg   RecordKeeper &Records;
21706f32e7eSjoerg   Record *R;
21806f32e7eSjoerg   std::vector<Transition> Transitions;
21906f32e7eSjoerg   /// All possible action tuples, uniqued.
22006f32e7eSjoerg   UniqueVector<ActionTuple> Actions;
22106f32e7eSjoerg   /// The fields within each Transition object to find the action symbols.
22206f32e7eSjoerg   std::vector<StringRef> ActionSymbolFields;
22306f32e7eSjoerg 
22406f32e7eSjoerg public:
22506f32e7eSjoerg   Automaton(RecordKeeper &Records, Record *R);
22606f32e7eSjoerg   void emit(raw_ostream &OS);
22706f32e7eSjoerg 
getActionSymbolFields()22806f32e7eSjoerg   ArrayRef<StringRef> getActionSymbolFields() { return ActionSymbolFields; }
22906f32e7eSjoerg   /// If the type of action A has been overridden (there exists a field
23006f32e7eSjoerg   /// "TypeOf_A") return that, otherwise return the empty string.
23106f32e7eSjoerg   StringRef getActionSymbolType(StringRef A);
23206f32e7eSjoerg };
23306f32e7eSjoerg 
23406f32e7eSjoerg class AutomatonEmitter {
23506f32e7eSjoerg   RecordKeeper &Records;
23606f32e7eSjoerg 
23706f32e7eSjoerg public:
AutomatonEmitter(RecordKeeper & R)23806f32e7eSjoerg   AutomatonEmitter(RecordKeeper &R) : Records(R) {}
23906f32e7eSjoerg   void run(raw_ostream &OS);
24006f32e7eSjoerg };
24106f32e7eSjoerg 
24206f32e7eSjoerg /// A DfaEmitter implementation that can print our variant action type.
24306f32e7eSjoerg class CustomDfaEmitter : public DfaEmitter {
24406f32e7eSjoerg   const UniqueVector<ActionTuple> &Actions;
24506f32e7eSjoerg   std::string TypeName;
24606f32e7eSjoerg 
24706f32e7eSjoerg public:
CustomDfaEmitter(const UniqueVector<ActionTuple> & Actions,StringRef TypeName)24806f32e7eSjoerg   CustomDfaEmitter(const UniqueVector<ActionTuple> &Actions, StringRef TypeName)
24906f32e7eSjoerg       : Actions(Actions), TypeName(TypeName) {}
25006f32e7eSjoerg 
25106f32e7eSjoerg   void printActionType(raw_ostream &OS) override;
25206f32e7eSjoerg   void printActionValue(action_type A, raw_ostream &OS) override;
25306f32e7eSjoerg };
25406f32e7eSjoerg } // namespace
25506f32e7eSjoerg 
run(raw_ostream & OS)25606f32e7eSjoerg void AutomatonEmitter::run(raw_ostream &OS) {
25706f32e7eSjoerg   for (Record *R : Records.getAllDerivedDefinitions("GenericAutomaton")) {
25806f32e7eSjoerg     Automaton A(Records, R);
25906f32e7eSjoerg     OS << "#ifdef GET_" << R->getName() << "_DECL\n";
26006f32e7eSjoerg     A.emit(OS);
26106f32e7eSjoerg     OS << "#endif  // GET_" << R->getName() << "_DECL\n";
26206f32e7eSjoerg   }
26306f32e7eSjoerg }
26406f32e7eSjoerg 
Automaton(RecordKeeper & Records,Record * R)26506f32e7eSjoerg Automaton::Automaton(RecordKeeper &Records, Record *R)
26606f32e7eSjoerg     : Records(Records), R(R) {
26706f32e7eSjoerg   LLVM_DEBUG(dbgs() << "Emitting automaton for " << R->getName() << "\n");
26806f32e7eSjoerg   ActionSymbolFields = R->getValueAsListOfStrings("SymbolFields");
26906f32e7eSjoerg }
27006f32e7eSjoerg 
emit(raw_ostream & OS)27106f32e7eSjoerg void Automaton::emit(raw_ostream &OS) {
27206f32e7eSjoerg   StringRef TransitionClass = R->getValueAsString("TransitionClass");
27306f32e7eSjoerg   for (Record *T : Records.getAllDerivedDefinitions(TransitionClass)) {
27406f32e7eSjoerg     assert(T->isSubClassOf("Transition"));
27506f32e7eSjoerg     Transitions.emplace_back(T, this);
27606f32e7eSjoerg     Actions.insert(Transitions.back().getActions());
27706f32e7eSjoerg   }
27806f32e7eSjoerg 
27906f32e7eSjoerg   LLVM_DEBUG(dbgs() << "  Action alphabet cardinality: " << Actions.size()
28006f32e7eSjoerg                     << "\n");
28106f32e7eSjoerg   LLVM_DEBUG(dbgs() << "  Each state has " << Transitions.size()
28206f32e7eSjoerg                     << " potential transitions.\n");
28306f32e7eSjoerg 
28406f32e7eSjoerg   StringRef Name = R->getName();
28506f32e7eSjoerg 
28606f32e7eSjoerg   CustomDfaEmitter Emitter(Actions, std::string(Name) + "Action");
28706f32e7eSjoerg   // Starting from the initial state, build up a list of possible states and
28806f32e7eSjoerg   // transitions.
28906f32e7eSjoerg   std::deque<uint64_t> Worklist(1, 0);
29006f32e7eSjoerg   std::set<uint64_t> SeenStates;
29106f32e7eSjoerg   unsigned NumTransitions = 0;
29206f32e7eSjoerg   SeenStates.insert(Worklist.front());
29306f32e7eSjoerg   while (!Worklist.empty()) {
29406f32e7eSjoerg     uint64_t State = Worklist.front();
29506f32e7eSjoerg     Worklist.pop_front();
29606f32e7eSjoerg     for (Transition &T : Transitions) {
29706f32e7eSjoerg       if (!T.canTransitionFrom(State))
29806f32e7eSjoerg         continue;
29906f32e7eSjoerg       uint64_t NewState = T.transitionFrom(State);
30006f32e7eSjoerg       if (SeenStates.emplace(NewState).second)
30106f32e7eSjoerg         Worklist.emplace_back(NewState);
30206f32e7eSjoerg       ++NumTransitions;
30306f32e7eSjoerg       Emitter.addTransition(State, NewState, Actions.idFor(T.getActions()));
30406f32e7eSjoerg     }
30506f32e7eSjoerg   }
30606f32e7eSjoerg   LLVM_DEBUG(dbgs() << "  NFA automaton has " << SeenStates.size()
30706f32e7eSjoerg                     << " states with " << NumTransitions << " transitions.\n");
30806f32e7eSjoerg 
30906f32e7eSjoerg   const auto &ActionTypes = Transitions.back().getTypes();
31006f32e7eSjoerg   OS << "// The type of an action in the " << Name << " automaton.\n";
31106f32e7eSjoerg   if (ActionTypes.size() == 1) {
31206f32e7eSjoerg     OS << "using " << Name << "Action = " << ActionTypes[0] << ";\n";
31306f32e7eSjoerg   } else {
31406f32e7eSjoerg     OS << "using " << Name << "Action = std::tuple<" << join(ActionTypes, ", ")
31506f32e7eSjoerg        << ">;\n";
31606f32e7eSjoerg   }
31706f32e7eSjoerg   OS << "\n";
31806f32e7eSjoerg 
31906f32e7eSjoerg   Emitter.emit(Name, OS);
32006f32e7eSjoerg }
32106f32e7eSjoerg 
getActionSymbolType(StringRef A)32206f32e7eSjoerg StringRef Automaton::getActionSymbolType(StringRef A) {
32306f32e7eSjoerg   Twine Ty = "TypeOf_" + A;
32406f32e7eSjoerg   if (!R->getValue(Ty.str()))
32506f32e7eSjoerg     return "";
32606f32e7eSjoerg   return R->getValueAsString(Ty.str());
32706f32e7eSjoerg }
32806f32e7eSjoerg 
Transition(Record * R,Automaton * Parent)32906f32e7eSjoerg Transition::Transition(Record *R, Automaton *Parent) {
33006f32e7eSjoerg   BitsInit *NewStateInit = R->getValueAsBitsInit("NewState");
33106f32e7eSjoerg   NewState = 0;
33206f32e7eSjoerg   assert(NewStateInit->getNumBits() <= sizeof(uint64_t) * 8 &&
33306f32e7eSjoerg          "State cannot be represented in 64 bits!");
33406f32e7eSjoerg   for (unsigned I = 0; I < NewStateInit->getNumBits(); ++I) {
33506f32e7eSjoerg     if (auto *Bit = dyn_cast<BitInit>(NewStateInit->getBit(I))) {
33606f32e7eSjoerg       if (Bit->getValue())
33706f32e7eSjoerg         NewState |= 1ULL << I;
33806f32e7eSjoerg     }
33906f32e7eSjoerg   }
34006f32e7eSjoerg 
34106f32e7eSjoerg   for (StringRef A : Parent->getActionSymbolFields()) {
34206f32e7eSjoerg     RecordVal *SymbolV = R->getValue(A);
34306f32e7eSjoerg     if (auto *Ty = dyn_cast<RecordRecTy>(SymbolV->getType())) {
34406f32e7eSjoerg       Actions.emplace_back(R->getValueAsDef(A), 0, "");
34506f32e7eSjoerg       Types.emplace_back(Ty->getAsString());
34606f32e7eSjoerg     } else if (isa<IntRecTy>(SymbolV->getType())) {
34706f32e7eSjoerg       Actions.emplace_back(nullptr, R->getValueAsInt(A), "");
34806f32e7eSjoerg       Types.emplace_back("unsigned");
349*da58b97aSjoerg     } else if (isa<StringRecTy>(SymbolV->getType())) {
350*da58b97aSjoerg       Actions.emplace_back(nullptr, 0, std::string(R->getValueAsString(A)));
35106f32e7eSjoerg       Types.emplace_back("std::string");
35206f32e7eSjoerg     } else {
35306f32e7eSjoerg       report_fatal_error("Unhandled symbol type!");
35406f32e7eSjoerg     }
35506f32e7eSjoerg 
35606f32e7eSjoerg     StringRef TypeOverride = Parent->getActionSymbolType(A);
35706f32e7eSjoerg     if (!TypeOverride.empty())
358*da58b97aSjoerg       Types.back() = std::string(TypeOverride);
35906f32e7eSjoerg   }
36006f32e7eSjoerg }
36106f32e7eSjoerg 
canTransitionFrom(uint64_t State)36206f32e7eSjoerg bool Transition::canTransitionFrom(uint64_t State) {
36306f32e7eSjoerg   if ((State & NewState) == 0)
36406f32e7eSjoerg     // The bits we want to set are not set;
36506f32e7eSjoerg     return true;
36606f32e7eSjoerg   return false;
36706f32e7eSjoerg }
36806f32e7eSjoerg 
transitionFrom(uint64_t State)36906f32e7eSjoerg uint64_t Transition::transitionFrom(uint64_t State) {
37006f32e7eSjoerg   return State | NewState;
37106f32e7eSjoerg }
37206f32e7eSjoerg 
printActionType(raw_ostream & OS)37306f32e7eSjoerg void CustomDfaEmitter::printActionType(raw_ostream &OS) { OS << TypeName; }
37406f32e7eSjoerg 
printActionValue(action_type A,raw_ostream & OS)37506f32e7eSjoerg void CustomDfaEmitter::printActionValue(action_type A, raw_ostream &OS) {
37606f32e7eSjoerg   const ActionTuple &AT = Actions[A];
37706f32e7eSjoerg   if (AT.size() > 1)
37806f32e7eSjoerg     OS << "std::make_tuple(";
379*da58b97aSjoerg   ListSeparator LS;
38006f32e7eSjoerg   for (const auto &SingleAction : AT) {
381*da58b97aSjoerg     OS << LS;
38206f32e7eSjoerg     SingleAction.print(OS);
38306f32e7eSjoerg   }
38406f32e7eSjoerg   if (AT.size() > 1)
38506f32e7eSjoerg     OS << ")";
38606f32e7eSjoerg }
38706f32e7eSjoerg 
38806f32e7eSjoerg namespace llvm {
38906f32e7eSjoerg 
EmitAutomata(RecordKeeper & RK,raw_ostream & OS)39006f32e7eSjoerg void EmitAutomata(RecordKeeper &RK, raw_ostream &OS) {
39106f32e7eSjoerg   AutomatonEmitter(RK).run(OS);
39206f32e7eSjoerg }
39306f32e7eSjoerg 
39406f32e7eSjoerg } // namespace llvm
395