osl_scop_remove_unions functionality
[openscop.git] / source / statement.c
bloba15f5bc287035030f10012b345d4c358df5d258f
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
4 **-----------------------------------------------------------------**
5 ** statement.c **
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 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
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 *****************************************************************************/
64 #include <stdlib.h>
65 #include <stdio.h>
66 #include <string.h>
67 #include <ctype.h>
69 #include <osl/macros.h>
70 #include <osl/util.h>
71 #include <osl/strings.h>
72 #include <osl/body.h>
73 #include <osl/relation.h>
74 #include <osl/relation_list.h>
75 #include <osl/names.h>
76 #include <osl/interface.h>
77 #include <osl/generic.h>
78 #include <osl/statement.h>
81 /*+***************************************************************************
82 * Structure display functions *
83 *****************************************************************************/
86 /**
87 * osl_statement_idump function:
88 * this function displays an osl_statement_t structure (*statement) into
89 * a file (file, possibly stdout) in a way that trends to be understandable.
90 * It includes an indentation level (level) in order to work with others
91 * dumping functions.
92 * \param[in] file File where the information has to be printed.
93 * \param[in] statement The statement whose information has to be printed.
94 * \param[in] level Number of spaces before printing, for each line.
96 void osl_statement_idump(FILE * file, osl_statement_p statement, int level) {
97 int j, first = 1, number = 1;
99 // Go to the right level.
100 for (j = 0; j < level; j++)
101 fprintf(file, "|\t");
103 if (statement != NULL)
104 fprintf(file, "+-- osl_statement_t (S%d)\n", number);
105 else
106 fprintf(file, "+-- NULL statement\n");
108 while (statement != NULL) {
109 if (!first) {
110 // Go to the right level.
111 for (j = 0; j < level; j++)
112 fprintf(file, "|\t");
113 fprintf(file, "| osl_statement_t (S%d)\n", number);
115 else
116 first = 0;
118 // A blank line.
119 for (j = 0; j <= level + 1; j++)
120 fprintf(file, "|\t");
121 fprintf(file, "\n");
123 // Print the domain of the statement.
124 osl_relation_idump(file, statement->domain, level + 1);
126 // Print the scattering of the statement.
127 osl_relation_idump(file, statement->scattering, level + 1);
129 // Print the array access information of the statement.
130 osl_relation_list_idump(file, statement->access, level + 1);
132 // Print the original body expression.
133 osl_generic_idump(file, statement->extension, level + 1);
135 statement = statement->next;
136 number++;
138 // Next line.
139 if (statement != NULL) {
140 for (j = 0; j <= level; j++)
141 fprintf(file, "|\t");
142 fprintf(file, "V\n");
146 // The last line.
147 for (j = 0; j <= level; j++)
148 fprintf(file, "|\t");
149 fprintf(file, "\n");
154 * osl_statement_dump function:
155 * this function prints the content of an osl_statement_t structure
156 * (*statement) into a file (file, possibly stdout).
157 * \param[in] file The file where the information has to be printed.
158 * \param[in] statement The statement whose information has to be printed.
160 void osl_statement_dump(FILE * file, osl_statement_p statement) {
161 osl_statement_idump(file, statement, 0);
166 * osl_statement_names function:
167 * this function generates as set of names for all the dimensions
168 * involved in a given statement.
169 * \param[in] statement The statement (list) we have to generate names for.
170 * \return A set of generated names for the input statement dimensions.
172 static
173 osl_names_p osl_statement_names(osl_statement_p statement) {
174 int nb_parameters = OSL_UNDEFINED;
175 int nb_iterators = OSL_UNDEFINED;
176 int nb_scattdims = OSL_UNDEFINED;
177 int nb_localdims = OSL_UNDEFINED;
178 int array_id = OSL_UNDEFINED;
180 osl_statement_get_attributes(statement, &nb_parameters, &nb_iterators,
181 &nb_scattdims, &nb_localdims, &array_id);
183 return osl_names_generate("P", nb_parameters,
184 "i", nb_iterators,
185 "c", nb_scattdims,
186 "l", nb_localdims,
187 "A", array_id);
192 * osl_statement_pprint function:
193 * this function pretty-prints the content of an osl_statement_t structure
194 * (*statement) into a file (file, possibly stdout) in the OpenScop format.
195 * \param[in] file The file where the information has to be printed.
196 * \param[in] statement The statement whose information has to be printed.
197 * \param[in] names The names of the constraint columns for comments.
199 void osl_statement_pprint(FILE * file, osl_statement_p statement,
200 osl_names_p names) {
201 size_t nb_relations;
202 int number = 1;
203 int generated_names = 0;
204 int iterators_backedup = 0;
205 int nb_ext = 0;
206 osl_body_p body = NULL;
207 osl_strings_p iterators_backup = NULL;
209 // Generate the dimension names if necessary and replace iterators with
210 // statement iterators if possible.
211 if (names == NULL) {
212 generated_names = 1;
213 names = osl_statement_names(statement);
216 while (statement != NULL) {
217 // If possible, replace iterator names with statement iterator names.
218 body = (osl_body_p)osl_generic_lookup(statement->extension, OSL_URI_BODY);
219 if (body && body->iterators != NULL) {
220 iterators_backedup = 1;
221 iterators_backup = names->iterators;
222 names->iterators = body->iterators;
225 nb_relations = 0;
227 fprintf(file, "# =============================================== ");
228 fprintf(file, "Statement %d\n", number);
230 fprintf(file, "# Number of relations describing the statement:\n");
232 if (statement->domain != NULL)
233 nb_relations ++;
234 if (statement->scattering != NULL)
235 nb_relations ++;
236 nb_relations += osl_relation_list_count(statement->access);
238 fprintf(file, "%lu\n\n", nb_relations);
240 fprintf(file, "# ---------------------------------------------- ");
241 fprintf(file, "%2d.1 Domain\n", number);
242 osl_relation_pprint(file, statement->domain, names);
243 fprintf(file, "\n");
245 fprintf(file, "# ---------------------------------------------- ");
246 fprintf(file, "%2d.2 Scattering\n", number);
247 osl_relation_pprint(file, statement->scattering, names);
248 fprintf(file, "\n");
250 fprintf(file, "# ---------------------------------------------- ");
251 fprintf(file, "%2d.3 Access\n", number);
252 osl_relation_list_pprint_elts(file, statement->access, names);
253 fprintf(file, "\n");
255 fprintf(file, "# ---------------------------------------------- ");
256 fprintf(file, "%2d.4 Statement Extensions\n", number);
257 fprintf(file, "# Number of Statement Extensions\n");
258 nb_ext = osl_generic_number(statement->extension);
259 fprintf(file, "%d\n", nb_ext);
260 if(nb_ext>0)
261 osl_generic_print(file, statement->extension);
263 fprintf(file, "\n");
265 // If necessary, switch back iterator names.
266 if (iterators_backedup) {
267 iterators_backedup = 0;
268 names->iterators = iterators_backup;
271 statement = statement->next;
272 number++;
275 if (generated_names)
276 osl_names_free(names);
281 * osl_statement_pprint_scoplib function:
282 * this function pretty-prints the content of an osl_statement_t structure
283 * (*statement) into a file (file, possibly stdout) in the SCoPLib format.
284 * \param[in] file The file where the information has to be printed.
285 * \param[in] statement The statement whose information has to be printed.
286 * \param[in] names The names of the constraint columns for comments.
288 void osl_statement_pprint_scoplib(FILE * file, osl_statement_p statement,
289 osl_names_p names) {
290 int number = 1;
291 int generated_names = 0;
292 int iterators_backedup = 0;
293 osl_body_p body = NULL;
294 osl_strings_p iterators_backup = NULL;
295 int add_fakeiter;
297 // Generate the dimension names if necessary and replace iterators with
298 // statement iterators if possible.
299 if (names == NULL) {
300 generated_names = 1;
301 names = osl_statement_names(statement);
304 while (statement != NULL) {
305 // If possible, replace iterator names with statement iterator names.
306 body = (osl_body_p)osl_generic_lookup(statement->extension, OSL_URI_BODY);
307 if (body && body->iterators != NULL) {
308 iterators_backedup = 1;
309 iterators_backup = names->iterators;
310 names->iterators = body->iterators;
313 add_fakeiter = statement->domain->nb_rows == 0 &&
314 statement->scattering->nb_rows == 1;
316 fprintf(file, "# =============================================== ");
317 fprintf(file, "Statement %d\n", number);
319 fprintf(file, "# ---------------------------------------------- ");
320 fprintf(file, "%2d.1 Domain\n", number);
321 fprintf(file, "# Iteration domain\n");
322 osl_relation_pprint_scoplib(file, statement->domain, names, 1, add_fakeiter);
323 fprintf(file, "\n");
325 fprintf(file, "# ---------------------------------------------- ");
326 fprintf(file, "%2d.2 Scattering\n", number);
327 fprintf(file,"# Scattering function is provided\n1\n");
328 osl_relation_pprint_scoplib(file, statement->scattering, names, 0,
329 add_fakeiter);
330 fprintf(file, "\n");
332 fprintf(file, "# ---------------------------------------------- ");
333 fprintf(file, "%2d.3 Access\n", number);
334 fprintf(file,"# Access informations are provided\n1\n");
336 osl_relation_list_pprint_access_array_scoplib(file, statement->access,
337 names, add_fakeiter);
338 fprintf(file, "\n");
340 fprintf(file, "# ---------------------------------------------- ");
341 fprintf(file, "%2d.4 Body\n", number);
342 if (body != NULL) {
343 fprintf(file, "# Statement body is provided\n1\n");
344 osl_body_print_scoplib(file, body);
345 body = NULL; //re-initialize for next statement
347 else {
348 fprintf(file, "# Statement body is not provided\n0\n");
351 fprintf(file, "\n");
353 // If necessary, switch back iterator names.
354 if (iterators_backedup) {
355 iterators_backedup = 0;
356 names->iterators = iterators_backup;
359 statement = statement->next;
360 number++;
363 if (generated_names)
364 osl_names_free(names);
369 * osl_statement_print function:
370 * this function prints the content of an osl_statement_t structure
371 * (*statement) into a file (file, possibly stdout) in the OpenScop format.
372 * \param[in] file The file where the information has to be printed.
373 * \param[in] statement The statement whose information has to be printed.
375 void osl_statement_print(FILE * file, osl_statement_p statement) {
377 osl_statement_pprint(file, statement, NULL);
381 /*****************************************************************************
382 * Reading function *
383 *****************************************************************************/
387 * osl_statement_dispatch function:
388 * this function dispatches the relations from a relation list to the
389 * convenient fields of a statement structure: it extracts the domain,
390 * the scattering and the access list and store them accordingly in the
391 * statement structure provided as a parameter.
392 * \param[in,out] stmt The statement where to dispatch the relations.
393 * \param[in,out] list The "brute" relation list to sort and dispatch (freed).
395 static
396 void osl_statement_dispatch(osl_statement_p stmt, osl_relation_list_p list) {
397 osl_relation_list_p domain_list;
398 osl_relation_list_p scattering_list;
399 size_t nb_domains, nb_scattering, nb_accesses;
401 // Domain.
402 domain_list = osl_relation_list_filter(list, OSL_TYPE_DOMAIN);
403 nb_domains = osl_relation_list_count(domain_list);
404 if (nb_domains > 1)
405 OSL_error("more than one domain for a statement");
407 if (domain_list != NULL) {
408 stmt->domain = domain_list->elt;
409 domain_list->elt = NULL;
410 osl_relation_list_free(domain_list);
412 else {
413 stmt->domain = NULL;
416 // Scattering.
417 scattering_list=osl_relation_list_filter(list,OSL_TYPE_SCATTERING);
418 nb_scattering = osl_relation_list_count(scattering_list);
419 if (nb_scattering > 1)
420 OSL_error("more than one scattering relation for a statement");
422 if (scattering_list != NULL) {
423 stmt->scattering = scattering_list->elt;
424 scattering_list->elt = NULL;
425 osl_relation_list_free(scattering_list);
427 else {
428 stmt->scattering = NULL;
431 // Access.
432 stmt->access = osl_relation_list_filter(list, OSL_TYPE_ACCESS);
433 nb_accesses = osl_relation_list_count(stmt->access);
435 if ((nb_domains + nb_scattering + nb_accesses) !=
436 (osl_relation_list_count(list)))
437 OSL_error("unexpected relation type to define a statement");
439 osl_relation_list_free(list);
444 * osl_statement_pread function ("precision read"):
445 * this function reads an osl_statement_t structure from an input stream
446 * (possibly stdin).
447 * \param[in] file The input stream.
448 * \param[in] registry The list of known interfaces (others are ignored).
449 * \param[in] precision The precision of the relation elements.
450 * \return A pointer to the statement structure that has been read.
452 osl_statement_p osl_statement_pread(FILE * file, osl_interface_p registry,
453 int precision) {
454 osl_statement_p stmt = osl_statement_malloc();
455 osl_relation_list_p list;
456 osl_generic_p new = NULL;
457 int i, nb_ext = 0;
459 if (file) {
460 // Read all statement relations.
461 list = osl_relation_list_pread(file, precision);
463 // Store relations at the right place according to their type.
464 osl_statement_dispatch(stmt, list);
466 // Read the Extensions
467 nb_ext = osl_util_read_int(file, NULL);
468 for (i=0; i<nb_ext; i++) {
469 new = osl_generic_read_one(file, registry);
470 osl_generic_add(&stmt->extension, new);
474 return stmt;
479 * osl_statement_read function:
480 * this function is equivalent to osl_statement_pread() except that
481 * (1) the precision corresponds to the precision environment variable or
482 * to the highest available precision if it is not defined, and
483 * (2) the list of known interface is set to the default one.
484 * \see{osl_statement_pread}
486 osl_statement_p osl_statement_read(FILE * foo) {
487 int precision = osl_util_get_precision();
488 osl_interface_p registry = osl_interface_get_default_registry();
489 osl_statement_p statement = osl_statement_pread(foo, registry, precision);
491 osl_interface_free(registry);
492 return statement;
496 /*+***************************************************************************
497 * Memory allocation/deallocation functions *
498 *****************************************************************************/
502 * osl_statement_malloc function:
503 * this function allocates the memory space for an osl_statement_t
504 * structure and sets its fields with default values. Then it returns a pointer
505 * to the allocated space.
506 * \return A pointer to an empty statement with fields set to default values.
508 osl_statement_p osl_statement_malloc() {
509 osl_statement_p statement;
511 OSL_malloc(statement, osl_statement_p, sizeof(osl_statement_t));
512 statement->domain = NULL;
513 statement->scattering = NULL;
514 statement->access = NULL;
515 statement->extension = NULL;
516 statement->next = NULL;
518 return statement;
523 * osl_statement_free function:
524 * this function frees the allocated memory for an osl_statement_t
525 * structure.
526 * \param[in,out] statement The pointer to the statement we want to free.
528 void osl_statement_free(osl_statement_p statement) {
529 osl_statement_p next;
531 while (statement != NULL) {
532 next = statement->next;
533 osl_relation_free(statement->domain);
534 osl_relation_free(statement->scattering);
535 osl_relation_list_free(statement->access);
536 osl_generic_free(statement->extension);
538 free(statement);
539 statement = next;
544 /*+***************************************************************************
545 * Processing functions *
546 *****************************************************************************/
550 * osl_statement_add function:
551 * this function adds a statement "statement" at the end of the statement
552 * list pointed by "location".
553 * \param[in,out] location Address of the first element of the statement list.
554 * \param[in] statement The statement to add to the list.
556 void osl_statement_add(osl_statement_p * location,
557 osl_statement_p statement) {
558 while (*location != NULL)
559 location = &((*location)->next);
561 *location = statement;
566 * osl_statement_number function:
567 * this function returns the number of statements in the statement list
568 * provided as parameter.
569 * \param[in] statement The first element of the statement list.
570 * \return The number of statements in the statement list.
572 int osl_statement_number(osl_statement_p statement) {
573 int number = 0;
575 while (statement != NULL) {
576 number++;
577 statement = statement->next;
579 return number;
584 * osl_statement_nclone function:
585 * This function builds and returns a "hard copy" (not a pointer copy) of the
586 * n first elements of an osl_statement_t list.
587 * \param statement The pointer to the statement structure we want to clone.
588 * \param n The number of nodes we want to copy (-1 for infinity).
589 * \return The clone of the n first nodes of the statement list.
591 osl_statement_p osl_statement_nclone(osl_statement_p statement, int n) {
592 int first = 1, i = 0;
593 osl_statement_p clone = NULL, node, previous = NULL;
595 while ((statement != NULL) && ((n == -1) || (i < n))) {
596 node = osl_statement_malloc();
597 node->domain = osl_relation_clone(statement->domain);
598 node->scattering = osl_relation_clone(statement->scattering);
599 node->access = osl_relation_list_clone(statement->access);
600 node->extension = osl_generic_clone(statement->extension);
601 node->next = NULL;
603 if (first) {
604 first = 0;
605 clone = node;
606 previous = node;
608 else {
609 previous->next = node;
610 previous = previous->next;
613 i++;
614 statement = statement->next;
617 return clone;
622 * osl_statement_clone function:
623 * This functions builds and returns a "hard copy" (not a pointer copy) of an
624 * osl_statement_t data structure provided as parameter.
625 * \param[in] statement The pointer to the statement we want to clone.
626 * \return A pointer to the clone of the statement provided as parameter.
628 osl_statement_p osl_statement_clone(osl_statement_p statement) {
629 return osl_statement_nclone(statement, -1);
632 /// Clone first part of the union, return NULL if input is NULL.
633 static osl_relation_p osl_relation_clone_one_safe(osl_relation_p relation) {
634 if (relation == NULL)
635 return NULL;
636 return osl_relation_nclone(relation, 1);
640 * osl_statement_remove_unions function:
641 * Create a statement list from a statement containing unions of relations in
642 * domain and/or scattering. This functions constructs multiple new statements
643 * and keeps the original statement intact. Each new statement has exactly one
644 * domain union component and exactly one scattering union component. If the
645 * statement does not have the domain relation or the scattering relation, this
646 * function returns \c NULL.
647 * \param[in] statement A pointer to the statement
648 * \return A pointer to the head of the newly created statement list.
650 osl_statement_p osl_statement_remove_unions(osl_statement_p statement) {
651 osl_relation_p domain, scattering;
652 osl_statement_p statement_ptr, result;
653 if (!statement)
654 return NULL;
656 // Make at least one new statement, even if there are no relations.
657 domain = statement->domain;
658 scattering = statement->scattering;
659 result = NULL;
660 do {
661 do {
662 osl_statement_p new_statement = osl_statement_malloc();
663 new_statement->domain = osl_relation_clone_one_safe(domain);
664 new_statement->scattering = osl_relation_clone_one_safe(scattering);
665 new_statement->access = osl_relation_list_clone(statement->access);
666 new_statement->extension = osl_generic_clone(statement->extension);
667 if (!result) {
668 statement_ptr = new_statement;
669 result = new_statement;
670 } else {
671 statement_ptr->next = new_statement;
672 statement_ptr = statement_ptr->next;
674 if (scattering == NULL || scattering->next == NULL)
675 break;
676 scattering = scattering->next;
677 } while (1);
678 if (domain == NULL || domain->next == NULL)
679 break;
680 domain = domain->next;
681 } while (1);
683 return result;
689 * osl_statement_equal function:
690 * this function returns true if the two statements provided as parameters
691 * are the same, false otherwise (the usr field is not tested).
692 * \param[in] s1 The first statement.
693 * \param[in] s2 The second statement.
694 * \return 1 if s1 and s2 are the same (content-wise), 0 otherwise.
696 int osl_statement_equal(osl_statement_p s1, osl_statement_p s2) {
698 if (s1 == s2)
699 return 1;
701 if (((s1->next != NULL) && (s2->next == NULL)) ||
702 ((s1->next == NULL) && (s2->next != NULL))) {
703 OSL_info("statements are not the same");
704 return 0;
707 if ((s1->next != NULL) && (s2->next != NULL)) {
708 if (!osl_statement_equal(s1->next, s2->next)) {
709 OSL_info("number of statements is not the same");
710 return 0;
714 if (!osl_relation_equal(s1->domain, s2->domain)) {
715 OSL_info("statement domains are not the same");
716 return 0;
719 if (!osl_relation_equal(s1->scattering, s2->scattering)) {
720 OSL_info("statement scatterings are not the same");
721 return 0;
724 if (!osl_relation_list_equal(s1->access, s2->access)) {
725 OSL_info("statement accesses are not the same");
726 return 0;
729 if (!osl_generic_equal(s1->extension, s2->extension)) {
730 OSL_info("statement bodies are not the same");
731 return 0;
734 return 1;
739 * osl_statement_integrity_check function:
740 * this function checks that a statement is "well formed" according to some
741 * expected properties (setting an expected value to OSL_UNDEFINED means
742 * that we do not expect a specific value). It returns 0 if the check failed
743 * or 1 if no problem has been detected.
744 * \param[in] statement The statement we want to check.
745 * \param[in] expected_nb_parameters Expected number of parameters.
746 * \return 0 if the integrity check fails, 1 otherwise.
748 int osl_statement_integrity_check(osl_statement_p statement,
749 int expected_nb_parameters) {
750 int expected_nb_iterators;
751 osl_body_p body = NULL;
753 while (statement != NULL) {
754 // Check the domain.
755 if (!osl_relation_integrity_check(statement->domain,
756 OSL_TYPE_DOMAIN,
757 OSL_UNDEFINED,
759 expected_nb_parameters)) {
760 return 0;
763 // Get the number of iterators.
764 if (statement->domain != NULL)
765 expected_nb_iterators = statement->domain->nb_output_dims;
766 else
767 expected_nb_iterators = OSL_UNDEFINED;
769 // Check the scattering relation.
770 if (!osl_relation_integrity_check(statement->scattering,
771 OSL_TYPE_SCATTERING,
772 OSL_UNDEFINED,
773 expected_nb_iterators,
774 expected_nb_parameters)) {
775 return 0;
778 // Check the access relations.
779 if (!osl_relation_list_integrity_check(statement->access,
780 OSL_TYPE_ACCESS,
781 OSL_UNDEFINED,
782 expected_nb_iterators,
783 expected_nb_parameters)) {
784 return 0;
787 // Check the statement body.
788 body = (osl_body_p)osl_generic_lookup(statement->extension, OSL_URI_BODY);
789 if ((expected_nb_iterators != OSL_UNDEFINED) &&
790 body && body->iterators != NULL &&
791 ((size_t)expected_nb_iterators != osl_strings_size(body->iterators))) {
792 OSL_warning("unexpected number of original iterators");
793 return 0;
796 statement = statement->next;
799 return 1;
804 * osl_statement_get_nb_iterators function:
805 * this function returns the number of surroounding iterators of a given
806 * statement.
807 * \param statement The statement we want to know the number of iterators.
808 * \return The number of surrounding iterators for the statement.
810 int osl_statement_get_nb_iterators(osl_statement_p statement) {
812 if (statement->domain == NULL) {
813 OSL_warning("no statement domain, assuming 0 iterators");
814 return 0;
816 else {
817 return statement->domain->nb_output_dims;
823 * osl_statement_get_attributes function:
824 * this function returns, through its parameters, the maximum values of the
825 * relation attributes (nb_iterators, nb_parameters etc) in the statement.
826 * HOWEVER, it updates the parameter value iff the attribute is greater than
827 * the input parameter value. Hence it may be used to get the attributes as
828 * well as to find the maximum attributes for several statement lists. The
829 * array identifier 0 is used when there is no array identifier (AND this is
830 * OK), OSL_UNDEFINED is used to report it is impossible to provide the
831 * property while it should. This function is not intended for checking, the
832 * input statement should be correct.
833 * \param[in] statement The statement to extract attributes values.
834 * \param[in,out] nb_parameters Number of parameter attribute.
835 * \param[in,out] nb_iterators Number of iterators attribute.
836 * \param[in,out] nb_scattdims Number of scattering dimensions attribute.
837 * \param[in,out] nb_localdims Number of local dimensions attribute.
838 * \param[in,out] array_id Maximum array identifier attribute.
840 void osl_statement_get_attributes(osl_statement_p statement,
841 int * nb_parameters,
842 int * nb_iterators,
843 int * nb_scattdims,
844 int * nb_localdims,
845 int * array_id) {
846 int local_nb_parameters = OSL_UNDEFINED;
847 int local_nb_iterators = OSL_UNDEFINED;
848 int local_nb_scattdims = OSL_UNDEFINED;
849 int local_nb_localdims = OSL_UNDEFINED;
850 int local_array_id = OSL_UNDEFINED;
852 while (statement != NULL) {
853 osl_relation_get_attributes(statement->domain,
854 &local_nb_parameters,
855 &local_nb_iterators,
856 &local_nb_scattdims,
857 &local_nb_localdims,
858 &local_array_id);
860 osl_relation_get_attributes(statement->scattering,
861 &local_nb_parameters,
862 &local_nb_iterators,
863 &local_nb_scattdims,
864 &local_nb_localdims,
865 &local_array_id);
867 osl_relation_list_get_attributes(statement->access,
868 &local_nb_parameters,
869 &local_nb_iterators,
870 &local_nb_scattdims,
871 &local_nb_localdims,
872 &local_array_id);
873 // Update.
874 *nb_parameters = OSL_max(*nb_parameters, local_nb_parameters);
875 *nb_iterators = OSL_max(*nb_iterators, local_nb_iterators);
876 *nb_scattdims = OSL_max(*nb_scattdims, local_nb_scattdims);
877 *nb_localdims = OSL_max(*nb_localdims, local_nb_localdims);
878 *array_id = OSL_max(*array_id, local_array_id);
879 statement = statement->next;
885 * osl_statement_get_body function:
886 * this function returns the body of the statement.
887 * \param[in] statement The statement to search the body.
888 * \return the body if found, NULL otherwise.
890 osl_body_p osl_statement_get_body(osl_statement_p statement) {
891 osl_body_p body;
892 osl_extbody_p ebody;
894 if (statement == NULL || statement->extension == NULL) {
895 return NULL;
898 body = (osl_body_p)osl_generic_lookup(statement->extension, OSL_URI_BODY);
899 if (body != NULL)
900 return body;
901 ebody = (osl_extbody_p)osl_generic_lookup(statement->extension,
902 OSL_URI_EXTBODY);
903 if (ebody != NULL)
904 return ebody->body;
905 return NULL;