aboutsummaryrefslogtreecommitdiff
path: root/gcc/treelang/tree1.c
blob: 5067bea22286c4e17010636c4af8716c5216cd20 (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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
  /* 

    TREELANG Compiler almost main (tree1)
    Called by GCC's toplev.c

    Copyright (C) 1986, 87, 89, 92-96, 1997, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.

    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by the
    Free Software Foundation; either version 2, or (at your option) any
    later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.

    In other words, you are welcome to use, share and improve this program.
    You are forbidden to forbid anyone else to use, share and improve
    what you give them.   Help stamp out software-hoarding!  

    ---------------------------------------------------------------------------

    Written by Tim Josling 1999, 2000, 2001, based in part on other
    parts of the GCC compiler.

*/

#include "config.h"
#include "system.h"
#include "ansidecl.h"
#include "flags.h"
#include "output.h"
#include "toplev.h"

#include "ggc.h"
#include "tree.h"
#include "diagnostic.h"

#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>

#include "treelang.h"
#include "treetree.h"

extern int yyparse (void);
/* Linked list of symbols - all must be unique in treelang.  */

struct production *symbol_table = NULL;

/* Language for usage for messages.  */

const char *const language_string = "TREELANG - sample front end for GCC ";

/* Local prototypes.  */

void version (void);

/* GC routine for symbol table.  */
static void symbol_table_ggc (void *m);

/* Global variables.  */

extern struct cbl_tree_struct_parse_tree_top* parse_tree_top;

/* 
   Options. 
*/

/* Trace the parser.  */
unsigned int option_parser_trace = 0;

/* Trace the lexical analysis.  */

unsigned int option_lexer_trace = 0;

/* Warning levels.  */

/* Local variables.  */

unsigned char *in_fname = NULL;	/* Input file name.  */

/* This is 1 if we have output the version string.  */

static int version_done = 0;

/* Variable nesting level.  */

static unsigned int work_nesting_level = 0;

/* Process one switch - called by toplev.c.  */

int
treelang_decode_option (num_options_left, first_option_left)
     int num_options_left ATTRIBUTE_UNUSED; 
     char** first_option_left;
{
  
  /*
    Process options - bear in mind I may get options that are really
    meant for someone else (eg the main compiler) so I have to be very
    permissive. 
    
  */
  
  if (first_option_left[0][0] != '-')
    return 0; 
  
  switch (first_option_left[0][1]) 
    {
    case '-':
      if (!strcmp (first_option_left[0],"--help"))
        {
          if (!version_done)
            {
              fputs (language_string, stdout);
              fputs (version_string, stdout);
              fputs ("\n", stdout);
              version_done = 1;
            }
          fprintf (stdout, "Usage: tree1 [switches] -o output input\n");
          return 1;
        }
    case 'v':
      if (!strcmp (first_option_left[0],"-v"))
        {
          if (!version_done)
            {
              fputs (language_string, stdout);
              fputs (version_string, stdout);
              fputs ("\n", stdout);
              version_done = 1;
            }
          return 1;
        }
    case 'y':
      if (!strcmp (first_option_left[0],"-y"))
        {
          option_lexer_trace = 1;
          option_parser_trace = 1;
          return 1;
        }
    case 'f':
      if (!strcmp (first_option_left[0],"-fparser-trace"))
        {
          option_parser_trace = 1;
          return 1;
        }
      if (!strcmp (first_option_left[0],"-flexer-trace"))
        {
          option_lexer_trace = 1;
          return 1;
        }
      return 0;

    case 'w':
      if (!strcmp (first_option_left[0],"-w"))
        {
          /* Tolerate this option but ignore it - we always put out
             all warnings.  */
          return 1;
        }
      return 0;

    case 'W':
      if (!strcmp (first_option_left[0],"-Wall"))
        {
          return 1;
        }
      return 0;

    default:
      return 0;
    }

  return 0;

}

/* Language dependent parser setup.  */

const char*
treelang_init (const char* filename)
{

  /* Define my garbage collection routines.  */
  ggc_add_root (&symbol_table, 1, 
                /* Unused size.  */ sizeof (void*), symbol_table_ggc);
  /* Note: only storage that has to be kept across functions needs to
     be protected from GC.  */
  /* Define my garbage collection routines.  */
  ggc_add_root (&symbol_table, 1, 
                /* Unused size.  */ sizeof (void*), symbol_table_ggc);
  /* Note: only storage that has to be kept across functions needs to
     be protected from GC.  */

  /* Set up the declarations needed for this front end.  */

  input_filename = "";
  lineno = 0;

  treelang_init_decl_processing ();

  /* This error will not happen from GCC as it will always create a
     fake input file.  */
  if (!filename || (filename[0] == ' ') || (!filename[0])) 
    {
      if (!version_done)
        {
          fprintf (stderr, "No input file specified, try --help for help\n");
          exit (1);
        }

      in_fname = NULL;
      return NULL;
    }
  yyin = fopen (filename, "r");
  if (!yyin)
    {
      fprintf (stderr, "Unable to open input file %s\n", filename);
      exit (1);
    }
  input_filename = filename;
  return (char*) (in_fname = (unsigned char*)filename);
}

/* Language dependent wrapup.  */

void 
treelang_finish (void)
{
  fclose (yyin);
}

/* Parse a file.  Debug flag doesn't seem to work. */

void
treelang_parse_file (int debug_flag ATTRIBUTE_UNUSED)
{
  treelang_debug ();
  yyparse ();
}


/* Scan the symbol table* M, marking storage used.  */

static void
symbol_table_ggc (void *m)
{
  struct production *pp;
  pp = * (struct production**)m;
  /* Actually it is a pointer to a pointer, to allow reallocation and
     relinking.  */
  mark_production_used (pp);
}

/* Mark a production PP as used so it wont be garbage collected.  */

void
mark_production_used (struct production *pp)
{
  int sub_ix;
 loop:
  if (!pp)
    return;
  ggc_mark (pp);
  
  if (pp->category == token_category)
    {
      mark_token_used ((struct token*)pp);
      return;
    }
  if (pp->category != production_category)
    abort ();
  mark_token_used (pp->main_token);
  for (sub_ix = 0; sub_ix < SUB_COUNT; sub_ix++)
    mark_production_used (pp->sub[sub_ix]);
  /* The macro tests for NULL so I don't need to.  */
  ggc_mark_tree (pp->code);
  pp = pp->next;
  goto loop;
}

/* Mark a token TT as used so it wont be garbage collected.  */

void
mark_token_used (struct token* tt)
{
  if (!tt) 
    return;
  ggc_mark (tt);
  if (tt->chars)
    ggc_mark (tt->chars);
}

/* Allocate SIZE bytes and clear them.  */

void *
my_malloc (size_t size)
{
  void *mem;
  mem = ggc_alloc (size);
  if (!mem)
    {
      fprintf (stderr, "\nOut of memory\n");
      abort ();
    }
  memset (mem, 0, size);
  return mem;
}

/* Look up a name in PROD->SYMBOL_TABLE_NAME in the symbol table;
   return the symbol table entry from the symbol table if found there,
   else 0.  */

struct production*
lookup_tree_name (struct production *prod)
{
  struct production *this;
  struct token* this_tok;
  struct token* tok;
  tok = SYMBOL_TABLE_NAME (prod);
  for (this = symbol_table; this; this = this->next)
    {
      this_tok = this->main_token;
      if (tok->length != this_tok->length) 
        continue;
      if (memcmp (tok->chars, this_tok->chars, this_tok->length))
        continue;
      if (option_parser_trace)
        fprintf (stderr, "Found symbol %s (%i:%i) as %i \n", tok->chars, 
                tok->lineno, tok->charno, NUMERIC_TYPE (this));
      return this;
    }
  if (option_parser_trace)
    fprintf (stderr, "Not found symbol %s (%i:%i) as %i \n", tok->chars, 
            tok->lineno, tok->charno, tok->type);
  return NULL;
}

/* Insert name PROD into the symbol table.  Return 1 if duplicate, 0 if OK.  */

int
insert_tree_name (struct production *prod)
{
  struct token* tok;
  tok = SYMBOL_TABLE_NAME (prod);
  if (lookup_tree_name (prod))
    {
      fprintf (stderr, "%s:%i:%i duplicate name %s\n", in_fname, tok->lineno, tok->charno, tok->chars);
      errorcount++;
      return 1;
    }
  prod->next = symbol_table;
  NESTING_LEVEL (prod) = work_nesting_level;
  symbol_table = prod;
  return 0;
}

/* Create a struct productions of type TYPE, main token MAIN_TOK.  */

struct production *
make_production (int type, struct token* main_tok)
{
  struct production *prod;
  prod = my_malloc (sizeof (struct production));
  prod->category = production_category;
  prod->type = type;
  prod->main_token = main_tok;
  return prod;
}