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
|
/*****************************************************************************
Copyright (C) 1994-2000 the Omega Project Team
Copyright (C) 2005-2011 Chun Chen
All Rights Reserved.
Purpose:
abstract base class of compiler IR code builder
Notes:
All "CG_outputRepr *" parameters are consumed inside the the function
unless explicitly stated otherwise, i.e., not valid after the call.
Parameter "indent" normally not used except it is used in unstructured
string output for correct indentation.
History:
04/17/96 created - Lei Zhou
05/02/08 clarify integer floor/mod/ceil definitions, -chen
05/31/08 use virtual clone to implement CreateCopy, -chun
08/05/10 clarify NULL parameter allowance, -chun
*****************************************************************************/
#ifndef _CG_OUTPUTBUILDER_H
#define _CG_OUTPUTBUILDER_H
#include <code_gen/CG_outputRepr.h>
#include <string>
#include <vector>
namespace omega {
//! abstract base class of compiler IR code builder
class CG_outputBuilder {
private:
public:
CG_outputBuilder() {}
virtual ~CG_outputBuilder() {}
virtual CG_outputRepr *CreateStruct(const std::string class_name,
std::vector<std::string> class_data_members, // why not just vector< CG_outputRepr> subparts;
std::vector<CG_outputRepr *> class_data_types)=0;
virtual CG_outputRepr *CreateClassInstance(std::string name, CG_outputRepr *class_def)=0;
virtual CG_outputRepr *lookup_member_data(CG_outputRepr *scope, std::string varName, CG_outputRepr *instance)=0;
virtual CG_outputRepr *CreatePointer(std::string &name) const = 0;
virtual CG_outputRepr *ObtainInspectorRange(const std::string &_s, const std::string &_name) const =0;
/*!
* \brief substitute variable in stmt
*
* @param indent
* @param stmt
* @param vars
* @param subs
* @param actuallyPrint
* @return
*/
virtual CG_outputRepr *CreateSubstitutedStmt(int indent,
CG_outputRepr *stmt,
const std::vector<std::string> &vars,
std::vector<CG_outputRepr *> &subs,
bool actuallyPrint = true) const = 0;
/*!
* @brief assignment stmt generation
* @param indent
* @param lhs
* @param rhs
* @return
*/
virtual CG_outputRepr *CreateAssignment(int indent,
CG_outputRepr *lhs,
CG_outputRepr *rhs) const = 0;
/*!
* @brief Plus assignment stmt generation
* @param indent
* @param lhs
* @param rhs
* @return CG_outputRepr
*/
virtual CG_outputRepr *CreatePlusAssignment(int indent,
CG_outputRepr *lhs,
CG_outputRepr *rhs) const = 0;
/*!
* @brief function invoation generation
* @param funcName
* @param argList
* @param is_array
* @return
*/
virtual CG_outputRepr *CreateInvoke(const std::string &funcName,
std::vector<CG_outputRepr *> &argList,
bool is_array = false) const = 0;
/*!
* @brief comment generation
* @param indent
* @param commentText
* @return
*/
virtual CG_outputRepr *CreateComment(int indent,
const std::string &commentText) const = 0;
/*!
* @brief Attribute Generation
* @param control
* @param commentText
* @return
*/
virtual CG_outputRepr *CreateAttribute(CG_outputRepr *control,
const std::string &commentText) const = 0;
/*!
* @brief pragma attribute
* @param scopeStmt
* @param looplevel
* @param pragmaText
* @return
*/
virtual CG_outputRepr *
CreatePragmaAttribute(CG_outputRepr *scopeStmt, int looplevel, const std::string &pragmaText) const = 0;
/*!
* @brief Prefetch attribute
* @param scopeStmt
* @param looplevel
* @param arrName
* @param hint
* @return
*/
virtual CG_outputRepr *
CreatePrefetchAttribute(CG_outputRepr *scopeStmt, int looplevel, const std::string &arrName, int hint) const = 0;
/*!
* @brief generate if stmt, true/false stmt can be NULL but not the condition
* @param indent
* @param guardCondition
* @param true_stmtList
* @param false_stmtList
* @return
*/
virtual CG_outputRepr *CreateIf(int indent, CG_outputRepr *guardCondition,
CG_outputRepr *true_stmtList,
CG_outputRepr *false_stmtList) const = 0;
/*!
* @brief generate loop inductive variable (loop control structure)
* @param index
* @param lower
* @param upper
* @param step
* @return
*/
virtual CG_outputRepr *CreateInductive(CG_outputRepr *index,
CG_outputRepr *lower,
CG_outputRepr *upper,
CG_outputRepr *step) const = 0;
/*!
* @brief generate loop stmt from loop control and loop body, NULL parameter allowed
* @param indent
* @param control
* @param stmtList
* @return
*/
virtual CG_outputRepr *CreateLoop(int indent, CG_outputRepr *control,
CG_outputRepr *stmtList) const = 0;
/*!
* @brief copy operation, NULL parameter allowed
*
* this function makes pointer handling uniform regardless NULL status
*
* @param original
* @return
*/
virtual CG_outputRepr *CreateCopy(CG_outputRepr *original) const {
if (original == NULL)
return NULL;
else
return original->clone();
}
//! basic interger number creation
virtual CG_outputRepr *CreateInt(int num) const = 0;
//! basic single precision float number creation
virtual CG_outputRepr *CreateFloat(float num) const = 0;
//! basic double precision float number creation
virtual CG_outputRepr *CreateDouble(double num) const = 0;
/*!
* @brief check if op is integer literal
* @param op
* @return
*/
virtual bool isInteger(CG_outputRepr *op) const = 0;
/*!
* @brief DOC needed
* @param varName
* @return
*/
virtual bool QueryInspectorType(const std::string &varName) const = 0;
/*!
* @brief basic identity/variable creation
* @param varName
* @return
*/
virtual CG_outputRepr *CreateIdent(const std::string &varName) const = 0;
/*!
* @brief member access creation?
* @param lop
* @param rop
* @return
*/
virtual CG_outputRepr *CreateDotExpression(CG_outputRepr *lop,
CG_outputRepr *rop) const =0;
/*!
* @brief Create array reference expression starting with a string
* @param _s
* @param rop
* @return
*/
virtual CG_outputRepr *CreateArrayRefExpression(const std::string &_s,
CG_outputRepr *rop) const =0;
/*!
* @brief Create array reference expression starting with a expresion
* @param lop
* @param rop
* @return
*/
virtual CG_outputRepr *CreateArrayRefExpression(CG_outputRepr *lop,
CG_outputRepr *rop) const =0;
/*!
* @brief TODO DOC NEEDED
* @param _s
* @param member_name
* @return
*/
virtual CG_outputRepr *ObtainInspectorData(const std::string &_s, const std::string &member_name) const =0;
/*!
* @brief TODO USAGE NEEDED
* @return
*/
virtual CG_outputRepr *CreateNullStatement() const =0;
/*!
* @brief Addition operations, NULL parameter means 0
* @param lop
* @param rop
* @return
*/
virtual CG_outputRepr *CreatePlus(CG_outputRepr *lop, CG_outputRepr *rop) const = 0;
/*!
* @brief Subtraction operations, NULL parameter means 0
* @param lop
* @param rop
* @return
*/
virtual CG_outputRepr *CreateMinus(CG_outputRepr *lop, CG_outputRepr *rop) const = 0;
/*!
* @brief Multiplication operations, NULL parameter means 0
* @param lop
* @param rop
* @return
*/
virtual CG_outputRepr *CreateTimes(CG_outputRepr *lop, CG_outputRepr *rop) const = 0;
/*!
* @brief Division operations, NULL parameter means 0
*
* integer division truncation method undefined, only use when lop is known
* to be multiple of rop, otherwise use integer floor instead
*
* @param lop
* @param rop
* @return
*/
virtual CG_outputRepr *CreateDivide(CG_outputRepr *lop, CG_outputRepr *rop) const {
return CreateIntegerFloor(lop, rop);
}
/*!
* @brief integer floor functions, NULL parameter means 0
*
* second parameter must be postive (i.e. b > 0 below), otherwise function undefined
*
* floor(a, b)
* * = a/b if a >= 0
* * = (a-b+1)/b if a < 0
*
* @param lop
* @param rop
* @return
*/
virtual CG_outputRepr *CreateIntegerFloor(CG_outputRepr *lop, CG_outputRepr *rop) const = 0;
/*!
* @brief integer mod functions, NULL parameter means 0
*
* second parameter must be postive (i.e. b > 0 below), otherwise function undefined
*
* mod(a, b) = a-b*floor(a, b) where result must lie in range [0,b)
*
* @param lop
* @param rop
* @return
*/
virtual CG_outputRepr *CreateIntegerMod(CG_outputRepr *lop, CG_outputRepr *rop) const {
CG_outputRepr *lop2 = CreateCopy(lop);
CG_outputRepr *rop2 = CreateCopy(rop);
return CreateMinus(lop2, CreateTimes(rop2, CreateIntegerFloor(lop, rop)));
}
/*!
* @brief integer ceil functions, NULL parameter means 0
*
* second parameter must be postive (i.e. b > 0 below), otherwise function undefined
*
* ceil(a, b) = -floor(-a, b) or floor(a+b-1, b) or floor(a-1, b)+1
*
* @param lop
* @param rop
* @return
*/
virtual CG_outputRepr *CreateIntegerCeil(CG_outputRepr *lop, CG_outputRepr *rop) const {
return CreateMinus(NULL, CreateIntegerFloor(CreateMinus(NULL, lop), rop));
}
//! binary logical operation, NULL parameter means TRUE
virtual CG_outputRepr *CreateAnd(CG_outputRepr *lop, CG_outputRepr *rop) const = 0;
//! binary conditional greater than or equal to
virtual CG_outputRepr *CreateGE(CG_outputRepr *lop, CG_outputRepr *rop) const {
return CreateLE(rop, lop);
}
//! binary conditional less than or equal to
virtual CG_outputRepr *CreateLE(CG_outputRepr *lop, CG_outputRepr *rop) const = 0;
//! binary conditional equal to
virtual CG_outputRepr *CreateEQ(CG_outputRepr *lop, CG_outputRepr *rop) const = 0;
//! binary conditional not equal to
virtual CG_outputRepr *CreateNEQ(CG_outputRepr *lop, CG_outputRepr *rop) const = 0;
//! address of operator
virtual CG_outputRepr *CreateAddressOf(CG_outputRepr *op) const = 0;
//! create a break statement
virtual CG_outputRepr *CreateBreakStatement(void) const = 0;
//! join stmts together, NULL parameter allowed
virtual CG_outputRepr *StmtListAppend(CG_outputRepr *list1, CG_outputRepr *list2) const = 0;
//! Wraps a expression into a statement. TODO DOC NEEDED
virtual CG_outputRepr *CreateStatementFromExpression(CG_outputRepr *exp) const = 0;
//! TODO USAGE AND DOC
virtual const char *ClassName() { return "UNKNOWN"; }
};
}
#endif
|