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 <openscop/scop.h>
70 /*+***************************************************************************
71 * Structure display functions *
72 *****************************************************************************/
76 * openscop_scop_idump function:
77 * this function displays an openscop_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 openscop_scop_idump(FILE * file
, openscop_scop_p scop
, int level
) {
88 // Go to the right level.
89 for (j
= 0; j
< level
; j
++)
93 fprintf(file
, "+-- openscop_scop_t\n");
96 for (j
= 0; j
<= level
+1; j
++)
100 // Print the version.
101 for (j
= 0; j
< level
; j
++)
102 fprintf(file
, "|\t");
103 fprintf(file
, "|\tVersion: %d\n", scop
->version
);
106 for (j
= 0; j
<= level
+1; j
++)
107 fprintf(file
, "|\t");
110 // Print the language.
111 for (j
= 0; j
< level
; j
++)
112 fprintf(file
, "|\t");
113 fprintf(file
, "|\tLanguage: %s\n", scop
->language
);
116 for (j
= 0; j
<= level
+1; j
++)
117 fprintf(file
, "|\t");
120 // Print the context of the scop.
121 openscop_relation_idump(file
, scop
->context
, level
+1);
123 // Print the parameters.
124 openscop_util_strings_idump(file
,
125 (scop
->parameter_type
== OPENSCOP_TYPE_STRING
) ?
126 (char **)scop
->parameter
: NULL
,
127 (scop
->context
!= NULL
) ? scop
->context
->nb_parameters
: 0,
128 level
, "parameters");
130 // Print the statements.
131 openscop_statement_idump(file
, scop
->statement
, level
+1);
133 // Print the extensions.
134 openscop_extension_idump(file
, scop
->extension
, level
+1);
137 for (j
= 0; j
<= level
+1; j
++)
138 fprintf(file
, "|\t");
142 fprintf(file
, "+-- NULL scop\n");
146 for (j
= 0; j
<= level
; j
++)
147 fprintf(file
, "|\t");
153 * openscop_scop_dump function:
154 * this function prints the content of an openscop_scop_t structure (*scop)
155 * into a file (file, possibly stdout).
156 * \param file The file where the information has to be printed.
157 * \param scop The scop structure whose information has to be printed.
159 void openscop_scop_dump(FILE * file
, openscop_scop_p scop
) {
160 openscop_scop_idump(file
, scop
, 0);
166 * openscop_scop_name_limits function:
167 * this function finds the (maximum) number of various elements of a scop and
168 * return the values through parameters. To ensure the correctness of the
169 * results, an integrity check of the input scop should be run before calling
171 * \param scop The scop to analyse.
172 * \parem nb_parameters The number of parameters in the scop (output).
173 * \parem nb_iterators The number of iterators in the scop (output).
174 * \parem nb_scattdims The number of scattdims in the scop (output).
175 * \parem nb_localdims The number of local dimensions in the scop (output).
176 * \parem nb_arrays The number of arrays in the scop (output).
179 void openscop_scop_name_limits(openscop_scop_p scop
,
186 openscop_statement_p statement
;
187 openscop_relation_list_p list
;
189 // * The number of parameters is collected from the context,
190 // * The numbers of local dimensions are collected from all relations
191 // in the corresponding field.
194 if (scop
->context
!= NULL
) {
195 *nb_parameters
= scop
->context
->nb_parameters
;
196 *nb_localdims
= scop
->context
->nb_local_dims
;
202 statement
= scop
->statement
;
203 while (statement
!= NULL
) {
204 // * The number of iterators are defined by iteration domains,
205 // it corresponds to the #output_dims.
206 if (statement
->domain
!= NULL
) {
207 if (statement
->domain
->nb_output_dims
> *nb_iterators
)
208 *nb_iterators
= statement
->domain
->nb_output_dims
;
210 if (statement
->domain
->nb_local_dims
> *nb_localdims
)
211 *nb_localdims
= statement
->domain
->nb_local_dims
;
214 // * The number of scattdims are defined by scattering,
215 // it corresponds to the #output_dims.
216 if (statement
->scattering
!= NULL
) {
217 if (statement
->scattering
->nb_output_dims
> *nb_scattdims
)
218 *nb_scattdims
= statement
->scattering
->nb_output_dims
;
220 if (statement
->scattering
->nb_local_dims
> *nb_localdims
)
221 *nb_localdims
= statement
->scattering
->nb_local_dims
;
224 // * The number of arrays are defined by accesses,
225 list
= statement
->access
;
226 while (list
!= NULL
) {
227 array_id
= openscop_relation_get_array_id(list
->elt
);
228 if (array_id
> *nb_arrays
)
229 *nb_arrays
= array_id
;
234 statement
= statement
->next
;
240 * openscop_scop_full_names function:
241 * this function generates an openscop_names_p structure which contains
242 * enough names for the scop provided as parameter, for each kind of names.
243 * If the names contained in the input scop are not sufficient, this function
244 * generated the missing names.
245 * \param scop The scop we need a name for each element.
246 * \return A set of names for the scop.
249 openscop_names_p
openscop_scop_full_names(openscop_scop_p scop
) {
255 openscop_arrays_p arrays
;
256 openscop_names_p names
;
258 names
= openscop_names_copy(scop
->names
);
260 // Extract array names information from extensions.
261 openscop_util_strings_free(names
->arrays
, names
->nb_arrays
);
262 arrays
= (openscop_arrays_p
)openscop_extension_lookup(scop
->extension
,
263 OPENSCOP_EXTENSION_ARRAYS
);
264 names
->arrays
= openscop_arrays_generate_names(arrays
,
265 &(names
->nb_arrays
));
267 // Complete names if necessary.
268 openscop_scop_name_limits(scop
, &nb_parameters
,
274 openscop_util_strings_complete(&names
->parameters
, &names
->nb_parameters
,
275 "P_", nb_parameters
);
277 openscop_util_strings_complete(&names
->iterators
, &names
->nb_iterators
,
280 openscop_util_strings_complete(&names
->scattdims
, &names
->nb_scattdims
,
283 openscop_util_strings_complete(&names
->localdims
, &names
->nb_localdims
,
286 openscop_util_strings_complete(&names
->arrays
, &names
->nb_arrays
,
294 * openscop_scop_print function:
295 * this function prints the content of an openscop_scop_t structure (*scop)
296 * into a file (file, possibly stdout) in the OpenScop textual format.
297 * \param file The file where the information has to be printed.
298 * \param scop The scop structure whose information has to be printed.
300 void openscop_scop_print(FILE * file
, openscop_scop_p scop
) {
302 if (openscop_scop_integrity_check(scop
) == 0) {
303 fprintf(stderr
, "[OpenScop] Warning: OpenScop integrity check failed. "
304 " Something may go wrong.\n");
308 fprintf(file
, "# \n");
309 fprintf(file
, "# <| \n");
310 fprintf(file
, "# A \n");
311 fprintf(file
, "# /.\\ \n");
312 fprintf(file
, "# <| [\"\"M# \n");
313 fprintf(file
, "# A | # Clan McCloog Castle \n");
314 fprintf(file
, "# /.\\ [\"\"M# [Generated by the OpenScop ");
315 fprintf(file
, "Library %s %s bits]\n",OPENSCOP_RELEASE
, OPENSCOP_VERSION
);
316 fprintf(file
, "# [\"\"M# | # U\"U#U \n");
317 fprintf(file
, "# | # | # \\ .:/ \n");
318 fprintf(file
, "# | # | #___| # \n");
319 fprintf(file
, "# | \"--' .-\" \n");
320 fprintf(file
, "# |\"-\"-\"-\"-\"-#-#-## \n");
321 fprintf(file
, "# | # ## ###### \n");
322 fprintf(file
, "# \\ .::::'/ \n");
323 fprintf(file
, "# \\ ::::'/ \n");
324 fprintf(file
, "# :8a| # # ## \n");
325 fprintf(file
, "# ::88a ### \n");
326 fprintf(file
, "# ::::888a 8a ##::. \n");
327 fprintf(file
, "# ::::::888a88a[]:::: \n");
328 fprintf(file
, "# :::::::::SUNDOGa8a::::. .. \n");
329 fprintf(file
, "# :::::8::::888:Y8888:::::::::... \n");
330 fprintf(file
, "#::':::88::::888::Y88a______________________________");
331 fprintf(file
, "________________________\n");
332 fprintf(file
, "#:: ::::88a::::88a:Y88a ");
333 fprintf(file
, " __---__-- __\n");
334 fprintf(file
, "#' .: ::Y88a:::::8a:Y88a ");
335 fprintf(file
, "__----_-- -------_-__\n");
336 fprintf(file
, "# :' ::::8P::::::::::88aa. _ _- -");
337 fprintf(file
, "- --_ --- __ --- __--\n");
338 fprintf(file
, "#.:: :::::::::::::::::::Y88as88a...s88aa.\n");
341 fprintf(file
, "# [File generated by the OpenScop Library %s %s bits]\n",
342 OPENSCOP_RELEASE
,OPENSCOP_VERSION
);
345 fprintf(file
, "\nOpenScop\n\n");
346 fprintf(file
, "# =============================================== Global\n");
347 fprintf(file
, "# Language\n");
348 fprintf(file
, "%s\n\n", scop
->language
);
350 fprintf(file
, "# Context\n");
351 openscop_relation_print(file
, scop
->context
, NULL
);
354 openscop_util_strings_print(file
,
355 (char **)scop
->parameter
,
356 (scop
->context
!= NULL
) ? scop
->context
->nb_parameters
: 0,
357 (scop
->parameter_type
== OPENSCOP_TYPE_STRING
),
360 fprintf(file
, "# Number of statements\n");
361 fprintf(file
, "%d\n\n",openscop_statement_number(scop
->statement
));
363 openscop_statement_print(file
, scop
->statement
, NULL
);
365 if (scop
->extension
) {
366 fprintf(file
, "# ==============================================="
368 openscop_extension_print(file
, scop
->extension
);
373 /*****************************************************************************
375 *****************************************************************************/
379 * openscop_scop_read function:
380 * this function reads a scop structure from a file (possibly stdin)
381 * complying to the OpenScop textual format and returns a pointer to this
382 * scop structure. If some relation properties (number of input/output/local
383 * dimensions and number of parameters) are undefined, it will define them
384 * according to the available information.
385 * \param file The file where the scop has to be read.
386 * \return A pointer to the scop structure that has been read.
388 openscop_scop_p
openscop_scop_read(FILE * file
) {
389 openscop_scop_p scop
= NULL
;
390 openscop_statement_p stmt
= NULL
;
391 openscop_statement_p prev
= NULL
;
392 int nb_statements
, nb_parameters
;
400 scop
= openscop_scop_malloc();
406 // Ensure the file is a .scop.
407 tmp
= openscop_util_strings_read(file
, &max
);
408 if ((max
== 0) || (strcmp(*tmp
, "OpenScop"))) {
409 fprintf(stderr
, "[OpenScop] Error: not an OpenScop file "
410 "(type \"%s\".\n", *tmp
);
414 fprintf(stderr
, "[OpenScop] Warning: uninterpreted information "
415 "(after file type).\n");
419 // Read the language.
420 char ** language
= openscop_util_strings_read(file
, &max
);
422 fprintf(stderr
, "[OpenScop] Error: no language (backend) specified.\n");
426 fprintf(stderr
, "[OpenScop] Warning: uninterpreted information "
427 "(after language).\n");
428 scop
->language
= *language
;
432 scop
->context
= openscop_relation_read(file
);
434 // Read the parameters.
435 scop
->parameter_type
= OPENSCOP_TYPE_STRING
;
436 if (openscop_util_read_int(file
, NULL
) > 0) {
437 scop
->parameter
= (void**)openscop_util_strings_read(file
, &nb_parameters
);
438 if ((scop
->context
!= NULL
) &&
439 (nb_parameters
!= scop
->context
->nb_parameters
))
440 fprintf(stderr
, "[OpenScop] Warning: bad number of parameters.\n");
444 // II. STATEMENT PART
447 // Read the number of statements.
448 nb_statements
= openscop_util_read_int(file
, NULL
);
450 for (i
= 0; i
< nb_statements
; i
++) {
451 // Read each statement.
452 stmt
= openscop_statement_read(file
);
453 if (scop
->statement
== NULL
)
454 scop
->statement
= stmt
;
461 // III. EXTENSION PART
464 // Read the remainder of the file, and store it in the extension field.
465 scop
->extension
= openscop_extension_read(file
);
468 // VI. FINALIZE AND CHECK
470 if (!openscop_scop_integrity_check(scop
))
471 fprintf(stderr
, "[OpenScop] Warning: scop integrity check failed.\n");
477 /*+***************************************************************************
478 * Memory allocation/deallocation functions *
479 *****************************************************************************/
483 * openscop_scop_malloc function:
484 * this function allocates the memory space for a openscop_scop_t structure and
485 * sets its fields with default values. Then it returns a pointer to the
487 * \return A pointer to an empty scop with fields set to default values.
489 openscop_scop_p
openscop_scop_malloc() {
490 openscop_scop_p scop
;
492 scop
= (openscop_scop_p
)malloc(sizeof(openscop_scop_t
));
494 fprintf(stderr
, "[OpenScop] Memory Overflow.\n");
499 scop
->language
= NULL
;
500 scop
->context
= NULL
;
501 scop
->parameter_type
= OPENSCOP_UNDEFINED
;
502 scop
->parameter
= NULL
;
503 scop
->statement
= NULL
;
504 scop
->extension
= NULL
;
512 * openscop_scop_free function:
513 * This function frees the allocated memory for a openscop_scop_t structure.
514 * \param scop The pointer to the scop we want to free.
516 void openscop_scop_free(openscop_scop_p scop
) {
518 if (scop
->language
!= NULL
)
519 free(scop
->language
);
521 openscop_util_strings_free(
522 (char **)scop
->parameter
,
523 (scop
->context
!= NULL
) ? scop
->context
->nb_parameters
: 0);
524 openscop_relation_free(scop
->context
);
525 openscop_statement_free(scop
->statement
);
526 openscop_extension_free(scop
->extension
);
533 /*+***************************************************************************
534 * Processing functions *
535 *****************************************************************************/
539 * openscop_scop_copy function:
540 * This functions builds and returns a "hard copy" (not a pointer copy)
541 * of a openscop_statement_t data structure provided as parameter.
542 * Note that the usr field is not touched by this function.
543 * \param statement The pointer to the scop we want to copy.
544 * \return A pointer to the full copy of the scop provided as parameter.
546 openscop_scop_p
openscop_scop_copy(openscop_scop_p scop
) {
547 openscop_scop_p copy
;
549 copy
= openscop_scop_malloc();
550 copy
->version
= scop
->version
;
551 if (scop
->language
!= NULL
)
552 copy
->language
= strdup(scop
->language
);
553 copy
->context
= openscop_relation_copy(scop
->context
);
554 copy
->parameter_type
= scop
->parameter_type
;
555 copy
->parameter
= (void **)openscop_util_strings_copy(
556 (char **)scop
->parameter
,
557 (scop
->context
!= NULL
) ? scop
->context
->nb_parameters
: 0);
558 copy
->statement
= openscop_statement_copy(scop
->statement
);
559 copy
->extension
= openscop_extension_copy(scop
->extension
);
566 * openscop_scop_equal function:
567 * this function returns true if the two scops are the same, false
568 * otherwise (the usr field is not tested).
569 * \param s1 The first scop.
570 * \param s2 The second scop.
571 * \return 1 if s1 and s2 are the same (content-wise), 0 otherwise.
573 int openscop_scop_equal(openscop_scop_p s1
, openscop_scop_p s2
) {
578 if (s1
->version
!= s2
->version
) {
579 fprintf(stderr
, "[OpenScop] info: versions are not the same.\n");
583 if (strcmp(s1
->language
, s2
->language
) != 0) {
584 fprintf(stderr
, "[OpenScop] info: languages are not the same.\n");
588 if (!openscop_relation_equal(s1
->context
, s2
->context
)) {
589 fprintf(stderr
, "[OpenScop] info: contexts are not the same.\n");
593 if (s1
->parameter_type
!= s2
->parameter_type
) {
594 fprintf(stderr
, "[OpenScop] info: parameter types are not the same.\n");
598 if ((s1
->parameter_type
== OPENSCOP_TYPE_STRING
) &&
599 (!openscop_util_strings_equal(
600 (char **)s1
->parameter
,
601 (s1
->context
!= NULL
) ? s1
->context
->nb_parameters
: 0,
602 (char **)s2
->parameter
,
603 (s2
->context
!= NULL
) ? s2
->context
->nb_parameters
: 0))) {
604 fprintf(stderr
, "[OpenScop] info: parameters are not the same.\n");
608 if (!openscop_statement_equal(s1
->statement
, s2
->statement
)) {
609 fprintf(stderr
, "[OpenScop] info: statements are not the same.\n");
613 if (!openscop_extension_equal(s1
->extension
, s2
->extension
)) {
614 fprintf(stderr
, "[OpenScop] info: extensions are not the same.\n");
623 * openscop_scop_integrity_check function:
624 * This function checks that a scop is "well formed". It returns 0 if the
625 * check failed or 1 if no problem has been detected.
626 * \param scop The scop we want to check.
627 * \return 0 if the integrity check fails, 1 otherwise.
629 int openscop_scop_integrity_check(openscop_scop_p scop
) {
630 int expected_nb_parameters
;
632 // Check the language.
633 if ((scop
->language
!= NULL
) &&
634 (!strcmp(scop
->language
, "caml") || !strcmp(scop
->language
, "Caml") ||
635 !strcmp(scop
->language
, "ocaml") || !strcmp(scop
->language
, "OCaml")))
636 fprintf(stderr
, "[OpenScop] Alert: What ?! Caml ?! Are you sure ?!?!\n");
638 // Check the context.
639 if (!openscop_relation_integrity_check(scop
->context
,
640 OPENSCOP_TYPE_CONTEXT
,
646 // Get the number of parameters.
647 if (scop
->context
!= NULL
)
648 expected_nb_parameters
= scop
->context
->nb_parameters
;
650 expected_nb_parameters
= OPENSCOP_UNDEFINED
;
652 if (!openscop_statement_integrity_check(scop
->statement
,
653 expected_nb_parameters
))