1/*
2 * Copyright (c) 1999, 2019, 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#ifndef SHARE_CI_CICONSTANT_HPP
26#define SHARE_CI_CICONSTANT_HPP
27
28#include "ci/ciClassList.hpp"
29#include "ci/ciNullObject.hpp"
30
31// ciConstant
32//
33// This class represents a constant value.
34class ciConstant {
35 friend class VMStructs;
36private:
37 friend class ciEnv;
38 friend class ciField;
39
40 BasicType _type;
41 union {
42 jint _int;
43 jlong _long;
44 jfloat _float;
45 jdouble _double;
46 ciObject* _object;
47 } _value;
48
49public:
50
51 ciConstant() {
52 _type = T_ILLEGAL; _value._long = -1;
53 }
54 ciConstant(BasicType type, jint value) {
55 assert(type != T_LONG && type != T_DOUBLE && type != T_FLOAT,
56 "using the wrong ciConstant constructor");
57 _type = type; _value._int = value;
58 }
59 ciConstant(jlong value) {
60 _type = T_LONG; _value._long = value;
61 }
62 ciConstant(jfloat value) {
63 _type = T_FLOAT; _value._float = value;
64 }
65 ciConstant(jdouble value) {
66 _type = T_DOUBLE; _value._double = value;
67 }
68 ciConstant(BasicType type, ciObject* p) {
69 _type = type; _value._object = p;
70 }
71
72 BasicType basic_type() const { return _type; }
73
74 jboolean as_boolean() {
75 assert(basic_type() == T_BOOLEAN, "wrong type");
76 return (jboolean)_value._int;
77 }
78 jchar as_char() {
79 assert(basic_type() == T_CHAR, "wrong type");
80 return (jchar)_value._int;
81 }
82 jbyte as_byte() {
83 assert(basic_type() == T_BYTE, "wrong type");
84 return (jbyte)_value._int;
85 }
86 jshort as_short() {
87 assert(basic_type() == T_SHORT, "wrong type");
88 return (jshort)_value._int;
89 }
90 jint as_int() {
91 assert(basic_type() == T_BOOLEAN || basic_type() == T_CHAR ||
92 basic_type() == T_BYTE || basic_type() == T_SHORT ||
93 basic_type() == T_INT, "wrong type");
94 return _value._int;
95 }
96 jlong as_long() {
97 assert(basic_type() == T_LONG, "wrong type");
98 return _value._long;
99 }
100 jfloat as_float() {
101 assert(basic_type() == T_FLOAT, "wrong type");
102 return _value._float;
103 }
104 jdouble as_double() {
105 assert(basic_type() == T_DOUBLE, "wrong type");
106 return _value._double;
107 }
108 ciObject* as_object() const {
109 assert(basic_type() == T_OBJECT || basic_type() == T_ARRAY, "wrong type");
110 return _value._object;
111 }
112
113 bool is_null_or_zero() const {
114 if (!is_java_primitive(basic_type())) {
115 return as_object()->is_null_object();
116 } else if (type2size[basic_type()] == 1) {
117 // treat float bits as int, to avoid comparison with -0 and NaN
118 return (_value._int == 0);
119 } else if (type2size[basic_type()] == 2) {
120 // treat double bits as long, to avoid comparison with -0 and NaN
121 return (_value._long == 0);
122 } else {
123 return false;
124 }
125 }
126
127 bool is_valid() const {
128 return basic_type() != T_ILLEGAL;
129 }
130 // Debugging output
131 void print();
132};
133
134#endif // SHARE_CI_CICONSTANT_HPP
135