1 /* TREELANG Compiler interface to GCC's middle end (treetree.c)
4 If you want a working example of how to write a front end to GCC,
5 you are in the right place.
7 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
8 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
10 This code is based on toy.c written by Richard Kenner.
12 It was later modified by Jonathan Bartlett whose changes have all
13 been removed (by Tim Josling).
15 Various bits and pieces were cloned from the GCC main tree, as
16 GCC evolved, for COBOLForGCC, by Tim Josling.
18 It was adapted to TREELANG by Tim Josling 2001.
20 Updated to function-at-a-time by James A. Morrison, 2004.
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 ----------------------------------------------------------------------- */
45 /* Assumption: garbage collection is never called implicitly. It will
46 not be called 'at any time' when short of memory. It will only be
47 called explicitly at the end of each function. This removes the
48 need for a *lot* of bother to ensure everything is in the mark trees
51 /* Note, it is OK to use GCC extensions such as long long in a compiler front
52 end. This is because the GCC front ends are built using GCC. */
58 #include "coretypes.h"
61 #include "tree-dump.h"
62 #include "tree-iterator.h"
63 #include "tree-gimple.h"
70 #include "langhooks-def.h"
71 #include "langhooks.h"
80 extern int option_main
;
81 extern char **file_names
;
83 /* Types expected by gcc's garbage collector.
84 These types exist to allow language front-ends to
85 add extra information in gcc's parse tree data structure.
86 But the treelang front end doesn't use them -- it has
87 its own parse tree data structure.
88 We define them here only to satisfy gcc's garbage collector. */
90 /* Language-specific identifier information. */
92 struct lang_identifier
GTY(())
94 struct tree_identifier common
;
97 /* Language-specific tree node information. */
100 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE")))
102 union tree_node
GTY ((tag ("0"),
103 desc ("tree_node_structure (&%h)")))
105 struct lang_identifier
GTY ((tag ("1"))) identifier
;
108 /* Language-specific type information. */
110 struct lang_type
GTY(())
112 char junk
; /* dummy field to ensure struct is not empty */
115 /* Language-specific declaration information. */
117 struct lang_decl
GTY(())
119 char junk
; /* dummy field to ensure struct is not empty */
122 struct language_function
GTY(())
124 char junk
; /* dummy field to ensure struct is not empty */
127 static tree
tree_lang_truthvalue_conversion (tree expr
);
128 static bool tree_mark_addressable (tree exp
);
129 static tree
tree_lang_type_for_size (unsigned precision
, int unsignedp
);
130 static tree
tree_lang_type_for_mode (enum machine_mode mode
, int unsignedp
);
131 static tree
tree_lang_unsigned_type (tree type_node
);
132 static tree
tree_lang_signed_type (tree type_node
);
133 static tree
tree_lang_signed_or_unsigned_type (int unsignedp
, tree type
);
135 /* Functions to keep track of the current scope. */
136 static void pushlevel (int ignore
);
137 static tree
poplevel (int keep
, int reverse
, int functionbody
);
138 static tree
pushdecl (tree decl
);
139 static tree
* getstmtlist (void);
142 static tree
builtin_function (const char *name
, tree type
, int function_code
,
143 enum built_in_class
class, const char *library_name
,
145 static tree
getdecls (void);
146 static int global_bindings_p (void);
147 static void insert_block (tree
);
149 static void tree_push_type_decl (tree id
, tree type_node
);
150 static void treelang_expand_function (tree fndecl
);
152 /* The front end language hooks (addresses of code for this front
153 end). These are not really very language-dependent, i.e.
154 treelang, C, Mercury, etc. can all use almost the same definitions. */
156 #undef LANG_HOOKS_TRUTHVALUE_CONVERSION
157 #define LANG_HOOKS_TRUTHVALUE_CONVERSION tree_lang_truthvalue_conversion
158 #undef LANG_HOOKS_MARK_ADDRESSABLE
159 #define LANG_HOOKS_MARK_ADDRESSABLE tree_mark_addressable
160 #undef LANG_HOOKS_SIGNED_TYPE
161 #define LANG_HOOKS_SIGNED_TYPE tree_lang_signed_type
162 #undef LANG_HOOKS_UNSIGNED_TYPE
163 #define LANG_HOOKS_UNSIGNED_TYPE tree_lang_unsigned_type
164 #undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
165 #define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE tree_lang_signed_or_unsigned_type
166 #undef LANG_HOOKS_TYPE_FOR_MODE
167 #define LANG_HOOKS_TYPE_FOR_MODE tree_lang_type_for_mode
168 #undef LANG_HOOKS_TYPE_FOR_SIZE
169 #define LANG_HOOKS_TYPE_FOR_SIZE tree_lang_type_for_size
170 #undef LANG_HOOKS_PARSE_FILE
171 #define LANG_HOOKS_PARSE_FILE treelang_parse_file
173 #undef LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION
174 #define LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION treelang_expand_function
176 /* #undef LANG_HOOKS_TYPES_COMPATIBLE_P
177 #define LANG_HOOKS_TYPES_COMPATIBLE_P hook_bool_tree_tree_true
179 /* Hook routines and data unique to treelang. */
181 #undef LANG_HOOKS_INIT
182 #define LANG_HOOKS_INIT treelang_init
183 #undef LANG_HOOKS_NAME
184 #define LANG_HOOKS_NAME "GNU treelang"
185 #undef LANG_HOOKS_FINISH
186 #define LANG_HOOKS_FINISH treelang_finish
187 #undef LANG_HOOKS_INIT_OPTIONS
188 #define LANG_HOOKS_INIT_OPTIONS treelang_init_options
189 #undef LANG_HOOKS_HANDLE_OPTION
190 #define LANG_HOOKS_HANDLE_OPTION treelang_handle_option
191 const struct lang_hooks lang_hooks
= LANG_HOOKS_INITIALIZER
;
193 /* Tree code type/name/code tables. */
195 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
197 const enum tree_code_class tree_code_type
[] = {
203 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
205 const unsigned char tree_code_length
[] = {
211 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
213 const char *const tree_code_name
[] = {
219 /* Number of bits in int and char - accessed by front end. */
221 unsigned int tree_code_int_size
= SIZEOF_INT
* HOST_BITS_PER_CHAR
;
223 unsigned int tree_code_char_size
= HOST_BITS_PER_CHAR
;
225 /* Return the tree stuff for this type TYPE_NUM. */
228 tree_code_get_type (int type_num
)
233 return signed_char_type_node
;
236 return unsigned_char_type_node
;
239 return integer_type_node
;
242 return unsigned_type_node
;
245 return void_type_node
;
252 /* Output the code for the start of an if statement. The test
253 expression is EXP (true if not zero), and the stmt occurred at line
254 LINENO in file FILENAME. */
257 tree_code_if_start (tree exp
, location_t loc
)
260 cond_exp
= fold (build2 (NE_EXPR
, boolean_type_node
, exp
,
261 fold (build1 (CONVERT_EXPR
, TREE_TYPE (exp
),
262 integer_zero_node
))));
263 SET_EXPR_LOCATION (cond_exp
, loc
);
264 cond
= build3 (COND_EXPR
, void_type_node
, cond_exp
, NULL_TREE
,
266 SET_EXPR_LOCATION (cond
, loc
);
267 append_to_statement_list_force (cond
, getstmtlist ());
271 /* Output the code for the else of an if statement. The else occurred
272 at line LINENO in file FILENAME. */
275 tree_code_if_else (location_t loc ATTRIBUTE_UNUSED
)
277 tree stmts
= *getstmtlist ();
278 tree block
= poplevel (1, 0, 0);
279 if (BLOCK_VARS (block
))
281 tree bindexpr
= build3 (BIND_EXPR
, void_type_node
, BLOCK_VARS (block
),
283 stmts
= alloc_stmt_list ();
284 append_to_statement_list (bindexpr
, &stmts
);
287 TREE_OPERAND (STATEMENT_LIST_TAIL (*getstmtlist ())->stmt
, 1) = stmts
;
291 /* Output the code for the end_if an if statement. The end_if (final brace)
292 occurred at line LINENO in file FILENAME. */
295 tree_code_if_end (location_t loc ATTRIBUTE_UNUSED
)
297 tree stmts
= *getstmtlist ();
298 tree block
= poplevel (1, 0, 0);
299 if (BLOCK_VARS (block
))
301 tree bindexpr
= build3 (BIND_EXPR
, void_type_node
, BLOCK_VARS (block
),
303 stmts
= alloc_stmt_list ();
304 append_to_statement_list (bindexpr
, &stmts
);
307 TREE_OPERAND (STATEMENT_LIST_TAIL (*getstmtlist ())->stmt
, 2) = stmts
;
310 /* Create a function. The prototype name is NAME, storage class is
311 STORAGE_CLASS, type of return variable is RET_TYPE, parameter lists
312 is PARMS, returns decl for this function. */
315 tree_code_create_function_prototype (unsigned char* chars
,
316 unsigned int storage_class
,
317 unsigned int ret_type
,
318 struct prod_token_parm_item
* parms
,
323 struct prod_token_parm_item
* parm
;
324 tree type_list
= NULL_TREE
;
328 tree parm_list
= NULL_TREE
;
330 /* Build the type. */
331 id
= get_identifier ((const char*)chars
);
332 for (parm
= parms
; parm
; parm
= parm
->tp
.par
.next
)
334 gcc_assert (parm
->category
== parameter_category
);
335 type_node
= tree_code_get_type (parm
->type
);
336 type_list
= tree_cons (NULL_TREE
, type_node
, type_list
);
338 /* Last parm if void indicates fixed length list (as opposed to
339 printf style va_* list). */
340 type_list
= tree_cons (NULL_TREE
, void_type_node
, type_list
);
342 /* The back end needs them in reverse order. */
343 type_list
= nreverse (type_list
);
345 type_node
= tree_code_get_type (ret_type
);
346 fn_type
= build_function_type (type_node
, type_list
);
348 id
= get_identifier ((const char*)chars
);
349 fn_decl
= build_decl (FUNCTION_DECL
, id
, fn_type
);
351 /* Nested functions not supported here. */
352 DECL_CONTEXT (fn_decl
) = NULL_TREE
;
353 DECL_SOURCE_LOCATION (fn_decl
) = loc
;
355 TREE_USED (fn_decl
) = 1;
357 TREE_PUBLIC (fn_decl
) = 0;
358 DECL_EXTERNAL (fn_decl
) = 0;
359 TREE_STATIC (fn_decl
) = 0;
360 switch (storage_class
)
363 TREE_PUBLIC (fn_decl
) = 0;
366 case EXTERNAL_DEFINITION_STORAGE
:
367 TREE_PUBLIC (fn_decl
) = 1;
368 TREE_STATIC (fn_decl
) = 0;
369 DECL_EXTERNAL (fn_decl
) = 0;
372 case EXTERNAL_REFERENCE_STORAGE
:
373 TREE_PUBLIC (fn_decl
) = 0;
374 DECL_EXTERNAL (fn_decl
) = 1;
377 case AUTOMATIC_STORAGE
:
382 /* Make the argument variable decls. */
383 for (parm
= parms
; parm
; parm
= parm
->tp
.par
.next
)
385 tree parm_decl
= build_decl (PARM_DECL
, get_identifier
386 ((const char*) (parm
->tp
.par
.variable_name
)),
387 tree_code_get_type (parm
->type
));
389 /* Some languages have different nominal and real types. */
390 DECL_ARG_TYPE (parm_decl
) = TREE_TYPE (parm_decl
);
391 gcc_assert (DECL_ARG_TYPE (parm_decl
));
392 gcc_assert (fn_decl
);
393 DECL_CONTEXT (parm_decl
) = fn_decl
;
394 DECL_SOURCE_LOCATION (parm_decl
) = loc
;
395 parm_list
= chainon (parm_decl
, parm_list
);
398 /* Back into reverse order as the back end likes them. */
399 parm_list
= nreverse (parm_list
);
401 DECL_ARGUMENTS (fn_decl
) = parm_list
;
403 /* Save the decls for use when the args are referred to. */
404 for (parm
= parms
; parm_list
;
405 parm_list
= TREE_CHAIN (parm_list
),
406 parm
= parm
->tp
.par
.next
)
408 gcc_assert (parm
); /* Too few. */
409 *parm
->tp
.par
.where_to_put_var_tree
= parm_list
;
411 gcc_assert (!parm
); /* Too many. */
413 /* Process declaration of function defined elsewhere. */
414 rest_of_decl_compilation (fn_decl
, 1, 0);
420 /* Output code for start of function; the decl of the function is in
421 PREV_SAVED (as created by tree_code_create_function_prototype),
422 the function is at line number LINENO in file FILENAME. The
423 parameter details are in the lists PARMS. Returns nothing. */
426 tree_code_create_function_initial (tree prev_saved
,
432 fn_decl
= prev_saved
;
433 gcc_assert (fn_decl
);
435 /* Output message if not -quiet. */
436 announce_function (fn_decl
);
438 /* This has something to do with forcing output also. */
441 /* Set current function for error msgs etc. */
442 current_function_decl
= fn_decl
;
443 DECL_INITIAL (fn_decl
) = error_mark_node
;
445 DECL_SOURCE_LOCATION (fn_decl
) = loc
;
447 /* Create a DECL for the functions result. */
449 build_decl (RESULT_DECL
, NULL_TREE
, TREE_TYPE (TREE_TYPE (fn_decl
)));
450 DECL_CONTEXT (resultdecl
) = fn_decl
;
451 DECL_ARTIFICIAL (resultdecl
) = 1;
452 DECL_IGNORED_P (resultdecl
) = 1;
453 DECL_SOURCE_LOCATION (resultdecl
) = loc
;
454 DECL_RESULT (fn_decl
) = resultdecl
;
456 /* Create a new level at the start of the function. */
460 /* Force it to be output, else may be solely inlined. */
461 TREE_ADDRESSABLE (fn_decl
) = 1;
463 /* Stop -O3 from deleting it. */
464 TREE_USED (fn_decl
) = 1;
467 /* Wrapup a function contained in file FILENAME, ending at line LINENO. */
469 tree_code_create_function_wrapup (location_t loc
)
473 tree stmts
= *getstmtlist ();
475 fn_decl
= current_function_decl
;
479 block
= poplevel (1, 0, 1);
481 /* And attach it to the function. */
483 DECL_SAVED_TREE (fn_decl
) = build3 (BIND_EXPR
, void_type_node
,
487 allocate_struct_function (fn_decl
);
488 cfun
->function_end_locus
= loc
;
490 /* Dump the original tree to a file. */
491 dump_function (TDI_original
, fn_decl
);
493 /* Convert current function to GIMPLE for the middle end. */
494 gimplify_function_tree (fn_decl
);
495 dump_function (TDI_generic
, fn_decl
);
497 /* We are not inside of any scope now. */
498 current_function_decl
= NULL_TREE
;
501 /* Pass the current function off to the middle end. */
502 (void)cgraph_node (fn_decl
);
503 cgraph_finalize_function (fn_decl
, false);
506 /* Create a variable.
508 The storage class is STORAGE_CLASS (eg LOCAL).
509 The name is CHARS/LENGTH.
510 The type is EXPRESSION_TYPE (eg UNSIGNED_TYPE).
511 The init tree is INIT. */
514 tree_code_create_variable (unsigned int storage_class
,
515 unsigned char* chars
,
517 unsigned int expression_type
,
525 /* 1. Build the type. */
526 var_type
= tree_code_get_type (expression_type
);
528 /* 2. Build the name. */
529 gcc_assert (chars
[length
] == 0); /* Should be null terminated. */
531 var_id
= get_identifier ((const char*)chars
);
533 /* 3. Build the decl and set up init. */
534 var_decl
= build_decl (VAR_DECL
, var_id
, var_type
);
536 /* 3a. Initialization. */
538 DECL_INITIAL (var_decl
) = fold (build1 (CONVERT_EXPR
, var_type
, init
));
540 DECL_INITIAL (var_decl
) = NULL_TREE
;
542 gcc_assert (TYPE_SIZE (var_type
) != 0); /* Did not calculate size. */
544 DECL_CONTEXT (var_decl
) = current_function_decl
;
546 DECL_SOURCE_LOCATION (var_decl
) = loc
;
548 DECL_EXTERNAL (var_decl
) = 0;
549 TREE_PUBLIC (var_decl
) = 0;
550 TREE_STATIC (var_decl
) = 0;
551 /* Set the storage mode and whether only visible in the same file. */
552 switch (storage_class
)
555 TREE_STATIC (var_decl
) = 1;
558 case AUTOMATIC_STORAGE
:
561 case EXTERNAL_DEFINITION_STORAGE
:
562 TREE_PUBLIC (var_decl
) = 1;
565 case EXTERNAL_REFERENCE_STORAGE
:
566 DECL_EXTERNAL (var_decl
) = 1;
573 /* This should really only be set if the variable is used. */
574 TREE_USED (var_decl
) = 1;
576 TYPE_NAME (TREE_TYPE (var_decl
)) = TYPE_NAME (var_type
);
577 return pushdecl (copy_node (var_decl
));
581 /* Generate code for return statement. Type is in TYPE, expression
582 is in EXP if present. */
585 tree_code_generate_return (tree type
, tree exp
)
588 #ifdef ENABLE_CHECKING
591 for (param
= DECL_ARGUMENTS (current_function_decl
);
593 param
= TREE_CHAIN (param
))
594 gcc_assert (DECL_CONTEXT (param
) == current_function_decl
);
597 if (exp
&& TREE_TYPE (TREE_TYPE (current_function_decl
)) != void_type_node
)
599 setret
= fold (build2 (MODIFY_EXPR
, type
,
600 DECL_RESULT (current_function_decl
),
601 fold (build1 (CONVERT_EXPR
, type
, exp
))));
602 TREE_SIDE_EFFECTS (setret
) = 1;
603 TREE_USED (setret
) = 1;
604 setret
= build1 (RETURN_EXPR
, type
, setret
);
607 setret
= build1 (RETURN_EXPR
, type
, NULL_TREE
);
609 append_to_statement_list_force (setret
, getstmtlist ());
613 /* Output the code for this expression statement CODE. */
616 tree_code_output_expression_statement (tree code
, location_t loc
)
618 /* Output the line number information. */
619 SET_EXPR_LOCATION (code
, loc
);
620 TREE_USED (code
) = 1;
621 TREE_SIDE_EFFECTS (code
) = 1;
622 /* put CODE into the code list. */
623 append_to_statement_list_force (code
, getstmtlist ());
626 /* Return a tree for a constant integer value in the token TOK. No
627 size checking is done. */
630 tree_code_get_integer_value (unsigned char* chars
, unsigned int length
)
632 long long int val
= 0;
634 unsigned int start
= 0;
638 case (unsigned char)'-':
643 case (unsigned char)'+':
650 for (ix
= start
; ix
< length
; ix
++)
651 val
= val
* 10 + chars
[ix
] - (unsigned char)'0';
653 return build_int_cst_wide (NULL_TREE
,
654 val
& 0xffffffff, (val
>> 32) & 0xffffffff);
657 /* Return the tree for an expresssion, type EXP_TYPE (see treetree.h)
658 with tree type TYPE and with operands1 OP1, OP2 (maybe), OP3 (maybe). */
660 tree_code_get_expression (unsigned int exp_type
,
661 tree type
, tree op1
, tree op2
,
662 tree op3 ATTRIBUTE_UNUSED
)
670 gcc_assert (op1
&& op2
);
671 operator = MODIFY_EXPR
;
672 ret1
= fold (build2 (operator, void_type_node
, op1
,
673 fold (build1 (CONVERT_EXPR
, TREE_TYPE (op1
),
679 operator = PLUS_EXPR
;
680 goto binary_expression
;
683 operator = MINUS_EXPR
;
684 goto binary_expression
;
688 goto binary_expression
;
690 /* Expand a binary expression. Ensure the operands are the right type. */
692 gcc_assert (op1
&& op2
);
693 ret1
= fold (build2 (operator, type
,
694 fold (build1 (CONVERT_EXPR
, type
, op1
)),
695 fold (build1 (CONVERT_EXPR
, type
, op2
))));
698 /* Reference to a variable. This is dead easy, just return the
699 decl for the variable. If the TYPE is different than the
700 variable type, convert it. */
703 if (type
== TREE_TYPE (op1
))
706 ret1
= fold (build1 (CONVERT_EXPR
, type
, op1
));
709 case EXP_FUNCTION_INVOCATION
:
713 fun_ptr
= fold (build1 (ADDR_EXPR
,
714 build_pointer_type (TREE_TYPE (op1
)), op1
));
715 ret1
= build3 (CALL_EXPR
, type
, fun_ptr
, nreverse (op2
), NULL_TREE
);
726 /* Init parameter list and return empty list. */
729 tree_code_init_parameters (void)
734 /* Add a parameter EXP whose expression type is EXP_PROTO to list
735 LIST, returning the new list. */
738 tree_code_add_parameter (tree list
, tree proto_exp
, tree exp
)
741 new_exp
= tree_cons (NULL_TREE
,
742 fold (build1 (CONVERT_EXPR
, TREE_TYPE (proto_exp
),
746 return chainon (new_exp
, list
);
749 /* Get a stringpool entry for a string S of length L. This is needed
750 because the GTY routines don't mark strings, forcing you to put
751 them into stringpool, which is never freed. */
754 get_string (const char *s
, size_t l
)
757 t
= get_identifier_with_length (s
, l
);
758 return IDENTIFIER_POINTER(t
);
761 /* Save typing debug_tree all the time. Dump a tree T pretty and
772 /* Routines Expected by gcc: */
774 /* These are used to build types for various sizes. The code below
775 is a simplified version of that of GNAT. */
777 #ifndef MAX_BITS_PER_WORD
778 #define MAX_BITS_PER_WORD BITS_PER_WORD
781 /* This variable keeps a table for types for each precision so that we only
782 allocate each of them once. Signed and unsigned types are kept separate. */
783 static GTY(()) tree signed_and_unsigned_types
[MAX_BITS_PER_WORD
+ 1][2];
785 /* XXX is this definition OK? */
787 tree_lang_truthvalue_conversion (tree expr
)
792 /* Mark EXP saying that we need to be able to take the
793 address of it; it should not be allocated in a register.
794 Value is 1 if successful.
796 This implementation was copied from c-decl.c. */
799 tree_mark_addressable (tree exp
)
801 register tree x
= exp
;
803 switch (TREE_CODE (x
))
810 x
= TREE_OPERAND (x
, 0);
814 TREE_ADDRESSABLE (x
) = 1;
821 if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
)
822 && DECL_NONLOCAL (x
))
826 error ("Global register variable %qD used in nested function.",
830 pedwarn ("Register variable %qD used in nested function.", x
);
832 else if (DECL_REGISTER (x
) && !TREE_ADDRESSABLE (x
))
836 error ("Address of global register variable %qD requested.",
841 pedwarn ("Address of register variable %qD requested.", x
);
846 TREE_ADDRESSABLE (x
) = 1;
853 /* Return an integer type with the number of bits of precision given by
854 PRECISION. UNSIGNEDP is nonzero if the type is unsigned; otherwise
855 it is a signed type. */
858 tree_lang_type_for_size (unsigned precision
, int unsignedp
)
862 if (precision
<= MAX_BITS_PER_WORD
863 && signed_and_unsigned_types
[precision
][unsignedp
] != 0)
864 return signed_and_unsigned_types
[precision
][unsignedp
];
867 t
= signed_and_unsigned_types
[precision
][1]
868 = make_unsigned_type (precision
);
870 t
= signed_and_unsigned_types
[precision
][0]
871 = make_signed_type (precision
);
876 /* Return a data type that has machine mode MODE. UNSIGNEDP selects
877 an unsigned type; otherwise a signed type is returned. */
880 tree_lang_type_for_mode (enum machine_mode mode
, int unsignedp
)
882 if (SCALAR_INT_MODE_P (mode
))
883 return tree_lang_type_for_size (GET_MODE_BITSIZE (mode
), unsignedp
);
888 /* Return the unsigned version of a TYPE_NODE, a scalar type. */
891 tree_lang_unsigned_type (tree type_node
)
893 return tree_lang_type_for_size (TYPE_PRECISION (type_node
), 1);
896 /* Return the signed version of a TYPE_NODE, a scalar type. */
899 tree_lang_signed_type (tree type_node
)
901 return tree_lang_type_for_size (TYPE_PRECISION (type_node
), 0);
904 /* Return a type the same as TYPE except unsigned or signed according to
908 tree_lang_signed_or_unsigned_type (int unsignedp
, tree type
)
910 if (! INTEGRAL_TYPE_P (type
) || TYPE_UNSIGNED (type
) == unsignedp
)
913 return tree_lang_type_for_size (TYPE_PRECISION (type
), unsignedp
);
916 /* These functions and variables deal with binding contours. We only
917 need these functions for the list of PARM_DECLs, but we leave the
918 functions more general; these are a simplified version of the
919 functions from GNAT. */
921 /* For each binding contour we allocate a binding_level structure which records
922 the entities defined or declared in that contour. Contours include:
925 one for each subprogram definition
926 one for each compound statement (declare block)
928 Binding contours are used to create GCC tree BLOCK nodes. */
932 /* A chain of ..._DECL nodes for all variables, constants, functions,
933 parameters and type declarations. These ..._DECL nodes are chained
934 through the TREE_CHAIN field. Note that these ..._DECL nodes are stored
935 in the reverse of the order supplied to be compatible with the
938 /* For each level (except the global one), a chain of BLOCK nodes for all
939 the levels that were entered and exited one level down from this one. */
943 /* The binding level containing this one (the enclosing binding level). */
944 struct binding_level
*level_chain
;
947 /* The binding level currently in effect. */
948 static struct binding_level
*current_binding_level
= NULL
;
950 /* The outermost binding level. This binding level is created when the
951 compiler is started and it will exist through the entire compilation. */
952 static struct binding_level
*global_binding_level
;
954 /* Binding level structures are initialized by copying this one. */
955 static struct binding_level clear_binding_level
= {NULL
, NULL
, NULL
, NULL
};
957 /* Return non-zero if we are currently in the global binding level. */
960 global_bindings_p (void)
962 return current_binding_level
== global_binding_level
? -1 : 0;
966 /* Return the list of declarations in the current level. Note that this list
967 is in reverse order (it has to be so for back-end compatibility). */
972 return current_binding_level
->names
;
975 /* Return a STATMENT_LIST for the current block. */
980 return ¤t_binding_level
->stmts
;
983 /* Enter a new binding level. The input parameter is ignored, but has to be
984 specified for back-end compatibility. */
987 pushlevel (int ignore ATTRIBUTE_UNUSED
)
989 struct binding_level
*newlevel
= xmalloc (sizeof (struct binding_level
));
991 *newlevel
= clear_binding_level
;
993 /* Add this level to the front of the chain (stack) of levels that are
995 newlevel
->level_chain
= current_binding_level
;
996 current_binding_level
= newlevel
;
997 current_binding_level
->stmts
= alloc_stmt_list ();
1000 /* Exit a binding level.
1001 Pop the level off, and restore the state of the identifier-decl mappings
1002 that were in effect when this level was entered.
1004 If KEEP is nonzero, this level had explicit declarations, so
1005 and create a "block" (a BLOCK node) for the level
1006 to record its declarations and subblocks for symbol table output.
1008 If FUNCTIONBODY is nonzero, this level is the body of a function,
1009 so create a block as if KEEP were set and also clear out all
1012 If REVERSE is nonzero, reverse the order of decls before putting
1013 them into the BLOCK. */
1016 poplevel (int keep
, int reverse
, int functionbody
)
1018 /* Points to a BLOCK tree node. This is the BLOCK node construted for the
1019 binding level that we are about to exit and which is returned by this
1021 tree block_node
= NULL_TREE
;
1023 tree subblock_chain
= current_binding_level
->blocks
;
1026 /* Reverse the list of *_DECL nodes if desired. Note that the ..._DECL
1027 nodes chained through the `names' field of current_binding_level are in
1028 reverse order except for PARM_DECL node, which are explicitely stored in
1030 decl_chain
= (reverse
) ? nreverse (current_binding_level
->names
)
1031 : current_binding_level
->names
;
1033 /* If there were any declarations in the current binding level, or if this
1034 binding level is a function body, or if there are any nested blocks then
1035 create a BLOCK node to record them for the life of this function. */
1036 if (keep
|| functionbody
)
1037 block_node
= build_block (keep
? decl_chain
: 0, 0, subblock_chain
, 0, 0);
1039 /* Record the BLOCK node just built as the subblock its enclosing scope. */
1040 for (subblock_node
= subblock_chain
; subblock_node
;
1041 subblock_node
= TREE_CHAIN (subblock_node
))
1042 BLOCK_SUPERCONTEXT (subblock_node
) = block_node
;
1044 /* Clear out the meanings of the local variables of this level. */
1046 for (subblock_node
= decl_chain
; subblock_node
;
1047 subblock_node
= TREE_CHAIN (subblock_node
))
1048 if (DECL_NAME (subblock_node
) != 0)
1049 /* If the identifier was used or addressed via a local extern decl,
1050 don't forget that fact. */
1051 if (DECL_EXTERNAL (subblock_node
))
1053 if (TREE_USED (subblock_node
))
1054 TREE_USED (DECL_NAME (subblock_node
)) = 1;
1057 /* Pop the current level. */
1058 current_binding_level
= current_binding_level
->level_chain
;
1062 /* This is the top level block of a function. */
1063 DECL_INITIAL (current_function_decl
) = block_node
;
1065 else if (block_node
)
1067 current_binding_level
->blocks
1068 = chainon (current_binding_level
->blocks
, block_node
);
1071 /* If we did not make a block for the level just exited, any blocks made for
1072 inner levels (since they cannot be recorded as subblocks in that level)
1073 must be carried forward so they will later become subblocks of something
1075 else if (subblock_chain
)
1076 current_binding_level
->blocks
1077 = chainon (current_binding_level
->blocks
, subblock_chain
);
1079 TREE_USED (block_node
) = 1;
1084 /* Insert BLOCK at the end of the list of subblocks of the
1085 current binding level. This is used when a BIND_EXPR is expanded,
1086 to handle the BLOCK node inside the BIND_EXPR. */
1089 insert_block (tree block
)
1091 TREE_USED (block
) = 1;
1092 current_binding_level
->blocks
1093 = chainon (current_binding_level
->blocks
, block
);
1097 /* Records a ..._DECL node DECL as belonging to the current lexical scope.
1098 Returns the ..._DECL node. */
1101 pushdecl (tree decl
)
1103 /* External objects aren't nested, other objects may be. */
1105 if ((DECL_EXTERNAL (decl
)) || (decl
==current_function_decl
))
1106 DECL_CONTEXT (decl
) = 0;
1108 DECL_CONTEXT (decl
) = current_function_decl
;
1110 /* Put the declaration on the list. The list of declarations is in reverse
1111 order. The list will be reversed later if necessary. This needs to be
1112 this way for compatibility with the back-end. */
1114 TREE_CHAIN (decl
) = current_binding_level
->names
;
1115 current_binding_level
->names
= decl
;
1117 /* For the declartion of a type, set its name if it is not already set. */
1119 if (TREE_CODE (decl
) == TYPE_DECL
1120 && TYPE_NAME (TREE_TYPE (decl
)) == 0)
1121 TYPE_NAME (TREE_TYPE (decl
)) = DECL_NAME (decl
);
1123 /* Put automatic variables into the intermediate representation. */
1124 if (TREE_CODE (decl
) == VAR_DECL
&& !DECL_EXTERNAL (decl
)
1125 && !TREE_STATIC (decl
) && !TREE_PUBLIC (decl
))
1126 tree_code_output_expression_statement (build1 (DECL_EXPR
, void_type_node
,
1128 DECL_SOURCE_LOCATION (decl
));
1134 tree_push_type_decl(tree id
, tree type_node
)
1136 tree decl
= build_decl (TYPE_DECL
, id
, type_node
);
1137 TYPE_NAME (type_node
) = id
;
1141 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
1143 /* Create the predefined scalar types of C,
1144 and some nodes representing standard constants (0, 1, (void *) 0).
1145 Initialize the global binding level.
1146 Make definitions for built-in primitive functions. */
1149 treelang_init_decl_processing (void)
1151 current_function_decl
= NULL
;
1152 current_binding_level
= NULL_BINDING_LEVEL
;
1153 pushlevel (0); /* make the binding_level structure for global names */
1154 global_binding_level
= current_binding_level
;
1156 build_common_tree_nodes (flag_signed_char
, false);
1158 /* set standard type names */
1160 /* Define `int' and `char' last so that they are not overwritten. */
1161 tree_push_type_decl (NULL_TREE
, intQI_type_node
);
1162 tree_push_type_decl (NULL_TREE
, intHI_type_node
);
1163 tree_push_type_decl (NULL_TREE
, intSI_type_node
);
1164 tree_push_type_decl (NULL_TREE
, intDI_type_node
);
1165 #if HOST_BITS_PER_WIDE_INT >= 64
1166 tree_push_type_decl (NULL_TREE
, intTI_type_node
);
1168 tree_push_type_decl (NULL_TREE
, unsigned_intQI_type_node
);
1169 tree_push_type_decl (NULL_TREE
, unsigned_intHI_type_node
);
1170 tree_push_type_decl (NULL_TREE
, unsigned_intSI_type_node
);
1171 tree_push_type_decl (NULL_TREE
, unsigned_intDI_type_node
);
1172 #if HOST_BITS_PER_WIDE_INT >= 64
1173 tree_push_type_decl (NULL_TREE
, unsigned_intTI_type_node
);
1176 tree_push_type_decl (get_identifier ("int"), integer_type_node
);
1177 tree_push_type_decl (get_identifier ("char"), char_type_node
);
1178 tree_push_type_decl (get_identifier ("long int"),
1179 long_integer_type_node
);
1180 tree_push_type_decl (get_identifier ("unsigned int"),
1181 unsigned_type_node
);
1182 tree_push_type_decl (get_identifier ("long unsigned int"),
1183 long_unsigned_type_node
);
1184 tree_push_type_decl (get_identifier ("long long int"),
1185 long_long_integer_type_node
);
1186 tree_push_type_decl (get_identifier ("long long unsigned int"),
1187 long_long_unsigned_type_node
);
1188 tree_push_type_decl (get_identifier ("short int"),
1189 short_integer_type_node
);
1190 tree_push_type_decl (get_identifier ("short unsigned int"),
1191 short_unsigned_type_node
);
1192 tree_push_type_decl (get_identifier ("signed char"),
1193 signed_char_type_node
);
1194 tree_push_type_decl (get_identifier ("unsigned char"),
1195 unsigned_char_type_node
);
1196 size_type_node
= make_unsigned_type (POINTER_SIZE
);
1197 tree_push_type_decl (get_identifier ("size_t"), size_type_node
);
1198 set_sizetype (size_type_node
);
1200 build_common_tree_nodes_2 (/* short_double= */ 0);
1202 tree_push_type_decl (get_identifier ("float"), float_type_node
);
1203 tree_push_type_decl (get_identifier ("double"), double_type_node
);
1204 tree_push_type_decl (get_identifier ("long double"), long_double_type_node
);
1205 tree_push_type_decl (get_identifier ("void"), void_type_node
);
1207 build_common_builtin_nodes ();
1208 (*targetm
.init_builtins
) ();
1210 pedantic_lvalues
= pedantic
;
1213 /* Return a definition for a builtin function named NAME and whose data type
1214 is TYPE. TYPE should be a function type with argument types.
1215 FUNCTION_CODE tells later passes how to compile calls to this function.
1216 See tree.h for its possible values.
1218 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
1219 the name to be called if we can't opencode the function. If
1220 ATTRS is nonzero, use that for the function's attribute list.
1222 copied from gcc/c-decl.c
1226 builtin_function (const char *name
, tree type
, int function_code
,
1227 enum built_in_class
class, const char *library_name
,
1230 tree decl
= build_decl (FUNCTION_DECL
, get_identifier (name
), type
);
1231 DECL_EXTERNAL (decl
) = 1;
1232 TREE_PUBLIC (decl
) = 1;
1234 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier (library_name
));
1236 DECL_BUILT_IN_CLASS (decl
) = class;
1237 DECL_FUNCTION_CODE (decl
) = function_code
;
1239 /* Possibly apply some default attributes to this built-in function. */
1241 decl_attributes (&decl
, attrs
, ATTR_FLAG_BUILT_IN
);
1243 decl_attributes (&decl
, NULL_TREE
, 0);
1248 /* Treelang expand function langhook. */
1251 treelang_expand_function (tree fndecl
)
1253 /* We have nothing special to do while expanding functions for treelang. */
1254 tree_rest_of_compilation (fndecl
);
1257 #include "debug.h" /* for debug_hooks, needed by gt-treelang-treetree.h */
1258 #include "gt-treelang-treetree.h"