First commit : 0.14.0 version (with roadmap in doc instead of
[cloog/uuh.git] / source / names.c
blobf0ade0ed93df6cc332804ea493e6a080ac99357a
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 /******************************************************************************
47 * Structure display function *
48 ******************************************************************************/
51 /**
52 * cloog_names_print function:
53 * this function is a human-friendly way to display the CloogNames data
54 * structure, it shows all the different fields and includes an indentation
55 * level (level) in order to work with others print_structure functions.
56 * - July 1st 2005: first version based on the old cloog_names_print function,
57 * it was the first modification in this file since two years !
59 void cloog_names_print_structure(FILE * file, CloogNames * names, int level)
60 { int i ;
62 /* Go to the right level. */
63 for (i=0; i<level; i++)
64 fprintf(file,"|\t") ;
66 if (names != NULL)
67 { fprintf(file,"+-- CloogNames\n") ;
69 /* A blank line. */
70 for (i=0; i<=level+1; i++)
71 fprintf(file,"|\t") ;
72 fprintf(file,"\n") ;
74 /* Print the scalar dimension number. */
75 for (i=0; i<=level; i++)
76 fprintf(file,"|\t") ;
77 fprintf(file,"Scalar dimension number ---: %d\n",names->nb_scalars) ;
79 /* A blank line. */
80 for (i=0; i<=level+1; i++)
81 fprintf(file,"|\t") ;
82 fprintf(file,"\n") ;
84 /* Print the scalar iterators. */
85 for (i=0; i<=level; i++)
86 fprintf(file,"|\t") ;
87 if (names->nb_scalars > 0)
88 { fprintf(file,"+-- Scalar iterator strings:") ;
89 for (i=0;i<names->nb_scalars;i++)
90 fprintf(file," %s",names->scalars[i]) ;
91 fprintf(file,"\n") ;
93 else
94 fprintf(file,"+-- No scalar string\n") ;
96 /* A blank line. */
97 for (i=0; i<=level+1; i++)
98 fprintf(file,"|\t") ;
99 fprintf(file,"\n") ;
101 /* Print the scattering dimension number. */
102 for (i=0; i<=level; i++)
103 fprintf(file,"|\t") ;
104 fprintf(file,"Scattering dimension number: %d\n",names->nb_scattering) ;
106 /* A blank line. */
107 for (i=0; i<=level+1; i++)
108 fprintf(file,"|\t") ;
109 fprintf(file,"\n") ;
111 /* Print the scattering iterators. */
112 for (i=0; i<=level; i++)
113 fprintf(file,"|\t") ;
114 if (names->nb_scattering > 0)
115 { fprintf(file,"+-- Scattering strings ----:") ;
116 for (i=0;i<names->nb_scattering;i++)
117 fprintf(file," %s",names->scattering[i]) ;
118 fprintf(file,"\n") ;
120 else
121 fprintf(file,"+-- No scattering string\n") ;
123 /* A blank line. */
124 for (i=0; i<=level+1; i++)
125 fprintf(file,"|\t") ;
126 fprintf(file,"\n") ;
128 /* Print the iterator number. */
129 for (i=0; i<=level; i++)
130 fprintf(file,"|\t") ;
131 fprintf(file,"Iterator number -----------: %d\n",names->nb_iterators) ;
133 /* A blank line. */
134 for (i=0; i<=level+1; i++)
135 fprintf(file,"|\t") ;
136 fprintf(file,"\n") ;
138 /* Print the iterators. */
139 for (i=0; i<=level; i++)
140 fprintf(file,"|\t") ;
141 if (names->nb_iterators > 0)
142 { fprintf(file,"+-- Iterator strings ------:") ;
143 for (i=0;i<names->nb_iterators;i++)
144 fprintf(file," %s",names->iterators[i]) ;
145 fprintf(file,"\n") ;
147 else
148 fprintf(file,"+-- No iterators\n") ;
150 /* A blank line. */
151 for (i=0; i<=level+1; i++)
152 fprintf(file,"|\t") ;
153 fprintf(file,"\n") ;
155 /* Print the parameter number. */
156 for (i=0; i<=level; i++)
157 fprintf(file,"|\t") ;
158 fprintf(file,"Parameter number ----------: %d\n",names->nb_parameters) ;
160 /* A blank line. */
161 for (i=0; i<=level+1; i++)
162 fprintf(file,"|\t") ;
163 fprintf(file,"\n") ;
165 /* Print the parameters. */
166 for (i=0; i<=level; i++)
167 fprintf(file,"|\t") ;
168 if (names->nb_parameters > 0)
169 { fprintf(file,"+-- Parameter strings -----:") ;
170 for (i=0;i<names->nb_parameters;i++)
171 fprintf(file," %s",names->parameters[i]) ;
172 fprintf(file,"\n") ;
174 else
175 fprintf(file,"No parameters\n") ;
178 else
179 fprintf(file,"+-- No CloogNames\n") ;
184 * cloog_names_print function:
185 * This function prints the content of a CloogNames structure (names) into a
186 * file (file, possibly stdout).
187 * - July 1st 2005: Now this function is only a frontend to
188 * cloog_program_print_structure, with a quite better
189 * human-readable representation.
191 void cloog_names_print(FILE * file, CloogNames * names)
192 { cloog_names_print_structure(file,names,0) ;
196 /******************************************************************************
197 * Memory deallocation function *
198 ******************************************************************************/
202 * cloog_names_free function:
203 * This function frees the allocated memory for a CloogNames structure.
205 void cloog_names_free(CloogNames * names)
206 { int i ;
208 if (names->scalars != NULL)
209 { for (i=0;i<names->nb_scalars;i++)
210 free(names->scalars[i]) ;
211 free(names->scalars) ;
214 if (names->scattering != NULL)
215 { for (i=0;i<names->nb_scattering;i++)
216 free(names->scattering[i]) ;
217 free(names->scattering) ;
220 if (names->iterators != NULL)
221 { for (i=0;i<names->nb_iterators;i++)
222 free(names->iterators[i]) ;
223 free(names->iterators) ;
226 if (names->parameters != NULL)
227 { for (i=0;i<names->nb_parameters;i++)
228 free(names->parameters[i]) ;
229 free(names->parameters) ;
231 free(names) ;
235 /******************************************************************************
236 * Reading functions *
237 ******************************************************************************/
241 * cloog_names_read_strings function:
242 * This function reads names data from a file (file, possibly stdin). It first
243 * reads the naming option to know if whether has to automatically generate the
244 * names, or to read them. Names are stored into an array of strings, and a
245 * pointer to this array is returned.
246 * - nb_items is the number of names the function will have to read if the
247 * naming option is set to read.
248 * - prefix is the prefix to give to each name in case of automatic generation.
249 * - first item is the name of the first suffix in case of automatic generation.
251 * - September 9th 2002: first version.
253 char ** cloog_names_read_strings(file, nb_items, prefix, first_item)
254 FILE * file ;
255 int nb_items ;
256 char * prefix, first_item ;
257 { int i, option, n ;
258 char s[MAX_STRING], str[MAX_STRING], * c, ** names ;
260 /* We first read name option. */
261 while (fgets(s,MAX_STRING,file) == 0) ;
262 while ((*s=='#' || *s=='\n') || (sscanf(s," %d",&option)<1))
263 fgets(s,MAX_STRING,file) ;
265 /* If there is no item to read, then return NULL. */
266 if (nb_items == 0)
267 return NULL ;
269 /* If option is to read them in the file, then we do it and put them into
270 * the array.
272 if (option)
273 { /* Memory allocation. */
274 names = (char **)malloc(nb_items*sizeof(char *)) ;
275 if (names == NULL)
276 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
277 exit(1) ;
279 for (i=0;i<nb_items;i++)
280 { names[i] = (char *)malloc(MAX_NAME*sizeof(char)) ;
281 if (names[i] == NULL)
282 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
283 exit(1) ;
287 do /* Skip the comments, spaces and empty lines... */
288 { c = fgets(s,MAX_STRING,file) ;
289 while ((c != NULL) && isspace(*c) && (*c != '\n'))
290 c++ ;
292 while (c != NULL && (*c == '#' || *c == '\n'));
294 if (c == NULL)
295 { fprintf(stderr, "[CLooG]ERROR: no names in input file.\n") ;
296 exit(1) ;
298 for (i=0;i<nb_items;i++)
299 { /* All names must be on the same line. */
300 while (isspace(*c))
301 c++ ;
302 if (c == NULL || *c == '#' || *c == '\n')
303 { fprintf(stderr, "[CLooG]ERROR: not enough names in input file.\n") ;
304 exit(1) ;
306 /* n is strlen(str). */
307 if (sscanf(c,"%s%n",str,&n) == 0)
308 { fprintf(stderr, "[CLooG]ERROR: no names in input file.\n") ;
309 exit(1) ;
311 sscanf(str,"%s",names[i]) ;
312 c += n ;
315 /* Else we create names automatically. */
316 else
317 names = cloog_names_generate_items(nb_items,prefix,first_item) ;
319 return names ;
323 /******************************************************************************
324 * Processing functions *
325 ******************************************************************************/
329 * cloog_names_malloc function:
330 * This function allocates the memory space for a CloogNames structure and
331 * sets its fields with default values. Then it returns a pointer to the
332 * allocated space.
333 * - November 21th 2005: first version.
335 CloogNames * cloog_names_malloc()
336 { CloogNames * names ;
338 /* Memory allocation for the CloogNames structure. */
339 names = (CloogNames *)malloc(sizeof(CloogNames)) ;
340 if (names == NULL)
341 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
342 exit(1) ;
345 /* We set the various fields with default values. */
346 names->nb_scalars = 0 ;
347 names->nb_scattering = 0 ;
348 names->nb_iterators = 0 ;
349 names->nb_parameters = 0 ;
350 names->scalars = NULL ;
351 names->scattering = NULL ;
352 names->iterators = NULL ;
353 names->parameters = NULL ;
355 return names ;
360 * cloog_domain_alloc function:
361 * This function allocates the memory space for a CloogNames structure and
362 * sets its fields with those given as input. Then it returns a pointer to the
363 * allocated space.
364 * - July 7th 2005: first version.
365 * - September 11th 2005: addition of both scalar and scattering informations.
366 * - November 21th 2005: use of cloog_names_malloc.
368 CloogNames * cloog_names_alloc(
369 nb_scalars, nb_scattering, nb_iterators, nb_parameters,
370 scalars, scattering, iterators, parameters)
371 int nb_scalars, nb_scattering, nb_iterators, nb_parameters ;
372 char ** scalars, ** scattering, ** iterators, ** parameters ;
373 { CloogNames * names ;
375 /* Memory allocation for the CloogNames structure. */
376 names = cloog_names_malloc() ;
378 names->nb_scalars = nb_scalars ;
379 names->nb_scattering = nb_scattering ;
380 names->nb_iterators = nb_iterators ;
381 names->nb_parameters = nb_parameters ;
382 names->scalars = scalars ;
383 names->scattering = scattering ;
384 names->iterators = iterators ;
385 names->parameters = parameters ;
387 return names ;
392 * cloog_names_generate_items function:
393 * This function returns a pointer to an array of strings with entries set
394 * thanks to the function's parameters.
395 * - nb_items will be the number of entries in the string array.
396 * - prefix is the name prefix of each item.
397 * - first_item is the name of the first item (without prefix), the nb_items-1
398 * following items will be the nb_items-1 following letters in ASCII code.
400 * - September 9th 2002 : first version, extracted from cloog_names_generate.
402 char ** cloog_names_generate_items(int nb_items, char * prefix, char first_item)
403 { int i ;
404 char ** names ;
406 if (nb_items == 0)
407 return NULL ;
409 names = (char **)malloc(nb_items*sizeof(char *)) ;
410 if (names == NULL)
411 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
412 exit(1) ;
414 for (i=0;i<nb_items;i++)
415 { names[i] = (char *)malloc(MAX_NAME*sizeof(char)) ;
416 if (names[i] == NULL)
417 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
418 exit(1) ;
420 if (prefix == NULL)
421 sprintf(names[i],"%c",first_item+i) ;
422 else
423 sprintf(names[i],"%s%c",prefix,first_item+i) ;
426 return names ;
431 * cloog_names_generate function:
432 * This function returns a pointer to a CloogNames structure with fields set
433 * thanks to the function's parameters.
434 * - nb_scalars will be the number of scalar dimensions in the structure.
435 * - nb_scattering will be the number of scattering dimensions in the structure.
436 * - nb_iterators will be the number of iterators in the CloogNames structure.
437 * - nb_parameters will be the number of parameters in the CloogNames structure.
438 * - first_s is the name of the first scalar iterator, the nb_scalars-1
439 * following iterators will be the nb_scalars-1 following letters in ASCII.
440 * - first_t is the name of the first scattering iterator, the nb_scattering-1
441 * following iterators will be the nb_scattering-1 following letters in ASCII.
442 * - first_i is the name of the first iterator, the nb_iterators-1 following
443 * iterators will be the nb_iterators-1 following letters in ASCII code.
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_p is the name of the first parameter, the nb_parameters-1 following
447 * parameters will be the nb_parameters-1 following letters in ASCII code.
449 * - July 1st 2002 : first version.
450 * - September 9th 2002 : use of cloog_names_generate_items.
451 * - September 11th 2005 : addition of both scalar and scattering informations.
453 CloogNames * cloog_names_generate(
454 nb_scalars, nb_scattering, nb_iterators, nb_parameters,
455 first_s, first_t, first_i, first_p)
456 int nb_scalars, nb_scattering, nb_iterators, nb_parameters ;
457 char first_s, first_t, first_i, first_p ;
458 { CloogNames * names ;
460 names = (CloogNames *)malloc(sizeof(CloogNames)) ;
461 if (names == NULL)
462 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
463 exit(1) ;
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 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
504 exit(1) ;
506 scalars = (char **)malloc(nb_scalars * sizeof(char *)) ;
507 if (scalars == NULL)
508 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
509 exit(1) ;
512 current_scalar = 0 ;
513 current_scattering = 0 ;
514 for (i=0;i<nb_scattdims;i++)
515 { if (!scaldims[i])
516 { scattering[current_scattering] = names->scattering[i] ;
517 current_scattering ++ ;
519 else
520 { scalars[current_scalar] = names->scattering[i] ;
521 current_scalar ++ ;
525 free(names->scattering) ;
526 names->scattering = scattering ;
527 names->scalars = scalars ;
528 names->nb_scattering = nb_scattering ;
529 names->nb_scalars = nb_scalars ;