|
Lines 1-5
Source/JavaScriptCore/bytecode/StructureSet.h_sec1
|
| 1 |
/* |
1 |
/* |
| 2 |
* Copyright (C) 2011, 2013 Apple Inc. All rights reserved. |
2 |
* Copyright (C) 2011, 2013, 2014 Apple Inc. All rights reserved. |
| 3 |
* |
3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions |
5 |
* modification, are permitted provided that the following conditions |
|
Lines 30-196
Source/JavaScriptCore/bytecode/StructureSet.h_sec2
|
| 30 |
#include "SpeculatedType.h" |
30 |
#include "SpeculatedType.h" |
| 31 |
#include "Structure.h" |
31 |
#include "Structure.h" |
| 32 |
#include "DumpContext.h" |
32 |
#include "DumpContext.h" |
| 33 |
#include <wtf/CommaPrinter.h> |
|
|
| 34 |
#include <wtf/Vector.h> |
| 35 |
|
33 |
|
| 36 |
namespace JSC { |
34 |
namespace JSC { |
| 37 |
|
35 |
|
| 38 |
namespace DFG { |
36 |
// This is a structure set that doesn't own any memory. You can use it to wrap a |
| 39 |
class StructureAbstractValue; |
37 |
// uintptr_t, which it will then interpret as a structure set. This can be either |
| 40 |
} |
38 |
// subtyped to provide StructureSet functionality or it can be used as part of a |
| 41 |
|
39 |
// more sophisticated data structure that revolves around a uintptr_t. |
| 42 |
class StructureSet { |
40 |
class BorrowedStructureSet { |
| 43 |
public: |
41 |
public: |
| 44 |
StructureSet() { } |
42 |
static const unsigned thinFlag = 1; |
|
|
43 |
static const unsigned reservedFlag = 2; |
| 44 |
static const unsigned flags = 3; |
| 45 |
static const unsigned reservedValue = 4; |
| 45 |
|
46 |
|
| 46 |
StructureSet(Structure* structure) |
47 |
explicit BorrowedStructureSet(uintptr_t pointer) |
|
|
48 |
: m_pointer(pointer) |
| 47 |
{ |
49 |
{ |
| 48 |
ASSERT(structure); |
|
|
| 49 |
m_structures.append(structure); |
| 50 |
} |
50 |
} |
| 51 |
|
51 |
|
| 52 |
void clear() |
52 |
void clear(); |
| 53 |
{ |
|
|
| 54 |
m_structures.clear(); |
| 55 |
} |
| 56 |
|
| 57 |
void add(Structure* structure) |
| 58 |
{ |
| 59 |
ASSERT(structure); |
| 60 |
ASSERT(!contains(structure)); |
| 61 |
m_structures.append(structure); |
| 62 |
} |
| 63 |
|
53 |
|
| 64 |
bool addAll(const StructureSet& other) |
54 |
Structure* onlyStructure() const |
| 65 |
{ |
55 |
{ |
| 66 |
bool changed = false; |
56 |
if (isThin()) { |
| 67 |
for (size_t i = 0; i < other.size(); ++i) { |
57 |
ASSERT(singleStructure()); |
| 68 |
if (contains(other[i])) |
58 |
return singleStructure(); |
| 69 |
continue; |
|
|
| 70 |
add(other[i]); |
| 71 |
changed = true; |
| 72 |
} |
59 |
} |
| 73 |
return changed; |
60 |
ASSERT(structureList()->m_length == 1); |
|
|
61 |
return structureList()->list()[0]; |
| 74 |
} |
62 |
} |
| 75 |
|
63 |
|
| 76 |
void remove(Structure* structure) |
64 |
bool isEmpty() const |
| 77 |
{ |
65 |
{ |
| 78 |
for (size_t i = 0; i < m_structures.size(); ++i) { |
66 |
return isThin() && !singleStructure(); |
| 79 |
if (m_structures[i] != structure) |
|
|
| 80 |
continue; |
| 81 |
|
| 82 |
m_structures[i] = m_structures.last(); |
| 83 |
m_structures.removeLast(); |
| 84 |
return; |
| 85 |
} |
| 86 |
} |
67 |
} |
| 87 |
|
68 |
|
| 88 |
bool contains(Structure* structure) const |
69 |
bool add(Structure* structure); |
|
|
70 |
bool remove(Structure* structure); |
| 71 |
bool contains(Structure* structure) const; |
| 72 |
|
| 73 |
bool merge(const BorrowedStructureSet& other); |
| 74 |
void filter(const BorrowedStructureSet& other); |
| 75 |
void exclude(const BorrowedStructureSet& other); |
| 76 |
|
| 77 |
bool isSubsetOf(const BorrowedStructureSet& other) const; |
| 78 |
bool isSupersetOf(const BorrowedStructureSet& other) const |
| 89 |
{ |
79 |
{ |
| 90 |
for (size_t i = 0; i < m_structures.size(); ++i) { |
80 |
return other.isSubsetOf(*this); |
| 91 |
if (m_structures[i] == structure) |
|
|
| 92 |
return true; |
| 93 |
} |
| 94 |
return false; |
| 95 |
} |
81 |
} |
| 96 |
|
82 |
|
| 97 |
bool containsOnly(Structure* structure) const |
83 |
bool overlaps(const BorrowedStructureSet& other) const; |
|
|
84 |
|
| 85 |
size_t size() const |
| 98 |
{ |
86 |
{ |
| 99 |
if (size() != 1) |
87 |
if (isThin()) |
| 100 |
return false; |
88 |
return !!singleStructure(); |
| 101 |
return singletonStructure() == structure; |
89 |
return structureList()->m_length; |
| 102 |
} |
90 |
} |
| 103 |
|
91 |
|
| 104 |
bool isSubsetOf(const StructureSet& other) const |
92 |
Structure* at(size_t i) const |
| 105 |
{ |
93 |
{ |
| 106 |
for (size_t i = 0; i < m_structures.size(); ++i) { |
94 |
if (isThin()) { |
| 107 |
if (!other.contains(m_structures[i])) |
95 |
ASSERT(!i); |
| 108 |
return false; |
96 |
ASSERT(singleStructure()); |
|
|
97 |
return singleStructure(); |
| 109 |
} |
98 |
} |
| 110 |
return true; |
99 |
ASSERT(i < structureList()->m_length); |
|
|
100 |
return structureList()->list()[i]; |
| 111 |
} |
101 |
} |
| 112 |
|
102 |
|
| 113 |
bool isSupersetOf(const StructureSet& other) const |
103 |
Structure* operator[](size_t i) const { return at(i); } |
| 114 |
{ |
|
|
| 115 |
return other.isSubsetOf(*this); |
| 116 |
} |
| 117 |
|
104 |
|
| 118 |
bool overlaps(const StructureSet& other) const |
105 |
Structure* last() const |
| 119 |
{ |
106 |
{ |
| 120 |
for (size_t i = 0; i < m_structures.size(); ++i) { |
107 |
if (isThin()) { |
| 121 |
if (other.contains(m_structures[i])) |
108 |
ASSERT(singleStructure()); |
| 122 |
return true; |
109 |
return singleStructure(); |
| 123 |
} |
110 |
} |
| 124 |
return false; |
111 |
return structureList()->list()[structureList()->m_length - 1]; |
| 125 |
} |
112 |
} |
| 126 |
|
113 |
|
| 127 |
size_t size() const { return m_structures.size(); } |
114 |
bool operator==(const BorrowedStructureSet& other) const; |
| 128 |
|
115 |
|
| 129 |
// Call this if you know that the structure set must consist of exactly |
116 |
SpeculatedType speculationFromStructures() const; |
| 130 |
// one structure. |
117 |
ArrayModes arrayModesFromStructures() const; |
| 131 |
Structure* singletonStructure() const |
|
|
| 132 |
{ |
| 133 |
ASSERT(m_structures.size() == 1); |
| 134 |
return m_structures[0]; |
| 135 |
} |
| 136 |
|
118 |
|
| 137 |
Structure* at(size_t i) const { return m_structures.at(i); } |
119 |
void dumpInContext(PrintStream& out, DumpContext* context) const; |
|
|
120 |
void dump(PrintStream& out) const; |
| 138 |
|
121 |
|
| 139 |
Structure* operator[](size_t i) const { return at(i); } |
122 |
uintptr_t encodedPointer() const { return m_pointer; } |
| 140 |
|
123 |
|
| 141 |
Structure* last() const { return m_structures.last(); } |
124 |
protected: |
| 142 |
|
125 |
bool addOutOfLine(Structure* structure); |
| 143 |
SpeculatedType speculationFromStructures() const |
126 |
bool containsOutOfLine(Structure* structure) const; |
| 144 |
{ |
127 |
|
| 145 |
SpeculatedType result = SpecNone; |
128 |
static const unsigned defaultStartingSize = 4; |
|
|
129 |
|
| 130 |
class OutOfLineList { |
| 131 |
public: |
| 132 |
static OutOfLineList* create(unsigned capacity); |
| 133 |
static void destroy(OutOfLineList* list); |
| 146 |
|
134 |
|
| 147 |
for (size_t i = 0; i < m_structures.size(); ++i) |
135 |
Structure** list() { return bitwise_cast<Structure**>(this + 1); } |
| 148 |
mergeSpeculation(result, speculationFromStructure(m_structures[i])); |
|
|
| 149 |
|
136 |
|
| 150 |
return result; |
137 |
OutOfLineList(unsigned length, unsigned capacity) |
|
|
138 |
: m_length(length) |
| 139 |
, m_capacity(capacity) |
| 140 |
{ |
| 141 |
} |
| 142 |
|
| 143 |
unsigned m_length; |
| 144 |
unsigned m_capacity; |
| 145 |
}; |
| 146 |
|
| 147 |
void deleteStructureListIfNecessary() |
| 148 |
{ |
| 149 |
if (!isThin()) |
| 150 |
OutOfLineList::destroy(structureList()); |
| 151 |
} |
151 |
} |
| 152 |
|
152 |
|
| 153 |
ArrayModes arrayModesFromStructures() const |
153 |
bool isThin() const { return m_pointer & thinFlag; } |
|
|
154 |
|
| 155 |
void* pointer() const |
| 154 |
{ |
156 |
{ |
| 155 |
ArrayModes result = 0; |
157 |
return bitwise_cast<void*>(m_pointer & ~flags); |
| 156 |
|
|
|
| 157 |
for (size_t i = 0; i < m_structures.size(); ++i) |
| 158 |
mergeArrayModes(result, asArrayModes(m_structures[i]->indexingType())); |
| 159 |
|
| 160 |
return result; |
| 161 |
} |
158 |
} |
| 162 |
|
159 |
|
| 163 |
bool operator==(const StructureSet& other) const |
160 |
Structure* singleStructure() const |
| 164 |
{ |
161 |
{ |
| 165 |
if (m_structures.size() != other.m_structures.size()) |
162 |
ASSERT(isThin()); |
| 166 |
return false; |
163 |
return static_cast<Structure*>(pointer()); |
| 167 |
|
|
|
| 168 |
for (size_t i = 0; i < m_structures.size(); ++i) { |
| 169 |
if (!other.contains(m_structures[i])) |
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
return true; |
| 174 |
} |
164 |
} |
| 175 |
|
165 |
|
| 176 |
void dumpInContext(PrintStream& out, DumpContext* context) const |
166 |
OutOfLineList* structureList() const |
| 177 |
{ |
167 |
{ |
| 178 |
CommaPrinter comma; |
168 |
ASSERT(!isThin()); |
| 179 |
out.print("["); |
169 |
return static_cast<OutOfLineList*>(pointer()); |
| 180 |
for (size_t i = 0; i < m_structures.size(); ++i) |
|
|
| 181 |
out.print(comma, inContext(*m_structures[i], context)); |
| 182 |
out.print("]"); |
| 183 |
} |
170 |
} |
| 184 |
|
171 |
|
| 185 |
void dump(PrintStream& out) const |
172 |
void set(Structure* structure) |
|
|
173 |
{ |
| 174 |
set(bitwise_cast<uintptr_t>(structure), true); |
| 175 |
} |
| 176 |
void set(OutOfLineList* structures) |
| 177 |
{ |
| 178 |
set(bitwise_cast<uintptr_t>(structures), false); |
| 179 |
} |
| 180 |
void set(uintptr_t pointer, bool singleStructure) |
| 181 |
{ |
| 182 |
m_pointer = pointer | (singleStructure ? thinFlag : 0) | (m_pointer & reservedFlag); |
| 183 |
} |
| 184 |
|
| 185 |
uintptr_t m_pointer; |
| 186 |
}; |
| 187 |
|
| 188 |
class StructureSet : public BorrowedStructureSet { |
| 189 |
public: |
| 190 |
StructureSet() |
| 191 |
: BorrowedStructureSet(0) |
| 186 |
{ |
192 |
{ |
| 187 |
dumpInContext(out, 0); |
|
|
| 188 |
} |
193 |
} |
| 189 |
|
194 |
|
| 190 |
private: |
195 |
StructureSet(Structure* structure) |
| 191 |
friend class DFG::StructureAbstractValue; |
196 |
: BorrowedStructureSet(0) |
|
|
197 |
{ |
| 198 |
set(structure); |
| 199 |
} |
| 192 |
|
200 |
|
| 193 |
Vector<Structure*, 2> m_structures; |
201 |
~StructureSet() |
|
|
202 |
{ |
| 203 |
deleteStructureListIfNecessary(); |
| 204 |
} |
| 194 |
}; |
205 |
}; |
| 195 |
|
206 |
|
| 196 |
} // namespace JSC |
207 |
} // namespace JSC |