1 /*
2  * Copyright (c) 2000, 2009, 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 sun.jvm.hotspot.runtime;
26 
27 import java.io.*;
28 import sun.jvm.hotspot.debugger.*;
29 import sun.jvm.hotspot.utilities.*;
30 
31 public class StackValue {
32   private int       type;
33   private OopHandle handleValue;
34   private long      integerValue;
35 
StackValue()36   public StackValue() {
37     type = BasicType.getTConflict();
38   }
39 
StackValue(OopHandle h, long scalar_replaced)40   public StackValue(OopHandle h, long scalar_replaced) {
41     handleValue = h;
42     type = BasicType.getTObject();
43     integerValue = scalar_replaced;
44     Assert.that(integerValue == 0 || handleValue == null, "not null object should not be marked as scalar replaced");
45   }
46 
StackValue(long i)47   public StackValue(long i) {
48     integerValue = i;
49     type = BasicType.getTInt();
50   }
51 
52   /** This returns one of the "enum" values in BasicType.java */
getType()53   public int getType() {
54     return type;
55   }
56 
getObject()57   public OopHandle getObject() {
58     if (Assert.ASSERTS_ENABLED) {
59       Assert.that(type == BasicType.getTObject(), "type check");
60     }
61     return handleValue;
62   }
63 
objIsScalarReplaced()64   boolean objIsScalarReplaced() {
65     if (Assert.ASSERTS_ENABLED) {
66       Assert.that(type == BasicType.getTObject(), "type check");
67     }
68     return integerValue != 0;
69   }
70 
getInteger()71   public long getInteger() {
72     if (Assert.ASSERTS_ENABLED) {
73       Assert.that(type == BasicType.getTInt(), "type check");
74     }
75     return integerValue;
76   }
77 
equals(Object arg)78   public boolean equals(Object arg) {
79     if (arg == null) {
80       return false;
81     }
82 
83     if (!arg.getClass().equals(getClass())) {
84       return false;
85     }
86 
87     StackValue sv = (StackValue) arg;
88     if (type != sv.type) {
89       return false;
90     }
91     if (type == BasicType.getTObject()) {
92       return handleValue.equals(sv.handleValue);
93     } else if (type == BasicType.getTInt()) {
94       return (integerValue == sv.integerValue);
95     } else {
96       // Conflict type (not yet used)
97       return true;
98     }
99   }
100 
hashCode()101   public int hashCode() {
102     if (type == BasicType.getTObject()) {
103       return handleValue != null ? handleValue.hashCode() : 5;
104     } else {
105       // Returns 0 for conflict type
106       return (int) integerValue;
107     }
108   }
109 
print()110   public void print() {
111     printOn(System.out);
112   }
113 
printOn(PrintStream tty)114   public void printOn(PrintStream tty) {
115     if (type == BasicType.getTInt()) {
116       tty.print(integerValue + " (long) " + (int) integerValue + " (int)");
117     } else if (type == BasicType.getTObject()) {
118       tty.print("<" + handleValue + ">");
119     } else if (type == BasicType.getTConflict()) {
120       tty.print("conflict");
121     } else {
122       throw new RuntimeException("should not reach here");
123     }
124   }
125 }
126