Wed, 21 Sep 2011 21:56:53 -0700
7092965: javac should not close processorClassLoader before end of compilation
Reviewed-by: darcy
duke@1 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 1999, 2006, 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.comp; |
duke@1 | 27 | |
duke@1 | 28 | import com.sun.tools.javac.code.*; |
duke@1 | 29 | import com.sun.tools.javac.jvm.*; |
duke@1 | 30 | import com.sun.tools.javac.util.*; |
duke@1 | 31 | |
duke@1 | 32 | import com.sun.tools.javac.code.Type.*; |
duke@1 | 33 | |
duke@1 | 34 | import static com.sun.tools.javac.code.TypeTags.*; |
duke@1 | 35 | import static com.sun.tools.javac.jvm.ByteCodes.*; |
duke@1 | 36 | |
duke@1 | 37 | /** Helper class for constant folding, used by the attribution phase. |
duke@1 | 38 | * This class is marked strictfp as mandated by JLS 15.4. |
duke@1 | 39 | * |
jjg@581 | 40 | * <p><b>This is NOT part of any supported API. |
jjg@581 | 41 | * If you write code that depends on this, you do so at your own risk. |
duke@1 | 42 | * This code and its internal interfaces are subject to change or |
duke@1 | 43 | * deletion without notice.</b> |
duke@1 | 44 | */ |
duke@1 | 45 | strictfp class ConstFold { |
duke@1 | 46 | protected static final Context.Key<ConstFold> constFoldKey = |
duke@1 | 47 | new Context.Key<ConstFold>(); |
duke@1 | 48 | |
duke@1 | 49 | private Symtab syms; |
duke@1 | 50 | |
duke@1 | 51 | public static ConstFold instance(Context context) { |
duke@1 | 52 | ConstFold instance = context.get(constFoldKey); |
duke@1 | 53 | if (instance == null) |
duke@1 | 54 | instance = new ConstFold(context); |
duke@1 | 55 | return instance; |
duke@1 | 56 | } |
duke@1 | 57 | |
duke@1 | 58 | private ConstFold(Context context) { |
duke@1 | 59 | context.put(constFoldKey, this); |
duke@1 | 60 | |
duke@1 | 61 | syms = Symtab.instance(context); |
duke@1 | 62 | } |
duke@1 | 63 | |
duke@1 | 64 | static Integer minusOne = -1; |
duke@1 | 65 | static Integer zero = 0; |
duke@1 | 66 | static Integer one = 1; |
duke@1 | 67 | |
duke@1 | 68 | /** Convert boolean to integer (true = 1, false = 0). |
duke@1 | 69 | */ |
duke@1 | 70 | private static Integer b2i(boolean b) { |
duke@1 | 71 | return b ? one : zero; |
duke@1 | 72 | } |
duke@1 | 73 | private static int intValue(Object x) { return ((Number)x).intValue(); } |
duke@1 | 74 | private static long longValue(Object x) { return ((Number)x).longValue(); } |
duke@1 | 75 | private static float floatValue(Object x) { return ((Number)x).floatValue(); } |
duke@1 | 76 | private static double doubleValue(Object x) { return ((Number)x).doubleValue(); } |
duke@1 | 77 | |
duke@1 | 78 | /** Fold binary or unary operation, returning constant type reflecting the |
duke@1 | 79 | * operations result. Return null if fold failed due to an |
duke@1 | 80 | * arithmetic exception. |
duke@1 | 81 | * @param opcode The operation's opcode instruction (usually a byte code), |
duke@1 | 82 | * as entered by class Symtab. |
duke@1 | 83 | * @param argtypes The operation's argument types (a list of length 1 or 2). |
duke@1 | 84 | * Argument types are assumed to have non-null constValue's. |
duke@1 | 85 | */ |
duke@1 | 86 | Type fold(int opcode, List<Type> argtypes) { |
duke@1 | 87 | int argCount = argtypes.length(); |
duke@1 | 88 | if (argCount == 1) |
duke@1 | 89 | return fold1(opcode, argtypes.head); |
duke@1 | 90 | else if (argCount == 2) |
duke@1 | 91 | return fold2(opcode, argtypes.head, argtypes.tail.head); |
duke@1 | 92 | else |
duke@1 | 93 | throw new AssertionError(); |
duke@1 | 94 | } |
duke@1 | 95 | |
duke@1 | 96 | /** Fold unary operation. |
duke@1 | 97 | * @param opcode The operation's opcode instruction (usually a byte code), |
duke@1 | 98 | * as entered by class Symtab. |
duke@1 | 99 | * opcode's ifeq to ifge are for postprocessing |
duke@1 | 100 | * xcmp; ifxx pairs of instructions. |
duke@1 | 101 | * @param operand The operation's operand type. |
duke@1 | 102 | * Argument types are assumed to have non-null constValue's. |
duke@1 | 103 | */ |
duke@1 | 104 | Type fold1(int opcode, Type operand) { |
duke@1 | 105 | try { |
duke@1 | 106 | Object od = operand.constValue(); |
duke@1 | 107 | switch (opcode) { |
duke@1 | 108 | case nop: |
duke@1 | 109 | return operand; |
duke@1 | 110 | case ineg: // unary - |
duke@1 | 111 | return syms.intType.constType(-intValue(od)); |
duke@1 | 112 | case ixor: // ~ |
duke@1 | 113 | return syms.intType.constType(~intValue(od)); |
duke@1 | 114 | case bool_not: // ! |
duke@1 | 115 | return syms.booleanType.constType(b2i(intValue(od) == 0)); |
duke@1 | 116 | case ifeq: |
duke@1 | 117 | return syms.booleanType.constType(b2i(intValue(od) == 0)); |
duke@1 | 118 | case ifne: |
duke@1 | 119 | return syms.booleanType.constType(b2i(intValue(od) != 0)); |
duke@1 | 120 | case iflt: |
duke@1 | 121 | return syms.booleanType.constType(b2i(intValue(od) < 0)); |
duke@1 | 122 | case ifgt: |
duke@1 | 123 | return syms.booleanType.constType(b2i(intValue(od) > 0)); |
duke@1 | 124 | case ifle: |
duke@1 | 125 | return syms.booleanType.constType(b2i(intValue(od) <= 0)); |
duke@1 | 126 | case ifge: |
duke@1 | 127 | return syms.booleanType.constType(b2i(intValue(od) >= 0)); |
duke@1 | 128 | |
duke@1 | 129 | case lneg: // unary - |
duke@1 | 130 | return syms.longType.constType(new Long(-longValue(od))); |
duke@1 | 131 | case lxor: // ~ |
duke@1 | 132 | return syms.longType.constType(new Long(~longValue(od))); |
duke@1 | 133 | |
duke@1 | 134 | case fneg: // unary - |
duke@1 | 135 | return syms.floatType.constType(new Float(-floatValue(od))); |
duke@1 | 136 | |
duke@1 | 137 | case dneg: // ~ |
duke@1 | 138 | return syms.doubleType.constType(new Double(-doubleValue(od))); |
duke@1 | 139 | |
duke@1 | 140 | default: |
duke@1 | 141 | return null; |
duke@1 | 142 | } |
duke@1 | 143 | } catch (ArithmeticException e) { |
duke@1 | 144 | return null; |
duke@1 | 145 | } |
duke@1 | 146 | } |
duke@1 | 147 | |
duke@1 | 148 | /** Fold binary operation. |
duke@1 | 149 | * @param opcode The operation's opcode instruction (usually a byte code), |
duke@1 | 150 | * as entered by class Symtab. |
duke@1 | 151 | * opcode's ifeq to ifge are for postprocessing |
duke@1 | 152 | * xcmp; ifxx pairs of instructions. |
duke@1 | 153 | * @param left The type of the operation's left operand. |
duke@1 | 154 | * @param right The type of the operation's right operand. |
duke@1 | 155 | */ |
duke@1 | 156 | Type fold2(int opcode, Type left, Type right) { |
duke@1 | 157 | try { |
duke@1 | 158 | if (opcode > ByteCodes.preMask) { |
duke@1 | 159 | // we are seeing a composite instruction of the form xcmp; ifxx. |
duke@1 | 160 | // In this case fold both instructions separately. |
duke@1 | 161 | Type t1 = fold2(opcode >> ByteCodes.preShift, left, right); |
duke@1 | 162 | return (t1.constValue() == null) ? t1 |
duke@1 | 163 | : fold1(opcode & ByteCodes.preMask, t1); |
duke@1 | 164 | } else { |
duke@1 | 165 | Object l = left.constValue(); |
duke@1 | 166 | Object r = right.constValue(); |
duke@1 | 167 | switch (opcode) { |
duke@1 | 168 | case iadd: |
duke@1 | 169 | return syms.intType.constType(intValue(l) + intValue(r)); |
duke@1 | 170 | case isub: |
duke@1 | 171 | return syms.intType.constType(intValue(l) - intValue(r)); |
duke@1 | 172 | case imul: |
duke@1 | 173 | return syms.intType.constType(intValue(l) * intValue(r)); |
duke@1 | 174 | case idiv: |
duke@1 | 175 | return syms.intType.constType(intValue(l) / intValue(r)); |
duke@1 | 176 | case imod: |
duke@1 | 177 | return syms.intType.constType(intValue(l) % intValue(r)); |
duke@1 | 178 | case iand: |
duke@1 | 179 | return (left.tag == BOOLEAN |
duke@1 | 180 | ? syms.booleanType : syms.intType) |
duke@1 | 181 | .constType(intValue(l) & intValue(r)); |
duke@1 | 182 | case bool_and: |
duke@1 | 183 | return syms.booleanType.constType(b2i((intValue(l) & intValue(r)) != 0)); |
duke@1 | 184 | case ior: |
duke@1 | 185 | return (left.tag == BOOLEAN |
duke@1 | 186 | ? syms.booleanType : syms.intType) |
duke@1 | 187 | .constType(intValue(l) | intValue(r)); |
duke@1 | 188 | case bool_or: |
duke@1 | 189 | return syms.booleanType.constType(b2i((intValue(l) | intValue(r)) != 0)); |
duke@1 | 190 | case ixor: |
duke@1 | 191 | return (left.tag == BOOLEAN |
duke@1 | 192 | ? syms.booleanType : syms.intType) |
duke@1 | 193 | .constType(intValue(l) ^ intValue(r)); |
duke@1 | 194 | case ishl: case ishll: |
duke@1 | 195 | return syms.intType.constType(intValue(l) << intValue(r)); |
duke@1 | 196 | case ishr: case ishrl: |
duke@1 | 197 | return syms.intType.constType(intValue(l) >> intValue(r)); |
duke@1 | 198 | case iushr: case iushrl: |
duke@1 | 199 | return syms.intType.constType(intValue(l) >>> intValue(r)); |
duke@1 | 200 | case if_icmpeq: |
duke@1 | 201 | return syms.booleanType.constType( |
duke@1 | 202 | b2i(intValue(l) == intValue(r))); |
duke@1 | 203 | case if_icmpne: |
duke@1 | 204 | return syms.booleanType.constType( |
duke@1 | 205 | b2i(intValue(l) != intValue(r))); |
duke@1 | 206 | case if_icmplt: |
duke@1 | 207 | return syms.booleanType.constType( |
duke@1 | 208 | b2i(intValue(l) < intValue(r))); |
duke@1 | 209 | case if_icmpgt: |
duke@1 | 210 | return syms.booleanType.constType( |
duke@1 | 211 | b2i(intValue(l) > intValue(r))); |
duke@1 | 212 | case if_icmple: |
duke@1 | 213 | return syms.booleanType.constType( |
duke@1 | 214 | b2i(intValue(l) <= intValue(r))); |
duke@1 | 215 | case if_icmpge: |
duke@1 | 216 | return syms.booleanType.constType( |
duke@1 | 217 | b2i(intValue(l) >= intValue(r))); |
duke@1 | 218 | |
duke@1 | 219 | case ladd: |
duke@1 | 220 | return syms.longType.constType( |
duke@1 | 221 | new Long(longValue(l) + longValue(r))); |
duke@1 | 222 | case lsub: |
duke@1 | 223 | return syms.longType.constType( |
duke@1 | 224 | new Long(longValue(l) - longValue(r))); |
duke@1 | 225 | case lmul: |
duke@1 | 226 | return syms.longType.constType( |
duke@1 | 227 | new Long(longValue(l) * longValue(r))); |
duke@1 | 228 | case ldiv: |
duke@1 | 229 | return syms.longType.constType( |
duke@1 | 230 | new Long(longValue(l) / longValue(r))); |
duke@1 | 231 | case lmod: |
duke@1 | 232 | return syms.longType.constType( |
duke@1 | 233 | new Long(longValue(l) % longValue(r))); |
duke@1 | 234 | case land: |
duke@1 | 235 | return syms.longType.constType( |
duke@1 | 236 | new Long(longValue(l) & longValue(r))); |
duke@1 | 237 | case lor: |
duke@1 | 238 | return syms.longType.constType( |
duke@1 | 239 | new Long(longValue(l) | longValue(r))); |
duke@1 | 240 | case lxor: |
duke@1 | 241 | return syms.longType.constType( |
duke@1 | 242 | new Long(longValue(l) ^ longValue(r))); |
duke@1 | 243 | case lshl: case lshll: |
duke@1 | 244 | return syms.longType.constType( |
duke@1 | 245 | new Long(longValue(l) << intValue(r))); |
duke@1 | 246 | case lshr: case lshrl: |
duke@1 | 247 | return syms.longType.constType( |
duke@1 | 248 | new Long(longValue(l) >> intValue(r))); |
duke@1 | 249 | case lushr: |
duke@1 | 250 | return syms.longType.constType( |
duke@1 | 251 | new Long(longValue(l) >>> intValue(r))); |
duke@1 | 252 | case lcmp: |
duke@1 | 253 | if (longValue(l) < longValue(r)) |
duke@1 | 254 | return syms.intType.constType(minusOne); |
duke@1 | 255 | else if (longValue(l) > longValue(r)) |
duke@1 | 256 | return syms.intType.constType(one); |
duke@1 | 257 | else |
duke@1 | 258 | return syms.intType.constType(zero); |
duke@1 | 259 | case fadd: |
duke@1 | 260 | return syms.floatType.constType( |
duke@1 | 261 | new Float(floatValue(l) + floatValue(r))); |
duke@1 | 262 | case fsub: |
duke@1 | 263 | return syms.floatType.constType( |
duke@1 | 264 | new Float(floatValue(l) - floatValue(r))); |
duke@1 | 265 | case fmul: |
duke@1 | 266 | return syms.floatType.constType( |
duke@1 | 267 | new Float(floatValue(l) * floatValue(r))); |
duke@1 | 268 | case fdiv: |
duke@1 | 269 | return syms.floatType.constType( |
duke@1 | 270 | new Float(floatValue(l) / floatValue(r))); |
duke@1 | 271 | case fmod: |
duke@1 | 272 | return syms.floatType.constType( |
duke@1 | 273 | new Float(floatValue(l) % floatValue(r))); |
duke@1 | 274 | case fcmpg: case fcmpl: |
duke@1 | 275 | if (floatValue(l) < floatValue(r)) |
duke@1 | 276 | return syms.intType.constType(minusOne); |
duke@1 | 277 | else if (floatValue(l) > floatValue(r)) |
duke@1 | 278 | return syms.intType.constType(one); |
duke@1 | 279 | else if (floatValue(l) == floatValue(r)) |
duke@1 | 280 | return syms.intType.constType(zero); |
duke@1 | 281 | else if (opcode == fcmpg) |
duke@1 | 282 | return syms.intType.constType(one); |
duke@1 | 283 | else |
duke@1 | 284 | return syms.intType.constType(minusOne); |
duke@1 | 285 | case dadd: |
duke@1 | 286 | return syms.doubleType.constType( |
duke@1 | 287 | new Double(doubleValue(l) + doubleValue(r))); |
duke@1 | 288 | case dsub: |
duke@1 | 289 | return syms.doubleType.constType( |
duke@1 | 290 | new Double(doubleValue(l) - doubleValue(r))); |
duke@1 | 291 | case dmul: |
duke@1 | 292 | return syms.doubleType.constType( |
duke@1 | 293 | new Double(doubleValue(l) * doubleValue(r))); |
duke@1 | 294 | case ddiv: |
duke@1 | 295 | return syms.doubleType.constType( |
duke@1 | 296 | new Double(doubleValue(l) / doubleValue(r))); |
duke@1 | 297 | case dmod: |
duke@1 | 298 | return syms.doubleType.constType( |
duke@1 | 299 | new Double(doubleValue(l) % doubleValue(r))); |
duke@1 | 300 | case dcmpg: case dcmpl: |
duke@1 | 301 | if (doubleValue(l) < doubleValue(r)) |
duke@1 | 302 | return syms.intType.constType(minusOne); |
duke@1 | 303 | else if (doubleValue(l) > doubleValue(r)) |
duke@1 | 304 | return syms.intType.constType(one); |
duke@1 | 305 | else if (doubleValue(l) == doubleValue(r)) |
duke@1 | 306 | return syms.intType.constType(zero); |
duke@1 | 307 | else if (opcode == dcmpg) |
duke@1 | 308 | return syms.intType.constType(one); |
duke@1 | 309 | else |
duke@1 | 310 | return syms.intType.constType(minusOne); |
duke@1 | 311 | case if_acmpeq: |
duke@1 | 312 | return syms.booleanType.constType(b2i(l.equals(r))); |
duke@1 | 313 | case if_acmpne: |
duke@1 | 314 | return syms.booleanType.constType(b2i(!l.equals(r))); |
duke@1 | 315 | case string_add: |
duke@1 | 316 | return syms.stringType.constType( |
duke@1 | 317 | left.stringValue() + right.stringValue()); |
duke@1 | 318 | default: |
duke@1 | 319 | return null; |
duke@1 | 320 | } |
duke@1 | 321 | } |
duke@1 | 322 | } catch (ArithmeticException e) { |
duke@1 | 323 | return null; |
duke@1 | 324 | } |
duke@1 | 325 | } |
duke@1 | 326 | |
duke@1 | 327 | /** Coerce constant type to target type. |
duke@1 | 328 | * @param etype The source type of the coercion, |
duke@1 | 329 | * which is assumed to be a constant type compatble with |
duke@1 | 330 | * ttype. |
duke@1 | 331 | * @param ttype The target type of the coercion. |
duke@1 | 332 | */ |
duke@1 | 333 | Type coerce(Type etype, Type ttype) { |
duke@1 | 334 | // WAS if (etype.baseType() == ttype.baseType()) |
duke@1 | 335 | if (etype.tsym.type == ttype.tsym.type) |
duke@1 | 336 | return etype; |
duke@1 | 337 | if (etype.tag <= DOUBLE) { |
duke@1 | 338 | Object n = etype.constValue(); |
duke@1 | 339 | switch (ttype.tag) { |
duke@1 | 340 | case BYTE: |
duke@1 | 341 | return syms.byteType.constType(0 + (byte)intValue(n)); |
duke@1 | 342 | case CHAR: |
duke@1 | 343 | return syms.charType.constType(0 + (char)intValue(n)); |
duke@1 | 344 | case SHORT: |
duke@1 | 345 | return syms.shortType.constType(0 + (short)intValue(n)); |
duke@1 | 346 | case INT: |
duke@1 | 347 | return syms.intType.constType(intValue(n)); |
duke@1 | 348 | case LONG: |
duke@1 | 349 | return syms.longType.constType(longValue(n)); |
duke@1 | 350 | case FLOAT: |
duke@1 | 351 | return syms.floatType.constType(floatValue(n)); |
duke@1 | 352 | case DOUBLE: |
duke@1 | 353 | return syms.doubleType.constType(doubleValue(n)); |
duke@1 | 354 | } |
duke@1 | 355 | } |
duke@1 | 356 | return ttype; |
duke@1 | 357 | } |
duke@1 | 358 | } |