summaryrefslogtreecommitdiff
path: root/compiler/optimizing/intrinsic_objects.cc
blob: c625d435ae7b4d982ff963c553785f43ac0354ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "intrinsic_objects.h"

#include "art_field-inl.h"
#include "base/casts.h"
#include "base/logging.h"
#include "intrinsics.h"
#include "oat/image.h"
#include "obj_ptr-inl.h"
#include "well_known_classes.h"

namespace art HIDDEN {

static constexpr size_t kIntrinsicObjectsOffset =
    enum_cast<size_t>(ImageHeader::kIntrinsicObjectsStart);

template <typename T>
static int32_t FillIntrinsicsObjects(
    ArtField* cache_field,
    ObjPtr<mirror::ObjectArray<mirror::Object>> live_objects,
    int32_t expected_low,
    int32_t expected_high,
    T&& type_check,
    int32_t index)
    REQUIRES_SHARED(Locks::mutator_lock_) {
  ObjPtr<mirror::ObjectArray<mirror::Object>> cache =
      ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast(
          cache_field->GetObject(cache_field->GetDeclaringClass()));
  int32_t length = expected_high - expected_low + 1;
  DCHECK_EQ(length, cache->GetLength());
  for (int32_t i = 0; i != length; ++i) {
    ObjPtr<mirror::Object> value = cache->GetWithoutChecks(i);
    live_objects->Set(index + i, value);
    type_check(value, expected_low + i);
  }
  return index + length;
}

void IntrinsicObjects::FillIntrinsicObjects(
    ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects, size_t start_index) {
  DCHECK_EQ(start_index, ImageHeader::kIntrinsicObjectsStart);
  int32_t index = dchecked_integral_cast<int32_t>(start_index);
#define FILL_OBJECTS(name, low, high, type, offset) \
  index = FillIntrinsicsObjects( \
      WellKnownClasses::java_lang_ ##name ##_ ##name ##Cache_cache, \
      boot_image_live_objects, \
      low, \
      high, \
      [](ObjPtr<mirror::Object> obj, int32_t expected) REQUIRES_SHARED(Locks::mutator_lock_) { \
        CHECK_EQ(expected, WellKnownClasses::java_lang_ ##name ##_value->Get ##name(obj)); \
      }, \
      index);
  BOXED_TYPES(FILL_OBJECTS)
#undef FILL_OBJECTS
  DCHECK_EQ(dchecked_integral_cast<size_t>(index), start_index + GetNumberOfIntrinsicObjects());
}

static bool HasIntrinsicObjects(
    ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects)
    REQUIRES_SHARED(Locks::mutator_lock_) {
  DCHECK(boot_image_live_objects != nullptr);
  uint32_t length = static_cast<uint32_t>(boot_image_live_objects->GetLength());
  DCHECK_GE(length, kIntrinsicObjectsOffset);
  return length != kIntrinsicObjectsOffset;
}

ObjPtr<mirror::Object> IntrinsicObjects::GetValueOfObject(
    ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects,
    size_t start_index,
    uint32_t index) {
  DCHECK(HasIntrinsicObjects(boot_image_live_objects));
  // No need for read barrier for boot image object or for verifying the value that was just stored.
  ObjPtr<mirror::Object> result =
      boot_image_live_objects->GetWithoutChecks<kVerifyNone, kWithoutReadBarrier>(
          kIntrinsicObjectsOffset + start_index + index);
  DCHECK(result != nullptr);
  return result;
}

MemberOffset IntrinsicObjects::GetValueOfArrayDataOffset(
    ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects,
    size_t start_index) {
  DCHECK(HasIntrinsicObjects(boot_image_live_objects));
  MemberOffset result =
      mirror::ObjectArray<mirror::Object>::OffsetOfElement(kIntrinsicObjectsOffset + start_index);
  DCHECK_EQ(GetValueOfObject(boot_image_live_objects, start_index, 0u),
            (boot_image_live_objects
                 ->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(result)));
  return result;
}

}  // namespace art