Turn integrity check failure as a warning
[openscop.git] / source / scop.c
blob13dcdd8d18ea084232c333071486707cbde83a03
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_print_structure 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_print_structure(FILE * file, openscop_scop_p scop,
86 int level) {
87 int j;
89 // Go to the right level.
90 for (j = 0; j < level; j++)
91 fprintf(file, "|\t");
93 if (scop != NULL) {
94 fprintf(file, "+-- openscop_scop_t\n");
96 // A blank line.
97 for (j = 0; j <= level+1; j++)
98 fprintf(file, "|\t");
99 fprintf(file, "\n");
101 // Print the language.
102 for (j = 0; j < level; j++)
103 fprintf(file, "|\t");
104 fprintf(file, "|\tLanguage: %s\n", scop->language);
106 // A blank line.
107 for (j = 0; j <= level+1; j++)
108 fprintf(file, "|\t");
109 fprintf(file, "\n");
111 // Print the context of the scop.
112 openscop_relation_print_structure(file, scop->context, level+1);
114 // Print the names.
115 openscop_names_print_structure(file, scop->names, level+1);
117 // Print the statements.
118 openscop_statement_print_structure(file, scop->statement, level+1);
120 // Print the extensions.
121 openscop_extension_print_structure(file, scop->extension, level+1);
123 // A blank line.
124 for (j = 0; j <= level+1; j++)
125 fprintf(file, "|\t");
126 fprintf(file, "\n");
128 else {
129 fprintf(file, "+-- NULL scop\n");
132 // The last line.
133 for (j = 0; j <= level; j++)
134 fprintf(file, "|\t");
135 fprintf(file, "\n");
140 * openscop_scop_print function:
141 * this function prints the content of an openscop_scop_t structure (*scop)
142 * into a file (file, possibly stdout).
143 * \param file The file where the information has to be printed.
144 * \param scop The scop structure whose information has to be printed.
146 void openscop_scop_print(FILE * file, openscop_scop_p scop) {
147 openscop_scop_print_structure(file, scop, 0);
152 * openscop_scop_name_limits function:
153 * this function finds the (maximum) number of various elements of a scop and
154 * return the values through parameters. To ensure the correctness of the
155 * results, an integrity check of the input scop should be run before calling
156 * this function.
157 * \param scop The scop to analyse.
158 * \parem nb_parameters The number of parameters in the scop (output).
159 * \parem nb_iterators The number of iterators in the scop (output).
160 * \parem nb_scattdims The number of scattdims in the scop (output).
161 * \parem nb_localdims The number of local dimensions in the scop (output).
162 * \parem nb_arrays The number of arrays in the scop (output).
164 static
165 void openscop_scop_name_limits(openscop_scop_p scop,
166 int * nb_parameters,
167 int * nb_iterators,
168 int * nb_scattdims,
169 int * nb_localdims,
170 int * nb_arrays) {
171 int i, k, tmp, coef, id;
172 openscop_statement_p statement;
173 openscop_relation_list_p list;
175 // * The number of parameters is collected from the context,
176 // - in matrix format we compute it from the context using #columns,
177 // - in relation format it corresponds to the #parameters field.
178 // * The numbers of local dimensions are collected from all relations
179 // in the corresponding field, it is 0 for matrix format.
180 *nb_parameters = 0;
181 *nb_localdims = 0;
182 if (scop->context != NULL) {
183 if (openscop_relation_is_matrix(scop->context)) {
184 *nb_parameters = scop->context->nb_columns - 2;
186 else {
187 *nb_parameters = scop->context->nb_parameters;
188 *nb_localdims = scop->context->nb_local_dims;
192 *nb_iterators = 0;
193 *nb_scattdims = 0;
194 *nb_arrays = 0;
195 statement = scop->statement;
196 while (statement != NULL) {
197 // * The number of iterators are defined by iteration domains,
198 // - in matrix format we compute it using #columns and #parameters,
199 // - in relation format it corresponds to the #output_dims.
200 if (statement->domain != NULL) {
201 if (openscop_relation_is_matrix(statement->domain)) {
202 tmp = statement->domain->nb_columns - *nb_parameters - 2;
203 if (tmp > *nb_iterators)
204 *nb_iterators = tmp;
206 else {
207 if (statement->domain->nb_output_dims > *nb_iterators)
208 *nb_iterators = statement->domain->nb_output_dims;
210 if (statement->domain->nb_local_dims > *nb_localdims)
211 *nb_localdims = statement->domain->nb_local_dims;
215 // * The number of scattdims are defined by scattering,
216 // - in matrix format it corresponds to the number of rows,
217 // - in relation format it corresponds to the #input_dims.
218 if (statement->scattering != NULL) {
219 if (openscop_relation_is_matrix(statement->scattering)) {
220 if (statement->domain->nb_rows > *nb_scattdims)
221 *nb_scattdims = statement->scattering->nb_rows;
223 else {
224 if (statement->scattering->nb_input_dims > *nb_scattdims)
225 *nb_scattdims = statement->scattering->nb_input_dims;
227 if (statement->scattering->nb_local_dims > *nb_localdims)
228 *nb_localdims = statement->scattering->nb_local_dims;
232 // * The number of arrays are defined by accesses,
233 // - in matrix format, array identifiers are m[0][0],
234 // - in relation format, array identifiers are
235 // m[i][#columns -1] / m[i][1], with i the (supposedly only) row
236 // where m[i][1] is not 0.
237 for (k = 0; k < 2; k++) {
238 if (k == 0)
239 list = statement->read;
240 else
241 list = statement->write;
243 while (list != NULL) {
244 if (list->elt != NULL) {
245 if (openscop_relation_is_matrix(list->elt)) {
246 if (SCOPINT_get_si(list->elt->m[0][0]) > *nb_arrays)
247 *nb_arrays = SCOPINT_get_si(list->elt->m[0][0]);
249 else {
250 for (i = 0; i < list->elt->nb_rows; i++) {
251 coef = SCOPINT_get_si(list->elt->m[i][1]);
252 if (coef != 0) {
253 id = SCOPINT_get_si(list->elt->m[0][list->elt->nb_columns-1]);
254 if (abs(id / coef) > *nb_arrays)
255 *nb_arrays = abs(id / coef);
259 if (statement->scattering->nb_local_dims > *nb_localdims)
260 *nb_localdims = statement->scattering->nb_local_dims;
264 list = list->next;
268 statement = statement->next;
274 * openscop_scop_full_names function:
275 * this function generates an openscop_names_p structure which contains
276 * enough names for the scop provided as parameter, for each kind of names.
277 * If the names contained in the input scop are not sufficient, this function
278 * generated the missing names.
279 * \param scop The scop we need a name for each element.
280 * \return A set of names for the scop.
282 static
283 openscop_names_p openscop_scop_full_names(openscop_scop_p scop) {
284 int nb_parameters;
285 int nb_iterators;
286 int nb_scattdims;
287 int nb_localdims;
288 int nb_arrays;
289 openscop_arrays_p arrays;
290 openscop_names_p names;
292 names = openscop_names_copy(scop->names);
294 // Extract array names information from extensions.
295 openscop_util_strings_free(names->arrays, names->nb_arrays);
296 arrays = (openscop_arrays_p)openscop_extension_lookup(scop->extension,
297 OPENSCOP_EXTENSION_ARRAYS);
298 names->arrays = openscop_arrays_generate_names(arrays,
299 &(names->nb_arrays));
301 // Complete names if necessary.
302 openscop_scop_name_limits(scop, &nb_parameters,
303 &nb_iterators,
304 &nb_scattdims,
305 &nb_localdims,
306 &nb_arrays);
308 openscop_util_strings_complete(&names->parameters, &names->nb_parameters,
309 "P_", nb_parameters);
311 openscop_util_strings_complete(&names->iterators, &names->nb_iterators,
312 "i_", nb_iterators);
314 openscop_util_strings_complete(&names->scattdims, &names->nb_scattdims,
315 "s_", nb_scattdims);
317 openscop_util_strings_complete(&names->localdims, &names->nb_localdims,
318 "l_", nb_localdims);
320 openscop_util_strings_complete(&names->arrays, &names->nb_arrays,
321 "A_", nb_arrays);
323 return names;
328 * openscop_scop_print_openscop function:
329 * this function prints the content of an openscop_scop_t structure (*scop)
330 * into a file (file, possibly stdout) in the OpenScop textual format.
331 * \param file The file where the information has to be printed.
332 * \param scop The scop structure whose information has to be printed.
334 void openscop_scop_print_openscop(FILE * file, openscop_scop_p scop) {
335 openscop_names_p names;
336 int tmp_nb_iterators = 0;
337 char ** tmp_iterators = NULL;
339 if (openscop_scop_integrity_check(scop) == 0) {
340 fprintf(stderr, "[OpenScop] Warning: OpenScop integrity check failed. "
341 " Something may go wrong\n");
344 // Build a name structure for pretty printing of relations.
345 names = openscop_scop_full_names(scop);
347 if (0) {
348 fprintf(file, "# \n");
349 fprintf(file, "# <| \n");
350 fprintf(file, "# A \n");
351 fprintf(file, "# /.\\ \n");
352 fprintf(file, "# <| [\"\"M# \n");
353 fprintf(file, "# A | # Clan McCloog Castle \n");
354 fprintf(file, "# /.\\ [\"\"M# [Generated by the OpenScop ");
355 fprintf(file, "Library %s %s bits]\n",OPENSCOP_RELEASE, OPENSCOP_VERSION);
356 fprintf(file, "# [\"\"M# | # U\"U#U \n");
357 fprintf(file, "# | # | # \\ .:/ \n");
358 fprintf(file, "# | # | #___| # \n");
359 fprintf(file, "# | \"--' .-\" \n");
360 fprintf(file, "# |\"-\"-\"-\"-\"-#-#-## \n");
361 fprintf(file, "# | # ## ###### \n");
362 fprintf(file, "# \\ .::::'/ \n");
363 fprintf(file, "# \\ ::::'/ \n");
364 fprintf(file, "# :8a| # # ## \n");
365 fprintf(file, "# ::88a ### \n");
366 fprintf(file, "# ::::888a 8a ##::. \n");
367 fprintf(file, "# ::::::888a88a[]:::: \n");
368 fprintf(file, "# :::::::::SUNDOGa8a::::. .. \n");
369 fprintf(file, "# :::::8::::888:Y8888:::::::::... \n");
370 fprintf(file, "#::':::88::::888::Y88a______________________________");
371 fprintf(file, "________________________\n");
372 fprintf(file, "#:: ::::88a::::88a:Y88a ");
373 fprintf(file, " __---__-- __\n");
374 fprintf(file, "#' .: ::Y88a:::::8a:Y88a ");
375 fprintf(file, "__----_-- -------_-__\n");
376 fprintf(file, "# :' ::::8P::::::::::88aa. _ _- -");
377 fprintf(file, "- --_ --- __ --- __--\n");
378 fprintf(file, "#.:: :::::::::::::::::::Y88as88a...s88aa.\n");
380 else {
381 fprintf(file, "# [File generated by the OpenScop Library %s %s bits]\n",
382 OPENSCOP_RELEASE,OPENSCOP_VERSION);
385 fprintf(file, "\nOpenScop\n\n");
386 fprintf(file, "# =============================================== Global\n");
387 fprintf(file, "# Language\n");
388 fprintf(file, "%s\n\n", scop->language);
390 fprintf(file, "# Context\n");
391 // Remove the iterators from the names structure to print comments, as
392 // this information is used to know the number of iterators.
393 // TODO: do this in openscop_relation_print_openscop
394 tmp_nb_iterators = names->nb_iterators;
395 tmp_iterators = names->iterators;
396 names->nb_iterators = 0;
397 names->iterators = NULL;
398 openscop_relation_print_openscop(file, scop->context, names);
399 names->nb_iterators = tmp_nb_iterators;
400 names->iterators = tmp_iterators;
401 fprintf(file, "\n");
403 openscop_names_print_openscop(file, scop->names);
405 fprintf(file, "# Number of statements\n");
406 fprintf(file, "%d\n\n",openscop_statement_number(scop->statement));
408 openscop_statement_print_openscop(file, scop->statement, names);
410 if (scop->extension) {
411 fprintf(file, "# ==============================================="
412 " Extensions\n");
413 openscop_extension_print_openscop(file, scop->extension);
416 openscop_names_free(names);
420 /*****************************************************************************
421 * Reading function *
422 *****************************************************************************/
425 static
426 void openscop_scop_update_val(int * variable, int value) {
427 if ((*variable == OPENSCOP_UNDEFINED) || (*variable == value))
428 *variable = value;
429 else
430 fprintf(stderr, "[OpenScop] Warning: number of iterators and "
431 "parameters inconsistency.\n");
434 static
435 void openscop_scop_update_properties(openscop_relation_p relation,
436 int nb_output_dims, int nb_input_dims,
437 int nb_parameters) {
438 if (relation != NULL) {
439 openscop_scop_update_val(&(relation->nb_output_dims), nb_output_dims);
440 openscop_scop_update_val(&(relation->nb_input_dims), nb_input_dims);
441 openscop_scop_update_val(&(relation->nb_parameters), nb_parameters);
447 * openscop_scop_propagate_properties internal function:
448 * This function tries to propagate information in all relations through the
449 * whole openscop representation. For instance, the number of parameters can
450 * be found in the context as well as in any relation: if it is undefined in
451 * the relation, this function defines it, if it is different than the
452 * expected value, it reports an error. This function does the same for
453 * the number of output and input dimensions.
454 * \param scop The SCoP we want to propagate properties.
456 static
457 void openscop_scop_propagate_properties(openscop_scop_p scop) {
458 int i, nb_parameters;
459 openscop_statement_p statement;
460 openscop_relation_p relation;
461 openscop_relation_list_p list;
463 // Context part: get the number of parameters.
464 if ((scop->context != NULL) &&
465 (openscop_relation_is_matrix(scop->context))) {
466 nb_parameters = scop->context->nb_columns - 2;
467 openscop_scop_update_properties(scop->context, 0, 0, nb_parameters);
469 else {
470 return;
473 // For each statement:
474 statement = scop->statement;
475 while (statement != NULL) {
476 // - Domain part,
477 relation = statement->domain;
478 if (openscop_relation_is_matrix(relation)) {
479 while (relation != NULL) {
480 openscop_scop_update_properties(
481 relation, relation->nb_columns-nb_parameters-2, 0, nb_parameters);
482 relation = relation->next;
486 // - Scattering part,
487 relation = statement->scattering;
488 if (openscop_relation_is_matrix(relation)) {
489 while (relation != NULL) {
490 openscop_scop_update_properties(
491 relation, 0, relation->nb_columns-nb_parameters-2, nb_parameters);
492 relation = relation->next;
496 // - Access part.
497 for (i = 0; i < 2; i++) {
498 if (i == 0)
499 list = statement->read;
500 else
501 list = statement->write;
503 while (list != NULL) {
504 relation = list->elt;
505 if (openscop_relation_is_matrix(relation)) {
506 while (relation != NULL) {
507 openscop_scop_update_properties(
508 relation, 0, relation->nb_columns - nb_parameters - 2,
509 nb_parameters);
510 relation = relation->next;
514 list = list->next;
518 statement = statement->next;
524 * openscop_scop_read function:
525 * this function reads a scop structure from a file (possibly stdin)
526 * complying to the OpenScop textual format and returns a pointer to this
527 * scop structure. If some relation properties (number of input/output/local
528 * dimensions and number of parameters) are undefined, it will define them
529 * according to the available information.
530 * \param file The file where the scop has to be read.
531 * \return A pointer to the scop structure that has been read.
533 openscop_scop_p openscop_scop_read(FILE * file) {
534 openscop_scop_p scop = NULL;
535 openscop_statement_p stmt = NULL;
536 openscop_statement_p prev = NULL;
537 int nb_statements;
538 int nb_parameters;
539 int max;
540 char ** tmp;
541 int i;
543 if (file == NULL)
544 return NULL;
546 scop = openscop_scop_malloc();
549 // I. CONTEXT PART
552 // Ensure the file is a .scop.
553 tmp = openscop_util_strings_read(file, &max);
554 if ((max == 0) || (strcmp(*tmp, "OpenScop"))) {
555 fprintf(stderr, "[OpenScop] Error: not an OpenScop file "
556 "(type \"%s\".\n", *tmp);
557 exit (1);
559 if (max > 1)
560 fprintf(stderr, "[OpenScop] Warning: uninterpreted information "
561 "(after file type).\n");
562 free(*tmp);
563 free(tmp);
565 // Read the language.
566 char ** language = openscop_util_strings_read(file, &max);
567 if (max == 0) {
568 fprintf(stderr, "[OpenScop] Error: no language (backend) specified.\n");
569 exit (1);
571 if (max > 1)
572 fprintf(stderr, "[OpenScop] Warning: uninterpreted information "
573 "(after language).\n");
574 scop->language = *language;
575 free(language);
577 // Read the context.
578 scop->context = openscop_relation_read(file);
579 openscop_relation_set_type(scop->context, OPENSCOP_TYPE_DOMAIN);
580 scop->names = openscop_names_read(file);
581 if (scop->context != NULL) {
582 if (openscop_relation_is_matrix(scop->context))
583 nb_parameters = scop->context->nb_columns - 2;
584 else
585 nb_parameters = scop->context->nb_parameters;
587 else {
588 nb_parameters = OPENSCOP_UNDEFINED;
592 // II. STATEMENT PART
595 // Read the number of statements.
596 nb_statements = openscop_util_read_int(file, NULL);
598 for (i = 0; i < nb_statements; ++i) {
599 // Read each statement.
600 stmt = openscop_statement_read(file, nb_parameters);
601 if (scop->statement == NULL)
602 scop->statement = stmt;
603 else
604 prev->next = stmt;
605 prev = stmt;
609 // III. OPTION PART
612 // Read the remainder of the file, and store it in the extension field.
613 scop->extension = openscop_extension_read(file);
616 // VI. FINALIZE AND CHECK
618 openscop_scop_propagate_properties(scop);
620 if (!openscop_scop_integrity_check(scop))
621 fprintf(stderr, "[OpenScop] Warning: global integrity check failed.\n");
623 return scop;
627 /*+***************************************************************************
628 * Memory allocation/deallocation functions *
629 *****************************************************************************/
633 * openscop_scop_malloc function:
634 * this function allocates the memory space for a openscop_scop_t structure and
635 * sets its fields with default values. Then it returns a pointer to the
636 * allocated space.
637 * \return A pointer to an empty scop with fields set to default values.
639 openscop_scop_p openscop_scop_malloc() {
640 openscop_scop_p scop;
642 scop = (openscop_scop_p)malloc(sizeof(openscop_scop_t));
643 if (scop == NULL) {
644 fprintf(stderr, "[OpenScop] Memory Overflow.\n");
645 exit(1);
648 scop->version = 1;
649 scop->language = NULL;
650 scop->context = NULL;
651 scop->names = NULL;
652 scop->statement = NULL;
653 scop->extension = NULL;
654 scop->usr = NULL;
656 return scop;
661 * openscop_scop_free function:
662 * This function frees the allocated memory for a openscop_scop_t structure.
663 * \param scop The pointer to the scop we want to free.
665 void openscop_scop_free(openscop_scop_p scop) {
666 if (scop != NULL) {
667 if (scop->language != NULL)
668 free(scop->language);
670 openscop_relation_free(scop->context);
671 openscop_names_free(scop->names);
672 openscop_statement_free(scop->statement);
673 openscop_extension_free(scop->extension);
675 free(scop);
680 /*+***************************************************************************
681 * Processing functions *
682 *****************************************************************************/
686 * openscop_scop_copy function:
687 * This functions builds and returns a "hard copy" (not a pointer copy)
688 * of a openscop_statement_t data structure provided as parameter.
689 * Note that the usr field is not touched by this function.
690 * \param statement The pointer to the scop we want to copy.
691 * \return A pointer to the full copy of the scop provided as parameter.
693 openscop_scop_p openscop_scop_copy(openscop_scop_p scop) {
694 openscop_scop_p copy;
696 copy = openscop_scop_malloc();
697 copy->version = scop->version;
698 if (scop->language != NULL)
699 copy->language = strdup(scop->language);
700 copy->context = openscop_relation_copy(scop->context);
701 copy->names = openscop_names_copy(scop->names);
702 copy->statement = openscop_statement_copy(scop->statement);
703 copy->extension = openscop_extension_copy(scop->extension);
705 return copy;
710 * openscop_scop_equal function:
711 * this function returns true if the two scops are the same, false
712 * otherwise (the usr field is not tested).
713 * \param s1 The first scop.
714 * \param s2 The second scop.
715 * \return 1 if s1 and s2 are the same (content-wise), 0 otherwise.
717 int openscop_scop_equal(openscop_scop_p s1, openscop_scop_p s2) {
719 if (s1 == s2)
720 return 1;
722 if (s1->version != s2->version) {
723 fprintf(stderr, "[OpenScop] info: versions are not the same.\n");
724 return 0;
727 if (strcmp(s1->language, s2->language) != 0) {
728 fprintf(stderr, "[OpenScop] info: languages are not the same.\n");
729 return 0;
732 if (!openscop_relation_equal(s1->context, s2->context)) {
733 fprintf(stderr, "[OpenScop] info: contexts are not the same.\n");
734 return 0;
737 if (!openscop_names_equal(s1->names, s2->names)) {
738 fprintf(stderr, "[OpenScop] info: names are not the same.\n");
739 return 0;
742 if (!openscop_statement_equal(s1->statement, s2->statement)) {
743 fprintf(stderr, "[OpenScop] info: statements are not the same.\n");
744 return 0;
747 if (!openscop_extension_equal(s1->extension, s2->extension)) {
748 fprintf(stderr, "[OpenScop] info: extensions are not the same.\n");
749 return 0;
752 return 1;
757 * openscop_scop_integrity_check function:
758 * This function checks that a scop is "well formed". It returns 0 if the
759 * check failed or 1 if no problem has been detected.
760 * \param scop The scop we want to check.
761 * \return 0 if the integrity check fails, 1 otherwise.
763 int openscop_scop_integrity_check(openscop_scop_p scop) {
764 int expected_nb_parameters;
765 int max_nb_parameters;
766 int max_nb_iterators;
767 int max_nb_scattdims;
768 int max_nb_localdims;
769 int max_nb_arrays;
771 openscop_scop_print(stdout, scop);
773 // Check the language.
774 if ((scop->language != NULL) &&
775 (!strcmp(scop->language, "caml") || !strcmp(scop->language, "Caml") ||
776 !strcmp(scop->language, "ocaml") || !strcmp(scop->language, "OCaml")))
777 fprintf(stderr, "[OpenScop] Alert: What ?! Caml ?! Are you sure ?!\n");
779 // Check the context.
780 if (openscop_relation_is_matrix(scop->context))
782 if (!openscop_relation_integrity_check(scop->context,
783 OPENSCOP_TYPE_CONTEXT,
784 OPENSCOP_UNDEFINED,
785 OPENSCOP_UNDEFINED,
786 OPENSCOP_UNDEFINED))
787 return 0;
789 // Get the number of parameters.
790 if (scop->context != NULL) {
791 if (openscop_relation_is_matrix(scop->context))
792 expected_nb_parameters = scop->context->nb_columns - 2;
793 else
794 expected_nb_parameters = scop->context->nb_parameters;
796 else
797 expected_nb_parameters = OPENSCOP_UNDEFINED;
799 if (!openscop_statement_integrity_check(scop->statement,
800 expected_nb_parameters))
801 return 0;
803 // Ensure we have enough names.
804 openscop_scop_name_limits(scop, &max_nb_parameters,
805 &max_nb_iterators,
806 &max_nb_scattdims,
807 &max_nb_localdims,
808 &max_nb_arrays);
810 return openscop_names_integrity_check(scop->names, expected_nb_parameters,
811 max_nb_iterators, max_nb_scattdims);