1/*
2 * Copyright (C) 2015-2016 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 *
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 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30
31#if ENABLE(JIT)
32
33#include "CCallHelpers.h"
34#include "CallFrame.h"
35#include "CodeBlock.h"
36#include "IntrinsicGetterAccessCase.h"
37#include "JSArrayBufferView.h"
38#include "JSCJSValueInlines.h"
39#include "JSCellInlines.h"
40#include "PolymorphicAccess.h"
41#include "StructureStubInfo.h"
42
43namespace JSC {
44
45typedef CCallHelpers::TrustedImm32 TrustedImm32;
46typedef CCallHelpers::Imm32 Imm32;
47typedef CCallHelpers::TrustedImmPtr TrustedImmPtr;
48typedef CCallHelpers::ImmPtr ImmPtr;
49typedef CCallHelpers::TrustedImm64 TrustedImm64;
50typedef CCallHelpers::Imm64 Imm64;
51
52bool IntrinsicGetterAccessCase::canEmitIntrinsicGetter(JSFunction* getter, Structure* structure)
53{
54
55 switch (getter->intrinsic()) {
56 case TypedArrayByteOffsetIntrinsic:
57 case TypedArrayByteLengthIntrinsic:
58 case TypedArrayLengthIntrinsic: {
59 TypedArrayType type = structure->classInfo()->typedArrayStorageType;
60
61 if (!isTypedView(type))
62 return false;
63
64 return true;
65 }
66 case UnderscoreProtoIntrinsic: {
67 auto getPrototypeMethod = structure->classInfo()->methodTable.getPrototype;
68 MethodTable::GetPrototypeFunctionPtr defaultGetPrototype = JSObject::getPrototype;
69 return getPrototypeMethod == defaultGetPrototype;
70 }
71 default:
72 return false;
73 }
74 RELEASE_ASSERT_NOT_REACHED();
75}
76
77void IntrinsicGetterAccessCase::emitIntrinsicGetter(AccessGenerationState& state)
78{
79 CCallHelpers& jit = *state.jit;
80 JSValueRegs valueRegs = state.valueRegs;
81 GPRReg baseGPR = state.baseGPR;
82 GPRReg valueGPR = valueRegs.payloadGPR();
83
84 switch (intrinsic()) {
85 case TypedArrayLengthIntrinsic: {
86 jit.load32(MacroAssembler::Address(state.baseGPR, JSArrayBufferView::offsetOfLength()), valueGPR);
87 jit.boxInt32(valueGPR, valueRegs);
88 state.succeed();
89 return;
90 }
91
92 case TypedArrayByteLengthIntrinsic: {
93 TypedArrayType type = structure()->classInfo()->typedArrayStorageType;
94
95 jit.load32(MacroAssembler::Address(state.baseGPR, JSArrayBufferView::offsetOfLength()), valueGPR);
96
97 if (elementSize(type) > 1) {
98 // We can use a bitshift here since we TypedArrays cannot have byteLength that overflows an int32.
99 jit.lshift32(valueGPR, Imm32(logElementSize(type)), valueGPR);
100 }
101
102 jit.boxInt32(valueGPR, valueRegs);
103 state.succeed();
104 return;
105 }
106
107 case TypedArrayByteOffsetIntrinsic: {
108 GPRReg scratchGPR = state.scratchGPR;
109
110 CCallHelpers::Jump emptyByteOffset = jit.branch32(
111 MacroAssembler::NotEqual,
112 MacroAssembler::Address(baseGPR, JSArrayBufferView::offsetOfMode()),
113 TrustedImm32(WastefulTypedArray));
114
115 jit.loadPtr(MacroAssembler::Address(baseGPR, JSObject::butterflyOffset()), scratchGPR);
116 jit.loadPtr(MacroAssembler::Address(baseGPR, JSArrayBufferView::offsetOfVector()), valueGPR);
117#if CPU(ARM64E)
118 jit.removeArrayPtrTag(valueGPR);
119#endif
120 jit.loadPtr(MacroAssembler::Address(scratchGPR, Butterfly::offsetOfArrayBuffer()), scratchGPR);
121 jit.loadPtr(MacroAssembler::Address(scratchGPR, ArrayBuffer::offsetOfData()), scratchGPR);
122#if CPU(ARM64E)
123 jit.removeArrayPtrTag(scratchGPR);
124#endif
125 jit.subPtr(scratchGPR, valueGPR);
126
127 CCallHelpers::Jump done = jit.jump();
128
129 emptyByteOffset.link(&jit);
130 jit.move(TrustedImmPtr(nullptr), valueGPR);
131
132 done.link(&jit);
133
134 jit.boxInt32(valueGPR, valueRegs);
135 state.succeed();
136 return;
137 }
138
139 case UnderscoreProtoIntrinsic: {
140 if (structure()->hasPolyProto())
141 jit.loadValue(CCallHelpers::Address(baseGPR, offsetRelativeToBase(knownPolyProtoOffset)), valueRegs);
142 else
143 jit.moveValue(structure()->storedPrototype(), valueRegs);
144 state.succeed();
145 return;
146 }
147
148 default:
149 break;
150 }
151 RELEASE_ASSERT_NOT_REACHED();
152}
153
154} // namespace JSC
155
156#endif // ENABLE(JIT)
157