aboutsummaryrefslogtreecommitdiff
path: root/src/jdk/nashorn/internal/runtime/arrays/ContinuousArrayData.java
blob: 2fdd184e45c0c54ecf63bd89dd8395f17a41c403 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package jdk.nashorn.internal.runtime.arrays;

import static jdk.nashorn.internal.codegen.CompilerConstants.staticCall;
import static jdk.nashorn.internal.lookup.Lookup.MH;
import static jdk.nashorn.internal.runtime.JSType.getAccessorTypeIndex;
import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.INVALID_PROGRAM_POINT;
import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.isValid;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.invoke.SwitchPoint;
import jdk.internal.dynalink.CallSiteDescriptor;
import jdk.internal.dynalink.linker.GuardedInvocation;
import jdk.internal.dynalink.linker.LinkRequest;
import jdk.nashorn.internal.codegen.types.Type;
import jdk.nashorn.internal.lookup.Lookup;
import jdk.nashorn.internal.runtime.ScriptObject;
import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
import jdk.nashorn.internal.runtime.logging.Logger;

/**
 * Interface implemented by all arrays that are directly accessible as underlying
 * native arrays
 */
@Logger(name="arrays")
public abstract class ContinuousArrayData extends ArrayData {
    /**
     * Constructor
     * @param length length (elementLength)
     */
    protected ContinuousArrayData(final long length) {
        super(length);
    }

    /**
     * Check if we can put one more element at the end of this continous
     * array without reallocating, or if we are overwriting an already
     * allocated element
     *
     * @param index index to check
     * @return true if we don't need to do any array reallocation to fit an element at index
     */
    public final boolean hasRoomFor(final int index) {
        return has(index) || (index == length() && ensure(index) == this);
    }

    /**
     * Check if an arraydata is empty
     * @return true if empty
     */
    public boolean isEmpty() {
        return length() == 0L;
    }

    /**
     * Return element getter for a certain type at a certain program point
     * @param returnType   return type
     * @param programPoint program point
     * @return element getter or null if not supported (used to implement slow linkage instead
     *   as fast isn't possible)
     */
    public abstract MethodHandle getElementGetter(final Class<?> returnType, final int programPoint);

    /**
     * Return element getter for a certain type at a certain program point
     * @param elementType element type
     * @return element setter or null if not supported (used to implement slow linkage instead
     *   as fast isn't possible)
     */
    public abstract MethodHandle getElementSetter(final Class<?> elementType);

    /**
     * Version of has that throws a class cast exception if element does not exist
     * used for relinking
     *
     * @param index index to check - currently only int indexes
     * @return index
     */
    protected final int throwHas(final int index) {
        if (!has(index)) {
            throw new ClassCastException();
        }
        return index;
    }

    @Override
    public abstract ContinuousArrayData copy();

    /**
     * Returns the type used to store an element in this array
     * @return element type
     */
    public abstract Class<?> getElementType();

    @Override
    public Type getOptimisticType() {
        return Type.typeFor(getElementType());
    }

    /**
     * Returns the boxed type of the type used to store an element in this array
     * @return element type
     */
    public abstract Class<?> getBoxedElementType();

    /**
     * Get the widest element type of two arrays. This can be done faster in subclasses, but
     * this works for all ContinuousArrayDatas and for where more optimal checks haven't been
     * implemented.
     *
     * @param otherData another ContinuousArrayData
     * @return the widest boxed element type
     */
    public ContinuousArrayData widest(final ContinuousArrayData otherData) {
        final Class<?> elementType = getElementType();
        return Type.widest(elementType, otherData.getElementType()) == elementType ? this : otherData;
    }

    /**
     * Look up a continuous array element getter
     * @param get          getter, sometimes combined with a has check that throws CCE on failure for relink
     * @param returnType   return type
     * @param programPoint program point
     * @return array getter
     */
    protected final MethodHandle getContinuousElementGetter(final MethodHandle get, final Class<?> returnType, final int programPoint) {
        return getContinuousElementGetter(getClass(), get, returnType, programPoint);
    }

    /**
     * Look up a continuous array element setter
     * @param set          setter, sometimes combined with a has check that throws CCE on failure for relink
     * @param returnType   return type
     * @return array setter
     */
    protected final MethodHandle getContinuousElementSetter(final MethodHandle set, final Class<?> returnType) {
        return getContinuousElementSetter(getClass(), set, returnType);
    }

    /**
     * Return element getter for a {@link ContinuousArrayData}
     * @param clazz        clazz for exact type guard
     * @param getHas       has getter
     * @param returnType   return type
     * @param programPoint program point
     * @return method handle for element setter
     */
    protected MethodHandle getContinuousElementGetter(final Class<? extends ContinuousArrayData> clazz, final MethodHandle getHas, final Class<?> returnType, final int programPoint) {
        final boolean isOptimistic = isValid(programPoint);
        final int     fti          = getAccessorTypeIndex(getHas.type().returnType());
        final int     ti           = getAccessorTypeIndex(returnType);
        MethodHandle  mh           = getHas;

        if (isOptimistic) {
            if (ti < fti) {
                mh = MH.insertArguments(ArrayData.THROW_UNWARRANTED.methodHandle(), 1, programPoint);
            }
        }
        mh = MH.asType(mh, mh.type().changeReturnType(returnType).changeParameterType(0, clazz));

        if (!isOptimistic) {
            //for example a & array[17];
            return Lookup.filterReturnType(mh, returnType);
        }
        return mh;
    }

    /**
     * Return element setter for a {@link ContinuousArrayData}
     * @param clazz        clazz for exact type guard
     * @param setHas       set has guard
     * @param elementType  element type
     * @return method handle for element setter
     */
    protected MethodHandle getContinuousElementSetter(final Class<? extends ContinuousArrayData> clazz, final MethodHandle setHas, final Class<?> elementType) {
        return MH.asType(setHas, setHas.type().changeParameterType(2, elementType).changeParameterType(0, clazz));
    }

    /** Fast access guard - it is impractical for JIT performance reasons to use only CCE asType as guard :-(, also we need
      the null case explicitly, which is the one that CCE doesn't handle */
    protected static final MethodHandle FAST_ACCESS_GUARD =
            MH.dropArguments(
                    staticCall(
                            MethodHandles.lookup(),
                            ContinuousArrayData.class,
                            "guard",
                            boolean.class,
                            Class.class,
                            ScriptObject.class).methodHandle(),
                    2,
                    int.class);

    @SuppressWarnings("unused")
    private static final boolean guard(final Class<? extends ContinuousArrayData> clazz, final ScriptObject sobj) {
        if (sobj != null && sobj.getArray().getClass() == clazz) {
            return true;
        }
        return false;
    }

    /**
     * Return a fast linked array getter, or null if we have to dispatch to super class
     * @param desc     descriptor
     * @param request  link request
     * @return invocation or null if needs to be sent to slow relink
     */
    @Override
    public GuardedInvocation findFastGetIndexMethod(final Class<? extends ArrayData> clazz, final CallSiteDescriptor desc, final LinkRequest request) {
        final MethodType callType   = desc.getMethodType();
        final Class<?>   indexType  = callType.parameterType(1);
        final Class<?>   returnType = callType.returnType();

        if (ContinuousArrayData.class.isAssignableFrom(clazz) && indexType == int.class) {
            final Object[] args  = request.getArguments();
            final int      index = (int)args[args.length - 1];

            if (has(index)) {
                final MethodHandle getArray     = ScriptObject.GET_ARRAY.methodHandle();
                final int          programPoint = NashornCallSiteDescriptor.isOptimistic(desc) ? NashornCallSiteDescriptor.getProgramPoint(desc) : INVALID_PROGRAM_POINT;
                MethodHandle       getElement   = getElementGetter(returnType, programPoint);
                if (getElement != null) {
                    getElement = MH.filterArguments(getElement, 0, MH.asType(getArray, getArray.type().changeReturnType(clazz)));
                    final MethodHandle guard = MH.insertArguments(FAST_ACCESS_GUARD, 0, clazz);
                    return new GuardedInvocation(getElement, guard, (SwitchPoint)null, ClassCastException.class);
                }
            }
        }

        return null;
    }

    /**
     * Return a fast linked array setter, or null if we have to dispatch to super class
     * @param desc     descriptor
     * @param request  link request
     * @return invocation or null if needs to be sent to slow relink
     */
    @Override
    public GuardedInvocation findFastSetIndexMethod(final Class<? extends ArrayData> clazz, final CallSiteDescriptor desc, final LinkRequest request) { // array, index, value
        final MethodType callType    = desc.getMethodType();
        final Class<?>   indexType   = callType.parameterType(1);
        final Class<?>   elementType = callType.parameterType(2);

        if (ContinuousArrayData.class.isAssignableFrom(clazz) && indexType == int.class) {
            final Object[]        args  = request.getArguments();
            final int             index = (int)args[args.length - 2];

            if (hasRoomFor(index)) {
                MethodHandle setElement = getElementSetter(elementType); //Z(continuousarraydata, int, int), return true if successful
                if (setElement != null) {
                    //else we are dealing with a wider type than supported by this callsite
                    MethodHandle getArray = ScriptObject.GET_ARRAY.methodHandle();
                    getArray   = MH.asType(getArray, getArray.type().changeReturnType(getClass()));
                    setElement = MH.filterArguments(setElement, 0, getArray);
                    final MethodHandle guard = MH.insertArguments(FAST_ACCESS_GUARD, 0, clazz);
                    return new GuardedInvocation(setElement, guard, (SwitchPoint)null, ClassCastException.class); //CCE if not a scriptObject anymore
                }
            }
        }

        return null;
    }

    /**
     * Specialization - fast push implementation
     * @param arg argument
     * @return new array length
     */
    public long fastPush(final int arg) {
        throw new ClassCastException(String.valueOf(getClass())); //type is wrong, relink
    }

    /**
     * Specialization - fast push implementation
     * @param arg argument
     * @return new array length
     */
    public long fastPush(final long arg) {
        throw new ClassCastException(String.valueOf(getClass())); //type is wrong, relink
    }

    /**
     * Specialization - fast push implementation
     * @param arg argument
     * @return new array length
     */
    public long fastPush(final double arg) {
        throw new ClassCastException(String.valueOf(getClass())); //type is wrong, relink
    }

    /**
     * Specialization - fast push implementation
     * @param arg argument
     * @return new array length
     */
    public long fastPush(final Object arg) {
        throw new ClassCastException(String.valueOf(getClass())); //type is wrong, relink
    }

    /**
     * Specialization - fast pop implementation
     * @return element value
     */
    public int fastPopInt() {
        throw new ClassCastException(String.valueOf(getClass())); //type is wrong, relink
    }

    /**
     * Specialization - fast pop implementation
     * @return element value
     */
    public long fastPopLong() {
        throw new ClassCastException(String.valueOf(getClass())); //type is wrong, relink
    }

    /**
     * Specialization - fast pop implementation
     * @return element value
     */
    public double fastPopDouble() {
       throw new ClassCastException(String.valueOf(getClass())); //type is wrong, relink
    }

    /**
     * Specialization - fast pop implementation
     * @return element value
     */
    public Object fastPopObject() {
        throw new ClassCastException(String.valueOf(getClass())); //type is wrong, relink
    }

    /**
     * Specialization - fast concat implementation
     * @param otherData data to concat
     * @return new arraydata
     */
    public ContinuousArrayData fastConcat(final ContinuousArrayData otherData) {
        throw new ClassCastException(String.valueOf(getClass()) + " != " + String.valueOf(otherData.getClass()));
    }
}