da.cc: fix typos in comments
[ppn.git] / xml_AST.cc
blob0402187976c2695603ef91f36a555de70aa10d2c
1 #include <sstream>
2 #include <isl/int.h>
3 #include "xml_AST.h"
4 #include "cloog_util.h"
6 using namespace std;
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)
15 if (!e)
16 return os;
17 switch (e->type) {
18 case clast_expr_name:
19 os << ((clast_name*) e);
20 break;
21 case clast_expr_term:
22 os << ((clast_term*) e);
23 break;
24 case clast_expr_red:
25 os << ((clast_reduction*) e);
26 break;
27 case clast_expr_bin:
28 os << ((clast_binary*) e);
29 break;
30 default:
31 assert(0);
33 return os;
36 static ostream& operator<< (ostream& os, clast_name *n)
38 os << n->name;
39 return os;
42 static ostream& operator<< (ostream& os, clast_term *t)
44 os << t->val;
45 if (t->var) {
46 os << "*";
47 if (t->var->type == clast_expr_red)
48 os << "(";
49 os << t->var;
50 if (t->var->type == clast_expr_red)
51 os << ")";
53 return os;
56 static ostream& operator<< (ostream& os, clast_reduction *r)
58 if (r->n == 1) {
59 os << (r->elts[0]);
60 return os;
63 assert(r->type == clast_red_sum);
64 os << (r->elts[0]);
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))
69 os << "+";
70 os << (r->elts[i]);
72 return os;
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)
84 if (r->n == 1) {
85 clast_negate(r->elts[0]);
86 return;
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)
96 switch (e->type) {
97 case clast_expr_term:
98 clast_negate((clast_term *) e);
99 break;
100 case clast_expr_red:
101 clast_negate((clast_reduction *) e);
102 break;
103 default:
104 assert(0);
108 static ostream& operator<< (ostream& os, clast_binary *b)
110 switch (b->type) {
111 case clast_bin_mod:
112 os << "mod(" << b->LHS << "," << b->RHS << ")";
113 return os;
114 case clast_bin_div:
115 os << "(" << b->LHS << ")/(" << b->RHS << ")";
116 return os;
117 case clast_bin_fdiv:
118 os << "div(" << b->LHS << "," << b->RHS << ")";
119 return os;
120 case clast_bin_cdiv:
121 clast_negate(b->LHS);
122 os << "-div(" << b->LHS << "," << b->RHS << ")";
123 clast_negate(b->LHS);
124 return os;
125 default:
126 assert(0);
130 static string expr2string(clast_expr *e)
132 std::ostringstream strm;
134 strm << e;
136 return strm.str();
139 static string int2string(isl_int i)
141 std::ostringstream strm;
143 strm << i;
145 return strm.str();
148 static void writeStmt(xmlTextWriterPtr writer, clast_stmt *s);
150 static void writeStmt(xmlTextWriterPtr writer, clast_for *f)
152 int rc;
154 rc = xmlTextWriterStartElement(writer, BAD_CAST "for");
155 assert(rc >= 0);
157 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "iterator",
158 BAD_CAST f->iterator);
159 assert(rc >= 0);
161 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "LB",
162 BAD_CAST expr2string(f->LB).c_str());
163 assert(rc >= 0);
165 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "UB",
166 BAD_CAST expr2string(f->UB).c_str());
167 assert(rc >= 0);
169 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "stride",
170 BAD_CAST int2string(f->stride).c_str());
171 assert(rc >= 0);
173 writeStmt(writer, f->body);
175 rc = xmlTextWriterEndElement(writer);
176 assert(rc >= 0);
179 static void writeStmt(xmlTextWriterPtr writer, clast_guard *g)
181 int rc;
182 char buf[10];
184 assert(g->n == 1);
186 rc = xmlTextWriterStartElement(writer, BAD_CAST "if");
187 assert(rc >= 0);
189 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "LHS",
190 BAD_CAST expr2string(g->eq[0].LHS).c_str());
191 assert(rc >= 0);
193 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "RHS",
194 BAD_CAST expr2string(g->eq[0].RHS).c_str());
195 assert(rc >= 0);
197 snprintf(buf, 10, "%d", g->eq[0].sign);
198 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "sign", BAD_CAST buf);
199 assert(rc >= 0);
201 writeStmt(writer, g->then);
203 rc = xmlTextWriterEndElement(writer);
204 assert(rc >= 0);
207 static void writeStmt(xmlTextWriterPtr writer, clast_user_stmt *u)
209 int rc;
210 char *name = u->statement->name;
211 char *I;
213 if ((I = strchr(name, 'I')) && I[1] == 'D') {
214 rc = xmlTextWriterStartElement(writer, BAD_CAST "var");
215 assert(rc >= 0);
217 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "name", BAD_CAST name);
218 assert(rc >= 0);
219 } else if (!strchr(name, 'P')) {
220 rc = xmlTextWriterStartElement(writer, BAD_CAST "stmt");
221 assert(rc >= 0);
223 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "node", BAD_CAST name);
224 assert(rc >= 0);
225 } else {
226 rc = xmlTextWriterStartElement(writer, BAD_CAST "port");
227 assert(rc >= 0);
229 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "name", BAD_CAST name);
230 assert(rc >= 0);
233 rc = xmlTextWriterEndElement(writer);
234 assert(rc >= 0);
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)
253 int rc;
255 rc = xmlTextWriterStartElement(writer, BAD_CAST "ast");
256 assert(rc >= 0);
258 options->override = 1;
259 options->f = -1;
260 options->l = dim;
261 options->esp = 1;
262 options->sh = 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);
271 assert(rc >= 0);