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