ARM VFP pre-UAL mnemonic aliases for fmul[sd].
[oota-llvm.git] / lib / Target / ARM / AsmParser / ARMAsmParser.cpp
1 //===-- ARMAsmParser.cpp - Parse ARM assembly to MCInst instructions ------===//
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 #include "MCTargetDesc/ARMBaseInfo.h"
11 #include "MCTargetDesc/ARMAddressingModes.h"
12 #include "MCTargetDesc/ARMMCExpr.h"
13 #include "llvm/MC/MCParser/MCAsmLexer.h"
14 #include "llvm/MC/MCParser/MCAsmParser.h"
15 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
16 #include "llvm/MC/MCAsmInfo.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCInstrDesc.h"
22 #include "llvm/MC/MCRegisterInfo.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/MC/MCTargetAsmParser.h"
25 #include "llvm/Support/MathExtras.h"
26 #include "llvm/Support/SourceMgr.h"
27 #include "llvm/Support/TargetRegistry.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/ADT/BitVector.h"
30 #include "llvm/ADT/OwningPtr.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/ADT/SmallVector.h"
33 #include "llvm/ADT/StringSwitch.h"
34 #include "llvm/ADT/Twine.h"
35
36 using namespace llvm;
37
38 namespace {
39
40 class ARMOperand;
41
42 enum VectorLaneTy { NoLanes, AllLanes, IndexedLane };
43
44 class ARMAsmParser : public MCTargetAsmParser {
45   MCSubtargetInfo &STI;
46   MCAsmParser &Parser;
47
48   // Map of register aliases registers via the .req directive.
49   StringMap<unsigned> RegisterReqs;
50
51   struct {
52     ARMCC::CondCodes Cond;    // Condition for IT block.
53     unsigned Mask:4;          // Condition mask for instructions.
54                               // Starting at first 1 (from lsb).
55                               //   '1'  condition as indicated in IT.
56                               //   '0'  inverse of condition (else).
57                               // Count of instructions in IT block is
58                               // 4 - trailingzeroes(mask)
59
60     bool FirstCond;           // Explicit flag for when we're parsing the
61                               // First instruction in the IT block. It's
62                               // implied in the mask, so needs special
63                               // handling.
64
65     unsigned CurPosition;     // Current position in parsing of IT
66                               // block. In range [0,3]. Initialized
67                               // according to count of instructions in block.
68                               // ~0U if no active IT block.
69   } ITState;
70   bool inITBlock() { return ITState.CurPosition != ~0U;}
71   void forwardITPosition() {
72     if (!inITBlock()) return;
73     // Move to the next instruction in the IT block, if there is one. If not,
74     // mark the block as done.
75     unsigned TZ = CountTrailingZeros_32(ITState.Mask);
76     if (++ITState.CurPosition == 5 - TZ)
77       ITState.CurPosition = ~0U; // Done with the IT block after this.
78   }
79
80
81   MCAsmParser &getParser() const { return Parser; }
82   MCAsmLexer &getLexer() const { return Parser.getLexer(); }
83
84   void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
85   bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
86
87   int tryParseRegister();
88   bool tryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
89   int tryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
90   bool parseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
91   bool parseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
92   bool parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
93   bool parsePrefix(ARMMCExpr::VariantKind &RefKind);
94   bool parseMemRegOffsetShift(ARM_AM::ShiftOpc &ShiftType,
95                               unsigned &ShiftAmount);
96   bool parseDirectiveWord(unsigned Size, SMLoc L);
97   bool parseDirectiveThumb(SMLoc L);
98   bool parseDirectiveARM(SMLoc L);
99   bool parseDirectiveThumbFunc(SMLoc L);
100   bool parseDirectiveCode(SMLoc L);
101   bool parseDirectiveSyntax(SMLoc L);
102   bool parseDirectiveReq(StringRef Name, SMLoc L);
103   bool parseDirectiveUnreq(SMLoc L);
104
105   StringRef splitMnemonic(StringRef Mnemonic, unsigned &PredicationCode,
106                           bool &CarrySetting, unsigned &ProcessorIMod,
107                           StringRef &ITMask);
108   void getMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
109                              bool &CanAcceptPredicationCode);
110
111   bool isThumb() const {
112     // FIXME: Can tablegen auto-generate this?
113     return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
114   }
115   bool isThumbOne() const {
116     return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) == 0;
117   }
118   bool isThumbTwo() const {
119     return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2);
120   }
121   bool hasV6Ops() const {
122     return STI.getFeatureBits() & ARM::HasV6Ops;
123   }
124   bool hasV7Ops() const {
125     return STI.getFeatureBits() & ARM::HasV7Ops;
126   }
127   void SwitchMode() {
128     unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
129     setAvailableFeatures(FB);
130   }
131   bool isMClass() const {
132     return STI.getFeatureBits() & ARM::FeatureMClass;
133   }
134
135   /// @name Auto-generated Match Functions
136   /// {
137
138 #define GET_ASSEMBLER_HEADER
139 #include "ARMGenAsmMatcher.inc"
140
141   /// }
142
143   OperandMatchResultTy parseITCondCode(SmallVectorImpl<MCParsedAsmOperand*>&);
144   OperandMatchResultTy parseCoprocNumOperand(
145     SmallVectorImpl<MCParsedAsmOperand*>&);
146   OperandMatchResultTy parseCoprocRegOperand(
147     SmallVectorImpl<MCParsedAsmOperand*>&);
148   OperandMatchResultTy parseCoprocOptionOperand(
149     SmallVectorImpl<MCParsedAsmOperand*>&);
150   OperandMatchResultTy parseMemBarrierOptOperand(
151     SmallVectorImpl<MCParsedAsmOperand*>&);
152   OperandMatchResultTy parseProcIFlagsOperand(
153     SmallVectorImpl<MCParsedAsmOperand*>&);
154   OperandMatchResultTy parseMSRMaskOperand(
155     SmallVectorImpl<MCParsedAsmOperand*>&);
156   OperandMatchResultTy parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &O,
157                                    StringRef Op, int Low, int High);
158   OperandMatchResultTy parsePKHLSLImm(SmallVectorImpl<MCParsedAsmOperand*> &O) {
159     return parsePKHImm(O, "lsl", 0, 31);
160   }
161   OperandMatchResultTy parsePKHASRImm(SmallVectorImpl<MCParsedAsmOperand*> &O) {
162     return parsePKHImm(O, "asr", 1, 32);
163   }
164   OperandMatchResultTy parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*>&);
165   OperandMatchResultTy parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*>&);
166   OperandMatchResultTy parseRotImm(SmallVectorImpl<MCParsedAsmOperand*>&);
167   OperandMatchResultTy parseBitfield(SmallVectorImpl<MCParsedAsmOperand*>&);
168   OperandMatchResultTy parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*>&);
169   OperandMatchResultTy parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*>&);
170   OperandMatchResultTy parseFPImm(SmallVectorImpl<MCParsedAsmOperand*>&);
171   OperandMatchResultTy parseVectorList(SmallVectorImpl<MCParsedAsmOperand*>&);
172   OperandMatchResultTy parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index);
173
174   // Asm Match Converter Methods
175   bool cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
176                     const SmallVectorImpl<MCParsedAsmOperand*> &);
177   bool cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
178                     const SmallVectorImpl<MCParsedAsmOperand*> &);
179   bool cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
180                                   const SmallVectorImpl<MCParsedAsmOperand*> &);
181   bool cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
182                                   const SmallVectorImpl<MCParsedAsmOperand*> &);
183   bool cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
184                                   const SmallVectorImpl<MCParsedAsmOperand*> &);
185   bool cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
186                                   const SmallVectorImpl<MCParsedAsmOperand*> &);
187   bool cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
188                                   const SmallVectorImpl<MCParsedAsmOperand*> &);
189   bool cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
190                                   const SmallVectorImpl<MCParsedAsmOperand*> &);
191   bool cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
192                                   const SmallVectorImpl<MCParsedAsmOperand*> &);
193   bool cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
194                              const SmallVectorImpl<MCParsedAsmOperand*> &);
195   bool cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
196                              const SmallVectorImpl<MCParsedAsmOperand*> &);
197   bool cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
198                              const SmallVectorImpl<MCParsedAsmOperand*> &);
199   bool cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
200                              const SmallVectorImpl<MCParsedAsmOperand*> &);
201   bool cvtLdrdPre(MCInst &Inst, unsigned Opcode,
202                   const SmallVectorImpl<MCParsedAsmOperand*> &);
203   bool cvtStrdPre(MCInst &Inst, unsigned Opcode,
204                   const SmallVectorImpl<MCParsedAsmOperand*> &);
205   bool cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
206                                   const SmallVectorImpl<MCParsedAsmOperand*> &);
207   bool cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
208                         const SmallVectorImpl<MCParsedAsmOperand*> &);
209   bool cvtVLDwbFixed(MCInst &Inst, unsigned Opcode,
210                      const SmallVectorImpl<MCParsedAsmOperand*> &);
211   bool cvtVLDwbRegister(MCInst &Inst, unsigned Opcode,
212                         const SmallVectorImpl<MCParsedAsmOperand*> &);
213   bool cvtVSTwbFixed(MCInst &Inst, unsigned Opcode,
214                      const SmallVectorImpl<MCParsedAsmOperand*> &);
215   bool cvtVSTwbRegister(MCInst &Inst, unsigned Opcode,
216                         const SmallVectorImpl<MCParsedAsmOperand*> &);
217
218   bool validateInstruction(MCInst &Inst,
219                            const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
220   bool processInstruction(MCInst &Inst,
221                           const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
222   bool shouldOmitCCOutOperand(StringRef Mnemonic,
223                               SmallVectorImpl<MCParsedAsmOperand*> &Operands);
224
225 public:
226   enum ARMMatchResultTy {
227     Match_RequiresITBlock = FIRST_TARGET_MATCH_RESULT_TY,
228     Match_RequiresNotITBlock,
229     Match_RequiresV6,
230     Match_RequiresThumb2
231   };
232
233   ARMAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
234     : MCTargetAsmParser(), STI(_STI), Parser(_Parser) {
235     MCAsmParserExtension::Initialize(_Parser);
236
237     // Initialize the set of available features.
238     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
239
240     // Not in an ITBlock to start with.
241     ITState.CurPosition = ~0U;
242   }
243
244   // Implementation of the MCTargetAsmParser interface:
245   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
246   bool ParseInstruction(StringRef Name, SMLoc NameLoc,
247                         SmallVectorImpl<MCParsedAsmOperand*> &Operands);
248   bool ParseDirective(AsmToken DirectiveID);
249
250   unsigned checkTargetMatchPredicate(MCInst &Inst);
251
252   bool MatchAndEmitInstruction(SMLoc IDLoc,
253                                SmallVectorImpl<MCParsedAsmOperand*> &Operands,
254                                MCStreamer &Out);
255 };
256 } // end anonymous namespace
257
258 namespace {
259
260 /// ARMOperand - Instances of this class represent a parsed ARM machine
261 /// instruction.
262 class ARMOperand : public MCParsedAsmOperand {
263   enum KindTy {
264     k_CondCode,
265     k_CCOut,
266     k_ITCondMask,
267     k_CoprocNum,
268     k_CoprocReg,
269     k_CoprocOption,
270     k_Immediate,
271     k_FPImmediate,
272     k_MemBarrierOpt,
273     k_Memory,
274     k_PostIndexRegister,
275     k_MSRMask,
276     k_ProcIFlags,
277     k_VectorIndex,
278     k_Register,
279     k_RegisterList,
280     k_DPRRegisterList,
281     k_SPRRegisterList,
282     k_VectorList,
283     k_VectorListAllLanes,
284     k_VectorListIndexed,
285     k_ShiftedRegister,
286     k_ShiftedImmediate,
287     k_ShifterImmediate,
288     k_RotateImmediate,
289     k_BitfieldDescriptor,
290     k_Token
291   } Kind;
292
293   SMLoc StartLoc, EndLoc;
294   SmallVector<unsigned, 8> Registers;
295
296   union {
297     struct {
298       ARMCC::CondCodes Val;
299     } CC;
300
301     struct {
302       unsigned Val;
303     } Cop;
304
305     struct {
306       unsigned Val;
307     } CoprocOption;
308
309     struct {
310       unsigned Mask:4;
311     } ITMask;
312
313     struct {
314       ARM_MB::MemBOpt Val;
315     } MBOpt;
316
317     struct {
318       ARM_PROC::IFlags Val;
319     } IFlags;
320
321     struct {
322       unsigned Val;
323     } MMask;
324
325     struct {
326       const char *Data;
327       unsigned Length;
328     } Tok;
329
330     struct {
331       unsigned RegNum;
332     } Reg;
333
334     // A vector register list is a sequential list of 1 to 4 registers.
335     struct {
336       unsigned RegNum;
337       unsigned Count;
338       unsigned LaneIndex;
339       bool isDoubleSpaced;
340     } VectorList;
341
342     struct {
343       unsigned Val;
344     } VectorIndex;
345
346     struct {
347       const MCExpr *Val;
348     } Imm;
349
350     struct {
351       unsigned Val;       // encoded 8-bit representation
352     } FPImm;
353
354     /// Combined record for all forms of ARM address expressions.
355     struct {
356       unsigned BaseRegNum;
357       // Offset is in OffsetReg or OffsetImm. If both are zero, no offset
358       // was specified.
359       const MCConstantExpr *OffsetImm;  // Offset immediate value
360       unsigned OffsetRegNum;    // Offset register num, when OffsetImm == NULL
361       ARM_AM::ShiftOpc ShiftType; // Shift type for OffsetReg
362       unsigned ShiftImm;        // shift for OffsetReg.
363       unsigned Alignment;       // 0 = no alignment specified
364                                 // n = alignment in bytes (2, 4, 8, 16, or 32)
365       unsigned isNegative : 1;  // Negated OffsetReg? (~'U' bit)
366     } Memory;
367
368     struct {
369       unsigned RegNum;
370       bool isAdd;
371       ARM_AM::ShiftOpc ShiftTy;
372       unsigned ShiftImm;
373     } PostIdxReg;
374
375     struct {
376       bool isASR;
377       unsigned Imm;
378     } ShifterImm;
379     struct {
380       ARM_AM::ShiftOpc ShiftTy;
381       unsigned SrcReg;
382       unsigned ShiftReg;
383       unsigned ShiftImm;
384     } RegShiftedReg;
385     struct {
386       ARM_AM::ShiftOpc ShiftTy;
387       unsigned SrcReg;
388       unsigned ShiftImm;
389     } RegShiftedImm;
390     struct {
391       unsigned Imm;
392     } RotImm;
393     struct {
394       unsigned LSB;
395       unsigned Width;
396     } Bitfield;
397   };
398
399   ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
400 public:
401   ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
402     Kind = o.Kind;
403     StartLoc = o.StartLoc;
404     EndLoc = o.EndLoc;
405     switch (Kind) {
406     case k_CondCode:
407       CC = o.CC;
408       break;
409     case k_ITCondMask:
410       ITMask = o.ITMask;
411       break;
412     case k_Token:
413       Tok = o.Tok;
414       break;
415     case k_CCOut:
416     case k_Register:
417       Reg = o.Reg;
418       break;
419     case k_RegisterList:
420     case k_DPRRegisterList:
421     case k_SPRRegisterList:
422       Registers = o.Registers;
423       break;
424     case k_VectorList:
425     case k_VectorListAllLanes:
426     case k_VectorListIndexed:
427       VectorList = o.VectorList;
428       break;
429     case k_CoprocNum:
430     case k_CoprocReg:
431       Cop = o.Cop;
432       break;
433     case k_CoprocOption:
434       CoprocOption = o.CoprocOption;
435       break;
436     case k_Immediate:
437       Imm = o.Imm;
438       break;
439     case k_FPImmediate:
440       FPImm = o.FPImm;
441       break;
442     case k_MemBarrierOpt:
443       MBOpt = o.MBOpt;
444       break;
445     case k_Memory:
446       Memory = o.Memory;
447       break;
448     case k_PostIndexRegister:
449       PostIdxReg = o.PostIdxReg;
450       break;
451     case k_MSRMask:
452       MMask = o.MMask;
453       break;
454     case k_ProcIFlags:
455       IFlags = o.IFlags;
456       break;
457     case k_ShifterImmediate:
458       ShifterImm = o.ShifterImm;
459       break;
460     case k_ShiftedRegister:
461       RegShiftedReg = o.RegShiftedReg;
462       break;
463     case k_ShiftedImmediate:
464       RegShiftedImm = o.RegShiftedImm;
465       break;
466     case k_RotateImmediate:
467       RotImm = o.RotImm;
468       break;
469     case k_BitfieldDescriptor:
470       Bitfield = o.Bitfield;
471       break;
472     case k_VectorIndex:
473       VectorIndex = o.VectorIndex;
474       break;
475     }
476   }
477
478   /// getStartLoc - Get the location of the first token of this operand.
479   SMLoc getStartLoc() const { return StartLoc; }
480   /// getEndLoc - Get the location of the last token of this operand.
481   SMLoc getEndLoc() const { return EndLoc; }
482
483   ARMCC::CondCodes getCondCode() const {
484     assert(Kind == k_CondCode && "Invalid access!");
485     return CC.Val;
486   }
487
488   unsigned getCoproc() const {
489     assert((Kind == k_CoprocNum || Kind == k_CoprocReg) && "Invalid access!");
490     return Cop.Val;
491   }
492
493   StringRef getToken() const {
494     assert(Kind == k_Token && "Invalid access!");
495     return StringRef(Tok.Data, Tok.Length);
496   }
497
498   unsigned getReg() const {
499     assert((Kind == k_Register || Kind == k_CCOut) && "Invalid access!");
500     return Reg.RegNum;
501   }
502
503   const SmallVectorImpl<unsigned> &getRegList() const {
504     assert((Kind == k_RegisterList || Kind == k_DPRRegisterList ||
505             Kind == k_SPRRegisterList) && "Invalid access!");
506     return Registers;
507   }
508
509   const MCExpr *getImm() const {
510     assert(Kind == k_Immediate && "Invalid access!");
511     return Imm.Val;
512   }
513
514   unsigned getFPImm() const {
515     assert(Kind == k_FPImmediate && "Invalid access!");
516     return FPImm.Val;
517   }
518
519   unsigned getVectorIndex() const {
520     assert(Kind == k_VectorIndex && "Invalid access!");
521     return VectorIndex.Val;
522   }
523
524   ARM_MB::MemBOpt getMemBarrierOpt() const {
525     assert(Kind == k_MemBarrierOpt && "Invalid access!");
526     return MBOpt.Val;
527   }
528
529   ARM_PROC::IFlags getProcIFlags() const {
530     assert(Kind == k_ProcIFlags && "Invalid access!");
531     return IFlags.Val;
532   }
533
534   unsigned getMSRMask() const {
535     assert(Kind == k_MSRMask && "Invalid access!");
536     return MMask.Val;
537   }
538
539   bool isCoprocNum() const { return Kind == k_CoprocNum; }
540   bool isCoprocReg() const { return Kind == k_CoprocReg; }
541   bool isCoprocOption() const { return Kind == k_CoprocOption; }
542   bool isCondCode() const { return Kind == k_CondCode; }
543   bool isCCOut() const { return Kind == k_CCOut; }
544   bool isITMask() const { return Kind == k_ITCondMask; }
545   bool isITCondCode() const { return Kind == k_CondCode; }
546   bool isImm() const { return Kind == k_Immediate; }
547   bool isFPImm() const { return Kind == k_FPImmediate; }
548   bool isImm8s4() const {
549     if (Kind != k_Immediate)
550       return false;
551     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
552     if (!CE) return false;
553     int64_t Value = CE->getValue();
554     return ((Value & 3) == 0) && Value >= -1020 && Value <= 1020;
555   }
556   bool isImm0_1020s4() const {
557     if (Kind != k_Immediate)
558       return false;
559     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
560     if (!CE) return false;
561     int64_t Value = CE->getValue();
562     return ((Value & 3) == 0) && Value >= 0 && Value <= 1020;
563   }
564   bool isImm0_508s4() const {
565     if (Kind != k_Immediate)
566       return false;
567     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
568     if (!CE) return false;
569     int64_t Value = CE->getValue();
570     return ((Value & 3) == 0) && Value >= 0 && Value <= 508;
571   }
572   bool isImm0_255() const {
573     if (Kind != k_Immediate)
574       return false;
575     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
576     if (!CE) return false;
577     int64_t Value = CE->getValue();
578     return Value >= 0 && Value < 256;
579   }
580   bool isImm0_1() const {
581     if (Kind != k_Immediate)
582       return false;
583     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
584     if (!CE) return false;
585     int64_t Value = CE->getValue();
586     return Value >= 0 && Value < 2;
587   }
588   bool isImm0_3() const {
589     if (Kind != k_Immediate)
590       return false;
591     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
592     if (!CE) return false;
593     int64_t Value = CE->getValue();
594     return Value >= 0 && Value < 4;
595   }
596   bool isImm0_7() const {
597     if (Kind != k_Immediate)
598       return false;
599     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
600     if (!CE) return false;
601     int64_t Value = CE->getValue();
602     return Value >= 0 && Value < 8;
603   }
604   bool isImm0_15() const {
605     if (Kind != k_Immediate)
606       return false;
607     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
608     if (!CE) return false;
609     int64_t Value = CE->getValue();
610     return Value >= 0 && Value < 16;
611   }
612   bool isImm0_31() const {
613     if (Kind != k_Immediate)
614       return false;
615     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
616     if (!CE) return false;
617     int64_t Value = CE->getValue();
618     return Value >= 0 && Value < 32;
619   }
620   bool isImm0_63() const {
621     if (Kind != k_Immediate)
622       return false;
623     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
624     if (!CE) return false;
625     int64_t Value = CE->getValue();
626     return Value >= 0 && Value < 64;
627   }
628   bool isImm8() const {
629     if (Kind != k_Immediate)
630       return false;
631     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
632     if (!CE) return false;
633     int64_t Value = CE->getValue();
634     return Value == 8;
635   }
636   bool isImm16() const {
637     if (Kind != k_Immediate)
638       return false;
639     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
640     if (!CE) return false;
641     int64_t Value = CE->getValue();
642     return Value == 16;
643   }
644   bool isImm32() const {
645     if (Kind != k_Immediate)
646       return false;
647     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
648     if (!CE) return false;
649     int64_t Value = CE->getValue();
650     return Value == 32;
651   }
652   bool isShrImm8() const {
653     if (Kind != k_Immediate)
654       return false;
655     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
656     if (!CE) return false;
657     int64_t Value = CE->getValue();
658     return Value > 0 && Value <= 8;
659   }
660   bool isShrImm16() const {
661     if (Kind != k_Immediate)
662       return false;
663     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
664     if (!CE) return false;
665     int64_t Value = CE->getValue();
666     return Value > 0 && Value <= 16;
667   }
668   bool isShrImm32() const {
669     if (Kind != k_Immediate)
670       return false;
671     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
672     if (!CE) return false;
673     int64_t Value = CE->getValue();
674     return Value > 0 && Value <= 32;
675   }
676   bool isShrImm64() const {
677     if (Kind != k_Immediate)
678       return false;
679     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
680     if (!CE) return false;
681     int64_t Value = CE->getValue();
682     return Value > 0 && Value <= 64;
683   }
684   bool isImm1_7() const {
685     if (Kind != k_Immediate)
686       return false;
687     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
688     if (!CE) return false;
689     int64_t Value = CE->getValue();
690     return Value > 0 && Value < 8;
691   }
692   bool isImm1_15() const {
693     if (Kind != k_Immediate)
694       return false;
695     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
696     if (!CE) return false;
697     int64_t Value = CE->getValue();
698     return Value > 0 && Value < 16;
699   }
700   bool isImm1_31() const {
701     if (Kind != k_Immediate)
702       return false;
703     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
704     if (!CE) return false;
705     int64_t Value = CE->getValue();
706     return Value > 0 && Value < 32;
707   }
708   bool isImm1_16() const {
709     if (Kind != k_Immediate)
710       return false;
711     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
712     if (!CE) return false;
713     int64_t Value = CE->getValue();
714     return Value > 0 && Value < 17;
715   }
716   bool isImm1_32() const {
717     if (Kind != k_Immediate)
718       return false;
719     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
720     if (!CE) return false;
721     int64_t Value = CE->getValue();
722     return Value > 0 && Value < 33;
723   }
724   bool isImm0_32() const {
725     if (Kind != k_Immediate)
726       return false;
727     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
728     if (!CE) return false;
729     int64_t Value = CE->getValue();
730     return Value >= 0 && Value < 33;
731   }
732   bool isImm0_65535() const {
733     if (Kind != k_Immediate)
734       return false;
735     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
736     if (!CE) return false;
737     int64_t Value = CE->getValue();
738     return Value >= 0 && Value < 65536;
739   }
740   bool isImm0_65535Expr() const {
741     if (Kind != k_Immediate)
742       return false;
743     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
744     // If it's not a constant expression, it'll generate a fixup and be
745     // handled later.
746     if (!CE) return true;
747     int64_t Value = CE->getValue();
748     return Value >= 0 && Value < 65536;
749   }
750   bool isImm24bit() const {
751     if (Kind != k_Immediate)
752       return false;
753     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
754     if (!CE) return false;
755     int64_t Value = CE->getValue();
756     return Value >= 0 && Value <= 0xffffff;
757   }
758   bool isImmThumbSR() const {
759     if (Kind != k_Immediate)
760       return false;
761     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
762     if (!CE) return false;
763     int64_t Value = CE->getValue();
764     return Value > 0 && Value < 33;
765   }
766   bool isPKHLSLImm() const {
767     if (Kind != k_Immediate)
768       return false;
769     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
770     if (!CE) return false;
771     int64_t Value = CE->getValue();
772     return Value >= 0 && Value < 32;
773   }
774   bool isPKHASRImm() const {
775     if (Kind != k_Immediate)
776       return false;
777     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
778     if (!CE) return false;
779     int64_t Value = CE->getValue();
780     return Value > 0 && Value <= 32;
781   }
782   bool isARMSOImm() const {
783     if (Kind != k_Immediate)
784       return false;
785     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
786     if (!CE) return false;
787     int64_t Value = CE->getValue();
788     return ARM_AM::getSOImmVal(Value) != -1;
789   }
790   bool isARMSOImmNot() const {
791     if (Kind != k_Immediate)
792       return false;
793     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
794     if (!CE) return false;
795     int64_t Value = CE->getValue();
796     return ARM_AM::getSOImmVal(~Value) != -1;
797   }
798   bool isARMSOImmNeg() const {
799     if (Kind != k_Immediate)
800       return false;
801     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
802     if (!CE) return false;
803     int64_t Value = CE->getValue();
804     return ARM_AM::getSOImmVal(-Value) != -1;
805   }
806   bool isT2SOImm() const {
807     if (Kind != k_Immediate)
808       return false;
809     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
810     if (!CE) return false;
811     int64_t Value = CE->getValue();
812     return ARM_AM::getT2SOImmVal(Value) != -1;
813   }
814   bool isT2SOImmNot() const {
815     if (Kind != k_Immediate)
816       return false;
817     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
818     if (!CE) return false;
819     int64_t Value = CE->getValue();
820     return ARM_AM::getT2SOImmVal(~Value) != -1;
821   }
822   bool isT2SOImmNeg() const {
823     if (Kind != k_Immediate)
824       return false;
825     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
826     if (!CE) return false;
827     int64_t Value = CE->getValue();
828     return ARM_AM::getT2SOImmVal(-Value) != -1;
829   }
830   bool isSetEndImm() const {
831     if (Kind != k_Immediate)
832       return false;
833     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
834     if (!CE) return false;
835     int64_t Value = CE->getValue();
836     return Value == 1 || Value == 0;
837   }
838   bool isReg() const { return Kind == k_Register; }
839   bool isRegList() const { return Kind == k_RegisterList; }
840   bool isDPRRegList() const { return Kind == k_DPRRegisterList; }
841   bool isSPRRegList() const { return Kind == k_SPRRegisterList; }
842   bool isToken() const { return Kind == k_Token; }
843   bool isMemBarrierOpt() const { return Kind == k_MemBarrierOpt; }
844   bool isMemory() const { return Kind == k_Memory; }
845   bool isShifterImm() const { return Kind == k_ShifterImmediate; }
846   bool isRegShiftedReg() const { return Kind == k_ShiftedRegister; }
847   bool isRegShiftedImm() const { return Kind == k_ShiftedImmediate; }
848   bool isRotImm() const { return Kind == k_RotateImmediate; }
849   bool isBitfield() const { return Kind == k_BitfieldDescriptor; }
850   bool isPostIdxRegShifted() const { return Kind == k_PostIndexRegister; }
851   bool isPostIdxReg() const {
852     return Kind == k_PostIndexRegister && PostIdxReg.ShiftTy ==ARM_AM::no_shift;
853   }
854   bool isMemNoOffset(bool alignOK = false) const {
855     if (!isMemory())
856       return false;
857     // No offset of any kind.
858     return Memory.OffsetRegNum == 0 && Memory.OffsetImm == 0 &&
859      (alignOK || Memory.Alignment == 0);
860   }
861   bool isAlignedMemory() const {
862     return isMemNoOffset(true);
863   }
864   bool isAddrMode2() const {
865     if (!isMemory() || Memory.Alignment != 0) return false;
866     // Check for register offset.
867     if (Memory.OffsetRegNum) return true;
868     // Immediate offset in range [-4095, 4095].
869     if (!Memory.OffsetImm) return true;
870     int64_t Val = Memory.OffsetImm->getValue();
871     return Val > -4096 && Val < 4096;
872   }
873   bool isAM2OffsetImm() const {
874     if (Kind != k_Immediate)
875       return false;
876     // Immediate offset in range [-4095, 4095].
877     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
878     if (!CE) return false;
879     int64_t Val = CE->getValue();
880     return Val > -4096 && Val < 4096;
881   }
882   bool isAddrMode3() const {
883     if (!isMemory() || Memory.Alignment != 0) return false;
884     // No shifts are legal for AM3.
885     if (Memory.ShiftType != ARM_AM::no_shift) return false;
886     // Check for register offset.
887     if (Memory.OffsetRegNum) return true;
888     // Immediate offset in range [-255, 255].
889     if (!Memory.OffsetImm) return true;
890     int64_t Val = Memory.OffsetImm->getValue();
891     return Val > -256 && Val < 256;
892   }
893   bool isAM3Offset() const {
894     if (Kind != k_Immediate && Kind != k_PostIndexRegister)
895       return false;
896     if (Kind == k_PostIndexRegister)
897       return PostIdxReg.ShiftTy == ARM_AM::no_shift;
898     // Immediate offset in range [-255, 255].
899     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
900     if (!CE) return false;
901     int64_t Val = CE->getValue();
902     // Special case, #-0 is INT32_MIN.
903     return (Val > -256 && Val < 256) || Val == INT32_MIN;
904   }
905   bool isAddrMode5() const {
906     // If we have an immediate that's not a constant, treat it as a label
907     // reference needing a fixup. If it is a constant, it's something else
908     // and we reject it.
909     if (Kind == k_Immediate && !isa<MCConstantExpr>(getImm()))
910       return true;
911     if (!isMemory() || Memory.Alignment != 0) return false;
912     // Check for register offset.
913     if (Memory.OffsetRegNum) return false;
914     // Immediate offset in range [-1020, 1020] and a multiple of 4.
915     if (!Memory.OffsetImm) return true;
916     int64_t Val = Memory.OffsetImm->getValue();
917     return (Val >= -1020 && Val <= 1020 && ((Val & 3) == 0)) ||
918       Val == INT32_MIN;
919   }
920   bool isMemTBB() const {
921     if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
922         Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
923       return false;
924     return true;
925   }
926   bool isMemTBH() const {
927     if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
928         Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm != 1 ||
929         Memory.Alignment != 0 )
930       return false;
931     return true;
932   }
933   bool isMemRegOffset() const {
934     if (!isMemory() || !Memory.OffsetRegNum || Memory.Alignment != 0)
935       return false;
936     return true;
937   }
938   bool isT2MemRegOffset() const {
939     if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
940         Memory.Alignment != 0)
941       return false;
942     // Only lsl #{0, 1, 2, 3} allowed.
943     if (Memory.ShiftType == ARM_AM::no_shift)
944       return true;
945     if (Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm > 3)
946       return false;
947     return true;
948   }
949   bool isMemThumbRR() const {
950     // Thumb reg+reg addressing is simple. Just two registers, a base and
951     // an offset. No shifts, negations or any other complicating factors.
952     if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
953         Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
954       return false;
955     return isARMLowRegister(Memory.BaseRegNum) &&
956       (!Memory.OffsetRegNum || isARMLowRegister(Memory.OffsetRegNum));
957   }
958   bool isMemThumbRIs4() const {
959     if (!isMemory() || Memory.OffsetRegNum != 0 ||
960         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
961       return false;
962     // Immediate offset, multiple of 4 in range [0, 124].
963     if (!Memory.OffsetImm) return true;
964     int64_t Val = Memory.OffsetImm->getValue();
965     return Val >= 0 && Val <= 124 && (Val % 4) == 0;
966   }
967   bool isMemThumbRIs2() const {
968     if (!isMemory() || Memory.OffsetRegNum != 0 ||
969         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
970       return false;
971     // Immediate offset, multiple of 4 in range [0, 62].
972     if (!Memory.OffsetImm) return true;
973     int64_t Val = Memory.OffsetImm->getValue();
974     return Val >= 0 && Val <= 62 && (Val % 2) == 0;
975   }
976   bool isMemThumbRIs1() const {
977     if (!isMemory() || Memory.OffsetRegNum != 0 ||
978         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
979       return false;
980     // Immediate offset in range [0, 31].
981     if (!Memory.OffsetImm) return true;
982     int64_t Val = Memory.OffsetImm->getValue();
983     return Val >= 0 && Val <= 31;
984   }
985   bool isMemThumbSPI() const {
986     if (!isMemory() || Memory.OffsetRegNum != 0 ||
987         Memory.BaseRegNum != ARM::SP || Memory.Alignment != 0)
988       return false;
989     // Immediate offset, multiple of 4 in range [0, 1020].
990     if (!Memory.OffsetImm) return true;
991     int64_t Val = Memory.OffsetImm->getValue();
992     return Val >= 0 && Val <= 1020 && (Val % 4) == 0;
993   }
994   bool isMemImm8s4Offset() const {
995     if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
996       return false;
997     // Immediate offset a multiple of 4 in range [-1020, 1020].
998     if (!Memory.OffsetImm) return true;
999     int64_t Val = Memory.OffsetImm->getValue();
1000     return Val >= -1020 && Val <= 1020 && (Val & 3) == 0;
1001   }
1002   bool isMemImm0_1020s4Offset() const {
1003     if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1004       return false;
1005     // Immediate offset a multiple of 4 in range [0, 1020].
1006     if (!Memory.OffsetImm) return true;
1007     int64_t Val = Memory.OffsetImm->getValue();
1008     return Val >= 0 && Val <= 1020 && (Val & 3) == 0;
1009   }
1010   bool isMemImm8Offset() const {
1011     if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1012       return false;
1013     // Immediate offset in range [-255, 255].
1014     if (!Memory.OffsetImm) return true;
1015     int64_t Val = Memory.OffsetImm->getValue();
1016     return (Val == INT32_MIN) || (Val > -256 && Val < 256);
1017   }
1018   bool isMemPosImm8Offset() const {
1019     if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1020       return false;
1021     // Immediate offset in range [0, 255].
1022     if (!Memory.OffsetImm) return true;
1023     int64_t Val = Memory.OffsetImm->getValue();
1024     return Val >= 0 && Val < 256;
1025   }
1026   bool isMemNegImm8Offset() const {
1027     if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1028       return false;
1029     // Immediate offset in range [-255, -1].
1030     if (!Memory.OffsetImm) return false;
1031     int64_t Val = Memory.OffsetImm->getValue();
1032     return (Val == INT32_MIN) || (Val > -256 && Val < 0);
1033   }
1034   bool isMemUImm12Offset() const {
1035     if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1036       return false;
1037     // Immediate offset in range [0, 4095].
1038     if (!Memory.OffsetImm) return true;
1039     int64_t Val = Memory.OffsetImm->getValue();
1040     return (Val >= 0 && Val < 4096);
1041   }
1042   bool isMemImm12Offset() const {
1043     // If we have an immediate that's not a constant, treat it as a label
1044     // reference needing a fixup. If it is a constant, it's something else
1045     // and we reject it.
1046     if (Kind == k_Immediate && !isa<MCConstantExpr>(getImm()))
1047       return true;
1048
1049     if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
1050       return false;
1051     // Immediate offset in range [-4095, 4095].
1052     if (!Memory.OffsetImm) return true;
1053     int64_t Val = Memory.OffsetImm->getValue();
1054     return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
1055   }
1056   bool isPostIdxImm8() const {
1057     if (Kind != k_Immediate)
1058       return false;
1059     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1060     if (!CE) return false;
1061     int64_t Val = CE->getValue();
1062     return (Val > -256 && Val < 256) || (Val == INT32_MIN);
1063   }
1064   bool isPostIdxImm8s4() const {
1065     if (Kind != k_Immediate)
1066       return false;
1067     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1068     if (!CE) return false;
1069     int64_t Val = CE->getValue();
1070     return ((Val & 3) == 0 && Val >= -1020 && Val <= 1020) ||
1071       (Val == INT32_MIN);
1072   }
1073
1074   bool isMSRMask() const { return Kind == k_MSRMask; }
1075   bool isProcIFlags() const { return Kind == k_ProcIFlags; }
1076
1077   // NEON operands.
1078   bool isSingleSpacedVectorList() const {
1079     return Kind == k_VectorList && !VectorList.isDoubleSpaced;
1080   }
1081   bool isDoubleSpacedVectorList() const {
1082     return Kind == k_VectorList && VectorList.isDoubleSpaced;
1083   }
1084   bool isVecListOneD() const {
1085     if (!isSingleSpacedVectorList()) return false;
1086     return VectorList.Count == 1;
1087   }
1088
1089   bool isVecListTwoD() const {
1090     if (!isSingleSpacedVectorList()) return false;
1091     return VectorList.Count == 2;
1092   }
1093
1094   bool isVecListThreeD() const {
1095     if (!isSingleSpacedVectorList()) return false;
1096     return VectorList.Count == 3;
1097   }
1098
1099   bool isVecListFourD() const {
1100     if (!isSingleSpacedVectorList()) return false;
1101     return VectorList.Count == 4;
1102   }
1103
1104   bool isVecListTwoQ() const {
1105     if (!isDoubleSpacedVectorList()) return false;
1106     return VectorList.Count == 2;
1107   }
1108
1109   bool isVecListOneDAllLanes() const {
1110     if (Kind != k_VectorListAllLanes) return false;
1111     return VectorList.Count == 1;
1112   }
1113
1114   bool isVecListTwoDAllLanes() const {
1115     if (Kind != k_VectorListAllLanes) return false;
1116     return VectorList.Count == 2;
1117   }
1118
1119   bool isVecListOneDByteIndexed() const {
1120     if (Kind != k_VectorListIndexed) return false;
1121     return VectorList.Count == 1 && VectorList.LaneIndex <= 7;
1122   }
1123
1124   bool isVecListOneDHWordIndexed() const {
1125     if (Kind != k_VectorListIndexed) return false;
1126     return VectorList.Count == 1 && VectorList.LaneIndex <= 3;
1127   }
1128
1129   bool isVecListOneDWordIndexed() const {
1130     if (Kind != k_VectorListIndexed) return false;
1131     return VectorList.Count == 1 && VectorList.LaneIndex <= 1;
1132   }
1133
1134   bool isVecListTwoDByteIndexed() const {
1135     if (Kind != k_VectorListIndexed) return false;
1136     return VectorList.Count == 2 && VectorList.LaneIndex <= 7;
1137   }
1138
1139   bool isVecListTwoDHWordIndexed() const {
1140     if (Kind != k_VectorListIndexed) return false;
1141     return VectorList.Count == 2 && VectorList.LaneIndex <= 3;
1142   }
1143
1144   bool isVecListTwoDWordIndexed() const {
1145     if (Kind != k_VectorListIndexed) return false;
1146     return VectorList.Count == 2 && VectorList.LaneIndex <= 1;
1147   }
1148
1149   bool isVectorIndex8() const {
1150     if (Kind != k_VectorIndex) return false;
1151     return VectorIndex.Val < 8;
1152   }
1153   bool isVectorIndex16() const {
1154     if (Kind != k_VectorIndex) return false;
1155     return VectorIndex.Val < 4;
1156   }
1157   bool isVectorIndex32() const {
1158     if (Kind != k_VectorIndex) return false;
1159     return VectorIndex.Val < 2;
1160   }
1161
1162   bool isNEONi8splat() const {
1163     if (Kind != k_Immediate)
1164       return false;
1165     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1166     // Must be a constant.
1167     if (!CE) return false;
1168     int64_t Value = CE->getValue();
1169     // i8 value splatted across 8 bytes. The immediate is just the 8 byte
1170     // value.
1171     return Value >= 0 && Value < 256;
1172   }
1173
1174   bool isNEONi16splat() const {
1175     if (Kind != k_Immediate)
1176       return false;
1177     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1178     // Must be a constant.
1179     if (!CE) return false;
1180     int64_t Value = CE->getValue();
1181     // i16 value in the range [0,255] or [0x0100, 0xff00]
1182     return (Value >= 0 && Value < 256) || (Value >= 0x0100 && Value <= 0xff00);
1183   }
1184
1185   bool isNEONi32splat() const {
1186     if (Kind != k_Immediate)
1187       return false;
1188     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1189     // Must be a constant.
1190     if (!CE) return false;
1191     int64_t Value = CE->getValue();
1192     // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X.
1193     return (Value >= 0 && Value < 256) ||
1194       (Value >= 0x0100 && Value <= 0xff00) ||
1195       (Value >= 0x010000 && Value <= 0xff0000) ||
1196       (Value >= 0x01000000 && Value <= 0xff000000);
1197   }
1198
1199   bool isNEONi32vmov() const {
1200     if (Kind != k_Immediate)
1201       return false;
1202     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1203     // Must be a constant.
1204     if (!CE) return false;
1205     int64_t Value = CE->getValue();
1206     // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
1207     // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
1208     return (Value >= 0 && Value < 256) ||
1209       (Value >= 0x0100 && Value <= 0xff00) ||
1210       (Value >= 0x010000 && Value <= 0xff0000) ||
1211       (Value >= 0x01000000 && Value <= 0xff000000) ||
1212       (Value >= 0x01ff && Value <= 0xffff && (Value & 0xff) == 0xff) ||
1213       (Value >= 0x01ffff && Value <= 0xffffff && (Value & 0xffff) == 0xffff);
1214   }
1215
1216   bool isNEONi64splat() const {
1217     if (Kind != k_Immediate)
1218       return false;
1219     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1220     // Must be a constant.
1221     if (!CE) return false;
1222     uint64_t Value = CE->getValue();
1223     // i64 value with each byte being either 0 or 0xff.
1224     for (unsigned i = 0; i < 8; ++i)
1225       if ((Value & 0xff) != 0 && (Value & 0xff) != 0xff) return false;
1226     return true;
1227   }
1228
1229   void addExpr(MCInst &Inst, const MCExpr *Expr) const {
1230     // Add as immediates when possible.  Null MCExpr = 0.
1231     if (Expr == 0)
1232       Inst.addOperand(MCOperand::CreateImm(0));
1233     else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
1234       Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1235     else
1236       Inst.addOperand(MCOperand::CreateExpr(Expr));
1237   }
1238
1239   void addCondCodeOperands(MCInst &Inst, unsigned N) const {
1240     assert(N == 2 && "Invalid number of operands!");
1241     Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
1242     unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
1243     Inst.addOperand(MCOperand::CreateReg(RegNum));
1244   }
1245
1246   void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
1247     assert(N == 1 && "Invalid number of operands!");
1248     Inst.addOperand(MCOperand::CreateImm(getCoproc()));
1249   }
1250
1251   void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
1252     assert(N == 1 && "Invalid number of operands!");
1253     Inst.addOperand(MCOperand::CreateImm(getCoproc()));
1254   }
1255
1256   void addCoprocOptionOperands(MCInst &Inst, unsigned N) const {
1257     assert(N == 1 && "Invalid number of operands!");
1258     Inst.addOperand(MCOperand::CreateImm(CoprocOption.Val));
1259   }
1260
1261   void addITMaskOperands(MCInst &Inst, unsigned N) const {
1262     assert(N == 1 && "Invalid number of operands!");
1263     Inst.addOperand(MCOperand::CreateImm(ITMask.Mask));
1264   }
1265
1266   void addITCondCodeOperands(MCInst &Inst, unsigned N) const {
1267     assert(N == 1 && "Invalid number of operands!");
1268     Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
1269   }
1270
1271   void addCCOutOperands(MCInst &Inst, unsigned N) const {
1272     assert(N == 1 && "Invalid number of operands!");
1273     Inst.addOperand(MCOperand::CreateReg(getReg()));
1274   }
1275
1276   void addRegOperands(MCInst &Inst, unsigned N) const {
1277     assert(N == 1 && "Invalid number of operands!");
1278     Inst.addOperand(MCOperand::CreateReg(getReg()));
1279   }
1280
1281   void addRegShiftedRegOperands(MCInst &Inst, unsigned N) const {
1282     assert(N == 3 && "Invalid number of operands!");
1283     assert(isRegShiftedReg() &&
1284            "addRegShiftedRegOperands() on non RegShiftedReg!");
1285     Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.SrcReg));
1286     Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.ShiftReg));
1287     Inst.addOperand(MCOperand::CreateImm(
1288       ARM_AM::getSORegOpc(RegShiftedReg.ShiftTy, RegShiftedReg.ShiftImm)));
1289   }
1290
1291   void addRegShiftedImmOperands(MCInst &Inst, unsigned N) const {
1292     assert(N == 2 && "Invalid number of operands!");
1293     assert(isRegShiftedImm() &&
1294            "addRegShiftedImmOperands() on non RegShiftedImm!");
1295     Inst.addOperand(MCOperand::CreateReg(RegShiftedImm.SrcReg));
1296     Inst.addOperand(MCOperand::CreateImm(
1297       ARM_AM::getSORegOpc(RegShiftedImm.ShiftTy, RegShiftedImm.ShiftImm)));
1298   }
1299
1300   void addShifterImmOperands(MCInst &Inst, unsigned N) const {
1301     assert(N == 1 && "Invalid number of operands!");
1302     Inst.addOperand(MCOperand::CreateImm((ShifterImm.isASR << 5) |
1303                                          ShifterImm.Imm));
1304   }
1305
1306   void addRegListOperands(MCInst &Inst, unsigned N) const {
1307     assert(N == 1 && "Invalid number of operands!");
1308     const SmallVectorImpl<unsigned> &RegList = getRegList();
1309     for (SmallVectorImpl<unsigned>::const_iterator
1310            I = RegList.begin(), E = RegList.end(); I != E; ++I)
1311       Inst.addOperand(MCOperand::CreateReg(*I));
1312   }
1313
1314   void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
1315     addRegListOperands(Inst, N);
1316   }
1317
1318   void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
1319     addRegListOperands(Inst, N);
1320   }
1321
1322   void addRotImmOperands(MCInst &Inst, unsigned N) const {
1323     assert(N == 1 && "Invalid number of operands!");
1324     // Encoded as val>>3. The printer handles display as 8, 16, 24.
1325     Inst.addOperand(MCOperand::CreateImm(RotImm.Imm >> 3));
1326   }
1327
1328   void addBitfieldOperands(MCInst &Inst, unsigned N) const {
1329     assert(N == 1 && "Invalid number of operands!");
1330     // Munge the lsb/width into a bitfield mask.
1331     unsigned lsb = Bitfield.LSB;
1332     unsigned width = Bitfield.Width;
1333     // Make a 32-bit mask w/ the referenced bits clear and all other bits set.
1334     uint32_t Mask = ~(((uint32_t)0xffffffff >> lsb) << (32 - width) >>
1335                       (32 - (lsb + width)));
1336     Inst.addOperand(MCOperand::CreateImm(Mask));
1337   }
1338
1339   void addImmOperands(MCInst &Inst, unsigned N) const {
1340     assert(N == 1 && "Invalid number of operands!");
1341     addExpr(Inst, getImm());
1342   }
1343
1344   void addFPImmOperands(MCInst &Inst, unsigned N) const {
1345     assert(N == 1 && "Invalid number of operands!");
1346     Inst.addOperand(MCOperand::CreateImm(getFPImm()));
1347   }
1348
1349   void addImm8s4Operands(MCInst &Inst, unsigned N) const {
1350     assert(N == 1 && "Invalid number of operands!");
1351     // FIXME: We really want to scale the value here, but the LDRD/STRD
1352     // instruction don't encode operands that way yet.
1353     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1354     Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1355   }
1356
1357   void addImm0_1020s4Operands(MCInst &Inst, unsigned N) const {
1358     assert(N == 1 && "Invalid number of operands!");
1359     // The immediate is scaled by four in the encoding and is stored
1360     // in the MCInst as such. Lop off the low two bits here.
1361     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1362     Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1363   }
1364
1365   void addImm0_508s4Operands(MCInst &Inst, unsigned N) const {
1366     assert(N == 1 && "Invalid number of operands!");
1367     // The immediate is scaled by four in the encoding and is stored
1368     // in the MCInst as such. Lop off the low two bits here.
1369     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1370     Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1371   }
1372
1373   void addImm1_16Operands(MCInst &Inst, unsigned N) const {
1374     assert(N == 1 && "Invalid number of operands!");
1375     // The constant encodes as the immediate-1, and we store in the instruction
1376     // the bits as encoded, so subtract off one here.
1377     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1378     Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1379   }
1380
1381   void addImm1_32Operands(MCInst &Inst, unsigned N) const {
1382     assert(N == 1 && "Invalid number of operands!");
1383     // The constant encodes as the immediate-1, and we store in the instruction
1384     // the bits as encoded, so subtract off one here.
1385     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1386     Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1387   }
1388
1389   void addImmThumbSROperands(MCInst &Inst, unsigned N) const {
1390     assert(N == 1 && "Invalid number of operands!");
1391     // The constant encodes as the immediate, except for 32, which encodes as
1392     // zero.
1393     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1394     unsigned Imm = CE->getValue();
1395     Inst.addOperand(MCOperand::CreateImm((Imm == 32 ? 0 : Imm)));
1396   }
1397
1398   void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
1399     assert(N == 1 && "Invalid number of operands!");
1400     // An ASR value of 32 encodes as 0, so that's how we want to add it to
1401     // the instruction as well.
1402     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1403     int Val = CE->getValue();
1404     Inst.addOperand(MCOperand::CreateImm(Val == 32 ? 0 : Val));
1405   }
1406
1407   void addT2SOImmNotOperands(MCInst &Inst, unsigned N) const {
1408     assert(N == 1 && "Invalid number of operands!");
1409     // The operand is actually a t2_so_imm, but we have its bitwise
1410     // negation in the assembly source, so twiddle it here.
1411     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1412     Inst.addOperand(MCOperand::CreateImm(~CE->getValue()));
1413   }
1414
1415   void addT2SOImmNegOperands(MCInst &Inst, unsigned N) const {
1416     assert(N == 1 && "Invalid number of operands!");
1417     // The operand is actually a t2_so_imm, but we have its
1418     // negation in the assembly source, so twiddle it here.
1419     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1420     Inst.addOperand(MCOperand::CreateImm(-CE->getValue()));
1421   }
1422
1423   void addARMSOImmNotOperands(MCInst &Inst, unsigned N) const {
1424     assert(N == 1 && "Invalid number of operands!");
1425     // The operand is actually a so_imm, but we have its bitwise
1426     // negation in the assembly source, so twiddle it here.
1427     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1428     Inst.addOperand(MCOperand::CreateImm(~CE->getValue()));
1429   }
1430
1431   void addARMSOImmNegOperands(MCInst &Inst, unsigned N) const {
1432     assert(N == 1 && "Invalid number of operands!");
1433     // The operand is actually a so_imm, but we have its
1434     // negation in the assembly source, so twiddle it here.
1435     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1436     Inst.addOperand(MCOperand::CreateImm(-CE->getValue()));
1437   }
1438
1439   void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
1440     assert(N == 1 && "Invalid number of operands!");
1441     Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
1442   }
1443
1444   void addMemNoOffsetOperands(MCInst &Inst, unsigned N) const {
1445     assert(N == 1 && "Invalid number of operands!");
1446     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1447   }
1448
1449   void addAlignedMemoryOperands(MCInst &Inst, unsigned N) const {
1450     assert(N == 2 && "Invalid number of operands!");
1451     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1452     Inst.addOperand(MCOperand::CreateImm(Memory.Alignment));
1453   }
1454
1455   void addAddrMode2Operands(MCInst &Inst, unsigned N) const {
1456     assert(N == 3 && "Invalid number of operands!");
1457     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1458     if (!Memory.OffsetRegNum) {
1459       ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1460       // Special case for #-0
1461       if (Val == INT32_MIN) Val = 0;
1462       if (Val < 0) Val = -Val;
1463       Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1464     } else {
1465       // For register offset, we encode the shift type and negation flag
1466       // here.
1467       Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1468                               Memory.ShiftImm, Memory.ShiftType);
1469     }
1470     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1471     Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1472     Inst.addOperand(MCOperand::CreateImm(Val));
1473   }
1474
1475   void addAM2OffsetImmOperands(MCInst &Inst, unsigned N) const {
1476     assert(N == 2 && "Invalid number of operands!");
1477     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1478     assert(CE && "non-constant AM2OffsetImm operand!");
1479     int32_t Val = CE->getValue();
1480     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1481     // Special case for #-0
1482     if (Val == INT32_MIN) Val = 0;
1483     if (Val < 0) Val = -Val;
1484     Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1485     Inst.addOperand(MCOperand::CreateReg(0));
1486     Inst.addOperand(MCOperand::CreateImm(Val));
1487   }
1488
1489   void addAddrMode3Operands(MCInst &Inst, unsigned N) const {
1490     assert(N == 3 && "Invalid number of operands!");
1491     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1492     if (!Memory.OffsetRegNum) {
1493       ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1494       // Special case for #-0
1495       if (Val == INT32_MIN) Val = 0;
1496       if (Val < 0) Val = -Val;
1497       Val = ARM_AM::getAM3Opc(AddSub, Val);
1498     } else {
1499       // For register offset, we encode the shift type and negation flag
1500       // here.
1501       Val = ARM_AM::getAM3Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add, 0);
1502     }
1503     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1504     Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1505     Inst.addOperand(MCOperand::CreateImm(Val));
1506   }
1507
1508   void addAM3OffsetOperands(MCInst &Inst, unsigned N) const {
1509     assert(N == 2 && "Invalid number of operands!");
1510     if (Kind == k_PostIndexRegister) {
1511       int32_t Val =
1512         ARM_AM::getAM3Opc(PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub, 0);
1513       Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1514       Inst.addOperand(MCOperand::CreateImm(Val));
1515       return;
1516     }
1517
1518     // Constant offset.
1519     const MCConstantExpr *CE = static_cast<const MCConstantExpr*>(getImm());
1520     int32_t Val = CE->getValue();
1521     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1522     // Special case for #-0
1523     if (Val == INT32_MIN) Val = 0;
1524     if (Val < 0) Val = -Val;
1525     Val = ARM_AM::getAM3Opc(AddSub, Val);
1526     Inst.addOperand(MCOperand::CreateReg(0));
1527     Inst.addOperand(MCOperand::CreateImm(Val));
1528   }
1529
1530   void addAddrMode5Operands(MCInst &Inst, unsigned N) const {
1531     assert(N == 2 && "Invalid number of operands!");
1532     // If we have an immediate that's not a constant, treat it as a label
1533     // reference needing a fixup. If it is a constant, it's something else
1534     // and we reject it.
1535     if (isImm()) {
1536       Inst.addOperand(MCOperand::CreateExpr(getImm()));
1537       Inst.addOperand(MCOperand::CreateImm(0));
1538       return;
1539     }
1540
1541     // The lower two bits are always zero and as such are not encoded.
1542     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1543     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1544     // Special case for #-0
1545     if (Val == INT32_MIN) Val = 0;
1546     if (Val < 0) Val = -Val;
1547     Val = ARM_AM::getAM5Opc(AddSub, Val);
1548     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1549     Inst.addOperand(MCOperand::CreateImm(Val));
1550   }
1551
1552   void addMemImm8s4OffsetOperands(MCInst &Inst, unsigned N) const {
1553     assert(N == 2 && "Invalid number of operands!");
1554     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1555     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1556     Inst.addOperand(MCOperand::CreateImm(Val));
1557   }
1558
1559   void addMemImm0_1020s4OffsetOperands(MCInst &Inst, unsigned N) const {
1560     assert(N == 2 && "Invalid number of operands!");
1561     // The lower two bits are always zero and as such are not encoded.
1562     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1563     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1564     Inst.addOperand(MCOperand::CreateImm(Val));
1565   }
1566
1567   void addMemImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1568     assert(N == 2 && "Invalid number of operands!");
1569     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1570     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1571     Inst.addOperand(MCOperand::CreateImm(Val));
1572   }
1573
1574   void addMemPosImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1575     addMemImm8OffsetOperands(Inst, N);
1576   }
1577
1578   void addMemNegImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1579     addMemImm8OffsetOperands(Inst, N);
1580   }
1581
1582   void addMemUImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1583     assert(N == 2 && "Invalid number of operands!");
1584     // If this is an immediate, it's a label reference.
1585     if (Kind == k_Immediate) {
1586       addExpr(Inst, getImm());
1587       Inst.addOperand(MCOperand::CreateImm(0));
1588       return;
1589     }
1590
1591     // Otherwise, it's a normal memory reg+offset.
1592     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1593     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1594     Inst.addOperand(MCOperand::CreateImm(Val));
1595   }
1596
1597   void addMemImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1598     assert(N == 2 && "Invalid number of operands!");
1599     // If this is an immediate, it's a label reference.
1600     if (Kind == k_Immediate) {
1601       addExpr(Inst, getImm());
1602       Inst.addOperand(MCOperand::CreateImm(0));
1603       return;
1604     }
1605
1606     // Otherwise, it's a normal memory reg+offset.
1607     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1608     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1609     Inst.addOperand(MCOperand::CreateImm(Val));
1610   }
1611
1612   void addMemTBBOperands(MCInst &Inst, unsigned N) const {
1613     assert(N == 2 && "Invalid number of operands!");
1614     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1615     Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1616   }
1617
1618   void addMemTBHOperands(MCInst &Inst, unsigned N) const {
1619     assert(N == 2 && "Invalid number of operands!");
1620     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1621     Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1622   }
1623
1624   void addMemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1625     assert(N == 3 && "Invalid number of operands!");
1626     unsigned Val =
1627       ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1628                         Memory.ShiftImm, Memory.ShiftType);
1629     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1630     Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1631     Inst.addOperand(MCOperand::CreateImm(Val));
1632   }
1633
1634   void addT2MemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1635     assert(N == 3 && "Invalid number of operands!");
1636     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1637     Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1638     Inst.addOperand(MCOperand::CreateImm(Memory.ShiftImm));
1639   }
1640
1641   void addMemThumbRROperands(MCInst &Inst, unsigned N) const {
1642     assert(N == 2 && "Invalid number of operands!");
1643     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1644     Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1645   }
1646
1647   void addMemThumbRIs4Operands(MCInst &Inst, unsigned N) const {
1648     assert(N == 2 && "Invalid number of operands!");
1649     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1650     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1651     Inst.addOperand(MCOperand::CreateImm(Val));
1652   }
1653
1654   void addMemThumbRIs2Operands(MCInst &Inst, unsigned N) const {
1655     assert(N == 2 && "Invalid number of operands!");
1656     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 2) : 0;
1657     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1658     Inst.addOperand(MCOperand::CreateImm(Val));
1659   }
1660
1661   void addMemThumbRIs1Operands(MCInst &Inst, unsigned N) const {
1662     assert(N == 2 && "Invalid number of operands!");
1663     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue()) : 0;
1664     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1665     Inst.addOperand(MCOperand::CreateImm(Val));
1666   }
1667
1668   void addMemThumbSPIOperands(MCInst &Inst, unsigned N) const {
1669     assert(N == 2 && "Invalid number of operands!");
1670     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1671     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1672     Inst.addOperand(MCOperand::CreateImm(Val));
1673   }
1674
1675   void addPostIdxImm8Operands(MCInst &Inst, unsigned N) const {
1676     assert(N == 1 && "Invalid number of operands!");
1677     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1678     assert(CE && "non-constant post-idx-imm8 operand!");
1679     int Imm = CE->getValue();
1680     bool isAdd = Imm >= 0;
1681     if (Imm == INT32_MIN) Imm = 0;
1682     Imm = (Imm < 0 ? -Imm : Imm) | (int)isAdd << 8;
1683     Inst.addOperand(MCOperand::CreateImm(Imm));
1684   }
1685
1686   void addPostIdxImm8s4Operands(MCInst &Inst, unsigned N) const {
1687     assert(N == 1 && "Invalid number of operands!");
1688     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1689     assert(CE && "non-constant post-idx-imm8s4 operand!");
1690     int Imm = CE->getValue();
1691     bool isAdd = Imm >= 0;
1692     if (Imm == INT32_MIN) Imm = 0;
1693     // Immediate is scaled by 4.
1694     Imm = ((Imm < 0 ? -Imm : Imm) / 4) | (int)isAdd << 8;
1695     Inst.addOperand(MCOperand::CreateImm(Imm));
1696   }
1697
1698   void addPostIdxRegOperands(MCInst &Inst, unsigned N) const {
1699     assert(N == 2 && "Invalid number of operands!");
1700     Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1701     Inst.addOperand(MCOperand::CreateImm(PostIdxReg.isAdd));
1702   }
1703
1704   void addPostIdxRegShiftedOperands(MCInst &Inst, unsigned N) const {
1705     assert(N == 2 && "Invalid number of operands!");
1706     Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1707     // The sign, shift type, and shift amount are encoded in a single operand
1708     // using the AM2 encoding helpers.
1709     ARM_AM::AddrOpc opc = PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub;
1710     unsigned Imm = ARM_AM::getAM2Opc(opc, PostIdxReg.ShiftImm,
1711                                      PostIdxReg.ShiftTy);
1712     Inst.addOperand(MCOperand::CreateImm(Imm));
1713   }
1714
1715   void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
1716     assert(N == 1 && "Invalid number of operands!");
1717     Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
1718   }
1719
1720   void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
1721     assert(N == 1 && "Invalid number of operands!");
1722     Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
1723   }
1724
1725   void addVecListOperands(MCInst &Inst, unsigned N) const {
1726     assert(N == 1 && "Invalid number of operands!");
1727     Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1728   }
1729
1730   void addVecListIndexedOperands(MCInst &Inst, unsigned N) const {
1731     assert(N == 2 && "Invalid number of operands!");
1732     Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1733     Inst.addOperand(MCOperand::CreateImm(VectorList.LaneIndex));
1734   }
1735
1736   void addVectorIndex8Operands(MCInst &Inst, unsigned N) const {
1737     assert(N == 1 && "Invalid number of operands!");
1738     Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1739   }
1740
1741   void addVectorIndex16Operands(MCInst &Inst, unsigned N) const {
1742     assert(N == 1 && "Invalid number of operands!");
1743     Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1744   }
1745
1746   void addVectorIndex32Operands(MCInst &Inst, unsigned N) const {
1747     assert(N == 1 && "Invalid number of operands!");
1748     Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1749   }
1750
1751   void addNEONi8splatOperands(MCInst &Inst, unsigned N) const {
1752     assert(N == 1 && "Invalid number of operands!");
1753     // The immediate encodes the type of constant as well as the value.
1754     // Mask in that this is an i8 splat.
1755     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1756     Inst.addOperand(MCOperand::CreateImm(CE->getValue() | 0xe00));
1757   }
1758
1759   void addNEONi16splatOperands(MCInst &Inst, unsigned N) const {
1760     assert(N == 1 && "Invalid number of operands!");
1761     // The immediate encodes the type of constant as well as the value.
1762     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1763     unsigned Value = CE->getValue();
1764     if (Value >= 256)
1765       Value = (Value >> 8) | 0xa00;
1766     else
1767       Value |= 0x800;
1768     Inst.addOperand(MCOperand::CreateImm(Value));
1769   }
1770
1771   void addNEONi32splatOperands(MCInst &Inst, unsigned N) const {
1772     assert(N == 1 && "Invalid number of operands!");
1773     // The immediate encodes the type of constant as well as the value.
1774     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1775     unsigned Value = CE->getValue();
1776     if (Value >= 256 && Value <= 0xff00)
1777       Value = (Value >> 8) | 0x200;
1778     else if (Value > 0xffff && Value <= 0xff0000)
1779       Value = (Value >> 16) | 0x400;
1780     else if (Value > 0xffffff)
1781       Value = (Value >> 24) | 0x600;
1782     Inst.addOperand(MCOperand::CreateImm(Value));
1783   }
1784
1785   void addNEONi32vmovOperands(MCInst &Inst, unsigned N) const {
1786     assert(N == 1 && "Invalid number of operands!");
1787     // The immediate encodes the type of constant as well as the value.
1788     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1789     unsigned Value = CE->getValue();
1790     if (Value >= 256 && Value <= 0xffff)
1791       Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
1792     else if (Value > 0xffff && Value <= 0xffffff)
1793       Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
1794     else if (Value > 0xffffff)
1795       Value = (Value >> 24) | 0x600;
1796     Inst.addOperand(MCOperand::CreateImm(Value));
1797   }
1798
1799   void addNEONi64splatOperands(MCInst &Inst, unsigned N) const {
1800     assert(N == 1 && "Invalid number of operands!");
1801     // The immediate encodes the type of constant as well as the value.
1802     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1803     uint64_t Value = CE->getValue();
1804     unsigned Imm = 0;
1805     for (unsigned i = 0; i < 8; ++i, Value >>= 8) {
1806       Imm |= (Value & 1) << i;
1807     }
1808     Inst.addOperand(MCOperand::CreateImm(Imm | 0x1e00));
1809   }
1810
1811   virtual void print(raw_ostream &OS) const;
1812
1813   static ARMOperand *CreateITMask(unsigned Mask, SMLoc S) {
1814     ARMOperand *Op = new ARMOperand(k_ITCondMask);
1815     Op->ITMask.Mask = Mask;
1816     Op->StartLoc = S;
1817     Op->EndLoc = S;
1818     return Op;
1819   }
1820
1821   static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
1822     ARMOperand *Op = new ARMOperand(k_CondCode);
1823     Op->CC.Val = CC;
1824     Op->StartLoc = S;
1825     Op->EndLoc = S;
1826     return Op;
1827   }
1828
1829   static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
1830     ARMOperand *Op = new ARMOperand(k_CoprocNum);
1831     Op->Cop.Val = CopVal;
1832     Op->StartLoc = S;
1833     Op->EndLoc = S;
1834     return Op;
1835   }
1836
1837   static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
1838     ARMOperand *Op = new ARMOperand(k_CoprocReg);
1839     Op->Cop.Val = CopVal;
1840     Op->StartLoc = S;
1841     Op->EndLoc = S;
1842     return Op;
1843   }
1844
1845   static ARMOperand *CreateCoprocOption(unsigned Val, SMLoc S, SMLoc E) {
1846     ARMOperand *Op = new ARMOperand(k_CoprocOption);
1847     Op->Cop.Val = Val;
1848     Op->StartLoc = S;
1849     Op->EndLoc = E;
1850     return Op;
1851   }
1852
1853   static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
1854     ARMOperand *Op = new ARMOperand(k_CCOut);
1855     Op->Reg.RegNum = RegNum;
1856     Op->StartLoc = S;
1857     Op->EndLoc = S;
1858     return Op;
1859   }
1860
1861   static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
1862     ARMOperand *Op = new ARMOperand(k_Token);
1863     Op->Tok.Data = Str.data();
1864     Op->Tok.Length = Str.size();
1865     Op->StartLoc = S;
1866     Op->EndLoc = S;
1867     return Op;
1868   }
1869
1870   static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
1871     ARMOperand *Op = new ARMOperand(k_Register);
1872     Op->Reg.RegNum = RegNum;
1873     Op->StartLoc = S;
1874     Op->EndLoc = E;
1875     return Op;
1876   }
1877
1878   static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
1879                                            unsigned SrcReg,
1880                                            unsigned ShiftReg,
1881                                            unsigned ShiftImm,
1882                                            SMLoc S, SMLoc E) {
1883     ARMOperand *Op = new ARMOperand(k_ShiftedRegister);
1884     Op->RegShiftedReg.ShiftTy = ShTy;
1885     Op->RegShiftedReg.SrcReg = SrcReg;
1886     Op->RegShiftedReg.ShiftReg = ShiftReg;
1887     Op->RegShiftedReg.ShiftImm = ShiftImm;
1888     Op->StartLoc = S;
1889     Op->EndLoc = E;
1890     return Op;
1891   }
1892
1893   static ARMOperand *CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy,
1894                                             unsigned SrcReg,
1895                                             unsigned ShiftImm,
1896                                             SMLoc S, SMLoc E) {
1897     ARMOperand *Op = new ARMOperand(k_ShiftedImmediate);
1898     Op->RegShiftedImm.ShiftTy = ShTy;
1899     Op->RegShiftedImm.SrcReg = SrcReg;
1900     Op->RegShiftedImm.ShiftImm = ShiftImm;
1901     Op->StartLoc = S;
1902     Op->EndLoc = E;
1903     return Op;
1904   }
1905
1906   static ARMOperand *CreateShifterImm(bool isASR, unsigned Imm,
1907                                    SMLoc S, SMLoc E) {
1908     ARMOperand *Op = new ARMOperand(k_ShifterImmediate);
1909     Op->ShifterImm.isASR = isASR;
1910     Op->ShifterImm.Imm = Imm;
1911     Op->StartLoc = S;
1912     Op->EndLoc = E;
1913     return Op;
1914   }
1915
1916   static ARMOperand *CreateRotImm(unsigned Imm, SMLoc S, SMLoc E) {
1917     ARMOperand *Op = new ARMOperand(k_RotateImmediate);
1918     Op->RotImm.Imm = Imm;
1919     Op->StartLoc = S;
1920     Op->EndLoc = E;
1921     return Op;
1922   }
1923
1924   static ARMOperand *CreateBitfield(unsigned LSB, unsigned Width,
1925                                     SMLoc S, SMLoc E) {
1926     ARMOperand *Op = new ARMOperand(k_BitfieldDescriptor);
1927     Op->Bitfield.LSB = LSB;
1928     Op->Bitfield.Width = Width;
1929     Op->StartLoc = S;
1930     Op->EndLoc = E;
1931     return Op;
1932   }
1933
1934   static ARMOperand *
1935   CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
1936                 SMLoc StartLoc, SMLoc EndLoc) {
1937     KindTy Kind = k_RegisterList;
1938
1939     if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Regs.front().first))
1940       Kind = k_DPRRegisterList;
1941     else if (ARMMCRegisterClasses[ARM::SPRRegClassID].
1942              contains(Regs.front().first))
1943       Kind = k_SPRRegisterList;
1944
1945     ARMOperand *Op = new ARMOperand(Kind);
1946     for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
1947            I = Regs.begin(), E = Regs.end(); I != E; ++I)
1948       Op->Registers.push_back(I->first);
1949     array_pod_sort(Op->Registers.begin(), Op->Registers.end());
1950     Op->StartLoc = StartLoc;
1951     Op->EndLoc = EndLoc;
1952     return Op;
1953   }
1954
1955   static ARMOperand *CreateVectorList(unsigned RegNum, unsigned Count,
1956                                       bool isDoubleSpaced, SMLoc S, SMLoc E) {
1957     ARMOperand *Op = new ARMOperand(k_VectorList);
1958     Op->VectorList.RegNum = RegNum;
1959     Op->VectorList.Count = Count;
1960     Op->VectorList.isDoubleSpaced = isDoubleSpaced;
1961     Op->StartLoc = S;
1962     Op->EndLoc = E;
1963     return Op;
1964   }
1965
1966   static ARMOperand *CreateVectorListAllLanes(unsigned RegNum, unsigned Count,
1967                                               SMLoc S, SMLoc E) {
1968     ARMOperand *Op = new ARMOperand(k_VectorListAllLanes);
1969     Op->VectorList.RegNum = RegNum;
1970     Op->VectorList.Count = Count;
1971     Op->StartLoc = S;
1972     Op->EndLoc = E;
1973     return Op;
1974   }
1975
1976   static ARMOperand *CreateVectorListIndexed(unsigned RegNum, unsigned Count,
1977                                              unsigned Index, SMLoc S, SMLoc E) {
1978     ARMOperand *Op = new ARMOperand(k_VectorListIndexed);
1979     Op->VectorList.RegNum = RegNum;
1980     Op->VectorList.Count = Count;
1981     Op->VectorList.LaneIndex = Index;
1982     Op->StartLoc = S;
1983     Op->EndLoc = E;
1984     return Op;
1985   }
1986
1987   static ARMOperand *CreateVectorIndex(unsigned Idx, SMLoc S, SMLoc E,
1988                                        MCContext &Ctx) {
1989     ARMOperand *Op = new ARMOperand(k_VectorIndex);
1990     Op->VectorIndex.Val = Idx;
1991     Op->StartLoc = S;
1992     Op->EndLoc = E;
1993     return Op;
1994   }
1995
1996   static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
1997     ARMOperand *Op = new ARMOperand(k_Immediate);
1998     Op->Imm.Val = Val;
1999     Op->StartLoc = S;
2000     Op->EndLoc = E;
2001     return Op;
2002   }
2003
2004   static ARMOperand *CreateFPImm(unsigned Val, SMLoc S, MCContext &Ctx) {
2005     ARMOperand *Op = new ARMOperand(k_FPImmediate);
2006     Op->FPImm.Val = Val;
2007     Op->StartLoc = S;
2008     Op->EndLoc = S;
2009     return Op;
2010   }
2011
2012   static ARMOperand *CreateMem(unsigned BaseRegNum,
2013                                const MCConstantExpr *OffsetImm,
2014                                unsigned OffsetRegNum,
2015                                ARM_AM::ShiftOpc ShiftType,
2016                                unsigned ShiftImm,
2017                                unsigned Alignment,
2018                                bool isNegative,
2019                                SMLoc S, SMLoc E) {
2020     ARMOperand *Op = new ARMOperand(k_Memory);
2021     Op->Memory.BaseRegNum = BaseRegNum;
2022     Op->Memory.OffsetImm = OffsetImm;
2023     Op->Memory.OffsetRegNum = OffsetRegNum;
2024     Op->Memory.ShiftType = ShiftType;
2025     Op->Memory.ShiftImm = ShiftImm;
2026     Op->Memory.Alignment = Alignment;
2027     Op->Memory.isNegative = isNegative;
2028     Op->StartLoc = S;
2029     Op->EndLoc = E;
2030     return Op;
2031   }
2032
2033   static ARMOperand *CreatePostIdxReg(unsigned RegNum, bool isAdd,
2034                                       ARM_AM::ShiftOpc ShiftTy,
2035                                       unsigned ShiftImm,
2036                                       SMLoc S, SMLoc E) {
2037     ARMOperand *Op = new ARMOperand(k_PostIndexRegister);
2038     Op->PostIdxReg.RegNum = RegNum;
2039     Op->PostIdxReg.isAdd = isAdd;
2040     Op->PostIdxReg.ShiftTy = ShiftTy;
2041     Op->PostIdxReg.ShiftImm = ShiftImm;
2042     Op->StartLoc = S;
2043     Op->EndLoc = E;
2044     return Op;
2045   }
2046
2047   static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
2048     ARMOperand *Op = new ARMOperand(k_MemBarrierOpt);
2049     Op->MBOpt.Val = Opt;
2050     Op->StartLoc = S;
2051     Op->EndLoc = S;
2052     return Op;
2053   }
2054
2055   static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
2056     ARMOperand *Op = new ARMOperand(k_ProcIFlags);
2057     Op->IFlags.Val = IFlags;
2058     Op->StartLoc = S;
2059     Op->EndLoc = S;
2060     return Op;
2061   }
2062
2063   static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
2064     ARMOperand *Op = new ARMOperand(k_MSRMask);
2065     Op->MMask.Val = MMask;
2066     Op->StartLoc = S;
2067     Op->EndLoc = S;
2068     return Op;
2069   }
2070 };
2071
2072 } // end anonymous namespace.
2073
2074 void ARMOperand::print(raw_ostream &OS) const {
2075   switch (Kind) {
2076   case k_FPImmediate:
2077     OS << "<fpimm " << getFPImm() << "(" << ARM_AM::getFPImmFloat(getFPImm())
2078        << ") >";
2079     break;
2080   case k_CondCode:
2081     OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
2082     break;
2083   case k_CCOut:
2084     OS << "<ccout " << getReg() << ">";
2085     break;
2086   case k_ITCondMask: {
2087     static const char *MaskStr[] = {
2088       "()", "(t)", "(e)", "(tt)", "(et)", "(te)", "(ee)", "(ttt)", "(ett)",
2089       "(tet)", "(eet)", "(tte)", "(ete)", "(tee)", "(eee)"
2090     };
2091     assert((ITMask.Mask & 0xf) == ITMask.Mask);
2092     OS << "<it-mask " << MaskStr[ITMask.Mask] << ">";
2093     break;
2094   }
2095   case k_CoprocNum:
2096     OS << "<coprocessor number: " << getCoproc() << ">";
2097     break;
2098   case k_CoprocReg:
2099     OS << "<coprocessor register: " << getCoproc() << ">";
2100     break;
2101   case k_CoprocOption:
2102     OS << "<coprocessor option: " << CoprocOption.Val << ">";
2103     break;
2104   case k_MSRMask:
2105     OS << "<mask: " << getMSRMask() << ">";
2106     break;
2107   case k_Immediate:
2108     getImm()->print(OS);
2109     break;
2110   case k_MemBarrierOpt:
2111     OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
2112     break;
2113   case k_Memory:
2114     OS << "<memory "
2115        << " base:" << Memory.BaseRegNum;
2116     OS << ">";
2117     break;
2118   case k_PostIndexRegister:
2119     OS << "post-idx register " << (PostIdxReg.isAdd ? "" : "-")
2120        << PostIdxReg.RegNum;
2121     if (PostIdxReg.ShiftTy != ARM_AM::no_shift)
2122       OS << ARM_AM::getShiftOpcStr(PostIdxReg.ShiftTy) << " "
2123          << PostIdxReg.ShiftImm;
2124     OS << ">";
2125     break;
2126   case k_ProcIFlags: {
2127     OS << "<ARM_PROC::";
2128     unsigned IFlags = getProcIFlags();
2129     for (int i=2; i >= 0; --i)
2130       if (IFlags & (1 << i))
2131         OS << ARM_PROC::IFlagsToString(1 << i);
2132     OS << ">";
2133     break;
2134   }
2135   case k_Register:
2136     OS << "<register " << getReg() << ">";
2137     break;
2138   case k_ShifterImmediate:
2139     OS << "<shift " << (ShifterImm.isASR ? "asr" : "lsl")
2140        << " #" << ShifterImm.Imm << ">";
2141     break;
2142   case k_ShiftedRegister:
2143     OS << "<so_reg_reg "
2144        << RegShiftedReg.SrcReg << " "
2145        << ARM_AM::getShiftOpcStr(RegShiftedReg.ShiftTy)
2146        << " " << RegShiftedReg.ShiftReg << ">";
2147     break;
2148   case k_ShiftedImmediate:
2149     OS << "<so_reg_imm "
2150        << RegShiftedImm.SrcReg << " "
2151        << ARM_AM::getShiftOpcStr(RegShiftedImm.ShiftTy)
2152        << " #" << RegShiftedImm.ShiftImm << ">";
2153     break;
2154   case k_RotateImmediate:
2155     OS << "<ror " << " #" << (RotImm.Imm * 8) << ">";
2156     break;
2157   case k_BitfieldDescriptor:
2158     OS << "<bitfield " << "lsb: " << Bitfield.LSB
2159        << ", width: " << Bitfield.Width << ">";
2160     break;
2161   case k_RegisterList:
2162   case k_DPRRegisterList:
2163   case k_SPRRegisterList: {
2164     OS << "<register_list ";
2165
2166     const SmallVectorImpl<unsigned> &RegList = getRegList();
2167     for (SmallVectorImpl<unsigned>::const_iterator
2168            I = RegList.begin(), E = RegList.end(); I != E; ) {
2169       OS << *I;
2170       if (++I < E) OS << ", ";
2171     }
2172
2173     OS << ">";
2174     break;
2175   }
2176   case k_VectorList:
2177     OS << "<vector_list " << VectorList.Count << " * "
2178        << VectorList.RegNum << ">";
2179     break;
2180   case k_VectorListAllLanes:
2181     OS << "<vector_list(all lanes) " << VectorList.Count << " * "
2182        << VectorList.RegNum << ">";
2183     break;
2184   case k_VectorListIndexed:
2185     OS << "<vector_list(lane " << VectorList.LaneIndex << ") "
2186        << VectorList.Count << " * " << VectorList.RegNum << ">";
2187     break;
2188   case k_Token:
2189     OS << "'" << getToken() << "'";
2190     break;
2191   case k_VectorIndex:
2192     OS << "<vectorindex " << getVectorIndex() << ">";
2193     break;
2194   }
2195 }
2196
2197 /// @name Auto-generated Match Functions
2198 /// {
2199
2200 static unsigned MatchRegisterName(StringRef Name);
2201
2202 /// }
2203
2204 bool ARMAsmParser::ParseRegister(unsigned &RegNo,
2205                                  SMLoc &StartLoc, SMLoc &EndLoc) {
2206   StartLoc = Parser.getTok().getLoc();
2207   RegNo = tryParseRegister();
2208   EndLoc = Parser.getTok().getLoc();
2209
2210   return (RegNo == (unsigned)-1);
2211 }
2212
2213 /// Try to parse a register name.  The token must be an Identifier when called,
2214 /// and if it is a register name the token is eaten and the register number is
2215 /// returned.  Otherwise return -1.
2216 ///
2217 int ARMAsmParser::tryParseRegister() {
2218   const AsmToken &Tok = Parser.getTok();
2219   if (Tok.isNot(AsmToken::Identifier)) return -1;
2220
2221   std::string lowerCase = Tok.getString().lower();
2222   unsigned RegNum = MatchRegisterName(lowerCase);
2223   if (!RegNum) {
2224     RegNum = StringSwitch<unsigned>(lowerCase)
2225       .Case("r13", ARM::SP)
2226       .Case("r14", ARM::LR)
2227       .Case("r15", ARM::PC)
2228       .Case("ip", ARM::R12)
2229       // Additional register name aliases for 'gas' compatibility.
2230       .Case("a1", ARM::R0)
2231       .Case("a2", ARM::R1)
2232       .Case("a3", ARM::R2)
2233       .Case("a4", ARM::R3)
2234       .Case("v1", ARM::R4)
2235       .Case("v2", ARM::R5)
2236       .Case("v3", ARM::R6)
2237       .Case("v4", ARM::R7)
2238       .Case("v5", ARM::R8)
2239       .Case("v6", ARM::R9)
2240       .Case("v7", ARM::R10)
2241       .Case("v8", ARM::R11)
2242       .Case("sb", ARM::R9)
2243       .Case("sl", ARM::R10)
2244       .Case("fp", ARM::R11)
2245       .Default(0);
2246   }
2247   if (!RegNum) {
2248     // Check for aliases registered via .req.
2249     StringMap<unsigned>::const_iterator Entry =
2250       RegisterReqs.find(Tok.getIdentifier());
2251     // If no match, return failure.
2252     if (Entry == RegisterReqs.end())
2253       return -1;
2254     Parser.Lex(); // Eat identifier token.
2255     return Entry->getValue();
2256   }
2257
2258   Parser.Lex(); // Eat identifier token.
2259
2260   return RegNum;
2261 }
2262
2263 // Try to parse a shifter  (e.g., "lsl <amt>"). On success, return 0.
2264 // If a recoverable error occurs, return 1. If an irrecoverable error
2265 // occurs, return -1. An irrecoverable error is one where tokens have been
2266 // consumed in the process of trying to parse the shifter (i.e., when it is
2267 // indeed a shifter operand, but malformed).
2268 int ARMAsmParser::tryParseShiftRegister(
2269                                SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2270   SMLoc S = Parser.getTok().getLoc();
2271   const AsmToken &Tok = Parser.getTok();
2272   assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2273
2274   std::string lowerCase = Tok.getString().lower();
2275   ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
2276       .Case("asl", ARM_AM::lsl)
2277       .Case("lsl", ARM_AM::lsl)
2278       .Case("lsr", ARM_AM::lsr)
2279       .Case("asr", ARM_AM::asr)
2280       .Case("ror", ARM_AM::ror)
2281       .Case("rrx", ARM_AM::rrx)
2282       .Default(ARM_AM::no_shift);
2283
2284   if (ShiftTy == ARM_AM::no_shift)
2285     return 1;
2286
2287   Parser.Lex(); // Eat the operator.
2288
2289   // The source register for the shift has already been added to the
2290   // operand list, so we need to pop it off and combine it into the shifted
2291   // register operand instead.
2292   OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
2293   if (!PrevOp->isReg())
2294     return Error(PrevOp->getStartLoc(), "shift must be of a register");
2295   int SrcReg = PrevOp->getReg();
2296   int64_t Imm = 0;
2297   int ShiftReg = 0;
2298   if (ShiftTy == ARM_AM::rrx) {
2299     // RRX Doesn't have an explicit shift amount. The encoder expects
2300     // the shift register to be the same as the source register. Seems odd,
2301     // but OK.
2302     ShiftReg = SrcReg;
2303   } else {
2304     // Figure out if this is shifted by a constant or a register (for non-RRX).
2305     if (Parser.getTok().is(AsmToken::Hash) ||
2306         Parser.getTok().is(AsmToken::Dollar)) {
2307       Parser.Lex(); // Eat hash.
2308       SMLoc ImmLoc = Parser.getTok().getLoc();
2309       const MCExpr *ShiftExpr = 0;
2310       if (getParser().ParseExpression(ShiftExpr)) {
2311         Error(ImmLoc, "invalid immediate shift value");
2312         return -1;
2313       }
2314       // The expression must be evaluatable as an immediate.
2315       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
2316       if (!CE) {
2317         Error(ImmLoc, "invalid immediate shift value");
2318         return -1;
2319       }
2320       // Range check the immediate.
2321       // lsl, ror: 0 <= imm <= 31
2322       // lsr, asr: 0 <= imm <= 32
2323       Imm = CE->getValue();
2324       if (Imm < 0 ||
2325           ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
2326           ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
2327         Error(ImmLoc, "immediate shift value out of range");
2328         return -1;
2329       }
2330     } else if (Parser.getTok().is(AsmToken::Identifier)) {
2331       ShiftReg = tryParseRegister();
2332       SMLoc L = Parser.getTok().getLoc();
2333       if (ShiftReg == -1) {
2334         Error (L, "expected immediate or register in shift operand");
2335         return -1;
2336       }
2337     } else {
2338       Error (Parser.getTok().getLoc(),
2339                     "expected immediate or register in shift operand");
2340       return -1;
2341     }
2342   }
2343
2344   if (ShiftReg && ShiftTy != ARM_AM::rrx)
2345     Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
2346                                                          ShiftReg, Imm,
2347                                                S, Parser.getTok().getLoc()));
2348   else
2349     Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
2350                                                S, Parser.getTok().getLoc()));
2351
2352   return 0;
2353 }
2354
2355
2356 /// Try to parse a register name.  The token must be an Identifier when called.
2357 /// If it's a register, an AsmOperand is created. Another AsmOperand is created
2358 /// if there is a "writeback". 'true' if it's not a register.
2359 ///
2360 /// TODO this is likely to change to allow different register types and or to
2361 /// parse for a specific register type.
2362 bool ARMAsmParser::
2363 tryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2364   SMLoc S = Parser.getTok().getLoc();
2365   int RegNo = tryParseRegister();
2366   if (RegNo == -1)
2367     return true;
2368
2369   Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
2370
2371   const AsmToken &ExclaimTok = Parser.getTok();
2372   if (ExclaimTok.is(AsmToken::Exclaim)) {
2373     Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
2374                                                ExclaimTok.getLoc()));
2375     Parser.Lex(); // Eat exclaim token
2376     return false;
2377   }
2378
2379   // Also check for an index operand. This is only legal for vector registers,
2380   // but that'll get caught OK in operand matching, so we don't need to
2381   // explicitly filter everything else out here.
2382   if (Parser.getTok().is(AsmToken::LBrac)) {
2383     SMLoc SIdx = Parser.getTok().getLoc();
2384     Parser.Lex(); // Eat left bracket token.
2385
2386     const MCExpr *ImmVal;
2387     if (getParser().ParseExpression(ImmVal))
2388       return MatchOperand_ParseFail;
2389     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
2390     if (!MCE) {
2391       TokError("immediate value expected for vector index");
2392       return MatchOperand_ParseFail;
2393     }
2394
2395     SMLoc E = Parser.getTok().getLoc();
2396     if (Parser.getTok().isNot(AsmToken::RBrac)) {
2397       Error(E, "']' expected");
2398       return MatchOperand_ParseFail;
2399     }
2400
2401     Parser.Lex(); // Eat right bracket token.
2402
2403     Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
2404                                                      SIdx, E,
2405                                                      getContext()));
2406   }
2407
2408   return false;
2409 }
2410
2411 /// MatchCoprocessorOperandName - Try to parse an coprocessor related
2412 /// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
2413 /// "c5", ...
2414 static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
2415   // Use the same layout as the tablegen'erated register name matcher. Ugly,
2416   // but efficient.
2417   switch (Name.size()) {
2418   default: break;
2419   case 2:
2420     if (Name[0] != CoprocOp)
2421       return -1;
2422     switch (Name[1]) {
2423     default:  return -1;
2424     case '0': return 0;
2425     case '1': return 1;
2426     case '2': return 2;
2427     case '3': return 3;
2428     case '4': return 4;
2429     case '5': return 5;
2430     case '6': return 6;
2431     case '7': return 7;
2432     case '8': return 8;
2433     case '9': return 9;
2434     }
2435     break;
2436   case 3:
2437     if (Name[0] != CoprocOp || Name[1] != '1')
2438       return -1;
2439     switch (Name[2]) {
2440     default:  return -1;
2441     case '0': return 10;
2442     case '1': return 11;
2443     case '2': return 12;
2444     case '3': return 13;
2445     case '4': return 14;
2446     case '5': return 15;
2447     }
2448     break;
2449   }
2450
2451   return -1;
2452 }
2453
2454 /// parseITCondCode - Try to parse a condition code for an IT instruction.
2455 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2456 parseITCondCode(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2457   SMLoc S = Parser.getTok().getLoc();
2458   const AsmToken &Tok = Parser.getTok();
2459   if (!Tok.is(AsmToken::Identifier))
2460     return MatchOperand_NoMatch;
2461   unsigned CC = StringSwitch<unsigned>(Tok.getString())
2462     .Case("eq", ARMCC::EQ)
2463     .Case("ne", ARMCC::NE)
2464     .Case("hs", ARMCC::HS)
2465     .Case("cs", ARMCC::HS)
2466     .Case("lo", ARMCC::LO)
2467     .Case("cc", ARMCC::LO)
2468     .Case("mi", ARMCC::MI)
2469     .Case("pl", ARMCC::PL)
2470     .Case("vs", ARMCC::VS)
2471     .Case("vc", ARMCC::VC)
2472     .Case("hi", ARMCC::HI)
2473     .Case("ls", ARMCC::LS)
2474     .Case("ge", ARMCC::GE)
2475     .Case("lt", ARMCC::LT)
2476     .Case("gt", ARMCC::GT)
2477     .Case("le", ARMCC::LE)
2478     .Case("al", ARMCC::AL)
2479     .Default(~0U);
2480   if (CC == ~0U)
2481     return MatchOperand_NoMatch;
2482   Parser.Lex(); // Eat the token.
2483
2484   Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), S));
2485
2486   return MatchOperand_Success;
2487 }
2488
2489 /// parseCoprocNumOperand - Try to parse an coprocessor number operand. The
2490 /// token must be an Identifier when called, and if it is a coprocessor
2491 /// number, the token is eaten and the operand is added to the operand list.
2492 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2493 parseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2494   SMLoc S = Parser.getTok().getLoc();
2495   const AsmToken &Tok = Parser.getTok();
2496   if (Tok.isNot(AsmToken::Identifier))
2497     return MatchOperand_NoMatch;
2498
2499   int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
2500   if (Num == -1)
2501     return MatchOperand_NoMatch;
2502
2503   Parser.Lex(); // Eat identifier token.
2504   Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
2505   return MatchOperand_Success;
2506 }
2507
2508 /// parseCoprocRegOperand - Try to parse an coprocessor register operand. The
2509 /// token must be an Identifier when called, and if it is a coprocessor
2510 /// number, the token is eaten and the operand is added to the operand list.
2511 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2512 parseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2513   SMLoc S = Parser.getTok().getLoc();
2514   const AsmToken &Tok = Parser.getTok();
2515   if (Tok.isNot(AsmToken::Identifier))
2516     return MatchOperand_NoMatch;
2517
2518   int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
2519   if (Reg == -1)
2520     return MatchOperand_NoMatch;
2521
2522   Parser.Lex(); // Eat identifier token.
2523   Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
2524   return MatchOperand_Success;
2525 }
2526
2527 /// parseCoprocOptionOperand - Try to parse an coprocessor option operand.
2528 /// coproc_option : '{' imm0_255 '}'
2529 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2530 parseCoprocOptionOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2531   SMLoc S = Parser.getTok().getLoc();
2532
2533   // If this isn't a '{', this isn't a coprocessor immediate operand.
2534   if (Parser.getTok().isNot(AsmToken::LCurly))
2535     return MatchOperand_NoMatch;
2536   Parser.Lex(); // Eat the '{'
2537
2538   const MCExpr *Expr;
2539   SMLoc Loc = Parser.getTok().getLoc();
2540   if (getParser().ParseExpression(Expr)) {
2541     Error(Loc, "illegal expression");
2542     return MatchOperand_ParseFail;
2543   }
2544   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
2545   if (!CE || CE->getValue() < 0 || CE->getValue() > 255) {
2546     Error(Loc, "coprocessor option must be an immediate in range [0, 255]");
2547     return MatchOperand_ParseFail;
2548   }
2549   int Val = CE->getValue();
2550
2551   // Check for and consume the closing '}'
2552   if (Parser.getTok().isNot(AsmToken::RCurly))
2553     return MatchOperand_ParseFail;
2554   SMLoc E = Parser.getTok().getLoc();
2555   Parser.Lex(); // Eat the '}'
2556
2557   Operands.push_back(ARMOperand::CreateCoprocOption(Val, S, E));
2558   return MatchOperand_Success;
2559 }
2560
2561 // For register list parsing, we need to map from raw GPR register numbering
2562 // to the enumeration values. The enumeration values aren't sorted by
2563 // register number due to our using "sp", "lr" and "pc" as canonical names.
2564 static unsigned getNextRegister(unsigned Reg) {
2565   // If this is a GPR, we need to do it manually, otherwise we can rely
2566   // on the sort ordering of the enumeration since the other reg-classes
2567   // are sane.
2568   if (!ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2569     return Reg + 1;
2570   switch(Reg) {
2571   default: assert(0 && "Invalid GPR number!");
2572   case ARM::R0:  return ARM::R1;  case ARM::R1:  return ARM::R2;
2573   case ARM::R2:  return ARM::R3;  case ARM::R3:  return ARM::R4;
2574   case ARM::R4:  return ARM::R5;  case ARM::R5:  return ARM::R6;
2575   case ARM::R6:  return ARM::R7;  case ARM::R7:  return ARM::R8;
2576   case ARM::R8:  return ARM::R9;  case ARM::R9:  return ARM::R10;
2577   case ARM::R10: return ARM::R11; case ARM::R11: return ARM::R12;
2578   case ARM::R12: return ARM::SP;  case ARM::SP:  return ARM::LR;
2579   case ARM::LR:  return ARM::PC;  case ARM::PC:  return ARM::R0;
2580   }
2581 }
2582
2583 // Return the low-subreg of a given Q register.
2584 static unsigned getDRegFromQReg(unsigned QReg) {
2585   switch (QReg) {
2586   default: llvm_unreachable("expected a Q register!");
2587   case ARM::Q0:  return ARM::D0;
2588   case ARM::Q1:  return ARM::D2;
2589   case ARM::Q2:  return ARM::D4;
2590   case ARM::Q3:  return ARM::D6;
2591   case ARM::Q4:  return ARM::D8;
2592   case ARM::Q5:  return ARM::D10;
2593   case ARM::Q6:  return ARM::D12;
2594   case ARM::Q7:  return ARM::D14;
2595   case ARM::Q8:  return ARM::D16;
2596   case ARM::Q9:  return ARM::D18;
2597   case ARM::Q10: return ARM::D20;
2598   case ARM::Q11: return ARM::D22;
2599   case ARM::Q12: return ARM::D24;
2600   case ARM::Q13: return ARM::D26;
2601   case ARM::Q14: return ARM::D28;
2602   case ARM::Q15: return ARM::D30;
2603   }
2604 }
2605
2606 /// Parse a register list.
2607 bool ARMAsmParser::
2608 parseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2609   assert(Parser.getTok().is(AsmToken::LCurly) &&
2610          "Token is not a Left Curly Brace");
2611   SMLoc S = Parser.getTok().getLoc();
2612   Parser.Lex(); // Eat '{' token.
2613   SMLoc RegLoc = Parser.getTok().getLoc();
2614
2615   // Check the first register in the list to see what register class
2616   // this is a list of.
2617   int Reg = tryParseRegister();
2618   if (Reg == -1)
2619     return Error(RegLoc, "register expected");
2620
2621   // The reglist instructions have at most 16 registers, so reserve
2622   // space for that many.
2623   SmallVector<std::pair<unsigned, SMLoc>, 16> Registers;
2624
2625   // Allow Q regs and just interpret them as the two D sub-registers.
2626   if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2627     Reg = getDRegFromQReg(Reg);
2628     Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2629     ++Reg;
2630   }
2631   const MCRegisterClass *RC;
2632   if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2633     RC = &ARMMCRegisterClasses[ARM::GPRRegClassID];
2634   else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg))
2635     RC = &ARMMCRegisterClasses[ARM::DPRRegClassID];
2636   else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg))
2637     RC = &ARMMCRegisterClasses[ARM::SPRRegClassID];
2638   else
2639     return Error(RegLoc, "invalid register in register list");
2640
2641   // Store the register.
2642   Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2643
2644   // This starts immediately after the first register token in the list,
2645   // so we can see either a comma or a minus (range separator) as a legal
2646   // next token.
2647   while (Parser.getTok().is(AsmToken::Comma) ||
2648          Parser.getTok().is(AsmToken::Minus)) {
2649     if (Parser.getTok().is(AsmToken::Minus)) {
2650       Parser.Lex(); // Eat the minus.
2651       SMLoc EndLoc = Parser.getTok().getLoc();
2652       int EndReg = tryParseRegister();
2653       if (EndReg == -1)
2654         return Error(EndLoc, "register expected");
2655       // Allow Q regs and just interpret them as the two D sub-registers.
2656       if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
2657         EndReg = getDRegFromQReg(EndReg) + 1;
2658       // If the register is the same as the start reg, there's nothing
2659       // more to do.
2660       if (Reg == EndReg)
2661         continue;
2662       // The register must be in the same register class as the first.
2663       if (!RC->contains(EndReg))
2664         return Error(EndLoc, "invalid register in register list");
2665       // Ranges must go from low to high.
2666       if (getARMRegisterNumbering(Reg) > getARMRegisterNumbering(EndReg))
2667         return Error(EndLoc, "bad range in register list");
2668
2669       // Add all the registers in the range to the register list.
2670       while (Reg != EndReg) {
2671         Reg = getNextRegister(Reg);
2672         Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2673       }
2674       continue;
2675     }
2676     Parser.Lex(); // Eat the comma.
2677     RegLoc = Parser.getTok().getLoc();
2678     int OldReg = Reg;
2679     const AsmToken RegTok = Parser.getTok();
2680     Reg = tryParseRegister();
2681     if (Reg == -1)
2682       return Error(RegLoc, "register expected");
2683     // Allow Q regs and just interpret them as the two D sub-registers.
2684     bool isQReg = false;
2685     if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2686       Reg = getDRegFromQReg(Reg);
2687       isQReg = true;
2688     }
2689     // The register must be in the same register class as the first.
2690     if (!RC->contains(Reg))
2691       return Error(RegLoc, "invalid register in register list");
2692     // List must be monotonically increasing.
2693     if (getARMRegisterNumbering(Reg) < getARMRegisterNumbering(OldReg))
2694       return Error(RegLoc, "register list not in ascending order");
2695     if (getARMRegisterNumbering(Reg) == getARMRegisterNumbering(OldReg)) {
2696       Warning(RegLoc, "duplicated register (" + RegTok.getString() +
2697               ") in register list");
2698       continue;
2699     }
2700     // VFP register lists must also be contiguous.
2701     // It's OK to use the enumeration values directly here rather, as the
2702     // VFP register classes have the enum sorted properly.
2703     if (RC != &ARMMCRegisterClasses[ARM::GPRRegClassID] &&
2704         Reg != OldReg + 1)
2705       return Error(RegLoc, "non-contiguous register range");
2706     Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2707     if (isQReg)
2708       Registers.push_back(std::pair<unsigned, SMLoc>(++Reg, RegLoc));
2709   }
2710
2711   SMLoc E = Parser.getTok().getLoc();
2712   if (Parser.getTok().isNot(AsmToken::RCurly))
2713     return Error(E, "'}' expected");
2714   Parser.Lex(); // Eat '}' token.
2715
2716   // Push the register list operand.
2717   Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
2718
2719   // The ARM system instruction variants for LDM/STM have a '^' token here.
2720   if (Parser.getTok().is(AsmToken::Caret)) {
2721     Operands.push_back(ARMOperand::CreateToken("^",Parser.getTok().getLoc()));
2722     Parser.Lex(); // Eat '^' token.
2723   }
2724
2725   return false;
2726 }
2727
2728 // Helper function to parse the lane index for vector lists.
2729 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2730 parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index) {
2731   Index = 0; // Always return a defined index value.
2732   if (Parser.getTok().is(AsmToken::LBrac)) {
2733     Parser.Lex(); // Eat the '['.
2734     if (Parser.getTok().is(AsmToken::RBrac)) {
2735       // "Dn[]" is the 'all lanes' syntax.
2736       LaneKind = AllLanes;
2737       Parser.Lex(); // Eat the ']'.
2738       return MatchOperand_Success;
2739     }
2740     if (Parser.getTok().is(AsmToken::Integer)) {
2741       int64_t Val = Parser.getTok().getIntVal();
2742       // Make this range check context sensitive for .8, .16, .32.
2743       if (Val < 0 && Val > 7)
2744         Error(Parser.getTok().getLoc(), "lane index out of range");
2745       Index = Val;
2746       LaneKind = IndexedLane;
2747       Parser.Lex(); // Eat the token;
2748       if (Parser.getTok().isNot(AsmToken::RBrac))
2749         Error(Parser.getTok().getLoc(), "']' expected");
2750       Parser.Lex(); // Eat the ']'.
2751       return MatchOperand_Success;
2752     }
2753     Error(Parser.getTok().getLoc(), "lane index must be empty or an integer");
2754     return MatchOperand_ParseFail;
2755   }
2756   LaneKind = NoLanes;
2757   return MatchOperand_Success;
2758 }
2759
2760 // parse a vector register list
2761 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2762 parseVectorList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2763   VectorLaneTy LaneKind;
2764   unsigned LaneIndex;
2765   SMLoc S = Parser.getTok().getLoc();
2766   // As an extension (to match gas), support a plain D register or Q register
2767   // (without encosing curly braces) as a single or double entry list,
2768   // respectively.
2769   if (Parser.getTok().is(AsmToken::Identifier)) {
2770     int Reg = tryParseRegister();
2771     if (Reg == -1)
2772       return MatchOperand_NoMatch;
2773     SMLoc E = Parser.getTok().getLoc();
2774     if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg)) {
2775       OperandMatchResultTy Res = parseVectorLane(LaneKind, LaneIndex);
2776       if (Res != MatchOperand_Success)
2777         return Res;
2778       switch (LaneKind) {
2779       default:
2780         assert(0 && "unexpected lane kind!");
2781       case NoLanes:
2782         E = Parser.getTok().getLoc();
2783         Operands.push_back(ARMOperand::CreateVectorList(Reg, 1, false, S, E));
2784         break;
2785       case AllLanes:
2786         E = Parser.getTok().getLoc();
2787         Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 1, S, E));
2788         break;
2789       case IndexedLane:
2790         Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 1,
2791                                                                LaneIndex, S,E));
2792         break;
2793       }
2794       return MatchOperand_Success;
2795     }
2796     if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2797       Reg = getDRegFromQReg(Reg);
2798       OperandMatchResultTy Res = parseVectorLane(LaneKind, LaneIndex);
2799       if (Res != MatchOperand_Success)
2800         return Res;
2801       switch (LaneKind) {
2802       default:
2803         assert(0 && "unexpected lane kind!");
2804       case NoLanes:
2805         E = Parser.getTok().getLoc();
2806         Operands.push_back(ARMOperand::CreateVectorList(Reg, 2, false, S, E));
2807         break;
2808       case AllLanes:
2809         E = Parser.getTok().getLoc();
2810         Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 2, S, E));
2811         break;
2812       case IndexedLane:
2813         Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 2,
2814                                                                LaneIndex, S,E));
2815         break;
2816       }
2817       return MatchOperand_Success;
2818     }
2819     Error(S, "vector register expected");
2820     return MatchOperand_ParseFail;
2821   }
2822
2823   if (Parser.getTok().isNot(AsmToken::LCurly))
2824     return MatchOperand_NoMatch;
2825
2826   Parser.Lex(); // Eat '{' token.
2827   SMLoc RegLoc = Parser.getTok().getLoc();
2828
2829   int Reg = tryParseRegister();
2830   if (Reg == -1) {
2831     Error(RegLoc, "register expected");
2832     return MatchOperand_ParseFail;
2833   }
2834   unsigned Count = 1;
2835   int Spacing = 0;
2836   unsigned FirstReg = Reg;
2837   // The list is of D registers, but we also allow Q regs and just interpret
2838   // them as the two D sub-registers.
2839   if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2840     FirstReg = Reg = getDRegFromQReg(Reg);
2841     Spacing = 1; // double-spacing requires explicit D registers, otherwise
2842                  // it's ambiguous with four-register single spaced.
2843     ++Reg;
2844     ++Count;
2845   }
2846   if (parseVectorLane(LaneKind, LaneIndex) != MatchOperand_Success)
2847     return MatchOperand_ParseFail;
2848
2849   while (Parser.getTok().is(AsmToken::Comma) ||
2850          Parser.getTok().is(AsmToken::Minus)) {
2851     if (Parser.getTok().is(AsmToken::Minus)) {
2852       if (!Spacing)
2853         Spacing = 1; // Register range implies a single spaced list.
2854       else if (Spacing == 2) {
2855         Error(Parser.getTok().getLoc(),
2856               "sequential registers in double spaced list");
2857         return MatchOperand_ParseFail;
2858       }
2859       Parser.Lex(); // Eat the minus.
2860       SMLoc EndLoc = Parser.getTok().getLoc();
2861       int EndReg = tryParseRegister();
2862       if (EndReg == -1) {
2863         Error(EndLoc, "register expected");
2864         return MatchOperand_ParseFail;
2865       }
2866       // Allow Q regs and just interpret them as the two D sub-registers.
2867       if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
2868         EndReg = getDRegFromQReg(EndReg) + 1;
2869       // If the register is the same as the start reg, there's nothing
2870       // more to do.
2871       if (Reg == EndReg)
2872         continue;
2873       // The register must be in the same register class as the first.
2874       if (!ARMMCRegisterClasses[ARM::DPRRegClassID].contains(EndReg)) {
2875         Error(EndLoc, "invalid register in register list");
2876         return MatchOperand_ParseFail;
2877       }
2878       // Ranges must go from low to high.
2879       if (Reg > EndReg) {
2880         Error(EndLoc, "bad range in register list");
2881         return MatchOperand_ParseFail;
2882       }
2883       // Parse the lane specifier if present.
2884       VectorLaneTy NextLaneKind;
2885       unsigned NextLaneIndex;
2886       if (parseVectorLane(NextLaneKind, NextLaneIndex) != MatchOperand_Success)
2887         return MatchOperand_ParseFail;
2888       if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
2889         Error(EndLoc, "mismatched lane index in register list");
2890         return MatchOperand_ParseFail;
2891       }
2892       EndLoc = Parser.getTok().getLoc();
2893
2894       // Add all the registers in the range to the register list.
2895       Count += EndReg - Reg;
2896       Reg = EndReg;
2897       continue;
2898     }
2899     Parser.Lex(); // Eat the comma.
2900     RegLoc = Parser.getTok().getLoc();
2901     int OldReg = Reg;
2902     Reg = tryParseRegister();
2903     if (Reg == -1) {
2904       Error(RegLoc, "register expected");
2905       return MatchOperand_ParseFail;
2906     }
2907     // vector register lists must be contiguous.
2908     // It's OK to use the enumeration values directly here rather, as the
2909     // VFP register classes have the enum sorted properly.
2910     //
2911     // The list is of D registers, but we also allow Q regs and just interpret
2912     // them as the two D sub-registers.
2913     if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2914       if (!Spacing)
2915         Spacing = 1; // Register range implies a single spaced list.
2916       else if (Spacing == 2) {
2917         Error(RegLoc,
2918               "invalid register in double-spaced list (must be 'D' register')");
2919         return MatchOperand_ParseFail;
2920       }
2921       Reg = getDRegFromQReg(Reg);
2922       if (Reg != OldReg + 1) {
2923         Error(RegLoc, "non-contiguous register range");
2924         return MatchOperand_ParseFail;
2925       }
2926       ++Reg;
2927       Count += 2;
2928       // Parse the lane specifier if present.
2929       VectorLaneTy NextLaneKind;
2930       unsigned NextLaneIndex;
2931       SMLoc EndLoc = Parser.getTok().getLoc();
2932       if (parseVectorLane(NextLaneKind, NextLaneIndex) != MatchOperand_Success)
2933         return MatchOperand_ParseFail;
2934       if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
2935         Error(EndLoc, "mismatched lane index in register list");
2936         return MatchOperand_ParseFail;
2937       }
2938       continue;
2939     }
2940     // Normal D register.
2941     // Figure out the register spacing (single or double) of the list if
2942     // we don't know it already.
2943     if (!Spacing)
2944       Spacing = 1 + (Reg == OldReg + 2);
2945
2946     // Just check that it's contiguous and keep going.
2947     if (Reg != OldReg + Spacing) {
2948       Error(RegLoc, "non-contiguous register range");
2949       return MatchOperand_ParseFail;
2950     }
2951     ++Count;
2952     // Parse the lane specifier if present.
2953     VectorLaneTy NextLaneKind;
2954     unsigned NextLaneIndex;
2955     SMLoc EndLoc = Parser.getTok().getLoc();
2956     if (parseVectorLane(NextLaneKind, NextLaneIndex) != MatchOperand_Success)
2957       return MatchOperand_ParseFail;
2958     if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
2959       Error(EndLoc, "mismatched lane index in register list");
2960       return MatchOperand_ParseFail;
2961     }
2962     if (Spacing == 2 && LaneKind != NoLanes) {
2963       Error(EndLoc,
2964             "lane index specfier invalid in double spaced register list");
2965       return MatchOperand_ParseFail;
2966     }
2967   }
2968
2969   SMLoc E = Parser.getTok().getLoc();
2970   if (Parser.getTok().isNot(AsmToken::RCurly)) {
2971     Error(E, "'}' expected");
2972     return MatchOperand_ParseFail;
2973   }
2974   Parser.Lex(); // Eat '}' token.
2975
2976   switch (LaneKind) {
2977   default:
2978     assert(0 && "unexpected lane kind in register list.");
2979   case NoLanes:
2980     Operands.push_back(ARMOperand::CreateVectorList(FirstReg, Count,
2981                                                     (Spacing == 2), S, E));
2982     break;
2983   case AllLanes:
2984     Operands.push_back(ARMOperand::CreateVectorListAllLanes(FirstReg, Count,
2985                                                             S, E));
2986     break;
2987   case IndexedLane:
2988     Operands.push_back(ARMOperand::CreateVectorListIndexed(FirstReg, Count,
2989                                                            LaneIndex, S, E));
2990     break;
2991   }
2992   return MatchOperand_Success;
2993 }
2994
2995 /// parseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
2996 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2997 parseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2998   SMLoc S = Parser.getTok().getLoc();
2999   const AsmToken &Tok = Parser.getTok();
3000   assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
3001   StringRef OptStr = Tok.getString();
3002
3003   unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
3004     .Case("sy",    ARM_MB::SY)
3005     .Case("st",    ARM_MB::ST)
3006     .Case("sh",    ARM_MB::ISH)
3007     .Case("ish",   ARM_MB::ISH)
3008     .Case("shst",  ARM_MB::ISHST)
3009     .Case("ishst", ARM_MB::ISHST)
3010     .Case("nsh",   ARM_MB::NSH)
3011     .Case("un",    ARM_MB::NSH)
3012     .Case("nshst", ARM_MB::NSHST)
3013     .Case("unst",  ARM_MB::NSHST)
3014     .Case("osh",   ARM_MB::OSH)
3015     .Case("oshst", ARM_MB::OSHST)
3016     .Default(~0U);
3017
3018   if (Opt == ~0U)
3019     return MatchOperand_NoMatch;
3020
3021   Parser.Lex(); // Eat identifier token.
3022   Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
3023   return MatchOperand_Success;
3024 }
3025
3026 /// parseProcIFlagsOperand - Try to parse iflags from CPS instruction.
3027 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3028 parseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3029   SMLoc S = Parser.getTok().getLoc();
3030   const AsmToken &Tok = Parser.getTok();
3031   assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
3032   StringRef IFlagsStr = Tok.getString();
3033
3034   // An iflags string of "none" is interpreted to mean that none of the AIF
3035   // bits are set.  Not a terribly useful instruction, but a valid encoding.
3036   unsigned IFlags = 0;
3037   if (IFlagsStr != "none") {
3038         for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
3039       unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
3040         .Case("a", ARM_PROC::A)
3041         .Case("i", ARM_PROC::I)
3042         .Case("f", ARM_PROC::F)
3043         .Default(~0U);
3044
3045       // If some specific iflag is already set, it means that some letter is
3046       // present more than once, this is not acceptable.
3047       if (Flag == ~0U || (IFlags & Flag))
3048         return MatchOperand_NoMatch;
3049
3050       IFlags |= Flag;
3051     }
3052   }
3053
3054   Parser.Lex(); // Eat identifier token.
3055   Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
3056   return MatchOperand_Success;
3057 }
3058
3059 /// parseMSRMaskOperand - Try to parse mask flags from MSR instruction.
3060 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3061 parseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3062   SMLoc S = Parser.getTok().getLoc();
3063   const AsmToken &Tok = Parser.getTok();
3064   assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
3065   StringRef Mask = Tok.getString();
3066
3067   if (isMClass()) {
3068     // See ARMv6-M 10.1.1
3069     unsigned FlagsVal = StringSwitch<unsigned>(Mask)
3070       .Case("apsr", 0)
3071       .Case("iapsr", 1)
3072       .Case("eapsr", 2)
3073       .Case("xpsr", 3)
3074       .Case("ipsr", 5)
3075       .Case("epsr", 6)
3076       .Case("iepsr", 7)
3077       .Case("msp", 8)
3078       .Case("psp", 9)
3079       .Case("primask", 16)
3080       .Case("basepri", 17)
3081       .Case("basepri_max", 18)
3082       .Case("faultmask", 19)
3083       .Case("control", 20)
3084       .Default(~0U);
3085     
3086     if (FlagsVal == ~0U)
3087       return MatchOperand_NoMatch;
3088
3089     if (!hasV7Ops() && FlagsVal >= 17 && FlagsVal <= 19)
3090       // basepri, basepri_max and faultmask only valid for V7m.
3091       return MatchOperand_NoMatch;
3092     
3093     Parser.Lex(); // Eat identifier token.
3094     Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
3095     return MatchOperand_Success;
3096   }
3097
3098   // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
3099   size_t Start = 0, Next = Mask.find('_');
3100   StringRef Flags = "";
3101   std::string SpecReg = Mask.slice(Start, Next).lower();
3102   if (Next != StringRef::npos)
3103     Flags = Mask.slice(Next+1, Mask.size());
3104
3105   // FlagsVal contains the complete mask:
3106   // 3-0: Mask
3107   // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
3108   unsigned FlagsVal = 0;
3109
3110   if (SpecReg == "apsr") {
3111     FlagsVal = StringSwitch<unsigned>(Flags)
3112     .Case("nzcvq",  0x8) // same as CPSR_f
3113     .Case("g",      0x4) // same as CPSR_s
3114     .Case("nzcvqg", 0xc) // same as CPSR_fs
3115     .Default(~0U);
3116
3117     if (FlagsVal == ~0U) {
3118       if (!Flags.empty())
3119         return MatchOperand_NoMatch;
3120       else
3121         FlagsVal = 8; // No flag
3122     }
3123   } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
3124     if (Flags == "all") // cpsr_all is an alias for cpsr_fc
3125       Flags = "fc";
3126     for (int i = 0, e = Flags.size(); i != e; ++i) {
3127       unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
3128       .Case("c", 1)
3129       .Case("x", 2)
3130       .Case("s", 4)
3131       .Case("f", 8)
3132       .Default(~0U);
3133
3134       // If some specific flag is already set, it means that some letter is
3135       // present more than once, this is not acceptable.
3136       if (FlagsVal == ~0U || (FlagsVal & Flag))
3137         return MatchOperand_NoMatch;
3138       FlagsVal |= Flag;
3139     }
3140   } else // No match for special register.
3141     return MatchOperand_NoMatch;
3142
3143   // Special register without flags is NOT equivalent to "fc" flags.
3144   // NOTE: This is a divergence from gas' behavior.  Uncommenting the following
3145   // two lines would enable gas compatibility at the expense of breaking
3146   // round-tripping.
3147   //
3148   // if (!FlagsVal)
3149   //  FlagsVal = 0x9;
3150
3151   // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
3152   if (SpecReg == "spsr")
3153     FlagsVal |= 16;
3154
3155   Parser.Lex(); // Eat identifier token.
3156   Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
3157   return MatchOperand_Success;
3158 }
3159
3160 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3161 parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands, StringRef Op,
3162             int Low, int High) {
3163   const AsmToken &Tok = Parser.getTok();
3164   if (Tok.isNot(AsmToken::Identifier)) {
3165     Error(Parser.getTok().getLoc(), Op + " operand expected.");
3166     return MatchOperand_ParseFail;
3167   }
3168   StringRef ShiftName = Tok.getString();
3169   std::string LowerOp = Op.lower();
3170   std::string UpperOp = Op.upper();
3171   if (ShiftName != LowerOp && ShiftName != UpperOp) {
3172     Error(Parser.getTok().getLoc(), Op + " operand expected.");
3173     return MatchOperand_ParseFail;
3174   }
3175   Parser.Lex(); // Eat shift type token.
3176
3177   // There must be a '#' and a shift amount.
3178   if (Parser.getTok().isNot(AsmToken::Hash) &&
3179       Parser.getTok().isNot(AsmToken::Dollar)) {
3180     Error(Parser.getTok().getLoc(), "'#' expected");
3181     return MatchOperand_ParseFail;
3182   }
3183   Parser.Lex(); // Eat hash token.
3184
3185   const MCExpr *ShiftAmount;
3186   SMLoc Loc = Parser.getTok().getLoc();
3187   if (getParser().ParseExpression(ShiftAmount)) {
3188     Error(Loc, "illegal expression");
3189     return MatchOperand_ParseFail;
3190   }
3191   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
3192   if (!CE) {
3193     Error(Loc, "constant expression expected");
3194     return MatchOperand_ParseFail;
3195   }
3196   int Val = CE->getValue();
3197   if (Val < Low || Val > High) {
3198     Error(Loc, "immediate value out of range");
3199     return MatchOperand_ParseFail;
3200   }
3201
3202   Operands.push_back(ARMOperand::CreateImm(CE, Loc, Parser.getTok().getLoc()));
3203
3204   return MatchOperand_Success;
3205 }
3206
3207 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3208 parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3209   const AsmToken &Tok = Parser.getTok();
3210   SMLoc S = Tok.getLoc();
3211   if (Tok.isNot(AsmToken::Identifier)) {
3212     Error(Tok.getLoc(), "'be' or 'le' operand expected");
3213     return MatchOperand_ParseFail;
3214   }
3215   int Val = StringSwitch<int>(Tok.getString())
3216     .Case("be", 1)
3217     .Case("le", 0)
3218     .Default(-1);
3219   Parser.Lex(); // Eat the token.
3220
3221   if (Val == -1) {
3222     Error(Tok.getLoc(), "'be' or 'le' operand expected");
3223     return MatchOperand_ParseFail;
3224   }
3225   Operands.push_back(ARMOperand::CreateImm(MCConstantExpr::Create(Val,
3226                                                                   getContext()),
3227                                            S, Parser.getTok().getLoc()));
3228   return MatchOperand_Success;
3229 }
3230
3231 /// parseShifterImm - Parse the shifter immediate operand for SSAT/USAT
3232 /// instructions. Legal values are:
3233 ///     lsl #n  'n' in [0,31]
3234 ///     asr #n  'n' in [1,32]
3235 ///             n == 32 encoded as n == 0.
3236 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3237 parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3238   const AsmToken &Tok = Parser.getTok();
3239   SMLoc S = Tok.getLoc();
3240   if (Tok.isNot(AsmToken::Identifier)) {
3241     Error(S, "shift operator 'asr' or 'lsl' expected");
3242     return MatchOperand_ParseFail;
3243   }
3244   StringRef ShiftName = Tok.getString();
3245   bool isASR;
3246   if (ShiftName == "lsl" || ShiftName == "LSL")
3247     isASR = false;
3248   else if (ShiftName == "asr" || ShiftName == "ASR")
3249     isASR = true;
3250   else {
3251     Error(S, "shift operator 'asr' or 'lsl' expected");
3252     return MatchOperand_ParseFail;
3253   }
3254   Parser.Lex(); // Eat the operator.
3255
3256   // A '#' and a shift amount.
3257   if (Parser.getTok().isNot(AsmToken::Hash) &&
3258       Parser.getTok().isNot(AsmToken::Dollar)) {
3259     Error(Parser.getTok().getLoc(), "'#' expected");
3260     return MatchOperand_ParseFail;
3261   }
3262   Parser.Lex(); // Eat hash token.
3263
3264   const MCExpr *ShiftAmount;
3265   SMLoc E = Parser.getTok().getLoc();
3266   if (getParser().ParseExpression(ShiftAmount)) {
3267     Error(E, "malformed shift expression");
3268     return MatchOperand_ParseFail;
3269   }
3270   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
3271   if (!CE) {
3272     Error(E, "shift amount must be an immediate");
3273     return MatchOperand_ParseFail;
3274   }
3275
3276   int64_t Val = CE->getValue();
3277   if (isASR) {
3278     // Shift amount must be in [1,32]
3279     if (Val < 1 || Val > 32) {
3280       Error(E, "'asr' shift amount must be in range [1,32]");
3281       return MatchOperand_ParseFail;
3282     }
3283     // asr #32 encoded as asr #0, but is not allowed in Thumb2 mode.
3284     if (isThumb() && Val == 32) {
3285       Error(E, "'asr #32' shift amount not allowed in Thumb mode");
3286       return MatchOperand_ParseFail;
3287     }
3288     if (Val == 32) Val = 0;
3289   } else {
3290     // Shift amount must be in [1,32]
3291     if (Val < 0 || Val > 31) {
3292       Error(E, "'lsr' shift amount must be in range [0,31]");
3293       return MatchOperand_ParseFail;
3294     }
3295   }
3296
3297   E = Parser.getTok().getLoc();
3298   Operands.push_back(ARMOperand::CreateShifterImm(isASR, Val, S, E));
3299
3300   return MatchOperand_Success;
3301 }
3302
3303 /// parseRotImm - Parse the shifter immediate operand for SXTB/UXTB family
3304 /// of instructions. Legal values are:
3305 ///     ror #n  'n' in {0, 8, 16, 24}
3306 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3307 parseRotImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3308   const AsmToken &Tok = Parser.getTok();
3309   SMLoc S = Tok.getLoc();
3310   if (Tok.isNot(AsmToken::Identifier))
3311     return MatchOperand_NoMatch;
3312   StringRef ShiftName = Tok.getString();
3313   if (ShiftName != "ror" && ShiftName != "ROR")
3314     return MatchOperand_NoMatch;
3315   Parser.Lex(); // Eat the operator.
3316
3317   // A '#' and a rotate amount.
3318   if (Parser.getTok().isNot(AsmToken::Hash) &&
3319       Parser.getTok().isNot(AsmToken::Dollar)) {
3320     Error(Parser.getTok().getLoc(), "'#' expected");
3321     return MatchOperand_ParseFail;
3322   }
3323   Parser.Lex(); // Eat hash token.
3324
3325   const MCExpr *ShiftAmount;
3326   SMLoc E = Parser.getTok().getLoc();
3327   if (getParser().ParseExpression(ShiftAmount)) {
3328     Error(E, "malformed rotate expression");
3329     return MatchOperand_ParseFail;
3330   }
3331   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
3332   if (!CE) {
3333     Error(E, "rotate amount must be an immediate");
3334     return MatchOperand_ParseFail;
3335   }
3336
3337   int64_t Val = CE->getValue();
3338   // Shift amount must be in {0, 8, 16, 24} (0 is undocumented extension)
3339   // normally, zero is represented in asm by omitting the rotate operand
3340   // entirely.
3341   if (Val != 8 && Val != 16 && Val != 24 && Val != 0) {
3342     Error(E, "'ror' rotate amount must be 8, 16, or 24");
3343     return MatchOperand_ParseFail;
3344   }
3345
3346   E = Parser.getTok().getLoc();
3347   Operands.push_back(ARMOperand::CreateRotImm(Val, S, E));
3348
3349   return MatchOperand_Success;
3350 }
3351
3352 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3353 parseBitfield(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3354   SMLoc S = Parser.getTok().getLoc();
3355   // The bitfield descriptor is really two operands, the LSB and the width.
3356   if (Parser.getTok().isNot(AsmToken::Hash) &&
3357       Parser.getTok().isNot(AsmToken::Dollar)) {
3358     Error(Parser.getTok().getLoc(), "'#' expected");
3359     return MatchOperand_ParseFail;
3360   }
3361   Parser.Lex(); // Eat hash token.
3362
3363   const MCExpr *LSBExpr;
3364   SMLoc E = Parser.getTok().getLoc();
3365   if (getParser().ParseExpression(LSBExpr)) {
3366     Error(E, "malformed immediate expression");
3367     return MatchOperand_ParseFail;
3368   }
3369   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LSBExpr);
3370   if (!CE) {
3371     Error(E, "'lsb' operand must be an immediate");
3372     return MatchOperand_ParseFail;
3373   }
3374
3375   int64_t LSB = CE->getValue();
3376   // The LSB must be in the range [0,31]
3377   if (LSB < 0 || LSB > 31) {
3378     Error(E, "'lsb' operand must be in the range [0,31]");
3379     return MatchOperand_ParseFail;
3380   }
3381   E = Parser.getTok().getLoc();
3382
3383   // Expect another immediate operand.
3384   if (Parser.getTok().isNot(AsmToken::Comma)) {
3385     Error(Parser.getTok().getLoc(), "too few operands");
3386     return MatchOperand_ParseFail;
3387   }
3388   Parser.Lex(); // Eat hash token.
3389   if (Parser.getTok().isNot(AsmToken::Hash) &&
3390       Parser.getTok().isNot(AsmToken::Dollar)) {
3391     Error(Parser.getTok().getLoc(), "'#' expected");
3392     return MatchOperand_ParseFail;
3393   }
3394   Parser.Lex(); // Eat hash token.
3395
3396   const MCExpr *WidthExpr;
3397   if (getParser().ParseExpression(WidthExpr)) {
3398     Error(E, "malformed immediate expression");
3399     return MatchOperand_ParseFail;
3400   }
3401   CE = dyn_cast<MCConstantExpr>(WidthExpr);
3402   if (!CE) {
3403     Error(E, "'width' operand must be an immediate");
3404     return MatchOperand_ParseFail;
3405   }
3406
3407   int64_t Width = CE->getValue();
3408   // The LSB must be in the range [1,32-lsb]
3409   if (Width < 1 || Width > 32 - LSB) {
3410     Error(E, "'width' operand must be in the range [1,32-lsb]");
3411     return MatchOperand_ParseFail;
3412   }
3413   E = Parser.getTok().getLoc();
3414
3415   Operands.push_back(ARMOperand::CreateBitfield(LSB, Width, S, E));
3416
3417   return MatchOperand_Success;
3418 }
3419
3420 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3421 parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3422   // Check for a post-index addressing register operand. Specifically:
3423   // postidx_reg := '+' register {, shift}
3424   //              | '-' register {, shift}
3425   //              | register {, shift}
3426
3427   // This method must return MatchOperand_NoMatch without consuming any tokens
3428   // in the case where there is no match, as other alternatives take other
3429   // parse methods.
3430   AsmToken Tok = Parser.getTok();
3431   SMLoc S = Tok.getLoc();
3432   bool haveEaten = false;
3433   bool isAdd = true;
3434   int Reg = -1;
3435   if (Tok.is(AsmToken::Plus)) {
3436     Parser.Lex(); // Eat the '+' token.
3437     haveEaten = true;
3438   } else if (Tok.is(AsmToken::Minus)) {
3439     Parser.Lex(); // Eat the '-' token.
3440     isAdd = false;
3441     haveEaten = true;
3442   }
3443   if (Parser.getTok().is(AsmToken::Identifier))
3444     Reg = tryParseRegister();
3445   if (Reg == -1) {
3446     if (!haveEaten)
3447       return MatchOperand_NoMatch;
3448     Error(Parser.getTok().getLoc(), "register expected");
3449     return MatchOperand_ParseFail;
3450   }
3451   SMLoc E = Parser.getTok().getLoc();
3452
3453   ARM_AM::ShiftOpc ShiftTy = ARM_AM::no_shift;
3454   unsigned ShiftImm = 0;
3455   if (Parser.getTok().is(AsmToken::Comma)) {
3456     Parser.Lex(); // Eat the ','.
3457     if (parseMemRegOffsetShift(ShiftTy, ShiftImm))
3458       return MatchOperand_ParseFail;
3459   }
3460
3461   Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ShiftTy,
3462                                                   ShiftImm, S, E));
3463
3464   return MatchOperand_Success;
3465 }
3466
3467 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3468 parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3469   // Check for a post-index addressing register operand. Specifically:
3470   // am3offset := '+' register
3471   //              | '-' register
3472   //              | register
3473   //              | # imm
3474   //              | # + imm
3475   //              | # - imm
3476
3477   // This method must return MatchOperand_NoMatch without consuming any tokens
3478   // in the case where there is no match, as other alternatives take other
3479   // parse methods.
3480   AsmToken Tok = Parser.getTok();
3481   SMLoc S = Tok.getLoc();
3482
3483   // Do immediates first, as we always parse those if we have a '#'.
3484   if (Parser.getTok().is(AsmToken::Hash) ||
3485       Parser.getTok().is(AsmToken::Dollar)) {
3486     Parser.Lex(); // Eat the '#'.
3487     // Explicitly look for a '-', as we need to encode negative zero
3488     // differently.
3489     bool isNegative = Parser.getTok().is(AsmToken::Minus);
3490     const MCExpr *Offset;
3491     if (getParser().ParseExpression(Offset))
3492       return MatchOperand_ParseFail;
3493     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
3494     if (!CE) {
3495       Error(S, "constant expression expected");
3496       return MatchOperand_ParseFail;
3497     }
3498     SMLoc E = Tok.getLoc();
3499     // Negative zero is encoded as the flag value INT32_MIN.
3500     int32_t Val = CE->getValue();
3501     if (isNegative && Val == 0)
3502       Val = INT32_MIN;
3503
3504     Operands.push_back(
3505       ARMOperand::CreateImm(MCConstantExpr::Create(Val, getContext()), S, E));
3506
3507     return MatchOperand_Success;
3508   }
3509
3510
3511   bool haveEaten = false;
3512   bool isAdd = true;
3513   int Reg = -1;
3514   if (Tok.is(AsmToken::Plus)) {
3515     Parser.Lex(); // Eat the '+' token.
3516     haveEaten = true;
3517   } else if (Tok.is(AsmToken::Minus)) {
3518     Parser.Lex(); // Eat the '-' token.
3519     isAdd = false;
3520     haveEaten = true;
3521   }
3522   if (Parser.getTok().is(AsmToken::Identifier))
3523     Reg = tryParseRegister();
3524   if (Reg == -1) {
3525     if (!haveEaten)
3526       return MatchOperand_NoMatch;
3527     Error(Parser.getTok().getLoc(), "register expected");
3528     return MatchOperand_ParseFail;
3529   }
3530   SMLoc E = Parser.getTok().getLoc();
3531
3532   Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ARM_AM::no_shift,
3533                                                   0, S, E));
3534
3535   return MatchOperand_Success;
3536 }
3537
3538 /// cvtT2LdrdPre - Convert parsed operands to MCInst.
3539 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3540 /// when they refer multiple MIOperands inside a single one.
3541 bool ARMAsmParser::
3542 cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
3543              const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3544   // Rt, Rt2
3545   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3546   ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3547   // Create a writeback register dummy placeholder.
3548   Inst.addOperand(MCOperand::CreateReg(0));
3549   // addr
3550   ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
3551   // pred
3552   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3553   return true;
3554 }
3555
3556 /// cvtT2StrdPre - Convert parsed operands to MCInst.
3557 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3558 /// when they refer multiple MIOperands inside a single one.
3559 bool ARMAsmParser::
3560 cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
3561              const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3562   // Create a writeback register dummy placeholder.
3563   Inst.addOperand(MCOperand::CreateReg(0));
3564   // Rt, Rt2
3565   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3566   ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3567   // addr
3568   ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
3569   // pred
3570   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3571   return true;
3572 }
3573
3574 /// cvtLdWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
3575 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3576 /// when they refer multiple MIOperands inside a single one.
3577 bool ARMAsmParser::
3578 cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
3579                          const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3580   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3581
3582   // Create a writeback register dummy placeholder.
3583   Inst.addOperand(MCOperand::CreateImm(0));
3584
3585   ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
3586   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3587   return true;
3588 }
3589
3590 /// cvtStWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
3591 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3592 /// when they refer multiple MIOperands inside a single one.
3593 bool ARMAsmParser::
3594 cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
3595                          const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3596   // Create a writeback register dummy placeholder.
3597   Inst.addOperand(MCOperand::CreateImm(0));
3598   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3599   ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
3600   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3601   return true;
3602 }
3603
3604 /// cvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
3605 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3606 /// when they refer multiple MIOperands inside a single one.
3607 bool ARMAsmParser::
3608 cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
3609                          const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3610   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3611
3612   // Create a writeback register dummy placeholder.
3613   Inst.addOperand(MCOperand::CreateImm(0));
3614
3615   ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
3616   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3617   return true;
3618 }
3619
3620 /// cvtLdWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
3621 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3622 /// when they refer multiple MIOperands inside a single one.
3623 bool ARMAsmParser::
3624 cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
3625                          const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3626   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3627
3628   // Create a writeback register dummy placeholder.
3629   Inst.addOperand(MCOperand::CreateImm(0));
3630
3631   ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
3632   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3633   return true;
3634 }
3635
3636
3637 /// cvtStWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
3638 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3639 /// when they refer multiple MIOperands inside a single one.
3640 bool ARMAsmParser::
3641 cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
3642                          const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3643   // Create a writeback register dummy placeholder.
3644   Inst.addOperand(MCOperand::CreateImm(0));
3645   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3646   ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
3647   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3648   return true;
3649 }
3650
3651 /// cvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
3652 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3653 /// when they refer multiple MIOperands inside a single one.
3654 bool ARMAsmParser::
3655 cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
3656                          const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3657   // Create a writeback register dummy placeholder.
3658   Inst.addOperand(MCOperand::CreateImm(0));
3659   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3660   ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
3661   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3662   return true;
3663 }
3664
3665 /// cvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
3666 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3667 /// when they refer multiple MIOperands inside a single one.
3668 bool ARMAsmParser::
3669 cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
3670                          const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3671   // Create a writeback register dummy placeholder.
3672   Inst.addOperand(MCOperand::CreateImm(0));
3673   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3674   ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
3675   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3676   return true;
3677 }
3678
3679 /// cvtLdExtTWriteBackImm - Convert parsed operands to MCInst.
3680 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3681 /// when they refer multiple MIOperands inside a single one.
3682 bool ARMAsmParser::
3683 cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
3684                       const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3685   // Rt
3686   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3687   // Create a writeback register dummy placeholder.
3688   Inst.addOperand(MCOperand::CreateImm(0));
3689   // addr
3690   ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3691   // offset
3692   ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
3693   // pred
3694   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3695   return true;
3696 }
3697
3698 /// cvtLdExtTWriteBackReg - Convert parsed operands to MCInst.
3699 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3700 /// when they refer multiple MIOperands inside a single one.
3701 bool ARMAsmParser::
3702 cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
3703                       const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3704   // Rt
3705   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3706   // Create a writeback register dummy placeholder.
3707   Inst.addOperand(MCOperand::CreateImm(0));
3708   // addr
3709   ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3710   // offset
3711   ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
3712   // pred
3713   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3714   return true;
3715 }
3716
3717 /// cvtStExtTWriteBackImm - Convert parsed operands to MCInst.
3718 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3719 /// when they refer multiple MIOperands inside a single one.
3720 bool ARMAsmParser::
3721 cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
3722                       const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3723   // Create a writeback register dummy placeholder.
3724   Inst.addOperand(MCOperand::CreateImm(0));
3725   // Rt
3726   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3727   // addr
3728   ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3729   // offset
3730   ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
3731   // pred
3732   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3733   return true;
3734 }
3735
3736 /// cvtStExtTWriteBackReg - Convert parsed operands to MCInst.
3737 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3738 /// when they refer multiple MIOperands inside a single one.
3739 bool ARMAsmParser::
3740 cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
3741                       const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3742   // Create a writeback register dummy placeholder.
3743   Inst.addOperand(MCOperand::CreateImm(0));
3744   // Rt
3745   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3746   // addr
3747   ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3748   // offset
3749   ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
3750   // pred
3751   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3752   return true;
3753 }
3754
3755 /// cvtLdrdPre - Convert parsed operands to MCInst.
3756 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3757 /// when they refer multiple MIOperands inside a single one.
3758 bool ARMAsmParser::
3759 cvtLdrdPre(MCInst &Inst, unsigned Opcode,
3760            const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3761   // Rt, Rt2
3762   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3763   ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3764   // Create a writeback register dummy placeholder.
3765   Inst.addOperand(MCOperand::CreateImm(0));
3766   // addr
3767   ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
3768   // pred
3769   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3770   return true;
3771 }
3772
3773 /// cvtStrdPre - Convert parsed operands to MCInst.
3774 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3775 /// when they refer multiple MIOperands inside a single one.
3776 bool ARMAsmParser::
3777 cvtStrdPre(MCInst &Inst, unsigned Opcode,
3778            const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3779   // Create a writeback register dummy placeholder.
3780   Inst.addOperand(MCOperand::CreateImm(0));
3781   // Rt, Rt2
3782   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3783   ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3784   // addr
3785   ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
3786   // pred
3787   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3788   return true;
3789 }
3790
3791 /// cvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
3792 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3793 /// when they refer multiple MIOperands inside a single one.
3794 bool ARMAsmParser::
3795 cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
3796                          const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3797   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3798   // Create a writeback register dummy placeholder.
3799   Inst.addOperand(MCOperand::CreateImm(0));
3800   ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
3801   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3802   return true;
3803 }
3804
3805 /// cvtThumbMultiple- Convert parsed operands to MCInst.
3806 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
3807 /// when they refer multiple MIOperands inside a single one.
3808 bool ARMAsmParser::
3809 cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
3810            const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3811   // The second source operand must be the same register as the destination
3812   // operand.
3813   if (Operands.size() == 6 &&
3814       (((ARMOperand*)Operands[3])->getReg() !=
3815        ((ARMOperand*)Operands[5])->getReg()) &&
3816       (((ARMOperand*)Operands[3])->getReg() !=
3817        ((ARMOperand*)Operands[4])->getReg())) {
3818     Error(Operands[3]->getStartLoc(),
3819           "destination register must match source register");
3820     return false;
3821   }
3822   ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3823   ((ARMOperand*)Operands[1])->addCCOutOperands(Inst, 1);
3824   // If we have a three-operand form, make sure to set Rn to be the operand
3825   // that isn't the same as Rd.
3826   unsigned RegOp = 4;
3827   if (Operands.size() == 6 &&
3828       ((ARMOperand*)Operands[4])->getReg() ==
3829         ((ARMOperand*)Operands[3])->getReg())
3830     RegOp = 5;
3831   ((ARMOperand*)Operands[RegOp])->addRegOperands(Inst, 1);
3832   Inst.addOperand(Inst.getOperand(0));
3833   ((ARMOperand*)Operands[2])->addCondCodeOperands(Inst, 2);
3834
3835   return true;
3836 }
3837
3838 bool ARMAsmParser::
3839 cvtVLDwbFixed(MCInst &Inst, unsigned Opcode,
3840               const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3841   // Vd
3842   ((ARMOperand*)Operands[3])->addVecListOperands(Inst, 1);
3843   // Create a writeback register dummy placeholder.
3844   Inst.addOperand(MCOperand::CreateImm(0));
3845   // Vn
3846   ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3847   // pred
3848   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3849   return true;
3850 }
3851
3852 bool ARMAsmParser::
3853 cvtVLDwbRegister(MCInst &Inst, unsigned Opcode,
3854                  const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3855   // Vd
3856   ((ARMOperand*)Operands[3])->addVecListOperands(Inst, 1);
3857   // Create a writeback register dummy placeholder.
3858   Inst.addOperand(MCOperand::CreateImm(0));
3859   // Vn
3860   ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3861   // Vm
3862   ((ARMOperand*)Operands[5])->addRegOperands(Inst, 1);
3863   // pred
3864   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3865   return true;
3866 }
3867
3868 bool ARMAsmParser::
3869 cvtVSTwbFixed(MCInst &Inst, unsigned Opcode,
3870               const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3871   // Create a writeback register dummy placeholder.
3872   Inst.addOperand(MCOperand::CreateImm(0));
3873   // Vn
3874   ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3875   // Vt
3876   ((ARMOperand*)Operands[3])->addVecListOperands(Inst, 1);
3877   // pred
3878   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3879   return true;
3880 }
3881
3882 bool ARMAsmParser::
3883 cvtVSTwbRegister(MCInst &Inst, unsigned Opcode,
3884                  const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3885   // Create a writeback register dummy placeholder.
3886   Inst.addOperand(MCOperand::CreateImm(0));
3887   // Vn
3888   ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3889   // Vm
3890   ((ARMOperand*)Operands[5])->addRegOperands(Inst, 1);
3891   // Vt
3892   ((ARMOperand*)Operands[3])->addVecListOperands(Inst, 1);
3893   // pred
3894   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3895   return true;
3896 }
3897
3898 /// Parse an ARM memory expression, return false if successful else return true
3899 /// or an error.  The first token must be a '[' when called.
3900 bool ARMAsmParser::
3901 parseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3902   SMLoc S, E;
3903   assert(Parser.getTok().is(AsmToken::LBrac) &&
3904          "Token is not a Left Bracket");
3905   S = Parser.getTok().getLoc();
3906   Parser.Lex(); // Eat left bracket token.
3907
3908   const AsmToken &BaseRegTok = Parser.getTok();
3909   int BaseRegNum = tryParseRegister();
3910   if (BaseRegNum == -1)
3911     return Error(BaseRegTok.getLoc(), "register expected");
3912
3913   // The next token must either be a comma or a closing bracket.
3914   const AsmToken &Tok = Parser.getTok();
3915   if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
3916     return Error(Tok.getLoc(), "malformed memory operand");
3917
3918   if (Tok.is(AsmToken::RBrac)) {
3919     E = Tok.getLoc();
3920     Parser.Lex(); // Eat right bracket token.
3921
3922     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0, ARM_AM::no_shift,
3923                                              0, 0, false, S, E));
3924
3925     // If there's a pre-indexing writeback marker, '!', just add it as a token
3926     // operand. It's rather odd, but syntactically valid.
3927     if (Parser.getTok().is(AsmToken::Exclaim)) {
3928       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3929       Parser.Lex(); // Eat the '!'.
3930     }
3931
3932     return false;
3933   }
3934
3935   assert(Tok.is(AsmToken::Comma) && "Lost comma in memory operand?!");
3936   Parser.Lex(); // Eat the comma.
3937
3938   // If we have a ':', it's an alignment specifier.
3939   if (Parser.getTok().is(AsmToken::Colon)) {
3940     Parser.Lex(); // Eat the ':'.
3941     E = Parser.getTok().getLoc();
3942
3943     const MCExpr *Expr;
3944     if (getParser().ParseExpression(Expr))
3945      return true;
3946
3947     // The expression has to be a constant. Memory references with relocations
3948     // don't come through here, as they use the <label> forms of the relevant
3949     // instructions.
3950     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3951     if (!CE)
3952       return Error (E, "constant expression expected");
3953
3954     unsigned Align = 0;
3955     switch (CE->getValue()) {
3956     default:
3957       return Error(E,
3958                    "alignment specifier must be 16, 32, 64, 128, or 256 bits");
3959     case 16:  Align = 2; break;
3960     case 32:  Align = 4; break;
3961     case 64:  Align = 8; break;
3962     case 128: Align = 16; break;
3963     case 256: Align = 32; break;
3964     }
3965
3966     // Now we should have the closing ']'
3967     E = Parser.getTok().getLoc();
3968     if (Parser.getTok().isNot(AsmToken::RBrac))
3969       return Error(E, "']' expected");
3970     Parser.Lex(); // Eat right bracket token.
3971
3972     // Don't worry about range checking the value here. That's handled by
3973     // the is*() predicates.
3974     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0,
3975                                              ARM_AM::no_shift, 0, Align,
3976                                              false, S, E));
3977
3978     // If there's a pre-indexing writeback marker, '!', just add it as a token
3979     // operand.
3980     if (Parser.getTok().is(AsmToken::Exclaim)) {
3981       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3982       Parser.Lex(); // Eat the '!'.
3983     }
3984
3985     return false;
3986   }
3987
3988   // If we have a '#', it's an immediate offset, else assume it's a register
3989   // offset. Be friendly and also accept a plain integer (without a leading
3990   // hash) for gas compatibility.
3991   if (Parser.getTok().is(AsmToken::Hash) ||
3992       Parser.getTok().is(AsmToken::Dollar) ||
3993       Parser.getTok().is(AsmToken::Integer)) {
3994     if (Parser.getTok().isNot(AsmToken::Integer))
3995       Parser.Lex(); // Eat the '#'.
3996     E = Parser.getTok().getLoc();
3997
3998     bool isNegative = getParser().getTok().is(AsmToken::Minus);
3999     const MCExpr *Offset;
4000     if (getParser().ParseExpression(Offset))
4001      return true;
4002
4003     // The expression has to be a constant. Memory references with relocations
4004     // don't come through here, as they use the <label> forms of the relevant
4005     // instructions.
4006     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
4007     if (!CE)
4008       return Error (E, "constant expression expected");
4009
4010     // If the constant was #-0, represent it as INT32_MIN.
4011     int32_t Val = CE->getValue();
4012     if (isNegative && Val == 0)
4013       CE = MCConstantExpr::Create(INT32_MIN, getContext());
4014
4015     // Now we should have the closing ']'
4016     E = Parser.getTok().getLoc();
4017     if (Parser.getTok().isNot(AsmToken::RBrac))
4018       return Error(E, "']' expected");
4019     Parser.Lex(); // Eat right bracket token.
4020
4021     // Don't worry about range checking the value here. That's handled by
4022     // the is*() predicates.
4023     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, CE, 0,
4024                                              ARM_AM::no_shift, 0, 0,
4025                                              false, S, E));
4026
4027     // If there's a pre-indexing writeback marker, '!', just add it as a token
4028     // operand.
4029     if (Parser.getTok().is(AsmToken::Exclaim)) {
4030       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4031       Parser.Lex(); // Eat the '!'.
4032     }
4033
4034     return false;
4035   }
4036
4037   // The register offset is optionally preceded by a '+' or '-'
4038   bool isNegative = false;
4039   if (Parser.getTok().is(AsmToken::Minus)) {
4040     isNegative = true;
4041     Parser.Lex(); // Eat the '-'.
4042   } else if (Parser.getTok().is(AsmToken::Plus)) {
4043     // Nothing to do.
4044     Parser.Lex(); // Eat the '+'.
4045   }
4046
4047   E = Parser.getTok().getLoc();
4048   int OffsetRegNum = tryParseRegister();
4049   if (OffsetRegNum == -1)
4050     return Error(E, "register expected");
4051
4052   // If there's a shift operator, handle it.
4053   ARM_AM::ShiftOpc ShiftType = ARM_AM::no_shift;
4054   unsigned ShiftImm = 0;
4055   if (Parser.getTok().is(AsmToken::Comma)) {
4056     Parser.Lex(); // Eat the ','.
4057     if (parseMemRegOffsetShift(ShiftType, ShiftImm))
4058       return true;
4059   }
4060
4061   // Now we should have the closing ']'
4062   E = Parser.getTok().getLoc();
4063   if (Parser.getTok().isNot(AsmToken::RBrac))
4064     return Error(E, "']' expected");
4065   Parser.Lex(); // Eat right bracket token.
4066
4067   Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, OffsetRegNum,
4068                                            ShiftType, ShiftImm, 0, isNegative,
4069                                            S, E));
4070
4071   // If there's a pre-indexing writeback marker, '!', just add it as a token
4072   // operand.
4073   if (Parser.getTok().is(AsmToken::Exclaim)) {
4074     Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
4075     Parser.Lex(); // Eat the '!'.
4076   }
4077
4078   return false;
4079 }
4080
4081 /// parseMemRegOffsetShift - one of these two:
4082 ///   ( lsl | lsr | asr | ror ) , # shift_amount
4083 ///   rrx
4084 /// return true if it parses a shift otherwise it returns false.
4085 bool ARMAsmParser::parseMemRegOffsetShift(ARM_AM::ShiftOpc &St,
4086                                           unsigned &Amount) {
4087   SMLoc Loc = Parser.getTok().getLoc();
4088   const AsmToken &Tok = Parser.getTok();
4089   if (Tok.isNot(AsmToken::Identifier))
4090     return true;
4091   StringRef ShiftName = Tok.getString();
4092   if (ShiftName == "lsl" || ShiftName == "LSL" ||
4093       ShiftName == "asl" || ShiftName == "ASL")
4094     St = ARM_AM::lsl;
4095   else if (ShiftName == "lsr" || ShiftName == "LSR")
4096     St = ARM_AM::lsr;
4097   else if (ShiftName == "asr" || ShiftName == "ASR")
4098     St = ARM_AM::asr;
4099   else if (ShiftName == "ror" || ShiftName == "ROR")
4100     St = ARM_AM::ror;
4101   else if (ShiftName == "rrx" || ShiftName == "RRX")
4102     St = ARM_AM::rrx;
4103   else
4104     return Error(Loc, "illegal shift operator");
4105   Parser.Lex(); // Eat shift type token.
4106
4107   // rrx stands alone.
4108   Amount = 0;
4109   if (St != ARM_AM::rrx) {
4110     Loc = Parser.getTok().getLoc();
4111     // A '#' and a shift amount.
4112     const AsmToken &HashTok = Parser.getTok();
4113     if (HashTok.isNot(AsmToken::Hash) &&
4114         HashTok.isNot(AsmToken::Dollar))
4115       return Error(HashTok.getLoc(), "'#' expected");
4116     Parser.Lex(); // Eat hash token.
4117
4118     const MCExpr *Expr;
4119     if (getParser().ParseExpression(Expr))
4120       return true;
4121     // Range check the immediate.
4122     // lsl, ror: 0 <= imm <= 31
4123     // lsr, asr: 0 <= imm <= 32
4124     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
4125     if (!CE)
4126       return Error(Loc, "shift amount must be an immediate");
4127     int64_t Imm = CE->getValue();
4128     if (Imm < 0 ||
4129         ((St == ARM_AM::lsl || St == ARM_AM::ror) && Imm > 31) ||
4130         ((St == ARM_AM::lsr || St == ARM_AM::asr) && Imm > 32))
4131       return Error(Loc, "immediate shift value out of range");
4132     Amount = Imm;
4133   }
4134
4135   return false;
4136 }
4137
4138 /// parseFPImm - A floating point immediate expression operand.
4139 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
4140 parseFPImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4141   SMLoc S = Parser.getTok().getLoc();
4142
4143   if (Parser.getTok().isNot(AsmToken::Hash) &&
4144       Parser.getTok().isNot(AsmToken::Dollar))
4145     return MatchOperand_NoMatch;
4146
4147   // Disambiguate the VMOV forms that can accept an FP immediate.
4148   // vmov.f32 <sreg>, #imm
4149   // vmov.f64 <dreg>, #imm
4150   // vmov.f32 <dreg>, #imm  @ vector f32x2
4151   // vmov.f32 <qreg>, #imm  @ vector f32x4
4152   //
4153   // There are also the NEON VMOV instructions which expect an
4154   // integer constant. Make sure we don't try to parse an FPImm
4155   // for these:
4156   // vmov.i{8|16|32|64} <dreg|qreg>, #imm
4157   ARMOperand *TyOp = static_cast<ARMOperand*>(Operands[2]);
4158   if (!TyOp->isToken() || (TyOp->getToken() != ".f32" &&
4159                            TyOp->getToken() != ".f64"))
4160     return MatchOperand_NoMatch;
4161
4162   Parser.Lex(); // Eat the '#'.
4163
4164   // Handle negation, as that still comes through as a separate token.
4165   bool isNegative = false;
4166   if (Parser.getTok().is(AsmToken::Minus)) {
4167     isNegative = true;
4168     Parser.Lex();
4169   }
4170   const AsmToken &Tok = Parser.getTok();
4171   if (Tok.is(AsmToken::Real)) {
4172     APFloat RealVal(APFloat::IEEEdouble, Tok.getString());
4173     uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
4174     // If we had a '-' in front, toggle the sign bit.
4175     IntVal ^= (uint64_t)isNegative << 63;
4176     int Val = ARM_AM::getFP64Imm(APInt(64, IntVal));
4177     Parser.Lex(); // Eat the token.
4178     if (Val == -1) {
4179       TokError("floating point value out of range");
4180       return MatchOperand_ParseFail;
4181     }
4182     Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
4183     return MatchOperand_Success;
4184   }
4185   if (Tok.is(AsmToken::Integer)) {
4186     int64_t Val = Tok.getIntVal();
4187     Parser.Lex(); // Eat the token.
4188     if (Val > 255 || Val < 0) {
4189       TokError("encoded floating point value out of range");
4190       return MatchOperand_ParseFail;
4191     }
4192     Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
4193     return MatchOperand_Success;
4194   }
4195
4196   TokError("invalid floating point immediate");
4197   return MatchOperand_ParseFail;
4198 }
4199 /// Parse a arm instruction operand.  For now this parses the operand regardless
4200 /// of the mnemonic.
4201 bool ARMAsmParser::parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
4202                                 StringRef Mnemonic) {
4203   SMLoc S, E;
4204
4205   // Check if the current operand has a custom associated parser, if so, try to
4206   // custom parse the operand, or fallback to the general approach.
4207   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
4208   if (ResTy == MatchOperand_Success)
4209     return false;
4210   // If there wasn't a custom match, try the generic matcher below. Otherwise,
4211   // there was a match, but an error occurred, in which case, just return that
4212   // the operand parsing failed.
4213   if (ResTy == MatchOperand_ParseFail)
4214     return true;
4215
4216   switch (getLexer().getKind()) {
4217   default:
4218     Error(Parser.getTok().getLoc(), "unexpected token in operand");
4219     return true;
4220   case AsmToken::Identifier: {
4221     // If this is VMRS, check for the apsr_nzcv operand.
4222     if (!tryParseRegisterWithWriteBack(Operands))
4223       return false;
4224     int Res = tryParseShiftRegister(Operands);
4225     if (Res == 0) // success
4226       return false;
4227     else if (Res == -1) // irrecoverable error
4228       return true;
4229     if (Mnemonic == "vmrs" && Parser.getTok().getString() == "apsr_nzcv") {
4230       S = Parser.getTok().getLoc();
4231       Parser.Lex();
4232       Operands.push_back(ARMOperand::CreateToken("apsr_nzcv", S));
4233       return false;
4234     }
4235
4236     // Fall though for the Identifier case that is not a register or a
4237     // special name.
4238   }
4239   case AsmToken::LParen:  // parenthesized expressions like (_strcmp-4)
4240   case AsmToken::Integer: // things like 1f and 2b as a branch targets
4241   case AsmToken::String:  // quoted label names.
4242   case AsmToken::Dot: {   // . as a branch target
4243     // This was not a register so parse other operands that start with an
4244     // identifier (like labels) as expressions and create them as immediates.
4245     const MCExpr *IdVal;
4246     S = Parser.getTok().getLoc();
4247     if (getParser().ParseExpression(IdVal))
4248       return true;
4249     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
4250     Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
4251     return false;
4252   }
4253   case AsmToken::LBrac:
4254     return parseMemory(Operands);
4255   case AsmToken::LCurly:
4256     return parseRegisterList(Operands);
4257   case AsmToken::Dollar:
4258   case AsmToken::Hash: {
4259     // #42 -> immediate.
4260     // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
4261     S = Parser.getTok().getLoc();
4262     Parser.Lex();
4263     bool isNegative = Parser.getTok().is(AsmToken::Minus);
4264     const MCExpr *ImmVal;
4265     if (getParser().ParseExpression(ImmVal))
4266       return true;
4267     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmVal);
4268     if (CE) {
4269       int32_t Val = CE->getValue();
4270       if (isNegative && Val == 0)
4271         ImmVal = MCConstantExpr::Create(INT32_MIN, getContext());
4272     }
4273     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
4274     Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
4275     return false;
4276   }
4277   case AsmToken::Colon: {
4278     // ":lower16:" and ":upper16:" expression prefixes
4279     // FIXME: Check it's an expression prefix,
4280     // e.g. (FOO - :lower16:BAR) isn't legal.
4281     ARMMCExpr::VariantKind RefKind;
4282     if (parsePrefix(RefKind))
4283       return true;
4284
4285     const MCExpr *SubExprVal;
4286     if (getParser().ParseExpression(SubExprVal))
4287       return true;
4288
4289     const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
4290                                                    getContext());
4291     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
4292     Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
4293     return false;
4294   }
4295   }
4296 }
4297
4298 // parsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
4299 //  :lower16: and :upper16:.
4300 bool ARMAsmParser::parsePrefix(ARMMCExpr::VariantKind &RefKind) {
4301   RefKind = ARMMCExpr::VK_ARM_None;
4302
4303   // :lower16: and :upper16: modifiers
4304   assert(getLexer().is(AsmToken::Colon) && "expected a :");
4305   Parser.Lex(); // Eat ':'
4306
4307   if (getLexer().isNot(AsmToken::Identifier)) {
4308     Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
4309     return true;
4310   }
4311
4312   StringRef IDVal = Parser.getTok().getIdentifier();
4313   if (IDVal == "lower16") {
4314     RefKind = ARMMCExpr::VK_ARM_LO16;
4315   } else if (IDVal == "upper16") {
4316     RefKind = ARMMCExpr::VK_ARM_HI16;
4317   } else {
4318     Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
4319     return true;
4320   }
4321   Parser.Lex();
4322
4323   if (getLexer().isNot(AsmToken::Colon)) {
4324     Error(Parser.getTok().getLoc(), "unexpected token after prefix");
4325     return true;
4326   }
4327   Parser.Lex(); // Eat the last ':'
4328   return false;
4329 }
4330
4331 /// \brief Given a mnemonic, split out possible predication code and carry
4332 /// setting letters to form a canonical mnemonic and flags.
4333 //
4334 // FIXME: Would be nice to autogen this.
4335 // FIXME: This is a bit of a maze of special cases.
4336 StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic,
4337                                       unsigned &PredicationCode,
4338                                       bool &CarrySetting,
4339                                       unsigned &ProcessorIMod,
4340                                       StringRef &ITMask) {
4341   PredicationCode = ARMCC::AL;
4342   CarrySetting = false;
4343   ProcessorIMod = 0;
4344
4345   // Ignore some mnemonics we know aren't predicated forms.
4346   //
4347   // FIXME: Would be nice to autogen this.
4348   if ((Mnemonic == "movs" && isThumb()) ||
4349       Mnemonic == "teq"   || Mnemonic == "vceq"   || Mnemonic == "svc"   ||
4350       Mnemonic == "mls"   || Mnemonic == "smmls"  || Mnemonic == "vcls"  ||
4351       Mnemonic == "vmls"  || Mnemonic == "vnmls"  || Mnemonic == "vacge" ||
4352       Mnemonic == "vcge"  || Mnemonic == "vclt"   || Mnemonic == "vacgt" ||
4353       Mnemonic == "vcgt"  || Mnemonic == "vcle"   || Mnemonic == "smlal" ||
4354       Mnemonic == "umaal" || Mnemonic == "umlal"  || Mnemonic == "vabal" ||
4355       Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal" ||
4356       Mnemonic == "fmuls")
4357     return Mnemonic;
4358
4359   // First, split out any predication code. Ignore mnemonics we know aren't
4360   // predicated but do have a carry-set and so weren't caught above.
4361   if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
4362       Mnemonic != "muls" && Mnemonic != "smlals" && Mnemonic != "smulls" &&
4363       Mnemonic != "umlals" && Mnemonic != "umulls" && Mnemonic != "lsls" &&
4364       Mnemonic != "sbcs" && Mnemonic != "rscs") {
4365     unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
4366       .Case("eq", ARMCC::EQ)
4367       .Case("ne", ARMCC::NE)
4368       .Case("hs", ARMCC::HS)
4369       .Case("cs", ARMCC::HS)
4370       .Case("lo", ARMCC::LO)
4371       .Case("cc", ARMCC::LO)
4372       .Case("mi", ARMCC::MI)
4373       .Case("pl", ARMCC::PL)
4374       .Case("vs", ARMCC::VS)
4375       .Case("vc", ARMCC::VC)
4376       .Case("hi", ARMCC::HI)
4377       .Case("ls", ARMCC::LS)
4378       .Case("ge", ARMCC::GE)
4379       .Case("lt", ARMCC::LT)
4380       .Case("gt", ARMCC::GT)
4381       .Case("le", ARMCC::LE)
4382       .Case("al", ARMCC::AL)
4383       .Default(~0U);
4384     if (CC != ~0U) {
4385       Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
4386       PredicationCode = CC;
4387     }
4388   }
4389
4390   // Next, determine if we have a carry setting bit. We explicitly ignore all
4391   // the instructions we know end in 's'.
4392   if (Mnemonic.endswith("s") &&
4393       !(Mnemonic == "cps" || Mnemonic == "mls" ||
4394         Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
4395         Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
4396         Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
4397         Mnemonic == "vrsqrts" || Mnemonic == "srs" || Mnemonic == "flds" ||
4398         Mnemonic == "fmrs" || Mnemonic == "fsqrts" || Mnemonic == "fsubs" ||
4399         Mnemonic == "fsts" || Mnemonic == "fcpys" || Mnemonic == "fdivs" ||
4400         Mnemonic == "fmuls" ||
4401         (Mnemonic == "movs" && isThumb()))) {
4402     Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
4403     CarrySetting = true;
4404   }
4405
4406   // The "cps" instruction can have a interrupt mode operand which is glued into
4407   // the mnemonic. Check if this is the case, split it and parse the imod op
4408   if (Mnemonic.startswith("cps")) {
4409     // Split out any imod code.
4410     unsigned IMod =
4411       StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
4412       .Case("ie", ARM_PROC::IE)
4413       .Case("id", ARM_PROC::ID)
4414       .Default(~0U);
4415     if (IMod != ~0U) {
4416       Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
4417       ProcessorIMod = IMod;
4418     }
4419   }
4420
4421   // The "it" instruction has the condition mask on the end of the mnemonic.
4422   if (Mnemonic.startswith("it")) {
4423     ITMask = Mnemonic.slice(2, Mnemonic.size());
4424     Mnemonic = Mnemonic.slice(0, 2);
4425   }
4426
4427   return Mnemonic;
4428 }
4429
4430 /// \brief Given a canonical mnemonic, determine if the instruction ever allows
4431 /// inclusion of carry set or predication code operands.
4432 //
4433 // FIXME: It would be nice to autogen this.
4434 void ARMAsmParser::
4435 getMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
4436                       bool &CanAcceptPredicationCode) {
4437   if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
4438       Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
4439       Mnemonic == "add" || Mnemonic == "adc" ||
4440       Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
4441       Mnemonic == "orr" || Mnemonic == "mvn" ||
4442       Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
4443       Mnemonic == "sbc" || Mnemonic == "eor" || Mnemonic == "neg" ||
4444       (!isThumb() && (Mnemonic == "smull" || Mnemonic == "mov" ||
4445                       Mnemonic == "mla" || Mnemonic == "smlal" ||
4446                       Mnemonic == "umlal" || Mnemonic == "umull"))) {
4447     CanAcceptCarrySet = true;
4448   } else
4449     CanAcceptCarrySet = false;
4450
4451   if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
4452       Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
4453       Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
4454       Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
4455       Mnemonic == "dsb" || Mnemonic == "isb" || Mnemonic == "setend" ||
4456       (Mnemonic == "clrex" && !isThumb()) ||
4457       (Mnemonic == "nop" && isThumbOne()) ||
4458       ((Mnemonic == "pld" || Mnemonic == "pli" || Mnemonic == "pldw" ||
4459         Mnemonic == "ldc2" || Mnemonic == "ldc2l" ||
4460         Mnemonic == "stc2" || Mnemonic == "stc2l") && !isThumb()) ||
4461       ((Mnemonic.startswith("rfe") || Mnemonic.startswith("srs")) &&
4462        !isThumb()) ||
4463       Mnemonic.startswith("cps") || (Mnemonic == "movs" && isThumbOne())) {
4464     CanAcceptPredicationCode = false;
4465   } else
4466     CanAcceptPredicationCode = true;
4467
4468   if (isThumb()) {
4469     if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
4470         Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
4471       CanAcceptPredicationCode = false;
4472   }
4473 }
4474
4475 bool ARMAsmParser::shouldOmitCCOutOperand(StringRef Mnemonic,
4476                                SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4477   // FIXME: This is all horribly hacky. We really need a better way to deal
4478   // with optional operands like this in the matcher table.
4479
4480   // The 'mov' mnemonic is special. One variant has a cc_out operand, while
4481   // another does not. Specifically, the MOVW instruction does not. So we
4482   // special case it here and remove the defaulted (non-setting) cc_out
4483   // operand if that's the instruction we're trying to match.
4484   //
4485   // We do this as post-processing of the explicit operands rather than just
4486   // conditionally adding the cc_out in the first place because we need
4487   // to check the type of the parsed immediate operand.
4488   if (Mnemonic == "mov" && Operands.size() > 4 && !isThumb() &&
4489       !static_cast<ARMOperand*>(Operands[4])->isARMSOImm() &&
4490       static_cast<ARMOperand*>(Operands[4])->isImm0_65535Expr() &&
4491       static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
4492     return true;
4493
4494   // Register-register 'add' for thumb does not have a cc_out operand
4495   // when there are only two register operands.
4496   if (isThumb() && Mnemonic == "add" && Operands.size() == 5 &&
4497       static_cast<ARMOperand*>(Operands[3])->isReg() &&
4498       static_cast<ARMOperand*>(Operands[4])->isReg() &&
4499       static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
4500     return true;
4501   // Register-register 'add' for thumb does not have a cc_out operand
4502   // when it's an ADD Rdm, SP, {Rdm|#imm0_255} instruction. We do
4503   // have to check the immediate range here since Thumb2 has a variant
4504   // that can handle a different range and has a cc_out operand.
4505   if (((isThumb() && Mnemonic == "add") ||
4506        (isThumbTwo() && Mnemonic == "sub")) &&
4507       Operands.size() == 6 &&
4508       static_cast<ARMOperand*>(Operands[3])->isReg() &&
4509       static_cast<ARMOperand*>(Operands[4])->isReg() &&
4510       static_cast<ARMOperand*>(Operands[4])->getReg() == ARM::SP &&
4511       static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
4512       (static_cast<ARMOperand*>(Operands[5])->isReg() ||
4513        static_cast<ARMOperand*>(Operands[5])->isImm0_1020s4()))
4514     return true;
4515   // For Thumb2, add/sub immediate does not have a cc_out operand for the
4516   // imm0_4095 variant. That's the least-preferred variant when
4517   // selecting via the generic "add" mnemonic, so to know that we
4518   // should remove the cc_out operand, we have to explicitly check that
4519   // it's not one of the other variants. Ugh.
4520   if (isThumbTwo() && (Mnemonic == "add" || Mnemonic == "sub") &&
4521       Operands.size() == 6 &&
4522       static_cast<ARMOperand*>(Operands[3])->isReg() &&
4523       static_cast<ARMOperand*>(Operands[4])->isReg() &&
4524       static_cast<ARMOperand*>(Operands[5])->isImm()) {
4525     // Nest conditions rather than one big 'if' statement for readability.
4526     //
4527     // If either register is a high reg, it's either one of the SP
4528     // variants (handled above) or a 32-bit encoding, so we just
4529     // check against T3.
4530     if ((!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
4531          !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg())) &&
4532         static_cast<ARMOperand*>(Operands[5])->isT2SOImm())
4533       return false;
4534     // If both registers are low, we're in an IT block, and the immediate is
4535     // in range, we should use encoding T1 instead, which has a cc_out.
4536     if (inITBlock() &&
4537         isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) &&
4538         isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) &&
4539         static_cast<ARMOperand*>(Operands[5])->isImm0_7())
4540       return false;
4541
4542     // Otherwise, we use encoding T4, which does not have a cc_out
4543     // operand.
4544     return true;
4545   }
4546
4547   // The thumb2 multiply instruction doesn't have a CCOut register, so
4548   // if we have a "mul" mnemonic in Thumb mode, check if we'll be able to
4549   // use the 16-bit encoding or not.
4550   if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 6 &&
4551       static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
4552       static_cast<ARMOperand*>(Operands[3])->isReg() &&
4553       static_cast<ARMOperand*>(Operands[4])->isReg() &&
4554       static_cast<ARMOperand*>(Operands[5])->isReg() &&
4555       // If the registers aren't low regs, the destination reg isn't the
4556       // same as one of the source regs, or the cc_out operand is zero
4557       // outside of an IT block, we have to use the 32-bit encoding, so
4558       // remove the cc_out operand.
4559       (!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
4560        !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) ||
4561        !isARMLowRegister(static_cast<ARMOperand*>(Operands[5])->getReg()) ||
4562        !inITBlock() ||
4563        (static_cast<ARMOperand*>(Operands[3])->getReg() !=
4564         static_cast<ARMOperand*>(Operands[5])->getReg() &&
4565         static_cast<ARMOperand*>(Operands[3])->getReg() !=
4566         static_cast<ARMOperand*>(Operands[4])->getReg())))
4567     return true;
4568
4569   // Also check the 'mul' syntax variant that doesn't specify an explicit
4570   // destination register.
4571   if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 5 &&
4572       static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
4573       static_cast<ARMOperand*>(Operands[3])->isReg() &&
4574       static_cast<ARMOperand*>(Operands[4])->isReg() &&
4575       // If the registers aren't low regs  or the cc_out operand is zero
4576       // outside of an IT block, we have to use the 32-bit encoding, so
4577       // remove the cc_out operand.
4578       (!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
4579        !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) ||
4580        !inITBlock()))
4581     return true;
4582
4583
4584
4585   // Register-register 'add/sub' for thumb does not have a cc_out operand
4586   // when it's an ADD/SUB SP, #imm. Be lenient on count since there's also
4587   // the "add/sub SP, SP, #imm" version. If the follow-up operands aren't
4588   // right, this will result in better diagnostics (which operand is off)
4589   // anyway.
4590   if (isThumb() && (Mnemonic == "add" || Mnemonic == "sub") &&
4591       (Operands.size() == 5 || Operands.size() == 6) &&
4592       static_cast<ARMOperand*>(Operands[3])->isReg() &&
4593       static_cast<ARMOperand*>(Operands[3])->getReg() == ARM::SP &&
4594       static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
4595     return true;
4596
4597   return false;
4598 }
4599
4600 static bool isDataTypeToken(StringRef Tok) {
4601   return Tok == ".8" || Tok == ".16" || Tok == ".32" || Tok == ".64" ||
4602     Tok == ".i8" || Tok == ".i16" || Tok == ".i32" || Tok == ".i64" ||
4603     Tok == ".u8" || Tok == ".u16" || Tok == ".u32" || Tok == ".u64" ||
4604     Tok == ".s8" || Tok == ".s16" || Tok == ".s32" || Tok == ".s64" ||
4605     Tok == ".p8" || Tok == ".p16" || Tok == ".f32" || Tok == ".f64" ||
4606     Tok == ".f" || Tok == ".d";
4607 }
4608
4609 // FIXME: This bit should probably be handled via an explicit match class
4610 // in the .td files that matches the suffix instead of having it be
4611 // a literal string token the way it is now.
4612 static bool doesIgnoreDataTypeSuffix(StringRef Mnemonic, StringRef DT) {
4613   return Mnemonic.startswith("vldm") || Mnemonic.startswith("vstm");
4614 }
4615
4616 static void applyMnemonicAliases(StringRef &Mnemonic, unsigned Features);
4617 /// Parse an arm instruction mnemonic followed by its operands.
4618 bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
4619                                SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4620   // Apply mnemonic aliases before doing anything else, as the destination
4621   // mnemnonic may include suffices and we want to handle them normally.
4622   // The generic tblgen'erated code does this later, at the start of
4623   // MatchInstructionImpl(), but that's too late for aliases that include
4624   // any sort of suffix.
4625   unsigned AvailableFeatures = getAvailableFeatures();
4626   applyMnemonicAliases(Name, AvailableFeatures);
4627
4628   // First check for the ARM-specific .req directive.
4629   if (Parser.getTok().is(AsmToken::Identifier) &&
4630       Parser.getTok().getIdentifier() == ".req") {
4631     parseDirectiveReq(Name, NameLoc);
4632     // We always return 'error' for this, as we're done with this
4633     // statement and don't need to match the 'instruction."
4634     return true;
4635   }
4636
4637   // Create the leading tokens for the mnemonic, split by '.' characters.
4638   size_t Start = 0, Next = Name.find('.');
4639   StringRef Mnemonic = Name.slice(Start, Next);
4640
4641   // Split out the predication code and carry setting flag from the mnemonic.
4642   unsigned PredicationCode;
4643   unsigned ProcessorIMod;
4644   bool CarrySetting;
4645   StringRef ITMask;
4646   Mnemonic = splitMnemonic(Mnemonic, PredicationCode, CarrySetting,
4647                            ProcessorIMod, ITMask);
4648
4649   // In Thumb1, only the branch (B) instruction can be predicated.
4650   if (isThumbOne() && PredicationCode != ARMCC::AL && Mnemonic != "b") {
4651     Parser.EatToEndOfStatement();
4652     return Error(NameLoc, "conditional execution not supported in Thumb1");
4653   }
4654
4655   Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
4656
4657   // Handle the IT instruction ITMask. Convert it to a bitmask. This
4658   // is the mask as it will be for the IT encoding if the conditional
4659   // encoding has a '1' as it's bit0 (i.e. 't' ==> '1'). In the case
4660   // where the conditional bit0 is zero, the instruction post-processing
4661   // will adjust the mask accordingly.
4662   if (Mnemonic == "it") {
4663     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + 2);
4664     if (ITMask.size() > 3) {
4665       Parser.EatToEndOfStatement();
4666       return Error(Loc, "too many conditions on IT instruction");
4667     }
4668     unsigned Mask = 8;
4669     for (unsigned i = ITMask.size(); i != 0; --i) {
4670       char pos = ITMask[i - 1];
4671       if (pos != 't' && pos != 'e') {
4672         Parser.EatToEndOfStatement();
4673         return Error(Loc, "illegal IT block condition mask '" + ITMask + "'");
4674       }
4675       Mask >>= 1;
4676       if (ITMask[i - 1] == 't')
4677         Mask |= 8;
4678     }
4679     Operands.push_back(ARMOperand::CreateITMask(Mask, Loc));
4680   }
4681
4682   // FIXME: This is all a pretty gross hack. We should automatically handle
4683   // optional operands like this via tblgen.
4684
4685   // Next, add the CCOut and ConditionCode operands, if needed.
4686   //
4687   // For mnemonics which can ever incorporate a carry setting bit or predication
4688   // code, our matching model involves us always generating CCOut and
4689   // ConditionCode operands to match the mnemonic "as written" and then we let
4690   // the matcher deal with finding the right instruction or generating an
4691   // appropriate error.
4692   bool CanAcceptCarrySet, CanAcceptPredicationCode;
4693   getMnemonicAcceptInfo(Mnemonic, CanAcceptCarrySet, CanAcceptPredicationCode);
4694
4695   // If we had a carry-set on an instruction that can't do that, issue an
4696   // error.
4697   if (!CanAcceptCarrySet && CarrySetting) {
4698     Parser.EatToEndOfStatement();
4699     return Error(NameLoc, "instruction '" + Mnemonic +
4700                  "' can not set flags, but 's' suffix specified");
4701   }
4702   // If we had a predication code on an instruction that can't do that, issue an
4703   // error.
4704   if (!CanAcceptPredicationCode && PredicationCode != ARMCC::AL) {
4705     Parser.EatToEndOfStatement();
4706     return Error(NameLoc, "instruction '" + Mnemonic +
4707                  "' is not predicable, but condition code specified");
4708   }
4709
4710   // Add the carry setting operand, if necessary.
4711   if (CanAcceptCarrySet) {
4712     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size());
4713     Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
4714                                                Loc));
4715   }
4716
4717   // Add the predication code operand, if necessary.
4718   if (CanAcceptPredicationCode) {
4719     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size() +
4720                                       CarrySetting);
4721     Operands.push_back(ARMOperand::CreateCondCode(
4722                          ARMCC::CondCodes(PredicationCode), Loc));
4723   }
4724
4725   // Add the processor imod operand, if necessary.
4726   if (ProcessorIMod) {
4727     Operands.push_back(ARMOperand::CreateImm(
4728           MCConstantExpr::Create(ProcessorIMod, getContext()),
4729                                  NameLoc, NameLoc));
4730   }
4731
4732   // Add the remaining tokens in the mnemonic.
4733   while (Next != StringRef::npos) {
4734     Start = Next;
4735     Next = Name.find('.', Start + 1);
4736     StringRef ExtraToken = Name.slice(Start, Next);
4737
4738     // Some NEON instructions have an optional datatype suffix that is
4739     // completely ignored. Check for that.
4740     if (isDataTypeToken(ExtraToken) &&
4741         doesIgnoreDataTypeSuffix(Mnemonic, ExtraToken))
4742       continue;
4743
4744     if (ExtraToken != ".n") {
4745       SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
4746       Operands.push_back(ARMOperand::CreateToken(ExtraToken, Loc));
4747     }
4748   }
4749
4750   // Read the remaining operands.
4751   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4752     // Read the first operand.
4753     if (parseOperand(Operands, Mnemonic)) {
4754       Parser.EatToEndOfStatement();
4755       return true;
4756     }
4757
4758     while (getLexer().is(AsmToken::Comma)) {
4759       Parser.Lex();  // Eat the comma.
4760
4761       // Parse and remember the operand.
4762       if (parseOperand(Operands, Mnemonic)) {
4763         Parser.EatToEndOfStatement();
4764         return true;
4765       }
4766     }
4767   }
4768
4769   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4770     SMLoc Loc = getLexer().getLoc();
4771     Parser.EatToEndOfStatement();
4772     return Error(Loc, "unexpected token in argument list");
4773   }
4774
4775   Parser.Lex(); // Consume the EndOfStatement
4776
4777   // Some instructions, mostly Thumb, have forms for the same mnemonic that
4778   // do and don't have a cc_out optional-def operand. With some spot-checks
4779   // of the operand list, we can figure out which variant we're trying to
4780   // parse and adjust accordingly before actually matching. We shouldn't ever
4781   // try to remove a cc_out operand that was explicitly set on the the
4782   // mnemonic, of course (CarrySetting == true). Reason number #317 the
4783   // table driven matcher doesn't fit well with the ARM instruction set.
4784   if (!CarrySetting && shouldOmitCCOutOperand(Mnemonic, Operands)) {
4785     ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
4786     Operands.erase(Operands.begin() + 1);
4787     delete Op;
4788   }
4789
4790   // ARM mode 'blx' need special handling, as the register operand version
4791   // is predicable, but the label operand version is not. So, we can't rely
4792   // on the Mnemonic based checking to correctly figure out when to put
4793   // a k_CondCode operand in the list. If we're trying to match the label
4794   // version, remove the k_CondCode operand here.
4795   if (!isThumb() && Mnemonic == "blx" && Operands.size() == 3 &&
4796       static_cast<ARMOperand*>(Operands[2])->isImm()) {
4797     ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
4798     Operands.erase(Operands.begin() + 1);
4799     delete Op;
4800   }
4801
4802   // The vector-compare-to-zero instructions have a literal token "#0" at
4803   // the end that comes to here as an immediate operand. Convert it to a
4804   // token to play nicely with the matcher.
4805   if ((Mnemonic == "vceq" || Mnemonic == "vcge" || Mnemonic == "vcgt" ||
4806       Mnemonic == "vcle" || Mnemonic == "vclt") && Operands.size() == 6 &&
4807       static_cast<ARMOperand*>(Operands[5])->isImm()) {
4808     ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
4809     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4810     if (CE && CE->getValue() == 0) {
4811       Operands.erase(Operands.begin() + 5);
4812       Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4813       delete Op;
4814     }
4815   }
4816   // VCMP{E} does the same thing, but with a different operand count.
4817   if ((Mnemonic == "vcmp" || Mnemonic == "vcmpe") && Operands.size() == 5 &&
4818       static_cast<ARMOperand*>(Operands[4])->isImm()) {
4819     ARMOperand *Op = static_cast<ARMOperand*>(Operands[4]);
4820     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4821     if (CE && CE->getValue() == 0) {
4822       Operands.erase(Operands.begin() + 4);
4823       Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4824       delete Op;
4825     }
4826   }
4827   // Similarly, the Thumb1 "RSB" instruction has a literal "#0" on the
4828   // end. Convert it to a token here. Take care not to convert those
4829   // that should hit the Thumb2 encoding.
4830   if (Mnemonic == "rsb" && isThumb() && Operands.size() == 6 &&
4831       static_cast<ARMOperand*>(Operands[3])->isReg() &&
4832       static_cast<ARMOperand*>(Operands[4])->isReg() &&
4833       static_cast<ARMOperand*>(Operands[5])->isImm()) {
4834     ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
4835     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4836     if (CE && CE->getValue() == 0 &&
4837         (isThumbOne() ||
4838          // The cc_out operand matches the IT block.
4839          ((inITBlock() != CarrySetting) &&
4840          // Neither register operand is a high register.
4841          (isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) &&
4842           isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()))))){
4843       Operands.erase(Operands.begin() + 5);
4844       Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4845       delete Op;
4846     }
4847   }
4848
4849   return false;
4850 }
4851
4852 // Validate context-sensitive operand constraints.
4853
4854 // return 'true' if register list contains non-low GPR registers,
4855 // 'false' otherwise. If Reg is in the register list or is HiReg, set
4856 // 'containsReg' to true.
4857 static bool checkLowRegisterList(MCInst Inst, unsigned OpNo, unsigned Reg,
4858                                  unsigned HiReg, bool &containsReg) {
4859   containsReg = false;
4860   for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
4861     unsigned OpReg = Inst.getOperand(i).getReg();
4862     if (OpReg == Reg)
4863       containsReg = true;
4864     // Anything other than a low register isn't legal here.
4865     if (!isARMLowRegister(OpReg) && (!HiReg || OpReg != HiReg))
4866       return true;
4867   }
4868   return false;
4869 }
4870
4871 // Check if the specified regisgter is in the register list of the inst,
4872 // starting at the indicated operand number.
4873 static bool listContainsReg(MCInst &Inst, unsigned OpNo, unsigned Reg) {
4874   for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
4875     unsigned OpReg = Inst.getOperand(i).getReg();
4876     if (OpReg == Reg)
4877       return true;
4878   }
4879   return false;
4880 }
4881
4882 // FIXME: We would really prefer to have MCInstrInfo (the wrapper around
4883 // the ARMInsts array) instead. Getting that here requires awkward
4884 // API changes, though. Better way?
4885 namespace llvm {
4886 extern const MCInstrDesc ARMInsts[];
4887 }
4888 static const MCInstrDesc &getInstDesc(unsigned Opcode) {
4889   return ARMInsts[Opcode];
4890 }
4891
4892 // FIXME: We would really like to be able to tablegen'erate this.
4893 bool ARMAsmParser::
4894 validateInstruction(MCInst &Inst,
4895                     const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4896   const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
4897   SMLoc Loc = Operands[0]->getStartLoc();
4898   // Check the IT block state first.
4899   // NOTE: In Thumb mode, the BKPT instruction has the interesting property of
4900   // being allowed in IT blocks, but not being predicable.  It just always
4901   // executes.
4902   if (inITBlock() && Inst.getOpcode() != ARM::tBKPT) {
4903     unsigned bit = 1;
4904     if (ITState.FirstCond)
4905       ITState.FirstCond = false;
4906     else
4907       bit = (ITState.Mask >> (5 - ITState.CurPosition)) & 1;
4908     // The instruction must be predicable.
4909     if (!MCID.isPredicable())
4910       return Error(Loc, "instructions in IT block must be predicable");
4911     unsigned Cond = Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm();
4912     unsigned ITCond = bit ? ITState.Cond :
4913       ARMCC::getOppositeCondition(ITState.Cond);
4914     if (Cond != ITCond) {
4915       // Find the condition code Operand to get its SMLoc information.
4916       SMLoc CondLoc;
4917       for (unsigned i = 1; i < Operands.size(); ++i)
4918         if (static_cast<ARMOperand*>(Operands[i])->isCondCode())
4919           CondLoc = Operands[i]->getStartLoc();
4920       return Error(CondLoc, "incorrect condition in IT block; got '" +
4921                    StringRef(ARMCondCodeToString(ARMCC::CondCodes(Cond))) +
4922                    "', but expected '" +
4923                    ARMCondCodeToString(ARMCC::CondCodes(ITCond)) + "'");
4924     }
4925   // Check for non-'al' condition codes outside of the IT block.
4926   } else if (isThumbTwo() && MCID.isPredicable() &&
4927              Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
4928              ARMCC::AL && Inst.getOpcode() != ARM::tB &&
4929              Inst.getOpcode() != ARM::t2B)
4930     return Error(Loc, "predicated instructions must be in IT block");
4931
4932   switch (Inst.getOpcode()) {
4933   case ARM::LDRD:
4934   case ARM::LDRD_PRE:
4935   case ARM::LDRD_POST:
4936   case ARM::LDREXD: {
4937     // Rt2 must be Rt + 1.
4938     unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
4939     unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4940     if (Rt2 != Rt + 1)
4941       return Error(Operands[3]->getStartLoc(),
4942                    "destination operands must be sequential");
4943     return false;
4944   }
4945   case ARM::STRD: {
4946     // Rt2 must be Rt + 1.
4947     unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
4948     unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4949     if (Rt2 != Rt + 1)
4950       return Error(Operands[3]->getStartLoc(),
4951                    "source operands must be sequential");
4952     return false;
4953   }
4954   case ARM::STRD_PRE:
4955   case ARM::STRD_POST:
4956   case ARM::STREXD: {
4957     // Rt2 must be Rt + 1.
4958     unsigned Rt = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4959     unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(2).getReg());
4960     if (Rt2 != Rt + 1)
4961       return Error(Operands[3]->getStartLoc(),
4962                    "source operands must be sequential");
4963     return false;
4964   }
4965   case ARM::SBFX:
4966   case ARM::UBFX: {
4967     // width must be in range [1, 32-lsb]
4968     unsigned lsb = Inst.getOperand(2).getImm();
4969     unsigned widthm1 = Inst.getOperand(3).getImm();
4970     if (widthm1 >= 32 - lsb)
4971       return Error(Operands[5]->getStartLoc(),
4972                    "bitfield width must be in range [1,32-lsb]");
4973     return false;
4974   }
4975   case ARM::tLDMIA: {
4976     // If we're parsing Thumb2, the .w variant is available and handles
4977     // most cases that are normally illegal for a Thumb1 LDM
4978     // instruction. We'll make the transformation in processInstruction()
4979     // if necessary.
4980     //
4981     // Thumb LDM instructions are writeback iff the base register is not
4982     // in the register list.
4983     unsigned Rn = Inst.getOperand(0).getReg();
4984     bool hasWritebackToken =
4985       (static_cast<ARMOperand*>(Operands[3])->isToken() &&
4986        static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
4987     bool listContainsBase;
4988     if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) && !isThumbTwo())
4989       return Error(Operands[3 + hasWritebackToken]->getStartLoc(),
4990                    "registers must be in range r0-r7");
4991     // If we should have writeback, then there should be a '!' token.
4992     if (!listContainsBase && !hasWritebackToken && !isThumbTwo())
4993       return Error(Operands[2]->getStartLoc(),
4994                    "writeback operator '!' expected");
4995     // If we should not have writeback, there must not be a '!'. This is
4996     // true even for the 32-bit wide encodings.
4997     if (listContainsBase && hasWritebackToken)
4998       return Error(Operands[3]->getStartLoc(),
4999                    "writeback operator '!' not allowed when base register "
5000                    "in register list");
5001
5002     break;
5003   }
5004   case ARM::t2LDMIA_UPD: {
5005     if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
5006       return Error(Operands[4]->getStartLoc(),
5007                    "writeback operator '!' not allowed when base register "
5008                    "in register list");
5009     break;
5010   }
5011   // Like for ldm/stm, push and pop have hi-reg handling version in Thumb2,
5012   // so only issue a diagnostic for thumb1. The instructions will be
5013   // switched to the t2 encodings in processInstruction() if necessary.
5014   case ARM::tPOP: {
5015     bool listContainsBase;
5016     if (checkLowRegisterList(Inst, 2, 0, ARM::PC, listContainsBase) &&
5017         !isThumbTwo())
5018       return Error(Operands[2]->getStartLoc(),
5019                    "registers must be in range r0-r7 or pc");
5020     break;
5021   }
5022   case ARM::tPUSH: {
5023     bool listContainsBase;
5024     if (checkLowRegisterList(Inst, 2, 0, ARM::LR, listContainsBase) &&
5025         !isThumbTwo())
5026       return Error(Operands[2]->getStartLoc(),
5027                    "registers must be in range r0-r7 or lr");
5028     break;
5029   }
5030   case ARM::tSTMIA_UPD: {
5031     bool listContainsBase;
5032     if (checkLowRegisterList(Inst, 4, 0, 0, listContainsBase) && !isThumbTwo())
5033       return Error(Operands[4]->getStartLoc(),
5034                    "registers must be in range r0-r7");
5035     break;
5036   }
5037   }
5038
5039   return false;
5040 }
5041
5042 static unsigned getRealVSTLNOpcode(unsigned Opc) {
5043   switch(Opc) {
5044   default: assert(0 && "unexpected opcode!");
5045   // VST1LN
5046   case ARM::VST1LNdWB_fixed_Asm_8:  case ARM::VST1LNdWB_fixed_Asm_P8:
5047   case ARM::VST1LNdWB_fixed_Asm_I8: case ARM::VST1LNdWB_fixed_Asm_S8:
5048   case ARM::VST1LNdWB_fixed_Asm_U8:
5049     return ARM::VST1LNd8_UPD;
5050   case ARM::VST1LNdWB_fixed_Asm_16:  case ARM::VST1LNdWB_fixed_Asm_P16:
5051   case ARM::VST1LNdWB_fixed_Asm_I16: case ARM::VST1LNdWB_fixed_Asm_S16:
5052   case ARM::VST1LNdWB_fixed_Asm_U16:
5053     return ARM::VST1LNd16_UPD;
5054   case ARM::VST1LNdWB_fixed_Asm_32:  case ARM::VST1LNdWB_fixed_Asm_F:
5055   case ARM::VST1LNdWB_fixed_Asm_F32: case ARM::VST1LNdWB_fixed_Asm_I32:
5056   case ARM::VST1LNdWB_fixed_Asm_S32: case ARM::VST1LNdWB_fixed_Asm_U32:
5057     return ARM::VST1LNd32_UPD;
5058   case ARM::VST1LNdWB_register_Asm_8:  case ARM::VST1LNdWB_register_Asm_P8:
5059   case ARM::VST1LNdWB_register_Asm_I8: case ARM::VST1LNdWB_register_Asm_S8:
5060   case ARM::VST1LNdWB_register_Asm_U8:
5061     return ARM::VST1LNd8_UPD;
5062   case ARM::VST1LNdWB_register_Asm_16:  case ARM::VST1LNdWB_register_Asm_P16:
5063   case ARM::VST1LNdWB_register_Asm_I16: case ARM::VST1LNdWB_register_Asm_S16:
5064   case ARM::VST1LNdWB_register_Asm_U16:
5065     return ARM::VST1LNd16_UPD;
5066   case ARM::VST1LNdWB_register_Asm_32:  case ARM::VST1LNdWB_register_Asm_F:
5067   case ARM::VST1LNdWB_register_Asm_F32: case ARM::VST1LNdWB_register_Asm_I32:
5068   case ARM::VST1LNdWB_register_Asm_S32: case ARM::VST1LNdWB_register_Asm_U32:
5069     return ARM::VST1LNd32_UPD;
5070   case ARM::VST1LNdAsm_8:  case ARM::VST1LNdAsm_P8:
5071   case ARM::VST1LNdAsm_I8: case ARM::VST1LNdAsm_S8:
5072   case ARM::VST1LNdAsm_U8:
5073     return ARM::VST1LNd8;
5074   case ARM::VST1LNdAsm_16:  case ARM::VST1LNdAsm_P16:
5075   case ARM::VST1LNdAsm_I16: case ARM::VST1LNdAsm_S16:
5076   case ARM::VST1LNdAsm_U16:
5077     return ARM::VST1LNd16;
5078   case ARM::VST1LNdAsm_32:  case ARM::VST1LNdAsm_F:
5079   case ARM::VST1LNdAsm_F32: case ARM::VST1LNdAsm_I32:
5080   case ARM::VST1LNdAsm_S32: case ARM::VST1LNdAsm_U32:
5081     return ARM::VST1LNd32;
5082
5083   // VST2LN
5084   case ARM::VST2LNdWB_fixed_Asm_8:  case ARM::VST2LNdWB_fixed_Asm_P8:
5085   case ARM::VST2LNdWB_fixed_Asm_I8: case ARM::VST2LNdWB_fixed_Asm_S8:
5086   case ARM::VST2LNdWB_fixed_Asm_U8:
5087     return ARM::VST2LNd8_UPD;
5088   case ARM::VST2LNdWB_fixed_Asm_16:  case ARM::VST2LNdWB_fixed_Asm_P16:
5089   case ARM::VST2LNdWB_fixed_Asm_I16: case ARM::VST2LNdWB_fixed_Asm_S16:
5090   case ARM::VST2LNdWB_fixed_Asm_U16:
5091     return ARM::VST2LNd16_UPD;
5092   case ARM::VST2LNdWB_fixed_Asm_32:  case ARM::VST2LNdWB_fixed_Asm_F:
5093   case ARM::VST2LNdWB_fixed_Asm_F32: case ARM::VST2LNdWB_fixed_Asm_I32:
5094   case ARM::VST2LNdWB_fixed_Asm_S32: case ARM::VST2LNdWB_fixed_Asm_U32:
5095     return ARM::VST2LNd32_UPD;
5096   case ARM::VST2LNdWB_register_Asm_8:  case ARM::VST2LNdWB_register_Asm_P8:
5097   case ARM::VST2LNdWB_register_Asm_I8: case ARM::VST2LNdWB_register_Asm_S8:
5098   case ARM::VST2LNdWB_register_Asm_U8:
5099     return ARM::VST2LNd8_UPD;
5100   case ARM::VST2LNdWB_register_Asm_16:  case ARM::VST2LNdWB_register_Asm_P16:
5101   case ARM::VST2LNdWB_register_Asm_I16: case ARM::VST2LNdWB_register_Asm_S16:
5102   case ARM::VST2LNdWB_register_Asm_U16:
5103     return ARM::VST2LNd16_UPD;
5104   case ARM::VST2LNdWB_register_Asm_32:  case ARM::VST2LNdWB_register_Asm_F:
5105   case ARM::VST2LNdWB_register_Asm_F32: case ARM::VST2LNdWB_register_Asm_I32:
5106   case ARM::VST2LNdWB_register_Asm_S32: case ARM::VST2LNdWB_register_Asm_U32:
5107     return ARM::VST2LNd32_UPD;
5108   case ARM::VST2LNdAsm_8:  case ARM::VST2LNdAsm_P8:
5109   case ARM::VST2LNdAsm_I8: case ARM::VST2LNdAsm_S8:
5110   case ARM::VST2LNdAsm_U8:
5111     return ARM::VST2LNd8;
5112   case ARM::VST2LNdAsm_16:  case ARM::VST2LNdAsm_P16:
5113   case ARM::VST2LNdAsm_I16: case ARM::VST2LNdAsm_S16:
5114   case ARM::VST2LNdAsm_U16:
5115     return ARM::VST2LNd16;
5116   case ARM::VST2LNdAsm_32:  case ARM::VST2LNdAsm_F:
5117   case ARM::VST2LNdAsm_F32: case ARM::VST2LNdAsm_I32:
5118   case ARM::VST2LNdAsm_S32: case ARM::VST2LNdAsm_U32:
5119     return ARM::VST2LNd32;
5120   }
5121 }
5122
5123 static unsigned getRealVLDLNOpcode(unsigned Opc) {
5124   switch(Opc) {
5125   default: assert(0 && "unexpected opcode!");
5126   // VLD1LN
5127   case ARM::VLD1LNdWB_fixed_Asm_8:  case ARM::VLD1LNdWB_fixed_Asm_P8:
5128   case ARM::VLD1LNdWB_fixed_Asm_I8: case ARM::VLD1LNdWB_fixed_Asm_S8:
5129   case ARM::VLD1LNdWB_fixed_Asm_U8:
5130     return ARM::VLD1LNd8_UPD;
5131   case ARM::VLD1LNdWB_fixed_Asm_16:  case ARM::VLD1LNdWB_fixed_Asm_P16:
5132   case ARM::VLD1LNdWB_fixed_Asm_I16: case ARM::VLD1LNdWB_fixed_Asm_S16:
5133   case ARM::VLD1LNdWB_fixed_Asm_U16:
5134     return ARM::VLD1LNd16_UPD;
5135   case ARM::VLD1LNdWB_fixed_Asm_32:  case ARM::VLD1LNdWB_fixed_Asm_F:
5136   case ARM::VLD1LNdWB_fixed_Asm_F32: case ARM::VLD1LNdWB_fixed_Asm_I32:
5137   case ARM::VLD1LNdWB_fixed_Asm_S32: case ARM::VLD1LNdWB_fixed_Asm_U32:
5138     return ARM::VLD1LNd32_UPD;
5139   case ARM::VLD1LNdWB_register_Asm_8:  case ARM::VLD1LNdWB_register_Asm_P8:
5140   case ARM::VLD1LNdWB_register_Asm_I8: case ARM::VLD1LNdWB_register_Asm_S8:
5141   case ARM::VLD1LNdWB_register_Asm_U8:
5142     return ARM::VLD1LNd8_UPD;
5143   case ARM::VLD1LNdWB_register_Asm_16:  case ARM::VLD1LNdWB_register_Asm_P16:
5144   case ARM::VLD1LNdWB_register_Asm_I16: case ARM::VLD1LNdWB_register_Asm_S16:
5145   case ARM::VLD1LNdWB_register_Asm_U16:
5146     return ARM::VLD1LNd16_UPD;
5147   case ARM::VLD1LNdWB_register_Asm_32:  case ARM::VLD1LNdWB_register_Asm_F:
5148   case ARM::VLD1LNdWB_register_Asm_F32: case ARM::VLD1LNdWB_register_Asm_I32:
5149   case ARM::VLD1LNdWB_register_Asm_S32: case ARM::VLD1LNdWB_register_Asm_U32:
5150     return ARM::VLD1LNd32_UPD;
5151   case ARM::VLD1LNdAsm_8:  case ARM::VLD1LNdAsm_P8:
5152   case ARM::VLD1LNdAsm_I8: case ARM::VLD1LNdAsm_S8:
5153   case ARM::VLD1LNdAsm_U8:
5154     return ARM::VLD1LNd8;
5155   case ARM::VLD1LNdAsm_16:  case ARM::VLD1LNdAsm_P16:
5156   case ARM::VLD1LNdAsm_I16: case ARM::VLD1LNdAsm_S16:
5157   case ARM::VLD1LNdAsm_U16:
5158     return ARM::VLD1LNd16;
5159   case ARM::VLD1LNdAsm_32:  case ARM::VLD1LNdAsm_F:
5160   case ARM::VLD1LNdAsm_F32: case ARM::VLD1LNdAsm_I32:
5161   case ARM::VLD1LNdAsm_S32: case ARM::VLD1LNdAsm_U32:
5162     return ARM::VLD1LNd32;
5163
5164   // VLD2LN
5165   case ARM::VLD2LNdWB_fixed_Asm_8:  case ARM::VLD2LNdWB_fixed_Asm_P8:
5166   case ARM::VLD2LNdWB_fixed_Asm_I8: case ARM::VLD2LNdWB_fixed_Asm_S8:
5167   case ARM::VLD2LNdWB_fixed_Asm_U8:
5168     return ARM::VLD2LNd8_UPD;
5169   case ARM::VLD2LNdWB_fixed_Asm_16:  case ARM::VLD2LNdWB_fixed_Asm_P16:
5170   case ARM::VLD2LNdWB_fixed_Asm_I16: case ARM::VLD2LNdWB_fixed_Asm_S16:
5171   case ARM::VLD2LNdWB_fixed_Asm_U16:
5172     return ARM::VLD2LNd16_UPD;
5173   case ARM::VLD2LNdWB_fixed_Asm_32:  case ARM::VLD2LNdWB_fixed_Asm_F:
5174   case ARM::VLD2LNdWB_fixed_Asm_F32: case ARM::VLD2LNdWB_fixed_Asm_I32:
5175   case ARM::VLD2LNdWB_fixed_Asm_S32: case ARM::VLD2LNdWB_fixed_Asm_U32:
5176     return ARM::VLD2LNd32_UPD;
5177   case ARM::VLD2LNdWB_register_Asm_8:  case ARM::VLD2LNdWB_register_Asm_P8:
5178   case ARM::VLD2LNdWB_register_Asm_I8: case ARM::VLD2LNdWB_register_Asm_S8:
5179   case ARM::VLD2LNdWB_register_Asm_U8:
5180     return ARM::VLD2LNd8_UPD;
5181   case ARM::VLD2LNdWB_register_Asm_16:  case ARM::VLD2LNdWB_register_Asm_P16:
5182   case ARM::VLD2LNdWB_register_Asm_I16: case ARM::VLD2LNdWB_register_Asm_S16:
5183   case ARM::VLD2LNdWB_register_Asm_U16:
5184     return ARM::VLD2LNd16_UPD;
5185   case ARM::VLD2LNdWB_register_Asm_32:  case ARM::VLD2LNdWB_register_Asm_F:
5186   case ARM::VLD2LNdWB_register_Asm_F32: case ARM::VLD2LNdWB_register_Asm_I32:
5187   case ARM::VLD2LNdWB_register_Asm_S32: case ARM::VLD2LNdWB_register_Asm_U32:
5188     return ARM::VLD2LNd32_UPD;
5189   case ARM::VLD2LNdAsm_8:  case ARM::VLD2LNdAsm_P8:
5190   case ARM::VLD2LNdAsm_I8: case ARM::VLD2LNdAsm_S8:
5191   case ARM::VLD2LNdAsm_U8:
5192     return ARM::VLD2LNd8;
5193   case ARM::VLD2LNdAsm_16:  case ARM::VLD2LNdAsm_P16:
5194   case ARM::VLD2LNdAsm_I16: case ARM::VLD2LNdAsm_S16:
5195   case ARM::VLD2LNdAsm_U16:
5196     return ARM::VLD2LNd16;
5197   case ARM::VLD2LNdAsm_32:  case ARM::VLD2LNdAsm_F:
5198   case ARM::VLD2LNdAsm_F32: case ARM::VLD2LNdAsm_I32:
5199   case ARM::VLD2LNdAsm_S32: case ARM::VLD2LNdAsm_U32:
5200     return ARM::VLD2LNd32;
5201   }
5202 }
5203
5204 bool ARMAsmParser::
5205 processInstruction(MCInst &Inst,
5206                    const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
5207   switch (Inst.getOpcode()) {
5208   // Handle NEON VST complex aliases.
5209   case ARM::VST1LNdWB_register_Asm_8: case ARM::VST1LNdWB_register_Asm_P8:
5210   case ARM::VST1LNdWB_register_Asm_I8: case ARM::VST1LNdWB_register_Asm_S8:
5211   case ARM::VST1LNdWB_register_Asm_U8: case ARM::VST1LNdWB_register_Asm_16:
5212   case ARM::VST1LNdWB_register_Asm_P16: case ARM::VST1LNdWB_register_Asm_I16:
5213   case ARM::VST1LNdWB_register_Asm_S16: case ARM::VST1LNdWB_register_Asm_U16:
5214   case ARM::VST1LNdWB_register_Asm_32: case ARM::VST1LNdWB_register_Asm_F:
5215   case ARM::VST1LNdWB_register_Asm_F32: case ARM::VST1LNdWB_register_Asm_I32:
5216   case ARM::VST1LNdWB_register_Asm_S32: case ARM::VST1LNdWB_register_Asm_U32: {
5217     MCInst TmpInst;
5218     // Shuffle the operands around so the lane index operand is in the
5219     // right place.
5220     TmpInst.setOpcode(getRealVSTLNOpcode(Inst.getOpcode()));
5221     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5222     TmpInst.addOperand(Inst.getOperand(2)); // Rn
5223     TmpInst.addOperand(Inst.getOperand(3)); // alignment
5224     TmpInst.addOperand(Inst.getOperand(4)); // Rm
5225     TmpInst.addOperand(Inst.getOperand(0)); // Vd
5226     TmpInst.addOperand(Inst.getOperand(1)); // lane
5227     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
5228     TmpInst.addOperand(Inst.getOperand(6));
5229     Inst = TmpInst;
5230     return true;
5231   }
5232
5233   case ARM::VST2LNdWB_register_Asm_8: case ARM::VST2LNdWB_register_Asm_P8:
5234   case ARM::VST2LNdWB_register_Asm_I8: case ARM::VST2LNdWB_register_Asm_S8:
5235   case ARM::VST2LNdWB_register_Asm_U8: case ARM::VST2LNdWB_register_Asm_16:
5236   case ARM::VST2LNdWB_register_Asm_P16: case ARM::VST2LNdWB_register_Asm_I16:
5237   case ARM::VST2LNdWB_register_Asm_S16: case ARM::VST2LNdWB_register_Asm_U16:
5238   case ARM::VST2LNdWB_register_Asm_32: case ARM::VST2LNdWB_register_Asm_F:
5239   case ARM::VST2LNdWB_register_Asm_F32: case ARM::VST2LNdWB_register_Asm_I32:
5240   case ARM::VST2LNdWB_register_Asm_S32: case ARM::VST2LNdWB_register_Asm_U32: {
5241     MCInst TmpInst;
5242     // Shuffle the operands around so the lane index operand is in the
5243     // right place.
5244     TmpInst.setOpcode(getRealVSTLNOpcode(Inst.getOpcode()));
5245     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5246     TmpInst.addOperand(Inst.getOperand(2)); // Rn
5247     TmpInst.addOperand(Inst.getOperand(3)); // alignment
5248     TmpInst.addOperand(Inst.getOperand(4)); // Rm
5249     TmpInst.addOperand(Inst.getOperand(0)); // Vd
5250     TmpInst.addOperand(MCOperand::CreateReg(Inst.getOperand(0).getReg()+1));
5251     TmpInst.addOperand(Inst.getOperand(1)); // lane
5252     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
5253     TmpInst.addOperand(Inst.getOperand(6));
5254     Inst = TmpInst;
5255     return true;
5256   }
5257   case ARM::VST1LNdWB_fixed_Asm_8: case ARM::VST1LNdWB_fixed_Asm_P8:
5258   case ARM::VST1LNdWB_fixed_Asm_I8: case ARM::VST1LNdWB_fixed_Asm_S8:
5259   case ARM::VST1LNdWB_fixed_Asm_U8: case ARM::VST1LNdWB_fixed_Asm_16:
5260   case ARM::VST1LNdWB_fixed_Asm_P16: case ARM::VST1LNdWB_fixed_Asm_I16:
5261   case ARM::VST1LNdWB_fixed_Asm_S16: case ARM::VST1LNdWB_fixed_Asm_U16:
5262   case ARM::VST1LNdWB_fixed_Asm_32: case ARM::VST1LNdWB_fixed_Asm_F:
5263   case ARM::VST1LNdWB_fixed_Asm_F32: case ARM::VST1LNdWB_fixed_Asm_I32:
5264   case ARM::VST1LNdWB_fixed_Asm_S32: case ARM::VST1LNdWB_fixed_Asm_U32: {
5265     MCInst TmpInst;
5266     // Shuffle the operands around so the lane index operand is in the
5267     // right place.
5268     TmpInst.setOpcode(getRealVSTLNOpcode(Inst.getOpcode()));
5269     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5270     TmpInst.addOperand(Inst.getOperand(2)); // Rn
5271     TmpInst.addOperand(Inst.getOperand(3)); // alignment
5272     TmpInst.addOperand(MCOperand::CreateReg(0)); // Rm
5273     TmpInst.addOperand(Inst.getOperand(0)); // Vd
5274     TmpInst.addOperand(Inst.getOperand(1)); // lane
5275     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5276     TmpInst.addOperand(Inst.getOperand(5));
5277     Inst = TmpInst;
5278     return true;
5279   }
5280
5281   case ARM::VST2LNdWB_fixed_Asm_8: case ARM::VST2LNdWB_fixed_Asm_P8:
5282   case ARM::VST2LNdWB_fixed_Asm_I8: case ARM::VST2LNdWB_fixed_Asm_S8:
5283   case ARM::VST2LNdWB_fixed_Asm_U8: case ARM::VST2LNdWB_fixed_Asm_16:
5284   case ARM::VST2LNdWB_fixed_Asm_P16: case ARM::VST2LNdWB_fixed_Asm_I16:
5285   case ARM::VST2LNdWB_fixed_Asm_S16: case ARM::VST2LNdWB_fixed_Asm_U16:
5286   case ARM::VST2LNdWB_fixed_Asm_32: case ARM::VST2LNdWB_fixed_Asm_F:
5287   case ARM::VST2LNdWB_fixed_Asm_F32: case ARM::VST2LNdWB_fixed_Asm_I32:
5288   case ARM::VST2LNdWB_fixed_Asm_S32: case ARM::VST2LNdWB_fixed_Asm_U32: {
5289     MCInst TmpInst;
5290     // Shuffle the operands around so the lane index operand is in the
5291     // right place.
5292     TmpInst.setOpcode(getRealVSTLNOpcode(Inst.getOpcode()));
5293     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5294     TmpInst.addOperand(Inst.getOperand(2)); // Rn
5295     TmpInst.addOperand(Inst.getOperand(3)); // alignment
5296     TmpInst.addOperand(MCOperand::CreateReg(0)); // Rm
5297     TmpInst.addOperand(Inst.getOperand(0)); // Vd
5298     TmpInst.addOperand(MCOperand::CreateReg(Inst.getOperand(0).getReg()+1));
5299     TmpInst.addOperand(Inst.getOperand(1)); // lane
5300     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5301     TmpInst.addOperand(Inst.getOperand(5));
5302     Inst = TmpInst;
5303     return true;
5304   }
5305   case ARM::VST1LNdAsm_8: case ARM::VST1LNdAsm_P8: case ARM::VST1LNdAsm_I8:
5306   case ARM::VST1LNdAsm_S8: case ARM::VST1LNdAsm_U8: case ARM::VST1LNdAsm_16:
5307   case ARM::VST1LNdAsm_P16: case ARM::VST1LNdAsm_I16: case ARM::VST1LNdAsm_S16:
5308   case ARM::VST1LNdAsm_U16: case ARM::VST1LNdAsm_32: case ARM::VST1LNdAsm_F:
5309   case ARM::VST1LNdAsm_F32: case ARM::VST1LNdAsm_I32: case ARM::VST1LNdAsm_S32:
5310   case ARM::VST1LNdAsm_U32: {
5311     MCInst TmpInst;
5312     // Shuffle the operands around so the lane index operand is in the
5313     // right place.
5314     TmpInst.setOpcode(getRealVSTLNOpcode(Inst.getOpcode()));
5315     TmpInst.addOperand(Inst.getOperand(2)); // Rn
5316     TmpInst.addOperand(Inst.getOperand(3)); // alignment
5317     TmpInst.addOperand(Inst.getOperand(0)); // Vd
5318     TmpInst.addOperand(Inst.getOperand(1)); // lane
5319     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5320     TmpInst.addOperand(Inst.getOperand(5));
5321     Inst = TmpInst;
5322     return true;
5323   }
5324
5325   case ARM::VST2LNdAsm_8: case ARM::VST2LNdAsm_P8: case ARM::VST2LNdAsm_I8:
5326   case ARM::VST2LNdAsm_S8: case ARM::VST2LNdAsm_U8: case ARM::VST2LNdAsm_16:
5327   case ARM::VST2LNdAsm_P16: case ARM::VST2LNdAsm_I16: case ARM::VST2LNdAsm_S16:
5328   case ARM::VST2LNdAsm_U16: case ARM::VST2LNdAsm_32: case ARM::VST2LNdAsm_F:
5329   case ARM::VST2LNdAsm_F32: case ARM::VST2LNdAsm_I32: case ARM::VST2LNdAsm_S32:
5330   case ARM::VST2LNdAsm_U32: {
5331     MCInst TmpInst;
5332     // Shuffle the operands around so the lane index operand is in the
5333     // right place.
5334     TmpInst.setOpcode(getRealVSTLNOpcode(Inst.getOpcode()));
5335     TmpInst.addOperand(Inst.getOperand(2)); // Rn
5336     TmpInst.addOperand(Inst.getOperand(3)); // alignment
5337     TmpInst.addOperand(Inst.getOperand(0)); // Vd
5338     TmpInst.addOperand(MCOperand::CreateReg(Inst.getOperand(0).getReg()+1));
5339     TmpInst.addOperand(Inst.getOperand(1)); // lane
5340     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5341     TmpInst.addOperand(Inst.getOperand(5));
5342     Inst = TmpInst;
5343     return true;
5344   }
5345   // Handle NEON VLD complex aliases.
5346   case ARM::VLD1LNdWB_register_Asm_8: case ARM::VLD1LNdWB_register_Asm_P8:
5347   case ARM::VLD1LNdWB_register_Asm_I8: case ARM::VLD1LNdWB_register_Asm_S8:
5348   case ARM::VLD1LNdWB_register_Asm_U8: case ARM::VLD1LNdWB_register_Asm_16:
5349   case ARM::VLD1LNdWB_register_Asm_P16: case ARM::VLD1LNdWB_register_Asm_I16:
5350   case ARM::VLD1LNdWB_register_Asm_S16: case ARM::VLD1LNdWB_register_Asm_U16:
5351   case ARM::VLD1LNdWB_register_Asm_32: case ARM::VLD1LNdWB_register_Asm_F:
5352   case ARM::VLD1LNdWB_register_Asm_F32: case ARM::VLD1LNdWB_register_Asm_I32:
5353   case ARM::VLD1LNdWB_register_Asm_S32: case ARM::VLD1LNdWB_register_Asm_U32: {
5354     MCInst TmpInst;
5355     // Shuffle the operands around so the lane index operand is in the
5356     // right place.
5357     TmpInst.setOpcode(getRealVLDLNOpcode(Inst.getOpcode()));
5358     TmpInst.addOperand(Inst.getOperand(0)); // Vd
5359     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5360     TmpInst.addOperand(Inst.getOperand(2)); // Rn
5361     TmpInst.addOperand(Inst.getOperand(3)); // alignment
5362     TmpInst.addOperand(Inst.getOperand(4)); // Rm
5363     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
5364     TmpInst.addOperand(Inst.getOperand(1)); // lane
5365     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
5366     TmpInst.addOperand(Inst.getOperand(6));
5367     Inst = TmpInst;
5368     return true;
5369   }
5370
5371   case ARM::VLD2LNdWB_register_Asm_8: case ARM::VLD2LNdWB_register_Asm_P8:
5372   case ARM::VLD2LNdWB_register_Asm_I8: case ARM::VLD2LNdWB_register_Asm_S8:
5373   case ARM::VLD2LNdWB_register_Asm_U8: case ARM::VLD2LNdWB_register_Asm_16:
5374   case ARM::VLD2LNdWB_register_Asm_P16: case ARM::VLD2LNdWB_register_Asm_I16:
5375   case ARM::VLD2LNdWB_register_Asm_S16: case ARM::VLD2LNdWB_register_Asm_U16:
5376   case ARM::VLD2LNdWB_register_Asm_32: case ARM::VLD2LNdWB_register_Asm_F:
5377   case ARM::VLD2LNdWB_register_Asm_F32: case ARM::VLD2LNdWB_register_Asm_I32:
5378   case ARM::VLD2LNdWB_register_Asm_S32: case ARM::VLD2LNdWB_register_Asm_U32: {
5379     MCInst TmpInst;
5380     // Shuffle the operands around so the lane index operand is in the
5381     // right place.
5382     TmpInst.setOpcode(getRealVLDLNOpcode(Inst.getOpcode()));
5383     TmpInst.addOperand(Inst.getOperand(0)); // Vd
5384     TmpInst.addOperand(MCOperand::CreateReg(Inst.getOperand(0).getReg()+1));
5385     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5386     TmpInst.addOperand(Inst.getOperand(2)); // Rn
5387     TmpInst.addOperand(Inst.getOperand(3)); // alignment
5388     TmpInst.addOperand(Inst.getOperand(4)); // Rm
5389     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
5390     TmpInst.addOperand(MCOperand::CreateReg(Inst.getOperand(0).getReg()+1));
5391     TmpInst.addOperand(Inst.getOperand(1)); // lane
5392     TmpInst.addOperand(Inst.getOperand(5)); // CondCode
5393     TmpInst.addOperand(Inst.getOperand(6));
5394     Inst = TmpInst;
5395     return true;
5396   }
5397
5398   case ARM::VLD1LNdWB_fixed_Asm_8: case ARM::VLD1LNdWB_fixed_Asm_P8:
5399   case ARM::VLD1LNdWB_fixed_Asm_I8: case ARM::VLD1LNdWB_fixed_Asm_S8:
5400   case ARM::VLD1LNdWB_fixed_Asm_U8: case ARM::VLD1LNdWB_fixed_Asm_16:
5401   case ARM::VLD1LNdWB_fixed_Asm_P16: case ARM::VLD1LNdWB_fixed_Asm_I16:
5402   case ARM::VLD1LNdWB_fixed_Asm_S16: case ARM::VLD1LNdWB_fixed_Asm_U16:
5403   case ARM::VLD1LNdWB_fixed_Asm_32: case ARM::VLD1LNdWB_fixed_Asm_F:
5404   case ARM::VLD1LNdWB_fixed_Asm_F32: case ARM::VLD1LNdWB_fixed_Asm_I32:
5405   case ARM::VLD1LNdWB_fixed_Asm_S32: case ARM::VLD1LNdWB_fixed_Asm_U32: {
5406     MCInst TmpInst;
5407     // Shuffle the operands around so the lane index operand is in the
5408     // right place.
5409     TmpInst.setOpcode(getRealVLDLNOpcode(Inst.getOpcode()));
5410     TmpInst.addOperand(Inst.getOperand(0)); // Vd
5411     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5412     TmpInst.addOperand(Inst.getOperand(2)); // Rn
5413     TmpInst.addOperand(Inst.getOperand(3)); // alignment
5414     TmpInst.addOperand(MCOperand::CreateReg(0)); // Rm
5415     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
5416     TmpInst.addOperand(Inst.getOperand(1)); // lane
5417     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5418     TmpInst.addOperand(Inst.getOperand(5));
5419     Inst = TmpInst;
5420     return true;
5421   }
5422
5423   case ARM::VLD2LNdWB_fixed_Asm_8: case ARM::VLD2LNdWB_fixed_Asm_P8:
5424   case ARM::VLD2LNdWB_fixed_Asm_I8: case ARM::VLD2LNdWB_fixed_Asm_S8:
5425   case ARM::VLD2LNdWB_fixed_Asm_U8: case ARM::VLD2LNdWB_fixed_Asm_16:
5426   case ARM::VLD2LNdWB_fixed_Asm_P16: case ARM::VLD2LNdWB_fixed_Asm_I16:
5427   case ARM::VLD2LNdWB_fixed_Asm_S16: case ARM::VLD2LNdWB_fixed_Asm_U16:
5428   case ARM::VLD2LNdWB_fixed_Asm_32: case ARM::VLD2LNdWB_fixed_Asm_F:
5429   case ARM::VLD2LNdWB_fixed_Asm_F32: case ARM::VLD2LNdWB_fixed_Asm_I32:
5430   case ARM::VLD2LNdWB_fixed_Asm_S32: case ARM::VLD2LNdWB_fixed_Asm_U32: {
5431     MCInst TmpInst;
5432     // Shuffle the operands around so the lane index operand is in the
5433     // right place.
5434     TmpInst.setOpcode(getRealVLDLNOpcode(Inst.getOpcode()));
5435     TmpInst.addOperand(Inst.getOperand(0)); // Vd
5436     TmpInst.addOperand(MCOperand::CreateReg(Inst.getOperand(0).getReg()+1));
5437     TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5438     TmpInst.addOperand(Inst.getOperand(2)); // Rn
5439     TmpInst.addOperand(Inst.getOperand(3)); // alignment
5440     TmpInst.addOperand(MCOperand::CreateReg(0)); // Rm
5441     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
5442     TmpInst.addOperand(MCOperand::CreateReg(Inst.getOperand(0).getReg()+1));
5443     TmpInst.addOperand(Inst.getOperand(1)); // lane
5444     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5445     TmpInst.addOperand(Inst.getOperand(5));
5446     Inst = TmpInst;
5447     return true;
5448   }
5449
5450   case ARM::VLD1LNdAsm_8: case ARM::VLD1LNdAsm_P8: case ARM::VLD1LNdAsm_I8:
5451   case ARM::VLD1LNdAsm_S8: case ARM::VLD1LNdAsm_U8: case ARM::VLD1LNdAsm_16:
5452   case ARM::VLD1LNdAsm_P16: case ARM::VLD1LNdAsm_I16: case ARM::VLD1LNdAsm_S16:
5453   case ARM::VLD1LNdAsm_U16: case ARM::VLD1LNdAsm_32: case ARM::VLD1LNdAsm_F:
5454   case ARM::VLD1LNdAsm_F32: case ARM::VLD1LNdAsm_I32: case ARM::VLD1LNdAsm_S32:
5455   case ARM::VLD1LNdAsm_U32: {
5456     MCInst TmpInst;
5457     // Shuffle the operands around so the lane index operand is in the
5458     // right place.
5459     TmpInst.setOpcode(getRealVLDLNOpcode(Inst.getOpcode()));
5460     TmpInst.addOperand(Inst.getOperand(0)); // Vd
5461     TmpInst.addOperand(Inst.getOperand(2)); // Rn
5462     TmpInst.addOperand(Inst.getOperand(3)); // alignment
5463     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
5464     TmpInst.addOperand(Inst.getOperand(1)); // lane
5465     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5466     TmpInst.addOperand(Inst.getOperand(5));
5467     Inst = TmpInst;
5468     return true;
5469   }
5470
5471   case ARM::VLD2LNdAsm_8: case ARM::VLD2LNdAsm_P8: case ARM::VLD2LNdAsm_I8:
5472   case ARM::VLD2LNdAsm_S8: case ARM::VLD2LNdAsm_U8: case ARM::VLD2LNdAsm_16:
5473   case ARM::VLD2LNdAsm_P16: case ARM::VLD2LNdAsm_I16: case ARM::VLD2LNdAsm_S16:
5474   case ARM::VLD2LNdAsm_U16: case ARM::VLD2LNdAsm_32: case ARM::VLD2LNdAsm_F:
5475   case ARM::VLD2LNdAsm_F32: case ARM::VLD2LNdAsm_I32: case ARM::VLD2LNdAsm_S32:
5476   case ARM::VLD2LNdAsm_U32: {
5477     MCInst TmpInst;
5478     // Shuffle the operands around so the lane index operand is in the
5479     // right place.
5480     TmpInst.setOpcode(getRealVLDLNOpcode(Inst.getOpcode()));
5481     TmpInst.addOperand(Inst.getOperand(0)); // Vd
5482     TmpInst.addOperand(MCOperand::CreateReg(Inst.getOperand(0).getReg()+1));
5483     TmpInst.addOperand(Inst.getOperand(2)); // Rn
5484     TmpInst.addOperand(Inst.getOperand(3)); // alignment
5485     TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
5486     TmpInst.addOperand(MCOperand::CreateReg(Inst.getOperand(0).getReg()+1));
5487     TmpInst.addOperand(Inst.getOperand(1)); // lane
5488     TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5489     TmpInst.addOperand(Inst.getOperand(5));
5490     Inst = TmpInst;
5491     return true;
5492   }
5493   // Handle the Thumb2 mode MOV complex aliases.
5494   case ARM::t2MOVsi:
5495   case ARM::t2MOVSsi: {
5496     // Which instruction to expand to depends on the CCOut operand and
5497     // whether we're in an IT block if the register operands are low
5498     // registers.
5499     bool isNarrow = false;
5500     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
5501         isARMLowRegister(Inst.getOperand(1).getReg()) &&
5502         inITBlock() == (Inst.getOpcode() == ARM::t2MOVsi))
5503       isNarrow = true;
5504     MCInst TmpInst;
5505     unsigned newOpc;
5506     switch(ARM_AM::getSORegShOp(Inst.getOperand(2).getImm())) {
5507     default: llvm_unreachable("unexpected opcode!");
5508     case ARM_AM::asr: newOpc = isNarrow ? ARM::tASRri : ARM::t2ASRri; break;
5509     case ARM_AM::lsr: newOpc = isNarrow ? ARM::tLSRri : ARM::t2LSRri; break;
5510     case ARM_AM::lsl: newOpc = isNarrow ? ARM::tLSLri : ARM::t2LSLri; break;
5511     case ARM_AM::ror: newOpc = ARM::t2RORri; isNarrow = false; break;
5512     }
5513     unsigned Ammount = ARM_AM::getSORegOffset(Inst.getOperand(2).getImm());
5514     if (Ammount == 32) Ammount = 0;
5515     TmpInst.setOpcode(newOpc);
5516     TmpInst.addOperand(Inst.getOperand(0)); // Rd
5517     if (isNarrow)
5518       TmpInst.addOperand(MCOperand::CreateReg(
5519           Inst.getOpcode() == ARM::t2MOVSsi ? ARM::CPSR : 0));
5520     TmpInst.addOperand(Inst.getOperand(1)); // Rn
5521     TmpInst.addOperand(MCOperand::CreateImm(Ammount));
5522     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
5523     TmpInst.addOperand(Inst.getOperand(4));
5524     if (!isNarrow)
5525       TmpInst.addOperand(MCOperand::CreateReg(
5526           Inst.getOpcode() == ARM::t2MOVSsi ? ARM::CPSR : 0));
5527     Inst = TmpInst;
5528     return true;
5529   }
5530   // Handle the ARM mode MOV complex aliases.
5531   case ARM::ASRr:
5532   case ARM::LSRr:
5533   case ARM::LSLr:
5534   case ARM::RORr: {
5535     ARM_AM::ShiftOpc ShiftTy;
5536     switch(Inst.getOpcode()) {
5537     default: llvm_unreachable("unexpected opcode!");
5538     case ARM::ASRr: ShiftTy = ARM_AM::asr; break;
5539     case ARM::LSRr: ShiftTy = ARM_AM::lsr; break;
5540     case ARM::LSLr: ShiftTy = ARM_AM::lsl; break;
5541     case ARM::RORr: ShiftTy = ARM_AM::ror; break;
5542     }
5543     // A shift by zero is a plain MOVr, not a MOVsi.
5544     unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, 0);
5545     MCInst TmpInst;
5546     TmpInst.setOpcode(ARM::MOVsr);
5547     TmpInst.addOperand(Inst.getOperand(0)); // Rd
5548     TmpInst.addOperand(Inst.getOperand(1)); // Rn
5549     TmpInst.addOperand(Inst.getOperand(2)); // Rm
5550     TmpInst.addOperand(MCOperand::CreateImm(Shifter)); // Shift value and ty
5551     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
5552     TmpInst.addOperand(Inst.getOperand(4));
5553     TmpInst.addOperand(Inst.getOperand(5)); // cc_out
5554     Inst = TmpInst;
5555     return true;
5556   }
5557   case ARM::ASRi:
5558   case ARM::LSRi:
5559   case ARM::LSLi:
5560   case ARM::RORi: {
5561     ARM_AM::ShiftOpc ShiftTy;
5562     switch(Inst.getOpcode()) {
5563     default: llvm_unreachable("unexpected opcode!");
5564     case ARM::ASRi: ShiftTy = ARM_AM::asr; break;
5565     case ARM::LSRi: ShiftTy = ARM_AM::lsr; break;
5566     case ARM::LSLi: ShiftTy = ARM_AM::lsl; break;
5567     case ARM::RORi: ShiftTy = ARM_AM::ror; break;
5568     }
5569     // A shift by zero is a plain MOVr, not a MOVsi.
5570     unsigned Amt = Inst.getOperand(2).getImm();
5571     unsigned Opc = Amt == 0 ? ARM::MOVr : ARM::MOVsi;
5572     unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, Amt);
5573     MCInst TmpInst;
5574     TmpInst.setOpcode(Opc);
5575     TmpInst.addOperand(Inst.getOperand(0)); // Rd
5576     TmpInst.addOperand(Inst.getOperand(1)); // Rn
5577     if (Opc == ARM::MOVsi)
5578       TmpInst.addOperand(MCOperand::CreateImm(Shifter)); // Shift value and ty
5579     TmpInst.addOperand(Inst.getOperand(3)); // CondCode
5580     TmpInst.addOperand(Inst.getOperand(4));
5581     TmpInst.addOperand(Inst.getOperand(5)); // cc_out
5582     Inst = TmpInst;
5583     return true;
5584   }
5585   case ARM::RRXi: {
5586     unsigned Shifter = ARM_AM::getSORegOpc(ARM_AM::rrx, 0);
5587     MCInst TmpInst;
5588     TmpInst.setOpcode(ARM::MOVsi);
5589     TmpInst.addOperand(Inst.getOperand(0)); // Rd
5590     TmpInst.addOperand(Inst.getOperand(1)); // Rn
5591     TmpInst.addOperand(MCOperand::CreateImm(Shifter)); // Shift value and ty
5592     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
5593     TmpInst.addOperand(Inst.getOperand(3));
5594     TmpInst.addOperand(Inst.getOperand(4)); // cc_out
5595     Inst = TmpInst;
5596     return true;
5597   }
5598   case ARM::t2LDMIA_UPD: {
5599     // If this is a load of a single register, then we should use
5600     // a post-indexed LDR instruction instead, per the ARM ARM.
5601     if (Inst.getNumOperands() != 5)
5602       return false;
5603     MCInst TmpInst;
5604     TmpInst.setOpcode(ARM::t2LDR_POST);
5605     TmpInst.addOperand(Inst.getOperand(4)); // Rt
5606     TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
5607     TmpInst.addOperand(Inst.getOperand(1)); // Rn
5608     TmpInst.addOperand(MCOperand::CreateImm(4));
5609     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
5610     TmpInst.addOperand(Inst.getOperand(3));
5611     Inst = TmpInst;
5612     return true;
5613   }
5614   case ARM::t2STMDB_UPD: {
5615     // If this is a store of a single register, then we should use
5616     // a pre-indexed STR instruction instead, per the ARM ARM.
5617     if (Inst.getNumOperands() != 5)
5618       return false;
5619     MCInst TmpInst;
5620     TmpInst.setOpcode(ARM::t2STR_PRE);
5621     TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
5622     TmpInst.addOperand(Inst.getOperand(4)); // Rt
5623     TmpInst.addOperand(Inst.getOperand(1)); // Rn
5624     TmpInst.addOperand(MCOperand::CreateImm(-4));
5625     TmpInst.addOperand(Inst.getOperand(2)); // CondCode
5626     TmpInst.addOperand(Inst.getOperand(3));
5627     Inst = TmpInst;
5628     return true;
5629   }
5630   case ARM::LDMIA_UPD:
5631     // If this is a load of a single register via a 'pop', then we should use
5632     // a post-indexed LDR instruction instead, per the ARM ARM.
5633     if (static_cast<ARMOperand*>(Operands[0])->getToken() == "pop" &&
5634         Inst.getNumOperands() == 5) {
5635       MCInst TmpInst;
5636       TmpInst.setOpcode(ARM::LDR_POST_IMM);
5637       TmpInst.addOperand(Inst.getOperand(4)); // Rt
5638       TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
5639       TmpInst.addOperand(Inst.getOperand(1)); // Rn
5640       TmpInst.addOperand(MCOperand::CreateReg(0));  // am2offset
5641       TmpInst.addOperand(MCOperand::CreateImm(4));
5642       TmpInst.addOperand(Inst.getOperand(2)); // CondCode
5643       TmpInst.addOperand(Inst.getOperand(3));
5644       Inst = TmpInst;
5645       return true;
5646     }
5647     break;
5648   case ARM::STMDB_UPD:
5649     // If this is a store of a single register via a 'push', then we should use
5650     // a pre-indexed STR instruction instead, per the ARM ARM.
5651     if (static_cast<ARMOperand*>(Operands[0])->getToken() == "push" &&
5652         Inst.getNumOperands() == 5) {
5653       MCInst TmpInst;
5654       TmpInst.setOpcode(ARM::STR_PRE_IMM);
5655       TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
5656       TmpInst.addOperand(Inst.getOperand(4)); // Rt
5657       TmpInst.addOperand(Inst.getOperand(1)); // addrmode_imm12
5658       TmpInst.addOperand(MCOperand::CreateImm(-4));
5659       TmpInst.addOperand(Inst.getOperand(2)); // CondCode
5660       TmpInst.addOperand(Inst.getOperand(3));
5661       Inst = TmpInst;
5662     }
5663     break;
5664   case ARM::t2ADDri12:
5665     // If the immediate fits for encoding T3 (t2ADDri) and the generic "add"
5666     // mnemonic was used (not "addw"), encoding T3 is preferred.
5667     if (static_cast<ARMOperand*>(Operands[0])->getToken() != "add" ||
5668         ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
5669       break;
5670     Inst.setOpcode(ARM::t2ADDri);
5671     Inst.addOperand(MCOperand::CreateReg(0)); // cc_out
5672     break;
5673   case ARM::t2SUBri12:
5674     // If the immediate fits for encoding T3 (t2SUBri) and the generic "sub"
5675     // mnemonic was used (not "subw"), encoding T3 is preferred.
5676     if (static_cast<ARMOperand*>(Operands[0])->getToken() != "sub" ||
5677         ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
5678       break;
5679     Inst.setOpcode(ARM::t2SUBri);
5680     Inst.addOperand(MCOperand::CreateReg(0)); // cc_out
5681     break;
5682   case ARM::tADDi8:
5683     // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
5684     // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
5685     // to encoding T2 if <Rd> is specified and encoding T2 is preferred
5686     // to encoding T1 if <Rd> is omitted."
5687     if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
5688       Inst.setOpcode(ARM::tADDi3);
5689       return true;
5690     }
5691     break;
5692   case ARM::tSUBi8:
5693     // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
5694     // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
5695     // to encoding T2 if <Rd> is specified and encoding T2 is preferred
5696     // to encoding T1 if <Rd> is omitted."
5697     if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
5698       Inst.setOpcode(ARM::tSUBi3);
5699       return true;
5700     }
5701     break;
5702   case ARM::t2ADDrr: {
5703     // If the destination and first source operand are the same, and
5704     // there's no setting of the flags, use encoding T2 instead of T3.
5705     // Note that this is only for ADD, not SUB. This mirrors the system
5706     // 'as' behaviour. Make sure the wide encoding wasn't explicit.
5707     if (Inst.getOperand(0).getReg() != Inst.getOperand(1).getReg() ||
5708         Inst.getOperand(5).getReg() != 0 ||
5709         (static_cast<ARMOperand*>(Operands[3])->isToken() &&
5710          static_cast<ARMOperand*>(Operands[3])->getToken() == ".w"))
5711       break;
5712     MCInst TmpInst;
5713     TmpInst.setOpcode(ARM::tADDhirr);
5714     TmpInst.addOperand(Inst.getOperand(0));
5715     TmpInst.addOperand(Inst.getOperand(0));
5716     TmpInst.addOperand(Inst.getOperand(2));
5717     TmpInst.addOperand(Inst.getOperand(3));
5718     TmpInst.addOperand(Inst.getOperand(4));
5719     Inst = TmpInst;
5720     return true;
5721   }
5722   case ARM::tB:
5723     // A Thumb conditional branch outside of an IT block is a tBcc.
5724     if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()) {
5725       Inst.setOpcode(ARM::tBcc);
5726       return true;
5727     }
5728     break;
5729   case ARM::t2B:
5730     // A Thumb2 conditional branch outside of an IT block is a t2Bcc.
5731     if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()){
5732       Inst.setOpcode(ARM::t2Bcc);
5733       return true;
5734     }
5735     break;
5736   case ARM::t2Bcc:
5737     // If the conditional is AL or we're in an IT block, we really want t2B.
5738     if (Inst.getOperand(1).getImm() == ARMCC::AL || inITBlock()) {
5739       Inst.setOpcode(ARM::t2B);
5740       return true;
5741     }
5742     break;
5743   case ARM::tBcc:
5744     // If the conditional is AL, we really want tB.
5745     if (Inst.getOperand(1).getImm() == ARMCC::AL) {
5746       Inst.setOpcode(ARM::tB);
5747       return true;
5748     }
5749     break;
5750   case ARM::tLDMIA: {
5751     // If the register list contains any high registers, or if the writeback
5752     // doesn't match what tLDMIA can do, we need to use the 32-bit encoding
5753     // instead if we're in Thumb2. Otherwise, this should have generated
5754     // an error in validateInstruction().
5755     unsigned Rn = Inst.getOperand(0).getReg();
5756     bool hasWritebackToken =
5757       (static_cast<ARMOperand*>(Operands[3])->isToken() &&
5758        static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
5759     bool listContainsBase;
5760     if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) ||
5761         (!listContainsBase && !hasWritebackToken) ||
5762         (listContainsBase && hasWritebackToken)) {
5763       // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
5764       assert (isThumbTwo());
5765       Inst.setOpcode(hasWritebackToken ? ARM::t2LDMIA_UPD : ARM::t2LDMIA);
5766       // If we're switching to the updating version, we need to insert
5767       // the writeback tied operand.
5768       if (hasWritebackToken)
5769         Inst.insert(Inst.begin(),
5770                     MCOperand::CreateReg(Inst.getOperand(0).getReg()));
5771       return true;
5772     }
5773     break;
5774   }
5775   case ARM::tSTMIA_UPD: {
5776     // If the register list contains any high registers, we need to use
5777     // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
5778     // should have generated an error in validateInstruction().
5779     unsigned Rn = Inst.getOperand(0).getReg();
5780     bool listContainsBase;
5781     if (checkLowRegisterList(Inst, 4, Rn, 0, listContainsBase)) {
5782       // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
5783       assert (isThumbTwo());
5784       Inst.setOpcode(ARM::t2STMIA_UPD);
5785       return true;
5786     }
5787     break;
5788   }
5789   case ARM::tPOP: {
5790     bool listContainsBase;
5791     // If the register list contains any high registers, we need to use
5792     // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
5793     // should have generated an error in validateInstruction().
5794     if (!checkLowRegisterList(Inst, 2, 0, ARM::PC, listContainsBase))
5795       return false;
5796     assert (isThumbTwo());
5797     Inst.setOpcode(ARM::t2LDMIA_UPD);
5798     // Add the base register and writeback operands.
5799     Inst.insert(Inst.begin(), MCOperand::CreateReg(ARM::SP));
5800     Inst.insert(Inst.begin(), MCOperand::CreateReg(ARM::SP));
5801     return true;
5802   }
5803   case ARM::tPUSH: {
5804     bool listContainsBase;
5805     if (!checkLowRegisterList(Inst, 2, 0, ARM::LR, listContainsBase))
5806       return false;
5807     assert (isThumbTwo());
5808     Inst.setOpcode(ARM::t2STMDB_UPD);
5809     // Add the base register and writeback operands.
5810     Inst.insert(Inst.begin(), MCOperand::CreateReg(ARM::SP));
5811     Inst.insert(Inst.begin(), MCOperand::CreateReg(ARM::SP));
5812     return true;
5813   }
5814   case ARM::t2MOVi: {
5815     // If we can use the 16-bit encoding and the user didn't explicitly
5816     // request the 32-bit variant, transform it here.
5817     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
5818         Inst.getOperand(1).getImm() <= 255 &&
5819         ((!inITBlock() && Inst.getOperand(2).getImm() == ARMCC::AL &&
5820          Inst.getOperand(4).getReg() == ARM::CPSR) ||
5821         (inITBlock() && Inst.getOperand(4).getReg() == 0)) &&
5822         (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
5823          static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
5824       // The operands aren't in the same order for tMOVi8...
5825       MCInst TmpInst;
5826       TmpInst.setOpcode(ARM::tMOVi8);
5827       TmpInst.addOperand(Inst.getOperand(0));
5828       TmpInst.addOperand(Inst.getOperand(4));
5829       TmpInst.addOperand(Inst.getOperand(1));
5830       TmpInst.addOperand(Inst.getOperand(2));
5831       TmpInst.addOperand(Inst.getOperand(3));
5832       Inst = TmpInst;
5833       return true;
5834     }
5835     break;
5836   }
5837   case ARM::t2MOVr: {
5838     // If we can use the 16-bit encoding and the user didn't explicitly
5839     // request the 32-bit variant, transform it here.
5840     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
5841         isARMLowRegister(Inst.getOperand(1).getReg()) &&
5842         Inst.getOperand(2).getImm() == ARMCC::AL &&
5843         Inst.getOperand(4).getReg() == ARM::CPSR &&
5844         (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
5845          static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
5846       // The operands aren't the same for tMOV[S]r... (no cc_out)
5847       MCInst TmpInst;
5848       TmpInst.setOpcode(Inst.getOperand(4).getReg() ? ARM::tMOVSr : ARM::tMOVr);
5849       TmpInst.addOperand(Inst.getOperand(0));
5850       TmpInst.addOperand(Inst.getOperand(1));
5851       TmpInst.addOperand(Inst.getOperand(2));
5852       TmpInst.addOperand(Inst.getOperand(3));
5853       Inst = TmpInst;
5854       return true;
5855     }
5856     break;
5857   }
5858   case ARM::t2SXTH:
5859   case ARM::t2SXTB:
5860   case ARM::t2UXTH:
5861   case ARM::t2UXTB: {
5862     // If we can use the 16-bit encoding and the user didn't explicitly
5863     // request the 32-bit variant, transform it here.
5864     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
5865         isARMLowRegister(Inst.getOperand(1).getReg()) &&
5866         Inst.getOperand(2).getImm() == 0 &&
5867         (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
5868          static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
5869       unsigned NewOpc;
5870       switch (Inst.getOpcode()) {
5871       default: llvm_unreachable("Illegal opcode!");
5872       case ARM::t2SXTH: NewOpc = ARM::tSXTH; break;
5873       case ARM::t2SXTB: NewOpc = ARM::tSXTB; break;
5874       case ARM::t2UXTH: NewOpc = ARM::tUXTH; break;
5875       case ARM::t2UXTB: NewOpc = ARM::tUXTB; break;
5876       }
5877       // The operands aren't the same for thumb1 (no rotate operand).
5878       MCInst TmpInst;
5879       TmpInst.setOpcode(NewOpc);
5880       TmpInst.addOperand(Inst.getOperand(0));
5881       TmpInst.addOperand(Inst.getOperand(1));
5882       TmpInst.addOperand(Inst.getOperand(3));
5883       TmpInst.addOperand(Inst.getOperand(4));
5884       Inst = TmpInst;
5885       return true;
5886     }
5887     break;
5888   }
5889   case ARM::t2IT: {
5890     // The mask bits for all but the first condition are represented as
5891     // the low bit of the condition code value implies 't'. We currently
5892     // always have 1 implies 't', so XOR toggle the bits if the low bit
5893     // of the condition code is zero. The encoding also expects the low
5894     // bit of the condition to be encoded as bit 4 of the mask operand,
5895     // so mask that in if needed
5896     MCOperand &MO = Inst.getOperand(1);
5897     unsigned Mask = MO.getImm();
5898     unsigned OrigMask = Mask;
5899     unsigned TZ = CountTrailingZeros_32(Mask);
5900     if ((Inst.getOperand(0).getImm() & 1) == 0) {
5901       assert(Mask && TZ <= 3 && "illegal IT mask value!");
5902       for (unsigned i = 3; i != TZ; --i)
5903         Mask ^= 1 << i;
5904     } else
5905       Mask |= 0x10;
5906     MO.setImm(Mask);
5907
5908     // Set up the IT block state according to the IT instruction we just
5909     // matched.
5910     assert(!inITBlock() && "nested IT blocks?!");
5911     ITState.Cond = ARMCC::CondCodes(Inst.getOperand(0).getImm());
5912     ITState.Mask = OrigMask; // Use the original mask, not the updated one.
5913     ITState.CurPosition = 0;
5914     ITState.FirstCond = true;
5915     break;
5916   }
5917   }
5918   return false;
5919 }
5920
5921 unsigned ARMAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
5922   // 16-bit thumb arithmetic instructions either require or preclude the 'S'
5923   // suffix depending on whether they're in an IT block or not.
5924   unsigned Opc = Inst.getOpcode();
5925   const MCInstrDesc &MCID = getInstDesc(Opc);
5926   if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
5927     assert(MCID.hasOptionalDef() &&
5928            "optionally flag setting instruction missing optional def operand");
5929     assert(MCID.NumOperands == Inst.getNumOperands() &&
5930            "operand count mismatch!");
5931     // Find the optional-def operand (cc_out).
5932     unsigned OpNo;
5933     for (OpNo = 0;
5934          !MCID.OpInfo[OpNo].isOptionalDef() && OpNo < MCID.NumOperands;
5935          ++OpNo)
5936       ;
5937     // If we're parsing Thumb1, reject it completely.
5938     if (isThumbOne() && Inst.getOperand(OpNo).getReg() != ARM::CPSR)
5939       return Match_MnemonicFail;
5940     // If we're parsing Thumb2, which form is legal depends on whether we're
5941     // in an IT block.
5942     if (isThumbTwo() && Inst.getOperand(OpNo).getReg() != ARM::CPSR &&
5943         !inITBlock())
5944       return Match_RequiresITBlock;
5945     if (isThumbTwo() && Inst.getOperand(OpNo).getReg() == ARM::CPSR &&
5946         inITBlock())
5947       return Match_RequiresNotITBlock;
5948   }
5949   // Some high-register supporting Thumb1 encodings only allow both registers
5950   // to be from r0-r7 when in Thumb2.
5951   else if (Opc == ARM::tADDhirr && isThumbOne() &&
5952            isARMLowRegister(Inst.getOperand(1).getReg()) &&
5953            isARMLowRegister(Inst.getOperand(2).getReg()))
5954     return Match_RequiresThumb2;
5955   // Others only require ARMv6 or later.
5956   else if (Opc == ARM::tMOVr && isThumbOne() && !hasV6Ops() &&
5957            isARMLowRegister(Inst.getOperand(0).getReg()) &&
5958            isARMLowRegister(Inst.getOperand(1).getReg()))
5959     return Match_RequiresV6;
5960   return Match_Success;
5961 }
5962
5963 bool ARMAsmParser::
5964 MatchAndEmitInstruction(SMLoc IDLoc,
5965                         SmallVectorImpl<MCParsedAsmOperand*> &Operands,
5966                         MCStreamer &Out) {
5967   MCInst Inst;
5968   unsigned ErrorInfo;
5969   unsigned MatchResult;
5970   MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
5971   switch (MatchResult) {
5972   default: break;
5973   case Match_Success:
5974     // Context sensitive operand constraints aren't handled by the matcher,
5975     // so check them here.
5976     if (validateInstruction(Inst, Operands)) {
5977       // Still progress the IT block, otherwise one wrong condition causes
5978       // nasty cascading errors.
5979       forwardITPosition();
5980       return true;
5981     }
5982
5983     // Some instructions need post-processing to, for example, tweak which
5984     // encoding is selected. Loop on it while changes happen so the
5985     // individual transformations can chain off each other. E.g.,
5986     // tPOP(r8)->t2LDMIA_UPD(sp,r8)->t2STR_POST(sp,r8)
5987     while (processInstruction(Inst, Operands))
5988       ;
5989
5990     // Only move forward at the very end so that everything in validate
5991     // and process gets a consistent answer about whether we're in an IT
5992     // block.
5993     forwardITPosition();
5994
5995     Out.EmitInstruction(Inst);
5996     return false;
5997   case Match_MissingFeature:
5998     Error(IDLoc, "instruction requires a CPU feature not currently enabled");
5999     return true;
6000   case Match_InvalidOperand: {
6001     SMLoc ErrorLoc = IDLoc;
6002     if (ErrorInfo != ~0U) {
6003       if (ErrorInfo >= Operands.size())
6004         return Error(IDLoc, "too few operands for instruction");
6005
6006       ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
6007       if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
6008     }
6009
6010     return Error(ErrorLoc, "invalid operand for instruction");
6011   }
6012   case Match_MnemonicFail:
6013     return Error(IDLoc, "invalid instruction");
6014   case Match_ConversionFail:
6015     // The converter function will have already emited a diagnostic.
6016     return true;
6017   case Match_RequiresNotITBlock:
6018     return Error(IDLoc, "flag setting instruction only valid outside IT block");
6019   case Match_RequiresITBlock:
6020     return Error(IDLoc, "instruction only valid inside IT block");
6021   case Match_RequiresV6:
6022     return Error(IDLoc, "instruction variant requires ARMv6 or later");
6023   case Match_RequiresThumb2:
6024     return Error(IDLoc, "instruction variant requires Thumb2");
6025   }
6026
6027   llvm_unreachable("Implement any new match types added!");
6028   return true;
6029 }
6030
6031 /// parseDirective parses the arm specific directives
6032 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
6033   StringRef IDVal = DirectiveID.getIdentifier();
6034   if (IDVal == ".word")
6035     return parseDirectiveWord(4, DirectiveID.getLoc());
6036   else if (IDVal == ".thumb")
6037     return parseDirectiveThumb(DirectiveID.getLoc());
6038   else if (IDVal == ".arm")
6039     return parseDirectiveARM(DirectiveID.getLoc());
6040   else if (IDVal == ".thumb_func")
6041     return parseDirectiveThumbFunc(DirectiveID.getLoc());
6042   else if (IDVal == ".code")
6043     return parseDirectiveCode(DirectiveID.getLoc());
6044   else if (IDVal == ".syntax")
6045     return parseDirectiveSyntax(DirectiveID.getLoc());
6046   else if (IDVal == ".unreq")
6047     return parseDirectiveUnreq(DirectiveID.getLoc());
6048   return true;
6049 }
6050
6051 /// parseDirectiveWord
6052 ///  ::= .word [ expression (, expression)* ]
6053 bool ARMAsmParser::parseDirectiveWord(unsigned Size, SMLoc L) {
6054   if (getLexer().isNot(AsmToken::EndOfStatement)) {
6055     for (;;) {
6056       const MCExpr *Value;
6057       if (getParser().ParseExpression(Value))
6058         return true;
6059
6060       getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
6061
6062       if (getLexer().is(AsmToken::EndOfStatement))
6063         break;
6064
6065       // FIXME: Improve diagnostic.
6066       if (getLexer().isNot(AsmToken::Comma))
6067         return Error(L, "unexpected token in directive");
6068       Parser.Lex();
6069     }
6070   }
6071
6072   Parser.Lex();
6073   return false;
6074 }
6075
6076 /// parseDirectiveThumb
6077 ///  ::= .thumb
6078 bool ARMAsmParser::parseDirectiveThumb(SMLoc L) {
6079   if (getLexer().isNot(AsmToken::EndOfStatement))
6080     return Error(L, "unexpected token in directive");
6081   Parser.Lex();
6082
6083   if (!isThumb())
6084     SwitchMode();
6085   getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
6086   return false;
6087 }
6088
6089 /// parseDirectiveARM
6090 ///  ::= .arm
6091 bool ARMAsmParser::parseDirectiveARM(SMLoc L) {
6092   if (getLexer().isNot(AsmToken::EndOfStatement))
6093     return Error(L, "unexpected token in directive");
6094   Parser.Lex();
6095
6096   if (isThumb())
6097     SwitchMode();
6098   getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
6099   return false;
6100 }
6101
6102 /// parseDirectiveThumbFunc
6103 ///  ::= .thumbfunc symbol_name
6104 bool ARMAsmParser::parseDirectiveThumbFunc(SMLoc L) {
6105   const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
6106   bool isMachO = MAI.hasSubsectionsViaSymbols();
6107   StringRef Name;
6108
6109   // Darwin asm has function name after .thumb_func direction
6110   // ELF doesn't
6111   if (isMachO) {
6112     const AsmToken &Tok = Parser.getTok();
6113     if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
6114       return Error(L, "unexpected token in .thumb_func directive");
6115     Name = Tok.getIdentifier();
6116     Parser.Lex(); // Consume the identifier token.
6117   }
6118
6119  if (getLexer().isNot(AsmToken::EndOfStatement))
6120     return Error(L, "unexpected token in directive");
6121   Parser.Lex();
6122
6123   // FIXME: assuming function name will be the line following .thumb_func
6124   if (!isMachO) {
6125     Name = Parser.getTok().getIdentifier();
6126   }
6127
6128   // Mark symbol as a thumb symbol.
6129   MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
6130   getParser().getStreamer().EmitThumbFunc(Func);
6131   return false;
6132 }
6133
6134 /// parseDirectiveSyntax
6135 ///  ::= .syntax unified | divided
6136 bool ARMAsmParser::parseDirectiveSyntax(SMLoc L) {
6137   const AsmToken &Tok = Parser.getTok();
6138   if (Tok.isNot(AsmToken::Identifier))
6139     return Error(L, "unexpected token in .syntax directive");
6140   StringRef Mode = Tok.getString();
6141   if (Mode == "unified" || Mode == "UNIFIED")
6142     Parser.Lex();
6143   else if (Mode == "divided" || Mode == "DIVIDED")
6144     return Error(L, "'.syntax divided' arm asssembly not supported");
6145   else
6146     return Error(L, "unrecognized syntax mode in .syntax directive");
6147
6148   if (getLexer().isNot(AsmToken::EndOfStatement))
6149     return Error(Parser.getTok().getLoc(), "unexpected token in directive");
6150   Parser.Lex();
6151
6152   // TODO tell the MC streamer the mode
6153   // getParser().getStreamer().Emit???();
6154   return false;
6155 }
6156
6157 /// parseDirectiveCode
6158 ///  ::= .code 16 | 32
6159 bool ARMAsmParser::parseDirectiveCode(SMLoc L) {
6160   const AsmToken &Tok = Parser.getTok();
6161   if (Tok.isNot(AsmToken::Integer))
6162     return Error(L, "unexpected token in .code directive");
6163   int64_t Val = Parser.getTok().getIntVal();
6164   if (Val == 16)
6165     Parser.Lex();
6166   else if (Val == 32)
6167     Parser.Lex();
6168   else
6169     return Error(L, "invalid operand to .code directive");
6170
6171   if (getLexer().isNot(AsmToken::EndOfStatement))
6172     return Error(Parser.getTok().getLoc(), "unexpected token in directive");
6173   Parser.Lex();
6174
6175   if (Val == 16) {
6176     if (!isThumb())
6177       SwitchMode();
6178     getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
6179   } else {
6180     if (isThumb())
6181       SwitchMode();
6182     getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
6183   }
6184
6185   return false;
6186 }
6187
6188 /// parseDirectiveReq
6189 ///  ::= name .req registername
6190 bool ARMAsmParser::parseDirectiveReq(StringRef Name, SMLoc L) {
6191   Parser.Lex(); // Eat the '.req' token.
6192   unsigned Reg;
6193   SMLoc SRegLoc, ERegLoc;
6194   if (ParseRegister(Reg, SRegLoc, ERegLoc)) {
6195     Parser.EatToEndOfStatement();
6196     return Error(SRegLoc, "register name expected");
6197   }
6198
6199   // Shouldn't be anything else.
6200   if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
6201     Parser.EatToEndOfStatement();
6202     return Error(Parser.getTok().getLoc(),
6203                  "unexpected input in .req directive.");
6204   }
6205
6206   Parser.Lex(); // Consume the EndOfStatement
6207
6208   if (RegisterReqs.GetOrCreateValue(Name, Reg).getValue() != Reg)
6209     return Error(SRegLoc, "redefinition of '" + Name +
6210                           "' does not match original.");
6211
6212   return false;
6213 }
6214
6215 /// parseDirectiveUneq
6216 ///  ::= .unreq registername
6217 bool ARMAsmParser::parseDirectiveUnreq(SMLoc L) {
6218   if (Parser.getTok().isNot(AsmToken::Identifier)) {
6219     Parser.EatToEndOfStatement();
6220     return Error(L, "unexpected input in .unreq directive.");
6221   }
6222   RegisterReqs.erase(Parser.getTok().getIdentifier());
6223   Parser.Lex(); // Eat the identifier.
6224   return false;
6225 }
6226
6227 extern "C" void LLVMInitializeARMAsmLexer();
6228
6229 /// Force static initialization.
6230 extern "C" void LLVMInitializeARMAsmParser() {
6231   RegisterMCAsmParser<ARMAsmParser> X(TheARMTarget);
6232   RegisterMCAsmParser<ARMAsmParser> Y(TheThumbTarget);
6233   LLVMInitializeARMAsmLexer();
6234 }
6235
6236 #define GET_REGISTER_MATCHER
6237 #define GET_MATCHER_IMPLEMENTATION
6238 #include "ARMGenAsmMatcher.inc"