001/*
002 * Copyright (c) 2011, 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 com.oracle.graal.java;
024
025public class JsrScope {
026
027    public static final JsrScope EMPTY_SCOPE = new JsrScope();
028
029    private final long scope;
030
031    private JsrScope(long scope) {
032        this.scope = scope;
033    }
034
035    public JsrScope() {
036        this.scope = 0;
037    }
038
039    public int nextReturnAddress() {
040        return (int) (scope & 0xffff);
041    }
042
043    public JsrScope push(int jsrReturnBci) {
044        if ((scope & 0xffff000000000000L) != 0) {
045            throw new JsrNotSupportedBailout("only four jsr nesting levels are supported");
046        }
047        return new JsrScope((scope << 16) | jsrReturnBci);
048    }
049
050    public boolean isEmpty() {
051        return scope == 0;
052    }
053
054    public boolean isPrefixOf(JsrScope other) {
055        return (scope & other.scope) == scope;
056    }
057
058    public JsrScope pop() {
059        return new JsrScope(scope >>> 16);
060    }
061
062    @Override
063    public int hashCode() {
064        return (int) (scope ^ (scope >>> 32));
065    }
066
067    @Override
068    public boolean equals(Object obj) {
069        if (this == obj) {
070            return true;
071        }
072        return obj != null && getClass() == obj.getClass() && scope == ((JsrScope) obj).scope;
073    }
074
075    @Override
076    public String toString() {
077        StringBuilder sb = new StringBuilder();
078        long tmp = scope;
079        sb.append(" [");
080        while (tmp != 0) {
081            sb.append(", ").append(tmp & 0xffff);
082            tmp = tmp >>> 16;
083        }
084        sb.append(']');
085        return sb.toString();
086    }
087}