aboutsummaryrefslogtreecommitdiff
path: root/src/jdk/nashorn/internal/runtime/SpillProperty.java
blob: 7b42b2bfa3003418f0d679ed50eb01959c1e49cb (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
/*
 * Copyright (c) 2010-2014, Oracle and/or its affiliates. All rights reserved.
 * 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;

import static jdk.nashorn.internal.codegen.ObjectClassGenerator.OBJECT_FIELDS_ONLY;
import static jdk.nashorn.internal.lookup.Lookup.MH;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;

/**
 * Spill property
 */
public class SpillProperty extends AccessorProperty {
    private static final long serialVersionUID = 3028496245198669460L;

    private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();

    private static final MethodHandle PARRAY_GETTER = MH.asType(MH.getter(LOOKUP, ScriptObject.class, "primitiveSpill",  long[].class), MH.type(long[].class, Object.class));
    private static final MethodHandle OARRAY_GETTER = MH.asType(MH.getter(LOOKUP, ScriptObject.class, "objectSpill",  Object[].class), MH.type(Object[].class, Object.class));

    private static final MethodHandle OBJECT_GETTER    = MH.filterArguments(MH.arrayElementGetter(Object[].class), 0, OARRAY_GETTER);
    private static final MethodHandle PRIMITIVE_GETTER = MH.filterArguments(MH.arrayElementGetter(long[].class), 0, PARRAY_GETTER);
    private static final MethodHandle OBJECT_SETTER    = MH.filterArguments(MH.arrayElementSetter(Object[].class), 0, OARRAY_GETTER);
    private static final MethodHandle PRIMITIVE_SETTER = MH.filterArguments(MH.arrayElementSetter(long[].class), 0, PARRAY_GETTER);

    private static class Accessors {
        private MethodHandle objectGetter;
        private MethodHandle objectSetter;
        private MethodHandle primitiveGetter;
        private MethodHandle primitiveSetter;

        private final int slot;
        private final MethodHandle ensureSpillSize;

        private static Accessors ACCESSOR_CACHE[] = new Accessors[512];

        //private static final Map<Integer, Reference<Accessors>> ACCESSOR_CACHE = Collections.synchronizedMap(new WeakHashMap<Integer, Reference<Accessors>>());

        Accessors(final int slot) {
            assert slot >= 0;
            this.slot = slot;
            this.ensureSpillSize = MH.asType(MH.insertArguments(ScriptObject.ENSURE_SPILL_SIZE, 1, slot), MH.type(Object.class, Object.class));
        }

        private static void ensure(final int slot) {
            int len = ACCESSOR_CACHE.length;
            if (slot >= len) {
                do {
                    len *= 2;
                } while (slot >= len);
                final Accessors newCache[] = new Accessors[len];
                System.arraycopy(ACCESSOR_CACHE, 0, newCache, 0, ACCESSOR_CACHE.length);
                ACCESSOR_CACHE = newCache;
            }
        }

        static MethodHandle getCached(final int slot, final boolean isPrimitive, final boolean isGetter) {
            //Reference<Accessors> ref = ACCESSOR_CACHE.get(slot);
            ensure(slot);
            Accessors acc = ACCESSOR_CACHE[slot];
            if (acc == null) {
                acc = new Accessors(slot);
                ACCESSOR_CACHE[slot] = acc;
            }

            return acc.getOrCreate(isPrimitive, isGetter);
        }

        private static MethodHandle primordial(final boolean isPrimitive, final boolean isGetter) {
            if (isPrimitive) {
                return isGetter ? PRIMITIVE_GETTER : PRIMITIVE_SETTER;
            }
            return isGetter ? OBJECT_GETTER : OBJECT_SETTER;
        }

        MethodHandle getOrCreate(final boolean isPrimitive, final boolean isGetter) {
            MethodHandle accessor;

            accessor = getInner(isPrimitive, isGetter);
            if (accessor != null) {
                return accessor;
            }

            accessor = primordial(isPrimitive, isGetter);
            accessor = MH.insertArguments(accessor, 1, slot);
            if (!isGetter) {
                accessor = MH.filterArguments(accessor, 0, ensureSpillSize);
            }
            setInner(isPrimitive, isGetter, accessor);

            return accessor;
        }

        void setInner(final boolean isPrimitive, final boolean isGetter, final MethodHandle mh) {
            if (isPrimitive) {
                if (isGetter) {
                    primitiveGetter = mh;
                } else {
                    primitiveSetter = mh;
                }
            } else {
                if (isGetter) {
                    objectGetter = mh;
                } else {
                    objectSetter = mh;
                }
            }
        }

        MethodHandle getInner(final boolean isPrimitive, final boolean isGetter) {
            if (isPrimitive) {
                return isGetter ? primitiveGetter : primitiveSetter;
            }
            return isGetter ? objectGetter : objectSetter;
        }
    }

    private static MethodHandle primitiveGetter(final int slot) {
        return OBJECT_FIELDS_ONLY ? null : Accessors.getCached(slot, true, true);
    }
    private static MethodHandle primitiveSetter(final int slot) {
        return OBJECT_FIELDS_ONLY ? null : Accessors.getCached(slot, true, false);
    }
    private static MethodHandle objectGetter(final int slot) {
        return Accessors.getCached(slot, false, true);
    }
    private static MethodHandle objectSetter(final int slot) {
        return Accessors.getCached(slot, false, false);
    }

    /**
     * Constructor for spill properties. Array getters and setters will be created on demand.
     *
     * @param key    the property key
     * @param flags  the property flags
     * @param slot   spill slot
     */
    public SpillProperty(final String key, final int flags, final int slot) {
        super(key, flags, slot, primitiveGetter(slot), primitiveSetter(slot), objectGetter(slot), objectSetter(slot));
        assert !OBJECT_FIELDS_ONLY || getLocalType() == Object.class;
    }

    SpillProperty(final String key, final int flags, final int slot, final Class<?> initialType) {
        this(key, flags, slot);
        setType(OBJECT_FIELDS_ONLY ? Object.class : initialType);
    }

    SpillProperty(final String key, final int flags, final int slot, final ScriptObject owner, final Object initialValue) {
        this(key, flags, slot);
        setInitialValue(owner, initialValue);
    }

    /**
     * Copy constructor
     * @param property other property
     */
    protected SpillProperty(final SpillProperty property) {
        super(property);
    }

    /**
     * Copy constructor
     * @param newType new type
     * @param property other property
     */
    protected SpillProperty(final SpillProperty property, final Class<?> newType) {
        super(property, newType);
    }

    @Override
    public Property copy() {
        return new SpillProperty(this);
    }

    @Override
    public Property copy(final Class<?> newType) {
        return new SpillProperty(this, newType);
    }

    @Override
    public boolean isSpill() {
        return true;
    }

    @Override
    void initMethodHandles(final Class<?> structure) {
        final int slot  = getSlot();
        primitiveGetter = primitiveGetter(slot);
        primitiveSetter = primitiveSetter(slot);
        objectGetter    = objectGetter(slot);
        objectSetter    = objectSetter(slot);
    }
}