Blame SOURCES/gdb-rhbz1964167-fortran-array-slices-at-prompt.patch

a8223e
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
a8223e
From: Kevin Buettner <kevinb@redhat.com>
a8223e
Date: Mon, 24 May 2021 22:46:21 -0700
a8223e
Subject: gdb-rhbz1964167-fortran-array-slices-at-prompt.patch
a8223e
a8223e
;; [fortran] Backport Andrew Burgess's commit for Fortran array
a8223e
;; slice support
a8223e
a8223e
gdb/fortran: Add support for Fortran array slices at the GDB prompt
a8223e
a8223e
This commit brings array slice support to GDB.
a8223e
a8223e
WARNING: This patch contains a rather big hack which is limited to
a8223e
Fortran arrays, this can be seen in gdbtypes.c and f-lang.c.  More
a8223e
details on this below.
a8223e
a8223e
This patch rewrites two areas of GDB's Fortran support, the code to
a8223e
extract an array slice, and the code to print an array.
a8223e
a8223e
After this commit a user can, from the GDB prompt, ask for a slice of
a8223e
a Fortran array and should get the correct result back.  Slices can
a8223e
(optionally) have the lower bound, upper bound, and a stride
a8223e
specified.  Slices can also have a negative stride.
a8223e
a8223e
Fortran has the concept of repacking array slices.  Within a compiled
a8223e
Fortran program if a user passes a non-contiguous array slice to a
a8223e
function then the compiler may have to repack the slice, this involves
a8223e
copying the elements of the slice to a new area of memory before the
a8223e
call, and copying the elements back to the original array after the
a8223e
call.  Whether repacking occurs will depend on which version of
a8223e
Fortran is being used, and what type of function is being called.
a8223e
a8223e
This commit adds support for both packed, and unpacked array slicing,
a8223e
with the default being unpacked.
a8223e
a8223e
With an unpacked array slice, when the user asks for a slice of an
a8223e
array GDB creates a new type that accurately describes where the
a8223e
elements of the slice can be found within the original array, a
a8223e
value of this type is then returned to the user.  The address of an
a8223e
element within the slice will be equal to the address of an element
a8223e
within the original array.
a8223e
a8223e
A user can choose to select packed array slices instead using:
a8223e
a8223e
  (gdb) set fortran repack-array-slices on|off
a8223e
  (gdb) show fortran repack-array-slices
a8223e
a8223e
With packed array slices GDB creates a new type that reflects how the
a8223e
elements of the slice would look if they were laid out in contiguous
a8223e
memory, allocates a value of this type, and then fetches the elements
a8223e
from the original array and places then into the contents buffer of
a8223e
the new value.
a8223e
a8223e
One benefit of using packed slices over unpacked slices is the memory
a8223e
usage, taking a small slice of N elements from a large array will
a8223e
require (in GDB) N * ELEMENT_SIZE bytes of memory, while an unpacked
a8223e
array will also include all of the "padding" between the
a8223e
non-contiguous elements.  There are new tests added that highlight
a8223e
this difference.
a8223e
a8223e
There is also a new debugging flag added with this commit that
a8223e
introduces these commands:
a8223e
a8223e
  (gdb) set debug fortran-array-slicing on|off
a8223e
  (gdb) show debug fortran-array-slicing
a8223e
a8223e
This prints information about how the array slices are being built.
a8223e
a8223e
As both the repacking, and the array printing requires GDB to walk
a8223e
through a multi-dimensional Fortran array visiting each element, this
a8223e
commit adds the file f-array-walk.h, which introduces some
a8223e
infrastructure to support this process.  This means the array printing
a8223e
code in f-valprint.c is significantly reduced.
a8223e
a8223e
The only slight issue with this commit is the "rather big hack" that I
a8223e
mentioned above.  This hack allows us to handle one specific case,
a8223e
array slices with negative strides.  This is something that I don't
a8223e
believe the current GDB value contents model will allow us to
a8223e
correctly handle, and rather than rewrite the value contents code
a8223e
right now, I'm hoping to slip this hack in as a work around.
a8223e
a8223e
The problem is that, as I see it, the current value contents model
a8223e
assumes that an object base address will be the lowest address within
a8223e
that object, and that the contents of the object start at this base
a8223e
address and occupy the TYPE_LENGTH bytes after that.
a8223e
a8223e
( We do have the embedded_offset, which is used for C++ sub-classes,
a8223e
such that an object can start at some offset from the content buffer,
a8223e
however, the assumption that the object then occupies the next
a8223e
TYPE_LENGTH bytes is still true within GDB. )
a8223e
a8223e
The problem is that Fortran arrays with a negative stride don't follow
a8223e
this pattern.  In this case the base address of the object points to
a8223e
the element with the highest address, the contents of the array then
a8223e
start at some offset _before_ the base address, and proceed for one
a8223e
element _past_ the base address.
a8223e
a8223e
As the stride for such an array would be negative then, in theory the
a8223e
TYPE_LENGTH for this type would also be negative.  However, in many
a8223e
places a value in GDB will degrade to a pointer + length, and the
a8223e
length almost always comes from the TYPE_LENGTH.
a8223e
a8223e
It is my belief that in order to correctly model this case the value
a8223e
content handling of GDB will need to be reworked to split apart the
a8223e
value's content buffer (which is a block of memory with a length), and
a8223e
the object's in memory base address and length, which could be
a8223e
negative.
a8223e
a8223e
Things are further complicated because arrays with negative strides
a8223e
like this are always dynamic types.  When a value has a dynamic type
a8223e
and its base address needs resolving we actually store the address of
a8223e
the object within the resolved dynamic type, not within the value
a8223e
object itself.
a8223e
a8223e
In short I don't currently see an easy path to cleanly support this
a8223e
situation within GDB.  And so I believe that leaves two options,
a8223e
either add a work around, or catch cases where the user tries to make
a8223e
use of a negative stride, or access an array with a negative stride,
a8223e
and throw an error.
a8223e
a8223e
This patch currently goes with adding a work around, which is that
a8223e
when we resolve a dynamic Fortran array type, if the stride is
a8223e
negative, then we adjust the base address to point to the lowest
a8223e
address required by the array.  The printing and slicing code is aware
a8223e
of this adjustment and will correctly slice and print Fortran arrays.
a8223e
a8223e
Where this hack will show through to the user is if they ask for the
a8223e
address of an array in their program with a negative array stride, the
a8223e
address they get from GDB will not match the address that would be
a8223e
computed within the Fortran program.
a8223e
a8223e
gdb/ChangeLog:
a8223e
a8223e
	* Makefile.in (HFILES_NO_SRCDIR): Add f-array-walker.h.
a8223e
	* NEWS: Mention new options.
a8223e
	* f-array-walker.h: New file.
a8223e
	* f-lang.c: Include 'gdbcmd.h' and 'f-array-walker.h'.
a8223e
	(repack_array_slices): New static global.
a8223e
	(show_repack_array_slices): New function.
a8223e
	(fortran_array_slicing_debug): New static global.
a8223e
	(show_fortran_array_slicing_debug): New function.
a8223e
	(value_f90_subarray): Delete.
a8223e
	(skip_undetermined_arglist): Delete.
a8223e
	(class fortran_array_repacker_base_impl): New class.
a8223e
	(class fortran_lazy_array_repacker_impl): New class.
a8223e
	(class fortran_array_repacker_impl): New class.
a8223e
	(fortran_value_subarray): Complete rewrite.
a8223e
	(set_fortran_list): New static global.
a8223e
	(show_fortran_list): Likewise.
a8223e
	(_initialize_f_language): Register new commands.
a8223e
	(fortran_adjust_dynamic_array_base_address_hack): New function.
a8223e
	* f-lang.h (fortran_adjust_dynamic_array_base_address_hack):
a8223e
	Declare.
a8223e
	* f-valprint.c: Include 'f-array-walker.h'.
a8223e
	(class fortran_array_printer_impl): New class.
a8223e
	(f77_print_array_1): Delete.
a8223e
	(f77_print_array): Delete.
a8223e
	(fortran_print_array): New.
a8223e
	(f_value_print_inner): Update to call fortran_print_array.
a8223e
	* gdbtypes.c: Include 'f-lang.h'.
a8223e
	(resolve_dynamic_type_internal): Call
a8223e
	fortran_adjust_dynamic_array_base_address_hack.
a8223e
a8223e
gdb/testsuite/ChangeLog:
a8223e
a8223e
        * gdb.fortran/array-slices-bad.exp: New file.
a8223e
        * gdb.fortran/array-slices-bad.f90: New file.
a8223e
        * gdb.fortran/array-slices-sub-slices.exp: New file.
a8223e
        * gdb.fortran/array-slices-sub-slices.f90: New file.
a8223e
        * gdb.fortran/array-slices.exp: Rewrite tests.
a8223e
        * gdb.fortran/array-slices.f90: Rewrite tests.
a8223e
        * gdb.fortran/vla-sizeof.exp: Correct expected results.
a8223e
a8223e
gdb/doc/ChangeLog:
a8223e
a8223e
        * gdb.texinfo (Debugging Output): Document 'set/show debug
a8223e
        fortran-array-slicing'.
a8223e
        (Special Fortran Commands): Document 'set/show fortran
a8223e
        repack-array-slices'.
a8223e
a8223e
diff --git a/gdb/Makefile.in b/gdb/Makefile.in
a8223e
--- a/gdb/Makefile.in
a8223e
+++ b/gdb/Makefile.in
a8223e
@@ -1268,6 +1268,7 @@ HFILES_NO_SRCDIR = \
a8223e
 	expression.h \
a8223e
 	extension.h \
a8223e
 	extension-priv.h \
a8223e
+	f-array-walker.h \
a8223e
 	f-lang.h \
a8223e
 	fbsd-nat.h \
a8223e
 	fbsd-tdep.h \
a8223e
diff --git a/gdb/NEWS b/gdb/NEWS
a8223e
--- a/gdb/NEWS
a8223e
+++ b/gdb/NEWS
a8223e
@@ -111,6 +111,19 @@ maintenance print core-file-backed-mappings
a8223e
   Prints file-backed mappings loaded from a core file's note section.
a8223e
   Output is expected to be similar to that of "info proc mappings".
a8223e
 
a8223e
+set debug fortran-array-slicing on|off
a8223e
+show debug fortran-array-slicing
a8223e
+  Print debugging when taking slices of Fortran arrays.
a8223e
+
a8223e
+set fortran repack-array-slices on|off
a8223e
+show fortran repack-array-slices
a8223e
+  When taking slices from Fortran arrays and strings, if the slice is
a8223e
+  non-contiguous within the original value then, when this option is
a8223e
+  on, the new value will be repacked into a single contiguous value.
a8223e
+  When this option is off, then the value returned will consist of a
a8223e
+  descriptor that describes the slice within the memory of the
a8223e
+  original parent value.
a8223e
+
a8223e
 * Changed commands
a8223e
 
a8223e
 alias [-a] [--] ALIAS = COMMAND [DEFAULT-ARGS...]
a8223e
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
a8223e
--- a/gdb/doc/gdb.texinfo
a8223e
+++ b/gdb/doc/gdb.texinfo
a8223e
@@ -16919,6 +16919,29 @@ This command prints the values contained in the Fortran @code{COMMON}
a8223e
 block whose name is @var{common-name}.  With no argument, the names of
a8223e
 all @code{COMMON} blocks visible at the current program location are
a8223e
 printed.
a8223e
+@cindex arrays slices (Fortran)
a8223e
+@kindex set fortran repack-array-slices
a8223e
+@kindex show fortran repack-array-slices
a8223e
+@item set fortran repack-array-slices [on|off]
a8223e
+@item show fortran repack-array-slices
a8223e
+When taking a slice from an array, a Fortran compiler can choose to
a8223e
+either produce an array descriptor that describes the slice in place,
a8223e
+or it may repack the slice, copying the elements of the slice into a
a8223e
+new region of memory.
a8223e
+
a8223e
+When this setting is on, then @value{GDBN} will also repack array
a8223e
+slices in some situations.  When this setting is off, then
a8223e
+@value{GDBN} will create array descriptors for slices that reference
a8223e
+the original data in place.
a8223e
+
a8223e
+@value{GDBN} will never repack an array slice if the data for the
a8223e
+slice is contiguous within the original array.
a8223e
+
a8223e
+@value{GDBN} will always repack string slices if the data for the
a8223e
+slice is non-contiguous within the original string as @value{GDBN}
a8223e
+does not support printing non-contiguous strings.
a8223e
+
a8223e
+The default for this setting is @code{off}.
a8223e
 @end table
a8223e
 
a8223e
 @node Pascal
a8223e
@@ -26507,6 +26530,16 @@ Show the current state of FreeBSD LWP debugging messages.
a8223e
 Turns on or off debugging messages from the FreeBSD native target.
a8223e
 @item show debug fbsd-nat
a8223e
 Show the current state of FreeBSD native target debugging messages.
a8223e
+
a8223e
+@item set debug fortran-array-slicing
a8223e
+@cindex fortran array slicing debugging info
a8223e
+Turns on or off display of @value{GDBN} Fortran array slicing
a8223e
+debugging info.  The default is off.
a8223e
+
a8223e
+@item show debug fortran-array-slicing
a8223e
+Displays the current state of displaying @value{GDBN} Fortran array
a8223e
+slicing debugging info.
a8223e
+
a8223e
 @item set debug frame
a8223e
 @cindex frame debugging info
a8223e
 Turns on or off display of @value{GDBN} frame debugging info.  The
a8223e
diff --git a/gdb/f-array-walker.h b/gdb/f-array-walker.h
a8223e
new file mode 100644
a8223e
--- /dev/null
a8223e
+++ b/gdb/f-array-walker.h
a8223e
@@ -0,0 +1,265 @@
a8223e
+/* Copyright (C) 2020 Free Software Foundation, Inc.
a8223e
+
a8223e
+   This file is part of GDB.
a8223e
+
a8223e
+   This program is free software; you can redistribute it and/or modify
a8223e
+   it under the terms of the GNU General Public License as published by
a8223e
+   the Free Software Foundation; either version 3 of the License, or
a8223e
+   (at your option) any later version.
a8223e
+
a8223e
+   This program is distributed in the hope that it will be useful,
a8223e
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
a8223e
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a8223e
+   GNU General Public License for more details.
a8223e
+
a8223e
+   You should have received a copy of the GNU General Public License
a8223e
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
a8223e
+
a8223e
+/* Support classes to wrap up the process of iterating over a
a8223e
+   multi-dimensional Fortran array.  */
a8223e
+
a8223e
+#ifndef F_ARRAY_WALKER_H
a8223e
+#define F_ARRAY_WALKER_H
a8223e
+
a8223e
+#include "defs.h"
a8223e
+#include "gdbtypes.h"
a8223e
+#include "f-lang.h"
a8223e
+
a8223e
+/* Class for calculating the byte offset for elements within a single
a8223e
+   dimension of a Fortran array.  */
a8223e
+class fortran_array_offset_calculator
a8223e
+{
a8223e
+public:
a8223e
+  /* Create a new offset calculator for TYPE, which is either an array or a
a8223e
+     string.  */
a8223e
+  explicit fortran_array_offset_calculator (struct type *type)
a8223e
+  {
a8223e
+    /* Validate the type.  */
a8223e
+    type = check_typedef (type);
a8223e
+    if (type->code () != TYPE_CODE_ARRAY
a8223e
+	&& (type->code () != TYPE_CODE_STRING))
a8223e
+      error (_("can only compute offsets for arrays and strings"));
a8223e
+
a8223e
+    /* Get the range, and extract the bounds.  */
a8223e
+    struct type *range_type = type->index_type ();
a8223e
+    if (!get_discrete_bounds (range_type, &m_lowerbound, &m_upperbound))
a8223e
+      error ("unable to read array bounds");
a8223e
+
a8223e
+    /* Figure out the stride for this array.  */
a8223e
+    struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (type));
a8223e
+    m_stride = type->index_type ()->bounds ()->bit_stride ();
a8223e
+    if (m_stride == 0)
a8223e
+      m_stride = type_length_units (elt_type);
a8223e
+    else
a8223e
+      {
a8223e
+	struct gdbarch *arch = get_type_arch (elt_type);
a8223e
+	int unit_size = gdbarch_addressable_memory_unit_size (arch);
a8223e
+	m_stride /= (unit_size * 8);
a8223e
+      }
a8223e
+  };
a8223e
+
a8223e
+  /* Get the byte offset for element INDEX within the type we are working
a8223e
+     on.  There is no bounds checking done on INDEX.  If the stride is
a8223e
+     negative then we still assume that the base address (for the array
a8223e
+     object) points to the element with the lowest memory address, we then
a8223e
+     calculate an offset assuming that index 0 will be the element at the
a8223e
+     highest address, index 1 the next highest, and so on.  This is not
a8223e
+     quite how Fortran works in reality; in reality the base address of
a8223e
+     the object would point at the element with the highest address, and
a8223e
+     we would index backwards from there in the "normal" way, however,
a8223e
+     GDB's current value contents model doesn't support having the base
a8223e
+     address be near to the end of the value contents, so we currently
a8223e
+     adjust the base address of Fortran arrays with negative strides so
a8223e
+     their base address points at the lowest memory address.  This code
a8223e
+     here is part of working around this weirdness.  */
a8223e
+  LONGEST index_offset (LONGEST index)
a8223e
+  {
a8223e
+    LONGEST offset;
a8223e
+    if (m_stride < 0)
a8223e
+      offset = std::abs (m_stride) * (m_upperbound - index);
a8223e
+    else
a8223e
+      offset = std::abs (m_stride) * (index - m_lowerbound);
a8223e
+    return offset;
a8223e
+  }
a8223e
+
a8223e
+private:
a8223e
+
a8223e
+  /* The stride for the type we are working with.  */
a8223e
+  LONGEST m_stride;
a8223e
+
a8223e
+  /* The upper bound for the type we are working with.  */
a8223e
+  LONGEST m_upperbound;
a8223e
+
a8223e
+  /* The lower bound for the type we are working with.  */
a8223e
+  LONGEST m_lowerbound;
a8223e
+};
a8223e
+
a8223e
+/* A base class used by fortran_array_walker.  There's no virtual methods
a8223e
+   here, sub-classes should just override the functions they want in order
a8223e
+   to specialise the behaviour to their needs.  The functionality
a8223e
+   provided in these default implementations will visit every array
a8223e
+   element, but do nothing for each element.  */
a8223e
+
a8223e
+struct fortran_array_walker_base_impl
a8223e
+{
a8223e
+  /* Called when iterating between the lower and upper bounds of each
a8223e
+     dimension of the array.  Return true if GDB should continue iterating,
a8223e
+     otherwise, return false.
a8223e
+
a8223e
+     SHOULD_CONTINUE indicates if GDB is going to stop anyway, and should
a8223e
+     be taken into consideration when deciding what to return.  If
a8223e
+     SHOULD_CONTINUE is false then this function must also return false,
a8223e
+     the function is still called though in case extra work needs to be
a8223e
+     done as part of the stopping process.  */
a8223e
+  bool continue_walking (bool should_continue)
a8223e
+  { return should_continue; }
a8223e
+
a8223e
+  /* Called when GDB starts iterating over a dimension of the array.  The
a8223e
+     argument INNER_P is true for the inner most dimension (the dimension
a8223e
+     containing the actual elements of the array), and false for more outer
a8223e
+     dimensions.  For a concrete example of how this function is called
a8223e
+     see the comment on process_element below.  */
a8223e
+  void start_dimension (bool inner_p)
a8223e
+  { /* Nothing.  */ }
a8223e
+
a8223e
+  /* Called when GDB finishes iterating over a dimension of the array.  The
a8223e
+     argument INNER_P is true for the inner most dimension (the dimension
a8223e
+     containing the actual elements of the array), and false for more outer
a8223e
+     dimensions.  LAST_P is true for the last call at a particular
a8223e
+     dimension.  For a concrete example of how this function is called
a8223e
+     see the comment on process_element below.  */
a8223e
+  void finish_dimension (bool inner_p, bool last_p)
a8223e
+  { /* Nothing.  */ }
a8223e
+
a8223e
+  /* Called when processing the inner most dimension of the array, for
a8223e
+     every element in the array.  ELT_TYPE is the type of the element being
a8223e
+     extracted, and ELT_OFF is the offset of the element from the start of
a8223e
+     array being walked, and LAST_P is true only when this is the last
a8223e
+     element that will be processed in this dimension.
a8223e
+
a8223e
+     Given this two dimensional array ((1, 2) (3, 4)), the calls to
a8223e
+     start_dimension, process_element, and finish_dimension look like this:
a8223e
+
a8223e
+     start_dimension (false);
a8223e
+       start_dimension (true);
a8223e
+         process_element (TYPE, OFFSET, false);
a8223e
+         process_element (TYPE, OFFSET, true);
a8223e
+       finish_dimension (true, false);
a8223e
+       start_dimension (true);
a8223e
+         process_element (TYPE, OFFSET, false);
a8223e
+         process_element (TYPE, OFFSET, true);
a8223e
+       finish_dimension (true, true);
a8223e
+     finish_dimension (false, true);  */
a8223e
+  void process_element (struct type *elt_type, LONGEST elt_off, bool last_p)
a8223e
+  { /* Nothing.  */ }
a8223e
+};
a8223e
+
a8223e
+/* A class to wrap up the process of iterating over a multi-dimensional
a8223e
+   Fortran array.  IMPL is used to specialise what happens as we walk over
a8223e
+   the array.  See class FORTRAN_ARRAY_WALKER_BASE_IMPL (above) for the
a8223e
+   methods than can be used to customise the array walk.  */
a8223e
+template<typename Impl>
a8223e
+class fortran_array_walker
a8223e
+{
a8223e
+  /* Ensure that Impl is derived from the required base class.  This just
a8223e
+     ensures that all of the required API methods are available and have a
a8223e
+     sensible default implementation.  */
a8223e
+  gdb_static_assert ((std::is_base_of<fortran_array_walker_base_impl,Impl>::value));
a8223e
+
a8223e
+public:
a8223e
+  /* Create a new array walker.  TYPE is the type of the array being walked
a8223e
+     over, and ADDRESS is the base address for the object of TYPE in
a8223e
+     memory.  All other arguments are forwarded to the constructor of the
a8223e
+     template parameter class IMPL.  */
a8223e
+  template <typename ...Args>
a8223e
+  fortran_array_walker (struct type *type, CORE_ADDR address,
a8223e
+			Args... args)
a8223e
+    : m_type (type),
a8223e
+      m_address (address),
a8223e
+      m_impl (type, address, args...)
a8223e
+  {
a8223e
+    m_ndimensions =  calc_f77_array_dims (m_type);
a8223e
+  }
a8223e
+
a8223e
+  /* Walk the array.  */
a8223e
+  void
a8223e
+  walk ()
a8223e
+  {
a8223e
+    walk_1 (1, m_type, 0, false);
a8223e
+  }
a8223e
+
a8223e
+private:
a8223e
+  /* The core of the array walking algorithm.  NSS is the current
a8223e
+     dimension number being processed, TYPE is the type of this dimension,
a8223e
+     and OFFSET is the offset (in bytes) for the start of this dimension.  */
a8223e
+  void
a8223e
+  walk_1 (int nss, struct type *type, int offset, bool last_p)
a8223e
+  {
a8223e
+    /* Extract the range, and get lower and upper bounds.  */
a8223e
+    struct type *range_type = check_typedef (type)->index_type ();
a8223e
+    LONGEST lowerbound, upperbound;
a8223e
+    if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
a8223e
+      error ("failed to get range bounds");
a8223e
+
a8223e
+    /* CALC is used to calculate the offsets for each element in this
a8223e
+       dimension.  */
a8223e
+    fortran_array_offset_calculator calc (type);
a8223e
+
a8223e
+    m_impl.start_dimension (nss == m_ndimensions);
a8223e
+
a8223e
+    if (nss != m_ndimensions)
a8223e
+      {
a8223e
+	/* For dimensions other than the inner most, walk each element and
a8223e
+	   recurse while peeling off one more dimension of the array.  */
a8223e
+	for (LONGEST i = lowerbound;
a8223e
+	     m_impl.continue_walking (i < upperbound + 1);
a8223e
+	     i++)
a8223e
+	  {
a8223e
+	    /* Use the index and the stride to work out a new offset.  */
a8223e
+	    LONGEST new_offset = offset + calc.index_offset (i);
a8223e
+
a8223e
+	    /* Now print the lower dimension.  */
a8223e
+	    struct type *subarray_type
a8223e
+	      = TYPE_TARGET_TYPE (check_typedef (type));
a8223e
+	    walk_1 (nss + 1, subarray_type, new_offset, (i == upperbound));
a8223e
+	  }
a8223e
+      }
a8223e
+    else
a8223e
+      {
a8223e
+	/* For the inner most dimension of the array, process each element
a8223e
+	   within this dimension.  */
a8223e
+	for (LONGEST i = lowerbound;
a8223e
+	     m_impl.continue_walking (i < upperbound + 1);
a8223e
+	     i++)
a8223e
+	  {
a8223e
+	    LONGEST elt_off = offset + calc.index_offset (i);
a8223e
+
a8223e
+	    struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (type));
a8223e
+	    if (is_dynamic_type (elt_type))
a8223e
+	      {
a8223e
+		CORE_ADDR e_address = m_address + elt_off;
a8223e
+		elt_type = resolve_dynamic_type (elt_type, {}, e_address);
a8223e
+	      }
a8223e
+
a8223e
+	    m_impl.process_element (elt_type, elt_off, (i == upperbound));
a8223e
+	  }
a8223e
+      }
a8223e
+
a8223e
+    m_impl.finish_dimension (nss == m_ndimensions, last_p || nss == 1);
a8223e
+  }
a8223e
+
a8223e
+  /* The array type being processed.  */
a8223e
+  struct type *m_type;
a8223e
+
a8223e
+  /* The address in target memory for the object of M_TYPE being
a8223e
+     processed.  This is required in order to resolve dynamic types.  */
a8223e
+  CORE_ADDR m_address;
a8223e
+
a8223e
+  /* An instance of the template specialisation class.  */
a8223e
+  Impl m_impl;
a8223e
+
a8223e
+  /* The total number of dimensions in M_TYPE.  */
a8223e
+  int m_ndimensions;
a8223e
+};
a8223e
+
a8223e
+#endif /* F_ARRAY_WALKER_H */
a8223e
diff --git a/gdb/f-lang.c b/gdb/f-lang.c
a8223e
--- a/gdb/f-lang.c
a8223e
+++ b/gdb/f-lang.c
a8223e
@@ -36,9 +36,36 @@
a8223e
 #include "c-lang.h"
a8223e
 #include "target-float.h"
a8223e
 #include "gdbarch.h"
a8223e
+#include "gdbcmd.h"
a8223e
+#include "f-array-walker.h"
a8223e
 
a8223e
 #include <math.h>
a8223e
 
a8223e
+/* Whether GDB should repack array slices created by the user.  */
a8223e
+static bool repack_array_slices = false;
a8223e
+
a8223e
+/* Implement 'show fortran repack-array-slices'.  */
a8223e
+static void
a8223e
+show_repack_array_slices (struct ui_file *file, int from_tty,
a8223e
+			  struct cmd_list_element *c, const char *value)
a8223e
+{
a8223e
+  fprintf_filtered (file, _("Repacking of Fortran array slices is %s.\n"),
a8223e
+		    value);
a8223e
+}
a8223e
+
a8223e
+/* Debugging of Fortran's array slicing.  */
a8223e
+static bool fortran_array_slicing_debug = false;
a8223e
+
a8223e
+/* Implement 'show debug fortran-array-slicing'.  */
a8223e
+static void
a8223e
+show_fortran_array_slicing_debug (struct ui_file *file, int from_tty,
a8223e
+				  struct cmd_list_element *c,
a8223e
+				  const char *value)
a8223e
+{
a8223e
+  fprintf_filtered (file, _("Debugging of Fortran array slicing is %s.\n"),
a8223e
+		    value);
a8223e
+}
a8223e
+
a8223e
 /* Local functions */
a8223e
 
a8223e
 /* Return the encoding that should be used for the character type
a8223e
@@ -114,57 +141,6 @@ enum f_primitive_types {
a8223e
   nr_f_primitive_types
a8223e
 };
a8223e
 
a8223e
-/* Called from fortran_value_subarray to take a slice of an array or a
a8223e
-   string.  ARRAY is the array or string to be accessed.  EXP, POS, and
a8223e
-   NOSIDE are as for evaluate_subexp_standard.  Return a value that is a
a8223e
-   slice of the array.  */
a8223e
-
a8223e
-static struct value *
a8223e
-value_f90_subarray (struct value *array,
a8223e
-		    struct expression *exp, int *pos, enum noside noside)
a8223e
-{
a8223e
-  int pc = (*pos) + 1;
a8223e
-  LONGEST low_bound, high_bound, stride;
a8223e
-  struct type *range = check_typedef (value_type (array)->index_type ());
a8223e
-  enum range_flag range_flag
a8223e
-    = (enum range_flag) longest_to_int (exp->elts[pc].longconst);
a8223e
-
a8223e
-  *pos += 3;
a8223e
-
a8223e
-  if (range_flag & RANGE_LOW_BOUND_DEFAULT)
a8223e
-    low_bound = range->bounds ()->low.const_val ();
a8223e
-  else
a8223e
-    low_bound = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
a8223e
-
a8223e
-  if (range_flag & RANGE_HIGH_BOUND_DEFAULT)
a8223e
-    high_bound = range->bounds ()->high.const_val ();
a8223e
-  else
a8223e
-    high_bound = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
a8223e
-
a8223e
-  if (range_flag & RANGE_HAS_STRIDE)
a8223e
-    stride = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
a8223e
-  else
a8223e
-    stride = 1;
a8223e
-
a8223e
-  if (stride != 1)
a8223e
-    error (_("Fortran array strides are not currently supported"));
a8223e
-
a8223e
-  return value_slice (array, low_bound, high_bound - low_bound + 1);
a8223e
-}
a8223e
-
a8223e
-/* Helper for skipping all the arguments in an undetermined argument list.
a8223e
-   This function was designed for use in the OP_F77_UNDETERMINED_ARGLIST
a8223e
-   case of evaluate_subexp_standard as multiple, but not all, code paths
a8223e
-   require a generic skip.  */
a8223e
-
a8223e
-static void
a8223e
-skip_undetermined_arglist (int nargs, struct expression *exp, int *pos,
a8223e
-			   enum noside noside)
a8223e
-{
a8223e
-  for (int i = 0; i < nargs; ++i)
a8223e
-    evaluate_subexp (nullptr, exp, pos, noside);
a8223e
-}
a8223e
-
a8223e
 /* Return the number of dimensions for a Fortran array or string.  */
a8223e
 
a8223e
 int
a8223e
@@ -189,6 +165,145 @@ enum f_primitive_types {
a8223e
   return ndimen;
a8223e
 }
a8223e
 
a8223e
+/* A class used by FORTRAN_VALUE_SUBARRAY when repacking Fortran array
a8223e
+   slices.  This is a base class for two alternative repacking mechanisms,
a8223e
+   one for when repacking from a lazy value, and one for repacking from a
a8223e
+   non-lazy (already loaded) value.  */
a8223e
+class fortran_array_repacker_base_impl
a8223e
+  : public fortran_array_walker_base_impl
a8223e
+{
a8223e
+public:
a8223e
+  /* Constructor, DEST is the value we are repacking into.  */
a8223e
+  fortran_array_repacker_base_impl (struct value *dest)
a8223e
+    : m_dest (dest),
a8223e
+      m_dest_offset (0)
a8223e
+  { /* Nothing.  */ }
a8223e
+
a8223e
+  /* When we start processing the inner most dimension, this is where we
a8223e
+     will be creating values for each element as we load them and then copy
a8223e
+     them into the M_DEST value.  Set a value mark so we can free these
a8223e
+     temporary values.  */
a8223e
+  void start_dimension (bool inner_p)
a8223e
+  {
a8223e
+    if (inner_p)
a8223e
+      {
a8223e
+	gdb_assert (m_mark == nullptr);
a8223e
+	m_mark = value_mark ();
a8223e
+      }
a8223e
+  }
a8223e
+
a8223e
+  /* When we finish processing the inner most dimension free all temporary
a8223e
+     value that were created.  */
a8223e
+  void finish_dimension (bool inner_p, bool last_p)
a8223e
+  {
a8223e
+    if (inner_p)
a8223e
+      {
a8223e
+	gdb_assert (m_mark != nullptr);
a8223e
+	value_free_to_mark (m_mark);
a8223e
+	m_mark = nullptr;
a8223e
+      }
a8223e
+  }
a8223e
+
a8223e
+protected:
a8223e
+  /* Copy the contents of array element ELT into M_DEST at the next
a8223e
+     available offset.  */
a8223e
+  void copy_element_to_dest (struct value *elt)
a8223e
+  {
a8223e
+    value_contents_copy (m_dest, m_dest_offset, elt, 0,
a8223e
+			 TYPE_LENGTH (value_type (elt)));
a8223e
+    m_dest_offset += TYPE_LENGTH (value_type (elt));
a8223e
+  }
a8223e
+
a8223e
+  /* The value being written to.  */
a8223e
+  struct value *m_dest;
a8223e
+
a8223e
+  /* The byte offset in M_DEST at which the next element should be
a8223e
+     written.  */
a8223e
+  LONGEST m_dest_offset;
a8223e
+
a8223e
+  /* Set with a call to VALUE_MARK, and then reset after calling
a8223e
+     VALUE_FREE_TO_MARK.  */
a8223e
+  struct value *m_mark = nullptr;
a8223e
+};
a8223e
+
a8223e
+/* A class used by FORTRAN_VALUE_SUBARRAY when repacking Fortran array
a8223e
+   slices.  This class is specialised for repacking an array slice from a
a8223e
+   lazy array value, as such it does not require the parent array value to
a8223e
+   be loaded into GDB's memory; the parent value could be huge, while the
a8223e
+   slice could be tiny.  */
a8223e
+class fortran_lazy_array_repacker_impl
a8223e
+  : public fortran_array_repacker_base_impl
a8223e
+{
a8223e
+public:
a8223e
+  /* Constructor.  TYPE is the type of the slice being loaded from the
a8223e
+     parent value, so this type will correctly reflect the strides required
a8223e
+     to find all of the elements from the parent value.  ADDRESS is the
a8223e
+     address in target memory of value matching TYPE, and DEST is the value
a8223e
+     we are repacking into.  */
a8223e
+  explicit fortran_lazy_array_repacker_impl (struct type *type,
a8223e
+					     CORE_ADDR address,
a8223e
+					     struct value *dest)
a8223e
+    : fortran_array_repacker_base_impl (dest),
a8223e
+      m_addr (address)
a8223e
+  { /* Nothing.  */ }
a8223e
+
a8223e
+  /* Create a lazy value in target memory representing a single element,
a8223e
+     then load the element into GDB's memory and copy the contents into the
a8223e
+     destination value.  */
a8223e
+  void process_element (struct type *elt_type, LONGEST elt_off, bool last_p)
a8223e
+  {
a8223e
+    copy_element_to_dest (value_at_lazy (elt_type, m_addr + elt_off));
a8223e
+  }
a8223e
+
a8223e
+private:
a8223e
+  /* The address in target memory where the parent value starts.  */
a8223e
+  CORE_ADDR m_addr;
a8223e
+};
a8223e
+
a8223e
+/* A class used by FORTRAN_VALUE_SUBARRAY when repacking Fortran array
a8223e
+   slices.  This class is specialised for repacking an array slice from a
a8223e
+   previously loaded (non-lazy) array value, as such it fetches the
a8223e
+   element values from the contents of the parent value.  */
a8223e
+class fortran_array_repacker_impl
a8223e
+  : public fortran_array_repacker_base_impl
a8223e
+{
a8223e
+public:
a8223e
+  /* Constructor.  TYPE is the type for the array slice within the parent
a8223e
+     value, as such it has stride values as required to find the elements
a8223e
+     within the original parent value.  ADDRESS is the address in target
a8223e
+     memory of the value matching TYPE.  BASE_OFFSET is the offset from
a8223e
+     the start of VAL's content buffer to the start of the object of TYPE,
a8223e
+     VAL is the parent object from which we are loading the value, and
a8223e
+     DEST is the value into which we are repacking.  */
a8223e
+  explicit fortran_array_repacker_impl (struct type *type, CORE_ADDR address,
a8223e
+					LONGEST base_offset,
a8223e
+					struct value *val, struct value *dest)
a8223e
+    : fortran_array_repacker_base_impl (dest),
a8223e
+      m_base_offset (base_offset),
a8223e
+      m_val (val)
a8223e
+  {
a8223e
+    gdb_assert (!value_lazy (val));
a8223e
+  }
a8223e
+
a8223e
+  /* Extract an element of ELT_TYPE at offset (M_BASE_OFFSET + ELT_OFF)
a8223e
+     from the content buffer of M_VAL then copy this extracted value into
a8223e
+     the repacked destination value.  */
a8223e
+  void process_element (struct type *elt_type, LONGEST elt_off, bool last_p)
a8223e
+  {
a8223e
+    struct value *elt
a8223e
+      = value_from_component (m_val, elt_type, (elt_off + m_base_offset));
a8223e
+    copy_element_to_dest (elt);
a8223e
+  }
a8223e
+
a8223e
+private:
a8223e
+  /* The offset into the content buffer of M_VAL to the start of the slice
a8223e
+     being extracted.  */
a8223e
+  LONGEST m_base_offset;
a8223e
+
a8223e
+  /* The parent value from which we are extracting a slice.  */
a8223e
+  struct value *m_val;
a8223e
+};
a8223e
+
a8223e
 /* Called from evaluate_subexp_standard to perform array indexing, and
a8223e
    sub-range extraction, for Fortran.  As well as arrays this function
a8223e
    also handles strings as they can be treated like arrays of characters.
a8223e
@@ -200,51 +315,394 @@ enum f_primitive_types {
a8223e
 fortran_value_subarray (struct value *array, struct expression *exp,
a8223e
 			int *pos, int nargs, enum noside noside)
a8223e
 {
a8223e
-  if (exp->elts[*pos].opcode == OP_RANGE)
a8223e
-    return value_f90_subarray (array, exp, pos, noside);
a8223e
-
a8223e
-  if (noside == EVAL_SKIP)
a8223e
+  type *original_array_type = check_typedef (value_type (array));
a8223e
+  bool is_string_p = original_array_type->code () == TYPE_CODE_STRING;
a8223e
+
a8223e
+  /* Perform checks for ARRAY not being available.  The somewhat overly
a8223e
+     complex logic here is just to keep backward compatibility with the
a8223e
+     errors that we used to get before FORTRAN_VALUE_SUBARRAY was
a8223e
+     rewritten.  Maybe a future task would streamline the error messages we
a8223e
+     get here, and update all the expected test results.  */
a8223e
+  if (exp->elts[*pos].opcode != OP_RANGE)
a8223e
     {
a8223e
-      skip_undetermined_arglist (nargs, exp, pos, noside);
a8223e
-      /* Return the dummy value with the correct type.  */
a8223e
-      return array;
a8223e
+      if (type_not_associated (original_array_type))
a8223e
+	error (_("no such vector element (vector not associated)"));
a8223e
+      else if (type_not_allocated (original_array_type))
a8223e
+	error (_("no such vector element (vector not allocated)"));
a8223e
+    }
a8223e
+  else
a8223e
+    {
a8223e
+      if (type_not_associated (original_array_type))
a8223e
+	error (_("array not associated"));
a8223e
+      else if (type_not_allocated (original_array_type))
a8223e
+	error (_("array not allocated"));
a8223e
     }
a8223e
 
a8223e
-  LONGEST subscript_array[MAX_FORTRAN_DIMS];
a8223e
-  int ndimensions = 1;
a8223e
-  struct type *type = check_typedef (value_type (array));
a8223e
+  /* First check that the number of dimensions in the type we are slicing
a8223e
+     matches the number of arguments we were passed.  */
a8223e
+  int ndimensions = calc_f77_array_dims (original_array_type);
a8223e
+  if (nargs != ndimensions)
a8223e
+    error (_("Wrong number of subscripts"));
a8223e
 
a8223e
-  if (nargs > MAX_FORTRAN_DIMS)
a8223e
-    error (_("Too many subscripts for F77 (%d Max)"), MAX_FORTRAN_DIMS);
a8223e
+  /* This will be initialised below with the type of the elements held in
a8223e
+     ARRAY.  */
a8223e
+  struct type *inner_element_type;
a8223e
 
a8223e
-  ndimensions = calc_f77_array_dims (type);
a8223e
+  /* Extract the types of each array dimension from the original array
a8223e
+     type.  We need these available so we can fill in the default upper and
a8223e
+     lower bounds if the user requested slice doesn't provide that
a8223e
+     information.  Additionally unpacking the dimensions like this gives us
a8223e
+     the inner element type.  */
a8223e
+  std::vector<struct type *> dim_types;
a8223e
+  {
a8223e
+    dim_types.reserve (ndimensions);
a8223e
+    struct type *type = original_array_type;
a8223e
+    for (int i = 0; i < ndimensions; ++i)
a8223e
+      {
a8223e
+	dim_types.push_back (type);
a8223e
+	type = TYPE_TARGET_TYPE (type);
a8223e
+      }
a8223e
+    /* TYPE is now the inner element type of the array, we start the new
a8223e
+       array slice off as this type, then as we process the requested slice
a8223e
+       (from the user) we wrap new types around this to build up the final
a8223e
+       slice type.  */
a8223e
+    inner_element_type = type;
a8223e
+  }
a8223e
 
a8223e
-  if (nargs != ndimensions)
a8223e
-    error (_("Wrong number of subscripts"));
a8223e
+  /* As we analyse the new slice type we need to understand if the data
a8223e
+     being referenced is contiguous.  Do decide this we must track the size
a8223e
+     of an element at each dimension of the new slice array.  Initially the
a8223e
+     elements of the inner most dimension of the array are the same inner
a8223e
+     most elements as the original ARRAY.  */
a8223e
+  LONGEST slice_element_size = TYPE_LENGTH (inner_element_type);
a8223e
+
a8223e
+  /* Start off assuming all data is contiguous, this will be set to false
a8223e
+     if access to any dimension results in non-contiguous data.  */
a8223e
+  bool is_all_contiguous = true;
a8223e
+
a8223e
+  /* The TOTAL_OFFSET is the distance in bytes from the start of the
a8223e
+     original ARRAY to the start of the new slice.  This is calculated as
a8223e
+     we process the information from the user.  */
a8223e
+  LONGEST total_offset = 0;
a8223e
+
a8223e
+  /* A structure representing information about each dimension of the
a8223e
+     resulting slice.  */
a8223e
+  struct slice_dim
a8223e
+  {
a8223e
+    /* Constructor.  */
a8223e
+    slice_dim (LONGEST l, LONGEST h, LONGEST s, struct type *idx)
a8223e
+      : low (l),
a8223e
+	high (h),
a8223e
+	stride (s),
a8223e
+	index (idx)
a8223e
+    { /* Nothing.  */ }
a8223e
+
a8223e
+    /* The low bound for this dimension of the slice.  */
a8223e
+    LONGEST low;
a8223e
+
a8223e
+    /* The high bound for this dimension of the slice.  */
a8223e
+    LONGEST high;
a8223e
+
a8223e
+    /* The byte stride for this dimension of the slice.  */
a8223e
+    LONGEST stride;
a8223e
+
a8223e
+    struct type *index;
a8223e
+  };
a8223e
+
a8223e
+  /* The dimensions of the resulting slice.  */
a8223e
+  std::vector<slice_dim> slice_dims;
a8223e
+
a8223e
+  /* Process the incoming arguments.   These arguments are in the reverse
a8223e
+     order to the array dimensions, that is the first argument refers to
a8223e
+     the last array dimension.  */
a8223e
+  if (fortran_array_slicing_debug)
a8223e
+    debug_printf ("Processing array access:\n");
a8223e
+  for (int i = 0; i < nargs; ++i)
a8223e
+    {
a8223e
+      /* For each dimension of the array the user will have either provided
a8223e
+	 a ranged access with optional lower bound, upper bound, and
a8223e
+	 stride, or the user will have supplied a single index.  */
a8223e
+      struct type *dim_type = dim_types[ndimensions - (i + 1)];
a8223e
+      if (exp->elts[*pos].opcode == OP_RANGE)
a8223e
+	{
a8223e
+	  int pc = (*pos) + 1;
a8223e
+	  enum range_flag range_flag = (enum range_flag) exp->elts[pc].longconst;
a8223e
+	  *pos += 3;
a8223e
+
a8223e
+	  LONGEST low, high, stride;
a8223e
+	  low = high = stride = 0;
a8223e
+
a8223e
+	  if ((range_flag & RANGE_LOW_BOUND_DEFAULT) == 0)
a8223e
+	    low = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
a8223e
+	  else
a8223e
+	    low = f77_get_lowerbound (dim_type);
a8223e
+	  if ((range_flag & RANGE_HIGH_BOUND_DEFAULT) == 0)
a8223e
+	    high = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
a8223e
+	  else
a8223e
+	    high = f77_get_upperbound (dim_type);
a8223e
+	  if ((range_flag & RANGE_HAS_STRIDE) == RANGE_HAS_STRIDE)
a8223e
+	    stride = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
a8223e
+	  else
a8223e
+	    stride = 1;
a8223e
+
a8223e
+	  if (stride == 0)
a8223e
+	    error (_("stride must not be 0"));
a8223e
+
a8223e
+	  /* Get information about this dimension in the original ARRAY.  */
a8223e
+	  struct type *target_type = TYPE_TARGET_TYPE (dim_type);
a8223e
+	  struct type *index_type = dim_type->index_type ();
a8223e
+	  LONGEST lb = f77_get_lowerbound (dim_type);
a8223e
+	  LONGEST ub = f77_get_upperbound (dim_type);
a8223e
+	  LONGEST sd = index_type->bit_stride ();
a8223e
+	  if (sd == 0)
a8223e
+	    sd = TYPE_LENGTH (target_type) * 8;
a8223e
+
a8223e
+	  if (fortran_array_slicing_debug)
a8223e
+	    {
a8223e
+	      debug_printf ("|-> Range access\n");
a8223e
+	      std::string str = type_to_string (dim_type);
a8223e
+	      debug_printf ("|   |-> Type: %s\n", str.c_str ());
a8223e
+	      debug_printf ("|   |-> Array:\n");
a8223e
+	      debug_printf ("|   |   |-> Low bound: %ld\n", lb);
a8223e
+	      debug_printf ("|   |   |-> High bound: %ld\n", ub);
a8223e
+	      debug_printf ("|   |   |-> Bit stride: %ld\n", sd);
a8223e
+	      debug_printf ("|   |   |-> Byte stride: %ld\n", sd / 8);
a8223e
+	      debug_printf ("|   |   |-> Type size: %ld\n",
a8223e
+			    TYPE_LENGTH (dim_type));
a8223e
+	      debug_printf ("|   |   '-> Target type size: %ld\n",
a8223e
+			    TYPE_LENGTH (target_type));
a8223e
+	      debug_printf ("|   |-> Accessing:\n");
a8223e
+	      debug_printf ("|   |   |-> Low bound: %ld\n",
a8223e
+			    low);
a8223e
+	      debug_printf ("|   |   |-> High bound: %ld\n",
a8223e
+			    high);
a8223e
+	      debug_printf ("|   |   '-> Element stride: %ld\n",
a8223e
+			    stride);
a8223e
+	    }
a8223e
+
a8223e
+	  /* Check the user hasn't asked for something invalid.  */
a8223e
+	  if (high > ub || low < lb)
a8223e
+	    error (_("array subscript out of bounds"));
a8223e
+
a8223e
+	  /* Calculate what this dimension of the new slice array will look
a8223e
+	     like.  OFFSET is the byte offset from the start of the
a8223e
+	     previous (more outer) dimension to the start of this
a8223e
+	     dimension.  E_COUNT is the number of elements in this
a8223e
+	     dimension.  REMAINDER is the number of elements remaining
a8223e
+	     between the last included element and the upper bound.  For
a8223e
+	     example an access '1:6:2' will include elements 1, 3, 5 and
a8223e
+	     have a remainder of 1 (element #6).  */
a8223e
+	  LONGEST lowest = std::min (low, high);
a8223e
+	  LONGEST offset = (sd / 8) * (lowest - lb);
a8223e
+	  LONGEST e_count = std::abs (high - low) + 1;
a8223e
+	  e_count = (e_count + (std::abs (stride) - 1)) / std::abs (stride);
a8223e
+	  LONGEST new_low = 1;
a8223e
+	  LONGEST new_high = new_low + e_count - 1;
a8223e
+	  LONGEST new_stride = (sd * stride) / 8;
a8223e
+	  LONGEST last_elem = low + ((e_count - 1) * stride);
a8223e
+	  LONGEST remainder = high - last_elem;
a8223e
+	  if (low > high)
a8223e
+	    {
a8223e
+	      offset += std::abs (remainder) * TYPE_LENGTH (target_type);
a8223e
+	      if (stride > 0)
a8223e
+		error (_("incorrect stride and boundary combination"));
a8223e
+	    }
a8223e
+	  else if (stride < 0)
a8223e
+	    error (_("incorrect stride and boundary combination"));
a8223e
+
a8223e
+	  /* Is the data within this dimension contiguous?  It is if the
a8223e
+	     newly computed stride is the same size as a single element of
a8223e
+	     this dimension.  */
a8223e
+	  bool is_dim_contiguous = (new_stride == slice_element_size);
a8223e
+	  is_all_contiguous &= is_dim_contiguous;
a8223e
+
a8223e
+	  if (fortran_array_slicing_debug)
a8223e
+	    {
a8223e
+	      debug_printf ("|   '-> Results:\n");
a8223e
+	      debug_printf ("|       |-> Offset = %ld\n", offset);
a8223e
+	      debug_printf ("|       |-> Elements = %ld\n", e_count);
a8223e
+	      debug_printf ("|       |-> Low bound = %ld\n", new_low);
a8223e
+	      debug_printf ("|       |-> High bound = %ld\n", new_high);
a8223e
+	      debug_printf ("|       |-> Byte stride = %ld\n", new_stride);
a8223e
+	      debug_printf ("|       |-> Last element = %ld\n", last_elem);
a8223e
+	      debug_printf ("|       |-> Remainder = %ld\n", remainder);
a8223e
+	      debug_printf ("|       '-> Contiguous = %s\n",
a8223e
+			    (is_dim_contiguous ? "Yes" : "No"));
a8223e
+	    }
a8223e
+
a8223e
+	  /* Figure out how big (in bytes) an element of this dimension of
a8223e
+	     the new array slice will be.  */
a8223e
+	  slice_element_size = std::abs (new_stride * e_count);
a8223e
+
a8223e
+	  slice_dims.emplace_back (new_low, new_high, new_stride,
a8223e
+				   index_type);
a8223e
+
a8223e
+	  /* Update the total offset.  */
a8223e
+	  total_offset += offset;
a8223e
+	}
a8223e
+      else
a8223e
+	{
a8223e
+	  /* There is a single index for this dimension.  */
a8223e
+	  LONGEST index
a8223e
+	    = value_as_long (evaluate_subexp_with_coercion (exp, pos, noside));
a8223e
+
a8223e
+	  /* Get information about this dimension in the original ARRAY.  */
a8223e
+	  struct type *target_type = TYPE_TARGET_TYPE (dim_type);
a8223e
+	  struct type *index_type = dim_type->index_type ();
a8223e
+	  LONGEST lb = f77_get_lowerbound (dim_type);
a8223e
+	  LONGEST ub = f77_get_upperbound (dim_type);
a8223e
+	  LONGEST sd = index_type->bit_stride () / 8;
a8223e
+	  if (sd == 0)
a8223e
+	    sd = TYPE_LENGTH (target_type);
a8223e
+
a8223e
+	  if (fortran_array_slicing_debug)
a8223e
+	    {
a8223e
+	      debug_printf ("|-> Index access\n");
a8223e
+	      std::string str = type_to_string (dim_type);
a8223e
+	      debug_printf ("|   |-> Type: %s\n", str.c_str ());
a8223e
+	      debug_printf ("|   |-> Array:\n");
a8223e
+	      debug_printf ("|   |   |-> Low bound: %ld\n", lb);
a8223e
+	      debug_printf ("|   |   |-> High bound: %ld\n", ub);
a8223e
+	      debug_printf ("|   |   |-> Byte stride: %ld\n", sd);
a8223e
+	      debug_printf ("|   |   |-> Type size: %ld\n", TYPE_LENGTH (dim_type));
a8223e
+	      debug_printf ("|   |   '-> Target type size: %ld\n",
a8223e
+			    TYPE_LENGTH (target_type));
a8223e
+	      debug_printf ("|   '-> Accessing:\n");
a8223e
+	      debug_printf ("|       '-> Index: %ld\n", index);
a8223e
+	    }
a8223e
+
a8223e
+	  /* If the array has actual content then check the index is in
a8223e
+	     bounds.  An array without content (an unbound array) doesn't
a8223e
+	     have a known upper bound, so don't error check in that
a8223e
+	     situation.  */
a8223e
+	  if (index < lb
a8223e
+	      || (dim_type->index_type ()->bounds ()->high.kind () != PROP_UNDEFINED
a8223e
+		  && index > ub)
a8223e
+	      || (VALUE_LVAL (array) != lval_memory
a8223e
+		  && dim_type->index_type ()->bounds ()->high.kind () == PROP_UNDEFINED))
a8223e
+	    {
a8223e
+	      if (type_not_associated (dim_type))
a8223e
+		error (_("no such vector element (vector not associated)"));
a8223e
+	      else if (type_not_allocated (dim_type))
a8223e
+		error (_("no such vector element (vector not allocated)"));
a8223e
+	      else
a8223e
+		error (_("no such vector element"));
a8223e
+	    }
a8223e
 
a8223e
-  gdb_assert (nargs > 0);
a8223e
+	  /* Calculate using the type stride, not the target type size.  */
a8223e
+	  LONGEST offset = sd * (index - lb);
a8223e
+	  total_offset += offset;
a8223e
+	}
a8223e
+    }
a8223e
 
a8223e
-  /* Now that we know we have a legal array subscript expression let us
a8223e
-     actually find out where this element exists in the array.  */
a8223e
+  if (noside == EVAL_SKIP)
a8223e
+    return array;
a8223e
 
a8223e
-  /* Take array indices left to right.  */
a8223e
-  for (int i = 0; i < nargs; i++)
a8223e
+  /* Build a type that represents the new array slice in the target memory
a8223e
+     of the original ARRAY, this type makes use of strides to correctly
a8223e
+     find only those elements that are part of the new slice.  */
a8223e
+  struct type *array_slice_type = inner_element_type;
a8223e
+  for (const auto &d : slice_dims)
a8223e
     {
a8223e
-      /* Evaluate each subscript; it must be a legal integer in F77.  */
a8223e
-      value *arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
a8223e
+      /* Create the range.  */
a8223e
+      dynamic_prop p_low, p_high, p_stride;
a8223e
+
a8223e
+      p_low.set_const_val (d.low);
a8223e
+      p_high.set_const_val (d.high);
a8223e
+      p_stride.set_const_val (d.stride);
a8223e
+
a8223e
+      struct type *new_range
a8223e
+	= create_range_type_with_stride ((struct type *) NULL,
a8223e
+					 TYPE_TARGET_TYPE (d.index),
a8223e
+					 &p_low, &p_high, 0, &p_stride,
a8223e
+					 true);
a8223e
+      array_slice_type
a8223e
+	= create_array_type (nullptr, array_slice_type, new_range);
a8223e
+    }
a8223e
 
a8223e
-      /* Fill in the subscript array.  */
a8223e
-      subscript_array[i] = value_as_long (arg2);
a8223e
+  if (fortran_array_slicing_debug)
a8223e
+    {
a8223e
+      debug_printf ("'-> Final result:\n");
a8223e
+      debug_printf ("    |-> Type: %s\n",
a8223e
+		    type_to_string (array_slice_type).c_str ());
a8223e
+      debug_printf ("    |-> Total offset: %ld\n", total_offset);
a8223e
+      debug_printf ("    |-> Base address: %s\n",
a8223e
+		    core_addr_to_string (value_address (array)));
a8223e
+      debug_printf ("    '-> Contiguous = %s\n",
a8223e
+		    (is_all_contiguous ? "Yes" : "No"));
a8223e
     }
a8223e
 
a8223e
-  /* Internal type of array is arranged right to left.  */
a8223e
-  for (int i = nargs; i > 0; i--)
a8223e
+  /* Should we repack this array slice?  */
a8223e
+  if (!is_all_contiguous && (repack_array_slices || is_string_p))
a8223e
     {
a8223e
-      struct type *array_type = check_typedef (value_type (array));
a8223e
-      LONGEST index = subscript_array[i - 1];
a8223e
+      /* Build a type for the repacked slice.  */
a8223e
+      struct type *repacked_array_type = inner_element_type;
a8223e
+      for (const auto &d : slice_dims)
a8223e
+	{
a8223e
+	  /* Create the range.  */
a8223e
+	  dynamic_prop p_low, p_high, p_stride;
a8223e
+
a8223e
+	  p_low.set_const_val (d.low);
a8223e
+	  p_high.set_const_val (d.high);
a8223e
+	  p_stride.set_const_val (TYPE_LENGTH (repacked_array_type));
a8223e
+
a8223e
+	  struct type *new_range
a8223e
+	    = create_range_type_with_stride ((struct type *) NULL,
a8223e
+					     TYPE_TARGET_TYPE (d.index),
a8223e
+					     &p_low, &p_high, 0, &p_stride,
a8223e
+					     true);
a8223e
+	  repacked_array_type
a8223e
+	    = create_array_type (nullptr, repacked_array_type, new_range);
a8223e
+	}
a8223e
 
a8223e
-      array = value_subscripted_rvalue (array, index,
a8223e
-					f77_get_lowerbound (array_type));
a8223e
+      /* Now copy the elements from the original ARRAY into the packed
a8223e
+	 array value DEST.  */
a8223e
+      struct value *dest = allocate_value (repacked_array_type);
a8223e
+      if (value_lazy (array)
a8223e
+	  || (total_offset + TYPE_LENGTH (array_slice_type)
a8223e
+	      > TYPE_LENGTH (check_typedef (value_type (array)))))
a8223e
+	{
a8223e
+	  fortran_array_walker<fortran_lazy_array_repacker_impl> p
a8223e
+	    (array_slice_type, value_address (array) + total_offset, dest);
a8223e
+	  p.walk ();
a8223e
+	}
a8223e
+      else
a8223e
+	{
a8223e
+	  fortran_array_walker<fortran_array_repacker_impl> p
a8223e
+	    (array_slice_type, value_address (array) + total_offset,
a8223e
+	     total_offset, array, dest);
a8223e
+	  p.walk ();
a8223e
+	}
a8223e
+      array = dest;
a8223e
+    }
a8223e
+  else
a8223e
+    {
a8223e
+      if (VALUE_LVAL (array) == lval_memory)
a8223e
+	{
a8223e
+	  /* If the value we're taking a slice from is not yet loaded, or
a8223e
+	     the requested slice is outside the values content range then
a8223e
+	     just create a new lazy value pointing at the memory where the
a8223e
+	     contents we're looking for exist.  */
a8223e
+	  if (value_lazy (array)
a8223e
+	      || (total_offset + TYPE_LENGTH (array_slice_type)
a8223e
+		  > TYPE_LENGTH (check_typedef (value_type (array)))))
a8223e
+	    array = value_at_lazy (array_slice_type,
a8223e
+				   value_address (array) + total_offset);
a8223e
+	  else
a8223e
+	    array = value_from_contents_and_address (array_slice_type,
a8223e
+						     (value_contents (array)
a8223e
+						      + total_offset),
a8223e
+						     (value_address (array)
a8223e
+						      + total_offset));
a8223e
+	}
a8223e
+      else if (!value_lazy (array))
a8223e
+	{
a8223e
+	  const void *valaddr = value_contents (array) + total_offset;
a8223e
+	  array = allocate_value (array_slice_type);
a8223e
+	  memcpy (value_contents_raw (array), valaddr, TYPE_LENGTH (array_slice_type));
a8223e
+	}
a8223e
+      else
a8223e
+	error (_("cannot subscript arrays that are not in memory"));
a8223e
     }
a8223e
 
a8223e
   return array;
a8223e
@@ -1031,11 +1489,50 @@ class f_language : public language_defn
a8223e
   return (const struct builtin_f_type *) gdbarch_data (gdbarch, f_type_data);
a8223e
 }
a8223e
 
a8223e
+/* Command-list for the "set/show fortran" prefix command.  */
a8223e
+static struct cmd_list_element *set_fortran_list;
a8223e
+static struct cmd_list_element *show_fortran_list;
a8223e
+
a8223e
 void _initialize_f_language ();
a8223e
 void
a8223e
 _initialize_f_language ()
a8223e
 {
a8223e
   f_type_data = gdbarch_data_register_post_init (build_fortran_types);
a8223e
+
a8223e
+  add_basic_prefix_cmd ("fortran", no_class,
a8223e
+			_("Prefix command for changing Fortran-specific settings."),
a8223e
+			&set_fortran_list, "set fortran ", 0, &setlist);
a8223e
+
a8223e
+  add_show_prefix_cmd ("fortran", no_class,
a8223e
+		       _("Generic command for showing Fortran-specific settings."),
a8223e
+		       &show_fortran_list, "show fortran ", 0, &showlist);
a8223e
+
a8223e
+  add_setshow_boolean_cmd ("repack-array-slices", class_vars,
a8223e
+			   &repack_array_slices, _("\
a8223e
+Enable or disable repacking of non-contiguous array slices."), _("\
a8223e
+Show whether non-contiguous array slices are repacked."), _("\
a8223e
+When the user requests a slice of a Fortran array then we can either return\n\
a8223e
+a descriptor that describes the array in place (using the original array data\n\
a8223e
+in its existing location) or the original data can be repacked (copied) to a\n\
a8223e
+new location.\n\
a8223e
+\n\
a8223e
+When the content of the array slice is contiguous within the original array\n\
a8223e
+then the result will never be repacked, but when the data for the new array\n\
a8223e
+is non-contiguous within the original array repacking will only be performed\n\
a8223e
+when this setting is on."),
a8223e
+			   NULL,
a8223e
+			   show_repack_array_slices,
a8223e
+			   &set_fortran_list, &show_fortran_list);
a8223e
+
a8223e
+  /* Debug Fortran's array slicing logic.  */
a8223e
+  add_setshow_boolean_cmd ("fortran-array-slicing", class_maintenance,
a8223e
+			   &fortran_array_slicing_debug, _("\
a8223e
+Set debugging of Fortran array slicing."), _("\
a8223e
+Show debugging of Fortran array slicing."), _("\
a8223e
+When on, debugging of Fortran array slicing is enabled."),
a8223e
+			    NULL,
a8223e
+			    show_fortran_array_slicing_debug,
a8223e
+			    &setdebuglist, &showdebuglist);
a8223e
 }
a8223e
 
a8223e
 /* See f-lang.h.  */
a8223e
@@ -1074,3 +1571,56 @@ struct type *
a8223e
     return value_type (arg);
a8223e
   return type;
a8223e
 }
a8223e
+
a8223e
+/* See f-lang.h.  */
a8223e
+
a8223e
+CORE_ADDR
a8223e
+fortran_adjust_dynamic_array_base_address_hack (struct type *type,
a8223e
+						CORE_ADDR address)
a8223e
+{
a8223e
+  gdb_assert (type->code () == TYPE_CODE_ARRAY);
a8223e
+
a8223e
+  int ndimensions = calc_f77_array_dims (type);
a8223e
+  LONGEST total_offset = 0;
a8223e
+
a8223e
+  /* Walk through each of the dimensions of this array type and figure out
a8223e
+     if any of the dimensions are "backwards", that is the base address
a8223e
+     for this dimension points to the element at the highest memory
a8223e
+     address and the stride is negative.  */
a8223e
+  struct type *tmp_type = type;
a8223e
+  for (int i = 0 ; i < ndimensions; ++i)
a8223e
+    {
a8223e
+      /* Grab the range for this dimension and extract the lower and upper
a8223e
+	 bounds.  */
a8223e
+      tmp_type = check_typedef (tmp_type);
a8223e
+      struct type *range_type = tmp_type->index_type ();
a8223e
+      LONGEST lowerbound, upperbound, stride;
a8223e
+      if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
a8223e
+	error ("failed to get range bounds");
a8223e
+
a8223e
+      /* Figure out the stride for this dimension.  */
a8223e
+      struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (tmp_type));
a8223e
+      stride = tmp_type->index_type ()->bounds ()->bit_stride ();
a8223e
+      if (stride == 0)
a8223e
+	stride = type_length_units (elt_type);
a8223e
+      else
a8223e
+	{
a8223e
+	  struct gdbarch *arch = get_type_arch (elt_type);
a8223e
+	  int unit_size = gdbarch_addressable_memory_unit_size (arch);
a8223e
+	  stride /= (unit_size * 8);
a8223e
+	}
a8223e
+
a8223e
+      /* If this dimension is "backward" then figure out the offset
a8223e
+	 adjustment required to point to the element at the lowest memory
a8223e
+	 address, and add this to the total offset.  */
a8223e
+      LONGEST offset = 0;
a8223e
+      if (stride < 0 && lowerbound < upperbound)
a8223e
+	offset = (upperbound - lowerbound) * stride;
a8223e
+      total_offset += offset;
a8223e
+      tmp_type = TYPE_TARGET_TYPE (tmp_type);
a8223e
+    }
a8223e
+
a8223e
+  /* Adjust the address of this object and return it.  */
a8223e
+  address += total_offset;
a8223e
+  return address;
a8223e
+}
a8223e
diff --git a/gdb/f-lang.h b/gdb/f-lang.h
a8223e
--- a/gdb/f-lang.h
a8223e
+++ b/gdb/f-lang.h
a8223e
@@ -64,7 +64,6 @@ struct common_block
a8223e
 
a8223e
 extern int calc_f77_array_dims (struct type *);
a8223e
 
a8223e
-
a8223e
 /* Fortran (F77) types */
a8223e
 
a8223e
 struct builtin_f_type
a8223e
@@ -122,4 +121,22 @@ extern struct value *fortran_argument_convert (struct value *value,
a8223e
 extern struct type *fortran_preserve_arg_pointer (struct value *arg,
a8223e
 						  struct type *type);
a8223e
 
a8223e
+/* Fortran arrays can have a negative stride.  When this happens it is
a8223e
+   often the case that the base address for an object is not the lowest
a8223e
+   address occupied by that object.  For example, an array slice (10:1:-1)
a8223e
+   will be encoded with lower bound 1, upper bound 10, a stride of
a8223e
+   -ELEMENT_SIZE, and have a base address pointer that points at the
a8223e
+   element with the highest address in memory.
a8223e
+
a8223e
+   This really doesn't play well with our current model of value contents,
a8223e
+   but could easily require a significant update in order to be supported
a8223e
+   "correctly".
a8223e
+
a8223e
+   For now, we manually force the base address to be the lowest addressed
a8223e
+   element here.  Yes, this will break some things, but it fixes other
a8223e
+   things.  The hope is that it fixes more than it breaks.  */
a8223e
+
a8223e
+extern CORE_ADDR fortran_adjust_dynamic_array_base_address_hack
a8223e
+	(struct type *type, CORE_ADDR address);
a8223e
+
a8223e
 #endif /* F_LANG_H */
a8223e
diff --git a/gdb/f-valprint.c b/gdb/f-valprint.c
a8223e
--- a/gdb/f-valprint.c
a8223e
+++ b/gdb/f-valprint.c
a8223e
@@ -35,6 +35,7 @@
a8223e
 #include "dictionary.h"
a8223e
 #include "cli/cli-style.h"
a8223e
 #include "gdbarch.h"
a8223e
+#include "f-array-walker.h"
a8223e
 
a8223e
 static void f77_get_dynamic_length_of_aggregate (struct type *);
a8223e
 
a8223e
@@ -100,100 +101,103 @@
a8223e
     * TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type)));
a8223e
 }
a8223e
 
a8223e
-/* Actual function which prints out F77 arrays, Valaddr == address in 
a8223e
-   the superior.  Address == the address in the inferior.  */
a8223e
+/* A class used by FORTRAN_PRINT_ARRAY as a specialisation of the array
a8223e
+   walking template.  This specialisation prints Fortran arrays.  */
a8223e
 
a8223e
-static void
a8223e
-f77_print_array_1 (int nss, int ndimensions, struct type *type,
a8223e
-		   const gdb_byte *valaddr,
a8223e
-		   int embedded_offset, CORE_ADDR address,
a8223e
-		   struct ui_file *stream, int recurse,
a8223e
-		   const struct value *val,
a8223e
-		   const struct value_print_options *options,
a8223e
-		   int *elts)
a8223e
+class fortran_array_printer_impl : public fortran_array_walker_base_impl
a8223e
 {
a8223e
-  struct type *range_type = check_typedef (type)->index_type ();
a8223e
-  CORE_ADDR addr = address + embedded_offset;
a8223e
-  LONGEST lowerbound, upperbound;
a8223e
-  LONGEST i;
a8223e
-
a8223e
-  get_discrete_bounds (range_type, &lowerbound, &upperbound);
a8223e
-
a8223e
-  if (nss != ndimensions)
a8223e
-    {
a8223e
-      struct gdbarch *gdbarch = get_type_arch (type);
a8223e
-      size_t dim_size = type_length_units (TYPE_TARGET_TYPE (type));
a8223e
-      int unit_size = gdbarch_addressable_memory_unit_size (gdbarch);
a8223e
-      size_t byte_stride = type->bit_stride () / (unit_size * 8);
a8223e
-      if (byte_stride == 0)
a8223e
-	byte_stride = dim_size;
a8223e
-      size_t offs = 0;
a8223e
-
a8223e
-      for (i = lowerbound;
a8223e
-	   (i < upperbound + 1 && (*elts) < options->print_max);
a8223e
-	   i++)
a8223e
-	{
a8223e
-	  struct value *subarray = value_from_contents_and_address
a8223e
-	    (TYPE_TARGET_TYPE (type), value_contents_for_printing_const (val)
a8223e
-	     + offs, addr + offs);
a8223e
-
a8223e
-	  fprintf_filtered (stream, "(");
a8223e
-	  f77_print_array_1 (nss + 1, ndimensions, value_type (subarray),
a8223e
-			     value_contents_for_printing (subarray),
a8223e
-			     value_embedded_offset (subarray),
a8223e
-			     value_address (subarray),
a8223e
-			     stream, recurse, subarray, options, elts);
a8223e
-	  offs += byte_stride;
a8223e
-	  fprintf_filtered (stream, ")");
a8223e
-
a8223e
-	  if (i < upperbound)
a8223e
-	    fprintf_filtered (stream, " ");
a8223e
-	}
a8223e
-      if (*elts >= options->print_max && i < upperbound)
a8223e
-	fprintf_filtered (stream, "...");
a8223e
-    }
a8223e
-  else
a8223e
-    {
a8223e
-      for (i = lowerbound; i < upperbound + 1 && (*elts) < options->print_max;
a8223e
-	   i++, (*elts)++)
a8223e
-	{
a8223e
-	  struct value *elt = value_subscript ((struct value *)val, i);
a8223e
-
a8223e
-	  common_val_print (elt, stream, recurse, options, current_language);
a8223e
-
a8223e
-	  if (i != upperbound)
a8223e
-	    fprintf_filtered (stream, ", ");
a8223e
-
a8223e
-	  if ((*elts == options->print_max - 1)
a8223e
-	      && (i != upperbound))
a8223e
-	    fprintf_filtered (stream, "...");
a8223e
-	}
a8223e
-    }
a8223e
-}
a8223e
+public:
a8223e
+  /* Constructor.  TYPE is the array type being printed, ADDRESS is the
a8223e
+     address in target memory for the object of TYPE being printed.  VAL is
a8223e
+     the GDB value (of TYPE) being printed.  STREAM is where to print to,
a8223e
+     RECOURSE is passed through (and prevents infinite recursion), and
a8223e
+     OPTIONS are the printing control options.  */
a8223e
+  explicit fortran_array_printer_impl (struct type *type,
a8223e
+				       CORE_ADDR address,
a8223e
+				       struct value *val,
a8223e
+				       struct ui_file *stream,
a8223e
+				       int recurse,
a8223e
+				       const struct value_print_options *options)
a8223e
+    : m_elts (0),
a8223e
+      m_val (val),
a8223e
+      m_stream (stream),
a8223e
+      m_recurse (recurse),
a8223e
+      m_options (options)
a8223e
+  { /* Nothing.  */ }
a8223e
+
a8223e
+  /* Called while iterating over the array bounds.  When SHOULD_CONTINUE is
a8223e
+     false then we must return false, as we have reached the end of the
a8223e
+     array bounds for this dimension.  However, we also return false if we
a8223e
+     have printed too many elements (after printing '...').  In all other
a8223e
+     cases, return true.  */
a8223e
+  bool continue_walking (bool should_continue)
a8223e
+  {
a8223e
+    bool cont = should_continue && (m_elts < m_options->print_max);
a8223e
+    if (!cont && should_continue)
a8223e
+      fputs_filtered ("...", m_stream);
a8223e
+    return cont;
a8223e
+  }
a8223e
+
a8223e
+  /* Called when we start iterating over a dimension.  If it's not the
a8223e
+     inner most dimension then print an opening '(' character.  */
a8223e
+  void start_dimension (bool inner_p)
a8223e
+  {
a8223e
+    fputs_filtered ("(", m_stream);
a8223e
+  }
a8223e
+
a8223e
+  /* Called when we finish processing a batch of items within a dimension
a8223e
+     of the array.  Depending on whether this is the inner most dimension
a8223e
+     or not we print different things, but this is all about adding
a8223e
+     separators between elements, and dimensions of the array.  */
a8223e
+  void finish_dimension (bool inner_p, bool last_p)
a8223e
+  {
a8223e
+    fputs_filtered (")", m_stream);
a8223e
+    if (!last_p)
a8223e
+      fputs_filtered (" ", m_stream);
a8223e
+  }
a8223e
+
a8223e
+  /* Called to process an element of ELT_TYPE at offset ELT_OFF from the
a8223e
+     start of the parent object.  */
a8223e
+  void process_element (struct type *elt_type, LONGEST elt_off, bool last_p)
a8223e
+  {
a8223e
+    /* Extract the element value from the parent value.  */
a8223e
+    struct value *e_val
a8223e
+      = value_from_component (m_val, elt_type, elt_off);
a8223e
+    common_val_print (e_val, m_stream, m_recurse, m_options, current_language);
a8223e
+    if (!last_p)
a8223e
+      fputs_filtered (", ", m_stream);
a8223e
+    ++m_elts;
a8223e
+  }
a8223e
+
a8223e
+private:
a8223e
+  /* The number of elements printed so far.  */
a8223e
+  int m_elts;
a8223e
+
a8223e
+  /* The value from which we are printing elements.  */
a8223e
+  struct value *m_val;
a8223e
+
a8223e
+  /* The stream we should print too.  */
a8223e
+  struct ui_file *m_stream;
a8223e
+
a8223e
+  /* The recursion counter, passed through when we print each element.  */
a8223e
+  int m_recurse;
a8223e
+
a8223e
+  /* The print control options.  Gives us the maximum number of elements to
a8223e
+     print, and is passed through to each element that we print.  */
a8223e
+  const struct value_print_options *m_options = nullptr;
a8223e
+};
a8223e
 
a8223e
-/* This function gets called to print an F77 array, we set up some 
a8223e
-   stuff and then immediately call f77_print_array_1().  */
a8223e
+/* This function gets called to print a Fortran array.  */
a8223e
 
a8223e
 static void
a8223e
-f77_print_array (struct type *type, const gdb_byte *valaddr,
a8223e
-		 int embedded_offset,
a8223e
-		 CORE_ADDR address, struct ui_file *stream,
a8223e
-		 int recurse,
a8223e
-		 const struct value *val,
a8223e
-		 const struct value_print_options *options)
a8223e
+fortran_print_array (struct type *type, CORE_ADDR address,
a8223e
+		     struct ui_file *stream, int recurse,
a8223e
+		     const struct value *val,
a8223e
+		     const struct value_print_options *options)
a8223e
 {
a8223e
-  int ndimensions;
a8223e
-  int elts = 0;
a8223e
-
a8223e
-  ndimensions = calc_f77_array_dims (type);
a8223e
-
a8223e
-  if (ndimensions > MAX_FORTRAN_DIMS || ndimensions < 0)
a8223e
-    error (_("\
a8223e
-Type node corrupt! F77 arrays cannot have %d subscripts (%d Max)"),
a8223e
-	   ndimensions, MAX_FORTRAN_DIMS);
a8223e
-
a8223e
-  f77_print_array_1 (1, ndimensions, type, valaddr, embedded_offset,
a8223e
-		     address, stream, recurse, val, options, &elts);
a8223e
+  fortran_array_walker<fortran_array_printer_impl> p
a8223e
+    (type, address, (struct value *) val, stream, recurse, options);
a8223e
+  p.walk ();
a8223e
 }
a8223e
 
a8223e
 
a8223e
@@ -236,12 +240,7 @@
a8223e
 
a8223e
     case TYPE_CODE_ARRAY:
a8223e
       if (TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_CHAR)
a8223e
-	{
a8223e
-	  fprintf_filtered (stream, "(");
a8223e
-	  f77_print_array (type, valaddr, 0,
a8223e
-			   address, stream, recurse, val, options);
a8223e
-	  fprintf_filtered (stream, ")");
a8223e
-	}
a8223e
+	fortran_print_array (type, address, stream, recurse, val, options);
a8223e
       else
a8223e
 	{
a8223e
 	  struct type *ch_type = TYPE_TARGET_TYPE (type);
a8223e
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
a8223e
--- a/gdb/gdbtypes.c
a8223e
+++ b/gdb/gdbtypes.c
a8223e
@@ -39,6 +39,7 @@
a8223e
 #include "dwarf2/loc.h"
a8223e
 #include "gdbcore.h"
a8223e
 #include "floatformat.h"
a8223e
+#include "f-lang.h"
a8223e
 #include <algorithm>
a8223e
 
a8223e
 /* Initialize BADNESS constants.  */
a8223e
@@ -2695,7 +2696,16 @@ struct type *
a8223e
   prop = TYPE_DATA_LOCATION (resolved_type);
a8223e
   if (prop != NULL
a8223e
       && dwarf2_evaluate_property (prop, NULL, addr_stack, &value))
a8223e
-    prop->set_const_val (value);
a8223e
+    {
a8223e
+      /* Start of Fortran hack.  See comment in f-lang.h for what is going
a8223e
+	 on here.*/
a8223e
+      if (current_language->la_language == language_fortran
a8223e
+	  && resolved_type->code () == TYPE_CODE_ARRAY)
a8223e
+	value = fortran_adjust_dynamic_array_base_address_hack (resolved_type,
a8223e
+								value);
a8223e
+      /* End of Fortran hack.  */
a8223e
+      prop->set_const_val (value);
a8223e
+    }
a8223e
 
a8223e
   return resolved_type;
a8223e
 }
a8223e
@@ -3600,9 +3610,11 @@ struct type *
a8223e
       LONGEST low_bound, high_bound;
a8223e
       struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (t));
a8223e
 
a8223e
-      get_discrete_bounds (t->index_type (), &low_bound, &high_bound);
a8223e
-
a8223e
-      return high_bound == low_bound && is_scalar_type_recursive (elt_type);
a8223e
+      if (get_discrete_bounds (t->index_type (), &low_bound, &high_bound))
a8223e
+	return (high_bound == low_bound
a8223e
+	        && is_scalar_type_recursive (elt_type));
a8223e
+      else
a8223e
+	return 0;
a8223e
     }
a8223e
   /* Are we dealing with a struct with one element?  */
a8223e
   else if (t->code () == TYPE_CODE_STRUCT && t->num_fields () == 1)
a8223e
diff --git a/gdb/testsuite/gdb.fortran/array-slices-bad.exp b/gdb/testsuite/gdb.fortran/array-slices-bad.exp
a8223e
new file mode 100644
a8223e
--- /dev/null
a8223e
+++ b/gdb/testsuite/gdb.fortran/array-slices-bad.exp
a8223e
@@ -0,0 +1,69 @@
a8223e
+# Copyright 2020 Free Software Foundation, Inc.
a8223e
+
a8223e
+# This program is free software; you can redistribute it and/or modify
a8223e
+# it under the terms of the GNU General Public License as published by
a8223e
+# the Free Software Foundation; either version 3 of the License, or
a8223e
+# (at your option) any later version.
a8223e
+#
a8223e
+# This program is distributed in the hope that it will be useful,
a8223e
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
a8223e
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a8223e
+# GNU General Public License for more details.
a8223e
+#
a8223e
+# You should have received a copy of the GNU General Public License
a8223e
+# along with this program.  If not, see <http://www.gnu.org/licenses/> .
a8223e
+
a8223e
+# Test invalid element and slice array accesses.
a8223e
+
a8223e
+if {[skip_fortran_tests]} { return -1 }
a8223e
+
a8223e
+standard_testfile ".f90"
a8223e
+load_lib fortran.exp
a8223e
+
a8223e
+if {[prepare_for_testing ${testfile}.exp ${testfile} ${srcfile} \
a8223e
+	 {debug f90}]} {
a8223e
+    return -1
a8223e
+}
a8223e
+
a8223e
+if ![fortran_runto_main] {
a8223e
+    untested "could not run to main"
a8223e
+    return -1
a8223e
+}
a8223e
+
a8223e
+# gdb_breakpoint [gdb_get_line_number "Display Message Breakpoint"]
a8223e
+gdb_breakpoint [gdb_get_line_number "First Breakpoint"]
a8223e
+gdb_breakpoint [gdb_get_line_number "Second Breakpoint"]
a8223e
+gdb_breakpoint [gdb_get_line_number "Final Breakpoint"]
a8223e
+
a8223e
+gdb_continue_to_breakpoint "First Breakpoint"
a8223e
+
a8223e
+# Access not yet allocated array.
a8223e
+gdb_test "print other" " = <not allocated>"
a8223e
+gdb_test "print other(0:4,2:3)" "array not allocated"
a8223e
+gdb_test "print other(1,1)" "no such vector element \\(vector not allocated\\)"
a8223e
+
a8223e
+# Access not yet associated pointer.
a8223e
+gdb_test "print pointer2d" " = <not associated>"
a8223e
+gdb_test "print pointer2d(1:2,1:2)" "array not associated"
a8223e
+gdb_test "print pointer2d(1,1)" "no such vector element \\(vector not associated\\)"
a8223e
+
a8223e
+gdb_continue_to_breakpoint "Second Breakpoint"
a8223e
+
a8223e
+# Accessing just outside the arrays.
a8223e
+foreach name {array pointer2d other} {
a8223e
+    gdb_test "print $name (0:,:)" "array subscript out of bounds"
a8223e
+    gdb_test "print $name (:11,:)" "array subscript out of bounds"
a8223e
+    gdb_test "print $name (:,0:)" "array subscript out of bounds"
a8223e
+    gdb_test "print $name (:,:11)" "array subscript out of bounds"
a8223e
+
a8223e
+    gdb_test "print $name (0,:)" "no such vector element"
a8223e
+    gdb_test "print $name (11,:)" "no such vector element"
a8223e
+    gdb_test "print $name (:,0)" "no such vector element"
a8223e
+    gdb_test "print $name (:,11)" "no such vector element"
a8223e
+}
a8223e
+
a8223e
+# Stride in the wrong direction.
a8223e
+gdb_test "print array (1:10:-1,:)" "incorrect stride and boundary combination"
a8223e
+gdb_test "print array (:,1:10:-1)" "incorrect stride and boundary combination"
a8223e
+gdb_test "print array (10:1:1,:)" "incorrect stride and boundary combination"
a8223e
+gdb_test "print array (:,10:1:1)" "incorrect stride and boundary combination"
a8223e
diff --git a/gdb/testsuite/gdb.fortran/array-slices-bad.f90 b/gdb/testsuite/gdb.fortran/array-slices-bad.f90
a8223e
new file mode 100644
a8223e
--- /dev/null
a8223e
+++ b/gdb/testsuite/gdb.fortran/array-slices-bad.f90
a8223e
@@ -0,0 +1,42 @@
a8223e
+! Copyright 2020 Free Software Foundation, Inc.
a8223e
+!
a8223e
+! This program is free software; you can redistribute it and/or modify
a8223e
+! it under the terms of the GNU General Public License as published by
a8223e
+! the Free Software Foundation; either version 3 of the License, or
a8223e
+! (at your option) any later version.
a8223e
+!
a8223e
+! This program is distributed in the hope that it will be useful,
a8223e
+! but WITHOUT ANY WARRANTY; without even the implied warranty of
a8223e
+! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a8223e
+! GNU General Public License for more details.
a8223e
+!
a8223e
+! You should have received a copy of the GNU General Public License
a8223e
+! along with this program.  If not, see <http://www.gnu.org/licenses/>.
a8223e
+
a8223e
+!
a8223e
+! Start of test program.
a8223e
+!
a8223e
+program test
a8223e
+
a8223e
+  ! Declare variables used in this test.
a8223e
+  integer, dimension (1:10,1:10) :: array
a8223e
+  integer, allocatable :: other (:, :)
a8223e
+  integer, dimension(:,:), pointer :: pointer2d => null()
a8223e
+  integer, dimension(1:10,1:10), target :: tarray
a8223e
+
a8223e
+  print *, "" ! First Breakpoint.
a8223e
+
a8223e
+  ! Allocate or associate any variables as needed.
a8223e
+  allocate (other (1:10, 1:10))
a8223e
+  pointer2d => tarray
a8223e
+  array = 0
a8223e
+
a8223e
+  print *, "" ! Second Breakpoint.
a8223e
+
a8223e
+  ! All done.  Deallocate.
a8223e
+  deallocate (other)
a8223e
+
a8223e
+  ! GDB catches this final breakpoint to indicate the end of the test.
a8223e
+  print *, "" ! Final Breakpoint.
a8223e
+
a8223e
+end program test
a8223e
diff --git a/gdb/testsuite/gdb.fortran/array-slices-sub-slices.exp b/gdb/testsuite/gdb.fortran/array-slices-sub-slices.exp
a8223e
new file mode 100644
a8223e
--- /dev/null
a8223e
+++ b/gdb/testsuite/gdb.fortran/array-slices-sub-slices.exp
a8223e
@@ -0,0 +1,111 @@
a8223e
+# Copyright 2020 Free Software Foundation, Inc.
a8223e
+
a8223e
+# This program is free software; you can redistribute it and/or modify
a8223e
+# it under the terms of the GNU General Public License as published by
a8223e
+# the Free Software Foundation; either version 3 of the License, or
a8223e
+# (at your option) any later version.
a8223e
+#
a8223e
+# This program is distributed in the hope that it will be useful,
a8223e
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
a8223e
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a8223e
+# GNU General Public License for more details.
a8223e
+#
a8223e
+# You should have received a copy of the GNU General Public License
a8223e
+# along with this program.  If not, see <http://www.gnu.org/licenses/> .
a8223e
+
a8223e
+# Create a slice of an array, then take a slice of that slice.
a8223e
+
a8223e
+if {[skip_fortran_tests]} { return -1 }
a8223e
+
a8223e
+standard_testfile ".f90"
a8223e
+load_lib fortran.exp
a8223e
+
a8223e
+if {[prepare_for_testing ${testfile}.exp ${testfile} ${srcfile} \
a8223e
+	 {debug f90}]} {
a8223e
+    return -1
a8223e
+}
a8223e
+
a8223e
+if ![fortran_runto_main] {
a8223e
+    untested "could not run to main"
a8223e
+    return -1
a8223e
+}
a8223e
+
a8223e
+# gdb_breakpoint [gdb_get_line_number "Display Message Breakpoint"]
a8223e
+gdb_breakpoint [gdb_get_line_number "Stop Here"]
a8223e
+gdb_breakpoint [gdb_get_line_number "Final Breakpoint"]
a8223e
+
a8223e
+# We're going to print some reasonably large arrays.
a8223e
+gdb_test_no_output "set print elements unlimited"
a8223e
+
a8223e
+gdb_continue_to_breakpoint "Stop Here"
a8223e
+
a8223e
+# Print a slice, capture the convenience variable name created.
a8223e
+set cmd "print array (1:10:2, 1:10:2)"
a8223e
+gdb_test_multiple $cmd $cmd {
a8223e
+    -re "\r\n\\\$(\\d+) = .*\r\n$gdb_prompt $" {
a8223e
+	set varname "\$$expect_out(1,string)"
a8223e
+    }
a8223e
+}
a8223e
+
a8223e
+# Now check that we can correctly extract all the elements from this
a8223e
+# slice.
a8223e
+for { set j 1 } { $j < 6 } { incr j } {
a8223e
+    for { set i 1 } { $i < 6 } { incr i } {
a8223e
+	set val [expr ((($i - 1) * 2) + (($j - 1) * 20)) + 1]
a8223e
+	gdb_test "print ${varname} ($i,$j)" " = $val"
a8223e
+    }
a8223e
+}
a8223e
+
a8223e
+# Now take a slice of the slice.
a8223e
+gdb_test "print ${varname} (3:5, 3:5)" \
a8223e
+    " = \\(\\(45, 47, 49\\) \\(65, 67, 69\\) \\(85, 87, 89\\)\\)"
a8223e
+
a8223e
+# Now take a different slice of a slice.
a8223e
+set cmd "print ${varname} (1:5:2, 1:5:2)"
a8223e
+gdb_test_multiple $cmd $cmd {
a8223e
+    -re "\r\n\\\$(\\d+) = \\(\\(1, 5, 9\\) \\(41, 45, 49\\) \\(81, 85, 89\\)\\)\r\n$gdb_prompt $" {
a8223e
+	set varname "\$$expect_out(1,string)"
a8223e
+	pass $gdb_test_name
a8223e
+    }
a8223e
+}
a8223e
+
a8223e
+# Now take a slice from the slice, of a slice!
a8223e
+set cmd "print ${varname} (1:3:2, 1:3:2)"
a8223e
+gdb_test_multiple $cmd $cmd {
a8223e
+    -re "\r\n\\\$(\\d+) = \\(\\(1, 9\\) \\(81, 89\\)\\)\r\n$gdb_prompt $" {
a8223e
+	set varname "\$$expect_out(1,string)"
a8223e
+	pass $gdb_test_name
a8223e
+    }
a8223e
+}
a8223e
+
a8223e
+# And again!
a8223e
+set cmd "print ${varname} (1:2:2, 1:2:2)"
a8223e
+gdb_test_multiple $cmd $cmd {
a8223e
+    -re "\r\n\\\$(\\d+) = \\(\\(1\\)\\)\r\n$gdb_prompt $" {
a8223e
+	set varname "\$$expect_out(1,string)"
a8223e
+	pass $gdb_test_name
a8223e
+    }
a8223e
+}
a8223e
+
a8223e
+# Test taking a slice with stride of a string.  This isn't actually
a8223e
+# supported within gfortran (at least), but naturally drops out of how
a8223e
+# GDB models arrays and strings in a similar way, so we may as well
a8223e
+# test that this is still working.
a8223e
+gdb_test "print str (1:26:2)" " = 'acegikmoqsuwy'"
a8223e
+gdb_test "print str (26:1:-1)" " = 'zyxwvutsrqponmlkjihgfedcba'"
a8223e
+gdb_test "print str (26:1:-2)" " = 'zxvtrpnljhfdb'"
a8223e
+
a8223e
+# Now test the memory requirements of taking a slice from an array.
a8223e
+# The idea is that we shouldn't require more memory to extract a slice
a8223e
+# than the size of the slice.
a8223e
+#
a8223e
+# This will only work if array repacking is turned on, otherwise GDB
a8223e
+# will create the slice by generating a new type that sits over the
a8223e
+# existing value in memory.
a8223e
+gdb_test_no_output "set fortran repack-array-slices on"
a8223e
+set element_size [get_integer_valueof "sizeof (array (1,1))" "unknown"]
a8223e
+set slice_size [expr $element_size * 4]
a8223e
+gdb_test_no_output "set max-value-size $slice_size"
a8223e
+gdb_test "print array (1:2, 1:2)" "= \\(\\(1, 2\\) \\(11, 12\\)\\)"
a8223e
+gdb_test "print array (2:3, 2:3)" "= \\(\\(12, 13\\) \\(22, 23\\)\\)"
a8223e
+gdb_test "print array (2:5:2, 2:5:2)" "= \\(\\(12, 14\\) \\(32, 34\\)\\)"
a8223e
diff --git a/gdb/testsuite/gdb.fortran/array-slices-sub-slices.f90 b/gdb/testsuite/gdb.fortran/array-slices-sub-slices.f90
a8223e
new file mode 100644
a8223e
--- /dev/null
a8223e
+++ b/gdb/testsuite/gdb.fortran/array-slices-sub-slices.f90
a8223e
@@ -0,0 +1,96 @@
a8223e
+! Copyright 2020 Free Software Foundation, Inc.
a8223e
+!
a8223e
+! This program is free software; you can redistribute it and/or modify
a8223e
+! it under the terms of the GNU General Public License as published by
a8223e
+! the Free Software Foundation; either version 3 of the License, or
a8223e
+! (at your option) any later version.
a8223e
+!
a8223e
+! This program is distributed in the hope that it will be useful,
a8223e
+! but WITHOUT ANY WARRANTY; without even the implied warranty of
a8223e
+! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a8223e
+! GNU General Public License for more details.
a8223e
+!
a8223e
+! You should have received a copy of the GNU General Public License
a8223e
+! along with this program.  If not, see <http://www.gnu.org/licenses/>.
a8223e
+
a8223e
+!
a8223e
+! Start of test program.
a8223e
+!
a8223e
+program test
a8223e
+  integer, dimension (1:10,1:11) :: array
a8223e
+  character (len=26) :: str = "abcdefghijklmnopqrstuvwxyz"
a8223e
+
a8223e
+  call fill_array_2d (array)
a8223e
+
a8223e
+  ! GDB catches this final breakpoint to indicate the end of the test.
a8223e
+  print *, "" ! Stop Here
a8223e
+
a8223e
+  print *, array
a8223e
+  print *, str
a8223e
+
a8223e
+  ! GDB catches this final breakpoint to indicate the end of the test.
a8223e
+  print *, "" ! Final Breakpoint.
a8223e
+
a8223e
+contains
a8223e
+
a8223e
+  ! Fill a 1D array with a unique positive integer in each element.
a8223e
+  subroutine fill_array_1d (array)
a8223e
+    integer, dimension (:) :: array
a8223e
+    integer :: counter
a8223e
+
a8223e
+    counter = 1
a8223e
+    do j=LBOUND (array, 1), UBOUND (array, 1), 1
a8223e
+       array (j) = counter
a8223e
+       counter = counter + 1
a8223e
+    end do
a8223e
+  end subroutine fill_array_1d
a8223e
+
a8223e
+  ! Fill a 2D array with a unique positive integer in each element.
a8223e
+  subroutine fill_array_2d (array)
a8223e
+    integer, dimension (:,:) :: array
a8223e
+    integer :: counter
a8223e
+
a8223e
+    counter = 1
a8223e
+    do i=LBOUND (array, 2), UBOUND (array, 2), 1
a8223e
+       do j=LBOUND (array, 1), UBOUND (array, 1), 1
a8223e
+          array (j,i) = counter
a8223e
+          counter = counter + 1
a8223e
+       end do
a8223e
+    end do
a8223e
+  end subroutine fill_array_2d
a8223e
+
a8223e
+  ! Fill a 3D array with a unique positive integer in each element.
a8223e
+  subroutine fill_array_3d (array)
a8223e
+    integer, dimension (:,:,:) :: array
a8223e
+    integer :: counter
a8223e
+
a8223e
+    counter = 1
a8223e
+    do i=LBOUND (array, 3), UBOUND (array, 3), 1
a8223e
+       do j=LBOUND (array, 2), UBOUND (array, 2), 1
a8223e
+          do k=LBOUND (array, 1), UBOUND (array, 1), 1
a8223e
+             array (k, j,i) = counter
a8223e
+             counter = counter + 1
a8223e
+          end do
a8223e
+       end do
a8223e
+    end do
a8223e
+  end subroutine fill_array_3d
a8223e
+
a8223e
+  ! Fill a 4D array with a unique positive integer in each element.
a8223e
+  subroutine fill_array_4d (array)
a8223e
+    integer, dimension (:,:,:,:) :: array
a8223e
+    integer :: counter
a8223e
+
a8223e
+    counter = 1
a8223e
+    do i=LBOUND (array, 4), UBOUND (array, 4), 1
a8223e
+       do j=LBOUND (array, 3), UBOUND (array, 3), 1
a8223e
+          do k=LBOUND (array, 2), UBOUND (array, 2), 1
a8223e
+             do l=LBOUND (array, 1), UBOUND (array, 1), 1
a8223e
+                array (l, k, j,i) = counter
a8223e
+                counter = counter + 1
a8223e
+             end do
a8223e
+          end do
a8223e
+       end do
a8223e
+    end do
a8223e
+    print *, ""
a8223e
+  end subroutine fill_array_4d
a8223e
+end program test
a8223e
diff --git a/gdb/testsuite/gdb.fortran/array-slices.exp b/gdb/testsuite/gdb.fortran/array-slices.exp
a8223e
--- a/gdb/testsuite/gdb.fortran/array-slices.exp
a8223e
+++ b/gdb/testsuite/gdb.fortran/array-slices.exp
a8223e
@@ -18,6 +18,21 @@
a8223e
 # the subroutine.  This should exercise GDB's ability to handle
a8223e
 # different strides for the different dimensions.
a8223e
 
a8223e
+# Testing GDB's ability to print array (and string) slices, including
a8223e
+# slices that make use of array strides.
a8223e
+#
a8223e
+# In the Fortran code various arrays of different ranks are filled
a8223e
+# with data, and slices are passed to a series of show functions.
a8223e
+#
a8223e
+# In this test script we break in each of the show functions, print
a8223e
+# the array slice that was passed in, and then move up the stack to
a8223e
+# the parent frame and check GDB can manually extract the same slice.
a8223e
+#
a8223e
+# This test also checks that the size of the array slice passed to the
a8223e
+# function (so as extracted and described by the compiler and the
a8223e
+# debug information) matches the size of the slice manually extracted
a8223e
+# by GDB.
a8223e
+
a8223e
 if {[skip_fortran_tests]} { return -1 }
a8223e
 
a8223e
 standard_testfile ".f90"
a8223e
@@ -28,57 +43,224 @@ if {[prepare_for_testing ${testfile}.exp ${testfile} ${srcfile} \
a8223e
     return -1
a8223e
 }
a8223e
 
a8223e
-if ![fortran_runto_main] {
a8223e
-    untested "could not run to main"
a8223e
-    return -1
a8223e
+# Takes the name of an array slice as used in the test source, and extracts
a8223e
+# the base array name.  For example: 'array (1,2)' becomes 'array'.
a8223e
+proc array_slice_to_var { slice_str } {
a8223e
+    regexp "^(?:\\s*\\()*(\[^( \t\]+)" $slice_str matchvar varname
a8223e
+    return $varname
a8223e
 }
a8223e
 
a8223e
-gdb_breakpoint "show"
a8223e
-gdb_breakpoint [gdb_get_line_number "Final Breakpoint"]
a8223e
-
a8223e
-set array_contents \
a8223e
-    [list \
a8223e
-	 " = \\(\\(1, 2, 3, 4, 5, 6, 7, 8, 9, 10\\) \\(11, 12, 13, 14, 15, 16, 17, 18, 19, 20\\) \\(21, 22, 23, 24, 25, 26, 27, 28, 29, 30\\) \\(31, 32, 33, 34, 35, 36, 37, 38, 39, 40\\) \\(41, 42, 43, 44, 45, 46, 47, 48, 49, 50\\) \\(51, 52, 53, 54, 55, 56, 57, 58, 59, 60\\) \\(61, 62, 63, 64, 65, 66, 67, 68, 69, 70\\) \\(71, 72, 73, 74, 75, 76, 77, 78, 79, 80\\) \\(81, 82, 83, 84, 85, 86, 87, 88, 89, 90\\) \\(91, 92, 93, 94, 95, 96, 97, 98, 99, 100\\)\\)" \
a8223e
-	 " = \\(\\(1, 2, 3, 4, 5\\) \\(11, 12, 13, 14, 15\\) \\(21, 22, 23, 24, 25\\) \\(31, 32, 33, 34, 35\\) \\(41, 42, 43, 44, 45\\)\\)" \
a8223e
-	 " = \\(\\(1, 3, 5, 7, 9\\) \\(21, 23, 25, 27, 29\\) \\(41, 43, 45, 47, 49\\) \\(61, 63, 65, 67, 69\\) \\(81, 83, 85, 87, 89\\)\\)" \
a8223e
-	 " = \\(\\(1, 4, 7, 10\\) \\(21, 24, 27, 30\\) \\(41, 44, 47, 50\\) \\(61, 64, 67, 70\\) \\(81, 84, 87, 90\\)\\)" \
a8223e
-	 " = \\(\\(1, 5, 9\\) \\(31, 35, 39\\) \\(61, 65, 69\\) \\(91, 95, 99\\)\\)" \
a8223e
-	 " = \\(\\(-26, -25, -24, -23, -22, -21, -20, -19, -18, -17\\) \\(-19, -18, -17, -16, -15, -14, -13, -12, -11, -10\\) \\(-12, -11, -10, -9, -8, -7, -6, -5, -4, -3\\) \\(-5, -4, -3, -2, -1, 0, 1, 2, 3, 4\\) \\(2, 3, 4, 5, 6, 7, 8, 9, 10, 11\\) \\(9, 10, 11, 12, 13, 14, 15, 16, 17, 18\\) \\(16, 17, 18, 19, 20, 21, 22, 23, 24, 25\\) \\(23, 24, 25, 26, 27, 28, 29, 30, 31, 32\\) \\(30, 31, 32, 33, 34, 35, 36, 37, 38, 39\\) \\(37, 38, 39, 40, 41, 42, 43, 44, 45, 46\\)\\)" \
a8223e
-	 " = \\(\\(-26, -25, -24, -23, -22, -21\\) \\(-19, -18, -17, -16, -15, -14\\) \\(-12, -11, -10, -9, -8, -7\\)\\)" \
a8223e
-	 " = \\(\\(-26, -24, -22, -20, -18\\) \\(-5, -3, -1, 1, 3\\) \\(16, 18, 20, 22, 24\\) \\(37, 39, 41, 43, 45\\)\\)" ]
a8223e
-
a8223e
-set message_strings \
a8223e
-    [list \
a8223e
-	 " = 'array'" \
a8223e
-	 " = 'array \\(1:5,1:5\\)'" \
a8223e
-	 " = 'array \\(1:10:2,1:10:2\\)'" \
a8223e
-	 " = 'array \\(1:10:3,1:10:2\\)'" \
a8223e
-	 " = 'array \\(1:10:5,1:10:3\\)'" ]
a8223e
-
a8223e
-set i 0
a8223e
-foreach result $array_contents msg $message_strings {
a8223e
-    incr i
a8223e
-    with_test_prefix "test $i" {
a8223e
-	gdb_continue_to_breakpoint "show"
a8223e
-	gdb_test "p array" $result
a8223e
-	gdb_test "p message" "$msg"
a8223e
+proc run_test { repack } {
a8223e
+    global binfile gdb_prompt
a8223e
+
a8223e
+    clean_restart ${binfile}
a8223e
+
a8223e
+    if ![fortran_runto_main] {
a8223e
+	untested "could not run to main"
a8223e
+	return -1
a8223e
     }
a8223e
-}
a8223e
 
a8223e
-gdb_continue_to_breakpoint "continue to Final Breakpoint"
a8223e
+    gdb_test_no_output "set fortran repack-array-slices $repack"
a8223e
+
a8223e
+    # gdb_breakpoint [gdb_get_line_number "Display Message Breakpoint"]
a8223e
+    gdb_breakpoint [gdb_get_line_number "Display Element"]
a8223e
+    gdb_breakpoint [gdb_get_line_number "Display String"]
a8223e
+    gdb_breakpoint [gdb_get_line_number "Display Array Slice 1D"]
a8223e
+    gdb_breakpoint [gdb_get_line_number "Display Array Slice 2D"]
a8223e
+    gdb_breakpoint [gdb_get_line_number "Display Array Slice 3D"]
a8223e
+    gdb_breakpoint [gdb_get_line_number "Display Array Slice 4D"]
a8223e
+    gdb_breakpoint [gdb_get_line_number "Final Breakpoint"]
a8223e
+
a8223e
+    # We're going to print some reasonably large arrays.
a8223e
+    gdb_test_no_output "set print elements unlimited"
a8223e
+
a8223e
+    set found_final_breakpoint false
a8223e
+
a8223e
+    # We place a limit on the number of tests that can be run, just in
a8223e
+    # case something goes wrong, and GDB gets stuck in an loop here.
a8223e
+    set test_count 0
a8223e
+    while { $test_count < 500 } {
a8223e
+	with_test_prefix "test $test_count" {
a8223e
+	    incr test_count
a8223e
+
a8223e
+	    set found_final_breakpoint false
a8223e
+	    set expected_result ""
a8223e
+	    set func_name ""
a8223e
+	    gdb_test_multiple "continue" "continue" {
a8223e
+		-re ".*GDB = (\[^\r\n\]+)\r\n" {
a8223e
+		    set expected_result $expect_out(1,string)
a8223e
+		    exp_continue
a8223e
+		}
a8223e
+		-re "! Display Element" {
a8223e
+		    set func_name "show_elem"
a8223e
+		    exp_continue
a8223e
+		}
a8223e
+		-re "! Display String" {
a8223e
+		    set func_name "show_str"
a8223e
+		    exp_continue
a8223e
+		}
a8223e
+		-re "! Display Array Slice (.)D" {
a8223e
+		    set func_name "show_$expect_out(1,string)d"
a8223e
+		    exp_continue
a8223e
+		}
a8223e
+		-re "! Final Breakpoint" {
a8223e
+		    set found_final_breakpoint true
a8223e
+		    exp_continue
a8223e
+		}
a8223e
+		-re "$gdb_prompt $" {
a8223e
+		    # We're done.
a8223e
+		}
a8223e
+	    }
a8223e
 
a8223e
-# Next test that asking for an array with stride at the CLI gives an
a8223e
-# error.
a8223e
-clean_restart ${testfile}
a8223e
+	    if ($found_final_breakpoint) {
a8223e
+		break
a8223e
+	    }
a8223e
 
a8223e
-if ![fortran_runto_main] then {
a8223e
-    perror "couldn't run to main"
a8223e
-    continue
a8223e
+	    # We want to take a look at the line in the previous frame that
a8223e
+	    # called the current function.  I couldn't find a better way of
a8223e
+	    # doing this than 'up', which will print the line, then 'down'
a8223e
+	    # again.
a8223e
+	    #
a8223e
+	    # I don't want to fill the log with passes for these up/down
a8223e
+	    # commands, so we don't report any.  If something goes wrong then we
a8223e
+	    # should get a fail from gdb_test_multiple.
a8223e
+	    set array_slice_name ""
a8223e
+	    set unique_id ""
a8223e
+	    array unset replacement_vars
a8223e
+	    array set replacement_vars {}
a8223e
+	    gdb_test_multiple "up" "up" {
a8223e
+		-re "\r\n\[0-9\]+\[ \t\]+call ${func_name} \\((\[^\r\n\]+)\\)\r\n$gdb_prompt $" {
a8223e
+		    set array_slice_name $expect_out(1,string)
a8223e
+		}
a8223e
+		-re "\r\n\[0-9\]+\[ \t\]+call ${func_name} \\((\[^\r\n\]+)\\)\[ \t\]+! VARS=(\[^ \t\r\n\]+)\r\n$gdb_prompt $" {
a8223e
+		    set array_slice_name $expect_out(1,string)
a8223e
+		    set unique_id $expect_out(2,string)
a8223e
+		}
a8223e
+	    }
a8223e
+	    if {$unique_id != ""} {
a8223e
+		set str ""
a8223e
+		foreach v [split $unique_id ,] {
a8223e
+		    set val [get_integer_valueof "${v}" "??"\
a8223e
+				 "get variable '$v' for '$array_slice_name'"]
a8223e
+		    set replacement_vars($v) $val
a8223e
+		    if {$str != ""} {
a8223e
+			set str "Str,"
a8223e
+		    }
a8223e
+		    set str "$str$v=$val"
a8223e
+		}
a8223e
+		set unique_id " $str"
a8223e
+	    }
a8223e
+	    gdb_test_multiple "down" "down" {
a8223e
+		-re "\r\n$gdb_prompt $" {
a8223e
+		    # Don't issue a pass here.
a8223e
+		}
a8223e
+	    }
a8223e
+
a8223e
+	    # Check we have all the information we need to successfully run one
a8223e
+	    # of these tests.
a8223e
+	    if { $expected_result == "" } {
a8223e
+		perror "failed to extract expected results"
a8223e
+		return 0
a8223e
+	    }
a8223e
+	    if { $array_slice_name == "" } {
a8223e
+		perror "failed to extract array slice name"
a8223e
+		return 0
a8223e
+	    }
a8223e
+
a8223e
+	    # Check GDB can correctly print the array slice that was passed into
a8223e
+	    # the current frame.
a8223e
+	    set pattern [string_to_regexp " = $expected_result"]
a8223e
+	    gdb_test "p array" "$pattern" \
a8223e
+		"check value of '$array_slice_name'$unique_id"
a8223e
+
a8223e
+	    # Get the size of the slice.
a8223e
+	    set size_in_show \
a8223e
+		[get_integer_valueof "sizeof (array)" "show_unknown" \
a8223e
+		     "get sizeof '$array_slice_name'$unique_id in show"]
a8223e
+	    set addr_in_show \
a8223e
+		[get_hexadecimal_valueof "&array" "show_unknown" \
a8223e
+		     "get address '$array_slice_name'$unique_id in show"]
a8223e
+
a8223e
+	    # Now move into the previous frame, and see if GDB can extract the
a8223e
+	    # array slice from the original parent object.  Again, use of
a8223e
+	    # gdb_test_multiple to avoid filling the logs with unnecessary
a8223e
+	    # passes.
a8223e
+	    gdb_test_multiple "up" "up" {
a8223e
+		-re "\r\n$gdb_prompt $" {
a8223e
+		    # Do nothing.
a8223e
+		}
a8223e
+	    }
a8223e
+
a8223e
+	    # Print the array slice, this will force GDB to manually extract the
a8223e
+	    # slice from the parent array.
a8223e
+	    gdb_test "p $array_slice_name" "$pattern" \
a8223e
+		"check array slice '$array_slice_name'$unique_id can be extracted"
a8223e
+
a8223e
+	    # Get the size of the slice in the calling frame.
a8223e
+	    set size_in_parent \
a8223e
+		[get_integer_valueof "sizeof ($array_slice_name)" \
a8223e
+		     "parent_unknown" \
a8223e
+		     "get sizeof '$array_slice_name'$unique_id in parent"]
a8223e
+
a8223e
+	    # Figure out the start and end addresses of the full array in the
a8223e
+	    # parent frame.
a8223e
+	    set full_var_name [array_slice_to_var $array_slice_name]
a8223e
+	    set start_addr [get_hexadecimal_valueof "&${full_var_name}" \
a8223e
+				"start unknown"]
a8223e
+	    set end_addr [get_hexadecimal_valueof \
a8223e
+			      "(&${full_var_name}) + sizeof (${full_var_name})" \
a8223e
+			      "end unknown"]
a8223e
+
a8223e
+	    # The Fortran compiler can choose to either send a descriptor that
a8223e
+	    # describes the array slice to the subroutine, or it can repack the
a8223e
+	    # slice into an array section and send that.
a8223e
+	    #
a8223e
+	    # We find the address range of the original array in the parent,
a8223e
+	    # and the address of the slice in the show function, if the
a8223e
+	    # address of the slice (from show) is in the range of the original
a8223e
+	    # array then repacking has not occurred, otherwise, the slice is
a8223e
+	    # outside of the parent, and repacking must have occurred.
a8223e
+	    #
a8223e
+	    # The goal here is to compare the sizes of the slice in show with
a8223e
+	    # the size of the slice extracted by GDB.  So we can only compare
a8223e
+	    # sizes when GDB's repacking setting matches the repacking
a8223e
+	    # behaviour we got from the compiler.
a8223e
+	    if { ($addr_in_show < $start_addr || $addr_in_show >= $end_addr) \
a8223e
+		 == ($repack == "on") } {
a8223e
+		gdb_assert {$size_in_show == $size_in_parent} \
a8223e
+		    "check sizes match"
a8223e
+	    } elseif { $repack == "off" } {
a8223e
+		# GDB's repacking is off (so slices are left unpacked), but
a8223e
+		# the compiler did pack this one.  As a result we can't
a8223e
+		# compare the sizes between the compiler's slice and GDB's
a8223e
+		# slice.
a8223e
+		verbose -log "slice '$array_slice_name' was repacked, sizes can't be compared"
a8223e
+	    } else {
a8223e
+		# Like the above, but the reverse, GDB's repacking is on, but
a8223e
+		# the compiler didn't repack this slice.
a8223e
+		verbose -log "slice '$array_slice_name' was not repacked, sizes can't be compared"
a8223e
+	    }
a8223e
+
a8223e
+	    # If the array name we just tested included variable names, then
a8223e
+	    # test again with all the variables expanded.
a8223e
+	    if {$unique_id != ""} {
a8223e
+		foreach v [array names replacement_vars] {
a8223e
+		    set val $replacement_vars($v)
a8223e
+		    set array_slice_name \
a8223e
+			[regsub "\\y${v}\\y" $array_slice_name $val]
a8223e
+		}
a8223e
+		gdb_test "p $array_slice_name" "$pattern" \
a8223e
+		    "check array slice '$array_slice_name'$unique_id can be extracted, with variables expanded"
a8223e
+	    }
a8223e
+	}
a8223e
+    }
a8223e
+
a8223e
+    # Ensure we reached the final breakpoint.  If more tests have been added
a8223e
+    # to the test script, and this starts failing, then the safety 'while'
a8223e
+    # loop above might need to be increased.
a8223e
+    gdb_assert {$found_final_breakpoint} "ran all tests"
a8223e
 }
a8223e
 
a8223e
-gdb_breakpoint "show"
a8223e
-gdb_continue_to_breakpoint "show"
a8223e
-gdb_test "up" ".*"
a8223e
-gdb_test "p array (1:10:2, 1:10:2)" \
a8223e
-    "Fortran array strides are not currently supported" \
a8223e
-    "using array stride gives an error"
a8223e
+foreach_with_prefix repack { on off } {
a8223e
+    run_test $repack
a8223e
+}
a8223e
diff --git a/gdb/testsuite/gdb.fortran/array-slices.f90 b/gdb/testsuite/gdb.fortran/array-slices.f90
a8223e
--- a/gdb/testsuite/gdb.fortran/array-slices.f90
a8223e
+++ b/gdb/testsuite/gdb.fortran/array-slices.f90
a8223e
@@ -13,58 +13,368 @@
a8223e
 ! You should have received a copy of the GNU General Public License
a8223e
 ! along with this program.  If not, see <http://www.gnu.org/licenses/>.
a8223e
 
a8223e
-subroutine show (message, array)
a8223e
-  character (len=*) :: message
a8223e
+subroutine show_elem (array)
a8223e
+  integer :: array
a8223e
+
a8223e
+  print *, ""
a8223e
+  print *, "Expected GDB Output:"
a8223e
+  print *, ""
a8223e
+
a8223e
+  write(*, fmt="(A)", advance="no") "GDB = "
a8223e
+  write(*, fmt="(I0)", advance="no") array
a8223e
+  write(*, fmt="(A)", advance="yes") ""
a8223e
+
a8223e
+  print *, ""	! Display Element
a8223e
+end subroutine show_elem
a8223e
+
a8223e
+subroutine show_str (array)
a8223e
+  character (len=*) :: array
a8223e
+
a8223e
+  print *, ""
a8223e
+  print *, "Expected GDB Output:"
a8223e
+  print *, ""
a8223e
+  write (*, fmt="(A)", advance="no") "GDB = '"
a8223e
+  write (*, fmt="(A)", advance="no") array
a8223e
+  write (*, fmt="(A)", advance="yes") "'"
a8223e
+
a8223e
+  print *, ""	! Display String
a8223e
+end subroutine show_str
a8223e
+
a8223e
+subroutine show_1d (array)
a8223e
+  integer, dimension (:) :: array
a8223e
+
a8223e
+  print *, "Array Contents:"
a8223e
+  print *, ""
a8223e
+
a8223e
+  do i=LBOUND (array, 1), UBOUND (array, 1), 1
a8223e
+     write(*, fmt="(i4)", advance="no") array (i)
a8223e
+  end do
a8223e
+
a8223e
+  print *, ""
a8223e
+  print *, "Expected GDB Output:"
a8223e
+  print *, ""
a8223e
+
a8223e
+  write(*, fmt="(A)", advance="no") "GDB = ("
a8223e
+  do i=LBOUND (array, 1), UBOUND (array, 1), 1
a8223e
+     if (i > LBOUND (array, 1)) then
a8223e
+        write(*, fmt="(A)", advance="no") ", "
a8223e
+     end if
a8223e
+     write(*, fmt="(I0)", advance="no") array (i)
a8223e
+  end do
a8223e
+  write(*, fmt="(A)", advance="no") ")"
a8223e
+
a8223e
+  print *, ""	! Display Array Slice 1D
a8223e
+end subroutine show_1d
a8223e
+
a8223e
+subroutine show_2d (array)
a8223e
   integer, dimension (:,:) :: array
a8223e
 
a8223e
-  print *, message
a8223e
+  print *, "Array Contents:"
a8223e
+  print *, ""
a8223e
+
a8223e
   do i=LBOUND (array, 2), UBOUND (array, 2), 1
a8223e
      do j=LBOUND (array, 1), UBOUND (array, 1), 1
a8223e
         write(*, fmt="(i4)", advance="no") array (j, i)
a8223e
      end do
a8223e
      print *, ""
a8223e
- end do
a8223e
- print *, array
a8223e
- print *, ""
a8223e
+  end do
a8223e
 
a8223e
-end subroutine show
a8223e
+  print *, ""
a8223e
+  print *, "Expected GDB Output:"
a8223e
+  print *, ""
a8223e
 
a8223e
-program test
a8223e
+  write(*, fmt="(A)", advance="no") "GDB = ("
a8223e
+  do i=LBOUND (array, 2), UBOUND (array, 2), 1
a8223e
+     if (i > LBOUND (array, 2)) then
a8223e
+        write(*, fmt="(A)", advance="no") " "
a8223e
+     end if
a8223e
+     write(*, fmt="(A)", advance="no") "("
a8223e
+     do j=LBOUND (array, 1), UBOUND (array, 1), 1
a8223e
+        if (j > LBOUND (array, 1)) then
a8223e
+           write(*, fmt="(A)", advance="no") ", "
a8223e
+        end if
a8223e
+        write(*, fmt="(I0)", advance="no") array (j, i)
a8223e
+     end do
a8223e
+     write(*, fmt="(A)", advance="no") ")"
a8223e
+  end do
a8223e
+  write(*, fmt="(A)", advance="yes") ")"
a8223e
+
a8223e
+  print *, ""	! Display Array Slice 2D
a8223e
+end subroutine show_2d
a8223e
+
a8223e
+subroutine show_3d (array)
a8223e
+  integer, dimension (:,:,:) :: array
a8223e
+
a8223e
+  print *, ""
a8223e
+  print *, "Expected GDB Output:"
a8223e
+  print *, ""
a8223e
+
a8223e
+  write(*, fmt="(A)", advance="no") "GDB = ("
a8223e
+  do i=LBOUND (array, 3), UBOUND (array, 3), 1
a8223e
+     if (i > LBOUND (array, 3)) then
a8223e
+        write(*, fmt="(A)", advance="no") " "
a8223e
+     end if
a8223e
+     write(*, fmt="(A)", advance="no") "("
a8223e
+     do j=LBOUND (array, 2), UBOUND (array, 2), 1
a8223e
+        if (j > LBOUND (array, 2)) then
a8223e
+           write(*, fmt="(A)", advance="no") " "
a8223e
+        end if
a8223e
+        write(*, fmt="(A)", advance="no") "("
a8223e
+        do k=LBOUND (array, 1), UBOUND (array, 1), 1
a8223e
+           if (k > LBOUND (array, 1)) then
a8223e
+              write(*, fmt="(A)", advance="no") ", "
a8223e
+           end if
a8223e
+           write(*, fmt="(I0)", advance="no") array (k, j, i)
a8223e
+        end do
a8223e
+        write(*, fmt="(A)", advance="no") ")"
a8223e
+     end do
a8223e
+     write(*, fmt="(A)", advance="no") ")"
a8223e
+  end do
a8223e
+  write(*, fmt="(A)", advance="yes") ")"
a8223e
+
a8223e
+  print *, ""	! Display Array Slice 3D
a8223e
+end subroutine show_3d
a8223e
+
a8223e
+subroutine show_4d (array)
a8223e
+  integer, dimension (:,:,:,:) :: array
a8223e
+
a8223e
+  print *, ""
a8223e
+  print *, "Expected GDB Output:"
a8223e
+  print *, ""
a8223e
+
a8223e
+  write(*, fmt="(A)", advance="no") "GDB = ("
a8223e
+  do i=LBOUND (array, 4), UBOUND (array, 4), 1
a8223e
+     if (i > LBOUND (array, 4)) then
a8223e
+        write(*, fmt="(A)", advance="no") " "
a8223e
+     end if
a8223e
+     write(*, fmt="(A)", advance="no") "("
a8223e
+     do j=LBOUND (array, 3), UBOUND (array, 3), 1
a8223e
+        if (j > LBOUND (array, 3)) then
a8223e
+           write(*, fmt="(A)", advance="no") " "
a8223e
+        end if
a8223e
+        write(*, fmt="(A)", advance="no") "("
a8223e
+
a8223e
+        do k=LBOUND (array, 2), UBOUND (array, 2), 1
a8223e
+           if (k > LBOUND (array, 2)) then
a8223e
+              write(*, fmt="(A)", advance="no") " "
a8223e
+           end if
a8223e
+           write(*, fmt="(A)", advance="no") "("
a8223e
+           do l=LBOUND (array, 1), UBOUND (array, 1), 1
a8223e
+              if (l > LBOUND (array, 1)) then
a8223e
+                 write(*, fmt="(A)", advance="no") ", "
a8223e
+              end if
a8223e
+              write(*, fmt="(I0)", advance="no") array (l, k, j, i)
a8223e
+           end do
a8223e
+           write(*, fmt="(A)", advance="no") ")"
a8223e
+        end do
a8223e
+        write(*, fmt="(A)", advance="no") ")"
a8223e
+     end do
a8223e
+     write(*, fmt="(A)", advance="no") ")"
a8223e
+  end do
a8223e
+  write(*, fmt="(A)", advance="yes") ")"
a8223e
+
a8223e
+  print *, ""	! Display Array Slice 4D
a8223e
+end subroutine show_4d
a8223e
 
a8223e
+!
a8223e
+! Start of test program.
a8223e
+!
a8223e
+program test
a8223e
   interface
a8223e
-     subroutine show (message, array)
a8223e
-       character (len=*) :: message
a8223e
+     subroutine show_str (array)
a8223e
+       character (len=*) :: array
a8223e
+     end subroutine show_str
a8223e
+
a8223e
+     subroutine show_1d (array)
a8223e
+       integer, dimension (:) :: array
a8223e
+     end subroutine show_1d
a8223e
+
a8223e
+     subroutine show_2d (array)
a8223e
        integer, dimension(:,:) :: array
a8223e
-     end subroutine show
a8223e
+     end subroutine show_2d
a8223e
+
a8223e
+     subroutine show_3d (array)
a8223e
+       integer, dimension(:,:,:) :: array
a8223e
+     end subroutine show_3d
a8223e
+
a8223e
+     subroutine show_4d (array)
a8223e
+       integer, dimension(:,:,:,:) :: array
a8223e
+     end subroutine show_4d
a8223e
   end interface
a8223e
 
a8223e
+  ! Declare variables used in this test.
a8223e
+  integer, dimension (-10:-1,-10:-2) :: neg_array
a8223e
   integer, dimension (1:10,1:10) :: array
a8223e
   integer, allocatable :: other (:, :)
a8223e
+  character (len=26) :: str_1 = "abcdefghijklmnopqrstuvwxyz"
a8223e
+  integer, dimension (-2:2,-2:2,-2:2) :: array3d
a8223e
+  integer, dimension (-3:3,7:10,-3:3,-10:-7) :: array4d
a8223e
+  integer, dimension (10:20) :: array1d
a8223e
+  integer, dimension(:,:), pointer :: pointer2d => null()
a8223e
+  integer, dimension(-1:9,-1:9), target :: tarray
a8223e
 
a8223e
+  ! Allocate or associate any variables as needed.
a8223e
   allocate (other (-5:4, -2:7))
a8223e
+  pointer2d => tarray
a8223e
 
a8223e
-  do i=LBOUND (array, 2), UBOUND (array, 2), 1
a8223e
-     do j=LBOUND (array, 1), UBOUND (array, 1), 1
a8223e
-        array (j,i) = ((i - 1) * UBOUND (array, 2)) + j
a8223e
-     end do
a8223e
-  end do
a8223e
+  ! Fill arrays with contents ready for testing.
a8223e
+  call fill_array_1d (array1d)
a8223e
+
a8223e
+  call fill_array_2d (neg_array)
a8223e
+  call fill_array_2d (array)
a8223e
+  call fill_array_2d (other)
a8223e
+  call fill_array_2d (tarray)
a8223e
+
a8223e
+  call fill_array_3d (array3d)
a8223e
+  call fill_array_4d (array4d)
a8223e
+
a8223e
+  ! The tests.  Each call to a show_* function must have a unique set
a8223e
+  ! of arguments as GDB uses the arguments are part of the test name
a8223e
+  ! string, so duplicate arguments will result in duplicate test
a8223e
+  ! names.
a8223e
+  !
a8223e
+  ! If a show_* line ends with VARS=... where '...' is a comma
a8223e
+  ! separated list of variable names, these variables are assumed to
a8223e
+  ! be part of the call line, and will be expanded by the test script,
a8223e
+  ! for example:
a8223e
+  !
a8223e
+  !     do x=1,9,1
a8223e
+  !       do y=x,10,1
a8223e
+  !         call show_1d (some_array (x,y))	! VARS=x,y
a8223e
+  !       end do
a8223e
+  !     end do
a8223e
+  !
a8223e
+  ! In this example the test script will automatically expand 'x' and
a8223e
+  ! 'y' in order to better test different aspects of GDB.  Do take
a8223e
+  ! care, the expansion is not very "smart", so try to avoid clashing
a8223e
+  ! with other text on the line, in the example above, avoid variables
a8223e
+  ! named 'some' or 'array', as these will likely clash with
a8223e
+  ! 'some_array'.
a8223e
+  call show_str (str_1)
a8223e
+  call show_str (str_1 (1:20))
a8223e
+  call show_str (str_1 (10:20))
a8223e
 
a8223e
-  do i=LBOUND (other, 2), UBOUND (other, 2), 1
a8223e
-     do j=LBOUND (other, 1), UBOUND (other, 1), 1
a8223e
-        other (j,i) = ((i - 1) * UBOUND (other, 2)) + j
a8223e
+  call show_elem (array1d (11))
a8223e
+  call show_elem (pointer2d (2,3))
a8223e
+
a8223e
+  call show_1d (array1d)
a8223e
+  call show_1d (array1d (13:17))
a8223e
+  call show_1d (array1d (17:13:-1))
a8223e
+  call show_1d (array (1:5,1))
a8223e
+  call show_1d (array4d (1,7,3,:))
a8223e
+  call show_1d (pointer2d (-1:3, 2))
a8223e
+  call show_1d (pointer2d (-1, 2:4))
a8223e
+
a8223e
+  ! Enclosing the array slice argument in (...) causess gfortran to
a8223e
+  ! repack the array.
a8223e
+  call show_1d ((array (1:5,1)))
a8223e
+
a8223e
+  call show_2d (pointer2d)
a8223e
+  call show_2d (array)
a8223e
+  call show_2d (array (1:5,1:5))
a8223e
+  do i=1,10,2
a8223e
+     do j=1,10,3
a8223e
+        call show_2d (array (1:10:i,1:10:j))	! VARS=i,j
a8223e
+        call show_2d (array (10:1:-i,1:10:j))	! VARS=i,j
a8223e
+        call show_2d (array (10:1:-i,10:1:-j))	! VARS=i,j
a8223e
+        call show_2d (array (1:10:i,10:1:-j))	! VARS=i,j
a8223e
      end do
a8223e
   end do
a8223e
+  call show_2d (array (6:2:-1,3:9))
a8223e
+  call show_2d (array (1:10:2, 1:10:2))
a8223e
+  call show_2d (other)
a8223e
+  call show_2d (other (-5:0, -2:0))
a8223e
+  call show_2d (other (-5:4:2, -2:7:3))
a8223e
+  call show_2d (neg_array)
a8223e
+  call show_2d (neg_array (-10:-3,-8:-4:2))
a8223e
+
a8223e
+  ! Enclosing the array slice argument in (...) causess gfortran to
a8223e
+  ! repack the array.
a8223e
+  call show_2d ((array (1:10:3, 1:10:2)))
a8223e
+  call show_2d ((neg_array (-10:-3,-8:-4:2)))
a8223e
 
a8223e
-  call show ("array", array)
a8223e
-  call show ("array (1:5,1:5)", array (1:5,1:5))
a8223e
-  call show ("array (1:10:2,1:10:2)", array (1:10:2,1:10:2))
a8223e
-  call show ("array (1:10:3,1:10:2)", array (1:10:3,1:10:2))
a8223e
-  call show ("array (1:10:5,1:10:3)", array (1:10:4,1:10:3))
a8223e
+  call show_3d (array3d)
a8223e
+  call show_3d (array3d(-1:1,-1:1,-1:1))
a8223e
+  call show_3d (array3d(1:-1:-1,1:-1:-1,1:-1:-1))
a8223e
 
a8223e
-  call show ("other", other)
a8223e
-  call show ("other (-5:0, -2:0)", other (-5:0, -2:0))
a8223e
-  call show ("other (-5:4:2, -2:7:3)", other (-5:4:2, -2:7:3))
a8223e
+  ! Enclosing the array slice argument in (...) causess gfortran to
a8223e
+  ! repack the array.
a8223e
+  call show_3d ((array3d(1:-1:-1,1:-1:-1,1:-1:-1)))
a8223e
 
a8223e
+  call show_4d (array4d)
a8223e
+  call show_4d (array4d (-3:0,10:7:-1,0:3,-7:-10:-1))
a8223e
+  call show_4d (array4d (3:0:-1, 10:7:-1, :, -7:-10:-1))
a8223e
+
a8223e
+  ! Enclosing the array slice argument in (...) causess gfortran to
a8223e
+  ! repack the array.
a8223e
+  call show_4d ((array4d (3:-2:-2, 10:7:-2, :, -7:-10:-1)))
a8223e
+
a8223e
+  ! All done.  Deallocate.
a8223e
   deallocate (other)
a8223e
+
a8223e
+  ! GDB catches this final breakpoint to indicate the end of the test.
a8223e
   print *, "" ! Final Breakpoint.
a8223e
+
a8223e
+contains
a8223e
+
a8223e
+  ! Fill a 1D array with a unique positive integer in each element.
a8223e
+  subroutine fill_array_1d (array)
a8223e
+    integer, dimension (:) :: array
a8223e
+    integer :: counter
a8223e
+
a8223e
+    counter = 1
a8223e
+    do j=LBOUND (array, 1), UBOUND (array, 1), 1
a8223e
+       array (j) = counter
a8223e
+       counter = counter + 1
a8223e
+    end do
a8223e
+  end subroutine fill_array_1d
a8223e
+
a8223e
+  ! Fill a 2D array with a unique positive integer in each element.
a8223e
+  subroutine fill_array_2d (array)
a8223e
+    integer, dimension (:,:) :: array
a8223e
+    integer :: counter
a8223e
+
a8223e
+    counter = 1
a8223e
+    do i=LBOUND (array, 2), UBOUND (array, 2), 1
a8223e
+       do j=LBOUND (array, 1), UBOUND (array, 1), 1
a8223e
+          array (j,i) = counter
a8223e
+          counter = counter + 1
a8223e
+       end do
a8223e
+    end do
a8223e
+  end subroutine fill_array_2d
a8223e
+
a8223e
+  ! Fill a 3D array with a unique positive integer in each element.
a8223e
+  subroutine fill_array_3d (array)
a8223e
+    integer, dimension (:,:,:) :: array
a8223e
+    integer :: counter
a8223e
+
a8223e
+    counter = 1
a8223e
+    do i=LBOUND (array, 3), UBOUND (array, 3), 1
a8223e
+       do j=LBOUND (array, 2), UBOUND (array, 2), 1
a8223e
+          do k=LBOUND (array, 1), UBOUND (array, 1), 1
a8223e
+             array (k, j,i) = counter
a8223e
+             counter = counter + 1
a8223e
+          end do
a8223e
+       end do
a8223e
+    end do
a8223e
+  end subroutine fill_array_3d
a8223e
+
a8223e
+  ! Fill a 4D array with a unique positive integer in each element.
a8223e
+  subroutine fill_array_4d (array)
a8223e
+    integer, dimension (:,:,:,:) :: array
a8223e
+    integer :: counter
a8223e
+
a8223e
+    counter = 1
a8223e
+    do i=LBOUND (array, 4), UBOUND (array, 4), 1
a8223e
+       do j=LBOUND (array, 3), UBOUND (array, 3), 1
a8223e
+          do k=LBOUND (array, 2), UBOUND (array, 2), 1
a8223e
+             do l=LBOUND (array, 1), UBOUND (array, 1), 1
a8223e
+                array (l, k, j,i) = counter
a8223e
+                counter = counter + 1
a8223e
+             end do
a8223e
+          end do
a8223e
+       end do
a8223e
+    end do
a8223e
+    print *, ""
a8223e
+  end subroutine fill_array_4d
a8223e
 end program test
a8223e
diff --git a/gdb/testsuite/gdb.fortran/vla-sizeof.exp b/gdb/testsuite/gdb.fortran/vla-sizeof.exp
a8223e
--- a/gdb/testsuite/gdb.fortran/vla-sizeof.exp
a8223e
+++ b/gdb/testsuite/gdb.fortran/vla-sizeof.exp
a8223e
@@ -44,7 +44,7 @@ gdb_continue_to_breakpoint "vla1-allocated"
a8223e
 gdb_test "print sizeof(vla1)" " = 4000" "print sizeof allocated vla1"
a8223e
 gdb_test "print sizeof(vla1(3,2,1))" "4" \
a8223e
     "print sizeof element from allocated vla1"
a8223e
-gdb_test "print sizeof(vla1(3:4,2,1))" "800" \
a8223e
+gdb_test "print sizeof(vla1(3:4,2,1))" "8" \
a8223e
     "print sizeof sliced vla1"
a8223e
 
a8223e
 # Try to access values in undefined pointer to VLA (dangling)
a8223e
@@ -61,7 +61,7 @@ gdb_continue_to_breakpoint "pvla-associated"
a8223e
 gdb_test "print sizeof(pvla)" " = 4000" "print sizeof associated pvla"
a8223e
 gdb_test "print sizeof(pvla(3,2,1))" "4" \
a8223e
     "print sizeof element from associated pvla"
a8223e
-gdb_test "print sizeof(pvla(3:4,2,1))" "800" "print sizeof sliced pvla"
a8223e
+gdb_test "print sizeof(pvla(3:4,2,1))" "8" "print sizeof sliced pvla"
a8223e
 
a8223e
 gdb_breakpoint [gdb_get_line_number "vla1-neg-bounds-v1"]
a8223e
 gdb_continue_to_breakpoint "vla1-neg-bounds-v1"