Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 24 | namespace unwindstack { |
| 25 | |
| 26 | template <typename AddressType> |
| 27 | struct RegsInfo { |
Christopher Ferris | 2786608 | 2018-08-02 15:21:37 -0700 | [diff] [blame] | 28 | static constexpr size_t MAX_REGISTERS = 64; |
| 29 | |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 30 | RegsInfo(RegsImpl<AddressType>* regs) : regs(regs) {} |
| 31 | |
| 32 | RegsImpl<AddressType>* regs = nullptr; |
| 33 | uint64_t saved_reg_map = 0; |
Christopher Ferris | 2786608 | 2018-08-02 15:21:37 -0700 | [diff] [blame] | 34 | AddressType saved_regs[MAX_REGISTERS]; |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 35 | |
| 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 Mayer | 9c36a99 | 2018-10-23 14:09:48 +0100 | [diff] [blame] | 44 | if (reg >= MAX_REGISTERS) { |
Christopher Ferris | 2786608 | 2018-08-02 15:21:37 -0700 | [diff] [blame] | 45 | // This should never happen since all currently supported |
| 46 | // architectures have < 64 total registers. |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 47 | abort(); |
| 48 | } |
Christopher Ferris | 2786608 | 2018-08-02 15:21:37 -0700 | [diff] [blame] | 49 | saved_reg_map |= 1ULL << reg; |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 50 | saved_regs[reg] = (*regs)[reg]; |
| 51 | return &(*regs)[reg]; |
| 52 | } |
| 53 | |
| 54 | inline bool IsSaved(uint32_t reg) { |
Christopher Ferris | 2786608 | 2018-08-02 15:21:37 -0700 | [diff] [blame] | 55 | if (reg > MAX_REGISTERS) { |
| 56 | // This should never happen since all currently supported |
| 57 | // architectures have < 64 total registers. |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 58 | abort(); |
| 59 | } |
Christopher Ferris | 2786608 | 2018-08-02 15:21:37 -0700 | [diff] [blame] | 60 | return saved_reg_map & (1ULL << reg); |
Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | inline uint16_t Total() { return regs->total_regs(); } |
| 64 | }; |
| 65 | |
| 66 | } // namespace unwindstack |
| 67 | |
| 68 | #endif // _LIBUNWINDSTACK_REGS_INFO_H |