README file update
[openscop.git] / source / extensions / irregular.c
blob0666f70761c2609cab3949f94d3decbb02a6ceaf
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
4 **-----------------------------------------------------------------**
5 ** extensions/irregular.c **
6 **-----------------------------------------------------------------**
7 ** First version: 07/12/2010 **
8 **-----------------------------------------------------------------**
11 *****************************************************************************
12 * OpenScop: Structures and formats for polyhedral tools to talk together *
13 *****************************************************************************
14 * ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__, *
15 * / / / // // // // / / / // // / / // / /|,_, *
16 * / / / // // // // / / / // // / / // / / / /\ *
17 * |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/ \ *
18 * | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\ \ /\ *
19 * | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\ *
20 * | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \ \ *
21 * | P |n| l | = | s | t |=| = |d| = | = | = | | |=| o | | \# \ \ *
22 * | H | | y | | e | o | | = |l| | | = | | | | G | | \ \ \ *
23 * | I | | | | e | | | | | | | | | | | | | \ \ \ *
24 * | T | | | | | | | | | | | | | | | | | \ \ \ *
25 * | E | | | | | | | | | | | | | | | | | \ \ \ *
26 * | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | / \* \ \ *
27 * | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/ \ \ / *
28 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
29 * *
30 * Copyright (C) 2008 University Paris-Sud 11 and INRIA *
31 * *
32 * (3-clause BSD license) *
33 * Redistribution and use in source and binary forms, with or without *
34 * modification, are permitted provided that the following conditions *
35 * are met: *
36 * *
37 * 1. Redistributions of source code must retain the above copyright notice, *
38 * this list of conditions and the following disclaimer. *
39 * 2. Redistributions in binary form must reproduce the above copyright *
40 * notice, this list of conditions and the following disclaimer in the *
41 * documentation and/or other materials provided with the distribution. *
42 * 3. The name of the author may not be used to endorse or promote products *
43 * derived from this software without specific prior written permission. *
44 * *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR *
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. *
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, *
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT *
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF *
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
55 * *
56 * OpenScop Library, a library to manipulate OpenScop formats and data *
57 * structures. Written by: *
58 * Cedric Bastoul <Cedric.Bastoul@u-psud.fr> and *
59 * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr> *
60 * *
61 *****************************************************************************/
63 # include <stdlib.h>
64 # include <stdio.h>
65 # include <string.h>
66 # include <openscop/extension.h>
69 /*+***************************************************************************
70 * Structure display function *
71 *****************************************************************************/
74 /**
75 * openscop_irregular_idump function:
76 * this function displays an openscop_irregular_t structure (*irregular) into a
77 * file (file, possibly stdout) in a way that trends to be understandable. It
78 * includes an indentation level (level) in order to work with others
79 * print_structure functions.
80 * \param file The file where the information has to be printed.
81 * \param irregular The irregular structure whose information has to be printed.
82 * \param level Number of spaces before printing, for each line.
84 void
85 openscop_irregular_idump(FILE * file, openscop_irregular_p irregular,
86 int level)
88 int i,j;
90 // Go to the right level.
91 for (j = 0; j < level; j++)
92 fprintf(file, "|\t");
94 if (irregular != NULL)
95 fprintf(file, "+-- openscop_irregular_t\n");
96 else
97 fprintf(file, "+-- NULL irregular\n");
99 if (irregular != NULL)
101 // Go to the right level.
102 for(j = 0; j <= level; j++)
103 fprintf(file, "|\t");
105 // Display the irregular contents.
107 // Print statements
108 for (i=0; i<irregular->nb_statements; i++)
110 fprintf(file, "statement%d's predicats : ", i);
111 for(j=0; j<irregular->nb_predicates[i]; j++)
112 fprintf(file, "%d ", irregular->predicates[i][j]);
113 fprintf(file, "\n");
115 // Print predicats
116 for (i=0; i<irregular->nb_control; i++)
118 fprintf(file, "predicat%d's\niterators : ", i);
119 for(j=0; j<irregular->nb_iterators[i]; j++)
120 fprintf(file, "%s ", irregular->iterators[i][j]);
121 fprintf(file, "\nbody: %s\n", irregular->body[i]);
125 // The last line.
126 for (j = 0; j <= level; j++)
127 fprintf(file, "|\t");
128 fprintf(file, "\n");
133 * openscop_irregular_dump function:
134 * this function prints the content of an openscop_irregular_t structure
135 * (*irregular) into a file (file, possibly stdout).
136 * \param file The file where the information has to be printed.
137 * \param irregular The irregular structure whose information has to be printed.
139 void
140 openscop_irregular_dump(FILE * file, openscop_irregular_p irregular)
142 openscop_irregular_idump(file, irregular, 0);
147 * openscop_irregular_sprint function:
148 * this function prints the content of an openscop_irregular_t structure
149 * (*irregular) into a string (returned) in the OpenScop textual format.
150 * \param irregular The irregular structure whose information has to be printed.
151 * \return A string containing the OpenScop dump of the irregular structure.
153 char *
154 openscop_irregular_sprint(openscop_irregular_p irregular)
156 int high_water_mark = OPENSCOP_MAX_STRING,i,j;
157 char * string = NULL;
158 char * buffer;
160 if (irregular != NULL)
162 string = (char *)malloc(high_water_mark * sizeof(char));
163 buffer = (char *)malloc(OPENSCOP_MAX_STRING * sizeof(char));
164 if ((string == NULL) || (buffer == NULL))
166 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
167 exit(1);
169 string[0] = '\0';
171 // Print the begin tag.
172 sprintf(buffer, OPENSCOP_TAG_IRREGULAR_START);
173 openscop_util_safe_strcat(&string, buffer, &high_water_mark);
175 // Print the content.
176 sprintf(buffer, "\n%d\n", irregular->nb_statements);
177 for(i=0; i<irregular->nb_statements; i++)
179 sprintf(buffer, "%s%d ", buffer, irregular->nb_predicates[i]);
180 for(j=0; j<irregular->nb_predicates[i]; j++)
182 sprintf(buffer, "%s%d ", buffer, irregular->predicates[i][j]);
184 sprintf(buffer, "%s\n", buffer);
186 // Print the predicats.
187 sprintf(buffer, "%s%d\n", buffer, irregular->nb_control);
188 for(i=0; i<irregular->nb_control; i++)
190 sprintf(buffer, "%s%d ", buffer, irregular->nb_iterators[i]);
191 for(j=0; j<irregular->nb_iterators[i];j++)
192 sprintf(buffer, "%s%s ", buffer, irregular->iterators[i][j]);
193 sprintf(buffer, "%s\n%s\n", buffer, irregular->body[i]);
196 openscop_util_safe_strcat(&string, buffer, &high_water_mark);
198 // Print the end tag.
199 sprintf(buffer, OPENSCOP_TAG_IRREGULAR_STOP"\n");
200 openscop_util_safe_strcat(&string, buffer, &high_water_mark);
202 // Keep only the memory space we need.
203 string = (char *)realloc(string, (strlen(string) + 1) * sizeof(char));
204 free(buffer);
207 return string;
211 /*****************************************************************************
212 * Reading function *
213 *****************************************************************************/
216 * openscop_irregular_sread function:
217 * this function reads a irregular structure from a string complying to the
218 * OpenScop textual format and returns a pointer to this irregular structure.
219 * The string should contain only one textual format of a irregular structure.
220 * \param extensions The input string where to find a irregular structure.
221 * \return A pointer to the irregular structure that has been read.
223 openscop_irregular_p
224 openscop_irregular_sread(char * extensions)
226 char * content,*tok;
227 int i,j;
228 openscop_irregular_p irregular;
230 content = openscop_util_tag_content(extensions, OPENSCOP_TAG_IRREGULAR_START,
231 OPENSCOP_TAG_IRREGULAR_STOP);
232 if (content == NULL)
234 fprintf(stderr, "[OpenScop] Info: no irregular optional tag.\n");
235 return NULL;
238 if (strlen(content) > OPENSCOP_MAX_STRING)
240 fprintf(stderr, "[OpenScop] Error: irregular too long.\n");
241 exit(1);
244 irregular = openscop_irregular_malloc();
246 // nb statements
247 tok = strtok(content," \n");
248 irregular->nb_statements = atoi(tok);
249 irregular->predicates = (int**) malloc(sizeof(int*) *
250 irregular->nb_statements);
251 irregular->nb_predicates = (int*) malloc(sizeof(int) *
252 irregular->nb_statements);
253 if (irregular->predicates == NULL)
255 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
256 exit(1);
258 // get predicats
259 for(i=0; i<irregular->nb_statements; i++)
261 // nb conditions
262 tok = strtok(NULL," \n");
263 irregular->nb_predicates[i] = atoi(tok);
264 irregular->predicates[i] = (int* )malloc(sizeof(int)*
265 irregular->nb_predicates[i]);
266 if (irregular->predicates[i] == NULL)
268 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
269 exit(1);
271 for(j=0; j<irregular->nb_predicates[i]; j++){
272 tok = strtok(NULL," \n");
273 irregular->predicates[i][j]=atoi(tok);
276 // Get nb predicat
277 tok = strtok(NULL," \n");
278 irregular->nb_control=atoi(tok);
279 irregular->iterators = (char***) malloc(sizeof(char**) *
280 irregular->nb_control);
281 if (irregular->iterators == NULL)
283 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
284 exit(1);
286 irregular->nb_iterators = (int*) malloc(sizeof(int) *
287 irregular->nb_control);
288 if (irregular->nb_iterators == NULL)
290 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
291 exit(1);
293 irregular->body = (char**) malloc(sizeof(char*) *
294 irregular->nb_control);
295 if (irregular->body == NULL)
297 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
298 exit(1);
300 for(i=0; i<irregular->nb_control; i++)
302 // Get number of iterators
303 tok = strtok(NULL," \n");
304 irregular->nb_iterators[i]=atoi(tok);
305 irregular->iterators[i] = (char**) malloc(sizeof(char*) *
306 irregular->nb_iterators[i]);
307 if (irregular->iterators[i] == NULL)
309 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
310 exit(1);
312 // Get iterators
313 for(j=0; j<irregular->nb_iterators[i]; j++)
314 irregular->iterators[i][j]=strdup(strtok(NULL," \n"));
315 // Get predicat string
316 irregular->body[i] = strdup(strtok(NULL,"\n"));
319 return irregular;
323 /*+***************************************************************************
324 * Memory allocation/deallocation function *
325 *****************************************************************************/
329 * openscop_irregular_malloc function:
330 * This function allocates the memory space for an openscop_irregular_t
331 * structure and sets its fields with default values. Then it returns a
332 * pointer to the allocated space.
333 * \return A pointer to an empty irregular structure with fields set to
334 * default values.
336 openscop_irregular_p
337 openscop_irregular_malloc()
339 openscop_irregular_p irregular;
341 irregular = (openscop_irregular_p)malloc(sizeof(openscop_irregular_t));
342 if (irregular == NULL)
344 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
345 exit(1);
348 irregular->nb_statements = 0;
349 irregular->predicates = NULL;
350 irregular->nb_predicates = NULL;
351 irregular->nb_control = 0;
352 irregular->nb_iterators = NULL;
353 irregular->iterators = NULL;
354 irregular->body = NULL;
356 return irregular;
361 * openscop_irregular_free function:
362 * This function frees the allocated memory for an openscop_irregular_t
363 * structure.
364 * \param irregular The pointer to the irregular structure we want to free.
366 void
367 openscop_irregular_free(openscop_irregular_p irregular)
369 int i,j;
370 if (irregular != NULL)
372 for(i=0; i<irregular->nb_statements; i++)
373 free(irregular->predicates[i]);
375 if(irregular->predicates != NULL)
376 free(irregular->predicates);
378 for(i=0; i<irregular->nb_control; i++)
380 for(j=0; j<irregular->nb_iterators[i]; j++)
381 free(irregular->iterators[i][j]);
382 free(irregular->body[i]);
384 if(irregular->iterators != NULL)
385 free(irregular->iterators);
386 if(irregular->nb_iterators != NULL)
387 free(irregular->nb_iterators);
388 if(irregular->body != NULL)
389 free(irregular->body);
390 if(irregular->nb_predicates != NULL)
391 free(irregular->nb_predicates);
392 free(irregular);
397 /*+***************************************************************************
398 * Processing functions *
399 *****************************************************************************/
403 * openscop_irregular_copy function:
404 * This function builds and returns a "hard copy" (not a pointer copy) of an
405 * openscop_irregular_t data structure.
406 * \param irregular The pointer to the irregular structure we want to copy.
407 * \return A pointer to the copy of the irregular structure.
409 openscop_irregular_p
410 openscop_irregular_copy(openscop_irregular_p irregular)
412 int i,j;
413 openscop_irregular_p copy;
415 if (irregular == NULL)
416 return NULL;
418 copy = openscop_irregular_malloc();
419 if (copy == NULL)
421 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
422 exit(1);
425 copy->nb_statements = irregular->nb_statements;
426 copy->nb_predicates = (int *)malloc(sizeof(int)*copy->nb_statements);
427 if (copy->nb_predicates == NULL)
429 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
430 exit(1);
432 copy->predicates = (int **)malloc(sizeof(int*)*copy->nb_statements);
433 if (copy->predicates == NULL)
435 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
436 exit(1);
438 for(i=0; i<copy->nb_statements; i++)
440 copy->nb_predicates[i]=irregular->nb_predicates[i];
441 copy->predicates[i] = (int *)malloc(sizeof(int)*copy->nb_predicates[i]);
442 if (copy->predicates[i] == NULL)
444 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
445 exit(1);
447 for(j=0; j<copy->nb_predicates[i]; j++)
448 copy->predicates[i][j] = irregular->predicates[i][j];
451 copy->nb_control = irregular->nb_control;
452 copy->nb_iterators = (int *)malloc(sizeof(int)*copy->nb_control);
453 if (copy->nb_predicates == NULL)
455 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
456 exit(1);
458 copy->iterators = (char ***)malloc(sizeof(char**)*copy->nb_control);
459 if (copy->iterators == NULL)
461 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
462 exit(1);
464 copy->body = (char **)malloc(sizeof(char*)*copy->nb_control);
465 if (copy->body == NULL)
467 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
468 exit(1);
470 for(i=0; i<copy->nb_control; i++)
472 copy->nb_iterators[i] = irregular->nb_iterators[i];
473 copy->iterators[i] = (char**)malloc(sizeof(char*)*copy->nb_iterators[i]);
474 if (copy->iterators[i] == NULL)
476 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
477 exit(1);
479 for(j=0;j<copy->nb_iterators[i];j++)
480 copy->iterators[i][j] = strdup(irregular->iterators[i][j]);
481 copy->body[i] = strdup(irregular->body[i]);
484 return copy;
489 * openscop_irregular_equal function:
490 * this function returns true if the two irregular structures are the same
491 * (content-wise), false otherwise. This functions considers two irregular
492 * \param c1 The first irregular structure.
493 * \param c2 The second irregular structure.
494 * \return 1 if c1 and c2 are the same (content-wise), 0 otherwise.
497 openscop_irregular_equal(openscop_irregular_p c1, openscop_irregular_p c2)
499 int i,j,bool = 0;
500 if (c1 == c2)
501 return 1;
503 if (((c1 == NULL) && (c2 != NULL)) || ((c1 != NULL) && (c2 == NULL)))
504 return 0;
506 if(c1->nb_statements != c2->nb_statements||c1->nb_control != c2->nb_control)
507 return 0;
508 i=0;
509 while(bool == 0 && i < c1->nb_statements)
511 bool = c1->nb_predicates[i] != c2->nb_predicates[i] ? 1 : 0;
512 i++;
514 if(bool != 0)
515 return 0;
517 i = 0;
518 while(bool == 0 && i < c1->nb_control)
520 bool += c1->nb_iterators[i] != c2->nb_iterators[i] ? 1 : 0;
521 bool += strcmp(c1->body[i],c2->body[i]);
522 j = 0;
523 while(bool == 0 && j < c1->nb_iterators[i])
525 bool += strcmp(c1->iterators[i][j],c2->iterators[i][j]);
526 j++;
528 i++;
530 if(bool != 0)
531 return 0;
532 return 1;
535 openscop_irregular_p openscop_irregular_add_control(
536 openscop_irregular_p irregular,
537 char** iterators,
538 int nb_iterators,
539 char* body)
541 int i,j;
542 openscop_irregular_p result=openscop_irregular_malloc();
544 result->nb_control = irregular->nb_control+1;
545 result->nb_statements = irregular->nb_statements;
547 result->iterators = (char***)malloc(sizeof(char**)*result->nb_control);
548 result->nb_iterators = (int*)malloc(sizeof(int)*result->nb_control);
549 result->body = (char**)malloc(sizeof(char*)*result->nb_control);
550 //copy
551 for(i=0; i<irregular->nb_control; i++)
553 result->nb_iterators[i] = irregular->nb_iterators[i];
554 result->body[i] = strdup(irregular->body[i]);
555 result->iterators[i] = (char**)malloc(sizeof(char*) *
556 irregular->nb_iterators[i]);
557 for(j=0; j<irregular->nb_iterators[i];j++)
558 result->iterators[i][j] = strdup(irregular->iterators[i][j]);
560 result->nb_predicates = (int*)malloc(sizeof(int)*irregular->nb_statements);
561 result->predicates = (int**)malloc(sizeof(int*)*irregular->nb_statements);
562 for(i=0; i<irregular->nb_statements; i++)
564 result->predicates[i] = (int*)malloc(sizeof(int)*irregular->nb_predicates[i]);
565 result->nb_predicates[i] = irregular->nb_predicates[i];
566 for(j=0; j<irregular->nb_predicates[i]; j++)
567 result->predicates[i][j]=irregular->predicates[i][j];
569 //add
570 result->iterators[irregular->nb_control] = (char**)malloc(sizeof(char*)*nb_iterators);
571 for(i=0; i<nb_iterators; i++)
572 result->iterators[irregular->nb_control][i] = strdup(iterators[i]);
573 result->nb_iterators[irregular->nb_control] = nb_iterators;
574 result->body[irregular->nb_control] = strdup(body);
576 return result;
580 openscop_irregular_p openscop_irregular_add_predicates(
581 openscop_irregular_p irregular,
582 int* predicates,
583 int nb_predicates)
585 int i,j;
586 openscop_irregular_p result=openscop_irregular_malloc();
588 result->nb_control = irregular->nb_control;
589 result->nb_statements = irregular->nb_statements+1;
591 result->iterators = (char***)malloc(sizeof(char**)*irregular->nb_control);
592 result->nb_iterators = (int*)malloc(sizeof(int)*irregular->nb_control);
593 result->body = (char**)malloc(sizeof(char*)*irregular->nb_control);
594 //copy
595 for(i=0; i<irregular->nb_control; i++)
597 result->nb_iterators[i] = irregular->nb_iterators[i];
598 result->body[i] = strdup(irregular->body[i]);
599 result->iterators[i] = (char**)malloc(sizeof(char*) *
600 irregular->nb_iterators[i]);
601 for(j=0; j<irregular->nb_iterators[i];j++)
602 result->iterators[i][j] = irregular->iterators[i][j];
604 result->nb_predicates = (int*)malloc(sizeof(int)*result->nb_statements);
605 result->predicates = (int**)malloc(sizeof(int*)*result->nb_statements);
606 for(i=0; i<irregular->nb_statements; i++)
608 result->predicates[i] = (int*)malloc(sizeof(int)*irregular->nb_predicates[i]);
609 result->nb_predicates[i] = irregular->nb_predicates[i];
610 for(j=0; j<irregular->nb_predicates[i]; j++)
611 result->predicates[i][j]=irregular->predicates[i][j];
613 //add
614 result->predicates[irregular->nb_statements] = (int*)malloc(sizeof(int)*nb_predicates);
615 for(i=0; i<nb_predicates; i++)
616 result->predicates[irregular->nb_statements][i] = predicates[i];
617 result->nb_predicates[irregular->nb_statements] = nb_predicates;
619 return result;