da.cc: fix typos in comments
[ppn.git] / adg_parse.cc
blobd0c5864b032d2ddac587c9f142c13c06764de293
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 */
5 #include "adg_parse.h"
7 static int extract_isl_int(isl_ctx *ctx, yaml_document_t *document,
8 yaml_node_t *node, isl_int &v)
10 if (node->type != YAML_SCALAR_NODE)
11 isl_die(ctx, isl_error_invalid, "expecting scalar node",
12 return -1);
14 isl_int_read(v, (char *) node->data.scalar.value);
16 return 0;
19 static __isl_give isl_id *extract_id(isl_ctx *ctx, yaml_document_t *document,
20 yaml_node_t *node)
22 if (node->type != YAML_SCALAR_NODE)
23 isl_die(ctx, isl_error_invalid, "expecting scalar node",
24 return NULL);
26 return isl_id_alloc(ctx, (char *) node->data.scalar.value, NULL);
29 static __isl_give isl_set *extract_set(isl_ctx *ctx, yaml_document_t *document,
30 yaml_node_t *node)
32 if (node->type != YAML_SCALAR_NODE)
33 isl_die(ctx, isl_error_invalid, "expecting scalar node",
34 return NULL);
36 return isl_set_read_from_str(ctx, (char *) node->data.scalar.value);
39 static __isl_give isl_map *extract_map(isl_ctx *ctx, yaml_document_t *document,
40 yaml_node_t *node)
42 if (node->type != YAML_SCALAR_NODE)
43 isl_die(ctx, isl_error_invalid, "expecting scalar node",
44 return NULL);
46 return isl_map_read_from_str(ctx, (char *) node->data.scalar.value);
49 static __isl_give isl_aff *extract_aff(isl_ctx *ctx,
50 yaml_document_t *document, yaml_node_t *node)
52 if (node->type != YAML_SCALAR_NODE)
53 isl_die(ctx, isl_error_invalid, "expecting scalar node",
54 return NULL);
56 return isl_aff_read_from_str(ctx, (char *) node->data.scalar.value);
59 static __isl_give isl_pw_aff *extract_pw_aff(isl_ctx *ctx,
60 yaml_document_t *document, yaml_node_t *node)
62 if (node->type != YAML_SCALAR_NODE)
63 isl_die(ctx, isl_error_invalid, "expecting scalar node",
64 return NULL);
66 return isl_pw_aff_read_from_str(ctx, (char *) node->data.scalar.value);
69 static __isl_give isl_multi_aff *extract_multi_aff(isl_ctx *ctx,
70 yaml_document_t *document, yaml_node_t *node)
72 if (node->type != YAML_SCALAR_NODE)
73 isl_die(ctx, isl_error_invalid, "expecting scalar node",
74 return NULL);
76 return isl_multi_aff_read_from_str(ctx,
77 (char *) node->data.scalar.value);
80 static __isl_give isl_pw_multi_aff *extract_pw_multi_aff(isl_ctx *ctx,
81 yaml_document_t *document, yaml_node_t *node)
83 if (node->type != YAML_SCALAR_NODE)
84 isl_die(ctx, isl_error_invalid, "expecting scalar node",
85 return NULL);
87 return isl_pw_multi_aff_read_from_str(ctx,
88 (char *) node->data.scalar.value);
91 static int extract_id_list(isl_ctx *ctx, yaml_document_t *document,
92 yaml_node_t *node, std::vector<isl_id *> &list)
94 yaml_node_item_t *item;
96 if (node->type != YAML_SEQUENCE_NODE)
97 isl_die(ctx, isl_error_invalid, "expecting sequence",
98 return -1);
100 for (item = node->data.sequence.items.start;
101 item < node->data.sequence.items.top; ++item) {
102 yaml_node_t *n;
103 isl_id *id;
105 n = yaml_document_get_node(document, *item);
106 id = extract_id(ctx, document, n);
107 if (!id)
108 return -1;
109 list.push_back(id);
112 return 0;
115 static adg_var *extract_var(isl_ctx *ctx, yaml_document_t *document,
116 yaml_node_t *node)
118 adg_var *var;
119 yaml_node_pair_t *pair;
121 if (!node)
122 return NULL;
124 if (node->type != YAML_MAPPING_NODE)
125 isl_die(ctx, isl_error_invalid, "expecting mapping",
126 return NULL);
128 var = new adg_var;
129 if (!var)
130 return NULL;
132 for (pair = node->data.mapping.pairs.start;
133 pair < node->data.mapping.pairs.top; ++pair) {
134 yaml_node_t *key, *value;
136 key = yaml_document_get_node(document, pair->key);
137 value = yaml_document_get_node(document, pair->value);
139 if (key->type != YAML_SCALAR_NODE)
140 isl_die(ctx, isl_error_invalid, "expecting scalar key",
141 goto error);
142 if (!strcmp((char *) key->data.scalar.value, "access"))
143 var->access = extract_pw_multi_aff(ctx, document, value);
144 if (!strcmp((char *) key->data.scalar.value, "type"))
145 var->type = extract_id(ctx, document, value);
148 return var;
149 error:
150 if (var)
151 delete var;
152 return NULL;
155 static adg_expr *extract_expr(isl_ctx *ctx, yaml_document_t *document,
156 yaml_node_t *node)
158 adg_expr *expr;
159 yaml_node_pair_t *pair;
161 if (!node)
162 return NULL;
164 if (node->type != YAML_MAPPING_NODE)
165 isl_die(ctx, isl_error_invalid, "expecting mapping",
166 return NULL);
168 expr = new adg_expr;
169 if (!expr)
170 return NULL;
172 for (pair = node->data.mapping.pairs.start;
173 pair < node->data.mapping.pairs.top; ++pair) {
174 yaml_node_t *key, *value;
176 key = yaml_document_get_node(document, pair->key);
177 value = yaml_document_get_node(document, pair->value);
179 if (key->type != YAML_SCALAR_NODE)
180 isl_die(ctx, isl_error_invalid, "expecting scalar key",
181 goto error);
182 if (!strcmp((char *) key->data.scalar.value, "name"))
183 expr->name = extract_id(ctx, document, value);
184 if (!strcmp((char *) key->data.scalar.value, "expr"))
185 expr->expr = extract_pw_aff(ctx, document, value);
188 return expr;
189 error:
190 if (expr)
191 delete expr;
192 return NULL;
195 static int extract_var_list(isl_ctx *ctx, yaml_document_t *document,
196 yaml_node_t *node, std::vector<adg_var *> &list)
198 yaml_node_item_t *item;
200 if (node->type != YAML_SEQUENCE_NODE)
201 isl_die(ctx, isl_error_invalid, "expecting sequence",
202 return -1);
204 for (item = node->data.sequence.items.start;
205 item < node->data.sequence.items.top; ++item) {
206 yaml_node_t *n;
207 adg_var *var;
209 n = yaml_document_get_node(document, *item);
210 var = extract_var(ctx, document, n);
211 if (!var)
212 return -1;
213 list.push_back(var);
216 return 0;
219 static enum adg_arg_type extract_arg_type(isl_ctx *ctx,
220 yaml_document_t *document, yaml_node_t *node)
222 if (node->type != YAML_SCALAR_NODE)
223 isl_die(ctx, isl_error_invalid, "expecting scalar node",
224 return adg_arg_unknown);
226 if (!strcmp((char *) node->data.scalar.value, "value"))
227 return adg_arg_value;
228 if (!strcmp((char *) node->data.scalar.value, "reference"))
229 return adg_arg_reference;
230 if (!strcmp((char *) node->data.scalar.value, "return"))
231 return adg_arg_return;
233 return adg_arg_unknown;
236 static adg_arg *extract_arg(isl_ctx *ctx, yaml_document_t *document,
237 yaml_node_t *node)
239 adg_arg *arg;
240 yaml_node_pair_t *pair;
242 if (!node)
243 return NULL;
245 if (node->type != YAML_MAPPING_NODE)
246 isl_die(ctx, isl_error_invalid, "expecting mapping",
247 return NULL);
249 arg = new adg_arg;
250 if (!arg)
251 return NULL;
253 for (pair = node->data.mapping.pairs.start;
254 pair < node->data.mapping.pairs.top; ++pair) {
255 yaml_node_t *key, *value;
257 key = yaml_document_get_node(document, pair->key);
258 value = yaml_document_get_node(document, pair->value);
260 if (key->type != YAML_SCALAR_NODE)
261 isl_die(ctx, isl_error_invalid, "expecting scalar key",
262 goto error);
263 if (!strcmp((char *) key->data.scalar.value, "var"))
264 arg->var = extract_var(ctx, document, value);
265 if (!strcmp((char *) key->data.scalar.value, "type"))
266 arg->type = extract_arg_type(ctx, document, value);
269 return arg;
270 error:
271 if (arg)
272 delete arg;
273 return NULL;
276 static int extract_arg_list(isl_ctx *ctx, yaml_document_t *document,
277 yaml_node_t *node, std::vector<adg_arg *> &list)
279 yaml_node_item_t *item;
281 if (node->type != YAML_SEQUENCE_NODE)
282 isl_die(ctx, isl_error_invalid, "expecting sequence",
283 return -1);
285 for (item = node->data.sequence.items.start;
286 item < node->data.sequence.items.top; ++item) {
287 yaml_node_t *n;
288 adg_arg *arg;
290 n = yaml_document_get_node(document, *item);
291 arg = extract_arg(ctx, document, n);
292 if (!arg)
293 return -1;
294 list.push_back(arg);
297 return 0;
300 static int extract_expr_list(isl_ctx *ctx, yaml_document_t *document,
301 yaml_node_t *node, std::vector<adg_expr *> &list)
303 yaml_node_item_t *item;
305 if (node->type != YAML_SEQUENCE_NODE)
306 isl_die(ctx, isl_error_invalid, "expecting sequence",
307 return -1);
309 for (item = node->data.sequence.items.start;
310 item < node->data.sequence.items.top; ++item) {
311 yaml_node_t *n;
312 adg_expr *expr;
314 n = yaml_document_get_node(document, *item);
315 expr = extract_expr(ctx, document, n);
316 if (!expr)
317 return -1;
318 list.push_back(expr);
321 return 0;
324 static adg_domain *extract_domain(isl_ctx *ctx, yaml_document_t *document,
325 yaml_node_t *node)
327 adg_domain *domain;
328 yaml_node_pair_t *pair;
330 if (!node)
331 return NULL;
333 if (node->type != YAML_MAPPING_NODE)
334 isl_die(ctx, isl_error_invalid, "expecting mapping",
335 return NULL);
337 domain = new adg_domain;
338 if (!domain)
339 return NULL;
341 for (pair = node->data.mapping.pairs.start;
342 pair < node->data.mapping.pairs.top; ++pair) {
343 yaml_node_t *key, *value;
345 key = yaml_document_get_node(document, pair->key);
346 value = yaml_document_get_node(document, pair->value);
348 if (key->type != YAML_SCALAR_NODE)
349 isl_die(ctx, isl_error_invalid, "expecting scalar key",
350 goto error);
351 if (!strcmp((char *) key->data.scalar.value, "bounds"))
352 domain->bounds = extract_set(ctx, document, value);
353 if (!strcmp((char *) key->data.scalar.value, "controls") &&
354 extract_expr_list(ctx, document, value, domain->controls) < 0)
355 goto error;
356 if (!strcmp((char *) key->data.scalar.value, "filters") &&
357 extract_var_list(ctx, document, value, domain->filters) < 0)
358 goto error;
361 return domain;
362 error:
363 if (domain)
364 delete domain;
365 return NULL;
368 static adg_function *extract_function(isl_ctx *ctx, yaml_document_t *document,
369 yaml_node_t *node)
371 adg_function *fn;
372 yaml_node_pair_t *pair;
374 if (!node)
375 return NULL;
377 if (node->type != YAML_MAPPING_NODE)
378 isl_die(ctx, isl_error_invalid, "expecting mapping",
379 return NULL);
381 fn = new adg_function;
382 if (!fn)
383 return NULL;
385 for (pair = node->data.mapping.pairs.start;
386 pair < node->data.mapping.pairs.top; ++pair) {
387 yaml_node_t *key, *value;
389 key = yaml_document_get_node(document, pair->key);
390 value = yaml_document_get_node(document, pair->value);
392 if (key->type != YAML_SCALAR_NODE)
393 isl_die(ctx, isl_error_invalid, "expecting scalar key",
394 goto error);
395 if (!strcmp((char *) key->data.scalar.value, "name"))
396 fn->name = extract_id(ctx, document, value);
397 if (!strcmp((char *) key->data.scalar.value, "in") &&
398 extract_arg_list(ctx, document, value, fn->in) < 0)
399 goto error;
400 if (!strcmp((char *) key->data.scalar.value, "out") &&
401 extract_arg_list(ctx, document, value, fn->out) < 0)
402 goto error;
403 if (!strcmp((char *) key->data.scalar.value, "ctrl") &&
404 extract_expr_list(ctx, document, value, fn->ctrl) < 0)
405 goto error;
406 if (!strcmp((char *) key->data.scalar.value, "domain"))
407 fn->domain = extract_domain(ctx, document, value);
410 return fn;
411 error:
412 if (fn)
413 delete fn;
414 return NULL;
417 static adg_port *extract_port(isl_ctx *ctx, yaml_document_t *document,
418 yaml_node_t *node)
420 adg_port *port;
421 yaml_node_pair_t *pair;
423 if (!node)
424 return NULL;
426 if (node->type != YAML_MAPPING_NODE)
427 isl_die(ctx, isl_error_invalid, "expecting mapping",
428 return NULL);
430 port = new adg_port;
431 if (!port)
432 return NULL;
434 for (pair = node->data.mapping.pairs.start;
435 pair < node->data.mapping.pairs.top; ++pair) {
436 yaml_node_t *key, *value;
438 key = yaml_document_get_node(document, pair->key);
439 value = yaml_document_get_node(document, pair->value);
441 if (key->type != YAML_SCALAR_NODE)
442 isl_die(ctx, isl_error_invalid, "expecting scalar key",
443 goto error);
444 if (!strcmp((char *) key->data.scalar.value, "name"))
445 port->name = extract_id(ctx, document, value);
446 if (!strcmp((char *) key->data.scalar.value, "node"))
447 port->node_name = extract_id(ctx, document, value);
448 if (!strcmp((char *) key->data.scalar.value, "edge"))
449 port->edge_name = extract_id(ctx, document, value);
450 if (!strcmp((char *) key->data.scalar.value, "vars") &&
451 extract_var_list(ctx, document, value, port->vars) < 0)
452 goto error;
453 if (!strcmp((char *) key->data.scalar.value, "domain"))
454 port->domain = extract_domain(ctx, document, value);
457 return port;
458 error:
459 if (port)
460 delete port;
461 return NULL;
464 static int extract_port_list(isl_ctx *ctx, yaml_document_t *document,
465 yaml_node_t *node, std::vector<adg_port *> &list)
467 yaml_node_item_t *item;
469 if (node->type != YAML_SEQUENCE_NODE)
470 isl_die(ctx, isl_error_invalid, "expecting sequence",
471 return -1);
473 for (item = node->data.sequence.items.start;
474 item < node->data.sequence.items.top; ++item) {
475 yaml_node_t *n;
476 adg_port *port;
478 n = yaml_document_get_node(document, *item);
479 port = extract_port(ctx, document, n);
480 if (!port)
481 return -1;
482 list.push_back(port);
485 return 0;
488 static adg_gate *extract_gate(isl_ctx *ctx, yaml_document_t *document,
489 yaml_node_t *node)
491 adg_gate *gate;
492 yaml_node_pair_t *pair;
494 if (!node)
495 return NULL;
497 if (node->type != YAML_MAPPING_NODE)
498 isl_die(ctx, isl_error_invalid, "expecting mapping",
499 return NULL);
501 gate = new adg_gate;
502 if (!gate)
503 return NULL;
505 for (pair = node->data.mapping.pairs.start;
506 pair < node->data.mapping.pairs.top; ++pair) {
507 yaml_node_t *key, *value;
509 key = yaml_document_get_node(document, pair->key);
510 value = yaml_document_get_node(document, pair->value);
512 if (key->type != YAML_SCALAR_NODE)
513 isl_die(ctx, isl_error_invalid, "expecting scalar key",
514 goto error);
515 if (!strcmp((char *) key->data.scalar.value, "name"))
516 gate->name = extract_id(ctx, document, value);
517 if (!strcmp((char *) key->data.scalar.value, "node"))
518 gate->node_name = extract_id(ctx, document, value);
519 if (!strcmp((char *) key->data.scalar.value, "in"))
520 gate->in = extract_var(ctx, document, value);
521 if (!strcmp((char *) key->data.scalar.value, "out"))
522 gate->out = extract_var(ctx, document, value);
523 if (!strcmp((char *) key->data.scalar.value, "domain"))
524 gate->domain = extract_domain(ctx, document, value);
527 return gate;
528 error:
529 if (gate)
530 delete gate;
531 return NULL;
534 static int extract_gate_list(isl_ctx *ctx, yaml_document_t *document,
535 yaml_node_t *node, std::vector<adg_gate *> &list)
537 yaml_node_item_t *item;
539 if (node->type != YAML_SEQUENCE_NODE)
540 isl_die(ctx, isl_error_invalid, "expecting sequence",
541 return -1);
543 for (item = node->data.sequence.items.start;
544 item < node->data.sequence.items.top; ++item) {
545 yaml_node_t *n;
546 adg_gate *gate;
548 n = yaml_document_get_node(document, *item);
549 gate = extract_gate(ctx, document, n);
550 if (!gate)
551 return -1;
552 list.push_back(gate);
555 return 0;
558 static adg_node *extract_node(isl_ctx *ctx, yaml_document_t *document,
559 yaml_node_t *node)
561 adg_node *anode;
562 yaml_node_pair_t *pair;
564 if (!node)
565 return NULL;
567 if (node->type != YAML_MAPPING_NODE)
568 isl_die(ctx, isl_error_invalid, "expecting mapping",
569 return NULL);
571 anode = new adg_node;
572 if (!anode)
573 return NULL;
575 for (pair = node->data.mapping.pairs.start;
576 pair < node->data.mapping.pairs.top; ++pair) {
577 yaml_node_t *key, *value;
579 key = yaml_document_get_node(document, pair->key);
580 value = yaml_document_get_node(document, pair->value);
582 if (key->type != YAML_SCALAR_NODE)
583 isl_die(ctx, isl_error_invalid, "expecting scalar key",
584 goto error);
585 if (!strcmp((char *) key->data.scalar.value, "name"))
586 anode->name = extract_id(ctx, document, value);
587 if (!strcmp((char *) key->data.scalar.value, "function"))
588 anode->function = extract_function(ctx, document, value);
589 if (!strcmp((char *) key->data.scalar.value, "domain"))
590 anode->domain = extract_domain(ctx, document, value);
591 if (!strcmp((char *) key->data.scalar.value, "schedule"))
592 anode->schedule = extract_map(ctx, document, value);
593 if (!strcmp((char *) key->data.scalar.value, "input_ports") &&
594 extract_port_list(ctx, document, value,
595 anode->input_ports) < 0)
596 goto error;
597 if (!strcmp((char *) key->data.scalar.value, "output_ports") &&
598 extract_port_list(ctx, document, value,
599 anode->output_ports) < 0)
600 goto error;
601 if (!strcmp((char *) key->data.scalar.value, "gates") &&
602 extract_gate_list(ctx, document, value, anode->gates) < 0)
603 goto error;
604 if (!strcmp((char *) key->data.scalar.value, "expressions") &&
605 extract_expr_list(ctx, document, value,
606 anode->expressions) < 0)
607 goto error;
610 return anode;
611 error:
612 if (anode)
613 delete anode;
614 return NULL;
617 static int extract_node_list(isl_ctx *ctx, yaml_document_t *document,
618 yaml_node_t *node, std::vector<adg_node *> &list)
620 yaml_node_item_t *item;
622 if (node->type != YAML_SEQUENCE_NODE)
623 isl_die(ctx, isl_error_invalid, "expecting sequence",
624 return -1);
626 for (item = node->data.sequence.items.start;
627 item < node->data.sequence.items.top; ++item) {
628 yaml_node_t *n;
629 adg_node *node;
631 n = yaml_document_get_node(document, *item);
632 node = extract_node(ctx, document, n);
633 if (!node)
634 return -1;
635 list.push_back(node);
638 return 0;
641 static enum adg_edge_type extract_type(isl_ctx *ctx,
642 yaml_document_t *document, yaml_node_t *node)
644 if (node->type != YAML_SCALAR_NODE)
645 isl_die(ctx, isl_error_invalid, "expecting scalar node",
646 return adg_edge_error);
648 if (!strcmp((char *) node->data.scalar.value, "fifo"))
649 return adg_edge_fifo;
650 if (!strcmp((char *) node->data.scalar.value, "sticky fifo"))
651 return adg_edge_sticky_fifo;
652 if (!strcmp((char *) node->data.scalar.value, "shift register"))
653 return adg_edge_shift_register;
654 if (!strcmp((char *) node->data.scalar.value, "broadcast"))
655 return adg_edge_broadcast;
656 if (!strcmp((char *) node->data.scalar.value, "generic"))
657 return adg_edge_generic;
659 return adg_edge_error;
662 static adg_edge *extract_edge(isl_ctx *ctx, yaml_document_t *document,
663 yaml_node_t *node)
665 adg_edge *edge;
666 yaml_node_pair_t *pair;
668 if (!node)
669 return NULL;
671 if (node->type != YAML_MAPPING_NODE)
672 isl_die(ctx, isl_error_invalid, "expecting mapping",
673 return NULL);
675 edge = new adg_edge;
676 if (!edge)
677 return NULL;
679 for (pair = node->data.mapping.pairs.start;
680 pair < node->data.mapping.pairs.top; ++pair) {
681 yaml_node_t *key, *value;
683 key = yaml_document_get_node(document, pair->key);
684 value = yaml_document_get_node(document, pair->value);
686 if (key->type != YAML_SCALAR_NODE)
687 isl_die(ctx, isl_error_invalid, "expecting scalar key",
688 goto error);
689 if (!strcmp((char *) key->data.scalar.value, "name"))
690 edge->name = extract_id(ctx, document, value);
691 if (!strcmp((char *) key->data.scalar.value, "map"))
692 edge->map = extract_multi_aff(ctx, document, value);
693 if (!strcmp((char *) key->data.scalar.value, "from_node"))
694 edge->from_node_name = extract_id(ctx, document, value);
695 if (!strcmp((char *) key->data.scalar.value, "from_port"))
696 edge->from_port_name = extract_id(ctx, document, value);
697 if (!strcmp((char *) key->data.scalar.value, "to_node"))
698 edge->to_node_name = extract_id(ctx, document, value);
699 if (!strcmp((char *) key->data.scalar.value, "to_port"))
700 edge->to_port_name = extract_id(ctx, document, value);
701 if (!strcmp((char *) key->data.scalar.value, "value_size") &&
702 extract_isl_int(ctx, document, value, edge->value_size) < 0)
703 goto error;
704 if (!strcmp((char *) key->data.scalar.value, "type"))
705 edge->type = extract_type(ctx, document, value);
708 return edge;
709 error:
710 if (edge)
711 delete edge;
712 return NULL;
715 static int extract_edge_list(isl_ctx *ctx, yaml_document_t *document,
716 yaml_node_t *node, std::vector<adg_edge *> &list)
718 yaml_node_item_t *item;
720 if (node->type != YAML_SEQUENCE_NODE)
721 isl_die(ctx, isl_error_invalid, "expecting sequence",
722 return -1);
724 for (item = node->data.sequence.items.start;
725 item < node->data.sequence.items.top; ++item) {
726 yaml_node_t *n;
727 adg_edge *edge;
729 n = yaml_document_get_node(document, *item);
730 edge = extract_edge(ctx, document, n);
731 if (!edge)
732 return -1;
733 list.push_back(edge);
736 return 0;
739 static adg_param *extract_param(isl_ctx *ctx, yaml_document_t *document,
740 yaml_node_t *node)
742 adg_param *param;
743 yaml_node_pair_t *pair;
745 if (!node)
746 return NULL;
748 if (node->type != YAML_MAPPING_NODE)
749 isl_die(ctx, isl_error_invalid, "expecting mapping",
750 return NULL);
752 param = new adg_param;
753 if (!param)
754 return NULL;
756 for (pair = node->data.mapping.pairs.start;
757 pair < node->data.mapping.pairs.top; ++pair) {
758 yaml_node_t *key, *value;
760 key = yaml_document_get_node(document, pair->key);
761 value = yaml_document_get_node(document, pair->value);
763 if (key->type != YAML_SCALAR_NODE)
764 isl_die(ctx, isl_error_invalid, "expecting scalar key",
765 goto error);
766 if (!strcmp((char *) key->data.scalar.value, "name"))
767 param->name = extract_id(ctx, document, value);
768 if (!strcmp((char *) key->data.scalar.value, "lb"))
769 param->lb = extract_aff(ctx, document, value);
770 if (!strcmp((char *) key->data.scalar.value, "ub"))
771 param->ub = extract_aff(ctx, document, value);
772 if (!strcmp((char *) key->data.scalar.value, "val"))
773 param->val = extract_aff(ctx, document, value);
776 return param;
777 error:
778 if (param)
779 delete param;
780 return NULL;
783 static int extract_param_list(isl_ctx *ctx, yaml_document_t *document,
784 yaml_node_t *node, std::vector<adg_param *> &list)
786 yaml_node_item_t *item;
788 if (node->type != YAML_SEQUENCE_NODE)
789 isl_die(ctx, isl_error_invalid, "expecting sequence",
790 return -1);
792 for (item = node->data.sequence.items.start;
793 item < node->data.sequence.items.top; ++item) {
794 yaml_node_t *n;
795 adg_param *param;
797 n = yaml_document_get_node(document, *item);
798 param = extract_param(ctx, document, n);
799 if (!param)
800 return -1;
801 list.push_back(param);
804 return 0;
807 static adg *extract_adg(isl_ctx *ctx, yaml_document_t *document,
808 yaml_node_t *node)
810 adg *adg;
811 yaml_node_pair_t *pair;
813 if (!node)
814 return NULL;
816 if (node->type != YAML_MAPPING_NODE)
817 isl_die(ctx, isl_error_invalid, "expecting mapping",
818 return NULL);
820 adg = new ::adg;
821 if (!adg)
822 return NULL;
824 for (pair = node->data.mapping.pairs.start;
825 pair < node->data.mapping.pairs.top; ++pair) {
826 yaml_node_t *key, *value;
828 key = yaml_document_get_node(document, pair->key);
829 value = yaml_document_get_node(document, pair->value);
831 if (key->type != YAML_SCALAR_NODE)
832 isl_die(ctx, isl_error_invalid, "expecting scalar key",
833 goto error);
834 if (!strcmp((char *) key->data.scalar.value, "name"))
835 adg->name = extract_id(ctx, document, value);
836 if (!strcmp((char *) key->data.scalar.value, "context"))
837 adg->context = extract_set(ctx, document, value);
838 if (!strcmp((char *) key->data.scalar.value, "iterator_map"))
839 adg->iterator_map = extract_map(ctx, document, value);
840 if (!strcmp((char *) key->data.scalar.value, "iterators") &&
841 extract_id_list(ctx, document, value, adg->iterators) < 0)
842 goto error;
843 if (!strcmp((char *) key->data.scalar.value, "all_iterators") &&
844 extract_id_list(ctx, document, value, adg->all_iterators) < 0)
845 goto error;
846 if (!strcmp((char *) key->data.scalar.value, "nodes") &&
847 extract_node_list(ctx, document, value, adg->nodes) < 0)
848 goto error;
849 if (!strcmp((char *) key->data.scalar.value, "edges") &&
850 extract_edge_list(ctx, document, value, adg->edges) < 0)
851 goto error;
852 if (!strcmp((char *) key->data.scalar.value, "params") &&
853 extract_param_list(ctx, document, value, adg->params) < 0)
854 goto error;
857 return adg;
858 error:
859 if (adg)
860 delete adg;
861 return NULL;
864 /* Extract an adg from the YAML description in "in".
866 adg *adg_parse(isl_ctx *ctx, FILE *in)
868 adg *adg = NULL;
869 yaml_parser_t parser;
870 yaml_node_t *root;
871 yaml_document_t document = { 0 };
873 yaml_parser_initialize(&parser);
875 yaml_parser_set_input_file(&parser, in);
877 if (!yaml_parser_load(&parser, &document))
878 goto error;
880 root = yaml_document_get_root_node(&document);
882 adg = extract_adg(ctx, &document, root);
884 yaml_document_delete(&document);
886 yaml_parser_delete(&parser);
888 return adg;
889 error:
890 yaml_parser_delete(&parser);
891 return NULL;