/* Generate code to allocate an array temporary, or create a variable to
- hold the data. */
+ hold the data. If size is NULL zero the descriptor so that so that the
+ callee will allocate the array. Also generates code to free the array
+ afterwards. */
static void
gfc_trans_allocate_array_storage (gfc_loopinfo * loop, gfc_ss_info * info,
desc = info->descriptor;
data = gfc_conv_descriptor_data (desc);
- onstack = gfc_can_put_var_on_stack (size);
- if (onstack)
+ if (size == NULL_TREE)
{
- /* Make a temporary variable to hold the data. */
- tmp = fold (build (MINUS_EXPR, TREE_TYPE (nelem), nelem,
- integer_one_node));
- tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node, tmp);
- tmp = build_array_type (gfc_get_element_type (TREE_TYPE (desc)), tmp);
- tmp = gfc_create_var (tmp, "A");
- tmp = gfc_build_addr_expr (TREE_TYPE (data), tmp);
- gfc_add_modify_expr (&loop->pre, data, tmp);
+ /* A callee allocated array. */
+ gfc_add_modify_expr (&loop->pre, data, convert (TREE_TYPE (data),
+ gfc_index_zero_node));
info->data = data;
info->offset = gfc_index_zero_node;
-
+ onstack = FALSE;
}
else
{
- /* Allocate memory to hold the data. */
- args = gfc_chainon_list (NULL_TREE, size);
+ /* Allocate the temporary. */
+ onstack = gfc_can_put_var_on_stack (size);
- if (gfc_index_integer_kind == 4)
- tmp = gfor_fndecl_internal_malloc;
- else if (gfc_index_integer_kind == 8)
- tmp = gfor_fndecl_internal_malloc64;
+ if (onstack)
+ {
+ /* Make a temporary variable to hold the data. */
+ tmp = fold (build (MINUS_EXPR, TREE_TYPE (nelem), nelem,
+ integer_one_node));
+ tmp = build_range_type (gfc_array_index_type, gfc_index_zero_node,
+ tmp);
+ tmp = build_array_type (gfc_get_element_type (TREE_TYPE (desc)),
+ tmp);
+ tmp = gfc_create_var (tmp, "A");
+ tmp = gfc_build_addr_expr (TREE_TYPE (data), tmp);
+ gfc_add_modify_expr (&loop->pre, data, tmp);
+ info->data = data;
+ info->offset = gfc_index_zero_node;
+
+ }
else
- abort ();
- tmp = gfc_build_function_call (tmp, args);
- tmp = convert (TREE_TYPE (data), tmp);
- gfc_add_modify_expr (&loop->pre, data, tmp);
+ {
+ /* Allocate memory to hold the data. */
+ args = gfc_chainon_list (NULL_TREE, size);
- info->data = data;
- info->offset = gfc_index_zero_node;
+ if (gfc_index_integer_kind == 4)
+ tmp = gfor_fndecl_internal_malloc;
+ else if (gfc_index_integer_kind == 8)
+ tmp = gfor_fndecl_internal_malloc64;
+ else
+ abort ();
+ tmp = gfc_build_function_call (tmp, args);
+ tmp = convert (TREE_TYPE (data), tmp);
+ gfc_add_modify_expr (&loop->pre, data, tmp);
+
+ info->data = data;
+ info->offset = gfc_index_zero_node;
+ }
}
/* The offset is zero because we create temporaries with a zero
/* Generate code to allocate and initialize the descriptor for a temporary
- array. Fills in the descriptor, data and offset fields of info. Also
- adjusts the loop variables to be zero-based. Returns the size of the
- array. */
+ array. This is used for both temporaries needed by the scaparizer, and
+ functions returning arrays. Adjusts the loop variables to be zero-based,
+ and calculates the loop bounds for callee allocated arrays.
+ Also fills in the descriptor, data and offset fields of info if known.
+ Returns the size of the array, or NULL for a callee allocated array. */
tree
gfc_trans_allocate_temp_array (gfc_loopinfo * loop, gfc_ss_info * info,
assert (integer_zerop (loop->from[n]));
else
{
- loop->to[n] = fold (build (MINUS_EXPR, gfc_array_index_type,
+ /* Callee allocated arrays may not have a known bound yet. */
+ if (loop->to[n])
+ loop->to[n] = fold (build (MINUS_EXPR, gfc_array_index_type,
loop->to[n], loop->from[n]));
loop->from[n] = gfc_index_zero_node;
}
for (n = 0; n < info->dimen; n++)
{
+ if (loop->to[n] == NULL_TREE)
+ {
+ /* For a callee allocated array express the loop bounds in terms
+ of the descriptor fields. */
+ tmp = build (MINUS_EXPR, gfc_array_index_type,
+ gfc_conv_descriptor_ubound (desc, gfc_rank_cst[n]),
+ gfc_conv_descriptor_lbound (desc, gfc_rank_cst[n]));
+ loop->to[n] = tmp;
+ size = NULL_TREE;
+ continue;
+ }
+
/* Store the stride and bound components in the descriptor. */
tmp = gfc_conv_descriptor_stride (desc, gfc_rank_cst[n]);
gfc_add_modify_expr (&loop->pre, tmp, size);
/* Get the size of the array. */
nelem = size;
- size = fold (build (MULT_EXPR, gfc_array_index_type, size,
+ if (size)
+ size = fold (build (MULT_EXPR, gfc_array_index_type, size,
TYPE_SIZE_UNIT (gfc_get_element_type (type))));
gfc_trans_allocate_array_storage (loop, info, size, nelem);
p = p->next;
}
- bound = build_int_2 (n - 1, 0);
+ bound = build_int_cst (NULL_TREE, n - 1, 0);
/* Create an array type to hold them. */
tmptype = build_range_type (gfc_array_index_type,
gfc_index_zero_node, bound);
init = gfc_build_addr_expr (NULL, init);
size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
- bound = build_int_2 (n * size, 0);
+ bound = build_int_cst (NULL_TREE, n * size, 0);
tmp = gfc_chainon_list (NULL_TREE, tmp);
tmp = gfc_chainon_list (tmp, init);
tmp = gfc_chainon_list (tmp, bound);
/* Add the pre and post chains for all the scalar expressions in a SS chain
to loop. This is called after the loop parameters have been calculated,
but before the actual scalarizing loops. */
-/*GCC ARRAYS*/
static void
gfc_add_loop_ss_code (gfc_loopinfo * loop, gfc_ss * ss, bool subscript)
gfc_se se;
int n;
+ /* TODO: This can generate bad code if there are ordering dependencies.
+ eg. a callee allocated function and an unknown size constructor. */
assert (ss != NULL);
for (; ss != gfc_ss_terminator; ss = ss->loop_chain)
gfc_trans_array_constructor (loop, ss);
break;
+ case GFC_SS_TEMP:
+ case GFC_SS_COMPONENT:
+ /* Do nothing. These are handled elsewhere. */
+ break;
+
default:
abort ();
}
}
else
{
- /* Temporary array. */
+ /* Temporary array or derived type component. */
assert (se->loop);
index = se->loop->loopvar[se->loop->order[i]];
+ if (!integer_zerop (info->delta[i]))
+ index = fold (build (PLUS_EXPR, gfc_array_index_type, index,
+ info->delta[i]));
}
/* Multiply by the stride. */
continue;
if (ss->type != GFC_SS_SECTION
- && ss->type != GFC_SS_FUNCTION && ss->type != GFC_SS_CONSTRUCTOR)
+ && ss->type != GFC_SS_FUNCTION && ss->type != GFC_SS_CONSTRUCTOR
+ && ss->type != GFC_SS_COMPONENT)
continue;
info = &ss->data.info;
continue;
if (ss->type != GFC_SS_SECTION
- && ss->type != GFC_SS_FUNCTION && ss->type != GFC_SS_CONSTRUCTOR)
+ && ss->type != GFC_SS_FUNCTION && ss->type != GFC_SS_CONSTRUCTOR
+ && ss->type != GFC_SS_COMPONENT)
continue;
ss->data.info.offset = ss->data.info.saved_offset;
case GFC_SS_SECTION:
case GFC_SS_CONSTRUCTOR:
case GFC_SS_FUNCTION:
+ case GFC_SS_COMPONENT:
loop->dimen = ss->data.info.dimen;
break;
/* Loop over all the SS in the chain. */
for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
{
+ if (ss->expr && ss->expr->shape && !ss->shape)
+ ss->shape = ss->expr->shape;
+
switch (ss->type)
{
case GFC_SS_SECTION:
loop for this dimension. We try to pick the simplest term. */
for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
{
- if (ss->expr && ss->expr->shape)
+ if (ss->shape)
{
/* The frontend has worked out the size for us. */
loopspec[n] = ss;
if (ss->type == GFC_SS_CONSTRUCTOR)
{
+ /* An unknown size constructor will always be rank one.
+ Higher rank constructors will wither have known shape,
+ or still be wrapped in a call to reshape. */
+ assert (loop->dimen == 1);
/* Try to figure out the size of the constructor. */
/* TODO: avoid this by making the frontend set the shape. */
gfc_get_array_cons_size (&i, ss->expr->value.constructor);
continue;
}
- /* We don't know how to handle functions yet.
- This may not be possible in all cases. */
+ /* TODO: Pick the best bound if we have a choice between a
+ function and something else. */
+ if (ss->type == GFC_SS_FUNCTION)
+ {
+ loopspec[n] = ss;
+ continue;
+ }
+
if (ss->type != GFC_SS_SECTION)
continue;
- info = &ss->data.info;
-
if (loopspec[n])
specinfo = &loopspec[n]->data.info;
else
*/
if (!specinfo)
loopspec[n] = ss;
+ /* TODO: Is != contructor correct? */
else if (loopspec[n]->type != GFC_SS_CONSTRUCTOR)
{
if (integer_onep (info->stride[n])
info = &loopspec[n]->data.info;
/* Set the extents of this range. */
- cshape = loopspec[n]->expr->shape;
+ cshape = loopspec[n]->shape;
if (cshape && INTEGER_CST_P (info->start[n])
&& INTEGER_CST_P (info->stride[n]))
{
&loop->pre);
break;
+ case GFC_SS_FUNCTION:
+ /* The loop bound will be set when we generate the call. */
+ assert (loop->to[n] == NULL_TREE);
+ break;
+
default:
abort ();
}
}
}
+ /* Add all the scalar code that can be taken out of the loops.
+ This may include calculating the loop bounds, so do it before
+ allocating the temporary. */
+ gfc_add_loop_ss_code (loop, loop->ss, false);
+
/* If we want a temporary then create it. */
if (loop->temp_ss != NULL)
{
tmp, len);
}
- /* Add all the scalar code that can be taken out of the loops. */
- gfc_add_loop_ss_code (loop, loop->ss, false);
-
for (n = 0; n < loop->temp_dim; n++)
loopspec[loop->order[n]] = NULL;
/* Calculate the translation from loop variables to array indices. */
for (ss = loop->ss; ss != gfc_ss_terminator; ss = ss->loop_chain)
{
- if (ss->type != GFC_SS_SECTION)
+ if (ss->type != GFC_SS_SECTION && ss->type != GFC_SS_COMPONENT)
continue;
info = &ss->data.info;
{
dim = info->dim[n];
- /* If we are specifying the range the delta may already be set. */
+ /* If we are specifying the range the delta is already set. */
if (loopspec[n] != ss)
{
/* Calculate the offset relative to the loop variable.
/* A single scalar or derived type value. Create an array with all
elements equal to that value. */
gfc_init_se (&se, NULL);
- gfc_conv_expr (&se, expr);
+
+ if (expr->expr_type == EXPR_CONSTANT)
+ gfc_conv_constant (&se, expr);
+ else
+ gfc_conv_structure (&se, expr, 1);
tmp = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
assert (tmp && INTEGER_CST_P (tmp));
tree fndecl;
tree size;
tree offset;
- tree args;
bool onstack;
assert (!(sym->attr.pointer || sym->attr.allocatable));
{
gfc_trans_init_string_length (sym->ts.cl, &block);
- DECL_DEFER_OUTPUT (decl) = 1;
-
- /* Generate code to allocate the automatic variable. It will be
- freed automatically. */
- tmp = gfc_build_addr_expr (NULL, decl);
- args = gfc_chainon_list (NULL_TREE, tmp);
- args = gfc_chainon_list (args, sym->ts.cl->backend_decl);
- tmp = gfc_build_function_call (built_in_decls[BUILT_IN_STACK_ALLOC],
- args);
+ /* Emit a DECL_EXPR for this variable, which will cause the
+ gimplifier to allocate stoage, and all that good stuff. */
+ tmp = build (DECL_EXPR, TREE_TYPE (decl), decl);
gfc_add_expr_to_block (&block, tmp);
}
int n;
int checkparm;
int no_repack;
+ bool optional_arg;
+
+ /* Do nothing for pointer and allocatable arrays. */
+ if (sym->attr.pointer || sym->attr.allocatable)
+ return body;
if (sym->attr.dummy && gfc_is_nodesc_array (sym))
return gfc_trans_g77_array (sym, body);
/* Only do the entry/initialization code if the arg is present. */
dumdesc = GFC_DECL_SAVED_DESCRIPTOR (tmpdesc);
- if (sym->attr.optional)
+ optional_arg = sym->attr.optional || sym->ns->proc_name->attr.entry_master;
+ if (optional_arg)
{
tmp = gfc_conv_expr_present (sym);
stmt = build_v (COND_EXPR, tmp, stmt, build_empty_stmt ());
tmp = build (NE_EXPR, boolean_type_node, tmp, tmpdesc);
stmt = build_v (COND_EXPR, tmp, stmt, build_empty_stmt ());
- if (sym->attr.optional)
+ if (optional_arg)
{
tmp = gfc_conv_expr_present (sym);
stmt = build_v (COND_EXPR, tmp, stmt, build_empty_stmt ());
tree start;
tree offset;
int full;
+ gfc_ss *vss;
assert (ss != gfc_ss_terminator);
/* TODO: Pass constant array constructors without a temporary. */
- /* If we have a linear array section, we can pass it directly. Otherwise
- we need to copy it into a temporary. */
- if (expr->expr_type == EXPR_VARIABLE)
+ /* Special case things we know we can pass easily. */
+ switch (expr->expr_type)
{
- gfc_ss *vss;
+ case EXPR_VARIABLE:
+ /* If we have a linear array section, we can pass it directly.
+ Otherwise we need to copy it into a temporary. */
/* Find the SS for the array section. */
secss = ss;
else if (se->want_pointer)
{
/* We pass full arrays directly. This means that pointers and
- allocatable arrays should also work. */
- se->expr = gfc_build_addr_expr (NULL, desc);
+ allocatable arrays should also work. */
+ se->expr = gfc_build_addr_expr (NULL_TREE, desc);
}
else
{
se->string_length = expr->symtree->n.sym->ts.cl->backend_decl;
return;
}
- }
- else
- {
+ break;
+
+ case EXPR_FUNCTION:
+ /* A transformational function return value will be a temporary
+ array descriptor. We still need to go through the scalarizer
+ to create the descriptor. Elemental functions ar handled as
+ arbitary expressions, ie. copy to a temporary. */
+ secss = ss;
+ /* Look for the SS for this function. */
+ while (secss != gfc_ss_terminator
+ && (secss->type != GFC_SS_FUNCTION || secss->expr != expr))
+ secss = secss->next;
+
+ if (se->direct_byref)
+ {
+ assert (secss != gfc_ss_terminator);
+
+ /* For pointer assignments pass the descriptor directly. */
+ se->ss = secss;
+ se->expr = gfc_build_addr_expr (NULL, se->expr);
+ gfc_conv_expr (se, expr);
+ return;
+ }
+
+ if (secss == gfc_ss_terminator)
+ {
+ /* Elemental function. */
+ need_tmp = 1;
+ info = NULL;
+ }
+ else
+ {
+ /* Transformational function. */
+ info = &secss->data.info;
+ need_tmp = 0;
+ }
+ break;
+
+ default:
+ /* Something complicated. Copy it into a temporary. */
need_tmp = 1;
secss = NULL;
info = NULL;
+ break;
}
+
gfc_init_loopinfo (&loop);
/* Associate the SS with the loop. */
assert (is_gimple_lvalue (desc));
se->expr = gfc_build_addr_expr (NULL, desc);
}
+ else if (expr->expr_type == EXPR_FUNCTION)
+ {
+ desc = info->descriptor;
+
+ if (se->want_pointer)
+ se->expr = gfc_build_addr_expr (NULL_TREE, desc);
+ else
+ se->expr = desc;
+
+ if (expr->ts.type == BT_CHARACTER)
+ se->string_length = expr->symtree->n.sym->ts.cl->backend_decl;
+ }
else
{
- /* We pass sections without copying to a temporary. A function may
- decide to repack the array to speed up access, but we're not
- bothered about that here. */
+ /* We pass sections without copying to a temporary. Make a new
+ descriptor and point it at the section we want. The loop variable
+ limits will be the limits of the section.
+ A function may decide to repack the array to speed up access, but
+ we're not bothered about that here. */
int dim;
tree parm;
tree parmtype;
tree to;
tree base;
- /* set the string_length for a character array. */
+ /* Set the string_length for a character array. */
if (expr->ts.type == BT_CHARACTER)
se->string_length = expr->symtree->n.sym->ts.cl->backend_decl;
- /* Otherwise make a new descriptor and point it at the section we
- want. The loop variable limits will be the limits of the section.
- */
desc = info->descriptor;
assert (secss && secss != gfc_ss_terminator);
if (se->direct_byref)