duke@1: /* duke@1: * Copyright 2002-2006 Sun Microsystems, Inc. All Rights Reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as duke@1: * published by the Free Software Foundation. Sun designates this duke@1: * particular file as subject to the "Classpath" exception as provided duke@1: * by Sun in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or duke@1: * have any questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.jvm; duke@1: duke@1: import java.util.*; duke@1: duke@1: import com.sun.tools.javac.code.Flags; duke@1: import com.sun.tools.javac.code.Symbol; duke@1: import com.sun.tools.javac.util.*; duke@1: duke@1: /** The classfile version target. duke@1: * duke@1: *

This is NOT part of any API supported by Sun Microsystems. If duke@1: * you write code that depends on this, you do so at your own risk. duke@1: * This code and its internal interfaces are subject to change or duke@1: * deletion without notice. duke@1: */ duke@1: public enum Target { duke@1: JDK1_1("1.1", 45, 3), duke@1: JDK1_2("1.2", 46, 0), duke@1: JDK1_3("1.3", 47, 0), duke@1: duke@1: /** J2SE1.4 = Merlin. */ duke@1: JDK1_4("1.4", 48, 0), duke@1: duke@1: /** Support for the JSR14 prototype compiler (targeting 1.4 VMs duke@1: * augmented with a few support classes). This is a transitional duke@1: * option that will not be supported in the product. */ duke@1: JSR14("jsr14", 48, 0), duke@1: duke@1: /** The following are undocumented transitional targets that we duke@1: * had used to test VM fixes in update releases. We do not duke@1: * promise to retain support for them. */ duke@1: JDK1_4_1("1.4.1", 48, 0), duke@1: JDK1_4_2("1.4.2", 48, 0), duke@1: duke@1: /** Tiger. */ duke@1: JDK1_5("1.5", 49, 0), duke@1: duke@1: /** JDK 6. */ duke@1: JDK1_6("1.6", 50, 0), duke@1: duke@1: /** JDK 7. */ duke@1: JDK1_7("1.7", 51, 0); duke@1: duke@1: private static final Context.Key targetKey = duke@1: new Context.Key(); duke@1: duke@1: public static Target instance(Context context) { duke@1: Target instance = context.get(targetKey); duke@1: if (instance == null) { duke@1: Options options = Options.instance(context); duke@1: String targetString = options.get("-target"); duke@1: if (targetString != null) instance = lookup(targetString); duke@1: if (instance == null) instance = DEFAULT; duke@1: context.put(targetKey, instance); duke@1: } duke@1: return instance; duke@1: } duke@1: duke@1: private static Target MIN; duke@1: public static Target MIN() { return MIN; } duke@1: duke@1: private static Target MAX; duke@1: public static Target MAX() { return MAX; } duke@1: duke@1: private static Map tab = new HashMap(); duke@1: static { duke@1: for (Target t : values()) { duke@1: if (MIN == null) MIN = t; duke@1: MAX = t; duke@1: tab.put(t.name, t); duke@1: } duke@1: tab.put("5", JDK1_5); duke@1: tab.put("6", JDK1_6); duke@1: tab.put("7", JDK1_7); duke@1: } duke@1: duke@1: public final String name; duke@1: public final int majorVersion; duke@1: public final int minorVersion; duke@1: private Target(String name, int majorVersion, int minorVersion) { duke@1: this.name = name; duke@1: this.majorVersion = majorVersion; duke@1: this.minorVersion = minorVersion; duke@1: } duke@1: jjg@286: public static final Target DEFAULT = JDK1_7; duke@1: duke@1: public static Target lookup(String name) { duke@1: return tab.get(name); duke@1: } duke@1: duke@1: /** In -target 1.1 and earlier, the compiler is required to emit duke@1: * synthetic method definitions in abstract classes for interface duke@1: * methods that are not overridden. We call them "Miranda" methods. duke@1: */ duke@1: public boolean requiresIproxy() { duke@1: return compareTo(JDK1_1) <= 0; duke@1: } duke@1: duke@1: /** Beginning in 1.4, we take advantage of the possibility of emitting duke@1: * code to initialize fields before calling the superclass constructor. duke@1: * This is allowed by the VM spec, but the verifier refused to allow duke@1: * it until 1.4. This is necesary to translate some code involving duke@1: * inner classes. See, for example, 4030374. duke@1: */ duke@1: public boolean initializeFieldsBeforeSuper() { duke@1: return compareTo(JDK1_4) >= 0; duke@1: } duke@1: duke@1: /** Beginning with -target 1.2 we obey the JLS rules for binary duke@1: * compatibility, emitting as the qualifying type of a reference duke@1: * to a method or field the type of the qualifier. In earlier duke@1: * targets we use as the qualifying type the class in which the duke@1: * member was found. The following methods named duke@1: * *binaryCompatibility() indicate places where we vary from this duke@1: * general rule. */ duke@1: public boolean obeyBinaryCompatibility() { duke@1: return compareTo(JDK1_2) >= 0; duke@1: } duke@1: duke@1: /** Starting in 1.5, the compiler uses an array type as duke@1: * the qualifier for method calls (such as clone) where required by duke@1: * the language and VM spec. Earlier versions of the compiler duke@1: * qualified them by Object. duke@1: */ duke@1: public boolean arrayBinaryCompatibility() { duke@1: return compareTo(JDK1_5) >= 0; duke@1: } duke@1: duke@1: /** Beginning after 1.2, we follow the binary compatibility rules for duke@1: * interface fields. The 1.2 VMs had bugs handling interface fields duke@1: * when compiled using binary compatibility (see 4400598), so this is duke@1: * an accommodation to them. duke@1: */ duke@1: public boolean interfaceFieldsBinaryCompatibility() { duke@1: return compareTo(JDK1_2) > 0; duke@1: } duke@1: duke@1: /** Beginning in -target 1.5, we follow the binary compatibility duke@1: * rules for interface methods that redefine Object methods. duke@1: * Earlier VMs had bugs handling such methods compiled using binary duke@1: * compatibility (see 4392595, 4398791, 4392595, 4400415). duke@1: * The VMs were fixed during or soon after 1.4. See 4392595. duke@1: */ duke@1: public boolean interfaceObjectOverridesBinaryCompatibility() { duke@1: return compareTo(JDK1_5) >= 0; duke@1: } duke@1: duke@1: /** Beginning in -target 1.4.2, we make synthetic variables duke@1: * package-private instead of private. This is to prevent the duke@1: * necessity of access methods, which effectively relax the duke@1: * protection of the field but bloat the class files and affect duke@1: * execution. duke@1: */ duke@1: public boolean usePrivateSyntheticFields() { duke@1: return compareTo(JDK1_4_2) < 0; duke@1: } duke@1: duke@1: /** Sometimes we need to create a field to cache a value like a duke@1: * class literal of the assertions flag. In -target 1.4.2 and duke@1: * later we create a new synthetic class for this instead of duke@1: * using the outermost class. See 4401576. duke@1: */ duke@1: public boolean useInnerCacheClass() { duke@1: return compareTo(JDK1_4_2) >= 0; duke@1: } duke@1: duke@1: /** Return true if cldc-style stack maps need to be generated. */ duke@1: public boolean generateCLDCStackmap() { duke@1: return false; duke@1: } duke@1: duke@1: /** Beginning in -target 6, we generate stackmap attribute in duke@1: * compact format. */ duke@1: public boolean generateStackMapTable() { duke@1: return compareTo(JDK1_6) >= 0; duke@1: } duke@1: duke@1: /** Beginning in -target 6, package-info classes are marked synthetic. duke@1: */ duke@1: public boolean isPackageInfoSynthetic() { duke@1: return compareTo(JDK1_6) >= 0; duke@1: } duke@1: duke@1: /** Do we generate "empty" stackmap slots after double and long? duke@1: */ duke@1: public boolean generateEmptyAfterBig() { duke@1: return false; duke@1: } duke@1: duke@1: /** Beginning in 1.5, we have an unsynchronized version of duke@1: * StringBuffer called StringBuilder that can be used by the duke@1: * compiler for string concatenation. duke@1: */ duke@1: public boolean useStringBuilder() { duke@1: return compareTo(JDK1_5) >= 0; duke@1: } duke@1: duke@1: /** Beginning in 1.5, we have flag bits we can use instead of duke@1: * marker attributes. duke@1: */ duke@1: public boolean useSyntheticFlag() { duke@1: return compareTo(JDK1_5) >= 0; duke@1: } duke@1: public boolean useEnumFlag() { duke@1: return compareTo(JDK1_5) >= 0; duke@1: } duke@1: public boolean useAnnotationFlag() { duke@1: return compareTo(JDK1_5) >= 0; duke@1: } duke@1: public boolean useVarargsFlag() { duke@1: return compareTo(JDK1_5) >= 0; duke@1: } duke@1: public boolean useBridgeFlag() { duke@1: return compareTo(JDK1_5) >= 0; duke@1: } duke@1: duke@1: /** Return the character to be used in constructing synthetic duke@1: * identifiers, where not specified by the JLS. duke@1: */ duke@1: public char syntheticNameChar() { duke@1: return '$'; duke@1: } duke@1: duke@1: /** Does the VM have direct support for class literals? duke@1: */ duke@1: public boolean hasClassLiterals() { duke@1: return compareTo(JDK1_5) >= 0; duke@1: } duke@1: jrose@267: /** Does the VM support an invokedynamic instruction? jrose@267: */ jrose@267: public boolean hasInvokedynamic() { jrose@267: return compareTo(JDK1_7) >= 0; jrose@267: } jrose@267: duke@1: /** Although we may not have support for class literals, should we duke@1: * avoid initializing the class that the literal refers to? duke@1: * See 4468823 duke@1: */ duke@1: public boolean classLiteralsNoInit() { duke@1: return compareTo(JDK1_4_2) >= 0; duke@1: } duke@1: duke@1: /** Although we may not have support for class literals, when we duke@1: * throw a NoClassDefFoundError, should we initialize its cause? duke@1: */ duke@1: public boolean hasInitCause() { duke@1: return compareTo(JDK1_4) >= 0; duke@1: } duke@1: duke@1: /** For bootstrapping, we use J2SE1.4's wrapper class constructors duke@1: * to implement boxing. duke@1: */ duke@1: public boolean boxWithConstructors() { duke@1: return compareTo(JDK1_5) < 0; duke@1: } duke@1: duke@1: /** For bootstrapping, we use J2SE1.4's java.util.Collection duke@1: * instead of java.lang.Iterable. duke@1: */ duke@1: public boolean hasIterable() { duke@1: return compareTo(JDK1_5) >= 0; duke@1: } duke@1: duke@1: /** For bootstrapping javac only, we do without java.lang.Enum if duke@1: * necessary. duke@1: */ duke@1: public boolean compilerBootstrap(Symbol c) { duke@1: return duke@1: this == JSR14 && duke@1: (c.flags() & Flags.ENUM) != 0 && duke@1: c.flatName().toString().startsWith("com.sun.tools.") duke@1: // && !Target.class.getSuperclass().getName().equals("java.lang.Enum") duke@1: ; duke@1: } duke@1: duke@1: /** In J2SE1.5.0, we introduced the "EnclosingMethod" attribute duke@1: * for improved reflection support. duke@1: */ duke@1: public boolean hasEnclosingMethodAttribute() { duke@1: return compareTo(JDK1_5) >= 0 || this == JSR14; duke@1: } duke@1: }