Jan Kratochvil fdbd5e3
http://sourceware.org/ml/gdb-patches/2016-02/msg00844.html
Jan Kratochvil fdbd5e3
Subject: [PATCH v2 5/6] fortran: calculate subarray with stride values.
Jan Kratochvil fdbd5e3
Jan Kratochvil 046f33b
From: Christoph Weinmann <christoph.t.weinmann@intel.com>
Jan Kratochvil 046f33b
Jan Kratochvil 046f33b
Calculate elements of a subarray using a provided stride value
Jan Kratochvil 046f33b
The stride value can be a positive or negative integer, but may
Jan Kratochvil 046f33b
not be zero.  If no stride is provided, use the default value
Jan Kratochvil 046f33b
1 to print all elements inside the range.
Jan Kratochvil 046f33b
Jan Kratochvil 046f33b
1| program prog
Jan Kratochvil 046f33b
2|   integer :: ary(10) = (/ (i, i=1, 10) /)
Jan Kratochvil 046f33b
3| end program prog
Jan Kratochvil 046f33b
Jan Kratochvil 046f33b
(gdb) print ary(1:10:2)
Jan Kratochvil 046f33b
$3 = (1, 3, 5, 7, 9)
Jan Kratochvil 046f33b
Jan Kratochvil 046f33b
2013-11-27  Christoph Weinmann  <christoph.t.weinmann>
Jan Kratochvil 046f33b
Jan Kratochvil 046f33b
	* eval.c (value_f90_subarray): Add range size calculation
Jan Kratochvil 046f33b
	for stride based ranges, and evaluation of user stride
Jan Kratochvil 046f33b
	parameters.  Add check for matching user input to array
Jan Kratochvil 046f33b
	bounds.
Jan Kratochvil 046f33b
	* valops.c (value_slice): Add call parameter with default
Jan Kratochvil 046f33b
	stride value for calling value_slice_1.
Jan Kratochvil 046f33b
	* valops.c (value_slice_1): Add function parameter for
Jan Kratochvil 046f33b
	stride length in the return subarray.  Calculate array
Jan Kratochvil 046f33b
	elements based on stride value.
Jan Kratochvil 046f33b
	* value.h: Add stride parameter to declaration of
Jan Kratochvil 046f33b
	value_slice_1.
Jan Kratochvil 046f33b
Jan Kratochvil 046f33b
Jan Kratochvil 046f33b
Signed-off-by: Christoph Weinmann <christoph.t.weinmann@intel.com>
Jan Kratochvil 046f33b
---
Jan Kratochvil fdbd5e3
 gdb/eval.c   | 111 ++++++++++++++++++++++++++++++++++++++++++++++-------------
Jan Kratochvil fdbd5e3
 gdb/valops.c |  87 +++++++++++++++++++++++++++++++++-------------
Jan Kratochvil fdbd5e3
 gdb/value.h  |   2 +-
Jan Kratochvil fdbd5e3
 3 files changed, 152 insertions(+), 48 deletions(-)
Jan Kratochvil 046f33b
Jan Kratochvil 046f33b
diff --git a/gdb/eval.c b/gdb/eval.c
Jan Kratochvil fdbd5e3
index 308ada3..d01b579 100644
Jan Kratochvil 046f33b
--- a/gdb/eval.c
Jan Kratochvil 046f33b
+++ b/gdb/eval.c
Jan Kratochvil 046f33b
@@ -437,8 +437,8 @@ value_f90_subarray (struct value *array, struct expression *exp,
Jan Kratochvil 046f33b
     {
Jan Kratochvil 046f33b
       struct subscript_range
Jan Kratochvil 046f33b
       {
Jan Kratochvil 046f33b
-        enum f90_range_type f90_range_type;
Jan Kratochvil 046f33b
-        LONGEST low, high, stride;
Jan Kratochvil 046f33b
+	enum f90_range_type f90_range_type;
Jan Kratochvil 046f33b
+	LONGEST low, high, stride;
Jan Kratochvil 046f33b
       }
Jan Kratochvil 046f33b
       range;
Jan Kratochvil 046f33b
       LONGEST number;
Jan Kratochvil 046f33b
@@ -475,7 +475,7 @@ value_f90_subarray (struct value *array, struct expression *exp,
Jan Kratochvil 046f33b
 	  range = &index->range;
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
 	  *pos += 3;
Jan Kratochvil 046f33b
-	  range->f90_range_type = longest_to_int (exp->elts[pc].longconst);
Jan Kratochvil 046f33b
+	  range->f90_range_type = exp->elts[pc].longconst;
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
 	  /* If a lower bound was provided by the user, the bit has been
Jan Kratochvil 046f33b
 	     set and we can assign the value from the elt stack.  Same for
Jan Kratochvil 046f33b
@@ -484,6 +484,7 @@ value_f90_subarray (struct value *array, struct expression *exp,
Jan Kratochvil 046f33b
 	      == SUBARRAY_LOW_BOUND)
Jan Kratochvil 046f33b
 	    range->low = value_as_long (evaluate_subexp (NULL_TYPE, exp,
Jan Kratochvil 046f33b
 							 pos, noside));
Jan Kratochvil 046f33b
+
Jan Kratochvil 046f33b
 	  if ((range->f90_range_type & SUBARRAY_HIGH_BOUND)
Jan Kratochvil 046f33b
 	      == SUBARRAY_HIGH_BOUND)
Jan Kratochvil 046f33b
 	    range->high = value_as_long (evaluate_subexp (NULL_TYPE, exp,
Jan Kratochvil 046f33b
@@ -496,6 +497,10 @@ value_f90_subarray (struct value *array, struct expression *exp,
Jan Kratochvil 046f33b
 	  /* Assign the default stride value '1'.  */
Jan Kratochvil 046f33b
 	  else
Jan Kratochvil 046f33b
 	    range->stride = 1;
Jan Kratochvil 046f33b
+
Jan Kratochvil 046f33b
+	  /* Check the provided stride value is illegal, aka '0'.  */
Jan Kratochvil 046f33b
+	  if (range->stride == 0)
Jan Kratochvil 046f33b
+	    error (_("Stride must not be 0"));
Jan Kratochvil 046f33b
 	}
Jan Kratochvil 046f33b
       /* User input is an index.  E.g.: "p arry(5)".  */
Jan Kratochvil 046f33b
       else
Jan Kratochvil 046f33b
@@ -512,10 +517,8 @@ value_f90_subarray (struct value *array, struct expression *exp,
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
     }
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
-  /* Traverse the array from right to left and evaluate each corresponding
Jan Kratochvil 046f33b
-     user input.  VALUE_SUBSCRIPT is called for every index, until a range
Jan Kratochvil 046f33b
-     expression is evaluated.  After a range expression has been evaluated,
Jan Kratochvil 046f33b
-     every subsequent expression is also treated as a range.  */
Jan Kratochvil 046f33b
+  /* Traverse the array from right to left and set the high and low bounds
Jan Kratochvil 046f33b
+     for later use.  */
Jan Kratochvil 046f33b
   for (i = nargs - 1; i >= 0; i--)
Jan Kratochvil 046f33b
     {
Jan Kratochvil 046f33b
       struct subscript_store *index = &subscript_array[i];
Jan Kratochvil 046f33b
@@ -548,6 +551,48 @@ value_f90_subarray (struct value *array, struct expression *exp,
Jan Kratochvil 046f33b
 		|| range->high > TYPE_HIGH_BOUND (index_type))
Jan Kratochvil 046f33b
 	      error (_("provided bound(s) outside array bound(s)"));
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
+	    /* For a negative stride the lower boundary must be larger than the
Jan Kratochvil 046f33b
+	       upper boundary.
Jan Kratochvil 046f33b
+	       For a positive stride the lower boundary must be smaller than the
Jan Kratochvil 046f33b
+	       upper boundary.  */
Jan Kratochvil 046f33b
+	    if ((range->stride < 0 && range->low < range->high)
Jan Kratochvil 046f33b
+		|| (range->stride > 0 && range->low > range->high))
Jan Kratochvil 046f33b
+	      error (_("Wrong value provided for stride and boundaries"));
Jan Kratochvil 046f33b
+
Jan Kratochvil 046f33b
+	  }
Jan Kratochvil 046f33b
+	  break;
Jan Kratochvil 046f33b
+
Jan Kratochvil 046f33b
+	case SUBSCRIPT_INDEX:
Jan Kratochvil 046f33b
+	  break;
Jan Kratochvil 046f33b
+
Jan Kratochvil 046f33b
+	}
Jan Kratochvil 046f33b
+
Jan Kratochvil 046f33b
+      array_type = TYPE_TARGET_TYPE (array_type);
Jan Kratochvil 046f33b
+    }
Jan Kratochvil 046f33b
+
Jan Kratochvil 046f33b
+  /* Reset ARRAY_TYPE before slicing.*/
Jan Kratochvil 046f33b
+  array_type = check_typedef (value_type (new_array));
Jan Kratochvil 046f33b
+
Jan Kratochvil 046f33b
+  /* Traverse the array from right to left and evaluate each corresponding
Jan Kratochvil 046f33b
+     user input.  VALUE_SUBSCRIPT is called for every index, until a range
Jan Kratochvil 046f33b
+     expression is evaluated.  After a range expression has been evaluated,
Jan Kratochvil 046f33b
+     every subsequent expression is also treated as a range.  */
Jan Kratochvil 046f33b
+  for (i = nargs - 1; i >= 0; i--)
Jan Kratochvil 046f33b
+    {
Jan Kratochvil 046f33b
+      struct subscript_store *index = &subscript_array[i];
Jan Kratochvil 046f33b
+      struct type *index_type = TYPE_INDEX_TYPE (array_type);
Jan Kratochvil 046f33b
+
Jan Kratochvil 046f33b
+      switch (index->kind)
Jan Kratochvil 046f33b
+	{
Jan Kratochvil 046f33b
+	case SUBSCRIPT_RANGE:
Jan Kratochvil 046f33b
+	  {
Jan Kratochvil 046f33b
+
Jan Kratochvil 046f33b
+	    /* When we hit the first range specified by the user, we must
Jan Kratochvil 046f33b
+	       treat any subsequent user entry as a range.  We simply
Jan Kratochvil 046f33b
+	       increment DIM_COUNT which tells us how many times we are
Jan Kratochvil 046f33b
+	       calling VALUE_SLICE_1.  */
Jan Kratochvil 046f33b
+	    struct subscript_range *range = &index->range;
Jan Kratochvil 046f33b
+
Jan Kratochvil 046f33b
 	    /* DIM_COUNT counts every user argument that is treated as a range.
Jan Kratochvil 046f33b
 	       This is necessary for expressions like 'print array(7, 8:9).
Jan Kratochvil 046f33b
 	       Here the first argument is a literal, but must be treated as a
Jan Kratochvil 046f33b
@@ -555,10 +600,9 @@ value_f90_subarray (struct value *array, struct expression *exp,
Jan Kratochvil 046f33b
 	    dim_count++;
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
 	    new_array
Jan Kratochvil 046f33b
-	      = value_slice_1 (new_array,
Jan Kratochvil 046f33b
-			       longest_to_int (range->low),
Jan Kratochvil 046f33b
-			       longest_to_int (range->high - range->low + 1),
Jan Kratochvil 046f33b
-			       dim_count);
Jan Kratochvil 046f33b
+	      = value_slice_1 (new_array, range->low,
Jan Kratochvil 046f33b
+			       range->high - range->low + 1,
Jan Kratochvil 046f33b
+			       range->stride, dim_count);
Jan Kratochvil 046f33b
 	  }
Jan Kratochvil 046f33b
 	  break;
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
@@ -572,27 +616,38 @@ value_f90_subarray (struct value *array, struct expression *exp,
Jan Kratochvil 046f33b
 	       to get the value offset right.  */
Jan Kratochvil 046f33b
 	    if (dim_count == 0)
Jan Kratochvil 046f33b
 	      new_array
Jan Kratochvil 046f33b
-	        = value_subscripted_rvalue (new_array, index->number,
Jan Kratochvil 046f33b
+		= value_subscripted_rvalue (new_array, index->number,
Jan Kratochvil 046f33b
 					    f77_get_lowerbound (value_type
Jan Kratochvil 046f33b
 								  (new_array)));
Jan Kratochvil 046f33b
 	    else
Jan Kratochvil 046f33b
 	      {
Jan Kratochvil 046f33b
-		/* Check for valid index input.  */
Jan Kratochvil 046f33b
+		dim_count++;
Jan Kratochvil 046f33b
+
Jan Kratochvil 046f33b
+		/* We might end up here, because we have to treat the provided
Jan Kratochvil 046f33b
+		   index like a range. But now VALUE_SUBSCRIPTED_RVALUE
Jan Kratochvil 046f33b
+		   cannot do the range checks for us. So we have to make sure
Jan Kratochvil 046f33b
+		   ourselves that the user provided index is inside the
Jan Kratochvil 046f33b
+		   array bounds.  Throw an error if not.  */
Jan Kratochvil 046f33b
 		if (index->number < TYPE_LOW_BOUND (index_type)
Jan Kratochvil 046f33b
-		    || index->number > TYPE_HIGH_BOUND (index_type))
Jan Kratochvil 046f33b
-		  error (_("error no such vector element"));
Jan Kratochvil 046f33b
+		    && index->number < TYPE_HIGH_BOUND (index_type))
Jan Kratochvil 046f33b
+		  error (_("provided bound(s) outside array bound(s)"));
Jan Kratochvil 046f33b
+
Jan Kratochvil 046f33b
+		if (index->number > TYPE_LOW_BOUND (index_type)
Jan Kratochvil 046f33b
+		    && index->number > TYPE_HIGH_BOUND (index_type))
Jan Kratochvil 046f33b
+		  error (_("provided bound(s) outside array bound(s)"));
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
-		dim_count++;
Jan Kratochvil 046f33b
 		new_array = value_slice_1 (new_array,
Jan Kratochvil 046f33b
-					   longest_to_int (index->number),
Jan Kratochvil 046f33b
-					   1, /* length is '1' element  */
Jan Kratochvil 046f33b
+					   index->number,
Jan Kratochvil 046f33b
+					   1, /* COUNT is '1' element  */
Jan Kratochvil 046f33b
+					   1, /* STRIDE set to '1'  */
Jan Kratochvil 046f33b
 					   dim_count);
Jan Kratochvil 046f33b
 	      }
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
 	  }
Jan Kratochvil 046f33b
 	  break;
Jan Kratochvil 046f33b
 	}
Jan Kratochvil 046f33b
-    }
Jan Kratochvil 046f33b
+      array_type = TYPE_TARGET_TYPE (array_type);
Jan Kratochvil 046f33b
+  }
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
   /* With DIM_COUNT > 1 we currently have a one dimensional array, but expect
Jan Kratochvil 046f33b
      an array of arrays, depending on how many ranges have been provided by
Jan Kratochvil 046f33b
@@ -617,7 +672,9 @@ value_f90_subarray (struct value *array, struct expression *exp,
Jan Kratochvil 046f33b
 	 the output array.  So we traverse the SUBSCRIPT_ARRAY again, looking
Jan Kratochvil 046f33b
 	 for a range entry.  When we find one, we use the range info to create
Jan Kratochvil 046f33b
 	 an additional range_type to set the correct bounds and dimensions for
Jan Kratochvil 046f33b
-	 the output array.  */
Jan Kratochvil 046f33b
+	 the output array.  In addition, we may have a stride value that is not
Jan Kratochvil 046f33b
+	 '1', forcing us to adjust the number of elements in a range, according
Jan Kratochvil 046f33b
+	 to the stride value.  */
Jan Kratochvil 046f33b
       for (i = 0; i < nargs; i++)
Jan Kratochvil 046f33b
 	{
Jan Kratochvil 046f33b
 	  struct subscript_store *index = &subscript_array[i];
Jan Kratochvil fdbd5e3
@@ -625,12 +682,20 @@ value_f90_subarray (struct value *array, struct expression *exp,
Jan Kratochvil 046f33b
 	  if (index->kind == SUBSCRIPT_RANGE)
Jan Kratochvil 046f33b
 	    {
Jan Kratochvil 046f33b
 	      struct type *range_type, *interim_array_type;
Jan Kratochvil 046f33b
+	      int new_length;
Jan Kratochvil 046f33b
+
Jan Kratochvil 046f33b
+	      /* The length of a sub-dimension with all elements between the
Jan Kratochvil 046f33b
+		 bounds plus the start element itself.  It may be modified by
Jan Kratochvil 046f33b
+		 a user provided stride value.  */
Jan Kratochvil 046f33b
+	      new_length = index->range.high - index->range.low;
Jan Kratochvil fdbd5e3
+
Jan Kratochvil 046f33b
+	      new_length /= index->range.stride;
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
 	      range_type
Jan Kratochvil 046f33b
 		= create_static_range_type (NULL,
Jan Kratochvil 046f33b
-				     temp_type,
Jan Kratochvil 046f33b
-				     1,
Jan Kratochvil 046f33b
-				     index->range.high - index->range.low + 1);
Jan Kratochvil 046f33b
+					    temp_type,
Jan Kratochvil 046f33b
+					    index->range.low,
Jan Kratochvil 046f33b
+					    index->range.low + new_length);
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
 	      interim_array_type = create_array_type (NULL,
Jan Kratochvil 046f33b
 						      temp_type,
Jan Kratochvil 046f33b
diff --git a/gdb/valops.c b/gdb/valops.c
Jan Kratochvil fdbd5e3
index 09ea877..83c8462 100644
Jan Kratochvil 046f33b
--- a/gdb/valops.c
Jan Kratochvil 046f33b
+++ b/gdb/valops.c
Jan Kratochvil 046f33b
@@ -3759,10 +3759,13 @@ value_of_this_silent (const struct language_defn *lang)
Jan Kratochvil 046f33b
 struct value *
Jan Kratochvil 046f33b
 value_slice (struct value *array, int lowbound, int length)
Jan Kratochvil 046f33b
 {
Jan Kratochvil 046f33b
-  /* Pass unaltered arguments to VALUE_SLICE_1, plus a CALL_COUNT of '1' as we
Jan Kratochvil 046f33b
-     are only considering the highest dimension, or we are working on a one
Jan Kratochvil 046f33b
-     dimensional array.  So we call VALUE_SLICE_1 exactly once.  */
Jan Kratochvil 046f33b
-  return value_slice_1 (array, lowbound, length, 1);
Jan Kratochvil 046f33b
+  /* Pass unaltered arguments to VALUE_SLICE_1, plus a default stride
Jan Kratochvil 046f33b
+     value of '1', which returns every element between LOWBOUND and
Jan Kratochvil 046f33b
+     (LOWBOUND + LENGTH).  We also provide a default CALL_COUNT of '1'
Jan Kratochvil 046f33b
+     as we are only considering the highest dimension, or we are
Jan Kratochvil 046f33b
+     working on a one dimensional array.  So we call VALUE_SLICE_1
Jan Kratochvil 046f33b
+     exactly once.  */
Jan Kratochvil 046f33b
+  return value_slice_1 (array, lowbound, length, 1, 1);
Jan Kratochvil 046f33b
 }
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
 /* CALL_COUNT is used to determine if we are calling the function once, e.g.
Jan Kratochvil 046f33b
@@ -3776,7 +3779,8 @@ value_slice (struct value *array, int lowbound, int length)
Jan Kratochvil 046f33b
    ranges in the calling function.  */
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
 struct value *
Jan Kratochvil 046f33b
-value_slice_1 (struct value *array, int lowbound, int length, int call_count)
Jan Kratochvil 046f33b
+value_slice_1 (struct value *array, int lowbound, int length,
Jan Kratochvil 046f33b
+	       int stride_length, int call_count)
Jan Kratochvil 046f33b
 {
Jan Kratochvil 046f33b
   struct type *slice_range_type, *slice_type, *range_type;
Jan Kratochvil 046f33b
   struct type *array_type = check_typedef (value_type (array));
Jan Kratochvil 046f33b
@@ -3799,14 +3803,24 @@ value_slice_1 (struct value *array, int lowbound, int length, int call_count)
Jan Kratochvil 046f33b
      attributes of the underlying type.  */
Jan Kratochvil 046f33b
   if (call_count > 1)
Jan Kratochvil 046f33b
     {
Jan Kratochvil 046f33b
+      ary_low_bound = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (elt_type));
Jan Kratochvil 046f33b
+      ary_high_bound = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (elt_type));
Jan Kratochvil 046f33b
       elt_type = check_typedef (TYPE_TARGET_TYPE (elt_type));
Jan Kratochvil 046f33b
       row_count = TYPE_LENGTH (array_type)
Jan Kratochvil 046f33b
 		    / TYPE_LENGTH (TYPE_TARGET_TYPE (array_type));
Jan Kratochvil 046f33b
     }
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
-  elem_count = length;
Jan Kratochvil 046f33b
+  /* With a stride of '1', the number of elements per result row is equal to
Jan Kratochvil 046f33b
+     the LENGTH of the subarray.  With non-default stride values, we skip
Jan Kratochvil 046f33b
+     elements, but have to add the start element to the total number of
Jan Kratochvil 046f33b
+     elements per row.  */
Jan Kratochvil 046f33b
+  if (stride_length == 1)
Jan Kratochvil 046f33b
+    elem_count = length;
Jan Kratochvil 046f33b
+  else
Jan Kratochvil 046f33b
+    elem_count = ((length - 1) / stride_length) + 1;
Jan Kratochvil 046f33b
+
Jan Kratochvil 046f33b
   elt_size = TYPE_LENGTH (elt_type);
Jan Kratochvil 046f33b
-  elt_offs = longest_to_int (lowbound - ary_low_bound);
Jan Kratochvil 046f33b
+  elt_offs = lowbound - ary_low_bound;
Jan Kratochvil 046f33b
   elt_stride = TYPE_LENGTH (TYPE_INDEX_TYPE (array_type));
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
   elt_offs *= elt_size;
Jan Kratochvil fdbd5e3
@@ -3825,7 +3839,7 @@ value_slice_1 (struct value *array, int lowbound, int length, int call_count)
Jan Kratochvil fdbd5e3
   if (call_count == 1)
Jan Kratochvil fdbd5e3
     {
Jan Kratochvil fdbd5e3
       range_type = TYPE_INDEX_TYPE (array_type);
Jan Kratochvil fdbd5e3
-      slice_range_size = elem_count;
Jan Kratochvil fdbd5e3
+      slice_range_size = ary_low_bound + elem_count - 1;
Jan Kratochvil fdbd5e3
 
Jan Kratochvil fdbd5e3
       /* Check if the array bounds are valid.  */
Jan Kratochvil fdbd5e3
       if (get_discrete_bounds (range_type, &ary_low_bound, &ary_high_bound) < 0)
Jan Kratochvil 046f33b
@@ -3837,7 +3851,7 @@ value_slice_1 (struct value *array, int lowbound, int length, int call_count)
Jan Kratochvil 046f33b
   else
Jan Kratochvil 046f33b
     {
Jan Kratochvil 046f33b
       range_type = TYPE_INDEX_TYPE (TYPE_TARGET_TYPE (array_type));
Jan Kratochvil 046f33b
-      slice_range_size = (ary_low_bound + row_count - 1) * (elem_count);
Jan Kratochvil 046f33b
+      slice_range_size = ary_low_bound + (row_count * elem_count) - 1;
Jan Kratochvil 046f33b
       ary_low_bound = TYPE_LOW_BOUND (range_type);
Jan Kratochvil 046f33b
     }
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
@@ -3849,8 +3863,9 @@ value_slice_1 (struct value *array, int lowbound, int length, int call_count)
Jan Kratochvil 046f33b
   {
Jan Kratochvil 046f33b
     struct type *element_type;
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
-    /* When CALL_COUNT equals 1 we can use the legacy code for subarrays.  */
Jan Kratochvil 046f33b
-    if (call_count == 1)
Jan Kratochvil 046f33b
+    /* When both CALL_COUNT and STRIDE_LENGTH equal 1, we can use the legacy
Jan Kratochvil 046f33b
+       code for subarrays.  */
Jan Kratochvil 046f33b
+    if (call_count == 1 && stride_length == 1)
Jan Kratochvil 046f33b
       {
Jan Kratochvil 046f33b
 	element_type = TYPE_TARGET_TYPE (array_type);
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
@@ -3871,29 +3886,53 @@ value_slice_1 (struct value *array, int lowbound, int length, int call_count)
Jan Kratochvil 046f33b
 	  }
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
       }
Jan Kratochvil 046f33b
-    /* When CALL_COUNT is larger than 1 we are working on a range of ranges.
Jan Kratochvil 046f33b
-       So we copy the relevant elements into the new array we return.  */
Jan Kratochvil 046f33b
+    /* With a CALL_COUNT or STRIDE_LENGTH are greater than 1 we are working
Jan Kratochvil 046f33b
+       on a range of ranges.  So we copy the relevant elements into the
Jan Kratochvil 046f33b
+       new array we return.  */
Jan Kratochvil 046f33b
     else
Jan Kratochvil 046f33b
       {
Jan Kratochvil 046f33b
+	int j, offs_store = elt_offs;
Jan Kratochvil 046f33b
 	LONGEST dst_offset = 0;
Jan Kratochvil 046f33b
 	LONGEST src_row_length = TYPE_LENGTH (TYPE_TARGET_TYPE (array_type));
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
-	element_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (array_type));
Jan Kratochvil 046f33b
+	if (call_count == 1)
Jan Kratochvil 046f33b
+	  {
Jan Kratochvil 046f33b
+	    /* When CALL_COUNT is equal to 1 we are working on the current range
Jan Kratochvil 046f33b
+	       and use these elements directly.  */
Jan Kratochvil 046f33b
+	    element_type = TYPE_TARGET_TYPE (array_type);
Jan Kratochvil 046f33b
+	  }
Jan Kratochvil 046f33b
+	else
Jan Kratochvil 046f33b
+	  {
Jan Kratochvil 046f33b
+	    /* Working on an array of arrays, the type of the elements is the type
Jan Kratochvil 046f33b
+	       of the subarrays' type.  */
Jan Kratochvil 046f33b
+	    element_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (array_type));
Jan Kratochvil 046f33b
+	  }
Jan Kratochvil 046f33b
+
Jan Kratochvil 046f33b
 	slice_type = create_array_type (NULL, element_type, slice_range_type);
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
-	TYPE_CODE (slice_type) = TYPE_CODE (TYPE_TARGET_TYPE (array_type));
Jan Kratochvil 046f33b
+	/* If we have a one dimensional array, we copy its TYPE_CODE.  For a
Jan Kratochvil 046f33b
+	   multi dimensional array we copy the embedded type's TYPE_CODE.  */
Jan Kratochvil 046f33b
+	if (call_count == 1)
Jan Kratochvil 046f33b
+	  TYPE_CODE (slice_type) = TYPE_CODE (array_type);
Jan Kratochvil 046f33b
+	else
Jan Kratochvil 046f33b
+	  TYPE_CODE (slice_type) = TYPE_CODE (TYPE_TARGET_TYPE (array_type));
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
 	v = allocate_value (slice_type);
Jan Kratochvil 046f33b
-	for (i = 0; i < longest_to_int (row_count); i++)
Jan Kratochvil 046f33b
+
Jan Kratochvil 046f33b
+	/* Iterate through the rows of the outer array and set the new offset
Jan Kratochvil 046f33b
+	   for each row.  */
Jan Kratochvil 046f33b
+	for (i = 0; i < row_count; i++)
Jan Kratochvil 046f33b
 	  {
Jan Kratochvil 046f33b
-	    /* Fetches the contents of ARRAY and copies them into V.  */
Jan Kratochvil 046f33b
-	    value_contents_copy (v,
Jan Kratochvil 046f33b
-				 dst_offset,
Jan Kratochvil 046f33b
-				 array,
Jan Kratochvil 046f33b
-				 elt_offs,
Jan Kratochvil 046f33b
-				 elt_size * elem_count);
Jan Kratochvil 046f33b
-	    elt_offs += src_row_length;
Jan Kratochvil 046f33b
-	    dst_offset += elt_size * elem_count;
Jan Kratochvil 046f33b
+	    elt_offs = offs_store + i * src_row_length;
Jan Kratochvil 046f33b
+
Jan Kratochvil 046f33b
+	    /* Iterate through the elements in each row to copy only those.  */
Jan Kratochvil 046f33b
+	    for (j = 1; j <= elem_count; j++)
Jan Kratochvil 046f33b
+	      {
Jan Kratochvil 046f33b
+		/* Fetches the contents of ARRAY and copies them into V.  */
Jan Kratochvil 046f33b
+		value_contents_copy (v, dst_offset, array, elt_offs, elt_size);
Jan Kratochvil 046f33b
+		elt_offs += elt_size * stride_length;
Jan Kratochvil 046f33b
+		dst_offset += elt_size;
Jan Kratochvil 046f33b
+	      }
Jan Kratochvil 046f33b
 	  }
Jan Kratochvil 046f33b
       }
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
diff --git a/gdb/value.h b/gdb/value.h
Jan Kratochvil fdbd5e3
index 3400460..c18ef2e 100644
Jan Kratochvil 046f33b
--- a/gdb/value.h
Jan Kratochvil 046f33b
+++ b/gdb/value.h
Jan Kratochvil 046f33b
@@ -1056,7 +1056,7 @@ extern struct value *varying_to_slice (struct value *);
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
 extern struct value *value_slice (struct value *, int, int);
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
-extern struct value *value_slice_1 (struct value *, int, int, int);
Jan Kratochvil 046f33b
+extern struct value *value_slice_1 (struct value *, int, int, int, int);
Jan Kratochvil 046f33b
 
Jan Kratochvil 046f33b
 extern struct value *value_literal_complex (struct value *, struct value *,
Jan Kratochvil 046f33b
 					    struct type *);
Jan Kratochvil 046f33b
-- 
Jan Kratochvil fdbd5e3
2.5.0
Jan Kratochvil fdbd5e3