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