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
34VoidType* voidType = NULL;
35IntType* intType = NULL;
36LongType* longType = NULL;
37FloatType* floatType = NULL;
38DoubleType* doubleType = NULL;
39ObjectType* objectType = NULL;
40ArrayType* arrayType = NULL;
41InstanceType* instanceType = NULL;
42ClassType* classType = NULL;
43AddressType* addressType = NULL;
44IllegalType* illegalType = NULL;
45
46
47// predefined constants
48IntConstant* intZero = NULL;
49IntConstant* intOne = NULL;
50ObjectConstant* objectNull = NULL;
51
52
53void 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
76ValueType* 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
83ValueType* ValueType::join(ValueType* y) const {
84 Unimplemented();
85 return NULL;
86}
87
88
89ciType* ObjectConstant::exact_type() const {
90 ciObject* c = constant_value();
91 return (c != NULL && !c->is_null_object()) ? c->klass() : NULL;
92}
93ciType* ArrayConstant::exact_type() const {
94 ciObject* c = constant_value();
95 return (c != NULL && !c->is_null_object()) ? c->klass() : NULL;
96}
97ciType* InstanceConstant::exact_type() const {
98 ciObject* c = constant_value();
99 return (c != NULL && !c->is_null_object()) ? c->klass() : NULL;
100}
101ciType* ClassConstant::exact_type() const {
102 return Compilation::current()->env()->Class_klass();
103}
104
105
106jobject ObjectType::encoding() const {
107 assert(is_constant(), "must be");
108 return constant_value()->constant_encoding();
109}
110
111bool ObjectType::is_loaded() const {
112 assert(is_constant(), "must be");
113 return constant_value()->is_loaded();
114}
115
116bool MetadataType::is_loaded() const {
117 assert(is_constant(), "must be");
118 return constant_value()->is_loaded();
119}
120
121ciObject* ObjectConstant::constant_value() const { return _value; }
122ciObject* ArrayConstant::constant_value() const { return _value; }
123ciObject* InstanceConstant::constant_value() const { return _value; }
124
125ValueType* as_ValueType(BasicType type) {
126 switch (type) {
127 case T_VOID : return voidType;
128 case T_BYTE : // fall through
129 case T_CHAR : // fall through
130 case T_SHORT : // fall through
131 case T_BOOLEAN: // fall through
132 case T_INT : return intType;
133 case T_LONG : return longType;
134 case T_FLOAT : return floatType;
135 case T_DOUBLE : return doubleType;
136 case T_ARRAY : return arrayType;
137 case T_OBJECT : return objectType;
138 case T_ADDRESS: return addressType;
139 case T_ILLEGAL: return illegalType;
140 default : ShouldNotReachHere();
141 return illegalType;
142 }
143}
144
145
146ValueType* as_ValueType(ciConstant value) {
147 switch (value.basic_type()) {
148 case T_BYTE : // fall through
149 case T_CHAR : // fall through
150 case T_SHORT : // fall through
151 case T_BOOLEAN: // fall through
152 case T_INT : return new IntConstant (value.as_int ());
153 case T_LONG : return new LongConstant (value.as_long ());
154 case T_FLOAT : return new FloatConstant (value.as_float ());
155 case T_DOUBLE : return new DoubleConstant(value.as_double());
156 case T_ARRAY : // fall through (ciConstant doesn't have an array accessor)
157 case T_OBJECT : {
158 // TODO: Common the code with GraphBuilder::load_constant?
159 ciObject* obj = value.as_object();
160 if (obj->is_null_object())
161 return objectNull;
162 if (obj->is_loaded()) {
163 if (obj->is_array())
164 return new ArrayConstant(obj->as_array());
165 else if (obj->is_instance())
166 return new InstanceConstant(obj->as_instance());
167 }
168 return new ObjectConstant(obj);
169 }
170 default : ShouldNotReachHere();
171 return illegalType;
172 }
173}
174
175
176BasicType as_BasicType(ValueType* type) {
177 switch (type->tag()) {
178 case voidTag: return T_VOID;
179 case intTag: return T_INT;
180 case longTag: return T_LONG;
181 case floatTag: return T_FLOAT;
182 case doubleTag: return T_DOUBLE;
183 case objectTag: return T_OBJECT;
184 case metaDataTag:return T_METADATA;
185 case addressTag: return T_ADDRESS;
186 case illegalTag: return T_ILLEGAL;
187 default : ShouldNotReachHere();
188 return T_ILLEGAL;
189 }
190}
191