1 /* Code translation -- generate GCC trees from gfc_code.
2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Paul Brook
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
24 #include "coretypes.h"
26 #include "tree-gimple.h"
33 #include "trans-stmt.h"
34 #include "trans-array.h"
35 #include "trans-types.h"
36 #include "trans-const.h"
38 /* Naming convention for backend interface code:
40 gfc_trans_* translate gfc_code into STMT trees.
42 gfc_conv_* expression conversion
44 gfc_get_* get a backend tree representation of a decl or type */
46 static gfc_file
*gfc_current_backend_file
;
49 /* Advance along TREE_CHAIN n times. */
52 gfc_advance_chain (tree t
, int n
)
56 gcc_assert (t
!= NULL_TREE
);
63 /* Wrap a node in a TREE_LIST node and add it to the end of a list. */
66 gfc_chainon_list (tree list
, tree add
)
70 l
= tree_cons (NULL_TREE
, add
, NULL_TREE
);
72 return chainon (list
, l
);
76 /* Strip off a legitimate source ending from the input
77 string NAME of length LEN. */
80 remove_suffix (char *name
, int len
)
84 for (i
= 2; i
< 8 && len
> i
; i
++)
86 if (name
[len
- i
] == '.')
95 /* Creates a variable declaration with a given TYPE. */
98 gfc_create_var_np (tree type
, const char *prefix
)
100 return create_tmp_var_raw (type
, prefix
);
104 /* Like above, but also adds it to the current scope. */
107 gfc_create_var (tree type
, const char *prefix
)
111 tmp
= gfc_create_var_np (type
, prefix
);
119 /* If the an expression is not constant, evaluate it now. We assign the
120 result of the expression to an artificially created variable VAR, and
121 return a pointer to the VAR_DECL node for this variable. */
124 gfc_evaluate_now (tree expr
, stmtblock_t
* pblock
)
128 if (CONSTANT_CLASS_P (expr
))
131 var
= gfc_create_var (TREE_TYPE (expr
), NULL
);
132 gfc_add_modify_expr (pblock
, var
, expr
);
138 /* Build a MODIFY_EXPR node and add it to a given statement block PBLOCK.
139 A MODIFY_EXPR is an assignment: LHS <- RHS. */
142 gfc_add_modify_expr (stmtblock_t
* pblock
, tree lhs
, tree rhs
)
146 #ifdef ENABLE_CHECKING
147 /* Make sure that the types of the rhs and the lhs are the same
148 for scalar assignments. We should probably have something
149 similar for aggregates, but right now removing that check just
150 breaks everything. */
151 gcc_assert (TREE_TYPE (rhs
) == TREE_TYPE (lhs
)
152 || AGGREGATE_TYPE_P (TREE_TYPE (lhs
)));
155 tmp
= fold_build2 (MODIFY_EXPR
, void_type_node
, lhs
, rhs
);
156 gfc_add_expr_to_block (pblock
, tmp
);
160 /* Create a new scope/binding level and initialize a block. Care must be
161 taken when translating expressions as any temporaries will be placed in
162 the innermost scope. */
165 gfc_start_block (stmtblock_t
* block
)
167 /* Start a new binding level. */
169 block
->has_scope
= 1;
171 /* The block is empty. */
172 block
->head
= NULL_TREE
;
176 /* Initialize a block without creating a new scope. */
179 gfc_init_block (stmtblock_t
* block
)
181 block
->head
= NULL_TREE
;
182 block
->has_scope
= 0;
186 /* Sometimes we create a scope but it turns out that we don't actually
187 need it. This function merges the scope of BLOCK with its parent.
188 Only variable decls will be merged, you still need to add the code. */
191 gfc_merge_block_scope (stmtblock_t
* block
)
196 gcc_assert (block
->has_scope
);
197 block
->has_scope
= 0;
199 /* Remember the decls in this scope. */
203 /* Add them to the parent scope. */
204 while (decl
!= NULL_TREE
)
206 next
= TREE_CHAIN (decl
);
207 TREE_CHAIN (decl
) = NULL_TREE
;
215 /* Finish a scope containing a block of statements. */
218 gfc_finish_block (stmtblock_t
* stmtblock
)
224 expr
= stmtblock
->head
;
226 expr
= build_empty_stmt ();
228 stmtblock
->head
= NULL_TREE
;
230 if (stmtblock
->has_scope
)
236 block
= poplevel (1, 0, 0);
237 expr
= build3_v (BIND_EXPR
, decl
, expr
, block
);
247 /* Build an ADDR_EXPR and cast the result to TYPE. If TYPE is NULL, the
248 natural type is used. */
251 gfc_build_addr_expr (tree type
, tree t
)
253 tree base_type
= TREE_TYPE (t
);
256 if (type
&& POINTER_TYPE_P (type
)
257 && TREE_CODE (base_type
) == ARRAY_TYPE
258 && TYPE_MAIN_VARIANT (TREE_TYPE (type
))
259 == TYPE_MAIN_VARIANT (TREE_TYPE (base_type
)))
262 natural_type
= build_pointer_type (base_type
);
264 if (TREE_CODE (t
) == INDIRECT_REF
)
268 t
= TREE_OPERAND (t
, 0);
269 natural_type
= TREE_TYPE (t
);
274 TREE_ADDRESSABLE (t
) = 1;
275 t
= build1 (ADDR_EXPR
, natural_type
, t
);
278 if (type
&& natural_type
!= type
)
279 t
= convert (type
, t
);
285 /* Build an ARRAY_REF with its natural type. */
288 gfc_build_array_ref (tree base
, tree offset
)
290 tree type
= TREE_TYPE (base
);
291 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
292 type
= TREE_TYPE (type
);
295 TREE_ADDRESSABLE (base
) = 1;
297 return build4 (ARRAY_REF
, type
, base
, offset
, NULL_TREE
, NULL_TREE
);
301 /* Given a function declaration FNDECL and an argument list ARGLIST,
302 build a CALL_EXPR. */
305 gfc_build_function_call (tree fndecl
, tree arglist
)
310 fn
= gfc_build_addr_expr (NULL
, fndecl
);
311 call
= build3 (CALL_EXPR
, TREE_TYPE (TREE_TYPE (fndecl
)),
313 TREE_SIDE_EFFECTS (call
) = 1;
319 /* Generate a runtime error if COND is true. */
322 gfc_trans_runtime_check (tree cond
, tree msg
, stmtblock_t
* pblock
)
331 if (integer_zerop (cond
))
334 /* The code to generate the error. */
335 gfc_start_block (&block
);
337 gcc_assert (TREE_CODE (msg
) == STRING_CST
);
341 tmp
= gfc_build_addr_expr (pchar_type_node
, msg
);
342 args
= gfc_chainon_list (NULL_TREE
, tmp
);
344 tmp
= gfc_build_addr_expr (pchar_type_node
, gfc_strconst_current_filename
);
345 args
= gfc_chainon_list (args
, tmp
);
347 tmp
= build_int_cst (NULL_TREE
, input_line
);
348 args
= gfc_chainon_list (args
, tmp
);
350 tmp
= gfc_build_function_call (gfor_fndecl_runtime_error
, args
);
351 gfc_add_expr_to_block (&block
, tmp
);
353 body
= gfc_finish_block (&block
);
355 if (integer_onep (cond
))
357 gfc_add_expr_to_block (pblock
, body
);
361 /* Tell the compiler that this isn't likely. */
362 tmp
= gfc_chainon_list (NULL_TREE
, cond
);
363 tmp
= gfc_chainon_list (tmp
, integer_zero_node
);
364 cond
= gfc_build_function_call (built_in_decls
[BUILT_IN_EXPECT
], tmp
);
366 tmp
= build3_v (COND_EXPR
, cond
, body
, build_empty_stmt ());
367 gfc_add_expr_to_block (pblock
, tmp
);
372 /* Add a statement to a block. */
375 gfc_add_expr_to_block (stmtblock_t
* block
, tree expr
)
379 if (expr
== NULL_TREE
|| IS_EMPTY_STMT (expr
))
382 if (TREE_CODE (expr
) != STATEMENT_LIST
)
387 if (TREE_CODE (block
->head
) != STATEMENT_LIST
)
392 block
->head
= NULL_TREE
;
393 append_to_statement_list (tmp
, &block
->head
);
395 append_to_statement_list (expr
, &block
->head
);
398 /* Don't bother creating a list if we only have a single statement. */
403 /* Add a block the end of a block. */
406 gfc_add_block_to_block (stmtblock_t
* block
, stmtblock_t
* append
)
409 gcc_assert (!append
->has_scope
);
411 gfc_add_expr_to_block (block
, append
->head
);
412 append
->head
= NULL_TREE
;
416 /* Get the current locus. The structure may not be complete, and should
417 only be used with gfc_set_backend_locus. */
420 gfc_get_backend_locus (locus
* loc
)
422 loc
->lb
= gfc_getmem (sizeof (gfc_linebuf
));
423 #ifdef USE_MAPPED_LOCATION
424 loc
->lb
->location
= input_location
;
426 loc
->lb
->linenum
= input_line
;
428 loc
->lb
->file
= gfc_current_backend_file
;
432 /* Set the current locus. */
435 gfc_set_backend_locus (locus
* loc
)
437 gfc_current_backend_file
= loc
->lb
->file
;
438 #ifdef USE_MAPPED_LOCATION
439 input_location
= loc
->lb
->location
;
441 input_line
= loc
->lb
->linenum
;
442 input_filename
= loc
->lb
->file
->filename
;
447 /* Translate an executable statement. */
450 gfc_trans_code (gfc_code
* code
)
456 return build_empty_stmt ();
458 gfc_start_block (&block
);
460 /* Translate statements one by one to GIMPLE trees until we reach
461 the end of this gfc_code branch. */
462 for (; code
; code
= code
->next
)
466 res
= gfc_trans_label_here (code
);
467 gfc_add_expr_to_block (&block
, res
);
477 res
= gfc_trans_assign (code
);
480 case EXEC_LABEL_ASSIGN
:
481 res
= gfc_trans_label_assign (code
);
484 case EXEC_POINTER_ASSIGN
:
485 res
= gfc_trans_pointer_assign (code
);
493 res
= gfc_trans_cycle (code
);
497 res
= gfc_trans_exit (code
);
501 res
= gfc_trans_goto (code
);
505 res
= gfc_trans_entry (code
);
509 res
= gfc_trans_pause (code
);
513 res
= gfc_trans_stop (code
);
517 res
= gfc_trans_call (code
);
521 res
= gfc_trans_return (code
);
525 res
= gfc_trans_if (code
);
528 case EXEC_ARITHMETIC_IF
:
529 res
= gfc_trans_arithmetic_if (code
);
533 res
= gfc_trans_do (code
);
537 res
= gfc_trans_do_while (code
);
541 res
= gfc_trans_select (code
);
545 res
= gfc_trans_flush (code
);
549 res
= gfc_trans_forall (code
);
553 res
= gfc_trans_where (code
);
557 res
= gfc_trans_allocate (code
);
560 case EXEC_DEALLOCATE
:
561 res
= gfc_trans_deallocate (code
);
565 res
= gfc_trans_open (code
);
569 res
= gfc_trans_close (code
);
573 res
= gfc_trans_read (code
);
577 res
= gfc_trans_write (code
);
581 res
= gfc_trans_iolength (code
);
585 res
= gfc_trans_backspace (code
);
589 res
= gfc_trans_endfile (code
);
593 res
= gfc_trans_inquire (code
);
597 res
= gfc_trans_rewind (code
);
601 res
= gfc_trans_transfer (code
);
605 res
= gfc_trans_dt_end (code
);
609 internal_error ("gfc_trans_code(): Bad statement code");
612 gfc_set_backend_locus (&code
->loc
);
614 if (res
!= NULL_TREE
&& ! IS_EMPTY_STMT (res
))
616 if (TREE_CODE (res
) == STATEMENT_LIST
)
617 annotate_all_with_locus (&res
, input_location
);
619 SET_EXPR_LOCATION (res
, input_location
);
621 /* Add the new statement to the block. */
622 gfc_add_expr_to_block (&block
, res
);
626 /* Return the finished block. */
627 return gfc_finish_block (&block
);
631 /* This function is called after a complete program unit has been parsed
635 gfc_generate_code (gfc_namespace
* ns
)
637 if (ns
->is_block_data
)
639 gfc_generate_block_data (ns
);
643 gfc_generate_function_code (ns
);
647 /* This function is called after a complete module has been parsed
651 gfc_generate_module_code (gfc_namespace
* ns
)
655 gfc_generate_module_vars (ns
);
657 /* We need to generate all module function prototypes first, to allow
659 for (n
= ns
->contained
; n
; n
= n
->sibling
)
664 gfc_create_function_decl (n
);
667 for (n
= ns
->contained
; n
; n
= n
->sibling
)
672 gfc_generate_function_code (n
);