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 /* ??? This is actually backwards, we should test the "base" type
166 from which the nontarget_type was copied, but we don't have this
167 backlink. This will do for now, it's for checking anyway. */
168 if (TYPE_LANG_SPECIFIC (t1))
169 t1 = TYPE_LANG_SPECIFIC (t1)->nontarget_type;
170 if (TYPE_LANG_SPECIFIC (t2))
171 t2 = TYPE_LANG_SPECIFIC (t2)->nontarget_type;
172 /* Make sure that the types of the rhs and the lhs are the same
173 for scalar assignments. We should probably have something
174 similar for aggregates, but right now removing that check just
175 breaks everything. */
177 || AGGREGATE_TYPE_P (TREE_TYPE (lhs)));
180 tmp = fold_build2 (MODIFY_EXPR, void_type_node, lhs, rhs);
181 gfc_add_expr_to_block (pblock, tmp);
185 /* Create a new scope/binding level and initialize a block. Care must be
186 taken when translating expressions as any temporaries will be placed in
187 the innermost scope. */
190 gfc_start_block (stmtblock_t * block)
192 /* Start a new binding level. */
194 block->has_scope = 1;
196 /* The block is empty. */
197 block->head = NULL_TREE;
201 /* Initialize a block without creating a new scope. */
204 gfc_init_block (stmtblock_t * block)
206 block->head = NULL_TREE;
207 block->has_scope = 0;
211 /* Sometimes we create a scope but it turns out that we don't actually
212 need it. This function merges the scope of BLOCK with its parent.
213 Only variable decls will be merged, you still need to add the code. */
216 gfc_merge_block_scope (stmtblock_t * block)
221 gcc_assert (block->has_scope);
222 block->has_scope = 0;
224 /* Remember the decls in this scope. */
228 /* Add them to the parent scope. */
229 while (decl != NULL_TREE)
231 next = TREE_CHAIN (decl);
232 TREE_CHAIN (decl) = NULL_TREE;
240 /* Finish a scope containing a block of statements. */
243 gfc_finish_block (stmtblock_t * stmtblock)
249 expr = stmtblock->head;
251 expr = build_empty_stmt (input_location);
253 stmtblock->head = NULL_TREE;
255 if (stmtblock->has_scope)
261 block = poplevel (1, 0, 0);
262 expr = build3_v (BIND_EXPR, decl, expr, block);
272 /* Build an ADDR_EXPR and cast the result to TYPE. If TYPE is NULL, the
273 natural type is used. */
276 gfc_build_addr_expr (tree type, tree t)
278 tree base_type = TREE_TYPE (t);
281 if (type && POINTER_TYPE_P (type)
282 && TREE_CODE (base_type) == ARRAY_TYPE
283 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
284 == TYPE_MAIN_VARIANT (TREE_TYPE (base_type)))
286 tree min_val = size_zero_node;
287 tree type_domain = TYPE_DOMAIN (base_type);
288 if (type_domain && TYPE_MIN_VALUE (type_domain))
289 min_val = TYPE_MIN_VALUE (type_domain);
290 t = fold (build4 (ARRAY_REF, TREE_TYPE (type),
291 t, min_val, NULL_TREE, NULL_TREE));
295 natural_type = build_pointer_type (base_type);
297 if (TREE_CODE (t) == INDIRECT_REF)
301 t = TREE_OPERAND (t, 0);
302 natural_type = TREE_TYPE (t);
306 tree base = get_base_address (t);
307 if (base && DECL_P (base))
308 TREE_ADDRESSABLE (base) = 1;
309 t = fold_build1 (ADDR_EXPR, natural_type, t);
312 if (type && natural_type != type)
313 t = convert (type, t);
319 /* Build an ARRAY_REF with its natural type. */
322 gfc_build_array_ref (tree base, tree offset, tree decl)
324 tree type = TREE_TYPE (base);
327 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
328 type = TREE_TYPE (type);
331 TREE_ADDRESSABLE (base) = 1;
333 /* Strip NON_LVALUE_EXPR nodes. */
334 STRIP_TYPE_NOPS (offset);
336 /* If the array reference is to a pointer, whose target contains a
337 subreference, use the span that is stored with the backend decl
338 and reference the element with pointer arithmetic. */
339 if (decl && (TREE_CODE (decl) == FIELD_DECL
340 || TREE_CODE (decl) == VAR_DECL
341 || TREE_CODE (decl) == PARM_DECL)
342 && GFC_DECL_SUBREF_ARRAY_P (decl)
343 && !integer_zerop (GFC_DECL_SPAN(decl)))
345 offset = fold_build2 (MULT_EXPR, gfc_array_index_type,
346 offset, GFC_DECL_SPAN(decl));
347 tmp = gfc_build_addr_expr (pvoid_type_node, base);
348 tmp = fold_build2 (POINTER_PLUS_EXPR, pvoid_type_node,
349 tmp, fold_convert (sizetype, offset));
350 tmp = fold_convert (build_pointer_type (type), tmp);
351 if (!TYPE_STRING_FLAG (type))
352 tmp = build_fold_indirect_ref_loc (input_location, tmp);
356 /* Otherwise use a straightforward array reference. */
357 return build4 (ARRAY_REF, type, base, offset, NULL_TREE, NULL_TREE);
361 /* Generate a call to print a runtime error possibly including multiple
362 arguments and a locus. */
365 gfc_trans_runtime_error (bool error, locus* where, const char* msgid, ...)
369 va_start (ap, msgid);
370 return gfc_trans_runtime_error_vararg (error, where, msgid, ap);
374 gfc_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 = (tree *) alloca (sizeof (tree) * (nargs + 2));
421 for (i = 0; i < nargs; i++)
422 argarray[2 + i] = va_arg (ap, tree);
425 /* Build the function call to runtime_(warning,error)_at; because of the
426 variable number of arguments, we can't use build_call_expr_loc dinput_location,
429 fntype = TREE_TYPE (gfor_fndecl_runtime_error_at);
431 fntype = TREE_TYPE (gfor_fndecl_runtime_warning_at);
433 tmp = fold_builtin_call_array (input_location, TREE_TYPE (fntype),
434 fold_build1 (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);
446 /* Generate a runtime error if COND is true. */
449 gfc_trans_runtime_check (bool error, bool once, tree cond, stmtblock_t * pblock,
450 locus * where, const char * msgid, ...)
458 if (integer_zerop (cond))
463 tmpvar = gfc_create_var (boolean_type_node, "print_warning");
464 TREE_STATIC (tmpvar) = 1;
465 DECL_INITIAL (tmpvar) = boolean_true_node;
466 gfc_add_expr_to_block (pblock, tmpvar);
469 gfc_start_block (&block);
471 /* The code to generate the error. */
472 va_start (ap, msgid);
473 gfc_add_expr_to_block (&block,
474 gfc_trans_runtime_error_vararg (error, where,
478 gfc_add_modify (&block, tmpvar, boolean_false_node);
480 body = gfc_finish_block (&block);
482 if (integer_onep (cond))
484 gfc_add_expr_to_block (pblock, body);
488 /* Tell the compiler that this isn't likely. */
490 cond = fold_build2 (TRUTH_AND_EXPR, long_integer_type_node, tmpvar,
493 cond = fold_convert (long_integer_type_node, cond);
495 tmp = build_int_cst (long_integer_type_node, 0);
496 cond = build_call_expr_loc (input_location,
497 built_in_decls[BUILT_IN_EXPECT], 2, cond, tmp);
498 cond = fold_convert (boolean_type_node, cond);
500 tmp = build3_v (COND_EXPR, cond, body, build_empty_stmt (input_location));
501 gfc_add_expr_to_block (pblock, tmp);
506 /* Call malloc to allocate size bytes of memory, with special conditions:
507 + if size < 0, generate a runtime error,
508 + if size == 0, return a malloced area of size 1,
509 + if malloc returns NULL, issue a runtime error. */
511 gfc_call_malloc (stmtblock_t * block, tree type, tree size)
513 tree tmp, msg, negative, malloc_result, null_result, res;
516 size = gfc_evaluate_now (size, block);
518 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
519 size = fold_convert (size_type_node, size);
521 /* Create a variable to hold the result. */
522 res = gfc_create_var (prvoid_type_node, NULL);
525 negative = fold_build2 (LT_EXPR, boolean_type_node, size,
526 build_int_cst (size_type_node, 0));
527 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
528 ("Attempt to allocate a negative amount of memory."));
529 tmp = fold_build3 (COND_EXPR, void_type_node, negative,
530 build_call_expr_loc (input_location,
531 gfor_fndecl_runtime_error, 1, msg),
532 build_empty_stmt (input_location));
533 gfc_add_expr_to_block (block, tmp);
535 /* Call malloc and check the result. */
536 gfc_start_block (&block2);
538 size = fold_build2 (MAX_EXPR, size_type_node, size,
539 build_int_cst (size_type_node, 1));
541 gfc_add_modify (&block2, res,
542 fold_convert (prvoid_type_node,
543 build_call_expr_loc (input_location,
544 built_in_decls[BUILT_IN_MALLOC], 1, size)));
545 null_result = fold_build2 (EQ_EXPR, boolean_type_node, res,
546 build_int_cst (pvoid_type_node, 0));
547 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
548 ("Memory allocation failed"));
549 tmp = fold_build3 (COND_EXPR, void_type_node, null_result,
550 build_call_expr_loc (input_location,
551 gfor_fndecl_os_error, 1, msg),
552 build_empty_stmt (input_location));
553 gfc_add_expr_to_block (&block2, tmp);
554 malloc_result = gfc_finish_block (&block2);
556 gfc_add_expr_to_block (block, malloc_result);
559 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 // The only time this can happen is the size wraps around.
580 *stat = LIBERROR_ALLOCATION;
584 runtime_error ("Attempt to allocate negative amount of memory. "
585 "Possible integer overflow");
589 newmem = malloc (MAX (size, 1));
593 *stat = LIBERROR_ALLOCATION;
595 runtime_error ("Out of memory");
602 gfc_allocate_with_status (stmtblock_t * block, tree size, tree status)
604 stmtblock_t alloc_block;
605 tree res, tmp, error, msg, cond;
606 tree status_type = status ? TREE_TYPE (TREE_TYPE (status)) : NULL_TREE;
608 /* Evaluate size only once, and make sure it has the right type. */
609 size = gfc_evaluate_now (size, block);
610 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
611 size = fold_convert (size_type_node, size);
613 /* Create a variable to hold the result. */
614 res = gfc_create_var (prvoid_type_node, NULL);
616 /* Set the optional status variable to zero. */
617 if (status != NULL_TREE && !integer_zerop (status))
619 tmp = fold_build2 (MODIFY_EXPR, status_type,
620 fold_build1 (INDIRECT_REF, status_type, status),
621 build_int_cst (status_type, 0));
622 tmp = fold_build3 (COND_EXPR, void_type_node,
623 fold_build2 (NE_EXPR, boolean_type_node, status,
624 build_int_cst (TREE_TYPE (status), 0)),
625 tmp, build_empty_stmt (input_location));
626 gfc_add_expr_to_block (block, tmp);
629 /* Generate the block of code handling (size < 0). */
630 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
631 ("Attempt to allocate negative amount of memory. "
632 "Possible integer overflow"));
633 error = build_call_expr_loc (input_location,
634 gfor_fndecl_runtime_error, 1, msg);
636 if (status != NULL_TREE && !integer_zerop (status))
638 /* Set the status variable if it's present. */
639 stmtblock_t set_status_block;
641 gfc_start_block (&set_status_block);
642 gfc_add_modify (&set_status_block,
643 fold_build1 (INDIRECT_REF, status_type, status),
644 build_int_cst (status_type, LIBERROR_ALLOCATION));
645 gfc_add_modify (&set_status_block, res,
646 build_int_cst (prvoid_type_node, 0));
648 tmp = fold_build2 (EQ_EXPR, boolean_type_node, status,
649 build_int_cst (TREE_TYPE (status), 0));
650 error = fold_build3 (COND_EXPR, void_type_node, tmp, error,
651 gfc_finish_block (&set_status_block));
654 /* The allocation itself. */
655 gfc_start_block (&alloc_block);
656 gfc_add_modify (&alloc_block, res,
657 fold_convert (prvoid_type_node,
658 build_call_expr_loc (input_location,
659 built_in_decls[BUILT_IN_MALLOC], 1,
660 fold_build2 (MAX_EXPR, size_type_node,
662 build_int_cst (size_type_node, 1)))));
664 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
666 tmp = build_call_expr_loc (input_location,
667 gfor_fndecl_os_error, 1, msg);
669 if (status != NULL_TREE && !integer_zerop (status))
671 /* Set the status variable if it's present. */
674 cond = fold_build2 (EQ_EXPR, boolean_type_node, status,
675 build_int_cst (TREE_TYPE (status), 0));
676 tmp2 = fold_build2 (MODIFY_EXPR, status_type,
677 fold_build1 (INDIRECT_REF, status_type, status),
678 build_int_cst (status_type, LIBERROR_ALLOCATION));
679 tmp = fold_build3 (COND_EXPR, void_type_node, cond, tmp,
683 tmp = fold_build3 (COND_EXPR, void_type_node,
684 fold_build2 (EQ_EXPR, boolean_type_node, res,
685 build_int_cst (prvoid_type_node, 0)),
686 tmp, build_empty_stmt (input_location));
687 gfc_add_expr_to_block (&alloc_block, tmp);
689 cond = fold_build2 (LT_EXPR, boolean_type_node, size,
690 build_int_cst (TREE_TYPE (size), 0));
691 tmp = fold_build3 (COND_EXPR, void_type_node, cond, error,
692 gfc_finish_block (&alloc_block));
693 gfc_add_expr_to_block (block, tmp);
699 /* Generate code for an ALLOCATE statement when the argument is an
700 allocatable array. If the array is currently allocated, it is an
701 error to allocate it again.
703 This function follows the following pseudo-code:
706 allocate_array (void *mem, size_t size, integer_type *stat)
709 return allocate (size, stat);
715 mem = allocate (size, stat);
716 *stat = LIBERROR_ALLOCATION;
720 runtime_error ("Attempting to allocate already allocated array");
723 expr must be set to the original expression being allocated for its locus
724 and variable name in case a runtime error has to be printed. */
726 gfc_allocate_array_with_status (stmtblock_t * block, tree mem, tree size,
727 tree status, gfc_expr* expr)
729 stmtblock_t alloc_block;
730 tree res, tmp, null_mem, alloc, error;
731 tree type = TREE_TYPE (mem);
733 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
734 size = fold_convert (size_type_node, size);
736 /* Create a variable to hold the result. */
737 res = gfc_create_var (type, NULL);
738 null_mem = fold_build2 (EQ_EXPR, boolean_type_node, mem,
739 build_int_cst (type, 0));
741 /* If mem is NULL, we call gfc_allocate_with_status. */
742 gfc_start_block (&alloc_block);
743 tmp = gfc_allocate_with_status (&alloc_block, size, status);
744 gfc_add_modify (&alloc_block, res, fold_convert (type, tmp));
745 alloc = gfc_finish_block (&alloc_block);
747 /* Otherwise, we issue a runtime error or set the status variable. */
752 gcc_assert (expr->expr_type == EXPR_VARIABLE && expr->symtree);
753 varname = gfc_build_cstring_const (expr->symtree->name);
754 varname = gfc_build_addr_expr (pchar_type_node, varname);
756 error = gfc_trans_runtime_error (true, &expr->where,
757 "Attempting to allocate already"
758 " allocated array '%s'",
762 error = gfc_trans_runtime_error (true, NULL,
763 "Attempting to allocate already allocated"
766 if (status != NULL_TREE && !integer_zerop (status))
768 tree status_type = TREE_TYPE (TREE_TYPE (status));
769 stmtblock_t set_status_block;
771 gfc_start_block (&set_status_block);
772 tmp = build_call_expr_loc (input_location,
773 built_in_decls[BUILT_IN_FREE], 1,
774 fold_convert (pvoid_type_node, mem));
775 gfc_add_expr_to_block (&set_status_block, tmp);
777 tmp = gfc_allocate_with_status (&set_status_block, size, status);
778 gfc_add_modify (&set_status_block, res, fold_convert (type, tmp));
780 gfc_add_modify (&set_status_block,
781 fold_build1 (INDIRECT_REF, status_type, status),
782 build_int_cst (status_type, LIBERROR_ALLOCATION));
784 tmp = fold_build2 (EQ_EXPR, boolean_type_node, status,
785 build_int_cst (status_type, 0));
786 error = fold_build3 (COND_EXPR, void_type_node, tmp, error,
787 gfc_finish_block (&set_status_block));
790 tmp = fold_build3 (COND_EXPR, void_type_node, null_mem, alloc, error);
791 gfc_add_expr_to_block (block, tmp);
797 /* Free a given variable, if it's not NULL. */
799 gfc_call_free (tree var)
802 tree tmp, cond, call;
804 if (TREE_TYPE (var) != TREE_TYPE (pvoid_type_node))
805 var = fold_convert (pvoid_type_node, var);
807 gfc_start_block (&block);
808 var = gfc_evaluate_now (var, &block);
809 cond = fold_build2 (NE_EXPR, boolean_type_node, var,
810 build_int_cst (pvoid_type_node, 0));
811 call = build_call_expr_loc (input_location,
812 built_in_decls[BUILT_IN_FREE], 1, var);
813 tmp = fold_build3 (COND_EXPR, void_type_node, cond, call,
814 build_empty_stmt (input_location));
815 gfc_add_expr_to_block (&block, tmp);
817 return gfc_finish_block (&block);
822 /* User-deallocate; we emit the code directly from the front-end, and the
823 logic is the same as the previous library function:
826 deallocate (void *pointer, GFC_INTEGER_4 * stat)
833 runtime_error ("Attempt to DEALLOCATE unallocated memory.");
843 In this front-end version, status doesn't have to be GFC_INTEGER_4.
844 Moreover, if CAN_FAIL is true, then we will not emit a runtime error,
845 even when no status variable is passed to us (this is used for
846 unconditional deallocation generated by the front-end at end of
849 If a runtime-message is possible, `expr' must point to the original
850 expression being deallocated for its locus and variable name. */
852 gfc_deallocate_with_status (tree pointer, tree status, bool can_fail,
855 stmtblock_t null, non_null;
856 tree cond, tmp, error;
858 cond = fold_build2 (EQ_EXPR, boolean_type_node, pointer,
859 build_int_cst (TREE_TYPE (pointer), 0));
861 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
862 we emit a runtime error. */
863 gfc_start_block (&null);
868 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
870 varname = gfc_build_cstring_const (expr->symtree->name);
871 varname = gfc_build_addr_expr (pchar_type_node, varname);
873 error = gfc_trans_runtime_error (true, &expr->where,
874 "Attempt to DEALLOCATE unallocated '%s'",
878 error = build_empty_stmt (input_location);
880 if (status != NULL_TREE && !integer_zerop (status))
882 tree status_type = TREE_TYPE (TREE_TYPE (status));
885 cond2 = fold_build2 (NE_EXPR, boolean_type_node, status,
886 build_int_cst (TREE_TYPE (status), 0));
887 tmp = fold_build2 (MODIFY_EXPR, status_type,
888 fold_build1 (INDIRECT_REF, status_type, status),
889 build_int_cst (status_type, 1));
890 error = fold_build3 (COND_EXPR, void_type_node, cond2, tmp, error);
893 gfc_add_expr_to_block (&null, error);
895 /* When POINTER is not NULL, we free it. */
896 gfc_start_block (&non_null);
897 tmp = build_call_expr_loc (input_location,
898 built_in_decls[BUILT_IN_FREE], 1,
899 fold_convert (pvoid_type_node, pointer));
900 gfc_add_expr_to_block (&non_null, tmp);
902 if (status != NULL_TREE && !integer_zerop (status))
904 /* We set STATUS to zero if it is present. */
905 tree status_type = TREE_TYPE (TREE_TYPE (status));
908 cond2 = fold_build2 (NE_EXPR, boolean_type_node, status,
909 build_int_cst (TREE_TYPE (status), 0));
910 tmp = fold_build2 (MODIFY_EXPR, status_type,
911 fold_build1 (INDIRECT_REF, status_type, status),
912 build_int_cst (status_type, 0));
913 tmp = fold_build3 (COND_EXPR, void_type_node, cond2, tmp,
914 build_empty_stmt (input_location));
915 gfc_add_expr_to_block (&non_null, tmp);
918 return fold_build3 (COND_EXPR, void_type_node, cond,
919 gfc_finish_block (&null), gfc_finish_block (&non_null));
923 /* Reallocate MEM so it has SIZE bytes of data. This behaves like the
924 following pseudo-code:
927 internal_realloc (void *mem, size_t size)
930 runtime_error ("Attempt to allocate a negative amount of memory.");
931 res = realloc (mem, size);
932 if (!res && size != 0)
933 _gfortran_os_error ("Out of memory");
941 gfc_call_realloc (stmtblock_t * block, tree mem, tree size)
943 tree msg, res, negative, nonzero, zero, null_result, tmp;
944 tree type = TREE_TYPE (mem);
946 size = gfc_evaluate_now (size, block);
948 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
949 size = fold_convert (size_type_node, size);
951 /* Create a variable to hold the result. */
952 res = gfc_create_var (type, NULL);
955 negative = fold_build2 (LT_EXPR, boolean_type_node, size,
956 build_int_cst (size_type_node, 0));
957 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
958 ("Attempt to allocate a negative amount of memory."));
959 tmp = fold_build3 (COND_EXPR, void_type_node, negative,
960 build_call_expr_loc (input_location,
961 gfor_fndecl_runtime_error, 1, msg),
962 build_empty_stmt (input_location));
963 gfc_add_expr_to_block (block, tmp);
965 /* Call realloc and check the result. */
966 tmp = build_call_expr_loc (input_location,
967 built_in_decls[BUILT_IN_REALLOC], 2,
968 fold_convert (pvoid_type_node, mem), size);
969 gfc_add_modify (block, res, fold_convert (type, tmp));
970 null_result = fold_build2 (EQ_EXPR, boolean_type_node, res,
971 build_int_cst (pvoid_type_node, 0));
972 nonzero = fold_build2 (NE_EXPR, boolean_type_node, size,
973 build_int_cst (size_type_node, 0));
974 null_result = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, null_result,
976 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
978 tmp = fold_build3 (COND_EXPR, void_type_node, null_result,
979 build_call_expr_loc (input_location,
980 gfor_fndecl_os_error, 1, msg),
981 build_empty_stmt (input_location));
982 gfc_add_expr_to_block (block, tmp);
984 /* if (size == 0) then the result is NULL. */
985 tmp = fold_build2 (MODIFY_EXPR, type, res, build_int_cst (type, 0));
986 zero = fold_build1 (TRUTH_NOT_EXPR, boolean_type_node, nonzero);
987 tmp = fold_build3 (COND_EXPR, void_type_node, zero, tmp,
988 build_empty_stmt (input_location));
989 gfc_add_expr_to_block (block, tmp);
994 /* Add a statement to a block. */
997 gfc_add_expr_to_block (stmtblock_t * block, tree expr)
1001 if (expr == NULL_TREE || IS_EMPTY_STMT (expr))
1006 if (TREE_CODE (block->head) != STATEMENT_LIST)
1011 block->head = NULL_TREE;
1012 append_to_statement_list (tmp, &block->head);
1014 append_to_statement_list (expr, &block->head);
1017 /* Don't bother creating a list if we only have a single statement. */
1022 /* Add a block the end of a block. */
1025 gfc_add_block_to_block (stmtblock_t * block, stmtblock_t * append)
1027 gcc_assert (append);
1028 gcc_assert (!append->has_scope);
1030 gfc_add_expr_to_block (block, append->head);
1031 append->head = NULL_TREE;
1035 /* Get the current locus. The structure may not be complete, and should
1036 only be used with gfc_set_backend_locus. */
1039 gfc_get_backend_locus (locus * loc)
1041 loc->lb = XCNEW (gfc_linebuf);
1042 loc->lb->location = input_location;
1043 loc->lb->file = gfc_current_backend_file;
1047 /* Set the current locus. */
1050 gfc_set_backend_locus (locus * loc)
1052 gfc_current_backend_file = loc->lb->file;
1053 input_location = loc->lb->location;
1057 /* Translate an executable statement. */
1060 gfc_trans_code (gfc_code * code)
1066 return build_empty_stmt (input_location);
1068 gfc_start_block (&block);
1070 /* Translate statements one by one into GENERIC trees until we reach
1071 the end of this gfc_code branch. */
1072 for (; code; code = code->next)
1074 if (code->here != 0)
1076 res = gfc_trans_label_here (code);
1077 gfc_add_expr_to_block (&block, res);
1083 case EXEC_END_BLOCK:
1084 case EXEC_END_PROCEDURE:
1089 res = gfc_trans_assign (code);
1092 case EXEC_LABEL_ASSIGN:
1093 res = gfc_trans_label_assign (code);
1096 case EXEC_POINTER_ASSIGN:
1097 res = gfc_trans_pointer_assign (code);
1100 case EXEC_INIT_ASSIGN:
1101 res = gfc_trans_init_assign (code);
1109 res = gfc_trans_cycle (code);
1113 res = gfc_trans_exit (code);
1117 res = gfc_trans_goto (code);
1121 res = gfc_trans_entry (code);
1125 res = gfc_trans_pause (code);
1129 res = gfc_trans_stop (code);
1133 /* For MVBITS we've got the special exception that we need a
1134 dependency check, too. */
1136 bool is_mvbits = false;
1137 if (code->resolved_isym
1138 && code->resolved_isym->id == GFC_ISYM_MVBITS)
1140 res = gfc_trans_call (code, is_mvbits, NULL_TREE,
1146 res = gfc_trans_call (code, false, NULL_TREE,
1150 case EXEC_ASSIGN_CALL:
1151 res = gfc_trans_call (code, true, NULL_TREE,
1156 res = gfc_trans_return (code);
1160 res = gfc_trans_if (code);
1163 case EXEC_ARITHMETIC_IF:
1164 res = gfc_trans_arithmetic_if (code);
1168 res = gfc_trans_do (code);
1172 res = gfc_trans_do_while (code);
1176 res = gfc_trans_select (code);
1180 res = gfc_trans_flush (code);
1184 res = gfc_trans_forall (code);
1188 res = gfc_trans_where (code);
1192 res = gfc_trans_allocate (code);
1195 case EXEC_DEALLOCATE:
1196 res = gfc_trans_deallocate (code);
1200 res = gfc_trans_open (code);
1204 res = gfc_trans_close (code);
1208 res = gfc_trans_read (code);
1212 res = gfc_trans_write (code);
1216 res = gfc_trans_iolength (code);
1219 case EXEC_BACKSPACE:
1220 res = gfc_trans_backspace (code);
1224 res = gfc_trans_endfile (code);
1228 res = gfc_trans_inquire (code);
1232 res = gfc_trans_wait (code);
1236 res = gfc_trans_rewind (code);
1240 res = gfc_trans_transfer (code);
1244 res = gfc_trans_dt_end (code);
1247 case EXEC_OMP_ATOMIC:
1248 case EXEC_OMP_BARRIER:
1249 case EXEC_OMP_CRITICAL:
1251 case EXEC_OMP_FLUSH:
1252 case EXEC_OMP_MASTER:
1253 case EXEC_OMP_ORDERED:
1254 case EXEC_OMP_PARALLEL:
1255 case EXEC_OMP_PARALLEL_DO:
1256 case EXEC_OMP_PARALLEL_SECTIONS:
1257 case EXEC_OMP_PARALLEL_WORKSHARE:
1258 case EXEC_OMP_SECTIONS:
1259 case EXEC_OMP_SINGLE:
1261 case EXEC_OMP_TASKWAIT:
1262 case EXEC_OMP_WORKSHARE:
1263 res = gfc_trans_omp_directive (code);
1267 internal_error ("gfc_trans_code(): Bad statement code");
1270 gfc_set_backend_locus (&code->loc);
1272 if (res != NULL_TREE && ! IS_EMPTY_STMT (res))
1274 if (TREE_CODE (res) == STATEMENT_LIST)
1275 tree_annotate_all_with_location (&res, input_location);
1277 SET_EXPR_LOCATION (res, input_location);
1279 /* Add the new statement to the block. */
1280 gfc_add_expr_to_block (&block, res);
1284 /* Return the finished block. */
1285 return gfc_finish_block (&block);
1289 /* This function is called after a complete program unit has been parsed
1293 gfc_generate_code (gfc_namespace * ns)
1296 if (ns->is_block_data)
1298 gfc_generate_block_data (ns);
1302 gfc_generate_function_code (ns);
1306 /* This function is called after a complete module has been parsed
1310 gfc_generate_module_code (gfc_namespace * ns)
1313 struct module_htab_entry *entry;
1315 gcc_assert (ns->proc_name->backend_decl == NULL);
1316 ns->proc_name->backend_decl
1317 = build_decl (ns->proc_name->declared_at.lb->location,
1318 NAMESPACE_DECL, get_identifier (ns->proc_name->name),
1320 entry = gfc_find_module (ns->proc_name->name);
1321 if (entry->namespace_decl)
1322 /* Buggy sourcecode, using a module before defining it? */
1323 htab_empty (entry->decls);
1324 entry->namespace_decl = ns->proc_name->backend_decl;
1326 gfc_generate_module_vars (ns);
1328 /* We need to generate all module function prototypes first, to allow
1330 for (n = ns->contained; n; n = n->sibling)
1337 gfc_create_function_decl (n);
1338 gcc_assert (DECL_CONTEXT (n->proc_name->backend_decl) == NULL_TREE);
1339 DECL_CONTEXT (n->proc_name->backend_decl) = ns->proc_name->backend_decl;
1340 gfc_module_add_decl (entry, n->proc_name->backend_decl);
1341 for (el = ns->entries; el; el = el->next)
1343 gcc_assert (DECL_CONTEXT (el->sym->backend_decl) == NULL_TREE);
1344 DECL_CONTEXT (el->sym->backend_decl) = ns->proc_name->backend_decl;
1345 gfc_module_add_decl (entry, el->sym->backend_decl);
1349 for (n = ns->contained; n; n = n->sibling)
1354 gfc_generate_function_code (n);