update isl for missing include in interface/python.cc
[pet.git] / parse.c
blob7283dfa84d393c90b6b6512380c1c7283d6ece92
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
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
31 * Leiden University.
32 */
34 #include <stdlib.h>
35 #include <yaml.h>
37 #include "scop.h"
38 #include "scop_yaml.h"
40 static char *extract_string(isl_ctx *ctx, yaml_document_t *document,
41 yaml_node_t *node)
43 if (node->type != YAML_SCALAR_NODE)
44 isl_die(ctx, isl_error_invalid, "expecting scalar node",
45 return NULL);
47 return strdup((char *) node->data.scalar.value);
50 static int extract_int(isl_ctx *ctx, yaml_document_t *document,
51 yaml_node_t *node)
53 if (node->type != YAML_SCALAR_NODE)
54 isl_die(ctx, isl_error_invalid, "expecting scalar node",
55 return -1);
57 return atoi((char *) node->data.scalar.value);
60 static double extract_double(isl_ctx *ctx, yaml_document_t *document,
61 yaml_node_t *node)
63 if (node->type != YAML_SCALAR_NODE)
64 isl_die(ctx, isl_error_invalid, "expecting scalar node",
65 return -1);
67 return strtod((char *) node->data.scalar.value, NULL);
70 static enum pet_expr_type extract_type(isl_ctx *ctx, yaml_document_t *document,
71 yaml_node_t *node)
73 if (node->type != YAML_SCALAR_NODE)
74 isl_die(ctx, isl_error_invalid, "expecting scalar node",
75 return -1);
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,
81 yaml_node_t *node)
83 if (node->type != YAML_SCALAR_NODE)
84 isl_die(ctx, isl_error_invalid, "expecting scalar node",
85 return -1);
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,
91 yaml_node_t *node)
93 if (node->type != YAML_SCALAR_NODE)
94 isl_die(ctx, isl_error_invalid, "expecting scalar node",
95 return NULL);
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,
101 yaml_node_t *node)
103 if (node->type != YAML_SCALAR_NODE)
104 isl_die(ctx, isl_error_invalid, "expecting scalar node",
105 return NULL);
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,
111 yaml_node_t *node)
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",
118 return NULL);
120 array = isl_calloc_type(ctx, struct pet_array);
121 if (!array)
122 return NULL;
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);
148 if (!strcmp((char *) key->data.scalar.value,
149 "uniquely_defined"))
150 array->uniquely_defined =
151 extract_int(ctx, document, value);
152 if (!strcmp((char *) key->data.scalar.value, "declared"))
153 array->declared = extract_int(ctx, document, value);
154 if (!strcmp((char *) key->data.scalar.value, "exposed"))
155 array->exposed = extract_int(ctx, document, value);
158 return array;
161 static struct pet_scop *extract_arrays(isl_ctx *ctx, yaml_document_t *document,
162 yaml_node_t *node, struct pet_scop *scop)
164 int i;
165 yaml_node_item_t *item;
167 if (node->type != YAML_SEQUENCE_NODE)
168 isl_die(ctx, isl_error_invalid, "expecting sequence",
169 return NULL);
171 scop->n_array = node->data.sequence.items.top
172 - node->data.sequence.items.start;
173 scop->arrays = isl_calloc_array(ctx, struct pet_array *, scop->n_array);
174 if (!scop->arrays)
175 return pet_scop_free(scop);
177 for (item = node->data.sequence.items.start, i = 0;
178 item < node->data.sequence.items.top; ++item, ++i) {
179 yaml_node_t *n;
181 n = yaml_document_get_node(document, *item);
182 scop->arrays[i] = extract_array(ctx, document, n);
183 if (!scop->arrays[i])
184 return pet_scop_free(scop);
187 return scop;
190 static struct pet_expr *extract_expr(isl_ctx *ctx, yaml_document_t *document,
191 yaml_node_t *node);
193 static struct pet_expr *extract_arguments(isl_ctx *ctx,
194 yaml_document_t *document, yaml_node_t *node, struct pet_expr *expr)
196 int i;
197 yaml_node_item_t *item;
199 if (node->type != YAML_SEQUENCE_NODE)
200 isl_die(ctx, isl_error_invalid, "expecting sequence",
201 return pet_expr_free(expr));
203 expr->n_arg = node->data.sequence.items.top
204 - node->data.sequence.items.start;
205 expr->args = isl_calloc_array(ctx, struct pet_expr *, expr->n_arg);
206 if (!expr->args)
207 return pet_expr_free(expr);
209 for (item = node->data.sequence.items.start, i = 0;
210 item < node->data.sequence.items.top; ++item, ++i) {
211 yaml_node_t *n;
213 n = yaml_document_get_node(document, *item);
214 expr->args[i] = extract_expr(ctx, document, n);
215 if (!expr->args[i])
216 return pet_expr_free(expr);
219 return expr;
222 static struct pet_expr *extract_expr(isl_ctx *ctx, yaml_document_t *document,
223 yaml_node_t *node)
225 struct pet_expr *expr;
226 yaml_node_pair_t * pair;
228 if (node->type != YAML_MAPPING_NODE)
229 isl_die(ctx, isl_error_invalid, "expecting mapping",
230 return NULL);
232 expr = isl_calloc_type(ctx, struct pet_expr);
233 if (!expr)
234 return NULL;
236 for (pair = node->data.mapping.pairs.start;
237 pair < node->data.mapping.pairs.top; ++pair) {
238 yaml_node_t *key, *value;
240 key = yaml_document_get_node(document, pair->key);
241 value = yaml_document_get_node(document, pair->value);
243 if (key->type != YAML_SCALAR_NODE)
244 isl_die(ctx, isl_error_invalid, "expecting scalar key",
245 return pet_expr_free(expr));
247 if (!strcmp((char *) key->data.scalar.value, "type"))
248 expr->type = extract_type(ctx, document, value);
250 if (!strcmp((char *) key->data.scalar.value, "value"))
251 expr->d.val = extract_double(ctx, document, value);
252 if (!strcmp((char *) key->data.scalar.value, "string"))
253 expr->d.s = extract_string(ctx, document, value);
255 if (!strcmp((char *) key->data.scalar.value, "relation"))
256 expr->acc.access = extract_map(ctx, document, value);
257 if (!strcmp((char *) key->data.scalar.value, "read"))
258 expr->acc.read = extract_int(ctx, document, value);
259 if (!strcmp((char *) key->data.scalar.value, "write"))
260 expr->acc.write = extract_int(ctx, document, value);
262 if (!strcmp((char *) key->data.scalar.value, "operation"))
263 expr->op = extract_op(ctx, document, value);
265 if (!strcmp((char *) key->data.scalar.value, "name"))
266 expr->name = extract_string(ctx, document, value);
268 if (!strcmp((char *) key->data.scalar.value, "arguments"))
269 expr = extract_arguments(ctx, document, value, expr);
270 if (!expr)
271 return NULL;
274 return expr;
277 static struct pet_stmt *extract_stmt_arguments(isl_ctx *ctx,
278 yaml_document_t *document, yaml_node_t *node, struct pet_stmt *stmt)
280 int i;
281 yaml_node_item_t *item;
283 if (node->type != YAML_SEQUENCE_NODE)
284 isl_die(ctx, isl_error_invalid, "expecting sequence",
285 return pet_stmt_free(stmt));
287 stmt->n_arg = node->data.sequence.items.top
288 - node->data.sequence.items.start;
289 stmt->args = isl_calloc_array(ctx, struct pet_expr *, stmt->n_arg);
290 if (!stmt->args)
291 return pet_stmt_free(stmt);
293 for (item = node->data.sequence.items.start, i = 0;
294 item < node->data.sequence.items.top; ++item, ++i) {
295 yaml_node_t *n;
297 n = yaml_document_get_node(document, *item);
298 stmt->args[i] = extract_expr(ctx, document, n);
299 if (!stmt->args[i])
300 return pet_stmt_free(stmt);
303 return stmt;
306 static struct pet_stmt *extract_stmt(isl_ctx *ctx, yaml_document_t *document,
307 yaml_node_t *node)
309 struct pet_stmt *stmt;
310 yaml_node_pair_t * pair;
312 if (node->type != YAML_MAPPING_NODE)
313 isl_die(ctx, isl_error_invalid, "expecting mapping",
314 return NULL);
316 stmt = isl_calloc_type(ctx, struct pet_stmt);
317 if (!stmt)
318 return NULL;
320 for (pair = node->data.mapping.pairs.start;
321 pair < node->data.mapping.pairs.top; ++pair) {
322 yaml_node_t *key, *value;
324 key = yaml_document_get_node(document, pair->key);
325 value = yaml_document_get_node(document, pair->value);
327 if (key->type != YAML_SCALAR_NODE)
328 isl_die(ctx, isl_error_invalid, "expecting scalar key",
329 return pet_stmt_free(stmt));
331 if (!strcmp((char *) key->data.scalar.value, "line"))
332 stmt->line = extract_int(ctx, document, value);
333 if (!strcmp((char *) key->data.scalar.value, "domain"))
334 stmt->domain = extract_set(ctx, document, value);
335 if (!strcmp((char *) key->data.scalar.value, "schedule"))
336 stmt->schedule = extract_map(ctx, document, value);
337 if (!strcmp((char *) key->data.scalar.value, "body"))
338 stmt->body = extract_expr(ctx, document, value);
340 if (!strcmp((char *) key->data.scalar.value, "arguments"))
341 stmt = extract_stmt_arguments(ctx, document,
342 value, stmt);
343 if (!stmt)
344 return NULL;
347 return stmt;
350 static struct pet_scop *extract_statements(isl_ctx *ctx,
351 yaml_document_t *document, yaml_node_t *node, struct pet_scop *scop)
353 int i;
354 yaml_node_item_t *item;
356 if (node->type != YAML_SEQUENCE_NODE)
357 isl_die(ctx, isl_error_invalid, "expecting sequence",
358 return NULL);
360 scop->n_stmt = node->data.sequence.items.top
361 - node->data.sequence.items.start;
362 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, scop->n_stmt);
363 if (!scop->stmts)
364 return pet_scop_free(scop);
366 for (item = node->data.sequence.items.start, i = 0;
367 item < node->data.sequence.items.top; ++item, ++i) {
368 yaml_node_t *n;
370 n = yaml_document_get_node(document, *item);
371 scop->stmts[i] = extract_stmt(ctx, document, n);
372 if (!scop->stmts[i])
373 return pet_scop_free(scop);
376 return scop;
379 static struct pet_scop *extract_scop(isl_ctx *ctx, yaml_document_t *document,
380 yaml_node_t *node)
382 struct pet_scop *scop;
383 yaml_node_pair_t * pair;
385 if (!node)
386 return NULL;
388 if (node->type != YAML_MAPPING_NODE)
389 isl_die(ctx, isl_error_invalid, "expecting mapping",
390 return NULL);
392 scop = pet_scop_alloc(ctx);
393 if (!scop)
394 return NULL;
396 for (pair = node->data.mapping.pairs.start;
397 pair < node->data.mapping.pairs.top; ++pair) {
398 yaml_node_t *key, *value;
400 key = yaml_document_get_node(document, pair->key);
401 value = yaml_document_get_node(document, pair->value);
403 if (key->type != YAML_SCALAR_NODE)
404 isl_die(ctx, isl_error_invalid, "expecting scalar key",
405 return pet_scop_free(scop));
406 if (!strcmp((char *) key->data.scalar.value, "context"))
407 scop->context = extract_set(ctx, document, value);
408 if (!strcmp((char *) key->data.scalar.value, "context_value"))
409 scop->context_value = extract_set(ctx, document, value);
410 if (!strcmp((char *) key->data.scalar.value, "arrays"))
411 scop = extract_arrays(ctx, document, value, scop);
412 if (!strcmp((char *) key->data.scalar.value, "statements"))
413 scop = extract_statements(ctx, document, value, scop);
414 if (!scop)
415 return NULL;
418 if (!scop->context_value) {
419 isl_space *space = isl_space_params_alloc(ctx, 0);
420 scop->context_value = isl_set_universe(space);
421 if (!scop->context_value)
422 return pet_scop_free(scop);
425 return scop;
428 /* Extract a pet_scop from the YAML description in "in".
430 struct pet_scop *pet_scop_parse(isl_ctx *ctx, FILE *in)
432 struct pet_scop *scop = NULL;
433 yaml_parser_t parser;
434 yaml_node_t *root;
435 yaml_document_t document = { 0 };
437 yaml_parser_initialize(&parser);
439 yaml_parser_set_input_file(&parser, in);
441 if (!yaml_parser_load(&parser, &document))
442 goto error;
444 root = yaml_document_get_root_node(&document);
446 scop = extract_scop(ctx, &document, root);
448 yaml_document_delete(&document);
450 yaml_parser_delete(&parser);
452 return scop;
453 error:
454 yaml_parser_delete(&parser);
455 pet_scop_free(scop);
456 return NULL;