1/*
2 * Copyright (C) 2015 Yusuke Suzuki <[email protected]>.
3 * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include "Identifier.h"
30#include <wtf/Noncopyable.h>
31
32namespace JSC {
33
34class CommonIdentifiers;
35class BytecodeGenerator;
36class BytecodeIntrinsicNode;
37class RegisterID;
38enum class LinkTimeConstant : int32_t;
39
40#define JSC_COMMON_BYTECODE_INTRINSIC_FUNCTIONS_EACH_NAME(macro) \
41 macro(argument) \
42 macro(argumentCount) \
43 macro(getByIdDirect) \
44 macro(getByIdDirectPrivate) \
45 macro(getPromiseInternalField) \
46 macro(getGeneratorInternalField) \
47 macro(getAsyncGeneratorInternalField) \
48 macro(idWithProfile) \
49 macro(isObject) \
50 macro(isJSArray) \
51 macro(isProxyObject) \
52 macro(isDerivedArray) \
53 macro(isGenerator) \
54 macro(isAsyncGenerator) \
55 macro(isPromise) \
56 macro(isRegExpObject) \
57 macro(isMap) \
58 macro(isSet) \
59 macro(isUndefinedOrNull) \
60 macro(tailCallForwardArguments) \
61 macro(throwTypeError) \
62 macro(throwRangeError) \
63 macro(throwOutOfMemoryError) \
64 macro(tryGetById) \
65 macro(putByIdDirect) \
66 macro(putByIdDirectPrivate) \
67 macro(putByValDirect) \
68 macro(putPromiseInternalField) \
69 macro(putGeneratorInternalField) \
70 macro(putAsyncGeneratorInternalField) \
71 macro(toNumber) \
72 macro(toString) \
73 macro(toObject) \
74 macro(newArrayWithSize) \
75 macro(newPromise) \
76 macro(createPromise) \
77 macro(defineEnumerableWritableConfigurableDataProperty) \
78
79#define JSC_COMMON_BYTECODE_INTRINSIC_CONSTANTS_EACH_NAME(macro) \
80 JSC_COMMON_BYTECODE_INTRINSIC_CONSTANTS_SIMPLE_EACH_NAME(macro) \
81 JSC_COMMON_BYTECODE_INTRINSIC_CONSTANTS_CUSTOM_EACH_NAME(macro) \
82
83#define JSC_COMMON_BYTECODE_INTRINSIC_CONSTANTS_SIMPLE_EACH_NAME(macro) \
84 macro(undefined) \
85 macro(Infinity) \
86 macro(iterationKindKey) \
87 macro(iterationKindValue) \
88 macro(iterationKindKeyValue) \
89 macro(MAX_ARRAY_INDEX) \
90 macro(MAX_STRING_LENGTH) \
91 macro(MAX_SAFE_INTEGER) \
92 macro(ModuleFetch) \
93 macro(ModuleTranslate) \
94 macro(ModuleInstantiate) \
95 macro(ModuleSatisfy) \
96 macro(ModuleLink) \
97 macro(ModuleReady) \
98 macro(promiseRejectionReject) \
99 macro(promiseRejectionHandle) \
100 macro(promiseStatePending) \
101 macro(promiseStateFulfilled) \
102 macro(promiseStateRejected) \
103 macro(promiseStateMask) \
104 macro(promiseFlagsIsHandled) \
105 macro(promiseFlagsIsFirstResolvingFunctionCalled) \
106 macro(promiseFieldFlags) \
107 macro(promiseFieldReactionsOrResult) \
108 macro(generatorFieldState) \
109 macro(generatorFieldNext) \
110 macro(generatorFieldThis) \
111 macro(generatorFieldFrame) \
112 macro(GeneratorResumeModeNormal) \
113 macro(GeneratorResumeModeThrow) \
114 macro(GeneratorResumeModeReturn) \
115 macro(GeneratorStateCompleted) \
116 macro(GeneratorStateExecuting) \
117 macro(asyncGeneratorFieldSuspendReason) \
118 macro(asyncGeneratorFieldQueueFirst) \
119 macro(asyncGeneratorFieldQueueLast) \
120 macro(AsyncGeneratorStateCompleted) \
121 macro(AsyncGeneratorStateExecuting) \
122 macro(AsyncGeneratorStateAwaitingReturn) \
123 macro(AsyncGeneratorStateSuspendedStart) \
124 macro(AsyncGeneratorStateSuspendedYield) \
125 macro(AsyncGeneratorSuspendReasonYield) \
126 macro(AsyncGeneratorSuspendReasonAwait) \
127 macro(AsyncGeneratorSuspendReasonNone) \
128
129#define JSC_COMMON_BYTECODE_INTRINSIC_CONSTANTS_CUSTOM_EACH_NAME(macro) \
130 macro(sentinelMapBucket) \
131 macro(sentinelSetBucket) \
132
133class BytecodeIntrinsicRegistry {
134 WTF_MAKE_FAST_ALLOCATED;
135 WTF_MAKE_NONCOPYABLE(BytecodeIntrinsicRegistry);
136public:
137 explicit BytecodeIntrinsicRegistry(VM&);
138
139 typedef RegisterID* (BytecodeIntrinsicNode::* EmitterType)(BytecodeGenerator&, RegisterID*);
140
141 enum class Type : uint8_t {
142 Emitter = 0,
143 LinkTimeConstant = 1,
144 };
145
146 class Entry {
147 public:
148 Entry()
149 : m_type(Type::Emitter)
150 {
151 m_emitter = nullptr;
152 }
153
154 Entry(EmitterType emitter)
155 : m_type(Type::Emitter)
156 {
157 m_emitter = emitter;
158 }
159
160 Entry(LinkTimeConstant linkTimeConstant)
161 : m_type(Type::LinkTimeConstant)
162 {
163 m_linkTimeConstant = linkTimeConstant;
164 }
165
166 Type type() const { return m_type; }
167 LinkTimeConstant linkTimeConstant() const { return m_linkTimeConstant; }
168 EmitterType emitter() const { return m_emitter; }
169
170 private:
171 union {
172 EmitterType m_emitter;
173 LinkTimeConstant m_linkTimeConstant;
174 };
175 Type m_type;
176 };
177
178 Optional<Entry> lookup(const Identifier&) const;
179
180#define JSC_DECLARE_BYTECODE_INTRINSIC_CONSTANT_GENERATORS(name) JSValue name##Value(BytecodeGenerator&);
181 JSC_COMMON_BYTECODE_INTRINSIC_CONSTANTS_EACH_NAME(JSC_DECLARE_BYTECODE_INTRINSIC_CONSTANT_GENERATORS)
182#undef JSC_DECLARE_BYTECODE_INTRINSIC_CONSTANT_GENERATORS
183
184private:
185 VM& m_vm;
186 HashMap<RefPtr<UniquedStringImpl>, Entry, IdentifierRepHash> m_bytecodeIntrinsicMap;
187
188#define JSC_DECLARE_BYTECODE_INTRINSIC_CONSTANT_GENERATORS(name) Strong<Unknown> m_##name;
189 JSC_COMMON_BYTECODE_INTRINSIC_CONSTANTS_SIMPLE_EACH_NAME(JSC_DECLARE_BYTECODE_INTRINSIC_CONSTANT_GENERATORS)
190#undef JSC_DECLARE_BYTECODE_INTRINSIC_CONSTANT_GENERATORS
191};
192
193} // namespace JSC
194