blob: ca42c4790c868fed9c0b1b85eeaffcd2ef5ac69e [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
Mathieu Chartierc7853442015-03-27 14:35:38 -070029constexpr size_t ComponentSizeShiftWidth(size_t component_size) {
30 return component_size == 1u ? 0u :
31 component_size == 2u ? 1u :
32 component_size == 4u ? 2u :
33 component_size == 8u ? 3u : 0u;
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070034}
35
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070036class Primitive {
37 public:
38 enum Type {
39 kPrimNot = 0,
40 kPrimBoolean,
41 kPrimByte,
42 kPrimChar,
43 kPrimShort,
44 kPrimInt,
45 kPrimLong,
46 kPrimFloat,
47 kPrimDouble,
48 kPrimVoid,
49 };
50
51 static Type GetType(char type) {
52 switch (type) {
53 case 'B':
54 return kPrimByte;
55 case 'C':
56 return kPrimChar;
57 case 'D':
58 return kPrimDouble;
59 case 'F':
60 return kPrimFloat;
61 case 'I':
62 return kPrimInt;
63 case 'J':
64 return kPrimLong;
65 case 'S':
66 return kPrimShort;
67 case 'Z':
68 return kPrimBoolean;
69 case 'V':
70 return kPrimVoid;
71 default:
72 return kPrimNot;
73 }
74 }
75
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070076 static size_t ComponentSizeShift(Type type) {
77 switch (type) {
78 case kPrimVoid:
79 case kPrimBoolean:
80 case kPrimByte: return 0;
81 case kPrimChar:
82 case kPrimShort: return 1;
83 case kPrimInt:
84 case kPrimFloat: return 2;
85 case kPrimLong:
86 case kPrimDouble: return 3;
Mathieu Chartierc7853442015-03-27 14:35:38 -070087 case kPrimNot: return ComponentSizeShiftWidth(kObjectReferenceSize);
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070088 default:
89 LOG(FATAL) << "Invalid type " << static_cast<int>(type);
90 return 0;
91 }
92 }
93
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070094 static size_t ComponentSize(Type type) {
95 switch (type) {
Ian Rogers169c9a72011-11-13 20:13:17 -080096 case kPrimVoid: return 0;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070097 case kPrimBoolean:
98 case kPrimByte: return 1;
99 case kPrimChar:
100 case kPrimShort: return 2;
101 case kPrimInt:
102 case kPrimFloat: return 4;
103 case kPrimLong:
104 case kPrimDouble: return 8;
Ian Rogers68d8b422014-07-17 11:09:10 -0700105 case kPrimNot: return kObjectReferenceSize;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700106 default:
107 LOG(FATAL) << "Invalid type " << static_cast<int>(type);
108 return 0;
109 }
110 }
111
Elliott Hughes91250e02011-12-13 22:30:35 -0800112 static const char* Descriptor(Type type) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700113 switch (type) {
114 case kPrimBoolean:
Elliott Hughes91250e02011-12-13 22:30:35 -0800115 return "Z";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700116 case kPrimByte:
Elliott Hughes91250e02011-12-13 22:30:35 -0800117 return "B";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700118 case kPrimChar:
Elliott Hughes91250e02011-12-13 22:30:35 -0800119 return "C";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700120 case kPrimShort:
Elliott Hughes91250e02011-12-13 22:30:35 -0800121 return "S";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700122 case kPrimInt:
Elliott Hughes91250e02011-12-13 22:30:35 -0800123 return "I";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700124 case kPrimFloat:
Elliott Hughes91250e02011-12-13 22:30:35 -0800125 return "F";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700126 case kPrimLong:
Elliott Hughes91250e02011-12-13 22:30:35 -0800127 return "J";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700128 case kPrimDouble:
Elliott Hughes91250e02011-12-13 22:30:35 -0800129 return "D";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800130 case kPrimVoid:
Elliott Hughes91250e02011-12-13 22:30:35 -0800131 return "V";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700132 default:
133 LOG(FATAL) << "Primitive char conversion on invalid type " << static_cast<int>(type);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700134 return nullptr;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700135 }
136 }
137
Roland Levillain5c4405e2015-01-21 11:39:58 +0000138 static const char* PrettyDescriptor(Type type);
139
Alexandre Rames542361f2015-01-29 16:57:31 +0000140 static bool IsFloatingPointType(Type type) {
141 return type == kPrimFloat || type == kPrimDouble;
142 }
143
144 static bool IsIntegralType(Type type) {
David Brazdilb2bd1c52015-03-25 11:17:37 +0000145 // The Java language does not allow treating boolean as an integral type but
146 // our bit representation makes it safe.
Alexandre Rames542361f2015-01-29 16:57:31 +0000147 switch (type) {
David Brazdil46e2a392015-03-16 17:31:52 +0000148 case kPrimBoolean:
Alexandre Rames542361f2015-01-29 16:57:31 +0000149 case kPrimByte:
150 case kPrimChar:
151 case kPrimShort:
152 case kPrimInt:
153 case kPrimLong:
154 return true;
155 default:
156 return false;
157 }
158 }
159
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +0000160 static bool IsIntOrLongType(Type type) {
161 return type == kPrimInt || type == kPrimLong;
162 }
163
Alexandre Rames542361f2015-01-29 16:57:31 +0000164 static bool Is64BitType(Type type) {
165 return type == kPrimLong || type == kPrimDouble;
166 }
167
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700168 private:
169 DISALLOW_IMPLICIT_CONSTRUCTORS(Primitive);
170};
171
Brian Carlstromae826982011-11-09 01:33:42 -0800172std::ostream& operator<<(std::ostream& os, const Primitive::Type& state);
173
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700174} // namespace art
175
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700176#endif // ART_RUNTIME_PRIMITIVE_H_