extract out (nearly) shared pet_expr_access_from_id
[pet.git] / parse.c
blobadd143cd0c4af0bce708d23d1db457033a360e69
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_tree of type pet_tree_expr from "node".
828 static __isl_give pet_tree *extract_tree_expr(isl_ctx *ctx,
829 yaml_document_t *document, yaml_node_t *node)
831 yaml_node_pair_t *pair;
832 pet_expr *expr = NULL;
834 for (pair = node->data.mapping.pairs.start;
835 pair < node->data.mapping.pairs.top; ++pair) {
836 yaml_node_t *key, *value;
838 key = yaml_document_get_node(document, pair->key);
839 value = yaml_document_get_node(document, pair->value);
841 if (key->type != YAML_SCALAR_NODE)
842 isl_die(ctx, isl_error_invalid, "expecting scalar key",
843 return NULL);
845 if (!strcmp((char *) key->data.scalar.value, "expr")) {
846 expr = extract_expr(ctx, document, value);
847 if (!expr)
848 return NULL;
852 if (!expr)
853 isl_die(ctx, isl_error_invalid,
854 "no expr field", return NULL);
856 return pet_tree_new_expr(expr);
859 /* Extract a pet_tree of type pet_tree_while from "node".
861 static __isl_give pet_tree *extract_tree_while(isl_ctx *ctx,
862 yaml_document_t *document, yaml_node_t *node)
864 yaml_node_pair_t *pair;
865 pet_expr *cond = NULL;
866 pet_tree *body = NULL;
868 for (pair = node->data.mapping.pairs.start;
869 pair < node->data.mapping.pairs.top; ++pair) {
870 yaml_node_t *key, *value;
872 key = yaml_document_get_node(document, pair->key);
873 value = yaml_document_get_node(document, pair->value);
875 if (key->type != YAML_SCALAR_NODE)
876 isl_die(ctx, isl_error_invalid, "expecting scalar key",
877 return NULL);
879 if (!strcmp((char *) key->data.scalar.value, "condition")) {
880 cond = extract_expr(ctx, document, value);
881 if (!cond)
882 goto error;
884 if (!strcmp((char *) key->data.scalar.value, "body")) {
885 body = extract_tree(ctx, document, value);
886 if (!body)
887 goto error;
891 if (!cond)
892 isl_die(ctx, isl_error_invalid,
893 "no condition field", goto error);
894 if (!body)
895 isl_die(ctx, isl_error_invalid,
896 "no body field", goto error);
898 return pet_tree_new_while(cond, body);
899 error:
900 pet_expr_free(cond);
901 pet_tree_free(body);
902 return NULL;
905 /* Extract a pet_tree of type pet_tree_infinite_loop from "node".
907 static __isl_give pet_tree *extract_tree_infinite_loop(isl_ctx *ctx,
908 yaml_document_t *document, yaml_node_t *node)
910 yaml_node_pair_t *pair;
911 pet_tree *body;
913 for (pair = node->data.mapping.pairs.start;
914 pair < node->data.mapping.pairs.top; ++pair) {
915 yaml_node_t *key, *value;
917 key = yaml_document_get_node(document, pair->key);
918 value = yaml_document_get_node(document, pair->value);
920 if (key->type != YAML_SCALAR_NODE)
921 isl_die(ctx, isl_error_invalid, "expecting scalar key",
922 return NULL);
924 if (!strcmp((char *) key->data.scalar.value, "body")) {
925 body = extract_tree(ctx, document, value);
926 if (!body)
927 return NULL;
931 if (!body)
932 isl_die(ctx, isl_error_invalid,
933 "no body field", return NULL);
935 return pet_tree_new_infinite_loop(body);
938 /* Extract a pet_tree of type pet_tree_if from "node".
940 static __isl_give pet_tree *extract_tree_if(isl_ctx *ctx,
941 yaml_document_t *document, yaml_node_t *node)
943 yaml_node_pair_t *pair;
944 pet_expr *cond = NULL;
945 pet_tree *then_body = NULL;
947 for (pair = node->data.mapping.pairs.start;
948 pair < node->data.mapping.pairs.top; ++pair) {
949 yaml_node_t *key, *value;
951 key = yaml_document_get_node(document, pair->key);
952 value = yaml_document_get_node(document, pair->value);
954 if (key->type != YAML_SCALAR_NODE)
955 isl_die(ctx, isl_error_invalid, "expecting scalar key",
956 return NULL);
958 if (!strcmp((char *) key->data.scalar.value, "condition")) {
959 cond = extract_expr(ctx, document, value);
960 if (!cond)
961 goto error;
963 if (!strcmp((char *) key->data.scalar.value, "then")) {
964 then_body = extract_tree(ctx, document, value);
965 if (!then_body)
966 goto error;
970 if (!cond)
971 isl_die(ctx, isl_error_invalid,
972 "no condition field", goto error);
973 if (!then_body)
974 isl_die(ctx, isl_error_invalid,
975 "no then body", goto error);
977 return pet_tree_new_if(cond, then_body);
978 error:
979 pet_expr_free(cond);
980 pet_tree_free(then_body);
981 return NULL;
984 /* Extract a pet_tree of type pet_tree_if_else from "node".
986 static __isl_give pet_tree *extract_tree_if_else(isl_ctx *ctx,
987 yaml_document_t *document, yaml_node_t *node)
989 yaml_node_pair_t *pair;
990 pet_expr *cond = NULL;
991 pet_tree *then_body = NULL;
992 pet_tree *else_body = NULL;
994 for (pair = node->data.mapping.pairs.start;
995 pair < node->data.mapping.pairs.top; ++pair) {
996 yaml_node_t *key, *value;
998 key = yaml_document_get_node(document, pair->key);
999 value = yaml_document_get_node(document, pair->value);
1001 if (key->type != YAML_SCALAR_NODE)
1002 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1003 return NULL);
1005 if (!strcmp((char *) key->data.scalar.value, "condition")) {
1006 cond = extract_expr(ctx, document, value);
1007 if (!cond)
1008 goto error;
1010 if (!strcmp((char *) key->data.scalar.value, "then")) {
1011 then_body = extract_tree(ctx, document, value);
1012 if (!then_body)
1013 goto error;
1015 if (!strcmp((char *) key->data.scalar.value, "else")) {
1016 else_body = extract_tree(ctx, document, value);
1017 if (!else_body)
1018 goto error;
1022 if (!cond)
1023 isl_die(ctx, isl_error_invalid,
1024 "no condition field", goto error);
1025 if (!then_body)
1026 isl_die(ctx, isl_error_invalid,
1027 "no then body", goto error);
1028 if (!else_body)
1029 isl_die(ctx, isl_error_invalid,
1030 "no else body", goto error);
1032 return pet_tree_new_if_else(cond, then_body, else_body);
1033 error:
1034 pet_expr_free(cond);
1035 pet_tree_free(then_body);
1036 pet_tree_free(else_body);
1037 return NULL;
1040 /* Extract a pet_tree of type pet_tree_for from "node".
1042 static __isl_give pet_tree *extract_tree_for(isl_ctx *ctx,
1043 yaml_document_t *document, yaml_node_t *node)
1045 yaml_node_pair_t *pair;
1046 int declared = 0;
1047 int independent = 0;
1048 pet_expr *iv = NULL;
1049 pet_expr *init = NULL;
1050 pet_expr *cond = NULL;
1051 pet_expr *inc = NULL;
1052 pet_tree *body = NULL;
1054 for (pair = node->data.mapping.pairs.start;
1055 pair < node->data.mapping.pairs.top; ++pair) {
1056 yaml_node_t *key, *value;
1058 key = yaml_document_get_node(document, pair->key);
1059 value = yaml_document_get_node(document, pair->value);
1061 if (key->type != YAML_SCALAR_NODE)
1062 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1063 return NULL);
1065 if (!strcmp((char *) key->data.scalar.value, "declared"))
1066 declared = extract_int(ctx, document, value);
1067 if (!strcmp((char *) key->data.scalar.value, "independent"))
1068 independent = extract_int(ctx, document, value);
1069 if (!strcmp((char *) key->data.scalar.value, "variable")) {
1070 iv = extract_expr(ctx, document, value);
1071 if (!iv)
1072 goto error;
1074 if (!strcmp((char *) key->data.scalar.value,
1075 "initialization")) {
1076 init = extract_expr(ctx, document, value);
1077 if (!init)
1078 goto error;
1080 if (!strcmp((char *) key->data.scalar.value, "condition")) {
1081 cond = extract_expr(ctx, document, value);
1082 if (!cond)
1083 goto error;
1085 if (!strcmp((char *) key->data.scalar.value, "increment")) {
1086 inc = extract_expr(ctx, document, value);
1087 if (!inc)
1088 goto error;
1090 if (!strcmp((char *) key->data.scalar.value, "body")) {
1091 body = extract_tree(ctx, document, value);
1092 if (!body)
1093 goto error;
1097 if (!iv)
1098 isl_die(ctx, isl_error_invalid,
1099 "no variable field", goto error);
1100 if (!init)
1101 isl_die(ctx, isl_error_invalid,
1102 "no initialization field", goto error);
1103 if (!cond)
1104 isl_die(ctx, isl_error_invalid,
1105 "no condition field", goto error);
1106 if (!inc)
1107 isl_die(ctx, isl_error_invalid,
1108 "no increment field", goto error);
1109 if (!body)
1110 isl_die(ctx, isl_error_invalid,
1111 "no body field", goto error);
1113 return pet_tree_new_for(independent, declared, iv, init, cond, inc,
1114 body);
1115 error:
1116 pet_expr_free(iv);
1117 pet_expr_free(init);
1118 pet_expr_free(cond);
1119 pet_expr_free(inc);
1120 pet_tree_free(body);
1121 return NULL;
1124 /* Extract a pet_tree from "node".
1126 * We first extract the type of the pet_tree and then call
1127 * the appropriate function to extract and construct a pet_tree
1128 * of that type.
1130 static __isl_give pet_tree *extract_tree(isl_ctx *ctx,
1131 yaml_document_t *document, yaml_node_t *node)
1133 enum pet_tree_type type = pet_tree_error;
1134 pet_tree *tree;
1135 yaml_node_pair_t *pair;
1137 if (node->type != YAML_MAPPING_NODE)
1138 isl_die(ctx, isl_error_invalid, "expecting mapping",
1139 return NULL);
1141 for (pair = node->data.mapping.pairs.start;
1142 pair < node->data.mapping.pairs.top; ++pair) {
1143 yaml_node_t *key, *value;
1145 key = yaml_document_get_node(document, pair->key);
1146 value = yaml_document_get_node(document, pair->value);
1148 if (key->type != YAML_SCALAR_NODE)
1149 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1150 return NULL);
1152 if (!strcmp((char *) key->data.scalar.value, "type"))
1153 type = extract_tree_type(ctx, document, value);
1156 if (type == pet_tree_error)
1157 isl_die(ctx, isl_error_invalid, "cannot determine type",
1158 return NULL);
1160 switch (type) {
1161 case pet_tree_error:
1162 return NULL;
1163 case pet_tree_block:
1164 tree = extract_tree_block(ctx, document, node);
1165 break;
1166 case pet_tree_break:
1167 tree = pet_tree_new_break(ctx);
1168 break;
1169 case pet_tree_continue:
1170 tree = pet_tree_new_continue(ctx);
1171 break;
1172 case pet_tree_decl:
1173 tree = extract_tree_decl(ctx, document, node);
1174 break;
1175 case pet_tree_decl_init:
1176 tree = extract_tree_decl_init(ctx, document, node);
1177 break;
1178 case pet_tree_expr:
1179 tree = extract_tree_expr(ctx, document, node);
1180 break;
1181 case pet_tree_for:
1182 tree = extract_tree_for(ctx, document, node);
1183 break;
1184 case pet_tree_while:
1185 tree = extract_tree_while(ctx, document, node);
1186 break;
1187 case pet_tree_infinite_loop:
1188 tree = extract_tree_infinite_loop(ctx, document, node);
1189 break;
1190 case pet_tree_if:
1191 tree = extract_tree_if(ctx, document, node);
1192 break;
1193 case pet_tree_if_else:
1194 tree = extract_tree_if_else(ctx, document, node);
1195 break;
1198 return tree;
1201 static struct pet_stmt *extract_stmt_arguments(isl_ctx *ctx,
1202 yaml_document_t *document, yaml_node_t *node, struct pet_stmt *stmt)
1204 int i;
1205 yaml_node_item_t *item;
1207 if (node->type != YAML_SEQUENCE_NODE)
1208 isl_die(ctx, isl_error_invalid, "expecting sequence",
1209 return pet_stmt_free(stmt));
1211 stmt->n_arg = node->data.sequence.items.top
1212 - node->data.sequence.items.start;
1213 stmt->args = isl_calloc_array(ctx, pet_expr *, stmt->n_arg);
1214 if (!stmt->args)
1215 return pet_stmt_free(stmt);
1217 for (item = node->data.sequence.items.start, i = 0;
1218 item < node->data.sequence.items.top; ++item, ++i) {
1219 yaml_node_t *n;
1221 n = yaml_document_get_node(document, *item);
1222 stmt->args[i] = extract_expr(ctx, document, n);
1223 if (!stmt->args[i])
1224 return pet_stmt_free(stmt);
1227 return stmt;
1230 static struct pet_stmt *extract_stmt(isl_ctx *ctx, yaml_document_t *document,
1231 yaml_node_t *node)
1233 struct pet_stmt *stmt;
1234 yaml_node_pair_t * pair;
1235 int line = -1;
1236 unsigned start = 0, end = 0;
1237 char *indent = NULL;
1239 if (node->type != YAML_MAPPING_NODE)
1240 isl_die(ctx, isl_error_invalid, "expecting mapping",
1241 return NULL);
1243 stmt = isl_calloc_type(ctx, struct pet_stmt);
1244 if (!stmt)
1245 return NULL;
1247 stmt->loc = &pet_loc_dummy;
1249 for (pair = node->data.mapping.pairs.start;
1250 pair < node->data.mapping.pairs.top; ++pair) {
1251 yaml_node_t *key, *value;
1253 key = yaml_document_get_node(document, pair->key);
1254 value = yaml_document_get_node(document, pair->value);
1256 if (key->type != YAML_SCALAR_NODE)
1257 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1258 return pet_stmt_free(stmt));
1260 if (!strcmp((char *) key->data.scalar.value, "indent"))
1261 indent = extract_string(ctx, document, value);
1262 if (!strcmp((char *) key->data.scalar.value, "line"))
1263 line = extract_int(ctx, document, value);
1264 if (!strcmp((char *) key->data.scalar.value, "start"))
1265 start = extract_int(ctx, document, value);
1266 if (!strcmp((char *) key->data.scalar.value, "end"))
1267 end = extract_int(ctx, document, value);
1268 if (!strcmp((char *) key->data.scalar.value, "domain"))
1269 stmt->domain = extract_set(ctx, document, value);
1270 if (!strcmp((char *) key->data.scalar.value, "body"))
1271 stmt->body = extract_tree(ctx, document, value);
1273 if (!strcmp((char *) key->data.scalar.value, "arguments"))
1274 stmt = extract_stmt_arguments(ctx, document,
1275 value, stmt);
1276 if (!stmt)
1277 return NULL;
1280 if (!indent)
1281 indent = strdup("");
1282 stmt->loc = pet_loc_alloc(ctx, start, end, line, indent);
1283 if (!stmt->loc)
1284 return pet_stmt_free(stmt);
1286 return stmt;
1289 static struct pet_scop *extract_statements(isl_ctx *ctx,
1290 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1292 int i;
1293 yaml_node_item_t *item;
1295 if (node->type != YAML_SEQUENCE_NODE)
1296 isl_die(ctx, isl_error_invalid, "expecting sequence",
1297 return NULL);
1299 scop->n_stmt = node->data.sequence.items.top
1300 - node->data.sequence.items.start;
1301 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, scop->n_stmt);
1302 if (!scop->stmts)
1303 return pet_scop_free(scop);
1305 for (item = node->data.sequence.items.start, i = 0;
1306 item < node->data.sequence.items.top; ++item, ++i) {
1307 yaml_node_t *n;
1309 n = yaml_document_get_node(document, *item);
1310 scop->stmts[i] = extract_stmt(ctx, document, n);
1311 if (!scop->stmts[i])
1312 return pet_scop_free(scop);
1315 return scop;
1318 /* Extract a pet_implication from "node".
1320 static struct pet_implication *extract_implication(isl_ctx *ctx,
1321 yaml_document_t *document, yaml_node_t *node)
1323 struct pet_implication *implication;
1324 yaml_node_pair_t * pair;
1326 if (node->type != YAML_MAPPING_NODE)
1327 isl_die(ctx, isl_error_invalid, "expecting mapping",
1328 return NULL);
1330 implication = isl_calloc_type(ctx, struct pet_implication);
1331 if (!implication)
1332 return NULL;
1334 for (pair = node->data.mapping.pairs.start;
1335 pair < node->data.mapping.pairs.top; ++pair) {
1336 yaml_node_t *key, *value;
1338 key = yaml_document_get_node(document, pair->key);
1339 value = yaml_document_get_node(document, pair->value);
1341 if (key->type != YAML_SCALAR_NODE)
1342 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1343 return pet_implication_free(implication));
1345 if (!strcmp((char *) key->data.scalar.value, "satisfied"))
1346 implication->satisfied =
1347 extract_int(ctx, document, value);
1348 if (!strcmp((char *) key->data.scalar.value, "extension"))
1349 implication->extension =
1350 extract_map(ctx, document, value);
1353 return implication;
1356 /* Extract a sequence of implications from "node" and
1357 * store them in scop->implications.
1359 static struct pet_scop *extract_implications(isl_ctx *ctx,
1360 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1362 int i;
1363 yaml_node_item_t *item;
1365 if (node->type != YAML_SEQUENCE_NODE)
1366 isl_die(ctx, isl_error_invalid, "expecting sequence",
1367 return NULL);
1369 scop->n_implication = node->data.sequence.items.top
1370 - node->data.sequence.items.start;
1371 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
1372 scop->n_implication);
1373 if (!scop->implications)
1374 return pet_scop_free(scop);
1376 for (item = node->data.sequence.items.start, i = 0;
1377 item < node->data.sequence.items.top; ++item, ++i) {
1378 yaml_node_t *n;
1380 n = yaml_document_get_node(document, *item);
1381 scop->implications[i] = extract_implication(ctx, document, n);
1382 if (!scop->implications[i])
1383 return pet_scop_free(scop);
1386 return scop;
1389 /* Extract a pet_independence from "node".
1391 static struct pet_independence *extract_independence(isl_ctx *ctx,
1392 yaml_document_t *document, yaml_node_t *node)
1394 struct pet_independence *independence;
1395 yaml_node_pair_t * pair;
1397 if (node->type != YAML_MAPPING_NODE)
1398 isl_die(ctx, isl_error_invalid, "expecting mapping",
1399 return NULL);
1401 independence = isl_calloc_type(ctx, struct pet_independence);
1402 if (!independence)
1403 return NULL;
1405 for (pair = node->data.mapping.pairs.start;
1406 pair < node->data.mapping.pairs.top; ++pair) {
1407 yaml_node_t *key, *value;
1409 key = yaml_document_get_node(document, pair->key);
1410 value = yaml_document_get_node(document, pair->value);
1412 if (key->type != YAML_SCALAR_NODE)
1413 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1414 return pet_independence_free(independence));
1416 if (!strcmp((char *) key->data.scalar.value, "filter"))
1417 independence->filter =
1418 extract_union_map(ctx, document, value);
1419 if (!strcmp((char *) key->data.scalar.value, "local"))
1420 independence->local =
1421 extract_union_set(ctx, document, value);
1424 if (!independence->filter)
1425 isl_die(ctx, isl_error_invalid, "no filter field",
1426 return pet_independence_free(independence));
1427 if (!independence->local)
1428 isl_die(ctx, isl_error_invalid, "no local field",
1429 return pet_independence_free(independence));
1431 return independence;
1434 /* Extract a sequence of independences from "node" and
1435 * store them in scop->independences.
1437 static struct pet_scop *extract_independences(isl_ctx *ctx,
1438 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
1440 int i;
1441 yaml_node_item_t *item;
1443 if (node->type != YAML_SEQUENCE_NODE)
1444 isl_die(ctx, isl_error_invalid, "expecting sequence",
1445 return NULL);
1447 scop->n_independence = node->data.sequence.items.top
1448 - node->data.sequence.items.start;
1449 scop->independences = isl_calloc_array(ctx, struct pet_independence *,
1450 scop->n_independence);
1451 if (!scop->independences)
1452 return pet_scop_free(scop);
1454 for (item = node->data.sequence.items.start, i = 0;
1455 item < node->data.sequence.items.top; ++item, ++i) {
1456 yaml_node_t *n;
1458 n = yaml_document_get_node(document, *item);
1459 scop->independences[i] = extract_independence(ctx, document, n);
1460 if (!scop->independences[i])
1461 return pet_scop_free(scop);
1464 return scop;
1467 static struct pet_scop *extract_scop(isl_ctx *ctx, yaml_document_t *document,
1468 yaml_node_t *node)
1470 struct pet_scop *scop;
1471 yaml_node_pair_t * pair;
1473 if (!node)
1474 return NULL;
1476 if (node->type != YAML_MAPPING_NODE)
1477 isl_die(ctx, isl_error_invalid, "expecting mapping",
1478 return NULL);
1480 scop = pet_scop_alloc(ctx);
1481 if (!scop)
1482 return NULL;
1484 for (pair = node->data.mapping.pairs.start;
1485 pair < node->data.mapping.pairs.top; ++pair) {
1486 yaml_node_t *key, *value;
1488 key = yaml_document_get_node(document, pair->key);
1489 value = yaml_document_get_node(document, pair->value);
1491 if (key->type != YAML_SCALAR_NODE)
1492 isl_die(ctx, isl_error_invalid, "expecting scalar key",
1493 return pet_scop_free(scop));
1494 if (!strcmp((char *) key->data.scalar.value, "context"))
1495 scop->context = extract_set(ctx, document, value);
1496 if (!strcmp((char *) key->data.scalar.value, "context_value"))
1497 scop->context_value = extract_set(ctx, document, value);
1498 if (!strcmp((char *) key->data.scalar.value, "schedule"))
1499 scop->schedule = extract_schedule(ctx, document, value);
1500 if (!strcmp((char *) key->data.scalar.value, "types"))
1501 scop = extract_types(ctx, document, value, scop);
1502 if (!strcmp((char *) key->data.scalar.value, "arrays"))
1503 scop = extract_arrays(ctx, document, value, scop);
1504 if (!strcmp((char *) key->data.scalar.value, "statements"))
1505 scop = extract_statements(ctx, document, value, scop);
1506 if (!strcmp((char *) key->data.scalar.value, "implications"))
1507 scop = extract_implications(ctx, document, value, scop);
1508 if (!strcmp((char *) key->data.scalar.value, "independences"))
1509 scop = extract_independences(ctx,
1510 document, value, scop);
1511 if (!scop)
1512 return NULL;
1515 if (!scop->context_value) {
1516 isl_space *space = isl_space_params_alloc(ctx, 0);
1517 scop->context_value = isl_set_universe(space);
1518 if (!scop->context_value)
1519 return pet_scop_free(scop);
1522 return scop;
1525 /* Extract a pet_scop from the YAML description in "in".
1527 struct pet_scop *pet_scop_parse(isl_ctx *ctx, FILE *in)
1529 struct pet_scop *scop = NULL;
1530 yaml_parser_t parser;
1531 yaml_node_t *root;
1532 yaml_document_t document = { 0 };
1534 yaml_parser_initialize(&parser);
1536 yaml_parser_set_input_file(&parser, in);
1538 if (!yaml_parser_load(&parser, &document))
1539 goto error;
1541 root = yaml_document_get_root_node(&document);
1543 scop = extract_scop(ctx, &document, root);
1545 yaml_document_delete(&document);
1547 yaml_parser_delete(&parser);
1549 return scop;
1550 error:
1551 yaml_parser_delete(&parser);
1552 pet_scop_free(scop);
1553 return NULL;