Mon, 04 Feb 2013 16:20:05 +0100
8007215: Varargs broken for the case of passing more than the arg limit arguments.
Reviewed-by: jlaskey, attila
jlaskey@3 | 1 | /* |
jlaskey@7 | 2 | * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
jlaskey@3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jlaskey@3 | 4 | * |
jlaskey@3 | 5 | * This code is free software; you can redistribute it and/or modify it |
jlaskey@3 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jlaskey@3 | 7 | * published by the Free Software Foundation. Oracle designates this |
jlaskey@3 | 8 | * particular file as subject to the "Classpath" exception as provided |
jlaskey@3 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
jlaskey@3 | 10 | * |
jlaskey@3 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jlaskey@3 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jlaskey@3 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jlaskey@3 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
jlaskey@3 | 15 | * accompanied this code). |
jlaskey@3 | 16 | * |
jlaskey@3 | 17 | * You should have received a copy of the GNU General Public License version |
jlaskey@3 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
jlaskey@3 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jlaskey@3 | 20 | * |
jlaskey@3 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jlaskey@3 | 22 | * or visit www.oracle.com if you need additional information or have any |
jlaskey@3 | 23 | * questions. |
jlaskey@3 | 24 | */ |
jlaskey@3 | 25 | |
jlaskey@3 | 26 | package jdk.nashorn.internal.codegen; |
jlaskey@3 | 27 | |
jlaskey@3 | 28 | import java.util.Arrays; |
jlaskey@3 | 29 | import java.util.EnumSet; |
jlaskey@3 | 30 | import jdk.nashorn.internal.codegen.types.Type; |
jlaskey@3 | 31 | import jdk.nashorn.internal.ir.Symbol; |
jlaskey@3 | 32 | import jdk.nashorn.internal.runtime.ScriptObject; |
jlaskey@3 | 33 | |
jlaskey@3 | 34 | /** |
jlaskey@3 | 35 | * A scope call or get operation that can be shared by several callsites. This generates a static |
jlaskey@3 | 36 | * method that wraps the invokedynamic instructions to get or call scope variables. |
jlaskey@3 | 37 | * The rationale for this is that initial linking of invokedynamic callsites is expensive, |
jlaskey@3 | 38 | * so by sharing them we can reduce startup overhead and allow very large scripts to run that otherwise wouldn't. |
jlaskey@3 | 39 | * |
jlaskey@3 | 40 | * <p>Static methods generated by this class expect two parameters in addition to the parameters of the |
jlaskey@3 | 41 | * function call: The current scope object and the depth of the target scope relative to the scope argument |
jlaskey@3 | 42 | * for when this is known at compile-time (fast-scope access).</p> |
jlaskey@3 | 43 | * |
jlaskey@3 | 44 | * <p>The second argument may be -1 for non-fast-scope symbols, in which case the scope chain is checked |
jlaskey@3 | 45 | * for each call. This may cause callsite invalidation when the shared method is used from different |
jlaskey@3 | 46 | * scopes, but such sharing of non-fast scope calls may still be necessary for very large scripts.</p> |
jlaskey@3 | 47 | * |
jlaskey@3 | 48 | * <p>Scope calls must not be shared between normal callsites and callsites contained in a <tt>with</tt> |
jlaskey@3 | 49 | * statement as this condition is not handled by current guards and will cause a runtime error.</p> |
jlaskey@3 | 50 | */ |
jlaskey@3 | 51 | public class SharedScopeCall { |
jlaskey@3 | 52 | |
jlaskey@3 | 53 | /** Threshold for using shared scope calls with fast scope access. */ |
jlaskey@3 | 54 | public static final int FAST_SCOPE_CALL_THRESHOLD = 4; |
jlaskey@3 | 55 | /** Threshold for using shared scope calls with slow scope access. */ |
jlaskey@3 | 56 | public static final int SLOW_SCOPE_CALL_THRESHOLD = 500; |
jlaskey@3 | 57 | /** Threshold for using shared scope gets with fast scope access. */ |
jlaskey@3 | 58 | public static final int FAST_SCOPE_GET_THRESHOLD = 200; |
jlaskey@3 | 59 | |
jlaskey@3 | 60 | final Type valueType; |
jlaskey@3 | 61 | final Symbol symbol; |
jlaskey@3 | 62 | final Type returnType; |
jlaskey@3 | 63 | final Type[] paramTypes; |
jlaskey@3 | 64 | final int flags; |
jlaskey@3 | 65 | final boolean isCall; |
jlaskey@3 | 66 | private CompileUnit compileUnit; |
jlaskey@3 | 67 | private String methodName; |
jlaskey@3 | 68 | private String staticSignature; |
jlaskey@3 | 69 | |
jlaskey@3 | 70 | /** |
jlaskey@3 | 71 | * Constructor. |
jlaskey@3 | 72 | * |
jlaskey@3 | 73 | * @param symbol the symbol |
jlaskey@3 | 74 | * @param valueType the type of the value |
jlaskey@3 | 75 | * @param returnType the return type |
jlaskey@3 | 76 | * @param paramTypes the function parameter types |
jlaskey@3 | 77 | * @param flags the callsite flags |
jlaskey@3 | 78 | */ |
jlaskey@3 | 79 | SharedScopeCall(final Symbol symbol, final Type valueType, final Type returnType, final Type[] paramTypes, final int flags) { |
jlaskey@3 | 80 | this.symbol = symbol; |
jlaskey@3 | 81 | this.valueType = valueType; |
jlaskey@3 | 82 | this.returnType = returnType; |
jlaskey@3 | 83 | this.paramTypes = paramTypes; |
jlaskey@3 | 84 | this.flags = flags; |
jlaskey@3 | 85 | // If paramTypes is not null this is a call, otherwise it's just a get. |
jlaskey@3 | 86 | this.isCall = paramTypes != null; |
jlaskey@3 | 87 | } |
jlaskey@3 | 88 | |
jlaskey@3 | 89 | @Override |
jlaskey@3 | 90 | public int hashCode() { |
jlaskey@3 | 91 | return symbol.hashCode() ^ returnType.hashCode() ^ Arrays.hashCode(paramTypes) ^ flags; |
jlaskey@3 | 92 | } |
jlaskey@3 | 93 | |
jlaskey@3 | 94 | @Override |
jlaskey@3 | 95 | public boolean equals(final Object obj) { |
jlaskey@3 | 96 | if (obj instanceof SharedScopeCall) { |
jlaskey@3 | 97 | final SharedScopeCall c = (SharedScopeCall) obj; |
jlaskey@3 | 98 | return symbol.equals(c.symbol) |
jlaskey@3 | 99 | && flags == c.flags |
jlaskey@3 | 100 | && returnType.equals(c.returnType) |
jlaskey@3 | 101 | && Arrays.equals(paramTypes, c.paramTypes); |
jlaskey@3 | 102 | } |
jlaskey@3 | 103 | return false; |
jlaskey@3 | 104 | } |
jlaskey@3 | 105 | |
jlaskey@3 | 106 | /** |
jlaskey@3 | 107 | * Set the compile unit and method name. |
jlaskey@3 | 108 | * @param compileUnit the compile unit |
jlaskey@3 | 109 | * @param compiler the compiler to generate a unique method name |
jlaskey@3 | 110 | */ |
jlaskey@3 | 111 | protected void setClassAndName(final CompileUnit compileUnit, final Compiler compiler) { |
jlaskey@3 | 112 | this.compileUnit = compileUnit; |
jlaskey@3 | 113 | this.methodName = compiler.uniqueName("scopeCall"); |
jlaskey@3 | 114 | } |
jlaskey@3 | 115 | |
jlaskey@3 | 116 | /** |
jlaskey@3 | 117 | * Generate the invoke instruction for this shared scope call. |
jlaskey@3 | 118 | * @param method the method emitter |
jlaskey@3 | 119 | */ |
jlaskey@3 | 120 | public void generateInvoke(final MethodEmitter method) { |
jlaskey@3 | 121 | method.invokeStatic(compileUnit.getUnitClassName(), methodName, getStaticSignature()); |
jlaskey@3 | 122 | } |
jlaskey@3 | 123 | |
jlaskey@3 | 124 | /** |
jlaskey@3 | 125 | * Generate the method that implements the scope get or call. |
jlaskey@3 | 126 | */ |
jlaskey@3 | 127 | protected void generateScopeCall() { |
jlaskey@3 | 128 | final ClassEmitter classEmitter = compileUnit.getClassEmitter(); |
jlaskey@3 | 129 | final EnumSet<ClassEmitter.Flag> methodFlags = EnumSet.of(ClassEmitter.Flag.STATIC); |
jlaskey@3 | 130 | |
jlaskey@3 | 131 | // This method expects two fixed parameters in addition to any parameters that may be |
jlaskey@3 | 132 | // passed on to the function: A ScriptObject representing the caller's current scope object, |
jlaskey@3 | 133 | // and an int specifying the distance to the target scope containing the symbol we want to |
jlaskey@3 | 134 | // access, or -1 if this is not known at compile time (e.g. because of a "with" or "eval"). |
jlaskey@3 | 135 | |
jlaskey@3 | 136 | final MethodEmitter method = classEmitter.method(methodFlags, methodName, getStaticSignature()); |
jlaskey@3 | 137 | method.begin(); |
jlaskey@3 | 138 | |
jlaskey@3 | 139 | // Load correct scope by calling getProto() on the scope argument as often as specified |
jlaskey@3 | 140 | // by the second argument. |
jlaskey@3 | 141 | final MethodEmitter.Label parentLoopStart = new MethodEmitter.Label("parent_loop_start"); |
jlaskey@3 | 142 | final MethodEmitter.Label parentLoopDone = new MethodEmitter.Label("parent_loop_done"); |
jlaskey@3 | 143 | method.load(Type.OBJECT, 0); |
jlaskey@3 | 144 | method.label(parentLoopStart); |
jlaskey@3 | 145 | method.load(Type.INT, 1); |
jlaskey@3 | 146 | method.iinc(1, -1); |
jlaskey@3 | 147 | method.ifle(parentLoopDone); |
jlaskey@3 | 148 | method.invoke(ScriptObject.GET_PROTO); |
jlaskey@3 | 149 | method._goto(parentLoopStart); |
jlaskey@3 | 150 | method.label(parentLoopDone); |
jlaskey@3 | 151 | |
jlaskey@3 | 152 | method.dynamicGet(valueType, symbol.getName(), flags, isCall); |
jlaskey@3 | 153 | |
jlaskey@3 | 154 | // If this is a get we're done, otherwise call the value as function. |
jlaskey@3 | 155 | if (isCall) { |
jlaskey@3 | 156 | method.convert(Type.OBJECT); |
jlaskey@3 | 157 | // ScriptFunction will see CALLSITE_SCOPE and will bind scope accordingly. |
jlaskey@3 | 158 | method.loadNull(); |
jlaskey@3 | 159 | int slot = 2; |
jlaskey@3 | 160 | for (final Type type : paramTypes) { |
jlaskey@3 | 161 | method.load(type, slot++); |
lagergren@57 | 162 | if (type == Type.NUMBER || type == Type.LONG) { |
lagergren@57 | 163 | slot++; |
lagergren@57 | 164 | } |
jlaskey@3 | 165 | } |
lagergren@66 | 166 | method.dynamicCall(returnType, 2 + paramTypes.length, flags); |
jlaskey@3 | 167 | } |
jlaskey@3 | 168 | |
jlaskey@3 | 169 | method._return(returnType); |
jlaskey@3 | 170 | method.end(); |
jlaskey@3 | 171 | } |
jlaskey@3 | 172 | |
jlaskey@3 | 173 | private String getStaticSignature() { |
jlaskey@3 | 174 | if (staticSignature == null) { |
jlaskey@3 | 175 | if (paramTypes == null) { |
jlaskey@3 | 176 | staticSignature = Type.getMethodDescriptor(returnType, Type.typeFor(ScriptObject.class), Type.INT); |
jlaskey@3 | 177 | } else { |
jlaskey@3 | 178 | final Type[] params = new Type[paramTypes.length + 2]; |
jlaskey@3 | 179 | params[0] = Type.typeFor(ScriptObject.class); |
jlaskey@3 | 180 | params[1] = Type.INT; |
jlaskey@3 | 181 | int i = 2; |
jlaskey@3 | 182 | for (Type type : paramTypes) { |
jlaskey@3 | 183 | if (type.isObject()) { |
jlaskey@3 | 184 | type = Type.OBJECT; |
jlaskey@3 | 185 | } |
jlaskey@3 | 186 | params[i++] = type; |
jlaskey@3 | 187 | } |
jlaskey@3 | 188 | staticSignature = Type.getMethodDescriptor(returnType, params); |
jlaskey@3 | 189 | } |
jlaskey@3 | 190 | } |
jlaskey@3 | 191 | return staticSignature; |
jlaskey@3 | 192 | } |
jlaskey@3 | 193 | |
jlaskey@3 | 194 | } |