2 * Copyright 2011 Leiden University. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * The views and conclusions contained in the software and documentation
29 * are those of the authors and should not be interpreted as
30 * representing official policies, either expressed or implied, of
38 #include "scop_yaml.h"
40 static char *extract_string(isl_ctx
*ctx
, yaml_document_t
*document
,
43 if (node
->type
!= YAML_SCALAR_NODE
)
44 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
47 return strdup((char *) node
->data
.scalar
.value
);
50 static int extract_int(isl_ctx
*ctx
, yaml_document_t
*document
,
53 if (node
->type
!= YAML_SCALAR_NODE
)
54 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
57 return atoi((char *) node
->data
.scalar
.value
);
60 static int extract_double(isl_ctx
*ctx
, yaml_document_t
*document
,
63 if (node
->type
!= YAML_SCALAR_NODE
)
64 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
67 return strtod((char *) node
->data
.scalar
.value
, NULL
);
70 static enum pet_expr_type
extract_type(isl_ctx
*ctx
, yaml_document_t
*document
,
73 if (node
->type
!= YAML_SCALAR_NODE
)
74 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
77 return pet_str_type((char *) node
->data
.scalar
.value
);
80 static enum pet_op_type
extract_op(isl_ctx
*ctx
, yaml_document_t
*document
,
83 if (node
->type
!= YAML_SCALAR_NODE
)
84 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
87 return pet_str_op((char *) node
->data
.scalar
.value
);
90 static __isl_give isl_set
*extract_set(isl_ctx
*ctx
, yaml_document_t
*document
,
93 if (node
->type
!= YAML_SCALAR_NODE
)
94 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
97 return isl_set_read_from_str(ctx
, (char *) node
->data
.scalar
.value
);
100 static __isl_give isl_map
*extract_map(isl_ctx
*ctx
, yaml_document_t
*document
,
103 if (node
->type
!= YAML_SCALAR_NODE
)
104 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
107 return isl_map_read_from_str(ctx
, (char *) node
->data
.scalar
.value
);
110 static struct pet_array
*extract_array(isl_ctx
*ctx
, yaml_document_t
*document
,
113 struct pet_array
*array
;
114 yaml_node_pair_t
* pair
;
116 if (node
->type
!= YAML_MAPPING_NODE
)
117 isl_die(ctx
, isl_error_invalid
, "expecting mapping",
120 array
= isl_calloc_type(ctx
, struct pet_array
);
124 for (pair
= node
->data
.mapping
.pairs
.start
;
125 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
126 yaml_node_t
*key
, *value
;
128 key
= yaml_document_get_node(document
, pair
->key
);
129 value
= yaml_document_get_node(document
, pair
->value
);
131 if (key
->type
!= YAML_SCALAR_NODE
)
132 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
133 return pet_array_free(array
));
135 if (!strcmp((char *) key
->data
.scalar
.value
, "context"))
136 array
->context
= extract_set(ctx
, document
, value
);
137 if (!strcmp((char *) key
->data
.scalar
.value
, "extent"))
138 array
->extent
= extract_set(ctx
, document
, value
);
139 if (!strcmp((char *) key
->data
.scalar
.value
, "value_bounds"))
140 array
->value_bounds
= extract_set(ctx
, document
, value
);
141 if (!strcmp((char *) key
->data
.scalar
.value
, "element_type"))
142 array
->element_type
=
143 extract_string(ctx
, document
, value
);
144 if (!strcmp((char *) key
->data
.scalar
.value
, "live_out"))
145 array
->live_out
= extract_int(ctx
, document
, value
);
151 static struct pet_scop
*extract_arrays(isl_ctx
*ctx
, yaml_document_t
*document
,
152 yaml_node_t
*node
, struct pet_scop
*scop
)
155 yaml_node_item_t
*item
;
157 if (node
->type
!= YAML_SEQUENCE_NODE
)
158 isl_die(ctx
, isl_error_invalid
, "expecting sequence",
161 scop
->n_array
= node
->data
.sequence
.items
.top
162 - node
->data
.sequence
.items
.start
;
163 scop
->arrays
= isl_calloc_array(ctx
, struct pet_array
*, scop
->n_array
);
165 return pet_scop_free(scop
);
167 for (item
= node
->data
.sequence
.items
.start
, i
= 0;
168 item
< node
->data
.sequence
.items
.top
; ++item
, ++i
) {
171 n
= yaml_document_get_node(document
, *item
);
172 scop
->arrays
[i
] = extract_array(ctx
, document
, n
);
173 if (!scop
->arrays
[i
])
174 return pet_scop_free(scop
);
180 static struct pet_expr
*extract_expr(isl_ctx
*ctx
, yaml_document_t
*document
,
183 static struct pet_expr
*extract_arguments(isl_ctx
*ctx
,
184 yaml_document_t
*document
, yaml_node_t
*node
, struct pet_expr
*expr
)
187 yaml_node_item_t
*item
;
189 if (node
->type
!= YAML_SEQUENCE_NODE
)
190 isl_die(ctx
, isl_error_invalid
, "expecting sequence",
191 return pet_expr_free(expr
));
193 expr
->n_arg
= node
->data
.sequence
.items
.top
194 - node
->data
.sequence
.items
.start
;
195 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, expr
->n_arg
);
197 return pet_expr_free(expr
);
199 for (item
= node
->data
.sequence
.items
.start
, i
= 0;
200 item
< node
->data
.sequence
.items
.top
; ++item
, ++i
) {
203 n
= yaml_document_get_node(document
, *item
);
204 expr
->args
[i
] = extract_expr(ctx
, document
, n
);
206 return pet_expr_free(expr
);
212 static struct pet_expr
*extract_expr(isl_ctx
*ctx
, yaml_document_t
*document
,
215 struct pet_expr
*expr
;
216 yaml_node_pair_t
* pair
;
218 if (node
->type
!= YAML_MAPPING_NODE
)
219 isl_die(ctx
, isl_error_invalid
, "expecting mapping",
222 expr
= isl_calloc_type(ctx
, struct pet_expr
);
226 for (pair
= node
->data
.mapping
.pairs
.start
;
227 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
228 yaml_node_t
*key
, *value
;
230 key
= yaml_document_get_node(document
, pair
->key
);
231 value
= yaml_document_get_node(document
, pair
->value
);
233 if (key
->type
!= YAML_SCALAR_NODE
)
234 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
235 return pet_expr_free(expr
));
237 if (!strcmp((char *) key
->data
.scalar
.value
, "type"))
238 expr
->type
= extract_type(ctx
, document
, value
);
240 if (!strcmp((char *) key
->data
.scalar
.value
, "value"))
241 expr
->d
= extract_double(ctx
, document
, value
);
243 if (!strcmp((char *) key
->data
.scalar
.value
, "relation"))
244 expr
->acc
.access
= extract_map(ctx
, document
, value
);
245 if (!strcmp((char *) key
->data
.scalar
.value
, "read"))
246 expr
->acc
.read
= extract_int(ctx
, document
, value
);
247 if (!strcmp((char *) key
->data
.scalar
.value
, "write"))
248 expr
->acc
.write
= extract_int(ctx
, document
, value
);
250 if (!strcmp((char *) key
->data
.scalar
.value
, "operation"))
251 expr
->op
= extract_op(ctx
, document
, value
);
253 if (!strcmp((char *) key
->data
.scalar
.value
, "name"))
254 expr
->name
= extract_string(ctx
, document
, value
);
256 if (!strcmp((char *) key
->data
.scalar
.value
, "arguments"))
257 expr
= extract_arguments(ctx
, document
, value
, expr
);
265 static struct pet_stmt
*extract_stmt_arguments(isl_ctx
*ctx
,
266 yaml_document_t
*document
, yaml_node_t
*node
, struct pet_stmt
*stmt
)
269 yaml_node_item_t
*item
;
271 if (node
->type
!= YAML_SEQUENCE_NODE
)
272 isl_die(ctx
, isl_error_invalid
, "expecting sequence",
273 return pet_stmt_free(stmt
));
275 stmt
->n_arg
= node
->data
.sequence
.items
.top
276 - node
->data
.sequence
.items
.start
;
277 stmt
->args
= isl_calloc_array(ctx
, struct pet_expr
*, stmt
->n_arg
);
279 return pet_stmt_free(stmt
);
281 for (item
= node
->data
.sequence
.items
.start
, i
= 0;
282 item
< node
->data
.sequence
.items
.top
; ++item
, ++i
) {
285 n
= yaml_document_get_node(document
, *item
);
286 stmt
->args
[i
] = extract_expr(ctx
, document
, n
);
288 return pet_stmt_free(stmt
);
294 static struct pet_stmt
*extract_stmt(isl_ctx
*ctx
, yaml_document_t
*document
,
297 struct pet_stmt
*stmt
;
298 yaml_node_pair_t
* pair
;
300 if (node
->type
!= YAML_MAPPING_NODE
)
301 isl_die(ctx
, isl_error_invalid
, "expecting mapping",
304 stmt
= isl_calloc_type(ctx
, struct pet_stmt
);
308 for (pair
= node
->data
.mapping
.pairs
.start
;
309 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
310 yaml_node_t
*key
, *value
;
312 key
= yaml_document_get_node(document
, pair
->key
);
313 value
= yaml_document_get_node(document
, pair
->value
);
315 if (key
->type
!= YAML_SCALAR_NODE
)
316 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
317 return pet_stmt_free(stmt
));
319 if (!strcmp((char *) key
->data
.scalar
.value
, "line"))
320 stmt
->line
= extract_int(ctx
, document
, value
);
321 if (!strcmp((char *) key
->data
.scalar
.value
, "domain"))
322 stmt
->domain
= extract_set(ctx
, document
, value
);
323 if (!strcmp((char *) key
->data
.scalar
.value
, "schedule"))
324 stmt
->schedule
= extract_map(ctx
, document
, value
);
325 if (!strcmp((char *) key
->data
.scalar
.value
, "body"))
326 stmt
->body
= extract_expr(ctx
, document
, value
);
328 if (!strcmp((char *) key
->data
.scalar
.value
, "arguments"))
329 stmt
= extract_stmt_arguments(ctx
, document
,
338 static struct pet_scop
*extract_statements(isl_ctx
*ctx
,
339 yaml_document_t
*document
, yaml_node_t
*node
, struct pet_scop
*scop
)
342 yaml_node_item_t
*item
;
344 if (node
->type
!= YAML_SEQUENCE_NODE
)
345 isl_die(ctx
, isl_error_invalid
, "expecting sequence",
348 scop
->n_stmt
= node
->data
.sequence
.items
.top
349 - node
->data
.sequence
.items
.start
;
350 scop
->stmts
= isl_calloc_array(ctx
, struct pet_stmt
*, scop
->n_stmt
);
352 return pet_scop_free(scop
);
354 for (item
= node
->data
.sequence
.items
.start
, i
= 0;
355 item
< node
->data
.sequence
.items
.top
; ++item
, ++i
) {
358 n
= yaml_document_get_node(document
, *item
);
359 scop
->stmts
[i
] = extract_stmt(ctx
, document
, n
);
361 return pet_scop_free(scop
);
367 static struct pet_scop
*extract_scop(isl_ctx
*ctx
, yaml_document_t
*document
,
370 struct pet_scop
*scop
;
371 yaml_node_pair_t
* pair
;
376 if (node
->type
!= YAML_MAPPING_NODE
)
377 isl_die(ctx
, isl_error_invalid
, "expecting mapping",
380 scop
= isl_calloc_type(ctx
, struct pet_scop
);
384 for (pair
= node
->data
.mapping
.pairs
.start
;
385 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
386 yaml_node_t
*key
, *value
;
388 key
= yaml_document_get_node(document
, pair
->key
);
389 value
= yaml_document_get_node(document
, pair
->value
);
391 if (key
->type
!= YAML_SCALAR_NODE
)
392 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
393 return pet_scop_free(scop
));
394 if (!strcmp((char *) key
->data
.scalar
.value
, "context"))
395 scop
->context
= extract_set(ctx
, document
, value
);
396 if (!strcmp((char *) key
->data
.scalar
.value
, "context_value"))
397 scop
->context_value
= extract_set(ctx
, document
, value
);
398 if (!strcmp((char *) key
->data
.scalar
.value
, "arrays"))
399 scop
= extract_arrays(ctx
, document
, value
, scop
);
400 if (!strcmp((char *) key
->data
.scalar
.value
, "statements"))
401 scop
= extract_statements(ctx
, document
, value
, scop
);
406 if (!scop
->context_value
) {
407 isl_space
*space
= isl_space_params_alloc(ctx
, 0);
408 scop
->context_value
= isl_set_universe(space
);
409 if (!scop
->context_value
)
410 return pet_scop_free(scop
);
416 /* Extract a pet_scop from the YAML description in "in".
418 struct pet_scop
*pet_scop_parse(isl_ctx
*ctx
, FILE *in
)
420 struct pet_scop
*scop
= NULL
;
421 yaml_parser_t parser
;
423 yaml_document_t document
= { 0 };
425 yaml_parser_initialize(&parser
);
427 yaml_parser_set_input_file(&parser
, in
);
429 if (!yaml_parser_load(&parser
, &document
))
432 root
= yaml_document_get_root_node(&document
);
434 scop
= extract_scop(ctx
, &document
, root
);
436 yaml_document_delete(&document
);
438 yaml_parser_delete(&parser
);
442 yaml_parser_delete(&parser
);