2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2013-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
44 #include <isl/union_set.h>
45 #include <isl/union_map.h>
50 #include "scop_yaml.h"
53 static char *extract_string(isl_ctx
*ctx
, yaml_document_t
*document
,
56 if (node
->type
!= YAML_SCALAR_NODE
)
57 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
60 return strdup((char *) node
->data
.scalar
.value
);
63 static int extract_int(isl_ctx
*ctx
, yaml_document_t
*document
,
66 if (node
->type
!= YAML_SCALAR_NODE
)
67 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
70 return atoi((char *) node
->data
.scalar
.value
);
73 static double extract_double(isl_ctx
*ctx
, yaml_document_t
*document
,
76 if (node
->type
!= YAML_SCALAR_NODE
)
77 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
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",
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
,
96 if (node
->type
!= YAML_SCALAR_NODE
)
97 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
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
,
106 if (node
->type
!= YAML_SCALAR_NODE
)
107 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
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
,
116 if (node
->type
!= YAML_SCALAR_NODE
)
117 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
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
,
126 if (node
->type
!= YAML_SCALAR_NODE
)
127 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
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",
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",
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
,
164 if (node
->type
!= YAML_SCALAR_NODE
)
165 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
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",
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",
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",
209 type
= isl_calloc_type(ctx
, struct pet_type
);
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
);
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
)
239 yaml_node_item_t
*item
;
241 if (node
->type
!= YAML_SEQUENCE_NODE
)
242 isl_die(ctx
, isl_error_invalid
, "expecting sequence",
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
);
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
) {
255 n
= yaml_document_get_node(document
, *item
);
256 scop
->types
[i
] = extract_type(ctx
, document
, n
);
258 return pet_scop_free(scop
);
264 static struct pet_array
*extract_array(isl_ctx
*ctx
, yaml_document_t
*document
,
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",
274 array
= isl_calloc_type(ctx
, struct pet_array
);
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
,
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
);
321 static struct pet_scop
*extract_arrays(isl_ctx
*ctx
, yaml_document_t
*document
,
322 yaml_node_t
*node
, struct pet_scop
*scop
)
325 yaml_node_item_t
*item
;
327 if (node
->type
!= YAML_SEQUENCE_NODE
)
328 isl_die(ctx
, isl_error_invalid
, "expecting sequence",
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
);
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
) {
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
);
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
)
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
) {
371 n
= yaml_document_get_node(document
, *item
);
372 arg
= extract_expr(ctx
, document
, n
);
373 expr
= pet_expr_set_arg(expr
, i
, arg
);
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
;
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
);
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
;
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
);
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
));
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
));
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
));
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
));
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
));
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
;
610 yaml_node_pair_t
*pair
;
612 if (node
->type
!= YAML_MAPPING_NODE
)
613 isl_die(ctx
, isl_error_invalid
, "expecting mapping",
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",
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",
635 expr
= pet_expr_alloc(ctx
, type
);
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
);
654 isl_die(ctx
, isl_error_internal
, "unreachable code",
656 case pet_expr_access
:
657 expr
= extract_expr_access(ctx
, document
, node
, expr
);
659 case pet_expr_double
:
660 expr
= extract_expr_double(ctx
, document
, node
, expr
);
663 expr
= extract_expr_call(ctx
, document
, node
, expr
);
666 expr
= extract_expr_cast(ctx
, document
, node
, expr
);
669 expr
= extract_expr_int(ctx
, document
, node
, expr
);
672 expr
= extract_expr_op(ctx
, document
, node
, 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",
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
)
701 yaml_node_pair_t
*pair
;
702 yaml_node_item_t
*item
;
703 yaml_node_t
*children
= NULL
;
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",
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"))
726 n
= children
->data
.sequence
.items
.top
-
727 children
->data
.sequence
.items
.start
;
729 tree
= pet_tree_new_block(ctx
, block
, n
);
733 for (item
= children
->data
.sequence
.items
.start
, i
= 0;
734 item
< children
->data
.sequence
.items
.top
; ++item
, ++i
) {
738 n
= yaml_document_get_node(document
, *item
);
739 child
= extract_tree(ctx
, document
, n
);
740 tree
= pet_tree_block_add_child(tree
, child
);
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",
765 if (!strcmp((char *) key
->data
.scalar
.value
, "variable")) {
766 var
= extract_expr(ctx
, document
, value
);
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",
799 if (!strcmp((char *) key
->data
.scalar
.value
, "variable")) {
800 var
= extract_expr(ctx
, document
, value
);
804 if (!strcmp((char *) key
->data
.scalar
.value
,
806 init
= extract_expr(ctx
, document
, value
);
813 isl_die(ctx
, isl_error_invalid
,
814 "no variable field", goto error
);
816 isl_die(ctx
, isl_error_invalid
,
817 "no initialization field", goto error
);
819 return pet_tree_new_decl_init(var
, init
);
826 /* Extract a pet_expr corresponding to a pet_tree with a single "expr" field
829 static __isl_give pet_expr
*extract_expr_field(isl_ctx
*ctx
,
830 yaml_document_t
*document
, yaml_node_t
*node
)
832 yaml_node_pair_t
*pair
;
833 pet_expr
*expr
= NULL
;
835 for (pair
= node
->data
.mapping
.pairs
.start
;
836 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
837 yaml_node_t
*key
, *value
;
839 key
= yaml_document_get_node(document
, pair
->key
);
840 value
= yaml_document_get_node(document
, pair
->value
);
842 if (key
->type
!= YAML_SCALAR_NODE
)
843 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
846 if (!strcmp((char *) key
->data
.scalar
.value
, "expr")) {
847 expr
= extract_expr(ctx
, document
, value
);
854 isl_die(ctx
, isl_error_invalid
,
855 "no expr field", return NULL
);
860 /* Extract a pet_tree of type pet_tree_expr from "node".
862 static __isl_give pet_tree
*extract_tree_expr(isl_ctx
*ctx
,
863 yaml_document_t
*document
, yaml_node_t
*node
)
865 return pet_tree_new_expr(extract_expr_field(ctx
, document
, node
));
868 /* Extract a pet_tree of type pet_tree_return from "node".
870 static __isl_give pet_tree
*extract_tree_return(isl_ctx
*ctx
,
871 yaml_document_t
*document
, yaml_node_t
*node
)
873 return pet_tree_new_return(extract_expr_field(ctx
, document
, node
));
876 /* Extract a pet_tree of type pet_tree_while from "node".
878 static __isl_give pet_tree
*extract_tree_while(isl_ctx
*ctx
,
879 yaml_document_t
*document
, yaml_node_t
*node
)
881 yaml_node_pair_t
*pair
;
882 pet_expr
*cond
= NULL
;
883 pet_tree
*body
= NULL
;
885 for (pair
= node
->data
.mapping
.pairs
.start
;
886 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
887 yaml_node_t
*key
, *value
;
889 key
= yaml_document_get_node(document
, pair
->key
);
890 value
= yaml_document_get_node(document
, pair
->value
);
892 if (key
->type
!= YAML_SCALAR_NODE
)
893 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
896 if (!strcmp((char *) key
->data
.scalar
.value
, "condition")) {
897 cond
= extract_expr(ctx
, document
, value
);
901 if (!strcmp((char *) key
->data
.scalar
.value
, "body")) {
902 body
= extract_tree(ctx
, document
, value
);
909 isl_die(ctx
, isl_error_invalid
,
910 "no condition field", goto error
);
912 isl_die(ctx
, isl_error_invalid
,
913 "no body field", goto error
);
915 return pet_tree_new_while(cond
, body
);
922 /* Extract a pet_tree of type pet_tree_infinite_loop from "node".
924 static __isl_give pet_tree
*extract_tree_infinite_loop(isl_ctx
*ctx
,
925 yaml_document_t
*document
, yaml_node_t
*node
)
927 yaml_node_pair_t
*pair
;
930 for (pair
= node
->data
.mapping
.pairs
.start
;
931 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
932 yaml_node_t
*key
, *value
;
934 key
= yaml_document_get_node(document
, pair
->key
);
935 value
= yaml_document_get_node(document
, pair
->value
);
937 if (key
->type
!= YAML_SCALAR_NODE
)
938 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
941 if (!strcmp((char *) key
->data
.scalar
.value
, "body")) {
942 body
= extract_tree(ctx
, document
, value
);
949 isl_die(ctx
, isl_error_invalid
,
950 "no body field", return NULL
);
952 return pet_tree_new_infinite_loop(body
);
955 /* Extract a pet_tree of type pet_tree_if from "node".
957 static __isl_give pet_tree
*extract_tree_if(isl_ctx
*ctx
,
958 yaml_document_t
*document
, yaml_node_t
*node
)
960 yaml_node_pair_t
*pair
;
961 pet_expr
*cond
= NULL
;
962 pet_tree
*then_body
= NULL
;
964 for (pair
= node
->data
.mapping
.pairs
.start
;
965 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
966 yaml_node_t
*key
, *value
;
968 key
= yaml_document_get_node(document
, pair
->key
);
969 value
= yaml_document_get_node(document
, pair
->value
);
971 if (key
->type
!= YAML_SCALAR_NODE
)
972 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
975 if (!strcmp((char *) key
->data
.scalar
.value
, "condition")) {
976 cond
= extract_expr(ctx
, document
, value
);
980 if (!strcmp((char *) key
->data
.scalar
.value
, "then")) {
981 then_body
= extract_tree(ctx
, document
, value
);
988 isl_die(ctx
, isl_error_invalid
,
989 "no condition field", goto error
);
991 isl_die(ctx
, isl_error_invalid
,
992 "no then body", goto error
);
994 return pet_tree_new_if(cond
, then_body
);
997 pet_tree_free(then_body
);
1001 /* Extract a pet_tree of type pet_tree_if_else from "node".
1003 static __isl_give pet_tree
*extract_tree_if_else(isl_ctx
*ctx
,
1004 yaml_document_t
*document
, yaml_node_t
*node
)
1006 yaml_node_pair_t
*pair
;
1007 pet_expr
*cond
= NULL
;
1008 pet_tree
*then_body
= NULL
;
1009 pet_tree
*else_body
= NULL
;
1011 for (pair
= node
->data
.mapping
.pairs
.start
;
1012 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
1013 yaml_node_t
*key
, *value
;
1015 key
= yaml_document_get_node(document
, pair
->key
);
1016 value
= yaml_document_get_node(document
, pair
->value
);
1018 if (key
->type
!= YAML_SCALAR_NODE
)
1019 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
1022 if (!strcmp((char *) key
->data
.scalar
.value
, "condition")) {
1023 cond
= extract_expr(ctx
, document
, value
);
1027 if (!strcmp((char *) key
->data
.scalar
.value
, "then")) {
1028 then_body
= extract_tree(ctx
, document
, value
);
1032 if (!strcmp((char *) key
->data
.scalar
.value
, "else")) {
1033 else_body
= extract_tree(ctx
, document
, value
);
1040 isl_die(ctx
, isl_error_invalid
,
1041 "no condition field", goto error
);
1043 isl_die(ctx
, isl_error_invalid
,
1044 "no then body", goto error
);
1046 isl_die(ctx
, isl_error_invalid
,
1047 "no else body", goto error
);
1049 return pet_tree_new_if_else(cond
, then_body
, else_body
);
1051 pet_expr_free(cond
);
1052 pet_tree_free(then_body
);
1053 pet_tree_free(else_body
);
1057 /* Extract a pet_tree of type pet_tree_for from "node".
1059 static __isl_give pet_tree
*extract_tree_for(isl_ctx
*ctx
,
1060 yaml_document_t
*document
, yaml_node_t
*node
)
1062 yaml_node_pair_t
*pair
;
1064 int independent
= 0;
1065 pet_expr
*iv
= NULL
;
1066 pet_expr
*init
= NULL
;
1067 pet_expr
*cond
= NULL
;
1068 pet_expr
*inc
= NULL
;
1069 pet_tree
*body
= NULL
;
1071 for (pair
= node
->data
.mapping
.pairs
.start
;
1072 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
1073 yaml_node_t
*key
, *value
;
1075 key
= yaml_document_get_node(document
, pair
->key
);
1076 value
= yaml_document_get_node(document
, pair
->value
);
1078 if (key
->type
!= YAML_SCALAR_NODE
)
1079 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
1082 if (!strcmp((char *) key
->data
.scalar
.value
, "declared"))
1083 declared
= extract_int(ctx
, document
, value
);
1084 if (!strcmp((char *) key
->data
.scalar
.value
, "independent"))
1085 independent
= extract_int(ctx
, document
, value
);
1086 if (!strcmp((char *) key
->data
.scalar
.value
, "variable")) {
1087 iv
= extract_expr(ctx
, document
, value
);
1091 if (!strcmp((char *) key
->data
.scalar
.value
,
1092 "initialization")) {
1093 init
= extract_expr(ctx
, document
, value
);
1097 if (!strcmp((char *) key
->data
.scalar
.value
, "condition")) {
1098 cond
= extract_expr(ctx
, document
, value
);
1102 if (!strcmp((char *) key
->data
.scalar
.value
, "increment")) {
1103 inc
= extract_expr(ctx
, document
, value
);
1107 if (!strcmp((char *) key
->data
.scalar
.value
, "body")) {
1108 body
= extract_tree(ctx
, document
, value
);
1115 isl_die(ctx
, isl_error_invalid
,
1116 "no variable field", goto error
);
1118 isl_die(ctx
, isl_error_invalid
,
1119 "no initialization field", goto error
);
1121 isl_die(ctx
, isl_error_invalid
,
1122 "no condition field", goto error
);
1124 isl_die(ctx
, isl_error_invalid
,
1125 "no increment field", goto error
);
1127 isl_die(ctx
, isl_error_invalid
,
1128 "no body field", goto error
);
1130 return pet_tree_new_for(independent
, declared
, iv
, init
, cond
, inc
,
1134 pet_expr_free(init
);
1135 pet_expr_free(cond
);
1137 pet_tree_free(body
);
1141 /* Extract a pet_tree from "node".
1143 * We first extract the type of the pet_tree and then call
1144 * the appropriate function to extract and construct a pet_tree
1147 static __isl_give pet_tree
*extract_tree(isl_ctx
*ctx
,
1148 yaml_document_t
*document
, yaml_node_t
*node
)
1150 enum pet_tree_type type
= pet_tree_error
;
1152 yaml_node_pair_t
*pair
;
1154 if (node
->type
!= YAML_MAPPING_NODE
)
1155 isl_die(ctx
, isl_error_invalid
, "expecting mapping",
1158 for (pair
= node
->data
.mapping
.pairs
.start
;
1159 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
1160 yaml_node_t
*key
, *value
;
1162 key
= yaml_document_get_node(document
, pair
->key
);
1163 value
= yaml_document_get_node(document
, pair
->value
);
1165 if (key
->type
!= YAML_SCALAR_NODE
)
1166 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
1169 if (!strcmp((char *) key
->data
.scalar
.value
, "type"))
1170 type
= extract_tree_type(ctx
, document
, value
);
1173 if (type
== pet_tree_error
)
1174 isl_die(ctx
, isl_error_invalid
, "cannot determine type",
1178 case pet_tree_error
:
1180 case pet_tree_block
:
1181 tree
= extract_tree_block(ctx
, document
, node
);
1183 case pet_tree_break
:
1184 tree
= pet_tree_new_break(ctx
);
1186 case pet_tree_continue
:
1187 tree
= pet_tree_new_continue(ctx
);
1190 tree
= extract_tree_decl(ctx
, document
, node
);
1192 case pet_tree_decl_init
:
1193 tree
= extract_tree_decl_init(ctx
, document
, node
);
1196 tree
= extract_tree_expr(ctx
, document
, node
);
1198 case pet_tree_return
:
1199 tree
= extract_tree_return(ctx
, document
, node
);
1202 tree
= extract_tree_for(ctx
, document
, node
);
1204 case pet_tree_while
:
1205 tree
= extract_tree_while(ctx
, document
, node
);
1207 case pet_tree_infinite_loop
:
1208 tree
= extract_tree_infinite_loop(ctx
, document
, node
);
1211 tree
= extract_tree_if(ctx
, document
, node
);
1213 case pet_tree_if_else
:
1214 tree
= extract_tree_if_else(ctx
, document
, node
);
1221 static struct pet_stmt
*extract_stmt_arguments(isl_ctx
*ctx
,
1222 yaml_document_t
*document
, yaml_node_t
*node
, struct pet_stmt
*stmt
)
1225 yaml_node_item_t
*item
;
1227 if (node
->type
!= YAML_SEQUENCE_NODE
)
1228 isl_die(ctx
, isl_error_invalid
, "expecting sequence",
1229 return pet_stmt_free(stmt
));
1231 stmt
->n_arg
= node
->data
.sequence
.items
.top
1232 - node
->data
.sequence
.items
.start
;
1233 stmt
->args
= isl_calloc_array(ctx
, pet_expr
*, stmt
->n_arg
);
1235 return pet_stmt_free(stmt
);
1237 for (item
= node
->data
.sequence
.items
.start
, i
= 0;
1238 item
< node
->data
.sequence
.items
.top
; ++item
, ++i
) {
1241 n
= yaml_document_get_node(document
, *item
);
1242 stmt
->args
[i
] = extract_expr(ctx
, document
, n
);
1244 return pet_stmt_free(stmt
);
1250 static struct pet_stmt
*extract_stmt(isl_ctx
*ctx
, yaml_document_t
*document
,
1253 struct pet_stmt
*stmt
;
1254 yaml_node_pair_t
* pair
;
1256 unsigned start
= 0, end
= 0;
1257 char *indent
= NULL
;
1259 if (node
->type
!= YAML_MAPPING_NODE
)
1260 isl_die(ctx
, isl_error_invalid
, "expecting mapping",
1263 stmt
= isl_calloc_type(ctx
, struct pet_stmt
);
1267 stmt
->loc
= &pet_loc_dummy
;
1269 for (pair
= node
->data
.mapping
.pairs
.start
;
1270 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
1271 yaml_node_t
*key
, *value
;
1273 key
= yaml_document_get_node(document
, pair
->key
);
1274 value
= yaml_document_get_node(document
, pair
->value
);
1276 if (key
->type
!= YAML_SCALAR_NODE
)
1277 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
1278 return pet_stmt_free(stmt
));
1280 if (!strcmp((char *) key
->data
.scalar
.value
, "indent"))
1281 indent
= extract_string(ctx
, document
, value
);
1282 if (!strcmp((char *) key
->data
.scalar
.value
, "line"))
1283 line
= extract_int(ctx
, document
, value
);
1284 if (!strcmp((char *) key
->data
.scalar
.value
, "start"))
1285 start
= extract_int(ctx
, document
, value
);
1286 if (!strcmp((char *) key
->data
.scalar
.value
, "end"))
1287 end
= extract_int(ctx
, document
, value
);
1288 if (!strcmp((char *) key
->data
.scalar
.value
, "domain"))
1289 stmt
->domain
= extract_set(ctx
, document
, value
);
1290 if (!strcmp((char *) key
->data
.scalar
.value
, "body"))
1291 stmt
->body
= extract_tree(ctx
, document
, value
);
1293 if (!strcmp((char *) key
->data
.scalar
.value
, "arguments"))
1294 stmt
= extract_stmt_arguments(ctx
, document
,
1301 indent
= strdup("");
1302 stmt
->loc
= pet_loc_alloc(ctx
, start
, end
, line
, indent
);
1304 return pet_stmt_free(stmt
);
1309 static struct pet_scop
*extract_statements(isl_ctx
*ctx
,
1310 yaml_document_t
*document
, yaml_node_t
*node
, struct pet_scop
*scop
)
1313 yaml_node_item_t
*item
;
1315 if (node
->type
!= YAML_SEQUENCE_NODE
)
1316 isl_die(ctx
, isl_error_invalid
, "expecting sequence",
1319 scop
->n_stmt
= node
->data
.sequence
.items
.top
1320 - node
->data
.sequence
.items
.start
;
1321 scop
->stmts
= isl_calloc_array(ctx
, struct pet_stmt
*, scop
->n_stmt
);
1323 return pet_scop_free(scop
);
1325 for (item
= node
->data
.sequence
.items
.start
, i
= 0;
1326 item
< node
->data
.sequence
.items
.top
; ++item
, ++i
) {
1329 n
= yaml_document_get_node(document
, *item
);
1330 scop
->stmts
[i
] = extract_stmt(ctx
, document
, n
);
1331 if (!scop
->stmts
[i
])
1332 return pet_scop_free(scop
);
1338 /* Extract a pet_implication from "node".
1340 static struct pet_implication
*extract_implication(isl_ctx
*ctx
,
1341 yaml_document_t
*document
, yaml_node_t
*node
)
1343 struct pet_implication
*implication
;
1344 yaml_node_pair_t
* pair
;
1346 if (node
->type
!= YAML_MAPPING_NODE
)
1347 isl_die(ctx
, isl_error_invalid
, "expecting mapping",
1350 implication
= isl_calloc_type(ctx
, struct pet_implication
);
1354 for (pair
= node
->data
.mapping
.pairs
.start
;
1355 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
1356 yaml_node_t
*key
, *value
;
1358 key
= yaml_document_get_node(document
, pair
->key
);
1359 value
= yaml_document_get_node(document
, pair
->value
);
1361 if (key
->type
!= YAML_SCALAR_NODE
)
1362 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
1363 return pet_implication_free(implication
));
1365 if (!strcmp((char *) key
->data
.scalar
.value
, "satisfied"))
1366 implication
->satisfied
=
1367 extract_int(ctx
, document
, value
);
1368 if (!strcmp((char *) key
->data
.scalar
.value
, "extension"))
1369 implication
->extension
=
1370 extract_map(ctx
, document
, value
);
1376 /* Extract a sequence of implications from "node" and
1377 * store them in scop->implications.
1379 static struct pet_scop
*extract_implications(isl_ctx
*ctx
,
1380 yaml_document_t
*document
, yaml_node_t
*node
, struct pet_scop
*scop
)
1383 yaml_node_item_t
*item
;
1385 if (node
->type
!= YAML_SEQUENCE_NODE
)
1386 isl_die(ctx
, isl_error_invalid
, "expecting sequence",
1389 scop
->n_implication
= node
->data
.sequence
.items
.top
1390 - node
->data
.sequence
.items
.start
;
1391 scop
->implications
= isl_calloc_array(ctx
, struct pet_implication
*,
1392 scop
->n_implication
);
1393 if (!scop
->implications
)
1394 return pet_scop_free(scop
);
1396 for (item
= node
->data
.sequence
.items
.start
, i
= 0;
1397 item
< node
->data
.sequence
.items
.top
; ++item
, ++i
) {
1400 n
= yaml_document_get_node(document
, *item
);
1401 scop
->implications
[i
] = extract_implication(ctx
, document
, n
);
1402 if (!scop
->implications
[i
])
1403 return pet_scop_free(scop
);
1409 /* Extract a pet_independence from "node".
1411 static struct pet_independence
*extract_independence(isl_ctx
*ctx
,
1412 yaml_document_t
*document
, yaml_node_t
*node
)
1414 struct pet_independence
*independence
;
1415 yaml_node_pair_t
* pair
;
1417 if (node
->type
!= YAML_MAPPING_NODE
)
1418 isl_die(ctx
, isl_error_invalid
, "expecting mapping",
1421 independence
= isl_calloc_type(ctx
, struct pet_independence
);
1425 for (pair
= node
->data
.mapping
.pairs
.start
;
1426 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
1427 yaml_node_t
*key
, *value
;
1429 key
= yaml_document_get_node(document
, pair
->key
);
1430 value
= yaml_document_get_node(document
, pair
->value
);
1432 if (key
->type
!= YAML_SCALAR_NODE
)
1433 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
1434 return pet_independence_free(independence
));
1436 if (!strcmp((char *) key
->data
.scalar
.value
, "filter"))
1437 independence
->filter
=
1438 extract_union_map(ctx
, document
, value
);
1439 if (!strcmp((char *) key
->data
.scalar
.value
, "local"))
1440 independence
->local
=
1441 extract_union_set(ctx
, document
, value
);
1444 if (!independence
->filter
)
1445 isl_die(ctx
, isl_error_invalid
, "no filter field",
1446 return pet_independence_free(independence
));
1447 if (!independence
->local
)
1448 isl_die(ctx
, isl_error_invalid
, "no local field",
1449 return pet_independence_free(independence
));
1451 return independence
;
1454 /* Extract a sequence of independences from "node" and
1455 * store them in scop->independences.
1457 static struct pet_scop
*extract_independences(isl_ctx
*ctx
,
1458 yaml_document_t
*document
, yaml_node_t
*node
, struct pet_scop
*scop
)
1461 yaml_node_item_t
*item
;
1463 if (node
->type
!= YAML_SEQUENCE_NODE
)
1464 isl_die(ctx
, isl_error_invalid
, "expecting sequence",
1467 scop
->n_independence
= node
->data
.sequence
.items
.top
1468 - node
->data
.sequence
.items
.start
;
1469 scop
->independences
= isl_calloc_array(ctx
, struct pet_independence
*,
1470 scop
->n_independence
);
1471 if (!scop
->independences
)
1472 return pet_scop_free(scop
);
1474 for (item
= node
->data
.sequence
.items
.start
, i
= 0;
1475 item
< node
->data
.sequence
.items
.top
; ++item
, ++i
) {
1478 n
= yaml_document_get_node(document
, *item
);
1479 scop
->independences
[i
] = extract_independence(ctx
, document
, n
);
1480 if (!scop
->independences
[i
])
1481 return pet_scop_free(scop
);
1487 static struct pet_scop
*extract_scop(isl_ctx
*ctx
, yaml_document_t
*document
,
1490 struct pet_scop
*scop
;
1491 yaml_node_pair_t
* pair
;
1496 if (node
->type
!= YAML_MAPPING_NODE
)
1497 isl_die(ctx
, isl_error_invalid
, "expecting mapping",
1500 scop
= pet_scop_alloc(ctx
);
1504 for (pair
= node
->data
.mapping
.pairs
.start
;
1505 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
1506 yaml_node_t
*key
, *value
;
1508 key
= yaml_document_get_node(document
, pair
->key
);
1509 value
= yaml_document_get_node(document
, pair
->value
);
1511 if (key
->type
!= YAML_SCALAR_NODE
)
1512 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
1513 return pet_scop_free(scop
));
1514 if (!strcmp((char *) key
->data
.scalar
.value
, "context"))
1515 scop
->context
= extract_set(ctx
, document
, value
);
1516 if (!strcmp((char *) key
->data
.scalar
.value
, "context_value"))
1517 scop
->context_value
= extract_set(ctx
, document
, value
);
1518 if (!strcmp((char *) key
->data
.scalar
.value
, "schedule"))
1519 scop
->schedule
= extract_schedule(ctx
, document
, value
);
1520 if (!strcmp((char *) key
->data
.scalar
.value
, "types"))
1521 scop
= extract_types(ctx
, document
, value
, scop
);
1522 if (!strcmp((char *) key
->data
.scalar
.value
, "arrays"))
1523 scop
= extract_arrays(ctx
, document
, value
, scop
);
1524 if (!strcmp((char *) key
->data
.scalar
.value
, "statements"))
1525 scop
= extract_statements(ctx
, document
, value
, scop
);
1526 if (!strcmp((char *) key
->data
.scalar
.value
, "implications"))
1527 scop
= extract_implications(ctx
, document
, value
, scop
);
1528 if (!strcmp((char *) key
->data
.scalar
.value
, "independences"))
1529 scop
= extract_independences(ctx
,
1530 document
, value
, scop
);
1535 if (!scop
->context_value
) {
1536 isl_space
*space
= isl_space_params_alloc(ctx
, 0);
1537 scop
->context_value
= isl_set_universe(space
);
1538 if (!scop
->context_value
)
1539 return pet_scop_free(scop
);
1545 /* Extract a pet_scop from the YAML description in "in".
1547 struct pet_scop
*pet_scop_parse(isl_ctx
*ctx
, FILE *in
)
1549 struct pet_scop
*scop
= NULL
;
1550 yaml_parser_t parser
;
1552 yaml_document_t document
= { 0 };
1554 yaml_parser_initialize(&parser
);
1556 yaml_parser_set_input_file(&parser
, in
);
1558 if (!yaml_parser_load(&parser
, &document
))
1561 root
= yaml_document_get_root_node(&document
);
1563 scop
= extract_scop(ctx
, &document
, root
);
1565 yaml_document_delete(&document
);
1567 yaml_parser_delete(&parser
);
1571 yaml_parser_delete(&parser
);
1572 pet_scop_free(scop
);