Fix compiler warning
[cloog/uuh.git] / source / names.c
bloba912ccde22e45a9781ed14a1188db79b3e22ab8d
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 library is free software; you can redistribute it and/or *
18 * modify it under the terms of the GNU Lesser General Public *
19 * License as published by the Free Software Foundation; either *
20 * version 2.1 of the License, or (at your option) any later version. *
21 * *
22 * This library is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
25 * Lesser General Public License for more details. *
26 * *
27 * You should have received a copy of the GNU Lesser General Public *
28 * License along with this library; if not, write to the Free Software *
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
30 * Boston, MA 02110-1301 USA *
31 * *
32 * CLooG, the Chunky Loop Generator *
33 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr *
34 * *
35 ******************************************************************************/
36 /* CAUTION: the english used for comments is probably the worst you ever read,
37 * please feel free to correct and improve it !
41 # include <stdlib.h>
42 # include <stdio.h>
43 # include <ctype.h>
44 # include "../include/cloog/cloog.h"
47 /******************************************************************************
48 * Structure display function *
49 ******************************************************************************/
52 /**
53 * cloog_names_print function:
54 * this function is a human-friendly way to display the CloogNames data
55 * structure, it shows all the different fields and includes an indentation
56 * level (level) in order to work with others print_structure functions.
57 * - July 1st 2005: first version based on the old cloog_names_print function,
58 * it was the first modification in this file since two years !
60 void cloog_names_print_structure(FILE * file, CloogNames * names, int level)
61 { int i ;
63 /* Go to the right level. */
64 for (i=0; i<level; i++)
65 fprintf(file,"|\t") ;
67 if (names != NULL)
68 { fprintf(file,"+-- CloogNames\n") ;
70 /* A blank line. */
71 for (i=0; i<=level+1; i++)
72 fprintf(file,"|\t") ;
73 fprintf(file,"\n") ;
75 /* Print the scalar dimension number. */
76 for (i=0; i<=level; i++)
77 fprintf(file,"|\t") ;
78 fprintf(file,"Scalar dimension number ---: %d\n",names->nb_scalars) ;
80 /* A blank line. */
81 for (i=0; i<=level+1; i++)
82 fprintf(file,"|\t") ;
83 fprintf(file,"\n") ;
85 /* Print the scalar iterators. */
86 for (i=0; i<=level; i++)
87 fprintf(file,"|\t") ;
88 if (names->nb_scalars > 0)
89 { fprintf(file,"+-- Scalar iterator strings:") ;
90 for (i=0;i<names->nb_scalars;i++)
91 fprintf(file," %s",names->scalars[i]) ;
92 fprintf(file,"\n") ;
94 else
95 fprintf(file,"+-- No scalar string\n") ;
97 /* A blank line. */
98 for (i=0; i<=level+1; i++)
99 fprintf(file,"|\t") ;
100 fprintf(file,"\n") ;
102 /* Print the scattering dimension number. */
103 for (i=0; i<=level; i++)
104 fprintf(file,"|\t") ;
105 fprintf(file,"Scattering dimension number: %d\n",names->nb_scattering) ;
107 /* A blank line. */
108 for (i=0; i<=level+1; i++)
109 fprintf(file,"|\t") ;
110 fprintf(file,"\n") ;
112 /* Print the scattering iterators. */
113 for (i=0; i<=level; i++)
114 fprintf(file,"|\t") ;
115 if (names->nb_scattering > 0)
116 { fprintf(file,"+-- Scattering strings ----:") ;
117 for (i=0;i<names->nb_scattering;i++)
118 fprintf(file," %s",names->scattering[i]) ;
119 fprintf(file,"\n") ;
121 else
122 fprintf(file,"+-- No scattering string\n") ;
124 /* A blank line. */
125 for (i=0; i<=level+1; i++)
126 fprintf(file,"|\t") ;
127 fprintf(file,"\n") ;
129 /* Print the iterator number. */
130 for (i=0; i<=level; i++)
131 fprintf(file,"|\t") ;
132 fprintf(file,"Iterator number -----------: %d\n",names->nb_iterators) ;
134 /* A blank line. */
135 for (i=0; i<=level+1; i++)
136 fprintf(file,"|\t") ;
137 fprintf(file,"\n") ;
139 /* Print the iterators. */
140 for (i=0; i<=level; i++)
141 fprintf(file,"|\t") ;
142 if (names->nb_iterators > 0)
143 { fprintf(file,"+-- Iterator strings ------:") ;
144 for (i=0;i<names->nb_iterators;i++)
145 fprintf(file," %s",names->iterators[i]) ;
146 fprintf(file,"\n") ;
148 else
149 fprintf(file,"+-- No iterators\n") ;
151 /* A blank line. */
152 for (i=0; i<=level+1; i++)
153 fprintf(file,"|\t") ;
154 fprintf(file,"\n") ;
156 /* Print the parameter number. */
157 for (i=0; i<=level; i++)
158 fprintf(file,"|\t") ;
159 fprintf(file,"Parameter number ----------: %d\n",names->nb_parameters) ;
161 /* A blank line. */
162 for (i=0; i<=level+1; i++)
163 fprintf(file,"|\t") ;
164 fprintf(file,"\n") ;
166 /* Print the parameters. */
167 for (i=0; i<=level; i++)
168 fprintf(file,"|\t") ;
169 if (names->nb_parameters > 0)
170 { fprintf(file,"+-- Parameter strings -----:") ;
171 for (i=0;i<names->nb_parameters;i++)
172 fprintf(file," %s",names->parameters[i]) ;
173 fprintf(file,"\n") ;
175 else
176 fprintf(file,"No parameters\n") ;
179 else
180 fprintf(file,"+-- No CloogNames\n") ;
181 fprintf(file, "Number of active references: %d\n", names->references);
186 * cloog_names_print function:
187 * This function prints the content of a CloogNames structure (names) into a
188 * file (file, possibly stdout).
189 * - July 1st 2005: Now this function is only a frontend to
190 * cloog_program_print_structure, with a quite better
191 * human-readable representation.
193 void cloog_names_print(FILE * file, CloogNames * names)
194 { cloog_names_print_structure(file,names,0) ;
198 /******************************************************************************
199 * Memory deallocation function *
200 ******************************************************************************/
204 * cloog_names_free function:
205 * This function decrements the number of active references to
206 * a CloogNames structure and frees the allocated memory for this structure
207 * if the count drops to zero.
209 void cloog_names_free(CloogNames * names)
210 { int i ;
212 if (--names->references)
213 return;
215 if (names->scalars != NULL)
216 { for (i=0;i<names->nb_scalars;i++)
217 free(names->scalars[i]) ;
218 free(names->scalars) ;
221 if (names->scattering != NULL)
222 { for (i=0;i<names->nb_scattering;i++)
223 free(names->scattering[i]) ;
224 free(names->scattering) ;
227 if (names->iterators != NULL)
228 { for (i=0;i<names->nb_iterators;i++)
229 free(names->iterators[i]) ;
230 free(names->iterators) ;
233 if (names->parameters != NULL)
234 { for (i=0;i<names->nb_parameters;i++)
235 free(names->parameters[i]) ;
236 free(names->parameters) ;
238 free(names) ;
243 * cloog_names_copy function:
244 * As usual in CLooG, "copy" means incrementing the reference count.
246 CloogNames *cloog_names_copy(CloogNames *names)
248 names->references++;
249 return names;
253 /******************************************************************************
254 * Reading functions *
255 ******************************************************************************/
259 * cloog_names_read_strings function:
260 * This function reads names data from a file (file, possibly stdin). It first
261 * reads the naming option to know if whether has to automatically generate the
262 * names, or to read them. Names are stored into an array of strings, and a
263 * pointer to this array is returned.
264 * - nb_items is the number of names the function will have to read if the
265 * naming option is set to read.
266 * - prefix is the prefix to give to each name in case of automatic generation.
267 * - first item is the name of the first suffix in case of automatic generation.
269 * - September 9th 2002: first version.
271 char ** cloog_names_read_strings(file, nb_items, prefix, first_item)
272 FILE * file ;
273 int nb_items ;
274 char * prefix, first_item ;
275 { int i, option, n ;
276 char s[MAX_STRING], str[MAX_STRING], * c, ** names ;
278 /* We first read name option. */
279 while (fgets(s,MAX_STRING,file) == 0) ;
280 while ((*s=='#' || *s=='\n') || (sscanf(s," %d",&option)<1))
281 fgets(s,MAX_STRING,file) ;
283 /* If there is no item to read, then return NULL. */
284 if (nb_items == 0)
285 return NULL ;
287 /* If option is to read them in the file, then we do it and put them into
288 * the array.
290 if (option)
291 { /* Memory allocation. */
292 names = (char **)malloc(nb_items*sizeof(char *)) ;
293 if (names == NULL)
294 cloog_die("memory overflow.\n");
295 for (i=0;i<nb_items;i++)
296 { names[i] = (char *)malloc(MAX_NAME*sizeof(char)) ;
297 if (names[i] == NULL)
298 cloog_die("memory overflow.\n");
301 do /* Skip the comments, spaces and empty lines... */
302 { c = fgets(s,MAX_STRING,file) ;
303 while ((c != NULL) && isspace(*c) && (*c != '\n'))
304 c++ ;
306 while (c != NULL && (*c == '#' || *c == '\n'));
308 if (c == NULL)
309 cloog_die("no names in input file.\n");
310 for (i=0;i<nb_items;i++)
311 { /* All names must be on the same line. */
312 while (isspace(*c))
313 c++ ;
314 if (!*c || *c == '#' || *c == '\n')
315 cloog_die("not enough names in input file.\n");
316 /* n is strlen(str). */
317 if (sscanf(c,"%s%n",str,&n) == 0)
318 cloog_die("no names in input file.\n");
319 sscanf(str,"%s",names[i]) ;
320 c += n ;
323 /* Else we create names automatically. */
324 else
325 names = cloog_names_generate_items(nb_items,prefix,first_item) ;
327 return names ;
331 /******************************************************************************
332 * Processing functions *
333 ******************************************************************************/
337 * cloog_names_malloc function:
338 * This function allocates the memory space for a CloogNames structure and
339 * sets its fields with default values. Then it returns a pointer to the
340 * allocated space.
341 * - November 21th 2005: first version.
343 CloogNames * cloog_names_malloc()
344 { CloogNames * names ;
346 /* Memory allocation for the CloogNames structure. */
347 names = (CloogNames *)malloc(sizeof(CloogNames)) ;
348 if (names == NULL)
349 cloog_die("memory overflow.\n");
351 /* We set the various fields with default values. */
352 names->nb_scalars = 0 ;
353 names->nb_scattering = 0 ;
354 names->nb_iterators = 0 ;
355 names->nb_parameters = 0 ;
356 names->scalars = NULL ;
357 names->scattering = NULL ;
358 names->iterators = NULL ;
359 names->parameters = NULL ;
360 names->references = 1;
362 return names ;
367 * cloog_names_alloc function:
368 * This function allocates the memory space for a CloogNames structure and
369 * sets its fields with those given as input. Then it returns a pointer to the
370 * allocated space.
371 * - July 7th 2005: first version.
372 * - September 11th 2005: addition of both scalar and scattering informations.
373 * - November 21th 2005: use of cloog_names_malloc.
375 CloogNames * cloog_names_alloc()
376 { CloogNames * names ;
378 /* Memory allocation for the CloogNames structure. */
379 names = cloog_names_malloc() ;
381 names->nb_scalars = 0;
382 names->nb_scattering = 0;
383 names->nb_iterators = 0;
384 names->nb_parameters = 0;
385 names->scalars = NULL;
386 names->scattering = NULL;
387 names->iterators = NULL;
388 names->parameters = NULL;
390 return names ;
395 * cloog_names_generate_items function:
396 * This function returns a pointer to an array of strings with entries set
397 * based on the function's parameters.
398 * - nb_items will be the number of entries in the string array.
399 * - prefix is the name prefix of each item or NULL.
400 * If not NULL, then the remainder of the name will be an integer
401 * in the range [0, nb_items-1].
402 * - first_item is the name of the first item (if prefix == NULL),
403 * the nb_items-1 following items will be the nb_items-1
404 * following letters in ASCII code.
406 * - September 9th 2002 : first version, extracted from cloog_names_generate.
408 char ** cloog_names_generate_items(int nb_items, char * prefix, char first_item)
409 { int i ;
410 char ** names ;
412 if (nb_items == 0)
413 return NULL ;
415 names = (char **)malloc(nb_items*sizeof(char *)) ;
416 if (names == NULL)
417 cloog_die("memory overflow.\n");
418 for (i=0;i<nb_items;i++)
419 { names[i] = (char *)malloc(MAX_NAME*sizeof(char)) ;
420 if (names[i] == NULL)
421 cloog_die("memory overflow.\n");
422 if (prefix == NULL)
423 sprintf(names[i],"%c",first_item+i) ;
424 else
425 sprintf(names[i], "%s%d", prefix, 1+i);
428 return names ;
433 * cloog_names_generate function:
434 * This function returns a pointer to a CloogNames structure with fields set
435 * thanks to the function's parameters.
436 * - nb_scalars will be the number of scalar dimensions in the structure.
437 * - nb_scattering will be the number of scattering dimensions in the structure.
438 * - nb_iterators will be the number of iterators in the CloogNames structure.
439 * - nb_parameters will be the number of parameters in the CloogNames structure.
440 * - first_s is the name of the first scalar iterator, the nb_scalars-1
441 * following iterators will be the nb_scalars-1 following letters in ASCII.
442 * - first_t is the name of the first scattering iterator, the nb_scattering-1
443 * following iterators will be the nb_scattering-1 following letters in ASCII.
444 * - first_i is the name of the first iterator, the nb_iterators-1 following
445 * iterators will be the nb_iterators-1 following letters in ASCII code.
446 * - first_i is the name of the first iterator, the nb_iterators-1 following
447 * iterators will be the nb_iterators-1 following letters in ASCII code.
448 * - first_p is the name of the first parameter, the nb_parameters-1 following
449 * parameters will be the nb_parameters-1 following letters in ASCII code.
451 * - July 1st 2002 : first version.
452 * - September 9th 2002 : use of cloog_names_generate_items.
453 * - September 11th 2005 : addition of both scalar and scattering informations.
455 CloogNames * cloog_names_generate(
456 nb_scalars, nb_scattering, nb_iterators, nb_parameters,
457 first_s, first_t, first_i, first_p)
458 int nb_scalars, nb_scattering, nb_iterators, nb_parameters ;
459 char first_s, first_t, first_i, first_p ;
460 { CloogNames * names ;
462 names = (CloogNames *)malloc(sizeof(CloogNames)) ;
463 if (names == NULL)
464 cloog_die("memory overflow.\n");
466 names->nb_scalars = nb_scalars ;
467 names->nb_scattering = nb_scattering ;
468 names->nb_parameters = nb_parameters ;
469 names->nb_iterators = nb_iterators ;
470 names->scalars = cloog_names_generate_items(nb_scalars, NULL,first_s);
471 names->scattering = cloog_names_generate_items(nb_scattering,NULL,first_t);
472 names->parameters = cloog_names_generate_items(nb_parameters,NULL,first_p);
473 names->iterators = cloog_names_generate_items(nb_iterators, NULL,first_i);
475 return names ;
479 /* Lastly we update the CLoogNames structure: the iterators corresponding to
480 * scalar dimensions have to be removed since these dimensions have been
481 * erased and do not need to be print. We copy all the iterator names except
482 * the scalar ones in a new string array.
483 * - September 12th 2005: first version.
485 void cloog_names_scalarize(CloogNames * names, int nb_scattdims, int * scaldims)
486 { int nb_scalars, nb_scattering, i, current_scalar, current_scattering ;
487 char ** scalars, ** scattering ;
489 if (!nb_scattdims || (scaldims == NULL))
490 return ;
492 nb_scalars = 0 ;
493 for (i=0;i<nb_scattdims;i++)
494 if (scaldims[i])
495 nb_scalars ++ ;
497 if (!nb_scalars)
498 return ;
500 nb_scattering = names->nb_scattering - nb_scalars ;
501 scattering = (char **)malloc(nb_scattering * sizeof(char *)) ;
502 if (scattering == NULL)
503 cloog_die("memory overflow.\n");
504 scalars = (char **)malloc(nb_scalars * sizeof(char *)) ;
505 if (scalars == NULL)
506 cloog_die("memory overflow.\n");
508 current_scalar = 0 ;
509 current_scattering = 0 ;
510 for (i=0;i<nb_scattdims;i++)
511 { if (!scaldims[i])
512 { scattering[current_scattering] = names->scattering[i] ;
513 current_scattering ++ ;
515 else
516 { scalars[current_scalar] = names->scattering[i] ;
517 current_scalar ++ ;
521 free(names->scattering) ;
522 names->scattering = scattering ;
523 names->scalars = scalars ;
524 names->nb_scattering = nb_scattering ;
525 names->nb_scalars = nb_scalars ;
529 * Return the name at a given level (starting at one).
530 * May be a scattering dimension or an iterator of the original domain.
532 const char *cloog_names_name_at_level(CloogNames *names, int level)
534 if (level <= names->nb_scattering)
535 return names->scattering[level - 1];
536 else
537 return names->iterators[level - names->nb_scattering - 1];