Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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_JVALUE_H_ |
| 18 | #define ART_RUNTIME_JVALUE_H_ |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 19 | |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 20 | #include "base/locks.h" |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 21 | #include "base/macros.h" |
| 22 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 25 | #include "obj_ptr.h" |
| 26 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 27 | namespace art { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | namespace mirror { |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 29 | class Object; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | } // namespace mirror |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 31 | |
Mathieu Chartier | b12be8c | 2016-12-01 14:54:38 -0800 | [diff] [blame] | 32 | union PACKED(alignof(mirror::Object*)) JValue { |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 33 | // We default initialize JValue instances to all-zeros. |
| 34 | JValue() : j(0) {} |
| 35 | |
Andreas Gampe | c5b7564 | 2018-05-16 15:12:11 -0700 | [diff] [blame] | 36 | template<typename T> ALWAYS_INLINE static JValue FromPrimitive(T v); |
Alex Light | e00ec30 | 2017-06-16 08:56:43 -0700 | [diff] [blame] | 37 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 38 | int8_t GetB() const { return b; } |
| 39 | void SetB(int8_t new_b) { |
Douglas Leung | 44f1019 | 2015-09-23 16:42:08 -0700 | [diff] [blame] | 40 | j = ((static_cast<int64_t>(new_b) << 56) >> 56); // Sign-extend to 64 bits. |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | uint16_t GetC() const { return c; } |
buzbee | e667a3c | 2017-03-09 13:51:23 -0800 | [diff] [blame] | 44 | void SetC(uint16_t new_c) { |
| 45 | j = static_cast<int64_t>(new_c); // Zero-extend to 64 bits. |
| 46 | } |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 47 | |
| 48 | double GetD() const { return d; } |
| 49 | void SetD(double new_d) { d = new_d; } |
| 50 | |
| 51 | float GetF() const { return f; } |
| 52 | void SetF(float new_f) { f = new_f; } |
| 53 | |
| 54 | int32_t GetI() const { return i; } |
Douglas Leung | 44f1019 | 2015-09-23 16:42:08 -0700 | [diff] [blame] | 55 | void SetI(int32_t new_i) { |
| 56 | j = ((static_cast<int64_t>(new_i) << 32) >> 32); // Sign-extend to 64 bits. |
| 57 | } |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 58 | |
| 59 | int64_t GetJ() const { return j; } |
| 60 | void SetJ(int64_t new_j) { j = new_j; } |
| 61 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 62 | mirror::Object* GetL() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 63 | return l; |
| 64 | } |
Andreas Gampe | c5b7564 | 2018-05-16 15:12:11 -0700 | [diff] [blame] | 65 | ALWAYS_INLINE |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 66 | void SetL(ObjPtr<mirror::Object> new_l) REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 67 | |
| 68 | int16_t GetS() const { return s; } |
| 69 | void SetS(int16_t new_s) { |
Douglas Leung | 44f1019 | 2015-09-23 16:42:08 -0700 | [diff] [blame] | 70 | j = ((static_cast<int64_t>(new_s) << 48) >> 48); // Sign-extend to 64 bits. |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | uint8_t GetZ() const { return z; } |
buzbee | e667a3c | 2017-03-09 13:51:23 -0800 | [diff] [blame] | 74 | void SetZ(uint8_t new_z) { |
| 75 | j = static_cast<int64_t>(new_z); // Zero-extend to 64 bits. |
| 76 | } |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 77 | |
Mingyao Yang | 1f2d3ba | 2015-05-18 12:12:50 -0700 | [diff] [blame] | 78 | mirror::Object** GetGCRoot() { return &l; } |
| 79 | |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 80 | private: |
| 81 | uint8_t z; |
| 82 | int8_t b; |
| 83 | uint16_t c; |
| 84 | int16_t s; |
| 85 | int32_t i; |
| 86 | int64_t j; |
| 87 | float f; |
| 88 | double d; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 89 | mirror::Object* l; |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | } // namespace art |
| 93 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 94 | #endif // ART_RUNTIME_JVALUE_H_ |