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 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. *
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. *
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 *
32 * CLooG, the Chunky Loop Generator *
33 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr *
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.
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
,
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
);
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
)
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
))
102 cloog_int_print(dst
, t
->val
);
107 pprint_expr(i
, dst
, t
->var
);
111 cloog_int_print(dst
, t
->val
);
114 void pprint_sum(struct cloogoptions
*opt
, FILE *dst
, struct clast_reduction
*r
)
117 struct clast_term
*t
;
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
))
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
== LANGUAGE_FORTRAN
) {
141 s1
= "FLOOR(REAL(", s2
= ")/REAL(", s3
= "))";
144 s1
= "CEILING(REAL(", s2
= ")/REAL(", s3
= "))";
148 s1
= "(", s2
= ")/", s3
= "";
150 s1
= "", s2
= "/", s3
= "";
153 s1
= "MOD(", s2
= ", ", s3
= ")";
159 s1
= "floord(", s2
= ",", s3
= ")";
162 s1
= "ceild(", s2
= ",", s3
= ")";
166 s1
= "(", s2
= ")/", s3
= "";
168 s1
= "", s2
= "/", s3
= "";
172 s1
= "(", s2
= ")%", s3
= "";
174 s1
= "", s2
= "%", s3
= "";
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
)
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
) {
194 pprint_expr(info
, dst
, r
->elts
[i
]);
199 void pprint_minmax_c(struct cloogoptions
*info
, FILE *dst
, struct clast_reduction
*r
)
202 for (i
= 1; i
< r
->n
; ++i
)
203 fprintf(dst
, r
->type
== clast_red_max
? "max(" : "min(");
205 pprint_expr(info
, dst
, r
->elts
[0]);
206 for (i
= 1; i
< r
->n
; ++i
) {
208 pprint_expr(info
, dst
, r
->elts
[i
]);
213 void pprint_reduction(struct cloogoptions
*i
, FILE *dst
, struct clast_reduction
*r
)
217 pprint_sum(i
, dst
, r
);
222 pprint_expr(i
, dst
, r
->elts
[0]);
225 if (i
->language
== LANGUAGE_FORTRAN
)
226 pprint_minmax_f(i
, dst
, r
);
228 pprint_minmax_c(i
, dst
, r
);
235 void pprint_expr(struct cloogoptions
*i
, FILE *dst
, struct clast_expr
*e
)
240 case clast_expr_name
:
241 pprint_name(dst
, (struct clast_name
*) e
);
243 case clast_expr_term
:
244 pprint_term(i
, dst
, (struct clast_term
*) e
);
247 pprint_reduction(i
, dst
, (struct clast_reduction
*) e
);
250 pprint_binary(i
, dst
, (struct clast_binary
*) e
);
257 void pprint_equation(struct cloogoptions
*i
, FILE *dst
, struct clast_equation
*eq
)
259 pprint_expr(i
, dst
, eq
->LHS
);
261 fprintf(dst
, " == ");
262 else if (eq
->sign
> 0)
263 fprintf(dst
, " >= ");
265 fprintf(dst
, " <= ");
266 pprint_expr(i
, dst
, eq
->RHS
);
269 void pprint_assignment(struct cloogoptions
*i
, FILE *dst
,
270 struct clast_assignment
*a
)
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
);
284 fprintf(dst
, "S%d", u
->statement
->number
);
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
);
293 if (options
->language
!= LANGUAGE_FORTRAN
)
298 void pprint_guard(struct cloogoptions
*options
, FILE *dst
, int indent
,
299 struct clast_guard
*g
)
302 if (options
->language
== LANGUAGE_FORTRAN
)
308 for (k
= 0; k
< g
->n
; ++k
) {
310 if (options
->language
== LANGUAGE_FORTRAN
)
311 fprintf(dst
," .AND. ");
316 pprint_equation(options
, dst
, &g
->eq
[k
]);
321 if (options
->language
== LANGUAGE_FORTRAN
)
322 fprintf(dst
," THEN\n");
326 pprint_stmt_list(options
, dst
, indent
+ INDENT_STEP
, g
->then
);
328 fprintf(dst
, "%*s", indent
, "");
329 if (options
->language
== LANGUAGE_FORTRAN
)
330 fprintf(dst
,"END IF\n");
335 void pprint_for(struct cloogoptions
*options
, FILE *dst
, int indent
,
338 if (options
->language
== LANGUAGE_FORTRAN
)
341 fprintf(dst
, "for (");
344 fprintf(dst
, "%s=", f
->iterator
);
345 pprint_expr(options
, dst
, f
->LB
);
346 } else if (options
->language
== LANGUAGE_FORTRAN
)
347 cloog_die("unbounded loops not allowed in FORTRAN.\n");
349 if (options
->language
== LANGUAGE_FORTRAN
)
355 if (options
->language
!= LANGUAGE_FORTRAN
)
356 fprintf(dst
,"%s<=", f
->iterator
);
357 pprint_expr(options
, dst
, f
->UB
);
358 } else if (options
->language
== LANGUAGE_FORTRAN
)
359 cloog_die("unbounded loops not allowed in FORTRAN.\n");
361 if (options
->language
== LANGUAGE_FORTRAN
) {
362 if (cloog_int_gt_si(f
->stride
, 1))
363 cloog_int_print(dst
, f
->stride
);
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");
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
== LANGUAGE_FORTRAN
)
379 fprintf(dst
,"END DO\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
))
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
!= LANGUAGE_FORTRAN
)
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
)) {
404 pprint_stmt_list(options
, dst
, indent
+ INDENT_STEP
,
405 ((struct clast_block
*)s
)->body
);
406 fprintf(dst
, "%*s", indent
, "");
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
);