OpenScop  0.9.0
interface.c
Go to the documentation of this file.
00001 
00002     /*+-----------------------------------------------------------------**
00003      **                       OpenScop Library                          **
00004      **-----------------------------------------------------------------**
00005      **                          interface.c                            **
00006      **-----------------------------------------------------------------**
00007      **                   First version: 15/07/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 #include <stdlib.h>
00064 #include <stdio.h>
00065 #include <string.h>
00066 #include <osl/extensions/textual.h>
00067 #include <osl/extensions/comment.h>
00068 #include <osl/extensions/null.h>
00069 #include <osl/extensions/scatnames.h>
00070 #include <osl/extensions/arrays.h>
00071 #include <osl/extensions/coordinates.h>
00072 #include <osl/extensions/clay.h>
00073 #include <osl/extensions/dependence.h>
00074 #include <osl/extensions/symbols.h>
00075 #include <osl/extensions/irregular.h>
00076 #include <osl/extensions/extbody.h>
00077 #include <osl/extensions/loop.h>
00078 #include <osl/extensions/pluto_unroll.h>
00079 #include <osl/strings.h>
00080 #include <osl/body.h>
00081 #include <osl/relation.h>
00082 #include <osl/interface.h>
00083 
00084 
00085 /*+***************************************************************************
00086  *                          Structure display function                       *
00087  *****************************************************************************/
00088 
00089 
00100 void osl_interface_idump(FILE * file, osl_interface_p interface, int level) {
00101   int j, first = 1;
00102 
00103   // Go to the right level.
00104   for (j = 0; j < level; j++)
00105     fprintf(file, "|\t");
00106 
00107   if (interface != NULL)
00108     fprintf(file, "+-- osl_interface_t: URI = %s\n", interface->URI);
00109   else
00110     fprintf(file, "+-- NULL interface\n");
00111 
00112   
00113   while (interface != NULL) {
00114     if (!first) {
00115       // Go to the right level.
00116       for (j = 0; j < level; j++)
00117         fprintf(file, "|\t");
00118 
00119       if (interface->URI != NULL)
00120         fprintf(file, "|   osl_interface_t: URI = %s\n", interface->URI);
00121       else
00122         fprintf(file, "|   osl_interface_t: URI = (NULL)\n");
00123     }
00124     else
00125       first = 0;
00126 
00127     interface = interface->next;
00128 
00129     // Next line.
00130     if (interface != NULL) {
00131       for (j = 0; j <= level + 1; j++)
00132         fprintf(file, "|\t");
00133       fprintf(file, "\n");
00134       for (j = 0; j <= level; j++)
00135         fprintf(file, "|\t");
00136       fprintf(file, "V\n");
00137     }
00138   }
00139   
00140   // The last line.
00141   for (j = 0; j <= level; j++)
00142     fprintf(file, "|\t");
00143   fprintf(file, "\n");
00144 }
00145 
00146 
00154 void osl_interface_dump(FILE * file, osl_interface_p interface) {
00155   osl_interface_idump(file, interface, 0); 
00156 }
00157 
00158 
00159 /*****************************************************************************
00160  *                               Reading function                            *
00161  *****************************************************************************/
00162 
00163 
00164 /*+***************************************************************************
00165  *                    Memory allocation/deallocation function                *
00166  *****************************************************************************/
00167 
00168 
00177 void osl_interface_add(osl_interface_p * list, osl_interface_p interface) {
00178   osl_interface_p tmp = *list, check_interface;
00179 
00180   if (interface != NULL) {
00181     // First, check that the interface list is OK.
00182     check_interface = interface;
00183     while (check_interface != NULL) {
00184       if (check_interface->URI == NULL)
00185         OSL_error("no URI in an interface to add to a list");
00186 
00187       if (osl_interface_lookup(*list, check_interface->URI) != NULL)
00188         OSL_error("only one interface with a given URI is allowed");
00189       check_interface = check_interface->next;
00190     }
00191 
00192     if (*list != NULL) {
00193       while (tmp->next != NULL)
00194         tmp = tmp->next;
00195       tmp->next = interface;
00196     }
00197     else {
00198       *list = interface;
00199     }
00200   }
00201 }
00202 
00203 
00212 osl_interface_p osl_interface_malloc() {
00213   osl_interface_p interface;
00214 
00215   OSL_malloc(interface, osl_interface_p,
00216                   sizeof(osl_interface_t));
00217   interface->URI    = NULL;
00218   interface->idump  = NULL; 
00219   interface->sprint = NULL; 
00220   interface->sread  = NULL; 
00221   interface->malloc = NULL; 
00222   interface->free   = NULL; 
00223   interface->clone  = NULL; 
00224   interface->equal  = NULL;
00225   interface->next   = NULL;
00226 
00227   return interface;
00228 }
00229 
00230 
00237 void osl_interface_free(osl_interface_p interface) {
00238   osl_interface_p tmp;
00239   int i = 0;
00240  
00241   while (interface != NULL) {
00242     tmp = interface->next;
00243     if (interface->URI != NULL)
00244       free(interface->URI);
00245     free(interface);
00246     interface = tmp;
00247     i++;
00248   }
00249 }
00250 
00251 
00252 /*+***************************************************************************
00253  *                            Processing functions                           *
00254  *****************************************************************************/
00255 
00256 
00264 int osl_interface_number(osl_interface_p interface) {
00265   int number = 0;
00266 
00267   while (interface != NULL) {
00268     number++;
00269     interface = interface->next;
00270   }
00271   return number;
00272 }
00273 
00274 
00283 osl_interface_p osl_interface_nclone(osl_interface_p interface, int n) {
00284   osl_interface_p clone = NULL, new;
00285   int i = 0;
00286 
00287   while ((interface != NULL) && ((n == -1) || (i < n))) {
00288     new = osl_interface_malloc();
00289     OSL_strdup(new->URI, interface->URI);
00290     new->idump  = interface->idump;
00291     new->sprint = interface->sprint;
00292     new->sread  = interface->sread;
00293     new->malloc = interface->malloc;
00294     new->free   = interface->free;
00295     new->clone  = interface->clone;
00296     new->equal  = interface->equal;
00297     
00298     osl_interface_add(&clone, new);
00299     interface = interface->next;
00300     i++;
00301   }
00302 
00303   return clone;
00304 }
00305 
00306 
00314 osl_interface_p osl_interface_clone(osl_interface_p interface) {
00315 
00316   return osl_interface_nclone(interface, -1);
00317 }
00318 
00319 
00328 int osl_interface_equal(osl_interface_p interface1,
00329                         osl_interface_p interface2) {
00330 
00331   if (interface1 == interface2)
00332     return 1;
00333   
00334   if (((interface1 == NULL) && (interface2 != NULL)) ||
00335       ((interface1 != NULL) && (interface2 == NULL)))
00336     return 0;
00337 
00338   if (strcmp(interface1->URI, interface2->URI)   ||
00339       (interface1->idump  != interface2->idump)  ||
00340       (interface1->sprint != interface2->sprint) ||
00341       (interface1->sread  != interface2->sread)  ||
00342       (interface1->malloc != interface2->malloc) ||
00343       (interface1->free   != interface2->free)   ||
00344       (interface1->clone  != interface2->clone)  ||
00345       (interface1->equal  != interface2->equal))
00346     return 0;
00347 
00348   return 1;
00349 }
00350 
00351 
00361 osl_interface_p
00362 osl_interface_lookup(osl_interface_p list, char * URI) {
00363 
00364   if (URI == NULL) {
00365     OSL_warning("lookup for a NULL URI");
00366   }
00367   else {
00368     while (list != NULL) {
00369       if ((list->URI != NULL) && (!strcmp(list->URI, URI)))
00370         return list;
00371 
00372       list = list->next;
00373     }
00374   }
00375 
00376   return NULL;
00377 }
00378 
00379 
00386 osl_interface_p osl_interface_get_default_registry() {
00387   osl_interface_p registry = NULL;
00388 
00389   // Internal generics
00390   osl_interface_add(&registry, osl_strings_interface());
00391   osl_interface_add(&registry, osl_body_interface());
00392   osl_interface_add(&registry, osl_relation_interface());
00393 
00394   // Extensions
00395   osl_interface_add(&registry, osl_textual_interface());
00396   osl_interface_add(&registry, osl_comment_interface());
00397   osl_interface_add(&registry, osl_null_interface());
00398   osl_interface_add(&registry, osl_scatnames_interface());
00399   osl_interface_add(&registry, osl_arrays_interface());
00400   osl_interface_add(&registry, osl_coordinates_interface());
00401   osl_interface_add(&registry, osl_clay_interface());
00402   osl_interface_add(&registry, osl_dependence_interface());
00403   osl_interface_add(&registry, osl_symbols_interface());
00404   osl_interface_add(&registry, osl_extbody_interface());
00405   osl_interface_add(&registry, osl_loop_interface());
00406   osl_interface_add(&registry, osl_pluto_unroll_interface());
00407   //osl_interface_add(&registry, osl_irregular_interface());
00408 
00409   return registry;
00410 }
00411 
00412 
00413