update isl for isl_schedule_constraints_get_coincidence
[ppcg.git] / print.c
blob1e008fb4bf750c227cfb2014098998bd857a99b1
1 /*
2 * Copyright 2012-2013 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
8 */
10 #include <isl/aff.h>
11 #include <isl/ast_build.h>
13 #include "print.h"
15 __isl_give isl_printer *ppcg_start_block(__isl_take isl_printer *p)
17 p = isl_printer_start_line(p);
18 p = isl_printer_print_str(p, "{");
19 p = isl_printer_end_line(p);
20 p = isl_printer_indent(p, 2);
21 return p;
24 __isl_give isl_printer *ppcg_end_block(__isl_take isl_printer *p)
26 p = isl_printer_indent(p, -2);
27 p = isl_printer_start_line(p);
28 p = isl_printer_print_str(p, "}");
29 p = isl_printer_end_line(p);
30 return p;
33 /* Print "extent" as a sequence of
35 * [1 + maximal_value]
37 * one for each dimension.
39 static __isl_give isl_printer *print_extent(__isl_take isl_printer *p,
40 __isl_keep isl_set *extent)
42 int i, n;
44 n = isl_set_dim(extent, isl_dim_set);
45 if (n == 0)
46 return p;
48 for (i = 0; i < n; ++i) {
49 isl_set *dom;
50 isl_local_space *ls;
51 isl_aff *one;
52 isl_pw_aff *bound;
54 bound = isl_set_dim_max(isl_set_copy(extent), i);
55 dom = isl_pw_aff_domain(isl_pw_aff_copy(bound));
56 ls = isl_local_space_from_space(isl_set_get_space(dom));
57 one = isl_aff_zero_on_domain(ls);
58 one = isl_aff_add_constant_si(one, 1);
59 bound = isl_pw_aff_add(bound, isl_pw_aff_alloc(dom, one));
61 p = isl_printer_print_str(p, "[");
62 p = isl_printer_print_pw_aff(p, bound);
63 p = isl_printer_print_str(p, "]");
65 isl_pw_aff_free(bound);
68 return p;
71 /* Print a declaration for array "array" to "p".
73 __isl_give isl_printer *ppcg_print_declaration(__isl_take isl_printer *p,
74 struct pet_array *array)
76 const char *name;
78 if (!array)
79 return isl_printer_free(p);
81 name = isl_set_get_tuple_name(array->extent);
83 p = isl_printer_start_line(p);
84 p = isl_printer_print_str(p, array->element_type);
85 p = isl_printer_print_str(p, " ");
86 p = isl_printer_print_str(p, name);
87 p = print_extent(p, array->extent);
88 p = isl_printer_print_str(p, ";");
89 p = isl_printer_end_line(p);
91 return p;
94 /* Print declarations for the arrays in "scop" that are declared
95 * and that are exposed (if exposed == 1) or not exposed (if exposed == 0).
97 static __isl_give isl_printer *print_declarations(__isl_take isl_printer *p,
98 struct ppcg_scop *scop, int exposed)
100 int i;
102 if (!scop)
103 return isl_printer_free(p);
105 for (i = 0; i < scop->pet->n_array; ++i) {
106 struct pet_array *array = scop->pet->arrays[i];
108 if (!array->declared)
109 continue;
110 if (array->exposed != exposed)
111 continue;
113 p = ppcg_print_declaration(p, array);
116 return p;
119 /* Print declarations for the arrays in "scop" that are declared
120 * and exposed to the code after the scop.
122 __isl_give isl_printer *ppcg_print_exposed_declarations(
123 __isl_take isl_printer *p, struct ppcg_scop *scop)
125 return print_declarations(p, scop, 1);
128 /* Print declarations for the arrays in "scop" that are declared,
129 * but not exposed to the code after the scop.
131 __isl_give isl_printer *ppcg_print_hidden_declarations(
132 __isl_take isl_printer *p, struct ppcg_scop *scop)
134 return print_declarations(p, scop, 0);
137 /* Internal data structure for print_guarded_user.
139 * fn is the function that should be called to print the body.
140 * user is the argument that should be passed to this function.
142 struct ppcg_print_guarded_data {
143 __isl_give isl_printer *(*fn)(__isl_take isl_printer *p, void *user);
144 void *user;
147 /* Print the body of the if statement expressing the guard passed
148 * to "ppcg_print_guarded" by calling data->fn.
150 static __isl_give isl_printer *print_guarded_user(__isl_take isl_printer *p,
151 __isl_take isl_ast_print_options *options,
152 __isl_keep isl_ast_node *node, void *user)
154 struct ppcg_print_guarded_data *data = user;
156 p = data->fn(p, data->user);
158 isl_ast_print_options_free(options);
159 return p;
162 /* Print a condition for the given "guard" within the given "context"
163 * on "p", calling "fn" with "user" to print the body of the if statement.
164 * If the guard is implied by the context, then no if statement is printed
165 * and the body is printed directly to "p".
167 * Both "guard" and "context" are assumed to be parameter sets.
169 * We slightly abuse the AST generator to print this guard.
170 * In particular, we create a trivial schedule for an iteration
171 * domain with a single instance, restricted by the guard.
173 __isl_give isl_printer *ppcg_print_guarded(__isl_take isl_printer *p,
174 __isl_take isl_set *guard, __isl_take isl_set *context,
175 __isl_give isl_printer *(*fn)(__isl_take isl_printer *p, void *user),
176 void *user)
178 struct ppcg_print_guarded_data data = { fn, user };
179 isl_ctx *ctx;
180 isl_union_map *schedule;
181 isl_ast_build *build;
182 isl_ast_node *tree;
183 isl_ast_print_options *options;
185 ctx = isl_printer_get_ctx(p);
186 guard = isl_set_from_params(guard);
187 schedule = isl_union_map_from_map(isl_map_from_domain(guard));
188 build = isl_ast_build_from_context(context);
189 tree = isl_ast_build_node_from_schedule_map(build, schedule);
190 isl_ast_build_free(build);
192 options = isl_ast_print_options_alloc(ctx);
193 options = isl_ast_print_options_set_print_user(options,
194 &print_guarded_user, &data);
195 p = isl_ast_node_print(tree, p, options);
196 isl_ast_node_free(tree);
198 return p;