initial import
[vuplus_webkit] / Source / JavaScriptCore / bytecode / PredictionTracker.h
1 /*
2  * Copyright (C) 2011 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #ifndef PredictionTracker_h
27 #define PredictionTracker_h
28
29 #include "PredictedType.h"
30 #include <wtf/HashMap.h>
31 #include <wtf/Vector.h>
32
33 namespace JSC {
34
35 // helper function to distinguish vars & temporaries from arguments.
36 inline bool operandIsArgument(int operand) { return operand < 0; }
37
38 struct PredictionSlot {
39 public:
40     PredictionSlot()
41         : m_value(PredictNone)
42     {
43     }
44     PredictedType m_value;
45 };
46
47 class PredictionTracker {
48 public:
49     PredictionTracker()
50     {
51     }
52     
53     PredictionTracker(unsigned numArguments, unsigned numVariables)
54         : m_arguments(numArguments)
55         , m_variables(numVariables)
56     {
57     }
58     
59     void initializeSimilarTo(const PredictionTracker& other)
60     {
61         m_arguments.resize(other.numberOfArguments());
62         m_variables.resize(other.numberOfVariables());
63     }
64     
65     void copyLocalsFrom(const PredictionTracker& other)
66     {
67         m_arguments = other.m_arguments;
68         m_variables = other.m_variables;
69     }
70     
71     size_t numberOfArguments() const { return m_arguments.size(); }
72     size_t numberOfVariables() const { return m_variables.size(); }
73     
74     unsigned argumentIndexForOperand(int operand) const { return operand + m_arguments.size() + RegisterFile::CallFrameHeaderSize; }
75
76     bool predictArgument(unsigned argument, PredictedType prediction, PredictionSource source)
77     {
78         return mergePrediction(m_arguments[argument].m_value, makePrediction(prediction, source));
79     }
80     
81     bool predict(int operand, PredictedType prediction, PredictionSource source)
82     {
83         if (operandIsArgument(operand))
84             return predictArgument(argumentIndexForOperand(operand), prediction, source);
85         if ((unsigned)operand >= m_variables.size()) {
86             ASSERT(operand >= 0);
87             m_variables.resize(operand + 1);
88         }
89         
90         return mergePrediction(m_variables[operand].m_value, makePrediction(prediction, source));
91     }
92     
93     bool predictGlobalVar(unsigned varNumber, PredictedType prediction, PredictionSource source)
94     {
95         HashMap<unsigned, PredictionSlot>::iterator iter = m_globalVars.find(varNumber + 1);
96         if (iter == m_globalVars.end()) {
97             PredictionSlot predictionSlot;
98             bool result = mergePrediction(predictionSlot.m_value, makePrediction(prediction, source));
99             m_globalVars.add(varNumber + 1, predictionSlot);
100             return result;
101         }
102         return mergePrediction(iter->second.m_value, makePrediction(prediction, source));
103     }
104     
105     PredictedType getArgumentPrediction(unsigned argument)
106     {
107         return m_arguments[argument].m_value;
108     }
109
110     PredictedType getPrediction(int operand)
111     {
112         if (operandIsArgument(operand))
113             return getArgumentPrediction(argumentIndexForOperand(operand));
114         if ((unsigned)operand < m_variables.size())
115             return m_variables[operand].m_value;
116         return PredictNone;
117     }
118     
119     PredictedType getGlobalVarPrediction(unsigned varNumber)
120     {
121         HashMap<unsigned, PredictionSlot>::iterator iter = m_globalVars.find(varNumber + 1);
122         if (iter == m_globalVars.end())
123             return PredictNone;
124         return iter->second.m_value;
125     }
126     
127 private:
128     Vector<PredictionSlot, 16> m_arguments;
129     Vector<PredictionSlot, 16> m_variables;
130     HashMap<unsigned, PredictionSlot> m_globalVars;
131 };
132
133 } // namespace JSC
134
135 #endif // PredictionTracker_h
136