summaryrefslogtreecommitdiff
path: root/BaseTools/Source/C/VfrCompile/Pccts/antlr/egman.c
blob: c8a633fc068a35428c1edf4effe6a4024e5b7885 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * egman.c
 *
 * SOFTWARE RIGHTS
 *
 * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
 * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
 * company may do whatever they wish with source code distributed with
 * PCCTS or the code generated by PCCTS, including the incorporation of
 * PCCTS, or its output, into commerical software.
 *
 * We encourage users to develop software with PCCTS.  However, we do ask
 * that credit is given to us for developing PCCTS.  By "credit",
 * we mean that if you incorporate our source code into one of your
 * programs (commercial product, research project, or otherwise) that you
 * acknowledge this fact somewhere in the documentation, research report,
 * etc...  If you like PCCTS and have developed a nice tool with the
 * output, please mention that you developed it using PCCTS.  In
 * addition, we ask that this header remain intact in our source code.
 * As long as these guidelines are kept, we expect to continue enhancing
 * this system and expect to make other tools available as they are
 * completed.
 *
 * ANTLR 1.33MR10
 * 2001
 *
 */

#include <stdio.h>
#include <stdlib.h>

#include "set.h"
#include "syn.h"
#include "hash.h"
#include "generic.h"
#include "proto.h"

static ExceptionGroup **egArray=NULL;   /* ExceptionGroup by BlkLevel */
static LabelEntry     **leArray=NULL;   /* LabelEntry by BlkLevel     */
static Junction       **altArray=NULL;  /* start of alternates        */
static int              arraySize=0;
static int              highWater=0;
static ExceptionGroup *lastEG=NULL;     /* used in altFixup()         */
static int             lastBlkLevel=0;  /* used in altFixup()         */

#ifdef __USE_PROTOS
static void arrayCheck(void);
#else
static void arrayCheck();
#endif

/* Called to add an exception group for an alternative EG */

#ifdef __USE_PROTOS
void egAdd(ExceptionGroup * eg)
#else
void egAdd(eg)
ExceptionGroup *eg;
#endif
{
  int               i;

  ExceptionGroup    *nextEG;
  ExceptionGroup    *innerEG;

  LabelEntry        *nextLE;
  LabelEntry        *innerLE;

  Junction          *nextAlt;
  Junction          *innerAlt;

  lastEG=eg;
  lastBlkLevel=BlkLevel;

  arrayCheck();
  eg->pendingLink=egArray[BlkLevel];
  egArray[BlkLevel]=eg;

  /* EG for alternates already have their altID filled in      */

  for (i=BlkLevel+1; i<=highWater ; i++) {
    for (innerEG=egArray[i]; innerEG != NULL ; innerEG=nextEG) {
      nextEG=innerEG->pendingLink;
      innerEG->pendingLink=NULL;
      innerEG->outerEG=eg;
    };
    egArray[i]=NULL;
  };

  /*
   *  for patching up the LabelEntry you might use an EG for the
   *  current alternative - unlike patching up an alternative EG
   *    i.e. start the loop at BlkLevel rather than (BlkLevel+1)
   *  fill it in only if the EG and the LE are for the very
   *    same alternative if they're at the same BlkLevel
   *  it's easier to leave the LE on this list (filled in) rather than
   *    trying to selectively remove it.  It will eventually be
   *    removed anyway when the BlkLevel gets small enough.
   */

  for (i=BlkLevel; i<=highWater ; i++) {
    for (innerLE=leArray[i]; innerLE != NULL ; innerLE=nextLE) {
      nextLE=innerLE->pendingLink;
      if (BlkLevel != i ||
        innerLE->curAltNum == CurAltNum_array[BlkLevel]) {
        if (innerLE->outerEG == NULL) {
          innerLE->outerEG=eg;
        };
      };
    };
    if (BlkLevel != i) leArray[i]=NULL;
  };

/*
 * For the start of alternatives it is necessary to make a
 * distinction between the exception group for the current
 * alternative and the "fallback" EG for the block which
 * contains the alternative
 *
 * The fallback outerEG is used to handle the case where
 * no alternative of a block matches.  In that case the
 * signal is "NoViableAlt" (or "NoSemViableAlt" and the
 * generator needs the EG of the block CONTAINING the
 * current one.
 *
 *      rule: ( ( ( a
 *                | b
 *                )
 *              | c
 *              )
 *            | d
 *            );
 */

  for (i=BlkLevel; i <= highWater ; i++) {
    for (innerAlt=altArray[i]; innerAlt != NULL ; innerAlt=nextAlt) {
      nextAlt=innerAlt->pendingLink;

      /*  first fill in the EG for the current alternative         */
      /*  but leave it on the list in order to get the fallback EG */
      /*  if the EG is at the same LEVEL as the alternative then   */
      /*    fill it in only if in the very same alternative        */
      /*                                                           */
      /*        rule: ( a                                          */
      /*              | b                                          */
      /*              | c  exception ...                           */
      /*              )                                            */
      /*                                                           */
      /*  if the EG is outside the alternative (e.g. BlkLevel < i) */
      /*    then it doesn't matter about the alternative           */
      /*                                                           */
      /*        rule: ( a                                          */
      /*              | b                                          */
      /*              | c                                          */
      /*              )   exception ...                            */
      /*                                                           */

#if 0
      printf("BlkLevel=%d i=%d altnum=%d CurAltNum=%d altID=%s\n",
        BlkLevel,i,innerAlt->curAltNum,CurAltNum_array[BlkLevel],eg->altID);
#endif
      if (BlkLevel != i ||
          innerAlt->curAltNum == CurAltNum_array[BlkLevel]) {
        if (innerAlt->exception_label == NULL) {
          innerAlt->exception_label=eg->altID;
        };
      };

      /*  ocurs at a later pass then for the exception_label       */
      /*  if an outerEG has been found then fill in the outer EG   */
      /*  remove if from the list when the BlkLevel gets smaller   */

      if (BlkLevel != i) {
        if (innerAlt->outerEG == NULL) {
          innerAlt->outerEG=eg;
        };
      };
    };
    if (BlkLevel != i) altArray[i]=NULL;
  };
}

#ifdef __USE_PROTOS
void leAdd(LabelEntry * le)
#else
void leAdd(le)
LabelEntry *le;
#endif

{
  arrayCheck();
  le->pendingLink=leArray[BlkLevel];
  le->curAltNum=CurAltNum_array[BlkLevel];
  leArray[BlkLevel]=le;
}

#ifdef __USE_PROTOS
void altAdd(Junction *alt)
#else
void altAdd(alt)
Junction *alt;
#endif

{
  arrayCheck();
#if 0
  printf("BlkLevel=%d CurAltNum=%d\n",
            BlkLevel,CurAltNum_array[BlkLevel]);
#endif
  alt->curAltNum=CurAltNum_array[BlkLevel];
  alt->pendingLink=altArray[BlkLevel];
  altArray[BlkLevel]=alt;
}

static void 
#ifdef __USE_PROTOS
arrayCheck(void)
#else
arrayCheck()
#endif
{
  ExceptionGroup    **egArrayNew;
  LabelEntry        **leArrayNew;
  Junction          **altArrayNew;
  int               arraySizeNew;
  int               i;

  if (BlkLevel > highWater) highWater=BlkLevel;

  if (BlkLevel >= arraySize) {
    arraySizeNew=BlkLevel+5;	/* MR20 */
    egArrayNew=(ExceptionGroup **)
        calloc(arraySizeNew,sizeof(ExceptionGroup *));
    leArrayNew=(LabelEntry **)
        calloc(arraySizeNew,sizeof(LabelEntry *));
    altArrayNew=(Junction **)
        calloc(arraySizeNew,sizeof(Junction *));
    for (i=0; i<arraySize ; i++) {
      egArrayNew[i]=egArray[i];
      leArrayNew[i]=leArray[i];
      altArrayNew[i]=altArray[i];
    };
    arraySize=arraySizeNew;
    if (egArray != NULL) free( (char *) egArray);
    if (leArray != NULL) free( (char *) leArray);
    if (altArray != NULL) free( (char *) altArray);
    egArray=egArrayNew;
    leArray=leArrayNew;
    altArray=altArrayNew;
  };
}

/* always call leFixup() BEFORE egFixup() */

void 
#ifdef __USE_PROTOS
egFixup(void) 
#else
egFixup()
#endif
{
  int               i;
  ExceptionGroup    *nextEG;
  ExceptionGroup    *innerEG;

  for (i=1; i<=highWater ; i++) {
    for (innerEG=egArray[i]; innerEG != NULL ; innerEG=nextEG) {
      nextEG=innerEG->pendingLink;
      innerEG->pendingLink=NULL;
    };
    egArray[i]=NULL;
  };
  lastEG=NULL;
  lastBlkLevel=0;
}

/* always call leFixup() BEFORE egFixup() */

#ifdef __USE_PROTOS
void leFixup(void) 
#else
void leFixup() 
#endif
{

  int               i;
  LabelEntry        *nextLE;
  LabelEntry        *innerLE;

  for (i=BlkLevel; i<=highWater ; i++) {
    for (innerLE=leArray[i]; innerLE != NULL ; innerLE=nextLE) {
      nextLE=innerLE->pendingLink;
      innerLE->pendingLink=NULL;
    };
    leArray[i]=NULL;
  };
}

/* always call altFixup() BEFORE egFixup() */

#ifdef __USE_PROTOS
void altFixup(void)
#else
void altFixup() 
#endif
{

  int               i;
  Junction          *nextAlt;
  Junction          *innerAlt;

  for (i=BlkLevel; i<=highWater ; i++) {
    for (innerAlt=altArray[i]; innerAlt != NULL ; innerAlt=nextAlt) {

      /*  if an outerEG has been found then fill in the outer EG   */

      if (lastBlkLevel <= i) {
        if (innerAlt->outerEG == NULL) {
          innerAlt->outerEG=lastEG;
        };
      };
      nextAlt=innerAlt->pendingLink;
      innerAlt->pendingLink=NULL;
    };
    altArray[i]=NULL;
  };
}