* config/sh/linux-atomic.asm (ATOMIC_BOOL_COMPARE_AND_SWAP,
[official-gcc.git] / libgfortran / runtime / bounds.c
blob8a7affd2e180e428957711fc27708837cc515a9c
1 /* Copyright (C) 2009
2 Free Software Foundation, Inc.
3 Contributed by Thomas Koenig
5 This file is part of the GNU Fortran runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "libgfortran.h"
27 #include <assert.h>
29 /* Auxiliary functions for bounds checking, mostly to reduce library size. */
31 /* Bounds checking for the return values of the iforeach functions (such
32 as maxloc and minloc). The extent of ret_array must
33 must match the rank of array. */
35 void
36 bounds_iforeach_return (array_t *retarray, array_t *array, const char *name)
38 index_type rank;
39 index_type ret_rank;
40 index_type ret_extent;
42 ret_rank = GFC_DESCRIPTOR_RANK (retarray);
44 if (ret_rank != 1)
45 runtime_error ("Incorrect rank of return array in %s intrinsic:"
46 "is %ld, should be 1", name, (long int) ret_rank);
48 rank = GFC_DESCRIPTOR_RANK (array);
49 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,0);
50 if (ret_extent != rank)
51 runtime_error ("Incorrect extent in return value of"
52 " %s intrinsic: is %ld, should be %ld",
53 name, (long int) ret_extent, (long int) rank);
57 /* Check the return of functions generated from ifunction.m4.
58 We check the array descriptor "a" against the extents precomputed
59 from ifunction.m4, and complain about the argument a_name in the
60 intrinsic function. */
62 void
63 bounds_ifunction_return (array_t * a, const index_type * extent,
64 const char * a_name, const char * intrinsic)
66 int empty;
67 int n;
68 int rank;
69 index_type a_size;
71 rank = GFC_DESCRIPTOR_RANK (a);
72 a_size = size0 (a);
74 empty = 0;
75 for (n = 0; n < rank; n++)
77 if (extent[n] == 0)
78 empty = 1;
80 if (empty)
82 if (a_size != 0)
83 runtime_error ("Incorrect size in %s of %s"
84 " intrinsic: should be zero-sized",
85 a_name, intrinsic);
87 else
89 if (a_size == 0)
90 runtime_error ("Incorrect size of %s in %s"
91 " intrinsic: should not be zero-sized",
92 a_name, intrinsic);
94 for (n = 0; n < rank; n++)
96 index_type a_extent;
97 a_extent = GFC_DESCRIPTOR_EXTENT(a, n);
98 if (a_extent != extent[n])
99 runtime_error("Incorrect extent in %s of %s"
100 " intrinsic in dimension %ld: is %ld,"
101 " should be %ld", a_name, intrinsic, (long int) n + 1,
102 (long int) a_extent, (long int) extent[n]);
108 /* Check that two arrays have equal extents, or are both zero-sized. Abort
109 with a runtime error if this is not the case. Complain that a has the
110 wrong size. */
112 void
113 bounds_equal_extents (array_t *a, array_t *b, const char *a_name,
114 const char *intrinsic)
116 index_type a_size, b_size, n;
118 assert (GFC_DESCRIPTOR_RANK(a) == GFC_DESCRIPTOR_RANK(b));
120 a_size = size0 (a);
121 b_size = size0 (b);
123 if (b_size == 0)
125 if (a_size != 0)
126 runtime_error ("Incorrect size of %s in %s"
127 " intrinsic: should be zero-sized",
128 a_name, intrinsic);
130 else
132 if (a_size == 0)
133 runtime_error ("Incorrect size of %s of %s"
134 " intrinsic: Should not be zero-sized",
135 a_name, intrinsic);
137 for (n = 0; n < GFC_DESCRIPTOR_RANK (b); n++)
139 index_type a_extent, b_extent;
141 a_extent = GFC_DESCRIPTOR_EXTENT(a, n);
142 b_extent = GFC_DESCRIPTOR_EXTENT(b, n);
143 if (a_extent != b_extent)
144 runtime_error("Incorrect extent in %s of %s"
145 " intrinsic in dimension %ld: is %ld,"
146 " should be %ld", a_name, intrinsic, (long int) n + 1,
147 (long int) a_extent, (long int) b_extent);
152 /* Check that the extents of a and b agree, except that a has a missing
153 dimension in argument which. Complain about a if anything is wrong. */
155 void
156 bounds_reduced_extents (array_t *a, array_t *b, int which, const char *a_name,
157 const char *intrinsic)
160 index_type i, n, a_size, b_size;
162 assert (GFC_DESCRIPTOR_RANK(a) == GFC_DESCRIPTOR_RANK(b) - 1);
164 a_size = size0 (a);
165 b_size = size0 (b);
167 if (b_size == 0)
169 if (a_size != 0)
170 runtime_error ("Incorrect size in %s of %s"
171 " intrinsic: should not be zero-sized",
172 a_name, intrinsic);
174 else
176 if (a_size == 0)
177 runtime_error ("Incorrect size of %s of %s"
178 " intrinsic: should be zero-sized",
179 a_name, intrinsic);
181 i = 0;
182 for (n = 0; n < GFC_DESCRIPTOR_RANK (b); n++)
184 index_type a_extent, b_extent;
186 if (n != which)
188 a_extent = GFC_DESCRIPTOR_EXTENT(a, i);
189 b_extent = GFC_DESCRIPTOR_EXTENT(b, n);
190 if (a_extent != b_extent)
191 runtime_error("Incorrect extent in %s of %s"
192 " intrinsic in dimension %ld: is %ld,"
193 " should be %ld", a_name, intrinsic, (long int) i + 1,
194 (long int) a_extent, (long int) b_extent);
195 i++;