OSDN Git Service

2004-10-02 James A. Morrison <phython@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / treelang / tree1.c
1 /* TREELANG Compiler almost main (tree1)
2    Called by GCC's toplev.c
3
4    Copyright (C) 1986, 87, 89, 92-96, 1997, 1999, 2000, 2001, 2002, 2003, 2004
5    Free Software Foundation, Inc.
6
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
10    later version.
11
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.
16
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.
21
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!  
25
26    ---------------------------------------------------------------------------
27
28    Written by Tim Josling 1999, 2000, 2001, based in part on other
29    parts of the GCC compiler.  */
30
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "flags.h"
36 #include "toplev.h"
37 #include "version.h"
38
39 #include "ggc.h"
40 #include "tree.h"
41 #include "cgraph.h"
42 #include "diagnostic.h"
43
44 #include "treelang.h"
45 #include "treetree.h"
46 #include "opts.h"
47 #include "options.h"
48
49 extern int yyparse (void);
50
51 /* Linked list of symbols - all must be unique in treelang.  */
52
53 static GTY(()) struct prod_token_parm_item *symbol_table = NULL;
54
55 /* Language for usage for messages.  */
56
57 const char *const language_string = "TREELANG - sample front end for GCC ";
58
59 /* Local prototypes.  */
60
61 void version (void);
62
63 /* Global variables.  */
64
65 extern struct cbl_tree_struct_parse_tree_top* parse_tree_top;
66
67 /* 
68    Options. 
69 */
70
71 /* Trace the parser.  */
72 unsigned int option_parser_trace = 0;
73
74 /* Trace the lexical analysis.  */
75
76 unsigned int option_lexer_trace = 0;
77
78 /* Warning levels.  */
79
80 /* Local variables.  */
81
82 /* This is 1 if we have output the version string.  */
83
84 static int version_done = 0;
85
86 /* Variable nesting level.  */
87
88 static unsigned int work_nesting_level = 0;
89
90 /* Prepare to handle switches.  */
91 unsigned int
92 treelang_init_options (unsigned int argc ATTRIBUTE_UNUSED,
93                        const char **argv ATTRIBUTE_UNUSED)
94 {
95   return CL_Treelang;
96 }
97
98 /* Process a switch - called by opts.c.  */
99 int
100 treelang_handle_option (size_t scode, const char *arg ATTRIBUTE_UNUSED,
101                         int value)
102 {
103   enum opt_code code = (enum opt_code) scode;
104
105   switch (code)
106     {
107     default:
108       abort();
109
110     case OPT_v:
111       if (!version_done)
112         {
113           fputs (language_string, stdout);
114           fputs (version_string, stdout);
115           fputs ("\n", stdout);
116           version_done = 1;
117         }
118       break;
119
120     case OPT_y:
121       option_lexer_trace = 1;
122       option_parser_trace = 1;
123       break;
124
125     case OPT_fparser_trace:
126       option_parser_trace = value;
127       break;
128
129     case OPT_flexer_trace:
130       option_lexer_trace = value;
131       break;
132     }
133
134   return 1;
135 }
136
137 /* Language dependent parser setup.  */
138
139 bool
140 treelang_init (void)
141 {
142   input_filename = main_input_filename;
143   input_line = 1;
144
145   /* Init decls etc.  */
146
147   treelang_init_decl_processing ();
148
149   /* This error will not happen from GCC as it will always create a
150      fake input file.  */
151   if (!input_filename || input_filename[0] == ' ' || !input_filename[0]) 
152     {
153       if (!version_done)
154         {
155           fprintf (stderr, "No input file specified, try --help for help\n");
156           exit (1);
157         }
158
159       return false;
160     }
161
162   yyin = fopen (input_filename, "r");
163   if (!yyin)
164     {
165       fprintf (stderr, "Unable to open input file %s\n", input_filename);
166       exit (1);
167     }
168
169   return true;
170 }
171
172 /* Language dependent wrapup.  */
173
174 void 
175 treelang_finish (void)
176 {
177   fclose (yyin);
178 }
179
180 /* Parse a file.  Debug flag doesn't seem to work. */
181
182 void
183 treelang_parse_file (int debug_flag ATTRIBUTE_UNUSED)
184 {
185   treelang_debug ();
186   yyparse ();
187   cgraph_finalize_compilation_unit ();
188   cgraph_optimize ();
189 }
190
191 /* Allocate SIZE bytes and clear them.  Not to be used for strings
192    which must go in stringpool.  */
193
194 void *
195 my_malloc (size_t size)
196 {
197   void *mem;
198   mem = ggc_alloc (size);
199   if (!mem)
200     {
201       fprintf (stderr, "\nOut of memory\n");
202       abort ();
203     }
204   memset (mem, 0, size);
205   return mem;
206 }
207
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,
210    else 0.  */
211
212 struct prod_token_parm_item*
213 lookup_tree_name (struct prod_token_parm_item *prod)
214 {
215   struct prod_token_parm_item *this;
216   struct prod_token_parm_item *this_tok;
217   struct prod_token_parm_item *tok;
218
219   sanity_check (prod);
220   
221   tok = SYMBOL_TABLE_NAME (prod);
222   sanity_check (tok);
223   
224   for (this = symbol_table; this; this = this->tp.pro.next)
225     {
226       sanity_check (this);
227       this_tok = this->tp.pro.main_token;
228       sanity_check (this_tok);
229       if (tok->tp.tok.length != this_tok->tp.tok.length) 
230         continue;
231       if (memcmp (tok->tp.tok.chars, this_tok->tp.tok.chars,
232                   this_tok->tp.tok.length))
233         continue;
234
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));
239       return this;
240     }
241
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);
246   return NULL;
247 }
248
249 /* Insert name PROD into the symbol table.  Return 1 if duplicate, 0 if OK.  */
250
251 int
252 insert_tree_name (struct prod_token_parm_item *prod)
253 {
254   struct prod_token_parm_item *tok;
255   tok = SYMBOL_TABLE_NAME (prod);
256   sanity_check (prod);
257   if (lookup_tree_name (prod))
258     {
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);
262       errorcount++;
263       return 1;
264     }
265   prod->tp.pro.next = symbol_table;
266   NESTING_LEVEL (prod) = work_nesting_level;
267   symbol_table = prod;
268   return 0;
269 }
270
271 /* Create a struct productions of type TYPE, main token MAIN_TOK.  */
272
273 struct prod_token_parm_item *
274 make_production (int type, struct prod_token_parm_item *main_tok)
275 {
276   struct prod_token_parm_item *prod;
277   prod = my_malloc (sizeof (struct prod_token_parm_item));
278   prod->category = production_category;
279   prod->type = type;
280   prod->tp.pro.main_token = main_tok;
281   return prod;
282
283
284 /* Abort if ITEM is not a valid structure, based on 'category'.  */
285
286 void
287 sanity_check (struct prod_token_parm_item *item)
288 {
289   switch (item->category)
290     {
291     case token_category:
292     case production_category:
293     case parameter_category:
294       break;
295       
296     default:
297       abort ();
298     }
299 }  
300
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"