extract_affine_from_call: extract out is_min_or_max_builtin
[pet.git] / parse.c
blob58002a4e3ad94e7cd57b58c5e61f64c607fa4e0b
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 "expr.h"
39 #include "loc.h"
40 #include "scop.h"
41 #include "scop_yaml.h"
42 #include "tree.h"
44 static char *extract_string(isl_ctx *ctx, yaml_document_t *document,
45 yaml_node_t *node)
47 if (node->type != YAML_SCALAR_NODE)
48 isl_die(ctx, isl_error_invalid, "expecting scalar node",
49 return NULL);
51 return strdup((char *) node->data.scalar.value);
54 static int extract_int(isl_ctx *ctx, yaml_document_t *document,
55 yaml_node_t *node)
57 if (node->type != YAML_SCALAR_NODE)
58 isl_die(ctx, isl_error_invalid, "expecting scalar node",
59 return -1);
61 return atoi((char *) node->data.scalar.value);
64 static double extract_double(isl_ctx *ctx, yaml_document_t *document,
65 yaml_node_t *node)
67 if (node->type != YAML_SCALAR_NODE)
68 isl_die(ctx, isl_error_invalid, "expecting scalar node",
69 return -1);
71 return strtod((char *) node->data.scalar.value, NULL);
74 static enum pet_expr_type extract_expr_type(isl_ctx *ctx,
75 yaml_document_t *document, yaml_node_t *node)
77 if (node->type != YAML_SCALAR_NODE)
78 isl_die(ctx, isl_error_invalid, "expecting scalar node",
79 return -1);
81 return pet_str_type((char *) node->data.scalar.value);
84 static enum pet_op_type extract_op(isl_ctx *ctx, yaml_document_t *document,
85 yaml_node_t *node)
87 if (node->type != YAML_SCALAR_NODE)
88 isl_die(ctx, isl_error_invalid, "expecting scalar node",
89 return -1);
91 return pet_str_op((char *) node->data.scalar.value);
94 static __isl_give isl_set *extract_set(isl_ctx *ctx, yaml_document_t *document,
95 yaml_node_t *node)
97 if (node->type != YAML_SCALAR_NODE)
98 isl_die(ctx, isl_error_invalid, "expecting scalar node",
99 return NULL);
101 return isl_set_read_from_str(ctx, (char *) node->data.scalar.value);
104 static __isl_give isl_id *extract_id(isl_ctx *ctx, yaml_document_t *document,
105 yaml_node_t *node)
107 if (node->type != YAML_SCALAR_NODE)
108 isl_die(ctx, isl_error_invalid, "expecting scalar node",
109 return NULL);
111 return isl_id_alloc(ctx, (char *) node->data.scalar.value, NULL);
114 static __isl_give isl_map *extract_map(isl_ctx *ctx, yaml_document_t *document,
115 yaml_node_t *node)
117 if (node->type != YAML_SCALAR_NODE)
118 isl_die(ctx, isl_error_invalid, "expecting scalar node",
119 return NULL);
121 return isl_map_read_from_str(ctx, (char *) node->data.scalar.value);
124 /* Extract an isl_union_set from "node".
126 static __isl_give isl_union_set *extract_union_set(isl_ctx *ctx,
127 yaml_document_t *document, yaml_node_t *node)
129 if (node->type != YAML_SCALAR_NODE)
130 isl_die(ctx, isl_error_invalid, "expecting scalar node",
131 return NULL);
133 return isl_union_set_read_from_str(ctx,
134 (char *) node->data.scalar.value);
137 /* Extract an isl_union_map from "node".
139 static __isl_give isl_union_map *extract_union_map(isl_ctx *ctx,
140 yaml_document_t *document, yaml_node_t *node)
142 if (node->type != YAML_SCALAR_NODE)
143 isl_die(ctx, isl_error_invalid, "expecting scalar node",
144 return NULL);
146 return isl_union_map_read_from_str(ctx,
147 (char *) node->data.scalar.value);
150 /* Extract an isl_val from "node".
152 static __isl_give isl_val *extract_val(isl_ctx *ctx, yaml_document_t *document,
153 yaml_node_t *node)
155 if (node->type != YAML_SCALAR_NODE)
156 isl_die(ctx, isl_error_invalid, "expecting scalar node",
157 return NULL);
159 return isl_val_read_from_str(ctx, (char *) node->data.scalar.value);
162 /* Extract an isl_multi_pw_aff from "node".
164 static __isl_give isl_multi_pw_aff *extract_multi_pw_aff(isl_ctx *ctx,
165 yaml_document_t *document, yaml_node_t *node)
167 if (node->type != YAML_SCALAR_NODE)
168 isl_die(ctx, isl_error_invalid, "expecting scalar node",
169 return NULL);
171 return isl_multi_pw_aff_read_from_str(ctx,
172 (char *) node->data.scalar.value);
175 /* Extract an isl_schedule from "node".
177 static __isl_give isl_schedule *extract_schedule(isl_ctx *ctx,
178 yaml_document_t *document, yaml_node_t *node)
180 if (node->type != YAML_SCALAR_NODE)
181 isl_die(ctx, isl_error_invalid, "expecting scalar node",
182 return NULL);
184 return isl_schedule_read_from_str(ctx,
185 (char *) node->data.scalar.value);
188 /* Extract a pet_type from "node".
190 static struct pet_type *extract_type(isl_ctx *ctx,
191 yaml_document_t *document, yaml_node_t *node)
193 struct pet_type *type;
194 yaml_node_pair_t * pair;
196 if (node->type != YAML_MAPPING_NODE)
197 isl_die(ctx, isl_error_invalid, "expecting mapping",
198 return NULL);
200 type = isl_calloc_type(ctx, struct pet_type);
201 if (!type)
202 return NULL;
204 for (pair = node->data.mapping.pairs.start;
205 pair < node->data.mapping.pairs.top; ++pair) {
206 yaml_node_t *key, *value;
208 key = yaml_document_get_node(document, pair->key);
209 value = yaml_document_get_node(document, pair->value);
211 if (key->type != YAML_SCALAR_NODE)
212 isl_die(ctx, isl_error_invalid, "expecting scalar key",
213 return pet_type_free(type));
215 if (!strcmp((char *) key->data.scalar.value, "name"))
216 type->name = extract_string(ctx, document, value);
217 if (!strcmp((char *) key->data.scalar.value, "definition"))
218 type->definition = extract_string(ctx, document, value);
221 return type;
224 /* Extract a sequence of types from "node" and store them in scop->types.
226 static struct pet_scop *extract_types(isl_ctx *ctx,
227 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
229 int i;
230 yaml_node_item_t *item;
232 if (node->type != YAML_SEQUENCE_NODE)
233 isl_die(ctx, isl_error_invalid, "expecting sequence",
234 return NULL);
236 scop->n_type = node->data.sequence.items.top
237 - node->data.sequence.items.start;
238 scop->types = isl_calloc_array(ctx, struct pet_type *, scop->n_type);
239 if (!scop->types)
240 return pet_scop_free(scop);
242 for (item = node->data.sequence.items.start, i = 0;
243 item < node->data.sequence.items.top; ++item, ++i) {
244 yaml_node_t *n;
246 n = yaml_document_get_node(document, *item);
247 scop->types[i] = extract_type(ctx, document, n);
248 if (!scop->types[i])
249 return pet_scop_free(scop);
252 return scop;
255 static struct pet_array *extract_array(isl_ctx *ctx, yaml_document_t *document,
256 yaml_node_t *node)
258 struct pet_array *array;
259 yaml_node_pair_t * pair;
261 if (node->type != YAML_MAPPING_NODE)
262 isl_die(ctx, isl_error_invalid, "expecting mapping",
263 return NULL);
265 array = isl_calloc_type(ctx, struct pet_array);
266 if (!array)
267 return NULL;
269 for (pair = node->data.mapping.pairs.start;
270 pair < node->data.mapping.pairs.top; ++pair) {
271 yaml_node_t *key, *value;
273 key = yaml_document_get_node(document, pair->key);
274 value = yaml_document_get_node(document, pair->value);
276 if (key->type != YAML_SCALAR_NODE)
277 isl_die(ctx, isl_error_invalid, "expecting scalar key",
278 return pet_array_free(array));
280 if (!strcmp((char *) key->data.scalar.value, "context"))
281 array->context = extract_set(ctx, document, value);
282 if (!strcmp((char *) key->data.scalar.value, "extent"))
283 array->extent = extract_set(ctx, document, value);
284 if (!strcmp((char *) key->data.scalar.value, "value_bounds"))
285 array->value_bounds = extract_set(ctx, document, value);
286 if (!strcmp((char *) key->data.scalar.value, "element_type"))
287 array->element_type =
288 extract_string(ctx, document, value);
289 if (!strcmp((char *) key->data.scalar.value, "element_size"))
290 array->element_size = extract_int(ctx, document, value);
291 if (!strcmp((char *) key->data.scalar.value,
292 "element_is_record"))
293 array->element_is_record =
294 extract_int(ctx, document, value);
295 if (!strcmp((char *) key->data.scalar.value, "live_out"))
296 array->live_out = extract_int(ctx, document, value);
297 if (!strcmp((char *) key->data.scalar.value,
298 "uniquely_defined"))
299 array->uniquely_defined =
300 extract_int(ctx, document, value);
301 if (!strcmp((char *) key->data.scalar.value, "declared"))
302 array->declared = extract_int(ctx, document, value);
303 if (!strcmp((char *) key->data.scalar.value, "exposed"))
304 array->exposed = extract_int(ctx, document, value);
307 return array;
310 static struct pet_scop *extract_arrays(isl_ctx *ctx, yaml_document_t *document,
311 yaml_node_t *node, struct pet_scop *scop)
313 int i;
314 yaml_node_item_t *item;
316 if (node->type != YAML_SEQUENCE_NODE)
317 isl_die(ctx, isl_error_invalid, "expecting sequence",
318 return NULL);
320 scop->n_array = node->data.sequence.items.top
321 - node->data.sequence.items.start;
322 scop->arrays = isl_calloc_array(ctx, struct pet_array *, scop->n_array);
323 if (!scop->arrays)
324 return pet_scop_free(scop);
326 for (item = node->data.sequence.items.start, i = 0;
327 item < node->data.sequence.items.top; ++item, ++i) {
328 yaml_node_t *n;
330 n = yaml_document_get_node(document, *item);
331 scop->arrays[i] = extract_array(ctx, document, n);
332 if (!scop->arrays[i])
333 return pet_scop_free(scop);
336 return scop;
339 static __isl_give pet_expr *extract_expr(isl_ctx *ctx,
340 yaml_document_t *document, yaml_node_t *node);
342 static __isl_give pet_expr *extract_arguments(isl_ctx *ctx,
343 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
345 int i, n;
346 yaml_node_item_t *item;
348 if (node->type != YAML_SEQUENCE_NODE)
349 isl_die(ctx, isl_error_invalid, "expecting sequence",
350 return pet_expr_free(expr));
352 n = node->data.sequence.items.top - node->data.sequence.items.start;
353 expr = pet_expr_set_n_arg(expr, n);
355 for (item = node->data.sequence.items.start, i = 0;
356 item < node->data.sequence.items.top; ++item, ++i) {
357 yaml_node_t *n;
358 pet_expr *arg;
360 n = yaml_document_get_node(document, *item);
361 arg = extract_expr(ctx, document, n);
362 expr = pet_expr_set_arg(expr, i, arg);
365 return expr;
368 /* Extract pet_expr_double specific fields from "node" and
369 * update "expr" accordingly.
371 static __isl_give pet_expr *extract_expr_double(isl_ctx *ctx,
372 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
374 yaml_node_pair_t *pair;
375 double d = 0;
376 char *s = NULL;
378 for (pair = node->data.mapping.pairs.start;
379 pair < node->data.mapping.pairs.top; ++pair) {
380 yaml_node_t *key, *value;
382 key = yaml_document_get_node(document, pair->key);
383 value = yaml_document_get_node(document, pair->value);
385 if (key->type != YAML_SCALAR_NODE)
386 isl_die(ctx, isl_error_invalid, "expecting scalar key",
387 return pet_expr_free(expr));
389 if (!strcmp((char *) key->data.scalar.value, "value"))
390 d = extract_double(ctx, document, value);
391 if (!strcmp((char *) key->data.scalar.value, "string"))
392 s = extract_string(ctx, document, value);
395 expr = pet_expr_double_set(expr, d, s);
396 free(s);
398 return expr;
401 /* Extract pet_expr_access specific fields from "node" and
402 * update "expr" accordingly.
404 * The depth of the access is initialized by pet_expr_access_set_index.
405 * Any explicitly specified depth therefore needs to be set after
406 * setting the index expression. Similiarly, the access relations (if any)
407 * need to be set after setting the depth.
409 static __isl_give pet_expr *extract_expr_access(isl_ctx *ctx,
410 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
412 yaml_node_pair_t *pair;
413 int depth = -1;
414 isl_multi_pw_aff *index = NULL;
416 for (pair = node->data.mapping.pairs.start;
417 pair < node->data.mapping.pairs.top; ++pair) {
418 yaml_node_t *key, *value;
420 key = yaml_document_get_node(document, pair->key);
421 value = yaml_document_get_node(document, pair->value);
423 if (key->type != YAML_SCALAR_NODE)
424 isl_die(ctx, isl_error_invalid, "expecting scalar key",
425 return pet_expr_free(expr));
427 if (!strcmp((char *) key->data.scalar.value, "index"))
428 index = extract_multi_pw_aff(ctx, document, value);
429 if (!strcmp((char *) key->data.scalar.value, "depth"))
430 depth = extract_int(ctx, document, value);
433 expr = pet_expr_access_set_index(expr, index);
434 if (depth >= 0)
435 expr = pet_expr_access_set_depth(expr, depth);
437 for (pair = node->data.mapping.pairs.start;
438 pair < node->data.mapping.pairs.top; ++pair) {
439 yaml_node_t *key, *value;
441 key = yaml_document_get_node(document, pair->key);
442 value = yaml_document_get_node(document, pair->value);
444 if (key->type != YAML_SCALAR_NODE)
445 isl_die(ctx, isl_error_invalid, "expecting scalar key",
446 return pet_expr_free(expr));
448 if (!strcmp((char *) key->data.scalar.value, "may_read"))
449 expr = pet_expr_access_set_access(expr,
450 pet_expr_access_may_read,
451 extract_union_map(ctx, document, value));
452 if (!strcmp((char *) key->data.scalar.value, "may_write"))
453 expr = pet_expr_access_set_access(expr,
454 pet_expr_access_may_write,
455 extract_union_map(ctx, document, value));
456 if (!strcmp((char *) key->data.scalar.value, "must_write"))
457 expr = pet_expr_access_set_access(expr,
458 pet_expr_access_must_write,
459 extract_union_map(ctx, document, value));
460 if (!strcmp((char *) key->data.scalar.value, "killed"))
461 expr = pet_expr_access_set_access(expr,
462 pet_expr_access_killed,
463 extract_union_map(ctx, document, value));
464 if (!strcmp((char *) key->data.scalar.value, "reference"))
465 expr = pet_expr_access_set_ref_id(expr,
466 extract_id(ctx, document, value));
467 if (!strcmp((char *) key->data.scalar.value, "read"))
468 expr = pet_expr_access_set_read(expr,
469 extract_int(ctx, document, value));
470 if (!strcmp((char *) key->data.scalar.value, "write"))
471 expr = pet_expr_access_set_write(expr,
472 extract_int(ctx, document, value));
473 if (!strcmp((char *) key->data.scalar.value, "kill"))
474 expr = pet_expr_access_set_kill(expr,
475 extract_int(ctx, document, value));
478 return expr;
481 /* Extract operation expression specific fields from "node" and
482 * update "expr" accordingly.
484 static __isl_give pet_expr *extract_expr_op(isl_ctx *ctx,
485 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
487 yaml_node_pair_t *pair;
489 for (pair = node->data.mapping.pairs.start;
490 pair < node->data.mapping.pairs.top; ++pair) {
491 yaml_node_t *key, *value;
493 key = yaml_document_get_node(document, pair->key);
494 value = yaml_document_get_node(document, pair->value);
496 if (key->type != YAML_SCALAR_NODE)
497 isl_die(ctx, isl_error_invalid, "expecting scalar key",
498 return pet_expr_free(expr));
500 if (!strcmp((char *) key->data.scalar.value, "operation"))
501 expr = pet_expr_op_set_type(expr,
502 extract_op(ctx, document, value));
505 return expr;
508 /* Extract pet_expr_call specific fields from "node" and
509 * update "expr" accordingly.
511 static __isl_give pet_expr *extract_expr_call(isl_ctx *ctx,
512 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
514 yaml_node_pair_t *pair;
516 for (pair = node->data.mapping.pairs.start;
517 pair < node->data.mapping.pairs.top; ++pair) {
518 yaml_node_t *key, *value;
520 key = yaml_document_get_node(document, pair->key);
521 value = yaml_document_get_node(document, pair->value);
523 if (key->type != YAML_SCALAR_NODE)
524 isl_die(ctx, isl_error_invalid, "expecting scalar key",
525 return pet_expr_free(expr));
527 if (!strcmp((char *) key->data.scalar.value, "name"))
528 expr = pet_expr_call_set_name(expr,
529 extract_string(ctx, document, value));
532 return expr;
535 /* Extract pet_expr_cast specific fields from "node" and
536 * update "expr" accordingly.
538 static __isl_give pet_expr *extract_expr_cast(isl_ctx *ctx,
539 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
541 yaml_node_pair_t *pair;
543 for (pair = node->data.mapping.pairs.start;
544 pair < node->data.mapping.pairs.top; ++pair) {
545 yaml_node_t *key, *value;
547 key = yaml_document_get_node(document, pair->key);
548 value = yaml_document_get_node(document, pair->value);
550 if (key->type != YAML_SCALAR_NODE)
551 isl_die(ctx, isl_error_invalid, "expecting scalar key",
552 return pet_expr_free(expr));
554 if (!strcmp((char *) key->data.scalar.value, "type_name"))
555 expr = pet_expr_cast_set_type_name(expr,
556 extract_string(ctx, document, value));
559 return expr;
562 /* Extract pet_expr_int specific fields from "node" and
563 * update "expr" accordingly.
565 static __isl_give pet_expr *extract_expr_int(isl_ctx *ctx,
566 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
568 yaml_node_pair_t * pair;
570 for (pair = node->data.mapping.pairs.start;
571 pair < node->data.mapping.pairs.top; ++pair) {
572 yaml_node_t *key, *value;
574 key = yaml_document_get_node(document, pair->key);
575 value = yaml_document_get_node(document, pair->value);
577 if (key->type != YAML_SCALAR_NODE)
578 isl_die(ctx, isl_error_invalid, "expecting scalar key",
579 return pet_expr_free(expr));
581 if (!strcmp((char *) key->data.scalar.value, "value"))
582 expr = pet_expr_int_set_val(expr,
583 extract_val(ctx, document, value));
586 return expr;
589 /* Extract a pet_expr from "node".
591 * We first extract the type and arguments of the expression and
592 * then extract additional fields depending on the type.
594 static __isl_give pet_expr *extract_expr(isl_ctx *ctx,
595 yaml_document_t *document, yaml_node_t *node)
597 enum pet_expr_type type = pet_expr_error;
598 pet_expr *expr;
599 yaml_node_pair_t *pair;
601 if (node->type != YAML_MAPPING_NODE)
602 isl_die(ctx, isl_error_invalid, "expecting mapping",
603 return NULL);
605 for (pair = node->data.mapping.pairs.start;
606 pair < node->data.mapping.pairs.top; ++pair) {
607 yaml_node_t *key, *value;
609 key = yaml_document_get_node(document, pair->key);
610 value = yaml_document_get_node(document, pair->value);
612 if (key->type != YAML_SCALAR_NODE)
613 isl_die(ctx, isl_error_invalid, "expecting scalar key",
614 return pet_expr_free(expr));
616 if (!strcmp((char *) key->data.scalar.value, "type"))
617 type = extract_expr_type(ctx, document, value);
620 if (type == pet_expr_error)
621 isl_die(ctx, isl_error_invalid, "cannot determine type",
622 return NULL);
624 expr = pet_expr_alloc(ctx, type);
625 if (!expr)
626 return NULL;
628 for (pair = node->data.mapping.pairs.start;
629 pair < node->data.mapping.pairs.top; ++pair) {
630 yaml_node_t *key, *value;
632 key = yaml_document_get_node(document, pair->key);
633 value = yaml_document_get_node(document, pair->value);
635 if (!strcmp((char *) key->data.scalar.value, "arguments"))
636 expr = extract_arguments(ctx, document, value, expr);
637 if (!expr)
638 return NULL;
641 switch (type) {
642 case pet_expr_error:
643 isl_die(ctx, isl_error_internal, "unreachable code",
644 return NULL);
645 case pet_expr_access:
646 expr = extract_expr_access(ctx, document, node, expr);
647 break;
648 case pet_expr_double:
649 expr = extract_expr_double(ctx, document, node, expr);
650 break;
651 case pet_expr_call:
652 expr = extract_expr_call(ctx, document, node, expr);
653 break;
654 case pet_expr_cast:
655 expr = extract_expr_cast(ctx, document, node, expr);
656 break;
657 case pet_expr_int:
658 expr = extract_expr_int(ctx, document, node, expr);
659 break;
660 case pet_expr_op:
661 expr = extract_expr_op(ctx, document, node, expr);
662 break;
665 return expr;
668 /* Extract a pet_tree_type from "node".
670 static enum pet_tree_type extract_tree_type(isl_ctx *ctx,
671 yaml_document_t *document, yaml_node_t *node)
673 if (node->type != YAML_SCALAR_NODE)
674 isl_die(ctx, isl_error_invalid, "expecting scalar node",
675 return -1);
677 return pet_tree_str_type((char *) node->data.scalar.value);
680 static __isl_give pet_tree *extract_tree(isl_ctx *ctx,
681 yaml_document_t *document, yaml_node_t *node);
683 /* Extract a pet_tree of type pet_tree_block from "node".
685 static __isl_give pet_tree *extract_tree_block(isl_ctx *ctx,
686 yaml_document_t *document, yaml_node_t *node)
688 int block = 0;
689 int i, n;
690 yaml_node_pair_t *pair;
691 yaml_node_item_t *item;
692 yaml_node_t *children = NULL;
693 pet_tree *tree;
695 for (pair = node->data.mapping.pairs.start;
696 pair < node->data.mapping.pairs.top; ++pair) {
697 yaml_node_t *key, *value;
699 key = yaml_document_get_node(document, pair->key);
700 value = yaml_document_get_node(document, pair->value);
702 if (key->type != YAML_SCALAR_NODE)
703 isl_die(ctx, isl_error_invalid, "expecting scalar key",
704 return NULL);
706 if (!strcmp((char *) key->data.scalar.value, "block"))
707 block = extract_int(ctx, document, value);
708 if (!strcmp((char *) key->data.scalar.value, "children"))
709 children = value;
712 if (!children)
713 n = 0;
714 else
715 n = children->data.sequence.items.top -
716 children->data.sequence.items.start;
718 tree = pet_tree_new_block(ctx, block, n);
719 if (!children)
720 return tree;
722 for (item = children->data.sequence.items.start, i = 0;
723 item < children->data.sequence.items.top; ++item, ++i) {
724 yaml_node_t *n;
725 pet_tree *child;
727 n = yaml_document_get_node(document, *item);
728 child = extract_tree(ctx, document, n);
729 tree = pet_tree_block_add_child(tree, child);
732 return tree;
735 /* Extract a pet_tree of type pet_tree_decl from "node".
737 static __isl_give pet_tree *extract_tree_decl(isl_ctx *ctx,
738 yaml_document_t *document, yaml_node_t *node)
740 yaml_node_pair_t *pair;
741 pet_expr *var = NULL;
743 for (pair = node->data.mapping.pairs.start;
744 pair < node->data.mapping.pairs.top; ++pair) {
745 yaml_node_t *key, *value;
747 key = yaml_document_get_node(document, pair->key);
748 value = yaml_document_get_node(document, pair->value);
750 if (key->type != YAML_SCALAR_NODE)
751 isl_die(ctx, isl_error_invalid, "expecting scalar key",
752 return NULL);
754 if (!strcmp((char *) key->data.scalar.value, "variable")) {
755 var = extract_expr(ctx, document, value);
756 if (!var)
757 return NULL;
761 if (!var)
762 isl_die(ctx, isl_error_invalid,
763 "no variable field", return NULL);
765 return pet_tree_new_decl(var);
768 /* Extract a pet_tree of type pet_tree_decl_init from "node".
770 static __isl_give pet_tree *extract_tree_decl_init(isl_ctx *ctx,
771 yaml_document_t *document, yaml_node_t *node)
773 yaml_node_pair_t *pair;
774 pet_expr *var = NULL;
775 pet_expr *init = NULL;
777 for (pair = node->data.mapping.pairs.start;
778 pair < node->data.mapping.pairs.top; ++pair) {
779 yaml_node_t *key, *value;
781 key = yaml_document_get_node(document, pair->key);
782 value = yaml_document_get_node(document, pair->value);
784 if (key->type != YAML_SCALAR_NODE)
785 isl_die(ctx, isl_error_invalid, "expecting scalar key",
786 return NULL);
788 if (!strcmp((char *) key->data.scalar.value, "variable")) {
789 var = extract_expr(ctx, document, value);
790 if (!var)
791 goto error;
793 if (!strcmp((char *) key->data.scalar.value,
794 "initialization")) {
795 init = extract_expr(ctx, document, value);
796 if (!init)
797 goto error;
801 if (!var)
802 isl_die(ctx, isl_error_invalid,
803 "no variable field", goto error);
804 if (!init)
805 isl_die(ctx, isl_error_invalid,
806 "no initialization field", goto error);
808 return pet_tree_new_decl_init(var, init);
809 error:
810 pet_expr_free(var);
811 pet_expr_free(init);
812 return NULL;
815 /* Extract a pet_tree of type pet_tree_expr from "node".
817 static __isl_give pet_tree *extract_tree_expr(isl_ctx *ctx,
818 yaml_document_t *document, yaml_node_t *node)
820 yaml_node_pair_t *pair;
821 pet_expr *expr = NULL;
823 for (pair = node->data.mapping.pairs.start;
824 pair < node->data.mapping.pairs.top; ++pair) {
825 yaml_node_t *key, *value;
827 key = yaml_document_get_node(document, pair->key);
828 value = yaml_document_get_node(document, pair->value);
830 if (key->type != YAML_SCALAR_NODE)
831 isl_die(ctx, isl_error_invalid, "expecting scalar key",
832 return NULL);
834 if (!strcmp((char *) key->data.scalar.value, "expr")) {
835 expr = extract_expr(ctx, document, value);
836 if (!expr)
837 return NULL;
841 if (!expr)
842 isl_die(ctx, isl_error_invalid,
843 "no expr field", return NULL);
845 return pet_tree_new_expr(expr);
848 /* Extract a pet_tree of type pet_tree_while from "node".
850 static __isl_give pet_tree *extract_tree_while(isl_ctx *ctx,
851 yaml_document_t *document, yaml_node_t *node)
853 yaml_node_pair_t *pair;
854 pet_expr *cond = NULL;
855 pet_tree *body = NULL;
857 for (pair = node->data.mapping.pairs.start;
858 pair < node->data.mapping.pairs.top; ++pair) {
859 yaml_node_t *key, *value;
861 key = yaml_document_get_node(document, pair->key);
862 value = yaml_document_get_node(document, pair->value);
864 if (key->type != YAML_SCALAR_NODE)
865 isl_die(ctx, isl_error_invalid, "expecting scalar key",
866 return NULL);
868 if (!strcmp((char *) key->data.scalar.value, "condition")) {
869 cond = extract_expr(ctx, document, value);
870 if (!cond)
871 goto error;
873 if (!strcmp((char *) key->data.scalar.value, "body")) {
874 body = extract_tree(ctx, document, value);
875 if (!body)
876 goto error;
880 if (!cond)
881 isl_die(ctx, isl_error_invalid,
882 "no condition field", goto error);
883 if (!body)
884 isl_die(ctx, isl_error_invalid,
885 "no body field", goto error);
887 return pet_tree_new_while(cond, body);
888 error:
889 pet_expr_free(cond);
890 pet_tree_free(body);
891 return NULL;
894 /* Extract a pet_tree of type pet_tree_infinite_loop from "node".
896 static __isl_give pet_tree *extract_tree_infinite_loop(isl_ctx *ctx,
897 yaml_document_t *document, yaml_node_t *node)
899 yaml_node_pair_t *pair;
900 pet_tree *body;
902 for (pair = node->data.mapping.pairs.start;
903 pair < node->data.mapping.pairs.top; ++pair) {
904 yaml_node_t *key, *value;
906 key = yaml_document_get_node(document, pair->key);
907 value = yaml_document_get_node(document, pair->value);
909 if (key->type != YAML_SCALAR_NODE)
910 isl_die(ctx, isl_error_invalid, "expecting scalar key",
911 return NULL);
913 if (!strcmp((char *) key->data.scalar.value, "body")) {
914 body = extract_tree(ctx, document, value);
915 if (!body)
916 return NULL;
920 if (!body)
921 isl_die(ctx, isl_error_invalid,
922 "no body field", return NULL);
924 return pet_tree_new_infinite_loop(body);
927 /* Extract a pet_tree of type pet_tree_if from "node".
929 static __isl_give pet_tree *extract_tree_if(isl_ctx *ctx,
930 yaml_document_t *document, yaml_node_t *node)
932 yaml_node_pair_t *pair;
933 pet_expr *cond = NULL;
934 pet_tree *then_body = NULL;
936 for (pair = node->data.mapping.pairs.start;
937 pair < node->data.mapping.pairs.top; ++pair) {
938 yaml_node_t *key, *value;
940 key = yaml_document_get_node(document, pair->key);
941 value = yaml_document_get_node(document, pair->value);
943 if (key->type != YAML_SCALAR_NODE)
944 isl_die(ctx, isl_error_invalid, "expecting scalar key",
945 return NULL);
947 if (!strcmp((char *) key->data.scalar.value, "condition")) {
948 cond = extract_expr(ctx, document, value);
949 if (!cond)
950 goto error;
952 if (!strcmp((char *) key->data.scalar.value, "then")) {
953 then_body = extract_tree(ctx, document, value);
954 if (!then_body)
955 goto error;
959 if (!cond)
960 isl_die(ctx, isl_error_invalid,
961 "no condition field", goto error);
962 if (!then_body)
963 isl_die(ctx, isl_error_invalid,
964 "no then body", goto error);
966 return pet_tree_new_if(cond, then_body);
967 error:
968 pet_expr_free(cond);
969 pet_tree_free(then_body);
970 return NULL;
973 /* Extract a pet_tree of type pet_tree_if_else from "node".
975 static __isl_give pet_tree *extract_tree_if_else(isl_ctx *ctx,
976 yaml_document_t *document, yaml_node_t *node)
978 yaml_node_pair_t *pair;
979 pet_expr *cond = NULL;
980 pet_tree *then_body = NULL;
981 pet_tree *else_body = NULL;
983 for (pair = node->data.mapping.pairs.start;
984 pair < node->data.mapping.pairs.top; ++pair) {
985 yaml_node_t *key, *value;
987 key = yaml_document_get_node(document, pair->key);
988 value = yaml_document_get_node(document, pair->value);
990 if (key->type != YAML_SCALAR_NODE)
991 isl_die(ctx, isl_error_invalid, "expecting scalar key",
992 return NULL);
994 if (!strcmp((char *) key->data.scalar.value, "condition")) {
995 cond = extract_expr(ctx, document, value);
996 if (!cond)
997 goto error;
999 if (!strcmp((char *) key->data.scalar.value, "then")) {
1000 then_body = extract_tree(ctx, document, value);
1001 if (!then_body)
1002 goto error;
1004 if (!strcmp((char *) key->data.scalar.value, "else")) {
1005 else_body = extract_tree(ctx, document, value);
1006 if (!else_body)
1007 goto error;
1011 if (!cond)
1012 isl_die(ctx, isl_error_invalid,
1013 "no condition field", goto error);
1014 if (!then_body)
1015 isl_die(ctx, isl_error_invalid,
1016 "no then body", goto error);
1017 if (!else_body)
1018 isl_die(ctx, isl_error_invalid,
1019 "no else body", goto error);
1021 return pet_tree_new_if_else(cond, then_body, else_body);
1022 error:
1023 pet_expr_free(cond);
1024 pet_tree_free(then_body);
1025 pet_tree_free(else_body);
1026 return NULL;
1029 /* Extract a pet_tree of type pet_tree_for from "node".
1031 static __isl_give pet_tree *extract_tree_for(isl_ctx *ctx,
1032 yaml_document_t *document, yaml_node_t *node)
1034 yaml_node_pair_t *pair;
1035 int declared = 0;
1036 int independent = 0;
1037 pet_expr *iv = NULL;
1038 pet_expr *init = NULL;
1039 pet_expr *cond = NULL;
1040 pet_expr *inc = NULL;
1041 pet_tree *body = NULL;
1043 for (pair = node->data.mapping.pairs.start;
1044 pair < node->data.mapping.pairs.top; ++pair) {
1045 yaml_node_t *key, *value;
1047 key = yaml_document_get_node(document, pair->key);
1048 value = yaml_document_get_node(document, pair->value);
1050 if (key->type != YAML_SCALAR_NODE)
1051 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1052 return NULL);
1054 if (!strcmp((char *) key->data.scalar.value, "declared"))
1055 declared = extract_int(ctx, document, value);
1056 if (!strcmp((char *) key->data.scalar.value, "independent"))
1057 independent = extract_int(ctx, document, value);
1058 if (!strcmp((char *) key->data.scalar.value, "variable")) {
1059 iv = extract_expr(ctx, document, value);
1060 if (!iv)
1061 goto error;
1063 if (!strcmp((char *) key->data.scalar.value,
1064 "initialization")) {
1065 init = extract_expr(ctx, document, value);
1066 if (!init)
1067 goto error;
1069 if (!strcmp((char *) key->data.scalar.value, "condition")) {
1070 cond = extract_expr(ctx, document, value);
1071 if (!cond)
1072 goto error;
1074 if (!strcmp((char *) key->data.scalar.value, "increment")) {
1075 inc = extract_expr(ctx, document, value);
1076 if (!inc)
1077 goto error;
1079 if (!strcmp((char *) key->data.scalar.value, "body")) {
1080 body = extract_tree(ctx, document, value);
1081 if (!body)
1082 goto error;
1086 if (!iv)
1087 isl_die(ctx, isl_error_invalid,
1088 "no variable field", goto error);
1089 if (!init)
1090 isl_die(ctx, isl_error_invalid,
1091 "no initialization field", goto error);
1092 if (!cond)
1093 isl_die(ctx, isl_error_invalid,
1094 "no condition field", goto error);
1095 if (!inc)
1096 isl_die(ctx, isl_error_invalid,
1097 "no increment field", goto error);
1098 if (!body)
1099 isl_die(ctx, isl_error_invalid,
1100 "no body field", goto error);
1102 return pet_tree_new_for(independent, declared, iv, init, cond, inc,
1103 body);
1104 error:
1105 pet_expr_free(iv);
1106 pet_expr_free(init);
1107 pet_expr_free(cond);
1108 pet_expr_free(inc);
1109 pet_tree_free(body);
1110 return NULL;
1113 /* Extract a pet_tree from "node".
1115 * We first extract the type of the pet_tree and then call
1116 * the appropriate function to extract and construct a pet_tree
1117 * of that type.
1119 static __isl_give pet_tree *extract_tree(isl_ctx *ctx,
1120 yaml_document_t *document, yaml_node_t *node)
1122 enum pet_tree_type type = pet_tree_error;
1123 pet_tree *tree;
1124 yaml_node_pair_t *pair;
1126 if (node->type != YAML_MAPPING_NODE)
1127 isl_die(ctx, isl_error_invalid, "expecting mapping",
1128 return NULL);
1130 for (pair = node->data.mapping.pairs.start;
1131 pair < node->data.mapping.pairs.top; ++pair) {
1132 yaml_node_t *key, *value;
1134 key = yaml_document_get_node(document, pair->key);
1135 value = yaml_document_get_node(document, pair->value);
1137 if (key->type != YAML_SCALAR_NODE)
1138 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1139 return pet_tree_free(tree));
1141 if (!strcmp((char *) key->data.scalar.value, "type"))
1142 type = extract_tree_type(ctx, document, value);
1145 if (type == pet_tree_error)
1146 isl_die(ctx, isl_error_invalid, "cannot determine type",
1147 return NULL);
1149 switch (type) {
1150 case pet_tree_error:
1151 return NULL;
1152 case pet_tree_block:
1153 tree = extract_tree_block(ctx, document, node);
1154 break;
1155 case pet_tree_break:
1156 tree = pet_tree_new_break(ctx);
1157 break;
1158 case pet_tree_continue:
1159 tree = pet_tree_new_continue(ctx);
1160 break;
1161 case pet_tree_decl:
1162 tree = extract_tree_decl(ctx, document, node);
1163 break;
1164 case pet_tree_decl_init:
1165 tree = extract_tree_decl_init(ctx, document, node);
1166 break;
1167 case pet_tree_expr:
1168 tree = extract_tree_expr(ctx, document, node);
1169 break;
1170 case pet_tree_for:
1171 tree = extract_tree_for(ctx, document, node);
1172 break;
1173 case pet_tree_while:
1174 tree = extract_tree_while(ctx, document, node);
1175 break;
1176 case pet_tree_infinite_loop:
1177 tree = extract_tree_infinite_loop(ctx, document, node);
1178 break;
1179 case pet_tree_if:
1180 tree = extract_tree_if(ctx, document, node);
1181 break;
1182 case pet_tree_if_else:
1183 tree = extract_tree_if_else(ctx, document, node);
1184 break;
1187 return tree;
1190 static struct pet_stmt *extract_stmt_arguments(isl_ctx *ctx,
1191 yaml_document_t *document, yaml_node_t *node, struct pet_stmt *stmt)
1193 int i;
1194 yaml_node_item_t *item;
1196 if (node->type != YAML_SEQUENCE_NODE)
1197 isl_die(ctx, isl_error_invalid, "expecting sequence",
1198 return pet_stmt_free(stmt));
1200 stmt->n_arg = node->data.sequence.items.top
1201 - node->data.sequence.items.start;
1202 stmt->args = isl_calloc_array(ctx, pet_expr *, stmt->n_arg);
1203 if (!stmt->args)
1204 return pet_stmt_free(stmt);
1206 for (item = node->data.sequence.items.start, i = 0;
1207 item < node->data.sequence.items.top; ++item, ++i) {
1208 yaml_node_t *n;
1210 n = yaml_document_get_node(document, *item);
1211 stmt->args[i] = extract_expr(ctx, document, n);
1212 if (!stmt->args[i])
1213 return pet_stmt_free(stmt);
1216 return stmt;
1219 static struct pet_stmt *extract_stmt(isl_ctx *ctx, yaml_document_t *document,
1220 yaml_node_t *node)
1222 struct pet_stmt *stmt;
1223 yaml_node_pair_t * pair;
1224 int line = -1;
1225 unsigned start = 0, end = 0;
1226 char *indent = NULL;
1228 if (node->type != YAML_MAPPING_NODE)
1229 isl_die(ctx, isl_error_invalid, "expecting mapping",
1230 return NULL);
1232 stmt = isl_calloc_type(ctx, struct pet_stmt);
1233 if (!stmt)
1234 return NULL;
1236 stmt->loc = &pet_loc_dummy;
1238 for (pair = node->data.mapping.pairs.start;
1239 pair < node->data.mapping.pairs.top; ++pair) {
1240 yaml_node_t *key, *value;
1242 key = yaml_document_get_node(document, pair->key);
1243 value = yaml_document_get_node(document, pair->value);
1245 if (key->type != YAML_SCALAR_NODE)
1246 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1247 return pet_stmt_free(stmt));
1249 if (!strcmp((char *) key->data.scalar.value, "indent"))
1250 indent = extract_string(ctx, document, value);
1251 if (!strcmp((char *) key->data.scalar.value, "line"))
1252 line = extract_int(ctx, document, value);
1253 if (!strcmp((char *) key->data.scalar.value, "start"))
1254 start = extract_int(ctx, document, value);
1255 if (!strcmp((char *) key->data.scalar.value, "end"))
1256 end = extract_int(ctx, document, value);
1257 if (!strcmp((char *) key->data.scalar.value, "domain"))
1258 stmt->domain = extract_set(ctx, document, value);
1259 if (!strcmp((char *) key->data.scalar.value, "body"))
1260 stmt->body = extract_tree(ctx, document, value);
1262 if (!strcmp((char *) key->data.scalar.value, "arguments"))
1263 stmt = extract_stmt_arguments(ctx, document,
1264 value, stmt);
1265 if (!stmt)
1266 return NULL;
1269 if (!indent)
1270 indent = strdup("");
1271 stmt->loc = pet_loc_alloc(ctx, start, end, line, indent);
1272 if (!stmt->loc)
1273 return pet_stmt_free(stmt);
1275 return stmt;
1278 static struct pet_scop *extract_statements(isl_ctx *ctx,
1279 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1281 int i;
1282 yaml_node_item_t *item;
1284 if (node->type != YAML_SEQUENCE_NODE)
1285 isl_die(ctx, isl_error_invalid, "expecting sequence",
1286 return NULL);
1288 scop->n_stmt = node->data.sequence.items.top
1289 - node->data.sequence.items.start;
1290 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, scop->n_stmt);
1291 if (!scop->stmts)
1292 return pet_scop_free(scop);
1294 for (item = node->data.sequence.items.start, i = 0;
1295 item < node->data.sequence.items.top; ++item, ++i) {
1296 yaml_node_t *n;
1298 n = yaml_document_get_node(document, *item);
1299 scop->stmts[i] = extract_stmt(ctx, document, n);
1300 if (!scop->stmts[i])
1301 return pet_scop_free(scop);
1304 return scop;
1307 /* Extract a pet_implication from "node".
1309 static struct pet_implication *extract_implication(isl_ctx *ctx,
1310 yaml_document_t *document, yaml_node_t *node)
1312 struct pet_implication *implication;
1313 yaml_node_pair_t * pair;
1315 if (node->type != YAML_MAPPING_NODE)
1316 isl_die(ctx, isl_error_invalid, "expecting mapping",
1317 return NULL);
1319 implication = isl_calloc_type(ctx, struct pet_implication);
1320 if (!implication)
1321 return NULL;
1323 for (pair = node->data.mapping.pairs.start;
1324 pair < node->data.mapping.pairs.top; ++pair) {
1325 yaml_node_t *key, *value;
1327 key = yaml_document_get_node(document, pair->key);
1328 value = yaml_document_get_node(document, pair->value);
1330 if (key->type != YAML_SCALAR_NODE)
1331 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1332 return pet_implication_free(implication));
1334 if (!strcmp((char *) key->data.scalar.value, "satisfied"))
1335 implication->satisfied =
1336 extract_int(ctx, document, value);
1337 if (!strcmp((char *) key->data.scalar.value, "extension"))
1338 implication->extension =
1339 extract_map(ctx, document, value);
1342 return implication;
1345 /* Extract a sequence of implications from "node" and
1346 * store them in scop->implications.
1348 static struct pet_scop *extract_implications(isl_ctx *ctx,
1349 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1351 int i;
1352 yaml_node_item_t *item;
1354 if (node->type != YAML_SEQUENCE_NODE)
1355 isl_die(ctx, isl_error_invalid, "expecting sequence",
1356 return NULL);
1358 scop->n_implication = node->data.sequence.items.top
1359 - node->data.sequence.items.start;
1360 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
1361 scop->n_implication);
1362 if (!scop->implications)
1363 return pet_scop_free(scop);
1365 for (item = node->data.sequence.items.start, i = 0;
1366 item < node->data.sequence.items.top; ++item, ++i) {
1367 yaml_node_t *n;
1369 n = yaml_document_get_node(document, *item);
1370 scop->implications[i] = extract_implication(ctx, document, n);
1371 if (!scop->implications[i])
1372 return pet_scop_free(scop);
1375 return scop;
1378 /* Extract a pet_independence from "node".
1380 static struct pet_independence *extract_independence(isl_ctx *ctx,
1381 yaml_document_t *document, yaml_node_t *node)
1383 struct pet_independence *independence;
1384 yaml_node_pair_t * pair;
1386 if (node->type != YAML_MAPPING_NODE)
1387 isl_die(ctx, isl_error_invalid, "expecting mapping",
1388 return NULL);
1390 independence = isl_calloc_type(ctx, struct pet_independence);
1391 if (!independence)
1392 return NULL;
1394 for (pair = node->data.mapping.pairs.start;
1395 pair < node->data.mapping.pairs.top; ++pair) {
1396 yaml_node_t *key, *value;
1398 key = yaml_document_get_node(document, pair->key);
1399 value = yaml_document_get_node(document, pair->value);
1401 if (key->type != YAML_SCALAR_NODE)
1402 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1403 return pet_independence_free(independence));
1405 if (!strcmp((char *) key->data.scalar.value, "filter"))
1406 independence->filter =
1407 extract_union_map(ctx, document, value);
1408 if (!strcmp((char *) key->data.scalar.value, "local"))
1409 independence->local =
1410 extract_union_set(ctx, document, value);
1413 if (!independence->filter)
1414 isl_die(ctx, isl_error_invalid, "no filter field",
1415 return pet_independence_free(independence));
1416 if (!independence->local)
1417 isl_die(ctx, isl_error_invalid, "no local field",
1418 return pet_independence_free(independence));
1420 return independence;
1423 /* Extract a sequence of independences from "node" and
1424 * store them in scop->independences.
1426 static struct pet_scop *extract_independences(isl_ctx *ctx,
1427 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1429 int i;
1430 yaml_node_item_t *item;
1432 if (node->type != YAML_SEQUENCE_NODE)
1433 isl_die(ctx, isl_error_invalid, "expecting sequence",
1434 return NULL);
1436 scop->n_independence = node->data.sequence.items.top
1437 - node->data.sequence.items.start;
1438 scop->independences = isl_calloc_array(ctx, struct pet_independence *,
1439 scop->n_independence);
1440 if (!scop->independences)
1441 return pet_scop_free(scop);
1443 for (item = node->data.sequence.items.start, i = 0;
1444 item < node->data.sequence.items.top; ++item, ++i) {
1445 yaml_node_t *n;
1447 n = yaml_document_get_node(document, *item);
1448 scop->independences[i] = extract_independence(ctx, document, n);
1449 if (!scop->independences[i])
1450 return pet_scop_free(scop);
1453 return scop;
1456 static struct pet_scop *extract_scop(isl_ctx *ctx, yaml_document_t *document,
1457 yaml_node_t *node)
1459 struct pet_scop *scop;
1460 yaml_node_pair_t * pair;
1462 if (!node)
1463 return NULL;
1465 if (node->type != YAML_MAPPING_NODE)
1466 isl_die(ctx, isl_error_invalid, "expecting mapping",
1467 return NULL);
1469 scop = pet_scop_alloc(ctx);
1470 if (!scop)
1471 return NULL;
1473 for (pair = node->data.mapping.pairs.start;
1474 pair < node->data.mapping.pairs.top; ++pair) {
1475 yaml_node_t *key, *value;
1477 key = yaml_document_get_node(document, pair->key);
1478 value = yaml_document_get_node(document, pair->value);
1480 if (key->type != YAML_SCALAR_NODE)
1481 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1482 return pet_scop_free(scop));
1483 if (!strcmp((char *) key->data.scalar.value, "context"))
1484 scop->context = extract_set(ctx, document, value);
1485 if (!strcmp((char *) key->data.scalar.value, "context_value"))
1486 scop->context_value = extract_set(ctx, document, value);
1487 if (!strcmp((char *) key->data.scalar.value, "schedule"))
1488 scop->schedule = extract_schedule(ctx, document, value);
1489 if (!strcmp((char *) key->data.scalar.value, "types"))
1490 scop = extract_types(ctx, document, value, scop);
1491 if (!strcmp((char *) key->data.scalar.value, "arrays"))
1492 scop = extract_arrays(ctx, document, value, scop);
1493 if (!strcmp((char *) key->data.scalar.value, "statements"))
1494 scop = extract_statements(ctx, document, value, scop);
1495 if (!strcmp((char *) key->data.scalar.value, "implications"))
1496 scop = extract_implications(ctx, document, value, scop);
1497 if (!strcmp((char *) key->data.scalar.value, "independences"))
1498 scop = extract_independences(ctx,
1499 document, value, scop);
1500 if (!scop)
1501 return NULL;
1504 if (!scop->context_value) {
1505 isl_space *space = isl_space_params_alloc(ctx, 0);
1506 scop->context_value = isl_set_universe(space);
1507 if (!scop->context_value)
1508 return pet_scop_free(scop);
1511 return scop;
1514 /* Extract a pet_scop from the YAML description in "in".
1516 struct pet_scop *pet_scop_parse(isl_ctx *ctx, FILE *in)
1518 struct pet_scop *scop = NULL;
1519 yaml_parser_t parser;
1520 yaml_node_t *root;
1521 yaml_document_t document = { 0 };
1523 yaml_parser_initialize(&parser);
1525 yaml_parser_set_input_file(&parser, in);
1527 if (!yaml_parser_load(&parser, &document))
1528 goto error;
1530 root = yaml_document_get_root_node(&document);
1532 scop = extract_scop(ctx, &document, root);
1534 yaml_document_delete(&document);
1536 yaml_parser_delete(&parser);
1538 return scop;
1539 error:
1540 yaml_parser_delete(&parser);
1541 pet_scop_free(scop);
1542 return NULL;