2
/*+-----------------------------------------------------------------**
4
**-----------------------------------------------------------------**
6
**-----------------------------------------------------------------**
7
** First version: 26/11/2010 **
8
**-----------------------------------------------------------------**
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
* '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
30
* Copyright (C) 2008 University Paris-Sud 11 and INRIA *
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 *
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. *
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. *
56
* OpenScop Library, a library to manipulate OpenScop formats and data *
57
* structures. Written by: *
58
* Cedric Bastoul <Cedric.Bastoul@u-psud.fr> and *
59
* Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr> *
61
*****************************************************************************/
67
#include <osl/macros.h>
69
#include <osl/interface.h>
70
#include <osl/generic.h>
73
/*+***************************************************************************
74
* Structure display function *
75
*****************************************************************************/
79
* osl_generic_idump function:
80
* this function displays an osl_generic_t structure (*generic) into
81
* a file (file, possibly stdout) in a way that trends to be understandable.
82
* It includes an indentation level (level) in order to work with others
84
* \param[in] file File where informations are printed.
85
* \param[in] generic The generic whose information has to be printed.
86
* \param[in] level Number of spaces before printing, for each line.
88
void osl_generic_idump(FILE * file, osl_generic_p generic, int level) {
91
// Go to the right level.
92
for (j = 0; j < level; j++)
96
fprintf(file, "+-- osl_generic_t\n");
98
fprintf(file, "+-- NULL generic\n");
100
while (generic != NULL) {
102
// Go to the right level.
103
for (j = 0; j < level; j++)
104
fprintf(file, "|\t");
105
fprintf(file, "| osl_generic_t\n");
112
for(j = 0; j <= level + 1; j++)
113
fprintf(file, "|\t");
116
osl_interface_idump(file, generic->interface, level + 1);
118
if (generic->interface != NULL)
119
generic->interface->idump(file, generic->data, level + 1);
121
generic = generic->next;
124
if (generic != NULL) {
125
for (j = 0; j <= level; j++)
126
fprintf(file, "|\t");
127
fprintf(file, "V\n");
132
for (j = 0; j <= level; j++)
133
fprintf(file, "|\t");
139
* osl_generic_dump function:
140
* this function prints the content of an osl_generic_t structure
141
* (*generic) into a file (file, possibly stdout).
142
* \param[in] file File where the information has to be printed.
143
* \param[in] generic The generic structure to print.
145
void osl_generic_dump(FILE * file, osl_generic_p generic) {
146
osl_generic_idump(file, generic, 0);
151
* osl_generic_print function:
152
* this function prints the content of an osl_generic_t structure
153
* (*generic) into a string (returned) in the OpenScop format.
154
* \param[in] file File where the information has to be printed.
155
* \param[in] generic The generic structure to print.
157
void osl_generic_print(FILE * file, osl_generic_p generic) {
163
while (generic != NULL) {
164
if (generic->interface != NULL) {
165
string = generic->interface->sprint(generic->data);
166
if (string != NULL) {
167
fprintf(file, "<%s>\n", generic->interface->URI);
168
fprintf(file, "%s", string);
169
fprintf(file, "</%s>\n", generic->interface->URI);
173
generic = generic->next;
178
/*****************************************************************************
180
*****************************************************************************/
184
* osl_generic_sread function:
185
* this function reads a list of generics from a string complying to the
186
* OpenScop textual format and a list of known interfaces. It returns a
187
* pointer to the corresponding list of generic structures.
188
* \param[in] string The string where to read a list of data.
189
* \param[in] registry The list of known interfaces (others are ignored).
190
* \return A pointer to the generic information list that has been read.
192
osl_generic_p osl_generic_sread(char * string, osl_interface_p registry) {
193
osl_generic_p generic = NULL, new;
194
char * content, * start;
197
while (registry != NULL) {
198
content = osl_util_tag_content(string, registry->URI);
199
if (content != NULL) {
201
data = registry->sread(&content);
203
new = osl_generic_malloc();
204
new->interface = osl_interface_nclone(registry, 1);
206
osl_generic_add(&generic, new);
210
registry = registry->next;
218
* osl_generic_read_one function:
219
* this function reads one generic from a file (possibly stdin)
220
* complying to the OpenScop textual format and a list of known interfaces.
221
* It returns a pointer to the corresponding generic structure. If no
222
* tag is found, an error is reported, in the case of an empty or closing tag
223
* name the function returns the NULL pointer.
224
* \param[in] file The input file where to read a list of data.
225
* \param[in] registry The list of known interfaces (others are ignored).
226
* \return A pointer to the generic that has been read.
228
osl_generic_p osl_generic_read_one(FILE * file, osl_interface_p registry) {
230
char * content, * temp;
231
osl_generic_p generic = NULL;
232
osl_interface_p interface;
234
tag = osl_util_read_tag(file, NULL);
235
if ((tag == NULL) || (strlen(tag) < 1) || (tag[0] == '/')) {
236
OSL_debug("empty tag name or closing tag instead of an opening one");
240
content = osl_util_read_uptoendtag(file, tag);
241
interface = osl_interface_lookup(registry, tag);
244
if (interface == NULL) {
245
OSL_warning("unsupported generic");
246
fprintf(stderr, "[osl] Warning: unknown URI \"%s\".\n", tag);
249
generic = osl_generic_malloc();
250
generic->interface = osl_interface_nclone(interface, 1);
251
generic->data = interface->sread(&temp);
261
* osl_generic_read function:
262
* this function reads a list of generics from a file (possibly stdin)
263
* complying to the OpenScop textual format and a list of known interfaces.
264
* It returns a pointer to the list of corresponding generic structures.
265
* \param[in] file The input file where to read a list of data.
266
* \param[in] registry The list of known interfaces (others are ignored).
267
* \return A pointer to the generic information list that has been read.
269
osl_generic_p osl_generic_read(FILE * file, osl_interface_p registry) {
270
char * generic_string;
271
osl_generic_p generic_list;
273
generic_string = osl_util_read_uptotag(file, OSL_TAG_END_SCOP);
274
generic_list = osl_generic_sread(generic_string, registry);
275
free(generic_string);
280
/*+***************************************************************************
281
* Memory allocation/deallocation function *
282
*****************************************************************************/
286
* osl_generic_add function:
287
* this function adds a generic node (it may be a list as well) to a list
288
* of generics provided as parameter (list). The new node is inserted at
289
* the end of the list.
290
* \param[in] list The list of generics to add a node (NULL if empty).
291
* \param[in] generic The generic list to add to the initial list.
293
void osl_generic_add(osl_generic_p * list, osl_generic_p generic) {
294
osl_generic_p tmp = *list, check;
296
if (generic != NULL) {
297
// First, check that the generic list is OK.
299
while (check != NULL) {
300
if ((check->interface == NULL) || (check->interface->URI == NULL))
301
OSL_error("no interface or URI in a generic to add to a list");
303
// TODO: move this to the integrity check.
304
if (osl_generic_lookup(*list, check->interface->URI) != NULL)
305
OSL_error("only one generic with a given URI is allowed");
310
while (tmp->next != NULL)
322
* osl_generic_malloc function:
323
* This function allocates the memory space for an osl_generic_t
324
* structure and sets its fields with default values. Then it returns a
325
* pointer to the allocated space.
326
* \return A pointer to an empty generic structure with fields set to
329
osl_generic_p osl_generic_malloc() {
330
osl_generic_p generic;
332
OSL_malloc(generic, osl_generic_p, sizeof(osl_generic_t));
333
generic->interface = NULL;
334
generic->data = NULL;
335
generic->next = NULL;
342
* osl_generic_free function:
343
* This function frees the allocated memory for a generic structure.
344
* \param[in] generic The pointer to the generic structure we want to free.
346
void osl_generic_free(osl_generic_p generic) {
349
while (generic != NULL) {
350
next = generic->next;
351
if (generic->interface != NULL) {
352
generic->interface->free(generic->data);
353
osl_interface_free(generic->interface);
356
if (generic->data != NULL) {
357
OSL_warning("unregistered interface, memory leaks are possible");
367
/*+***************************************************************************
368
* Processing functions *
369
*****************************************************************************/
373
* osl_generic_clone function:
374
* This function builds and returns a "hard copy" (not a pointer copy) of an
375
* osl_generic_t data structure.
376
* \param[in] generic The pointer to the generic structure we want to clone.
377
* \return A pointer to the clone of the input generic structure.
379
osl_generic_p osl_generic_clone(osl_generic_p generic) {
380
osl_generic_p clone = NULL, new;
381
osl_interface_p interface;
384
while (generic != NULL) {
385
if (generic->interface != NULL) {
386
x = generic->interface->clone(generic->data);
387
interface = osl_interface_clone(generic->interface);
388
new = osl_generic_malloc();
389
new->interface = interface;
391
osl_generic_add(&clone, new);
394
OSL_warning("unregistered interface, cloning ignored");
396
generic = generic->next;
404
* osl_generic_count function:
405
* this function counts the number of elements in the generic list provided
406
* as parameter (x) and returns this number.
407
* \param[in] x The list of generics.
408
* \return The number of elements in the list.
410
int osl_generic_count(osl_generic_p x) {
411
int generic_number = 0;
418
return generic_number;
423
* osl_generic_equal function:
424
* this function returns true if the two generic structures are the same,
425
* false otherwise. This functions considers two generic structures as equal
426
* independently of the order of the nodes. TODO: make it dependent on the
428
* \param x1 The first generic structure.
429
* \param x2 The second generic structure.
430
* \return 1 if x1 and x2 are the same (content-wise), 0 otherwise.
432
int osl_generic_equal(osl_generic_p x1, osl_generic_p x2) {
433
int x1_generic_number, x2_generic_number;
435
osl_generic_p backup_x2 = x2;
440
// Check whether the number of generics is the same or not.
441
x1_generic_number = osl_generic_count(x1);
442
x2_generic_number = osl_generic_count(x2);
443
if (x1_generic_number != x2_generic_number)
446
// Check that for each generic in x1 a similar generic is in x2.
450
while ((x2 != NULL) && (found != 1)) {
451
if (osl_interface_equal(x1->interface, x2->interface)) {
452
if (x1->interface != NULL) {
453
equal = x1->interface->equal(x1->data, x2->data);
456
OSL_warning("unregistered generic, "
457
"cannot state generic equality");
481
* osl_generic_has_URI function:
482
* this function returns 1 if the generic provided as parameter has
483
* a given URI, 0 other wise.
484
* \param x The generic structure to test.
485
* \param URI The URI value to test.
486
* \return 1 if x has the provided URI, 0 otherwise.
488
int osl_generic_has_URI(osl_generic_p x, char * URI) {
491
(x->interface == NULL) ||
492
(x->interface->URI == NULL) ||
493
(strcmp(x->interface->URI, URI)))
501
* osl_generic_lookup function:
502
* this function returns the first generic with a given URI in the
503
* generic list provided as parameter and NULL if it doesn't find such
505
* \param x The generic list where to search a given generic URI.
506
* \param URI The URI of the generic we are looking for.
507
* \return The first generic of the requested URI in the list.
509
void * osl_generic_lookup(osl_generic_p x, char * URI) {
511
if (osl_generic_has_URI(x, URI))