43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, 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.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
package org.graalvm.compiler.options;
|
|
24 |
|
|
25 |
import java.util.ArrayList;
|
|
26 |
import java.util.Formatter;
|
|
27 |
import java.util.List;
|
|
28 |
import java.util.ServiceLoader;
|
46344
|
29 |
|
|
30 |
import org.graalvm.util.CollectionsUtil;
|
|
31 |
import org.graalvm.util.EconomicMap;
|
|
32 |
import org.graalvm.util.MapCursor;
|
43972
|
33 |
|
|
34 |
/**
|
|
35 |
* This class contains methods for parsing Graal options and matching them against a set of
|
|
36 |
* {@link OptionDescriptors}. The {@link OptionDescriptors} are loaded via a {@link ServiceLoader}.
|
|
37 |
*/
|
|
38 |
public class OptionsParser {
|
|
39 |
|
46344
|
40 |
/**
|
|
41 |
* Gets an iterable composed of the {@link ServiceLoader}s to be used when looking for
|
|
42 |
* {@link OptionDescriptors} providers.
|
|
43 |
*/
|
|
44 |
public static Iterable<OptionDescriptors> getOptionsLoader() {
|
|
45 |
ServiceLoader<OptionDescriptors> graalLoader = ServiceLoader.load(OptionDescriptors.class, OptionDescriptors.class.getClassLoader());
|
|
46 |
boolean java8OrEarlier = System.getProperty("java.specification.version").compareTo("1.9") < 0;
|
|
47 |
if (java8OrEarlier) {
|
|
48 |
return graalLoader;
|
|
49 |
} else {
|
|
50 |
/*
|
|
51 |
* The Graal module (i.e., jdk.internal.vm.compiler) is loaded by the platform class
|
|
52 |
* loader on JDK 9. Other modules that extend Graal or are Graal dependencies (such as
|
|
53 |
* Truffle) are supplied via --module-path which means they are loaded by the app class
|
|
54 |
* loader. As such, we need to search the app class loader path as well.
|
|
55 |
*/
|
|
56 |
ServiceLoader<OptionDescriptors> truffleLoader = ServiceLoader.load(OptionDescriptors.class, ClassLoader.getSystemClassLoader());
|
|
57 |
return CollectionsUtil.concat(graalLoader, truffleLoader);
|
|
58 |
}
|
43972
|
59 |
}
|
|
60 |
|
|
61 |
/**
|
|
62 |
* Parses a map representing assignments of values to options.
|
|
63 |
*
|
|
64 |
* @param optionSettings option settings (i.e., assignments of values to options)
|
46344
|
65 |
* @param values the object in which to store the parsed values
|
|
66 |
* @param loader source of the available {@link OptionDescriptors}
|
43972
|
67 |
* @throws IllegalArgumentException if there's a problem parsing {@code option}
|
|
68 |
*/
|
46344
|
69 |
public static void parseOptions(EconomicMap<String, String> optionSettings, EconomicMap<OptionKey<?>, Object> values, Iterable<OptionDescriptors> loader) {
|
43972
|
70 |
if (optionSettings != null && !optionSettings.isEmpty()) {
|
46344
|
71 |
MapCursor<String, String> cursor = optionSettings.getEntries();
|
|
72 |
while (cursor.advance()) {
|
|
73 |
parseOption(cursor.getKey(), cursor.getValue(), values, loader);
|
43972
|
74 |
}
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
|
78 |
/**
|
46344
|
79 |
* Parses a given option setting string and adds the parsed key and value {@code dst}.
|
43972
|
80 |
*
|
|
81 |
* @param optionSetting a string matching the pattern {@code <name>=<value>}
|
|
82 |
*/
|
46344
|
83 |
public static void parseOptionSettingTo(String optionSetting, EconomicMap<String, String> dst) {
|
43972
|
84 |
int eqIndex = optionSetting.indexOf('=');
|
|
85 |
if (eqIndex == -1) {
|
|
86 |
throw new InternalError("Option setting has does not match the pattern <name>=<value>: " + optionSetting);
|
|
87 |
}
|
|
88 |
dst.put(optionSetting.substring(0, eqIndex), optionSetting.substring(eqIndex + 1));
|
|
89 |
}
|
|
90 |
|
|
91 |
/**
|
|
92 |
* Looks up an {@link OptionDescriptor} based on a given name.
|
|
93 |
*
|
46344
|
94 |
* @param loader source of the available {@link OptionDescriptors}
|
43972
|
95 |
* @param name the name of the option to look up
|
|
96 |
* @return the {@link OptionDescriptor} whose name equals {@code name} or null if not such
|
|
97 |
* descriptor is available
|
|
98 |
*/
|
46344
|
99 |
private static OptionDescriptor lookup(Iterable<OptionDescriptors> loader, String name) {
|
43972
|
100 |
for (OptionDescriptors optionDescriptors : loader) {
|
|
101 |
OptionDescriptor desc = optionDescriptors.get(name);
|
|
102 |
if (desc != null) {
|
|
103 |
return desc;
|
|
104 |
}
|
|
105 |
}
|
|
106 |
return null;
|
|
107 |
}
|
|
108 |
|
|
109 |
/**
|
|
110 |
* Parses a given option name and value.
|
|
111 |
*
|
|
112 |
* @param name the option name
|
46344
|
113 |
* @param uncheckedValue the unchecked value for the option
|
|
114 |
* @param values the object in which to store the parsed option and value
|
|
115 |
* @param loader source of the available {@link OptionDescriptors}
|
43972
|
116 |
* @throws IllegalArgumentException if there's a problem parsing {@code option}
|
|
117 |
*/
|
46344
|
118 |
static void parseOption(String name, Object uncheckedValue, EconomicMap<OptionKey<?>, Object> values, Iterable<OptionDescriptors> loader) {
|
43972
|
119 |
|
|
120 |
OptionDescriptor desc = lookup(loader, name);
|
|
121 |
if (desc == null) {
|
|
122 |
List<OptionDescriptor> matches = fuzzyMatch(loader, name);
|
|
123 |
Formatter msg = new Formatter();
|
|
124 |
msg.format("Could not find option %s", name);
|
|
125 |
if (!matches.isEmpty()) {
|
|
126 |
msg.format("%nDid you mean one of the following?");
|
|
127 |
for (OptionDescriptor match : matches) {
|
|
128 |
msg.format("%n %s=<value>", match.getName());
|
|
129 |
}
|
|
130 |
}
|
|
131 |
throw new IllegalArgumentException(msg.toString());
|
|
132 |
}
|
|
133 |
|
|
134 |
Class<?> optionType = desc.getType();
|
|
135 |
Object value;
|
46344
|
136 |
if (!(uncheckedValue instanceof String)) {
|
|
137 |
if (optionType != uncheckedValue.getClass()) {
|
|
138 |
String type = optionType.getSimpleName();
|
|
139 |
throw new IllegalArgumentException(type + " option '" + name + "' must have " + type + " value, not " + uncheckedValue.getClass() + " [toString: " + uncheckedValue + "]");
|
43972
|
140 |
}
|
46344
|
141 |
value = uncheckedValue;
|
|
142 |
} else {
|
|
143 |
String valueString = (String) uncheckedValue;
|
|
144 |
if (optionType == Boolean.class) {
|
|
145 |
if ("true".equals(valueString)) {
|
|
146 |
value = Boolean.TRUE;
|
|
147 |
} else if ("false".equals(valueString)) {
|
|
148 |
value = Boolean.FALSE;
|
43972
|
149 |
} else {
|
46344
|
150 |
throw new IllegalArgumentException("Boolean option '" + name + "' must have value \"true\" or \"false\", not \"" + uncheckedValue + "\"");
|
|
151 |
}
|
|
152 |
} else if (optionType == String.class) {
|
|
153 |
value = valueString;
|
|
154 |
} else if (Enum.class.isAssignableFrom(optionType)) {
|
|
155 |
value = ((EnumOptionKey<?>) desc.getOptionKey()).valueOf(valueString);
|
|
156 |
} else {
|
|
157 |
if (valueString.isEmpty()) {
|
|
158 |
throw new IllegalArgumentException("Non empty value required for option '" + name + "'");
|
43972
|
159 |
}
|
46344
|
160 |
try {
|
|
161 |
if (optionType == Float.class) {
|
|
162 |
value = Float.parseFloat(valueString);
|
|
163 |
} else if (optionType == Double.class) {
|
|
164 |
value = Double.parseDouble(valueString);
|
|
165 |
} else if (optionType == Integer.class) {
|
|
166 |
value = Integer.valueOf((int) parseLong(valueString));
|
|
167 |
} else if (optionType == Long.class) {
|
|
168 |
value = Long.valueOf(parseLong(valueString));
|
|
169 |
} else {
|
|
170 |
throw new IllegalArgumentException("Wrong value for option '" + name + "'");
|
|
171 |
}
|
|
172 |
} catch (NumberFormatException nfe) {
|
|
173 |
throw new IllegalArgumentException("Value for option '" + name + "' has invalid number format: " + valueString);
|
|
174 |
}
|
43972
|
175 |
}
|
|
176 |
}
|
46344
|
177 |
|
|
178 |
desc.optionKey.update(values, value);
|
43972
|
179 |
}
|
|
180 |
|
|
181 |
private static long parseLong(String v) {
|
|
182 |
String valueString = v.toLowerCase();
|
|
183 |
long scale = 1;
|
|
184 |
if (valueString.endsWith("k")) {
|
|
185 |
scale = 1024L;
|
|
186 |
} else if (valueString.endsWith("m")) {
|
|
187 |
scale = 1024L * 1024L;
|
|
188 |
} else if (valueString.endsWith("g")) {
|
|
189 |
scale = 1024L * 1024L * 1024L;
|
|
190 |
} else if (valueString.endsWith("t")) {
|
|
191 |
scale = 1024L * 1024L * 1024L * 1024L;
|
|
192 |
}
|
|
193 |
|
|
194 |
if (scale != 1) {
|
|
195 |
/* Remove trailing scale character. */
|
|
196 |
valueString = valueString.substring(0, valueString.length() - 1);
|
|
197 |
}
|
|
198 |
|
|
199 |
return Long.parseLong(valueString) * scale;
|
|
200 |
}
|
|
201 |
|
|
202 |
/**
|
|
203 |
* Compute string similarity based on Dice's coefficient.
|
|
204 |
*
|
|
205 |
* Ported from str_similar() in globals.cpp.
|
|
206 |
*/
|
|
207 |
static float stringSimiliarity(String str1, String str2) {
|
|
208 |
int hit = 0;
|
|
209 |
for (int i = 0; i < str1.length() - 1; ++i) {
|
|
210 |
for (int j = 0; j < str2.length() - 1; ++j) {
|
|
211 |
if ((str1.charAt(i) == str2.charAt(j)) && (str1.charAt(i + 1) == str2.charAt(j + 1))) {
|
|
212 |
++hit;
|
|
213 |
break;
|
|
214 |
}
|
|
215 |
}
|
|
216 |
}
|
|
217 |
return 2.0f * hit / (str1.length() + str2.length());
|
|
218 |
}
|
|
219 |
|
|
220 |
private static final float FUZZY_MATCH_THRESHOLD = 0.7F;
|
|
221 |
|
|
222 |
/**
|
|
223 |
* Returns the set of options that fuzzy match a given option name.
|
|
224 |
*/
|
46344
|
225 |
private static List<OptionDescriptor> fuzzyMatch(Iterable<OptionDescriptors> loader, String optionName) {
|
43972
|
226 |
List<OptionDescriptor> matches = new ArrayList<>();
|
|
227 |
for (OptionDescriptors options : loader) {
|
|
228 |
for (OptionDescriptor option : options) {
|
|
229 |
float score = stringSimiliarity(option.getName(), optionName);
|
|
230 |
if (score >= FUZZY_MATCH_THRESHOLD) {
|
|
231 |
matches.add(option);
|
|
232 |
}
|
|
233 |
}
|
|
234 |
}
|
|
235 |
return matches;
|
|
236 |
}
|
|
237 |
}
|