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
17#ifndef ART_RUNTIME_JVALUE_H_
18#define ART_RUNTIME_JVALUE_H_
19
20#include "base/macros.h"
21#include "base/mutex.h"
22
23#include <stdint.h>
24
25#include "obj_ptr.h"
26
27namespace art {
28namespace mirror {
29class Object;
30}  // namespace mirror
31
32union PACKED(alignof(mirror::Object*)) JValue {
33  // We default initialize JValue instances to all-zeros.
34  JValue() : j(0) {}
35
36  int8_t GetB() const { return b; }
37  void SetB(int8_t new_b) {
38    j = ((static_cast<int64_t>(new_b) << 56) >> 56);  // Sign-extend to 64 bits.
39  }
40
41  uint16_t GetC() const { return c; }
42  void SetC(uint16_t new_c) {
43    j = static_cast<int64_t>(new_c);  // Zero-extend to 64 bits.
44  }
45
46  double GetD() const { return d; }
47  void SetD(double new_d) { d = new_d; }
48
49  float GetF() const { return f; }
50  void SetF(float new_f) { f = new_f; }
51
52  int32_t GetI() const { return i; }
53  void SetI(int32_t new_i) {
54    j = ((static_cast<int64_t>(new_i) << 32) >> 32);  // Sign-extend to 64 bits.
55  }
56
57  int64_t GetJ() const { return j; }
58  void SetJ(int64_t new_j) { j = new_j; }
59
60  mirror::Object* GetL() const REQUIRES_SHARED(Locks::mutator_lock_) {
61    return l;
62  }
63  void SetL(ObjPtr<mirror::Object> new_l) REQUIRES_SHARED(Locks::mutator_lock_);
64
65  int16_t GetS() const { return s; }
66  void SetS(int16_t new_s) {
67    j = ((static_cast<int64_t>(new_s) << 48) >> 48);  // Sign-extend to 64 bits.
68  }
69
70  uint8_t GetZ() const { return z; }
71  void SetZ(uint8_t new_z) {
72    j = static_cast<int64_t>(new_z);  // Zero-extend to 64 bits.
73  }
74
75  mirror::Object** GetGCRoot() { return &l; }
76
77 private:
78  uint8_t z;
79  int8_t b;
80  uint16_t c;
81  int16_t s;
82  int32_t i;
83  int64_t j;
84  float f;
85  double d;
86  mirror::Object* l;
87};
88
89}  // namespace art
90
91#endif  // ART_RUNTIME_JVALUE_H_
92