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::should_be_constant
 65 bool ciConstant::should_be_constant() const {
 66   if (is_valid()) {
 67     if (is_reference_type(basic_type())) {
 68       return as_object()->should_be_constant();
 69     } else {
 70       return true;
 71     }
 72   }
 73   return false;
 74 }
 75 
 76 // ------------------------------------------------------------------
 77 // ciConstant::print
 78 void ciConstant::print() {
 79   tty->print("<ciConstant type=%s value=",
 80              basictype_to_str(basic_type()));
 81   switch (basic_type()) {
 82   case T_BOOLEAN:
 83     tty->print("%s", bool_to_str(_value._int != 0));
 84     break;
 85   case T_CHAR:
 86   case T_BYTE:
 87   case T_SHORT:
 88   case T_INT:
 89     tty->print("%d", _value._int);
 90     break;
 91   case T_LONG:
 92     tty->print(INT64_FORMAT, (int64_t)(_value._long));
 93     break;
 94   case T_FLOAT:
 95     tty->print("%f", _value._float);
 96     break;
 97   case T_DOUBLE:
 98     tty->print("%lf", _value._double);
 99     break;
100   default:
101     if (is_reference_type(basic_type())) {
102       _value._object->print();
103     } else {
104       tty->print("ILLEGAL");
105     }
106     break;
107   }
108   tty->print(">");
109 }