OSDN Git Service

* config/mips/mips.h (ISA_HAS_DCLZ_DCLO): Delete.
[pf3gnuchains/gcc-fork.git] / gcc / treelang / treetree.c
1 /*
2
3     TREELANG Compiler interface to GCC's middle end (treetree.c)
4     Called by the parser.
5
6     If you want a working example of how to write a front end to GCC,
7     you are in the right place.
8
9     Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
10     1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
11
12     This code is based on toy.c written by Richard Kenner.
13
14     It was later modified by Jonathan Bartlett whose changes have all
15     been removed (by Tim Josling).
16
17     Various bits and pieces were cloned from the GCC main tree, as
18     GCC evolved, for COBOLForGCC, by Tim Josling.
19
20     It was adapted to TREELANG by Tim Josling 2001.
21
22     Updated to function-at-a-time by James A. Morrison, 2004.
23
24     ---------------------------------------------------------------------------
25
26     This program is free software; you can redistribute it and/or modify it
27     under the terms of the GNU General Public License as published by the
28     Free Software Foundation; either version 2, or (at your option) any
29     later version.
30
31     This program is distributed in the hope that it will be useful,
32     but WITHOUT ANY WARRANTY; without even the implied warranty of
33     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34     GNU General Public License for more details.
35
36     You should have received a copy of the GNU General Public License
37     along with this program; if not, write to the Free Software
38     Foundation, 59 Temple Place - Suite 330,
39     Boston, MA 02111-1307, USA.
40
41     In other words, you are welcome to use, share and improve this program.
42     You are forbidden to forbid anyone else to use, share and improve
43     what you give them.   Help stamp out software-hoarding!
44
45     ---------------------------------------------------------------------------
46
47  */
48
49 /*
50   Assumption: garbage collection is never called implicitly.  It will
51   not be called 'at any time' when short of memory.  It will only be
52   called explicitly at the end of each function.  This removes the
53   need for a *lot* of bother to ensure everything is in the mark trees
54   at all times.  */
55
56 /* Note, it is OK to use GCC extensions such as long long in a compiler front
57    end.  This is because the GCC front ends are built using GCC.   */
58
59 /* GCC headers.  */
60
61 #include "config.h"
62 #include "system.h"
63 #include "coretypes.h"
64 #include "tm.h"
65 #include "tree.h"
66 #include "tree-dump.h"
67 #include "tree-iterator.h"
68 #include "tree-gimple.h"
69 #include "function.h"
70 #include "flags.h"
71 #include "output.h"
72 #include "ggc.h"
73 #include "toplev.h"
74 #include "varray.h"
75 #include "langhooks-def.h"
76 #include "langhooks.h"
77 #include "target.h"
78
79 #include "cgraph.h"
80
81 #include "treelang.h"
82 #include "treetree.h"
83 #include "opts.h"
84
85 extern int option_main;
86 extern char **file_names;
87
88 /* Types expected by gcc's garbage collector.
89    These types exist to allow language front-ends to
90    add extra information in gcc's parse tree data structure.
91    But the treelang front end doesn't use them -- it has
92    its own parse tree data structure.
93    We define them here only to satisfy gcc's garbage collector.  */
94
95 /* Language-specific identifier information.  */
96
97 struct lang_identifier GTY(())
98 {
99   struct tree_identifier common;
100 };
101
102 /* Language-specific tree node information.  */
103
104 union lang_tree_node 
105   GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE")))
106 {
107   union tree_node GTY ((tag ("0"), 
108                         desc ("tree_node_structure (&%h)"))) 
109     generic;
110   struct lang_identifier GTY ((tag ("1"))) identifier;
111 };
112
113 /* Language-specific type information.  */
114
115 struct lang_type GTY(())
116 {
117   char junk; /* dummy field to ensure struct is not empty */
118 };
119
120 /* Language-specific declaration information.  */
121
122 struct lang_decl GTY(())
123 {
124   char junk; /* dummy field to ensure struct is not empty */
125 };
126
127 struct language_function GTY(())
128 {
129   char junk; /* dummy field to ensure struct is not empty */
130 };
131
132 static tree tree_lang_truthvalue_conversion (tree expr);
133 static bool tree_mark_addressable (tree exp);
134 static tree tree_lang_type_for_size (unsigned precision, int unsignedp);
135 static tree tree_lang_type_for_mode (enum machine_mode mode, int unsignedp);
136 static tree tree_lang_unsigned_type (tree type_node);
137 static tree tree_lang_signed_type (tree type_node);
138 static tree tree_lang_signed_or_unsigned_type (int unsignedp, tree type);
139
140 /* Functions to keep track of the current scope.  */
141 static void pushlevel (int ignore);
142 static tree poplevel (int keep, int reverse, int functionbody);
143 static tree pushdecl (tree decl);
144 static tree* getstmtlist (void);
145
146 /* Langhooks.  */
147 static tree builtin_function (const char *name, tree type, int function_code,
148                   enum built_in_class class, const char *library_name,
149                   tree attrs);
150 static tree getdecls (void);
151 static int global_bindings_p (void);
152 static void insert_block (tree);
153
154 static void tree_push_type_decl (tree id, tree type_node);
155 static void treelang_expand_function (tree fndecl);
156
157 /* The front end language hooks (addresses of code for this front
158    end).  These are not really very language-dependent, i.e.
159    treelang, C, Mercury, etc. can all use almost the same definitions.  */
160
161 #undef LANG_HOOKS_TRUTHVALUE_CONVERSION
162 #define LANG_HOOKS_TRUTHVALUE_CONVERSION tree_lang_truthvalue_conversion
163 #undef LANG_HOOKS_MARK_ADDRESSABLE
164 #define LANG_HOOKS_MARK_ADDRESSABLE tree_mark_addressable
165 #undef LANG_HOOKS_SIGNED_TYPE
166 #define LANG_HOOKS_SIGNED_TYPE tree_lang_signed_type
167 #undef LANG_HOOKS_UNSIGNED_TYPE
168 #define LANG_HOOKS_UNSIGNED_TYPE tree_lang_unsigned_type
169 #undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
170 #define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE tree_lang_signed_or_unsigned_type
171 #undef LANG_HOOKS_TYPE_FOR_MODE
172 #define LANG_HOOKS_TYPE_FOR_MODE tree_lang_type_for_mode
173 #undef LANG_HOOKS_TYPE_FOR_SIZE
174 #define LANG_HOOKS_TYPE_FOR_SIZE tree_lang_type_for_size
175 #undef LANG_HOOKS_PARSE_FILE
176 #define LANG_HOOKS_PARSE_FILE treelang_parse_file
177
178 #undef LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION
179 #define LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION treelang_expand_function
180
181 /* #undef LANG_HOOKS_TYPES_COMPATIBLE_P
182 #define LANG_HOOKS_TYPES_COMPATIBLE_P hook_bool_tree_tree_true
183 */
184 /* Hook routines and data unique to treelang.  */
185
186 #undef LANG_HOOKS_INIT
187 #define LANG_HOOKS_INIT treelang_init
188 #undef LANG_HOOKS_NAME
189 #define LANG_HOOKS_NAME "GNU treelang"
190 #undef LANG_HOOKS_FINISH
191 #define LANG_HOOKS_FINISH               treelang_finish
192 #undef LANG_HOOKS_INIT_OPTIONS
193 #define LANG_HOOKS_INIT_OPTIONS  treelang_init_options
194 #undef LANG_HOOKS_HANDLE_OPTION
195 #define LANG_HOOKS_HANDLE_OPTION treelang_handle_option
196 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
197
198 /* Tree code type/name/code tables.  */
199
200 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
201
202 const char tree_code_type[] = {
203 #include "tree.def"
204   'x'
205 };
206 #undef DEFTREECODE
207
208 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
209
210 const unsigned char tree_code_length[] = {
211 #include "tree.def"
212   0
213 };
214 #undef DEFTREECODE
215
216 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
217
218 const char *const tree_code_name[] = {
219 #include "tree.def"
220   "@@dummy"
221 };
222 #undef DEFTREECODE
223
224 /* Number of bits in int and char - accessed by front end.  */
225
226 unsigned int tree_code_int_size = SIZEOF_INT * HOST_BITS_PER_CHAR;
227
228 unsigned int tree_code_char_size = HOST_BITS_PER_CHAR;
229
230 /* Return the tree stuff for this type TYPE_NUM.  */
231
232 tree
233 tree_code_get_type (int type_num)
234 {
235   switch (type_num)
236     {
237     case SIGNED_CHAR:
238       return signed_char_type_node;
239
240     case UNSIGNED_CHAR:
241       return unsigned_char_type_node;
242
243     case SIGNED_INT:
244       return integer_type_node;
245
246     case UNSIGNED_INT:
247       return unsigned_type_node;
248
249     case VOID_TYPE:
250       return void_type_node;
251
252     default:
253       abort ();
254     }
255 }
256
257 /* Output the code for the start of an if statement.  The test
258    expression is EXP (true if not zero), and the stmt occurred at line
259    LINENO in file FILENAME.  */
260
261 void
262 tree_code_if_start (tree exp, location_t loc)
263 {
264   tree cond_exp, cond;
265   cond_exp = fold (build2 (NE_EXPR, boolean_type_node, exp,
266                      fold (build1 (CONVERT_EXPR, TREE_TYPE (exp), integer_zero_node))));
267   SET_EXPR_LOCATION (cond_exp, loc);
268   cond = build3 (COND_EXPR, void_type_node, cond_exp, NULL_TREE,
269                  NULL_TREE);
270   SET_EXPR_LOCATION (cond, loc);
271   append_to_statement_list_force (cond, getstmtlist ());
272   pushlevel (0);
273 }
274
275 /* Output the code for the else of an if statement.  The else occurred
276    at line LINENO in file FILENAME.  */
277
278 void
279 tree_code_if_else (location_t loc ATTRIBUTE_UNUSED)
280 {
281   tree stmts = *getstmtlist ();
282   tree block = poplevel (1, 0, 0);
283   if (BLOCK_VARS (block))
284     {
285       tree bindexpr = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
286                               stmts, block);
287       stmts = alloc_stmt_list ();
288       append_to_statement_list (bindexpr, &stmts);
289     }
290
291   TREE_OPERAND (STATEMENT_LIST_TAIL (*getstmtlist ())->stmt, 1) = stmts;
292   pushlevel (0);
293 }
294
295 /* Output the code for the end_if an if statement.  The end_if (final brace)
296    occurred at line LINENO in file FILENAME.  */
297
298 void
299 tree_code_if_end (location_t loc ATTRIBUTE_UNUSED)
300 {
301   tree stmts = *getstmtlist ();
302   tree block = poplevel (1, 0, 0);
303   if (BLOCK_VARS (block))
304     {
305        tree bindexpr = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
306                                stmts, block);
307        stmts = alloc_stmt_list ();
308        append_to_statement_list (bindexpr, &stmts);
309     }
310
311   TREE_OPERAND (STATEMENT_LIST_TAIL (*getstmtlist ())->stmt, 2) = stmts;
312 }
313
314 /* Create a function.  The prototype name is NAME, storage class is
315    STORAGE_CLASS, type of return variable is RET_TYPE, parameter lists
316    is PARMS, returns decl for this function.  */
317
318 tree
319 tree_code_create_function_prototype (unsigned char* chars,
320                                      unsigned int storage_class,
321                                      unsigned int ret_type,
322                                      struct prod_token_parm_item* parms,
323                                      location_t loc)
324 {
325
326   tree id;
327   struct prod_token_parm_item* parm;
328   tree type_list = NULL_TREE;
329   tree type_node;
330   tree fn_type;
331   tree fn_decl;
332
333   /* Build the type.  */
334   id = get_identifier ((const char*)chars);
335   for (parm = parms; parm; parm = parm->tp.par.next)
336     {
337       if (parm->category != parameter_category)
338         abort ();
339       type_node = tree_code_get_type (parm->type);
340       type_list = tree_cons (NULL_TREE, type_node, type_list);
341     }
342   /* Last parm if void indicates fixed length list (as opposed to
343      printf style va_* list).  */
344   type_list = tree_cons (NULL_TREE, void_type_node, type_list);
345   /* The back end needs them in reverse order.  */
346   type_list = nreverse (type_list);
347
348   type_node = tree_code_get_type (ret_type);
349   fn_type = build_function_type (type_node, type_list);
350
351   id = get_identifier ((const char*)chars);
352   fn_decl = build_decl (FUNCTION_DECL, id, fn_type);
353
354   /* Nested functions not supported here.  */
355   DECL_CONTEXT (fn_decl) = NULL_TREE;
356   DECL_SOURCE_LOCATION (fn_decl) = loc;
357
358   TREE_USED (fn_decl) = 1;
359
360   TREE_PUBLIC (fn_decl) = 0;
361   DECL_EXTERNAL (fn_decl) = 0;
362   TREE_STATIC (fn_decl) = 0;
363   switch (storage_class)
364     {
365     case STATIC_STORAGE:
366       TREE_PUBLIC (fn_decl) = 0;
367       break;
368
369     case EXTERNAL_DEFINITION_STORAGE:
370       TREE_PUBLIC (fn_decl) = 1;
371       TREE_STATIC (fn_decl) = 0;
372       DECL_EXTERNAL (fn_decl) = 0;
373       break;
374
375     case EXTERNAL_REFERENCE_STORAGE:
376       TREE_PUBLIC (fn_decl) = 0;
377       DECL_EXTERNAL (fn_decl) = 1;
378       break;
379
380     case AUTOMATIC_STORAGE:
381     default:
382       abort ();
383     }
384
385   /* Process declaration of function defined elsewhere.  */
386   rest_of_decl_compilation (fn_decl, 1, 0);
387
388   return fn_decl;
389 }
390
391
392 /* Output code for start of function; the decl of the function is in
393     PREV_SAVED (as created by tree_code_create_function_prototype),
394     the function is at line number LINENO in file FILENAME.  The
395     parameter details are in the lists PARMS. Returns nothing.  */
396 void
397 tree_code_create_function_initial (tree prev_saved,
398                                    location_t loc,
399                                    struct prod_token_parm_item* parms)
400 {
401   tree fn_decl;
402   tree param_decl;
403   tree parm_decl;
404   tree parm_list;
405   tree resultdecl;
406   struct prod_token_parm_item* this_parm;
407   struct prod_token_parm_item* parm;
408
409   fn_decl = prev_saved;
410   if (!fn_decl)
411     abort ();
412
413   /* Output message if not -quiet.  */
414   announce_function (fn_decl);
415
416   /* This has something to do with forcing output also.  */
417   pushdecl (fn_decl);
418
419   /* Set current function for error msgs etc.  */
420   current_function_decl = fn_decl;
421   DECL_INITIAL (fn_decl) = error_mark_node;
422
423   DECL_SOURCE_LOCATION (fn_decl) = loc;
424
425   /* Create a DECL for the functions result.  */
426   resultdecl =
427     build_decl (RESULT_DECL, NULL_TREE, TREE_TYPE (TREE_TYPE (fn_decl)));
428   DECL_CONTEXT (resultdecl) = fn_decl;
429   DECL_ARTIFICIAL (resultdecl) = 1;
430   DECL_IGNORED_P (resultdecl) = 1;
431   DECL_SOURCE_LOCATION (resultdecl) = loc;
432   DECL_RESULT (fn_decl) = resultdecl;
433
434   /* Make the argument variable decls.  */
435   parm_list = NULL_TREE;
436   for (parm = parms; parm; parm = parm->tp.par.next)
437     {
438       parm_decl = build_decl (PARM_DECL, get_identifier
439                               ((const char*) (parm->tp.par.variable_name)),
440                               tree_code_get_type (parm->type));
441
442       /* Some languages have different nominal and real types.  */
443       DECL_ARG_TYPE (parm_decl) = TREE_TYPE (parm_decl);
444       if (!DECL_ARG_TYPE (parm_decl))
445         abort ();
446       if (!fn_decl)
447         abort ();
448       DECL_CONTEXT (parm_decl) = fn_decl;
449       DECL_SOURCE_LOCATION (parm_decl) = loc;
450       parm_list = chainon (parm_decl, parm_list);
451     }
452
453   /* Back into reverse order as the back end likes them.  */
454   parm_list = nreverse (parm_list);
455
456   DECL_ARGUMENTS (fn_decl) = parm_list;
457
458   /* Save the decls for use when the args are referred to.  */
459   for (param_decl = DECL_ARGUMENTS (fn_decl),
460          this_parm = parms;
461        param_decl;
462        param_decl = TREE_CHAIN (param_decl),
463          this_parm = this_parm->tp.par.next)
464     {
465       if (!this_parm)
466         abort (); /* Too few.  */
467       *this_parm->tp.par.where_to_put_var_tree = param_decl;
468     }
469   if (this_parm)
470     abort (); /* Too many.  */
471
472   /* Create a new level at the start of the function.  */
473
474   pushlevel (0);
475
476   /* Force it to be output, else may be solely inlined.  */
477   TREE_ADDRESSABLE (fn_decl) = 1;
478
479   /* Stop -O3 from deleting it.  */
480   TREE_USED (fn_decl) = 1;
481 }
482
483 /* Wrapup a function contained in file FILENAME, ending at line LINENO.  */
484 void
485 tree_code_create_function_wrapup (location_t loc)
486 {
487   tree block;
488   tree fn_decl;
489   tree stmts = *getstmtlist ();
490
491   fn_decl = current_function_decl;
492
493   /* Pop the level.  */
494
495   block = poplevel (1, 0, 1);
496
497   /* And attach it to the function.  */
498
499   DECL_SAVED_TREE (fn_decl) = build3 (BIND_EXPR, void_type_node,
500                                       BLOCK_VARS (block),
501                                       stmts, block);
502
503   allocate_struct_function (fn_decl);
504   cfun->function_end_locus = loc;
505
506
507   /* Dump the original tree to a file.  */
508   dump_function (TDI_original, fn_decl);
509
510   /* Convert current function to GIMPLE for the middle end.  */
511   gimplify_function_tree (fn_decl);
512   dump_function (TDI_generic, fn_decl);
513
514   /* We are not inside of any scope now.  */
515   current_function_decl = NULL_TREE;
516   cfun = NULL;
517
518   /* Pass the current function off to the middle end.  */
519   (void)cgraph_node (fn_decl);
520   cgraph_finalize_function (fn_decl, false);
521 }
522
523 /*
524    Create a variable.
525
526    The storage class is STORAGE_CLASS (eg LOCAL).
527    The name is CHARS/LENGTH.
528    The type is EXPRESSION_TYPE (eg UNSIGNED_TYPE).
529    The init tree is INIT.
530 */
531
532 tree
533 tree_code_create_variable (unsigned int storage_class,
534                            unsigned char* chars,
535                            unsigned int length,
536                            unsigned int expression_type,
537                            tree init,
538                            location_t loc)
539 {
540   tree var_type;
541   tree var_id;
542   tree var_decl;
543
544   /* 1. Build the type.  */
545   var_type = tree_code_get_type (expression_type);
546
547   /* 2. Build the name.  */
548   if (chars[length] != 0)
549     abort (); /* Should be null terminated.  */
550
551   var_id = get_identifier ((const char*)chars);
552
553   /* 3. Build the decl and set up init.  */
554   var_decl = build_decl (VAR_DECL, var_id, var_type);
555
556   /* 3a. Initialization.  */
557   if (init)
558     DECL_INITIAL (var_decl) = fold (build1 (CONVERT_EXPR, var_type, init));
559   else
560     DECL_INITIAL (var_decl) = NULL_TREE;
561
562   if (TYPE_SIZE (var_type) == 0)
563     abort (); /* Did not calculate size.  */
564
565   DECL_CONTEXT (var_decl) = current_function_decl;
566
567   DECL_SOURCE_LOCATION (var_decl) = loc;
568
569   /* Set the storage mode and whether only visible in the same file.  */
570   switch (storage_class)
571     {
572     case STATIC_STORAGE:
573       TREE_STATIC (var_decl) = 1;
574       TREE_PUBLIC (var_decl) = 0;
575       break;
576
577     case AUTOMATIC_STORAGE:
578       TREE_STATIC (var_decl) = 0;
579       TREE_PUBLIC (var_decl) = 0;
580       break;
581
582     case EXTERNAL_DEFINITION_STORAGE:
583       TREE_STATIC (var_decl) = 0;
584       TREE_PUBLIC (var_decl) = 1;
585       break;
586
587     case EXTERNAL_REFERENCE_STORAGE:
588       DECL_EXTERNAL (var_decl) = 1;
589       TREE_PUBLIC (var_decl) = 0;
590       break;
591
592     default:
593       abort ();
594     }
595
596   /* This should really only be set if the variable is used.  */
597   TREE_USED (var_decl) = 1;
598
599   /* Expand declaration and initial value if any.  */
600
601   if (TREE_STATIC (var_decl))
602     rest_of_decl_compilation (var_decl, 0, 0);
603
604   TYPE_NAME (TREE_TYPE (var_decl)) = TYPE_NAME (var_type);
605   return pushdecl (copy_node (var_decl));
606
607 }
608
609
610 /* Generate code for return statement.  Type is in TYPE, expression
611    is in EXP if present.  */
612
613 void
614 tree_code_generate_return (tree type, tree exp)
615 {
616   tree setret;
617   tree param;
618
619   for (param = DECL_ARGUMENTS (current_function_decl);
620        param;
621        param = TREE_CHAIN (param))
622     {
623       if (DECL_CONTEXT (param) != current_function_decl)
624         abort ();
625     }
626
627   if (exp && TREE_TYPE (TREE_TYPE (current_function_decl)) != void_type_node)
628     {
629       setret = fold (build2 (MODIFY_EXPR, type, 
630                              DECL_RESULT (current_function_decl),
631                              fold (build1 (CONVERT_EXPR, type, exp))));
632       TREE_SIDE_EFFECTS (setret) = 1;
633       TREE_USED (setret) = 1;
634       setret = build1 (RETURN_EXPR, type, setret);
635     }
636    else
637      setret = build1 (RETURN_EXPR, type, NULL_TREE);
638
639    append_to_statement_list_force (setret, getstmtlist ());
640 }
641
642
643 /* Output the code for this expression statement CODE.  */
644
645 void
646 tree_code_output_expression_statement (tree code, location_t loc)
647 {
648   /* Output the line number information.  */
649   SET_EXPR_LOCATION (code, loc);
650   TREE_USED (code) = 1;
651   TREE_SIDE_EFFECTS (code) = 1;
652   /* put CODE into the code list.  */
653   append_to_statement_list_force (code, getstmtlist ());
654 }
655
656 /* Return a tree for a constant integer value in the token TOK.  No
657    size checking is done.  */
658
659 tree
660 tree_code_get_integer_value (unsigned char* chars, unsigned int length)
661 {
662   long long int val = 0;
663   unsigned int ix;
664   unsigned int start = 0;
665   int negative = 1;
666   switch (chars[0])
667     {
668     case (unsigned char)'-':
669       negative = -1;
670       start = 1;
671       break;
672
673     case (unsigned char)'+':
674       start = 1;
675       break;
676
677     default:
678       break;
679     }
680   for (ix = start; ix < length; ix++)
681     val = val * 10 + chars[ix] - (unsigned char)'0';
682   val = val*negative;
683   return build_int_cst (NULL_TREE, val & 0xffffffff, (val >> 32) & 0xffffffff);
684 }
685
686 /* Return the tree for an expresssion, type EXP_TYPE (see treetree.h)
687    with tree type TYPE and with operands1 OP1, OP2 (maybe), OP3 (maybe).  */
688 tree
689 tree_code_get_expression (unsigned int exp_type,
690                           tree type, tree op1, tree op2,
691                           tree op3 ATTRIBUTE_UNUSED)
692 {
693   tree ret1;
694   int operator;
695
696   switch (exp_type)
697     {
698     case EXP_ASSIGN:
699       if (!op1 || !op2)
700         abort ();
701       operator = MODIFY_EXPR;
702       ret1 = fold (build2 (operator, void_type_node, op1,
703                            fold (build1 (CONVERT_EXPR, TREE_TYPE (op1), op2))));
704
705       break;
706
707     case EXP_PLUS:
708       operator = PLUS_EXPR;
709       goto binary_expression;
710
711     case EXP_MINUS:
712       operator = MINUS_EXPR;
713       goto binary_expression;
714
715     case EXP_EQUALS:
716       operator = EQ_EXPR;
717       goto binary_expression;
718
719     /* Expand a binary expression.  Ensure the operands are the right type.  */
720     binary_expression:
721       if (!op1 || !op2)
722         abort ();
723       ret1  =  fold (build2 (operator, type,
724                        fold (build1 (CONVERT_EXPR, type, op1)),
725                        fold (build1 (CONVERT_EXPR, type, op2))));
726       break;
727
728       /* Reference to a variable.  This is dead easy, just return the
729          decl for the variable.  If the TYPE is different than the
730          variable type, convert it.  */
731     case EXP_REFERENCE:
732       if (!op1)
733         abort ();
734       if (type == TREE_TYPE (op1))
735         ret1 = op1;
736       else
737         ret1 = fold (build1 (CONVERT_EXPR, type, op1));
738       break;
739
740     case EXP_FUNCTION_INVOCATION:
741       if (!op1 || !op2)
742         abort ();
743
744       {
745         tree fun_ptr;
746         fun_ptr = fold (build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (op1)),
747                                 op1));
748         ret1 = build3 (CALL_EXPR, type, fun_ptr, nreverse (op2), NULL_TREE);
749       }
750       break;
751
752     default:
753       abort ();
754     }
755
756   return ret1;
757 }
758
759 /* Init parameter list and return empty list.  */
760
761 tree
762 tree_code_init_parameters (void)
763 {
764   return NULL_TREE;
765 }
766
767 /* Add a parameter EXP whose expression type is EXP_PROTO to list
768    LIST, returning the new list.  */
769
770 tree
771 tree_code_add_parameter (tree list, tree proto_exp, tree exp)
772 {
773   tree new_exp;
774   new_exp = tree_cons (NULL_TREE,
775                        fold (build1 (CONVERT_EXPR, TREE_TYPE (proto_exp), exp)),
776                        NULL_TREE);
777   if (!list)
778     return new_exp;
779   return chainon (new_exp, list);
780 }
781
782 /* Get a stringpool entry for a string S of length L.  This is needed
783    because the GTY routines don't mark strings, forcing you to put
784    them into stringpool, which is never freed.  */
785
786 const char*
787 get_string (const char *s, size_t l)
788 {
789   tree t;
790   t = get_identifier_with_length (s, l);
791   return IDENTIFIER_POINTER(t);
792 }
793   
794 /* Save typing debug_tree all the time. Dump a tree T pretty and
795    concise.  */
796
797 void dt (tree t);
798
799 void
800 dt (tree t)
801 {
802   debug_tree (t);
803 }
804
805 /* Routines Expected by gcc:  */
806
807 /* These are used to build types for various sizes.  The code below
808    is a simplified version of that of GNAT.  */
809
810 #ifndef MAX_BITS_PER_WORD
811 #define MAX_BITS_PER_WORD  BITS_PER_WORD
812 #endif
813
814 /* This variable keeps a table for types for each precision so that we only 
815    allocate each of them once. Signed and unsigned types are kept separate.  */
816 static GTY(()) tree signed_and_unsigned_types[MAX_BITS_PER_WORD + 1][2];
817
818 /* XXX is this definition OK? */
819 static tree
820 tree_lang_truthvalue_conversion (tree expr)
821 {
822   return expr;
823 }
824
825 /* Mark EXP saying that we need to be able to take the
826    address of it; it should not be allocated in a register.
827    Value is 1 if successful.  
828    
829    This implementation was copied from c-decl.c. */
830
831 static bool
832 tree_mark_addressable (tree exp)
833 {
834   register tree x = exp;
835   while (1)
836     switch (TREE_CODE (x))
837       {
838       case COMPONENT_REF:
839       case ADDR_EXPR:
840       case ARRAY_REF:
841       case REALPART_EXPR:
842       case IMAGPART_EXPR:
843         x = TREE_OPERAND (x, 0);
844         break;
845   
846       case CONSTRUCTOR:
847         TREE_ADDRESSABLE (x) = 1;
848         return 1;
849
850       case VAR_DECL:
851       case CONST_DECL:
852       case PARM_DECL:
853       case RESULT_DECL:
854         if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
855             && DECL_NONLOCAL (x))
856           {
857             if (TREE_PUBLIC (x))
858               {
859                 error ("global register variable `%s' used in nested function",
860                        IDENTIFIER_POINTER (DECL_NAME (x)));
861                 return 0;
862               }
863             pedwarn ("register variable `%s' used in nested function",
864                      IDENTIFIER_POINTER (DECL_NAME (x)));
865           }
866         else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
867           {
868             if (TREE_PUBLIC (x))
869               {
870                 error ("address of global register variable `%s' requested",
871                        IDENTIFIER_POINTER (DECL_NAME (x)));
872                 return 0;
873               }
874
875             pedwarn ("address of register variable `%s' requested",
876                      IDENTIFIER_POINTER (DECL_NAME (x)));
877           }
878
879         /* drops in */
880       case FUNCTION_DECL:
881         TREE_ADDRESSABLE (x) = 1;
882
883       default:
884         return 1;
885     }
886 }
887   
888 /* Return an integer type with the number of bits of precision given by  
889    PRECISION.  UNSIGNEDP is nonzero if the type is unsigned; otherwise
890    it is a signed type.  */
891   
892 static tree
893 tree_lang_type_for_size (unsigned precision, int unsignedp)
894 {
895   tree t;
896
897   if (precision <= MAX_BITS_PER_WORD
898       && signed_and_unsigned_types[precision][unsignedp] != 0)
899     return signed_and_unsigned_types[precision][unsignedp];
900
901   if (unsignedp)
902     t = signed_and_unsigned_types[precision][1]
903       = make_unsigned_type (precision);
904   else
905     t = signed_and_unsigned_types[precision][0]
906       = make_signed_type (precision);
907   
908   return t;
909 }
910
911 /* Return a data type that has machine mode MODE.  UNSIGNEDP selects
912    an unsigned type; otherwise a signed type is returned.  */
913
914 static tree
915 tree_lang_type_for_mode (enum machine_mode mode, int unsignedp)
916 {
917   return tree_lang_type_for_size (GET_MODE_BITSIZE (mode), unsignedp);
918 }
919
920 /* Return the unsigned version of a TYPE_NODE, a scalar type.  */
921
922 static tree
923 tree_lang_unsigned_type (tree type_node)
924 {
925   return tree_lang_type_for_size (TYPE_PRECISION (type_node), 1);
926 }
927
928 /* Return the signed version of a TYPE_NODE, a scalar type.  */
929
930 static tree
931 tree_lang_signed_type (tree type_node)
932 {
933   return tree_lang_type_for_size (TYPE_PRECISION (type_node), 0);
934 }
935
936 /* Return a type the same as TYPE except unsigned or signed according to
937    UNSIGNEDP.  */
938
939 static tree
940 tree_lang_signed_or_unsigned_type (int unsignedp, tree type)
941 {
942   if (! INTEGRAL_TYPE_P (type) || TYPE_UNSIGNED (type) == unsignedp)
943     return type;
944   else
945     return tree_lang_type_for_size (TYPE_PRECISION (type), unsignedp);
946 }
947 \f
948 /* These functions and variables deal with binding contours.  We only
949    need these functions for the list of PARM_DECLs, but we leave the
950    functions more general; these are a simplified version of the
951    functions from GNAT.  */
952
953 /* For each binding contour we allocate a binding_level structure which records
954    the entities defined or declared in that contour. Contours include:
955
956         the global one
957         one for each subprogram definition
958         one for each compound statement (declare block)
959
960    Binding contours are used to create GCC tree BLOCK nodes.  */
961
962 struct binding_level
963 {
964   /* A chain of ..._DECL nodes for all variables, constants, functions,
965      parameters and type declarations.  These ..._DECL nodes are chained
966      through the TREE_CHAIN field. Note that these ..._DECL nodes are stored
967      in the reverse of the order supplied to be compatible with the
968      back-end.  */
969   tree names;
970   /* For each level (except the global one), a chain of BLOCK nodes for all
971      the levels that were entered and exited one level down from this one.  */
972   tree blocks;
973
974   tree stmts;
975   /* The binding level containing this one (the enclosing binding level). */
976   struct binding_level *level_chain;
977 };
978
979 /* The binding level currently in effect.  */
980 static struct binding_level *current_binding_level = NULL;
981
982 /* The outermost binding level. This binding level is created when the
983    compiler is started and it will exist through the entire compilation.  */
984 static struct binding_level *global_binding_level;
985
986 /* Binding level structures are initialized by copying this one.  */
987 static struct binding_level clear_binding_level = {NULL, NULL, NULL, NULL };
988 \f
989 /* Return non-zero if we are currently in the global binding level.  */
990
991 static int
992 global_bindings_p (void)
993 {
994   return current_binding_level == global_binding_level ? -1 : 0;
995 }
996
997
998 /* Return the list of declarations in the current level. Note that this list
999    is in reverse order (it has to be so for back-end compatibility).  */
1000
1001 static tree
1002 getdecls (void)
1003 {
1004   return current_binding_level->names;
1005 }
1006
1007 /* Return a STATMENT_LIST for the current block.  */
1008
1009 static tree*
1010 getstmtlist (void)
1011 {
1012   return &current_binding_level->stmts;
1013 }
1014
1015 /* Enter a new binding level. The input parameter is ignored, but has to be
1016    specified for back-end compatibility.  */
1017
1018 static void
1019 pushlevel (int ignore ATTRIBUTE_UNUSED)
1020 {
1021   struct binding_level *newlevel = xmalloc (sizeof (struct binding_level));
1022
1023   *newlevel = clear_binding_level;
1024
1025   /* Add this level to the front of the chain (stack) of levels that are
1026      active.  */
1027   newlevel->level_chain = current_binding_level;
1028   current_binding_level = newlevel;
1029   current_binding_level->stmts = alloc_stmt_list ();
1030 }
1031
1032 /* Exit a binding level.
1033    Pop the level off, and restore the state of the identifier-decl mappings
1034    that were in effect when this level was entered.
1035
1036    If KEEP is nonzero, this level had explicit declarations, so
1037    and create a "block" (a BLOCK node) for the level
1038    to record its declarations and subblocks for symbol table output.
1039
1040    If FUNCTIONBODY is nonzero, this level is the body of a function,
1041    so create a block as if KEEP were set and also clear out all
1042    label names.
1043
1044    If REVERSE is nonzero, reverse the order of decls before putting
1045    them into the BLOCK.  */
1046
1047 static tree
1048 poplevel (int keep, int reverse, int functionbody)
1049 {
1050   /* Points to a BLOCK tree node. This is the BLOCK node construted for the
1051      binding level that we are about to exit and which is returned by this
1052      routine.  */
1053   tree block_node = NULL_TREE;
1054   tree decl_chain;
1055   tree subblock_chain = current_binding_level->blocks;
1056   tree subblock_node;
1057
1058   /* Reverse the list of *_DECL nodes if desired.  Note that the ..._DECL
1059      nodes chained through the `names' field of current_binding_level are in
1060      reverse order except for PARM_DECL node, which are explicitely stored in
1061      the right order.  */
1062   decl_chain = (reverse) ? nreverse (current_binding_level->names)
1063                          : current_binding_level->names;
1064
1065   /* If there were any declarations in the current binding level, or if this
1066      binding level is a function body, or if there are any nested blocks then
1067      create a BLOCK node to record them for the life of this function.  */
1068   if (keep || functionbody)
1069     block_node = build_block (keep ? decl_chain : 0, 0, subblock_chain, 0, 0);
1070
1071   /* Record the BLOCK node just built as the subblock its enclosing scope.  */
1072   for (subblock_node = subblock_chain; subblock_node;
1073        subblock_node = TREE_CHAIN (subblock_node))
1074     BLOCK_SUPERCONTEXT (subblock_node) = block_node;
1075
1076   /* Clear out the meanings of the local variables of this level.  */
1077
1078   for (subblock_node = decl_chain; subblock_node;
1079        subblock_node = TREE_CHAIN (subblock_node))
1080     if (DECL_NAME (subblock_node) != 0)
1081       /* If the identifier was used or addressed via a local extern decl,  
1082          don't forget that fact.   */
1083       if (DECL_EXTERNAL (subblock_node))
1084         {
1085           if (TREE_USED (subblock_node))
1086             TREE_USED (DECL_NAME (subblock_node)) = 1;
1087         }
1088
1089   /* Pop the current level.  */
1090   current_binding_level = current_binding_level->level_chain;
1091
1092   if (functionbody)
1093     {
1094       /* This is the top level block of a function.  */
1095       DECL_INITIAL (current_function_decl) = block_node;
1096     }
1097   else if (block_node)
1098     {
1099       current_binding_level->blocks
1100         = chainon (current_binding_level->blocks, block_node);
1101     }
1102
1103   /* If we did not make a block for the level just exited, any blocks made for
1104      inner levels (since they cannot be recorded as subblocks in that level)
1105      must be carried forward so they will later become subblocks of something
1106      else.  */
1107   else if (subblock_chain)
1108     current_binding_level->blocks
1109       = chainon (current_binding_level->blocks, subblock_chain);
1110   if (block_node)
1111     TREE_USED (block_node) = 1;
1112
1113   return block_node;
1114 }
1115 \f
1116 /* Insert BLOCK at the end of the list of subblocks of the
1117    current binding level.  This is used when a BIND_EXPR is expanded,
1118    to handle the BLOCK node inside the BIND_EXPR.  */
1119
1120 static void
1121 insert_block (tree block)
1122 {
1123   TREE_USED (block) = 1;
1124   current_binding_level->blocks
1125     = chainon (current_binding_level->blocks, block);
1126 }
1127
1128
1129 /* Records a ..._DECL node DECL as belonging to the current lexical scope.
1130    Returns the ..._DECL node. */
1131
1132 tree
1133 pushdecl (tree decl)
1134 {
1135   /* External objects aren't nested, other objects may be.  */
1136     
1137   if ((DECL_EXTERNAL (decl)) || (decl==current_function_decl))
1138     DECL_CONTEXT (decl) = 0;
1139   else
1140     DECL_CONTEXT (decl) = current_function_decl;
1141
1142   /* Put the declaration on the list.  The list of declarations is in reverse
1143      order. The list will be reversed later if necessary.  This needs to be
1144      this way for compatibility with the back-end.  */
1145
1146   TREE_CHAIN (decl) = current_binding_level->names;
1147   current_binding_level->names = decl;
1148
1149   /* For the declartion of a type, set its name if it is not already set. */
1150
1151   if (TREE_CODE (decl) == TYPE_DECL
1152       && TYPE_NAME (TREE_TYPE (decl)) == 0)
1153     TYPE_NAME (TREE_TYPE (decl)) = DECL_NAME (decl);
1154
1155   return decl;
1156 }
1157 \f
1158
1159 static void
1160 tree_push_type_decl(tree id, tree type_node)
1161 {
1162   tree decl = build_decl (TYPE_DECL, id, type_node);
1163   TYPE_NAME (type_node) = id;
1164   pushdecl (decl);
1165 }
1166
1167 #define NULL_BINDING_LEVEL (struct binding_level *) NULL                        
1168
1169 /* Create the predefined scalar types of C,
1170    and some nodes representing standard constants (0, 1, (void *) 0).
1171    Initialize the global binding level.
1172    Make definitions for built-in primitive functions.  */
1173
1174 void
1175 treelang_init_decl_processing (void)
1176 {
1177   current_function_decl = NULL;
1178   current_binding_level = NULL_BINDING_LEVEL;
1179   pushlevel (0);        /* make the binding_level structure for global names */
1180   global_binding_level = current_binding_level;
1181
1182   build_common_tree_nodes (flag_signed_char);
1183
1184   /* set standard type names */
1185
1186   /* Define `int' and `char' last so that they are not overwritten.  */
1187   tree_push_type_decl (NULL_TREE, intQI_type_node);
1188   tree_push_type_decl (NULL_TREE, intHI_type_node);
1189   tree_push_type_decl (NULL_TREE, intSI_type_node);
1190   tree_push_type_decl (NULL_TREE, intDI_type_node);
1191 #if HOST_BITS_PER_WIDE_INT >= 64
1192   tree_push_type_decl (NULL_TREE, intTI_type_node);
1193 #endif
1194   tree_push_type_decl (NULL_TREE, unsigned_intQI_type_node);
1195   tree_push_type_decl (NULL_TREE, unsigned_intHI_type_node);
1196   tree_push_type_decl (NULL_TREE, unsigned_intSI_type_node);
1197   tree_push_type_decl (NULL_TREE, unsigned_intDI_type_node);
1198 #if HOST_BITS_PER_WIDE_INT >= 64
1199   tree_push_type_decl (NULL_TREE, unsigned_intTI_type_node);
1200 #endif
1201
1202   tree_push_type_decl (get_identifier ("int"), integer_type_node);
1203   tree_push_type_decl (get_identifier ("char"), char_type_node);
1204   tree_push_type_decl (get_identifier ("long int"),
1205                               long_integer_type_node);
1206   tree_push_type_decl (get_identifier ("unsigned int"),
1207                               unsigned_type_node);
1208   tree_push_type_decl (get_identifier ("long unsigned int"),
1209                               long_unsigned_type_node);
1210   tree_push_type_decl (get_identifier ("long long int"),
1211                               long_long_integer_type_node);
1212   tree_push_type_decl (get_identifier ("long long unsigned int"),
1213                               long_long_unsigned_type_node);
1214   tree_push_type_decl (get_identifier ("short int"),
1215                               short_integer_type_node);
1216   tree_push_type_decl (get_identifier ("short unsigned int"),
1217                               short_unsigned_type_node);
1218   tree_push_type_decl (get_identifier ("signed char"),
1219                               signed_char_type_node);
1220   tree_push_type_decl (get_identifier ("unsigned char"),
1221                               unsigned_char_type_node);
1222   size_type_node = make_unsigned_type (POINTER_SIZE);
1223   tree_push_type_decl (get_identifier ("size_t"), size_type_node);
1224   set_sizetype (size_type_node);
1225
1226   build_common_tree_nodes_2 (/* short_double= */ 0);
1227
1228   tree_push_type_decl (get_identifier ("float"), float_type_node);
1229   tree_push_type_decl (get_identifier ("double"), double_type_node);
1230   tree_push_type_decl (get_identifier ("long double"), long_double_type_node);
1231   tree_push_type_decl (get_identifier ("void"), void_type_node);
1232
1233   /* Add any target-specific builtin functions.  */
1234   (*targetm.init_builtins) ();
1235
1236   pedantic_lvalues = pedantic;
1237 }
1238
1239 /* Return a definition for a builtin function named NAME and whose data type
1240    is TYPE.  TYPE should be a function type with argument types.
1241    FUNCTION_CODE tells later passes how to compile calls to this function.
1242    See tree.h for its possible values.
1243
1244    If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
1245    the name to be called if we can't opencode the function.  If
1246    ATTRS is nonzero, use that for the function's attribute list.
1247
1248    copied from gcc/c-decl.c
1249 */
1250
1251 static tree
1252 builtin_function (const char *name, tree type, int function_code,
1253                   enum built_in_class class, const char *library_name,
1254                   tree attrs)
1255 {
1256   tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
1257   DECL_EXTERNAL (decl) = 1;
1258   TREE_PUBLIC (decl) = 1;
1259   if (library_name)
1260     SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
1261   pushdecl (decl);
1262   DECL_BUILT_IN_CLASS (decl) = class;
1263   DECL_FUNCTION_CODE (decl) = function_code;
1264
1265   /* Possibly apply some default attributes to this built-in function.  */
1266   if (attrs)
1267     decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
1268   else
1269     decl_attributes (&decl, NULL_TREE, 0);
1270
1271   return decl;
1272 }
1273
1274 /* Treelang expand function langhook.  */
1275
1276 static void
1277 treelang_expand_function (tree fndecl)
1278 {
1279   /* We have nothing special to do while expanding functions for treelang.  */
1280   tree_rest_of_compilation (fndecl, 0);
1281 }
1282
1283 #include "debug.h" /* for debug_hooks, needed by gt-treelang-treetree.h */
1284 #include "gt-treelang-treetree.h"