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 | |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 29 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame^] | 30 | constexpr size_t ComponentSizeShiftWidth(size_t component_size) { |
| 31 | return component_size == 1u ? 0u : |
| 32 | component_size == 2u ? 1u : |
| 33 | component_size == 4u ? 2u : |
| 34 | component_size == 8u ? 3u : 0u; |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 37 | class Primitive { |
| 38 | public: |
| 39 | enum Type { |
| 40 | kPrimNot = 0, |
| 41 | kPrimBoolean, |
| 42 | kPrimByte, |
| 43 | kPrimChar, |
| 44 | kPrimShort, |
| 45 | kPrimInt, |
| 46 | kPrimLong, |
| 47 | kPrimFloat, |
| 48 | kPrimDouble, |
| 49 | kPrimVoid, |
| 50 | }; |
| 51 | |
| 52 | static Type GetType(char type) { |
| 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 | |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 77 | static size_t ComponentSizeShift(Type type) { |
| 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 | default: |
| 90 | LOG(FATAL) << "Invalid type " << static_cast<int>(type); |
| 91 | return 0; |
| 92 | } |
| 93 | } |
| 94 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 95 | static size_t ComponentSize(Type type) { |
| 96 | switch (type) { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 97 | case kPrimVoid: return 0; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 98 | case kPrimBoolean: |
| 99 | case kPrimByte: return 1; |
| 100 | case kPrimChar: |
| 101 | case kPrimShort: return 2; |
| 102 | case kPrimInt: |
| 103 | case kPrimFloat: return 4; |
| 104 | case kPrimLong: |
| 105 | case kPrimDouble: return 8; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 106 | case kPrimNot: return kObjectReferenceSize; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 107 | default: |
| 108 | LOG(FATAL) << "Invalid type " << static_cast<int>(type); |
| 109 | return 0; |
| 110 | } |
| 111 | } |
| 112 | |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 113 | static const char* Descriptor(Type type) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 114 | switch (type) { |
| 115 | case kPrimBoolean: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 116 | return "Z"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 117 | case kPrimByte: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 118 | return "B"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 119 | case kPrimChar: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 120 | return "C"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 121 | case kPrimShort: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 122 | return "S"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 123 | case kPrimInt: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 124 | return "I"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 125 | case kPrimFloat: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 126 | return "F"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 127 | case kPrimLong: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 128 | return "J"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 129 | case kPrimDouble: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 130 | return "D"; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 131 | case kPrimVoid: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 132 | return "V"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 133 | default: |
| 134 | LOG(FATAL) << "Primitive char conversion on invalid type " << static_cast<int>(type); |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 135 | return NULL; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Roland Levillain | 5c4405e | 2015-01-21 11:39:58 +0000 | [diff] [blame] | 139 | static const char* PrettyDescriptor(Type type); |
| 140 | |
Alexandre Rames | 542361f | 2015-01-29 16:57:31 +0000 | [diff] [blame] | 141 | static bool IsFloatingPointType(Type type) { |
| 142 | return type == kPrimFloat || type == kPrimDouble; |
| 143 | } |
| 144 | |
| 145 | static bool IsIntegralType(Type type) { |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 146 | // The Java language does not allow treating boolean as an integral type but |
| 147 | // our bit representation makes it safe. |
Alexandre Rames | 542361f | 2015-01-29 16:57:31 +0000 | [diff] [blame] | 148 | switch (type) { |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 149 | case kPrimBoolean: |
Alexandre Rames | 542361f | 2015-01-29 16:57:31 +0000 | [diff] [blame] | 150 | case kPrimByte: |
| 151 | case kPrimChar: |
| 152 | case kPrimShort: |
| 153 | case kPrimInt: |
| 154 | case kPrimLong: |
| 155 | return true; |
| 156 | default: |
| 157 | return false; |
| 158 | } |
| 159 | } |
| 160 | |
Alexandre Rames | b2fd7bc | 2015-03-11 16:48:16 +0000 | [diff] [blame] | 161 | static bool IsIntOrLongType(Type type) { |
| 162 | return type == kPrimInt || type == kPrimLong; |
| 163 | } |
| 164 | |
Alexandre Rames | 542361f | 2015-01-29 16:57:31 +0000 | [diff] [blame] | 165 | static bool Is64BitType(Type type) { |
| 166 | return type == kPrimLong || type == kPrimDouble; |
| 167 | } |
| 168 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 169 | private: |
| 170 | DISALLOW_IMPLICIT_CONSTRUCTORS(Primitive); |
| 171 | }; |
| 172 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 173 | std::ostream& operator<<(std::ostream& os, const Primitive::Type& state); |
| 174 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 175 | } // namespace art |
| 176 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 177 | #endif // ART_RUNTIME_PRIMITIVE_H_ |