blob: 177bb49fe41bd44715e7b058934f814879bdcb9d [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009******************************************************************/
10
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011/* Parser accelerator module */
12
Guido van Rossum3f5da241990-12-20 15:06:42 +000013/* The parser as originally conceived had disappointing performance.
14 This module does some precomputation that speeds up the selection
15 of a DFA based upon a token, turning a search through an array
16 into a simple indexing operation. The parser now cannot work
17 without the accelerators installed. Note that the accelerators
18 are installed dynamically when the parser is initialized, they
19 are not part of the static data structure written on graminit.[ch]
20 by the parser generator. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000021
Guido van Rossum3f5da241990-12-20 15:06:42 +000022#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000023#include "grammar.h"
Guido van Rossum1d5735e1994-08-30 08:27:36 +000024#include "node.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025#include "token.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000026#include "parser.h"
27
28/* Forward references */
Tim Petersdbd9ba62000-07-09 03:09:57 +000029static void fixdfa(grammar *, dfa *);
30static void fixstate(grammar *, state *);
Guido van Rossum3f5da241990-12-20 15:06:42 +000031
32void
Guido van Rossum86bea461997-04-29 21:03:06 +000033PyGrammar_AddAccelerators(g)
Guido van Rossum3f5da241990-12-20 15:06:42 +000034 grammar *g;
35{
36 dfa *d;
37 int i;
Guido van Rossum408027e1996-12-30 16:17:54 +000038#ifdef Py_DEBUG
Guido van Rossum888d2051992-09-03 20:45:24 +000039 fprintf(stderr, "Adding parser accelerators ...\n");
Guido van Rossum3f5da241990-12-20 15:06:42 +000040#endif
41 d = g->g_dfa;
42 for (i = g->g_ndfas; --i >= 0; d++)
43 fixdfa(g, d);
44 g->g_accel = 1;
Guido van Rossum408027e1996-12-30 16:17:54 +000045#ifdef Py_DEBUG
Guido van Rossum888d2051992-09-03 20:45:24 +000046 fprintf(stderr, "Done.\n");
Guido van Rossum3f5da241990-12-20 15:06:42 +000047#endif
48}
49
Guido van Rossumaee094c1997-08-02 03:02:27 +000050void
51PyGrammar_RemoveAccelerators(g)
52 grammar *g;
53{
54 dfa *d;
55 int i;
56 g->g_accel = 0;
57 d = g->g_dfa;
58 for (i = g->g_ndfas; --i >= 0; d++) {
59 state *s;
60 int j;
61 s = d->d_state;
62 for (j = 0; j < d->d_nstates; j++, s++) {
63 if (s->s_accel)
64 PyMem_DEL(s->s_accel);
65 s->s_accel = NULL;
66 }
67 }
68}
69
Guido van Rossum3f5da241990-12-20 15:06:42 +000070static void
71fixdfa(g, d)
72 grammar *g;
73 dfa *d;
74{
75 state *s;
76 int j;
77 s = d->d_state;
78 for (j = 0; j < d->d_nstates; j++, s++)
Guido van Rossum9abc5391992-03-27 17:24:37 +000079 fixstate(g, s);
Guido van Rossum3f5da241990-12-20 15:06:42 +000080}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000081
82static void
Guido van Rossum9abc5391992-03-27 17:24:37 +000083fixstate(g, s)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084 grammar *g;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085 state *s;
86{
87 arc *a;
88 int k;
89 int *accel;
90 int nl = g->g_ll.ll_nlabels;
91 s->s_accept = 0;
Guido van Rossum86bea461997-04-29 21:03:06 +000092 accel = PyMem_NEW(int, nl);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093 for (k = 0; k < nl; k++)
94 accel[k] = -1;
95 a = s->s_arc;
96 for (k = s->s_narcs; --k >= 0; a++) {
97 int lbl = a->a_lbl;
98 label *l = &g->g_ll.ll_label[lbl];
99 int type = l->lb_type;
100 if (a->a_arrow >= (1 << 7)) {
101 printf("XXX too many states!\n");
102 continue;
103 }
104 if (ISNONTERMINAL(type)) {
Guido van Rossum86bea461997-04-29 21:03:06 +0000105 dfa *d1 = PyGrammar_FindDFA(g, type);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106 int ibit;
107 if (type - NT_OFFSET >= (1 << 7)) {
108 printf("XXX too high nonterminal number!\n");
109 continue;
110 }
111 for (ibit = 0; ibit < g->g_ll.ll_nlabels; ibit++) {
112 if (testbit(d1->d_first, ibit)) {
Guido van Rossum7d8b5091991-09-10 14:53:39 +0000113#ifdef applec
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000114#define MPW_881_BUG /* Undefine if bug below is fixed */
Guido van Rossum7d8b5091991-09-10 14:53:39 +0000115#endif
116#ifdef MPW_881_BUG
117 /* In 881 mode MPW 3.1 has a code
118 generation bug which seems to
119 set the upper bits; fix this by
120 explicitly masking them off */
121 int temp;
122#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123 if (accel[ibit] != -1)
124 printf("XXX ambiguity!\n");
Guido van Rossum7d8b5091991-09-10 14:53:39 +0000125#ifdef MPW_881_BUG
126 temp = 0xFFFF &
127 (a->a_arrow | (1 << 7) |
128 ((type - NT_OFFSET) << 8));
129 accel[ibit] = temp;
130#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131 accel[ibit] = a->a_arrow | (1 << 7) |
132 ((type - NT_OFFSET) << 8);
Guido van Rossum7d8b5091991-09-10 14:53:39 +0000133#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134 }
135 }
136 }
137 else if (lbl == EMPTY)
138 s->s_accept = 1;
139 else if (lbl >= 0 && lbl < nl)
140 accel[lbl] = a->a_arrow;
141 }
142 while (nl > 0 && accel[nl-1] == -1)
143 nl--;
144 for (k = 0; k < nl && accel[k] == -1;)
145 k++;
146 if (k < nl) {
147 int i;
Guido van Rossum86bea461997-04-29 21:03:06 +0000148 s->s_accel = PyMem_NEW(int, nl-k);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149 if (s->s_accel == NULL) {
150 fprintf(stderr, "no mem to add parser accelerators\n");
151 exit(1);
152 }
153 s->s_lower = k;
154 s->s_upper = nl;
155 for (i = 0; k < nl; i++, k++)
156 s->s_accel[i] = accel[k];
157 }
Guido van Rossum86bea461997-04-29 21:03:06 +0000158 PyMem_DEL(accel);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159}