Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Parser accelerator module */ |
| 3 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4 | /* The parser as originally conceived had disappointing performance. |
| 5 | This module does some precomputation that speeds up the selection |
| 6 | of a DFA based upon a token, turning a search through an array |
| 7 | into a simple indexing operation. The parser now cannot work |
| 8 | without the accelerators installed. Note that the accelerators |
| 9 | are installed dynamically when the parser is initialized, they |
| 10 | are not part of the static data structure written on graminit.[ch] |
| 11 | by the parser generator. */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 13 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 14 | #include "grammar.h" |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 15 | #include "node.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 16 | #include "token.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 17 | #include "parser.h" |
| 18 | |
| 19 | /* Forward references */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 20 | static void fixdfa(grammar *, dfa *); |
| 21 | static void fixstate(grammar *, state *); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 22 | |
| 23 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 24 | PyGrammar_AddAccelerators(grammar *g) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 25 | { |
| 26 | dfa *d; |
| 27 | int i; |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 28 | #ifdef Py_DEBUG |
Guido van Rossum | 888d205 | 1992-09-03 20:45:24 +0000 | [diff] [blame] | 29 | fprintf(stderr, "Adding parser accelerators ...\n"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 30 | #endif |
| 31 | d = g->g_dfa; |
| 32 | for (i = g->g_ndfas; --i >= 0; d++) |
| 33 | fixdfa(g, d); |
| 34 | g->g_accel = 1; |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 35 | #ifdef Py_DEBUG |
Guido van Rossum | 888d205 | 1992-09-03 20:45:24 +0000 | [diff] [blame] | 36 | fprintf(stderr, "Done.\n"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 37 | #endif |
| 38 | } |
| 39 | |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 40 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 41 | PyGrammar_RemoveAccelerators(grammar *g) |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 42 | { |
| 43 | dfa *d; |
| 44 | int i; |
| 45 | g->g_accel = 0; |
| 46 | d = g->g_dfa; |
| 47 | for (i = g->g_ndfas; --i >= 0; d++) { |
| 48 | state *s; |
| 49 | int j; |
| 50 | s = d->d_state; |
| 51 | for (j = 0; j < d->d_nstates; j++, s++) { |
| 52 | if (s->s_accel) |
| 53 | PyMem_DEL(s->s_accel); |
| 54 | s->s_accel = NULL; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 59 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 60 | fixdfa(grammar *g, dfa *d) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 61 | { |
| 62 | state *s; |
| 63 | int j; |
| 64 | s = d->d_state; |
| 65 | for (j = 0; j < d->d_nstates; j++, s++) |
Guido van Rossum | 9abc539 | 1992-03-27 17:24:37 +0000 | [diff] [blame] | 66 | fixstate(g, s); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 67 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 68 | |
| 69 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 70 | fixstate(grammar *g, state *s) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 71 | { |
| 72 | arc *a; |
| 73 | int k; |
| 74 | int *accel; |
| 75 | int nl = g->g_ll.ll_nlabels; |
| 76 | s->s_accept = 0; |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 77 | accel = PyMem_NEW(int, nl); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 78 | for (k = 0; k < nl; k++) |
| 79 | accel[k] = -1; |
| 80 | a = s->s_arc; |
| 81 | for (k = s->s_narcs; --k >= 0; a++) { |
| 82 | int lbl = a->a_lbl; |
| 83 | label *l = &g->g_ll.ll_label[lbl]; |
| 84 | int type = l->lb_type; |
| 85 | if (a->a_arrow >= (1 << 7)) { |
| 86 | printf("XXX too many states!\n"); |
| 87 | continue; |
| 88 | } |
| 89 | if (ISNONTERMINAL(type)) { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 90 | dfa *d1 = PyGrammar_FindDFA(g, type); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 91 | int ibit; |
| 92 | if (type - NT_OFFSET >= (1 << 7)) { |
| 93 | printf("XXX too high nonterminal number!\n"); |
| 94 | continue; |
| 95 | } |
| 96 | for (ibit = 0; ibit < g->g_ll.ll_nlabels; ibit++) { |
| 97 | if (testbit(d1->d_first, ibit)) { |
Guido van Rossum | 7d8b509 | 1991-09-10 14:53:39 +0000 | [diff] [blame] | 98 | #ifdef applec |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 99 | #define MPW_881_BUG /* Undefine if bug below is fixed */ |
Guido van Rossum | 7d8b509 | 1991-09-10 14:53:39 +0000 | [diff] [blame] | 100 | #endif |
| 101 | #ifdef MPW_881_BUG |
| 102 | /* In 881 mode MPW 3.1 has a code |
| 103 | generation bug which seems to |
| 104 | set the upper bits; fix this by |
| 105 | explicitly masking them off */ |
| 106 | int temp; |
| 107 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 108 | if (accel[ibit] != -1) |
| 109 | printf("XXX ambiguity!\n"); |
Guido van Rossum | 7d8b509 | 1991-09-10 14:53:39 +0000 | [diff] [blame] | 110 | #ifdef MPW_881_BUG |
| 111 | temp = 0xFFFF & |
| 112 | (a->a_arrow | (1 << 7) | |
| 113 | ((type - NT_OFFSET) << 8)); |
| 114 | accel[ibit] = temp; |
| 115 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 116 | accel[ibit] = a->a_arrow | (1 << 7) | |
| 117 | ((type - NT_OFFSET) << 8); |
Guido van Rossum | 7d8b509 | 1991-09-10 14:53:39 +0000 | [diff] [blame] | 118 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | } |
| 122 | else if (lbl == EMPTY) |
| 123 | s->s_accept = 1; |
| 124 | else if (lbl >= 0 && lbl < nl) |
| 125 | accel[lbl] = a->a_arrow; |
| 126 | } |
| 127 | while (nl > 0 && accel[nl-1] == -1) |
| 128 | nl--; |
| 129 | for (k = 0; k < nl && accel[k] == -1;) |
| 130 | k++; |
| 131 | if (k < nl) { |
| 132 | int i; |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 133 | s->s_accel = PyMem_NEW(int, nl-k); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 134 | if (s->s_accel == NULL) { |
| 135 | fprintf(stderr, "no mem to add parser accelerators\n"); |
| 136 | exit(1); |
| 137 | } |
| 138 | s->s_lower = k; |
| 139 | s->s_upper = nl; |
| 140 | for (i = 0; k < nl; i++, k++) |
| 141 | s->s_accel[i] = accel[k]; |
| 142 | } |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 143 | PyMem_DEL(accel); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 144 | } |