OpenScop 0.8.1

body.c

Go to the documentation of this file.
00001 
00002     /*+-----------------------------------------------------------------**
00003      **                       OpenScop Library                          **
00004      **-----------------------------------------------------------------**
00005      **                            body.c                               **
00006      **-----------------------------------------------------------------**
00007      **                   First version: 25/06/2011                     **
00008      **-----------------------------------------------------------------**
00009 
00010  
00011  *****************************************************************************
00012  * OpenScop: Structures and formats for polyhedral tools to talk together    *
00013  *****************************************************************************
00014  *    ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__,                *
00015  *    /   / /  //  //  //  // /   / /  //  //   / /  // /  /|,_,             *
00016  *   /   / /  //  //  //  // /   / /  //  //   / /  // /  / / /\             *
00017  *  |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/  \            *
00018  *  | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\  \ /\           *
00019  *  | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\          *
00020  *  | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \  \         *
00021  *  | P |n| l | = | s | t |=| = |d| = | = | = | |   |=| o | | \# \  \        *
00022  *  | H | | y |   | e | o | | = |l|   |   | = | |   | | G | |  \  \  \       *
00023  *  | I | |   |   | e |   | |   | |   |   |   | |   | |   | |   \  \  \      *
00024  *  | T | |   |   |   |   | |   | |   |   |   | |   | |   | |    \  \  \     *
00025  *  | E | |   |   |   |   | |   | |   |   |   | |   | |   | |     \  \  \    *
00026  *  | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | /      \* \  \   *
00027  *  | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/        \  \ /   *
00028  *  '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---'          '--'    *
00029  *                                                                           *
00030  * Copyright (C) 2008 University Paris-Sud 11 and INRIA                      *
00031  *                                                                           *
00032  * (3-clause BSD license)                                                    *
00033  * Redistribution and use in source  and binary forms, with or without       *
00034  * modification, are permitted provided that the following conditions        *
00035  * are met:                                                                  *
00036  *                                                                           *
00037  * 1. Redistributions of source code must retain the above copyright notice, *
00038  *    this list of conditions and the following disclaimer.                  *
00039  * 2. Redistributions in binary form must reproduce the above copyright      *
00040  *    notice, this list of conditions and the following disclaimer in the    *
00041  *    documentation and/or other materials provided with the distribution.   *
00042  * 3. The name of the author may not be used to endorse or promote products  *
00043  *    derived from this software without specific prior written permission.  *
00044  *                                                                           *
00045  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR      *
00046  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
00047  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.   *
00048  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,          *
00049  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT  *
00050  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
00051  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY     *
00052  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT       *
00053  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF  *
00054  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.         *
00055  *                                                                           *
00056  * OpenScop Library, a library to manipulate OpenScop formats and data       *
00057  * structures. Written by:                                                   *
00058  * Cedric Bastoul     <Cedric.Bastoul@u-psud.fr> and                         *
00059  * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr>                          *
00060  *                                                                           *
00061  *****************************************************************************/
00062 
00063 
00064 # include <stdlib.h>
00065 # include <stdio.h>
00066 # include <string.h>
00067 # include <ctype.h>
00068 # include <osl/macros.h>
00069 # include <osl/util.h>
00070 # include <osl/strings.h>
00071 # include <osl/interface.h>
00072 # include <osl/body.h>
00073 
00074 
00075 /*+***************************************************************************
00076  *                         Structure display functions                       *
00077  *****************************************************************************/
00078 
00079 
00090 void osl_body_idump(FILE * file, osl_body_p body, int level) {
00091   int j;
00092 
00093   // Go to the right level.
00094   for (j = 0; j < level; j++)
00095     fprintf(file, "|\t");
00096 
00097   if (body != NULL) {
00098     fprintf(file, "+-- osl_body_t\n");
00099 
00100     // A blank line.
00101     for (j = 0; j <= level+1; j++)
00102       fprintf(file, "|\t");
00103     fprintf(file, "\n");
00104 
00105     // Print the iterators
00106     osl_strings_idump(file, body->iterators, level + 1);
00107 
00108     // Print the original body expression.
00109     osl_strings_idump(file, body->expression, level + 1);
00110   }
00111   else {
00112     fprintf(file, "+-- NULL body\n");
00113   }
00114   
00115   // The last line.
00116   for (j = 0; j <= level; j++)
00117     fprintf(file, "|\t");
00118   fprintf(file, "\n");
00119 }
00120 
00121 
00129 void osl_body_dump(FILE * file, osl_body_p body) {
00130   osl_body_idump(file, body, 0);
00131 }
00132 
00133 
00141 void osl_body_print(FILE * file, osl_body_p body) {
00142   int nb_iterators;
00143 
00144   if (body != NULL) {
00145     nb_iterators = osl_strings_size(body->iterators);
00146     fprintf(file, "# Number of original iterators\n");
00147     fprintf(file, "%d\n", nb_iterators);
00148 
00149     if (nb_iterators > 0) {
00150       fprintf(file, "\n# List of original iterators\n");
00151       osl_strings_print(file, body->iterators);
00152     }
00153 
00154     fprintf(file, "\n# Statement body expression\n");
00155     osl_strings_print(file, body->expression);
00156   }
00157   else {
00158     fprintf(file, "# NULL statement body\n");
00159   }
00160 }
00161 
00162 
00170 char * osl_body_sprint(osl_body_p body) {
00171   int nb_iterators;
00172   int high_water_mark = OSL_MAX_STRING;
00173   char * string = NULL;
00174   char buffer[OSL_MAX_STRING];
00175   char * iterators, * expression;
00176 
00177   OSL_malloc(string, char *, high_water_mark * sizeof(char));
00178   string[0] = '\0';
00179   
00180   if (body != NULL) {
00181     nb_iterators = osl_strings_size(body->iterators);
00182     sprintf(buffer, "# Number of original iterators\n%d\n", nb_iterators);
00183     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00184 
00185     if (nb_iterators > 0) {
00186       sprintf(buffer, "# List of original iterators\n");
00187       osl_util_safe_strcat(&string, buffer, &high_water_mark);
00188       iterators = osl_strings_sprint(body->iterators);
00189       osl_util_safe_strcat(&string, iterators, &high_water_mark);
00190       free(iterators);
00191     }
00192 
00193     sprintf(buffer, "# Statement body expression\n");
00194     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00195     expression = osl_strings_sprint(body->expression);
00196     osl_util_safe_strcat(&string, expression, &high_water_mark);
00197     free(expression);
00198   }
00199   else {
00200     sprintf(buffer, "# NULL body\n");
00201     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00202   }
00203 
00204   return string;
00205 }
00206 
00207 
00208 
00209 /*****************************************************************************
00210  *                               Reading function                            *
00211  *****************************************************************************/
00212 
00213 
00226 osl_body_p osl_body_sread(char ** input) {
00227   osl_body_p body = NULL;
00228   char * expression;
00229   int nb_iterators;
00230 
00231   if (input) {
00232     body = osl_body_malloc();
00233     
00234     // Read the number of iterators.
00235     nb_iterators = osl_util_read_int(NULL, input);
00236     
00237     // Read the iterator strings if any.
00238     if (nb_iterators > 0)
00239       body->iterators = osl_strings_sread(input);
00240 
00241     // Read the body:
00242     // - Skip blank/commented lines and spaces before the body.
00243     osl_util_sskip_blank_and_comments(input);
00244       
00245     // - Remove the comments after the body.
00246     expression = *input;
00247     while (*input && **input != '#' && **input != '\n')
00248       (*input)++;
00249     
00250     if (*input && **input == '#') {
00251       **input = '\0';
00252       while (**input != '\n')
00253         (*input)++;
00254     }
00255     else {
00256       if (*input && **input == '\n') {
00257         **input = '\0';
00258         (*input)++;
00259       }
00260     }
00261 
00262     // - Copy the body.
00263     body->expression = osl_strings_encapsulate(strdup(expression));
00264   }
00265 
00266   return body;
00267 }
00268 
00269 
00270 /*+***************************************************************************
00271  *                   Memory allocation/deallocation functions                *
00272  *****************************************************************************/
00273 
00274 
00282 osl_body_p osl_body_malloc() {
00283   osl_body_p body;
00284 
00285   OSL_malloc(body, osl_body_p, sizeof(osl_body_t));
00286   body->iterators    = NULL;
00287   body->expression   = NULL;
00288 
00289   return body;
00290 }
00291 
00292 
00299 void osl_body_free(osl_body_p body) {
00300 
00301   if (body != NULL) {
00302     osl_strings_free(body->iterators);
00303     osl_strings_free(body->expression);
00304     free(body);
00305   }
00306 }
00307 
00308 
00309 /*+***************************************************************************
00310  *                            Processing functions                           *
00311  *****************************************************************************/
00312 
00313 
00322 osl_body_p osl_body_clone(osl_body_p body) {
00323   osl_body_p copy = NULL;
00324 
00325   if (body != NULL) {
00326     copy = osl_body_malloc();
00327     copy->iterators  = osl_strings_clone(body->iterators);
00328     copy->expression = osl_strings_clone(body->expression);
00329   }
00330 
00331   return copy;
00332 }
00333 
00334 
00344 int osl_body_equal(osl_body_p b1, osl_body_p b2) {
00345   
00346   if (b1 == b2)
00347     return 1;
00348  
00349   if (((b1 != NULL) && (b2 == NULL)) ||
00350       ((b1 == NULL) && (b2 != NULL))) {
00351     OSL_info("bodies are not the same"); 
00352     return 0;
00353   }
00354 
00355   if (!osl_strings_equal(b1->iterators, b2->iterators)) {
00356     OSL_info("body iterators are not the same");
00357     return 0;
00358   }
00359   
00360   if (!osl_strings_equal(b1->expression, b2->expression)) {
00361     OSL_info("body expressions are not the same");
00362     return 0;
00363   }
00364 
00365   return 1;
00366 }
00367 
00368 
00375 osl_interface_p osl_body_interface() {
00376   osl_interface_p interface = osl_interface_malloc();
00377   
00378   interface->URI    = strdup(OSL_URI_BODY);
00379   interface->idump  = (osl_idump_f)osl_body_idump;
00380   interface->sprint = (osl_sprint_f)osl_body_sprint;
00381   interface->sread  = (osl_sread_f)osl_body_sread;
00382   interface->malloc = (osl_malloc_f)osl_body_malloc;
00383   interface->free   = (osl_free_f)osl_body_free;
00384   interface->clone  = (osl_clone_f)osl_body_clone;
00385   interface->equal  = (osl_equal_f)osl_body_equal;
00386 
00387   return interface;
00388 }
00389