18bcb0991SDimitry Andric //===- DFAEmitter.cpp - Finite state automaton emitter --------------------===// 28bcb0991SDimitry Andric // 38bcb0991SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 48bcb0991SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 58bcb0991SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 68bcb0991SDimitry Andric // 78bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 88bcb0991SDimitry Andric // 98bcb0991SDimitry Andric // This class can produce a generic deterministic finite state automaton (DFA), 108bcb0991SDimitry Andric // given a set of possible states and transitions. 118bcb0991SDimitry Andric // 128bcb0991SDimitry Andric // The input transitions can be nondeterministic - this class will produce the 138bcb0991SDimitry Andric // deterministic equivalent state machine. 148bcb0991SDimitry Andric // 158bcb0991SDimitry Andric // The generated code can run the DFA and produce an accepted / not accepted 168bcb0991SDimitry Andric // state and also produce, given a sequence of transitions that results in an 178bcb0991SDimitry Andric // accepted state, the sequence of intermediate states. This is useful if the 188bcb0991SDimitry Andric // initial automaton was nondeterministic - it allows mapping back from the DFA 198bcb0991SDimitry Andric // to the NFA. 208bcb0991SDimitry Andric // 218bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 228bcb0991SDimitry Andric #define DEBUG_TYPE "dfa-emitter" 238bcb0991SDimitry Andric 248bcb0991SDimitry Andric #include "DFAEmitter.h" 258bcb0991SDimitry Andric #include "CodeGenTarget.h" 268bcb0991SDimitry Andric #include "SequenceToOffsetTable.h" 278bcb0991SDimitry Andric #include "TableGenBackends.h" 288bcb0991SDimitry Andric #include "llvm/ADT/SmallVector.h" 298bcb0991SDimitry Andric #include "llvm/ADT/StringExtras.h" 308bcb0991SDimitry Andric #include "llvm/ADT/UniqueVector.h" 318bcb0991SDimitry Andric #include "llvm/Support/Debug.h" 328bcb0991SDimitry Andric #include "llvm/Support/raw_ostream.h" 338bcb0991SDimitry Andric #include "llvm/TableGen/Record.h" 348bcb0991SDimitry Andric #include "llvm/TableGen/TableGenBackend.h" 358bcb0991SDimitry Andric #include <cassert> 368bcb0991SDimitry Andric #include <cstdint> 378bcb0991SDimitry Andric #include <map> 388bcb0991SDimitry Andric #include <set> 398bcb0991SDimitry Andric #include <string> 408bcb0991SDimitry Andric #include <vector> 418bcb0991SDimitry Andric 428bcb0991SDimitry Andric using namespace llvm; 438bcb0991SDimitry Andric 448bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 458bcb0991SDimitry Andric // DfaEmitter implementation. This is independent of the GenAutomaton backend. 468bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 478bcb0991SDimitry Andric 488bcb0991SDimitry Andric void DfaEmitter::addTransition(state_type From, state_type To, action_type A) { 498bcb0991SDimitry Andric Actions.insert(A); 508bcb0991SDimitry Andric NfaStates.insert(From); 518bcb0991SDimitry Andric NfaStates.insert(To); 528bcb0991SDimitry Andric NfaTransitions[{From, A}].push_back(To); 538bcb0991SDimitry Andric ++NumNfaTransitions; 548bcb0991SDimitry Andric } 558bcb0991SDimitry Andric 5647395794SDimitry Andric void DfaEmitter::visitDfaState(const DfaState &DS) { 578bcb0991SDimitry Andric // For every possible action... 588bcb0991SDimitry Andric auto FromId = DfaStates.idFor(DS); 598bcb0991SDimitry Andric for (action_type A : Actions) { 608bcb0991SDimitry Andric DfaState NewStates; 618bcb0991SDimitry Andric DfaTransitionInfo TI; 628bcb0991SDimitry Andric // For every represented state, word pair in the original NFA... 6347395794SDimitry Andric for (state_type FromState : DS) { 648bcb0991SDimitry Andric // If this action is possible from this state add the transitioned-to 658bcb0991SDimitry Andric // states to NewStates. 668bcb0991SDimitry Andric auto I = NfaTransitions.find({FromState, A}); 678bcb0991SDimitry Andric if (I == NfaTransitions.end()) 688bcb0991SDimitry Andric continue; 698bcb0991SDimitry Andric for (state_type &ToState : I->second) { 708bcb0991SDimitry Andric NewStates.push_back(ToState); 718bcb0991SDimitry Andric TI.emplace_back(FromState, ToState); 728bcb0991SDimitry Andric } 738bcb0991SDimitry Andric } 748bcb0991SDimitry Andric if (NewStates.empty()) 758bcb0991SDimitry Andric continue; 768bcb0991SDimitry Andric // Sort and unique. 778bcb0991SDimitry Andric sort(NewStates); 788bcb0991SDimitry Andric NewStates.erase(std::unique(NewStates.begin(), NewStates.end()), 798bcb0991SDimitry Andric NewStates.end()); 808bcb0991SDimitry Andric sort(TI); 818bcb0991SDimitry Andric TI.erase(std::unique(TI.begin(), TI.end()), TI.end()); 828bcb0991SDimitry Andric unsigned ToId = DfaStates.insert(NewStates); 838bcb0991SDimitry Andric DfaTransitions.emplace(std::make_pair(FromId, A), std::make_pair(ToId, TI)); 848bcb0991SDimitry Andric } 858bcb0991SDimitry Andric } 868bcb0991SDimitry Andric 878bcb0991SDimitry Andric void DfaEmitter::constructDfa() { 888bcb0991SDimitry Andric DfaState Initial(1, /*NFA initial state=*/0); 898bcb0991SDimitry Andric DfaStates.insert(Initial); 908bcb0991SDimitry Andric 918bcb0991SDimitry Andric // Note that UniqueVector starts indices at 1, not zero. 928bcb0991SDimitry Andric unsigned DfaStateId = 1; 9347395794SDimitry Andric while (DfaStateId <= DfaStates.size()) { 9447395794SDimitry Andric DfaState S = DfaStates[DfaStateId]; 9547395794SDimitry Andric visitDfaState(S); 9647395794SDimitry Andric DfaStateId++; 9747395794SDimitry Andric } 988bcb0991SDimitry Andric } 998bcb0991SDimitry Andric 1008bcb0991SDimitry Andric void DfaEmitter::emit(StringRef Name, raw_ostream &OS) { 1018bcb0991SDimitry Andric constructDfa(); 1028bcb0991SDimitry Andric 1038bcb0991SDimitry Andric OS << "// Input NFA has " << NfaStates.size() << " states with " 1048bcb0991SDimitry Andric << NumNfaTransitions << " transitions.\n"; 1058bcb0991SDimitry Andric OS << "// Generated DFA has " << DfaStates.size() << " states with " 1068bcb0991SDimitry Andric << DfaTransitions.size() << " transitions.\n\n"; 1078bcb0991SDimitry Andric 1088bcb0991SDimitry Andric // Implementation note: We don't bake a simple std::pair<> here as it requires 1098bcb0991SDimitry Andric // significantly more effort to parse. A simple test with a large array of 1108bcb0991SDimitry Andric // struct-pairs (N=100000) took clang-10 6s to parse. The same array of 1118bcb0991SDimitry Andric // std::pair<uint64_t, uint64_t> took 242s. Instead we allow the user to 1128bcb0991SDimitry Andric // define the pair type. 1138bcb0991SDimitry Andric // 1148bcb0991SDimitry Andric // FIXME: It may make sense to emit these as ULEB sequences instead of 1158bcb0991SDimitry Andric // pairs of uint64_t. 1168bcb0991SDimitry Andric OS << "// A zero-terminated sequence of NFA state transitions. Every DFA\n"; 1178bcb0991SDimitry Andric OS << "// transition implies a set of NFA transitions. These are referred\n"; 1188bcb0991SDimitry Andric OS << "// to by index in " << Name << "Transitions[].\n"; 1198bcb0991SDimitry Andric 1208bcb0991SDimitry Andric SequenceToOffsetTable<DfaTransitionInfo> Table; 1218bcb0991SDimitry Andric std::map<DfaTransitionInfo, unsigned> EmittedIndices; 1228bcb0991SDimitry Andric for (auto &T : DfaTransitions) 1238bcb0991SDimitry Andric Table.add(T.second.second); 1248bcb0991SDimitry Andric Table.layout(); 125*5ffd83dbSDimitry Andric OS << "const std::array<NfaStatePair, " << Table.size() << "> " << Name 1268bcb0991SDimitry Andric << "TransitionInfo = {{\n"; 1278bcb0991SDimitry Andric Table.emit( 1288bcb0991SDimitry Andric OS, 1298bcb0991SDimitry Andric [](raw_ostream &OS, std::pair<uint64_t, uint64_t> P) { 1308bcb0991SDimitry Andric OS << "{" << P.first << ", " << P.second << "}"; 1318bcb0991SDimitry Andric }, 1328bcb0991SDimitry Andric "{0ULL, 0ULL}"); 1338bcb0991SDimitry Andric 1348bcb0991SDimitry Andric OS << "}};\n\n"; 1358bcb0991SDimitry Andric 1368bcb0991SDimitry Andric OS << "// A transition in the generated " << Name << " DFA.\n"; 1378bcb0991SDimitry Andric OS << "struct " << Name << "Transition {\n"; 1388bcb0991SDimitry Andric OS << " unsigned FromDfaState; // The transitioned-from DFA state.\n"; 1398bcb0991SDimitry Andric OS << " "; 1408bcb0991SDimitry Andric printActionType(OS); 1418bcb0991SDimitry Andric OS << " Action; // The input symbol that causes this transition.\n"; 1428bcb0991SDimitry Andric OS << " unsigned ToDfaState; // The transitioned-to DFA state.\n"; 1438bcb0991SDimitry Andric OS << " unsigned InfoIdx; // Start index into " << Name 1448bcb0991SDimitry Andric << "TransitionInfo.\n"; 1458bcb0991SDimitry Andric OS << "};\n\n"; 1468bcb0991SDimitry Andric 1478bcb0991SDimitry Andric OS << "// A table of DFA transitions, ordered by {FromDfaState, Action}.\n"; 1488bcb0991SDimitry Andric OS << "// The initial state is 1, not zero.\n"; 149*5ffd83dbSDimitry Andric OS << "const std::array<" << Name << "Transition, " 150*5ffd83dbSDimitry Andric << DfaTransitions.size() << "> " << Name << "Transitions = {{\n"; 1518bcb0991SDimitry Andric for (auto &KV : DfaTransitions) { 1528bcb0991SDimitry Andric dfa_state_type From = KV.first.first; 1538bcb0991SDimitry Andric dfa_state_type To = KV.second.first; 1548bcb0991SDimitry Andric action_type A = KV.first.second; 1558bcb0991SDimitry Andric unsigned InfoIdx = Table.get(KV.second.second); 1568bcb0991SDimitry Andric OS << " {" << From << ", "; 1578bcb0991SDimitry Andric printActionValue(A, OS); 1588bcb0991SDimitry Andric OS << ", " << To << ", " << InfoIdx << "},\n"; 1598bcb0991SDimitry Andric } 1608bcb0991SDimitry Andric OS << "\n}};\n\n"; 1618bcb0991SDimitry Andric } 1628bcb0991SDimitry Andric 1638bcb0991SDimitry Andric void DfaEmitter::printActionType(raw_ostream &OS) { OS << "uint64_t"; } 1648bcb0991SDimitry Andric 1658bcb0991SDimitry Andric void DfaEmitter::printActionValue(action_type A, raw_ostream &OS) { OS << A; } 1668bcb0991SDimitry Andric 1678bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 1688bcb0991SDimitry Andric // AutomatonEmitter implementation 1698bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 1708bcb0991SDimitry Andric 1718bcb0991SDimitry Andric namespace { 1728bcb0991SDimitry Andric // FIXME: This entire discriminated union could be removed with c++17: 1738bcb0991SDimitry Andric // using Action = std::variant<Record *, unsigned, std::string>; 1748bcb0991SDimitry Andric struct Action { 1758bcb0991SDimitry Andric Record *R = nullptr; 1768bcb0991SDimitry Andric unsigned I = 0; 1778bcb0991SDimitry Andric std::string S = nullptr; 1788bcb0991SDimitry Andric 1798bcb0991SDimitry Andric Action() = default; 1808bcb0991SDimitry Andric Action(Record *R, unsigned I, std::string S) : R(R), I(I), S(S) {} 1818bcb0991SDimitry Andric 1828bcb0991SDimitry Andric void print(raw_ostream &OS) const { 1838bcb0991SDimitry Andric if (R) 1848bcb0991SDimitry Andric OS << R->getName(); 1858bcb0991SDimitry Andric else if (!S.empty()) 1868bcb0991SDimitry Andric OS << '"' << S << '"'; 1878bcb0991SDimitry Andric else 1888bcb0991SDimitry Andric OS << I; 1898bcb0991SDimitry Andric } 1908bcb0991SDimitry Andric bool operator<(const Action &Other) const { 1918bcb0991SDimitry Andric return std::make_tuple(R, I, S) < 1928bcb0991SDimitry Andric std::make_tuple(Other.R, Other.I, Other.S); 1938bcb0991SDimitry Andric } 1948bcb0991SDimitry Andric }; 1958bcb0991SDimitry Andric 1968bcb0991SDimitry Andric using ActionTuple = std::vector<Action>; 1978bcb0991SDimitry Andric class Automaton; 1988bcb0991SDimitry Andric 1998bcb0991SDimitry Andric class Transition { 2008bcb0991SDimitry Andric uint64_t NewState; 2018bcb0991SDimitry Andric // The tuple of actions that causes this transition. 2028bcb0991SDimitry Andric ActionTuple Actions; 2038bcb0991SDimitry Andric // The types of the actions; this is the same across all transitions. 2048bcb0991SDimitry Andric SmallVector<std::string, 4> Types; 2058bcb0991SDimitry Andric 2068bcb0991SDimitry Andric public: 2078bcb0991SDimitry Andric Transition(Record *R, Automaton *Parent); 2088bcb0991SDimitry Andric const ActionTuple &getActions() { return Actions; } 2098bcb0991SDimitry Andric SmallVector<std::string, 4> getTypes() { return Types; } 2108bcb0991SDimitry Andric 2118bcb0991SDimitry Andric bool canTransitionFrom(uint64_t State); 2128bcb0991SDimitry Andric uint64_t transitionFrom(uint64_t State); 2138bcb0991SDimitry Andric }; 2148bcb0991SDimitry Andric 2158bcb0991SDimitry Andric class Automaton { 2168bcb0991SDimitry Andric RecordKeeper &Records; 2178bcb0991SDimitry Andric Record *R; 2188bcb0991SDimitry Andric std::vector<Transition> Transitions; 2198bcb0991SDimitry Andric /// All possible action tuples, uniqued. 2208bcb0991SDimitry Andric UniqueVector<ActionTuple> Actions; 2218bcb0991SDimitry Andric /// The fields within each Transition object to find the action symbols. 2228bcb0991SDimitry Andric std::vector<StringRef> ActionSymbolFields; 2238bcb0991SDimitry Andric 2248bcb0991SDimitry Andric public: 2258bcb0991SDimitry Andric Automaton(RecordKeeper &Records, Record *R); 2268bcb0991SDimitry Andric void emit(raw_ostream &OS); 2278bcb0991SDimitry Andric 2288bcb0991SDimitry Andric ArrayRef<StringRef> getActionSymbolFields() { return ActionSymbolFields; } 2298bcb0991SDimitry Andric /// If the type of action A has been overridden (there exists a field 2308bcb0991SDimitry Andric /// "TypeOf_A") return that, otherwise return the empty string. 2318bcb0991SDimitry Andric StringRef getActionSymbolType(StringRef A); 2328bcb0991SDimitry Andric }; 2338bcb0991SDimitry Andric 2348bcb0991SDimitry Andric class AutomatonEmitter { 2358bcb0991SDimitry Andric RecordKeeper &Records; 2368bcb0991SDimitry Andric 2378bcb0991SDimitry Andric public: 2388bcb0991SDimitry Andric AutomatonEmitter(RecordKeeper &R) : Records(R) {} 2398bcb0991SDimitry Andric void run(raw_ostream &OS); 2408bcb0991SDimitry Andric }; 2418bcb0991SDimitry Andric 2428bcb0991SDimitry Andric /// A DfaEmitter implementation that can print our variant action type. 2438bcb0991SDimitry Andric class CustomDfaEmitter : public DfaEmitter { 2448bcb0991SDimitry Andric const UniqueVector<ActionTuple> &Actions; 2458bcb0991SDimitry Andric std::string TypeName; 2468bcb0991SDimitry Andric 2478bcb0991SDimitry Andric public: 2488bcb0991SDimitry Andric CustomDfaEmitter(const UniqueVector<ActionTuple> &Actions, StringRef TypeName) 2498bcb0991SDimitry Andric : Actions(Actions), TypeName(TypeName) {} 2508bcb0991SDimitry Andric 2518bcb0991SDimitry Andric void printActionType(raw_ostream &OS) override; 2528bcb0991SDimitry Andric void printActionValue(action_type A, raw_ostream &OS) override; 2538bcb0991SDimitry Andric }; 2548bcb0991SDimitry Andric } // namespace 2558bcb0991SDimitry Andric 2568bcb0991SDimitry Andric void AutomatonEmitter::run(raw_ostream &OS) { 2578bcb0991SDimitry Andric for (Record *R : Records.getAllDerivedDefinitions("GenericAutomaton")) { 2588bcb0991SDimitry Andric Automaton A(Records, R); 2598bcb0991SDimitry Andric OS << "#ifdef GET_" << R->getName() << "_DECL\n"; 2608bcb0991SDimitry Andric A.emit(OS); 2618bcb0991SDimitry Andric OS << "#endif // GET_" << R->getName() << "_DECL\n"; 2628bcb0991SDimitry Andric } 2638bcb0991SDimitry Andric } 2648bcb0991SDimitry Andric 2658bcb0991SDimitry Andric Automaton::Automaton(RecordKeeper &Records, Record *R) 2668bcb0991SDimitry Andric : Records(Records), R(R) { 2678bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Emitting automaton for " << R->getName() << "\n"); 2688bcb0991SDimitry Andric ActionSymbolFields = R->getValueAsListOfStrings("SymbolFields"); 2698bcb0991SDimitry Andric } 2708bcb0991SDimitry Andric 2718bcb0991SDimitry Andric void Automaton::emit(raw_ostream &OS) { 2728bcb0991SDimitry Andric StringRef TransitionClass = R->getValueAsString("TransitionClass"); 2738bcb0991SDimitry Andric for (Record *T : Records.getAllDerivedDefinitions(TransitionClass)) { 2748bcb0991SDimitry Andric assert(T->isSubClassOf("Transition")); 2758bcb0991SDimitry Andric Transitions.emplace_back(T, this); 2768bcb0991SDimitry Andric Actions.insert(Transitions.back().getActions()); 2778bcb0991SDimitry Andric } 2788bcb0991SDimitry Andric 2798bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << " Action alphabet cardinality: " << Actions.size() 2808bcb0991SDimitry Andric << "\n"); 2818bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << " Each state has " << Transitions.size() 2828bcb0991SDimitry Andric << " potential transitions.\n"); 2838bcb0991SDimitry Andric 2848bcb0991SDimitry Andric StringRef Name = R->getName(); 2858bcb0991SDimitry Andric 2868bcb0991SDimitry Andric CustomDfaEmitter Emitter(Actions, std::string(Name) + "Action"); 2878bcb0991SDimitry Andric // Starting from the initial state, build up a list of possible states and 2888bcb0991SDimitry Andric // transitions. 2898bcb0991SDimitry Andric std::deque<uint64_t> Worklist(1, 0); 2908bcb0991SDimitry Andric std::set<uint64_t> SeenStates; 2918bcb0991SDimitry Andric unsigned NumTransitions = 0; 2928bcb0991SDimitry Andric SeenStates.insert(Worklist.front()); 2938bcb0991SDimitry Andric while (!Worklist.empty()) { 2948bcb0991SDimitry Andric uint64_t State = Worklist.front(); 2958bcb0991SDimitry Andric Worklist.pop_front(); 2968bcb0991SDimitry Andric for (Transition &T : Transitions) { 2978bcb0991SDimitry Andric if (!T.canTransitionFrom(State)) 2988bcb0991SDimitry Andric continue; 2998bcb0991SDimitry Andric uint64_t NewState = T.transitionFrom(State); 3008bcb0991SDimitry Andric if (SeenStates.emplace(NewState).second) 3018bcb0991SDimitry Andric Worklist.emplace_back(NewState); 3028bcb0991SDimitry Andric ++NumTransitions; 3038bcb0991SDimitry Andric Emitter.addTransition(State, NewState, Actions.idFor(T.getActions())); 3048bcb0991SDimitry Andric } 3058bcb0991SDimitry Andric } 3068bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << " NFA automaton has " << SeenStates.size() 3078bcb0991SDimitry Andric << " states with " << NumTransitions << " transitions.\n"); 3088bcb0991SDimitry Andric 3098bcb0991SDimitry Andric const auto &ActionTypes = Transitions.back().getTypes(); 3108bcb0991SDimitry Andric OS << "// The type of an action in the " << Name << " automaton.\n"; 3118bcb0991SDimitry Andric if (ActionTypes.size() == 1) { 3128bcb0991SDimitry Andric OS << "using " << Name << "Action = " << ActionTypes[0] << ";\n"; 3138bcb0991SDimitry Andric } else { 3148bcb0991SDimitry Andric OS << "using " << Name << "Action = std::tuple<" << join(ActionTypes, ", ") 3158bcb0991SDimitry Andric << ">;\n"; 3168bcb0991SDimitry Andric } 3178bcb0991SDimitry Andric OS << "\n"; 3188bcb0991SDimitry Andric 3198bcb0991SDimitry Andric Emitter.emit(Name, OS); 3208bcb0991SDimitry Andric } 3218bcb0991SDimitry Andric 3228bcb0991SDimitry Andric StringRef Automaton::getActionSymbolType(StringRef A) { 3238bcb0991SDimitry Andric Twine Ty = "TypeOf_" + A; 3248bcb0991SDimitry Andric if (!R->getValue(Ty.str())) 3258bcb0991SDimitry Andric return ""; 3268bcb0991SDimitry Andric return R->getValueAsString(Ty.str()); 3278bcb0991SDimitry Andric } 3288bcb0991SDimitry Andric 3298bcb0991SDimitry Andric Transition::Transition(Record *R, Automaton *Parent) { 3308bcb0991SDimitry Andric BitsInit *NewStateInit = R->getValueAsBitsInit("NewState"); 3318bcb0991SDimitry Andric NewState = 0; 3328bcb0991SDimitry Andric assert(NewStateInit->getNumBits() <= sizeof(uint64_t) * 8 && 3338bcb0991SDimitry Andric "State cannot be represented in 64 bits!"); 3348bcb0991SDimitry Andric for (unsigned I = 0; I < NewStateInit->getNumBits(); ++I) { 3358bcb0991SDimitry Andric if (auto *Bit = dyn_cast<BitInit>(NewStateInit->getBit(I))) { 3368bcb0991SDimitry Andric if (Bit->getValue()) 3378bcb0991SDimitry Andric NewState |= 1ULL << I; 3388bcb0991SDimitry Andric } 3398bcb0991SDimitry Andric } 3408bcb0991SDimitry Andric 3418bcb0991SDimitry Andric for (StringRef A : Parent->getActionSymbolFields()) { 3428bcb0991SDimitry Andric RecordVal *SymbolV = R->getValue(A); 3438bcb0991SDimitry Andric if (auto *Ty = dyn_cast<RecordRecTy>(SymbolV->getType())) { 3448bcb0991SDimitry Andric Actions.emplace_back(R->getValueAsDef(A), 0, ""); 3458bcb0991SDimitry Andric Types.emplace_back(Ty->getAsString()); 3468bcb0991SDimitry Andric } else if (isa<IntRecTy>(SymbolV->getType())) { 3478bcb0991SDimitry Andric Actions.emplace_back(nullptr, R->getValueAsInt(A), ""); 3488bcb0991SDimitry Andric Types.emplace_back("unsigned"); 3498bcb0991SDimitry Andric } else if (isa<StringRecTy>(SymbolV->getType()) || 3508bcb0991SDimitry Andric isa<CodeRecTy>(SymbolV->getType())) { 351*5ffd83dbSDimitry Andric Actions.emplace_back(nullptr, 0, std::string(R->getValueAsString(A))); 3528bcb0991SDimitry Andric Types.emplace_back("std::string"); 3538bcb0991SDimitry Andric } else { 3548bcb0991SDimitry Andric report_fatal_error("Unhandled symbol type!"); 3558bcb0991SDimitry Andric } 3568bcb0991SDimitry Andric 3578bcb0991SDimitry Andric StringRef TypeOverride = Parent->getActionSymbolType(A); 3588bcb0991SDimitry Andric if (!TypeOverride.empty()) 359*5ffd83dbSDimitry Andric Types.back() = std::string(TypeOverride); 3608bcb0991SDimitry Andric } 3618bcb0991SDimitry Andric } 3628bcb0991SDimitry Andric 3638bcb0991SDimitry Andric bool Transition::canTransitionFrom(uint64_t State) { 3648bcb0991SDimitry Andric if ((State & NewState) == 0) 3658bcb0991SDimitry Andric // The bits we want to set are not set; 3668bcb0991SDimitry Andric return true; 3678bcb0991SDimitry Andric return false; 3688bcb0991SDimitry Andric } 3698bcb0991SDimitry Andric 3708bcb0991SDimitry Andric uint64_t Transition::transitionFrom(uint64_t State) { 3718bcb0991SDimitry Andric return State | NewState; 3728bcb0991SDimitry Andric } 3738bcb0991SDimitry Andric 3748bcb0991SDimitry Andric void CustomDfaEmitter::printActionType(raw_ostream &OS) { OS << TypeName; } 3758bcb0991SDimitry Andric 3768bcb0991SDimitry Andric void CustomDfaEmitter::printActionValue(action_type A, raw_ostream &OS) { 3778bcb0991SDimitry Andric const ActionTuple &AT = Actions[A]; 3788bcb0991SDimitry Andric if (AT.size() > 1) 3798bcb0991SDimitry Andric OS << "std::make_tuple("; 3808bcb0991SDimitry Andric bool First = true; 3818bcb0991SDimitry Andric for (const auto &SingleAction : AT) { 3828bcb0991SDimitry Andric if (!First) 3838bcb0991SDimitry Andric OS << ", "; 3848bcb0991SDimitry Andric First = false; 3858bcb0991SDimitry Andric SingleAction.print(OS); 3868bcb0991SDimitry Andric } 3878bcb0991SDimitry Andric if (AT.size() > 1) 3888bcb0991SDimitry Andric OS << ")"; 3898bcb0991SDimitry Andric } 3908bcb0991SDimitry Andric 3918bcb0991SDimitry Andric namespace llvm { 3928bcb0991SDimitry Andric 3938bcb0991SDimitry Andric void EmitAutomata(RecordKeeper &RK, raw_ostream &OS) { 3948bcb0991SDimitry Andric AutomatonEmitter(RK).run(OS); 3958bcb0991SDimitry Andric } 3968bcb0991SDimitry Andric 3978bcb0991SDimitry Andric } // namespace llvm 398