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