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 *****************************************************************************/
68 # include <openscop/statement.h>
71 /*+***************************************************************************
72 * Structure display functions *
73 *****************************************************************************/
77 * openscop_statement_idump function:
78 * Displays a openscop_statement_t structure (*statement) into a file (file,
79 * possibly stdout) in a way that trends to be understandable without falling
80 * in a deep depression or, for the lucky ones, getting a headache... It
81 * includes an indentation level (level) in order to work with others
82 * print_structure functions.
83 * \param file File where informations are printed.
84 * \param statement The statement whose information have to be printed.
85 * \param level Number of spaces before printing, for each line.
87 void openscop_statement_idump(FILE * file
,
88 openscop_statement_p statement
,
90 int i
, j
, first
= 1, number
= 1;
92 // Go to the right level.
93 for (j
= 0; j
< level
; j
++)
96 if (statement
!= NULL
)
97 fprintf(file
, "+-- openscop_statement_t (S%d)\n", number
);
99 fprintf(file
, "+-- NULL statement\n");
101 while (statement
!= NULL
) {
103 // Go to the right level.
104 for (j
= 0; j
< level
; j
++)
105 fprintf(file
, "|\t");
106 fprintf(file
, "| openscop_statement_t (S%d)\n", number
);
112 for (j
= 0; j
<= level
+1; j
++)
113 fprintf(file
, "|\t");
116 // Print the domain of the statement.
117 openscop_relation_idump(file
, statement
->domain
, level
+1);
119 // Print the scattering of the statement.
120 openscop_relation_idump(file
, statement
->scattering
, level
+1);
122 // Print the array read access informations of the statement.
123 openscop_relation_list_idump(file
, statement
->read
, level
+1);
125 // Print the array write access informations of the statement.
126 openscop_relation_list_idump(file
, statement
->write
, level
+1);
128 // Print the original iterator names.
129 for (i
= 0; i
<= level
; i
++)
130 fprintf(file
, "|\t");
131 if (statement
->nb_iterators
> 0) {
132 fprintf(file
, "+-- Original iterator strings:");
133 for (i
= 0; i
< statement
->nb_iterators
; i
++)
134 fprintf(file
, " %s", statement
->iterators
[i
]);
138 fprintf(file
, "+-- No original iterator string\n");
141 for (i
= 0; i
<= level
+1; i
++)
142 fprintf(file
, "|\t");
145 // Print the original statement body.
146 for (i
= 0; i
<= level
; i
++)
147 fprintf(file
, "|\t");
148 if (statement
->body
!= NULL
)
149 fprintf(file
, "+-- Original body: %s\n", statement
->body
);
151 fprintf(file
, "+-- No original body\n");
154 for (i
= 0; i
<= level
+1; i
++)
155 fprintf(file
, "|\t");
158 statement
= statement
->next
;
162 if (statement
!= NULL
) {
163 for (j
= 0; j
<= level
; j
++)
164 fprintf(file
, "|\t");
165 fprintf(file
, "V\n");
170 for (j
= 0; j
<= level
; j
++)
171 fprintf(file
, "|\t");
177 * openscop_statement_dump function:
178 * This function prints the content of a openscop_statement_t structure
179 * (*statement) into a file (file, possibly stdout).
180 * \param file File where informations are printed.
181 * \param statement The statement whose information have to be printed.
183 void openscop_statement_dump(FILE * file
, openscop_statement_p statement
) {
184 openscop_statement_idump(file
, statement
, 0);
189 * openscop_statement_print function:
190 * This function prints the content of a openscop_statement_t structure
191 * (*statement) into a file (file, possibly stdout) in the OpenScop format.
192 * \param file File where informations are printed.
193 * \param statement The statement whose information have to be printed.
194 * \param names The textual names of the various elements. Is is important
195 * that names->nb_parameters is exact if the matrix
196 * representation is used. Set to NULL if printing comments
199 void openscop_statement_print(FILE * file
,
200 openscop_statement_p statement
,
201 openscop_names_p names
) {
202 int i
, switched
, number
= 1;
203 int tmp_nb_iterators
= 0;
204 char ** tmp_iterators
= NULL
;
206 while (statement
!= NULL
) {
207 // Switch iterator names to the current statement names if possible.
209 if (statement
->nb_iterators
> 0) {
210 tmp_nb_iterators
= names
->nb_iterators
;
211 tmp_iterators
= names
->iterators
;
212 names
->nb_iterators
= statement
->nb_iterators
;
213 names
->iterators
= statement
->iterators
;
217 fprintf(file
, "# =============================================== ");
218 fprintf(file
, "Statement %d\n", number
);
220 fprintf(file
, "# ---------------------------------------------- ");
221 fprintf(file
, "%2d.1 Domain\n", number
);
222 fprintf(file
, "# Iteration domain\n");
223 openscop_relation_print(file
, statement
->domain
, names
);
226 fprintf(file
, "# ---------------------------------------------- ");
227 fprintf(file
, "%2d.2 Scattering\n", number
);
228 if (statement
->scattering
!= NULL
) {
229 fprintf(file
, "# Scattering function is provided\n");
230 fprintf(file
, "1\n");
231 fprintf(file
, "# Scattering function\n");
232 openscop_relation_print(file
, statement
->scattering
, names
);
235 fprintf(file
, "# Scattering function is not provided\n");
236 fprintf(file
, "0\n");
240 fprintf(file
, "# ---------------------------------------------- ");
241 fprintf(file
, "%2d.3 Access\n", number
);
242 if ((statement
->read
!= NULL
) || (statement
->write
!= NULL
)) {
243 fprintf(file
, "# Access information is provided\n");
244 fprintf(file
, "1\n");
245 fprintf(file
, "\n# Read access information\n");
246 openscop_relation_list_print(file
, statement
->read
, names
);
247 fprintf(file
, "\n# Write access information\n");
248 openscop_relation_list_print(file
, statement
->write
, names
);
251 fprintf(file
, "# Access information is not provided\n");
252 fprintf(file
, "0\n");
256 fprintf(file
, "# ---------------------------------------------- ");
257 fprintf(file
, "%2d.4 Body\n", number
);
258 if (statement
->body
!= NULL
) {
259 fprintf(file
, "# Statement body is provided\n");
260 fprintf(file
, "1\n");
261 if (statement
->nb_iterators
> 0) {
262 fprintf(file
, "# Original iterator names\n");
263 for (i
= 0; i
< statement
->nb_iterators
; i
++)
264 fprintf(file
, "%s ", statement
->iterators
[i
]);
268 fprintf(file
, "# No original iterator names\n");
270 fprintf(file
, "# Statement body\n");
271 fprintf(file
, "%s\n", statement
->body
);
274 fprintf(file
, "# Statement body is not provided\n");
275 fprintf(file
, "0\n");
277 fprintf(file
, "\n\n");
280 statement
->nb_iterators
= tmp_nb_iterators
;
281 statement
->iterators
= tmp_iterators
;
283 statement
= statement
->next
;
289 /*****************************************************************************
291 *****************************************************************************/
295 * openscop_statement_read function:
296 * This function reads a openscop_statement_t structure from an input stream
298 * \param file The input stream.
299 * \param nb_parameters The number of global parameters.
300 * \return A pointer to the statement structure that has been read.
302 openscop_statement_p
openscop_statement_read(FILE * file
,
304 openscop_statement_p stmt
= openscop_statement_malloc();
305 char buff
[OPENSCOP_MAX_STRING
], * start
, * end
;
306 int expected_nb_iterators
, nb_iterators
;
309 // Read the domain matrices.
310 stmt
->domain
= openscop_relation_read(file
);
312 // Read the scattering, if any.
313 if (openscop_util_read_int(file
, NULL
) > 0) {
314 stmt
->scattering
= openscop_relation_read(file
);
317 // Read the access relations, if any.
318 if (openscop_util_read_int(file
, NULL
) > 0) {
319 stmt
->read
= openscop_relation_list_read(file
);
320 stmt
->write
= openscop_relation_list_read(file
);
323 // Read the body information, if any.
324 if (openscop_util_read_int(file
, NULL
) > 0) {
325 // Is there any iterator to read ?
326 if (openscop_relation_is_matrix(stmt
->domain
)) {
327 if (nb_parameters
== OPENSCOP_UNDEFINED
) {
328 fprintf(stderr
, "[OpenScop] Warning: undefined number of "
329 "parameters (no context ?), assuming 0.\n");
333 expected_nb_iterators
= stmt
->domain
->nb_columns
-
337 expected_nb_iterators
= stmt
->domain
->nb_output_dims
;
340 // Read the original iterator names.
341 if (expected_nb_iterators
> 0) {
342 stmt
->iterators
= openscop_util_strings_read(file
, &nb_iterators
);
343 stmt
->nb_iterators
= nb_iterators
;
344 if (expected_nb_iterators
!= nb_iterators
) {
345 fprintf(stderr
, "[OpenScop] Warning: not the right number of "
346 "original iterators.\n");
351 // - Skip blank/commented lines and spaces before the body.
352 start
= openscop_util_skip_blank_and_comments(file
, buff
);
354 // - Remove the comments after the body.
356 while ((*end
!= '#') && (*end
!= '\n'))
361 stmt
->body
= strdup(start
);
364 stmt
->nb_iterators
= 0;
365 stmt
->iterators
= NULL
;
374 /*+***************************************************************************
375 * Memory allocation/deallocation functions *
376 *****************************************************************************/
380 * openscop_statement_malloc function:
381 * This function allocates the memory space for a openscop_statement_t
382 * structure and sets its fields with default values. Then it returns a pointer
383 * to the allocated space.
384 * \return A pointer to an empty statement with fields set to default values.
386 openscop_statement_p
openscop_statement_malloc() {
387 openscop_statement_p statement
;
389 statement
= (openscop_statement_p
)malloc(sizeof(openscop_statement_t
));
390 if (statement
== NULL
) {
391 fprintf(stderr
, "[OpenScop] Error: memory overflow.\n");
395 statement
->domain
= NULL
;
396 statement
->scattering
= NULL
;
397 statement
->read
= NULL
;
398 statement
->write
= NULL
;
399 statement
->nb_iterators
= 0;
400 statement
->iterators
= NULL
;
401 statement
->body
= NULL
;
402 statement
->next
= NULL
;
409 * openscop_statement_free function:
410 * This function frees the allocated memory for a openscop_statement_t
412 * \param statement The pointer to the statement we want to free.
414 void openscop_statement_free(openscop_statement_p statement
) {
416 openscop_statement_p next
;
418 while (statement
!= NULL
) {
419 next
= statement
->next
;
420 openscop_relation_free(statement
->domain
);
421 openscop_relation_free(statement
->scattering
);
422 openscop_relation_list_free(statement
->read
);
423 openscop_relation_list_free(statement
->write
);
424 if (statement
->iterators
!= NULL
) {
425 for (i
= 0; i
< statement
->nb_iterators
; i
++)
426 free(statement
->iterators
[i
]);
427 free(statement
->iterators
);
429 if (statement
->body
!= NULL
)
430 free(statement
->body
);
438 /*+***************************************************************************
439 * Processing functions *
440 *****************************************************************************/
444 * openscop_statement_add function:
445 * This function adds a statement "statement" at the end of the statement
446 * list pointed by "location".
447 * \param location Address of the first element of the statement list.
448 * \param statement The statement to add to the list.
450 void openscop_statement_add(openscop_statement_p
* location
,
451 openscop_statement_p statement
) {
452 while (*location
!= NULL
)
453 location
= &((*location
)->next
);
455 *location
= statement
;
460 * openscop_statement_number function:
461 * This function returns the number of statements in the statement list
462 * provided as parameter.
463 * \param statement The first element of the statement list.
464 * \return The number of statements in the statement list.
466 int openscop_statement_number(openscop_statement_p statement
) {
469 while (statement
!= NULL
) {
471 statement
= statement
->next
;
478 * openscop_statement_copy function:
479 * This functions builds and returns a "hard copy" (not a pointer copy) of a
480 * openscop_statement_t data structure provided as parameter.
481 * \param statement The pointer to the statement we want to copy.
482 * \return A pointer to the full copy of the statement provided as parameter.
484 openscop_statement_p
openscop_statement_copy(openscop_statement_p statement
) {
486 openscop_statement_p copy
= NULL
, node
, previous
= NULL
;
488 while (statement
!= NULL
) {
489 node
= openscop_statement_malloc();
490 node
->domain
= openscop_relation_copy(statement
->domain
);
491 node
->scattering
= openscop_relation_copy(statement
->scattering
);
492 node
->read
= openscop_relation_list_copy(statement
->read
);
493 node
->write
= openscop_relation_list_copy(statement
->write
);
494 node
->nb_iterators
= statement
->nb_iterators
;
495 node
->iterators
= openscop_util_strings_copy(statement
->iterators
,
496 statement
->nb_iterators
);
497 node
->body
= strdup(statement
->body
);
506 previous
->next
= node
;
507 previous
= previous
->next
;
510 statement
= statement
->next
;
518 * openscop_statement_equal function:
519 * This function returns true if the two statements are the same, false
520 * otherwise (the usr field is not tested).
521 * \param s1 The first statement.
522 * \param s2 The second statement.
523 * \return 1 if s1 and s2 are the same (content-wise), 0 otherwise.
525 int openscop_statement_equal(openscop_statement_p s1
,
526 openscop_statement_p s2
) {
532 if (((s1
->next
!= NULL
) && (s2
->next
== NULL
)) ||
533 ((s1
->next
== NULL
) && (s2
->next
!= NULL
)))
536 if ((s1
->next
!= NULL
) && (s2
->next
!= NULL
))
537 if (!openscop_statement_equal(s1
->next
, s2
->next
))
540 if ((s1
->nb_iterators
!= s2
->nb_iterators
) ||
541 (!openscop_relation_equal(s1
->domain
, s2
->domain
)) ||
542 (!openscop_relation_equal(s1
->scattering
, s2
->scattering
)) ||
543 (!openscop_relation_list_equal(s1
->read
, s2
->read
)) ||
544 (!openscop_relation_list_equal(s1
->write
, s2
->write
)))
547 if (((s1
->body
== NULL
) && (s2
->body
!= NULL
)) ||
548 ((s1
->body
!= NULL
) && (s2
->body
== NULL
)) ||
549 ((s1
->body
!= NULL
) && (s2
->body
!= NULL
) &&
550 (strcmp(s1
->body
, s2
->body
) != 0))) {
551 fprintf(stderr
, "[OpenScop] info: bodies are not the same.\n");
555 for (i
= 0; i
< s1
->nb_iterators
; i
++) {
556 if (((s1
->iterators
[i
] == NULL
) && (s2
->iterators
[i
] != NULL
)) ||
557 ((s1
->iterators
[i
] != NULL
) && (s2
->iterators
[i
] == NULL
)) ||
558 ((s1
->iterators
[i
] != NULL
) && (s2
->iterators
[i
] != NULL
) &&
559 (strcmp(s1
->iterators
[i
], s2
->iterators
[i
]) != 0))) {
560 fprintf(stderr
, "[OpenScop] info: original iterators "
561 "are not the same.\n");
571 * openscop_statement_integrity_check function:
572 * This function checks that a statement is "well formed" according to some
573 * expected properties (setting an expected value to OPENSCOP_UNDEFINED means
574 * that we do not expect a specific value). It returns 0 if the check failed
575 * or 1 if no problem has been detected.
576 * \param statement The statement we want to check.
577 * \param expected_nb_parameters Expected number of parameters.
578 * \return 0 if the integrity check fails, 1 otherwise.
580 int openscop_statement_integrity_check(openscop_statement_p statement
,
581 int expected_nb_parameters
) {
582 int expected_nb_iterators
= OPENSCOP_UNDEFINED
;
584 while (statement
!= NULL
) {
586 if ((openscop_relation_is_matrix(statement
->domain
) &&
587 !openscop_relation_integrity_check(statement
->domain
,
588 OPENSCOP_TYPE_DOMAIN
,
591 OPENSCOP_UNDEFINED
)) ||
592 (!openscop_relation_is_matrix(statement
->domain
) &&
593 !openscop_relation_integrity_check(statement
->domain
,
594 OPENSCOP_TYPE_DOMAIN
,
597 expected_nb_parameters
))) {
601 // Get the number of iterators.
602 if (statement
->domain
!= NULL
) {
603 if (openscop_relation_is_matrix(statement
->domain
)) {
604 if (expected_nb_parameters
!= OPENSCOP_UNDEFINED
)
605 expected_nb_iterators
= statement
->domain
->nb_columns
-
606 expected_nb_parameters
- 2;
608 expected_nb_iterators
= OPENSCOP_UNDEFINED
;
611 expected_nb_iterators
= statement
->domain
->nb_output_dims
;
614 // Check the scattering function.
615 if ((openscop_relation_is_matrix(statement
->scattering
) &&
616 !openscop_relation_integrity_check(statement
->scattering
,
617 OPENSCOP_TYPE_SCATTERING
,
620 OPENSCOP_UNDEFINED
)) ||
621 (!openscop_relation_is_matrix(statement
->scattering
) &&
622 !openscop_relation_integrity_check(statement
->scattering
,
623 OPENSCOP_TYPE_SCATTERING
,
625 expected_nb_iterators
,
626 expected_nb_parameters
))) {
630 // Check the access functions.
631 if (!openscop_relation_list_integrity_check(statement
->read
,
632 OPENSCOP_TYPE_ACCESS
,
634 expected_nb_iterators
,
635 expected_nb_parameters
) ||
636 !openscop_relation_list_integrity_check(statement
->write
,
637 OPENSCOP_TYPE_ACCESS
,
639 expected_nb_iterators
,
640 expected_nb_parameters
)) {
644 if ((statement
->nb_iterators
> 0) &&
645 (statement
->nb_iterators
< statement
->domain
->nb_output_dims
)) {
646 fprintf(stderr
, "[OpenScop] Warning: not enough original iterator "
651 statement
= statement
->next
;