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]
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"
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);
46 static int maxspr
= 0; /* maximum spread of any entry */
47 static int maxoff
= 0; /* maximum offset into an array */
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.
72 "optimizer cannot open tempfile"));
83 temp1
[++nstate
] = (--optimmem
) - tracemem
;
93 error("bad tempfile");
98 temp1
[nstate
] = yypgo
[0] = (--optimmem
) - tracemem
;
104 yypgo
[++nnonter
] = optimmem
-tracemem
;
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'.
124 yypgo
[nnonter
--] = (--optimmem
) - tracemem
;
126 for (i
= 0; i
< nstate
; ++i
) {
129 q
= tracemem
+ temp1
[i
+1];
130 for (p
= tracemem
+ temp1
[i
]; p
< q
; p
+= 2) {
138 * nontrivial situation
139 * temporarily, kill this for compatibility
141 /* j -= k; j is now the range */
145 tystate
[i
] = (temp1
[i
+1] - temp1
[i
]) + 2*j
;
150 /* initialize ggreed table */
151 for (i
= 1; i
<= nnonter
; ++i
) {
154 /* minimum entry index is always 0 */
155 q
= tracemem
+ yypgo
[i
+1] -1;
156 for (p
= tracemem
+ yypgo
[i
]; p
< q
; p
+= 2) {
161 ggreed
[i
] = ggreed
[i
] + 2*j
;
166 /* now, prepare to put the shift actions into the amem array */
167 for (i
= 0; i
< new_actsize
; ++i
)
171 for (i
= 0; i
< nstate
; ++i
) {
172 if (tystate
[i
] == 0 && adb
> 1)
173 (void) fprintf(ftable
, "State %d: null\n", i
);
177 while ((i
= nxti()) != NOMORE
) {
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 */
201 int *r
, *s
, *q1
, *q2
;
204 /* enter gotos on nonterminal i into array amem */
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 ){ */
215 while (p
>= &amem
[new_actsize
])
219 for (r
= q1
; r
< q2
; r
+= 2) {
222 * Check if action table needs to
223 * be expanded or not. If so,
226 while (s
>= &amem
[new_actsize
]) {
233 while ((maxa
= s
) >= &amem
[new_actsize
])
234 /* error( "amem array overflow" ); */
238 /* we have found a spot */
241 while ((maxa
= p
) >= &amem
[new_actsize
])
242 /* error("amem array overflow"); */
245 for (r
= q1
; r
< q2
; r
+= 2) {
248 * Check if action table needs to
249 * be expanded or not. If so,
252 while (s
>= &amem
[new_actsize
]) {
261 (void) fprintf(ftable
,
262 "Nonterminal %d, entry at %d\n", i
, pgo
[i
]);
268 /* error( "cannot place goto %d\n", i ); */
275 int *r
, n
, nn
, flag
, j
, *q1
, *q2
;
280 /* Enter state i into the amem array */
282 q2
= tracemem
+ temp1
[i
+ 1];
283 q1
= tracemem
+ temp1
[i
];
284 /* Find an acceptable place */
288 for (n
= nn
; n
< new_actsize
; ++n
) {
290 for (r
= q1
; r
< q2
; r
+= 2) {
295 * Check if action table needs to
296 * be expanded or not. If so,
299 while (s
>= &amem
[new_actsize
]) {
300 exp_act((int **)NULL
);
310 * check that the position equals another
311 * only if the states are identical
313 for (j
= 0; j
< nstate
; ++j
) {
317 * we have some disagreement.
320 if (temp1
[j
+1] + temp1
[i
] ==
321 temp1
[j
] + temp1
[i
+1]) {
322 /* states are equal */
325 (void) fprintf(ftable
,
327 " %d equals state %d\n",
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
);
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.
351 "clobber of amem array, pos'n %d, by %d"),
357 (void) fprintf(ftable
,
358 "State %d: entry at %d\n", i
, indgo
[i
]);
363 /* error( "Error; failure to place state %d\n", i ); */
364 exp_act((int **)NULL
);
365 nn
= new_actsize
- ACTSIZE
;
373 /* finds the next i */
377 for (i
= 1; i
<= nnonter
; ++i
)
378 if (ggreed
[i
] >= max
) {
383 for (i
= 0; i
< nstate
; ++i
)
384 if (tystate
[i
] >= max
) {
389 (void) fprintf(ftable
, "nxti = %d, max = %d\n", maxi
, max
);
405 for (p
= maxa
; p
>= amem
; --p
) {
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
);
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);
440 (void) fprintf(ftable
, WSFMT("static YYCONST yytabelem %ws[]={\n"), s
);
441 for (i
= 0; i
< n
; ) {
443 (void) fprintf(ftable
, "\n");
444 (void) fprintf(ftable
, "%6d", v
[i
]);
446 (void) fprintf(ftable
, " };\n");
448 (void) fprintf(ftable
, ",");
457 /* read and convert an integer from the standard input */
458 /* return the terminating character */
459 /* blanks, tabs, and newlines are ignored */
464 while ((c
= getwc(finput
)) != EOF
) {
466 val
= val
* 10 + c
- L
'0';
473 if (optimmem
>= &tracemem
[new_memsize
])
484 new_actsize
+= ACTSIZE
;
487 amem
= (int *) realloc((char *)amem
, sizeof (int) * new_actsize
);
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.'
497 "couldn't expand action table"));
499 for (i
= new_actsize
-ACTSIZE
; i
< new_actsize
; ++i
)
502 *ptr
= *ptr
- actbase
+ amem
;
504 memp
= memp
- actbase
+ amem
;
506 maxa
= maxa
- actbase
+ amem
;