1 /* TREELANG Compiler almost main (tree1)
2 Called by GCC's toplev.c
4 Copyright (C) 1986, 87, 89, 92-96, 1997, 1999, 2000, 2001, 2002, 2003, 2004
5 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 In other words, you are welcome to use, share and improve this program.
23 You are forbidden to forbid anyone else to use, share and improve
24 what you give them. Help stamp out software-hoarding!
26 ---------------------------------------------------------------------------
28 Written by Tim Josling 1999, 2000, 2001, based in part on other
29 parts of the GCC compiler. */
33 #include "coretypes.h"
42 #include "diagnostic.h"
49 extern int yyparse (void);
51 /* Linked list of symbols - all must be unique in treelang. */
53 static GTY(()) struct prod_token_parm_item *symbol_table = NULL;
55 /* Language for usage for messages. */
57 const char *const language_string = "TREELANG - sample front end for GCC ";
59 /* Local prototypes. */
63 /* Global variables. */
65 extern struct cbl_tree_struct_parse_tree_top* parse_tree_top;
71 /* Trace the parser. */
72 unsigned int option_parser_trace = 0;
74 /* Trace the lexical analysis. */
76 unsigned int option_lexer_trace = 0;
80 /* Local variables. */
82 /* This is 1 if we have output the version string. */
84 static int version_done = 0;
86 /* Variable nesting level. */
88 static unsigned int work_nesting_level = 0;
90 /* Prepare to handle switches. */
92 treelang_init_options (unsigned int argc ATTRIBUTE_UNUSED,
93 const char **argv ATTRIBUTE_UNUSED)
98 /* Process a switch - called by opts.c. */
100 treelang_handle_option (size_t scode, const char *arg ATTRIBUTE_UNUSED,
103 enum opt_code code = (enum opt_code) scode;
113 fputs (language_string, stdout);
114 fputs (version_string, stdout);
115 fputs ("\n", stdout);
121 option_lexer_trace = 1;
122 option_parser_trace = 1;
125 case OPT_fparser_trace:
126 option_parser_trace = value;
129 case OPT_flexer_trace:
130 option_lexer_trace = value;
137 /* Language dependent parser setup. */
142 input_filename = main_input_filename;
145 /* Init decls etc. */
147 treelang_init_decl_processing ();
149 /* This error will not happen from GCC as it will always create a
151 if (!input_filename || input_filename[0] == ' ' || !input_filename[0])
155 fprintf (stderr, "No input file specified, try --help for help\n");
162 yyin = fopen (input_filename, "r");
165 fprintf (stderr, "Unable to open input file %s\n", input_filename);
172 /* Language dependent wrapup. */
175 treelang_finish (void)
180 /* Parse a file. Debug flag doesn't seem to work. */
183 treelang_parse_file (int debug_flag ATTRIBUTE_UNUSED)
187 cgraph_finalize_compilation_unit ();
191 /* Allocate SIZE bytes and clear them. Not to be used for strings
192 which must go in stringpool. */
195 my_malloc (size_t size)
198 mem = ggc_alloc (size);
201 fprintf (stderr, "\nOut of memory\n");
204 memset (mem, 0, size);
208 /* Look up a name in PROD->SYMBOL_TABLE_NAME in the symbol table;
209 return the symbol table entry from the symbol table if found there,
212 struct prod_token_parm_item*
213 lookup_tree_name (struct prod_token_parm_item *prod)
215 struct prod_token_parm_item *this;
216 struct prod_token_parm_item *this_tok;
217 struct prod_token_parm_item *tok;
221 tok = SYMBOL_TABLE_NAME (prod);
224 for (this = symbol_table; this; this = this->tp.pro.next)
227 this_tok = this->tp.pro.main_token;
228 sanity_check (this_tok);
229 if (tok->tp.tok.length != this_tok->tp.tok.length)
231 if (memcmp (tok->tp.tok.chars, this_tok->tp.tok.chars,
232 this_tok->tp.tok.length))
235 if (option_parser_trace)
236 fprintf (stderr, "Found symbol %s (%i:%i) as %i \n",
237 tok->tp.tok.chars, LOCATION_LINE (tok->tp.tok.location),
238 tok->tp.tok.charno, NUMERIC_TYPE (this));
242 if (option_parser_trace)
243 fprintf (stderr, "Not found symbol %s (%i:%i) as %i \n",
244 tok->tp.tok.chars, LOCATION_LINE (tok->tp.tok.location),
245 tok->tp.tok.charno, tok->type);
249 /* Insert name PROD into the symbol table. Return 1 if duplicate, 0 if OK. */
252 insert_tree_name (struct prod_token_parm_item *prod)
254 struct prod_token_parm_item *tok;
255 tok = SYMBOL_TABLE_NAME (prod);
257 if (lookup_tree_name (prod))
259 fprintf (stderr, "%s:%i:%i duplicate name %s\n",
260 tok->tp.tok.location.file, tok->tp.tok.location.line,
261 tok->tp.tok.charno, tok->tp.tok.chars);
265 prod->tp.pro.next = symbol_table;
266 NESTING_LEVEL (prod) = work_nesting_level;
271 /* Create a struct productions of type TYPE, main token MAIN_TOK. */
273 struct prod_token_parm_item *
274 make_production (int type, struct prod_token_parm_item *main_tok)
276 struct prod_token_parm_item *prod;
277 prod = my_malloc (sizeof (struct prod_token_parm_item));
278 prod->category = production_category;
280 prod->tp.pro.main_token = main_tok;
284 /* Abort if ITEM is not a valid structure, based on 'category'. */
287 sanity_check (struct prod_token_parm_item *item)
289 switch (item->category)
292 case production_category:
293 case parameter_category:
301 /* New garbage collection regime see gty.texi. */
302 #include "gt-treelang-tree1.h"
303 /*#include "gt-treelang-treelang.h"*/
304 #include "gtype-treelang.h"