2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / treelang / treetree.c
blob7697e3c49e51c2acf54069252933f41440c6d7be
1 /*
3 TREELANG Compiler back end interface (treetree.c)
4 Called by the parser.
6 If you want a working example of how to write a front end to GCC,
7 you are in the right place.
9 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
10 1999, 2000, 2001, 2002, 2003, Free Software Foundation, Inc.
12 This code is based on toy.c written by Richard Kenner.
14 It was later modified by Jonathan Bartlett whose changes have all
15 been removed (by Tim Josling).
17 Various bits and pieces were cloned from the GCC main tree, as
18 GCC evolved, for COBOLForGCC, by Tim Josling.
20 It was adapted to TREELANG by Tim Josling 2001.
22 ---------------------------------------------------------------------------
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.
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.
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.
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!
43 ---------------------------------------------------------------------------
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. */
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. */
57 /* GCC headers. */
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "tm.h"
63 #include "tree.h"
64 #include "flags.h"
65 #include "output.h"
66 #include "rtl.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"
74 #include "treelang.h"
75 #include "treetree.h"
76 #include "opts.h"
78 extern int option_main;
79 extern char **file_names;
81 /* Types expected by gcc's garbage collector.
82 These types exist to allow language front-ends to
83 add extra information in gcc's parse tree data structure.
84 But the treelang front end doesn't use them -- it has
85 its own parse tree data structure.
86 We define them here only to satisfy gcc's garbage collector. */
88 /* Language-specific identifier information. */
90 struct lang_identifier GTY(())
92 struct tree_identifier common;
95 /* Language-specific tree node information. */
97 union lang_tree_node
98 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE")))
100 union tree_node GTY ((tag ("0"),
101 desc ("tree_node_structure (&%h)")))
102 generic;
103 struct lang_identifier GTY ((tag ("1"))) identifier;
106 /* Language-specific type information. */
108 struct lang_type GTY(())
110 char junk; /* dummy field to ensure struct is not empty */
113 /* Language-specific declaration information. */
115 struct lang_decl GTY(())
117 char junk; /* dummy field to ensure struct is not empty */
120 struct language_function GTY(())
122 char junk; /* dummy field to ensure struct is not empty */
125 static tree tree_lang_truthvalue_conversion (tree expr);
126 static bool tree_mark_addressable (tree exp);
127 static tree tree_lang_type_for_size (unsigned precision, int unsignedp);
128 static tree tree_lang_type_for_mode (enum machine_mode mode, int unsignedp);
129 static tree tree_lang_unsigned_type (tree type_node);
130 static tree tree_lang_signed_type (tree type_node);
131 static tree tree_lang_signed_or_unsigned_type (int unsignedp, tree type);
133 /* XXX these should be static */
134 void pushlevel (int ignore);
135 tree poplevel (int keep, int reverse, int functionbody);
136 int global_bindings_p (void);
137 void insert_block (tree block);
138 void set_block (tree block);
139 tree pushdecl (tree decl);
140 tree getdecls (void);
141 int kept_level_p (void);
143 static void tree_push_type_decl (tree id, tree type_node);
144 static void tree_push_atomic_type_decl (tree id, tree type_node);
146 /* The front end language hooks (addresses of code for this front
147 end). These are not really very language-dependent, i.e.
148 treelang, C, Mercury, etc. can all use almost the same definitions. */
150 #undef LANG_HOOKS_TRUTHVALUE_CONVERSION
151 #define LANG_HOOKS_TRUTHVALUE_CONVERSION tree_lang_truthvalue_conversion
152 #undef LANG_HOOKS_MARK_ADDRESSABLE
153 #define LANG_HOOKS_MARK_ADDRESSABLE tree_mark_addressable
154 #undef LANG_HOOKS_SIGNED_TYPE
155 #define LANG_HOOKS_SIGNED_TYPE tree_lang_signed_type
156 #undef LANG_HOOKS_UNSIGNED_TYPE
157 #define LANG_HOOKS_UNSIGNED_TYPE tree_lang_unsigned_type
158 #undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
159 #define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE tree_lang_signed_or_unsigned_type
160 #undef LANG_HOOKS_TYPE_FOR_MODE
161 #define LANG_HOOKS_TYPE_FOR_MODE tree_lang_type_for_mode
162 #undef LANG_HOOKS_TYPE_FOR_SIZE
163 #define LANG_HOOKS_TYPE_FOR_SIZE tree_lang_type_for_size
164 #undef LANG_HOOKS_PARSE_FILE
165 #define LANG_HOOKS_PARSE_FILE treelang_parse_file
167 /* Hook routines and data unique to treelang. */
169 #undef LANG_HOOKS_INIT
170 #define LANG_HOOKS_INIT treelang_init
171 #undef LANG_HOOKS_NAME
172 #define LANG_HOOKS_NAME "GNU treelang"
173 #undef LANG_HOOKS_FINISH
174 #define LANG_HOOKS_FINISH treelang_finish
175 #undef LANG_HOOKS_INIT_OPTIONS
176 #define LANG_HOOKS_INIT_OPTIONS treelang_init_options
177 #undef LANG_HOOKS_HANDLE_OPTION
178 #define LANG_HOOKS_HANDLE_OPTION treelang_handle_option
179 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
181 /* Tree code type/name/code tables. */
183 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
185 const char tree_code_type[] = {
186 #include "tree.def"
189 #undef DEFTREECODE
191 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
193 const unsigned char tree_code_length[] = {
194 #include "tree.def"
197 #undef DEFTREECODE
199 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
201 const char *const tree_code_name[] = {
202 #include "tree.def"
203 "@@dummy"
205 #undef DEFTREECODE
207 /* Number of bits in int and char - accessed by front end. */
209 unsigned int tree_code_int_size = SIZEOF_INT * HOST_BITS_PER_CHAR;
211 unsigned int tree_code_char_size = HOST_BITS_PER_CHAR;
213 /* Return the tree stuff for this type TYPE_NUM. */
215 tree
216 tree_code_get_type (int type_num)
218 switch (type_num)
220 case SIGNED_CHAR:
221 return signed_char_type_node;
223 case UNSIGNED_CHAR:
224 return unsigned_char_type_node;
226 case SIGNED_INT:
227 return integer_type_node;
229 case UNSIGNED_INT:
230 return unsigned_type_node;
232 case VOID_TYPE:
233 return void_type_node;
235 default:
236 abort ();
240 /* Output the code for the start of an if statement. The test
241 expression is EXP (true if not zero), and the stmt occurred at line
242 LINENO in file FILENAME. */
244 void
245 tree_code_if_start (tree exp, location_t loc)
247 tree cond_exp;
248 cond_exp = build (NE_EXPR,
249 TREE_TYPE (exp),
250 exp,
251 build1 (CONVERT_EXPR, TREE_TYPE (exp), integer_zero_node));
252 emit_line_note (loc); /* Output the line number information. */
253 expand_start_cond (cond_exp, /* Exit-able if nonzero. */ 0);
256 /* Output the code for the else of an if statement. The else occurred
257 at line LINENO in file FILENAME. */
259 void
260 tree_code_if_else (location_t loc)
262 emit_line_note (loc); /* Output the line number information. */
263 expand_start_else ();
266 /* Output the code for the end_if an if statement. The end_if (final brace) occurred
267 at line LINENO in file FILENAME. */
269 void
270 tree_code_if_end (location_t loc)
272 emit_line_note (loc); /* Output the line number information. */
273 expand_end_cond ();
276 /* Create a function. The prototype name is NAME, storage class is
277 STORAGE_CLASS, type of return variable is RET_TYPE, parameter lists
278 is PARMS, returns decl for this function. */
280 tree
281 tree_code_create_function_prototype (unsigned char* chars,
282 unsigned int storage_class,
283 unsigned int ret_type,
284 struct prod_token_parm_item* parms,
285 location_t loc)
288 tree id;
289 struct prod_token_parm_item* parm;
290 tree type_list = NULL_TREE;
291 tree type_node;
292 tree fn_type;
293 tree fn_decl;
295 /* Build the type. */
296 id = get_identifier ((const char*)chars);
297 for (parm = parms; parm; parm = parm->tp.par.next)
299 if (parm->category != parameter_category)
300 abort ();
301 type_node = get_type_for_numeric_type (parm->type);
302 type_list = tree_cons (NULL_TREE, type_node, type_list);
304 /* Last parm if void indicates fixed length list (as opposed to
305 printf style va_* list). */
306 type_list = tree_cons (NULL_TREE, void_type_node, type_list);
307 /* The back end needs them in reverse order. */
308 type_list = nreverse (type_list);
310 type_node = get_type_for_numeric_type (ret_type);
311 fn_type = build_function_type (type_node, type_list);
313 id = get_identifier ((const char*)chars);
314 fn_decl = build_decl (FUNCTION_DECL, id, fn_type);
316 DECL_CONTEXT (fn_decl) = NULL_TREE; /* Nested functions not supported here. */
317 DECL_SOURCE_LOCATION (fn_decl) = loc;
319 TREE_USED (fn_decl) = 1;
321 /* Real name (optional). */
322 SET_DECL_ASSEMBLER_NAME (fn_decl, DECL_NAME (fn_decl));
324 TREE_PUBLIC (fn_decl) = 0;
325 DECL_EXTERNAL (fn_decl) = 0;
326 TREE_STATIC (fn_decl) = 0;
327 switch (storage_class)
329 case STATIC_STORAGE:
330 TREE_PUBLIC (fn_decl) = 0;
331 break;
333 case EXTERNAL_DEFINITION_STORAGE:
334 TREE_PUBLIC (fn_decl) = 1;
335 TREE_STATIC (fn_decl) = 0;
336 DECL_EXTERNAL (fn_decl) = 0;
337 break;
339 case EXTERNAL_REFERENCE_STORAGE:
340 TREE_PUBLIC (fn_decl) = 0;
341 DECL_EXTERNAL (fn_decl) = 1;
342 break;
345 case AUTOMATIC_STORAGE:
346 default:
347 abort ();
350 /* Process declaration of function defined elsewhere. */
351 rest_of_decl_compilation (fn_decl, NULL, 1, 0);
353 return fn_decl;
357 /* Output code for start of function; the decl of the function is in
358 PREV_SAVED (as created by tree_code_create_function_prototype),
359 the function is at line number LINENO in file FILENAME. The
360 parameter details are in the lists PARMS. Returns nothing. */
361 void
362 tree_code_create_function_initial (tree prev_saved,
363 location_t loc,
364 struct prod_token_parm_item* parms)
366 tree fn_decl;
367 tree param_decl;
368 tree next_param;
369 tree first_param;
370 tree parm_decl;
371 tree parm_list;
372 tree resultdecl;
373 struct prod_token_parm_item* this_parm;
374 struct prod_token_parm_item* parm;
376 fn_decl = prev_saved;
377 if (!fn_decl)
378 abort ();
380 /* Output message if not -quiet. */
381 announce_function (fn_decl);
383 /* This has something to do with forcing output also. */
384 pushdecl (fn_decl);
386 /* Set current function for error msgs etc. */
387 current_function_decl = fn_decl;
388 DECL_INITIAL (fn_decl) = error_mark_node;
390 DECL_SOURCE_LOCATION (fn_decl) = loc;
392 /* Prepare creation of rtl for a new function. */
394 resultdecl = DECL_RESULT (fn_decl)
395 = build_decl (RESULT_DECL, NULL_TREE, TREE_TYPE (TREE_TYPE (fn_decl)));
396 DECL_CONTEXT (DECL_RESULT (fn_decl)) = fn_decl;
397 DECL_SOURCE_LOCATION (resultdecl) = loc;
399 /* Work out the size. ??? is this needed. */
400 layout_decl (DECL_RESULT (fn_decl), 0);
402 /* Make the argument variable decls. */
403 parm_list = NULL_TREE;
404 for (parm = parms; parm; parm = parm->tp.par.next)
406 parm_decl = build_decl (PARM_DECL, get_identifier
407 ((const char*) (parm->tp.par.variable_name)),
408 get_type_for_numeric_type (parm->type));
410 /* Some languages have different nominal and real types. */
411 DECL_ARG_TYPE (parm_decl) = TREE_TYPE (parm_decl);
412 if (!DECL_ARG_TYPE (parm_decl))
413 abort ();
414 if (!fn_decl)
415 abort ();
416 DECL_CONTEXT (parm_decl) = fn_decl;
417 DECL_SOURCE_LOCATION (parm_decl) = loc;
418 parm_list = chainon (parm_decl, parm_list);
421 /* Back into reverse order as the back end likes them. */
422 parm_list = nreverse (parm_list);
424 DECL_ARGUMENTS (fn_decl) = parm_list;
426 /* Save the decls for use when the args are referred to. */
427 for (param_decl = DECL_ARGUMENTS (fn_decl),
428 this_parm = parms;
429 param_decl;
430 param_decl = TREE_CHAIN (param_decl),
431 this_parm = this_parm->tp.par.next)
433 if (!this_parm)
434 abort (); /* Too few. */
435 *this_parm->tp.par.where_to_put_var_tree = param_decl;
437 if (this_parm)
438 abort (); /* Too many. */
440 /* Output the decl rtl (not the rtl for the function code). ???.
441 If the function is not defined in this file, when should you
442 execute this? */
443 make_decl_rtl (fn_decl, NULL);
445 init_function_start (fn_decl);
447 /* Create rtl for startup code of function, such as saving registers. */
449 expand_function_start (fn_decl, 0);
451 /* Function.c requires a push at the start of the function. that
452 looks like a bug to me but let's make it happy. */
454 (*lang_hooks.decls.pushlevel) (0);
456 /* Create rtl for the start of a new scope. */
458 expand_start_bindings (2);
460 /* Put the parameters into the symbol table. */
462 for (first_param = param_decl = nreverse (DECL_ARGUMENTS (fn_decl));
463 param_decl;
464 param_decl = next_param)
466 next_param = TREE_CHAIN (param_decl);
467 TREE_CHAIN (param_decl) = NULL;
468 /* layout_decl (param_decl, 0); Already done in build_decl tej 13/4/2002. */
469 pushdecl (param_decl);
470 if (DECL_CONTEXT (param_decl) != current_function_decl)
471 abort ();
474 /* Store back the PARM_DECL nodes. They appear in the right order. */
475 DECL_ARGUMENTS (fn_decl) = getdecls ();
477 /* Force it to be output, else may be solely inlined. */
478 TREE_ADDRESSABLE (fn_decl) = 1;
480 /* Stop -O3 from deleting it. */
481 TREE_USED (fn_decl) = 1;
483 /* Add a new level to the debugger symbol table. */
485 (*lang_hooks.decls.pushlevel) (0);
487 /* Create rtl for the start of a new scope. */
489 expand_start_bindings (0);
491 emit_line_note (loc); /* Output the line number information. */
494 /* Wrapup a function contained in file FILENAME, ending at line LINENO. */
495 void
496 tree_code_create_function_wrapup (location_t loc)
498 tree block;
499 tree fn_decl;
501 fn_decl = current_function_decl;
503 emit_line_note (loc); /* Output the line number information. */
505 /* Get completely built level from debugger symbol table. */
507 block = (*lang_hooks.decls.poplevel) (1, 0, 0);
509 /* Emit rtl for end of scope. */
511 expand_end_bindings (block, 0, 1);
513 /* Emit rtl for end of function. */
515 expand_function_end ();
517 /* Pop the level. */
519 block = (*lang_hooks.decls.poplevel) (1, 0, 1);
521 /* And attach it to the function. */
523 DECL_INITIAL (fn_decl) = block;
525 /* Emit rtl for end of scope. */
527 expand_end_bindings (block, 0, 1);
529 /* Call optimization and convert optimized rtl to assembly code. */
531 rest_of_compilation (fn_decl);
533 /* We are not inside of any scope now. */
535 current_function_decl = NULL_TREE;
539 Create a variable.
541 The storage class is STORAGE_CLASS (eg LOCAL).
542 The name is CHARS/LENGTH.
543 The type is EXPRESSION_TYPE (eg UNSIGNED_TYPE).
544 The init tree is INIT.
547 tree
548 tree_code_create_variable (unsigned int storage_class,
549 unsigned char* chars,
550 unsigned int length,
551 unsigned int expression_type,
552 tree init,
553 location_t loc)
555 tree var_type;
556 tree var_id;
557 tree var_decl;
559 /* 1. Build the type. */
560 var_type = get_type_for_numeric_type (expression_type);
562 /* 2. Build the name. */
563 if (chars[length] != 0)
564 abort (); /* Should be null terminated. */
566 var_id = get_identifier ((const char*)chars);
568 /* 3. Build the decl and set up init. */
569 var_decl = build_decl (VAR_DECL, var_id, var_type);
571 /* 3a. Initialization. */
572 if (init)
573 DECL_INITIAL (var_decl) = build1 (CONVERT_EXPR, var_type, init);
574 else
575 DECL_INITIAL (var_decl) = NULL_TREE;
577 /* 4. Compute size etc. */
578 layout_decl (var_decl, 0);
580 if (TYPE_SIZE (var_type) == 0)
581 abort (); /* Did not calculate size. */
583 DECL_CONTEXT (var_decl) = current_function_decl;
585 DECL_SOURCE_LOCATION (var_decl) = loc;
587 /* Set the storage mode and whether only visible in the same file. */
588 switch (storage_class)
590 case STATIC_STORAGE:
591 TREE_STATIC (var_decl) = 1;
592 TREE_PUBLIC (var_decl) = 0;
593 break;
595 case AUTOMATIC_STORAGE:
596 TREE_STATIC (var_decl) = 0;
597 TREE_PUBLIC (var_decl) = 0;
598 break;
600 case EXTERNAL_DEFINITION_STORAGE:
601 TREE_STATIC (var_decl) = 0;
602 TREE_PUBLIC (var_decl) = 1;
603 break;
605 case EXTERNAL_REFERENCE_STORAGE:
606 DECL_EXTERNAL (var_decl) = 1;
607 TREE_PUBLIC (var_decl) = 0;
608 break;
610 default:
611 abort ();
614 /* This should really only be set if the variable is used. */
615 TREE_USED (var_decl) = 1;
617 /* Expand declaration and initial value if any. */
619 if (TREE_STATIC (var_decl))
620 rest_of_decl_compilation (var_decl, 0, 0, 0);
621 else
623 expand_decl (var_decl);
624 if (DECL_INITIAL (var_decl))
625 expand_decl_init (var_decl);
628 return pushdecl (copy_node (var_decl));
633 /* Generate code for return statement. Type is in TYPE, expression
634 is in EXP if present. */
636 void
637 tree_code_generate_return (tree type, tree exp)
639 tree setret;
640 tree param;
642 for (param = DECL_ARGUMENTS (current_function_decl);
643 param;
644 param = TREE_CHAIN (param))
646 if (DECL_CONTEXT (param) != current_function_decl)
647 abort ();
650 if (exp)
652 setret = build (MODIFY_EXPR, type, DECL_RESULT (current_function_decl),
653 build1 (CONVERT_EXPR, type, exp));
654 TREE_SIDE_EFFECTS (setret) = 1;
655 TREE_USED (setret) = 1;
656 expand_expr_stmt (setret);
658 expand_return (DECL_RESULT (current_function_decl));
661 /* Output the code for this expression statement CODE. */
664 void
665 tree_code_output_expression_statement (tree code, location_t loc)
667 /* Output the line number information. */
668 emit_line_note (loc);
669 TREE_USED (code) = 1;
670 TREE_SIDE_EFFECTS (code) = 1;
671 expand_expr_stmt (code);
674 /* Return a tree for a constant integer value in the token TOK. No
675 size checking is done. */
677 tree
678 tree_code_get_integer_value (unsigned char* chars, unsigned int length)
680 long long int val = 0;
681 unsigned int ix;
682 unsigned int start = 0;
683 int negative = 1;
684 switch (chars[0])
686 case (unsigned char)'-':
687 negative = -1;
688 start = 1;
689 break;
691 case (unsigned char)'+':
692 start = 1;
693 break;
695 default:
696 break;
698 for (ix = start; ix < length; ix++)
699 val = val * 10 + chars[ix] - (unsigned char)'0';
700 val = val*negative;
701 return build_int_2 (val & 0xffffffff, (val >> 32) & 0xffffffff);
704 /* Return the tree for an expresssion, type EXP_TYPE (see treetree.h)
705 with tree type TYPE and with operands1 OP1, OP2 (maybe), OP3 (maybe). */
706 tree
707 tree_code_get_expression (unsigned int exp_type,
708 tree type, tree op1, tree op2,
709 tree op3 ATTRIBUTE_UNUSED)
711 tree ret1;
712 int operator;
714 switch (exp_type)
716 case EXP_ASSIGN:
717 if (!op1 || !op2)
718 abort ();
719 operator = MODIFY_EXPR;
720 ret1 = build (operator, type,
721 op1,
722 build1 (CONVERT_EXPR, type, op2));
724 break;
726 case EXP_PLUS:
727 operator = PLUS_EXPR;
728 goto binary_expression;
730 case EXP_MINUS:
731 operator = MINUS_EXPR;
732 goto binary_expression;
734 case EXP_EQUALS:
735 operator = EQ_EXPR;
736 goto binary_expression;
738 /* Expand a binary expression. Ensure the operands are the right type. */
739 binary_expression:
740 if (!op1 || !op2)
741 abort ();
742 ret1 = build (operator, type,
743 build1 (CONVERT_EXPR, type, op1),
744 build1 (CONVERT_EXPR, type, op2));
745 break;
747 /* Reference to a variable. This is dead easy, just return the
748 decl for the variable. If the TYPE is different than the
749 variable type, convert it. */
750 case EXP_REFERENCE:
751 if (!op1)
752 abort ();
753 if (type == TREE_TYPE (op1))
754 ret1 = op1;
755 else
756 ret1 = build1 (CONVERT_EXPR, type, op1);
757 break;
759 case EXP_FUNCTION_INVOCATION:
760 if (!op1 || !op2)
761 abort ();
763 tree fun_ptr;
764 fun_ptr = build1 (ADDR_EXPR, build_pointer_type (type), op1);
765 ret1 = build (CALL_EXPR, type, fun_ptr, nreverse (op2));
767 break;
769 default:
770 abort ();
773 return ret1;
776 /* Init parameter list and return empty list. */
778 tree
779 tree_code_init_parameters (void)
781 return NULL_TREE;
784 /* Add a parameter EXP whose expression type is EXP_PROTO to list
785 LIST, returning the new list. */
787 tree
788 tree_code_add_parameter (tree list, tree proto_exp, tree exp)
790 tree new_exp;
791 new_exp = tree_cons (NULL_TREE,
792 build1 (CONVERT_EXPR, TREE_TYPE (proto_exp), exp),
793 NULL_TREE);
794 if (!list)
795 return new_exp;
796 return chainon (new_exp, list);
799 /* Get the tree type for this type whose number is NUMERIC_TYPE. */
801 tree
802 get_type_for_numeric_type (unsigned int numeric_type)
805 int size1;
806 int sign1;
807 switch (numeric_type)
809 case VOID_TYPE:
810 return void_type_node;
812 case SIGNED_INT:
813 size1 = tree_code_int_size;
814 sign1 = 1;
815 break;
817 case UNSIGNED_INT:
818 size1 = tree_code_int_size;
819 sign1 = 0;
820 break;
822 case SIGNED_CHAR:
823 size1 = tree_code_char_size;
824 sign1 = 1;
825 break;
827 case UNSIGNED_CHAR:
828 size1 = tree_code_char_size;
829 sign1 = 0;
830 break;
832 default:
833 abort ();
836 return tree_code_get_numeric_type (size1, sign1);
840 /* Return tree representing a numeric type of size SIZE1 bits and
841 signed if SIGN1 != 0. */
842 tree
843 tree_code_get_numeric_type (unsigned int size1, unsigned int sign1)
845 tree ret1;
846 if (!size1)
847 abort ();
848 if (size1 == tree_code_int_size)
850 if (sign1)
851 ret1 = integer_type_node;
852 else
853 ret1 = unsigned_type_node;
855 else
856 if (size1 == tree_code_char_size)
858 if (sign1)
859 ret1 = signed_char_type_node;
860 else
861 ret1 = unsigned_char_type_node;
863 else
864 abort ();
866 return ret1;
869 /* Get a stringpool entry for a string S of length L. This is needed
870 because the GTY routines don't mark strings, forcing you to put
871 them into stringpool, which is never freed. */
873 const char*
874 get_string (const char *s, size_t l)
876 tree t;
877 t = get_identifier_with_length (s, l);
878 return IDENTIFIER_POINTER(t);
881 /* Save typing debug_tree all the time. Dump a tree T pretty and
882 concise. */
884 void dt (tree t);
886 void
887 dt (tree t)
889 debug_tree (t);
892 /* Routines Expected by gcc: */
894 /* These are used to build types for various sizes. The code below
895 is a simplified version of that of GNAT. */
897 #ifndef MAX_BITS_PER_WORD
898 #define MAX_BITS_PER_WORD BITS_PER_WORD
899 #endif
901 /* This variable keeps a table for types for each precision so that we only
902 allocate each of them once. Signed and unsigned types are kept separate. */
903 static GTY(()) tree signed_and_unsigned_types[MAX_BITS_PER_WORD + 1][2];
905 /* XXX is this definition OK? */
906 static tree
907 tree_lang_truthvalue_conversion (tree expr)
909 return expr;
912 /* Mark EXP saying that we need to be able to take the
913 address of it; it should not be allocated in a register.
914 Value is 1 if successful.
916 This implementation was copied from c-decl.c. */
918 static bool
919 tree_mark_addressable (tree exp)
921 register tree x = exp;
922 while (1)
923 switch (TREE_CODE (x))
925 case COMPONENT_REF:
926 case ADDR_EXPR:
927 case ARRAY_REF:
928 case REALPART_EXPR:
929 case IMAGPART_EXPR:
930 x = TREE_OPERAND (x, 0);
931 break;
933 case CONSTRUCTOR:
934 TREE_ADDRESSABLE (x) = 1;
935 return 1;
937 case VAR_DECL:
938 case CONST_DECL:
939 case PARM_DECL:
940 case RESULT_DECL:
941 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
942 && DECL_NONLOCAL (x))
944 if (TREE_PUBLIC (x))
946 error ("global register variable `%s' used in nested function",
947 IDENTIFIER_POINTER (DECL_NAME (x)));
948 return 0;
950 pedwarn ("register variable `%s' used in nested function",
951 IDENTIFIER_POINTER (DECL_NAME (x)));
953 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
955 if (TREE_PUBLIC (x))
957 error ("address of global register variable `%s' requested",
958 IDENTIFIER_POINTER (DECL_NAME (x)));
959 return 0;
962 pedwarn ("address of register variable `%s' requested",
963 IDENTIFIER_POINTER (DECL_NAME (x)));
965 put_var_into_stack (x, /*rescan=*/ true);
967 /* drops in */
968 case FUNCTION_DECL:
969 TREE_ADDRESSABLE (x) = 1;
971 default:
972 return 1;
976 /* Return an integer type with the number of bits of precision given by
977 PRECISION. UNSIGNEDP is nonzero if the type is unsigned; otherwise
978 it is a signed type. */
980 static tree
981 tree_lang_type_for_size (unsigned precision, int unsignedp)
983 tree t;
985 if (precision <= MAX_BITS_PER_WORD
986 && signed_and_unsigned_types[precision][unsignedp] != 0)
987 return signed_and_unsigned_types[precision][unsignedp];
989 if (unsignedp)
990 t = signed_and_unsigned_types[precision][1]
991 = make_unsigned_type (precision);
992 else
993 t = signed_and_unsigned_types[precision][0]
994 = make_signed_type (precision);
996 return t;
999 /* Return a data type that has machine mode MODE. UNSIGNEDP selects
1000 an unsigned type; otherwise a signed type is returned. */
1002 static tree
1003 tree_lang_type_for_mode (enum machine_mode mode, int unsignedp)
1005 return tree_lang_type_for_size (GET_MODE_BITSIZE (mode), unsignedp);
1008 /* Return the unsigned version of a TYPE_NODE, a scalar type. */
1010 static tree
1011 tree_lang_unsigned_type (tree type_node)
1013 return tree_lang_type_for_size (TYPE_PRECISION (type_node), 1);
1016 /* Return the signed version of a TYPE_NODE, a scalar type. */
1018 static tree
1019 tree_lang_signed_type (tree type_node)
1021 return tree_lang_type_for_size (TYPE_PRECISION (type_node), 0);
1024 /* Return a type the same as TYPE except unsigned or signed according to
1025 UNSIGNEDP. */
1027 static tree
1028 tree_lang_signed_or_unsigned_type (int unsignedp, tree type)
1030 if (! INTEGRAL_TYPE_P (type) || TREE_UNSIGNED (type) == unsignedp)
1031 return type;
1032 else
1033 return tree_lang_type_for_size (TYPE_PRECISION (type), unsignedp);
1036 /* These functions and variables deal with binding contours. We only
1037 need these functions for the list of PARM_DECLs, but we leave the
1038 functions more general; these are a simplified version of the
1039 functions from GNAT. */
1041 /* For each binding contour we allocate a binding_level structure which records
1042 the entities defined or declared in that contour. Contours include:
1044 the global one
1045 one for each subprogram definition
1046 one for each compound statement (declare block)
1048 Binding contours are used to create GCC tree BLOCK nodes. */
1050 struct binding_level
1052 /* A chain of ..._DECL nodes for all variables, constants, functions,
1053 parameters and type declarations. These ..._DECL nodes are chained
1054 through the TREE_CHAIN field. Note that these ..._DECL nodes are stored
1055 in the reverse of the order supplied to be compatible with the
1056 back-end. */
1057 tree names;
1058 /* For each level (except the global one), a chain of BLOCK nodes for all
1059 the levels that were entered and exited one level down from this one. */
1060 tree blocks;
1061 /* The back end may need, for its own internal processing, to create a BLOCK
1062 node. This field is set aside for this purpose. If this field is non-null
1063 when the level is popped, i.e. when poplevel is invoked, we will use such
1064 block instead of creating a new one from the 'names' field, that is the
1065 ..._DECL nodes accumulated so far. Typically the routine 'pushlevel'
1066 will be called before setting this field, so that if the front-end had
1067 inserted ..._DECL nodes in the current block they will not be lost. */
1068 tree block_created_by_back_end;
1069 /* The binding level containing this one (the enclosing binding level). */
1070 struct binding_level *level_chain;
1073 /* The binding level currently in effect. */
1074 static struct binding_level *current_binding_level = NULL;
1076 /* The outermost binding level. This binding level is created when the
1077 compiler is started and it will exist through the entire compilation. */
1078 static struct binding_level *global_binding_level;
1080 /* Binding level structures are initialized by copying this one. */
1081 static struct binding_level clear_binding_level = {NULL, NULL, NULL, NULL};
1083 /* Return non-zero if we are currently in the global binding level. */
1086 global_bindings_p (void)
1088 return current_binding_level == global_binding_level ? -1 : 0;
1091 /* Return the list of declarations in the current level. Note that this list
1092 is in reverse order (it has to be so for back-end compatibility). */
1094 tree
1095 getdecls (void)
1097 return current_binding_level->names;
1100 /* Nonzero if the current level needs to have a BLOCK made. */
1103 kept_level_p (void)
1105 return (current_binding_level->names != 0);
1108 /* Enter a new binding level. The input parameter is ignored, but has to be
1109 specified for back-end compatibility. */
1111 void
1112 pushlevel (int ignore ATTRIBUTE_UNUSED)
1114 struct binding_level *newlevel = xmalloc (sizeof (struct binding_level));
1116 *newlevel = clear_binding_level;
1118 /* Add this level to the front of the chain (stack) of levels that are
1119 active. */
1120 newlevel->level_chain = current_binding_level;
1121 current_binding_level = newlevel;
1124 /* Exit a binding level.
1125 Pop the level off, and restore the state of the identifier-decl mappings
1126 that were in effect when this level was entered.
1128 If KEEP is nonzero, this level had explicit declarations, so
1129 and create a "block" (a BLOCK node) for the level
1130 to record its declarations and subblocks for symbol table output.
1132 If FUNCTIONBODY is nonzero, this level is the body of a function,
1133 so create a block as if KEEP were set and also clear out all
1134 label names.
1136 If REVERSE is nonzero, reverse the order of decls before putting
1137 them into the BLOCK. */
1139 tree
1140 poplevel (int keep, int reverse, int functionbody)
1142 /* Points to a BLOCK tree node. This is the BLOCK node construted for the
1143 binding level that we are about to exit and which is returned by this
1144 routine. */
1145 tree block_node = NULL_TREE;
1146 tree decl_chain;
1147 tree subblock_chain = current_binding_level->blocks;
1148 tree subblock_node;
1149 tree block_created_by_back_end;
1151 /* Reverse the list of *_DECL nodes if desired. Note that the ..._DECL
1152 nodes chained through the `names' field of current_binding_level are in
1153 reverse order except for PARM_DECL node, which are explicitely stored in
1154 the right order. */
1155 decl_chain = (reverse) ? nreverse (current_binding_level->names)
1156 : current_binding_level->names;
1158 block_created_by_back_end = current_binding_level->block_created_by_back_end;
1159 if (block_created_by_back_end != 0)
1161 block_node = block_created_by_back_end;
1163 /* Check if we are about to discard some information that was gathered
1164 by the front-end. Nameley check if the back-end created a new block
1165 without calling pushlevel first. To understand why things are lost
1166 just look at the next case (i.e. no block created by back-end. */
1167 if ((keep || functionbody) && (decl_chain || subblock_chain))
1168 abort ();
1171 /* If there were any declarations in the current binding level, or if this
1172 binding level is a function body, or if there are any nested blocks then
1173 create a BLOCK node to record them for the life of this function. */
1174 else if (keep || functionbody)
1175 block_node = build_block (keep ? decl_chain : 0, 0, subblock_chain, 0, 0);
1177 /* Record the BLOCK node just built as the subblock its enclosing scope. */
1178 for (subblock_node = subblock_chain; subblock_node;
1179 subblock_node = TREE_CHAIN (subblock_node))
1180 BLOCK_SUPERCONTEXT (subblock_node) = block_node;
1182 /* Clear out the meanings of the local variables of this level. */
1184 for (subblock_node = decl_chain; subblock_node;
1185 subblock_node = TREE_CHAIN (subblock_node))
1186 if (DECL_NAME (subblock_node) != 0)
1187 /* If the identifier was used or addressed via a local extern decl,
1188 don't forget that fact. */
1189 if (DECL_EXTERNAL (subblock_node))
1191 if (TREE_USED (subblock_node))
1192 TREE_USED (DECL_NAME (subblock_node)) = 1;
1193 if (TREE_ADDRESSABLE (subblock_node))
1194 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (subblock_node)) = 1;
1197 /* Pop the current level. */
1198 current_binding_level = current_binding_level->level_chain;
1200 if (functionbody)
1202 /* This is the top level block of a function. The ..._DECL chain stored
1203 in BLOCK_VARS are the function's parameters (PARM_DECL nodes). Don't
1204 leave them in the BLOCK because they are found in the FUNCTION_DECL
1205 instead. */
1206 DECL_INITIAL (current_function_decl) = block_node;
1207 BLOCK_VARS (block_node) = 0;
1209 else if (block_node)
1211 if (block_created_by_back_end == NULL)
1212 current_binding_level->blocks
1213 = chainon (current_binding_level->blocks, block_node);
1216 /* If we did not make a block for the level just exited, any blocks made for
1217 inner levels (since they cannot be recorded as subblocks in that level)
1218 must be carried forward so they will later become subblocks of something
1219 else. */
1220 else if (subblock_chain)
1221 current_binding_level->blocks
1222 = chainon (current_binding_level->blocks, subblock_chain);
1223 if (block_node)
1224 TREE_USED (block_node) = 1;
1226 return block_node;
1229 /* Insert BLOCK at the end of the list of subblocks of the
1230 current binding level. This is used when a BIND_EXPR is expanded,
1231 to handle the BLOCK node inside the BIND_EXPR. */
1233 void
1234 insert_block (tree block)
1236 TREE_USED (block) = 1;
1237 current_binding_level->blocks
1238 = chainon (current_binding_level->blocks, block);
1241 /* Set the BLOCK node for the innermost scope
1242 (the one we are currently in). */
1244 void
1245 set_block (tree block)
1247 current_binding_level->block_created_by_back_end = block;
1250 /* Records a ..._DECL node DECL as belonging to the current lexical scope.
1251 Returns the ..._DECL node. */
1253 tree
1254 pushdecl (tree decl)
1256 /* External objects aren't nested, other objects may be. */
1258 if ((DECL_EXTERNAL (decl)) || (decl==current_function_decl))
1259 DECL_CONTEXT (decl) = 0;
1260 else
1261 DECL_CONTEXT (decl) = current_function_decl;
1263 /* Put the declaration on the list. The list of declarations is in reverse
1264 order. The list will be reversed later if necessary. This needs to be
1265 this way for compatibility with the back-end. */
1267 TREE_CHAIN (decl) = current_binding_level->names;
1268 current_binding_level->names = decl;
1270 /* For the declartion of a type, set its name if it is not already set. */
1272 if (TREE_CODE (decl) == TYPE_DECL
1273 && TYPE_NAME (TREE_TYPE (decl)) == 0)
1274 TYPE_NAME (TREE_TYPE (decl)) = DECL_NAME (decl);
1276 return decl;
1280 static void
1281 tree_push_type_decl(tree id, tree type_node)
1283 tree decl = build_decl (TYPE_DECL, id, type_node);
1284 TYPE_NAME (type_node) = decl;
1285 TYPE_STUB_DECL (type_node) = decl;
1286 pushdecl (decl);
1289 /* push_atomic_type_decl() ensures that the type's type is itself.
1290 Needed for DBX. Must only be used for atomic types,
1291 not for e.g. pointer or array types. */
1293 static void
1294 tree_push_atomic_type_decl(tree id, tree type_node)
1296 TREE_TYPE (type_node) = type_node;
1297 tree_push_type_decl (id, type_node);
1300 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
1302 /* Create the predefined scalar types of C,
1303 and some nodes representing standard constants (0, 1, (void *) 0).
1304 Initialize the global binding level.
1305 Make definitions for built-in primitive functions. */
1307 void
1308 treelang_init_decl_processing (void)
1310 current_function_decl = NULL;
1311 current_binding_level = NULL_BINDING_LEVEL;
1312 pushlevel (0); /* make the binding_level structure for global names */
1313 global_binding_level = current_binding_level;
1315 build_common_tree_nodes (flag_signed_char);
1317 /* set standard type names */
1319 /* Define `int' and `char' first so that dbx will output them first. */
1321 tree_push_atomic_type_decl (get_identifier ("int"), integer_type_node);
1322 tree_push_atomic_type_decl (get_identifier ("char"), char_type_node);
1323 tree_push_atomic_type_decl (get_identifier ("long int"),
1324 long_integer_type_node);
1325 tree_push_atomic_type_decl (get_identifier ("unsigned int"),
1326 unsigned_type_node);
1327 tree_push_atomic_type_decl (get_identifier ("long unsigned int"),
1328 long_unsigned_type_node);
1329 tree_push_atomic_type_decl (get_identifier ("long long int"),
1330 long_long_integer_type_node);
1331 tree_push_atomic_type_decl (get_identifier ("long long unsigned int"),
1332 long_long_unsigned_type_node);
1333 tree_push_atomic_type_decl (get_identifier ("short int"),
1334 short_integer_type_node);
1335 tree_push_atomic_type_decl (get_identifier ("short unsigned int"),
1336 short_unsigned_type_node);
1337 tree_push_atomic_type_decl (get_identifier ("signed char"),
1338 signed_char_type_node);
1339 tree_push_atomic_type_decl (get_identifier ("unsigned char"),
1340 unsigned_char_type_node);
1341 tree_push_atomic_type_decl (NULL_TREE, intQI_type_node);
1342 tree_push_atomic_type_decl (NULL_TREE, intHI_type_node);
1343 tree_push_atomic_type_decl (NULL_TREE, intSI_type_node);
1344 tree_push_atomic_type_decl (NULL_TREE, intDI_type_node);
1345 #if HOST_BITS_PER_WIDE_INT >= 64
1346 tree_push_atomic_type_decl (NULL_TREE, intTI_type_node);
1347 #endif
1348 tree_push_atomic_type_decl (NULL_TREE, unsigned_intQI_type_node);
1349 tree_push_atomic_type_decl (NULL_TREE, unsigned_intHI_type_node);
1350 tree_push_atomic_type_decl (NULL_TREE, unsigned_intSI_type_node);
1351 tree_push_atomic_type_decl (NULL_TREE, unsigned_intDI_type_node);
1352 #if HOST_BITS_PER_WIDE_INT >= 64
1353 tree_push_atomic_type_decl (NULL_TREE, unsigned_intTI_type_node);
1354 #endif
1356 size_type_node = make_unsigned_type (POINTER_SIZE);
1357 tree_push_atomic_type_decl (get_identifier ("size_t"), size_type_node);
1358 set_sizetype (size_type_node);
1360 build_common_tree_nodes_2 (/* short_double= */ 0);
1362 tree_push_atomic_type_decl (get_identifier ("float"), float_type_node);
1363 tree_push_atomic_type_decl (get_identifier ("double"), double_type_node);
1364 tree_push_atomic_type_decl (get_identifier ("long double"), long_double_type_node);
1365 tree_push_atomic_type_decl (get_identifier ("void"), void_type_node);
1367 /* Add any target-specific builtin functions. */
1368 (*targetm.init_builtins) ();
1370 pedantic_lvalues = pedantic;
1373 /* Return a definition for a builtin function named NAME and whose data type
1374 is TYPE. TYPE should be a function type with argument types.
1375 FUNCTION_CODE tells later passes how to compile calls to this function.
1376 See tree.h for its possible values.
1378 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
1379 the name to be called if we can't opencode the function. If
1380 ATTRS is nonzero, use that for the function's attribute list.
1382 copied from gcc/c-decl.c
1385 tree
1386 builtin_function (const char *name, tree type, int function_code,
1387 enum built_in_class class, const char *library_name,
1388 tree attrs)
1390 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
1391 DECL_EXTERNAL (decl) = 1;
1392 TREE_PUBLIC (decl) = 1;
1393 if (library_name)
1394 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
1395 make_decl_rtl (decl, NULL);
1396 pushdecl (decl);
1397 DECL_BUILT_IN_CLASS (decl) = class;
1398 DECL_FUNCTION_CODE (decl) = function_code;
1400 /* Possibly apply some default attributes to this built-in function. */
1401 if (attrs)
1402 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
1403 else
1404 decl_attributes (&decl, NULL_TREE, 0);
1406 return decl;
1409 #include "debug.h" /* for debug_hooks, needed by gt-treelang-treetree.h */
1410 #include "gt-treelang-treetree.h"