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
, "element_size"))
145 array
->element_size
= extract_int(ctx
, document
, value
);
146 if (!strcmp((char *) key
->data
.scalar
.value
, "live_out"))
147 array
->live_out
= extract_int(ctx
, document
, value
);
153 static struct pet_scop
*extract_arrays(isl_ctx
*ctx
, yaml_document_t
*document
,
154 yaml_node_t
*node
, struct pet_scop
*scop
)
157 yaml_node_item_t
*item
;
159 if (node
->type
!= YAML_SEQUENCE_NODE
)
160 isl_die(ctx
, isl_error_invalid
, "expecting sequence",
163 scop
->n_array
= node
->data
.sequence
.items
.top
164 - node
->data
.sequence
.items
.start
;
165 scop
->arrays
= isl_calloc_array(ctx
, struct pet_array
*, scop
->n_array
);
167 return pet_scop_free(scop
);
169 for (item
= node
->data
.sequence
.items
.start
, i
= 0;
170 item
< node
->data
.sequence
.items
.top
; ++item
, ++i
) {
173 n
= yaml_document_get_node(document
, *item
);
174 scop
->arrays
[i
] = extract_array(ctx
, document
, n
);
175 if (!scop
->arrays
[i
])
176 return pet_scop_free(scop
);
182 static struct pet_expr
*extract_expr(isl_ctx
*ctx
, yaml_document_t
*document
,
185 static struct pet_expr
*extract_arguments(isl_ctx
*ctx
,
186 yaml_document_t
*document
, yaml_node_t
*node
, struct pet_expr
*expr
)
189 yaml_node_item_t
*item
;
191 if (node
->type
!= YAML_SEQUENCE_NODE
)
192 isl_die(ctx
, isl_error_invalid
, "expecting sequence",
193 return pet_expr_free(expr
));
195 expr
->n_arg
= node
->data
.sequence
.items
.top
196 - node
->data
.sequence
.items
.start
;
197 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, expr
->n_arg
);
199 return pet_expr_free(expr
);
201 for (item
= node
->data
.sequence
.items
.start
, i
= 0;
202 item
< node
->data
.sequence
.items
.top
; ++item
, ++i
) {
205 n
= yaml_document_get_node(document
, *item
);
206 expr
->args
[i
] = extract_expr(ctx
, document
, n
);
208 return pet_expr_free(expr
);
214 static struct pet_expr
*extract_expr(isl_ctx
*ctx
, yaml_document_t
*document
,
217 struct pet_expr
*expr
;
218 yaml_node_pair_t
* pair
;
220 if (node
->type
!= YAML_MAPPING_NODE
)
221 isl_die(ctx
, isl_error_invalid
, "expecting mapping",
224 expr
= isl_calloc_type(ctx
, struct pet_expr
);
228 for (pair
= node
->data
.mapping
.pairs
.start
;
229 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
230 yaml_node_t
*key
, *value
;
232 key
= yaml_document_get_node(document
, pair
->key
);
233 value
= yaml_document_get_node(document
, pair
->value
);
235 if (key
->type
!= YAML_SCALAR_NODE
)
236 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
237 return pet_expr_free(expr
));
239 if (!strcmp((char *) key
->data
.scalar
.value
, "type"))
240 expr
->type
= extract_type(ctx
, document
, value
);
242 if (!strcmp((char *) key
->data
.scalar
.value
, "value"))
243 expr
->d
= extract_double(ctx
, document
, value
);
245 if (!strcmp((char *) key
->data
.scalar
.value
, "relation"))
246 expr
->acc
.access
= extract_map(ctx
, document
, value
);
247 if (!strcmp((char *) key
->data
.scalar
.value
, "read"))
248 expr
->acc
.read
= extract_int(ctx
, document
, value
);
249 if (!strcmp((char *) key
->data
.scalar
.value
, "write"))
250 expr
->acc
.write
= extract_int(ctx
, document
, value
);
252 if (!strcmp((char *) key
->data
.scalar
.value
, "operation"))
253 expr
->op
= extract_op(ctx
, document
, value
);
255 if (!strcmp((char *) key
->data
.scalar
.value
, "name"))
256 expr
->name
= extract_string(ctx
, document
, value
);
258 if (!strcmp((char *) key
->data
.scalar
.value
, "arguments"))
259 expr
= extract_arguments(ctx
, document
, value
, expr
);
267 static struct pet_stmt
*extract_stmt_arguments(isl_ctx
*ctx
,
268 yaml_document_t
*document
, yaml_node_t
*node
, struct pet_stmt
*stmt
)
271 yaml_node_item_t
*item
;
273 if (node
->type
!= YAML_SEQUENCE_NODE
)
274 isl_die(ctx
, isl_error_invalid
, "expecting sequence",
275 return pet_stmt_free(stmt
));
277 stmt
->n_arg
= node
->data
.sequence
.items
.top
278 - node
->data
.sequence
.items
.start
;
279 stmt
->args
= isl_calloc_array(ctx
, struct pet_expr
*, stmt
->n_arg
);
281 return pet_stmt_free(stmt
);
283 for (item
= node
->data
.sequence
.items
.start
, i
= 0;
284 item
< node
->data
.sequence
.items
.top
; ++item
, ++i
) {
287 n
= yaml_document_get_node(document
, *item
);
288 stmt
->args
[i
] = extract_expr(ctx
, document
, n
);
290 return pet_stmt_free(stmt
);
296 static struct pet_stmt
*extract_stmt(isl_ctx
*ctx
, yaml_document_t
*document
,
299 struct pet_stmt
*stmt
;
300 yaml_node_pair_t
* pair
;
302 if (node
->type
!= YAML_MAPPING_NODE
)
303 isl_die(ctx
, isl_error_invalid
, "expecting mapping",
306 stmt
= isl_calloc_type(ctx
, struct pet_stmt
);
310 for (pair
= node
->data
.mapping
.pairs
.start
;
311 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
312 yaml_node_t
*key
, *value
;
314 key
= yaml_document_get_node(document
, pair
->key
);
315 value
= yaml_document_get_node(document
, pair
->value
);
317 if (key
->type
!= YAML_SCALAR_NODE
)
318 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
319 return pet_stmt_free(stmt
));
321 if (!strcmp((char *) key
->data
.scalar
.value
, "line"))
322 stmt
->line
= extract_int(ctx
, document
, value
);
323 if (!strcmp((char *) key
->data
.scalar
.value
, "domain"))
324 stmt
->domain
= extract_set(ctx
, document
, value
);
325 if (!strcmp((char *) key
->data
.scalar
.value
, "schedule"))
326 stmt
->schedule
= extract_map(ctx
, document
, value
);
327 if (!strcmp((char *) key
->data
.scalar
.value
, "body"))
328 stmt
->body
= extract_expr(ctx
, document
, value
);
330 if (!strcmp((char *) key
->data
.scalar
.value
, "arguments"))
331 stmt
= extract_stmt_arguments(ctx
, document
,
340 static struct pet_scop
*extract_statements(isl_ctx
*ctx
,
341 yaml_document_t
*document
, yaml_node_t
*node
, struct pet_scop
*scop
)
344 yaml_node_item_t
*item
;
346 if (node
->type
!= YAML_SEQUENCE_NODE
)
347 isl_die(ctx
, isl_error_invalid
, "expecting sequence",
350 scop
->n_stmt
= node
->data
.sequence
.items
.top
351 - node
->data
.sequence
.items
.start
;
352 scop
->stmts
= isl_calloc_array(ctx
, struct pet_stmt
*, scop
->n_stmt
);
354 return pet_scop_free(scop
);
356 for (item
= node
->data
.sequence
.items
.start
, i
= 0;
357 item
< node
->data
.sequence
.items
.top
; ++item
, ++i
) {
360 n
= yaml_document_get_node(document
, *item
);
361 scop
->stmts
[i
] = extract_stmt(ctx
, document
, n
);
363 return pet_scop_free(scop
);
369 static struct pet_scop
*extract_scop(isl_ctx
*ctx
, yaml_document_t
*document
,
372 struct pet_scop
*scop
;
373 yaml_node_pair_t
* pair
;
378 if (node
->type
!= YAML_MAPPING_NODE
)
379 isl_die(ctx
, isl_error_invalid
, "expecting mapping",
382 scop
= isl_calloc_type(ctx
, struct pet_scop
);
386 for (pair
= node
->data
.mapping
.pairs
.start
;
387 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
388 yaml_node_t
*key
, *value
;
390 key
= yaml_document_get_node(document
, pair
->key
);
391 value
= yaml_document_get_node(document
, pair
->value
);
393 if (key
->type
!= YAML_SCALAR_NODE
)
394 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
395 return pet_scop_free(scop
));
396 if (!strcmp((char *) key
->data
.scalar
.value
, "context"))
397 scop
->context
= extract_set(ctx
, document
, value
);
398 if (!strcmp((char *) key
->data
.scalar
.value
, "context_value"))
399 scop
->context_value
= extract_set(ctx
, document
, value
);
400 if (!strcmp((char *) key
->data
.scalar
.value
, "arrays"))
401 scop
= extract_arrays(ctx
, document
, value
, scop
);
402 if (!strcmp((char *) key
->data
.scalar
.value
, "statements"))
403 scop
= extract_statements(ctx
, document
, value
, scop
);
408 if (!scop
->context_value
) {
409 isl_space
*space
= isl_space_params_alloc(ctx
, 0);
410 scop
->context_value
= isl_set_universe(space
);
411 if (!scop
->context_value
)
412 return pet_scop_free(scop
);
418 /* Extract a pet_scop from the YAML description in "in".
420 struct pet_scop
*pet_scop_parse(isl_ctx
*ctx
, FILE *in
)
422 struct pet_scop
*scop
= NULL
;
423 yaml_parser_t parser
;
425 yaml_document_t document
= { 0 };
427 yaml_parser_initialize(&parser
);
429 yaml_parser_set_input_file(&parser
, in
);
431 if (!yaml_parser_load(&parser
, &document
))
434 root
= yaml_document_get_root_node(&document
);
436 scop
= extract_scop(ctx
, &document
, root
);
438 yaml_document_delete(&document
);
440 yaml_parser_delete(&parser
);
444 yaml_parser_delete(&parser
);