OpenScop  0.9.0
symbols.c
Go to the documentation of this file.
00001 
00002     /*+-----------------------------------------------------------------**
00003      **                       OpenScop Library                          **
00004      **-----------------------------------------------------------------**
00005      **                     extensions/symbols.c                        **
00006      **-----------------------------------------------------------------**
00007      **                   First version: 07/03/2012                     **
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>                            *
00059  * Louis-Noel Pouchet  <Louis-Noel.pouchet@inria.fr>                         *
00060  * Prasanth Chatharasi <prasanth@iith.ac.in>                                 *
00061  *                                                                           *
00062  *****************************************************************************/
00063 
00064 #include <stdlib.h>
00065 #include <stdio.h>
00066 #include <string.h>
00067 
00068 #include <osl/macros.h>
00069 #include <osl/util.h>
00070 #include <osl/relation.h>
00071 #include <osl/interface.h>
00072 #include <osl/extensions/symbols.h>
00073 
00074 
00075 /*+***************************************************************************
00076  *                          Structure display function                       *
00077  *****************************************************************************/
00078 
00079 
00090 void osl_symbols_idump(FILE * file, osl_symbols_p symbols, int level) {
00091 
00092   int i, j, first = 1, number = 1;
00093 
00094   // Go to the right level.
00095   for (j = 0; j < level; j++)
00096     fprintf(file, "|\t");
00097 
00098   if (symbols != NULL)
00099     fprintf(file, "+-- osl_symbols_t\n");
00100   else
00101     fprintf(file, "+-- NULL symbols\n");
00102 
00103   while (symbols != NULL) {
00104     if (!first) {
00105       // Go to the right level.
00106       for (j = 0; j < level; j++)
00107         fprintf(file, "|\t");
00108       fprintf(file, "|   osl_symbol_t (node %d)\n", number);
00109     } else {
00110       first = 0;
00111     }
00112 
00113     // A blank line.
00114     for (j = 0; j <= level+1; j++)
00115       fprintf(file, "|\t");
00116     fprintf(file, "\n");
00117 
00118     // 1. Print the symbol kind.
00119     for (i = 0; i <= level; i++)
00120       fprintf(file, "|\t");
00121     if (symbols->type != OSL_UNDEFINED) {
00122       fprintf(file, "+-- Type: ");
00123       switch (symbols->type) {
00124         case OSL_SYMBOL_TYPE_ITERATOR : fprintf(file, "Iterator\n");  break;
00125         case OSL_SYMBOL_TYPE_PARAMETER: fprintf(file, "Parameter\n"); break;
00126         case OSL_SYMBOL_TYPE_ARRAY    : fprintf(file, "Array\n");     break;
00127         case OSL_SYMBOL_TYPE_FUNCTION : fprintf(file, "Function\n");  break;
00128         default : fprintf(file, "Unknown\n") ;
00129       }
00130     } else {
00131       fprintf(file, "+-- NULL type\n");
00132     }
00133 
00134     // A blank line.
00135     for(j = 0; j <= level + 1; j++)
00136       fprintf(file, "|\t") ;
00137     fprintf(file, "\n") ;
00138 
00139     // 2. Print the origin of the symbol.
00140     for (i = 0; i <= level; i++)
00141       fprintf(file, "|\t");
00142     if (symbols->generated != OSL_UNDEFINED)
00143       fprintf(file, "+-- Origin: %d\n", symbols->generated);
00144     else
00145       fprintf(file, "+-- Undefined origin\n");
00146 
00147     // A blank line.
00148     for(j = 0; j <= level + 1; j++)
00149       fprintf(file, "|\t") ;
00150     fprintf(file, "\n") ;
00151 
00152     // 3. Print the number of array dimensions for the symbol.
00153     for (i = 0; i <= level; i++)
00154       fprintf(file, "|\t");
00155     if (symbols->nb_dims != OSL_UNDEFINED)
00156       fprintf(file, "+-- Number of Dimensions: %d\n", symbols->nb_dims);
00157     else
00158       fprintf(file, "+-- Undefined number of dimensions\n");
00159 
00160     // A blank line.
00161     for(j = 0; j <= level + 1; j++)
00162       fprintf(file, "|\t") ;
00163     fprintf(file, "\n") ;
00164 
00165     // 4. Print the symbol identifier.
00166     osl_generic_idump(file, symbols->identifier, level + 1);
00167 
00168     // 5. Print the data type of the symbol.
00169     osl_generic_idump(file, symbols->datatype, level + 1);
00170 
00171     // 6. Print the scope of the symbol.
00172     osl_generic_idump(file, symbols->scope, level + 1);
00173 
00174     // 7. Print the extent of the symbol.
00175     osl_generic_idump(file, symbols->extent, level + 1);
00176 
00177     symbols = symbols->next;
00178     number++;
00179     // Next line.
00180     if (symbols != NULL) {
00181       for (j = 0; j <= level; j++)
00182         fprintf(file, "|\t");
00183       fprintf(file, "V\n");
00184     }
00185   } 
00186 
00187   // The last line.
00188   for (j = 0; j <= level; j++)
00189     fprintf(file, "|\t");
00190   fprintf(file, "\n");
00191 }
00192 
00193 
00201 void osl_symbols_dump(FILE * file, osl_symbols_p symbols) {
00202   osl_symbols_idump(file, symbols, 0);
00203 }
00204 
00205 
00213 char * osl_symbols_sprint(osl_symbols_p symbols) {
00214   int i = 1;
00215   int high_water_mark = OSL_MAX_STRING;
00216   char* string = NULL, *temp;
00217   char buffer[OSL_MAX_STRING];
00218 
00219   OSL_malloc(string, char *, high_water_mark * sizeof(char));
00220   string[0] = '\0';
00221 
00222   sprintf(buffer, "# Number of symbols\n%d\n",
00223           osl_symbols_get_nb_symbols(symbols));
00224   osl_util_safe_strcat(&string, buffer, &high_water_mark);
00225 
00226   while (symbols != NULL) {
00227     sprintf(buffer, "# ===========================================\n");
00228     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00229     sprintf(buffer, "# %d Data for symbol number %d \n", i, i);
00230     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00231 
00232     if (symbols->type == OSL_UNDEFINED) {
00233       sprintf(buffer, "# %d.1 Symbol type\nUndefined\n", i);
00234       osl_util_safe_strcat(&string, buffer, &high_water_mark);
00235     }
00236     else {
00237       sprintf(buffer, "# %d.1 Symbol type\n", i);
00238       osl_util_safe_strcat(&string, buffer, &high_water_mark);
00239       switch (symbols->type) {
00240         case OSL_SYMBOL_TYPE_ITERATOR : sprintf(buffer, "Iterator\n");  break;
00241         case OSL_SYMBOL_TYPE_PARAMETER: sprintf(buffer, "Parameter\n"); break;
00242         case OSL_SYMBOL_TYPE_ARRAY    : sprintf(buffer, "Array\n");     break;
00243         case OSL_SYMBOL_TYPE_FUNCTION : sprintf(buffer, "Function\n");  break;
00244         default : sprintf(buffer, "Undefined\n") ;
00245       }
00246       osl_util_safe_strcat(&string, buffer, &high_water_mark);
00247     }
00248 
00249     // Printing Generated Boolean flag
00250     sprintf(buffer, "\n# %d.2 Generated Boolean\n%d\n", i, symbols->generated);
00251     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00252 
00253     // Printing Number of dimensions
00254     sprintf(buffer,"\n# %d.3 Number of dimensions\n%d\n", i, symbols->nb_dims);
00255     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00256 
00257     // Printing Identifier
00258     sprintf(buffer, "\n# %d.4 Identifier\n", i);
00259     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00260     temp = osl_generic_sprint(symbols->identifier);
00261     osl_util_safe_strcat(&string, temp, &high_water_mark);
00262     free(temp);
00263 
00264     // Printing Datatype
00265     sprintf(buffer, "\n# %d.5 Datatype\n", i);
00266     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00267     temp = osl_generic_sprint(symbols->datatype);
00268     osl_util_safe_strcat(&string, temp, &high_water_mark);
00269     free(temp);
00270 
00271     // Printing Scope
00272     sprintf(buffer, "\n# %d.6 Scope\n", i);
00273     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00274     temp = osl_generic_sprint(symbols->scope);
00275     osl_util_safe_strcat(&string, temp, &high_water_mark);
00276     free(temp);
00277 
00278     // Printing Extent
00279     sprintf(buffer, "\n# %d.7 Extent\n", i);
00280     osl_util_safe_strcat(&string, buffer, &high_water_mark);
00281     temp = osl_generic_sprint(symbols->extent);
00282     osl_util_safe_strcat(&string, temp, &high_water_mark);
00283     free(temp);
00284 
00285     symbols = symbols->next;
00286   }
00287 
00288   OSL_realloc(string, char *, (strlen(string) + 1) * sizeof(char));
00289   return string;
00290 }
00291 
00292 
00293 /*****************************************************************************
00294  *                               Reading function                            *
00295  *****************************************************************************/
00296 
00297 
00308 osl_symbols_p osl_symbols_sread(char ** input) {
00309   int nb_symbols;
00310   char* type;
00311   osl_symbols_p symbols;
00312   osl_symbols_p head;
00313   osl_interface_p registry;
00314   
00315   if (*input == NULL) {
00316     OSL_debug("no symbols optional tag");
00317     return NULL;
00318   }
00319 
00320   if (strlen(*input) > OSL_MAX_STRING) 
00321     OSL_error("symbols too long");
00322 
00323   // Find the number of names provided.
00324   nb_symbols = osl_util_read_int(NULL, input);
00325 
00326   if (nb_symbols == 0)
00327     return NULL;
00328 
00329   head = symbols = osl_symbols_malloc();
00330   registry = osl_interface_get_default_registry();
00331 
00332   while (nb_symbols != 0) {
00333     // Reading the type of symbol
00334     type = osl_util_read_string(NULL, input);
00335     if (type != NULL) {
00336       if (strcmp(type, "Iterator") == 0)
00337         symbols->type = OSL_SYMBOL_TYPE_ITERATOR;
00338       else if (strcmp(type, "Parameter") == 0)
00339         symbols->type = OSL_SYMBOL_TYPE_PARAMETER;
00340       else if (strcmp(type, "Array") == 0)
00341         symbols->type = OSL_SYMBOL_TYPE_ARRAY;
00342       else if (strcmp(type, "Function") == 0)
00343         symbols->type = OSL_SYMBOL_TYPE_FUNCTION;
00344       else
00345         symbols->type = OSL_UNDEFINED;
00346       free(type);
00347     }
00348 
00349     // Reading origin of symbol
00350     symbols->generated = osl_util_read_int(NULL, input);
00351 
00352     // Reading the number of dimensions of a symbol
00353     symbols->nb_dims = osl_util_read_int(NULL, input);
00354 
00355     // Reading identifier
00356     symbols->identifier = osl_generic_sread_one(input, registry);
00357 
00358     // Reading data type
00359     symbols->datatype = osl_generic_sread_one(input, registry);
00360 
00361     // Reading scope
00362     symbols->scope = osl_generic_sread_one(input, registry);
00363 
00364     // Reading extent
00365     symbols->extent = osl_generic_sread_one(input, registry);
00366 
00367     nb_symbols --;
00368     if (nb_symbols != 0) {
00369       symbols->next = osl_symbols_malloc ();
00370       symbols = symbols->next;
00371     }
00372   } 
00373 
00374   osl_interface_free(registry);
00375   return head;
00376 } 
00377 
00378 
00379 /*+***************************************************************************
00380  *                    Memory allocation/deallocation function                *
00381  *****************************************************************************/
00382 
00383 
00392 osl_symbols_p osl_symbols_malloc() {
00393   osl_symbols_p symbols;
00394 
00395   OSL_malloc(symbols, osl_symbols_p, sizeof(osl_symbols_t));
00396   symbols->type       = OSL_UNDEFINED;
00397   symbols->generated  = OSL_UNDEFINED;
00398   symbols->nb_dims    = OSL_UNDEFINED;
00399   symbols->identifier = NULL;
00400   symbols->datatype   = NULL;
00401   symbols->scope      = NULL;
00402   symbols->extent     = NULL;
00403   symbols->next       = NULL;
00404 
00405   return symbols;
00406 }
00407 
00408 
00415 void osl_symbols_free(osl_symbols_p symbols) {
00416   osl_symbols_p tmp;
00417 
00418   while (symbols != NULL) {
00419     tmp = symbols->next;
00420     osl_generic_free(symbols->identifier);
00421     osl_generic_free(symbols->datatype);
00422     osl_generic_free(symbols->scope);
00423     osl_generic_free(symbols->extent);
00424     free(symbols);
00425     symbols = tmp;
00426   }
00427 }
00428 
00429 
00430 /*+***************************************************************************
00431  *                            Processing functions                           *
00432  *****************************************************************************/
00433 
00434 
00442 void osl_symbols_add(osl_symbols_p* location, osl_symbols_p symbols) {
00443   while (*location != NULL)
00444     location = &((*location)->next);
00445 
00446   *location = symbols;
00447 }
00448 
00449 
00458 osl_symbols_p osl_symbols_nclone(osl_symbols_p symbols, int n) {
00459   osl_symbols_p clone = NULL, new;
00460   int i = 0;
00461 
00462   while ((symbols != NULL) && ((n == -1) || (i < n))) {
00463     new             = osl_symbols_malloc();
00464     new->type       = symbols->type;
00465     new->generated  = symbols->generated;
00466     new->nb_dims    = symbols->nb_dims;
00467     new->identifier = osl_generic_clone(symbols->identifier);
00468     new->datatype   = osl_generic_clone(symbols->datatype);
00469     new->scope      = osl_generic_clone(symbols->scope);
00470     new->extent     = osl_generic_clone(symbols->extent);
00471     
00472     osl_symbols_add(&clone, new);
00473     symbols = symbols->next;
00474     i++;
00475   }
00476 
00477   return clone;
00478 }
00479 
00480 
00488 osl_symbols_p osl_symbols_clone(osl_symbols_p symbols) {
00489 
00490   return osl_symbols_nclone(symbols, -1);
00491 }
00492 
00493 
00502 int osl_symbols_equal(osl_symbols_p c1, osl_symbols_p c2) {
00503 
00504   if (c1 == c2)
00505     return 1;
00506 
00507   if (((c1 == NULL) && (c2 != NULL)) || ((c1 != NULL) && (c2 == NULL)))
00508     return 0;
00509 
00510   if (c1->type == c2->type && c1->generated == c2->generated && 
00511       c1->nb_dims == c2->nb_dims) {
00512     if (osl_generic_equal(c1->identifier, c2->identifier)) {
00513       if (osl_generic_equal(c1->datatype, c2->datatype)) {
00514         if (osl_generic_equal(c1->scope, c2->scope)) {
00515           if (osl_generic_equal(c1->extent, c2->extent)) {
00516             return 1;
00517           }
00518         }
00519       }
00520     }
00521   }
00522 
00523   return 0;
00524 }
00525 
00526 
00534 int osl_symbols_get_nb_symbols(osl_symbols_p symbols) {
00535   int nb_symbols = 0;
00536 
00537   while (symbols != NULL) {
00538     nb_symbols++;
00539     symbols = symbols->next;
00540   }
00541   return nb_symbols;
00542 }
00543 
00544 
00551 osl_interface_p osl_symbols_interface() {
00552   osl_interface_p interface = osl_interface_malloc();
00553 
00554   OSL_strdup(interface->URI, OSL_URI_SYMBOLS);
00555   interface->idump  = (osl_idump_f)osl_symbols_idump;
00556   interface->sprint = (osl_sprint_f)osl_symbols_sprint;
00557   interface->sread  = (osl_sread_f)osl_symbols_sread;
00558   interface->malloc = (osl_malloc_f)osl_symbols_malloc;
00559   interface->free   = (osl_free_f)osl_symbols_free;
00560   interface->clone  = (osl_clone_f)osl_symbols_clone;
00561   interface->equal  = (osl_equal_f)osl_symbols_equal;
00562   return interface;
00563 }