ARM parse alignment specifier for NEON load/store instructions.
[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/StringExtras.h"
34 #include "llvm/ADT/StringSwitch.h"
35 #include "llvm/ADT/Twine.h"
36
37 using namespace llvm;
38
39 namespace {
40
41 class ARMOperand;
42
43 class ARMAsmParser : public MCTargetAsmParser {
44   MCSubtargetInfo &STI;
45   MCAsmParser &Parser;
46
47   struct {
48     ARMCC::CondCodes Cond;    // Condition for IT block.
49     unsigned Mask:4;          // Condition mask for instructions.
50                               // Starting at first 1 (from lsb).
51                               //   '1'  condition as indicated in IT.
52                               //   '0'  inverse of condition (else).
53                               // Count of instructions in IT block is
54                               // 4 - trailingzeroes(mask)
55
56     bool FirstCond;           // Explicit flag for when we're parsing the
57                               // First instruction in the IT block. It's
58                               // implied in the mask, so needs special
59                               // handling.
60
61     unsigned CurPosition;     // Current position in parsing of IT
62                               // block. In range [0,3]. Initialized
63                               // according to count of instructions in block.
64                               // ~0U if no active IT block.
65   } ITState;
66   bool inITBlock() { return ITState.CurPosition != ~0U;}
67   void forwardITPosition() {
68     if (!inITBlock()) return;
69     // Move to the next instruction in the IT block, if there is one. If not,
70     // mark the block as done.
71     unsigned TZ = CountTrailingZeros_32(ITState.Mask);
72     if (++ITState.CurPosition == 5 - TZ)
73       ITState.CurPosition = ~0U; // Done with the IT block after this.
74   }
75
76
77   MCAsmParser &getParser() const { return Parser; }
78   MCAsmLexer &getLexer() const { return Parser.getLexer(); }
79
80   void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
81   bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
82
83   int tryParseRegister();
84   bool tryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
85   int tryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
86   bool parseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
87   bool parseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
88   bool parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
89   bool parsePrefix(ARMMCExpr::VariantKind &RefKind);
90   bool parseMemRegOffsetShift(ARM_AM::ShiftOpc &ShiftType,
91                               unsigned &ShiftAmount);
92   bool parseDirectiveWord(unsigned Size, SMLoc L);
93   bool parseDirectiveThumb(SMLoc L);
94   bool parseDirectiveThumbFunc(SMLoc L);
95   bool parseDirectiveCode(SMLoc L);
96   bool parseDirectiveSyntax(SMLoc L);
97
98   StringRef splitMnemonic(StringRef Mnemonic, unsigned &PredicationCode,
99                           bool &CarrySetting, unsigned &ProcessorIMod,
100                           StringRef &ITMask);
101   void getMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
102                              bool &CanAcceptPredicationCode);
103
104   bool isThumb() const {
105     // FIXME: Can tablegen auto-generate this?
106     return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
107   }
108   bool isThumbOne() const {
109     return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) == 0;
110   }
111   bool isThumbTwo() const {
112     return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2);
113   }
114   bool hasV6Ops() const {
115     return STI.getFeatureBits() & ARM::HasV6Ops;
116   }
117   bool hasV7Ops() const {
118     return STI.getFeatureBits() & ARM::HasV7Ops;
119   }
120   void SwitchMode() {
121     unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
122     setAvailableFeatures(FB);
123   }
124   bool isMClass() const {
125     return STI.getFeatureBits() & ARM::FeatureMClass;
126   }
127
128   /// @name Auto-generated Match Functions
129   /// {
130
131 #define GET_ASSEMBLER_HEADER
132 #include "ARMGenAsmMatcher.inc"
133
134   /// }
135
136   OperandMatchResultTy parseITCondCode(SmallVectorImpl<MCParsedAsmOperand*>&);
137   OperandMatchResultTy parseCoprocNumOperand(
138     SmallVectorImpl<MCParsedAsmOperand*>&);
139   OperandMatchResultTy parseCoprocRegOperand(
140     SmallVectorImpl<MCParsedAsmOperand*>&);
141   OperandMatchResultTy parseMemBarrierOptOperand(
142     SmallVectorImpl<MCParsedAsmOperand*>&);
143   OperandMatchResultTy parseProcIFlagsOperand(
144     SmallVectorImpl<MCParsedAsmOperand*>&);
145   OperandMatchResultTy parseMSRMaskOperand(
146     SmallVectorImpl<MCParsedAsmOperand*>&);
147   OperandMatchResultTy parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &O,
148                                    StringRef Op, int Low, int High);
149   OperandMatchResultTy parsePKHLSLImm(SmallVectorImpl<MCParsedAsmOperand*> &O) {
150     return parsePKHImm(O, "lsl", 0, 31);
151   }
152   OperandMatchResultTy parsePKHASRImm(SmallVectorImpl<MCParsedAsmOperand*> &O) {
153     return parsePKHImm(O, "asr", 1, 32);
154   }
155   OperandMatchResultTy parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*>&);
156   OperandMatchResultTy parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*>&);
157   OperandMatchResultTy parseRotImm(SmallVectorImpl<MCParsedAsmOperand*>&);
158   OperandMatchResultTy parseBitfield(SmallVectorImpl<MCParsedAsmOperand*>&);
159   OperandMatchResultTy parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*>&);
160   OperandMatchResultTy parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*>&);
161   OperandMatchResultTy parseFPImm(SmallVectorImpl<MCParsedAsmOperand*>&);
162
163   // Asm Match Converter Methods
164   bool cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
165                     const SmallVectorImpl<MCParsedAsmOperand*> &);
166   bool cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
167                     const SmallVectorImpl<MCParsedAsmOperand*> &);
168   bool cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
169                                   const SmallVectorImpl<MCParsedAsmOperand*> &);
170   bool cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
171                                   const SmallVectorImpl<MCParsedAsmOperand*> &);
172   bool cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
173                                   const SmallVectorImpl<MCParsedAsmOperand*> &);
174   bool cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
175                                   const SmallVectorImpl<MCParsedAsmOperand*> &);
176   bool cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
177                                   const SmallVectorImpl<MCParsedAsmOperand*> &);
178   bool cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
179                                   const SmallVectorImpl<MCParsedAsmOperand*> &);
180   bool cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
181                                   const SmallVectorImpl<MCParsedAsmOperand*> &);
182   bool cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
183                              const SmallVectorImpl<MCParsedAsmOperand*> &);
184   bool cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
185                              const SmallVectorImpl<MCParsedAsmOperand*> &);
186   bool cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
187                              const SmallVectorImpl<MCParsedAsmOperand*> &);
188   bool cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
189                              const SmallVectorImpl<MCParsedAsmOperand*> &);
190   bool cvtLdrdPre(MCInst &Inst, unsigned Opcode,
191                   const SmallVectorImpl<MCParsedAsmOperand*> &);
192   bool cvtStrdPre(MCInst &Inst, unsigned Opcode,
193                   const SmallVectorImpl<MCParsedAsmOperand*> &);
194   bool cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
195                                   const SmallVectorImpl<MCParsedAsmOperand*> &);
196   bool cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
197                         const SmallVectorImpl<MCParsedAsmOperand*> &);
198
199   bool validateInstruction(MCInst &Inst,
200                            const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
201   void processInstruction(MCInst &Inst,
202                           const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
203   bool shouldOmitCCOutOperand(StringRef Mnemonic,
204                               SmallVectorImpl<MCParsedAsmOperand*> &Operands);
205
206 public:
207   enum ARMMatchResultTy {
208     Match_RequiresITBlock = FIRST_TARGET_MATCH_RESULT_TY,
209     Match_RequiresNotITBlock,
210     Match_RequiresV6,
211     Match_RequiresThumb2
212   };
213
214   ARMAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
215     : MCTargetAsmParser(), STI(_STI), Parser(_Parser) {
216     MCAsmParserExtension::Initialize(_Parser);
217
218     // Initialize the set of available features.
219     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
220
221     // Not in an ITBlock to start with.
222     ITState.CurPosition = ~0U;
223   }
224
225   // Implementation of the MCTargetAsmParser interface:
226   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
227   bool ParseInstruction(StringRef Name, SMLoc NameLoc,
228                         SmallVectorImpl<MCParsedAsmOperand*> &Operands);
229   bool ParseDirective(AsmToken DirectiveID);
230
231   unsigned checkTargetMatchPredicate(MCInst &Inst);
232
233   bool MatchAndEmitInstruction(SMLoc IDLoc,
234                                SmallVectorImpl<MCParsedAsmOperand*> &Operands,
235                                MCStreamer &Out);
236 };
237 } // end anonymous namespace
238
239 namespace {
240
241 /// ARMOperand - Instances of this class represent a parsed ARM machine
242 /// instruction.
243 class ARMOperand : public MCParsedAsmOperand {
244   enum KindTy {
245     k_CondCode,
246     k_CCOut,
247     k_ITCondMask,
248     k_CoprocNum,
249     k_CoprocReg,
250     k_Immediate,
251     k_FPImmediate,
252     k_MemBarrierOpt,
253     k_Memory,
254     k_PostIndexRegister,
255     k_MSRMask,
256     k_ProcIFlags,
257     k_VectorIndex,
258     k_Register,
259     k_RegisterList,
260     k_DPRRegisterList,
261     k_SPRRegisterList,
262     k_ShiftedRegister,
263     k_ShiftedImmediate,
264     k_ShifterImmediate,
265     k_RotateImmediate,
266     k_BitfieldDescriptor,
267     k_Token
268   } Kind;
269
270   SMLoc StartLoc, EndLoc;
271   SmallVector<unsigned, 8> Registers;
272
273   union {
274     struct {
275       ARMCC::CondCodes Val;
276     } CC;
277
278     struct {
279       unsigned Val;
280     } Cop;
281
282     struct {
283       unsigned Mask:4;
284     } ITMask;
285
286     struct {
287       ARM_MB::MemBOpt Val;
288     } MBOpt;
289
290     struct {
291       ARM_PROC::IFlags Val;
292     } IFlags;
293
294     struct {
295       unsigned Val;
296     } MMask;
297
298     struct {
299       const char *Data;
300       unsigned Length;
301     } Tok;
302
303     struct {
304       unsigned RegNum;
305     } Reg;
306
307     struct {
308       unsigned Val;
309     } VectorIndex;
310
311     struct {
312       const MCExpr *Val;
313     } Imm;
314
315     struct {
316       unsigned Val;       // encoded 8-bit representation
317     } FPImm;
318
319     /// Combined record for all forms of ARM address expressions.
320     struct {
321       unsigned BaseRegNum;
322       // Offset is in OffsetReg or OffsetImm. If both are zero, no offset
323       // was specified.
324       const MCConstantExpr *OffsetImm;  // Offset immediate value
325       unsigned OffsetRegNum;    // Offset register num, when OffsetImm == NULL
326       ARM_AM::ShiftOpc ShiftType; // Shift type for OffsetReg
327       unsigned ShiftImm;        // shift for OffsetReg.
328       unsigned Alignment;       // 0 = no alignment specified
329                                 // n = alignment in bytes (8, 16, or 32)
330       unsigned isNegative : 1;  // Negated OffsetReg? (~'U' bit)
331     } Memory;
332
333     struct {
334       unsigned RegNum;
335       bool isAdd;
336       ARM_AM::ShiftOpc ShiftTy;
337       unsigned ShiftImm;
338     } PostIdxReg;
339
340     struct {
341       bool isASR;
342       unsigned Imm;
343     } ShifterImm;
344     struct {
345       ARM_AM::ShiftOpc ShiftTy;
346       unsigned SrcReg;
347       unsigned ShiftReg;
348       unsigned ShiftImm;
349     } RegShiftedReg;
350     struct {
351       ARM_AM::ShiftOpc ShiftTy;
352       unsigned SrcReg;
353       unsigned ShiftImm;
354     } RegShiftedImm;
355     struct {
356       unsigned Imm;
357     } RotImm;
358     struct {
359       unsigned LSB;
360       unsigned Width;
361     } Bitfield;
362   };
363
364   ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
365 public:
366   ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
367     Kind = o.Kind;
368     StartLoc = o.StartLoc;
369     EndLoc = o.EndLoc;
370     switch (Kind) {
371     case k_CondCode:
372       CC = o.CC;
373       break;
374     case k_ITCondMask:
375       ITMask = o.ITMask;
376       break;
377     case k_Token:
378       Tok = o.Tok;
379       break;
380     case k_CCOut:
381     case k_Register:
382       Reg = o.Reg;
383       break;
384     case k_RegisterList:
385     case k_DPRRegisterList:
386     case k_SPRRegisterList:
387       Registers = o.Registers;
388       break;
389     case k_CoprocNum:
390     case k_CoprocReg:
391       Cop = o.Cop;
392       break;
393     case k_Immediate:
394       Imm = o.Imm;
395       break;
396     case k_FPImmediate:
397       FPImm = o.FPImm;
398       break;
399     case k_MemBarrierOpt:
400       MBOpt = o.MBOpt;
401       break;
402     case k_Memory:
403       Memory = o.Memory;
404       break;
405     case k_PostIndexRegister:
406       PostIdxReg = o.PostIdxReg;
407       break;
408     case k_MSRMask:
409       MMask = o.MMask;
410       break;
411     case k_ProcIFlags:
412       IFlags = o.IFlags;
413       break;
414     case k_ShifterImmediate:
415       ShifterImm = o.ShifterImm;
416       break;
417     case k_ShiftedRegister:
418       RegShiftedReg = o.RegShiftedReg;
419       break;
420     case k_ShiftedImmediate:
421       RegShiftedImm = o.RegShiftedImm;
422       break;
423     case k_RotateImmediate:
424       RotImm = o.RotImm;
425       break;
426     case k_BitfieldDescriptor:
427       Bitfield = o.Bitfield;
428       break;
429     case k_VectorIndex:
430       VectorIndex = o.VectorIndex;
431       break;
432     }
433   }
434
435   /// getStartLoc - Get the location of the first token of this operand.
436   SMLoc getStartLoc() const { return StartLoc; }
437   /// getEndLoc - Get the location of the last token of this operand.
438   SMLoc getEndLoc() const { return EndLoc; }
439
440   ARMCC::CondCodes getCondCode() const {
441     assert(Kind == k_CondCode && "Invalid access!");
442     return CC.Val;
443   }
444
445   unsigned getCoproc() const {
446     assert((Kind == k_CoprocNum || Kind == k_CoprocReg) && "Invalid access!");
447     return Cop.Val;
448   }
449
450   StringRef getToken() const {
451     assert(Kind == k_Token && "Invalid access!");
452     return StringRef(Tok.Data, Tok.Length);
453   }
454
455   unsigned getReg() const {
456     assert((Kind == k_Register || Kind == k_CCOut) && "Invalid access!");
457     return Reg.RegNum;
458   }
459
460   const SmallVectorImpl<unsigned> &getRegList() const {
461     assert((Kind == k_RegisterList || Kind == k_DPRRegisterList ||
462             Kind == k_SPRRegisterList) && "Invalid access!");
463     return Registers;
464   }
465
466   const MCExpr *getImm() const {
467     assert(Kind == k_Immediate && "Invalid access!");
468     return Imm.Val;
469   }
470
471   unsigned getFPImm() const {
472     assert(Kind == k_FPImmediate && "Invalid access!");
473     return FPImm.Val;
474   }
475
476   unsigned getVectorIndex() const {
477     assert(Kind == k_VectorIndex && "Invalid access!");
478     return VectorIndex.Val;
479   }
480
481   ARM_MB::MemBOpt getMemBarrierOpt() const {
482     assert(Kind == k_MemBarrierOpt && "Invalid access!");
483     return MBOpt.Val;
484   }
485
486   ARM_PROC::IFlags getProcIFlags() const {
487     assert(Kind == k_ProcIFlags && "Invalid access!");
488     return IFlags.Val;
489   }
490
491   unsigned getMSRMask() const {
492     assert(Kind == k_MSRMask && "Invalid access!");
493     return MMask.Val;
494   }
495
496   bool isCoprocNum() const { return Kind == k_CoprocNum; }
497   bool isCoprocReg() const { return Kind == k_CoprocReg; }
498   bool isCondCode() const { return Kind == k_CondCode; }
499   bool isCCOut() const { return Kind == k_CCOut; }
500   bool isITMask() const { return Kind == k_ITCondMask; }
501   bool isITCondCode() const { return Kind == k_CondCode; }
502   bool isImm() const { return Kind == k_Immediate; }
503   bool isFPImm() const { return Kind == k_FPImmediate; }
504   bool isImm8s4() const {
505     if (Kind != k_Immediate)
506       return false;
507     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
508     if (!CE) return false;
509     int64_t Value = CE->getValue();
510     return ((Value & 3) == 0) && Value >= -1020 && Value <= 1020;
511   }
512   bool isImm0_1020s4() const {
513     if (Kind != k_Immediate)
514       return false;
515     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
516     if (!CE) return false;
517     int64_t Value = CE->getValue();
518     return ((Value & 3) == 0) && Value >= 0 && Value <= 1020;
519   }
520   bool isImm0_508s4() const {
521     if (Kind != k_Immediate)
522       return false;
523     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
524     if (!CE) return false;
525     int64_t Value = CE->getValue();
526     return ((Value & 3) == 0) && Value >= 0 && Value <= 508;
527   }
528   bool isImm0_255() const {
529     if (Kind != k_Immediate)
530       return false;
531     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
532     if (!CE) return false;
533     int64_t Value = CE->getValue();
534     return Value >= 0 && Value < 256;
535   }
536   bool isImm0_7() const {
537     if (Kind != k_Immediate)
538       return false;
539     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
540     if (!CE) return false;
541     int64_t Value = CE->getValue();
542     return Value >= 0 && Value < 8;
543   }
544   bool isImm0_15() const {
545     if (Kind != k_Immediate)
546       return false;
547     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
548     if (!CE) return false;
549     int64_t Value = CE->getValue();
550     return Value >= 0 && Value < 16;
551   }
552   bool isImm0_31() const {
553     if (Kind != k_Immediate)
554       return false;
555     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
556     if (!CE) return false;
557     int64_t Value = CE->getValue();
558     return Value >= 0 && Value < 32;
559   }
560   bool isImm1_16() const {
561     if (Kind != k_Immediate)
562       return false;
563     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
564     if (!CE) return false;
565     int64_t Value = CE->getValue();
566     return Value > 0 && Value < 17;
567   }
568   bool isImm1_32() const {
569     if (Kind != k_Immediate)
570       return false;
571     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
572     if (!CE) return false;
573     int64_t Value = CE->getValue();
574     return Value > 0 && Value < 33;
575   }
576   bool isImm0_65535() const {
577     if (Kind != k_Immediate)
578       return false;
579     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
580     if (!CE) return false;
581     int64_t Value = CE->getValue();
582     return Value >= 0 && Value < 65536;
583   }
584   bool isImm0_65535Expr() const {
585     if (Kind != k_Immediate)
586       return false;
587     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
588     // If it's not a constant expression, it'll generate a fixup and be
589     // handled later.
590     if (!CE) return true;
591     int64_t Value = CE->getValue();
592     return Value >= 0 && Value < 65536;
593   }
594   bool isImm24bit() const {
595     if (Kind != k_Immediate)
596       return false;
597     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
598     if (!CE) return false;
599     int64_t Value = CE->getValue();
600     return Value >= 0 && Value <= 0xffffff;
601   }
602   bool isImmThumbSR() const {
603     if (Kind != k_Immediate)
604       return false;
605     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
606     if (!CE) return false;
607     int64_t Value = CE->getValue();
608     return Value > 0 && Value < 33;
609   }
610   bool isPKHLSLImm() const {
611     if (Kind != k_Immediate)
612       return false;
613     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
614     if (!CE) return false;
615     int64_t Value = CE->getValue();
616     return Value >= 0 && Value < 32;
617   }
618   bool isPKHASRImm() const {
619     if (Kind != k_Immediate)
620       return false;
621     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
622     if (!CE) return false;
623     int64_t Value = CE->getValue();
624     return Value > 0 && Value <= 32;
625   }
626   bool isARMSOImm() const {
627     if (Kind != k_Immediate)
628       return false;
629     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
630     if (!CE) return false;
631     int64_t Value = CE->getValue();
632     return ARM_AM::getSOImmVal(Value) != -1;
633   }
634   bool isT2SOImm() const {
635     if (Kind != k_Immediate)
636       return false;
637     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
638     if (!CE) return false;
639     int64_t Value = CE->getValue();
640     return ARM_AM::getT2SOImmVal(Value) != -1;
641   }
642   bool isSetEndImm() const {
643     if (Kind != k_Immediate)
644       return false;
645     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
646     if (!CE) return false;
647     int64_t Value = CE->getValue();
648     return Value == 1 || Value == 0;
649   }
650   bool isReg() const { return Kind == k_Register; }
651   bool isRegList() const { return Kind == k_RegisterList; }
652   bool isDPRRegList() const { return Kind == k_DPRRegisterList; }
653   bool isSPRRegList() const { return Kind == k_SPRRegisterList; }
654   bool isToken() const { return Kind == k_Token; }
655   bool isMemBarrierOpt() const { return Kind == k_MemBarrierOpt; }
656   bool isMemory() const { return Kind == k_Memory; }
657   bool isShifterImm() const { return Kind == k_ShifterImmediate; }
658   bool isRegShiftedReg() const { return Kind == k_ShiftedRegister; }
659   bool isRegShiftedImm() const { return Kind == k_ShiftedImmediate; }
660   bool isRotImm() const { return Kind == k_RotateImmediate; }
661   bool isBitfield() const { return Kind == k_BitfieldDescriptor; }
662   bool isPostIdxRegShifted() const { return Kind == k_PostIndexRegister; }
663   bool isPostIdxReg() const {
664     return Kind == k_PostIndexRegister && PostIdxReg.ShiftTy == ARM_AM::no_shift;
665   }
666   bool isMemNoOffset(bool alignOK = false) const {
667     if (!isMemory())
668       return false;
669     // No offset of any kind.
670     return Memory.OffsetRegNum == 0 && Memory.OffsetImm == 0 &&
671      (alignOK || Memory.Alignment == 0);
672   }
673   bool isAlignedMemory() const {
674     return isMemNoOffset(true);
675   }
676   bool isAddrMode2() const {
677     if (!isMemory() || Memory.Alignment != 0) return false;
678     // Check for register offset.
679     if (Memory.OffsetRegNum) return true;
680     // Immediate offset in range [-4095, 4095].
681     if (!Memory.OffsetImm) return true;
682     int64_t Val = Memory.OffsetImm->getValue();
683     return Val > -4096 && Val < 4096;
684   }
685   bool isAM2OffsetImm() const {
686     if (Kind != k_Immediate)
687       return false;
688     // Immediate offset in range [-4095, 4095].
689     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
690     if (!CE) return false;
691     int64_t Val = CE->getValue();
692     return Val > -4096 && Val < 4096;
693   }
694   bool isAddrMode3() const {
695     if (!isMemory() || Memory.Alignment != 0) return false;
696     // No shifts are legal for AM3.
697     if (Memory.ShiftType != ARM_AM::no_shift) return false;
698     // Check for register offset.
699     if (Memory.OffsetRegNum) return true;
700     // Immediate offset in range [-255, 255].
701     if (!Memory.OffsetImm) return true;
702     int64_t Val = Memory.OffsetImm->getValue();
703     return Val > -256 && Val < 256;
704   }
705   bool isAM3Offset() const {
706     if (Kind != k_Immediate && Kind != k_PostIndexRegister)
707       return false;
708     if (Kind == k_PostIndexRegister)
709       return PostIdxReg.ShiftTy == ARM_AM::no_shift;
710     // Immediate offset in range [-255, 255].
711     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
712     if (!CE) return false;
713     int64_t Val = CE->getValue();
714     // Special case, #-0 is INT32_MIN.
715     return (Val > -256 && Val < 256) || Val == INT32_MIN;
716   }
717   bool isAddrMode5() const {
718     if (!isMemory() || Memory.Alignment != 0) return false;
719     // Check for register offset.
720     if (Memory.OffsetRegNum) return false;
721     // Immediate offset in range [-1020, 1020] and a multiple of 4.
722     if (!Memory.OffsetImm) return true;
723     int64_t Val = Memory.OffsetImm->getValue();
724     return (Val >= -1020 && Val <= 1020 && ((Val & 3) == 0)) ||
725            Val == INT32_MIN;
726   }
727   bool isMemTBB() const {
728     if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
729         Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
730       return false;
731     return true;
732   }
733   bool isMemTBH() const {
734     if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
735         Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm != 1 ||
736         Memory.Alignment != 0 )
737       return false;
738     return true;
739   }
740   bool isMemRegOffset() const {
741     if (!isMemory() || !Memory.OffsetRegNum || Memory.Alignment != 0)
742       return false;
743     return true;
744   }
745   bool isT2MemRegOffset() const {
746     if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
747         Memory.Alignment != 0)
748       return false;
749     // Only lsl #{0, 1, 2, 3} allowed.
750     if (Memory.ShiftType == ARM_AM::no_shift)
751       return true;
752     if (Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm > 3)
753       return false;
754     return true;
755   }
756   bool isMemThumbRR() const {
757     // Thumb reg+reg addressing is simple. Just two registers, a base and
758     // an offset. No shifts, negations or any other complicating factors.
759     if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
760         Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
761       return false;
762     return isARMLowRegister(Memory.BaseRegNum) &&
763       (!Memory.OffsetRegNum || isARMLowRegister(Memory.OffsetRegNum));
764   }
765   bool isMemThumbRIs4() const {
766     if (!isMemory() || Memory.OffsetRegNum != 0 ||
767         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
768       return false;
769     // Immediate offset, multiple of 4 in range [0, 124].
770     if (!Memory.OffsetImm) return true;
771     int64_t Val = Memory.OffsetImm->getValue();
772     return Val >= 0 && Val <= 124 && (Val % 4) == 0;
773   }
774   bool isMemThumbRIs2() const {
775     if (!isMemory() || Memory.OffsetRegNum != 0 ||
776         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
777       return false;
778     // Immediate offset, multiple of 4 in range [0, 62].
779     if (!Memory.OffsetImm) return true;
780     int64_t Val = Memory.OffsetImm->getValue();
781     return Val >= 0 && Val <= 62 && (Val % 2) == 0;
782   }
783   bool isMemThumbRIs1() const {
784     if (!isMemory() || Memory.OffsetRegNum != 0 ||
785         !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
786       return false;
787     // Immediate offset in range [0, 31].
788     if (!Memory.OffsetImm) return true;
789     int64_t Val = Memory.OffsetImm->getValue();
790     return Val >= 0 && Val <= 31;
791   }
792   bool isMemThumbSPI() const {
793     if (!isMemory() || Memory.OffsetRegNum != 0 ||
794         Memory.BaseRegNum != ARM::SP || Memory.Alignment != 0)
795       return false;
796     // Immediate offset, multiple of 4 in range [0, 1020].
797     if (!Memory.OffsetImm) return true;
798     int64_t Val = Memory.OffsetImm->getValue();
799     return Val >= 0 && Val <= 1020 && (Val % 4) == 0;
800   }
801   bool isMemImm8s4Offset() const {
802     if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
803       return false;
804     // Immediate offset a multiple of 4 in range [-1020, 1020].
805     if (!Memory.OffsetImm) return true;
806     int64_t Val = Memory.OffsetImm->getValue();
807     return Val >= -1020 && Val <= 1020 && (Val & 3) == 0;
808   }
809   bool isMemImm0_1020s4Offset() const {
810     if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
811       return false;
812     // Immediate offset a multiple of 4 in range [0, 1020].
813     if (!Memory.OffsetImm) return true;
814     int64_t Val = Memory.OffsetImm->getValue();
815     return Val >= 0 && Val <= 1020 && (Val & 3) == 0;
816   }
817   bool isMemImm8Offset() const {
818     if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
819       return false;
820     // Immediate offset in range [-255, 255].
821     if (!Memory.OffsetImm) return true;
822     int64_t Val = Memory.OffsetImm->getValue();
823     return (Val == INT32_MIN) || (Val > -256 && Val < 256);
824   }
825   bool isMemPosImm8Offset() const {
826     if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
827       return false;
828     // Immediate offset in range [0, 255].
829     if (!Memory.OffsetImm) return true;
830     int64_t Val = Memory.OffsetImm->getValue();
831     return Val >= 0 && Val < 256;
832   }
833   bool isMemNegImm8Offset() const {
834     if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
835       return false;
836     // Immediate offset in range [-255, -1].
837     if (!Memory.OffsetImm) return true;
838     int64_t Val = Memory.OffsetImm->getValue();
839     return Val > -256 && Val < 0;
840   }
841   bool isMemUImm12Offset() const {
842     // If we have an immediate that's not a constant, treat it as a label
843     // reference needing a fixup. If it is a constant, it's something else
844     // and we reject it.
845     if (Kind == k_Immediate && !isa<MCConstantExpr>(getImm()))
846       return true;
847
848     if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
849       return false;
850     // Immediate offset in range [0, 4095].
851     if (!Memory.OffsetImm) return true;
852     int64_t Val = Memory.OffsetImm->getValue();
853     return (Val >= 0 && Val < 4096);
854   }
855   bool isMemImm12Offset() const {
856     // If we have an immediate that's not a constant, treat it as a label
857     // reference needing a fixup. If it is a constant, it's something else
858     // and we reject it.
859     if (Kind == k_Immediate && !isa<MCConstantExpr>(getImm()))
860       return true;
861
862     if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
863       return false;
864     // Immediate offset in range [-4095, 4095].
865     if (!Memory.OffsetImm) return true;
866     int64_t Val = Memory.OffsetImm->getValue();
867     return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
868   }
869   bool isPostIdxImm8() const {
870     if (Kind != k_Immediate)
871       return false;
872     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
873     if (!CE) return false;
874     int64_t Val = CE->getValue();
875     return (Val > -256 && Val < 256) || (Val == INT32_MIN);
876   }
877
878   bool isMSRMask() const { return Kind == k_MSRMask; }
879   bool isProcIFlags() const { return Kind == k_ProcIFlags; }
880
881   bool isVectorIndex8() const {
882     if (Kind != k_VectorIndex) return false;
883     return VectorIndex.Val < 8;
884   }
885   bool isVectorIndex16() const {
886     if (Kind != k_VectorIndex) return false;
887     return VectorIndex.Val < 4;
888   }
889   bool isVectorIndex32() const {
890     if (Kind != k_VectorIndex) return false;
891     return VectorIndex.Val < 2;
892   }
893
894
895
896   void addExpr(MCInst &Inst, const MCExpr *Expr) const {
897     // Add as immediates when possible.  Null MCExpr = 0.
898     if (Expr == 0)
899       Inst.addOperand(MCOperand::CreateImm(0));
900     else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
901       Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
902     else
903       Inst.addOperand(MCOperand::CreateExpr(Expr));
904   }
905
906   void addCondCodeOperands(MCInst &Inst, unsigned N) const {
907     assert(N == 2 && "Invalid number of operands!");
908     Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
909     unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
910     Inst.addOperand(MCOperand::CreateReg(RegNum));
911   }
912
913   void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
914     assert(N == 1 && "Invalid number of operands!");
915     Inst.addOperand(MCOperand::CreateImm(getCoproc()));
916   }
917
918   void addITMaskOperands(MCInst &Inst, unsigned N) const {
919     assert(N == 1 && "Invalid number of operands!");
920     Inst.addOperand(MCOperand::CreateImm(ITMask.Mask));
921   }
922
923   void addITCondCodeOperands(MCInst &Inst, unsigned N) const {
924     assert(N == 1 && "Invalid number of operands!");
925     Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
926   }
927
928   void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
929     assert(N == 1 && "Invalid number of operands!");
930     Inst.addOperand(MCOperand::CreateImm(getCoproc()));
931   }
932
933   void addCCOutOperands(MCInst &Inst, unsigned N) const {
934     assert(N == 1 && "Invalid number of operands!");
935     Inst.addOperand(MCOperand::CreateReg(getReg()));
936   }
937
938   void addRegOperands(MCInst &Inst, unsigned N) const {
939     assert(N == 1 && "Invalid number of operands!");
940     Inst.addOperand(MCOperand::CreateReg(getReg()));
941   }
942
943   void addRegShiftedRegOperands(MCInst &Inst, unsigned N) const {
944     assert(N == 3 && "Invalid number of operands!");
945     assert(isRegShiftedReg() && "addRegShiftedRegOperands() on non RegShiftedReg!");
946     Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.SrcReg));
947     Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.ShiftReg));
948     Inst.addOperand(MCOperand::CreateImm(
949       ARM_AM::getSORegOpc(RegShiftedReg.ShiftTy, RegShiftedReg.ShiftImm)));
950   }
951
952   void addRegShiftedImmOperands(MCInst &Inst, unsigned N) const {
953     assert(N == 2 && "Invalid number of operands!");
954     assert(isRegShiftedImm() && "addRegShiftedImmOperands() on non RegShiftedImm!");
955     Inst.addOperand(MCOperand::CreateReg(RegShiftedImm.SrcReg));
956     Inst.addOperand(MCOperand::CreateImm(
957       ARM_AM::getSORegOpc(RegShiftedImm.ShiftTy, RegShiftedImm.ShiftImm)));
958   }
959
960   void addShifterImmOperands(MCInst &Inst, unsigned N) const {
961     assert(N == 1 && "Invalid number of operands!");
962     Inst.addOperand(MCOperand::CreateImm((ShifterImm.isASR << 5) |
963                                          ShifterImm.Imm));
964   }
965
966   void addRegListOperands(MCInst &Inst, unsigned N) const {
967     assert(N == 1 && "Invalid number of operands!");
968     const SmallVectorImpl<unsigned> &RegList = getRegList();
969     for (SmallVectorImpl<unsigned>::const_iterator
970            I = RegList.begin(), E = RegList.end(); I != E; ++I)
971       Inst.addOperand(MCOperand::CreateReg(*I));
972   }
973
974   void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
975     addRegListOperands(Inst, N);
976   }
977
978   void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
979     addRegListOperands(Inst, N);
980   }
981
982   void addRotImmOperands(MCInst &Inst, unsigned N) const {
983     assert(N == 1 && "Invalid number of operands!");
984     // Encoded as val>>3. The printer handles display as 8, 16, 24.
985     Inst.addOperand(MCOperand::CreateImm(RotImm.Imm >> 3));
986   }
987
988   void addBitfieldOperands(MCInst &Inst, unsigned N) const {
989     assert(N == 1 && "Invalid number of operands!");
990     // Munge the lsb/width into a bitfield mask.
991     unsigned lsb = Bitfield.LSB;
992     unsigned width = Bitfield.Width;
993     // Make a 32-bit mask w/ the referenced bits clear and all other bits set.
994     uint32_t Mask = ~(((uint32_t)0xffffffff >> lsb) << (32 - width) >>
995                       (32 - (lsb + width)));
996     Inst.addOperand(MCOperand::CreateImm(Mask));
997   }
998
999   void addImmOperands(MCInst &Inst, unsigned N) const {
1000     assert(N == 1 && "Invalid number of operands!");
1001     addExpr(Inst, getImm());
1002   }
1003
1004   void addFPImmOperands(MCInst &Inst, unsigned N) const {
1005     assert(N == 1 && "Invalid number of operands!");
1006     Inst.addOperand(MCOperand::CreateImm(getFPImm()));
1007   }
1008
1009   void addImm8s4Operands(MCInst &Inst, unsigned N) const {
1010     assert(N == 1 && "Invalid number of operands!");
1011     // FIXME: We really want to scale the value here, but the LDRD/STRD
1012     // instruction don't encode operands that way yet.
1013     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1014     Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1015   }
1016
1017   void addImm0_1020s4Operands(MCInst &Inst, unsigned N) const {
1018     assert(N == 1 && "Invalid number of operands!");
1019     // The immediate is scaled by four in the encoding and is stored
1020     // in the MCInst as such. Lop off the low two bits here.
1021     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1022     Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1023   }
1024
1025   void addImm0_508s4Operands(MCInst &Inst, unsigned N) const {
1026     assert(N == 1 && "Invalid number of operands!");
1027     // The immediate is scaled by four in the encoding and is stored
1028     // in the MCInst as such. Lop off the low two bits here.
1029     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1030     Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1031   }
1032
1033   void addImm0_255Operands(MCInst &Inst, unsigned N) const {
1034     assert(N == 1 && "Invalid number of operands!");
1035     addExpr(Inst, getImm());
1036   }
1037
1038   void addImm0_7Operands(MCInst &Inst, unsigned N) const {
1039     assert(N == 1 && "Invalid number of operands!");
1040     addExpr(Inst, getImm());
1041   }
1042
1043   void addImm0_15Operands(MCInst &Inst, unsigned N) const {
1044     assert(N == 1 && "Invalid number of operands!");
1045     addExpr(Inst, getImm());
1046   }
1047
1048   void addImm0_31Operands(MCInst &Inst, unsigned N) const {
1049     assert(N == 1 && "Invalid number of operands!");
1050     addExpr(Inst, getImm());
1051   }
1052
1053   void addImm1_16Operands(MCInst &Inst, unsigned N) const {
1054     assert(N == 1 && "Invalid number of operands!");
1055     // The constant encodes as the immediate-1, and we store in the instruction
1056     // the bits as encoded, so subtract off one here.
1057     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1058     Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1059   }
1060
1061   void addImm1_32Operands(MCInst &Inst, unsigned N) const {
1062     assert(N == 1 && "Invalid number of operands!");
1063     // The constant encodes as the immediate-1, and we store in the instruction
1064     // the bits as encoded, so subtract off one here.
1065     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1066     Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1067   }
1068
1069   void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
1070     assert(N == 1 && "Invalid number of operands!");
1071     addExpr(Inst, getImm());
1072   }
1073
1074   void addImm0_65535ExprOperands(MCInst &Inst, unsigned N) const {
1075     assert(N == 1 && "Invalid number of operands!");
1076     addExpr(Inst, getImm());
1077   }
1078
1079   void addImm24bitOperands(MCInst &Inst, unsigned N) const {
1080     assert(N == 1 && "Invalid number of operands!");
1081     addExpr(Inst, getImm());
1082   }
1083
1084   void addImmThumbSROperands(MCInst &Inst, unsigned N) const {
1085     assert(N == 1 && "Invalid number of operands!");
1086     // The constant encodes as the immediate, except for 32, which encodes as
1087     // zero.
1088     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1089     unsigned Imm = CE->getValue();
1090     Inst.addOperand(MCOperand::CreateImm((Imm == 32 ? 0 : Imm)));
1091   }
1092
1093   void addPKHLSLImmOperands(MCInst &Inst, unsigned N) const {
1094     assert(N == 1 && "Invalid number of operands!");
1095     addExpr(Inst, getImm());
1096   }
1097
1098   void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
1099     assert(N == 1 && "Invalid number of operands!");
1100     // An ASR value of 32 encodes as 0, so that's how we want to add it to
1101     // the instruction as well.
1102     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1103     int Val = CE->getValue();
1104     Inst.addOperand(MCOperand::CreateImm(Val == 32 ? 0 : Val));
1105   }
1106
1107   void addARMSOImmOperands(MCInst &Inst, unsigned N) const {
1108     assert(N == 1 && "Invalid number of operands!");
1109     addExpr(Inst, getImm());
1110   }
1111
1112   void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
1113     assert(N == 1 && "Invalid number of operands!");
1114     addExpr(Inst, getImm());
1115   }
1116
1117   void addSetEndImmOperands(MCInst &Inst, unsigned N) const {
1118     assert(N == 1 && "Invalid number of operands!");
1119     addExpr(Inst, getImm());
1120   }
1121
1122   void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
1123     assert(N == 1 && "Invalid number of operands!");
1124     Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
1125   }
1126
1127   void addMemNoOffsetOperands(MCInst &Inst, unsigned N) const {
1128     assert(N == 1 && "Invalid number of operands!");
1129     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1130   }
1131
1132   void addAlignedMemoryOperands(MCInst &Inst, unsigned N) const {
1133     assert(N == 2 && "Invalid number of operands!");
1134     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1135     Inst.addOperand(MCOperand::CreateImm(Memory.Alignment));
1136   }
1137
1138   void addAddrMode2Operands(MCInst &Inst, unsigned N) const {
1139     assert(N == 3 && "Invalid number of operands!");
1140     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1141     if (!Memory.OffsetRegNum) {
1142       ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1143       // Special case for #-0
1144       if (Val == INT32_MIN) Val = 0;
1145       if (Val < 0) Val = -Val;
1146       Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1147     } else {
1148       // For register offset, we encode the shift type and negation flag
1149       // here.
1150       Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1151                               Memory.ShiftImm, Memory.ShiftType);
1152     }
1153     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1154     Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1155     Inst.addOperand(MCOperand::CreateImm(Val));
1156   }
1157
1158   void addAM2OffsetImmOperands(MCInst &Inst, unsigned N) const {
1159     assert(N == 2 && "Invalid number of operands!");
1160     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1161     assert(CE && "non-constant AM2OffsetImm operand!");
1162     int32_t Val = CE->getValue();
1163     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1164     // Special case for #-0
1165     if (Val == INT32_MIN) Val = 0;
1166     if (Val < 0) Val = -Val;
1167     Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1168     Inst.addOperand(MCOperand::CreateReg(0));
1169     Inst.addOperand(MCOperand::CreateImm(Val));
1170   }
1171
1172   void addAddrMode3Operands(MCInst &Inst, unsigned N) const {
1173     assert(N == 3 && "Invalid number of operands!");
1174     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1175     if (!Memory.OffsetRegNum) {
1176       ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1177       // Special case for #-0
1178       if (Val == INT32_MIN) Val = 0;
1179       if (Val < 0) Val = -Val;
1180       Val = ARM_AM::getAM3Opc(AddSub, Val);
1181     } else {
1182       // For register offset, we encode the shift type and negation flag
1183       // here.
1184       Val = ARM_AM::getAM3Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add, 0);
1185     }
1186     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1187     Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1188     Inst.addOperand(MCOperand::CreateImm(Val));
1189   }
1190
1191   void addAM3OffsetOperands(MCInst &Inst, unsigned N) const {
1192     assert(N == 2 && "Invalid number of operands!");
1193     if (Kind == k_PostIndexRegister) {
1194       int32_t Val =
1195         ARM_AM::getAM3Opc(PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub, 0);
1196       Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1197       Inst.addOperand(MCOperand::CreateImm(Val));
1198       return;
1199     }
1200
1201     // Constant offset.
1202     const MCConstantExpr *CE = static_cast<const MCConstantExpr*>(getImm());
1203     int32_t Val = CE->getValue();
1204     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1205     // Special case for #-0
1206     if (Val == INT32_MIN) Val = 0;
1207     if (Val < 0) Val = -Val;
1208     Val = ARM_AM::getAM3Opc(AddSub, Val);
1209     Inst.addOperand(MCOperand::CreateReg(0));
1210     Inst.addOperand(MCOperand::CreateImm(Val));
1211   }
1212
1213   void addAddrMode5Operands(MCInst &Inst, unsigned N) const {
1214     assert(N == 2 && "Invalid number of operands!");
1215     // The lower two bits are always zero and as such are not encoded.
1216     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1217     ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1218     // Special case for #-0
1219     if (Val == INT32_MIN) Val = 0;
1220     if (Val < 0) Val = -Val;
1221     Val = ARM_AM::getAM5Opc(AddSub, Val);
1222     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1223     Inst.addOperand(MCOperand::CreateImm(Val));
1224   }
1225
1226   void addMemImm8s4OffsetOperands(MCInst &Inst, unsigned N) const {
1227     assert(N == 2 && "Invalid number of operands!");
1228     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1229     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1230     Inst.addOperand(MCOperand::CreateImm(Val));
1231   }
1232
1233   void addMemImm0_1020s4OffsetOperands(MCInst &Inst, unsigned N) const {
1234     assert(N == 2 && "Invalid number of operands!");
1235     // The lower two bits are always zero and as such are not encoded.
1236     int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1237     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1238     Inst.addOperand(MCOperand::CreateImm(Val));
1239   }
1240
1241   void addMemImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1242     assert(N == 2 && "Invalid number of operands!");
1243     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1244     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1245     Inst.addOperand(MCOperand::CreateImm(Val));
1246   }
1247
1248   void addMemPosImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1249     addMemImm8OffsetOperands(Inst, N);
1250   }
1251
1252   void addMemNegImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1253     addMemImm8OffsetOperands(Inst, N);
1254   }
1255
1256   void addMemUImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1257     assert(N == 2 && "Invalid number of operands!");
1258     // If this is an immediate, it's a label reference.
1259     if (Kind == k_Immediate) {
1260       addExpr(Inst, getImm());
1261       Inst.addOperand(MCOperand::CreateImm(0));
1262       return;
1263     }
1264
1265     // Otherwise, it's a normal memory reg+offset.
1266     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1267     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1268     Inst.addOperand(MCOperand::CreateImm(Val));
1269   }
1270
1271   void addMemImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1272     assert(N == 2 && "Invalid number of operands!");
1273     // If this is an immediate, it's a label reference.
1274     if (Kind == k_Immediate) {
1275       addExpr(Inst, getImm());
1276       Inst.addOperand(MCOperand::CreateImm(0));
1277       return;
1278     }
1279
1280     // Otherwise, it's a normal memory reg+offset.
1281     int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1282     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1283     Inst.addOperand(MCOperand::CreateImm(Val));
1284   }
1285
1286   void addMemTBBOperands(MCInst &Inst, unsigned N) const {
1287     assert(N == 2 && "Invalid number of operands!");
1288     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1289     Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1290   }
1291
1292   void addMemTBHOperands(MCInst &Inst, unsigned N) const {
1293     assert(N == 2 && "Invalid number of operands!");
1294     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1295     Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1296   }
1297
1298   void addMemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1299     assert(N == 3 && "Invalid number of operands!");
1300     unsigned Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1301                                      Memory.ShiftImm, Memory.ShiftType);
1302     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1303     Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1304     Inst.addOperand(MCOperand::CreateImm(Val));
1305   }
1306
1307   void addT2MemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1308     assert(N == 3 && "Invalid number of operands!");
1309     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1310     Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1311     Inst.addOperand(MCOperand::CreateImm(Memory.ShiftImm));
1312   }
1313
1314   void addMemThumbRROperands(MCInst &Inst, unsigned N) const {
1315     assert(N == 2 && "Invalid number of operands!");
1316     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1317     Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1318   }
1319
1320   void addMemThumbRIs4Operands(MCInst &Inst, unsigned N) const {
1321     assert(N == 2 && "Invalid number of operands!");
1322     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1323     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1324     Inst.addOperand(MCOperand::CreateImm(Val));
1325   }
1326
1327   void addMemThumbRIs2Operands(MCInst &Inst, unsigned N) const {
1328     assert(N == 2 && "Invalid number of operands!");
1329     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 2) : 0;
1330     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1331     Inst.addOperand(MCOperand::CreateImm(Val));
1332   }
1333
1334   void addMemThumbRIs1Operands(MCInst &Inst, unsigned N) const {
1335     assert(N == 2 && "Invalid number of operands!");
1336     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue()) : 0;
1337     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1338     Inst.addOperand(MCOperand::CreateImm(Val));
1339   }
1340
1341   void addMemThumbSPIOperands(MCInst &Inst, unsigned N) const {
1342     assert(N == 2 && "Invalid number of operands!");
1343     int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1344     Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1345     Inst.addOperand(MCOperand::CreateImm(Val));
1346   }
1347
1348   void addPostIdxImm8Operands(MCInst &Inst, unsigned N) const {
1349     assert(N == 1 && "Invalid number of operands!");
1350     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1351     assert(CE && "non-constant post-idx-imm8 operand!");
1352     int Imm = CE->getValue();
1353     bool isAdd = Imm >= 0;
1354     if (Imm == INT32_MIN) Imm = 0;
1355     Imm = (Imm < 0 ? -Imm : Imm) | (int)isAdd << 8;
1356     Inst.addOperand(MCOperand::CreateImm(Imm));
1357   }
1358
1359   void addPostIdxRegOperands(MCInst &Inst, unsigned N) const {
1360     assert(N == 2 && "Invalid number of operands!");
1361     Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1362     Inst.addOperand(MCOperand::CreateImm(PostIdxReg.isAdd));
1363   }
1364
1365   void addPostIdxRegShiftedOperands(MCInst &Inst, unsigned N) const {
1366     assert(N == 2 && "Invalid number of operands!");
1367     Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1368     // The sign, shift type, and shift amount are encoded in a single operand
1369     // using the AM2 encoding helpers.
1370     ARM_AM::AddrOpc opc = PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub;
1371     unsigned Imm = ARM_AM::getAM2Opc(opc, PostIdxReg.ShiftImm,
1372                                      PostIdxReg.ShiftTy);
1373     Inst.addOperand(MCOperand::CreateImm(Imm));
1374   }
1375
1376   void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
1377     assert(N == 1 && "Invalid number of operands!");
1378     Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
1379   }
1380
1381   void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
1382     assert(N == 1 && "Invalid number of operands!");
1383     Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
1384   }
1385
1386   void addVectorIndex8Operands(MCInst &Inst, unsigned N) const {
1387     assert(N == 1 && "Invalid number of operands!");
1388     Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1389   }
1390
1391   void addVectorIndex16Operands(MCInst &Inst, unsigned N) const {
1392     assert(N == 1 && "Invalid number of operands!");
1393     Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1394   }
1395
1396   void addVectorIndex32Operands(MCInst &Inst, unsigned N) const {
1397     assert(N == 1 && "Invalid number of operands!");
1398     Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1399   }
1400
1401   virtual void print(raw_ostream &OS) const;
1402
1403   static ARMOperand *CreateITMask(unsigned Mask, SMLoc S) {
1404     ARMOperand *Op = new ARMOperand(k_ITCondMask);
1405     Op->ITMask.Mask = Mask;
1406     Op->StartLoc = S;
1407     Op->EndLoc = S;
1408     return Op;
1409   }
1410
1411   static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
1412     ARMOperand *Op = new ARMOperand(k_CondCode);
1413     Op->CC.Val = CC;
1414     Op->StartLoc = S;
1415     Op->EndLoc = S;
1416     return Op;
1417   }
1418
1419   static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
1420     ARMOperand *Op = new ARMOperand(k_CoprocNum);
1421     Op->Cop.Val = CopVal;
1422     Op->StartLoc = S;
1423     Op->EndLoc = S;
1424     return Op;
1425   }
1426
1427   static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
1428     ARMOperand *Op = new ARMOperand(k_CoprocReg);
1429     Op->Cop.Val = CopVal;
1430     Op->StartLoc = S;
1431     Op->EndLoc = S;
1432     return Op;
1433   }
1434
1435   static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
1436     ARMOperand *Op = new ARMOperand(k_CCOut);
1437     Op->Reg.RegNum = RegNum;
1438     Op->StartLoc = S;
1439     Op->EndLoc = S;
1440     return Op;
1441   }
1442
1443   static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
1444     ARMOperand *Op = new ARMOperand(k_Token);
1445     Op->Tok.Data = Str.data();
1446     Op->Tok.Length = Str.size();
1447     Op->StartLoc = S;
1448     Op->EndLoc = S;
1449     return Op;
1450   }
1451
1452   static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
1453     ARMOperand *Op = new ARMOperand(k_Register);
1454     Op->Reg.RegNum = RegNum;
1455     Op->StartLoc = S;
1456     Op->EndLoc = E;
1457     return Op;
1458   }
1459
1460   static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
1461                                            unsigned SrcReg,
1462                                            unsigned ShiftReg,
1463                                            unsigned ShiftImm,
1464                                            SMLoc S, SMLoc E) {
1465     ARMOperand *Op = new ARMOperand(k_ShiftedRegister);
1466     Op->RegShiftedReg.ShiftTy = ShTy;
1467     Op->RegShiftedReg.SrcReg = SrcReg;
1468     Op->RegShiftedReg.ShiftReg = ShiftReg;
1469     Op->RegShiftedReg.ShiftImm = ShiftImm;
1470     Op->StartLoc = S;
1471     Op->EndLoc = E;
1472     return Op;
1473   }
1474
1475   static ARMOperand *CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy,
1476                                             unsigned SrcReg,
1477                                             unsigned ShiftImm,
1478                                             SMLoc S, SMLoc E) {
1479     ARMOperand *Op = new ARMOperand(k_ShiftedImmediate);
1480     Op->RegShiftedImm.ShiftTy = ShTy;
1481     Op->RegShiftedImm.SrcReg = SrcReg;
1482     Op->RegShiftedImm.ShiftImm = ShiftImm;
1483     Op->StartLoc = S;
1484     Op->EndLoc = E;
1485     return Op;
1486   }
1487
1488   static ARMOperand *CreateShifterImm(bool isASR, unsigned Imm,
1489                                    SMLoc S, SMLoc E) {
1490     ARMOperand *Op = new ARMOperand(k_ShifterImmediate);
1491     Op->ShifterImm.isASR = isASR;
1492     Op->ShifterImm.Imm = Imm;
1493     Op->StartLoc = S;
1494     Op->EndLoc = E;
1495     return Op;
1496   }
1497
1498   static ARMOperand *CreateRotImm(unsigned Imm, SMLoc S, SMLoc E) {
1499     ARMOperand *Op = new ARMOperand(k_RotateImmediate);
1500     Op->RotImm.Imm = Imm;
1501     Op->StartLoc = S;
1502     Op->EndLoc = E;
1503     return Op;
1504   }
1505
1506   static ARMOperand *CreateBitfield(unsigned LSB, unsigned Width,
1507                                     SMLoc S, SMLoc E) {
1508     ARMOperand *Op = new ARMOperand(k_BitfieldDescriptor);
1509     Op->Bitfield.LSB = LSB;
1510     Op->Bitfield.Width = Width;
1511     Op->StartLoc = S;
1512     Op->EndLoc = E;
1513     return Op;
1514   }
1515
1516   static ARMOperand *
1517   CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
1518                 SMLoc StartLoc, SMLoc EndLoc) {
1519     KindTy Kind = k_RegisterList;
1520
1521     if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Regs.front().first))
1522       Kind = k_DPRRegisterList;
1523     else if (ARMMCRegisterClasses[ARM::SPRRegClassID].
1524              contains(Regs.front().first))
1525       Kind = k_SPRRegisterList;
1526
1527     ARMOperand *Op = new ARMOperand(Kind);
1528     for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
1529            I = Regs.begin(), E = Regs.end(); I != E; ++I)
1530       Op->Registers.push_back(I->first);
1531     array_pod_sort(Op->Registers.begin(), Op->Registers.end());
1532     Op->StartLoc = StartLoc;
1533     Op->EndLoc = EndLoc;
1534     return Op;
1535   }
1536
1537   static ARMOperand *CreateVectorIndex(unsigned Idx, SMLoc S, SMLoc E,
1538                                        MCContext &Ctx) {
1539     ARMOperand *Op = new ARMOperand(k_VectorIndex);
1540     Op->VectorIndex.Val = Idx;
1541     Op->StartLoc = S;
1542     Op->EndLoc = E;
1543     return Op;
1544   }
1545
1546   static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
1547     ARMOperand *Op = new ARMOperand(k_Immediate);
1548     Op->Imm.Val = Val;
1549     Op->StartLoc = S;
1550     Op->EndLoc = E;
1551     return Op;
1552   }
1553
1554   static ARMOperand *CreateFPImm(unsigned Val, SMLoc S, MCContext &Ctx) {
1555     ARMOperand *Op = new ARMOperand(k_FPImmediate);
1556     Op->FPImm.Val = Val;
1557     Op->StartLoc = S;
1558     Op->EndLoc = S;
1559     return Op;
1560   }
1561
1562   static ARMOperand *CreateMem(unsigned BaseRegNum,
1563                                const MCConstantExpr *OffsetImm,
1564                                unsigned OffsetRegNum,
1565                                ARM_AM::ShiftOpc ShiftType,
1566                                unsigned ShiftImm,
1567                                unsigned Alignment,
1568                                bool isNegative,
1569                                SMLoc S, SMLoc E) {
1570     ARMOperand *Op = new ARMOperand(k_Memory);
1571     Op->Memory.BaseRegNum = BaseRegNum;
1572     Op->Memory.OffsetImm = OffsetImm;
1573     Op->Memory.OffsetRegNum = OffsetRegNum;
1574     Op->Memory.ShiftType = ShiftType;
1575     Op->Memory.ShiftImm = ShiftImm;
1576     Op->Memory.Alignment = Alignment;
1577     Op->Memory.isNegative = isNegative;
1578     Op->StartLoc = S;
1579     Op->EndLoc = E;
1580     return Op;
1581   }
1582
1583   static ARMOperand *CreatePostIdxReg(unsigned RegNum, bool isAdd,
1584                                       ARM_AM::ShiftOpc ShiftTy,
1585                                       unsigned ShiftImm,
1586                                       SMLoc S, SMLoc E) {
1587     ARMOperand *Op = new ARMOperand(k_PostIndexRegister);
1588     Op->PostIdxReg.RegNum = RegNum;
1589     Op->PostIdxReg.isAdd = isAdd;
1590     Op->PostIdxReg.ShiftTy = ShiftTy;
1591     Op->PostIdxReg.ShiftImm = ShiftImm;
1592     Op->StartLoc = S;
1593     Op->EndLoc = E;
1594     return Op;
1595   }
1596
1597   static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
1598     ARMOperand *Op = new ARMOperand(k_MemBarrierOpt);
1599     Op->MBOpt.Val = Opt;
1600     Op->StartLoc = S;
1601     Op->EndLoc = S;
1602     return Op;
1603   }
1604
1605   static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
1606     ARMOperand *Op = new ARMOperand(k_ProcIFlags);
1607     Op->IFlags.Val = IFlags;
1608     Op->StartLoc = S;
1609     Op->EndLoc = S;
1610     return Op;
1611   }
1612
1613   static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
1614     ARMOperand *Op = new ARMOperand(k_MSRMask);
1615     Op->MMask.Val = MMask;
1616     Op->StartLoc = S;
1617     Op->EndLoc = S;
1618     return Op;
1619   }
1620 };
1621
1622 } // end anonymous namespace.
1623
1624 void ARMOperand::print(raw_ostream &OS) const {
1625   switch (Kind) {
1626   case k_FPImmediate:
1627     OS << "<fpimm " << getFPImm() << "(" << ARM_AM::getFPImmFloat(getFPImm())
1628        << ") >";
1629     break;
1630   case k_CondCode:
1631     OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
1632     break;
1633   case k_CCOut:
1634     OS << "<ccout " << getReg() << ">";
1635     break;
1636   case k_ITCondMask: {
1637     static char MaskStr[][6] = { "()", "(t)", "(e)", "(tt)", "(et)", "(te)",
1638       "(ee)", "(ttt)", "(ett)", "(tet)", "(eet)", "(tte)", "(ete)",
1639       "(tee)", "(eee)" };
1640     assert((ITMask.Mask & 0xf) == ITMask.Mask);
1641     OS << "<it-mask " << MaskStr[ITMask.Mask] << ">";
1642     break;
1643   }
1644   case k_CoprocNum:
1645     OS << "<coprocessor number: " << getCoproc() << ">";
1646     break;
1647   case k_CoprocReg:
1648     OS << "<coprocessor register: " << getCoproc() << ">";
1649     break;
1650   case k_MSRMask:
1651     OS << "<mask: " << getMSRMask() << ">";
1652     break;
1653   case k_Immediate:
1654     getImm()->print(OS);
1655     break;
1656   case k_MemBarrierOpt:
1657     OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
1658     break;
1659   case k_Memory:
1660     OS << "<memory "
1661        << " base:" << Memory.BaseRegNum;
1662     OS << ">";
1663     break;
1664   case k_PostIndexRegister:
1665     OS << "post-idx register " << (PostIdxReg.isAdd ? "" : "-")
1666        << PostIdxReg.RegNum;
1667     if (PostIdxReg.ShiftTy != ARM_AM::no_shift)
1668       OS << ARM_AM::getShiftOpcStr(PostIdxReg.ShiftTy) << " "
1669          << PostIdxReg.ShiftImm;
1670     OS << ">";
1671     break;
1672   case k_ProcIFlags: {
1673     OS << "<ARM_PROC::";
1674     unsigned IFlags = getProcIFlags();
1675     for (int i=2; i >= 0; --i)
1676       if (IFlags & (1 << i))
1677         OS << ARM_PROC::IFlagsToString(1 << i);
1678     OS << ">";
1679     break;
1680   }
1681   case k_Register:
1682     OS << "<register " << getReg() << ">";
1683     break;
1684   case k_ShifterImmediate:
1685     OS << "<shift " << (ShifterImm.isASR ? "asr" : "lsl")
1686        << " #" << ShifterImm.Imm << ">";
1687     break;
1688   case k_ShiftedRegister:
1689     OS << "<so_reg_reg "
1690        << RegShiftedReg.SrcReg
1691        << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(RegShiftedReg.ShiftImm))
1692        << ", " << RegShiftedReg.ShiftReg << ", "
1693        << ARM_AM::getSORegOffset(RegShiftedReg.ShiftImm)
1694        << ">";
1695     break;
1696   case k_ShiftedImmediate:
1697     OS << "<so_reg_imm "
1698        << RegShiftedImm.SrcReg
1699        << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(RegShiftedImm.ShiftImm))
1700        << ", " << ARM_AM::getSORegOffset(RegShiftedImm.ShiftImm)
1701        << ">";
1702     break;
1703   case k_RotateImmediate:
1704     OS << "<ror " << " #" << (RotImm.Imm * 8) << ">";
1705     break;
1706   case k_BitfieldDescriptor:
1707     OS << "<bitfield " << "lsb: " << Bitfield.LSB
1708        << ", width: " << Bitfield.Width << ">";
1709     break;
1710   case k_RegisterList:
1711   case k_DPRRegisterList:
1712   case k_SPRRegisterList: {
1713     OS << "<register_list ";
1714
1715     const SmallVectorImpl<unsigned> &RegList = getRegList();
1716     for (SmallVectorImpl<unsigned>::const_iterator
1717            I = RegList.begin(), E = RegList.end(); I != E; ) {
1718       OS << *I;
1719       if (++I < E) OS << ", ";
1720     }
1721
1722     OS << ">";
1723     break;
1724   }
1725   case k_Token:
1726     OS << "'" << getToken() << "'";
1727     break;
1728   case k_VectorIndex:
1729     OS << "<vectorindex " << getVectorIndex() << ">";
1730     break;
1731   }
1732 }
1733
1734 /// @name Auto-generated Match Functions
1735 /// {
1736
1737 static unsigned MatchRegisterName(StringRef Name);
1738
1739 /// }
1740
1741 bool ARMAsmParser::ParseRegister(unsigned &RegNo,
1742                                  SMLoc &StartLoc, SMLoc &EndLoc) {
1743   RegNo = tryParseRegister();
1744
1745   return (RegNo == (unsigned)-1);
1746 }
1747
1748 /// Try to parse a register name.  The token must be an Identifier when called,
1749 /// and if it is a register name the token is eaten and the register number is
1750 /// returned.  Otherwise return -1.
1751 ///
1752 int ARMAsmParser::tryParseRegister() {
1753   const AsmToken &Tok = Parser.getTok();
1754   if (Tok.isNot(AsmToken::Identifier)) return -1;
1755
1756   // FIXME: Validate register for the current architecture; we have to do
1757   // validation later, so maybe there is no need for this here.
1758   std::string upperCase = Tok.getString().str();
1759   std::string lowerCase = LowercaseString(upperCase);
1760   unsigned RegNum = MatchRegisterName(lowerCase);
1761   if (!RegNum) {
1762     RegNum = StringSwitch<unsigned>(lowerCase)
1763       .Case("r13", ARM::SP)
1764       .Case("r14", ARM::LR)
1765       .Case("r15", ARM::PC)
1766       .Case("ip", ARM::R12)
1767       .Default(0);
1768   }
1769   if (!RegNum) return -1;
1770
1771   Parser.Lex(); // Eat identifier token.
1772
1773 #if 0
1774   // Also check for an index operand. This is only legal for vector registers,
1775   // but that'll get caught OK in operand matching, so we don't need to
1776   // explicitly filter everything else out here.
1777   if (Parser.getTok().is(AsmToken::LBrac)) {
1778     SMLoc SIdx = Parser.getTok().getLoc();
1779     Parser.Lex(); // Eat left bracket token.
1780
1781     const MCExpr *ImmVal;
1782     SMLoc ExprLoc = Parser.getTok().getLoc();
1783     if (getParser().ParseExpression(ImmVal))
1784       return MatchOperand_ParseFail;
1785     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
1786     if (!MCE) {
1787       TokError("immediate value expected for vector index");
1788       return MatchOperand_ParseFail;
1789     }
1790
1791     SMLoc E = Parser.getTok().getLoc();
1792     if (Parser.getTok().isNot(AsmToken::RBrac)) {
1793       Error(E, "']' expected");
1794       return MatchOperand_ParseFail;
1795     }
1796
1797     Parser.Lex(); // Eat right bracket token.
1798
1799     Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
1800                                                      SIdx, E,
1801                                                      getContext()));
1802   }
1803 #endif
1804
1805   return RegNum;
1806 }
1807
1808 // Try to parse a shifter  (e.g., "lsl <amt>"). On success, return 0.
1809 // If a recoverable error occurs, return 1. If an irrecoverable error
1810 // occurs, return -1. An irrecoverable error is one where tokens have been
1811 // consumed in the process of trying to parse the shifter (i.e., when it is
1812 // indeed a shifter operand, but malformed).
1813 int ARMAsmParser::tryParseShiftRegister(
1814                                SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1815   SMLoc S = Parser.getTok().getLoc();
1816   const AsmToken &Tok = Parser.getTok();
1817   assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1818
1819   std::string upperCase = Tok.getString().str();
1820   std::string lowerCase = LowercaseString(upperCase);
1821   ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
1822       .Case("lsl", ARM_AM::lsl)
1823       .Case("lsr", ARM_AM::lsr)
1824       .Case("asr", ARM_AM::asr)
1825       .Case("ror", ARM_AM::ror)
1826       .Case("rrx", ARM_AM::rrx)
1827       .Default(ARM_AM::no_shift);
1828
1829   if (ShiftTy == ARM_AM::no_shift)
1830     return 1;
1831
1832   Parser.Lex(); // Eat the operator.
1833
1834   // The source register for the shift has already been added to the
1835   // operand list, so we need to pop it off and combine it into the shifted
1836   // register operand instead.
1837   OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
1838   if (!PrevOp->isReg())
1839     return Error(PrevOp->getStartLoc(), "shift must be of a register");
1840   int SrcReg = PrevOp->getReg();
1841   int64_t Imm = 0;
1842   int ShiftReg = 0;
1843   if (ShiftTy == ARM_AM::rrx) {
1844     // RRX Doesn't have an explicit shift amount. The encoder expects
1845     // the shift register to be the same as the source register. Seems odd,
1846     // but OK.
1847     ShiftReg = SrcReg;
1848   } else {
1849     // Figure out if this is shifted by a constant or a register (for non-RRX).
1850     if (Parser.getTok().is(AsmToken::Hash)) {
1851       Parser.Lex(); // Eat hash.
1852       SMLoc ImmLoc = Parser.getTok().getLoc();
1853       const MCExpr *ShiftExpr = 0;
1854       if (getParser().ParseExpression(ShiftExpr)) {
1855         Error(ImmLoc, "invalid immediate shift value");
1856         return -1;
1857       }
1858       // The expression must be evaluatable as an immediate.
1859       const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
1860       if (!CE) {
1861         Error(ImmLoc, "invalid immediate shift value");
1862         return -1;
1863       }
1864       // Range check the immediate.
1865       // lsl, ror: 0 <= imm <= 31
1866       // lsr, asr: 0 <= imm <= 32
1867       Imm = CE->getValue();
1868       if (Imm < 0 ||
1869           ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
1870           ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
1871         Error(ImmLoc, "immediate shift value out of range");
1872         return -1;
1873       }
1874     } else if (Parser.getTok().is(AsmToken::Identifier)) {
1875       ShiftReg = tryParseRegister();
1876       SMLoc L = Parser.getTok().getLoc();
1877       if (ShiftReg == -1) {
1878         Error (L, "expected immediate or register in shift operand");
1879         return -1;
1880       }
1881     } else {
1882       Error (Parser.getTok().getLoc(),
1883                     "expected immediate or register in shift operand");
1884       return -1;
1885     }
1886   }
1887
1888   if (ShiftReg && ShiftTy != ARM_AM::rrx)
1889     Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
1890                                                          ShiftReg, Imm,
1891                                                S, Parser.getTok().getLoc()));
1892   else
1893     Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
1894                                                S, Parser.getTok().getLoc()));
1895
1896   return 0;
1897 }
1898
1899
1900 /// Try to parse a register name.  The token must be an Identifier when called.
1901 /// If it's a register, an AsmOperand is created. Another AsmOperand is created
1902 /// if there is a "writeback". 'true' if it's not a register.
1903 ///
1904 /// TODO this is likely to change to allow different register types and or to
1905 /// parse for a specific register type.
1906 bool ARMAsmParser::
1907 tryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1908   SMLoc S = Parser.getTok().getLoc();
1909   int RegNo = tryParseRegister();
1910   if (RegNo == -1)
1911     return true;
1912
1913   Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
1914
1915   const AsmToken &ExclaimTok = Parser.getTok();
1916   if (ExclaimTok.is(AsmToken::Exclaim)) {
1917     Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1918                                                ExclaimTok.getLoc()));
1919     Parser.Lex(); // Eat exclaim token
1920     return false;
1921   }
1922
1923   // Also check for an index operand. This is only legal for vector registers,
1924   // but that'll get caught OK in operand matching, so we don't need to
1925   // explicitly filter everything else out here.
1926   if (Parser.getTok().is(AsmToken::LBrac)) {
1927     SMLoc SIdx = Parser.getTok().getLoc();
1928     Parser.Lex(); // Eat left bracket token.
1929
1930     const MCExpr *ImmVal;
1931     SMLoc ExprLoc = Parser.getTok().getLoc();
1932     if (getParser().ParseExpression(ImmVal))
1933       return MatchOperand_ParseFail;
1934     const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
1935     if (!MCE) {
1936       TokError("immediate value expected for vector index");
1937       return MatchOperand_ParseFail;
1938     }
1939
1940     SMLoc E = Parser.getTok().getLoc();
1941     if (Parser.getTok().isNot(AsmToken::RBrac)) {
1942       Error(E, "']' expected");
1943       return MatchOperand_ParseFail;
1944     }
1945
1946     Parser.Lex(); // Eat right bracket token.
1947
1948     Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
1949                                                      SIdx, E,
1950                                                      getContext()));
1951   }
1952
1953   return false;
1954 }
1955
1956 /// MatchCoprocessorOperandName - Try to parse an coprocessor related
1957 /// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
1958 /// "c5", ...
1959 static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
1960   // Use the same layout as the tablegen'erated register name matcher. Ugly,
1961   // but efficient.
1962   switch (Name.size()) {
1963   default: break;
1964   case 2:
1965     if (Name[0] != CoprocOp)
1966       return -1;
1967     switch (Name[1]) {
1968     default:  return -1;
1969     case '0': return 0;
1970     case '1': return 1;
1971     case '2': return 2;
1972     case '3': return 3;
1973     case '4': return 4;
1974     case '5': return 5;
1975     case '6': return 6;
1976     case '7': return 7;
1977     case '8': return 8;
1978     case '9': return 9;
1979     }
1980     break;
1981   case 3:
1982     if (Name[0] != CoprocOp || Name[1] != '1')
1983       return -1;
1984     switch (Name[2]) {
1985     default:  return -1;
1986     case '0': return 10;
1987     case '1': return 11;
1988     case '2': return 12;
1989     case '3': return 13;
1990     case '4': return 14;
1991     case '5': return 15;
1992     }
1993     break;
1994   }
1995
1996   return -1;
1997 }
1998
1999 /// parseITCondCode - Try to parse a condition code for an IT instruction.
2000 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2001 parseITCondCode(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2002   SMLoc S = Parser.getTok().getLoc();
2003   const AsmToken &Tok = Parser.getTok();
2004   if (!Tok.is(AsmToken::Identifier))
2005     return MatchOperand_NoMatch;
2006   unsigned CC = StringSwitch<unsigned>(Tok.getString())
2007     .Case("eq", ARMCC::EQ)
2008     .Case("ne", ARMCC::NE)
2009     .Case("hs", ARMCC::HS)
2010     .Case("cs", ARMCC::HS)
2011     .Case("lo", ARMCC::LO)
2012     .Case("cc", ARMCC::LO)
2013     .Case("mi", ARMCC::MI)
2014     .Case("pl", ARMCC::PL)
2015     .Case("vs", ARMCC::VS)
2016     .Case("vc", ARMCC::VC)
2017     .Case("hi", ARMCC::HI)
2018     .Case("ls", ARMCC::LS)
2019     .Case("ge", ARMCC::GE)
2020     .Case("lt", ARMCC::LT)
2021     .Case("gt", ARMCC::GT)
2022     .Case("le", ARMCC::LE)
2023     .Case("al", ARMCC::AL)
2024     .Default(~0U);
2025   if (CC == ~0U)
2026     return MatchOperand_NoMatch;
2027   Parser.Lex(); // Eat the token.
2028
2029   Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), S));
2030
2031   return MatchOperand_Success;
2032 }
2033
2034 /// parseCoprocNumOperand - Try to parse an coprocessor number operand. The
2035 /// token must be an Identifier when called, and if it is a coprocessor
2036 /// number, the token is eaten and the operand is added to the operand list.
2037 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2038 parseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2039   SMLoc S = Parser.getTok().getLoc();
2040   const AsmToken &Tok = Parser.getTok();
2041   assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2042
2043   int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
2044   if (Num == -1)
2045     return MatchOperand_NoMatch;
2046
2047   Parser.Lex(); // Eat identifier token.
2048   Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
2049   return MatchOperand_Success;
2050 }
2051
2052 /// parseCoprocRegOperand - Try to parse an coprocessor register operand. The
2053 /// token must be an Identifier when called, and if it is a coprocessor
2054 /// number, the token is eaten and the operand is added to the operand list.
2055 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2056 parseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2057   SMLoc S = Parser.getTok().getLoc();
2058   const AsmToken &Tok = Parser.getTok();
2059   assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2060
2061   int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
2062   if (Reg == -1)
2063     return MatchOperand_NoMatch;
2064
2065   Parser.Lex(); // Eat identifier token.
2066   Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
2067   return MatchOperand_Success;
2068 }
2069
2070 // For register list parsing, we need to map from raw GPR register numbering
2071 // to the enumeration values. The enumeration values aren't sorted by
2072 // register number due to our using "sp", "lr" and "pc" as canonical names.
2073 static unsigned getNextRegister(unsigned Reg) {
2074   // If this is a GPR, we need to do it manually, otherwise we can rely
2075   // on the sort ordering of the enumeration since the other reg-classes
2076   // are sane.
2077   if (!ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2078     return Reg + 1;
2079   switch(Reg) {
2080   default: assert(0 && "Invalid GPR number!");
2081   case ARM::R0:  return ARM::R1;  case ARM::R1:  return ARM::R2;
2082   case ARM::R2:  return ARM::R3;  case ARM::R3:  return ARM::R4;
2083   case ARM::R4:  return ARM::R5;  case ARM::R5:  return ARM::R6;
2084   case ARM::R6:  return ARM::R7;  case ARM::R7:  return ARM::R8;
2085   case ARM::R8:  return ARM::R9;  case ARM::R9:  return ARM::R10;
2086   case ARM::R10: return ARM::R11; case ARM::R11: return ARM::R12;
2087   case ARM::R12: return ARM::SP;  case ARM::SP:  return ARM::LR;
2088   case ARM::LR:  return ARM::PC;  case ARM::PC:  return ARM::R0;
2089   }
2090 }
2091
2092 /// Parse a register list.
2093 bool ARMAsmParser::
2094 parseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2095   assert(Parser.getTok().is(AsmToken::LCurly) &&
2096          "Token is not a Left Curly Brace");
2097   SMLoc S = Parser.getTok().getLoc();
2098   Parser.Lex(); // Eat '{' token.
2099   SMLoc RegLoc = Parser.getTok().getLoc();
2100
2101   // Check the first register in the list to see what register class
2102   // this is a list of.
2103   int Reg = tryParseRegister();
2104   if (Reg == -1)
2105     return Error(RegLoc, "register expected");
2106
2107   MCRegisterClass *RC;
2108   if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2109     RC = &ARMMCRegisterClasses[ARM::GPRRegClassID];
2110   else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg))
2111     RC = &ARMMCRegisterClasses[ARM::DPRRegClassID];
2112   else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg))
2113     RC = &ARMMCRegisterClasses[ARM::SPRRegClassID];
2114   else
2115     return Error(RegLoc, "invalid register in register list");
2116
2117   // The reglist instructions have at most 16 registers, so reserve
2118   // space for that many.
2119   SmallVector<std::pair<unsigned, SMLoc>, 16> Registers;
2120   // Store the first register.
2121   Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2122
2123   // This starts immediately after the first register token in the list,
2124   // so we can see either a comma or a minus (range separator) as a legal
2125   // next token.
2126   while (Parser.getTok().is(AsmToken::Comma) ||
2127          Parser.getTok().is(AsmToken::Minus)) {
2128     if (Parser.getTok().is(AsmToken::Minus)) {
2129       Parser.Lex(); // Eat the comma.
2130       SMLoc EndLoc = Parser.getTok().getLoc();
2131       int EndReg = tryParseRegister();
2132       if (EndReg == -1)
2133         return Error(EndLoc, "register expected");
2134       // If the register is the same as the start reg, there's nothing
2135       // more to do.
2136       if (Reg == EndReg)
2137         continue;
2138       // The register must be in the same register class as the first.
2139       if (!RC->contains(EndReg))
2140         return Error(EndLoc, "invalid register in register list");
2141       // Ranges must go from low to high.
2142       if (getARMRegisterNumbering(Reg) > getARMRegisterNumbering(EndReg))
2143         return Error(EndLoc, "bad range in register list");
2144
2145       // Add all the registers in the range to the register list.
2146       while (Reg != EndReg) {
2147         Reg = getNextRegister(Reg);
2148         Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2149       }
2150       continue;
2151     }
2152     Parser.Lex(); // Eat the comma.
2153     RegLoc = Parser.getTok().getLoc();
2154     int OldReg = Reg;
2155     Reg = tryParseRegister();
2156     if (Reg == -1)
2157       return Error(RegLoc, "register expected");
2158     // The register must be in the same register class as the first.
2159     if (!RC->contains(Reg))
2160       return Error(RegLoc, "invalid register in register list");
2161     // List must be monotonically increasing.
2162     if (getARMRegisterNumbering(Reg) <= getARMRegisterNumbering(OldReg))
2163       return Error(RegLoc, "register list not in ascending order");
2164     // VFP register lists must also be contiguous.
2165     // It's OK to use the enumeration values directly here rather, as the
2166     // VFP register classes have the enum sorted properly.
2167     if (RC != &ARMMCRegisterClasses[ARM::GPRRegClassID] &&
2168         Reg != OldReg + 1)
2169       return Error(RegLoc, "non-contiguous register range");
2170     Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2171   }
2172
2173   SMLoc E = Parser.getTok().getLoc();
2174   if (Parser.getTok().isNot(AsmToken::RCurly))
2175     return Error(E, "'}' expected");
2176   Parser.Lex(); // Eat '}' token.
2177
2178   Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
2179   return false;
2180 }
2181
2182 /// parseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
2183 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2184 parseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2185   SMLoc S = Parser.getTok().getLoc();
2186   const AsmToken &Tok = Parser.getTok();
2187   assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2188   StringRef OptStr = Tok.getString();
2189
2190   unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
2191     .Case("sy",    ARM_MB::SY)
2192     .Case("st",    ARM_MB::ST)
2193     .Case("sh",    ARM_MB::ISH)
2194     .Case("ish",   ARM_MB::ISH)
2195     .Case("shst",  ARM_MB::ISHST)
2196     .Case("ishst", ARM_MB::ISHST)
2197     .Case("nsh",   ARM_MB::NSH)
2198     .Case("un",    ARM_MB::NSH)
2199     .Case("nshst", ARM_MB::NSHST)
2200     .Case("unst",  ARM_MB::NSHST)
2201     .Case("osh",   ARM_MB::OSH)
2202     .Case("oshst", ARM_MB::OSHST)
2203     .Default(~0U);
2204
2205   if (Opt == ~0U)
2206     return MatchOperand_NoMatch;
2207
2208   Parser.Lex(); // Eat identifier token.
2209   Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
2210   return MatchOperand_Success;
2211 }
2212
2213 /// parseProcIFlagsOperand - Try to parse iflags from CPS instruction.
2214 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2215 parseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2216   SMLoc S = Parser.getTok().getLoc();
2217   const AsmToken &Tok = Parser.getTok();
2218   assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2219   StringRef IFlagsStr = Tok.getString();
2220
2221   // An iflags string of "none" is interpreted to mean that none of the AIF
2222   // bits are set.  Not a terribly useful instruction, but a valid encoding.
2223   unsigned IFlags = 0;
2224   if (IFlagsStr != "none") {
2225         for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
2226       unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
2227         .Case("a", ARM_PROC::A)
2228         .Case("i", ARM_PROC::I)
2229         .Case("f", ARM_PROC::F)
2230         .Default(~0U);
2231
2232       // If some specific iflag is already set, it means that some letter is
2233       // present more than once, this is not acceptable.
2234       if (Flag == ~0U || (IFlags & Flag))
2235         return MatchOperand_NoMatch;
2236
2237       IFlags |= Flag;
2238     }
2239   }
2240
2241   Parser.Lex(); // Eat identifier token.
2242   Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
2243   return MatchOperand_Success;
2244 }
2245
2246 /// parseMSRMaskOperand - Try to parse mask flags from MSR instruction.
2247 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2248 parseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2249   SMLoc S = Parser.getTok().getLoc();
2250   const AsmToken &Tok = Parser.getTok();
2251   assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2252   StringRef Mask = Tok.getString();
2253
2254   if (isMClass()) {
2255     // See ARMv6-M 10.1.1
2256     unsigned FlagsVal = StringSwitch<unsigned>(Mask)
2257       .Case("apsr", 0)
2258       .Case("iapsr", 1)
2259       .Case("eapsr", 2)
2260       .Case("xpsr", 3)
2261       .Case("ipsr", 5)
2262       .Case("epsr", 6)
2263       .Case("iepsr", 7)
2264       .Case("msp", 8)
2265       .Case("psp", 9)
2266       .Case("primask", 16)
2267       .Case("basepri", 17)
2268       .Case("basepri_max", 18)
2269       .Case("faultmask", 19)
2270       .Case("control", 20)
2271       .Default(~0U);
2272     
2273     if (FlagsVal == ~0U)
2274       return MatchOperand_NoMatch;
2275
2276     if (!hasV7Ops() && FlagsVal >= 17 && FlagsVal <= 19)
2277       // basepri, basepri_max and faultmask only valid for V7m.
2278       return MatchOperand_NoMatch;
2279     
2280     Parser.Lex(); // Eat identifier token.
2281     Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
2282     return MatchOperand_Success;
2283   }
2284
2285   // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
2286   size_t Start = 0, Next = Mask.find('_');
2287   StringRef Flags = "";
2288   std::string SpecReg = LowercaseString(Mask.slice(Start, Next));
2289   if (Next != StringRef::npos)
2290     Flags = Mask.slice(Next+1, Mask.size());
2291
2292   // FlagsVal contains the complete mask:
2293   // 3-0: Mask
2294   // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
2295   unsigned FlagsVal = 0;
2296
2297   if (SpecReg == "apsr") {
2298     FlagsVal = StringSwitch<unsigned>(Flags)
2299     .Case("nzcvq",  0x8) // same as CPSR_f
2300     .Case("g",      0x4) // same as CPSR_s
2301     .Case("nzcvqg", 0xc) // same as CPSR_fs
2302     .Default(~0U);
2303
2304     if (FlagsVal == ~0U) {
2305       if (!Flags.empty())
2306         return MatchOperand_NoMatch;
2307       else
2308         FlagsVal = 8; // No flag
2309     }
2310   } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
2311     if (Flags == "all") // cpsr_all is an alias for cpsr_fc
2312       Flags = "fc";
2313     for (int i = 0, e = Flags.size(); i != e; ++i) {
2314       unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
2315       .Case("c", 1)
2316       .Case("x", 2)
2317       .Case("s", 4)
2318       .Case("f", 8)
2319       .Default(~0U);
2320
2321       // If some specific flag is already set, it means that some letter is
2322       // present more than once, this is not acceptable.
2323       if (FlagsVal == ~0U || (FlagsVal & Flag))
2324         return MatchOperand_NoMatch;
2325       FlagsVal |= Flag;
2326     }
2327   } else // No match for special register.
2328     return MatchOperand_NoMatch;
2329
2330   // Special register without flags are equivalent to "fc" flags.
2331   if (!FlagsVal)
2332     FlagsVal = 0x9;
2333
2334   // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
2335   if (SpecReg == "spsr")
2336     FlagsVal |= 16;
2337
2338   Parser.Lex(); // Eat identifier token.
2339   Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
2340   return MatchOperand_Success;
2341 }
2342
2343 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2344 parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands, StringRef Op,
2345             int Low, int High) {
2346   const AsmToken &Tok = Parser.getTok();
2347   if (Tok.isNot(AsmToken::Identifier)) {
2348     Error(Parser.getTok().getLoc(), Op + " operand expected.");
2349     return MatchOperand_ParseFail;
2350   }
2351   StringRef ShiftName = Tok.getString();
2352   std::string LowerOp = LowercaseString(Op);
2353   std::string UpperOp = UppercaseString(Op);
2354   if (ShiftName != LowerOp && ShiftName != UpperOp) {
2355     Error(Parser.getTok().getLoc(), Op + " operand expected.");
2356     return MatchOperand_ParseFail;
2357   }
2358   Parser.Lex(); // Eat shift type token.
2359
2360   // There must be a '#' and a shift amount.
2361   if (Parser.getTok().isNot(AsmToken::Hash)) {
2362     Error(Parser.getTok().getLoc(), "'#' expected");
2363     return MatchOperand_ParseFail;
2364   }
2365   Parser.Lex(); // Eat hash token.
2366
2367   const MCExpr *ShiftAmount;
2368   SMLoc Loc = Parser.getTok().getLoc();
2369   if (getParser().ParseExpression(ShiftAmount)) {
2370     Error(Loc, "illegal expression");
2371     return MatchOperand_ParseFail;
2372   }
2373   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2374   if (!CE) {
2375     Error(Loc, "constant expression expected");
2376     return MatchOperand_ParseFail;
2377   }
2378   int Val = CE->getValue();
2379   if (Val < Low || Val > High) {
2380     Error(Loc, "immediate value out of range");
2381     return MatchOperand_ParseFail;
2382   }
2383
2384   Operands.push_back(ARMOperand::CreateImm(CE, Loc, Parser.getTok().getLoc()));
2385
2386   return MatchOperand_Success;
2387 }
2388
2389 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2390 parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2391   const AsmToken &Tok = Parser.getTok();
2392   SMLoc S = Tok.getLoc();
2393   if (Tok.isNot(AsmToken::Identifier)) {
2394     Error(Tok.getLoc(), "'be' or 'le' operand expected");
2395     return MatchOperand_ParseFail;
2396   }
2397   int Val = StringSwitch<int>(Tok.getString())
2398     .Case("be", 1)
2399     .Case("le", 0)
2400     .Default(-1);
2401   Parser.Lex(); // Eat the token.
2402
2403   if (Val == -1) {
2404     Error(Tok.getLoc(), "'be' or 'le' operand expected");
2405     return MatchOperand_ParseFail;
2406   }
2407   Operands.push_back(ARMOperand::CreateImm(MCConstantExpr::Create(Val,
2408                                                                   getContext()),
2409                                            S, Parser.getTok().getLoc()));
2410   return MatchOperand_Success;
2411 }
2412
2413 /// parseShifterImm - Parse the shifter immediate operand for SSAT/USAT
2414 /// instructions. Legal values are:
2415 ///     lsl #n  'n' in [0,31]
2416 ///     asr #n  'n' in [1,32]
2417 ///             n == 32 encoded as n == 0.
2418 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2419 parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2420   const AsmToken &Tok = Parser.getTok();
2421   SMLoc S = Tok.getLoc();
2422   if (Tok.isNot(AsmToken::Identifier)) {
2423     Error(S, "shift operator 'asr' or 'lsl' expected");
2424     return MatchOperand_ParseFail;
2425   }
2426   StringRef ShiftName = Tok.getString();
2427   bool isASR;
2428   if (ShiftName == "lsl" || ShiftName == "LSL")
2429     isASR = false;
2430   else if (ShiftName == "asr" || ShiftName == "ASR")
2431     isASR = true;
2432   else {
2433     Error(S, "shift operator 'asr' or 'lsl' expected");
2434     return MatchOperand_ParseFail;
2435   }
2436   Parser.Lex(); // Eat the operator.
2437
2438   // A '#' and a shift amount.
2439   if (Parser.getTok().isNot(AsmToken::Hash)) {
2440     Error(Parser.getTok().getLoc(), "'#' expected");
2441     return MatchOperand_ParseFail;
2442   }
2443   Parser.Lex(); // Eat hash token.
2444
2445   const MCExpr *ShiftAmount;
2446   SMLoc E = Parser.getTok().getLoc();
2447   if (getParser().ParseExpression(ShiftAmount)) {
2448     Error(E, "malformed shift expression");
2449     return MatchOperand_ParseFail;
2450   }
2451   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2452   if (!CE) {
2453     Error(E, "shift amount must be an immediate");
2454     return MatchOperand_ParseFail;
2455   }
2456
2457   int64_t Val = CE->getValue();
2458   if (isASR) {
2459     // Shift amount must be in [1,32]
2460     if (Val < 1 || Val > 32) {
2461       Error(E, "'asr' shift amount must be in range [1,32]");
2462       return MatchOperand_ParseFail;
2463     }
2464     // asr #32 encoded as asr #0, but is not allowed in Thumb2 mode.
2465     if (isThumb() && Val == 32) {
2466       Error(E, "'asr #32' shift amount not allowed in Thumb mode");
2467       return MatchOperand_ParseFail;
2468     }
2469     if (Val == 32) Val = 0;
2470   } else {
2471     // Shift amount must be in [1,32]
2472     if (Val < 0 || Val > 31) {
2473       Error(E, "'lsr' shift amount must be in range [0,31]");
2474       return MatchOperand_ParseFail;
2475     }
2476   }
2477
2478   E = Parser.getTok().getLoc();
2479   Operands.push_back(ARMOperand::CreateShifterImm(isASR, Val, S, E));
2480
2481   return MatchOperand_Success;
2482 }
2483
2484 /// parseRotImm - Parse the shifter immediate operand for SXTB/UXTB family
2485 /// of instructions. Legal values are:
2486 ///     ror #n  'n' in {0, 8, 16, 24}
2487 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2488 parseRotImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2489   const AsmToken &Tok = Parser.getTok();
2490   SMLoc S = Tok.getLoc();
2491   if (Tok.isNot(AsmToken::Identifier))
2492     return MatchOperand_NoMatch;
2493   StringRef ShiftName = Tok.getString();
2494   if (ShiftName != "ror" && ShiftName != "ROR")
2495     return MatchOperand_NoMatch;
2496   Parser.Lex(); // Eat the operator.
2497
2498   // A '#' and a rotate amount.
2499   if (Parser.getTok().isNot(AsmToken::Hash)) {
2500     Error(Parser.getTok().getLoc(), "'#' expected");
2501     return MatchOperand_ParseFail;
2502   }
2503   Parser.Lex(); // Eat hash token.
2504
2505   const MCExpr *ShiftAmount;
2506   SMLoc E = Parser.getTok().getLoc();
2507   if (getParser().ParseExpression(ShiftAmount)) {
2508     Error(E, "malformed rotate expression");
2509     return MatchOperand_ParseFail;
2510   }
2511   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2512   if (!CE) {
2513     Error(E, "rotate amount must be an immediate");
2514     return MatchOperand_ParseFail;
2515   }
2516
2517   int64_t Val = CE->getValue();
2518   // Shift amount must be in {0, 8, 16, 24} (0 is undocumented extension)
2519   // normally, zero is represented in asm by omitting the rotate operand
2520   // entirely.
2521   if (Val != 8 && Val != 16 && Val != 24 && Val != 0) {
2522     Error(E, "'ror' rotate amount must be 8, 16, or 24");
2523     return MatchOperand_ParseFail;
2524   }
2525
2526   E = Parser.getTok().getLoc();
2527   Operands.push_back(ARMOperand::CreateRotImm(Val, S, E));
2528
2529   return MatchOperand_Success;
2530 }
2531
2532 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2533 parseBitfield(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2534   SMLoc S = Parser.getTok().getLoc();
2535   // The bitfield descriptor is really two operands, the LSB and the width.
2536   if (Parser.getTok().isNot(AsmToken::Hash)) {
2537     Error(Parser.getTok().getLoc(), "'#' expected");
2538     return MatchOperand_ParseFail;
2539   }
2540   Parser.Lex(); // Eat hash token.
2541
2542   const MCExpr *LSBExpr;
2543   SMLoc E = Parser.getTok().getLoc();
2544   if (getParser().ParseExpression(LSBExpr)) {
2545     Error(E, "malformed immediate expression");
2546     return MatchOperand_ParseFail;
2547   }
2548   const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LSBExpr);
2549   if (!CE) {
2550     Error(E, "'lsb' operand must be an immediate");
2551     return MatchOperand_ParseFail;
2552   }
2553
2554   int64_t LSB = CE->getValue();
2555   // The LSB must be in the range [0,31]
2556   if (LSB < 0 || LSB > 31) {
2557     Error(E, "'lsb' operand must be in the range [0,31]");
2558     return MatchOperand_ParseFail;
2559   }
2560   E = Parser.getTok().getLoc();
2561
2562   // Expect another immediate operand.
2563   if (Parser.getTok().isNot(AsmToken::Comma)) {
2564     Error(Parser.getTok().getLoc(), "too few operands");
2565     return MatchOperand_ParseFail;
2566   }
2567   Parser.Lex(); // Eat hash token.
2568   if (Parser.getTok().isNot(AsmToken::Hash)) {
2569     Error(Parser.getTok().getLoc(), "'#' expected");
2570     return MatchOperand_ParseFail;
2571   }
2572   Parser.Lex(); // Eat hash token.
2573
2574   const MCExpr *WidthExpr;
2575   if (getParser().ParseExpression(WidthExpr)) {
2576     Error(E, "malformed immediate expression");
2577     return MatchOperand_ParseFail;
2578   }
2579   CE = dyn_cast<MCConstantExpr>(WidthExpr);
2580   if (!CE) {
2581     Error(E, "'width' operand must be an immediate");
2582     return MatchOperand_ParseFail;
2583   }
2584
2585   int64_t Width = CE->getValue();
2586   // The LSB must be in the range [1,32-lsb]
2587   if (Width < 1 || Width > 32 - LSB) {
2588     Error(E, "'width' operand must be in the range [1,32-lsb]");
2589     return MatchOperand_ParseFail;
2590   }
2591   E = Parser.getTok().getLoc();
2592
2593   Operands.push_back(ARMOperand::CreateBitfield(LSB, Width, S, E));
2594
2595   return MatchOperand_Success;
2596 }
2597
2598 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2599 parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2600   // Check for a post-index addressing register operand. Specifically:
2601   // postidx_reg := '+' register {, shift}
2602   //              | '-' register {, shift}
2603   //              | register {, shift}
2604
2605   // This method must return MatchOperand_NoMatch without consuming any tokens
2606   // in the case where there is no match, as other alternatives take other
2607   // parse methods.
2608   AsmToken Tok = Parser.getTok();
2609   SMLoc S = Tok.getLoc();
2610   bool haveEaten = false;
2611   bool isAdd = true;
2612   int Reg = -1;
2613   if (Tok.is(AsmToken::Plus)) {
2614     Parser.Lex(); // Eat the '+' token.
2615     haveEaten = true;
2616   } else if (Tok.is(AsmToken::Minus)) {
2617     Parser.Lex(); // Eat the '-' token.
2618     isAdd = false;
2619     haveEaten = true;
2620   }
2621   if (Parser.getTok().is(AsmToken::Identifier))
2622     Reg = tryParseRegister();
2623   if (Reg == -1) {
2624     if (!haveEaten)
2625       return MatchOperand_NoMatch;
2626     Error(Parser.getTok().getLoc(), "register expected");
2627     return MatchOperand_ParseFail;
2628   }
2629   SMLoc E = Parser.getTok().getLoc();
2630
2631   ARM_AM::ShiftOpc ShiftTy = ARM_AM::no_shift;
2632   unsigned ShiftImm = 0;
2633   if (Parser.getTok().is(AsmToken::Comma)) {
2634     Parser.Lex(); // Eat the ','.
2635     if (parseMemRegOffsetShift(ShiftTy, ShiftImm))
2636       return MatchOperand_ParseFail;
2637   }
2638
2639   Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ShiftTy,
2640                                                   ShiftImm, S, E));
2641
2642   return MatchOperand_Success;
2643 }
2644
2645 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2646 parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2647   // Check for a post-index addressing register operand. Specifically:
2648   // am3offset := '+' register
2649   //              | '-' register
2650   //              | register
2651   //              | # imm
2652   //              | # + imm
2653   //              | # - imm
2654
2655   // This method must return MatchOperand_NoMatch without consuming any tokens
2656   // in the case where there is no match, as other alternatives take other
2657   // parse methods.
2658   AsmToken Tok = Parser.getTok();
2659   SMLoc S = Tok.getLoc();
2660
2661   // Do immediates first, as we always parse those if we have a '#'.
2662   if (Parser.getTok().is(AsmToken::Hash)) {
2663     Parser.Lex(); // Eat the '#'.
2664     // Explicitly look for a '-', as we need to encode negative zero
2665     // differently.
2666     bool isNegative = Parser.getTok().is(AsmToken::Minus);
2667     const MCExpr *Offset;
2668     if (getParser().ParseExpression(Offset))
2669       return MatchOperand_ParseFail;
2670     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
2671     if (!CE) {
2672       Error(S, "constant expression expected");
2673       return MatchOperand_ParseFail;
2674     }
2675     SMLoc E = Tok.getLoc();
2676     // Negative zero is encoded as the flag value INT32_MIN.
2677     int32_t Val = CE->getValue();
2678     if (isNegative && Val == 0)
2679       Val = INT32_MIN;
2680
2681     Operands.push_back(
2682       ARMOperand::CreateImm(MCConstantExpr::Create(Val, getContext()), S, E));
2683
2684     return MatchOperand_Success;
2685   }
2686
2687
2688   bool haveEaten = false;
2689   bool isAdd = true;
2690   int Reg = -1;
2691   if (Tok.is(AsmToken::Plus)) {
2692     Parser.Lex(); // Eat the '+' token.
2693     haveEaten = true;
2694   } else if (Tok.is(AsmToken::Minus)) {
2695     Parser.Lex(); // Eat the '-' token.
2696     isAdd = false;
2697     haveEaten = true;
2698   }
2699   if (Parser.getTok().is(AsmToken::Identifier))
2700     Reg = tryParseRegister();
2701   if (Reg == -1) {
2702     if (!haveEaten)
2703       return MatchOperand_NoMatch;
2704     Error(Parser.getTok().getLoc(), "register expected");
2705     return MatchOperand_ParseFail;
2706   }
2707   SMLoc E = Parser.getTok().getLoc();
2708
2709   Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ARM_AM::no_shift,
2710                                                   0, S, E));
2711
2712   return MatchOperand_Success;
2713 }
2714
2715 /// cvtT2LdrdPre - Convert parsed operands to MCInst.
2716 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2717 /// when they refer multiple MIOperands inside a single one.
2718 bool ARMAsmParser::
2719 cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
2720              const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2721   // Rt, Rt2
2722   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2723   ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
2724   // Create a writeback register dummy placeholder.
2725   Inst.addOperand(MCOperand::CreateReg(0));
2726   // addr
2727   ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
2728   // pred
2729   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2730   return true;
2731 }
2732
2733 /// cvtT2StrdPre - Convert parsed operands to MCInst.
2734 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2735 /// when they refer multiple MIOperands inside a single one.
2736 bool ARMAsmParser::
2737 cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
2738              const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2739   // Create a writeback register dummy placeholder.
2740   Inst.addOperand(MCOperand::CreateReg(0));
2741   // Rt, Rt2
2742   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2743   ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
2744   // addr
2745   ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
2746   // pred
2747   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2748   return true;
2749 }
2750
2751 /// cvtLdWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
2752 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2753 /// when they refer multiple MIOperands inside a single one.
2754 bool ARMAsmParser::
2755 cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
2756                          const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2757   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2758
2759   // Create a writeback register dummy placeholder.
2760   Inst.addOperand(MCOperand::CreateImm(0));
2761
2762   ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
2763   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2764   return true;
2765 }
2766
2767 /// cvtStWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
2768 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2769 /// when they refer multiple MIOperands inside a single one.
2770 bool ARMAsmParser::
2771 cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
2772                          const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2773   // Create a writeback register dummy placeholder.
2774   Inst.addOperand(MCOperand::CreateImm(0));
2775   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2776   ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
2777   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2778   return true;
2779 }
2780
2781 /// cvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
2782 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2783 /// when they refer multiple MIOperands inside a single one.
2784 bool ARMAsmParser::
2785 cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
2786                          const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2787   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2788
2789   // Create a writeback register dummy placeholder.
2790   Inst.addOperand(MCOperand::CreateImm(0));
2791
2792   ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
2793   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2794   return true;
2795 }
2796
2797 /// cvtLdWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
2798 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2799 /// when they refer multiple MIOperands inside a single one.
2800 bool ARMAsmParser::
2801 cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
2802                          const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2803   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2804
2805   // Create a writeback register dummy placeholder.
2806   Inst.addOperand(MCOperand::CreateImm(0));
2807
2808   ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
2809   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2810   return true;
2811 }
2812
2813
2814 /// cvtStWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
2815 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2816 /// when they refer multiple MIOperands inside a single one.
2817 bool ARMAsmParser::
2818 cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
2819                          const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2820   // Create a writeback register dummy placeholder.
2821   Inst.addOperand(MCOperand::CreateImm(0));
2822   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2823   ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
2824   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2825   return true;
2826 }
2827
2828 /// cvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
2829 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2830 /// when they refer multiple MIOperands inside a single one.
2831 bool ARMAsmParser::
2832 cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
2833                          const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2834   // Create a writeback register dummy placeholder.
2835   Inst.addOperand(MCOperand::CreateImm(0));
2836   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2837   ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
2838   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2839   return true;
2840 }
2841
2842 /// cvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
2843 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2844 /// when they refer multiple MIOperands inside a single one.
2845 bool ARMAsmParser::
2846 cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
2847                          const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2848   // Create a writeback register dummy placeholder.
2849   Inst.addOperand(MCOperand::CreateImm(0));
2850   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2851   ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
2852   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2853   return true;
2854 }
2855
2856 /// cvtLdExtTWriteBackImm - Convert parsed operands to MCInst.
2857 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2858 /// when they refer multiple MIOperands inside a single one.
2859 bool ARMAsmParser::
2860 cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
2861                       const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2862   // Rt
2863   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2864   // Create a writeback register dummy placeholder.
2865   Inst.addOperand(MCOperand::CreateImm(0));
2866   // addr
2867   ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
2868   // offset
2869   ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
2870   // pred
2871   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2872   return true;
2873 }
2874
2875 /// cvtLdExtTWriteBackReg - Convert parsed operands to MCInst.
2876 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2877 /// when they refer multiple MIOperands inside a single one.
2878 bool ARMAsmParser::
2879 cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
2880                       const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2881   // Rt
2882   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2883   // Create a writeback register dummy placeholder.
2884   Inst.addOperand(MCOperand::CreateImm(0));
2885   // addr
2886   ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
2887   // offset
2888   ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
2889   // pred
2890   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2891   return true;
2892 }
2893
2894 /// cvtStExtTWriteBackImm - Convert parsed operands to MCInst.
2895 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2896 /// when they refer multiple MIOperands inside a single one.
2897 bool ARMAsmParser::
2898 cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
2899                       const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2900   // Create a writeback register dummy placeholder.
2901   Inst.addOperand(MCOperand::CreateImm(0));
2902   // Rt
2903   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2904   // addr
2905   ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
2906   // offset
2907   ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
2908   // pred
2909   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2910   return true;
2911 }
2912
2913 /// cvtStExtTWriteBackReg - Convert parsed operands to MCInst.
2914 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2915 /// when they refer multiple MIOperands inside a single one.
2916 bool ARMAsmParser::
2917 cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
2918                       const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2919   // Create a writeback register dummy placeholder.
2920   Inst.addOperand(MCOperand::CreateImm(0));
2921   // Rt
2922   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2923   // addr
2924   ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
2925   // offset
2926   ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
2927   // pred
2928   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2929   return true;
2930 }
2931
2932 /// cvtLdrdPre - Convert parsed operands to MCInst.
2933 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2934 /// when they refer multiple MIOperands inside a single one.
2935 bool ARMAsmParser::
2936 cvtLdrdPre(MCInst &Inst, unsigned Opcode,
2937            const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2938   // Rt, Rt2
2939   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2940   ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
2941   // Create a writeback register dummy placeholder.
2942   Inst.addOperand(MCOperand::CreateImm(0));
2943   // addr
2944   ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
2945   // pred
2946   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2947   return true;
2948 }
2949
2950 /// cvtStrdPre - Convert parsed operands to MCInst.
2951 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2952 /// when they refer multiple MIOperands inside a single one.
2953 bool ARMAsmParser::
2954 cvtStrdPre(MCInst &Inst, unsigned Opcode,
2955            const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2956   // Create a writeback register dummy placeholder.
2957   Inst.addOperand(MCOperand::CreateImm(0));
2958   // Rt, Rt2
2959   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2960   ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
2961   // addr
2962   ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
2963   // pred
2964   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2965   return true;
2966 }
2967
2968 /// cvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
2969 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2970 /// when they refer multiple MIOperands inside a single one.
2971 bool ARMAsmParser::
2972 cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
2973                          const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2974   ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
2975   // Create a writeback register dummy placeholder.
2976   Inst.addOperand(MCOperand::CreateImm(0));
2977   ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
2978   ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
2979   return true;
2980 }
2981
2982 /// cvtThumbMultiple- Convert parsed operands to MCInst.
2983 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
2984 /// when they refer multiple MIOperands inside a single one.
2985 bool ARMAsmParser::
2986 cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
2987            const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2988   // The second source operand must be the same register as the destination
2989   // operand.
2990   if (Operands.size() == 6 &&
2991       (((ARMOperand*)Operands[3])->getReg() !=
2992        ((ARMOperand*)Operands[5])->getReg()) &&
2993       (((ARMOperand*)Operands[3])->getReg() !=
2994        ((ARMOperand*)Operands[4])->getReg())) {
2995     Error(Operands[3]->getStartLoc(),
2996           "destination register must match source register");
2997     return false;
2998   }
2999   ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3000   ((ARMOperand*)Operands[1])->addCCOutOperands(Inst, 1);
3001   ((ARMOperand*)Operands[4])->addRegOperands(Inst, 1);
3002   // If we have a three-operand form, use that, else the second source operand
3003   // is just the destination operand again.
3004   if (Operands.size() == 6)
3005     ((ARMOperand*)Operands[5])->addRegOperands(Inst, 1);
3006   else
3007     Inst.addOperand(Inst.getOperand(0));
3008   ((ARMOperand*)Operands[2])->addCondCodeOperands(Inst, 2);
3009
3010   return true;
3011 }
3012
3013 /// Parse an ARM memory expression, return false if successful else return true
3014 /// or an error.  The first token must be a '[' when called.
3015 bool ARMAsmParser::
3016 parseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3017   SMLoc S, E;
3018   assert(Parser.getTok().is(AsmToken::LBrac) &&
3019          "Token is not a Left Bracket");
3020   S = Parser.getTok().getLoc();
3021   Parser.Lex(); // Eat left bracket token.
3022
3023   const AsmToken &BaseRegTok = Parser.getTok();
3024   int BaseRegNum = tryParseRegister();
3025   if (BaseRegNum == -1)
3026     return Error(BaseRegTok.getLoc(), "register expected");
3027
3028   // The next token must either be a comma or a closing bracket.
3029   const AsmToken &Tok = Parser.getTok();
3030   if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
3031     return Error(Tok.getLoc(), "malformed memory operand");
3032
3033   if (Tok.is(AsmToken::RBrac)) {
3034     E = Tok.getLoc();
3035     Parser.Lex(); // Eat right bracket token.
3036
3037     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0, ARM_AM::no_shift,
3038                                              0, 0, false, S, E));
3039
3040     // If there's a pre-indexing writeback marker, '!', just add it as a token
3041     // operand. It's rather odd, but syntactically valid.
3042     if (Parser.getTok().is(AsmToken::Exclaim)) {
3043       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3044       Parser.Lex(); // Eat the '!'.
3045     }
3046
3047     return false;
3048   }
3049
3050   assert(Tok.is(AsmToken::Comma) && "Lost comma in memory operand?!");
3051   Parser.Lex(); // Eat the comma.
3052
3053   // If we have a ':', it's an alignment specifier.
3054   if (Parser.getTok().is(AsmToken::Colon)) {
3055     Parser.Lex(); // Eat the ':'.
3056     E = Parser.getTok().getLoc();
3057
3058     const MCExpr *Expr;
3059     if (getParser().ParseExpression(Expr))
3060      return true;
3061
3062     // The expression has to be a constant. Memory references with relocations
3063     // don't come through here, as they use the <label> forms of the relevant
3064     // instructions.
3065     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3066     if (!CE)
3067       return Error (E, "constant expression expected");
3068
3069     unsigned Align = 0;
3070     switch (CE->getValue()) {
3071     default:
3072       return Error(E, "alignment specifier must be 64, 128, or 256 bits");
3073     case 64:  Align = 8; break;
3074     case 128: Align = 16; break;
3075     case 256: Align = 32; break;
3076     }
3077
3078     // Now we should have the closing ']'
3079     E = Parser.getTok().getLoc();
3080     if (Parser.getTok().isNot(AsmToken::RBrac))
3081       return Error(E, "']' expected");
3082     Parser.Lex(); // Eat right bracket token.
3083
3084     // Don't worry about range checking the value here. That's handled by
3085     // the is*() predicates.
3086     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0,
3087                                              ARM_AM::no_shift, 0, Align,
3088                                              false, S, E));
3089
3090     // If there's a pre-indexing writeback marker, '!', just add it as a token
3091     // operand.
3092     if (Parser.getTok().is(AsmToken::Exclaim)) {
3093       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3094       Parser.Lex(); // Eat the '!'.
3095     }
3096
3097     return false;
3098   }
3099
3100   // If we have a '#', it's an immediate offset, else assume it's a register
3101   // offset.
3102   if (Parser.getTok().is(AsmToken::Hash)) {
3103     Parser.Lex(); // Eat the '#'.
3104     E = Parser.getTok().getLoc();
3105
3106     bool isNegative = getParser().getTok().is(AsmToken::Minus);
3107     const MCExpr *Offset;
3108     if (getParser().ParseExpression(Offset))
3109      return true;
3110
3111     // The expression has to be a constant. Memory references with relocations
3112     // don't come through here, as they use the <label> forms of the relevant
3113     // instructions.
3114     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
3115     if (!CE)
3116       return Error (E, "constant expression expected");
3117
3118     // If the constant was #-0, represent it as INT32_MIN.
3119     int32_t Val = CE->getValue();
3120     if (isNegative && Val == 0)
3121       CE = MCConstantExpr::Create(INT32_MIN, getContext());
3122
3123     // Now we should have the closing ']'
3124     E = Parser.getTok().getLoc();
3125     if (Parser.getTok().isNot(AsmToken::RBrac))
3126       return Error(E, "']' expected");
3127     Parser.Lex(); // Eat right bracket token.
3128
3129     // Don't worry about range checking the value here. That's handled by
3130     // the is*() predicates.
3131     Operands.push_back(ARMOperand::CreateMem(BaseRegNum, CE, 0,
3132                                              ARM_AM::no_shift, 0, 0,
3133                                              false, S, E));
3134
3135     // If there's a pre-indexing writeback marker, '!', just add it as a token
3136     // operand.
3137     if (Parser.getTok().is(AsmToken::Exclaim)) {
3138       Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3139       Parser.Lex(); // Eat the '!'.
3140     }
3141
3142     return false;
3143   }
3144
3145   // The register offset is optionally preceded by a '+' or '-'
3146   bool isNegative = false;
3147   if (Parser.getTok().is(AsmToken::Minus)) {
3148     isNegative = true;
3149     Parser.Lex(); // Eat the '-'.
3150   } else if (Parser.getTok().is(AsmToken::Plus)) {
3151     // Nothing to do.
3152     Parser.Lex(); // Eat the '+'.
3153   }
3154
3155   E = Parser.getTok().getLoc();
3156   int OffsetRegNum = tryParseRegister();
3157   if (OffsetRegNum == -1)
3158     return Error(E, "register expected");
3159
3160   // If there's a shift operator, handle it.
3161   ARM_AM::ShiftOpc ShiftType = ARM_AM::no_shift;
3162   unsigned ShiftImm = 0;
3163   if (Parser.getTok().is(AsmToken::Comma)) {
3164     Parser.Lex(); // Eat the ','.
3165     if (parseMemRegOffsetShift(ShiftType, ShiftImm))
3166       return true;
3167   }
3168
3169   // Now we should have the closing ']'
3170   E = Parser.getTok().getLoc();
3171   if (Parser.getTok().isNot(AsmToken::RBrac))
3172     return Error(E, "']' expected");
3173   Parser.Lex(); // Eat right bracket token.
3174
3175   Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, OffsetRegNum,
3176                                            ShiftType, ShiftImm, 0, isNegative,
3177                                            S, E));
3178
3179   // If there's a pre-indexing writeback marker, '!', just add it as a token
3180   // operand.
3181   if (Parser.getTok().is(AsmToken::Exclaim)) {
3182     Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3183     Parser.Lex(); // Eat the '!'.
3184   }
3185
3186   return false;
3187 }
3188
3189 /// parseMemRegOffsetShift - one of these two:
3190 ///   ( lsl | lsr | asr | ror ) , # shift_amount
3191 ///   rrx
3192 /// return true if it parses a shift otherwise it returns false.
3193 bool ARMAsmParser::parseMemRegOffsetShift(ARM_AM::ShiftOpc &St,
3194                                           unsigned &Amount) {
3195   SMLoc Loc = Parser.getTok().getLoc();
3196   const AsmToken &Tok = Parser.getTok();
3197   if (Tok.isNot(AsmToken::Identifier))
3198     return true;
3199   StringRef ShiftName = Tok.getString();
3200   if (ShiftName == "lsl" || ShiftName == "LSL")
3201     St = ARM_AM::lsl;
3202   else if (ShiftName == "lsr" || ShiftName == "LSR")
3203     St = ARM_AM::lsr;
3204   else if (ShiftName == "asr" || ShiftName == "ASR")
3205     St = ARM_AM::asr;
3206   else if (ShiftName == "ror" || ShiftName == "ROR")
3207     St = ARM_AM::ror;
3208   else if (ShiftName == "rrx" || ShiftName == "RRX")
3209     St = ARM_AM::rrx;
3210   else
3211     return Error(Loc, "illegal shift operator");
3212   Parser.Lex(); // Eat shift type token.
3213
3214   // rrx stands alone.
3215   Amount = 0;
3216   if (St != ARM_AM::rrx) {
3217     Loc = Parser.getTok().getLoc();
3218     // A '#' and a shift amount.
3219     const AsmToken &HashTok = Parser.getTok();
3220     if (HashTok.isNot(AsmToken::Hash))
3221       return Error(HashTok.getLoc(), "'#' expected");
3222     Parser.Lex(); // Eat hash token.
3223
3224     const MCExpr *Expr;
3225     if (getParser().ParseExpression(Expr))
3226       return true;
3227     // Range check the immediate.
3228     // lsl, ror: 0 <= imm <= 31
3229     // lsr, asr: 0 <= imm <= 32
3230     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3231     if (!CE)
3232       return Error(Loc, "shift amount must be an immediate");
3233     int64_t Imm = CE->getValue();
3234     if (Imm < 0 ||
3235         ((St == ARM_AM::lsl || St == ARM_AM::ror) && Imm > 31) ||
3236         ((St == ARM_AM::lsr || St == ARM_AM::asr) && Imm > 32))
3237       return Error(Loc, "immediate shift value out of range");
3238     Amount = Imm;
3239   }
3240
3241   return false;
3242 }
3243
3244 /// parseFPImm - A floating point immediate expression operand.
3245 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3246 parseFPImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3247   SMLoc S = Parser.getTok().getLoc();
3248
3249   if (Parser.getTok().isNot(AsmToken::Hash))
3250     return MatchOperand_NoMatch;
3251   Parser.Lex(); // Eat the '#'.
3252
3253   // Handle negation, as that still comes through as a separate token.
3254   bool isNegative = false;
3255   if (Parser.getTok().is(AsmToken::Minus)) {
3256     isNegative = true;
3257     Parser.Lex();
3258   }
3259   const AsmToken &Tok = Parser.getTok();
3260   if (Tok.is(AsmToken::Real)) {
3261     APFloat RealVal(APFloat::IEEEdouble, Tok.getString());
3262     uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
3263     // If we had a '-' in front, toggle the sign bit.
3264     IntVal ^= (uint64_t)isNegative << 63;
3265     int Val = ARM_AM::getFP64Imm(APInt(64, IntVal));
3266     Parser.Lex(); // Eat the token.
3267     if (Val == -1) {
3268       TokError("floating point value out of range");
3269       return MatchOperand_ParseFail;
3270     }
3271     Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
3272     return MatchOperand_Success;
3273   }
3274   if (Tok.is(AsmToken::Integer)) {
3275     int64_t Val = Tok.getIntVal();
3276     Parser.Lex(); // Eat the token.
3277     if (Val > 255 || Val < 0) {
3278       TokError("encoded floating point value out of range");
3279       return MatchOperand_ParseFail;
3280     }
3281     Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
3282     return MatchOperand_Success;
3283   }
3284
3285   TokError("invalid floating point immediate");
3286   return MatchOperand_ParseFail;
3287 }
3288 /// Parse a arm instruction operand.  For now this parses the operand regardless
3289 /// of the mnemonic.
3290 bool ARMAsmParser::parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
3291                                 StringRef Mnemonic) {
3292   SMLoc S, E;
3293
3294   // Check if the current operand has a custom associated parser, if so, try to
3295   // custom parse the operand, or fallback to the general approach.
3296   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
3297   if (ResTy == MatchOperand_Success)
3298     return false;
3299   // If there wasn't a custom match, try the generic matcher below. Otherwise,
3300   // there was a match, but an error occurred, in which case, just return that
3301   // the operand parsing failed.
3302   if (ResTy == MatchOperand_ParseFail)
3303     return true;
3304
3305   switch (getLexer().getKind()) {
3306   default:
3307     Error(Parser.getTok().getLoc(), "unexpected token in operand");
3308     return true;
3309   case AsmToken::Identifier: {
3310     // If this is VMRS, check for the apsr_nzcv operand.
3311     if (!tryParseRegisterWithWriteBack(Operands))
3312       return false;
3313     int Res = tryParseShiftRegister(Operands);
3314     if (Res == 0) // success
3315       return false;
3316     else if (Res == -1) // irrecoverable error
3317       return true;
3318     if (Mnemonic == "vmrs" && Parser.getTok().getString() == "apsr_nzcv") {
3319       S = Parser.getTok().getLoc();
3320       Parser.Lex();
3321       Operands.push_back(ARMOperand::CreateToken("apsr_nzcv", S));
3322       return false;
3323     }
3324
3325     // Fall though for the Identifier case that is not a register or a
3326     // special name.
3327   }
3328   case AsmToken::Integer: // things like 1f and 2b as a branch targets
3329   case AsmToken::Dot: {   // . as a branch target
3330     // This was not a register so parse other operands that start with an
3331     // identifier (like labels) as expressions and create them as immediates.
3332     const MCExpr *IdVal;
3333     S = Parser.getTok().getLoc();
3334     if (getParser().ParseExpression(IdVal))
3335       return true;
3336     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3337     Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
3338     return false;
3339   }
3340   case AsmToken::LBrac:
3341     return parseMemory(Operands);
3342   case AsmToken::LCurly:
3343     return parseRegisterList(Operands);
3344   case AsmToken::Hash: {
3345     // #42 -> immediate.
3346     // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
3347     S = Parser.getTok().getLoc();
3348     Parser.Lex();
3349     bool isNegative = Parser.getTok().is(AsmToken::Minus);
3350     const MCExpr *ImmVal;
3351     if (getParser().ParseExpression(ImmVal))
3352       return true;
3353     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmVal);
3354     if (!CE) {
3355       Error(S, "constant expression expected");
3356       return MatchOperand_ParseFail;
3357     }
3358     int32_t Val = CE->getValue();
3359     if (isNegative && Val == 0)
3360       ImmVal = MCConstantExpr::Create(INT32_MIN, getContext());
3361     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3362     Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
3363     return false;
3364   }
3365   case AsmToken::Colon: {
3366     // ":lower16:" and ":upper16:" expression prefixes
3367     // FIXME: Check it's an expression prefix,
3368     // e.g. (FOO - :lower16:BAR) isn't legal.
3369     ARMMCExpr::VariantKind RefKind;
3370     if (parsePrefix(RefKind))
3371       return true;
3372
3373     const MCExpr *SubExprVal;
3374     if (getParser().ParseExpression(SubExprVal))
3375       return true;
3376
3377     const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
3378                                                    getContext());
3379     E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3380     Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
3381     return false;
3382   }
3383   }
3384 }
3385
3386 // parsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
3387 //  :lower16: and :upper16:.
3388 bool ARMAsmParser::parsePrefix(ARMMCExpr::VariantKind &RefKind) {
3389   RefKind = ARMMCExpr::VK_ARM_None;
3390
3391   // :lower16: and :upper16: modifiers
3392   assert(getLexer().is(AsmToken::Colon) && "expected a :");
3393   Parser.Lex(); // Eat ':'
3394
3395   if (getLexer().isNot(AsmToken::Identifier)) {
3396     Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
3397     return true;
3398   }
3399
3400   StringRef IDVal = Parser.getTok().getIdentifier();
3401   if (IDVal == "lower16") {
3402     RefKind = ARMMCExpr::VK_ARM_LO16;
3403   } else if (IDVal == "upper16") {
3404     RefKind = ARMMCExpr::VK_ARM_HI16;
3405   } else {
3406     Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
3407     return true;
3408   }
3409   Parser.Lex();
3410
3411   if (getLexer().isNot(AsmToken::Colon)) {
3412     Error(Parser.getTok().getLoc(), "unexpected token after prefix");
3413     return true;
3414   }
3415   Parser.Lex(); // Eat the last ':'
3416   return false;
3417 }
3418
3419 /// \brief Given a mnemonic, split out possible predication code and carry
3420 /// setting letters to form a canonical mnemonic and flags.
3421 //
3422 // FIXME: Would be nice to autogen this.
3423 // FIXME: This is a bit of a maze of special cases.
3424 StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic,
3425                                       unsigned &PredicationCode,
3426                                       bool &CarrySetting,
3427                                       unsigned &ProcessorIMod,
3428                                       StringRef &ITMask) {
3429   PredicationCode = ARMCC::AL;
3430   CarrySetting = false;
3431   ProcessorIMod = 0;
3432
3433   // Ignore some mnemonics we know aren't predicated forms.
3434   //
3435   // FIXME: Would be nice to autogen this.
3436   if ((Mnemonic == "movs" && isThumb()) ||
3437       Mnemonic == "teq"   || Mnemonic == "vceq"   || Mnemonic == "svc"   ||
3438       Mnemonic == "mls"   || Mnemonic == "smmls"  || Mnemonic == "vcls"  ||
3439       Mnemonic == "vmls"  || Mnemonic == "vnmls"  || Mnemonic == "vacge" ||
3440       Mnemonic == "vcge"  || Mnemonic == "vclt"   || Mnemonic == "vacgt" ||
3441       Mnemonic == "vcgt"  || Mnemonic == "vcle"   || Mnemonic == "smlal" ||
3442       Mnemonic == "umaal" || Mnemonic == "umlal"  || Mnemonic == "vabal" ||
3443       Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal")
3444     return Mnemonic;
3445
3446   // First, split out any predication code. Ignore mnemonics we know aren't
3447   // predicated but do have a carry-set and so weren't caught above.
3448   if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
3449       Mnemonic != "muls" && Mnemonic != "smlals" && Mnemonic != "smulls" &&
3450       Mnemonic != "umlals" && Mnemonic != "umulls" && Mnemonic != "lsls" &&
3451       Mnemonic != "sbcs" && Mnemonic != "rscs") {
3452     unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
3453       .Case("eq", ARMCC::EQ)
3454       .Case("ne", ARMCC::NE)
3455       .Case("hs", ARMCC::HS)
3456       .Case("cs", ARMCC::HS)
3457       .Case("lo", ARMCC::LO)
3458       .Case("cc", ARMCC::LO)
3459       .Case("mi", ARMCC::MI)
3460       .Case("pl", ARMCC::PL)
3461       .Case("vs", ARMCC::VS)
3462       .Case("vc", ARMCC::VC)
3463       .Case("hi", ARMCC::HI)
3464       .Case("ls", ARMCC::LS)
3465       .Case("ge", ARMCC::GE)
3466       .Case("lt", ARMCC::LT)
3467       .Case("gt", ARMCC::GT)
3468       .Case("le", ARMCC::LE)
3469       .Case("al", ARMCC::AL)
3470       .Default(~0U);
3471     if (CC != ~0U) {
3472       Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
3473       PredicationCode = CC;
3474     }
3475   }
3476
3477   // Next, determine if we have a carry setting bit. We explicitly ignore all
3478   // the instructions we know end in 's'.
3479   if (Mnemonic.endswith("s") &&
3480       !(Mnemonic == "cps" || Mnemonic == "mls" ||
3481         Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
3482         Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
3483         Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
3484         Mnemonic == "vrsqrts" || Mnemonic == "srs" ||
3485         (Mnemonic == "movs" && isThumb()))) {
3486     Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
3487     CarrySetting = true;
3488   }
3489
3490   // The "cps" instruction can have a interrupt mode operand which is glued into
3491   // the mnemonic. Check if this is the case, split it and parse the imod op
3492   if (Mnemonic.startswith("cps")) {
3493     // Split out any imod code.
3494     unsigned IMod =
3495       StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
3496       .Case("ie", ARM_PROC::IE)
3497       .Case("id", ARM_PROC::ID)
3498       .Default(~0U);
3499     if (IMod != ~0U) {
3500       Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
3501       ProcessorIMod = IMod;
3502     }
3503   }
3504
3505   // The "it" instruction has the condition mask on the end of the mnemonic.
3506   if (Mnemonic.startswith("it")) {
3507     ITMask = Mnemonic.slice(2, Mnemonic.size());
3508     Mnemonic = Mnemonic.slice(0, 2);
3509   }
3510
3511   return Mnemonic;
3512 }
3513
3514 /// \brief Given a canonical mnemonic, determine if the instruction ever allows
3515 /// inclusion of carry set or predication code operands.
3516 //
3517 // FIXME: It would be nice to autogen this.
3518 void ARMAsmParser::
3519 getMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
3520                       bool &CanAcceptPredicationCode) {
3521   if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
3522       Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
3523       Mnemonic == "add" || Mnemonic == "adc" ||
3524       Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
3525       Mnemonic == "orr" || Mnemonic == "mvn" ||
3526       Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
3527       Mnemonic == "sbc" || Mnemonic == "eor" || Mnemonic == "neg" ||
3528       (!isThumb() && (Mnemonic == "smull" || Mnemonic == "mov" ||
3529                       Mnemonic == "mla" || Mnemonic == "smlal" ||
3530                       Mnemonic == "umlal" || Mnemonic == "umull"))) {
3531     CanAcceptCarrySet = true;
3532   } else
3533     CanAcceptCarrySet = false;
3534
3535   if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
3536       Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
3537       Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
3538       Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
3539       Mnemonic == "dsb" || Mnemonic == "isb" || Mnemonic == "setend" ||
3540       (Mnemonic == "clrex" && !isThumb()) ||
3541       (Mnemonic == "nop" && isThumbOne()) ||
3542       ((Mnemonic == "pld" || Mnemonic == "pli" || Mnemonic == "pldw") &&
3543        !isThumb()) ||
3544       ((Mnemonic.startswith("rfe") || Mnemonic.startswith("srs")) &&
3545        !isThumb()) ||
3546       Mnemonic.startswith("cps") || (Mnemonic == "movs" && isThumbOne())) {
3547     CanAcceptPredicationCode = false;
3548   } else
3549     CanAcceptPredicationCode = true;
3550
3551   if (isThumb()) {
3552     if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
3553         Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
3554       CanAcceptPredicationCode = false;
3555   }
3556 }
3557
3558 bool ARMAsmParser::shouldOmitCCOutOperand(StringRef Mnemonic,
3559                                SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3560   // FIXME: This is all horribly hacky. We really need a better way to deal
3561   // with optional operands like this in the matcher table.
3562
3563   // The 'mov' mnemonic is special. One variant has a cc_out operand, while
3564   // another does not. Specifically, the MOVW instruction does not. So we
3565   // special case it here and remove the defaulted (non-setting) cc_out
3566   // operand if that's the instruction we're trying to match.
3567   //
3568   // We do this as post-processing of the explicit operands rather than just
3569   // conditionally adding the cc_out in the first place because we need
3570   // to check the type of the parsed immediate operand.
3571   if (Mnemonic == "mov" && Operands.size() > 4 && !isThumb() &&
3572       !static_cast<ARMOperand*>(Operands[4])->isARMSOImm() &&
3573       static_cast<ARMOperand*>(Operands[4])->isImm0_65535Expr() &&
3574       static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
3575     return true;
3576
3577   // Register-register 'add' for thumb does not have a cc_out operand
3578   // when there are only two register operands.
3579   if (isThumb() && Mnemonic == "add" && Operands.size() == 5 &&
3580       static_cast<ARMOperand*>(Operands[3])->isReg() &&
3581       static_cast<ARMOperand*>(Operands[4])->isReg() &&
3582       static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
3583     return true;
3584   // Register-register 'add' for thumb does not have a cc_out operand
3585   // when it's an ADD Rdm, SP, {Rdm|#imm0_255} instruction. We do
3586   // have to check the immediate range here since Thumb2 has a variant
3587   // that can handle a different range and has a cc_out operand.
3588   if (((isThumb() && Mnemonic == "add") ||
3589        (isThumbTwo() && Mnemonic == "sub")) &&
3590       Operands.size() == 6 &&
3591       static_cast<ARMOperand*>(Operands[3])->isReg() &&
3592       static_cast<ARMOperand*>(Operands[4])->isReg() &&
3593       static_cast<ARMOperand*>(Operands[4])->getReg() == ARM::SP &&
3594       static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
3595       (static_cast<ARMOperand*>(Operands[5])->isReg() ||
3596        static_cast<ARMOperand*>(Operands[5])->isImm0_1020s4()))
3597     return true;
3598   // For Thumb2, add/sub immediate does not have a cc_out operand for the
3599   // imm0_4095 variant. That's the least-preferred variant when
3600   // selecting via the generic "add" mnemonic, so to know that we
3601   // should remove the cc_out operand, we have to explicitly check that
3602   // it's not one of the other variants. Ugh.
3603   if (isThumbTwo() && (Mnemonic == "add" || Mnemonic == "sub") &&
3604       Operands.size() == 6 &&
3605       static_cast<ARMOperand*>(Operands[3])->isReg() &&
3606       static_cast<ARMOperand*>(Operands[4])->isReg() &&
3607       static_cast<ARMOperand*>(Operands[5])->isImm()) {
3608     // Nest conditions rather than one big 'if' statement for readability.
3609     //
3610     // If either register is a high reg, it's either one of the SP
3611     // variants (handled above) or a 32-bit encoding, so we just
3612     // check against T3.
3613     if ((!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
3614          !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg())) &&
3615         static_cast<ARMOperand*>(Operands[5])->isT2SOImm())
3616       return false;
3617     // If both registers are low, we're in an IT block, and the immediate is
3618     // in range, we should use encoding T1 instead, which has a cc_out.
3619     if (inITBlock() &&
3620         isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) &&
3621         isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) &&
3622         static_cast<ARMOperand*>(Operands[5])->isImm0_7())
3623       return false;
3624
3625     // Otherwise, we use encoding T4, which does not have a cc_out
3626     // operand.
3627     return true;
3628   }
3629
3630   // The thumb2 multiply instruction doesn't have a CCOut register, so
3631   // if we have a "mul" mnemonic in Thumb mode, check if we'll be able to
3632   // use the 16-bit encoding or not.
3633   if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 6 &&
3634       static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
3635       static_cast<ARMOperand*>(Operands[3])->isReg() &&
3636       static_cast<ARMOperand*>(Operands[4])->isReg() &&
3637       static_cast<ARMOperand*>(Operands[5])->isReg() &&
3638       // If the registers aren't low regs, the destination reg isn't the
3639       // same as one of the source regs, or the cc_out operand is zero
3640       // outside of an IT block, we have to use the 32-bit encoding, so
3641       // remove the cc_out operand.
3642       (!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
3643        !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) ||
3644        !inITBlock() ||
3645        (static_cast<ARMOperand*>(Operands[3])->getReg() !=
3646         static_cast<ARMOperand*>(Operands[5])->getReg() &&
3647         static_cast<ARMOperand*>(Operands[3])->getReg() !=
3648         static_cast<ARMOperand*>(Operands[4])->getReg())))
3649     return true;
3650
3651
3652
3653   // Register-register 'add/sub' for thumb does not have a cc_out operand
3654   // when it's an ADD/SUB SP, #imm. Be lenient on count since there's also
3655   // the "add/sub SP, SP, #imm" version. If the follow-up operands aren't
3656   // right, this will result in better diagnostics (which operand is off)
3657   // anyway.
3658   if (isThumb() && (Mnemonic == "add" || Mnemonic == "sub") &&
3659       (Operands.size() == 5 || Operands.size() == 6) &&
3660       static_cast<ARMOperand*>(Operands[3])->isReg() &&
3661       static_cast<ARMOperand*>(Operands[3])->getReg() == ARM::SP &&
3662       static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
3663     return true;
3664
3665   return false;
3666 }
3667
3668 /// Parse an arm instruction mnemonic followed by its operands.
3669 bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
3670                                SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3671   // Create the leading tokens for the mnemonic, split by '.' characters.
3672   size_t Start = 0, Next = Name.find('.');
3673   StringRef Mnemonic = Name.slice(Start, Next);
3674
3675   // Split out the predication code and carry setting flag from the mnemonic.
3676   unsigned PredicationCode;
3677   unsigned ProcessorIMod;
3678   bool CarrySetting;
3679   StringRef ITMask;
3680   Mnemonic = splitMnemonic(Mnemonic, PredicationCode, CarrySetting,
3681                            ProcessorIMod, ITMask);
3682
3683   // In Thumb1, only the branch (B) instruction can be predicated.
3684   if (isThumbOne() && PredicationCode != ARMCC::AL && Mnemonic != "b") {
3685     Parser.EatToEndOfStatement();
3686     return Error(NameLoc, "conditional execution not supported in Thumb1");
3687   }
3688
3689   Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
3690
3691   // Handle the IT instruction ITMask. Convert it to a bitmask. This
3692   // is the mask as it will be for the IT encoding if the conditional
3693   // encoding has a '1' as it's bit0 (i.e. 't' ==> '1'). In the case
3694   // where the conditional bit0 is zero, the instruction post-processing
3695   // will adjust the mask accordingly.
3696   if (Mnemonic == "it") {
3697     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + 2);
3698     if (ITMask.size() > 3) {
3699       Parser.EatToEndOfStatement();
3700       return Error(Loc, "too many conditions on IT instruction");
3701     }
3702     unsigned Mask = 8;
3703     for (unsigned i = ITMask.size(); i != 0; --i) {
3704       char pos = ITMask[i - 1];
3705       if (pos != 't' && pos != 'e') {
3706         Parser.EatToEndOfStatement();
3707         return Error(Loc, "illegal IT block condition mask '" + ITMask + "'");
3708       }
3709       Mask >>= 1;
3710       if (ITMask[i - 1] == 't')
3711         Mask |= 8;
3712     }
3713     Operands.push_back(ARMOperand::CreateITMask(Mask, Loc));
3714   }
3715
3716   // FIXME: This is all a pretty gross hack. We should automatically handle
3717   // optional operands like this via tblgen.
3718
3719   // Next, add the CCOut and ConditionCode operands, if needed.
3720   //
3721   // For mnemonics which can ever incorporate a carry setting bit or predication
3722   // code, our matching model involves us always generating CCOut and
3723   // ConditionCode operands to match the mnemonic "as written" and then we let
3724   // the matcher deal with finding the right instruction or generating an
3725   // appropriate error.
3726   bool CanAcceptCarrySet, CanAcceptPredicationCode;
3727   getMnemonicAcceptInfo(Mnemonic, CanAcceptCarrySet, CanAcceptPredicationCode);
3728
3729   // If we had a carry-set on an instruction that can't do that, issue an
3730   // error.
3731   if (!CanAcceptCarrySet && CarrySetting) {
3732     Parser.EatToEndOfStatement();
3733     return Error(NameLoc, "instruction '" + Mnemonic +
3734                  "' can not set flags, but 's' suffix specified");
3735   }
3736   // If we had a predication code on an instruction that can't do that, issue an
3737   // error.
3738   if (!CanAcceptPredicationCode && PredicationCode != ARMCC::AL) {
3739     Parser.EatToEndOfStatement();
3740     return Error(NameLoc, "instruction '" + Mnemonic +
3741                  "' is not predicable, but condition code specified");
3742   }
3743
3744   // Add the carry setting operand, if necessary.
3745   if (CanAcceptCarrySet) {
3746     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size());
3747     Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
3748                                                Loc));
3749   }
3750
3751   // Add the predication code operand, if necessary.
3752   if (CanAcceptPredicationCode) {
3753     SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size() +
3754                                       CarrySetting);
3755     Operands.push_back(ARMOperand::CreateCondCode(
3756                          ARMCC::CondCodes(PredicationCode), Loc));
3757   }
3758
3759   // Add the processor imod operand, if necessary.
3760   if (ProcessorIMod) {
3761     Operands.push_back(ARMOperand::CreateImm(
3762           MCConstantExpr::Create(ProcessorIMod, getContext()),
3763                                  NameLoc, NameLoc));
3764   }
3765
3766   // Add the remaining tokens in the mnemonic.
3767   while (Next != StringRef::npos) {
3768     Start = Next;
3769     Next = Name.find('.', Start + 1);
3770     StringRef ExtraToken = Name.slice(Start, Next);
3771
3772     // For now, we're only parsing Thumb1 (for the most part), so
3773     // just ignore ".n" qualifiers. We'll use them to restrict
3774     // matching when we do Thumb2.
3775     if (ExtraToken != ".n") {
3776       SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
3777       Operands.push_back(ARMOperand::CreateToken(ExtraToken, Loc));
3778     }
3779   }
3780
3781   // Read the remaining operands.
3782   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3783     // Read the first operand.
3784     if (parseOperand(Operands, Mnemonic)) {
3785       Parser.EatToEndOfStatement();
3786       return true;
3787     }
3788
3789     while (getLexer().is(AsmToken::Comma)) {
3790       Parser.Lex();  // Eat the comma.
3791
3792       // Parse and remember the operand.
3793       if (parseOperand(Operands, Mnemonic)) {
3794         Parser.EatToEndOfStatement();
3795         return true;
3796       }
3797     }
3798   }
3799
3800   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3801     SMLoc Loc = getLexer().getLoc();
3802     Parser.EatToEndOfStatement();
3803     return Error(Loc, "unexpected token in argument list");
3804   }
3805
3806   Parser.Lex(); // Consume the EndOfStatement
3807
3808   // Some instructions, mostly Thumb, have forms for the same mnemonic that
3809   // do and don't have a cc_out optional-def operand. With some spot-checks
3810   // of the operand list, we can figure out which variant we're trying to
3811   // parse and adjust accordingly before actually matching. We shouldn't ever
3812   // try to remove a cc_out operand that was explicitly set on the the
3813   // mnemonic, of course (CarrySetting == true). Reason number #317 the
3814   // table driven matcher doesn't fit well with the ARM instruction set.
3815   if (!CarrySetting && shouldOmitCCOutOperand(Mnemonic, Operands)) {
3816     ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
3817     Operands.erase(Operands.begin() + 1);
3818     delete Op;
3819   }
3820
3821   // ARM mode 'blx' need special handling, as the register operand version
3822   // is predicable, but the label operand version is not. So, we can't rely
3823   // on the Mnemonic based checking to correctly figure out when to put
3824   // a k_CondCode operand in the list. If we're trying to match the label
3825   // version, remove the k_CondCode operand here.
3826   if (!isThumb() && Mnemonic == "blx" && Operands.size() == 3 &&
3827       static_cast<ARMOperand*>(Operands[2])->isImm()) {
3828     ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
3829     Operands.erase(Operands.begin() + 1);
3830     delete Op;
3831   }
3832
3833   // The vector-compare-to-zero instructions have a literal token "#0" at
3834   // the end that comes to here as an immediate operand. Convert it to a
3835   // token to play nicely with the matcher.
3836   if ((Mnemonic == "vceq" || Mnemonic == "vcge" || Mnemonic == "vcgt" ||
3837       Mnemonic == "vcle" || Mnemonic == "vclt") && Operands.size() == 6 &&
3838       static_cast<ARMOperand*>(Operands[5])->isImm()) {
3839     ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
3840     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
3841     if (CE && CE->getValue() == 0) {
3842       Operands.erase(Operands.begin() + 5);
3843       Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
3844       delete Op;
3845     }
3846   }
3847   // VCMP{E} does the same thing, but with a different operand count.
3848   if ((Mnemonic == "vcmp" || Mnemonic == "vcmpe") && Operands.size() == 5 &&
3849       static_cast<ARMOperand*>(Operands[4])->isImm()) {
3850     ARMOperand *Op = static_cast<ARMOperand*>(Operands[4]);
3851     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
3852     if (CE && CE->getValue() == 0) {
3853       Operands.erase(Operands.begin() + 4);
3854       Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
3855       delete Op;
3856     }
3857   }
3858   // Similarly, the Thumb1 "RSB" instruction has a literal "#0" on the
3859   // end. Convert it to a token here.
3860   if (Mnemonic == "rsb" && isThumb() && Operands.size() == 6 &&
3861       static_cast<ARMOperand*>(Operands[5])->isImm()) {
3862     ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
3863     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
3864     if (CE && CE->getValue() == 0) {
3865       Operands.erase(Operands.begin() + 5);
3866       Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
3867       delete Op;
3868     }
3869   }
3870
3871   return false;
3872 }
3873
3874 // Validate context-sensitive operand constraints.
3875
3876 // return 'true' if register list contains non-low GPR registers,
3877 // 'false' otherwise. If Reg is in the register list or is HiReg, set
3878 // 'containsReg' to true.
3879 static bool checkLowRegisterList(MCInst Inst, unsigned OpNo, unsigned Reg,
3880                                  unsigned HiReg, bool &containsReg) {
3881   containsReg = false;
3882   for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
3883     unsigned OpReg = Inst.getOperand(i).getReg();
3884     if (OpReg == Reg)
3885       containsReg = true;
3886     // Anything other than a low register isn't legal here.
3887     if (!isARMLowRegister(OpReg) && (!HiReg || OpReg != HiReg))
3888       return true;
3889   }
3890   return false;
3891 }
3892
3893 // Check if the specified regisgter is in the register list of the inst,
3894 // starting at the indicated operand number.
3895 static bool listContainsReg(MCInst &Inst, unsigned OpNo, unsigned Reg) {
3896   for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
3897     unsigned OpReg = Inst.getOperand(i).getReg();
3898     if (OpReg == Reg)
3899       return true;
3900   }
3901   return false;
3902 }
3903
3904 // FIXME: We would really prefer to have MCInstrInfo (the wrapper around
3905 // the ARMInsts array) instead. Getting that here requires awkward
3906 // API changes, though. Better way?
3907 namespace llvm {
3908 extern MCInstrDesc ARMInsts[];
3909 }
3910 static MCInstrDesc &getInstDesc(unsigned Opcode) {
3911   return ARMInsts[Opcode];
3912 }
3913
3914 // FIXME: We would really like to be able to tablegen'erate this.
3915 bool ARMAsmParser::
3916 validateInstruction(MCInst &Inst,
3917                     const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3918   MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
3919   SMLoc Loc = Operands[0]->getStartLoc();
3920   // Check the IT block state first.
3921   // NOTE: In Thumb mode, the BKPT instruction has the interesting property of
3922   // being allowed in IT blocks, but not being predicable.  It just always
3923   // executes.
3924   if (inITBlock() && Inst.getOpcode() != ARM::tBKPT) {
3925     unsigned bit = 1;
3926     if (ITState.FirstCond)
3927       ITState.FirstCond = false;
3928     else
3929       bit = (ITState.Mask >> (5 - ITState.CurPosition)) & 1;
3930     // The instruction must be predicable.
3931     if (!MCID.isPredicable())
3932       return Error(Loc, "instructions in IT block must be predicable");
3933     unsigned Cond = Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm();
3934     unsigned ITCond = bit ? ITState.Cond :
3935       ARMCC::getOppositeCondition(ITState.Cond);
3936     if (Cond != ITCond) {
3937       // Find the condition code Operand to get its SMLoc information.
3938       SMLoc CondLoc;
3939       for (unsigned i = 1; i < Operands.size(); ++i)
3940         if (static_cast<ARMOperand*>(Operands[i])->isCondCode())
3941           CondLoc = Operands[i]->getStartLoc();
3942       return Error(CondLoc, "incorrect condition in IT block; got '" +
3943                    StringRef(ARMCondCodeToString(ARMCC::CondCodes(Cond))) +
3944                    "', but expected '" +
3945                    ARMCondCodeToString(ARMCC::CondCodes(ITCond)) + "'");
3946     }
3947   // Check for non-'al' condition codes outside of the IT block.
3948   } else if (isThumbTwo() && MCID.isPredicable() &&
3949              Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
3950              ARMCC::AL && Inst.getOpcode() != ARM::tB &&
3951              Inst.getOpcode() != ARM::t2B)
3952     return Error(Loc, "predicated instructions must be in IT block");
3953
3954   switch (Inst.getOpcode()) {
3955   case ARM::LDRD:
3956   case ARM::LDRD_PRE:
3957   case ARM::LDRD_POST:
3958   case ARM::LDREXD: {
3959     // Rt2 must be Rt + 1.
3960     unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
3961     unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
3962     if (Rt2 != Rt + 1)
3963       return Error(Operands[3]->getStartLoc(),
3964                    "destination operands must be sequential");
3965     return false;
3966   }
3967   case ARM::STRD: {
3968     // Rt2 must be Rt + 1.
3969     unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
3970     unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
3971     if (Rt2 != Rt + 1)
3972       return Error(Operands[3]->getStartLoc(),
3973                    "source operands must be sequential");
3974     return false;
3975   }
3976   case ARM::STRD_PRE:
3977   case ARM::STRD_POST:
3978   case ARM::STREXD: {
3979     // Rt2 must be Rt + 1.
3980     unsigned Rt = getARMRegisterNumbering(Inst.getOperand(1).getReg());
3981     unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(2).getReg());
3982     if (Rt2 != Rt + 1)
3983       return Error(Operands[3]->getStartLoc(),
3984                    "source operands must be sequential");
3985     return false;
3986   }
3987   case ARM::SBFX:
3988   case ARM::UBFX: {
3989     // width must be in range [1, 32-lsb]
3990     unsigned lsb = Inst.getOperand(2).getImm();
3991     unsigned widthm1 = Inst.getOperand(3).getImm();
3992     if (widthm1 >= 32 - lsb)
3993       return Error(Operands[5]->getStartLoc(),
3994                    "bitfield width must be in range [1,32-lsb]");
3995     return false;
3996   }
3997   case ARM::tLDMIA: {
3998     // If we're parsing Thumb2, the .w variant is available and handles
3999     // most cases that are normally illegal for a Thumb1 LDM
4000     // instruction. We'll make the transformation in processInstruction()
4001     // if necessary.
4002     //
4003     // Thumb LDM instructions are writeback iff the base register is not
4004     // in the register list.
4005     unsigned Rn = Inst.getOperand(0).getReg();
4006     bool hasWritebackToken =
4007       (static_cast<ARMOperand*>(Operands[3])->isToken() &&
4008        static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
4009     bool listContainsBase;
4010     if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) && !isThumbTwo())
4011       return Error(Operands[3 + hasWritebackToken]->getStartLoc(),
4012                    "registers must be in range r0-r7");
4013     // If we should have writeback, then there should be a '!' token.
4014     if (!listContainsBase && !hasWritebackToken && !isThumbTwo())
4015       return Error(Operands[2]->getStartLoc(),
4016                    "writeback operator '!' expected");
4017     // If we should not have writeback, there must not be a '!'. This is
4018     // true even for the 32-bit wide encodings.
4019     if (listContainsBase && hasWritebackToken)
4020       return Error(Operands[3]->getStartLoc(),
4021                    "writeback operator '!' not allowed when base register "
4022                    "in register list");
4023
4024     break;
4025   }
4026   case ARM::t2LDMIA_UPD: {
4027     if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
4028       return Error(Operands[4]->getStartLoc(),
4029                    "writeback operator '!' not allowed when base register "
4030                    "in register list");
4031     break;
4032   }
4033   case ARM::tPOP: {
4034     bool listContainsBase;
4035     if (checkLowRegisterList(Inst, 3, 0, ARM::PC, listContainsBase))
4036       return Error(Operands[2]->getStartLoc(),
4037                    "registers must be in range r0-r7 or pc");
4038     break;
4039   }
4040   case ARM::tPUSH: {
4041     bool listContainsBase;
4042     if (checkLowRegisterList(Inst, 3, 0, ARM::LR, listContainsBase))
4043       return Error(Operands[2]->getStartLoc(),
4044                    "registers must be in range r0-r7 or lr");
4045     break;
4046   }
4047   case ARM::tSTMIA_UPD: {
4048     bool listContainsBase;
4049     if (checkLowRegisterList(Inst, 4, 0, 0, listContainsBase) && !isThumbTwo())
4050       return Error(Operands[4]->getStartLoc(),
4051                    "registers must be in range r0-r7");
4052     break;
4053   }
4054   }
4055
4056   return false;
4057 }
4058
4059 void ARMAsmParser::
4060 processInstruction(MCInst &Inst,
4061                    const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4062   switch (Inst.getOpcode()) {
4063   case ARM::LDMIA_UPD:
4064     // If this is a load of a single register via a 'pop', then we should use
4065     // a post-indexed LDR instruction instead, per the ARM ARM.
4066     if (static_cast<ARMOperand*>(Operands[0])->getToken() == "pop" &&
4067         Inst.getNumOperands() == 5) {
4068       MCInst TmpInst;
4069       TmpInst.setOpcode(ARM::LDR_POST_IMM);
4070       TmpInst.addOperand(Inst.getOperand(4)); // Rt
4071       TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
4072       TmpInst.addOperand(Inst.getOperand(1)); // Rn
4073       TmpInst.addOperand(MCOperand::CreateReg(0));  // am2offset
4074       TmpInst.addOperand(MCOperand::CreateImm(4));
4075       TmpInst.addOperand(Inst.getOperand(2)); // CondCode
4076       TmpInst.addOperand(Inst.getOperand(3));
4077       Inst = TmpInst;
4078     }
4079     break;
4080   case ARM::STMDB_UPD:
4081     // If this is a store of a single register via a 'push', then we should use
4082     // a pre-indexed STR instruction instead, per the ARM ARM.
4083     if (static_cast<ARMOperand*>(Operands[0])->getToken() == "push" &&
4084         Inst.getNumOperands() == 5) {
4085       MCInst TmpInst;
4086       TmpInst.setOpcode(ARM::STR_PRE_IMM);
4087       TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
4088       TmpInst.addOperand(Inst.getOperand(4)); // Rt
4089       TmpInst.addOperand(Inst.getOperand(1)); // addrmode_imm12
4090       TmpInst.addOperand(MCOperand::CreateImm(-4));
4091       TmpInst.addOperand(Inst.getOperand(2)); // CondCode
4092       TmpInst.addOperand(Inst.getOperand(3));
4093       Inst = TmpInst;
4094     }
4095     break;
4096   case ARM::tADDi8:
4097     // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
4098     // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
4099     // to encoding T2 if <Rd> is specified and encoding T2 is preferred
4100     // to encoding T1 if <Rd> is omitted."
4101     if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6)
4102       Inst.setOpcode(ARM::tADDi3);
4103     break;
4104   case ARM::tSUBi8:
4105     // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
4106     // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
4107     // to encoding T2 if <Rd> is specified and encoding T2 is preferred
4108     // to encoding T1 if <Rd> is omitted."
4109     if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6)
4110       Inst.setOpcode(ARM::tSUBi3);
4111     break;
4112   case ARM::tB:
4113     // A Thumb conditional branch outside of an IT block is a tBcc.
4114     if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock())
4115       Inst.setOpcode(ARM::tBcc);
4116     break;
4117   case ARM::t2B:
4118     // A Thumb2 conditional branch outside of an IT block is a t2Bcc.
4119     if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock())
4120       Inst.setOpcode(ARM::t2Bcc);
4121     break;
4122   case ARM::t2Bcc:
4123     // If the conditional is AL or we're in an IT block, we really want t2B.
4124     if (Inst.getOperand(1).getImm() == ARMCC::AL || inITBlock())
4125       Inst.setOpcode(ARM::t2B);
4126     break;
4127   case ARM::tBcc:
4128     // If the conditional is AL, we really want tB.
4129     if (Inst.getOperand(1).getImm() == ARMCC::AL)
4130       Inst.setOpcode(ARM::tB);
4131     break;
4132   case ARM::tLDMIA: {
4133     // If the register list contains any high registers, or if the writeback
4134     // doesn't match what tLDMIA can do, we need to use the 32-bit encoding
4135     // instead if we're in Thumb2. Otherwise, this should have generated
4136     // an error in validateInstruction().
4137     unsigned Rn = Inst.getOperand(0).getReg();
4138     bool hasWritebackToken =
4139       (static_cast<ARMOperand*>(Operands[3])->isToken() &&
4140        static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
4141     bool listContainsBase;
4142     if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) ||
4143         (!listContainsBase && !hasWritebackToken) ||
4144         (listContainsBase && hasWritebackToken)) {
4145       // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
4146       assert (isThumbTwo());
4147       Inst.setOpcode(hasWritebackToken ? ARM::t2LDMIA_UPD : ARM::t2LDMIA);
4148       // If we're switching to the updating version, we need to insert
4149       // the writeback tied operand.
4150       if (hasWritebackToken)
4151         Inst.insert(Inst.begin(),
4152                     MCOperand::CreateReg(Inst.getOperand(0).getReg()));
4153     }
4154     break;
4155   }
4156   case ARM::tSTMIA_UPD: {
4157     // If the register list contains any high registers, we need to use
4158     // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
4159     // should have generated an error in validateInstruction().
4160     unsigned Rn = Inst.getOperand(0).getReg();
4161     bool listContainsBase;
4162     if (checkLowRegisterList(Inst, 4, Rn, 0, listContainsBase)) {
4163       // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
4164       assert (isThumbTwo());
4165       Inst.setOpcode(ARM::t2STMIA_UPD);
4166     }
4167     break;
4168   }
4169   case ARM::t2MOVi: {
4170     // If we can use the 16-bit encoding and the user didn't explicitly
4171     // request the 32-bit variant, transform it here.
4172     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4173         Inst.getOperand(1).getImm() <= 255 &&
4174         ((!inITBlock() && Inst.getOperand(2).getImm() == ARMCC::AL &&
4175          Inst.getOperand(4).getReg() == ARM::CPSR) ||
4176         (inITBlock() && Inst.getOperand(4).getReg() == 0)) &&
4177         (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4178          static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4179       // The operands aren't in the same order for tMOVi8...
4180       MCInst TmpInst;
4181       TmpInst.setOpcode(ARM::tMOVi8);
4182       TmpInst.addOperand(Inst.getOperand(0));
4183       TmpInst.addOperand(Inst.getOperand(4));
4184       TmpInst.addOperand(Inst.getOperand(1));
4185       TmpInst.addOperand(Inst.getOperand(2));
4186       TmpInst.addOperand(Inst.getOperand(3));
4187       Inst = TmpInst;
4188     }
4189     break;
4190   }
4191   case ARM::t2MOVr: {
4192     // If we can use the 16-bit encoding and the user didn't explicitly
4193     // request the 32-bit variant, transform it here.
4194     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4195         isARMLowRegister(Inst.getOperand(1).getReg()) &&
4196         Inst.getOperand(2).getImm() == ARMCC::AL &&
4197         Inst.getOperand(4).getReg() == ARM::CPSR &&
4198         (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4199          static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4200       // The operands aren't the same for tMOV[S]r... (no cc_out)
4201       MCInst TmpInst;
4202       TmpInst.setOpcode(Inst.getOperand(4).getReg() ? ARM::tMOVSr : ARM::tMOVr);
4203       TmpInst.addOperand(Inst.getOperand(0));
4204       TmpInst.addOperand(Inst.getOperand(1));
4205       TmpInst.addOperand(Inst.getOperand(2));
4206       TmpInst.addOperand(Inst.getOperand(3));
4207       Inst = TmpInst;
4208     }
4209     break;
4210   }
4211   case ARM::t2SXTH:
4212   case ARM::t2SXTB:
4213   case ARM::t2UXTH:
4214   case ARM::t2UXTB: {
4215     // If we can use the 16-bit encoding and the user didn't explicitly
4216     // request the 32-bit variant, transform it here.
4217     if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4218         isARMLowRegister(Inst.getOperand(1).getReg()) &&
4219         Inst.getOperand(2).getImm() == 0 &&
4220         (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4221          static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4222       unsigned NewOpc;
4223       switch (Inst.getOpcode()) {
4224       default: llvm_unreachable("Illegal opcode!");
4225       case ARM::t2SXTH: NewOpc = ARM::tSXTH; break;
4226       case ARM::t2SXTB: NewOpc = ARM::tSXTB; break;
4227       case ARM::t2UXTH: NewOpc = ARM::tUXTH; break;
4228       case ARM::t2UXTB: NewOpc = ARM::tUXTB; break;
4229       }
4230       // The operands aren't the same for thumb1 (no rotate operand).
4231       MCInst TmpInst;
4232       TmpInst.setOpcode(NewOpc);
4233       TmpInst.addOperand(Inst.getOperand(0));
4234       TmpInst.addOperand(Inst.getOperand(1));
4235       TmpInst.addOperand(Inst.getOperand(3));
4236       TmpInst.addOperand(Inst.getOperand(4));
4237       Inst = TmpInst;
4238     }
4239     break;
4240   }
4241   case ARM::t2IT: {
4242     // The mask bits for all but the first condition are represented as
4243     // the low bit of the condition code value implies 't'. We currently
4244     // always have 1 implies 't', so XOR toggle the bits if the low bit
4245     // of the condition code is zero. The encoding also expects the low
4246     // bit of the condition to be encoded as bit 4 of the mask operand,
4247     // so mask that in if needed
4248     MCOperand &MO = Inst.getOperand(1);
4249     unsigned Mask = MO.getImm();
4250     unsigned OrigMask = Mask;
4251     unsigned TZ = CountTrailingZeros_32(Mask);
4252     if ((Inst.getOperand(0).getImm() & 1) == 0) {
4253       assert(Mask && TZ <= 3 && "illegal IT mask value!");
4254       for (unsigned i = 3; i != TZ; --i)
4255         Mask ^= 1 << i;
4256     } else
4257       Mask |= 0x10;
4258     MO.setImm(Mask);
4259
4260     // Set up the IT block state according to the IT instruction we just
4261     // matched.
4262     assert(!inITBlock() && "nested IT blocks?!");
4263     ITState.Cond = ARMCC::CondCodes(Inst.getOperand(0).getImm());
4264     ITState.Mask = OrigMask; // Use the original mask, not the updated one.
4265     ITState.CurPosition = 0;
4266     ITState.FirstCond = true;
4267     break;
4268   }
4269   }
4270 }
4271
4272 unsigned ARMAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
4273   // 16-bit thumb arithmetic instructions either require or preclude the 'S'
4274   // suffix depending on whether they're in an IT block or not.
4275   unsigned Opc = Inst.getOpcode();
4276   MCInstrDesc &MCID = getInstDesc(Opc);
4277   if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
4278     assert(MCID.hasOptionalDef() &&
4279            "optionally flag setting instruction missing optional def operand");
4280     assert(MCID.NumOperands == Inst.getNumOperands() &&
4281            "operand count mismatch!");
4282     // Find the optional-def operand (cc_out).
4283     unsigned OpNo;
4284     for (OpNo = 0;
4285          !MCID.OpInfo[OpNo].isOptionalDef() && OpNo < MCID.NumOperands;
4286          ++OpNo)
4287       ;
4288     // If we're parsing Thumb1, reject it completely.
4289     if (isThumbOne() && Inst.getOperand(OpNo).getReg() != ARM::CPSR)
4290       return Match_MnemonicFail;
4291     // If we're parsing Thumb2, which form is legal depends on whether we're
4292     // in an IT block.
4293     if (isThumbTwo() && Inst.getOperand(OpNo).getReg() != ARM::CPSR &&
4294         !inITBlock())
4295       return Match_RequiresITBlock;
4296     if (isThumbTwo() && Inst.getOperand(OpNo).getReg() == ARM::CPSR &&
4297         inITBlock())
4298       return Match_RequiresNotITBlock;
4299   }
4300   // Some high-register supporting Thumb1 encodings only allow both registers
4301   // to be from r0-r7 when in Thumb2.
4302   else if (Opc == ARM::tADDhirr && isThumbOne() &&
4303            isARMLowRegister(Inst.getOperand(1).getReg()) &&
4304            isARMLowRegister(Inst.getOperand(2).getReg()))
4305     return Match_RequiresThumb2;
4306   // Others only require ARMv6 or later.
4307   else if (Opc == ARM::tMOVr && isThumbOne() && !hasV6Ops() &&
4308            isARMLowRegister(Inst.getOperand(0).getReg()) &&
4309            isARMLowRegister(Inst.getOperand(1).getReg()))
4310     return Match_RequiresV6;
4311   return Match_Success;
4312 }
4313
4314 bool ARMAsmParser::
4315 MatchAndEmitInstruction(SMLoc IDLoc,
4316                         SmallVectorImpl<MCParsedAsmOperand*> &Operands,
4317                         MCStreamer &Out) {
4318   MCInst Inst;
4319   unsigned ErrorInfo;
4320   unsigned MatchResult;
4321   MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
4322   switch (MatchResult) {
4323   default: break;
4324   case Match_Success:
4325     // Context sensitive operand constraints aren't handled by the matcher,
4326     // so check them here.
4327     if (validateInstruction(Inst, Operands)) {
4328       // Still progress the IT block, otherwise one wrong condition causes
4329       // nasty cascading errors.
4330       forwardITPosition();
4331       return true;
4332     }
4333
4334     // Some instructions need post-processing to, for example, tweak which
4335     // encoding is selected.
4336     processInstruction(Inst, Operands);
4337
4338     // Only move forward at the very end so that everything in validate
4339     // and process gets a consistent answer about whether we're in an IT
4340     // block.
4341     forwardITPosition();
4342
4343     Out.EmitInstruction(Inst);
4344     return false;
4345   case Match_MissingFeature:
4346     Error(IDLoc, "instruction requires a CPU feature not currently enabled");
4347     return true;
4348   case Match_InvalidOperand: {
4349     SMLoc ErrorLoc = IDLoc;
4350     if (ErrorInfo != ~0U) {
4351       if (ErrorInfo >= Operands.size())
4352         return Error(IDLoc, "too few operands for instruction");
4353
4354       ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
4355       if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
4356     }
4357
4358     return Error(ErrorLoc, "invalid operand for instruction");
4359   }
4360   case Match_MnemonicFail:
4361     return Error(IDLoc, "invalid instruction");
4362   case Match_ConversionFail:
4363     // The converter function will have already emited a diagnostic.
4364     return true;
4365   case Match_RequiresNotITBlock:
4366     return Error(IDLoc, "flag setting instruction only valid outside IT block");
4367   case Match_RequiresITBlock:
4368     return Error(IDLoc, "instruction only valid inside IT block");
4369   case Match_RequiresV6:
4370     return Error(IDLoc, "instruction variant requires ARMv6 or later");
4371   case Match_RequiresThumb2:
4372     return Error(IDLoc, "instruction variant requires Thumb2");
4373   }
4374
4375   llvm_unreachable("Implement any new match types added!");
4376   return true;
4377 }
4378
4379 /// parseDirective parses the arm specific directives
4380 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
4381   StringRef IDVal = DirectiveID.getIdentifier();
4382   if (IDVal == ".word")
4383     return parseDirectiveWord(4, DirectiveID.getLoc());
4384   else if (IDVal == ".thumb")
4385     return parseDirectiveThumb(DirectiveID.getLoc());
4386   else if (IDVal == ".thumb_func")
4387     return parseDirectiveThumbFunc(DirectiveID.getLoc());
4388   else if (IDVal == ".code")
4389     return parseDirectiveCode(DirectiveID.getLoc());
4390   else if (IDVal == ".syntax")
4391     return parseDirectiveSyntax(DirectiveID.getLoc());
4392   return true;
4393 }
4394
4395 /// parseDirectiveWord
4396 ///  ::= .word [ expression (, expression)* ]
4397 bool ARMAsmParser::parseDirectiveWord(unsigned Size, SMLoc L) {
4398   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4399     for (;;) {
4400       const MCExpr *Value;
4401       if (getParser().ParseExpression(Value))
4402         return true;
4403
4404       getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
4405
4406       if (getLexer().is(AsmToken::EndOfStatement))
4407         break;
4408
4409       // FIXME: Improve diagnostic.
4410       if (getLexer().isNot(AsmToken::Comma))
4411         return Error(L, "unexpected token in directive");
4412       Parser.Lex();
4413     }
4414   }
4415
4416   Parser.Lex();
4417   return false;
4418 }
4419
4420 /// parseDirectiveThumb
4421 ///  ::= .thumb
4422 bool ARMAsmParser::parseDirectiveThumb(SMLoc L) {
4423   if (getLexer().isNot(AsmToken::EndOfStatement))
4424     return Error(L, "unexpected token in directive");
4425   Parser.Lex();
4426
4427   // TODO: set thumb mode
4428   // TODO: tell the MC streamer the mode
4429   // getParser().getStreamer().Emit???();
4430   return false;
4431 }
4432
4433 /// parseDirectiveThumbFunc
4434 ///  ::= .thumbfunc symbol_name
4435 bool ARMAsmParser::parseDirectiveThumbFunc(SMLoc L) {
4436   const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
4437   bool isMachO = MAI.hasSubsectionsViaSymbols();
4438   StringRef Name;
4439
4440   // Darwin asm has function name after .thumb_func direction
4441   // ELF doesn't
4442   if (isMachO) {
4443     const AsmToken &Tok = Parser.getTok();
4444     if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
4445       return Error(L, "unexpected token in .thumb_func directive");
4446     Name = Tok.getString();
4447     Parser.Lex(); // Consume the identifier token.
4448   }
4449
4450   if (getLexer().isNot(AsmToken::EndOfStatement))
4451     return Error(L, "unexpected token in directive");
4452   Parser.Lex();
4453
4454   // FIXME: assuming function name will be the line following .thumb_func
4455   if (!isMachO) {
4456     Name = Parser.getTok().getString();
4457   }
4458
4459   // Mark symbol as a thumb symbol.
4460   MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
4461   getParser().getStreamer().EmitThumbFunc(Func);
4462   return false;
4463 }
4464
4465 /// parseDirectiveSyntax
4466 ///  ::= .syntax unified | divided
4467 bool ARMAsmParser::parseDirectiveSyntax(SMLoc L) {
4468   const AsmToken &Tok = Parser.getTok();
4469   if (Tok.isNot(AsmToken::Identifier))
4470     return Error(L, "unexpected token in .syntax directive");
4471   StringRef Mode = Tok.getString();
4472   if (Mode == "unified" || Mode == "UNIFIED")
4473     Parser.Lex();
4474   else if (Mode == "divided" || Mode == "DIVIDED")
4475     return Error(L, "'.syntax divided' arm asssembly not supported");
4476   else
4477     return Error(L, "unrecognized syntax mode in .syntax directive");
4478
4479   if (getLexer().isNot(AsmToken::EndOfStatement))
4480     return Error(Parser.getTok().getLoc(), "unexpected token in directive");
4481   Parser.Lex();
4482
4483   // TODO tell the MC streamer the mode
4484   // getParser().getStreamer().Emit???();
4485   return false;
4486 }
4487
4488 /// parseDirectiveCode
4489 ///  ::= .code 16 | 32
4490 bool ARMAsmParser::parseDirectiveCode(SMLoc L) {
4491   const AsmToken &Tok = Parser.getTok();
4492   if (Tok.isNot(AsmToken::Integer))
4493     return Error(L, "unexpected token in .code directive");
4494   int64_t Val = Parser.getTok().getIntVal();
4495   if (Val == 16)
4496     Parser.Lex();
4497   else if (Val == 32)
4498     Parser.Lex();
4499   else
4500     return Error(L, "invalid operand to .code directive");
4501
4502   if (getLexer().isNot(AsmToken::EndOfStatement))
4503     return Error(Parser.getTok().getLoc(), "unexpected token in directive");
4504   Parser.Lex();
4505
4506   if (Val == 16) {
4507     if (!isThumb())
4508       SwitchMode();
4509     getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
4510   } else {
4511     if (isThumb())
4512       SwitchMode();
4513     getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
4514   }
4515
4516   return false;
4517 }
4518
4519 extern "C" void LLVMInitializeARMAsmLexer();
4520
4521 /// Force static initialization.
4522 extern "C" void LLVMInitializeARMAsmParser() {
4523   RegisterMCAsmParser<ARMAsmParser> X(TheARMTarget);
4524   RegisterMCAsmParser<ARMAsmParser> Y(TheThumbTarget);
4525   LLVMInitializeARMAsmLexer();
4526 }
4527
4528 #define GET_REGISTER_MATCHER
4529 #define GET_MATCHER_IMPLEMENTATION
4530 #include "ARMGenAsmMatcher.inc"