Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_PRIMITIVE_H_ |
| 18 | #define ART_RUNTIME_PRIMITIVE_H_ |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 19 | |
| 20 | #include <sys/types.h> |
| 21 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 22 | #include "base/logging.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 23 | #include "base/macros.h" |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 24 | |
| 25 | namespace art { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 26 | |
| 27 | static constexpr size_t kObjectReferenceSize = 4; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 28 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 29 | constexpr 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 Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 36 | class 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, |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 49 | kPrimLast = kPrimVoid |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 52 | static constexpr Type GetType(char type) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 53 | switch (type) { |
| 54 | case 'B': |
| 55 | return kPrimByte; |
| 56 | case 'C': |
| 57 | return kPrimChar; |
| 58 | case 'D': |
| 59 | return kPrimDouble; |
| 60 | case 'F': |
| 61 | return kPrimFloat; |
| 62 | case 'I': |
| 63 | return kPrimInt; |
| 64 | case 'J': |
| 65 | return kPrimLong; |
| 66 | case 'S': |
| 67 | return kPrimShort; |
| 68 | case 'Z': |
| 69 | return kPrimBoolean; |
| 70 | case 'V': |
| 71 | return kPrimVoid; |
| 72 | default: |
| 73 | return kPrimNot; |
| 74 | } |
| 75 | } |
| 76 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 77 | static constexpr size_t ComponentSizeShift(Type type) { |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 78 | switch (type) { |
| 79 | case kPrimVoid: |
| 80 | case kPrimBoolean: |
| 81 | case kPrimByte: return 0; |
| 82 | case kPrimChar: |
| 83 | case kPrimShort: return 1; |
| 84 | case kPrimInt: |
| 85 | case kPrimFloat: return 2; |
| 86 | case kPrimLong: |
| 87 | case kPrimDouble: return 3; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 88 | case kPrimNot: return ComponentSizeShiftWidth(kObjectReferenceSize); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 89 | } |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 90 | LOG(FATAL) << "Invalid type " << static_cast<int>(type); |
| 91 | UNREACHABLE(); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 94 | static constexpr size_t ComponentSize(Type type) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 95 | switch (type) { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 96 | case kPrimVoid: return 0; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 97 | 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 Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 105 | case kPrimNot: return kObjectReferenceSize; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 106 | } |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 107 | LOG(FATAL) << "Invalid type " << static_cast<int>(type); |
| 108 | UNREACHABLE(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 111 | static const char* Descriptor(Type type) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 112 | switch (type) { |
| 113 | case kPrimBoolean: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 114 | return "Z"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 115 | case kPrimByte: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 116 | return "B"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 117 | case kPrimChar: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 118 | return "C"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 119 | case kPrimShort: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 120 | return "S"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 121 | case kPrimInt: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 122 | return "I"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 123 | case kPrimFloat: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 124 | return "F"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 125 | case kPrimLong: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 126 | return "J"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 127 | case kPrimDouble: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 128 | return "D"; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 129 | case kPrimVoid: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 130 | return "V"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 131 | default: |
| 132 | LOG(FATAL) << "Primitive char conversion on invalid type " << static_cast<int>(type); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 133 | return nullptr; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 137 | static const char* PrettyDescriptor(Type type); |
| 138 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 139 | // Returns the descriptor corresponding to the boxed type of |type|. |
| 140 | static const char* BoxedDescriptor(Type type); |
| 141 | |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 142 | // Return true if |type| is an numeric type. |
Orion Hodson | f1412b4 | 2016-11-11 12:03:29 +0000 | [diff] [blame] | 143 | static constexpr bool IsNumericType(Type type) { |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 144 | switch (type) { |
| 145 | case Primitive::Type::kPrimNot: return false; |
| 146 | case Primitive::Type::kPrimBoolean: return false; |
| 147 | case Primitive::Type::kPrimByte: return true; |
| 148 | case Primitive::Type::kPrimChar: return false; |
| 149 | case Primitive::Type::kPrimShort: return true; |
| 150 | case Primitive::Type::kPrimInt: return true; |
| 151 | case Primitive::Type::kPrimLong: return true; |
| 152 | case Primitive::Type::kPrimFloat: return true; |
| 153 | case Primitive::Type::kPrimDouble: return true; |
| 154 | case Primitive::Type::kPrimVoid: return false; |
| 155 | } |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 156 | LOG(FATAL) << "Invalid type " << static_cast<int>(type); |
| 157 | UNREACHABLE(); |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Orion Hodson | f1412b4 | 2016-11-11 12:03:29 +0000 | [diff] [blame] | 160 | // Returns true if it is possible to widen type |from| to type |to|. Both |from| and |
| 161 | // |to| should be numeric primitive types. |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 162 | static bool IsWidenable(Type from, Type to) { |
| 163 | static_assert(Primitive::Type::kPrimByte < Primitive::Type::kPrimShort, "Bad ordering"); |
| 164 | static_assert(Primitive::Type::kPrimShort < Primitive::Type::kPrimInt, "Bad ordering"); |
| 165 | static_assert(Primitive::Type::kPrimInt < Primitive::Type::kPrimLong, "Bad ordering"); |
| 166 | static_assert(Primitive::Type::kPrimLong < Primitive::Type::kPrimFloat, "Bad ordering"); |
| 167 | static_assert(Primitive::Type::kPrimFloat < Primitive::Type::kPrimDouble, "Bad ordering"); |
Orion Hodson | f1412b4 | 2016-11-11 12:03:29 +0000 | [diff] [blame] | 168 | // Widening is only applicable between numeric types, like byte |
| 169 | // and int. Non-numeric types, such as boolean, cannot be widened. |
Orion Hodson | 1a06f9f | 2016-11-09 08:32:42 +0000 | [diff] [blame] | 170 | return IsNumericType(from) && IsNumericType(to) && from <= to; |
| 171 | } |
| 172 | |
Alexandre Rames | 542361f | 2015-01-29 16:57:31 +0000 | [diff] [blame] | 173 | static bool Is64BitType(Type type) { |
| 174 | return type == kPrimLong || type == kPrimDouble; |
| 175 | } |
| 176 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 177 | private: |
| 178 | DISALLOW_IMPLICIT_CONSTRUCTORS(Primitive); |
| 179 | }; |
| 180 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 181 | std::ostream& operator<<(std::ostream& os, Primitive::Type state); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 182 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 183 | } // namespace art |
| 184 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 185 | #endif // ART_RUNTIME_PRIMITIVE_H_ |