Be more careful in the Thumb decoder hooks to avoid walking off the end of the OpInfo...
authorOwen Anderson <resistor@mac.com>
Wed, 17 Aug 2011 18:14:48 +0000 (18:14 +0000)
committerOwen Anderson <resistor@mac.com>
Wed, 17 Aug 2011 18:14:48 +0000 (18:14 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@137838 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/ARM/Disassembler/ARMDisassembler.cpp

index 98cdc64746af98515afd072569db37a4bc20b386..a585dcbb291dc729a63c471278d1c2b115b075c7 100644 (file)
@@ -329,17 +329,18 @@ extern MCInstrDesc ARMInsts[];
 // that as a post-pass.
 static void AddThumb1SBit(MCInst &MI, bool InITBlock) {
   const MCOperandInfo *OpInfo = ARMInsts[MI.getOpcode()].OpInfo;
+  unsigned short NumOps = ARMInsts[MI.getOpcode()].NumOperands;
   MCInst::iterator I = MI.begin();
-  for (unsigned i = 0, e = MI.size(); i < e; ++i, ++I) {
+  for (unsigned i = 0; i < NumOps; ++i, ++I) {
+    if (I == MI.end()) break;
     if (OpInfo[i].isOptionalDef() && OpInfo[i].RegClass == ARM::CCRRegClassID) {
+      if (i > 0 && OpInfo[i-1].isPredicate()) continue;
       MI.insert(I, MCOperand::CreateReg(InITBlock ? 0 : ARM::CPSR));
       return;
     }
   }
 
-  if (OpInfo[MI.size()].isOptionalDef() &&
-      OpInfo[MI.size()].RegClass == ARM::CCRRegClassID)
-    MI.insert(MI.end(), MCOperand::CreateReg(InITBlock ? 0 : ARM::CPSR));
+  MI.insert(I, MCOperand::CreateReg(InITBlock ? 0 : ARM::CPSR));
 }
 
 // Most Thumb instructions don't have explicit predicates in the
@@ -367,8 +368,10 @@ void ThumbDisassembler::AddThumbPredicate(MCInst &MI) const {
     CC = ARMCC::AL;
 
   const MCOperandInfo *OpInfo = ARMInsts[MI.getOpcode()].OpInfo;
+  unsigned short NumOps = ARMInsts[MI.getOpcode()].NumOperands;
   MCInst::iterator I = MI.begin();
-  for (unsigned i = 0, e = MI.size(); i < e; ++i, ++I) {
+  for (unsigned i = 0; i < NumOps; ++i, ++I) {
+    if (I == MI.end()) break;
     if (OpInfo[i].isPredicate()) {
       I = MI.insert(I, MCOperand::CreateImm(CC));
       ++I;
@@ -380,11 +383,12 @@ void ThumbDisassembler::AddThumbPredicate(MCInst &MI) const {
     }
   }
 
-  MI.insert(MI.end(), MCOperand::CreateImm(CC));
+  I = MI.insert(I, MCOperand::CreateImm(CC));
+  ++I;
   if (CC == ARMCC::AL)
-    MI.insert(MI.end(), MCOperand::CreateReg(0));
+    MI.insert(I, MCOperand::CreateReg(0));
   else
-    MI.insert(MI.end(), MCOperand::CreateReg(ARM::CPSR));
+    MI.insert(I, MCOperand::CreateReg(ARM::CPSR));
 }
 
 // Thumb VFP instructions are a special case.  Because we share their