One list to rule all accesses
[openscop.git] / source / statement.c
blob31d215b37c77a002f5a4a7b09b0fb9933d86a9ac
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>
68 # include <openscop/statement.h>
71 /*+***************************************************************************
72 * Structure display functions *
73 *****************************************************************************/
76 /**
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,
89 int level) {
90 int i, j, first = 1, number = 1;
92 // Go to the right level.
93 for (j = 0; j < level; j++)
94 fprintf(file, "|\t");
96 if (statement != NULL)
97 fprintf(file, "+-- openscop_statement_t (S%d)\n", number);
98 else
99 fprintf(file, "+-- NULL statement\n");
101 while (statement != NULL) {
102 if (!first) {
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);
108 else
109 first = 0;
111 // A blank line.
112 for (j = 0; j <= level+1; j++)
113 fprintf(file, "|\t");
114 fprintf(file, "\n");
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 original iterator names.
126 for (i = 0; i <= level; i++)
127 fprintf(file, "|\t");
128 if (statement->nb_iterators > 0) {
129 fprintf(file, "+-- Original iterator strings:");
130 for (i = 0; i < statement->nb_iterators; i++)
131 fprintf(file, " %s", statement->iterators[i]);
132 fprintf(file, "\n");
134 else
135 fprintf(file, "+-- No original iterator string\n");
137 // A blank line.
138 for (i = 0; i <= level+1; i++)
139 fprintf(file, "|\t");
140 fprintf(file, "\n");
142 // Print the original statement body.
143 for (i = 0; i <= level; i++)
144 fprintf(file, "|\t");
145 if (statement->body != NULL)
146 fprintf(file, "+-- Original body: %s\n", statement->body);
147 else
148 fprintf(file, "+-- No original body\n");
150 // A blank line.
151 for (i = 0; i <= level+1; i++)
152 fprintf(file, "|\t");
153 fprintf(file, "\n");
155 statement = statement->next;
156 number++;
158 // Next line.
159 if (statement != NULL) {
160 for (j = 0; j <= level; j++)
161 fprintf(file, "|\t");
162 fprintf(file, "V\n");
166 // The last line.
167 for (j = 0; j <= level; j++)
168 fprintf(file, "|\t");
169 fprintf(file, "\n");
174 * openscop_statement_dump function:
175 * This function prints the content of a openscop_statement_t structure
176 * (*statement) into a file (file, possibly stdout).
177 * \param file File where informations are printed.
178 * \param statement The statement whose information have to be printed.
180 void openscop_statement_dump(FILE * file, openscop_statement_p statement) {
181 openscop_statement_idump(file, statement, 0);
186 * openscop_statement_print function:
187 * This function prints the content of a openscop_statement_t structure
188 * (*statement) into a file (file, possibly stdout) in the OpenScop format.
189 * \param file File where informations are printed.
190 * \param statement The statement whose information have to be printed.
191 * \param names The textual names of the various elements. Is is important
192 * that names->nb_parameters is exact if the matrix
193 * representation is used. Set to NULL if printing comments
194 * is not needed.
196 void openscop_statement_print(FILE * file,
197 openscop_statement_p statement,
198 openscop_names_p names) {
199 int i, switched, number = 1;
200 int nb_relations = 0;
201 int tmp_nb_iterators = 0;
202 char ** tmp_iterators = NULL;
204 while (statement != NULL) {
205 // Switch iterator names to the current statement names if possible.
206 switched = 0;
207 if ((statement->nb_iterators > 0) && (names != NULL)) {
208 tmp_nb_iterators = names->nb_iterators;
209 tmp_iterators = names->iterators;
210 names->nb_iterators = statement->nb_iterators;
211 names->iterators = statement->iterators;
212 switched = 1;
215 fprintf(file, "# =============================================== ");
216 fprintf(file, "Statement %d\n", number);
218 fprintf(file, "# Number of relations describing the statement:\n");
220 if (statement->domain != NULL)
221 nb_relations ++;
222 if (statement->scattering != NULL)
223 nb_relations ++;
224 nb_relations += openscop_relation_list_count(statement->access);
226 fprintf(file, "%d\n", nb_relations);
228 fprintf(file, "# ---------------------------------------------- ");
229 fprintf(file, "%2d.1 Domain\n", number);
230 openscop_relation_print(file, statement->domain, names);
231 fprintf(file, "\n");
233 fprintf(file, "# ---------------------------------------------- ");
234 fprintf(file, "%2d.2 Scattering\n", number);
235 openscop_relation_print(file, statement->scattering, names);
236 fprintf(file, "\n");
238 fprintf(file, "# ---------------------------------------------- ");
239 fprintf(file, "%2d.3 Access\n", number);
240 openscop_relation_list_print_elts(file, statement->access, names);
241 fprintf(file, "\n");
243 fprintf(file, "# ---------------------------------------------- ");
244 fprintf(file, "%2d.4 Body\n", number);
245 if (statement->body != NULL) {
246 fprintf(file, "# Statement body is provided\n");
247 fprintf(file, "1\n");
248 if (statement->nb_iterators > 0) {
249 fprintf(file, "# Original iterator names\n");
250 for (i = 0; i < statement->nb_iterators; i++)
251 fprintf(file, "%s ", statement->iterators[i]);
252 fprintf(file, "\n");
254 else {
255 fprintf(file, "# No original iterator names\n");
257 fprintf(file, "# Statement body\n");
258 fprintf(file, "%s\n", statement->body);
260 else {
261 fprintf(file, "# Statement body is not provided\n");
262 fprintf(file, "0\n");
264 fprintf(file, "\n\n");
266 if (switched == 1) {
267 statement->nb_iterators = tmp_nb_iterators;
268 statement->iterators = tmp_iterators;
270 statement = statement->next;
271 number++;
276 /*****************************************************************************
277 * Reading function *
278 *****************************************************************************/
282 * openscop_statement_dispatch function:
283 * this function dispatches the relations from a relation list to the
284 * convenient fields of a statement structure: it extracts the domain,
285 * the scattering and the access list and store them accordingly in the
286 * statement structure provided as a parameter.
287 * \param stmt The statement where to dispatch the relations.
288 * \param list The "brute" relation list to sort and dispatch (freed).
290 static
291 void openscop_statement_dispatch(openscop_statement_p stmt,
292 openscop_relation_list_p list) {
293 openscop_relation_list_p domain_list;
294 openscop_relation_list_p scattering_list;
295 int nb_domains, nb_scattering, nb_accesses;
297 // Domain.
298 domain_list = openscop_relation_list_filter(list, OPENSCOP_TYPE_DOMAIN);
299 nb_domains = openscop_relation_list_count(domain_list);
300 if (nb_domains > 1) {
301 fprintf(stderr, "[OpenScop] Error: more than one domain for a "
302 "statement.\n");
303 exit(1);
305 if (domain_list != NULL)
306 stmt->domain = domain_list->elt;
307 else
308 stmt->domain = NULL;
310 // Scattering.
311 scattering_list=openscop_relation_list_filter(list,OPENSCOP_TYPE_SCATTERING);
312 nb_scattering = openscop_relation_list_count(scattering_list);
313 if (nb_scattering > 1) {
314 fprintf(stderr, "[OpenScop] Error: more than one scattering relation "
315 "for a statement.\n");
316 exit(1);
318 if (scattering_list != NULL)
319 stmt->scattering = scattering_list->elt;
320 else
321 stmt->scattering = NULL;
323 // Access.
324 stmt->access = openscop_relation_list_filter(list, OPENSCOP_TYPE_ACCESS);
325 nb_accesses = openscop_relation_list_count(stmt->access);
327 if ((nb_domains + nb_scattering + nb_accesses) !=
328 (openscop_relation_list_count(list))) {
329 fprintf(stderr, "[OpenScop] Error: unexpected relation type to define "
330 "a statement.\n");
331 exit(1);
334 openscop_relation_list_free(list);
339 * openscop_statement_read function:
340 * This function reads a openscop_statement_t structure from an input stream
341 * (possibly stdin).
342 * \param file The input stream.
343 * \param nb_parameters The number of global parameters.
344 * \return A pointer to the statement structure that has been read.
346 openscop_statement_p openscop_statement_read(FILE * file,
347 int nb_parameters) {
348 openscop_statement_p stmt = openscop_statement_malloc();
349 openscop_relation_list_p list;
350 char buff[OPENSCOP_MAX_STRING], * start, * end;
351 int expected_nb_iterators, nb_iterators;
353 if (file) {
354 // Read all statement relations.
355 list = openscop_relation_list_read(file);
357 // Store relations at the right place according to their type.
358 openscop_statement_dispatch(stmt, list);
360 // Read the body information, if any.
361 if (openscop_util_read_int(file, NULL) > 0) {
362 // Is there any iterator to read ?
363 if (openscop_relation_is_matrix(stmt->domain)) {
364 if (nb_parameters == OPENSCOP_UNDEFINED) {
365 fprintf(stderr, "[OpenScop] Warning: undefined number of "
366 "parameters (no context ?), assuming 0.\n");
367 nb_parameters = 0;
370 expected_nb_iterators = stmt->domain->nb_columns -
371 nb_parameters - 2;
373 else {
374 expected_nb_iterators = stmt->domain->nb_output_dims;
377 // Read the original iterator names.
378 if (expected_nb_iterators > 0) {
379 stmt->iterators = openscop_util_strings_read(file, &nb_iterators);
380 stmt->nb_iterators = nb_iterators;
381 if (expected_nb_iterators != nb_iterators) {
382 fprintf(stderr, "[OpenScop] Warning: not the right number of "
383 "original iterators.\n");
387 // Read the body:
388 // - Skip blank/commented lines and spaces before the body.
389 start = openscop_util_skip_blank_and_comments(file, buff);
391 // - Remove the comments after the body.
392 end = start;
393 while ((*end != '#') && (*end != '\n'))
394 end++;
395 *end = '\0';
397 // - Copy the body.
398 stmt->body = strdup(start);
400 else {
401 stmt->nb_iterators = 0;
402 stmt->iterators = NULL;
403 stmt->body = NULL;
407 return stmt;
411 /*+***************************************************************************
412 * Memory allocation/deallocation functions *
413 *****************************************************************************/
417 * openscop_statement_malloc function:
418 * This function allocates the memory space for a openscop_statement_t
419 * structure and sets its fields with default values. Then it returns a pointer
420 * to the allocated space.
421 * \return A pointer to an empty statement with fields set to default values.
423 openscop_statement_p openscop_statement_malloc() {
424 openscop_statement_p statement;
426 statement = (openscop_statement_p)malloc(sizeof(openscop_statement_t));
427 if (statement == NULL) {
428 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
429 exit(1);
432 statement->domain = NULL;
433 statement->scattering = NULL;
434 statement->access = NULL;
435 statement->nb_iterators = 0;
436 statement->iterators = NULL;
437 statement->body = NULL;
438 statement->next = NULL;
440 return statement;
445 * openscop_statement_free function:
446 * This function frees the allocated memory for a openscop_statement_t
447 * structure.
448 * \param statement The pointer to the statement we want to free.
450 void openscop_statement_free(openscop_statement_p statement) {
451 int i;
452 openscop_statement_p next;
454 while (statement != NULL) {
455 next = statement->next;
456 openscop_relation_free(statement->domain);
457 openscop_relation_free(statement->scattering);
458 openscop_relation_list_free(statement->access);
459 if (statement->iterators != NULL) {
460 for (i = 0; i < statement->nb_iterators; i++)
461 free(statement->iterators[i]);
462 free(statement->iterators);
464 if (statement->body != NULL)
465 free(statement->body);
467 free(statement);
468 statement = next;
473 /*+***************************************************************************
474 * Processing functions *
475 *****************************************************************************/
479 * openscop_statement_add function:
480 * This function adds a statement "statement" at the end of the statement
481 * list pointed by "location".
482 * \param location Address of the first element of the statement list.
483 * \param statement The statement to add to the list.
485 void openscop_statement_add(openscop_statement_p * location,
486 openscop_statement_p statement) {
487 while (*location != NULL)
488 location = &((*location)->next);
490 *location = statement;
495 * openscop_statement_number function:
496 * This function returns the number of statements in the statement list
497 * provided as parameter.
498 * \param statement The first element of the statement list.
499 * \return The number of statements in the statement list.
501 int openscop_statement_number(openscop_statement_p statement) {
502 int number = 0;
504 while (statement != NULL) {
505 number++;
506 statement = statement->next;
508 return number;
513 * openscop_statement_copy function:
514 * This functions builds and returns a "hard copy" (not a pointer copy) of a
515 * openscop_statement_t data structure provided as parameter.
516 * \param statement The pointer to the statement we want to copy.
517 * \return A pointer to the full copy of the statement provided as parameter.
519 openscop_statement_p openscop_statement_copy(openscop_statement_p statement) {
520 int first = 1;
521 openscop_statement_p copy = NULL, node, previous = NULL;
523 while (statement != NULL) {
524 node = openscop_statement_malloc();
525 node->domain = openscop_relation_copy(statement->domain);
526 node->scattering = openscop_relation_copy(statement->scattering);
527 node->access = openscop_relation_list_copy(statement->access);
528 node->nb_iterators = statement->nb_iterators;
529 node->iterators = openscop_util_strings_copy(statement->iterators,
530 statement->nb_iterators);
531 node->body = strdup(statement->body);
532 node->next = NULL;
534 if (first) {
535 first = 0;
536 copy = node;
537 previous = node;
539 else {
540 previous->next = node;
541 previous = previous->next;
544 statement = statement->next;
547 return copy;
552 * openscop_statement_equal function:
553 * This function returns true if the two statements are the same, false
554 * otherwise (the usr field is not tested).
555 * \param s1 The first statement.
556 * \param s2 The second statement.
557 * \return 1 if s1 and s2 are the same (content-wise), 0 otherwise.
559 int openscop_statement_equal(openscop_statement_p s1,
560 openscop_statement_p s2) {
561 int i;
563 if (s1 == s2)
564 return 1;
566 if (((s1->next != NULL) && (s2->next == NULL)) ||
567 ((s1->next == NULL) && (s2->next != NULL)))
568 return 0;
570 if ((s1->next != NULL) && (s2->next != NULL))
571 if (!openscop_statement_equal(s1->next, s2->next))
572 return 0;
574 if ((s1->nb_iterators != s2->nb_iterators) ||
575 (!openscop_relation_equal(s1->domain, s2->domain)) ||
576 (!openscop_relation_equal(s1->scattering, s2->scattering)) ||
577 (!openscop_relation_list_equal(s1->access, s2->access)))
578 return 0;
580 if (((s1->body == NULL) && (s2->body != NULL)) ||
581 ((s1->body != NULL) && (s2->body == NULL)) ||
582 ((s1->body != NULL) && (s2->body != NULL) &&
583 (strcmp(s1->body, s2->body) != 0))) {
584 fprintf(stderr, "[OpenScop] info: bodies are not the same.\n");
585 return 0;
588 for (i = 0; i < s1->nb_iterators; i++) {
589 if (((s1->iterators[i] == NULL) && (s2->iterators[i] != NULL)) ||
590 ((s1->iterators[i] != NULL) && (s2->iterators[i] == NULL)) ||
591 ((s1->iterators[i] != NULL) && (s2->iterators[i] != NULL) &&
592 (strcmp(s1->iterators[i], s2->iterators[i]) != 0))) {
593 fprintf(stderr, "[OpenScop] info: original iterators "
594 "are not the same.\n");
595 return 0;
599 return 1;
604 * openscop_statement_integrity_check function:
605 * This function checks that a statement is "well formed" according to some
606 * expected properties (setting an expected value to OPENSCOP_UNDEFINED means
607 * that we do not expect a specific value). It returns 0 if the check failed
608 * or 1 if no problem has been detected.
609 * \param statement The statement we want to check.
610 * \param expected_nb_parameters Expected number of parameters.
611 * \return 0 if the integrity check fails, 1 otherwise.
613 int openscop_statement_integrity_check(openscop_statement_p statement,
614 int expected_nb_parameters) {
615 int expected_nb_iterators = OPENSCOP_UNDEFINED;
617 while (statement != NULL) {
618 // Check the domain.
619 if ((openscop_relation_is_matrix(statement->domain) &&
620 !openscop_relation_integrity_check(statement->domain,
621 OPENSCOP_TYPE_DOMAIN,
622 OPENSCOP_UNDEFINED,
623 OPENSCOP_UNDEFINED,
624 OPENSCOP_UNDEFINED)) ||
625 (!openscop_relation_is_matrix(statement->domain) &&
626 !openscop_relation_integrity_check(statement->domain,
627 OPENSCOP_TYPE_DOMAIN,
628 OPENSCOP_UNDEFINED,
630 expected_nb_parameters))) {
631 return 0;
634 // Get the number of iterators.
635 if (statement->domain != NULL) {
636 if (openscop_relation_is_matrix(statement->domain)) {
637 if (expected_nb_parameters != OPENSCOP_UNDEFINED)
638 expected_nb_iterators = statement->domain->nb_columns -
639 expected_nb_parameters - 2;
640 else
641 expected_nb_iterators = OPENSCOP_UNDEFINED;
643 else
644 expected_nb_iterators = statement->domain->nb_output_dims;
647 // Check the scattering relation.
648 if ((openscop_relation_is_matrix(statement->scattering) &&
649 !openscop_relation_integrity_check(statement->scattering,
650 OPENSCOP_TYPE_SCATTERING,
651 OPENSCOP_UNDEFINED,
652 OPENSCOP_UNDEFINED,
653 OPENSCOP_UNDEFINED)) ||
654 (!openscop_relation_is_matrix(statement->scattering) &&
655 !openscop_relation_integrity_check(statement->scattering,
656 OPENSCOP_TYPE_SCATTERING,
657 OPENSCOP_UNDEFINED,
658 expected_nb_iterators,
659 expected_nb_parameters))) {
660 return 0;
663 // Check the access relations.
664 if (!openscop_relation_list_integrity_check(statement->access,
665 OPENSCOP_TYPE_ACCESS,
666 OPENSCOP_UNDEFINED,
667 expected_nb_iterators,
668 expected_nb_parameters)) {
669 return 0;
672 if ((statement->nb_iterators > 0) &&
673 (statement->nb_iterators < statement->domain->nb_output_dims)) {
674 fprintf(stderr, "[OpenScop] Warning: not enough original iterator "
675 "names.\n");
676 return 0;
679 statement = statement->next;
682 return 1;