1 /*
 2  * Copyright (c) 1999, 2025, 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 // ------------------------------------------------------------------
35 // ciConstant::is_null_or_zero
36 bool ciConstant::is_null_or_zero() const {
37   if (!is_java_primitive(basic_type())) {
38     return as_object()->is_null_object();
39   } else if (type2size[basic_type()] == 1) {
40     // treat float bits as int, to avoid comparison with -0 and NaN
41     return (_value._int == 0);
42   } else if (type2size[basic_type()] == 2) {
43     // treat double bits as long, to avoid comparison with -0 and NaN
44     return (_value._long == 0);
45   } else {
46     return false;
47   }
48 }
49 
50 // ------------------------------------------------------------------
51 // ciConstant::is_loaded
52 bool ciConstant::is_loaded() const {
53   if (is_valid()) {
54     if (is_reference_type(basic_type())) {
55       return as_object()->is_loaded();
56     } else {
57       return true;
58     }
59   }
60   return false;
61 }
62 
63 // ------------------------------------------------------------------
64 // ciConstant::print
65 void ciConstant::print() {
66   tty->print("<ciConstant type=%s value=",
67              basictype_to_str(basic_type()));
68   switch (basic_type()) {
69   case T_BOOLEAN:
70     tty->print("%s", bool_to_str(_value._int != 0));
71     break;
72   case T_CHAR:
73   case T_BYTE:
74   case T_SHORT:
75   case T_INT:
76     tty->print("%d", _value._int);
77     break;
78   case T_LONG:
79     tty->print(INT64_FORMAT, (int64_t)(_value._long));
80     break;
81   case T_FLOAT:
82     tty->print("%f", _value._float);
83     break;
84   case T_DOUBLE:
85     tty->print("%lf", _value._double);
86     break;
87   default:
88     if (is_reference_type(basic_type())) {
89       _value._object->print();
90     } else {
91       tty->print("ILLEGAL");
92     }
93     break;
94   }
95   tty->print(">");
96 }