simplify ppcg_print_macros
[ppcg.git] / print.c
blob3fdcbedb84784bf7e46240cd6e95f62a4a1733cc
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"
14 #include "util.h"
16 __isl_give isl_printer *ppcg_start_block(__isl_take isl_printer *p)
18 p = isl_printer_start_line(p);
19 p = isl_printer_print_str(p, "{");
20 p = isl_printer_end_line(p);
21 p = isl_printer_indent(p, 2);
22 return p;
25 __isl_give isl_printer *ppcg_end_block(__isl_take isl_printer *p)
27 p = isl_printer_indent(p, -2);
28 p = isl_printer_start_line(p);
29 p = isl_printer_print_str(p, "}");
30 p = isl_printer_end_line(p);
31 return p;
34 /* Print the required macros for "node".
36 __isl_give isl_printer *ppcg_print_macros(__isl_take isl_printer *p,
37 __isl_keep isl_ast_node *node)
39 return isl_ast_node_print_macros(node, p);
42 /* Names used for the macros that may appear in a printed isl AST.
44 const char *ppcg_min = "ppcg_min";
45 const char *ppcg_max = "ppcg_max";
46 const char *ppcg_fdiv_q = "ppcg_fdiv_q";
48 /* Set the names of the macros that may appear in a printed isl AST.
50 __isl_give isl_printer *ppcg_set_macro_names(__isl_take isl_printer *p)
52 p = isl_ast_op_type_set_print_name(p, isl_ast_op_min, ppcg_min);
53 p = isl_ast_op_type_set_print_name(p, isl_ast_op_max, ppcg_max);
54 p = isl_ast_op_type_set_print_name(p, isl_ast_op_fdiv_q, ppcg_fdiv_q);
56 return p;
59 /* Given a multi affine expression "mpa" without domain, modify it to have
60 * the schedule space of "build" as domain.
62 * If the schedule space of "build" is a parameter space, then nothing
63 * needs to be done.
64 * Otherwise, "mpa" is first given a 0D domain and then it is combined
65 * with a mapping from the schedule space of "build" to the same 0D domain.
67 __isl_give isl_multi_pw_aff *ppcg_attach_multi_pw_aff(
68 __isl_take isl_multi_pw_aff *mpa, __isl_keep isl_ast_build *build)
70 isl_bool params;
71 isl_space *space;
72 isl_multi_aff *ma;
74 space = isl_ast_build_get_schedule_space(build);
75 params = isl_space_is_params(space);
76 if (params < 0 || params) {
77 isl_space_free(space);
78 if (params < 0)
79 return isl_multi_pw_aff_free(mpa);
80 return mpa;
82 space = isl_space_from_domain(space);
83 ma = isl_multi_aff_zero(space);
84 mpa = isl_multi_pw_aff_from_range(mpa);
85 mpa = isl_multi_pw_aff_pullback_multi_aff(mpa, ma);
87 return mpa;
90 /* Build an access AST expression from "size" using "build".
91 * "size" does not have a domain, but "build" may have a proper schedule space.
92 * First modify "size" to have that schedule space as domain.
94 __isl_give isl_ast_expr *ppcg_build_size_expr(__isl_take isl_multi_pw_aff *size,
95 __isl_keep isl_ast_build *build)
97 size = ppcg_attach_multi_pw_aff(size, build);
98 return isl_ast_build_access_from_multi_pw_aff(build, size);
101 /* Print a declaration for array "array" with size "size" to "p".
103 __isl_give isl_printer *ppcg_print_declaration_with_size(
104 __isl_take isl_printer *p, struct pet_array *array,
105 __isl_keep isl_ast_expr *size)
107 if (!array || !size)
108 return isl_printer_free(p);
110 p = isl_printer_start_line(p);
111 p = isl_printer_print_str(p, array->element_type);
112 p = isl_printer_print_str(p, " ");
113 p = isl_printer_print_ast_expr(p, size);
114 p = isl_printer_print_str(p, ";");
115 p = isl_printer_end_line(p);
117 return p;
120 /* Print a declaration for array "array" to "p", using "build"
121 * to simplify any size expressions.
123 * The size is computed from the extent of the array and is
124 * subsequently converted to an "access expression" by "build".
126 __isl_give isl_printer *ppcg_print_declaration(__isl_take isl_printer *p,
127 struct pet_array *array, __isl_keep isl_ast_build *build)
129 isl_multi_pw_aff *size;
130 isl_ast_expr *expr;
132 if (!array)
133 return isl_printer_free(p);
135 size = ppcg_size_from_extent(isl_set_copy(array->extent));
136 expr = isl_ast_build_access_from_multi_pw_aff(build, size);
137 p = ppcg_print_declaration_with_size(p, array, expr);
138 isl_ast_expr_free(expr);
140 return p;
143 /* Print declarations for the arrays in "scop" that are declared
144 * and that are exposed (if exposed == 1) or not exposed (if exposed == 0).
146 static __isl_give isl_printer *print_declarations(__isl_take isl_printer *p,
147 struct ppcg_scop *scop, int exposed)
149 int i;
150 isl_ast_build *build;
152 if (!scop)
153 return isl_printer_free(p);
155 build = isl_ast_build_from_context(isl_set_copy(scop->context));
156 for (i = 0; i < scop->pet->n_array; ++i) {
157 struct pet_array *array = scop->pet->arrays[i];
159 if (!array->declared)
160 continue;
161 if (array->exposed != exposed)
162 continue;
164 p = ppcg_print_declaration(p, array, build);
166 isl_ast_build_free(build);
168 return p;
171 /* Print declarations for the arrays in "scop" that are declared
172 * and exposed to the code after the scop.
174 __isl_give isl_printer *ppcg_print_exposed_declarations(
175 __isl_take isl_printer *p, struct ppcg_scop *scop)
177 return print_declarations(p, scop, 1);
180 /* Print declarations for the arrays in "scop" that are declared,
181 * but not exposed to the code after the scop.
183 __isl_give isl_printer *ppcg_print_hidden_declarations(
184 __isl_take isl_printer *p, struct ppcg_scop *scop)
186 return print_declarations(p, scop, 0);