Home | History | Annotate | Download | only in libunwindstack
      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 {
     28   RegsInfo(RegsImpl<AddressType>* regs) : regs(regs) {}
     29 
     30   RegsImpl<AddressType>* regs = nullptr;
     31   uint64_t saved_reg_map = 0;
     32   AddressType saved_regs[64];
     33 
     34   inline AddressType Get(uint32_t reg) {
     35     if (IsSaved(reg)) {
     36       return saved_regs[reg];
     37     }
     38     return (*regs)[reg];
     39   }
     40 
     41   inline AddressType* Save(uint32_t reg) {
     42     if (reg > sizeof(saved_regs) / sizeof(AddressType)) {
     43       // This should never happen as since all currently supported
     44       // architectures have the total number of registers < 64.
     45       abort();
     46     }
     47     saved_reg_map |= 1 << reg;
     48     saved_regs[reg] = (*regs)[reg];
     49     return &(*regs)[reg];
     50   }
     51 
     52   inline bool IsSaved(uint32_t reg) {
     53     if (reg > sizeof(saved_regs) / sizeof(AddressType)) {
     54       // This should never happen as since all currently supported
     55       // architectures have the total number of registers < 64.
     56       abort();
     57     }
     58     return saved_reg_map & (1 << reg);
     59   }
     60 
     61   inline uint16_t Total() { return regs->total_regs(); }
     62 };
     63 
     64 }  // namespace unwindstack
     65 
     66 #endif  // _LIBUNWINDSTACK_REGS_INFO_H
     67