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)))
270 tree min_val = size_zero_node;
271 tree type_domain = TYPE_DOMAIN (base_type);
272 if (type_domain && TYPE_MIN_VALUE (type_domain))
273 min_val = TYPE_MIN_VALUE (type_domain);
274 t = build4 (ARRAY_REF, TREE_TYPE (type), t, min_val,
275 NULL_TREE, NULL_TREE);
279 natural_type = build_pointer_type (base_type);
281 if (TREE_CODE (t) == INDIRECT_REF)
285 t = TREE_OPERAND (t, 0);
286 natural_type = TREE_TYPE (t);
291 TREE_ADDRESSABLE (t) = 1;
292 t = build1 (ADDR_EXPR, natural_type, t);
295 if (type && natural_type != type)
296 t = convert (type, t);
302 /* Build an ARRAY_REF with its natural type. */
305 gfc_build_array_ref (tree base, tree offset)
307 tree type = TREE_TYPE (base);
308 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
309 type = TREE_TYPE (type);
312 TREE_ADDRESSABLE (base) = 1;
314 /* Strip NON_LVALUE_EXPR nodes. */
315 STRIP_TYPE_NOPS (offset);
317 return build4 (ARRAY_REF, type, base, offset, NULL_TREE, NULL_TREE);
321 /* Generate a runtime error if COND is true. */
324 gfc_trans_runtime_check (tree cond, const char * msgid, stmtblock_t * pblock,
334 if (integer_zerop (cond))
337 /* The code to generate the error. */
338 gfc_start_block (&block);
342 #ifdef USE_MAPPED_LOCATION
343 line = LOCATION_LINE (where->lb->location);
345 line = where->lb->linenum;
347 asprintf (&message, "At line %d of file %s", line,
348 where->lb->file->filename);
351 asprintf (&message, "In file '%s', around line %d",
352 gfc_source_file, input_line + 1);
354 arg = gfc_build_addr_expr (pchar_type_node, gfc_build_cstring_const(message));
357 asprintf (&message, "%s", _(msgid));
358 arg2 = gfc_build_addr_expr (pchar_type_node, gfc_build_cstring_const(message));
361 tmp = build_call_expr (gfor_fndecl_runtime_error_at, 2, arg, arg2);
362 gfc_add_expr_to_block (&block, tmp);
364 body = gfc_finish_block (&block);
366 if (integer_onep (cond))
368 gfc_add_expr_to_block (pblock, body);
372 /* Tell the compiler that this isn't likely. */
373 cond = fold_convert (long_integer_type_node, cond);
374 tmp = build_int_cst (long_integer_type_node, 0);
375 cond = build_call_expr (built_in_decls[BUILT_IN_EXPECT], 2, cond, tmp);
376 cond = fold_convert (boolean_type_node, cond);
378 tmp = build3_v (COND_EXPR, cond, body, build_empty_stmt ());
379 gfc_add_expr_to_block (pblock, tmp);
384 /* Call malloc to allocate size bytes of memory, with special conditions:
385 + if size < 0, generate a runtime error,
386 + if size == 0, return a NULL pointer,
387 + if malloc returns NULL, issue a runtime error. */
389 gfc_call_malloc (stmtblock_t * block, tree type, tree size)
391 tree tmp, msg, negative, zero, malloc_result, null_result, res;
394 size = gfc_evaluate_now (size, block);
396 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
397 size = fold_convert (size_type_node, size);
399 /* Create a variable to hold the result. */
400 res = gfc_create_var (pvoid_type_node, NULL);
403 negative = fold_build2 (LT_EXPR, boolean_type_node, size,
404 build_int_cst (size_type_node, 0));
405 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_cstring_const
406 ("Attempt to allocate a negative amount of memory."));
407 tmp = fold_build3 (COND_EXPR, void_type_node, negative,
408 build_call_expr (gfor_fndecl_runtime_error, 1, msg),
409 build_empty_stmt ());
410 gfc_add_expr_to_block (block, tmp);
412 /* Call malloc and check the result. */
413 gfc_start_block (&block2);
414 gfc_add_modify_expr (&block2, res,
415 build_call_expr (built_in_decls[BUILT_IN_MALLOC], 1,
417 null_result = fold_build2 (EQ_EXPR, boolean_type_node, res,
418 build_int_cst (pvoid_type_node, 0));
419 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_cstring_const
420 ("Memory allocation failed"));
421 tmp = fold_build3 (COND_EXPR, void_type_node, null_result,
422 build_call_expr (gfor_fndecl_os_error, 1, msg),
423 build_empty_stmt ());
424 gfc_add_expr_to_block (&block2, tmp);
425 malloc_result = gfc_finish_block (&block2);
428 zero = fold_build2 (EQ_EXPR, boolean_type_node, size,
429 build_int_cst (size_type_node, 0));
430 tmp = fold_build2 (MODIFY_EXPR, pvoid_type_node, res,
431 build_int_cst (pvoid_type_node, 0));
432 tmp = fold_build3 (COND_EXPR, void_type_node, zero, tmp, malloc_result);
433 gfc_add_expr_to_block (block, tmp);
436 res = fold_convert (type, res);
441 /* Free a given variable, if it's not NULL. */
443 gfc_call_free (tree var)
446 tree tmp, cond, call;
448 if (TREE_TYPE (var) != TREE_TYPE (pvoid_type_node))
449 var = fold_convert (pvoid_type_node, var);
451 gfc_start_block (&block);
452 var = gfc_evaluate_now (var, &block);
453 cond = fold_build2 (NE_EXPR, boolean_type_node, var,
454 build_int_cst (pvoid_type_node, 0));
455 call = build_call_expr (built_in_decls[BUILT_IN_FREE], 1, var);
456 tmp = fold_build3 (COND_EXPR, void_type_node, cond, call,
457 build_empty_stmt ());
458 gfc_add_expr_to_block (&block, tmp);
460 return gfc_finish_block (&block);
464 /* Add a statement to a block. */
467 gfc_add_expr_to_block (stmtblock_t * block, tree expr)
471 if (expr == NULL_TREE || IS_EMPTY_STMT (expr))
476 if (TREE_CODE (block->head) != STATEMENT_LIST)
481 block->head = NULL_TREE;
482 append_to_statement_list (tmp, &block->head);
484 append_to_statement_list (expr, &block->head);
487 /* Don't bother creating a list if we only have a single statement. */
492 /* Add a block the end of a block. */
495 gfc_add_block_to_block (stmtblock_t * block, stmtblock_t * append)
498 gcc_assert (!append->has_scope);
500 gfc_add_expr_to_block (block, append->head);
501 append->head = NULL_TREE;
505 /* Get the current locus. The structure may not be complete, and should
506 only be used with gfc_set_backend_locus. */
509 gfc_get_backend_locus (locus * loc)
511 loc->lb = gfc_getmem (sizeof (gfc_linebuf));
512 #ifdef USE_MAPPED_LOCATION
513 loc->lb->location = input_location;
515 loc->lb->linenum = input_line;
517 loc->lb->file = gfc_current_backend_file;
521 /* Set the current locus. */
524 gfc_set_backend_locus (locus * loc)
526 gfc_current_backend_file = loc->lb->file;
527 #ifdef USE_MAPPED_LOCATION
528 input_location = loc->lb->location;
530 input_line = loc->lb->linenum;
531 input_filename = loc->lb->file->filename;
536 /* Translate an executable statement. */
539 gfc_trans_code (gfc_code * code)
545 return build_empty_stmt ();
547 gfc_start_block (&block);
549 /* Translate statements one by one to GIMPLE trees until we reach
550 the end of this gfc_code branch. */
551 for (; code; code = code->next)
555 res = gfc_trans_label_here (code);
556 gfc_add_expr_to_block (&block, res);
566 res = gfc_trans_assign (code);
569 case EXEC_LABEL_ASSIGN:
570 res = gfc_trans_label_assign (code);
573 case EXEC_POINTER_ASSIGN:
574 res = gfc_trans_pointer_assign (code);
577 case EXEC_INIT_ASSIGN:
578 res = gfc_trans_init_assign (code);
586 res = gfc_trans_cycle (code);
590 res = gfc_trans_exit (code);
594 res = gfc_trans_goto (code);
598 res = gfc_trans_entry (code);
602 res = gfc_trans_pause (code);
606 res = gfc_trans_stop (code);
610 res = gfc_trans_call (code, false);
613 case EXEC_ASSIGN_CALL:
614 res = gfc_trans_call (code, true);
618 res = gfc_trans_return (code);
622 res = gfc_trans_if (code);
625 case EXEC_ARITHMETIC_IF:
626 res = gfc_trans_arithmetic_if (code);
630 res = gfc_trans_do (code);
634 res = gfc_trans_do_while (code);
638 res = gfc_trans_select (code);
642 res = gfc_trans_flush (code);
646 res = gfc_trans_forall (code);
650 res = gfc_trans_where (code);
654 res = gfc_trans_allocate (code);
657 case EXEC_DEALLOCATE:
658 res = gfc_trans_deallocate (code);
662 res = gfc_trans_open (code);
666 res = gfc_trans_close (code);
670 res = gfc_trans_read (code);
674 res = gfc_trans_write (code);
678 res = gfc_trans_iolength (code);
682 res = gfc_trans_backspace (code);
686 res = gfc_trans_endfile (code);
690 res = gfc_trans_inquire (code);
694 res = gfc_trans_rewind (code);
698 res = gfc_trans_transfer (code);
702 res = gfc_trans_dt_end (code);
705 case EXEC_OMP_ATOMIC:
706 case EXEC_OMP_BARRIER:
707 case EXEC_OMP_CRITICAL:
710 case EXEC_OMP_MASTER:
711 case EXEC_OMP_ORDERED:
712 case EXEC_OMP_PARALLEL:
713 case EXEC_OMP_PARALLEL_DO:
714 case EXEC_OMP_PARALLEL_SECTIONS:
715 case EXEC_OMP_PARALLEL_WORKSHARE:
716 case EXEC_OMP_SECTIONS:
717 case EXEC_OMP_SINGLE:
718 case EXEC_OMP_WORKSHARE:
719 res = gfc_trans_omp_directive (code);
723 internal_error ("gfc_trans_code(): Bad statement code");
726 gfc_set_backend_locus (&code->loc);
728 if (res != NULL_TREE && ! IS_EMPTY_STMT (res))
730 if (TREE_CODE (res) == STATEMENT_LIST)
731 annotate_all_with_locus (&res, input_location);
733 SET_EXPR_LOCATION (res, input_location);
735 /* Add the new statement to the block. */
736 gfc_add_expr_to_block (&block, res);
740 /* Return the finished block. */
741 return gfc_finish_block (&block);
745 /* This function is called after a complete program unit has been parsed
749 gfc_generate_code (gfc_namespace * ns)
751 if (ns->is_block_data)
753 gfc_generate_block_data (ns);
757 gfc_generate_function_code (ns);
761 /* This function is called after a complete module has been parsed
765 gfc_generate_module_code (gfc_namespace * ns)
769 gfc_generate_module_vars (ns);
771 /* We need to generate all module function prototypes first, to allow
773 for (n = ns->contained; n; n = n->sibling)
778 gfc_create_function_decl (n);
781 for (n = ns->contained; n; n = n->sibling)
786 gfc_generate_function_code (n);