1 /* Code translation -- generate GCC trees from gfc_code.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software
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
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. */
166 gcc_assert (TREE_TYPE (rhs) == TREE_TYPE (lhs)
167 || AGGREGATE_TYPE_P (TREE_TYPE (lhs)));
170 tmp = fold_build2 (MODIFY_EXPR, void_type_node, lhs, rhs);
171 gfc_add_expr_to_block (pblock, tmp);
175 /* Create a new scope/binding level and initialize a block. Care must be
176 taken when translating expressions as any temporaries will be placed in
177 the innermost scope. */
180 gfc_start_block (stmtblock_t * block)
182 /* Start a new binding level. */
184 block->has_scope = 1;
186 /* The block is empty. */
187 block->head = NULL_TREE;
191 /* Initialize a block without creating a new scope. */
194 gfc_init_block (stmtblock_t * block)
196 block->head = NULL_TREE;
197 block->has_scope = 0;
201 /* Sometimes we create a scope but it turns out that we don't actually
202 need it. This function merges the scope of BLOCK with its parent.
203 Only variable decls will be merged, you still need to add the code. */
206 gfc_merge_block_scope (stmtblock_t * block)
211 gcc_assert (block->has_scope);
212 block->has_scope = 0;
214 /* Remember the decls in this scope. */
218 /* Add them to the parent scope. */
219 while (decl != NULL_TREE)
221 next = TREE_CHAIN (decl);
222 TREE_CHAIN (decl) = NULL_TREE;
230 /* Finish a scope containing a block of statements. */
233 gfc_finish_block (stmtblock_t * stmtblock)
239 expr = stmtblock->head;
241 expr = build_empty_stmt ();
243 stmtblock->head = NULL_TREE;
245 if (stmtblock->has_scope)
251 block = poplevel (1, 0, 0);
252 expr = build3_v (BIND_EXPR, decl, expr, block);
262 /* Build an ADDR_EXPR and cast the result to TYPE. If TYPE is NULL, the
263 natural type is used. */
266 gfc_build_addr_expr (tree type, tree t)
268 tree base_type = TREE_TYPE (t);
271 if (type && POINTER_TYPE_P (type)
272 && TREE_CODE (base_type) == ARRAY_TYPE
273 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
274 == TYPE_MAIN_VARIANT (TREE_TYPE (base_type)))
276 tree min_val = size_zero_node;
277 tree type_domain = TYPE_DOMAIN (base_type);
278 if (type_domain && TYPE_MIN_VALUE (type_domain))
279 min_val = TYPE_MIN_VALUE (type_domain);
280 t = fold (build4 (ARRAY_REF, TREE_TYPE (type),
281 t, min_val, NULL_TREE, NULL_TREE));
285 natural_type = build_pointer_type (base_type);
287 if (TREE_CODE (t) == INDIRECT_REF)
291 t = TREE_OPERAND (t, 0);
292 natural_type = TREE_TYPE (t);
297 TREE_ADDRESSABLE (t) = 1;
298 t = fold_build1 (ADDR_EXPR, natural_type, t);
301 if (type && natural_type != type)
302 t = convert (type, t);
308 /* Build an ARRAY_REF with its natural type. */
311 gfc_build_array_ref (tree base, tree offset, tree decl)
313 tree type = TREE_TYPE (base);
316 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
317 type = TREE_TYPE (type);
320 TREE_ADDRESSABLE (base) = 1;
322 /* Strip NON_LVALUE_EXPR nodes. */
323 STRIP_TYPE_NOPS (offset);
325 /* If the array reference is to a pointer, whose target contains a
326 subreference, use the span that is stored with the backend decl
327 and reference the element with pointer arithmetic. */
328 if (decl && (TREE_CODE (decl) == FIELD_DECL
329 || TREE_CODE (decl) == VAR_DECL
330 || TREE_CODE (decl) == PARM_DECL)
331 && GFC_DECL_SUBREF_ARRAY_P (decl)
332 && !integer_zerop (GFC_DECL_SPAN(decl)))
334 offset = fold_build2 (MULT_EXPR, gfc_array_index_type,
335 offset, GFC_DECL_SPAN(decl));
336 tmp = gfc_build_addr_expr (pvoid_type_node, base);
337 tmp = fold_build2 (POINTER_PLUS_EXPR, pvoid_type_node,
338 tmp, fold_convert (sizetype, offset));
339 tmp = fold_convert (build_pointer_type (type), tmp);
340 if (!TYPE_STRING_FLAG (type))
341 tmp = build_fold_indirect_ref (tmp);
345 /* Otherwise use a straightforward array reference. */
346 return build4 (ARRAY_REF, type, base, offset, NULL_TREE, NULL_TREE);
350 /* Generate a runtime error if COND is true. */
353 gfc_trans_runtime_check (bool error, bool once, tree cond, stmtblock_t * pblock,
354 locus * where, const char * msgid, ...)
368 if (integer_zerop (cond))
371 /* Compute the number of extra arguments from the format string. */
372 for (p = msgid, nargs = 0; *p; p++)
382 tmpvar = gfc_create_var (boolean_type_node, "print_warning");
383 TREE_STATIC (tmpvar) = 1;
384 DECL_INITIAL (tmpvar) = boolean_true_node;
385 gfc_add_expr_to_block (pblock, tmpvar);
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 va_start (ap, msgid);
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 directly. */
422 fntype = TREE_TYPE (gfor_fndecl_runtime_error_at);
424 fntype = TREE_TYPE (gfor_fndecl_runtime_warning_at);
426 tmp = fold_builtin_call_array (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);
436 gfc_add_modify (&block, tmpvar, boolean_false_node);
438 body = gfc_finish_block (&block);
440 if (integer_onep (cond))
442 gfc_add_expr_to_block (pblock, body);
446 /* Tell the compiler that this isn't likely. */
448 cond = fold_build2 (TRUTH_AND_EXPR, long_integer_type_node, tmpvar,
451 cond = fold_convert (long_integer_type_node, cond);
453 tmp = build_int_cst (long_integer_type_node, 0);
454 cond = build_call_expr (built_in_decls[BUILT_IN_EXPECT], 2, cond, tmp);
455 cond = fold_convert (boolean_type_node, cond);
457 tmp = build3_v (COND_EXPR, cond, body, build_empty_stmt ());
458 gfc_add_expr_to_block (pblock, tmp);
463 /* Call malloc to allocate size bytes of memory, with special conditions:
464 + if size < 0, generate a runtime error,
465 + if size == 0, return a malloced area of size 1,
466 + if malloc returns NULL, issue a runtime error. */
468 gfc_call_malloc (stmtblock_t * block, tree type, tree size)
470 tree tmp, msg, negative, malloc_result, null_result, res;
473 size = gfc_evaluate_now (size, block);
475 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
476 size = fold_convert (size_type_node, size);
478 /* Create a variable to hold the result. */
479 res = gfc_create_var (pvoid_type_node, NULL);
482 negative = fold_build2 (LT_EXPR, boolean_type_node, size,
483 build_int_cst (size_type_node, 0));
484 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
485 ("Attempt to allocate a negative amount of memory."));
486 tmp = fold_build3 (COND_EXPR, void_type_node, negative,
487 build_call_expr (gfor_fndecl_runtime_error, 1, msg),
488 build_empty_stmt ());
489 gfc_add_expr_to_block (block, tmp);
491 /* Call malloc and check the result. */
492 gfc_start_block (&block2);
494 size = fold_build2 (MAX_EXPR, size_type_node, size,
495 build_int_cst (size_type_node, 1));
497 gfc_add_modify (&block2, res,
498 build_call_expr (built_in_decls[BUILT_IN_MALLOC], 1,
500 null_result = fold_build2 (EQ_EXPR, boolean_type_node, res,
501 build_int_cst (pvoid_type_node, 0));
502 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
503 ("Memory allocation failed"));
504 tmp = fold_build3 (COND_EXPR, void_type_node, null_result,
505 build_call_expr (gfor_fndecl_os_error, 1, msg),
506 build_empty_stmt ());
507 gfc_add_expr_to_block (&block2, tmp);
508 malloc_result = gfc_finish_block (&block2);
510 gfc_add_expr_to_block (block, malloc_result);
513 res = fold_convert (type, res);
517 /* Allocate memory, using an optional status argument.
519 This function follows the following pseudo-code:
522 allocate (size_t size, integer_type* stat)
529 // The only time this can happen is the size wraps around.
534 *stat = LIBERROR_ALLOCATION;
538 runtime_error ("Attempt to allocate negative amount of memory. "
539 "Possible integer overflow");
543 newmem = malloc (MAX (size, 1));
547 *stat = LIBERROR_ALLOCATION;
549 runtime_error ("Out of memory");
556 gfc_allocate_with_status (stmtblock_t * block, tree size, tree status)
558 stmtblock_t alloc_block;
559 tree res, tmp, error, msg, cond;
560 tree status_type = status ? TREE_TYPE (TREE_TYPE (status)) : NULL_TREE;
562 /* Evaluate size only once, and make sure it has the right type. */
563 size = gfc_evaluate_now (size, block);
564 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
565 size = fold_convert (size_type_node, size);
567 /* Create a variable to hold the result. */
568 res = gfc_create_var (pvoid_type_node, NULL);
570 /* Set the optional status variable to zero. */
571 if (status != NULL_TREE && !integer_zerop (status))
573 tmp = fold_build2 (MODIFY_EXPR, status_type,
574 fold_build1 (INDIRECT_REF, status_type, status),
575 build_int_cst (status_type, 0));
576 tmp = fold_build3 (COND_EXPR, void_type_node,
577 fold_build2 (NE_EXPR, boolean_type_node,
578 status, build_int_cst (status_type, 0)),
579 tmp, build_empty_stmt ());
580 gfc_add_expr_to_block (block, tmp);
583 /* Generate the block of code handling (size < 0). */
584 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
585 ("Attempt to allocate negative amount of memory. "
586 "Possible integer overflow"));
587 error = build_call_expr (gfor_fndecl_runtime_error, 1, msg);
589 if (status != NULL_TREE && !integer_zerop (status))
591 /* Set the status variable if it's present. */
592 stmtblock_t set_status_block;
594 gfc_start_block (&set_status_block);
595 gfc_add_modify (&set_status_block,
596 fold_build1 (INDIRECT_REF, status_type, status),
597 build_int_cst (status_type, LIBERROR_ALLOCATION));
598 gfc_add_modify (&set_status_block, res,
599 build_int_cst (pvoid_type_node, 0));
601 tmp = fold_build2 (EQ_EXPR, boolean_type_node, status,
602 build_int_cst (status_type, 0));
603 error = fold_build3 (COND_EXPR, void_type_node, tmp, error,
604 gfc_finish_block (&set_status_block));
607 /* The allocation itself. */
608 gfc_start_block (&alloc_block);
609 gfc_add_modify (&alloc_block, res,
610 build_call_expr (built_in_decls[BUILT_IN_MALLOC], 1,
611 fold_build2 (MAX_EXPR, size_type_node,
613 build_int_cst (size_type_node, 1))));
615 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
617 tmp = build_call_expr (gfor_fndecl_os_error, 1, msg);
619 if (status != NULL_TREE && !integer_zerop (status))
621 /* Set the status variable if it's present. */
624 cond = fold_build2 (EQ_EXPR, boolean_type_node, status,
625 build_int_cst (status_type, 0));
626 tmp2 = fold_build2 (MODIFY_EXPR, status_type,
627 fold_build1 (INDIRECT_REF, status_type, status),
628 build_int_cst (status_type, LIBERROR_ALLOCATION));
629 tmp = fold_build3 (COND_EXPR, void_type_node, cond, tmp,
633 tmp = fold_build3 (COND_EXPR, void_type_node,
634 fold_build2 (EQ_EXPR, boolean_type_node, res,
635 build_int_cst (pvoid_type_node, 0)),
636 tmp, build_empty_stmt ());
637 gfc_add_expr_to_block (&alloc_block, tmp);
639 cond = fold_build2 (LT_EXPR, boolean_type_node, size,
640 build_int_cst (TREE_TYPE (size), 0));
641 tmp = fold_build3 (COND_EXPR, void_type_node, cond, error,
642 gfc_finish_block (&alloc_block));
643 gfc_add_expr_to_block (block, tmp);
649 /* Generate code for an ALLOCATE statement when the argument is an
650 allocatable array. If the array is currently allocated, it is an
651 error to allocate it again.
653 This function follows the following pseudo-code:
656 allocate_array (void *mem, size_t size, integer_type *stat)
659 return allocate (size, stat);
665 mem = allocate (size, stat);
666 *stat = LIBERROR_ALLOCATION;
670 runtime_error ("Attempting to allocate already allocated array");
673 gfc_allocate_array_with_status (stmtblock_t * block, tree mem, tree size,
676 stmtblock_t alloc_block;
677 tree res, tmp, null_mem, alloc, error, msg;
678 tree type = TREE_TYPE (mem);
680 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
681 size = fold_convert (size_type_node, size);
683 /* Create a variable to hold the result. */
684 res = gfc_create_var (pvoid_type_node, NULL);
685 null_mem = fold_build2 (EQ_EXPR, boolean_type_node, mem,
686 build_int_cst (type, 0));
688 /* If mem is NULL, we call gfc_allocate_with_status. */
689 gfc_start_block (&alloc_block);
690 tmp = gfc_allocate_with_status (&alloc_block, size, status);
691 gfc_add_modify (&alloc_block, res, fold_convert (type, tmp));
692 alloc = gfc_finish_block (&alloc_block);
694 /* Otherwise, we issue a runtime error or set the status variable. */
695 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
696 ("Attempting to allocate already allocated array"));
697 error = build_call_expr (gfor_fndecl_runtime_error, 1, msg);
699 if (status != NULL_TREE && !integer_zerop (status))
701 tree status_type = TREE_TYPE (TREE_TYPE (status));
702 stmtblock_t set_status_block;
704 gfc_start_block (&set_status_block);
705 tmp = build_call_expr (built_in_decls[BUILT_IN_FREE], 1,
706 fold_convert (pvoid_type_node, mem));
707 gfc_add_expr_to_block (&set_status_block, tmp);
709 tmp = gfc_allocate_with_status (&set_status_block, size, status);
710 gfc_add_modify (&set_status_block, res, fold_convert (type, tmp));
712 gfc_add_modify (&set_status_block,
713 fold_build1 (INDIRECT_REF, status_type, status),
714 build_int_cst (status_type, LIBERROR_ALLOCATION));
716 tmp = fold_build2 (EQ_EXPR, boolean_type_node, status,
717 build_int_cst (status_type, 0));
718 error = fold_build3 (COND_EXPR, void_type_node, tmp, error,
719 gfc_finish_block (&set_status_block));
722 tmp = fold_build3 (COND_EXPR, void_type_node, null_mem, alloc, error);
723 gfc_add_expr_to_block (block, tmp);
729 /* Free a given variable, if it's not NULL. */
731 gfc_call_free (tree var)
734 tree tmp, cond, call;
736 if (TREE_TYPE (var) != TREE_TYPE (pvoid_type_node))
737 var = fold_convert (pvoid_type_node, var);
739 gfc_start_block (&block);
740 var = gfc_evaluate_now (var, &block);
741 cond = fold_build2 (NE_EXPR, boolean_type_node, var,
742 build_int_cst (pvoid_type_node, 0));
743 call = build_call_expr (built_in_decls[BUILT_IN_FREE], 1, var);
744 tmp = fold_build3 (COND_EXPR, void_type_node, cond, call,
745 build_empty_stmt ());
746 gfc_add_expr_to_block (&block, tmp);
748 return gfc_finish_block (&block);
753 /* User-deallocate; we emit the code directly from the front-end, and the
754 logic is the same as the previous library function:
757 deallocate (void *pointer, GFC_INTEGER_4 * stat)
764 runtime_error ("Attempt to DEALLOCATE unallocated memory.");
774 In this front-end version, status doesn't have to be GFC_INTEGER_4.
775 Moreover, if CAN_FAIL is true, then we will not emit a runtime error,
776 even when no status variable is passed to us (this is used for
777 unconditional deallocation generated by the front-end at end of
780 gfc_deallocate_with_status (tree pointer, tree status, bool can_fail)
782 stmtblock_t null, non_null;
783 tree cond, tmp, error, msg;
785 cond = fold_build2 (EQ_EXPR, boolean_type_node, pointer,
786 build_int_cst (TREE_TYPE (pointer), 0));
788 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
789 we emit a runtime error. */
790 gfc_start_block (&null);
793 msg = gfc_build_addr_expr (pchar_type_node,
794 gfc_build_localized_cstring_const
795 ("Attempt to DEALLOCATE unallocated memory."));
796 error = build_call_expr (gfor_fndecl_runtime_error, 1, msg);
799 error = build_empty_stmt ();
801 if (status != NULL_TREE && !integer_zerop (status))
803 tree status_type = TREE_TYPE (TREE_TYPE (status));
806 cond2 = fold_build2 (NE_EXPR, boolean_type_node, status,
807 build_int_cst (TREE_TYPE (status), 0));
808 tmp = fold_build2 (MODIFY_EXPR, status_type,
809 fold_build1 (INDIRECT_REF, status_type, status),
810 build_int_cst (status_type, 1));
811 error = fold_build3 (COND_EXPR, void_type_node, cond2, tmp, error);
814 gfc_add_expr_to_block (&null, error);
816 /* When POINTER is not NULL, we free it. */
817 gfc_start_block (&non_null);
818 tmp = build_call_expr (built_in_decls[BUILT_IN_FREE], 1,
819 fold_convert (pvoid_type_node, pointer));
820 gfc_add_expr_to_block (&non_null, tmp);
822 if (status != NULL_TREE && !integer_zerop (status))
824 /* We set STATUS to zero if it is present. */
825 tree status_type = TREE_TYPE (TREE_TYPE (status));
828 cond2 = fold_build2 (NE_EXPR, boolean_type_node, status,
829 build_int_cst (TREE_TYPE (status), 0));
830 tmp = fold_build2 (MODIFY_EXPR, status_type,
831 fold_build1 (INDIRECT_REF, status_type, status),
832 build_int_cst (status_type, 0));
833 tmp = fold_build3 (COND_EXPR, void_type_node, cond2, tmp,
834 build_empty_stmt ());
835 gfc_add_expr_to_block (&non_null, tmp);
838 return fold_build3 (COND_EXPR, void_type_node, cond,
839 gfc_finish_block (&null), gfc_finish_block (&non_null));
843 /* Reallocate MEM so it has SIZE bytes of data. This behaves like the
844 following pseudo-code:
847 internal_realloc (void *mem, size_t size)
850 runtime_error ("Attempt to allocate a negative amount of memory.");
851 res = realloc (mem, size);
852 if (!res && size != 0)
853 _gfortran_os_error ("Out of memory");
861 gfc_call_realloc (stmtblock_t * block, tree mem, tree size)
863 tree msg, res, negative, nonzero, zero, null_result, tmp;
864 tree type = TREE_TYPE (mem);
866 size = gfc_evaluate_now (size, block);
868 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
869 size = fold_convert (size_type_node, size);
871 /* Create a variable to hold the result. */
872 res = gfc_create_var (type, NULL);
875 negative = fold_build2 (LT_EXPR, boolean_type_node, size,
876 build_int_cst (size_type_node, 0));
877 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
878 ("Attempt to allocate a negative amount of memory."));
879 tmp = fold_build3 (COND_EXPR, void_type_node, negative,
880 build_call_expr (gfor_fndecl_runtime_error, 1, msg),
881 build_empty_stmt ());
882 gfc_add_expr_to_block (block, tmp);
884 /* Call realloc and check the result. */
885 tmp = build_call_expr (built_in_decls[BUILT_IN_REALLOC], 2,
886 fold_convert (pvoid_type_node, mem), size);
887 gfc_add_modify (block, res, fold_convert (type, tmp));
888 null_result = fold_build2 (EQ_EXPR, boolean_type_node, res,
889 build_int_cst (pvoid_type_node, 0));
890 nonzero = fold_build2 (NE_EXPR, boolean_type_node, size,
891 build_int_cst (size_type_node, 0));
892 null_result = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, null_result,
894 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
896 tmp = fold_build3 (COND_EXPR, void_type_node, null_result,
897 build_call_expr (gfor_fndecl_os_error, 1, msg),
898 build_empty_stmt ());
899 gfc_add_expr_to_block (block, tmp);
901 /* if (size == 0) then the result is NULL. */
902 tmp = fold_build2 (MODIFY_EXPR, type, res, build_int_cst (type, 0));
903 zero = fold_build1 (TRUTH_NOT_EXPR, boolean_type_node, nonzero);
904 tmp = fold_build3 (COND_EXPR, void_type_node, zero, tmp,
905 build_empty_stmt ());
906 gfc_add_expr_to_block (block, tmp);
911 /* Add a statement to a block. */
914 gfc_add_expr_to_block (stmtblock_t * block, tree expr)
918 if (expr == NULL_TREE || IS_EMPTY_STMT (expr))
923 if (TREE_CODE (block->head) != STATEMENT_LIST)
928 block->head = NULL_TREE;
929 append_to_statement_list (tmp, &block->head);
931 append_to_statement_list (expr, &block->head);
934 /* Don't bother creating a list if we only have a single statement. */
939 /* Add a block the end of a block. */
942 gfc_add_block_to_block (stmtblock_t * block, stmtblock_t * append)
945 gcc_assert (!append->has_scope);
947 gfc_add_expr_to_block (block, append->head);
948 append->head = NULL_TREE;
952 /* Get the current locus. The structure may not be complete, and should
953 only be used with gfc_set_backend_locus. */
956 gfc_get_backend_locus (locus * loc)
958 loc->lb = XCNEW (gfc_linebuf);
959 loc->lb->location = input_location;
960 loc->lb->file = gfc_current_backend_file;
964 /* Set the current locus. */
967 gfc_set_backend_locus (locus * loc)
969 gfc_current_backend_file = loc->lb->file;
970 input_location = loc->lb->location;
974 /* Translate an executable statement. */
977 gfc_trans_code (gfc_code * code)
983 return build_empty_stmt ();
985 gfc_start_block (&block);
987 /* Translate statements one by one into GENERIC trees until we reach
988 the end of this gfc_code branch. */
989 for (; code; code = code->next)
993 res = gfc_trans_label_here (code);
994 gfc_add_expr_to_block (&block, res);
1004 res = gfc_trans_assign (code);
1007 case EXEC_LABEL_ASSIGN:
1008 res = gfc_trans_label_assign (code);
1011 case EXEC_POINTER_ASSIGN:
1012 res = gfc_trans_pointer_assign (code);
1015 case EXEC_INIT_ASSIGN:
1016 res = gfc_trans_init_assign (code);
1024 res = gfc_trans_cycle (code);
1028 res = gfc_trans_exit (code);
1032 res = gfc_trans_goto (code);
1036 res = gfc_trans_entry (code);
1040 res = gfc_trans_pause (code);
1044 res = gfc_trans_stop (code);
1048 res = gfc_trans_call (code, false);
1051 case EXEC_ASSIGN_CALL:
1052 res = gfc_trans_call (code, true);
1056 res = gfc_trans_return (code);
1060 res = gfc_trans_if (code);
1063 case EXEC_ARITHMETIC_IF:
1064 res = gfc_trans_arithmetic_if (code);
1068 res = gfc_trans_do (code);
1072 res = gfc_trans_do_while (code);
1076 res = gfc_trans_select (code);
1080 res = gfc_trans_flush (code);
1084 res = gfc_trans_forall (code);
1088 res = gfc_trans_where (code);
1092 res = gfc_trans_allocate (code);
1095 case EXEC_DEALLOCATE:
1096 res = gfc_trans_deallocate (code);
1100 res = gfc_trans_open (code);
1104 res = gfc_trans_close (code);
1108 res = gfc_trans_read (code);
1112 res = gfc_trans_write (code);
1116 res = gfc_trans_iolength (code);
1119 case EXEC_BACKSPACE:
1120 res = gfc_trans_backspace (code);
1124 res = gfc_trans_endfile (code);
1128 res = gfc_trans_inquire (code);
1132 res = gfc_trans_wait (code);
1136 res = gfc_trans_rewind (code);
1140 res = gfc_trans_transfer (code);
1144 res = gfc_trans_dt_end (code);
1147 case EXEC_OMP_ATOMIC:
1148 case EXEC_OMP_BARRIER:
1149 case EXEC_OMP_CRITICAL:
1151 case EXEC_OMP_FLUSH:
1152 case EXEC_OMP_MASTER:
1153 case EXEC_OMP_ORDERED:
1154 case EXEC_OMP_PARALLEL:
1155 case EXEC_OMP_PARALLEL_DO:
1156 case EXEC_OMP_PARALLEL_SECTIONS:
1157 case EXEC_OMP_PARALLEL_WORKSHARE:
1158 case EXEC_OMP_SECTIONS:
1159 case EXEC_OMP_SINGLE:
1161 case EXEC_OMP_TASKWAIT:
1162 case EXEC_OMP_WORKSHARE:
1163 res = gfc_trans_omp_directive (code);
1167 internal_error ("gfc_trans_code(): Bad statement code");
1170 gfc_set_backend_locus (&code->loc);
1172 if (res != NULL_TREE && ! IS_EMPTY_STMT (res))
1174 if (TREE_CODE (res) == STATEMENT_LIST)
1175 tree_annotate_all_with_location (&res, input_location);
1177 SET_EXPR_LOCATION (res, input_location);
1179 /* Add the new statement to the block. */
1180 gfc_add_expr_to_block (&block, res);
1184 /* Return the finished block. */
1185 return gfc_finish_block (&block);
1189 /* This function is called after a complete program unit has been parsed
1193 gfc_generate_code (gfc_namespace * ns)
1195 if (ns->is_block_data)
1197 gfc_generate_block_data (ns);
1201 gfc_generate_function_code (ns);
1205 /* This function is called after a complete module has been parsed
1209 gfc_generate_module_code (gfc_namespace * ns)
1212 struct module_htab_entry *entry;
1214 gcc_assert (ns->proc_name->backend_decl == NULL);
1215 ns->proc_name->backend_decl
1216 = build_decl (NAMESPACE_DECL, get_identifier (ns->proc_name->name),
1218 gfc_set_decl_location (ns->proc_name->backend_decl,
1219 &ns->proc_name->declared_at);
1220 entry = gfc_find_module (ns->proc_name->name);
1221 if (entry->namespace_decl)
1222 /* Buggy sourcecode, using a module before defining it? */
1223 htab_empty (entry->decls);
1224 entry->namespace_decl = ns->proc_name->backend_decl;
1226 gfc_generate_module_vars (ns);
1228 /* We need to generate all module function prototypes first, to allow
1230 for (n = ns->contained; n; n = n->sibling)
1237 gfc_create_function_decl (n);
1238 gcc_assert (DECL_CONTEXT (n->proc_name->backend_decl) == NULL_TREE);
1239 DECL_CONTEXT (n->proc_name->backend_decl) = ns->proc_name->backend_decl;
1240 gfc_module_add_decl (entry, n->proc_name->backend_decl);
1241 for (el = ns->entries; el; el = el->next)
1243 gcc_assert (DECL_CONTEXT (el->sym->backend_decl) == NULL_TREE);
1244 DECL_CONTEXT (el->sym->backend_decl) = ns->proc_name->backend_decl;
1245 gfc_module_add_decl (entry, el->sym->backend_decl);
1249 for (n = ns->contained; n; n = n->sibling)
1254 gfc_generate_function_code (n);