OpenScop  0.9.0
symbols.c
Go to the documentation of this file.
1 
2  /*+-----------------------------------------------------------------**
3  ** OpenScop Library **
4  **-----------------------------------------------------------------**
5  ** extensions/symbols.c **
6  **-----------------------------------------------------------------**
7  ** First version: 07/03/2012 **
8  **-----------------------------------------------------------------**
9 
10 
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> *
59  * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr> *
60  * Prasanth Chatharasi <prasanth@iith.ac.in> *
61  * *
62  *****************************************************************************/
63 
64 #include <stdlib.h>
65 #include <stdio.h>
66 #include <string.h>
67 
68 #include <osl/macros.h>
69 #include <osl/util.h>
70 #include <osl/relation.h>
71 #include <osl/interface.h>
72 #include <osl/extensions/symbols.h>
73 
74 
75 /*+***************************************************************************
76  * Structure display function *
77  *****************************************************************************/
78 
79 
90 void osl_symbols_idump(FILE * file, osl_symbols_p symbols, int level) {
91 
92  int i, j, first = 1, number = 1;
93 
94  // Go to the right level.
95  for (j = 0; j < level; j++)
96  fprintf(file, "|\t");
97 
98  if (symbols != NULL)
99  fprintf(file, "+-- osl_symbols_t\n");
100  else
101  fprintf(file, "+-- NULL symbols\n");
102 
103  while (symbols != NULL) {
104  if (!first) {
105  // Go to the right level.
106  for (j = 0; j < level; j++)
107  fprintf(file, "|\t");
108  fprintf(file, "| osl_symbol_t (node %d)\n", number);
109  } else {
110  first = 0;
111  }
112 
113  // A blank line.
114  for (j = 0; j <= level+1; j++)
115  fprintf(file, "|\t");
116  fprintf(file, "\n");
117 
118  // 1. Print the symbol kind.
119  for (i = 0; i <= level; i++)
120  fprintf(file, "|\t");
121  if (symbols->type != OSL_UNDEFINED) {
122  fprintf(file, "+-- Type: ");
123  switch (symbols->type) {
124  case OSL_SYMBOL_TYPE_ITERATOR : fprintf(file, "Iterator\n"); break;
125  case OSL_SYMBOL_TYPE_PARAMETER: fprintf(file, "Parameter\n"); break;
126  case OSL_SYMBOL_TYPE_ARRAY : fprintf(file, "Array\n"); break;
127  case OSL_SYMBOL_TYPE_FUNCTION : fprintf(file, "Function\n"); break;
128  default : fprintf(file, "Unknown\n") ;
129  }
130  } else {
131  fprintf(file, "+-- NULL type\n");
132  }
133 
134  // A blank line.
135  for(j = 0; j <= level + 1; j++)
136  fprintf(file, "|\t") ;
137  fprintf(file, "\n") ;
138 
139  // 2. Print the origin of the symbol.
140  for (i = 0; i <= level; i++)
141  fprintf(file, "|\t");
142  if (symbols->generated != OSL_UNDEFINED)
143  fprintf(file, "+-- Origin: %d\n", symbols->generated);
144  else
145  fprintf(file, "+-- Undefined origin\n");
146 
147  // A blank line.
148  for(j = 0; j <= level + 1; j++)
149  fprintf(file, "|\t") ;
150  fprintf(file, "\n") ;
151 
152  // 3. Print the number of array dimensions for the symbol.
153  for (i = 0; i <= level; i++)
154  fprintf(file, "|\t");
155  if (symbols->nb_dims != OSL_UNDEFINED)
156  fprintf(file, "+-- Number of Dimensions: %d\n", symbols->nb_dims);
157  else
158  fprintf(file, "+-- Undefined number of dimensions\n");
159 
160  // A blank line.
161  for(j = 0; j <= level + 1; j++)
162  fprintf(file, "|\t") ;
163  fprintf(file, "\n") ;
164 
165  // 4. Print the symbol identifier.
166  osl_generic_idump(file, symbols->identifier, level + 1);
167 
168  // 5. Print the data type of the symbol.
169  osl_generic_idump(file, symbols->datatype, level + 1);
170 
171  // 6. Print the scope of the symbol.
172  osl_generic_idump(file, symbols->scope, level + 1);
173 
174  // 7. Print the extent of the symbol.
175  osl_generic_idump(file, symbols->extent, level + 1);
176 
177  symbols = symbols->next;
178  number++;
179  // Next line.
180  if (symbols != NULL) {
181  for (j = 0; j <= level; j++)
182  fprintf(file, "|\t");
183  fprintf(file, "V\n");
184  }
185  }
186 
187  // The last line.
188  for (j = 0; j <= level; j++)
189  fprintf(file, "|\t");
190  fprintf(file, "\n");
191 }
192 
193 
201 void osl_symbols_dump(FILE * file, osl_symbols_p symbols) {
202  osl_symbols_idump(file, symbols, 0);
203 }
204 
205 
214  int i = 1;
215  int high_water_mark = OSL_MAX_STRING;
216  char* string = NULL, *temp;
217  char buffer[OSL_MAX_STRING];
218 
219  OSL_malloc(string, char *, high_water_mark * sizeof(char));
220  string[0] = '\0';
221 
222  sprintf(buffer, "# Number of symbols\n%d\n",
223  osl_symbols_get_nb_symbols(symbols));
224  osl_util_safe_strcat(&string, buffer, &high_water_mark);
225 
226  while (symbols != NULL) {
227  sprintf(buffer, "# ===========================================\n");
228  osl_util_safe_strcat(&string, buffer, &high_water_mark);
229  sprintf(buffer, "# %d Data for symbol number %d \n", i, i);
230  osl_util_safe_strcat(&string, buffer, &high_water_mark);
231 
232  if (symbols->type == OSL_UNDEFINED) {
233  sprintf(buffer, "# %d.1 Symbol type\nUndefined\n", i);
234  osl_util_safe_strcat(&string, buffer, &high_water_mark);
235  }
236  else {
237  sprintf(buffer, "# %d.1 Symbol type\n", i);
238  osl_util_safe_strcat(&string, buffer, &high_water_mark);
239  switch (symbols->type) {
240  case OSL_SYMBOL_TYPE_ITERATOR : sprintf(buffer, "Iterator\n"); break;
241  case OSL_SYMBOL_TYPE_PARAMETER: sprintf(buffer, "Parameter\n"); break;
242  case OSL_SYMBOL_TYPE_ARRAY : sprintf(buffer, "Array\n"); break;
243  case OSL_SYMBOL_TYPE_FUNCTION : sprintf(buffer, "Function\n"); break;
244  default : sprintf(buffer, "Undefined\n") ;
245  }
246  osl_util_safe_strcat(&string, buffer, &high_water_mark);
247  }
248 
249  // Printing Generated Boolean flag
250  sprintf(buffer, "\n# %d.2 Generated Boolean\n%d\n", i, symbols->generated);
251  osl_util_safe_strcat(&string, buffer, &high_water_mark);
252 
253  // Printing Number of dimensions
254  sprintf(buffer,"\n# %d.3 Number of dimensions\n%d\n", i, symbols->nb_dims);
255  osl_util_safe_strcat(&string, buffer, &high_water_mark);
256 
257  // Printing Identifier
258  sprintf(buffer, "\n# %d.4 Identifier\n", i);
259  osl_util_safe_strcat(&string, buffer, &high_water_mark);
260  temp = osl_generic_sprint(symbols->identifier);
261  osl_util_safe_strcat(&string, temp, &high_water_mark);
262  free(temp);
263 
264  // Printing Datatype
265  sprintf(buffer, "\n# %d.5 Datatype\n", i);
266  osl_util_safe_strcat(&string, buffer, &high_water_mark);
267  temp = osl_generic_sprint(symbols->datatype);
268  osl_util_safe_strcat(&string, temp, &high_water_mark);
269  free(temp);
270 
271  // Printing Scope
272  sprintf(buffer, "\n# %d.6 Scope\n", i);
273  osl_util_safe_strcat(&string, buffer, &high_water_mark);
274  temp = osl_generic_sprint(symbols->scope);
275  osl_util_safe_strcat(&string, temp, &high_water_mark);
276  free(temp);
277 
278  // Printing Extent
279  sprintf(buffer, "\n# %d.7 Extent\n", i);
280  osl_util_safe_strcat(&string, buffer, &high_water_mark);
281  temp = osl_generic_sprint(symbols->extent);
282  osl_util_safe_strcat(&string, temp, &high_water_mark);
283  free(temp);
284 
285  symbols = symbols->next;
286  }
287 
288  OSL_realloc(string, char *, (strlen(string) + 1) * sizeof(char));
289  return string;
290 }
291 
292 
293 /*****************************************************************************
294  * Reading function *
295  *****************************************************************************/
296 
297 
309  int nb_symbols;
310  char* type;
311  osl_symbols_p symbols;
312  osl_symbols_p head;
313  osl_interface_p registry;
314 
315  if (*input == NULL) {
316  OSL_debug("no symbols optional tag");
317  return NULL;
318  }
319 
320  if (strlen(*input) > OSL_MAX_STRING)
321  OSL_error("symbols too long");
322 
323  // Find the number of names provided.
324  nb_symbols = osl_util_read_int(NULL, input);
325 
326  if (nb_symbols == 0)
327  return NULL;
328 
329  head = symbols = osl_symbols_malloc();
331 
332  while (nb_symbols != 0) {
333  // Reading the type of symbol
334  type = osl_util_read_string(NULL, input);
335  if (type != NULL) {
336  if (strcmp(type, "Iterator") == 0)
337  symbols->type = OSL_SYMBOL_TYPE_ITERATOR;
338  else if (strcmp(type, "Parameter") == 0)
339  symbols->type = OSL_SYMBOL_TYPE_PARAMETER;
340  else if (strcmp(type, "Array") == 0)
341  symbols->type = OSL_SYMBOL_TYPE_ARRAY;
342  else if (strcmp(type, "Function") == 0)
343  symbols->type = OSL_SYMBOL_TYPE_FUNCTION;
344  else
345  symbols->type = OSL_UNDEFINED;
346  free(type);
347  }
348 
349  // Reading origin of symbol
350  symbols->generated = osl_util_read_int(NULL, input);
351 
352  // Reading the number of dimensions of a symbol
353  symbols->nb_dims = osl_util_read_int(NULL, input);
354 
355  // Reading identifier
356  symbols->identifier = osl_generic_sread_one(input, registry);
357 
358  // Reading data type
359  symbols->datatype = osl_generic_sread_one(input, registry);
360 
361  // Reading scope
362  symbols->scope = osl_generic_sread_one(input, registry);
363 
364  // Reading extent
365  symbols->extent = osl_generic_sread_one(input, registry);
366 
367  nb_symbols --;
368  if (nb_symbols != 0) {
369  symbols->next = osl_symbols_malloc ();
370  symbols = symbols->next;
371  }
372  }
373 
374  osl_interface_free(registry);
375  return head;
376 }
377 
378 
379 /*+***************************************************************************
380  * Memory allocation/deallocation function *
381  *****************************************************************************/
382 
383 
393  osl_symbols_p symbols;
394 
395  OSL_malloc(symbols, osl_symbols_p, sizeof(osl_symbols_t));
396  symbols->type = OSL_UNDEFINED;
397  symbols->generated = OSL_UNDEFINED;
398  symbols->nb_dims = OSL_UNDEFINED;
399  symbols->identifier = NULL;
400  symbols->datatype = NULL;
401  symbols->scope = NULL;
402  symbols->extent = NULL;
403  symbols->next = NULL;
404 
405  return symbols;
406 }
407 
408 
416  osl_symbols_p tmp;
417 
418  while (symbols != NULL) {
419  tmp = symbols->next;
420  osl_generic_free(symbols->identifier);
421  osl_generic_free(symbols->datatype);
422  osl_generic_free(symbols->scope);
423  osl_generic_free(symbols->extent);
424  free(symbols);
425  symbols = tmp;
426  }
427 }
428 
429 
430 /*+***************************************************************************
431  * Processing functions *
432  *****************************************************************************/
433 
434 
442 void osl_symbols_add(osl_symbols_p* location, osl_symbols_p symbols) {
443  while (*location != NULL)
444  location = &((*location)->next);
445 
446  *location = symbols;
447 }
448 
449 
459  osl_symbols_p clone = NULL, new;
460  int i = 0;
461 
462  while ((symbols != NULL) && ((n == -1) || (i < n))) {
463  new = osl_symbols_malloc();
464  new->type = symbols->type;
465  new->generated = symbols->generated;
466  new->nb_dims = symbols->nb_dims;
467  new->identifier = osl_generic_clone(symbols->identifier);
468  new->datatype = osl_generic_clone(symbols->datatype);
469  new->scope = osl_generic_clone(symbols->scope);
470  new->extent = osl_generic_clone(symbols->extent);
471 
472  osl_symbols_add(&clone, new);
473  symbols = symbols->next;
474  i++;
475  }
476 
477  return clone;
478 }
479 
480 
489 
490  return osl_symbols_nclone(symbols, -1);
491 }
492 
493 
503 
504  if (c1 == c2)
505  return 1;
506 
507  if (((c1 == NULL) && (c2 != NULL)) || ((c1 != NULL) && (c2 == NULL)))
508  return 0;
509 
510  if (c1->type == c2->type && c1->generated == c2->generated &&
511  c1->nb_dims == c2->nb_dims) {
512  if (osl_generic_equal(c1->identifier, c2->identifier)) {
513  if (osl_generic_equal(c1->datatype, c2->datatype)) {
514  if (osl_generic_equal(c1->scope, c2->scope)) {
515  if (osl_generic_equal(c1->extent, c2->extent)) {
516  return 1;
517  }
518  }
519  }
520  }
521  }
522 
523  return 0;
524 }
525 
526 
535  int nb_symbols = 0;
536 
537  while (symbols != NULL) {
538  nb_symbols++;
539  symbols = symbols->next;
540  }
541  return nb_symbols;
542 }
543 
544 
553 
554  OSL_strdup(interface->URI, OSL_URI_SYMBOLS);
555  interface->idump = (osl_idump_f)osl_symbols_idump;
556  interface->sprint = (osl_sprint_f)osl_symbols_sprint;
557  interface->sread = (osl_sread_f)osl_symbols_sread;
558  interface->malloc = (osl_malloc_f)osl_symbols_malloc;
559  interface->free = (osl_free_f)osl_symbols_free;
560  interface->clone = (osl_clone_f)osl_symbols_clone;
561  interface->equal = (osl_equal_f)osl_symbols_equal;
562  return interface;
563 }
osl_interface_p osl_symbols_interface()
Definition: symbols.c:551
void *(* osl_clone_f)(void *)
Definition: interface.h:80
void osl_interface_free(osl_interface_p interface)
Definition: interface.c:237
char * osl_generic_sprint(osl_generic_p generic)
Definition: generic.c:158
osl_generic_p identifier
Definition: symbols.h:87
int osl_symbols_get_nb_symbols(osl_symbols_p symbols)
Definition: symbols.c:534
osl_interface_p osl_interface_malloc()
Definition: interface.c:212
int generated
Definition: symbols.h:85
int nb_dims
Definition: symbols.h:86
osl_symbols_p osl_symbols_nclone(osl_symbols_p symbols, int n)
Definition: symbols.c:458
struct osl_symbols * next
Definition: symbols.h:93
osl_symbols_p osl_symbols_sread(char **input)
Definition: symbols.c:308
osl_generic_p datatype
Definition: symbols.h:88
void *(* osl_sread_f)(char **)
Definition: interface.h:77
void osl_symbols_dump(FILE *file, osl_symbols_p symbols)
Definition: symbols.c:201
void osl_symbols_idump(FILE *file, osl_symbols_p symbols, int level)
Definition: symbols.c:90
void osl_util_safe_strcat(char **dst, char *src, int *hwm)
Definition: util.c:483
void(* osl_idump_f)(FILE *, void *, int)
Definition: interface.h:75
osl_symbols_p osl_symbols_malloc()
Definition: symbols.c:392
void osl_symbols_free(osl_symbols_p symbols)
Definition: symbols.c:415
void osl_generic_idump(FILE *file, osl_generic_p generic, int level)
Definition: generic.c:89
osl_symbols_p osl_symbols_clone(osl_symbols_p symbols)
Definition: symbols.c:488
int osl_util_read_int(FILE *file, char **str)
Definition: util.c:140
char *(* osl_sprint_f)(void *)
Definition: interface.h:76
char * osl_util_read_string(FILE *file, char **str)
Definition: util.c:181
char * osl_symbols_sprint(osl_symbols_p symbols)
Definition: symbols.c:213
int osl_symbols_equal(osl_symbols_p c1, osl_symbols_p c2)
Definition: symbols.c:502
int type
Definition: symbols.h:84
void *(* osl_malloc_f)()
Definition: interface.h:78
int(* osl_equal_f)(void *, void *)
Definition: interface.h:81
void osl_symbols_add(osl_symbols_p *location, osl_symbols_p symbols)
Definition: symbols.c:442
osl_interface_p osl_interface_get_default_registry()
Definition: interface.c:386
osl_generic_p osl_generic_sread_one(char **input, osl_interface_p registry)
Definition: generic.c:266
int osl_generic_equal(osl_generic_p x1, osl_generic_p x2)
Definition: generic.c:592
osl_generic_p osl_generic_clone(osl_generic_p generic)
Definition: generic.c:540
osl_generic_p scope
Definition: symbols.h:89
void osl_generic_free(osl_generic_p generic)
Definition: generic.c:489
void(* osl_free_f)(void *)
Definition: interface.h:79
osl_generic_p extent
Definition: symbols.h:90