add pet_expr_access_is_write
[pet.git] / parse.c
blob4d788e1061e3dd81a7ae3ac760d807f9e9986621
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2013 Ecole Normale Superieure. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
32 * Leiden University.
33 */
35 #include <stdlib.h>
36 #include <yaml.h>
38 #include "expr.h"
39 #include "scop.h"
40 #include "scop_yaml.h"
42 static char *extract_string(isl_ctx *ctx, yaml_document_t *document,
43 yaml_node_t *node)
45 if (node->type != YAML_SCALAR_NODE)
46 isl_die(ctx, isl_error_invalid, "expecting scalar node",
47 return NULL);
49 return strdup((char *) node->data.scalar.value);
52 static int extract_int(isl_ctx *ctx, yaml_document_t *document,
53 yaml_node_t *node)
55 if (node->type != YAML_SCALAR_NODE)
56 isl_die(ctx, isl_error_invalid, "expecting scalar node",
57 return -1);
59 return atoi((char *) node->data.scalar.value);
62 static double extract_double(isl_ctx *ctx, yaml_document_t *document,
63 yaml_node_t *node)
65 if (node->type != YAML_SCALAR_NODE)
66 isl_die(ctx, isl_error_invalid, "expecting scalar node",
67 return -1);
69 return strtod((char *) node->data.scalar.value, NULL);
72 static enum pet_expr_type extract_expr_type(isl_ctx *ctx,
73 yaml_document_t *document, yaml_node_t *node)
75 if (node->type != YAML_SCALAR_NODE)
76 isl_die(ctx, isl_error_invalid, "expecting scalar node",
77 return -1);
79 return pet_str_type((char *) node->data.scalar.value);
82 static enum pet_op_type extract_op(isl_ctx *ctx, yaml_document_t *document,
83 yaml_node_t *node)
85 if (node->type != YAML_SCALAR_NODE)
86 isl_die(ctx, isl_error_invalid, "expecting scalar node",
87 return -1);
89 return pet_str_op((char *) node->data.scalar.value);
92 static __isl_give isl_set *extract_set(isl_ctx *ctx, yaml_document_t *document,
93 yaml_node_t *node)
95 if (node->type != YAML_SCALAR_NODE)
96 isl_die(ctx, isl_error_invalid, "expecting scalar node",
97 return NULL);
99 return isl_set_read_from_str(ctx, (char *) node->data.scalar.value);
102 static __isl_give isl_id *extract_id(isl_ctx *ctx, yaml_document_t *document,
103 yaml_node_t *node)
105 if (node->type != YAML_SCALAR_NODE)
106 isl_die(ctx, isl_error_invalid, "expecting scalar node",
107 return NULL);
109 return isl_id_alloc(ctx, (char *) node->data.scalar.value, NULL);
112 static __isl_give isl_map *extract_map(isl_ctx *ctx, yaml_document_t *document,
113 yaml_node_t *node)
115 if (node->type != YAML_SCALAR_NODE)
116 isl_die(ctx, isl_error_invalid, "expecting scalar node",
117 return NULL);
119 return isl_map_read_from_str(ctx, (char *) node->data.scalar.value);
122 /* Extract an isl_val from "node".
124 static __isl_give isl_val *extract_val(isl_ctx *ctx, yaml_document_t *document,
125 yaml_node_t *node)
127 if (node->type != YAML_SCALAR_NODE)
128 isl_die(ctx, isl_error_invalid, "expecting scalar node",
129 return NULL);
131 return isl_val_read_from_str(ctx, (char *) node->data.scalar.value);
134 /* Extract an isl_multi_pw_aff from "node".
136 static __isl_give isl_multi_pw_aff *extract_multi_pw_aff(isl_ctx *ctx,
137 yaml_document_t *document, yaml_node_t *node)
139 if (node->type != YAML_SCALAR_NODE)
140 isl_die(ctx, isl_error_invalid, "expecting scalar node",
141 return NULL);
143 return isl_multi_pw_aff_read_from_str(ctx,
144 (char *) node->data.scalar.value);
147 /* Extract a pet_type from "node".
149 static struct pet_type *extract_type(isl_ctx *ctx,
150 yaml_document_t *document, yaml_node_t *node)
152 struct pet_type *type;
153 yaml_node_pair_t * pair;
155 if (node->type != YAML_MAPPING_NODE)
156 isl_die(ctx, isl_error_invalid, "expecting mapping",
157 return NULL);
159 type = isl_calloc_type(ctx, struct pet_type);
160 if (!type)
161 return NULL;
163 for (pair = node->data.mapping.pairs.start;
164 pair < node->data.mapping.pairs.top; ++pair) {
165 yaml_node_t *key, *value;
167 key = yaml_document_get_node(document, pair->key);
168 value = yaml_document_get_node(document, pair->value);
170 if (key->type != YAML_SCALAR_NODE)
171 isl_die(ctx, isl_error_invalid, "expecting scalar key",
172 return pet_type_free(type));
174 if (!strcmp((char *) key->data.scalar.value, "name"))
175 type->name = extract_string(ctx, document, value);
176 if (!strcmp((char *) key->data.scalar.value, "definition"))
177 type->definition = extract_string(ctx, document, value);
180 return type;
183 /* Extract a sequence of types from "node" and store them in scop->types.
185 static struct pet_scop *extract_types(isl_ctx *ctx,
186 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
188 int i;
189 yaml_node_item_t *item;
191 if (node->type != YAML_SEQUENCE_NODE)
192 isl_die(ctx, isl_error_invalid, "expecting sequence",
193 return NULL);
195 scop->n_type = node->data.sequence.items.top
196 - node->data.sequence.items.start;
197 scop->types = isl_calloc_array(ctx, struct pet_type *, scop->n_type);
198 if (!scop->types)
199 return pet_scop_free(scop);
201 for (item = node->data.sequence.items.start, i = 0;
202 item < node->data.sequence.items.top; ++item, ++i) {
203 yaml_node_t *n;
205 n = yaml_document_get_node(document, *item);
206 scop->types[i] = extract_type(ctx, document, n);
207 if (!scop->types[i])
208 return pet_scop_free(scop);
211 return scop;
214 static struct pet_array *extract_array(isl_ctx *ctx, yaml_document_t *document,
215 yaml_node_t *node)
217 struct pet_array *array;
218 yaml_node_pair_t * pair;
220 if (node->type != YAML_MAPPING_NODE)
221 isl_die(ctx, isl_error_invalid, "expecting mapping",
222 return NULL);
224 array = isl_calloc_type(ctx, struct pet_array);
225 if (!array)
226 return NULL;
228 for (pair = node->data.mapping.pairs.start;
229 pair < node->data.mapping.pairs.top; ++pair) {
230 yaml_node_t *key, *value;
232 key = yaml_document_get_node(document, pair->key);
233 value = yaml_document_get_node(document, pair->value);
235 if (key->type != YAML_SCALAR_NODE)
236 isl_die(ctx, isl_error_invalid, "expecting scalar key",
237 return pet_array_free(array));
239 if (!strcmp((char *) key->data.scalar.value, "context"))
240 array->context = extract_set(ctx, document, value);
241 if (!strcmp((char *) key->data.scalar.value, "extent"))
242 array->extent = extract_set(ctx, document, value);
243 if (!strcmp((char *) key->data.scalar.value, "value_bounds"))
244 array->value_bounds = extract_set(ctx, document, value);
245 if (!strcmp((char *) key->data.scalar.value, "element_type"))
246 array->element_type =
247 extract_string(ctx, document, value);
248 if (!strcmp((char *) key->data.scalar.value, "element_size"))
249 array->element_size = extract_int(ctx, document, value);
250 if (!strcmp((char *) key->data.scalar.value,
251 "element_is_record"))
252 array->element_is_record =
253 extract_int(ctx, document, value);
254 if (!strcmp((char *) key->data.scalar.value, "live_out"))
255 array->live_out = extract_int(ctx, document, value);
256 if (!strcmp((char *) key->data.scalar.value,
257 "uniquely_defined"))
258 array->uniquely_defined =
259 extract_int(ctx, document, value);
260 if (!strcmp((char *) key->data.scalar.value, "declared"))
261 array->declared = extract_int(ctx, document, value);
262 if (!strcmp((char *) key->data.scalar.value, "exposed"))
263 array->exposed = extract_int(ctx, document, value);
266 return array;
269 static struct pet_scop *extract_arrays(isl_ctx *ctx, yaml_document_t *document,
270 yaml_node_t *node, struct pet_scop *scop)
272 int i;
273 yaml_node_item_t *item;
275 if (node->type != YAML_SEQUENCE_NODE)
276 isl_die(ctx, isl_error_invalid, "expecting sequence",
277 return NULL);
279 scop->n_array = node->data.sequence.items.top
280 - node->data.sequence.items.start;
281 scop->arrays = isl_calloc_array(ctx, struct pet_array *, scop->n_array);
282 if (!scop->arrays)
283 return pet_scop_free(scop);
285 for (item = node->data.sequence.items.start, i = 0;
286 item < node->data.sequence.items.top; ++item, ++i) {
287 yaml_node_t *n;
289 n = yaml_document_get_node(document, *item);
290 scop->arrays[i] = extract_array(ctx, document, n);
291 if (!scop->arrays[i])
292 return pet_scop_free(scop);
295 return scop;
298 static struct pet_expr *extract_expr(isl_ctx *ctx, yaml_document_t *document,
299 yaml_node_t *node);
301 static struct pet_expr *extract_arguments(isl_ctx *ctx,
302 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
304 int i;
305 yaml_node_item_t *item;
307 if (node->type != YAML_SEQUENCE_NODE)
308 isl_die(ctx, isl_error_invalid, "expecting sequence",
309 return pet_expr_free(expr));
311 expr->n_arg = node->data.sequence.items.top
312 - node->data.sequence.items.start;
313 expr->args = isl_calloc_array(ctx, struct pet_expr *, expr->n_arg);
314 if (!expr->args)
315 return pet_expr_free(expr);
317 for (item = node->data.sequence.items.start, i = 0;
318 item < node->data.sequence.items.top; ++item, ++i) {
319 yaml_node_t *n;
321 n = yaml_document_get_node(document, *item);
322 expr->args[i] = extract_expr(ctx, document, n);
323 if (!expr->args[i])
324 return pet_expr_free(expr);
327 return expr;
330 /* Extract pet_expr_double specific fields from "node" and
331 * update "expr" accordingly.
333 static struct pet_expr *extract_expr_double(isl_ctx *ctx,
334 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
336 yaml_node_pair_t *pair;
338 for (pair = node->data.mapping.pairs.start;
339 pair < node->data.mapping.pairs.top; ++pair) {
340 yaml_node_t *key, *value;
342 key = yaml_document_get_node(document, pair->key);
343 value = yaml_document_get_node(document, pair->value);
345 if (key->type != YAML_SCALAR_NODE)
346 isl_die(ctx, isl_error_invalid, "expecting scalar key",
347 return pet_expr_free(expr));
349 if (!strcmp((char *) key->data.scalar.value, "value"))
350 expr->d.val = extract_double(ctx, document, value);
351 if (!strcmp((char *) key->data.scalar.value, "string"))
352 expr->d.s = extract_string(ctx, document, value);
355 return expr;
358 /* Extract pet_expr_access specific fields from "node" and
359 * update "expr" accordingly.
361 static struct pet_expr *extract_expr_access(isl_ctx *ctx,
362 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
364 yaml_node_pair_t *pair;
366 for (pair = node->data.mapping.pairs.start;
367 pair < node->data.mapping.pairs.top; ++pair) {
368 yaml_node_t *key, *value;
370 key = yaml_document_get_node(document, pair->key);
371 value = yaml_document_get_node(document, pair->value);
373 if (key->type != YAML_SCALAR_NODE)
374 isl_die(ctx, isl_error_invalid, "expecting scalar key",
375 return pet_expr_free(expr));
377 if (!strcmp((char *) key->data.scalar.value, "relation"))
378 expr->acc.access = extract_map(ctx, document, value);
379 if (!strcmp((char *) key->data.scalar.value, "index"))
380 expr->acc.index = extract_multi_pw_aff(ctx, document,
381 value);
382 if (!strcmp((char *) key->data.scalar.value, "reference"))
383 expr->acc.ref_id = extract_id(ctx, document, value);
384 if (!strcmp((char *) key->data.scalar.value, "read"))
385 expr->acc.read = extract_int(ctx, document, value);
386 if (!strcmp((char *) key->data.scalar.value, "write"))
387 expr->acc.write = extract_int(ctx, document, value);
390 return expr;
393 /* Extract operation expression specific fields from "node" and
394 * update "expr" accordingly.
396 static struct pet_expr *extract_expr_op(isl_ctx *ctx,
397 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
399 yaml_node_pair_t *pair;
401 for (pair = node->data.mapping.pairs.start;
402 pair < node->data.mapping.pairs.top; ++pair) {
403 yaml_node_t *key, *value;
405 key = yaml_document_get_node(document, pair->key);
406 value = yaml_document_get_node(document, pair->value);
408 if (key->type != YAML_SCALAR_NODE)
409 isl_die(ctx, isl_error_invalid, "expecting scalar key",
410 return pet_expr_free(expr));
412 if (!strcmp((char *) key->data.scalar.value, "operation"))
413 expr->op = extract_op(ctx, document, value);
416 return expr;
419 /* Extract pet_expr_call specific fields from "node" and
420 * update "expr" accordingly.
422 static struct pet_expr *extract_expr_call(isl_ctx *ctx,
423 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
425 yaml_node_pair_t *pair;
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, "name"))
439 expr->name = extract_string(ctx, document, value);
442 return expr;
445 /* Extract pet_expr_cast specific fields from "node" and
446 * update "expr" accordingly.
448 static struct pet_expr *extract_expr_cast(isl_ctx *ctx,
449 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
451 yaml_node_pair_t *pair;
453 for (pair = node->data.mapping.pairs.start;
454 pair < node->data.mapping.pairs.top; ++pair) {
455 yaml_node_t *key, *value;
457 key = yaml_document_get_node(document, pair->key);
458 value = yaml_document_get_node(document, pair->value);
460 if (key->type != YAML_SCALAR_NODE)
461 isl_die(ctx, isl_error_invalid, "expecting scalar key",
462 return pet_expr_free(expr));
464 if (!strcmp((char *) key->data.scalar.value, "type_name"))
465 expr->type_name = extract_string(ctx, document, value);
468 return expr;
471 /* Extract pet_expr_int specific fields from "node" and
472 * update "expr" accordingly.
474 static struct pet_expr *extract_expr_int(isl_ctx *ctx,
475 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
477 yaml_node_pair_t * pair;
479 for (pair = node->data.mapping.pairs.start;
480 pair < node->data.mapping.pairs.top; ++pair) {
481 yaml_node_t *key, *value;
483 key = yaml_document_get_node(document, pair->key);
484 value = yaml_document_get_node(document, pair->value);
486 if (key->type != YAML_SCALAR_NODE)
487 isl_die(ctx, isl_error_invalid, "expecting scalar key",
488 return pet_expr_free(expr));
490 if (!strcmp((char *) key->data.scalar.value, "value"))
491 expr->i = extract_val(ctx, document, value);
494 return expr;
497 /* Extract a pet_expr from "node".
499 * We first extract the type and arguments of the expression and
500 * then extract additional fields depending on the type.
502 static struct pet_expr *extract_expr(isl_ctx *ctx, yaml_document_t *document,
503 yaml_node_t *node)
505 struct pet_expr *expr;
506 yaml_node_pair_t *pair;
508 if (node->type != YAML_MAPPING_NODE)
509 isl_die(ctx, isl_error_invalid, "expecting mapping",
510 return NULL);
512 expr = isl_calloc_type(ctx, struct pet_expr);
513 if (!expr)
514 return NULL;
516 for (pair = node->data.mapping.pairs.start;
517 pair < node->data.mapping.pairs.top; ++pair) {
518 yaml_node_t *key, *value;
520 key = yaml_document_get_node(document, pair->key);
521 value = yaml_document_get_node(document, pair->value);
523 if (key->type != YAML_SCALAR_NODE)
524 isl_die(ctx, isl_error_invalid, "expecting scalar key",
525 return pet_expr_free(expr));
527 if (!strcmp((char *) key->data.scalar.value, "type"))
528 expr->type = extract_expr_type(ctx, document, value);
530 if (!strcmp((char *) key->data.scalar.value, "arguments"))
531 expr = extract_arguments(ctx, document, value, expr);
532 if (!expr)
533 return NULL;
536 switch (expr->type) {
537 case pet_expr_access:
538 expr = extract_expr_access(ctx, document, node, expr);
539 break;
540 case pet_expr_double:
541 expr = extract_expr_double(ctx, document, node, expr);
542 break;
543 case pet_expr_call:
544 expr = extract_expr_call(ctx, document, node, expr);
545 break;
546 case pet_expr_cast:
547 expr = extract_expr_cast(ctx, document, node, expr);
548 break;
549 case pet_expr_int:
550 expr = extract_expr_int(ctx, document, node, expr);
551 break;
552 case pet_expr_op:
553 expr = extract_expr_op(ctx, document, node, expr);
554 break;
557 return expr;
560 static struct pet_stmt *extract_stmt_arguments(isl_ctx *ctx,
561 yaml_document_t *document, yaml_node_t *node, struct pet_stmt *stmt)
563 int i;
564 yaml_node_item_t *item;
566 if (node->type != YAML_SEQUENCE_NODE)
567 isl_die(ctx, isl_error_invalid, "expecting sequence",
568 return pet_stmt_free(stmt));
570 stmt->n_arg = node->data.sequence.items.top
571 - node->data.sequence.items.start;
572 stmt->args = isl_calloc_array(ctx, struct pet_expr *, stmt->n_arg);
573 if (!stmt->args)
574 return pet_stmt_free(stmt);
576 for (item = node->data.sequence.items.start, i = 0;
577 item < node->data.sequence.items.top; ++item, ++i) {
578 yaml_node_t *n;
580 n = yaml_document_get_node(document, *item);
581 stmt->args[i] = extract_expr(ctx, document, n);
582 if (!stmt->args[i])
583 return pet_stmt_free(stmt);
586 return stmt;
589 static struct pet_stmt *extract_stmt(isl_ctx *ctx, yaml_document_t *document,
590 yaml_node_t *node)
592 struct pet_stmt *stmt;
593 yaml_node_pair_t * pair;
595 if (node->type != YAML_MAPPING_NODE)
596 isl_die(ctx, isl_error_invalid, "expecting mapping",
597 return NULL);
599 stmt = isl_calloc_type(ctx, struct pet_stmt);
600 if (!stmt)
601 return NULL;
603 for (pair = node->data.mapping.pairs.start;
604 pair < node->data.mapping.pairs.top; ++pair) {
605 yaml_node_t *key, *value;
607 key = yaml_document_get_node(document, pair->key);
608 value = yaml_document_get_node(document, pair->value);
610 if (key->type != YAML_SCALAR_NODE)
611 isl_die(ctx, isl_error_invalid, "expecting scalar key",
612 return pet_stmt_free(stmt));
614 if (!strcmp((char *) key->data.scalar.value, "line"))
615 stmt->line = extract_int(ctx, document, value);
616 if (!strcmp((char *) key->data.scalar.value, "domain"))
617 stmt->domain = extract_set(ctx, document, value);
618 if (!strcmp((char *) key->data.scalar.value, "schedule"))
619 stmt->schedule = extract_map(ctx, document, value);
620 if (!strcmp((char *) key->data.scalar.value, "body"))
621 stmt->body = extract_expr(ctx, document, value);
623 if (!strcmp((char *) key->data.scalar.value, "arguments"))
624 stmt = extract_stmt_arguments(ctx, document,
625 value, stmt);
626 if (!stmt)
627 return NULL;
630 return stmt;
633 static struct pet_scop *extract_statements(isl_ctx *ctx,
634 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
636 int i;
637 yaml_node_item_t *item;
639 if (node->type != YAML_SEQUENCE_NODE)
640 isl_die(ctx, isl_error_invalid, "expecting sequence",
641 return NULL);
643 scop->n_stmt = node->data.sequence.items.top
644 - node->data.sequence.items.start;
645 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, scop->n_stmt);
646 if (!scop->stmts)
647 return pet_scop_free(scop);
649 for (item = node->data.sequence.items.start, i = 0;
650 item < node->data.sequence.items.top; ++item, ++i) {
651 yaml_node_t *n;
653 n = yaml_document_get_node(document, *item);
654 scop->stmts[i] = extract_stmt(ctx, document, n);
655 if (!scop->stmts[i])
656 return pet_scop_free(scop);
659 return scop;
662 /* Extract a pet_implication from "node".
664 static struct pet_implication *extract_implication(isl_ctx *ctx,
665 yaml_document_t *document, yaml_node_t *node)
667 struct pet_implication *implication;
668 yaml_node_pair_t * pair;
670 if (node->type != YAML_MAPPING_NODE)
671 isl_die(ctx, isl_error_invalid, "expecting mapping",
672 return NULL);
674 implication = isl_calloc_type(ctx, struct pet_implication);
675 if (!implication)
676 return NULL;
678 for (pair = node->data.mapping.pairs.start;
679 pair < node->data.mapping.pairs.top; ++pair) {
680 yaml_node_t *key, *value;
682 key = yaml_document_get_node(document, pair->key);
683 value = yaml_document_get_node(document, pair->value);
685 if (key->type != YAML_SCALAR_NODE)
686 isl_die(ctx, isl_error_invalid, "expecting scalar key",
687 return pet_implication_free(implication));
689 if (!strcmp((char *) key->data.scalar.value, "satisfied"))
690 implication->satisfied =
691 extract_int(ctx, document, value);
692 if (!strcmp((char *) key->data.scalar.value, "extension"))
693 implication->extension =
694 extract_map(ctx, document, value);
697 return implication;
700 /* Extract a sequence of implications from "node" and
701 * store them in scop->implications.
703 static struct pet_scop *extract_implications(isl_ctx *ctx,
704 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
706 int i;
707 yaml_node_item_t *item;
709 if (node->type != YAML_SEQUENCE_NODE)
710 isl_die(ctx, isl_error_invalid, "expecting sequence",
711 return NULL);
713 scop->n_implication = node->data.sequence.items.top
714 - node->data.sequence.items.start;
715 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
716 scop->n_implication);
717 if (!scop->implications)
718 return pet_scop_free(scop);
720 for (item = node->data.sequence.items.start, i = 0;
721 item < node->data.sequence.items.top; ++item, ++i) {
722 yaml_node_t *n;
724 n = yaml_document_get_node(document, *item);
725 scop->implications[i] = extract_implication(ctx, document, n);
726 if (!scop->implications[i])
727 return pet_scop_free(scop);
730 return scop;
733 static struct pet_scop *extract_scop(isl_ctx *ctx, yaml_document_t *document,
734 yaml_node_t *node)
736 struct pet_scop *scop;
737 yaml_node_pair_t * pair;
739 if (!node)
740 return NULL;
742 if (node->type != YAML_MAPPING_NODE)
743 isl_die(ctx, isl_error_invalid, "expecting mapping",
744 return NULL);
746 scop = pet_scop_alloc(ctx);
747 if (!scop)
748 return NULL;
750 for (pair = node->data.mapping.pairs.start;
751 pair < node->data.mapping.pairs.top; ++pair) {
752 yaml_node_t *key, *value;
754 key = yaml_document_get_node(document, pair->key);
755 value = yaml_document_get_node(document, pair->value);
757 if (key->type != YAML_SCALAR_NODE)
758 isl_die(ctx, isl_error_invalid, "expecting scalar key",
759 return pet_scop_free(scop));
760 if (!strcmp((char *) key->data.scalar.value, "context"))
761 scop->context = extract_set(ctx, document, value);
762 if (!strcmp((char *) key->data.scalar.value, "context_value"))
763 scop->context_value = extract_set(ctx, document, value);
764 if (!strcmp((char *) key->data.scalar.value, "types"))
765 scop = extract_types(ctx, document, value, scop);
766 if (!strcmp((char *) key->data.scalar.value, "arrays"))
767 scop = extract_arrays(ctx, document, value, scop);
768 if (!strcmp((char *) key->data.scalar.value, "statements"))
769 scop = extract_statements(ctx, document, value, scop);
770 if (!strcmp((char *) key->data.scalar.value, "implications"))
771 scop = extract_implications(ctx, document, value, scop);
772 if (!scop)
773 return NULL;
776 if (!scop->context_value) {
777 isl_space *space = isl_space_params_alloc(ctx, 0);
778 scop->context_value = isl_set_universe(space);
779 if (!scop->context_value)
780 return pet_scop_free(scop);
783 return scop;
786 /* Extract a pet_scop from the YAML description in "in".
788 struct pet_scop *pet_scop_parse(isl_ctx *ctx, FILE *in)
790 struct pet_scop *scop = NULL;
791 yaml_parser_t parser;
792 yaml_node_t *root;
793 yaml_document_t document = { 0 };
795 yaml_parser_initialize(&parser);
797 yaml_parser_set_input_file(&parser, in);
799 if (!yaml_parser_load(&parser, &document))
800 goto error;
802 root = yaml_document_get_root_node(&document);
804 scop = extract_scop(ctx, &document, root);
806 yaml_document_delete(&document);
808 yaml_parser_delete(&parser);
810 return scop;
811 error:
812 yaml_parser_delete(&parser);
813 pet_scop_free(scop);
814 return NULL;