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 */
23package jdk.vm.ci.code;
24
25import jdk.vm.ci.meta.AllocatableValue;
26import jdk.vm.ci.meta.JavaConstant;
27import jdk.vm.ci.meta.JavaValue;
28import jdk.vm.ci.meta.PlatformKind;
29import jdk.vm.ci.meta.Value;
30
31/**
32 * Utility class for working with the {@link Value} class and its subclasses.
33 */
34public final class ValueUtil {
35
36    public static boolean isIllegal(Value value) {
37        assert value != null;
38        return Value.ILLEGAL.equals(value);
39    }
40
41    public static boolean isIllegalJavaValue(JavaValue value) {
42        assert value != null;
43        return Value.ILLEGAL.equals(value);
44    }
45
46    public static boolean isLegal(Value value) {
47        return !isIllegal(value);
48    }
49
50    public static boolean isVirtualObject(JavaValue value) {
51        assert value != null;
52        return value instanceof VirtualObject;
53    }
54
55    public static VirtualObject asVirtualObject(JavaValue value) {
56        assert value != null;
57        return (VirtualObject) value;
58    }
59
60    public static boolean isConstantJavaValue(JavaValue value) {
61        assert value != null;
62        return value instanceof JavaConstant;
63    }
64
65    public static JavaConstant asConstantJavaValue(JavaValue value) {
66        assert value != null;
67        return (JavaConstant) value;
68    }
69
70    public static boolean isAllocatableValue(Value value) {
71        assert value != null;
72        return value instanceof AllocatableValue;
73    }
74
75    public static AllocatableValue asAllocatableValue(Value value) {
76        assert value != null;
77        return (AllocatableValue) value;
78    }
79
80    public static boolean isStackSlot(Value value) {
81        assert value != null;
82        return value instanceof StackSlot;
83    }
84
85    public static StackSlot asStackSlot(Value value) {
86        assert value != null;
87        return (StackSlot) value;
88    }
89
90    public static boolean isRegister(Value value) {
91        assert value != null;
92        return value instanceof RegisterValue;
93    }
94
95    public static Register asRegister(Value value) {
96        return asRegisterValue(value).getRegister();
97    }
98
99    public static RegisterValue asRegisterValue(Value value) {
100        assert value != null;
101        return (RegisterValue) value;
102    }
103
104    public static Register asRegister(Value value, PlatformKind kind) {
105        if (value.getPlatformKind() != kind) {
106            throw new InternalError("needed: " + kind + " got: " + value.getPlatformKind());
107        } else {
108            return asRegister(value);
109        }
110    }
111}
112