1 /* Code translation -- generate GCC trees from gfc_code.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free
3 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"
27 #include "tree-iterator.h"
35 #include "trans-stmt.h"
36 #include "trans-array.h"
37 #include "trans-types.h"
38 #include "trans-const.h"
40 /* Naming convention for backend interface code:
42 gfc_trans_* translate gfc_code into STMT trees.
44 gfc_conv_* expression conversion
46 gfc_get_* get a backend tree representation of a decl or type */
48 static gfc_file *gfc_current_backend_file;
50 const char gfc_msg_bounds[] = N_("Array bound mismatch");
51 const char gfc_msg_fault[] = N_("Array reference out of bounds");
52 const char gfc_msg_wrong_return[] = N_("Incorrect function return value");
55 /* Advance along TREE_CHAIN n times. */
58 gfc_advance_chain (tree t, int n)
62 gcc_assert (t != NULL_TREE);
69 /* Wrap a node in a TREE_LIST node and add it to the end of a list. */
72 gfc_chainon_list (tree list, tree add)
76 l = tree_cons (NULL_TREE, add, NULL_TREE);
78 return chainon (list, l);
82 /* Strip off a legitimate source ending from the input
83 string NAME of length LEN. */
86 remove_suffix (char *name, int len)
90 for (i = 2; i < 8 && len > i; i++)
92 if (name[len - i] == '.')
101 /* Creates a variable declaration with a given TYPE. */
104 gfc_create_var_np (tree type, const char *prefix)
108 t = create_tmp_var_raw (type, prefix);
110 /* No warnings for anonymous variables. */
112 TREE_NO_WARNING (t) = 1;
118 /* Like above, but also adds it to the current scope. */
121 gfc_create_var (tree type, const char *prefix)
125 tmp = gfc_create_var_np (type, prefix);
133 /* If the expression is not constant, evaluate it now. We assign the
134 result of the expression to an artificially created variable VAR, and
135 return a pointer to the VAR_DECL node for this variable. */
138 gfc_evaluate_now (tree expr, stmtblock_t * pblock)
142 if (CONSTANT_CLASS_P (expr))
145 var = gfc_create_var (TREE_TYPE (expr), NULL);
146 gfc_add_modify (pblock, var, expr);
152 /* Build a MODIFY_EXPR node and add it to a given statement block PBLOCK.
153 A MODIFY_EXPR is an assignment:
157 gfc_add_modify (stmtblock_t * pblock, tree lhs, tree rhs)
161 #ifdef ENABLE_CHECKING
163 t1 = TREE_TYPE (rhs);
164 t2 = TREE_TYPE (lhs);
165 /* Make sure that the types of the rhs and the lhs are the same
166 for scalar assignments. We should probably have something
167 similar for aggregates, but right now removing that check just
168 breaks everything. */
170 || AGGREGATE_TYPE_P (TREE_TYPE (lhs)));
173 tmp = fold_build2 (MODIFY_EXPR, void_type_node, lhs, rhs);
174 gfc_add_expr_to_block (pblock, tmp);
178 /* Create a new scope/binding level and initialize a block. Care must be
179 taken when translating expressions as any temporaries will be placed in
180 the innermost scope. */
183 gfc_start_block (stmtblock_t * block)
185 /* Start a new binding level. */
187 block->has_scope = 1;
189 /* The block is empty. */
190 block->head = NULL_TREE;
194 /* Initialize a block without creating a new scope. */
197 gfc_init_block (stmtblock_t * block)
199 block->head = NULL_TREE;
200 block->has_scope = 0;
204 /* Sometimes we create a scope but it turns out that we don't actually
205 need it. This function merges the scope of BLOCK with its parent.
206 Only variable decls will be merged, you still need to add the code. */
209 gfc_merge_block_scope (stmtblock_t * block)
214 gcc_assert (block->has_scope);
215 block->has_scope = 0;
217 /* Remember the decls in this scope. */
221 /* Add them to the parent scope. */
222 while (decl != NULL_TREE)
224 next = TREE_CHAIN (decl);
225 TREE_CHAIN (decl) = NULL_TREE;
233 /* Finish a scope containing a block of statements. */
236 gfc_finish_block (stmtblock_t * stmtblock)
242 expr = stmtblock->head;
244 expr = build_empty_stmt (input_location);
246 stmtblock->head = NULL_TREE;
248 if (stmtblock->has_scope)
254 block = poplevel (1, 0, 0);
255 expr = build3_v (BIND_EXPR, decl, expr, block);
265 /* Build an ADDR_EXPR and cast the result to TYPE. If TYPE is NULL, the
266 natural type is used. */
269 gfc_build_addr_expr (tree type, tree t)
271 tree base_type = TREE_TYPE (t);
274 if (type && POINTER_TYPE_P (type)
275 && TREE_CODE (base_type) == ARRAY_TYPE
276 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
277 == TYPE_MAIN_VARIANT (TREE_TYPE (base_type)))
279 tree min_val = size_zero_node;
280 tree type_domain = TYPE_DOMAIN (base_type);
281 if (type_domain && TYPE_MIN_VALUE (type_domain))
282 min_val = TYPE_MIN_VALUE (type_domain);
283 t = fold (build4 (ARRAY_REF, TREE_TYPE (type),
284 t, min_val, NULL_TREE, NULL_TREE));
288 natural_type = build_pointer_type (base_type);
290 if (TREE_CODE (t) == INDIRECT_REF)
294 t = TREE_OPERAND (t, 0);
295 natural_type = TREE_TYPE (t);
299 tree base = get_base_address (t);
300 if (base && DECL_P (base))
301 TREE_ADDRESSABLE (base) = 1;
302 t = fold_build1 (ADDR_EXPR, natural_type, t);
305 if (type && natural_type != type)
306 t = convert (type, t);
312 /* Build an ARRAY_REF with its natural type. */
315 gfc_build_array_ref (tree base, tree offset, tree decl)
317 tree type = TREE_TYPE (base);
320 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
321 type = TREE_TYPE (type);
324 TREE_ADDRESSABLE (base) = 1;
326 /* Strip NON_LVALUE_EXPR nodes. */
327 STRIP_TYPE_NOPS (offset);
329 /* If the array reference is to a pointer, whose target contains a
330 subreference, use the span that is stored with the backend decl
331 and reference the element with pointer arithmetic. */
332 if (decl && (TREE_CODE (decl) == FIELD_DECL
333 || TREE_CODE (decl) == VAR_DECL
334 || TREE_CODE (decl) == PARM_DECL)
335 && GFC_DECL_SUBREF_ARRAY_P (decl)
336 && !integer_zerop (GFC_DECL_SPAN(decl)))
338 offset = fold_build2 (MULT_EXPR, gfc_array_index_type,
339 offset, GFC_DECL_SPAN(decl));
340 tmp = gfc_build_addr_expr (pvoid_type_node, base);
341 tmp = fold_build2 (POINTER_PLUS_EXPR, pvoid_type_node,
342 tmp, fold_convert (sizetype, offset));
343 tmp = fold_convert (build_pointer_type (type), tmp);
344 if (!TYPE_STRING_FLAG (type))
345 tmp = build_fold_indirect_ref_loc (input_location, tmp);
349 /* Otherwise use a straightforward array reference. */
350 return build4 (ARRAY_REF, type, base, offset, NULL_TREE, NULL_TREE);
354 /* Generate a call to print a runtime error possibly including multiple
355 arguments and a locus. */
358 gfc_trans_runtime_error (bool error, locus* where, const char* msgid, ...)
362 va_start (ap, msgid);
363 return gfc_trans_runtime_error_vararg (error, where, msgid, ap);
367 gfc_trans_runtime_error_vararg (bool error, locus* where, const char* msgid,
379 /* Compute the number of extra arguments from the format string. */
380 for (p = msgid, nargs = 0; *p; p++)
388 /* The code to generate the error. */
389 gfc_start_block (&block);
393 line = LOCATION_LINE (where->lb->location);
394 asprintf (&message, "At line %d of file %s", line,
395 where->lb->file->filename);
398 asprintf (&message, "In file '%s', around line %d",
399 gfc_source_file, input_line + 1);
401 arg = gfc_build_addr_expr (pchar_type_node,
402 gfc_build_localized_cstring_const (message));
405 asprintf (&message, "%s", _(msgid));
406 arg2 = gfc_build_addr_expr (pchar_type_node,
407 gfc_build_localized_cstring_const (message));
410 /* Build the argument array. */
411 argarray = (tree *) alloca (sizeof (tree) * (nargs + 2));
414 for (i = 0; i < nargs; i++)
415 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 tmp = fold_builtin_call_array (input_location, TREE_TYPE (fntype),
427 fold_build1 (ADDR_EXPR,
428 build_pointer_type (fntype),
430 ? gfor_fndecl_runtime_error_at
431 : gfor_fndecl_runtime_warning_at),
432 nargs + 2, argarray);
433 gfc_add_expr_to_block (&block, tmp);
435 return gfc_finish_block (&block);
439 /* Generate a runtime error if COND is true. */
442 gfc_trans_runtime_check (bool error, bool once, tree cond, stmtblock_t * pblock,
443 locus * where, const char * msgid, ...)
451 if (integer_zerop (cond))
456 tmpvar = gfc_create_var (boolean_type_node, "print_warning");
457 TREE_STATIC (tmpvar) = 1;
458 DECL_INITIAL (tmpvar) = boolean_true_node;
459 gfc_add_expr_to_block (pblock, tmpvar);
462 gfc_start_block (&block);
464 /* The code to generate the error. */
465 va_start (ap, msgid);
466 gfc_add_expr_to_block (&block,
467 gfc_trans_runtime_error_vararg (error, where,
471 gfc_add_modify (&block, tmpvar, boolean_false_node);
473 body = gfc_finish_block (&block);
475 if (integer_onep (cond))
477 gfc_add_expr_to_block (pblock, body);
481 /* Tell the compiler that this isn't likely. */
483 cond = fold_build2 (TRUTH_AND_EXPR, long_integer_type_node, tmpvar,
486 cond = fold_convert (long_integer_type_node, cond);
488 tmp = build_int_cst (long_integer_type_node, 0);
489 cond = build_call_expr_loc (input_location,
490 built_in_decls[BUILT_IN_EXPECT], 2, cond, tmp);
491 cond = fold_convert (boolean_type_node, cond);
493 tmp = build3_v (COND_EXPR, cond, body, build_empty_stmt (input_location));
494 gfc_add_expr_to_block (pblock, tmp);
499 /* Call malloc to allocate size bytes of memory, with special conditions:
500 + if size < 0, generate a runtime error,
501 + if size == 0, return a malloced area of size 1,
502 + if malloc returns NULL, issue a runtime error. */
504 gfc_call_malloc (stmtblock_t * block, tree type, tree size)
506 tree tmp, msg, negative, malloc_result, null_result, res;
509 size = gfc_evaluate_now (size, block);
511 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
512 size = fold_convert (size_type_node, size);
514 /* Create a variable to hold the result. */
515 res = gfc_create_var (prvoid_type_node, NULL);
518 negative = fold_build2 (LT_EXPR, boolean_type_node, size,
519 build_int_cst (size_type_node, 0));
520 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
521 ("Attempt to allocate a negative amount of memory."));
522 tmp = fold_build3 (COND_EXPR, void_type_node, negative,
523 build_call_expr_loc (input_location,
524 gfor_fndecl_runtime_error, 1, msg),
525 build_empty_stmt (input_location));
526 gfc_add_expr_to_block (block, tmp);
528 /* Call malloc and check the result. */
529 gfc_start_block (&block2);
531 size = fold_build2 (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)));
538 null_result = fold_build2 (EQ_EXPR, boolean_type_node, res,
539 build_int_cst (pvoid_type_node, 0));
540 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
541 ("Memory allocation failed"));
542 tmp = fold_build3 (COND_EXPR, void_type_node, null_result,
543 build_call_expr_loc (input_location,
544 gfor_fndecl_os_error, 1, msg),
545 build_empty_stmt (input_location));
546 gfc_add_expr_to_block (&block2, tmp);
547 malloc_result = gfc_finish_block (&block2);
549 gfc_add_expr_to_block (block, malloc_result);
552 res = fold_convert (type, res);
556 /* Allocate memory, using an optional status argument.
558 This function follows the following pseudo-code:
561 allocate (size_t size, integer_type* stat)
568 // The only time this can happen is the size wraps around.
573 *stat = LIBERROR_ALLOCATION;
577 runtime_error ("Attempt to allocate negative amount of memory. "
578 "Possible integer overflow");
582 newmem = malloc (MAX (size, 1));
586 *stat = LIBERROR_ALLOCATION;
588 runtime_error ("Out of memory");
595 gfc_allocate_with_status (stmtblock_t * block, tree size, tree status)
597 stmtblock_t alloc_block;
598 tree res, tmp, error, msg, cond;
599 tree status_type = status ? TREE_TYPE (TREE_TYPE (status)) : NULL_TREE;
601 /* Evaluate size only once, and make sure it has the right type. */
602 size = gfc_evaluate_now (size, block);
603 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
604 size = fold_convert (size_type_node, size);
606 /* Create a variable to hold the result. */
607 res = gfc_create_var (prvoid_type_node, NULL);
609 /* Set the optional status variable to zero. */
610 if (status != NULL_TREE && !integer_zerop (status))
612 tmp = fold_build2 (MODIFY_EXPR, status_type,
613 fold_build1 (INDIRECT_REF, status_type, status),
614 build_int_cst (status_type, 0));
615 tmp = fold_build3 (COND_EXPR, void_type_node,
616 fold_build2 (NE_EXPR, boolean_type_node, status,
617 build_int_cst (TREE_TYPE (status), 0)),
618 tmp, build_empty_stmt (input_location));
619 gfc_add_expr_to_block (block, tmp);
622 /* Generate the block of code handling (size < 0). */
623 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
624 ("Attempt to allocate negative amount of memory. "
625 "Possible integer overflow"));
626 error = build_call_expr_loc (input_location,
627 gfor_fndecl_runtime_error, 1, msg);
629 if (status != NULL_TREE && !integer_zerop (status))
631 /* Set the status variable if it's present. */
632 stmtblock_t set_status_block;
634 gfc_start_block (&set_status_block);
635 gfc_add_modify (&set_status_block,
636 fold_build1 (INDIRECT_REF, status_type, status),
637 build_int_cst (status_type, LIBERROR_ALLOCATION));
638 gfc_add_modify (&set_status_block, res,
639 build_int_cst (prvoid_type_node, 0));
641 tmp = fold_build2 (EQ_EXPR, boolean_type_node, status,
642 build_int_cst (TREE_TYPE (status), 0));
643 error = fold_build3 (COND_EXPR, void_type_node, tmp, error,
644 gfc_finish_block (&set_status_block));
647 /* The allocation itself. */
648 gfc_start_block (&alloc_block);
649 gfc_add_modify (&alloc_block, res,
650 fold_convert (prvoid_type_node,
651 build_call_expr_loc (input_location,
652 built_in_decls[BUILT_IN_MALLOC], 1,
653 fold_build2 (MAX_EXPR, size_type_node,
655 build_int_cst (size_type_node, 1)))));
657 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
659 tmp = build_call_expr_loc (input_location,
660 gfor_fndecl_os_error, 1, msg);
662 if (status != NULL_TREE && !integer_zerop (status))
664 /* Set the status variable if it's present. */
667 cond = fold_build2 (EQ_EXPR, boolean_type_node, status,
668 build_int_cst (TREE_TYPE (status), 0));
669 tmp2 = fold_build2 (MODIFY_EXPR, status_type,
670 fold_build1 (INDIRECT_REF, status_type, status),
671 build_int_cst (status_type, LIBERROR_ALLOCATION));
672 tmp = fold_build3 (COND_EXPR, void_type_node, cond, tmp,
676 tmp = fold_build3 (COND_EXPR, void_type_node,
677 fold_build2 (EQ_EXPR, boolean_type_node, res,
678 build_int_cst (prvoid_type_node, 0)),
679 tmp, build_empty_stmt (input_location));
680 gfc_add_expr_to_block (&alloc_block, tmp);
682 cond = fold_build2 (LT_EXPR, boolean_type_node, size,
683 build_int_cst (TREE_TYPE (size), 0));
684 tmp = fold_build3 (COND_EXPR, void_type_node, cond, error,
685 gfc_finish_block (&alloc_block));
686 gfc_add_expr_to_block (block, tmp);
692 /* Generate code for an ALLOCATE statement when the argument is an
693 allocatable array. If the array is currently allocated, it is an
694 error to allocate it again.
696 This function follows the following pseudo-code:
699 allocate_array (void *mem, size_t size, integer_type *stat)
702 return allocate (size, stat);
708 mem = allocate (size, stat);
709 *stat = LIBERROR_ALLOCATION;
713 runtime_error ("Attempting to allocate already allocated array");
716 expr must be set to the original expression being allocated for its locus
717 and variable name in case a runtime error has to be printed. */
719 gfc_allocate_array_with_status (stmtblock_t * block, tree mem, tree size,
720 tree status, gfc_expr* expr)
722 stmtblock_t alloc_block;
723 tree res, tmp, null_mem, alloc, error;
724 tree type = TREE_TYPE (mem);
726 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
727 size = fold_convert (size_type_node, size);
729 /* Create a variable to hold the result. */
730 res = gfc_create_var (type, NULL);
731 null_mem = fold_build2 (EQ_EXPR, boolean_type_node, mem,
732 build_int_cst (type, 0));
734 /* If mem is NULL, we call gfc_allocate_with_status. */
735 gfc_start_block (&alloc_block);
736 tmp = gfc_allocate_with_status (&alloc_block, size, status);
737 gfc_add_modify (&alloc_block, res, fold_convert (type, tmp));
738 alloc = gfc_finish_block (&alloc_block);
740 /* Otherwise, we issue a runtime error or set the status variable. */
745 gcc_assert (expr->expr_type == EXPR_VARIABLE && expr->symtree);
746 varname = gfc_build_cstring_const (expr->symtree->name);
747 varname = gfc_build_addr_expr (pchar_type_node, varname);
749 error = gfc_trans_runtime_error (true, &expr->where,
750 "Attempting to allocate already"
751 " allocated array '%s'",
755 error = gfc_trans_runtime_error (true, NULL,
756 "Attempting to allocate already allocated"
759 if (status != NULL_TREE && !integer_zerop (status))
761 tree status_type = TREE_TYPE (TREE_TYPE (status));
762 stmtblock_t set_status_block;
764 gfc_start_block (&set_status_block);
765 tmp = build_call_expr_loc (input_location,
766 built_in_decls[BUILT_IN_FREE], 1,
767 fold_convert (pvoid_type_node, mem));
768 gfc_add_expr_to_block (&set_status_block, tmp);
770 tmp = gfc_allocate_with_status (&set_status_block, size, status);
771 gfc_add_modify (&set_status_block, res, fold_convert (type, tmp));
773 gfc_add_modify (&set_status_block,
774 fold_build1 (INDIRECT_REF, status_type, status),
775 build_int_cst (status_type, LIBERROR_ALLOCATION));
777 tmp = fold_build2 (EQ_EXPR, boolean_type_node, status,
778 build_int_cst (status_type, 0));
779 error = fold_build3 (COND_EXPR, void_type_node, tmp, error,
780 gfc_finish_block (&set_status_block));
783 tmp = fold_build3 (COND_EXPR, void_type_node, null_mem, alloc, error);
784 gfc_add_expr_to_block (block, tmp);
790 /* Free a given variable, if it's not NULL. */
792 gfc_call_free (tree var)
795 tree tmp, cond, call;
797 if (TREE_TYPE (var) != TREE_TYPE (pvoid_type_node))
798 var = fold_convert (pvoid_type_node, var);
800 gfc_start_block (&block);
801 var = gfc_evaluate_now (var, &block);
802 cond = fold_build2 (NE_EXPR, boolean_type_node, var,
803 build_int_cst (pvoid_type_node, 0));
804 call = build_call_expr_loc (input_location,
805 built_in_decls[BUILT_IN_FREE], 1, var);
806 tmp = fold_build3 (COND_EXPR, void_type_node, cond, call,
807 build_empty_stmt (input_location));
808 gfc_add_expr_to_block (&block, tmp);
810 return gfc_finish_block (&block);
815 /* User-deallocate; we emit the code directly from the front-end, and the
816 logic is the same as the previous library function:
819 deallocate (void *pointer, GFC_INTEGER_4 * stat)
826 runtime_error ("Attempt to DEALLOCATE unallocated memory.");
836 In this front-end version, status doesn't have to be GFC_INTEGER_4.
837 Moreover, if CAN_FAIL is true, then we will not emit a runtime error,
838 even when no status variable is passed to us (this is used for
839 unconditional deallocation generated by the front-end at end of
842 If a runtime-message is possible, `expr' must point to the original
843 expression being deallocated for its locus and variable name. */
845 gfc_deallocate_with_status (tree pointer, tree status, bool can_fail,
848 stmtblock_t null, non_null;
849 tree cond, tmp, error;
851 cond = fold_build2 (EQ_EXPR, boolean_type_node, pointer,
852 build_int_cst (TREE_TYPE (pointer), 0));
854 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
855 we emit a runtime error. */
856 gfc_start_block (&null);
861 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
863 varname = gfc_build_cstring_const (expr->symtree->name);
864 varname = gfc_build_addr_expr (pchar_type_node, varname);
866 error = gfc_trans_runtime_error (true, &expr->where,
867 "Attempt to DEALLOCATE unallocated '%s'",
871 error = build_empty_stmt (input_location);
873 if (status != NULL_TREE && !integer_zerop (status))
875 tree status_type = TREE_TYPE (TREE_TYPE (status));
878 cond2 = fold_build2 (NE_EXPR, boolean_type_node, status,
879 build_int_cst (TREE_TYPE (status), 0));
880 tmp = fold_build2 (MODIFY_EXPR, status_type,
881 fold_build1 (INDIRECT_REF, status_type, status),
882 build_int_cst (status_type, 1));
883 error = fold_build3 (COND_EXPR, void_type_node, cond2, tmp, error);
886 gfc_add_expr_to_block (&null, error);
888 /* When POINTER is not NULL, we free it. */
889 gfc_start_block (&non_null);
890 tmp = build_call_expr_loc (input_location,
891 built_in_decls[BUILT_IN_FREE], 1,
892 fold_convert (pvoid_type_node, pointer));
893 gfc_add_expr_to_block (&non_null, tmp);
895 if (status != NULL_TREE && !integer_zerop (status))
897 /* We set STATUS to zero if it is present. */
898 tree status_type = TREE_TYPE (TREE_TYPE (status));
901 cond2 = fold_build2 (NE_EXPR, boolean_type_node, status,
902 build_int_cst (TREE_TYPE (status), 0));
903 tmp = fold_build2 (MODIFY_EXPR, status_type,
904 fold_build1 (INDIRECT_REF, status_type, status),
905 build_int_cst (status_type, 0));
906 tmp = fold_build3 (COND_EXPR, void_type_node, cond2, tmp,
907 build_empty_stmt (input_location));
908 gfc_add_expr_to_block (&non_null, tmp);
911 return fold_build3 (COND_EXPR, void_type_node, cond,
912 gfc_finish_block (&null), gfc_finish_block (&non_null));
916 /* Reallocate MEM so it has SIZE bytes of data. This behaves like the
917 following pseudo-code:
920 internal_realloc (void *mem, size_t size)
923 runtime_error ("Attempt to allocate a negative amount of memory.");
924 res = realloc (mem, size);
925 if (!res && size != 0)
926 _gfortran_os_error ("Out of memory");
934 gfc_call_realloc (stmtblock_t * block, tree mem, tree size)
936 tree msg, res, negative, nonzero, zero, null_result, tmp;
937 tree type = TREE_TYPE (mem);
939 size = gfc_evaluate_now (size, block);
941 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
942 size = fold_convert (size_type_node, size);
944 /* Create a variable to hold the result. */
945 res = gfc_create_var (type, NULL);
948 negative = fold_build2 (LT_EXPR, boolean_type_node, size,
949 build_int_cst (size_type_node, 0));
950 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
951 ("Attempt to allocate a negative amount of memory."));
952 tmp = fold_build3 (COND_EXPR, void_type_node, negative,
953 build_call_expr_loc (input_location,
954 gfor_fndecl_runtime_error, 1, msg),
955 build_empty_stmt (input_location));
956 gfc_add_expr_to_block (block, tmp);
958 /* Call realloc and check the result. */
959 tmp = build_call_expr_loc (input_location,
960 built_in_decls[BUILT_IN_REALLOC], 2,
961 fold_convert (pvoid_type_node, mem), size);
962 gfc_add_modify (block, res, fold_convert (type, tmp));
963 null_result = fold_build2 (EQ_EXPR, boolean_type_node, res,
964 build_int_cst (pvoid_type_node, 0));
965 nonzero = fold_build2 (NE_EXPR, boolean_type_node, size,
966 build_int_cst (size_type_node, 0));
967 null_result = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, null_result,
969 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
971 tmp = fold_build3 (COND_EXPR, void_type_node, null_result,
972 build_call_expr_loc (input_location,
973 gfor_fndecl_os_error, 1, msg),
974 build_empty_stmt (input_location));
975 gfc_add_expr_to_block (block, tmp);
977 /* if (size == 0) then the result is NULL. */
978 tmp = fold_build2 (MODIFY_EXPR, type, res, build_int_cst (type, 0));
979 zero = fold_build1 (TRUTH_NOT_EXPR, boolean_type_node, nonzero);
980 tmp = fold_build3 (COND_EXPR, void_type_node, zero, tmp,
981 build_empty_stmt (input_location));
982 gfc_add_expr_to_block (block, tmp);
987 /* Add a statement to a block. */
990 gfc_add_expr_to_block (stmtblock_t * block, tree expr)
994 if (expr == NULL_TREE || IS_EMPTY_STMT (expr))
999 if (TREE_CODE (block->head) != STATEMENT_LIST)
1004 block->head = NULL_TREE;
1005 append_to_statement_list (tmp, &block->head);
1007 append_to_statement_list (expr, &block->head);
1010 /* Don't bother creating a list if we only have a single statement. */
1015 /* Add a block the end of a block. */
1018 gfc_add_block_to_block (stmtblock_t * block, stmtblock_t * append)
1020 gcc_assert (append);
1021 gcc_assert (!append->has_scope);
1023 gfc_add_expr_to_block (block, append->head);
1024 append->head = NULL_TREE;
1028 /* Get the current locus. The structure may not be complete, and should
1029 only be used with gfc_set_backend_locus. */
1032 gfc_get_backend_locus (locus * loc)
1034 loc->lb = XCNEW (gfc_linebuf);
1035 loc->lb->location = input_location;
1036 loc->lb->file = gfc_current_backend_file;
1040 /* Set the current locus. */
1043 gfc_set_backend_locus (locus * loc)
1045 gfc_current_backend_file = loc->lb->file;
1046 input_location = loc->lb->location;
1050 /* Translate an executable statement. */
1053 gfc_trans_code (gfc_code * code)
1059 return build_empty_stmt (input_location);
1061 gfc_start_block (&block);
1063 /* Translate statements one by one into GENERIC trees until we reach
1064 the end of this gfc_code branch. */
1065 for (; code; code = code->next)
1067 if (code->here != 0)
1069 res = gfc_trans_label_here (code);
1070 gfc_add_expr_to_block (&block, res);
1076 case EXEC_END_BLOCK:
1077 case EXEC_END_PROCEDURE:
1082 if (code->expr1->ts.type == BT_CLASS)
1083 res = gfc_trans_class_assign (code);
1085 res = gfc_trans_assign (code);
1088 case EXEC_LABEL_ASSIGN:
1089 res = gfc_trans_label_assign (code);
1092 case EXEC_POINTER_ASSIGN:
1093 if (code->expr1->ts.type == BT_CLASS)
1094 res = gfc_trans_class_assign (code);
1096 res = gfc_trans_pointer_assign (code);
1099 case EXEC_INIT_ASSIGN:
1100 res = gfc_trans_init_assign (code);
1108 res = gfc_trans_cycle (code);
1112 res = gfc_trans_exit (code);
1116 res = gfc_trans_goto (code);
1120 res = gfc_trans_entry (code);
1124 res = gfc_trans_pause (code);
1128 res = gfc_trans_stop (code);
1132 /* For MVBITS we've got the special exception that we need a
1133 dependency check, too. */
1135 bool is_mvbits = false;
1136 if (code->resolved_isym
1137 && code->resolved_isym->id == GFC_ISYM_MVBITS)
1139 res = gfc_trans_call (code, is_mvbits, NULL_TREE,
1145 res = gfc_trans_call (code, false, NULL_TREE,
1149 case EXEC_ASSIGN_CALL:
1150 res = gfc_trans_call (code, true, NULL_TREE,
1155 res = gfc_trans_return (code);
1159 res = gfc_trans_if (code);
1162 case EXEC_ARITHMETIC_IF:
1163 res = gfc_trans_arithmetic_if (code);
1167 res = gfc_trans_block_construct (code);
1171 res = gfc_trans_do (code);
1175 res = gfc_trans_do_while (code);
1179 res = gfc_trans_select (code);
1182 case EXEC_SELECT_TYPE:
1183 /* Do nothing. SELECT TYPE statements should be transformed into
1184 an ordinary SELECT CASE at resolution stage.
1185 TODO: Add an error message here once this is done. */
1190 res = gfc_trans_flush (code);
1194 res = gfc_trans_forall (code);
1198 res = gfc_trans_where (code);
1202 res = gfc_trans_allocate (code);
1205 case EXEC_DEALLOCATE:
1206 res = gfc_trans_deallocate (code);
1210 res = gfc_trans_open (code);
1214 res = gfc_trans_close (code);
1218 res = gfc_trans_read (code);
1222 res = gfc_trans_write (code);
1226 res = gfc_trans_iolength (code);
1229 case EXEC_BACKSPACE:
1230 res = gfc_trans_backspace (code);
1234 res = gfc_trans_endfile (code);
1238 res = gfc_trans_inquire (code);
1242 res = gfc_trans_wait (code);
1246 res = gfc_trans_rewind (code);
1250 res = gfc_trans_transfer (code);
1254 res = gfc_trans_dt_end (code);
1257 case EXEC_OMP_ATOMIC:
1258 case EXEC_OMP_BARRIER:
1259 case EXEC_OMP_CRITICAL:
1261 case EXEC_OMP_FLUSH:
1262 case EXEC_OMP_MASTER:
1263 case EXEC_OMP_ORDERED:
1264 case EXEC_OMP_PARALLEL:
1265 case EXEC_OMP_PARALLEL_DO:
1266 case EXEC_OMP_PARALLEL_SECTIONS:
1267 case EXEC_OMP_PARALLEL_WORKSHARE:
1268 case EXEC_OMP_SECTIONS:
1269 case EXEC_OMP_SINGLE:
1271 case EXEC_OMP_TASKWAIT:
1272 case EXEC_OMP_WORKSHARE:
1273 res = gfc_trans_omp_directive (code);
1277 internal_error ("gfc_trans_code(): Bad statement code");
1280 gfc_set_backend_locus (&code->loc);
1282 if (res != NULL_TREE && ! IS_EMPTY_STMT (res))
1284 if (TREE_CODE (res) != STATEMENT_LIST)
1285 SET_EXPR_LOCATION (res, input_location);
1287 /* Add the new statement to the block. */
1288 gfc_add_expr_to_block (&block, res);
1292 /* Return the finished block. */
1293 return gfc_finish_block (&block);
1297 /* This function is called after a complete program unit has been parsed
1301 gfc_generate_code (gfc_namespace * ns)
1304 if (ns->is_block_data)
1306 gfc_generate_block_data (ns);
1310 gfc_generate_function_code (ns);
1314 /* This function is called after a complete module has been parsed
1318 gfc_generate_module_code (gfc_namespace * ns)
1321 struct module_htab_entry *entry;
1323 gcc_assert (ns->proc_name->backend_decl == NULL);
1324 ns->proc_name->backend_decl
1325 = build_decl (ns->proc_name->declared_at.lb->location,
1326 NAMESPACE_DECL, get_identifier (ns->proc_name->name),
1328 entry = gfc_find_module (ns->proc_name->name);
1329 if (entry->namespace_decl)
1330 /* Buggy sourcecode, using a module before defining it? */
1331 htab_empty (entry->decls);
1332 entry->namespace_decl = ns->proc_name->backend_decl;
1334 gfc_generate_module_vars (ns);
1336 /* We need to generate all module function prototypes first, to allow
1338 for (n = ns->contained; n; n = n->sibling)
1345 gfc_create_function_decl (n);
1346 gcc_assert (DECL_CONTEXT (n->proc_name->backend_decl) == NULL_TREE);
1347 DECL_CONTEXT (n->proc_name->backend_decl) = ns->proc_name->backend_decl;
1348 gfc_module_add_decl (entry, n->proc_name->backend_decl);
1349 for (el = ns->entries; el; el = el->next)
1351 gcc_assert (DECL_CONTEXT (el->sym->backend_decl) == NULL_TREE);
1352 DECL_CONTEXT (el->sym->backend_decl) = ns->proc_name->backend_decl;
1353 gfc_module_add_decl (entry, el->sym->backend_decl);
1357 for (n = ns->contained; n; n = n->sibling)
1362 gfc_generate_function_code (n);