1 /*
   2  * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package jdk.vm.ci.code;
  24 
  25 import java.util.*;
  26 
  27 import jdk.vm.ci.meta.*;
  28 
  29 /**
  30  * Utility class for working with the {@link Value} class and its subclasses.
  31  */
  32 public final class ValueUtil {
  33 
  34     public static boolean isIllegal(Value value) {
  35         assert value != null;
  36         return Value.ILLEGAL.equals(value);
  37     }
  38 
  39     public static boolean isIllegalJavaValue(JavaValue value) {
  40         assert value != null;
  41         return Value.ILLEGAL.equals(value);
  42     }
  43 
  44     public static boolean isLegal(Value value) {
  45         return !isIllegal(value);
  46     }
  47 
  48     public static boolean isVirtualObject(JavaValue value) {
  49         assert value != null;
  50         return value instanceof VirtualObject;
  51     }
  52 
  53     public static VirtualObject asVirtualObject(JavaValue value) {
  54         assert value != null;
  55         return (VirtualObject) value;
  56     }
  57 
  58     public static boolean isConstantJavaValue(JavaValue value) {
  59         assert value != null;
  60         return value instanceof JavaConstant;
  61     }
  62 
  63     public static boolean isAllocatableValue(Value value) {
  64         assert value != null;
  65         return value instanceof AllocatableValue;
  66     }
  67 
  68     public static AllocatableValue asAllocatableValue(Value value) {
  69         assert value != null;
  70         return (AllocatableValue) value;
  71     }
  72 
  73     public static boolean isStackSlot(Value value) {
  74         assert value != null;
  75         return value instanceof StackSlot;
  76     }
  77 
  78     public static StackSlot asStackSlot(Value value) {
  79         assert value != null;
  80         return (StackSlot) value;
  81     }
  82 
  83     public static boolean isStackSlotValue(Value value) {
  84         assert value != null;
  85         return value instanceof StackSlotValue;
  86     }
  87 
  88     public static StackSlotValue asStackSlotValue(Value value) {
  89         assert value != null;
  90         return (StackSlotValue) value;
  91     }
  92 
  93     public static boolean isVirtualStackSlot(Value value) {
  94         assert value != null;
  95         return value instanceof VirtualStackSlot;
  96     }
  97 
  98     public static VirtualStackSlot asVirtualStackSlot(Value value) {
  99         assert value != null;
 100         return (VirtualStackSlot) value;
 101     }
 102 
 103     public static boolean isRegister(Value value) {
 104         assert value != null;
 105         return value instanceof RegisterValue;
 106     }
 107 
 108     public static Register asRegister(Value value) {
 109         return asRegisterValue(value).getRegister();
 110     }
 111 
 112     public static RegisterValue asRegisterValue(Value value) {
 113         assert value != null;
 114         return (RegisterValue) value;
 115     }
 116 
 117     public static Register asRegister(Value value, PlatformKind kind) {
 118         if (value.getPlatformKind() != kind) {
 119             throw new InternalError("needed: " + kind + " got: " + value.getPlatformKind());
 120         } else {
 121             return asRegister(value);
 122         }
 123     }
 124 
 125     public static boolean sameRegister(Value v1, Value v2) {
 126         return isRegister(v1) && isRegister(v2) && asRegister(v1).equals(asRegister(v2));
 127     }
 128 
 129     public static boolean sameRegister(Value v1, Value v2, Value v3) {
 130         return sameRegister(v1, v2) && sameRegister(v1, v3);
 131     }
 132 
 133     /**
 134      * Checks if all the provided values are different physical registers. The parameters can be
 135      * either {@link Register registers}, {@link Value values} or arrays of them. All values that
 136      * are not {@link RegisterValue registers} are ignored.
 137      */
 138     public static boolean differentRegisters(Object... values) {
 139         List<Register> registers = collectRegisters(values, new ArrayList<Register>());
 140         for (int i = 1; i < registers.size(); i++) {
 141             Register r1 = registers.get(i);
 142             for (int j = 0; j < i; j++) {
 143                 Register r2 = registers.get(j);
 144                 if (r1.equals(r2)) {
 145                     return false;
 146                 }
 147             }
 148         }
 149         return true;
 150     }
 151 
 152     private static List<Register> collectRegisters(Object[] values, List<Register> registers) {
 153         for (Object o : values) {
 154             if (o instanceof Register) {
 155                 registers.add((Register) o);
 156             } else if (o instanceof Value) {
 157                 if (isRegister((Value) o)) {
 158                     registers.add(asRegister((Value) o));
 159                 }
 160             } else if (o instanceof Object[]) {
 161                 collectRegisters((Object[]) o, registers);
 162             } else {
 163                 throw new IllegalArgumentException("Not a Register or Value: " + o);
 164             }
 165         }
 166         return registers;
 167     }
 168 
 169     /**
 170      * Subtract sets of registers (x - y).
 171      *
 172      * @param x a set of register to subtract from.
 173      * @param y a set of registers to subtract.
 174      * @return resulting set of registers (x - y).
 175      */
 176     public static Value[] subtractRegisters(Value[] x, Value[] y) {
 177         ArrayList<Value> result = new ArrayList<>(x.length);
 178         for (Value i : x) {
 179             boolean append = true;
 180             for (Value j : y) {
 181                 if (ValueUtil.sameRegister(i, j)) {
 182                     append = false;
 183                     break;
 184                 }
 185             }
 186             if (append) {
 187                 result.add(i);
 188             }
 189         }
 190         Value[] resultArray = new Value[result.size()];
 191         return result.toArray(resultArray);
 192     }
 193 }