Hybrid precision (32, 64, GMP)
[openscop.git] / source / body.c
blob64c6b535fb89af02ab5052a5de94a82e0dab9dad
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_strings_idump(file, (char **)body->iterators, level,
104 "original iterator strings");
106 // Print the original body expression.
107 for (i = 0; i <= level; i++)
108 fprintf(file, "|\t");
109 if (body->expression != NULL)
110 fprintf(file, "+-- Original expression: %s\n",(char*)body->expression);
111 else
112 fprintf(file, "+-- No original body expression\n");
114 else {
115 for (i = 0; i <= level; i++)
116 fprintf(file, "|\t");
117 fprintf(file, "+-- Non textual information\n");
120 else {
121 fprintf(file, "+-- NULL body\n");
124 // The last line.
125 for (j = 0; j <= level+1; j++)
126 fprintf(file, "|\t");
127 fprintf(file, "\n");
132 * openscop_body_dump function:
133 * this function prints the content of an openscop_body_t structure
134 * (*body) into a file (file, possibly stdout).
135 * \param file[in] File where informations are printed.
136 * \param body[in] The body whose information has to be printed.
138 void openscop_body_dump(FILE * file, openscop_body_p body) {
139 openscop_body_idump(file, body, 0);
144 * openscop_body_print function:
145 * this function prints the content of an openscop_body_t structure
146 * (*body) into a file (file, possibly stdout) in the OpenScop format.
147 * \param file[in] File where informations are printed.
148 * \param body[in] The body whose information has to be printed.
150 void openscop_body_print(FILE * file, openscop_body_p body) {
152 if ((body != NULL) && (body->type == OPENSCOP_TYPE_STRING)) {
153 fprintf(file, "# Statement body is provided\n");
154 fprintf(file, "1\n");
155 openscop_strings_print(file, (char **)body->iterators, 0, 1,
156 "original iterators");
157 fprintf(file, "# Statement body expression\n");
158 fprintf(file, "%s\n", (char *)body->expression);
160 else {
161 fprintf(file, "# Statement body is not provided\n");
162 fprintf(file, "0\n");
167 /*****************************************************************************
168 * Reading function *
169 *****************************************************************************/
173 * openscop_body_read function:
174 * this function reads an openscop_body_t structure from an input stream
175 * (possibly stdin).
176 * \param[in] file The input stream.
177 * \param[in] nb_iterators The number of iterators.
178 * \return A pointer to the body structure that has been read.
180 openscop_body_p openscop_body_read(FILE * file, int nb_iterators) {
181 openscop_body_p body = NULL;
182 char buff[OPENSCOP_MAX_STRING], * start, * end;
184 if (file) {
185 // Read the body information, if any.
186 if (openscop_util_read_int(file, NULL) > 0) {
187 body = openscop_body_malloc();
188 body->type = OPENSCOP_TYPE_STRING;
190 // Read the original iterator names.
191 if (nb_iterators > 0) {
192 body->iterators = (void *)openscop_strings_read(file);
193 if (nb_iterators != openscop_strings_size((char **)body->iterators))
194 OPENSCOP_warning("not the expected number of original iterators");
197 // Read the body:
198 // - Skip blank/commented lines and spaces before the body.
199 start = openscop_util_skip_blank_and_comments(file, buff);
201 // - Remove the comments after the body.
202 end = start;
203 while ((*end != '#') && (*end != '\n'))
204 end++;
205 *end = '\0';
207 // - Copy the body.
208 body->expression = strdup(start);
212 return body;
216 /*+***************************************************************************
217 * Memory allocation/deallocation functions *
218 *****************************************************************************/
222 * openscop_body_malloc function:
223 * this function allocates the memory space for an openscop_body_t
224 * structure and sets its fields with default values. Then it returns a pointer
225 * to the allocated space.
226 * \return A pointer to an empty body with fields set to default values.
228 openscop_body_p openscop_body_malloc() {
229 openscop_body_p body;
231 OPENSCOP_malloc(body, openscop_body_p, sizeof(openscop_body_t));
232 body->type = OPENSCOP_UNDEFINED;
233 body->iterators = NULL;
234 body->expression = NULL;
236 return body;
241 * openscop_body_free function:
242 * this function frees the allocated memory for an openscop_body_t
243 * structure.
244 * \param[in,out] body The pointer to the body we want to free.
246 void openscop_body_free(openscop_body_p body) {
248 if (body != NULL) {
249 openscop_strings_free((char **)body->iterators);
250 if (body->expression != NULL)
251 free(body->expression);
253 free(body);
258 /*+***************************************************************************
259 * Processing functions *
260 *****************************************************************************/
264 * openscop_body_clone function:
265 * this functions builds and returns a "hard copy" (not a pointer copy) of an
266 * openscop_body_t data structure provided as parameter. However, let us
267 * recall here that non-string elements are untouched by the OpenScop Library.
268 * \param[in] body The pointer to the body we want to copy.
269 * \return A pointer to the full copy of the body provided as parameter.
271 openscop_body_p openscop_body_clone(openscop_body_p body) {
272 openscop_body_p copy = NULL;
274 if (body != NULL) {
275 copy = openscop_body_malloc();
276 copy->type = body->type;
277 if (body->type == OPENSCOP_TYPE_STRING) {
278 copy->iterators = (void *)openscop_strings_clone(
279 (char **)body->iterators);
280 copy->expression = strdup(body->expression);
284 return copy;
289 * openscop_body_equal function:
290 * this function returns true if the two bodies are the same, false
291 * otherwise (the usr field is not tested). However, let us
292 * recall here that non-string elements are untouched by the OpenScop Library.
293 * \param b1 The first body.
294 * \param b2 The second body.
295 * \return 1 if b1 and b2 are the same (content-wise), 0 otherwise.
297 int openscop_body_equal(openscop_body_p b1, openscop_body_p b2) {
299 if (b1 == b2)
300 return 1;
302 if (((b1 != NULL) && (b2 == NULL)) ||
303 ((b1 == NULL) && (b2 != NULL))) {
304 OPENSCOP_info("bodies are not the same");
305 return 0;
308 if (b1->type != b2->type) {
309 OPENSCOP_info("body types are not the same");
310 return 0;
313 if (b1->type == OPENSCOP_TYPE_STRING) {
314 if (!openscop_strings_equal((char **)b1->iterators,
315 (char **)b2->iterators)) {
316 OPENSCOP_info("body iterators are not the same");
317 return 0;
320 if (((b1->expression == NULL) && (b2->expression != NULL)) ||
321 ((b1->expression != NULL) && (b2->expression == NULL)) ||
322 ((b1->expression != NULL) &&
323 (b2->expression != NULL) &&
324 (b1->type == OPENSCOP_TYPE_STRING) &&
325 (b2->type == OPENSCOP_TYPE_STRING) &&
326 (strcmp(b1->expression, b2->expression) != 0))) {
327 OPENSCOP_info("body expressions are not the same");
328 return 0;
332 return 1;
337 * openscop_body_integrity_check function:
338 * this function checks that a body is "well formed" according to some
339 * expected properties (setting an expected value to OPENSCOP_UNDEFINED means
340 * that we do not expect a specific value). It returns 0 if the check failed
341 * or 1 if no problem has been detected.
342 * \param[in] body The body we want to check.
343 * \param[in] expected_nb_iterators Expected number of iterators.
344 * \return 0 if the integrity check fails, 1 otherwise.
346 int openscop_body_integrity_check(openscop_body_p body,
347 int expected_nb_iterators) {
349 if ((body != NULL) &&
350 (expected_nb_iterators != OPENSCOP_UNDEFINED) &&
351 (expected_nb_iterators !=
352 openscop_strings_size((char **)body->iterators))) {
353 OPENSCOP_warning("unexpected number of original iterators");
354 return 0;
357 return 1;