src/share/classes/com/sun/tools/javac/jvm/Target.java

Tue, 13 Dec 2011 11:21:28 -0800

author
jjg
date
Tue, 13 Dec 2011 11:21:28 -0800
changeset 1157
3809292620c9
parent 1042
defdd98cb7ce
child 1395
9bce0c73583d
permissions
-rw-r--r--

7120736: refactor javac option handling
Reviewed-by: mcimadamore

duke@1 1 /*
darcy@1042 2 * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.jvm;
duke@1 27
duke@1 28 import java.util.*;
duke@1 29
duke@1 30 import com.sun.tools.javac.code.Flags;
duke@1 31 import com.sun.tools.javac.code.Symbol;
duke@1 32 import com.sun.tools.javac.util.*;
duke@1 33
jjg@1157 34 import static com.sun.tools.javac.main.Option.*;
jjg@700 35
duke@1 36 /** The classfile version target.
duke@1 37 *
jjg@581 38 * <p><b>This is NOT part of any supported API.
jjg@581 39 * If you write code that depends on this, you do so at your own risk.
duke@1 40 * This code and its internal interfaces are subject to change or
duke@1 41 * deletion without notice.</b>
duke@1 42 */
duke@1 43 public enum Target {
duke@1 44 JDK1_1("1.1", 45, 3),
duke@1 45 JDK1_2("1.2", 46, 0),
duke@1 46 JDK1_3("1.3", 47, 0),
duke@1 47
duke@1 48 /** J2SE1.4 = Merlin. */
duke@1 49 JDK1_4("1.4", 48, 0),
duke@1 50
duke@1 51 /** Support for the JSR14 prototype compiler (targeting 1.4 VMs
duke@1 52 * augmented with a few support classes). This is a transitional
duke@1 53 * option that will not be supported in the product. */
duke@1 54 JSR14("jsr14", 48, 0),
duke@1 55
duke@1 56 /** The following are undocumented transitional targets that we
duke@1 57 * had used to test VM fixes in update releases. We do not
duke@1 58 * promise to retain support for them. */
duke@1 59 JDK1_4_1("1.4.1", 48, 0),
duke@1 60 JDK1_4_2("1.4.2", 48, 0),
duke@1 61
duke@1 62 /** Tiger. */
duke@1 63 JDK1_5("1.5", 49, 0),
duke@1 64
duke@1 65 /** JDK 6. */
duke@1 66 JDK1_6("1.6", 50, 0),
duke@1 67
duke@1 68 /** JDK 7. */
darcy@1042 69 JDK1_7("1.7", 51, 0),
darcy@1042 70
darcy@1042 71 /** JDK 8. */ // For now, a clone of 7
darcy@1042 72 JDK1_8("1.8", 51, 0);
duke@1 73
duke@1 74 private static final Context.Key<Target> targetKey =
duke@1 75 new Context.Key<Target>();
duke@1 76
duke@1 77 public static Target instance(Context context) {
duke@1 78 Target instance = context.get(targetKey);
duke@1 79 if (instance == null) {
duke@1 80 Options options = Options.instance(context);
jjg@700 81 String targetString = options.get(TARGET);
duke@1 82 if (targetString != null) instance = lookup(targetString);
duke@1 83 if (instance == null) instance = DEFAULT;
duke@1 84 context.put(targetKey, instance);
duke@1 85 }
duke@1 86 return instance;
duke@1 87 }
duke@1 88
duke@1 89 private static Target MIN;
duke@1 90 public static Target MIN() { return MIN; }
duke@1 91
duke@1 92 private static Target MAX;
duke@1 93 public static Target MAX() { return MAX; }
duke@1 94
duke@1 95 private static Map<String,Target> tab = new HashMap<String,Target>();
duke@1 96 static {
duke@1 97 for (Target t : values()) {
duke@1 98 if (MIN == null) MIN = t;
duke@1 99 MAX = t;
duke@1 100 tab.put(t.name, t);
duke@1 101 }
duke@1 102 tab.put("5", JDK1_5);
duke@1 103 tab.put("6", JDK1_6);
duke@1 104 tab.put("7", JDK1_7);
darcy@1042 105 tab.put("8", JDK1_8);
duke@1 106 }
duke@1 107
duke@1 108 public final String name;
duke@1 109 public final int majorVersion;
duke@1 110 public final int minorVersion;
duke@1 111 private Target(String name, int majorVersion, int minorVersion) {
duke@1 112 this.name = name;
duke@1 113 this.majorVersion = majorVersion;
duke@1 114 this.minorVersion = minorVersion;
duke@1 115 }
duke@1 116
darcy@1042 117 public static final Target DEFAULT = JDK1_8;
duke@1 118
duke@1 119 public static Target lookup(String name) {
duke@1 120 return tab.get(name);
duke@1 121 }
duke@1 122
duke@1 123 /** In -target 1.1 and earlier, the compiler is required to emit
duke@1 124 * synthetic method definitions in abstract classes for interface
duke@1 125 * methods that are not overridden. We call them "Miranda" methods.
duke@1 126 */
duke@1 127 public boolean requiresIproxy() {
duke@1 128 return compareTo(JDK1_1) <= 0;
duke@1 129 }
duke@1 130
duke@1 131 /** Beginning in 1.4, we take advantage of the possibility of emitting
duke@1 132 * code to initialize fields before calling the superclass constructor.
duke@1 133 * This is allowed by the VM spec, but the verifier refused to allow
duke@1 134 * it until 1.4. This is necesary to translate some code involving
duke@1 135 * inner classes. See, for example, 4030374.
duke@1 136 */
duke@1 137 public boolean initializeFieldsBeforeSuper() {
duke@1 138 return compareTo(JDK1_4) >= 0;
duke@1 139 }
duke@1 140
duke@1 141 /** Beginning with -target 1.2 we obey the JLS rules for binary
duke@1 142 * compatibility, emitting as the qualifying type of a reference
duke@1 143 * to a method or field the type of the qualifier. In earlier
duke@1 144 * targets we use as the qualifying type the class in which the
duke@1 145 * member was found. The following methods named
duke@1 146 * *binaryCompatibility() indicate places where we vary from this
duke@1 147 * general rule. */
duke@1 148 public boolean obeyBinaryCompatibility() {
duke@1 149 return compareTo(JDK1_2) >= 0;
duke@1 150 }
duke@1 151
duke@1 152 /** Starting in 1.5, the compiler uses an array type as
duke@1 153 * the qualifier for method calls (such as clone) where required by
duke@1 154 * the language and VM spec. Earlier versions of the compiler
duke@1 155 * qualified them by Object.
duke@1 156 */
duke@1 157 public boolean arrayBinaryCompatibility() {
duke@1 158 return compareTo(JDK1_5) >= 0;
duke@1 159 }
duke@1 160
duke@1 161 /** Beginning after 1.2, we follow the binary compatibility rules for
duke@1 162 * interface fields. The 1.2 VMs had bugs handling interface fields
duke@1 163 * when compiled using binary compatibility (see 4400598), so this is
duke@1 164 * an accommodation to them.
duke@1 165 */
duke@1 166 public boolean interfaceFieldsBinaryCompatibility() {
duke@1 167 return compareTo(JDK1_2) > 0;
duke@1 168 }
duke@1 169
duke@1 170 /** Beginning in -target 1.5, we follow the binary compatibility
duke@1 171 * rules for interface methods that redefine Object methods.
duke@1 172 * Earlier VMs had bugs handling such methods compiled using binary
duke@1 173 * compatibility (see 4392595, 4398791, 4392595, 4400415).
duke@1 174 * The VMs were fixed during or soon after 1.4. See 4392595.
duke@1 175 */
duke@1 176 public boolean interfaceObjectOverridesBinaryCompatibility() {
duke@1 177 return compareTo(JDK1_5) >= 0;
duke@1 178 }
duke@1 179
duke@1 180 /** Beginning in -target 1.4.2, we make synthetic variables
duke@1 181 * package-private instead of private. This is to prevent the
duke@1 182 * necessity of access methods, which effectively relax the
duke@1 183 * protection of the field but bloat the class files and affect
duke@1 184 * execution.
duke@1 185 */
duke@1 186 public boolean usePrivateSyntheticFields() {
duke@1 187 return compareTo(JDK1_4_2) < 0;
duke@1 188 }
duke@1 189
duke@1 190 /** Sometimes we need to create a field to cache a value like a
duke@1 191 * class literal of the assertions flag. In -target 1.4.2 and
duke@1 192 * later we create a new synthetic class for this instead of
duke@1 193 * using the outermost class. See 4401576.
duke@1 194 */
duke@1 195 public boolean useInnerCacheClass() {
duke@1 196 return compareTo(JDK1_4_2) >= 0;
duke@1 197 }
duke@1 198
duke@1 199 /** Return true if cldc-style stack maps need to be generated. */
duke@1 200 public boolean generateCLDCStackmap() {
duke@1 201 return false;
duke@1 202 }
duke@1 203
duke@1 204 /** Beginning in -target 6, we generate stackmap attribute in
duke@1 205 * compact format. */
duke@1 206 public boolean generateStackMapTable() {
duke@1 207 return compareTo(JDK1_6) >= 0;
duke@1 208 }
duke@1 209
duke@1 210 /** Beginning in -target 6, package-info classes are marked synthetic.
duke@1 211 */
duke@1 212 public boolean isPackageInfoSynthetic() {
duke@1 213 return compareTo(JDK1_6) >= 0;
duke@1 214 }
duke@1 215
duke@1 216 /** Do we generate "empty" stackmap slots after double and long?
duke@1 217 */
duke@1 218 public boolean generateEmptyAfterBig() {
duke@1 219 return false;
duke@1 220 }
duke@1 221
duke@1 222 /** Beginning in 1.5, we have an unsynchronized version of
duke@1 223 * StringBuffer called StringBuilder that can be used by the
duke@1 224 * compiler for string concatenation.
duke@1 225 */
duke@1 226 public boolean useStringBuilder() {
duke@1 227 return compareTo(JDK1_5) >= 0;
duke@1 228 }
duke@1 229
duke@1 230 /** Beginning in 1.5, we have flag bits we can use instead of
duke@1 231 * marker attributes.
duke@1 232 */
duke@1 233 public boolean useSyntheticFlag() {
duke@1 234 return compareTo(JDK1_5) >= 0;
duke@1 235 }
duke@1 236 public boolean useEnumFlag() {
duke@1 237 return compareTo(JDK1_5) >= 0;
duke@1 238 }
duke@1 239 public boolean useAnnotationFlag() {
duke@1 240 return compareTo(JDK1_5) >= 0;
duke@1 241 }
duke@1 242 public boolean useVarargsFlag() {
duke@1 243 return compareTo(JDK1_5) >= 0;
duke@1 244 }
duke@1 245 public boolean useBridgeFlag() {
duke@1 246 return compareTo(JDK1_5) >= 0;
duke@1 247 }
duke@1 248
duke@1 249 /** Return the character to be used in constructing synthetic
duke@1 250 * identifiers, where not specified by the JLS.
duke@1 251 */
duke@1 252 public char syntheticNameChar() {
duke@1 253 return '$';
duke@1 254 }
duke@1 255
duke@1 256 /** Does the VM have direct support for class literals?
duke@1 257 */
duke@1 258 public boolean hasClassLiterals() {
duke@1 259 return compareTo(JDK1_5) >= 0;
duke@1 260 }
duke@1 261
jrose@267 262 /** Does the VM support an invokedynamic instruction?
jrose@267 263 */
jrose@267 264 public boolean hasInvokedynamic() {
jrose@267 265 return compareTo(JDK1_7) >= 0;
jrose@267 266 }
jrose@267 267
mcimadamore@674 268 /** Does the VM support polymorphic method handle invocation?
mcimadamore@674 269 * Affects the linkage information output to the classfile.
mcimadamore@674 270 * An alias for {@code hasInvokedynamic}, since all the JSR 292 features appear together.
mcimadamore@674 271 */
mcimadamore@674 272 public boolean hasMethodHandles() {
mcimadamore@674 273 return hasInvokedynamic();
mcimadamore@674 274 }
mcimadamore@674 275
duke@1 276 /** Although we may not have support for class literals, should we
duke@1 277 * avoid initializing the class that the literal refers to?
duke@1 278 * See 4468823
duke@1 279 */
duke@1 280 public boolean classLiteralsNoInit() {
duke@1 281 return compareTo(JDK1_4_2) >= 0;
duke@1 282 }
duke@1 283
duke@1 284 /** Although we may not have support for class literals, when we
duke@1 285 * throw a NoClassDefFoundError, should we initialize its cause?
duke@1 286 */
duke@1 287 public boolean hasInitCause() {
duke@1 288 return compareTo(JDK1_4) >= 0;
duke@1 289 }
duke@1 290
duke@1 291 /** For bootstrapping, we use J2SE1.4's wrapper class constructors
duke@1 292 * to implement boxing.
duke@1 293 */
duke@1 294 public boolean boxWithConstructors() {
duke@1 295 return compareTo(JDK1_5) < 0;
duke@1 296 }
duke@1 297
duke@1 298 /** For bootstrapping, we use J2SE1.4's java.util.Collection
duke@1 299 * instead of java.lang.Iterable.
duke@1 300 */
duke@1 301 public boolean hasIterable() {
duke@1 302 return compareTo(JDK1_5) >= 0;
duke@1 303 }
duke@1 304
duke@1 305 /** For bootstrapping javac only, we do without java.lang.Enum if
duke@1 306 * necessary.
duke@1 307 */
duke@1 308 public boolean compilerBootstrap(Symbol c) {
duke@1 309 return
duke@1 310 this == JSR14 &&
duke@1 311 (c.flags() & Flags.ENUM) != 0 &&
duke@1 312 c.flatName().toString().startsWith("com.sun.tools.")
duke@1 313 // && !Target.class.getSuperclass().getName().equals("java.lang.Enum")
duke@1 314 ;
duke@1 315 }
duke@1 316
duke@1 317 /** In J2SE1.5.0, we introduced the "EnclosingMethod" attribute
duke@1 318 * for improved reflection support.
duke@1 319 */
duke@1 320 public boolean hasEnclosingMethodAttribute() {
duke@1 321 return compareTo(JDK1_5) >= 0 || this == JSR14;
duke@1 322 }
duke@1 323 }

mercurial