1 /*
  2  * Copyright (c) 2015, 2017, 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.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package java.lang;
 26 
 27 final class LiveStackFrameInfo extends StackFrameInfo implements LiveStackFrame {
 28     private static Object[] EMPTY_ARRAY = new Object[0];
 29 
 30     // These flags must match the values maintained in the VM
 31     private static final int MODE_INTERPRETED = 0x01;
 32     private static final int MODE_COMPILED    = 0x02;
 33 
 34     LiveStackFrameInfo(StackWalker walker) {
 35         super(walker);
 36     }
 37 
 38     // These fields are initialized by the VM if ExtendedOption.LOCALS_AND_OPERANDS is set
 39     private Object[] monitors = EMPTY_ARRAY;
 40     private Object[] locals = EMPTY_ARRAY;
 41     private Object[] operands = EMPTY_ARRAY;
 42     private int mode = 0;
 43 
 44     @Override
 45     protected void clear() {
 46         super.clear();
 47         monitors = EMPTY_ARRAY;
 48         locals = EMPTY_ARRAY;
 49         operands = EMPTY_ARRAY;
 50         mode = 0;
 51     }
 52 
 53     @Override
 54     public Object[] getMonitors() {
 55         return monitors;
 56     }
 57 
 58     @Override
 59     public Object[] getLocals() {
 60         return locals;
 61     }
 62 
 63     @Override
 64     public Object[] getStack() {
 65         return operands;
 66     }
 67 
 68     @Override
 69     public String toString() {
 70         StringBuilder retVal = new StringBuilder(super.toString());
 71         if (mode != 0) {
 72             retVal.append("(");
 73             if ((mode & MODE_INTERPRETED) == MODE_INTERPRETED) {
 74                 retVal.append(" interpreted ");
 75             }
 76             if ((mode & MODE_COMPILED) == MODE_COMPILED) {
 77                 retVal.append(" compiled ");
 78             }
 79             retVal.append(")");
 80         }
 81         return retVal.toString();
 82     }
 83 
 84     /*
 85      * Convert primitive value to {@code PrimitiveSlot} object to represent
 86      * a local variable or an element on the operand stack of primitive type.
 87      */
 88 
 89     static PrimitiveSlot asPrimitive(int value) {
 90         return new PrimitiveSlot32(value);
 91     }
 92 
 93     static PrimitiveSlot asPrimitive(long value) {
 94         return new PrimitiveSlot64(value);
 95     }
 96 
 97     private static class PrimitiveSlot32 extends PrimitiveSlot {
 98         final int value;
 99         PrimitiveSlot32(int value) {
100             this.value = value;
101         }
102 
103         @Override
104         public int size() {
105             return 4;
106         }
107 
108         @Override
109         public int intValue() {
110             return value;
111         }
112 
113         @Override
114         public String toString() {
115             return String.valueOf(value);
116         }
117     }
118 
119     private static class PrimitiveSlot64 extends PrimitiveSlot {
120         final long value;
121         PrimitiveSlot64(long value) {
122             this.value = value;
123         }
124 
125         @Override
126         public int size() {
127             return 8;
128         }
129 
130         @Override
131         public long longValue() {
132             return value;
133         }
134 
135         @Override
136         public String toString() {
137             return String.valueOf(value);
138         }
139     }
140 }