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 access information of the statement.
123 openscop_relation_list_idump(file
, statement
->access
, level
+1);
125 // Print the statement body information.
126 openscop_body_idump(file
, statement
->body
, level
+1);
129 for (i
= 0; i
<= level
+1; i
++)
130 fprintf(file
, "|\t");
133 statement
= statement
->next
;
137 if (statement
!= NULL
) {
138 for (j
= 0; j
<= level
; j
++)
139 fprintf(file
, "|\t");
140 fprintf(file
, "V\n");
145 for (j
= 0; j
<= level
; j
++)
146 fprintf(file
, "|\t");
152 * openscop_statement_dump function:
153 * This function prints the content of a openscop_statement_t structure
154 * (*statement) into a file (file, possibly stdout).
155 * \param file File where informations are printed.
156 * \param statement The statement whose information have to be printed.
158 void openscop_statement_dump(FILE * file
, openscop_statement_p statement
) {
159 openscop_statement_idump(file
, statement
, 0);
164 * openscop_statement_print function:
165 * This function prints the content of a openscop_statement_t structure
166 * (*statement) into a file (file, possibly stdout) in the OpenScop format.
167 * \param file File where informations are printed.
168 * \param statement The statement whose information have to be printed.
169 * \param names The textual names of the various elements.
170 * Set to NULL if printing comments is not needed.
172 void openscop_statement_print(FILE * file
,
173 openscop_statement_p statement
,
174 openscop_names_p names
) {
175 int nb_relations
, number
= 1;
177 while (statement
!= NULL
) {
180 fprintf(file
, "# =============================================== ");
181 fprintf(file
, "Statement %d\n", number
);
183 fprintf(file
, "# Number of relations describing the statement:\n");
185 if (statement
->domain
!= NULL
)
187 if (statement
->scattering
!= NULL
)
189 nb_relations
+= openscop_relation_list_count(statement
->access
);
191 fprintf(file
, "%d\n\n", nb_relations
);
193 fprintf(file
, "# ---------------------------------------------- ");
194 fprintf(file
, "%2d.1 Domain\n", number
);
195 openscop_relation_print(file
, statement
->domain
, names
);
198 fprintf(file
, "# ---------------------------------------------- ");
199 fprintf(file
, "%2d.2 Scattering\n", number
);
200 openscop_relation_print(file
, statement
->scattering
, names
);
203 fprintf(file
, "# ---------------------------------------------- ");
204 fprintf(file
, "%2d.3 Access\n", number
);
205 openscop_relation_list_print_elts(file
, statement
->access
, names
);
208 fprintf(file
, "# ---------------------------------------------- ");
209 fprintf(file
, "%2d.4 Body\n", number
);
210 openscop_body_print(file
, statement
->body
);
212 fprintf(file
, "\n\n");
213 statement
= statement
->next
;
219 /*****************************************************************************
221 *****************************************************************************/
225 * openscop_statement_dispatch function:
226 * this function dispatches the relations from a relation list to the
227 * convenient fields of a statement structure: it extracts the domain,
228 * the scattering and the access list and store them accordingly in the
229 * statement structure provided as a parameter.
230 * \param stmt The statement where to dispatch the relations.
231 * \param list The "brute" relation list to sort and dispatch (freed).
234 void openscop_statement_dispatch(openscop_statement_p stmt
,
235 openscop_relation_list_p list
) {
236 openscop_relation_list_p domain_list
;
237 openscop_relation_list_p scattering_list
;
238 int nb_domains
, nb_scattering
, nb_accesses
;
241 domain_list
= openscop_relation_list_filter(list
, OPENSCOP_TYPE_DOMAIN
);
242 nb_domains
= openscop_relation_list_count(domain_list
);
244 OPENSCOP_error("more than one domain for a statement");
246 if (domain_list
!= NULL
) {
247 stmt
->domain
= domain_list
->elt
;
248 domain_list
->elt
= NULL
;
249 openscop_relation_list_free(domain_list
);
256 scattering_list
=openscop_relation_list_filter(list
,OPENSCOP_TYPE_SCATTERING
);
257 nb_scattering
= openscop_relation_list_count(scattering_list
);
258 if (nb_scattering
> 1)
259 OPENSCOP_error("more than one scattering relation for a statement");
261 if (scattering_list
!= NULL
) {
262 stmt
->scattering
= scattering_list
->elt
;
263 scattering_list
->elt
= NULL
;
264 openscop_relation_list_free(scattering_list
);
267 stmt
->scattering
= NULL
;
271 stmt
->access
= openscop_relation_list_filter(list
, OPENSCOP_TYPE_ACCESS
);
272 nb_accesses
= openscop_relation_list_count(stmt
->access
);
274 if ((nb_domains
+ nb_scattering
+ nb_accesses
) !=
275 (openscop_relation_list_count(list
)))
276 OPENSCOP_error("unexpected relation type to define a statement");
278 openscop_relation_list_free(list
);
283 * openscop_statement_read function:
284 * This function reads a openscop_statement_t structure from an input stream
286 * \param file The input stream.
287 * \return A pointer to the statement structure that has been read.
289 openscop_statement_p
openscop_statement_read(FILE * file
) {
290 openscop_statement_p stmt
= openscop_statement_malloc();
291 openscop_relation_list_p list
;
292 int expected_nb_iterators
;
295 // Read all statement relations.
296 list
= openscop_relation_list_read(file
);
298 // Store relations at the right place according to their type.
299 openscop_statement_dispatch(stmt
, list
);
301 // Read the body information.
302 if (stmt
->domain
!= NULL
) {
303 expected_nb_iterators
= stmt
->domain
->nb_output_dims
;
306 OPENSCOP_warning("no domain, assuming 0 original iterator");
307 expected_nb_iterators
= 0;
309 stmt
->body
= openscop_body_read(file
, expected_nb_iterators
);
316 /*+***************************************************************************
317 * Memory allocation/deallocation functions *
318 *****************************************************************************/
322 * openscop_statement_malloc function:
323 * This function allocates the memory space for a openscop_statement_t
324 * structure and sets its fields with default values. Then it returns a pointer
325 * to the allocated space.
326 * \return A pointer to an empty statement with fields set to default values.
328 openscop_statement_p
openscop_statement_malloc() {
329 openscop_statement_p statement
;
331 OPENSCOP_malloc(statement
, openscop_statement_p
,
332 sizeof(openscop_statement_t
));
333 statement
->domain
= NULL
;
334 statement
->scattering
= NULL
;
335 statement
->access
= NULL
;
336 statement
->body
= NULL
;
337 statement
->next
= NULL
;
344 * openscop_statement_free function:
345 * This function frees the allocated memory for a openscop_statement_t
347 * \param statement The pointer to the statement we want to free.
349 void openscop_statement_free(openscop_statement_p statement
) {
350 openscop_statement_p next
;
352 while (statement
!= NULL
) {
353 next
= statement
->next
;
354 openscop_relation_free(statement
->domain
);
355 openscop_relation_free(statement
->scattering
);
356 openscop_relation_list_free(statement
->access
);
357 openscop_body_free(statement
->body
);
365 /*+***************************************************************************
366 * Processing functions *
367 *****************************************************************************/
371 * openscop_statement_add function:
372 * This function adds a statement "statement" at the end of the statement
373 * list pointed by "location".
374 * \param location Address of the first element of the statement list.
375 * \param statement The statement to add to the list.
377 void openscop_statement_add(openscop_statement_p
* location
,
378 openscop_statement_p statement
) {
379 while (*location
!= NULL
)
380 location
= &((*location
)->next
);
382 *location
= statement
;
387 * openscop_statement_number function:
388 * This function returns the number of statements in the statement list
389 * provided as parameter.
390 * \param statement The first element of the statement list.
391 * \return The number of statements in the statement list.
393 int openscop_statement_number(openscop_statement_p statement
) {
396 while (statement
!= NULL
) {
398 statement
= statement
->next
;
405 * openscop_statement_copy function:
406 * This functions builds and returns a "hard copy" (not a pointer copy) of a
407 * openscop_statement_t data structure provided as parameter.
408 * \param statement The pointer to the statement we want to copy.
409 * \return A pointer to the full copy of the statement provided as parameter.
411 openscop_statement_p
openscop_statement_copy(openscop_statement_p statement
) {
413 openscop_statement_p copy
= NULL
, node
, previous
= NULL
;
415 while (statement
!= NULL
) {
416 node
= openscop_statement_malloc();
417 node
->domain
= openscop_relation_copy(statement
->domain
);
418 node
->scattering
= openscop_relation_copy(statement
->scattering
);
419 node
->access
= openscop_relation_list_copy(statement
->access
);
420 node
->body
= openscop_body_copy(statement
->body
);
429 previous
->next
= node
;
430 previous
= previous
->next
;
433 statement
= statement
->next
;
441 * openscop_statement_equal function:
442 * This function returns true if the two statements are the same, false
443 * otherwise (the usr field is not tested).
444 * \param s1 The first statement.
445 * \param s2 The second statement.
446 * \return 1 if s1 and s2 are the same (content-wise), 0 otherwise.
448 int openscop_statement_equal(openscop_statement_p s1
,
449 openscop_statement_p s2
) {
454 if (((s1
->next
!= NULL
) && (s2
->next
== NULL
)) ||
455 ((s1
->next
== NULL
) && (s2
->next
!= NULL
)))
458 if ((s1
->next
!= NULL
) && (s2
->next
!= NULL
))
459 if (!openscop_statement_equal(s1
->next
, s2
->next
))
462 if ((!openscop_relation_equal(s1
->domain
, s2
->domain
)) ||
463 (!openscop_relation_equal(s1
->scattering
, s2
->scattering
)) ||
464 (!openscop_relation_list_equal(s1
->access
, s2
->access
)) ||
465 (!openscop_body_equal(s1
->body
, s2
->body
)))
473 * openscop_statement_integrity_check function:
474 * This function checks that a statement is "well formed" according to some
475 * expected properties (setting an expected value to OPENSCOP_UNDEFINED means
476 * that we do not expect a specific value). It returns 0 if the check failed
477 * or 1 if no problem has been detected.
478 * \param statement The statement we want to check.
479 * \param expected_nb_parameters Expected number of parameters.
480 * \return 0 if the integrity check fails, 1 otherwise.
482 int openscop_statement_integrity_check(openscop_statement_p statement
,
483 int expected_nb_parameters
) {
484 int expected_nb_iterators
;
486 while (statement
!= NULL
) {
488 if (!openscop_relation_integrity_check(statement
->domain
,
489 OPENSCOP_TYPE_DOMAIN
,
492 expected_nb_parameters
)) {
496 // Get the number of iterators.
497 if (statement
->domain
!= NULL
)
498 expected_nb_iterators
= statement
->domain
->nb_output_dims
;
500 expected_nb_iterators
= OPENSCOP_UNDEFINED
;
502 // Check the scattering relation.
503 if (!openscop_relation_integrity_check(statement
->scattering
,
504 OPENSCOP_TYPE_SCATTERING
,
506 expected_nb_iterators
,
507 expected_nb_parameters
)) {
511 // Check the access relations.
512 if (!openscop_relation_list_integrity_check(statement
->access
,
513 OPENSCOP_TYPE_ACCESS
,
515 expected_nb_iterators
,
516 expected_nb_parameters
)) {
520 // Check the statement body.
521 if (!openscop_body_integrity_check(statement
->body
,
522 expected_nb_iterators
)) {
526 statement
= statement
->next
;