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