3 TREELANG Compiler almost main (tree1)
4 Called by GCC's toplev.c
6 Copyright (C) 1986, 87, 89, 92-96, 1997, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
23 In other words, you are welcome to use, share and improve this program.
24 You are forbidden to forbid anyone else to use, share and improve
25 what you give them. Help stamp out software-hoarding!
27 ---------------------------------------------------------------------------
29 Written by Tim Josling 1999, 2000, 2001, based in part on other
30 parts of the GCC compiler.
43 #include "diagnostic.h"
55 extern int yyparse (void);
56 /* Linked list of symbols - all must be unique in treelang. */
58 struct production
*symbol_table
= NULL
;
60 /* Language for usage for messages. */
62 const char *const language_string
= "TREELANG - sample front end for GCC ";
64 /* Local prototypes. */
68 /* GC routine for symbol table. */
69 static void symbol_table_ggc (void *m
);
71 /* Global variables. */
73 extern struct cbl_tree_struct_parse_tree_top
* parse_tree_top
;
79 /* Trace the parser. */
80 unsigned int option_parser_trace
= 0;
82 /* Trace the lexical analysis. */
84 unsigned int option_lexer_trace
= 0;
88 /* Local variables. */
90 unsigned char *in_fname
= NULL
; /* Input file name. */
92 /* This is 1 if we have output the version string. */
94 static int version_done
= 0;
96 /* Variable nesting level. */
98 static unsigned int work_nesting_level
= 0;
100 /* Process one switch - called by toplev.c. */
103 treelang_decode_option (num_options_left
, first_option_left
)
104 int num_options_left ATTRIBUTE_UNUSED
;
105 char** first_option_left
;
109 Process options - bear in mind I may get options that are really
110 meant for someone else (eg the main compiler) so I have to be very
115 if (first_option_left
[0][0] != '-')
118 switch (first_option_left
[0][1])
121 if (!strcmp (first_option_left
[0],"--help"))
125 fputs (language_string
, stdout
);
126 fputs (version_string
, stdout
);
127 fputs ("\n", stdout
);
130 fprintf (stdout
, "Usage: tree1 [switches] -o output input\n");
134 if (!strcmp (first_option_left
[0],"-v"))
138 fputs (language_string
, stdout
);
139 fputs (version_string
, stdout
);
140 fputs ("\n", stdout
);
146 if (!strcmp (first_option_left
[0],"-y"))
148 option_lexer_trace
= 1;
149 option_parser_trace
= 1;
153 if (!strcmp (first_option_left
[0],"-fparser-trace"))
155 option_parser_trace
= 1;
158 if (!strcmp (first_option_left
[0],"-flexer-trace"))
160 option_lexer_trace
= 1;
166 if (!strcmp (first_option_left
[0],"-w"))
168 /* Tolerate this option but ignore it - we always put out
175 if (!strcmp (first_option_left
[0],"-Wall"))
189 /* Language dependent parser setup. */
192 treelang_init (const char* filename
)
195 /* Define my garbage collection routines. */
196 ggc_add_root (&symbol_table
, 1,
197 /* Unused size. */ sizeof (void*), symbol_table_ggc
);
198 /* Note: only storage that has to be kept across functions needs to
199 be protected from GC. */
200 /* Define my garbage collection routines. */
201 ggc_add_root (&symbol_table
, 1,
202 /* Unused size. */ sizeof (void*), symbol_table_ggc
);
203 /* Note: only storage that has to be kept across functions needs to
204 be protected from GC. */
206 /* Set up the declarations needed for this front end. */
211 treelang_init_decl_processing ();
213 /* This error will not happen from GCC as it will always create a
215 if (!filename
|| (filename
[0] == ' ') || (!filename
[0]))
219 fprintf (stderr
, "No input file specified, try --help for help\n");
226 yyin
= fopen (filename
, "r");
229 fprintf (stderr
, "Unable to open input file %s\n", filename
);
232 input_filename
= filename
;
233 return (char*) (in_fname
= (unsigned char*)filename
);
236 /* Language dependent wrapup. */
239 treelang_finish (void)
244 /* Parse a file. Debug flag doesn't seem to work. */
247 treelang_parse_file (int debug_flag ATTRIBUTE_UNUSED
)
254 /* Scan the symbol table* M, marking storage used. */
257 symbol_table_ggc (void *m
)
259 struct production
*pp
;
260 pp
= * (struct production
**)m
;
261 /* Actually it is a pointer to a pointer, to allow reallocation and
263 mark_production_used (pp
);
266 /* Mark a production PP as used so it wont be garbage collected. */
269 mark_production_used (struct production
*pp
)
277 if (pp
->category
== token_category
)
279 mark_token_used ((struct token
*)pp
);
282 if (pp
->category
!= production_category
)
284 mark_token_used (pp
->main_token
);
285 for (sub_ix
= 0; sub_ix
< SUB_COUNT
; sub_ix
++)
286 mark_production_used (pp
->sub
[sub_ix
]);
287 /* The macro tests for NULL so I don't need to. */
288 ggc_mark_tree (pp
->code
);
293 /* Mark a token TT as used so it wont be garbage collected. */
296 mark_token_used (struct token
* tt
)
302 ggc_mark (tt
->chars
);
305 /* Allocate SIZE bytes and clear them. */
308 my_malloc (size_t size
)
311 mem
= ggc_alloc (size
);
314 fprintf (stderr
, "\nOut of memory\n");
317 memset (mem
, 0, size
);
321 /* Look up a name in PROD->SYMBOL_TABLE_NAME in the symbol table;
322 return the symbol table entry from the symbol table if found there,
326 lookup_tree_name (struct production
*prod
)
328 struct production
*this;
329 struct token
* this_tok
;
331 tok
= SYMBOL_TABLE_NAME (prod
);
332 for (this = symbol_table
; this; this = this->next
)
334 this_tok
= this->main_token
;
335 if (tok
->length
!= this_tok
->length
)
337 if (memcmp (tok
->chars
, this_tok
->chars
, this_tok
->length
))
339 if (option_parser_trace
)
340 fprintf (stderr
, "Found symbol %s (%i:%i) as %i \n", tok
->chars
,
341 tok
->lineno
, tok
->charno
, NUMERIC_TYPE (this));
344 if (option_parser_trace
)
345 fprintf (stderr
, "Not found symbol %s (%i:%i) as %i \n", tok
->chars
,
346 tok
->lineno
, tok
->charno
, tok
->type
);
350 /* Insert name PROD into the symbol table. Return 1 if duplicate, 0 if OK. */
353 insert_tree_name (struct production
*prod
)
356 tok
= SYMBOL_TABLE_NAME (prod
);
357 if (lookup_tree_name (prod
))
359 fprintf (stderr
, "%s:%i:%i duplicate name %s\n", in_fname
, tok
->lineno
, tok
->charno
, tok
->chars
);
363 prod
->next
= symbol_table
;
364 NESTING_LEVEL (prod
) = work_nesting_level
;
369 /* Create a struct productions of type TYPE, main token MAIN_TOK. */
372 make_production (int type
, struct token
* main_tok
)
374 struct production
*prod
;
375 prod
= my_malloc (sizeof (struct production
));
376 prod
->category
= production_category
;
378 prod
->main_token
= main_tok
;