README file update
[openscop.git] / source / statement.c
blob01d7df79ab764554f690af7797cc7314c40fa876
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;
201 int tmp_nb_iterators = 0;
202 char ** tmp_iterators = NULL;
204 while (statement != NULL) {
205 nb_relations = 0;
207 // Switch iterator names to the current statement names if possible.
208 switched = 0;
209 if ((statement->nb_iterators > 0) && (names != NULL)) {
210 tmp_nb_iterators = names->nb_iterators;
211 tmp_iterators = names->iterators;
212 names->nb_iterators = statement->nb_iterators;
213 names->iterators = statement->iterators;
214 switched = 1;
217 fprintf(file, "# =============================================== ");
218 fprintf(file, "Statement %d\n", number);
220 fprintf(file, "# Number of relations describing the statement:\n");
222 if (statement->domain != NULL)
223 nb_relations ++;
224 if (statement->scattering != NULL)
225 nb_relations ++;
226 nb_relations += openscop_relation_list_count(statement->access);
228 fprintf(file, "%d\n\n", nb_relations);
230 fprintf(file, "# ---------------------------------------------- ");
231 fprintf(file, "%2d.1 Domain\n", number);
232 openscop_relation_print(file, statement->domain, names);
233 fprintf(file, "\n");
235 fprintf(file, "# ---------------------------------------------- ");
236 fprintf(file, "%2d.2 Scattering\n", number);
237 openscop_relation_print(file, statement->scattering, names);
238 fprintf(file, "\n");
240 fprintf(file, "# ---------------------------------------------- ");
241 fprintf(file, "%2d.3 Access\n", number);
242 openscop_relation_list_print_elts(file, statement->access, names);
243 fprintf(file, "\n");
245 fprintf(file, "# ---------------------------------------------- ");
246 fprintf(file, "%2d.4 Body\n", number);
247 if (statement->body != NULL) {
248 fprintf(file, "# Statement body is provided\n");
249 fprintf(file, "1\n");
250 if (statement->nb_iterators > 0) {
251 fprintf(file, "# Original iterator names\n");
252 for (i = 0; i < statement->nb_iterators; i++)
253 fprintf(file, "%s ", statement->iterators[i]);
254 fprintf(file, "\n");
256 else {
257 fprintf(file, "# No original iterator names\n");
259 fprintf(file, "# Statement body\n");
260 fprintf(file, "%s\n", statement->body);
262 else {
263 fprintf(file, "# Statement body is not provided\n");
264 fprintf(file, "0\n");
266 fprintf(file, "\n\n");
268 if (switched == 1) {
269 names->nb_iterators = tmp_nb_iterators;
270 names->iterators = tmp_iterators;
272 statement = statement->next;
273 number++;
278 /*****************************************************************************
279 * Reading function *
280 *****************************************************************************/
284 * openscop_statement_dispatch function:
285 * this function dispatches the relations from a relation list to the
286 * convenient fields of a statement structure: it extracts the domain,
287 * the scattering and the access list and store them accordingly in the
288 * statement structure provided as a parameter.
289 * \param stmt The statement where to dispatch the relations.
290 * \param list The "brute" relation list to sort and dispatch (freed).
292 static
293 void openscop_statement_dispatch(openscop_statement_p stmt,
294 openscop_relation_list_p list) {
295 openscop_relation_list_p domain_list;
296 openscop_relation_list_p scattering_list;
297 int nb_domains, nb_scattering, nb_accesses;
299 // Domain.
300 domain_list = openscop_relation_list_filter(list, OPENSCOP_TYPE_DOMAIN);
301 nb_domains = openscop_relation_list_count(domain_list);
302 if (nb_domains > 1) {
303 fprintf(stderr, "[OpenScop] Error: more than one domain for a "
304 "statement.\n");
305 exit(1);
307 if (domain_list != NULL) {
308 stmt->domain = domain_list->elt;
309 domain_list->elt = NULL;
310 openscop_relation_list_free(domain_list);
312 else {
313 stmt->domain = NULL;
316 // Scattering.
317 scattering_list=openscop_relation_list_filter(list,OPENSCOP_TYPE_SCATTERING);
318 nb_scattering = openscop_relation_list_count(scattering_list);
319 if (nb_scattering > 1) {
320 fprintf(stderr, "[OpenScop] Error: more than one scattering relation "
321 "for a statement.\n");
322 exit(1);
324 if (scattering_list != NULL) {
325 stmt->scattering = scattering_list->elt;
326 scattering_list->elt = NULL;
327 openscop_relation_list_free(scattering_list);
329 else {
330 stmt->scattering = NULL;
333 // Access.
334 stmt->access = openscop_relation_list_filter(list, OPENSCOP_TYPE_ACCESS);
335 nb_accesses = openscop_relation_list_count(stmt->access);
337 if ((nb_domains + nb_scattering + nb_accesses) !=
338 (openscop_relation_list_count(list))) {
339 fprintf(stderr, "[OpenScop] Error: unexpected relation type to define "
340 "a statement.\n");
341 exit(1);
344 openscop_relation_list_free(list);
349 * openscop_statement_read function:
350 * This function reads a openscop_statement_t structure from an input stream
351 * (possibly stdin).
352 * \param file The input stream.
353 * \param nb_parameters The number of global parameters.
354 * \return A pointer to the statement structure that has been read.
356 openscop_statement_p openscop_statement_read(FILE * file,
357 int nb_parameters) {
358 openscop_statement_p stmt = openscop_statement_malloc();
359 openscop_relation_list_p list;
360 char buff[OPENSCOP_MAX_STRING], * start, * end;
361 int expected_nb_iterators, nb_iterators;
363 if (file) {
364 // Read all statement relations.
365 list = openscop_relation_list_read(file);
367 // Store relations at the right place according to their type.
368 openscop_statement_dispatch(stmt, list);
370 // Read the body information, if any.
371 if (openscop_util_read_int(file, NULL) > 0) {
372 // Is there any iterator to read ?
373 if (stmt->domain != NULL) {
374 if (openscop_relation_is_matrix(stmt->domain)) {
375 if (nb_parameters == OPENSCOP_UNDEFINED) {
376 fprintf(stderr, "[OpenScop] Warning: undefined number of "
377 "parameters (no context ?), assuming 0.\n");
378 nb_parameters = 0;
381 expected_nb_iterators = stmt->domain->nb_columns -
382 nb_parameters - 2;
384 else {
385 expected_nb_iterators = stmt->domain->nb_output_dims;
388 else {
389 fprintf(stderr, "[OpenScop] Warning: no domain, assuming 0 "
390 "original iterator.\n");
391 expected_nb_iterators = 0;
394 // Read the original iterator names.
395 if (expected_nb_iterators > 0) {
396 stmt->iterators = openscop_util_strings_read(file, &nb_iterators);
397 stmt->nb_iterators = nb_iterators;
398 if (expected_nb_iterators != nb_iterators) {
399 fprintf(stderr, "[OpenScop] Warning: not the right number of "
400 "original iterators.\n");
404 // Read the body:
405 // - Skip blank/commented lines and spaces before the body.
406 start = openscop_util_skip_blank_and_comments(file, buff);
408 // - Remove the comments after the body.
409 end = start;
410 while ((*end != '#') && (*end != '\n'))
411 end++;
412 *end = '\0';
414 // - Copy the body.
415 stmt->body = strdup(start);
417 else {
418 stmt->nb_iterators = 0;
419 stmt->iterators = NULL;
420 stmt->body = NULL;
424 return stmt;
428 /*+***************************************************************************
429 * Memory allocation/deallocation functions *
430 *****************************************************************************/
434 * openscop_statement_malloc function:
435 * This function allocates the memory space for a openscop_statement_t
436 * structure and sets its fields with default values. Then it returns a pointer
437 * to the allocated space.
438 * \return A pointer to an empty statement with fields set to default values.
440 openscop_statement_p openscop_statement_malloc() {
441 openscop_statement_p statement;
443 statement = (openscop_statement_p)malloc(sizeof(openscop_statement_t));
444 if (statement == NULL) {
445 fprintf(stderr, "[OpenScop] Error: memory overflow.\n");
446 exit(1);
449 statement->domain = NULL;
450 statement->scattering = NULL;
451 statement->access = NULL;
452 statement->nb_iterators = 0;
453 statement->iterators = NULL;
454 statement->body = NULL;
455 statement->next = NULL;
457 return statement;
462 * openscop_statement_free function:
463 * This function frees the allocated memory for a openscop_statement_t
464 * structure.
465 * \param statement The pointer to the statement we want to free.
467 void openscop_statement_free(openscop_statement_p statement) {
468 int i;
469 openscop_statement_p next;
471 while (statement != NULL) {
472 next = statement->next;
473 openscop_relation_free(statement->domain);
474 openscop_relation_free(statement->scattering);
475 openscop_relation_list_free(statement->access);
476 if (statement->iterators != NULL) {
477 for (i = 0; i < statement->nb_iterators; i++)
478 free(statement->iterators[i]);
479 free(statement->iterators);
481 if (statement->body != NULL)
482 free(statement->body);
484 free(statement);
485 statement = next;
490 /*+***************************************************************************
491 * Processing functions *
492 *****************************************************************************/
496 * openscop_statement_add function:
497 * This function adds a statement "statement" at the end of the statement
498 * list pointed by "location".
499 * \param location Address of the first element of the statement list.
500 * \param statement The statement to add to the list.
502 void openscop_statement_add(openscop_statement_p * location,
503 openscop_statement_p statement) {
504 while (*location != NULL)
505 location = &((*location)->next);
507 *location = statement;
512 * openscop_statement_number function:
513 * This function returns the number of statements in the statement list
514 * provided as parameter.
515 * \param statement The first element of the statement list.
516 * \return The number of statements in the statement list.
518 int openscop_statement_number(openscop_statement_p statement) {
519 int number = 0;
521 while (statement != NULL) {
522 number++;
523 statement = statement->next;
525 return number;
530 * openscop_statement_copy function:
531 * This functions builds and returns a "hard copy" (not a pointer copy) of a
532 * openscop_statement_t data structure provided as parameter.
533 * \param statement The pointer to the statement we want to copy.
534 * \return A pointer to the full copy of the statement provided as parameter.
536 openscop_statement_p openscop_statement_copy(openscop_statement_p statement) {
537 int first = 1;
538 openscop_statement_p copy = NULL, node, previous = NULL;
540 while (statement != NULL) {
541 node = openscop_statement_malloc();
542 node->domain = openscop_relation_copy(statement->domain);
543 node->scattering = openscop_relation_copy(statement->scattering);
544 node->access = openscop_relation_list_copy(statement->access);
545 node->nb_iterators = statement->nb_iterators;
546 node->iterators = openscop_util_strings_copy(statement->iterators,
547 statement->nb_iterators);
548 node->body = strdup(statement->body);
549 node->next = NULL;
551 if (first) {
552 first = 0;
553 copy = node;
554 previous = node;
556 else {
557 previous->next = node;
558 previous = previous->next;
561 statement = statement->next;
564 return copy;
569 * openscop_statement_equal function:
570 * This function returns true if the two statements are the same, false
571 * otherwise (the usr field is not tested).
572 * \param s1 The first statement.
573 * \param s2 The second statement.
574 * \return 1 if s1 and s2 are the same (content-wise), 0 otherwise.
576 int openscop_statement_equal(openscop_statement_p s1,
577 openscop_statement_p s2) {
578 int i;
580 if (s1 == s2)
581 return 1;
583 if (((s1->next != NULL) && (s2->next == NULL)) ||
584 ((s1->next == NULL) && (s2->next != NULL)))
585 return 0;
587 if ((s1->next != NULL) && (s2->next != NULL))
588 if (!openscop_statement_equal(s1->next, s2->next))
589 return 0;
591 if ((s1->nb_iterators != s2->nb_iterators) ||
592 (!openscop_relation_equal(s1->domain, s2->domain)) ||
593 (!openscop_relation_equal(s1->scattering, s2->scattering)) ||
594 (!openscop_relation_list_equal(s1->access, s2->access)))
595 return 0;
597 if (((s1->body == NULL) && (s2->body != NULL)) ||
598 ((s1->body != NULL) && (s2->body == NULL)) ||
599 ((s1->body != NULL) && (s2->body != NULL) &&
600 (strcmp(s1->body, s2->body) != 0))) {
601 fprintf(stderr, "[OpenScop] info: bodies are not the same.\n");
602 return 0;
605 for (i = 0; i < s1->nb_iterators; i++) {
606 if (((s1->iterators[i] == NULL) && (s2->iterators[i] != NULL)) ||
607 ((s1->iterators[i] != NULL) && (s2->iterators[i] == NULL)) ||
608 ((s1->iterators[i] != NULL) && (s2->iterators[i] != NULL) &&
609 (strcmp(s1->iterators[i], s2->iterators[i]) != 0))) {
610 fprintf(stderr, "[OpenScop] info: original iterators "
611 "are not the same.\n");
612 return 0;
616 return 1;
621 * openscop_statement_integrity_check function:
622 * This function checks that a statement is "well formed" according to some
623 * expected properties (setting an expected value to OPENSCOP_UNDEFINED means
624 * that we do not expect a specific value). It returns 0 if the check failed
625 * or 1 if no problem has been detected.
626 * \param statement The statement we want to check.
627 * \param expected_nb_parameters Expected number of parameters.
628 * \return 0 if the integrity check fails, 1 otherwise.
630 int openscop_statement_integrity_check(openscop_statement_p statement,
631 int expected_nb_parameters) {
632 int expected_nb_iterators = OPENSCOP_UNDEFINED;
634 while (statement != NULL) {
635 // Check the domain.
636 if ((openscop_relation_is_matrix(statement->domain) &&
637 !openscop_relation_integrity_check(statement->domain,
638 OPENSCOP_TYPE_DOMAIN,
639 OPENSCOP_UNDEFINED,
640 OPENSCOP_UNDEFINED,
641 OPENSCOP_UNDEFINED)) ||
642 (!openscop_relation_is_matrix(statement->domain) &&
643 !openscop_relation_integrity_check(statement->domain,
644 OPENSCOP_TYPE_DOMAIN,
645 OPENSCOP_UNDEFINED,
647 expected_nb_parameters))) {
648 return 0;
651 // Get the number of iterators.
652 if (statement->domain != NULL) {
653 if (openscop_relation_is_matrix(statement->domain)) {
654 if (expected_nb_parameters != OPENSCOP_UNDEFINED)
655 expected_nb_iterators = statement->domain->nb_columns -
656 expected_nb_parameters - 2;
657 else
658 expected_nb_iterators = OPENSCOP_UNDEFINED;
660 else
661 expected_nb_iterators = statement->domain->nb_output_dims;
664 // Check the scattering relation.
665 if ((openscop_relation_is_matrix(statement->scattering) &&
666 !openscop_relation_integrity_check(statement->scattering,
667 OPENSCOP_TYPE_SCATTERING,
668 OPENSCOP_UNDEFINED,
669 OPENSCOP_UNDEFINED,
670 OPENSCOP_UNDEFINED)) ||
671 (!openscop_relation_is_matrix(statement->scattering) &&
672 !openscop_relation_integrity_check(statement->scattering,
673 OPENSCOP_TYPE_SCATTERING,
674 OPENSCOP_UNDEFINED,
675 expected_nb_iterators,
676 expected_nb_parameters))) {
677 return 0;
680 // Check the access relations.
681 if (!openscop_relation_list_integrity_check(statement->access,
682 OPENSCOP_TYPE_ACCESS,
683 OPENSCOP_UNDEFINED,
684 expected_nb_iterators,
685 expected_nb_parameters)) {
686 return 0;
689 if ((statement->nb_iterators > 0) &&
690 (statement->nb_iterators < statement->domain->nb_output_dims)) {
691 fprintf(stderr, "[OpenScop] Warning: not enough original iterator "
692 "names.\n");
693 return 0;
696 statement = statement->next;
699 return 1;