xref: /netbsd-src/external/gpl3/gcc/dist/libgfortran/runtime/bounds.c (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1*b1e83836Smrg /* Copyright (C) 2009-2022 Free Software Foundation, Inc.
2181254a7Smrg    Contributed by Thomas Koenig
3181254a7Smrg 
4181254a7Smrg This file is part of the GNU Fortran runtime library (libgfortran).
5181254a7Smrg 
6181254a7Smrg Libgfortran is free software; you can redistribute it and/or modify
7181254a7Smrg it under the terms of the GNU General Public License as published by
8181254a7Smrg the Free Software Foundation; either version 3, or (at your option)
9181254a7Smrg any later version.
10181254a7Smrg 
11181254a7Smrg Libgfortran is distributed in the hope that it will be useful,
12181254a7Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
13181254a7Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14181254a7Smrg GNU General Public License for more details.
15181254a7Smrg 
16181254a7Smrg Under Section 7 of GPL version 3, you are granted additional
17181254a7Smrg permissions described in the GCC Runtime Library Exception, version
18181254a7Smrg 3.1, as published by the Free Software Foundation.
19181254a7Smrg 
20181254a7Smrg You should have received a copy of the GNU General Public License and
21181254a7Smrg a copy of the GCC Runtime Library Exception along with this program;
22181254a7Smrg see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23181254a7Smrg <http://www.gnu.org/licenses/>.  */
24181254a7Smrg 
25181254a7Smrg #include "libgfortran.h"
26181254a7Smrg #include <assert.h>
27181254a7Smrg 
28181254a7Smrg /* Auxiliary functions for bounds checking, mostly to reduce library size.  */
29181254a7Smrg 
30181254a7Smrg /* Bounds checking for the return values of the iforeach functions (such
31181254a7Smrg    as maxloc and minloc).  The extent of ret_array must
32181254a7Smrg    must match the rank of array.  */
33181254a7Smrg 
34181254a7Smrg void
bounds_iforeach_return(array_t * retarray,array_t * array,const char * name)35181254a7Smrg bounds_iforeach_return (array_t *retarray, array_t *array, const char *name)
36181254a7Smrg {
37181254a7Smrg   index_type rank;
38181254a7Smrg   index_type ret_rank;
39181254a7Smrg   index_type ret_extent;
40181254a7Smrg 
41181254a7Smrg   ret_rank = GFC_DESCRIPTOR_RANK (retarray);
42181254a7Smrg 
43181254a7Smrg   /* ret_rank should always be 1, otherwise there is an internal error */
44181254a7Smrg   GFC_ASSERT(ret_rank == 1);
45181254a7Smrg 
46181254a7Smrg   rank = GFC_DESCRIPTOR_RANK (array);
47181254a7Smrg   ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,0);
48181254a7Smrg   if (ret_extent != rank)
49181254a7Smrg     runtime_error ("Incorrect extent in return value of"
50181254a7Smrg 		   " %s intrinsic: is %ld, should be %ld",
51181254a7Smrg 		   name, (long int) ret_extent, (long int) rank);
52181254a7Smrg 
53181254a7Smrg }
54181254a7Smrg 
55181254a7Smrg /* Check the return of functions generated from ifunction.m4.
56181254a7Smrg    We check the array descriptor "a" against the extents precomputed
57181254a7Smrg    from ifunction.m4, and complain about the argument a_name in the
58181254a7Smrg    intrinsic function. */
59181254a7Smrg 
60181254a7Smrg void
bounds_ifunction_return(array_t * a,const index_type * extent,const char * a_name,const char * intrinsic)61181254a7Smrg bounds_ifunction_return (array_t * a, const index_type * extent,
62181254a7Smrg 			 const char * a_name, const char * intrinsic)
63181254a7Smrg {
64181254a7Smrg   int empty;
65181254a7Smrg   int rank;
66181254a7Smrg   index_type a_size;
67181254a7Smrg 
68181254a7Smrg   rank = GFC_DESCRIPTOR_RANK (a);
69181254a7Smrg   a_size = size0 (a);
70181254a7Smrg 
71181254a7Smrg   empty = 0;
72181254a7Smrg   for (index_type n = 0; n < rank; n++)
73181254a7Smrg     {
74181254a7Smrg       if (extent[n] == 0)
75181254a7Smrg 	empty = 1;
76181254a7Smrg     }
77181254a7Smrg   if (empty)
78181254a7Smrg     {
79181254a7Smrg       if (a_size != 0)
80181254a7Smrg 	runtime_error ("Incorrect size in %s of %s"
81181254a7Smrg 		       " intrinsic: should be zero-sized",
82181254a7Smrg 		       a_name, intrinsic);
83181254a7Smrg     }
84181254a7Smrg   else
85181254a7Smrg     {
86181254a7Smrg       if (a_size == 0)
87181254a7Smrg 	runtime_error ("Incorrect size of %s in %s"
88181254a7Smrg 		       " intrinsic: should not be zero-sized",
89181254a7Smrg 		       a_name, intrinsic);
90181254a7Smrg 
91181254a7Smrg       for (index_type n = 0; n < rank; n++)
92181254a7Smrg 	{
93181254a7Smrg 	  index_type a_extent;
94181254a7Smrg 	  a_extent = GFC_DESCRIPTOR_EXTENT(a, n);
95181254a7Smrg 	  if (a_extent != extent[n])
96181254a7Smrg 	    runtime_error("Incorrect extent in %s of %s"
97181254a7Smrg 			  " intrinsic in dimension %ld: is %ld,"
98181254a7Smrg 			  " should be %ld", a_name, intrinsic, (long int) n + 1,
99181254a7Smrg 			  (long int) a_extent, (long int) extent[n]);
100181254a7Smrg 
101181254a7Smrg 	}
102181254a7Smrg     }
103181254a7Smrg }
104181254a7Smrg 
105181254a7Smrg /* Check that two arrays have equal extents, or are both zero-sized.  Abort
106181254a7Smrg    with a runtime error if this is not the case.  Complain that a has the
107181254a7Smrg    wrong size.  */
108181254a7Smrg 
109181254a7Smrg void
bounds_equal_extents(array_t * a,array_t * b,const char * a_name,const char * intrinsic)110181254a7Smrg bounds_equal_extents (array_t *a, array_t *b, const char *a_name,
111181254a7Smrg 		      const char *intrinsic)
112181254a7Smrg {
113181254a7Smrg   index_type a_size, b_size, n;
114181254a7Smrg 
115181254a7Smrg   assert (GFC_DESCRIPTOR_RANK(a) == GFC_DESCRIPTOR_RANK(b));
116181254a7Smrg 
117181254a7Smrg   a_size = size0 (a);
118181254a7Smrg   b_size = size0 (b);
119181254a7Smrg 
120181254a7Smrg   if (b_size == 0)
121181254a7Smrg     {
122181254a7Smrg       if (a_size != 0)
123181254a7Smrg 	runtime_error ("Incorrect size of %s in %s"
124181254a7Smrg 		       " intrinsic: should be zero-sized",
125181254a7Smrg 		       a_name, intrinsic);
126181254a7Smrg     }
127181254a7Smrg   else
128181254a7Smrg     {
129181254a7Smrg       if (a_size == 0)
130181254a7Smrg 	runtime_error ("Incorrect size of %s of %s"
131181254a7Smrg 		       " intrinsic: Should not be zero-sized",
132181254a7Smrg 		       a_name, intrinsic);
133181254a7Smrg 
134181254a7Smrg       for (n = 0; n < GFC_DESCRIPTOR_RANK (b); n++)
135181254a7Smrg 	{
136181254a7Smrg 	  index_type a_extent, b_extent;
137181254a7Smrg 
138181254a7Smrg 	  a_extent = GFC_DESCRIPTOR_EXTENT(a, n);
139181254a7Smrg 	  b_extent = GFC_DESCRIPTOR_EXTENT(b, n);
140181254a7Smrg 	  if (a_extent != b_extent)
141181254a7Smrg 	    runtime_error("Incorrect extent in %s of %s"
142181254a7Smrg 			  " intrinsic in dimension %ld: is %ld,"
143181254a7Smrg 			  " should be %ld", a_name, intrinsic, (long int) n + 1,
144181254a7Smrg 			  (long int) a_extent, (long int) b_extent);
145181254a7Smrg 	}
146181254a7Smrg     }
147181254a7Smrg }
148181254a7Smrg 
149181254a7Smrg /* Check that the extents of a and b agree, except that a has a missing
150181254a7Smrg    dimension in argument which.  Complain about a if anything is wrong.  */
151181254a7Smrg 
152181254a7Smrg void
bounds_reduced_extents(array_t * a,array_t * b,int which,const char * a_name,const char * intrinsic)153181254a7Smrg bounds_reduced_extents (array_t *a, array_t *b, int which, const char *a_name,
154181254a7Smrg 		      const char *intrinsic)
155181254a7Smrg {
156181254a7Smrg 
157181254a7Smrg   index_type i, n, a_size, b_size;
158181254a7Smrg 
159181254a7Smrg   assert (GFC_DESCRIPTOR_RANK(a) == GFC_DESCRIPTOR_RANK(b) - 1);
160181254a7Smrg 
161181254a7Smrg   a_size = size0 (a);
162181254a7Smrg   b_size = size0 (b);
163181254a7Smrg 
164181254a7Smrg   if (b_size == 0)
165181254a7Smrg     {
166181254a7Smrg       if (a_size != 0)
167181254a7Smrg 	runtime_error ("Incorrect size in %s of %s"
168181254a7Smrg 		       " intrinsic: should not be zero-sized",
169181254a7Smrg 		       a_name, intrinsic);
170181254a7Smrg     }
171181254a7Smrg   else
172181254a7Smrg     {
173181254a7Smrg       if (a_size == 0)
174181254a7Smrg 	runtime_error ("Incorrect size of %s of %s"
175181254a7Smrg 		       " intrinsic: should be zero-sized",
176181254a7Smrg 		       a_name, intrinsic);
177181254a7Smrg 
178181254a7Smrg       i = 0;
179181254a7Smrg       for (n = 0; n < GFC_DESCRIPTOR_RANK (b); n++)
180181254a7Smrg 	{
181181254a7Smrg 	  index_type a_extent, b_extent;
182181254a7Smrg 
183181254a7Smrg 	  if (n != which)
184181254a7Smrg 	    {
185181254a7Smrg 	      a_extent = GFC_DESCRIPTOR_EXTENT(a, i);
186181254a7Smrg 	      b_extent = GFC_DESCRIPTOR_EXTENT(b, n);
187181254a7Smrg 	      if (a_extent != b_extent)
188181254a7Smrg 		runtime_error("Incorrect extent in %s of %s"
189181254a7Smrg 			      " intrinsic in dimension %ld: is %ld,"
190181254a7Smrg 			      " should be %ld", a_name, intrinsic, (long int) i + 1,
191181254a7Smrg 			      (long int) a_extent, (long int) b_extent);
192181254a7Smrg 	      i++;
193181254a7Smrg 	    }
194181254a7Smrg 	}
195181254a7Smrg     }
196181254a7Smrg }
197181254a7Smrg 
198181254a7Smrg /* count_0 - count all the true elements in an array.  The front
199181254a7Smrg    end usually inlines this, we need this for bounds checking
200181254a7Smrg    for unpack.  */
201181254a7Smrg 
count_0(const gfc_array_l1 * array)202181254a7Smrg index_type count_0 (const gfc_array_l1 * array)
203181254a7Smrg {
204181254a7Smrg   const GFC_LOGICAL_1 * restrict base;
205181254a7Smrg   index_type rank;
206181254a7Smrg   int kind;
207181254a7Smrg   int continue_loop;
208181254a7Smrg   index_type count[GFC_MAX_DIMENSIONS];
209181254a7Smrg   index_type extent[GFC_MAX_DIMENSIONS];
210181254a7Smrg   index_type sstride[GFC_MAX_DIMENSIONS];
211181254a7Smrg   index_type result;
212181254a7Smrg   index_type n;
213181254a7Smrg 
214181254a7Smrg   rank = GFC_DESCRIPTOR_RANK (array);
215181254a7Smrg   kind = GFC_DESCRIPTOR_SIZE (array);
216181254a7Smrg 
217181254a7Smrg   base = array->base_addr;
218181254a7Smrg 
219181254a7Smrg   if (kind == 1 || kind == 2 || kind == 4 || kind == 8
220181254a7Smrg #ifdef HAVE_GFC_LOGICAL_16
221181254a7Smrg       || kind == 16
222181254a7Smrg #endif
223181254a7Smrg     )
224181254a7Smrg     {
225181254a7Smrg       if (base)
226181254a7Smrg 	base = GFOR_POINTER_TO_L1 (base, kind);
227181254a7Smrg     }
228181254a7Smrg   else
229181254a7Smrg     internal_error (NULL, "Funny sized logical array in count_0");
230181254a7Smrg 
231181254a7Smrg   for (n = 0; n < rank; n++)
232181254a7Smrg     {
233181254a7Smrg       sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,n);
234181254a7Smrg       extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
235181254a7Smrg       count[n] = 0;
236181254a7Smrg 
237181254a7Smrg       if (extent[n] <= 0)
238181254a7Smrg 	return 0;
239181254a7Smrg     }
240181254a7Smrg 
241181254a7Smrg   result = 0;
242181254a7Smrg   continue_loop = 1;
243181254a7Smrg   while (continue_loop)
244181254a7Smrg     {
245181254a7Smrg       if (*base)
246181254a7Smrg 	result ++;
247181254a7Smrg 
248181254a7Smrg       count[0]++;
249181254a7Smrg       base += sstride[0];
250181254a7Smrg       n = 0;
251181254a7Smrg       while (count[n] == extent[n])
252181254a7Smrg 	{
253181254a7Smrg 	  count[n] = 0;
254181254a7Smrg 	  base -= sstride[n] * extent[n];
255181254a7Smrg 	  n++;
256181254a7Smrg 	  if (n == rank)
257181254a7Smrg 	    {
258181254a7Smrg 	      continue_loop = 0;
259181254a7Smrg 	      break;
260181254a7Smrg 	    }
261181254a7Smrg 	  else
262181254a7Smrg 	    {
263181254a7Smrg 	      count[n]++;
264181254a7Smrg 	      base += sstride[n];
265181254a7Smrg 	    }
266181254a7Smrg 	}
267181254a7Smrg     }
268181254a7Smrg   return result;
269181254a7Smrg }
270