-
Notifications
You must be signed in to change notification settings - Fork 6
/
ir_chill.hh
executable file
·419 lines (317 loc) · 12.7 KB
/
ir_chill.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#ifndef IR_CHILL_HH
#define IR_CHILL_HH
// INTERMEDIATE REPRESENTATION WITH CHILLAST INTERNALS
#include <omega.h>
#include "ir_code.hh"
#include "chill_error.hh"
#include "chill_ast.hh"
#include "chill_io.hh"
void findmanually( chillAST_node *node, char *procname, vector<chillAST_node*>& procs );
extern vector<chillAST_VarDecl *> VariableDeclarations; // a global. TODO
struct IR_chillScalarSymbol: public IR_ScalarSymbol {
chillAST_VarDecl *chillvd;
IR_chillScalarSymbol(const IR_Code *ir, chillAST_VarDecl *vd) {
debug_fprintf(stderr, "making CHILL scalar symbol %s\n", vd->varname);
ir_ = ir;
chillvd = vd;
}
std::string name() const;
int size() const;
bool operator==(const IR_Symbol &that) const;
IR_Symbol *clone() const;
};
struct IR_chillArraySymbol: public IR_ArraySymbol {
//int indirect_; // what was this?
int offset_; // what is this?
chillAST_VarDecl *chillvd;
IR_chillArraySymbol(const IR_Code *ir, chillAST_VarDecl *vd, int offset = 0) {
//if ( vd == 0 )
//debug_fprintf(stderr, "IR_chillArraySymbol::IR_chillArraySymbol (%s) vd 0x%x\n", vd->varname, vd);
ir_ = ir;
chillvd = vd;
//indirect_ = indirect;
offset_ = offset;
}
// No Fortran support!
IR_ARRAY_LAYOUT_TYPE layout_type() const {
return IR_ARRAY_LAYOUT_ROW_MAJOR;
}
std::string name() const;
int elem_size() const;
IR_CONSTANT_TYPE elem_type() const;
int n_dim() const;
omega::CG_outputRepr *size(int dim) const;
bool operator!=(const IR_Symbol &that) const;
bool operator==(const IR_Symbol &that) const;
IR_Symbol *clone() const;
};
struct IR_chillConstantRef: public IR_ConstantRef {
union {
omega::coef_t i_;
double f_;
};
IR_chillConstantRef(const IR_Code *ir, omega::coef_t i) {
ir_ = ir;
type_ = IR_CONSTANT_INT;
i_ = i;
}
IR_chillConstantRef(const IR_Code *ir, double f) {
ir_ = ir;
type_ = IR_CONSTANT_FLOAT;
f_ = f;
}
omega::coef_t integer() const {
assert(is_integer());
return i_;
}
bool operator==(const IR_Ref &that) const;
omega::CG_outputRepr *convert();
IR_Ref *clone() const;
};
enum OP_POSITION { OP_DEST =-1, OP_UNKNOWN, OP_SRC };
#define OP_LEFT OP_DEST
#define OP_RIGHT OP_SRC
struct IR_chillScalarRef: public IR_ScalarRef {
OP_POSITION op_pos_; // -1 means destination operand, 0== unknown, 1 == source operand
//chillAST_BinaryOperator *bop; // binary op that contains this scalar?
chillAST_DeclRefExpr *dre; // declrefexpr that uses this scalar ref, if that exists
chillAST_VarDecl *chillvd; // the vardecl for this scalar
IR_chillScalarRef(const IR_Code *ir, chillAST_BinaryOperator *ins, OP_POSITION pos) {
debug_fprintf(stderr, "\n***** new IR_xxxxScalarRef( ir, ins, pos ) *****\n\n");
exit(-1);
// this constructor takes a binary operation and an indicator of which side of the op to use,
// and finds the scalar in the lhs or rhs of the binary op.
ir_ = ir;
dre = NULL;
//bop = ins; // do we need this?
if (pos == OP_LEFT) {
chillAST_node *lhs = ins->lhs;
if (lhs->isDeclRefExpr()) {
chillAST_DeclRefExpr *DRE = (chillAST_DeclRefExpr *) lhs;
dre = DRE;
chillvd = DRE->getVarDecl();
}
else if (lhs->isVarDecl()) {
chillvd = (chillAST_VarDecl *)lhs;
}
else {
debug_fprintf(stderr, "IR_chillScalarRef constructor, I'm confused\n"); exit(-1);
}
}
else {
chillAST_node *rhs = ins->rhs;
if (rhs->isDeclRefExpr()) {
chillAST_DeclRefExpr *DRE = (chillAST_DeclRefExpr *) rhs;
dre = DRE;
chillvd = DRE->getVarDecl();
}
else if (rhs->isVarDecl()) {
chillvd = (chillAST_VarDecl *)rhs;
}
else {
debug_fprintf(stderr, "IR_chillScalarRef constructor, I'm confused\n"); exit(-1);
}
}
op_pos_ = pos;
}
IR_chillScalarRef(const IR_Code *ir, chillAST_DeclRefExpr *d) {
// debug_fprintf(stderr, "\n***** new IR_xxxxScalarRef( ir, REF EXPR sym %s ) *****\n\n", d->getVarDecl()->varname);
//debug_fprintf(stderr, "new IR_chillScalarRef with a DECLREFEXPR (has dre) \n");
ir_ = ir;
dre = d;
//bop = NULL;
chillvd = d->getVarDecl();
op_pos_ = OP_UNKNOWN;
//debug_fprintf(stderr, "\nScalarRef has:\n");
//debug_fprintf(stderr, "assignment op DOESNT EXIST\n");
//debug_fprintf(stderr, "ins_pos %d\n", ins_pos_);
//debug_fprintf(stderr, "op_pos %d\n", op_pos_);
//debug_fprintf(stderr, "ref expr dre = 0x%x\n", dre);
}
IR_chillScalarRef(const IR_Code *ir, chillAST_VarDecl *vardecl) {
debug_fprintf(stderr, "\n***** new IR_xxxxScalarRef( ir, sym 0x1234567 ) ***** THIS SHOULD NEVER HAPPEN\n\n");
debug_fprintf(stderr, "vardecl %s\n", vardecl->varname);
ir_ = ir;
dre = NULL; debug_fprintf(stderr, "new IR_chillScalarRef with a vardecl but no dre\n");
//bop = NULL;
chillvd = vardecl;
op_pos_ = OP_UNKNOWN;
//debug_fprintf(stderr, "\nScalarRef has:\n");
//debug_fprintf(stderr, "assignment op DOESNT EXIST\n");
//debug_fprintf(stderr, "ins_pos %d\n", ins_pos_);
//debug_fprintf(stderr, "op_pos %d\n", op_pos_);
//debug_fprintf(stderr, "ref expr dre = 0x%x\n", dre);
}
bool is_write() const;
IR_ScalarSymbol *symbol() const;
bool operator==(const IR_Ref &that) const;
omega::CG_outputRepr *convert();
IR_Ref *clone() const;
};
struct IR_chillArrayRef: public IR_ArrayRef {
//DeclRefExpr *as_;
//chillAST_DeclRefExpr *chillDRE;
chillAST_ArraySubscriptExpr* chillASE;
char *printable;
int iswrite;
IR_chillArrayRef(const IR_Code *ir, chillAST_ArraySubscriptExpr *ase, int write ) {
debug_fprintf(stderr, "IR_chillArrayRef::IR_chillArrayRef() write %d\n", write);
ir_ = ir;
chillASE = ase;
iswrite = write; // ase->imwrittento;
printable = NULL;
}
IR_chillArrayRef(const IR_Code *ir, chillAST_ArraySubscriptExpr *ase, const char *printname, int write ) {
//debug_fprintf(stderr, "IR_chillArrayRef::IR_chillArrayRef() write %d\n", write);
ir_ = ir;
chillASE = ase;
iswrite = write; // ase->imwrittento;
printable = NULL;
if (printname) printable = strdup(printname);
}
std::string name() const; // overriding virtual one in ir_code.hh
bool is_write() const;
omega::CG_outputRepr *index(int dim) const;
IR_ArraySymbol *symbol() const;
bool operator!=(const IR_Ref &that) const;
bool operator==(const IR_Ref &that) const;
omega::CG_outputRepr *convert();
IR_Ref *clone() const;
virtual void Dump() const;
};
struct IR_chillLoop: public IR_Loop {
int step_size_;
chillAST_DeclRefExpr *chillindex; // the loop index variable (I) // was DeclRefExpr
chillAST_ForStmt *chillforstmt;
chillAST_node *chilllowerbound;
chillAST_node *chillupperbound;
chillAST_node *chillbody; // presumably a compound statement, but not guaranteeed
IR_CONDITION_TYPE conditionoperator;
IR_chillLoop(const IR_Code *ir, chillAST_ForStmt *forstmt);
~IR_chillLoop() {}
IR_ScalarSymbol *index() const { return new IR_chillScalarSymbol(ir_, chillindex->getVarDecl()); }
omega::CG_outputRepr *lower_bound() const;
omega::CG_outputRepr *upper_bound() const;
IR_CONDITION_TYPE stop_cond() const;
IR_Block *body() const;
// Handle following types of increment expressions:
// Unary increment/decrement
// i += K OR i -= K
// i = i + K OR i = i - K
// where K is positive
int step_size() const { return step_size_; } // K is always an integer ???
IR_Control *clone() const;
IR_Block *convert() ;
virtual void dump() const;
};
struct IR_chillBlock: public IR_Block { // ONLY ONE OF bDecl or cs_ will be nonNULL ??
private:
chillAST_node *chillAST; // how about for now we say if there are statements, which is presumably the top level of statements from ... somewhere, otherwise the code is in chillAST
public:
vector<chillAST_node *>statements;
// Block is a basic block?? (no, just a chunk of code )
IR_chillBlock() {
ir_ = NULL;
chillAST = NULL;
}
IR_chillBlock(const IR_Code *ir, chillAST_node *ast) {
ir_ = ir;
chillAST = ast;
}
IR_chillBlock(const IR_Code *ir) { // : cs_(NULL), bDecl_(NULL) {
chillAST = NULL;
ir_ = ir;
}
IR_chillBlock( const IR_chillBlock *CB ) { // clone existing IR_chillBlock
ir_ = CB->ir_;
for (int i=0; i<CB->statements.size(); i++) statements.push_back( CB->statements[i] );
chillAST = CB->chillAST;
}
~IR_chillBlock() {} // leaves AST and statements intact
omega::CG_outputRepr *extract() const;
omega::CG_outputRepr *original() const;
IR_Control *clone() const;
vector<chillAST_node*> getStmtList() const;
int numstatements() { return statements.size(); } ;
void addStatement( chillAST_node* s );
void dump() const;
virtual chillAST_node *getChillAST() const {
//debug_fprintf(stderr, "IR_chillBlock::getChillAST(), %d statements, chillAST %p\n", statements.size(), chillAST );
debug_fprintf(stderr, "IR_chillBlock::getChillAST(), %d statements\n", statements.size() );
return chillAST;
}
virtual void setChillAST( chillAST_node *n) { chillAST = n; };
};
struct IR_chillIf: public IR_If {
chillAST_IfStmt *chillif;
IR_chillIf(const IR_Code *ir, chillAST_node *i) {
debug_fprintf(stderr, "IR_chillIf::IR_chillIf( ir, chillast_node )\n");
ir_ = ir;
if (!i->isIfStmt()) {
debug_fprintf(stderr, "IR_chillIf::IR_chillIf( ir, chillast_node ) node is not an ifstmt\n");
debug_fprintf(stderr, "it is a %s\n", i->getTypeString());
i->print(0, stderr); debug_fprintf(stderr, "\n\n");
}
chillif = (chillAST_IfStmt *)i;
}
IR_chillIf(const IR_Code *ir, chillAST_IfStmt *i) {
debug_fprintf(stderr, "IR_chillIf::IR_chillIf( ir, chillast_IfStmt )\n");
ir_ = ir;
if (!i->isIfStmt()) {
debug_fprintf(stderr, "IR_chillIf::IR_chillIf( ir, chillast_IfStmt ) node is not an ifstmt\n");
debug_fprintf(stderr, "it is a %s\n", i->getTypeString());
i->print(0, stderr); debug_fprintf(stderr, "\n\n");
}
chillif = i;
}
~IR_chillIf() { // leave ast alone
}
omega::CG_outputRepr *condition() const;
IR_Block *then_body() const;
IR_Block *else_body() const;
IR_Block *convert();
IR_Control *clone() const;
};
class IR_chillCode: public IR_Code{ // for an entire file? A single function?
protected:
//
char *filename;
char *outputname; // so we can output different results from one source, using different scripts
chillAST_node *entire_file_AST;
chillAST_FunctionDecl * chillfunc; // the function we're currenly modifying
std::vector<chillAST_VarDecl> entire_file_symbol_table;
// loop symbol table?? for (int i=0; ... ) ??
public:
char *procedurename;
IR_chillCode();
IR_chillCode(const char *filename, char *proc_name);
IR_chillCode(const char *filename, char *proc_name, char *script_name);
~IR_chillCode();
void setOutputName( const char *name ) { outputname = strdup(name); }
IR_ScalarSymbol *CreateScalarSymbol(const IR_Symbol *sym, int i);
IR_ArraySymbol *CreateArraySymbol(const IR_Symbol *sym, std::vector<omega::CG_outputRepr *> &size, int i);
IR_ScalarRef *CreateScalarRef(const IR_ScalarSymbol *sym);
IR_ArrayRef *CreateArrayRef(const IR_ArraySymbol *sym, std::vector<omega::CG_outputRepr *> &index);
omega::CG_outputRepr* CreateArrayRefRepr(const IR_ArraySymbol *sym,
std::vector<omega::CG_outputRepr *> &index) {
debug_fprintf(stderr, "IR_chillCode::CreateArrayRefRepr() not implemented\n");
exit(-1);
return NULL;
}
int ArrayIndexStartAt() { return 0;} // TODO FORTRAN
std::vector<IR_ScalarRef *> FindScalarRef(const omega::CG_outputRepr *repr) const;
std::vector<IR_ArrayRef *> FindArrayRef(const omega::CG_outputRepr *repr) const;
std::vector<IR_PointerArrayRef *> FindPointerArrayRef(const omega::CG_outputRepr *repr) const;
std::vector<IR_Control *> FindOneLevelControlStructure(const IR_Block *block) const;
IR_Block *MergeNeighboringControlStructures(const std::vector<IR_Control *> &controls) const;
IR_Block *GetCode() const;
void ReplaceCode(IR_Control *old, omega::CG_outputRepr *repr);
void ReplaceExpression(IR_Ref *old, omega::CG_outputRepr *repr);
IR_CONDITION_TYPE QueryBooleanExpOperation(const omega::CG_outputRepr*) const;
IR_OPERATION_TYPE QueryExpOperation(const omega::CG_outputRepr *repr) const;
std::vector<omega::CG_outputRepr *> QueryExpOperand(const omega::CG_outputRepr *repr) const;
IR_Ref *Repr2Ref(const omega::CG_outputRepr *) const;
friend class IR_chillArraySymbol;
friend class IR_chillArrayRef;
};
#endif