3 TREELANG Compiler back end interface (treetree.c)
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
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
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. */
61 #include "coretypes.h"
70 #include "langhooks-def.h"
71 #include "langhooks.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. */
98 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE")))
100 union tree_node GTY ((tag ("0"),
101 desc ("tree_node_structure (&%h)")))
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 PARAMS((tree expr));
126 static bool tree_mark_addressable PARAMS((tree exp));
127 static tree tree_lang_type_for_size PARAMS((unsigned precision,
129 static tree tree_lang_type_for_mode PARAMS((enum machine_mode mode,
131 static tree tree_lang_unsigned_type PARAMS((tree type_node));
132 static tree tree_lang_signed_type PARAMS((tree type_node));
133 static tree tree_lang_signed_or_unsigned_type PARAMS((int unsignedp,
136 /* XXX these should be static */
137 void pushlevel PARAMS((int ignore));
138 tree poplevel PARAMS((int keep, int reverse, int functionbody));
139 int global_bindings_p PARAMS((void));
140 void insert_block PARAMS((tree block));
141 void set_block PARAMS((tree block));
142 tree pushdecl PARAMS((tree decl));
143 tree getdecls PARAMS((void));
144 int kept_level_p PARAMS((void));
146 static void tree_push_type_decl PARAMS((tree id, tree type_node));
147 static void tree_push_atomic_type_decl PARAMS((tree id, tree type_node));
149 /* The front end language hooks (addresses of code for this front
150 end). These are not really very language-dependent, i.e.
151 treelang, C, Mercury, etc. can all use almost the same definitions. */
153 #undef LANG_HOOKS_TRUTHVALUE_CONVERSION
154 #define LANG_HOOKS_TRUTHVALUE_CONVERSION tree_lang_truthvalue_conversion
155 #undef LANG_HOOKS_MARK_ADDRESSABLE
156 #define LANG_HOOKS_MARK_ADDRESSABLE tree_mark_addressable
157 #undef LANG_HOOKS_SIGNED_TYPE
158 #define LANG_HOOKS_SIGNED_TYPE tree_lang_signed_type
159 #undef LANG_HOOKS_UNSIGNED_TYPE
160 #define LANG_HOOKS_UNSIGNED_TYPE tree_lang_unsigned_type
161 #undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
162 #define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE tree_lang_signed_or_unsigned_type
163 #undef LANG_HOOKS_TYPE_FOR_MODE
164 #define LANG_HOOKS_TYPE_FOR_MODE tree_lang_type_for_mode
165 #undef LANG_HOOKS_TYPE_FOR_SIZE
166 #define LANG_HOOKS_TYPE_FOR_SIZE tree_lang_type_for_size
167 #undef LANG_HOOKS_PARSE_FILE
168 #define LANG_HOOKS_PARSE_FILE treelang_parse_file
170 /* Hook routines and data unique to treelang. */
172 #undef LANG_HOOKS_INIT
173 #define LANG_HOOKS_INIT treelang_init
174 #undef LANG_HOOKS_NAME
175 #define LANG_HOOKS_NAME "GNU treelang"
176 #undef LANG_HOOKS_FINISH
177 #define LANG_HOOKS_FINISH treelang_finish
178 #undef LANG_HOOKS_INIT_OPTIONS
179 #define LANG_HOOKS_INIT_OPTIONS treelang_init_options
180 #undef LANG_HOOKS_HANDLE_OPTION
181 #define LANG_HOOKS_HANDLE_OPTION treelang_handle_option
182 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
184 /* Tree code type/name/code tables. */
186 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
188 const char tree_code_type[] = {
194 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
196 const unsigned char tree_code_length[] = {
202 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
204 const char *const tree_code_name[] = {
210 /* Number of bits in int and char - accessed by front end. */
212 unsigned int tree_code_int_size = SIZEOF_INT * HOST_BITS_PER_CHAR;
214 unsigned int tree_code_char_size = HOST_BITS_PER_CHAR;
216 /* Return the tree stuff for this type TYPE_NUM. */
219 tree_code_get_type (int type_num)
224 return signed_char_type_node;
227 return unsigned_char_type_node;
230 return integer_type_node;
233 return unsigned_type_node;
236 return void_type_node;
243 /* Output the code for the start of an if statement. The test
244 expression is EXP (true if not zero), and the stmt occurred at line
245 LINENO in file FILENAME. */
248 tree_code_if_start (tree exp, location_t loc)
251 cond_exp = build (NE_EXPR,
254 build1 (CONVERT_EXPR, TREE_TYPE (exp), integer_zero_node));
255 emit_line_note (loc); /* Output the line number information. */
256 expand_start_cond (cond_exp, /* Exit-able if nonzero. */ 0);
259 /* Output the code for the else of an if statement. The else occurred
260 at line LINENO in file FILENAME. */
263 tree_code_if_else (location_t loc)
265 emit_line_note (loc); /* Output the line number information. */
266 expand_start_else ();
269 /* Output the code for the end_if an if statement. The end_if (final brace) occurred
270 at line LINENO in file FILENAME. */
273 tree_code_if_end (location_t loc)
275 emit_line_note (loc); /* Output the line number information. */
279 /* Create a function. The prototype name is NAME, storage class is
280 STORAGE_CLASS, type of return variable is RET_TYPE, parameter lists
281 is PARMS, returns decl for this function. */
284 tree_code_create_function_prototype (unsigned char* chars,
285 unsigned int storage_class,
286 unsigned int ret_type,
287 struct prod_token_parm_item* parms,
292 struct prod_token_parm_item* parm;
293 tree type_list = NULL_TREE;
298 /* Build the type. */
299 id = get_identifier ((const char*)chars);
300 for (parm = parms; parm; parm = parm->tp.par.next)
302 if (parm->category != parameter_category)
304 type_node = get_type_for_numeric_type (parm->type);
305 type_list = tree_cons (NULL_TREE, type_node, type_list);
307 /* Last parm if void indicates fixed length list (as opposed to
308 printf style va_* list). */
309 type_list = tree_cons (NULL_TREE, void_type_node, type_list);
310 /* The back end needs them in reverse order. */
311 type_list = nreverse (type_list);
313 type_node = get_type_for_numeric_type (ret_type);
314 fn_type = build_function_type (type_node, type_list);
316 id = get_identifier ((const char*)chars);
317 fn_decl = build_decl (FUNCTION_DECL, id, fn_type);
319 DECL_CONTEXT (fn_decl) = NULL_TREE; /* Nested functions not supported here. */
320 DECL_SOURCE_LOCATION (fn_decl) = loc;
322 TREE_USED (fn_decl) = 1;
324 /* Real name (optional). */
325 SET_DECL_ASSEMBLER_NAME (fn_decl, DECL_NAME (fn_decl));
327 TREE_PUBLIC (fn_decl) = 0;
328 DECL_EXTERNAL (fn_decl) = 0;
329 TREE_STATIC (fn_decl) = 0;
330 switch (storage_class)
333 TREE_PUBLIC (fn_decl) = 0;
336 case EXTERNAL_DEFINITION_STORAGE:
337 TREE_PUBLIC (fn_decl) = 1;
338 TREE_STATIC (fn_decl) = 0;
339 DECL_EXTERNAL (fn_decl) = 0;
342 case EXTERNAL_REFERENCE_STORAGE:
343 TREE_PUBLIC (fn_decl) = 0;
344 DECL_EXTERNAL (fn_decl) = 1;
348 case AUTOMATIC_STORAGE:
353 /* Process declaration of function defined elsewhere. */
354 rest_of_decl_compilation (fn_decl, NULL, 1, 0);
360 /* Output code for start of function; the decl of the function is in
361 PREV_SAVED (as created by tree_code_create_function_prototype),
362 the function is at line number LINENO in file FILENAME. The
363 parameter details are in the lists PARMS. Returns nothing. */
365 tree_code_create_function_initial (tree prev_saved,
367 struct prod_token_parm_item* parms)
376 struct prod_token_parm_item* this_parm;
377 struct prod_token_parm_item* parm;
379 fn_decl = prev_saved;
383 /* Output message if not -quiet. */
384 announce_function (fn_decl);
386 /* This has something to do with forcing output also. */
389 /* Set current function for error msgs etc. */
390 current_function_decl = fn_decl;
391 DECL_INITIAL (fn_decl) = error_mark_node;
393 DECL_SOURCE_LOCATION (fn_decl) = loc;
395 /* Prepare creation of rtl for a new function. */
397 resultdecl = DECL_RESULT (fn_decl)
398 = build_decl (RESULT_DECL, NULL_TREE, TREE_TYPE (TREE_TYPE (fn_decl)));
399 DECL_CONTEXT (DECL_RESULT (fn_decl)) = fn_decl;
400 DECL_SOURCE_LOCATION (resultdecl) = loc;
402 /* Work out the size. ??? is this needed. */
403 layout_decl (DECL_RESULT (fn_decl), 0);
405 /* Make the argument variable decls. */
406 parm_list = NULL_TREE;
407 for (parm = parms; parm; parm = parm->tp.par.next)
409 parm_decl = build_decl (PARM_DECL, get_identifier
410 ((const char*) (parm->tp.par.variable_name)),
411 get_type_for_numeric_type (parm->type));
413 /* Some languages have different nominal and real types. */
414 DECL_ARG_TYPE (parm_decl) = TREE_TYPE (parm_decl);
415 if (!DECL_ARG_TYPE (parm_decl))
419 DECL_CONTEXT (parm_decl) = fn_decl;
420 DECL_SOURCE_LOCATION (parm_decl) = loc;
421 parm_list = chainon (parm_decl, parm_list);
424 /* Back into reverse order as the back end likes them. */
425 parm_list = nreverse (parm_list);
427 DECL_ARGUMENTS (fn_decl) = parm_list;
429 /* Save the decls for use when the args are referred to. */
430 for (param_decl = DECL_ARGUMENTS (fn_decl),
433 param_decl = TREE_CHAIN (param_decl),
434 this_parm = this_parm->tp.par.next)
437 abort (); /* Too few. */
438 *this_parm->tp.par.where_to_put_var_tree = param_decl;
441 abort (); /* Too many. */
443 /* Output the decl rtl (not the rtl for the function code). ???.
444 If the function is not defined in this file, when should you
446 make_decl_rtl (fn_decl, NULL);
448 init_function_start (fn_decl);
450 /* Create rtl for startup code of function, such as saving registers. */
452 expand_function_start (fn_decl, 0);
454 /* Function.c requires a push at the start of the function. that
455 looks like a bug to me but let's make it happy. */
457 (*lang_hooks.decls.pushlevel) (0);
459 /* Create rtl for the start of a new scope. */
461 expand_start_bindings (2);
463 /* Put the parameters into the symbol table. */
465 for (first_param = param_decl = nreverse (DECL_ARGUMENTS (fn_decl));
467 param_decl = next_param)
469 next_param = TREE_CHAIN (param_decl);
470 TREE_CHAIN (param_decl) = NULL;
471 /* layout_decl (param_decl, 0); Already done in build_decl tej 13/4/2002. */
472 pushdecl (param_decl);
473 if (DECL_CONTEXT (param_decl) != current_function_decl)
477 /* Store back the PARM_DECL nodes. They appear in the right order. */
478 DECL_ARGUMENTS (fn_decl) = getdecls ();
480 /* Force it to be output, else may be solely inlined. */
481 TREE_ADDRESSABLE (fn_decl) = 1;
483 /* Stop -O3 from deleting it. */
484 TREE_USED (fn_decl) = 1;
486 /* Add a new level to the debugger symbol table. */
488 (*lang_hooks.decls.pushlevel) (0);
490 /* Create rtl for the start of a new scope. */
492 expand_start_bindings (0);
494 emit_line_note (loc); /* Output the line number information. */
497 /* Wrapup a function contained in file FILENAME, ending at line LINENO. */
499 tree_code_create_function_wrapup (location_t loc)
504 fn_decl = current_function_decl;
506 emit_line_note (loc); /* Output the line number information. */
508 /* Get completely built level from debugger symbol table. */
510 block = (*lang_hooks.decls.poplevel) (1, 0, 0);
512 /* Emit rtl for end of scope. */
514 expand_end_bindings (block, 0, 1);
516 /* Emit rtl for end of function. */
518 expand_function_end ();
522 block = (*lang_hooks.decls.poplevel) (1, 0, 1);
524 /* And attach it to the function. */
526 DECL_INITIAL (fn_decl) = block;
528 /* Emit rtl for end of scope. */
530 expand_end_bindings (block, 0, 1);
532 /* Call optimization and convert optimized rtl to assembly code. */
534 rest_of_compilation (fn_decl);
536 /* We are not inside of any scope now. */
538 current_function_decl = NULL_TREE;
544 The storage class is STORAGE_CLASS (eg LOCAL).
545 The name is CHARS/LENGTH.
546 The type is EXPRESSION_TYPE (eg UNSIGNED_TYPE).
547 The init tree is INIT.
551 tree_code_create_variable (unsigned int storage_class,
552 unsigned char* chars,
554 unsigned int expression_type,
562 /* 1. Build the type. */
563 var_type = get_type_for_numeric_type (expression_type);
565 /* 2. Build the name. */
566 if (chars[length] != 0)
567 abort (); /* Should be null terminated. */
569 var_id = get_identifier ((const char*)chars);
571 /* 3. Build the decl and set up init. */
572 var_decl = build_decl (VAR_DECL, var_id, var_type);
574 /* 3a. Initialization. */
576 DECL_INITIAL (var_decl) = build1 (CONVERT_EXPR, var_type, init);
578 DECL_INITIAL (var_decl) = NULL_TREE;
580 /* 4. Compute size etc. */
581 layout_decl (var_decl, 0);
583 if (TYPE_SIZE (var_type) == 0)
584 abort (); /* Did not calculate size. */
586 DECL_CONTEXT (var_decl) = current_function_decl;
588 DECL_SOURCE_LOCATION (var_decl) = loc;
590 /* Set the storage mode and whether only visible in the same file. */
591 switch (storage_class)
594 TREE_STATIC (var_decl) = 1;
595 TREE_PUBLIC (var_decl) = 0;
598 case AUTOMATIC_STORAGE:
599 TREE_STATIC (var_decl) = 0;
600 TREE_PUBLIC (var_decl) = 0;
603 case EXTERNAL_DEFINITION_STORAGE:
604 TREE_STATIC (var_decl) = 0;
605 TREE_PUBLIC (var_decl) = 1;
608 case EXTERNAL_REFERENCE_STORAGE:
609 DECL_EXTERNAL (var_decl) = 1;
610 TREE_PUBLIC (var_decl) = 0;
617 /* This should really only be set if the variable is used. */
618 TREE_USED (var_decl) = 1;
620 /* Expand declaration and initial value if any. */
622 if (TREE_STATIC (var_decl))
623 rest_of_decl_compilation (var_decl, 0, 0, 0);
626 expand_decl (var_decl);
627 if (DECL_INITIAL (var_decl))
628 expand_decl_init (var_decl);
631 return pushdecl (copy_node (var_decl));
636 /* Generate code for return statement. Type is in TYPE, expression
637 is in EXP if present. */
640 tree_code_generate_return (tree type, tree exp)
645 for (param = DECL_ARGUMENTS (current_function_decl);
647 param = TREE_CHAIN (param))
649 if (DECL_CONTEXT (param) != current_function_decl)
655 setret = build (MODIFY_EXPR, type, DECL_RESULT (current_function_decl),
656 build1 (CONVERT_EXPR, type, exp));
657 TREE_SIDE_EFFECTS (setret) = 1;
658 TREE_USED (setret) = 1;
659 expand_expr_stmt (setret);
661 expand_return (DECL_RESULT (current_function_decl));
664 /* Output the code for this expression statement CODE. */
668 tree_code_output_expression_statement (tree code, location_t loc)
670 /* Output the line number information. */
671 emit_line_note (loc);
672 TREE_USED (code) = 1;
673 TREE_SIDE_EFFECTS (code) = 1;
674 expand_expr_stmt (code);
677 /* Return a tree for a constant integer value in the token TOK. No
678 size checking is done. */
681 tree_code_get_integer_value (unsigned char* chars, unsigned int length)
683 long long int val = 0;
685 unsigned int start = 0;
689 case (unsigned char)'-':
694 case (unsigned char)'+':
701 for (ix = start; ix < length; ix++)
702 val = val * 10 + chars[ix] - (unsigned char)'0';
704 return build_int_2 (val & 0xffffffff, (val >> 32) & 0xffffffff);
707 /* Return the tree for an expresssion, type EXP_TYPE (see treetree.h)
708 with tree type TYPE and with operands1 OP1, OP2 (maybe), OP3 (maybe). */
710 tree_code_get_expression (unsigned int exp_type,
711 tree type, tree op1, tree op2, tree op3 ATTRIBUTE_UNUSED)
721 operator = MODIFY_EXPR;
722 ret1 = build (operator, type,
724 build1 (CONVERT_EXPR, type, op2));
729 operator = PLUS_EXPR;
730 goto binary_expression;
733 operator = MINUS_EXPR;
734 goto binary_expression;
738 goto binary_expression;
740 /* Expand a binary expression. Ensure the operands are the right type. */
744 ret1 = build (operator, type,
745 build1 (CONVERT_EXPR, type, op1),
746 build1 (CONVERT_EXPR, type, op2));
749 /* Reference to a variable. This is dead easy, just return the
750 decl for the variable. If the TYPE is different than the
751 variable type, convert it. */
755 if (type == TREE_TYPE (op1))
758 ret1 = build1 (CONVERT_EXPR, type, op1);
761 case EXP_FUNCTION_INVOCATION:
766 fun_ptr = build1 (ADDR_EXPR, build_pointer_type (type), op1);
767 ret1 = build (CALL_EXPR, type, fun_ptr, nreverse (op2));
778 /* Init parameter list and return empty list. */
781 tree_code_init_parameters (void)
786 /* Add a parameter EXP whose expression type is EXP_PROTO to list
787 LIST, returning the new list. */
790 tree_code_add_parameter (tree list, tree proto_exp, tree exp)
793 new_exp = tree_cons (NULL_TREE,
794 build1 (CONVERT_EXPR, TREE_TYPE (proto_exp), exp),
798 return chainon (new_exp, list);
801 /* Get the tree type for this type whose number is NUMERIC_TYPE. */
804 get_type_for_numeric_type (unsigned int numeric_type)
809 switch (numeric_type)
812 return void_type_node;
815 size1 = tree_code_int_size;
820 size1 = tree_code_int_size;
825 size1 = tree_code_char_size;
830 size1 = tree_code_char_size;
838 return tree_code_get_numeric_type (size1, sign1);
842 /* Return tree representing a numeric type of size SIZE1 bits and
843 signed if SIGN1 != 0. */
845 tree_code_get_numeric_type (unsigned int size1, unsigned int sign1)
850 if (size1 == tree_code_int_size)
853 ret1 = integer_type_node;
855 ret1 = unsigned_type_node;
858 if (size1 == tree_code_char_size)
861 ret1 = signed_char_type_node;
863 ret1 = unsigned_char_type_node;
871 /* Get a stringpool entry for a string S of length L. This is needed
872 because the GTY routines don't mark strings, forcing you to put
873 them into stringpool, which is never freed. */
876 get_string (const char *s, size_t l)
879 t = get_identifier_with_length (s, l);
880 return IDENTIFIER_POINTER(t);
883 /* Save typing debug_tree all the time. Dump a tree T pretty and
894 /* Routines Expected by gcc: */
896 /* These are used to build types for various sizes. The code below
897 is a simplified version of that of GNAT. */
899 #ifndef MAX_BITS_PER_WORD
900 #define MAX_BITS_PER_WORD BITS_PER_WORD
903 /* This variable keeps a table for types for each precision so that we only
904 allocate each of them once. Signed and unsigned types are kept separate. */
905 static GTY(()) tree signed_and_unsigned_types[MAX_BITS_PER_WORD + 1][2];
907 /* XXX is this definition OK? */
909 tree_lang_truthvalue_conversion (expr)
915 /* Mark EXP saying that we need to be able to take the
916 address of it; it should not be allocated in a register.
917 Value is 1 if successful.
919 This implementation was copied from c-decl.c. */
922 tree_mark_addressable (exp)
925 register tree x = exp;
927 switch (TREE_CODE (x))
934 x = TREE_OPERAND (x, 0);
938 TREE_ADDRESSABLE (x) = 1;
945 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
946 && DECL_NONLOCAL (x))
950 error ("global register variable `%s' used in nested function",
951 IDENTIFIER_POINTER (DECL_NAME (x)));
954 pedwarn ("register variable `%s' used in nested function",
955 IDENTIFIER_POINTER (DECL_NAME (x)));
957 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
961 error ("address of global register variable `%s' requested",
962 IDENTIFIER_POINTER (DECL_NAME (x)));
966 pedwarn ("address of register variable `%s' requested",
967 IDENTIFIER_POINTER (DECL_NAME (x)));
969 put_var_into_stack (x, /*rescan=*/ true);
973 TREE_ADDRESSABLE (x) = 1;
980 /* Return an integer type with the number of bits of precision given by
981 PRECISION. UNSIGNEDP is nonzero if the type is unsigned; otherwise
982 it is a signed type. */
985 tree_lang_type_for_size (precision, unsignedp)
991 if (precision <= MAX_BITS_PER_WORD
992 && signed_and_unsigned_types[precision][unsignedp] != 0)
993 return signed_and_unsigned_types[precision][unsignedp];
996 t = signed_and_unsigned_types[precision][1]
997 = make_unsigned_type (precision);
999 t = signed_and_unsigned_types[precision][0]
1000 = make_signed_type (precision);
1005 /* Return a data type that has machine mode MODE. UNSIGNEDP selects
1006 an unsigned type; otherwise a signed type is returned. */
1009 tree_lang_type_for_mode (mode, unsignedp)
1010 enum machine_mode mode;
1013 return tree_lang_type_for_size (GET_MODE_BITSIZE (mode), unsignedp);
1016 /* Return the unsigned version of a TYPE_NODE, a scalar type. */
1019 tree_lang_unsigned_type (type_node)
1022 return tree_lang_type_for_size (TYPE_PRECISION (type_node), 1);
1025 /* Return the signed version of a TYPE_NODE, a scalar type. */
1028 tree_lang_signed_type (type_node)
1031 return tree_lang_type_for_size (TYPE_PRECISION (type_node), 0);
1034 /* Return a type the same as TYPE except unsigned or signed according to
1038 tree_lang_signed_or_unsigned_type (unsignedp, type)
1042 if (! INTEGRAL_TYPE_P (type) || TREE_UNSIGNED (type) == unsignedp)
1045 return tree_lang_type_for_size (TYPE_PRECISION (type), unsignedp);
1048 /* These functions and variables deal with binding contours. We only
1049 need these functions for the list of PARM_DECLs, but we leave the
1050 functions more general; these are a simplified version of the
1051 functions from GNAT. */
1053 /* For each binding contour we allocate a binding_level structure which records
1054 the entities defined or declared in that contour. Contours include:
1057 one for each subprogram definition
1058 one for each compound statement (declare block)
1060 Binding contours are used to create GCC tree BLOCK nodes. */
1062 struct binding_level
1064 /* A chain of ..._DECL nodes for all variables, constants, functions,
1065 parameters and type declarations. These ..._DECL nodes are chained
1066 through the TREE_CHAIN field. Note that these ..._DECL nodes are stored
1067 in the reverse of the order supplied to be compatible with the
1070 /* For each level (except the global one), a chain of BLOCK nodes for all
1071 the levels that were entered and exited one level down from this one. */
1073 /* The back end may need, for its own internal processing, to create a BLOCK
1074 node. This field is set aside for this purpose. If this field is non-null
1075 when the level is popped, i.e. when poplevel is invoked, we will use such
1076 block instead of creating a new one from the 'names' field, that is the
1077 ..._DECL nodes accumulated so far. Typically the routine 'pushlevel'
1078 will be called before setting this field, so that if the front-end had
1079 inserted ..._DECL nodes in the current block they will not be lost. */
1080 tree block_created_by_back_end;
1081 /* The binding level containing this one (the enclosing binding level). */
1082 struct binding_level *level_chain;
1085 /* The binding level currently in effect. */
1086 static struct binding_level *current_binding_level = NULL;
1088 /* The outermost binding level. This binding level is created when the
1089 compiler is started and it will exist through the entire compilation. */
1090 static struct binding_level *global_binding_level;
1092 /* Binding level structures are initialized by copying this one. */
1093 static struct binding_level clear_binding_level = {NULL, NULL, NULL, NULL};
1095 /* Return non-zero if we are currently in the global binding level. */
1098 global_bindings_p ()
1100 return current_binding_level == global_binding_level ? -1 : 0;
1103 /* Return the list of declarations in the current level. Note that this list
1104 is in reverse order (it has to be so for back-end compatibility). */
1109 return current_binding_level->names;
1112 /* Nonzero if the current level needs to have a BLOCK made. */
1117 return (current_binding_level->names != 0);
1120 /* Enter a new binding level. The input parameter is ignored, but has to be
1121 specified for back-end compatibility. */
1125 int ignore ATTRIBUTE_UNUSED;
1127 struct binding_level *newlevel
1128 = (struct binding_level *) xmalloc (sizeof (struct binding_level));
1130 *newlevel = clear_binding_level;
1132 /* Add this level to the front of the chain (stack) of levels that are
1134 newlevel->level_chain = current_binding_level;
1135 current_binding_level = newlevel;
1138 /* Exit a binding level.
1139 Pop the level off, and restore the state of the identifier-decl mappings
1140 that were in effect when this level was entered.
1142 If KEEP is nonzero, this level had explicit declarations, so
1143 and create a "block" (a BLOCK node) for the level
1144 to record its declarations and subblocks for symbol table output.
1146 If FUNCTIONBODY is nonzero, this level is the body of a function,
1147 so create a block as if KEEP were set and also clear out all
1150 If REVERSE is nonzero, reverse the order of decls before putting
1151 them into the BLOCK. */
1154 poplevel (keep, reverse, functionbody)
1159 /* Points to a BLOCK tree node. This is the BLOCK node construted for the
1160 binding level that we are about to exit and which is returned by this
1162 tree block_node = NULL_TREE;
1164 tree subblock_chain = current_binding_level->blocks;
1166 tree block_created_by_back_end;
1168 /* Reverse the list of *_DECL nodes if desired. Note that the ..._DECL
1169 nodes chained through the `names' field of current_binding_level are in
1170 reverse order except for PARM_DECL node, which are explicitely stored in
1172 decl_chain = (reverse) ? nreverse (current_binding_level->names)
1173 : current_binding_level->names;
1175 block_created_by_back_end = current_binding_level->block_created_by_back_end;
1176 if (block_created_by_back_end != 0)
1178 block_node = block_created_by_back_end;
1180 /* Check if we are about to discard some information that was gathered
1181 by the front-end. Nameley check if the back-end created a new block
1182 without calling pushlevel first. To understand why things are lost
1183 just look at the next case (i.e. no block created by back-end. */
1184 if ((keep || functionbody) && (decl_chain || subblock_chain))
1188 /* If there were any declarations in the current binding level, or if this
1189 binding level is a function body, or if there are any nested blocks then
1190 create a BLOCK node to record them for the life of this function. */
1191 else if (keep || functionbody)
1192 block_node = build_block (keep ? decl_chain : 0, 0, subblock_chain, 0, 0);
1194 /* Record the BLOCK node just built as the subblock its enclosing scope. */
1195 for (subblock_node = subblock_chain; subblock_node;
1196 subblock_node = TREE_CHAIN (subblock_node))
1197 BLOCK_SUPERCONTEXT (subblock_node) = block_node;
1199 /* Clear out the meanings of the local variables of this level. */
1201 for (subblock_node = decl_chain; subblock_node;
1202 subblock_node = TREE_CHAIN (subblock_node))
1203 if (DECL_NAME (subblock_node) != 0)
1204 /* If the identifier was used or addressed via a local extern decl,
1205 don't forget that fact. */
1206 if (DECL_EXTERNAL (subblock_node))
1208 if (TREE_USED (subblock_node))
1209 TREE_USED (DECL_NAME (subblock_node)) = 1;
1210 if (TREE_ADDRESSABLE (subblock_node))
1211 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (subblock_node)) = 1;
1214 /* Pop the current level. */
1215 current_binding_level = current_binding_level->level_chain;
1219 /* This is the top level block of a function. The ..._DECL chain stored
1220 in BLOCK_VARS are the function's parameters (PARM_DECL nodes). Don't
1221 leave them in the BLOCK because they are found in the FUNCTION_DECL
1223 DECL_INITIAL (current_function_decl) = block_node;
1224 BLOCK_VARS (block_node) = 0;
1226 else if (block_node)
1228 if (block_created_by_back_end == NULL)
1229 current_binding_level->blocks
1230 = chainon (current_binding_level->blocks, block_node);
1233 /* If we did not make a block for the level just exited, any blocks made for
1234 inner levels (since they cannot be recorded as subblocks in that level)
1235 must be carried forward so they will later become subblocks of something
1237 else if (subblock_chain)
1238 current_binding_level->blocks
1239 = chainon (current_binding_level->blocks, subblock_chain);
1241 TREE_USED (block_node) = 1;
1246 /* Insert BLOCK at the end of the list of subblocks of the
1247 current binding level. This is used when a BIND_EXPR is expanded,
1248 to handle the BLOCK node inside the BIND_EXPR. */
1251 insert_block (block)
1254 TREE_USED (block) = 1;
1255 current_binding_level->blocks
1256 = chainon (current_binding_level->blocks, block);
1259 /* Set the BLOCK node for the innermost scope
1260 (the one we are currently in). */
1266 current_binding_level->block_created_by_back_end = block;
1269 /* Records a ..._DECL node DECL as belonging to the current lexical scope.
1270 Returns the ..._DECL node. */
1276 /* External objects aren't nested, other objects may be. */
1278 if ((DECL_EXTERNAL (decl)) || (decl==current_function_decl))
1279 DECL_CONTEXT (decl) = 0;
1281 DECL_CONTEXT (decl) = current_function_decl;
1283 /* Put the declaration on the list. The list of declarations is in reverse
1284 order. The list will be reversed later if necessary. This needs to be
1285 this way for compatibility with the back-end. */
1287 TREE_CHAIN (decl) = current_binding_level->names;
1288 current_binding_level->names = decl;
1290 /* For the declartion of a type, set its name if it is not already set. */
1292 if (TREE_CODE (decl) == TYPE_DECL
1293 && TYPE_NAME (TREE_TYPE (decl)) == 0)
1294 TYPE_NAME (TREE_TYPE (decl)) = DECL_NAME (decl);
1301 tree_push_type_decl(id, type_node)
1305 tree decl = build_decl (TYPE_DECL, id, type_node);
1306 TYPE_NAME (type_node) = decl;
1307 TYPE_STUB_DECL (type_node) = decl;
1311 /* push_atomic_type_decl() ensures that the type's type is itself.
1312 Needed for DBX. Must only be used for atomic types,
1313 not for e.g. pointer or array types. */
1316 tree_push_atomic_type_decl(id, type_node)
1320 TREE_TYPE (type_node) = type_node;
1321 tree_push_type_decl (id, type_node);
1324 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
1326 /* Create the predefined scalar types of C,
1327 and some nodes representing standard constants (0, 1, (void *) 0).
1328 Initialize the global binding level.
1329 Make definitions for built-in primitive functions. */
1332 treelang_init_decl_processing ()
1334 current_function_decl = NULL;
1335 current_binding_level = NULL_BINDING_LEVEL;
1336 pushlevel (0); /* make the binding_level structure for global names */
1337 global_binding_level = current_binding_level;
1339 build_common_tree_nodes (flag_signed_char);
1341 /* set standard type names */
1343 /* Define `int' and `char' first so that dbx will output them first. */
1345 tree_push_atomic_type_decl (get_identifier ("int"), integer_type_node);
1346 tree_push_atomic_type_decl (get_identifier ("char"), char_type_node);
1347 tree_push_atomic_type_decl (get_identifier ("long int"),
1348 long_integer_type_node);
1349 tree_push_atomic_type_decl (get_identifier ("unsigned int"),
1350 unsigned_type_node);
1351 tree_push_atomic_type_decl (get_identifier ("long unsigned int"),
1352 long_unsigned_type_node);
1353 tree_push_atomic_type_decl (get_identifier ("long long int"),
1354 long_long_integer_type_node);
1355 tree_push_atomic_type_decl (get_identifier ("long long unsigned int"),
1356 long_long_unsigned_type_node);
1357 tree_push_atomic_type_decl (get_identifier ("short int"),
1358 short_integer_type_node);
1359 tree_push_atomic_type_decl (get_identifier ("short unsigned int"),
1360 short_unsigned_type_node);
1361 tree_push_atomic_type_decl (get_identifier ("signed char"),
1362 signed_char_type_node);
1363 tree_push_atomic_type_decl (get_identifier ("unsigned char"),
1364 unsigned_char_type_node);
1365 tree_push_atomic_type_decl (NULL_TREE, intQI_type_node);
1366 tree_push_atomic_type_decl (NULL_TREE, intHI_type_node);
1367 tree_push_atomic_type_decl (NULL_TREE, intSI_type_node);
1368 tree_push_atomic_type_decl (NULL_TREE, intDI_type_node);
1369 #if HOST_BITS_PER_WIDE_INT >= 64
1370 tree_push_atomic_type_decl (NULL_TREE, intTI_type_node);
1372 tree_push_atomic_type_decl (NULL_TREE, unsigned_intQI_type_node);
1373 tree_push_atomic_type_decl (NULL_TREE, unsigned_intHI_type_node);
1374 tree_push_atomic_type_decl (NULL_TREE, unsigned_intSI_type_node);
1375 tree_push_atomic_type_decl (NULL_TREE, unsigned_intDI_type_node);
1376 #if HOST_BITS_PER_WIDE_INT >= 64
1377 tree_push_atomic_type_decl (NULL_TREE, unsigned_intTI_type_node);
1380 size_type_node = make_unsigned_type (POINTER_SIZE);
1381 tree_push_atomic_type_decl (get_identifier ("size_t"), size_type_node);
1382 set_sizetype (size_type_node);
1384 build_common_tree_nodes_2 (/* short_double= */ 0);
1386 tree_push_atomic_type_decl (get_identifier ("float"), float_type_node);
1387 tree_push_atomic_type_decl (get_identifier ("double"), double_type_node);
1388 tree_push_atomic_type_decl (get_identifier ("long double"), long_double_type_node);
1389 tree_push_atomic_type_decl (get_identifier ("void"), void_type_node);
1391 /* Add any target-specific builtin functions. */
1392 (*targetm.init_builtins) ();
1394 pedantic_lvalues = pedantic;
1397 /* Return a definition for a builtin function named NAME and whose data type
1398 is TYPE. TYPE should be a function type with argument types.
1399 FUNCTION_CODE tells later passes how to compile calls to this function.
1400 See tree.h for its possible values.
1402 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
1403 the name to be called if we can't opencode the function. If
1404 ATTRS is nonzero, use that for the function's attribute list.
1406 copied from gcc/c-decl.c
1410 builtin_function (name, type, function_code, class, library_name, attrs)
1414 enum built_in_class class;
1415 const char *library_name;
1418 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
1419 DECL_EXTERNAL (decl) = 1;
1420 TREE_PUBLIC (decl) = 1;
1422 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
1423 make_decl_rtl (decl, NULL);
1425 DECL_BUILT_IN_CLASS (decl) = class;
1426 DECL_FUNCTION_CODE (decl) = function_code;
1428 /* Possibly apply some default attributes to this built-in function. */
1430 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
1432 decl_attributes (&decl, NULL_TREE, 0);
1437 #include "debug.h" /* for debug_hooks, needed by gt-treelang-treetree.h */
1438 #include "gt-treelang-treetree.h"