443cd6c1d1802f0ff67cbe8e840cd90da1b5e3b7
[WebKit-https.git] / Source / JavaScriptCore / jit / AssemblyHelpers.cpp
1 /*
2  * Copyright (C) 2011, 2013-2015 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 #include "config.h"
27 #include "AssemblyHelpers.h"
28
29 #if ENABLE(JIT)
30
31 #include "JITOperations.h"
32 #include "JSCInlines.h"
33
34 namespace JSC {
35
36 ExecutableBase* AssemblyHelpers::executableFor(const CodeOrigin& codeOrigin)
37 {
38     if (!codeOrigin.inlineCallFrame)
39         return m_codeBlock->ownerExecutable();
40     
41     return codeOrigin.inlineCallFrame->executable.get();
42 }
43
44 Vector<BytecodeAndMachineOffset>& AssemblyHelpers::decodedCodeMapFor(CodeBlock* codeBlock)
45 {
46     ASSERT(codeBlock == codeBlock->baselineVersion());
47     ASSERT(codeBlock->jitType() == JITCode::BaselineJIT);
48     ASSERT(codeBlock->jitCodeMap());
49     
50     HashMap<CodeBlock*, Vector<BytecodeAndMachineOffset>>::AddResult result = m_decodedCodeMaps.add(codeBlock, Vector<BytecodeAndMachineOffset>());
51     
52     if (result.isNewEntry)
53         codeBlock->jitCodeMap()->decode(result.iterator->value);
54     
55     return result.iterator->value;
56 }
57
58 void AssemblyHelpers::purifyNaN(FPRReg fpr)
59 {
60     MacroAssembler::Jump notNaN = branchDouble(DoubleEqual, fpr, fpr);
61     static const double NaN = PNaN;
62     loadDouble(TrustedImmPtr(&NaN), fpr);
63     notNaN.link(this);
64 }
65
66 #if ENABLE(SAMPLING_FLAGS)
67 void AssemblyHelpers::setSamplingFlag(int32_t flag)
68 {
69     ASSERT(flag >= 1);
70     ASSERT(flag <= 32);
71     or32(TrustedImm32(1u << (flag - 1)), AbsoluteAddress(SamplingFlags::addressOfFlags()));
72 }
73
74 void AssemblyHelpers::clearSamplingFlag(int32_t flag)
75 {
76     ASSERT(flag >= 1);
77     ASSERT(flag <= 32);
78     and32(TrustedImm32(~(1u << (flag - 1))), AbsoluteAddress(SamplingFlags::addressOfFlags()));
79 }
80 #endif
81
82 #if !ASSERT_DISABLED
83 #if USE(JSVALUE64)
84 void AssemblyHelpers::jitAssertIsInt32(GPRReg gpr)
85 {
86 #if CPU(X86_64)
87     Jump checkInt32 = branch64(BelowOrEqual, gpr, TrustedImm64(static_cast<uintptr_t>(0xFFFFFFFFu)));
88     abortWithReason(AHIsNotInt32);
89     checkInt32.link(this);
90 #else
91     UNUSED_PARAM(gpr);
92 #endif
93 }
94
95 void AssemblyHelpers::jitAssertIsJSInt32(GPRReg gpr)
96 {
97     Jump checkJSInt32 = branch64(AboveOrEqual, gpr, GPRInfo::tagTypeNumberRegister);
98     abortWithReason(AHIsNotJSInt32);
99     checkJSInt32.link(this);
100 }
101
102 void AssemblyHelpers::jitAssertIsJSNumber(GPRReg gpr)
103 {
104     Jump checkJSNumber = branchTest64(MacroAssembler::NonZero, gpr, GPRInfo::tagTypeNumberRegister);
105     abortWithReason(AHIsNotJSNumber);
106     checkJSNumber.link(this);
107 }
108
109 void AssemblyHelpers::jitAssertIsJSDouble(GPRReg gpr)
110 {
111     Jump checkJSInt32 = branch64(AboveOrEqual, gpr, GPRInfo::tagTypeNumberRegister);
112     Jump checkJSNumber = branchTest64(MacroAssembler::NonZero, gpr, GPRInfo::tagTypeNumberRegister);
113     checkJSInt32.link(this);
114     abortWithReason(AHIsNotJSDouble);
115     checkJSNumber.link(this);
116 }
117
118 void AssemblyHelpers::jitAssertIsCell(GPRReg gpr)
119 {
120     Jump checkCell = branchTest64(MacroAssembler::Zero, gpr, GPRInfo::tagMaskRegister);
121     abortWithReason(AHIsNotCell);
122     checkCell.link(this);
123 }
124
125 void AssemblyHelpers::jitAssertTagsInPlace()
126 {
127     Jump ok = branch64(Equal, GPRInfo::tagTypeNumberRegister, TrustedImm64(TagTypeNumber));
128     abortWithReason(AHTagTypeNumberNotInPlace);
129     breakpoint();
130     ok.link(this);
131     
132     ok = branch64(Equal, GPRInfo::tagMaskRegister, TrustedImm64(TagMask));
133     abortWithReason(AHTagMaskNotInPlace);
134     ok.link(this);
135 }
136 #elif USE(JSVALUE32_64)
137 void AssemblyHelpers::jitAssertIsInt32(GPRReg gpr)
138 {
139     UNUSED_PARAM(gpr);
140 }
141
142 void AssemblyHelpers::jitAssertIsJSInt32(GPRReg gpr)
143 {
144     Jump checkJSInt32 = branch32(Equal, gpr, TrustedImm32(JSValue::Int32Tag));
145     abortWithReason(AHIsNotJSInt32);
146     checkJSInt32.link(this);
147 }
148
149 void AssemblyHelpers::jitAssertIsJSNumber(GPRReg gpr)
150 {
151     Jump checkJSInt32 = branch32(Equal, gpr, TrustedImm32(JSValue::Int32Tag));
152     Jump checkJSDouble = branch32(Below, gpr, TrustedImm32(JSValue::LowestTag));
153     abortWithReason(AHIsNotJSNumber);
154     checkJSInt32.link(this);
155     checkJSDouble.link(this);
156 }
157
158 void AssemblyHelpers::jitAssertIsJSDouble(GPRReg gpr)
159 {
160     Jump checkJSDouble = branch32(Below, gpr, TrustedImm32(JSValue::LowestTag));
161     abortWithReason(AHIsNotJSDouble);
162     checkJSDouble.link(this);
163 }
164
165 void AssemblyHelpers::jitAssertIsCell(GPRReg gpr)
166 {
167     Jump checkCell = branch32(Equal, gpr, TrustedImm32(JSValue::CellTag));
168     abortWithReason(AHIsNotCell);
169     checkCell.link(this);
170 }
171
172 void AssemblyHelpers::jitAssertTagsInPlace()
173 {
174 }
175 #endif // USE(JSVALUE32_64)
176
177 void AssemblyHelpers::jitAssertHasValidCallFrame()
178 {
179     Jump checkCFR = branchTestPtr(Zero, GPRInfo::callFrameRegister, TrustedImm32(7));
180     abortWithReason(AHCallFrameMisaligned);
181     checkCFR.link(this);
182 }
183
184 void AssemblyHelpers::jitAssertIsNull(GPRReg gpr)
185 {
186     Jump checkNull = branchTestPtr(Zero, gpr);
187     abortWithReason(AHIsNotNull);
188     checkNull.link(this);
189 }
190
191 void AssemblyHelpers::jitAssertArgumentCountSane()
192 {
193     Jump ok = branch32(Below, payloadFor(JSStack::ArgumentCount), TrustedImm32(10000000));
194     abortWithReason(AHInsaneArgumentCount);
195     ok.link(this);
196 }
197 #endif // !ASSERT_DISABLED
198
199 void AssemblyHelpers::callExceptionFuzz()
200 {
201     if (!Options::enableExceptionFuzz())
202         return;
203
204     ASSERT(stackAlignmentBytes() >= sizeof(void*) * 2);
205     subPtr(TrustedImm32(stackAlignmentBytes()), stackPointerRegister);
206     poke(GPRInfo::returnValueGPR, 0);
207     poke(GPRInfo::returnValueGPR2, 1);
208     move(TrustedImmPtr(bitwise_cast<void*>(operationExceptionFuzz)), GPRInfo::nonPreservedNonReturnGPR);
209     call(GPRInfo::nonPreservedNonReturnGPR);
210     peek(GPRInfo::returnValueGPR, 0);
211     peek(GPRInfo::returnValueGPR2, 1);
212     addPtr(TrustedImm32(stackAlignmentBytes()), stackPointerRegister);
213 }
214
215 AssemblyHelpers::Jump AssemblyHelpers::emitExceptionCheck(ExceptionCheckKind kind, ExceptionJumpWidth width)
216 {
217     callExceptionFuzz();
218
219     if (width == FarJumpWidth)
220         kind = (kind == NormalExceptionCheck ? InvertedExceptionCheck : NormalExceptionCheck);
221     
222     Jump result;
223 #if USE(JSVALUE64)
224     result = branchTest64(kind == NormalExceptionCheck ? NonZero : Zero, AbsoluteAddress(vm()->addressOfException()));
225 #elif USE(JSVALUE32_64)
226     result = branch32(kind == NormalExceptionCheck ? NotEqual : Equal, AbsoluteAddress(reinterpret_cast<char*>(vm()->addressOfException()) + OBJECT_OFFSETOF(JSValue, u.asBits.tag)), TrustedImm32(JSValue::EmptyValueTag));
227 #endif
228     
229     if (width == NormalJumpWidth)
230         return result;
231     
232     PatchableJump realJump = patchableJump();
233     result.link(this);
234     
235     return realJump.m_jump;
236 }
237
238 void AssemblyHelpers::emitStoreStructureWithTypeInfo(AssemblyHelpers& jit, TrustedImmPtr structure, RegisterID dest)
239 {
240     const Structure* structurePtr = static_cast<const Structure*>(structure.m_value);
241 #if USE(JSVALUE64)
242     jit.store64(TrustedImm64(structurePtr->idBlob()), MacroAssembler::Address(dest, JSCell::structureIDOffset()));
243     if (!ASSERT_DISABLED) {
244         Jump correctStructure = jit.branch32(Equal, MacroAssembler::Address(dest, JSCell::structureIDOffset()), TrustedImm32(structurePtr->id()));
245         jit.abortWithReason(AHStructureIDIsValid);
246         correctStructure.link(&jit);
247
248         Jump correctIndexingType = jit.branch8(Equal, MacroAssembler::Address(dest, JSCell::indexingTypeOffset()), TrustedImm32(structurePtr->indexingType()));
249         jit.abortWithReason(AHIndexingTypeIsValid);
250         correctIndexingType.link(&jit);
251
252         Jump correctType = jit.branch8(Equal, MacroAssembler::Address(dest, JSCell::typeInfoTypeOffset()), TrustedImm32(structurePtr->typeInfo().type()));
253         jit.abortWithReason(AHTypeInfoIsValid);
254         correctType.link(&jit);
255
256         Jump correctFlags = jit.branch8(Equal, MacroAssembler::Address(dest, JSCell::typeInfoFlagsOffset()), TrustedImm32(structurePtr->typeInfo().inlineTypeFlags()));
257         jit.abortWithReason(AHTypeInfoInlineTypeFlagsAreValid);
258         correctFlags.link(&jit);
259     }
260 #else
261     // Do a 32-bit wide store to initialize the cell's fields.
262     jit.store32(TrustedImm32(structurePtr->objectInitializationBlob()), MacroAssembler::Address(dest, JSCell::indexingTypeOffset()));
263     jit.storePtr(structure, MacroAssembler::Address(dest, JSCell::structureIDOffset()));
264 #endif
265 }
266
267 } // namespace JSC
268
269 #endif // ENABLE(JIT)
270