1/*
2 * Copyright (c) 2011, 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.java;
24
25public class JsrScope {
26
27    public static final JsrScope EMPTY_SCOPE = new JsrScope();
28
29    private final long scope;
30
31    private JsrScope(long scope) {
32        this.scope = scope;
33    }
34
35    public JsrScope() {
36        this.scope = 0;
37    }
38
39    public int nextReturnAddress() {
40        return (int) (scope & 0xffff);
41    }
42
43    public JsrScope push(int jsrReturnBci) {
44        if ((scope & 0xffff000000000000L) != 0) {
45            throw new JsrNotSupportedBailout("only four jsr nesting levels are supported");
46        }
47        return new JsrScope((scope << 16) | jsrReturnBci);
48    }
49
50    public boolean isEmpty() {
51        return scope == 0;
52    }
53
54    public boolean isPrefixOf(JsrScope other) {
55        return (scope & other.scope) == scope;
56    }
57
58    public JsrScope pop() {
59        return new JsrScope(scope >>> 16);
60    }
61
62    @Override
63    public int hashCode() {
64        return (int) (scope ^ (scope >>> 32));
65    }
66
67    @Override
68    public boolean equals(Object obj) {
69        if (this == obj) {
70            return true;
71        }
72        return obj != null && getClass() == obj.getClass() && scope == ((JsrScope) obj).scope;
73    }
74
75    @Override
76    public String toString() {
77        StringBuilder sb = new StringBuilder();
78        long tmp = scope;
79        sb.append(" [");
80        while (tmp != 0) {
81            sb.append(", ").append(tmp & 0xffff);
82            tmp = tmp >>> 16;
83        }
84        sb.append(']');
85        return sb.toString();
86    }
87}
88