trans-common.c 36 KB

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