blob: 1b789e7e836927d27b41b24850221cf867916f58 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Parser accelerator module */
3
Guido van Rossum3f5da241990-12-20 15:06:42 +00004/* 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 Rossum85a5fbb1990-10-14 12:07:46 +000012
Guido van Rossum3f5da241990-12-20 15:06:42 +000013#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014#include "grammar.h"
Guido van Rossum1d5735e1994-08-30 08:27:36 +000015#include "node.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000016#include "token.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000017#include "parser.h"
18
19/* Forward references */
Tim Petersdbd9ba62000-07-09 03:09:57 +000020static void fixdfa(grammar *, dfa *);
21static void fixstate(grammar *, state *);
Guido van Rossum3f5da241990-12-20 15:06:42 +000022
23void
Thomas Wouters23c9e002000-07-22 19:20:54 +000024PyGrammar_AddAccelerators(grammar *g)
Guido van Rossum3f5da241990-12-20 15:06:42 +000025{
26 dfa *d;
27 int i;
Guido van Rossum408027e1996-12-30 16:17:54 +000028#ifdef Py_DEBUG
Guido van Rossum888d2051992-09-03 20:45:24 +000029 fprintf(stderr, "Adding parser accelerators ...\n");
Guido van Rossum3f5da241990-12-20 15:06:42 +000030#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 Rossum408027e1996-12-30 16:17:54 +000035#ifdef Py_DEBUG
Guido van Rossum888d2051992-09-03 20:45:24 +000036 fprintf(stderr, "Done.\n");
Guido van Rossum3f5da241990-12-20 15:06:42 +000037#endif
38}
39
Guido van Rossumaee094c1997-08-02 03:02:27 +000040void
Thomas Wouters23c9e002000-07-22 19:20:54 +000041PyGrammar_RemoveAccelerators(grammar *g)
Guido van Rossumaee094c1997-08-02 03:02:27 +000042{
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 Rossum3f5da241990-12-20 15:06:42 +000059static void
Thomas Wouters23c9e002000-07-22 19:20:54 +000060fixdfa(grammar *g, dfa *d)
Guido van Rossum3f5da241990-12-20 15:06:42 +000061{
62 state *s;
63 int j;
64 s = d->d_state;
65 for (j = 0; j < d->d_nstates; j++, s++)
Guido van Rossum9abc5391992-03-27 17:24:37 +000066 fixstate(g, s);
Guido van Rossum3f5da241990-12-20 15:06:42 +000067}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068
69static void
Thomas Wouters23c9e002000-07-22 19:20:54 +000070fixstate(grammar *g, state *s)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071{
72 arc *a;
73 int k;
74 int *accel;
75 int nl = g->g_ll.ll_nlabels;
76 s->s_accept = 0;
Guido van Rossum86bea461997-04-29 21:03:06 +000077 accel = PyMem_NEW(int, nl);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078 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 Rossum86bea461997-04-29 21:03:06 +000090 dfa *d1 = PyGrammar_FindDFA(g, type);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000091 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 Rossum7d8b5091991-09-10 14:53:39 +000098#ifdef applec
Guido van Rossum1d5735e1994-08-30 08:27:36 +000099#define MPW_881_BUG /* Undefine if bug below is fixed */
Guido van Rossum7d8b5091991-09-10 14:53:39 +0000100#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 Rossum85a5fbb1990-10-14 12:07:46 +0000108 if (accel[ibit] != -1)
109 printf("XXX ambiguity!\n");
Guido van Rossum7d8b5091991-09-10 14:53:39 +0000110#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 Rossum85a5fbb1990-10-14 12:07:46 +0000116 accel[ibit] = a->a_arrow | (1 << 7) |
117 ((type - NT_OFFSET) << 8);
Guido van Rossum7d8b5091991-09-10 14:53:39 +0000118#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119 }
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 Rossum86bea461997-04-29 21:03:06 +0000133 s->s_accel = PyMem_NEW(int, nl-k);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134 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 Rossum86bea461997-04-29 21:03:06 +0000143 PyMem_DEL(accel);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144}