001/* 002 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package jdk.internal.jvmci.code; 024 025import java.util.*; 026 027import jdk.internal.jvmci.meta.*; 028 029/** 030 * Utility class for working with the {@link Value} class and its subclasses. 031 */ 032public final class ValueUtil { 033 034 public static boolean isIllegal(Value value) { 035 assert value != null; 036 return Value.ILLEGAL.equals(value); 037 } 038 039 public static boolean isLegal(Value value) { 040 return !isIllegal(value); 041 } 042 043 public static boolean isVirtualObject(Value value) { 044 assert value != null; 045 return value instanceof VirtualObject; 046 } 047 048 public static VirtualObject asVirtualObject(Value value) { 049 assert value != null; 050 return (VirtualObject) value; 051 } 052 053 public static boolean isConstant(Value value) { 054 assert value != null; 055 return value instanceof JavaConstant; 056 } 057 058 public static JavaConstant asConstant(Value value) { 059 assert value != null; 060 return (JavaConstant) value; 061 } 062 063 public static boolean isAllocatableValue(Value value) { 064 assert value != null; 065 return value instanceof AllocatableValue; 066 } 067 068 public static AllocatableValue asAllocatableValue(Value value) { 069 assert value != null; 070 return (AllocatableValue) value; 071 } 072 073 public static boolean isStackSlot(Value value) { 074 assert value != null; 075 return value instanceof StackSlot; 076 } 077 078 public static StackSlot asStackSlot(Value value) { 079 assert value != null; 080 return (StackSlot) value; 081 } 082 083 public static boolean isStackSlotValue(Value value) { 084 assert value != null; 085 return value instanceof StackSlotValue; 086 } 087 088 public static StackSlotValue asStackSlotValue(Value value) { 089 assert value != null; 090 return (StackSlotValue) value; 091 } 092 093 public static boolean isVirtualStackSlot(Value value) { 094 assert value != null; 095 return value instanceof VirtualStackSlot; 096 } 097 098 public static VirtualStackSlot asVirtualStackSlot(Value value) { 099 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 assert value != null; 110 return ((RegisterValue) value).getRegister(); 111 } 112 113 public static Register asIntReg(Value value) { 114 if (value.getKind().getStackKind() != Kind.Int) { 115 throw new InternalError("needed Int got: " + value.getKind()); 116 } else { 117 return asRegister(value); 118 } 119 } 120 121 public static Register asLongReg(Value value) { 122 if (value.getKind() != Kind.Long) { 123 throw new InternalError("needed Long got: " + value.getKind()); 124 } else { 125 return asRegister(value); 126 } 127 } 128 129 public static Register asObjectReg(Value value) { 130 assert value.getKind() == Kind.Object : value.getKind(); 131 return asRegister(value); 132 } 133 134 public static Register asFloatReg(Value value) { 135 assert value.getKind() == Kind.Float : value.getKind(); 136 return asRegister(value); 137 } 138 139 public static Register asDoubleReg(Value value) { 140 assert value.getKind() == Kind.Double : value.getKind(); 141 return asRegister(value); 142 } 143 144 public static boolean sameRegister(Value v1, Value v2) { 145 return isRegister(v1) && isRegister(v2) && asRegister(v1).equals(asRegister(v2)); 146 } 147 148 public static boolean sameRegister(Value v1, Value v2, Value v3) { 149 return sameRegister(v1, v2) && sameRegister(v1, v3); 150 } 151 152 /** 153 * Checks if all the provided values are different physical registers. The parameters can be 154 * either {@link Register registers}, {@link Value values} or arrays of them. All values that 155 * are not {@link RegisterValue registers} are ignored. 156 */ 157 public static boolean differentRegisters(Object... values) { 158 List<Register> registers = collectRegisters(values, new ArrayList<Register>()); 159 for (int i = 1; i < registers.size(); i++) { 160 Register r1 = registers.get(i); 161 for (int j = 0; j < i; j++) { 162 Register r2 = registers.get(j); 163 if (r1.equals(r2)) { 164 return false; 165 } 166 } 167 } 168 return true; 169 } 170 171 private static List<Register> collectRegisters(Object[] values, List<Register> registers) { 172 for (Object o : values) { 173 if (o instanceof Register) { 174 registers.add((Register) o); 175 } else if (o instanceof Value) { 176 if (isRegister((Value) o)) { 177 registers.add(asRegister((Value) o)); 178 } 179 } else if (o instanceof Object[]) { 180 collectRegisters((Object[]) o, registers); 181 } else { 182 throw new IllegalArgumentException("Not a Register or Value: " + o); 183 } 184 } 185 return registers; 186 } 187}