langhooks.h (truthvalue_conversion): Remove.
[gcc.git] / gcc / treelang / treetree.c
1 /* TREELANG Compiler interface to GCC's middle end (treetree.c)
2 Called by the parser.
3
4 If you want a working example of how to write a front end to GCC,
5 you are in the right place.
6
7 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
8 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
9
10 This code is based on toy.c written by Richard Kenner.
11
12 It was later modified by Jonathan Bartlett whose changes have all
13 been removed (by Tim Josling).
14
15 Various bits and pieces were cloned from the GCC main tree, as
16 GCC evolved, for COBOLForGCC, by Tim Josling.
17
18 It was adapted to TREELANG by Tim Josling 2001.
19
20 Updated to function-at-a-time by James A. Morrison, 2004.
21
22 -----------------------------------------------------------------------
23
24 This program is free software; you can redistribute it and/or modify it
25 under the terms of the GNU General Public License as published by the
26 Free Software Foundation; either version 2, or (at your option) any
27 later version.
28
29 This program is distributed in the hope that it will be useful,
30 but WITHOUT ANY WARRANTY; without even the implied warranty of
31 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 GNU General Public License for more details.
33
34 You should have received a copy of the GNU General Public License
35 along with this program; if not, write to the Free Software
36 Foundation, 59 Temple Place - Suite 330,
37 Boston, MA 02111-1307, USA.
38
39 In other words, you are welcome to use, share and improve this program.
40 You are forbidden to forbid anyone else to use, share and improve
41 what you give them. Help stamp out software-hoarding!
42
43 ----------------------------------------------------------------------- */
44
45 /* 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. */
50
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. */
53
54 /* GCC headers. */
55
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"
73
74 #include "cgraph.h"
75
76 #include "treelang.h"
77 #include "treetree.h"
78 #include "opts.h"
79
80 extern int option_main;
81 extern char **file_names;
82
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. */
89
90 /* Language-specific identifier information. */
91
92 struct lang_identifier GTY(())
93 {
94 struct tree_identifier common;
95 };
96
97 /* Language-specific tree node information. */
98
99 union lang_tree_node
100 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE")))
101 {
102 union tree_node GTY ((tag ("0"),
103 desc ("tree_node_structure (&%h)")))
104 generic;
105 struct lang_identifier GTY ((tag ("1"))) identifier;
106 };
107
108 /* Language-specific type information. */
109
110 struct lang_type GTY(())
111 {
112 char junk; /* dummy field to ensure struct is not empty */
113 };
114
115 /* Language-specific declaration information. */
116
117 struct lang_decl GTY(())
118 {
119 char junk; /* dummy field to ensure struct is not empty */
120 };
121
122 struct language_function GTY(())
123 {
124 char junk; /* dummy field to ensure struct is not empty */
125 };
126
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);
133
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);
139
140 /* Langhooks. */
141 static tree builtin_function (const char *name, tree type, int function_code,
142 enum built_in_class class, const char *library_name,
143 tree attrs);
144 static tree getdecls (void);
145 static int global_bindings_p (void);
146 static void insert_block (tree);
147
148 static void tree_push_type_decl (tree id, tree type_node);
149 static void treelang_expand_function (tree fndecl);
150
151 /* The front end language hooks (addresses of code for this front
152 end). These are not really very language-dependent, i.e.
153 treelang, C, Mercury, etc. can all use almost the same definitions. */
154
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
169
170 #undef LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION
171 #define LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION treelang_expand_function
172
173 /* #undef LANG_HOOKS_TYPES_COMPATIBLE_P
174 #define LANG_HOOKS_TYPES_COMPATIBLE_P hook_bool_tree_tree_true
175 */
176 /* Hook routines and data unique to treelang. */
177
178 #undef LANG_HOOKS_INIT
179 #define LANG_HOOKS_INIT treelang_init
180 #undef LANG_HOOKS_NAME
181 #define LANG_HOOKS_NAME "GNU treelang"
182 #undef LANG_HOOKS_FINISH
183 #define LANG_HOOKS_FINISH treelang_finish
184 #undef LANG_HOOKS_INIT_OPTIONS
185 #define LANG_HOOKS_INIT_OPTIONS treelang_init_options
186 #undef LANG_HOOKS_HANDLE_OPTION
187 #define LANG_HOOKS_HANDLE_OPTION treelang_handle_option
188 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
189
190 /* Tree code type/name/code tables. */
191
192 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
193
194 const enum tree_code_class tree_code_type[] = {
195 #include "tree.def"
196 tcc_exceptional
197 };
198 #undef DEFTREECODE
199
200 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
201
202 const unsigned char tree_code_length[] = {
203 #include "tree.def"
204 0
205 };
206 #undef DEFTREECODE
207
208 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
209
210 const char *const tree_code_name[] = {
211 #include "tree.def"
212 "@@dummy"
213 };
214 #undef DEFTREECODE
215
216 /* Number of bits in int and char - accessed by front end. */
217
218 unsigned int tree_code_int_size = SIZEOF_INT * HOST_BITS_PER_CHAR;
219
220 unsigned int tree_code_char_size = HOST_BITS_PER_CHAR;
221
222 /* Return the tree stuff for this type TYPE_NUM. */
223
224 tree
225 tree_code_get_type (int type_num)
226 {
227 switch (type_num)
228 {
229 case SIGNED_CHAR:
230 return signed_char_type_node;
231
232 case UNSIGNED_CHAR:
233 return unsigned_char_type_node;
234
235 case SIGNED_INT:
236 return integer_type_node;
237
238 case UNSIGNED_INT:
239 return unsigned_type_node;
240
241 case VOID_TYPE:
242 return void_type_node;
243
244 default:
245 gcc_unreachable ();
246 }
247 }
248
249 /* Output the code for the start of an if statement. The test
250 expression is EXP (true if not zero), and the stmt occurred at line
251 LINENO in file FILENAME. */
252
253 void
254 tree_code_if_start (tree exp, location_t loc)
255 {
256 tree cond_exp, cond;
257 cond_exp = fold_build2 (NE_EXPR, boolean_type_node, exp,
258 fold_build1 (CONVERT_EXPR, TREE_TYPE (exp),
259 integer_zero_node));
260 SET_EXPR_LOCATION (cond_exp, loc);
261 cond = build3 (COND_EXPR, void_type_node, cond_exp, NULL_TREE,
262 NULL_TREE);
263 SET_EXPR_LOCATION (cond, loc);
264 append_to_statement_list_force (cond, getstmtlist ());
265 pushlevel (0);
266 }
267
268 /* Output the code for the else of an if statement. The else occurred
269 at line LINENO in file FILENAME. */
270
271 void
272 tree_code_if_else (location_t loc ATTRIBUTE_UNUSED)
273 {
274 tree stmts = *getstmtlist ();
275 tree block = poplevel (1, 0, 0);
276 if (BLOCK_VARS (block))
277 {
278 tree bindexpr = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
279 stmts, block);
280 stmts = alloc_stmt_list ();
281 append_to_statement_list (bindexpr, &stmts);
282 }
283
284 TREE_OPERAND (STATEMENT_LIST_TAIL (*getstmtlist ())->stmt, 1) = stmts;
285 pushlevel (0);
286 }
287
288 /* Output the code for the end_if an if statement. The end_if (final brace)
289 occurred at line LINENO in file FILENAME. */
290
291 void
292 tree_code_if_end (location_t loc ATTRIBUTE_UNUSED)
293 {
294 tree stmts = *getstmtlist ();
295 tree block = poplevel (1, 0, 0);
296 if (BLOCK_VARS (block))
297 {
298 tree bindexpr = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
299 stmts, block);
300 stmts = alloc_stmt_list ();
301 append_to_statement_list (bindexpr, &stmts);
302 }
303
304 TREE_OPERAND (STATEMENT_LIST_TAIL (*getstmtlist ())->stmt, 2) = stmts;
305 }
306
307 /* Create a function. The prototype name is NAME, storage class is
308 STORAGE_CLASS, type of return variable is RET_TYPE, parameter lists
309 is PARMS, returns decl for this function. */
310
311 tree
312 tree_code_create_function_prototype (unsigned char* chars,
313 unsigned int storage_class,
314 unsigned int ret_type,
315 struct prod_token_parm_item* parms,
316 location_t loc)
317 {
318
319 tree id;
320 struct prod_token_parm_item* parm;
321 tree type_list = NULL_TREE;
322 tree type_node;
323 tree fn_type;
324 tree fn_decl;
325 tree parm_list = NULL_TREE;
326
327 /* Build the type. */
328 id = get_identifier ((const char*)chars);
329 for (parm = parms; parm; parm = parm->tp.par.next)
330 {
331 gcc_assert (parm->category == parameter_category);
332 type_node = tree_code_get_type (parm->type);
333 type_list = tree_cons (NULL_TREE, type_node, type_list);
334 }
335 /* Last parm if void indicates fixed length list (as opposed to
336 printf style va_* list). */
337 type_list = tree_cons (NULL_TREE, void_type_node, type_list);
338
339 /* The back end needs them in reverse order. */
340 type_list = nreverse (type_list);
341
342 type_node = tree_code_get_type (ret_type);
343 fn_type = build_function_type (type_node, type_list);
344
345 id = get_identifier ((const char*)chars);
346 fn_decl = build_decl (FUNCTION_DECL, id, fn_type);
347
348 /* Nested functions not supported here. */
349 DECL_CONTEXT (fn_decl) = NULL_TREE;
350 DECL_SOURCE_LOCATION (fn_decl) = loc;
351
352 TREE_PUBLIC (fn_decl) = 0;
353 DECL_EXTERNAL (fn_decl) = 0;
354 TREE_STATIC (fn_decl) = 0;
355 switch (storage_class)
356 {
357 case STATIC_STORAGE:
358 break;
359
360 case EXTERNAL_DEFINITION_STORAGE:
361 TREE_PUBLIC (fn_decl) = 1;
362 break;
363
364 case EXTERNAL_REFERENCE_STORAGE:
365 DECL_EXTERNAL (fn_decl) = 1;
366 break;
367
368 case AUTOMATIC_STORAGE:
369 default:
370 gcc_unreachable ();
371 }
372
373 /* Make the argument variable decls. */
374 for (parm = parms; parm; parm = parm->tp.par.next)
375 {
376 tree parm_decl = build_decl (PARM_DECL, get_identifier
377 ((const char*) (parm->tp.par.variable_name)),
378 tree_code_get_type (parm->type));
379
380 /* Some languages have different nominal and real types. */
381 DECL_ARG_TYPE (parm_decl) = TREE_TYPE (parm_decl);
382 gcc_assert (DECL_ARG_TYPE (parm_decl));
383 gcc_assert (fn_decl);
384 DECL_CONTEXT (parm_decl) = fn_decl;
385 DECL_SOURCE_LOCATION (parm_decl) = loc;
386 parm_list = chainon (parm_decl, parm_list);
387 }
388
389 /* Back into reverse order as the back end likes them. */
390 parm_list = nreverse (parm_list);
391
392 DECL_ARGUMENTS (fn_decl) = parm_list;
393
394 /* Save the decls for use when the args are referred to. */
395 for (parm = parms; parm_list;
396 parm_list = TREE_CHAIN (parm_list),
397 parm = parm->tp.par.next)
398 {
399 gcc_assert (parm); /* Too few. */
400 *parm->tp.par.where_to_put_var_tree = parm_list;
401 }
402 gcc_assert (!parm); /* Too many. */
403
404 /* Process declaration of function defined elsewhere. */
405 rest_of_decl_compilation (fn_decl, 1, 0);
406
407 return fn_decl;
408 }
409
410
411 /* Output code for start of function; the decl of the function is in
412 PREV_SAVED (as created by tree_code_create_function_prototype),
413 the function is at line number LINENO in file FILENAME. The
414 parameter details are in the lists PARMS. Returns nothing. */
415
416 void
417 tree_code_create_function_initial (tree prev_saved,
418 location_t loc)
419 {
420 tree fn_decl;
421 tree resultdecl;
422
423 fn_decl = prev_saved;
424 gcc_assert (fn_decl);
425
426 /* Output message if not -quiet. */
427 announce_function (fn_decl);
428
429 /* This has something to do with forcing output also. */
430 pushdecl (fn_decl);
431
432 /* Set current function for error msgs etc. */
433 current_function_decl = fn_decl;
434 DECL_INITIAL (fn_decl) = error_mark_node;
435
436 DECL_SOURCE_LOCATION (fn_decl) = loc;
437
438 /* Create a DECL for the functions result. */
439 resultdecl =
440 build_decl (RESULT_DECL, NULL_TREE, TREE_TYPE (TREE_TYPE (fn_decl)));
441 DECL_CONTEXT (resultdecl) = fn_decl;
442 DECL_ARTIFICIAL (resultdecl) = 1;
443 DECL_IGNORED_P (resultdecl) = 1;
444 DECL_SOURCE_LOCATION (resultdecl) = loc;
445 DECL_RESULT (fn_decl) = resultdecl;
446
447 /* Create a new level at the start of the function. */
448
449 pushlevel (0);
450
451 TREE_STATIC (fn_decl) = 1;
452 }
453
454 /* Wrapup a function contained in file FILENAME, ending at line LINENO. */
455 void
456 tree_code_create_function_wrapup (location_t loc)
457 {
458 tree block;
459 tree fn_decl;
460 tree stmts = *getstmtlist ();
461
462 fn_decl = current_function_decl;
463
464 /* Pop the level. */
465
466 block = poplevel (1, 0, 1);
467
468 /* And attach it to the function. */
469
470 DECL_SAVED_TREE (fn_decl) = build3 (BIND_EXPR, void_type_node,
471 BLOCK_VARS (block),
472 stmts, block);
473
474 allocate_struct_function (fn_decl);
475 cfun->function_end_locus = loc;
476
477 /* Dump the original tree to a file. */
478 dump_function (TDI_original, fn_decl);
479
480 /* Convert current function to GIMPLE for the middle end. */
481 gimplify_function_tree (fn_decl);
482 dump_function (TDI_generic, fn_decl);
483
484 /* We are not inside of any scope now. */
485 current_function_decl = NULL_TREE;
486 cfun = NULL;
487
488 /* Pass the current function off to the middle end. */
489 (void)cgraph_node (fn_decl);
490 cgraph_finalize_function (fn_decl, false);
491 }
492
493 /* Create a variable.
494
495 The storage class is STORAGE_CLASS (eg LOCAL).
496 The name is CHARS/LENGTH.
497 The type is EXPRESSION_TYPE (eg UNSIGNED_TYPE).
498 The init tree is INIT. */
499
500 tree
501 tree_code_create_variable (unsigned int storage_class,
502 unsigned char* chars,
503 unsigned int length,
504 unsigned int expression_type,
505 tree init,
506 location_t loc)
507 {
508 tree var_type;
509 tree var_id;
510 tree var_decl;
511
512 /* 1. Build the type. */
513 var_type = tree_code_get_type (expression_type);
514
515 /* 2. Build the name. */
516 gcc_assert (chars[length] == 0); /* Should be null terminated. */
517
518 var_id = get_identifier ((const char*)chars);
519
520 /* 3. Build the decl and set up init. */
521 var_decl = build_decl (VAR_DECL, var_id, var_type);
522
523 /* 3a. Initialization. */
524 if (init)
525 DECL_INITIAL (var_decl) = fold_build1 (CONVERT_EXPR, var_type, init);
526 else
527 DECL_INITIAL (var_decl) = NULL_TREE;
528
529 gcc_assert (TYPE_SIZE (var_type) != 0); /* Did not calculate size. */
530
531 DECL_CONTEXT (var_decl) = current_function_decl;
532
533 DECL_SOURCE_LOCATION (var_decl) = loc;
534
535 DECL_EXTERNAL (var_decl) = 0;
536 TREE_PUBLIC (var_decl) = 0;
537 TREE_STATIC (var_decl) = 0;
538 /* Set the storage mode and whether only visible in the same file. */
539 switch (storage_class)
540 {
541 case STATIC_STORAGE:
542 TREE_STATIC (var_decl) = 1;
543 break;
544
545 case AUTOMATIC_STORAGE:
546 break;
547
548 case EXTERNAL_DEFINITION_STORAGE:
549 TREE_PUBLIC (var_decl) = 1;
550 break;
551
552 case EXTERNAL_REFERENCE_STORAGE:
553 DECL_EXTERNAL (var_decl) = 1;
554 break;
555
556 default:
557 gcc_unreachable ();
558 }
559
560 TYPE_NAME (TREE_TYPE (var_decl)) = TYPE_NAME (var_type);
561 return pushdecl (copy_node (var_decl));
562 }
563
564
565 /* Generate code for return statement. Type is in TYPE, expression
566 is in EXP if present. */
567
568 void
569 tree_code_generate_return (tree type, tree exp)
570 {
571 tree setret;
572 #ifdef ENABLE_CHECKING
573 tree param;
574
575 for (param = DECL_ARGUMENTS (current_function_decl);
576 param;
577 param = TREE_CHAIN (param))
578 gcc_assert (DECL_CONTEXT (param) == current_function_decl);
579 #endif
580
581 if (exp && TREE_TYPE (TREE_TYPE (current_function_decl)) != void_type_node)
582 {
583 setret = fold_build2 (MODIFY_EXPR, type,
584 DECL_RESULT (current_function_decl),
585 fold_build1 (CONVERT_EXPR, type, exp));
586 TREE_SIDE_EFFECTS (setret) = 1;
587 TREE_USED (setret) = 1;
588 setret = build1 (RETURN_EXPR, type, setret);
589 /* Use EXPR_LOCUS so we don't lose any information about the file we
590 are compiling. */
591 SET_EXPR_LOCUS (setret, EXPR_LOCUS (exp));
592 }
593 else
594 setret = build1 (RETURN_EXPR, type, NULL_TREE);
595
596 append_to_statement_list_force (setret, getstmtlist ());
597 }
598
599
600 /* Output the code for this expression statement CODE. */
601
602 void
603 tree_code_output_expression_statement (tree code, location_t loc)
604 {
605 /* Output the line number information. */
606 SET_EXPR_LOCATION (code, loc);
607 TREE_USED (code) = 1;
608 TREE_SIDE_EFFECTS (code) = 1;
609 /* put CODE into the code list. */
610 append_to_statement_list_force (code, getstmtlist ());
611 }
612
613 /* Return a tree for a constant integer value in the token TOK. No
614 size checking is done. */
615
616 tree
617 tree_code_get_integer_value (unsigned char* chars, unsigned int length)
618 {
619 long long int val = 0;
620 unsigned int ix;
621 unsigned int start = 0;
622 int negative = 1;
623 switch (chars[0])
624 {
625 case (unsigned char)'-':
626 negative = -1;
627 start = 1;
628 break;
629
630 case (unsigned char)'+':
631 start = 1;
632 break;
633
634 default:
635 break;
636 }
637 for (ix = start; ix < length; ix++)
638 val = val * 10 + chars[ix] - (unsigned char)'0';
639 val = val*negative;
640 return build_int_cst_wide (start == 1 ?
641 integer_type_node : unsigned_type_node,
642 val & 0xffffffff, (val >> 32) & 0xffffffff);
643 }
644
645 /* Return the tree for an expresssion, type EXP_TYPE (see treetree.h)
646 with tree type TYPE and with operands1 OP1, OP2 (maybe), OP3 (maybe). */
647 tree
648 tree_code_get_expression (unsigned int exp_type,
649 tree type, tree op1, tree op2,
650 tree op3 ATTRIBUTE_UNUSED,
651 location_t loc)
652 {
653 tree ret1;
654 int operator;
655
656 switch (exp_type)
657 {
658 case EXP_ASSIGN:
659 gcc_assert (op1 && op2);
660 operator = MODIFY_EXPR;
661 ret1 = fold_build2 (operator, void_type_node, op1,
662 fold_build1 (CONVERT_EXPR, TREE_TYPE (op1), op2));
663
664 break;
665
666 case EXP_PLUS:
667 operator = PLUS_EXPR;
668 goto binary_expression;
669
670 case EXP_MINUS:
671 operator = MINUS_EXPR;
672 goto binary_expression;
673
674 case EXP_EQUALS:
675 operator = EQ_EXPR;
676 goto binary_expression;
677
678 /* Expand a binary expression. Ensure the operands are the right type. */
679 binary_expression:
680 gcc_assert (op1 && op2);
681 ret1 = fold_build2 (operator, type,
682 fold_build1 (CONVERT_EXPR, type, op1),
683 fold_build1 (CONVERT_EXPR, type, op2));
684 break;
685
686 /* Reference to a variable. This is dead easy, just return the
687 decl for the variable. If the TYPE is different than the
688 variable type, convert it. However, to keep accurate location
689 information we wrap it in a NOP_EXPR is is easily stripped. */
690 case EXP_REFERENCE:
691 gcc_assert (op1);
692 TREE_USED (op1) = 1;
693 if (type == TREE_TYPE (op1))
694 ret1 = build1 (NOP_EXPR, type, op1);
695 else
696 ret1 = fold_build1 (CONVERT_EXPR, type, op1);
697 break;
698
699 case EXP_FUNCTION_INVOCATION:
700 gcc_assert (op1);
701 {
702 tree fun_ptr;
703 TREE_USED (op1) = 1;
704 fun_ptr = fold_build1 (ADDR_EXPR,
705 build_pointer_type (TREE_TYPE (op1)), op1);
706 ret1 = fold_build3 (CALL_EXPR, type, fun_ptr, nreverse (op2),
707 NULL_TREE);
708 }
709 break;
710
711 default:
712 gcc_unreachable ();
713 }
714
715 /* Declarations already have a location and constants can be shared so they
716 shouldn't a location set on them. */
717 if (! DECL_P (ret1) && ! TREE_CONSTANT (ret1))
718 SET_EXPR_LOCATION (ret1, loc);
719 return ret1;
720 }
721
722 /* Init parameter list and return empty list. */
723
724 tree
725 tree_code_init_parameters (void)
726 {
727 return NULL_TREE;
728 }
729
730 /* Add a parameter EXP whose expression type is EXP_PROTO to list
731 LIST, returning the new list. */
732
733 tree
734 tree_code_add_parameter (tree list, tree proto_exp, tree exp)
735 {
736 tree new_exp;
737 new_exp = tree_cons (NULL_TREE,
738 fold_build1 (CONVERT_EXPR, TREE_TYPE (proto_exp),
739 exp), NULL_TREE);
740 if (!list)
741 return new_exp;
742 return chainon (new_exp, list);
743 }
744
745 /* Get a stringpool entry for a string S of length L. This is needed
746 because the GTY routines don't mark strings, forcing you to put
747 them into stringpool, which is never freed. */
748
749 const char*
750 get_string (const char *s, size_t l)
751 {
752 tree t;
753 t = get_identifier_with_length (s, l);
754 return IDENTIFIER_POINTER(t);
755 }
756
757 /* Save typing debug_tree all the time. Dump a tree T pretty and
758 concise. */
759
760 void dt (tree t);
761
762 void
763 dt (tree t)
764 {
765 debug_tree (t);
766 }
767
768 /* Routines Expected by gcc: */
769
770 /* These are used to build types for various sizes. The code below
771 is a simplified version of that of GNAT. */
772
773 #ifndef MAX_BITS_PER_WORD
774 #define MAX_BITS_PER_WORD BITS_PER_WORD
775 #endif
776
777 /* This variable keeps a table for types for each precision so that we only
778 allocate each of them once. Signed and unsigned types are kept separate. */
779 static GTY(()) tree signed_and_unsigned_types[MAX_BITS_PER_WORD + 1][2];
780
781 /* Mark EXP saying that we need to be able to take the
782 address of it; it should not be allocated in a register.
783 Value is 1 if successful.
784
785 This implementation was copied from c-decl.c. */
786
787 static bool
788 tree_mark_addressable (tree exp)
789 {
790 register tree x = exp;
791 while (1)
792 switch (TREE_CODE (x))
793 {
794 case COMPONENT_REF:
795 case ADDR_EXPR:
796 case ARRAY_REF:
797 case REALPART_EXPR:
798 case IMAGPART_EXPR:
799 x = TREE_OPERAND (x, 0);
800 break;
801
802 case CONSTRUCTOR:
803 TREE_ADDRESSABLE (x) = 1;
804 return 1;
805
806 case VAR_DECL:
807 case CONST_DECL:
808 case PARM_DECL:
809 case RESULT_DECL:
810 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
811 && DECL_NONLOCAL (x))
812 {
813 if (TREE_PUBLIC (x))
814 {
815 error ("Global register variable %qD used in nested function.",
816 x);
817 return 0;
818 }
819 pedwarn ("Register variable %qD used in nested function.", x);
820 }
821 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
822 {
823 if (TREE_PUBLIC (x))
824 {
825 error ("Address of global register variable %qD requested.",
826 x);
827 return 0;
828 }
829
830 pedwarn ("Address of register variable %qD requested.", x);
831 }
832
833 /* drops in */
834 case FUNCTION_DECL:
835 TREE_ADDRESSABLE (x) = 1;
836
837 default:
838 return 1;
839 }
840 }
841
842 /* Return an integer type with the number of bits of precision given by
843 PRECISION. UNSIGNEDP is nonzero if the type is unsigned; otherwise
844 it is a signed type. */
845
846 static tree
847 tree_lang_type_for_size (unsigned precision, int unsignedp)
848 {
849 tree t;
850
851 if (precision <= MAX_BITS_PER_WORD
852 && signed_and_unsigned_types[precision][unsignedp] != 0)
853 return signed_and_unsigned_types[precision][unsignedp];
854
855 if (unsignedp)
856 t = signed_and_unsigned_types[precision][1]
857 = make_unsigned_type (precision);
858 else
859 t = signed_and_unsigned_types[precision][0]
860 = make_signed_type (precision);
861
862 return t;
863 }
864
865 /* Return a data type that has machine mode MODE. UNSIGNEDP selects
866 an unsigned type; otherwise a signed type is returned. */
867
868 static tree
869 tree_lang_type_for_mode (enum machine_mode mode, int unsignedp)
870 {
871 if (SCALAR_INT_MODE_P (mode))
872 return tree_lang_type_for_size (GET_MODE_BITSIZE (mode), unsignedp);
873 else
874 return NULL_TREE;
875 }
876
877 /* Return the unsigned version of a TYPE_NODE, a scalar type. */
878
879 static tree
880 tree_lang_unsigned_type (tree type_node)
881 {
882 return tree_lang_type_for_size (TYPE_PRECISION (type_node), 1);
883 }
884
885 /* Return the signed version of a TYPE_NODE, a scalar type. */
886
887 static tree
888 tree_lang_signed_type (tree type_node)
889 {
890 return tree_lang_type_for_size (TYPE_PRECISION (type_node), 0);
891 }
892
893 /* Return a type the same as TYPE except unsigned or signed according to
894 UNSIGNEDP. */
895
896 static tree
897 tree_lang_signed_or_unsigned_type (int unsignedp, tree type)
898 {
899 if (! INTEGRAL_TYPE_P (type) || TYPE_UNSIGNED (type) == unsignedp)
900 return type;
901 else
902 return tree_lang_type_for_size (TYPE_PRECISION (type), unsignedp);
903 }
904 \f
905 /* These functions and variables deal with binding contours. We only
906 need these functions for the list of PARM_DECLs, but we leave the
907 functions more general; these are a simplified version of the
908 functions from GNAT. */
909
910 /* For each binding contour we allocate a binding_level structure which records
911 the entities defined or declared in that contour. Contours include:
912
913 the global one
914 one for each subprogram definition
915 one for each compound statement (declare block)
916
917 Binding contours are used to create GCC tree BLOCK nodes. */
918
919 struct binding_level
920 {
921 /* A chain of ..._DECL nodes for all variables, constants, functions,
922 parameters and type declarations. These ..._DECL nodes are chained
923 through the TREE_CHAIN field. Note that these ..._DECL nodes are stored
924 in the reverse of the order supplied to be compatible with the
925 back-end. */
926 tree names;
927 /* For each level (except the global one), a chain of BLOCK nodes for all
928 the levels that were entered and exited one level down from this one. */
929 tree blocks;
930
931 tree stmts;
932 /* The binding level containing this one (the enclosing binding level). */
933 struct binding_level *level_chain;
934 };
935
936 /* The binding level currently in effect. */
937 static struct binding_level *current_binding_level = NULL;
938
939 /* The outermost binding level. This binding level is created when the
940 compiler is started and it will exist through the entire compilation. */
941 static struct binding_level *global_binding_level;
942
943 /* Binding level structures are initialized by copying this one. */
944 static struct binding_level clear_binding_level = {NULL, NULL, NULL, NULL };
945 \f
946 /* Return non-zero if we are currently in the global binding level. */
947
948 static int
949 global_bindings_p (void)
950 {
951 return current_binding_level == global_binding_level ? -1 : 0;
952 }
953
954
955 /* Return the list of declarations in the current level. Note that this list
956 is in reverse order (it has to be so for back-end compatibility). */
957
958 static tree
959 getdecls (void)
960 {
961 return current_binding_level->names;
962 }
963
964 /* Return a STATMENT_LIST for the current block. */
965
966 static tree*
967 getstmtlist (void)
968 {
969 return &current_binding_level->stmts;
970 }
971
972 /* Enter a new binding level. The input parameter is ignored, but has to be
973 specified for back-end compatibility. */
974
975 static void
976 pushlevel (int ignore ATTRIBUTE_UNUSED)
977 {
978 struct binding_level *newlevel = xmalloc (sizeof (struct binding_level));
979
980 *newlevel = clear_binding_level;
981
982 /* Add this level to the front of the chain (stack) of levels that are
983 active. */
984 newlevel->level_chain = current_binding_level;
985 current_binding_level = newlevel;
986 current_binding_level->stmts = alloc_stmt_list ();
987 }
988
989 /* Exit a binding level.
990 Pop the level off, and restore the state of the identifier-decl mappings
991 that were in effect when this level was entered.
992
993 If KEEP is nonzero, this level had explicit declarations, so
994 and create a "block" (a BLOCK node) for the level
995 to record its declarations and subblocks for symbol table output.
996
997 If FUNCTIONBODY is nonzero, this level is the body of a function,
998 so create a block as if KEEP were set and also clear out all
999 label names.
1000
1001 If REVERSE is nonzero, reverse the order of decls before putting
1002 them into the BLOCK. */
1003
1004 static tree
1005 poplevel (int keep, int reverse, int functionbody)
1006 {
1007 /* Points to a BLOCK tree node. This is the BLOCK node construted for the
1008 binding level that we are about to exit and which is returned by this
1009 routine. */
1010 tree block_node = NULL_TREE;
1011 tree decl_chain;
1012 tree subblock_chain = current_binding_level->blocks;
1013 tree subblock_node;
1014
1015 /* Reverse the list of *_DECL nodes if desired. Note that the ..._DECL
1016 nodes chained through the `names' field of current_binding_level are in
1017 reverse order except for PARM_DECL node, which are explicitely stored in
1018 the right order. */
1019 decl_chain = (reverse) ? nreverse (current_binding_level->names)
1020 : current_binding_level->names;
1021
1022 /* If there were any declarations in the current binding level, or if this
1023 binding level is a function body, or if there are any nested blocks then
1024 create a BLOCK node to record them for the life of this function. */
1025 if (keep || functionbody)
1026 block_node = build_block (keep ? decl_chain : 0, 0, subblock_chain, 0, 0);
1027
1028 /* Record the BLOCK node just built as the subblock its enclosing scope. */
1029 for (subblock_node = subblock_chain; subblock_node;
1030 subblock_node = TREE_CHAIN (subblock_node))
1031 BLOCK_SUPERCONTEXT (subblock_node) = block_node;
1032
1033 /* Clear out the meanings of the local variables of this level. */
1034
1035 for (subblock_node = decl_chain; subblock_node;
1036 subblock_node = TREE_CHAIN (subblock_node))
1037 if (DECL_NAME (subblock_node) != 0)
1038 /* If the identifier was used or addressed via a local extern decl,
1039 don't forget that fact. */
1040 if (DECL_EXTERNAL (subblock_node))
1041 {
1042 if (TREE_USED (subblock_node))
1043 TREE_USED (DECL_NAME (subblock_node)) = 1;
1044 }
1045
1046 /* Pop the current level. */
1047 current_binding_level = current_binding_level->level_chain;
1048
1049 if (functionbody)
1050 {
1051 /* This is the top level block of a function. */
1052 DECL_INITIAL (current_function_decl) = block_node;
1053 }
1054 else if (block_node)
1055 {
1056 current_binding_level->blocks
1057 = chainon (current_binding_level->blocks, block_node);
1058 }
1059
1060 /* If we did not make a block for the level just exited, any blocks made for
1061 inner levels (since they cannot be recorded as subblocks in that level)
1062 must be carried forward so they will later become subblocks of something
1063 else. */
1064 else if (subblock_chain)
1065 current_binding_level->blocks
1066 = chainon (current_binding_level->blocks, subblock_chain);
1067 if (block_node)
1068 TREE_USED (block_node) = 1;
1069
1070 return block_node;
1071 }
1072 \f
1073 /* Insert BLOCK at the end of the list of subblocks of the
1074 current binding level. This is used when a BIND_EXPR is expanded,
1075 to handle the BLOCK node inside the BIND_EXPR. */
1076
1077 static void
1078 insert_block (tree block)
1079 {
1080 TREE_USED (block) = 1;
1081 current_binding_level->blocks
1082 = chainon (current_binding_level->blocks, block);
1083 }
1084
1085
1086 /* Records a ..._DECL node DECL as belonging to the current lexical scope.
1087 Returns the ..._DECL node. */
1088
1089 tree
1090 pushdecl (tree decl)
1091 {
1092 /* External objects aren't nested, other objects may be. */
1093
1094 if ((DECL_EXTERNAL (decl)) || (decl==current_function_decl))
1095 DECL_CONTEXT (decl) = 0;
1096 else
1097 DECL_CONTEXT (decl) = current_function_decl;
1098
1099 /* Put the declaration on the list. The list of declarations is in reverse
1100 order. The list will be reversed later if necessary. This needs to be
1101 this way for compatibility with the back-end. */
1102
1103 TREE_CHAIN (decl) = current_binding_level->names;
1104 current_binding_level->names = decl;
1105
1106 /* For the declartion of a type, set its name if it is not already set. */
1107
1108 if (TREE_CODE (decl) == TYPE_DECL
1109 && TYPE_NAME (TREE_TYPE (decl)) == 0)
1110 TYPE_NAME (TREE_TYPE (decl)) = DECL_NAME (decl);
1111
1112 /* Put automatic variables into the intermediate representation. */
1113 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
1114 && !TREE_STATIC (decl) && !TREE_PUBLIC (decl))
1115 tree_code_output_expression_statement (build1 (DECL_EXPR, void_type_node,
1116 decl),
1117 DECL_SOURCE_LOCATION (decl));
1118 return decl;
1119 }
1120 \f
1121
1122 static void
1123 tree_push_type_decl(tree id, tree type_node)
1124 {
1125 tree decl = build_decl (TYPE_DECL, id, type_node);
1126 TYPE_NAME (type_node) = id;
1127 pushdecl (decl);
1128 }
1129
1130 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
1131
1132 /* Create the predefined scalar types of C,
1133 and some nodes representing standard constants (0, 1, (void *) 0).
1134 Initialize the global binding level.
1135 Make definitions for built-in primitive functions. */
1136
1137 void
1138 treelang_init_decl_processing (void)
1139 {
1140 current_function_decl = NULL;
1141 current_binding_level = NULL_BINDING_LEVEL;
1142 pushlevel (0); /* make the binding_level structure for global names */
1143 global_binding_level = current_binding_level;
1144
1145 build_common_tree_nodes (flag_signed_char, false);
1146
1147 /* set standard type names */
1148
1149 /* Define `int' and `char' last so that they are not overwritten. */
1150 tree_push_type_decl (NULL_TREE, intQI_type_node);
1151 tree_push_type_decl (NULL_TREE, intHI_type_node);
1152 tree_push_type_decl (NULL_TREE, intSI_type_node);
1153 tree_push_type_decl (NULL_TREE, intDI_type_node);
1154 #if HOST_BITS_PER_WIDE_INT >= 64
1155 tree_push_type_decl (NULL_TREE, intTI_type_node);
1156 #endif
1157 tree_push_type_decl (NULL_TREE, unsigned_intQI_type_node);
1158 tree_push_type_decl (NULL_TREE, unsigned_intHI_type_node);
1159 tree_push_type_decl (NULL_TREE, unsigned_intSI_type_node);
1160 tree_push_type_decl (NULL_TREE, unsigned_intDI_type_node);
1161 #if HOST_BITS_PER_WIDE_INT >= 64
1162 tree_push_type_decl (NULL_TREE, unsigned_intTI_type_node);
1163 #endif
1164
1165 tree_push_type_decl (get_identifier ("int"), integer_type_node);
1166 tree_push_type_decl (get_identifier ("char"), char_type_node);
1167 tree_push_type_decl (get_identifier ("long int"),
1168 long_integer_type_node);
1169 tree_push_type_decl (get_identifier ("unsigned int"),
1170 unsigned_type_node);
1171 tree_push_type_decl (get_identifier ("long unsigned int"),
1172 long_unsigned_type_node);
1173 tree_push_type_decl (get_identifier ("long long int"),
1174 long_long_integer_type_node);
1175 tree_push_type_decl (get_identifier ("long long unsigned int"),
1176 long_long_unsigned_type_node);
1177 tree_push_type_decl (get_identifier ("short int"),
1178 short_integer_type_node);
1179 tree_push_type_decl (get_identifier ("short unsigned int"),
1180 short_unsigned_type_node);
1181 tree_push_type_decl (get_identifier ("signed char"),
1182 signed_char_type_node);
1183 tree_push_type_decl (get_identifier ("unsigned char"),
1184 unsigned_char_type_node);
1185 size_type_node = make_unsigned_type (POINTER_SIZE);
1186 tree_push_type_decl (get_identifier ("size_t"), size_type_node);
1187 set_sizetype (size_type_node);
1188
1189 build_common_tree_nodes_2 (/* short_double= */ 0);
1190
1191 tree_push_type_decl (get_identifier ("float"), float_type_node);
1192 tree_push_type_decl (get_identifier ("double"), double_type_node);
1193 tree_push_type_decl (get_identifier ("long double"), long_double_type_node);
1194 tree_push_type_decl (get_identifier ("void"), void_type_node);
1195
1196 build_common_builtin_nodes ();
1197 (*targetm.init_builtins) ();
1198
1199 pedantic_lvalues = pedantic;
1200 }
1201
1202 /* Return a definition for a builtin function named NAME and whose data type
1203 is TYPE. TYPE should be a function type with argument types.
1204 FUNCTION_CODE tells later passes how to compile calls to this function.
1205 See tree.h for its possible values.
1206
1207 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
1208 the name to be called if we can't opencode the function. If
1209 ATTRS is nonzero, use that for the function's attribute list.
1210
1211 copied from gcc/c-decl.c
1212 */
1213
1214 static tree
1215 builtin_function (const char *name, tree type, int function_code,
1216 enum built_in_class class, const char *library_name,
1217 tree attrs)
1218 {
1219 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
1220 DECL_EXTERNAL (decl) = 1;
1221 TREE_PUBLIC (decl) = 1;
1222 if (library_name)
1223 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
1224 pushdecl (decl);
1225 DECL_BUILT_IN_CLASS (decl) = class;
1226 DECL_FUNCTION_CODE (decl) = function_code;
1227
1228 /* Possibly apply some default attributes to this built-in function. */
1229 if (attrs)
1230 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
1231 else
1232 decl_attributes (&decl, NULL_TREE, 0);
1233
1234 return decl;
1235 }
1236
1237 /* Treelang expand function langhook. */
1238
1239 static void
1240 treelang_expand_function (tree fndecl)
1241 {
1242 /* We have nothing special to do while expanding functions for treelang. */
1243 tree_rest_of_compilation (fndecl);
1244 }
1245
1246 #include "debug.h" /* for debug_hooks, needed by gt-treelang-treetree.h */
1247 #include "gt-treelang-treetree.h"