scop.c: filter_implied: drop unused variable
[pet.git] / parse.c
blob60945ed0c3ef4e1fe4ace2e91360e6f4ef292103
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2013-2014 Ecole Normale Superieure. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
32 * Leiden University.
33 */
35 #include <stdlib.h>
36 #include <yaml.h>
38 #include <isl/ctx.h>
39 #include <isl/id.h>
40 #include <isl/val.h>
41 #include <isl/aff.h>
42 #include <isl/set.h>
43 #include <isl/map.h>
44 #include <isl/union_set.h>
45 #include <isl/union_map.h>
47 #include "expr.h"
48 #include "loc.h"
49 #include "scop.h"
50 #include "scop_yaml.h"
51 #include "tree.h"
53 static char *extract_string(isl_ctx *ctx, yaml_document_t *document,
54 yaml_node_t *node)
56 if (node->type != YAML_SCALAR_NODE)
57 isl_die(ctx, isl_error_invalid, "expecting scalar node",
58 return NULL);
60 return strdup((char *) node->data.scalar.value);
63 static int extract_int(isl_ctx *ctx, yaml_document_t *document,
64 yaml_node_t *node)
66 if (node->type != YAML_SCALAR_NODE)
67 isl_die(ctx, isl_error_invalid, "expecting scalar node",
68 return -1);
70 return atoi((char *) node->data.scalar.value);
73 static double extract_double(isl_ctx *ctx, yaml_document_t *document,
74 yaml_node_t *node)
76 if (node->type != YAML_SCALAR_NODE)
77 isl_die(ctx, isl_error_invalid, "expecting scalar node",
78 return -1);
80 return strtod((char *) node->data.scalar.value, NULL);
83 static enum pet_expr_type extract_expr_type(isl_ctx *ctx,
84 yaml_document_t *document, yaml_node_t *node)
86 if (node->type != YAML_SCALAR_NODE)
87 isl_die(ctx, isl_error_invalid, "expecting scalar node",
88 return -1);
90 return pet_str_type((char *) node->data.scalar.value);
93 static enum pet_op_type extract_op(isl_ctx *ctx, yaml_document_t *document,
94 yaml_node_t *node)
96 if (node->type != YAML_SCALAR_NODE)
97 isl_die(ctx, isl_error_invalid, "expecting scalar node",
98 return -1);
100 return pet_str_op((char *) node->data.scalar.value);
103 static __isl_give isl_set *extract_set(isl_ctx *ctx, yaml_document_t *document,
104 yaml_node_t *node)
106 if (node->type != YAML_SCALAR_NODE)
107 isl_die(ctx, isl_error_invalid, "expecting scalar node",
108 return NULL);
110 return isl_set_read_from_str(ctx, (char *) node->data.scalar.value);
113 static __isl_give isl_id *extract_id(isl_ctx *ctx, yaml_document_t *document,
114 yaml_node_t *node)
116 if (node->type != YAML_SCALAR_NODE)
117 isl_die(ctx, isl_error_invalid, "expecting scalar node",
118 return NULL);
120 return isl_id_alloc(ctx, (char *) node->data.scalar.value, NULL);
123 static __isl_give isl_map *extract_map(isl_ctx *ctx, yaml_document_t *document,
124 yaml_node_t *node)
126 if (node->type != YAML_SCALAR_NODE)
127 isl_die(ctx, isl_error_invalid, "expecting scalar node",
128 return NULL);
130 return isl_map_read_from_str(ctx, (char *) node->data.scalar.value);
133 /* Extract an isl_union_set from "node".
135 static __isl_give isl_union_set *extract_union_set(isl_ctx *ctx,
136 yaml_document_t *document, yaml_node_t *node)
138 if (node->type != YAML_SCALAR_NODE)
139 isl_die(ctx, isl_error_invalid, "expecting scalar node",
140 return NULL);
142 return isl_union_set_read_from_str(ctx,
143 (char *) node->data.scalar.value);
146 /* Extract an isl_union_map from "node".
148 static __isl_give isl_union_map *extract_union_map(isl_ctx *ctx,
149 yaml_document_t *document, yaml_node_t *node)
151 if (node->type != YAML_SCALAR_NODE)
152 isl_die(ctx, isl_error_invalid, "expecting scalar node",
153 return NULL);
155 return isl_union_map_read_from_str(ctx,
156 (char *) node->data.scalar.value);
159 /* Extract an isl_val from "node".
161 static __isl_give isl_val *extract_val(isl_ctx *ctx, yaml_document_t *document,
162 yaml_node_t *node)
164 if (node->type != YAML_SCALAR_NODE)
165 isl_die(ctx, isl_error_invalid, "expecting scalar node",
166 return NULL);
168 return isl_val_read_from_str(ctx, (char *) node->data.scalar.value);
171 /* Extract an isl_multi_pw_aff from "node".
173 static __isl_give isl_multi_pw_aff *extract_multi_pw_aff(isl_ctx *ctx,
174 yaml_document_t *document, yaml_node_t *node)
176 if (node->type != YAML_SCALAR_NODE)
177 isl_die(ctx, isl_error_invalid, "expecting scalar node",
178 return NULL);
180 return isl_multi_pw_aff_read_from_str(ctx,
181 (char *) node->data.scalar.value);
184 /* Extract an isl_schedule from "node".
186 static __isl_give isl_schedule *extract_schedule(isl_ctx *ctx,
187 yaml_document_t *document, yaml_node_t *node)
189 if (node->type != YAML_SCALAR_NODE)
190 isl_die(ctx, isl_error_invalid, "expecting scalar node",
191 return NULL);
193 return isl_schedule_read_from_str(ctx,
194 (char *) node->data.scalar.value);
197 /* Extract a pet_type from "node".
199 static struct pet_type *extract_type(isl_ctx *ctx,
200 yaml_document_t *document, yaml_node_t *node)
202 struct pet_type *type;
203 yaml_node_pair_t * pair;
205 if (node->type != YAML_MAPPING_NODE)
206 isl_die(ctx, isl_error_invalid, "expecting mapping",
207 return NULL);
209 type = isl_calloc_type(ctx, struct pet_type);
210 if (!type)
211 return NULL;
213 for (pair = node->data.mapping.pairs.start;
214 pair < node->data.mapping.pairs.top; ++pair) {
215 yaml_node_t *key, *value;
217 key = yaml_document_get_node(document, pair->key);
218 value = yaml_document_get_node(document, pair->value);
220 if (key->type != YAML_SCALAR_NODE)
221 isl_die(ctx, isl_error_invalid, "expecting scalar key",
222 return pet_type_free(type));
224 if (!strcmp((char *) key->data.scalar.value, "name"))
225 type->name = extract_string(ctx, document, value);
226 if (!strcmp((char *) key->data.scalar.value, "definition"))
227 type->definition = extract_string(ctx, document, value);
230 return type;
233 /* Extract a sequence of types from "node" and store them in scop->types.
235 static struct pet_scop *extract_types(isl_ctx *ctx,
236 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
238 int i;
239 yaml_node_item_t *item;
241 if (node->type != YAML_SEQUENCE_NODE)
242 isl_die(ctx, isl_error_invalid, "expecting sequence",
243 return NULL);
245 scop->n_type = node->data.sequence.items.top
246 - node->data.sequence.items.start;
247 scop->types = isl_calloc_array(ctx, struct pet_type *, scop->n_type);
248 if (!scop->types)
249 return pet_scop_free(scop);
251 for (item = node->data.sequence.items.start, i = 0;
252 item < node->data.sequence.items.top; ++item, ++i) {
253 yaml_node_t *n;
255 n = yaml_document_get_node(document, *item);
256 scop->types[i] = extract_type(ctx, document, n);
257 if (!scop->types[i])
258 return pet_scop_free(scop);
261 return scop;
264 static struct pet_array *extract_array(isl_ctx *ctx, yaml_document_t *document,
265 yaml_node_t *node)
267 struct pet_array *array;
268 yaml_node_pair_t * pair;
270 if (node->type != YAML_MAPPING_NODE)
271 isl_die(ctx, isl_error_invalid, "expecting mapping",
272 return NULL);
274 array = isl_calloc_type(ctx, struct pet_array);
275 if (!array)
276 return NULL;
278 for (pair = node->data.mapping.pairs.start;
279 pair < node->data.mapping.pairs.top; ++pair) {
280 yaml_node_t *key, *value;
282 key = yaml_document_get_node(document, pair->key);
283 value = yaml_document_get_node(document, pair->value);
285 if (key->type != YAML_SCALAR_NODE)
286 isl_die(ctx, isl_error_invalid, "expecting scalar key",
287 return pet_array_free(array));
289 if (!strcmp((char *) key->data.scalar.value, "context"))
290 array->context = extract_set(ctx, document, value);
291 if (!strcmp((char *) key->data.scalar.value, "extent"))
292 array->extent = extract_set(ctx, document, value);
293 if (!strcmp((char *) key->data.scalar.value, "value_bounds"))
294 array->value_bounds = extract_set(ctx, document, value);
295 if (!strcmp((char *) key->data.scalar.value, "element_type"))
296 array->element_type =
297 extract_string(ctx, document, value);
298 if (!strcmp((char *) key->data.scalar.value, "element_size"))
299 array->element_size = extract_int(ctx, document, value);
300 if (!strcmp((char *) key->data.scalar.value,
301 "element_is_record"))
302 array->element_is_record =
303 extract_int(ctx, document, value);
304 if (!strcmp((char *) key->data.scalar.value, "live_out"))
305 array->live_out = extract_int(ctx, document, value);
306 if (!strcmp((char *) key->data.scalar.value,
307 "uniquely_defined"))
308 array->uniquely_defined =
309 extract_int(ctx, document, value);
310 if (!strcmp((char *) key->data.scalar.value, "declared"))
311 array->declared = extract_int(ctx, document, value);
312 if (!strcmp((char *) key->data.scalar.value, "exposed"))
313 array->exposed = extract_int(ctx, document, value);
316 return array;
319 static struct pet_scop *extract_arrays(isl_ctx *ctx, yaml_document_t *document,
320 yaml_node_t *node, struct pet_scop *scop)
322 int i;
323 yaml_node_item_t *item;
325 if (node->type != YAML_SEQUENCE_NODE)
326 isl_die(ctx, isl_error_invalid, "expecting sequence",
327 return NULL);
329 scop->n_array = node->data.sequence.items.top
330 - node->data.sequence.items.start;
331 scop->arrays = isl_calloc_array(ctx, struct pet_array *, scop->n_array);
332 if (!scop->arrays)
333 return pet_scop_free(scop);
335 for (item = node->data.sequence.items.start, i = 0;
336 item < node->data.sequence.items.top; ++item, ++i) {
337 yaml_node_t *n;
339 n = yaml_document_get_node(document, *item);
340 scop->arrays[i] = extract_array(ctx, document, n);
341 if (!scop->arrays[i])
342 return pet_scop_free(scop);
345 return scop;
348 static __isl_give pet_expr *extract_expr(isl_ctx *ctx,
349 yaml_document_t *document, yaml_node_t *node);
351 static __isl_give pet_expr *extract_arguments(isl_ctx *ctx,
352 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
354 int i, n;
355 yaml_node_item_t *item;
357 if (node->type != YAML_SEQUENCE_NODE)
358 isl_die(ctx, isl_error_invalid, "expecting sequence",
359 return pet_expr_free(expr));
361 n = node->data.sequence.items.top - node->data.sequence.items.start;
362 expr = pet_expr_set_n_arg(expr, n);
364 for (item = node->data.sequence.items.start, i = 0;
365 item < node->data.sequence.items.top; ++item, ++i) {
366 yaml_node_t *n;
367 pet_expr *arg;
369 n = yaml_document_get_node(document, *item);
370 arg = extract_expr(ctx, document, n);
371 expr = pet_expr_set_arg(expr, i, arg);
374 return expr;
377 /* Extract pet_expr_double specific fields from "node" and
378 * update "expr" accordingly.
380 static __isl_give pet_expr *extract_expr_double(isl_ctx *ctx,
381 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
383 yaml_node_pair_t *pair;
384 double d = 0;
385 char *s = NULL;
387 for (pair = node->data.mapping.pairs.start;
388 pair < node->data.mapping.pairs.top; ++pair) {
389 yaml_node_t *key, *value;
391 key = yaml_document_get_node(document, pair->key);
392 value = yaml_document_get_node(document, pair->value);
394 if (key->type != YAML_SCALAR_NODE)
395 isl_die(ctx, isl_error_invalid, "expecting scalar key",
396 return pet_expr_free(expr));
398 if (!strcmp((char *) key->data.scalar.value, "value"))
399 d = extract_double(ctx, document, value);
400 if (!strcmp((char *) key->data.scalar.value, "string"))
401 s = extract_string(ctx, document, value);
404 expr = pet_expr_double_set(expr, d, s);
405 free(s);
407 return expr;
410 /* Extract pet_expr_access specific fields from "node" and
411 * update "expr" accordingly.
413 * The depth of the access is initialized by pet_expr_access_set_index.
414 * Any explicitly specified depth therefore needs to be set after
415 * setting the index expression. Similiarly, the access relations (if any)
416 * need to be set after setting the depth.
418 static __isl_give pet_expr *extract_expr_access(isl_ctx *ctx,
419 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
421 yaml_node_pair_t *pair;
422 int depth = -1;
423 isl_multi_pw_aff *index = NULL;
425 for (pair = node->data.mapping.pairs.start;
426 pair < node->data.mapping.pairs.top; ++pair) {
427 yaml_node_t *key, *value;
429 key = yaml_document_get_node(document, pair->key);
430 value = yaml_document_get_node(document, pair->value);
432 if (key->type != YAML_SCALAR_NODE)
433 isl_die(ctx, isl_error_invalid, "expecting scalar key",
434 return pet_expr_free(expr));
436 if (!strcmp((char *) key->data.scalar.value, "index"))
437 index = extract_multi_pw_aff(ctx, document, value);
438 if (!strcmp((char *) key->data.scalar.value, "depth"))
439 depth = extract_int(ctx, document, value);
442 expr = pet_expr_access_set_index(expr, index);
443 if (depth >= 0)
444 expr = pet_expr_access_set_depth(expr, depth);
446 for (pair = node->data.mapping.pairs.start;
447 pair < node->data.mapping.pairs.top; ++pair) {
448 yaml_node_t *key, *value;
450 key = yaml_document_get_node(document, pair->key);
451 value = yaml_document_get_node(document, pair->value);
453 if (key->type != YAML_SCALAR_NODE)
454 isl_die(ctx, isl_error_invalid, "expecting scalar key",
455 return pet_expr_free(expr));
457 if (!strcmp((char *) key->data.scalar.value, "may_read"))
458 expr = pet_expr_access_set_access(expr,
459 pet_expr_access_may_read,
460 extract_union_map(ctx, document, value));
461 if (!strcmp((char *) key->data.scalar.value, "may_write"))
462 expr = pet_expr_access_set_access(expr,
463 pet_expr_access_may_write,
464 extract_union_map(ctx, document, value));
465 if (!strcmp((char *) key->data.scalar.value, "must_write"))
466 expr = pet_expr_access_set_access(expr,
467 pet_expr_access_must_write,
468 extract_union_map(ctx, document, value));
469 if (!strcmp((char *) key->data.scalar.value, "killed"))
470 expr = pet_expr_access_set_access(expr,
471 pet_expr_access_killed,
472 extract_union_map(ctx, document, value));
473 if (!strcmp((char *) key->data.scalar.value, "reference"))
474 expr = pet_expr_access_set_ref_id(expr,
475 extract_id(ctx, document, value));
476 if (!strcmp((char *) key->data.scalar.value, "read"))
477 expr = pet_expr_access_set_read(expr,
478 extract_int(ctx, document, value));
479 if (!strcmp((char *) key->data.scalar.value, "write"))
480 expr = pet_expr_access_set_write(expr,
481 extract_int(ctx, document, value));
482 if (!strcmp((char *) key->data.scalar.value, "kill"))
483 expr = pet_expr_access_set_kill(expr,
484 extract_int(ctx, document, value));
487 return expr;
490 /* Extract operation expression specific fields from "node" and
491 * update "expr" accordingly.
493 static __isl_give pet_expr *extract_expr_op(isl_ctx *ctx,
494 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
496 yaml_node_pair_t *pair;
498 for (pair = node->data.mapping.pairs.start;
499 pair < node->data.mapping.pairs.top; ++pair) {
500 yaml_node_t *key, *value;
502 key = yaml_document_get_node(document, pair->key);
503 value = yaml_document_get_node(document, pair->value);
505 if (key->type != YAML_SCALAR_NODE)
506 isl_die(ctx, isl_error_invalid, "expecting scalar key",
507 return pet_expr_free(expr));
509 if (!strcmp((char *) key->data.scalar.value, "operation"))
510 expr = pet_expr_op_set_type(expr,
511 extract_op(ctx, document, value));
514 return expr;
517 /* Extract pet_expr_call specific fields from "node" and
518 * update "expr" accordingly.
520 static __isl_give pet_expr *extract_expr_call(isl_ctx *ctx,
521 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
523 yaml_node_pair_t *pair;
525 for (pair = node->data.mapping.pairs.start;
526 pair < node->data.mapping.pairs.top; ++pair) {
527 yaml_node_t *key, *value;
529 key = yaml_document_get_node(document, pair->key);
530 value = yaml_document_get_node(document, pair->value);
532 if (key->type != YAML_SCALAR_NODE)
533 isl_die(ctx, isl_error_invalid, "expecting scalar key",
534 return pet_expr_free(expr));
536 if (!strcmp((char *) key->data.scalar.value, "name"))
537 expr = pet_expr_call_set_name(expr,
538 extract_string(ctx, document, value));
541 return expr;
544 /* Extract pet_expr_cast specific fields from "node" and
545 * update "expr" accordingly.
547 static __isl_give pet_expr *extract_expr_cast(isl_ctx *ctx,
548 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
550 yaml_node_pair_t *pair;
552 for (pair = node->data.mapping.pairs.start;
553 pair < node->data.mapping.pairs.top; ++pair) {
554 yaml_node_t *key, *value;
556 key = yaml_document_get_node(document, pair->key);
557 value = yaml_document_get_node(document, pair->value);
559 if (key->type != YAML_SCALAR_NODE)
560 isl_die(ctx, isl_error_invalid, "expecting scalar key",
561 return pet_expr_free(expr));
563 if (!strcmp((char *) key->data.scalar.value, "type_name"))
564 expr = pet_expr_cast_set_type_name(expr,
565 extract_string(ctx, document, value));
568 return expr;
571 /* Extract pet_expr_int specific fields from "node" and
572 * update "expr" accordingly.
574 static __isl_give pet_expr *extract_expr_int(isl_ctx *ctx,
575 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
577 yaml_node_pair_t * pair;
579 for (pair = node->data.mapping.pairs.start;
580 pair < node->data.mapping.pairs.top; ++pair) {
581 yaml_node_t *key, *value;
583 key = yaml_document_get_node(document, pair->key);
584 value = yaml_document_get_node(document, pair->value);
586 if (key->type != YAML_SCALAR_NODE)
587 isl_die(ctx, isl_error_invalid, "expecting scalar key",
588 return pet_expr_free(expr));
590 if (!strcmp((char *) key->data.scalar.value, "value"))
591 expr = pet_expr_int_set_val(expr,
592 extract_val(ctx, document, value));
595 return expr;
598 /* Extract a pet_expr from "node".
600 * We first extract the type and arguments of the expression and
601 * then extract additional fields depending on the type.
603 static __isl_give pet_expr *extract_expr(isl_ctx *ctx,
604 yaml_document_t *document, yaml_node_t *node)
606 enum pet_expr_type type = pet_expr_error;
607 pet_expr *expr;
608 yaml_node_pair_t *pair;
610 if (node->type != YAML_MAPPING_NODE)
611 isl_die(ctx, isl_error_invalid, "expecting mapping",
612 return NULL);
614 for (pair = node->data.mapping.pairs.start;
615 pair < node->data.mapping.pairs.top; ++pair) {
616 yaml_node_t *key, *value;
618 key = yaml_document_get_node(document, pair->key);
619 value = yaml_document_get_node(document, pair->value);
621 if (key->type != YAML_SCALAR_NODE)
622 isl_die(ctx, isl_error_invalid, "expecting scalar key",
623 return NULL);
625 if (!strcmp((char *) key->data.scalar.value, "type"))
626 type = extract_expr_type(ctx, document, value);
629 if (type == pet_expr_error)
630 isl_die(ctx, isl_error_invalid, "cannot determine type",
631 return NULL);
633 expr = pet_expr_alloc(ctx, type);
634 if (!expr)
635 return NULL;
637 for (pair = node->data.mapping.pairs.start;
638 pair < node->data.mapping.pairs.top; ++pair) {
639 yaml_node_t *key, *value;
641 key = yaml_document_get_node(document, pair->key);
642 value = yaml_document_get_node(document, pair->value);
644 if (!strcmp((char *) key->data.scalar.value, "arguments"))
645 expr = extract_arguments(ctx, document, value, expr);
646 if (!expr)
647 return NULL;
650 switch (type) {
651 case pet_expr_error:
652 isl_die(ctx, isl_error_internal, "unreachable code",
653 return NULL);
654 case pet_expr_access:
655 expr = extract_expr_access(ctx, document, node, expr);
656 break;
657 case pet_expr_double:
658 expr = extract_expr_double(ctx, document, node, expr);
659 break;
660 case pet_expr_call:
661 expr = extract_expr_call(ctx, document, node, expr);
662 break;
663 case pet_expr_cast:
664 expr = extract_expr_cast(ctx, document, node, expr);
665 break;
666 case pet_expr_int:
667 expr = extract_expr_int(ctx, document, node, expr);
668 break;
669 case pet_expr_op:
670 expr = extract_expr_op(ctx, document, node, expr);
671 break;
674 return expr;
677 /* Extract a pet_tree_type from "node".
679 static enum pet_tree_type extract_tree_type(isl_ctx *ctx,
680 yaml_document_t *document, yaml_node_t *node)
682 if (node->type != YAML_SCALAR_NODE)
683 isl_die(ctx, isl_error_invalid, "expecting scalar node",
684 return -1);
686 return pet_tree_str_type((char *) node->data.scalar.value);
689 static __isl_give pet_tree *extract_tree(isl_ctx *ctx,
690 yaml_document_t *document, yaml_node_t *node);
692 /* Extract a pet_tree of type pet_tree_block from "node".
694 static __isl_give pet_tree *extract_tree_block(isl_ctx *ctx,
695 yaml_document_t *document, yaml_node_t *node)
697 int block = 0;
698 int i, n;
699 yaml_node_pair_t *pair;
700 yaml_node_item_t *item;
701 yaml_node_t *children = NULL;
702 pet_tree *tree;
704 for (pair = node->data.mapping.pairs.start;
705 pair < node->data.mapping.pairs.top; ++pair) {
706 yaml_node_t *key, *value;
708 key = yaml_document_get_node(document, pair->key);
709 value = yaml_document_get_node(document, pair->value);
711 if (key->type != YAML_SCALAR_NODE)
712 isl_die(ctx, isl_error_invalid, "expecting scalar key",
713 return NULL);
715 if (!strcmp((char *) key->data.scalar.value, "block"))
716 block = extract_int(ctx, document, value);
717 if (!strcmp((char *) key->data.scalar.value, "children"))
718 children = value;
721 if (!children)
722 n = 0;
723 else
724 n = children->data.sequence.items.top -
725 children->data.sequence.items.start;
727 tree = pet_tree_new_block(ctx, block, n);
728 if (!children)
729 return tree;
731 for (item = children->data.sequence.items.start, i = 0;
732 item < children->data.sequence.items.top; ++item, ++i) {
733 yaml_node_t *n;
734 pet_tree *child;
736 n = yaml_document_get_node(document, *item);
737 child = extract_tree(ctx, document, n);
738 tree = pet_tree_block_add_child(tree, child);
741 return tree;
744 /* Extract a pet_tree of type pet_tree_decl from "node".
746 static __isl_give pet_tree *extract_tree_decl(isl_ctx *ctx,
747 yaml_document_t *document, yaml_node_t *node)
749 yaml_node_pair_t *pair;
750 pet_expr *var = NULL;
752 for (pair = node->data.mapping.pairs.start;
753 pair < node->data.mapping.pairs.top; ++pair) {
754 yaml_node_t *key, *value;
756 key = yaml_document_get_node(document, pair->key);
757 value = yaml_document_get_node(document, pair->value);
759 if (key->type != YAML_SCALAR_NODE)
760 isl_die(ctx, isl_error_invalid, "expecting scalar key",
761 return NULL);
763 if (!strcmp((char *) key->data.scalar.value, "variable")) {
764 var = extract_expr(ctx, document, value);
765 if (!var)
766 return NULL;
770 if (!var)
771 isl_die(ctx, isl_error_invalid,
772 "no variable field", return NULL);
774 return pet_tree_new_decl(var);
777 /* Extract a pet_tree of type pet_tree_decl_init from "node".
779 static __isl_give pet_tree *extract_tree_decl_init(isl_ctx *ctx,
780 yaml_document_t *document, yaml_node_t *node)
782 yaml_node_pair_t *pair;
783 pet_expr *var = NULL;
784 pet_expr *init = NULL;
786 for (pair = node->data.mapping.pairs.start;
787 pair < node->data.mapping.pairs.top; ++pair) {
788 yaml_node_t *key, *value;
790 key = yaml_document_get_node(document, pair->key);
791 value = yaml_document_get_node(document, pair->value);
793 if (key->type != YAML_SCALAR_NODE)
794 isl_die(ctx, isl_error_invalid, "expecting scalar key",
795 return NULL);
797 if (!strcmp((char *) key->data.scalar.value, "variable")) {
798 var = extract_expr(ctx, document, value);
799 if (!var)
800 goto error;
802 if (!strcmp((char *) key->data.scalar.value,
803 "initialization")) {
804 init = extract_expr(ctx, document, value);
805 if (!init)
806 goto error;
810 if (!var)
811 isl_die(ctx, isl_error_invalid,
812 "no variable field", goto error);
813 if (!init)
814 isl_die(ctx, isl_error_invalid,
815 "no initialization field", goto error);
817 return pet_tree_new_decl_init(var, init);
818 error:
819 pet_expr_free(var);
820 pet_expr_free(init);
821 return NULL;
824 /* Extract a pet_tree of type pet_tree_expr from "node".
826 static __isl_give pet_tree *extract_tree_expr(isl_ctx *ctx,
827 yaml_document_t *document, yaml_node_t *node)
829 yaml_node_pair_t *pair;
830 pet_expr *expr = NULL;
832 for (pair = node->data.mapping.pairs.start;
833 pair < node->data.mapping.pairs.top; ++pair) {
834 yaml_node_t *key, *value;
836 key = yaml_document_get_node(document, pair->key);
837 value = yaml_document_get_node(document, pair->value);
839 if (key->type != YAML_SCALAR_NODE)
840 isl_die(ctx, isl_error_invalid, "expecting scalar key",
841 return NULL);
843 if (!strcmp((char *) key->data.scalar.value, "expr")) {
844 expr = extract_expr(ctx, document, value);
845 if (!expr)
846 return NULL;
850 if (!expr)
851 isl_die(ctx, isl_error_invalid,
852 "no expr field", return NULL);
854 return pet_tree_new_expr(expr);
857 /* Extract a pet_tree of type pet_tree_while from "node".
859 static __isl_give pet_tree *extract_tree_while(isl_ctx *ctx,
860 yaml_document_t *document, yaml_node_t *node)
862 yaml_node_pair_t *pair;
863 pet_expr *cond = NULL;
864 pet_tree *body = NULL;
866 for (pair = node->data.mapping.pairs.start;
867 pair < node->data.mapping.pairs.top; ++pair) {
868 yaml_node_t *key, *value;
870 key = yaml_document_get_node(document, pair->key);
871 value = yaml_document_get_node(document, pair->value);
873 if (key->type != YAML_SCALAR_NODE)
874 isl_die(ctx, isl_error_invalid, "expecting scalar key",
875 return NULL);
877 if (!strcmp((char *) key->data.scalar.value, "condition")) {
878 cond = extract_expr(ctx, document, value);
879 if (!cond)
880 goto error;
882 if (!strcmp((char *) key->data.scalar.value, "body")) {
883 body = extract_tree(ctx, document, value);
884 if (!body)
885 goto error;
889 if (!cond)
890 isl_die(ctx, isl_error_invalid,
891 "no condition field", goto error);
892 if (!body)
893 isl_die(ctx, isl_error_invalid,
894 "no body field", goto error);
896 return pet_tree_new_while(cond, body);
897 error:
898 pet_expr_free(cond);
899 pet_tree_free(body);
900 return NULL;
903 /* Extract a pet_tree of type pet_tree_infinite_loop from "node".
905 static __isl_give pet_tree *extract_tree_infinite_loop(isl_ctx *ctx,
906 yaml_document_t *document, yaml_node_t *node)
908 yaml_node_pair_t *pair;
909 pet_tree *body;
911 for (pair = node->data.mapping.pairs.start;
912 pair < node->data.mapping.pairs.top; ++pair) {
913 yaml_node_t *key, *value;
915 key = yaml_document_get_node(document, pair->key);
916 value = yaml_document_get_node(document, pair->value);
918 if (key->type != YAML_SCALAR_NODE)
919 isl_die(ctx, isl_error_invalid, "expecting scalar key",
920 return NULL);
922 if (!strcmp((char *) key->data.scalar.value, "body")) {
923 body = extract_tree(ctx, document, value);
924 if (!body)
925 return NULL;
929 if (!body)
930 isl_die(ctx, isl_error_invalid,
931 "no body field", return NULL);
933 return pet_tree_new_infinite_loop(body);
936 /* Extract a pet_tree of type pet_tree_if from "node".
938 static __isl_give pet_tree *extract_tree_if(isl_ctx *ctx,
939 yaml_document_t *document, yaml_node_t *node)
941 yaml_node_pair_t *pair;
942 pet_expr *cond = NULL;
943 pet_tree *then_body = NULL;
945 for (pair = node->data.mapping.pairs.start;
946 pair < node->data.mapping.pairs.top; ++pair) {
947 yaml_node_t *key, *value;
949 key = yaml_document_get_node(document, pair->key);
950 value = yaml_document_get_node(document, pair->value);
952 if (key->type != YAML_SCALAR_NODE)
953 isl_die(ctx, isl_error_invalid, "expecting scalar key",
954 return NULL);
956 if (!strcmp((char *) key->data.scalar.value, "condition")) {
957 cond = extract_expr(ctx, document, value);
958 if (!cond)
959 goto error;
961 if (!strcmp((char *) key->data.scalar.value, "then")) {
962 then_body = extract_tree(ctx, document, value);
963 if (!then_body)
964 goto error;
968 if (!cond)
969 isl_die(ctx, isl_error_invalid,
970 "no condition field", goto error);
971 if (!then_body)
972 isl_die(ctx, isl_error_invalid,
973 "no then body", goto error);
975 return pet_tree_new_if(cond, then_body);
976 error:
977 pet_expr_free(cond);
978 pet_tree_free(then_body);
979 return NULL;
982 /* Extract a pet_tree of type pet_tree_if_else from "node".
984 static __isl_give pet_tree *extract_tree_if_else(isl_ctx *ctx,
985 yaml_document_t *document, yaml_node_t *node)
987 yaml_node_pair_t *pair;
988 pet_expr *cond = NULL;
989 pet_tree *then_body = NULL;
990 pet_tree *else_body = NULL;
992 for (pair = node->data.mapping.pairs.start;
993 pair < node->data.mapping.pairs.top; ++pair) {
994 yaml_node_t *key, *value;
996 key = yaml_document_get_node(document, pair->key);
997 value = yaml_document_get_node(document, pair->value);
999 if (key->type != YAML_SCALAR_NODE)
1000 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1001 return NULL);
1003 if (!strcmp((char *) key->data.scalar.value, "condition")) {
1004 cond = extract_expr(ctx, document, value);
1005 if (!cond)
1006 goto error;
1008 if (!strcmp((char *) key->data.scalar.value, "then")) {
1009 then_body = extract_tree(ctx, document, value);
1010 if (!then_body)
1011 goto error;
1013 if (!strcmp((char *) key->data.scalar.value, "else")) {
1014 else_body = extract_tree(ctx, document, value);
1015 if (!else_body)
1016 goto error;
1020 if (!cond)
1021 isl_die(ctx, isl_error_invalid,
1022 "no condition field", goto error);
1023 if (!then_body)
1024 isl_die(ctx, isl_error_invalid,
1025 "no then body", goto error);
1026 if (!else_body)
1027 isl_die(ctx, isl_error_invalid,
1028 "no else body", goto error);
1030 return pet_tree_new_if_else(cond, then_body, else_body);
1031 error:
1032 pet_expr_free(cond);
1033 pet_tree_free(then_body);
1034 pet_tree_free(else_body);
1035 return NULL;
1038 /* Extract a pet_tree of type pet_tree_for from "node".
1040 static __isl_give pet_tree *extract_tree_for(isl_ctx *ctx,
1041 yaml_document_t *document, yaml_node_t *node)
1043 yaml_node_pair_t *pair;
1044 int declared = 0;
1045 int independent = 0;
1046 pet_expr *iv = NULL;
1047 pet_expr *init = NULL;
1048 pet_expr *cond = NULL;
1049 pet_expr *inc = NULL;
1050 pet_tree *body = NULL;
1052 for (pair = node->data.mapping.pairs.start;
1053 pair < node->data.mapping.pairs.top; ++pair) {
1054 yaml_node_t *key, *value;
1056 key = yaml_document_get_node(document, pair->key);
1057 value = yaml_document_get_node(document, pair->value);
1059 if (key->type != YAML_SCALAR_NODE)
1060 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1061 return NULL);
1063 if (!strcmp((char *) key->data.scalar.value, "declared"))
1064 declared = extract_int(ctx, document, value);
1065 if (!strcmp((char *) key->data.scalar.value, "independent"))
1066 independent = extract_int(ctx, document, value);
1067 if (!strcmp((char *) key->data.scalar.value, "variable")) {
1068 iv = extract_expr(ctx, document, value);
1069 if (!iv)
1070 goto error;
1072 if (!strcmp((char *) key->data.scalar.value,
1073 "initialization")) {
1074 init = extract_expr(ctx, document, value);
1075 if (!init)
1076 goto error;
1078 if (!strcmp((char *) key->data.scalar.value, "condition")) {
1079 cond = extract_expr(ctx, document, value);
1080 if (!cond)
1081 goto error;
1083 if (!strcmp((char *) key->data.scalar.value, "increment")) {
1084 inc = extract_expr(ctx, document, value);
1085 if (!inc)
1086 goto error;
1088 if (!strcmp((char *) key->data.scalar.value, "body")) {
1089 body = extract_tree(ctx, document, value);
1090 if (!body)
1091 goto error;
1095 if (!iv)
1096 isl_die(ctx, isl_error_invalid,
1097 "no variable field", goto error);
1098 if (!init)
1099 isl_die(ctx, isl_error_invalid,
1100 "no initialization field", goto error);
1101 if (!cond)
1102 isl_die(ctx, isl_error_invalid,
1103 "no condition field", goto error);
1104 if (!inc)
1105 isl_die(ctx, isl_error_invalid,
1106 "no increment field", goto error);
1107 if (!body)
1108 isl_die(ctx, isl_error_invalid,
1109 "no body field", goto error);
1111 return pet_tree_new_for(independent, declared, iv, init, cond, inc,
1112 body);
1113 error:
1114 pet_expr_free(iv);
1115 pet_expr_free(init);
1116 pet_expr_free(cond);
1117 pet_expr_free(inc);
1118 pet_tree_free(body);
1119 return NULL;
1122 /* Extract a pet_tree from "node".
1124 * We first extract the type of the pet_tree and then call
1125 * the appropriate function to extract and construct a pet_tree
1126 * of that type.
1128 static __isl_give pet_tree *extract_tree(isl_ctx *ctx,
1129 yaml_document_t *document, yaml_node_t *node)
1131 enum pet_tree_type type = pet_tree_error;
1132 pet_tree *tree;
1133 yaml_node_pair_t *pair;
1135 if (node->type != YAML_MAPPING_NODE)
1136 isl_die(ctx, isl_error_invalid, "expecting mapping",
1137 return NULL);
1139 for (pair = node->data.mapping.pairs.start;
1140 pair < node->data.mapping.pairs.top; ++pair) {
1141 yaml_node_t *key, *value;
1143 key = yaml_document_get_node(document, pair->key);
1144 value = yaml_document_get_node(document, pair->value);
1146 if (key->type != YAML_SCALAR_NODE)
1147 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1148 return NULL);
1150 if (!strcmp((char *) key->data.scalar.value, "type"))
1151 type = extract_tree_type(ctx, document, value);
1154 if (type == pet_tree_error)
1155 isl_die(ctx, isl_error_invalid, "cannot determine type",
1156 return NULL);
1158 switch (type) {
1159 case pet_tree_error:
1160 return NULL;
1161 case pet_tree_block:
1162 tree = extract_tree_block(ctx, document, node);
1163 break;
1164 case pet_tree_break:
1165 tree = pet_tree_new_break(ctx);
1166 break;
1167 case pet_tree_continue:
1168 tree = pet_tree_new_continue(ctx);
1169 break;
1170 case pet_tree_decl:
1171 tree = extract_tree_decl(ctx, document, node);
1172 break;
1173 case pet_tree_decl_init:
1174 tree = extract_tree_decl_init(ctx, document, node);
1175 break;
1176 case pet_tree_expr:
1177 tree = extract_tree_expr(ctx, document, node);
1178 break;
1179 case pet_tree_for:
1180 tree = extract_tree_for(ctx, document, node);
1181 break;
1182 case pet_tree_while:
1183 tree = extract_tree_while(ctx, document, node);
1184 break;
1185 case pet_tree_infinite_loop:
1186 tree = extract_tree_infinite_loop(ctx, document, node);
1187 break;
1188 case pet_tree_if:
1189 tree = extract_tree_if(ctx, document, node);
1190 break;
1191 case pet_tree_if_else:
1192 tree = extract_tree_if_else(ctx, document, node);
1193 break;
1196 return tree;
1199 static struct pet_stmt *extract_stmt_arguments(isl_ctx *ctx,
1200 yaml_document_t *document, yaml_node_t *node, struct pet_stmt *stmt)
1202 int i;
1203 yaml_node_item_t *item;
1205 if (node->type != YAML_SEQUENCE_NODE)
1206 isl_die(ctx, isl_error_invalid, "expecting sequence",
1207 return pet_stmt_free(stmt));
1209 stmt->n_arg = node->data.sequence.items.top
1210 - node->data.sequence.items.start;
1211 stmt->args = isl_calloc_array(ctx, pet_expr *, stmt->n_arg);
1212 if (!stmt->args)
1213 return pet_stmt_free(stmt);
1215 for (item = node->data.sequence.items.start, i = 0;
1216 item < node->data.sequence.items.top; ++item, ++i) {
1217 yaml_node_t *n;
1219 n = yaml_document_get_node(document, *item);
1220 stmt->args[i] = extract_expr(ctx, document, n);
1221 if (!stmt->args[i])
1222 return pet_stmt_free(stmt);
1225 return stmt;
1228 static struct pet_stmt *extract_stmt(isl_ctx *ctx, yaml_document_t *document,
1229 yaml_node_t *node)
1231 struct pet_stmt *stmt;
1232 yaml_node_pair_t * pair;
1233 int line = -1;
1234 unsigned start = 0, end = 0;
1235 char *indent = NULL;
1237 if (node->type != YAML_MAPPING_NODE)
1238 isl_die(ctx, isl_error_invalid, "expecting mapping",
1239 return NULL);
1241 stmt = isl_calloc_type(ctx, struct pet_stmt);
1242 if (!stmt)
1243 return NULL;
1245 stmt->loc = &pet_loc_dummy;
1247 for (pair = node->data.mapping.pairs.start;
1248 pair < node->data.mapping.pairs.top; ++pair) {
1249 yaml_node_t *key, *value;
1251 key = yaml_document_get_node(document, pair->key);
1252 value = yaml_document_get_node(document, pair->value);
1254 if (key->type != YAML_SCALAR_NODE)
1255 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1256 return pet_stmt_free(stmt));
1258 if (!strcmp((char *) key->data.scalar.value, "indent"))
1259 indent = extract_string(ctx, document, value);
1260 if (!strcmp((char *) key->data.scalar.value, "line"))
1261 line = extract_int(ctx, document, value);
1262 if (!strcmp((char *) key->data.scalar.value, "start"))
1263 start = extract_int(ctx, document, value);
1264 if (!strcmp((char *) key->data.scalar.value, "end"))
1265 end = extract_int(ctx, document, value);
1266 if (!strcmp((char *) key->data.scalar.value, "domain"))
1267 stmt->domain = extract_set(ctx, document, value);
1268 if (!strcmp((char *) key->data.scalar.value, "body"))
1269 stmt->body = extract_tree(ctx, document, value);
1271 if (!strcmp((char *) key->data.scalar.value, "arguments"))
1272 stmt = extract_stmt_arguments(ctx, document,
1273 value, stmt);
1274 if (!stmt)
1275 return NULL;
1278 if (!indent)
1279 indent = strdup("");
1280 stmt->loc = pet_loc_alloc(ctx, start, end, line, indent);
1281 if (!stmt->loc)
1282 return pet_stmt_free(stmt);
1284 return stmt;
1287 static struct pet_scop *extract_statements(isl_ctx *ctx,
1288 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1290 int i;
1291 yaml_node_item_t *item;
1293 if (node->type != YAML_SEQUENCE_NODE)
1294 isl_die(ctx, isl_error_invalid, "expecting sequence",
1295 return NULL);
1297 scop->n_stmt = node->data.sequence.items.top
1298 - node->data.sequence.items.start;
1299 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, scop->n_stmt);
1300 if (!scop->stmts)
1301 return pet_scop_free(scop);
1303 for (item = node->data.sequence.items.start, i = 0;
1304 item < node->data.sequence.items.top; ++item, ++i) {
1305 yaml_node_t *n;
1307 n = yaml_document_get_node(document, *item);
1308 scop->stmts[i] = extract_stmt(ctx, document, n);
1309 if (!scop->stmts[i])
1310 return pet_scop_free(scop);
1313 return scop;
1316 /* Extract a pet_implication from "node".
1318 static struct pet_implication *extract_implication(isl_ctx *ctx,
1319 yaml_document_t *document, yaml_node_t *node)
1321 struct pet_implication *implication;
1322 yaml_node_pair_t * pair;
1324 if (node->type != YAML_MAPPING_NODE)
1325 isl_die(ctx, isl_error_invalid, "expecting mapping",
1326 return NULL);
1328 implication = isl_calloc_type(ctx, struct pet_implication);
1329 if (!implication)
1330 return NULL;
1332 for (pair = node->data.mapping.pairs.start;
1333 pair < node->data.mapping.pairs.top; ++pair) {
1334 yaml_node_t *key, *value;
1336 key = yaml_document_get_node(document, pair->key);
1337 value = yaml_document_get_node(document, pair->value);
1339 if (key->type != YAML_SCALAR_NODE)
1340 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1341 return pet_implication_free(implication));
1343 if (!strcmp((char *) key->data.scalar.value, "satisfied"))
1344 implication->satisfied =
1345 extract_int(ctx, document, value);
1346 if (!strcmp((char *) key->data.scalar.value, "extension"))
1347 implication->extension =
1348 extract_map(ctx, document, value);
1351 return implication;
1354 /* Extract a sequence of implications from "node" and
1355 * store them in scop->implications.
1357 static struct pet_scop *extract_implications(isl_ctx *ctx,
1358 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1360 int i;
1361 yaml_node_item_t *item;
1363 if (node->type != YAML_SEQUENCE_NODE)
1364 isl_die(ctx, isl_error_invalid, "expecting sequence",
1365 return NULL);
1367 scop->n_implication = node->data.sequence.items.top
1368 - node->data.sequence.items.start;
1369 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
1370 scop->n_implication);
1371 if (!scop->implications)
1372 return pet_scop_free(scop);
1374 for (item = node->data.sequence.items.start, i = 0;
1375 item < node->data.sequence.items.top; ++item, ++i) {
1376 yaml_node_t *n;
1378 n = yaml_document_get_node(document, *item);
1379 scop->implications[i] = extract_implication(ctx, document, n);
1380 if (!scop->implications[i])
1381 return pet_scop_free(scop);
1384 return scop;
1387 /* Extract a pet_independence from "node".
1389 static struct pet_independence *extract_independence(isl_ctx *ctx,
1390 yaml_document_t *document, yaml_node_t *node)
1392 struct pet_independence *independence;
1393 yaml_node_pair_t * pair;
1395 if (node->type != YAML_MAPPING_NODE)
1396 isl_die(ctx, isl_error_invalid, "expecting mapping",
1397 return NULL);
1399 independence = isl_calloc_type(ctx, struct pet_independence);
1400 if (!independence)
1401 return NULL;
1403 for (pair = node->data.mapping.pairs.start;
1404 pair < node->data.mapping.pairs.top; ++pair) {
1405 yaml_node_t *key, *value;
1407 key = yaml_document_get_node(document, pair->key);
1408 value = yaml_document_get_node(document, pair->value);
1410 if (key->type != YAML_SCALAR_NODE)
1411 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1412 return pet_independence_free(independence));
1414 if (!strcmp((char *) key->data.scalar.value, "filter"))
1415 independence->filter =
1416 extract_union_map(ctx, document, value);
1417 if (!strcmp((char *) key->data.scalar.value, "local"))
1418 independence->local =
1419 extract_union_set(ctx, document, value);
1422 if (!independence->filter)
1423 isl_die(ctx, isl_error_invalid, "no filter field",
1424 return pet_independence_free(independence));
1425 if (!independence->local)
1426 isl_die(ctx, isl_error_invalid, "no local field",
1427 return pet_independence_free(independence));
1429 return independence;
1432 /* Extract a sequence of independences from "node" and
1433 * store them in scop->independences.
1435 static struct pet_scop *extract_independences(isl_ctx *ctx,
1436 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1438 int i;
1439 yaml_node_item_t *item;
1441 if (node->type != YAML_SEQUENCE_NODE)
1442 isl_die(ctx, isl_error_invalid, "expecting sequence",
1443 return NULL);
1445 scop->n_independence = node->data.sequence.items.top
1446 - node->data.sequence.items.start;
1447 scop->independences = isl_calloc_array(ctx, struct pet_independence *,
1448 scop->n_independence);
1449 if (!scop->independences)
1450 return pet_scop_free(scop);
1452 for (item = node->data.sequence.items.start, i = 0;
1453 item < node->data.sequence.items.top; ++item, ++i) {
1454 yaml_node_t *n;
1456 n = yaml_document_get_node(document, *item);
1457 scop->independences[i] = extract_independence(ctx, document, n);
1458 if (!scop->independences[i])
1459 return pet_scop_free(scop);
1462 return scop;
1465 static struct pet_scop *extract_scop(isl_ctx *ctx, yaml_document_t *document,
1466 yaml_node_t *node)
1468 struct pet_scop *scop;
1469 yaml_node_pair_t * pair;
1471 if (!node)
1472 return NULL;
1474 if (node->type != YAML_MAPPING_NODE)
1475 isl_die(ctx, isl_error_invalid, "expecting mapping",
1476 return NULL);
1478 scop = pet_scop_alloc(ctx);
1479 if (!scop)
1480 return NULL;
1482 for (pair = node->data.mapping.pairs.start;
1483 pair < node->data.mapping.pairs.top; ++pair) {
1484 yaml_node_t *key, *value;
1486 key = yaml_document_get_node(document, pair->key);
1487 value = yaml_document_get_node(document, pair->value);
1489 if (key->type != YAML_SCALAR_NODE)
1490 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1491 return pet_scop_free(scop));
1492 if (!strcmp((char *) key->data.scalar.value, "context"))
1493 scop->context = extract_set(ctx, document, value);
1494 if (!strcmp((char *) key->data.scalar.value, "context_value"))
1495 scop->context_value = extract_set(ctx, document, value);
1496 if (!strcmp((char *) key->data.scalar.value, "schedule"))
1497 scop->schedule = extract_schedule(ctx, document, value);
1498 if (!strcmp((char *) key->data.scalar.value, "types"))
1499 scop = extract_types(ctx, document, value, scop);
1500 if (!strcmp((char *) key->data.scalar.value, "arrays"))
1501 scop = extract_arrays(ctx, document, value, scop);
1502 if (!strcmp((char *) key->data.scalar.value, "statements"))
1503 scop = extract_statements(ctx, document, value, scop);
1504 if (!strcmp((char *) key->data.scalar.value, "implications"))
1505 scop = extract_implications(ctx, document, value, scop);
1506 if (!strcmp((char *) key->data.scalar.value, "independences"))
1507 scop = extract_independences(ctx,
1508 document, value, scop);
1509 if (!scop)
1510 return NULL;
1513 if (!scop->context_value) {
1514 isl_space *space = isl_space_params_alloc(ctx, 0);
1515 scop->context_value = isl_set_universe(space);
1516 if (!scop->context_value)
1517 return pet_scop_free(scop);
1520 return scop;
1523 /* Extract a pet_scop from the YAML description in "in".
1525 struct pet_scop *pet_scop_parse(isl_ctx *ctx, FILE *in)
1527 struct pet_scop *scop = NULL;
1528 yaml_parser_t parser;
1529 yaml_node_t *root;
1530 yaml_document_t document = { 0 };
1532 yaml_parser_initialize(&parser);
1534 yaml_parser_set_input_file(&parser, in);
1536 if (!yaml_parser_load(&parser, &document))
1537 goto error;
1539 root = yaml_document_get_root_node(&document);
1541 scop = extract_scop(ctx, &document, root);
1543 yaml_document_delete(&document);
1545 yaml_parser_delete(&parser);
1547 return scop;
1548 error:
1549 yaml_parser_delete(&parser);
1550 pet_scop_free(scop);
1551 return NULL;