da.cc: need_parametrization: check if potential sources can have executed
[ppn.git] / clast_scanner.cc
blob5518c80c65d20d133f6cc2877007a92d5a4f303a
1 #include <assert.h>
2 #include <stdlib.h>
3 #include "clast_scanner.h"
5 void clast_scanner::print(clast_name *n)
7 fprintf(dst, "%s", n->name);
10 void clast_scanner::print(clast_term *t)
12 cloog_int_print(dst, t->val);
13 if (t->var) {
14 fprintf(dst, "*");
15 if (t->var->type == clast_expr_red)
16 fprintf(dst, "(");
17 print(t->var);
18 if (t->var->type == clast_expr_red)
19 fprintf(dst, ")");
23 void clast_scanner::print(clast_binary *b)
25 const char *s1, *s2;
26 switch (b->type) {
27 case clast_bin_mod:
28 s1 = "MOD(", s2 = ", ";
29 break;
30 case clast_bin_div:
31 s1 = "(", s2 = ")/(";
32 break;
33 case clast_bin_cdiv:
34 s1 = "DIVCEIL(", s2 = ", ";
35 break;
36 case clast_bin_fdiv:
37 s1 = "DIVFLOOR(", s2 = ", ";
38 break;
39 default:
40 assert(0);
42 fprintf(dst, s1);
43 print(b->LHS);
44 fprintf(dst, s2);
45 cloog_int_print(dst, b->RHS);
46 fprintf(dst, ")");
49 void clast_scanner::print(clast_reduction *r)
51 if (r->n == 1) {
52 print(r->elts[0]);
53 return;
56 const char *s1, *s2, *s3;
58 switch (r->type) {
59 case clast_red_sum:
60 s1 = "", s2 = " + ", s3 = "";
61 break;
62 case clast_red_max:
63 s1 = "MAX(", s2 = ", ", s3 = ")";
64 break;
65 case clast_red_min:
66 s1 = "MIN(", s2 = ", ", s3 = ")";
67 break;
68 default:
69 assert(0);
72 for (int i = 1; i < r->n; ++i)
73 fprintf(dst, s1);
74 print(r->elts[0]);
75 for (int i = 1; i < r->n; ++i) {
76 fprintf(dst, s2);
77 print(r->elts[i]);
78 fprintf(dst, s3);
82 void clast_scanner::print(clast_expr *e)
84 switch (e->type) {
85 case clast_expr_name:
86 print((clast_name*) e);
87 break;
88 case clast_expr_term:
89 print((clast_term*) e);
90 break;
91 case clast_expr_red:
92 print((clast_reduction*) e);
93 break;
94 case clast_expr_bin:
95 print((clast_binary*) e);
96 break;
97 default:
98 assert(0);
102 void clast_scanner::print(clast_assignment *a)
104 print_indent();
105 fprintf(dst, "%s = ", a->LHS);
106 print(a->RHS);
107 fprintf(dst, ";\n");
110 void clast_scanner::print(clast_guard *g)
112 int n = g->n;
113 print_indent();
114 fprintf(dst, "if (");
115 for (int i = 0; i < n; ++i) {
116 if (i > 0)
117 fprintf(dst," && ");
118 fprintf(dst,"(");
119 print(g->eq[i].LHS);
120 if (g->eq[i].sign == 0)
121 fprintf(dst," == ");
122 else if (g->eq[i].sign > 0)
123 fprintf(dst," >= ");
124 else
125 fprintf(dst," <= ");
126 print(g->eq[i].RHS);
127 fprintf(dst,")");
129 fprintf(dst, ") {\n");
130 indent += 4;
131 print(g->then);
132 indent -= 4;
133 print_indent();
134 fprintf(dst, "}\n");
137 void clast_scanner::print(clast_for *f)
139 assert(f->LB && f->UB);
140 print_indent();
141 fprintf(dst, "for (%s=", f->iterator);
142 print(f->LB);
143 fprintf(dst, "; %s<=", f->iterator);
144 print(f->UB);
145 fprintf(dst, "; ++%s) {\n", f->iterator);
146 indent += 4;
147 print(f->body);
148 indent -= 4;
149 print_indent();
150 fprintf(dst, "}\n");
153 void clast_scanner::print(clast_stmt *s)
155 for ( ; s; s = s->next) {
156 if (CLAST_STMT_IS_A(s, stmt_root))
157 continue;
158 if (CLAST_STMT_IS_A(s, stmt_ass)) {
159 print((clast_assignment *) s);
160 } else if (CLAST_STMT_IS_A(s, stmt_user)) {
161 print_user((clast_user_stmt *) s);
162 } else if (CLAST_STMT_IS_A(s, stmt_for)) {
163 print((clast_for *) s);
164 } else if (CLAST_STMT_IS_A(s, stmt_guard)) {
165 print((clast_guard *) s);
166 } else {
167 assert(0);
172 void clast_scanner::print_header()
174 fprintf(dst, "%s", "#include <assert.h>\n");
175 fprintf(dst, "%s", "#include <stdio.h>\n");
176 fprintf(dst, "%s", "#define MIN(x,y)\t" "((x)<(y)?(x):(y))" "\n");
177 fprintf(dst, "%s", "#define MAX(x,y)\t" "((x)>(y)?(x):(y))" "\n");
178 fprintf(dst, "%s", "#define MOD(x,y)\t" "((((x) % (y)) < 0) ? "
179 "(((y) < 0) ? (((x) % (y)) - (y)) : (((x) % (y)) + (y))) : ((x) % (y)))"
180 "\n");
181 fprintf(dst, "%s", "#define DIVCEIL(x,y)\t" "(((x)<0)^((y)<0) ? (x)/(y) : "
182 "((x) < 0 ? ((x)+(y)+1)/(y) : ((x)+(y)-1)/(y)))" "\n");
183 fprintf(dst, "%s", "#define DIVFLOOR(x,y)\t"
184 "(((x)<0)^((y)<0) ? ((x) < 0 ? ((x)-(y)+1)/(y) : ((x)-(y)-1)/(y)) : "
185 "(x)/(y))" "\n");
188 void clast_scanner::print_iterators(CloogNames* names)
190 print_indent();
191 if (names->nb_iterators <= 0)
192 return;
193 fprintf(dst, "int ");
194 for (int i = 0; i < names->nb_iterators; ++i) {
195 if (i)
196 fprintf(dst, ", ");
197 fprintf(dst, names->iterators[i]);
199 fprintf(dst, ";\n");