update isl for rename of isl_map_insert
[pet.git] / parse.c
blob36f9dd9e6d1a2f954940d0d1a2bab75bee9cf62e
1 #include <stdlib.h>
2 #include <yaml.h>
4 #include "scop.h"
5 #include "scop_yaml.h"
7 static char *extract_string(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 strdup((char *) node->data.scalar.value);
17 static int extract_int(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 -1);
24 return atoi((char *) node->data.scalar.value);
27 static int extract_double(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 -1);
34 return strtod((char *) node->data.scalar.value, NULL);
37 static enum pet_expr_type extract_type(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 -1);
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,
48 yaml_node_t *node)
50 if (node->type != YAML_SCALAR_NODE)
51 isl_die(ctx, isl_error_invalid, "expecting scalar node",
52 return -1);
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,
58 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_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,
68 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_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,
78 yaml_node_t *node)
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",
85 return NULL);
87 array = isl_calloc_type(ctx, struct pet_array);
88 if (!array)
89 return NULL;
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);
115 return array;
118 static struct pet_scop *extract_arrays(isl_ctx *ctx, yaml_document_t *document,
119 yaml_node_t *node, struct pet_scop *scop)
121 int i;
122 yaml_node_item_t *item;
124 if (node->type != YAML_SEQUENCE_NODE)
125 isl_die(ctx, isl_error_invalid, "expecting sequence",
126 return NULL);
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);
131 if (!scop->arrays)
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) {
136 yaml_node_t *n;
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);
144 return scop;
147 static struct pet_expr *extract_expr(isl_ctx *ctx, yaml_document_t *document,
148 yaml_node_t *node);
150 static struct pet_expr *extract_arguments(isl_ctx *ctx,
151 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
153 int i;
154 yaml_node_item_t *item;
156 if (node->type != YAML_SEQUENCE_NODE)
157 isl_die(ctx, isl_error_invalid, "expecting sequence",
158 return NULL);
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);
163 if (!expr->args)
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) {
168 yaml_node_t *n;
170 n = yaml_document_get_node(document, *item);
171 expr->args[i] = extract_expr(ctx, document, n);
172 if (!expr->args[i])
173 return pet_expr_free(expr);
176 return expr;
179 static struct pet_expr *extract_expr(isl_ctx *ctx, yaml_document_t *document,
180 yaml_node_t *node)
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",
187 return NULL);
189 expr = isl_calloc_type(ctx, struct pet_expr);
190 if (!expr)
191 return NULL;
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);
225 if (!expr)
226 return NULL;
229 return expr;
232 static struct pet_stmt *extract_stmt(isl_ctx *ctx, yaml_document_t *document,
233 yaml_node_t *node)
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",
240 return NULL);
242 stmt = isl_calloc_type(ctx, struct pet_stmt);
243 if (!stmt)
244 return NULL;
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);
267 return stmt;
270 static struct pet_scop *extract_statements(isl_ctx *ctx,
271 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
273 int i;
274 yaml_node_item_t *item;
276 if (node->type != YAML_SEQUENCE_NODE)
277 isl_die(ctx, isl_error_invalid, "expecting sequence",
278 return NULL);
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);
283 if (!scop->stmts)
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) {
288 yaml_node_t *n;
290 n = yaml_document_get_node(document, *item);
291 scop->stmts[i] = extract_stmt(ctx, document, n);
292 if (!scop->stmts[i])
293 return pet_scop_free(scop);
296 return scop;
299 static struct pet_scop *extract_scop(isl_ctx *ctx, yaml_document_t *document,
300 yaml_node_t *node)
302 struct pet_scop *scop;
303 yaml_node_pair_t * pair;
305 if (!node)
306 return NULL;
308 if (node->type != YAML_MAPPING_NODE)
309 isl_die(ctx, isl_error_invalid, "expecting mapping",
310 return NULL);
312 scop = isl_calloc_type(ctx, struct pet_scop);
313 if (!scop)
314 return NULL;
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);
332 if (!scop)
333 return NULL;
336 return 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;
345 yaml_node_t *root;
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))
353 goto error;
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);
363 return scop;
364 error:
365 yaml_parser_delete(&parser);
366 pet_scop_free(scop);
367 return NULL;