1 /*
2 * Copyright (c) 1999, 2026, 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 #include "ci/ciConstant.hpp"
26 #include "ci/ciUtilities.hpp"
27 #include "memory/allocation.hpp"
28 #include "memory/allocation.inline.hpp"
29
30 // ciConstant
31 //
32 // This class represents a constant value.
33
34 ciConstant ciConstant::make_zero_or_null(BasicType bt) {
35 switch (bt) {
36 case T_FLOAT: return ciConstant((jfloat).0f);
37 case T_DOUBLE: return ciConstant((jdouble).0);
38 case T_LONG: return ciConstant((jlong)0L);
39 case T_BOOLEAN:
40 case T_CHAR:
41 case T_BYTE:
42 case T_SHORT:
43 case T_INT:
44 return ciConstant(bt, 0);
45 case T_OBJECT:
46 case T_ARRAY:
47 return ciConstant(bt, CURRENT_ENV->get_object(nullptr));
48 default:
49 ShouldNotReachHere();
50 return ciConstant();
51 }
52 }
53
54 // ------------------------------------------------------------------
55 // ciConstant::is_null_or_zero
56 // This assumes `this->is_valid()`, otherwise, `as_object` will assert.
57 bool ciConstant::is_null_or_zero() const {
58 if (!is_java_primitive(basic_type())) {
59 return as_object()->is_null_object();
60 } else if (type2size[basic_type()] == 1) {
61 // treat float bits as int, to avoid comparison with -0 and NaN
62 return (_value._int == 0);
63 } else if (type2size[basic_type()] == 2) {
64 // treat double bits as long, to avoid comparison with -0 and NaN
65 return (_value._long == 0);
66 } else {
67 return false;
68 }
69 }
70
71 // ------------------------------------------------------------------
72 // ciConstant::is_loaded
73 bool ciConstant::is_loaded() const {
74 if (is_valid()) {
75 if (is_reference_type(basic_type())) {
76 return as_object()->is_loaded();
77 } else {
78 return true;
79 }
80 }
81 return false;
82 }
83
84 // ------------------------------------------------------------------
85 // ciConstant::print
86 void ciConstant::print() const {
87 tty->print("<ciConstant type=%s value=",
88 basictype_to_str(basic_type()));
89 switch (basic_type()) {
90 case T_BOOLEAN:
91 tty->print("%s", bool_to_str(_value._int != 0));
92 break;
93 case T_CHAR:
94 case T_BYTE:
95 case T_SHORT:
96 case T_INT:
97 tty->print("%d", _value._int);
98 break;
99 case T_LONG:
100 tty->print(INT64_FORMAT, (int64_t)(_value._long));
101 break;
102 case T_FLOAT:
103 tty->print("%f", _value._float);
104 break;
105 case T_DOUBLE:
106 tty->print("%lf", _value._double);
107 break;
108 default:
109 if (is_reference_type(basic_type())) {
110 _value._object->print();
111 } else {
112 tty->print("ILLEGAL");
113 }
114 break;
115 }
116 tty->print(">");
117 }