1 /* This file contains the definitions and documentation for the common
2 tree codes used in the GNU C and C++ compilers (see c-common.def
3 for the standard codes).
4 Copyright (C) 2000 Free Software Foundation, Inc. Written by
5 Benjamin Chelf (chelf@codesourcery.com).
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
28 #include "splay-tree.h"
39 static tree prune_unused_decls PARAMS ((tree *, int *, void *));
41 /* Create an empty statement tree rooted at T. */
47 /* We create a trivial EXPR_STMT so that last_tree is never NULL in
48 what follows. We remove the extraneous statement in
50 *t = build_nt (EXPR_STMT, void_zero_node);
52 last_expr_type = NULL_TREE;
55 /* T is a statement. Add it to the statement-tree. */
61 /* Add T to the statement-tree. */
62 TREE_CHAIN (last_tree) = t;
64 /* When we expand a statement-tree, we must know whether or not the
65 statements are full-expresions. We record that fact here. */
66 STMT_IS_FULL_EXPR_P (last_tree) = stmts_are_full_exprs_p ();
69 /* Remove declarations of internal variables that are not used from a
70 stmt tree. To qualify, the variable must have a name and must have
71 a zero DECL_SOURCE_LINE. We tried to remove all variables for
72 which TREE_USED was false, but it turns out that there's tons of
73 variables for which TREE_USED is false but that are still in fact
77 prune_unused_decls (tp, walk_subtrees, data)
79 int *walk_subtrees ATTRIBUTE_UNUSED;
80 void *data ATTRIBUTE_UNUSED;
90 if (TREE_CODE (t) == DECL_STMT)
92 tree d = DECL_STMT_DECL (t);
93 if (!TREE_USED (d) && DECL_NAME (d) && DECL_SOURCE_LINE (d) == 0)
96 /* Recurse on the new value of tp, otherwise we will skip
97 the next statement. */
98 return prune_unused_decls (tp, walk_subtrees, data);
101 else if (TREE_CODE (t) == SCOPE_STMT)
103 /* Remove all unused decls from the BLOCK of this SCOPE_STMT. */
104 tree block = SCOPE_STMT_BLOCK (t);
110 for (vp = &BLOCK_VARS (block); *vp; )
113 if (! TREE_USED (v) && DECL_NAME (v) && DECL_SOURCE_LINE (v) == 0)
114 *vp = TREE_CHAIN (v); /* drop */
116 vp = &TREE_CHAIN (v); /* advance */
118 /* If there are now no variables, the entire BLOCK can be dropped.
119 (This causes SCOPE_NULLIFIED_P (t) to be true.) */
120 if (BLOCK_VARS (block) == NULL_TREE)
121 SCOPE_STMT_BLOCK (t) = NULL_TREE;
127 /* Finish the statement tree rooted at T. */
135 /* Remove the fake extra statement added in begin_stmt_tree. */
136 stmt = TREE_CHAIN (*t);
138 last_tree = NULL_TREE;
140 /* Remove unused decls from the stmt tree. */
141 walk_stmt_tree (t, prune_unused_decls, NULL);
145 /* The line-number recorded in the outermost statement in a function
146 is the line number of the end of the function. */
147 STMT_LINENO (stmt) = lineno;
148 STMT_LINENO_FOR_FN_P (stmt) = 1;
152 /* Build a generic statement based on the given type of node and
153 arguments. Similar to `build_nt', except that we set
154 TREE_COMPLEXITY to be the current line number. */
157 build_stmt VPARAMS ((enum tree_code code, ...))
159 #ifndef ANSI_PROTOTYPES
169 #ifndef ANSI_PROTOTYPES
170 code = va_arg (p, enum tree_code);
173 t = make_node (code);
174 length = TREE_CODE_LENGTH (code);
175 TREE_COMPLEXITY (t) = lineno;
177 for (i = 0; i < length; i++)
178 TREE_OPERAND (t, i) = va_arg (p, tree);
184 /* Some statements, like for-statements or if-statements, require a
185 condition. This condition can be a declaration. If T is such a
186 declaration it is processed, and an expression appropriate to use
187 as the condition is returned. Otherwise, T itself is returned. */
193 if (t && TREE_CODE (t) == TREE_LIST)
195 expand_stmt (TREE_PURPOSE (t));
196 return TREE_VALUE (t);
202 /* Create RTL for the local static variable DECL. */
205 make_rtl_for_local_static (decl)
208 const char *asmspec = NULL;
210 /* If we inlined this variable, we could see it's declaration
212 if (TREE_ASM_WRITTEN (decl))
215 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
217 /* The only way this situaton can occur is if the
218 user specified a name for this DECL using the
219 `attribute' syntax. */
220 asmspec = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
221 DECL_ASSEMBLER_NAME (decl) = DECL_NAME (decl);
224 rest_of_decl_compilation (decl, asmspec, /*top_level=*/0, /*at_end=*/0);
227 /* Let the back-end know about DECL. */
230 emit_local_var (decl)
233 /* Create RTL for this variable. */
234 if (!DECL_RTL (decl))
236 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
237 /* The user must have specified an assembler name for this
238 variable. Set that up now. */
239 rest_of_decl_compilation
240 (decl, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
241 /*top_level=*/0, /*at_end=*/0);
246 /* Actually do the initialization. */
247 if (stmts_are_full_exprs_p ())
248 expand_start_target_temps ();
250 expand_decl_init (decl);
252 if (stmts_are_full_exprs_p ())
253 expand_end_target_temps ();
256 /* Helper for generating the RTL at the beginning of a scope. */
259 genrtl_do_pushlevel ()
261 emit_line_note (input_filename, lineno);
265 /* Helper for generating the RTL. */
268 genrtl_clear_out_block ()
270 /* If COND wasn't a declaration, clear out the
271 block we made for it and start a new one here so the
272 optimization in expand_end_loop will work. */
273 if (getdecls () == NULL_TREE)
274 genrtl_do_pushlevel ();
277 /* Generate the RTL for DESTINATION, which is a GOTO_STMT. */
280 genrtl_goto_stmt (destination)
283 if (TREE_CODE (destination) == IDENTIFIER_NODE)
286 /* We warn about unused labels with -Wunused. That means we have to
287 mark the used labels as used. */
288 if (TREE_CODE (destination) == LABEL_DECL)
289 TREE_USED (destination) = 1;
291 emit_line_note (input_filename, lineno);
293 if (TREE_CODE (destination) == LABEL_DECL)
295 label_rtx (destination);
296 expand_goto (destination);
299 expand_computed_goto (destination);
302 /* Generate the RTL for EXPR, which is an EXPR_STMT. */
305 genrtl_expr_stmt (expr)
308 if (expr != NULL_TREE)
310 emit_line_note (input_filename, lineno);
312 if (stmts_are_full_exprs_p ())
313 expand_start_target_temps ();
315 lang_expand_expr_stmt (expr);
317 if (stmts_are_full_exprs_p ())
318 expand_end_target_temps ();
322 /* Generate the RTL for T, which is a DECL_STMT. */
329 emit_line_note (input_filename, lineno);
330 decl = DECL_STMT_DECL (t);
331 /* If this is a declaration for an automatic local
332 variable, initialize it. Note that we might also see a
333 declaration for a namespace-scope object (declared with
334 `extern'). We don't have to handle the initialization
335 of those objects here; they can only be declarations,
336 rather than definitions. */
337 if (TREE_CODE (decl) == VAR_DECL
338 && !TREE_STATIC (decl)
339 && !DECL_EXTERNAL (decl))
341 /* Let the back-end know about this variable. */
342 if (!anon_aggr_type_p (TREE_TYPE (decl)))
343 emit_local_var (decl);
345 expand_anon_union_decl (decl, NULL_TREE,
346 DECL_ANON_UNION_ELEMS (decl));
348 else if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
350 if (DECL_ARTIFICIAL (decl) && ! TREE_USED (decl))
351 /* Do not emit unused decls. This is not just an
352 optimization. We really do not want to emit
353 __PRETTY_FUNCTION__ etc, if they're never used. */
354 DECL_IGNORED_P (decl) = 1;
356 make_rtl_for_local_static (decl);
360 /* Generate the RTL for T, which is an IF_STMT. */
367 genrtl_do_pushlevel ();
368 cond = expand_cond (IF_COND (t));
369 emit_line_note (input_filename, lineno);
370 expand_start_cond (cond, 0);
372 expand_stmt (THEN_CLAUSE (t));
375 expand_start_else ();
376 expand_stmt (ELSE_CLAUSE (t));
381 /* Generate the RTL for T, which is a WHILE_STMT. */
384 genrtl_while_stmt (t)
389 emit_line_note (input_filename, lineno);
390 expand_start_loop (1);
391 genrtl_do_pushlevel ();
393 cond = expand_cond (WHILE_COND (t));
394 emit_line_note (input_filename, lineno);
395 expand_exit_loop_if_false (0, cond);
396 genrtl_clear_out_block ();
398 expand_stmt (WHILE_BODY (t));
403 /* Generate the RTL for T, which is a DO_STMT. */
411 emit_line_note (input_filename, lineno);
412 expand_start_loop_continue_elsewhere (1);
414 expand_stmt (DO_BODY (t));
416 expand_loop_continue_here ();
418 cond = expand_cond (DO_COND (t));
419 emit_line_note (input_filename, lineno);
420 expand_exit_loop_if_false (0, cond);
424 /* Build the node for a return statement and return it. */
427 build_return_stmt (expr)
430 return (build_stmt (RETURN_STMT, expr));
433 /* Generate the RTL for EXPR, which is a RETURN_STMT. */
436 genrtl_return_stmt (expr)
439 emit_line_note (input_filename, lineno);
440 c_expand_return (expr);
443 /* Generate the RTL for T, which is a FOR_STMT. */
451 if (NEW_FOR_SCOPE_P (t))
452 genrtl_do_pushlevel ();
454 expand_stmt (FOR_INIT_STMT (t));
457 emit_line_note (input_filename, lineno);
458 expand_start_loop_continue_elsewhere (1);
459 genrtl_do_pushlevel ();
460 cond = expand_cond (FOR_COND (t));
461 emit_line_note (input_filename, lineno);
463 expand_exit_loop_if_false (0, cond);
464 genrtl_clear_out_block ();
467 expand_stmt (FOR_BODY (t));
469 emit_line_note (input_filename, lineno);
470 expand_loop_continue_here ();
472 genrtl_expr_stmt (tmp);
476 /* Build a break statement node and return it. */
481 return (build_stmt (BREAK_STMT));
484 /* Generate the RTL for a BREAK_STMT. */
489 emit_line_note (input_filename, lineno);
490 if ( ! expand_exit_something ())
491 error ("break statement not within loop or switch");
494 /* Build a continue statement node and return it. */
497 build_continue_stmt ()
499 return (build_stmt (CONTINUE_STMT));
502 /* Generate the RTL for a CONTINUE_STMT. */
505 genrtl_continue_stmt ()
507 emit_line_note (input_filename, lineno);
508 if (! expand_continue_loop (0))
509 error ("continue statement not within a loop");
512 /* Generate the RTL for T, which is a SCOPE_STMT. */
515 genrtl_scope_stmt (t)
518 if (!SCOPE_NO_CLEANUPS_P (t))
520 if (SCOPE_BEGIN_P (t))
521 expand_start_bindings_and_block (2 * SCOPE_NULLIFIED_P (t),
522 SCOPE_STMT_BLOCK (t));
523 else if (SCOPE_END_P (t))
524 expand_end_bindings (NULL_TREE, !SCOPE_NULLIFIED_P (t), 0);
526 else if (!SCOPE_NULLIFIED_P (t))
528 rtx note = emit_note (NULL,
530 ? NOTE_INSN_BLOCK_BEG
531 : NOTE_INSN_BLOCK_END));
532 NOTE_BLOCK (note) = SCOPE_STMT_BLOCK (t);
536 /* Generate the RTL for T, which is a SWITCH_STMT. */
539 genrtl_switch_stmt (t)
543 genrtl_do_pushlevel ();
545 cond = expand_cond (SWITCH_COND (t));
546 if (cond != error_mark_node)
548 emit_line_note (input_filename, lineno);
549 c_expand_start_case (cond);
552 /* The code is in error, but we don't want expand_end_case to
554 c_expand_start_case (boolean_false_node);
556 expand_stmt (SWITCH_BODY (t));
558 expand_end_case (cond);
561 /* Create a CASE_LABEL tree node and return it. */
564 build_case_label (low_value, high_value)
568 return build_stmt (CASE_LABEL, low_value, high_value);
572 /* Generate the RTL for a CASE_LABEL. */
575 genrtl_case_label (low_value, high_value)
579 do_case (low_value, high_value);
582 /* Generate the RTL for T, which is a COMPOUND_STMT. */
585 genrtl_compound_stmt (t)
588 /* If this is the outermost block of the function, declare the
589 variables __FUNCTION__, __PRETTY_FUNCTION__, and so forth. */
591 && !current_function_name_declared ()
592 && !COMPOUND_STMT_NO_SCOPE (t))
594 set_current_function_name_declared (1);
595 declare_function_name ();
598 expand_stmt (COMPOUND_BODY (t));
601 /* Generate the RTL for an ASM_STMT. */
604 genrtl_asm_stmt (cv_qualifier, string, output_operands,
605 input_operands, clobbers)
608 tree output_operands;
612 if (TREE_CHAIN (string))
613 string = combine_strings (string);
615 if (cv_qualifier != NULL_TREE
616 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
618 warning ("%s qualifier ignored on asm",
619 IDENTIFIER_POINTER (cv_qualifier));
620 cv_qualifier = NULL_TREE;
623 emit_line_note (input_filename, lineno);
624 if (output_operands != NULL_TREE || input_operands != NULL_TREE
625 || clobbers != NULL_TREE)
626 c_expand_asm_operands (string, output_operands,
629 cv_qualifier != NULL_TREE,
630 input_filename, lineno);
635 /* Generate the RTL for a DECL_CLEANUP. */
638 genrtl_decl_cleanup (decl, cleanup)
642 if (!decl || (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node))
643 expand_decl_cleanup (decl, cleanup);
646 /* Generate the RTL for the statement T, its substatements, and any
647 other statements at its nesting level. */
654 rval = lang_expand_stmt (t);