|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * Copyright (c) 2021, 2021, Alibaba Group Holding Limited. All rights reserved. |
| 4 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | + * |
| 6 | + * This code is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU General Public License version 2 only, as |
| 8 | + * published by the Free Software Foundation. Oracle designates this |
| 9 | + * particular file as subject to the "Classpath" exception as provided |
| 10 | + * by Oracle in the LICENSE file that accompanied this code. |
| 11 | + * |
| 12 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 13 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 16 | + * accompanied this code). |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License version |
| 19 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 20 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | + * |
| 22 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 23 | + * or visit www.oracle.com if you need additional information or have any |
| 24 | + * questions. |
| 25 | + */ |
| 26 | + |
| 27 | +package com.oracle.graal.pointsto.util; |
| 28 | + |
| 29 | +import org.graalvm.collections.EconomicMap; |
| 30 | +import org.graalvm.compiler.options.OptionDescriptor; |
| 31 | +import org.graalvm.compiler.options.OptionDescriptors; |
| 32 | +import org.graalvm.compiler.options.OptionKey; |
| 33 | +import org.graalvm.compiler.options.OptionValues; |
| 34 | + |
| 35 | +import java.util.Iterator; |
| 36 | + |
| 37 | +public final class PointsToOptionUtil { |
| 38 | + |
| 39 | + private static PointsToOptionUtil instance = new PointsToOptionUtil(); |
| 40 | + |
| 41 | + private OptionValues optionValues; |
| 42 | + private OptionDescriptors pointsToDescriptors; |
| 43 | + |
| 44 | + public static PointsToOptionUtil getInstance() { |
| 45 | + return instance; |
| 46 | + } |
| 47 | + |
| 48 | + private PointsToOptionUtil() { |
| 49 | + try { |
| 50 | + Class<?> descriptorClazz = Class.forName("com.oracle.graal.pointsto.api.PointstoOptions_OptionDescriptors"); |
| 51 | + pointsToDescriptors = (OptionDescriptors) descriptorClazz.getDeclaredConstructor().newInstance(); |
| 52 | + } catch (ReflectiveOperationException e) { |
| 53 | + AnalysisError.shouldNotReachHere(e); |
| 54 | + } |
| 55 | + |
| 56 | + EconomicMap<OptionKey<?>, Object> initialMap = EconomicMap.create(); |
| 57 | + Iterator<OptionDescriptor> iterator = pointsToDescriptors.iterator(); |
| 58 | + while (iterator.hasNext()) { |
| 59 | + OptionDescriptor descriptor = iterator.next(); |
| 60 | + OptionKey<?> key = descriptor.getOptionKey(); |
| 61 | + initialMap.put(key, key.getDefaultValue()); |
| 62 | + } |
| 63 | + optionValues = new OptionValues(initialMap); |
| 64 | + } |
| 65 | + |
| 66 | + public void setOptionValue(OptionKey<?> optionKey, Object value) { |
| 67 | + optionKey.update((EconomicMap<OptionKey<?>, Object>) optionValues.getMap(), value); |
| 68 | + } |
| 69 | + |
| 70 | + public OptionValues parse(String[] args) { |
| 71 | + for (String arg : args) { |
| 72 | + if (arg.startsWith("-H:")) { |
| 73 | + String inputArg = arg.substring(3); |
| 74 | + Object value = null; |
| 75 | + String option = null; |
| 76 | + if (inputArg.startsWith("-") || inputArg.startsWith("+")) { |
| 77 | + option = inputArg.substring(1); |
| 78 | + if (inputArg.startsWith("-")) { |
| 79 | + value = false; |
| 80 | + } else { |
| 81 | + value = true; |
| 82 | + } |
| 83 | + } else if (inputArg.indexOf('=') != -1) { |
| 84 | + int splitIdx = inputArg.indexOf('='); |
| 85 | + option = inputArg.substring(0, splitIdx); |
| 86 | + String srcValue = inputArg.substring(splitIdx + 1); |
| 87 | + OptionDescriptor descriptor = pointsToDescriptors.get(option); |
| 88 | + Class<?> optionValType = descriptor.getOptionValueType(); |
| 89 | + if (optionValType.equals(Integer.class)) { |
| 90 | + value = Integer.parseInt(srcValue); |
| 91 | + } else if (optionValType.equals(String.class)) { |
| 92 | + value = srcValue; |
| 93 | + } else { |
| 94 | + // Currently there are only 2 types |
| 95 | + AnalysisError.shouldNotReachHere("Current pointsto option values are only Boolean, String and Integer," + |
| 96 | + " but the given option " + option + " is " + optionValType.getName()); |
| 97 | + } |
| 98 | + |
| 99 | + } else { |
| 100 | + AnalysisError.shouldNotReachHere("Option format not supported:" + inputArg); |
| 101 | + } |
| 102 | + pointsToDescriptors.get(option).getOptionKey().update((EconomicMap<OptionKey<?>, Object>) optionValues.getMap(), value); |
| 103 | + } |
| 104 | + } |
| 105 | + return optionValues; |
| 106 | + } |
| 107 | +} |
0 commit comments