2011-06-14 Zdenek Dvorak <ook@ucw.cz>
[official-gcc.git] / gcc / fortran / trans.c
blobee35387a7d9dd62f13d89f27882de31b7c3cdbd5
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
11 version.
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
16 for more details.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tree.h"
26 #include "gimple.h" /* For create_tmp_var_raw. */
27 #include "tree-iterator.h"
28 #include "diagnostic-core.h" /* For internal_error. */
29 #include "defaults.h"
30 #include "flags.h"
31 #include "gfortran.h"
32 #include "trans.h"
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. */
54 tree
55 gfc_advance_chain (tree t, int n)
57 for (; n > 0; n--)
59 gcc_assert (t != NULL_TREE);
60 t = DECL_CHAIN (t);
62 return t;
66 /* Strip off a legitimate source ending from the input
67 string NAME of length LEN. */
69 static inline void
70 remove_suffix (char *name, int len)
72 int i;
74 for (i = 2; i < 8 && len > i; i++)
76 if (name[len - i] == '.')
78 name[len - i] = '\0';
79 break;
85 /* Creates a variable declaration with a given TYPE. */
87 tree
88 gfc_create_var_np (tree type, const char *prefix)
90 tree t;
92 t = create_tmp_var_raw (type, prefix);
94 /* No warnings for anonymous variables. */
95 if (prefix == NULL)
96 TREE_NO_WARNING (t) = 1;
98 return t;
102 /* Like above, but also adds it to the current scope. */
104 tree
105 gfc_create_var (tree type, const char *prefix)
107 tree tmp;
109 tmp = gfc_create_var_np (type, prefix);
111 pushdecl (tmp);
113 return tmp;
117 /* If the expression is not constant, evaluate it now. We assign the
118 result of the expression to an artificially created variable VAR, and
119 return a pointer to the VAR_DECL node for this variable. */
121 tree
122 gfc_evaluate_now_loc (location_t loc, tree expr, stmtblock_t * pblock)
124 tree var;
126 if (CONSTANT_CLASS_P (expr))
127 return expr;
129 var = gfc_create_var (TREE_TYPE (expr), NULL);
130 gfc_add_modify_loc (loc, pblock, var, expr);
132 return var;
136 tree
137 gfc_evaluate_now (tree expr, stmtblock_t * pblock)
139 return gfc_evaluate_now_loc (input_location, expr, pblock);
143 /* Build a MODIFY_EXPR node and add it to a given statement block PBLOCK.
144 A MODIFY_EXPR is an assignment:
145 LHS <- RHS. */
147 void
148 gfc_add_modify_loc (location_t loc, stmtblock_t * pblock, tree lhs, tree rhs)
150 tree tmp;
152 #ifdef ENABLE_CHECKING
153 tree t1, t2;
154 t1 = TREE_TYPE (rhs);
155 t2 = TREE_TYPE (lhs);
156 /* Make sure that the types of the rhs and the lhs are the same
157 for scalar assignments. We should probably have something
158 similar for aggregates, but right now removing that check just
159 breaks everything. */
160 gcc_assert (t1 == t2
161 || AGGREGATE_TYPE_P (TREE_TYPE (lhs)));
162 #endif
164 tmp = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, lhs,
165 rhs);
166 gfc_add_expr_to_block (pblock, tmp);
170 void
171 gfc_add_modify (stmtblock_t * pblock, tree lhs, tree rhs)
173 gfc_add_modify_loc (input_location, pblock, lhs, rhs);
177 /* Create a new scope/binding level and initialize a block. Care must be
178 taken when translating expressions as any temporaries will be placed in
179 the innermost scope. */
181 void
182 gfc_start_block (stmtblock_t * block)
184 /* Start a new binding level. */
185 pushlevel (0);
186 block->has_scope = 1;
188 /* The block is empty. */
189 block->head = NULL_TREE;
193 /* Initialize a block without creating a new scope. */
195 void
196 gfc_init_block (stmtblock_t * block)
198 block->head = NULL_TREE;
199 block->has_scope = 0;
203 /* Sometimes we create a scope but it turns out that we don't actually
204 need it. This function merges the scope of BLOCK with its parent.
205 Only variable decls will be merged, you still need to add the code. */
207 void
208 gfc_merge_block_scope (stmtblock_t * block)
210 tree decl;
211 tree next;
213 gcc_assert (block->has_scope);
214 block->has_scope = 0;
216 /* Remember the decls in this scope. */
217 decl = getdecls ();
218 poplevel (0, 0, 0);
220 /* Add them to the parent scope. */
221 while (decl != NULL_TREE)
223 next = DECL_CHAIN (decl);
224 DECL_CHAIN (decl) = NULL_TREE;
226 pushdecl (decl);
227 decl = next;
232 /* Finish a scope containing a block of statements. */
234 tree
235 gfc_finish_block (stmtblock_t * stmtblock)
237 tree decl;
238 tree expr;
239 tree block;
241 expr = stmtblock->head;
242 if (!expr)
243 expr = build_empty_stmt (input_location);
245 stmtblock->head = NULL_TREE;
247 if (stmtblock->has_scope)
249 decl = getdecls ();
251 if (decl)
253 block = poplevel (1, 0, 0);
254 expr = build3_v (BIND_EXPR, decl, expr, block);
256 else
257 poplevel (0, 0, 0);
260 return expr;
264 /* Build an ADDR_EXPR and cast the result to TYPE. If TYPE is NULL, the
265 natural type is used. */
267 tree
268 gfc_build_addr_expr (tree type, tree t)
270 tree base_type = TREE_TYPE (t);
271 tree natural_type;
273 if (type && POINTER_TYPE_P (type)
274 && TREE_CODE (base_type) == ARRAY_TYPE
275 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
276 == TYPE_MAIN_VARIANT (TREE_TYPE (base_type)))
278 tree min_val = size_zero_node;
279 tree type_domain = TYPE_DOMAIN (base_type);
280 if (type_domain && TYPE_MIN_VALUE (type_domain))
281 min_val = TYPE_MIN_VALUE (type_domain);
282 t = fold (build4_loc (input_location, ARRAY_REF, TREE_TYPE (type),
283 t, min_val, NULL_TREE, NULL_TREE));
284 natural_type = type;
286 else
287 natural_type = build_pointer_type (base_type);
289 if (TREE_CODE (t) == INDIRECT_REF)
291 if (!type)
292 type = natural_type;
293 t = TREE_OPERAND (t, 0);
294 natural_type = TREE_TYPE (t);
296 else
298 tree base = get_base_address (t);
299 if (base && DECL_P (base))
300 TREE_ADDRESSABLE (base) = 1;
301 t = fold_build1_loc (input_location, ADDR_EXPR, natural_type, t);
304 if (type && natural_type != type)
305 t = convert (type, t);
307 return t;
311 /* Build an ARRAY_REF with its natural type. */
313 tree
314 gfc_build_array_ref (tree base, tree offset, tree decl)
316 tree type = TREE_TYPE (base);
317 tree tmp;
319 if (GFC_ARRAY_TYPE_P (type) && GFC_TYPE_ARRAY_RANK (type) == 0)
321 gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
323 return fold_convert (TYPE_MAIN_VARIANT (type), base);
326 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
327 type = TREE_TYPE (type);
329 if (DECL_P (base))
330 TREE_ADDRESSABLE (base) = 1;
332 /* Strip NON_LVALUE_EXPR nodes. */
333 STRIP_TYPE_NOPS (offset);
335 /* If the array reference is to a pointer, whose target contains a
336 subreference, use the span that is stored with the backend decl
337 and reference the element with pointer arithmetic. */
338 if (decl && (TREE_CODE (decl) == FIELD_DECL
339 || TREE_CODE (decl) == VAR_DECL
340 || TREE_CODE (decl) == PARM_DECL)
341 && GFC_DECL_SUBREF_ARRAY_P (decl)
342 && !integer_zerop (GFC_DECL_SPAN(decl)))
344 offset = fold_build2_loc (input_location, MULT_EXPR,
345 gfc_array_index_type,
346 offset, GFC_DECL_SPAN(decl));
347 tmp = gfc_build_addr_expr (pvoid_type_node, base);
348 tmp = fold_build2_loc (input_location, POINTER_PLUS_EXPR,
349 pvoid_type_node, tmp,
350 fold_convert (sizetype, offset));
351 tmp = fold_convert (build_pointer_type (type), tmp);
352 if (!TYPE_STRING_FLAG (type))
353 tmp = build_fold_indirect_ref_loc (input_location, tmp);
354 return tmp;
356 else
357 /* Otherwise use a straightforward array reference. */
358 return build4_loc (input_location, ARRAY_REF, type, base, offset,
359 NULL_TREE, NULL_TREE);
363 /* Generate a call to print a runtime error possibly including multiple
364 arguments and a locus. */
366 static tree
367 trans_runtime_error_vararg (bool error, locus* where, const char* msgid,
368 va_list ap)
370 stmtblock_t block;
371 tree tmp;
372 tree arg, arg2;
373 tree *argarray;
374 tree fntype;
375 char *message;
376 const char *p;
377 int line, nargs, i;
378 location_t loc;
380 /* Compute the number of extra arguments from the format string. */
381 for (p = msgid, nargs = 0; *p; p++)
382 if (*p == '%')
384 p++;
385 if (*p != '%')
386 nargs++;
389 /* The code to generate the error. */
390 gfc_start_block (&block);
392 if (where)
394 line = LOCATION_LINE (where->lb->location);
395 asprintf (&message, "At line %d of file %s", line,
396 where->lb->file->filename);
398 else
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));
404 free (message);
406 asprintf (&message, "%s", _(msgid));
407 arg2 = gfc_build_addr_expr (pchar_type_node,
408 gfc_build_localized_cstring_const (message));
409 free (message);
411 /* Build the argument array. */
412 argarray = XALLOCAVEC (tree, nargs + 2);
413 argarray[0] = arg;
414 argarray[1] = arg2;
415 for (i = 0; i < nargs; i++)
416 argarray[2 + i] = va_arg (ap, tree);
418 /* Build the function call to runtime_(warning,error)_at; because of the
419 variable number of arguments, we can't use build_call_expr_loc dinput_location,
420 irectly. */
421 if (error)
422 fntype = TREE_TYPE (gfor_fndecl_runtime_error_at);
423 else
424 fntype = TREE_TYPE (gfor_fndecl_runtime_warning_at);
426 loc = where ? where->lb->location : input_location;
427 tmp = fold_builtin_call_array (loc, TREE_TYPE (fntype),
428 fold_build1_loc (loc, ADDR_EXPR,
429 build_pointer_type (fntype),
430 error
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 tree
441 gfc_trans_runtime_error (bool error, locus* where, const char* msgid, ...)
443 va_list ap;
444 tree result;
446 va_start (ap, msgid);
447 result = trans_runtime_error_vararg (error, where, msgid, ap);
448 va_end (ap);
449 return result;
453 /* Generate a runtime error if COND is true. */
455 void
456 gfc_trans_runtime_check (bool error, bool once, tree cond, stmtblock_t * pblock,
457 locus * where, const char * msgid, ...)
459 va_list ap;
460 stmtblock_t block;
461 tree body;
462 tree tmp;
463 tree tmpvar = NULL;
465 if (integer_zerop (cond))
466 return;
468 if (once)
470 tmpvar = gfc_create_var (boolean_type_node, "print_warning");
471 TREE_STATIC (tmpvar) = 1;
472 DECL_INITIAL (tmpvar) = boolean_true_node;
473 gfc_add_expr_to_block (pblock, tmpvar);
476 gfc_start_block (&block);
478 /* The code to generate the error. */
479 va_start (ap, msgid);
480 gfc_add_expr_to_block (&block,
481 trans_runtime_error_vararg (error, where,
482 msgid, ap));
484 if (once)
485 gfc_add_modify (&block, tmpvar, boolean_false_node);
487 body = gfc_finish_block (&block);
489 if (integer_onep (cond))
491 gfc_add_expr_to_block (pblock, body);
493 else
495 /* Tell the compiler that this isn't likely. */
496 if (once)
497 cond = fold_build2_loc (where->lb->location, TRUTH_AND_EXPR,
498 long_integer_type_node, tmpvar, cond);
499 else
500 cond = fold_convert (long_integer_type_node, cond);
502 cond = gfc_unlikely (cond);
503 tmp = fold_build3_loc (where->lb->location, COND_EXPR, void_type_node,
504 cond, body,
505 build_empty_stmt (where->lb->location));
506 gfc_add_expr_to_block (pblock, tmp);
511 /* Call malloc to allocate size bytes of memory, with special conditions:
512 + if size == 0, return a malloced area of size 1,
513 + if malloc returns NULL, issue a runtime error. */
514 tree
515 gfc_call_malloc (stmtblock_t * block, tree type, tree size)
517 tree tmp, msg, malloc_result, null_result, res;
518 stmtblock_t block2;
520 size = gfc_evaluate_now (size, block);
522 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
523 size = fold_convert (size_type_node, size);
525 /* Create a variable to hold the result. */
526 res = gfc_create_var (prvoid_type_node, NULL);
528 /* Call malloc. */
529 gfc_start_block (&block2);
531 size = fold_build2_loc (input_location, MAX_EXPR, size_type_node, size,
532 build_int_cst (size_type_node, 1));
534 gfc_add_modify (&block2, res,
535 fold_convert (prvoid_type_node,
536 build_call_expr_loc (input_location,
537 built_in_decls[BUILT_IN_MALLOC], 1, size)));
539 /* Optionally check whether malloc was successful. */
540 if (gfc_option.rtcheck & GFC_RTCHECK_MEM)
542 null_result = fold_build2_loc (input_location, EQ_EXPR,
543 boolean_type_node, res,
544 build_int_cst (pvoid_type_node, 0));
545 msg = gfc_build_addr_expr (pchar_type_node,
546 gfc_build_localized_cstring_const ("Memory allocation failed"));
547 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
548 null_result,
549 build_call_expr_loc (input_location,
550 gfor_fndecl_os_error, 1, msg),
551 build_empty_stmt (input_location));
552 gfc_add_expr_to_block (&block2, tmp);
555 malloc_result = gfc_finish_block (&block2);
557 gfc_add_expr_to_block (block, malloc_result);
559 if (type != NULL)
560 res = fold_convert (type, res);
561 return res;
565 /* Allocate memory, using an optional status argument.
567 This function follows the following pseudo-code:
569 void *
570 allocate (size_t size, integer_type* stat)
572 void *newmem;
574 if (stat)
575 *stat = 0;
577 newmem = malloc (MAX (size, 1));
578 if (newmem == NULL)
580 if (stat)
581 *stat = LIBERROR_ALLOCATION;
582 else
583 runtime_error ("Allocation would exceed memory limit");
585 return newmem;
586 } */
587 tree
588 gfc_allocate_with_status (stmtblock_t * block, tree size, tree status)
590 stmtblock_t alloc_block;
591 tree res, tmp, msg, cond;
592 tree status_type = status ? TREE_TYPE (TREE_TYPE (status)) : NULL_TREE;
594 /* Evaluate size only once, and make sure it has the right type. */
595 size = gfc_evaluate_now (size, block);
596 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
597 size = fold_convert (size_type_node, size);
599 /* Create a variable to hold the result. */
600 res = gfc_create_var (prvoid_type_node, NULL);
602 /* Set the optional status variable to zero. */
603 if (status != NULL_TREE && !integer_zerop (status))
605 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
606 fold_build1_loc (input_location, INDIRECT_REF,
607 status_type, status),
608 build_int_cst (status_type, 0));
609 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
610 fold_build2_loc (input_location, NE_EXPR,
611 boolean_type_node, status,
612 build_int_cst (TREE_TYPE (status), 0)),
613 tmp, build_empty_stmt (input_location));
614 gfc_add_expr_to_block (block, tmp);
617 /* The allocation itself. */
618 gfc_start_block (&alloc_block);
619 gfc_add_modify (&alloc_block, res,
620 fold_convert (prvoid_type_node,
621 build_call_expr_loc (input_location,
622 built_in_decls[BUILT_IN_MALLOC], 1,
623 fold_build2_loc (input_location,
624 MAX_EXPR, size_type_node, size,
625 build_int_cst (size_type_node,
626 1)))));
628 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
629 ("Allocation would exceed memory limit"));
630 tmp = build_call_expr_loc (input_location,
631 gfor_fndecl_os_error, 1, msg);
633 if (status != NULL_TREE && !integer_zerop (status))
635 /* Set the status variable if it's present. */
636 tree tmp2;
638 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
639 status, build_int_cst (TREE_TYPE (status), 0));
640 tmp2 = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
641 fold_build1_loc (input_location, INDIRECT_REF,
642 status_type, status),
643 build_int_cst (status_type, LIBERROR_ALLOCATION));
644 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
645 tmp, tmp2);
648 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
649 fold_build2_loc (input_location, EQ_EXPR,
650 boolean_type_node, res,
651 build_int_cst (prvoid_type_node, 0)),
652 tmp, build_empty_stmt (input_location));
653 gfc_add_expr_to_block (&alloc_block, tmp);
654 gfc_add_expr_to_block (block, gfc_finish_block (&alloc_block));
656 return res;
660 /* Generate code for an ALLOCATE statement when the argument is an
661 allocatable array. If the array is currently allocated, it is an
662 error to allocate it again.
664 This function follows the following pseudo-code:
666 void *
667 allocate_array (void *mem, size_t size, integer_type *stat)
669 if (mem == NULL)
670 return allocate (size, stat);
671 else
673 if (stat)
675 free (mem);
676 mem = allocate (size, stat);
677 *stat = LIBERROR_ALLOCATION;
678 return mem;
680 else
681 runtime_error ("Attempting to allocate already allocated variable");
685 expr must be set to the original expression being allocated for its locus
686 and variable name in case a runtime error has to be printed. */
687 tree
688 gfc_allocate_array_with_status (stmtblock_t * block, tree mem, tree size,
689 tree status, gfc_expr* expr)
691 stmtblock_t alloc_block;
692 tree res, tmp, null_mem, alloc, error;
693 tree type = TREE_TYPE (mem);
695 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
696 size = fold_convert (size_type_node, size);
698 /* Create a variable to hold the result. */
699 res = gfc_create_var (type, NULL);
700 null_mem = gfc_unlikely (fold_build2_loc (input_location, NE_EXPR,
701 boolean_type_node, mem,
702 build_int_cst (type, 0)));
704 /* If mem is NULL, we call gfc_allocate_with_status. */
705 gfc_start_block (&alloc_block);
706 tmp = gfc_allocate_with_status (&alloc_block, size, status);
707 gfc_add_modify (&alloc_block, res, fold_convert (type, tmp));
708 alloc = gfc_finish_block (&alloc_block);
710 /* Otherwise, we issue a runtime error or set the status variable. */
711 if (expr)
713 tree varname;
715 gcc_assert (expr->expr_type == EXPR_VARIABLE && expr->symtree);
716 varname = gfc_build_cstring_const (expr->symtree->name);
717 varname = gfc_build_addr_expr (pchar_type_node, varname);
719 error = gfc_trans_runtime_error (true, &expr->where,
720 "Attempting to allocate already"
721 " allocated variable '%s'",
722 varname);
724 else
725 error = gfc_trans_runtime_error (true, NULL,
726 "Attempting to allocate already allocated"
727 " variable");
729 if (status != NULL_TREE && !integer_zerop (status))
731 tree status_type = TREE_TYPE (TREE_TYPE (status));
732 stmtblock_t set_status_block;
734 gfc_start_block (&set_status_block);
735 tmp = build_call_expr_loc (input_location,
736 built_in_decls[BUILT_IN_FREE], 1,
737 fold_convert (pvoid_type_node, mem));
738 gfc_add_expr_to_block (&set_status_block, tmp);
740 tmp = gfc_allocate_with_status (&set_status_block, size, status);
741 gfc_add_modify (&set_status_block, res, fold_convert (type, tmp));
743 gfc_add_modify (&set_status_block,
744 fold_build1_loc (input_location, INDIRECT_REF,
745 status_type, status),
746 build_int_cst (status_type, LIBERROR_ALLOCATION));
748 tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
749 status, build_int_cst (status_type, 0));
750 error = fold_build3_loc (input_location, COND_EXPR, void_type_node, tmp,
751 error, gfc_finish_block (&set_status_block));
754 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, null_mem,
755 error, alloc);
756 gfc_add_expr_to_block (block, tmp);
758 return res;
762 /* Free a given variable, if it's not NULL. */
763 tree
764 gfc_call_free (tree var)
766 stmtblock_t block;
767 tree tmp, cond, call;
769 if (TREE_TYPE (var) != TREE_TYPE (pvoid_type_node))
770 var = fold_convert (pvoid_type_node, var);
772 gfc_start_block (&block);
773 var = gfc_evaluate_now (var, &block);
774 cond = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, var,
775 build_int_cst (pvoid_type_node, 0));
776 call = build_call_expr_loc (input_location,
777 built_in_decls[BUILT_IN_FREE], 1, var);
778 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, call,
779 build_empty_stmt (input_location));
780 gfc_add_expr_to_block (&block, tmp);
782 return gfc_finish_block (&block);
787 /* User-deallocate; we emit the code directly from the front-end, and the
788 logic is the same as the previous library function:
790 void
791 deallocate (void *pointer, GFC_INTEGER_4 * stat)
793 if (!pointer)
795 if (stat)
796 *stat = 1;
797 else
798 runtime_error ("Attempt to DEALLOCATE unallocated memory.");
800 else
802 free (pointer);
803 if (stat)
804 *stat = 0;
808 In this front-end version, status doesn't have to be GFC_INTEGER_4.
809 Moreover, if CAN_FAIL is true, then we will not emit a runtime error,
810 even when no status variable is passed to us (this is used for
811 unconditional deallocation generated by the front-end at end of
812 each procedure).
814 If a runtime-message is possible, `expr' must point to the original
815 expression being deallocated for its locus and variable name. */
816 tree
817 gfc_deallocate_with_status (tree pointer, tree status, bool can_fail,
818 gfc_expr* expr)
820 stmtblock_t null, non_null;
821 tree cond, tmp, error;
823 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, pointer,
824 build_int_cst (TREE_TYPE (pointer), 0));
826 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
827 we emit a runtime error. */
828 gfc_start_block (&null);
829 if (!can_fail)
831 tree varname;
833 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
835 varname = gfc_build_cstring_const (expr->symtree->name);
836 varname = gfc_build_addr_expr (pchar_type_node, varname);
838 error = gfc_trans_runtime_error (true, &expr->where,
839 "Attempt to DEALLOCATE unallocated '%s'",
840 varname);
842 else
843 error = build_empty_stmt (input_location);
845 if (status != NULL_TREE && !integer_zerop (status))
847 tree status_type = TREE_TYPE (TREE_TYPE (status));
848 tree cond2;
850 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
851 status, build_int_cst (TREE_TYPE (status), 0));
852 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
853 fold_build1_loc (input_location, INDIRECT_REF,
854 status_type, status),
855 build_int_cst (status_type, 1));
856 error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
857 cond2, tmp, error);
860 gfc_add_expr_to_block (&null, error);
862 /* When POINTER is not NULL, we free it. */
863 gfc_start_block (&non_null);
864 tmp = build_call_expr_loc (input_location,
865 built_in_decls[BUILT_IN_FREE], 1,
866 fold_convert (pvoid_type_node, pointer));
867 gfc_add_expr_to_block (&non_null, tmp);
869 if (status != NULL_TREE && !integer_zerop (status))
871 /* We set STATUS to zero if it is present. */
872 tree status_type = TREE_TYPE (TREE_TYPE (status));
873 tree cond2;
875 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
876 status, build_int_cst (TREE_TYPE (status), 0));
877 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
878 fold_build1_loc (input_location, INDIRECT_REF,
879 status_type, status),
880 build_int_cst (status_type, 0));
881 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
882 tmp, build_empty_stmt (input_location));
883 gfc_add_expr_to_block (&non_null, tmp);
886 return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
887 gfc_finish_block (&null),
888 gfc_finish_block (&non_null));
892 /* Generate code for deallocation of allocatable scalars (variables or
893 components). Before the object itself is freed, any allocatable
894 subcomponents are being deallocated. */
896 tree
897 gfc_deallocate_scalar_with_status (tree pointer, tree status, bool can_fail,
898 gfc_expr* expr, gfc_typespec ts)
900 stmtblock_t null, non_null;
901 tree cond, tmp, error;
903 cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, pointer,
904 build_int_cst (TREE_TYPE (pointer), 0));
906 /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
907 we emit a runtime error. */
908 gfc_start_block (&null);
909 if (!can_fail)
911 tree varname;
913 gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
915 varname = gfc_build_cstring_const (expr->symtree->name);
916 varname = gfc_build_addr_expr (pchar_type_node, varname);
918 error = gfc_trans_runtime_error (true, &expr->where,
919 "Attempt to DEALLOCATE unallocated '%s'",
920 varname);
922 else
923 error = build_empty_stmt (input_location);
925 if (status != NULL_TREE && !integer_zerop (status))
927 tree status_type = TREE_TYPE (TREE_TYPE (status));
928 tree cond2;
930 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
931 status, build_int_cst (TREE_TYPE (status), 0));
932 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
933 fold_build1_loc (input_location, INDIRECT_REF,
934 status_type, status),
935 build_int_cst (status_type, 1));
936 error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
937 cond2, tmp, error);
940 gfc_add_expr_to_block (&null, error);
942 /* When POINTER is not NULL, we free it. */
943 gfc_start_block (&non_null);
945 /* Free allocatable components. */
946 if (ts.type == BT_DERIVED && ts.u.derived->attr.alloc_comp)
948 tmp = build_fold_indirect_ref_loc (input_location, pointer);
949 tmp = gfc_deallocate_alloc_comp (ts.u.derived, tmp, 0);
950 gfc_add_expr_to_block (&non_null, tmp);
952 else if (ts.type == BT_CLASS
953 && ts.u.derived->components->ts.u.derived->attr.alloc_comp)
955 tmp = build_fold_indirect_ref_loc (input_location, pointer);
956 tmp = gfc_deallocate_alloc_comp (ts.u.derived->components->ts.u.derived,
957 tmp, 0);
958 gfc_add_expr_to_block (&non_null, tmp);
961 tmp = build_call_expr_loc (input_location,
962 built_in_decls[BUILT_IN_FREE], 1,
963 fold_convert (pvoid_type_node, pointer));
964 gfc_add_expr_to_block (&non_null, tmp);
966 if (status != NULL_TREE && !integer_zerop (status))
968 /* We set STATUS to zero if it is present. */
969 tree status_type = TREE_TYPE (TREE_TYPE (status));
970 tree cond2;
972 cond2 = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
973 status, build_int_cst (TREE_TYPE (status), 0));
974 tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
975 fold_build1_loc (input_location, INDIRECT_REF,
976 status_type, status),
977 build_int_cst (status_type, 0));
978 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
979 tmp, build_empty_stmt (input_location));
980 gfc_add_expr_to_block (&non_null, tmp);
983 return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
984 gfc_finish_block (&null),
985 gfc_finish_block (&non_null));
989 /* Reallocate MEM so it has SIZE bytes of data. This behaves like the
990 following pseudo-code:
992 void *
993 internal_realloc (void *mem, size_t size)
995 res = realloc (mem, size);
996 if (!res && size != 0)
997 _gfortran_os_error ("Allocation would exceed memory limit");
999 if (size == 0)
1000 return NULL;
1002 return res;
1003 } */
1004 tree
1005 gfc_call_realloc (stmtblock_t * block, tree mem, tree size)
1007 tree msg, res, nonzero, zero, null_result, tmp;
1008 tree type = TREE_TYPE (mem);
1010 size = gfc_evaluate_now (size, block);
1012 if (TREE_TYPE (size) != TREE_TYPE (size_type_node))
1013 size = fold_convert (size_type_node, size);
1015 /* Create a variable to hold the result. */
1016 res = gfc_create_var (type, NULL);
1018 /* Call realloc and check the result. */
1019 tmp = build_call_expr_loc (input_location,
1020 built_in_decls[BUILT_IN_REALLOC], 2,
1021 fold_convert (pvoid_type_node, mem), size);
1022 gfc_add_modify (block, res, fold_convert (type, tmp));
1023 null_result = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
1024 res, build_int_cst (pvoid_type_node, 0));
1025 nonzero = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, size,
1026 build_int_cst (size_type_node, 0));
1027 null_result = fold_build2_loc (input_location, TRUTH_AND_EXPR, boolean_type_node,
1028 null_result, nonzero);
1029 msg = gfc_build_addr_expr (pchar_type_node, gfc_build_localized_cstring_const
1030 ("Allocation would exceed memory limit"));
1031 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
1032 null_result,
1033 build_call_expr_loc (input_location,
1034 gfor_fndecl_os_error, 1, msg),
1035 build_empty_stmt (input_location));
1036 gfc_add_expr_to_block (block, tmp);
1038 /* if (size == 0) then the result is NULL. */
1039 tmp = fold_build2_loc (input_location, MODIFY_EXPR, type, res,
1040 build_int_cst (type, 0));
1041 zero = fold_build1_loc (input_location, TRUTH_NOT_EXPR, boolean_type_node,
1042 nonzero);
1043 tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, zero, tmp,
1044 build_empty_stmt (input_location));
1045 gfc_add_expr_to_block (block, tmp);
1047 return res;
1051 /* Add an expression to another one, either at the front or the back. */
1053 static void
1054 add_expr_to_chain (tree* chain, tree expr, bool front)
1056 if (expr == NULL_TREE || IS_EMPTY_STMT (expr))
1057 return;
1059 if (*chain)
1061 if (TREE_CODE (*chain) != STATEMENT_LIST)
1063 tree tmp;
1065 tmp = *chain;
1066 *chain = NULL_TREE;
1067 append_to_statement_list (tmp, chain);
1070 if (front)
1072 tree_stmt_iterator i;
1074 i = tsi_start (*chain);
1075 tsi_link_before (&i, expr, TSI_CONTINUE_LINKING);
1077 else
1078 append_to_statement_list (expr, chain);
1080 else
1081 *chain = expr;
1085 /* Add a statement at the end of a block. */
1087 void
1088 gfc_add_expr_to_block (stmtblock_t * block, tree expr)
1090 gcc_assert (block);
1091 add_expr_to_chain (&block->head, expr, false);
1095 /* Add a statement at the beginning of a block. */
1097 void
1098 gfc_prepend_expr_to_block (stmtblock_t * block, tree expr)
1100 gcc_assert (block);
1101 add_expr_to_chain (&block->head, expr, true);
1105 /* Add a block the end of a block. */
1107 void
1108 gfc_add_block_to_block (stmtblock_t * block, stmtblock_t * append)
1110 gcc_assert (append);
1111 gcc_assert (!append->has_scope);
1113 gfc_add_expr_to_block (block, append->head);
1114 append->head = NULL_TREE;
1118 /* Save the current locus. The structure may not be complete, and should
1119 only be used with gfc_restore_backend_locus. */
1121 void
1122 gfc_save_backend_locus (locus * loc)
1124 loc->lb = XCNEW (gfc_linebuf);
1125 loc->lb->location = input_location;
1126 loc->lb->file = gfc_current_backend_file;
1130 /* Set the current locus. */
1132 void
1133 gfc_set_backend_locus (locus * loc)
1135 gfc_current_backend_file = loc->lb->file;
1136 input_location = loc->lb->location;
1140 /* Restore the saved locus. Only used in conjonction with
1141 gfc_save_backend_locus, to free the memory when we are done. */
1143 void
1144 gfc_restore_backend_locus (locus * loc)
1146 gfc_set_backend_locus (loc);
1147 free (loc->lb);
1151 /* Translate an executable statement. The tree cond is used by gfc_trans_do.
1152 This static function is wrapped by gfc_trans_code_cond and
1153 gfc_trans_code. */
1155 static tree
1156 trans_code (gfc_code * code, tree cond)
1158 stmtblock_t block;
1159 tree res;
1161 if (!code)
1162 return build_empty_stmt (input_location);
1164 gfc_start_block (&block);
1166 /* Translate statements one by one into GENERIC trees until we reach
1167 the end of this gfc_code branch. */
1168 for (; code; code = code->next)
1170 if (code->here != 0)
1172 res = gfc_trans_label_here (code);
1173 gfc_add_expr_to_block (&block, res);
1176 gfc_set_backend_locus (&code->loc);
1178 switch (code->op)
1180 case EXEC_NOP:
1181 case EXEC_END_BLOCK:
1182 case EXEC_END_PROCEDURE:
1183 res = NULL_TREE;
1184 break;
1186 case EXEC_ASSIGN:
1187 if (code->expr1->ts.type == BT_CLASS)
1188 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1189 else
1190 res = gfc_trans_assign (code);
1191 break;
1193 case EXEC_LABEL_ASSIGN:
1194 res = gfc_trans_label_assign (code);
1195 break;
1197 case EXEC_POINTER_ASSIGN:
1198 if (code->expr1->ts.type == BT_CLASS)
1199 res = gfc_trans_class_assign (code->expr1, code->expr2, code->op);
1200 else
1201 res = gfc_trans_pointer_assign (code);
1202 break;
1204 case EXEC_INIT_ASSIGN:
1205 if (code->expr1->ts.type == BT_CLASS)
1206 res = gfc_trans_class_init_assign (code);
1207 else
1208 res = gfc_trans_init_assign (code);
1209 break;
1211 case EXEC_CONTINUE:
1212 res = NULL_TREE;
1213 break;
1215 case EXEC_CRITICAL:
1216 res = gfc_trans_critical (code);
1217 break;
1219 case EXEC_CYCLE:
1220 res = gfc_trans_cycle (code);
1221 break;
1223 case EXEC_EXIT:
1224 res = gfc_trans_exit (code);
1225 break;
1227 case EXEC_GOTO:
1228 res = gfc_trans_goto (code);
1229 break;
1231 case EXEC_ENTRY:
1232 res = gfc_trans_entry (code);
1233 break;
1235 case EXEC_PAUSE:
1236 res = gfc_trans_pause (code);
1237 break;
1239 case EXEC_STOP:
1240 case EXEC_ERROR_STOP:
1241 res = gfc_trans_stop (code, code->op == EXEC_ERROR_STOP);
1242 break;
1244 case EXEC_CALL:
1245 /* For MVBITS we've got the special exception that we need a
1246 dependency check, too. */
1248 bool is_mvbits = false;
1250 if (code->resolved_isym)
1252 res = gfc_conv_intrinsic_subroutine (code);
1253 if (res != NULL_TREE)
1254 break;
1257 if (code->resolved_isym
1258 && code->resolved_isym->id == GFC_ISYM_MVBITS)
1259 is_mvbits = true;
1261 res = gfc_trans_call (code, is_mvbits, NULL_TREE,
1262 NULL_TREE, false);
1264 break;
1266 case EXEC_CALL_PPC:
1267 res = gfc_trans_call (code, false, NULL_TREE,
1268 NULL_TREE, false);
1269 break;
1271 case EXEC_ASSIGN_CALL:
1272 res = gfc_trans_call (code, true, NULL_TREE,
1273 NULL_TREE, false);
1274 break;
1276 case EXEC_RETURN:
1277 res = gfc_trans_return (code);
1278 break;
1280 case EXEC_IF:
1281 res = gfc_trans_if (code);
1282 break;
1284 case EXEC_ARITHMETIC_IF:
1285 res = gfc_trans_arithmetic_if (code);
1286 break;
1288 case EXEC_BLOCK:
1289 res = gfc_trans_block_construct (code);
1290 break;
1292 case EXEC_DO:
1293 res = gfc_trans_do (code, cond);
1294 break;
1296 case EXEC_DO_WHILE:
1297 res = gfc_trans_do_while (code);
1298 break;
1300 case EXEC_SELECT:
1301 res = gfc_trans_select (code);
1302 break;
1304 case EXEC_SELECT_TYPE:
1305 /* Do nothing. SELECT TYPE statements should be transformed into
1306 an ordinary SELECT CASE at resolution stage.
1307 TODO: Add an error message here once this is done. */
1308 res = NULL_TREE;
1309 break;
1311 case EXEC_FLUSH:
1312 res = gfc_trans_flush (code);
1313 break;
1315 case EXEC_SYNC_ALL:
1316 case EXEC_SYNC_IMAGES:
1317 case EXEC_SYNC_MEMORY:
1318 res = gfc_trans_sync (code, code->op);
1319 break;
1321 case EXEC_FORALL:
1322 res = gfc_trans_forall (code);
1323 break;
1325 case EXEC_WHERE:
1326 res = gfc_trans_where (code);
1327 break;
1329 case EXEC_ALLOCATE:
1330 res = gfc_trans_allocate (code);
1331 break;
1333 case EXEC_DEALLOCATE:
1334 res = gfc_trans_deallocate (code);
1335 break;
1337 case EXEC_OPEN:
1338 res = gfc_trans_open (code);
1339 break;
1341 case EXEC_CLOSE:
1342 res = gfc_trans_close (code);
1343 break;
1345 case EXEC_READ:
1346 res = gfc_trans_read (code);
1347 break;
1349 case EXEC_WRITE:
1350 res = gfc_trans_write (code);
1351 break;
1353 case EXEC_IOLENGTH:
1354 res = gfc_trans_iolength (code);
1355 break;
1357 case EXEC_BACKSPACE:
1358 res = gfc_trans_backspace (code);
1359 break;
1361 case EXEC_ENDFILE:
1362 res = gfc_trans_endfile (code);
1363 break;
1365 case EXEC_INQUIRE:
1366 res = gfc_trans_inquire (code);
1367 break;
1369 case EXEC_WAIT:
1370 res = gfc_trans_wait (code);
1371 break;
1373 case EXEC_REWIND:
1374 res = gfc_trans_rewind (code);
1375 break;
1377 case EXEC_TRANSFER:
1378 res = gfc_trans_transfer (code);
1379 break;
1381 case EXEC_DT_END:
1382 res = gfc_trans_dt_end (code);
1383 break;
1385 case EXEC_OMP_ATOMIC:
1386 case EXEC_OMP_BARRIER:
1387 case EXEC_OMP_CRITICAL:
1388 case EXEC_OMP_DO:
1389 case EXEC_OMP_FLUSH:
1390 case EXEC_OMP_MASTER:
1391 case EXEC_OMP_ORDERED:
1392 case EXEC_OMP_PARALLEL:
1393 case EXEC_OMP_PARALLEL_DO:
1394 case EXEC_OMP_PARALLEL_SECTIONS:
1395 case EXEC_OMP_PARALLEL_WORKSHARE:
1396 case EXEC_OMP_SECTIONS:
1397 case EXEC_OMP_SINGLE:
1398 case EXEC_OMP_TASK:
1399 case EXEC_OMP_TASKWAIT:
1400 case EXEC_OMP_WORKSHARE:
1401 res = gfc_trans_omp_directive (code);
1402 break;
1404 default:
1405 internal_error ("gfc_trans_code(): Bad statement code");
1408 gfc_set_backend_locus (&code->loc);
1410 if (res != NULL_TREE && ! IS_EMPTY_STMT (res))
1412 if (TREE_CODE (res) != STATEMENT_LIST)
1413 SET_EXPR_LOCATION (res, input_location);
1415 /* Add the new statement to the block. */
1416 gfc_add_expr_to_block (&block, res);
1420 /* Return the finished block. */
1421 return gfc_finish_block (&block);
1425 /* Translate an executable statement with condition, cond. The condition is
1426 used by gfc_trans_do to test for IO result conditions inside implied
1427 DO loops of READ and WRITE statements. See build_dt in trans-io.c. */
1429 tree
1430 gfc_trans_code_cond (gfc_code * code, tree cond)
1432 return trans_code (code, cond);
1435 /* Translate an executable statement without condition. */
1437 tree
1438 gfc_trans_code (gfc_code * code)
1440 return trans_code (code, NULL_TREE);
1444 /* This function is called after a complete program unit has been parsed
1445 and resolved. */
1447 void
1448 gfc_generate_code (gfc_namespace * ns)
1450 ompws_flags = 0;
1451 if (ns->is_block_data)
1453 gfc_generate_block_data (ns);
1454 return;
1457 gfc_generate_function_code (ns);
1461 /* This function is called after a complete module has been parsed
1462 and resolved. */
1464 void
1465 gfc_generate_module_code (gfc_namespace * ns)
1467 gfc_namespace *n;
1468 struct module_htab_entry *entry;
1470 gcc_assert (ns->proc_name->backend_decl == NULL);
1471 ns->proc_name->backend_decl
1472 = build_decl (ns->proc_name->declared_at.lb->location,
1473 NAMESPACE_DECL, get_identifier (ns->proc_name->name),
1474 void_type_node);
1475 entry = gfc_find_module (ns->proc_name->name);
1476 if (entry->namespace_decl)
1477 /* Buggy sourcecode, using a module before defining it? */
1478 htab_empty (entry->decls);
1479 entry->namespace_decl = ns->proc_name->backend_decl;
1481 gfc_generate_module_vars (ns);
1483 /* We need to generate all module function prototypes first, to allow
1484 sibling calls. */
1485 for (n = ns->contained; n; n = n->sibling)
1487 gfc_entry_list *el;
1489 if (!n->proc_name)
1490 continue;
1492 gfc_create_function_decl (n, false);
1493 DECL_CONTEXT (n->proc_name->backend_decl) = ns->proc_name->backend_decl;
1494 gfc_module_add_decl (entry, n->proc_name->backend_decl);
1495 for (el = ns->entries; el; el = el->next)
1497 DECL_CONTEXT (el->sym->backend_decl) = ns->proc_name->backend_decl;
1498 gfc_module_add_decl (entry, el->sym->backend_decl);
1502 for (n = ns->contained; n; n = n->sibling)
1504 if (!n->proc_name)
1505 continue;
1507 gfc_generate_function_code (n);
1512 /* Initialize an init/cleanup block with existing code. */
1514 void
1515 gfc_start_wrapped_block (gfc_wrapped_block* block, tree code)
1517 gcc_assert (block);
1519 block->init = NULL_TREE;
1520 block->code = code;
1521 block->cleanup = NULL_TREE;
1525 /* Add a new pair of initializers/clean-up code. */
1527 void
1528 gfc_add_init_cleanup (gfc_wrapped_block* block, tree init, tree cleanup)
1530 gcc_assert (block);
1532 /* The new pair of init/cleanup should be "wrapped around" the existing
1533 block of code, thus the initialization is added to the front and the
1534 cleanup to the back. */
1535 add_expr_to_chain (&block->init, init, true);
1536 add_expr_to_chain (&block->cleanup, cleanup, false);
1540 /* Finish up a wrapped block by building a corresponding try-finally expr. */
1542 tree
1543 gfc_finish_wrapped_block (gfc_wrapped_block* block)
1545 tree result;
1547 gcc_assert (block);
1549 /* Build the final expression. For this, just add init and body together,
1550 and put clean-up with that into a TRY_FINALLY_EXPR. */
1551 result = block->init;
1552 add_expr_to_chain (&result, block->code, false);
1553 if (block->cleanup)
1554 result = build2_loc (input_location, TRY_FINALLY_EXPR, void_type_node,
1555 result, block->cleanup);
1557 /* Clear the block. */
1558 block->init = NULL_TREE;
1559 block->code = NULL_TREE;
1560 block->cleanup = NULL_TREE;
1562 return result;
1566 /* Helper function for marking a boolean expression tree as unlikely. */
1568 tree
1569 gfc_unlikely (tree cond)
1571 tree tmp;
1573 cond = fold_convert (long_integer_type_node, cond);
1574 tmp = build_zero_cst (long_integer_type_node);
1575 cond = build_call_expr_loc (input_location,
1576 built_in_decls[BUILT_IN_EXPECT], 2, cond, tmp);
1577 cond = fold_convert (boolean_type_node, cond);
1578 return cond;