OpenScop  0.9.0
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 void osl_body_print_scoplib(FILE * file, osl_body_p body) {
00171   int nb_iterators;
00172 
00173   if (body != NULL) {
00174     nb_iterators = osl_strings_size(body->iterators);
00175 
00176     if (nb_iterators > 0) {
00177       fprintf(file, "# List of original iterators\n");
00178       osl_strings_print(file, body->iterators);
00179     } else {
00180       fprintf(file, "fakeiter\n");
00181     }
00182 
00183     fprintf(file, "# Statement body expression\n");
00184     osl_strings_print(file, body->expression);
00185   }
00186   else {
00187     fprintf(file, "# NULL statement body\n");
00188   }
00189 }
00190 
00191 
00199 char * osl_body_sprint(osl_body_p body) {
00200   int nb_iterators;
00201   int high_water_mark = OSL_MAX_STRING;
00202   char * string = NULL;
00203   char buffer[OSL_MAX_STRING];
00204   char * iterators, * expression;
00205 
00206   OSL_malloc(string, char *, high_water_mark * sizeof(char));
00207   string[0] = '\0';
00208   
00209   if (body != NULL) {
00210     nb_iterators = osl_strings_size(body->iterators);
00211     sprintf(buffer, "# Number of original iterators\n%d\n", nb_iterators);
00212     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00213 
00214     if (nb_iterators > 0) {
00215       sprintf(buffer, "# List of original iterators\n");
00216       osl_util_safe_strcat(&string, buffer, &high_water_mark);
00217       iterators = osl_strings_sprint(body->iterators);
00218       osl_util_safe_strcat(&string, iterators, &high_water_mark);
00219       free(iterators);
00220     }
00221 
00222     sprintf(buffer, "# Statement body expression\n");
00223     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00224     expression = osl_strings_sprint(body->expression);
00225     osl_util_safe_strcat(&string, expression, &high_water_mark);
00226     free(expression);
00227   }
00228   else {
00229     sprintf(buffer, "# NULL body\n");
00230     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00231   }
00232 
00233   return string;
00234 }
00235 
00236 
00237 
00238 /*****************************************************************************
00239  *                               Reading function                            *
00240  *****************************************************************************/
00241 
00242 
00255 osl_body_p osl_body_sread(char ** input) {
00256   osl_body_p body = NULL;
00257   char * expression;
00258   int nb_iterators;
00259 
00260   if (input) {
00261     body = osl_body_malloc();
00262     
00263     // Read the number of iterators.
00264     nb_iterators = osl_util_read_int(NULL, input);
00265     
00266     // Read the iterator strings if any.
00267     if (nb_iterators > 0) {
00268       body->iterators = osl_strings_sread(input);
00269     }
00270     else {
00271       body->iterators = osl_strings_malloc();
00272       OSL_malloc(body->iterators->string, char **, sizeof(char *));
00273       body->iterators->string[0] = NULL;
00274     }
00275 
00276     // Read the body:
00277     expression = osl_util_read_line(NULL, input);
00278 
00279     // Insert the body.
00280     body->expression = osl_strings_encapsulate(expression);
00281   }
00282 
00283   return body;
00284 }
00285 
00286 
00287 /*+***************************************************************************
00288  *                   Memory allocation/deallocation functions                *
00289  *****************************************************************************/
00290 
00291 
00299 osl_body_p osl_body_malloc() {
00300   osl_body_p body;
00301 
00302   OSL_malloc(body, osl_body_p, sizeof(osl_body_t));
00303   body->iterators    = NULL;
00304   body->expression   = NULL;
00305 
00306   return body;
00307 }
00308 
00309 
00316 void osl_body_free(osl_body_p body) {
00317 
00318   if (body != NULL) {
00319     osl_strings_free(body->iterators);
00320     osl_strings_free(body->expression);
00321     free(body);
00322   }
00323 }
00324 
00325 
00326 /*+***************************************************************************
00327  *                            Processing functions                           *
00328  *****************************************************************************/
00329 
00330 
00339 osl_body_p osl_body_clone(osl_body_p body) {
00340   osl_body_p copy = NULL;
00341 
00342   if (body != NULL) {
00343     copy = osl_body_malloc();
00344     copy->iterators  = osl_strings_clone(body->iterators);
00345     copy->expression = osl_strings_clone(body->expression);
00346   }
00347 
00348   return copy;
00349 }
00350 
00351 
00361 int osl_body_equal(osl_body_p b1, osl_body_p b2) {
00362   
00363   if (b1 == b2)
00364     return 1;
00365  
00366   if (((b1 != NULL) && (b2 == NULL)) ||
00367       ((b1 == NULL) && (b2 != NULL))) {
00368     OSL_info("bodies are not the same"); 
00369     return 0;
00370   }
00371 
00372   if (!osl_strings_equal(b1->iterators, b2->iterators)) {
00373     OSL_info("body iterators are not the same");
00374     return 0;
00375   }
00376   
00377   if (!osl_strings_equal(b1->expression, b2->expression)) {
00378     OSL_info("body expressions are not the same");
00379     return 0;
00380   }
00381 
00382   return 1;
00383 }
00384 
00385 
00392 osl_interface_p osl_body_interface() {
00393   osl_interface_p interface = osl_interface_malloc();
00394   
00395   OSL_strdup(interface->URI, OSL_URI_BODY);
00396   interface->idump  = (osl_idump_f)osl_body_idump;
00397   interface->sprint = (osl_sprint_f)osl_body_sprint;
00398   interface->sread  = (osl_sread_f)osl_body_sread;
00399   interface->malloc = (osl_malloc_f)osl_body_malloc;
00400   interface->free   = (osl_free_f)osl_body_free;
00401   interface->clone  = (osl_clone_f)osl_body_clone;
00402   interface->equal  = (osl_equal_f)osl_body_equal;
00403 
00404   return interface;
00405 }
00406