dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / sgs / yacc / common / y4.c
blobe9c6b60a6215c5aca3e305d3cd732f55b02a0ddf
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1988 AT&T */
27 /* All Rights Reserved */
29 #pragma ident "%Z%%M% %I% %E% SMI"
31 #include "dextern.h"
32 #define NOMORE -1000
34 static void gin(int);
35 static void stin(int);
36 static void osummary(void);
37 static void aoutput(void);
38 static void arout(wchar_t *, int *, int);
39 static int nxti(void);
40 static int gtnm(void);
42 static int *ggreed;
43 static int *pgo;
44 static int *yypgo;
46 static int maxspr = 0; /* maximum spread of any entry */
47 static int maxoff = 0; /* maximum offset into an array */
48 int *optimmem;
49 static int *maxa;
51 static int nxdb = 0;
52 static int adb = 0;
54 void
55 callopt()
57 int i, *p, j, k, *q;
59 ggreed = (int *) malloc(sizeof (int) * size);
60 pgo = (int *) malloc(sizeof (int) * size);
61 yypgo = &nontrst[0].tvalue;
63 /* read the arrays from tempfile and set parameters */
65 if ((finput = fopen(TEMPNAME, "r")) == NULL)
67 * TRANSLATION_NOTE -- This is a message from yacc.
68 * This message is passed to error() function.
69 * tempfile can be translated as temporary file.
71 error(gettext(
72 "optimizer cannot open tempfile"));
74 optimmem = tracemem;
75 pgo[0] = 0;
76 temp1[0] = 0;
77 nstate = 0;
78 nnonter = 0;
79 for (;;) {
80 switch (gtnm()) {
82 case L'\n':
83 temp1[++nstate] = (--optimmem) - tracemem;
84 /* FALLTHRU */
86 case L',':
87 continue;
89 case L'$':
90 break;
92 default:
93 error("bad tempfile");
95 break;
98 temp1[nstate] = yypgo[0] = (--optimmem) - tracemem;
100 for (;;) {
101 switch (gtnm()) {
103 case L'\n':
104 yypgo[++nnonter] = optimmem-tracemem;
105 /* FALLTHRU */
106 case L',':
107 continue;
109 case EOF:
110 break;
112 default:
114 * TRANSLATION_NOTE -- This is a message from yacc.
115 * This message is passed to error() function.
116 * tempfile can be translated as 'temporary file'.
118 error(gettext(
119 "bad tempfile"));
121 break;
124 yypgo[nnonter--] = (--optimmem) - tracemem;
126 for (i = 0; i < nstate; ++i) {
127 k = 32000000;
128 j = 0;
129 q = tracemem + temp1[i+1];
130 for (p = tracemem + temp1[i]; p < q; p += 2) {
131 if (*p > j)
132 j = *p;
133 if (*p < k)
134 k = *p;
136 if (k <= j) {
138 * nontrivial situation
139 * temporarily, kill this for compatibility
141 /* j -= k; j is now the range */
142 if (k > maxoff)
143 maxoff = k;
145 tystate[i] = (temp1[i+1] - temp1[i]) + 2*j;
146 if (j > maxspr)
147 maxspr = j;
150 /* initialize ggreed table */
151 for (i = 1; i <= nnonter; ++i) {
152 ggreed[i] = 1;
153 j = 0;
154 /* minimum entry index is always 0 */
155 q = tracemem + yypgo[i+1] -1;
156 for (p = tracemem + yypgo[i]; p < q; p += 2) {
157 ggreed[i] += 2;
158 if (*p > j)
159 j = *p;
161 ggreed[i] = ggreed[i] + 2*j;
162 if (j > maxoff)
163 maxoff = j;
166 /* now, prepare to put the shift actions into the amem array */
167 for (i = 0; i < new_actsize; ++i)
168 amem[i] = 0;
169 maxa = amem;
171 for (i = 0; i < nstate; ++i) {
172 if (tystate[i] == 0 && adb > 1)
173 (void) fprintf(ftable, "State %d: null\n", i);
174 indgo[i] = YYFLAG1;
177 while ((i = nxti()) != NOMORE) {
178 if (i >= 0)
179 stin(i);
180 else
181 gin(-i);
184 if (adb > 2) { /* print a array */
185 for (p = amem; p <= maxa; p += 10) {
186 (void) fprintf(ftable, "%4" PRIdPTR " ", p-amem);
187 for (i = 0; i < 10; ++i)
188 (void) fprintf(ftable, "%4d ", p[i]);
189 (void) fprintf(ftable, "\n");
192 /* write out the output appropriate to the language */
193 aoutput();
194 osummary();
195 ZAPFILE(TEMPNAME);
198 static void
199 gin(int i)
201 int *r, *s, *q1, *q2;
202 int *p;
204 /* enter gotos on nonterminal i into array amem */
205 ggreed[i] = 0;
207 q2 = tracemem + yypgo[i+1] - 1;
208 q1 = tracemem + yypgo[i];
210 /* now, find a place for it */
212 /* for( p=amem; p < &amem[new_actsize]; ++p ){ */
213 p = amem;
214 for (;;) {
215 while (p >= &amem[new_actsize])
216 exp_act(&p);
217 if (*p)
218 goto nextgp;
219 for (r = q1; r < q2; r += 2) {
220 s = p + *r + 1;
222 * Check if action table needs to
223 * be expanded or not. If so,
224 * expand it.
226 while (s >= &amem[new_actsize]) {
227 exp_act(&p);
228 s = p + *r + 1;
230 if (*s)
231 goto nextgp;
232 if (s > maxa) {
233 while ((maxa = s) >= &amem[new_actsize])
234 /* error( "amem array overflow" ); */
235 exp_act(&p);
238 /* we have found a spot */
239 *p = *q2;
240 if (p > maxa) {
241 while ((maxa = p) >= &amem[new_actsize])
242 /* error("amem array overflow"); */
243 exp_act(&p);
245 for (r = q1; r < q2; r += 2) {
246 s = p + *r + 1;
248 * Check if action table needs to
249 * be expanded or not. If so,
250 * expand it.
252 while (s >= &amem[new_actsize]) {
253 exp_act(&p);
254 s = p + *r + 1;
256 *s = r[1];
259 pgo[i] = p - amem;
260 if (adb > 1)
261 (void) fprintf(ftable,
262 "Nonterminal %d, entry at %d\n", i, pgo[i]);
263 goto nextgi;
265 nextgp:
266 ++p;
268 /* error( "cannot place goto %d\n", i ); */
269 nextgi:;
272 static void
273 stin(int i)
275 int *r, n, nn, flag, j, *q1, *q2;
276 int *s;
278 tystate[i] = 0;
280 /* Enter state i into the amem array */
282 q2 = tracemem + temp1[i + 1];
283 q1 = tracemem + temp1[i];
284 /* Find an acceptable place */
286 nn = -maxoff;
287 more:
288 for (n = nn; n < new_actsize; ++n) {
289 flag = 0;
290 for (r = q1; r < q2; r += 2) {
291 s = *r + n + amem;
292 if (s < amem)
293 goto nextn;
295 * Check if action table needs to
296 * be expanded or not. If so,
297 * expand it.
299 while (s >= &amem[new_actsize]) {
300 exp_act((int **)NULL);
301 s = *r + n + amem;
303 if (*s == 0)
304 ++flag;
305 else if (*s != r[1])
306 goto nextn;
310 * check that the position equals another
311 * only if the states are identical
313 for (j = 0; j < nstate; ++j) {
314 if (indgo[j] == n) {
315 if (flag)
317 * we have some disagreement.
319 goto nextn;
320 if (temp1[j+1] + temp1[i] ==
321 temp1[j] + temp1[i+1]) {
322 /* states are equal */
323 indgo[i] = n;
324 if (adb > 1)
325 (void) fprintf(ftable,
326 "State %d: entry at"
327 " %d equals state %d\n",
328 i, n, j);
329 return;
331 goto nextn; /* we have some disagreement */
335 for (r = q1; r < q2; r += 2) {
336 while ((s = *r + n + amem) >= &amem[new_actsize]) {
338 * error( "out of space");
340 exp_act((int **)NULL);
342 if (s > maxa)
343 maxa = s;
344 if (*s != 0 && *s != r[1])
346 * TRANSLATION_NOTE -- This is a message from yacc.
347 * This message is passed to error() function.
348 * Leave this untrasnlated. Yacc internal error.
350 error(gettext(
351 "clobber of amem array, pos'n %d, by %d"),
352 s-amem, r[1]);
353 *s = r[1];
355 indgo[i] = n;
356 if (adb > 1)
357 (void) fprintf(ftable,
358 "State %d: entry at %d\n", i, indgo[i]);
359 return;
360 nextn:;
363 /* error( "Error; failure to place state %d\n", i ); */
364 exp_act((int **)NULL);
365 nn = new_actsize - ACTSIZE;
366 goto more;
367 /* NOTREACHED */
370 static int
371 nxti()
373 /* finds the next i */
374 int i, max, maxi;
375 max = 0;
377 for (i = 1; i <= nnonter; ++i)
378 if (ggreed[i] >= max) {
379 max = ggreed[i];
380 maxi = -i;
383 for (i = 0; i < nstate; ++i)
384 if (tystate[i] >= max) {
385 max = tystate[i];
386 maxi = i;
388 if (nxdb)
389 (void) fprintf(ftable, "nxti = %d, max = %d\n", maxi, max);
390 if (max == 0)
391 return (NOMORE);
392 else
393 return (maxi);
396 static void
397 osummary()
399 /* write summary */
400 int i, *p;
402 if (foutput == NULL)
403 return;
404 i = 0;
405 for (p = maxa; p >= amem; --p) {
406 if (*p == 0)
407 ++i;
410 (void) fprintf(foutput,
411 "Optimizer space used: input %" PRIdPTR
412 "/%d, output %" PRIdPTR "/%d\n",
413 optimmem-tracemem + 1, new_memsize, maxa-amem + 1, new_actsize);
414 (void) fprintf(foutput,
415 "%" PRIdPTR " table entries, %d zero\n", (maxa-amem) + 1, i);
416 (void) fprintf(foutput,
417 "maximum spread: %d, maximum offset: %d\n", maxspr, maxoff);
421 static void
422 aoutput()
424 /* this version is for C */
425 /* write out the optimized parser */
427 (void) fprintf(ftable, "# define YYLAST %" PRIdPTR "\n", maxa-amem + 1);
428 arout(L"yyact", amem, (maxa - amem) + 1);
429 arout(L"yypact", indgo, nstate);
430 arout(L"yypgo", pgo, nnonter + 1);
433 static void
434 arout(s, v, n)
435 wchar_t *s;
436 int *v, n;
438 int i;
440 (void) fprintf(ftable, WSFMT("static YYCONST yytabelem %ws[]={\n"), s);
441 for (i = 0; i < n; ) {
442 if (i % 10 == 0)
443 (void) fprintf(ftable, "\n");
444 (void) fprintf(ftable, "%6d", v[i]);
445 if (++i == n)
446 (void) fprintf(ftable, " };\n");
447 else
448 (void) fprintf(ftable, ",");
452 static int
453 gtnm()
455 int s, val, c;
457 /* read and convert an integer from the standard input */
458 /* return the terminating character */
459 /* blanks, tabs, and newlines are ignored */
461 s = 1;
462 val = 0;
464 while ((c = getwc(finput)) != EOF) {
465 if (iswdigit(c))
466 val = val * 10 + c - L'0';
467 else if (c == L'-')
468 s = -1;
469 else
470 break;
472 *optimmem++ = s*val;
473 if (optimmem >= &tracemem[new_memsize])
474 exp_mem(0);
475 return (c);
478 void
479 exp_act(ptr)
480 int **ptr;
482 static int *actbase;
483 int i;
484 new_actsize += ACTSIZE;
486 actbase = amem;
487 amem = (int *) realloc((char *)amem, sizeof (int) * new_actsize);
488 if (amem == NULL)
490 * TRANSLATION_NOTE -- This is a message from yacc.
491 * This message is passed to error() function.
493 * You may just translate this as:
494 * 'Could not allocate internally used memory.'
496 error(gettext(
497 "couldn't expand action table"));
499 for (i = new_actsize-ACTSIZE; i < new_actsize; ++i)
500 amem[i] = 0;
501 if (ptr != NULL)
502 *ptr = *ptr - actbase + amem;
503 if (memp >= amem)
504 memp = memp - actbase + amem;
505 if (maxa >= amem)
506 maxa = maxa - actbase + amem;