1 /* Code translation -- generate GCC trees from gfc_code.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "gimple.h" /* For create_tmp_var_raw. */
27 #include "tree-iterator.h"
28 #include "diagnostic-core.h" /* For internal_error. */
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;
48 const char gfc_msg_fault[] = N_("Array reference out of bounds");
49 const char gfc_msg_wrong_return[] = N_("Incorrect function return value");
52 /* Advance along TREE_CHAIN n times. */
55 gfc_advance_chain (tree t, int n)
59 gcc_assert (t != NULL_TREE);
66 /* Strip off a legitimate source ending from the input
67 string NAME of length LEN. */
70 remove_suffix (char *name, int len)
74 for (i = 2; i < 8 && len > i; i++)
76 if (name[len - i] == '.')
85 /* Creates a variable declaration with a given TYPE. */
88 gfc_create_var_np (tree type, const char *prefix)
92 t = create_tmp_var_raw (type, prefix);
94 /* No warnings for anonymous variables. */
96 TREE_NO_WARNING (t) = 1;
102 /* Like above, but also adds it to the current scope. */
105 gfc_create_var (tree type, const char *prefix)
109 tmp = gfc_create_var_np (type, prefix);
117 /* If the expression is not constant, evaluate it now. We assign the
118 result of the expression to an artificially created variable VAR, and
119 return a pointer to the VAR_DECL node for this variable. */
122 gfc_evaluate_now_loc (location_t loc, tree expr, stmtblock_t * pblock)
126 if (CONSTANT_CLASS_P (expr))
129 var = gfc_create_var (TREE_TYPE (expr), NULL);
130 gfc_add_modify_loc (loc, pblock, var, expr);
137 gfc_evaluate_now (tree expr, stmtblock_t * pblock)
139 return gfc_evaluate_now_loc (input_location, expr, pblock);
143 /* Build a MODIFY_EXPR node and add it to a given statement block PBLOCK.
144 A MODIFY_EXPR is an assignment:
148 gfc_add_modify_loc (location_t loc, stmtblock_t * pblock, tree lhs, tree rhs)
152 #ifdef ENABLE_CHECKING
154 t1 = TREE_TYPE (rhs);
155 t2 = TREE_TYPE (lhs);
156 /* Make sure that the types of the rhs and the lhs are the same
157 for scalar assignments. We should probably have something
158 similar for aggregates, but right now removing that check just
159 breaks everything. */
161 || AGGREGATE_TYPE_P (TREE_TYPE (lhs)));
164 tmp = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, lhs,
166 gfc_add_expr_to_block (pblock, tmp);
171 gfc_add_modify (stmtblock_t * pblock, tree lhs, tree rhs)
173 gfc_add_modify_loc (input_location, pblock, lhs, rhs);
177 /* Create a new scope/binding level and initialize a block. Care must be
178 taken when translating expressions as any temporaries will be placed in
179 the innermost scope. */
182 gfc_start_block (stmtblock_t * block)
184 /* Start a new binding level. */
186 block->has_scope = 1;
188 /* The block is empty. */
189 block->head = NULL_TREE;
193 /* Initialize a block without creating a new scope. */
196 gfc_init_block (stmtblock_t * block)
198 block->head = NULL_TREE;
199 block->has_scope = 0;
203 /* Sometimes we create a scope but it turns out that we don't actually
204 need it. This function merges the scope of BLOCK with its parent.
205 Only variable decls will be merged, you still need to add the code. */
208 gfc_merge_block_scope (stmtblock_t * block)
213 gcc_assert (block->has_scope);
214 block->has_scope = 0;
216 /* Remember the decls in this scope. */
220 /* Add them to the parent scope. */
221 while (decl != NULL_TREE)
223 next = DECL_CHAIN (decl);
224 DECL_CHAIN (decl) = NULL_TREE;
232 /* Finish a scope containing a block of statements. */
235 gfc_finish_block (stmtblock_t * stmtblock)
241 expr = stmtblock->head;
243 expr = build_empty_stmt (input_location);
245 stmtblock->head = NULL_TREE;
247 if (stmtblock->has_scope)
253 block = poplevel (1, 0, 0);
254 expr = build3_v (BIND_EXPR, decl, expr, block);
264 /* Build an ADDR_EXPR and cast the result to TYPE. If TYPE is NULL, the
265 natural type is used. */
268 gfc_build_addr_expr (tree type, tree t)
270 tree base_type = TREE_TYPE (t);
273 if (type && POINTER_TYPE_P (type)
274 && TREE_CODE (base_type) == ARRAY_TYPE
275 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
276 == TYPE_MAIN_VARIANT (TREE_TYPE (base_type)))
278 tree min_val = size_zero_node;
279 tree type_domain = TYPE_DOMAIN (base_type);
280 if (type_domain && TYPE_MIN_VALUE (type_domain))
281 min_val = TYPE_MIN_VALUE (type_domain);
282 t = fold (build4_loc (input_location, ARRAY_REF, TREE_TYPE (type),
283 t, min_val, NULL_TREE, NULL_TREE));
287 natural_type = build_pointer_type (base_type);
289 if (TREE_CODE (t) == INDIRECT_REF)
293 t = TREE_OPERAND (t, 0);
294 natural_type = TREE_TYPE (t);
298 tree base = get_base_address (t);
299 if (base && DECL_P (base))
300 TREE_ADDRESSABLE (base) = 1;
301 t = fold_build1_loc (input_location, ADDR_EXPR, natural_type, t);
304 if (type && natural_type != type)
305 t = convert (type, t);
311 /* Build an ARRAY_REF with its natural type. */
314 gfc_build_array_ref (tree base, tree offset, tree decl)
316 tree type = TREE_TYPE (base);
319 if (GFC_ARRAY_TYPE_P (type) && GFC_TYPE_ARRAY_RANK (type) == 0)
321 gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
323 return fold_convert (TYPE_MAIN_VARIANT (type), base);
326 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
327 type = TREE_TYPE (type);
330 TREE_ADDRESSABLE (base) = 1;
332 /* Strip NON_LVALUE_EXPR nodes. */
333 STRIP_TYPE_NOPS (offset);
335 /* If the array reference is to a pointer, whose target contains a
336 subreference, use the span that is stored with the backend decl
337 and reference the element with pointer arithmetic. */
338 if (decl && (TREE_CODE (decl) == FIELD_DECL
339 || TREE_CODE (decl) == VAR_DECL
340 || TREE_CODE (decl) == PARM_DECL)
341 && GFC_DECL_SUBREF_ARRAY_P (decl)
342 && !integer_zerop (GFC_DECL_SPAN(decl)))
344 offset = fold_build2_loc (input_location, MULT_EXPR,
345 gfc_array_index_type,
346 offset, GFC_DECL_SPAN(decl));
347 tmp = gfc_build_addr_expr (pvoid_type_node, base);
348 tmp = fold_build2_loc (input_location, POINTER_PLUS_EXPR,
349 pvoid_type_node, tmp,
350 fold_convert (sizetype, offset));
351 tmp = fold_convert (build_pointer_type (type), tmp);
352 if (!TYPE_STRING_FLAG (type))
353 tmp = build_fold_indirect_ref_loc (input_location, tmp);
357 /* Otherwise use a straightforward array reference. */
358 return build4_loc (input_location, ARRAY_REF, type, base, offset,
359 NULL_TREE, NULL_TREE);
363 /* Generate a call to print a runtime error possibly including multiple
364 arguments and a locus. */
367 trans_runtime_error_vararg (bool error, locus* where, const char* msgid,
380 /* Compute the number of extra arguments from the format string. */
381 for (p = msgid, nargs = 0; *p; p++)
389 /* The code to generate the error. */
390 gfc_start_block (&block);
394 line = LOCATION_LINE (where->lb->location);
395 asprintf (&message, "At line %d of file %s", line,
396 where->lb->file->filename);
399 asprintf (&message, "In file '%s', around line %d",
400 gfc_source_file, input_line + 1);
402 arg = gfc_build_addr_expr (pchar_type_node,
403 gfc_build_localized_cstring_const (message));
406 asprintf (&message, "%s", _(msgid));
407 arg2 = gfc_build_addr_expr (pchar_type_node,
408 gfc_build_localized_cstring_const (message));
411 /* Build the argument array. */
412 argarray = XALLOCAVEC (tree, nargs + 2);
415 for (i = 0; i < nargs; i++)
416 argarray[2 + i] = va_arg (ap, tree);
418 /* Build the function call to runtime_(warning,error)_at; because of the
419 variable number of arguments, we can't use build_call_expr_loc dinput_location,
422 fntype = TREE_TYPE (gfor_fndecl_runtime_error_at);
424 fntype = TREE_TYPE (gfor_fndecl_runtime_warning_at);
426 loc = where ? where->lb->location : input_location;
427 tmp = fold_builtin_call_array (loc, TREE_TYPE (fntype),
428 fold_build1_loc (loc, ADDR_EXPR,
429 build_pointer_type (fntype),
431 ? gfor_fndecl_runtime_error_at
432 : gfor_fndecl_runtime_warning_at),
433 nargs + 2, argarray);
434 gfc_add_expr_to_block (&block, tmp);
436 return gfc_finish_block (&block);
441 gfc_trans_runtime_error (bool error, locus* where, const char* msgid, ...)
446 va_start (ap, msgid);
447 result = trans_runtime_error_vararg (error, where, msgid, ap);
453 /* Generate a runtime error if COND is true. */
456 gfc_trans_runtime_check (bool error, bool once, tree cond, stmtblock_t * pblock,
457 locus * where, const char * msgid, ...)
465 if (integer_zerop (cond))
470 tmpvar = gfc_create_var (boolean_type_node, "print_warning");
471 TREE_STATIC (tmpvar) = 1;
472 DECL_INITIAL (tmpvar) = boolean_true_node;
473 gfc_add_expr_to_block (pblock, tmpvar);
476 gfc_start_block (&block);
478 /* The code to generate the error. */
479 va_start (ap, msgid);
480 gfc_add_expr_to_block (&block,
481 trans_runtime_error_vararg (error, where,
485 gfc_add_modify (&block, tmpvar, boolean_false_node);
487 body = gfc_finish_block (&block);
489 if (integer_onep (cond))
491 gfc_add_expr_to_block (pblock, body);
495 /* Tell the compiler that this isn't likely. */
497 cond = fold_build2_loc (where->lb->location, TRUTH_AND_EXPR,
498 long_integer_type_node, tmpvar, cond);
500 cond = fold_convert (long_integer_type_node, cond);
502 cond = gfc_unlikely (cond);
503 tmp = fold_build3_loc (where->lb->location, COND_EXPR, void_type_node,
505 build_empty_stmt (where->lb->location));
506 gfc_add_expr_to_block (pblock, tmp);
511 /* Call malloc to allocate size bytes of memory, with special conditions:
512 + if size == 0, return a malloced area of size 1,
513 + if malloc returns NULL, issue a runtime error. */
515 gfc_call_malloc (stmtblock_t * block, tree type, tree size)
517 tree tmp, msg, malloc_result, null_result, res;
520 size = gfc_evaluate_now (size, block);
522 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
523 size = fold_convert (size_type_node, size);
525 /* Create a variable to hold the result. */
526 res = gfc_create_var (prvoid_type_node, NULL);
529 gfc_start_block (&block2);
531 size = fold_build2_loc (input_location, MAX_EXPR, size_type_node, size,
532 build_int_cst (size_type_node, 1));
534 gfc_add_modify (&block2, res,
535 fold_convert (prvoid_type_node,
536 build_call_expr_loc (input_location,
537 built_in_decls[BUILT_IN_MALLOC], 1, size)));
539 /* Optionally check whether malloc was successful. */
540 if (gfc_option.rtcheck & GFC_RTCHECK_MEM)
542 null_result = fold_build2_loc (input_location, EQ_EXPR,
543 boolean_type_node, res,
544 build_int_cst (pvoid_type_node, 0));
545 msg = gfc_build_addr_expr (pchar_type_node,
546 gfc_build_localized_cstring_const ("Memory allocation failed"));
547 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
549 build_call_expr_loc (input_location,
550 gfor_fndecl_os_error, 1, msg),
551 build_empty_stmt (input_location));
552 gfc_add_expr_to_block (&block2, tmp);
555 malloc_result = gfc_finish_block (&block2);
557 gfc_add_expr_to_block (block, malloc_result);
560 res = fold_convert (type, res);
565 /* Allocate memory, using an optional status argument.
567 This function follows the following pseudo-code:
570 allocate (size_t size, integer_type* stat)
577 newmem = malloc (MAX (size, 1));
581 *stat = LIBERROR_ALLOCATION;
583 runtime_error ("Allocation would exceed memory limit");
588 gfc_allocate_with_status (stmtblock_t * block, tree size, tree status,
591 stmtblock_t alloc_block;
592 tree res, tmp, msg, cond;
593 tree status_type = status ? TREE_TYPE (TREE_TYPE (status)) : NULL_TREE;
595 /* Evaluate size only once, and make sure it has the right type. */
596 size = gfc_evaluate_now (size, block);
597 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
598 size = fold_convert (size_type_node, size);
600 /* Create a variable to hold the result. */
601 res = gfc_create_var (prvoid_type_node, NULL);
603 /* Set the optional status variable to zero. */
604 if (status != NULL_TREE && !integer_zerop (status))
606 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
607 fold_build1_loc (input_location, INDIRECT_REF,
608 status_type, status),
609 build_int_cst (status_type, 0));
610 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
611 fold_build2_loc (input_location, NE_EXPR,
612 boolean_type_node, status,
613 build_int_cst (TREE_TYPE (status), 0)),
614 tmp, build_empty_stmt (input_location));
615 gfc_add_expr_to_block (block, tmp);
618 /* The allocation itself. */
619 gfc_start_block (&alloc_block);
622 gfc_add_modify (&alloc_block, res,
623 fold_convert (prvoid_type_node,
624 build_call_expr_loc (input_location,
625 gfor_fndecl_caf_register, 6,
626 fold_build2_loc (input_location,
627 MAX_EXPR, size_type_node, size,
628 build_int_cst (size_type_node, 1)),
629 build_int_cst (integer_type_node,
630 GFC_CAF_COARRAY_ALLOC),
631 null_pointer_node, /* token */
632 null_pointer_node, /* stat */
633 null_pointer_node, /* errmsg, errmsg_len */
634 build_int_cst (integer_type_node, 0))));
638 gfc_add_modify (&alloc_block, res,
639 fold_convert (prvoid_type_node,
640 build_call_expr_loc (input_location,
641 built_in_decls[BUILT_IN_MALLOC], 1,
642 fold_build2_loc (input_location,
643 MAX_EXPR, size_type_node, size,
644 build_int_cst (size_type_node, 1)))));
647 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
648 ("Allocation would exceed memory limit"));
649 tmp = build_call_expr_loc (input_location,
650 gfor_fndecl_os_error, 1, msg);
652 if (status != NULL_TREE && !integer_zerop (status))
654 /* Set the status variable if it's present. */
657 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
658 status, build_int_cst (TREE_TYPE (status), 0));
659 tmp2 = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
660 fold_build1_loc (input_location, INDIRECT_REF,
661 status_type, status),
662 build_int_cst (status_type, LIBERROR_ALLOCATION));
663 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
667 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
668 fold_build2_loc (input_location, EQ_EXPR,
669 boolean_type_node, res,
670 build_int_cst (prvoid_type_node, 0)),
671 tmp, build_empty_stmt (input_location));
672 gfc_add_expr_to_block (&alloc_block, tmp);
673 gfc_add_expr_to_block (block, gfc_finish_block (&alloc_block));
679 /* Generate code for an ALLOCATE statement when the argument is an
680 allocatable variable. If the variable is currently allocated, it is an
681 error to allocate it again.
683 This function follows the following pseudo-code:
686 allocate_allocatable (void *mem, size_t size, integer_type *stat)
689 return allocate (size, stat);
695 mem = allocate (size, stat);
696 *stat = LIBERROR_ALLOCATION;
700 runtime_error ("Attempting to allocate already allocated variable");
704 expr must be set to the original expression being allocated for its locus
705 and variable name in case a runtime error has to be printed. */
707 gfc_allocate_allocatable_with_status (stmtblock_t * block, tree mem, tree size,
708 tree status, gfc_expr* expr)
710 stmtblock_t alloc_block;
711 tree res, tmp, null_mem, alloc, error;
712 tree type = TREE_TYPE (mem);
714 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
715 size = fold_convert (size_type_node, size);
717 /* Create a variable to hold the result. */
718 res = gfc_create_var (type, NULL);
719 null_mem = gfc_unlikely (fold_build2_loc (input_location, NE_EXPR,
720 boolean_type_node, mem,
721 build_int_cst (type, 0)));
723 /* If mem is NULL, we call gfc_allocate_with_status. */
724 gfc_start_block (&alloc_block);
725 tmp = gfc_allocate_with_status (&alloc_block, size, status,
726 gfc_option.coarray == GFC_FCOARRAY_LIB
727 && gfc_expr_attr (expr).codimension);
729 gfc_add_modify (&alloc_block, res, fold_convert (type, tmp));
730 alloc = gfc_finish_block (&alloc_block);
732 /* If mem is not NULL, we issue a runtime error or set the
738 gcc_assert (expr->expr_type == EXPR_VARIABLE && expr->symtree);
739 varname = gfc_build_cstring_const (expr->symtree->name);
740 varname = gfc_build_addr_expr (pchar_type_node, varname);
742 error = gfc_trans_runtime_error (true, &expr->where,
743 "Attempting to allocate already"
744 " allocated variable '%s'",
748 error = gfc_trans_runtime_error (true, NULL,
749 "Attempting to allocate already allocated"
752 if (status != NULL_TREE && !integer_zerop (status))
754 tree status_type = TREE_TYPE (TREE_TYPE (status));
755 stmtblock_t set_status_block;
757 gfc_start_block (&set_status_block);
758 tmp = build_call_expr_loc (input_location,
759 built_in_decls[BUILT_IN_FREE], 1,
760 fold_convert (pvoid_type_node, mem));
761 gfc_add_expr_to_block (&set_status_block, tmp);
763 tmp = gfc_allocate_with_status (&set_status_block, size, status, false);
764 gfc_add_modify (&set_status_block, res, fold_convert (type, tmp));
766 gfc_add_modify (&set_status_block,
767 fold_build1_loc (input_location, INDIRECT_REF,
768 status_type, status),
769 build_int_cst (status_type, LIBERROR_ALLOCATION));
771 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
772 status, build_int_cst (status_type, 0));
773 error = fold_build3_loc (input_location, COND_EXPR, void_type_node, tmp,
774 error, gfc_finish_block (&set_status_block));
777 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, null_mem,
779 gfc_add_expr_to_block (block, tmp);
785 /* Free a given variable, if it's not NULL. */
787 gfc_call_free (tree var)
790 tree tmp, cond, call;
792 if (TREE_TYPE (var) != TREE_TYPE (pvoid_type_node))
793 var = fold_convert (pvoid_type_node, var);
795 gfc_start_block (&block);
796 var = gfc_evaluate_now (var, &block);
797 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, var,
798 build_int_cst (pvoid_type_node, 0));
799 call = build_call_expr_loc (input_location,
800 built_in_decls[BUILT_IN_FREE], 1, var);
801 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, call,
802 build_empty_stmt (input_location));
803 gfc_add_expr_to_block (&block, tmp);
805 return gfc_finish_block (&block);
810 /* User-deallocate; we emit the code directly from the front-end, and the
811 logic is the same as the previous library function:
814 deallocate (void *pointer, GFC_INTEGER_4 * stat)
821 runtime_error ("Attempt to DEALLOCATE unallocated memory.");
831 In this front-end version, status doesn't have to be GFC_INTEGER_4.
832 Moreover, if CAN_FAIL is true, then we will not emit a runtime error,
833 even when no status variable is passed to us (this is used for
834 unconditional deallocation generated by the front-end at end of
837 If a runtime-message is possible, `expr' must point to the original
838 expression being deallocated for its locus and variable name. */
840 gfc_deallocate_with_status (tree pointer, tree status, bool can_fail,
843 stmtblock_t null, non_null;
844 tree cond, tmp, error;
846 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, pointer,
847 build_int_cst (TREE_TYPE (pointer), 0));
849 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
850 we emit a runtime error. */
851 gfc_start_block (&null);
856 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
858 varname = gfc_build_cstring_const (expr->symtree->name);
859 varname = gfc_build_addr_expr (pchar_type_node, varname);
861 error = gfc_trans_runtime_error (true, &expr->where,
862 "Attempt to DEALLOCATE unallocated '%s'",
866 error = build_empty_stmt (input_location);
868 if (status != NULL_TREE && !integer_zerop (status))
870 tree status_type = TREE_TYPE (TREE_TYPE (status));
873 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
874 status, build_int_cst (TREE_TYPE (status), 0));
875 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
876 fold_build1_loc (input_location, INDIRECT_REF,
877 status_type, status),
878 build_int_cst (status_type, 1));
879 error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
883 gfc_add_expr_to_block (&null, error);
885 /* When POINTER is not NULL, we free it. */
886 gfc_start_block (&non_null);
887 tmp = build_call_expr_loc (input_location,
888 built_in_decls[BUILT_IN_FREE], 1,
889 fold_convert (pvoid_type_node, pointer));
890 gfc_add_expr_to_block (&non_null, tmp);
892 if (status != NULL_TREE && !integer_zerop (status))
894 /* We set STATUS to zero if it is present. */
895 tree status_type = TREE_TYPE (TREE_TYPE (status));
898 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
899 status, build_int_cst (TREE_TYPE (status), 0));
900 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
901 fold_build1_loc (input_location, INDIRECT_REF,
902 status_type, status),
903 build_int_cst (status_type, 0));
904 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
905 tmp, build_empty_stmt (input_location));
906 gfc_add_expr_to_block (&non_null, tmp);
909 return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
910 gfc_finish_block (&null),
911 gfc_finish_block (&non_null));
915 /* Generate code for deallocation of allocatable scalars (variables or
916 components). Before the object itself is freed, any allocatable
917 subcomponents are being deallocated. */
920 gfc_deallocate_scalar_with_status (tree pointer, tree status, bool can_fail,
921 gfc_expr* expr, gfc_typespec ts)
923 stmtblock_t null, non_null;
924 tree cond, tmp, error;
926 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, pointer,
927 build_int_cst (TREE_TYPE (pointer), 0));
929 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
930 we emit a runtime error. */
931 gfc_start_block (&null);
936 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
938 varname = gfc_build_cstring_const (expr->symtree->name);
939 varname = gfc_build_addr_expr (pchar_type_node, varname);
941 error = gfc_trans_runtime_error (true, &expr->where,
942 "Attempt to DEALLOCATE unallocated '%s'",
946 error = build_empty_stmt (input_location);
948 if (status != NULL_TREE && !integer_zerop (status))
950 tree status_type = TREE_TYPE (TREE_TYPE (status));
953 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
954 status, build_int_cst (TREE_TYPE (status), 0));
955 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
956 fold_build1_loc (input_location, INDIRECT_REF,
957 status_type, status),
958 build_int_cst (status_type, 1));
959 error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
963 gfc_add_expr_to_block (&null, error);
965 /* When POINTER is not NULL, we free it. */
966 gfc_start_block (&non_null);
968 /* Free allocatable components. */
969 if (ts.type == BT_DERIVED && ts.u.derived->attr.alloc_comp)
971 tmp = build_fold_indirect_ref_loc (input_location, pointer);
972 tmp = gfc_deallocate_alloc_comp (ts.u.derived, tmp, 0);
973 gfc_add_expr_to_block (&non_null, tmp);
975 else if (ts.type == BT_CLASS
976 && ts.u.derived->components->ts.u.derived->attr.alloc_comp)
978 tmp = build_fold_indirect_ref_loc (input_location, pointer);
979 tmp = gfc_deallocate_alloc_comp (ts.u.derived->components->ts.u.derived,
981 gfc_add_expr_to_block (&non_null, tmp);
984 tmp = build_call_expr_loc (input_location,
985 built_in_decls[BUILT_IN_FREE], 1,
986 fold_convert (pvoid_type_node, pointer));
987 gfc_add_expr_to_block (&non_null, tmp);
989 if (status != NULL_TREE && !integer_zerop (status))
991 /* We set STATUS to zero if it is present. */
992 tree status_type = TREE_TYPE (TREE_TYPE (status));
995 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
996 status, build_int_cst (TREE_TYPE (status), 0));
997 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
998 fold_build1_loc (input_location, INDIRECT_REF,
999 status_type, status),
1000 build_int_cst (status_type, 0));
1001 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
1002 tmp, build_empty_stmt (input_location));
1003 gfc_add_expr_to_block (&non_null, tmp);
1006 return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
1007 gfc_finish_block (&null),
1008 gfc_finish_block (&non_null));
1012 /* Reallocate MEM so it has SIZE bytes of data. This behaves like the
1013 following pseudo-code:
1016 internal_realloc (void *mem, size_t size)
1018 res = realloc (mem, size);
1019 if (!res && size != 0)
1020 _gfortran_os_error ("Allocation would exceed memory limit");
1028 gfc_call_realloc (stmtblock_t * block, tree mem, tree size)
1030 tree msg, res, nonzero, zero, null_result, tmp;
1031 tree type = TREE_TYPE (mem);
1033 size = gfc_evaluate_now (size, block);
1035 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
1036 size = fold_convert (size_type_node, size);
1038 /* Create a variable to hold the result. */
1039 res = gfc_create_var (type, NULL);
1041 /* Call realloc and check the result. */
1042 tmp = build_call_expr_loc (input_location,
1043 built_in_decls[BUILT_IN_REALLOC], 2,
1044 fold_convert (pvoid_type_node, mem), size);
1045 gfc_add_modify (block, res, fold_convert (type, tmp));
1046 null_result = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
1047 res, build_int_cst (pvoid_type_node, 0));
1048 nonzero = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, size,
1049 build_int_cst (size_type_node, 0));
1050 null_result = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
1051 null_result, nonzero);
1052 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
1053 ("Allocation would exceed memory limit"));
1054 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1056 build_call_expr_loc (input_location,
1057 gfor_fndecl_os_error, 1, msg),
1058 build_empty_stmt (input_location));
1059 gfc_add_expr_to_block (block, tmp);
1061 /* if (size == 0) then the result is NULL. */
1062 tmp = fold_build2_loc (input_location, MODIFY_EXPR, type, res,
1063 build_int_cst (type, 0));
1064 zero = fold_build1_loc (input_location, TRUTH_NOT_EXPR, boolean_type_node,
1066 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, zero, tmp,
1067 build_empty_stmt (input_location));
1068 gfc_add_expr_to_block (block, tmp);
1074 /* Add an expression to another one, either at the front or the back. */
1077 add_expr_to_chain (tree* chain, tree expr, bool front)
1079 if (expr == NULL_TREE || IS_EMPTY_STMT (expr))
1084 if (TREE_CODE (*chain) != STATEMENT_LIST)
1090 append_to_statement_list (tmp, chain);
1095 tree_stmt_iterator i;
1097 i = tsi_start (*chain);
1098 tsi_link_before (&i, expr, TSI_CONTINUE_LINKING);
1101 append_to_statement_list (expr, chain);
1108 /* Add a statement at the end of a block. */
1111 gfc_add_expr_to_block (stmtblock_t * block, tree expr)
1114 add_expr_to_chain (&block->head, expr, false);
1118 /* Add a statement at the beginning of a block. */
1121 gfc_prepend_expr_to_block (stmtblock_t * block, tree expr)
1124 add_expr_to_chain (&block->head, expr, true);
1128 /* Add a block the end of a block. */
1131 gfc_add_block_to_block (stmtblock_t * block, stmtblock_t * append)
1133 gcc_assert (append);
1134 gcc_assert (!append->has_scope);
1136 gfc_add_expr_to_block (block, append->head);
1137 append->head = NULL_TREE;
1141 /* Save the current locus. The structure may not be complete, and should
1142 only be used with gfc_restore_backend_locus. */
1145 gfc_save_backend_locus (locus * loc)
1147 loc->lb = XCNEW (gfc_linebuf);
1148 loc->lb->location = input_location;
1149 loc->lb->file = gfc_current_backend_file;
1153 /* Set the current locus. */
1156 gfc_set_backend_locus (locus * loc)
1158 gfc_current_backend_file = loc->lb->file;
1159 input_location = loc->lb->location;
1163 /* Restore the saved locus. Only used in conjonction with
1164 gfc_save_backend_locus, to free the memory when we are done. */
1167 gfc_restore_backend_locus (locus * loc)
1169 gfc_set_backend_locus (loc);
1174 /* Translate an executable statement. The tree cond is used by gfc_trans_do.
1175 This static function is wrapped by gfc_trans_code_cond and
1179 trans_code (gfc_code * code, tree cond)
1185 return build_empty_stmt (input_location);
1187 gfc_start_block (&block);
1189 /* Translate statements one by one into GENERIC trees until we reach
1190 the end of this gfc_code branch. */
1191 for (; code; code = code->next)
1193 if (code->here != 0)
1195 res = gfc_trans_label_here (code);
1196 gfc_add_expr_to_block (&block, res);
1199 gfc_set_backend_locus (&code->loc);
1204 case EXEC_END_BLOCK:
1205 case EXEC_END_PROCEDURE:
1210 if (code->expr1->ts.type == BT_CLASS)
1211 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1213 res = gfc_trans_assign (code);
1216 case EXEC_LABEL_ASSIGN:
1217 res = gfc_trans_label_assign (code);
1220 case EXEC_POINTER_ASSIGN:
1221 if (code->expr1->ts.type == BT_CLASS)
1222 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1224 res = gfc_trans_pointer_assign (code);
1227 case EXEC_INIT_ASSIGN:
1228 if (code->expr1->ts.type == BT_CLASS)
1229 res = gfc_trans_class_init_assign (code);
1231 res = gfc_trans_init_assign (code);
1239 res = gfc_trans_critical (code);
1243 res = gfc_trans_cycle (code);
1247 res = gfc_trans_exit (code);
1251 res = gfc_trans_goto (code);
1255 res = gfc_trans_entry (code);
1259 res = gfc_trans_pause (code);
1263 case EXEC_ERROR_STOP:
1264 res = gfc_trans_stop (code, code->op == EXEC_ERROR_STOP);
1268 /* For MVBITS we've got the special exception that we need a
1269 dependency check, too. */
1271 bool is_mvbits = false;
1273 if (code->resolved_isym)
1275 res = gfc_conv_intrinsic_subroutine (code);
1276 if (res != NULL_TREE)
1280 if (code->resolved_isym
1281 && code->resolved_isym->id == GFC_ISYM_MVBITS)
1284 res = gfc_trans_call (code, is_mvbits, NULL_TREE,
1290 res = gfc_trans_call (code, false, NULL_TREE,
1294 case EXEC_ASSIGN_CALL:
1295 res = gfc_trans_call (code, true, NULL_TREE,
1300 res = gfc_trans_return (code);
1304 res = gfc_trans_if (code);
1307 case EXEC_ARITHMETIC_IF:
1308 res = gfc_trans_arithmetic_if (code);
1312 res = gfc_trans_block_construct (code);
1316 res = gfc_trans_do (code, cond);
1320 res = gfc_trans_do_while (code);
1324 res = gfc_trans_select (code);
1327 case EXEC_SELECT_TYPE:
1328 /* Do nothing. SELECT TYPE statements should be transformed into
1329 an ordinary SELECT CASE at resolution stage.
1330 TODO: Add an error message here once this is done. */
1335 res = gfc_trans_flush (code);
1339 case EXEC_SYNC_IMAGES:
1340 case EXEC_SYNC_MEMORY:
1341 res = gfc_trans_sync (code, code->op);
1346 res = gfc_trans_lock_unlock (code, code->op);
1350 res = gfc_trans_forall (code);
1354 res = gfc_trans_where (code);
1358 res = gfc_trans_allocate (code);
1361 case EXEC_DEALLOCATE:
1362 res = gfc_trans_deallocate (code);
1366 res = gfc_trans_open (code);
1370 res = gfc_trans_close (code);
1374 res = gfc_trans_read (code);
1378 res = gfc_trans_write (code);
1382 res = gfc_trans_iolength (code);
1385 case EXEC_BACKSPACE:
1386 res = gfc_trans_backspace (code);
1390 res = gfc_trans_endfile (code);
1394 res = gfc_trans_inquire (code);
1398 res = gfc_trans_wait (code);
1402 res = gfc_trans_rewind (code);
1406 res = gfc_trans_transfer (code);
1410 res = gfc_trans_dt_end (code);
1413 case EXEC_OMP_ATOMIC:
1414 case EXEC_OMP_BARRIER:
1415 case EXEC_OMP_CRITICAL:
1417 case EXEC_OMP_FLUSH:
1418 case EXEC_OMP_MASTER:
1419 case EXEC_OMP_ORDERED:
1420 case EXEC_OMP_PARALLEL:
1421 case EXEC_OMP_PARALLEL_DO:
1422 case EXEC_OMP_PARALLEL_SECTIONS:
1423 case EXEC_OMP_PARALLEL_WORKSHARE:
1424 case EXEC_OMP_SECTIONS:
1425 case EXEC_OMP_SINGLE:
1427 case EXEC_OMP_TASKWAIT:
1428 case EXEC_OMP_WORKSHARE:
1429 res = gfc_trans_omp_directive (code);
1433 internal_error ("gfc_trans_code(): Bad statement code");
1436 gfc_set_backend_locus (&code->loc);
1438 if (res != NULL_TREE && ! IS_EMPTY_STMT (res))
1440 if (TREE_CODE (res) != STATEMENT_LIST)
1441 SET_EXPR_LOCATION (res, input_location);
1443 /* Add the new statement to the block. */
1444 gfc_add_expr_to_block (&block, res);
1448 /* Return the finished block. */
1449 return gfc_finish_block (&block);
1453 /* Translate an executable statement with condition, cond. The condition is
1454 used by gfc_trans_do to test for IO result conditions inside implied
1455 DO loops of READ and WRITE statements. See build_dt in trans-io.c. */
1458 gfc_trans_code_cond (gfc_code * code, tree cond)
1460 return trans_code (code, cond);
1463 /* Translate an executable statement without condition. */
1466 gfc_trans_code (gfc_code * code)
1468 return trans_code (code, NULL_TREE);
1472 /* This function is called after a complete program unit has been parsed
1476 gfc_generate_code (gfc_namespace * ns)
1479 if (ns->is_block_data)
1481 gfc_generate_block_data (ns);
1485 gfc_generate_function_code (ns);
1489 /* This function is called after a complete module has been parsed
1493 gfc_generate_module_code (gfc_namespace * ns)
1496 struct module_htab_entry *entry;
1498 gcc_assert (ns->proc_name->backend_decl == NULL);
1499 ns->proc_name->backend_decl
1500 = build_decl (ns->proc_name->declared_at.lb->location,
1501 NAMESPACE_DECL, get_identifier (ns->proc_name->name),
1503 entry = gfc_find_module (ns->proc_name->name);
1504 if (entry->namespace_decl)
1505 /* Buggy sourcecode, using a module before defining it? */
1506 htab_empty (entry->decls);
1507 entry->namespace_decl = ns->proc_name->backend_decl;
1509 gfc_generate_module_vars (ns);
1511 /* We need to generate all module function prototypes first, to allow
1513 for (n = ns->contained; n; n = n->sibling)
1520 gfc_create_function_decl (n, false);
1521 DECL_CONTEXT (n->proc_name->backend_decl) = ns->proc_name->backend_decl;
1522 gfc_module_add_decl (entry, n->proc_name->backend_decl);
1523 for (el = ns->entries; el; el = el->next)
1525 DECL_CONTEXT (el->sym->backend_decl) = ns->proc_name->backend_decl;
1526 gfc_module_add_decl (entry, el->sym->backend_decl);
1530 for (n = ns->contained; n; n = n->sibling)
1535 gfc_generate_function_code (n);
1540 /* Initialize an init/cleanup block with existing code. */
1543 gfc_start_wrapped_block (gfc_wrapped_block* block, tree code)
1547 block->init = NULL_TREE;
1549 block->cleanup = NULL_TREE;
1553 /* Add a new pair of initializers/clean-up code. */
1556 gfc_add_init_cleanup (gfc_wrapped_block* block, tree init, tree cleanup)
1560 /* The new pair of init/cleanup should be "wrapped around" the existing
1561 block of code, thus the initialization is added to the front and the
1562 cleanup to the back. */
1563 add_expr_to_chain (&block->init, init, true);
1564 add_expr_to_chain (&block->cleanup, cleanup, false);
1568 /* Finish up a wrapped block by building a corresponding try-finally expr. */
1571 gfc_finish_wrapped_block (gfc_wrapped_block* block)
1577 /* Build the final expression. For this, just add init and body together,
1578 and put clean-up with that into a TRY_FINALLY_EXPR. */
1579 result = block->init;
1580 add_expr_to_chain (&result, block->code, false);
1582 result = build2_loc (input_location, TRY_FINALLY_EXPR, void_type_node,
1583 result, block->cleanup);
1585 /* Clear the block. */
1586 block->init = NULL_TREE;
1587 block->code = NULL_TREE;
1588 block->cleanup = NULL_TREE;
1594 /* Helper function for marking a boolean expression tree as unlikely. */
1597 gfc_unlikely (tree cond)
1601 cond = fold_convert (long_integer_type_node, cond);
1602 tmp = build_zero_cst (long_integer_type_node);
1603 cond = build_call_expr_loc (input_location,
1604 built_in_decls[BUILT_IN_EXPECT], 2, cond, tmp);
1605 cond = fold_convert (boolean_type_node, cond);