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, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, 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
;
110 fputs (language_string
, stdout
);
111 fputs (version_string
, stdout
);
112 fputs ("\n", stdout
);
118 option_lexer_trace
= 1;
119 option_parser_trace
= 1;
122 case OPT_fparser_trace
:
123 option_parser_trace
= value
;
126 case OPT_flexer_trace
:
127 option_lexer_trace
= value
;
137 /* Language dependent parser setup. */
142 #ifndef USE_MAPPED_LOCATION
143 input_filename
= main_input_filename
;
145 linemap_add (&line_table
, LC_ENTER
, false, main_input_filename
, 1);
148 /* This error will not happen from GCC as it will always create a
150 if (!input_filename
|| input_filename
[0] == ' ' || !input_filename
[0])
154 fprintf (stderr
, "No input file specified, try --help for help\n");
161 yyin
= fopen (input_filename
, "r");
164 fprintf (stderr
, "Unable to open input file %s\n", input_filename
);
168 #ifdef USE_MAPPED_LOCATION
169 linemap_add (&line_table
, LC_RENAME
, false, "<built-in>", 1);
170 linemap_line_start (&line_table
, 0, 1);
173 /* Init decls, etc. */
174 treelang_init_decl_processing ();
179 /* Language dependent wrapup. */
182 treelang_finish (void)
187 /* Parse a file. Debug flag doesn't seem to work. */
190 treelang_parse_file (int debug_flag ATTRIBUTE_UNUSED
)
192 #ifdef USE_MAPPED_LOCATION
194 linemap_add (&line_table
, LC_RENAME
, false, main_input_filename
, 1);
195 s
= linemap_line_start (&line_table
, 1, 80);
203 cgraph_finalize_compilation_unit ();
204 #ifdef USE_MAPPED_LOCATION
205 linemap_add (&line_table
, LC_LEAVE
, false, NULL
, 0);
210 /* Allocate SIZE bytes and clear them. Not to be used for strings
211 which must go in stringpool. */
214 my_malloc (size_t size
)
217 mem
= ggc_alloc (size
);
220 fprintf (stderr
, "\nOut of memory\n");
223 memset (mem
, 0, size
);
227 /* Look up a name in PROD->SYMBOL_TABLE_NAME in the symbol table;
228 return the symbol table entry from the symbol table if found there,
231 struct prod_token_parm_item
*
232 lookup_tree_name (struct prod_token_parm_item
*prod
)
234 struct prod_token_parm_item
*this;
235 struct prod_token_parm_item
*this_tok
;
236 struct prod_token_parm_item
*tok
;
240 tok
= SYMBOL_TABLE_NAME (prod
);
243 for (this = symbol_table
; this; this = this->tp
.pro
.next
)
246 this_tok
= this->tp
.pro
.main_token
;
247 sanity_check (this_tok
);
248 if (tok
->tp
.tok
.length
!= this_tok
->tp
.tok
.length
)
250 if (memcmp (tok
->tp
.tok
.chars
, this_tok
->tp
.tok
.chars
,
251 this_tok
->tp
.tok
.length
))
254 if (option_parser_trace
)
255 fprintf (stderr
, "Found symbol %s (%i:%i) as %i \n",
256 tok
->tp
.tok
.chars
, LOCATION_LINE (tok
->tp
.tok
.location
),
257 tok
->tp
.tok
.charno
, NUMERIC_TYPE (this));
261 if (option_parser_trace
)
262 fprintf (stderr
, "Not found symbol %s (%i:%i) as %i \n",
263 tok
->tp
.tok
.chars
, LOCATION_LINE (tok
->tp
.tok
.location
),
264 tok
->tp
.tok
.charno
, tok
->type
);
268 /* Insert name PROD into the symbol table. Return 1 if duplicate, 0 if OK. */
271 insert_tree_name (struct prod_token_parm_item
*prod
)
273 struct prod_token_parm_item
*tok
;
274 tok
= SYMBOL_TABLE_NAME (prod
);
276 if (lookup_tree_name (prod
))
278 error ("%HDuplicate name %q.*s.", &tok
->tp
.tok
.location
,
279 tok
->tp
.tok
.length
, tok
->tp
.tok
.chars
);
282 prod
->tp
.pro
.next
= symbol_table
;
283 NESTING_LEVEL (prod
) = work_nesting_level
;
288 /* Create a struct productions of type TYPE, main token MAIN_TOK. */
290 struct prod_token_parm_item
*
291 make_production (int type
, struct prod_token_parm_item
*main_tok
)
293 struct prod_token_parm_item
*prod
;
294 prod
= my_malloc (sizeof (struct prod_token_parm_item
));
295 prod
->category
= production_category
;
297 prod
->tp
.pro
.main_token
= main_tok
;
301 /* Abort if ITEM is not a valid structure, based on 'category'. */
304 sanity_check (struct prod_token_parm_item
*item
)
306 switch (item
->category
)
309 case production_category
:
310 case parameter_category
:
318 /* New garbage collection regime see gty.texi. */
319 #include "gt-treelang-tree1.h"
320 /*#include "gt-treelang-treelang.h"*/
321 #include "gtype-treelang.h"