]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/fortran/trans-common.c
Update copyright years in gcc/
[thirdparty/gcc.git] / gcc / fortran / trans-common.c
1 /* Common block and equivalence list handling
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Contributed by Canqun Yang <canqun@nudt.edu.cn>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 /* The core algorithm is based on Andy Vaught's g95 tree. Also the
22 way to build UNION_TYPE is borrowed from Richard Henderson.
23
24 Transform common blocks. An integral part of this is processing
25 equivalence variables. Equivalenced variables that are not in a
26 common block end up in a private block of their own.
27
28 Each common block or local equivalence list is declared as a union.
29 Variables within the block are represented as a field within the
30 block with the proper offset.
31
32 So if two variables are equivalenced, they just point to a common
33 area in memory.
34
35 Mathematically, laying out an equivalence block is equivalent to
36 solving a linear system of equations. The matrix is usually a
37 sparse matrix in which each row contains all zero elements except
38 for a +1 and a -1, a sort of a generalized Vandermonde matrix. The
39 matrix is usually block diagonal. The system can be
40 overdetermined, underdetermined or have a unique solution. If the
41 system is inconsistent, the program is not standard conforming.
42 The solution vector is integral, since all of the pivots are +1 or -1.
43
44 How we lay out an equivalence block is a little less complicated.
45 In an equivalence list with n elements, there are n-1 conditions to
46 be satisfied. The conditions partition the variables into what we
47 will call segments. If A and B are equivalenced then A and B are
48 in the same segment. If B and C are equivalenced as well, then A,
49 B and C are in a segment and so on. Each segment is a block of
50 memory that has one or more variables equivalenced in some way. A
51 common block is made up of a series of segments that are joined one
52 after the other. In the linear system, a segment is a block
53 diagonal.
54
55 To lay out a segment we first start with some variable and
56 determine its length. The first variable is assumed to start at
57 offset one and extends to however long it is. We then traverse the
58 list of equivalences to find an unused condition that involves at
59 least one of the variables currently in the segment.
60
61 Each equivalence condition amounts to the condition B+b=C+c where B
62 and C are the offsets of the B and C variables, and b and c are
63 constants which are nonzero for array elements, substrings or
64 structure components. So for
65
66 EQUIVALENCE(B(2), C(3))
67 we have
68 B + 2*size of B's elements = C + 3*size of C's elements.
69
70 If B and C are known we check to see if the condition already
71 holds. If B is known we can solve for C. Since we know the length
72 of C, we can see if the minimum and maximum extents of the segment
73 are affected. Eventually, we make a full pass through the
74 equivalence list without finding any new conditions and the segment
75 is fully specified.
76
77 At this point, the segment is added to the current common block.
78 Since we know the minimum extent of the segment, everything in the
79 segment is translated to its position in the common block. The
80 usual case here is that there are no equivalence statements and the
81 common block is series of segments with one variable each, which is
82 a diagonal matrix in the matrix formulation.
83
84 Each segment is described by a chain of segment_info structures. Each
85 segment_info structure describes the extents of a single variable within
86 the segment. This list is maintained in the order the elements are
87 positioned within the segment. If two elements have the same starting
88 offset the smaller will come first. If they also have the same size their
89 ordering is undefined.
90
91 Once all common blocks have been created, the list of equivalences
92 is examined for still-unused equivalence conditions. We create a
93 block for each merged equivalence list. */
94
95 #include <map>
96 #include "config.h"
97 #include "system.h"
98 #include "coretypes.h"
99 #include "tm.h"
100 #include "tree.h"
101 #include "stringpool.h"
102 #include "stor-layout.h"
103 #include "varasm.h"
104 #include "gfortran.h"
105 #include "trans.h"
106 #include "trans-types.h"
107 #include "trans-const.h"
108 #include "target-memory.h"
109
110
111 /* Holds a single variable in an equivalence set. */
112 typedef struct segment_info
113 {
114 gfc_symbol *sym;
115 HOST_WIDE_INT offset;
116 HOST_WIDE_INT length;
117 /* This will contain the field type until the field is created. */
118 tree field;
119 struct segment_info *next;
120 } segment_info;
121
122 static segment_info * current_segment;
123
124 /* Store decl of all common blocks in this translation unit; the first
125 tree is the identifier. */
126 static std::map<tree, tree> gfc_map_of_all_commons;
127
128
129 /* Make a segment_info based on a symbol. */
130
131 static segment_info *
132 get_segment_info (gfc_symbol * sym, HOST_WIDE_INT offset)
133 {
134 segment_info *s;
135
136 /* Make sure we've got the character length. */
137 if (sym->ts.type == BT_CHARACTER)
138 gfc_conv_const_charlen (sym->ts.u.cl);
139
140 /* Create the segment_info and fill it in. */
141 s = XCNEW (segment_info);
142 s->sym = sym;
143 /* We will use this type when building the segment aggregate type. */
144 s->field = gfc_sym_type (sym);
145 s->length = int_size_in_bytes (s->field);
146 s->offset = offset;
147
148 return s;
149 }
150
151
152 /* Add a copy of a segment list to the namespace. This is specifically for
153 equivalence segments, so that dependency checking can be done on
154 equivalence group members. */
155
156 static void
157 copy_equiv_list_to_ns (segment_info *c)
158 {
159 segment_info *f;
160 gfc_equiv_info *s;
161 gfc_equiv_list *l;
162
163 l = XCNEW (gfc_equiv_list);
164
165 l->next = c->sym->ns->equiv_lists;
166 c->sym->ns->equiv_lists = l;
167
168 for (f = c; f; f = f->next)
169 {
170 s = XCNEW (gfc_equiv_info);
171 s->next = l->equiv;
172 l->equiv = s;
173 s->sym = f->sym;
174 s->offset = f->offset;
175 s->length = f->length;
176 }
177 }
178
179
180 /* Add combine segment V and segment LIST. */
181
182 static segment_info *
183 add_segments (segment_info *list, segment_info *v)
184 {
185 segment_info *s;
186 segment_info *p;
187 segment_info *next;
188
189 p = NULL;
190 s = list;
191
192 while (v)
193 {
194 /* Find the location of the new element. */
195 while (s)
196 {
197 if (v->offset < s->offset)
198 break;
199 if (v->offset == s->offset
200 && v->length <= s->length)
201 break;
202
203 p = s;
204 s = s->next;
205 }
206
207 /* Insert the new element in between p and s. */
208 next = v->next;
209 v->next = s;
210 if (p == NULL)
211 list = v;
212 else
213 p->next = v;
214
215 p = v;
216 v = next;
217 }
218
219 return list;
220 }
221
222
223 /* Construct mangled common block name from symbol name. */
224
225 /* We need the bind(c) flag to tell us how/if we should mangle the symbol
226 name. There are few calls to this function, so few places that this
227 would need to be added. At the moment, there is only one call, in
228 build_common_decl(). We can't attempt to look up the common block
229 because we may be building it for the first time and therefore, it won't
230 be in the common_root. We also need the binding label, if it's bind(c).
231 Therefore, send in the pointer to the common block, so whatever info we
232 have so far can be used. All of the necessary info should be available
233 in the gfc_common_head by now, so it should be accurate to test the
234 isBindC flag and use the binding label given if it is bind(c).
235
236 We may NOT know yet if it's bind(c) or not, but we can try at least.
237 Will have to figure out what to do later if it's labeled bind(c)
238 after this is called. */
239
240 static tree
241 gfc_sym_mangled_common_id (gfc_common_head *com)
242 {
243 int has_underscore;
244 char mangled_name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
245 char name[GFC_MAX_SYMBOL_LEN + 1];
246
247 /* Get the name out of the common block pointer. */
248 strcpy (name, com->name);
249
250 /* If we're suppose to do a bind(c). */
251 if (com->is_bind_c == 1 && com->binding_label)
252 return get_identifier (com->binding_label);
253
254 if (strcmp (name, BLANK_COMMON_NAME) == 0)
255 return get_identifier (name);
256
257 if (gfc_option.flag_underscoring)
258 {
259 has_underscore = strchr (name, '_') != 0;
260 if (gfc_option.flag_second_underscore && has_underscore)
261 snprintf (mangled_name, sizeof mangled_name, "%s__", name);
262 else
263 snprintf (mangled_name, sizeof mangled_name, "%s_", name);
264
265 return get_identifier (mangled_name);
266 }
267 else
268 return get_identifier (name);
269 }
270
271
272 /* Build a field declaration for a common variable or a local equivalence
273 object. */
274
275 static void
276 build_field (segment_info *h, tree union_type, record_layout_info rli)
277 {
278 tree field;
279 tree name;
280 HOST_WIDE_INT offset = h->offset;
281 unsigned HOST_WIDE_INT desired_align, known_align;
282
283 name = get_identifier (h->sym->name);
284 field = build_decl (h->sym->declared_at.lb->location,
285 FIELD_DECL, name, h->field);
286 known_align = (offset & -offset) * BITS_PER_UNIT;
287 if (known_align == 0 || known_align > BIGGEST_ALIGNMENT)
288 known_align = BIGGEST_ALIGNMENT;
289
290 desired_align = update_alignment_for_field (rli, field, known_align);
291 if (desired_align > known_align)
292 DECL_PACKED (field) = 1;
293
294 DECL_FIELD_CONTEXT (field) = union_type;
295 DECL_FIELD_OFFSET (field) = size_int (offset);
296 DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
297 SET_DECL_OFFSET_ALIGN (field, known_align);
298
299 rli->offset = size_binop (MAX_EXPR, rli->offset,
300 size_binop (PLUS_EXPR,
301 DECL_FIELD_OFFSET (field),
302 DECL_SIZE_UNIT (field)));
303 /* If this field is assigned to a label, we create another two variables.
304 One will hold the address of target label or format label. The other will
305 hold the length of format label string. */
306 if (h->sym->attr.assign)
307 {
308 tree len;
309 tree addr;
310
311 gfc_allocate_lang_decl (field);
312 GFC_DECL_ASSIGN (field) = 1;
313 len = gfc_create_var_np (gfc_charlen_type_node,h->sym->name);
314 addr = gfc_create_var_np (pvoid_type_node, h->sym->name);
315 TREE_STATIC (len) = 1;
316 TREE_STATIC (addr) = 1;
317 DECL_INITIAL (len) = build_int_cst (gfc_charlen_type_node, -2);
318 gfc_set_decl_location (len, &h->sym->declared_at);
319 gfc_set_decl_location (addr, &h->sym->declared_at);
320 GFC_DECL_STRING_LEN (field) = pushdecl_top_level (len);
321 GFC_DECL_ASSIGN_ADDR (field) = pushdecl_top_level (addr);
322 }
323
324 /* If this field is volatile, mark it. */
325 if (h->sym->attr.volatile_)
326 {
327 tree new_type;
328 TREE_THIS_VOLATILE (field) = 1;
329 TREE_SIDE_EFFECTS (field) = 1;
330 new_type = build_qualified_type (TREE_TYPE (field), TYPE_QUAL_VOLATILE);
331 TREE_TYPE (field) = new_type;
332 }
333
334 h->field = field;
335 }
336
337
338 /* Get storage for local equivalence. */
339
340 static tree
341 build_equiv_decl (tree union_type, bool is_init, bool is_saved)
342 {
343 tree decl;
344 char name[15];
345 static int serial = 0;
346
347 if (is_init)
348 {
349 decl = gfc_create_var (union_type, "equiv");
350 TREE_STATIC (decl) = 1;
351 GFC_DECL_COMMON_OR_EQUIV (decl) = 1;
352 return decl;
353 }
354
355 snprintf (name, sizeof (name), "equiv.%d", serial++);
356 decl = build_decl (input_location,
357 VAR_DECL, get_identifier (name), union_type);
358 DECL_ARTIFICIAL (decl) = 1;
359 DECL_IGNORED_P (decl) = 1;
360
361 if (!gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
362 || is_saved)
363 TREE_STATIC (decl) = 1;
364
365 TREE_ADDRESSABLE (decl) = 1;
366 TREE_USED (decl) = 1;
367 GFC_DECL_COMMON_OR_EQUIV (decl) = 1;
368
369 /* The source location has been lost, and doesn't really matter.
370 We need to set it to something though. */
371 gfc_set_decl_location (decl, &gfc_current_locus);
372
373 gfc_add_decl_to_function (decl);
374
375 return decl;
376 }
377
378
379 /* Get storage for common block. */
380
381 static tree
382 build_common_decl (gfc_common_head *com, tree union_type, bool is_init)
383 {
384 tree decl, identifier;
385
386 identifier = gfc_sym_mangled_common_id (com);
387 decl = gfc_map_of_all_commons.count(identifier)
388 ? gfc_map_of_all_commons[identifier] : NULL_TREE;
389
390 /* Update the size of this common block as needed. */
391 if (decl != NULL_TREE)
392 {
393 tree size = TYPE_SIZE_UNIT (union_type);
394
395 /* Named common blocks of the same name shall be of the same size
396 in all scoping units of a program in which they appear, but
397 blank common blocks may be of different sizes. */
398 if (!tree_int_cst_equal (DECL_SIZE_UNIT (decl), size)
399 && strcmp (com->name, BLANK_COMMON_NAME))
400 gfc_warning ("Named COMMON block '%s' at %L shall be of the "
401 "same size as elsewhere (%lu vs %lu bytes)", com->name,
402 &com->where,
403 (unsigned long) TREE_INT_CST_LOW (size),
404 (unsigned long) TREE_INT_CST_LOW (DECL_SIZE_UNIT (decl)));
405
406 if (tree_int_cst_lt (DECL_SIZE_UNIT (decl), size))
407 {
408 DECL_SIZE (decl) = TYPE_SIZE (union_type);
409 DECL_SIZE_UNIT (decl) = size;
410 DECL_MODE (decl) = TYPE_MODE (union_type);
411 TREE_TYPE (decl) = union_type;
412 layout_decl (decl, 0);
413 }
414 }
415
416 /* If this common block has been declared in a previous program unit,
417 and either it is already initialized or there is no new initialization
418 for it, just return. */
419 if ((decl != NULL_TREE) && (!is_init || DECL_INITIAL (decl)))
420 return decl;
421
422 /* If there is no backend_decl for the common block, build it. */
423 if (decl == NULL_TREE)
424 {
425 if (com->is_bind_c == 1 && com->binding_label)
426 decl = build_decl (input_location, VAR_DECL, identifier, union_type);
427 else
428 {
429 decl = build_decl (input_location, VAR_DECL, get_identifier (com->name),
430 union_type);
431 gfc_set_decl_assembler_name (decl, identifier);
432 }
433
434 TREE_PUBLIC (decl) = 1;
435 TREE_STATIC (decl) = 1;
436 DECL_IGNORED_P (decl) = 1;
437 if (!com->is_bind_c)
438 DECL_ALIGN (decl) = BIGGEST_ALIGNMENT;
439 else
440 {
441 /* Do not set the alignment for bind(c) common blocks to
442 BIGGEST_ALIGNMENT because that won't match what C does. Also,
443 for common blocks with one element, the alignment must be
444 that of the field within the common block in order to match
445 what C will do. */
446 tree field = NULL_TREE;
447 field = TYPE_FIELDS (TREE_TYPE (decl));
448 if (DECL_CHAIN (field) == NULL_TREE)
449 DECL_ALIGN (decl) = TYPE_ALIGN (TREE_TYPE (field));
450 }
451 DECL_USER_ALIGN (decl) = 0;
452 GFC_DECL_COMMON_OR_EQUIV (decl) = 1;
453
454 gfc_set_decl_location (decl, &com->where);
455
456 if (com->threadprivate)
457 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
458
459 /* Place the back end declaration for this common block in
460 GLOBAL_BINDING_LEVEL. */
461 gfc_map_of_all_commons[identifier] = pushdecl_top_level (decl);
462 }
463
464 /* Has no initial values. */
465 if (!is_init)
466 {
467 DECL_INITIAL (decl) = NULL_TREE;
468 DECL_COMMON (decl) = 1;
469 DECL_DEFER_OUTPUT (decl) = 1;
470 }
471 else
472 {
473 DECL_INITIAL (decl) = error_mark_node;
474 DECL_COMMON (decl) = 0;
475 DECL_DEFER_OUTPUT (decl) = 0;
476 }
477 return decl;
478 }
479
480
481 /* Return a field that is the size of the union, if an equivalence has
482 overlapping initializers. Merge the initializers into a single
483 initializer for this new field, then free the old ones. */
484
485 static tree
486 get_init_field (segment_info *head, tree union_type, tree *field_init,
487 record_layout_info rli)
488 {
489 segment_info *s;
490 HOST_WIDE_INT length = 0;
491 HOST_WIDE_INT offset = 0;
492 unsigned HOST_WIDE_INT known_align, desired_align;
493 bool overlap = false;
494 tree tmp, field;
495 tree init;
496 unsigned char *data, *chk;
497 vec<constructor_elt, va_gc> *v = NULL;
498
499 tree type = unsigned_char_type_node;
500 int i;
501
502 /* Obtain the size of the union and check if there are any overlapping
503 initializers. */
504 for (s = head; s; s = s->next)
505 {
506 HOST_WIDE_INT slen = s->offset + s->length;
507 if (s->sym->value)
508 {
509 if (s->offset < offset)
510 overlap = true;
511 offset = slen;
512 }
513 length = length < slen ? slen : length;
514 }
515
516 if (!overlap)
517 return NULL_TREE;
518
519 /* Now absorb all the initializer data into a single vector,
520 whilst checking for overlapping, unequal values. */
521 data = XCNEWVEC (unsigned char, (size_t)length);
522 chk = XCNEWVEC (unsigned char, (size_t)length);
523
524 /* TODO - change this when default initialization is implemented. */
525 memset (data, '\0', (size_t)length);
526 memset (chk, '\0', (size_t)length);
527 for (s = head; s; s = s->next)
528 if (s->sym->value)
529 gfc_merge_initializers (s->sym->ts, s->sym->value,
530 &data[s->offset],
531 &chk[s->offset],
532 (size_t)s->length);
533
534 for (i = 0; i < length; i++)
535 CONSTRUCTOR_APPEND_ELT (v, NULL, build_int_cst (type, data[i]));
536
537 free (data);
538 free (chk);
539
540 /* Build a char[length] array to hold the initializers. Much of what
541 follows is borrowed from build_field, above. */
542
543 tmp = build_int_cst (gfc_array_index_type, length - 1);
544 tmp = build_range_type (gfc_array_index_type,
545 gfc_index_zero_node, tmp);
546 tmp = build_array_type (type, tmp);
547 field = build_decl (gfc_current_locus.lb->location,
548 FIELD_DECL, NULL_TREE, tmp);
549
550 known_align = BIGGEST_ALIGNMENT;
551
552 desired_align = update_alignment_for_field (rli, field, known_align);
553 if (desired_align > known_align)
554 DECL_PACKED (field) = 1;
555
556 DECL_FIELD_CONTEXT (field) = union_type;
557 DECL_FIELD_OFFSET (field) = size_int (0);
558 DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
559 SET_DECL_OFFSET_ALIGN (field, known_align);
560
561 rli->offset = size_binop (MAX_EXPR, rli->offset,
562 size_binop (PLUS_EXPR,
563 DECL_FIELD_OFFSET (field),
564 DECL_SIZE_UNIT (field)));
565
566 init = build_constructor (TREE_TYPE (field), v);
567 TREE_CONSTANT (init) = 1;
568
569 *field_init = init;
570
571 for (s = head; s; s = s->next)
572 {
573 if (s->sym->value == NULL)
574 continue;
575
576 gfc_free_expr (s->sym->value);
577 s->sym->value = NULL;
578 }
579
580 return field;
581 }
582
583
584 /* Declare memory for the common block or local equivalence, and create
585 backend declarations for all of the elements. */
586
587 static void
588 create_common (gfc_common_head *com, segment_info *head, bool saw_equiv)
589 {
590 segment_info *s, *next_s;
591 tree union_type;
592 tree *field_link;
593 tree field;
594 tree field_init = NULL_TREE;
595 record_layout_info rli;
596 tree decl;
597 bool is_init = false;
598 bool is_saved = false;
599
600 /* Declare the variables inside the common block.
601 If the current common block contains any equivalence object, then
602 make a UNION_TYPE node, otherwise RECORD_TYPE. This will let the
603 alias analyzer work well when there is no address overlapping for
604 common variables in the current common block. */
605 if (saw_equiv)
606 union_type = make_node (UNION_TYPE);
607 else
608 union_type = make_node (RECORD_TYPE);
609
610 rli = start_record_layout (union_type);
611 field_link = &TYPE_FIELDS (union_type);
612
613 /* Check for overlapping initializers and replace them with a single,
614 artificial field that contains all the data. */
615 if (saw_equiv)
616 field = get_init_field (head, union_type, &field_init, rli);
617 else
618 field = NULL_TREE;
619
620 if (field != NULL_TREE)
621 {
622 is_init = true;
623 *field_link = field;
624 field_link = &DECL_CHAIN (field);
625 }
626
627 for (s = head; s; s = s->next)
628 {
629 build_field (s, union_type, rli);
630
631 /* Link the field into the type. */
632 *field_link = s->field;
633 field_link = &DECL_CHAIN (s->field);
634
635 /* Has initial value. */
636 if (s->sym->value)
637 is_init = true;
638
639 /* Has SAVE attribute. */
640 if (s->sym->attr.save)
641 is_saved = true;
642 }
643
644 finish_record_layout (rli, true);
645
646 if (com)
647 decl = build_common_decl (com, union_type, is_init);
648 else
649 decl = build_equiv_decl (union_type, is_init, is_saved);
650
651 if (is_init)
652 {
653 tree ctor, tmp;
654 vec<constructor_elt, va_gc> *v = NULL;
655
656 if (field != NULL_TREE && field_init != NULL_TREE)
657 CONSTRUCTOR_APPEND_ELT (v, field, field_init);
658 else
659 for (s = head; s; s = s->next)
660 {
661 if (s->sym->value)
662 {
663 /* Add the initializer for this field. */
664 tmp = gfc_conv_initializer (s->sym->value, &s->sym->ts,
665 TREE_TYPE (s->field),
666 s->sym->attr.dimension,
667 s->sym->attr.pointer
668 || s->sym->attr.allocatable, false);
669
670 CONSTRUCTOR_APPEND_ELT (v, s->field, tmp);
671 }
672 }
673
674 gcc_assert (!v->is_empty ());
675 ctor = build_constructor (union_type, v);
676 TREE_CONSTANT (ctor) = 1;
677 TREE_STATIC (ctor) = 1;
678 DECL_INITIAL (decl) = ctor;
679
680 #ifdef ENABLE_CHECKING
681 {
682 tree field, value;
683 unsigned HOST_WIDE_INT idx;
684 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, field, value)
685 gcc_assert (TREE_CODE (field) == FIELD_DECL);
686 }
687 #endif
688 }
689
690 /* Build component reference for each variable. */
691 for (s = head; s; s = next_s)
692 {
693 tree var_decl;
694
695 var_decl = build_decl (s->sym->declared_at.lb->location,
696 VAR_DECL, DECL_NAME (s->field),
697 TREE_TYPE (s->field));
698 TREE_STATIC (var_decl) = TREE_STATIC (decl);
699 /* Mark the variable as used in order to avoid warnings about
700 unused variables. */
701 TREE_USED (var_decl) = 1;
702 if (s->sym->attr.use_assoc)
703 DECL_IGNORED_P (var_decl) = 1;
704 if (s->sym->attr.target)
705 TREE_ADDRESSABLE (var_decl) = 1;
706 /* Fake variables are not visible from other translation units. */
707 TREE_PUBLIC (var_decl) = 0;
708
709 /* To preserve identifier names in COMMON, chain to procedure
710 scope unless at top level in a module definition. */
711 if (com
712 && s->sym->ns->proc_name
713 && s->sym->ns->proc_name->attr.flavor == FL_MODULE)
714 var_decl = pushdecl_top_level (var_decl);
715 else
716 gfc_add_decl_to_function (var_decl);
717
718 SET_DECL_VALUE_EXPR (var_decl,
719 fold_build3_loc (input_location, COMPONENT_REF,
720 TREE_TYPE (s->field),
721 decl, s->field, NULL_TREE));
722 DECL_HAS_VALUE_EXPR_P (var_decl) = 1;
723 GFC_DECL_COMMON_OR_EQUIV (var_decl) = 1;
724
725 if (s->sym->attr.assign)
726 {
727 gfc_allocate_lang_decl (var_decl);
728 GFC_DECL_ASSIGN (var_decl) = 1;
729 GFC_DECL_STRING_LEN (var_decl) = GFC_DECL_STRING_LEN (s->field);
730 GFC_DECL_ASSIGN_ADDR (var_decl) = GFC_DECL_ASSIGN_ADDR (s->field);
731 }
732
733 s->sym->backend_decl = var_decl;
734
735 next_s = s->next;
736 free (s);
737 }
738 }
739
740
741 /* Given a symbol, find it in the current segment list. Returns NULL if
742 not found. */
743
744 static segment_info *
745 find_segment_info (gfc_symbol *symbol)
746 {
747 segment_info *n;
748
749 for (n = current_segment; n; n = n->next)
750 {
751 if (n->sym == symbol)
752 return n;
753 }
754
755 return NULL;
756 }
757
758
759 /* Given an expression node, make sure it is a constant integer and return
760 the mpz_t value. */
761
762 static mpz_t *
763 get_mpz (gfc_expr *e)
764 {
765
766 if (e->expr_type != EXPR_CONSTANT)
767 gfc_internal_error ("get_mpz(): Not an integer constant");
768
769 return &e->value.integer;
770 }
771
772
773 /* Given an array specification and an array reference, figure out the
774 array element number (zero based). Bounds and elements are guaranteed
775 to be constants. If something goes wrong we generate an error and
776 return zero. */
777
778 static HOST_WIDE_INT
779 element_number (gfc_array_ref *ar)
780 {
781 mpz_t multiplier, offset, extent, n;
782 gfc_array_spec *as;
783 HOST_WIDE_INT i, rank;
784
785 as = ar->as;
786 rank = as->rank;
787 mpz_init_set_ui (multiplier, 1);
788 mpz_init_set_ui (offset, 0);
789 mpz_init (extent);
790 mpz_init (n);
791
792 for (i = 0; i < rank; i++)
793 {
794 if (ar->dimen_type[i] != DIMEN_ELEMENT)
795 gfc_internal_error ("element_number(): Bad dimension type");
796
797 mpz_sub (n, *get_mpz (ar->start[i]), *get_mpz (as->lower[i]));
798
799 mpz_mul (n, n, multiplier);
800 mpz_add (offset, offset, n);
801
802 mpz_sub (extent, *get_mpz (as->upper[i]), *get_mpz (as->lower[i]));
803 mpz_add_ui (extent, extent, 1);
804
805 if (mpz_sgn (extent) < 0)
806 mpz_set_ui (extent, 0);
807
808 mpz_mul (multiplier, multiplier, extent);
809 }
810
811 i = mpz_get_ui (offset);
812
813 mpz_clear (multiplier);
814 mpz_clear (offset);
815 mpz_clear (extent);
816 mpz_clear (n);
817
818 return i;
819 }
820
821
822 /* Given a single element of an equivalence list, figure out the offset
823 from the base symbol. For simple variables or full arrays, this is
824 simply zero. For an array element we have to calculate the array
825 element number and multiply by the element size. For a substring we
826 have to calculate the further reference. */
827
828 static HOST_WIDE_INT
829 calculate_offset (gfc_expr *e)
830 {
831 HOST_WIDE_INT n, element_size, offset;
832 gfc_typespec *element_type;
833 gfc_ref *reference;
834
835 offset = 0;
836 element_type = &e->symtree->n.sym->ts;
837
838 for (reference = e->ref; reference; reference = reference->next)
839 switch (reference->type)
840 {
841 case REF_ARRAY:
842 switch (reference->u.ar.type)
843 {
844 case AR_FULL:
845 break;
846
847 case AR_ELEMENT:
848 n = element_number (&reference->u.ar);
849 if (element_type->type == BT_CHARACTER)
850 gfc_conv_const_charlen (element_type->u.cl);
851 element_size =
852 int_size_in_bytes (gfc_typenode_for_spec (element_type));
853 offset += n * element_size;
854 break;
855
856 default:
857 gfc_error ("Bad array reference at %L", &e->where);
858 }
859 break;
860 case REF_SUBSTRING:
861 if (reference->u.ss.start != NULL)
862 offset += mpz_get_ui (*get_mpz (reference->u.ss.start)) - 1;
863 break;
864 default:
865 gfc_error ("Illegal reference type at %L as EQUIVALENCE object",
866 &e->where);
867 }
868 return offset;
869 }
870
871
872 /* Add a new segment_info structure to the current segment. eq1 is already
873 in the list, eq2 is not. */
874
875 static void
876 new_condition (segment_info *v, gfc_equiv *eq1, gfc_equiv *eq2)
877 {
878 HOST_WIDE_INT offset1, offset2;
879 segment_info *a;
880
881 offset1 = calculate_offset (eq1->expr);
882 offset2 = calculate_offset (eq2->expr);
883
884 a = get_segment_info (eq2->expr->symtree->n.sym,
885 v->offset + offset1 - offset2);
886
887 current_segment = add_segments (current_segment, a);
888 }
889
890
891 /* Given two equivalence structures that are both already in the list, make
892 sure that this new condition is not violated, generating an error if it
893 is. */
894
895 static void
896 confirm_condition (segment_info *s1, gfc_equiv *eq1, segment_info *s2,
897 gfc_equiv *eq2)
898 {
899 HOST_WIDE_INT offset1, offset2;
900
901 offset1 = calculate_offset (eq1->expr);
902 offset2 = calculate_offset (eq2->expr);
903
904 if (s1->offset + offset1 != s2->offset + offset2)
905 gfc_error ("Inconsistent equivalence rules involving '%s' at %L and "
906 "'%s' at %L", s1->sym->name, &s1->sym->declared_at,
907 s2->sym->name, &s2->sym->declared_at);
908 }
909
910
911 /* Process a new equivalence condition. eq1 is know to be in segment f.
912 If eq2 is also present then confirm that the condition holds.
913 Otherwise add a new variable to the segment list. */
914
915 static void
916 add_condition (segment_info *f, gfc_equiv *eq1, gfc_equiv *eq2)
917 {
918 segment_info *n;
919
920 n = find_segment_info (eq2->expr->symtree->n.sym);
921
922 if (n == NULL)
923 new_condition (f, eq1, eq2);
924 else
925 confirm_condition (f, eq1, n, eq2);
926 }
927
928
929 /* Given a segment element, search through the equivalence lists for unused
930 conditions that involve the symbol. Add these rules to the segment. */
931
932 static bool
933 find_equivalence (segment_info *n)
934 {
935 gfc_equiv *e1, *e2, *eq;
936 bool found;
937
938 found = FALSE;
939
940 for (e1 = n->sym->ns->equiv; e1; e1 = e1->next)
941 {
942 eq = NULL;
943
944 /* Search the equivalence list, including the root (first) element
945 for the symbol that owns the segment. */
946 for (e2 = e1; e2; e2 = e2->eq)
947 {
948 if (!e2->used && e2->expr->symtree->n.sym == n->sym)
949 {
950 eq = e2;
951 break;
952 }
953 }
954
955 /* Go to the next root element. */
956 if (eq == NULL)
957 continue;
958
959 eq->used = 1;
960
961 /* Now traverse the equivalence list matching the offsets. */
962 for (e2 = e1; e2; e2 = e2->eq)
963 {
964 if (!e2->used && e2 != eq)
965 {
966 add_condition (n, eq, e2);
967 e2->used = 1;
968 found = TRUE;
969 }
970 }
971 }
972 return found;
973 }
974
975
976 /* Add all symbols equivalenced within a segment. We need to scan the
977 segment list multiple times to include indirect equivalences. Since
978 a new segment_info can inserted at the beginning of the segment list,
979 depending on its offset, we have to force a final pass through the
980 loop by demanding that completion sees a pass with no matches; i.e.,
981 all symbols with equiv_built set and no new equivalences found. */
982
983 static void
984 add_equivalences (bool *saw_equiv)
985 {
986 segment_info *f;
987 bool seen_one, more;
988
989 seen_one = false;
990 more = TRUE;
991 while (more)
992 {
993 more = FALSE;
994 for (f = current_segment; f; f = f->next)
995 {
996 if (!f->sym->equiv_built)
997 {
998 f->sym->equiv_built = 1;
999 seen_one = find_equivalence (f);
1000 if (seen_one)
1001 {
1002 *saw_equiv = true;
1003 more = true;
1004 }
1005 }
1006 }
1007 }
1008
1009 /* Add a copy of this segment list to the namespace. */
1010 copy_equiv_list_to_ns (current_segment);
1011 }
1012
1013
1014 /* Returns the offset necessary to properly align the current equivalence.
1015 Sets *palign to the required alignment. */
1016
1017 static HOST_WIDE_INT
1018 align_segment (unsigned HOST_WIDE_INT *palign)
1019 {
1020 segment_info *s;
1021 unsigned HOST_WIDE_INT offset;
1022 unsigned HOST_WIDE_INT max_align;
1023 unsigned HOST_WIDE_INT this_align;
1024 unsigned HOST_WIDE_INT this_offset;
1025
1026 max_align = 1;
1027 offset = 0;
1028 for (s = current_segment; s; s = s->next)
1029 {
1030 this_align = TYPE_ALIGN_UNIT (s->field);
1031 if (s->offset & (this_align - 1))
1032 {
1033 /* Field is misaligned. */
1034 this_offset = this_align - ((s->offset + offset) & (this_align - 1));
1035 if (this_offset & (max_align - 1))
1036 {
1037 /* Aligning this field would misalign a previous field. */
1038 gfc_error ("The equivalence set for variable '%s' "
1039 "declared at %L violates alignment requirements",
1040 s->sym->name, &s->sym->declared_at);
1041 }
1042 offset += this_offset;
1043 }
1044 max_align = this_align;
1045 }
1046 if (palign)
1047 *palign = max_align;
1048 return offset;
1049 }
1050
1051
1052 /* Adjust segment offsets by the given amount. */
1053
1054 static void
1055 apply_segment_offset (segment_info *s, HOST_WIDE_INT offset)
1056 {
1057 for (; s; s = s->next)
1058 s->offset += offset;
1059 }
1060
1061
1062 /* Lay out a symbol in a common block. If the symbol has already been seen
1063 then check the location is consistent. Otherwise create segments
1064 for that symbol and all the symbols equivalenced with it. */
1065
1066 /* Translate a single common block. */
1067
1068 static void
1069 translate_common (gfc_common_head *common, gfc_symbol *var_list)
1070 {
1071 gfc_symbol *sym;
1072 segment_info *s;
1073 segment_info *common_segment;
1074 HOST_WIDE_INT offset;
1075 HOST_WIDE_INT current_offset;
1076 unsigned HOST_WIDE_INT align;
1077 bool saw_equiv;
1078
1079 common_segment = NULL;
1080 offset = 0;
1081 current_offset = 0;
1082 align = 1;
1083 saw_equiv = false;
1084
1085 /* Add symbols to the segment. */
1086 for (sym = var_list; sym; sym = sym->common_next)
1087 {
1088 current_segment = common_segment;
1089 s = find_segment_info (sym);
1090
1091 /* Symbol has already been added via an equivalence. Multiple
1092 use associations of the same common block result in equiv_built
1093 being set but no information about the symbol in the segment. */
1094 if (s && sym->equiv_built)
1095 {
1096 /* Ensure the current location is properly aligned. */
1097 align = TYPE_ALIGN_UNIT (s->field);
1098 current_offset = (current_offset + align - 1) &~ (align - 1);
1099
1100 /* Verify that it ended up where we expect it. */
1101 if (s->offset != current_offset)
1102 {
1103 gfc_error ("Equivalence for '%s' does not match ordering of "
1104 "COMMON '%s' at %L", sym->name,
1105 common->name, &common->where);
1106 }
1107 }
1108 else
1109 {
1110 /* A symbol we haven't seen before. */
1111 s = current_segment = get_segment_info (sym, current_offset);
1112
1113 /* Add all objects directly or indirectly equivalenced with this
1114 symbol. */
1115 add_equivalences (&saw_equiv);
1116
1117 if (current_segment->offset < 0)
1118 gfc_error ("The equivalence set for '%s' cause an invalid "
1119 "extension to COMMON '%s' at %L", sym->name,
1120 common->name, &common->where);
1121
1122 if (gfc_option.flag_align_commons)
1123 offset = align_segment (&align);
1124
1125 if (offset)
1126 {
1127 /* The required offset conflicts with previous alignment
1128 requirements. Insert padding immediately before this
1129 segment. */
1130 if (gfc_option.warn_align_commons)
1131 {
1132 if (strcmp (common->name, BLANK_COMMON_NAME))
1133 gfc_warning ("Padding of %d bytes required before '%s' in "
1134 "COMMON '%s' at %L; reorder elements or use "
1135 "-fno-align-commons", (int)offset,
1136 s->sym->name, common->name, &common->where);
1137 else
1138 gfc_warning ("Padding of %d bytes required before '%s' in "
1139 "COMMON at %L; reorder elements or use "
1140 "-fno-align-commons", (int)offset,
1141 s->sym->name, &common->where);
1142 }
1143 }
1144
1145 /* Apply the offset to the new segments. */
1146 apply_segment_offset (current_segment, offset);
1147 current_offset += offset;
1148
1149 /* Add the new segments to the common block. */
1150 common_segment = add_segments (common_segment, current_segment);
1151 }
1152
1153 /* The offset of the next common variable. */
1154 current_offset += s->length;
1155 }
1156
1157 if (common_segment == NULL)
1158 {
1159 gfc_error ("COMMON '%s' at %L does not exist",
1160 common->name, &common->where);
1161 return;
1162 }
1163
1164 if (common_segment->offset != 0 && gfc_option.warn_align_commons)
1165 {
1166 if (strcmp (common->name, BLANK_COMMON_NAME))
1167 gfc_warning ("COMMON '%s' at %L requires %d bytes of padding; "
1168 "reorder elements or use -fno-align-commons",
1169 common->name, &common->where, (int)common_segment->offset);
1170 else
1171 gfc_warning ("COMMON at %L requires %d bytes of padding; "
1172 "reorder elements or use -fno-align-commons",
1173 &common->where, (int)common_segment->offset);
1174 }
1175
1176 create_common (common, common_segment, saw_equiv);
1177 }
1178
1179
1180 /* Create a new block for each merged equivalence list. */
1181
1182 static void
1183 finish_equivalences (gfc_namespace *ns)
1184 {
1185 gfc_equiv *z, *y;
1186 gfc_symbol *sym;
1187 gfc_common_head * c;
1188 HOST_WIDE_INT offset;
1189 unsigned HOST_WIDE_INT align;
1190 bool dummy;
1191
1192 for (z = ns->equiv; z; z = z->next)
1193 for (y = z->eq; y; y = y->eq)
1194 {
1195 if (y->used)
1196 continue;
1197 sym = z->expr->symtree->n.sym;
1198 current_segment = get_segment_info (sym, 0);
1199
1200 /* All objects directly or indirectly equivalenced with this
1201 symbol. */
1202 add_equivalences (&dummy);
1203
1204 /* Align the block. */
1205 offset = align_segment (&align);
1206
1207 /* Ensure all offsets are positive. */
1208 offset -= current_segment->offset & ~(align - 1);
1209
1210 apply_segment_offset (current_segment, offset);
1211
1212 /* Create the decl. If this is a module equivalence, it has a
1213 unique name, pointed to by z->module. This is written to a
1214 gfc_common_header to push create_common into using
1215 build_common_decl, so that the equivalence appears as an
1216 external symbol. Otherwise, a local declaration is built using
1217 build_equiv_decl. */
1218 if (z->module)
1219 {
1220 c = gfc_get_common_head ();
1221 /* We've lost the real location, so use the location of the
1222 enclosing procedure. */
1223 c->where = ns->proc_name->declared_at;
1224 strcpy (c->name, z->module);
1225 }
1226 else
1227 c = NULL;
1228
1229 create_common (c, current_segment, true);
1230 break;
1231 }
1232 }
1233
1234
1235 /* Work function for translating a named common block. */
1236
1237 static void
1238 named_common (gfc_symtree *st)
1239 {
1240 translate_common (st->n.common, st->n.common->head);
1241 }
1242
1243
1244 /* Translate the common blocks in a namespace. Unlike other variables,
1245 these have to be created before code, because the backend_decl depends
1246 on the rest of the common block. */
1247
1248 void
1249 gfc_trans_common (gfc_namespace *ns)
1250 {
1251 gfc_common_head *c;
1252
1253 /* Translate the blank common block. */
1254 if (ns->blank_common.head != NULL)
1255 {
1256 c = gfc_get_common_head ();
1257 c->where = ns->blank_common.head->common_head->where;
1258 strcpy (c->name, BLANK_COMMON_NAME);
1259 translate_common (c, ns->blank_common.head);
1260 }
1261
1262 /* Translate all named common blocks. */
1263 gfc_traverse_symtree (ns->common_root, named_common);
1264
1265 /* Translate local equivalence. */
1266 finish_equivalences (ns);
1267
1268 /* Commit the newly created symbols for common blocks and module
1269 equivalences. */
1270 gfc_commit_symbols ();
1271 }