c2pdg.cc: extract_node: extract statement name from domain instead of schedule
[ppn.git] / adg_parse.cc
blob9427077b1fbb4e370b65e2df747171b2e0602a4d
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 */
5 #include "adg_parse.h"
7 static __isl_give isl_val *extract_val(isl_ctx *ctx, yaml_document_t *document,
8 yaml_node_t *node)
10 if (node->type != YAML_SCALAR_NODE)
11 isl_die(ctx, isl_error_invalid, "expecting scalar node",
12 return NULL);
14 return isl_val_read_from_str(ctx, (char *) node->data.scalar.value);
17 static __isl_give isl_id *extract_id(isl_ctx *ctx, yaml_document_t *document,
18 yaml_node_t *node)
20 if (node->type != YAML_SCALAR_NODE)
21 isl_die(ctx, isl_error_invalid, "expecting scalar node",
22 return NULL);
24 return isl_id_alloc(ctx, (char *) node->data.scalar.value, NULL);
27 static __isl_give isl_set *extract_set(isl_ctx *ctx, yaml_document_t *document,
28 yaml_node_t *node)
30 if (node->type != YAML_SCALAR_NODE)
31 isl_die(ctx, isl_error_invalid, "expecting scalar node",
32 return NULL);
34 return isl_set_read_from_str(ctx, (char *) node->data.scalar.value);
37 static __isl_give isl_map *extract_map(isl_ctx *ctx, yaml_document_t *document,
38 yaml_node_t *node)
40 if (node->type != YAML_SCALAR_NODE)
41 isl_die(ctx, isl_error_invalid, "expecting scalar node",
42 return NULL);
44 return isl_map_read_from_str(ctx, (char *) node->data.scalar.value);
47 static __isl_give isl_aff *extract_aff(isl_ctx *ctx,
48 yaml_document_t *document, yaml_node_t *node)
50 if (node->type != YAML_SCALAR_NODE)
51 isl_die(ctx, isl_error_invalid, "expecting scalar node",
52 return NULL);
54 return isl_aff_read_from_str(ctx, (char *) node->data.scalar.value);
57 static __isl_give isl_pw_aff *extract_pw_aff(isl_ctx *ctx,
58 yaml_document_t *document, yaml_node_t *node)
60 if (node->type != YAML_SCALAR_NODE)
61 isl_die(ctx, isl_error_invalid, "expecting scalar node",
62 return NULL);
64 return isl_pw_aff_read_from_str(ctx, (char *) node->data.scalar.value);
67 static __isl_give isl_multi_aff *extract_multi_aff(isl_ctx *ctx,
68 yaml_document_t *document, yaml_node_t *node)
70 if (node->type != YAML_SCALAR_NODE)
71 isl_die(ctx, isl_error_invalid, "expecting scalar node",
72 return NULL);
74 return isl_multi_aff_read_from_str(ctx,
75 (char *) node->data.scalar.value);
78 static __isl_give isl_pw_multi_aff *extract_pw_multi_aff(isl_ctx *ctx,
79 yaml_document_t *document, yaml_node_t *node)
81 if (node->type != YAML_SCALAR_NODE)
82 isl_die(ctx, isl_error_invalid, "expecting scalar node",
83 return NULL);
85 return isl_pw_multi_aff_read_from_str(ctx,
86 (char *) node->data.scalar.value);
89 static int extract_id_list(isl_ctx *ctx, yaml_document_t *document,
90 yaml_node_t *node, std::vector<isl_id *> &list)
92 yaml_node_item_t *item;
94 if (node->type != YAML_SEQUENCE_NODE)
95 isl_die(ctx, isl_error_invalid, "expecting sequence",
96 return -1);
98 for (item = node->data.sequence.items.start;
99 item < node->data.sequence.items.top; ++item) {
100 yaml_node_t *n;
101 isl_id *id;
103 n = yaml_document_get_node(document, *item);
104 id = extract_id(ctx, document, n);
105 if (!id)
106 return -1;
107 list.push_back(id);
110 return 0;
113 static adg_var *extract_var(isl_ctx *ctx, yaml_document_t *document,
114 yaml_node_t *node)
116 adg_var *var;
117 yaml_node_pair_t *pair;
119 if (!node)
120 return NULL;
122 if (node->type != YAML_MAPPING_NODE)
123 isl_die(ctx, isl_error_invalid, "expecting mapping",
124 return NULL);
126 var = new adg_var;
127 if (!var)
128 return NULL;
130 for (pair = node->data.mapping.pairs.start;
131 pair < node->data.mapping.pairs.top; ++pair) {
132 yaml_node_t *key, *value;
134 key = yaml_document_get_node(document, pair->key);
135 value = yaml_document_get_node(document, pair->value);
137 if (key->type != YAML_SCALAR_NODE)
138 isl_die(ctx, isl_error_invalid, "expecting scalar key",
139 goto error);
140 if (!strcmp((char *) key->data.scalar.value, "access"))
141 var->access = extract_pw_multi_aff(ctx, document, value);
142 if (!strcmp((char *) key->data.scalar.value, "type"))
143 var->type = extract_id(ctx, document, value);
146 return var;
147 error:
148 if (var)
149 delete var;
150 return NULL;
153 static adg_expr *extract_expr(isl_ctx *ctx, yaml_document_t *document,
154 yaml_node_t *node)
156 adg_expr *expr;
157 yaml_node_pair_t *pair;
159 if (!node)
160 return NULL;
162 if (node->type != YAML_MAPPING_NODE)
163 isl_die(ctx, isl_error_invalid, "expecting mapping",
164 return NULL);
166 expr = new adg_expr;
167 if (!expr)
168 return NULL;
170 for (pair = node->data.mapping.pairs.start;
171 pair < node->data.mapping.pairs.top; ++pair) {
172 yaml_node_t *key, *value;
174 key = yaml_document_get_node(document, pair->key);
175 value = yaml_document_get_node(document, pair->value);
177 if (key->type != YAML_SCALAR_NODE)
178 isl_die(ctx, isl_error_invalid, "expecting scalar key",
179 goto error);
180 if (!strcmp((char *) key->data.scalar.value, "name"))
181 expr->name = extract_id(ctx, document, value);
182 if (!strcmp((char *) key->data.scalar.value, "expr"))
183 expr->expr = extract_pw_aff(ctx, document, value);
186 return expr;
187 error:
188 if (expr)
189 delete expr;
190 return NULL;
193 static int extract_var_list(isl_ctx *ctx, yaml_document_t *document,
194 yaml_node_t *node, std::vector<adg_var *> &list)
196 yaml_node_item_t *item;
198 if (node->type != YAML_SEQUENCE_NODE)
199 isl_die(ctx, isl_error_invalid, "expecting sequence",
200 return -1);
202 for (item = node->data.sequence.items.start;
203 item < node->data.sequence.items.top; ++item) {
204 yaml_node_t *n;
205 adg_var *var;
207 n = yaml_document_get_node(document, *item);
208 var = extract_var(ctx, document, n);
209 if (!var)
210 return -1;
211 list.push_back(var);
214 return 0;
217 static enum adg_arg_type extract_arg_type(isl_ctx *ctx,
218 yaml_document_t *document, yaml_node_t *node)
220 if (node->type != YAML_SCALAR_NODE)
221 isl_die(ctx, isl_error_invalid, "expecting scalar node",
222 return adg_arg_unknown);
224 if (!strcmp((char *) node->data.scalar.value, "value"))
225 return adg_arg_value;
226 if (!strcmp((char *) node->data.scalar.value, "reference"))
227 return adg_arg_reference;
228 if (!strcmp((char *) node->data.scalar.value, "return"))
229 return adg_arg_return;
231 return adg_arg_unknown;
234 static adg_arg *extract_arg(isl_ctx *ctx, yaml_document_t *document,
235 yaml_node_t *node)
237 adg_arg *arg;
238 yaml_node_pair_t *pair;
240 if (!node)
241 return NULL;
243 if (node->type != YAML_MAPPING_NODE)
244 isl_die(ctx, isl_error_invalid, "expecting mapping",
245 return NULL);
247 arg = new adg_arg;
248 if (!arg)
249 return NULL;
251 for (pair = node->data.mapping.pairs.start;
252 pair < node->data.mapping.pairs.top; ++pair) {
253 yaml_node_t *key, *value;
255 key = yaml_document_get_node(document, pair->key);
256 value = yaml_document_get_node(document, pair->value);
258 if (key->type != YAML_SCALAR_NODE)
259 isl_die(ctx, isl_error_invalid, "expecting scalar key",
260 goto error);
261 if (!strcmp((char *) key->data.scalar.value, "var"))
262 arg->var = extract_var(ctx, document, value);
263 if (!strcmp((char *) key->data.scalar.value, "type"))
264 arg->type = extract_arg_type(ctx, document, value);
267 return arg;
268 error:
269 if (arg)
270 delete arg;
271 return NULL;
274 static int extract_arg_list(isl_ctx *ctx, yaml_document_t *document,
275 yaml_node_t *node, std::vector<adg_arg *> &list)
277 yaml_node_item_t *item;
279 if (node->type != YAML_SEQUENCE_NODE)
280 isl_die(ctx, isl_error_invalid, "expecting sequence",
281 return -1);
283 for (item = node->data.sequence.items.start;
284 item < node->data.sequence.items.top; ++item) {
285 yaml_node_t *n;
286 adg_arg *arg;
288 n = yaml_document_get_node(document, *item);
289 arg = extract_arg(ctx, document, n);
290 if (!arg)
291 return -1;
292 list.push_back(arg);
295 return 0;
298 static int extract_expr_list(isl_ctx *ctx, yaml_document_t *document,
299 yaml_node_t *node, std::vector<adg_expr *> &list)
301 yaml_node_item_t *item;
303 if (node->type != YAML_SEQUENCE_NODE)
304 isl_die(ctx, isl_error_invalid, "expecting sequence",
305 return -1);
307 for (item = node->data.sequence.items.start;
308 item < node->data.sequence.items.top; ++item) {
309 yaml_node_t *n;
310 adg_expr *expr;
312 n = yaml_document_get_node(document, *item);
313 expr = extract_expr(ctx, document, n);
314 if (!expr)
315 return -1;
316 list.push_back(expr);
319 return 0;
322 static adg_domain *extract_domain(isl_ctx *ctx, yaml_document_t *document,
323 yaml_node_t *node)
325 adg_domain *domain;
326 yaml_node_pair_t *pair;
328 if (!node)
329 return NULL;
331 if (node->type != YAML_MAPPING_NODE)
332 isl_die(ctx, isl_error_invalid, "expecting mapping",
333 return NULL);
335 domain = new adg_domain;
336 if (!domain)
337 return NULL;
339 for (pair = node->data.mapping.pairs.start;
340 pair < node->data.mapping.pairs.top; ++pair) {
341 yaml_node_t *key, *value;
343 key = yaml_document_get_node(document, pair->key);
344 value = yaml_document_get_node(document, pair->value);
346 if (key->type != YAML_SCALAR_NODE)
347 isl_die(ctx, isl_error_invalid, "expecting scalar key",
348 goto error);
349 if (!strcmp((char *) key->data.scalar.value, "bounds"))
350 domain->bounds = extract_set(ctx, document, value);
351 if (!strcmp((char *) key->data.scalar.value, "controls") &&
352 extract_expr_list(ctx, document, value, domain->controls) < 0)
353 goto error;
354 if (!strcmp((char *) key->data.scalar.value, "filters") &&
355 extract_var_list(ctx, document, value, domain->filters) < 0)
356 goto error;
359 return domain;
360 error:
361 if (domain)
362 delete domain;
363 return NULL;
366 static adg_function *extract_function(isl_ctx *ctx, yaml_document_t *document,
367 yaml_node_t *node)
369 adg_function *fn;
370 yaml_node_pair_t *pair;
372 if (!node)
373 return NULL;
375 if (node->type != YAML_MAPPING_NODE)
376 isl_die(ctx, isl_error_invalid, "expecting mapping",
377 return NULL);
379 fn = new adg_function;
380 if (!fn)
381 return NULL;
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 goto error);
393 if (!strcmp((char *) key->data.scalar.value, "name"))
394 fn->name = extract_id(ctx, document, value);
395 if (!strcmp((char *) key->data.scalar.value, "in") &&
396 extract_arg_list(ctx, document, value, fn->in) < 0)
397 goto error;
398 if (!strcmp((char *) key->data.scalar.value, "out") &&
399 extract_arg_list(ctx, document, value, fn->out) < 0)
400 goto error;
401 if (!strcmp((char *) key->data.scalar.value, "ctrl") &&
402 extract_expr_list(ctx, document, value, fn->ctrl) < 0)
403 goto error;
404 if (!strcmp((char *) key->data.scalar.value, "domain"))
405 fn->domain = extract_domain(ctx, document, value);
408 return fn;
409 error:
410 if (fn)
411 delete fn;
412 return NULL;
415 static adg_port *extract_port(isl_ctx *ctx, yaml_document_t *document,
416 yaml_node_t *node)
418 adg_port *port;
419 yaml_node_pair_t *pair;
421 if (!node)
422 return NULL;
424 if (node->type != YAML_MAPPING_NODE)
425 isl_die(ctx, isl_error_invalid, "expecting mapping",
426 return NULL);
428 port = new adg_port;
429 if (!port)
430 return NULL;
432 for (pair = node->data.mapping.pairs.start;
433 pair < node->data.mapping.pairs.top; ++pair) {
434 yaml_node_t *key, *value;
436 key = yaml_document_get_node(document, pair->key);
437 value = yaml_document_get_node(document, pair->value);
439 if (key->type != YAML_SCALAR_NODE)
440 isl_die(ctx, isl_error_invalid, "expecting scalar key",
441 goto error);
442 if (!strcmp((char *) key->data.scalar.value, "name"))
443 port->name = extract_id(ctx, document, value);
444 if (!strcmp((char *) key->data.scalar.value, "node"))
445 port->node_name = extract_id(ctx, document, value);
446 if (!strcmp((char *) key->data.scalar.value, "edge"))
447 port->edge_name = extract_id(ctx, document, value);
448 if (!strcmp((char *) key->data.scalar.value, "vars") &&
449 extract_var_list(ctx, document, value, port->vars) < 0)
450 goto error;
451 if (!strcmp((char *) key->data.scalar.value, "domain"))
452 port->domain = extract_domain(ctx, document, value);
455 return port;
456 error:
457 if (port)
458 delete port;
459 return NULL;
462 static int extract_port_list(isl_ctx *ctx, yaml_document_t *document,
463 yaml_node_t *node, std::vector<adg_port *> &list)
465 yaml_node_item_t *item;
467 if (node->type != YAML_SEQUENCE_NODE)
468 isl_die(ctx, isl_error_invalid, "expecting sequence",
469 return -1);
471 for (item = node->data.sequence.items.start;
472 item < node->data.sequence.items.top; ++item) {
473 yaml_node_t *n;
474 adg_port *port;
476 n = yaml_document_get_node(document, *item);
477 port = extract_port(ctx, document, n);
478 if (!port)
479 return -1;
480 list.push_back(port);
483 return 0;
486 static adg_gate *extract_gate(isl_ctx *ctx, yaml_document_t *document,
487 yaml_node_t *node)
489 adg_gate *gate;
490 yaml_node_pair_t *pair;
492 if (!node)
493 return NULL;
495 if (node->type != YAML_MAPPING_NODE)
496 isl_die(ctx, isl_error_invalid, "expecting mapping",
497 return NULL);
499 gate = new adg_gate;
500 if (!gate)
501 return NULL;
503 for (pair = node->data.mapping.pairs.start;
504 pair < node->data.mapping.pairs.top; ++pair) {
505 yaml_node_t *key, *value;
507 key = yaml_document_get_node(document, pair->key);
508 value = yaml_document_get_node(document, pair->value);
510 if (key->type != YAML_SCALAR_NODE)
511 isl_die(ctx, isl_error_invalid, "expecting scalar key",
512 goto error);
513 if (!strcmp((char *) key->data.scalar.value, "name"))
514 gate->name = extract_id(ctx, document, value);
515 if (!strcmp((char *) key->data.scalar.value, "node"))
516 gate->node_name = extract_id(ctx, document, value);
517 if (!strcmp((char *) key->data.scalar.value, "in"))
518 gate->in = extract_var(ctx, document, value);
519 if (!strcmp((char *) key->data.scalar.value, "out"))
520 gate->out = extract_var(ctx, document, value);
521 if (!strcmp((char *) key->data.scalar.value, "domain"))
522 gate->domain = extract_domain(ctx, document, value);
525 return gate;
526 error:
527 if (gate)
528 delete gate;
529 return NULL;
532 static int extract_gate_list(isl_ctx *ctx, yaml_document_t *document,
533 yaml_node_t *node, std::vector<adg_gate *> &list)
535 yaml_node_item_t *item;
537 if (node->type != YAML_SEQUENCE_NODE)
538 isl_die(ctx, isl_error_invalid, "expecting sequence",
539 return -1);
541 for (item = node->data.sequence.items.start;
542 item < node->data.sequence.items.top; ++item) {
543 yaml_node_t *n;
544 adg_gate *gate;
546 n = yaml_document_get_node(document, *item);
547 gate = extract_gate(ctx, document, n);
548 if (!gate)
549 return -1;
550 list.push_back(gate);
553 return 0;
556 static adg_node *extract_node(isl_ctx *ctx, yaml_document_t *document,
557 yaml_node_t *node)
559 adg_node *anode;
560 yaml_node_pair_t *pair;
562 if (!node)
563 return NULL;
565 if (node->type != YAML_MAPPING_NODE)
566 isl_die(ctx, isl_error_invalid, "expecting mapping",
567 return NULL);
569 anode = new adg_node;
570 if (!anode)
571 return NULL;
573 for (pair = node->data.mapping.pairs.start;
574 pair < node->data.mapping.pairs.top; ++pair) {
575 yaml_node_t *key, *value;
577 key = yaml_document_get_node(document, pair->key);
578 value = yaml_document_get_node(document, pair->value);
580 if (key->type != YAML_SCALAR_NODE)
581 isl_die(ctx, isl_error_invalid, "expecting scalar key",
582 goto error);
583 if (!strcmp((char *) key->data.scalar.value, "name"))
584 anode->name = extract_id(ctx, document, value);
585 if (!strcmp((char *) key->data.scalar.value, "function"))
586 anode->function = extract_function(ctx, document, value);
587 if (!strcmp((char *) key->data.scalar.value, "domain"))
588 anode->domain = extract_domain(ctx, document, value);
589 if (!strcmp((char *) key->data.scalar.value, "schedule"))
590 anode->schedule = extract_map(ctx, document, value);
591 if (!strcmp((char *) key->data.scalar.value, "input_ports") &&
592 extract_port_list(ctx, document, value,
593 anode->input_ports) < 0)
594 goto error;
595 if (!strcmp((char *) key->data.scalar.value, "output_ports") &&
596 extract_port_list(ctx, document, value,
597 anode->output_ports) < 0)
598 goto error;
599 if (!strcmp((char *) key->data.scalar.value, "gates") &&
600 extract_gate_list(ctx, document, value, anode->gates) < 0)
601 goto error;
602 if (!strcmp((char *) key->data.scalar.value, "expressions") &&
603 extract_expr_list(ctx, document, value,
604 anode->expressions) < 0)
605 goto error;
608 return anode;
609 error:
610 if (anode)
611 delete anode;
612 return NULL;
615 static int extract_node_list(isl_ctx *ctx, yaml_document_t *document,
616 yaml_node_t *node, std::vector<adg_node *> &list)
618 yaml_node_item_t *item;
620 if (node->type != YAML_SEQUENCE_NODE)
621 isl_die(ctx, isl_error_invalid, "expecting sequence",
622 return -1);
624 for (item = node->data.sequence.items.start;
625 item < node->data.sequence.items.top; ++item) {
626 yaml_node_t *n;
627 adg_node *node;
629 n = yaml_document_get_node(document, *item);
630 node = extract_node(ctx, document, n);
631 if (!node)
632 return -1;
633 list.push_back(node);
636 return 0;
639 static enum adg_edge_type extract_type(isl_ctx *ctx,
640 yaml_document_t *document, yaml_node_t *node)
642 if (node->type != YAML_SCALAR_NODE)
643 isl_die(ctx, isl_error_invalid, "expecting scalar node",
644 return adg_edge_error);
646 if (!strcmp((char *) node->data.scalar.value, "fifo"))
647 return adg_edge_fifo;
648 if (!strcmp((char *) node->data.scalar.value, "sticky fifo"))
649 return adg_edge_sticky_fifo;
650 if (!strcmp((char *) node->data.scalar.value, "shift register"))
651 return adg_edge_shift_register;
652 if (!strcmp((char *) node->data.scalar.value, "broadcast"))
653 return adg_edge_broadcast;
654 if (!strcmp((char *) node->data.scalar.value, "generic"))
655 return adg_edge_generic;
657 return adg_edge_error;
660 static adg_edge *extract_edge(isl_ctx *ctx, yaml_document_t *document,
661 yaml_node_t *node)
663 adg_edge *edge;
664 yaml_node_pair_t *pair;
666 if (!node)
667 return NULL;
669 if (node->type != YAML_MAPPING_NODE)
670 isl_die(ctx, isl_error_invalid, "expecting mapping",
671 return NULL);
673 edge = new adg_edge;
674 if (!edge)
675 return NULL;
677 for (pair = node->data.mapping.pairs.start;
678 pair < node->data.mapping.pairs.top; ++pair) {
679 yaml_node_t *key, *value;
681 key = yaml_document_get_node(document, pair->key);
682 value = yaml_document_get_node(document, pair->value);
684 if (key->type != YAML_SCALAR_NODE)
685 isl_die(ctx, isl_error_invalid, "expecting scalar key",
686 goto error);
687 if (!strcmp((char *) key->data.scalar.value, "name"))
688 edge->name = extract_id(ctx, document, value);
689 if (!strcmp((char *) key->data.scalar.value, "map"))
690 edge->map = extract_multi_aff(ctx, document, value);
691 if (!strcmp((char *) key->data.scalar.value, "from_node"))
692 edge->from_node_name = extract_id(ctx, document, value);
693 if (!strcmp((char *) key->data.scalar.value, "from_port"))
694 edge->from_port_name = extract_id(ctx, document, value);
695 if (!strcmp((char *) key->data.scalar.value, "to_node"))
696 edge->to_node_name = extract_id(ctx, document, value);
697 if (!strcmp((char *) key->data.scalar.value, "to_port"))
698 edge->to_port_name = extract_id(ctx, document, value);
699 if (!strcmp((char *) key->data.scalar.value, "value_size"))
700 edge->value_size = extract_val(ctx, document, value);
701 if (!strcmp((char *) key->data.scalar.value, "type"))
702 edge->type = extract_type(ctx, document, value);
705 return edge;
706 error:
707 if (edge)
708 delete edge;
709 return NULL;
712 static int extract_edge_list(isl_ctx *ctx, yaml_document_t *document,
713 yaml_node_t *node, std::vector<adg_edge *> &list)
715 yaml_node_item_t *item;
717 if (node->type != YAML_SEQUENCE_NODE)
718 isl_die(ctx, isl_error_invalid, "expecting sequence",
719 return -1);
721 for (item = node->data.sequence.items.start;
722 item < node->data.sequence.items.top; ++item) {
723 yaml_node_t *n;
724 adg_edge *edge;
726 n = yaml_document_get_node(document, *item);
727 edge = extract_edge(ctx, document, n);
728 if (!edge)
729 return -1;
730 list.push_back(edge);
733 return 0;
736 static adg_param *extract_param(isl_ctx *ctx, yaml_document_t *document,
737 yaml_node_t *node)
739 adg_param *param;
740 yaml_node_pair_t *pair;
742 if (!node)
743 return NULL;
745 if (node->type != YAML_MAPPING_NODE)
746 isl_die(ctx, isl_error_invalid, "expecting mapping",
747 return NULL);
749 param = new adg_param;
750 if (!param)
751 return NULL;
753 for (pair = node->data.mapping.pairs.start;
754 pair < node->data.mapping.pairs.top; ++pair) {
755 yaml_node_t *key, *value;
757 key = yaml_document_get_node(document, pair->key);
758 value = yaml_document_get_node(document, pair->value);
760 if (key->type != YAML_SCALAR_NODE)
761 isl_die(ctx, isl_error_invalid, "expecting scalar key",
762 goto error);
763 if (!strcmp((char *) key->data.scalar.value, "name"))
764 param->name = extract_id(ctx, document, value);
765 if (!strcmp((char *) key->data.scalar.value, "lb"))
766 param->lb = extract_aff(ctx, document, value);
767 if (!strcmp((char *) key->data.scalar.value, "ub"))
768 param->ub = extract_aff(ctx, document, value);
769 if (!strcmp((char *) key->data.scalar.value, "val"))
770 param->val = extract_aff(ctx, document, value);
773 return param;
774 error:
775 if (param)
776 delete param;
777 return NULL;
780 static int extract_param_list(isl_ctx *ctx, yaml_document_t *document,
781 yaml_node_t *node, std::vector<adg_param *> &list)
783 yaml_node_item_t *item;
785 if (node->type != YAML_SEQUENCE_NODE)
786 isl_die(ctx, isl_error_invalid, "expecting sequence",
787 return -1);
789 for (item = node->data.sequence.items.start;
790 item < node->data.sequence.items.top; ++item) {
791 yaml_node_t *n;
792 adg_param *param;
794 n = yaml_document_get_node(document, *item);
795 param = extract_param(ctx, document, n);
796 if (!param)
797 return -1;
798 list.push_back(param);
801 return 0;
804 static adg *extract_adg(isl_ctx *ctx, yaml_document_t *document,
805 yaml_node_t *node)
807 adg *adg;
808 yaml_node_pair_t *pair;
810 if (!node)
811 return NULL;
813 if (node->type != YAML_MAPPING_NODE)
814 isl_die(ctx, isl_error_invalid, "expecting mapping",
815 return NULL);
817 adg = new ::adg;
818 if (!adg)
819 return NULL;
821 for (pair = node->data.mapping.pairs.start;
822 pair < node->data.mapping.pairs.top; ++pair) {
823 yaml_node_t *key, *value;
825 key = yaml_document_get_node(document, pair->key);
826 value = yaml_document_get_node(document, pair->value);
828 if (key->type != YAML_SCALAR_NODE)
829 isl_die(ctx, isl_error_invalid, "expecting scalar key",
830 goto error);
831 if (!strcmp((char *) key->data.scalar.value, "name"))
832 adg->name = extract_id(ctx, document, value);
833 if (!strcmp((char *) key->data.scalar.value, "context"))
834 adg->context = extract_set(ctx, document, value);
835 if (!strcmp((char *) key->data.scalar.value, "iterator_map"))
836 adg->iterator_map = extract_map(ctx, document, value);
837 if (!strcmp((char *) key->data.scalar.value, "iterators") &&
838 extract_id_list(ctx, document, value, adg->iterators) < 0)
839 goto error;
840 if (!strcmp((char *) key->data.scalar.value, "all_iterators") &&
841 extract_id_list(ctx, document, value, adg->all_iterators) < 0)
842 goto error;
843 if (!strcmp((char *) key->data.scalar.value, "nodes") &&
844 extract_node_list(ctx, document, value, adg->nodes) < 0)
845 goto error;
846 if (!strcmp((char *) key->data.scalar.value, "edges") &&
847 extract_edge_list(ctx, document, value, adg->edges) < 0)
848 goto error;
849 if (!strcmp((char *) key->data.scalar.value, "params") &&
850 extract_param_list(ctx, document, value, adg->params) < 0)
851 goto error;
854 return adg;
855 error:
856 if (adg)
857 delete adg;
858 return NULL;
861 /* Extract an adg from the YAML description in "in".
863 adg *adg_parse(isl_ctx *ctx, FILE *in)
865 adg *adg = NULL;
866 yaml_parser_t parser;
867 yaml_node_t *root;
868 yaml_document_t document = { 0 };
870 yaml_parser_initialize(&parser);
872 yaml_parser_set_input_file(&parser, in);
874 if (!yaml_parser_load(&parser, &document))
875 goto error;
877 root = yaml_document_get_root_node(&document);
879 adg = extract_adg(ctx, &document, root);
881 yaml_document_delete(&document);
883 yaml_parser_delete(&parser);
885 return adg;
886 error:
887 yaml_parser_delete(&parser);
888 return NULL;