Solve a memory leak for cloned scops
[openscop.git] / source / scop.c
blob91ea7635ad96ea63aa902a65615a76950f18f123
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();
403 openscop_scop_register_default_extensions(scop);
406 // II. CONTEXT PART
409 // Read the language.
410 language = openscop_strings_read(file);
411 if (openscop_strings_size(language) == 0)
412 OPENSCOP_error("no language (backend) specified");
414 if (openscop_strings_size(language) > 1)
415 OPENSCOP_warning("uninterpreted information (after language)");
417 if (language != NULL) {
418 scop->language = strdup(language->string[0]);
419 openscop_strings_free(language);
422 // Read the context domain.
423 scop->context = openscop_relation_read(file);
425 // Read the parameters.
426 scop->parameter_type = OPENSCOP_TYPE_STRING;
427 if (openscop_util_read_int(file, NULL) > 0) {
428 interface = openscop_strings_interface();
429 start = openscop_util_skip_blank_and_comments(file, buffer);
430 scop->parameters = openscop_generic_sread(start, interface);
431 openscop_interface_free(interface);
435 // III. STATEMENT PART
438 // Read the number of statements.
439 nb_statements = openscop_util_read_int(file, NULL);
441 for (i = 0; i < nb_statements; i++) {
442 // Read each statement.
443 stmt = openscop_statement_read(file);
444 if (scop->statement == NULL)
445 scop->statement = stmt;
446 else
447 prev->next = stmt;
448 prev = stmt;
452 // IV. EXTENSION PART (TO THE END TAG)
455 // Read up the end tag (if any), and store extensions.
456 scop->extension = openscop_generic_read(file, scop->registry);
458 // Add the new scop to the list.
459 if (first) {
460 list = scop;
461 first = 0;
463 else {
464 current->next = scop;
466 current = scop;
469 if (!openscop_scop_integrity_check(list))
470 OPENSCOP_warning("scop integrity check failed");
472 return list;
476 /*+***************************************************************************
477 * Memory allocation/deallocation functions *
478 *****************************************************************************/
482 * openscop_scop_malloc function:
483 * this function allocates the memory space for a openscop_scop_t structure and
484 * sets its fields with default values. Then it returns a pointer to the
485 * allocated space.
486 * \return A pointer to an empty scop with fields set to default values.
488 openscop_scop_p openscop_scop_malloc() {
489 openscop_scop_p scop;
491 OPENSCOP_malloc(scop, openscop_scop_p, sizeof(openscop_scop_t));
492 scop->version = 1;
493 scop->language = NULL;
494 scop->context = NULL;
495 scop->parameter_type = OPENSCOP_UNDEFINED;
496 scop->parameters = NULL;
497 scop->statement = NULL;
498 scop->registry = NULL;
499 scop->extension = NULL;
500 scop->usr = NULL;
501 scop->next = NULL;
503 return scop;
508 * openscop_scop_free function:
509 * This function frees the allocated memory for a openscop_scop_t structure.
510 * \param scop The pointer to the scop we want to free.
512 void openscop_scop_free(openscop_scop_p scop) {
513 openscop_scop_p tmp;
515 while (scop != NULL) {
516 if (scop->language != NULL)
517 free(scop->language);
518 openscop_generic_free(scop->parameters);
519 openscop_relation_free(scop->context);
520 openscop_statement_free(scop->statement);
521 openscop_interface_free(scop->registry);
522 openscop_generic_free(scop->extension);
524 tmp = scop->next;
525 free(scop);
526 scop = tmp;
531 /*+***************************************************************************
532 * Processing functions *
533 *****************************************************************************/
537 * openscop_scop_register_default_extensions function:
538 * this function registers the default OpenScop Library extensions to an
539 * existing scop.
540 * \param scop The scop for which default options have to be registered.
542 void openscop_scop_register_default_extensions(openscop_scop_p scop) {
544 openscop_interface_add(&(scop->registry), openscop_textual_interface());
545 openscop_interface_add(&(scop->registry), openscop_comment_interface());
546 openscop_interface_add(&(scop->registry), openscop_arrays_interface());
547 openscop_interface_add(&(scop->registry), openscop_lines_interface());
548 openscop_interface_add(&(scop->registry), openscop_irregular_interface());
553 * openscop_scop_clone function:
554 * This functions builds and returns a "hard copy" (not a pointer copy)
555 * of a openscop_statement_t data structure provided as parameter.
556 * Note that the usr field is not touched by this function.
557 * \param statement The pointer to the scop we want to clone.
558 * \return A pointer to the full clone of the scop provided as parameter.
560 openscop_scop_p openscop_scop_clone(openscop_scop_p scop) {
561 openscop_scop_p clone = NULL, node, previous = NULL;
562 int first = 1;
564 while (scop != NULL) {
565 node = openscop_scop_malloc();
566 node->version = scop->version;
567 if (scop->language != NULL)
568 node->language = strdup(scop->language);
569 node->context = openscop_relation_clone(scop->context);
570 node->parameter_type = scop->parameter_type;
571 node->parameters = openscop_generic_clone(scop->parameters);
572 node->statement = openscop_statement_clone(scop->statement);
573 node->registry = openscop_interface_clone(scop->registry);
574 node->extension = openscop_generic_clone(scop->extension);
576 if (first) {
577 first = 0;
578 clone = node;
579 previous = node;
581 else {
582 previous->next = node;
583 previous = previous->next;
586 scop = scop->next;
589 return clone;
594 * openscop_scop_equal function:
595 * this function returns true if the two scops are the same, false
596 * otherwise (the usr field is not tested).
597 * \param s1 The first scop.
598 * \param s2 The second scop.
599 * \return 1 if s1 and s2 are the same (content-wise), 0 otherwise.
601 int openscop_scop_equal(openscop_scop_p s1, openscop_scop_p s2) {
603 while ((s1 != NULL) && (s2 != NULL)) {
604 if (s1 == s2)
605 return 1;
607 if (s1->version != s2->version) {
608 OPENSCOP_info("versions are not the same");
609 return 0;
612 if (strcmp(s1->language, s2->language) != 0) {
613 OPENSCOP_info("languages are not the same");
614 return 0;
617 if (!openscop_relation_equal(s1->context, s2->context)) {
618 OPENSCOP_info("contexts are not the same");
619 return 0;
622 if (s1->parameter_type != s2->parameter_type) {
623 OPENSCOP_info("parameter types are not the same");
624 return 0;
627 if (!openscop_generic_equal(s1->parameters, s2->parameters)) {
628 OPENSCOP_info("parameters are not the same");
629 return 0;
632 if (!openscop_statement_equal(s1->statement, s2->statement)) {
633 OPENSCOP_info("statements are not the same");
634 return 0;
637 if (!openscop_interface_equal(s1->registry, s2->registry)) {
638 OPENSCOP_info("registries are not the same");
639 return 0;
642 if (!openscop_generic_equal(s1->extension, s2->extension)) {
643 OPENSCOP_info("extensions are not the same");
644 return 0;
647 s1 = s1->next;
648 s2 = s2->next;
651 if (((s1 == NULL) && (s2 != NULL)) || ((s1 != NULL) && (s2 == NULL)))
652 return 0;
654 return 1;
659 * openscop_scop_integrity_check function:
660 * This function checks that a scop is "well formed". It returns 0 if the
661 * check failed or 1 if no problem has been detected.
662 * \param scop The scop we want to check.
663 * \return 0 if the integrity check fails, 1 otherwise.
665 int openscop_scop_integrity_check(openscop_scop_p scop) {
666 int expected_nb_parameters;
669 while (scop != NULL) {
670 // Check the language.
671 if ((scop->language != NULL) &&
672 (!strcmp(scop->language, "caml") || !strcmp(scop->language, "Caml") ||
673 !strcmp(scop->language, "ocaml") || !strcmp(scop->language, "OCaml")))
674 fprintf(stderr, "[OpenScop] Alert: What ?! Caml ?! Are you sure ?!?!\n");
676 // Check the context.
677 if (!openscop_relation_integrity_check(scop->context,
678 OPENSCOP_TYPE_CONTEXT,
679 OPENSCOP_UNDEFINED,
680 OPENSCOP_UNDEFINED,
681 OPENSCOP_UNDEFINED))
682 return 0;
684 // Get the number of parameters.
685 if (scop->context != NULL)
686 expected_nb_parameters = scop->context->nb_parameters;
687 else
688 expected_nb_parameters = OPENSCOP_UNDEFINED;
690 // TODO : check the number of parameter strings.
692 if (!openscop_statement_integrity_check(scop->statement,
693 expected_nb_parameters))
694 return 0;
696 scop = scop->next;
699 return 1;
704 * openscop_scop_get_nb_parameters function:
705 * this function returns the number of global parameters of a given SCoP.
706 * \param scop The scop we want to know the number of global parameters.
707 * \return The number of global parameters in the scop.
709 int openscop_scop_get_nb_parameters(openscop_scop_p scop) {
711 if (scop->context == NULL) {
712 OPENSCOP_warning("no context domain, assuming 0 parameters");
713 return 0;
715 else {
716 return scop->context->nb_parameters;
722 * openscop_scop_register_extension function:
723 * this function registers a list of extension interfaces to a scop, i.e., it
724 * adds them to the scop registry. In addition, it will extract extensions
725 * corresponding to those interfaces from the textual form of the extensions
726 * (if any) and add them to the scop extension list.
727 * \param scop The scop for which an extension has to be registered.
728 * \param interface The extension interface to register within the scop.
730 void openscop_scop_register_extension(openscop_scop_p scop,
731 openscop_interface_p interface) {
732 openscop_generic_p textual, new;
733 char * extension_string;
735 if ((interface != NULL) && (scop != NULL)) {
736 openscop_interface_add(&scop->registry, interface);
738 textual = openscop_generic_lookup(scop->extension, interface->URI);
739 if (textual != NULL) {
740 extension_string = ((openscop_textual_p)textual->data)->textual;
741 new = openscop_generic_sread(extension_string, interface);
742 openscop_generic_add(&scop->extension, new);