link against isl libraries first
[pet.git] / parse.c
blob5388ed3f58a7ae76e2a7c2f81a733273f387ea58
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);
314 if (!strcmp((char *) key->data.scalar.value, "outer"))
315 array->outer = extract_int(ctx, document, value);
318 return array;
321 static struct pet_scop *extract_arrays(isl_ctx *ctx, yaml_document_t *document,
322 yaml_node_t *node, struct pet_scop *scop)
324 int i;
325 yaml_node_item_t *item;
327 if (node->type != YAML_SEQUENCE_NODE)
328 isl_die(ctx, isl_error_invalid, "expecting sequence",
329 return NULL);
331 scop->n_array = node->data.sequence.items.top
332 - node->data.sequence.items.start;
333 scop->arrays = isl_calloc_array(ctx, struct pet_array *, scop->n_array);
334 if (!scop->arrays)
335 return pet_scop_free(scop);
337 for (item = node->data.sequence.items.start, i = 0;
338 item < node->data.sequence.items.top; ++item, ++i) {
339 yaml_node_t *n;
341 n = yaml_document_get_node(document, *item);
342 scop->arrays[i] = extract_array(ctx, document, n);
343 if (!scop->arrays[i])
344 return pet_scop_free(scop);
347 return scop;
350 static __isl_give pet_expr *extract_expr(isl_ctx *ctx,
351 yaml_document_t *document, yaml_node_t *node);
353 static __isl_give pet_expr *extract_arguments(isl_ctx *ctx,
354 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
356 int i, n;
357 yaml_node_item_t *item;
359 if (node->type != YAML_SEQUENCE_NODE)
360 isl_die(ctx, isl_error_invalid, "expecting sequence",
361 return pet_expr_free(expr));
363 n = node->data.sequence.items.top - node->data.sequence.items.start;
364 expr = pet_expr_set_n_arg(expr, n);
366 for (item = node->data.sequence.items.start, i = 0;
367 item < node->data.sequence.items.top; ++item, ++i) {
368 yaml_node_t *n;
369 pet_expr *arg;
371 n = yaml_document_get_node(document, *item);
372 arg = extract_expr(ctx, document, n);
373 expr = pet_expr_set_arg(expr, i, arg);
376 return expr;
379 /* Extract pet_expr_double specific fields from "node" and
380 * update "expr" accordingly.
382 static __isl_give pet_expr *extract_expr_double(isl_ctx *ctx,
383 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
385 yaml_node_pair_t *pair;
386 double d = 0;
387 char *s = NULL;
389 for (pair = node->data.mapping.pairs.start;
390 pair < node->data.mapping.pairs.top; ++pair) {
391 yaml_node_t *key, *value;
393 key = yaml_document_get_node(document, pair->key);
394 value = yaml_document_get_node(document, pair->value);
396 if (key->type != YAML_SCALAR_NODE)
397 isl_die(ctx, isl_error_invalid, "expecting scalar key",
398 return pet_expr_free(expr));
400 if (!strcmp((char *) key->data.scalar.value, "value"))
401 d = extract_double(ctx, document, value);
402 if (!strcmp((char *) key->data.scalar.value, "string"))
403 s = extract_string(ctx, document, value);
406 expr = pet_expr_double_set(expr, d, s);
407 free(s);
409 return expr;
412 /* Extract pet_expr_access specific fields from "node" and
413 * update "expr" accordingly.
415 * The depth of the access is initialized by pet_expr_access_set_index.
416 * Any explicitly specified depth therefore needs to be set after
417 * setting the index expression. Similiarly, the access relations (if any)
418 * need to be set after setting the depth.
420 static __isl_give pet_expr *extract_expr_access(isl_ctx *ctx,
421 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
423 yaml_node_pair_t *pair;
424 int depth = -1;
425 isl_multi_pw_aff *index = NULL;
427 for (pair = node->data.mapping.pairs.start;
428 pair < node->data.mapping.pairs.top; ++pair) {
429 yaml_node_t *key, *value;
431 key = yaml_document_get_node(document, pair->key);
432 value = yaml_document_get_node(document, pair->value);
434 if (key->type != YAML_SCALAR_NODE)
435 isl_die(ctx, isl_error_invalid, "expecting scalar key",
436 return pet_expr_free(expr));
438 if (!strcmp((char *) key->data.scalar.value, "index"))
439 index = extract_multi_pw_aff(ctx, document, value);
440 if (!strcmp((char *) key->data.scalar.value, "depth"))
441 depth = extract_int(ctx, document, value);
444 expr = pet_expr_access_set_index(expr, index);
445 if (depth >= 0)
446 expr = pet_expr_access_set_depth(expr, depth);
448 for (pair = node->data.mapping.pairs.start;
449 pair < node->data.mapping.pairs.top; ++pair) {
450 yaml_node_t *key, *value;
452 key = yaml_document_get_node(document, pair->key);
453 value = yaml_document_get_node(document, pair->value);
455 if (key->type != YAML_SCALAR_NODE)
456 isl_die(ctx, isl_error_invalid, "expecting scalar key",
457 return pet_expr_free(expr));
459 if (!strcmp((char *) key->data.scalar.value, "may_read"))
460 expr = pet_expr_access_set_access(expr,
461 pet_expr_access_may_read,
462 extract_union_map(ctx, document, value));
463 if (!strcmp((char *) key->data.scalar.value, "may_write"))
464 expr = pet_expr_access_set_access(expr,
465 pet_expr_access_may_write,
466 extract_union_map(ctx, document, value));
467 if (!strcmp((char *) key->data.scalar.value, "must_write"))
468 expr = pet_expr_access_set_access(expr,
469 pet_expr_access_must_write,
470 extract_union_map(ctx, document, value));
471 if (!strcmp((char *) key->data.scalar.value, "killed"))
472 expr = pet_expr_access_set_access(expr,
473 pet_expr_access_killed,
474 extract_union_map(ctx, document, value));
475 if (!strcmp((char *) key->data.scalar.value, "reference"))
476 expr = pet_expr_access_set_ref_id(expr,
477 extract_id(ctx, document, value));
478 if (!strcmp((char *) key->data.scalar.value, "read"))
479 expr = pet_expr_access_set_read(expr,
480 extract_int(ctx, document, value));
481 if (!strcmp((char *) key->data.scalar.value, "write"))
482 expr = pet_expr_access_set_write(expr,
483 extract_int(ctx, document, value));
484 if (!strcmp((char *) key->data.scalar.value, "kill"))
485 expr = pet_expr_access_set_kill(expr,
486 extract_int(ctx, document, value));
489 return expr;
492 /* Extract operation expression specific fields from "node" and
493 * update "expr" accordingly.
495 static __isl_give pet_expr *extract_expr_op(isl_ctx *ctx,
496 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
498 yaml_node_pair_t *pair;
500 for (pair = node->data.mapping.pairs.start;
501 pair < node->data.mapping.pairs.top; ++pair) {
502 yaml_node_t *key, *value;
504 key = yaml_document_get_node(document, pair->key);
505 value = yaml_document_get_node(document, pair->value);
507 if (key->type != YAML_SCALAR_NODE)
508 isl_die(ctx, isl_error_invalid, "expecting scalar key",
509 return pet_expr_free(expr));
511 if (!strcmp((char *) key->data.scalar.value, "operation"))
512 expr = pet_expr_op_set_type(expr,
513 extract_op(ctx, document, value));
516 return expr;
519 /* Extract pet_expr_call specific fields from "node" and
520 * update "expr" accordingly.
522 static __isl_give pet_expr *extract_expr_call(isl_ctx *ctx,
523 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
525 yaml_node_pair_t *pair;
527 for (pair = node->data.mapping.pairs.start;
528 pair < node->data.mapping.pairs.top; ++pair) {
529 yaml_node_t *key, *value;
531 key = yaml_document_get_node(document, pair->key);
532 value = yaml_document_get_node(document, pair->value);
534 if (key->type != YAML_SCALAR_NODE)
535 isl_die(ctx, isl_error_invalid, "expecting scalar key",
536 return pet_expr_free(expr));
538 if (!strcmp((char *) key->data.scalar.value, "name"))
539 expr = pet_expr_call_set_name(expr,
540 extract_string(ctx, document, value));
543 return expr;
546 /* Extract pet_expr_cast specific fields from "node" and
547 * update "expr" accordingly.
549 static __isl_give pet_expr *extract_expr_cast(isl_ctx *ctx,
550 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
552 yaml_node_pair_t *pair;
554 for (pair = node->data.mapping.pairs.start;
555 pair < node->data.mapping.pairs.top; ++pair) {
556 yaml_node_t *key, *value;
558 key = yaml_document_get_node(document, pair->key);
559 value = yaml_document_get_node(document, pair->value);
561 if (key->type != YAML_SCALAR_NODE)
562 isl_die(ctx, isl_error_invalid, "expecting scalar key",
563 return pet_expr_free(expr));
565 if (!strcmp((char *) key->data.scalar.value, "type_name"))
566 expr = pet_expr_cast_set_type_name(expr,
567 extract_string(ctx, document, value));
570 return expr;
573 /* Extract pet_expr_int specific fields from "node" and
574 * update "expr" accordingly.
576 static __isl_give pet_expr *extract_expr_int(isl_ctx *ctx,
577 yaml_document_t *document, yaml_node_t *node, __isl_take pet_expr *expr)
579 yaml_node_pair_t * pair;
581 for (pair = node->data.mapping.pairs.start;
582 pair < node->data.mapping.pairs.top; ++pair) {
583 yaml_node_t *key, *value;
585 key = yaml_document_get_node(document, pair->key);
586 value = yaml_document_get_node(document, pair->value);
588 if (key->type != YAML_SCALAR_NODE)
589 isl_die(ctx, isl_error_invalid, "expecting scalar key",
590 return pet_expr_free(expr));
592 if (!strcmp((char *) key->data.scalar.value, "value"))
593 expr = pet_expr_int_set_val(expr,
594 extract_val(ctx, document, value));
597 return expr;
600 /* Extract a pet_expr from "node".
602 * We first extract the type and arguments of the expression and
603 * then extract additional fields depending on the type.
605 static __isl_give pet_expr *extract_expr(isl_ctx *ctx,
606 yaml_document_t *document, yaml_node_t *node)
608 enum pet_expr_type type = pet_expr_error;
609 pet_expr *expr;
610 yaml_node_pair_t *pair;
612 if (node->type != YAML_MAPPING_NODE)
613 isl_die(ctx, isl_error_invalid, "expecting mapping",
614 return NULL);
616 for (pair = node->data.mapping.pairs.start;
617 pair < node->data.mapping.pairs.top; ++pair) {
618 yaml_node_t *key, *value;
620 key = yaml_document_get_node(document, pair->key);
621 value = yaml_document_get_node(document, pair->value);
623 if (key->type != YAML_SCALAR_NODE)
624 isl_die(ctx, isl_error_invalid, "expecting scalar key",
625 return NULL);
627 if (!strcmp((char *) key->data.scalar.value, "type"))
628 type = extract_expr_type(ctx, document, value);
631 if (type == pet_expr_error)
632 isl_die(ctx, isl_error_invalid, "cannot determine type",
633 return NULL);
635 expr = pet_expr_alloc(ctx, type);
636 if (!expr)
637 return NULL;
639 for (pair = node->data.mapping.pairs.start;
640 pair < node->data.mapping.pairs.top; ++pair) {
641 yaml_node_t *key, *value;
643 key = yaml_document_get_node(document, pair->key);
644 value = yaml_document_get_node(document, pair->value);
646 if (!strcmp((char *) key->data.scalar.value, "arguments"))
647 expr = extract_arguments(ctx, document, value, expr);
648 if (!expr)
649 return NULL;
652 switch (type) {
653 case pet_expr_error:
654 isl_die(ctx, isl_error_internal, "unreachable code",
655 return NULL);
656 case pet_expr_access:
657 expr = extract_expr_access(ctx, document, node, expr);
658 break;
659 case pet_expr_double:
660 expr = extract_expr_double(ctx, document, node, expr);
661 break;
662 case pet_expr_call:
663 expr = extract_expr_call(ctx, document, node, expr);
664 break;
665 case pet_expr_cast:
666 expr = extract_expr_cast(ctx, document, node, expr);
667 break;
668 case pet_expr_int:
669 expr = extract_expr_int(ctx, document, node, expr);
670 break;
671 case pet_expr_op:
672 expr = extract_expr_op(ctx, document, node, expr);
673 break;
676 return expr;
679 /* Extract a pet_tree_type from "node".
681 static enum pet_tree_type extract_tree_type(isl_ctx *ctx,
682 yaml_document_t *document, yaml_node_t *node)
684 if (node->type != YAML_SCALAR_NODE)
685 isl_die(ctx, isl_error_invalid, "expecting scalar node",
686 return -1);
688 return pet_tree_str_type((char *) node->data.scalar.value);
691 static __isl_give pet_tree *extract_tree(isl_ctx *ctx,
692 yaml_document_t *document, yaml_node_t *node);
694 /* Extract a pet_tree of type pet_tree_block from "node".
696 static __isl_give pet_tree *extract_tree_block(isl_ctx *ctx,
697 yaml_document_t *document, yaml_node_t *node)
699 int block = 0;
700 int i, n;
701 yaml_node_pair_t *pair;
702 yaml_node_item_t *item;
703 yaml_node_t *children = NULL;
704 pet_tree *tree;
706 for (pair = node->data.mapping.pairs.start;
707 pair < node->data.mapping.pairs.top; ++pair) {
708 yaml_node_t *key, *value;
710 key = yaml_document_get_node(document, pair->key);
711 value = yaml_document_get_node(document, pair->value);
713 if (key->type != YAML_SCALAR_NODE)
714 isl_die(ctx, isl_error_invalid, "expecting scalar key",
715 return NULL);
717 if (!strcmp((char *) key->data.scalar.value, "block"))
718 block = extract_int(ctx, document, value);
719 if (!strcmp((char *) key->data.scalar.value, "children"))
720 children = value;
723 if (!children)
724 n = 0;
725 else
726 n = children->data.sequence.items.top -
727 children->data.sequence.items.start;
729 tree = pet_tree_new_block(ctx, block, n);
730 if (!children)
731 return tree;
733 for (item = children->data.sequence.items.start, i = 0;
734 item < children->data.sequence.items.top; ++item, ++i) {
735 yaml_node_t *n;
736 pet_tree *child;
738 n = yaml_document_get_node(document, *item);
739 child = extract_tree(ctx, document, n);
740 tree = pet_tree_block_add_child(tree, child);
743 return tree;
746 /* Extract a pet_tree of type pet_tree_decl from "node".
748 static __isl_give pet_tree *extract_tree_decl(isl_ctx *ctx,
749 yaml_document_t *document, yaml_node_t *node)
751 yaml_node_pair_t *pair;
752 pet_expr *var = NULL;
754 for (pair = node->data.mapping.pairs.start;
755 pair < node->data.mapping.pairs.top; ++pair) {
756 yaml_node_t *key, *value;
758 key = yaml_document_get_node(document, pair->key);
759 value = yaml_document_get_node(document, pair->value);
761 if (key->type != YAML_SCALAR_NODE)
762 isl_die(ctx, isl_error_invalid, "expecting scalar key",
763 return NULL);
765 if (!strcmp((char *) key->data.scalar.value, "variable")) {
766 var = extract_expr(ctx, document, value);
767 if (!var)
768 return NULL;
772 if (!var)
773 isl_die(ctx, isl_error_invalid,
774 "no variable field", return NULL);
776 return pet_tree_new_decl(var);
779 /* Extract a pet_tree of type pet_tree_decl_init from "node".
781 static __isl_give pet_tree *extract_tree_decl_init(isl_ctx *ctx,
782 yaml_document_t *document, yaml_node_t *node)
784 yaml_node_pair_t *pair;
785 pet_expr *var = NULL;
786 pet_expr *init = NULL;
788 for (pair = node->data.mapping.pairs.start;
789 pair < node->data.mapping.pairs.top; ++pair) {
790 yaml_node_t *key, *value;
792 key = yaml_document_get_node(document, pair->key);
793 value = yaml_document_get_node(document, pair->value);
795 if (key->type != YAML_SCALAR_NODE)
796 isl_die(ctx, isl_error_invalid, "expecting scalar key",
797 return NULL);
799 if (!strcmp((char *) key->data.scalar.value, "variable")) {
800 var = extract_expr(ctx, document, value);
801 if (!var)
802 goto error;
804 if (!strcmp((char *) key->data.scalar.value,
805 "initialization")) {
806 init = extract_expr(ctx, document, value);
807 if (!init)
808 goto error;
812 if (!var)
813 isl_die(ctx, isl_error_invalid,
814 "no variable field", goto error);
815 if (!init)
816 isl_die(ctx, isl_error_invalid,
817 "no initialization field", goto error);
819 return pet_tree_new_decl_init(var, init);
820 error:
821 pet_expr_free(var);
822 pet_expr_free(init);
823 return NULL;
826 /* Extract a pet_expr corresponding to a pet_tree with a single "expr" field
827 * from "node".
829 static __isl_give pet_expr *extract_expr_field(isl_ctx *ctx,
830 yaml_document_t *document, yaml_node_t *node)
832 yaml_node_pair_t *pair;
833 pet_expr *expr = NULL;
835 for (pair = node->data.mapping.pairs.start;
836 pair < node->data.mapping.pairs.top; ++pair) {
837 yaml_node_t *key, *value;
839 key = yaml_document_get_node(document, pair->key);
840 value = yaml_document_get_node(document, pair->value);
842 if (key->type != YAML_SCALAR_NODE)
843 isl_die(ctx, isl_error_invalid, "expecting scalar key",
844 return NULL);
846 if (!strcmp((char *) key->data.scalar.value, "expr")) {
847 expr = extract_expr(ctx, document, value);
848 if (!expr)
849 return NULL;
853 if (!expr)
854 isl_die(ctx, isl_error_invalid,
855 "no expr field", return NULL);
857 return expr;
860 /* Extract a pet_tree of type pet_tree_expr from "node".
862 static __isl_give pet_tree *extract_tree_expr(isl_ctx *ctx,
863 yaml_document_t *document, yaml_node_t *node)
865 return pet_tree_new_expr(extract_expr_field(ctx, document, node));
868 /* Extract a pet_tree of type pet_tree_return from "node".
870 static __isl_give pet_tree *extract_tree_return(isl_ctx *ctx,
871 yaml_document_t *document, yaml_node_t *node)
873 return pet_tree_new_return(extract_expr_field(ctx, document, node));
876 /* Extract a pet_tree of type pet_tree_while from "node".
878 static __isl_give pet_tree *extract_tree_while(isl_ctx *ctx,
879 yaml_document_t *document, yaml_node_t *node)
881 yaml_node_pair_t *pair;
882 pet_expr *cond = NULL;
883 pet_tree *body = NULL;
885 for (pair = node->data.mapping.pairs.start;
886 pair < node->data.mapping.pairs.top; ++pair) {
887 yaml_node_t *key, *value;
889 key = yaml_document_get_node(document, pair->key);
890 value = yaml_document_get_node(document, pair->value);
892 if (key->type != YAML_SCALAR_NODE)
893 isl_die(ctx, isl_error_invalid, "expecting scalar key",
894 return NULL);
896 if (!strcmp((char *) key->data.scalar.value, "condition")) {
897 cond = extract_expr(ctx, document, value);
898 if (!cond)
899 goto error;
901 if (!strcmp((char *) key->data.scalar.value, "body")) {
902 body = extract_tree(ctx, document, value);
903 if (!body)
904 goto error;
908 if (!cond)
909 isl_die(ctx, isl_error_invalid,
910 "no condition field", goto error);
911 if (!body)
912 isl_die(ctx, isl_error_invalid,
913 "no body field", goto error);
915 return pet_tree_new_while(cond, body);
916 error:
917 pet_expr_free(cond);
918 pet_tree_free(body);
919 return NULL;
922 /* Extract a pet_tree of type pet_tree_infinite_loop from "node".
924 static __isl_give pet_tree *extract_tree_infinite_loop(isl_ctx *ctx,
925 yaml_document_t *document, yaml_node_t *node)
927 yaml_node_pair_t *pair;
928 pet_tree *body;
930 for (pair = node->data.mapping.pairs.start;
931 pair < node->data.mapping.pairs.top; ++pair) {
932 yaml_node_t *key, *value;
934 key = yaml_document_get_node(document, pair->key);
935 value = yaml_document_get_node(document, pair->value);
937 if (key->type != YAML_SCALAR_NODE)
938 isl_die(ctx, isl_error_invalid, "expecting scalar key",
939 return NULL);
941 if (!strcmp((char *) key->data.scalar.value, "body")) {
942 body = extract_tree(ctx, document, value);
943 if (!body)
944 return NULL;
948 if (!body)
949 isl_die(ctx, isl_error_invalid,
950 "no body field", return NULL);
952 return pet_tree_new_infinite_loop(body);
955 /* Extract a pet_tree of type pet_tree_if from "node".
957 static __isl_give pet_tree *extract_tree_if(isl_ctx *ctx,
958 yaml_document_t *document, yaml_node_t *node)
960 yaml_node_pair_t *pair;
961 pet_expr *cond = NULL;
962 pet_tree *then_body = NULL;
964 for (pair = node->data.mapping.pairs.start;
965 pair < node->data.mapping.pairs.top; ++pair) {
966 yaml_node_t *key, *value;
968 key = yaml_document_get_node(document, pair->key);
969 value = yaml_document_get_node(document, pair->value);
971 if (key->type != YAML_SCALAR_NODE)
972 isl_die(ctx, isl_error_invalid, "expecting scalar key",
973 return NULL);
975 if (!strcmp((char *) key->data.scalar.value, "condition")) {
976 cond = extract_expr(ctx, document, value);
977 if (!cond)
978 goto error;
980 if (!strcmp((char *) key->data.scalar.value, "then")) {
981 then_body = extract_tree(ctx, document, value);
982 if (!then_body)
983 goto error;
987 if (!cond)
988 isl_die(ctx, isl_error_invalid,
989 "no condition field", goto error);
990 if (!then_body)
991 isl_die(ctx, isl_error_invalid,
992 "no then body", goto error);
994 return pet_tree_new_if(cond, then_body);
995 error:
996 pet_expr_free(cond);
997 pet_tree_free(then_body);
998 return NULL;
1001 /* Extract a pet_tree of type pet_tree_if_else from "node".
1003 static __isl_give pet_tree *extract_tree_if_else(isl_ctx *ctx,
1004 yaml_document_t *document, yaml_node_t *node)
1006 yaml_node_pair_t *pair;
1007 pet_expr *cond = NULL;
1008 pet_tree *then_body = NULL;
1009 pet_tree *else_body = NULL;
1011 for (pair = node->data.mapping.pairs.start;
1012 pair < node->data.mapping.pairs.top; ++pair) {
1013 yaml_node_t *key, *value;
1015 key = yaml_document_get_node(document, pair->key);
1016 value = yaml_document_get_node(document, pair->value);
1018 if (key->type != YAML_SCALAR_NODE)
1019 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1020 return NULL);
1022 if (!strcmp((char *) key->data.scalar.value, "condition")) {
1023 cond = extract_expr(ctx, document, value);
1024 if (!cond)
1025 goto error;
1027 if (!strcmp((char *) key->data.scalar.value, "then")) {
1028 then_body = extract_tree(ctx, document, value);
1029 if (!then_body)
1030 goto error;
1032 if (!strcmp((char *) key->data.scalar.value, "else")) {
1033 else_body = extract_tree(ctx, document, value);
1034 if (!else_body)
1035 goto error;
1039 if (!cond)
1040 isl_die(ctx, isl_error_invalid,
1041 "no condition field", goto error);
1042 if (!then_body)
1043 isl_die(ctx, isl_error_invalid,
1044 "no then body", goto error);
1045 if (!else_body)
1046 isl_die(ctx, isl_error_invalid,
1047 "no else body", goto error);
1049 return pet_tree_new_if_else(cond, then_body, else_body);
1050 error:
1051 pet_expr_free(cond);
1052 pet_tree_free(then_body);
1053 pet_tree_free(else_body);
1054 return NULL;
1057 /* Extract a pet_tree of type pet_tree_for from "node".
1059 static __isl_give pet_tree *extract_tree_for(isl_ctx *ctx,
1060 yaml_document_t *document, yaml_node_t *node)
1062 yaml_node_pair_t *pair;
1063 int declared = 0;
1064 int independent = 0;
1065 pet_expr *iv = NULL;
1066 pet_expr *init = NULL;
1067 pet_expr *cond = NULL;
1068 pet_expr *inc = NULL;
1069 pet_tree *body = NULL;
1071 for (pair = node->data.mapping.pairs.start;
1072 pair < node->data.mapping.pairs.top; ++pair) {
1073 yaml_node_t *key, *value;
1075 key = yaml_document_get_node(document, pair->key);
1076 value = yaml_document_get_node(document, pair->value);
1078 if (key->type != YAML_SCALAR_NODE)
1079 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1080 return NULL);
1082 if (!strcmp((char *) key->data.scalar.value, "declared"))
1083 declared = extract_int(ctx, document, value);
1084 if (!strcmp((char *) key->data.scalar.value, "independent"))
1085 independent = extract_int(ctx, document, value);
1086 if (!strcmp((char *) key->data.scalar.value, "variable")) {
1087 iv = extract_expr(ctx, document, value);
1088 if (!iv)
1089 goto error;
1091 if (!strcmp((char *) key->data.scalar.value,
1092 "initialization")) {
1093 init = extract_expr(ctx, document, value);
1094 if (!init)
1095 goto error;
1097 if (!strcmp((char *) key->data.scalar.value, "condition")) {
1098 cond = extract_expr(ctx, document, value);
1099 if (!cond)
1100 goto error;
1102 if (!strcmp((char *) key->data.scalar.value, "increment")) {
1103 inc = extract_expr(ctx, document, value);
1104 if (!inc)
1105 goto error;
1107 if (!strcmp((char *) key->data.scalar.value, "body")) {
1108 body = extract_tree(ctx, document, value);
1109 if (!body)
1110 goto error;
1114 if (!iv)
1115 isl_die(ctx, isl_error_invalid,
1116 "no variable field", goto error);
1117 if (!init)
1118 isl_die(ctx, isl_error_invalid,
1119 "no initialization field", goto error);
1120 if (!cond)
1121 isl_die(ctx, isl_error_invalid,
1122 "no condition field", goto error);
1123 if (!inc)
1124 isl_die(ctx, isl_error_invalid,
1125 "no increment field", goto error);
1126 if (!body)
1127 isl_die(ctx, isl_error_invalid,
1128 "no body field", goto error);
1130 return pet_tree_new_for(independent, declared, iv, init, cond, inc,
1131 body);
1132 error:
1133 pet_expr_free(iv);
1134 pet_expr_free(init);
1135 pet_expr_free(cond);
1136 pet_expr_free(inc);
1137 pet_tree_free(body);
1138 return NULL;
1141 /* Extract a pet_tree from "node".
1143 * We first extract the type of the pet_tree and then call
1144 * the appropriate function to extract and construct a pet_tree
1145 * of that type.
1147 static __isl_give pet_tree *extract_tree(isl_ctx *ctx,
1148 yaml_document_t *document, yaml_node_t *node)
1150 enum pet_tree_type type = pet_tree_error;
1151 pet_tree *tree;
1152 yaml_node_pair_t *pair;
1154 if (node->type != YAML_MAPPING_NODE)
1155 isl_die(ctx, isl_error_invalid, "expecting mapping",
1156 return NULL);
1158 for (pair = node->data.mapping.pairs.start;
1159 pair < node->data.mapping.pairs.top; ++pair) {
1160 yaml_node_t *key, *value;
1162 key = yaml_document_get_node(document, pair->key);
1163 value = yaml_document_get_node(document, pair->value);
1165 if (key->type != YAML_SCALAR_NODE)
1166 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1167 return NULL);
1169 if (!strcmp((char *) key->data.scalar.value, "type"))
1170 type = extract_tree_type(ctx, document, value);
1173 if (type == pet_tree_error)
1174 isl_die(ctx, isl_error_invalid, "cannot determine type",
1175 return NULL);
1177 switch (type) {
1178 case pet_tree_error:
1179 return NULL;
1180 case pet_tree_block:
1181 tree = extract_tree_block(ctx, document, node);
1182 break;
1183 case pet_tree_break:
1184 tree = pet_tree_new_break(ctx);
1185 break;
1186 case pet_tree_continue:
1187 tree = pet_tree_new_continue(ctx);
1188 break;
1189 case pet_tree_decl:
1190 tree = extract_tree_decl(ctx, document, node);
1191 break;
1192 case pet_tree_decl_init:
1193 tree = extract_tree_decl_init(ctx, document, node);
1194 break;
1195 case pet_tree_expr:
1196 tree = extract_tree_expr(ctx, document, node);
1197 break;
1198 case pet_tree_return:
1199 tree = extract_tree_return(ctx, document, node);
1200 break;
1201 case pet_tree_for:
1202 tree = extract_tree_for(ctx, document, node);
1203 break;
1204 case pet_tree_while:
1205 tree = extract_tree_while(ctx, document, node);
1206 break;
1207 case pet_tree_infinite_loop:
1208 tree = extract_tree_infinite_loop(ctx, document, node);
1209 break;
1210 case pet_tree_if:
1211 tree = extract_tree_if(ctx, document, node);
1212 break;
1213 case pet_tree_if_else:
1214 tree = extract_tree_if_else(ctx, document, node);
1215 break;
1218 return tree;
1221 static struct pet_stmt *extract_stmt_arguments(isl_ctx *ctx,
1222 yaml_document_t *document, yaml_node_t *node, struct pet_stmt *stmt)
1224 int i;
1225 yaml_node_item_t *item;
1227 if (node->type != YAML_SEQUENCE_NODE)
1228 isl_die(ctx, isl_error_invalid, "expecting sequence",
1229 return pet_stmt_free(stmt));
1231 stmt->n_arg = node->data.sequence.items.top
1232 - node->data.sequence.items.start;
1233 stmt->args = isl_calloc_array(ctx, pet_expr *, stmt->n_arg);
1234 if (!stmt->args)
1235 return pet_stmt_free(stmt);
1237 for (item = node->data.sequence.items.start, i = 0;
1238 item < node->data.sequence.items.top; ++item, ++i) {
1239 yaml_node_t *n;
1241 n = yaml_document_get_node(document, *item);
1242 stmt->args[i] = extract_expr(ctx, document, n);
1243 if (!stmt->args[i])
1244 return pet_stmt_free(stmt);
1247 return stmt;
1250 static struct pet_stmt *extract_stmt(isl_ctx *ctx, yaml_document_t *document,
1251 yaml_node_t *node)
1253 struct pet_stmt *stmt;
1254 yaml_node_pair_t * pair;
1255 int line = -1;
1256 unsigned start = 0, end = 0;
1257 char *indent = NULL;
1259 if (node->type != YAML_MAPPING_NODE)
1260 isl_die(ctx, isl_error_invalid, "expecting mapping",
1261 return NULL);
1263 stmt = isl_calloc_type(ctx, struct pet_stmt);
1264 if (!stmt)
1265 return NULL;
1267 stmt->loc = &pet_loc_dummy;
1269 for (pair = node->data.mapping.pairs.start;
1270 pair < node->data.mapping.pairs.top; ++pair) {
1271 yaml_node_t *key, *value;
1273 key = yaml_document_get_node(document, pair->key);
1274 value = yaml_document_get_node(document, pair->value);
1276 if (key->type != YAML_SCALAR_NODE)
1277 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1278 return pet_stmt_free(stmt));
1280 if (!strcmp((char *) key->data.scalar.value, "indent"))
1281 indent = extract_string(ctx, document, value);
1282 if (!strcmp((char *) key->data.scalar.value, "line"))
1283 line = extract_int(ctx, document, value);
1284 if (!strcmp((char *) key->data.scalar.value, "start"))
1285 start = extract_int(ctx, document, value);
1286 if (!strcmp((char *) key->data.scalar.value, "end"))
1287 end = extract_int(ctx, document, value);
1288 if (!strcmp((char *) key->data.scalar.value, "domain"))
1289 stmt->domain = extract_set(ctx, document, value);
1290 if (!strcmp((char *) key->data.scalar.value, "body"))
1291 stmt->body = extract_tree(ctx, document, value);
1293 if (!strcmp((char *) key->data.scalar.value, "arguments"))
1294 stmt = extract_stmt_arguments(ctx, document,
1295 value, stmt);
1296 if (!stmt)
1297 return NULL;
1300 if (!indent)
1301 indent = strdup("");
1302 stmt->loc = pet_loc_alloc(ctx, start, end, line, indent);
1303 if (!stmt->loc)
1304 return pet_stmt_free(stmt);
1306 return stmt;
1309 static struct pet_scop *extract_statements(isl_ctx *ctx,
1310 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1312 int i;
1313 yaml_node_item_t *item;
1315 if (node->type != YAML_SEQUENCE_NODE)
1316 isl_die(ctx, isl_error_invalid, "expecting sequence",
1317 return NULL);
1319 scop->n_stmt = node->data.sequence.items.top
1320 - node->data.sequence.items.start;
1321 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, scop->n_stmt);
1322 if (!scop->stmts)
1323 return pet_scop_free(scop);
1325 for (item = node->data.sequence.items.start, i = 0;
1326 item < node->data.sequence.items.top; ++item, ++i) {
1327 yaml_node_t *n;
1329 n = yaml_document_get_node(document, *item);
1330 scop->stmts[i] = extract_stmt(ctx, document, n);
1331 if (!scop->stmts[i])
1332 return pet_scop_free(scop);
1335 return scop;
1338 /* Extract a pet_implication from "node".
1340 static struct pet_implication *extract_implication(isl_ctx *ctx,
1341 yaml_document_t *document, yaml_node_t *node)
1343 struct pet_implication *implication;
1344 yaml_node_pair_t * pair;
1346 if (node->type != YAML_MAPPING_NODE)
1347 isl_die(ctx, isl_error_invalid, "expecting mapping",
1348 return NULL);
1350 implication = isl_calloc_type(ctx, struct pet_implication);
1351 if (!implication)
1352 return NULL;
1354 for (pair = node->data.mapping.pairs.start;
1355 pair < node->data.mapping.pairs.top; ++pair) {
1356 yaml_node_t *key, *value;
1358 key = yaml_document_get_node(document, pair->key);
1359 value = yaml_document_get_node(document, pair->value);
1361 if (key->type != YAML_SCALAR_NODE)
1362 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1363 return pet_implication_free(implication));
1365 if (!strcmp((char *) key->data.scalar.value, "satisfied"))
1366 implication->satisfied =
1367 extract_int(ctx, document, value);
1368 if (!strcmp((char *) key->data.scalar.value, "extension"))
1369 implication->extension =
1370 extract_map(ctx, document, value);
1373 return implication;
1376 /* Extract a sequence of implications from "node" and
1377 * store them in scop->implications.
1379 static struct pet_scop *extract_implications(isl_ctx *ctx,
1380 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1382 int i;
1383 yaml_node_item_t *item;
1385 if (node->type != YAML_SEQUENCE_NODE)
1386 isl_die(ctx, isl_error_invalid, "expecting sequence",
1387 return NULL);
1389 scop->n_implication = node->data.sequence.items.top
1390 - node->data.sequence.items.start;
1391 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
1392 scop->n_implication);
1393 if (!scop->implications)
1394 return pet_scop_free(scop);
1396 for (item = node->data.sequence.items.start, i = 0;
1397 item < node->data.sequence.items.top; ++item, ++i) {
1398 yaml_node_t *n;
1400 n = yaml_document_get_node(document, *item);
1401 scop->implications[i] = extract_implication(ctx, document, n);
1402 if (!scop->implications[i])
1403 return pet_scop_free(scop);
1406 return scop;
1409 /* Extract a pet_independence from "node".
1411 static struct pet_independence *extract_independence(isl_ctx *ctx,
1412 yaml_document_t *document, yaml_node_t *node)
1414 struct pet_independence *independence;
1415 yaml_node_pair_t * pair;
1417 if (node->type != YAML_MAPPING_NODE)
1418 isl_die(ctx, isl_error_invalid, "expecting mapping",
1419 return NULL);
1421 independence = isl_calloc_type(ctx, struct pet_independence);
1422 if (!independence)
1423 return NULL;
1425 for (pair = node->data.mapping.pairs.start;
1426 pair < node->data.mapping.pairs.top; ++pair) {
1427 yaml_node_t *key, *value;
1429 key = yaml_document_get_node(document, pair->key);
1430 value = yaml_document_get_node(document, pair->value);
1432 if (key->type != YAML_SCALAR_NODE)
1433 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1434 return pet_independence_free(independence));
1436 if (!strcmp((char *) key->data.scalar.value, "filter"))
1437 independence->filter =
1438 extract_union_map(ctx, document, value);
1439 if (!strcmp((char *) key->data.scalar.value, "local"))
1440 independence->local =
1441 extract_union_set(ctx, document, value);
1444 if (!independence->filter)
1445 isl_die(ctx, isl_error_invalid, "no filter field",
1446 return pet_independence_free(independence));
1447 if (!independence->local)
1448 isl_die(ctx, isl_error_invalid, "no local field",
1449 return pet_independence_free(independence));
1451 return independence;
1454 /* Extract a sequence of independences from "node" and
1455 * store them in scop->independences.
1457 static struct pet_scop *extract_independences(isl_ctx *ctx,
1458 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1460 int i;
1461 yaml_node_item_t *item;
1463 if (node->type != YAML_SEQUENCE_NODE)
1464 isl_die(ctx, isl_error_invalid, "expecting sequence",
1465 return NULL);
1467 scop->n_independence = node->data.sequence.items.top
1468 - node->data.sequence.items.start;
1469 scop->independences = isl_calloc_array(ctx, struct pet_independence *,
1470 scop->n_independence);
1471 if (!scop->independences)
1472 return pet_scop_free(scop);
1474 for (item = node->data.sequence.items.start, i = 0;
1475 item < node->data.sequence.items.top; ++item, ++i) {
1476 yaml_node_t *n;
1478 n = yaml_document_get_node(document, *item);
1479 scop->independences[i] = extract_independence(ctx, document, n);
1480 if (!scop->independences[i])
1481 return pet_scop_free(scop);
1484 return scop;
1487 static struct pet_scop *extract_scop(isl_ctx *ctx, yaml_document_t *document,
1488 yaml_node_t *node)
1490 struct pet_scop *scop;
1491 yaml_node_pair_t * pair;
1493 if (!node)
1494 return NULL;
1496 if (node->type != YAML_MAPPING_NODE)
1497 isl_die(ctx, isl_error_invalid, "expecting mapping",
1498 return NULL);
1500 scop = pet_scop_alloc(ctx);
1501 if (!scop)
1502 return NULL;
1504 for (pair = node->data.mapping.pairs.start;
1505 pair < node->data.mapping.pairs.top; ++pair) {
1506 yaml_node_t *key, *value;
1508 key = yaml_document_get_node(document, pair->key);
1509 value = yaml_document_get_node(document, pair->value);
1511 if (key->type != YAML_SCALAR_NODE)
1512 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1513 return pet_scop_free(scop));
1514 if (!strcmp((char *) key->data.scalar.value, "context"))
1515 scop->context = extract_set(ctx, document, value);
1516 if (!strcmp((char *) key->data.scalar.value, "context_value"))
1517 scop->context_value = extract_set(ctx, document, value);
1518 if (!strcmp((char *) key->data.scalar.value, "schedule"))
1519 scop->schedule = extract_schedule(ctx, document, value);
1520 if (!strcmp((char *) key->data.scalar.value, "types"))
1521 scop = extract_types(ctx, document, value, scop);
1522 if (!strcmp((char *) key->data.scalar.value, "arrays"))
1523 scop = extract_arrays(ctx, document, value, scop);
1524 if (!strcmp((char *) key->data.scalar.value, "statements"))
1525 scop = extract_statements(ctx, document, value, scop);
1526 if (!strcmp((char *) key->data.scalar.value, "implications"))
1527 scop = extract_implications(ctx, document, value, scop);
1528 if (!strcmp((char *) key->data.scalar.value, "independences"))
1529 scop = extract_independences(ctx,
1530 document, value, scop);
1531 if (!scop)
1532 return NULL;
1535 if (!scop->context_value) {
1536 isl_space *space = isl_space_params_alloc(ctx, 0);
1537 scop->context_value = isl_set_universe(space);
1538 if (!scop->context_value)
1539 return pet_scop_free(scop);
1542 return scop;
1545 /* Extract a pet_scop from the YAML description in "in".
1547 struct pet_scop *pet_scop_parse(isl_ctx *ctx, FILE *in)
1549 struct pet_scop *scop = NULL;
1550 yaml_parser_t parser;
1551 yaml_node_t *root;
1552 yaml_document_t document = { 0 };
1554 yaml_parser_initialize(&parser);
1556 yaml_parser_set_input_file(&parser, in);
1558 if (!yaml_parser_load(&parser, &document))
1559 goto error;
1561 root = yaml_document_get_root_node(&document);
1563 scop = extract_scop(ctx, &document, root);
1565 yaml_document_delete(&document);
1567 yaml_parser_delete(&parser);
1569 return scop;
1570 error:
1571 yaml_parser_delete(&parser);
1572 pet_scop_free(scop);
1573 return NULL;