DFAEmitter.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //===--------------------- DfaEmitter.h -----------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // Defines a generic automaton builder. This takes a set of transitions and
  9. // states that represent a nondeterministic finite state automaton (NFA) and
  10. // emits a determinized DFA in a form that include/llvm/Support/Automaton.h can
  11. // drive.
  12. //
  13. // See file llvm/TableGen/Automaton.td for the TableGen API definition.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #ifndef LLVM_UTILS_TABLEGEN_DFAEMITTER_H
  17. #define LLVM_UTILS_TABLEGEN_DFAEMITTER_H
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ADT/UniqueVector.h"
  20. #include <map>
  21. #include <set>
  22. namespace llvm {
  23. class raw_ostream;
  24. class StringRef;
  25. /// Construct a deterministic finite state automaton from possible
  26. /// nondeterministic state and transition data.
  27. ///
  28. /// The state type is a 64-bit unsigned integer. The generated automaton is
  29. /// invariant to the sparsity of the state representation - its size is only
  30. /// a function of the cardinality of the set of states.
  31. ///
  32. /// The inputs to this emitter are considered to define a nondeterministic
  33. /// finite state automaton (NFA). This is then converted to a DFA during
  34. /// emission. The emitted tables can be used to by
  35. /// include/llvm/Support/Automaton.h.
  36. class DfaEmitter {
  37. public:
  38. // The type of an NFA state. The initial state is always zero.
  39. using state_type = uint64_t;
  40. // The type of an action.
  41. using action_type = uint64_t;
  42. DfaEmitter() = default;
  43. virtual ~DfaEmitter() = default;
  44. void addTransition(state_type From, state_type To, action_type A);
  45. void emit(StringRef Name, raw_ostream &OS);
  46. protected:
  47. /// Emit the C++ type of an action to OS.
  48. virtual void printActionType(raw_ostream &OS);
  49. /// Emit the C++ value of an action A to OS.
  50. virtual void printActionValue(action_type A, raw_ostream &OS);
  51. private:
  52. /// The state type of deterministic states. These are only used internally to
  53. /// this class. This is an ID into the DfaStates UniqueVector.
  54. using dfa_state_type = unsigned;
  55. /// The actual representation of a DFA state, which is a union of one or more
  56. /// NFA states.
  57. using DfaState = SmallVector<state_type, 4>;
  58. /// A DFA transition consists of a set of NFA states transitioning to a
  59. /// new set of NFA states. The DfaTransitionInfo tracks, for every
  60. /// transitioned-from NFA state, a set of valid transitioned-to states.
  61. ///
  62. /// Emission of this transition relation allows algorithmic determination of
  63. /// the possible candidate NFA paths taken under a given input sequence to
  64. /// reach a given DFA state.
  65. using DfaTransitionInfo = SmallVector<std::pair<state_type, state_type>, 4>;
  66. /// The set of all possible actions.
  67. std::set<action_type> Actions;
  68. /// The set of nondeterministic transitions. A state-action pair can
  69. /// transition to multiple target states.
  70. std::map<std::pair<state_type, action_type>, std::vector<state_type>>
  71. NfaTransitions;
  72. std::set<state_type> NfaStates;
  73. unsigned NumNfaTransitions = 0;
  74. /// The set of deterministic states. DfaStates.getId(DfaState) returns an ID,
  75. /// which is dfa_state_type. Note that because UniqueVector reserves state
  76. /// zero, the initial DFA state is always 1.
  77. UniqueVector<DfaState> DfaStates;
  78. /// The set of deterministic transitions. A state-action pair has only a
  79. /// single target state.
  80. std::map<std::pair<dfa_state_type, action_type>,
  81. std::pair<dfa_state_type, DfaTransitionInfo>>
  82. DfaTransitions;
  83. /// Visit all NFA states and construct the DFA.
  84. void constructDfa();
  85. /// Visit a single DFA state and construct all possible transitions to new DFA
  86. /// states.
  87. void visitDfaState(const DfaState &DS);
  88. };
  89. } // namespace llvm
  90. #endif