OpenScop  0.9.0
body.c
Go to the documentation of this file.
1 
2  /*+-----------------------------------------------------------------**
3  ** OpenScop Library **
4  **-----------------------------------------------------------------**
5  ** body.c **
6  **-----------------------------------------------------------------**
7  ** First version: 25/06/2011 **
8  **-----------------------------------------------------------------**
9 
10 
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  *****************************************************************************/
62 
63 
64 # include <stdlib.h>
65 # include <stdio.h>
66 # include <string.h>
67 # include <ctype.h>
68 # include <osl/macros.h>
69 # include <osl/util.h>
70 # include <osl/strings.h>
71 # include <osl/interface.h>
72 # include <osl/body.h>
73 
74 
75 /*+***************************************************************************
76  * Structure display functions *
77  *****************************************************************************/
78 
79 
90 void osl_body_idump(FILE * file, osl_body_p body, int level) {
91  int j;
92 
93  // Go to the right level.
94  for (j = 0; j < level; j++)
95  fprintf(file, "|\t");
96 
97  if (body != NULL) {
98  fprintf(file, "+-- osl_body_t\n");
99 
100  // A blank line.
101  for (j = 0; j <= level+1; j++)
102  fprintf(file, "|\t");
103  fprintf(file, "\n");
104 
105  // Print the iterators
106  osl_strings_idump(file, body->iterators, level + 1);
107 
108  // Print the original body expression.
109  osl_strings_idump(file, body->expression, level + 1);
110  }
111  else {
112  fprintf(file, "+-- NULL body\n");
113  }
114 
115  // The last line.
116  for (j = 0; j <= level; j++)
117  fprintf(file, "|\t");
118  fprintf(file, "\n");
119 }
120 
121 
129 void osl_body_dump(FILE * file, osl_body_p body) {
130  osl_body_idump(file, body, 0);
131 }
132 
133 
141 void osl_body_print(FILE * file, osl_body_p body) {
142  int nb_iterators;
143 
144  if (body != NULL) {
145  nb_iterators = osl_strings_size(body->iterators);
146  fprintf(file, "# Number of original iterators\n");
147  fprintf(file, "%d\n", nb_iterators);
148 
149  if (nb_iterators > 0) {
150  fprintf(file, "\n# List of original iterators\n");
151  osl_strings_print(file, body->iterators);
152  }
153 
154  fprintf(file, "\n# Statement body expression\n");
155  osl_strings_print(file, body->expression);
156  }
157  else {
158  fprintf(file, "# NULL statement body\n");
159  }
160 }
161 
162 
170 void osl_body_print_scoplib(FILE * file, osl_body_p body) {
171  int nb_iterators;
172 
173  if (body != NULL) {
174  nb_iterators = osl_strings_size(body->iterators);
175 
176  if (nb_iterators > 0) {
177  fprintf(file, "# List of original iterators\n");
178  osl_strings_print(file, body->iterators);
179  } else {
180  fprintf(file, "fakeiter\n");
181  }
182 
183  fprintf(file, "# Statement body expression\n");
184  osl_strings_print(file, body->expression);
185  }
186  else {
187  fprintf(file, "# NULL statement body\n");
188  }
189 }
190 
191 
200  int nb_iterators;
201  int high_water_mark = OSL_MAX_STRING;
202  char * string = NULL;
203  char buffer[OSL_MAX_STRING];
204  char * iterators, * expression;
205 
206  OSL_malloc(string, char *, high_water_mark * sizeof(char));
207  string[0] = '\0';
208 
209  if (body != NULL) {
210  nb_iterators = osl_strings_size(body->iterators);
211  sprintf(buffer, "# Number of original iterators\n%d\n", nb_iterators);
212  osl_util_safe_strcat(&string, buffer, &high_water_mark);
213 
214  if (nb_iterators > 0) {
215  sprintf(buffer, "# List of original iterators\n");
216  osl_util_safe_strcat(&string, buffer, &high_water_mark);
217  iterators = osl_strings_sprint(body->iterators);
218  osl_util_safe_strcat(&string, iterators, &high_water_mark);
219  free(iterators);
220  }
221 
222  sprintf(buffer, "# Statement body expression\n");
223  osl_util_safe_strcat(&string, buffer, &high_water_mark);
224  expression = osl_strings_sprint(body->expression);
225  osl_util_safe_strcat(&string, expression, &high_water_mark);
226  free(expression);
227  }
228  else {
229  sprintf(buffer, "# NULL body\n");
230  osl_util_safe_strcat(&string, buffer, &high_water_mark);
231  }
232 
233  return string;
234 }
235 
236 
237 
238 /*****************************************************************************
239  * Reading function *
240  *****************************************************************************/
241 
242 
255 osl_body_p osl_body_sread(char ** input) {
256  osl_body_p body = NULL;
257  char * expression;
258  int nb_iterators;
259 
260  if (input) {
261  body = osl_body_malloc();
262 
263  // Read the number of iterators.
264  nb_iterators = osl_util_read_int(NULL, input);
265 
266  // Read the iterator strings if any.
267  if (nb_iterators > 0) {
268  body->iterators = osl_strings_sread(input);
269  }
270  else {
271  body->iterators = osl_strings_malloc();
272  }
273 
274  // Read the body:
275  expression = osl_util_read_line(NULL, input);
276 
277  // Insert the body.
278  body->expression = osl_strings_encapsulate(expression);
279  }
280 
281  return body;
282 }
283 
284 
285 /*+***************************************************************************
286  * Memory allocation/deallocation functions *
287  *****************************************************************************/
288 
289 
298  osl_body_p body;
299 
300  OSL_malloc(body, osl_body_p, sizeof(osl_body_t));
301  body->iterators = NULL;
302  body->expression = NULL;
303 
304  return body;
305 }
306 
307 
315 
316  if (body != NULL) {
319  free(body);
320  }
321 }
322 
323 
324 /*+***************************************************************************
325  * Processing functions *
326  *****************************************************************************/
327 
328 
338  osl_body_p copy = NULL;
339 
340  if (body != NULL) {
341  copy = osl_body_malloc();
342  copy->iterators = osl_strings_clone(body->iterators);
343  copy->expression = osl_strings_clone(body->expression);
344  }
345 
346  return copy;
347 }
348 
349 
360 
361  if (b1 == b2)
362  return 1;
363 
364  if (((b1 != NULL) && (b2 == NULL)) ||
365  ((b1 == NULL) && (b2 != NULL))) {
366  OSL_info("bodies are not the same");
367  return 0;
368  }
369 
370  if (!osl_strings_equal(b1->iterators, b2->iterators)) {
371  OSL_info("body iterators are not the same");
372  return 0;
373  }
374 
375  if (!osl_strings_equal(b1->expression, b2->expression)) {
376  OSL_info("body expressions are not the same");
377  return 0;
378  }
379 
380  return 1;
381 }
382 
383 
392 
393  OSL_strdup(interface->URI, OSL_URI_BODY);
394  interface->idump = (osl_idump_f)osl_body_idump;
395  interface->sprint = (osl_sprint_f)osl_body_sprint;
396  interface->sread = (osl_sread_f)osl_body_sread;
397  interface->malloc = (osl_malloc_f)osl_body_malloc;
398  interface->free = (osl_free_f)osl_body_free;
399  interface->clone = (osl_clone_f)osl_body_clone;
400  interface->equal = (osl_equal_f)osl_body_equal;
401 
402  return interface;
403 }
404 
osl_strings_p osl_strings_malloc()
Definition: strings.c:283
void *(* osl_clone_f)(void *)
Definition: interface.h:80
osl_strings_p osl_strings_clone(osl_strings_p strings)
Definition: strings.c:328
osl_strings_p expression
Definition: body.h:87
void osl_body_print(FILE *file, osl_body_p body)
Definition: body.c:141
char * osl_body_sprint(osl_body_p body)
Definition: body.c:199
osl_interface_p osl_interface_malloc()
Definition: interface.c:212
osl_body_p osl_body_malloc()
Definition: body.c:297
void osl_body_print_scoplib(FILE *file, osl_body_p body)
Definition: body.c:170
osl_interface_p osl_body_interface()
Definition: body.c:390
void *(* osl_sread_f)(char **)
Definition: interface.h:77
osl_strings_p iterators
Definition: body.h:86
osl_strings_p osl_strings_sread(char **input)
Definition: strings.c:194
void osl_strings_idump(FILE *file, osl_strings_p strings, int level)
Definition: strings.c:89
void osl_util_safe_strcat(char **dst, char *src, int *hwm)
Definition: util.c:483
size_t osl_strings_size(osl_const_strings_const_p strings)
Definition: strings.c:413
void(* osl_idump_f)(FILE *, void *, int)
Definition: interface.h:75
osl_body_p osl_body_sread(char **input)
Definition: body.c:255
void osl_body_idump(FILE *file, osl_body_p body, int level)
Definition: body.c:90
osl_strings_p osl_strings_encapsulate(char *string)
Definition: strings.c:433
int osl_util_read_int(FILE *file, char **str)
Definition: util.c:140
char *(* osl_sprint_f)(void *)
Definition: interface.h:76
void osl_strings_print(FILE *file, osl_strings_p strings)
Definition: strings.c:166
char * osl_strings_sprint(osl_strings_p strings)
Definition: strings.c:131
void *(* osl_malloc_f)()
Definition: interface.h:78
void osl_strings_free(osl_strings_p strings)
Definition: strings.c:299
int osl_body_equal(osl_body_p b1, osl_body_p b2)
Definition: body.c:359
int osl_strings_equal(osl_strings_p s1, osl_strings_p s2)
Definition: strings.c:387
int(* osl_equal_f)(void *, void *)
Definition: interface.h:81
char * osl_util_read_line(FILE *file, char **str)
Definition: util.c:226
osl_body_p osl_body_clone(osl_body_p body)
Definition: body.c:337
void osl_body_free(osl_body_p body)
Definition: body.c:314
Definition: body.h:85
void osl_body_dump(FILE *file, osl_body_p body)
Definition: body.c:129
void(* osl_free_f)(void *)
Definition: interface.h:79