Remove names_t and replace with parameter list only
[openscop.git] / source / body.c
blob0652716a96c6a6a80dc2b29fb6e238beb002daed
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 <openscop/body.h>
71 /*+***************************************************************************
72 * Structure display functions *
73 *****************************************************************************/
76 /**
77 * openscop_body_idump function:
78 * this function displays an openscop_body_t structure (*body) into a
79 * file (file, possibly stdout) in a way that trends to be understandable.
80 * It includes an indentation level (level) in order to work with others
81 * dumping functions.
82 * \param[in] file File where informations are printed.
83 * \param[in] body The body whose information has to be printed.
84 * \param[in] level Number of spaces before printing, for each line.
86 void openscop_body_idump(FILE * file, openscop_body_p body, int level) {
87 int i, j;
89 // Go to the right level.
90 for (j = 0; j < level; j++)
91 fprintf(file, "|\t");
93 if (body != NULL) {
94 fprintf(file, "+-- openscop_body_t\n");
96 // A blank line.
97 for (j = 0; j <= level+1; j++)
98 fprintf(file, "|\t");
99 fprintf(file, "\n");
101 if (body->type == OPENSCOP_TYPE_STRING) {
102 // Print the original iterator names.
103 openscop_util_strings_idump(file, (char **)body->iterator,
104 body->nb_iterators, level,
105 "original iterator strings");
107 // Print the original body expression.
108 for (i = 0; i <= level; i++)
109 fprintf(file, "|\t");
110 if (body->expression != NULL)
111 fprintf(file, "+-- Original expression: %s\n", (char *)body->expression);
112 else
113 fprintf(file, "+-- No original body expression\n");
115 else {
116 for (i = 0; i <= level; i++)
117 fprintf(file, "|\t");
118 fprintf(file, "+-- Non textual information\n");
121 else {
122 fprintf(file, "+-- NULL body\n");
125 // The last line.
126 for (j = 0; j <= level+1; j++)
127 fprintf(file, "|\t");
128 fprintf(file, "\n");
133 * openscop_body_dump function:
134 * This function prints the content of a openscop_body_t structure
135 * (*body) into a file (file, possibly stdout).
136 * \param file[in] File where informations are printed.
137 * \param body[in] The body whose information has to be printed.
139 void openscop_body_dump(FILE * file, openscop_body_p body) {
140 openscop_body_idump(file, body, 0);
145 * openscop_body_print function:
146 * This function prints the content of a openscop_body_t structure
147 * (*body) into a file (file, possibly stdout) in the OpenScop format.
148 * \param file[in] File where informations are printed.
149 * \param body[in] The body whose information has to be printed.
151 void openscop_body_print(FILE * file, openscop_body_p body) {
152 int i;
154 if ((body != NULL) && (body->type == OPENSCOP_TYPE_STRING)) {
155 fprintf(file, "# Statement body is provided\n");
156 fprintf(file, "1\n");
157 if (body->nb_iterators > 0) {
158 fprintf(file, "# Original iterator names\n");
159 for (i = 0; i < body->nb_iterators; i++)
160 fprintf(file, "%s ", (char *)body->iterator[i]);
161 fprintf(file, "\n");
163 else {
164 fprintf(file, "# No original iterator names\n");
166 fprintf(file, "# Statement body expression\n");
167 fprintf(file, "%s\n", (char *)body->expression);
169 else {
170 fprintf(file, "# Statement body is not provided\n");
171 fprintf(file, "0\n");
176 /*****************************************************************************
177 * Reading function *
178 *****************************************************************************/
182 * openscop_body_read function:
183 * This function reads a openscop_body_t structure from an input stream
184 * (possibly stdin).
185 * \param[in] file The input stream.
186 * \param[in] nb_iterators The number of iterators.
187 * \return A pointer to the body structure that has been read.
189 openscop_body_p openscop_body_read(FILE * file, int nb_iterators) {
190 openscop_body_p body = NULL;
191 char buff[OPENSCOP_MAX_STRING], * start, * end;
192 int nb_strings;
194 if (file) {
195 // Read the body information, if any.
196 if (openscop_util_read_int(file, NULL) > 0) {
197 body = openscop_body_malloc();
198 body->type = OPENSCOP_TYPE_STRING;
200 // Read the original iterator names.
201 if (nb_iterators > 0) {
202 body->iterator = (void *)openscop_util_strings_read(file, &nb_strings);
203 body->nb_iterators = nb_strings;
204 if (nb_iterators != nb_strings) {
205 fprintf(stderr, "[OpenScop] Warning: not the expected number of "
206 "original iterators.\n");
210 // Read the body:
211 // - Skip blank/commented lines and spaces before the body.
212 start = openscop_util_skip_blank_and_comments(file, buff);
214 // - Remove the comments after the body.
215 end = start;
216 while ((*end != '#') && (*end != '\n'))
217 end++;
218 *end = '\0';
220 // - Copy the body.
221 body->expression = strdup(start);
225 return body;
229 /*+***************************************************************************
230 * Memory allocation/deallocation functions *
231 *****************************************************************************/
235 * openscop_body_malloc function:
236 * This function allocates the memory space for a openscop_body_t
237 * structure and sets its fields with default values. Then it returns a pointer
238 * to the allocated space.
239 * \return A pointer to an empty body with fields set to default values.
241 openscop_body_p openscop_body_malloc() {
242 openscop_body_p body;
244 body = (openscop_body_p)malloc(sizeof(openscop_body_t));
245 if (body == NULL) {
246 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
247 exit(1);
250 body->type = OPENSCOP_UNDEFINED;
251 body->nb_iterators = 0;
252 body->iterator = NULL;
253 body->expression = NULL;
255 return body;
260 * openscop_body_free function:
261 * This function frees the allocated memory for a openscop_body_t
262 * structure.
263 * \param[in,out] body The pointer to the body we want to free.
265 void openscop_body_free(openscop_body_p body) {
267 if (body != NULL) {
268 openscop_util_strings_free((char**)(body->iterator), body->nb_iterators);
269 if (body->expression != NULL)
270 free(body->expression);
272 free(body);
277 /*+***************************************************************************
278 * Processing functions *
279 *****************************************************************************/
283 * openscop_body_copy function:
284 * This functions builds and returns a "hard copy" (not a pointer copy) of an
285 * openscop_body_t data structure provided as parameter. However, let us
286 * recall here that non-string elements are untouched by the OpenScop Library.
287 * \param[in] body The pointer to the body we want to copy.
288 * \return A pointer to the full copy of the body provided as parameter.
290 openscop_body_p openscop_body_copy(openscop_body_p body) {
291 openscop_body_p copy = NULL;
293 if (body != NULL) {
294 copy = openscop_body_malloc();
295 copy->type = body->type;
296 copy->nb_iterators = body->nb_iterators;
297 if (body->type == OPENSCOP_TYPE_STRING) {
298 copy->iterator = (void*)openscop_util_strings_copy(
299 (char**)body->iterator, body->nb_iterators);
300 copy->expression = strdup(body->expression);
304 return copy;
309 * openscop_body_equal function:
310 * This function returns true if the two bodies are the same, false
311 * otherwise (the usr field is not tested). However, let us
312 * recall here that non-string elements are untouched by the OpenScop Library.
313 * \param b1 The first body.
314 * \param b2 The second body.
315 * \return 1 if b1 and b2 are the same (content-wise), 0 otherwise.
317 int openscop_body_equal(openscop_body_p b1, openscop_body_p b2) {
319 if (b1 == b2)
320 return 1;
322 if (((b1 != NULL) && (b2 == NULL)) ||
323 ((b1 == NULL) && (b2 != NULL))) {
324 fprintf(stderr, "[OpenScop] info: bodies are not the same.\n");
325 return 0;
328 if (b1->type != b2->type) {
329 fprintf(stderr, "[OpenScop] info: body types are not the same.\n");
330 return 0;
333 if (b1->type == OPENSCOP_TYPE_STRING) {
334 if (!openscop_util_strings_equal((char**)b1->iterator, b1->nb_iterators,
335 (char**)b2->iterator, b2->nb_iterators)) {
336 fprintf(stderr, "[OpenScop] info: body iterators are not the same.\n");
337 return 0;
340 if (((b1->expression == NULL) && (b2->expression != NULL)) ||
341 ((b1->expression != NULL) && (b2->expression == NULL)) ||
342 ((b1->expression != NULL) &&
343 (b2->expression != NULL) &&
344 (b1->type == OPENSCOP_TYPE_STRING) &&
345 (b2->type == OPENSCOP_TYPE_STRING) &&
346 (strcmp(b1->expression, b2->expression) != 0))) {
347 fprintf(stderr,"[OpenScop] info: body expressions are not the same.\n");
348 return 0;
352 return 1;
357 * openscop_body_integrity_check function:
358 * This function checks that a body is "well formed" according to some
359 * expected properties (setting an expected value to OPENSCOP_UNDEFINED means
360 * that we do not expect a specific value). It returns 0 if the check failed
361 * or 1 if no problem has been detected.
362 * \param[in] body The body we want to check.
363 * \param[in] expected_nb_iterators Expected number of iterators.
364 * \return 0 if the integrity check fails, 1 otherwise.
366 int openscop_body_integrity_check(openscop_body_p body,
367 int expected_nb_iterators) {
369 if ((body != NULL) &&
370 (expected_nb_iterators != OPENSCOP_UNDEFINED) &&
371 (expected_nb_iterators != body->nb_iterators)) {
372 fprintf(stderr, "[OpenScop] Warning: unexpected #original iterators.\n");
373 return 0;
376 return 1;