src/share/classes/com/sun/tools/javac/comp/ConstFold.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/ConstFold.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,357 @@
     1.4 +/*
     1.5 + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.comp;
    1.30 +
    1.31 +import com.sun.tools.javac.code.*;
    1.32 +import com.sun.tools.javac.jvm.*;
    1.33 +import com.sun.tools.javac.util.*;
    1.34 +
    1.35 +import static com.sun.tools.javac.code.TypeTag.BOOLEAN;
    1.36 +
    1.37 +import static com.sun.tools.javac.jvm.ByteCodes.*;
    1.38 +
    1.39 +/** Helper class for constant folding, used by the attribution phase.
    1.40 + *  This class is marked strictfp as mandated by JLS 15.4.
    1.41 + *
    1.42 + *  <p><b>This is NOT part of any supported API.
    1.43 + *  If you write code that depends on this, you do so at your own risk.
    1.44 + *  This code and its internal interfaces are subject to change or
    1.45 + *  deletion without notice.</b>
    1.46 + */
    1.47 +strictfp class ConstFold {
    1.48 +    protected static final Context.Key<ConstFold> constFoldKey =
    1.49 +        new Context.Key<ConstFold>();
    1.50 +
    1.51 +    private Symtab syms;
    1.52 +
    1.53 +    public static ConstFold instance(Context context) {
    1.54 +        ConstFold instance = context.get(constFoldKey);
    1.55 +        if (instance == null)
    1.56 +            instance = new ConstFold(context);
    1.57 +        return instance;
    1.58 +    }
    1.59 +
    1.60 +    private ConstFold(Context context) {
    1.61 +        context.put(constFoldKey, this);
    1.62 +
    1.63 +        syms = Symtab.instance(context);
    1.64 +    }
    1.65 +
    1.66 +    static final Integer minusOne = -1;
    1.67 +    static final Integer zero     = 0;
    1.68 +    static final Integer one      = 1;
    1.69 +
    1.70 +   /** Convert boolean to integer (true = 1, false = 0).
    1.71 +    */
    1.72 +    private static Integer b2i(boolean b) {
    1.73 +        return b ? one : zero;
    1.74 +    }
    1.75 +    private static int intValue(Object x) { return ((Number)x).intValue(); }
    1.76 +    private static long longValue(Object x) { return ((Number)x).longValue(); }
    1.77 +    private static float floatValue(Object x) { return ((Number)x).floatValue(); }
    1.78 +    private static double doubleValue(Object x) { return ((Number)x).doubleValue(); }
    1.79 +
    1.80 +    /** Fold binary or unary operation, returning constant type reflecting the
    1.81 +     *  operations result. Return null if fold failed due to an
    1.82 +     *  arithmetic exception.
    1.83 +     *  @param opcode    The operation's opcode instruction (usually a byte code),
    1.84 +     *                   as entered by class Symtab.
    1.85 +     *  @param argtypes  The operation's argument types (a list of length 1 or 2).
    1.86 +     *                   Argument types are assumed to have non-null constValue's.
    1.87 +     */
    1.88 +    Type fold(int opcode, List<Type> argtypes) {
    1.89 +        int argCount = argtypes.length();
    1.90 +        if (argCount == 1)
    1.91 +            return fold1(opcode, argtypes.head);
    1.92 +        else if (argCount == 2)
    1.93 +            return fold2(opcode, argtypes.head, argtypes.tail.head);
    1.94 +        else
    1.95 +            throw new AssertionError();
    1.96 +    }
    1.97 +
    1.98 +    /** Fold unary operation.
    1.99 +     *  @param opcode    The operation's opcode instruction (usually a byte code),
   1.100 +     *                   as entered by class Symtab.
   1.101 +     *                   opcode's ifeq to ifge are for postprocessing
   1.102 +     *                   xcmp; ifxx pairs of instructions.
   1.103 +     *  @param operand   The operation's operand type.
   1.104 +     *                   Argument types are assumed to have non-null constValue's.
   1.105 +     */
   1.106 +    Type fold1(int opcode, Type operand) {
   1.107 +        try {
   1.108 +            Object od = operand.constValue();
   1.109 +            switch (opcode) {
   1.110 +            case nop:
   1.111 +                return operand;
   1.112 +            case ineg: // unary -
   1.113 +                return syms.intType.constType(-intValue(od));
   1.114 +            case ixor: // ~
   1.115 +                return syms.intType.constType(~intValue(od));
   1.116 +            case bool_not: // !
   1.117 +                return syms.booleanType.constType(b2i(intValue(od) == 0));
   1.118 +            case ifeq:
   1.119 +                return syms.booleanType.constType(b2i(intValue(od) == 0));
   1.120 +            case ifne:
   1.121 +                return syms.booleanType.constType(b2i(intValue(od) != 0));
   1.122 +            case iflt:
   1.123 +                return syms.booleanType.constType(b2i(intValue(od) < 0));
   1.124 +            case ifgt:
   1.125 +                return syms.booleanType.constType(b2i(intValue(od) > 0));
   1.126 +            case ifle:
   1.127 +                return syms.booleanType.constType(b2i(intValue(od) <= 0));
   1.128 +            case ifge:
   1.129 +                return syms.booleanType.constType(b2i(intValue(od) >= 0));
   1.130 +
   1.131 +            case lneg: // unary -
   1.132 +                return syms.longType.constType(new Long(-longValue(od)));
   1.133 +            case lxor: // ~
   1.134 +                return syms.longType.constType(new Long(~longValue(od)));
   1.135 +
   1.136 +            case fneg: // unary -
   1.137 +                return syms.floatType.constType(new Float(-floatValue(od)));
   1.138 +
   1.139 +            case dneg: // ~
   1.140 +                return syms.doubleType.constType(new Double(-doubleValue(od)));
   1.141 +
   1.142 +            default:
   1.143 +                return null;
   1.144 +            }
   1.145 +        } catch (ArithmeticException e) {
   1.146 +            return null;
   1.147 +        }
   1.148 +    }
   1.149 +
   1.150 +    /** Fold binary operation.
   1.151 +     *  @param opcode    The operation's opcode instruction (usually a byte code),
   1.152 +     *                   as entered by class Symtab.
   1.153 +     *                   opcode's ifeq to ifge are for postprocessing
   1.154 +     *                   xcmp; ifxx pairs of instructions.
   1.155 +     *  @param left      The type of the operation's left operand.
   1.156 +     *  @param right     The type of the operation's right operand.
   1.157 +     */
   1.158 +    Type fold2(int opcode, Type left, Type right) {
   1.159 +        try {
   1.160 +            if (opcode > ByteCodes.preMask) {
   1.161 +                // we are seeing a composite instruction of the form xcmp; ifxx.
   1.162 +                // In this case fold both instructions separately.
   1.163 +                Type t1 = fold2(opcode >> ByteCodes.preShift, left, right);
   1.164 +                return (t1.constValue() == null) ? t1
   1.165 +                    : fold1(opcode & ByteCodes.preMask, t1);
   1.166 +            } else {
   1.167 +                Object l = left.constValue();
   1.168 +                Object r = right.constValue();
   1.169 +                switch (opcode) {
   1.170 +                case iadd:
   1.171 +                    return syms.intType.constType(intValue(l) + intValue(r));
   1.172 +                case isub:
   1.173 +                    return syms.intType.constType(intValue(l) - intValue(r));
   1.174 +                case imul:
   1.175 +                    return syms.intType.constType(intValue(l) * intValue(r));
   1.176 +                case idiv:
   1.177 +                    return syms.intType.constType(intValue(l) / intValue(r));
   1.178 +                case imod:
   1.179 +                    return syms.intType.constType(intValue(l) % intValue(r));
   1.180 +                case iand:
   1.181 +                    return (left.hasTag(BOOLEAN)
   1.182 +                      ? syms.booleanType : syms.intType)
   1.183 +                      .constType(intValue(l) & intValue(r));
   1.184 +                case bool_and:
   1.185 +                    return syms.booleanType.constType(b2i((intValue(l) & intValue(r)) != 0));
   1.186 +                case ior:
   1.187 +                    return (left.hasTag(BOOLEAN)
   1.188 +                      ? syms.booleanType : syms.intType)
   1.189 +                      .constType(intValue(l) | intValue(r));
   1.190 +                case bool_or:
   1.191 +                    return syms.booleanType.constType(b2i((intValue(l) | intValue(r)) != 0));
   1.192 +                case ixor:
   1.193 +                    return (left.hasTag(BOOLEAN)
   1.194 +                      ? syms.booleanType : syms.intType)
   1.195 +                      .constType(intValue(l) ^ intValue(r));
   1.196 +                case ishl: case ishll:
   1.197 +                    return syms.intType.constType(intValue(l) << intValue(r));
   1.198 +                case ishr: case ishrl:
   1.199 +                    return syms.intType.constType(intValue(l) >> intValue(r));
   1.200 +                case iushr: case iushrl:
   1.201 +                    return syms.intType.constType(intValue(l) >>> intValue(r));
   1.202 +                case if_icmpeq:
   1.203 +                    return syms.booleanType.constType(
   1.204 +                        b2i(intValue(l) == intValue(r)));
   1.205 +                case if_icmpne:
   1.206 +                    return syms.booleanType.constType(
   1.207 +                        b2i(intValue(l) != intValue(r)));
   1.208 +                case if_icmplt:
   1.209 +                    return syms.booleanType.constType(
   1.210 +                        b2i(intValue(l) < intValue(r)));
   1.211 +                case if_icmpgt:
   1.212 +                    return syms.booleanType.constType(
   1.213 +                        b2i(intValue(l) > intValue(r)));
   1.214 +                case if_icmple:
   1.215 +                    return syms.booleanType.constType(
   1.216 +                        b2i(intValue(l) <= intValue(r)));
   1.217 +                case if_icmpge:
   1.218 +                    return syms.booleanType.constType(
   1.219 +                        b2i(intValue(l) >= intValue(r)));
   1.220 +
   1.221 +                case ladd:
   1.222 +                    return syms.longType.constType(
   1.223 +                        new Long(longValue(l) + longValue(r)));
   1.224 +                case lsub:
   1.225 +                    return syms.longType.constType(
   1.226 +                        new Long(longValue(l) - longValue(r)));
   1.227 +                case lmul:
   1.228 +                    return syms.longType.constType(
   1.229 +                        new Long(longValue(l) * longValue(r)));
   1.230 +                case ldiv:
   1.231 +                    return syms.longType.constType(
   1.232 +                        new Long(longValue(l) / longValue(r)));
   1.233 +                case lmod:
   1.234 +                    return syms.longType.constType(
   1.235 +                        new Long(longValue(l) % longValue(r)));
   1.236 +                case land:
   1.237 +                    return syms.longType.constType(
   1.238 +                        new Long(longValue(l) & longValue(r)));
   1.239 +                case lor:
   1.240 +                    return syms.longType.constType(
   1.241 +                        new Long(longValue(l) | longValue(r)));
   1.242 +                case lxor:
   1.243 +                    return syms.longType.constType(
   1.244 +                        new Long(longValue(l) ^ longValue(r)));
   1.245 +                case lshl: case lshll:
   1.246 +                    return syms.longType.constType(
   1.247 +                        new Long(longValue(l) << intValue(r)));
   1.248 +                case lshr: case lshrl:
   1.249 +                    return syms.longType.constType(
   1.250 +                        new Long(longValue(l) >> intValue(r)));
   1.251 +                case lushr:
   1.252 +                    return syms.longType.constType(
   1.253 +                        new Long(longValue(l) >>> intValue(r)));
   1.254 +                case lcmp:
   1.255 +                    if (longValue(l) < longValue(r))
   1.256 +                        return syms.intType.constType(minusOne);
   1.257 +                    else if (longValue(l) > longValue(r))
   1.258 +                        return syms.intType.constType(one);
   1.259 +                    else
   1.260 +                        return syms.intType.constType(zero);
   1.261 +                case fadd:
   1.262 +                    return syms.floatType.constType(
   1.263 +                        new Float(floatValue(l) + floatValue(r)));
   1.264 +                case fsub:
   1.265 +                    return syms.floatType.constType(
   1.266 +                        new Float(floatValue(l) - floatValue(r)));
   1.267 +                case fmul:
   1.268 +                    return syms.floatType.constType(
   1.269 +                        new Float(floatValue(l) * floatValue(r)));
   1.270 +                case fdiv:
   1.271 +                    return syms.floatType.constType(
   1.272 +                        new Float(floatValue(l) / floatValue(r)));
   1.273 +                case fmod:
   1.274 +                    return syms.floatType.constType(
   1.275 +                        new Float(floatValue(l) % floatValue(r)));
   1.276 +                case fcmpg: case fcmpl:
   1.277 +                    if (floatValue(l) < floatValue(r))
   1.278 +                        return syms.intType.constType(minusOne);
   1.279 +                    else if (floatValue(l) > floatValue(r))
   1.280 +                        return syms.intType.constType(one);
   1.281 +                    else if (floatValue(l) == floatValue(r))
   1.282 +                        return syms.intType.constType(zero);
   1.283 +                    else if (opcode == fcmpg)
   1.284 +                        return syms.intType.constType(one);
   1.285 +                    else
   1.286 +                        return syms.intType.constType(minusOne);
   1.287 +                case dadd:
   1.288 +                    return syms.doubleType.constType(
   1.289 +                        new Double(doubleValue(l) + doubleValue(r)));
   1.290 +                case dsub:
   1.291 +                    return syms.doubleType.constType(
   1.292 +                        new Double(doubleValue(l) - doubleValue(r)));
   1.293 +                case dmul:
   1.294 +                    return syms.doubleType.constType(
   1.295 +                        new Double(doubleValue(l) * doubleValue(r)));
   1.296 +                case ddiv:
   1.297 +                    return syms.doubleType.constType(
   1.298 +                        new Double(doubleValue(l) / doubleValue(r)));
   1.299 +                case dmod:
   1.300 +                    return syms.doubleType.constType(
   1.301 +                        new Double(doubleValue(l) % doubleValue(r)));
   1.302 +                case dcmpg: case dcmpl:
   1.303 +                    if (doubleValue(l) < doubleValue(r))
   1.304 +                        return syms.intType.constType(minusOne);
   1.305 +                    else if (doubleValue(l) > doubleValue(r))
   1.306 +                        return syms.intType.constType(one);
   1.307 +                    else if (doubleValue(l) == doubleValue(r))
   1.308 +                        return syms.intType.constType(zero);
   1.309 +                    else if (opcode == dcmpg)
   1.310 +                        return syms.intType.constType(one);
   1.311 +                    else
   1.312 +                        return syms.intType.constType(minusOne);
   1.313 +                case if_acmpeq:
   1.314 +                    return syms.booleanType.constType(b2i(l.equals(r)));
   1.315 +                case if_acmpne:
   1.316 +                    return syms.booleanType.constType(b2i(!l.equals(r)));
   1.317 +                case string_add:
   1.318 +                    return syms.stringType.constType(
   1.319 +                        left.stringValue() + right.stringValue());
   1.320 +                default:
   1.321 +                    return null;
   1.322 +                }
   1.323 +            }
   1.324 +        } catch (ArithmeticException e) {
   1.325 +            return null;
   1.326 +        }
   1.327 +    }
   1.328 +
   1.329 +    /** Coerce constant type to target type.
   1.330 +     *  @param etype      The source type of the coercion,
   1.331 +     *                    which is assumed to be a constant type compatible with
   1.332 +     *                    ttype.
   1.333 +     *  @param ttype      The target type of the coercion.
   1.334 +     */
   1.335 +     Type coerce(Type etype, Type ttype) {
   1.336 +         // WAS if (etype.baseType() == ttype.baseType())
   1.337 +         if (etype.tsym.type == ttype.tsym.type)
   1.338 +             return etype;
   1.339 +         if (etype.isNumeric()) {
   1.340 +             Object n = etype.constValue();
   1.341 +             switch (ttype.getTag()) {
   1.342 +             case BYTE:
   1.343 +                 return syms.byteType.constType(0 + (byte)intValue(n));
   1.344 +             case CHAR:
   1.345 +                 return syms.charType.constType(0 + (char)intValue(n));
   1.346 +             case SHORT:
   1.347 +                 return syms.shortType.constType(0 + (short)intValue(n));
   1.348 +             case INT:
   1.349 +                 return syms.intType.constType(intValue(n));
   1.350 +             case LONG:
   1.351 +                 return syms.longType.constType(longValue(n));
   1.352 +             case FLOAT:
   1.353 +                 return syms.floatType.constType(floatValue(n));
   1.354 +             case DOUBLE:
   1.355 +                 return syms.doubleType.constType(doubleValue(n));
   1.356 +             }
   1.357 +         }
   1.358 +         return ttype;
   1.359 +     }
   1.360 +}

mercurial