nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/ObjectCreator.java
author hannesw
Sat, 19 Sep 2015 16:04:28 +0200
changeset 32781 d8f34ffbbc7a
parent 30390 357f9a3f9394
child 34732 6605efbe8447
permissions -rw-r--r--
8135190: Method code too large in Babel browser.js script Reviewed-by: attila, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
16240
e1468b33e201 8008239: Unpublicized parts of the code generator package that were only package internal.
lagergren
parents: 16224
diff changeset
    26
package jdk.nashorn.internal.codegen;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
18852
604c1d681b6f 8017084: Use spill properties for large object literals
hannesw
parents: 17518
diff changeset
    28
import static jdk.nashorn.internal.codegen.CompilerConstants.SCOPE;
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
    29
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24719
diff changeset
    30
import java.util.List;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
    31
import jdk.nashorn.internal.codegen.types.Type;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
import jdk.nashorn.internal.runtime.PropertyMap;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
    33
import jdk.nashorn.internal.runtime.ScriptObject;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
 * Base class for object creation code generation.
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
    37
 * @param <T> value type
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
 */
32781
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
    39
public abstract class ObjectCreator<T> implements CodeGenerator.SplitLiteralCreator {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
    41
    /** List of keys & symbols to initiate in this ObjectCreator */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
    42
    final List<MapTuple<T>> tuples;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
    /** Code generator */
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
    45
    final CodeGenerator codegen;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
19472
9476460521b3 8023017: SUB missing for widest op == number for BinaryNode
lagergren
parents: 18852
diff changeset
    47
    /** Property map */
9476460521b3 8023017: SUB missing for widest op == number for BinaryNode
lagergren
parents: 18852
diff changeset
    48
    protected PropertyMap   propertyMap;
9476460521b3 8023017: SUB missing for widest op == number for BinaryNode
lagergren
parents: 18852
diff changeset
    49
9476460521b3 8023017: SUB missing for widest op == number for BinaryNode
lagergren
parents: 18852
diff changeset
    50
    private final boolean       isScope;
9476460521b3 8023017: SUB missing for widest op == number for BinaryNode
lagergren
parents: 18852
diff changeset
    51
    private final boolean       hasArguments;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
     * Constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
     *
16224
0c49ad4e3b55 8006222: Move slot from SpillProperty to Property
jlaskey
parents: 16216
diff changeset
    56
     * @param codegen      the code generator
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
    57
     * @param tuples       key,symbol,value (optional) tuples
16224
0c49ad4e3b55 8006222: Move slot from SpillProperty to Property
jlaskey
parents: 16216
diff changeset
    58
     * @param isScope      is this object scope
0c49ad4e3b55 8006222: Move slot from SpillProperty to Property
jlaskey
parents: 16216
diff changeset
    59
     * @param hasArguments does the created object have an "arguments" property
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
     */
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
    61
    ObjectCreator(final CodeGenerator codegen, final List<MapTuple<T>> tuples, final boolean isScope, final boolean hasArguments) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
        this.codegen       = codegen;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
    63
        this.tuples        = tuples;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
        this.isScope       = isScope;
16224
0c49ad4e3b55 8006222: Move slot from SpillProperty to Property
jlaskey
parents: 16216
diff changeset
    65
        this.hasArguments  = hasArguments;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
     * Generate code for making the object.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
     * @param method Script method.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
     */
32781
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
    72
    public void makeObject(final MethodEmitter method) {
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
    73
        createObject(method);
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
    74
        // We need to store the object in a temporary slot as populateRange expects to load the
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
    75
        // object from a slot (as it is also invoked within split methods). Note that this also
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
    76
        // helps optimistic continuations to handle the stack in case an optimistic assumption
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
    77
        // fails during initialization (see JDK-8079269).
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
    78
        final int objectSlot = method.getUsedSlotsWithLiveTemporaries();
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
    79
        final Type objectType = method.peekType();
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
    80
        method.storeTemp(objectType, objectSlot);
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
    81
        populateRange(method, objectType, objectSlot, 0, tuples.size());
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
    82
    }
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
    83
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
    84
    /**
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
    85
     * Generate code for creating and initializing the object.
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
    86
     * @param method the method emitter
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
    87
     */
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
    88
    protected abstract void createObject(final MethodEmitter method);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
    /**
18852
604c1d681b6f 8017084: Use spill properties for large object literals
hannesw
parents: 17518
diff changeset
    91
     * Construct the property map appropriate for the object.
604c1d681b6f 8017084: Use spill properties for large object literals
hannesw
parents: 17518
diff changeset
    92
     * @return the newly created property map
604c1d681b6f 8017084: Use spill properties for large object literals
hannesw
parents: 17518
diff changeset
    93
     */
604c1d681b6f 8017084: Use spill properties for large object literals
hannesw
parents: 17518
diff changeset
    94
    protected abstract PropertyMap makeMap();
604c1d681b6f 8017084: Use spill properties for large object literals
hannesw
parents: 17518
diff changeset
    95
604c1d681b6f 8017084: Use spill properties for large object literals
hannesw
parents: 17518
diff changeset
    96
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
     * Create a new MapCreator
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    98
     * @param clazz type of MapCreator
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    99
     * @return map creator instantiated by type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   100
     */
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   101
    protected MapCreator<?> newMapCreator(final Class<? extends ScriptObject> clazz) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   102
        return new MapCreator<>(clazz, tuples);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   103
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   104
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
    /**
18852
604c1d681b6f 8017084: Use spill properties for large object literals
hannesw
parents: 17518
diff changeset
   106
     * Loads the scope on the stack through the passed method emitter.
604c1d681b6f 8017084: Use spill properties for large object literals
hannesw
parents: 17518
diff changeset
   107
     * @param method the method emitter to use
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
     */
18852
604c1d681b6f 8017084: Use spill properties for large object literals
hannesw
parents: 17518
diff changeset
   109
    protected void loadScope(final MethodEmitter method) {
604c1d681b6f 8017084: Use spill properties for large object literals
hannesw
parents: 17518
diff changeset
   110
        method.loadCompilerConstant(SCOPE);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   111
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   112
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   113
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   114
     * Emit the correct map for the object.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
     * @param method method emitter
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   116
     * @return the method emitter
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
    protected MethodEmitter loadMap(final MethodEmitter method) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
        codegen.loadConstant(propertyMap);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
        return method;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   123
    PropertyMap getMap() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   124
        return propertyMap;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   125
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   126
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   128
     * Is this a scope object
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
     * @return true if scope
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
    protected boolean isScope() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
        return isScope;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
    /**
16224
0c49ad4e3b55 8006222: Move slot from SpillProperty to Property
jlaskey
parents: 16216
diff changeset
   136
     * Does the created object have an "arguments" property
0c49ad4e3b55 8006222: Move slot from SpillProperty to Property
jlaskey
parents: 16216
diff changeset
   137
     * @return true if has an "arguments" property
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
     */
16224
0c49ad4e3b55 8006222: Move slot from SpillProperty to Property
jlaskey
parents: 16216
diff changeset
   139
    protected boolean hasArguments() {
0c49ad4e3b55 8006222: Move slot from SpillProperty to Property
jlaskey
parents: 16216
diff changeset
   140
        return hasArguments;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
    }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   142
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   143
    /**
32781
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
   144
     * Get the class of objects created by this ObjectCreator
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
   145
     * @return class of created object
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
   146
     */
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
   147
    abstract protected Class<? extends ScriptObject> getAllocatorClass();
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
   148
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 30390
diff changeset
   149
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   150
     * Technique for loading an initial value. Defined by anonymous subclasses in code gen.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   151
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   152
     * @param value Value to load.
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   153
     * @param type the type of the value to load
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   154
     */
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   155
    protected abstract void loadValue(T value, Type type);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   156
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   157
    MethodEmitter loadTuple(final MethodEmitter method, final MapTuple<T> tuple, final boolean pack) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24738
diff changeset
   158
        loadValue(tuple.value, tuple.type);
29834
f678f348c947 8067215: Disable dual fields when not using optimistic types
hannesw
parents: 25865
diff changeset
   159
        if (pack && codegen.useDualFields() && tuple.isPrimitive()) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   160
            method.pack();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   161
        } else {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   162
            method.convert(Type.OBJECT);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   163
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   164
        return method;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   165
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   166
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   167
    MethodEmitter loadTuple(final MethodEmitter method, final MapTuple<T> tuple) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   168
        return loadTuple(method, tuple, true);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 19472
diff changeset
   169
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
}