blob: e445a9170e8310da3a30819c489dd1088c651ad9 [file] [log] [blame]
Christopher Ferris559c7f22018-02-12 20:18:03 -08001/*
2 * Copyright (C) 2018 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 _LIBUNWINDSTACK_REGS_INFO_H
18#define _LIBUNWINDSTACK_REGS_INFO_H
19
20#include <stdint.h>
21
22#include <unwindstack/Regs.h>
23
24namespace unwindstack {
25
26template <typename AddressType>
27struct RegsInfo {
Christopher Ferris27866082018-08-02 15:21:37 -070028 static constexpr size_t MAX_REGISTERS = 64;
29
Christopher Ferris559c7f22018-02-12 20:18:03 -080030 RegsInfo(RegsImpl<AddressType>* regs) : regs(regs) {}
31
32 RegsImpl<AddressType>* regs = nullptr;
33 uint64_t saved_reg_map = 0;
Christopher Ferris27866082018-08-02 15:21:37 -070034 AddressType saved_regs[MAX_REGISTERS];
Christopher Ferris559c7f22018-02-12 20:18:03 -080035
36 inline AddressType Get(uint32_t reg) {
37 if (IsSaved(reg)) {
38 return saved_regs[reg];
39 }
40 return (*regs)[reg];
41 }
42
43 inline AddressType* Save(uint32_t reg) {
Florian Mayer9c36a992018-10-23 14:09:48 +010044 if (reg >= MAX_REGISTERS) {
Christopher Ferris27866082018-08-02 15:21:37 -070045 // This should never happen since all currently supported
46 // architectures have < 64 total registers.
Christopher Ferris559c7f22018-02-12 20:18:03 -080047 abort();
48 }
Christopher Ferris27866082018-08-02 15:21:37 -070049 saved_reg_map |= 1ULL << reg;
Christopher Ferris559c7f22018-02-12 20:18:03 -080050 saved_regs[reg] = (*regs)[reg];
51 return &(*regs)[reg];
52 }
53
54 inline bool IsSaved(uint32_t reg) {
Christopher Ferris27866082018-08-02 15:21:37 -070055 if (reg > MAX_REGISTERS) {
56 // This should never happen since all currently supported
57 // architectures have < 64 total registers.
Christopher Ferris559c7f22018-02-12 20:18:03 -080058 abort();
59 }
Christopher Ferris27866082018-08-02 15:21:37 -070060 return saved_reg_map & (1ULL << reg);
Christopher Ferris559c7f22018-02-12 20:18:03 -080061 }
62
63 inline uint16_t Total() { return regs->total_regs(); }
64};
65
66} // namespace unwindstack
67
68#endif // _LIBUNWINDSTACK_REGS_INFO_H