43972
|
1 |
/*
|
54328
|
2 |
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.options;
|
|
26 |
|
54084
|
27 |
import static jdk.vm.ci.services.Services.IS_BUILDING_NATIVE_IMAGE;
|
|
28 |
import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
|
|
29 |
|
43972
|
30 |
import java.util.ArrayList;
|
46371
|
31 |
import java.util.Collection;
|
43972
|
32 |
import java.util.Formatter;
|
|
33 |
import java.util.List;
|
|
34 |
import java.util.ServiceLoader;
|
46344
|
35 |
|
49873
|
36 |
import jdk.internal.vm.compiler.collections.EconomicMap;
|
|
37 |
import jdk.internal.vm.compiler.collections.MapCursor;
|
43972
|
38 |
|
|
39 |
/**
|
|
40 |
* This class contains methods for parsing Graal options and matching them against a set of
|
|
41 |
* {@link OptionDescriptors}. The {@link OptionDescriptors} are loaded via a {@link ServiceLoader}.
|
|
42 |
*/
|
|
43 |
public class OptionsParser {
|
|
44 |
|
54084
|
45 |
private static volatile List<OptionDescriptors> cachedOptionDescriptors;
|
|
46 |
|
46344
|
47 |
/**
|
54084
|
48 |
* Gets an iterable of available {@link OptionDescriptors}.
|
46344
|
49 |
*/
|
|
50 |
public static Iterable<OptionDescriptors> getOptionsLoader() {
|
54084
|
51 |
if (IS_IN_NATIVE_IMAGE || cachedOptionDescriptors != null) {
|
|
52 |
return cachedOptionDescriptors;
|
|
53 |
}
|
58299
|
54 |
return ModuleSupport.getOptionsLoader();
|
55509
|
55 |
}
|
|
56 |
|
58299
|
57 |
public static void setCachedOptionDescriptors(List<OptionDescriptors> list) {
|
55509
|
58 |
assert IS_BUILDING_NATIVE_IMAGE : "Used to pre-initialize the option descriptors during native image generation";
|
58299
|
59 |
OptionsParser.cachedOptionDescriptors = list;
|
43972
|
60 |
}
|
|
61 |
|
|
62 |
/**
|
|
63 |
* Parses a map representing assignments of values to options.
|
|
64 |
*
|
|
65 |
* @param optionSettings option settings (i.e., assignments of values to options)
|
46344
|
66 |
* @param values the object in which to store the parsed values
|
|
67 |
* @param loader source of the available {@link OptionDescriptors}
|
43972
|
68 |
* @throws IllegalArgumentException if there's a problem parsing {@code option}
|
|
69 |
*/
|
46344
|
70 |
public static void parseOptions(EconomicMap<String, String> optionSettings, EconomicMap<OptionKey<?>, Object> values, Iterable<OptionDescriptors> loader) {
|
43972
|
71 |
if (optionSettings != null && !optionSettings.isEmpty()) {
|
46344
|
72 |
MapCursor<String, String> cursor = optionSettings.getEntries();
|
|
73 |
while (cursor.advance()) {
|
|
74 |
parseOption(cursor.getKey(), cursor.getValue(), values, loader);
|
43972
|
75 |
}
|
|
76 |
}
|
|
77 |
}
|
|
78 |
|
|
79 |
/**
|
46344
|
80 |
* Parses a given option setting string and adds the parsed key and value {@code dst}.
|
43972
|
81 |
*
|
|
82 |
* @param optionSetting a string matching the pattern {@code <name>=<value>}
|
|
83 |
*/
|
46344
|
84 |
public static void parseOptionSettingTo(String optionSetting, EconomicMap<String, String> dst) {
|
43972
|
85 |
int eqIndex = optionSetting.indexOf('=');
|
|
86 |
if (eqIndex == -1) {
|
|
87 |
throw new InternalError("Option setting has does not match the pattern <name>=<value>: " + optionSetting);
|
|
88 |
}
|
|
89 |
dst.put(optionSetting.substring(0, eqIndex), optionSetting.substring(eqIndex + 1));
|
|
90 |
}
|
|
91 |
|
|
92 |
/**
|
|
93 |
* Looks up an {@link OptionDescriptor} based on a given name.
|
|
94 |
*
|
46344
|
95 |
* @param loader source of the available {@link OptionDescriptors}
|
43972
|
96 |
* @param name the name of the option to look up
|
|
97 |
* @return the {@link OptionDescriptor} whose name equals {@code name} or null if not such
|
|
98 |
* descriptor is available
|
|
99 |
*/
|
46344
|
100 |
private static OptionDescriptor lookup(Iterable<OptionDescriptors> loader, String name) {
|
43972
|
101 |
for (OptionDescriptors optionDescriptors : loader) {
|
|
102 |
OptionDescriptor desc = optionDescriptors.get(name);
|
|
103 |
if (desc != null) {
|
|
104 |
return desc;
|
|
105 |
}
|
|
106 |
}
|
|
107 |
return null;
|
|
108 |
}
|
|
109 |
|
|
110 |
/**
|
|
111 |
* Parses a given option name and value.
|
|
112 |
*
|
|
113 |
* @param name the option name
|
46344
|
114 |
* @param uncheckedValue the unchecked value for the option
|
|
115 |
* @param values the object in which to store the parsed option and value
|
|
116 |
* @param loader source of the available {@link OptionDescriptors}
|
43972
|
117 |
* @throws IllegalArgumentException if there's a problem parsing {@code option}
|
|
118 |
*/
|
49873
|
119 |
public static void parseOption(String name, Object uncheckedValue, EconomicMap<OptionKey<?>, Object> values, Iterable<OptionDescriptors> loader) {
|
43972
|
120 |
|
|
121 |
OptionDescriptor desc = lookup(loader, name);
|
|
122 |
if (desc == null) {
|
|
123 |
List<OptionDescriptor> matches = fuzzyMatch(loader, name);
|
|
124 |
Formatter msg = new Formatter();
|
|
125 |
msg.format("Could not find option %s", name);
|
|
126 |
if (!matches.isEmpty()) {
|
|
127 |
msg.format("%nDid you mean one of the following?");
|
|
128 |
for (OptionDescriptor match : matches) {
|
|
129 |
msg.format("%n %s=<value>", match.getName());
|
|
130 |
}
|
|
131 |
}
|
|
132 |
throw new IllegalArgumentException(msg.toString());
|
|
133 |
}
|
|
134 |
|
59095
|
135 |
Object value = parseOptionValue(desc, uncheckedValue);
|
|
136 |
|
|
137 |
desc.getOptionKey().update(values, value);
|
|
138 |
}
|
|
139 |
|
|
140 |
/** Parses a given option value with a known descriptor. */
|
|
141 |
public static Object parseOptionValue(OptionDescriptor desc, Object uncheckedValue) {
|
49873
|
142 |
Class<?> optionType = desc.getOptionValueType();
|
43972
|
143 |
Object value;
|
46344
|
144 |
if (!(uncheckedValue instanceof String)) {
|
|
145 |
if (optionType != uncheckedValue.getClass()) {
|
|
146 |
String type = optionType.getSimpleName();
|
59095
|
147 |
throw new IllegalArgumentException(type + " option '" + desc.getName() + "' must have " + type + " value, not " + uncheckedValue.getClass() + " [toString: " + uncheckedValue + "]");
|
43972
|
148 |
}
|
46344
|
149 |
value = uncheckedValue;
|
|
150 |
} else {
|
|
151 |
String valueString = (String) uncheckedValue;
|
|
152 |
if (optionType == Boolean.class) {
|
|
153 |
if ("true".equals(valueString)) {
|
|
154 |
value = Boolean.TRUE;
|
|
155 |
} else if ("false".equals(valueString)) {
|
|
156 |
value = Boolean.FALSE;
|
43972
|
157 |
} else {
|
59095
|
158 |
throw new IllegalArgumentException("Boolean option '" + desc.getName() + "' must have value \"true\" or \"false\", not \"" + uncheckedValue + "\"");
|
46344
|
159 |
}
|
|
160 |
} else if (optionType == String.class) {
|
|
161 |
value = valueString;
|
|
162 |
} else if (Enum.class.isAssignableFrom(optionType)) {
|
|
163 |
value = ((EnumOptionKey<?>) desc.getOptionKey()).valueOf(valueString);
|
|
164 |
} else {
|
|
165 |
if (valueString.isEmpty()) {
|
59095
|
166 |
throw new IllegalArgumentException("Non empty value required for option '" + desc.getName() + "'");
|
43972
|
167 |
}
|
46344
|
168 |
try {
|
|
169 |
if (optionType == Float.class) {
|
|
170 |
value = Float.parseFloat(valueString);
|
|
171 |
} else if (optionType == Double.class) {
|
|
172 |
value = Double.parseDouble(valueString);
|
|
173 |
} else if (optionType == Integer.class) {
|
|
174 |
value = Integer.valueOf((int) parseLong(valueString));
|
|
175 |
} else if (optionType == Long.class) {
|
|
176 |
value = Long.valueOf(parseLong(valueString));
|
|
177 |
} else {
|
59095
|
178 |
throw new IllegalArgumentException("Wrong value for option '" + desc.getName() + "'");
|
46344
|
179 |
}
|
|
180 |
} catch (NumberFormatException nfe) {
|
59095
|
181 |
throw new IllegalArgumentException("Value for option '" + desc.getName() + "' has invalid number format: " + valueString);
|
46344
|
182 |
}
|
43972
|
183 |
}
|
|
184 |
}
|
59095
|
185 |
return value;
|
43972
|
186 |
}
|
|
187 |
|
|
188 |
private static long parseLong(String v) {
|
|
189 |
String valueString = v.toLowerCase();
|
|
190 |
long scale = 1;
|
|
191 |
if (valueString.endsWith("k")) {
|
|
192 |
scale = 1024L;
|
|
193 |
} else if (valueString.endsWith("m")) {
|
|
194 |
scale = 1024L * 1024L;
|
|
195 |
} else if (valueString.endsWith("g")) {
|
|
196 |
scale = 1024L * 1024L * 1024L;
|
|
197 |
} else if (valueString.endsWith("t")) {
|
|
198 |
scale = 1024L * 1024L * 1024L * 1024L;
|
|
199 |
}
|
|
200 |
|
|
201 |
if (scale != 1) {
|
|
202 |
/* Remove trailing scale character. */
|
|
203 |
valueString = valueString.substring(0, valueString.length() - 1);
|
|
204 |
}
|
|
205 |
|
|
206 |
return Long.parseLong(valueString) * scale;
|
|
207 |
}
|
|
208 |
|
|
209 |
/**
|
|
210 |
* Compute string similarity based on Dice's coefficient.
|
|
211 |
*
|
|
212 |
* Ported from str_similar() in globals.cpp.
|
|
213 |
*/
|
|
214 |
static float stringSimiliarity(String str1, String str2) {
|
|
215 |
int hit = 0;
|
|
216 |
for (int i = 0; i < str1.length() - 1; ++i) {
|
|
217 |
for (int j = 0; j < str2.length() - 1; ++j) {
|
|
218 |
if ((str1.charAt(i) == str2.charAt(j)) && (str1.charAt(i + 1) == str2.charAt(j + 1))) {
|
|
219 |
++hit;
|
|
220 |
break;
|
|
221 |
}
|
|
222 |
}
|
|
223 |
}
|
|
224 |
return 2.0f * hit / (str1.length() + str2.length());
|
|
225 |
}
|
|
226 |
|
|
227 |
private static final float FUZZY_MATCH_THRESHOLD = 0.7F;
|
|
228 |
|
|
229 |
/**
|
|
230 |
* Returns the set of options that fuzzy match a given option name.
|
|
231 |
*/
|
46344
|
232 |
private static List<OptionDescriptor> fuzzyMatch(Iterable<OptionDescriptors> loader, String optionName) {
|
43972
|
233 |
List<OptionDescriptor> matches = new ArrayList<>();
|
|
234 |
for (OptionDescriptors options : loader) {
|
46371
|
235 |
collectFuzzyMatches(options, optionName, matches);
|
43972
|
236 |
}
|
|
237 |
return matches;
|
|
238 |
}
|
46371
|
239 |
|
|
240 |
/**
|
|
241 |
* Collects the set of options that fuzzy match a given option name. String similarity for fuzzy
|
|
242 |
* matching is based on Dice's coefficient.
|
|
243 |
*
|
|
244 |
* @param toSearch the set of option descriptors to search
|
|
245 |
* @param name the option name to search for
|
|
246 |
* @param matches the collection to which fuzzy matches of {@code name} will be added
|
|
247 |
* @return whether any fuzzy matches were found
|
|
248 |
*/
|
|
249 |
public static boolean collectFuzzyMatches(Iterable<OptionDescriptor> toSearch, String name, Collection<OptionDescriptor> matches) {
|
|
250 |
boolean found = false;
|
|
251 |
for (OptionDescriptor option : toSearch) {
|
|
252 |
float score = stringSimiliarity(option.getName(), name);
|
|
253 |
if (score >= FUZZY_MATCH_THRESHOLD) {
|
|
254 |
found = true;
|
|
255 |
matches.add(option);
|
|
256 |
}
|
|
257 |
}
|
|
258 |
return found;
|
|
259 |
}
|
43972
|
260 |
}
|