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.runtime; |
|
27 |
|
28 import java.lang.invoke.MethodHandle; |
|
29 import java.lang.invoke.MethodType; |
|
30 import jdk.nashorn.internal.codegen.types.Type; |
|
31 import jdk.nashorn.internal.runtime.options.Options; |
|
32 |
|
33 class SpecializedMethodChooser { |
|
34 /** Should specialized function and specialized constructors for the builtin be used if available? */ |
|
35 private static final boolean DISABLE_SPECIALIZATION = Options.getBooleanProperty("nashorn.scriptfunction.specialization.disable"); |
|
36 |
|
37 static MethodHandle candidateWithLowestWeight(final MethodType descType, final MethodHandle initialCandidate, final MethodHandle[] specs) { |
|
38 if (DISABLE_SPECIALIZATION || specs == null) { |
|
39 return initialCandidate; |
|
40 } |
|
41 |
|
42 int minimumWeight = Integer.MAX_VALUE; |
|
43 MethodHandle candidate = initialCandidate; |
|
44 |
|
45 for (final MethodHandle spec : specs) { |
|
46 final MethodType specType = spec.type(); |
|
47 |
|
48 if (!typeCompatible(descType, specType)) { |
|
49 continue; |
|
50 } |
|
51 |
|
52 //return type is ok. we want a wider or equal one for our callsite. |
|
53 final int specWeight = weigh(specType); |
|
54 if (specWeight < minimumWeight) { |
|
55 candidate = spec; |
|
56 minimumWeight = specWeight; |
|
57 } |
|
58 } |
|
59 |
|
60 return candidate; |
|
61 } |
|
62 |
|
63 private static boolean typeCompatible(final MethodType desc, final MethodType spec) { |
|
64 //spec must fit in desc |
|
65 final Class<?>[] dparray = desc.parameterArray(); |
|
66 final Class<?>[] sparray = spec.parameterArray(); |
|
67 |
|
68 if (dparray.length != sparray.length) { |
|
69 return false; |
|
70 } |
|
71 |
|
72 for (int i = 0; i < dparray.length; i++) { |
|
73 final Type dp = Type.typeFor(dparray[i]); |
|
74 final Type sp = Type.typeFor(sparray[i]); |
|
75 |
|
76 if (dp.isBoolean()) { |
|
77 return false; //don't specialize on booleans, we have the "true" vs int 1 ambiguity in resolution |
|
78 } |
|
79 |
|
80 //specialization arguments must be at least as wide as dp, if not wider |
|
81 if (Type.widest(dp, sp) != sp) { |
|
82 //e.g. specialization takes double and callsite says "object". reject. |
|
83 //but if specialization says double and callsite says "int" or "long" or "double", that's fine |
|
84 return false; |
|
85 } |
|
86 } |
|
87 |
|
88 return true; // anything goes for return type, take the convenient one and it will be upcasted thru dynalink magic. |
|
89 } |
|
90 |
|
91 private static int weigh(final MethodType t) { |
|
92 int weight = Type.typeFor(t.returnType()).getWeight(); |
|
93 for (final Class<?> paramType : t.parameterArray()) { |
|
94 final int pweight = Type.typeFor(paramType).getWeight(); |
|
95 weight += pweight; |
|
96 } |
|
97 return weight; |
|
98 } |
|
99 } |
|