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