📄 trans-common.c
字号:
/* Common block and equivalence list handling Copyright (C) 2000, 2003, 2004, 2005 Free Software Foundation, Inc. Contributed by Canqun Yang <canqun@nudt.edu.cn>This file is part of GCC.GCC is free software; you can redistribute it and/or modify it underthe terms of the GNU General Public License as published by the FreeSoftware Foundation; either version 2, or (at your option) any laterversion.GCC is distributed in the hope that it will be useful, but WITHOUT ANYWARRANTY; without even the implied warranty of MERCHANTABILITY orFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public Licensefor more details.You should have received a copy of the GNU General Public Licensealong with GCC; see the file COPYING. If not, write to the FreeSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA02110-1301, USA. */ /* The core algorithm is based on Andy Vaught's g95 tree. Also the way to build UNION_TYPE is borrowed from Richard Henderson. Transform common blocks. An integral part of this is processing equivalence variables. Equivalenced variables that are not in a common block end up in a private block of their own. Each common block or local equivalence list is declared as a union. Variables within the block are represented as a field within the block with the proper offset. So if two variables are equivalenced, they just point to a common area in memory. Mathematically, laying out an equivalence block is equivalent to solving a linear system of equations. The matrix is usually a sparse matrix in which each row contains all zero elements except for a +1 and a -1, a sort of a generalized Vandermonde matrix. The matrix is usually block diagonal. The system can be overdetermined, underdetermined or have a unique solution. If the system is inconsistent, the program is not standard conforming. The solution vector is integral, since all of the pivots are +1 or -1. How we lay out an equivalence block is a little less complicated. In an equivalence list with n elements, there are n-1 conditions to be satisfied. The conditions partition the variables into what we will call segments. If A and B are equivalenced then A and B are in the same segment. If B and C are equivalenced as well, then A, B and C are in a segment and so on. Each segment is a block of memory that has one or more variables equivalenced in some way. A common block is made up of a series of segments that are joined one after the other. In the linear system, a segment is a block diagonal. To lay out a segment we first start with some variable and determine its length. The first variable is assumed to start at offset one and extends to however long it is. We then traverse the list of equivalences to find an unused condition that involves at least one of the variables currently in the segment. Each equivalence condition amounts to the condition B+b=C+c where B and C are the offsets of the B and C variables, and b and c are constants which are nonzero for array elements, substrings or structure components. So for EQUIVALENCE(B(2), C(3)) we have B + 2*size of B's elements = C + 3*size of C's elements. If B and C are known we check to see if the condition already holds. If B is known we can solve for C. Since we know the length of C, we can see if the minimum and maximum extents of the segment are affected. Eventually, we make a full pass through the equivalence list without finding any new conditions and the segment is fully specified. At this point, the segment is added to the current common block. Since we know the minimum extent of the segment, everything in the segment is translated to its position in the common block. The usual case here is that there are no equivalence statements and the common block is series of segments with one variable each, which is a diagonal matrix in the matrix formulation. Each segment is described by a chain of segment_info structures. Each segment_info structure describes the extents of a single varible within the segment. This list is maintained in the order the elements are positioned withing the segment. If two elements have the same starting offset the smaller will come first. If they also have the same size their ordering is undefined. Once all common blocks have been created, the list of equivalences is examined for still-unused equivalence conditions. We create a block for each merged equivalence list. */#include "config.h"#include "system.h"#include "coretypes.h"#include "tree.h"#include "toplev.h"#include "tm.h"#include "gfortran.h"#include "trans.h"#include "trans-types.h"#include "trans-const.h"/* Holds a single variable in an equivalence set. */typedef struct segment_info{ gfc_symbol *sym; HOST_WIDE_INT offset; HOST_WIDE_INT length; /* This will contain the field type until the field is created. */ tree field; struct segment_info *next;} segment_info;static segment_info * current_segment;static gfc_namespace *gfc_common_ns = NULL;/* Make a segment_info based on a symbol. */static segment_info *get_segment_info (gfc_symbol * sym, HOST_WIDE_INT offset){ segment_info *s; /* Make sure we've got the character length. */ if (sym->ts.type == BT_CHARACTER) gfc_conv_const_charlen (sym->ts.cl); /* Create the segment_info and fill it in. */ s = (segment_info *) gfc_getmem (sizeof (segment_info)); s->sym = sym; /* We will use this type when building the segment aggregate type. */ s->field = gfc_sym_type (sym); s->length = int_size_in_bytes (s->field); s->offset = offset; return s;}/* Add combine segment V and segment LIST. */static segment_info *add_segments (segment_info *list, segment_info *v){ segment_info *s; segment_info *p; segment_info *next; p = NULL; s = list; while (v) { /* Find the location of the new element. */ while (s) { if (v->offset < s->offset) break; if (v->offset == s->offset && v->length <= s->length) break; p = s; s = s->next; } /* Insert the new element in between p and s. */ next = v->next; v->next = s; if (p == NULL) list = v; else p->next = v; p = v; v = next; } return list;}/* Construct mangled common block name from symbol name. */static treegfc_sym_mangled_common_id (const char *name){ int has_underscore; char mangled_name[GFC_MAX_MANGLED_SYMBOL_LEN + 1]; if (strcmp (name, BLANK_COMMON_NAME) == 0) return get_identifier (name); if (gfc_option.flag_underscoring) { has_underscore = strchr (name, '_') != 0; if (gfc_option.flag_second_underscore && has_underscore) snprintf (mangled_name, sizeof mangled_name, "%s__", name); else snprintf (mangled_name, sizeof mangled_name, "%s_", name); return get_identifier (mangled_name); } else return get_identifier (name);}/* Build a field declaration for a common variable or a local equivalence object. */static voidbuild_field (segment_info *h, tree union_type, record_layout_info rli){ tree field; tree name; HOST_WIDE_INT offset = h->offset; unsigned HOST_WIDE_INT desired_align, known_align; name = get_identifier (h->sym->name); field = build_decl (FIELD_DECL, name, h->field); gfc_set_decl_location (field, &h->sym->declared_at); known_align = (offset & -offset) * BITS_PER_UNIT; if (known_align == 0 || known_align > BIGGEST_ALIGNMENT) known_align = BIGGEST_ALIGNMENT; desired_align = update_alignment_for_field (rli, field, known_align); if (desired_align > known_align) DECL_PACKED (field) = 1; DECL_FIELD_CONTEXT (field) = union_type; DECL_FIELD_OFFSET (field) = size_int (offset); DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node; SET_DECL_OFFSET_ALIGN (field, known_align); rli->offset = size_binop (MAX_EXPR, rli->offset, size_binop (PLUS_EXPR, DECL_FIELD_OFFSET (field), DECL_SIZE_UNIT (field))); /* If this field is assigned to a label, we create another two variables. One will hold the address of target label or format label. The other will hold the length of format label string. */ if (h->sym->attr.assign) { tree len; tree addr; gfc_allocate_lang_decl (field); GFC_DECL_ASSIGN (field) = 1; len = gfc_create_var_np (gfc_charlen_type_node,h->sym->name); addr = gfc_create_var_np (pvoid_type_node, h->sym->name); TREE_STATIC (len) = 1; TREE_STATIC (addr) = 1; DECL_INITIAL (len) = build_int_cst (NULL_TREE, -2); gfc_set_decl_location (len, &h->sym->declared_at); gfc_set_decl_location (addr, &h->sym->declared_at); GFC_DECL_STRING_LEN (field) = pushdecl_top_level (len); GFC_DECL_ASSIGN_ADDR (field) = pushdecl_top_level (addr); } h->field = field;}/* Get storage for local equivalence. */static treebuild_equiv_decl (tree union_type, bool is_init, bool is_saved){ tree decl; char name[15]; static int serial = 0; if (is_init) { decl = gfc_create_var (union_type, "equiv"); TREE_STATIC (decl) = 1; return decl; } snprintf (name, sizeof (name), "equiv.%d", serial++); decl = build_decl (VAR_DECL, get_identifier (name), union_type); DECL_ARTIFICIAL (decl) = 1; DECL_IGNORED_P (decl) = 1; if (!gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl)) || is_saved) TREE_STATIC (decl) = 1; TREE_ADDRESSABLE (decl) = 1; TREE_USED (decl) = 1; /* The source location has been lost, and doesn't really matter. We need to set it to something though. */ gfc_set_decl_location (decl, &gfc_current_locus); gfc_add_decl_to_function (decl); return decl;}/* Get storage for common block. */static treebuild_common_decl (gfc_common_head *com, tree union_type, bool is_init){ gfc_symbol *common_sym; tree decl; /* Create a namespace to store symbols for common blocks. */ if (gfc_common_ns == NULL) gfc_common_ns = gfc_get_namespace (NULL, 0); gfc_get_symbol (com->name, gfc_common_ns, &common_sym); decl = common_sym->backend_decl; /* Update the size of this common block as needed. */ if (decl != NULL_TREE) { tree size = TYPE_SIZE_UNIT (union_type); if (tree_int_cst_lt (DECL_SIZE_UNIT (decl), size)) { /* Named common blocks of the same name shall be of the same size in all scoping units of a program in which they appear, but blank common blocks may be of different sizes. */ if (strcmp (com->name, BLANK_COMMON_NAME)) gfc_warning ("Named COMMON block '%s' at %L shall be of the " "same size", com->name, &com->where); DECL_SIZE_UNIT (decl) = size; } } /* If this common block has been declared in a previous program unit, and either it is already initialized or there is no new initialization for it, just return. */ if ((decl != NULL_TREE) && (!is_init || DECL_INITIAL (decl))) return decl; /* If there is no backend_decl for the common block, build it. */ if (decl == NULL_TREE) { decl = build_decl (VAR_DECL, get_identifier (com->name), union_type); SET_DECL_ASSEMBLER_NAME (decl, gfc_sym_mangled_common_id (com->name)); TREE_PUBLIC (decl) = 1; TREE_STATIC (decl) = 1; DECL_ALIGN (decl) = BIGGEST_ALIGNMENT; DECL_USER_ALIGN (decl) = 0; gfc_set_decl_location (decl, &com->where); /* Place the back end declaration for this common block in GLOBAL_BINDING_LEVEL. */ common_sym->backend_decl = pushdecl_top_level (decl); } /* Has no initial values. */ if (!is_init) { DECL_INITIAL (decl) = NULL_TREE; DECL_COMMON (decl) = 1; DECL_DEFER_OUTPUT (decl) = 1; } else { DECL_INITIAL (decl) = error_mark_node; DECL_COMMON (decl) = 0; DECL_DEFER_OUTPUT (decl) = 0; } return decl;}/* Declare memory for the common block or local equivalence, and create backend declarations for all of the elements. */static voidcreate_common (gfc_common_head *com, segment_info * head, bool saw_equiv){ segment_info *s, *next_s; tree union_type; tree *field_link; record_layout_info rli; tree decl; bool is_init = false; bool is_saved = false; /* Declare the variables inside the common block. If the current common block contains any equivalence object, then make a UNION_TYPE node, otherwise RECORD_TYPE. This will let the alias analyzer work well when there is no address overlapping for common variables in the current common block. */ if (saw_equiv) union_type = make_node (UNION_TYPE); else union_type = make_node (RECORD_TYPE); rli = start_record_layout (union_type); field_link = &TYPE_FIELDS (union_type); for (s = head; s; s = s->next) { build_field (s, union_type, rli); /* Link the field into the type. */ *field_link = s->field; field_link = &TREE_CHAIN (s->field); /* Has initial value. */ if (s->sym->value) is_init = true; /* Has SAVE attribute. */ if (s->sym->attr.save) is_saved = true; } finish_record_layout (rli, true); if (com) decl = build_common_decl (com, union_type, is_init); else decl = build_equiv_decl (union_type, is_init, is_saved); if (is_init) { tree ctor, tmp; HOST_WIDE_INT offset = 0; VEC(constructor_elt,gc) *v = NULL; for (s = head; s; s = s->next) { if (s->sym->value) { if (s->offset < offset) { /* We have overlapping initializers. It could either be partially initialized arrays (legal), or the user specified multiple initial values (illegal). We don't implement this yet, so bail out. */ gfc_todo_error ("Initialization of overlapping variables"); } /* Add the initializer for this field. */ tmp = gfc_conv_initializer (s->sym->value, &s->sym->ts, TREE_TYPE (s->field), s->sym->attr.dimension, s->sym->attr.pointer || s->sym->attr.allocatable); CONSTRUCTOR_APPEND_ELT (v, s->field, tmp); offset = s->offset + s->length; } } gcc_assert (!VEC_empty (constructor_elt, v)); ctor = build_constructor (union_type, v); TREE_CONSTANT (ctor) = 1; TREE_INVARIANT (ctor) = 1; TREE_STATIC (ctor) = 1; DECL_INITIAL (decl) = ctor;#ifdef ENABLE_CHECKING { tree field, value; unsigned HOST_WIDE_INT idx; FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, field, value) gcc_assert (TREE_CODE (field) == FIELD_DECL); }#endif } /* Build component reference for each variable. */ for (s = head; s; s = next_s) { tree var_decl; var_decl = build_decl (VAR_DECL, DECL_NAME (s->field), TREE_TYPE (s->field)); gfc_set_decl_location (var_decl, &s->sym->declared_at); TREE_PUBLIC (var_decl) = TREE_PUBLIC (decl); TREE_STATIC (var_decl) = TREE_STATIC (decl); TREE_USED (var_decl) = TREE_USED (decl); if (s->sym->attr.target) TREE_ADDRESSABLE (var_decl) = 1; /* This is a fake variable just for debugging purposes. */ TREE_ASM_WRITTEN (var_decl) = 1; if (com) var_decl = pushdecl_top_level (var_decl); else gfc_add_decl_to_function (var_decl); SET_DECL_VALUE_EXPR (var_decl, build3 (COMPONENT_REF, TREE_TYPE (s->field), decl, s->field, NULL_TREE)); DECL_HAS_VALUE_EXPR_P (var_decl) = 1; if (s->sym->attr.assign) { gfc_allocate_lang_decl (var_decl); GFC_DECL_ASSIGN (var_decl) = 1; GFC_DECL_STRING_LEN (var_decl) = GFC_DECL_STRING_LEN (s->field); GFC_DECL_ASSIGN_ADDR (var_decl) = GFC_DECL_ASSIGN_ADDR (s->field); } s->sym->backend_decl = var_decl; next_s = s->next; gfc_free (s);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -