blob: 07f8f423295d5800e83739b4498f9f6165864308 [file] [log] [blame]
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001//===-- 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
Evan Cheng94b95502011-07-26 00:24:13 +000010#include "MCTargetDesc/ARMBaseInfo.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000011#include "MCTargetDesc/ARMAddressingModes.h"
12#include "MCTargetDesc/ARMMCExpr.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000013#include "llvm/MC/MCParser/MCAsmLexer.h"
14#include "llvm/MC/MCParser/MCAsmParser.h"
15#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Rafael Espindola64695402011-05-16 16:17:21 +000016#include "llvm/MC/MCAsmInfo.h"
Jim Grosbach642fc9c2010-11-05 22:33:53 +000017#include "llvm/MC/MCContext.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000018#include "llvm/MC/MCStreamer.h"
19#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCInst.h"
Evan Cheng78011362011-08-23 20:15:21 +000021#include "llvm/MC/MCInstrDesc.h"
Evan Cheng94b95502011-07-26 00:24:13 +000022#include "llvm/MC/MCRegisterInfo.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000023#include "llvm/MC/MCSubtargetInfo.h"
Evan Cheng94b95502011-07-26 00:24:13 +000024#include "llvm/MC/MCTargetAsmParser.h"
Jim Grosbach89df9962011-08-26 21:43:41 +000025#include "llvm/Support/MathExtras.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000026#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000027#include "llvm/Support/TargetRegistry.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000028#include "llvm/Support/raw_ostream.h"
Jim Grosbach11e03e72011-08-22 18:50:36 +000029#include "llvm/ADT/BitVector.h"
Benjamin Kramer75ca4b92011-07-08 21:06:23 +000030#include "llvm/ADT/OwningPtr.h"
Evan Cheng94b95502011-07-26 00:24:13 +000031#include "llvm/ADT/STLExtras.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000032#include "llvm/ADT/SmallVector.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000033#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000034#include "llvm/ADT/Twine.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000035
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000036using namespace llvm;
37
Chris Lattner3a697562010-10-28 17:20:03 +000038namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000039
40class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000041
Jim Grosbach7636bf62011-12-02 00:35:16 +000042enum VectorLaneTy { NoLanes, AllLanes, IndexedLane };
Jim Grosbach98b05a52011-11-30 01:09:44 +000043
Evan Cheng94b95502011-07-26 00:24:13 +000044class ARMAsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000045 MCSubtargetInfo &STI;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000046 MCAsmParser &Parser;
47
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +000048 struct {
49 ARMCC::CondCodes Cond; // Condition for IT block.
50 unsigned Mask:4; // Condition mask for instructions.
51 // Starting at first 1 (from lsb).
52 // '1' condition as indicated in IT.
53 // '0' inverse of condition (else).
54 // Count of instructions in IT block is
55 // 4 - trailingzeroes(mask)
56
57 bool FirstCond; // Explicit flag for when we're parsing the
58 // First instruction in the IT block. It's
59 // implied in the mask, so needs special
60 // handling.
61
62 unsigned CurPosition; // Current position in parsing of IT
63 // block. In range [0,3]. Initialized
64 // according to count of instructions in block.
65 // ~0U if no active IT block.
66 } ITState;
67 bool inITBlock() { return ITState.CurPosition != ~0U;}
Jim Grosbacha1109882011-09-02 23:22:08 +000068 void forwardITPosition() {
69 if (!inITBlock()) return;
70 // Move to the next instruction in the IT block, if there is one. If not,
71 // mark the block as done.
72 unsigned TZ = CountTrailingZeros_32(ITState.Mask);
73 if (++ITState.CurPosition == 5 - TZ)
74 ITState.CurPosition = ~0U; // Done with the IT block after this.
75 }
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +000076
77
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000078 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000079 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
80
81 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000082 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
83
Jim Grosbach1355cf12011-07-26 17:10:22 +000084 int tryParseRegister();
85 bool tryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach0d87ec22011-07-26 20:41:24 +000086 int tryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach1355cf12011-07-26 17:10:22 +000087 bool parseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach7ce05792011-08-03 23:50:40 +000088 bool parseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach1355cf12011-07-26 17:10:22 +000089 bool parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
90 bool parsePrefix(ARMMCExpr::VariantKind &RefKind);
Jim Grosbach7ce05792011-08-03 23:50:40 +000091 bool parseMemRegOffsetShift(ARM_AM::ShiftOpc &ShiftType,
92 unsigned &ShiftAmount);
Jim Grosbach1355cf12011-07-26 17:10:22 +000093 bool parseDirectiveWord(unsigned Size, SMLoc L);
94 bool parseDirectiveThumb(SMLoc L);
Jim Grosbach9a70df92011-12-07 18:04:19 +000095 bool parseDirectiveARM(SMLoc L);
Jim Grosbach1355cf12011-07-26 17:10:22 +000096 bool parseDirectiveThumbFunc(SMLoc L);
97 bool parseDirectiveCode(SMLoc L);
98 bool parseDirectiveSyntax(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000099
Jim Grosbach1355cf12011-07-26 17:10:22 +0000100 StringRef splitMnemonic(StringRef Mnemonic, unsigned &PredicationCode,
Jim Grosbach89df9962011-08-26 21:43:41 +0000101 bool &CarrySetting, unsigned &ProcessorIMod,
102 StringRef &ITMask);
Jim Grosbach1355cf12011-07-26 17:10:22 +0000103 void getMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +0000104 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +0000105
Evan Chengebdeeab2011-07-08 01:53:10 +0000106 bool isThumb() const {
107 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +0000108 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +0000109 }
Evan Chengebdeeab2011-07-08 01:53:10 +0000110 bool isThumbOne() const {
Evan Chengffc0e732011-07-09 05:47:46 +0000111 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) == 0;
Evan Chengebdeeab2011-07-08 01:53:10 +0000112 }
Jim Grosbach47a0d522011-08-16 20:45:50 +0000113 bool isThumbTwo() const {
114 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2);
115 }
Jim Grosbach194bd892011-08-16 22:20:01 +0000116 bool hasV6Ops() const {
117 return STI.getFeatureBits() & ARM::HasV6Ops;
118 }
James Molloyacad68d2011-09-28 14:21:38 +0000119 bool hasV7Ops() const {
120 return STI.getFeatureBits() & ARM::HasV7Ops;
121 }
Evan Cheng32869202011-07-08 22:36:29 +0000122 void SwitchMode() {
Evan Chengffc0e732011-07-09 05:47:46 +0000123 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
124 setAvailableFeatures(FB);
Evan Cheng32869202011-07-08 22:36:29 +0000125 }
James Molloyacad68d2011-09-28 14:21:38 +0000126 bool isMClass() const {
127 return STI.getFeatureBits() & ARM::FeatureMClass;
128 }
Evan Chengebdeeab2011-07-08 01:53:10 +0000129
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000130 /// @name Auto-generated Match Functions
131 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000132
Chris Lattner0692ee62010-09-06 19:11:01 +0000133#define GET_ASSEMBLER_HEADER
134#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000135
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000136 /// }
137
Jim Grosbach89df9962011-08-26 21:43:41 +0000138 OperandMatchResultTy parseITCondCode(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach43904292011-07-25 20:14:50 +0000139 OperandMatchResultTy parseCoprocNumOperand(
Jim Grosbachf922c472011-02-12 01:34:40 +0000140 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach43904292011-07-25 20:14:50 +0000141 OperandMatchResultTy parseCoprocRegOperand(
Jim Grosbachf922c472011-02-12 01:34:40 +0000142 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach9b8f2a02011-10-12 17:34:41 +0000143 OperandMatchResultTy parseCoprocOptionOperand(
144 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach43904292011-07-25 20:14:50 +0000145 OperandMatchResultTy parseMemBarrierOptOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000146 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach43904292011-07-25 20:14:50 +0000147 OperandMatchResultTy parseProcIFlagsOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000148 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach43904292011-07-25 20:14:50 +0000149 OperandMatchResultTy parseMSRMaskOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000150 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbachf6c05252011-07-21 17:23:04 +0000151 OperandMatchResultTy parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &O,
152 StringRef Op, int Low, int High);
153 OperandMatchResultTy parsePKHLSLImm(SmallVectorImpl<MCParsedAsmOperand*> &O) {
154 return parsePKHImm(O, "lsl", 0, 31);
155 }
156 OperandMatchResultTy parsePKHASRImm(SmallVectorImpl<MCParsedAsmOperand*> &O) {
157 return parsePKHImm(O, "asr", 1, 32);
158 }
Jim Grosbachc27d4f92011-07-22 17:44:50 +0000159 OperandMatchResultTy parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach580f4a92011-07-25 22:20:28 +0000160 OperandMatchResultTy parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach7e1547e2011-07-27 20:15:40 +0000161 OperandMatchResultTy parseRotImm(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach293a2ee2011-07-28 21:34:26 +0000162 OperandMatchResultTy parseBitfield(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach7ce05792011-08-03 23:50:40 +0000163 OperandMatchResultTy parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach251bf252011-08-10 21:56:18 +0000164 OperandMatchResultTy parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach9d390362011-10-03 23:38:36 +0000165 OperandMatchResultTy parseFPImm(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach862019c2011-10-18 23:02:30 +0000166 OperandMatchResultTy parseVectorList(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach7636bf62011-12-02 00:35:16 +0000167 OperandMatchResultTy parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000168
169 // Asm Match Converter Methods
Jim Grosbacha77295d2011-09-08 22:07:06 +0000170 bool cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
171 const SmallVectorImpl<MCParsedAsmOperand*> &);
172 bool cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
173 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbacheeec0252011-09-08 00:39:19 +0000174 bool cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
175 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbachee2c2a42011-09-16 21:55:56 +0000176 bool cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
177 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach1355cf12011-07-26 17:10:22 +0000178 bool cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000179 const SmallVectorImpl<MCParsedAsmOperand*> &);
Owen Anderson9ab0f252011-08-26 20:43:14 +0000180 bool cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
181 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach548340c2011-08-11 19:22:40 +0000182 bool cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
183 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach1355cf12011-07-26 17:10:22 +0000184 bool cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000185 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach7b8f46c2011-08-11 21:17:22 +0000186 bool cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
187 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach7ce05792011-08-03 23:50:40 +0000188 bool cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
189 const SmallVectorImpl<MCParsedAsmOperand*> &);
190 bool cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
191 const SmallVectorImpl<MCParsedAsmOperand*> &);
192 bool cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
193 const SmallVectorImpl<MCParsedAsmOperand*> &);
194 bool cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
195 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000196 bool cvtLdrdPre(MCInst &Inst, unsigned Opcode,
197 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach14605d12011-08-11 20:28:23 +0000198 bool cvtStrdPre(MCInst &Inst, unsigned Opcode,
199 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach623a4542011-08-10 22:42:16 +0000200 bool cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
201 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach88ae2bc2011-08-19 22:07:46 +0000202 bool cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
203 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach12431322011-10-24 22:16:58 +0000204 bool cvtVLDwbFixed(MCInst &Inst, unsigned Opcode,
205 const SmallVectorImpl<MCParsedAsmOperand*> &);
206 bool cvtVLDwbRegister(MCInst &Inst, unsigned Opcode,
207 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach4334e032011-10-31 21:50:31 +0000208 bool cvtVSTwbFixed(MCInst &Inst, unsigned Opcode,
209 const SmallVectorImpl<MCParsedAsmOperand*> &);
210 bool cvtVSTwbRegister(MCInst &Inst, unsigned Opcode,
211 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach189610f2011-07-26 18:25:39 +0000212
213 bool validateInstruction(MCInst &Inst,
214 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
Jim Grosbach83ec8772011-11-10 23:42:14 +0000215 bool processInstruction(MCInst &Inst,
Jim Grosbachf8fce712011-08-11 17:35:48 +0000216 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
Jim Grosbachd54b4e62011-08-16 21:12:37 +0000217 bool shouldOmitCCOutOperand(StringRef Mnemonic,
218 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Jim Grosbach189610f2011-07-26 18:25:39 +0000219
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000220public:
Jim Grosbach47a0d522011-08-16 20:45:50 +0000221 enum ARMMatchResultTy {
Jim Grosbach194bd892011-08-16 22:20:01 +0000222 Match_RequiresITBlock = FIRST_TARGET_MATCH_RESULT_TY,
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +0000223 Match_RequiresNotITBlock,
Jim Grosbach194bd892011-08-16 22:20:01 +0000224 Match_RequiresV6,
225 Match_RequiresThumb2
Jim Grosbach47a0d522011-08-16 20:45:50 +0000226 };
227
Evan Chengffc0e732011-07-09 05:47:46 +0000228 ARMAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
Evan Cheng94b95502011-07-26 00:24:13 +0000229 : MCTargetAsmParser(), STI(_STI), Parser(_Parser) {
Evan Chengebdeeab2011-07-08 01:53:10 +0000230 MCAsmParserExtension::Initialize(_Parser);
Evan Cheng32869202011-07-08 22:36:29 +0000231
Evan Chengebdeeab2011-07-08 01:53:10 +0000232 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000233 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +0000234
235 // Not in an ITBlock to start with.
236 ITState.CurPosition = ~0U;
Evan Chengebdeeab2011-07-08 01:53:10 +0000237 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000238
Jim Grosbach1355cf12011-07-26 17:10:22 +0000239 // Implementation of the MCTargetAsmParser interface:
240 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
241 bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Jim Grosbach189610f2011-07-26 18:25:39 +0000242 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Jim Grosbach1355cf12011-07-26 17:10:22 +0000243 bool ParseDirective(AsmToken DirectiveID);
244
Jim Grosbach47a0d522011-08-16 20:45:50 +0000245 unsigned checkTargetMatchPredicate(MCInst &Inst);
246
Jim Grosbach1355cf12011-07-26 17:10:22 +0000247 bool MatchAndEmitInstruction(SMLoc IDLoc,
248 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
249 MCStreamer &Out);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000250};
Jim Grosbach16c74252010-10-29 14:46:02 +0000251} // end anonymous namespace
252
Chris Lattner3a697562010-10-28 17:20:03 +0000253namespace {
254
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000255/// ARMOperand - Instances of this class represent a parsed ARM machine
256/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000257class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000258 enum KindTy {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000259 k_CondCode,
260 k_CCOut,
261 k_ITCondMask,
262 k_CoprocNum,
263 k_CoprocReg,
Jim Grosbach9b8f2a02011-10-12 17:34:41 +0000264 k_CoprocOption,
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000265 k_Immediate,
266 k_FPImmediate,
267 k_MemBarrierOpt,
268 k_Memory,
269 k_PostIndexRegister,
270 k_MSRMask,
271 k_ProcIFlags,
Jim Grosbach460a9052011-10-07 23:56:00 +0000272 k_VectorIndex,
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000273 k_Register,
274 k_RegisterList,
275 k_DPRRegisterList,
276 k_SPRRegisterList,
Jim Grosbach862019c2011-10-18 23:02:30 +0000277 k_VectorList,
Jim Grosbach98b05a52011-11-30 01:09:44 +0000278 k_VectorListAllLanes,
Jim Grosbach7636bf62011-12-02 00:35:16 +0000279 k_VectorListIndexed,
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000280 k_ShiftedRegister,
281 k_ShiftedImmediate,
282 k_ShifterImmediate,
283 k_RotateImmediate,
284 k_BitfieldDescriptor,
285 k_Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000286 } Kind;
287
Sean Callanan76264762010-04-02 22:27:05 +0000288 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000289 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000290
291 union {
292 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000293 ARMCC::CondCodes Val;
294 } CC;
295
296 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000297 unsigned Val;
298 } Cop;
299
300 struct {
Jim Grosbach9b8f2a02011-10-12 17:34:41 +0000301 unsigned Val;
302 } CoprocOption;
303
304 struct {
Jim Grosbach89df9962011-08-26 21:43:41 +0000305 unsigned Mask:4;
306 } ITMask;
307
308 struct {
309 ARM_MB::MemBOpt Val;
310 } MBOpt;
311
312 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000313 ARM_PROC::IFlags Val;
314 } IFlags;
315
316 struct {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000317 unsigned Val;
318 } MMask;
319
320 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000321 const char *Data;
322 unsigned Length;
323 } Tok;
324
325 struct {
326 unsigned RegNum;
327 } Reg;
328
Jim Grosbach862019c2011-10-18 23:02:30 +0000329 // A vector register list is a sequential list of 1 to 4 registers.
330 struct {
331 unsigned RegNum;
332 unsigned Count;
Jim Grosbach7636bf62011-12-02 00:35:16 +0000333 unsigned LaneIndex;
Jim Grosbach862019c2011-10-18 23:02:30 +0000334 } VectorList;
335
Bill Wendling8155e5b2010-11-06 22:19:43 +0000336 struct {
Jim Grosbach460a9052011-10-07 23:56:00 +0000337 unsigned Val;
338 } VectorIndex;
339
340 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000341 const MCExpr *Val;
342 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000343
Jim Grosbach9d390362011-10-03 23:38:36 +0000344 struct {
345 unsigned Val; // encoded 8-bit representation
346 } FPImm;
347
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000348 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000349 struct {
350 unsigned BaseRegNum;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000351 // Offset is in OffsetReg or OffsetImm. If both are zero, no offset
352 // was specified.
353 const MCConstantExpr *OffsetImm; // Offset immediate value
354 unsigned OffsetRegNum; // Offset register num, when OffsetImm == NULL
355 ARM_AM::ShiftOpc ShiftType; // Shift type for OffsetReg
Jim Grosbach57dcb852011-10-11 17:29:55 +0000356 unsigned ShiftImm; // shift for OffsetReg.
357 unsigned Alignment; // 0 = no alignment specified
358 // n = alignment in bytes (8, 16, or 32)
Jim Grosbach7ce05792011-08-03 23:50:40 +0000359 unsigned isNegative : 1; // Negated OffsetReg? (~'U' bit)
Jim Grosbache53c87b2011-10-11 15:59:20 +0000360 } Memory;
Owen Anderson00828302011-03-18 22:50:18 +0000361
362 struct {
Jim Grosbach7ce05792011-08-03 23:50:40 +0000363 unsigned RegNum;
Jim Grosbachf4fa3d62011-08-05 21:28:30 +0000364 bool isAdd;
365 ARM_AM::ShiftOpc ShiftTy;
366 unsigned ShiftImm;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000367 } PostIdxReg;
368
369 struct {
Jim Grosbach580f4a92011-07-25 22:20:28 +0000370 bool isASR;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000371 unsigned Imm;
Jim Grosbach580f4a92011-07-25 22:20:28 +0000372 } ShifterImm;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000373 struct {
374 ARM_AM::ShiftOpc ShiftTy;
375 unsigned SrcReg;
376 unsigned ShiftReg;
377 unsigned ShiftImm;
Jim Grosbachaf6981f2011-07-25 20:49:51 +0000378 } RegShiftedReg;
Owen Anderson92a20222011-07-21 18:54:16 +0000379 struct {
380 ARM_AM::ShiftOpc ShiftTy;
381 unsigned SrcReg;
382 unsigned ShiftImm;
Jim Grosbachaf6981f2011-07-25 20:49:51 +0000383 } RegShiftedImm;
Jim Grosbach7e1547e2011-07-27 20:15:40 +0000384 struct {
385 unsigned Imm;
386 } RotImm;
Jim Grosbach293a2ee2011-07-28 21:34:26 +0000387 struct {
388 unsigned LSB;
389 unsigned Width;
390 } Bitfield;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000391 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000392
Bill Wendling146018f2010-11-06 21:42:12 +0000393 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
394public:
Sean Callanan76264762010-04-02 22:27:05 +0000395 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
396 Kind = o.Kind;
397 StartLoc = o.StartLoc;
398 EndLoc = o.EndLoc;
399 switch (Kind) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000400 case k_CondCode:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000401 CC = o.CC;
402 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000403 case k_ITCondMask:
Jim Grosbach89df9962011-08-26 21:43:41 +0000404 ITMask = o.ITMask;
405 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000406 case k_Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000407 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000408 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000409 case k_CCOut:
410 case k_Register:
Sean Callanan76264762010-04-02 22:27:05 +0000411 Reg = o.Reg;
412 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000413 case k_RegisterList:
414 case k_DPRRegisterList:
415 case k_SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000416 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000417 break;
Jim Grosbach862019c2011-10-18 23:02:30 +0000418 case k_VectorList:
Jim Grosbach98b05a52011-11-30 01:09:44 +0000419 case k_VectorListAllLanes:
Jim Grosbach7636bf62011-12-02 00:35:16 +0000420 case k_VectorListIndexed:
Jim Grosbach862019c2011-10-18 23:02:30 +0000421 VectorList = o.VectorList;
422 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000423 case k_CoprocNum:
424 case k_CoprocReg:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000425 Cop = o.Cop;
426 break;
Jim Grosbach9b8f2a02011-10-12 17:34:41 +0000427 case k_CoprocOption:
428 CoprocOption = o.CoprocOption;
429 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000430 case k_Immediate:
Sean Callanan76264762010-04-02 22:27:05 +0000431 Imm = o.Imm;
432 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000433 case k_FPImmediate:
Jim Grosbach9d390362011-10-03 23:38:36 +0000434 FPImm = o.FPImm;
435 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000436 case k_MemBarrierOpt:
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000437 MBOpt = o.MBOpt;
438 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000439 case k_Memory:
Jim Grosbache53c87b2011-10-11 15:59:20 +0000440 Memory = o.Memory;
Sean Callanan76264762010-04-02 22:27:05 +0000441 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000442 case k_PostIndexRegister:
Jim Grosbach7ce05792011-08-03 23:50:40 +0000443 PostIdxReg = o.PostIdxReg;
444 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000445 case k_MSRMask:
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000446 MMask = o.MMask;
447 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000448 case k_ProcIFlags:
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000449 IFlags = o.IFlags;
Owen Anderson00828302011-03-18 22:50:18 +0000450 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000451 case k_ShifterImmediate:
Jim Grosbach580f4a92011-07-25 22:20:28 +0000452 ShifterImm = o.ShifterImm;
Owen Anderson00828302011-03-18 22:50:18 +0000453 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000454 case k_ShiftedRegister:
Jim Grosbachaf6981f2011-07-25 20:49:51 +0000455 RegShiftedReg = o.RegShiftedReg;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000456 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000457 case k_ShiftedImmediate:
Jim Grosbachaf6981f2011-07-25 20:49:51 +0000458 RegShiftedImm = o.RegShiftedImm;
Owen Anderson92a20222011-07-21 18:54:16 +0000459 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000460 case k_RotateImmediate:
Jim Grosbach7e1547e2011-07-27 20:15:40 +0000461 RotImm = o.RotImm;
462 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000463 case k_BitfieldDescriptor:
Jim Grosbach293a2ee2011-07-28 21:34:26 +0000464 Bitfield = o.Bitfield;
465 break;
Jim Grosbach460a9052011-10-07 23:56:00 +0000466 case k_VectorIndex:
467 VectorIndex = o.VectorIndex;
468 break;
Sean Callanan76264762010-04-02 22:27:05 +0000469 }
470 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000471
Sean Callanan76264762010-04-02 22:27:05 +0000472 /// getStartLoc - Get the location of the first token of this operand.
473 SMLoc getStartLoc() const { return StartLoc; }
474 /// getEndLoc - Get the location of the last token of this operand.
475 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000476
Daniel Dunbar8462b302010-08-11 06:36:53 +0000477 ARMCC::CondCodes getCondCode() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000478 assert(Kind == k_CondCode && "Invalid access!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000479 return CC.Val;
480 }
481
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000482 unsigned getCoproc() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000483 assert((Kind == k_CoprocNum || Kind == k_CoprocReg) && "Invalid access!");
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000484 return Cop.Val;
485 }
486
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000487 StringRef getToken() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000488 assert(Kind == k_Token && "Invalid access!");
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000489 return StringRef(Tok.Data, Tok.Length);
490 }
491
492 unsigned getReg() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000493 assert((Kind == k_Register || Kind == k_CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000494 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000495 }
496
Bill Wendling5fa22a12010-11-09 23:28:44 +0000497 const SmallVectorImpl<unsigned> &getRegList() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000498 assert((Kind == k_RegisterList || Kind == k_DPRRegisterList ||
499 Kind == k_SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000500 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000501 }
502
Kevin Enderbycfe07242009-10-13 22:19:02 +0000503 const MCExpr *getImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000504 assert(Kind == k_Immediate && "Invalid access!");
Kevin Enderbycfe07242009-10-13 22:19:02 +0000505 return Imm.Val;
506 }
507
Jim Grosbach9d390362011-10-03 23:38:36 +0000508 unsigned getFPImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000509 assert(Kind == k_FPImmediate && "Invalid access!");
Jim Grosbach9d390362011-10-03 23:38:36 +0000510 return FPImm.Val;
511 }
512
Jim Grosbach460a9052011-10-07 23:56:00 +0000513 unsigned getVectorIndex() const {
514 assert(Kind == k_VectorIndex && "Invalid access!");
515 return VectorIndex.Val;
516 }
517
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000518 ARM_MB::MemBOpt getMemBarrierOpt() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000519 assert(Kind == k_MemBarrierOpt && "Invalid access!");
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000520 return MBOpt.Val;
521 }
522
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000523 ARM_PROC::IFlags getProcIFlags() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000524 assert(Kind == k_ProcIFlags && "Invalid access!");
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000525 return IFlags.Val;
526 }
527
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000528 unsigned getMSRMask() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000529 assert(Kind == k_MSRMask && "Invalid access!");
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000530 return MMask.Val;
531 }
532
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000533 bool isCoprocNum() const { return Kind == k_CoprocNum; }
534 bool isCoprocReg() const { return Kind == k_CoprocReg; }
Jim Grosbach9b8f2a02011-10-12 17:34:41 +0000535 bool isCoprocOption() const { return Kind == k_CoprocOption; }
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000536 bool isCondCode() const { return Kind == k_CondCode; }
537 bool isCCOut() const { return Kind == k_CCOut; }
538 bool isITMask() const { return Kind == k_ITCondMask; }
539 bool isITCondCode() const { return Kind == k_CondCode; }
540 bool isImm() const { return Kind == k_Immediate; }
541 bool isFPImm() const { return Kind == k_FPImmediate; }
Jim Grosbacha77295d2011-09-08 22:07:06 +0000542 bool isImm8s4() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000543 if (Kind != k_Immediate)
Jim Grosbacha77295d2011-09-08 22:07:06 +0000544 return false;
545 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
546 if (!CE) return false;
547 int64_t Value = CE->getValue();
548 return ((Value & 3) == 0) && Value >= -1020 && Value <= 1020;
549 }
Jim Grosbach72f39f82011-08-24 21:22:15 +0000550 bool isImm0_1020s4() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000551 if (Kind != k_Immediate)
Jim Grosbach72f39f82011-08-24 21:22:15 +0000552 return false;
553 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
554 if (!CE) return false;
555 int64_t Value = CE->getValue();
556 return ((Value & 3) == 0) && Value >= 0 && Value <= 1020;
557 }
558 bool isImm0_508s4() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000559 if (Kind != k_Immediate)
Jim Grosbach72f39f82011-08-24 21:22:15 +0000560 return false;
561 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
562 if (!CE) return false;
563 int64_t Value = CE->getValue();
564 return ((Value & 3) == 0) && Value >= 0 && Value <= 508;
565 }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000566 bool isImm0_255() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000567 if (Kind != k_Immediate)
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000568 return false;
569 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
570 if (!CE) return false;
571 int64_t Value = CE->getValue();
572 return Value >= 0 && Value < 256;
573 }
Jim Grosbach587f5062011-12-02 23:34:39 +0000574 bool isImm0_1() const {
575 if (Kind != k_Immediate)
576 return false;
577 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
578 if (!CE) return false;
579 int64_t Value = CE->getValue();
580 return Value >= 0 && Value < 2;
581 }
582 bool isImm0_3() const {
583 if (Kind != k_Immediate)
584 return false;
585 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
586 if (!CE) return false;
587 int64_t Value = CE->getValue();
588 return Value >= 0 && Value < 4;
589 }
Jim Grosbach83ab0702011-07-13 22:01:08 +0000590 bool isImm0_7() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000591 if (Kind != k_Immediate)
Jim Grosbach83ab0702011-07-13 22:01:08 +0000592 return false;
593 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
594 if (!CE) return false;
595 int64_t Value = CE->getValue();
596 return Value >= 0 && Value < 8;
597 }
598 bool isImm0_15() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000599 if (Kind != k_Immediate)
Jim Grosbach83ab0702011-07-13 22:01:08 +0000600 return false;
601 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
602 if (!CE) return false;
603 int64_t Value = CE->getValue();
604 return Value >= 0 && Value < 16;
605 }
Jim Grosbach7c6e42e2011-07-21 23:26:25 +0000606 bool isImm0_31() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000607 if (Kind != k_Immediate)
Jim Grosbach7c6e42e2011-07-21 23:26:25 +0000608 return false;
609 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
610 if (!CE) return false;
611 int64_t Value = CE->getValue();
612 return Value >= 0 && Value < 32;
613 }
Jim Grosbach3b8991c2011-12-07 01:07:24 +0000614 bool isImm8() const {
615 if (Kind != k_Immediate)
616 return false;
617 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
618 if (!CE) return false;
619 int64_t Value = CE->getValue();
620 return Value == 8;
621 }
622 bool isImm16() const {
623 if (Kind != k_Immediate)
624 return false;
625 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
626 if (!CE) return false;
627 int64_t Value = CE->getValue();
628 return Value == 16;
629 }
630 bool isImm32() const {
631 if (Kind != k_Immediate)
632 return false;
633 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
634 if (!CE) return false;
635 int64_t Value = CE->getValue();
636 return Value == 32;
637 }
638 bool isImm1_7() const {
639 if (Kind != k_Immediate)
640 return false;
641 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
642 if (!CE) return false;
643 int64_t Value = CE->getValue();
644 return Value > 0 && Value < 8;
645 }
646 bool isImm1_15() const {
647 if (Kind != k_Immediate)
648 return false;
649 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
650 if (!CE) return false;
651 int64_t Value = CE->getValue();
652 return Value > 0 && Value < 16;
653 }
654 bool isImm1_31() const {
655 if (Kind != k_Immediate)
656 return false;
657 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
658 if (!CE) return false;
659 int64_t Value = CE->getValue();
660 return Value > 0 && Value < 32;
661 }
Jim Grosbachf4943352011-07-25 23:09:14 +0000662 bool isImm1_16() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000663 if (Kind != k_Immediate)
Jim Grosbachf4943352011-07-25 23:09:14 +0000664 return false;
665 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
666 if (!CE) return false;
667 int64_t Value = CE->getValue();
668 return Value > 0 && Value < 17;
669 }
Jim Grosbach4a5ffb32011-07-22 23:16:18 +0000670 bool isImm1_32() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000671 if (Kind != k_Immediate)
Jim Grosbach4a5ffb32011-07-22 23:16:18 +0000672 return false;
673 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
674 if (!CE) return false;
675 int64_t Value = CE->getValue();
676 return Value > 0 && Value < 33;
677 }
Jim Grosbachee10ff82011-11-10 19:18:01 +0000678 bool isImm0_32() const {
679 if (Kind != k_Immediate)
680 return false;
681 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
682 if (!CE) return false;
683 int64_t Value = CE->getValue();
684 return Value >= 0 && Value < 33;
685 }
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000686 bool isImm0_65535() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000687 if (Kind != k_Immediate)
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000688 return false;
689 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
690 if (!CE) return false;
691 int64_t Value = CE->getValue();
692 return Value >= 0 && Value < 65536;
693 }
Jim Grosbachffa32252011-07-19 19:13:28 +0000694 bool isImm0_65535Expr() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000695 if (Kind != k_Immediate)
Jim Grosbachffa32252011-07-19 19:13:28 +0000696 return false;
697 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
698 // If it's not a constant expression, it'll generate a fixup and be
699 // handled later.
700 if (!CE) return true;
701 int64_t Value = CE->getValue();
702 return Value >= 0 && Value < 65536;
703 }
Jim Grosbached838482011-07-26 16:24:27 +0000704 bool isImm24bit() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000705 if (Kind != k_Immediate)
Jim Grosbached838482011-07-26 16:24:27 +0000706 return false;
707 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
708 if (!CE) return false;
709 int64_t Value = CE->getValue();
710 return Value >= 0 && Value <= 0xffffff;
711 }
Jim Grosbach70939ee2011-08-17 21:51:27 +0000712 bool isImmThumbSR() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000713 if (Kind != k_Immediate)
Jim Grosbach70939ee2011-08-17 21:51:27 +0000714 return false;
715 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
716 if (!CE) return false;
717 int64_t Value = CE->getValue();
718 return Value > 0 && Value < 33;
719 }
Jim Grosbachf6c05252011-07-21 17:23:04 +0000720 bool isPKHLSLImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000721 if (Kind != k_Immediate)
Jim Grosbachf6c05252011-07-21 17:23:04 +0000722 return false;
723 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
724 if (!CE) return false;
725 int64_t Value = CE->getValue();
726 return Value >= 0 && Value < 32;
727 }
728 bool isPKHASRImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000729 if (Kind != k_Immediate)
Jim Grosbachf6c05252011-07-21 17:23:04 +0000730 return false;
731 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
732 if (!CE) return false;
733 int64_t Value = CE->getValue();
734 return Value > 0 && Value <= 32;
735 }
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000736 bool isARMSOImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000737 if (Kind != k_Immediate)
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000738 return false;
739 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
740 if (!CE) return false;
741 int64_t Value = CE->getValue();
742 return ARM_AM::getSOImmVal(Value) != -1;
743 }
Jim Grosbache70ec842011-10-28 22:50:54 +0000744 bool isARMSOImmNot() const {
745 if (Kind != k_Immediate)
746 return false;
747 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
748 if (!CE) return false;
749 int64_t Value = CE->getValue();
750 return ARM_AM::getSOImmVal(~Value) != -1;
751 }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000752 bool isT2SOImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000753 if (Kind != k_Immediate)
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000754 return false;
755 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
756 if (!CE) return false;
757 int64_t Value = CE->getValue();
758 return ARM_AM::getT2SOImmVal(Value) != -1;
759 }
Jim Grosbach89a63372011-10-28 22:36:30 +0000760 bool isT2SOImmNot() const {
761 if (Kind != k_Immediate)
762 return false;
763 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
764 if (!CE) return false;
765 int64_t Value = CE->getValue();
766 return ARM_AM::getT2SOImmVal(~Value) != -1;
767 }
Jim Grosbachc27d4f92011-07-22 17:44:50 +0000768 bool isSetEndImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000769 if (Kind != k_Immediate)
Jim Grosbachc27d4f92011-07-22 17:44:50 +0000770 return false;
771 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
772 if (!CE) return false;
773 int64_t Value = CE->getValue();
774 return Value == 1 || Value == 0;
775 }
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000776 bool isReg() const { return Kind == k_Register; }
777 bool isRegList() const { return Kind == k_RegisterList; }
778 bool isDPRRegList() const { return Kind == k_DPRRegisterList; }
779 bool isSPRRegList() const { return Kind == k_SPRRegisterList; }
780 bool isToken() const { return Kind == k_Token; }
781 bool isMemBarrierOpt() const { return Kind == k_MemBarrierOpt; }
782 bool isMemory() const { return Kind == k_Memory; }
783 bool isShifterImm() const { return Kind == k_ShifterImmediate; }
784 bool isRegShiftedReg() const { return Kind == k_ShiftedRegister; }
785 bool isRegShiftedImm() const { return Kind == k_ShiftedImmediate; }
786 bool isRotImm() const { return Kind == k_RotateImmediate; }
787 bool isBitfield() const { return Kind == k_BitfieldDescriptor; }
788 bool isPostIdxRegShifted() const { return Kind == k_PostIndexRegister; }
Jim Grosbachf4fa3d62011-08-05 21:28:30 +0000789 bool isPostIdxReg() const {
Jim Grosbach430052b2011-11-14 17:52:47 +0000790 return Kind == k_PostIndexRegister && PostIdxReg.ShiftTy ==ARM_AM::no_shift;
Jim Grosbachf4fa3d62011-08-05 21:28:30 +0000791 }
Jim Grosbach57dcb852011-10-11 17:29:55 +0000792 bool isMemNoOffset(bool alignOK = false) const {
Jim Grosbachf6c35c52011-10-10 23:06:42 +0000793 if (!isMemory())
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000794 return false;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000795 // No offset of any kind.
Jim Grosbach57dcb852011-10-11 17:29:55 +0000796 return Memory.OffsetRegNum == 0 && Memory.OffsetImm == 0 &&
797 (alignOK || Memory.Alignment == 0);
798 }
799 bool isAlignedMemory() const {
800 return isMemNoOffset(true);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000801 }
Jim Grosbach7ce05792011-08-03 23:50:40 +0000802 bool isAddrMode2() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000803 if (!isMemory() || Memory.Alignment != 0) return false;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000804 // Check for register offset.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000805 if (Memory.OffsetRegNum) return true;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000806 // Immediate offset in range [-4095, 4095].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000807 if (!Memory.OffsetImm) return true;
808 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbach7ce05792011-08-03 23:50:40 +0000809 return Val > -4096 && Val < 4096;
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000810 }
Jim Grosbach039c2e12011-08-04 23:01:30 +0000811 bool isAM2OffsetImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000812 if (Kind != k_Immediate)
Jim Grosbach039c2e12011-08-04 23:01:30 +0000813 return false;
814 // Immediate offset in range [-4095, 4095].
815 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
816 if (!CE) return false;
817 int64_t Val = CE->getValue();
818 return Val > -4096 && Val < 4096;
819 }
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000820 bool isAddrMode3() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000821 if (!isMemory() || Memory.Alignment != 0) return false;
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000822 // No shifts are legal for AM3.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000823 if (Memory.ShiftType != ARM_AM::no_shift) return false;
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000824 // Check for register offset.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000825 if (Memory.OffsetRegNum) return true;
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000826 // Immediate offset in range [-255, 255].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000827 if (!Memory.OffsetImm) return true;
828 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000829 return Val > -256 && Val < 256;
830 }
831 bool isAM3Offset() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000832 if (Kind != k_Immediate && Kind != k_PostIndexRegister)
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000833 return false;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000834 if (Kind == k_PostIndexRegister)
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000835 return PostIdxReg.ShiftTy == ARM_AM::no_shift;
836 // Immediate offset in range [-255, 255].
837 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
838 if (!CE) return false;
839 int64_t Val = CE->getValue();
Jim Grosbach251bf252011-08-10 21:56:18 +0000840 // Special case, #-0 is INT32_MIN.
841 return (Val > -256 && Val < 256) || Val == INT32_MIN;
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000842 }
Jim Grosbach7ce05792011-08-03 23:50:40 +0000843 bool isAddrMode5() const {
Jim Grosbach681460f2011-11-01 01:24:45 +0000844 // If we have an immediate that's not a constant, treat it as a label
845 // reference needing a fixup. If it is a constant, it's something else
846 // and we reject it.
847 if (Kind == k_Immediate && !isa<MCConstantExpr>(getImm()))
848 return true;
Jim Grosbach57dcb852011-10-11 17:29:55 +0000849 if (!isMemory() || Memory.Alignment != 0) return false;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000850 // Check for register offset.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000851 if (Memory.OffsetRegNum) return false;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000852 // Immediate offset in range [-1020, 1020] and a multiple of 4.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000853 if (!Memory.OffsetImm) return true;
854 int64_t Val = Memory.OffsetImm->getValue();
Owen Anderson0da10cf2011-08-29 19:36:44 +0000855 return (Val >= -1020 && Val <= 1020 && ((Val & 3) == 0)) ||
Jim Grosbach681460f2011-11-01 01:24:45 +0000856 Val == INT32_MIN;
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000857 }
Jim Grosbach7f739be2011-09-19 22:21:13 +0000858 bool isMemTBB() const {
Jim Grosbache53c87b2011-10-11 15:59:20 +0000859 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
Jim Grosbach57dcb852011-10-11 17:29:55 +0000860 Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
Jim Grosbach7f739be2011-09-19 22:21:13 +0000861 return false;
862 return true;
863 }
864 bool isMemTBH() const {
Jim Grosbache53c87b2011-10-11 15:59:20 +0000865 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
Jim Grosbach57dcb852011-10-11 17:29:55 +0000866 Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm != 1 ||
867 Memory.Alignment != 0 )
Jim Grosbach7f739be2011-09-19 22:21:13 +0000868 return false;
869 return true;
870 }
Jim Grosbach7ce05792011-08-03 23:50:40 +0000871 bool isMemRegOffset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000872 if (!isMemory() || !Memory.OffsetRegNum || Memory.Alignment != 0)
Bill Wendlingf4caf692010-12-14 03:36:38 +0000873 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000874 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000875 }
Jim Grosbachab899c12011-09-07 23:10:15 +0000876 bool isT2MemRegOffset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000877 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
878 Memory.Alignment != 0)
Jim Grosbachab899c12011-09-07 23:10:15 +0000879 return false;
880 // Only lsl #{0, 1, 2, 3} allowed.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000881 if (Memory.ShiftType == ARM_AM::no_shift)
Jim Grosbachab899c12011-09-07 23:10:15 +0000882 return true;
Jim Grosbache53c87b2011-10-11 15:59:20 +0000883 if (Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm > 3)
Jim Grosbachab899c12011-09-07 23:10:15 +0000884 return false;
885 return true;
886 }
Jim Grosbach7ce05792011-08-03 23:50:40 +0000887 bool isMemThumbRR() const {
888 // Thumb reg+reg addressing is simple. Just two registers, a base and
889 // an offset. No shifts, negations or any other complicating factors.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000890 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
Jim Grosbach57dcb852011-10-11 17:29:55 +0000891 Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000892 return false;
Jim Grosbache53c87b2011-10-11 15:59:20 +0000893 return isARMLowRegister(Memory.BaseRegNum) &&
894 (!Memory.OffsetRegNum || isARMLowRegister(Memory.OffsetRegNum));
Jim Grosbach60f91a32011-08-19 17:55:24 +0000895 }
896 bool isMemThumbRIs4() const {
Jim Grosbache53c87b2011-10-11 15:59:20 +0000897 if (!isMemory() || Memory.OffsetRegNum != 0 ||
Jim Grosbach57dcb852011-10-11 17:29:55 +0000898 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
Jim Grosbach60f91a32011-08-19 17:55:24 +0000899 return false;
900 // Immediate offset, multiple of 4 in range [0, 124].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000901 if (!Memory.OffsetImm) return true;
902 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbachecd85892011-08-19 18:13:48 +0000903 return Val >= 0 && Val <= 124 && (Val % 4) == 0;
904 }
Jim Grosbach38466302011-08-19 18:55:51 +0000905 bool isMemThumbRIs2() const {
Jim Grosbache53c87b2011-10-11 15:59:20 +0000906 if (!isMemory() || Memory.OffsetRegNum != 0 ||
Jim Grosbach57dcb852011-10-11 17:29:55 +0000907 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
Jim Grosbach38466302011-08-19 18:55:51 +0000908 return false;
909 // Immediate offset, multiple of 4 in range [0, 62].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000910 if (!Memory.OffsetImm) return true;
911 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbach38466302011-08-19 18:55:51 +0000912 return Val >= 0 && Val <= 62 && (Val % 2) == 0;
913 }
Jim Grosbach48ff5ff2011-08-19 18:49:59 +0000914 bool isMemThumbRIs1() const {
Jim Grosbache53c87b2011-10-11 15:59:20 +0000915 if (!isMemory() || Memory.OffsetRegNum != 0 ||
Jim Grosbach57dcb852011-10-11 17:29:55 +0000916 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
Jim Grosbach48ff5ff2011-08-19 18:49:59 +0000917 return false;
918 // Immediate offset in range [0, 31].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000919 if (!Memory.OffsetImm) return true;
920 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbach48ff5ff2011-08-19 18:49:59 +0000921 return Val >= 0 && Val <= 31;
922 }
Jim Grosbachecd85892011-08-19 18:13:48 +0000923 bool isMemThumbSPI() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000924 if (!isMemory() || Memory.OffsetRegNum != 0 ||
925 Memory.BaseRegNum != ARM::SP || Memory.Alignment != 0)
Jim Grosbachecd85892011-08-19 18:13:48 +0000926 return false;
927 // Immediate offset, multiple of 4 in range [0, 1020].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000928 if (!Memory.OffsetImm) return true;
929 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbachecd85892011-08-19 18:13:48 +0000930 return Val >= 0 && Val <= 1020 && (Val % 4) == 0;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000931 }
Jim Grosbacha77295d2011-09-08 22:07:06 +0000932 bool isMemImm8s4Offset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000933 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbacha77295d2011-09-08 22:07:06 +0000934 return false;
935 // Immediate offset a multiple of 4 in range [-1020, 1020].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000936 if (!Memory.OffsetImm) return true;
937 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbacha77295d2011-09-08 22:07:06 +0000938 return Val >= -1020 && Val <= 1020 && (Val & 3) == 0;
939 }
Jim Grosbachb6aed502011-09-09 18:37:27 +0000940 bool isMemImm0_1020s4Offset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000941 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbachb6aed502011-09-09 18:37:27 +0000942 return false;
943 // Immediate offset a multiple of 4 in range [0, 1020].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000944 if (!Memory.OffsetImm) return true;
945 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbachb6aed502011-09-09 18:37:27 +0000946 return Val >= 0 && Val <= 1020 && (Val & 3) == 0;
947 }
Jim Grosbach7ce05792011-08-03 23:50:40 +0000948 bool isMemImm8Offset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000949 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbach7ce05792011-08-03 23:50:40 +0000950 return false;
951 // Immediate offset in range [-255, 255].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000952 if (!Memory.OffsetImm) return true;
953 int64_t Val = Memory.OffsetImm->getValue();
Owen Anderson4d2a0012011-09-23 22:25:02 +0000954 return (Val == INT32_MIN) || (Val > -256 && Val < 256);
Jim Grosbach7ce05792011-08-03 23:50:40 +0000955 }
Jim Grosbachf0eee6e2011-09-07 23:39:14 +0000956 bool isMemPosImm8Offset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000957 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbachf0eee6e2011-09-07 23:39:14 +0000958 return false;
959 // Immediate offset in range [0, 255].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000960 if (!Memory.OffsetImm) return true;
961 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbachf0eee6e2011-09-07 23:39:14 +0000962 return Val >= 0 && Val < 256;
963 }
Jim Grosbacha8307dd2011-09-07 20:58:57 +0000964 bool isMemNegImm8Offset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000965 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbacha8307dd2011-09-07 20:58:57 +0000966 return false;
967 // Immediate offset in range [-255, -1].
Jim Grosbachdf33e0d2011-12-06 04:49:29 +0000968 if (!Memory.OffsetImm) return false;
Jim Grosbache53c87b2011-10-11 15:59:20 +0000969 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbachdf33e0d2011-12-06 04:49:29 +0000970 return (Val == INT32_MIN) || (Val > -256 && Val < 0);
Jim Grosbacha8307dd2011-09-07 20:58:57 +0000971 }
972 bool isMemUImm12Offset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000973 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbacha8307dd2011-09-07 20:58:57 +0000974 return false;
975 // Immediate offset in range [0, 4095].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000976 if (!Memory.OffsetImm) return true;
977 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbacha8307dd2011-09-07 20:58:57 +0000978 return (Val >= 0 && Val < 4096);
979 }
Jim Grosbach7ce05792011-08-03 23:50:40 +0000980 bool isMemImm12Offset() const {
Jim Grosbach09176e12011-08-08 20:59:31 +0000981 // If we have an immediate that's not a constant, treat it as a label
982 // reference needing a fixup. If it is a constant, it's something else
983 // and we reject it.
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000984 if (Kind == k_Immediate && !isa<MCConstantExpr>(getImm()))
Jim Grosbach09176e12011-08-08 20:59:31 +0000985 return true;
986
Jim Grosbach57dcb852011-10-11 17:29:55 +0000987 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbach7ce05792011-08-03 23:50:40 +0000988 return false;
989 // Immediate offset in range [-4095, 4095].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000990 if (!Memory.OffsetImm) return true;
991 int64_t Val = Memory.OffsetImm->getValue();
Owen Anderson0da10cf2011-08-29 19:36:44 +0000992 return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
Jim Grosbach7ce05792011-08-03 23:50:40 +0000993 }
994 bool isPostIdxImm8() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000995 if (Kind != k_Immediate)
Jim Grosbach7ce05792011-08-03 23:50:40 +0000996 return false;
997 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
998 if (!CE) return false;
999 int64_t Val = CE->getValue();
Owen Anderson63553c72011-08-29 17:17:09 +00001000 return (Val > -256 && Val < 256) || (Val == INT32_MIN);
Jim Grosbach7ce05792011-08-03 23:50:40 +00001001 }
Jim Grosbach2bd01182011-10-11 21:55:36 +00001002 bool isPostIdxImm8s4() const {
1003 if (Kind != k_Immediate)
1004 return false;
1005 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1006 if (!CE) return false;
1007 int64_t Val = CE->getValue();
1008 return ((Val & 3) == 0 && Val >= -1020 && Val <= 1020) ||
1009 (Val == INT32_MIN);
1010 }
Jim Grosbach7ce05792011-08-03 23:50:40 +00001011
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001012 bool isMSRMask() const { return Kind == k_MSRMask; }
1013 bool isProcIFlags() const { return Kind == k_ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001014
Jim Grosbach0e387b22011-10-17 22:26:03 +00001015 // NEON operands.
Jim Grosbach862019c2011-10-18 23:02:30 +00001016 bool isVecListOneD() const {
1017 if (Kind != k_VectorList) return false;
1018 return VectorList.Count == 1;
1019 }
1020
Jim Grosbach280dfad2011-10-21 18:54:25 +00001021 bool isVecListTwoD() const {
1022 if (Kind != k_VectorList) return false;
1023 return VectorList.Count == 2;
1024 }
1025
Jim Grosbachcdcfa282011-10-21 20:02:19 +00001026 bool isVecListThreeD() const {
1027 if (Kind != k_VectorList) return false;
1028 return VectorList.Count == 3;
1029 }
1030
Jim Grosbachb6310312011-10-21 20:35:01 +00001031 bool isVecListFourD() const {
1032 if (Kind != k_VectorList) return false;
1033 return VectorList.Count == 4;
1034 }
1035
Jim Grosbach4661d4c2011-10-21 22:21:10 +00001036 bool isVecListTwoQ() const {
1037 if (Kind != k_VectorList) return false;
1038 //FIXME: We haven't taught the parser to handle by-two register lists
1039 // yet, so don't pretend to know one.
1040 return VectorList.Count == 2 && false;
1041 }
1042
Jim Grosbach98b05a52011-11-30 01:09:44 +00001043 bool isVecListOneDAllLanes() const {
1044 if (Kind != k_VectorListAllLanes) return false;
1045 return VectorList.Count == 1;
1046 }
1047
Jim Grosbach13af2222011-11-30 18:21:25 +00001048 bool isVecListTwoDAllLanes() const {
1049 if (Kind != k_VectorListAllLanes) return false;
1050 return VectorList.Count == 2;
1051 }
1052
Jim Grosbach7636bf62011-12-02 00:35:16 +00001053 bool isVecListOneDByteIndexed() const {
1054 if (Kind != k_VectorListIndexed) return false;
1055 return VectorList.Count == 1 && VectorList.LaneIndex <= 7;
1056 }
1057
Jim Grosbach460a9052011-10-07 23:56:00 +00001058 bool isVectorIndex8() const {
1059 if (Kind != k_VectorIndex) return false;
1060 return VectorIndex.Val < 8;
1061 }
1062 bool isVectorIndex16() const {
1063 if (Kind != k_VectorIndex) return false;
1064 return VectorIndex.Val < 4;
1065 }
1066 bool isVectorIndex32() const {
1067 if (Kind != k_VectorIndex) return false;
1068 return VectorIndex.Val < 2;
1069 }
1070
Jim Grosbach0e387b22011-10-17 22:26:03 +00001071 bool isNEONi8splat() const {
1072 if (Kind != k_Immediate)
1073 return false;
1074 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1075 // Must be a constant.
1076 if (!CE) return false;
1077 int64_t Value = CE->getValue();
1078 // i8 value splatted across 8 bytes. The immediate is just the 8 byte
1079 // value.
Jim Grosbach0e387b22011-10-17 22:26:03 +00001080 return Value >= 0 && Value < 256;
1081 }
Jim Grosbach460a9052011-10-07 23:56:00 +00001082
Jim Grosbachea461102011-10-17 23:09:09 +00001083 bool isNEONi16splat() const {
1084 if (Kind != k_Immediate)
1085 return false;
1086 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1087 // Must be a constant.
1088 if (!CE) return false;
1089 int64_t Value = CE->getValue();
1090 // i16 value in the range [0,255] or [0x0100, 0xff00]
1091 return (Value >= 0 && Value < 256) || (Value >= 0x0100 && Value <= 0xff00);
1092 }
1093
Jim Grosbach6248a542011-10-18 00:22:00 +00001094 bool isNEONi32splat() const {
1095 if (Kind != k_Immediate)
1096 return false;
1097 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1098 // Must be a constant.
1099 if (!CE) return false;
1100 int64_t Value = CE->getValue();
1101 // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X.
1102 return (Value >= 0 && Value < 256) ||
1103 (Value >= 0x0100 && Value <= 0xff00) ||
1104 (Value >= 0x010000 && Value <= 0xff0000) ||
1105 (Value >= 0x01000000 && Value <= 0xff000000);
1106 }
1107
1108 bool isNEONi32vmov() const {
1109 if (Kind != k_Immediate)
1110 return false;
1111 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1112 // Must be a constant.
1113 if (!CE) return false;
1114 int64_t Value = CE->getValue();
1115 // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
1116 // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
1117 return (Value >= 0 && Value < 256) ||
1118 (Value >= 0x0100 && Value <= 0xff00) ||
1119 (Value >= 0x010000 && Value <= 0xff0000) ||
1120 (Value >= 0x01000000 && Value <= 0xff000000) ||
1121 (Value >= 0x01ff && Value <= 0xffff && (Value & 0xff) == 0xff) ||
1122 (Value >= 0x01ffff && Value <= 0xffffff && (Value & 0xffff) == 0xffff);
1123 }
1124
Jim Grosbachf2f5bc62011-10-18 16:18:11 +00001125 bool isNEONi64splat() const {
1126 if (Kind != k_Immediate)
1127 return false;
1128 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1129 // Must be a constant.
1130 if (!CE) return false;
1131 uint64_t Value = CE->getValue();
1132 // i64 value with each byte being either 0 or 0xff.
1133 for (unsigned i = 0; i < 8; ++i)
1134 if ((Value & 0xff) != 0 && (Value & 0xff) != 0xff) return false;
1135 return true;
1136 }
1137
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001138 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +00001139 // Add as immediates when possible. Null MCExpr = 0.
1140 if (Expr == 0)
1141 Inst.addOperand(MCOperand::CreateImm(0));
1142 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001143 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1144 else
1145 Inst.addOperand(MCOperand::CreateExpr(Expr));
1146 }
1147
Daniel Dunbar8462b302010-08-11 06:36:53 +00001148 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001149 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +00001150 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +00001151 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
1152 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +00001153 }
1154
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001155 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
1156 assert(N == 1 && "Invalid number of operands!");
1157 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
1158 }
1159
Jim Grosbach9b8f2a02011-10-12 17:34:41 +00001160 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
1161 assert(N == 1 && "Invalid number of operands!");
1162 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
1163 }
1164
1165 void addCoprocOptionOperands(MCInst &Inst, unsigned N) const {
1166 assert(N == 1 && "Invalid number of operands!");
1167 Inst.addOperand(MCOperand::CreateImm(CoprocOption.Val));
1168 }
1169
Jim Grosbach89df9962011-08-26 21:43:41 +00001170 void addITMaskOperands(MCInst &Inst, unsigned N) const {
1171 assert(N == 1 && "Invalid number of operands!");
1172 Inst.addOperand(MCOperand::CreateImm(ITMask.Mask));
1173 }
1174
1175 void addITCondCodeOperands(MCInst &Inst, unsigned N) const {
1176 assert(N == 1 && "Invalid number of operands!");
1177 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
1178 }
1179
Jim Grosbachd67641b2010-12-06 18:21:12 +00001180 void addCCOutOperands(MCInst &Inst, unsigned N) const {
1181 assert(N == 1 && "Invalid number of operands!");
1182 Inst.addOperand(MCOperand::CreateReg(getReg()));
1183 }
1184
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001185 void addRegOperands(MCInst &Inst, unsigned N) const {
1186 assert(N == 1 && "Invalid number of operands!");
1187 Inst.addOperand(MCOperand::CreateReg(getReg()));
1188 }
1189
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001190 void addRegShiftedRegOperands(MCInst &Inst, unsigned N) const {
Jim Grosbache8606dc2011-07-13 17:50:29 +00001191 assert(N == 3 && "Invalid number of operands!");
Jim Grosbach430052b2011-11-14 17:52:47 +00001192 assert(isRegShiftedReg() &&
1193 "addRegShiftedRegOperands() on non RegShiftedReg!");
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001194 Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.SrcReg));
1195 Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.ShiftReg));
Jim Grosbache8606dc2011-07-13 17:50:29 +00001196 Inst.addOperand(MCOperand::CreateImm(
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001197 ARM_AM::getSORegOpc(RegShiftedReg.ShiftTy, RegShiftedReg.ShiftImm)));
Jim Grosbache8606dc2011-07-13 17:50:29 +00001198 }
1199
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001200 void addRegShiftedImmOperands(MCInst &Inst, unsigned N) const {
Owen Anderson152d4a42011-07-21 23:38:37 +00001201 assert(N == 2 && "Invalid number of operands!");
Jim Grosbach430052b2011-11-14 17:52:47 +00001202 assert(isRegShiftedImm() &&
1203 "addRegShiftedImmOperands() on non RegShiftedImm!");
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001204 Inst.addOperand(MCOperand::CreateReg(RegShiftedImm.SrcReg));
Owen Anderson92a20222011-07-21 18:54:16 +00001205 Inst.addOperand(MCOperand::CreateImm(
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001206 ARM_AM::getSORegOpc(RegShiftedImm.ShiftTy, RegShiftedImm.ShiftImm)));
Owen Anderson92a20222011-07-21 18:54:16 +00001207 }
1208
Jim Grosbach580f4a92011-07-25 22:20:28 +00001209 void addShifterImmOperands(MCInst &Inst, unsigned N) const {
Owen Anderson00828302011-03-18 22:50:18 +00001210 assert(N == 1 && "Invalid number of operands!");
Jim Grosbach580f4a92011-07-25 22:20:28 +00001211 Inst.addOperand(MCOperand::CreateImm((ShifterImm.isASR << 5) |
1212 ShifterImm.Imm));
Owen Anderson00828302011-03-18 22:50:18 +00001213 }
1214
Bill Wendling87f4f9a2010-11-08 23:49:57 +00001215 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +00001216 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +00001217 const SmallVectorImpl<unsigned> &RegList = getRegList();
1218 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +00001219 I = RegList.begin(), E = RegList.end(); I != E; ++I)
1220 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +00001221 }
1222
Bill Wendling0f630752010-11-17 04:32:08 +00001223 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
1224 addRegListOperands(Inst, N);
1225 }
1226
1227 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
1228 addRegListOperands(Inst, N);
1229 }
1230
Jim Grosbach7e1547e2011-07-27 20:15:40 +00001231 void addRotImmOperands(MCInst &Inst, unsigned N) const {
1232 assert(N == 1 && "Invalid number of operands!");
1233 // Encoded as val>>3. The printer handles display as 8, 16, 24.
1234 Inst.addOperand(MCOperand::CreateImm(RotImm.Imm >> 3));
1235 }
1236
Jim Grosbach293a2ee2011-07-28 21:34:26 +00001237 void addBitfieldOperands(MCInst &Inst, unsigned N) const {
1238 assert(N == 1 && "Invalid number of operands!");
1239 // Munge the lsb/width into a bitfield mask.
1240 unsigned lsb = Bitfield.LSB;
1241 unsigned width = Bitfield.Width;
1242 // Make a 32-bit mask w/ the referenced bits clear and all other bits set.
1243 uint32_t Mask = ~(((uint32_t)0xffffffff >> lsb) << (32 - width) >>
1244 (32 - (lsb + width)));
1245 Inst.addOperand(MCOperand::CreateImm(Mask));
1246 }
1247
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001248 void addImmOperands(MCInst &Inst, unsigned N) const {
1249 assert(N == 1 && "Invalid number of operands!");
1250 addExpr(Inst, getImm());
1251 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001252
Jim Grosbach9d390362011-10-03 23:38:36 +00001253 void addFPImmOperands(MCInst &Inst, unsigned N) const {
1254 assert(N == 1 && "Invalid number of operands!");
1255 Inst.addOperand(MCOperand::CreateImm(getFPImm()));
1256 }
1257
Jim Grosbacha77295d2011-09-08 22:07:06 +00001258 void addImm8s4Operands(MCInst &Inst, unsigned N) const {
1259 assert(N == 1 && "Invalid number of operands!");
1260 // FIXME: We really want to scale the value here, but the LDRD/STRD
1261 // instruction don't encode operands that way yet.
1262 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1263 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1264 }
1265
Jim Grosbach72f39f82011-08-24 21:22:15 +00001266 void addImm0_1020s4Operands(MCInst &Inst, unsigned N) const {
1267 assert(N == 1 && "Invalid number of operands!");
1268 // The immediate is scaled by four in the encoding and is stored
1269 // in the MCInst as such. Lop off the low two bits here.
1270 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1271 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1272 }
1273
1274 void addImm0_508s4Operands(MCInst &Inst, unsigned N) const {
1275 assert(N == 1 && "Invalid number of operands!");
1276 // The immediate is scaled by four in the encoding and is stored
1277 // in the MCInst as such. Lop off the low two bits here.
1278 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1279 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1280 }
1281
Jim Grosbachf4943352011-07-25 23:09:14 +00001282 void addImm1_16Operands(MCInst &Inst, unsigned N) const {
1283 assert(N == 1 && "Invalid number of operands!");
1284 // The constant encodes as the immediate-1, and we store in the instruction
1285 // the bits as encoded, so subtract off one here.
1286 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1287 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1288 }
1289
Jim Grosbach4a5ffb32011-07-22 23:16:18 +00001290 void addImm1_32Operands(MCInst &Inst, unsigned N) const {
1291 assert(N == 1 && "Invalid number of operands!");
1292 // The constant encodes as the immediate-1, and we store in the instruction
1293 // the bits as encoded, so subtract off one here.
1294 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1295 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1296 }
1297
Jim Grosbach70939ee2011-08-17 21:51:27 +00001298 void addImmThumbSROperands(MCInst &Inst, unsigned N) const {
1299 assert(N == 1 && "Invalid number of operands!");
1300 // The constant encodes as the immediate, except for 32, which encodes as
1301 // zero.
1302 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1303 unsigned Imm = CE->getValue();
1304 Inst.addOperand(MCOperand::CreateImm((Imm == 32 ? 0 : Imm)));
1305 }
1306
Jim Grosbachf6c05252011-07-21 17:23:04 +00001307 void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
1308 assert(N == 1 && "Invalid number of operands!");
1309 // An ASR value of 32 encodes as 0, so that's how we want to add it to
1310 // the instruction as well.
1311 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1312 int Val = CE->getValue();
1313 Inst.addOperand(MCOperand::CreateImm(Val == 32 ? 0 : Val));
1314 }
1315
Jim Grosbach89a63372011-10-28 22:36:30 +00001316 void addT2SOImmNotOperands(MCInst &Inst, unsigned N) const {
1317 assert(N == 1 && "Invalid number of operands!");
1318 // The operand is actually a t2_so_imm, but we have its bitwise
1319 // negation in the assembly source, so twiddle it here.
1320 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1321 Inst.addOperand(MCOperand::CreateImm(~CE->getValue()));
1322 }
1323
Jim Grosbache70ec842011-10-28 22:50:54 +00001324 void addARMSOImmNotOperands(MCInst &Inst, unsigned N) const {
1325 assert(N == 1 && "Invalid number of operands!");
1326 // The operand is actually a so_imm, but we have its bitwise
1327 // negation in the assembly source, so twiddle it here.
1328 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1329 Inst.addOperand(MCOperand::CreateImm(~CE->getValue()));
1330 }
1331
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001332 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
1333 assert(N == 1 && "Invalid number of operands!");
1334 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
1335 }
1336
Jim Grosbach7ce05792011-08-03 23:50:40 +00001337 void addMemNoOffsetOperands(MCInst &Inst, unsigned N) const {
1338 assert(N == 1 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001339 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +00001340 }
1341
Jim Grosbach57dcb852011-10-11 17:29:55 +00001342 void addAlignedMemoryOperands(MCInst &Inst, unsigned N) const {
1343 assert(N == 2 && "Invalid number of operands!");
1344 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1345 Inst.addOperand(MCOperand::CreateImm(Memory.Alignment));
1346 }
1347
Jim Grosbach7ce05792011-08-03 23:50:40 +00001348 void addAddrMode2Operands(MCInst &Inst, unsigned N) const {
1349 assert(N == 3 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001350 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1351 if (!Memory.OffsetRegNum) {
Jim Grosbach7ce05792011-08-03 23:50:40 +00001352 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1353 // Special case for #-0
1354 if (Val == INT32_MIN) Val = 0;
1355 if (Val < 0) Val = -Val;
1356 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1357 } else {
1358 // For register offset, we encode the shift type and negation flag
1359 // here.
Jim Grosbache53c87b2011-10-11 15:59:20 +00001360 Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1361 Memory.ShiftImm, Memory.ShiftType);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001362 }
Jim Grosbache53c87b2011-10-11 15:59:20 +00001363 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1364 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
Jim Grosbach7ce05792011-08-03 23:50:40 +00001365 Inst.addOperand(MCOperand::CreateImm(Val));
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001366 }
1367
Jim Grosbach039c2e12011-08-04 23:01:30 +00001368 void addAM2OffsetImmOperands(MCInst &Inst, unsigned N) const {
1369 assert(N == 2 && "Invalid number of operands!");
1370 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1371 assert(CE && "non-constant AM2OffsetImm operand!");
1372 int32_t Val = CE->getValue();
1373 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1374 // Special case for #-0
1375 if (Val == INT32_MIN) Val = 0;
1376 if (Val < 0) Val = -Val;
1377 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1378 Inst.addOperand(MCOperand::CreateReg(0));
1379 Inst.addOperand(MCOperand::CreateImm(Val));
1380 }
1381
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001382 void addAddrMode3Operands(MCInst &Inst, unsigned N) const {
1383 assert(N == 3 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001384 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1385 if (!Memory.OffsetRegNum) {
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001386 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1387 // Special case for #-0
1388 if (Val == INT32_MIN) Val = 0;
1389 if (Val < 0) Val = -Val;
1390 Val = ARM_AM::getAM3Opc(AddSub, Val);
1391 } else {
1392 // For register offset, we encode the shift type and negation flag
1393 // here.
Jim Grosbache53c87b2011-10-11 15:59:20 +00001394 Val = ARM_AM::getAM3Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add, 0);
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001395 }
Jim Grosbache53c87b2011-10-11 15:59:20 +00001396 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1397 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001398 Inst.addOperand(MCOperand::CreateImm(Val));
1399 }
1400
1401 void addAM3OffsetOperands(MCInst &Inst, unsigned N) const {
1402 assert(N == 2 && "Invalid number of operands!");
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001403 if (Kind == k_PostIndexRegister) {
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001404 int32_t Val =
1405 ARM_AM::getAM3Opc(PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub, 0);
1406 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1407 Inst.addOperand(MCOperand::CreateImm(Val));
Jim Grosbach251bf252011-08-10 21:56:18 +00001408 return;
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001409 }
1410
1411 // Constant offset.
1412 const MCConstantExpr *CE = static_cast<const MCConstantExpr*>(getImm());
1413 int32_t Val = CE->getValue();
1414 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1415 // Special case for #-0
1416 if (Val == INT32_MIN) Val = 0;
1417 if (Val < 0) Val = -Val;
Jim Grosbach251bf252011-08-10 21:56:18 +00001418 Val = ARM_AM::getAM3Opc(AddSub, Val);
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001419 Inst.addOperand(MCOperand::CreateReg(0));
1420 Inst.addOperand(MCOperand::CreateImm(Val));
1421 }
1422
Jim Grosbach7ce05792011-08-03 23:50:40 +00001423 void addAddrMode5Operands(MCInst &Inst, unsigned N) const {
1424 assert(N == 2 && "Invalid number of operands!");
Jim Grosbach681460f2011-11-01 01:24:45 +00001425 // If we have an immediate that's not a constant, treat it as a label
1426 // reference needing a fixup. If it is a constant, it's something else
1427 // and we reject it.
1428 if (isImm()) {
1429 Inst.addOperand(MCOperand::CreateExpr(getImm()));
1430 Inst.addOperand(MCOperand::CreateImm(0));
1431 return;
1432 }
1433
Jim Grosbach7ce05792011-08-03 23:50:40 +00001434 // The lower two bits are always zero and as such are not encoded.
Jim Grosbache53c87b2011-10-11 15:59:20 +00001435 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
Jim Grosbach7ce05792011-08-03 23:50:40 +00001436 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1437 // Special case for #-0
1438 if (Val == INT32_MIN) Val = 0;
1439 if (Val < 0) Val = -Val;
1440 Val = ARM_AM::getAM5Opc(AddSub, Val);
Jim Grosbache53c87b2011-10-11 15:59:20 +00001441 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbach7ce05792011-08-03 23:50:40 +00001442 Inst.addOperand(MCOperand::CreateImm(Val));
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001443 }
1444
Jim Grosbacha77295d2011-09-08 22:07:06 +00001445 void addMemImm8s4OffsetOperands(MCInst &Inst, unsigned N) const {
1446 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001447 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1448 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbacha77295d2011-09-08 22:07:06 +00001449 Inst.addOperand(MCOperand::CreateImm(Val));
1450 }
1451
Jim Grosbachb6aed502011-09-09 18:37:27 +00001452 void addMemImm0_1020s4OffsetOperands(MCInst &Inst, unsigned N) const {
1453 assert(N == 2 && "Invalid number of operands!");
1454 // The lower two bits are always zero and as such are not encoded.
Jim Grosbache53c87b2011-10-11 15:59:20 +00001455 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1456 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbachb6aed502011-09-09 18:37:27 +00001457 Inst.addOperand(MCOperand::CreateImm(Val));
1458 }
1459
Jim Grosbach7ce05792011-08-03 23:50:40 +00001460 void addMemImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1461 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001462 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1463 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbach7ce05792011-08-03 23:50:40 +00001464 Inst.addOperand(MCOperand::CreateImm(Val));
Chris Lattner14b93852010-10-29 00:27:31 +00001465 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001466
Jim Grosbachf0eee6e2011-09-07 23:39:14 +00001467 void addMemPosImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1468 addMemImm8OffsetOperands(Inst, N);
1469 }
1470
Jim Grosbacha8307dd2011-09-07 20:58:57 +00001471 void addMemNegImm8OffsetOperands(MCInst &Inst, unsigned N) const {
Jim Grosbachf0eee6e2011-09-07 23:39:14 +00001472 addMemImm8OffsetOperands(Inst, N);
Jim Grosbacha8307dd2011-09-07 20:58:57 +00001473 }
1474
1475 void addMemUImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1476 assert(N == 2 && "Invalid number of operands!");
1477 // If this is an immediate, it's a label reference.
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001478 if (Kind == k_Immediate) {
Jim Grosbacha8307dd2011-09-07 20:58:57 +00001479 addExpr(Inst, getImm());
1480 Inst.addOperand(MCOperand::CreateImm(0));
1481 return;
1482 }
1483
1484 // Otherwise, it's a normal memory reg+offset.
Jim Grosbache53c87b2011-10-11 15:59:20 +00001485 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1486 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbacha8307dd2011-09-07 20:58:57 +00001487 Inst.addOperand(MCOperand::CreateImm(Val));
1488 }
1489
Jim Grosbach7ce05792011-08-03 23:50:40 +00001490 void addMemImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1491 assert(N == 2 && "Invalid number of operands!");
Jim Grosbach09176e12011-08-08 20:59:31 +00001492 // If this is an immediate, it's a label reference.
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001493 if (Kind == k_Immediate) {
Jim Grosbach09176e12011-08-08 20:59:31 +00001494 addExpr(Inst, getImm());
1495 Inst.addOperand(MCOperand::CreateImm(0));
1496 return;
1497 }
1498
1499 // Otherwise, it's a normal memory reg+offset.
Jim Grosbache53c87b2011-10-11 15:59:20 +00001500 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1501 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbach7ce05792011-08-03 23:50:40 +00001502 Inst.addOperand(MCOperand::CreateImm(Val));
Bill Wendlingf4caf692010-12-14 03:36:38 +00001503 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +00001504
Jim Grosbach7f739be2011-09-19 22:21:13 +00001505 void addMemTBBOperands(MCInst &Inst, unsigned N) const {
1506 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001507 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1508 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
Jim Grosbach7f739be2011-09-19 22:21:13 +00001509 }
1510
1511 void addMemTBHOperands(MCInst &Inst, unsigned N) const {
1512 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001513 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1514 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
Jim Grosbach7f739be2011-09-19 22:21:13 +00001515 }
1516
Jim Grosbach7ce05792011-08-03 23:50:40 +00001517 void addMemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1518 assert(N == 3 && "Invalid number of operands!");
Jim Grosbach430052b2011-11-14 17:52:47 +00001519 unsigned Val =
1520 ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1521 Memory.ShiftImm, Memory.ShiftType);
Jim Grosbache53c87b2011-10-11 15:59:20 +00001522 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1523 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
Jim Grosbach7ce05792011-08-03 23:50:40 +00001524 Inst.addOperand(MCOperand::CreateImm(Val));
1525 }
1526
Jim Grosbachab899c12011-09-07 23:10:15 +00001527 void addT2MemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1528 assert(N == 3 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001529 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1530 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1531 Inst.addOperand(MCOperand::CreateImm(Memory.ShiftImm));
Jim Grosbachab899c12011-09-07 23:10:15 +00001532 }
1533
Jim Grosbach7ce05792011-08-03 23:50:40 +00001534 void addMemThumbRROperands(MCInst &Inst, unsigned N) const {
1535 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001536 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1537 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
Jim Grosbach7ce05792011-08-03 23:50:40 +00001538 }
1539
Jim Grosbach60f91a32011-08-19 17:55:24 +00001540 void addMemThumbRIs4Operands(MCInst &Inst, unsigned N) const {
1541 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001542 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1543 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbach60f91a32011-08-19 17:55:24 +00001544 Inst.addOperand(MCOperand::CreateImm(Val));
1545 }
1546
Jim Grosbach38466302011-08-19 18:55:51 +00001547 void addMemThumbRIs2Operands(MCInst &Inst, unsigned N) const {
1548 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001549 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 2) : 0;
1550 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbach38466302011-08-19 18:55:51 +00001551 Inst.addOperand(MCOperand::CreateImm(Val));
1552 }
1553
Jim Grosbach48ff5ff2011-08-19 18:49:59 +00001554 void addMemThumbRIs1Operands(MCInst &Inst, unsigned N) const {
1555 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001556 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue()) : 0;
1557 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbach48ff5ff2011-08-19 18:49:59 +00001558 Inst.addOperand(MCOperand::CreateImm(Val));
1559 }
1560
Jim Grosbachecd85892011-08-19 18:13:48 +00001561 void addMemThumbSPIOperands(MCInst &Inst, unsigned N) const {
1562 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001563 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1564 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbachecd85892011-08-19 18:13:48 +00001565 Inst.addOperand(MCOperand::CreateImm(Val));
1566 }
1567
Jim Grosbach7ce05792011-08-03 23:50:40 +00001568 void addPostIdxImm8Operands(MCInst &Inst, unsigned N) const {
1569 assert(N == 1 && "Invalid number of operands!");
1570 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1571 assert(CE && "non-constant post-idx-imm8 operand!");
1572 int Imm = CE->getValue();
1573 bool isAdd = Imm >= 0;
Owen Anderson63553c72011-08-29 17:17:09 +00001574 if (Imm == INT32_MIN) Imm = 0;
Jim Grosbach7ce05792011-08-03 23:50:40 +00001575 Imm = (Imm < 0 ? -Imm : Imm) | (int)isAdd << 8;
1576 Inst.addOperand(MCOperand::CreateImm(Imm));
1577 }
1578
Jim Grosbach2bd01182011-10-11 21:55:36 +00001579 void addPostIdxImm8s4Operands(MCInst &Inst, unsigned N) const {
1580 assert(N == 1 && "Invalid number of operands!");
1581 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1582 assert(CE && "non-constant post-idx-imm8s4 operand!");
1583 int Imm = CE->getValue();
1584 bool isAdd = Imm >= 0;
1585 if (Imm == INT32_MIN) Imm = 0;
1586 // Immediate is scaled by 4.
1587 Imm = ((Imm < 0 ? -Imm : Imm) / 4) | (int)isAdd << 8;
1588 Inst.addOperand(MCOperand::CreateImm(Imm));
1589 }
1590
Jim Grosbach7ce05792011-08-03 23:50:40 +00001591 void addPostIdxRegOperands(MCInst &Inst, unsigned N) const {
1592 assert(N == 2 && "Invalid number of operands!");
1593 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00001594 Inst.addOperand(MCOperand::CreateImm(PostIdxReg.isAdd));
1595 }
1596
1597 void addPostIdxRegShiftedOperands(MCInst &Inst, unsigned N) const {
1598 assert(N == 2 && "Invalid number of operands!");
1599 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1600 // The sign, shift type, and shift amount are encoded in a single operand
1601 // using the AM2 encoding helpers.
1602 ARM_AM::AddrOpc opc = PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub;
1603 unsigned Imm = ARM_AM::getAM2Opc(opc, PostIdxReg.ShiftImm,
1604 PostIdxReg.ShiftTy);
1605 Inst.addOperand(MCOperand::CreateImm(Imm));
Bill Wendlingef4a68b2010-11-30 07:44:32 +00001606 }
1607
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001608 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
1609 assert(N == 1 && "Invalid number of operands!");
1610 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
1611 }
1612
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001613 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
1614 assert(N == 1 && "Invalid number of operands!");
1615 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
1616 }
1617
Jim Grosbach6029b6d2011-11-29 23:51:09 +00001618 void addVecListOperands(MCInst &Inst, unsigned N) const {
Jim Grosbach862019c2011-10-18 23:02:30 +00001619 assert(N == 1 && "Invalid number of operands!");
1620 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1621 }
1622
Jim Grosbach7636bf62011-12-02 00:35:16 +00001623 void addVecListIndexedOperands(MCInst &Inst, unsigned N) const {
1624 assert(N == 2 && "Invalid number of operands!");
1625 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1626 Inst.addOperand(MCOperand::CreateImm(VectorList.LaneIndex));
1627 }
1628
Jim Grosbach460a9052011-10-07 23:56:00 +00001629 void addVectorIndex8Operands(MCInst &Inst, unsigned N) const {
1630 assert(N == 1 && "Invalid number of operands!");
1631 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1632 }
1633
1634 void addVectorIndex16Operands(MCInst &Inst, unsigned N) const {
1635 assert(N == 1 && "Invalid number of operands!");
1636 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1637 }
1638
1639 void addVectorIndex32Operands(MCInst &Inst, unsigned N) const {
1640 assert(N == 1 && "Invalid number of operands!");
1641 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1642 }
1643
Jim Grosbach0e387b22011-10-17 22:26:03 +00001644 void addNEONi8splatOperands(MCInst &Inst, unsigned N) const {
1645 assert(N == 1 && "Invalid number of operands!");
1646 // The immediate encodes the type of constant as well as the value.
1647 // Mask in that this is an i8 splat.
1648 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1649 Inst.addOperand(MCOperand::CreateImm(CE->getValue() | 0xe00));
1650 }
1651
Jim Grosbachea461102011-10-17 23:09:09 +00001652 void addNEONi16splatOperands(MCInst &Inst, unsigned N) const {
1653 assert(N == 1 && "Invalid number of operands!");
1654 // The immediate encodes the type of constant as well as the value.
1655 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1656 unsigned Value = CE->getValue();
1657 if (Value >= 256)
1658 Value = (Value >> 8) | 0xa00;
1659 else
1660 Value |= 0x800;
1661 Inst.addOperand(MCOperand::CreateImm(Value));
1662 }
1663
Jim Grosbach6248a542011-10-18 00:22:00 +00001664 void addNEONi32splatOperands(MCInst &Inst, unsigned N) const {
1665 assert(N == 1 && "Invalid number of operands!");
1666 // The immediate encodes the type of constant as well as the value.
1667 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1668 unsigned Value = CE->getValue();
1669 if (Value >= 256 && Value <= 0xff00)
1670 Value = (Value >> 8) | 0x200;
1671 else if (Value > 0xffff && Value <= 0xff0000)
1672 Value = (Value >> 16) | 0x400;
1673 else if (Value > 0xffffff)
1674 Value = (Value >> 24) | 0x600;
1675 Inst.addOperand(MCOperand::CreateImm(Value));
1676 }
1677
1678 void addNEONi32vmovOperands(MCInst &Inst, unsigned N) const {
1679 assert(N == 1 && "Invalid number of operands!");
1680 // The immediate encodes the type of constant as well as the value.
1681 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1682 unsigned Value = CE->getValue();
1683 if (Value >= 256 && Value <= 0xffff)
1684 Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
1685 else if (Value > 0xffff && Value <= 0xffffff)
1686 Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
1687 else if (Value > 0xffffff)
1688 Value = (Value >> 24) | 0x600;
1689 Inst.addOperand(MCOperand::CreateImm(Value));
1690 }
1691
Jim Grosbachf2f5bc62011-10-18 16:18:11 +00001692 void addNEONi64splatOperands(MCInst &Inst, unsigned N) const {
1693 assert(N == 1 && "Invalid number of operands!");
1694 // The immediate encodes the type of constant as well as the value.
1695 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1696 uint64_t Value = CE->getValue();
1697 unsigned Imm = 0;
1698 for (unsigned i = 0; i < 8; ++i, Value >>= 8) {
1699 Imm |= (Value & 1) << i;
1700 }
1701 Inst.addOperand(MCOperand::CreateImm(Imm | 0x1e00));
1702 }
1703
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001704 virtual void print(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +00001705
Jim Grosbach89df9962011-08-26 21:43:41 +00001706 static ARMOperand *CreateITMask(unsigned Mask, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001707 ARMOperand *Op = new ARMOperand(k_ITCondMask);
Jim Grosbach89df9962011-08-26 21:43:41 +00001708 Op->ITMask.Mask = Mask;
1709 Op->StartLoc = S;
1710 Op->EndLoc = S;
1711 return Op;
1712 }
1713
Chris Lattner3a697562010-10-28 17:20:03 +00001714 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001715 ARMOperand *Op = new ARMOperand(k_CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001716 Op->CC.Val = CC;
1717 Op->StartLoc = S;
1718 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +00001719 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001720 }
1721
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001722 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001723 ARMOperand *Op = new ARMOperand(k_CoprocNum);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001724 Op->Cop.Val = CopVal;
1725 Op->StartLoc = S;
1726 Op->EndLoc = S;
1727 return Op;
1728 }
1729
1730 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001731 ARMOperand *Op = new ARMOperand(k_CoprocReg);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001732 Op->Cop.Val = CopVal;
1733 Op->StartLoc = S;
1734 Op->EndLoc = S;
1735 return Op;
1736 }
1737
Jim Grosbach9b8f2a02011-10-12 17:34:41 +00001738 static ARMOperand *CreateCoprocOption(unsigned Val, SMLoc S, SMLoc E) {
1739 ARMOperand *Op = new ARMOperand(k_CoprocOption);
1740 Op->Cop.Val = Val;
1741 Op->StartLoc = S;
1742 Op->EndLoc = E;
1743 return Op;
1744 }
1745
Jim Grosbachd67641b2010-12-06 18:21:12 +00001746 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001747 ARMOperand *Op = new ARMOperand(k_CCOut);
Jim Grosbachd67641b2010-12-06 18:21:12 +00001748 Op->Reg.RegNum = RegNum;
1749 Op->StartLoc = S;
1750 Op->EndLoc = S;
1751 return Op;
1752 }
1753
Chris Lattner3a697562010-10-28 17:20:03 +00001754 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001755 ARMOperand *Op = new ARMOperand(k_Token);
Sean Callanan76264762010-04-02 22:27:05 +00001756 Op->Tok.Data = Str.data();
1757 Op->Tok.Length = Str.size();
1758 Op->StartLoc = S;
1759 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +00001760 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001761 }
1762
Bill Wendling50d0f582010-11-18 23:43:05 +00001763 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001764 ARMOperand *Op = new ARMOperand(k_Register);
Sean Callanan76264762010-04-02 22:27:05 +00001765 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +00001766 Op->StartLoc = S;
1767 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +00001768 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001769 }
1770
Jim Grosbache8606dc2011-07-13 17:50:29 +00001771 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
1772 unsigned SrcReg,
1773 unsigned ShiftReg,
1774 unsigned ShiftImm,
1775 SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001776 ARMOperand *Op = new ARMOperand(k_ShiftedRegister);
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001777 Op->RegShiftedReg.ShiftTy = ShTy;
1778 Op->RegShiftedReg.SrcReg = SrcReg;
1779 Op->RegShiftedReg.ShiftReg = ShiftReg;
1780 Op->RegShiftedReg.ShiftImm = ShiftImm;
Jim Grosbache8606dc2011-07-13 17:50:29 +00001781 Op->StartLoc = S;
1782 Op->EndLoc = E;
1783 return Op;
1784 }
1785
Owen Anderson92a20222011-07-21 18:54:16 +00001786 static ARMOperand *CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy,
1787 unsigned SrcReg,
1788 unsigned ShiftImm,
1789 SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001790 ARMOperand *Op = new ARMOperand(k_ShiftedImmediate);
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001791 Op->RegShiftedImm.ShiftTy = ShTy;
1792 Op->RegShiftedImm.SrcReg = SrcReg;
1793 Op->RegShiftedImm.ShiftImm = ShiftImm;
Owen Anderson92a20222011-07-21 18:54:16 +00001794 Op->StartLoc = S;
1795 Op->EndLoc = E;
1796 return Op;
1797 }
1798
Jim Grosbach580f4a92011-07-25 22:20:28 +00001799 static ARMOperand *CreateShifterImm(bool isASR, unsigned Imm,
Owen Anderson00828302011-03-18 22:50:18 +00001800 SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001801 ARMOperand *Op = new ARMOperand(k_ShifterImmediate);
Jim Grosbach580f4a92011-07-25 22:20:28 +00001802 Op->ShifterImm.isASR = isASR;
1803 Op->ShifterImm.Imm = Imm;
Owen Anderson00828302011-03-18 22:50:18 +00001804 Op->StartLoc = S;
1805 Op->EndLoc = E;
1806 return Op;
1807 }
1808
Jim Grosbach7e1547e2011-07-27 20:15:40 +00001809 static ARMOperand *CreateRotImm(unsigned Imm, SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001810 ARMOperand *Op = new ARMOperand(k_RotateImmediate);
Jim Grosbach7e1547e2011-07-27 20:15:40 +00001811 Op->RotImm.Imm = Imm;
1812 Op->StartLoc = S;
1813 Op->EndLoc = E;
1814 return Op;
1815 }
1816
Jim Grosbach293a2ee2011-07-28 21:34:26 +00001817 static ARMOperand *CreateBitfield(unsigned LSB, unsigned Width,
1818 SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001819 ARMOperand *Op = new ARMOperand(k_BitfieldDescriptor);
Jim Grosbach293a2ee2011-07-28 21:34:26 +00001820 Op->Bitfield.LSB = LSB;
1821 Op->Bitfield.Width = Width;
1822 Op->StartLoc = S;
1823 Op->EndLoc = E;
1824 return Op;
1825 }
1826
Bill Wendling7729e062010-11-09 22:44:22 +00001827 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +00001828 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +00001829 SMLoc StartLoc, SMLoc EndLoc) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001830 KindTy Kind = k_RegisterList;
Bill Wendling0f630752010-11-17 04:32:08 +00001831
Jim Grosbachd300b942011-09-13 22:56:44 +00001832 if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Regs.front().first))
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001833 Kind = k_DPRRegisterList;
Jim Grosbachd300b942011-09-13 22:56:44 +00001834 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].
Evan Cheng275944a2011-07-25 21:32:49 +00001835 contains(Regs.front().first))
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001836 Kind = k_SPRRegisterList;
Bill Wendling0f630752010-11-17 04:32:08 +00001837
1838 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +00001839 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +00001840 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +00001841 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +00001842 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +00001843 Op->StartLoc = StartLoc;
1844 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +00001845 return Op;
1846 }
1847
Jim Grosbach862019c2011-10-18 23:02:30 +00001848 static ARMOperand *CreateVectorList(unsigned RegNum, unsigned Count,
1849 SMLoc S, SMLoc E) {
1850 ARMOperand *Op = new ARMOperand(k_VectorList);
1851 Op->VectorList.RegNum = RegNum;
1852 Op->VectorList.Count = Count;
1853 Op->StartLoc = S;
1854 Op->EndLoc = E;
1855 return Op;
1856 }
1857
Jim Grosbach98b05a52011-11-30 01:09:44 +00001858 static ARMOperand *CreateVectorListAllLanes(unsigned RegNum, unsigned Count,
1859 SMLoc S, SMLoc E) {
1860 ARMOperand *Op = new ARMOperand(k_VectorListAllLanes);
1861 Op->VectorList.RegNum = RegNum;
1862 Op->VectorList.Count = Count;
1863 Op->StartLoc = S;
1864 Op->EndLoc = E;
1865 return Op;
1866 }
1867
Jim Grosbach7636bf62011-12-02 00:35:16 +00001868 static ARMOperand *CreateVectorListIndexed(unsigned RegNum, unsigned Count,
1869 unsigned Index, SMLoc S, SMLoc E) {
1870 ARMOperand *Op = new ARMOperand(k_VectorListIndexed);
1871 Op->VectorList.RegNum = RegNum;
1872 Op->VectorList.Count = Count;
1873 Op->VectorList.LaneIndex = Index;
1874 Op->StartLoc = S;
1875 Op->EndLoc = E;
1876 return Op;
1877 }
1878
Jim Grosbach460a9052011-10-07 23:56:00 +00001879 static ARMOperand *CreateVectorIndex(unsigned Idx, SMLoc S, SMLoc E,
1880 MCContext &Ctx) {
1881 ARMOperand *Op = new ARMOperand(k_VectorIndex);
1882 Op->VectorIndex.Val = Idx;
1883 Op->StartLoc = S;
1884 Op->EndLoc = E;
1885 return Op;
1886 }
1887
Chris Lattner3a697562010-10-28 17:20:03 +00001888 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001889 ARMOperand *Op = new ARMOperand(k_Immediate);
Sean Callanan76264762010-04-02 22:27:05 +00001890 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +00001891 Op->StartLoc = S;
1892 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +00001893 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +00001894 }
1895
Jim Grosbach9d390362011-10-03 23:38:36 +00001896 static ARMOperand *CreateFPImm(unsigned Val, SMLoc S, MCContext &Ctx) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001897 ARMOperand *Op = new ARMOperand(k_FPImmediate);
Jim Grosbach9d390362011-10-03 23:38:36 +00001898 Op->FPImm.Val = Val;
1899 Op->StartLoc = S;
1900 Op->EndLoc = S;
1901 return Op;
1902 }
1903
Jim Grosbach7ce05792011-08-03 23:50:40 +00001904 static ARMOperand *CreateMem(unsigned BaseRegNum,
1905 const MCConstantExpr *OffsetImm,
1906 unsigned OffsetRegNum,
1907 ARM_AM::ShiftOpc ShiftType,
Jim Grosbach0d6fac32011-08-05 22:03:36 +00001908 unsigned ShiftImm,
Jim Grosbach57dcb852011-10-11 17:29:55 +00001909 unsigned Alignment,
Jim Grosbach7ce05792011-08-03 23:50:40 +00001910 bool isNegative,
Chris Lattner3a697562010-10-28 17:20:03 +00001911 SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001912 ARMOperand *Op = new ARMOperand(k_Memory);
Jim Grosbache53c87b2011-10-11 15:59:20 +00001913 Op->Memory.BaseRegNum = BaseRegNum;
1914 Op->Memory.OffsetImm = OffsetImm;
1915 Op->Memory.OffsetRegNum = OffsetRegNum;
1916 Op->Memory.ShiftType = ShiftType;
1917 Op->Memory.ShiftImm = ShiftImm;
Jim Grosbach57dcb852011-10-11 17:29:55 +00001918 Op->Memory.Alignment = Alignment;
Jim Grosbache53c87b2011-10-11 15:59:20 +00001919 Op->Memory.isNegative = isNegative;
Jim Grosbach7ce05792011-08-03 23:50:40 +00001920 Op->StartLoc = S;
1921 Op->EndLoc = E;
1922 return Op;
1923 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001924
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00001925 static ARMOperand *CreatePostIdxReg(unsigned RegNum, bool isAdd,
1926 ARM_AM::ShiftOpc ShiftTy,
1927 unsigned ShiftImm,
Jim Grosbach7ce05792011-08-03 23:50:40 +00001928 SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001929 ARMOperand *Op = new ARMOperand(k_PostIndexRegister);
Jim Grosbach7ce05792011-08-03 23:50:40 +00001930 Op->PostIdxReg.RegNum = RegNum;
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00001931 Op->PostIdxReg.isAdd = isAdd;
1932 Op->PostIdxReg.ShiftTy = ShiftTy;
1933 Op->PostIdxReg.ShiftImm = ShiftImm;
Sean Callanan76264762010-04-02 22:27:05 +00001934 Op->StartLoc = S;
1935 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +00001936 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001937 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001938
1939 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001940 ARMOperand *Op = new ARMOperand(k_MemBarrierOpt);
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001941 Op->MBOpt.Val = Opt;
1942 Op->StartLoc = S;
1943 Op->EndLoc = S;
1944 return Op;
1945 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001946
1947 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001948 ARMOperand *Op = new ARMOperand(k_ProcIFlags);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001949 Op->IFlags.Val = IFlags;
1950 Op->StartLoc = S;
1951 Op->EndLoc = S;
1952 return Op;
1953 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001954
1955 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001956 ARMOperand *Op = new ARMOperand(k_MSRMask);
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001957 Op->MMask.Val = MMask;
1958 Op->StartLoc = S;
1959 Op->EndLoc = S;
1960 return Op;
1961 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001962};
1963
1964} // end anonymous namespace.
1965
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001966void ARMOperand::print(raw_ostream &OS) const {
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001967 switch (Kind) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001968 case k_FPImmediate:
Jim Grosbach9d390362011-10-03 23:38:36 +00001969 OS << "<fpimm " << getFPImm() << "(" << ARM_AM::getFPImmFloat(getFPImm())
1970 << ") >";
1971 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001972 case k_CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +00001973 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001974 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001975 case k_CCOut:
Jim Grosbachd67641b2010-12-06 18:21:12 +00001976 OS << "<ccout " << getReg() << ">";
1977 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001978 case k_ITCondMask: {
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00001979 static const char *MaskStr[] = {
1980 "()", "(t)", "(e)", "(tt)", "(et)", "(te)", "(ee)", "(ttt)", "(ett)",
1981 "(tet)", "(eet)", "(tte)", "(ete)", "(tee)", "(eee)"
1982 };
Jim Grosbach89df9962011-08-26 21:43:41 +00001983 assert((ITMask.Mask & 0xf) == ITMask.Mask);
1984 OS << "<it-mask " << MaskStr[ITMask.Mask] << ">";
1985 break;
1986 }
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001987 case k_CoprocNum:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001988 OS << "<coprocessor number: " << getCoproc() << ">";
1989 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001990 case k_CoprocReg:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001991 OS << "<coprocessor register: " << getCoproc() << ">";
1992 break;
Jim Grosbach9b8f2a02011-10-12 17:34:41 +00001993 case k_CoprocOption:
1994 OS << "<coprocessor option: " << CoprocOption.Val << ">";
1995 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001996 case k_MSRMask:
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001997 OS << "<mask: " << getMSRMask() << ">";
1998 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001999 case k_Immediate:
Daniel Dunbarfa315de2010-08-11 06:37:12 +00002000 getImm()->print(OS);
2001 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002002 case k_MemBarrierOpt:
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002003 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
2004 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002005 case k_Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +00002006 OS << "<memory "
Jim Grosbache53c87b2011-10-11 15:59:20 +00002007 << " base:" << Memory.BaseRegNum;
Daniel Dunbar6ec56202011-01-18 05:55:21 +00002008 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00002009 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002010 case k_PostIndexRegister:
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00002011 OS << "post-idx register " << (PostIdxReg.isAdd ? "" : "-")
2012 << PostIdxReg.RegNum;
2013 if (PostIdxReg.ShiftTy != ARM_AM::no_shift)
2014 OS << ARM_AM::getShiftOpcStr(PostIdxReg.ShiftTy) << " "
2015 << PostIdxReg.ShiftImm;
2016 OS << ">";
Jim Grosbach7ce05792011-08-03 23:50:40 +00002017 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002018 case k_ProcIFlags: {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002019 OS << "<ARM_PROC::";
2020 unsigned IFlags = getProcIFlags();
2021 for (int i=2; i >= 0; --i)
2022 if (IFlags & (1 << i))
2023 OS << ARM_PROC::IFlagsToString(1 << i);
2024 OS << ">";
2025 break;
2026 }
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002027 case k_Register:
Bill Wendling50d0f582010-11-18 23:43:05 +00002028 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00002029 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002030 case k_ShifterImmediate:
Jim Grosbach580f4a92011-07-25 22:20:28 +00002031 OS << "<shift " << (ShifterImm.isASR ? "asr" : "lsl")
2032 << " #" << ShifterImm.Imm << ">";
Jim Grosbache8606dc2011-07-13 17:50:29 +00002033 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002034 case k_ShiftedRegister:
Owen Anderson92a20222011-07-21 18:54:16 +00002035 OS << "<so_reg_reg "
Jim Grosbachefed3d12011-11-16 21:46:50 +00002036 << RegShiftedReg.SrcReg << " "
2037 << ARM_AM::getShiftOpcStr(RegShiftedReg.ShiftTy)
2038 << " " << RegShiftedReg.ShiftReg << ">";
Owen Anderson00828302011-03-18 22:50:18 +00002039 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002040 case k_ShiftedImmediate:
Owen Anderson92a20222011-07-21 18:54:16 +00002041 OS << "<so_reg_imm "
Jim Grosbachefed3d12011-11-16 21:46:50 +00002042 << RegShiftedImm.SrcReg << " "
2043 << ARM_AM::getShiftOpcStr(RegShiftedImm.ShiftTy)
2044 << " #" << RegShiftedImm.ShiftImm << ">";
Owen Anderson92a20222011-07-21 18:54:16 +00002045 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002046 case k_RotateImmediate:
Jim Grosbach7e1547e2011-07-27 20:15:40 +00002047 OS << "<ror " << " #" << (RotImm.Imm * 8) << ">";
2048 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002049 case k_BitfieldDescriptor:
Jim Grosbach293a2ee2011-07-28 21:34:26 +00002050 OS << "<bitfield " << "lsb: " << Bitfield.LSB
2051 << ", width: " << Bitfield.Width << ">";
2052 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002053 case k_RegisterList:
2054 case k_DPRRegisterList:
2055 case k_SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +00002056 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00002057
Bill Wendling5fa22a12010-11-09 23:28:44 +00002058 const SmallVectorImpl<unsigned> &RegList = getRegList();
2059 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +00002060 I = RegList.begin(), E = RegList.end(); I != E; ) {
2061 OS << *I;
2062 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00002063 }
2064
2065 OS << ">";
2066 break;
2067 }
Jim Grosbach862019c2011-10-18 23:02:30 +00002068 case k_VectorList:
2069 OS << "<vector_list " << VectorList.Count << " * "
2070 << VectorList.RegNum << ">";
2071 break;
Jim Grosbach98b05a52011-11-30 01:09:44 +00002072 case k_VectorListAllLanes:
2073 OS << "<vector_list(all lanes) " << VectorList.Count << " * "
2074 << VectorList.RegNum << ">";
2075 break;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002076 case k_VectorListIndexed:
2077 OS << "<vector_list(lane " << VectorList.LaneIndex << ") "
2078 << VectorList.Count << " * " << VectorList.RegNum << ">";
2079 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002080 case k_Token:
Daniel Dunbarfa315de2010-08-11 06:37:12 +00002081 OS << "'" << getToken() << "'";
2082 break;
Jim Grosbach460a9052011-10-07 23:56:00 +00002083 case k_VectorIndex:
2084 OS << "<vectorindex " << getVectorIndex() << ">";
2085 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +00002086 }
2087}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002088
2089/// @name Auto-generated Match Functions
2090/// {
2091
2092static unsigned MatchRegisterName(StringRef Name);
2093
2094/// }
2095
Bob Wilson69df7232011-02-03 21:46:10 +00002096bool ARMAsmParser::ParseRegister(unsigned &RegNo,
2097 SMLoc &StartLoc, SMLoc &EndLoc) {
Jim Grosbach1355cf12011-07-26 17:10:22 +00002098 RegNo = tryParseRegister();
Roman Divackybf755322011-01-27 17:14:22 +00002099
2100 return (RegNo == (unsigned)-1);
2101}
2102
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002103/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +00002104/// and if it is a register name the token is eaten and the register number is
2105/// returned. Otherwise return -1.
2106///
Jim Grosbach1355cf12011-07-26 17:10:22 +00002107int ARMAsmParser::tryParseRegister() {
Chris Lattnere5658fa2010-10-30 04:09:10 +00002108 const AsmToken &Tok = Parser.getTok();
Jim Grosbach7ce05792011-08-03 23:50:40 +00002109 if (Tok.isNot(AsmToken::Identifier)) return -1;
Jim Grosbachd4462a52010-11-01 16:44:21 +00002110
Chris Lattnere5658fa2010-10-30 04:09:10 +00002111 // FIXME: Validate register for the current architecture; we have to do
2112 // validation later, so maybe there is no need for this here.
Benjamin Kramer59085362011-11-06 20:37:06 +00002113 std::string lowerCase = Tok.getString().lower();
Owen Anderson0c9f2502011-01-13 22:50:36 +00002114 unsigned RegNum = MatchRegisterName(lowerCase);
2115 if (!RegNum) {
2116 RegNum = StringSwitch<unsigned>(lowerCase)
2117 .Case("r13", ARM::SP)
2118 .Case("r14", ARM::LR)
2119 .Case("r15", ARM::PC)
2120 .Case("ip", ARM::R12)
2121 .Default(0);
2122 }
2123 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +00002124
Chris Lattnere5658fa2010-10-30 04:09:10 +00002125 Parser.Lex(); // Eat identifier token.
Jim Grosbach460a9052011-10-07 23:56:00 +00002126
Chris Lattnere5658fa2010-10-30 04:09:10 +00002127 return RegNum;
2128}
Jim Grosbachd4462a52010-11-01 16:44:21 +00002129
Jim Grosbach19906722011-07-13 18:49:30 +00002130// Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
2131// If a recoverable error occurs, return 1. If an irrecoverable error
2132// occurs, return -1. An irrecoverable error is one where tokens have been
2133// consumed in the process of trying to parse the shifter (i.e., when it is
2134// indeed a shifter operand, but malformed).
Jim Grosbach0d87ec22011-07-26 20:41:24 +00002135int ARMAsmParser::tryParseShiftRegister(
Owen Anderson00828302011-03-18 22:50:18 +00002136 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2137 SMLoc S = Parser.getTok().getLoc();
2138 const AsmToken &Tok = Parser.getTok();
2139 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2140
Benjamin Kramer59085362011-11-06 20:37:06 +00002141 std::string lowerCase = Tok.getString().lower();
Owen Anderson00828302011-03-18 22:50:18 +00002142 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
2143 .Case("lsl", ARM_AM::lsl)
2144 .Case("lsr", ARM_AM::lsr)
2145 .Case("asr", ARM_AM::asr)
2146 .Case("ror", ARM_AM::ror)
2147 .Case("rrx", ARM_AM::rrx)
2148 .Default(ARM_AM::no_shift);
2149
2150 if (ShiftTy == ARM_AM::no_shift)
Jim Grosbach19906722011-07-13 18:49:30 +00002151 return 1;
Owen Anderson00828302011-03-18 22:50:18 +00002152
Jim Grosbache8606dc2011-07-13 17:50:29 +00002153 Parser.Lex(); // Eat the operator.
Owen Anderson00828302011-03-18 22:50:18 +00002154
Jim Grosbache8606dc2011-07-13 17:50:29 +00002155 // The source register for the shift has already been added to the
2156 // operand list, so we need to pop it off and combine it into the shifted
2157 // register operand instead.
Benjamin Kramereac07962011-07-14 18:41:22 +00002158 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
Jim Grosbache8606dc2011-07-13 17:50:29 +00002159 if (!PrevOp->isReg())
2160 return Error(PrevOp->getStartLoc(), "shift must be of a register");
2161 int SrcReg = PrevOp->getReg();
2162 int64_t Imm = 0;
2163 int ShiftReg = 0;
2164 if (ShiftTy == ARM_AM::rrx) {
2165 // RRX Doesn't have an explicit shift amount. The encoder expects
2166 // the shift register to be the same as the source register. Seems odd,
2167 // but OK.
2168 ShiftReg = SrcReg;
2169 } else {
2170 // Figure out if this is shifted by a constant or a register (for non-RRX).
2171 if (Parser.getTok().is(AsmToken::Hash)) {
2172 Parser.Lex(); // Eat hash.
2173 SMLoc ImmLoc = Parser.getTok().getLoc();
2174 const MCExpr *ShiftExpr = 0;
Jim Grosbach19906722011-07-13 18:49:30 +00002175 if (getParser().ParseExpression(ShiftExpr)) {
2176 Error(ImmLoc, "invalid immediate shift value");
2177 return -1;
2178 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00002179 // The expression must be evaluatable as an immediate.
2180 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
Jim Grosbach19906722011-07-13 18:49:30 +00002181 if (!CE) {
2182 Error(ImmLoc, "invalid immediate shift value");
2183 return -1;
2184 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00002185 // Range check the immediate.
2186 // lsl, ror: 0 <= imm <= 31
2187 // lsr, asr: 0 <= imm <= 32
2188 Imm = CE->getValue();
2189 if (Imm < 0 ||
2190 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
2191 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
Jim Grosbach19906722011-07-13 18:49:30 +00002192 Error(ImmLoc, "immediate shift value out of range");
2193 return -1;
Jim Grosbache8606dc2011-07-13 17:50:29 +00002194 }
2195 } else if (Parser.getTok().is(AsmToken::Identifier)) {
Jim Grosbach1355cf12011-07-26 17:10:22 +00002196 ShiftReg = tryParseRegister();
Jim Grosbache8606dc2011-07-13 17:50:29 +00002197 SMLoc L = Parser.getTok().getLoc();
Jim Grosbach19906722011-07-13 18:49:30 +00002198 if (ShiftReg == -1) {
2199 Error (L, "expected immediate or register in shift operand");
2200 return -1;
2201 }
2202 } else {
2203 Error (Parser.getTok().getLoc(),
Jim Grosbache8606dc2011-07-13 17:50:29 +00002204 "expected immediate or register in shift operand");
Jim Grosbach19906722011-07-13 18:49:30 +00002205 return -1;
2206 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00002207 }
2208
Owen Anderson92a20222011-07-21 18:54:16 +00002209 if (ShiftReg && ShiftTy != ARM_AM::rrx)
2210 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
Jim Grosbachaf6981f2011-07-25 20:49:51 +00002211 ShiftReg, Imm,
Owen Anderson00828302011-03-18 22:50:18 +00002212 S, Parser.getTok().getLoc()));
Owen Anderson92a20222011-07-21 18:54:16 +00002213 else
2214 Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
2215 S, Parser.getTok().getLoc()));
Owen Anderson00828302011-03-18 22:50:18 +00002216
Jim Grosbach19906722011-07-13 18:49:30 +00002217 return 0;
Owen Anderson00828302011-03-18 22:50:18 +00002218}
2219
2220
Bill Wendling50d0f582010-11-18 23:43:05 +00002221/// Try to parse a register name. The token must be an Identifier when called.
2222/// If it's a register, an AsmOperand is created. Another AsmOperand is created
2223/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +00002224///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002225/// TODO this is likely to change to allow different register types and or to
2226/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +00002227bool ARMAsmParser::
Jim Grosbach1355cf12011-07-26 17:10:22 +00002228tryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00002229 SMLoc S = Parser.getTok().getLoc();
Jim Grosbach1355cf12011-07-26 17:10:22 +00002230 int RegNo = tryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +00002231 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +00002232 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +00002233
Bill Wendling50d0f582010-11-18 23:43:05 +00002234 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002235
Chris Lattnere5658fa2010-10-30 04:09:10 +00002236 const AsmToken &ExclaimTok = Parser.getTok();
2237 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00002238 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
2239 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +00002240 Parser.Lex(); // Eat exclaim token
Jim Grosbach460a9052011-10-07 23:56:00 +00002241 return false;
2242 }
2243
2244 // Also check for an index operand. This is only legal for vector registers,
2245 // but that'll get caught OK in operand matching, so we don't need to
2246 // explicitly filter everything else out here.
2247 if (Parser.getTok().is(AsmToken::LBrac)) {
2248 SMLoc SIdx = Parser.getTok().getLoc();
2249 Parser.Lex(); // Eat left bracket token.
2250
2251 const MCExpr *ImmVal;
Jim Grosbach460a9052011-10-07 23:56:00 +00002252 if (getParser().ParseExpression(ImmVal))
2253 return MatchOperand_ParseFail;
2254 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
2255 if (!MCE) {
2256 TokError("immediate value expected for vector index");
2257 return MatchOperand_ParseFail;
2258 }
2259
2260 SMLoc E = Parser.getTok().getLoc();
2261 if (Parser.getTok().isNot(AsmToken::RBrac)) {
2262 Error(E, "']' expected");
2263 return MatchOperand_ParseFail;
2264 }
2265
2266 Parser.Lex(); // Eat right bracket token.
2267
2268 Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
2269 SIdx, E,
2270 getContext()));
Kevin Enderby99e6d4e2009-10-07 18:01:35 +00002271 }
2272
Bill Wendling50d0f582010-11-18 23:43:05 +00002273 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002274}
2275
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002276/// MatchCoprocessorOperandName - Try to parse an coprocessor related
2277/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
2278/// "c5", ...
2279static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002280 // Use the same layout as the tablegen'erated register name matcher. Ugly,
2281 // but efficient.
2282 switch (Name.size()) {
2283 default: break;
2284 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002285 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002286 return -1;
2287 switch (Name[1]) {
2288 default: return -1;
2289 case '0': return 0;
2290 case '1': return 1;
2291 case '2': return 2;
2292 case '3': return 3;
2293 case '4': return 4;
2294 case '5': return 5;
2295 case '6': return 6;
2296 case '7': return 7;
2297 case '8': return 8;
2298 case '9': return 9;
2299 }
2300 break;
2301 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002302 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002303 return -1;
2304 switch (Name[2]) {
2305 default: return -1;
2306 case '0': return 10;
2307 case '1': return 11;
2308 case '2': return 12;
2309 case '3': return 13;
2310 case '4': return 14;
2311 case '5': return 15;
2312 }
2313 break;
2314 }
2315
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002316 return -1;
2317}
2318
Jim Grosbach89df9962011-08-26 21:43:41 +00002319/// parseITCondCode - Try to parse a condition code for an IT instruction.
2320ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2321parseITCondCode(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2322 SMLoc S = Parser.getTok().getLoc();
2323 const AsmToken &Tok = Parser.getTok();
2324 if (!Tok.is(AsmToken::Identifier))
2325 return MatchOperand_NoMatch;
2326 unsigned CC = StringSwitch<unsigned>(Tok.getString())
2327 .Case("eq", ARMCC::EQ)
2328 .Case("ne", ARMCC::NE)
2329 .Case("hs", ARMCC::HS)
2330 .Case("cs", ARMCC::HS)
2331 .Case("lo", ARMCC::LO)
2332 .Case("cc", ARMCC::LO)
2333 .Case("mi", ARMCC::MI)
2334 .Case("pl", ARMCC::PL)
2335 .Case("vs", ARMCC::VS)
2336 .Case("vc", ARMCC::VC)
2337 .Case("hi", ARMCC::HI)
2338 .Case("ls", ARMCC::LS)
2339 .Case("ge", ARMCC::GE)
2340 .Case("lt", ARMCC::LT)
2341 .Case("gt", ARMCC::GT)
2342 .Case("le", ARMCC::LE)
2343 .Case("al", ARMCC::AL)
2344 .Default(~0U);
2345 if (CC == ~0U)
2346 return MatchOperand_NoMatch;
2347 Parser.Lex(); // Eat the token.
2348
2349 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), S));
2350
2351 return MatchOperand_Success;
2352}
2353
Jim Grosbach43904292011-07-25 20:14:50 +00002354/// parseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002355/// token must be an Identifier when called, and if it is a coprocessor
2356/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00002357ARMAsmParser::OperandMatchResultTy ARMAsmParser::
Jim Grosbach43904292011-07-25 20:14:50 +00002358parseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002359 SMLoc S = Parser.getTok().getLoc();
2360 const AsmToken &Tok = Parser.getTok();
Jim Grosbachc66e7af2011-10-12 20:54:17 +00002361 if (Tok.isNot(AsmToken::Identifier))
2362 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002363
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002364 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002365 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00002366 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002367
2368 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002369 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00002370 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002371}
2372
Jim Grosbach43904292011-07-25 20:14:50 +00002373/// parseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002374/// token must be an Identifier when called, and if it is a coprocessor
2375/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00002376ARMAsmParser::OperandMatchResultTy ARMAsmParser::
Jim Grosbach43904292011-07-25 20:14:50 +00002377parseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002378 SMLoc S = Parser.getTok().getLoc();
2379 const AsmToken &Tok = Parser.getTok();
Jim Grosbachc66e7af2011-10-12 20:54:17 +00002380 if (Tok.isNot(AsmToken::Identifier))
2381 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002382
2383 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
2384 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00002385 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002386
2387 Parser.Lex(); // Eat identifier token.
2388 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00002389 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002390}
2391
Jim Grosbach9b8f2a02011-10-12 17:34:41 +00002392/// parseCoprocOptionOperand - Try to parse an coprocessor option operand.
2393/// coproc_option : '{' imm0_255 '}'
2394ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2395parseCoprocOptionOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2396 SMLoc S = Parser.getTok().getLoc();
2397
2398 // If this isn't a '{', this isn't a coprocessor immediate operand.
2399 if (Parser.getTok().isNot(AsmToken::LCurly))
2400 return MatchOperand_NoMatch;
2401 Parser.Lex(); // Eat the '{'
2402
2403 const MCExpr *Expr;
2404 SMLoc Loc = Parser.getTok().getLoc();
2405 if (getParser().ParseExpression(Expr)) {
2406 Error(Loc, "illegal expression");
2407 return MatchOperand_ParseFail;
2408 }
2409 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
2410 if (!CE || CE->getValue() < 0 || CE->getValue() > 255) {
2411 Error(Loc, "coprocessor option must be an immediate in range [0, 255]");
2412 return MatchOperand_ParseFail;
2413 }
2414 int Val = CE->getValue();
2415
2416 // Check for and consume the closing '}'
2417 if (Parser.getTok().isNot(AsmToken::RCurly))
2418 return MatchOperand_ParseFail;
2419 SMLoc E = Parser.getTok().getLoc();
2420 Parser.Lex(); // Eat the '}'
2421
2422 Operands.push_back(ARMOperand::CreateCoprocOption(Val, S, E));
2423 return MatchOperand_Success;
2424}
2425
Jim Grosbachd0588e22011-09-14 18:08:35 +00002426// For register list parsing, we need to map from raw GPR register numbering
2427// to the enumeration values. The enumeration values aren't sorted by
2428// register number due to our using "sp", "lr" and "pc" as canonical names.
2429static unsigned getNextRegister(unsigned Reg) {
2430 // If this is a GPR, we need to do it manually, otherwise we can rely
2431 // on the sort ordering of the enumeration since the other reg-classes
2432 // are sane.
2433 if (!ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2434 return Reg + 1;
2435 switch(Reg) {
2436 default: assert(0 && "Invalid GPR number!");
2437 case ARM::R0: return ARM::R1; case ARM::R1: return ARM::R2;
2438 case ARM::R2: return ARM::R3; case ARM::R3: return ARM::R4;
2439 case ARM::R4: return ARM::R5; case ARM::R5: return ARM::R6;
2440 case ARM::R6: return ARM::R7; case ARM::R7: return ARM::R8;
2441 case ARM::R8: return ARM::R9; case ARM::R9: return ARM::R10;
2442 case ARM::R10: return ARM::R11; case ARM::R11: return ARM::R12;
2443 case ARM::R12: return ARM::SP; case ARM::SP: return ARM::LR;
2444 case ARM::LR: return ARM::PC; case ARM::PC: return ARM::R0;
2445 }
2446}
2447
Jim Grosbachce485e72011-11-11 21:27:40 +00002448// Return the low-subreg of a given Q register.
2449static unsigned getDRegFromQReg(unsigned QReg) {
2450 switch (QReg) {
2451 default: llvm_unreachable("expected a Q register!");
2452 case ARM::Q0: return ARM::D0;
2453 case ARM::Q1: return ARM::D2;
2454 case ARM::Q2: return ARM::D4;
2455 case ARM::Q3: return ARM::D6;
2456 case ARM::Q4: return ARM::D8;
2457 case ARM::Q5: return ARM::D10;
2458 case ARM::Q6: return ARM::D12;
2459 case ARM::Q7: return ARM::D14;
2460 case ARM::Q8: return ARM::D16;
Jim Grosbach25e0a872011-11-15 21:01:30 +00002461 case ARM::Q9: return ARM::D18;
Jim Grosbachce485e72011-11-11 21:27:40 +00002462 case ARM::Q10: return ARM::D20;
2463 case ARM::Q11: return ARM::D22;
2464 case ARM::Q12: return ARM::D24;
2465 case ARM::Q13: return ARM::D26;
2466 case ARM::Q14: return ARM::D28;
2467 case ARM::Q15: return ARM::D30;
2468 }
2469}
2470
Jim Grosbachd0588e22011-09-14 18:08:35 +00002471/// Parse a register list.
Bill Wendling50d0f582010-11-18 23:43:05 +00002472bool ARMAsmParser::
Jim Grosbach1355cf12011-07-26 17:10:22 +00002473parseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00002474 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00002475 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00002476 SMLoc S = Parser.getTok().getLoc();
Jim Grosbachd0588e22011-09-14 18:08:35 +00002477 Parser.Lex(); // Eat '{' token.
2478 SMLoc RegLoc = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00002479
Jim Grosbachd0588e22011-09-14 18:08:35 +00002480 // Check the first register in the list to see what register class
2481 // this is a list of.
2482 int Reg = tryParseRegister();
2483 if (Reg == -1)
2484 return Error(RegLoc, "register expected");
2485
Jim Grosbachce485e72011-11-11 21:27:40 +00002486 // The reglist instructions have at most 16 registers, so reserve
2487 // space for that many.
2488 SmallVector<std::pair<unsigned, SMLoc>, 16> Registers;
2489
2490 // Allow Q regs and just interpret them as the two D sub-registers.
2491 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2492 Reg = getDRegFromQReg(Reg);
2493 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2494 ++Reg;
2495 }
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00002496 const MCRegisterClass *RC;
Jim Grosbachd0588e22011-09-14 18:08:35 +00002497 if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2498 RC = &ARMMCRegisterClasses[ARM::GPRRegClassID];
2499 else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg))
2500 RC = &ARMMCRegisterClasses[ARM::DPRRegClassID];
2501 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg))
2502 RC = &ARMMCRegisterClasses[ARM::SPRRegClassID];
2503 else
2504 return Error(RegLoc, "invalid register in register list");
2505
Jim Grosbachce485e72011-11-11 21:27:40 +00002506 // Store the register.
Jim Grosbachd0588e22011-09-14 18:08:35 +00002507 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
Kevin Enderbyd7894f12009-10-09 21:12:28 +00002508
Jim Grosbachd0588e22011-09-14 18:08:35 +00002509 // This starts immediately after the first register token in the list,
2510 // so we can see either a comma or a minus (range separator) as a legal
2511 // next token.
2512 while (Parser.getTok().is(AsmToken::Comma) ||
2513 Parser.getTok().is(AsmToken::Minus)) {
2514 if (Parser.getTok().is(AsmToken::Minus)) {
Jim Grosbache43862b2011-11-15 23:19:15 +00002515 Parser.Lex(); // Eat the minus.
Jim Grosbachd0588e22011-09-14 18:08:35 +00002516 SMLoc EndLoc = Parser.getTok().getLoc();
2517 int EndReg = tryParseRegister();
2518 if (EndReg == -1)
2519 return Error(EndLoc, "register expected");
Jim Grosbachce485e72011-11-11 21:27:40 +00002520 // Allow Q regs and just interpret them as the two D sub-registers.
2521 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
2522 EndReg = getDRegFromQReg(EndReg) + 1;
Jim Grosbachd0588e22011-09-14 18:08:35 +00002523 // If the register is the same as the start reg, there's nothing
2524 // more to do.
2525 if (Reg == EndReg)
2526 continue;
2527 // The register must be in the same register class as the first.
2528 if (!RC->contains(EndReg))
2529 return Error(EndLoc, "invalid register in register list");
2530 // Ranges must go from low to high.
2531 if (getARMRegisterNumbering(Reg) > getARMRegisterNumbering(EndReg))
2532 return Error(EndLoc, "bad range in register list");
Kevin Enderbyd7894f12009-10-09 21:12:28 +00002533
Jim Grosbachd0588e22011-09-14 18:08:35 +00002534 // Add all the registers in the range to the register list.
2535 while (Reg != EndReg) {
2536 Reg = getNextRegister(Reg);
2537 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2538 }
2539 continue;
2540 }
2541 Parser.Lex(); // Eat the comma.
2542 RegLoc = Parser.getTok().getLoc();
2543 int OldReg = Reg;
2544 Reg = tryParseRegister();
2545 if (Reg == -1)
Jim Grosbach2d539692011-09-12 23:36:42 +00002546 return Error(RegLoc, "register expected");
Jim Grosbachce485e72011-11-11 21:27:40 +00002547 // Allow Q regs and just interpret them as the two D sub-registers.
2548 bool isQReg = false;
2549 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2550 Reg = getDRegFromQReg(Reg);
2551 isQReg = true;
2552 }
Jim Grosbachd0588e22011-09-14 18:08:35 +00002553 // The register must be in the same register class as the first.
2554 if (!RC->contains(Reg))
2555 return Error(RegLoc, "invalid register in register list");
2556 // List must be monotonically increasing.
2557 if (getARMRegisterNumbering(Reg) <= getARMRegisterNumbering(OldReg))
2558 return Error(RegLoc, "register list not in ascending order");
2559 // VFP register lists must also be contiguous.
2560 // It's OK to use the enumeration values directly here rather, as the
2561 // VFP register classes have the enum sorted properly.
2562 if (RC != &ARMMCRegisterClasses[ARM::GPRRegClassID] &&
2563 Reg != OldReg + 1)
2564 return Error(RegLoc, "non-contiguous register range");
2565 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
Jim Grosbachce485e72011-11-11 21:27:40 +00002566 if (isQReg)
2567 Registers.push_back(std::pair<unsigned, SMLoc>(++Reg, RegLoc));
Bill Wendlinge7176102010-11-06 22:36:58 +00002568 }
2569
Jim Grosbachd0588e22011-09-14 18:08:35 +00002570 SMLoc E = Parser.getTok().getLoc();
2571 if (Parser.getTok().isNot(AsmToken::RCurly))
2572 return Error(E, "'}' expected");
2573 Parser.Lex(); // Eat '}' token.
2574
Bill Wendling50d0f582010-11-18 23:43:05 +00002575 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
2576 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00002577}
2578
Jim Grosbach98b05a52011-11-30 01:09:44 +00002579// Helper function to parse the lane index for vector lists.
2580ARMAsmParser::OperandMatchResultTy ARMAsmParser::
Jim Grosbach7636bf62011-12-02 00:35:16 +00002581parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index) {
2582 Index = 0; // Always return a defined index value.
Jim Grosbach98b05a52011-11-30 01:09:44 +00002583 if (Parser.getTok().is(AsmToken::LBrac)) {
2584 Parser.Lex(); // Eat the '['.
2585 if (Parser.getTok().is(AsmToken::RBrac)) {
2586 // "Dn[]" is the 'all lanes' syntax.
2587 LaneKind = AllLanes;
2588 Parser.Lex(); // Eat the ']'.
2589 return MatchOperand_Success;
2590 }
Jim Grosbach7636bf62011-12-02 00:35:16 +00002591 if (Parser.getTok().is(AsmToken::Integer)) {
2592 int64_t Val = Parser.getTok().getIntVal();
2593 // Make this range check context sensitive for .8, .16, .32.
2594 if (Val < 0 && Val > 7)
2595 Error(Parser.getTok().getLoc(), "lane index out of range");
2596 Index = Val;
2597 LaneKind = IndexedLane;
2598 Parser.Lex(); // Eat the token;
2599 if (Parser.getTok().isNot(AsmToken::RBrac))
2600 Error(Parser.getTok().getLoc(), "']' expected");
2601 Parser.Lex(); // Eat the ']'.
2602 return MatchOperand_Success;
2603 }
2604 Error(Parser.getTok().getLoc(), "lane index must be empty or an integer");
Jim Grosbach98b05a52011-11-30 01:09:44 +00002605 return MatchOperand_ParseFail;
2606 }
2607 LaneKind = NoLanes;
2608 return MatchOperand_Success;
2609}
2610
Jim Grosbach862019c2011-10-18 23:02:30 +00002611// parse a vector register list
2612ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2613parseVectorList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Jim Grosbach98b05a52011-11-30 01:09:44 +00002614 VectorLaneTy LaneKind;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002615 unsigned LaneIndex;
Jim Grosbach5c984e42011-11-15 21:45:55 +00002616 SMLoc S = Parser.getTok().getLoc();
2617 // As an extension (to match gas), support a plain D register or Q register
2618 // (without encosing curly braces) as a single or double entry list,
2619 // respectively.
2620 if (Parser.getTok().is(AsmToken::Identifier)) {
2621 int Reg = tryParseRegister();
2622 if (Reg == -1)
2623 return MatchOperand_NoMatch;
2624 SMLoc E = Parser.getTok().getLoc();
2625 if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg)) {
Jim Grosbach7636bf62011-12-02 00:35:16 +00002626 OperandMatchResultTy Res = parseVectorLane(LaneKind, LaneIndex);
Jim Grosbach98b05a52011-11-30 01:09:44 +00002627 if (Res != MatchOperand_Success)
2628 return Res;
2629 switch (LaneKind) {
2630 default:
2631 assert(0 && "unexpected lane kind!");
2632 case NoLanes:
2633 E = Parser.getTok().getLoc();
2634 Operands.push_back(ARMOperand::CreateVectorList(Reg, 1, S, E));
2635 break;
2636 case AllLanes:
2637 E = Parser.getTok().getLoc();
2638 Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 1, S, E));
2639 break;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002640 case IndexedLane:
2641 Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 1,
2642 LaneIndex, S,E));
2643 break;
Jim Grosbach98b05a52011-11-30 01:09:44 +00002644 }
Jim Grosbach5c984e42011-11-15 21:45:55 +00002645 return MatchOperand_Success;
2646 }
2647 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2648 Reg = getDRegFromQReg(Reg);
Jim Grosbach7636bf62011-12-02 00:35:16 +00002649 OperandMatchResultTy Res = parseVectorLane(LaneKind, LaneIndex);
Jim Grosbach98b05a52011-11-30 01:09:44 +00002650 if (Res != MatchOperand_Success)
2651 return Res;
2652 switch (LaneKind) {
2653 default:
2654 assert(0 && "unexpected lane kind!");
2655 case NoLanes:
2656 E = Parser.getTok().getLoc();
2657 Operands.push_back(ARMOperand::CreateVectorList(Reg, 2, S, E));
2658 break;
2659 case AllLanes:
2660 E = Parser.getTok().getLoc();
2661 Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 2, S, E));
2662 break;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002663 case IndexedLane:
2664 Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 2,
2665 LaneIndex, S,E));
2666 break;
Jim Grosbach98b05a52011-11-30 01:09:44 +00002667 }
Jim Grosbach5c984e42011-11-15 21:45:55 +00002668 return MatchOperand_Success;
2669 }
2670 Error(S, "vector register expected");
2671 return MatchOperand_ParseFail;
2672 }
2673
2674 if (Parser.getTok().isNot(AsmToken::LCurly))
Jim Grosbach862019c2011-10-18 23:02:30 +00002675 return MatchOperand_NoMatch;
2676
Jim Grosbach862019c2011-10-18 23:02:30 +00002677 Parser.Lex(); // Eat '{' token.
2678 SMLoc RegLoc = Parser.getTok().getLoc();
2679
2680 int Reg = tryParseRegister();
2681 if (Reg == -1) {
2682 Error(RegLoc, "register expected");
2683 return MatchOperand_ParseFail;
2684 }
Jim Grosbach862019c2011-10-18 23:02:30 +00002685 unsigned Count = 1;
Jim Grosbachc73d73e2011-10-28 00:06:50 +00002686 unsigned FirstReg = Reg;
2687 // The list is of D registers, but we also allow Q regs and just interpret
2688 // them as the two D sub-registers.
2689 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2690 FirstReg = Reg = getDRegFromQReg(Reg);
2691 ++Reg;
2692 ++Count;
2693 }
Jim Grosbach7636bf62011-12-02 00:35:16 +00002694 if (parseVectorLane(LaneKind, LaneIndex) != MatchOperand_Success)
Jim Grosbach98b05a52011-11-30 01:09:44 +00002695 return MatchOperand_ParseFail;
Jim Grosbachc73d73e2011-10-28 00:06:50 +00002696
Jim Grosbache43862b2011-11-15 23:19:15 +00002697 while (Parser.getTok().is(AsmToken::Comma) ||
2698 Parser.getTok().is(AsmToken::Minus)) {
2699 if (Parser.getTok().is(AsmToken::Minus)) {
2700 Parser.Lex(); // Eat the minus.
2701 SMLoc EndLoc = Parser.getTok().getLoc();
2702 int EndReg = tryParseRegister();
2703 if (EndReg == -1) {
2704 Error(EndLoc, "register expected");
2705 return MatchOperand_ParseFail;
2706 }
2707 // Allow Q regs and just interpret them as the two D sub-registers.
2708 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
2709 EndReg = getDRegFromQReg(EndReg) + 1;
2710 // If the register is the same as the start reg, there's nothing
2711 // more to do.
2712 if (Reg == EndReg)
2713 continue;
2714 // The register must be in the same register class as the first.
2715 if (!ARMMCRegisterClasses[ARM::DPRRegClassID].contains(EndReg)) {
2716 Error(EndLoc, "invalid register in register list");
2717 return MatchOperand_ParseFail;
2718 }
2719 // Ranges must go from low to high.
2720 if (Reg > EndReg) {
2721 Error(EndLoc, "bad range in register list");
2722 return MatchOperand_ParseFail;
2723 }
Jim Grosbach98b05a52011-11-30 01:09:44 +00002724 // Parse the lane specifier if present.
2725 VectorLaneTy NextLaneKind;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002726 unsigned NextLaneIndex;
2727 if (parseVectorLane(NextLaneKind, NextLaneIndex) != MatchOperand_Success)
Jim Grosbach98b05a52011-11-30 01:09:44 +00002728 return MatchOperand_ParseFail;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002729 if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
Jim Grosbach98b05a52011-11-30 01:09:44 +00002730 Error(EndLoc, "mismatched lane index in register list");
2731 return MatchOperand_ParseFail;
2732 }
2733 EndLoc = Parser.getTok().getLoc();
Jim Grosbache43862b2011-11-15 23:19:15 +00002734
2735 // Add all the registers in the range to the register list.
2736 Count += EndReg - Reg;
2737 Reg = EndReg;
2738 continue;
2739 }
Jim Grosbach862019c2011-10-18 23:02:30 +00002740 Parser.Lex(); // Eat the comma.
2741 RegLoc = Parser.getTok().getLoc();
2742 int OldReg = Reg;
2743 Reg = tryParseRegister();
2744 if (Reg == -1) {
2745 Error(RegLoc, "register expected");
2746 return MatchOperand_ParseFail;
2747 }
Jim Grosbachc73d73e2011-10-28 00:06:50 +00002748 // vector register lists must be contiguous.
Jim Grosbach862019c2011-10-18 23:02:30 +00002749 // It's OK to use the enumeration values directly here rather, as the
2750 // VFP register classes have the enum sorted properly.
Jim Grosbachc73d73e2011-10-28 00:06:50 +00002751 //
2752 // The list is of D registers, but we also allow Q regs and just interpret
2753 // them as the two D sub-registers.
2754 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2755 Reg = getDRegFromQReg(Reg);
2756 if (Reg != OldReg + 1) {
2757 Error(RegLoc, "non-contiguous register range");
2758 return MatchOperand_ParseFail;
2759 }
2760 ++Reg;
2761 Count += 2;
Jim Grosbach98b05a52011-11-30 01:09:44 +00002762 // Parse the lane specifier if present.
2763 VectorLaneTy NextLaneKind;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002764 unsigned NextLaneIndex;
Jim Grosbach98b05a52011-11-30 01:09:44 +00002765 SMLoc EndLoc = Parser.getTok().getLoc();
Jim Grosbach7636bf62011-12-02 00:35:16 +00002766 if (parseVectorLane(NextLaneKind, NextLaneIndex) != MatchOperand_Success)
Jim Grosbach98b05a52011-11-30 01:09:44 +00002767 return MatchOperand_ParseFail;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002768 if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
Jim Grosbach98b05a52011-11-30 01:09:44 +00002769 Error(EndLoc, "mismatched lane index in register list");
2770 return MatchOperand_ParseFail;
2771 }
Jim Grosbachc73d73e2011-10-28 00:06:50 +00002772 continue;
2773 }
2774 // Normal D register. Just check that it's contiguous and keep going.
Jim Grosbach862019c2011-10-18 23:02:30 +00002775 if (Reg != OldReg + 1) {
2776 Error(RegLoc, "non-contiguous register range");
2777 return MatchOperand_ParseFail;
2778 }
Jim Grosbach862019c2011-10-18 23:02:30 +00002779 ++Count;
Jim Grosbach98b05a52011-11-30 01:09:44 +00002780 // Parse the lane specifier if present.
2781 VectorLaneTy NextLaneKind;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002782 unsigned NextLaneIndex;
Jim Grosbach98b05a52011-11-30 01:09:44 +00002783 SMLoc EndLoc = Parser.getTok().getLoc();
Jim Grosbach7636bf62011-12-02 00:35:16 +00002784 if (parseVectorLane(NextLaneKind, NextLaneIndex) != MatchOperand_Success)
Jim Grosbach98b05a52011-11-30 01:09:44 +00002785 return MatchOperand_ParseFail;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002786 if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
Jim Grosbach98b05a52011-11-30 01:09:44 +00002787 Error(EndLoc, "mismatched lane index in register list");
2788 return MatchOperand_ParseFail;
2789 }
Jim Grosbach862019c2011-10-18 23:02:30 +00002790 }
2791
2792 SMLoc E = Parser.getTok().getLoc();
2793 if (Parser.getTok().isNot(AsmToken::RCurly)) {
2794 Error(E, "'}' expected");
2795 return MatchOperand_ParseFail;
2796 }
2797 Parser.Lex(); // Eat '}' token.
2798
Jim Grosbach98b05a52011-11-30 01:09:44 +00002799 switch (LaneKind) {
2800 default:
2801 assert(0 && "unexpected lane kind in register list.");
2802 case NoLanes:
2803 Operands.push_back(ARMOperand::CreateVectorList(FirstReg, Count, S, E));
2804 break;
2805 case AllLanes:
2806 Operands.push_back(ARMOperand::CreateVectorListAllLanes(FirstReg, Count,
2807 S, E));
2808 break;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002809 case IndexedLane:
2810 Operands.push_back(ARMOperand::CreateVectorListIndexed(FirstReg, Count,
2811 LaneIndex, S, E));
2812 break;
Jim Grosbach98b05a52011-11-30 01:09:44 +00002813 }
Jim Grosbach862019c2011-10-18 23:02:30 +00002814 return MatchOperand_Success;
2815}
2816
Jim Grosbach43904292011-07-25 20:14:50 +00002817/// parseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
Jim Grosbachf922c472011-02-12 01:34:40 +00002818ARMAsmParser::OperandMatchResultTy ARMAsmParser::
Jim Grosbach43904292011-07-25 20:14:50 +00002819parseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002820 SMLoc S = Parser.getTok().getLoc();
2821 const AsmToken &Tok = Parser.getTok();
2822 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2823 StringRef OptStr = Tok.getString();
2824
2825 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
2826 .Case("sy", ARM_MB::SY)
2827 .Case("st", ARM_MB::ST)
Jim Grosbach032434d2011-07-13 23:40:38 +00002828 .Case("sh", ARM_MB::ISH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002829 .Case("ish", ARM_MB::ISH)
Jim Grosbach032434d2011-07-13 23:40:38 +00002830 .Case("shst", ARM_MB::ISHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002831 .Case("ishst", ARM_MB::ISHST)
2832 .Case("nsh", ARM_MB::NSH)
Jim Grosbach032434d2011-07-13 23:40:38 +00002833 .Case("un", ARM_MB::NSH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002834 .Case("nshst", ARM_MB::NSHST)
Jim Grosbach032434d2011-07-13 23:40:38 +00002835 .Case("unst", ARM_MB::NSHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002836 .Case("osh", ARM_MB::OSH)
2837 .Case("oshst", ARM_MB::OSHST)
2838 .Default(~0U);
2839
2840 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00002841 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002842
2843 Parser.Lex(); // Eat identifier token.
2844 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00002845 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002846}
2847
Jim Grosbach43904292011-07-25 20:14:50 +00002848/// parseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002849ARMAsmParser::OperandMatchResultTy ARMAsmParser::
Jim Grosbach43904292011-07-25 20:14:50 +00002850parseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002851 SMLoc S = Parser.getTok().getLoc();
2852 const AsmToken &Tok = Parser.getTok();
2853 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2854 StringRef IFlagsStr = Tok.getString();
2855
Owen Anderson2dbb46a2011-10-05 17:16:40 +00002856 // An iflags string of "none" is interpreted to mean that none of the AIF
2857 // bits are set. Not a terribly useful instruction, but a valid encoding.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002858 unsigned IFlags = 0;
Owen Anderson2dbb46a2011-10-05 17:16:40 +00002859 if (IFlagsStr != "none") {
2860 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
2861 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
2862 .Case("a", ARM_PROC::A)
2863 .Case("i", ARM_PROC::I)
2864 .Case("f", ARM_PROC::F)
2865 .Default(~0U);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002866
Owen Anderson2dbb46a2011-10-05 17:16:40 +00002867 // If some specific iflag is already set, it means that some letter is
2868 // present more than once, this is not acceptable.
2869 if (Flag == ~0U || (IFlags & Flag))
2870 return MatchOperand_NoMatch;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002871
Owen Anderson2dbb46a2011-10-05 17:16:40 +00002872 IFlags |= Flag;
2873 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002874 }
2875
2876 Parser.Lex(); // Eat identifier token.
2877 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
2878 return MatchOperand_Success;
2879}
2880
Jim Grosbach43904292011-07-25 20:14:50 +00002881/// parseMSRMaskOperand - Try to parse mask flags from MSR instruction.
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002882ARMAsmParser::OperandMatchResultTy ARMAsmParser::
Jim Grosbach43904292011-07-25 20:14:50 +00002883parseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002884 SMLoc S = Parser.getTok().getLoc();
2885 const AsmToken &Tok = Parser.getTok();
2886 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2887 StringRef Mask = Tok.getString();
2888
James Molloyacad68d2011-09-28 14:21:38 +00002889 if (isMClass()) {
2890 // See ARMv6-M 10.1.1
2891 unsigned FlagsVal = StringSwitch<unsigned>(Mask)
2892 .Case("apsr", 0)
2893 .Case("iapsr", 1)
2894 .Case("eapsr", 2)
2895 .Case("xpsr", 3)
2896 .Case("ipsr", 5)
2897 .Case("epsr", 6)
2898 .Case("iepsr", 7)
2899 .Case("msp", 8)
2900 .Case("psp", 9)
2901 .Case("primask", 16)
2902 .Case("basepri", 17)
2903 .Case("basepri_max", 18)
2904 .Case("faultmask", 19)
2905 .Case("control", 20)
2906 .Default(~0U);
2907
2908 if (FlagsVal == ~0U)
2909 return MatchOperand_NoMatch;
2910
2911 if (!hasV7Ops() && FlagsVal >= 17 && FlagsVal <= 19)
2912 // basepri, basepri_max and faultmask only valid for V7m.
2913 return MatchOperand_NoMatch;
2914
2915 Parser.Lex(); // Eat identifier token.
2916 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
2917 return MatchOperand_Success;
2918 }
2919
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002920 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
2921 size_t Start = 0, Next = Mask.find('_');
2922 StringRef Flags = "";
Benjamin Kramer59085362011-11-06 20:37:06 +00002923 std::string SpecReg = Mask.slice(Start, Next).lower();
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002924 if (Next != StringRef::npos)
2925 Flags = Mask.slice(Next+1, Mask.size());
2926
2927 // FlagsVal contains the complete mask:
2928 // 3-0: Mask
2929 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
2930 unsigned FlagsVal = 0;
2931
2932 if (SpecReg == "apsr") {
2933 FlagsVal = StringSwitch<unsigned>(Flags)
Jim Grosbachb29b4dd2011-07-19 22:45:10 +00002934 .Case("nzcvq", 0x8) // same as CPSR_f
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002935 .Case("g", 0x4) // same as CPSR_s
2936 .Case("nzcvqg", 0xc) // same as CPSR_fs
2937 .Default(~0U);
2938
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00002939 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002940 if (!Flags.empty())
2941 return MatchOperand_NoMatch;
2942 else
Jim Grosbachbf841cf2011-09-14 20:03:46 +00002943 FlagsVal = 8; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00002944 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002945 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00002946 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
2947 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002948 for (int i = 0, e = Flags.size(); i != e; ++i) {
2949 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
2950 .Case("c", 1)
2951 .Case("x", 2)
2952 .Case("s", 4)
2953 .Case("f", 8)
2954 .Default(~0U);
2955
2956 // If some specific flag is already set, it means that some letter is
2957 // present more than once, this is not acceptable.
2958 if (FlagsVal == ~0U || (FlagsVal & Flag))
2959 return MatchOperand_NoMatch;
2960 FlagsVal |= Flag;
2961 }
2962 } else // No match for special register.
2963 return MatchOperand_NoMatch;
2964
Owen Anderson7784f1d2011-10-21 18:43:28 +00002965 // Special register without flags is NOT equivalent to "fc" flags.
2966 // NOTE: This is a divergence from gas' behavior. Uncommenting the following
2967 // two lines would enable gas compatibility at the expense of breaking
2968 // round-tripping.
2969 //
2970 // if (!FlagsVal)
2971 // FlagsVal = 0x9;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002972
2973 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
2974 if (SpecReg == "spsr")
2975 FlagsVal |= 16;
2976
2977 Parser.Lex(); // Eat identifier token.
2978 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
2979 return MatchOperand_Success;
2980}
2981
Jim Grosbachf6c05252011-07-21 17:23:04 +00002982ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2983parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands, StringRef Op,
2984 int Low, int High) {
2985 const AsmToken &Tok = Parser.getTok();
2986 if (Tok.isNot(AsmToken::Identifier)) {
2987 Error(Parser.getTok().getLoc(), Op + " operand expected.");
2988 return MatchOperand_ParseFail;
2989 }
2990 StringRef ShiftName = Tok.getString();
Benjamin Kramer59085362011-11-06 20:37:06 +00002991 std::string LowerOp = Op.lower();
2992 std::string UpperOp = Op.upper();
Jim Grosbachf6c05252011-07-21 17:23:04 +00002993 if (ShiftName != LowerOp && ShiftName != UpperOp) {
2994 Error(Parser.getTok().getLoc(), Op + " operand expected.");
2995 return MatchOperand_ParseFail;
2996 }
2997 Parser.Lex(); // Eat shift type token.
2998
2999 // There must be a '#' and a shift amount.
3000 if (Parser.getTok().isNot(AsmToken::Hash)) {
3001 Error(Parser.getTok().getLoc(), "'#' expected");
3002 return MatchOperand_ParseFail;
3003 }
3004 Parser.Lex(); // Eat hash token.
3005
3006 const MCExpr *ShiftAmount;
3007 SMLoc Loc = Parser.getTok().getLoc();
3008 if (getParser().ParseExpression(ShiftAmount)) {
3009 Error(Loc, "illegal expression");
3010 return MatchOperand_ParseFail;
3011 }
3012 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
3013 if (!CE) {
3014 Error(Loc, "constant expression expected");
3015 return MatchOperand_ParseFail;
3016 }
3017 int Val = CE->getValue();
3018 if (Val < Low || Val > High) {
3019 Error(Loc, "immediate value out of range");
3020 return MatchOperand_ParseFail;
3021 }
3022
3023 Operands.push_back(ARMOperand::CreateImm(CE, Loc, Parser.getTok().getLoc()));
3024
3025 return MatchOperand_Success;
3026}
3027
Jim Grosbachc27d4f92011-07-22 17:44:50 +00003028ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3029parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3030 const AsmToken &Tok = Parser.getTok();
3031 SMLoc S = Tok.getLoc();
3032 if (Tok.isNot(AsmToken::Identifier)) {
3033 Error(Tok.getLoc(), "'be' or 'le' operand expected");
3034 return MatchOperand_ParseFail;
3035 }
3036 int Val = StringSwitch<int>(Tok.getString())
3037 .Case("be", 1)
3038 .Case("le", 0)
3039 .Default(-1);
3040 Parser.Lex(); // Eat the token.
3041
3042 if (Val == -1) {
3043 Error(Tok.getLoc(), "'be' or 'le' operand expected");
3044 return MatchOperand_ParseFail;
3045 }
3046 Operands.push_back(ARMOperand::CreateImm(MCConstantExpr::Create(Val,
3047 getContext()),
3048 S, Parser.getTok().getLoc()));
3049 return MatchOperand_Success;
3050}
3051
Jim Grosbach580f4a92011-07-25 22:20:28 +00003052/// parseShifterImm - Parse the shifter immediate operand for SSAT/USAT
3053/// instructions. Legal values are:
3054/// lsl #n 'n' in [0,31]
3055/// asr #n 'n' in [1,32]
3056/// n == 32 encoded as n == 0.
3057ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3058parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3059 const AsmToken &Tok = Parser.getTok();
3060 SMLoc S = Tok.getLoc();
3061 if (Tok.isNot(AsmToken::Identifier)) {
3062 Error(S, "shift operator 'asr' or 'lsl' expected");
3063 return MatchOperand_ParseFail;
3064 }
3065 StringRef ShiftName = Tok.getString();
3066 bool isASR;
3067 if (ShiftName == "lsl" || ShiftName == "LSL")
3068 isASR = false;
3069 else if (ShiftName == "asr" || ShiftName == "ASR")
3070 isASR = true;
3071 else {
3072 Error(S, "shift operator 'asr' or 'lsl' expected");
3073 return MatchOperand_ParseFail;
3074 }
3075 Parser.Lex(); // Eat the operator.
3076
3077 // A '#' and a shift amount.
3078 if (Parser.getTok().isNot(AsmToken::Hash)) {
3079 Error(Parser.getTok().getLoc(), "'#' expected");
3080 return MatchOperand_ParseFail;
3081 }
3082 Parser.Lex(); // Eat hash token.
3083
3084 const MCExpr *ShiftAmount;
3085 SMLoc E = Parser.getTok().getLoc();
3086 if (getParser().ParseExpression(ShiftAmount)) {
3087 Error(E, "malformed shift expression");
3088 return MatchOperand_ParseFail;
3089 }
3090 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
3091 if (!CE) {
3092 Error(E, "shift amount must be an immediate");
3093 return MatchOperand_ParseFail;
3094 }
3095
3096 int64_t Val = CE->getValue();
3097 if (isASR) {
3098 // Shift amount must be in [1,32]
3099 if (Val < 1 || Val > 32) {
3100 Error(E, "'asr' shift amount must be in range [1,32]");
3101 return MatchOperand_ParseFail;
3102 }
Owen Anderson0afa0092011-09-26 21:06:22 +00003103 // asr #32 encoded as asr #0, but is not allowed in Thumb2 mode.
3104 if (isThumb() && Val == 32) {
3105 Error(E, "'asr #32' shift amount not allowed in Thumb mode");
3106 return MatchOperand_ParseFail;
3107 }
Jim Grosbach580f4a92011-07-25 22:20:28 +00003108 if (Val == 32) Val = 0;
3109 } else {
3110 // Shift amount must be in [1,32]
3111 if (Val < 0 || Val > 31) {
3112 Error(E, "'lsr' shift amount must be in range [0,31]");
3113 return MatchOperand_ParseFail;
3114 }
3115 }
3116
3117 E = Parser.getTok().getLoc();
3118 Operands.push_back(ARMOperand::CreateShifterImm(isASR, Val, S, E));
3119
3120 return MatchOperand_Success;
3121}
3122
Jim Grosbach7e1547e2011-07-27 20:15:40 +00003123/// parseRotImm - Parse the shifter immediate operand for SXTB/UXTB family
3124/// of instructions. Legal values are:
3125/// ror #n 'n' in {0, 8, 16, 24}
3126ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3127parseRotImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3128 const AsmToken &Tok = Parser.getTok();
3129 SMLoc S = Tok.getLoc();
Jim Grosbach326efe52011-09-19 20:29:33 +00003130 if (Tok.isNot(AsmToken::Identifier))
3131 return MatchOperand_NoMatch;
Jim Grosbach7e1547e2011-07-27 20:15:40 +00003132 StringRef ShiftName = Tok.getString();
Jim Grosbach326efe52011-09-19 20:29:33 +00003133 if (ShiftName != "ror" && ShiftName != "ROR")
3134 return MatchOperand_NoMatch;
Jim Grosbach7e1547e2011-07-27 20:15:40 +00003135 Parser.Lex(); // Eat the operator.
3136
3137 // A '#' and a rotate amount.
3138 if (Parser.getTok().isNot(AsmToken::Hash)) {
3139 Error(Parser.getTok().getLoc(), "'#' expected");
3140 return MatchOperand_ParseFail;
3141 }
3142 Parser.Lex(); // Eat hash token.
3143
3144 const MCExpr *ShiftAmount;
3145 SMLoc E = Parser.getTok().getLoc();
3146 if (getParser().ParseExpression(ShiftAmount)) {
3147 Error(E, "malformed rotate expression");
3148 return MatchOperand_ParseFail;
3149 }
3150 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
3151 if (!CE) {
3152 Error(E, "rotate amount must be an immediate");
3153 return MatchOperand_ParseFail;
3154 }
3155
3156 int64_t Val = CE->getValue();
3157 // Shift amount must be in {0, 8, 16, 24} (0 is undocumented extension)
3158 // normally, zero is represented in asm by omitting the rotate operand
3159 // entirely.
3160 if (Val != 8 && Val != 16 && Val != 24 && Val != 0) {
3161 Error(E, "'ror' rotate amount must be 8, 16, or 24");
3162 return MatchOperand_ParseFail;
3163 }
3164
3165 E = Parser.getTok().getLoc();
3166 Operands.push_back(ARMOperand::CreateRotImm(Val, S, E));
3167
3168 return MatchOperand_Success;
3169}
3170
Jim Grosbach293a2ee2011-07-28 21:34:26 +00003171ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3172parseBitfield(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3173 SMLoc S = Parser.getTok().getLoc();
3174 // The bitfield descriptor is really two operands, the LSB and the width.
3175 if (Parser.getTok().isNot(AsmToken::Hash)) {
3176 Error(Parser.getTok().getLoc(), "'#' expected");
3177 return MatchOperand_ParseFail;
3178 }
3179 Parser.Lex(); // Eat hash token.
3180
3181 const MCExpr *LSBExpr;
3182 SMLoc E = Parser.getTok().getLoc();
3183 if (getParser().ParseExpression(LSBExpr)) {
3184 Error(E, "malformed immediate expression");
3185 return MatchOperand_ParseFail;
3186 }
3187 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LSBExpr);
3188 if (!CE) {
3189 Error(E, "'lsb' operand must be an immediate");
3190 return MatchOperand_ParseFail;
3191 }
3192
3193 int64_t LSB = CE->getValue();
3194 // The LSB must be in the range [0,31]
3195 if (LSB < 0 || LSB > 31) {
3196 Error(E, "'lsb' operand must be in the range [0,31]");
3197 return MatchOperand_ParseFail;
3198 }
3199 E = Parser.getTok().getLoc();
3200
3201 // Expect another immediate operand.
3202 if (Parser.getTok().isNot(AsmToken::Comma)) {
3203 Error(Parser.getTok().getLoc(), "too few operands");
3204 return MatchOperand_ParseFail;
3205 }
3206 Parser.Lex(); // Eat hash token.
3207 if (Parser.getTok().isNot(AsmToken::Hash)) {
3208 Error(Parser.getTok().getLoc(), "'#' expected");
3209 return MatchOperand_ParseFail;
3210 }
3211 Parser.Lex(); // Eat hash token.
3212
3213 const MCExpr *WidthExpr;
3214 if (getParser().ParseExpression(WidthExpr)) {
3215 Error(E, "malformed immediate expression");
3216 return MatchOperand_ParseFail;
3217 }
3218 CE = dyn_cast<MCConstantExpr>(WidthExpr);
3219 if (!CE) {
3220 Error(E, "'width' operand must be an immediate");
3221 return MatchOperand_ParseFail;
3222 }
3223
3224 int64_t Width = CE->getValue();
3225 // The LSB must be in the range [1,32-lsb]
3226 if (Width < 1 || Width > 32 - LSB) {
3227 Error(E, "'width' operand must be in the range [1,32-lsb]");
3228 return MatchOperand_ParseFail;
3229 }
3230 E = Parser.getTok().getLoc();
3231
3232 Operands.push_back(ARMOperand::CreateBitfield(LSB, Width, S, E));
3233
3234 return MatchOperand_Success;
3235}
3236
Jim Grosbach7ce05792011-08-03 23:50:40 +00003237ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3238parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3239 // Check for a post-index addressing register operand. Specifically:
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00003240 // postidx_reg := '+' register {, shift}
3241 // | '-' register {, shift}
3242 // | register {, shift}
Jim Grosbach7ce05792011-08-03 23:50:40 +00003243
3244 // This method must return MatchOperand_NoMatch without consuming any tokens
3245 // in the case where there is no match, as other alternatives take other
3246 // parse methods.
3247 AsmToken Tok = Parser.getTok();
3248 SMLoc S = Tok.getLoc();
3249 bool haveEaten = false;
Jim Grosbach16578b52011-08-05 16:11:38 +00003250 bool isAdd = true;
Jim Grosbach7ce05792011-08-03 23:50:40 +00003251 int Reg = -1;
3252 if (Tok.is(AsmToken::Plus)) {
3253 Parser.Lex(); // Eat the '+' token.
3254 haveEaten = true;
3255 } else if (Tok.is(AsmToken::Minus)) {
3256 Parser.Lex(); // Eat the '-' token.
Jim Grosbach16578b52011-08-05 16:11:38 +00003257 isAdd = false;
Jim Grosbach7ce05792011-08-03 23:50:40 +00003258 haveEaten = true;
3259 }
3260 if (Parser.getTok().is(AsmToken::Identifier))
3261 Reg = tryParseRegister();
3262 if (Reg == -1) {
3263 if (!haveEaten)
3264 return MatchOperand_NoMatch;
3265 Error(Parser.getTok().getLoc(), "register expected");
3266 return MatchOperand_ParseFail;
3267 }
3268 SMLoc E = Parser.getTok().getLoc();
3269
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00003270 ARM_AM::ShiftOpc ShiftTy = ARM_AM::no_shift;
3271 unsigned ShiftImm = 0;
Jim Grosbach0d6fac32011-08-05 22:03:36 +00003272 if (Parser.getTok().is(AsmToken::Comma)) {
3273 Parser.Lex(); // Eat the ','.
3274 if (parseMemRegOffsetShift(ShiftTy, ShiftImm))
3275 return MatchOperand_ParseFail;
3276 }
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00003277
3278 Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ShiftTy,
3279 ShiftImm, S, E));
Jim Grosbach7ce05792011-08-03 23:50:40 +00003280
3281 return MatchOperand_Success;
3282}
3283
Jim Grosbach251bf252011-08-10 21:56:18 +00003284ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3285parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3286 // Check for a post-index addressing register operand. Specifically:
3287 // am3offset := '+' register
3288 // | '-' register
3289 // | register
3290 // | # imm
3291 // | # + imm
3292 // | # - imm
3293
3294 // This method must return MatchOperand_NoMatch without consuming any tokens
3295 // in the case where there is no match, as other alternatives take other
3296 // parse methods.
3297 AsmToken Tok = Parser.getTok();
3298 SMLoc S = Tok.getLoc();
3299
3300 // Do immediates first, as we always parse those if we have a '#'.
3301 if (Parser.getTok().is(AsmToken::Hash)) {
3302 Parser.Lex(); // Eat the '#'.
3303 // Explicitly look for a '-', as we need to encode negative zero
3304 // differently.
3305 bool isNegative = Parser.getTok().is(AsmToken::Minus);
3306 const MCExpr *Offset;
3307 if (getParser().ParseExpression(Offset))
3308 return MatchOperand_ParseFail;
3309 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
3310 if (!CE) {
3311 Error(S, "constant expression expected");
3312 return MatchOperand_ParseFail;
3313 }
3314 SMLoc E = Tok.getLoc();
3315 // Negative zero is encoded as the flag value INT32_MIN.
3316 int32_t Val = CE->getValue();
3317 if (isNegative && Val == 0)
3318 Val = INT32_MIN;
3319
3320 Operands.push_back(
3321 ARMOperand::CreateImm(MCConstantExpr::Create(Val, getContext()), S, E));
3322
3323 return MatchOperand_Success;
3324 }
3325
3326
3327 bool haveEaten = false;
3328 bool isAdd = true;
3329 int Reg = -1;
3330 if (Tok.is(AsmToken::Plus)) {
3331 Parser.Lex(); // Eat the '+' token.
3332 haveEaten = true;
3333 } else if (Tok.is(AsmToken::Minus)) {
3334 Parser.Lex(); // Eat the '-' token.
3335 isAdd = false;
3336 haveEaten = true;
3337 }
3338 if (Parser.getTok().is(AsmToken::Identifier))
3339 Reg = tryParseRegister();
3340 if (Reg == -1) {
3341 if (!haveEaten)
3342 return MatchOperand_NoMatch;
3343 Error(Parser.getTok().getLoc(), "register expected");
3344 return MatchOperand_ParseFail;
3345 }
3346 SMLoc E = Parser.getTok().getLoc();
3347
3348 Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ARM_AM::no_shift,
3349 0, S, E));
3350
3351 return MatchOperand_Success;
3352}
3353
Jim Grosbacha77295d2011-09-08 22:07:06 +00003354/// cvtT2LdrdPre - Convert parsed operands to MCInst.
3355/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3356/// when they refer multiple MIOperands inside a single one.
3357bool ARMAsmParser::
3358cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
3359 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3360 // Rt, Rt2
3361 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3362 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3363 // Create a writeback register dummy placeholder.
3364 Inst.addOperand(MCOperand::CreateReg(0));
3365 // addr
3366 ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
3367 // pred
3368 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3369 return true;
3370}
3371
3372/// cvtT2StrdPre - Convert parsed operands to MCInst.
3373/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3374/// when they refer multiple MIOperands inside a single one.
3375bool ARMAsmParser::
3376cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
3377 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3378 // Create a writeback register dummy placeholder.
3379 Inst.addOperand(MCOperand::CreateReg(0));
3380 // Rt, Rt2
3381 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3382 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3383 // addr
3384 ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
3385 // pred
3386 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3387 return true;
3388}
3389
Jim Grosbacheeec0252011-09-08 00:39:19 +00003390/// cvtLdWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
3391/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3392/// when they refer multiple MIOperands inside a single one.
3393bool ARMAsmParser::
3394cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
3395 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3396 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3397
3398 // Create a writeback register dummy placeholder.
3399 Inst.addOperand(MCOperand::CreateImm(0));
3400
3401 ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
3402 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3403 return true;
3404}
3405
Jim Grosbachee2c2a42011-09-16 21:55:56 +00003406/// cvtStWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
3407/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3408/// when they refer multiple MIOperands inside a single one.
3409bool ARMAsmParser::
3410cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
3411 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3412 // Create a writeback register dummy placeholder.
3413 Inst.addOperand(MCOperand::CreateImm(0));
3414 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3415 ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
3416 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3417 return true;
3418}
3419
Jim Grosbach1355cf12011-07-26 17:10:22 +00003420/// cvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003421/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3422/// when they refer multiple MIOperands inside a single one.
3423bool ARMAsmParser::
Jim Grosbach1355cf12011-07-26 17:10:22 +00003424cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003425 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3426 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3427
3428 // Create a writeback register dummy placeholder.
3429 Inst.addOperand(MCOperand::CreateImm(0));
3430
Jim Grosbach7ce05792011-08-03 23:50:40 +00003431 ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003432 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3433 return true;
3434}
3435
Owen Anderson9ab0f252011-08-26 20:43:14 +00003436/// cvtLdWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
3437/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3438/// when they refer multiple MIOperands inside a single one.
3439bool ARMAsmParser::
3440cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
3441 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3442 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3443
3444 // Create a writeback register dummy placeholder.
3445 Inst.addOperand(MCOperand::CreateImm(0));
3446
3447 ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
3448 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3449 return true;
3450}
3451
3452
Jim Grosbach548340c2011-08-11 19:22:40 +00003453/// cvtStWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
3454/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3455/// when they refer multiple MIOperands inside a single one.
3456bool ARMAsmParser::
3457cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
3458 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3459 // Create a writeback register dummy placeholder.
3460 Inst.addOperand(MCOperand::CreateImm(0));
3461 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3462 ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
3463 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3464 return true;
3465}
3466
Jim Grosbach1355cf12011-07-26 17:10:22 +00003467/// cvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003468/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3469/// when they refer multiple MIOperands inside a single one.
3470bool ARMAsmParser::
Jim Grosbach1355cf12011-07-26 17:10:22 +00003471cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003472 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3473 // Create a writeback register dummy placeholder.
3474 Inst.addOperand(MCOperand::CreateImm(0));
Jim Grosbach548340c2011-08-11 19:22:40 +00003475 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3476 ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
3477 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
Jim Grosbach7ce05792011-08-03 23:50:40 +00003478 return true;
3479}
3480
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00003481/// cvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
3482/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3483/// when they refer multiple MIOperands inside a single one.
3484bool ARMAsmParser::
3485cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
3486 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3487 // Create a writeback register dummy placeholder.
3488 Inst.addOperand(MCOperand::CreateImm(0));
3489 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3490 ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
3491 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3492 return true;
3493}
3494
Jim Grosbach7ce05792011-08-03 23:50:40 +00003495/// cvtLdExtTWriteBackImm - Convert parsed operands to MCInst.
3496/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3497/// when they refer multiple MIOperands inside a single one.
3498bool ARMAsmParser::
3499cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
3500 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3501 // Rt
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003502 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
Jim Grosbach7ce05792011-08-03 23:50:40 +00003503 // Create a writeback register dummy placeholder.
3504 Inst.addOperand(MCOperand::CreateImm(0));
3505 // addr
3506 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3507 // offset
3508 ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
3509 // pred
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003510 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3511 return true;
3512}
3513
Jim Grosbach7ce05792011-08-03 23:50:40 +00003514/// cvtLdExtTWriteBackReg - Convert parsed operands to MCInst.
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003515/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3516/// when they refer multiple MIOperands inside a single one.
3517bool ARMAsmParser::
Jim Grosbach7ce05792011-08-03 23:50:40 +00003518cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
3519 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3520 // Rt
Owen Andersonaa3402e2011-07-28 17:18:57 +00003521 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003522 // Create a writeback register dummy placeholder.
3523 Inst.addOperand(MCOperand::CreateImm(0));
Jim Grosbach7ce05792011-08-03 23:50:40 +00003524 // addr
3525 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3526 // offset
3527 ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
3528 // pred
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003529 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3530 return true;
3531}
3532
Jim Grosbach7ce05792011-08-03 23:50:40 +00003533/// cvtStExtTWriteBackImm - Convert parsed operands to MCInst.
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003534/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3535/// when they refer multiple MIOperands inside a single one.
3536bool ARMAsmParser::
Jim Grosbach7ce05792011-08-03 23:50:40 +00003537cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
3538 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003539 // Create a writeback register dummy placeholder.
3540 Inst.addOperand(MCOperand::CreateImm(0));
Jim Grosbach7ce05792011-08-03 23:50:40 +00003541 // Rt
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003542 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
Jim Grosbach7ce05792011-08-03 23:50:40 +00003543 // addr
3544 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3545 // offset
3546 ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
3547 // pred
3548 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3549 return true;
3550}
3551
3552/// cvtStExtTWriteBackReg - Convert parsed operands to MCInst.
3553/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3554/// when they refer multiple MIOperands inside a single one.
3555bool ARMAsmParser::
3556cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
3557 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3558 // Create a writeback register dummy placeholder.
3559 Inst.addOperand(MCOperand::CreateImm(0));
3560 // Rt
3561 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3562 // addr
3563 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3564 // offset
3565 ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
3566 // pred
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003567 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3568 return true;
3569}
3570
Jim Grosbach2fd2b872011-08-10 20:29:19 +00003571/// cvtLdrdPre - Convert parsed operands to MCInst.
3572/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3573/// when they refer multiple MIOperands inside a single one.
3574bool ARMAsmParser::
3575cvtLdrdPre(MCInst &Inst, unsigned Opcode,
3576 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3577 // Rt, Rt2
3578 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3579 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3580 // Create a writeback register dummy placeholder.
3581 Inst.addOperand(MCOperand::CreateImm(0));
3582 // addr
3583 ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
3584 // pred
3585 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3586 return true;
3587}
3588
Jim Grosbach14605d12011-08-11 20:28:23 +00003589/// cvtStrdPre - Convert parsed operands to MCInst.
3590/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3591/// when they refer multiple MIOperands inside a single one.
3592bool ARMAsmParser::
3593cvtStrdPre(MCInst &Inst, unsigned Opcode,
3594 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3595 // Create a writeback register dummy placeholder.
3596 Inst.addOperand(MCOperand::CreateImm(0));
3597 // Rt, Rt2
3598 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3599 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3600 // addr
3601 ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
3602 // pred
3603 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3604 return true;
3605}
3606
Jim Grosbach623a4542011-08-10 22:42:16 +00003607/// cvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
3608/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3609/// when they refer multiple MIOperands inside a single one.
3610bool ARMAsmParser::
3611cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
3612 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3613 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3614 // Create a writeback register dummy placeholder.
3615 Inst.addOperand(MCOperand::CreateImm(0));
3616 ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
3617 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3618 return true;
3619}
3620
Jim Grosbach88ae2bc2011-08-19 22:07:46 +00003621/// cvtThumbMultiple- Convert parsed operands to MCInst.
3622/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3623/// when they refer multiple MIOperands inside a single one.
3624bool ARMAsmParser::
3625cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
3626 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3627 // The second source operand must be the same register as the destination
3628 // operand.
3629 if (Operands.size() == 6 &&
Jim Grosbach7a010692011-08-19 22:30:46 +00003630 (((ARMOperand*)Operands[3])->getReg() !=
3631 ((ARMOperand*)Operands[5])->getReg()) &&
3632 (((ARMOperand*)Operands[3])->getReg() !=
3633 ((ARMOperand*)Operands[4])->getReg())) {
Jim Grosbach88ae2bc2011-08-19 22:07:46 +00003634 Error(Operands[3]->getStartLoc(),
Jim Grosbach7a010692011-08-19 22:30:46 +00003635 "destination register must match source register");
Jim Grosbach88ae2bc2011-08-19 22:07:46 +00003636 return false;
3637 }
3638 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3639 ((ARMOperand*)Operands[1])->addCCOutOperands(Inst, 1);
Jim Grosbach1b332862011-11-10 22:10:12 +00003640 // If we have a three-operand form, make sure to set Rn to be the operand
3641 // that isn't the same as Rd.
3642 unsigned RegOp = 4;
3643 if (Operands.size() == 6 &&
3644 ((ARMOperand*)Operands[4])->getReg() ==
3645 ((ARMOperand*)Operands[3])->getReg())
3646 RegOp = 5;
3647 ((ARMOperand*)Operands[RegOp])->addRegOperands(Inst, 1);
3648 Inst.addOperand(Inst.getOperand(0));
Jim Grosbach88ae2bc2011-08-19 22:07:46 +00003649 ((ARMOperand*)Operands[2])->addCondCodeOperands(Inst, 2);
3650
3651 return true;
3652}
Jim Grosbach623a4542011-08-10 22:42:16 +00003653
Jim Grosbach12431322011-10-24 22:16:58 +00003654bool ARMAsmParser::
3655cvtVLDwbFixed(MCInst &Inst, unsigned Opcode,
3656 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3657 // Vd
Jim Grosbach6029b6d2011-11-29 23:51:09 +00003658 ((ARMOperand*)Operands[3])->addVecListOperands(Inst, 1);
Jim Grosbach12431322011-10-24 22:16:58 +00003659 // Create a writeback register dummy placeholder.
3660 Inst.addOperand(MCOperand::CreateImm(0));
3661 // Vn
3662 ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3663 // pred
3664 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3665 return true;
3666}
3667
3668bool ARMAsmParser::
3669cvtVLDwbRegister(MCInst &Inst, unsigned Opcode,
3670 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3671 // Vd
Jim Grosbach6029b6d2011-11-29 23:51:09 +00003672 ((ARMOperand*)Operands[3])->addVecListOperands(Inst, 1);
Jim Grosbach12431322011-10-24 22:16:58 +00003673 // Create a writeback register dummy placeholder.
3674 Inst.addOperand(MCOperand::CreateImm(0));
3675 // Vn
3676 ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3677 // Vm
3678 ((ARMOperand*)Operands[5])->addRegOperands(Inst, 1);
3679 // pred
3680 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3681 return true;
3682}
3683
Jim Grosbach4334e032011-10-31 21:50:31 +00003684bool ARMAsmParser::
3685cvtVSTwbFixed(MCInst &Inst, unsigned Opcode,
3686 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3687 // Create a writeback register dummy placeholder.
3688 Inst.addOperand(MCOperand::CreateImm(0));
3689 // Vn
3690 ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3691 // Vt
Jim Grosbach6029b6d2011-11-29 23:51:09 +00003692 ((ARMOperand*)Operands[3])->addVecListOperands(Inst, 1);
Jim Grosbach4334e032011-10-31 21:50:31 +00003693 // pred
3694 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3695 return true;
3696}
3697
3698bool ARMAsmParser::
3699cvtVSTwbRegister(MCInst &Inst, unsigned Opcode,
3700 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3701 // Create a writeback register dummy placeholder.
3702 Inst.addOperand(MCOperand::CreateImm(0));
3703 // Vn
3704 ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3705 // Vm
3706 ((ARMOperand*)Operands[5])->addRegOperands(Inst, 1);
3707 // Vt
Jim Grosbach6029b6d2011-11-29 23:51:09 +00003708 ((ARMOperand*)Operands[3])->addVecListOperands(Inst, 1);
Jim Grosbach4334e032011-10-31 21:50:31 +00003709 // pred
3710 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3711 return true;
3712}
3713
Bill Wendlinge7176102010-11-06 22:36:58 +00003714/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003715/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00003716bool ARMAsmParser::
Jim Grosbach7ce05792011-08-03 23:50:40 +00003717parseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +00003718 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00003719 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00003720 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00003721 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00003722 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00003723
Sean Callanan18b83232010-01-19 21:44:56 +00003724 const AsmToken &BaseRegTok = Parser.getTok();
Jim Grosbach1355cf12011-07-26 17:10:22 +00003725 int BaseRegNum = tryParseRegister();
Jim Grosbach7ce05792011-08-03 23:50:40 +00003726 if (BaseRegNum == -1)
3727 return Error(BaseRegTok.getLoc(), "register expected");
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00003728
Daniel Dunbar05710932011-01-18 05:34:17 +00003729 // The next token must either be a comma or a closing bracket.
3730 const AsmToken &Tok = Parser.getTok();
3731 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
Jim Grosbach7ce05792011-08-03 23:50:40 +00003732 return Error(Tok.getLoc(), "malformed memory operand");
Daniel Dunbar05710932011-01-18 05:34:17 +00003733
Jim Grosbach7ce05792011-08-03 23:50:40 +00003734 if (Tok.is(AsmToken::RBrac)) {
Sean Callanan76264762010-04-02 22:27:05 +00003735 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00003736 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00003737
Jim Grosbach7ce05792011-08-03 23:50:40 +00003738 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0, ARM_AM::no_shift,
Jim Grosbach57dcb852011-10-11 17:29:55 +00003739 0, 0, false, S, E));
Jim Grosbach03f44a02010-11-29 23:18:01 +00003740
Jim Grosbachfb12f352011-09-19 18:42:21 +00003741 // If there's a pre-indexing writeback marker, '!', just add it as a token
3742 // operand. It's rather odd, but syntactically valid.
3743 if (Parser.getTok().is(AsmToken::Exclaim)) {
3744 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3745 Parser.Lex(); // Eat the '!'.
3746 }
3747
Jim Grosbach7ce05792011-08-03 23:50:40 +00003748 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00003749 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00003750
Jim Grosbach7ce05792011-08-03 23:50:40 +00003751 assert(Tok.is(AsmToken::Comma) && "Lost comma in memory operand?!");
3752 Parser.Lex(); // Eat the comma.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00003753
Jim Grosbach57dcb852011-10-11 17:29:55 +00003754 // If we have a ':', it's an alignment specifier.
3755 if (Parser.getTok().is(AsmToken::Colon)) {
3756 Parser.Lex(); // Eat the ':'.
3757 E = Parser.getTok().getLoc();
3758
3759 const MCExpr *Expr;
3760 if (getParser().ParseExpression(Expr))
3761 return true;
3762
3763 // The expression has to be a constant. Memory references with relocations
3764 // don't come through here, as they use the <label> forms of the relevant
3765 // instructions.
3766 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3767 if (!CE)
3768 return Error (E, "constant expression expected");
3769
3770 unsigned Align = 0;
3771 switch (CE->getValue()) {
3772 default:
3773 return Error(E, "alignment specifier must be 64, 128, or 256 bits");
3774 case 64: Align = 8; break;
3775 case 128: Align = 16; break;
3776 case 256: Align = 32; break;
3777 }
3778
3779 // Now we should have the closing ']'
3780 E = Parser.getTok().getLoc();
3781 if (Parser.getTok().isNot(AsmToken::RBrac))
3782 return Error(E, "']' expected");
3783 Parser.Lex(); // Eat right bracket token.
3784
3785 // Don't worry about range checking the value here. That's handled by
3786 // the is*() predicates.
3787 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0,
3788 ARM_AM::no_shift, 0, Align,
3789 false, S, E));
3790
3791 // If there's a pre-indexing writeback marker, '!', just add it as a token
3792 // operand.
3793 if (Parser.getTok().is(AsmToken::Exclaim)) {
3794 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3795 Parser.Lex(); // Eat the '!'.
3796 }
3797
3798 return false;
3799 }
3800
3801 // If we have a '#', it's an immediate offset, else assume it's a register
Jim Grosbach6cb4b082011-11-15 22:14:41 +00003802 // offset. Be friendly and also accept a plain integer (without a leading
3803 // hash) for gas compatibility.
3804 if (Parser.getTok().is(AsmToken::Hash) ||
3805 Parser.getTok().is(AsmToken::Integer)) {
3806 if (Parser.getTok().is(AsmToken::Hash))
3807 Parser.Lex(); // Eat the '#'.
Jim Grosbach7ce05792011-08-03 23:50:40 +00003808 E = Parser.getTok().getLoc();
Daniel Dunbar05d8b712011-01-18 05:34:24 +00003809
Owen Anderson0da10cf2011-08-29 19:36:44 +00003810 bool isNegative = getParser().getTok().is(AsmToken::Minus);
Jim Grosbach7ce05792011-08-03 23:50:40 +00003811 const MCExpr *Offset;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003812 if (getParser().ParseExpression(Offset))
3813 return true;
Jim Grosbach7ce05792011-08-03 23:50:40 +00003814
3815 // The expression has to be a constant. Memory references with relocations
3816 // don't come through here, as they use the <label> forms of the relevant
3817 // instructions.
3818 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
3819 if (!CE)
3820 return Error (E, "constant expression expected");
3821
Owen Anderson0da10cf2011-08-29 19:36:44 +00003822 // If the constant was #-0, represent it as INT32_MIN.
3823 int32_t Val = CE->getValue();
3824 if (isNegative && Val == 0)
3825 CE = MCConstantExpr::Create(INT32_MIN, getContext());
3826
Jim Grosbach7ce05792011-08-03 23:50:40 +00003827 // Now we should have the closing ']'
3828 E = Parser.getTok().getLoc();
3829 if (Parser.getTok().isNot(AsmToken::RBrac))
3830 return Error(E, "']' expected");
3831 Parser.Lex(); // Eat right bracket token.
3832
3833 // Don't worry about range checking the value here. That's handled by
3834 // the is*() predicates.
3835 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, CE, 0,
Jim Grosbach57dcb852011-10-11 17:29:55 +00003836 ARM_AM::no_shift, 0, 0,
3837 false, S, E));
Jim Grosbach7ce05792011-08-03 23:50:40 +00003838
3839 // If there's a pre-indexing writeback marker, '!', just add it as a token
3840 // operand.
3841 if (Parser.getTok().is(AsmToken::Exclaim)) {
3842 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3843 Parser.Lex(); // Eat the '!'.
3844 }
3845
3846 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00003847 }
Jim Grosbach7ce05792011-08-03 23:50:40 +00003848
3849 // The register offset is optionally preceded by a '+' or '-'
3850 bool isNegative = false;
3851 if (Parser.getTok().is(AsmToken::Minus)) {
3852 isNegative = true;
3853 Parser.Lex(); // Eat the '-'.
3854 } else if (Parser.getTok().is(AsmToken::Plus)) {
3855 // Nothing to do.
3856 Parser.Lex(); // Eat the '+'.
3857 }
3858
3859 E = Parser.getTok().getLoc();
3860 int OffsetRegNum = tryParseRegister();
3861 if (OffsetRegNum == -1)
3862 return Error(E, "register expected");
3863
3864 // If there's a shift operator, handle it.
3865 ARM_AM::ShiftOpc ShiftType = ARM_AM::no_shift;
Jim Grosbach0d6fac32011-08-05 22:03:36 +00003866 unsigned ShiftImm = 0;
Jim Grosbach7ce05792011-08-03 23:50:40 +00003867 if (Parser.getTok().is(AsmToken::Comma)) {
3868 Parser.Lex(); // Eat the ','.
Jim Grosbach0d6fac32011-08-05 22:03:36 +00003869 if (parseMemRegOffsetShift(ShiftType, ShiftImm))
Jim Grosbach7ce05792011-08-03 23:50:40 +00003870 return true;
3871 }
3872
3873 // Now we should have the closing ']'
3874 E = Parser.getTok().getLoc();
3875 if (Parser.getTok().isNot(AsmToken::RBrac))
3876 return Error(E, "']' expected");
3877 Parser.Lex(); // Eat right bracket token.
3878
3879 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, OffsetRegNum,
Jim Grosbach57dcb852011-10-11 17:29:55 +00003880 ShiftType, ShiftImm, 0, isNegative,
Jim Grosbach7ce05792011-08-03 23:50:40 +00003881 S, E));
3882
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00003883 // If there's a pre-indexing writeback marker, '!', just add it as a token
3884 // operand.
3885 if (Parser.getTok().is(AsmToken::Exclaim)) {
3886 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3887 Parser.Lex(); // Eat the '!'.
3888 }
Jim Grosbach7ce05792011-08-03 23:50:40 +00003889
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003890 return false;
3891}
3892
Jim Grosbach7ce05792011-08-03 23:50:40 +00003893/// parseMemRegOffsetShift - one of these two:
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003894/// ( lsl | lsr | asr | ror ) , # shift_amount
3895/// rrx
Jim Grosbach7ce05792011-08-03 23:50:40 +00003896/// return true if it parses a shift otherwise it returns false.
3897bool ARMAsmParser::parseMemRegOffsetShift(ARM_AM::ShiftOpc &St,
3898 unsigned &Amount) {
3899 SMLoc Loc = Parser.getTok().getLoc();
Sean Callanan18b83232010-01-19 21:44:56 +00003900 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003901 if (Tok.isNot(AsmToken::Identifier))
3902 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00003903 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003904 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00003905 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003906 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00003907 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003908 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00003909 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003910 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00003911 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003912 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00003913 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003914 else
Jim Grosbach7ce05792011-08-03 23:50:40 +00003915 return Error(Loc, "illegal shift operator");
Sean Callananb9a25b72010-01-19 20:27:46 +00003916 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003917
Jim Grosbach7ce05792011-08-03 23:50:40 +00003918 // rrx stands alone.
3919 Amount = 0;
3920 if (St != ARM_AM::rrx) {
3921 Loc = Parser.getTok().getLoc();
3922 // A '#' and a shift amount.
3923 const AsmToken &HashTok = Parser.getTok();
3924 if (HashTok.isNot(AsmToken::Hash))
3925 return Error(HashTok.getLoc(), "'#' expected");
3926 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003927
Jim Grosbach7ce05792011-08-03 23:50:40 +00003928 const MCExpr *Expr;
3929 if (getParser().ParseExpression(Expr))
3930 return true;
3931 // Range check the immediate.
3932 // lsl, ror: 0 <= imm <= 31
3933 // lsr, asr: 0 <= imm <= 32
3934 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3935 if (!CE)
3936 return Error(Loc, "shift amount must be an immediate");
3937 int64_t Imm = CE->getValue();
3938 if (Imm < 0 ||
3939 ((St == ARM_AM::lsl || St == ARM_AM::ror) && Imm > 31) ||
3940 ((St == ARM_AM::lsr || St == ARM_AM::asr) && Imm > 32))
3941 return Error(Loc, "immediate shift value out of range");
3942 Amount = Imm;
3943 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00003944
3945 return false;
3946}
3947
Jim Grosbach9d390362011-10-03 23:38:36 +00003948/// parseFPImm - A floating point immediate expression operand.
3949ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3950parseFPImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3951 SMLoc S = Parser.getTok().getLoc();
3952
3953 if (Parser.getTok().isNot(AsmToken::Hash))
3954 return MatchOperand_NoMatch;
Jim Grosbach0e387b22011-10-17 22:26:03 +00003955
3956 // Disambiguate the VMOV forms that can accept an FP immediate.
3957 // vmov.f32 <sreg>, #imm
3958 // vmov.f64 <dreg>, #imm
3959 // vmov.f32 <dreg>, #imm @ vector f32x2
3960 // vmov.f32 <qreg>, #imm @ vector f32x4
3961 //
3962 // There are also the NEON VMOV instructions which expect an
3963 // integer constant. Make sure we don't try to parse an FPImm
3964 // for these:
3965 // vmov.i{8|16|32|64} <dreg|qreg>, #imm
3966 ARMOperand *TyOp = static_cast<ARMOperand*>(Operands[2]);
3967 if (!TyOp->isToken() || (TyOp->getToken() != ".f32" &&
3968 TyOp->getToken() != ".f64"))
3969 return MatchOperand_NoMatch;
3970
Jim Grosbach9d390362011-10-03 23:38:36 +00003971 Parser.Lex(); // Eat the '#'.
3972
3973 // Handle negation, as that still comes through as a separate token.
3974 bool isNegative = false;
3975 if (Parser.getTok().is(AsmToken::Minus)) {
3976 isNegative = true;
3977 Parser.Lex();
3978 }
3979 const AsmToken &Tok = Parser.getTok();
3980 if (Tok.is(AsmToken::Real)) {
3981 APFloat RealVal(APFloat::IEEEdouble, Tok.getString());
3982 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
3983 // If we had a '-' in front, toggle the sign bit.
3984 IntVal ^= (uint64_t)isNegative << 63;
3985 int Val = ARM_AM::getFP64Imm(APInt(64, IntVal));
3986 Parser.Lex(); // Eat the token.
3987 if (Val == -1) {
3988 TokError("floating point value out of range");
3989 return MatchOperand_ParseFail;
3990 }
3991 Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
3992 return MatchOperand_Success;
3993 }
3994 if (Tok.is(AsmToken::Integer)) {
3995 int64_t Val = Tok.getIntVal();
3996 Parser.Lex(); // Eat the token.
3997 if (Val > 255 || Val < 0) {
3998 TokError("encoded floating point value out of range");
3999 return MatchOperand_ParseFail;
4000 }
4001 Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
4002 return MatchOperand_Success;
4003 }
4004
4005 TokError("invalid floating point immediate");
4006 return MatchOperand_ParseFail;
4007}
Kevin Enderby9c41fa82009-10-30 22:55:57 +00004008/// Parse a arm instruction operand. For now this parses the operand regardless
4009/// of the mnemonic.
Jim Grosbach1355cf12011-07-26 17:10:22 +00004010bool ARMAsmParser::parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00004011 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00004012 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00004013
4014 // Check if the current operand has a custom associated parser, if so, try to
4015 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00004016 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
4017 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00004018 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00004019 // If there wasn't a custom match, try the generic matcher below. Otherwise,
4020 // there was a match, but an error occurred, in which case, just return that
4021 // the operand parsing failed.
4022 if (ResTy == MatchOperand_ParseFail)
4023 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00004024
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004025 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00004026 default:
4027 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00004028 return true;
Jim Grosbach19906722011-07-13 18:49:30 +00004029 case AsmToken::Identifier: {
Jim Grosbach5cd5ac62011-10-03 21:12:43 +00004030 // If this is VMRS, check for the apsr_nzcv operand.
Jim Grosbach1355cf12011-07-26 17:10:22 +00004031 if (!tryParseRegisterWithWriteBack(Operands))
Bill Wendling50d0f582010-11-18 23:43:05 +00004032 return false;
Jim Grosbach0d87ec22011-07-26 20:41:24 +00004033 int Res = tryParseShiftRegister(Operands);
Jim Grosbach19906722011-07-13 18:49:30 +00004034 if (Res == 0) // success
Owen Anderson00828302011-03-18 22:50:18 +00004035 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00004036 else if (Res == -1) // irrecoverable error
4037 return true;
Jim Grosbach5cd5ac62011-10-03 21:12:43 +00004038 if (Mnemonic == "vmrs" && Parser.getTok().getString() == "apsr_nzcv") {
4039 S = Parser.getTok().getLoc();
4040 Parser.Lex();
4041 Operands.push_back(ARMOperand::CreateToken("apsr_nzcv", S));
4042 return false;
4043 }
Owen Andersone4e5e2a2011-01-13 21:46:02 +00004044
4045 // Fall though for the Identifier case that is not a register or a
4046 // special name.
Jim Grosbach19906722011-07-13 18:49:30 +00004047 }
Jim Grosbach758a5192011-10-26 21:14:08 +00004048 case AsmToken::LParen: // parenthesized expressions like (_strcmp-4)
Kevin Enderby67b212e2011-01-13 20:32:36 +00004049 case AsmToken::Integer: // things like 1f and 2b as a branch targets
Jim Grosbach6284afc2011-11-01 22:38:31 +00004050 case AsmToken::String: // quoted label names.
Kevin Enderby67b212e2011-01-13 20:32:36 +00004051 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00004052 // This was not a register so parse other operands that start with an
4053 // identifier (like labels) as expressions and create them as immediates.
4054 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00004055 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00004056 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00004057 return true;
Sean Callanan76264762010-04-02 22:27:05 +00004058 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00004059 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
4060 return false;
4061 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004062 case AsmToken::LBrac:
Jim Grosbach1355cf12011-07-26 17:10:22 +00004063 return parseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00004064 case AsmToken::LCurly:
Jim Grosbach1355cf12011-07-26 17:10:22 +00004065 return parseRegisterList(Operands);
Owen Anderson63553c72011-08-29 17:17:09 +00004066 case AsmToken::Hash: {
Kevin Enderby079469f2009-10-13 23:33:38 +00004067 // #42 -> immediate.
4068 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00004069 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00004070 Parser.Lex();
Owen Anderson63553c72011-08-29 17:17:09 +00004071 bool isNegative = Parser.getTok().is(AsmToken::Minus);
Kevin Enderby515d5092009-10-15 20:48:48 +00004072 const MCExpr *ImmVal;
4073 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00004074 return true;
Owen Anderson63553c72011-08-29 17:17:09 +00004075 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmVal);
Jim Grosbached6a0c52011-11-01 22:37:37 +00004076 if (CE) {
4077 int32_t Val = CE->getValue();
4078 if (isNegative && Val == 0)
4079 ImmVal = MCConstantExpr::Create(INT32_MIN, getContext());
Owen Anderson63553c72011-08-29 17:17:09 +00004080 }
Sean Callanan76264762010-04-02 22:27:05 +00004081 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00004082 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
4083 return false;
Owen Anderson63553c72011-08-29 17:17:09 +00004084 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00004085 case AsmToken::Colon: {
4086 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00004087 // FIXME: Check it's an expression prefix,
4088 // e.g. (FOO - :lower16:BAR) isn't legal.
4089 ARMMCExpr::VariantKind RefKind;
Jim Grosbach1355cf12011-07-26 17:10:22 +00004090 if (parsePrefix(RefKind))
Jason W Kim9081b4b2011-01-11 23:53:41 +00004091 return true;
4092
Evan Cheng75972122011-01-13 07:58:56 +00004093 const MCExpr *SubExprVal;
4094 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00004095 return true;
4096
Evan Cheng75972122011-01-13 07:58:56 +00004097 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
4098 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00004099 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00004100 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00004101 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004102 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00004103 }
4104}
4105
Jim Grosbach1355cf12011-07-26 17:10:22 +00004106// parsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
Evan Cheng75972122011-01-13 07:58:56 +00004107// :lower16: and :upper16:.
Jim Grosbach1355cf12011-07-26 17:10:22 +00004108bool ARMAsmParser::parsePrefix(ARMMCExpr::VariantKind &RefKind) {
Evan Cheng75972122011-01-13 07:58:56 +00004109 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00004110
4111 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00004112 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00004113 Parser.Lex(); // Eat ':'
4114
4115 if (getLexer().isNot(AsmToken::Identifier)) {
4116 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
4117 return true;
4118 }
4119
4120 StringRef IDVal = Parser.getTok().getIdentifier();
4121 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00004122 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00004123 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00004124 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00004125 } else {
4126 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
4127 return true;
4128 }
4129 Parser.Lex();
4130
4131 if (getLexer().isNot(AsmToken::Colon)) {
4132 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
4133 return true;
4134 }
4135 Parser.Lex(); // Eat the last ':'
4136 return false;
4137}
4138
Daniel Dunbar352e1482011-01-11 15:59:50 +00004139/// \brief Given a mnemonic, split out possible predication code and carry
4140/// setting letters to form a canonical mnemonic and flags.
4141//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00004142// FIXME: Would be nice to autogen this.
Jim Grosbach89df9962011-08-26 21:43:41 +00004143// FIXME: This is a bit of a maze of special cases.
Jim Grosbach1355cf12011-07-26 17:10:22 +00004144StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic,
Jim Grosbach5f160572011-07-19 20:10:31 +00004145 unsigned &PredicationCode,
4146 bool &CarrySetting,
Jim Grosbach89df9962011-08-26 21:43:41 +00004147 unsigned &ProcessorIMod,
4148 StringRef &ITMask) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00004149 PredicationCode = ARMCC::AL;
4150 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00004151 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00004152
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00004153 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00004154 //
4155 // FIXME: Would be nice to autogen this.
Jim Grosbach5f160572011-07-19 20:10:31 +00004156 if ((Mnemonic == "movs" && isThumb()) ||
4157 Mnemonic == "teq" || Mnemonic == "vceq" || Mnemonic == "svc" ||
4158 Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
4159 Mnemonic == "vmls" || Mnemonic == "vnmls" || Mnemonic == "vacge" ||
4160 Mnemonic == "vcge" || Mnemonic == "vclt" || Mnemonic == "vacgt" ||
4161 Mnemonic == "vcgt" || Mnemonic == "vcle" || Mnemonic == "smlal" ||
4162 Mnemonic == "umaal" || Mnemonic == "umlal" || Mnemonic == "vabal" ||
4163 Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal")
Daniel Dunbar352e1482011-01-11 15:59:50 +00004164 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00004165
Jim Grosbach3f00e312011-07-11 17:09:57 +00004166 // First, split out any predication code. Ignore mnemonics we know aren't
4167 // predicated but do have a carry-set and so weren't caught above.
Jim Grosbachab40f4b2011-07-20 18:20:31 +00004168 if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
Jim Grosbach71725a02011-07-27 21:58:11 +00004169 Mnemonic != "muls" && Mnemonic != "smlals" && Mnemonic != "smulls" &&
Jim Grosbach04d55f12011-08-22 23:55:58 +00004170 Mnemonic != "umlals" && Mnemonic != "umulls" && Mnemonic != "lsls" &&
Jim Grosbach2f25d9b2011-09-01 18:22:13 +00004171 Mnemonic != "sbcs" && Mnemonic != "rscs") {
Jim Grosbach3f00e312011-07-11 17:09:57 +00004172 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
4173 .Case("eq", ARMCC::EQ)
4174 .Case("ne", ARMCC::NE)
4175 .Case("hs", ARMCC::HS)
4176 .Case("cs", ARMCC::HS)
4177 .Case("lo", ARMCC::LO)
4178 .Case("cc", ARMCC::LO)
4179 .Case("mi", ARMCC::MI)
4180 .Case("pl", ARMCC::PL)
4181 .Case("vs", ARMCC::VS)
4182 .Case("vc", ARMCC::VC)
4183 .Case("hi", ARMCC::HI)
4184 .Case("ls", ARMCC::LS)
4185 .Case("ge", ARMCC::GE)
4186 .Case("lt", ARMCC::LT)
4187 .Case("gt", ARMCC::GT)
4188 .Case("le", ARMCC::LE)
4189 .Case("al", ARMCC::AL)
4190 .Default(~0U);
4191 if (CC != ~0U) {
4192 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
4193 PredicationCode = CC;
4194 }
Bill Wendling52925b62010-10-29 23:50:21 +00004195 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00004196
Daniel Dunbar352e1482011-01-11 15:59:50 +00004197 // Next, determine if we have a carry setting bit. We explicitly ignore all
4198 // the instructions we know end in 's'.
4199 if (Mnemonic.endswith("s") &&
Jim Grosbach00f5d982011-08-17 22:49:09 +00004200 !(Mnemonic == "cps" || Mnemonic == "mls" ||
Jim Grosbach5f160572011-07-19 20:10:31 +00004201 Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
4202 Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
4203 Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
Jim Grosbache1cf5902011-07-29 20:26:09 +00004204 Mnemonic == "vrsqrts" || Mnemonic == "srs" ||
4205 (Mnemonic == "movs" && isThumb()))) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00004206 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
4207 CarrySetting = true;
4208 }
4209
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00004210 // The "cps" instruction can have a interrupt mode operand which is glued into
4211 // the mnemonic. Check if this is the case, split it and parse the imod op
4212 if (Mnemonic.startswith("cps")) {
4213 // Split out any imod code.
4214 unsigned IMod =
4215 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
4216 .Case("ie", ARM_PROC::IE)
4217 .Case("id", ARM_PROC::ID)
4218 .Default(~0U);
4219 if (IMod != ~0U) {
4220 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
4221 ProcessorIMod = IMod;
4222 }
4223 }
4224
Jim Grosbach89df9962011-08-26 21:43:41 +00004225 // The "it" instruction has the condition mask on the end of the mnemonic.
4226 if (Mnemonic.startswith("it")) {
4227 ITMask = Mnemonic.slice(2, Mnemonic.size());
4228 Mnemonic = Mnemonic.slice(0, 2);
4229 }
4230
Daniel Dunbar352e1482011-01-11 15:59:50 +00004231 return Mnemonic;
4232}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004233
4234/// \brief Given a canonical mnemonic, determine if the instruction ever allows
4235/// inclusion of carry set or predication code operands.
4236//
4237// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00004238void ARMAsmParser::
Jim Grosbach1355cf12011-07-26 17:10:22 +00004239getMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00004240 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00004241 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
4242 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
Jim Grosbach3443ed52011-09-16 18:05:48 +00004243 Mnemonic == "add" || Mnemonic == "adc" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00004244 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Jim Grosbachd5d0e812011-09-19 23:31:02 +00004245 Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00004246 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
Jim Grosbachd5d0e812011-09-19 23:31:02 +00004247 Mnemonic == "sbc" || Mnemonic == "eor" || Mnemonic == "neg" ||
Jim Grosbach3443ed52011-09-16 18:05:48 +00004248 (!isThumb() && (Mnemonic == "smull" || Mnemonic == "mov" ||
Jim Grosbachd5d0e812011-09-19 23:31:02 +00004249 Mnemonic == "mla" || Mnemonic == "smlal" ||
4250 Mnemonic == "umlal" || Mnemonic == "umull"))) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00004251 CanAcceptCarrySet = true;
Jim Grosbachfb9cffe2011-09-16 16:39:25 +00004252 } else
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00004253 CanAcceptCarrySet = false;
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004254
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00004255 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
4256 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
4257 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
4258 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Jim Grosbachad2dad92011-09-06 20:27:04 +00004259 Mnemonic == "dsb" || Mnemonic == "isb" || Mnemonic == "setend" ||
4260 (Mnemonic == "clrex" && !isThumb()) ||
Jim Grosbach0780b632011-08-19 23:24:36 +00004261 (Mnemonic == "nop" && isThumbOne()) ||
Jim Grosbach2bd01182011-10-11 21:55:36 +00004262 ((Mnemonic == "pld" || Mnemonic == "pli" || Mnemonic == "pldw" ||
4263 Mnemonic == "ldc2" || Mnemonic == "ldc2l" ||
4264 Mnemonic == "stc2" || Mnemonic == "stc2l") && !isThumb()) ||
Jim Grosbach4af54a42011-08-26 22:21:51 +00004265 ((Mnemonic.startswith("rfe") || Mnemonic.startswith("srs")) &&
4266 !isThumb()) ||
Jim Grosbach1ad60c22011-09-10 00:15:36 +00004267 Mnemonic.startswith("cps") || (Mnemonic == "movs" && isThumbOne())) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004268 CanAcceptPredicationCode = false;
Jim Grosbachfb9cffe2011-09-16 16:39:25 +00004269 } else
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004270 CanAcceptPredicationCode = true;
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00004271
Jim Grosbachfb9cffe2011-09-16 16:39:25 +00004272 if (isThumb()) {
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00004273 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00004274 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00004275 CanAcceptPredicationCode = false;
Jim Grosbachfb9cffe2011-09-16 16:39:25 +00004276 }
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00004277}
4278
Jim Grosbachd54b4e62011-08-16 21:12:37 +00004279bool ARMAsmParser::shouldOmitCCOutOperand(StringRef Mnemonic,
4280 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Jim Grosbach20ed2e72011-09-01 00:28:52 +00004281 // FIXME: This is all horribly hacky. We really need a better way to deal
4282 // with optional operands like this in the matcher table.
Jim Grosbachd54b4e62011-08-16 21:12:37 +00004283
4284 // The 'mov' mnemonic is special. One variant has a cc_out operand, while
4285 // another does not. Specifically, the MOVW instruction does not. So we
4286 // special case it here and remove the defaulted (non-setting) cc_out
4287 // operand if that's the instruction we're trying to match.
4288 //
4289 // We do this as post-processing of the explicit operands rather than just
4290 // conditionally adding the cc_out in the first place because we need
4291 // to check the type of the parsed immediate operand.
Owen Anderson8adf6202011-09-14 22:46:14 +00004292 if (Mnemonic == "mov" && Operands.size() > 4 && !isThumb() &&
Jim Grosbachd54b4e62011-08-16 21:12:37 +00004293 !static_cast<ARMOperand*>(Operands[4])->isARMSOImm() &&
4294 static_cast<ARMOperand*>(Operands[4])->isImm0_65535Expr() &&
4295 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
4296 return true;
Jim Grosbach3912b732011-08-16 21:34:08 +00004297
4298 // Register-register 'add' for thumb does not have a cc_out operand
4299 // when there are only two register operands.
4300 if (isThumb() && Mnemonic == "add" && Operands.size() == 5 &&
4301 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4302 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4303 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
4304 return true;
Jim Grosbach72f39f82011-08-24 21:22:15 +00004305 // Register-register 'add' for thumb does not have a cc_out operand
Jim Grosbach20ed2e72011-09-01 00:28:52 +00004306 // when it's an ADD Rdm, SP, {Rdm|#imm0_255} instruction. We do
4307 // have to check the immediate range here since Thumb2 has a variant
4308 // that can handle a different range and has a cc_out operand.
Jim Grosbachf67e8552011-09-16 22:58:42 +00004309 if (((isThumb() && Mnemonic == "add") ||
4310 (isThumbTwo() && Mnemonic == "sub")) &&
4311 Operands.size() == 6 &&
Jim Grosbach72f39f82011-08-24 21:22:15 +00004312 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4313 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4314 static_cast<ARMOperand*>(Operands[4])->getReg() == ARM::SP &&
Jim Grosbach20ed2e72011-09-01 00:28:52 +00004315 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
4316 (static_cast<ARMOperand*>(Operands[5])->isReg() ||
4317 static_cast<ARMOperand*>(Operands[5])->isImm0_1020s4()))
Jim Grosbach72f39f82011-08-24 21:22:15 +00004318 return true;
Jim Grosbachf67e8552011-09-16 22:58:42 +00004319 // For Thumb2, add/sub immediate does not have a cc_out operand for the
4320 // imm0_4095 variant. That's the least-preferred variant when
Jim Grosbach20ed2e72011-09-01 00:28:52 +00004321 // selecting via the generic "add" mnemonic, so to know that we
4322 // should remove the cc_out operand, we have to explicitly check that
4323 // it's not one of the other variants. Ugh.
Jim Grosbachf67e8552011-09-16 22:58:42 +00004324 if (isThumbTwo() && (Mnemonic == "add" || Mnemonic == "sub") &&
4325 Operands.size() == 6 &&
Jim Grosbach20ed2e72011-09-01 00:28:52 +00004326 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4327 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4328 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4329 // Nest conditions rather than one big 'if' statement for readability.
4330 //
4331 // If either register is a high reg, it's either one of the SP
4332 // variants (handled above) or a 32-bit encoding, so we just
4333 // check against T3.
4334 if ((!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
4335 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg())) &&
4336 static_cast<ARMOperand*>(Operands[5])->isT2SOImm())
4337 return false;
4338 // If both registers are low, we're in an IT block, and the immediate is
4339 // in range, we should use encoding T1 instead, which has a cc_out.
4340 if (inITBlock() &&
Jim Grosbach64944f42011-09-14 21:00:40 +00004341 isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) &&
Jim Grosbach20ed2e72011-09-01 00:28:52 +00004342 isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) &&
4343 static_cast<ARMOperand*>(Operands[5])->isImm0_7())
4344 return false;
4345
4346 // Otherwise, we use encoding T4, which does not have a cc_out
4347 // operand.
4348 return true;
4349 }
4350
Jim Grosbach64944f42011-09-14 21:00:40 +00004351 // The thumb2 multiply instruction doesn't have a CCOut register, so
4352 // if we have a "mul" mnemonic in Thumb mode, check if we'll be able to
4353 // use the 16-bit encoding or not.
4354 if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 6 &&
4355 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
4356 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4357 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4358 static_cast<ARMOperand*>(Operands[5])->isReg() &&
4359 // If the registers aren't low regs, the destination reg isn't the
4360 // same as one of the source regs, or the cc_out operand is zero
4361 // outside of an IT block, we have to use the 32-bit encoding, so
4362 // remove the cc_out operand.
4363 (!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
4364 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) ||
Jim Grosbach1de0bd12011-11-15 19:29:45 +00004365 !isARMLowRegister(static_cast<ARMOperand*>(Operands[5])->getReg()) ||
Jim Grosbach64944f42011-09-14 21:00:40 +00004366 !inITBlock() ||
4367 (static_cast<ARMOperand*>(Operands[3])->getReg() !=
4368 static_cast<ARMOperand*>(Operands[5])->getReg() &&
4369 static_cast<ARMOperand*>(Operands[3])->getReg() !=
4370 static_cast<ARMOperand*>(Operands[4])->getReg())))
4371 return true;
4372
Jim Grosbach7f1ec952011-11-15 19:55:16 +00004373 // Also check the 'mul' syntax variant that doesn't specify an explicit
4374 // destination register.
4375 if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 5 &&
4376 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
4377 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4378 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4379 // If the registers aren't low regs or the cc_out operand is zero
4380 // outside of an IT block, we have to use the 32-bit encoding, so
4381 // remove the cc_out operand.
4382 (!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
4383 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) ||
4384 !inITBlock()))
4385 return true;
4386
Jim Grosbach64944f42011-09-14 21:00:40 +00004387
Jim Grosbach20ed2e72011-09-01 00:28:52 +00004388
Jim Grosbachf69c8042011-08-24 21:42:27 +00004389 // Register-register 'add/sub' for thumb does not have a cc_out operand
4390 // when it's an ADD/SUB SP, #imm. Be lenient on count since there's also
4391 // the "add/sub SP, SP, #imm" version. If the follow-up operands aren't
4392 // right, this will result in better diagnostics (which operand is off)
4393 // anyway.
4394 if (isThumb() && (Mnemonic == "add" || Mnemonic == "sub") &&
4395 (Operands.size() == 5 || Operands.size() == 6) &&
Jim Grosbach72f39f82011-08-24 21:22:15 +00004396 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4397 static_cast<ARMOperand*>(Operands[3])->getReg() == ARM::SP &&
4398 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
4399 return true;
Jim Grosbach3912b732011-08-16 21:34:08 +00004400
Jim Grosbachd54b4e62011-08-16 21:12:37 +00004401 return false;
4402}
4403
Jim Grosbach7aef99b2011-11-11 23:08:10 +00004404static bool isDataTypeToken(StringRef Tok) {
4405 return Tok == ".8" || Tok == ".16" || Tok == ".32" || Tok == ".64" ||
4406 Tok == ".i8" || Tok == ".i16" || Tok == ".i32" || Tok == ".i64" ||
4407 Tok == ".u8" || Tok == ".u16" || Tok == ".u32" || Tok == ".u64" ||
4408 Tok == ".s8" || Tok == ".s16" || Tok == ".s32" || Tok == ".s64" ||
4409 Tok == ".p8" || Tok == ".p16" || Tok == ".f32" || Tok == ".f64" ||
4410 Tok == ".f" || Tok == ".d";
4411}
4412
4413// FIXME: This bit should probably be handled via an explicit match class
4414// in the .td files that matches the suffix instead of having it be
4415// a literal string token the way it is now.
4416static bool doesIgnoreDataTypeSuffix(StringRef Mnemonic, StringRef DT) {
4417 return Mnemonic.startswith("vldm") || Mnemonic.startswith("vstm");
4418}
4419
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00004420/// Parse an arm instruction mnemonic followed by its operands.
4421bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
4422 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4423 // Create the leading tokens for the mnemonic, split by '.' characters.
4424 size_t Start = 0, Next = Name.find('.');
Jim Grosbachffa32252011-07-19 19:13:28 +00004425 StringRef Mnemonic = Name.slice(Start, Next);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00004426
Daniel Dunbar352e1482011-01-11 15:59:50 +00004427 // Split out the predication code and carry setting flag from the mnemonic.
4428 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00004429 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00004430 bool CarrySetting;
Jim Grosbach89df9962011-08-26 21:43:41 +00004431 StringRef ITMask;
Jim Grosbach1355cf12011-07-26 17:10:22 +00004432 Mnemonic = splitMnemonic(Mnemonic, PredicationCode, CarrySetting,
Jim Grosbach89df9962011-08-26 21:43:41 +00004433 ProcessorIMod, ITMask);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00004434
Jim Grosbach0c49ac02011-08-25 17:23:55 +00004435 // In Thumb1, only the branch (B) instruction can be predicated.
4436 if (isThumbOne() && PredicationCode != ARMCC::AL && Mnemonic != "b") {
4437 Parser.EatToEndOfStatement();
4438 return Error(NameLoc, "conditional execution not supported in Thumb1");
4439 }
4440
Jim Grosbachffa32252011-07-19 19:13:28 +00004441 Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
4442
Jim Grosbach89df9962011-08-26 21:43:41 +00004443 // Handle the IT instruction ITMask. Convert it to a bitmask. This
4444 // is the mask as it will be for the IT encoding if the conditional
4445 // encoding has a '1' as it's bit0 (i.e. 't' ==> '1'). In the case
4446 // where the conditional bit0 is zero, the instruction post-processing
4447 // will adjust the mask accordingly.
4448 if (Mnemonic == "it") {
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004449 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + 2);
4450 if (ITMask.size() > 3) {
4451 Parser.EatToEndOfStatement();
4452 return Error(Loc, "too many conditions on IT instruction");
4453 }
Jim Grosbach89df9962011-08-26 21:43:41 +00004454 unsigned Mask = 8;
4455 for (unsigned i = ITMask.size(); i != 0; --i) {
4456 char pos = ITMask[i - 1];
4457 if (pos != 't' && pos != 'e') {
4458 Parser.EatToEndOfStatement();
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004459 return Error(Loc, "illegal IT block condition mask '" + ITMask + "'");
Jim Grosbach89df9962011-08-26 21:43:41 +00004460 }
4461 Mask >>= 1;
4462 if (ITMask[i - 1] == 't')
4463 Mask |= 8;
4464 }
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004465 Operands.push_back(ARMOperand::CreateITMask(Mask, Loc));
Jim Grosbach89df9962011-08-26 21:43:41 +00004466 }
4467
Jim Grosbachffa32252011-07-19 19:13:28 +00004468 // FIXME: This is all a pretty gross hack. We should automatically handle
4469 // optional operands like this via tblgen.
Bill Wendling9717fa92010-11-21 10:56:05 +00004470
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004471 // Next, add the CCOut and ConditionCode operands, if needed.
4472 //
4473 // For mnemonics which can ever incorporate a carry setting bit or predication
4474 // code, our matching model involves us always generating CCOut and
4475 // ConditionCode operands to match the mnemonic "as written" and then we let
4476 // the matcher deal with finding the right instruction or generating an
4477 // appropriate error.
4478 bool CanAcceptCarrySet, CanAcceptPredicationCode;
Jim Grosbach1355cf12011-07-26 17:10:22 +00004479 getMnemonicAcceptInfo(Mnemonic, CanAcceptCarrySet, CanAcceptPredicationCode);
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004480
Jim Grosbach33c16a22011-07-14 22:04:21 +00004481 // If we had a carry-set on an instruction that can't do that, issue an
4482 // error.
4483 if (!CanAcceptCarrySet && CarrySetting) {
4484 Parser.EatToEndOfStatement();
Jim Grosbachffa32252011-07-19 19:13:28 +00004485 return Error(NameLoc, "instruction '" + Mnemonic +
Jim Grosbach33c16a22011-07-14 22:04:21 +00004486 "' can not set flags, but 's' suffix specified");
4487 }
Jim Grosbachc27d4f92011-07-22 17:44:50 +00004488 // If we had a predication code on an instruction that can't do that, issue an
4489 // error.
4490 if (!CanAcceptPredicationCode && PredicationCode != ARMCC::AL) {
4491 Parser.EatToEndOfStatement();
4492 return Error(NameLoc, "instruction '" + Mnemonic +
4493 "' is not predicable, but condition code specified");
4494 }
Jim Grosbach33c16a22011-07-14 22:04:21 +00004495
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004496 // Add the carry setting operand, if necessary.
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004497 if (CanAcceptCarrySet) {
4498 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size());
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004499 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004500 Loc));
4501 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004502
4503 // Add the predication code operand, if necessary.
4504 if (CanAcceptPredicationCode) {
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004505 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size() +
4506 CarrySetting);
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004507 Operands.push_back(ARMOperand::CreateCondCode(
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004508 ARMCC::CondCodes(PredicationCode), Loc));
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00004509 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00004510
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00004511 // Add the processor imod operand, if necessary.
4512 if (ProcessorIMod) {
4513 Operands.push_back(ARMOperand::CreateImm(
4514 MCConstantExpr::Create(ProcessorIMod, getContext()),
4515 NameLoc, NameLoc));
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00004516 }
4517
Daniel Dunbar345a9a62010-08-11 06:37:20 +00004518 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00004519 while (Next != StringRef::npos) {
4520 Start = Next;
4521 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00004522 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004523
Jim Grosbach7aef99b2011-11-11 23:08:10 +00004524 // Some NEON instructions have an optional datatype suffix that is
4525 // completely ignored. Check for that.
4526 if (isDataTypeToken(ExtraToken) &&
4527 doesIgnoreDataTypeSuffix(Mnemonic, ExtraToken))
4528 continue;
4529
Jim Grosbach81d2e392011-09-07 16:06:04 +00004530 if (ExtraToken != ".n") {
4531 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
4532 Operands.push_back(ARMOperand::CreateToken(ExtraToken, Loc));
4533 }
Daniel Dunbar5747b132010-08-11 06:37:16 +00004534 }
4535
4536 // Read the remaining operands.
4537 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004538 // Read the first operand.
Jim Grosbach1355cf12011-07-26 17:10:22 +00004539 if (parseOperand(Operands, Mnemonic)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00004540 Parser.EatToEndOfStatement();
4541 return true;
4542 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004543
4544 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00004545 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004546
4547 // Parse and remember the operand.
Jim Grosbach1355cf12011-07-26 17:10:22 +00004548 if (parseOperand(Operands, Mnemonic)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00004549 Parser.EatToEndOfStatement();
4550 return true;
4551 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004552 }
4553 }
Jim Grosbach16c74252010-10-29 14:46:02 +00004554
Chris Lattnercbf8a982010-09-11 16:18:25 +00004555 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbach186ffac2011-10-07 18:27:04 +00004556 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00004557 Parser.EatToEndOfStatement();
Jim Grosbach186ffac2011-10-07 18:27:04 +00004558 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00004559 }
Bill Wendling146018f2010-11-06 21:42:12 +00004560
Chris Lattner34e53142010-09-08 05:10:46 +00004561 Parser.Lex(); // Consume the EndOfStatement
Jim Grosbachffa32252011-07-19 19:13:28 +00004562
Jim Grosbachd54b4e62011-08-16 21:12:37 +00004563 // Some instructions, mostly Thumb, have forms for the same mnemonic that
4564 // do and don't have a cc_out optional-def operand. With some spot-checks
4565 // of the operand list, we can figure out which variant we're trying to
Jim Grosbach20ed2e72011-09-01 00:28:52 +00004566 // parse and adjust accordingly before actually matching. We shouldn't ever
4567 // try to remove a cc_out operand that was explicitly set on the the
4568 // mnemonic, of course (CarrySetting == true). Reason number #317 the
4569 // table driven matcher doesn't fit well with the ARM instruction set.
4570 if (!CarrySetting && shouldOmitCCOutOperand(Mnemonic, Operands)) {
Jim Grosbachffa32252011-07-19 19:13:28 +00004571 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
4572 Operands.erase(Operands.begin() + 1);
4573 delete Op;
4574 }
4575
Jim Grosbachcf121c32011-07-28 21:57:55 +00004576 // ARM mode 'blx' need special handling, as the register operand version
4577 // is predicable, but the label operand version is not. So, we can't rely
4578 // on the Mnemonic based checking to correctly figure out when to put
Jim Grosbach21ff17c2011-10-07 23:24:09 +00004579 // a k_CondCode operand in the list. If we're trying to match the label
4580 // version, remove the k_CondCode operand here.
Jim Grosbachcf121c32011-07-28 21:57:55 +00004581 if (!isThumb() && Mnemonic == "blx" && Operands.size() == 3 &&
4582 static_cast<ARMOperand*>(Operands[2])->isImm()) {
4583 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
4584 Operands.erase(Operands.begin() + 1);
4585 delete Op;
4586 }
Jim Grosbach857e1a72011-08-11 23:51:13 +00004587
4588 // The vector-compare-to-zero instructions have a literal token "#0" at
4589 // the end that comes to here as an immediate operand. Convert it to a
4590 // token to play nicely with the matcher.
4591 if ((Mnemonic == "vceq" || Mnemonic == "vcge" || Mnemonic == "vcgt" ||
4592 Mnemonic == "vcle" || Mnemonic == "vclt") && Operands.size() == 6 &&
4593 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4594 ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
4595 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4596 if (CE && CE->getValue() == 0) {
4597 Operands.erase(Operands.begin() + 5);
4598 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4599 delete Op;
4600 }
4601 }
Jim Grosbach68259142011-10-03 22:30:24 +00004602 // VCMP{E} does the same thing, but with a different operand count.
4603 if ((Mnemonic == "vcmp" || Mnemonic == "vcmpe") && Operands.size() == 5 &&
4604 static_cast<ARMOperand*>(Operands[4])->isImm()) {
4605 ARMOperand *Op = static_cast<ARMOperand*>(Operands[4]);
4606 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4607 if (CE && CE->getValue() == 0) {
4608 Operands.erase(Operands.begin() + 4);
4609 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4610 delete Op;
4611 }
4612 }
Jim Grosbach934755a2011-08-22 23:47:13 +00004613 // Similarly, the Thumb1 "RSB" instruction has a literal "#0" on the
4614 // end. Convert it to a token here.
4615 if (Mnemonic == "rsb" && isThumb() && Operands.size() == 6 &&
4616 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4617 ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
4618 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4619 if (CE && CE->getValue() == 0) {
4620 Operands.erase(Operands.begin() + 5);
4621 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4622 delete Op;
4623 }
4624 }
4625
Chris Lattner98986712010-01-14 22:21:20 +00004626 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00004627}
4628
Jim Grosbach189610f2011-07-26 18:25:39 +00004629// Validate context-sensitive operand constraints.
Jim Grosbachaa875f82011-08-23 18:13:04 +00004630
4631// return 'true' if register list contains non-low GPR registers,
4632// 'false' otherwise. If Reg is in the register list or is HiReg, set
4633// 'containsReg' to true.
4634static bool checkLowRegisterList(MCInst Inst, unsigned OpNo, unsigned Reg,
4635 unsigned HiReg, bool &containsReg) {
4636 containsReg = false;
4637 for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
4638 unsigned OpReg = Inst.getOperand(i).getReg();
4639 if (OpReg == Reg)
4640 containsReg = true;
4641 // Anything other than a low register isn't legal here.
4642 if (!isARMLowRegister(OpReg) && (!HiReg || OpReg != HiReg))
4643 return true;
4644 }
4645 return false;
4646}
4647
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00004648// Check if the specified regisgter is in the register list of the inst,
4649// starting at the indicated operand number.
4650static bool listContainsReg(MCInst &Inst, unsigned OpNo, unsigned Reg) {
4651 for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
4652 unsigned OpReg = Inst.getOperand(i).getReg();
4653 if (OpReg == Reg)
4654 return true;
4655 }
4656 return false;
4657}
4658
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004659// FIXME: We would really prefer to have MCInstrInfo (the wrapper around
4660// the ARMInsts array) instead. Getting that here requires awkward
4661// API changes, though. Better way?
4662namespace llvm {
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00004663extern const MCInstrDesc ARMInsts[];
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004664}
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00004665static const MCInstrDesc &getInstDesc(unsigned Opcode) {
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004666 return ARMInsts[Opcode];
4667}
4668
Jim Grosbach189610f2011-07-26 18:25:39 +00004669// FIXME: We would really like to be able to tablegen'erate this.
4670bool ARMAsmParser::
4671validateInstruction(MCInst &Inst,
4672 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00004673 const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004674 SMLoc Loc = Operands[0]->getStartLoc();
4675 // Check the IT block state first.
Owen Andersonb6b7f512011-09-13 17:59:19 +00004676 // NOTE: In Thumb mode, the BKPT instruction has the interesting property of
4677 // being allowed in IT blocks, but not being predicable. It just always
4678 // executes.
4679 if (inITBlock() && Inst.getOpcode() != ARM::tBKPT) {
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004680 unsigned bit = 1;
4681 if (ITState.FirstCond)
4682 ITState.FirstCond = false;
4683 else
Jim Grosbacha1109882011-09-02 23:22:08 +00004684 bit = (ITState.Mask >> (5 - ITState.CurPosition)) & 1;
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004685 // The instruction must be predicable.
4686 if (!MCID.isPredicable())
4687 return Error(Loc, "instructions in IT block must be predicable");
4688 unsigned Cond = Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm();
4689 unsigned ITCond = bit ? ITState.Cond :
4690 ARMCC::getOppositeCondition(ITState.Cond);
4691 if (Cond != ITCond) {
4692 // Find the condition code Operand to get its SMLoc information.
4693 SMLoc CondLoc;
4694 for (unsigned i = 1; i < Operands.size(); ++i)
4695 if (static_cast<ARMOperand*>(Operands[i])->isCondCode())
4696 CondLoc = Operands[i]->getStartLoc();
4697 return Error(CondLoc, "incorrect condition in IT block; got '" +
4698 StringRef(ARMCondCodeToString(ARMCC::CondCodes(Cond))) +
4699 "', but expected '" +
4700 ARMCondCodeToString(ARMCC::CondCodes(ITCond)) + "'");
4701 }
Jim Grosbachc9a9b442011-08-31 18:29:05 +00004702 // Check for non-'al' condition codes outside of the IT block.
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004703 } else if (isThumbTwo() && MCID.isPredicable() &&
4704 Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
Owen Anderson51f6a7a2011-09-09 21:48:23 +00004705 ARMCC::AL && Inst.getOpcode() != ARM::tB &&
4706 Inst.getOpcode() != ARM::t2B)
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004707 return Error(Loc, "predicated instructions must be in IT block");
4708
Jim Grosbach189610f2011-07-26 18:25:39 +00004709 switch (Inst.getOpcode()) {
Jim Grosbach2fd2b872011-08-10 20:29:19 +00004710 case ARM::LDRD:
4711 case ARM::LDRD_PRE:
4712 case ARM::LDRD_POST:
Jim Grosbach189610f2011-07-26 18:25:39 +00004713 case ARM::LDREXD: {
4714 // Rt2 must be Rt + 1.
4715 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
4716 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4717 if (Rt2 != Rt + 1)
4718 return Error(Operands[3]->getStartLoc(),
4719 "destination operands must be sequential");
4720 return false;
4721 }
Jim Grosbach14605d12011-08-11 20:28:23 +00004722 case ARM::STRD: {
4723 // Rt2 must be Rt + 1.
4724 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
4725 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4726 if (Rt2 != Rt + 1)
4727 return Error(Operands[3]->getStartLoc(),
4728 "source operands must be sequential");
4729 return false;
4730 }
Jim Grosbach53642c52011-08-10 20:49:18 +00004731 case ARM::STRD_PRE:
4732 case ARM::STRD_POST:
Jim Grosbach189610f2011-07-26 18:25:39 +00004733 case ARM::STREXD: {
4734 // Rt2 must be Rt + 1.
4735 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4736 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(2).getReg());
4737 if (Rt2 != Rt + 1)
Jim Grosbach14605d12011-08-11 20:28:23 +00004738 return Error(Operands[3]->getStartLoc(),
Jim Grosbach189610f2011-07-26 18:25:39 +00004739 "source operands must be sequential");
4740 return false;
4741 }
Jim Grosbachfb8989e2011-07-27 21:09:25 +00004742 case ARM::SBFX:
4743 case ARM::UBFX: {
4744 // width must be in range [1, 32-lsb]
4745 unsigned lsb = Inst.getOperand(2).getImm();
4746 unsigned widthm1 = Inst.getOperand(3).getImm();
4747 if (widthm1 >= 32 - lsb)
4748 return Error(Operands[5]->getStartLoc(),
4749 "bitfield width must be in range [1,32-lsb]");
Jim Grosbach00c9a512011-08-16 21:42:31 +00004750 return false;
Jim Grosbachfb8989e2011-07-27 21:09:25 +00004751 }
Jim Grosbach93b3eff2011-08-18 21:50:53 +00004752 case ARM::tLDMIA: {
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00004753 // If we're parsing Thumb2, the .w variant is available and handles
4754 // most cases that are normally illegal for a Thumb1 LDM
4755 // instruction. We'll make the transformation in processInstruction()
4756 // if necessary.
4757 //
Jim Grosbach93b3eff2011-08-18 21:50:53 +00004758 // Thumb LDM instructions are writeback iff the base register is not
4759 // in the register list.
4760 unsigned Rn = Inst.getOperand(0).getReg();
Jim Grosbach7260c6a2011-08-22 23:01:07 +00004761 bool hasWritebackToken =
4762 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
4763 static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
Jim Grosbachaa875f82011-08-23 18:13:04 +00004764 bool listContainsBase;
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00004765 if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) && !isThumbTwo())
Jim Grosbachaa875f82011-08-23 18:13:04 +00004766 return Error(Operands[3 + hasWritebackToken]->getStartLoc(),
4767 "registers must be in range r0-r7");
Jim Grosbach93b3eff2011-08-18 21:50:53 +00004768 // If we should have writeback, then there should be a '!' token.
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00004769 if (!listContainsBase && !hasWritebackToken && !isThumbTwo())
Jim Grosbach93b3eff2011-08-18 21:50:53 +00004770 return Error(Operands[2]->getStartLoc(),
4771 "writeback operator '!' expected");
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00004772 // If we should not have writeback, there must not be a '!'. This is
4773 // true even for the 32-bit wide encodings.
Jim Grosbachaa875f82011-08-23 18:13:04 +00004774 if (listContainsBase && hasWritebackToken)
Jim Grosbach7260c6a2011-08-22 23:01:07 +00004775 return Error(Operands[3]->getStartLoc(),
4776 "writeback operator '!' not allowed when base register "
4777 "in register list");
Jim Grosbach93b3eff2011-08-18 21:50:53 +00004778
4779 break;
4780 }
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00004781 case ARM::t2LDMIA_UPD: {
4782 if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
4783 return Error(Operands[4]->getStartLoc(),
4784 "writeback operator '!' not allowed when base register "
4785 "in register list");
4786 break;
4787 }
Jim Grosbach54026372011-11-10 23:17:11 +00004788 // Like for ldm/stm, push and pop have hi-reg handling version in Thumb2,
4789 // so only issue a diagnostic for thumb1. The instructions will be
4790 // switched to the t2 encodings in processInstruction() if necessary.
Jim Grosbach6dcafc02011-08-22 23:17:34 +00004791 case ARM::tPOP: {
Jim Grosbachaa875f82011-08-23 18:13:04 +00004792 bool listContainsBase;
Jim Grosbach54026372011-11-10 23:17:11 +00004793 if (checkLowRegisterList(Inst, 2, 0, ARM::PC, listContainsBase) &&
4794 !isThumbTwo())
Jim Grosbachaa875f82011-08-23 18:13:04 +00004795 return Error(Operands[2]->getStartLoc(),
4796 "registers must be in range r0-r7 or pc");
Jim Grosbach6dcafc02011-08-22 23:17:34 +00004797 break;
4798 }
4799 case ARM::tPUSH: {
Jim Grosbachaa875f82011-08-23 18:13:04 +00004800 bool listContainsBase;
Jim Grosbach54026372011-11-10 23:17:11 +00004801 if (checkLowRegisterList(Inst, 2, 0, ARM::LR, listContainsBase) &&
4802 !isThumbTwo())
Jim Grosbachaa875f82011-08-23 18:13:04 +00004803 return Error(Operands[2]->getStartLoc(),
4804 "registers must be in range r0-r7 or lr");
Jim Grosbach6dcafc02011-08-22 23:17:34 +00004805 break;
4806 }
Jim Grosbach1e84f192011-08-23 18:15:37 +00004807 case ARM::tSTMIA_UPD: {
4808 bool listContainsBase;
Jim Grosbach8213c962011-09-16 20:50:13 +00004809 if (checkLowRegisterList(Inst, 4, 0, 0, listContainsBase) && !isThumbTwo())
Jim Grosbach1e84f192011-08-23 18:15:37 +00004810 return Error(Operands[4]->getStartLoc(),
4811 "registers must be in range r0-r7");
4812 break;
4813 }
Jim Grosbach189610f2011-07-26 18:25:39 +00004814 }
4815
4816 return false;
4817}
4818
Jim Grosbach84defb52011-12-02 22:34:51 +00004819static unsigned getRealVSTLNOpcode(unsigned Opc) {
4820 switch(Opc) {
4821 default: assert(0 && "unexpected opcode!");
4822 case ARM::VST1LNdWB_fixed_Asm_8: return ARM::VST1LNd8_UPD;
4823 case ARM::VST1LNdWB_fixed_Asm_P8: return ARM::VST1LNd8_UPD;
4824 case ARM::VST1LNdWB_fixed_Asm_I8: return ARM::VST1LNd8_UPD;
4825 case ARM::VST1LNdWB_fixed_Asm_S8: return ARM::VST1LNd8_UPD;
4826 case ARM::VST1LNdWB_fixed_Asm_U8: return ARM::VST1LNd8_UPD;
4827 case ARM::VST1LNdWB_fixed_Asm_16: return ARM::VST1LNd16_UPD;
4828 case ARM::VST1LNdWB_fixed_Asm_P16: return ARM::VST1LNd16_UPD;
4829 case ARM::VST1LNdWB_fixed_Asm_I16: return ARM::VST1LNd16_UPD;
4830 case ARM::VST1LNdWB_fixed_Asm_S16: return ARM::VST1LNd16_UPD;
4831 case ARM::VST1LNdWB_fixed_Asm_U16: return ARM::VST1LNd16_UPD;
4832 case ARM::VST1LNdWB_fixed_Asm_32: return ARM::VST1LNd32_UPD;
4833 case ARM::VST1LNdWB_fixed_Asm_F: return ARM::VST1LNd32_UPD;
4834 case ARM::VST1LNdWB_fixed_Asm_F32: return ARM::VST1LNd32_UPD;
4835 case ARM::VST1LNdWB_fixed_Asm_I32: return ARM::VST1LNd32_UPD;
4836 case ARM::VST1LNdWB_fixed_Asm_S32: return ARM::VST1LNd32_UPD;
4837 case ARM::VST1LNdWB_fixed_Asm_U32: return ARM::VST1LNd32_UPD;
4838 case ARM::VST1LNdWB_register_Asm_8: return ARM::VST1LNd8_UPD;
4839 case ARM::VST1LNdWB_register_Asm_P8: return ARM::VST1LNd8_UPD;
4840 case ARM::VST1LNdWB_register_Asm_I8: return ARM::VST1LNd8_UPD;
4841 case ARM::VST1LNdWB_register_Asm_S8: return ARM::VST1LNd8_UPD;
4842 case ARM::VST1LNdWB_register_Asm_U8: return ARM::VST1LNd8_UPD;
4843 case ARM::VST1LNdWB_register_Asm_16: return ARM::VST1LNd16_UPD;
4844 case ARM::VST1LNdWB_register_Asm_P16: return ARM::VST1LNd16_UPD;
4845 case ARM::VST1LNdWB_register_Asm_I16: return ARM::VST1LNd16_UPD;
4846 case ARM::VST1LNdWB_register_Asm_S16: return ARM::VST1LNd16_UPD;
4847 case ARM::VST1LNdWB_register_Asm_U16: return ARM::VST1LNd16_UPD;
4848 case ARM::VST1LNdWB_register_Asm_32: return ARM::VST1LNd32_UPD;
4849 case ARM::VST1LNdWB_register_Asm_F: return ARM::VST1LNd32_UPD;
4850 case ARM::VST1LNdWB_register_Asm_F32: return ARM::VST1LNd32_UPD;
4851 case ARM::VST1LNdWB_register_Asm_I32: return ARM::VST1LNd32_UPD;
4852 case ARM::VST1LNdWB_register_Asm_S32: return ARM::VST1LNd32_UPD;
4853 case ARM::VST1LNdWB_register_Asm_U32: return ARM::VST1LNd32_UPD;
4854 case ARM::VST1LNdAsm_8: return ARM::VST1LNd8;
4855 case ARM::VST1LNdAsm_P8: return ARM::VST1LNd8;
4856 case ARM::VST1LNdAsm_I8: return ARM::VST1LNd8;
4857 case ARM::VST1LNdAsm_S8: return ARM::VST1LNd8;
4858 case ARM::VST1LNdAsm_U8: return ARM::VST1LNd8;
4859 case ARM::VST1LNdAsm_16: return ARM::VST1LNd16;
4860 case ARM::VST1LNdAsm_P16: return ARM::VST1LNd16;
4861 case ARM::VST1LNdAsm_I16: return ARM::VST1LNd16;
4862 case ARM::VST1LNdAsm_S16: return ARM::VST1LNd16;
4863 case ARM::VST1LNdAsm_U16: return ARM::VST1LNd16;
4864 case ARM::VST1LNdAsm_32: return ARM::VST1LNd32;
4865 case ARM::VST1LNdAsm_F: return ARM::VST1LNd32;
4866 case ARM::VST1LNdAsm_F32: return ARM::VST1LNd32;
4867 case ARM::VST1LNdAsm_I32: return ARM::VST1LNd32;
4868 case ARM::VST1LNdAsm_S32: return ARM::VST1LNd32;
4869 case ARM::VST1LNdAsm_U32: return ARM::VST1LNd32;
4870 }
4871}
4872
4873static unsigned getRealVLDLNOpcode(unsigned Opc) {
Jim Grosbach7636bf62011-12-02 00:35:16 +00004874 switch(Opc) {
4875 default: assert(0 && "unexpected opcode!");
Jim Grosbach872eedb2011-12-02 22:01:52 +00004876 case ARM::VLD1LNdWB_fixed_Asm_8: return ARM::VLD1LNd8_UPD;
4877 case ARM::VLD1LNdWB_fixed_Asm_P8: return ARM::VLD1LNd8_UPD;
4878 case ARM::VLD1LNdWB_fixed_Asm_I8: return ARM::VLD1LNd8_UPD;
4879 case ARM::VLD1LNdWB_fixed_Asm_S8: return ARM::VLD1LNd8_UPD;
4880 case ARM::VLD1LNdWB_fixed_Asm_U8: return ARM::VLD1LNd8_UPD;
4881 case ARM::VLD1LNdWB_fixed_Asm_16: return ARM::VLD1LNd16_UPD;
4882 case ARM::VLD1LNdWB_fixed_Asm_P16: return ARM::VLD1LNd16_UPD;
4883 case ARM::VLD1LNdWB_fixed_Asm_I16: return ARM::VLD1LNd16_UPD;
4884 case ARM::VLD1LNdWB_fixed_Asm_S16: return ARM::VLD1LNd16_UPD;
4885 case ARM::VLD1LNdWB_fixed_Asm_U16: return ARM::VLD1LNd16_UPD;
4886 case ARM::VLD1LNdWB_fixed_Asm_32: return ARM::VLD1LNd32_UPD;
4887 case ARM::VLD1LNdWB_fixed_Asm_F: return ARM::VLD1LNd32_UPD;
4888 case ARM::VLD1LNdWB_fixed_Asm_F32: return ARM::VLD1LNd32_UPD;
4889 case ARM::VLD1LNdWB_fixed_Asm_I32: return ARM::VLD1LNd32_UPD;
4890 case ARM::VLD1LNdWB_fixed_Asm_S32: return ARM::VLD1LNd32_UPD;
4891 case ARM::VLD1LNdWB_fixed_Asm_U32: return ARM::VLD1LNd32_UPD;
4892 case ARM::VLD1LNdWB_register_Asm_8: return ARM::VLD1LNd8_UPD;
4893 case ARM::VLD1LNdWB_register_Asm_P8: return ARM::VLD1LNd8_UPD;
4894 case ARM::VLD1LNdWB_register_Asm_I8: return ARM::VLD1LNd8_UPD;
4895 case ARM::VLD1LNdWB_register_Asm_S8: return ARM::VLD1LNd8_UPD;
4896 case ARM::VLD1LNdWB_register_Asm_U8: return ARM::VLD1LNd8_UPD;
4897 case ARM::VLD1LNdWB_register_Asm_16: return ARM::VLD1LNd16_UPD;
4898 case ARM::VLD1LNdWB_register_Asm_P16: return ARM::VLD1LNd16_UPD;
4899 case ARM::VLD1LNdWB_register_Asm_I16: return ARM::VLD1LNd16_UPD;
4900 case ARM::VLD1LNdWB_register_Asm_S16: return ARM::VLD1LNd16_UPD;
4901 case ARM::VLD1LNdWB_register_Asm_U16: return ARM::VLD1LNd16_UPD;
4902 case ARM::VLD1LNdWB_register_Asm_32: return ARM::VLD1LNd32_UPD;
4903 case ARM::VLD1LNdWB_register_Asm_F: return ARM::VLD1LNd32_UPD;
4904 case ARM::VLD1LNdWB_register_Asm_F32: return ARM::VLD1LNd32_UPD;
4905 case ARM::VLD1LNdWB_register_Asm_I32: return ARM::VLD1LNd32_UPD;
4906 case ARM::VLD1LNdWB_register_Asm_S32: return ARM::VLD1LNd32_UPD;
4907 case ARM::VLD1LNdWB_register_Asm_U32: return ARM::VLD1LNd32_UPD;
Jim Grosbachdad2f8e2011-12-02 18:52:30 +00004908 case ARM::VLD1LNdAsm_8: return ARM::VLD1LNd8;
4909 case ARM::VLD1LNdAsm_P8: return ARM::VLD1LNd8;
4910 case ARM::VLD1LNdAsm_I8: return ARM::VLD1LNd8;
4911 case ARM::VLD1LNdAsm_S8: return ARM::VLD1LNd8;
4912 case ARM::VLD1LNdAsm_U8: return ARM::VLD1LNd8;
Jim Grosbach872eedb2011-12-02 22:01:52 +00004913 case ARM::VLD1LNdAsm_16: return ARM::VLD1LNd16;
4914 case ARM::VLD1LNdAsm_P16: return ARM::VLD1LNd16;
4915 case ARM::VLD1LNdAsm_I16: return ARM::VLD1LNd16;
4916 case ARM::VLD1LNdAsm_S16: return ARM::VLD1LNd16;
4917 case ARM::VLD1LNdAsm_U16: return ARM::VLD1LNd16;
Jim Grosbachdad2f8e2011-12-02 18:52:30 +00004918 case ARM::VLD1LNdAsm_32: return ARM::VLD1LNd32;
4919 case ARM::VLD1LNdAsm_F: return ARM::VLD1LNd32;
4920 case ARM::VLD1LNdAsm_F32: return ARM::VLD1LNd32;
4921 case ARM::VLD1LNdAsm_I32: return ARM::VLD1LNd32;
4922 case ARM::VLD1LNdAsm_S32: return ARM::VLD1LNd32;
4923 case ARM::VLD1LNdAsm_U32: return ARM::VLD1LNd32;
Jim Grosbach7636bf62011-12-02 00:35:16 +00004924 }
4925}
4926
Jim Grosbach83ec8772011-11-10 23:42:14 +00004927bool ARMAsmParser::
Jim Grosbachf8fce712011-08-11 17:35:48 +00004928processInstruction(MCInst &Inst,
4929 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4930 switch (Inst.getOpcode()) {
Jim Grosbach84defb52011-12-02 22:34:51 +00004931 // Handle NEON VST1 complex aliases.
4932 case ARM::VST1LNdWB_register_Asm_8:
4933 case ARM::VST1LNdWB_register_Asm_P8:
4934 case ARM::VST1LNdWB_register_Asm_I8:
4935 case ARM::VST1LNdWB_register_Asm_S8:
4936 case ARM::VST1LNdWB_register_Asm_U8:
4937 case ARM::VST1LNdWB_register_Asm_16:
4938 case ARM::VST1LNdWB_register_Asm_P16:
4939 case ARM::VST1LNdWB_register_Asm_I16:
4940 case ARM::VST1LNdWB_register_Asm_S16:
4941 case ARM::VST1LNdWB_register_Asm_U16:
4942 case ARM::VST1LNdWB_register_Asm_32:
4943 case ARM::VST1LNdWB_register_Asm_F:
4944 case ARM::VST1LNdWB_register_Asm_F32:
4945 case ARM::VST1LNdWB_register_Asm_I32:
4946 case ARM::VST1LNdWB_register_Asm_S32:
4947 case ARM::VST1LNdWB_register_Asm_U32: {
4948 MCInst TmpInst;
4949 // Shuffle the operands around so the lane index operand is in the
4950 // right place.
4951 TmpInst.setOpcode(getRealVSTLNOpcode(Inst.getOpcode()));
4952 TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
4953 TmpInst.addOperand(Inst.getOperand(2)); // Rn
4954 TmpInst.addOperand(Inst.getOperand(3)); // alignment
4955 TmpInst.addOperand(Inst.getOperand(4)); // Rm
4956 TmpInst.addOperand(Inst.getOperand(0)); // Vd
4957 TmpInst.addOperand(Inst.getOperand(1)); // lane
4958 TmpInst.addOperand(Inst.getOperand(5)); // CondCode
4959 TmpInst.addOperand(Inst.getOperand(6));
4960 Inst = TmpInst;
4961 return true;
4962 }
4963 case ARM::VST1LNdWB_fixed_Asm_8:
4964 case ARM::VST1LNdWB_fixed_Asm_P8:
4965 case ARM::VST1LNdWB_fixed_Asm_I8:
4966 case ARM::VST1LNdWB_fixed_Asm_S8:
4967 case ARM::VST1LNdWB_fixed_Asm_U8:
4968 case ARM::VST1LNdWB_fixed_Asm_16:
4969 case ARM::VST1LNdWB_fixed_Asm_P16:
4970 case ARM::VST1LNdWB_fixed_Asm_I16:
4971 case ARM::VST1LNdWB_fixed_Asm_S16:
4972 case ARM::VST1LNdWB_fixed_Asm_U16:
4973 case ARM::VST1LNdWB_fixed_Asm_32:
4974 case ARM::VST1LNdWB_fixed_Asm_F:
4975 case ARM::VST1LNdWB_fixed_Asm_F32:
4976 case ARM::VST1LNdWB_fixed_Asm_I32:
4977 case ARM::VST1LNdWB_fixed_Asm_S32:
4978 case ARM::VST1LNdWB_fixed_Asm_U32: {
4979 MCInst TmpInst;
4980 // Shuffle the operands around so the lane index operand is in the
4981 // right place.
4982 TmpInst.setOpcode(getRealVSTLNOpcode(Inst.getOpcode()));
4983 TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
4984 TmpInst.addOperand(Inst.getOperand(2)); // Rn
4985 TmpInst.addOperand(Inst.getOperand(3)); // alignment
4986 TmpInst.addOperand(MCOperand::CreateReg(0)); // Rm
4987 TmpInst.addOperand(Inst.getOperand(0)); // Vd
4988 TmpInst.addOperand(Inst.getOperand(1)); // lane
4989 TmpInst.addOperand(Inst.getOperand(4)); // CondCode
4990 TmpInst.addOperand(Inst.getOperand(5));
4991 Inst = TmpInst;
4992 return true;
4993 }
4994 case ARM::VST1LNdAsm_8:
4995 case ARM::VST1LNdAsm_P8:
4996 case ARM::VST1LNdAsm_I8:
4997 case ARM::VST1LNdAsm_S8:
4998 case ARM::VST1LNdAsm_U8:
4999 case ARM::VST1LNdAsm_16:
5000 case ARM::VST1LNdAsm_P16:
5001 case ARM::VST1LNdAsm_I16:
5002 case ARM::VST1LNdAsm_S16:
5003 case ARM::VST1LNdAsm_U16:
5004 case ARM::VST1LNdAsm_32:
5005 case ARM::VST1LNdAsm_F:
5006 case ARM::VST1LNdAsm_F32:
5007 case ARM::VST1LNdAsm_I32:
5008 case ARM::VST1LNdAsm_S32:
5009 case ARM::VST1LNdAsm_U32: {
5010 MCInst TmpInst;
5011 // Shuffle the operands around so the lane index operand is in the
5012 // right place.
5013 TmpInst.setOpcode(getRealVSTLNOpcode(Inst.getOpcode()));
5014 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5015 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5016 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5017 TmpInst.addOperand(Inst.getOperand(1)); // lane
5018 TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5019 TmpInst.addOperand(Inst.getOperand(5));
5020 Inst = TmpInst;
5021 return true;
5022 }
Jim Grosbach7636bf62011-12-02 00:35:16 +00005023 // Handle NEON VLD1 complex aliases.
Jim Grosbach872eedb2011-12-02 22:01:52 +00005024 case ARM::VLD1LNdWB_register_Asm_8:
5025 case ARM::VLD1LNdWB_register_Asm_P8:
5026 case ARM::VLD1LNdWB_register_Asm_I8:
5027 case ARM::VLD1LNdWB_register_Asm_S8:
5028 case ARM::VLD1LNdWB_register_Asm_U8:
5029 case ARM::VLD1LNdWB_register_Asm_16:
5030 case ARM::VLD1LNdWB_register_Asm_P16:
5031 case ARM::VLD1LNdWB_register_Asm_I16:
5032 case ARM::VLD1LNdWB_register_Asm_S16:
5033 case ARM::VLD1LNdWB_register_Asm_U16:
5034 case ARM::VLD1LNdWB_register_Asm_32:
5035 case ARM::VLD1LNdWB_register_Asm_F:
5036 case ARM::VLD1LNdWB_register_Asm_F32:
5037 case ARM::VLD1LNdWB_register_Asm_I32:
5038 case ARM::VLD1LNdWB_register_Asm_S32:
5039 case ARM::VLD1LNdWB_register_Asm_U32: {
5040 MCInst TmpInst;
5041 // Shuffle the operands around so the lane index operand is in the
5042 // right place.
Jim Grosbach84defb52011-12-02 22:34:51 +00005043 TmpInst.setOpcode(getRealVLDLNOpcode(Inst.getOpcode()));
Jim Grosbach872eedb2011-12-02 22:01:52 +00005044 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5045 TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5046 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5047 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5048 TmpInst.addOperand(Inst.getOperand(4)); // Rm
5049 TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
5050 TmpInst.addOperand(Inst.getOperand(1)); // lane
5051 TmpInst.addOperand(Inst.getOperand(5)); // CondCode
5052 TmpInst.addOperand(Inst.getOperand(6));
5053 Inst = TmpInst;
5054 return true;
5055 }
5056 case ARM::VLD1LNdWB_fixed_Asm_8:
5057 case ARM::VLD1LNdWB_fixed_Asm_P8:
5058 case ARM::VLD1LNdWB_fixed_Asm_I8:
5059 case ARM::VLD1LNdWB_fixed_Asm_S8:
5060 case ARM::VLD1LNdWB_fixed_Asm_U8:
5061 case ARM::VLD1LNdWB_fixed_Asm_16:
5062 case ARM::VLD1LNdWB_fixed_Asm_P16:
5063 case ARM::VLD1LNdWB_fixed_Asm_I16:
5064 case ARM::VLD1LNdWB_fixed_Asm_S16:
5065 case ARM::VLD1LNdWB_fixed_Asm_U16:
5066 case ARM::VLD1LNdWB_fixed_Asm_32:
5067 case ARM::VLD1LNdWB_fixed_Asm_F:
5068 case ARM::VLD1LNdWB_fixed_Asm_F32:
5069 case ARM::VLD1LNdWB_fixed_Asm_I32:
5070 case ARM::VLD1LNdWB_fixed_Asm_S32:
5071 case ARM::VLD1LNdWB_fixed_Asm_U32: {
5072 MCInst TmpInst;
5073 // Shuffle the operands around so the lane index operand is in the
5074 // right place.
Jim Grosbach84defb52011-12-02 22:34:51 +00005075 TmpInst.setOpcode(getRealVLDLNOpcode(Inst.getOpcode()));
Jim Grosbach872eedb2011-12-02 22:01:52 +00005076 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5077 TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5078 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5079 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5080 TmpInst.addOperand(MCOperand::CreateReg(0)); // Rm
5081 TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
5082 TmpInst.addOperand(Inst.getOperand(1)); // lane
5083 TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5084 TmpInst.addOperand(Inst.getOperand(5));
5085 Inst = TmpInst;
5086 return true;
5087 }
Jim Grosbachdad2f8e2011-12-02 18:52:30 +00005088 case ARM::VLD1LNdAsm_8:
5089 case ARM::VLD1LNdAsm_P8:
5090 case ARM::VLD1LNdAsm_I8:
5091 case ARM::VLD1LNdAsm_S8:
5092 case ARM::VLD1LNdAsm_U8:
5093 case ARM::VLD1LNdAsm_16:
5094 case ARM::VLD1LNdAsm_P16:
5095 case ARM::VLD1LNdAsm_I16:
5096 case ARM::VLD1LNdAsm_S16:
5097 case ARM::VLD1LNdAsm_U16:
5098 case ARM::VLD1LNdAsm_32:
5099 case ARM::VLD1LNdAsm_F:
5100 case ARM::VLD1LNdAsm_F32:
5101 case ARM::VLD1LNdAsm_I32:
5102 case ARM::VLD1LNdAsm_S32:
5103 case ARM::VLD1LNdAsm_U32: {
Jim Grosbach7636bf62011-12-02 00:35:16 +00005104 MCInst TmpInst;
5105 // Shuffle the operands around so the lane index operand is in the
5106 // right place.
Jim Grosbach84defb52011-12-02 22:34:51 +00005107 TmpInst.setOpcode(getRealVLDLNOpcode(Inst.getOpcode()));
Jim Grosbach7636bf62011-12-02 00:35:16 +00005108 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5109 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5110 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5111 TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
5112 TmpInst.addOperand(Inst.getOperand(1)); // lane
5113 TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5114 TmpInst.addOperand(Inst.getOperand(5));
5115 Inst = TmpInst;
5116 return true;
5117 }
Jim Grosbach71810ab2011-11-10 16:44:55 +00005118 // Handle the MOV complex aliases.
Jim Grosbach23f22072011-11-16 18:31:45 +00005119 case ARM::ASRr:
5120 case ARM::LSRr:
5121 case ARM::LSLr:
5122 case ARM::RORr: {
5123 ARM_AM::ShiftOpc ShiftTy;
5124 switch(Inst.getOpcode()) {
5125 default: llvm_unreachable("unexpected opcode!");
5126 case ARM::ASRr: ShiftTy = ARM_AM::asr; break;
5127 case ARM::LSRr: ShiftTy = ARM_AM::lsr; break;
5128 case ARM::LSLr: ShiftTy = ARM_AM::lsl; break;
5129 case ARM::RORr: ShiftTy = ARM_AM::ror; break;
5130 }
5131 // A shift by zero is a plain MOVr, not a MOVsi.
5132 unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, 0);
5133 MCInst TmpInst;
5134 TmpInst.setOpcode(ARM::MOVsr);
5135 TmpInst.addOperand(Inst.getOperand(0)); // Rd
5136 TmpInst.addOperand(Inst.getOperand(1)); // Rn
5137 TmpInst.addOperand(Inst.getOperand(2)); // Rm
5138 TmpInst.addOperand(MCOperand::CreateImm(Shifter)); // Shift value and ty
5139 TmpInst.addOperand(Inst.getOperand(3)); // CondCode
5140 TmpInst.addOperand(Inst.getOperand(4));
5141 TmpInst.addOperand(Inst.getOperand(5)); // cc_out
5142 Inst = TmpInst;
5143 return true;
5144 }
Jim Grosbachee10ff82011-11-10 19:18:01 +00005145 case ARM::ASRi:
5146 case ARM::LSRi:
5147 case ARM::LSLi:
5148 case ARM::RORi: {
5149 ARM_AM::ShiftOpc ShiftTy;
Jim Grosbachee10ff82011-11-10 19:18:01 +00005150 switch(Inst.getOpcode()) {
5151 default: llvm_unreachable("unexpected opcode!");
5152 case ARM::ASRi: ShiftTy = ARM_AM::asr; break;
5153 case ARM::LSRi: ShiftTy = ARM_AM::lsr; break;
5154 case ARM::LSLi: ShiftTy = ARM_AM::lsl; break;
5155 case ARM::RORi: ShiftTy = ARM_AM::ror; break;
5156 }
5157 // A shift by zero is a plain MOVr, not a MOVsi.
Jim Grosbach48b368b2011-11-16 19:05:59 +00005158 unsigned Amt = Inst.getOperand(2).getImm();
Jim Grosbachee10ff82011-11-10 19:18:01 +00005159 unsigned Opc = Amt == 0 ? ARM::MOVr : ARM::MOVsi;
5160 unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, Amt);
Jim Grosbach71810ab2011-11-10 16:44:55 +00005161 MCInst TmpInst;
Jim Grosbachee10ff82011-11-10 19:18:01 +00005162 TmpInst.setOpcode(Opc);
Jim Grosbach71810ab2011-11-10 16:44:55 +00005163 TmpInst.addOperand(Inst.getOperand(0)); // Rd
5164 TmpInst.addOperand(Inst.getOperand(1)); // Rn
Jim Grosbachee10ff82011-11-10 19:18:01 +00005165 if (Opc == ARM::MOVsi)
5166 TmpInst.addOperand(MCOperand::CreateImm(Shifter)); // Shift value and ty
Jim Grosbach71810ab2011-11-10 16:44:55 +00005167 TmpInst.addOperand(Inst.getOperand(3)); // CondCode
5168 TmpInst.addOperand(Inst.getOperand(4));
5169 TmpInst.addOperand(Inst.getOperand(5)); // cc_out
5170 Inst = TmpInst;
Jim Grosbach83ec8772011-11-10 23:42:14 +00005171 return true;
Jim Grosbach71810ab2011-11-10 16:44:55 +00005172 }
Jim Grosbach48b368b2011-11-16 19:05:59 +00005173 case ARM::RRXi: {
5174 unsigned Shifter = ARM_AM::getSORegOpc(ARM_AM::rrx, 0);
5175 MCInst TmpInst;
5176 TmpInst.setOpcode(ARM::MOVsi);
5177 TmpInst.addOperand(Inst.getOperand(0)); // Rd
5178 TmpInst.addOperand(Inst.getOperand(1)); // Rn
5179 TmpInst.addOperand(MCOperand::CreateImm(Shifter)); // Shift value and ty
5180 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
5181 TmpInst.addOperand(Inst.getOperand(3));
5182 TmpInst.addOperand(Inst.getOperand(4)); // cc_out
5183 Inst = TmpInst;
5184 return true;
5185 }
Jim Grosbach0352b462011-11-10 23:58:34 +00005186 case ARM::t2LDMIA_UPD: {
5187 // If this is a load of a single register, then we should use
5188 // a post-indexed LDR instruction instead, per the ARM ARM.
5189 if (Inst.getNumOperands() != 5)
5190 return false;
5191 MCInst TmpInst;
5192 TmpInst.setOpcode(ARM::t2LDR_POST);
5193 TmpInst.addOperand(Inst.getOperand(4)); // Rt
5194 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
5195 TmpInst.addOperand(Inst.getOperand(1)); // Rn
5196 TmpInst.addOperand(MCOperand::CreateImm(4));
5197 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
5198 TmpInst.addOperand(Inst.getOperand(3));
5199 Inst = TmpInst;
5200 return true;
5201 }
5202 case ARM::t2STMDB_UPD: {
5203 // If this is a store of a single register, then we should use
5204 // a pre-indexed STR instruction instead, per the ARM ARM.
5205 if (Inst.getNumOperands() != 5)
5206 return false;
5207 MCInst TmpInst;
5208 TmpInst.setOpcode(ARM::t2STR_PRE);
5209 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
5210 TmpInst.addOperand(Inst.getOperand(4)); // Rt
5211 TmpInst.addOperand(Inst.getOperand(1)); // Rn
5212 TmpInst.addOperand(MCOperand::CreateImm(-4));
5213 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
5214 TmpInst.addOperand(Inst.getOperand(3));
5215 Inst = TmpInst;
5216 return true;
5217 }
Jim Grosbachf8fce712011-08-11 17:35:48 +00005218 case ARM::LDMIA_UPD:
5219 // If this is a load of a single register via a 'pop', then we should use
5220 // a post-indexed LDR instruction instead, per the ARM ARM.
5221 if (static_cast<ARMOperand*>(Operands[0])->getToken() == "pop" &&
5222 Inst.getNumOperands() == 5) {
5223 MCInst TmpInst;
5224 TmpInst.setOpcode(ARM::LDR_POST_IMM);
5225 TmpInst.addOperand(Inst.getOperand(4)); // Rt
5226 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
5227 TmpInst.addOperand(Inst.getOperand(1)); // Rn
5228 TmpInst.addOperand(MCOperand::CreateReg(0)); // am2offset
5229 TmpInst.addOperand(MCOperand::CreateImm(4));
5230 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
5231 TmpInst.addOperand(Inst.getOperand(3));
5232 Inst = TmpInst;
Jim Grosbach83ec8772011-11-10 23:42:14 +00005233 return true;
Jim Grosbachf8fce712011-08-11 17:35:48 +00005234 }
5235 break;
Jim Grosbachf6713912011-08-11 18:07:11 +00005236 case ARM::STMDB_UPD:
5237 // If this is a store of a single register via a 'push', then we should use
5238 // a pre-indexed STR instruction instead, per the ARM ARM.
5239 if (static_cast<ARMOperand*>(Operands[0])->getToken() == "push" &&
5240 Inst.getNumOperands() == 5) {
5241 MCInst TmpInst;
5242 TmpInst.setOpcode(ARM::STR_PRE_IMM);
5243 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
5244 TmpInst.addOperand(Inst.getOperand(4)); // Rt
5245 TmpInst.addOperand(Inst.getOperand(1)); // addrmode_imm12
5246 TmpInst.addOperand(MCOperand::CreateImm(-4));
5247 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
5248 TmpInst.addOperand(Inst.getOperand(3));
5249 Inst = TmpInst;
5250 }
5251 break;
Jim Grosbachda847862011-12-05 21:06:26 +00005252 case ARM::t2ADDri12:
5253 // If the immediate fits for encoding T3 (t2ADDri) and the generic "add"
5254 // mnemonic was used (not "addw"), encoding T3 is preferred.
5255 if (static_cast<ARMOperand*>(Operands[0])->getToken() != "add" ||
5256 ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
5257 break;
5258 Inst.setOpcode(ARM::t2ADDri);
5259 Inst.addOperand(MCOperand::CreateReg(0)); // cc_out
5260 break;
5261 case ARM::t2SUBri12:
5262 // If the immediate fits for encoding T3 (t2SUBri) and the generic "sub"
5263 // mnemonic was used (not "subw"), encoding T3 is preferred.
5264 if (static_cast<ARMOperand*>(Operands[0])->getToken() != "sub" ||
5265 ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
5266 break;
5267 Inst.setOpcode(ARM::t2SUBri);
5268 Inst.addOperand(MCOperand::CreateReg(0)); // cc_out
5269 break;
Jim Grosbach89e2aa62011-08-16 23:57:34 +00005270 case ARM::tADDi8:
Jim Grosbach0f3abd82011-08-31 17:07:33 +00005271 // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
5272 // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
5273 // to encoding T2 if <Rd> is specified and encoding T2 is preferred
5274 // to encoding T1 if <Rd> is omitted."
Jim Grosbach83ec8772011-11-10 23:42:14 +00005275 if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
Jim Grosbach89e2aa62011-08-16 23:57:34 +00005276 Inst.setOpcode(ARM::tADDi3);
Jim Grosbach83ec8772011-11-10 23:42:14 +00005277 return true;
5278 }
Jim Grosbach89e2aa62011-08-16 23:57:34 +00005279 break;
Jim Grosbachf67e8552011-09-16 22:58:42 +00005280 case ARM::tSUBi8:
5281 // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
5282 // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
5283 // to encoding T2 if <Rd> is specified and encoding T2 is preferred
5284 // to encoding T1 if <Rd> is omitted."
Jim Grosbach83ec8772011-11-10 23:42:14 +00005285 if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
Jim Grosbachf67e8552011-09-16 22:58:42 +00005286 Inst.setOpcode(ARM::tSUBi3);
Jim Grosbach83ec8772011-11-10 23:42:14 +00005287 return true;
5288 }
Jim Grosbachf67e8552011-09-16 22:58:42 +00005289 break;
Jim Grosbach927b9df2011-12-05 22:16:39 +00005290 case ARM::t2ADDrr: {
5291 // If the destination and first source operand are the same, and
5292 // there's no setting of the flags, use encoding T2 instead of T3.
5293 // Note that this is only for ADD, not SUB. This mirrors the system
5294 // 'as' behaviour. Make sure the wide encoding wasn't explicit.
5295 if (Inst.getOperand(0).getReg() != Inst.getOperand(1).getReg() ||
5296 Inst.getOperand(5).getReg() != 0 ||
Jim Grosbach713c7022011-12-05 22:27:04 +00005297 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
5298 static_cast<ARMOperand*>(Operands[3])->getToken() == ".w"))
Jim Grosbach927b9df2011-12-05 22:16:39 +00005299 break;
5300 MCInst TmpInst;
5301 TmpInst.setOpcode(ARM::tADDhirr);
5302 TmpInst.addOperand(Inst.getOperand(0));
5303 TmpInst.addOperand(Inst.getOperand(0));
5304 TmpInst.addOperand(Inst.getOperand(2));
5305 TmpInst.addOperand(Inst.getOperand(3));
5306 TmpInst.addOperand(Inst.getOperand(4));
5307 Inst = TmpInst;
5308 return true;
5309 }
Owen Anderson51f6a7a2011-09-09 21:48:23 +00005310 case ARM::tB:
5311 // A Thumb conditional branch outside of an IT block is a tBcc.
Jim Grosbach83ec8772011-11-10 23:42:14 +00005312 if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()) {
Owen Anderson51f6a7a2011-09-09 21:48:23 +00005313 Inst.setOpcode(ARM::tBcc);
Jim Grosbach83ec8772011-11-10 23:42:14 +00005314 return true;
5315 }
Owen Anderson51f6a7a2011-09-09 21:48:23 +00005316 break;
5317 case ARM::t2B:
5318 // A Thumb2 conditional branch outside of an IT block is a t2Bcc.
Jim Grosbach83ec8772011-11-10 23:42:14 +00005319 if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()){
Owen Anderson51f6a7a2011-09-09 21:48:23 +00005320 Inst.setOpcode(ARM::t2Bcc);
Jim Grosbach83ec8772011-11-10 23:42:14 +00005321 return true;
5322 }
Owen Anderson51f6a7a2011-09-09 21:48:23 +00005323 break;
Jim Grosbachc0755102011-08-31 21:17:31 +00005324 case ARM::t2Bcc:
Jim Grosbacha1109882011-09-02 23:22:08 +00005325 // If the conditional is AL or we're in an IT block, we really want t2B.
Jim Grosbach83ec8772011-11-10 23:42:14 +00005326 if (Inst.getOperand(1).getImm() == ARMCC::AL || inITBlock()) {
Jim Grosbachc0755102011-08-31 21:17:31 +00005327 Inst.setOpcode(ARM::t2B);
Jim Grosbach83ec8772011-11-10 23:42:14 +00005328 return true;
5329 }
Jim Grosbachc0755102011-08-31 21:17:31 +00005330 break;
Jim Grosbach395b4532011-08-17 22:57:40 +00005331 case ARM::tBcc:
5332 // If the conditional is AL, we really want tB.
Jim Grosbach83ec8772011-11-10 23:42:14 +00005333 if (Inst.getOperand(1).getImm() == ARMCC::AL) {
Jim Grosbach395b4532011-08-17 22:57:40 +00005334 Inst.setOpcode(ARM::tB);
Jim Grosbach83ec8772011-11-10 23:42:14 +00005335 return true;
5336 }
Jim Grosbach3ce23d32011-08-18 16:08:39 +00005337 break;
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00005338 case ARM::tLDMIA: {
5339 // If the register list contains any high registers, or if the writeback
5340 // doesn't match what tLDMIA can do, we need to use the 32-bit encoding
5341 // instead if we're in Thumb2. Otherwise, this should have generated
5342 // an error in validateInstruction().
5343 unsigned Rn = Inst.getOperand(0).getReg();
5344 bool hasWritebackToken =
5345 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
5346 static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
5347 bool listContainsBase;
5348 if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) ||
5349 (!listContainsBase && !hasWritebackToken) ||
5350 (listContainsBase && hasWritebackToken)) {
5351 // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
5352 assert (isThumbTwo());
5353 Inst.setOpcode(hasWritebackToken ? ARM::t2LDMIA_UPD : ARM::t2LDMIA);
5354 // If we're switching to the updating version, we need to insert
5355 // the writeback tied operand.
5356 if (hasWritebackToken)
5357 Inst.insert(Inst.begin(),
5358 MCOperand::CreateReg(Inst.getOperand(0).getReg()));
Jim Grosbach83ec8772011-11-10 23:42:14 +00005359 return true;
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00005360 }
5361 break;
5362 }
Jim Grosbach8213c962011-09-16 20:50:13 +00005363 case ARM::tSTMIA_UPD: {
5364 // If the register list contains any high registers, we need to use
5365 // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
5366 // should have generated an error in validateInstruction().
5367 unsigned Rn = Inst.getOperand(0).getReg();
5368 bool listContainsBase;
5369 if (checkLowRegisterList(Inst, 4, Rn, 0, listContainsBase)) {
5370 // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
5371 assert (isThumbTwo());
5372 Inst.setOpcode(ARM::t2STMIA_UPD);
Jim Grosbach83ec8772011-11-10 23:42:14 +00005373 return true;
Jim Grosbach8213c962011-09-16 20:50:13 +00005374 }
5375 break;
5376 }
Jim Grosbach54026372011-11-10 23:17:11 +00005377 case ARM::tPOP: {
5378 bool listContainsBase;
5379 // If the register list contains any high registers, we need to use
5380 // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
5381 // should have generated an error in validateInstruction().
5382 if (!checkLowRegisterList(Inst, 2, 0, ARM::PC, listContainsBase))
Jim Grosbach83ec8772011-11-10 23:42:14 +00005383 return false;
Jim Grosbach54026372011-11-10 23:17:11 +00005384 assert (isThumbTwo());
5385 Inst.setOpcode(ARM::t2LDMIA_UPD);
5386 // Add the base register and writeback operands.
5387 Inst.insert(Inst.begin(), MCOperand::CreateReg(ARM::SP));
5388 Inst.insert(Inst.begin(), MCOperand::CreateReg(ARM::SP));
Jim Grosbach83ec8772011-11-10 23:42:14 +00005389 return true;
Jim Grosbach54026372011-11-10 23:17:11 +00005390 }
5391 case ARM::tPUSH: {
5392 bool listContainsBase;
5393 if (!checkLowRegisterList(Inst, 2, 0, ARM::LR, listContainsBase))
Jim Grosbach83ec8772011-11-10 23:42:14 +00005394 return false;
Jim Grosbach54026372011-11-10 23:17:11 +00005395 assert (isThumbTwo());
5396 Inst.setOpcode(ARM::t2STMDB_UPD);
5397 // Add the base register and writeback operands.
5398 Inst.insert(Inst.begin(), MCOperand::CreateReg(ARM::SP));
5399 Inst.insert(Inst.begin(), MCOperand::CreateReg(ARM::SP));
Jim Grosbach83ec8772011-11-10 23:42:14 +00005400 return true;
Jim Grosbach54026372011-11-10 23:17:11 +00005401 }
Jim Grosbach1ad60c22011-09-10 00:15:36 +00005402 case ARM::t2MOVi: {
5403 // If we can use the 16-bit encoding and the user didn't explicitly
5404 // request the 32-bit variant, transform it here.
5405 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
5406 Inst.getOperand(1).getImm() <= 255 &&
Jim Grosbachc2d31642011-09-14 19:12:11 +00005407 ((!inITBlock() && Inst.getOperand(2).getImm() == ARMCC::AL &&
5408 Inst.getOperand(4).getReg() == ARM::CPSR) ||
5409 (inITBlock() && Inst.getOperand(4).getReg() == 0)) &&
Jim Grosbach1ad60c22011-09-10 00:15:36 +00005410 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
5411 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
5412 // The operands aren't in the same order for tMOVi8...
5413 MCInst TmpInst;
5414 TmpInst.setOpcode(ARM::tMOVi8);
5415 TmpInst.addOperand(Inst.getOperand(0));
5416 TmpInst.addOperand(Inst.getOperand(4));
5417 TmpInst.addOperand(Inst.getOperand(1));
5418 TmpInst.addOperand(Inst.getOperand(2));
5419 TmpInst.addOperand(Inst.getOperand(3));
5420 Inst = TmpInst;
Jim Grosbach83ec8772011-11-10 23:42:14 +00005421 return true;
Jim Grosbach1ad60c22011-09-10 00:15:36 +00005422 }
5423 break;
5424 }
5425 case ARM::t2MOVr: {
5426 // If we can use the 16-bit encoding and the user didn't explicitly
5427 // request the 32-bit variant, transform it here.
5428 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
5429 isARMLowRegister(Inst.getOperand(1).getReg()) &&
5430 Inst.getOperand(2).getImm() == ARMCC::AL &&
5431 Inst.getOperand(4).getReg() == ARM::CPSR &&
5432 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
5433 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
5434 // The operands aren't the same for tMOV[S]r... (no cc_out)
5435 MCInst TmpInst;
5436 TmpInst.setOpcode(Inst.getOperand(4).getReg() ? ARM::tMOVSr : ARM::tMOVr);
5437 TmpInst.addOperand(Inst.getOperand(0));
5438 TmpInst.addOperand(Inst.getOperand(1));
5439 TmpInst.addOperand(Inst.getOperand(2));
5440 TmpInst.addOperand(Inst.getOperand(3));
5441 Inst = TmpInst;
Jim Grosbach83ec8772011-11-10 23:42:14 +00005442 return true;
Jim Grosbach1ad60c22011-09-10 00:15:36 +00005443 }
5444 break;
5445 }
Jim Grosbach326efe52011-09-19 20:29:33 +00005446 case ARM::t2SXTH:
Jim Grosbach50f1c372011-09-20 00:46:54 +00005447 case ARM::t2SXTB:
5448 case ARM::t2UXTH:
5449 case ARM::t2UXTB: {
Jim Grosbach326efe52011-09-19 20:29:33 +00005450 // If we can use the 16-bit encoding and the user didn't explicitly
5451 // request the 32-bit variant, transform it here.
5452 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
5453 isARMLowRegister(Inst.getOperand(1).getReg()) &&
5454 Inst.getOperand(2).getImm() == 0 &&
5455 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
5456 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
Jim Grosbach50f1c372011-09-20 00:46:54 +00005457 unsigned NewOpc;
5458 switch (Inst.getOpcode()) {
5459 default: llvm_unreachable("Illegal opcode!");
5460 case ARM::t2SXTH: NewOpc = ARM::tSXTH; break;
5461 case ARM::t2SXTB: NewOpc = ARM::tSXTB; break;
5462 case ARM::t2UXTH: NewOpc = ARM::tUXTH; break;
5463 case ARM::t2UXTB: NewOpc = ARM::tUXTB; break;
5464 }
Jim Grosbach326efe52011-09-19 20:29:33 +00005465 // The operands aren't the same for thumb1 (no rotate operand).
5466 MCInst TmpInst;
5467 TmpInst.setOpcode(NewOpc);
5468 TmpInst.addOperand(Inst.getOperand(0));
5469 TmpInst.addOperand(Inst.getOperand(1));
5470 TmpInst.addOperand(Inst.getOperand(3));
5471 TmpInst.addOperand(Inst.getOperand(4));
5472 Inst = TmpInst;
Jim Grosbach83ec8772011-11-10 23:42:14 +00005473 return true;
Jim Grosbach326efe52011-09-19 20:29:33 +00005474 }
5475 break;
5476 }
Jim Grosbach89df9962011-08-26 21:43:41 +00005477 case ARM::t2IT: {
5478 // The mask bits for all but the first condition are represented as
5479 // the low bit of the condition code value implies 't'. We currently
5480 // always have 1 implies 't', so XOR toggle the bits if the low bit
5481 // of the condition code is zero. The encoding also expects the low
5482 // bit of the condition to be encoded as bit 4 of the mask operand,
5483 // so mask that in if needed
5484 MCOperand &MO = Inst.getOperand(1);
5485 unsigned Mask = MO.getImm();
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00005486 unsigned OrigMask = Mask;
5487 unsigned TZ = CountTrailingZeros_32(Mask);
Jim Grosbach89df9962011-08-26 21:43:41 +00005488 if ((Inst.getOperand(0).getImm() & 1) == 0) {
Jim Grosbach89df9962011-08-26 21:43:41 +00005489 assert(Mask && TZ <= 3 && "illegal IT mask value!");
5490 for (unsigned i = 3; i != TZ; --i)
5491 Mask ^= 1 << i;
5492 } else
5493 Mask |= 0x10;
5494 MO.setImm(Mask);
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00005495
5496 // Set up the IT block state according to the IT instruction we just
5497 // matched.
5498 assert(!inITBlock() && "nested IT blocks?!");
5499 ITState.Cond = ARMCC::CondCodes(Inst.getOperand(0).getImm());
5500 ITState.Mask = OrigMask; // Use the original mask, not the updated one.
5501 ITState.CurPosition = 0;
5502 ITState.FirstCond = true;
Jim Grosbach89df9962011-08-26 21:43:41 +00005503 break;
5504 }
Jim Grosbachf8fce712011-08-11 17:35:48 +00005505 }
Jim Grosbach83ec8772011-11-10 23:42:14 +00005506 return false;
Jim Grosbachf8fce712011-08-11 17:35:48 +00005507}
5508
Jim Grosbach47a0d522011-08-16 20:45:50 +00005509unsigned ARMAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
5510 // 16-bit thumb arithmetic instructions either require or preclude the 'S'
5511 // suffix depending on whether they're in an IT block or not.
Jim Grosbach194bd892011-08-16 22:20:01 +00005512 unsigned Opc = Inst.getOpcode();
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00005513 const MCInstrDesc &MCID = getInstDesc(Opc);
Jim Grosbach47a0d522011-08-16 20:45:50 +00005514 if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
5515 assert(MCID.hasOptionalDef() &&
5516 "optionally flag setting instruction missing optional def operand");
5517 assert(MCID.NumOperands == Inst.getNumOperands() &&
5518 "operand count mismatch!");
5519 // Find the optional-def operand (cc_out).
5520 unsigned OpNo;
5521 for (OpNo = 0;
5522 !MCID.OpInfo[OpNo].isOptionalDef() && OpNo < MCID.NumOperands;
5523 ++OpNo)
5524 ;
5525 // If we're parsing Thumb1, reject it completely.
5526 if (isThumbOne() && Inst.getOperand(OpNo).getReg() != ARM::CPSR)
5527 return Match_MnemonicFail;
5528 // If we're parsing Thumb2, which form is legal depends on whether we're
5529 // in an IT block.
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00005530 if (isThumbTwo() && Inst.getOperand(OpNo).getReg() != ARM::CPSR &&
5531 !inITBlock())
Jim Grosbach47a0d522011-08-16 20:45:50 +00005532 return Match_RequiresITBlock;
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00005533 if (isThumbTwo() && Inst.getOperand(OpNo).getReg() == ARM::CPSR &&
5534 inITBlock())
5535 return Match_RequiresNotITBlock;
Jim Grosbach47a0d522011-08-16 20:45:50 +00005536 }
Jim Grosbach194bd892011-08-16 22:20:01 +00005537 // Some high-register supporting Thumb1 encodings only allow both registers
5538 // to be from r0-r7 when in Thumb2.
5539 else if (Opc == ARM::tADDhirr && isThumbOne() &&
5540 isARMLowRegister(Inst.getOperand(1).getReg()) &&
5541 isARMLowRegister(Inst.getOperand(2).getReg()))
5542 return Match_RequiresThumb2;
5543 // Others only require ARMv6 or later.
Jim Grosbach4ec6e882011-08-19 20:46:54 +00005544 else if (Opc == ARM::tMOVr && isThumbOne() && !hasV6Ops() &&
Jim Grosbach194bd892011-08-16 22:20:01 +00005545 isARMLowRegister(Inst.getOperand(0).getReg()) &&
5546 isARMLowRegister(Inst.getOperand(1).getReg()))
5547 return Match_RequiresV6;
Jim Grosbach47a0d522011-08-16 20:45:50 +00005548 return Match_Success;
5549}
5550
Chris Lattnerfa42fad2010-10-28 21:28:01 +00005551bool ARMAsmParser::
5552MatchAndEmitInstruction(SMLoc IDLoc,
5553 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
5554 MCStreamer &Out) {
5555 MCInst Inst;
5556 unsigned ErrorInfo;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00005557 unsigned MatchResult;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00005558 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00005559 switch (MatchResult) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00005560 default: break;
Chris Lattnere73d4f82010-10-28 21:41:58 +00005561 case Match_Success:
Jim Grosbach189610f2011-07-26 18:25:39 +00005562 // Context sensitive operand constraints aren't handled by the matcher,
5563 // so check them here.
Jim Grosbacha1109882011-09-02 23:22:08 +00005564 if (validateInstruction(Inst, Operands)) {
5565 // Still progress the IT block, otherwise one wrong condition causes
5566 // nasty cascading errors.
5567 forwardITPosition();
Jim Grosbach189610f2011-07-26 18:25:39 +00005568 return true;
Jim Grosbacha1109882011-09-02 23:22:08 +00005569 }
Jim Grosbach189610f2011-07-26 18:25:39 +00005570
Jim Grosbachf8fce712011-08-11 17:35:48 +00005571 // Some instructions need post-processing to, for example, tweak which
Jim Grosbach83ec8772011-11-10 23:42:14 +00005572 // encoding is selected. Loop on it while changes happen so the
5573 // individual transformations can chain off each other. E.g.,
5574 // tPOP(r8)->t2LDMIA_UPD(sp,r8)->t2STR_POST(sp,r8)
5575 while (processInstruction(Inst, Operands))
5576 ;
Jim Grosbachf8fce712011-08-11 17:35:48 +00005577
Jim Grosbacha1109882011-09-02 23:22:08 +00005578 // Only move forward at the very end so that everything in validate
5579 // and process gets a consistent answer about whether we're in an IT
5580 // block.
5581 forwardITPosition();
5582
Chris Lattnerfa42fad2010-10-28 21:28:01 +00005583 Out.EmitInstruction(Inst);
5584 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00005585 case Match_MissingFeature:
5586 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
5587 return true;
5588 case Match_InvalidOperand: {
5589 SMLoc ErrorLoc = IDLoc;
5590 if (ErrorInfo != ~0U) {
5591 if (ErrorInfo >= Operands.size())
5592 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00005593
Chris Lattnere73d4f82010-10-28 21:41:58 +00005594 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
5595 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
5596 }
Jim Grosbach16c74252010-10-29 14:46:02 +00005597
Chris Lattnere73d4f82010-10-28 21:41:58 +00005598 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00005599 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00005600 case Match_MnemonicFail:
Jim Grosbach47a0d522011-08-16 20:45:50 +00005601 return Error(IDLoc, "invalid instruction");
Daniel Dunbarb4129152011-02-04 17:12:23 +00005602 case Match_ConversionFail:
Jim Grosbach88ae2bc2011-08-19 22:07:46 +00005603 // The converter function will have already emited a diagnostic.
5604 return true;
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00005605 case Match_RequiresNotITBlock:
5606 return Error(IDLoc, "flag setting instruction only valid outside IT block");
Jim Grosbach47a0d522011-08-16 20:45:50 +00005607 case Match_RequiresITBlock:
5608 return Error(IDLoc, "instruction only valid inside IT block");
Jim Grosbach194bd892011-08-16 22:20:01 +00005609 case Match_RequiresV6:
5610 return Error(IDLoc, "instruction variant requires ARMv6 or later");
5611 case Match_RequiresThumb2:
5612 return Error(IDLoc, "instruction variant requires Thumb2");
Chris Lattnere73d4f82010-10-28 21:41:58 +00005613 }
Jim Grosbach16c74252010-10-29 14:46:02 +00005614
Eric Christopherc223e2b2010-10-29 09:26:59 +00005615 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00005616 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00005617}
5618
Jim Grosbach1355cf12011-07-26 17:10:22 +00005619/// parseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00005620bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
5621 StringRef IDVal = DirectiveID.getIdentifier();
5622 if (IDVal == ".word")
Jim Grosbach1355cf12011-07-26 17:10:22 +00005623 return parseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00005624 else if (IDVal == ".thumb")
Jim Grosbach1355cf12011-07-26 17:10:22 +00005625 return parseDirectiveThumb(DirectiveID.getLoc());
Jim Grosbach9a70df92011-12-07 18:04:19 +00005626 else if (IDVal == ".arm")
5627 return parseDirectiveARM(DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00005628 else if (IDVal == ".thumb_func")
Jim Grosbach1355cf12011-07-26 17:10:22 +00005629 return parseDirectiveThumbFunc(DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00005630 else if (IDVal == ".code")
Jim Grosbach1355cf12011-07-26 17:10:22 +00005631 return parseDirectiveCode(DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00005632 else if (IDVal == ".syntax")
Jim Grosbach1355cf12011-07-26 17:10:22 +00005633 return parseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00005634 return true;
5635}
5636
Jim Grosbach1355cf12011-07-26 17:10:22 +00005637/// parseDirectiveWord
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00005638/// ::= .word [ expression (, expression)* ]
Jim Grosbach1355cf12011-07-26 17:10:22 +00005639bool ARMAsmParser::parseDirectiveWord(unsigned Size, SMLoc L) {
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00005640 if (getLexer().isNot(AsmToken::EndOfStatement)) {
5641 for (;;) {
5642 const MCExpr *Value;
5643 if (getParser().ParseExpression(Value))
5644 return true;
5645
Chris Lattneraaec2052010-01-19 19:46:13 +00005646 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00005647
5648 if (getLexer().is(AsmToken::EndOfStatement))
5649 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00005650
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00005651 // FIXME: Improve diagnostic.
5652 if (getLexer().isNot(AsmToken::Comma))
5653 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00005654 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00005655 }
5656 }
5657
Sean Callananb9a25b72010-01-19 20:27:46 +00005658 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00005659 return false;
5660}
5661
Jim Grosbach1355cf12011-07-26 17:10:22 +00005662/// parseDirectiveThumb
Kevin Enderby515d5092009-10-15 20:48:48 +00005663/// ::= .thumb
Jim Grosbach1355cf12011-07-26 17:10:22 +00005664bool ARMAsmParser::parseDirectiveThumb(SMLoc L) {
Kevin Enderby515d5092009-10-15 20:48:48 +00005665 if (getLexer().isNot(AsmToken::EndOfStatement))
5666 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00005667 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00005668
Jim Grosbach9a70df92011-12-07 18:04:19 +00005669 if (!isThumb())
5670 SwitchMode();
5671 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
5672 return false;
5673}
5674
5675/// parseDirectiveARM
5676/// ::= .arm
5677bool ARMAsmParser::parseDirectiveARM(SMLoc L) {
5678 if (getLexer().isNot(AsmToken::EndOfStatement))
5679 return Error(L, "unexpected token in directive");
5680 Parser.Lex();
5681
5682 if (isThumb())
5683 SwitchMode();
5684 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderby515d5092009-10-15 20:48:48 +00005685 return false;
5686}
5687
Jim Grosbach1355cf12011-07-26 17:10:22 +00005688/// parseDirectiveThumbFunc
Kevin Enderby515d5092009-10-15 20:48:48 +00005689/// ::= .thumbfunc symbol_name
Jim Grosbach1355cf12011-07-26 17:10:22 +00005690bool ARMAsmParser::parseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00005691 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
5692 bool isMachO = MAI.hasSubsectionsViaSymbols();
5693 StringRef Name;
5694
5695 // Darwin asm has function name after .thumb_func direction
5696 // ELF doesn't
5697 if (isMachO) {
5698 const AsmToken &Tok = Parser.getTok();
5699 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
5700 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbachd475f862011-11-10 20:48:53 +00005701 Name = Tok.getIdentifier();
Rafael Espindola64695402011-05-16 16:17:21 +00005702 Parser.Lex(); // Consume the identifier token.
5703 }
5704
Jim Grosbachd475f862011-11-10 20:48:53 +00005705 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby515d5092009-10-15 20:48:48 +00005706 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00005707 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00005708
Rafael Espindola64695402011-05-16 16:17:21 +00005709 // FIXME: assuming function name will be the line following .thumb_func
5710 if (!isMachO) {
Jim Grosbachd475f862011-11-10 20:48:53 +00005711 Name = Parser.getTok().getIdentifier();
Rafael Espindola64695402011-05-16 16:17:21 +00005712 }
5713
Jim Grosbach642fc9c2010-11-05 22:33:53 +00005714 // Mark symbol as a thumb symbol.
5715 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
5716 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00005717 return false;
5718}
5719
Jim Grosbach1355cf12011-07-26 17:10:22 +00005720/// parseDirectiveSyntax
Kevin Enderby515d5092009-10-15 20:48:48 +00005721/// ::= .syntax unified | divided
Jim Grosbach1355cf12011-07-26 17:10:22 +00005722bool ARMAsmParser::parseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00005723 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00005724 if (Tok.isNot(AsmToken::Identifier))
5725 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00005726 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00005727 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00005728 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00005729 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00005730 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00005731 else
5732 return Error(L, "unrecognized syntax mode in .syntax directive");
5733
5734 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00005735 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00005736 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00005737
5738 // TODO tell the MC streamer the mode
5739 // getParser().getStreamer().Emit???();
5740 return false;
5741}
5742
Jim Grosbach1355cf12011-07-26 17:10:22 +00005743/// parseDirectiveCode
Kevin Enderby515d5092009-10-15 20:48:48 +00005744/// ::= .code 16 | 32
Jim Grosbach1355cf12011-07-26 17:10:22 +00005745bool ARMAsmParser::parseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00005746 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00005747 if (Tok.isNot(AsmToken::Integer))
5748 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00005749 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00005750 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00005751 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00005752 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00005753 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00005754 else
5755 return Error(L, "invalid operand to .code directive");
5756
5757 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00005758 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00005759 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00005760
Evan Cheng32869202011-07-08 22:36:29 +00005761 if (Val == 16) {
Jim Grosbach98447da2011-09-06 18:46:23 +00005762 if (!isThumb())
Evan Chengffc0e732011-07-09 05:47:46 +00005763 SwitchMode();
Jim Grosbach98447da2011-09-06 18:46:23 +00005764 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Evan Cheng32869202011-07-08 22:36:29 +00005765 } else {
Jim Grosbach98447da2011-09-06 18:46:23 +00005766 if (isThumb())
Evan Chengffc0e732011-07-09 05:47:46 +00005767 SwitchMode();
Jim Grosbach98447da2011-09-06 18:46:23 +00005768 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Evan Chengeb0caa12011-07-08 22:49:55 +00005769 }
Jim Grosbach2a301702010-11-05 22:40:53 +00005770
Kevin Enderby515d5092009-10-15 20:48:48 +00005771 return false;
5772}
5773
Sean Callanan90b70972010-04-07 20:29:34 +00005774extern "C" void LLVMInitializeARMAsmLexer();
5775
Kevin Enderby9c41fa82009-10-30 22:55:57 +00005776/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00005777extern "C" void LLVMInitializeARMAsmParser() {
Evan Cheng94b95502011-07-26 00:24:13 +00005778 RegisterMCAsmParser<ARMAsmParser> X(TheARMTarget);
5779 RegisterMCAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00005780 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00005781}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00005782
Chris Lattner0692ee62010-09-06 19:11:01 +00005783#define GET_REGISTER_MATCHER
5784#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00005785#include "ARMGenAsmMatcher.inc"