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
, nb_items
, prefix
, first_item
)
299 char * prefix
, first_item
;
301 char s
[MAX_STRING
], str
[MAX_STRING
], * c
, ** names
;
303 /* We first read name option. */
304 while (fgets(s
,MAX_STRING
,file
) == 0) ;
305 while ((*s
=='#' || *s
=='\n') || (sscanf(s
," %d",&option
)<1))
306 fgets(s
,MAX_STRING
,file
) ;
308 /* If there is no item to read, then return NULL. */
312 /* If option is to read them in the file, then we do it and put them into
316 { /* Memory allocation. */
317 names
= (char **)malloc(nb_items
*sizeof(char *)) ;
319 { fprintf(stderr
, "[CLooG]ERROR: memory overflow.\n") ;
322 for (i
=0;i
<nb_items
;i
++)
323 { names
[i
] = (char *)malloc(MAX_NAME
*sizeof(char)) ;
324 if (names
[i
] == NULL
)
325 { fprintf(stderr
, "[CLooG]ERROR: memory overflow.\n") ;
330 do /* Skip the comments, spaces and empty lines... */
331 { c
= fgets(s
,MAX_STRING
,file
) ;
332 while ((c
!= NULL
) && isspace(*c
) && (*c
!= '\n'))
335 while (c
!= NULL
&& (*c
== '#' || *c
== '\n'));
338 { fprintf(stderr
, "[CLooG]ERROR: no names in input file.\n") ;
341 for (i
=0;i
<nb_items
;i
++)
342 { /* All names must be on the same line. */
345 if (c
== NULL
|| *c
== '#' || *c
== '\n')
346 { fprintf(stderr
, "[CLooG]ERROR: not enough names in input file.\n") ;
349 /* n is strlen(str). */
350 if (sscanf(c
,"%s%n",str
,&n
) == 0)
351 { fprintf(stderr
, "[CLooG]ERROR: no names in input file.\n") ;
354 sscanf(str
,"%s",names
[i
]) ;
358 /* Else we create names automatically. */
360 names
= cloog_names_generate_items(nb_items
,prefix
,first_item
) ;
366 /******************************************************************************
367 * Processing functions *
368 ******************************************************************************/
372 * cloog_names_malloc function:
373 * This function allocates the memory space for a CloogNames structure and
374 * sets its fields with default values. Then it returns a pointer to the
376 * - November 21th 2005: first version.
378 CloogNames
* cloog_names_malloc()
379 { CloogNames
* names
;
381 /* Memory allocation for the CloogNames structure. */
382 names
= (CloogNames
*)malloc(sizeof(CloogNames
)) ;
384 { fprintf(stderr
, "[CLooG]ERROR: memory overflow.\n") ;
388 /* We set the various fields with default values. */
389 cloog_names_set_nb_scalars (names
, 0);
390 cloog_names_set_nb_scattering (names
, 0);
391 cloog_names_set_nb_iterators (names
, 0);
392 cloog_names_set_nb_parameters (names
, 0);
393 cloog_names_set_scalars (names
, NULL
);
394 cloog_names_set_scattering (names
, NULL
);
395 cloog_names_set_iterators (names
, NULL
);
396 cloog_names_set_parameters (names
, NULL
);
397 cloog_names_init_references (names
);
404 * cloog_names_alloc function:
405 * This function allocates the memory space for a CloogNames structure and
406 * sets its fields with those given as input. Then it returns a pointer to the
408 * - July 7th 2005: first version.
409 * - September 11th 2005: addition of both scalar and scattering informations.
410 * - November 21th 2005: use of cloog_names_malloc.
412 CloogNames
* cloog_names_alloc(
413 nb_scalars
, nb_scattering
, nb_iterators
, nb_parameters
,
414 scalars
, scattering
, iterators
, parameters
)
415 int nb_scalars
, nb_scattering
, nb_iterators
, nb_parameters
;
416 char ** scalars
, ** scattering
, ** iterators
, ** parameters
;
417 { CloogNames
* names
;
419 /* Memory allocation for the CloogNames structure. */
420 names
= cloog_names_malloc() ;
422 cloog_names_set_nb_scalars (names
, nb_scalars
);
423 cloog_names_set_nb_scattering (names
, nb_scattering
);
424 cloog_names_set_nb_iterators (names
, nb_iterators
);
425 cloog_names_set_nb_parameters (names
, nb_parameters
);
426 cloog_names_set_scalars (names
, scalars
);
427 cloog_names_set_scattering (names
, scattering
);
428 cloog_names_set_iterators (names
, iterators
);
429 cloog_names_set_parameters (names
, parameters
);
436 * cloog_names_generate_items function:
437 * This function returns a pointer to an array of strings with entries set
438 * based on the function's parameters.
439 * - nb_items will be the number of entries in the string array.
440 * - prefix is the name prefix of each item or NULL.
441 * If not NULL, then the remainder of the name will be an integer
442 * in the range [0, nb_items-1].
443 * - first_item is the name of the first item (if prefix == NULL),
444 * the nb_items-1 following items will be the nb_items-1
445 * following letters in ASCII code.
447 * - September 9th 2002 : first version, extracted from cloog_names_generate.
449 char ** cloog_names_generate_items(int nb_items
, char * prefix
, char first_item
)
456 names
= (char **)malloc(nb_items
*sizeof(char *)) ;
458 { fprintf(stderr
, "[CLooG]ERROR: memory overflow.\n") ;
461 for (i
=0;i
<nb_items
;i
++)
462 { names
[i
] = (char *)malloc(MAX_NAME
*sizeof(char)) ;
463 if (names
[i
] == NULL
)
464 { fprintf(stderr
, "[CLooG]ERROR: memory overflow.\n") ;
468 sprintf(names
[i
],"%c",first_item
+i
) ;
470 sprintf(names
[i
], "%s%d", prefix
, 1+i
);
478 * cloog_names_generate function:
479 * This function returns a pointer to a CloogNames structure with fields set
480 * thanks to the function's parameters.
481 * - nb_scalars will be the number of scalar dimensions in the structure.
482 * - nb_scattering will be the number of scattering dimensions in the structure.
483 * - nb_iterators will be the number of iterators in the CloogNames structure.
484 * - nb_parameters will be the number of parameters in the CloogNames structure.
485 * - first_s is the name of the first scalar iterator, the nb_scalars-1
486 * following iterators will be the nb_scalars-1 following letters in ASCII.
487 * - first_t is the name of the first scattering iterator, the nb_scattering-1
488 * following iterators will be the nb_scattering-1 following letters in ASCII.
489 * - first_i is the name of the first iterator, the nb_iterators-1 following
490 * iterators will be the nb_iterators-1 following letters in ASCII code.
491 * - first_i is the name of the first iterator, the nb_iterators-1 following
492 * iterators will be the nb_iterators-1 following letters in ASCII code.
493 * - first_p is the name of the first parameter, the nb_parameters-1 following
494 * parameters will be the nb_parameters-1 following letters in ASCII code.
496 * - July 1st 2002 : first version.
497 * - September 9th 2002 : use of cloog_names_generate_items.
498 * - September 11th 2005 : addition of both scalar and scattering informations.
500 CloogNames
* cloog_names_generate(
501 nb_scalars
, nb_scattering
, nb_iterators
, nb_parameters
,
502 first_s
, first_t
, first_i
, first_p
)
503 int nb_scalars
, nb_scattering
, nb_iterators
, nb_parameters
;
504 char first_s
, first_t
, first_i
, first_p
;
505 { CloogNames
* names
;
507 names
= (CloogNames
*)malloc(sizeof(CloogNames
)) ;
509 { fprintf(stderr
, "[CLooG]ERROR: memory overflow.\n") ;
513 cloog_names_set_nb_scalars (names
, nb_scalars
);
514 cloog_names_set_nb_scattering (names
, nb_scattering
);
515 cloog_names_set_nb_parameters (names
, nb_parameters
);
516 cloog_names_set_nb_iterators (names
, nb_iterators
);
517 cloog_names_set_scalars (names
, cloog_names_generate_items (nb_scalars
, NULL
, first_s
));
518 cloog_names_set_scattering (names
, cloog_names_generate_items (nb_scattering
,
520 cloog_names_set_parameters (names
, cloog_names_generate_items (nb_parameters
, NULL
, first_p
));
521 cloog_names_set_iterators (names
, cloog_names_generate_items (nb_iterators
, NULL
, first_i
));
527 /* Lastly we update the CLoogNames structure: the iterators corresponding to
528 * scalar dimensions have to be removed since these dimensions have been
529 * erased and do not need to be print. We copy all the iterator names except
530 * the scalar ones in a new string array.
531 * - September 12th 2005: first version.
533 void cloog_names_scalarize(CloogNames
* names
, int nb_scattdims
, int * scaldims
)
534 { int nb_scalars
, nb_scattering
, i
, current_scalar
, current_scattering
;
535 char ** scalars
, ** scattering
;
537 if (!nb_scattdims
|| (scaldims
== NULL
))
541 for (i
=0;i
<nb_scattdims
;i
++)
548 nb_scattering
= cloog_names_nb_scattering (names
) - nb_scalars
;
549 scattering
= (char **)malloc(nb_scattering
* sizeof(char *)) ;
550 if (scattering
== NULL
)
551 { fprintf(stderr
, "[CLooG]ERROR: memory overflow.\n") ;
554 scalars
= (char **)malloc(nb_scalars
* sizeof(char *)) ;
556 { fprintf(stderr
, "[CLooG]ERROR: memory overflow.\n") ;
561 current_scattering
= 0 ;
562 for (i
=0;i
<nb_scattdims
;i
++)
564 { scattering
[current_scattering
] = cloog_names_scattering_elt (names
, i
);
565 current_scattering
++ ;
568 { scalars
[current_scalar
] = cloog_names_scattering_elt (names
, i
);
573 free(cloog_names_scattering (names
)) ;
574 cloog_names_set_scattering (names
, scattering
);
575 cloog_names_set_scalars (names
, scalars
);
576 cloog_names_set_nb_scattering (names
, nb_scattering
);
577 cloog_names_set_nb_scalars (names
, nb_scalars
);