Merge branch 'master' of github.com:periscop/openscop
[openscop.git] / source / interface.c
blobea0987a487547911f78cf52fdf62397646a606b6
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
4 **-----------------------------------------------------------------**
5 ** interface.c **
6 **-----------------------------------------------------------------**
7 ** First version: 15/07/2011 **
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 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
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> and *
59 * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr> *
60 * *
61 *****************************************************************************/
63 #include <stdlib.h>
64 #include <stdio.h>
65 #include <string.h>
66 #include <osl/extensions/textual.h>
67 #include <osl/extensions/comment.h>
68 #include <osl/extensions/null.h>
69 #include <osl/extensions/scatnames.h>
70 #include <osl/extensions/arrays.h>
71 #include <osl/extensions/coordinates.h>
72 #include <osl/extensions/clay.h>
73 #include <osl/extensions/dependence.h>
74 #include <osl/extensions/symbols.h>
75 #include <osl/extensions/irregular.h>
76 #include <osl/extensions/extbody.h>
77 #include <osl/extensions/loop.h>
78 #include <osl/extensions/pluto_unroll.h>
79 #include <osl/strings.h>
80 #include <osl/body.h>
81 #include <osl/relation.h>
82 #include <osl/interface.h>
85 /*+***************************************************************************
86 * Structure display function *
87 *****************************************************************************/
90 /**
91 * osl_interface_idump function:
92 * this function displays an osl_interface_t structure (*interface) into
93 * a file (file, possibly stdout) in a way that trends to be understandable.
94 * It includes an indentation level (level) in order to work with others
95 * idump functions.
96 * \param file The file where the information has to be printed.
97 * \param interface The interface structure which has to be printed.
98 * \param level Number of spaces before printing, for each line.
100 void osl_interface_idump(FILE * file, osl_interface_p interface, int level) {
101 int j, first = 1;
103 // Go to the right level.
104 for (j = 0; j < level; j++)
105 fprintf(file, "|\t");
107 if (interface != NULL)
108 fprintf(file, "+-- osl_interface_t: URI = %s\n", interface->URI);
109 else
110 fprintf(file, "+-- NULL interface\n");
113 while (interface != NULL) {
114 if (!first) {
115 // Go to the right level.
116 for (j = 0; j < level; j++)
117 fprintf(file, "|\t");
119 if (interface->URI != NULL)
120 fprintf(file, "| osl_interface_t: URI = %s\n", interface->URI);
121 else
122 fprintf(file, "| osl_interface_t: URI = (NULL)\n");
124 else
125 first = 0;
127 interface = interface->next;
129 // Next line.
130 if (interface != NULL) {
131 for (j = 0; j <= level + 1; j++)
132 fprintf(file, "|\t");
133 fprintf(file, "\n");
134 for (j = 0; j <= level; j++)
135 fprintf(file, "|\t");
136 fprintf(file, "V\n");
140 // The last line.
141 for (j = 0; j <= level; j++)
142 fprintf(file, "|\t");
143 fprintf(file, "\n");
148 * osl_interface_dump function:
149 * this function prints the content of a osl_interface_t structure
150 * (*interface) into a file (file, possibly stdout).
151 * \param file File where informations are printed.
152 * \param interface The interface structure to print.
154 void osl_interface_dump(FILE * file, osl_interface_p interface) {
155 osl_interface_idump(file, interface, 0);
159 /*****************************************************************************
160 * Reading function *
161 *****************************************************************************/
164 /*+***************************************************************************
165 * Memory allocation/deallocation function *
166 *****************************************************************************/
170 * osl_interface_add function:
171 * this function adds an interface node (it may be a list as well) to a
172 * list of interfaces provided as parameter (list). The new node
173 * is inserted at the end of the list.
174 * \param list The list of interfaces to add a node (NULL if empty).
175 * \param interface The interface to add to the list.
177 void osl_interface_add(osl_interface_p * list, osl_interface_p interface) {
178 osl_interface_p tmp = *list, check_interface;
180 if (interface != NULL) {
181 // First, check that the interface list is OK.
182 check_interface = interface;
183 while (check_interface != NULL) {
184 if (check_interface->URI == NULL)
185 OSL_error("no URI in an interface to add to a list");
187 if (osl_interface_lookup(*list, check_interface->URI) != NULL)
188 OSL_error("only one interface with a given URI is allowed");
189 check_interface = check_interface->next;
192 if (*list != NULL) {
193 while (tmp->next != NULL)
194 tmp = tmp->next;
195 tmp->next = interface;
197 else {
198 *list = interface;
205 * osl_interface_malloc function:
206 * This function allocates the memory space for a osl_interface_t
207 * structure and sets its fields with default values. Then it returns a
208 * pointer to the allocated space.
209 * \return A pointer to an empty interface structure with fields set to
210 * default values.
212 osl_interface_p osl_interface_malloc() {
213 osl_interface_p interface;
215 OSL_malloc(interface, osl_interface_p,
216 sizeof(osl_interface_t));
217 interface->URI = NULL;
218 interface->idump = NULL;
219 interface->sprint = NULL;
220 interface->sread = NULL;
221 interface->malloc = NULL;
222 interface->free = NULL;
223 interface->clone = NULL;
224 interface->equal = NULL;
225 interface->next = NULL;
227 return interface;
232 * osl_interface_free function:
233 * this function frees the allocated memory for an osl_interface_t
234 * structure, and all the interfaces stored in the list.
235 * \param[in] interface The pointer to the interface we want to free.
237 void osl_interface_free(osl_interface_p interface) {
238 osl_interface_p tmp;
239 int i = 0;
241 while (interface != NULL) {
242 tmp = interface->next;
243 if (interface->URI != NULL)
244 free(interface->URI);
245 free(interface);
246 interface = tmp;
247 i++;
252 /*+***************************************************************************
253 * Processing functions *
254 *****************************************************************************/
258 * osl_interface_number function:
259 * this function returns the number of statements in the interface list
260 * provided as parameter.
261 * \param[in] interface The first element of the interface list.
262 * \return The number of statements in the interface list.
264 int osl_interface_number(osl_interface_p interface) {
265 int number = 0;
267 while (interface != NULL) {
268 number++;
269 interface = interface->next;
271 return number;
276 * osl_interface_nclone function:
277 * This function builds and returns a "hard copy" (not a pointer copy) of the
278 * n first elements of an osl_interface_t list.
279 * \param interface The pointer to the interface structure we want to clone.
280 * \param n The number of nodes we want to copy (-1 for infinity).
281 * \return The clone of the n first nodes of the interface list.
283 osl_interface_p osl_interface_nclone(osl_interface_p interface, int n) {
284 osl_interface_p clone = NULL, new;
285 int i = 0;
287 while ((interface != NULL) && ((n == -1) || (i < n))) {
288 new = osl_interface_malloc();
289 OSL_strdup(new->URI, interface->URI);
290 new->idump = interface->idump;
291 new->sprint = interface->sprint;
292 new->sread = interface->sread;
293 new->malloc = interface->malloc;
294 new->free = interface->free;
295 new->clone = interface->clone;
296 new->equal = interface->equal;
298 osl_interface_add(&clone, new);
299 interface = interface->next;
300 i++;
303 return clone;
308 * osl_interface_clone function:
309 * This function builds and returns a "hard copy" (not a pointer copy) of an
310 * osl_interface_t data structure.
311 * \param interface The pointer to the interface structure we want to copy.
312 * \return A pointer to the copy of the interface structure.
314 osl_interface_p osl_interface_clone(osl_interface_p interface) {
316 return osl_interface_nclone(interface, -1);
321 * osl_interface_equal function:
322 * this function returns true if the two interface structures are the same,
323 * (content-wise) false otherwise.
324 * \param interface1 The first interface structure.
325 * \param interface2 The second interface structure.
326 * \return 1 if interface1 and interface2 are the same, 0 otherwise.
328 int osl_interface_equal(osl_interface_p interface1,
329 osl_interface_p interface2) {
331 if (interface1 == interface2)
332 return 1;
334 if (((interface1 == NULL) && (interface2 != NULL)) ||
335 ((interface1 != NULL) && (interface2 == NULL)))
336 return 0;
338 if (strcmp(interface1->URI, interface2->URI) ||
339 (interface1->idump != interface2->idump) ||
340 (interface1->sprint != interface2->sprint) ||
341 (interface1->sread != interface2->sread) ||
342 (interface1->malloc != interface2->malloc) ||
343 (interface1->free != interface2->free) ||
344 (interface1->clone != interface2->clone) ||
345 (interface1->equal != interface2->equal))
346 return 0;
348 return 1;
353 * osl_interface_lookup function:
354 * this function returns the first interface with a given URI in the
355 * interface list provided as parameter and NULL if it doesn't find such
356 * an interface.
357 * \param list The interface list where to search a given interface URI.
358 * \param URI The URI of the interface we are looking for.
359 * \return The first interface of the requested URI in the list.
361 osl_interface_p
362 osl_interface_lookup(osl_interface_p list, char * URI) {
364 if (URI == NULL) {
365 OSL_warning("lookup for a NULL URI");
367 else {
368 while (list != NULL) {
369 if ((list->URI != NULL) && (!strcmp(list->URI, URI)))
370 return list;
372 list = list->next;
376 return NULL;
381 * osl_interface_get_default_registry function:
382 * this function creates the list of known interfaces (of all generic types,
383 * including extensions) and returns it.
384 * \return The list of known interfaces.
386 osl_interface_p osl_interface_get_default_registry() {
387 osl_interface_p registry = NULL;
389 // Internal generics
390 osl_interface_add(&registry, osl_strings_interface());
391 osl_interface_add(&registry, osl_body_interface());
392 osl_interface_add(&registry, osl_relation_interface());
394 // Extensions
395 osl_interface_add(&registry, osl_textual_interface());
396 osl_interface_add(&registry, osl_comment_interface());
397 osl_interface_add(&registry, osl_null_interface());
398 osl_interface_add(&registry, osl_scatnames_interface());
399 osl_interface_add(&registry, osl_arrays_interface());
400 osl_interface_add(&registry, osl_coordinates_interface());
401 osl_interface_add(&registry, osl_clay_interface());
402 osl_interface_add(&registry, osl_dependence_interface());
403 osl_interface_add(&registry, osl_symbols_interface());
404 osl_interface_add(&registry, osl_extbody_interface());
405 osl_interface_add(&registry, osl_loop_interface());
406 osl_interface_add(&registry, osl_pluto_unroll_interface());
407 //osl_interface_add(&registry, osl_irregular_interface());
409 return registry;