blob: 32bfdaf089085867a589518390174e0b08202bf9 [file] [log] [blame]
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_PRIMITIVE_H_
18#define ART_RUNTIME_PRIMITIVE_H_
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070019
20#include <sys/types.h>
21
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080023#include "base/macros.h"
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070024
25namespace art {
Ian Rogers68d8b422014-07-17 11:09:10 -070026
27static constexpr size_t kObjectReferenceSize = 4;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070028
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070029
30template<size_t kComponentSize>
31size_t ComponentSizeShiftWidth() {
32 switch (kComponentSize) {
33 case 1:
34 return 0U;
35 case 2:
36 return 1U;
37 case 4:
38 return 2U;
39 case 8:
40 return 3U;
41 default:
42 LOG(FATAL) << "Unexpected component size : " << kComponentSize;
43 return 0U;
44 }
45}
46
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070047class Primitive {
48 public:
49 enum Type {
50 kPrimNot = 0,
51 kPrimBoolean,
52 kPrimByte,
53 kPrimChar,
54 kPrimShort,
55 kPrimInt,
56 kPrimLong,
57 kPrimFloat,
58 kPrimDouble,
59 kPrimVoid,
60 };
61
62 static Type GetType(char type) {
63 switch (type) {
64 case 'B':
65 return kPrimByte;
66 case 'C':
67 return kPrimChar;
68 case 'D':
69 return kPrimDouble;
70 case 'F':
71 return kPrimFloat;
72 case 'I':
73 return kPrimInt;
74 case 'J':
75 return kPrimLong;
76 case 'S':
77 return kPrimShort;
78 case 'Z':
79 return kPrimBoolean;
80 case 'V':
81 return kPrimVoid;
82 default:
83 return kPrimNot;
84 }
85 }
86
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070087 static size_t ComponentSizeShift(Type type) {
88 switch (type) {
89 case kPrimVoid:
90 case kPrimBoolean:
91 case kPrimByte: return 0;
92 case kPrimChar:
93 case kPrimShort: return 1;
94 case kPrimInt:
95 case kPrimFloat: return 2;
96 case kPrimLong:
97 case kPrimDouble: return 3;
98 case kPrimNot: return ComponentSizeShiftWidth<kObjectReferenceSize>();
99 default:
100 LOG(FATAL) << "Invalid type " << static_cast<int>(type);
101 return 0;
102 }
103 }
104
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700105 static size_t ComponentSize(Type type) {
106 switch (type) {
Ian Rogers169c9a72011-11-13 20:13:17 -0800107 case kPrimVoid: return 0;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700108 case kPrimBoolean:
109 case kPrimByte: return 1;
110 case kPrimChar:
111 case kPrimShort: return 2;
112 case kPrimInt:
113 case kPrimFloat: return 4;
114 case kPrimLong:
115 case kPrimDouble: return 8;
Ian Rogers68d8b422014-07-17 11:09:10 -0700116 case kPrimNot: return kObjectReferenceSize;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700117 default:
118 LOG(FATAL) << "Invalid type " << static_cast<int>(type);
119 return 0;
120 }
121 }
122
Elliott Hughes91250e02011-12-13 22:30:35 -0800123 static const char* Descriptor(Type type) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700124 switch (type) {
125 case kPrimBoolean:
Elliott Hughes91250e02011-12-13 22:30:35 -0800126 return "Z";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700127 case kPrimByte:
Elliott Hughes91250e02011-12-13 22:30:35 -0800128 return "B";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700129 case kPrimChar:
Elliott Hughes91250e02011-12-13 22:30:35 -0800130 return "C";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700131 case kPrimShort:
Elliott Hughes91250e02011-12-13 22:30:35 -0800132 return "S";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700133 case kPrimInt:
Elliott Hughes91250e02011-12-13 22:30:35 -0800134 return "I";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700135 case kPrimFloat:
Elliott Hughes91250e02011-12-13 22:30:35 -0800136 return "F";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700137 case kPrimLong:
Elliott Hughes91250e02011-12-13 22:30:35 -0800138 return "J";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700139 case kPrimDouble:
Elliott Hughes91250e02011-12-13 22:30:35 -0800140 return "D";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800141 case kPrimVoid:
Elliott Hughes91250e02011-12-13 22:30:35 -0800142 return "V";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700143 default:
144 LOG(FATAL) << "Primitive char conversion on invalid type " << static_cast<int>(type);
Elliott Hughes91250e02011-12-13 22:30:35 -0800145 return NULL;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700146 }
147 }
148
Roland Levillain5c4405e2015-01-21 11:39:58 +0000149 static const char* PrettyDescriptor(Type type);
150
Alexandre Rames542361f2015-01-29 16:57:31 +0000151 static bool IsFloatingPointType(Type type) {
152 return type == kPrimFloat || type == kPrimDouble;
153 }
154
155 static bool IsIntegralType(Type type) {
David Brazdilb2bd1c52015-03-25 11:17:37 +0000156 // The Java language does not allow treating boolean as an integral type but
157 // our bit representation makes it safe.
Alexandre Rames542361f2015-01-29 16:57:31 +0000158 switch (type) {
David Brazdil46e2a392015-03-16 17:31:52 +0000159 case kPrimBoolean:
Alexandre Rames542361f2015-01-29 16:57:31 +0000160 case kPrimByte:
161 case kPrimChar:
162 case kPrimShort:
163 case kPrimInt:
164 case kPrimLong:
165 return true;
166 default:
167 return false;
168 }
169 }
170
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000171 static bool IsIntOrLongType(Type type) {
172 return type == kPrimInt || type == kPrimLong;
173 }
174
Alexandre Rames542361f2015-01-29 16:57:31 +0000175 static bool Is64BitType(Type type) {
176 return type == kPrimLong || type == kPrimDouble;
177 }
178
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700179 private:
180 DISALLOW_IMPLICIT_CONSTRUCTORS(Primitive);
181};
182
Brian Carlstromae826982011-11-09 01:33:42 -0800183std::ostream& operator<<(std::ostream& os, const Primitive::Type& state);
184
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700185} // namespace art
186
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700187#endif // ART_RUNTIME_PRIMITIVE_H_