Merge branch 'master' of github.com:periscop/openscop
[openscop.git] / source / body.c
blob9fc3db533409d2bf97e8930f8aec599e8a37b2c1
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
4 **-----------------------------------------------------------------**
5 ** body.c **
6 **-----------------------------------------------------------------**
7 ** First version: 25/06/2011 **
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 *****************************************************************************/
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>
75 /*+***************************************************************************
76 * Structure display functions *
77 *****************************************************************************/
80 /**
81 * osl_body_idump function:
82 * this function displays an osl_body_t structure (*body) into a
83 * file (file, possibly stdout) in a way that trends to be understandable.
84 * It includes an indentation level (level) in order to work with others
85 * dumping functions.
86 * \param[in] file File where informations are printed.
87 * \param[in] body The body whose information has to be printed.
88 * \param[in] level Number of spaces before printing, for each line.
90 void osl_body_idump(FILE * file, osl_body_p body, int level) {
91 int j;
93 // Go to the right level.
94 for (j = 0; j < level; j++)
95 fprintf(file, "|\t");
97 if (body != NULL) {
98 fprintf(file, "+-- osl_body_t\n");
100 // A blank line.
101 for (j = 0; j <= level+1; j++)
102 fprintf(file, "|\t");
103 fprintf(file, "\n");
105 // Print the iterators
106 osl_strings_idump(file, body->iterators, level + 1);
108 // Print the original body expression.
109 osl_strings_idump(file, body->expression, level + 1);
111 else {
112 fprintf(file, "+-- NULL body\n");
115 // The last line.
116 for (j = 0; j <= level; j++)
117 fprintf(file, "|\t");
118 fprintf(file, "\n");
123 * osl_body_dump function:
124 * this function prints the content of an osl_body_t structure
125 * (*body) into a file (file, possibly stdout).
126 * \param[in] file File where informations are printed.
127 * \param[in] body The body whose information has to be printed.
129 void osl_body_dump(FILE * file, osl_body_p body) {
130 osl_body_idump(file, body, 0);
135 * osl_body_print function:
136 * this function prints the content of an osl_body_t structure
137 * (*body) into a file (file, possibly stdout) in the OpenScop format.
138 * \param[in] file File where informations are printed.
139 * \param[in] body The body whose information has to be printed.
141 void osl_body_print(FILE * file, osl_body_p body) {
142 int nb_iterators;
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);
149 if (nb_iterators > 0) {
150 fprintf(file, "\n# List of original iterators\n");
151 osl_strings_print(file, body->iterators);
154 fprintf(file, "\n# Statement body expression\n");
155 osl_strings_print(file, body->expression);
157 else {
158 fprintf(file, "# NULL statement body\n");
164 * osl_body_print_scoplib function:
165 * this function prints the content of an osl_body_t structure
166 * (*body) into a file (file, possibly stdout) in the SCoPLib format.
167 * \param[in] file File where informations are printed.
168 * \param[in] body The body whose information has to be printed.
170 void osl_body_print_scoplib(FILE * file, osl_body_p body) {
171 int nb_iterators;
173 if (body != NULL) {
174 nb_iterators = osl_strings_size(body->iterators);
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");
183 fprintf(file, "# Statement body expression\n");
184 osl_strings_print(file, body->expression);
186 else {
187 fprintf(file, "# NULL statement body\n");
193 * osl_body_sprint function:
194 * this function prints the content of an osl_body_t structure
195 * (*body) into a string (returned) in the OpenScop textual format.
196 * \param[in] body The body structure which has to be printed.
197 * \return A string containing the OpenScop dump of the body structure.
199 char * osl_body_sprint(osl_body_p body) {
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;
206 OSL_malloc(string, char *, high_water_mark * sizeof(char));
207 string[0] = '\0';
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);
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);
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);
228 else {
229 sprintf(buffer, "# NULL body\n");
230 osl_util_safe_strcat(&string, buffer, &high_water_mark);
233 return string;
238 /*****************************************************************************
239 * Reading function *
240 *****************************************************************************/
244 * osl_body_read function:
245 * this function reads a body structure from a string complying to the
246 * OpenScop textual format and returns a pointer to this body structure.
247 * The input string should only contain the body this function
248 * has to read (comments at the end of the line are accepted). The input
249 * parameter is updated to the position in the input string this function
250 * reach right after reading the strings structure.
251 * \param[in,out] input The input string where to find a body structure.
252 * Updated to the position after what has been read.
253 * \return A pointer to the body structure that has been read.
255 osl_body_p osl_body_sread(char ** input) {
256 osl_body_p body = NULL;
257 char * expression;
258 int nb_iterators;
260 if (input) {
261 body = osl_body_malloc();
263 // Read the number of iterators.
264 nb_iterators = osl_util_read_int(NULL, input);
266 // Read the iterator strings if any.
267 if (nb_iterators > 0) {
268 body->iterators = osl_strings_sread(input);
270 else {
271 body->iterators = osl_strings_malloc();
274 // Read the body:
275 expression = osl_util_read_line(NULL, input);
277 // Insert the body.
278 body->expression = osl_strings_encapsulate(expression);
281 return body;
285 /*+***************************************************************************
286 * Memory allocation/deallocation functions *
287 *****************************************************************************/
291 * osl_body_malloc function:
292 * this function allocates the memory space for an osl_body_t
293 * structure and sets its fields with default values. Then it returns a pointer
294 * to the allocated space.
295 * \return A pointer to an empty body with fields set to default values.
297 osl_body_p osl_body_malloc() {
298 osl_body_p body;
300 OSL_malloc(body, osl_body_p, sizeof(osl_body_t));
301 body->iterators = NULL;
302 body->expression = NULL;
304 return body;
309 * osl_body_free function:
310 * this function frees the allocated memory for an osl_body_t
311 * structure.
312 * \param[in,out] body The pointer to the body we want to free.
314 void osl_body_free(osl_body_p body) {
316 if (body != NULL) {
317 osl_strings_free(body->iterators);
318 osl_strings_free(body->expression);
319 free(body);
324 /*+***************************************************************************
325 * Processing functions *
326 *****************************************************************************/
330 * osl_body_clone function:
331 * this functions builds and returns a "hard copy" (not a pointer copy) of an
332 * osl_body_t data structure provided as parameter. However, let us
333 * recall here that non-string elements are untouched by the OpenScop Library.
334 * \param[in] body The pointer to the body we want to copy.
335 * \return A pointer to the full copy of the body provided as parameter.
337 osl_body_p osl_body_clone(osl_body_p body) {
338 osl_body_p copy = NULL;
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);
346 return copy;
351 * osl_body_equal function:
352 * this function returns true if the two bodies are the same, false
353 * otherwise (the usr field is not tested). However, let us
354 * recall here that non-string elements are untouched by the OpenScop Library.
355 * \param[in] b1 The first body.
356 * \param[in] b2 The second body.
357 * \return 1 if b1 and b2 are the same (content-wise), 0 otherwise.
359 int osl_body_equal(osl_body_p b1, osl_body_p b2) {
361 if (b1 == b2)
362 return 1;
364 if (((b1 != NULL) && (b2 == NULL)) ||
365 ((b1 == NULL) && (b2 != NULL))) {
366 OSL_info("bodies are not the same");
367 return 0;
370 if (!osl_strings_equal(b1->iterators, b2->iterators)) {
371 OSL_info("body iterators are not the same");
372 return 0;
375 if (!osl_strings_equal(b1->expression, b2->expression)) {
376 OSL_info("body expressions are not the same");
377 return 0;
380 return 1;
385 * osl_body_interface function:
386 * this function creates an interface structure corresponding to the body
387 * structure and returns it).
388 * \return An interface structure for the body structure.
390 osl_interface_p osl_body_interface() {
391 osl_interface_p interface = osl_interface_malloc();
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;
402 return interface;