Add --with-host-libstdcxx configure switch.
[cloog-ppl.git] / source / names.c
blobf0913ca7a8ed1bf3c573b9e8c41ee4528a87dc58
2 /**-------------------------------------------------------------------**
3 ** CLooG **
4 **-------------------------------------------------------------------**
5 ** names.c **
6 **-------------------------------------------------------------------**
7 ** First version: august 1st 2002 **
8 **-------------------------------------------------------------------**/
11 /******************************************************************************
12 * CLooG : the Chunky Loop Generator (experimental) *
13 ******************************************************************************
14 * *
15 * Copyright (C) 2002-2005 Cedric Bastoul *
16 * *
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 *
20 * version. *
21 * *
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 *
25 * for more details. *
26 * *
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 *
30 * *
31 * CLooG, the Chunky Loop Generator *
32 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr *
33 * *
34 ******************************************************************************/
35 /* CAUTION: the english used for comments is probably the worst you ever read,
36 * please feel free to correct and improve it !
40 # include <stdlib.h>
41 # include <stdio.h>
42 # include <ctype.h>
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)
53 n->_references = 1;
56 static inline void cloog_names_inc_references (CloogNames *n)
58 n->_references++;
61 static inline void cloog_names_dec_references (CloogNames *n)
63 n->_references--;
67 /******************************************************************************
68 * Structure display function *
69 ******************************************************************************/
72 /**
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)
81 { int i ;
83 /* Go to the right level. */
84 for (i=0; i<level; i++)
85 fprintf(file,"|\t") ;
87 if (names != NULL)
88 { fprintf(file,"+-- CloogNames\n") ;
90 /* A blank line. */
91 for (i=0; i<=level+1; i++)
92 fprintf(file,"|\t") ;
93 fprintf(file,"\n") ;
95 /* Print the scalar dimension number. */
96 for (i=0; i<=level; i++)
97 fprintf(file,"|\t") ;
98 fprintf (file, "Scalar dimension number ---: %d\n", cloog_names_nb_scalars (names));
100 /* A blank line. */
101 for (i=0; i<=level+1; i++)
102 fprintf(file,"|\t") ;
103 fprintf(file,"\n") ;
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)) ;
112 fprintf(file,"\n") ;
114 else
115 fprintf(file,"+-- No scalar string\n") ;
117 /* A blank line. */
118 for (i=0; i<=level+1; i++)
119 fprintf(file,"|\t") ;
120 fprintf(file,"\n") ;
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)) ;
127 /* A blank line. */
128 for (i=0; i<=level+1; i++)
129 fprintf(file,"|\t") ;
130 fprintf(file,"\n") ;
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));
139 fprintf(file,"\n") ;
141 else
142 fprintf(file,"+-- No scattering string\n") ;
144 /* A blank line. */
145 for (i=0; i<=level+1; i++)
146 fprintf(file,"|\t") ;
147 fprintf(file,"\n") ;
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)) ;
154 /* A blank line. */
155 for (i=0; i<=level+1; i++)
156 fprintf(file,"|\t") ;
157 fprintf(file,"\n") ;
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)) ;
166 fprintf(file,"\n") ;
168 else
169 fprintf(file,"+-- No iterators\n") ;
171 /* A blank line. */
172 for (i=0; i<=level+1; i++)
173 fprintf(file,"|\t") ;
174 fprintf(file,"\n") ;
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)) ;
181 /* A blank line. */
182 for (i=0; i<=level+1; i++)
183 fprintf(file,"|\t") ;
184 fprintf(file,"\n") ;
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)) ;
193 fprintf(file,"\n") ;
195 else
196 fprintf(file,"No parameters\n") ;
199 else
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)
230 { int i ;
232 cloog_names_dec_references (names);
233 if (cloog_names_references (names))
234 return;
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));
263 free(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);
274 return 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)
297 { int i, option, n ;
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. */
306 if (nb_items == 0)
307 return NULL ;
309 /* If option is to read them in the file, then we do it and put them into
310 * the array.
312 if (option)
313 { /* Memory allocation. */
314 names = (char **)malloc(nb_items*sizeof(char *)) ;
315 if (names == NULL)
316 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
317 exit(1) ;
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") ;
323 exit(1) ;
327 do /* Skip the comments, spaces and empty lines... */
328 { c = fgets(s,MAX_STRING,file) ;
329 while ((c != NULL) && isspace(*c) && (*c != '\n'))
330 c++ ;
332 while (c != NULL && (*c == '#' || *c == '\n'));
334 if (c == NULL)
335 { fprintf(stderr, "[CLooG]ERROR: no names in input file.\n") ;
336 exit(1) ;
338 for (i=0;i<nb_items;i++)
339 { /* All names must be on the same line. */
340 while (isspace(*c))
341 c++ ;
342 if (c == NULL || *c == '#' || *c == '\n')
343 { fprintf(stderr, "[CLooG]ERROR: not enough names in input file.\n") ;
344 exit(1) ;
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") ;
349 exit(1) ;
351 sscanf(str,"%s",names[i]) ;
352 c += n ;
355 /* Else we create names automatically. */
356 else
357 names = cloog_names_generate_items(nb_items,prefix,first_item) ;
359 return names ;
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
372 * allocated space.
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)) ;
380 if (names == NULL)
381 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
382 exit(1) ;
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);
396 return 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
404 * allocated space.
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);
426 return names ;
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)
445 { int i ;
446 char ** names ;
448 if (nb_items == 0)
449 return NULL ;
451 names = (char **)malloc(nb_items*sizeof(char *)) ;
452 if (names == NULL)
453 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
454 exit(1) ;
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") ;
460 exit(1) ;
462 if (prefix == NULL)
463 sprintf(names[i],"%c",first_item+i) ;
464 else
465 sprintf(names[i], "%s%d", prefix, 1+i);
468 return names ;
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)) ;
501 if (names == NULL)
502 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
503 exit(1) ;
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,
512 NULL, first_t));
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));
516 return names ;
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))
531 return ;
533 nb_scalars = 0 ;
534 for (i=0;i<nb_scattdims;i++)
535 if (scaldims[i])
536 nb_scalars ++ ;
538 if (!nb_scalars)
539 return ;
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") ;
545 exit(1) ;
547 scalars = (char **)malloc(nb_scalars * sizeof(char *)) ;
548 if (scalars == NULL)
549 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
550 exit(1) ;
553 current_scalar = 0 ;
554 current_scattering = 0 ;
555 for (i=0;i<nb_scattdims;i++)
556 { if (!scaldims[i])
557 { scattering[current_scattering] = cloog_names_scattering_elt (names, i);
558 current_scattering ++ ;
560 else
561 { scalars[current_scalar] = cloog_names_scattering_elt (names, i);
562 current_scalar ++ ;
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);