1/*
2 * Copyright (c) 2011, 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 org.graalvm.compiler.nodes;
24
25import static java.lang.Character.toLowerCase;
26
27import java.util.ArrayList;
28import java.util.Collection;
29
30import org.graalvm.compiler.graph.Node;
31import org.graalvm.compiler.nodeinfo.Verbosity;
32import org.graalvm.compiler.nodes.memory.MemoryNode;
33
34import jdk.vm.ci.meta.JavaKind;
35
36public class ValueNodeUtil {
37
38    public static ValueNode assertKind(JavaKind kind, ValueNode x) {
39        assert x != null && x.getStackKind() == kind : "kind=" + kind + ", value=" + x + ((x == null) ? "" : ", value.kind=" + x.getStackKind());
40        return x;
41    }
42
43    public static RuntimeException shouldNotReachHere(String msg) {
44        throw new InternalError("should not reach here: " + msg);
45    }
46
47    public static RuntimeException shouldNotReachHere() {
48        throw new InternalError("should not reach here");
49    }
50
51    public static ValueNode assertLong(ValueNode x) {
52        assert x != null && (x.getStackKind() == JavaKind.Long);
53        return x;
54    }
55
56    public static ValueNode assertInt(ValueNode x) {
57        assert x != null && (x.getStackKind() == JavaKind.Int);
58        return x;
59    }
60
61    public static ValueNode assertFloat(ValueNode x) {
62        assert x != null && (x.getStackKind() == JavaKind.Float);
63        return x;
64    }
65
66    public static ValueNode assertObject(ValueNode x) {
67        assert x != null && (x.getStackKind() == JavaKind.Object);
68        return x;
69    }
70
71    public static ValueNode assertDouble(ValueNode x) {
72        assert x != null && (x.getStackKind() == JavaKind.Double);
73        return x;
74    }
75
76    public static void assertHigh(ValueNode x) {
77        assert x == null;
78    }
79
80    @SuppressWarnings("unchecked")
81    public static <T extends Node> Collection<T> filter(Iterable<Node> nodes, Class<T> clazz) {
82        ArrayList<T> phis = new ArrayList<>();
83        for (Node node : nodes) {
84            if (clazz.isInstance(node)) {
85                phis.add((T) node);
86            }
87        }
88        return phis;
89    }
90
91    /**
92     * Converts a given instruction to a value string. The representation of an node as a value is
93     * formed by concatenating the {@linkplain jdk.vm.ci.meta.JavaKind#getTypeChar character}
94     * denoting its {@linkplain ValueNode#getStackKind kind} and its id. For example, {@code "i13"}.
95     *
96     * @param value the instruction to convert to a value string. If {@code value == null}, then "-"
97     *            is returned.
98     * @return the instruction representation as a string
99     */
100    public static String valueString(ValueNode value) {
101        return (value == null) ? "-" : ("" + toLowerCase(value.getStackKind().getTypeChar()) + value.toString(Verbosity.Id));
102    }
103
104    public static ValueNode asNode(MemoryNode node) {
105        if (node == null) {
106            return null;
107        } else {
108            return node.asNode();
109        }
110    }
111}
112