Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [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 | |
| 17 | #include "field.h" |
| 18 | |
| 19 | #include "field-inl.h" |
| 20 | #include "gc/card_table-inl.h" |
| 21 | #include "object-inl.h" |
| 22 | #include "object_utils.h" |
| 23 | #include "runtime.h" |
| 24 | #include "utils.h" |
| 25 | |
| 26 | namespace art { |
| 27 | namespace mirror { |
| 28 | |
| 29 | // TODO: get global references for these |
| 30 | Class* Field::java_lang_reflect_Field_ = NULL; |
| 31 | |
| 32 | void Field::SetClass(Class* java_lang_reflect_Field) { |
| 33 | CHECK(java_lang_reflect_Field_ == NULL); |
| 34 | CHECK(java_lang_reflect_Field != NULL); |
| 35 | java_lang_reflect_Field_ = java_lang_reflect_Field; |
| 36 | } |
| 37 | |
| 38 | void Field::ResetClass() { |
| 39 | CHECK(java_lang_reflect_Field_ != NULL); |
| 40 | java_lang_reflect_Field_ = NULL; |
| 41 | } |
| 42 | |
| 43 | void Field::SetOffset(MemberOffset num_bytes) { |
| 44 | DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous()); |
| 45 | #if 0 // TODO enable later in boot and under !NDEBUG |
| 46 | FieldHelper fh(this); |
| 47 | Primitive::Type type = fh.GetTypeAsPrimitiveType(); |
| 48 | if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) { |
| 49 | DCHECK_ALIGNED(num_bytes.Uint32Value(), 8); |
| 50 | } |
| 51 | #endif |
| 52 | SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false); |
| 53 | } |
| 54 | |
| 55 | uint32_t Field::Get32(const Object* object) const { |
| 56 | DCHECK(object != NULL) << PrettyField(this); |
| 57 | DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); |
| 58 | return object->GetField32(GetOffset(), IsVolatile()); |
| 59 | } |
| 60 | |
| 61 | void Field::Set32(Object* object, uint32_t new_value) const { |
| 62 | DCHECK(object != NULL) << PrettyField(this); |
| 63 | DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); |
| 64 | object->SetField32(GetOffset(), new_value, IsVolatile()); |
| 65 | } |
| 66 | |
| 67 | uint64_t Field::Get64(const Object* object) const { |
| 68 | DCHECK(object != NULL) << PrettyField(this); |
| 69 | DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); |
| 70 | return object->GetField64(GetOffset(), IsVolatile()); |
| 71 | } |
| 72 | |
| 73 | void Field::Set64(Object* object, uint64_t new_value) const { |
| 74 | DCHECK(object != NULL) << PrettyField(this); |
| 75 | DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); |
| 76 | object->SetField64(GetOffset(), new_value, IsVolatile()); |
| 77 | } |
| 78 | |
| 79 | Object* Field::GetObj(const Object* object) const { |
| 80 | DCHECK(object != NULL) << PrettyField(this); |
| 81 | DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); |
| 82 | return object->GetFieldObject<Object*>(GetOffset(), IsVolatile()); |
| 83 | } |
| 84 | |
| 85 | void Field::SetObj(Object* object, const Object* new_value) const { |
| 86 | DCHECK(object != NULL) << PrettyField(this); |
| 87 | DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); |
| 88 | object->SetFieldObject(GetOffset(), new_value, IsVolatile()); |
| 89 | } |
| 90 | |
| 91 | bool Field::GetBoolean(const Object* object) const { |
| 92 | DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 93 | << PrettyField(this); |
| 94 | return Get32(object); |
| 95 | } |
| 96 | |
| 97 | void Field::SetBoolean(Object* object, bool z) const { |
| 98 | DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 99 | << PrettyField(this); |
| 100 | Set32(object, z); |
| 101 | } |
| 102 | |
| 103 | int8_t Field::GetByte(const Object* object) const { |
| 104 | DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 105 | << PrettyField(this); |
| 106 | return Get32(object); |
| 107 | } |
| 108 | |
| 109 | void Field::SetByte(Object* object, int8_t b) const { |
| 110 | DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 111 | << PrettyField(this); |
| 112 | Set32(object, b); |
| 113 | } |
| 114 | |
| 115 | uint16_t Field::GetChar(const Object* object) const { |
| 116 | DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 117 | << PrettyField(this); |
| 118 | return Get32(object); |
| 119 | } |
| 120 | |
| 121 | void Field::SetChar(Object* object, uint16_t c) const { |
| 122 | DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 123 | << PrettyField(this); |
| 124 | Set32(object, c); |
| 125 | } |
| 126 | |
| 127 | int16_t Field::GetShort(const Object* object) const { |
| 128 | DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 129 | << PrettyField(this); |
| 130 | return Get32(object); |
| 131 | } |
| 132 | |
| 133 | void Field::SetShort(Object* object, int16_t s) const { |
| 134 | DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 135 | << PrettyField(this); |
| 136 | Set32(object, s); |
| 137 | } |
| 138 | |
| 139 | int32_t Field::GetInt(const Object* object) const { |
| 140 | #ifndef NDEBUG |
| 141 | Primitive::Type type = FieldHelper(this).GetTypeAsPrimitiveType(); |
| 142 | CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField(this); |
| 143 | #endif |
| 144 | return Get32(object); |
| 145 | } |
| 146 | |
| 147 | void Field::SetInt(Object* object, int32_t i) const { |
| 148 | #ifndef NDEBUG |
| 149 | Primitive::Type type = FieldHelper(this).GetTypeAsPrimitiveType(); |
| 150 | CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField(this); |
| 151 | #endif |
| 152 | Set32(object, i); |
| 153 | } |
| 154 | |
| 155 | int64_t Field::GetLong(const Object* object) const { |
| 156 | #ifndef NDEBUG |
| 157 | Primitive::Type type = FieldHelper(this).GetTypeAsPrimitiveType(); |
| 158 | CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField(this); |
| 159 | #endif |
| 160 | return Get64(object); |
| 161 | } |
| 162 | |
| 163 | void Field::SetLong(Object* object, int64_t j) const { |
| 164 | #ifndef NDEBUG |
| 165 | Primitive::Type type = FieldHelper(this).GetTypeAsPrimitiveType(); |
| 166 | CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField(this); |
| 167 | #endif |
| 168 | Set64(object, j); |
| 169 | } |
| 170 | |
| 171 | union Bits { |
| 172 | jdouble d; |
| 173 | jfloat f; |
| 174 | jint i; |
| 175 | jlong j; |
| 176 | }; |
| 177 | |
| 178 | float Field::GetFloat(const Object* object) const { |
| 179 | DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 180 | << PrettyField(this); |
| 181 | Bits bits; |
| 182 | bits.i = Get32(object); |
| 183 | return bits.f; |
| 184 | } |
| 185 | |
| 186 | void Field::SetFloat(Object* object, float f) const { |
| 187 | DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 188 | << PrettyField(this); |
| 189 | Bits bits; |
| 190 | bits.f = f; |
| 191 | Set32(object, bits.i); |
| 192 | } |
| 193 | |
| 194 | double Field::GetDouble(const Object* object) const { |
| 195 | DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 196 | << PrettyField(this); |
| 197 | Bits bits; |
| 198 | bits.j = Get64(object); |
| 199 | return bits.d; |
| 200 | } |
| 201 | |
| 202 | void Field::SetDouble(Object* object, double d) const { |
| 203 | DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 204 | << PrettyField(this); |
| 205 | Bits bits; |
| 206 | bits.d = d; |
| 207 | Set64(object, bits.j); |
| 208 | } |
| 209 | |
| 210 | Object* Field::GetObject(const Object* object) const { |
| 211 | DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 212 | << PrettyField(this); |
| 213 | return GetObj(object); |
| 214 | } |
| 215 | |
| 216 | void Field::SetObject(Object* object, const Object* l) const { |
| 217 | DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 218 | << PrettyField(this); |
| 219 | SetObj(object, l); |
| 220 | } |
| 221 | |
| 222 | } // namespace mirror |
| 223 | } // namespace art |