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