2 /*+-----------------------------------------------------------------**
4 **-----------------------------------------------------------------**
6 **-----------------------------------------------------------------**
7 ** First version: 30/04/2008 **
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/scop.h>
70 /*+***************************************************************************
71 * Structure display functions *
72 *****************************************************************************/
76 * osl_scop_idump function:
77 * this function displays an osl_scop_t structure (*scop) into a
78 * file (file, possibly stdout) in a way that trends to be understandable. It
79 * includes an indentation level (level) in order to work with others
80 * print_structure functions.
81 * \param file The file where the information has to be printed.
82 * \param scop The scop structure whose information has to be printed.
83 * \param level Number of spaces before printing, for each line.
85 void osl_scop_idump(FILE * file
, osl_scop_p scop
, int level
) {
88 // Go to the right level.
89 for (j
= 0; j
< level
; j
++)
93 fprintf(file
, "+-- osl_scop_t\n");
95 fprintf(file
, "+-- NULL scop\n");
97 while (scop
!= NULL
) {
99 // Go to the right level.
100 for (j
= 0; j
< level
; j
++)
101 fprintf(file
, "|\t");
102 fprintf(file
, "| osl_scop_t\n");
108 for (j
= 0; j
<= level
+1; j
++)
109 fprintf(file
, "|\t");
112 // Print the version.
113 for (j
= 0; j
< level
; j
++)
114 fprintf(file
, "|\t");
115 fprintf(file
, "|\tVersion: %d\n", scop
->version
);
118 for (j
= 0; j
<= level
+1; j
++)
119 fprintf(file
, "|\t");
122 // Print the language.
123 for (j
= 0; j
< level
; j
++)
124 fprintf(file
, "|\t");
125 fprintf(file
, "|\tLanguage: %s\n", scop
->language
);
128 for (j
= 0; j
<= level
+1; j
++)
129 fprintf(file
, "|\t");
132 // Print the context of the scop.
133 osl_relation_idump(file
, scop
->context
, level
+1);
135 // Print the parameters.
136 osl_generic_idump(file
, scop
->parameters
, level
+1);
138 // Print the statements.
139 osl_statement_idump(file
, scop
->statement
, level
+1);
141 // Print the registered extension interfaces.
142 osl_interface_idump(file
, scop
->registry
, level
+1);
144 // Print the extensions.
145 osl_generic_idump(file
, scop
->extension
, level
+1);
151 for (j
= 0; j
<= level
; j
++)
152 fprintf(file
, "|\t");
153 fprintf(file
, "V\n");
158 for (j
= 0; j
<= level
; j
++)
159 fprintf(file
, "|\t");
165 * osl_scop_dump function:
166 * this function prints the content of an osl_scop_t structure (*scop)
167 * into a file (file, possibly stdout).
168 * \param file The file where the information has to be printed.
169 * \param scop The scop structure whose information has to be printed.
171 void osl_scop_dump(FILE * file
, osl_scop_p scop
) {
172 osl_scop_idump(file
, scop
, 0);
178 * osl_scop_name_limits function:
179 * this function finds the (maximum) number of various elements of a scop and
180 * return the values through parameters. To ensure the correctness of the
181 * results, an integrity check of the input scop should be run before calling
183 * \param scop The scop to analyse.
184 * \parem nb_parameters The number of parameters in the scop (output).
185 * \parem nb_iterators The number of iterators in the scop (output).
186 * \parem nb_scattdims The number of scattdims in the scop (output).
187 * \parem nb_localdims The number of local dimensions in the scop (output).
188 * \parem nb_arrays The number of arrays in the scop (output).
191 void osl_scop_name_limits(osl_scop_p scop
,
198 osl_statement_p statement
;
199 osl_relation_list_p list
;
201 // * The number of parameters is collected from the context,
202 // * The numbers of local dimensions are collected from all relations
203 // in the corresponding field.
206 if (scop
->context
!= NULL
) {
207 *nb_parameters
= scop
->context
->nb_parameters
;
208 *nb_localdims
= scop
->context
->nb_local_dims
;
214 statement
= scop
->statement
;
215 while (statement
!= NULL
) {
216 // * The number of iterators are defined by iteration domains,
217 // it corresponds to the #output_dims.
218 if (statement
->domain
!= NULL
) {
219 if (statement
->domain
->nb_output_dims
> *nb_iterators
)
220 *nb_iterators
= statement
->domain
->nb_output_dims
;
222 if (statement
->domain
->nb_local_dims
> *nb_localdims
)
223 *nb_localdims
= statement
->domain
->nb_local_dims
;
226 // * The number of scattdims are defined by scattering,
227 // it corresponds to the #output_dims.
228 if (statement
->scattering
!= NULL
) {
229 if (statement
->scattering
->nb_output_dims
> *nb_scattdims
)
230 *nb_scattdims
= statement
->scattering
->nb_output_dims
;
232 if (statement
->scattering
->nb_local_dims
> *nb_localdims
)
233 *nb_localdims
= statement
->scattering
->nb_local_dims
;
236 // * The number of arrays are defined by accesses,
237 list
= statement
->access
;
238 while (list
!= NULL
) {
239 array_id
= osl_relation_get_array_id(list
->elt
);
240 if (array_id
> *nb_arrays
)
241 *nb_arrays
= array_id
;
246 statement
= statement
->next
;
252 * osl_scop_full_names function:
253 * this function generates an osl_names_p structure which contains
254 * enough names for the scop provided as parameter, for each kind of names.
255 * If the names contained in the input scop are not sufficient, this function
256 * generated the missing names.
257 * \param scop The scop we need a name for each element.
258 * \return A set of names for the scop.
261 osl_names_p
osl_scop_full_names(osl_scop_p scop
) {
270 names
= osl_names_clone(scop
->names
);
272 // Extract array names information from extensions.
273 osl_strings_free(names
->arrays
, names
->nb_arrays
);
274 arrays
= (osl_arrays_p
)osl_extension_lookup(scop
->extension
,
275 OSL_EXTENSION_ARRAYS
);
276 names
->arrays
= osl_arrays_generate_names(arrays
,
277 &(names
->nb_arrays
));
279 // Complete names if necessary.
280 osl_scop_name_limits(scop
, &nb_parameters
,
286 osl_strings_complete(&names
->parameters
, &names
->nb_parameters
,
287 "P_", nb_parameters
);
289 osl_strings_complete(&names
->iterators
, &names
->nb_iterators
,
292 osl_strings_complete(&names
->scattdims
, &names
->nb_scattdims
,
295 osl_strings_complete(&names
->localdims
, &names
->nb_localdims
,
298 osl_strings_complete(&names
->arrays
, &names
->nb_arrays
,
307 * osl_scop_print function:
308 * this function prints the content of an osl_scop_t structure (*scop)
309 * into a file (file, possibly stdout) in the OpenScop textual format.
310 * \param file The file where the information has to be printed.
311 * \param scop The scop structure whose information has to be printed.
313 void osl_scop_print(FILE * file
, osl_scop_p scop
) {
316 fprintf(file
, "# NULL scop\n");
320 fprintf(file
, "# [File generated by the OpenScop Library %s %s bits]\n",
321 OSL_RELEASE
,OSL_VERSION
);
324 if (osl_scop_integrity_check(scop
) == 0)
325 OSL_warning("OpenScop integrity check failed. "
326 "Something may go wrong.");
328 while (scop
!= NULL
) {
329 fprintf(file
, "\n"OSL_TAG_START_SCOP
"\n\n");
330 fprintf(file
, "# =============================================== "
332 fprintf(file
, "# Language\n");
333 fprintf(file
, "%s\n\n", scop
->language
);
335 fprintf(file
, "# Context\n");
336 osl_relation_print(file
, scop
->context
);
339 osl_util_print_provided(file
,
340 osl_generic_hasURI(scop
->parameters
, OSL_URI_STRINGS
),
342 osl_generic_print(file
, scop
->parameters
);
344 fprintf(file
, "\n# Number of statements\n");
345 fprintf(file
, "%d\n\n",osl_statement_number(scop
->statement
));
347 osl_statement_print(file
, scop
->statement
);
349 if (scop
->extension
) {
350 fprintf(file
, "# =============================================== "
352 osl_generic_print(file
, scop
->extension
);
354 fprintf(file
, "\n"OSL_TAG_END_SCOP
"\n\n");
361 /*****************************************************************************
363 *****************************************************************************/
367 * osl_scop_pread function ("precision read"):
368 * this function reads a list of scop structures from a file (possibly stdin)
369 * complying to the OpenScop textual format and returns a pointer to this
370 * scop list. If some relation properties (number of input/output/local
371 * dimensions and number of parameters) are undefined, it will define them
372 * according to the available information.
373 * \param[in] file The file where the scop has to be read.
374 * \param[in] precision The precision of the relation elements.
375 * \return A pointer to the scop structure that has been read.
377 osl_scop_p
osl_scop_pread(FILE * file
, int precision
) {
378 osl_scop_p list
= NULL
, current
= NULL
, scop
;
379 osl_statement_p stmt
= NULL
;
380 osl_statement_p prev
= NULL
;
381 osl_interface_p interface
;
382 osl_strings_p language
;
384 char buffer
[OSL_MAX_STRING
];
396 tmp
= osl_util_read_uptotag(file
, OSL_TAG_START_SCOP
);
399 OSL_info("no more scop in the file");
403 scop
= osl_scop_malloc();
404 osl_scop_register_default_extensions(scop
);
410 // Read the language.
411 language
= osl_strings_read(file
);
412 if (osl_strings_size(language
) == 0)
413 OSL_error("no language (backend) specified");
415 if (osl_strings_size(language
) > 1)
416 OSL_warning("uninterpreted information (after language)");
418 if (language
!= NULL
) {
419 scop
->language
= strdup(language
->string
[0]);
420 osl_strings_free(language
);
423 // Read the context domain.
424 scop
->context
= osl_relation_pread(file
, precision
);
426 // Read the parameters.
427 scop
->parameter_type
= OSL_TYPE_STRING
;
428 if (osl_util_read_int(file
, NULL
) > 0) {
429 interface
= osl_strings_interface();
430 start
= osl_util_skip_blank_and_comments(file
, buffer
);
431 scop
->parameters
= osl_generic_sread(start
, interface
);
432 osl_interface_free(interface
);
436 // III. STATEMENT PART
439 // Read the number of statements.
440 nb_statements
= osl_util_read_int(file
, NULL
);
442 for (i
= 0; i
< nb_statements
; i
++) {
443 // Read each statement.
444 stmt
= osl_statement_pread(file
, precision
);
445 if (scop
->statement
== NULL
)
446 scop
->statement
= stmt
;
453 // IV. EXTENSION PART (TO THE END TAG)
456 // Read up the end tag (if any), and store extensions.
457 scop
->extension
= osl_generic_read(file
, scop
->registry
);
459 // Add the new scop to the list.
465 current
->next
= scop
;
470 if (!osl_scop_integrity_check(list
))
471 OSL_warning("scop integrity check failed");
478 * osl_scop_read function:
479 * this function is equivalent to osl_scop_pread() except that
480 * the precision corresponds to the precision environment variable or
481 * to the highest available precision if it is not defined.
482 * \see{osl_scop_pread}
484 osl_scop_p
osl_scop_read(FILE * foo
) {
485 int precision
= osl_util_get_precision();
486 return osl_scop_pread(foo
, precision
);
490 /*+***************************************************************************
491 * Memory allocation/deallocation functions *
492 *****************************************************************************/
496 * osl_scop_malloc function:
497 * this function allocates the memory space for a osl_scop_t structure and
498 * sets its fields with default values. Then it returns a pointer to the
500 * \return A pointer to an empty scop with fields set to default values.
502 osl_scop_p
osl_scop_malloc() {
505 OSL_malloc(scop
, osl_scop_p
, sizeof(osl_scop_t
));
507 scop
->language
= NULL
;
508 scop
->context
= NULL
;
509 scop
->parameter_type
= OSL_UNDEFINED
;
510 scop
->parameters
= NULL
;
511 scop
->statement
= NULL
;
512 scop
->registry
= NULL
;
513 scop
->extension
= NULL
;
522 * osl_scop_free function:
523 * This function frees the allocated memory for a osl_scop_t structure.
524 * \param scop The pointer to the scop we want to free.
526 void osl_scop_free(osl_scop_p scop
) {
529 while (scop
!= NULL
) {
530 if (scop
->language
!= NULL
)
531 free(scop
->language
);
532 osl_generic_free(scop
->parameters
);
533 osl_relation_free(scop
->context
);
534 osl_statement_free(scop
->statement
);
535 osl_interface_free(scop
->registry
);
536 osl_generic_free(scop
->extension
);
545 /*+***************************************************************************
546 * Processing functions *
547 *****************************************************************************/
551 * osl_scop_register_default_extensions function:
552 * this function registers the default OpenScop Library extensions to an
554 * \param scop The scop for which default options have to be registered.
556 void osl_scop_register_default_extensions(osl_scop_p scop
) {
558 osl_interface_add(&(scop
->registry
), osl_textual_interface());
559 osl_interface_add(&(scop
->registry
), osl_comment_interface());
560 osl_interface_add(&(scop
->registry
), osl_arrays_interface());
561 osl_interface_add(&(scop
->registry
), osl_lines_interface());
562 osl_interface_add(&(scop
->registry
), osl_irregular_interface());
567 * osl_scop_clone function:
568 * This functions builds and returns a "hard copy" (not a pointer copy)
569 * of a osl_statement_t data structure provided as parameter.
570 * Note that the usr field is not touched by this function.
571 * \param statement The pointer to the scop we want to clone.
572 * \return A pointer to the full clone of the scop provided as parameter.
574 osl_scop_p
osl_scop_clone(osl_scop_p scop
) {
575 osl_scop_p clone
= NULL
, node
, previous
= NULL
;
578 while (scop
!= NULL
) {
579 node
= osl_scop_malloc();
580 node
->version
= scop
->version
;
581 if (scop
->language
!= NULL
)
582 node
->language
= strdup(scop
->language
);
583 node
->context
= osl_relation_clone(scop
->context
);
584 node
->parameter_type
= scop
->parameter_type
;
585 node
->parameters
= osl_generic_clone(scop
->parameters
);
586 node
->statement
= osl_statement_clone(scop
->statement
);
587 node
->registry
= osl_interface_clone(scop
->registry
);
588 node
->extension
= osl_generic_clone(scop
->extension
);
596 previous
->next
= node
;
597 previous
= previous
->next
;
608 * osl_scop_equal function:
609 * this function returns true if the two scops are the same, false
610 * otherwise (the usr field is not tested).
611 * \param s1 The first scop.
612 * \param s2 The second scop.
613 * \return 1 if s1 and s2 are the same (content-wise), 0 otherwise.
615 int osl_scop_equal(osl_scop_p s1
, osl_scop_p s2
) {
617 while ((s1
!= NULL
) && (s2
!= NULL
)) {
621 if (s1
->version
!= s2
->version
) {
622 OSL_info("versions are not the same");
626 if (strcmp(s1
->language
, s2
->language
) != 0) {
627 OSL_info("languages are not the same");
631 if (!osl_relation_equal(s1
->context
, s2
->context
)) {
632 OSL_info("contexts are not the same");
636 if (s1
->parameter_type
!= s2
->parameter_type
) {
637 OSL_info("parameter types are not the same");
641 if (!osl_generic_equal(s1
->parameters
, s2
->parameters
)) {
642 OSL_info("parameters are not the same");
646 if (!osl_statement_equal(s1
->statement
, s2
->statement
)) {
647 OSL_info("statements are not the same");
651 if (!osl_interface_equal(s1
->registry
, s2
->registry
)) {
652 OSL_info("registries are not the same");
656 if (!osl_generic_equal(s1
->extension
, s2
->extension
)) {
657 OSL_info("extensions are not the same");
665 if (((s1
== NULL
) && (s2
!= NULL
)) || ((s1
!= NULL
) && (s2
== NULL
)))
673 * osl_scop_integrity_check function:
674 * This function checks that a scop is "well formed". It returns 0 if the
675 * check failed or 1 if no problem has been detected.
676 * \param scop The scop we want to check.
677 * \return 0 if the integrity check fails, 1 otherwise.
679 int osl_scop_integrity_check(osl_scop_p scop
) {
680 int expected_nb_parameters
;
683 while (scop
!= NULL
) {
684 // Check the language.
685 if ((scop
->language
!= NULL
) &&
686 (!strcmp(scop
->language
, "caml") || !strcmp(scop
->language
, "Caml") ||
687 !strcmp(scop
->language
, "ocaml") || !strcmp(scop
->language
, "OCaml")))
688 fprintf(stderr
, "[OpenScop] Alert: What ?! Caml ?! Are you sure ?!?!\n");
690 // Check the context.
691 if (!osl_relation_integrity_check(scop
->context
,
698 // Get the number of parameters.
699 if (scop
->context
!= NULL
)
700 expected_nb_parameters
= scop
->context
->nb_parameters
;
702 expected_nb_parameters
= OSL_UNDEFINED
;
704 // TODO : check the number of parameter strings.
706 if (!osl_statement_integrity_check(scop
->statement
,
707 expected_nb_parameters
))
718 * osl_scop_get_nb_parameters function:
719 * this function returns the number of global parameters of a given SCoP.
720 * \param scop The scop we want to know the number of global parameters.
721 * \return The number of global parameters in the scop.
723 int osl_scop_get_nb_parameters(osl_scop_p scop
) {
725 if (scop
->context
== NULL
) {
726 OSL_warning("no context domain, assuming 0 parameters");
730 return scop
->context
->nb_parameters
;
736 * osl_scop_register_extension function:
737 * this function registers a list of extension interfaces to a scop, i.e., it
738 * adds them to the scop registry. In addition, it will extract extensions
739 * corresponding to those interfaces from the textual form of the extensions
740 * (if any) and add them to the scop extension list.
741 * \param scop The scop for which an extension has to be registered.
742 * \param interface The extension interface to register within the scop.
744 void osl_scop_register_extension(osl_scop_p scop
, osl_interface_p interface
) {
745 osl_generic_p textual
, new;
746 char * extension_string
;
748 if ((interface
!= NULL
) && (scop
!= NULL
)) {
749 osl_interface_add(&scop
->registry
, interface
);
751 textual
= osl_generic_lookup(scop
->extension
, interface
->URI
);
752 if (textual
!= NULL
) {
753 extension_string
= ((osl_textual_p
)textual
->data
)->textual
;
754 new = osl_generic_sread(extension_string
, interface
);
755 osl_generic_add(&scop
->extension
, new);