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