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 2007, 2008 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
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; see the file COPYING3. If not, see
19 <http://www.gnu.org/licenses/>.
21 In other words, you are welcome to use, share and improve this program.
22 You are forbidden to forbid anyone else to use, share and improve
23 what you give them. Help stamp out software-hoarding!
25 ---------------------------------------------------------------------------
27 Written by Tim Josling 1999, 2000, 2001, based in part on other
28 parts of the GCC compiler. */
32 #include "coretypes.h"
41 #include "diagnostic.h"
48 extern int yyparse (void);
50 /* Linked list of symbols - all must be unique in treelang. */
52 static GTY(()) struct prod_token_parm_item
*symbol_table
= NULL
;
54 /* Language for usage for messages. */
56 const char *const language_string
= "TREELANG - sample front end for GCC ";
58 /* Local prototypes. */
62 /* Global variables. */
64 extern struct cbl_tree_struct_parse_tree_top
* parse_tree_top
;
70 /* Trace the parser. */
71 unsigned int option_parser_trace
= 0;
73 /* Trace the lexical analysis. */
75 unsigned int option_lexer_trace
= 0;
79 /* Local variables. */
81 /* This is 1 if we have output the version string. */
83 static int version_done
= 0;
85 /* Variable nesting level. */
87 static unsigned int work_nesting_level
= 0;
89 /* Prepare to handle switches. */
91 treelang_init_options (unsigned int argc ATTRIBUTE_UNUSED
,
92 const char **argv ATTRIBUTE_UNUSED
)
97 /* Process a switch - called by opts.c. */
99 treelang_handle_option (size_t scode
, const char *arg ATTRIBUTE_UNUSED
,
102 enum opt_code code
= (enum opt_code
) scode
;
109 fputs (language_string
, stdout
);
110 fputs (pkgversion_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 linemap_add (line_table
, LC_ENTER
, false, main_input_filename
, 1);
144 /* This error will not happen from GCC as it will always create a
146 if (!input_filename
|| input_filename
[0] == ' ' || !input_filename
[0])
150 fprintf (stderr
, "No input file specified, try --help for help\n");
157 yyin
= fopen (input_filename
, "r");
160 fprintf (stderr
, "Unable to open input file %s\n", input_filename
);
164 linemap_add (line_table
, LC_RENAME
, false, "<built-in>", 1);
165 linemap_line_start (line_table
, 0, 1);
167 /* Init decls, etc. */
168 treelang_init_decl_processing ();
173 /* Language dependent wrapup. */
176 treelang_finish (void)
181 /* Parse a file. Debug flag doesn't seem to work. */
184 treelang_parse_file (int debug_flag ATTRIBUTE_UNUSED
)
187 linemap_add (line_table
, LC_RENAME
, false, main_input_filename
, 1);
188 s
= linemap_line_start (line_table
, 1, 80);
193 cgraph_finalize_compilation_unit ();
194 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
198 /* Allocate SIZE bytes and clear them. Not to be used for strings
199 which must go in stringpool. */
202 my_malloc (size_t size
)
205 mem
= ggc_alloc (size
);
208 fprintf (stderr
, "\nOut of memory\n");
211 memset (mem
, 0, size
);
215 /* Look up a name in PROD->SYMBOL_TABLE_NAME in the symbol table;
216 return the symbol table entry from the symbol table if found there,
219 struct prod_token_parm_item
*
220 lookup_tree_name (struct prod_token_parm_item
*prod
)
222 struct prod_token_parm_item
*this;
223 struct prod_token_parm_item
*this_tok
;
224 struct prod_token_parm_item
*tok
;
228 tok
= SYMBOL_TABLE_NAME (prod
);
231 for (this = symbol_table
; this; this = this->tp
.pro
.next
)
234 this_tok
= this->tp
.pro
.main_token
;
235 sanity_check (this_tok
);
236 if (tok
->tp
.tok
.length
!= this_tok
->tp
.tok
.length
)
238 if (memcmp (tok
->tp
.tok
.chars
, this_tok
->tp
.tok
.chars
,
239 this_tok
->tp
.tok
.length
))
242 if (option_parser_trace
)
243 fprintf (stderr
, "Found symbol %s (%i:%i) as %i \n",
244 tok
->tp
.tok
.chars
, LOCATION_LINE (tok
->tp
.tok
.location
),
245 tok
->tp
.tok
.charno
, NUMERIC_TYPE (this));
249 if (option_parser_trace
)
250 fprintf (stderr
, "Not found symbol %s (%i:%i) as %i \n",
251 tok
->tp
.tok
.chars
, LOCATION_LINE (tok
->tp
.tok
.location
),
252 tok
->tp
.tok
.charno
, tok
->type
);
256 /* Insert name PROD into the symbol table. Return 1 if duplicate, 0 if OK. */
259 insert_tree_name (struct prod_token_parm_item
*prod
)
261 struct prod_token_parm_item
*tok
;
262 tok
= SYMBOL_TABLE_NAME (prod
);
264 if (lookup_tree_name (prod
))
266 error ("%HDuplicate name %q.*s.", &tok
->tp
.tok
.location
,
267 tok
->tp
.tok
.length
, tok
->tp
.tok
.chars
);
270 prod
->tp
.pro
.next
= symbol_table
;
271 NESTING_LEVEL (prod
) = work_nesting_level
;
276 /* Create a struct productions of type TYPE, main token MAIN_TOK. */
278 struct prod_token_parm_item
*
279 make_production (int type
, struct prod_token_parm_item
*main_tok
)
281 struct prod_token_parm_item
*prod
;
282 prod
= my_malloc (sizeof (struct prod_token_parm_item
));
283 prod
->category
= production_category
;
285 prod
->tp
.pro
.main_token
= main_tok
;
289 /* Abort if ITEM is not a valid structure, based on 'category'. */
292 sanity_check (struct prod_token_parm_item
*item
)
294 switch (item
->category
)
297 case production_category
:
298 case parameter_category
:
306 /* New garbage collection regime see gty.texi. */
307 #include "gt-treelang-tree1.h"
308 /*#include "gt-treelang-treelang.h"*/
309 #include "gtype-treelang.h"