1 /*
  2  * Copyright (c) 1999, 2017, 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 "c1/c1_ValueType.hpp"
 27 #include "ci/ciArray.hpp"
 28 #include "ci/ciInstance.hpp"
 29 #include "ci/ciNullObject.hpp"
 30 #include "memory/resourceArea.hpp"
 31 
 32 
 33 // predefined types
 34 VoidType*       voidType     = NULL;
 35 IntType*        intType      = NULL;
 36 LongType*       longType     = NULL;
 37 FloatType*      floatType    = NULL;
 38 DoubleType*     doubleType   = NULL;
 39 ObjectType*     objectType   = NULL;
 40 ArrayType*      arrayType    = NULL;
 41 InstanceType*   instanceType = NULL;
 42 ClassType*      classType    = NULL;
 43 AddressType*    addressType  = NULL;
 44 IllegalType*    illegalType  = NULL;
 45 
 46 
 47 // predefined constants
 48 IntConstant*    intZero      = NULL;
 49 IntConstant*    intOne       = NULL;
 50 ObjectConstant* objectNull   = NULL;
 51 
 52 
 53 void ValueType::initialize(Arena* arena) {
 54   // Note: Must initialize all types for each compilation
 55   //       as they are allocated within a ResourceMark!
 56 
 57   // types
 58   voidType     = new (arena) VoidType();
 59   intType      = new (arena) IntType();
 60   longType     = new (arena) LongType();
 61   floatType    = new (arena) FloatType();
 62   doubleType   = new (arena) DoubleType();
 63   objectType   = new (arena) ObjectType();
 64   arrayType    = new (arena) ArrayType();
 65   instanceType = new (arena) InstanceType();
 66   classType    = new (arena) ClassType();
 67   addressType  = new (arena) AddressType();
 68   illegalType  = new (arena) IllegalType();
 69 
 70   intZero     = new (arena) IntConstant(0);
 71   intOne      = new (arena) IntConstant(1);
 72   objectNull  = new (arena) ObjectConstant(ciNullObject::make());
 73 };
 74 
 75 
 76 ValueType* ValueType::meet(ValueType* y) const {
 77   // incomplete & conservative solution for now - fix this!
 78   assert(tag() == y->tag(), "types must match");
 79   return base();
 80 }
 81 
 82 
 83 ciType* ObjectConstant::exact_type() const {
 84   ciObject* c = constant_value();
 85   return (c != NULL && !c->is_null_object()) ? c->klass() : NULL;
 86 }
 87 ciType* ArrayConstant::exact_type() const {
 88   ciObject* c = constant_value();
 89   return (c != NULL && !c->is_null_object()) ? c->klass() : NULL;
 90 }
 91 ciType* InstanceConstant::exact_type() const {
 92   ciObject* c = constant_value();
 93   return (c != NULL && !c->is_null_object()) ? c->klass() : NULL;
 94 }
 95 ciType* ClassConstant::exact_type() const {
 96   return Compilation::current()->env()->Class_klass();
 97 }
 98 
 99 
100 jobject ObjectType::encoding() const {
101   assert(is_constant(), "must be");
102   return constant_value()->constant_encoding();
103 }
104 
105 bool ObjectType::is_loaded() const {
106   assert(is_constant(), "must be");
107   return constant_value()->is_loaded();
108 }
109 
110 bool MetadataType::is_loaded() const {
111   assert(is_constant(), "must be");
112   return constant_value()->is_loaded();
113 }
114 
115 ciObject* ObjectConstant::constant_value() const                   { return _value; }
116 ciObject* ArrayConstant::constant_value() const                    { return _value; }
117 ciObject* InstanceConstant::constant_value() const                 { return _value; }
118 
119 ValueType* as_ValueType(BasicType type) {
120   switch (type) {
121     case T_VOID   : return voidType;
122     case T_BYTE   : // fall through
123     case T_CHAR   : // fall through
124     case T_SHORT  : // fall through
125     case T_BOOLEAN: // fall through
126     case T_INT    : return intType;
127     case T_LONG   : return longType;
128     case T_FLOAT  : return floatType;
129     case T_DOUBLE : return doubleType;
130     case T_ARRAY  : return arrayType;
131     case T_OBJECT : return objectType;
132     case T_INLINE_TYPE: return objectType;
133     case T_ADDRESS: return addressType;
134     case T_ILLEGAL: return illegalType;
135     default       : ShouldNotReachHere();
136                     return illegalType;
137   }
138 }
139 
140 
141 ValueType* as_ValueType(ciConstant value) {
142   switch (value.basic_type()) {
143     case T_BYTE   : // fall through
144     case T_CHAR   : // fall through
145     case T_SHORT  : // fall through
146     case T_BOOLEAN: // fall through
147     case T_INT    : return new IntConstant   (value.as_int   ());
148     case T_LONG   : return new LongConstant  (value.as_long  ());
149     case T_FLOAT  : return new FloatConstant (value.as_float ());
150     case T_DOUBLE : return new DoubleConstant(value.as_double());
151     case T_ARRAY  : // fall through (ciConstant doesn't have an array accessor)
152     case T_INLINE_TYPE: // fall through
153     case T_OBJECT : {
154       // TODO: Common the code with GraphBuilder::load_constant?
155       ciObject* obj = value.as_object();
156       if (obj->is_null_object())
157         return objectNull;
158       if (obj->is_loaded()) {
159         if (obj->is_array())
160           return new ArrayConstant(obj->as_array());
161         else if (obj->is_instance())
162           return new InstanceConstant(obj->as_instance());
163       }
164       return new ObjectConstant(obj);
165     }
166     default       : ShouldNotReachHere();
167                     return illegalType;
168   }
169 }
170 
171 
172 BasicType as_BasicType(ValueType* type) {
173   switch (type->tag()) {
174     case voidTag:    return T_VOID;
175     case intTag:     return T_INT;
176     case longTag:    return T_LONG;
177     case floatTag:   return T_FLOAT;
178     case doubleTag:  return T_DOUBLE;
179     case objectTag:  return T_OBJECT;
180     case metaDataTag:return T_METADATA;
181     case addressTag: return T_ADDRESS;
182     case illegalTag: return T_ILLEGAL;
183     default        : ShouldNotReachHere();
184                      return T_ILLEGAL;
185   }
186 }