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