Checks now the Load_mode
[libfirm] / ir / adt / array.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/adt/array.h
4  * Purpose:     Declarations for Array.
5  * Author:      Markus Armbruster
6  * Modified by:
7  * Created:     1999 by getting from fiasco
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1995, 1996 Markus Armbruster
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14  * @file array.h   Dynamic and flexible arrays for C.
15  */
16
17 #ifndef _ARRAY_H
18 #define _ARRAY_H
19
20 #include <assert.h>
21 #include <stddef.h>
22 #include <obstack.h>
23
24 #include "cookies.h"
25 #include "xmalloc.h"
26
27
28 /**
29  * Creates a flexible array.
30  *
31  * @param type     The element type of the new array.
32  * @param nelts    a size_t expression evaluating to the number of elements
33  *
34  * This macro creates a flexible array of a given type at runtime.
35  * The size of the array can be changed later.
36  *
37  * @return A pointer to the flexible array (can be used as a pointer to the
38  *         first element of this array).
39  */
40 #define NEW_ARR_F(type, nelts)                                          \
41   (XMALLOC_TRACE (type *)_new_arr_f ((nelts), sizeof(type) * (nelts)))
42
43 /**
44  * Creates a new flxible array with the same number of elements as a
45  * given one.
46  *
47  * @param type     The element type of the new array.
48  * @param arr      An array from which the number of elements will be taken
49  *
50  * This macro creates a flexible array of a given type at runtime.
51  * The size of the array can be changed later.
52  *
53  * @return A pointer to the flexible array (can be used as a pointer to the
54  *         first element of this array).
55  */
56 #define CLONE_ARR_F(type, arr)                  \
57   NEW_ARR_F (type, ARR_LEN ((arr)))
58
59 /**
60  * Duplicates an array and returns the new flexible one.
61  *
62  * @param type     The element type of the new array.
63  * @param arr      An array from which the elements will be duplicated
64  *
65  * This macro creates a flexible array of a given type at runtime.
66  * The size of the array can be changed later.
67  *
68  * @return A pointer to the flexible array (can be used as a pointer to the
69  *         first element of this array).
70  */
71 #define DUP_ARR_F(type, arr)                                                    \
72   memcpy (CLONE_ARR_F (type, (arr)), (arr), sizeof(type) * ARR_LEN((arr)))
73
74 /**
75  * Delete a flexible array.
76  *
77  * @param arr    The flexible array.
78  */
79 #define DEL_ARR_F(arr) (XMALLOC_TRACE _del_arr_f ((arr)))
80
81 /**
82  * Creates a dynamic array on an obstack.
83  *
84  * @param type     The element type of the new array.
85  * @param obstack  A struct obstack * were the data will be allocated
86  * @param nelts    A size_t expression evaluating to the number of elements
87  *
88  * This macro creates a dynamic array of a given type at runtime.
89  * The size of the array cannot be changed later.
90  *
91  * @return A pointer to the dynamic array (can be used as a pointer to the
92  *         first element of this array).
93  */
94 #define NEW_ARR_D(type, obstack, nelts)                                 \
95   (  nelts                                                              \
96    ? (type *)_new_arr_d ((obstack), (nelts), sizeof(type) * (nelts))    \
97    : (type *)arr_mt_descr.v.elts)
98
99 /**
100  * Creates a new dynamic array with the same number of elements as a
101  * given one.
102  *
103  * @param type     The element type of the new array.
104  * @param obstack  An struct obstack * were the data will be allocated
105  * @param arr      An array from which the number of elements will be taken
106  *
107  * This macro creates a dynamic array of a given type at runtime.
108  * The size of the array cannot be changed later.
109  *
110  * @return A pointer to the dynamic array (can be used as a pointer to the
111  *         first element of this array).
112  */
113 #define CLONE_ARR_D(type, obstack, arr)         \
114   NEW_ARR_D (type, (obstack), ARR_LEN ((arr)))
115
116 /**
117  * Duplicates an array and returns the new dynamic one.
118  *
119  * @param type     The element type of the new array.
120  * @param obstack  An struct obstack * were the data will be allocated
121  * @param arr      An array from which the elements will be duplicated
122  *
123  * This macro creates a dynamic array of a given type at runtime.
124  * The size of the array cannot be changed later.
125  *
126  * @return A pointer to the dynamic array (can be used as a pointer to the
127  *         first element of this array).
128  */
129 #define DUP_ARR_D(type, obstack, arr)                                                   \
130   memcpy (CLONE_ARR_D (type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr)))
131
132 /**
133  * Create an automatic array which will be deleted at return from function.
134  * Beware, the data will be allocated un the functions stack!
135  *
136  * @param type     The element type of the new array.
137  * @param var      A lvalue of type (type *) which will hold the new array.
138  * @param n        number of elements in this array.
139  *
140  * This macro creates a dynamic array on the functions stack of a given type at runtime.
141  * The size of the array cannot be changed later.
142  */
143 #define NEW_ARR_A(type, var, n)                                                                 \
144   do {                                                                                          \
145     int _nelts = (n);                                                                           \
146     assert (_nelts >= 0);                                                                       \
147     (var) = (void *)((_arr_descr *)alloca (_ARR_ELTS_OFFS + sizeof(type) * _nelts))->v.elts;    \
148     _ARR_SET_DBGINF (_ARR_DESCR ((var)), ARR_A_MAGIC, sizeof (type));                           \
149     (void)(_ARR_DESCR ((var))->nelts = _nelts);                                                 \
150   } while (0)
151
152 /**
153  * Creates a new automatic array with the same number of elements as a
154  * given one.
155  *
156  * @param type     The element type of the new array.
157  * @param var      A lvalue of type (type *) which will hold the new array.
158  * @param arr      An array from which the elements will be duplicated
159  *
160  * This macro creates a dynamic array of a given type at runtime.
161  * The size of the array cannot be changed later.
162  *
163  * @return A pointer to the dynamic array (can be used as a pointer to the
164  *         first element of this array).
165  */
166 #define CLONE_ARR_A(type, var, arr)             \
167   NEW_ARR_A (type, (var), ARR_LEN ((arr)))
168
169 /**
170  * Duplicates an array and returns a new automatic one.
171  *
172  * @param type     The element type of the new array.
173  * @param var      A lvalue of type (type *) which will hold the new array.
174  * @param arr      An array from with the number of elements will be taken
175  *
176  * This macro creates a dynamic array of a given type at runtime.
177  * The size of the array cannot be changed later.
178  *
179  * @return A pointer to the dynamic array (can be used as a pointer to the
180  *         first element of this array).
181  */
182 #define DUP_ARR_A(type, var, arr)                                       \
183   do { CLONE_ARR_A(type, (var), (arr));                                 \
184        memcpy ((var), (arr), sizeof (type) * ARR_LEN ((arr))); }        \
185   while (0)
186
187 /**
188  * Declare an initialized (zero'ed) array of fixed size.
189  * This macro should be used at file scope only.
190  *
191  * @param type     The element type of the new array.
192  * @param var      A lvalue of type (type *) which will hold the new array.
193  * @param _nelts   Number of elements in this new array.
194  */
195 #define DECL_ARR_S(type, var, _nelts)                                   \
196   ARR_STRUCT(type, (_nelts) ? (_nelts) : 1) _##var;                     \
197   type *var = (_ARR_SET_DBGINF (&_##var, ARR_A_MAGIC, sizeof (type)),   \
198                _##var.nelts = _nelts,                                   \
199                _##var.v.elts)
200
201 /**
202  * Returns the length of an array
203  *
204  * @param arr  a flexible, dynamic, automatic or static array.
205  */
206 #define ARR_LEN(arr) (ARR_VRFY ((arr)), _ARR_DESCR((arr))->nelts)
207
208 /**
209  * Resize a flexible array, allocate more data if needed but do NOT
210  * reduce.
211  *
212  * @param type     The element type of the array.
213  * @param arr      The array, which must be an lvalue.
214  * @param n        The new size of the array.
215  *
216  * @remark  This macro may change arr, so update all references!
217  */
218 #define ARR_RESIZE(type, arr, n)                                        \
219   (XMALLOC_TRACE (arr) = _arr_resize ((arr), (n), sizeof(type)))
220
221 /**
222  * Resize a flexible array, always reallocate data.
223  *
224  * @param type     The element type of the array.
225  * @param arr      The array, which must be an lvalue.
226  * @param n        The new size of the array.
227  *
228  * @remark  This macro may change arr, so update all references!
229  */
230 #define ARR_SETLEN(type, arr, n)                                        \
231   (XMALLOC_TRACE (arr) = _arr_setlen ((arr), (n), sizeof(type) * (n)))
232
233 /**
234  * Resize a flexible array by growing it by delta elements.
235  *
236  * @param type     The element type of the array.
237  * @param arr      The array, which must be an lvalue.
238  * @param delta    The delta number of elements.
239  *
240  * @remark  This macro may change arr, so update all references!
241  */
242 #define ARR_EXTEND(type, arr, delta)                    \
243   ARR_RESIZE (type, (arr), ARR_LEN ((arr)) + (delta))
244
245 /**
246  * Resize a flexible array to hold n elements only if it is currently shorter
247  * than n.
248  *
249  * @param type     The element type of the array.
250  * @param arr      The array, which must be an lvalue.
251  * @param n        The new size of the array.
252  *
253  * @remark  This macro may change arr, so update all references!
254  */
255 #define ARR_EXTO(type, arr, n)                                          \
256   ((n) >= ARR_LEN ((arr)) ? ARR_RESIZE (type, (arr), (n)+1) : (arr))
257
258 /**
259  * Append one element to a flexible array.
260  *
261  * @param type     The element type of the array.
262  * @param arr      The array, which must be an lvalue.
263  * @param elt      The new element, must be of type (type).
264  */
265 #define ARR_APP1(type, arr, elt)                                        \
266   (ARR_EXTEND (type, (arr), 1), (arr)[ARR_LEN ((arr))-1] = (elt))
267
268
269 #ifdef NDEBUG
270 # define ARR_VRFY(arr) ((void)0)
271 # define ARR_IDX_VRFY(arr, idx) ((void)0)
272 #else
273 # define ARR_VRFY(arr)                                                                  \
274     assert (   (   (_ARR_DESCR((arr))->cookie == ARR_D_MAGIC)                           \
275                 || (_ARR_DESCR((arr))->cookie == ARR_A_MAGIC)                           \
276                 || (_ARR_DESCR((arr))->cookie == ARR_F_MAGIC))                          \
277             && (   (_ARR_DESCR((arr))->cookie != ARR_F_MAGIC)                           \
278                 || (_ARR_DESCR((arr))->u.allocated >= _ARR_DESCR((arr))->nelts))        \
279             && (_ARR_DESCR((arr))->nelts >= 0))
280 # define ARR_IDX_VRFY(arr, idx)                         \
281     assert ((0 <= (idx)) && ((idx) < ARR_LEN ((arr))))
282 #endif
283
284
285 /* Private !!!
286    Don't try this at home, kids, we're trained professionals ;->
287    ... or at the IPD, either. */
288 #ifdef NDEBUG
289 # define _ARR_DBGINF_DECL
290 # define _ARR_SET_DBGINF(descr, co, es) ((co), (es))
291 #else
292 # define _ARR_DBGINF_DECL int cookie; size_t eltsize;
293 # define _ARR_SET_DBGINF(descr, co, es)                                 \
294     ((descr)->cookie = (co), (descr)->eltsize = (es))
295 #endif
296
297 /**
298  * Construct an array header.
299  */
300 #define ARR_STRUCT(type, _nelts)                                                \
301   struct {                                                                      \
302     _ARR_DBGINF_DECL                                                            \
303     union {                                                                     \
304       struct obstack *obstack;  /* dynamic: allocated on this obstack */        \
305       int allocated;                    /* flexible: #slots allocated */        \
306     } u;                                                                        \
307     int nelts;                                                                  \
308     union {                                                                     \
309       type elts[(_nelts)];                                                      \
310       aligned_type align[1];                                                    \
311     } v;                                                                        \
312   }
313
314 /**
315  * The array descriptor header type.
316  */
317 typedef ARR_STRUCT (aligned_type, 1) _arr_descr;
318
319 extern _arr_descr arr_mt_descr;
320
321 void *_new_arr_f (int, size_t);
322 void _del_arr_f (void *);
323 void *_new_arr_d (struct obstack *obstack, int nelts, size_t elts_size);
324 void *_arr_resize (void *, int, size_t);
325 void *_arr_setlen (void *, int, size_t);
326
327 #define _ARR_ELTS_OFFS offsetof (_arr_descr, v.elts)
328 #define _ARR_DESCR(elts) ((_arr_descr *)(void *)((char *)(elts) - _ARR_ELTS_OFFS))
329
330 #endif