Automaton.td 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //===- Automaton.td ----------------------------------------*- tablegen -*-===//
  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. //
  9. // This file defines the key top-level classes needed to produce a reasonably
  10. // generic finite-state automaton.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. // Define a record inheriting from GenericAutomaton to generate a reasonably
  14. // generic finite-state automaton over a set of actions and states.
  15. //
  16. // This automaton is defined by:
  17. // 1) a state space (explicit, always bits<32>).
  18. // 2) a set of input symbols (actions, explicit) and
  19. // 3) a transition function from state + action -> state.
  20. //
  21. // A theoretical automaton is defined by <Q, S, d, q0, F>:
  22. // Q: A set of possible states.
  23. // S: (sigma) The input alphabet.
  24. // d: (delta) The transition function f(q in Q, s in S) -> q' in Q.
  25. // F: The set of final (accepting) states.
  26. //
  27. // Because generating all possible states is tedious, we instead define the
  28. // transition function only and crawl all reachable states starting from the
  29. // initial state with all inputs under all transitions until termination.
  30. //
  31. // We define F = S, that is, all valid states are accepting.
  32. //
  33. // To ensure the generation of the automaton terminates, the state transitions
  34. // are defined as a lattice (meaning every transitioned-to state is more
  35. // specific than the transitioned-from state, for some definition of specificity).
  36. // Concretely a transition may set one or more bits in the state that were
  37. // previously zero to one. If any bit was not zero, the transition is invalid.
  38. //
  39. // Instead of defining all possible states (which would be cumbersome), the user
  40. // provides a set of possible Transitions from state A, consuming an input
  41. // symbol A to state B. The Transition object transforms state A to state B and
  42. // acts as a predicate. This means the state space can be discovered by crawling
  43. // all the possible transitions until none are valid.
  44. //
  45. // This automaton is considered to be nondeterministic, meaning that multiple
  46. // transitions can occur from any (state, action) pair. The generated automaton
  47. // is determinized, meaning that is executes in O(k) time where k is the input
  48. // sequence length.
  49. //
  50. // In addition to a generated automaton that determines if a sequence of inputs
  51. // is accepted or not, a table is emitted that allows determining a plausible
  52. // sequence of states traversed to accept that input.
  53. class GenericAutomaton {
  54. // Name of a class that inherits from Transition. All records inheriting from
  55. // this class will be considered when constructing the automaton.
  56. string TransitionClass;
  57. // Names of fields within TransitionClass that define the action symbol. This
  58. // defines the action as an N-tuple.
  59. //
  60. // Each symbol field can be of class, int, string or code type.
  61. // If the type of a field is a class, the Record's name is used verbatim
  62. // in C++ and the class name is used as the C++ type name.
  63. // If the type of a field is a string, code or int, that is also used
  64. // verbatim in C++.
  65. //
  66. // To override the C++ type name for field F, define a field called TypeOf_F.
  67. // This should be a string that will be used verbatim in C++.
  68. //
  69. // As an example, to define a 2-tuple with an enum and a string, one might:
  70. // def MyTransition : Transition {
  71. // MyEnum S1;
  72. // int S2;
  73. // }
  74. // def MyAutomaton : GenericAutomaton }{
  75. // let TransitionClass = "Transition";
  76. // let SymbolFields = ["S1", "S2"];
  77. // let TypeOf_S1 = "MyEnumInCxxKind";
  78. // }
  79. list<string> SymbolFields;
  80. }
  81. // All transitions inherit from Transition.
  82. class Transition {
  83. // A transition S' = T(S) is valid if, for every set bit in NewState, the
  84. // corresponding bit in S is clear. That is:
  85. // def T(S):
  86. // S' = S | NewState
  87. // return S' if S' != S else Failure
  88. //
  89. // The automaton generator uses this property to crawl the set of possible
  90. // transitions from a starting state of 0b0.
  91. bits<32> NewState;
  92. }