1//
2// Copyright (c) 2002-2014 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#ifndef COMPILER_TRANSLATOR_LOOP_INFO_H_
8#define COMPILER_TRANSLATOR_LOOP_INFO_H_
9
10#include "compiler/translator/IntermNode.h"
11
12class TLoopIndexInfo
13{
14  public:
15    TLoopIndexInfo();
16
17    // If type is EbtInt, fill all fields of the structure with info
18    // extracted from a loop node.
19    // If type is not EbtInt, only fill id and type.
20    void fillInfo(TIntermLoop *node);
21
22    int getId() const { return mId; }
23    void setId(int id) { mId = id; }
24    TBasicType getType() const { return mType; }
25    void setType(TBasicType type) { mType = type; }
26    int getCurrentValue() const { return mCurrentValue; }
27
28    void step() { mCurrentValue += mIncrementValue; }
29
30    // Check if the current value satisfies the loop condition.
31    bool satisfiesLoopCondition() const;
32
33  private:
34    int mId;
35    TBasicType mType;  // Either EbtInt or EbtFloat
36
37    // Below fields are only valid if the index's type is int.
38    int mInitValue;
39    int mStopValue;
40    int mIncrementValue;
41    TOperator mOp;
42    int mCurrentValue;
43};
44
45struct TLoopInfo
46{
47    TLoopIndexInfo index;
48    TIntermLoop *loop;
49
50    TLoopInfo();
51    TLoopInfo(TIntermLoop *node);
52};
53
54class TLoopStack : public TVector<TLoopInfo>
55{
56  public:
57    // Search loop stack for a loop whose index matches the input symbol.
58    TIntermLoop *findLoop(TIntermSymbol *symbol);
59
60    // Find the loop index info in the loop stack by the input symbol.
61    TLoopIndexInfo *getIndexInfo(TIntermSymbol *symbol);
62
63    // Update the currentValue for the next loop iteration.
64    void step();
65
66    // Return false if loop condition is no longer satisfied.
67    bool satisfiesLoopCondition();
68
69    // Check if the symbol is the index of a loop that's unrolled.
70    bool needsToReplaceSymbolWithValue(TIntermSymbol *symbol);
71
72    // Return the current value of a given loop index symbol.
73    int getLoopIndexValue(TIntermSymbol *symbol);
74
75    void push(TIntermLoop *info);
76    void pop();
77};
78
79#endif // COMPILER_TRANSLATOR_LOOP_INDEX_H_
80
81