add pet_scop_collect_tagged_{may,must}_{reads,writes}
[pet.git] / parse.c
blob6b8cd508e99e16099e339479ef4f9b89388d61a1
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * The views and conclusions contained in the software and documentation
29 * are those of the authors and should not be interpreted as
30 * representing official policies, either expressed or implied, of
31 * Leiden University.
32 */
34 #include <stdlib.h>
35 #include <yaml.h>
37 #include "scop.h"
38 #include "scop_yaml.h"
40 static char *extract_string(isl_ctx *ctx, yaml_document_t *document,
41 yaml_node_t *node)
43 if (node->type != YAML_SCALAR_NODE)
44 isl_die(ctx, isl_error_invalid, "expecting scalar node",
45 return NULL);
47 return strdup((char *) node->data.scalar.value);
50 static int extract_int(isl_ctx *ctx, yaml_document_t *document,
51 yaml_node_t *node)
53 if (node->type != YAML_SCALAR_NODE)
54 isl_die(ctx, isl_error_invalid, "expecting scalar node",
55 return -1);
57 return atoi((char *) node->data.scalar.value);
60 static double extract_double(isl_ctx *ctx, yaml_document_t *document,
61 yaml_node_t *node)
63 if (node->type != YAML_SCALAR_NODE)
64 isl_die(ctx, isl_error_invalid, "expecting scalar node",
65 return -1);
67 return strtod((char *) node->data.scalar.value, NULL);
70 static enum pet_expr_type extract_expr_type(isl_ctx *ctx,
71 yaml_document_t *document, yaml_node_t *node)
73 if (node->type != YAML_SCALAR_NODE)
74 isl_die(ctx, isl_error_invalid, "expecting scalar node",
75 return -1);
77 return pet_str_type((char *) node->data.scalar.value);
80 static enum pet_op_type extract_op(isl_ctx *ctx, yaml_document_t *document,
81 yaml_node_t *node)
83 if (node->type != YAML_SCALAR_NODE)
84 isl_die(ctx, isl_error_invalid, "expecting scalar node",
85 return -1);
87 return pet_str_op((char *) node->data.scalar.value);
90 static __isl_give isl_set *extract_set(isl_ctx *ctx, yaml_document_t *document,
91 yaml_node_t *node)
93 if (node->type != YAML_SCALAR_NODE)
94 isl_die(ctx, isl_error_invalid, "expecting scalar node",
95 return NULL);
97 return isl_set_read_from_str(ctx, (char *) node->data.scalar.value);
100 static __isl_give isl_id *extract_id(isl_ctx *ctx, yaml_document_t *document,
101 yaml_node_t *node)
103 if (node->type != YAML_SCALAR_NODE)
104 isl_die(ctx, isl_error_invalid, "expecting scalar node",
105 return NULL);
107 return isl_id_alloc(ctx, (char *) node->data.scalar.value, NULL);
110 static __isl_give isl_map *extract_map(isl_ctx *ctx, yaml_document_t *document,
111 yaml_node_t *node)
113 if (node->type != YAML_SCALAR_NODE)
114 isl_die(ctx, isl_error_invalid, "expecting scalar node",
115 return NULL);
117 return isl_map_read_from_str(ctx, (char *) node->data.scalar.value);
120 /* Extract an isl_multi_pw_aff from "node".
122 static __isl_give isl_multi_pw_aff *extract_multi_pw_aff(isl_ctx *ctx,
123 yaml_document_t *document, yaml_node_t *node)
125 if (node->type != YAML_SCALAR_NODE)
126 isl_die(ctx, isl_error_invalid, "expecting scalar node",
127 return NULL);
129 return isl_multi_pw_aff_read_from_str(ctx,
130 (char *) node->data.scalar.value);
133 /* Extract a pet_type from "node".
135 static struct pet_type *extract_type(isl_ctx *ctx,
136 yaml_document_t *document, yaml_node_t *node)
138 struct pet_type *type;
139 yaml_node_pair_t * pair;
141 if (node->type != YAML_MAPPING_NODE)
142 isl_die(ctx, isl_error_invalid, "expecting mapping",
143 return NULL);
145 type = isl_calloc_type(ctx, struct pet_type);
146 if (!type)
147 return NULL;
149 for (pair = node->data.mapping.pairs.start;
150 pair < node->data.mapping.pairs.top; ++pair) {
151 yaml_node_t *key, *value;
153 key = yaml_document_get_node(document, pair->key);
154 value = yaml_document_get_node(document, pair->value);
156 if (key->type != YAML_SCALAR_NODE)
157 isl_die(ctx, isl_error_invalid, "expecting scalar key",
158 return pet_type_free(type));
160 if (!strcmp((char *) key->data.scalar.value, "name"))
161 type->name = extract_string(ctx, document, value);
162 if (!strcmp((char *) key->data.scalar.value, "definition"))
163 type->definition = extract_string(ctx, document, value);
166 return type;
169 /* Extract a sequence of types from "node" and store them in scop->types.
171 static struct pet_scop *extract_types(isl_ctx *ctx,
172 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
174 int i;
175 yaml_node_item_t *item;
177 if (node->type != YAML_SEQUENCE_NODE)
178 isl_die(ctx, isl_error_invalid, "expecting sequence",
179 return NULL);
181 scop->n_type = node->data.sequence.items.top
182 - node->data.sequence.items.start;
183 scop->types = isl_calloc_array(ctx, struct pet_type *, scop->n_type);
184 if (!scop->types)
185 return pet_scop_free(scop);
187 for (item = node->data.sequence.items.start, i = 0;
188 item < node->data.sequence.items.top; ++item, ++i) {
189 yaml_node_t *n;
191 n = yaml_document_get_node(document, *item);
192 scop->types[i] = extract_type(ctx, document, n);
193 if (!scop->types[i])
194 return pet_scop_free(scop);
197 return scop;
200 static struct pet_array *extract_array(isl_ctx *ctx, yaml_document_t *document,
201 yaml_node_t *node)
203 struct pet_array *array;
204 yaml_node_pair_t * pair;
206 if (node->type != YAML_MAPPING_NODE)
207 isl_die(ctx, isl_error_invalid, "expecting mapping",
208 return NULL);
210 array = isl_calloc_type(ctx, struct pet_array);
211 if (!array)
212 return NULL;
214 for (pair = node->data.mapping.pairs.start;
215 pair < node->data.mapping.pairs.top; ++pair) {
216 yaml_node_t *key, *value;
218 key = yaml_document_get_node(document, pair->key);
219 value = yaml_document_get_node(document, pair->value);
221 if (key->type != YAML_SCALAR_NODE)
222 isl_die(ctx, isl_error_invalid, "expecting scalar key",
223 return pet_array_free(array));
225 if (!strcmp((char *) key->data.scalar.value, "context"))
226 array->context = extract_set(ctx, document, value);
227 if (!strcmp((char *) key->data.scalar.value, "extent"))
228 array->extent = extract_set(ctx, document, value);
229 if (!strcmp((char *) key->data.scalar.value, "value_bounds"))
230 array->value_bounds = extract_set(ctx, document, value);
231 if (!strcmp((char *) key->data.scalar.value, "element_type"))
232 array->element_type =
233 extract_string(ctx, document, value);
234 if (!strcmp((char *) key->data.scalar.value, "element_size"))
235 array->element_size = extract_int(ctx, document, value);
236 if (!strcmp((char *) key->data.scalar.value, "live_out"))
237 array->live_out = extract_int(ctx, document, value);
238 if (!strcmp((char *) key->data.scalar.value,
239 "uniquely_defined"))
240 array->uniquely_defined =
241 extract_int(ctx, document, value);
242 if (!strcmp((char *) key->data.scalar.value, "declared"))
243 array->declared = extract_int(ctx, document, value);
244 if (!strcmp((char *) key->data.scalar.value, "exposed"))
245 array->exposed = extract_int(ctx, document, value);
248 return array;
251 static struct pet_scop *extract_arrays(isl_ctx *ctx, yaml_document_t *document,
252 yaml_node_t *node, struct pet_scop *scop)
254 int i;
255 yaml_node_item_t *item;
257 if (node->type != YAML_SEQUENCE_NODE)
258 isl_die(ctx, isl_error_invalid, "expecting sequence",
259 return NULL);
261 scop->n_array = node->data.sequence.items.top
262 - node->data.sequence.items.start;
263 scop->arrays = isl_calloc_array(ctx, struct pet_array *, scop->n_array);
264 if (!scop->arrays)
265 return pet_scop_free(scop);
267 for (item = node->data.sequence.items.start, i = 0;
268 item < node->data.sequence.items.top; ++item, ++i) {
269 yaml_node_t *n;
271 n = yaml_document_get_node(document, *item);
272 scop->arrays[i] = extract_array(ctx, document, n);
273 if (!scop->arrays[i])
274 return pet_scop_free(scop);
277 return scop;
280 static struct pet_expr *extract_expr(isl_ctx *ctx, yaml_document_t *document,
281 yaml_node_t *node);
283 static struct pet_expr *extract_arguments(isl_ctx *ctx,
284 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
286 int i;
287 yaml_node_item_t *item;
289 if (node->type != YAML_SEQUENCE_NODE)
290 isl_die(ctx, isl_error_invalid, "expecting sequence",
291 return pet_expr_free(expr));
293 expr->n_arg = node->data.sequence.items.top
294 - node->data.sequence.items.start;
295 expr->args = isl_calloc_array(ctx, struct pet_expr *, expr->n_arg);
296 if (!expr->args)
297 return pet_expr_free(expr);
299 for (item = node->data.sequence.items.start, i = 0;
300 item < node->data.sequence.items.top; ++item, ++i) {
301 yaml_node_t *n;
303 n = yaml_document_get_node(document, *item);
304 expr->args[i] = extract_expr(ctx, document, n);
305 if (!expr->args[i])
306 return pet_expr_free(expr);
309 return expr;
312 /* Extract pet_expr_double specific fields from "node" and
313 * update "expr" accordingly.
315 static struct pet_expr *extract_expr_double(isl_ctx *ctx,
316 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
318 yaml_node_pair_t *pair;
320 for (pair = node->data.mapping.pairs.start;
321 pair < node->data.mapping.pairs.top; ++pair) {
322 yaml_node_t *key, *value;
324 key = yaml_document_get_node(document, pair->key);
325 value = yaml_document_get_node(document, pair->value);
327 if (key->type != YAML_SCALAR_NODE)
328 isl_die(ctx, isl_error_invalid, "expecting scalar key",
329 return pet_expr_free(expr));
331 if (!strcmp((char *) key->data.scalar.value, "value"))
332 expr->d.val = extract_double(ctx, document, value);
333 if (!strcmp((char *) key->data.scalar.value, "string"))
334 expr->d.s = extract_string(ctx, document, value);
337 return expr;
340 /* Extract pet_expr_access specific fields from "node" and
341 * update "expr" accordingly.
343 static struct pet_expr *extract_expr_access(isl_ctx *ctx,
344 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
346 yaml_node_pair_t *pair;
348 for (pair = node->data.mapping.pairs.start;
349 pair < node->data.mapping.pairs.top; ++pair) {
350 yaml_node_t *key, *value;
352 key = yaml_document_get_node(document, pair->key);
353 value = yaml_document_get_node(document, pair->value);
355 if (key->type != YAML_SCALAR_NODE)
356 isl_die(ctx, isl_error_invalid, "expecting scalar key",
357 return pet_expr_free(expr));
359 if (!strcmp((char *) key->data.scalar.value, "relation"))
360 expr->acc.access = extract_map(ctx, document, value);
361 if (!strcmp((char *) key->data.scalar.value, "index"))
362 expr->acc.index = extract_multi_pw_aff(ctx, document,
363 value);
364 if (!strcmp((char *) key->data.scalar.value, "reference"))
365 expr->acc.ref_id = extract_id(ctx, document, value);
366 if (!strcmp((char *) key->data.scalar.value, "read"))
367 expr->acc.read = extract_int(ctx, document, value);
368 if (!strcmp((char *) key->data.scalar.value, "write"))
369 expr->acc.write = extract_int(ctx, document, value);
372 return expr;
375 /* Extract operation expression specific fields from "node" and
376 * update "expr" accordingly.
378 static struct pet_expr *extract_expr_op(isl_ctx *ctx,
379 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
381 yaml_node_pair_t *pair;
383 for (pair = node->data.mapping.pairs.start;
384 pair < node->data.mapping.pairs.top; ++pair) {
385 yaml_node_t *key, *value;
387 key = yaml_document_get_node(document, pair->key);
388 value = yaml_document_get_node(document, pair->value);
390 if (key->type != YAML_SCALAR_NODE)
391 isl_die(ctx, isl_error_invalid, "expecting scalar key",
392 return pet_expr_free(expr));
394 if (!strcmp((char *) key->data.scalar.value, "operation"))
395 expr->op = extract_op(ctx, document, value);
398 return expr;
401 /* Extract pet_expr_call specific fields from "node" and
402 * update "expr" accordingly.
404 static struct pet_expr *extract_expr_call(isl_ctx *ctx,
405 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
407 yaml_node_pair_t *pair;
409 for (pair = node->data.mapping.pairs.start;
410 pair < node->data.mapping.pairs.top; ++pair) {
411 yaml_node_t *key, *value;
413 key = yaml_document_get_node(document, pair->key);
414 value = yaml_document_get_node(document, pair->value);
416 if (key->type != YAML_SCALAR_NODE)
417 isl_die(ctx, isl_error_invalid, "expecting scalar key",
418 return pet_expr_free(expr));
420 if (!strcmp((char *) key->data.scalar.value, "name"))
421 expr->name = extract_string(ctx, document, value);
424 return expr;
427 /* Extract pet_expr_cast specific fields from "node" and
428 * update "expr" accordingly.
430 static struct pet_expr *extract_expr_cast(isl_ctx *ctx,
431 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
433 yaml_node_pair_t *pair;
435 for (pair = node->data.mapping.pairs.start;
436 pair < node->data.mapping.pairs.top; ++pair) {
437 yaml_node_t *key, *value;
439 key = yaml_document_get_node(document, pair->key);
440 value = yaml_document_get_node(document, pair->value);
442 if (key->type != YAML_SCALAR_NODE)
443 isl_die(ctx, isl_error_invalid, "expecting scalar key",
444 return pet_expr_free(expr));
446 if (!strcmp((char *) key->data.scalar.value, "type_name"))
447 expr->type_name = extract_string(ctx, document, value);
450 return expr;
453 /* Extract a pet_expr from "node".
455 * We first extract the type and arguments of the expression and
456 * then extract additional fields depending on the type.
458 static struct pet_expr *extract_expr(isl_ctx *ctx, yaml_document_t *document,
459 yaml_node_t *node)
461 struct pet_expr *expr;
462 yaml_node_pair_t *pair;
464 if (node->type != YAML_MAPPING_NODE)
465 isl_die(ctx, isl_error_invalid, "expecting mapping",
466 return NULL);
468 expr = isl_calloc_type(ctx, struct pet_expr);
469 if (!expr)
470 return NULL;
472 for (pair = node->data.mapping.pairs.start;
473 pair < node->data.mapping.pairs.top; ++pair) {
474 yaml_node_t *key, *value;
476 key = yaml_document_get_node(document, pair->key);
477 value = yaml_document_get_node(document, pair->value);
479 if (key->type != YAML_SCALAR_NODE)
480 isl_die(ctx, isl_error_invalid, "expecting scalar key",
481 return pet_expr_free(expr));
483 if (!strcmp((char *) key->data.scalar.value, "type"))
484 expr->type = extract_expr_type(ctx, document, value);
486 if (!strcmp((char *) key->data.scalar.value, "arguments"))
487 expr = extract_arguments(ctx, document, value, expr);
488 if (!expr)
489 return NULL;
492 switch (expr->type) {
493 case pet_expr_access:
494 expr = extract_expr_access(ctx, document, node, expr);
495 break;
496 case pet_expr_double:
497 expr = extract_expr_double(ctx, document, node, expr);
498 break;
499 case pet_expr_call:
500 expr = extract_expr_call(ctx, document, node, expr);
501 break;
502 case pet_expr_cast:
503 expr = extract_expr_cast(ctx, document, node, expr);
504 break;
505 case pet_expr_unary:
506 case pet_expr_binary:
507 case pet_expr_ternary:
508 expr = extract_expr_op(ctx, document, node, expr);
509 break;
512 return expr;
515 static struct pet_stmt *extract_stmt_arguments(isl_ctx *ctx,
516 yaml_document_t *document, yaml_node_t *node, struct pet_stmt *stmt)
518 int i;
519 yaml_node_item_t *item;
521 if (node->type != YAML_SEQUENCE_NODE)
522 isl_die(ctx, isl_error_invalid, "expecting sequence",
523 return pet_stmt_free(stmt));
525 stmt->n_arg = node->data.sequence.items.top
526 - node->data.sequence.items.start;
527 stmt->args = isl_calloc_array(ctx, struct pet_expr *, stmt->n_arg);
528 if (!stmt->args)
529 return pet_stmt_free(stmt);
531 for (item = node->data.sequence.items.start, i = 0;
532 item < node->data.sequence.items.top; ++item, ++i) {
533 yaml_node_t *n;
535 n = yaml_document_get_node(document, *item);
536 stmt->args[i] = extract_expr(ctx, document, n);
537 if (!stmt->args[i])
538 return pet_stmt_free(stmt);
541 return stmt;
544 static struct pet_stmt *extract_stmt(isl_ctx *ctx, yaml_document_t *document,
545 yaml_node_t *node)
547 struct pet_stmt *stmt;
548 yaml_node_pair_t * pair;
550 if (node->type != YAML_MAPPING_NODE)
551 isl_die(ctx, isl_error_invalid, "expecting mapping",
552 return NULL);
554 stmt = isl_calloc_type(ctx, struct pet_stmt);
555 if (!stmt)
556 return NULL;
558 for (pair = node->data.mapping.pairs.start;
559 pair < node->data.mapping.pairs.top; ++pair) {
560 yaml_node_t *key, *value;
562 key = yaml_document_get_node(document, pair->key);
563 value = yaml_document_get_node(document, pair->value);
565 if (key->type != YAML_SCALAR_NODE)
566 isl_die(ctx, isl_error_invalid, "expecting scalar key",
567 return pet_stmt_free(stmt));
569 if (!strcmp((char *) key->data.scalar.value, "line"))
570 stmt->line = extract_int(ctx, document, value);
571 if (!strcmp((char *) key->data.scalar.value, "domain"))
572 stmt->domain = extract_set(ctx, document, value);
573 if (!strcmp((char *) key->data.scalar.value, "schedule"))
574 stmt->schedule = extract_map(ctx, document, value);
575 if (!strcmp((char *) key->data.scalar.value, "body"))
576 stmt->body = extract_expr(ctx, document, value);
578 if (!strcmp((char *) key->data.scalar.value, "arguments"))
579 stmt = extract_stmt_arguments(ctx, document,
580 value, stmt);
581 if (!stmt)
582 return NULL;
585 return stmt;
588 static struct pet_scop *extract_statements(isl_ctx *ctx,
589 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
591 int i;
592 yaml_node_item_t *item;
594 if (node->type != YAML_SEQUENCE_NODE)
595 isl_die(ctx, isl_error_invalid, "expecting sequence",
596 return NULL);
598 scop->n_stmt = node->data.sequence.items.top
599 - node->data.sequence.items.start;
600 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, scop->n_stmt);
601 if (!scop->stmts)
602 return pet_scop_free(scop);
604 for (item = node->data.sequence.items.start, i = 0;
605 item < node->data.sequence.items.top; ++item, ++i) {
606 yaml_node_t *n;
608 n = yaml_document_get_node(document, *item);
609 scop->stmts[i] = extract_stmt(ctx, document, n);
610 if (!scop->stmts[i])
611 return pet_scop_free(scop);
614 return scop;
617 /* Extract a pet_implication from "node".
619 static struct pet_implication *extract_implication(isl_ctx *ctx,
620 yaml_document_t *document, yaml_node_t *node)
622 struct pet_implication *implication;
623 yaml_node_pair_t * pair;
625 if (node->type != YAML_MAPPING_NODE)
626 isl_die(ctx, isl_error_invalid, "expecting mapping",
627 return NULL);
629 implication = isl_calloc_type(ctx, struct pet_implication);
630 if (!implication)
631 return NULL;
633 for (pair = node->data.mapping.pairs.start;
634 pair < node->data.mapping.pairs.top; ++pair) {
635 yaml_node_t *key, *value;
637 key = yaml_document_get_node(document, pair->key);
638 value = yaml_document_get_node(document, pair->value);
640 if (key->type != YAML_SCALAR_NODE)
641 isl_die(ctx, isl_error_invalid, "expecting scalar key",
642 return pet_implication_free(implication));
644 if (!strcmp((char *) key->data.scalar.value, "satisfied"))
645 implication->satisfied =
646 extract_int(ctx, document, value);
647 if (!strcmp((char *) key->data.scalar.value, "extension"))
648 implication->extension =
649 extract_map(ctx, document, value);
652 return implication;
655 /* Extract a sequence of implications from "node" and
656 * store them in scop->implications.
658 static struct pet_scop *extract_implications(isl_ctx *ctx,
659 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
661 int i;
662 yaml_node_item_t *item;
664 if (node->type != YAML_SEQUENCE_NODE)
665 isl_die(ctx, isl_error_invalid, "expecting sequence",
666 return NULL);
668 scop->n_implication = node->data.sequence.items.top
669 - node->data.sequence.items.start;
670 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
671 scop->n_implication);
672 if (!scop->implications)
673 return pet_scop_free(scop);
675 for (item = node->data.sequence.items.start, i = 0;
676 item < node->data.sequence.items.top; ++item, ++i) {
677 yaml_node_t *n;
679 n = yaml_document_get_node(document, *item);
680 scop->implications[i] = extract_implication(ctx, document, n);
681 if (!scop->implications[i])
682 return pet_scop_free(scop);
685 return scop;
688 static struct pet_scop *extract_scop(isl_ctx *ctx, yaml_document_t *document,
689 yaml_node_t *node)
691 struct pet_scop *scop;
692 yaml_node_pair_t * pair;
694 if (!node)
695 return NULL;
697 if (node->type != YAML_MAPPING_NODE)
698 isl_die(ctx, isl_error_invalid, "expecting mapping",
699 return NULL);
701 scop = pet_scop_alloc(ctx);
702 if (!scop)
703 return NULL;
705 for (pair = node->data.mapping.pairs.start;
706 pair < node->data.mapping.pairs.top; ++pair) {
707 yaml_node_t *key, *value;
709 key = yaml_document_get_node(document, pair->key);
710 value = yaml_document_get_node(document, pair->value);
712 if (key->type != YAML_SCALAR_NODE)
713 isl_die(ctx, isl_error_invalid, "expecting scalar key",
714 return pet_scop_free(scop));
715 if (!strcmp((char *) key->data.scalar.value, "context"))
716 scop->context = extract_set(ctx, document, value);
717 if (!strcmp((char *) key->data.scalar.value, "context_value"))
718 scop->context_value = extract_set(ctx, document, value);
719 if (!strcmp((char *) key->data.scalar.value, "types"))
720 scop = extract_types(ctx, document, value, scop);
721 if (!strcmp((char *) key->data.scalar.value, "arrays"))
722 scop = extract_arrays(ctx, document, value, scop);
723 if (!strcmp((char *) key->data.scalar.value, "statements"))
724 scop = extract_statements(ctx, document, value, scop);
725 if (!strcmp((char *) key->data.scalar.value, "implications"))
726 scop = extract_implications(ctx, document, value, scop);
727 if (!scop)
728 return NULL;
731 if (!scop->context_value) {
732 isl_space *space = isl_space_params_alloc(ctx, 0);
733 scop->context_value = isl_set_universe(space);
734 if (!scop->context_value)
735 return pet_scop_free(scop);
738 return scop;
741 /* Extract a pet_scop from the YAML description in "in".
743 struct pet_scop *pet_scop_parse(isl_ctx *ctx, FILE *in)
745 struct pet_scop *scop = NULL;
746 yaml_parser_t parser;
747 yaml_node_t *root;
748 yaml_document_t document = { 0 };
750 yaml_parser_initialize(&parser);
752 yaml_parser_set_input_file(&parser, in);
754 if (!yaml_parser_load(&parser, &document))
755 goto error;
757 root = yaml_document_get_root_node(&document);
759 scop = extract_scop(ctx, &document, root);
761 yaml_document_delete(&document);
763 yaml_parser_delete(&parser);
765 return scop;
766 error:
767 yaml_parser_delete(&parser);
768 pet_scop_free(scop);
769 return NULL;