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_build_pointer_plus_loc (input_location, tmp, offset);
349 tmp = fold_convert (build_pointer_type (type), tmp);
350 if (!TYPE_STRING_FLAG (type))
351 tmp = build_fold_indirect_ref_loc (input_location, tmp);
355 /* Otherwise use a straightforward array reference. */
356 return build4_loc (input_location, ARRAY_REF, type, base, offset,
357 NULL_TREE, NULL_TREE);
361 /* Generate a call to print a runtime error possibly including multiple
362 arguments and a locus. */
365 trans_runtime_error_vararg (bool error, locus* where, const char* msgid,
378 /* Compute the number of extra arguments from the format string. */
379 for (p = msgid, nargs = 0; *p; p++)
387 /* The code to generate the error. */
388 gfc_start_block (&block);
392 line = LOCATION_LINE (where->lb->location);
393 asprintf (&message, "At line %d of file %s", line,
394 where->lb->file->filename);
397 asprintf (&message, "In file '%s', around line %d",
398 gfc_source_file, input_line + 1);
400 arg = gfc_build_addr_expr (pchar_type_node,
401 gfc_build_localized_cstring_const (message));
404 asprintf (&message, "%s", _(msgid));
405 arg2 = gfc_build_addr_expr (pchar_type_node,
406 gfc_build_localized_cstring_const (message));
409 /* Build the argument array. */
410 argarray = XALLOCAVEC (tree, nargs + 2);
413 for (i = 0; i < nargs; i++)
414 argarray[2 + i] = va_arg (ap, tree);
416 /* Build the function call to runtime_(warning,error)_at; because of the
417 variable number of arguments, we can't use build_call_expr_loc dinput_location,
420 fntype = TREE_TYPE (gfor_fndecl_runtime_error_at);
422 fntype = TREE_TYPE (gfor_fndecl_runtime_warning_at);
424 loc = where ? where->lb->location : input_location;
425 tmp = fold_builtin_call_array (loc, TREE_TYPE (fntype),
426 fold_build1_loc (loc, ADDR_EXPR,
427 build_pointer_type (fntype),
429 ? gfor_fndecl_runtime_error_at
430 : gfor_fndecl_runtime_warning_at),
431 nargs + 2, argarray);
432 gfc_add_expr_to_block (&block, tmp);
434 return gfc_finish_block (&block);
439 gfc_trans_runtime_error (bool error, locus* where, const char* msgid, ...)
444 va_start (ap, msgid);
445 result = trans_runtime_error_vararg (error, where, msgid, ap);
451 /* Generate a runtime error if COND is true. */
454 gfc_trans_runtime_check (bool error, bool once, tree cond, stmtblock_t * pblock,
455 locus * where, const char * msgid, ...)
463 if (integer_zerop (cond))
468 tmpvar = gfc_create_var (boolean_type_node, "print_warning");
469 TREE_STATIC (tmpvar) = 1;
470 DECL_INITIAL (tmpvar) = boolean_true_node;
471 gfc_add_expr_to_block (pblock, tmpvar);
474 gfc_start_block (&block);
476 /* The code to generate the error. */
477 va_start (ap, msgid);
478 gfc_add_expr_to_block (&block,
479 trans_runtime_error_vararg (error, where,
483 gfc_add_modify (&block, tmpvar, boolean_false_node);
485 body = gfc_finish_block (&block);
487 if (integer_onep (cond))
489 gfc_add_expr_to_block (pblock, body);
493 /* Tell the compiler that this isn't likely. */
495 cond = fold_build2_loc (where->lb->location, TRUTH_AND_EXPR,
496 long_integer_type_node, tmpvar, cond);
498 cond = fold_convert (long_integer_type_node, cond);
500 cond = gfc_unlikely (cond);
501 tmp = fold_build3_loc (where->lb->location, COND_EXPR, void_type_node,
503 build_empty_stmt (where->lb->location));
504 gfc_add_expr_to_block (pblock, tmp);
509 /* Call malloc to allocate size bytes of memory, with special conditions:
510 + if size == 0, return a malloced area of size 1,
511 + if malloc returns NULL, issue a runtime error. */
513 gfc_call_malloc (stmtblock_t * block, tree type, tree size)
515 tree tmp, msg, malloc_result, null_result, res;
518 size = gfc_evaluate_now (size, block);
520 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
521 size = fold_convert (size_type_node, size);
523 /* Create a variable to hold the result. */
524 res = gfc_create_var (prvoid_type_node, NULL);
527 gfc_start_block (&block2);
529 size = fold_build2_loc (input_location, MAX_EXPR, size_type_node, size,
530 build_int_cst (size_type_node, 1));
532 gfc_add_modify (&block2, res,
533 fold_convert (prvoid_type_node,
534 build_call_expr_loc (input_location,
535 built_in_decls[BUILT_IN_MALLOC], 1, size)));
537 /* Optionally check whether malloc was successful. */
538 if (gfc_option.rtcheck & GFC_RTCHECK_MEM)
540 null_result = fold_build2_loc (input_location, EQ_EXPR,
541 boolean_type_node, res,
542 build_int_cst (pvoid_type_node, 0));
543 msg = gfc_build_addr_expr (pchar_type_node,
544 gfc_build_localized_cstring_const ("Memory allocation failed"));
545 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
547 build_call_expr_loc (input_location,
548 gfor_fndecl_os_error, 1, msg),
549 build_empty_stmt (input_location));
550 gfc_add_expr_to_block (&block2, tmp);
553 malloc_result = gfc_finish_block (&block2);
555 gfc_add_expr_to_block (block, malloc_result);
558 res = fold_convert (type, res);
563 /* Allocate memory, using an optional status argument.
565 This function follows the following pseudo-code:
568 allocate (size_t size, integer_type stat)
575 newmem = malloc (MAX (size, 1));
579 *stat = LIBERROR_ALLOCATION;
581 runtime_error ("Allocation would exceed memory limit");
586 gfc_allocate_using_malloc (stmtblock_t * block, tree pointer,
587 tree size, tree status)
589 tree tmp, on_error, error_cond;
590 tree status_type = status ? TREE_TYPE (status) : NULL_TREE;
592 /* Evaluate size only once, and make sure it has the right type. */
593 size = gfc_evaluate_now (size, block);
594 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
595 size = fold_convert (size_type_node, size);
597 /* If successful and stat= is given, set status to 0. */
598 if (status != NULL_TREE)
599 gfc_add_expr_to_block (block,
600 fold_build2_loc (input_location, MODIFY_EXPR, status_type,
601 status, build_int_cst (status_type, 0)));
603 /* The allocation itself. */
604 gfc_add_modify (block, pointer,
605 fold_convert (TREE_TYPE (pointer),
606 build_call_expr_loc (input_location,
607 built_in_decls[BUILT_IN_MALLOC], 1,
608 fold_build2_loc (input_location,
609 MAX_EXPR, size_type_node, size,
610 build_int_cst (size_type_node, 1)))));
612 /* What to do in case of error. */
613 if (status != NULL_TREE)
614 on_error = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
615 status, build_int_cst (status_type, LIBERROR_ALLOCATION));
617 on_error = build_call_expr_loc (input_location, gfor_fndecl_os_error, 1,
618 gfc_build_addr_expr (pchar_type_node,
619 gfc_build_localized_cstring_const
620 ("Allocation would exceed memory limit")));
622 error_cond = fold_build2_loc (input_location, EQ_EXPR,
623 boolean_type_node, pointer,
624 build_int_cst (prvoid_type_node, 0));
625 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
626 gfc_unlikely(error_cond), on_error,
627 build_empty_stmt (input_location));
629 gfc_add_expr_to_block (block, tmp);
633 /* Allocate memory, using an optional status argument.
635 This function follows the following pseudo-code:
638 allocate (size_t size, void** token, int *stat, char* errmsg, int errlen)
642 newmem = _caf_register (size, regtype, token, &stat, errmsg, errlen);
646 gfc_allocate_using_lib (stmtblock_t * block, tree pointer, tree size,
647 tree token, tree status, tree errmsg, tree errlen)
651 gcc_assert (token != NULL_TREE);
653 /* Evaluate size only once, and make sure it has the right type. */
654 size = gfc_evaluate_now (size, block);
655 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
656 size = fold_convert (size_type_node, size);
658 /* The allocation itself. */
659 if (status == NULL_TREE)
660 pstat = null_pointer_node;
662 pstat = gfc_build_addr_expr (NULL_TREE, status);
664 if (errmsg == NULL_TREE)
666 gcc_assert(errlen == NULL_TREE);
667 errmsg = null_pointer_node;
668 errlen = build_int_cst (integer_type_node, 0);
671 tmp = build_call_expr_loc (input_location,
672 gfor_fndecl_caf_register, 6,
673 fold_build2_loc (input_location,
674 MAX_EXPR, size_type_node, size,
675 build_int_cst (size_type_node, 1)),
676 build_int_cst (integer_type_node,
677 GFC_CAF_COARRAY_ALLOC),
678 token, pstat, errmsg, errlen);
680 tmp = fold_build2_loc (input_location, MODIFY_EXPR,
681 TREE_TYPE (pointer), pointer,
682 fold_convert ( TREE_TYPE (pointer), tmp));
683 gfc_add_expr_to_block (block, tmp);
687 /* Generate code for an ALLOCATE statement when the argument is an
688 allocatable variable. If the variable is currently allocated, it is an
689 error to allocate it again.
691 This function follows the following pseudo-code:
694 allocate_allocatable (void *mem, size_t size, integer_type stat)
697 return allocate (size, stat);
701 stat = LIBERROR_ALLOCATION;
703 runtime_error ("Attempting to allocate already allocated variable");
707 expr must be set to the original expression being allocated for its locus
708 and variable name in case a runtime error has to be printed. */
710 gfc_allocate_allocatable (stmtblock_t * block, tree mem, tree size, tree token,
711 tree status, tree errmsg, tree errlen, gfc_expr* expr)
713 stmtblock_t alloc_block;
714 tree tmp, null_mem, alloc, error;
715 tree type = TREE_TYPE (mem);
717 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
718 size = fold_convert (size_type_node, size);
720 null_mem = gfc_unlikely (fold_build2_loc (input_location, NE_EXPR,
721 boolean_type_node, mem,
722 build_int_cst (type, 0)));
724 /* If mem is NULL, we call gfc_allocate_using_malloc or
725 gfc_allocate_using_lib. */
726 gfc_start_block (&alloc_block);
728 if (gfc_option.coarray == GFC_FCOARRAY_LIB
729 && gfc_expr_attr (expr).codimension)
730 gfc_allocate_using_lib (&alloc_block, mem, size, token, status,
733 gfc_allocate_using_malloc (&alloc_block, mem, size, status);
735 alloc = gfc_finish_block (&alloc_block);
737 /* If mem is not NULL, we issue a runtime error or set the
743 gcc_assert (expr->expr_type == EXPR_VARIABLE && expr->symtree);
744 varname = gfc_build_cstring_const (expr->symtree->name);
745 varname = gfc_build_addr_expr (pchar_type_node, varname);
747 error = gfc_trans_runtime_error (true, &expr->where,
748 "Attempting to allocate already"
749 " allocated variable '%s'",
753 error = gfc_trans_runtime_error (true, NULL,
754 "Attempting to allocate already allocated"
757 if (status != NULL_TREE)
759 tree status_type = TREE_TYPE (status);
761 error = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
762 status, build_int_cst (status_type, LIBERROR_ALLOCATION));
765 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, null_mem,
767 gfc_add_expr_to_block (block, tmp);
771 /* Free a given variable, if it's not NULL. */
773 gfc_call_free (tree var)
776 tree tmp, cond, call;
778 if (TREE_TYPE (var) != TREE_TYPE (pvoid_type_node))
779 var = fold_convert (pvoid_type_node, var);
781 gfc_start_block (&block);
782 var = gfc_evaluate_now (var, &block);
783 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, var,
784 build_int_cst (pvoid_type_node, 0));
785 call = build_call_expr_loc (input_location,
786 built_in_decls[BUILT_IN_FREE], 1, var);
787 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, call,
788 build_empty_stmt (input_location));
789 gfc_add_expr_to_block (&block, tmp);
791 return gfc_finish_block (&block);
796 /* User-deallocate; we emit the code directly from the front-end, and the
797 logic is the same as the previous library function:
800 deallocate (void *pointer, GFC_INTEGER_4 * stat)
807 runtime_error ("Attempt to DEALLOCATE unallocated memory.");
817 In this front-end version, status doesn't have to be GFC_INTEGER_4.
818 Moreover, if CAN_FAIL is true, then we will not emit a runtime error,
819 even when no status variable is passed to us (this is used for
820 unconditional deallocation generated by the front-end at end of
823 If a runtime-message is possible, `expr' must point to the original
824 expression being deallocated for its locus and variable name. */
826 gfc_deallocate_with_status (tree pointer, tree status, bool can_fail,
829 stmtblock_t null, non_null;
830 tree cond, tmp, error;
832 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, pointer,
833 build_int_cst (TREE_TYPE (pointer), 0));
835 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
836 we emit a runtime error. */
837 gfc_start_block (&null);
842 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
844 varname = gfc_build_cstring_const (expr->symtree->name);
845 varname = gfc_build_addr_expr (pchar_type_node, varname);
847 error = gfc_trans_runtime_error (true, &expr->where,
848 "Attempt to DEALLOCATE unallocated '%s'",
852 error = build_empty_stmt (input_location);
854 if (status != NULL_TREE && !integer_zerop (status))
856 tree status_type = TREE_TYPE (TREE_TYPE (status));
859 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
860 status, build_int_cst (TREE_TYPE (status), 0));
861 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
862 fold_build1_loc (input_location, INDIRECT_REF,
863 status_type, status),
864 build_int_cst (status_type, 1));
865 error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
869 gfc_add_expr_to_block (&null, error);
871 /* When POINTER is not NULL, we free it. */
872 gfc_start_block (&non_null);
873 tmp = build_call_expr_loc (input_location,
874 built_in_decls[BUILT_IN_FREE], 1,
875 fold_convert (pvoid_type_node, pointer));
876 gfc_add_expr_to_block (&non_null, tmp);
878 if (status != NULL_TREE && !integer_zerop (status))
880 /* We set STATUS to zero if it is present. */
881 tree status_type = TREE_TYPE (TREE_TYPE (status));
884 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
885 status, build_int_cst (TREE_TYPE (status), 0));
886 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
887 fold_build1_loc (input_location, INDIRECT_REF,
888 status_type, status),
889 build_int_cst (status_type, 0));
890 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
891 tmp, build_empty_stmt (input_location));
892 gfc_add_expr_to_block (&non_null, tmp);
895 return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
896 gfc_finish_block (&null),
897 gfc_finish_block (&non_null));
901 /* Generate code for deallocation of allocatable scalars (variables or
902 components). Before the object itself is freed, any allocatable
903 subcomponents are being deallocated. */
906 gfc_deallocate_scalar_with_status (tree pointer, tree status, bool can_fail,
907 gfc_expr* expr, gfc_typespec ts)
909 stmtblock_t null, non_null;
910 tree cond, tmp, error;
912 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, pointer,
913 build_int_cst (TREE_TYPE (pointer), 0));
915 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
916 we emit a runtime error. */
917 gfc_start_block (&null);
922 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
924 varname = gfc_build_cstring_const (expr->symtree->name);
925 varname = gfc_build_addr_expr (pchar_type_node, varname);
927 error = gfc_trans_runtime_error (true, &expr->where,
928 "Attempt to DEALLOCATE unallocated '%s'",
932 error = build_empty_stmt (input_location);
934 if (status != NULL_TREE && !integer_zerop (status))
936 tree status_type = TREE_TYPE (TREE_TYPE (status));
939 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
940 status, build_int_cst (TREE_TYPE (status), 0));
941 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
942 fold_build1_loc (input_location, INDIRECT_REF,
943 status_type, status),
944 build_int_cst (status_type, 1));
945 error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
949 gfc_add_expr_to_block (&null, error);
951 /* When POINTER is not NULL, we free it. */
952 gfc_start_block (&non_null);
954 /* Free allocatable components. */
955 if (ts.type == BT_DERIVED && ts.u.derived->attr.alloc_comp)
957 tmp = build_fold_indirect_ref_loc (input_location, pointer);
958 tmp = gfc_deallocate_alloc_comp (ts.u.derived, tmp, 0);
959 gfc_add_expr_to_block (&non_null, tmp);
961 else if (ts.type == BT_CLASS
962 && ts.u.derived->components->ts.u.derived->attr.alloc_comp)
964 tmp = build_fold_indirect_ref_loc (input_location, pointer);
965 tmp = gfc_deallocate_alloc_comp (ts.u.derived->components->ts.u.derived,
967 gfc_add_expr_to_block (&non_null, tmp);
970 tmp = build_call_expr_loc (input_location,
971 built_in_decls[BUILT_IN_FREE], 1,
972 fold_convert (pvoid_type_node, pointer));
973 gfc_add_expr_to_block (&non_null, tmp);
975 if (status != NULL_TREE && !integer_zerop (status))
977 /* We set STATUS to zero if it is present. */
978 tree status_type = TREE_TYPE (TREE_TYPE (status));
981 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
982 status, build_int_cst (TREE_TYPE (status), 0));
983 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
984 fold_build1_loc (input_location, INDIRECT_REF,
985 status_type, status),
986 build_int_cst (status_type, 0));
987 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
988 tmp, build_empty_stmt (input_location));
989 gfc_add_expr_to_block (&non_null, tmp);
992 return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
993 gfc_finish_block (&null),
994 gfc_finish_block (&non_null));
998 /* Reallocate MEM so it has SIZE bytes of data. This behaves like the
999 following pseudo-code:
1002 internal_realloc (void *mem, size_t size)
1004 res = realloc (mem, size);
1005 if (!res && size != 0)
1006 _gfortran_os_error ("Allocation would exceed memory limit");
1014 gfc_call_realloc (stmtblock_t * block, tree mem, tree size)
1016 tree msg, res, nonzero, zero, null_result, tmp;
1017 tree type = TREE_TYPE (mem);
1019 size = gfc_evaluate_now (size, block);
1021 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
1022 size = fold_convert (size_type_node, size);
1024 /* Create a variable to hold the result. */
1025 res = gfc_create_var (type, NULL);
1027 /* Call realloc and check the result. */
1028 tmp = build_call_expr_loc (input_location,
1029 built_in_decls[BUILT_IN_REALLOC], 2,
1030 fold_convert (pvoid_type_node, mem), size);
1031 gfc_add_modify (block, res, fold_convert (type, tmp));
1032 null_result = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
1033 res, build_int_cst (pvoid_type_node, 0));
1034 nonzero = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, size,
1035 build_int_cst (size_type_node, 0));
1036 null_result = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
1037 null_result, nonzero);
1038 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
1039 ("Allocation would exceed memory limit"));
1040 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1042 build_call_expr_loc (input_location,
1043 gfor_fndecl_os_error, 1, msg),
1044 build_empty_stmt (input_location));
1045 gfc_add_expr_to_block (block, tmp);
1047 /* if (size == 0) then the result is NULL. */
1048 tmp = fold_build2_loc (input_location, MODIFY_EXPR, type, res,
1049 build_int_cst (type, 0));
1050 zero = fold_build1_loc (input_location, TRUTH_NOT_EXPR, boolean_type_node,
1052 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, zero, tmp,
1053 build_empty_stmt (input_location));
1054 gfc_add_expr_to_block (block, tmp);
1060 /* Add an expression to another one, either at the front or the back. */
1063 add_expr_to_chain (tree* chain, tree expr, bool front)
1065 if (expr == NULL_TREE || IS_EMPTY_STMT (expr))
1070 if (TREE_CODE (*chain) != STATEMENT_LIST)
1076 append_to_statement_list (tmp, chain);
1081 tree_stmt_iterator i;
1083 i = tsi_start (*chain);
1084 tsi_link_before (&i, expr, TSI_CONTINUE_LINKING);
1087 append_to_statement_list (expr, chain);
1094 /* Add a statement at the end of a block. */
1097 gfc_add_expr_to_block (stmtblock_t * block, tree expr)
1100 add_expr_to_chain (&block->head, expr, false);
1104 /* Add a statement at the beginning of a block. */
1107 gfc_prepend_expr_to_block (stmtblock_t * block, tree expr)
1110 add_expr_to_chain (&block->head, expr, true);
1114 /* Add a block the end of a block. */
1117 gfc_add_block_to_block (stmtblock_t * block, stmtblock_t * append)
1119 gcc_assert (append);
1120 gcc_assert (!append->has_scope);
1122 gfc_add_expr_to_block (block, append->head);
1123 append->head = NULL_TREE;
1127 /* Save the current locus. The structure may not be complete, and should
1128 only be used with gfc_restore_backend_locus. */
1131 gfc_save_backend_locus (locus * loc)
1133 loc->lb = XCNEW (gfc_linebuf);
1134 loc->lb->location = input_location;
1135 loc->lb->file = gfc_current_backend_file;
1139 /* Set the current locus. */
1142 gfc_set_backend_locus (locus * loc)
1144 gfc_current_backend_file = loc->lb->file;
1145 input_location = loc->lb->location;
1149 /* Restore the saved locus. Only used in conjonction with
1150 gfc_save_backend_locus, to free the memory when we are done. */
1153 gfc_restore_backend_locus (locus * loc)
1155 gfc_set_backend_locus (loc);
1160 /* Translate an executable statement. The tree cond is used by gfc_trans_do.
1161 This static function is wrapped by gfc_trans_code_cond and
1165 trans_code (gfc_code * code, tree cond)
1171 return build_empty_stmt (input_location);
1173 gfc_start_block (&block);
1175 /* Translate statements one by one into GENERIC trees until we reach
1176 the end of this gfc_code branch. */
1177 for (; code; code = code->next)
1179 if (code->here != 0)
1181 res = gfc_trans_label_here (code);
1182 gfc_add_expr_to_block (&block, res);
1185 gfc_set_backend_locus (&code->loc);
1190 case EXEC_END_BLOCK:
1191 case EXEC_END_PROCEDURE:
1196 if (code->expr1->ts.type == BT_CLASS)
1197 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1199 res = gfc_trans_assign (code);
1202 case EXEC_LABEL_ASSIGN:
1203 res = gfc_trans_label_assign (code);
1206 case EXEC_POINTER_ASSIGN:
1207 if (code->expr1->ts.type == BT_CLASS)
1208 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1210 res = gfc_trans_pointer_assign (code);
1213 case EXEC_INIT_ASSIGN:
1214 if (code->expr1->ts.type == BT_CLASS)
1215 res = gfc_trans_class_init_assign (code);
1217 res = gfc_trans_init_assign (code);
1225 res = gfc_trans_critical (code);
1229 res = gfc_trans_cycle (code);
1233 res = gfc_trans_exit (code);
1237 res = gfc_trans_goto (code);
1241 res = gfc_trans_entry (code);
1245 res = gfc_trans_pause (code);
1249 case EXEC_ERROR_STOP:
1250 res = gfc_trans_stop (code, code->op == EXEC_ERROR_STOP);
1254 /* For MVBITS we've got the special exception that we need a
1255 dependency check, too. */
1257 bool is_mvbits = false;
1259 if (code->resolved_isym)
1261 res = gfc_conv_intrinsic_subroutine (code);
1262 if (res != NULL_TREE)
1266 if (code->resolved_isym
1267 && code->resolved_isym->id == GFC_ISYM_MVBITS)
1270 res = gfc_trans_call (code, is_mvbits, NULL_TREE,
1276 res = gfc_trans_call (code, false, NULL_TREE,
1280 case EXEC_ASSIGN_CALL:
1281 res = gfc_trans_call (code, true, NULL_TREE,
1286 res = gfc_trans_return (code);
1290 res = gfc_trans_if (code);
1293 case EXEC_ARITHMETIC_IF:
1294 res = gfc_trans_arithmetic_if (code);
1298 res = gfc_trans_block_construct (code);
1302 res = gfc_trans_do (code, cond);
1306 res = gfc_trans_do_while (code);
1310 res = gfc_trans_select (code);
1313 case EXEC_SELECT_TYPE:
1314 /* Do nothing. SELECT TYPE statements should be transformed into
1315 an ordinary SELECT CASE at resolution stage.
1316 TODO: Add an error message here once this is done. */
1321 res = gfc_trans_flush (code);
1325 case EXEC_SYNC_IMAGES:
1326 case EXEC_SYNC_MEMORY:
1327 res = gfc_trans_sync (code, code->op);
1332 res = gfc_trans_lock_unlock (code, code->op);
1336 res = gfc_trans_forall (code);
1340 res = gfc_trans_where (code);
1344 res = gfc_trans_allocate (code);
1347 case EXEC_DEALLOCATE:
1348 res = gfc_trans_deallocate (code);
1352 res = gfc_trans_open (code);
1356 res = gfc_trans_close (code);
1360 res = gfc_trans_read (code);
1364 res = gfc_trans_write (code);
1368 res = gfc_trans_iolength (code);
1371 case EXEC_BACKSPACE:
1372 res = gfc_trans_backspace (code);
1376 res = gfc_trans_endfile (code);
1380 res = gfc_trans_inquire (code);
1384 res = gfc_trans_wait (code);
1388 res = gfc_trans_rewind (code);
1392 res = gfc_trans_transfer (code);
1396 res = gfc_trans_dt_end (code);
1399 case EXEC_OMP_ATOMIC:
1400 case EXEC_OMP_BARRIER:
1401 case EXEC_OMP_CRITICAL:
1403 case EXEC_OMP_FLUSH:
1404 case EXEC_OMP_MASTER:
1405 case EXEC_OMP_ORDERED:
1406 case EXEC_OMP_PARALLEL:
1407 case EXEC_OMP_PARALLEL_DO:
1408 case EXEC_OMP_PARALLEL_SECTIONS:
1409 case EXEC_OMP_PARALLEL_WORKSHARE:
1410 case EXEC_OMP_SECTIONS:
1411 case EXEC_OMP_SINGLE:
1413 case EXEC_OMP_TASKWAIT:
1414 case EXEC_OMP_TASKYIELD:
1415 case EXEC_OMP_WORKSHARE:
1416 res = gfc_trans_omp_directive (code);
1420 internal_error ("gfc_trans_code(): Bad statement code");
1423 gfc_set_backend_locus (&code->loc);
1425 if (res != NULL_TREE && ! IS_EMPTY_STMT (res))
1427 if (TREE_CODE (res) != STATEMENT_LIST)
1428 SET_EXPR_LOCATION (res, input_location);
1430 /* Add the new statement to the block. */
1431 gfc_add_expr_to_block (&block, res);
1435 /* Return the finished block. */
1436 return gfc_finish_block (&block);
1440 /* Translate an executable statement with condition, cond. The condition is
1441 used by gfc_trans_do to test for IO result conditions inside implied
1442 DO loops of READ and WRITE statements. See build_dt in trans-io.c. */
1445 gfc_trans_code_cond (gfc_code * code, tree cond)
1447 return trans_code (code, cond);
1450 /* Translate an executable statement without condition. */
1453 gfc_trans_code (gfc_code * code)
1455 return trans_code (code, NULL_TREE);
1459 /* This function is called after a complete program unit has been parsed
1463 gfc_generate_code (gfc_namespace * ns)
1466 if (ns->is_block_data)
1468 gfc_generate_block_data (ns);
1472 gfc_generate_function_code (ns);
1476 /* This function is called after a complete module has been parsed
1480 gfc_generate_module_code (gfc_namespace * ns)
1483 struct module_htab_entry *entry;
1485 gcc_assert (ns->proc_name->backend_decl == NULL);
1486 ns->proc_name->backend_decl
1487 = build_decl (ns->proc_name->declared_at.lb->location,
1488 NAMESPACE_DECL, get_identifier (ns->proc_name->name),
1490 entry = gfc_find_module (ns->proc_name->name);
1491 if (entry->namespace_decl)
1492 /* Buggy sourcecode, using a module before defining it? */
1493 htab_empty (entry->decls);
1494 entry->namespace_decl = ns->proc_name->backend_decl;
1496 gfc_generate_module_vars (ns);
1498 /* We need to generate all module function prototypes first, to allow
1500 for (n = ns->contained; n; n = n->sibling)
1507 gfc_create_function_decl (n, false);
1508 DECL_CONTEXT (n->proc_name->backend_decl) = ns->proc_name->backend_decl;
1509 gfc_module_add_decl (entry, n->proc_name->backend_decl);
1510 for (el = ns->entries; el; el = el->next)
1512 DECL_CONTEXT (el->sym->backend_decl) = ns->proc_name->backend_decl;
1513 gfc_module_add_decl (entry, el->sym->backend_decl);
1517 for (n = ns->contained; n; n = n->sibling)
1522 gfc_generate_function_code (n);
1527 /* Initialize an init/cleanup block with existing code. */
1530 gfc_start_wrapped_block (gfc_wrapped_block* block, tree code)
1534 block->init = NULL_TREE;
1536 block->cleanup = NULL_TREE;
1540 /* Add a new pair of initializers/clean-up code. */
1543 gfc_add_init_cleanup (gfc_wrapped_block* block, tree init, tree cleanup)
1547 /* The new pair of init/cleanup should be "wrapped around" the existing
1548 block of code, thus the initialization is added to the front and the
1549 cleanup to the back. */
1550 add_expr_to_chain (&block->init, init, true);
1551 add_expr_to_chain (&block->cleanup, cleanup, false);
1555 /* Finish up a wrapped block by building a corresponding try-finally expr. */
1558 gfc_finish_wrapped_block (gfc_wrapped_block* block)
1564 /* Build the final expression. For this, just add init and body together,
1565 and put clean-up with that into a TRY_FINALLY_EXPR. */
1566 result = block->init;
1567 add_expr_to_chain (&result, block->code, false);
1569 result = build2_loc (input_location, TRY_FINALLY_EXPR, void_type_node,
1570 result, block->cleanup);
1572 /* Clear the block. */
1573 block->init = NULL_TREE;
1574 block->code = NULL_TREE;
1575 block->cleanup = NULL_TREE;
1581 /* Helper function for marking a boolean expression tree as unlikely. */
1584 gfc_unlikely (tree cond)
1588 cond = fold_convert (long_integer_type_node, cond);
1589 tmp = build_zero_cst (long_integer_type_node);
1590 cond = build_call_expr_loc (input_location,
1591 built_in_decls[BUILT_IN_EXPECT], 2, cond, tmp);
1592 cond = fold_convert (boolean_type_node, cond);
1597 /* Helper function for marking a boolean expression tree as likely. */
1600 gfc_likely (tree cond)
1604 cond = fold_convert (long_integer_type_node, cond);
1605 tmp = build_one_cst (long_integer_type_node);
1606 cond = build_call_expr_loc (input_location,
1607 built_in_decls[BUILT_IN_EXPECT], 2, cond, tmp);
1608 cond = fold_convert (boolean_type_node, cond);