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 /* Wrap a node in a TREE_LIST node and add it to the end of a list. */
69 gfc_chainon_list (tree list, tree add)
73 l = tree_cons (NULL_TREE, add, NULL_TREE);
75 return chainon (list, l);
79 /* Strip off a legitimate source ending from the input
80 string NAME of length LEN. */
83 remove_suffix (char *name, int len)
87 for (i = 2; i < 8 && len > i; i++)
89 if (name[len - i] == '.')
98 /* Creates a variable declaration with a given TYPE. */
101 gfc_create_var_np (tree type, const char *prefix)
105 t = create_tmp_var_raw (type, prefix);
107 /* No warnings for anonymous variables. */
109 TREE_NO_WARNING (t) = 1;
115 /* Like above, but also adds it to the current scope. */
118 gfc_create_var (tree type, const char *prefix)
122 tmp = gfc_create_var_np (type, prefix);
130 /* If the expression is not constant, evaluate it now. We assign the
131 result of the expression to an artificially created variable VAR, and
132 return a pointer to the VAR_DECL node for this variable. */
135 gfc_evaluate_now_loc (location_t loc, tree expr, stmtblock_t * pblock)
139 if (CONSTANT_CLASS_P (expr))
142 var = gfc_create_var (TREE_TYPE (expr), NULL);
143 gfc_add_modify_loc (loc, pblock, var, expr);
150 gfc_evaluate_now (tree expr, stmtblock_t * pblock)
152 return gfc_evaluate_now_loc (input_location, expr, pblock);
156 /* Build a MODIFY_EXPR node and add it to a given statement block PBLOCK.
157 A MODIFY_EXPR is an assignment:
161 gfc_add_modify_loc (location_t loc, stmtblock_t * pblock, tree lhs, tree rhs)
165 #ifdef ENABLE_CHECKING
167 t1 = TREE_TYPE (rhs);
168 t2 = TREE_TYPE (lhs);
169 /* Make sure that the types of the rhs and the lhs are the same
170 for scalar assignments. We should probably have something
171 similar for aggregates, but right now removing that check just
172 breaks everything. */
174 || AGGREGATE_TYPE_P (TREE_TYPE (lhs)));
177 tmp = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, lhs,
179 gfc_add_expr_to_block (pblock, tmp);
184 gfc_add_modify (stmtblock_t * pblock, tree lhs, tree rhs)
186 gfc_add_modify_loc (input_location, pblock, lhs, rhs);
190 /* Create a new scope/binding level and initialize a block. Care must be
191 taken when translating expressions as any temporaries will be placed in
192 the innermost scope. */
195 gfc_start_block (stmtblock_t * block)
197 /* Start a new binding level. */
199 block->has_scope = 1;
201 /* The block is empty. */
202 block->head = NULL_TREE;
206 /* Initialize a block without creating a new scope. */
209 gfc_init_block (stmtblock_t * block)
211 block->head = NULL_TREE;
212 block->has_scope = 0;
216 /* Sometimes we create a scope but it turns out that we don't actually
217 need it. This function merges the scope of BLOCK with its parent.
218 Only variable decls will be merged, you still need to add the code. */
221 gfc_merge_block_scope (stmtblock_t * block)
226 gcc_assert (block->has_scope);
227 block->has_scope = 0;
229 /* Remember the decls in this scope. */
233 /* Add them to the parent scope. */
234 while (decl != NULL_TREE)
236 next = DECL_CHAIN (decl);
237 DECL_CHAIN (decl) = NULL_TREE;
245 /* Finish a scope containing a block of statements. */
248 gfc_finish_block (stmtblock_t * stmtblock)
254 expr = stmtblock->head;
256 expr = build_empty_stmt (input_location);
258 stmtblock->head = NULL_TREE;
260 if (stmtblock->has_scope)
266 block = poplevel (1, 0, 0);
267 expr = build3_v (BIND_EXPR, decl, expr, block);
277 /* Build an ADDR_EXPR and cast the result to TYPE. If TYPE is NULL, the
278 natural type is used. */
281 gfc_build_addr_expr (tree type, tree t)
283 tree base_type = TREE_TYPE (t);
286 if (type && POINTER_TYPE_P (type)
287 && TREE_CODE (base_type) == ARRAY_TYPE
288 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
289 == TYPE_MAIN_VARIANT (TREE_TYPE (base_type)))
291 tree min_val = size_zero_node;
292 tree type_domain = TYPE_DOMAIN (base_type);
293 if (type_domain && TYPE_MIN_VALUE (type_domain))
294 min_val = TYPE_MIN_VALUE (type_domain);
295 t = fold (build4_loc (input_location, ARRAY_REF, TREE_TYPE (type),
296 t, min_val, NULL_TREE, NULL_TREE));
300 natural_type = build_pointer_type (base_type);
302 if (TREE_CODE (t) == INDIRECT_REF)
306 t = TREE_OPERAND (t, 0);
307 natural_type = TREE_TYPE (t);
311 tree base = get_base_address (t);
312 if (base && DECL_P (base))
313 TREE_ADDRESSABLE (base) = 1;
314 t = fold_build1_loc (input_location, ADDR_EXPR, natural_type, t);
317 if (type && natural_type != type)
318 t = convert (type, t);
324 /* Build an ARRAY_REF with its natural type. */
327 gfc_build_array_ref (tree base, tree offset, tree decl)
329 tree type = TREE_TYPE (base);
332 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
333 type = TREE_TYPE (type);
336 TREE_ADDRESSABLE (base) = 1;
338 /* Strip NON_LVALUE_EXPR nodes. */
339 STRIP_TYPE_NOPS (offset);
341 /* If the array reference is to a pointer, whose target contains a
342 subreference, use the span that is stored with the backend decl
343 and reference the element with pointer arithmetic. */
344 if (decl && (TREE_CODE (decl) == FIELD_DECL
345 || TREE_CODE (decl) == VAR_DECL
346 || TREE_CODE (decl) == PARM_DECL)
347 && GFC_DECL_SUBREF_ARRAY_P (decl)
348 && !integer_zerop (GFC_DECL_SPAN(decl)))
350 offset = fold_build2_loc (input_location, MULT_EXPR,
351 gfc_array_index_type,
352 offset, GFC_DECL_SPAN(decl));
353 tmp = gfc_build_addr_expr (pvoid_type_node, base);
354 tmp = fold_build2_loc (input_location, POINTER_PLUS_EXPR,
355 pvoid_type_node, tmp,
356 fold_convert (sizetype, offset));
357 tmp = fold_convert (build_pointer_type (type), tmp);
358 if (!TYPE_STRING_FLAG (type))
359 tmp = build_fold_indirect_ref_loc (input_location, tmp);
363 /* Otherwise use a straightforward array reference. */
364 return build4_loc (input_location, ARRAY_REF, type, base, offset,
365 NULL_TREE, NULL_TREE);
369 /* Generate a call to print a runtime error possibly including multiple
370 arguments and a locus. */
373 trans_runtime_error_vararg (bool error, locus* where, const char* msgid,
386 /* Compute the number of extra arguments from the format string. */
387 for (p = msgid, nargs = 0; *p; p++)
395 /* The code to generate the error. */
396 gfc_start_block (&block);
400 line = LOCATION_LINE (where->lb->location);
401 asprintf (&message, "At line %d of file %s", line,
402 where->lb->file->filename);
405 asprintf (&message, "In file '%s', around line %d",
406 gfc_source_file, input_line + 1);
408 arg = gfc_build_addr_expr (pchar_type_node,
409 gfc_build_localized_cstring_const (message));
412 asprintf (&message, "%s", _(msgid));
413 arg2 = gfc_build_addr_expr (pchar_type_node,
414 gfc_build_localized_cstring_const (message));
417 /* Build the argument array. */
418 argarray = XALLOCAVEC (tree, nargs + 2);
421 for (i = 0; i < nargs; i++)
422 argarray[2 + i] = va_arg (ap, tree);
424 /* Build the function call to runtime_(warning,error)_at; because of the
425 variable number of arguments, we can't use build_call_expr_loc dinput_location,
428 fntype = TREE_TYPE (gfor_fndecl_runtime_error_at);
430 fntype = TREE_TYPE (gfor_fndecl_runtime_warning_at);
432 loc = where ? where->lb->location : input_location;
433 tmp = fold_builtin_call_array (loc, TREE_TYPE (fntype),
434 fold_build1_loc (loc, ADDR_EXPR,
435 build_pointer_type (fntype),
437 ? gfor_fndecl_runtime_error_at
438 : gfor_fndecl_runtime_warning_at),
439 nargs + 2, argarray);
440 gfc_add_expr_to_block (&block, tmp);
442 return gfc_finish_block (&block);
447 gfc_trans_runtime_error (bool error, locus* where, const char* msgid, ...)
452 va_start (ap, msgid);
453 result = trans_runtime_error_vararg (error, where, msgid, ap);
459 /* Generate a runtime error if COND is true. */
462 gfc_trans_runtime_check (bool error, bool once, tree cond, stmtblock_t * pblock,
463 locus * where, const char * msgid, ...)
471 if (integer_zerop (cond))
476 tmpvar = gfc_create_var (boolean_type_node, "print_warning");
477 TREE_STATIC (tmpvar) = 1;
478 DECL_INITIAL (tmpvar) = boolean_true_node;
479 gfc_add_expr_to_block (pblock, tmpvar);
482 gfc_start_block (&block);
484 /* The code to generate the error. */
485 va_start (ap, msgid);
486 gfc_add_expr_to_block (&block,
487 trans_runtime_error_vararg (error, where,
491 gfc_add_modify (&block, tmpvar, boolean_false_node);
493 body = gfc_finish_block (&block);
495 if (integer_onep (cond))
497 gfc_add_expr_to_block (pblock, body);
501 /* Tell the compiler that this isn't likely. */
503 cond = fold_build2_loc (where->lb->location, TRUTH_AND_EXPR,
504 long_integer_type_node, tmpvar, cond);
506 cond = fold_convert (long_integer_type_node, cond);
508 tmp = build_int_cst (long_integer_type_node, 0);
509 cond = build_call_expr_loc (where->lb->location,
510 built_in_decls[BUILT_IN_EXPECT], 2, cond, tmp);
511 cond = fold_convert (boolean_type_node, cond);
513 tmp = fold_build3_loc (where->lb->location, COND_EXPR, void_type_node,
515 build_empty_stmt (where->lb->location));
516 gfc_add_expr_to_block (pblock, tmp);
521 /* Call malloc to allocate size bytes of memory, with special conditions:
522 + if size <= 0, return a malloced area of size 1,
523 + if malloc returns NULL, issue a runtime error. */
525 gfc_call_malloc (stmtblock_t * block, tree type, tree size)
527 tree tmp, msg, malloc_result, null_result, res;
530 size = gfc_evaluate_now (size, block);
532 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
533 size = fold_convert (size_type_node, size);
535 /* Create a variable to hold the result. */
536 res = gfc_create_var (prvoid_type_node, NULL);
539 gfc_start_block (&block2);
541 size = fold_build2_loc (input_location, MAX_EXPR, size_type_node, size,
542 build_int_cst (size_type_node, 1));
544 gfc_add_modify (&block2, res,
545 fold_convert (prvoid_type_node,
546 build_call_expr_loc (input_location,
547 built_in_decls[BUILT_IN_MALLOC], 1, size)));
549 /* Optionally check whether malloc was successful. */
550 if (gfc_option.rtcheck & GFC_RTCHECK_MEM)
552 null_result = fold_build2_loc (input_location, EQ_EXPR,
553 boolean_type_node, res,
554 build_int_cst (pvoid_type_node, 0));
555 msg = gfc_build_addr_expr (pchar_type_node,
556 gfc_build_localized_cstring_const ("Memory allocation failed"));
557 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
559 build_call_expr_loc (input_location,
560 gfor_fndecl_os_error, 1, msg),
561 build_empty_stmt (input_location));
562 gfc_add_expr_to_block (&block2, tmp);
565 malloc_result = gfc_finish_block (&block2);
567 gfc_add_expr_to_block (block, malloc_result);
570 res = fold_convert (type, res);
575 /* Allocate memory, using an optional status argument.
577 This function follows the following pseudo-code:
580 allocate (size_t size, integer_type* stat)
587 // The only time this can happen is the size wraps around.
592 *stat = LIBERROR_ALLOCATION;
596 runtime_error ("Attempt to allocate negative amount of memory. "
597 "Possible integer overflow");
601 newmem = malloc (MAX (size, 1));
605 *stat = LIBERROR_ALLOCATION;
607 runtime_error ("Out of memory");
614 gfc_allocate_with_status (stmtblock_t * block, tree size, tree status)
616 stmtblock_t alloc_block;
617 tree res, tmp, error, msg, cond;
618 tree status_type = status ? TREE_TYPE (TREE_TYPE (status)) : NULL_TREE;
620 /* Evaluate size only once, and make sure it has the right type. */
621 size = gfc_evaluate_now (size, block);
622 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
623 size = fold_convert (size_type_node, size);
625 /* Create a variable to hold the result. */
626 res = gfc_create_var (prvoid_type_node, NULL);
628 /* Set the optional status variable to zero. */
629 if (status != NULL_TREE && !integer_zerop (status))
631 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
632 fold_build1_loc (input_location, INDIRECT_REF,
633 status_type, status),
634 build_int_cst (status_type, 0));
635 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
636 fold_build2_loc (input_location, NE_EXPR,
637 boolean_type_node, status,
638 build_int_cst (TREE_TYPE (status), 0)),
639 tmp, build_empty_stmt (input_location));
640 gfc_add_expr_to_block (block, tmp);
643 /* Generate the block of code handling (size < 0). */
644 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
645 ("Attempt to allocate negative amount of memory. "
646 "Possible integer overflow"));
647 error = build_call_expr_loc (input_location,
648 gfor_fndecl_runtime_error, 1, msg);
650 if (status != NULL_TREE && !integer_zerop (status))
652 /* Set the status variable if it's present. */
653 stmtblock_t set_status_block;
655 gfc_start_block (&set_status_block);
656 gfc_add_modify (&set_status_block,
657 fold_build1_loc (input_location, INDIRECT_REF,
658 status_type, status),
659 build_int_cst (status_type, LIBERROR_ALLOCATION));
660 gfc_add_modify (&set_status_block, res,
661 build_int_cst (prvoid_type_node, 0));
663 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
664 status, build_int_cst (TREE_TYPE (status), 0));
665 error = fold_build3_loc (input_location, COND_EXPR, void_type_node, tmp,
666 error, gfc_finish_block (&set_status_block));
669 /* The allocation itself. */
670 gfc_start_block (&alloc_block);
671 gfc_add_modify (&alloc_block, res,
672 fold_convert (prvoid_type_node,
673 build_call_expr_loc (input_location,
674 built_in_decls[BUILT_IN_MALLOC], 1,
675 fold_build2_loc (input_location,
676 MAX_EXPR, size_type_node, size,
677 build_int_cst (size_type_node,
680 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
682 tmp = build_call_expr_loc (input_location,
683 gfor_fndecl_os_error, 1, msg);
685 if (status != NULL_TREE && !integer_zerop (status))
687 /* Set the status variable if it's present. */
690 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
691 status, build_int_cst (TREE_TYPE (status), 0));
692 tmp2 = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
693 fold_build1_loc (input_location, INDIRECT_REF,
694 status_type, status),
695 build_int_cst (status_type, LIBERROR_ALLOCATION));
696 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
700 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
701 fold_build2_loc (input_location, EQ_EXPR,
702 boolean_type_node, res,
703 build_int_cst (prvoid_type_node, 0)),
704 tmp, build_empty_stmt (input_location));
705 gfc_add_expr_to_block (&alloc_block, tmp);
707 cond = fold_build2_loc (input_location, LT_EXPR, boolean_type_node, size,
708 build_int_cst (TREE_TYPE (size), 0));
709 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, error,
710 gfc_finish_block (&alloc_block));
711 gfc_add_expr_to_block (block, tmp);
717 /* Generate code for an ALLOCATE statement when the argument is an
718 allocatable array. If the array is currently allocated, it is an
719 error to allocate it again.
721 This function follows the following pseudo-code:
724 allocate_array (void *mem, size_t size, integer_type *stat)
727 return allocate (size, stat);
733 mem = allocate (size, stat);
734 *stat = LIBERROR_ALLOCATION;
738 runtime_error ("Attempting to allocate already allocated variable");
742 expr must be set to the original expression being allocated for its locus
743 and variable name in case a runtime error has to be printed. */
745 gfc_allocate_array_with_status (stmtblock_t * block, tree mem, tree size,
746 tree status, gfc_expr* expr)
748 stmtblock_t alloc_block;
749 tree res, tmp, null_mem, alloc, error;
750 tree type = TREE_TYPE (mem);
752 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
753 size = fold_convert (size_type_node, size);
755 /* Create a variable to hold the result. */
756 res = gfc_create_var (type, NULL);
757 null_mem = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, mem,
758 build_int_cst (type, 0));
760 /* If mem is NULL, we call gfc_allocate_with_status. */
761 gfc_start_block (&alloc_block);
762 tmp = gfc_allocate_with_status (&alloc_block, size, status);
763 gfc_add_modify (&alloc_block, res, fold_convert (type, tmp));
764 alloc = gfc_finish_block (&alloc_block);
766 /* Otherwise, we issue a runtime error or set the status variable. */
771 gcc_assert (expr->expr_type == EXPR_VARIABLE && expr->symtree);
772 varname = gfc_build_cstring_const (expr->symtree->name);
773 varname = gfc_build_addr_expr (pchar_type_node, varname);
775 error = gfc_trans_runtime_error (true, &expr->where,
776 "Attempting to allocate already"
777 " allocated variable '%s'",
781 error = gfc_trans_runtime_error (true, NULL,
782 "Attempting to allocate already allocated"
785 if (status != NULL_TREE && !integer_zerop (status))
787 tree status_type = TREE_TYPE (TREE_TYPE (status));
788 stmtblock_t set_status_block;
790 gfc_start_block (&set_status_block);
791 tmp = build_call_expr_loc (input_location,
792 built_in_decls[BUILT_IN_FREE], 1,
793 fold_convert (pvoid_type_node, mem));
794 gfc_add_expr_to_block (&set_status_block, tmp);
796 tmp = gfc_allocate_with_status (&set_status_block, size, status);
797 gfc_add_modify (&set_status_block, res, fold_convert (type, tmp));
799 gfc_add_modify (&set_status_block,
800 fold_build1_loc (input_location, INDIRECT_REF,
801 status_type, status),
802 build_int_cst (status_type, LIBERROR_ALLOCATION));
804 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
805 status, build_int_cst (status_type, 0));
806 error = fold_build3_loc (input_location, COND_EXPR, void_type_node, tmp,
807 error, gfc_finish_block (&set_status_block));
810 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, null_mem,
812 gfc_add_expr_to_block (block, tmp);
818 /* Free a given variable, if it's not NULL. */
820 gfc_call_free (tree var)
823 tree tmp, cond, call;
825 if (TREE_TYPE (var) != TREE_TYPE (pvoid_type_node))
826 var = fold_convert (pvoid_type_node, var);
828 gfc_start_block (&block);
829 var = gfc_evaluate_now (var, &block);
830 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, var,
831 build_int_cst (pvoid_type_node, 0));
832 call = build_call_expr_loc (input_location,
833 built_in_decls[BUILT_IN_FREE], 1, var);
834 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, call,
835 build_empty_stmt (input_location));
836 gfc_add_expr_to_block (&block, tmp);
838 return gfc_finish_block (&block);
843 /* User-deallocate; we emit the code directly from the front-end, and the
844 logic is the same as the previous library function:
847 deallocate (void *pointer, GFC_INTEGER_4 * stat)
854 runtime_error ("Attempt to DEALLOCATE unallocated memory.");
864 In this front-end version, status doesn't have to be GFC_INTEGER_4.
865 Moreover, if CAN_FAIL is true, then we will not emit a runtime error,
866 even when no status variable is passed to us (this is used for
867 unconditional deallocation generated by the front-end at end of
870 If a runtime-message is possible, `expr' must point to the original
871 expression being deallocated for its locus and variable name. */
873 gfc_deallocate_with_status (tree pointer, tree status, bool can_fail,
876 stmtblock_t null, non_null;
877 tree cond, tmp, error;
879 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, pointer,
880 build_int_cst (TREE_TYPE (pointer), 0));
882 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
883 we emit a runtime error. */
884 gfc_start_block (&null);
889 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
891 varname = gfc_build_cstring_const (expr->symtree->name);
892 varname = gfc_build_addr_expr (pchar_type_node, varname);
894 error = gfc_trans_runtime_error (true, &expr->where,
895 "Attempt to DEALLOCATE unallocated '%s'",
899 error = build_empty_stmt (input_location);
901 if (status != NULL_TREE && !integer_zerop (status))
903 tree status_type = TREE_TYPE (TREE_TYPE (status));
906 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
907 status, build_int_cst (TREE_TYPE (status), 0));
908 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
909 fold_build1_loc (input_location, INDIRECT_REF,
910 status_type, status),
911 build_int_cst (status_type, 1));
912 error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
916 gfc_add_expr_to_block (&null, error);
918 /* When POINTER is not NULL, we free it. */
919 gfc_start_block (&non_null);
920 tmp = build_call_expr_loc (input_location,
921 built_in_decls[BUILT_IN_FREE], 1,
922 fold_convert (pvoid_type_node, pointer));
923 gfc_add_expr_to_block (&non_null, tmp);
925 if (status != NULL_TREE && !integer_zerop (status))
927 /* We set STATUS to zero if it is present. */
928 tree status_type = TREE_TYPE (TREE_TYPE (status));
931 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
932 status, build_int_cst (TREE_TYPE (status), 0));
933 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
934 fold_build1_loc (input_location, INDIRECT_REF,
935 status_type, status),
936 build_int_cst (status_type, 0));
937 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
938 tmp, build_empty_stmt (input_location));
939 gfc_add_expr_to_block (&non_null, tmp);
942 return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
943 gfc_finish_block (&null),
944 gfc_finish_block (&non_null));
948 /* Reallocate MEM so it has SIZE bytes of data. This behaves like the
949 following pseudo-code:
952 internal_realloc (void *mem, size_t size)
955 runtime_error ("Attempt to allocate a negative amount of memory.");
956 res = realloc (mem, size);
957 if (!res && size != 0)
958 _gfortran_os_error ("Out of memory");
966 gfc_call_realloc (stmtblock_t * block, tree mem, tree size)
968 tree msg, res, negative, nonzero, zero, null_result, tmp;
969 tree type = TREE_TYPE (mem);
971 size = gfc_evaluate_now (size, block);
973 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
974 size = fold_convert (size_type_node, size);
976 /* Create a variable to hold the result. */
977 res = gfc_create_var (type, NULL);
980 negative = fold_build2_loc (input_location, LT_EXPR, boolean_type_node, size,
981 build_int_cst (size_type_node, 0));
982 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
983 ("Attempt to allocate a negative amount of memory."));
984 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, negative,
985 build_call_expr_loc (input_location,
986 gfor_fndecl_runtime_error, 1, msg),
987 build_empty_stmt (input_location));
988 gfc_add_expr_to_block (block, tmp);
990 /* Call realloc and check the result. */
991 tmp = build_call_expr_loc (input_location,
992 built_in_decls[BUILT_IN_REALLOC], 2,
993 fold_convert (pvoid_type_node, mem), size);
994 gfc_add_modify (block, res, fold_convert (type, tmp));
995 null_result = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
996 res, build_int_cst (pvoid_type_node, 0));
997 nonzero = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, size,
998 build_int_cst (size_type_node, 0));
999 null_result = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
1000 null_result, nonzero);
1001 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
1003 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1005 build_call_expr_loc (input_location,
1006 gfor_fndecl_os_error, 1, msg),
1007 build_empty_stmt (input_location));
1008 gfc_add_expr_to_block (block, tmp);
1010 /* if (size == 0) then the result is NULL. */
1011 tmp = fold_build2_loc (input_location, MODIFY_EXPR, type, res,
1012 build_int_cst (type, 0));
1013 zero = fold_build1_loc (input_location, TRUTH_NOT_EXPR, boolean_type_node,
1015 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, zero, tmp,
1016 build_empty_stmt (input_location));
1017 gfc_add_expr_to_block (block, tmp);
1023 /* Add an expression to another one, either at the front or the back. */
1026 add_expr_to_chain (tree* chain, tree expr, bool front)
1028 if (expr == NULL_TREE || IS_EMPTY_STMT (expr))
1033 if (TREE_CODE (*chain) != STATEMENT_LIST)
1039 append_to_statement_list (tmp, chain);
1044 tree_stmt_iterator i;
1046 i = tsi_start (*chain);
1047 tsi_link_before (&i, expr, TSI_CONTINUE_LINKING);
1050 append_to_statement_list (expr, chain);
1056 /* Add a statement to a block. */
1059 gfc_add_expr_to_block (stmtblock_t * block, tree expr)
1062 add_expr_to_chain (&block->head, expr, false);
1066 /* Add a block the end of a block. */
1069 gfc_add_block_to_block (stmtblock_t * block, stmtblock_t * append)
1071 gcc_assert (append);
1072 gcc_assert (!append->has_scope);
1074 gfc_add_expr_to_block (block, append->head);
1075 append->head = NULL_TREE;
1079 /* Save the current locus. The structure may not be complete, and should
1080 only be used with gfc_restore_backend_locus. */
1083 gfc_save_backend_locus (locus * loc)
1085 loc->lb = XCNEW (gfc_linebuf);
1086 loc->lb->location = input_location;
1087 loc->lb->file = gfc_current_backend_file;
1091 /* Set the current locus. */
1094 gfc_set_backend_locus (locus * loc)
1096 gfc_current_backend_file = loc->lb->file;
1097 input_location = loc->lb->location;
1101 /* Restore the saved locus. Only used in conjonction with
1102 gfc_save_backend_locus, to free the memory when we are done. */
1105 gfc_restore_backend_locus (locus * loc)
1107 gfc_set_backend_locus (loc);
1112 /* Translate an executable statement. The tree cond is used by gfc_trans_do.
1113 This static function is wrapped by gfc_trans_code_cond and
1117 trans_code (gfc_code * code, tree cond)
1123 return build_empty_stmt (input_location);
1125 gfc_start_block (&block);
1127 /* Translate statements one by one into GENERIC trees until we reach
1128 the end of this gfc_code branch. */
1129 for (; code; code = code->next)
1131 if (code->here != 0)
1133 res = gfc_trans_label_here (code);
1134 gfc_add_expr_to_block (&block, res);
1137 gfc_set_backend_locus (&code->loc);
1142 case EXEC_END_BLOCK:
1143 case EXEC_END_PROCEDURE:
1148 if (code->expr1->ts.type == BT_CLASS)
1149 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1151 res = gfc_trans_assign (code);
1154 case EXEC_LABEL_ASSIGN:
1155 res = gfc_trans_label_assign (code);
1158 case EXEC_POINTER_ASSIGN:
1159 if (code->expr1->ts.type == BT_CLASS)
1160 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1162 res = gfc_trans_pointer_assign (code);
1165 case EXEC_INIT_ASSIGN:
1166 if (code->expr1->ts.type == BT_CLASS)
1167 res = gfc_trans_class_init_assign (code);
1169 res = gfc_trans_init_assign (code);
1177 res = gfc_trans_critical (code);
1181 res = gfc_trans_cycle (code);
1185 res = gfc_trans_exit (code);
1189 res = gfc_trans_goto (code);
1193 res = gfc_trans_entry (code);
1197 res = gfc_trans_pause (code);
1201 case EXEC_ERROR_STOP:
1202 res = gfc_trans_stop (code, code->op == EXEC_ERROR_STOP);
1206 /* For MVBITS we've got the special exception that we need a
1207 dependency check, too. */
1209 bool is_mvbits = false;
1210 if (code->resolved_isym
1211 && code->resolved_isym->id == GFC_ISYM_MVBITS)
1213 if (code->resolved_isym
1214 && code->resolved_isym->id == GFC_ISYM_MOVE_ALLOC)
1215 res = gfc_conv_intrinsic_move_alloc (code);
1217 res = gfc_trans_call (code, is_mvbits, NULL_TREE,
1223 res = gfc_trans_call (code, false, NULL_TREE,
1227 case EXEC_ASSIGN_CALL:
1228 res = gfc_trans_call (code, true, NULL_TREE,
1233 res = gfc_trans_return (code);
1237 res = gfc_trans_if (code);
1240 case EXEC_ARITHMETIC_IF:
1241 res = gfc_trans_arithmetic_if (code);
1245 res = gfc_trans_block_construct (code);
1249 res = gfc_trans_do (code, cond);
1253 res = gfc_trans_do_while (code);
1257 res = gfc_trans_select (code);
1260 case EXEC_SELECT_TYPE:
1261 /* Do nothing. SELECT TYPE statements should be transformed into
1262 an ordinary SELECT CASE at resolution stage.
1263 TODO: Add an error message here once this is done. */
1268 res = gfc_trans_flush (code);
1272 case EXEC_SYNC_IMAGES:
1273 case EXEC_SYNC_MEMORY:
1274 res = gfc_trans_sync (code, code->op);
1278 res = gfc_trans_forall (code);
1282 res = gfc_trans_where (code);
1286 res = gfc_trans_allocate (code);
1289 case EXEC_DEALLOCATE:
1290 res = gfc_trans_deallocate (code);
1294 res = gfc_trans_open (code);
1298 res = gfc_trans_close (code);
1302 res = gfc_trans_read (code);
1306 res = gfc_trans_write (code);
1310 res = gfc_trans_iolength (code);
1313 case EXEC_BACKSPACE:
1314 res = gfc_trans_backspace (code);
1318 res = gfc_trans_endfile (code);
1322 res = gfc_trans_inquire (code);
1326 res = gfc_trans_wait (code);
1330 res = gfc_trans_rewind (code);
1334 res = gfc_trans_transfer (code);
1338 res = gfc_trans_dt_end (code);
1341 case EXEC_OMP_ATOMIC:
1342 case EXEC_OMP_BARRIER:
1343 case EXEC_OMP_CRITICAL:
1345 case EXEC_OMP_FLUSH:
1346 case EXEC_OMP_MASTER:
1347 case EXEC_OMP_ORDERED:
1348 case EXEC_OMP_PARALLEL:
1349 case EXEC_OMP_PARALLEL_DO:
1350 case EXEC_OMP_PARALLEL_SECTIONS:
1351 case EXEC_OMP_PARALLEL_WORKSHARE:
1352 case EXEC_OMP_SECTIONS:
1353 case EXEC_OMP_SINGLE:
1355 case EXEC_OMP_TASKWAIT:
1356 case EXEC_OMP_WORKSHARE:
1357 res = gfc_trans_omp_directive (code);
1361 internal_error ("gfc_trans_code(): Bad statement code");
1364 gfc_set_backend_locus (&code->loc);
1366 if (res != NULL_TREE && ! IS_EMPTY_STMT (res))
1368 if (TREE_CODE (res) != STATEMENT_LIST)
1369 SET_EXPR_LOCATION (res, input_location);
1371 /* Add the new statement to the block. */
1372 gfc_add_expr_to_block (&block, res);
1376 /* Return the finished block. */
1377 return gfc_finish_block (&block);
1381 /* Translate an executable statement with condition, cond. The condition is
1382 used by gfc_trans_do to test for IO result conditions inside implied
1383 DO loops of READ and WRITE statements. See build_dt in trans-io.c. */
1386 gfc_trans_code_cond (gfc_code * code, tree cond)
1388 return trans_code (code, cond);
1391 /* Translate an executable statement without condition. */
1394 gfc_trans_code (gfc_code * code)
1396 return trans_code (code, NULL_TREE);
1400 /* This function is called after a complete program unit has been parsed
1404 gfc_generate_code (gfc_namespace * ns)
1407 if (ns->is_block_data)
1409 gfc_generate_block_data (ns);
1413 gfc_generate_function_code (ns);
1417 /* This function is called after a complete module has been parsed
1421 gfc_generate_module_code (gfc_namespace * ns)
1424 struct module_htab_entry *entry;
1426 gcc_assert (ns->proc_name->backend_decl == NULL);
1427 ns->proc_name->backend_decl
1428 = build_decl (ns->proc_name->declared_at.lb->location,
1429 NAMESPACE_DECL, get_identifier (ns->proc_name->name),
1431 entry = gfc_find_module (ns->proc_name->name);
1432 if (entry->namespace_decl)
1433 /* Buggy sourcecode, using a module before defining it? */
1434 htab_empty (entry->decls);
1435 entry->namespace_decl = ns->proc_name->backend_decl;
1437 gfc_generate_module_vars (ns);
1439 /* We need to generate all module function prototypes first, to allow
1441 for (n = ns->contained; n; n = n->sibling)
1448 gfc_create_function_decl (n, false);
1449 DECL_CONTEXT (n->proc_name->backend_decl) = ns->proc_name->backend_decl;
1450 gfc_module_add_decl (entry, n->proc_name->backend_decl);
1451 for (el = ns->entries; el; el = el->next)
1453 DECL_CONTEXT (el->sym->backend_decl) = ns->proc_name->backend_decl;
1454 gfc_module_add_decl (entry, el->sym->backend_decl);
1458 for (n = ns->contained; n; n = n->sibling)
1463 gfc_generate_function_code (n);
1468 /* Initialize an init/cleanup block with existing code. */
1471 gfc_start_wrapped_block (gfc_wrapped_block* block, tree code)
1475 block->init = NULL_TREE;
1477 block->cleanup = NULL_TREE;
1481 /* Add a new pair of initializers/clean-up code. */
1484 gfc_add_init_cleanup (gfc_wrapped_block* block, tree init, tree cleanup)
1488 /* The new pair of init/cleanup should be "wrapped around" the existing
1489 block of code, thus the initialization is added to the front and the
1490 cleanup to the back. */
1491 add_expr_to_chain (&block->init, init, true);
1492 add_expr_to_chain (&block->cleanup, cleanup, false);
1496 /* Finish up a wrapped block by building a corresponding try-finally expr. */
1499 gfc_finish_wrapped_block (gfc_wrapped_block* block)
1505 /* Build the final expression. For this, just add init and body together,
1506 and put clean-up with that into a TRY_FINALLY_EXPR. */
1507 result = block->init;
1508 add_expr_to_chain (&result, block->code, false);
1510 result = build2_loc (input_location, TRY_FINALLY_EXPR, void_type_node,
1511 result, block->cleanup);
1513 /* Clear the block. */
1514 block->init = NULL_TREE;
1515 block->code = NULL_TREE;
1516 block->cleanup = NULL_TREE;