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