aoqi@0: /*
aoqi@0: * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0: *
aoqi@0: * This code is free software; you can redistribute it and/or modify it
aoqi@0: * under the terms of the GNU General Public License version 2 only, as
aoqi@0: * published by the Free Software Foundation. Oracle designates this
aoqi@0: * particular file as subject to the "Classpath" exception as provided
aoqi@0: * by Oracle in the LICENSE file that accompanied this code.
aoqi@0: *
aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0: * accompanied this code).
aoqi@0: *
aoqi@0: * You should have received a copy of the GNU General Public License version
aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0: *
aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0: * or visit www.oracle.com if you need additional information or have any
aoqi@0: * questions.
aoqi@0: */
aoqi@0:
aoqi@0: package com.sun.tools.javac.jvm;
aoqi@0:
aoqi@0: import java.util.*;
aoqi@0:
aoqi@0: import com.sun.tools.javac.code.Flags;
aoqi@0: import com.sun.tools.javac.code.Symbol;
aoqi@0: import com.sun.tools.javac.util.*;
aoqi@0:
aoqi@0: import static com.sun.tools.javac.main.Option.TARGET;
aoqi@0:
aoqi@0: /** The classfile version target.
aoqi@0: *
aoqi@0: *
This is NOT part of any supported API.
aoqi@0: * If you write code that depends on this, you do so at your own risk.
aoqi@0: * This code and its internal interfaces are subject to change or
aoqi@0: * deletion without notice.
aoqi@0: */
aoqi@0: public enum Target {
aoqi@0: JDK1_1("1.1", 45, 3),
aoqi@0: JDK1_2("1.2", 46, 0),
aoqi@0: JDK1_3("1.3", 47, 0),
aoqi@0:
aoqi@0: /** J2SE1.4 = Merlin. */
aoqi@0: JDK1_4("1.4", 48, 0),
aoqi@0:
aoqi@0: /** Tiger. */
aoqi@0: JDK1_5("1.5", 49, 0),
aoqi@0:
aoqi@0: /** JDK 6. */
aoqi@0: JDK1_6("1.6", 50, 0),
aoqi@0:
aoqi@0: /** JDK 7. */
aoqi@0: JDK1_7("1.7", 51, 0),
aoqi@0:
aoqi@0: /** JDK 8. */
aoqi@0: JDK1_8("1.8", 52, 0);
aoqi@0:
aoqi@0: private static final Context.Key targetKey =
aoqi@0: new Context.Key();
aoqi@0:
aoqi@0: public static Target instance(Context context) {
aoqi@0: Target instance = context.get(targetKey);
aoqi@0: if (instance == null) {
aoqi@0: Options options = Options.instance(context);
aoqi@0: String targetString = options.get(TARGET);
aoqi@0: if (targetString != null) instance = lookup(targetString);
aoqi@0: if (instance == null) instance = DEFAULT;
aoqi@0: context.put(targetKey, instance);
aoqi@0: }
aoqi@0: return instance;
aoqi@0: }
aoqi@0:
aoqi@0: private static final Target MIN = values()[0];
aoqi@0: public static Target MIN() { return MIN; }
aoqi@0:
aoqi@0: private static final Target MAX = values()[values().length - 1];
aoqi@0: public static Target MAX() { return MAX; }
aoqi@0:
aoqi@0: private static final Map tab = new HashMap();
aoqi@0: static {
aoqi@0: for (Target t : values()) {
aoqi@0: tab.put(t.name, t);
aoqi@0: }
aoqi@0: tab.put("5", JDK1_5);
aoqi@0: tab.put("6", JDK1_6);
aoqi@0: tab.put("7", JDK1_7);
aoqi@0: tab.put("8", JDK1_8);
aoqi@0: }
aoqi@0:
aoqi@0: public final String name;
aoqi@0: public final int majorVersion;
aoqi@0: public final int minorVersion;
aoqi@0: private Target(String name, int majorVersion, int minorVersion) {
aoqi@0: this.name = name;
aoqi@0: this.majorVersion = majorVersion;
aoqi@0: this.minorVersion = minorVersion;
aoqi@0: }
aoqi@0:
aoqi@0: public static final Target DEFAULT = JDK1_8;
aoqi@0:
aoqi@0: public static Target lookup(String name) {
aoqi@0: return tab.get(name);
aoqi@0: }
aoqi@0:
aoqi@0: /** In -target 1.1 and earlier, the compiler is required to emit
aoqi@0: * synthetic method definitions in abstract classes for interface
aoqi@0: * methods that are not overridden. We call them "Miranda" methods.
aoqi@0: */
aoqi@0: public boolean requiresIproxy() {
aoqi@0: return compareTo(JDK1_1) <= 0;
aoqi@0: }
aoqi@0:
aoqi@0: /** Beginning in 1.4, we take advantage of the possibility of emitting
aoqi@0: * code to initialize fields before calling the superclass constructor.
aoqi@0: * This is allowed by the VM spec, but the verifier refused to allow
aoqi@0: * it until 1.4. This is necesary to translate some code involving
aoqi@0: * inner classes. See, for example, 4030374.
aoqi@0: */
aoqi@0: public boolean initializeFieldsBeforeSuper() {
aoqi@0: return compareTo(JDK1_4) >= 0;
aoqi@0: }
aoqi@0:
aoqi@0: /** Beginning with -target 1.2 we obey the JLS rules for binary
aoqi@0: * compatibility, emitting as the qualifying type of a reference
aoqi@0: * to a method or field the type of the qualifier. In earlier
aoqi@0: * targets we use as the qualifying type the class in which the
aoqi@0: * member was found. The following methods named
aoqi@0: * *binaryCompatibility() indicate places where we vary from this
aoqi@0: * general rule. */
aoqi@0: public boolean obeyBinaryCompatibility() {
aoqi@0: return compareTo(JDK1_2) >= 0;
aoqi@0: }
aoqi@0:
aoqi@0: /** Starting in 1.5, the compiler uses an array type as
aoqi@0: * the qualifier for method calls (such as clone) where required by
aoqi@0: * the language and VM spec. Earlier versions of the compiler
aoqi@0: * qualified them by Object.
aoqi@0: */
aoqi@0: public boolean arrayBinaryCompatibility() {
aoqi@0: return compareTo(JDK1_5) >= 0;
aoqi@0: }
aoqi@0:
aoqi@0: /** Beginning after 1.2, we follow the binary compatibility rules for
aoqi@0: * interface fields. The 1.2 VMs had bugs handling interface fields
aoqi@0: * when compiled using binary compatibility (see 4400598), so this is
aoqi@0: * an accommodation to them.
aoqi@0: */
aoqi@0: public boolean interfaceFieldsBinaryCompatibility() {
aoqi@0: return compareTo(JDK1_2) > 0;
aoqi@0: }
aoqi@0:
aoqi@0: /** Beginning in -target 1.5, we follow the binary compatibility
aoqi@0: * rules for interface methods that redefine Object methods.
aoqi@0: * Earlier VMs had bugs handling such methods compiled using binary
aoqi@0: * compatibility (see 4392595, 4398791, 4392595, 4400415).
aoqi@0: * The VMs were fixed during or soon after 1.4. See 4392595.
aoqi@0: */
aoqi@0: public boolean interfaceObjectOverridesBinaryCompatibility() {
aoqi@0: return compareTo(JDK1_5) >= 0;
aoqi@0: }
aoqi@0:
aoqi@0: /** Beginning in -target 1.5, we make synthetic variables
aoqi@0: * package-private instead of private. This is to prevent the
aoqi@0: * necessity of access methods, which effectively relax the
aoqi@0: * protection of the field but bloat the class files and affect
aoqi@0: * execution.
aoqi@0: */
aoqi@0: public boolean usePrivateSyntheticFields() {
aoqi@0: return compareTo(JDK1_5) < 0;
aoqi@0: }
aoqi@0:
aoqi@0: /** Sometimes we need to create a field to cache a value like a
aoqi@0: * class literal of the assertions flag. In -target 1.5 and
aoqi@0: * later we create a new synthetic class for this instead of
aoqi@0: * using the outermost class. See 4401576.
aoqi@0: */
aoqi@0: public boolean useInnerCacheClass() {
aoqi@0: return compareTo(JDK1_5) >= 0;
aoqi@0: }
aoqi@0:
aoqi@0: /** Return true if cldc-style stack maps need to be generated. */
aoqi@0: public boolean generateCLDCStackmap() {
aoqi@0: return false;
aoqi@0: }
aoqi@0:
aoqi@0: /** Beginning in -target 6, we generate stackmap attribute in
aoqi@0: * compact format. */
aoqi@0: public boolean generateStackMapTable() {
aoqi@0: return compareTo(JDK1_6) >= 0;
aoqi@0: }
aoqi@0:
aoqi@0: /** Beginning in -target 6, package-info classes are marked synthetic.
aoqi@0: */
aoqi@0: public boolean isPackageInfoSynthetic() {
aoqi@0: return compareTo(JDK1_6) >= 0;
aoqi@0: }
aoqi@0:
aoqi@0: /** Do we generate "empty" stackmap slots after double and long?
aoqi@0: */
aoqi@0: public boolean generateEmptyAfterBig() {
aoqi@0: return false;
aoqi@0: }
aoqi@0:
aoqi@0: /** Beginning in 1.5, we have an unsynchronized version of
aoqi@0: * StringBuffer called StringBuilder that can be used by the
aoqi@0: * compiler for string concatenation.
aoqi@0: */
aoqi@0: public boolean useStringBuilder() {
aoqi@0: return compareTo(JDK1_5) >= 0;
aoqi@0: }
aoqi@0:
aoqi@0: /** Beginning in 1.5, we have flag bits we can use instead of
aoqi@0: * marker attributes.
aoqi@0: */
aoqi@0: public boolean useSyntheticFlag() {
aoqi@0: return compareTo(JDK1_5) >= 0;
aoqi@0: }
aoqi@0: public boolean useEnumFlag() {
aoqi@0: return compareTo(JDK1_5) >= 0;
aoqi@0: }
aoqi@0: public boolean useAnnotationFlag() {
aoqi@0: return compareTo(JDK1_5) >= 0;
aoqi@0: }
aoqi@0: public boolean useVarargsFlag() {
aoqi@0: return compareTo(JDK1_5) >= 0;
aoqi@0: }
aoqi@0: public boolean useBridgeFlag() {
aoqi@0: return compareTo(JDK1_5) >= 0;
aoqi@0: }
aoqi@0:
aoqi@0: /** Return the character to be used in constructing synthetic
aoqi@0: * identifiers, where not specified by the JLS.
aoqi@0: */
aoqi@0: public char syntheticNameChar() {
aoqi@0: return '$';
aoqi@0: }
aoqi@0:
aoqi@0: /** Does the VM have direct support for class literals?
aoqi@0: */
aoqi@0: public boolean hasClassLiterals() {
aoqi@0: return compareTo(JDK1_5) >= 0;
aoqi@0: }
aoqi@0:
aoqi@0: /** Does the VM support an invokedynamic instruction?
aoqi@0: */
aoqi@0: public boolean hasInvokedynamic() {
aoqi@0: return compareTo(JDK1_7) >= 0;
aoqi@0: }
aoqi@0:
aoqi@0: /** Does the VM support polymorphic method handle invocation?
aoqi@0: * Affects the linkage information output to the classfile.
aoqi@0: * An alias for {@code hasInvokedynamic}, since all the JSR 292 features appear together.
aoqi@0: */
aoqi@0: public boolean hasMethodHandles() {
aoqi@0: return hasInvokedynamic();
aoqi@0: }
aoqi@0:
aoqi@0: /** Although we may not have support for class literals, should we
aoqi@0: * avoid initializing the class that the literal refers to?
aoqi@0: * See 4468823
aoqi@0: */
aoqi@0: public boolean classLiteralsNoInit() {
aoqi@0: return compareTo(JDK1_5) >= 0;
aoqi@0: }
aoqi@0:
aoqi@0: /** Although we may not have support for class literals, when we
aoqi@0: * throw a NoClassDefFoundError, should we initialize its cause?
aoqi@0: */
aoqi@0: public boolean hasInitCause() {
aoqi@0: return compareTo(JDK1_4) >= 0;
aoqi@0: }
aoqi@0:
aoqi@0: /** For bootstrapping, we use J2SE1.4's wrapper class constructors
aoqi@0: * to implement boxing.
aoqi@0: */
aoqi@0: public boolean boxWithConstructors() {
aoqi@0: return compareTo(JDK1_5) < 0;
aoqi@0: }
aoqi@0:
aoqi@0: /** For bootstrapping, we use J2SE1.4's java.util.Collection
aoqi@0: * instead of java.lang.Iterable.
aoqi@0: */
aoqi@0: public boolean hasIterable() {
aoqi@0: return compareTo(JDK1_5) >= 0;
aoqi@0: }
aoqi@0:
aoqi@0: /** In J2SE1.5.0, we introduced the "EnclosingMethod" attribute
aoqi@0: * for improved reflection support.
aoqi@0: */
aoqi@0: public boolean hasEnclosingMethodAttribute() {
aoqi@0: return compareTo(JDK1_5) >= 0;
aoqi@0: }
aoqi@0: }