1 /* Code translation -- generate GCC trees from gfc_code.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software
4 Contributed by Paul Brook
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "coretypes.h"
27 #include "tree-gimple.h"
35 #include "trans-stmt.h"
36 #include "trans-array.h"
37 #include "trans-types.h"
38 #include "trans-const.h"
40 /* Naming convention for backend interface code:
42 gfc_trans_* translate gfc_code into STMT trees.
44 gfc_conv_* expression conversion
46 gfc_get_* get a backend tree representation of a decl or type */
48 static gfc_file
*gfc_current_backend_file
;
50 char gfc_msg_bounds
[] = N_("Array bound mismatch");
51 char gfc_msg_fault
[] = N_("Array reference out of bounds");
52 char gfc_msg_wrong_return
[] = N_("Incorrect function return value");
55 /* Advance along TREE_CHAIN n times. */
58 gfc_advance_chain (tree t
, int n
)
62 gcc_assert (t
!= NULL_TREE
);
69 /* Wrap a node in a TREE_LIST node and add it to the end of a list. */
72 gfc_chainon_list (tree list
, tree add
)
76 l
= tree_cons (NULL_TREE
, add
, NULL_TREE
);
78 return chainon (list
, l
);
82 /* Strip off a legitimate source ending from the input
83 string NAME of length LEN. */
86 remove_suffix (char *name
, int len
)
90 for (i
= 2; i
< 8 && len
> i
; i
++)
92 if (name
[len
- i
] == '.')
101 /* Creates a variable declaration with a given TYPE. */
104 gfc_create_var_np (tree type
, const char *prefix
)
106 return create_tmp_var_raw (type
, prefix
);
110 /* Like above, but also adds it to the current scope. */
113 gfc_create_var (tree type
, const char *prefix
)
117 tmp
= gfc_create_var_np (type
, prefix
);
125 /* If the an expression is not constant, evaluate it now. We assign the
126 result of the expression to an artificially created variable VAR, and
127 return a pointer to the VAR_DECL node for this variable. */
130 gfc_evaluate_now (tree expr
, stmtblock_t
* pblock
)
134 if (CONSTANT_CLASS_P (expr
))
137 var
= gfc_create_var (TREE_TYPE (expr
), NULL
);
138 gfc_add_modify_expr (pblock
, var
, expr
);
144 /* Build a MODIFY_EXPR (or GIMPLE_MODIFY_STMT) node and add it to a
145 given statement block PBLOCK. A MODIFY_EXPR is an assignment:
149 gfc_add_modify (stmtblock_t
* pblock
, tree lhs
, tree rhs
,
154 #ifdef ENABLE_CHECKING
155 /* Make sure that the types of the rhs and the lhs are the same
156 for scalar assignments. We should probably have something
157 similar for aggregates, but right now removing that check just
158 breaks everything. */
159 gcc_assert (TREE_TYPE (rhs
) == TREE_TYPE (lhs
)
160 || AGGREGATE_TYPE_P (TREE_TYPE (lhs
)));
163 tmp
= fold_build2 (tuples_p
? GIMPLE_MODIFY_STMT
: MODIFY_EXPR
,
164 void_type_node
, lhs
, rhs
);
165 gfc_add_expr_to_block (pblock
, tmp
);
169 /* Create a new scope/binding level and initialize a block. Care must be
170 taken when translating expressions as any temporaries will be placed in
171 the innermost scope. */
174 gfc_start_block (stmtblock_t
* block
)
176 /* Start a new binding level. */
178 block
->has_scope
= 1;
180 /* The block is empty. */
181 block
->head
= NULL_TREE
;
185 /* Initialize a block without creating a new scope. */
188 gfc_init_block (stmtblock_t
* block
)
190 block
->head
= NULL_TREE
;
191 block
->has_scope
= 0;
195 /* Sometimes we create a scope but it turns out that we don't actually
196 need it. This function merges the scope of BLOCK with its parent.
197 Only variable decls will be merged, you still need to add the code. */
200 gfc_merge_block_scope (stmtblock_t
* block
)
205 gcc_assert (block
->has_scope
);
206 block
->has_scope
= 0;
208 /* Remember the decls in this scope. */
212 /* Add them to the parent scope. */
213 while (decl
!= NULL_TREE
)
215 next
= TREE_CHAIN (decl
);
216 TREE_CHAIN (decl
) = NULL_TREE
;
224 /* Finish a scope containing a block of statements. */
227 gfc_finish_block (stmtblock_t
* stmtblock
)
233 expr
= stmtblock
->head
;
235 expr
= build_empty_stmt ();
237 stmtblock
->head
= NULL_TREE
;
239 if (stmtblock
->has_scope
)
245 block
= poplevel (1, 0, 0);
246 expr
= build3_v (BIND_EXPR
, decl
, expr
, block
);
256 /* Build an ADDR_EXPR and cast the result to TYPE. If TYPE is NULL, the
257 natural type is used. */
260 gfc_build_addr_expr (tree type
, tree t
)
262 tree base_type
= TREE_TYPE (t
);
265 if (type
&& POINTER_TYPE_P (type
)
266 && TREE_CODE (base_type
) == ARRAY_TYPE
267 && TYPE_MAIN_VARIANT (TREE_TYPE (type
))
268 == TYPE_MAIN_VARIANT (TREE_TYPE (base_type
)))
271 natural_type
= build_pointer_type (base_type
);
273 if (TREE_CODE (t
) == INDIRECT_REF
)
277 t
= TREE_OPERAND (t
, 0);
278 natural_type
= TREE_TYPE (t
);
283 TREE_ADDRESSABLE (t
) = 1;
284 t
= build1 (ADDR_EXPR
, natural_type
, t
);
287 if (type
&& natural_type
!= type
)
288 t
= convert (type
, t
);
294 /* Build an ARRAY_REF with its natural type. */
297 gfc_build_array_ref (tree base
, tree offset
)
299 tree type
= TREE_TYPE (base
);
300 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
301 type
= TREE_TYPE (type
);
304 TREE_ADDRESSABLE (base
) = 1;
306 /* Strip NON_LVALUE_EXPR nodes. */
307 STRIP_TYPE_NOPS (offset
);
309 return build4 (ARRAY_REF
, type
, base
, offset
, NULL_TREE
, NULL_TREE
);
313 /* Generate a runtime error if COND is true. */
316 gfc_trans_runtime_check (tree cond
, const char * msgid
, stmtblock_t
* pblock
,
326 if (integer_zerop (cond
))
329 /* The code to generate the error. */
330 gfc_start_block (&block
);
334 #ifdef USE_MAPPED_LOCATION
335 line
= LOCATION_LINE (where
->lb
->location
);
337 line
= where
->lb
->linenum
;
339 asprintf (&message
, "At line %d of file %s", line
,
340 where
->lb
->file
->filename
);
343 asprintf (&message
, "In file '%s', around line %d",
344 gfc_source_file
, input_line
+ 1);
346 arg
= gfc_build_addr_expr (pchar_type_node
, gfc_build_cstring_const(message
));
349 asprintf (&message
, "%s", _(msgid
));
350 arg2
= gfc_build_addr_expr (pchar_type_node
, gfc_build_cstring_const(message
));
353 tmp
= build_call_expr (gfor_fndecl_runtime_error_at
, 2, arg
, arg2
);
354 gfc_add_expr_to_block (&block
, tmp
);
356 body
= gfc_finish_block (&block
);
358 if (integer_onep (cond
))
360 gfc_add_expr_to_block (pblock
, body
);
364 /* Tell the compiler that this isn't likely. */
365 cond
= fold_convert (long_integer_type_node
, cond
);
366 tmp
= build_int_cst (long_integer_type_node
, 0);
367 cond
= build_call_expr (built_in_decls
[BUILT_IN_EXPECT
], 2, cond
, tmp
);
368 cond
= fold_convert (boolean_type_node
, cond
);
370 tmp
= build3_v (COND_EXPR
, cond
, body
, build_empty_stmt ());
371 gfc_add_expr_to_block (pblock
, tmp
);
376 /* Call malloc to allocate size bytes of memory, with special conditions:
377 + if size < 0, generate a runtime error,
378 + if size == 0, return a NULL pointer,
379 + if malloc returns NULL, issue a runtime error. */
381 gfc_call_malloc (stmtblock_t
* block
, tree type
, tree size
)
383 tree tmp
, msg
, negative
, zero
, malloc_result
, null_result
, res
;
386 size
= gfc_evaluate_now (size
, block
);
388 if (TREE_TYPE (size
) != TREE_TYPE (size_type_node
))
389 size
= fold_convert (size_type_node
, size
);
391 /* Create a variable to hold the result. */
392 res
= gfc_create_var (pvoid_type_node
, NULL
);
395 negative
= fold_build2 (LT_EXPR
, boolean_type_node
, size
,
396 build_int_cst (size_type_node
, 0));
397 msg
= gfc_build_addr_expr (pchar_type_node
, gfc_build_cstring_const
398 ("Attempt to allocate a negative amount of memory."));
399 tmp
= fold_build3 (COND_EXPR
, void_type_node
, negative
,
400 build_call_expr (gfor_fndecl_runtime_error
, 1, msg
),
401 build_empty_stmt ());
402 gfc_add_expr_to_block (block
, tmp
);
404 /* Call malloc and check the result. */
405 gfc_start_block (&block2
);
406 gfc_add_modify_expr (&block2
, res
,
407 build_call_expr (built_in_decls
[BUILT_IN_MALLOC
], 1,
409 null_result
= fold_build2 (EQ_EXPR
, boolean_type_node
, res
,
410 build_int_cst (pvoid_type_node
, 0));
411 msg
= gfc_build_addr_expr (pchar_type_node
, gfc_build_cstring_const
412 ("Memory allocation failed"));
413 tmp
= fold_build3 (COND_EXPR
, void_type_node
, null_result
,
414 build_call_expr (gfor_fndecl_os_error
, 1, msg
),
415 build_empty_stmt ());
416 gfc_add_expr_to_block (&block2
, tmp
);
417 malloc_result
= gfc_finish_block (&block2
);
420 zero
= fold_build2 (EQ_EXPR
, boolean_type_node
, size
,
421 build_int_cst (size_type_node
, 0));
422 tmp
= fold_build2 (MODIFY_EXPR
, pvoid_type_node
, res
,
423 build_int_cst (pvoid_type_node
, 0));
424 tmp
= fold_build3 (COND_EXPR
, void_type_node
, zero
, tmp
, malloc_result
);
425 gfc_add_expr_to_block (block
, tmp
);
428 res
= fold_convert (type
, res
);
433 /* Free a given variable, if it's not NULL. */
435 gfc_call_free (tree var
)
438 tree tmp
, cond
, call
;
440 if (TREE_TYPE (var
) != TREE_TYPE (pvoid_type_node
))
441 var
= fold_convert (pvoid_type_node
, var
);
443 gfc_start_block (&block
);
444 var
= gfc_evaluate_now (var
, &block
);
445 cond
= fold_build2 (NE_EXPR
, boolean_type_node
, var
,
446 build_int_cst (pvoid_type_node
, 0));
447 call
= build_call_expr (built_in_decls
[BUILT_IN_FREE
], 1, var
);
448 tmp
= fold_build3 (COND_EXPR
, void_type_node
, cond
, call
,
449 build_empty_stmt ());
450 gfc_add_expr_to_block (&block
, tmp
);
452 return gfc_finish_block (&block
);
456 /* Add a statement to a block. */
459 gfc_add_expr_to_block (stmtblock_t
* block
, tree expr
)
463 if (expr
== NULL_TREE
|| IS_EMPTY_STMT (expr
))
468 if (TREE_CODE (block
->head
) != STATEMENT_LIST
)
473 block
->head
= NULL_TREE
;
474 append_to_statement_list (tmp
, &block
->head
);
476 append_to_statement_list (expr
, &block
->head
);
479 /* Don't bother creating a list if we only have a single statement. */
484 /* Add a block the end of a block. */
487 gfc_add_block_to_block (stmtblock_t
* block
, stmtblock_t
* append
)
490 gcc_assert (!append
->has_scope
);
492 gfc_add_expr_to_block (block
, append
->head
);
493 append
->head
= NULL_TREE
;
497 /* Get the current locus. The structure may not be complete, and should
498 only be used with gfc_set_backend_locus. */
501 gfc_get_backend_locus (locus
* loc
)
503 loc
->lb
= gfc_getmem (sizeof (gfc_linebuf
));
504 #ifdef USE_MAPPED_LOCATION
505 loc
->lb
->location
= input_location
;
507 loc
->lb
->linenum
= input_line
;
509 loc
->lb
->file
= gfc_current_backend_file
;
513 /* Set the current locus. */
516 gfc_set_backend_locus (locus
* loc
)
518 gfc_current_backend_file
= loc
->lb
->file
;
519 #ifdef USE_MAPPED_LOCATION
520 input_location
= loc
->lb
->location
;
522 input_line
= loc
->lb
->linenum
;
523 input_filename
= loc
->lb
->file
->filename
;
528 /* Translate an executable statement. */
531 gfc_trans_code (gfc_code
* code
)
537 return build_empty_stmt ();
539 gfc_start_block (&block
);
541 /* Translate statements one by one to GIMPLE trees until we reach
542 the end of this gfc_code branch. */
543 for (; code
; code
= code
->next
)
547 res
= gfc_trans_label_here (code
);
548 gfc_add_expr_to_block (&block
, res
);
558 res
= gfc_trans_assign (code
);
561 case EXEC_LABEL_ASSIGN
:
562 res
= gfc_trans_label_assign (code
);
565 case EXEC_POINTER_ASSIGN
:
566 res
= gfc_trans_pointer_assign (code
);
569 case EXEC_INIT_ASSIGN
:
570 res
= gfc_trans_init_assign (code
);
578 res
= gfc_trans_cycle (code
);
582 res
= gfc_trans_exit (code
);
586 res
= gfc_trans_goto (code
);
590 res
= gfc_trans_entry (code
);
594 res
= gfc_trans_pause (code
);
598 res
= gfc_trans_stop (code
);
602 res
= gfc_trans_call (code
, false);
605 case EXEC_ASSIGN_CALL
:
606 res
= gfc_trans_call (code
, true);
610 res
= gfc_trans_return (code
);
614 res
= gfc_trans_if (code
);
617 case EXEC_ARITHMETIC_IF
:
618 res
= gfc_trans_arithmetic_if (code
);
622 res
= gfc_trans_do (code
);
626 res
= gfc_trans_do_while (code
);
630 res
= gfc_trans_select (code
);
634 res
= gfc_trans_flush (code
);
638 res
= gfc_trans_forall (code
);
642 res
= gfc_trans_where (code
);
646 res
= gfc_trans_allocate (code
);
649 case EXEC_DEALLOCATE
:
650 res
= gfc_trans_deallocate (code
);
654 res
= gfc_trans_open (code
);
658 res
= gfc_trans_close (code
);
662 res
= gfc_trans_read (code
);
666 res
= gfc_trans_write (code
);
670 res
= gfc_trans_iolength (code
);
674 res
= gfc_trans_backspace (code
);
678 res
= gfc_trans_endfile (code
);
682 res
= gfc_trans_inquire (code
);
686 res
= gfc_trans_rewind (code
);
690 res
= gfc_trans_transfer (code
);
694 res
= gfc_trans_dt_end (code
);
697 case EXEC_OMP_ATOMIC
:
698 case EXEC_OMP_BARRIER
:
699 case EXEC_OMP_CRITICAL
:
702 case EXEC_OMP_MASTER
:
703 case EXEC_OMP_ORDERED
:
704 case EXEC_OMP_PARALLEL
:
705 case EXEC_OMP_PARALLEL_DO
:
706 case EXEC_OMP_PARALLEL_SECTIONS
:
707 case EXEC_OMP_PARALLEL_WORKSHARE
:
708 case EXEC_OMP_SECTIONS
:
709 case EXEC_OMP_SINGLE
:
710 case EXEC_OMP_WORKSHARE
:
711 res
= gfc_trans_omp_directive (code
);
715 internal_error ("gfc_trans_code(): Bad statement code");
718 gfc_set_backend_locus (&code
->loc
);
720 if (res
!= NULL_TREE
&& ! IS_EMPTY_STMT (res
))
722 if (TREE_CODE (res
) == STATEMENT_LIST
)
723 annotate_all_with_locus (&res
, input_location
);
725 SET_EXPR_LOCATION (res
, input_location
);
727 /* Add the new statement to the block. */
728 gfc_add_expr_to_block (&block
, res
);
732 /* Return the finished block. */
733 return gfc_finish_block (&block
);
737 /* This function is called after a complete program unit has been parsed
741 gfc_generate_code (gfc_namespace
* ns
)
743 if (ns
->is_block_data
)
745 gfc_generate_block_data (ns
);
749 gfc_generate_function_code (ns
);
753 /* This function is called after a complete module has been parsed
757 gfc_generate_module_code (gfc_namespace
* ns
)
761 gfc_generate_module_vars (ns
);
763 /* We need to generate all module function prototypes first, to allow
765 for (n
= ns
->contained
; n
; n
= n
->sibling
)
770 gfc_create_function_decl (n
);
773 for (n
= ns
->contained
; n
; n
= n
->sibling
)
778 gfc_generate_function_code (n
);