Add CLOOG_ prefix to LANGUAGE_* macros
[cloog/uuh.git] / source / pprint.c
blob9269beedab495295c8c26e539eb13f4820e8c996
2 /**-------------------------------------------------------------------**
3 ** CLooG **
4 **-------------------------------------------------------------------**
5 ** pprint.c **
6 **-------------------------------------------------------------------**
7 ** First version: october 26th 2001 **
8 **-------------------------------------------------------------------**/
11 /******************************************************************************
12 * CLooG : the Chunky Loop Generator (experimental) *
13 ******************************************************************************
14 * *
15 * Copyright (C) 2001-2005 Cedric Bastoul *
16 * *
17 * This library is free software; you can redistribute it and/or *
18 * modify it under the terms of the GNU Lesser General Public *
19 * License as published by the Free Software Foundation; either *
20 * version 2.1 of the License, or (at your option) any later version. *
21 * *
22 * This library is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
25 * Lesser General Public License for more details. *
26 * *
27 * You should have received a copy of the GNU Lesser General Public *
28 * License along with this library; if not, write to the Free Software *
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
30 * Boston, MA 02110-1301 USA *
31 * *
32 * CLooG, the Chunky Loop Generator *
33 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr *
34 * *
35 ******************************************************************************/
36 /* CAUTION: the english used for comments is probably the worst you ever read,
37 * please feel free to correct and improve it !
40 /* June 22nd 2005: General adaptation for GMP.
41 * October 26th 2005: General adaptation from CloogDomain to Matrix data
42 * structure for all constraint systems.
43 * October 27th 2005: General adaptation from CloogEqual to Matrix data
44 * structure for equality spreading.
47 # include <stdlib.h>
48 # include <stdio.h>
49 # include <string.h>
50 #include <assert.h>
51 # include "../include/cloog/cloog.h"
54 static void pprint_name(FILE *dst, struct clast_name *n);
55 static void pprint_term(struct cloogoptions *i, FILE *dst, struct clast_term *t);
56 static void pprint_sum(struct cloogoptions *opt,
57 FILE *dst, struct clast_reduction *r);
58 static void pprint_binary(struct cloogoptions *i,
59 FILE *dst, struct clast_binary *b);
60 static void pprint_minmax_f(struct cloogoptions *info,
61 FILE *dst, struct clast_reduction *r);
62 static void pprint_minmax_c(struct cloogoptions *info,
63 FILE *dst, struct clast_reduction *r);
64 static void pprint_reduction(struct cloogoptions *i,
65 FILE *dst, struct clast_reduction *r);
66 static void pprint_expr(struct cloogoptions *i, FILE *dst, struct clast_expr *e);
67 static void pprint_equation(struct cloogoptions *i,
68 FILE *dst, struct clast_equation *eq);
69 static void pprint_assignment(struct cloogoptions *i, FILE *dst,
70 struct clast_assignment *a);
71 static void pprint_user_stmt(struct cloogoptions *options, FILE *dst,
72 struct clast_user_stmt *u);
73 static void pprint_guard(struct cloogoptions *options, FILE *dst, int indent,
74 struct clast_guard *g);
75 static void pprint_for(struct cloogoptions *options, FILE *dst, int indent,
76 struct clast_for *f);
77 static void pprint_stmt_list(struct cloogoptions *options, FILE *dst, int indent,
78 struct clast_stmt *s);
81 void pprint_name(FILE *dst, struct clast_name *n)
83 fprintf(dst, "%s", n->name);
86 /**
87 * This function returns a string containing the printing of a value (possibly
88 * an iterator or a parameter with its coefficient or a constant).
89 * - val is the coefficient or constant value,
90 * - name is a string containing the name of the iterator or of the parameter,
92 void pprint_term(struct cloogoptions *i, FILE *dst, struct clast_term *t)
94 if (t->var) {
95 int group = t->var->type == clast_expr_red &&
96 ((struct clast_reduction*) t->var)->n > 1;
97 if (cloog_int_is_one(t->val))
99 else if (cloog_int_is_neg_one(t->val))
100 fprintf(dst, "-");
101 else {
102 cloog_int_print(dst, t->val);
103 fprintf(dst, "*");
105 if (group)
106 fprintf(dst, "(");
107 pprint_expr(i, dst, t->var);
108 if (group)
109 fprintf(dst, ")");
110 } else
111 cloog_int_print(dst, t->val);
114 void pprint_sum(struct cloogoptions *opt, FILE *dst, struct clast_reduction *r)
116 int i;
117 struct clast_term *t;
119 assert(r->n >= 1);
120 assert(r->elts[0]->type == clast_expr_term);
121 t = (struct clast_term *) r->elts[0];
122 pprint_term(opt, dst, t);
124 for (i = 1; i < r->n; ++i) {
125 assert(r->elts[i]->type == clast_expr_term);
126 t = (struct clast_term *) r->elts[i];
127 if (cloog_int_is_pos(t->val))
128 fprintf(dst, "+");
129 pprint_term(opt, dst, t);
133 void pprint_binary(struct cloogoptions *i, FILE *dst, struct clast_binary *b)
135 const char *s1 = NULL, *s2 = NULL, *s3 = NULL;
136 int group = b->LHS->type == clast_expr_red &&
137 ((struct clast_reduction*) b->LHS)->n > 1;
138 if (i->language == CLOOG_LANGUAGE_FORTRAN) {
139 switch (b->type) {
140 case clast_bin_fdiv:
141 s1 = "FLOOR(REAL(", s2 = ")/REAL(", s3 = "))";
142 break;
143 case clast_bin_cdiv:
144 s1 = "CEILING(REAL(", s2 = ")/REAL(", s3 = "))";
145 break;
146 case clast_bin_div:
147 if (group)
148 s1 = "(", s2 = ")/", s3 = "";
149 else
150 s1 = "", s2 = "/", s3 = "";
151 break;
152 case clast_bin_mod:
153 s1 = "MOD(", s2 = ", ", s3 = ")";
154 break;
156 } else {
157 switch (b->type) {
158 case clast_bin_fdiv:
159 s1 = "floord(", s2 = ",", s3 = ")";
160 break;
161 case clast_bin_cdiv:
162 s1 = "ceild(", s2 = ",", s3 = ")";
163 break;
164 case clast_bin_div:
165 if (group)
166 s1 = "(", s2 = ")/", s3 = "";
167 else
168 s1 = "", s2 = "/", s3 = "";
169 break;
170 case clast_bin_mod:
171 if (group)
172 s1 = "(", s2 = ")%", s3 = "";
173 else
174 s1 = "", s2 = "%", s3 = "";
175 break;
178 fprintf(dst, "%s", s1);
179 pprint_expr(i, dst, b->LHS);
180 fprintf(dst, "%s", s2);
181 cloog_int_print(dst, b->RHS);
182 fprintf(dst, "%s", s3);
185 void pprint_minmax_f(struct cloogoptions *info, FILE *dst, struct clast_reduction *r)
187 int i;
188 if (r->n == 0)
189 return;
190 fprintf(dst, r->type == clast_red_max ? "MAX(" : "MIN(");
191 pprint_expr(info, dst, r->elts[0]);
192 for (i = 1; i < r->n; ++i) {
193 fprintf(dst, ",");
194 pprint_expr(info, dst, r->elts[i]);
196 fprintf(dst, ")");
199 void pprint_minmax_c(struct cloogoptions *info, FILE *dst, struct clast_reduction *r)
201 int i;
202 for (i = 1; i < r->n; ++i)
203 fprintf(dst, r->type == clast_red_max ? "max(" : "min(");
204 if (r->n > 0)
205 pprint_expr(info, dst, r->elts[0]);
206 for (i = 1; i < r->n; ++i) {
207 fprintf(dst, ",");
208 pprint_expr(info, dst, r->elts[i]);
209 fprintf(dst, ")");
213 void pprint_reduction(struct cloogoptions *i, FILE *dst, struct clast_reduction *r)
215 switch (r->type) {
216 case clast_red_sum:
217 pprint_sum(i, dst, r);
218 break;
219 case clast_red_min:
220 case clast_red_max:
221 if (r->n == 1) {
222 pprint_expr(i, dst, r->elts[0]);
223 break;
225 if (i->language == CLOOG_LANGUAGE_FORTRAN)
226 pprint_minmax_f(i, dst, r);
227 else
228 pprint_minmax_c(i, dst, r);
229 break;
230 default:
231 assert(0);
235 void pprint_expr(struct cloogoptions *i, FILE *dst, struct clast_expr *e)
237 if (!e)
238 return;
239 switch (e->type) {
240 case clast_expr_name:
241 pprint_name(dst, (struct clast_name*) e);
242 break;
243 case clast_expr_term:
244 pprint_term(i, dst, (struct clast_term*) e);
245 break;
246 case clast_expr_red:
247 pprint_reduction(i, dst, (struct clast_reduction*) e);
248 break;
249 case clast_expr_bin:
250 pprint_binary(i, dst, (struct clast_binary*) e);
251 break;
252 default:
253 assert(0);
257 void pprint_equation(struct cloogoptions *i, FILE *dst, struct clast_equation *eq)
259 pprint_expr(i, dst, eq->LHS);
260 if (eq->sign == 0)
261 fprintf(dst, " == ");
262 else if (eq->sign > 0)
263 fprintf(dst, " >= ");
264 else
265 fprintf(dst, " <= ");
266 pprint_expr(i, dst, eq->RHS);
269 void pprint_assignment(struct cloogoptions *i, FILE *dst,
270 struct clast_assignment *a)
272 if (a->LHS)
273 fprintf(dst, "%s = ", a->LHS);
274 pprint_expr(i, dst, a->RHS);
277 void pprint_user_stmt(struct cloogoptions *options, FILE *dst,
278 struct clast_user_stmt *u)
280 struct clast_stmt *t;
281 if (u->statement->name)
282 fprintf(dst, "%s", u->statement->name);
283 else
284 fprintf(dst, "S%d", u->statement->number);
285 fprintf(dst, "(");
286 for (t = u->substitutions; t; t = t->next) {
287 assert(CLAST_STMT_IS_A(t, stmt_ass));
288 pprint_assignment(options, dst, (struct clast_assignment *)t);
289 if (t->next)
290 fprintf(dst, ",");
292 fprintf(dst, ")");
293 if (options->language != CLOOG_LANGUAGE_FORTRAN)
294 fprintf(dst, ";");
295 fprintf(dst, "\n");
298 void pprint_guard(struct cloogoptions *options, FILE *dst, int indent,
299 struct clast_guard *g)
301 int k;
302 if (options->language == CLOOG_LANGUAGE_FORTRAN)
303 fprintf(dst,"IF ");
304 else
305 fprintf(dst,"if ");
306 if (g->n > 1)
307 fprintf(dst,"(");
308 for (k = 0; k < g->n; ++k) {
309 if (k > 0) {
310 if (options->language == CLOOG_LANGUAGE_FORTRAN)
311 fprintf(dst," .AND. ");
312 else
313 fprintf(dst," && ");
315 fprintf(dst,"(");
316 pprint_equation(options, dst, &g->eq[k]);
317 fprintf(dst,")");
319 if (g->n > 1)
320 fprintf(dst,")");
321 if (options->language == CLOOG_LANGUAGE_FORTRAN)
322 fprintf(dst," THEN\n");
323 else
324 fprintf(dst," {\n");
326 pprint_stmt_list(options, dst, indent + INDENT_STEP, g->then);
328 fprintf(dst, "%*s", indent, "");
329 if (options->language == CLOOG_LANGUAGE_FORTRAN)
330 fprintf(dst,"END IF\n");
331 else
332 fprintf(dst,"}\n");
335 void pprint_for(struct cloogoptions *options, FILE *dst, int indent,
336 struct clast_for *f)
338 if (options->language == CLOOG_LANGUAGE_FORTRAN)
339 fprintf(dst, "DO ");
340 else
341 fprintf(dst, "for (");
343 if (f->LB) {
344 fprintf(dst, "%s=", f->iterator);
345 pprint_expr(options, dst, f->LB);
346 } else if (options->language == CLOOG_LANGUAGE_FORTRAN)
347 cloog_die("unbounded loops not allowed in FORTRAN.\n");
349 if (options->language == CLOOG_LANGUAGE_FORTRAN)
350 fprintf(dst,", ");
351 else
352 fprintf(dst,";");
354 if (f->UB) {
355 if (options->language != CLOOG_LANGUAGE_FORTRAN)
356 fprintf(dst,"%s<=", f->iterator);
357 pprint_expr(options, dst, f->UB);
358 } else if (options->language == CLOOG_LANGUAGE_FORTRAN)
359 cloog_die("unbounded loops not allowed in FORTRAN.\n");
361 if (options->language == CLOOG_LANGUAGE_FORTRAN) {
362 if (cloog_int_gt_si(f->stride, 1))
363 cloog_int_print(dst, f->stride);
364 fprintf(dst,"\n");
366 else {
367 if (cloog_int_gt_si(f->stride, 1)) {
368 fprintf(dst,";%s+=", f->iterator);
369 cloog_int_print(dst, f->stride);
370 fprintf(dst, ") {\n");
371 } else
372 fprintf(dst, ";%s++) {\n", f->iterator);
375 pprint_stmt_list(options, dst, indent + INDENT_STEP, f->body);
377 fprintf(dst, "%*s", indent, "");
378 if (options->language == CLOOG_LANGUAGE_FORTRAN)
379 fprintf(dst,"END DO\n") ;
380 else
381 fprintf(dst,"}\n") ;
384 void pprint_stmt_list(struct cloogoptions *options, FILE *dst, int indent,
385 struct clast_stmt *s)
387 for ( ; s; s = s->next) {
388 if (CLAST_STMT_IS_A(s, stmt_root))
389 continue;
390 fprintf(dst, "%*s", indent, "");
391 if (CLAST_STMT_IS_A(s, stmt_ass)) {
392 pprint_assignment(options, dst, (struct clast_assignment *) s);
393 if (options->language != CLOOG_LANGUAGE_FORTRAN)
394 fprintf(dst, ";");
395 fprintf(dst, "\n");
396 } else if (CLAST_STMT_IS_A(s, stmt_user)) {
397 pprint_user_stmt(options, dst, (struct clast_user_stmt *) s);
398 } else if (CLAST_STMT_IS_A(s, stmt_for)) {
399 pprint_for(options, dst, indent, (struct clast_for *) s);
400 } else if (CLAST_STMT_IS_A(s, stmt_guard)) {
401 pprint_guard(options, dst, indent, (struct clast_guard *) s);
402 } else if (CLAST_STMT_IS_A(s, stmt_block)) {
403 fprintf(dst, "{\n");
404 pprint_stmt_list(options, dst, indent + INDENT_STEP,
405 ((struct clast_block *)s)->body);
406 fprintf(dst, "%*s", indent, "");
407 fprintf(dst, "}\n");
408 } else {
409 assert(0);
415 /******************************************************************************
416 * Pretty Printing (dirty) functions *
417 ******************************************************************************/
419 void clast_pprint(FILE *foo, struct clast_stmt *root,
420 int indent, CloogOptions *options)
422 pprint_stmt_list(options, foo, indent, root);