4 #include "cloog_util.h"
8 static ostream
& operator<< (ostream
& os
, clast_name
*t
);
9 static ostream
& operator<< (ostream
& os
, clast_term
*t
);
10 static ostream
& operator<< (ostream
& os
, clast_reduction
*r
);
11 static ostream
& operator<< (ostream
& os
, clast_binary
*b
);
13 static ostream
& operator<< (ostream
& os
, clast_expr
*e
)
19 os
<< ((clast_name
*) e
);
22 os
<< ((clast_term
*) e
);
25 os
<< ((clast_reduction
*) e
);
28 os
<< ((clast_binary
*) e
);
36 static ostream
& operator<< (ostream
& os
, clast_name
*n
)
42 static ostream
& operator<< (ostream
& os
, clast_term
*t
)
47 if (t
->var
->type
== clast_expr_red
)
50 if (t
->var
->type
== clast_expr_red
)
56 static ostream
& operator<< (ostream
& os
, clast_reduction
*r
)
63 assert(r
->type
== clast_red_sum
);
65 for (int i
= 1; i
< r
->n
; ++i
) {
66 assert(r
->elts
[i
]->type
== clast_expr_term
);
67 clast_term
*t
= ((clast_term
*) r
->elts
[i
]);
68 if (cloog_int_is_pos(t
->val
))
75 static void clast_negate(clast_expr
*e
);
77 static void clast_negate(clast_term
*t
)
79 isl_int_neg(t
->val
, t
->val
);
82 static void clast_negate(clast_reduction
*r
)
85 clast_negate(r
->elts
[0]);
89 assert(r
->type
== clast_red_sum
);
90 for (int i
= 0; i
< r
->n
; ++i
)
91 clast_negate(r
->elts
[i
]);
94 static void clast_negate(clast_expr
*e
)
98 clast_negate((clast_term
*) e
);
101 clast_negate((clast_reduction
*) e
);
108 static ostream
& operator<< (ostream
& os
, clast_binary
*b
)
112 os
<< "mod(" << b
->LHS
<< "," << b
->RHS
<< ")";
115 os
<< "(" << b
->LHS
<< ")/(" << b
->RHS
<< ")";
118 os
<< "div(" << b
->LHS
<< "," << b
->RHS
<< ")";
121 clast_negate(b
->LHS
);
122 os
<< "-div(" << b
->LHS
<< "," << b
->RHS
<< ")";
123 clast_negate(b
->LHS
);
130 static string
expr2string(clast_expr
*e
)
132 std::ostringstream strm
;
139 static string
int2string(isl_int i
)
141 std::ostringstream strm
;
148 static void writeStmt(xmlTextWriterPtr writer
, clast_stmt
*s
);
150 static void writeStmt(xmlTextWriterPtr writer
, clast_for
*f
)
154 rc
= xmlTextWriterStartElement(writer
, BAD_CAST
"for");
157 rc
= xmlTextWriterWriteAttribute(writer
, BAD_CAST
"iterator",
158 BAD_CAST f
->iterator
);
161 rc
= xmlTextWriterWriteAttribute(writer
, BAD_CAST
"LB",
162 BAD_CAST
expr2string(f
->LB
).c_str());
165 rc
= xmlTextWriterWriteAttribute(writer
, BAD_CAST
"UB",
166 BAD_CAST
expr2string(f
->UB
).c_str());
169 rc
= xmlTextWriterWriteAttribute(writer
, BAD_CAST
"stride",
170 BAD_CAST
int2string(f
->stride
).c_str());
173 writeStmt(writer
, f
->body
);
175 rc
= xmlTextWriterEndElement(writer
);
179 static void writeStmt(xmlTextWriterPtr writer
, clast_guard
*g
)
186 rc
= xmlTextWriterStartElement(writer
, BAD_CAST
"if");
189 rc
= xmlTextWriterWriteAttribute(writer
, BAD_CAST
"LHS",
190 BAD_CAST
expr2string(g
->eq
[0].LHS
).c_str());
193 rc
= xmlTextWriterWriteAttribute(writer
, BAD_CAST
"RHS",
194 BAD_CAST
expr2string(g
->eq
[0].RHS
).c_str());
197 snprintf(buf
, 10, "%d", g
->eq
[0].sign
);
198 rc
= xmlTextWriterWriteAttribute(writer
, BAD_CAST
"sign", BAD_CAST buf
);
201 writeStmt(writer
, g
->then
);
203 rc
= xmlTextWriterEndElement(writer
);
207 static void writeStmt(xmlTextWriterPtr writer
, clast_user_stmt
*u
)
210 char *name
= u
->statement
->name
;
213 if ((I
= strchr(name
, 'I')) && I
[1] == 'D') {
214 rc
= xmlTextWriterStartElement(writer
, BAD_CAST
"var");
217 rc
= xmlTextWriterWriteAttribute(writer
, BAD_CAST
"name", BAD_CAST name
);
219 } else if (!strchr(name
, 'P')) {
220 rc
= xmlTextWriterStartElement(writer
, BAD_CAST
"stmt");
223 rc
= xmlTextWriterWriteAttribute(writer
, BAD_CAST
"node", BAD_CAST name
);
226 rc
= xmlTextWriterStartElement(writer
, BAD_CAST
"port");
229 rc
= xmlTextWriterWriteAttribute(writer
, BAD_CAST
"name", BAD_CAST name
);
233 rc
= xmlTextWriterEndElement(writer
);
237 static void writeStmt(xmlTextWriterPtr writer
, clast_stmt
*s
)
239 for ( ; s
; s
= s
->next
) {
240 if (CLAST_STMT_IS_A(s
, stmt_user
)) {
241 writeStmt(writer
, (clast_user_stmt
*) s
);
242 } else if (CLAST_STMT_IS_A(s
, stmt_for
)) {
243 writeStmt(writer
, (clast_for
*) s
);
244 } else if (CLAST_STMT_IS_A(s
, stmt_guard
)) {
245 writeStmt(writer
, (clast_guard
*) s
);
250 void writeAST(xmlTextWriterPtr writer
, CloogInput
*input
, int dim
,
251 CloogOptions
*options
)
255 rc
= xmlTextWriterStartElement(writer
, BAD_CAST
"ast");
258 options
->override
= 1;
264 struct clast_stmt
*stmt
= cloog_clast_create_from_input(input
, options
);
265 cloog_util_simple_guards(stmt
);
266 stmt
= cloog_util_hoist_ifs(stmt
);
267 writeStmt(writer
, stmt
);
268 cloog_clast_free(stmt
);
270 rc
= xmlTextWriterEndElement(writer
);