xref: /openbsd-src/gnu/usr.bin/gcc/gcc/treelang/treetree.c (revision c87b03e512fc05ed6e0222f6fb0ae86264b1d05b)
1 /*
2 
3     TREELANG Compiler back end interface (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, 1999, 2000,
10     2001, 2002 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     ---------------------------------------------------------------------------
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  */
46 
47 /*
48   Assumption: garbage collection is never called implicitly.  It will
49   not be called 'at any time' when short of memory.  It will only be
50   called explicitly at the end of each function.  This removes the
51   need for a *lot* of bother to ensure everything is in the mark trees
52   at all times.  */
53 
54   /* Note it is OK to use GCC extensions such as long long in a compiler front end.
55      This is because the GCC front ends are built using GCC. */
56 
57 /* Standard/OS headers.  */
58 
59 #include <stdlib.h>
60 #include <unistd.h>
61 #include "safe-ctype.h"
62 #include <errno.h>
63 #include <stdarg.h>
64 #include <limits.h>
65 #include <string.h>
66 
67 #include <fcntl.h>
68 #include <getopt.h>
69 #include <stdio.h>
70 
71 /* GCC headers.  */
72 
73 #include "config.h"
74 #include "ansidecl.h"
75 #include "system.h"
76 #include "tree.h"
77 #include "flags.h"
78 #include "output.h"
79 #include "c-tree.h"
80 #include "rtl.h"
81 #include "ggc.h"
82 #include "toplev.h"
83 #include "varray.h"
84 #include "langhooks-def.h"
85 #include "langhooks.h"
86 
87 #include "treelang.h"
88 #include "treetree.h"
89 
90 extern int option_main;
91 extern char **file_names;
92 
93 /* The front end language hooks (addresses of code for this front
94    end).  Mostly just use the C routines.  */
95 
96 #undef LANG_HOOKS_TRUTHVALUE_CONVERSION
97 #define LANG_HOOKS_TRUTHVALUE_CONVERSION c_common_truthvalue_conversion
98 #undef LANG_HOOKS_MARK_ADDRESSABLE
99 #define LANG_HOOKS_MARK_ADDRESSABLE c_mark_addressable
100 #undef LANG_HOOKS_SIGNED_TYPE
101 #define LANG_HOOKS_SIGNED_TYPE c_common_signed_type
102 #undef LANG_HOOKS_UNSIGNED_TYPE
103 #define LANG_HOOKS_UNSIGNED_TYPE c_common_unsigned_type
104 #undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
105 #define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE c_common_signed_or_unsigned_type
106 #undef LANG_HOOKS_TYPE_FOR_MODE
107 #define LANG_HOOKS_TYPE_FOR_MODE c_common_type_for_mode
108 #undef LANG_HOOKS_TYPE_FOR_SIZE
109 #define LANG_HOOKS_TYPE_FOR_SIZE c_common_type_for_size
110 #undef LANG_HOOKS_PARSE_FILE
111 #define LANG_HOOKS_PARSE_FILE treelang_parse_file
112 #undef LANG_HOOKS_COMMON_ATTRIBUTE_TABLE
113 #define LANG_HOOKS_COMMON_ATTRIBUTE_TABLE c_common_attribute_table
114 #undef LANG_HOOKS_FORMAT_ATTRIBUTE_TABLE
115 #define LANG_HOOKS_FORMAT_ATTRIBUTE_TABLE c_common_format_attribute_table
116 #undef LANG_HOOKS_INSERT_DEFAULT_ATTRIBUTES
117 #define LANG_HOOKS_INSERT_DEFAULT_ATTRIBUTES c_insert_default_attributes
118 
119 /* Hook routines and data unique to treelang.  */
120 
121 #undef LANG_HOOKS_INIT
122 #define LANG_HOOKS_INIT treelang_init
123 #undef LANG_HOOKS_NAME
124 #define LANG_HOOKS_NAME	"GNU treelang"
125 #undef LANG_HOOKS_FINISH
126 #define LANG_HOOKS_FINISH		treelang_finish
127 #undef LANG_HOOKS_DECODE_OPTION
128 #define LANG_HOOKS_DECODE_OPTION treelang_decode_option
129 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
130 
131 /* Tree code type/name/code tables.  */
132 
133 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
134 
135 const char tree_code_type[] = {
136 #include "tree.def"
137   'x'
138 };
139 #undef DEFTREECODE
140 
141 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
142 
143 const unsigned char tree_code_length[] = {
144 #include "tree.def"
145   0
146 };
147 #undef DEFTREECODE
148 
149 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
150 
151 const char *const tree_code_name[] = {
152 #include "tree.def"
153   "@@dummy"
154 };
155 #undef DEFTREECODE
156 
157 /* Number of bits in int and char - accessed by front end.  */
158 
159 unsigned int tree_code_int_size = 0;
160 unsigned int tree_code_char_size = 0;
161 
162 /* Return the tree stuff for this type TYPE_NUM.  */
163 
164 tree
tree_code_get_type(int type_num)165 tree_code_get_type (int type_num)
166 {
167   switch (type_num)
168     {
169     case SIGNED_CHAR:
170       return signed_char_type_node;
171 
172     case UNSIGNED_CHAR:
173       return unsigned_char_type_node;
174 
175     case SIGNED_INT:
176       return integer_type_node;
177 
178     case UNSIGNED_INT:
179       return unsigned_type_node;
180 
181     case VOID_TYPE:
182       return void_type_node;
183 
184     default:
185       abort ();
186     }
187 }
188 
189 /* Output the code for the start of an if statement.  The test
190    expression is EXP (true if not zero), and the stmt occurred at line
191    LINENO in file FILENAME.  */
192 
193 void
tree_code_if_start(tree exp,unsigned char * filename,int lineno)194 tree_code_if_start (tree exp, unsigned char* filename, int lineno)
195 {
196   tree cond_exp;
197   cond_exp = build (NE_EXPR,
198                  TREE_TYPE (exp),
199                  exp,
200                  build1 (CONVERT_EXPR, TREE_TYPE (exp), integer_zero_node));
201   emit_line_note ((const char *)filename, lineno); /* Output the line number information.  */
202   expand_start_cond (cond_exp, /* Exit-able if nonzero.  */ 0);
203 }
204 
205 /* Output the code for the else of an if statement.  The else occurred
206    at line LINENO in file FILENAME.  */
207 
208 void
tree_code_if_else(unsigned char * filename,int lineno)209 tree_code_if_else (unsigned char* filename, int lineno)
210 {
211   emit_line_note ((const char *)filename, lineno); /* Output the line number information.  */
212   expand_start_else ();
213 }
214 
215 /* Output the code for the end_if an if statement.  The end_if (final brace) occurred
216    at line LINENO in file FILENAME.  */
217 
218 void
tree_code_if_end(unsigned char * filename,int lineno)219 tree_code_if_end (unsigned char* filename, int lineno)
220 {
221   emit_line_note ((const char *)filename, lineno); /* Output the line number information.  */
222   expand_end_cond ();
223 }
224 
225 /* Create a function.  The prototype name is NAME, storage class is
226    STORAGE_CLASS, type of return variable is RET_TYPE, parameter lists
227    is PARMS, returns decl for this function.  */
228 
229 tree
tree_code_create_function_prototype(unsigned char * chars,unsigned int storage_class,unsigned int ret_type,struct prod_token_parm_item * parms,unsigned char * filename,int lineno)230 tree_code_create_function_prototype (unsigned char* chars,
231                                     unsigned int storage_class,
232                                     unsigned int ret_type,
233                                     struct prod_token_parm_item* parms,
234                                     unsigned char* filename,
235                                     int lineno)
236 {
237 
238   tree id;
239   struct prod_token_parm_item* parm;
240   tree type_list = NULL_TREE;
241   tree type_node;
242   tree fn_type;
243   tree fn_decl;
244 
245   /* Build the type.  */
246   id = get_identifier ((const char*)chars);
247   for (parm = parms; parm; parm = parm->tp.par.next)
248     {
249       type_node = get_type_for_numeric_type (parm->type);
250       type_list = tree_cons (NULL_TREE, type_node, type_list);
251     }
252   /* Last parm if void indicates fixed length list (as opposed to
253      printf style va_* list).  */
254   type_list = tree_cons (NULL_TREE, void_type_node, type_list);
255   /* The back end needs them in reverse order.  */
256   type_list = nreverse (type_list);
257 
258   type_node = get_type_for_numeric_type (ret_type);
259   fn_type = build_function_type (type_node, type_list);
260 
261   id = get_identifier ((const char*)chars);
262   fn_decl = build_decl (FUNCTION_DECL, id, fn_type);
263 
264   DECL_CONTEXT (fn_decl) = NULL_TREE; /* Nested functions not supported here.  */
265   DECL_SOURCE_FILE (fn_decl) = (const char *)filename;
266  /*  if (lineno > 1000000)
267     ; */ /* Probably the line # is rubbish because someone forgot to set
268     the line number - and unfortunately impossible line #s are used as
269     magic flags at various times. The longest known function for
270     example is about 550,000 lines (it was written in COBOL).  */
271   DECL_SOURCE_LINE (fn_decl) = lineno;
272 
273   TREE_USED (fn_decl) = 1;
274 
275   /* Real name (optional).  */
276   SET_DECL_ASSEMBLER_NAME (fn_decl, DECL_NAME (fn_decl));
277 
278   TREE_PUBLIC (fn_decl) = 0;
279   DECL_EXTERNAL (fn_decl) = 0;
280   TREE_STATIC (fn_decl) = 0;
281   switch (storage_class)
282     {
283     case STATIC_STORAGE:
284       TREE_PUBLIC (fn_decl) = 0;
285       break;
286 
287     case EXTERNAL_DEFINITION_STORAGE:
288       TREE_PUBLIC (fn_decl) = 1;
289       TREE_STATIC (fn_decl) = 0;
290       DECL_EXTERNAL (fn_decl) = 0;
291       break;
292 
293     case EXTERNAL_REFERENCE_STORAGE:
294       TREE_PUBLIC (fn_decl) = 0;
295       DECL_EXTERNAL (fn_decl) = 1;
296       break;
297 
298 
299     case AUTOMATIC_STORAGE:
300     default:
301       abort ();
302     }
303 
304   /* Process declaration of function defined elsewhere.  */
305   rest_of_decl_compilation (fn_decl, NULL, 1, 0);
306 
307   return fn_decl;
308 }
309 
310 
311 /* Output code for start of function; the decl of the function is in
312     PREV_SAVED (as created by tree_code_create_function_prototype),
313     the function is at line number LINENO in file FILENAME.  The
314     parameter details are in the lists PARMS. Returns nothing.  */
315 void
tree_code_create_function_initial(tree prev_saved,unsigned char * filename,int lineno,struct prod_token_parm_item * parms)316 tree_code_create_function_initial (tree prev_saved,
317                                   unsigned char* filename,
318                                   int lineno,
319                                   struct prod_token_parm_item* parms)
320 {
321   tree fn_decl;
322   tree param_decl;
323   tree next_param;
324   tree first_param;
325   tree parm_decl;
326   tree parm_list;
327   tree resultdecl;
328   struct prod_token_parm_item* this_parm;
329   struct prod_token_parm_item* parm;
330 
331   fn_decl = prev_saved;
332   if (!fn_decl)
333     abort ();
334 
335   /* Output message if not -quiet.  */
336   announce_function (fn_decl);
337 
338   /* This has something to do with forcing output also.  */
339   pushdecl (fn_decl);
340 
341   /* Set current function for error msgs etc.  */
342   current_function_decl = fn_decl;
343   DECL_INITIAL (fn_decl) = error_mark_node;
344 
345   DECL_SOURCE_FILE (fn_decl) = (const char *)filename;
346   DECL_SOURCE_LINE (fn_decl) = lineno;
347 
348   /* Prepare creation of rtl for a new function.  */
349 
350   resultdecl = DECL_RESULT (fn_decl) = build_decl (RESULT_DECL, NULL_TREE, TREE_TYPE (TREE_TYPE (fn_decl)));
351   DECL_CONTEXT (DECL_RESULT (fn_decl)) = fn_decl;
352   DECL_SOURCE_FILE (resultdecl) = (const char *)filename;
353   DECL_SOURCE_LINE (resultdecl) = lineno;
354   /* Work out the size. ??? is this needed.  */
355   layout_decl (DECL_RESULT (fn_decl), 0);
356 
357   /* Make the argument variable decls.  */
358   parm_list = NULL_TREE;
359   for (parm = parms; parm; parm = parm->tp.par.next)
360     {
361       parm_decl = build_decl (PARM_DECL, get_identifier
362                               ((const char*) (parm->tp.par.variable_name)),
363                               get_type_for_numeric_type (parm->type));
364 
365       /* Some languages have different nominal and real types.  */
366       DECL_ARG_TYPE (parm_decl) = TREE_TYPE (parm_decl);
367       if (!DECL_ARG_TYPE (parm_decl))
368         abort ();
369       if (!fn_decl)
370         abort ();
371       DECL_CONTEXT (parm_decl) = fn_decl;
372       DECL_SOURCE_FILE (parm_decl) = (const char *)filename;
373       DECL_SOURCE_LINE (parm_decl) = lineno;
374       parm_list = chainon (parm_decl, parm_list);
375     }
376 
377   /* Back into reverse order as the back end likes them.  */
378   parm_list = nreverse (parm_list);
379 
380   DECL_ARGUMENTS (fn_decl) = parm_list;
381 
382   /* Save the decls for use when the args are referred to.  */
383   for (param_decl = DECL_ARGUMENTS (fn_decl),
384          this_parm = parms;
385        param_decl;
386        param_decl = TREE_CHAIN (param_decl),
387          this_parm = this_parm->tp.par.next)
388     {
389       if (!this_parm)
390         abort (); /* Too few.  */
391       *this_parm->tp.par.where_to_put_var_tree = param_decl;
392     }
393   if (this_parm)
394     abort (); /* Too many.  */
395 
396   /* Output the decl rtl (not the rtl for the function code).  ???.
397      If the function is not defined in this file, when should you
398      execute this?  */
399   make_decl_rtl (fn_decl, NULL);
400 
401   /* Use filename/lineno from above.  */
402   init_function_start (fn_decl, (const char *)filename, lineno);
403 
404   /* Create rtl for startup code of function, such as saving registers.  */
405 
406   expand_function_start (fn_decl, 0);
407 
408   /* Function.c requires a push at the start of the function. that
409      looks like a bug to me but let's make it happy.  */
410 
411   (*lang_hooks.decls.pushlevel) (0);
412 
413   /* Create rtl for the start of a new scope.  */
414 
415   expand_start_bindings (2);
416 
417   /* Put the parameters into the symbol table.  */
418 
419   for (first_param = param_decl = nreverse (DECL_ARGUMENTS (fn_decl));
420        param_decl;
421        param_decl = next_param)
422     {
423       next_param = TREE_CHAIN (param_decl);
424       TREE_CHAIN (param_decl) = NULL;
425       /* layout_decl (param_decl, 0);  Already done in build_decl tej 13/4/2002.  */
426       pushdecl (param_decl);
427       if (DECL_CONTEXT (param_decl) != current_function_decl)
428         abort ();
429     }
430 
431   /* Store back the PARM_DECL nodes.  They appear in the right order.  */
432   DECL_ARGUMENTS (fn_decl) = getdecls ();
433 
434   /* Force it to be output, else may be solely inlined.  */
435   TREE_ADDRESSABLE (fn_decl) = 1;
436 
437   /* Stop -O3 from deleting it.  */
438   TREE_USED (fn_decl) = 1;
439 
440   /* Add a new level to the debugger symbol table.  */
441 
442   (*lang_hooks.decls.pushlevel) (0);
443 
444   /* Create rtl for the start of a new scope.  */
445 
446   expand_start_bindings (0);
447 
448   emit_line_note ((const char *)filename, lineno); /* Output the line number information.  */
449 }
450 
451 /* Wrapup a function contained in file FILENAME, ending at line LINENO.  */
452 void
tree_code_create_function_wrapup(unsigned char * filename,int lineno)453 tree_code_create_function_wrapup (unsigned char* filename,
454                                  int lineno)
455 {
456   tree block;
457   tree fn_decl;
458 
459   fn_decl = current_function_decl;
460 
461   emit_line_note ((const char *)filename, lineno); /* Output the line number information.  */
462 
463   /* Get completely built level from debugger symbol table.  */
464 
465   block = (*lang_hooks.decls.poplevel) (1, 0, 0);
466 
467   /* Emit rtl for end of scope.  */
468 
469   expand_end_bindings (block, 0, 1);
470 
471   /* Emit rtl for end of function.  */
472 
473   expand_function_end ((const char *)filename, lineno, 0);
474 
475   /* Pop the level.  */
476 
477   block = (*lang_hooks.decls.poplevel) (1, 0, 1);
478 
479   /* And attach it to the function.  */
480 
481   DECL_INITIAL (fn_decl) = block;
482 
483   /* Emit rtl for end of scope.  */
484 
485   expand_end_bindings (block, 0, 1);
486 
487   /* Call optimization and convert optimized rtl to assembly code.  */
488 
489   rest_of_compilation (fn_decl);
490 
491   /* We are not inside of any scope now.  */
492 
493   current_function_decl = NULL_TREE;
494 }
495 
496 /*
497    Create a variable.
498 
499    The storage class is STORAGE_CLASS (eg LOCAL).
500    The name is CHARS/LENGTH.
501    The type is EXPRESSION_TYPE (eg UNSIGNED_TYPE).
502    The init tree is INIT.
503 */
504 
505 tree
tree_code_create_variable(unsigned int storage_class,unsigned char * chars,unsigned int length,unsigned int expression_type,tree init,unsigned char * filename,int lineno)506 tree_code_create_variable (unsigned int storage_class,
507                                unsigned char* chars,
508                                unsigned int length,
509                                unsigned int expression_type,
510                                tree init,
511                                unsigned char* filename,
512                                int lineno)
513 {
514   tree var_type;
515   tree var_id;
516   tree var_decl;
517 
518   /* 1. Build the type.  */
519   var_type = get_type_for_numeric_type (expression_type);
520 
521   /* 2. Build the name.  */
522   if (chars[length] != 0)
523     abort (); /* Should be null terminated.  */
524 
525   var_id = get_identifier ((const char*)chars);
526 
527   /* 3. Build the decl and set up init.  */
528   var_decl = build_decl (VAR_DECL, var_id, var_type);
529 
530   /* 3a. Initialization.  */
531   if (init)
532     DECL_INITIAL (var_decl) = build1 (CONVERT_EXPR, var_type, init);
533   else
534     DECL_INITIAL (var_decl) = NULL_TREE;
535 
536   /* 4. Compute size etc.  */
537   layout_decl (var_decl, 0);
538 
539   if (TYPE_SIZE (var_type) == 0)
540     abort (); /* Did not calculate size.  */
541 
542   DECL_CONTEXT (var_decl) = current_function_decl;
543 
544   DECL_SOURCE_FILE (var_decl) = (const char *)filename;
545   DECL_SOURCE_LINE (var_decl) = lineno;
546 
547   /* Set the storage mode and whether only visible in the same file.  */
548   switch (storage_class)
549     {
550     case STATIC_STORAGE:
551       TREE_STATIC (var_decl) = 1;
552       TREE_PUBLIC (var_decl) = 0;
553       break;
554 
555     case AUTOMATIC_STORAGE:
556       TREE_STATIC (var_decl) = 0;
557       TREE_PUBLIC (var_decl) = 0;
558       break;
559 
560     case EXTERNAL_DEFINITION_STORAGE:
561       TREE_STATIC (var_decl) = 0;
562       TREE_PUBLIC (var_decl) = 1;
563       break;
564 
565     case EXTERNAL_REFERENCE_STORAGE:
566       DECL_EXTERNAL (var_decl) = 1;
567       TREE_PUBLIC (var_decl) = 0;
568       break;
569 
570     default:
571       abort ();
572     }
573 
574   /* This should really only be set if the variable is used.  */
575   TREE_USED (var_decl) = 1;
576 
577   /* Expand declaration and initial value if any.  */
578 
579   if (TREE_STATIC (var_decl))
580     rest_of_decl_compilation (var_decl, 0, 0, 0);
581   else
582     {
583       expand_decl (var_decl);
584       if (DECL_INITIAL (var_decl))
585         expand_decl_init (var_decl);
586     }
587 
588   return pushdecl (copy_node (var_decl));
589 
590 }
591 
592 
593 /* Generate code for return statement.  Type is in TYPE, expression
594    is in EXP if present.  */
595 
596 void
tree_code_generate_return(tree type,tree exp)597 tree_code_generate_return (tree type, tree exp)
598 {
599   tree setret;
600   tree param;
601 
602   for (param = DECL_ARGUMENTS (current_function_decl);
603        param;
604        param = TREE_CHAIN (param))
605     {
606       if (DECL_CONTEXT (param) != current_function_decl)
607         abort ();
608     }
609 
610   if (exp)
611     {
612       setret = build (MODIFY_EXPR, type, DECL_RESULT (current_function_decl),
613                      build1 (CONVERT_EXPR, type, exp));
614       TREE_SIDE_EFFECTS (setret) = 1;
615       TREE_USED (setret) = 1;
616       expand_expr_stmt (setret);
617     }
618   expand_return (DECL_RESULT (current_function_decl));
619 }
620 
621 /* Output the code for this expression statement CODE.  */
622 
623 
624 void
tree_code_output_expression_statement(tree code,unsigned char * filename,int lineno)625 tree_code_output_expression_statement (tree code,
626                                        unsigned char* filename, int lineno)
627 {
628   /* Output the line number information.  */
629   emit_line_note ((const char *)filename, lineno);
630   TREE_USED (code) = 1;
631   TREE_SIDE_EFFECTS (code) = 1;
632   expand_expr_stmt (code);
633 }
634 
635 /* Return a tree for a constant integer value in the token TOK.  No
636    size checking is done.  */
637 
638 tree
tree_code_get_integer_value(unsigned char * chars,unsigned int length)639 tree_code_get_integer_value (unsigned char* chars, unsigned int length)
640 {
641   long long int val = 0;
642   unsigned int ix;
643   unsigned int start = 0;
644   int negative = 1;
645   switch (chars[0])
646     {
647     case (unsigned char)'-':
648       negative = -1;
649       start = 1;
650       break;
651 
652     case (unsigned char)'+':
653       start = 1;
654       break;
655 
656     default:
657       break;
658     }
659   for (ix = start; ix < length; ix++)
660     val = val * 10 + chars[ix] - (unsigned char)'0';
661   val = val*negative;
662   return build_int_2 (val & 0xffffffff, (val >> 32) & 0xffffffff);
663 }
664 
665 /* Return the tree for an expresssion, type EXP_TYPE (see treetree.h)
666    with tree type TYPE and with operands1 OP1, OP2 (maybe), OP3 (maybe).  */
667 tree
tree_code_get_expression(unsigned int exp_type,tree type,tree op1,tree op2,tree op3 ATTRIBUTE_UNUSED)668 tree_code_get_expression (unsigned int exp_type,
669                           tree type, tree op1, tree op2, tree op3 ATTRIBUTE_UNUSED)
670 {
671   tree ret1;
672   int operator;
673 
674   switch (exp_type)
675     {
676     case EXP_ASSIGN:
677       if (!op1 || !op2)
678         abort ();
679       operator = MODIFY_EXPR;
680       ret1 = build (operator, type,
681                  op1,
682                  build1 (CONVERT_EXPR, type, op2));
683 
684       break;
685 
686     case EXP_PLUS:
687       operator = PLUS_EXPR;
688       goto binary_expression;
689 
690     case EXP_MINUS:
691       operator = MINUS_EXPR;
692       goto binary_expression;
693 
694     case EXP_EQUALS:
695       operator = EQ_EXPR;
696       goto binary_expression;
697 
698       /* Expand a binary expression.  Ensure the operands are the right type.  */
699     binary_expression:
700       if (!op1 || !op2)
701         abort ();
702       ret1  =  build (operator, type,
703                    build1 (CONVERT_EXPR, type, op1),
704                    build1 (CONVERT_EXPR, type, op2));
705       break;
706 
707       /* Reference to a variable.  This is dead easy, just return the
708          decl for the variable.  If the TYPE is different than the
709          variable type, convert it.  */
710     case EXP_REFERENCE:
711       if (!op1)
712         abort ();
713       if (type == TREE_TYPE (op1))
714         ret1 = op1;
715       else
716         ret1 = build1 (CONVERT_EXPR, type, op1);
717       break;
718 
719     case EXP_FUNCTION_INVOCATION:
720       if (!op1 || !op2)
721         abort ();
722       {
723         tree fun_ptr;
724         fun_ptr = build1 (ADDR_EXPR, build_pointer_type (type), op1);
725         ret1 = build (CALL_EXPR, type, fun_ptr, nreverse (op2));
726       }
727       break;
728 
729     default:
730       abort ();
731     }
732 
733   return ret1;
734 }
735 
736 /* Init parameter list and return empty list.  */
737 
738 tree
tree_code_init_parameters(void)739 tree_code_init_parameters (void)
740 {
741   return NULL_TREE;
742 }
743 
744 /* Add a parameter EXP whose expression type is EXP_PROTO to list
745    LIST, returning the new list.  */
746 
747 tree
tree_code_add_parameter(tree list,tree proto_exp,tree exp)748 tree_code_add_parameter (tree list, tree proto_exp, tree exp)
749 {
750   tree new_exp;
751   new_exp = tree_cons (NULL_TREE,
752                     build1 (CONVERT_EXPR, TREE_TYPE (proto_exp), exp),
753                     NULL_TREE);
754   if (!list)
755     return new_exp;
756   return chainon (new_exp, list);
757 }
758 
759 /* Get the tree type for this type whose number is NUMERIC_TYPE.  */
760 
761 tree
get_type_for_numeric_type(unsigned int numeric_type)762 get_type_for_numeric_type (unsigned int numeric_type)
763 {
764 
765   int size1;
766   int sign1;
767   switch (numeric_type)
768     {
769     case VOID_TYPE:
770       return void_type_node;
771 
772     case SIGNED_INT:
773       size1 = tree_code_int_size;
774       sign1 = 1;
775       break;
776 
777     case UNSIGNED_INT:
778       size1 = tree_code_int_size;
779       sign1 = 0;
780       break;
781 
782     case SIGNED_CHAR:
783       size1 = tree_code_char_size;
784       sign1 = 1;
785       break;
786 
787     case UNSIGNED_CHAR:
788       size1 = tree_code_char_size;
789       sign1 = 0;
790       break;
791 
792     default:
793       abort ();
794     }
795 
796   return tree_code_get_numeric_type (size1, sign1);
797 
798 }
799 
800 /* Return tree representing a numeric type of size SIZE1 bits and
801    signed if SIGN1 !=  0.  */
802 tree
tree_code_get_numeric_type(unsigned int size1,unsigned int sign1)803 tree_code_get_numeric_type (unsigned int size1, unsigned int sign1)
804 {
805   tree ret1;
806   if (size1 == tree_code_int_size)
807     {
808       if (sign1)
809         ret1 = integer_type_node;
810       else
811         ret1 = unsigned_type_node;
812     }
813   else
814     if (size1 == tree_code_char_size)
815       {
816         if (sign1)
817           ret1 = signed_char_type_node;
818         else
819           ret1 = unsigned_char_type_node;
820       }
821     else
822       abort ();
823 
824   return ret1;
825 }
826 
827 /* Garbage Collection.  */
828 
829 /* Callback to mark storage M as used always.  */
830 
831 void
tree_ggc_storage_always_used(void * m)832 tree_ggc_storage_always_used (void * m)
833 {
834   void **mm; /* Actually M is a pointer to a pointer to the memory.  */
835   mm = (void**)m;
836 
837   if (*mm)
838     ggc_mark (*mm);
839 }
840 
841 /* Following  from c-lang.c.  */
842 
843 /* Used by c-typeck.c (build_external_ref), but only for objc.  */
844 
845 tree
lookup_objc_ivar(tree id ATTRIBUTE_UNUSED)846 lookup_objc_ivar (tree id ATTRIBUTE_UNUSED)
847 {
848   return 0;
849 }
850 
851 /* Dummy routines called from c code. Save copying c-decl.c, c-common.c etc.  */
852 
853 tree
objc_is_id(tree arg ATTRIBUTE_UNUSED)854 objc_is_id (tree arg ATTRIBUTE_UNUSED)
855 {
856   return 0;
857 }
858 
859 void
check_function_format(int * status ATTRIBUTE_UNUSED,tree attrs ATTRIBUTE_UNUSED,tree params ATTRIBUTE_UNUSED)860 check_function_format (int *status ATTRIBUTE_UNUSED,
861                        tree attrs ATTRIBUTE_UNUSED,
862                        tree params ATTRIBUTE_UNUSED)
863 {
864   return;
865 }
866 
867 /* Tell the c code we are not objective C.  */
868 
869 int
objc_comptypes(tree lhs ATTRIBUTE_UNUSED,tree rhs ATTRIBUTE_UNUSED,int reflexive ATTRIBUTE_UNUSED)870 objc_comptypes (tree lhs ATTRIBUTE_UNUSED,
871                 tree rhs ATTRIBUTE_UNUSED,
872                 int reflexive ATTRIBUTE_UNUSED)
873 {
874   return 0;
875 }
876 
877 /* Should not be called for treelang.   */
878 
879 tree
build_stmt(enum tree_code code ATTRIBUTE_UNUSED,...)880 build_stmt VPARAMS ((enum tree_code code  ATTRIBUTE_UNUSED, ...))
881 {
882   abort ();
883 }
884 
885 /* Should not be called for treelang.   */
886 
887 tree
add_stmt(tree t ATTRIBUTE_UNUSED)888 add_stmt (tree t ATTRIBUTE_UNUSED)
889 {
890   abort ();
891 }
892 
893 /* Should not be called for treelang.   */
894 
895 tree
build_return_stmt(tree expr ATTRIBUTE_UNUSED)896 build_return_stmt (tree expr ATTRIBUTE_UNUSED)
897 {
898   abort ();
899 }
900 
901 /* C warning, ignore.  */
902 
903 void
pedwarn_c99(const char * msgid ATTRIBUTE_UNUSED,...)904 pedwarn_c99 VPARAMS ((const char *msgid ATTRIBUTE_UNUSED, ...))
905 {
906   return;
907 }
908 
909 /* Should not be called for treelang.   */
910 
911 tree
build_case_label(tree low_value ATTRIBUTE_UNUSED,tree high_value ATTRIBUTE_UNUSED,tree label_decl ATTRIBUTE_UNUSED)912 build_case_label (tree low_value ATTRIBUTE_UNUSED,
913                   tree high_value ATTRIBUTE_UNUSED,
914                   tree label_decl ATTRIBUTE_UNUSED)
915 {
916   abort ();
917 }
918 
919 /* Should not be called for treelang.   */
920 
921 void
emit_local_var(tree decl ATTRIBUTE_UNUSED)922 emit_local_var (tree decl ATTRIBUTE_UNUSED)
923 {
924   abort ();
925 }
926 
927 /* Should not be called for treelang.   */
928 
929 void
expand_stmt(tree t ATTRIBUTE_UNUSED)930 expand_stmt (tree t ATTRIBUTE_UNUSED)
931 {
932   abort ();
933 }
934 
935 /* Should not be called for treelang.   */
936 
937 cpp_reader *
cpp_create_reader(enum c_lang lang ATTRIBUTE_UNUSED)938 cpp_create_reader (enum c_lang lang ATTRIBUTE_UNUSED)
939 {
940   abort ();
941 }
942 
943 /* Should not be called for treelang.   */
944 
945 const char *
init_c_lex(const char * filename ATTRIBUTE_UNUSED)946 init_c_lex (const char *filename ATTRIBUTE_UNUSED)
947 {
948   abort ();
949 }
950 
951 /* Should not be called for treelang.   */
952 
953 void init_pragma (void);
954 
955 void
init_pragma()956 init_pragma ()
957 {
958   abort ();
959 }
960 
961 /* Should not be called for treelang.   */
962 
963 int
cpp_finish(cpp_reader * pfile ATTRIBUTE_UNUSED,FILE * f ATTRIBUTE_UNUSED)964 cpp_finish (cpp_reader *pfile ATTRIBUTE_UNUSED, FILE *f ATTRIBUTE_UNUSED)
965 {
966   abort ();
967 }
968 
969 /* Should not be called for treelang.   */
970 
971 unsigned int
cpp_errors(cpp_reader * pfile ATTRIBUTE_UNUSED)972 cpp_errors (cpp_reader *pfile ATTRIBUTE_UNUSED)
973 {
974   abort ();
975 }
976 
977 /* Dummy called by C.   */
978 
979 tree
handle_format_attribute(tree * node ATTRIBUTE_UNUSED,tree name ATTRIBUTE_UNUSED,tree args ATTRIBUTE_UNUSED,int flags ATTRIBUTE_UNUSED,bool * no_add_attrs ATTRIBUTE_UNUSED)980 handle_format_attribute (tree *node ATTRIBUTE_UNUSED,
981                          tree name ATTRIBUTE_UNUSED,
982                          tree args ATTRIBUTE_UNUSED,
983                          int flags ATTRIBUTE_UNUSED,
984                          bool *no_add_attrs ATTRIBUTE_UNUSED)
985 {
986   return NULL_TREE;
987 }
988 
989 /* Should not be called for treelang.   */
990 
991 tree
handle_format_arg_attribute(tree * node ATTRIBUTE_UNUSED,tree name ATTRIBUTE_UNUSED,tree args ATTRIBUTE_UNUSED,int flags ATTRIBUTE_UNUSED,bool * no_add_attrs ATTRIBUTE_UNUSED)992 handle_format_arg_attribute (tree *node ATTRIBUTE_UNUSED,
993      tree name ATTRIBUTE_UNUSED,
994      tree args ATTRIBUTE_UNUSED,
995      int flags ATTRIBUTE_UNUSED,
996      bool *no_add_attrs ATTRIBUTE_UNUSED)
997 {
998   abort ();
999 }
1000 
1001 /* Should not be called for treelang.   */
1002 
1003 int
cpp_handle_option(cpp_reader * pfile ATTRIBUTE_UNUSED,int argc ATTRIBUTE_UNUSED,char ** argv ATTRIBUTE_UNUSED)1004 cpp_handle_option (cpp_reader *pfile ATTRIBUTE_UNUSED,
1005      int argc ATTRIBUTE_UNUSED,
1006      char **argv ATTRIBUTE_UNUSED)
1007 {
1008   abort ();
1009 }
1010 
1011 /* Should not be called for treelang.   */
1012 
1013 void
cpp_assert(cpp_reader * cr ATTRIBUTE_UNUSED,const char * s ATTRIBUTE_UNUSED)1014 cpp_assert (cpp_reader * cr ATTRIBUTE_UNUSED,
1015             const char *s ATTRIBUTE_UNUSED)
1016 {
1017   abort ();
1018 }
1019 
1020 /* Should not be called for treelang.   */
1021 
1022 void
set_Wformat(int setting ATTRIBUTE_UNUSED)1023 set_Wformat (int setting ATTRIBUTE_UNUSED)
1024 {
1025   abort ();
1026 }
1027 
1028 /* Used for objective C.  */
1029 
1030 void
1031 objc_check_decl (tree decl ATTRIBUTE_UNUSED);
1032 
1033 void
objc_check_decl(tree decl ATTRIBUTE_UNUSED)1034 objc_check_decl (tree decl ATTRIBUTE_UNUSED)
1035 {
1036   abort ();
1037 }
1038 
1039 /* Tell the c code we are not objective C.  */
1040 
1041 tree
1042 objc_message_selector (void);
1043 
1044 tree
objc_message_selector()1045 objc_message_selector ()
1046 {
1047   return 0;
1048 }
1049 
1050 /* Should not be called for treelang.   */
1051 
1052 void
gen_aux_info_record(tree fndecl ATTRIBUTE_UNUSED,int is_definition ATTRIBUTE_UNUSED,int is_implicit ATTRIBUTE_UNUSED,int is_prototyped ATTRIBUTE_UNUSED)1053 gen_aux_info_record (tree fndecl ATTRIBUTE_UNUSED,
1054                      int is_definition ATTRIBUTE_UNUSED,
1055                      int is_implicit ATTRIBUTE_UNUSED,
1056                      int is_prototyped ATTRIBUTE_UNUSED)
1057 {
1058   abort ();
1059 }
1060 
1061 /* Should not be called for treelang, but it is.   */
1062 
1063 void
c_parse_init()1064 c_parse_init ()
1065 {
1066   return;
1067 }
1068 
1069 /* Should not be called for treelang.   */
1070 
1071 void maybe_apply_pragma_weak (tree decl);
1072 
1073 void
maybe_apply_pragma_weak(tree decl ATTRIBUTE_UNUSED)1074 maybe_apply_pragma_weak (tree decl ATTRIBUTE_UNUSED)
1075 {
1076   abort ();
1077 }
1078 
1079 /* Should not be called for treelang.   */
1080 
1081 void
add_decl_stmt(tree decl ATTRIBUTE_UNUSED)1082 add_decl_stmt (tree decl ATTRIBUTE_UNUSED)
1083 {
1084   abort ();
1085 }
1086 
1087 /* Should not be called for treelang.   */
1088 
1089 tree
1090 maybe_apply_renaming_pragma (tree decl, tree asmname);
1091 
1092 /* Should not be called for treelang.   */
1093 
1094 tree
maybe_apply_renaming_pragma(tree decl ATTRIBUTE_UNUSED,tree asmname ATTRIBUTE_UNUSED)1095 maybe_apply_renaming_pragma (tree decl ATTRIBUTE_UNUSED, tree asmname ATTRIBUTE_UNUSED)
1096 {
1097   abort ();
1098 }
1099 
1100 /* Should not be called for treelang.   */
1101 
1102 void
begin_stmt_tree(tree * t ATTRIBUTE_UNUSED)1103 begin_stmt_tree (tree *t ATTRIBUTE_UNUSED)
1104 {
1105   abort ();
1106 }
1107 
1108 /* Should not be called for treelang.   */
1109 
1110 void
finish_stmt_tree(tree * t ATTRIBUTE_UNUSED)1111 finish_stmt_tree (tree *t ATTRIBUTE_UNUSED)
1112 {
1113   abort ();
1114 }
1115 
1116 /* Should not be called for treelang.   */
1117 
1118 int
defer_fn(tree fn ATTRIBUTE_UNUSED)1119 defer_fn (tree fn ATTRIBUTE_UNUSED)
1120 {
1121   abort ();
1122 }
1123 
1124 /* Should not be called for treelang.   */
1125 
1126 cpp_options
cpp_get_options(cpp_reader * cr ATTRIBUTE_UNUSED)1127 *cpp_get_options (cpp_reader * cr ATTRIBUTE_UNUSED)
1128 {
1129   abort ();
1130 }
1131 
1132 /* Should not be called for treelang.   */
1133 
1134 void
cpp_define(cpp_reader * cr ATTRIBUTE_UNUSED,const char * c ATTRIBUTE_UNUSED)1135 cpp_define (cpp_reader * cr ATTRIBUTE_UNUSED, const char * c ATTRIBUTE_UNUSED)
1136 {
1137   abort ();
1138 }
1139 
1140 /* Should not be called for treelang.   */
1141 
1142 cpp_callbacks *
cpp_get_callbacks(cpp_reader * cr ATTRIBUTE_UNUSED)1143 cpp_get_callbacks (cpp_reader * cr ATTRIBUTE_UNUSED)
1144 {
1145   abort ();
1146 }
1147 
1148 /* Create the predefined scalar types of C,
1149    and some nodes representing standard constants (0, 1, (void *) 0).
1150    Initialize the global binding level.
1151    Make definitions for built-in primitive functions.  */
1152 
1153   /* `unsigned long' is the standard type for sizeof.
1154      Note that stddef.h uses `unsigned long',
1155      and this must agree, even if long and int are the same size.  */
1156 
1157 /* The reserved keyword table.  */
1158 struct resword
1159 {
1160   const char *word;
1161   ENUM_BITFIELD(rid) rid : 16;
1162   unsigned int disable   : 16;
1163 };
1164 
1165 static const struct resword reswords[] =
1166 {
1167   { "_Bool",		RID_BOOL,	0 },
1168   { "_Complex",		RID_COMPLEX,	0 },
1169   { "__FUNCTION__",	RID_FUNCTION_NAME, 0 },
1170   { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
1171   { "__alignof",	RID_ALIGNOF,	0 },
1172   { "__alignof__",	RID_ALIGNOF,	0 },
1173   { "__asm",		RID_ASM,	0 },
1174   { "__asm__",		RID_ASM,	0 },
1175   { "__attribute",	RID_ATTRIBUTE,	0 },
1176   { "__attribute__",	RID_ATTRIBUTE,	0 },
1177   { "__bounded",	RID_BOUNDED,	0 },
1178   { "__bounded__",	RID_BOUNDED,	0 },
1179   { "__builtin_choose_expr", RID_CHOOSE_EXPR, 0 },
1180   { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, 0 },
1181   { "__builtin_va_arg",	RID_VA_ARG,	0 },
1182   { "__complex",	RID_COMPLEX,	0 },
1183   { "__complex__",	RID_COMPLEX,	0 },
1184   { "__const",		RID_CONST,	0 },
1185   { "__const__",	RID_CONST,	0 },
1186   { "__extension__",	RID_EXTENSION,	0 },
1187   { "__func__",		RID_C99_FUNCTION_NAME, 0 },
1188   { "__imag",		RID_IMAGPART,	0 },
1189   { "__imag__",		RID_IMAGPART,	0 },
1190   { "__inline",		RID_INLINE,	0 },
1191   { "__inline__",	RID_INLINE,	0 },
1192   { "__label__",	RID_LABEL,	0 },
1193   { "__ptrbase",	RID_PTRBASE,	0 },
1194   { "__ptrbase__",	RID_PTRBASE,	0 },
1195   { "__ptrextent",	RID_PTREXTENT,	0 },
1196   { "__ptrextent__",	RID_PTREXTENT,	0 },
1197   { "__ptrvalue",	RID_PTRVALUE,	0 },
1198   { "__ptrvalue__",	RID_PTRVALUE,	0 },
1199   { "__real",		RID_REALPART,	0 },
1200   { "__real__",		RID_REALPART,	0 },
1201   { "__restrict",	RID_RESTRICT,	0 },
1202   { "__restrict__",	RID_RESTRICT,	0 },
1203   { "__signed",		RID_SIGNED,	0 },
1204   { "__signed__",	RID_SIGNED,	0 },
1205   { "__typeof",		RID_TYPEOF,	0 },
1206   { "__typeof__",	RID_TYPEOF,	0 },
1207   { "__unbounded",	RID_UNBOUNDED,	0 },
1208   { "__unbounded__",	RID_UNBOUNDED,	0 },
1209   { "__volatile",	RID_VOLATILE,	0 },
1210   { "__volatile__",	RID_VOLATILE,	0 },
1211   { "asm",		RID_ASM,	0 },
1212   { "auto",		RID_AUTO,	0 },
1213   { "break",		RID_BREAK,	0 },
1214   { "case",		RID_CASE,	0 },
1215   { "char",		RID_CHAR,	0 },
1216   { "const",		RID_CONST,	0 },
1217   { "continue",		RID_CONTINUE,	0 },
1218   { "default",		RID_DEFAULT,	0 },
1219   { "do",		RID_DO,		0 },
1220   { "double",		RID_DOUBLE,	0 },
1221   { "else",		RID_ELSE,	0 },
1222   { "enum",		RID_ENUM,	0 },
1223   { "extern",		RID_EXTERN,	0 },
1224   { "float",		RID_FLOAT,	0 },
1225   { "for",		RID_FOR,	0 },
1226   { "goto",		RID_GOTO,	0 },
1227   { "if",		RID_IF,		0 },
1228   { "inline",		RID_INLINE,	0 },
1229   { "int",		RID_INT,	0 },
1230   { "long",		RID_LONG,	0 },
1231   { "register",		RID_REGISTER,	0 },
1232   { "restrict",		RID_RESTRICT,	0 },
1233   { "return",		RID_RETURN,	0 },
1234   { "short",		RID_SHORT,	0 },
1235   { "signed",		RID_SIGNED,	0 },
1236   { "sizeof",		RID_SIZEOF,	0 },
1237   { "static",		RID_STATIC,	0 },
1238   { "struct",		RID_STRUCT,	0 },
1239   { "switch",		RID_SWITCH,	0 },
1240   { "typedef",		RID_TYPEDEF,	0 },
1241   { "typeof",		RID_TYPEOF,	0 },
1242   { "union",		RID_UNION,	0 },
1243   { "unsigned",		RID_UNSIGNED,	0 },
1244   { "void",		RID_VOID,	0 },
1245   { "volatile",		RID_VOLATILE,	0 },
1246   { "while",		RID_WHILE,	0 },
1247 };
1248 #define N_reswords (sizeof reswords / sizeof (struct resword))
1249 
1250 /* Init enough to allow the C decl code to work, then clean up
1251    afterwards.  */
1252 
1253 void
treelang_init_decl_processing()1254 treelang_init_decl_processing ()
1255 {
1256   unsigned int i;
1257   tree id;
1258 
1259   /* It is not necessary to register ridpointers as a GC root, because
1260      all the trees it points to are permanently interned in the
1261      get_identifier hash anyway.  */
1262   ridpointers = (tree *) xcalloc ((int) RID_MAX, sizeof (tree));
1263 
1264   for (i = 0; i < N_reswords; i++)
1265     {
1266       id = get_identifier (reswords[i].word);
1267       C_RID_CODE (id) = reswords[i].rid;
1268       C_IS_RESERVED_WORD (id) = 1;
1269       ridpointers [(int) reswords[i].rid] = id;
1270     }
1271 
1272   c_init_decl_processing ();
1273 
1274   /* ix86_return_pops_args takes the type of these so need to patch
1275      their own type as themselves.  */
1276 
1277   for (i = 0; i < itk_none; i++)
1278     {
1279       if (integer_types[i])
1280         TREE_TYPE (integer_types [i]) = integer_types[i];
1281     }
1282 
1283   /* Probably these ones too.  */
1284   TREE_TYPE (float_type_node) = float_type_node;
1285   TREE_TYPE (double_type_node) = double_type_node;
1286   TREE_TYPE (long_double_type_node) = long_double_type_node;
1287 
1288 }
1289 
1290 /* Save typing debug_tree all the time. Dump a tree T pretty and
1291    concise.  */
1292 
1293 void dt (tree t);
1294 
1295 void
dt(tree t)1296 dt (tree t)
1297 {
1298   debug_tree (t);
1299 }
1300