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 (tree expr, stmtblock_t * pblock)
139 if (CONSTANT_CLASS_P (expr))
142 var = gfc_create_var (TREE_TYPE (expr), NULL);
143 gfc_add_modify (pblock, var, expr);
149 /* Build a MODIFY_EXPR node and add it to a given statement block PBLOCK.
150 A MODIFY_EXPR is an assignment:
154 gfc_add_modify (stmtblock_t * pblock, tree lhs, tree rhs)
158 #ifdef ENABLE_CHECKING
160 t1 = TREE_TYPE (rhs);
161 t2 = TREE_TYPE (lhs);
162 /* Make sure that the types of the rhs and the lhs are the same
163 for scalar assignments. We should probably have something
164 similar for aggregates, but right now removing that check just
165 breaks everything. */
167 || AGGREGATE_TYPE_P (TREE_TYPE (lhs)));
170 tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, lhs,
172 gfc_add_expr_to_block (pblock, tmp);
176 /* Create a new scope/binding level and initialize a block. Care must be
177 taken when translating expressions as any temporaries will be placed in
178 the innermost scope. */
181 gfc_start_block (stmtblock_t * block)
183 /* Start a new binding level. */
185 block->has_scope = 1;
187 /* The block is empty. */
188 block->head = NULL_TREE;
192 /* Initialize a block without creating a new scope. */
195 gfc_init_block (stmtblock_t * block)
197 block->head = NULL_TREE;
198 block->has_scope = 0;
202 /* Sometimes we create a scope but it turns out that we don't actually
203 need it. This function merges the scope of BLOCK with its parent.
204 Only variable decls will be merged, you still need to add the code. */
207 gfc_merge_block_scope (stmtblock_t * block)
212 gcc_assert (block->has_scope);
213 block->has_scope = 0;
215 /* Remember the decls in this scope. */
219 /* Add them to the parent scope. */
220 while (decl != NULL_TREE)
222 next = DECL_CHAIN (decl);
223 DECL_CHAIN (decl) = NULL_TREE;
231 /* Finish a scope containing a block of statements. */
234 gfc_finish_block (stmtblock_t * stmtblock)
240 expr = stmtblock->head;
242 expr = build_empty_stmt (input_location);
244 stmtblock->head = NULL_TREE;
246 if (stmtblock->has_scope)
252 block = poplevel (1, 0, 0);
253 expr = build3_v (BIND_EXPR, decl, expr, block);
263 /* Build an ADDR_EXPR and cast the result to TYPE. If TYPE is NULL, the
264 natural type is used. */
267 gfc_build_addr_expr (tree type, tree t)
269 tree base_type = TREE_TYPE (t);
272 if (type && POINTER_TYPE_P (type)
273 && TREE_CODE (base_type) == ARRAY_TYPE
274 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
275 == TYPE_MAIN_VARIANT (TREE_TYPE (base_type)))
277 tree min_val = size_zero_node;
278 tree type_domain = TYPE_DOMAIN (base_type);
279 if (type_domain && TYPE_MIN_VALUE (type_domain))
280 min_val = TYPE_MIN_VALUE (type_domain);
281 t = fold (build4_loc (input_location, ARRAY_REF, TREE_TYPE (type),
282 t, min_val, NULL_TREE, NULL_TREE));
286 natural_type = build_pointer_type (base_type);
288 if (TREE_CODE (t) == INDIRECT_REF)
292 t = TREE_OPERAND (t, 0);
293 natural_type = TREE_TYPE (t);
297 tree base = get_base_address (t);
298 if (base && DECL_P (base))
299 TREE_ADDRESSABLE (base) = 1;
300 t = fold_build1_loc (input_location, ADDR_EXPR, natural_type, t);
303 if (type && natural_type != type)
304 t = convert (type, t);
310 /* Build an ARRAY_REF with its natural type. */
313 gfc_build_array_ref (tree base, tree offset, tree decl)
315 tree type = TREE_TYPE (base);
318 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
319 type = TREE_TYPE (type);
322 TREE_ADDRESSABLE (base) = 1;
324 /* Strip NON_LVALUE_EXPR nodes. */
325 STRIP_TYPE_NOPS (offset);
327 /* If the array reference is to a pointer, whose target contains a
328 subreference, use the span that is stored with the backend decl
329 and reference the element with pointer arithmetic. */
330 if (decl && (TREE_CODE (decl) == FIELD_DECL
331 || TREE_CODE (decl) == VAR_DECL
332 || TREE_CODE (decl) == PARM_DECL)
333 && GFC_DECL_SUBREF_ARRAY_P (decl)
334 && !integer_zerop (GFC_DECL_SPAN(decl)))
336 offset = fold_build2_loc (input_location, MULT_EXPR,
337 gfc_array_index_type,
338 offset, GFC_DECL_SPAN(decl));
339 tmp = gfc_build_addr_expr (pvoid_type_node, base);
340 tmp = fold_build2_loc (input_location, POINTER_PLUS_EXPR,
341 pvoid_type_node, tmp,
342 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_loc (input_location, ARRAY_REF, type, base, offset,
351 NULL_TREE, NULL_TREE);
355 /* Generate a call to print a runtime error possibly including multiple
356 arguments and a locus. */
359 gfc_trans_runtime_error (bool error, locus* where, const char* msgid, ...)
363 va_start (ap, msgid);
364 return gfc_trans_runtime_error_vararg (error, where, msgid, ap);
368 gfc_trans_runtime_error_vararg (bool error, locus* where, const char* msgid,
380 /* Compute the number of extra arguments from the format string. */
381 for (p = msgid, nargs = 0; *p; p++)
389 /* The code to generate the error. */
390 gfc_start_block (&block);
394 line = LOCATION_LINE (where->lb->location);
395 asprintf (&message, "At line %d of file %s", line,
396 where->lb->file->filename);
399 asprintf (&message, "In file '%s', around line %d",
400 gfc_source_file, input_line + 1);
402 arg = gfc_build_addr_expr (pchar_type_node,
403 gfc_build_localized_cstring_const (message));
406 asprintf (&message, "%s", _(msgid));
407 arg2 = gfc_build_addr_expr (pchar_type_node,
408 gfc_build_localized_cstring_const (message));
411 /* Build the argument array. */
412 argarray = XALLOCAVEC (tree, nargs + 2);
415 for (i = 0; i < nargs; i++)
416 argarray[2 + i] = va_arg (ap, tree);
419 /* Build the function call to runtime_(warning,error)_at; because of the
420 variable number of arguments, we can't use build_call_expr_loc dinput_location,
423 fntype = TREE_TYPE (gfor_fndecl_runtime_error_at);
425 fntype = TREE_TYPE (gfor_fndecl_runtime_warning_at);
427 tmp = fold_builtin_call_array (input_location, TREE_TYPE (fntype),
428 fold_build1_loc (input_location, ADDR_EXPR,
429 build_pointer_type (fntype),
431 ? gfor_fndecl_runtime_error_at
432 : gfor_fndecl_runtime_warning_at),
433 nargs + 2, argarray);
434 gfc_add_expr_to_block (&block, tmp);
436 return gfc_finish_block (&block);
440 /* Generate a runtime error if COND is true. */
443 gfc_trans_runtime_check (bool error, bool once, tree cond, stmtblock_t * pblock,
444 locus * where, const char * msgid, ...)
452 if (integer_zerop (cond))
457 tmpvar = gfc_create_var (boolean_type_node, "print_warning");
458 TREE_STATIC (tmpvar) = 1;
459 DECL_INITIAL (tmpvar) = boolean_true_node;
460 gfc_add_expr_to_block (pblock, tmpvar);
463 gfc_start_block (&block);
465 /* The code to generate the error. */
466 va_start (ap, msgid);
467 gfc_add_expr_to_block (&block,
468 gfc_trans_runtime_error_vararg (error, where,
472 gfc_add_modify (&block, tmpvar, boolean_false_node);
474 body = gfc_finish_block (&block);
476 if (integer_onep (cond))
478 gfc_add_expr_to_block (pblock, body);
482 /* Tell the compiler that this isn't likely. */
484 cond = fold_build2_loc (input_location, TRUTH_AND_EXPR,
485 long_integer_type_node, tmpvar, cond);
487 cond = fold_convert (long_integer_type_node, cond);
489 tmp = build_int_cst (long_integer_type_node, 0);
490 cond = build_call_expr_loc (input_location,
491 built_in_decls[BUILT_IN_EXPECT], 2, cond, tmp);
492 cond = fold_convert (boolean_type_node, cond);
494 tmp = build3_v (COND_EXPR, cond, body, build_empty_stmt (input_location));
495 gfc_add_expr_to_block (pblock, tmp);
500 /* Call malloc to allocate size bytes of memory, with special conditions:
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, 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 gfc_start_block (&block2);
520 size = fold_build2_loc (input_location, MAX_EXPR, size_type_node, size,
521 build_int_cst (size_type_node, 1));
523 gfc_add_modify (&block2, res,
524 fold_convert (prvoid_type_node,
525 build_call_expr_loc (input_location,
526 built_in_decls[BUILT_IN_MALLOC], 1, size)));
528 /* Optionally check whether malloc was successful. */
529 if (gfc_option.rtcheck & GFC_RTCHECK_MEM)
531 null_result = fold_build2_loc (input_location, EQ_EXPR,
532 boolean_type_node, res,
533 build_int_cst (pvoid_type_node, 0));
534 msg = gfc_build_addr_expr (pchar_type_node,
535 gfc_build_localized_cstring_const ("Memory allocation failed"));
536 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
538 build_call_expr_loc (input_location,
539 gfor_fndecl_os_error, 1, msg),
540 build_empty_stmt (input_location));
541 gfc_add_expr_to_block (&block2, tmp);
544 malloc_result = gfc_finish_block (&block2);
546 gfc_add_expr_to_block (block, malloc_result);
549 res = fold_convert (type, res);
554 /* Allocate memory, using an optional status argument.
556 This function follows the following pseudo-code:
559 allocate (size_t size, integer_type* stat)
566 // The only time this can happen is the size wraps around.
571 *stat = LIBERROR_ALLOCATION;
575 runtime_error ("Attempt to allocate negative amount of memory. "
576 "Possible integer overflow");
580 newmem = malloc (MAX (size, 1));
584 *stat = LIBERROR_ALLOCATION;
586 runtime_error ("Out of memory");
593 gfc_allocate_with_status (stmtblock_t * block, tree size, tree status)
595 stmtblock_t alloc_block;
596 tree res, tmp, error, msg, cond;
597 tree status_type = status ? TREE_TYPE (TREE_TYPE (status)) : NULL_TREE;
599 /* Evaluate size only once, and make sure it has the right type. */
600 size = gfc_evaluate_now (size, block);
601 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
602 size = fold_convert (size_type_node, size);
604 /* Create a variable to hold the result. */
605 res = gfc_create_var (prvoid_type_node, NULL);
607 /* Set the optional status variable to zero. */
608 if (status != NULL_TREE && !integer_zerop (status))
610 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
611 fold_build1_loc (input_location, INDIRECT_REF,
612 status_type, status),
613 build_int_cst (status_type, 0));
614 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
615 fold_build2_loc (input_location, NE_EXPR,
616 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_loc (input_location, INDIRECT_REF,
637 status_type, status),
638 build_int_cst (status_type, LIBERROR_ALLOCATION));
639 gfc_add_modify (&set_status_block, res,
640 build_int_cst (prvoid_type_node, 0));
642 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
643 status, build_int_cst (TREE_TYPE (status), 0));
644 error = fold_build3_loc (input_location, COND_EXPR, void_type_node, tmp,
645 error, gfc_finish_block (&set_status_block));
648 /* The allocation itself. */
649 gfc_start_block (&alloc_block);
650 gfc_add_modify (&alloc_block, res,
651 fold_convert (prvoid_type_node,
652 build_call_expr_loc (input_location,
653 built_in_decls[BUILT_IN_MALLOC], 1,
654 fold_build2_loc (input_location,
655 MAX_EXPR, size_type_node, size,
656 build_int_cst (size_type_node,
659 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
661 tmp = build_call_expr_loc (input_location,
662 gfor_fndecl_os_error, 1, msg);
664 if (status != NULL_TREE && !integer_zerop (status))
666 /* Set the status variable if it's present. */
669 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
670 status, build_int_cst (TREE_TYPE (status), 0));
671 tmp2 = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
672 fold_build1_loc (input_location, INDIRECT_REF,
673 status_type, status),
674 build_int_cst (status_type, LIBERROR_ALLOCATION));
675 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
679 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
680 fold_build2_loc (input_location, EQ_EXPR,
681 boolean_type_node, res,
682 build_int_cst (prvoid_type_node, 0)),
683 tmp, build_empty_stmt (input_location));
684 gfc_add_expr_to_block (&alloc_block, tmp);
686 cond = fold_build2_loc (input_location, LT_EXPR, boolean_type_node, size,
687 build_int_cst (TREE_TYPE (size), 0));
688 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, error,
689 gfc_finish_block (&alloc_block));
690 gfc_add_expr_to_block (block, tmp);
696 /* Generate code for an ALLOCATE statement when the argument is an
697 allocatable array. If the array is currently allocated, it is an
698 error to allocate it again.
700 This function follows the following pseudo-code:
703 allocate_array (void *mem, size_t size, integer_type *stat)
706 return allocate (size, stat);
712 mem = allocate (size, stat);
713 *stat = LIBERROR_ALLOCATION;
717 runtime_error ("Attempting to allocate already allocated variable");
721 expr must be set to the original expression being allocated for its locus
722 and variable name in case a runtime error has to be printed. */
724 gfc_allocate_array_with_status (stmtblock_t * block, tree mem, tree size,
725 tree status, gfc_expr* expr)
727 stmtblock_t alloc_block;
728 tree res, tmp, null_mem, alloc, error;
729 tree type = TREE_TYPE (mem);
731 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
732 size = fold_convert (size_type_node, size);
734 /* Create a variable to hold the result. */
735 res = gfc_create_var (type, NULL);
736 null_mem = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, mem,
737 build_int_cst (type, 0));
739 /* If mem is NULL, we call gfc_allocate_with_status. */
740 gfc_start_block (&alloc_block);
741 tmp = gfc_allocate_with_status (&alloc_block, size, status);
742 gfc_add_modify (&alloc_block, res, fold_convert (type, tmp));
743 alloc = gfc_finish_block (&alloc_block);
745 /* Otherwise, we issue a runtime error or set the status variable. */
750 gcc_assert (expr->expr_type == EXPR_VARIABLE && expr->symtree);
751 varname = gfc_build_cstring_const (expr->symtree->name);
752 varname = gfc_build_addr_expr (pchar_type_node, varname);
754 error = gfc_trans_runtime_error (true, &expr->where,
755 "Attempting to allocate already"
756 " allocated variable '%s'",
760 error = gfc_trans_runtime_error (true, NULL,
761 "Attempting to allocate already allocated"
764 if (status != NULL_TREE && !integer_zerop (status))
766 tree status_type = TREE_TYPE (TREE_TYPE (status));
767 stmtblock_t set_status_block;
769 gfc_start_block (&set_status_block);
770 tmp = build_call_expr_loc (input_location,
771 built_in_decls[BUILT_IN_FREE], 1,
772 fold_convert (pvoid_type_node, mem));
773 gfc_add_expr_to_block (&set_status_block, tmp);
775 tmp = gfc_allocate_with_status (&set_status_block, size, status);
776 gfc_add_modify (&set_status_block, res, fold_convert (type, tmp));
778 gfc_add_modify (&set_status_block,
779 fold_build1_loc (input_location, INDIRECT_REF,
780 status_type, status),
781 build_int_cst (status_type, LIBERROR_ALLOCATION));
783 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
784 status, build_int_cst (status_type, 0));
785 error = fold_build3_loc (input_location, COND_EXPR, void_type_node, tmp,
786 error, gfc_finish_block (&set_status_block));
789 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, null_mem,
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_loc (input_location, 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_loc (input_location, 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_loc (input_location, 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_loc (input_location, NE_EXPR, boolean_type_node,
886 status, build_int_cst (TREE_TYPE (status), 0));
887 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
888 fold_build1_loc (input_location, INDIRECT_REF,
889 status_type, status),
890 build_int_cst (status_type, 1));
891 error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
895 gfc_add_expr_to_block (&null, error);
897 /* When POINTER is not NULL, we free it. */
898 gfc_start_block (&non_null);
899 tmp = build_call_expr_loc (input_location,
900 built_in_decls[BUILT_IN_FREE], 1,
901 fold_convert (pvoid_type_node, pointer));
902 gfc_add_expr_to_block (&non_null, tmp);
904 if (status != NULL_TREE && !integer_zerop (status))
906 /* We set STATUS to zero if it is present. */
907 tree status_type = TREE_TYPE (TREE_TYPE (status));
910 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
911 status, build_int_cst (TREE_TYPE (status), 0));
912 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
913 fold_build1_loc (input_location, INDIRECT_REF,
914 status_type, status),
915 build_int_cst (status_type, 0));
916 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
917 tmp, build_empty_stmt (input_location));
918 gfc_add_expr_to_block (&non_null, tmp);
921 return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
922 gfc_finish_block (&null),
923 gfc_finish_block (&non_null));
927 /* Reallocate MEM so it has SIZE bytes of data. This behaves like the
928 following pseudo-code:
931 internal_realloc (void *mem, size_t size)
934 runtime_error ("Attempt to allocate a negative amount of memory.");
935 res = realloc (mem, size);
936 if (!res && size != 0)
937 _gfortran_os_error ("Out of memory");
945 gfc_call_realloc (stmtblock_t * block, tree mem, tree size)
947 tree msg, res, negative, nonzero, zero, null_result, tmp;
948 tree type = TREE_TYPE (mem);
950 size = gfc_evaluate_now (size, block);
952 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
953 size = fold_convert (size_type_node, size);
955 /* Create a variable to hold the result. */
956 res = gfc_create_var (type, NULL);
959 negative = fold_build2_loc (input_location, LT_EXPR, boolean_type_node, size,
960 build_int_cst (size_type_node, 0));
961 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
962 ("Attempt to allocate a negative amount of memory."));
963 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, negative,
964 build_call_expr_loc (input_location,
965 gfor_fndecl_runtime_error, 1, msg),
966 build_empty_stmt (input_location));
967 gfc_add_expr_to_block (block, tmp);
969 /* Call realloc and check the result. */
970 tmp = build_call_expr_loc (input_location,
971 built_in_decls[BUILT_IN_REALLOC], 2,
972 fold_convert (pvoid_type_node, mem), size);
973 gfc_add_modify (block, res, fold_convert (type, tmp));
974 null_result = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
975 res, build_int_cst (pvoid_type_node, 0));
976 nonzero = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, size,
977 build_int_cst (size_type_node, 0));
978 null_result = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
979 null_result, nonzero);
980 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
982 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
984 build_call_expr_loc (input_location,
985 gfor_fndecl_os_error, 1, msg),
986 build_empty_stmt (input_location));
987 gfc_add_expr_to_block (block, tmp);
989 /* if (size == 0) then the result is NULL. */
990 tmp = fold_build2_loc (input_location, MODIFY_EXPR, type, res,
991 build_int_cst (type, 0));
992 zero = fold_build1_loc (input_location, TRUTH_NOT_EXPR, boolean_type_node,
994 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, zero, tmp,
995 build_empty_stmt (input_location));
996 gfc_add_expr_to_block (block, tmp);
1002 /* Add an expression to another one, either at the front or the back. */
1005 add_expr_to_chain (tree* chain, tree expr, bool front)
1007 if (expr == NULL_TREE || IS_EMPTY_STMT (expr))
1012 if (TREE_CODE (*chain) != STATEMENT_LIST)
1018 append_to_statement_list (tmp, chain);
1023 tree_stmt_iterator i;
1025 i = tsi_start (*chain);
1026 tsi_link_before (&i, expr, TSI_CONTINUE_LINKING);
1029 append_to_statement_list (expr, chain);
1035 /* Add a statement to a block. */
1038 gfc_add_expr_to_block (stmtblock_t * block, tree expr)
1041 add_expr_to_chain (&block->head, expr, false);
1045 /* Add a block the end of a block. */
1048 gfc_add_block_to_block (stmtblock_t * block, stmtblock_t * append)
1050 gcc_assert (append);
1051 gcc_assert (!append->has_scope);
1053 gfc_add_expr_to_block (block, append->head);
1054 append->head = NULL_TREE;
1058 /* Save the current locus. The structure may not be complete, and should
1059 only be used with gfc_restore_backend_locus. */
1062 gfc_save_backend_locus (locus * loc)
1064 loc->lb = XCNEW (gfc_linebuf);
1065 loc->lb->location = input_location;
1066 loc->lb->file = gfc_current_backend_file;
1070 /* Set the current locus. */
1073 gfc_set_backend_locus (locus * loc)
1075 gfc_current_backend_file = loc->lb->file;
1076 input_location = loc->lb->location;
1080 /* Restore the saved locus. Only used in conjonction with
1081 gfc_save_backend_locus, to free the memory when we are done. */
1084 gfc_restore_backend_locus (locus * loc)
1086 gfc_set_backend_locus (loc);
1091 /* Translate an executable statement. The tree cond is used by gfc_trans_do.
1092 This static function is wrapped by gfc_trans_code_cond and
1096 trans_code (gfc_code * code, tree cond)
1102 return build_empty_stmt (input_location);
1104 gfc_start_block (&block);
1106 /* Translate statements one by one into GENERIC trees until we reach
1107 the end of this gfc_code branch. */
1108 for (; code; code = code->next)
1110 if (code->here != 0)
1112 res = gfc_trans_label_here (code);
1113 gfc_add_expr_to_block (&block, res);
1116 gfc_set_backend_locus (&code->loc);
1121 case EXEC_END_BLOCK:
1122 case EXEC_END_PROCEDURE:
1127 if (code->expr1->ts.type == BT_CLASS)
1128 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1130 res = gfc_trans_assign (code);
1133 case EXEC_LABEL_ASSIGN:
1134 res = gfc_trans_label_assign (code);
1137 case EXEC_POINTER_ASSIGN:
1138 if (code->expr1->ts.type == BT_CLASS)
1139 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1141 res = gfc_trans_pointer_assign (code);
1144 case EXEC_INIT_ASSIGN:
1145 if (code->expr1->ts.type == BT_CLASS)
1146 res = gfc_trans_class_init_assign (code);
1148 res = gfc_trans_init_assign (code);
1156 res = gfc_trans_critical (code);
1160 res = gfc_trans_cycle (code);
1164 res = gfc_trans_exit (code);
1168 res = gfc_trans_goto (code);
1172 res = gfc_trans_entry (code);
1176 res = gfc_trans_pause (code);
1180 case EXEC_ERROR_STOP:
1181 res = gfc_trans_stop (code, code->op == EXEC_ERROR_STOP);
1185 /* For MVBITS we've got the special exception that we need a
1186 dependency check, too. */
1188 bool is_mvbits = false;
1189 if (code->resolved_isym
1190 && code->resolved_isym->id == GFC_ISYM_MVBITS)
1192 if (code->resolved_isym
1193 && code->resolved_isym->id == GFC_ISYM_MOVE_ALLOC)
1194 res = gfc_conv_intrinsic_move_alloc (code);
1196 res = gfc_trans_call (code, is_mvbits, NULL_TREE,
1202 res = gfc_trans_call (code, false, NULL_TREE,
1206 case EXEC_ASSIGN_CALL:
1207 res = gfc_trans_call (code, true, NULL_TREE,
1212 res = gfc_trans_return (code);
1216 res = gfc_trans_if (code);
1219 case EXEC_ARITHMETIC_IF:
1220 res = gfc_trans_arithmetic_if (code);
1224 res = gfc_trans_block_construct (code);
1228 res = gfc_trans_do (code, cond);
1232 res = gfc_trans_do_while (code);
1236 res = gfc_trans_select (code);
1239 case EXEC_SELECT_TYPE:
1240 /* Do nothing. SELECT TYPE statements should be transformed into
1241 an ordinary SELECT CASE at resolution stage.
1242 TODO: Add an error message here once this is done. */
1247 res = gfc_trans_flush (code);
1251 case EXEC_SYNC_IMAGES:
1252 case EXEC_SYNC_MEMORY:
1253 res = gfc_trans_sync (code, code->op);
1257 res = gfc_trans_forall (code);
1261 res = gfc_trans_where (code);
1265 res = gfc_trans_allocate (code);
1268 case EXEC_DEALLOCATE:
1269 res = gfc_trans_deallocate (code);
1273 res = gfc_trans_open (code);
1277 res = gfc_trans_close (code);
1281 res = gfc_trans_read (code);
1285 res = gfc_trans_write (code);
1289 res = gfc_trans_iolength (code);
1292 case EXEC_BACKSPACE:
1293 res = gfc_trans_backspace (code);
1297 res = gfc_trans_endfile (code);
1301 res = gfc_trans_inquire (code);
1305 res = gfc_trans_wait (code);
1309 res = gfc_trans_rewind (code);
1313 res = gfc_trans_transfer (code);
1317 res = gfc_trans_dt_end (code);
1320 case EXEC_OMP_ATOMIC:
1321 case EXEC_OMP_BARRIER:
1322 case EXEC_OMP_CRITICAL:
1324 case EXEC_OMP_FLUSH:
1325 case EXEC_OMP_MASTER:
1326 case EXEC_OMP_ORDERED:
1327 case EXEC_OMP_PARALLEL:
1328 case EXEC_OMP_PARALLEL_DO:
1329 case EXEC_OMP_PARALLEL_SECTIONS:
1330 case EXEC_OMP_PARALLEL_WORKSHARE:
1331 case EXEC_OMP_SECTIONS:
1332 case EXEC_OMP_SINGLE:
1334 case EXEC_OMP_TASKWAIT:
1335 case EXEC_OMP_WORKSHARE:
1336 res = gfc_trans_omp_directive (code);
1340 internal_error ("gfc_trans_code(): Bad statement code");
1343 gfc_set_backend_locus (&code->loc);
1345 if (res != NULL_TREE && ! IS_EMPTY_STMT (res))
1347 if (TREE_CODE (res) != STATEMENT_LIST)
1348 SET_EXPR_LOCATION (res, input_location);
1350 /* Add the new statement to the block. */
1351 gfc_add_expr_to_block (&block, res);
1355 /* Return the finished block. */
1356 return gfc_finish_block (&block);
1360 /* Translate an executable statement with condition, cond. The condition is
1361 used by gfc_trans_do to test for IO result conditions inside implied
1362 DO loops of READ and WRITE statements. See build_dt in trans-io.c. */
1365 gfc_trans_code_cond (gfc_code * code, tree cond)
1367 return trans_code (code, cond);
1370 /* Translate an executable statement without condition. */
1373 gfc_trans_code (gfc_code * code)
1375 return trans_code (code, NULL_TREE);
1379 /* This function is called after a complete program unit has been parsed
1383 gfc_generate_code (gfc_namespace * ns)
1386 if (ns->is_block_data)
1388 gfc_generate_block_data (ns);
1392 gfc_generate_function_code (ns);
1396 /* This function is called after a complete module has been parsed
1400 gfc_generate_module_code (gfc_namespace * ns)
1403 struct module_htab_entry *entry;
1405 gcc_assert (ns->proc_name->backend_decl == NULL);
1406 ns->proc_name->backend_decl
1407 = build_decl (ns->proc_name->declared_at.lb->location,
1408 NAMESPACE_DECL, get_identifier (ns->proc_name->name),
1410 entry = gfc_find_module (ns->proc_name->name);
1411 if (entry->namespace_decl)
1412 /* Buggy sourcecode, using a module before defining it? */
1413 htab_empty (entry->decls);
1414 entry->namespace_decl = ns->proc_name->backend_decl;
1416 gfc_generate_module_vars (ns);
1418 /* We need to generate all module function prototypes first, to allow
1420 for (n = ns->contained; n; n = n->sibling)
1427 gfc_create_function_decl (n, false);
1428 DECL_CONTEXT (n->proc_name->backend_decl) = ns->proc_name->backend_decl;
1429 gfc_module_add_decl (entry, n->proc_name->backend_decl);
1430 for (el = ns->entries; el; el = el->next)
1432 DECL_CONTEXT (el->sym->backend_decl) = ns->proc_name->backend_decl;
1433 gfc_module_add_decl (entry, el->sym->backend_decl);
1437 for (n = ns->contained; n; n = n->sibling)
1442 gfc_generate_function_code (n);
1447 /* Initialize an init/cleanup block with existing code. */
1450 gfc_start_wrapped_block (gfc_wrapped_block* block, tree code)
1454 block->init = NULL_TREE;
1456 block->cleanup = NULL_TREE;
1460 /* Add a new pair of initializers/clean-up code. */
1463 gfc_add_init_cleanup (gfc_wrapped_block* block, tree init, tree cleanup)
1467 /* The new pair of init/cleanup should be "wrapped around" the existing
1468 block of code, thus the initialization is added to the front and the
1469 cleanup to the back. */
1470 add_expr_to_chain (&block->init, init, true);
1471 add_expr_to_chain (&block->cleanup, cleanup, false);
1475 /* Finish up a wrapped block by building a corresponding try-finally expr. */
1478 gfc_finish_wrapped_block (gfc_wrapped_block* block)
1484 /* Build the final expression. For this, just add init and body together,
1485 and put clean-up with that into a TRY_FINALLY_EXPR. */
1486 result = block->init;
1487 add_expr_to_chain (&result, block->code, false);
1489 result = build2_loc (input_location, TRY_FINALLY_EXPR, void_type_node,
1490 result, block->cleanup);
1492 /* Clear the block. */
1493 block->init = NULL_TREE;
1494 block->code = NULL_TREE;
1495 block->cleanup = NULL_TREE;