Add a way to define the bit range covered by a SubRegIndex.
[oota-llvm.git] / lib / MC / MCRegisterInfo.cpp
1 //=== MC/MCRegisterInfo.cpp - Target Register Description -------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements MCRegisterInfo functions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/MC/MCRegisterInfo.h"
15
16 using namespace llvm;
17
18 unsigned MCRegisterInfo::getMatchingSuperReg(unsigned Reg, unsigned SubIdx,
19                                              const MCRegisterClass *RC) const {
20   for (MCSuperRegIterator Supers(Reg, this); Supers.isValid(); ++Supers)
21     if (RC->contains(*Supers) && Reg == getSubReg(*Supers, SubIdx))
22       return *Supers;
23   return 0;
24 }
25
26 unsigned MCRegisterInfo::getSubReg(unsigned Reg, unsigned Idx) const {
27   assert(Idx && Idx < getNumSubRegIndices() &&
28          "This is not a subregister index");
29   // Get a pointer to the corresponding SubRegIndices list. This list has the
30   // name of each sub-register in the same order as MCSubRegIterator.
31   const uint16_t *SRI = SubRegIndices + get(Reg).SubRegIndices;
32   for (MCSubRegIterator Subs(Reg, this); Subs.isValid(); ++Subs, ++SRI)
33     if (*SRI == Idx)
34       return *Subs;
35   return 0;
36 }
37
38 unsigned MCRegisterInfo::getSubRegIndex(unsigned Reg, unsigned SubReg) const {
39   assert(SubReg && SubReg < getNumRegs() && "This is not a register");
40   // Get a pointer to the corresponding SubRegIndices list. This list has the
41   // name of each sub-register in the same order as MCSubRegIterator.
42   const uint16_t *SRI = SubRegIndices + get(Reg).SubRegIndices;
43   for (MCSubRegIterator Subs(Reg, this); Subs.isValid(); ++Subs, ++SRI)
44     if (*Subs == SubReg)
45       return *SRI;
46   return 0;
47 }
48
49 bool MCRegisterInfo::getSubRegIdxCoveredBits(unsigned Idx, unsigned &Offset,
50                                              unsigned &Size) const {
51   assert(Idx && Idx < getNumSubRegIndices() &&
52          "This is not a subregister index");
53   // Get a pointer to the corresponding SubRegIdxRanges struct.
54   const SubRegCoveredBits *Bits = &SubRegIdxRanges[Idx];
55   if (Bits->Offset == (uint16_t)-1 || Bits->Size == (uint16_t)-1)
56     return false;
57   Offset = Bits->Offset;
58   Size = Bits->Size;
59   return true;
60 }
61
62 int MCRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
63   const DwarfLLVMRegPair *M = isEH ? EHL2DwarfRegs : L2DwarfRegs;
64   unsigned Size = isEH ? EHL2DwarfRegsSize : L2DwarfRegsSize;
65
66   DwarfLLVMRegPair Key = { RegNum, 0 };
67   const DwarfLLVMRegPair *I = std::lower_bound(M, M+Size, Key);
68   if (I == M+Size || I->FromReg != RegNum)
69     return -1;
70   return I->ToReg;
71 }
72
73 int MCRegisterInfo::getLLVMRegNum(unsigned RegNum, bool isEH) const {
74   const DwarfLLVMRegPair *M = isEH ? EHDwarf2LRegs : Dwarf2LRegs;
75   unsigned Size = isEH ? EHDwarf2LRegsSize : Dwarf2LRegsSize;
76
77   DwarfLLVMRegPair Key = { RegNum, 0 };
78   const DwarfLLVMRegPair *I = std::lower_bound(M, M+Size, Key);
79   assert(I != M+Size && I->FromReg == RegNum && "Invalid RegNum");
80   return I->ToReg;
81 }
82
83 int MCRegisterInfo::getSEHRegNum(unsigned RegNum) const {
84   const DenseMap<unsigned, int>::const_iterator I = L2SEHRegs.find(RegNum);
85   if (I == L2SEHRegs.end()) return (int)RegNum;
86   return I->second;
87 }