2 /**-------------------------------------------------------------------**
4 **-------------------------------------------------------------------**
6 **-------------------------------------------------------------------**
7 ** First version: august 1st 2002 **
8 **-------------------------------------------------------------------**/
11 /******************************************************************************
12 * CLooG : the Chunky Loop Generator (experimental) *
13 ******************************************************************************
15 * Copyright (C) 2002-2005 Cedric Bastoul *
17 * This is free software; you can redistribute it and/or modify it under the *
18 * terms of the GNU General Public License as published by the Free Software *
19 * Foundation; either version 2 of the License, or (at your option) any later *
22 * This software is distributed in the hope that it will be useful, but *
23 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
24 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
27 * You should have received a copy of the GNU General Public License along *
28 * with software; if not, write to the Free Software Foundation, Inc., *
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
31 * CLooG, the Chunky Loop Generator *
32 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr *
34 ******************************************************************************/
35 /* CAUTION: the english used for comments is probably the worst you ever read,
36 * please feel free to correct and improve it !
43 # include "../include/cloog/cloog.h"
46 static inline int cloog_names_references (CloogNames
*n
)
48 return n
->_references
;
51 static inline void cloog_names_init_references (CloogNames
*n
)
56 static inline void cloog_names_inc_references (CloogNames
*n
)
61 static inline void cloog_names_dec_references (CloogNames
*n
)
67 /******************************************************************************
68 * Structure display function *
69 ******************************************************************************/
73 * cloog_names_print function:
74 * this function is a human-friendly way to display the CloogNames data
75 * structure, it shows all the different fields and includes an indentation
76 * level (level) in order to work with others print_structure functions.
77 * - July 1st 2005: first version based on the old cloog_names_print function,
78 * it was the first modification in this file since two years !
80 void cloog_names_print_structure(FILE * file
, CloogNames
* names
, int level
)
83 /* Go to the right level. */
84 for (i
=0; i
<level
; i
++)
88 { fprintf(file
,"+-- CloogNames\n") ;
91 for (i
=0; i
<=level
+1; i
++)
95 /* Print the scalar dimension number. */
96 for (i
=0; i
<=level
; i
++)
98 fprintf (file
, "Scalar dimension number ---: %d\n", cloog_names_nb_scalars (names
));
101 for (i
=0; i
<=level
+1; i
++)
102 fprintf(file
,"|\t") ;
105 /* Print the scalar iterators. */
106 for (i
=0; i
<=level
; i
++)
107 fprintf(file
,"|\t") ;
108 if (cloog_names_nb_scalars (names
) > 0)
109 { fprintf(file
,"+-- Scalar iterator strings:") ;
110 for (i
=0;i
<cloog_names_nb_scalars (names
);i
++)
111 fprintf(file
," %s",cloog_names_scalar_elt (names
, i
)) ;
115 fprintf(file
,"+-- No scalar string\n") ;
118 for (i
=0; i
<=level
+1; i
++)
119 fprintf(file
,"|\t") ;
122 /* Print the scattering dimension number. */
123 for (i
=0; i
<=level
; i
++)
124 fprintf(file
,"|\t") ;
125 fprintf(file
,"Scattering dimension number: %d\n",cloog_names_nb_scattering (names
)) ;
128 for (i
=0; i
<=level
+1; i
++)
129 fprintf(file
,"|\t") ;
132 /* Print the scattering iterators. */
133 for (i
=0; i
<=level
; i
++)
134 fprintf(file
,"|\t") ;
135 if (cloog_names_nb_scattering (names
) > 0)
136 { fprintf(file
,"+-- Scattering strings ----:") ;
137 for (i
=0;i
<cloog_names_nb_scattering (names
);i
++)
138 fprintf (file
, " %s", cloog_names_scattering_elt (names
, i
));
142 fprintf(file
,"+-- No scattering string\n") ;
145 for (i
=0; i
<=level
+1; i
++)
146 fprintf(file
,"|\t") ;
149 /* Print the iterator number. */
150 for (i
=0; i
<=level
; i
++)
151 fprintf(file
,"|\t") ;
152 fprintf(file
,"Iterator number -----------: %d\n",cloog_names_nb_iterators (names
)) ;
155 for (i
=0; i
<=level
+1; i
++)
156 fprintf(file
,"|\t") ;
159 /* Print the iterators. */
160 for (i
=0; i
<=level
; i
++)
161 fprintf(file
,"|\t") ;
162 if (cloog_names_nb_iterators (names
) > 0)
163 { fprintf(file
,"+-- Iterator strings ------:") ;
164 for (i
=0;i
<cloog_names_nb_iterators (names
);i
++)
165 fprintf(file
," %s",cloog_names_iterator_elt (names
, i
)) ;
169 fprintf(file
,"+-- No iterators\n") ;
172 for (i
=0; i
<=level
+1; i
++)
173 fprintf(file
,"|\t") ;
176 /* Print the parameter number. */
177 for (i
=0; i
<=level
; i
++)
178 fprintf(file
,"|\t") ;
179 fprintf(file
,"Parameter number ----------: %d\n",cloog_names_nb_parameters (names
)) ;
182 for (i
=0; i
<=level
+1; i
++)
183 fprintf(file
,"|\t") ;
186 /* Print the parameters. */
187 for (i
=0; i
<=level
; i
++)
188 fprintf(file
,"|\t") ;
189 if (cloog_names_nb_parameters (names
) > 0)
190 { fprintf(file
,"+-- Parameter strings -----:") ;
191 for (i
=0;i
<cloog_names_nb_parameters (names
);i
++)
192 fprintf(file
," %s",cloog_names_parameter_elt (names
, i
)) ;
196 fprintf(file
,"No parameters\n") ;
200 fprintf(file
,"+-- No CloogNames\n") ;
201 fprintf(file
, "Number of active references: %d\n", cloog_names_references (names
));
206 * cloog_names_print function:
207 * This function prints the content of a CloogNames structure (names) into a
208 * file (file, possibly stdout).
209 * - July 1st 2005: Now this function is only a frontend to
210 * cloog_program_print_structure, with a quite better
211 * human-readable representation.
213 void cloog_names_print(FILE * file
, CloogNames
* names
)
214 { cloog_names_print_structure(file
,names
,0) ;
218 /******************************************************************************
219 * Memory deallocation function *
220 ******************************************************************************/
224 * cloog_names_free function:
225 * This function decrements the number of active references to
226 * a CloogNames structure and frees the allocated memory for this structure
227 * if the count drops to zero.
229 void cloog_names_free(CloogNames
* names
)
232 cloog_names_dec_references (names
);
233 if (cloog_names_references (names
))
236 if (cloog_names_scalars (names
))
238 for (i
=0;i
<cloog_names_nb_scalars (names
);i
++)
239 free (cloog_names_scalar_elt (names
, i
));
240 free (cloog_names_scalars (names
));
243 if (cloog_names_scattering (names
))
245 for (i
=0;i
<cloog_names_nb_scattering (names
);i
++)
246 free (cloog_names_scattering_elt (names
, i
));
247 free (cloog_names_scattering (names
));
250 if (cloog_names_iterators (names
))
252 for (i
=0;i
<cloog_names_nb_iterators (names
);i
++)
253 free (cloog_names_iterator_elt (names
, i
)) ;
254 free (cloog_names_iterators (names
)) ;
257 if (cloog_names_parameters (names
))
259 for (i
=0;i
<cloog_names_nb_parameters (names
);i
++)
260 free (cloog_names_parameter_elt (names
, i
));
261 free(cloog_names_parameters (names
));
268 * cloog_names_copy function:
269 * As usual in CLooG, "copy" means incrementing the reference count.
271 CloogNames
*cloog_names_copy(CloogNames
*names
)
273 cloog_names_inc_references (names
);
278 /******************************************************************************
279 * Reading functions *
280 ******************************************************************************/
284 * cloog_names_read_strings function:
285 * This function reads names data from a file (file, possibly stdin). It first
286 * reads the naming option to know if whether has to automatically generate the
287 * names, or to read them. Names are stored into an array of strings, and a
288 * pointer to this array is returned.
289 * - nb_items is the number of names the function will have to read if the
290 * naming option is set to read.
291 * - prefix is the prefix to give to each name in case of automatic generation.
292 * - first item is the name of the first suffix in case of automatic generation.
294 * - September 9th 2002: first version.
296 char ** cloog_names_read_strings(FILE *file
, int nb_items
, char *prefix
, char first_item
)
298 char s
[MAX_STRING
], str
[MAX_STRING
], * c
, ** names
;
300 /* We first read name option. */
301 while (fgets(s
,MAX_STRING
,file
) == 0) ;
302 while ((*s
=='#' || *s
=='\n') || (sscanf(s
," %d",&option
)<1))
303 fgets(s
,MAX_STRING
,file
) ;
305 /* If there is no item to read, then return NULL. */
309 /* If option is to read them in the file, then we do it and put them into
313 { /* Memory allocation. */
314 names
= (char **)malloc(nb_items
*sizeof(char *)) ;
316 { fprintf(stderr
, "[CLooG]ERROR: memory overflow.\n") ;
319 for (i
=0;i
<nb_items
;i
++)
320 { names
[i
] = (char *)malloc(MAX_NAME
*sizeof(char)) ;
321 if (names
[i
] == NULL
)
322 { fprintf(stderr
, "[CLooG]ERROR: memory overflow.\n") ;
327 do /* Skip the comments, spaces and empty lines... */
328 { c
= fgets(s
,MAX_STRING
,file
) ;
329 while ((c
!= NULL
) && isspace(*c
) && (*c
!= '\n'))
332 while (c
!= NULL
&& (*c
== '#' || *c
== '\n'));
335 { fprintf(stderr
, "[CLooG]ERROR: no names in input file.\n") ;
338 for (i
=0;i
<nb_items
;i
++)
339 { /* All names must be on the same line. */
342 if (c
== NULL
|| *c
== '#' || *c
== '\n')
343 { fprintf(stderr
, "[CLooG]ERROR: not enough names in input file.\n") ;
346 /* n is strlen(str). */
347 if (sscanf(c
,"%s%n",str
,&n
) == 0)
348 { fprintf(stderr
, "[CLooG]ERROR: no names in input file.\n") ;
351 sscanf(str
,"%s",names
[i
]) ;
355 /* Else we create names automatically. */
357 names
= cloog_names_generate_items(nb_items
,prefix
,first_item
) ;
363 /******************************************************************************
364 * Processing functions *
365 ******************************************************************************/
369 * cloog_names_malloc function:
370 * This function allocates the memory space for a CloogNames structure and
371 * sets its fields with default values. Then it returns a pointer to the
373 * - November 21th 2005: first version.
375 CloogNames
* cloog_names_malloc (void)
376 { CloogNames
* names
;
378 /* Memory allocation for the CloogNames structure. */
379 names
= (CloogNames
*)malloc(sizeof(CloogNames
)) ;
381 { fprintf(stderr
, "[CLooG]ERROR: memory overflow.\n") ;
385 /* We set the various fields with default values. */
386 cloog_names_set_nb_scalars (names
, 0);
387 cloog_names_set_nb_scattering (names
, 0);
388 cloog_names_set_nb_iterators (names
, 0);
389 cloog_names_set_nb_parameters (names
, 0);
390 cloog_names_set_scalars (names
, NULL
);
391 cloog_names_set_scattering (names
, NULL
);
392 cloog_names_set_iterators (names
, NULL
);
393 cloog_names_set_parameters (names
, NULL
);
394 cloog_names_init_references (names
);
401 * cloog_names_alloc function:
402 * This function allocates the memory space for a CloogNames structure and
403 * sets its fields with those given as input. Then it returns a pointer to the
405 * - July 7th 2005: first version.
406 * - September 11th 2005: addition of both scalar and scattering informations.
407 * - November 21th 2005: use of cloog_names_malloc.
409 CloogNames
* cloog_names_alloc(
410 int nb_scalars
, int nb_scattering
, int nb_iterators
, int nb_parameters
,
411 char **scalars
, char **scattering
, char **iterators
, char **parameters
)
412 { CloogNames
* names
;
414 /* Memory allocation for the CloogNames structure. */
415 names
= cloog_names_malloc() ;
417 cloog_names_set_nb_scalars (names
, nb_scalars
);
418 cloog_names_set_nb_scattering (names
, nb_scattering
);
419 cloog_names_set_nb_iterators (names
, nb_iterators
);
420 cloog_names_set_nb_parameters (names
, nb_parameters
);
421 cloog_names_set_scalars (names
, scalars
);
422 cloog_names_set_scattering (names
, scattering
);
423 cloog_names_set_iterators (names
, iterators
);
424 cloog_names_set_parameters (names
, parameters
);
431 * cloog_names_generate_items function:
432 * This function returns a pointer to an array of strings with entries set
433 * based on the function's parameters.
434 * - nb_items will be the number of entries in the string array.
435 * - prefix is the name prefix of each item or NULL.
436 * If not NULL, then the remainder of the name will be an integer
437 * in the range [0, nb_items-1].
438 * - first_item is the name of the first item (if prefix == NULL),
439 * the nb_items-1 following items will be the nb_items-1
440 * following letters in ASCII code.
442 * - September 9th 2002 : first version, extracted from cloog_names_generate.
444 char ** cloog_names_generate_items(int nb_items
, char * prefix
, char first_item
)
451 names
= (char **)malloc(nb_items
*sizeof(char *)) ;
453 { fprintf(stderr
, "[CLooG]ERROR: memory overflow.\n") ;
456 for (i
=0;i
<nb_items
;i
++)
457 { names
[i
] = (char *)malloc(MAX_NAME
*sizeof(char)) ;
458 if (names
[i
] == NULL
)
459 { fprintf(stderr
, "[CLooG]ERROR: memory overflow.\n") ;
463 sprintf(names
[i
],"%c",first_item
+i
) ;
465 sprintf(names
[i
], "%s%d", prefix
, 1+i
);
473 * cloog_names_generate function:
474 * This function returns a pointer to a CloogNames structure with fields set
475 * thanks to the function's parameters.
476 * - nb_scalars will be the number of scalar dimensions in the structure.
477 * - nb_scattering will be the number of scattering dimensions in the structure.
478 * - nb_iterators will be the number of iterators in the CloogNames structure.
479 * - nb_parameters will be the number of parameters in the CloogNames structure.
480 * - first_s is the name of the first scalar iterator, the nb_scalars-1
481 * following iterators will be the nb_scalars-1 following letters in ASCII.
482 * - first_t is the name of the first scattering iterator, the nb_scattering-1
483 * following iterators will be the nb_scattering-1 following letters in ASCII.
484 * - first_i is the name of the first iterator, the nb_iterators-1 following
485 * iterators will be the nb_iterators-1 following letters in ASCII code.
486 * - first_i is the name of the first iterator, the nb_iterators-1 following
487 * iterators will be the nb_iterators-1 following letters in ASCII code.
488 * - first_p is the name of the first parameter, the nb_parameters-1 following
489 * parameters will be the nb_parameters-1 following letters in ASCII code.
491 * - July 1st 2002 : first version.
492 * - September 9th 2002 : use of cloog_names_generate_items.
493 * - September 11th 2005 : addition of both scalar and scattering informations.
495 CloogNames
* cloog_names_generate(
496 int nb_scalars
, int nb_scattering
, int nb_iterators
, int nb_parameters
,
497 char first_s
, char first_t
, char first_i
, char first_p
)
498 { CloogNames
* names
;
500 names
= (CloogNames
*)malloc(sizeof(CloogNames
)) ;
502 { fprintf(stderr
, "[CLooG]ERROR: memory overflow.\n") ;
506 cloog_names_set_nb_scalars (names
, nb_scalars
);
507 cloog_names_set_nb_scattering (names
, nb_scattering
);
508 cloog_names_set_nb_parameters (names
, nb_parameters
);
509 cloog_names_set_nb_iterators (names
, nb_iterators
);
510 cloog_names_set_scalars (names
, cloog_names_generate_items (nb_scalars
, NULL
, first_s
));
511 cloog_names_set_scattering (names
, cloog_names_generate_items (nb_scattering
,
513 cloog_names_set_parameters (names
, cloog_names_generate_items (nb_parameters
, NULL
, first_p
));
514 cloog_names_set_iterators (names
, cloog_names_generate_items (nb_iterators
, NULL
, first_i
));
520 /* Lastly we update the CLoogNames structure: the iterators corresponding to
521 * scalar dimensions have to be removed since these dimensions have been
522 * erased and do not need to be print. We copy all the iterator names except
523 * the scalar ones in a new string array.
524 * - September 12th 2005: first version.
526 void cloog_names_scalarize(CloogNames
* names
, int nb_scattdims
, int * scaldims
)
527 { int nb_scalars
, nb_scattering
, i
, current_scalar
, current_scattering
;
528 char ** scalars
, ** scattering
;
530 if (!nb_scattdims
|| (scaldims
== NULL
))
534 for (i
=0;i
<nb_scattdims
;i
++)
541 nb_scattering
= cloog_names_nb_scattering (names
) - nb_scalars
;
542 scattering
= (char **)malloc(nb_scattering
* sizeof(char *)) ;
543 if (scattering
== NULL
)
544 { fprintf(stderr
, "[CLooG]ERROR: memory overflow.\n") ;
547 scalars
= (char **)malloc(nb_scalars
* sizeof(char *)) ;
549 { fprintf(stderr
, "[CLooG]ERROR: memory overflow.\n") ;
554 current_scattering
= 0 ;
555 for (i
=0;i
<nb_scattdims
;i
++)
557 { scattering
[current_scattering
] = cloog_names_scattering_elt (names
, i
);
558 current_scattering
++ ;
561 { scalars
[current_scalar
] = cloog_names_scattering_elt (names
, i
);
566 free(cloog_names_scattering (names
)) ;
567 cloog_names_set_scattering (names
, scattering
);
568 cloog_names_set_scalars (names
, scalars
);
569 cloog_names_set_nb_scattering (names
, nb_scattering
);
570 cloog_names_set_nb_scalars (names
, nb_scalars
);