2 /**-------------------------------------------------------------------**
4 **-------------------------------------------------------------------**
6 **-------------------------------------------------------------------**
7 ** First version: october 26th 2001 **
8 **-------------------------------------------------------------------**/
11 /******************************************************************************
12 * CLooG : the Chunky Loop Generator (experimental) *
13 ******************************************************************************
15 * Copyright (C) 2001-2005 Cedric Bastoul *
17 * This is free software; you can redistribute it and/or modify it under the *
18 * terms of the GNU General Public License as published by the Free Software *
19 * Foundation; either version 2 of the License, or (at your option) any later *
22 * This software is distributed in the hope that it will be useful, but *
23 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
24 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
27 * You should have received a copy of the GNU General Public License along *
28 * with software; if not, write to the Free Software Foundation, Inc., *
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
31 * CLooG, the Chunky Loop Generator *
32 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr *
34 ******************************************************************************/
35 /* CAUTION: the english used for comments is probably the worst you ever read,
36 * please feel free to correct and improve it !
39 /* June 22nd 2005: General adaptation for GMP.
40 * October 26th 2005: General adaptation from CloogDomain to CloogMatrix data
41 * structure for all constraint systems.
42 * October 27th 2005: General adaptation from CloogEqual to CloogMatrix data
43 * structure for equality spreading.
50 # include "../include/cloog/cloog.h"
53 /******************************************************************************
55 ******************************************************************************/
59 void pprint_expr(struct cloogoptions
*i
, FILE *dst
, struct clast_expr
*e
);
61 void pprint_name(FILE *dst
, struct clast_name
*n
)
63 fprintf(dst
, "%s", n
->name
);
67 * This function returns a string containing the printing of a value (possibly
68 * an iterator or a parameter with its coefficient or a constant).
69 * - val is the coefficient or constant value,
70 * - name is a string containing the name of the iterator or of the parameter,
72 void pprint_term(struct cloogoptions
*i
, FILE *dst
, struct clast_term
*t
)
75 int group
= t
->var
->type
== expr_red
&&
76 ((struct clast_reduction
*) t
->var
)->n
> 1;
77 if (cloog_int_is_one(t
->val
))
79 else if (cloog_int_is_neg_one(t
->val
))
82 cloog_int_print(dst
, t
->val
);
87 pprint_expr(i
, dst
, t
->var
);
91 cloog_int_print(dst
, t
->val
);
94 void pprint_sum(struct cloogoptions
*opt
, FILE *dst
, struct clast_reduction
*r
)
100 assert(r
->elts
[0]->type
== expr_term
);
101 t
= (struct clast_term
*) r
->elts
[0];
102 pprint_term(opt
, dst
, t
);
104 for (i
= 1; i
< r
->n
; ++i
) {
105 assert(r
->elts
[i
]->type
== expr_term
);
106 t
= (struct clast_term
*) r
->elts
[i
];
107 if (cloog_int_is_pos(t
->val
))
109 pprint_term(opt
, dst
, t
);
113 void pprint_binary(struct cloogoptions
*i
, FILE *dst
, struct clast_binary
*b
)
115 const char *s1
= NULL
, *s2
= NULL
, *s3
= NULL
;
116 int group
= b
->LHS
->type
== expr_red
&&
117 ((struct clast_reduction
*) b
->LHS
)->n
> 1;
118 if (i
->language
== LANGUAGE_FORTRAN
) {
121 s1
= "FLOOR(REAL(", s2
= ")/REAL(", s3
= "))";
124 s1
= "CEILING(REAL(", s2
= ")/REAL(", s3
= "))";
128 s1
= "(", s2
= ")/", s3
= "";
130 s1
= "", s2
= "/", s3
= "";
133 s1
= "MOD(", s2
= ", ", s3
= ")";
139 s1
= "floord(", s2
= ",", s3
= ")";
142 s1
= "ceild(", s2
= ",", s3
= ")";
146 s1
= "(", s2
= ")/", s3
= "";
148 s1
= "", s2
= "/", s3
= "";
152 s1
= "(", s2
= ")%", s3
= "";
154 s1
= "", s2
= "%", s3
= "";
158 fprintf(dst
, "%s", s1
);
159 pprint_expr(i
, dst
, b
->LHS
);
160 fprintf(dst
, "%s", s2
);
161 cloog_int_print(dst
, b
->RHS
);
162 fprintf(dst
, "%s", s3
);
165 void pprint_minmax_f(struct cloogoptions
*info
, FILE *dst
, struct clast_reduction
*r
)
170 fprintf(dst
, r
->type
== clast_red_max
? "MAX(" : "MIN(");
171 pprint_expr(info
, dst
, r
->elts
[0]);
172 for (i
= 1; i
< r
->n
; ++i
) {
174 pprint_expr(info
, dst
, r
->elts
[i
]);
179 void pprint_minmax_c(struct cloogoptions
*info
, FILE *dst
, struct clast_reduction
*r
)
182 for (i
= 1; i
< r
->n
; ++i
)
183 fprintf(dst
, r
->type
== clast_red_max
? "max(" : "min(");
185 pprint_expr(info
, dst
, r
->elts
[0]);
186 for (i
= 1; i
< r
->n
; ++i
) {
188 pprint_expr(info
, dst
, r
->elts
[i
]);
193 void pprint_reduction(struct cloogoptions
*i
, FILE *dst
, struct clast_reduction
*r
)
197 pprint_sum(i
, dst
, r
);
202 pprint_expr(i
, dst
, r
->elts
[0]);
205 if (i
->language
== LANGUAGE_FORTRAN
)
206 pprint_minmax_f(i
, dst
, r
);
208 pprint_minmax_c(i
, dst
, r
);
215 void pprint_expr(struct cloogoptions
*i
, FILE *dst
, struct clast_expr
*e
)
221 pprint_name(dst
, (struct clast_name
*) e
);
224 pprint_term(i
, dst
, (struct clast_term
*) e
);
227 pprint_reduction(i
, dst
, (struct clast_reduction
*) e
);
230 pprint_binary(i
, dst
, (struct clast_binary
*) e
);
237 void pprint_equation(struct cloogoptions
*i
, FILE *dst
, struct clast_equation
*eq
)
239 pprint_expr(i
, dst
, eq
->LHS
);
241 fprintf(dst
, " == ");
242 else if (eq
->sign
> 0)
243 fprintf(dst
, " >= ");
245 fprintf(dst
, " <= ");
246 pprint_expr(i
, dst
, eq
->RHS
);
249 void pprint_assignment(struct cloogoptions
*i
, FILE *dst
,
250 struct clast_assignment
*a
)
253 fprintf(dst
, "%s = ", a
->LHS
);
254 pprint_expr(i
, dst
, a
->RHS
);
257 void pprint_user_stmt(struct cloogoptions
*options
, FILE *dst
,
258 struct clast_user_stmt
*u
)
260 struct clast_stmt
*t
;
261 fprintf(dst
, "S%d", u
->statement
->number
);
262 if (options
->cpp
|| u
->substitutions
)
264 for (t
= u
->substitutions
; t
; t
= t
->next
) {
265 assert(CLAST_STMT_IS_A(t
, stmt_ass
));
266 pprint_assignment(options
, dst
, (struct clast_assignment
*)t
);
270 if (options
->cpp
|| u
->substitutions
)
272 if (options
->language
!= LANGUAGE_FORTRAN
)
277 void pprint_stmt_list(struct cloogoptions
*options
, FILE *dst
, int indent
,
278 struct clast_stmt
*s
);
280 void pprint_guard(struct cloogoptions
*options
, FILE *dst
, int indent
,
281 struct clast_guard
*g
)
284 if (options
->language
== LANGUAGE_FORTRAN
)
290 for (k
= 0; k
< g
->n
; ++k
) {
292 if (options
->language
== LANGUAGE_FORTRAN
)
293 fprintf(dst
," .AND. ");
298 pprint_equation(options
, dst
, &g
->eq
[k
]);
303 if (options
->language
== LANGUAGE_FORTRAN
)
304 fprintf(dst
," THEN\n");
308 pprint_stmt_list(options
, dst
, indent
+ INDENT_STEP
, g
->then
);
310 fprintf(dst
, "%*s", indent
, "");
311 if (options
->language
== LANGUAGE_FORTRAN
)
312 fprintf(dst
,"END IF\n");
317 void pprint_for(struct cloogoptions
*options
, FILE *dst
, int indent
,
320 if (options
->language
== LANGUAGE_FORTRAN
)
323 fprintf(dst
, "for (");
326 fprintf(dst
, "%s=", f
->iterator
);
327 pprint_expr(options
, dst
, f
->LB
);
328 } else if (options
->language
== LANGUAGE_FORTRAN
)
329 cloog_die("unbounded loops not allowed in FORTRAN.\n");
331 if (options
->language
== LANGUAGE_FORTRAN
)
337 if (options
->language
!= LANGUAGE_FORTRAN
)
338 fprintf(dst
,"%s<=", f
->iterator
);
339 pprint_expr(options
, dst
, f
->UB
);
340 } else if (options
->language
== LANGUAGE_FORTRAN
)
341 cloog_die("unbounded loops not allowed in FORTRAN.\n");
343 if (options
->language
== LANGUAGE_FORTRAN
) {
344 if (cloog_int_gt_si(f
->stride
, 1))
345 cloog_int_print(dst
, f
->stride
);
349 if (cloog_int_gt_si(f
->stride
, 1)) {
350 fprintf(dst
,";%s+=", f
->iterator
);
351 cloog_int_print(dst
, f
->stride
);
352 fprintf(dst
, ") {\n");
354 fprintf(dst
, ";%s++) {\n", f
->iterator
);
357 pprint_stmt_list(options
, dst
, indent
+ INDENT_STEP
, f
->body
);
359 fprintf(dst
, "%*s", indent
, "");
360 if (options
->language
== LANGUAGE_FORTRAN
)
361 fprintf(dst
,"END DO\n") ;
366 void pprint_stmt_list(struct cloogoptions
*options
, FILE *dst
, int indent
,
367 struct clast_stmt
*s
)
369 for ( ; s
; s
= s
->next
) {
370 if (CLAST_STMT_IS_A(s
, stmt_root
))
372 fprintf(dst
, "%*s", indent
, "");
373 if (CLAST_STMT_IS_A(s
, stmt_ass
)) {
374 pprint_assignment(options
, dst
, (struct clast_assignment
*) s
);
375 if (options
->language
!= LANGUAGE_FORTRAN
)
378 } else if (CLAST_STMT_IS_A(s
, stmt_user
)) {
379 pprint_user_stmt(options
, dst
, (struct clast_user_stmt
*) s
);
380 } else if (CLAST_STMT_IS_A(s
, stmt_for
)) {
381 pprint_for(options
, dst
, indent
, (struct clast_for
*) s
);
382 } else if (CLAST_STMT_IS_A(s
, stmt_guard
)) {
383 pprint_guard(options
, dst
, indent
, (struct clast_guard
*) s
);
384 } else if (CLAST_STMT_IS_A(s
, stmt_block
)) {
386 pprint_stmt_list(options
, dst
, indent
+ INDENT_STEP
,
387 ((struct clast_block
*)s
)->body
);
388 fprintf(dst
, "%*s", indent
, "");
397 /******************************************************************************
398 * Pretty Printing (dirty) functions *
399 ******************************************************************************/
401 void pprint(FILE *foo
, struct clast_stmt
*root
, int indent
, CloogOptions
*options
)
403 pprint_stmt_list(options
, foo
, indent
, root
);