blob: 2ec22f3b4bdb8d443ce5de722e72cc69c19345d6 [file] [log] [blame]
fpizlo@apple.com50cd41c2013-10-10 22:57:10 +00001/*
2 * Copyright (C) 2013 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 FTLLocation_h
27#define FTLLocation_h
28
29#include <wtf/Platform.h>
30
31#if ENABLE(FTL_JIT)
32
33#include "FPRInfo.h"
34#include "FTLStackMaps.h"
35#include "GPRInfo.h"
36#include <wtf/HashMap.h>
37
38namespace JSC { namespace FTL {
39
40class Location {
41public:
42 enum Kind {
43 Unprocessed,
44 Register,
45 Indirect,
46 Constant
47 };
48
49 Location()
50 : m_kind(Unprocessed)
51 {
52 u.constant = 0;
53 }
54
55 Location(WTF::HashTableDeletedValueType)
56 : m_kind(Unprocessed)
57 {
58 u.constant = 1;
59 }
60
61 static Location forRegister(int16_t dwarfRegNum)
62 {
63 Location result;
64 result.m_kind = Register;
65 result.u.variable.dwarfRegNum = dwarfRegNum;
66 return result;
67 }
68
69 static Location forIndirect(int16_t dwarfRegNum, int32_t offset)
70 {
71 Location result;
72 result.m_kind = Indirect;
73 result.u.variable.dwarfRegNum = dwarfRegNum;
74 result.u.variable.offset = offset;
75 return result;
76 }
77
78 static Location forConstant(int64_t constant)
79 {
80 Location result;
81 result.m_kind = Constant;
82 result.u.constant = constant;
83 return result;
84 }
85
86 static Location forStackmaps(const StackMaps&, const StackMaps::Location&);
87
88 Kind kind() const { return m_kind; }
89
90 bool hasDwarfRegNum() const { return kind() == Register || kind() == Indirect; }
91 int16_t dwarfRegNum() const
92 {
93 ASSERT(hasDwarfRegNum());
94 return u.variable.dwarfRegNum;
95 }
96
97 bool hasOffset() const { return kind() == Indirect; }
98 int32_t offset() const
99 {
100 ASSERT(hasOffset());
101 return u.variable.offset;
102 }
103
104 bool hasConstant() const { return kind() == Constant; }
105 int64_t constant() const
106 {
107 ASSERT(hasConstant());
108 return u.constant;
109 }
110
111 bool operator!() const { return kind() == Unprocessed && !u.variable.offset; }
112
113 bool isHashTableDeletedValue() const { return kind() == Unprocessed && u.variable.offset; }
114
115 bool operator==(const Location& other) const
116 {
117 return m_kind == other.m_kind
118 && u.constant == other.u.constant;
119 }
120
121 unsigned hash() const
122 {
123 unsigned result = m_kind;
124
125 switch (kind()) {
126 case Unprocessed:
127 result ^= u.variable.offset;
128 break;
129
130 case Register:
131 result ^= u.variable.dwarfRegNum;
132 break;
133
134 case Indirect:
135 result ^= u.variable.dwarfRegNum;
136 result ^= u.variable.offset;
137 break;
138
139 case Constant:
140 result ^= WTF::IntHash<int64_t>::hash(u.constant);
141 break;
142 }
143
144 return WTF::IntHash<unsigned>::hash(result);
145 }
146
147 void dump(PrintStream&) const;
148
149 bool isGPR() const;
150 bool involvesGPR() const;
151 GPRReg gpr() const;
152
153 bool isFPR() const;
154 FPRReg fpr() const;
155
156 // Assuming that all registers are saved to the savedRegisters buffer according
157 // to FTLSaveRestore convention, this loads the value into the given register.
fpizlo@apple.come5192ec2013-10-12 01:33:16 +0000158 // The code that this generates isn't exactly super fast. This assumes that FP
159 // and SP contain the same values that they would have contained in the original
160 // frame. If we did push things onto the stack then probably we'll have to change
161 // the signature of this method to take a stack offset for stack-relative
162 // indirects.
fpizlo@apple.com50cd41c2013-10-10 22:57:10 +0000163 void restoreInto(MacroAssembler&, char* savedRegisters, GPRReg result) const;
164
165private:
166 Kind m_kind;
167 union {
168 int64_t constant;
169 struct {
170 int16_t dwarfRegNum;
171 int32_t offset;
172 } variable;
173 } u;
174};
175
176struct LocationHash {
177 static unsigned hash(const Location& key) { return key.hash(); }
178 static bool equal(const Location& a, const Location& b) { return a == b; }
179 static const bool safeToCompareToEmptyOrDeleted = true;
180};
181
182} } // namespace JSC::FTL
183
184namespace WTF {
185
186void printInternal(PrintStream&, JSC::FTL::Location::Kind);
187
188template<typename T> struct DefaultHash;
189template<> struct DefaultHash<JSC::FTL::Location> {
190 typedef JSC::FTL::LocationHash Hash;
191};
192
193template<typename T> struct HashTraits;
194template<> struct HashTraits<JSC::FTL::Location> : SimpleClassHashTraits<JSC::FTL::Location> { };
195
196} // namespace WTF
197
198#endif // ENABLE(FTL_JIT)
199
200#endif // FTLLocation_h
201