port Vector_Copy.
[cloog-ppl.git] / source / names.c
blob61a3e0834530c789f63ccfc9ed34aa1db10331ef
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, nb_items, prefix, first_item)
297 FILE * file ;
298 int nb_items ;
299 char * prefix, first_item ;
300 { int i, option, n ;
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. */
309 if (nb_items == 0)
310 return NULL ;
312 /* If option is to read them in the file, then we do it and put them into
313 * the array.
315 if (option)
316 { /* Memory allocation. */
317 names = (char **)malloc(nb_items*sizeof(char *)) ;
318 if (names == NULL)
319 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
320 exit(1) ;
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") ;
326 exit(1) ;
330 do /* Skip the comments, spaces and empty lines... */
331 { c = fgets(s,MAX_STRING,file) ;
332 while ((c != NULL) && isspace(*c) && (*c != '\n'))
333 c++ ;
335 while (c != NULL && (*c == '#' || *c == '\n'));
337 if (c == NULL)
338 { fprintf(stderr, "[CLooG]ERROR: no names in input file.\n") ;
339 exit(1) ;
341 for (i=0;i<nb_items;i++)
342 { /* All names must be on the same line. */
343 while (isspace(*c))
344 c++ ;
345 if (c == NULL || *c == '#' || *c == '\n')
346 { fprintf(stderr, "[CLooG]ERROR: not enough names in input file.\n") ;
347 exit(1) ;
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") ;
352 exit(1) ;
354 sscanf(str,"%s",names[i]) ;
355 c += n ;
358 /* Else we create names automatically. */
359 else
360 names = cloog_names_generate_items(nb_items,prefix,first_item) ;
362 return names ;
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
375 * allocated space.
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)) ;
383 if (names == NULL)
384 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
385 exit(1) ;
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);
399 return 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
407 * allocated space.
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);
431 return names ;
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)
450 { int i ;
451 char ** names ;
453 if (nb_items == 0)
454 return NULL ;
456 names = (char **)malloc(nb_items*sizeof(char *)) ;
457 if (names == NULL)
458 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
459 exit(1) ;
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") ;
465 exit(1) ;
467 if (prefix == NULL)
468 sprintf(names[i],"%c",first_item+i) ;
469 else
470 sprintf(names[i], "%s%d", prefix, 1+i);
473 return names ;
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)) ;
508 if (names == NULL)
509 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
510 exit(1) ;
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,
519 NULL, first_t));
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));
523 return names ;
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))
538 return ;
540 nb_scalars = 0 ;
541 for (i=0;i<nb_scattdims;i++)
542 if (scaldims[i])
543 nb_scalars ++ ;
545 if (!nb_scalars)
546 return ;
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") ;
552 exit(1) ;
554 scalars = (char **)malloc(nb_scalars * sizeof(char *)) ;
555 if (scalars == NULL)
556 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
557 exit(1) ;
560 current_scalar = 0 ;
561 current_scattering = 0 ;
562 for (i=0;i<nb_scattdims;i++)
563 { if (!scaldims[i])
564 { scattering[current_scattering] = cloog_names_scattering_elt (names, i);
565 current_scattering ++ ;
567 else
568 { scalars[current_scalar] = cloog_names_scattering_elt (names, i);
569 current_scalar ++ ;
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);