7 static char *extract_string(isl_ctx
*ctx
, yaml_document_t
*document
,
10 if (node
->type
!= YAML_SCALAR_NODE
)
11 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
14 return strdup((char *) node
->data
.scalar
.value
);
17 static int extract_int(isl_ctx
*ctx
, yaml_document_t
*document
,
20 if (node
->type
!= YAML_SCALAR_NODE
)
21 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
24 return atoi((char *) node
->data
.scalar
.value
);
27 static int extract_double(isl_ctx
*ctx
, yaml_document_t
*document
,
30 if (node
->type
!= YAML_SCALAR_NODE
)
31 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
34 return strtod((char *) node
->data
.scalar
.value
, NULL
);
37 static enum pet_expr_type
extract_type(isl_ctx
*ctx
, yaml_document_t
*document
,
40 if (node
->type
!= YAML_SCALAR_NODE
)
41 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
44 return pet_str_type((char *) node
->data
.scalar
.value
);
47 static enum pet_op_type
extract_op(isl_ctx
*ctx
, yaml_document_t
*document
,
50 if (node
->type
!= YAML_SCALAR_NODE
)
51 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
54 return pet_str_op((char *) node
->data
.scalar
.value
);
57 static __isl_give isl_set
*extract_set(isl_ctx
*ctx
, yaml_document_t
*document
,
60 if (node
->type
!= YAML_SCALAR_NODE
)
61 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
64 return isl_set_read_from_str(ctx
, (char *) node
->data
.scalar
.value
, -1);
67 static __isl_give isl_map
*extract_map(isl_ctx
*ctx
, yaml_document_t
*document
,
70 if (node
->type
!= YAML_SCALAR_NODE
)
71 isl_die(ctx
, isl_error_invalid
, "expecting scalar node",
74 return isl_map_read_from_str(ctx
, (char *) node
->data
.scalar
.value
, -1);
77 static struct pet_array
*extract_array(isl_ctx
*ctx
, yaml_document_t
*document
,
80 struct pet_array
*array
;
81 yaml_node_pair_t
* pair
;
83 if (node
->type
!= YAML_MAPPING_NODE
)
84 isl_die(ctx
, isl_error_invalid
, "expecting mapping",
87 array
= isl_calloc_type(ctx
, struct pet_array
);
91 for (pair
= node
->data
.mapping
.pairs
.start
;
92 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
93 yaml_node_t
*key
, *value
;
95 key
= yaml_document_get_node(document
, pair
->key
);
96 value
= yaml_document_get_node(document
, pair
->value
);
98 if (key
->type
!= YAML_SCALAR_NODE
)
99 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
100 return pet_array_free(array
));
102 if (!strcmp((char *) key
->data
.scalar
.value
, "context"))
103 array
->context
= extract_set(ctx
, document
, value
);
104 if (!strcmp((char *) key
->data
.scalar
.value
, "extent"))
105 array
->extent
= extract_set(ctx
, document
, value
);
106 if (!strcmp((char *) key
->data
.scalar
.value
, "value_bounds"))
107 array
->value_bounds
= extract_set(ctx
, document
, value
);
108 if (!strcmp((char *) key
->data
.scalar
.value
, "element_type"))
109 array
->element_type
=
110 extract_string(ctx
, document
, value
);
111 if (!strcmp((char *) key
->data
.scalar
.value
, "live_out"))
112 array
->live_out
= extract_int(ctx
, document
, value
);
118 static struct pet_scop
*extract_arrays(isl_ctx
*ctx
, yaml_document_t
*document
,
119 yaml_node_t
*node
, struct pet_scop
*scop
)
122 yaml_node_item_t
*item
;
124 if (node
->type
!= YAML_SEQUENCE_NODE
)
125 isl_die(ctx
, isl_error_invalid
, "expecting sequence",
128 scop
->n_array
= node
->data
.sequence
.items
.top
129 - node
->data
.sequence
.items
.start
;
130 scop
->arrays
= isl_calloc_array(ctx
, struct pet_array
*, scop
->n_array
);
132 return pet_scop_free(scop
);
134 for (item
= node
->data
.sequence
.items
.start
, i
= 0;
135 item
< node
->data
.sequence
.items
.top
; ++item
, ++i
) {
138 n
= yaml_document_get_node(document
, *item
);
139 scop
->arrays
[i
] = extract_array(ctx
, document
, n
);
140 if (!scop
->arrays
[i
])
141 return pet_scop_free(scop
);
147 static struct pet_expr
*extract_expr(isl_ctx
*ctx
, yaml_document_t
*document
,
150 static struct pet_expr
*extract_arguments(isl_ctx
*ctx
,
151 yaml_document_t
*document
, yaml_node_t
*node
, struct pet_expr
*expr
)
154 yaml_node_item_t
*item
;
156 if (node
->type
!= YAML_SEQUENCE_NODE
)
157 isl_die(ctx
, isl_error_invalid
, "expecting sequence",
160 expr
->n_arg
= node
->data
.sequence
.items
.top
161 - node
->data
.sequence
.items
.start
;
162 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, expr
->n_arg
);
164 return pet_expr_free(expr
);
166 for (item
= node
->data
.sequence
.items
.start
, i
= 0;
167 item
< node
->data
.sequence
.items
.top
; ++item
, ++i
) {
170 n
= yaml_document_get_node(document
, *item
);
171 expr
->args
[i
] = extract_expr(ctx
, document
, n
);
173 return pet_expr_free(expr
);
179 static struct pet_expr
*extract_expr(isl_ctx
*ctx
, yaml_document_t
*document
,
182 struct pet_expr
*expr
;
183 yaml_node_pair_t
* pair
;
185 if (node
->type
!= YAML_MAPPING_NODE
)
186 isl_die(ctx
, isl_error_invalid
, "expecting mapping",
189 expr
= isl_calloc_type(ctx
, struct pet_expr
);
193 for (pair
= node
->data
.mapping
.pairs
.start
;
194 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
195 yaml_node_t
*key
, *value
;
197 key
= yaml_document_get_node(document
, pair
->key
);
198 value
= yaml_document_get_node(document
, pair
->value
);
200 if (key
->type
!= YAML_SCALAR_NODE
)
201 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
202 return pet_expr_free(expr
));
204 if (!strcmp((char *) key
->data
.scalar
.value
, "type"))
205 expr
->type
= extract_type(ctx
, document
, value
);
207 if (!strcmp((char *) key
->data
.scalar
.value
, "value"))
208 expr
->d
= extract_double(ctx
, document
, value
);
210 if (!strcmp((char *) key
->data
.scalar
.value
, "relation"))
211 expr
->acc
.access
= extract_map(ctx
, document
, value
);
212 if (!strcmp((char *) key
->data
.scalar
.value
, "read"))
213 expr
->acc
.read
= extract_int(ctx
, document
, value
);
214 if (!strcmp((char *) key
->data
.scalar
.value
, "write"))
215 expr
->acc
.write
= extract_int(ctx
, document
, value
);
217 if (!strcmp((char *) key
->data
.scalar
.value
, "operation"))
218 expr
->op
= extract_op(ctx
, document
, value
);
220 if (!strcmp((char *) key
->data
.scalar
.value
, "name"))
221 expr
->name
= extract_string(ctx
, document
, value
);
223 if (!strcmp((char *) key
->data
.scalar
.value
, "arguments"))
224 expr
= extract_arguments(ctx
, document
, value
, expr
);
232 static struct pet_stmt
*extract_stmt(isl_ctx
*ctx
, yaml_document_t
*document
,
235 struct pet_stmt
*stmt
;
236 yaml_node_pair_t
* pair
;
238 if (node
->type
!= YAML_MAPPING_NODE
)
239 isl_die(ctx
, isl_error_invalid
, "expecting mapping",
242 stmt
= isl_calloc_type(ctx
, struct pet_stmt
);
246 for (pair
= node
->data
.mapping
.pairs
.start
;
247 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
248 yaml_node_t
*key
, *value
;
250 key
= yaml_document_get_node(document
, pair
->key
);
251 value
= yaml_document_get_node(document
, pair
->value
);
253 if (key
->type
!= YAML_SCALAR_NODE
)
254 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
255 return pet_stmt_free(stmt
));
257 if (!strcmp((char *) key
->data
.scalar
.value
, "line"))
258 stmt
->line
= extract_int(ctx
, document
, value
);
259 if (!strcmp((char *) key
->data
.scalar
.value
, "domain"))
260 stmt
->domain
= extract_set(ctx
, document
, value
);
261 if (!strcmp((char *) key
->data
.scalar
.value
, "schedule"))
262 stmt
->schedule
= extract_map(ctx
, document
, value
);
263 if (!strcmp((char *) key
->data
.scalar
.value
, "body"))
264 stmt
->body
= extract_expr(ctx
, document
, value
);
270 static struct pet_scop
*extract_statements(isl_ctx
*ctx
,
271 yaml_document_t
*document
, yaml_node_t
*node
, struct pet_scop
*scop
)
274 yaml_node_item_t
*item
;
276 if (node
->type
!= YAML_SEQUENCE_NODE
)
277 isl_die(ctx
, isl_error_invalid
, "expecting sequence",
280 scop
->n_stmt
= node
->data
.sequence
.items
.top
281 - node
->data
.sequence
.items
.start
;
282 scop
->stmts
= isl_calloc_array(ctx
, struct pet_stmt
*, scop
->n_stmt
);
284 return pet_scop_free(scop
);
286 for (item
= node
->data
.sequence
.items
.start
, i
= 0;
287 item
< node
->data
.sequence
.items
.top
; ++item
, ++i
) {
290 n
= yaml_document_get_node(document
, *item
);
291 scop
->stmts
[i
] = extract_stmt(ctx
, document
, n
);
293 return pet_scop_free(scop
);
299 static struct pet_scop
*extract_scop(isl_ctx
*ctx
, yaml_document_t
*document
,
302 struct pet_scop
*scop
;
303 yaml_node_pair_t
* pair
;
308 if (node
->type
!= YAML_MAPPING_NODE
)
309 isl_die(ctx
, isl_error_invalid
, "expecting mapping",
312 scop
= isl_calloc_type(ctx
, struct pet_scop
);
316 for (pair
= node
->data
.mapping
.pairs
.start
;
317 pair
< node
->data
.mapping
.pairs
.top
; ++pair
) {
318 yaml_node_t
*key
, *value
;
320 key
= yaml_document_get_node(document
, pair
->key
);
321 value
= yaml_document_get_node(document
, pair
->value
);
323 if (key
->type
!= YAML_SCALAR_NODE
)
324 isl_die(ctx
, isl_error_invalid
, "expecting scalar key",
325 return pet_scop_free(scop
));
326 if (!strcmp((char *) key
->data
.scalar
.value
, "context"))
327 scop
->context
= extract_set(ctx
, document
, value
);
328 if (!strcmp((char *) key
->data
.scalar
.value
, "arrays"))
329 scop
= extract_arrays(ctx
, document
, value
, scop
);
330 if (!strcmp((char *) key
->data
.scalar
.value
, "statements"))
331 scop
= extract_statements(ctx
, document
, value
, scop
);
339 /* Extract a pet_scop from the YAML description in "in".
341 struct pet_scop
*pet_scop_parse(isl_ctx
*ctx
, FILE *in
)
343 struct pet_scop
*scop
= NULL
;
344 yaml_parser_t parser
;
346 yaml_document_t document
= { 0 };
348 yaml_parser_initialize(&parser
);
350 yaml_parser_set_input_file(&parser
, in
);
352 if (!yaml_parser_load(&parser
, &document
))
355 root
= yaml_document_get_root_node(&document
);
357 scop
= extract_scop(ctx
, &document
, root
);
359 yaml_document_delete(&document
);
361 yaml_parser_delete(&parser
);
365 yaml_parser_delete(&parser
);