zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
intermediate.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 //
8 // Definition of the in-memory high-level intermediate representation
9 // of shaders. This is a tree that parser creates.
10 //
11 // Nodes in the tree are defined as a hierarchy of classes derived from
12 // TIntermNode. Each is a node in a tree. There is no preset branching factor;
13 // each node can have it's own type of list of children.
14 //
15 
16 #ifndef __INTERMEDIATE_H
17 #define __INTERMEDIATE_H
18 
19 #include "GLSLANG/ShaderLang.h"
20 
21 #include <algorithm>
22 #include "compiler/Common.h"
23 #include "compiler/Types.h"
24 #include "compiler/ConstantUnion.h"
25 
26 //
27 // Operators used by the high-level (parse tree) representation.
28 //
29 enum TOperator {
30  EOpNull, // if in a node, should only mean a node is still being built
31  EOpSequence, // denotes a list of statements, or parameters, etc.
33  EOpFunction, // For function definition
34  EOpParameters, // an aggregate listing the parameters to a function
35 
38 
39  //
40  // Unary operators
41  //
42 
46 
51 
58 
59  //
60  // binary operations
61  //
62 
76 
81 
85 
89 
91 
92  //
93  // Built-in functions potentially mapped to operators
94  //
95 
104 
112 
125 
134 
135  EOpDFdx, // Fragment only, OES_standard_derivatives extension
136  EOpDFdy, // Fragment only, OES_standard_derivatives extension
137  EOpFwidth, // Fragment only, OES_standard_derivatives extension
138 
140 
143 
144  //
145  // Branch
146  //
147 
148  EOpKill, // Fragment only
152 
153  //
154  // Constructors
155  //
156 
173 
174  //
175  // moves
176  //
177 
188 };
189 
190 extern const char* getOperatorString(TOperator op);
191 
192 class TIntermTraverser;
193 class TIntermAggregate;
194 class TIntermBinary;
195 class TIntermUnary;
197 class TIntermSelection;
198 class TIntermTyped;
199 class TIntermSymbol;
200 class TIntermLoop;
201 class TInfoSink;
202 
203 //
204 // Base class for the tree nodes
205 //
206 class TIntermNode {
207 public:
210  // TODO: Move this to TSourceLoc constructor
211  // after getting rid of TPublicType.
214  }
215  virtual ~TIntermNode() { }
216 
217  const TSourceLoc& getLine() const { return line; }
218  void setLine(const TSourceLoc& l) { line = l; }
219 
220  virtual void traverse(TIntermTraverser*) = 0;
221  virtual TIntermTyped* getAsTyped() { return 0; }
222  virtual TIntermConstantUnion* getAsConstantUnion() { return 0; }
223  virtual TIntermAggregate* getAsAggregate() { return 0; }
224  virtual TIntermBinary* getAsBinaryNode() { return 0; }
225  virtual TIntermUnary* getAsUnaryNode() { return 0; }
226  virtual TIntermSelection* getAsSelectionNode() { return 0; }
227  virtual TIntermSymbol* getAsSymbolNode() { return 0; }
228  virtual TIntermLoop* getAsLoopNode() { return 0; }
229 
230 protected:
232 };
233 
234 //
235 // This is just to help yacc.
236 //
240 };
241 
242 //
243 // Intermediate class for nodes that have a type.
244 //
245 class TIntermTyped : public TIntermNode {
246 public:
247  TIntermTyped(const TType& t) : type(t) { }
248  virtual TIntermTyped* getAsTyped() { return this; }
249 
250  void setType(const TType& t) { type = t; }
251  const TType& getType() const { return type; }
252  TType* getTypePointer() { return &type; }
253 
254  TBasicType getBasicType() const { return type.getBasicType(); }
255  TQualifier getQualifier() const { return type.getQualifier(); }
256  TPrecision getPrecision() const { return type.getPrecision(); }
257  int getNominalSize() const { return type.getNominalSize(); }
258 
259  bool isMatrix() const { return type.isMatrix(); }
260  bool isArray() const { return type.isArray(); }
261  bool isVector() const { return type.isVector(); }
262  bool isScalar() const { return type.isScalar(); }
263  const char* getBasicString() const { return type.getBasicString(); }
264  const char* getQualifierString() const { return type.getQualifierString(); }
265  TString getCompleteString() const { return type.getCompleteString(); }
266 
267  int totalRegisterCount() const { return type.totalRegisterCount(); }
268  int elementRegisterCount() const { return type.elementRegisterCount(); }
269  int getArraySize() const { return type.getArraySize(); }
270 
271 protected:
273 };
274 
275 //
276 // Handle for, do-while, and while loops.
277 //
278 enum TLoopType {
282 };
283 
284 class TIntermLoop : public TIntermNode {
285 public:
287  TIntermNode *aInit, TIntermTyped* aCond, TIntermTyped* aExpr,
288  TIntermNode* aBody) :
289  type(aType),
290  init(aInit),
291  cond(aCond),
292  expr(aExpr),
293  body(aBody),
294  unrollFlag(false) { }
295 
296  virtual TIntermLoop* getAsLoopNode() { return this; }
297  virtual void traverse(TIntermTraverser*);
298 
299  TLoopType getType() const { return type; }
300  TIntermNode* getInit() { return init; }
303  TIntermNode* getBody() { return body; }
304 
305  void setUnrollFlag(bool flag) { unrollFlag = flag; }
306  bool getUnrollFlag() { return unrollFlag; }
307 
308 protected:
310  TIntermNode* init; // for-loop initialization
311  TIntermTyped* cond; // loop exit condition
312  TIntermTyped* expr; // for-loop expression
313  TIntermNode* body; // loop body
314 
315  bool unrollFlag; // Whether the loop should be unrolled or not.
316 };
317 
318 //
319 // Handle break, continue, return, and kill.
320 //
321 class TIntermBranch : public TIntermNode {
322 public:
324  flowOp(op),
325  expression(e) { }
326 
327  virtual void traverse(TIntermTraverser*);
328 
329  TOperator getFlowOp() { return flowOp; }
331 
332 protected:
334  TIntermTyped* expression; // non-zero except for "return exp;" statements
335 };
336 
337 //
338 // Nodes that correspond to symbols or constants in the source code.
339 //
340 class TIntermSymbol : public TIntermTyped {
341 public:
342  // if symbol is initialized as symbol(sym), the memory comes from the poolallocator of sym. If sym comes from
343  // per process globalpoolallocator, then it causes increased memory usage per compile
344  // it is essential to use "symbol = sym" to assign to symbol
345  TIntermSymbol(int i, const TString& sym, const TType& t) :
346  TIntermTyped(t), id(i) { symbol = sym; originalSymbol = sym; }
347 
348  int getId() const { return id; }
349  const TString& getSymbol() const { return symbol; }
350 
351  void setId(int newId) { id = newId; }
352  void setSymbol(const TString& sym) { symbol = sym; }
353 
354  const TString& getOriginalSymbol() const { return originalSymbol; }
355 
356  virtual void traverse(TIntermTraverser*);
357  virtual TIntermSymbol* getAsSymbolNode() { return this; }
358 
359 protected:
360  int id;
363 };
364 
366 public:
367  TIntermConstantUnion(ConstantUnion *unionPointer, const TType& t) : TIntermTyped(t), unionArrayPointer(unionPointer) { }
368 
370 
371  int getIConst(int index) const { return unionArrayPointer ? unionArrayPointer[index].getIConst() : 0; }
372  float getFConst(int index) const { return unionArrayPointer ? unionArrayPointer[index].getFConst() : 0.0f; }
373  bool getBConst(int index) const { return unionArrayPointer ? unionArrayPointer[index].getBConst() : false; }
374 
375  virtual TIntermConstantUnion* getAsConstantUnion() { return this; }
376  virtual void traverse(TIntermTraverser*);
377 
379 
380 protected:
382 };
383 
384 //
385 // Intermediate class for node types that hold operators.
386 //
388 public:
389  TOperator getOp() const { return op; }
390  void setOp(TOperator o) { op = o; }
391 
392  bool modifiesState() const;
393  bool isConstructor() const;
394 
395 protected:
399 };
400 
401 //
402 // Nodes for all the basic binary math operators.
403 //
405 public:
407 
408  virtual TIntermBinary* getAsBinaryNode() { return this; }
409  virtual void traverse(TIntermTraverser*);
410 
411  void setLeft(TIntermTyped* n) { left = n; }
412  void setRight(TIntermTyped* n) { right = n; }
413  TIntermTyped* getLeft() const { return left; }
414  TIntermTyped* getRight() const { return right; }
415  bool promote(TInfoSink&);
416 
417  void setAddIndexClamp() { addIndexClamp = true; }
418  bool getAddIndexClamp() { return addIndexClamp; }
419 
420 protected:
423 
424  // If set to true, wrap any EOpIndexIndirect with a clamp to bounds.
426 };
427 
428 //
429 // Nodes for unary math operators.
430 //
432 public:
435 
436  virtual void traverse(TIntermTraverser*);
437  virtual TIntermUnary* getAsUnaryNode() { return this; }
438 
439  void setOperand(TIntermTyped* o) { operand = o; }
441  bool promote(TInfoSink&);
442 
445 
446 protected:
448 
449  // If set to true, replace the built-in function call with an emulated one
450  // to work around driver bugs.
452 };
453 
456 
457 //
458 // Nodes that operate on an arbitrary sized set of children.
459 //
461 public:
465 
466  virtual TIntermAggregate* getAsAggregate() { return this; }
467  virtual void traverse(TIntermTraverser*);
468 
470 
471  void setName(const TString& n) { name = n; }
472  const TString& getName() const { return name; }
473 
474  void setUserDefined() { userDefined = true; }
475  bool isUserDefined() const { return userDefined; }
476 
477  void setOptimize(bool o) { optimize = o; }
478  bool getOptimize() { return optimize; }
479  void setDebug(bool d) { debug = d; }
480  bool getDebug() { return debug; }
481 
484 
485 protected:
486  TIntermAggregate(const TIntermAggregate&); // disallow copy constructor
487  TIntermAggregate& operator=(const TIntermAggregate&); // disallow assignment operator
490  bool userDefined; // used for user defined function names
491 
492  bool optimize;
493  bool debug;
494 
495  // If set to true, replace the built-in function call with an emulated one
496  // to work around driver bugs.
498 };
499 
500 //
501 // For if tests. Simplified since there is no switch statement.
502 //
504 public:
506  TIntermTyped(TType(EbtVoid, EbpUndefined)), condition(cond), trueBlock(trueB), falseBlock(falseB) {}
507  TIntermSelection(TIntermTyped* cond, TIntermNode* trueB, TIntermNode* falseB, const TType& type) :
508  TIntermTyped(type), condition(cond), trueBlock(trueB), falseBlock(falseB) {}
509 
510  virtual void traverse(TIntermTraverser*);
511 
512  bool usesTernaryOperator() const { return getBasicType() != EbtVoid; }
513  TIntermNode* getCondition() const { return condition; }
514  TIntermNode* getTrueBlock() const { return trueBlock; }
515  TIntermNode* getFalseBlock() const { return falseBlock; }
517 
518 protected:
522 };
523 
524 enum Visit
525 {
529 };
530 
531 //
532 // For traversing the tree. User should derive from this,
533 // put their traversal specific data in it, and then pass
534 // it to a Traverse method.
535 //
536 // When using this, just fill in the methods for nodes you want visited.
537 // Return false from a pre-visit to skip visiting that node's subtree.
538 //
540 {
541 public:
543  TIntermTraverser(bool preVisit = true, bool inVisit = false, bool postVisit = false, bool rightToLeft = false) :
545  inVisit(inVisit),
548  depth(0),
549  maxDepth(0) {}
550  virtual ~TIntermTraverser() {};
551 
552  virtual void visitSymbol(TIntermSymbol*) {}
554  virtual bool visitBinary(Visit visit, TIntermBinary*) {return true;}
555  virtual bool visitUnary(Visit visit, TIntermUnary*) {return true;}
556  virtual bool visitSelection(Visit visit, TIntermSelection*) {return true;}
557  virtual bool visitAggregate(Visit visit, TIntermAggregate*) {return true;}
558  virtual bool visitLoop(Visit visit, TIntermLoop*) {return true;}
559  virtual bool visitBranch(Visit visit, TIntermBranch*) {return true;}
560 
561  int getMaxDepth() const {return maxDepth;}
563  void decrementDepth() {depth--;}
564 
565  // Return the original name if hash function pointer is NULL;
566  // otherwise return the hashed name.
567  static TString hash(const TString& name, ShHashFunction64 hashFunction);
568 
569  const bool preVisit;
570  const bool inVisit;
571  const bool postVisit;
572  const bool rightToLeft;
573 
574 protected:
575  int depth;
576  int maxDepth;
577 };
578 
579 #endif // __INTERMEDIATE_H
bool isUserDefined() const
Definition: intermediate.h:475
TIntermConstantUnion(ConstantUnion *unionPointer, const TType &t)
Definition: intermediate.h:367
TOperator
Definition: intermediate.h:29
ConstantUnion * unionArrayPointer
Definition: intermediate.h:381
TIntermSelection * getAsSelectionNode()
Definition: intermediate.h:516
int elementRegisterCount() const
Definition: intermediate.h:268
GLenum condition
Definition: gl2ext.h:1403
virtual TIntermBinary * getAsBinaryNode()
Definition: intermediate.h:408
Visit
Definition: intermediate.h:524
TOperator getOp() const
Definition: intermediate.h:389
TQualifier
Definition: BaseTypes.h:81
virtual void traverse(TIntermTraverser *)
TIntermAggregate(TOperator o)
Definition: intermediate.h:463
bool getBConst()
Definition: ConstantUnion.h:27
TIntermTyped * expression
Definition: intermediate.h:334
virtual bool visitAggregate(Visit visit, TIntermAggregate *)
Definition: intermediate.h:557
void setOp(TOperator o)
Definition: intermediate.h:390
TIntermSequence & getSequence()
Definition: intermediate.h:469
TIntermNode * getTrueBlock() const
Definition: intermediate.h:514
TIntermTyped * getCondition()
Definition: intermediate.h:301
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum type
Definition: gl2ext.h:845
const TString & getOriginalSymbol() const
Definition: intermediate.h:354
POOL_ALLOCATOR_NEW_DELETE()
bool getUnrollFlag()
Definition: intermediate.h:306
void setName(const TString &n)
Definition: intermediate.h:471
GLint left
Definition: glew.h:7291
int getId() const
Definition: intermediate.h:348
TIntermTyped * getLeft() const
Definition: intermediate.h:413
bool promote(TInfoSink &)
TIntermAggregate & operator=(const TIntermAggregate &)
void setOptimize(bool o)
Definition: intermediate.h:477
virtual TIntermAggregate * getAsAggregate()
Definition: intermediate.h:223
virtual TIntermSymbol * getAsSymbolNode()
Definition: intermediate.h:357
TIntermBranch(TOperator op, TIntermTyped *e)
Definition: intermediate.h:323
khronos_uint64_t(* ShHashFunction64)(const char *, size_t)
Definition: ShaderLang.h:197
int getMaxDepth() const
Definition: intermediate.h:561
TIntermTyped * right
Definition: intermediate.h:422
GLclampd n
Definition: glew.h:7287
void setType(const TType &t)
Definition: intermediate.h:250
TString getCompleteString() const
Definition: intermediate.h:265
int last_file
Definition: Common.h:20
TOperator getFlowOp()
Definition: intermediate.h:329
TIntermNode * getFalseBlock() const
Definition: intermediate.h:515
TOperator flowOp
Definition: intermediate.h:333
TIntermTyped * getRight() const
Definition: intermediate.h:414
int totalRegisterCount() const
Definition: intermediate.h:267
SDL_Keycode sym
Definition: SDL_pspevents.c:51
return Display return Display Bool Bool int int e
Definition: SDL_x11sym.h:30
TLoopType type
Definition: intermediate.h:309
GLdouble GLdouble t
Definition: glew.h:1384
int getArraySize() const
Definition: intermediate.h:269
TBasicType getBasicType() const
Definition: intermediate.h:254
TPrecision getPrecision() const
Definition: intermediate.h:256
virtual TIntermConstantUnion * getAsConstantUnion()
Definition: intermediate.h:375
TIntermBinary(TOperator o)
Definition: intermediate.h:406
bool isConstructor() const
TIntermTraverser(bool preVisit=true, bool inVisit=false, bool postVisit=false, bool rightToLeft=false)
Definition: intermediate.h:543
EGLImageKHR EGLint * name
Definition: eglext.h:284
TString symbol
Definition: intermediate.h:361
virtual TIntermBinary * getAsBinaryNode()
Definition: intermediate.h:224
TQualifier getQualifier() const
Definition: intermediate.h:255
const bool preVisit
Definition: intermediate.h:569
return Display return Display Bool Bool int d
Definition: SDL_x11sym.h:30
TIntermNode * node2
Definition: intermediate.h:239
Definition: Types.h:93
GLuint id
Definition: gl2ext.h:1142
bool getBConst(int index) const
Definition: intermediate.h:373
bool isMatrix() const
Definition: intermediate.h:259
virtual ~TIntermTraverser()
Definition: intermediate.h:550
bool promote(TInfoSink &)
virtual bool visitBranch(Visit visit, TIntermBranch *)
Definition: intermediate.h:559
virtual ~TIntermNode()
Definition: intermediate.h:215
bool isScalar() const
Definition: intermediate.h:262
bool isVector() const
Definition: intermediate.h:261
void setUseEmulatedFunction()
Definition: intermediate.h:443
const bool postVisit
Definition: intermediate.h:571
bool getUseEmulatedFunction()
Definition: intermediate.h:444
void setLine(const TSourceLoc &l)
Definition: intermediate.h:218
virtual TIntermConstantUnion * getAsConstantUnion()
Definition: intermediate.h:222
TIntermTyped * cond
Definition: intermediate.h:311
virtual bool visitUnary(Visit visit, TIntermUnary *)
Definition: intermediate.h:555
TIntermTyped * condition
Definition: intermediate.h:519
TSourceLoc line
Definition: intermediate.h:231
float getFConst()
Definition: ConstantUnion.h:26
const bool rightToLeft
Definition: intermediate.h:572
virtual void visitSymbol(TIntermSymbol *)
Definition: intermediate.h:552
virtual bool visitBinary(Visit visit, TIntermBinary *)
Definition: intermediate.h:554
const bool inVisit
Definition: intermediate.h:570
bool getUseEmulatedFunction()
Definition: intermediate.h:483
virtual void traverse(TIntermTraverser *)=0
TIntermNode * getCondition() const
Definition: intermediate.h:513
const TSourceLoc & getLine() const
Definition: intermediate.h:217
const TType & getType() const
Definition: intermediate.h:251
static TString hash(const TString &name, ShHashFunction64 hashFunction)
virtual TIntermUnary * getAsUnaryNode()
Definition: intermediate.h:225
TIntermNode * getBody()
Definition: intermediate.h:303
ConstantUnion * getUnionArrayPointer() const
Definition: intermediate.h:369
void setRight(TIntermTyped *n)
Definition: intermediate.h:412
TIntermTyped * getExpression()
Definition: intermediate.h:330
const TString & getName() const
Definition: intermediate.h:472
TVector< int > TQualifierList
Definition: intermediate.h:455
GLint GLenum GLsizei GLsizei GLsizei depth
Definition: gl2ext.h:845
GLuint index
Definition: glew.h:1800
void setDebug(bool d)
Definition: intermediate.h:479
TIntermOperator(TOperator o)
Definition: intermediate.h:396
void setSymbol(const TString &sym)
Definition: intermediate.h:352
TIntermUnary(TOperator o)
Definition: intermediate.h:434
TIntermTyped * getOperand()
Definition: intermediate.h:440
TIntermNode * falseBlock
Definition: intermediate.h:521
virtual TIntermLoop * getAsLoopNode()
Definition: intermediate.h:228
virtual TIntermTyped * getAsTyped()
Definition: intermediate.h:221
virtual void traverse(TIntermTraverser *)
TBasicType
Definition: BaseTypes.h:36
TIntermTyped * left
Definition: intermediate.h:421
virtual TIntermLoop * getAsLoopNode()
Definition: intermediate.h:296
int getIConst(int index) const
Definition: intermediate.h:371
std::basic_string< char, std::char_traits< char >, TStringAllocator > TString
Definition: Common.h:41
void setLeft(TIntermTyped *n)
Definition: intermediate.h:411
int last_line
Definition: Common.h:21
virtual bool visitLoop(Visit visit, TIntermLoop *)
Definition: intermediate.h:558
bool usesTernaryOperator() const
Definition: intermediate.h:512
GLdouble l
Definition: glew.h:8383
int first_file
Definition: Common.h:18
virtual void traverse(TIntermTraverser *)
virtual bool visitSelection(Visit visit, TIntermSelection *)
Definition: intermediate.h:556
virtual TIntermUnary * getAsUnaryNode()
Definition: intermediate.h:437
void setAddIndexClamp()
Definition: intermediate.h:417
virtual void visitConstantUnion(TIntermConstantUnion *)
Definition: intermediate.h:553
TType * getTypePointer()
Definition: intermediate.h:252
TIntermNode * body
Definition: intermediate.h:313
bool isArray() const
Definition: intermediate.h:260
TIntermTyped * fold(TOperator, TIntermTyped *, TInfoSink &)
TIntermTyped * getExpression()
Definition: intermediate.h:302
TIntermLoop(TLoopType aType, TIntermNode *aInit, TIntermTyped *aCond, TIntermTyped *aExpr, TIntermNode *aBody)
Definition: intermediate.h:286
TIntermNode * node1
Definition: intermediate.h:238
virtual void traverse(TIntermTraverser *)
TIntermSymbol(int i, const TString &sym, const TType &t)
Definition: intermediate.h:345
bool getAddIndexClamp()
Definition: intermediate.h:418
virtual void traverse(TIntermTraverser *)
void setUseEmulatedFunction()
Definition: intermediate.h:482
virtual TIntermTyped * getAsTyped()
Definition: intermediate.h:248
TIntermNode * init
Definition: intermediate.h:310
TPrecision
Definition: BaseTypes.h:13
TString originalSymbol
Definition: intermediate.h:362
TVector< TIntermNode * > TIntermSequence
Definition: intermediate.h:454
virtual void traverse(TIntermTraverser *)
int first_line
Definition: Common.h:19
bool useEmulatedFunction
Definition: intermediate.h:451
TIntermOperator(TOperator o, TType &t)
Definition: intermediate.h:397
TIntermUnary(TOperator o, TType &t)
Definition: intermediate.h:433
const TString & getSymbol() const
Definition: intermediate.h:349
void setUnrollFlag(bool flag)
Definition: intermediate.h:305
TIntermSequence sequence
Definition: intermediate.h:488
TLoopType
Definition: intermediate.h:278
TIntermTyped * operand
Definition: intermediate.h:447
int i
Definition: pngrutil.c:1377
GLfloat right
Definition: glew.h:13816
const char * getBasicString() const
Definition: intermediate.h:263
virtual void traverse(TIntermTraverser *)
TIntermSelection(TIntermTyped *cond, TIntermNode *trueB, TIntermNode *falseB)
Definition: intermediate.h:505
float getFConst(int index) const
Definition: intermediate.h:372
TIntermNode * getInit()
Definition: intermediate.h:300
#define max(x, y)
Definition: os.h:79
const char * getOperatorString(TOperator op)
TIntermSelection(TIntermTyped *cond, TIntermNode *trueB, TIntermNode *falseB, const TType &type)
Definition: intermediate.h:507
bool modifiesState() const
int getNominalSize() const
Definition: intermediate.h:257
virtual void traverse(TIntermTraverser *)
virtual TIntermSymbol * getAsSymbolNode()
Definition: intermediate.h:227
void setId(int newId)
Definition: intermediate.h:351
TIntermTyped * expr
Definition: intermediate.h:312
#define false
Definition: ftrandom.c:50
TIntermNode * trueBlock
Definition: intermediate.h:520
virtual TIntermSelection * getAsSelectionNode()
Definition: intermediate.h:226
virtual TIntermAggregate * getAsAggregate()
Definition: intermediate.h:466
TIntermTyped(const TType &t)
Definition: intermediate.h:247
TLoopType getType() const
Definition: intermediate.h:299
const char * getQualifierString() const
Definition: intermediate.h:264
void setOperand(TIntermTyped *o)
Definition: intermediate.h:439