annotate gcc/fortran/trans-common.c @ 136:4627f235cf2a

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