blob: 98217535ee5485cb641bdbeb0d48c1bd8d84a969 [file] [log] [blame]
Ian Rogers96faf5b2013-08-09 22:05:32 -07001/*
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#ifndef ART_RUNTIME_VMAP_TABLE_H_
18#define ART_RUNTIME_VMAP_TABLE_H_
19
20#include "base/logging.h"
21#include "leb128.h"
22#include "stack.h"
23
24namespace art {
25
26class VmapTable {
27 public:
Vladimir Marko2e589aa2014-02-25 17:53:53 +000028 // For efficient encoding of special values, entries are adjusted by 2.
29 static constexpr uint16_t kEntryAdjustment = 2u;
30 static constexpr uint16_t kAdjustedFpMarker = static_cast<uint16_t>(0xffffu + kEntryAdjustment);
31
Ian Rogers96faf5b2013-08-09 22:05:32 -070032 explicit VmapTable(const uint8_t* table) : table_(table) {
33 }
34
35 // Look up nth entry, not called from performance critical code.
36 uint16_t operator[](size_t n) const {
37 const uint8_t* table = table_;
38 size_t size = DecodeUnsignedLeb128(&table);
39 CHECK_LT(n, size);
Vladimir Marko2e589aa2014-02-25 17:53:53 +000040 uint16_t adjusted_entry = DecodeUnsignedLeb128(&table);
Ian Rogers96faf5b2013-08-09 22:05:32 -070041 for (size_t i = 0; i < n; ++i) {
Vladimir Marko2e589aa2014-02-25 17:53:53 +000042 adjusted_entry = DecodeUnsignedLeb128(&table);
Ian Rogers96faf5b2013-08-09 22:05:32 -070043 }
Vladimir Marko2e589aa2014-02-25 17:53:53 +000044 return adjusted_entry - kEntryAdjustment;
Ian Rogers96faf5b2013-08-09 22:05:32 -070045 }
46
47 size_t Size() const {
48 const uint8_t* table = table_;
49 return DecodeUnsignedLeb128(&table);
50 }
51
52 // Is the dex register 'vreg' in the context or on the stack? Should not be called when the
53 // 'kind' is unknown or constant.
54 bool IsInContext(size_t vreg, VRegKind kind, uint32_t* vmap_offset) const {
55 DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg ||
56 kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg ||
57 kind == kDoubleHiVReg || kind == kImpreciseConstant);
58 *vmap_offset = 0xEBAD0FF5;
59 // TODO: take advantage of the registers being ordered
60 // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values
61 // are never promoted to floating point registers.
62 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
63 bool in_floats = false;
64 const uint8_t* table = table_;
Vladimir Marko2e589aa2014-02-25 17:53:53 +000065 uint16_t adjusted_vreg = vreg + kEntryAdjustment;
Ian Rogers96faf5b2013-08-09 22:05:32 -070066 size_t end = DecodeUnsignedLeb128(&table);
67 for (size_t i = 0; i < end; ++i) {
68 // Stop if we find what we are are looking for.
Vladimir Marko2e589aa2014-02-25 17:53:53 +000069 uint16_t adjusted_entry = DecodeUnsignedLeb128(&table);
70 if ((adjusted_entry == adjusted_vreg) && (in_floats == is_float)) {
Ian Rogers96faf5b2013-08-09 22:05:32 -070071 *vmap_offset = i;
72 return true;
73 }
74 // 0xffff is the marker for LR (return PC on x86), following it are spilled float registers.
Vladimir Marko2e589aa2014-02-25 17:53:53 +000075 if (adjusted_entry == kAdjustedFpMarker) {
Ian Rogers96faf5b2013-08-09 22:05:32 -070076 in_floats = true;
77 }
78 }
79 return false;
80 }
81
82 // Compute the register number that corresponds to the entry in the vmap (vmap_offset, computed
83 // by IsInContext above). If the kind is floating point then the result will be a floating point
84 // register number, otherwise it will be an integer register number.
85 uint32_t ComputeRegister(uint32_t spill_mask, uint32_t vmap_offset, VRegKind kind) const {
86 // Compute the register we need to load from the context.
87 DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg ||
88 kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg ||
89 kind == kDoubleHiVReg || kind == kImpreciseConstant);
90 // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values
91 // are never promoted to floating point registers.
92 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
93 uint32_t matches = 0;
94 if (UNLIKELY(is_float)) {
95 const uint8_t* table = table_;
96 DecodeUnsignedLeb128(&table); // Skip size.
Vladimir Marko2e589aa2014-02-25 17:53:53 +000097 while (DecodeUnsignedLeb128(&table) != kAdjustedFpMarker) {
Ian Rogers96faf5b2013-08-09 22:05:32 -070098 matches++;
99 }
100 matches++;
101 }
Vladimir Marko81949632014-05-02 11:53:22 +0100102 CHECK_LT(vmap_offset - matches, static_cast<uint32_t>(POPCOUNT(spill_mask)));
Ian Rogers96faf5b2013-08-09 22:05:32 -0700103 uint32_t spill_shifts = 0;
104 while (matches != (vmap_offset + 1)) {
105 DCHECK_NE(spill_mask, 0u);
106 matches += spill_mask & 1; // Add 1 if the low bit is set
107 spill_mask >>= 1;
108 spill_shifts++;
109 }
110 spill_shifts--; // wind back one as we want the last match
111 return spill_shifts;
112 }
113
114 private:
115 const uint8_t* const table_;
116};
117
118} // namespace art
119
120#endif // ART_RUNTIME_VMAP_TABLE_H_