Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gcc4 / gcc / treelang / treetree.c
blobb783dedcabec72bc680ff9ed865e5eda20bb305b
1 /* TREELANG Compiler interface to GCC's middle end (treetree.c)
2 Called by the parser.
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
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, 51 Franklin Street, Fifth Floor,
37 Boston, MA 02110-1301, 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
49 at all times. */
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. */
54 /* GCC headers. */
56 #include "config.h"
57 #include "system.h"
58 #include "coretypes.h"
59 #include "tm.h"
60 #include "tree.h"
61 #include "tree-dump.h"
62 #include "tree-iterator.h"
63 #include "tree-gimple.h"
64 #include "function.h"
65 #include "flags.h"
66 #include "output.h"
67 #include "ggc.h"
68 #include "toplev.h"
69 #include "varray.h"
70 #include "langhooks-def.h"
71 #include "langhooks.h"
72 #include "target.h"
74 #include "cgraph.h"
76 #include "treelang.h"
77 #include "treetree.h"
78 #include "opts.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. */
99 union lang_tree_node
100 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE")))
102 union tree_node GTY ((tag ("0"),
103 desc ("tree_node_structure (&%h)")))
104 generic;
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 bool tree_mark_addressable (tree exp);
128 static tree tree_lang_type_for_size (unsigned precision, int unsignedp);
129 static tree tree_lang_type_for_mode (enum machine_mode mode, int unsignedp);
130 static tree tree_lang_unsigned_type (tree type_node);
131 static tree tree_lang_signed_type (tree type_node);
132 static tree tree_lang_signed_or_unsigned_type (int unsignedp, tree type);
134 /* Functions to keep track of the current scope. */
135 static void pushlevel (int ignore);
136 static tree poplevel (int keep, int reverse, int functionbody);
137 static tree pushdecl (tree decl);
138 static tree* getstmtlist (void);
140 /* Langhooks. */
141 static tree builtin_function (const char *name, tree type, int function_code,
142 enum built_in_class class,
143 const char *library_name,
144 tree attrs);
145 extern const struct attribute_spec treelang_attribute_table[];
146 static tree getdecls (void);
147 static int global_bindings_p (void);
148 static void insert_block (tree);
150 static void tree_push_type_decl (tree id, tree type_node);
151 static void treelang_expand_function (tree fndecl);
153 /* The front end language hooks (addresses of code for this front
154 end). These are not really very language-dependent, i.e.
155 treelang, C, Mercury, etc. can all use almost the same definitions. */
157 #undef LANG_HOOKS_MARK_ADDRESSABLE
158 #define LANG_HOOKS_MARK_ADDRESSABLE tree_mark_addressable
159 #undef LANG_HOOKS_SIGNED_TYPE
160 #define LANG_HOOKS_SIGNED_TYPE tree_lang_signed_type
161 #undef LANG_HOOKS_UNSIGNED_TYPE
162 #define LANG_HOOKS_UNSIGNED_TYPE tree_lang_unsigned_type
163 #undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
164 #define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE tree_lang_signed_or_unsigned_type
165 #undef LANG_HOOKS_TYPE_FOR_MODE
166 #define LANG_HOOKS_TYPE_FOR_MODE tree_lang_type_for_mode
167 #undef LANG_HOOKS_TYPE_FOR_SIZE
168 #define LANG_HOOKS_TYPE_FOR_SIZE tree_lang_type_for_size
169 #undef LANG_HOOKS_PARSE_FILE
170 #define LANG_HOOKS_PARSE_FILE treelang_parse_file
171 #undef LANG_HOOKS_ATTRIBUTE_TABLE
172 #define LANG_HOOKS_ATTRIBUTE_TABLE treelang_attribute_table
174 #undef LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION
175 #define LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION treelang_expand_function
177 /* #undef LANG_HOOKS_TYPES_COMPATIBLE_P
178 #define LANG_HOOKS_TYPES_COMPATIBLE_P hook_bool_tree_tree_true
180 /* Hook routines and data unique to treelang. */
182 #undef LANG_HOOKS_INIT
183 #define LANG_HOOKS_INIT treelang_init
184 #undef LANG_HOOKS_NAME
185 #define LANG_HOOKS_NAME "GNU treelang"
186 #undef LANG_HOOKS_FINISH
187 #define LANG_HOOKS_FINISH treelang_finish
188 #undef LANG_HOOKS_INIT_OPTIONS
189 #define LANG_HOOKS_INIT_OPTIONS treelang_init_options
190 #undef LANG_HOOKS_HANDLE_OPTION
191 #define LANG_HOOKS_HANDLE_OPTION treelang_handle_option
192 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
194 /* Tree code type/name/code tables. */
196 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
198 const enum tree_code_class tree_code_type[] = {
199 #include "tree.def"
200 tcc_exceptional
202 #undef DEFTREECODE
204 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
206 const unsigned char tree_code_length[] = {
207 #include "tree.def"
210 #undef DEFTREECODE
212 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
214 const char *const tree_code_name[] = {
215 #include "tree.def"
216 "@@dummy"
218 #undef DEFTREECODE
220 /* Number of bits in int and char - accessed by front end. */
222 unsigned int tree_code_int_size = SIZEOF_INT * HOST_BITS_PER_CHAR;
224 unsigned int tree_code_char_size = HOST_BITS_PER_CHAR;
226 /* Return the tree stuff for this type TYPE_NUM. */
228 tree
229 tree_code_get_type (int type_num)
231 switch (type_num)
233 case SIGNED_CHAR:
234 return signed_char_type_node;
236 case UNSIGNED_CHAR:
237 return unsigned_char_type_node;
239 case SIGNED_INT:
240 return integer_type_node;
242 case UNSIGNED_INT:
243 return unsigned_type_node;
245 case VOID_TYPE:
246 return void_type_node;
248 default:
249 gcc_unreachable ();
253 /* Output the code for the start of an if statement. The test
254 expression is EXP (true if not zero), and the stmt occurred at line
255 LINENO in file FILENAME. */
257 void
258 tree_code_if_start (tree exp, location_t loc)
260 tree cond_exp, cond;
261 cond_exp = fold_build2 (NE_EXPR, boolean_type_node, exp,
262 fold_build1 (CONVERT_EXPR, TREE_TYPE (exp),
263 integer_zero_node));
264 SET_EXPR_LOCATION (cond_exp, loc);
265 cond = build3 (COND_EXPR, void_type_node, cond_exp, NULL_TREE,
266 NULL_TREE);
267 SET_EXPR_LOCATION (cond, loc);
268 append_to_statement_list_force (cond, getstmtlist ());
269 pushlevel (0);
272 /* Output the code for the else of an if statement. The else occurred
273 at line LINENO in file FILENAME. */
275 void
276 tree_code_if_else (location_t loc ATTRIBUTE_UNUSED)
278 tree stmts = *getstmtlist ();
279 tree block = poplevel (1, 0, 0);
280 if (BLOCK_VARS (block))
282 tree bindexpr = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
283 stmts, block);
284 stmts = alloc_stmt_list ();
285 append_to_statement_list (bindexpr, &stmts);
288 TREE_OPERAND (STATEMENT_LIST_TAIL (*getstmtlist ())->stmt, 1) = stmts;
289 pushlevel (0);
292 /* Output the code for the end_if an if statement. The end_if (final brace)
293 occurred at line LINENO in file FILENAME. */
295 void
296 tree_code_if_end (location_t loc ATTRIBUTE_UNUSED)
298 tree stmts = *getstmtlist ();
299 tree block = poplevel (1, 0, 0);
300 if (BLOCK_VARS (block))
302 tree bindexpr = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
303 stmts, block);
304 stmts = alloc_stmt_list ();
305 append_to_statement_list (bindexpr, &stmts);
308 TREE_OPERAND (STATEMENT_LIST_TAIL (*getstmtlist ())->stmt, 2) = stmts;
311 /* Create a function. The prototype name is NAME, storage class is
312 STORAGE_CLASS, type of return variable is RET_TYPE, parameter lists
313 is PARMS, returns decl for this function. */
315 tree
316 tree_code_create_function_prototype (unsigned char* chars,
317 unsigned int storage_class,
318 unsigned int ret_type,
319 struct prod_token_parm_item* parms,
320 location_t loc)
323 tree id;
324 struct prod_token_parm_item* parm;
325 tree type_list = NULL_TREE;
326 tree type_node;
327 tree fn_type;
328 tree fn_decl;
329 tree parm_list = NULL_TREE;
331 /* Build the type. */
332 id = get_identifier ((const char*)chars);
333 for (parm = parms; parm; parm = parm->tp.par.next)
335 gcc_assert (parm->category == parameter_category);
336 type_node = tree_code_get_type (parm->type);
337 type_list = tree_cons (NULL_TREE, type_node, type_list);
339 /* Last parm if void indicates fixed length list (as opposed to
340 printf style va_* list). */
341 type_list = tree_cons (NULL_TREE, void_type_node, type_list);
343 /* The back end needs them in reverse order. */
344 type_list = nreverse (type_list);
346 type_node = tree_code_get_type (ret_type);
347 fn_type = build_function_type (type_node, type_list);
349 id = get_identifier ((const char*)chars);
350 fn_decl = build_decl (FUNCTION_DECL, id, fn_type);
352 /* Nested functions not supported here. */
353 DECL_CONTEXT (fn_decl) = NULL_TREE;
354 DECL_SOURCE_LOCATION (fn_decl) = loc;
356 TREE_PUBLIC (fn_decl) = 0;
357 DECL_EXTERNAL (fn_decl) = 0;
358 TREE_STATIC (fn_decl) = 0;
359 switch (storage_class)
361 case STATIC_STORAGE:
362 break;
364 case EXTERNAL_DEFINITION_STORAGE:
365 TREE_PUBLIC (fn_decl) = 1;
366 break;
368 case EXTERNAL_REFERENCE_STORAGE:
369 DECL_EXTERNAL (fn_decl) = 1;
370 break;
372 case AUTOMATIC_STORAGE:
373 default:
374 gcc_unreachable ();
377 /* Make the argument variable decls. */
378 for (parm = parms; parm; parm = parm->tp.par.next)
380 tree parm_decl = build_decl (PARM_DECL, get_identifier
381 ((const char*) (parm->tp.par.variable_name)),
382 tree_code_get_type (parm->type));
384 /* Some languages have different nominal and real types. */
385 DECL_ARG_TYPE (parm_decl) = TREE_TYPE (parm_decl);
386 gcc_assert (DECL_ARG_TYPE (parm_decl));
387 gcc_assert (fn_decl);
388 DECL_CONTEXT (parm_decl) = fn_decl;
389 DECL_SOURCE_LOCATION (parm_decl) = loc;
390 parm_list = chainon (parm_decl, parm_list);
393 /* Back into reverse order as the back end likes them. */
394 parm_list = nreverse (parm_list);
396 DECL_ARGUMENTS (fn_decl) = parm_list;
398 /* Save the decls for use when the args are referred to. */
399 for (parm = parms; parm_list;
400 parm_list = TREE_CHAIN (parm_list),
401 parm = parm->tp.par.next)
403 gcc_assert (parm); /* Too few. */
404 *parm->tp.par.where_to_put_var_tree = parm_list;
406 gcc_assert (!parm); /* Too many. */
408 /* Process declaration of function defined elsewhere. */
409 rest_of_decl_compilation (fn_decl, 1, 0);
411 return fn_decl;
415 /* Output code for start of function; the decl of the function is in
416 PREV_SAVED (as created by tree_code_create_function_prototype),
417 the function is at line number LINENO in file FILENAME. The
418 parameter details are in the lists PARMS. Returns nothing. */
420 void
421 tree_code_create_function_initial (tree prev_saved,
422 location_t loc)
424 tree fn_decl;
425 tree resultdecl;
427 fn_decl = prev_saved;
428 gcc_assert (fn_decl);
430 /* Output message if not -quiet. */
431 announce_function (fn_decl);
433 /* This has something to do with forcing output also. */
434 pushdecl (fn_decl);
436 /* Set current function for error msgs etc. */
437 current_function_decl = fn_decl;
438 DECL_INITIAL (fn_decl) = error_mark_node;
440 DECL_SOURCE_LOCATION (fn_decl) = loc;
442 /* Create a DECL for the functions result. */
443 resultdecl =
444 build_decl (RESULT_DECL, NULL_TREE, TREE_TYPE (TREE_TYPE (fn_decl)));
445 DECL_CONTEXT (resultdecl) = fn_decl;
446 DECL_ARTIFICIAL (resultdecl) = 1;
447 DECL_IGNORED_P (resultdecl) = 1;
448 DECL_SOURCE_LOCATION (resultdecl) = loc;
449 DECL_RESULT (fn_decl) = resultdecl;
451 /* Create a new level at the start of the function. */
453 pushlevel (0);
455 TREE_STATIC (fn_decl) = 1;
458 /* Wrapup a function contained in file FILENAME, ending at line LINENO. */
459 void
460 tree_code_create_function_wrapup (location_t loc)
462 tree block;
463 tree fn_decl;
464 tree stmts = *getstmtlist ();
466 fn_decl = current_function_decl;
468 /* Pop the level. */
470 block = poplevel (1, 0, 1);
472 /* And attach it to the function. */
474 DECL_SAVED_TREE (fn_decl) = build3 (BIND_EXPR, void_type_node,
475 BLOCK_VARS (block),
476 stmts, block);
478 allocate_struct_function (fn_decl);
479 cfun->function_end_locus = loc;
481 /* Dump the original tree to a file. */
482 dump_function (TDI_original, fn_decl);
484 /* Convert current function to GIMPLE for the middle end. */
485 gimplify_function_tree (fn_decl);
486 dump_function (TDI_generic, fn_decl);
488 /* We are not inside of any scope now. */
489 current_function_decl = NULL_TREE;
490 cfun = NULL;
492 /* Pass the current function off to the middle end. */
493 (void)cgraph_node (fn_decl);
494 cgraph_finalize_function (fn_decl, false);
497 /* Create a variable.
499 The storage class is STORAGE_CLASS (eg LOCAL).
500 The name is CHARS/LENGTH.
501 The type is EXPRESSION_TYPE (eg UNSIGNED_TYPE).
502 The init tree is INIT. */
504 tree
505 tree_code_create_variable (unsigned int storage_class,
506 unsigned char* chars,
507 unsigned int length,
508 unsigned int expression_type,
509 tree init,
510 location_t loc)
512 tree var_type;
513 tree var_id;
514 tree var_decl;
516 /* 1. Build the type. */
517 var_type = tree_code_get_type (expression_type);
519 /* 2. Build the name. */
520 gcc_assert (chars[length] == 0); /* Should be null terminated. */
522 var_id = get_identifier ((const char*)chars);
524 /* 3. Build the decl and set up init. */
525 var_decl = build_decl (VAR_DECL, var_id, var_type);
527 /* 3a. Initialization. */
528 if (init)
529 DECL_INITIAL (var_decl) = fold_build1 (CONVERT_EXPR, var_type, init);
530 else
531 DECL_INITIAL (var_decl) = NULL_TREE;
533 gcc_assert (TYPE_SIZE (var_type) != 0); /* Did not calculate size. */
535 DECL_CONTEXT (var_decl) = current_function_decl;
537 DECL_SOURCE_LOCATION (var_decl) = loc;
539 DECL_EXTERNAL (var_decl) = 0;
540 TREE_PUBLIC (var_decl) = 0;
541 TREE_STATIC (var_decl) = 0;
542 /* Set the storage mode and whether only visible in the same file. */
543 switch (storage_class)
545 case STATIC_STORAGE:
546 TREE_STATIC (var_decl) = 1;
547 break;
549 case AUTOMATIC_STORAGE:
550 break;
552 case EXTERNAL_DEFINITION_STORAGE:
553 TREE_PUBLIC (var_decl) = 1;
554 break;
556 case EXTERNAL_REFERENCE_STORAGE:
557 DECL_EXTERNAL (var_decl) = 1;
558 break;
560 default:
561 gcc_unreachable ();
564 TYPE_NAME (TREE_TYPE (var_decl)) = TYPE_NAME (var_type);
565 return pushdecl (copy_node (var_decl));
569 /* Generate code for return statement. Type is in TYPE, expression
570 is in EXP if present. */
572 void
573 tree_code_generate_return (tree type, tree exp)
575 tree setret;
576 #ifdef ENABLE_CHECKING
577 tree param;
579 for (param = DECL_ARGUMENTS (current_function_decl);
580 param;
581 param = TREE_CHAIN (param))
582 gcc_assert (DECL_CONTEXT (param) == current_function_decl);
583 #endif
585 if (exp && TREE_TYPE (TREE_TYPE (current_function_decl)) != void_type_node)
587 setret = fold_build2 (MODIFY_EXPR, type,
588 DECL_RESULT (current_function_decl),
589 fold_build1 (CONVERT_EXPR, type, exp));
590 TREE_SIDE_EFFECTS (setret) = 1;
591 TREE_USED (setret) = 1;
592 setret = build1 (RETURN_EXPR, type, setret);
593 /* Use EXPR_LOCUS so we don't lose any information about the file we
594 are compiling. */
595 SET_EXPR_LOCUS (setret, EXPR_LOCUS (exp));
597 else
598 setret = build1 (RETURN_EXPR, type, NULL_TREE);
600 append_to_statement_list_force (setret, getstmtlist ());
604 /* Output the code for this expression statement CODE. */
606 void
607 tree_code_output_expression_statement (tree code, location_t loc)
609 /* Output the line number information. */
610 SET_EXPR_LOCATION (code, loc);
611 TREE_USED (code) = 1;
612 TREE_SIDE_EFFECTS (code) = 1;
613 /* put CODE into the code list. */
614 append_to_statement_list_force (code, getstmtlist ());
617 /* Return a tree for a constant integer value in the token TOK. No
618 size checking is done. */
620 tree
621 tree_code_get_integer_value (unsigned char* chars, unsigned int length)
623 long long int val = 0;
624 unsigned int ix;
625 unsigned int start = 0;
626 int negative = 1;
627 switch (chars[0])
629 case (unsigned char)'-':
630 negative = -1;
631 start = 1;
632 break;
634 case (unsigned char)'+':
635 start = 1;
636 break;
638 default:
639 break;
641 for (ix = start; ix < length; ix++)
642 val = val * 10 + chars[ix] - (unsigned char)'0';
643 val = val*negative;
644 return build_int_cst_wide (start == 1 ?
645 integer_type_node : unsigned_type_node,
646 val & 0xffffffff, (val >> 32) & 0xffffffff);
649 /* Return the tree for an expression, type EXP_TYPE (see treetree.h)
650 with tree type TYPE and with operands1 OP1, OP2 (maybe), OP3 (maybe). */
651 tree
652 tree_code_get_expression (unsigned int exp_type,
653 tree type, tree op1, tree op2,
654 tree op3 ATTRIBUTE_UNUSED,
655 location_t loc)
657 tree ret1;
658 int operator;
660 switch (exp_type)
662 case EXP_ASSIGN:
663 gcc_assert (op1 && op2);
664 operator = MODIFY_EXPR;
665 ret1 = fold_build2 (operator, void_type_node, op1,
666 fold_build1 (CONVERT_EXPR, TREE_TYPE (op1), op2));
668 break;
670 case EXP_PLUS:
671 operator = PLUS_EXPR;
672 goto binary_expression;
674 case EXP_MINUS:
675 operator = MINUS_EXPR;
676 goto binary_expression;
678 case EXP_EQUALS:
679 operator = EQ_EXPR;
680 goto binary_expression;
682 /* Expand a binary expression. Ensure the operands are the right type. */
683 binary_expression:
684 gcc_assert (op1 && op2);
685 ret1 = fold_build2 (operator, type,
686 fold_build1 (CONVERT_EXPR, type, op1),
687 fold_build1 (CONVERT_EXPR, type, op2));
688 break;
690 /* Reference to a variable. This is dead easy, just return the
691 decl for the variable. If the TYPE is different than the
692 variable type, convert it. However, to keep accurate location
693 information we wrap it in a NOP_EXPR is is easily stripped. */
694 case EXP_REFERENCE:
695 gcc_assert (op1);
696 TREE_USED (op1) = 1;
697 if (type == TREE_TYPE (op1))
698 ret1 = build1 (NOP_EXPR, type, op1);
699 else
700 ret1 = fold_build1 (CONVERT_EXPR, type, op1);
701 break;
703 case EXP_FUNCTION_INVOCATION:
704 gcc_assert (op1);
705 gcc_assert(TREE_TYPE (TREE_TYPE (op1)) == type);
706 TREE_USED (op1) = 1;
707 ret1 = build_function_call_expr(op1, op2);
708 break;
710 default:
711 gcc_unreachable ();
714 /* Declarations already have a location and constants can be shared so they
715 shouldn't a location set on them. */
716 if (! DECL_P (ret1) && ! TREE_CONSTANT (ret1))
717 SET_EXPR_LOCATION (ret1, loc);
718 return ret1;
721 /* Init parameter list and return empty list. */
723 tree
724 tree_code_init_parameters (void)
726 return NULL_TREE;
729 /* Add a parameter EXP whose expression type is EXP_PROTO to list
730 LIST, returning the new list. */
732 tree
733 tree_code_add_parameter (tree list, tree proto_exp, tree exp)
735 tree new_exp;
736 new_exp = tree_cons (NULL_TREE,
737 fold_build1 (CONVERT_EXPR, TREE_TYPE (proto_exp),
738 exp), NULL_TREE);
739 if (!list)
740 return new_exp;
741 return chainon (new_exp, list);
744 /* Get a stringpool entry for a string S of length L. This is needed
745 because the GTY routines don't mark strings, forcing you to put
746 them into stringpool, which is never freed. */
748 const char*
749 get_string (const char *s, size_t l)
751 tree t;
752 t = get_identifier_with_length (s, l);
753 return IDENTIFIER_POINTER(t);
756 /* Save typing debug_tree all the time. Dump a tree T pretty and
757 concise. */
759 void dt (tree t);
761 void
762 dt (tree t)
764 debug_tree (t);
767 /* Routines Expected by gcc: */
769 /* These are used to build types for various sizes. The code below
770 is a simplified version of that of GNAT. */
772 #ifndef MAX_BITS_PER_WORD
773 #define MAX_BITS_PER_WORD BITS_PER_WORD
774 #endif
776 /* This variable keeps a table for types for each precision so that we only
777 allocate each of them once. Signed and unsigned types are kept separate. */
778 static GTY(()) tree signed_and_unsigned_types[MAX_BITS_PER_WORD + 1][2];
780 /* Mark EXP saying that we need to be able to take the
781 address of it; it should not be allocated in a register.
782 Value is 1 if successful.
784 This implementation was copied from c-decl.c. */
786 static bool
787 tree_mark_addressable (tree exp)
789 register tree x = exp;
790 while (1)
791 switch (TREE_CODE (x))
793 case COMPONENT_REF:
794 case ADDR_EXPR:
795 case ARRAY_REF:
796 case REALPART_EXPR:
797 case IMAGPART_EXPR:
798 x = TREE_OPERAND (x, 0);
799 break;
801 case CONSTRUCTOR:
802 TREE_ADDRESSABLE (x) = 1;
803 return 1;
805 case VAR_DECL:
806 case CONST_DECL:
807 case PARM_DECL:
808 case RESULT_DECL:
809 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
810 && DECL_NONLOCAL (x))
812 if (TREE_PUBLIC (x))
814 error ("Global register variable %qD used in nested function.",
816 return 0;
818 pedwarn ("Register variable %qD used in nested function.", x);
820 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
822 if (TREE_PUBLIC (x))
824 error ("Address of global register variable %qD requested.",
826 return 0;
829 pedwarn ("Address of register variable %qD requested.", x);
832 /* drops in */
833 case FUNCTION_DECL:
834 TREE_ADDRESSABLE (x) = 1;
836 default:
837 return 1;
841 /* Return an integer type with the number of bits of precision given by
842 PRECISION. UNSIGNEDP is nonzero if the type is unsigned; otherwise
843 it is a signed type. */
845 static tree
846 tree_lang_type_for_size (unsigned precision, int unsignedp)
848 tree t;
850 if (precision <= MAX_BITS_PER_WORD
851 && signed_and_unsigned_types[precision][unsignedp] != 0)
852 return signed_and_unsigned_types[precision][unsignedp];
854 if (unsignedp)
855 t = signed_and_unsigned_types[precision][1]
856 = make_unsigned_type (precision);
857 else
858 t = signed_and_unsigned_types[precision][0]
859 = make_signed_type (precision);
861 return t;
864 /* Return a data type that has machine mode MODE. UNSIGNEDP selects
865 an unsigned type; otherwise a signed type is returned. */
867 static tree
868 tree_lang_type_for_mode (enum machine_mode mode, int unsignedp)
870 if (SCALAR_INT_MODE_P (mode))
871 return tree_lang_type_for_size (GET_MODE_BITSIZE (mode), unsignedp);
872 else
873 return NULL_TREE;
876 /* Return the unsigned version of a TYPE_NODE, a scalar type. */
878 static tree
879 tree_lang_unsigned_type (tree type_node)
881 return tree_lang_type_for_size (TYPE_PRECISION (type_node), 1);
884 /* Return the signed version of a TYPE_NODE, a scalar type. */
886 static tree
887 tree_lang_signed_type (tree type_node)
889 return tree_lang_type_for_size (TYPE_PRECISION (type_node), 0);
892 /* Return a type the same as TYPE except unsigned or signed according to
893 UNSIGNEDP. */
895 static tree
896 tree_lang_signed_or_unsigned_type (int unsignedp, tree type)
898 if (! INTEGRAL_TYPE_P (type) || TYPE_UNSIGNED (type) == unsignedp)
899 return type;
900 else
901 return tree_lang_type_for_size (TYPE_PRECISION (type), unsignedp);
904 /* These functions and variables deal with binding contours. We only
905 need these functions for the list of PARM_DECLs, but we leave the
906 functions more general; these are a simplified version of the
907 functions from GNAT. */
909 /* For each binding contour we allocate a binding_level structure which records
910 the entities defined or declared in that contour. Contours include:
912 the global one
913 one for each subprogram definition
914 one for each compound statement (declare block)
916 Binding contours are used to create GCC tree BLOCK nodes. */
918 struct binding_level
920 /* A chain of ..._DECL nodes for all variables, constants, functions,
921 parameters and type declarations. These ..._DECL nodes are chained
922 through the TREE_CHAIN field. Note that these ..._DECL nodes are stored
923 in the reverse of the order supplied to be compatible with the
924 back-end. */
925 tree names;
926 /* For each level (except the global one), a chain of BLOCK nodes for all
927 the levels that were entered and exited one level down from this one. */
928 tree blocks;
930 tree stmts;
931 /* The binding level containing this one (the enclosing binding level). */
932 struct binding_level *level_chain;
935 /* The binding level currently in effect. */
936 static struct binding_level *current_binding_level = NULL;
938 /* The outermost binding level. This binding level is created when the
939 compiler is started and it will exist through the entire compilation. */
940 static struct binding_level *global_binding_level;
942 /* Binding level structures are initialized by copying this one. */
943 static struct binding_level clear_binding_level = {NULL, NULL, NULL, NULL };
945 /* Return non-zero if we are currently in the global binding level. */
947 static int
948 global_bindings_p (void)
950 return current_binding_level == global_binding_level ? -1 : 0;
954 /* Return the list of declarations in the current level. Note that this list
955 is in reverse order (it has to be so for back-end compatibility). */
957 static tree
958 getdecls (void)
960 return current_binding_level->names;
963 /* Return a STATMENT_LIST for the current block. */
965 static tree*
966 getstmtlist (void)
968 return &current_binding_level->stmts;
971 /* Enter a new binding level. The input parameter is ignored, but has to be
972 specified for back-end compatibility. */
974 static void
975 pushlevel (int ignore ATTRIBUTE_UNUSED)
977 struct binding_level *newlevel = xmalloc (sizeof (struct binding_level));
979 *newlevel = clear_binding_level;
981 /* Add this level to the front of the chain (stack) of levels that are
982 active. */
983 newlevel->level_chain = current_binding_level;
984 current_binding_level = newlevel;
985 current_binding_level->stmts = alloc_stmt_list ();
988 /* Exit a binding level.
989 Pop the level off, and restore the state of the identifier-decl mappings
990 that were in effect when this level was entered.
992 If KEEP is nonzero, this level had explicit declarations, so
993 and create a "block" (a BLOCK node) for the level
994 to record its declarations and subblocks for symbol table output.
996 If FUNCTIONBODY is nonzero, this level is the body of a function,
997 so create a block as if KEEP were set and also clear out all
998 label names.
1000 If REVERSE is nonzero, reverse the order of decls before putting
1001 them into the BLOCK. */
1003 static tree
1004 poplevel (int keep, int reverse, int functionbody)
1006 /* Points to a BLOCK tree node. This is the BLOCK node constructed for the
1007 binding level that we are about to exit and which is returned by this
1008 routine. */
1009 tree block_node = NULL_TREE;
1010 tree decl_chain;
1011 tree subblock_chain = current_binding_level->blocks;
1012 tree subblock_node;
1014 /* Reverse the list of *_DECL nodes if desired. Note that the ..._DECL
1015 nodes chained through the `names' field of current_binding_level are in
1016 reverse order except for PARM_DECL node, which are explicitly stored in
1017 the right order. */
1018 decl_chain = (reverse) ? nreverse (current_binding_level->names)
1019 : current_binding_level->names;
1021 /* If there were any declarations in the current binding level, or if this
1022 binding level is a function body, or if there are any nested blocks then
1023 create a BLOCK node to record them for the life of this function. */
1024 if (keep || functionbody)
1025 block_node = build_block (keep ? decl_chain : 0, subblock_chain, 0, 0);
1027 /* Record the BLOCK node just built as the subblock its enclosing scope. */
1028 for (subblock_node = subblock_chain; subblock_node;
1029 subblock_node = TREE_CHAIN (subblock_node))
1030 BLOCK_SUPERCONTEXT (subblock_node) = block_node;
1032 /* Clear out the meanings of the local variables of this level. */
1034 for (subblock_node = decl_chain; subblock_node;
1035 subblock_node = TREE_CHAIN (subblock_node))
1036 if (DECL_NAME (subblock_node) != 0)
1037 /* If the identifier was used or addressed via a local extern decl,
1038 don't forget that fact. */
1039 if (DECL_EXTERNAL (subblock_node))
1041 if (TREE_USED (subblock_node))
1042 TREE_USED (DECL_NAME (subblock_node)) = 1;
1045 /* Pop the current level. */
1046 current_binding_level = current_binding_level->level_chain;
1048 if (functionbody)
1050 /* This is the top level block of a function. */
1051 DECL_INITIAL (current_function_decl) = block_node;
1053 else if (block_node)
1055 current_binding_level->blocks
1056 = chainon (current_binding_level->blocks, block_node);
1059 /* If we did not make a block for the level just exited, any blocks made for
1060 inner levels (since they cannot be recorded as subblocks in that level)
1061 must be carried forward so they will later become subblocks of something
1062 else. */
1063 else if (subblock_chain)
1064 current_binding_level->blocks
1065 = chainon (current_binding_level->blocks, subblock_chain);
1066 if (block_node)
1067 TREE_USED (block_node) = 1;
1069 return block_node;
1072 /* Insert BLOCK at the end of the list of subblocks of the
1073 current binding level. This is used when a BIND_EXPR is expanded,
1074 to handle the BLOCK node inside the BIND_EXPR. */
1076 static void
1077 insert_block (tree block)
1079 TREE_USED (block) = 1;
1080 current_binding_level->blocks
1081 = chainon (current_binding_level->blocks, block);
1085 /* Records a ..._DECL node DECL as belonging to the current lexical scope.
1086 Returns the ..._DECL node. */
1088 tree
1089 pushdecl (tree decl)
1091 /* External objects aren't nested, other objects may be. */
1093 if ((DECL_EXTERNAL (decl)) || (decl==current_function_decl))
1094 DECL_CONTEXT (decl) = 0;
1095 else
1096 DECL_CONTEXT (decl) = current_function_decl;
1098 /* Put the declaration on the list. The list of declarations is in reverse
1099 order. The list will be reversed later if necessary. This needs to be
1100 this way for compatibility with the back-end. */
1102 TREE_CHAIN (decl) = current_binding_level->names;
1103 current_binding_level->names = decl;
1105 /* For the declaration of a type, set its name if it is not already set. */
1107 if (TREE_CODE (decl) == TYPE_DECL
1108 && TYPE_NAME (TREE_TYPE (decl)) == 0)
1109 TYPE_NAME (TREE_TYPE (decl)) = DECL_NAME (decl);
1111 /* Put automatic variables into the intermediate representation. */
1112 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
1113 && !TREE_STATIC (decl) && !TREE_PUBLIC (decl))
1114 tree_code_output_expression_statement (build1 (DECL_EXPR, void_type_node,
1115 decl),
1116 DECL_SOURCE_LOCATION (decl));
1117 return decl;
1121 static void
1122 tree_push_type_decl(tree id, tree type_node)
1124 tree decl = build_decl (TYPE_DECL, id, type_node);
1125 TYPE_NAME (type_node) = id;
1126 pushdecl (decl);
1129 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
1131 /* Create the predefined scalar types of C,
1132 and some nodes representing standard constants (0, 1, (void *) 0).
1133 Initialize the global binding level.
1134 Make definitions for built-in primitive functions. */
1136 void
1137 treelang_init_decl_processing (void)
1139 current_function_decl = NULL;
1140 current_binding_level = NULL_BINDING_LEVEL;
1141 pushlevel (0); /* make the binding_level structure for global names */
1142 global_binding_level = current_binding_level;
1144 build_common_tree_nodes (flag_signed_char, false);
1146 /* set standard type names */
1148 /* Define `int' and `char' last so that they are not overwritten. */
1149 tree_push_type_decl (NULL_TREE, intQI_type_node);
1150 tree_push_type_decl (NULL_TREE, intHI_type_node);
1151 tree_push_type_decl (NULL_TREE, intSI_type_node);
1152 tree_push_type_decl (NULL_TREE, intDI_type_node);
1153 #if HOST_BITS_PER_WIDE_INT >= 64
1154 tree_push_type_decl (NULL_TREE, intTI_type_node);
1155 #endif
1156 tree_push_type_decl (NULL_TREE, unsigned_intQI_type_node);
1157 tree_push_type_decl (NULL_TREE, unsigned_intHI_type_node);
1158 tree_push_type_decl (NULL_TREE, unsigned_intSI_type_node);
1159 tree_push_type_decl (NULL_TREE, unsigned_intDI_type_node);
1160 #if HOST_BITS_PER_WIDE_INT >= 64
1161 tree_push_type_decl (NULL_TREE, unsigned_intTI_type_node);
1162 #endif
1164 tree_push_type_decl (get_identifier ("int"), integer_type_node);
1165 tree_push_type_decl (get_identifier ("char"), char_type_node);
1166 tree_push_type_decl (get_identifier ("long int"),
1167 long_integer_type_node);
1168 tree_push_type_decl (get_identifier ("unsigned int"),
1169 unsigned_type_node);
1170 tree_push_type_decl (get_identifier ("long unsigned int"),
1171 long_unsigned_type_node);
1172 tree_push_type_decl (get_identifier ("long long int"),
1173 long_long_integer_type_node);
1174 tree_push_type_decl (get_identifier ("long long unsigned int"),
1175 long_long_unsigned_type_node);
1176 tree_push_type_decl (get_identifier ("short int"),
1177 short_integer_type_node);
1178 tree_push_type_decl (get_identifier ("short unsigned int"),
1179 short_unsigned_type_node);
1180 tree_push_type_decl (get_identifier ("signed char"),
1181 signed_char_type_node);
1182 tree_push_type_decl (get_identifier ("unsigned char"),
1183 unsigned_char_type_node);
1184 size_type_node = make_unsigned_type (POINTER_SIZE);
1185 tree_push_type_decl (get_identifier ("size_t"), size_type_node);
1186 set_sizetype (size_type_node);
1188 build_common_tree_nodes_2 (/* short_double= */ 0);
1190 tree_push_type_decl (get_identifier ("float"), float_type_node);
1191 tree_push_type_decl (get_identifier ("double"), double_type_node);
1192 tree_push_type_decl (get_identifier ("long double"), long_double_type_node);
1193 tree_push_type_decl (get_identifier ("void"), void_type_node);
1195 build_common_builtin_nodes ();
1196 (*targetm.init_builtins) ();
1198 pedantic_lvalues = pedantic;
1201 static tree
1202 handle_attribute (tree *node, tree name, tree ARG_UNUSED (args),
1203 int ARG_UNUSED (flags), bool *no_add_attrs)
1205 if (TREE_CODE (*node) == FUNCTION_DECL)
1207 if (strcmp (IDENTIFIER_POINTER (name), "const") == 0)
1208 TREE_READONLY (*node) = 1;
1209 if (strcmp (IDENTIFIER_POINTER (name), "nothrow") == 0)
1210 TREE_NOTHROW (*node) = 1;
1212 else
1214 warning (OPT_Wattributes, "%qD attribute ignored", name);
1215 *no_add_attrs = true;
1218 return NULL_TREE;
1221 const struct attribute_spec treelang_attribute_table[] =
1223 { "const", 0, 0, true, false, false, handle_attribute },
1224 { "nothrow", 0, 0, true, false, false, handle_attribute },
1225 { NULL, 0, 0, false, false, false, NULL },
1228 /* Return a definition for a builtin function named NAME and whose data type
1229 is TYPE. TYPE should be a function type with argument types.
1230 FUNCTION_CODE tells later passes how to compile calls to this function.
1231 See tree.h for its possible values.
1233 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
1234 the name to be called if we can't opencode the function. If
1235 ATTRS is nonzero, use that for the function's attribute list.
1237 copied from gcc/c-decl.c
1240 static tree
1241 builtin_function (const char *name, tree type, int function_code,
1242 enum built_in_class class, const char *library_name,
1243 tree attrs)
1245 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
1246 DECL_EXTERNAL (decl) = 1;
1247 TREE_PUBLIC (decl) = 1;
1248 if (library_name)
1249 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
1250 pushdecl (decl);
1251 DECL_BUILT_IN_CLASS (decl) = class;
1252 DECL_FUNCTION_CODE (decl) = function_code;
1254 /* Possibly apply some default attributes to this built-in function. */
1255 if (attrs)
1256 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
1257 else
1258 decl_attributes (&decl, NULL_TREE, 0);
1260 return decl;
1263 /* Treelang expand function langhook. */
1265 static void
1266 treelang_expand_function (tree fndecl)
1268 /* We have nothing special to do while expanding functions for treelang. */
1269 tree_rest_of_compilation (fndecl);
1272 #include "debug.h" /* for debug_hooks, needed by gt-treelang-treetree.h */
1273 #include "gt-treelang-treetree.h"