1/*
2 * Copyright (C) 2018-2019 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#pragma once
27
28namespace JSC {
29
30enum class GetByIdMode : uint8_t {
31 ProtoLoad = 0, // This must be zero to reuse the higher bits of the pointer as this ProtoLoad mode.
32 Default = 1,
33 Unset = 2,
34 ArrayLength = 3,
35};
36
37struct GetByIdModeMetadataDefault {
38 StructureID structureID;
39 PropertyOffset cachedOffset;
40 unsigned padding1;
41};
42static_assert(sizeof(GetByIdModeMetadataDefault) == 12);
43
44struct GetByIdModeMetadataUnset {
45 StructureID structureID;
46 unsigned padding1;
47 unsigned padding2;
48};
49static_assert(sizeof(GetByIdModeMetadataUnset) == 12);
50
51struct GetByIdModeMetadataArrayLength {
52 ArrayProfile arrayProfile;
53};
54static_assert(sizeof(GetByIdModeMetadataArrayLength) == 12);
55
56struct GetByIdModeMetadataProtoLoad {
57 StructureID structureID;
58 PropertyOffset cachedOffset;
59 JSObject* cachedSlot;
60};
61#if CPU(LITTLE_ENDIAN) && CPU(ADDRESS64)
62static_assert(sizeof(GetByIdModeMetadataProtoLoad) == 16);
63#endif
64
65// In 64bit Little endian architecture, this union shares ProtoLoad's JSObject* cachedSlot with "hitCountForLLIntCaching" and "mode".
66// This is possible because these values must be zero if we use ProtoLoad mode.
67#if CPU(LITTLE_ENDIAN) && CPU(ADDRESS64)
68union GetByIdModeMetadata {
69 GetByIdModeMetadata()
70 {
71 defaultMode.structureID = 0;
72 defaultMode.cachedOffset = 0;
73 defaultMode.padding1 = 0;
74 mode = GetByIdMode::Default;
75 hitCountForLLIntCaching = Options::prototypeHitCountForLLIntCaching();
76 }
77
78 void clearToDefaultModeWithoutCache();
79 void setUnsetMode(Structure*);
80 void setArrayLengthMode();
81 void setProtoLoadMode(Structure*, PropertyOffset, JSObject*);
82
83 struct {
84 uint32_t padding1;
85 uint32_t padding2;
86 uint32_t padding3;
87 uint16_t padding4;
88 GetByIdMode mode;
89 uint8_t hitCountForLLIntCaching; // This must be zero when we use ProtoLoad mode.
90 };
91 GetByIdModeMetadataDefault defaultMode;
92 GetByIdModeMetadataUnset unsetMode;
93 GetByIdModeMetadataArrayLength arrayLengthMode;
94 GetByIdModeMetadataProtoLoad protoLoadMode;
95};
96static_assert(sizeof(GetByIdModeMetadata) == 16);
97#else
98struct GetByIdModeMetadata {
99 GetByIdModeMetadata()
100 {
101 defaultMode.structureID = 0;
102 defaultMode.cachedOffset = 0;
103 defaultMode.padding1 = 0;
104 mode = GetByIdMode::Default;
105 hitCountForLLIntCaching = Options::prototypeHitCountForLLIntCaching();
106 }
107
108 void clearToDefaultModeWithoutCache();
109 void setUnsetMode(Structure*);
110 void setArrayLengthMode();
111 void setProtoLoadMode(Structure*, PropertyOffset, JSObject*);
112
113 union {
114 GetByIdModeMetadataDefault defaultMode;
115 GetByIdModeMetadataUnset unsetMode;
116 GetByIdModeMetadataArrayLength arrayLengthMode;
117 GetByIdModeMetadataProtoLoad protoLoadMode;
118 };
119 GetByIdMode mode;
120 uint8_t hitCountForLLIntCaching;
121};
122#endif
123
124inline void GetByIdModeMetadata::clearToDefaultModeWithoutCache()
125{
126 mode = GetByIdMode::Default;
127 defaultMode.structureID = 0;
128 defaultMode.cachedOffset = 0;
129}
130
131inline void GetByIdModeMetadata::setUnsetMode(Structure* structure)
132{
133 mode = GetByIdMode::Unset;
134 unsetMode.structureID = structure->id();
135}
136
137inline void GetByIdModeMetadata::setArrayLengthMode()
138{
139 mode = GetByIdMode::ArrayLength;
140 new (&arrayLengthMode.arrayProfile) ArrayProfile;
141 // Prevent the prototype cache from ever happening.
142 hitCountForLLIntCaching = 0;
143}
144
145inline void GetByIdModeMetadata::setProtoLoadMode(Structure* structure, PropertyOffset offset, JSObject* cachedSlot)
146{
147 mode = GetByIdMode::ProtoLoad; // This must be first set. In 64bit architecture, this field is shared with protoLoadMode.cachedSlot.
148 protoLoadMode.structureID = structure->id();
149 protoLoadMode.cachedOffset = offset;
150 // We know that this pointer will remain valid because it will be cleared by either a watchpoint fire or
151 // during GC when we clear the LLInt caches.
152 protoLoadMode.cachedSlot = cachedSlot;
153
154 ASSERT(mode == GetByIdMode::ProtoLoad);
155 ASSERT(!hitCountForLLIntCaching);
156 ASSERT(protoLoadMode.structureID == structure->id());
157 ASSERT(protoLoadMode.cachedOffset == offset);
158 ASSERT(protoLoadMode.cachedSlot == cachedSlot);
159}
160
161} // namespace JSC
162