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" |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 24 | #include "mirror/object_reference.h" |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | namespace mirror { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 28 | class Object; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | } // namespace mirror |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 30 | |
| 31 | class Primitive { |
| 32 | public: |
| 33 | enum Type { |
| 34 | kPrimNot = 0, |
| 35 | kPrimBoolean, |
| 36 | kPrimByte, |
| 37 | kPrimChar, |
| 38 | kPrimShort, |
| 39 | kPrimInt, |
| 40 | kPrimLong, |
| 41 | kPrimFloat, |
| 42 | kPrimDouble, |
| 43 | kPrimVoid, |
| 44 | }; |
| 45 | |
| 46 | static Type GetType(char type) { |
| 47 | switch (type) { |
| 48 | case 'B': |
| 49 | return kPrimByte; |
| 50 | case 'C': |
| 51 | return kPrimChar; |
| 52 | case 'D': |
| 53 | return kPrimDouble; |
| 54 | case 'F': |
| 55 | return kPrimFloat; |
| 56 | case 'I': |
| 57 | return kPrimInt; |
| 58 | case 'J': |
| 59 | return kPrimLong; |
| 60 | case 'S': |
| 61 | return kPrimShort; |
| 62 | case 'Z': |
| 63 | return kPrimBoolean; |
| 64 | case 'V': |
| 65 | return kPrimVoid; |
| 66 | default: |
| 67 | return kPrimNot; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static size_t ComponentSize(Type type) { |
| 72 | switch (type) { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 73 | case kPrimVoid: return 0; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 74 | case kPrimBoolean: |
| 75 | case kPrimByte: return 1; |
| 76 | case kPrimChar: |
| 77 | case kPrimShort: return 2; |
| 78 | case kPrimInt: |
| 79 | case kPrimFloat: return 4; |
| 80 | case kPrimLong: |
| 81 | case kPrimDouble: return 8; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 82 | case kPrimNot: return sizeof(mirror::HeapReference<mirror::Object>); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 83 | default: |
| 84 | LOG(FATAL) << "Invalid type " << static_cast<int>(type); |
| 85 | return 0; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | static size_t FieldSize(Type type) { |
| 90 | return ComponentSize(type) <= 4 ? 4 : 8; |
| 91 | } |
| 92 | |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 93 | static const char* Descriptor(Type type) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 94 | switch (type) { |
| 95 | case kPrimBoolean: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 96 | return "Z"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 97 | case kPrimByte: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 98 | return "B"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 99 | case kPrimChar: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 100 | return "C"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 101 | case kPrimShort: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 102 | return "S"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 103 | case kPrimInt: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 104 | return "I"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 105 | case kPrimFloat: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 106 | return "F"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 107 | case kPrimLong: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 108 | return "J"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 109 | case kPrimDouble: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 110 | return "D"; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 111 | case kPrimVoid: |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 112 | return "V"; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 113 | default: |
| 114 | LOG(FATAL) << "Primitive char conversion on invalid type " << static_cast<int>(type); |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 115 | return NULL; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | private: |
| 120 | DISALLOW_IMPLICIT_CONSTRUCTORS(Primitive); |
| 121 | }; |
| 122 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 123 | std::ostream& operator<<(std::ostream& os, const Primitive::Type& state); |
| 124 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 125 | } // namespace art |
| 126 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 127 | #endif // ART_RUNTIME_PRIMITIVE_H_ |