Rename DIExpressionIterator to DIExpression::iterator.
[opencl/llvm.git] / lib / CodeGen / AsmPrinter / DwarfExpression.cpp
index 26c2a20dea19f7c51bc77c05ce72895bd82e0d56..eb101ca2dcbbeee70ee45ee63f284a560e39d43b 100644 (file)
@@ -12,7 +12,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "DwarfExpression.h"
-
 #include "DwarfDebug.h"
 #include "llvm/ADT/SmallBitVector.h"
 #include "llvm/CodeGen/AsmPrinter.h"
@@ -89,16 +88,10 @@ bool DwarfExpression::AddMachineRegIndirect(unsigned MachineReg, int Offset) {
   return true;
 }
 
-void DwarfExpression::AddMachineRegPiece(unsigned MachineReg,
+bool DwarfExpression::AddMachineRegPiece(unsigned MachineReg,
                                          unsigned PieceSizeInBits,
                                          unsigned PieceOffsetInBits) {
   const TargetRegisterInfo *TRI = getTRI();
-  if (!TRI->isPhysicalRegister(MachineReg)) {
-    // FIXME: We have no reasonable way of handling errors in here.
-    EmitOp(dwarf::DW_OP_nop, "nop (could not find a dwarf register number)");
-    return;
-  }
-
   int Reg = TRI->getDwarfRegNum(MachineReg, false);
 
   // If this is a valid register number, emit it.
@@ -106,7 +99,7 @@ void DwarfExpression::AddMachineRegPiece(unsigned MachineReg,
     AddReg(Reg);
     if (PieceSizeInBits)
       AddOpPiece(PieceSizeInBits, PieceOffsetInBits);
-    return;
+    return true;
   }
 
   // Walk up the super-register chain until we find a valid number.
@@ -129,7 +122,7 @@ void DwarfExpression::AddMachineRegPiece(unsigned MachineReg,
           AddShr(RegOffset);
         AddOpPiece(Size, PieceOffsetInBits);
       }
-      return;
+      return true;
     }
   }
 
@@ -168,9 +161,7 @@ void DwarfExpression::AddMachineRegPiece(unsigned MachineReg,
     }
   }
 
-  if (CurPos == PieceOffsetInBits)
-    // FIXME: We have no reasonable way of handling errors in here.
-    EmitOp(dwarf::DW_OP_nop, "nop (could not find a dwarf register number)");
+  return CurPos > PieceOffsetInBits;
 }
 
 void DwarfExpression::AddSignedConstant(int Value) {
@@ -206,52 +197,65 @@ static unsigned getOffsetOrZero(unsigned OffsetInBits,
   return OffsetInBits;
 }
 
-void DwarfExpression::AddMachineRegExpression(DIExpression Expr,
+bool DwarfExpression::AddMachineRegExpression(DIExpression Expr,
                                               unsigned MachineReg,
                                               unsigned PieceOffsetInBits) {
-  unsigned N = Expr.getNumElements();
-  unsigned I = 0;
+  auto I = Expr.begin();
   // Pattern-match combinations for which more efficient representations exist
   // first.
-  if (N >= 3 && Expr.getElement(0) == dwarf::DW_OP_piece) {
+  if (I == Expr.end())
+    return AddMachineRegPiece(MachineReg);
+
+  bool ValidReg = false;
+  switch (*I) {
+  case dwarf::DW_OP_piece: {
     unsigned SizeOfByte = 8;
-    unsigned OffsetInBits = Expr.getElement(1) * SizeOfByte;
-    unsigned SizeInBits = Expr.getElement(2) * SizeOfByte;
-    AddMachineRegPiece(MachineReg, SizeInBits,
-                       getOffsetOrZero(OffsetInBits, PieceOffsetInBits));
-    I = 3;
-  } else if (N >= 3 && Expr.getElement(0) == dwarf::DW_OP_plus &&
-             Expr.getElement(2) == dwarf::DW_OP_deref) {
+    unsigned OffsetInBits = I.getArg(1) * SizeOfByte;
+    unsigned SizeInBits   = I.getArg(2) * SizeOfByte;
+    // Piece always comes at the end of the expression.
+    return AddMachineRegPiece(MachineReg, SizeInBits,
+               getOffsetOrZero(OffsetInBits, PieceOffsetInBits));
+  }
+  case dwarf::DW_OP_plus:
     // [DW_OP_reg,Offset,DW_OP_plus,DW_OP_deref] --> [DW_OP_breg,Offset].
-    unsigned Offset = Expr.getElement(1);
-    AddMachineRegIndirect(MachineReg, Offset);
-    I = 3;
-  } else if (N >= 1 && Expr.getElement(0) == dwarf::DW_OP_deref) {
+    if (*std::next(I) == dwarf::DW_OP_deref) {
+      unsigned Offset = I.getArg(1);
+      ValidReg = AddMachineRegIndirect(MachineReg, Offset);
+      std::advance(I, 2);
+      break;
+    } else
+      ValidReg = AddMachineRegPiece(MachineReg);
+  case dwarf::DW_OP_deref:
     // [DW_OP_reg,DW_OP_deref] --> [DW_OP_breg].
-    AddMachineRegIndirect(MachineReg);
-    I = 1;
-  } else
-    AddMachineRegPiece(MachineReg);
+    ValidReg = AddMachineRegIndirect(MachineReg);
+    ++I;
+    break;
+  default:
+    llvm_unreachable("unsupported operand");
+  }
+
+  if (!ValidReg)
+    return false;
 
   // Emit remaining elements of the expression.
-  AddExpression(Expr, I);
+  AddExpression(I, PieceOffsetInBits);
+  return true;
 }
 
-void DwarfExpression::AddExpression(DIExpression Expr, unsigned I,
+void DwarfExpression::AddExpression(DIExpression::iterator I,
                                     unsigned PieceOffsetInBits) {
-  unsigned N = Expr.getNumElements();
-  for (; I < N; ++I) {
-    switch (Expr.getElement(I)) {
+ for (; I != DIExpression::iterator(); ++I) {
+    switch (*I) {
     case dwarf::DW_OP_piece: {
       unsigned SizeOfByte = 8;
-      unsigned OffsetInBits = Expr.getElement(++I) * SizeOfByte;
-      unsigned SizeInBits = Expr.getElement(++I) * SizeOfByte;
+      unsigned OffsetInBits = I.getArg(1) * SizeOfByte;
+      unsigned SizeInBits   = I.getArg(2) * SizeOfByte;
       AddOpPiece(SizeInBits, getOffsetOrZero(OffsetInBits, PieceOffsetInBits));
       break;
     }
     case dwarf::DW_OP_plus:
       EmitOp(dwarf::DW_OP_plus_uconst);
-      EmitUnsigned(Expr.getElement(++I));
+      EmitUnsigned(I.getArg(1));
       break;
     case dwarf::DW_OP_deref:
       EmitOp(dwarf::DW_OP_deref);