blob: db9e1ea5cbd37a39acd71cdab015d89a8c195cae [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);
buzbeeb5860fb2014-06-21 15:31:01 -070067 bool high_reg = (kind == kLongHiVReg) || (kind == kDoubleHiVReg);
Andreas Gampe1a5c4062015-01-15 12:10:47 -080068 bool target64 = (kRuntimeISA == kArm64) || (kRuntimeISA == kX86_64) || (kRuntimeISA == kMips64);
buzbeeb5860fb2014-06-21 15:31:01 -070069 if (target64 && high_reg) {
70 // Wide promoted registers are associated with the sreg of the low portion.
71 adjusted_vreg--;
72 }
Ian Rogers96faf5b2013-08-09 22:05:32 -070073 for (size_t i = 0; i < end; ++i) {
74 // Stop if we find what we are are looking for.
Vladimir Marko2e589aa2014-02-25 17:53:53 +000075 uint16_t adjusted_entry = DecodeUnsignedLeb128(&table);
76 if ((adjusted_entry == adjusted_vreg) && (in_floats == is_float)) {
Ian Rogers96faf5b2013-08-09 22:05:32 -070077 *vmap_offset = i;
78 return true;
79 }
80 // 0xffff is the marker for LR (return PC on x86), following it are spilled float registers.
Vladimir Marko2e589aa2014-02-25 17:53:53 +000081 if (adjusted_entry == kAdjustedFpMarker) {
Ian Rogers96faf5b2013-08-09 22:05:32 -070082 in_floats = true;
83 }
84 }
85 return false;
86 }
87
88 // Compute the register number that corresponds to the entry in the vmap (vmap_offset, computed
89 // by IsInContext above). If the kind is floating point then the result will be a floating point
90 // register number, otherwise it will be an integer register number.
91 uint32_t ComputeRegister(uint32_t spill_mask, uint32_t vmap_offset, VRegKind kind) const {
92 // Compute the register we need to load from the context.
93 DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg ||
94 kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg ||
95 kind == kDoubleHiVReg || kind == kImpreciseConstant);
96 // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values
97 // are never promoted to floating point registers.
98 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
99 uint32_t matches = 0;
100 if (UNLIKELY(is_float)) {
101 const uint8_t* table = table_;
102 DecodeUnsignedLeb128(&table); // Skip size.
Vladimir Marko2e589aa2014-02-25 17:53:53 +0000103 while (DecodeUnsignedLeb128(&table) != kAdjustedFpMarker) {
Ian Rogers96faf5b2013-08-09 22:05:32 -0700104 matches++;
105 }
106 matches++;
107 }
Vladimir Marko81949632014-05-02 11:53:22 +0100108 CHECK_LT(vmap_offset - matches, static_cast<uint32_t>(POPCOUNT(spill_mask)));
Ian Rogers96faf5b2013-08-09 22:05:32 -0700109 uint32_t spill_shifts = 0;
110 while (matches != (vmap_offset + 1)) {
111 DCHECK_NE(spill_mask, 0u);
112 matches += spill_mask & 1; // Add 1 if the low bit is set
113 spill_mask >>= 1;
114 spill_shifts++;
115 }
116 spill_shifts--; // wind back one as we want the last match
117 return spill_shifts;
118 }
119
120 private:
121 const uint8_t* const table_;
122};
123
124} // namespace art
125
126#endif // ART_RUNTIME_VMAP_TABLE_H_