]> gcc.gnu.org Git - gcc.git/blame - gcc/fortran/trans-decl.c
re PR target/22432 (Wrong code generation using MMX intrinsics on amd64)
[gcc.git] / gcc / fortran / trans-decl.c
CommitLineData
6de9cd9a 1/* Backend function setup
e7dc5b4f 2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
6de9cd9a
DN
3 Contributed by Paul Brook
4
9fc4d79b 5This file is part of GCC.
6de9cd9a 6
9fc4d79b
TS
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
6de9cd9a 11
9fc4d79b
TS
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
6de9cd9a
DN
16
17You should have received a copy of the GNU General Public License
9fc4d79b 18along with GCC; see the file COPYING. If not, write to the Free
ab57747b
KC
19Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2002110-1301, USA. */
6de9cd9a
DN
21
22/* trans-decl.c -- Handling of backend function and variable decls, etc */
23
24#include "config.h"
25#include "system.h"
26#include "coretypes.h"
27#include "tree.h"
28#include "tree-dump.h"
eadf906f 29#include "tree-gimple.h"
6de9cd9a
DN
30#include "ggc.h"
31#include "toplev.h"
32#include "tm.h"
33#include "target.h"
34#include "function.h"
6de9cd9a
DN
35#include "flags.h"
36#include "cgraph.h"
6de9cd9a
DN
37#include "gfortran.h"
38#include "trans.h"
39#include "trans-types.h"
40#include "trans-array.h"
41#include "trans-const.h"
42/* Only for gfc_trans_code. Shouldn't need to include this. */
43#include "trans-stmt.h"
44
45#define MAX_LABEL_VALUE 99999
46
47
48/* Holds the result of the function if no result variable specified. */
49
50static GTY(()) tree current_fake_result_decl;
51
52static GTY(()) tree current_function_return_label;
53
54
55/* Holds the variable DECLs for the current function. */
56
57static GTY(()) tree saved_function_decls = NULL_TREE;
58static GTY(()) tree saved_parent_function_decls = NULL_TREE;
59
60
61/* The namespace of the module we're currently generating. Only used while
62 outputting decls for module variables. Do not rely on this being set. */
63
64static gfc_namespace *module_namespace;
65
66
67/* List of static constructor functions. */
68
69tree gfc_static_ctors;
70
71
72/* Function declarations for builtin library functions. */
73
74tree gfor_fndecl_internal_malloc;
75tree gfor_fndecl_internal_malloc64;
ec25720b
RS
76tree gfor_fndecl_internal_realloc;
77tree gfor_fndecl_internal_realloc64;
6de9cd9a
DN
78tree gfor_fndecl_internal_free;
79tree gfor_fndecl_allocate;
80tree gfor_fndecl_allocate64;
81tree gfor_fndecl_deallocate;
82tree gfor_fndecl_pause_numeric;
83tree gfor_fndecl_pause_string;
84tree gfor_fndecl_stop_numeric;
85tree gfor_fndecl_stop_string;
86tree gfor_fndecl_select_string;
87tree gfor_fndecl_runtime_error;
944b8b35 88tree gfor_fndecl_set_fpe;
8b67b708 89tree gfor_fndecl_set_std;
25fc05eb 90tree gfor_fndecl_ttynam;
6de9cd9a
DN
91tree gfor_fndecl_in_pack;
92tree gfor_fndecl_in_unpack;
93tree gfor_fndecl_associated;
94
95
96/* Math functions. Many other math functions are handled in
97 trans-intrinsic.c. */
98
644cb69f 99gfc_powdecl_list gfor_fndecl_math_powi[4][3];
6de9cd9a
DN
100tree gfor_fndecl_math_cpowf;
101tree gfor_fndecl_math_cpow;
644cb69f
FXC
102tree gfor_fndecl_math_cpowl10;
103tree gfor_fndecl_math_cpowl16;
6de9cd9a
DN
104tree gfor_fndecl_math_ishftc4;
105tree gfor_fndecl_math_ishftc8;
644cb69f 106tree gfor_fndecl_math_ishftc16;
6de9cd9a
DN
107tree gfor_fndecl_math_exponent4;
108tree gfor_fndecl_math_exponent8;
644cb69f
FXC
109tree gfor_fndecl_math_exponent10;
110tree gfor_fndecl_math_exponent16;
6de9cd9a
DN
111
112
113/* String functions. */
114
115tree gfor_fndecl_copy_string;
116tree gfor_fndecl_compare_string;
117tree gfor_fndecl_concat_string;
118tree gfor_fndecl_string_len_trim;
119tree gfor_fndecl_string_index;
120tree gfor_fndecl_string_scan;
121tree gfor_fndecl_string_verify;
122tree gfor_fndecl_string_trim;
123tree gfor_fndecl_string_repeat;
124tree gfor_fndecl_adjustl;
125tree gfor_fndecl_adjustr;
126
127
128/* Other misc. runtime library functions. */
129
130tree gfor_fndecl_size0;
131tree gfor_fndecl_size1;
b41b2534 132tree gfor_fndecl_iargc;
6de9cd9a
DN
133
134/* Intrinsic functions implemented in FORTRAN. */
135tree gfor_fndecl_si_kind;
136tree gfor_fndecl_sr_kind;
137
138
139static void
140gfc_add_decl_to_parent_function (tree decl)
141{
6e45f57b 142 gcc_assert (decl);
6de9cd9a
DN
143 DECL_CONTEXT (decl) = DECL_CONTEXT (current_function_decl);
144 DECL_NONLOCAL (decl) = 1;
145 TREE_CHAIN (decl) = saved_parent_function_decls;
146 saved_parent_function_decls = decl;
147}
148
149void
150gfc_add_decl_to_function (tree decl)
151{
6e45f57b 152 gcc_assert (decl);
6de9cd9a
DN
153 TREE_USED (decl) = 1;
154 DECL_CONTEXT (decl) = current_function_decl;
155 TREE_CHAIN (decl) = saved_function_decls;
156 saved_function_decls = decl;
157}
158
159
c006df4e
SB
160/* Build a backend label declaration. Set TREE_USED for named labels.
161 The context of the label is always the current_function_decl. All
162 labels are marked artificial. */
6de9cd9a
DN
163
164tree
165gfc_build_label_decl (tree label_id)
166{
167 /* 2^32 temporaries should be enough. */
168 static unsigned int tmp_num = 1;
169 tree label_decl;
170 char *label_name;
171
172 if (label_id == NULL_TREE)
173 {
174 /* Build an internal label name. */
175 ASM_FORMAT_PRIVATE_NAME (label_name, "L", tmp_num++);
176 label_id = get_identifier (label_name);
177 }
178 else
179 label_name = NULL;
180
181 /* Build the LABEL_DECL node. Labels have no type. */
182 label_decl = build_decl (LABEL_DECL, label_id, void_type_node);
183 DECL_CONTEXT (label_decl) = current_function_decl;
184 DECL_MODE (label_decl) = VOIDmode;
185
c006df4e
SB
186 /* We always define the label as used, even if the original source
187 file never references the label. We don't want all kinds of
188 spurious warnings for old-style Fortran code with too many
189 labels. */
190 TREE_USED (label_decl) = 1;
6de9cd9a 191
c006df4e 192 DECL_ARTIFICIAL (label_decl) = 1;
6de9cd9a
DN
193 return label_decl;
194}
195
196
197/* Returns the return label for the current function. */
198
199tree
200gfc_get_return_label (void)
201{
202 char name[GFC_MAX_SYMBOL_LEN + 10];
203
204 if (current_function_return_label)
205 return current_function_return_label;
206
207 sprintf (name, "__return_%s",
208 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
209
210 current_function_return_label =
211 gfc_build_label_decl (get_identifier (name));
212
213 DECL_ARTIFICIAL (current_function_return_label) = 1;
214
215 return current_function_return_label;
216}
217
218
c8cc8542
PB
219/* Set the backend source location of a decl. */
220
221void
222gfc_set_decl_location (tree decl, locus * loc)
223{
224#ifdef USE_MAPPED_LOCATION
225 DECL_SOURCE_LOCATION (decl) = loc->lb->location;
226#else
227 DECL_SOURCE_LINE (decl) = loc->lb->linenum;
228 DECL_SOURCE_FILE (decl) = loc->lb->file->filename;
229#endif
230}
231
232
6de9cd9a
DN
233/* Return the backend label declaration for a given label structure,
234 or create it if it doesn't exist yet. */
235
236tree
237gfc_get_label_decl (gfc_st_label * lp)
238{
6de9cd9a
DN
239 if (lp->backend_decl)
240 return lp->backend_decl;
241 else
242 {
243 char label_name[GFC_MAX_SYMBOL_LEN + 1];
244 tree label_decl;
245
246 /* Validate the label declaration from the front end. */
6e45f57b 247 gcc_assert (lp != NULL && lp->value <= MAX_LABEL_VALUE);
6de9cd9a
DN
248
249 /* Build a mangled name for the label. */
250 sprintf (label_name, "__label_%.6d", lp->value);
251
252 /* Build the LABEL_DECL node. */
253 label_decl = gfc_build_label_decl (get_identifier (label_name));
254
255 /* Tell the debugger where the label came from. */
f8d0aee5 256 if (lp->value <= MAX_LABEL_VALUE) /* An internal label. */
c8cc8542 257 gfc_set_decl_location (label_decl, &lp->where);
6de9cd9a
DN
258 else
259 DECL_ARTIFICIAL (label_decl) = 1;
260
261 /* Store the label in the label list and return the LABEL_DECL. */
262 lp->backend_decl = label_decl;
263 return label_decl;
264 }
265}
266
267
268/* Convert a gfc_symbol to an identifier of the same name. */
269
270static tree
271gfc_sym_identifier (gfc_symbol * sym)
272{
273 return (get_identifier (sym->name));
274}
275
276
277/* Construct mangled name from symbol name. */
278
279static tree
280gfc_sym_mangled_identifier (gfc_symbol * sym)
281{
282 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
283
cb9e4f55 284 if (sym->module == NULL)
6de9cd9a
DN
285 return gfc_sym_identifier (sym);
286 else
287 {
288 snprintf (name, sizeof name, "__%s__%s", sym->module, sym->name);
289 return get_identifier (name);
290 }
291}
292
293
294/* Construct mangled function name from symbol name. */
295
296static tree
297gfc_sym_mangled_function_id (gfc_symbol * sym)
298{
299 int has_underscore;
300 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
301
cb9e4f55
TS
302 if (sym->module == NULL || sym->attr.proc == PROC_EXTERNAL
303 || (sym->module != NULL && sym->attr.if_source == IFSRC_IFBODY))
6de9cd9a
DN
304 {
305 if (strcmp (sym->name, "MAIN__") == 0
306 || sym->attr.proc == PROC_INTRINSIC)
307 return get_identifier (sym->name);
308
309 if (gfc_option.flag_underscoring)
310 {
311 has_underscore = strchr (sym->name, '_') != 0;
312 if (gfc_option.flag_second_underscore && has_underscore)
313 snprintf (name, sizeof name, "%s__", sym->name);
314 else
315 snprintf (name, sizeof name, "%s_", sym->name);
316 return get_identifier (name);
317 }
318 else
319 return get_identifier (sym->name);
320 }
321 else
322 {
323 snprintf (name, sizeof name, "__%s__%s", sym->module, sym->name);
324 return get_identifier (name);
325 }
326}
327
328
bae88af6
TS
329/* Returns true if a variable of specified size should go on the stack. */
330
331int
332gfc_can_put_var_on_stack (tree size)
333{
334 unsigned HOST_WIDE_INT low;
335
336 if (!INTEGER_CST_P (size))
337 return 0;
338
339 if (gfc_option.flag_max_stack_var_size < 0)
340 return 1;
341
342 if (TREE_INT_CST_HIGH (size) != 0)
343 return 0;
344
345 low = TREE_INT_CST_LOW (size);
346 if (low > (unsigned HOST_WIDE_INT) gfc_option.flag_max_stack_var_size)
347 return 0;
348
349/* TODO: Set a per-function stack size limit. */
350
351 return 1;
352}
353
354
b122dc6a
JJ
355/* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
356 an expression involving its corresponding pointer. There are
357 2 cases; one for variable size arrays, and one for everything else,
358 because variable-sized arrays require one fewer level of
359 indirection. */
360
361static void
362gfc_finish_cray_pointee (tree decl, gfc_symbol *sym)
363{
364 tree ptr_decl = gfc_get_symbol_decl (sym->cp_pointer);
365 tree value;
366
367 /* Parameters need to be dereferenced. */
368 if (sym->cp_pointer->attr.dummy)
369 ptr_decl = gfc_build_indirect_ref (ptr_decl);
370
371 /* Check to see if we're dealing with a variable-sized array. */
372 if (sym->attr.dimension
373 && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
374 {
831d7813 375 /* These decls will be dereferenced later, so we don't dereference
b122dc6a
JJ
376 them here. */
377 value = convert (TREE_TYPE (decl), ptr_decl);
378 }
379 else
380 {
381 ptr_decl = convert (build_pointer_type (TREE_TYPE (decl)),
382 ptr_decl);
383 value = gfc_build_indirect_ref (ptr_decl);
384 }
385
386 SET_DECL_VALUE_EXPR (decl, value);
387 DECL_HAS_VALUE_EXPR_P (decl) = 1;
388 /* This is a fake variable just for debugging purposes. */
389 TREE_ASM_WRITTEN (decl) = 1;
390}
391
392
6de9cd9a
DN
393/* Finish processing of a declaration and install its initial value. */
394
395static void
396gfc_finish_decl (tree decl, tree init)
397{
398 if (TREE_CODE (decl) == PARM_DECL)
6e45f57b 399 gcc_assert (init == NULL_TREE);
6de9cd9a
DN
400 /* Remember that PARM_DECL doesn't have a DECL_INITIAL field per se
401 -- it overlaps DECL_ARG_TYPE. */
402 else if (init == NULL_TREE)
6e45f57b 403 gcc_assert (DECL_INITIAL (decl) == NULL_TREE);
6de9cd9a 404 else
6e45f57b 405 gcc_assert (DECL_INITIAL (decl) == error_mark_node);
6de9cd9a
DN
406
407 if (init != NULL_TREE)
408 {
409 if (TREE_CODE (decl) != TYPE_DECL)
410 DECL_INITIAL (decl) = init;
411 else
412 {
413 /* typedef foo = bar; store the type of bar as the type of foo. */
414 TREE_TYPE (decl) = TREE_TYPE (init);
415 DECL_INITIAL (decl) = init = 0;
416 }
417 }
418
419 if (TREE_CODE (decl) == VAR_DECL)
420 {
421 if (DECL_SIZE (decl) == NULL_TREE
422 && TYPE_SIZE (TREE_TYPE (decl)) != NULL_TREE)
423 layout_decl (decl, 0);
424
425 /* A static variable with an incomplete type is an error if it is
426 initialized. Also if it is not file scope. Otherwise, let it
427 through, but if it is not `extern' then it may cause an error
428 message later. */
429 /* An automatic variable with an incomplete type is an error. */
430 if (DECL_SIZE (decl) == NULL_TREE
431 && (TREE_STATIC (decl) ? (DECL_INITIAL (decl) != 0
432 || DECL_CONTEXT (decl) != 0)
433 : !DECL_EXTERNAL (decl)))
434 {
435 gfc_fatal_error ("storage size not known");
436 }
437
438 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
439 && (DECL_SIZE (decl) != 0)
440 && (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST))
441 {
442 gfc_fatal_error ("storage size not constant");
443 }
444 }
445
446}
447
448
449/* Apply symbol attributes to a variable, and add it to the function scope. */
450
451static void
452gfc_finish_var_decl (tree decl, gfc_symbol * sym)
453{
f8d0aee5 454 /* TREE_ADDRESSABLE means the address of this variable is actually needed.
6de9cd9a
DN
455 This is the equivalent of the TARGET variables.
456 We also need to set this if the variable is passed by reference in a
457 CALL statement. */
83d890b9 458
b122dc6a 459 /* Set DECL_VALUE_EXPR for Cray Pointees. */
83d890b9 460 if (sym->attr.cray_pointee)
b122dc6a 461 gfc_finish_cray_pointee (decl, sym);
83d890b9 462
6de9cd9a
DN
463 if (sym->attr.target)
464 TREE_ADDRESSABLE (decl) = 1;
465 /* If it wasn't used we wouldn't be getting it. */
466 TREE_USED (decl) = 1;
467
468 /* Chain this decl to the pending declarations. Don't do pushdecl()
469 because this would add them to the current scope rather than the
470 function scope. */
471 if (current_function_decl != NULL_TREE)
472 {
d48734ef
EE
473 if (sym->ns->proc_name->backend_decl == current_function_decl
474 || sym->result == sym)
6de9cd9a
DN
475 gfc_add_decl_to_function (decl);
476 else
477 gfc_add_decl_to_parent_function (decl);
478 }
479
b122dc6a
JJ
480 if (sym->attr.cray_pointee)
481 return;
482
6de9cd9a
DN
483 /* If a variable is USE associated, it's always external. */
484 if (sym->attr.use_assoc)
485 {
486 DECL_EXTERNAL (decl) = 1;
487 TREE_PUBLIC (decl) = 1;
488 }
cb9e4f55 489 else if (sym->module && !sym->attr.result && !sym->attr.dummy)
6de9cd9a 490 {
adf3ed3f 491 /* TODO: Don't set sym->module for result or dummy variables. */
d48734ef 492 gcc_assert (current_function_decl == NULL_TREE || sym->result == sym);
6de9cd9a
DN
493 /* This is the declaration of a module variable. */
494 TREE_PUBLIC (decl) = 1;
495 TREE_STATIC (decl) = 1;
496 }
497
498 if ((sym->attr.save || sym->attr.data || sym->value)
499 && !sym->attr.use_assoc)
500 TREE_STATIC (decl) = 1;
501
502 /* Keep variables larger than max-stack-var-size off stack. */
503 if (!sym->ns->proc_name->attr.recursive
504 && INTEGER_CST_P (DECL_SIZE_UNIT (decl))
505 && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl)))
506 TREE_STATIC (decl) = 1;
507}
508
509
510/* Allocate the lang-specific part of a decl. */
511
512void
513gfc_allocate_lang_decl (tree decl)
514{
515 DECL_LANG_SPECIFIC (decl) = (struct lang_decl *)
516 ggc_alloc_cleared (sizeof (struct lang_decl));
517}
518
519/* Remember a symbol to generate initialization/cleanup code at function
520 entry/exit. */
521
522static void
523gfc_defer_symbol_init (gfc_symbol * sym)
524{
525 gfc_symbol *p;
526 gfc_symbol *last;
527 gfc_symbol *head;
528
529 /* Don't add a symbol twice. */
530 if (sym->tlink)
531 return;
532
533 last = head = sym->ns->proc_name;
534 p = last->tlink;
535
536 /* Make sure that setup code for dummy variables which are used in the
537 setup of other variables is generated first. */
538 if (sym->attr.dummy)
539 {
540 /* Find the first dummy arg seen after us, or the first non-dummy arg.
541 This is a circular list, so don't go past the head. */
542 while (p != head
543 && (!p->attr.dummy || p->dummy_order > sym->dummy_order))
544 {
545 last = p;
546 p = p->tlink;
547 }
548 }
549 /* Insert in between last and p. */
550 last->tlink = sym;
551 sym->tlink = p;
552}
553
554
555/* Create an array index type variable with function scope. */
556
557static tree
558create_index_var (const char * pfx, int nest)
559{
560 tree decl;
561
562 decl = gfc_create_var_np (gfc_array_index_type, pfx);
563 if (nest)
564 gfc_add_decl_to_parent_function (decl);
565 else
566 gfc_add_decl_to_function (decl);
567 return decl;
568}
569
570
571/* Create variables to hold all the non-constant bits of info for a
572 descriptorless array. Remember these in the lang-specific part of the
573 type. */
574
575static void
576gfc_build_qualified_array (tree decl, gfc_symbol * sym)
577{
578 tree type;
579 int dim;
580 int nest;
581
582 type = TREE_TYPE (decl);
583
584 /* We just use the descriptor, if there is one. */
585 if (GFC_DESCRIPTOR_TYPE_P (type))
586 return;
587
6e45f57b 588 gcc_assert (GFC_ARRAY_TYPE_P (type));
6de9cd9a
DN
589 nest = (sym->ns->proc_name->backend_decl != current_function_decl)
590 && !sym->attr.contained;
591
592 for (dim = 0; dim < GFC_TYPE_ARRAY_RANK (type); dim++)
593 {
594 if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE)
595 GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest);
aa9c57ec 596 /* Don't try to use the unknown bound for assumed shape arrays. */
6de9cd9a
DN
597 if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE
598 && (sym->as->type != AS_ASSUMED_SIZE
599 || dim < GFC_TYPE_ARRAY_RANK (type) - 1))
600 GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest);
601
602 if (GFC_TYPE_ARRAY_STRIDE (type, dim) == NULL_TREE)
603 GFC_TYPE_ARRAY_STRIDE (type, dim) = create_index_var ("stride", nest);
604 }
605 if (GFC_TYPE_ARRAY_OFFSET (type) == NULL_TREE)
606 {
607 GFC_TYPE_ARRAY_OFFSET (type) = gfc_create_var_np (gfc_array_index_type,
608 "offset");
609 if (nest)
610 gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type));
611 else
612 gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type));
613 }
614}
615
616
617/* For some dummy arguments we don't use the actual argument directly.
bae88af6 618 Instead we create a local decl and use that. This allows us to perform
6de9cd9a
DN
619 initialization, and construct full type information. */
620
621static tree
622gfc_build_dummy_array_decl (gfc_symbol * sym, tree dummy)
623{
624 tree decl;
625 tree type;
626 gfc_array_spec *as;
627 char *name;
628 int packed;
629 int n;
630 bool known_size;
631
632 if (sym->attr.pointer || sym->attr.allocatable)
633 return dummy;
634
635 /* Add to list of variables if not a fake result variable. */
636 if (sym->attr.result || sym->attr.dummy)
637 gfc_defer_symbol_init (sym);
638
639 type = TREE_TYPE (dummy);
6e45f57b 640 gcc_assert (TREE_CODE (dummy) == PARM_DECL
6de9cd9a
DN
641 && POINTER_TYPE_P (type));
642
f8d0aee5 643 /* Do we know the element size? */
6de9cd9a
DN
644 known_size = sym->ts.type != BT_CHARACTER
645 || INTEGER_CST_P (sym->ts.cl->backend_decl);
646
647 if (known_size && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type)))
648 {
649 /* For descriptorless arrays with known element size the actual
650 argument is sufficient. */
6e45f57b 651 gcc_assert (GFC_ARRAY_TYPE_P (type));
6de9cd9a
DN
652 gfc_build_qualified_array (dummy, sym);
653 return dummy;
654 }
655
656 type = TREE_TYPE (type);
657 if (GFC_DESCRIPTOR_TYPE_P (type))
658 {
659 /* Create a decriptorless array pointer. */
660 as = sym->as;
661 packed = 0;
662 if (!gfc_option.flag_repack_arrays)
663 {
664 if (as->type == AS_ASSUMED_SIZE)
665 packed = 2;
666 }
667 else
668 {
669 if (as->type == AS_EXPLICIT)
670 {
671 packed = 2;
672 for (n = 0; n < as->rank; n++)
673 {
674 if (!(as->upper[n]
675 && as->lower[n]
676 && as->upper[n]->expr_type == EXPR_CONSTANT
677 && as->lower[n]->expr_type == EXPR_CONSTANT))
678 packed = 1;
679 }
680 }
681 else
682 packed = 1;
683 }
684
685 type = gfc_typenode_for_spec (&sym->ts);
686 type = gfc_get_nodesc_array_type (type, sym->as, packed);
687 }
688 else
689 {
690 /* We now have an expression for the element size, so create a fully
691 qualified type. Reset sym->backend decl or this will just return the
692 old type. */
693 sym->backend_decl = NULL_TREE;
694 type = gfc_sym_type (sym);
695 packed = 2;
696 }
697
698 ASM_FORMAT_PRIVATE_NAME (name, IDENTIFIER_POINTER (DECL_NAME (dummy)), 0);
699 decl = build_decl (VAR_DECL, get_identifier (name), type);
700
701 DECL_ARTIFICIAL (decl) = 1;
702 TREE_PUBLIC (decl) = 0;
703 TREE_STATIC (decl) = 0;
704 DECL_EXTERNAL (decl) = 0;
705
706 /* We should never get deferred shape arrays here. We used to because of
707 frontend bugs. */
6e45f57b 708 gcc_assert (sym->as->type != AS_DEFERRED);
6de9cd9a
DN
709
710 switch (packed)
711 {
712 case 1:
713 GFC_DECL_PARTIAL_PACKED_ARRAY (decl) = 1;
714 break;
715
716 case 2:
717 GFC_DECL_PACKED_ARRAY (decl) = 1;
718 break;
719 }
720
721 gfc_build_qualified_array (decl, sym);
722
723 if (DECL_LANG_SPECIFIC (dummy))
724 DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (dummy);
725 else
726 gfc_allocate_lang_decl (decl);
727
728 GFC_DECL_SAVED_DESCRIPTOR (decl) = dummy;
729
730 if (sym->ns->proc_name->backend_decl == current_function_decl
731 || sym->attr.contained)
732 gfc_add_decl_to_function (decl);
733 else
734 gfc_add_decl_to_parent_function (decl);
735
736 return decl;
737}
738
739
740/* Return a constant or a variable to use as a string length. Does not
741 add the decl to the current scope. */
742
743static tree
744gfc_create_string_length (gfc_symbol * sym)
745{
746 tree length;
747
6e45f57b 748 gcc_assert (sym->ts.cl);
6de9cd9a
DN
749 gfc_conv_const_charlen (sym->ts.cl);
750
751 if (sym->ts.cl->backend_decl == NULL_TREE)
752 {
753 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
754
755 /* Also prefix the mangled name. */
756 strcpy (&name[1], sym->name);
757 name[0] = '.';
758 length = build_decl (VAR_DECL, get_identifier (name),
d7177ab2 759 gfc_charlen_type_node);
6de9cd9a
DN
760 DECL_ARTIFICIAL (length) = 1;
761 TREE_USED (length) = 1;
762 gfc_defer_symbol_init (sym);
763 sym->ts.cl->backend_decl = length;
764 }
765
766 return sym->ts.cl->backend_decl;
767}
768
910450c1
FW
769/* If a variable is assigned a label, we add another two auxiliary
770 variables. */
771
772static void
773gfc_add_assign_aux_vars (gfc_symbol * sym)
774{
775 tree addr;
776 tree length;
777 tree decl;
778
779 gcc_assert (sym->backend_decl);
780
781 decl = sym->backend_decl;
782 gfc_allocate_lang_decl (decl);
783 GFC_DECL_ASSIGN (decl) = 1;
784 length = build_decl (VAR_DECL, create_tmp_var_name (sym->name),
785 gfc_charlen_type_node);
786 addr = build_decl (VAR_DECL, create_tmp_var_name (sym->name),
787 pvoid_type_node);
788 gfc_finish_var_decl (length, sym);
789 gfc_finish_var_decl (addr, sym);
790 /* STRING_LENGTH is also used as flag. Less than -1 means that
791 ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the
792 target label's address. Otherwise, value is the length of a format string
793 and ASSIGN_ADDR is its address. */
794 if (TREE_STATIC (length))
795 DECL_INITIAL (length) = build_int_cst (NULL_TREE, -2);
796 else
797 gfc_defer_symbol_init (sym);
798
799 GFC_DECL_STRING_LEN (decl) = length;
800 GFC_DECL_ASSIGN_ADDR (decl) = addr;
801}
6de9cd9a
DN
802
803/* Return the decl for a gfc_symbol, create it if it doesn't already
804 exist. */
805
806tree
807gfc_get_symbol_decl (gfc_symbol * sym)
808{
809 tree decl;
810 tree length = NULL_TREE;
6de9cd9a
DN
811 int byref;
812
6e45f57b 813 gcc_assert (sym->attr.referenced);
6de9cd9a
DN
814
815 if (sym->ns && sym->ns->proc_name->attr.function)
816 byref = gfc_return_by_reference (sym->ns->proc_name);
817 else
818 byref = 0;
819
820 if ((sym->attr.dummy && ! sym->attr.function) || (sym->attr.result && byref))
821 {
822 /* Return via extra parameter. */
823 if (sym->attr.result && byref
824 && !sym->backend_decl)
825 {
826 sym->backend_decl =
827 DECL_ARGUMENTS (sym->ns->proc_name->backend_decl);
d198b59a
JJ
828 /* For entry master function skip over the __entry
829 argument. */
830 if (sym->ns->proc_name->attr.entry_master)
831 sym->backend_decl = TREE_CHAIN (sym->backend_decl);
6de9cd9a
DN
832 }
833
834 /* Dummy variables should already have been created. */
6e45f57b 835 gcc_assert (sym->backend_decl);
6de9cd9a
DN
836
837 /* Create a character length variable. */
838 if (sym->ts.type == BT_CHARACTER)
839 {
840 if (sym->ts.cl->backend_decl == NULL_TREE)
841 {
842 length = gfc_create_string_length (sym);
843 if (TREE_CODE (length) != INTEGER_CST)
844 {
845 gfc_finish_var_decl (length, sym);
846 gfc_defer_symbol_init (sym);
847 }
848 }
849 }
850
851 /* Use a copy of the descriptor for dummy arrays. */
852 if (sym->attr.dimension && !TREE_USED (sym->backend_decl))
853 {
854 sym->backend_decl =
855 gfc_build_dummy_array_decl (sym, sym->backend_decl);
856 }
857
858 TREE_USED (sym->backend_decl) = 1;
910450c1
FW
859 if (sym->attr.assign && GFC_DECL_ASSIGN (sym->backend_decl) == 0)
860 {
861 gfc_add_assign_aux_vars (sym);
862 }
6de9cd9a
DN
863 return sym->backend_decl;
864 }
865
866 if (sym->backend_decl)
867 return sym->backend_decl;
868
6de9cd9a
DN
869 /* Catch function declarations. Only used for actual parameters. */
870 if (sym->attr.flavor == FL_PROCEDURE)
871 {
872 decl = gfc_get_extern_function_decl (sym);
873 return decl;
874 }
875
876 if (sym->attr.intrinsic)
877 internal_error ("intrinsic variable which isn't a procedure");
878
879 /* Create string length decl first so that they can be used in the
880 type declaration. */
881 if (sym->ts.type == BT_CHARACTER)
882 length = gfc_create_string_length (sym);
883
884 /* Create the decl for the variable. */
885 decl = build_decl (VAR_DECL, gfc_sym_identifier (sym), gfc_sym_type (sym));
886
c8cc8542
PB
887 gfc_set_decl_location (decl, &sym->declared_at);
888
f8d0aee5 889 /* Symbols from modules should have their assembler names mangled.
6de9cd9a
DN
890 This is done here rather than in gfc_finish_var_decl because it
891 is different for string length variables. */
cb9e4f55 892 if (sym->module)
6de9cd9a
DN
893 SET_DECL_ASSEMBLER_NAME (decl, gfc_sym_mangled_identifier (sym));
894
895 if (sym->attr.dimension)
896 {
897 /* Create variables to hold the non-constant bits of array info. */
898 gfc_build_qualified_array (decl, sym);
899
900 /* Remember this variable for allocation/cleanup. */
901 gfc_defer_symbol_init (sym);
902
903 if ((sym->attr.allocatable || !sym->attr.dummy) && !sym->attr.pointer)
904 GFC_DECL_PACKED_ARRAY (decl) = 1;
905 }
906
907 gfc_finish_var_decl (decl, sym);
908
597073ac 909 if (sym->ts.type == BT_CHARACTER)
6de9cd9a 910 {
6de9cd9a
DN
911 /* Character variables need special handling. */
912 gfc_allocate_lang_decl (decl);
913
597073ac 914 if (TREE_CODE (length) != INTEGER_CST)
6de9cd9a
DN
915 {
916 char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
917
cb9e4f55 918 if (sym->module)
6de9cd9a
DN
919 {
920 /* Also prefix the mangled name for symbols from modules. */
921 strcpy (&name[1], sym->name);
922 name[0] = '.';
923 strcpy (&name[1],
924 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (length)));
925 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (name));
926 }
927 gfc_finish_var_decl (length, sym);
6e45f57b 928 gcc_assert (!sym->value);
6de9cd9a 929 }
6de9cd9a
DN
930 }
931 sym->backend_decl = decl;
932
910450c1
FW
933 if (sym->attr.assign)
934 {
935 gfc_add_assign_aux_vars (sym);
936 }
937
597073ac
PB
938 if (TREE_STATIC (decl) && !sym->attr.use_assoc)
939 {
940 /* Add static initializer. */
941 DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
942 TREE_TYPE (decl), sym->attr.dimension,
943 sym->attr.pointer || sym->attr.allocatable);
944 }
945
6de9cd9a
DN
946 return decl;
947}
948
949
7b5b57b7
PB
950/* Substitute a temporary variable in place of the real one. */
951
952void
953gfc_shadow_sym (gfc_symbol * sym, tree decl, gfc_saved_var * save)
954{
955 save->attr = sym->attr;
956 save->decl = sym->backend_decl;
957
958 gfc_clear_attr (&sym->attr);
959 sym->attr.referenced = 1;
960 sym->attr.flavor = FL_VARIABLE;
961
962 sym->backend_decl = decl;
963}
964
965
966/* Restore the original variable. */
967
968void
969gfc_restore_sym (gfc_symbol * sym, gfc_saved_var * save)
970{
971 sym->attr = save->attr;
972 sym->backend_decl = save->decl;
973}
974
975
6de9cd9a
DN
976/* Get a basic decl for an external function. */
977
978tree
979gfc_get_extern_function_decl (gfc_symbol * sym)
980{
981 tree type;
982 tree fndecl;
983 gfc_expr e;
984 gfc_intrinsic_sym *isym;
985 gfc_expr argexpr;
973ff4c0 986 char s[GFC_MAX_SYMBOL_LEN + 13]; /* "f2c_specific" and '\0'. */
6de9cd9a
DN
987 tree name;
988 tree mangled_name;
989
990 if (sym->backend_decl)
991 return sym->backend_decl;
992
3d79abbd
PB
993 /* We should never be creating external decls for alternate entry points.
994 The procedure may be an alternate entry point, but we don't want/need
995 to know that. */
6e45f57b 996 gcc_assert (!(sym->attr.entry || sym->attr.entry_master));
3d79abbd 997
6de9cd9a
DN
998 if (sym->attr.intrinsic)
999 {
1000 /* Call the resolution function to get the actual name. This is
1001 a nasty hack which relies on the resolution functions only looking
1002 at the first argument. We pass NULL for the second argument
1003 otherwise things like AINT get confused. */
1004 isym = gfc_find_function (sym->name);
6e45f57b 1005 gcc_assert (isym->resolve.f0 != NULL);
6de9cd9a
DN
1006
1007 memset (&e, 0, sizeof (e));
1008 e.expr_type = EXPR_FUNCTION;
1009
1010 memset (&argexpr, 0, sizeof (argexpr));
6e45f57b 1011 gcc_assert (isym->formal);
6de9cd9a
DN
1012 argexpr.ts = isym->formal->ts;
1013
1014 if (isym->formal->next == NULL)
1015 isym->resolve.f1 (&e, &argexpr);
1016 else
1017 {
1018 /* All specific intrinsics take one or two arguments. */
6e45f57b 1019 gcc_assert (isym->formal->next->next == NULL);
6de9cd9a
DN
1020 isym->resolve.f2 (&e, &argexpr, NULL);
1021 }
973ff4c0
TS
1022
1023 if (gfc_option.flag_f2c
1024 && ((e.ts.type == BT_REAL && e.ts.kind == gfc_default_real_kind)
1025 || e.ts.type == BT_COMPLEX))
1026 {
1027 /* Specific which needs a different implementation if f2c
1028 calling conventions are used. */
1029 sprintf (s, "f2c_specific%s", e.value.function.name);
1030 }
1031 else
1032 sprintf (s, "specific%s", e.value.function.name);
1033
6de9cd9a
DN
1034 name = get_identifier (s);
1035 mangled_name = name;
1036 }
1037 else
1038 {
1039 name = gfc_sym_identifier (sym);
1040 mangled_name = gfc_sym_mangled_function_id (sym);
1041 }
1042
1043 type = gfc_get_function_type (sym);
1044 fndecl = build_decl (FUNCTION_DECL, name, type);
1045
1046 SET_DECL_ASSEMBLER_NAME (fndecl, mangled_name);
1047 /* If the return type is a pointer, avoid alias issues by setting
1048 DECL_IS_MALLOC to nonzero. This means that the function should be
1049 treated as if it were a malloc, meaning it returns a pointer that
1050 is not an alias. */
1051 if (POINTER_TYPE_P (type))
1052 DECL_IS_MALLOC (fndecl) = 1;
1053
1054 /* Set the context of this decl. */
1055 if (0 && sym->ns && sym->ns->proc_name)
1056 {
1057 /* TODO: Add external decls to the appropriate scope. */
1058 DECL_CONTEXT (fndecl) = sym->ns->proc_name->backend_decl;
1059 }
1060 else
1061 {
f8d0aee5 1062 /* Global declaration, e.g. intrinsic subroutine. */
6de9cd9a
DN
1063 DECL_CONTEXT (fndecl) = NULL_TREE;
1064 }
1065
1066 DECL_EXTERNAL (fndecl) = 1;
1067
f8d0aee5 1068 /* This specifies if a function is globally addressable, i.e. it is
6de9cd9a
DN
1069 the opposite of declaring static in C. */
1070 TREE_PUBLIC (fndecl) = 1;
1071
1072 /* Set attributes for PURE functions. A call to PURE function in the
1073 Fortran 95 sense is both pure and without side effects in the C
1074 sense. */
1075 if (sym->attr.pure || sym->attr.elemental)
1076 {
b7e6a6b3
TS
1077 if (sym->attr.function)
1078 DECL_IS_PURE (fndecl) = 1;
1079 /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
1080 parameters and don't use alternate returns (is this
1081 allowed?). In that case, calls to them are meaningless, and
3d79abbd 1082 can be optimized away. See also in build_function_decl(). */
b7e6a6b3 1083 TREE_SIDE_EFFECTS (fndecl) = 0;
6de9cd9a
DN
1084 }
1085
fe58e076
TK
1086 /* Mark non-returning functions. */
1087 if (sym->attr.noreturn)
1088 TREE_THIS_VOLATILE(fndecl) = 1;
1089
6de9cd9a
DN
1090 sym->backend_decl = fndecl;
1091
1092 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1093 pushdecl_top_level (fndecl);
1094
1095 return fndecl;
1096}
1097
1098
1099/* Create a declaration for a procedure. For external functions (in the C
3d79abbd
PB
1100 sense) use gfc_get_extern_function_decl. HAS_ENTRIES is true if this is
1101 a master function with alternate entry points. */
6de9cd9a 1102
3d79abbd
PB
1103static void
1104build_function_decl (gfc_symbol * sym)
6de9cd9a 1105{
3d79abbd 1106 tree fndecl, type;
6de9cd9a 1107 symbol_attribute attr;
3d79abbd 1108 tree result_decl;
6de9cd9a
DN
1109 gfc_formal_arglist *f;
1110
6e45f57b
PB
1111 gcc_assert (!sym->backend_decl);
1112 gcc_assert (!sym->attr.external);
6de9cd9a 1113
c8cc8542
PB
1114 /* Set the line and filename. sym->declared_at seems to point to the
1115 last statement for subroutines, but it'll do for now. */
1116 gfc_set_backend_locus (&sym->declared_at);
1117
6de9cd9a 1118 /* Allow only one nesting level. Allow public declarations. */
6e45f57b 1119 gcc_assert (current_function_decl == NULL_TREE
6de9cd9a
DN
1120 || DECL_CONTEXT (current_function_decl) == NULL_TREE);
1121
1122 type = gfc_get_function_type (sym);
1123 fndecl = build_decl (FUNCTION_DECL, gfc_sym_identifier (sym), type);
1124
1125 /* Perform name mangling if this is a top level or module procedure. */
1126 if (current_function_decl == NULL_TREE)
1127 SET_DECL_ASSEMBLER_NAME (fndecl, gfc_sym_mangled_function_id (sym));
1128
1129 /* Figure out the return type of the declared function, and build a
f8d0aee5 1130 RESULT_DECL for it. If this is a subroutine with alternate
6de9cd9a
DN
1131 returns, build a RESULT_DECL for it. */
1132 attr = sym->attr;
1133
1134 result_decl = NULL_TREE;
1135 /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)). */
1136 if (attr.function)
1137 {
1138 if (gfc_return_by_reference (sym))
1139 type = void_type_node;
1140 else
1141 {
1142 if (sym->result != sym)
1143 result_decl = gfc_sym_identifier (sym->result);
1144
1145 type = TREE_TYPE (TREE_TYPE (fndecl));
1146 }
1147 }
1148 else
1149 {
1150 /* Look for alternate return placeholders. */
1151 int has_alternate_returns = 0;
1152 for (f = sym->formal; f; f = f->next)
1153 {
1154 if (f->sym == NULL)
1155 {
1156 has_alternate_returns = 1;
1157 break;
1158 }
1159 }
1160
1161 if (has_alternate_returns)
1162 type = integer_type_node;
1163 else
1164 type = void_type_node;
1165 }
1166
1167 result_decl = build_decl (RESULT_DECL, result_decl, type);
b785f485
RH
1168 DECL_ARTIFICIAL (result_decl) = 1;
1169 DECL_IGNORED_P (result_decl) = 1;
6de9cd9a
DN
1170 DECL_CONTEXT (result_decl) = fndecl;
1171 DECL_RESULT (fndecl) = result_decl;
1172
1173 /* Don't call layout_decl for a RESULT_DECL.
f8d0aee5 1174 layout_decl (result_decl, 0); */
6de9cd9a
DN
1175
1176 /* If the return type is a pointer, avoid alias issues by setting
1177 DECL_IS_MALLOC to nonzero. This means that the function should be
1178 treated as if it were a malloc, meaning it returns a pointer that
1179 is not an alias. */
1180 if (POINTER_TYPE_P (type))
1181 DECL_IS_MALLOC (fndecl) = 1;
1182
1183 /* Set up all attributes for the function. */
1184 DECL_CONTEXT (fndecl) = current_function_decl;
1185 DECL_EXTERNAL (fndecl) = 0;
1186
ae51017b 1187 /* This specifies if a function is globally visible, i.e. it is
472ca416 1188 the opposite of declaring static in C. */
3d79abbd
PB
1189 if (DECL_CONTEXT (fndecl) == NULL_TREE
1190 && !sym->attr.entry_master)
6de9cd9a
DN
1191 TREE_PUBLIC (fndecl) = 1;
1192
1193 /* TREE_STATIC means the function body is defined here. */
1d754240 1194 TREE_STATIC (fndecl) = 1;
6de9cd9a 1195
f8d0aee5 1196 /* Set attributes for PURE functions. A call to a PURE function in the
6de9cd9a
DN
1197 Fortran 95 sense is both pure and without side effects in the C
1198 sense. */
1199 if (attr.pure || attr.elemental)
1200 {
b7e6a6b3
TS
1201 /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
1202 including a alternate return. In that case it can also be
1f2959f0 1203 marked as PURE. See also in gfc_get_extern_function_decl(). */
b7e6a6b3
TS
1204 if (attr.function)
1205 DECL_IS_PURE (fndecl) = 1;
6de9cd9a
DN
1206 TREE_SIDE_EFFECTS (fndecl) = 0;
1207 }
1208
1209 /* Layout the function declaration and put it in the binding level
1210 of the current function. */
1d754240 1211 pushdecl (fndecl);
3d79abbd
PB
1212
1213 sym->backend_decl = fndecl;
1214}
1215
1216
1217/* Create the DECL_ARGUMENTS for a procedure. */
1218
1219static void
1220create_function_arglist (gfc_symbol * sym)
1221{
1222 tree fndecl;
1223 gfc_formal_arglist *f;
1224 tree typelist;
1225 tree arglist;
1226 tree length;
1227 tree type;
1228 tree parm;
1229
1230 fndecl = sym->backend_decl;
1231
1d754240
PB
1232 /* Build formal argument list. Make sure that their TREE_CONTEXT is
1233 the new FUNCTION_DECL node. */
1d754240
PB
1234 arglist = NULL_TREE;
1235 typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3d79abbd
PB
1236
1237 if (sym->attr.entry_master)
1238 {
1239 type = TREE_VALUE (typelist);
1240 parm = build_decl (PARM_DECL, get_identifier ("__entry"), type);
1241
1242 DECL_CONTEXT (parm) = fndecl;
1243 DECL_ARG_TYPE (parm) = type;
1244 TREE_READONLY (parm) = 1;
1245 gfc_finish_decl (parm, NULL_TREE);
1246
1247 arglist = chainon (arglist, parm);
1248 typelist = TREE_CHAIN (typelist);
1249 }
1250
1d754240 1251 if (gfc_return_by_reference (sym))
6de9cd9a 1252 {
1d754240
PB
1253 type = TREE_VALUE (typelist);
1254 parm = build_decl (PARM_DECL, get_identifier ("__result"), type);
6de9cd9a 1255
1d754240
PB
1256 DECL_CONTEXT (parm) = fndecl;
1257 DECL_ARG_TYPE (parm) = type;
1258 TREE_READONLY (parm) = 1;
ca0e9281 1259 DECL_ARTIFICIAL (parm) = 1;
1d754240 1260 gfc_finish_decl (parm, NULL_TREE);
6de9cd9a 1261
1d754240
PB
1262 arglist = chainon (arglist, parm);
1263 typelist = TREE_CHAIN (typelist);
6de9cd9a 1264
1d754240
PB
1265 if (sym->ts.type == BT_CHARACTER)
1266 {
1267 gfc_allocate_lang_decl (parm);
6de9cd9a 1268
1d754240
PB
1269 /* Length of character result. */
1270 type = TREE_VALUE (typelist);
6e45f57b 1271 gcc_assert (type == gfc_charlen_type_node);
6de9cd9a 1272
1d754240
PB
1273 length = build_decl (PARM_DECL,
1274 get_identifier (".__result"),
1275 type);
1276 if (!sym->ts.cl->length)
1277 {
1278 sym->ts.cl->backend_decl = length;
1279 TREE_USED (length) = 1;
6de9cd9a 1280 }
6e45f57b 1281 gcc_assert (TREE_CODE (length) == PARM_DECL);
1d754240
PB
1282 arglist = chainon (arglist, length);
1283 typelist = TREE_CHAIN (typelist);
1284 DECL_CONTEXT (length) = fndecl;
1285 DECL_ARG_TYPE (length) = type;
1286 TREE_READONLY (length) = 1;
ca0e9281 1287 DECL_ARTIFICIAL (length) = 1;
1d754240 1288 gfc_finish_decl (length, NULL_TREE);
6de9cd9a 1289 }
1d754240 1290 }
6de9cd9a 1291
1d754240
PB
1292 for (f = sym->formal; f; f = f->next)
1293 {
f8d0aee5 1294 if (f->sym != NULL) /* ignore alternate returns. */
6de9cd9a 1295 {
1d754240 1296 length = NULL_TREE;
6de9cd9a 1297
1d754240 1298 type = TREE_VALUE (typelist);
6de9cd9a 1299
1d754240
PB
1300 /* Build a the argument declaration. */
1301 parm = build_decl (PARM_DECL,
1302 gfc_sym_identifier (f->sym), type);
6de9cd9a 1303
1d754240
PB
1304 /* Fill in arg stuff. */
1305 DECL_CONTEXT (parm) = fndecl;
1306 DECL_ARG_TYPE (parm) = type;
1d754240
PB
1307 /* All implementation args are read-only. */
1308 TREE_READONLY (parm) = 1;
6de9cd9a 1309
1d754240 1310 gfc_finish_decl (parm, NULL_TREE);
6de9cd9a 1311
1d754240 1312 f->sym->backend_decl = parm;
6de9cd9a 1313
1d754240
PB
1314 arglist = chainon (arglist, parm);
1315 typelist = TREE_CHAIN (typelist);
1316 }
1317 }
6de9cd9a 1318
1d754240
PB
1319 /* Add the hidden string length parameters. */
1320 parm = arglist;
1321 for (f = sym->formal; f; f = f->next)
1322 {
1323 char name[GFC_MAX_SYMBOL_LEN + 2];
1324 /* Ignore alternate returns. */
1325 if (f->sym == NULL)
1326 continue;
6de9cd9a 1327
1d754240
PB
1328 if (f->sym->ts.type != BT_CHARACTER)
1329 continue;
6de9cd9a 1330
1d754240
PB
1331 parm = f->sym->backend_decl;
1332 type = TREE_VALUE (typelist);
6e45f57b 1333 gcc_assert (type == gfc_charlen_type_node);
6de9cd9a 1334
1d754240
PB
1335 strcpy (&name[1], f->sym->name);
1336 name[0] = '_';
1337 length = build_decl (PARM_DECL, get_identifier (name), type);
6de9cd9a 1338
1d754240
PB
1339 arglist = chainon (arglist, length);
1340 DECL_CONTEXT (length) = fndecl;
ca0e9281 1341 DECL_ARTIFICIAL (length) = 1;
1d754240
PB
1342 DECL_ARG_TYPE (length) = type;
1343 TREE_READONLY (length) = 1;
1344 gfc_finish_decl (length, NULL_TREE);
6de9cd9a 1345
1d754240 1346 /* TODO: Check string lengths when -fbounds-check. */
6de9cd9a 1347
1d754240
PB
1348 /* Use the passed value for assumed length variables. */
1349 if (!f->sym->ts.cl->length)
1350 {
1351 TREE_USED (length) = 1;
1352 if (!f->sym->ts.cl->backend_decl)
1353 f->sym->ts.cl->backend_decl = length;
1354 else
6de9cd9a 1355 {
1d754240
PB
1356 /* there is already another variable using this
1357 gfc_charlen node, build a new one for this variable
1358 and chain it into the list of gfc_charlens.
1359 This happens for e.g. in the case
1360 CHARACTER(*)::c1,c2
1361 since CHARACTER declarations on the same line share
1362 the same gfc_charlen node. */
1363 gfc_charlen *cl;
1364
1365 cl = gfc_get_charlen ();
1366 cl->backend_decl = length;
1367 cl->next = f->sym->ts.cl->next;
1368 f->sym->ts.cl->next = cl;
1369 f->sym->ts.cl = cl;
6de9cd9a 1370 }
6de9cd9a
DN
1371 }
1372
1d754240
PB
1373 parm = TREE_CHAIN (parm);
1374 typelist = TREE_CHAIN (typelist);
6de9cd9a 1375 }
1d754240 1376
6e45f57b 1377 gcc_assert (TREE_VALUE (typelist) == void_type_node);
1d754240 1378 DECL_ARGUMENTS (fndecl) = arglist;
3d79abbd 1379}
1d754240 1380
3d79abbd
PB
1381/* Convert FNDECL's code to GIMPLE and handle any nested functions. */
1382
1383static void
1384gfc_gimplify_function (tree fndecl)
1385{
1386 struct cgraph_node *cgn;
1387
1388 gimplify_function_tree (fndecl);
1389 dump_function (TDI_generic, fndecl);
1390
1391 /* Convert all nested functions to GIMPLE now. We do things in this order
1392 so that items like VLA sizes are expanded properly in the context of the
1393 correct function. */
1394 cgn = cgraph_node (fndecl);
1395 for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1396 gfc_gimplify_function (cgn->decl);
1397}
1398
1399
1400/* Do the setup necessary before generating the body of a function. */
1401
1402static void
1403trans_function_start (gfc_symbol * sym)
1404{
1405 tree fndecl;
1406
1407 fndecl = sym->backend_decl;
1408
f8d0aee5 1409 /* Let GCC know the current scope is this function. */
3d79abbd
PB
1410 current_function_decl = fndecl;
1411
f8d0aee5 1412 /* Let the world know what we're about to do. */
3d79abbd
PB
1413 announce_function (fndecl);
1414
1415 if (DECL_CONTEXT (fndecl) == NULL_TREE)
1416 {
f8d0aee5 1417 /* Create RTL for function declaration. */
3d79abbd
PB
1418 rest_of_decl_compilation (fndecl, 1, 0);
1419 }
1420
f8d0aee5 1421 /* Create RTL for function definition. */
3d79abbd
PB
1422 make_decl_rtl (fndecl);
1423
3d79abbd
PB
1424 init_function_start (fndecl);
1425
1426 /* Even though we're inside a function body, we still don't want to
1427 call expand_expr to calculate the size of a variable-sized array.
1428 We haven't necessarily assigned RTL to all variables yet, so it's
1429 not safe to try to expand expressions involving them. */
1430 cfun->x_dont_save_pending_sizes_p = 1;
1431
f8d0aee5 1432 /* function.c requires a push at the start of the function. */
3d79abbd
PB
1433 pushlevel (0);
1434}
1435
1436/* Create thunks for alternate entry points. */
1437
1438static void
1439build_entry_thunks (gfc_namespace * ns)
1440{
1441 gfc_formal_arglist *formal;
1442 gfc_formal_arglist *thunk_formal;
1443 gfc_entry_list *el;
1444 gfc_symbol *thunk_sym;
1445 stmtblock_t body;
1446 tree thunk_fndecl;
1447 tree args;
1448 tree string_args;
1449 tree tmp;
c8cc8542 1450 locus old_loc;
3d79abbd
PB
1451
1452 /* This should always be a toplevel function. */
6e45f57b 1453 gcc_assert (current_function_decl == NULL_TREE);
3d79abbd 1454
c8cc8542 1455 gfc_get_backend_locus (&old_loc);
3d79abbd
PB
1456 for (el = ns->entries; el; el = el->next)
1457 {
1458 thunk_sym = el->sym;
1459
1460 build_function_decl (thunk_sym);
1461 create_function_arglist (thunk_sym);
1462
1463 trans_function_start (thunk_sym);
1464
1465 thunk_fndecl = thunk_sym->backend_decl;
1466
1467 gfc_start_block (&body);
1468
f8d0aee5 1469 /* Pass extra parameter identifying this entry point. */
7d60be94 1470 tmp = build_int_cst (gfc_array_index_type, el->id);
3d79abbd
PB
1471 args = tree_cons (NULL_TREE, tmp, NULL_TREE);
1472 string_args = NULL_TREE;
1473
d198b59a
JJ
1474 if (thunk_sym->attr.function)
1475 {
1476 if (gfc_return_by_reference (ns->proc_name))
1477 {
1478 tree ref = DECL_ARGUMENTS (current_function_decl);
1479 args = tree_cons (NULL_TREE, ref, args);
1480 if (ns->proc_name->ts.type == BT_CHARACTER)
1481 args = tree_cons (NULL_TREE, TREE_CHAIN (ref),
1482 args);
1483 }
1484 }
1485
3d79abbd
PB
1486 for (formal = ns->proc_name->formal; formal; formal = formal->next)
1487 {
d198b59a
JJ
1488 /* Ignore alternate returns. */
1489 if (formal->sym == NULL)
1490 continue;
1491
3d79abbd
PB
1492 /* We don't have a clever way of identifying arguments, so resort to
1493 a brute-force search. */
1494 for (thunk_formal = thunk_sym->formal;
1495 thunk_formal;
1496 thunk_formal = thunk_formal->next)
1497 {
1498 if (thunk_formal->sym == formal->sym)
1499 break;
1500 }
1501
1502 if (thunk_formal)
1503 {
1504 /* Pass the argument. */
1505 args = tree_cons (NULL_TREE, thunk_formal->sym->backend_decl,
1506 args);
1507 if (formal->sym->ts.type == BT_CHARACTER)
1508 {
1509 tmp = thunk_formal->sym->ts.cl->backend_decl;
1510 string_args = tree_cons (NULL_TREE, tmp, string_args);
1511 }
1512 }
1513 else
1514 {
1515 /* Pass NULL for a missing argument. */
1516 args = tree_cons (NULL_TREE, null_pointer_node, args);
1517 if (formal->sym->ts.type == BT_CHARACTER)
1518 {
d7177ab2 1519 tmp = convert (gfc_charlen_type_node, integer_zero_node);
3d79abbd
PB
1520 string_args = tree_cons (NULL_TREE, tmp, string_args);
1521 }
1522 }
1523 }
1524
1525 /* Call the master function. */
1526 args = nreverse (args);
1527 args = chainon (args, nreverse (string_args));
1528 tmp = ns->proc_name->backend_decl;
1529 tmp = gfc_build_function_call (tmp, args);
d198b59a
JJ
1530 if (ns->proc_name->attr.mixed_entry_master)
1531 {
1532 tree union_decl, field;
1533 tree master_type = TREE_TYPE (ns->proc_name->backend_decl);
1534
1535 union_decl = build_decl (VAR_DECL, get_identifier ("__result"),
1536 TREE_TYPE (master_type));
1537 DECL_ARTIFICIAL (union_decl) = 1;
1538 DECL_EXTERNAL (union_decl) = 0;
1539 TREE_PUBLIC (union_decl) = 0;
1540 TREE_USED (union_decl) = 1;
1541 layout_decl (union_decl, 0);
1542 pushdecl (union_decl);
1543
1544 DECL_CONTEXT (union_decl) = current_function_decl;
1545 tmp = build2 (MODIFY_EXPR,
1546 TREE_TYPE (union_decl),
1547 union_decl, tmp);
1548 gfc_add_expr_to_block (&body, tmp);
1549
1550 for (field = TYPE_FIELDS (TREE_TYPE (union_decl));
1551 field; field = TREE_CHAIN (field))
1552 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
1553 thunk_sym->result->name) == 0)
1554 break;
1555 gcc_assert (field != NULL_TREE);
1556 tmp = build3 (COMPONENT_REF, TREE_TYPE (field), union_decl, field,
1557 NULL_TREE);
1558 tmp = build2 (MODIFY_EXPR,
1559 TREE_TYPE (DECL_RESULT (current_function_decl)),
1560 DECL_RESULT (current_function_decl), tmp);
1561 tmp = build1_v (RETURN_EXPR, tmp);
1562 }
1563 else if (TREE_TYPE (DECL_RESULT (current_function_decl))
1564 != void_type_node)
1565 {
1566 tmp = build2 (MODIFY_EXPR,
1567 TREE_TYPE (DECL_RESULT (current_function_decl)),
1568 DECL_RESULT (current_function_decl), tmp);
1569 tmp = build1_v (RETURN_EXPR, tmp);
1570 }
3d79abbd
PB
1571 gfc_add_expr_to_block (&body, tmp);
1572
1573 /* Finish off this function and send it for code generation. */
1574 DECL_SAVED_TREE (thunk_fndecl) = gfc_finish_block (&body);
1575 poplevel (1, 0, 1);
1576 BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl)) = thunk_fndecl;
1577
1578 /* Output the GENERIC tree. */
1579 dump_function (TDI_original, thunk_fndecl);
1580
1581 /* Store the end of the function, so that we get good line number
1582 info for the epilogue. */
1583 cfun->function_end_locus = input_location;
1584
1585 /* We're leaving the context of this function, so zap cfun.
1586 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
1587 tree_rest_of_compilation. */
1588 cfun = NULL;
1589
1590 current_function_decl = NULL_TREE;
1591
1592 gfc_gimplify_function (thunk_fndecl);
8f235343 1593 cgraph_finalize_function (thunk_fndecl, false);
3d79abbd
PB
1594
1595 /* We share the symbols in the formal argument list with other entry
1596 points and the master function. Clear them so that they are
1597 recreated for each function. */
1598 for (formal = thunk_sym->formal; formal; formal = formal->next)
d198b59a
JJ
1599 if (formal->sym != NULL) /* Ignore alternate returns. */
1600 {
1601 formal->sym->backend_decl = NULL_TREE;
1602 if (formal->sym->ts.type == BT_CHARACTER)
1603 formal->sym->ts.cl->backend_decl = NULL_TREE;
1604 }
1605
1606 if (thunk_sym->attr.function)
3d79abbd 1607 {
d198b59a
JJ
1608 if (thunk_sym->ts.type == BT_CHARACTER)
1609 thunk_sym->ts.cl->backend_decl = NULL_TREE;
1610 if (thunk_sym->result->ts.type == BT_CHARACTER)
1611 thunk_sym->result->ts.cl->backend_decl = NULL_TREE;
3d79abbd
PB
1612 }
1613 }
c8cc8542
PB
1614
1615 gfc_set_backend_locus (&old_loc);
3d79abbd
PB
1616}
1617
1618
1619/* Create a decl for a function, and create any thunks for alternate entry
1620 points. */
1621
1622void
1623gfc_create_function_decl (gfc_namespace * ns)
1624{
1625 /* Create a declaration for the master function. */
1626 build_function_decl (ns->proc_name);
1627
f8d0aee5 1628 /* Compile the entry thunks. */
3d79abbd
PB
1629 if (ns->entries)
1630 build_entry_thunks (ns);
1631
1632 /* Now create the read argument list. */
1633 create_function_arglist (ns->proc_name);
1634}
1635
6de9cd9a
DN
1636/* Return the decl used to hold the function return value. */
1637
1638tree
1639gfc_get_fake_result_decl (gfc_symbol * sym)
1640{
1641 tree decl;
1642 tree length;
1643
1644 char name[GFC_MAX_SYMBOL_LEN + 10];
1645
d198b59a
JJ
1646 if (sym
1647 && sym->ns->proc_name->backend_decl == current_function_decl
1648 && sym->ns->proc_name->attr.mixed_entry_master
1649 && sym != sym->ns->proc_name)
1650 {
1651 decl = gfc_get_fake_result_decl (sym->ns->proc_name);
1652 if (decl)
1653 {
1654 tree field;
1655
1656 for (field = TYPE_FIELDS (TREE_TYPE (decl));
1657 field; field = TREE_CHAIN (field))
1658 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
1659 sym->name) == 0)
1660 break;
1661
1662 gcc_assert (field != NULL_TREE);
1663 decl = build3 (COMPONENT_REF, TREE_TYPE (field), decl, field,
1664 NULL_TREE);
1665 }
1666 return decl;
1667 }
1668
6de9cd9a
DN
1669 if (current_fake_result_decl != NULL_TREE)
1670 return current_fake_result_decl;
1671
1672 /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
1673 sym is NULL. */
1674 if (!sym)
1675 return NULL_TREE;
1676
1677 if (sym->ts.type == BT_CHARACTER
1678 && !sym->ts.cl->backend_decl)
1679 {
1680 length = gfc_create_string_length (sym);
1681 gfc_finish_var_decl (length, sym);
1682 }
1683
1684 if (gfc_return_by_reference (sym))
1685 {
d198b59a
JJ
1686 decl = DECL_ARGUMENTS (current_function_decl);
1687
1688 if (sym->ns->proc_name->backend_decl == current_function_decl
1689 && sym->ns->proc_name->attr.entry_master)
1690 decl = TREE_CHAIN (decl);
6de9cd9a
DN
1691
1692 TREE_USED (decl) = 1;
1693 if (sym->as)
1694 decl = gfc_build_dummy_array_decl (sym, decl);
1695 }
1696 else
1697 {
1698 sprintf (name, "__result_%.20s",
1699 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1700
1701 decl = build_decl (VAR_DECL, get_identifier (name),
1702 TREE_TYPE (TREE_TYPE (current_function_decl)));
1703
1704 DECL_ARTIFICIAL (decl) = 1;
1705 DECL_EXTERNAL (decl) = 0;
1706 TREE_PUBLIC (decl) = 0;
1707 TREE_USED (decl) = 1;
1708
1709 layout_decl (decl, 0);
1710
1711 gfc_add_decl_to_function (decl);
1712 }
1713
1714 current_fake_result_decl = decl;
1715
1716 return decl;
1717}
1718
1719
1720/* Builds a function decl. The remaining parameters are the types of the
1721 function arguments. Negative nargs indicates a varargs function. */
1722
1723tree
1724gfc_build_library_function_decl (tree name, tree rettype, int nargs, ...)
1725{
1726 tree arglist;
1727 tree argtype;
1728 tree fntype;
1729 tree fndecl;
1730 va_list p;
1731 int n;
1732
1733 /* Library functions must be declared with global scope. */
6e45f57b 1734 gcc_assert (current_function_decl == NULL_TREE);
6de9cd9a
DN
1735
1736 va_start (p, nargs);
1737
1738
1739 /* Create a list of the argument types. */
1740 for (arglist = NULL_TREE, n = abs (nargs); n > 0; n--)
1741 {
1742 argtype = va_arg (p, tree);
1743 arglist = gfc_chainon_list (arglist, argtype);
1744 }
1745
1746 if (nargs >= 0)
1747 {
1748 /* Terminate the list. */
1749 arglist = gfc_chainon_list (arglist, void_type_node);
1750 }
1751
1752 /* Build the function type and decl. */
1753 fntype = build_function_type (rettype, arglist);
1754 fndecl = build_decl (FUNCTION_DECL, name, fntype);
1755
1756 /* Mark this decl as external. */
1757 DECL_EXTERNAL (fndecl) = 1;
1758 TREE_PUBLIC (fndecl) = 1;
1759
1760 va_end (p);
1761
1762 pushdecl (fndecl);
1763
0e6df31e 1764 rest_of_decl_compilation (fndecl, 1, 0);
6de9cd9a
DN
1765
1766 return fndecl;
1767}
1768
1769static void
1770gfc_build_intrinsic_function_decls (void)
1771{
e2cad04b
RH
1772 tree gfc_int4_type_node = gfc_get_int_type (4);
1773 tree gfc_int8_type_node = gfc_get_int_type (8);
644cb69f 1774 tree gfc_int16_type_node = gfc_get_int_type (16);
e2cad04b
RH
1775 tree gfc_logical4_type_node = gfc_get_logical_type (4);
1776 tree gfc_real4_type_node = gfc_get_real_type (4);
1777 tree gfc_real8_type_node = gfc_get_real_type (8);
644cb69f
FXC
1778 tree gfc_real10_type_node = gfc_get_real_type (10);
1779 tree gfc_real16_type_node = gfc_get_real_type (16);
e2cad04b
RH
1780 tree gfc_complex4_type_node = gfc_get_complex_type (4);
1781 tree gfc_complex8_type_node = gfc_get_complex_type (8);
644cb69f
FXC
1782 tree gfc_complex10_type_node = gfc_get_complex_type (10);
1783 tree gfc_complex16_type_node = gfc_get_complex_type (16);
25fc05eb 1784 tree gfc_c_int_type_node = gfc_get_int_type (gfc_c_int_kind);
e2cad04b 1785
6de9cd9a
DN
1786 /* String functions. */
1787 gfor_fndecl_copy_string =
1788 gfc_build_library_function_decl (get_identifier (PREFIX("copy_string")),
1789 void_type_node,
1790 4,
d7177ab2
TS
1791 gfc_charlen_type_node, pchar_type_node,
1792 gfc_charlen_type_node, pchar_type_node);
6de9cd9a
DN
1793
1794 gfor_fndecl_compare_string =
1795 gfc_build_library_function_decl (get_identifier (PREFIX("compare_string")),
1796 gfc_int4_type_node,
1797 4,
d7177ab2
TS
1798 gfc_charlen_type_node, pchar_type_node,
1799 gfc_charlen_type_node, pchar_type_node);
6de9cd9a
DN
1800
1801 gfor_fndecl_concat_string =
1802 gfc_build_library_function_decl (get_identifier (PREFIX("concat_string")),
1803 void_type_node,
1804 6,
d7177ab2
TS
1805 gfc_charlen_type_node, pchar_type_node,
1806 gfc_charlen_type_node, pchar_type_node,
1807 gfc_charlen_type_node, pchar_type_node);
6de9cd9a
DN
1808
1809 gfor_fndecl_string_len_trim =
1810 gfc_build_library_function_decl (get_identifier (PREFIX("string_len_trim")),
1811 gfc_int4_type_node,
d7177ab2 1812 2, gfc_charlen_type_node,
6de9cd9a
DN
1813 pchar_type_node);
1814
1815 gfor_fndecl_string_index =
1816 gfc_build_library_function_decl (get_identifier (PREFIX("string_index")),
1817 gfc_int4_type_node,
d7177ab2
TS
1818 5, gfc_charlen_type_node, pchar_type_node,
1819 gfc_charlen_type_node, pchar_type_node,
6de9cd9a
DN
1820 gfc_logical4_type_node);
1821
1822 gfor_fndecl_string_scan =
1823 gfc_build_library_function_decl (get_identifier (PREFIX("string_scan")),
1824 gfc_int4_type_node,
d7177ab2
TS
1825 5, gfc_charlen_type_node, pchar_type_node,
1826 gfc_charlen_type_node, pchar_type_node,
6de9cd9a
DN
1827 gfc_logical4_type_node);
1828
1829 gfor_fndecl_string_verify =
1830 gfc_build_library_function_decl (get_identifier (PREFIX("string_verify")),
1831 gfc_int4_type_node,
d7177ab2
TS
1832 5, gfc_charlen_type_node, pchar_type_node,
1833 gfc_charlen_type_node, pchar_type_node,
6de9cd9a
DN
1834 gfc_logical4_type_node);
1835
1836 gfor_fndecl_string_trim =
1837 gfc_build_library_function_decl (get_identifier (PREFIX("string_trim")),
1838 void_type_node,
1839 4,
d7177ab2 1840 build_pointer_type (gfc_charlen_type_node),
6de9cd9a 1841 ppvoid_type_node,
d7177ab2 1842 gfc_charlen_type_node,
6de9cd9a
DN
1843 pchar_type_node);
1844
1845 gfor_fndecl_string_repeat =
1846 gfc_build_library_function_decl (get_identifier (PREFIX("string_repeat")),
1847 void_type_node,
1848 4,
1849 pchar_type_node,
d7177ab2 1850 gfc_charlen_type_node,
6de9cd9a
DN
1851 pchar_type_node,
1852 gfc_int4_type_node);
1853
25fc05eb
FXC
1854 gfor_fndecl_ttynam =
1855 gfc_build_library_function_decl (get_identifier (PREFIX("ttynam")),
1856 void_type_node,
1857 3,
1858 pchar_type_node,
1859 gfc_charlen_type_node,
1860 gfc_c_int_type_node);
1861
6de9cd9a
DN
1862 gfor_fndecl_adjustl =
1863 gfc_build_library_function_decl (get_identifier (PREFIX("adjustl")),
1864 void_type_node,
1865 3,
1866 pchar_type_node,
d7177ab2 1867 gfc_charlen_type_node, pchar_type_node);
6de9cd9a
DN
1868
1869 gfor_fndecl_adjustr =
1870 gfc_build_library_function_decl (get_identifier (PREFIX("adjustr")),
1871 void_type_node,
1872 3,
1873 pchar_type_node,
d7177ab2 1874 gfc_charlen_type_node, pchar_type_node);
6de9cd9a
DN
1875
1876 gfor_fndecl_si_kind =
1877 gfc_build_library_function_decl (get_identifier ("selected_int_kind"),
1878 gfc_int4_type_node,
1879 1,
1880 pvoid_type_node);
1881
1882 gfor_fndecl_sr_kind =
1883 gfc_build_library_function_decl (get_identifier ("selected_real_kind"),
1884 gfc_int4_type_node,
1885 2, pvoid_type_node,
1886 pvoid_type_node);
1887
6de9cd9a 1888 /* Power functions. */
5b200ac2 1889 {
644cb69f
FXC
1890 tree ctype, rtype, itype, jtype;
1891 int rkind, ikind, jkind;
1892#define NIKINDS 3
1893#define NRKINDS 4
1894 static int ikinds[NIKINDS] = {4, 8, 16};
1895 static int rkinds[NRKINDS] = {4, 8, 10, 16};
1896 char name[PREFIX_LEN + 12]; /* _gfortran_pow_?n_?n */
1897
1898 for (ikind=0; ikind < NIKINDS; ikind++)
5b200ac2 1899 {
644cb69f
FXC
1900 itype = gfc_get_int_type (ikinds[ikind]);
1901
1902 for (jkind=0; jkind < NIKINDS; jkind++)
1903 {
1904 jtype = gfc_get_int_type (ikinds[jkind]);
1905 if (itype && jtype)
1906 {
1907 sprintf(name, PREFIX("pow_i%d_i%d"), ikinds[ikind],
1908 ikinds[jkind]);
1909 gfor_fndecl_math_powi[jkind][ikind].integer =
1910 gfc_build_library_function_decl (get_identifier (name),
1911 jtype, 2, jtype, itype);
1912 }
1913 }
1914
1915 for (rkind = 0; rkind < NRKINDS; rkind ++)
5b200ac2 1916 {
644cb69f
FXC
1917 rtype = gfc_get_real_type (rkinds[rkind]);
1918 if (rtype && itype)
1919 {
1920 sprintf(name, PREFIX("pow_r%d_i%d"), rkinds[rkind],
1921 ikinds[ikind]);
1922 gfor_fndecl_math_powi[rkind][ikind].real =
1923 gfc_build_library_function_decl (get_identifier (name),
1924 rtype, 2, rtype, itype);
1925 }
1926
1927 ctype = gfc_get_complex_type (rkinds[rkind]);
1928 if (ctype && itype)
1929 {
1930 sprintf(name, PREFIX("pow_c%d_i%d"), rkinds[rkind],
1931 ikinds[ikind]);
1932 gfor_fndecl_math_powi[rkind][ikind].cmplx =
1933 gfc_build_library_function_decl (get_identifier (name),
1934 ctype, 2,ctype, itype);
1935 }
5b200ac2
FW
1936 }
1937 }
644cb69f
FXC
1938#undef NIKINDS
1939#undef NRKINDS
5b200ac2
FW
1940 }
1941
6de9cd9a
DN
1942 gfor_fndecl_math_cpowf =
1943 gfc_build_library_function_decl (get_identifier ("cpowf"),
1944 gfc_complex4_type_node,
1945 1, gfc_complex4_type_node);
1946 gfor_fndecl_math_cpow =
1947 gfc_build_library_function_decl (get_identifier ("cpow"),
1948 gfc_complex8_type_node,
1949 1, gfc_complex8_type_node);
644cb69f
FXC
1950 if (gfc_complex10_type_node)
1951 gfor_fndecl_math_cpowl10 =
1952 gfc_build_library_function_decl (get_identifier ("cpowl"),
1953 gfc_complex10_type_node, 1,
1954 gfc_complex10_type_node);
1955 if (gfc_complex16_type_node)
1956 gfor_fndecl_math_cpowl16 =
1957 gfc_build_library_function_decl (get_identifier ("cpowl"),
1958 gfc_complex16_type_node, 1,
1959 gfc_complex16_type_node);
1960
6de9cd9a
DN
1961 gfor_fndecl_math_ishftc4 =
1962 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc4")),
1963 gfc_int4_type_node,
1964 3, gfc_int4_type_node,
1965 gfc_int4_type_node, gfc_int4_type_node);
1966 gfor_fndecl_math_ishftc8 =
1967 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc8")),
1968 gfc_int8_type_node,
1969 3, gfc_int8_type_node,
644cb69f
FXC
1970 gfc_int4_type_node, gfc_int4_type_node);
1971 if (gfc_int16_type_node)
1972 gfor_fndecl_math_ishftc16 =
1973 gfc_build_library_function_decl (get_identifier (PREFIX("ishftc16")),
1974 gfc_int16_type_node, 3,
1975 gfc_int16_type_node,
1976 gfc_int4_type_node,
1977 gfc_int4_type_node);
1978
6de9cd9a
DN
1979 gfor_fndecl_math_exponent4 =
1980 gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r4")),
1981 gfc_int4_type_node,
1982 1, gfc_real4_type_node);
1983 gfor_fndecl_math_exponent8 =
1984 gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r8")),
1985 gfc_int4_type_node,
1986 1, gfc_real8_type_node);
644cb69f
FXC
1987 if (gfc_real10_type_node)
1988 gfor_fndecl_math_exponent10 =
1989 gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r10")),
1990 gfc_int4_type_node, 1,
1991 gfc_real10_type_node);
1992 if (gfc_real16_type_node)
1993 gfor_fndecl_math_exponent16 =
1994 gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r16")),
1995 gfc_int4_type_node, 1,
1996 gfc_real16_type_node);
6de9cd9a
DN
1997
1998 /* Other functions. */
1999 gfor_fndecl_size0 =
2000 gfc_build_library_function_decl (get_identifier (PREFIX("size0")),
2001 gfc_array_index_type,
2002 1, pvoid_type_node);
2003 gfor_fndecl_size1 =
2004 gfc_build_library_function_decl (get_identifier (PREFIX("size1")),
2005 gfc_array_index_type,
2006 2, pvoid_type_node,
2007 gfc_array_index_type);
b41b2534
JB
2008
2009 gfor_fndecl_iargc =
2010 gfc_build_library_function_decl (get_identifier (PREFIX ("iargc")),
2011 gfc_int4_type_node,
2012 0);
6de9cd9a
DN
2013}
2014
2015
2016/* Make prototypes for runtime library functions. */
2017
2018void
2019gfc_build_builtin_function_decls (void)
2020{
944b8b35 2021 tree gfc_c_int_type_node = gfc_get_int_type (gfc_c_int_kind);
e2cad04b
RH
2022 tree gfc_int4_type_node = gfc_get_int_type (4);
2023 tree gfc_int8_type_node = gfc_get_int_type (8);
2024 tree gfc_logical4_type_node = gfc_get_logical_type (4);
364667a1 2025 tree gfc_pint4_type_node = build_pointer_type (gfc_int4_type_node);
e2cad04b 2026
16275f18 2027 /* Treat these two internal malloc wrappers as malloc. */
6de9cd9a
DN
2028 gfor_fndecl_internal_malloc =
2029 gfc_build_library_function_decl (get_identifier (PREFIX("internal_malloc")),
2030 pvoid_type_node, 1, gfc_int4_type_node);
16275f18 2031 DECL_IS_MALLOC (gfor_fndecl_internal_malloc) = 1;
6de9cd9a
DN
2032
2033 gfor_fndecl_internal_malloc64 =
2034 gfc_build_library_function_decl (get_identifier
2035 (PREFIX("internal_malloc64")),
2036 pvoid_type_node, 1, gfc_int8_type_node);
16275f18 2037 DECL_IS_MALLOC (gfor_fndecl_internal_malloc64) = 1;
6de9cd9a 2038
ec25720b
RS
2039 gfor_fndecl_internal_realloc =
2040 gfc_build_library_function_decl (get_identifier
2041 (PREFIX("internal_realloc")),
2042 pvoid_type_node, 2, pvoid_type_node,
2043 gfc_int4_type_node);
2044
2045 gfor_fndecl_internal_realloc64 =
2046 gfc_build_library_function_decl (get_identifier
2047 (PREFIX("internal_realloc64")),
2048 pvoid_type_node, 2, pvoid_type_node,
2049 gfc_int8_type_node);
2050
6de9cd9a
DN
2051 gfor_fndecl_internal_free =
2052 gfc_build_library_function_decl (get_identifier (PREFIX("internal_free")),
2053 void_type_node, 1, pvoid_type_node);
2054
2055 gfor_fndecl_allocate =
2056 gfc_build_library_function_decl (get_identifier (PREFIX("allocate")),
2057 void_type_node, 2, ppvoid_type_node,
2058 gfc_int4_type_node);
2059
2060 gfor_fndecl_allocate64 =
2061 gfc_build_library_function_decl (get_identifier (PREFIX("allocate64")),
2062 void_type_node, 2, ppvoid_type_node,
2063 gfc_int8_type_node);
2064
2065 gfor_fndecl_deallocate =
2066 gfc_build_library_function_decl (get_identifier (PREFIX("deallocate")),
364667a1
SK
2067 void_type_node, 2, ppvoid_type_node,
2068 gfc_pint4_type_node);
6de9cd9a
DN
2069
2070 gfor_fndecl_stop_numeric =
2071 gfc_build_library_function_decl (get_identifier (PREFIX("stop_numeric")),
2072 void_type_node, 1, gfc_int4_type_node);
2073
eed61baa
TK
2074 /* Stop doesn't return. */
2075 TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric) = 1;
2076
6de9cd9a
DN
2077 gfor_fndecl_stop_string =
2078 gfc_build_library_function_decl (get_identifier (PREFIX("stop_string")),
2079 void_type_node, 2, pchar_type_node,
2080 gfc_int4_type_node);
eed61baa
TK
2081 /* Stop doesn't return. */
2082 TREE_THIS_VOLATILE (gfor_fndecl_stop_string) = 1;
6de9cd9a
DN
2083
2084 gfor_fndecl_pause_numeric =
2085 gfc_build_library_function_decl (get_identifier (PREFIX("pause_numeric")),
2086 void_type_node, 1, gfc_int4_type_node);
2087
2088 gfor_fndecl_pause_string =
2089 gfc_build_library_function_decl (get_identifier (PREFIX("pause_string")),
2090 void_type_node, 2, pchar_type_node,
2091 gfc_int4_type_node);
2092
2093 gfor_fndecl_select_string =
2094 gfc_build_library_function_decl (get_identifier (PREFIX("select_string")),
2095 pvoid_type_node, 0);
2096
2097 gfor_fndecl_runtime_error =
2098 gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error")),
2099 void_type_node,
2100 3,
2101 pchar_type_node, pchar_type_node,
2102 gfc_int4_type_node);
16275f18
SB
2103 /* The runtime_error function does not return. */
2104 TREE_THIS_VOLATILE (gfor_fndecl_runtime_error) = 1;
6de9cd9a 2105
944b8b35
FXC
2106 gfor_fndecl_set_fpe =
2107 gfc_build_library_function_decl (get_identifier (PREFIX("set_fpe")),
2108 void_type_node, 1, gfc_c_int_type_node);
2109
8b67b708
FXC
2110 gfor_fndecl_set_std =
2111 gfc_build_library_function_decl (get_identifier (PREFIX("set_std")),
2112 void_type_node,
2113 2,
2114 gfc_int4_type_node,
2115 gfc_int4_type_node);
2116
6de9cd9a
DN
2117 gfor_fndecl_in_pack = gfc_build_library_function_decl (
2118 get_identifier (PREFIX("internal_pack")),
2119 pvoid_type_node, 1, pvoid_type_node);
2120
2121 gfor_fndecl_in_unpack = gfc_build_library_function_decl (
2122 get_identifier (PREFIX("internal_unpack")),
2123 pvoid_type_node, 1, pvoid_type_node);
2124
2125 gfor_fndecl_associated =
2126 gfc_build_library_function_decl (
2127 get_identifier (PREFIX("associated")),
2128 gfc_logical4_type_node,
2129 2,
2130 ppvoid_type_node,
2131 ppvoid_type_node);
2132
2133 gfc_build_intrinsic_function_decls ();
2134 gfc_build_intrinsic_lib_fndecls ();
2135 gfc_build_io_library_fndecls ();
2136}
2137
2138
1f2959f0 2139/* Evaluate the length of dummy character variables. */
6de9cd9a
DN
2140
2141static tree
2142gfc_trans_dummy_character (gfc_charlen * cl, tree fnbody)
2143{
2144 stmtblock_t body;
2145
2146 gfc_finish_decl (cl->backend_decl, NULL_TREE);
2147
2148 gfc_start_block (&body);
2149
2150 /* Evaluate the string length expression. */
2151 gfc_trans_init_string_length (cl, &body);
2152
2153 gfc_add_expr_to_block (&body, fnbody);
2154 return gfc_finish_block (&body);
2155}
2156
2157
2158/* Allocate and cleanup an automatic character variable. */
2159
2160static tree
2161gfc_trans_auto_character_variable (gfc_symbol * sym, tree fnbody)
2162{
2163 stmtblock_t body;
2164 tree decl;
6de9cd9a
DN
2165 tree tmp;
2166
6e45f57b
PB
2167 gcc_assert (sym->backend_decl);
2168 gcc_assert (sym->ts.cl && sym->ts.cl->length);
6de9cd9a
DN
2169
2170 gfc_start_block (&body);
2171
2172 /* Evaluate the string length expression. */
2173 gfc_trans_init_string_length (sym->ts.cl, &body);
2174
2175 decl = sym->backend_decl;
2176
1a186ec5 2177 /* Emit a DECL_EXPR for this variable, which will cause the
4ab2db93 2178 gimplifier to allocate storage, and all that good stuff. */
923ab88c 2179 tmp = build1 (DECL_EXPR, TREE_TYPE (decl), decl);
6de9cd9a 2180 gfc_add_expr_to_block (&body, tmp);
1a186ec5 2181
6de9cd9a
DN
2182 gfc_add_expr_to_block (&body, fnbody);
2183 return gfc_finish_block (&body);
2184}
2185
910450c1
FW
2186/* Set the initial value of ASSIGN statement auxiliary variable explicitly. */
2187
2188static tree
2189gfc_trans_assign_aux_var (gfc_symbol * sym, tree fnbody)
2190{
2191 stmtblock_t body;
2192
2193 gcc_assert (sym->backend_decl);
2194 gfc_start_block (&body);
2195
2196 /* Set the initial value to length. See the comments in
2197 function gfc_add_assign_aux_vars in this file. */
2198 gfc_add_modify_expr (&body, GFC_DECL_STRING_LEN (sym->backend_decl),
2199 build_int_cst (NULL_TREE, -2));
2200
2201 gfc_add_expr_to_block (&body, fnbody);
2202 return gfc_finish_block (&body);
2203}
2204
6de9cd9a
DN
2205
2206/* Generate function entry and exit code, and add it to the function body.
2207 This includes:
f8d0aee5 2208 Allocation and initialization of array variables.
6de9cd9a 2209 Allocation of character string variables.
910450c1
FW
2210 Initialization and possibly repacking of dummy arrays.
2211 Initialization of ASSIGN statement auxiliary variable. */
6de9cd9a
DN
2212
2213static tree
2214gfc_trans_deferred_vars (gfc_symbol * proc_sym, tree fnbody)
2215{
2216 locus loc;
2217 gfc_symbol *sym;
2218
2219 /* Deal with implicit return variables. Explicit return variables will
2220 already have been added. */
2221 if (gfc_return_by_reference (proc_sym) && proc_sym->result == proc_sym)
2222 {
2223 if (!current_fake_result_decl)
2224 {
d198b59a
JJ
2225 gfc_entry_list *el = NULL;
2226 if (proc_sym->attr.entry_master)
2227 {
2228 for (el = proc_sym->ns->entries; el; el = el->next)
2229 if (el->sym != el->sym->result)
2230 break;
2231 }
2232 if (el == NULL)
2233 warning (0, "Function does not return a value");
6de9cd9a 2234 }
d198b59a 2235 else if (proc_sym->as)
6de9cd9a
DN
2236 {
2237 fnbody = gfc_trans_dummy_array_bias (proc_sym,
2238 current_fake_result_decl,
2239 fnbody);
2240 }
2241 else if (proc_sym->ts.type == BT_CHARACTER)
2242 {
2243 if (TREE_CODE (proc_sym->ts.cl->backend_decl) == VAR_DECL)
2244 fnbody = gfc_trans_dummy_character (proc_sym->ts.cl, fnbody);
2245 }
2246 else
973ff4c0
TS
2247 gcc_assert (gfc_option.flag_f2c
2248 && proc_sym->ts.type == BT_COMPLEX);
6de9cd9a
DN
2249 }
2250
2251 for (sym = proc_sym->tlink; sym != proc_sym; sym = sym->tlink)
2252 {
2253 if (sym->attr.dimension)
2254 {
2255 switch (sym->as->type)
2256 {
2257 case AS_EXPLICIT:
2258 if (sym->attr.dummy || sym->attr.result)
2259 fnbody =
2260 gfc_trans_dummy_array_bias (sym, sym->backend_decl, fnbody);
2261 else if (sym->attr.pointer || sym->attr.allocatable)
2262 {
2263 if (TREE_STATIC (sym->backend_decl))
2264 gfc_trans_static_array_pointer (sym);
2265 else
2266 fnbody = gfc_trans_deferred_array (sym, fnbody);
2267 }
2268 else
2269 {
2270 gfc_get_backend_locus (&loc);
2271 gfc_set_backend_locus (&sym->declared_at);
2272 fnbody = gfc_trans_auto_array_allocation (sym->backend_decl,
2273 sym, fnbody);
2274 gfc_set_backend_locus (&loc);
2275 }
2276 break;
2277
2278 case AS_ASSUMED_SIZE:
2279 /* Must be a dummy parameter. */
6e45f57b 2280 gcc_assert (sym->attr.dummy);
6de9cd9a
DN
2281
2282 /* We should always pass assumed size arrays the g77 way. */
6de9cd9a
DN
2283 fnbody = gfc_trans_g77_array (sym, fnbody);
2284 break;
2285
2286 case AS_ASSUMED_SHAPE:
2287 /* Must be a dummy parameter. */
6e45f57b 2288 gcc_assert (sym->attr.dummy);
6de9cd9a
DN
2289
2290 fnbody = gfc_trans_dummy_array_bias (sym, sym->backend_decl,
2291 fnbody);
2292 break;
2293
2294 case AS_DEFERRED:
2295 fnbody = gfc_trans_deferred_array (sym, fnbody);
2296 break;
2297
2298 default:
6e45f57b 2299 gcc_unreachable ();
6de9cd9a
DN
2300 }
2301 }
2302 else if (sym->ts.type == BT_CHARACTER)
2303 {
2304 gfc_get_backend_locus (&loc);
2305 gfc_set_backend_locus (&sym->declared_at);
2306 if (sym->attr.dummy || sym->attr.result)
2307 fnbody = gfc_trans_dummy_character (sym->ts.cl, fnbody);
2308 else
2309 fnbody = gfc_trans_auto_character_variable (sym, fnbody);
2310 gfc_set_backend_locus (&loc);
2311 }
910450c1
FW
2312 else if (sym->attr.assign)
2313 {
2314 gfc_get_backend_locus (&loc);
2315 gfc_set_backend_locus (&sym->declared_at);
2316 fnbody = gfc_trans_assign_aux_var (sym, fnbody);
2317 gfc_set_backend_locus (&loc);
2318 }
6de9cd9a 2319 else
6e45f57b 2320 gcc_unreachable ();
6de9cd9a
DN
2321 }
2322
2323 return fnbody;
2324}
2325
2326
2327/* Output an initialized decl for a module variable. */
2328
2329static void
2330gfc_create_module_variable (gfc_symbol * sym)
2331{
2332 tree decl;
6de9cd9a
DN
2333
2334 /* Only output symbols from this module. */
2335 if (sym->ns != module_namespace)
2336 {
2337 /* I don't think this should ever happen. */
2338 internal_error ("module symbol %s in wrong namespace", sym->name);
2339 }
2340
e7dc5b4f 2341 /* Only output variables and array valued parameters. */
6de9cd9a
DN
2342 if (sym->attr.flavor != FL_VARIABLE
2343 && (sym->attr.flavor != FL_PARAMETER || sym->attr.dimension == 0))
2344 return;
2345
9cbf8b41
TS
2346 /* Don't generate variables from other modules. Variables from
2347 COMMONs will already have been generated. */
2348 if (sym->attr.use_assoc || sym->attr.in_common)
6de9cd9a
DN
2349 return;
2350
30aabb86
PT
2351 /* Equivalenced variables arrive here after creation. */
2352 if (sym->backend_decl && sym->equiv_built)
2353 return;
2354
6de9cd9a
DN
2355 if (sym->backend_decl)
2356 internal_error ("backend decl for module variable %s already exists",
2357 sym->name);
2358
2359 /* We always want module variables to be created. */
2360 sym->attr.referenced = 1;
2361 /* Create the decl. */
2362 decl = gfc_get_symbol_decl (sym);
2363
6de9cd9a
DN
2364 /* Create the variable. */
2365 pushdecl (decl);
0e6df31e 2366 rest_of_decl_compilation (decl, 1, 0);
6de9cd9a
DN
2367
2368 /* Also add length of strings. */
2369 if (sym->ts.type == BT_CHARACTER)
2370 {
2371 tree length;
2372
2373 length = sym->ts.cl->backend_decl;
2374 if (!INTEGER_CST_P (length))
2375 {
2376 pushdecl (length);
0e6df31e 2377 rest_of_decl_compilation (length, 1, 0);
6de9cd9a
DN
2378 }
2379 }
2380}
2381
2382
2383/* Generate all the required code for module variables. */
2384
2385void
2386gfc_generate_module_vars (gfc_namespace * ns)
2387{
2388 module_namespace = ns;
2389
472ca416 2390 /* Check if the frontend left the namespace in a reasonable state. */
6e45f57b 2391 gcc_assert (ns->proc_name && !ns->proc_name->tlink);
6de9cd9a 2392
9cbf8b41
TS
2393 /* Generate COMMON blocks. */
2394 gfc_trans_common (ns);
2395
472ca416 2396 /* Create decls for all the module variables. */
6de9cd9a
DN
2397 gfc_traverse_ns (ns, gfc_create_module_variable);
2398}
2399
2400static void
2401gfc_generate_contained_functions (gfc_namespace * parent)
2402{
2403 gfc_namespace *ns;
2404
2405 /* We create all the prototypes before generating any code. */
2406 for (ns = parent->contained; ns; ns = ns->sibling)
2407 {
2408 /* Skip namespaces from used modules. */
2409 if (ns->parent != parent)
2410 continue;
2411
3d79abbd 2412 gfc_create_function_decl (ns);
6de9cd9a
DN
2413 }
2414
2415 for (ns = parent->contained; ns; ns = ns->sibling)
2416 {
2417 /* Skip namespaces from used modules. */
2418 if (ns->parent != parent)
2419 continue;
2420
2421 gfc_generate_function_code (ns);
2422 }
2423}
2424
2425
2426/* Generate decls for all local variables. We do this to ensure correct
2427 handling of expressions which only appear in the specification of
2428 other functions. */
2429
2430static void
2431generate_local_decl (gfc_symbol * sym)
2432{
2433 if (sym->attr.flavor == FL_VARIABLE)
2434 {
6de9cd9a
DN
2435 if (sym->attr.referenced)
2436 gfc_get_symbol_decl (sym);
19e4c59d 2437 else if (sym->attr.dummy && warn_unused_parameter)
d4ee4d25 2438 warning (0, "unused parameter %qs", sym->name);
f8d0aee5 2439 /* Warn for unused variables, but not if they're inside a common
19e4c59d 2440 block or are use-associated. */
ce738b86
TS
2441 else if (warn_unused_variable
2442 && !(sym->attr.in_common || sym->attr.use_assoc))
d4ee4d25 2443 warning (0, "unused variable %qs", sym->name);
6de9cd9a
DN
2444 }
2445}
2446
2447static void
2448generate_local_vars (gfc_namespace * ns)
2449{
2450 gfc_traverse_ns (ns, generate_local_decl);
2451}
2452
2453
3d79abbd
PB
2454/* Generate a switch statement to jump to the correct entry point. Also
2455 creates the label decls for the entry points. */
6de9cd9a 2456
3d79abbd
PB
2457static tree
2458gfc_trans_entry_master_switch (gfc_entry_list * el)
6de9cd9a 2459{
3d79abbd
PB
2460 stmtblock_t block;
2461 tree label;
2462 tree tmp;
2463 tree val;
6de9cd9a 2464
3d79abbd
PB
2465 gfc_init_block (&block);
2466 for (; el; el = el->next)
2467 {
2468 /* Add the case label. */
c006df4e 2469 label = gfc_build_label_decl (NULL_TREE);
7d60be94 2470 val = build_int_cst (gfc_array_index_type, el->id);
923ab88c 2471 tmp = build3_v (CASE_LABEL_EXPR, val, NULL_TREE, label);
3d79abbd
PB
2472 gfc_add_expr_to_block (&block, tmp);
2473
2474 /* And jump to the actual entry point. */
2475 label = gfc_build_label_decl (NULL_TREE);
3d79abbd
PB
2476 tmp = build1_v (GOTO_EXPR, label);
2477 gfc_add_expr_to_block (&block, tmp);
2478
2479 /* Save the label decl. */
2480 el->label = label;
2481 }
2482 tmp = gfc_finish_block (&block);
2483 /* The first argument selects the entry point. */
2484 val = DECL_ARGUMENTS (current_function_decl);
923ab88c 2485 tmp = build3_v (SWITCH_EXPR, val, tmp, NULL_TREE);
3d79abbd 2486 return tmp;
6de9cd9a
DN
2487}
2488
44de5aeb 2489
6de9cd9a
DN
2490/* Generate code for a function. */
2491
2492void
2493gfc_generate_function_code (gfc_namespace * ns)
2494{
2495 tree fndecl;
2496 tree old_context;
2497 tree decl;
2498 tree tmp;
2499 stmtblock_t block;
2500 stmtblock_t body;
2501 tree result;
2502 gfc_symbol *sym;
2503
2504 sym = ns->proc_name;
3d79abbd 2505
6de9cd9a 2506 /* Check that the frontend isn't still using this. */
6e45f57b 2507 gcc_assert (sym->tlink == NULL);
6de9cd9a
DN
2508 sym->tlink = sym;
2509
2510 /* Create the declaration for functions with global scope. */
2511 if (!sym->backend_decl)
3d79abbd 2512 gfc_create_function_decl (ns);
6de9cd9a
DN
2513
2514 fndecl = sym->backend_decl;
2515 old_context = current_function_decl;
2516
2517 if (old_context)
2518 {
2519 push_function_context ();
2520 saved_parent_function_decls = saved_function_decls;
2521 saved_function_decls = NULL_TREE;
2522 }
2523
3d79abbd 2524 trans_function_start (sym);
6de9cd9a
DN
2525
2526 /* Will be created as needed. */
2527 current_fake_result_decl = NULL_TREE;
2528
6de9cd9a
DN
2529 gfc_start_block (&block);
2530
d198b59a
JJ
2531 if (ns->entries && ns->proc_name->ts.type == BT_CHARACTER)
2532 {
2533 /* Copy length backend_decls to all entry point result
2534 symbols. */
2535 gfc_entry_list *el;
2536 tree backend_decl;
2537
2538 gfc_conv_const_charlen (ns->proc_name->ts.cl);
2539 backend_decl = ns->proc_name->result->ts.cl->backend_decl;
2540 for (el = ns->entries; el; el = el->next)
2541 el->sym->result->ts.cl->backend_decl = backend_decl;
2542 }
2543
6de9cd9a
DN
2544 /* Translate COMMON blocks. */
2545 gfc_trans_common (ns);
2546
30aabb86
PT
2547 gfc_generate_contained_functions (ns);
2548
6de9cd9a
DN
2549 generate_local_vars (ns);
2550
2551 current_function_return_label = NULL;
2552
2553 /* Now generate the code for the body of this function. */
2554 gfc_init_block (&body);
2555
8b67b708
FXC
2556 /* If this is the main program and we compile with -pedantic, add a call
2557 to set_std to set up the runtime library Fortran language standard
2558 parameters. */
2559 if (sym->attr.is_main_program && pedantic)
2560 {
2561 tree arglist, gfc_int4_type_node;
2562
2563 gfc_int4_type_node = gfc_get_int_type (4);
2564 arglist = gfc_chainon_list (NULL_TREE,
2565 build_int_cst (gfc_int4_type_node,
2566 gfc_option.warn_std));
2567 arglist = gfc_chainon_list (arglist,
2568 build_int_cst (gfc_int4_type_node,
2569 gfc_option.allow_std));
2570 tmp = gfc_build_function_call (gfor_fndecl_set_std, arglist);
2571 gfc_add_expr_to_block (&body, tmp);
2572 }
2573
944b8b35
FXC
2574 /* If this is the main program and a -ffpe-trap option was provided,
2575 add a call to set_fpe so that the library will raise a FPE when
2576 needed. */
2577 if (sym->attr.is_main_program && gfc_option.fpe != 0)
2578 {
2579 tree arglist, gfc_c_int_type_node;
2580
2581 gfc_c_int_type_node = gfc_get_int_type (gfc_c_int_kind);
2582 arglist = gfc_chainon_list (NULL_TREE,
2583 build_int_cst (gfc_c_int_type_node,
2584 gfc_option.fpe));
2585 tmp = gfc_build_function_call (gfor_fndecl_set_fpe, arglist);
2586 gfc_add_expr_to_block (&body, tmp);
2587 }
2588
6de9cd9a
DN
2589 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node
2590 && sym->attr.subroutine)
2591 {
2592 tree alternate_return;
2593 alternate_return = gfc_get_fake_result_decl (sym);
2594 gfc_add_modify_expr (&body, alternate_return, integer_zero_node);
2595 }
2596
3d79abbd
PB
2597 if (ns->entries)
2598 {
2599 /* Jump to the correct entry point. */
2600 tmp = gfc_trans_entry_master_switch (ns->entries);
2601 gfc_add_expr_to_block (&body, tmp);
2602 }
2603
6de9cd9a
DN
2604 tmp = gfc_trans_code (ns->code);
2605 gfc_add_expr_to_block (&body, tmp);
2606
2607 /* Add a return label if needed. */
2608 if (current_function_return_label)
2609 {
2610 tmp = build1_v (LABEL_EXPR, current_function_return_label);
2611 gfc_add_expr_to_block (&body, tmp);
2612 }
2613
2614 tmp = gfc_finish_block (&body);
2615 /* Add code to create and cleanup arrays. */
2616 tmp = gfc_trans_deferred_vars (sym, tmp);
2617 gfc_add_expr_to_block (&block, tmp);
2618
2619 if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node)
2620 {
19e4c59d 2621 if (sym->attr.subroutine || sym == sym->result)
6de9cd9a
DN
2622 {
2623 result = current_fake_result_decl;
2624 current_fake_result_decl = NULL_TREE;
2625 }
2626 else
2627 result = sym->result->backend_decl;
2628
2629 if (result == NULL_TREE)
d4ee4d25 2630 warning (0, "Function return value not set");
6de9cd9a
DN
2631 else
2632 {
f8d0aee5 2633 /* Set the return value to the dummy result variable. */
923ab88c
TS
2634 tmp = build2 (MODIFY_EXPR, TREE_TYPE (result),
2635 DECL_RESULT (fndecl), result);
2636 tmp = build1_v (RETURN_EXPR, tmp);
6de9cd9a
DN
2637 gfc_add_expr_to_block (&block, tmp);
2638 }
2639 }
2640
2641 /* Add all the decls we created during processing. */
2642 decl = saved_function_decls;
2643 while (decl)
2644 {
2645 tree next;
2646
2647 next = TREE_CHAIN (decl);
2648 TREE_CHAIN (decl) = NULL_TREE;
2649 pushdecl (decl);
2650 decl = next;
2651 }
2652 saved_function_decls = NULL_TREE;
2653
2654 DECL_SAVED_TREE (fndecl) = gfc_finish_block (&block);
2655
2656 /* Finish off this function and send it for code generation. */
2657 poplevel (1, 0, 1);
2658 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
2659
2660 /* Output the GENERIC tree. */
2661 dump_function (TDI_original, fndecl);
2662
2663 /* Store the end of the function, so that we get good line number
2664 info for the epilogue. */
2665 cfun->function_end_locus = input_location;
2666
2667 /* We're leaving the context of this function, so zap cfun.
2668 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
2669 tree_rest_of_compilation. */
2670 cfun = NULL;
2671
2672 if (old_context)
2673 {
2674 pop_function_context ();
2675 saved_function_decls = saved_parent_function_decls;
2676 }
2677 current_function_decl = old_context;
2678
2679 if (decl_function_context (fndecl))
44de5aeb
RK
2680 /* Register this function with cgraph just far enough to get it
2681 added to our parent's nested function list. */
2682 (void) cgraph_node (fndecl);
6de9cd9a
DN
2683 else
2684 {
44de5aeb 2685 gfc_gimplify_function (fndecl);
8f235343 2686 cgraph_finalize_function (fndecl, false);
6de9cd9a
DN
2687 }
2688}
2689
6de9cd9a
DN
2690void
2691gfc_generate_constructors (void)
2692{
6e45f57b 2693 gcc_assert (gfc_static_ctors == NULL_TREE);
6de9cd9a
DN
2694#if 0
2695 tree fnname;
2696 tree type;
2697 tree fndecl;
2698 tree decl;
2699 tree tmp;
2700
2701 if (gfc_static_ctors == NULL_TREE)
2702 return;
2703
2704 fnname = get_file_function_name ('I');
2705 type = build_function_type (void_type_node,
2706 gfc_chainon_list (NULL_TREE, void_type_node));
2707
2708 fndecl = build_decl (FUNCTION_DECL, fnname, type);
2709 TREE_PUBLIC (fndecl) = 1;
2710
2711 decl = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
b785f485
RH
2712 DECL_ARTIFICIAL (decl) = 1;
2713 DECL_IGNORED_P (decl) = 1;
6de9cd9a
DN
2714 DECL_CONTEXT (decl) = fndecl;
2715 DECL_RESULT (fndecl) = decl;
2716
2717 pushdecl (fndecl);
2718
2719 current_function_decl = fndecl;
2720
0e6df31e 2721 rest_of_decl_compilation (fndecl, 1, 0);
6de9cd9a 2722
0e6df31e 2723 make_decl_rtl (fndecl);
6de9cd9a 2724
c8cc8542 2725 init_function_start (fndecl);
6de9cd9a 2726
6de9cd9a
DN
2727 pushlevel (0);
2728
2729 for (; gfc_static_ctors; gfc_static_ctors = TREE_CHAIN (gfc_static_ctors))
2730 {
2731 tmp =
2732 gfc_build_function_call (TREE_VALUE (gfc_static_ctors), NULL_TREE);
2733 DECL_SAVED_TREE (fndecl) = build_stmt (EXPR_STMT, tmp);
2734 }
2735
2736 poplevel (1, 0, 1);
2737
2738 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
2739
2740 free_after_parsing (cfun);
2741 free_after_compilation (cfun);
2742
0f0377f6 2743 tree_rest_of_compilation (fndecl);
6de9cd9a
DN
2744
2745 current_function_decl = NULL_TREE;
2746#endif
2747}
2748
0de4325e
TS
2749/* Translates a BLOCK DATA program unit. This means emitting the
2750 commons contained therein plus their initializations. We also emit
2751 a globally visible symbol to make sure that each BLOCK DATA program
2752 unit remains unique. */
2753
2754void
2755gfc_generate_block_data (gfc_namespace * ns)
2756{
2757 tree decl;
2758 tree id;
2759
c8cc8542
PB
2760 /* Tell the backend the source location of the block data. */
2761 if (ns->proc_name)
2762 gfc_set_backend_locus (&ns->proc_name->declared_at);
2763 else
2764 gfc_set_backend_locus (&gfc_current_locus);
2765
2766 /* Process the DATA statements. */
0de4325e
TS
2767 gfc_trans_common (ns);
2768
c8cc8542
PB
2769 /* Create a global symbol with the mane of the block data. This is to
2770 generate linker errors if the same name is used twice. It is never
2771 really used. */
0de4325e
TS
2772 if (ns->proc_name)
2773 id = gfc_sym_mangled_function_id (ns->proc_name);
2774 else
2775 id = get_identifier ("__BLOCK_DATA__");
2776
2777 decl = build_decl (VAR_DECL, id, gfc_array_index_type);
2778 TREE_PUBLIC (decl) = 1;
2779 TREE_STATIC (decl) = 1;
2780
2781 pushdecl (decl);
2782 rest_of_decl_compilation (decl, 1, 0);
2783}
2784
83d890b9 2785
6de9cd9a 2786#include "gt-fortran-trans-decl.h"
This page took 0.833981 seconds and 5 git commands to generate.