Set to 0.6.0
[openscop.git] / source / scop.c
blob765df185e918c641efbf3c2a56cfcd3fd97cb398
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
4 **-----------------------------------------------------------------**
5 ** scop.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 *****************************************************************************/
63 # include <stdlib.h>
64 # include <stdio.h>
65 # include <ctype.h>
66 # include <string.h>
67 # include <openscop/scop.h>
70 /*+***************************************************************************
71 * Structure display functions *
72 *****************************************************************************/
75 /**
76 * openscop_scop_idump function:
77 * this function displays an openscop_scop_t structure (*scop) into a
78 * file (file, possibly stdout) in a way that trends to be understandable. It
79 * includes an indentation level (level) in order to work with others
80 * print_structure functions.
81 * \param file The file where the information has to be printed.
82 * \param scop The scop structure whose information has to be printed.
83 * \param level Number of spaces before printing, for each line.
85 void openscop_scop_idump(FILE * file, openscop_scop_p scop, int level) {
86 int j, first = 1;
88 // Go to the right level.
89 for (j = 0; j < level; j++)
90 fprintf(file, "|\t");
92 if (scop != NULL)
93 fprintf(file, "+-- openscop_scop_t\n");
94 else
95 fprintf(file, "+-- NULL scop\n");
97 while (scop != NULL) {
98 if (!first) {
99 // Go to the right level.
100 for (j = 0; j < level; j++)
101 fprintf(file, "|\t");
102 fprintf(file, "| openscop_scop_t\n");
104 else
105 first = 0;
107 // A blank line.
108 for (j = 0; j <= level+1; j++)
109 fprintf(file, "|\t");
110 fprintf(file, "\n");
112 // Print the version.
113 for (j = 0; j < level; j++)
114 fprintf(file, "|\t");
115 fprintf(file, "|\tVersion: %d\n", scop->version);
117 // A blank line.
118 for (j = 0; j <= level+1; j++)
119 fprintf(file, "|\t");
120 fprintf(file, "\n");
122 // Print the language.
123 for (j = 0; j < level; j++)
124 fprintf(file, "|\t");
125 fprintf(file, "|\tLanguage: %s\n", scop->language);
127 // A blank line.
128 for (j = 0; j <= level+1; j++)
129 fprintf(file, "|\t");
130 fprintf(file, "\n");
132 // Print the context of the scop.
133 openscop_relation_idump(file, scop->context, level+1);
135 // Print the parameters.
136 openscop_generic_idump(file, scop->parameters, level+1);
138 // Print the statements.
139 openscop_statement_idump(file, scop->statement, level+1);
141 // Print the registered extension interfaces.
142 openscop_interface_idump(file, scop->registry, level+1);
144 // Print the extensions.
145 openscop_generic_idump(file, scop->extension, level+1);
147 scop = scop->next;
149 // Next line.
150 if (scop != NULL) {
151 for (j = 0; j <= level; j++)
152 fprintf(file, "|\t");
153 fprintf(file, "V\n");
157 // The last line.
158 for (j = 0; j <= level; j++)
159 fprintf(file, "|\t");
160 fprintf(file, "\n");
165 * openscop_scop_dump function:
166 * this function prints the content of an openscop_scop_t structure (*scop)
167 * into a file (file, possibly stdout).
168 * \param file The file where the information has to be printed.
169 * \param scop The scop structure whose information has to be printed.
171 void openscop_scop_dump(FILE * file, openscop_scop_p scop) {
172 openscop_scop_idump(file, scop, 0);
176 #if 0
178 * openscop_scop_name_limits function:
179 * this function finds the (maximum) number of various elements of a scop and
180 * return the values through parameters. To ensure the correctness of the
181 * results, an integrity check of the input scop should be run before calling
182 * this function.
183 * \param scop The scop to analyse.
184 * \parem nb_parameters The number of parameters in the scop (output).
185 * \parem nb_iterators The number of iterators in the scop (output).
186 * \parem nb_scattdims The number of scattdims in the scop (output).
187 * \parem nb_localdims The number of local dimensions in the scop (output).
188 * \parem nb_arrays The number of arrays in the scop (output).
190 static
191 void openscop_scop_name_limits(openscop_scop_p scop,
192 int * nb_parameters,
193 int * nb_iterators,
194 int * nb_scattdims,
195 int * nb_localdims,
196 int * nb_arrays) {
197 int array_id;
198 openscop_statement_p statement;
199 openscop_relation_list_p list;
201 // * The number of parameters is collected from the context,
202 // * The numbers of local dimensions are collected from all relations
203 // in the corresponding field.
204 *nb_parameters = 0;
205 *nb_localdims = 0;
206 if (scop->context != NULL) {
207 *nb_parameters = scop->context->nb_parameters;
208 *nb_localdims = scop->context->nb_local_dims;
211 *nb_iterators = 0;
212 *nb_scattdims = 0;
213 *nb_arrays = 0;
214 statement = scop->statement;
215 while (statement != NULL) {
216 // * The number of iterators are defined by iteration domains,
217 // it corresponds to the #output_dims.
218 if (statement->domain != NULL) {
219 if (statement->domain->nb_output_dims > *nb_iterators)
220 *nb_iterators = statement->domain->nb_output_dims;
222 if (statement->domain->nb_local_dims > *nb_localdims)
223 *nb_localdims = statement->domain->nb_local_dims;
226 // * The number of scattdims are defined by scattering,
227 // it corresponds to the #output_dims.
228 if (statement->scattering != NULL) {
229 if (statement->scattering->nb_output_dims > *nb_scattdims)
230 *nb_scattdims = statement->scattering->nb_output_dims;
232 if (statement->scattering->nb_local_dims > *nb_localdims)
233 *nb_localdims = statement->scattering->nb_local_dims;
236 // * The number of arrays are defined by accesses,
237 list = statement->access;
238 while (list != NULL) {
239 array_id = openscop_relation_get_array_id(list->elt);
240 if (array_id > *nb_arrays)
241 *nb_arrays = array_id;
243 list = list->next;
246 statement = statement->next;
252 * openscop_scop_full_names function:
253 * this function generates an openscop_names_p structure which contains
254 * enough names for the scop provided as parameter, for each kind of names.
255 * If the names contained in the input scop are not sufficient, this function
256 * generated the missing names.
257 * \param scop The scop we need a name for each element.
258 * \return A set of names for the scop.
260 static
261 openscop_names_p openscop_scop_full_names(openscop_scop_p scop) {
262 int nb_parameters;
263 int nb_iterators;
264 int nb_scattdims;
265 int nb_localdims;
266 int nb_arrays;
267 openscop_arrays_p arrays;
268 openscop_names_p names;
270 names = openscop_names_clone(scop->names);
272 // Extract array names information from extensions.
273 openscop_strings_free(names->arrays, names->nb_arrays);
274 arrays = (openscop_arrays_p)openscop_extension_lookup(scop->extension,
275 OPENSCOP_EXTENSION_ARRAYS);
276 names->arrays = openscop_arrays_generate_names(arrays,
277 &(names->nb_arrays));
279 // Complete names if necessary.
280 openscop_scop_name_limits(scop, &nb_parameters,
281 &nb_iterators,
282 &nb_scattdims,
283 &nb_localdims,
284 &nb_arrays);
286 openscop_strings_complete(&names->parameters, &names->nb_parameters,
287 "P_", nb_parameters);
289 openscop_strings_complete(&names->iterators, &names->nb_iterators,
290 "i_", nb_iterators);
292 openscop_strings_complete(&names->scattdims, &names->nb_scattdims,
293 "s_", nb_scattdims);
295 openscop_strings_complete(&names->localdims, &names->nb_localdims,
296 "l_", nb_localdims);
298 openscop_strings_complete(&names->arrays, &names->nb_arrays,
299 "A_", nb_arrays);
301 return names;
303 #endif
307 * openscop_scop_print function:
308 * this function prints the content of an openscop_scop_t structure (*scop)
309 * into a file (file, possibly stdout) in the OpenScop textual format.
310 * \param file The file where the information has to be printed.
311 * \param scop The scop structure whose information has to be printed.
313 void openscop_scop_print(FILE * file, openscop_scop_p scop) {
315 if (scop == NULL) {
316 fprintf(file, "# NULL scop\n");
317 return;
319 else {
320 fprintf(file, "# [File generated by the OpenScop Library %s %s bits]\n",
321 OPENSCOP_RELEASE,OPENSCOP_VERSION);
324 if (openscop_scop_integrity_check(scop) == 0)
325 OPENSCOP_warning("OpenScop integrity check failed. "
326 "Something may go wrong.");
328 while (scop != NULL) {
329 fprintf(file, "\n"OPENSCOP_TAG_START_SCOP"\n\n");
330 fprintf(file, "# =============================================== "
331 "Global\n");
332 fprintf(file, "# Language\n");
333 fprintf(file, "%s\n\n", scop->language);
335 fprintf(file, "# Context\n");
336 openscop_relation_print(file, scop->context);
337 fprintf(file, "\n");
339 openscop_util_print_provided(file,
340 openscop_generic_hasURI(scop->parameters, OPENSCOP_URI_STRINGS),
341 "Parameters are");
342 openscop_generic_print(file, scop->parameters);
344 fprintf(file, "# Number of statements\n");
345 fprintf(file, "%d\n\n",openscop_statement_number(scop->statement));
347 openscop_statement_print(file, scop->statement);
349 if (scop->extension) {
350 fprintf(file, "# =============================================== "
351 "Extensions\n");
352 openscop_generic_print(file, scop->extension);
354 fprintf(file, "\n"OPENSCOP_TAG_END_SCOP"\n\n");
356 scop = scop->next;
361 /*****************************************************************************
362 * Reading function *
363 *****************************************************************************/
367 * openscop_scop_read function:
368 * this function reads a list of scop structures from a file (possibly stdin)
369 * complying to the OpenScop textual format and returns a pointer to this
370 * scop list. If some relation properties (number of input/output/local
371 * dimensions and number of parameters) are undefined, it will define them
372 * according to the available information.
373 * \param file The file where the scop has to be read.
374 * \return A pointer to the scop structure that has been read.
376 openscop_scop_p openscop_scop_read(FILE * file) {
377 openscop_scop_p list = NULL, current = NULL, scop;
378 openscop_statement_p stmt = NULL;
379 openscop_statement_p prev = NULL;
380 openscop_interface_p interface;
381 openscop_strings_p language;
382 int nb_statements;
383 char buffer[OPENSCOP_MAX_STRING];
384 char * tmp, * start;
385 int first = 1;
386 int i;
388 if (file == NULL)
389 return NULL;
391 while(1) {
393 // I. START TAG
395 tmp = openscop_util_read_uptotag(file, OPENSCOP_TAG_START_SCOP);
396 free(tmp);
397 if (feof(file)) {
398 OPENSCOP_info("no more scop in the file");
399 break;
402 scop = openscop_scop_malloc();
405 // II. CONTEXT PART
408 // Read the language.
409 language = openscop_strings_read(file);
410 if (openscop_strings_size(language) == 0)
411 OPENSCOP_error("no language (backend) specified");
413 if (openscop_strings_size(language) > 1)
414 OPENSCOP_warning("uninterpreted information (after language)");
416 if (language != NULL) {
417 scop->language = strdup(language->string[0]);
418 openscop_strings_free(language);
421 // Read the context domain.
422 scop->context = openscop_relation_read(file);
424 // Read the parameters.
425 scop->parameter_type = OPENSCOP_TYPE_STRING;
426 if (openscop_util_read_int(file, NULL) > 0) {
427 interface = openscop_strings_interface();
428 start = openscop_util_skip_blank_and_comments(file, buffer);
429 scop->parameters = openscop_generic_sread(start, interface);
430 openscop_interface_free(interface);
434 // III. STATEMENT PART
437 // Read the number of statements.
438 nb_statements = openscop_util_read_int(file, NULL);
440 for (i = 0; i < nb_statements; i++) {
441 // Read each statement.
442 stmt = openscop_statement_read(file);
443 if (scop->statement == NULL)
444 scop->statement = stmt;
445 else
446 prev->next = stmt;
447 prev = stmt;
451 // IV. EXTENSION PART (TO THE END TAG)
454 // Read up the end tag (if any), and store extensions.
455 scop->extension = openscop_generic_read(file, scop->registry);
457 // Add the new scop to the list.
458 if (first) {
459 list = scop;
460 first = 0;
462 else {
463 current->next = scop;
465 current = scop;
468 if (!openscop_scop_integrity_check(list))
469 OPENSCOP_warning("scop integrity check failed");
471 return list;
475 /*+***************************************************************************
476 * Memory allocation/deallocation functions *
477 *****************************************************************************/
481 * openscop_scop_register_default_extensions function:
482 * this function registers the default OpenScop Library extensions to an
483 * existing scop.
484 * \param scop The scop for which default options have to be registered.
486 static
487 void openscop_scop_register_default_extensions(openscop_scop_p scop) {
489 openscop_interface_add(&scop->registry, openscop_textual_interface());
490 openscop_interface_add(&scop->registry, openscop_comment_interface());
491 openscop_interface_add(&scop->registry, openscop_arrays_interface());
492 openscop_interface_add(&scop->registry, openscop_lines_interface());
493 openscop_interface_add(&scop->registry, openscop_irregular_interface());
498 * openscop_scop_malloc function:
499 * this function allocates the memory space for a openscop_scop_t structure and
500 * sets its fields with default values. Then it returns a pointer to the
501 * allocated space.
502 * \return A pointer to an empty scop with fields set to default values.
504 openscop_scop_p openscop_scop_malloc() {
505 openscop_scop_p scop;
507 OPENSCOP_malloc(scop, openscop_scop_p, sizeof(openscop_scop_t));
508 scop->version = 1;
509 scop->language = NULL;
510 scop->context = NULL;
511 scop->parameter_type = OPENSCOP_UNDEFINED;
512 scop->parameters = NULL;
513 scop->statement = NULL;
514 scop->registry = NULL;
515 scop->extension = NULL;
516 scop->usr = NULL;
517 scop->next = NULL;
518 openscop_scop_register_default_extensions(scop);
520 return scop;
525 * openscop_scop_free function:
526 * This function frees the allocated memory for a openscop_scop_t structure.
527 * \param scop The pointer to the scop we want to free.
529 void openscop_scop_free(openscop_scop_p scop) {
530 openscop_scop_p tmp;
532 while (scop != NULL) {
533 if (scop->language != NULL)
534 free(scop->language);
535 openscop_generic_free(scop->parameters);
536 openscop_relation_free(scop->context);
537 openscop_statement_free(scop->statement);
538 openscop_interface_free(scop->registry);
539 openscop_generic_free(scop->extension);
541 tmp = scop->next;
542 free(scop);
543 scop = tmp;
548 /*+***************************************************************************
549 * Processing functions *
550 *****************************************************************************/
554 * openscop_scop_clone function:
555 * This functions builds and returns a "hard copy" (not a pointer copy)
556 * of a openscop_statement_t data structure provided as parameter.
557 * Note that the usr field is not touched by this function.
558 * \param statement The pointer to the scop we want to clone.
559 * \return A pointer to the full clone of the scop provided as parameter.
561 openscop_scop_p openscop_scop_clone(openscop_scop_p scop) {
562 openscop_scop_p clone = NULL, node, previous = NULL;
563 int first = 1;
565 while (scop != NULL) {
566 node = openscop_scop_malloc();
567 node->version = scop->version;
568 if (scop->language != NULL)
569 node->language = strdup(scop->language);
570 node->context = openscop_relation_clone(scop->context);
571 node->parameter_type = scop->parameter_type;
572 node->parameters = openscop_generic_clone(scop->parameters);
573 node->statement = openscop_statement_clone(scop->statement);
574 node->registry = openscop_interface_clone(scop->registry);
575 node->extension = openscop_generic_clone(scop->extension);
577 if (first) {
578 first = 0;
579 clone = node;
580 previous = node;
582 else {
583 previous->next = node;
584 previous = previous->next;
587 scop = scop->next;
590 return clone;
595 * openscop_scop_equal function:
596 * this function returns true if the two scops are the same, false
597 * otherwise (the usr field is not tested).
598 * \param s1 The first scop.
599 * \param s2 The second scop.
600 * \return 1 if s1 and s2 are the same (content-wise), 0 otherwise.
602 int openscop_scop_equal(openscop_scop_p s1, openscop_scop_p s2) {
604 while ((s1 != NULL) && (s2 != NULL)) {
605 if (s1 == s2)
606 return 1;
608 if (s1->version != s2->version) {
609 OPENSCOP_info("versions are not the same");
610 return 0;
613 if (strcmp(s1->language, s2->language) != 0) {
614 OPENSCOP_info("languages are not the same");
615 return 0;
618 if (!openscop_relation_equal(s1->context, s2->context)) {
619 OPENSCOP_info("contexts are not the same");
620 return 0;
623 if (s1->parameter_type != s2->parameter_type) {
624 OPENSCOP_info("parameter types are not the same");
625 return 0;
628 if (!openscop_generic_equal(s1->parameters, s2->parameters)) {
629 OPENSCOP_info("parameters are not the same");
630 return 0;
633 if (!openscop_statement_equal(s1->statement, s2->statement)) {
634 OPENSCOP_info("statements are not the same");
635 return 0;
638 if (!openscop_interface_equal(s1->registry, s2->registry)) {
639 OPENSCOP_info("registries are not the same");
640 return 0;
643 if (!openscop_generic_equal(s1->extension, s2->extension)) {
644 OPENSCOP_info("extensions are not the same");
645 return 0;
648 s1 = s1->next;
649 s2 = s2->next;
652 if (((s1 == NULL) && (s2 != NULL)) || ((s1 != NULL) && (s2 == NULL)))
653 return 0;
655 return 1;
660 * openscop_scop_integrity_check function:
661 * This function checks that a scop is "well formed". It returns 0 if the
662 * check failed or 1 if no problem has been detected.
663 * \param scop The scop we want to check.
664 * \return 0 if the integrity check fails, 1 otherwise.
666 int openscop_scop_integrity_check(openscop_scop_p scop) {
667 int expected_nb_parameters;
670 while (scop != NULL) {
671 // Check the language.
672 if ((scop->language != NULL) &&
673 (!strcmp(scop->language, "caml") || !strcmp(scop->language, "Caml") ||
674 !strcmp(scop->language, "ocaml") || !strcmp(scop->language, "OCaml")))
675 fprintf(stderr, "[OpenScop] Alert: What ?! Caml ?! Are you sure ?!?!\n");
677 // Check the context.
678 if (!openscop_relation_integrity_check(scop->context,
679 OPENSCOP_TYPE_CONTEXT,
680 OPENSCOP_UNDEFINED,
681 OPENSCOP_UNDEFINED,
682 OPENSCOP_UNDEFINED))
683 return 0;
685 // Get the number of parameters.
686 if (scop->context != NULL)
687 expected_nb_parameters = scop->context->nb_parameters;
688 else
689 expected_nb_parameters = OPENSCOP_UNDEFINED;
691 // TODO : check the number of parameter strings.
693 if (!openscop_statement_integrity_check(scop->statement,
694 expected_nb_parameters))
695 return 0;
697 scop = scop->next;
700 return 1;
705 * openscop_scop_register_extension function:
706 * this function registers a list of extension interfaces to a scop, i.e., it
707 * adds them to the scop registry. In addition, it will extract extensions
708 * corresponding to those interfaces from the textual form of the extensions
709 * (if any) and add them to the scop extension list.
710 * \param scop The scop for which an extension has to be registered.
711 * \param interface The extension interface to register within the scop.
713 void openscop_scop_register_extension(openscop_scop_p scop,
714 openscop_interface_p interface) {
715 openscop_generic_p textual, new;
716 char * extension_string;
718 if ((interface != NULL) && (scop != NULL)) {
719 openscop_interface_add(&scop->registry, interface);
721 textual = openscop_generic_lookup(scop->extension, interface->URI);
722 if (textual != NULL) {
723 extension_string = ((openscop_textual_p)textual->data)->textual;
724 new = openscop_generic_sread(extension_string, interface);
725 openscop_generic_add(&scop->extension, new);