blob: 3a60ff94d9bd90316eaf3e426068e189b6dc9273 [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"
Owen Anderson0c9f2502011-01-13 22:50:36 +000033#include "llvm/ADT/StringExtras.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000034#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000035#include "llvm/ADT/Twine.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000036
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000037using namespace llvm;
38
Chris Lattner3a697562010-10-28 17:20:03 +000039namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000040
41class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000042
Evan Cheng94b95502011-07-26 00:24:13 +000043class ARMAsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000044 MCSubtargetInfo &STI;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000045 MCAsmParser &Parser;
46
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +000047 struct {
48 ARMCC::CondCodes Cond; // Condition for IT block.
49 unsigned Mask:4; // Condition mask for instructions.
50 // Starting at first 1 (from lsb).
51 // '1' condition as indicated in IT.
52 // '0' inverse of condition (else).
53 // Count of instructions in IT block is
54 // 4 - trailingzeroes(mask)
55
56 bool FirstCond; // Explicit flag for when we're parsing the
57 // First instruction in the IT block. It's
58 // implied in the mask, so needs special
59 // handling.
60
61 unsigned CurPosition; // Current position in parsing of IT
62 // block. In range [0,3]. Initialized
63 // according to count of instructions in block.
64 // ~0U if no active IT block.
65 } ITState;
66 bool inITBlock() { return ITState.CurPosition != ~0U;}
Jim Grosbacha1109882011-09-02 23:22:08 +000067 void forwardITPosition() {
68 if (!inITBlock()) return;
69 // Move to the next instruction in the IT block, if there is one. If not,
70 // mark the block as done.
71 unsigned TZ = CountTrailingZeros_32(ITState.Mask);
72 if (++ITState.CurPosition == 5 - TZ)
73 ITState.CurPosition = ~0U; // Done with the IT block after this.
74 }
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +000075
76
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000077 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000078 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
79
80 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000081 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
82
Jim Grosbach1355cf12011-07-26 17:10:22 +000083 int tryParseRegister();
84 bool tryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach0d87ec22011-07-26 20:41:24 +000085 int tryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach1355cf12011-07-26 17:10:22 +000086 bool parseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach7ce05792011-08-03 23:50:40 +000087 bool parseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach1355cf12011-07-26 17:10:22 +000088 bool parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
89 bool parsePrefix(ARMMCExpr::VariantKind &RefKind);
Jim Grosbach7ce05792011-08-03 23:50:40 +000090 bool parseMemRegOffsetShift(ARM_AM::ShiftOpc &ShiftType,
91 unsigned &ShiftAmount);
Jim Grosbach1355cf12011-07-26 17:10:22 +000092 bool parseDirectiveWord(unsigned Size, SMLoc L);
93 bool parseDirectiveThumb(SMLoc L);
94 bool parseDirectiveThumbFunc(SMLoc L);
95 bool parseDirectiveCode(SMLoc L);
96 bool parseDirectiveSyntax(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000097
Jim Grosbach1355cf12011-07-26 17:10:22 +000098 StringRef splitMnemonic(StringRef Mnemonic, unsigned &PredicationCode,
Jim Grosbach89df9962011-08-26 21:43:41 +000099 bool &CarrySetting, unsigned &ProcessorIMod,
100 StringRef &ITMask);
Jim Grosbach1355cf12011-07-26 17:10:22 +0000101 void getMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +0000102 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +0000103
Evan Chengebdeeab2011-07-08 01:53:10 +0000104 bool isThumb() const {
105 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +0000106 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +0000107 }
Evan Chengebdeeab2011-07-08 01:53:10 +0000108 bool isThumbOne() const {
Evan Chengffc0e732011-07-09 05:47:46 +0000109 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) == 0;
Evan Chengebdeeab2011-07-08 01:53:10 +0000110 }
Jim Grosbach47a0d522011-08-16 20:45:50 +0000111 bool isThumbTwo() const {
112 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2);
113 }
Jim Grosbach194bd892011-08-16 22:20:01 +0000114 bool hasV6Ops() const {
115 return STI.getFeatureBits() & ARM::HasV6Ops;
116 }
James Molloyacad68d2011-09-28 14:21:38 +0000117 bool hasV7Ops() const {
118 return STI.getFeatureBits() & ARM::HasV7Ops;
119 }
Evan Cheng32869202011-07-08 22:36:29 +0000120 void SwitchMode() {
Evan Chengffc0e732011-07-09 05:47:46 +0000121 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
122 setAvailableFeatures(FB);
Evan Cheng32869202011-07-08 22:36:29 +0000123 }
James Molloyacad68d2011-09-28 14:21:38 +0000124 bool isMClass() const {
125 return STI.getFeatureBits() & ARM::FeatureMClass;
126 }
Evan Chengebdeeab2011-07-08 01:53:10 +0000127
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000128 /// @name Auto-generated Match Functions
129 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000130
Chris Lattner0692ee62010-09-06 19:11:01 +0000131#define GET_ASSEMBLER_HEADER
132#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000133
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000134 /// }
135
Jim Grosbach89df9962011-08-26 21:43:41 +0000136 OperandMatchResultTy parseITCondCode(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach43904292011-07-25 20:14:50 +0000137 OperandMatchResultTy parseCoprocNumOperand(
Jim Grosbachf922c472011-02-12 01:34:40 +0000138 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach43904292011-07-25 20:14:50 +0000139 OperandMatchResultTy parseCoprocRegOperand(
Jim Grosbachf922c472011-02-12 01:34:40 +0000140 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach9b8f2a02011-10-12 17:34:41 +0000141 OperandMatchResultTy parseCoprocOptionOperand(
142 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach43904292011-07-25 20:14:50 +0000143 OperandMatchResultTy parseMemBarrierOptOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000144 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach43904292011-07-25 20:14:50 +0000145 OperandMatchResultTy parseProcIFlagsOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000146 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach43904292011-07-25 20:14:50 +0000147 OperandMatchResultTy parseMSRMaskOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000148 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbachf6c05252011-07-21 17:23:04 +0000149 OperandMatchResultTy parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &O,
150 StringRef Op, int Low, int High);
151 OperandMatchResultTy parsePKHLSLImm(SmallVectorImpl<MCParsedAsmOperand*> &O) {
152 return parsePKHImm(O, "lsl", 0, 31);
153 }
154 OperandMatchResultTy parsePKHASRImm(SmallVectorImpl<MCParsedAsmOperand*> &O) {
155 return parsePKHImm(O, "asr", 1, 32);
156 }
Jim Grosbachc27d4f92011-07-22 17:44:50 +0000157 OperandMatchResultTy parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach580f4a92011-07-25 22:20:28 +0000158 OperandMatchResultTy parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach7e1547e2011-07-27 20:15:40 +0000159 OperandMatchResultTy parseRotImm(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach293a2ee2011-07-28 21:34:26 +0000160 OperandMatchResultTy parseBitfield(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach7ce05792011-08-03 23:50:40 +0000161 OperandMatchResultTy parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach251bf252011-08-10 21:56:18 +0000162 OperandMatchResultTy parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach9d390362011-10-03 23:38:36 +0000163 OperandMatchResultTy parseFPImm(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach862019c2011-10-18 23:02:30 +0000164 OperandMatchResultTy parseVectorList(SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000165
166 // Asm Match Converter Methods
Jim Grosbacha77295d2011-09-08 22:07:06 +0000167 bool cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
168 const SmallVectorImpl<MCParsedAsmOperand*> &);
169 bool cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
170 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbacheeec0252011-09-08 00:39:19 +0000171 bool cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
172 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbachee2c2a42011-09-16 21:55:56 +0000173 bool cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
174 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach1355cf12011-07-26 17:10:22 +0000175 bool cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000176 const SmallVectorImpl<MCParsedAsmOperand*> &);
Owen Anderson9ab0f252011-08-26 20:43:14 +0000177 bool cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
178 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach548340c2011-08-11 19:22:40 +0000179 bool cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
180 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach1355cf12011-07-26 17:10:22 +0000181 bool cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000182 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach7b8f46c2011-08-11 21:17:22 +0000183 bool cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
184 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach7ce05792011-08-03 23:50:40 +0000185 bool cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
186 const SmallVectorImpl<MCParsedAsmOperand*> &);
187 bool cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
188 const SmallVectorImpl<MCParsedAsmOperand*> &);
189 bool cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
190 const SmallVectorImpl<MCParsedAsmOperand*> &);
191 bool cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
192 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000193 bool cvtLdrdPre(MCInst &Inst, unsigned Opcode,
194 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach14605d12011-08-11 20:28:23 +0000195 bool cvtStrdPre(MCInst &Inst, unsigned Opcode,
196 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach623a4542011-08-10 22:42:16 +0000197 bool cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
198 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach88ae2bc2011-08-19 22:07:46 +0000199 bool cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
200 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach189610f2011-07-26 18:25:39 +0000201
202 bool validateInstruction(MCInst &Inst,
203 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
Jim Grosbachf8fce712011-08-11 17:35:48 +0000204 void processInstruction(MCInst &Inst,
205 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
Jim Grosbachd54b4e62011-08-16 21:12:37 +0000206 bool shouldOmitCCOutOperand(StringRef Mnemonic,
207 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Jim Grosbach189610f2011-07-26 18:25:39 +0000208
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000209public:
Jim Grosbach47a0d522011-08-16 20:45:50 +0000210 enum ARMMatchResultTy {
Jim Grosbach194bd892011-08-16 22:20:01 +0000211 Match_RequiresITBlock = FIRST_TARGET_MATCH_RESULT_TY,
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +0000212 Match_RequiresNotITBlock,
Jim Grosbach194bd892011-08-16 22:20:01 +0000213 Match_RequiresV6,
214 Match_RequiresThumb2
Jim Grosbach47a0d522011-08-16 20:45:50 +0000215 };
216
Evan Chengffc0e732011-07-09 05:47:46 +0000217 ARMAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
Evan Cheng94b95502011-07-26 00:24:13 +0000218 : MCTargetAsmParser(), STI(_STI), Parser(_Parser) {
Evan Chengebdeeab2011-07-08 01:53:10 +0000219 MCAsmParserExtension::Initialize(_Parser);
Evan Cheng32869202011-07-08 22:36:29 +0000220
Evan Chengebdeeab2011-07-08 01:53:10 +0000221 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000222 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +0000223
224 // Not in an ITBlock to start with.
225 ITState.CurPosition = ~0U;
Evan Chengebdeeab2011-07-08 01:53:10 +0000226 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000227
Jim Grosbach1355cf12011-07-26 17:10:22 +0000228 // Implementation of the MCTargetAsmParser interface:
229 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
230 bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Jim Grosbach189610f2011-07-26 18:25:39 +0000231 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Jim Grosbach1355cf12011-07-26 17:10:22 +0000232 bool ParseDirective(AsmToken DirectiveID);
233
Jim Grosbach47a0d522011-08-16 20:45:50 +0000234 unsigned checkTargetMatchPredicate(MCInst &Inst);
235
Jim Grosbach1355cf12011-07-26 17:10:22 +0000236 bool MatchAndEmitInstruction(SMLoc IDLoc,
237 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
238 MCStreamer &Out);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000239};
Jim Grosbach16c74252010-10-29 14:46:02 +0000240} // end anonymous namespace
241
Chris Lattner3a697562010-10-28 17:20:03 +0000242namespace {
243
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000244/// ARMOperand - Instances of this class represent a parsed ARM machine
245/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000246class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000247 enum KindTy {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000248 k_CondCode,
249 k_CCOut,
250 k_ITCondMask,
251 k_CoprocNum,
252 k_CoprocReg,
Jim Grosbach9b8f2a02011-10-12 17:34:41 +0000253 k_CoprocOption,
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000254 k_Immediate,
255 k_FPImmediate,
256 k_MemBarrierOpt,
257 k_Memory,
258 k_PostIndexRegister,
259 k_MSRMask,
260 k_ProcIFlags,
Jim Grosbach460a9052011-10-07 23:56:00 +0000261 k_VectorIndex,
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000262 k_Register,
263 k_RegisterList,
264 k_DPRRegisterList,
265 k_SPRRegisterList,
Jim Grosbach862019c2011-10-18 23:02:30 +0000266 k_VectorList,
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000267 k_ShiftedRegister,
268 k_ShiftedImmediate,
269 k_ShifterImmediate,
270 k_RotateImmediate,
271 k_BitfieldDescriptor,
272 k_Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000273 } Kind;
274
Sean Callanan76264762010-04-02 22:27:05 +0000275 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000276 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000277
278 union {
279 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000280 ARMCC::CondCodes Val;
281 } CC;
282
283 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000284 unsigned Val;
285 } Cop;
286
287 struct {
Jim Grosbach9b8f2a02011-10-12 17:34:41 +0000288 unsigned Val;
289 } CoprocOption;
290
291 struct {
Jim Grosbach89df9962011-08-26 21:43:41 +0000292 unsigned Mask:4;
293 } ITMask;
294
295 struct {
296 ARM_MB::MemBOpt Val;
297 } MBOpt;
298
299 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000300 ARM_PROC::IFlags Val;
301 } IFlags;
302
303 struct {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000304 unsigned Val;
305 } MMask;
306
307 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000308 const char *Data;
309 unsigned Length;
310 } Tok;
311
312 struct {
313 unsigned RegNum;
314 } Reg;
315
Jim Grosbach862019c2011-10-18 23:02:30 +0000316 // A vector register list is a sequential list of 1 to 4 registers.
317 struct {
318 unsigned RegNum;
319 unsigned Count;
320 } VectorList;
321
Bill Wendling8155e5b2010-11-06 22:19:43 +0000322 struct {
Jim Grosbach460a9052011-10-07 23:56:00 +0000323 unsigned Val;
324 } VectorIndex;
325
326 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000327 const MCExpr *Val;
328 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000329
Jim Grosbach9d390362011-10-03 23:38:36 +0000330 struct {
331 unsigned Val; // encoded 8-bit representation
332 } FPImm;
333
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000334 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000335 struct {
336 unsigned BaseRegNum;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000337 // Offset is in OffsetReg or OffsetImm. If both are zero, no offset
338 // was specified.
339 const MCConstantExpr *OffsetImm; // Offset immediate value
340 unsigned OffsetRegNum; // Offset register num, when OffsetImm == NULL
341 ARM_AM::ShiftOpc ShiftType; // Shift type for OffsetReg
Jim Grosbach57dcb852011-10-11 17:29:55 +0000342 unsigned ShiftImm; // shift for OffsetReg.
343 unsigned Alignment; // 0 = no alignment specified
344 // n = alignment in bytes (8, 16, or 32)
Jim Grosbach7ce05792011-08-03 23:50:40 +0000345 unsigned isNegative : 1; // Negated OffsetReg? (~'U' bit)
Jim Grosbache53c87b2011-10-11 15:59:20 +0000346 } Memory;
Owen Anderson00828302011-03-18 22:50:18 +0000347
348 struct {
Jim Grosbach7ce05792011-08-03 23:50:40 +0000349 unsigned RegNum;
Jim Grosbachf4fa3d62011-08-05 21:28:30 +0000350 bool isAdd;
351 ARM_AM::ShiftOpc ShiftTy;
352 unsigned ShiftImm;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000353 } PostIdxReg;
354
355 struct {
Jim Grosbach580f4a92011-07-25 22:20:28 +0000356 bool isASR;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000357 unsigned Imm;
Jim Grosbach580f4a92011-07-25 22:20:28 +0000358 } ShifterImm;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000359 struct {
360 ARM_AM::ShiftOpc ShiftTy;
361 unsigned SrcReg;
362 unsigned ShiftReg;
363 unsigned ShiftImm;
Jim Grosbachaf6981f2011-07-25 20:49:51 +0000364 } RegShiftedReg;
Owen Anderson92a20222011-07-21 18:54:16 +0000365 struct {
366 ARM_AM::ShiftOpc ShiftTy;
367 unsigned SrcReg;
368 unsigned ShiftImm;
Jim Grosbachaf6981f2011-07-25 20:49:51 +0000369 } RegShiftedImm;
Jim Grosbach7e1547e2011-07-27 20:15:40 +0000370 struct {
371 unsigned Imm;
372 } RotImm;
Jim Grosbach293a2ee2011-07-28 21:34:26 +0000373 struct {
374 unsigned LSB;
375 unsigned Width;
376 } Bitfield;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000377 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000378
Bill Wendling146018f2010-11-06 21:42:12 +0000379 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
380public:
Sean Callanan76264762010-04-02 22:27:05 +0000381 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
382 Kind = o.Kind;
383 StartLoc = o.StartLoc;
384 EndLoc = o.EndLoc;
385 switch (Kind) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000386 case k_CondCode:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000387 CC = o.CC;
388 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000389 case k_ITCondMask:
Jim Grosbach89df9962011-08-26 21:43:41 +0000390 ITMask = o.ITMask;
391 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000392 case k_Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000393 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000394 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000395 case k_CCOut:
396 case k_Register:
Sean Callanan76264762010-04-02 22:27:05 +0000397 Reg = o.Reg;
398 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000399 case k_RegisterList:
400 case k_DPRRegisterList:
401 case k_SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000402 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000403 break;
Jim Grosbach862019c2011-10-18 23:02:30 +0000404 case k_VectorList:
405 VectorList = o.VectorList;
406 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000407 case k_CoprocNum:
408 case k_CoprocReg:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000409 Cop = o.Cop;
410 break;
Jim Grosbach9b8f2a02011-10-12 17:34:41 +0000411 case k_CoprocOption:
412 CoprocOption = o.CoprocOption;
413 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000414 case k_Immediate:
Sean Callanan76264762010-04-02 22:27:05 +0000415 Imm = o.Imm;
416 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000417 case k_FPImmediate:
Jim Grosbach9d390362011-10-03 23:38:36 +0000418 FPImm = o.FPImm;
419 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000420 case k_MemBarrierOpt:
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000421 MBOpt = o.MBOpt;
422 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000423 case k_Memory:
Jim Grosbache53c87b2011-10-11 15:59:20 +0000424 Memory = o.Memory;
Sean Callanan76264762010-04-02 22:27:05 +0000425 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000426 case k_PostIndexRegister:
Jim Grosbach7ce05792011-08-03 23:50:40 +0000427 PostIdxReg = o.PostIdxReg;
428 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000429 case k_MSRMask:
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000430 MMask = o.MMask;
431 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000432 case k_ProcIFlags:
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000433 IFlags = o.IFlags;
Owen Anderson00828302011-03-18 22:50:18 +0000434 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000435 case k_ShifterImmediate:
Jim Grosbach580f4a92011-07-25 22:20:28 +0000436 ShifterImm = o.ShifterImm;
Owen Anderson00828302011-03-18 22:50:18 +0000437 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000438 case k_ShiftedRegister:
Jim Grosbachaf6981f2011-07-25 20:49:51 +0000439 RegShiftedReg = o.RegShiftedReg;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000440 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000441 case k_ShiftedImmediate:
Jim Grosbachaf6981f2011-07-25 20:49:51 +0000442 RegShiftedImm = o.RegShiftedImm;
Owen Anderson92a20222011-07-21 18:54:16 +0000443 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000444 case k_RotateImmediate:
Jim Grosbach7e1547e2011-07-27 20:15:40 +0000445 RotImm = o.RotImm;
446 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000447 case k_BitfieldDescriptor:
Jim Grosbach293a2ee2011-07-28 21:34:26 +0000448 Bitfield = o.Bitfield;
449 break;
Jim Grosbach460a9052011-10-07 23:56:00 +0000450 case k_VectorIndex:
451 VectorIndex = o.VectorIndex;
452 break;
Sean Callanan76264762010-04-02 22:27:05 +0000453 }
454 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000455
Sean Callanan76264762010-04-02 22:27:05 +0000456 /// getStartLoc - Get the location of the first token of this operand.
457 SMLoc getStartLoc() const { return StartLoc; }
458 /// getEndLoc - Get the location of the last token of this operand.
459 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000460
Daniel Dunbar8462b302010-08-11 06:36:53 +0000461 ARMCC::CondCodes getCondCode() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000462 assert(Kind == k_CondCode && "Invalid access!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000463 return CC.Val;
464 }
465
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000466 unsigned getCoproc() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000467 assert((Kind == k_CoprocNum || Kind == k_CoprocReg) && "Invalid access!");
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000468 return Cop.Val;
469 }
470
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000471 StringRef getToken() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000472 assert(Kind == k_Token && "Invalid access!");
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000473 return StringRef(Tok.Data, Tok.Length);
474 }
475
476 unsigned getReg() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000477 assert((Kind == k_Register || Kind == k_CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000478 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000479 }
480
Bill Wendling5fa22a12010-11-09 23:28:44 +0000481 const SmallVectorImpl<unsigned> &getRegList() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000482 assert((Kind == k_RegisterList || Kind == k_DPRRegisterList ||
483 Kind == k_SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000484 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000485 }
486
Kevin Enderbycfe07242009-10-13 22:19:02 +0000487 const MCExpr *getImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000488 assert(Kind == k_Immediate && "Invalid access!");
Kevin Enderbycfe07242009-10-13 22:19:02 +0000489 return Imm.Val;
490 }
491
Jim Grosbach9d390362011-10-03 23:38:36 +0000492 unsigned getFPImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000493 assert(Kind == k_FPImmediate && "Invalid access!");
Jim Grosbach9d390362011-10-03 23:38:36 +0000494 return FPImm.Val;
495 }
496
Jim Grosbach460a9052011-10-07 23:56:00 +0000497 unsigned getVectorIndex() const {
498 assert(Kind == k_VectorIndex && "Invalid access!");
499 return VectorIndex.Val;
500 }
501
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000502 ARM_MB::MemBOpt getMemBarrierOpt() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000503 assert(Kind == k_MemBarrierOpt && "Invalid access!");
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000504 return MBOpt.Val;
505 }
506
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000507 ARM_PROC::IFlags getProcIFlags() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000508 assert(Kind == k_ProcIFlags && "Invalid access!");
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000509 return IFlags.Val;
510 }
511
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000512 unsigned getMSRMask() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000513 assert(Kind == k_MSRMask && "Invalid access!");
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000514 return MMask.Val;
515 }
516
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000517 bool isCoprocNum() const { return Kind == k_CoprocNum; }
518 bool isCoprocReg() const { return Kind == k_CoprocReg; }
Jim Grosbach9b8f2a02011-10-12 17:34:41 +0000519 bool isCoprocOption() const { return Kind == k_CoprocOption; }
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000520 bool isCondCode() const { return Kind == k_CondCode; }
521 bool isCCOut() const { return Kind == k_CCOut; }
522 bool isITMask() const { return Kind == k_ITCondMask; }
523 bool isITCondCode() const { return Kind == k_CondCode; }
524 bool isImm() const { return Kind == k_Immediate; }
525 bool isFPImm() const { return Kind == k_FPImmediate; }
Jim Grosbacha77295d2011-09-08 22:07:06 +0000526 bool isImm8s4() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000527 if (Kind != k_Immediate)
Jim Grosbacha77295d2011-09-08 22:07:06 +0000528 return false;
529 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
530 if (!CE) return false;
531 int64_t Value = CE->getValue();
532 return ((Value & 3) == 0) && Value >= -1020 && Value <= 1020;
533 }
Jim Grosbach72f39f82011-08-24 21:22:15 +0000534 bool isImm0_1020s4() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000535 if (Kind != k_Immediate)
Jim Grosbach72f39f82011-08-24 21:22:15 +0000536 return false;
537 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
538 if (!CE) return false;
539 int64_t Value = CE->getValue();
540 return ((Value & 3) == 0) && Value >= 0 && Value <= 1020;
541 }
542 bool isImm0_508s4() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000543 if (Kind != k_Immediate)
Jim Grosbach72f39f82011-08-24 21:22:15 +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 >= 0 && Value <= 508;
549 }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000550 bool isImm0_255() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000551 if (Kind != k_Immediate)
Jim Grosbach6b8f1e32011-06-27 23:54:06 +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 >= 0 && Value < 256;
557 }
Jim Grosbach83ab0702011-07-13 22:01:08 +0000558 bool isImm0_7() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000559 if (Kind != k_Immediate)
Jim Grosbach83ab0702011-07-13 22:01:08 +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 >= 0 && Value < 8;
565 }
566 bool isImm0_15() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000567 if (Kind != k_Immediate)
Jim Grosbach83ab0702011-07-13 22:01:08 +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 < 16;
573 }
Jim Grosbach7c6e42e2011-07-21 23:26:25 +0000574 bool isImm0_31() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000575 if (Kind != k_Immediate)
Jim Grosbach7c6e42e2011-07-21 23:26:25 +0000576 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 < 32;
581 }
Jim Grosbachf4943352011-07-25 23:09:14 +0000582 bool isImm1_16() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000583 if (Kind != k_Immediate)
Jim Grosbachf4943352011-07-25 23:09:14 +0000584 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 < 17;
589 }
Jim Grosbach4a5ffb32011-07-22 23:16:18 +0000590 bool isImm1_32() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000591 if (Kind != k_Immediate)
Jim Grosbach4a5ffb32011-07-22 23:16:18 +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 < 33;
597 }
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000598 bool isImm0_65535() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000599 if (Kind != k_Immediate)
Jim Grosbachfff76ee2011-07-13 20:10:10 +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 < 65536;
605 }
Jim Grosbachffa32252011-07-19 19:13:28 +0000606 bool isImm0_65535Expr() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000607 if (Kind != k_Immediate)
Jim Grosbachffa32252011-07-19 19:13:28 +0000608 return false;
609 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
610 // If it's not a constant expression, it'll generate a fixup and be
611 // handled later.
612 if (!CE) return true;
613 int64_t Value = CE->getValue();
614 return Value >= 0 && Value < 65536;
615 }
Jim Grosbached838482011-07-26 16:24:27 +0000616 bool isImm24bit() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000617 if (Kind != k_Immediate)
Jim Grosbached838482011-07-26 16:24:27 +0000618 return false;
619 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
620 if (!CE) return false;
621 int64_t Value = CE->getValue();
622 return Value >= 0 && Value <= 0xffffff;
623 }
Jim Grosbach70939ee2011-08-17 21:51:27 +0000624 bool isImmThumbSR() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000625 if (Kind != k_Immediate)
Jim Grosbach70939ee2011-08-17 21:51:27 +0000626 return false;
627 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
628 if (!CE) return false;
629 int64_t Value = CE->getValue();
630 return Value > 0 && Value < 33;
631 }
Jim Grosbachf6c05252011-07-21 17:23:04 +0000632 bool isPKHLSLImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000633 if (Kind != k_Immediate)
Jim Grosbachf6c05252011-07-21 17:23:04 +0000634 return false;
635 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
636 if (!CE) return false;
637 int64_t Value = CE->getValue();
638 return Value >= 0 && Value < 32;
639 }
640 bool isPKHASRImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000641 if (Kind != k_Immediate)
Jim Grosbachf6c05252011-07-21 17:23:04 +0000642 return false;
643 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
644 if (!CE) return false;
645 int64_t Value = CE->getValue();
646 return Value > 0 && Value <= 32;
647 }
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000648 bool isARMSOImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000649 if (Kind != k_Immediate)
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000650 return false;
651 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
652 if (!CE) return false;
653 int64_t Value = CE->getValue();
654 return ARM_AM::getSOImmVal(Value) != -1;
655 }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000656 bool isT2SOImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000657 if (Kind != k_Immediate)
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000658 return false;
659 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
660 if (!CE) return false;
661 int64_t Value = CE->getValue();
662 return ARM_AM::getT2SOImmVal(Value) != -1;
663 }
Jim Grosbachc27d4f92011-07-22 17:44:50 +0000664 bool isSetEndImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000665 if (Kind != k_Immediate)
Jim Grosbachc27d4f92011-07-22 17:44:50 +0000666 return false;
667 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
668 if (!CE) return false;
669 int64_t Value = CE->getValue();
670 return Value == 1 || Value == 0;
671 }
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000672 bool isReg() const { return Kind == k_Register; }
673 bool isRegList() const { return Kind == k_RegisterList; }
674 bool isDPRRegList() const { return Kind == k_DPRRegisterList; }
675 bool isSPRRegList() const { return Kind == k_SPRRegisterList; }
676 bool isToken() const { return Kind == k_Token; }
677 bool isMemBarrierOpt() const { return Kind == k_MemBarrierOpt; }
678 bool isMemory() const { return Kind == k_Memory; }
679 bool isShifterImm() const { return Kind == k_ShifterImmediate; }
680 bool isRegShiftedReg() const { return Kind == k_ShiftedRegister; }
681 bool isRegShiftedImm() const { return Kind == k_ShiftedImmediate; }
682 bool isRotImm() const { return Kind == k_RotateImmediate; }
683 bool isBitfield() const { return Kind == k_BitfieldDescriptor; }
684 bool isPostIdxRegShifted() const { return Kind == k_PostIndexRegister; }
Jim Grosbachf4fa3d62011-08-05 21:28:30 +0000685 bool isPostIdxReg() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000686 return Kind == k_PostIndexRegister && PostIdxReg.ShiftTy == ARM_AM::no_shift;
Jim Grosbachf4fa3d62011-08-05 21:28:30 +0000687 }
Jim Grosbach57dcb852011-10-11 17:29:55 +0000688 bool isMemNoOffset(bool alignOK = false) const {
Jim Grosbachf6c35c52011-10-10 23:06:42 +0000689 if (!isMemory())
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000690 return false;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000691 // No offset of any kind.
Jim Grosbach57dcb852011-10-11 17:29:55 +0000692 return Memory.OffsetRegNum == 0 && Memory.OffsetImm == 0 &&
693 (alignOK || Memory.Alignment == 0);
694 }
695 bool isAlignedMemory() const {
696 return isMemNoOffset(true);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000697 }
Jim Grosbach7ce05792011-08-03 23:50:40 +0000698 bool isAddrMode2() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000699 if (!isMemory() || Memory.Alignment != 0) return false;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000700 // Check for register offset.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000701 if (Memory.OffsetRegNum) return true;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000702 // Immediate offset in range [-4095, 4095].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000703 if (!Memory.OffsetImm) return true;
704 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbach7ce05792011-08-03 23:50:40 +0000705 return Val > -4096 && Val < 4096;
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000706 }
Jim Grosbach039c2e12011-08-04 23:01:30 +0000707 bool isAM2OffsetImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000708 if (Kind != k_Immediate)
Jim Grosbach039c2e12011-08-04 23:01:30 +0000709 return false;
710 // Immediate offset in range [-4095, 4095].
711 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
712 if (!CE) return false;
713 int64_t Val = CE->getValue();
714 return Val > -4096 && Val < 4096;
715 }
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000716 bool isAddrMode3() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000717 if (!isMemory() || Memory.Alignment != 0) return false;
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000718 // No shifts are legal for AM3.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000719 if (Memory.ShiftType != ARM_AM::no_shift) return false;
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000720 // Check for register offset.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000721 if (Memory.OffsetRegNum) return true;
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000722 // Immediate offset in range [-255, 255].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000723 if (!Memory.OffsetImm) return true;
724 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000725 return Val > -256 && Val < 256;
726 }
727 bool isAM3Offset() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000728 if (Kind != k_Immediate && Kind != k_PostIndexRegister)
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000729 return false;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000730 if (Kind == k_PostIndexRegister)
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000731 return PostIdxReg.ShiftTy == ARM_AM::no_shift;
732 // Immediate offset in range [-255, 255].
733 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
734 if (!CE) return false;
735 int64_t Val = CE->getValue();
Jim Grosbach251bf252011-08-10 21:56:18 +0000736 // Special case, #-0 is INT32_MIN.
737 return (Val > -256 && Val < 256) || Val == INT32_MIN;
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000738 }
Jim Grosbach7ce05792011-08-03 23:50:40 +0000739 bool isAddrMode5() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000740 if (!isMemory() || Memory.Alignment != 0) return false;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000741 // Check for register offset.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000742 if (Memory.OffsetRegNum) return false;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000743 // Immediate offset in range [-1020, 1020] and a multiple of 4.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000744 if (!Memory.OffsetImm) return true;
745 int64_t Val = Memory.OffsetImm->getValue();
Owen Anderson0da10cf2011-08-29 19:36:44 +0000746 return (Val >= -1020 && Val <= 1020 && ((Val & 3) == 0)) ||
747 Val == INT32_MIN;
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000748 }
Jim Grosbach7f739be2011-09-19 22:21:13 +0000749 bool isMemTBB() const {
Jim Grosbache53c87b2011-10-11 15:59:20 +0000750 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
Jim Grosbach57dcb852011-10-11 17:29:55 +0000751 Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
Jim Grosbach7f739be2011-09-19 22:21:13 +0000752 return false;
753 return true;
754 }
755 bool isMemTBH() const {
Jim Grosbache53c87b2011-10-11 15:59:20 +0000756 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
Jim Grosbach57dcb852011-10-11 17:29:55 +0000757 Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm != 1 ||
758 Memory.Alignment != 0 )
Jim Grosbach7f739be2011-09-19 22:21:13 +0000759 return false;
760 return true;
761 }
Jim Grosbach7ce05792011-08-03 23:50:40 +0000762 bool isMemRegOffset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000763 if (!isMemory() || !Memory.OffsetRegNum || Memory.Alignment != 0)
Bill Wendlingf4caf692010-12-14 03:36:38 +0000764 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000765 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000766 }
Jim Grosbachab899c12011-09-07 23:10:15 +0000767 bool isT2MemRegOffset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000768 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
769 Memory.Alignment != 0)
Jim Grosbachab899c12011-09-07 23:10:15 +0000770 return false;
771 // Only lsl #{0, 1, 2, 3} allowed.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000772 if (Memory.ShiftType == ARM_AM::no_shift)
Jim Grosbachab899c12011-09-07 23:10:15 +0000773 return true;
Jim Grosbache53c87b2011-10-11 15:59:20 +0000774 if (Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm > 3)
Jim Grosbachab899c12011-09-07 23:10:15 +0000775 return false;
776 return true;
777 }
Jim Grosbach7ce05792011-08-03 23:50:40 +0000778 bool isMemThumbRR() const {
779 // Thumb reg+reg addressing is simple. Just two registers, a base and
780 // an offset. No shifts, negations or any other complicating factors.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000781 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
Jim Grosbach57dcb852011-10-11 17:29:55 +0000782 Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000783 return false;
Jim Grosbache53c87b2011-10-11 15:59:20 +0000784 return isARMLowRegister(Memory.BaseRegNum) &&
785 (!Memory.OffsetRegNum || isARMLowRegister(Memory.OffsetRegNum));
Jim Grosbach60f91a32011-08-19 17:55:24 +0000786 }
787 bool isMemThumbRIs4() const {
Jim Grosbache53c87b2011-10-11 15:59:20 +0000788 if (!isMemory() || Memory.OffsetRegNum != 0 ||
Jim Grosbach57dcb852011-10-11 17:29:55 +0000789 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
Jim Grosbach60f91a32011-08-19 17:55:24 +0000790 return false;
791 // Immediate offset, multiple of 4 in range [0, 124].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000792 if (!Memory.OffsetImm) return true;
793 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbachecd85892011-08-19 18:13:48 +0000794 return Val >= 0 && Val <= 124 && (Val % 4) == 0;
795 }
Jim Grosbach38466302011-08-19 18:55:51 +0000796 bool isMemThumbRIs2() const {
Jim Grosbache53c87b2011-10-11 15:59:20 +0000797 if (!isMemory() || Memory.OffsetRegNum != 0 ||
Jim Grosbach57dcb852011-10-11 17:29:55 +0000798 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
Jim Grosbach38466302011-08-19 18:55:51 +0000799 return false;
800 // Immediate offset, multiple of 4 in range [0, 62].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000801 if (!Memory.OffsetImm) return true;
802 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbach38466302011-08-19 18:55:51 +0000803 return Val >= 0 && Val <= 62 && (Val % 2) == 0;
804 }
Jim Grosbach48ff5ff2011-08-19 18:49:59 +0000805 bool isMemThumbRIs1() const {
Jim Grosbache53c87b2011-10-11 15:59:20 +0000806 if (!isMemory() || Memory.OffsetRegNum != 0 ||
Jim Grosbach57dcb852011-10-11 17:29:55 +0000807 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
Jim Grosbach48ff5ff2011-08-19 18:49:59 +0000808 return false;
809 // Immediate offset in range [0, 31].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000810 if (!Memory.OffsetImm) return true;
811 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbach48ff5ff2011-08-19 18:49:59 +0000812 return Val >= 0 && Val <= 31;
813 }
Jim Grosbachecd85892011-08-19 18:13:48 +0000814 bool isMemThumbSPI() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000815 if (!isMemory() || Memory.OffsetRegNum != 0 ||
816 Memory.BaseRegNum != ARM::SP || Memory.Alignment != 0)
Jim Grosbachecd85892011-08-19 18:13:48 +0000817 return false;
818 // Immediate offset, multiple of 4 in range [0, 1020].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000819 if (!Memory.OffsetImm) return true;
820 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbachecd85892011-08-19 18:13:48 +0000821 return Val >= 0 && Val <= 1020 && (Val % 4) == 0;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000822 }
Jim Grosbacha77295d2011-09-08 22:07:06 +0000823 bool isMemImm8s4Offset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000824 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbacha77295d2011-09-08 22:07:06 +0000825 return false;
826 // Immediate offset a multiple of 4 in range [-1020, 1020].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000827 if (!Memory.OffsetImm) return true;
828 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbacha77295d2011-09-08 22:07:06 +0000829 return Val >= -1020 && Val <= 1020 && (Val & 3) == 0;
830 }
Jim Grosbachb6aed502011-09-09 18:37:27 +0000831 bool isMemImm0_1020s4Offset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000832 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbachb6aed502011-09-09 18:37:27 +0000833 return false;
834 // Immediate offset a multiple of 4 in range [0, 1020].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000835 if (!Memory.OffsetImm) return true;
836 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbachb6aed502011-09-09 18:37:27 +0000837 return Val >= 0 && Val <= 1020 && (Val & 3) == 0;
838 }
Jim Grosbach7ce05792011-08-03 23:50:40 +0000839 bool isMemImm8Offset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000840 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbach7ce05792011-08-03 23:50:40 +0000841 return false;
842 // Immediate offset in range [-255, 255].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000843 if (!Memory.OffsetImm) return true;
844 int64_t Val = Memory.OffsetImm->getValue();
Owen Anderson4d2a0012011-09-23 22:25:02 +0000845 return (Val == INT32_MIN) || (Val > -256 && Val < 256);
Jim Grosbach7ce05792011-08-03 23:50:40 +0000846 }
Jim Grosbachf0eee6e2011-09-07 23:39:14 +0000847 bool isMemPosImm8Offset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000848 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbachf0eee6e2011-09-07 23:39:14 +0000849 return false;
850 // Immediate offset in range [0, 255].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000851 if (!Memory.OffsetImm) return true;
852 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbachf0eee6e2011-09-07 23:39:14 +0000853 return Val >= 0 && Val < 256;
854 }
Jim Grosbacha8307dd2011-09-07 20:58:57 +0000855 bool isMemNegImm8Offset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000856 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbacha8307dd2011-09-07 20:58:57 +0000857 return false;
858 // Immediate offset in range [-255, -1].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000859 if (!Memory.OffsetImm) return true;
860 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbacha8307dd2011-09-07 20:58:57 +0000861 return Val > -256 && Val < 0;
862 }
863 bool isMemUImm12Offset() const {
864 // If we have an immediate that's not a constant, treat it as a label
865 // reference needing a fixup. If it is a constant, it's something else
866 // and we reject it.
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000867 if (Kind == k_Immediate && !isa<MCConstantExpr>(getImm()))
Jim Grosbacha8307dd2011-09-07 20:58:57 +0000868 return true;
869
Jim Grosbach57dcb852011-10-11 17:29:55 +0000870 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbacha8307dd2011-09-07 20:58:57 +0000871 return false;
872 // Immediate offset in range [0, 4095].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000873 if (!Memory.OffsetImm) return true;
874 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbacha8307dd2011-09-07 20:58:57 +0000875 return (Val >= 0 && Val < 4096);
876 }
Jim Grosbach7ce05792011-08-03 23:50:40 +0000877 bool isMemImm12Offset() const {
Jim Grosbach09176e12011-08-08 20:59:31 +0000878 // If we have an immediate that's not a constant, treat it as a label
879 // reference needing a fixup. If it is a constant, it's something else
880 // and we reject it.
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000881 if (Kind == k_Immediate && !isa<MCConstantExpr>(getImm()))
Jim Grosbach09176e12011-08-08 20:59:31 +0000882 return true;
883
Jim Grosbach57dcb852011-10-11 17:29:55 +0000884 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbach7ce05792011-08-03 23:50:40 +0000885 return false;
886 // Immediate offset in range [-4095, 4095].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000887 if (!Memory.OffsetImm) return true;
888 int64_t Val = Memory.OffsetImm->getValue();
Owen Anderson0da10cf2011-08-29 19:36:44 +0000889 return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
Jim Grosbach7ce05792011-08-03 23:50:40 +0000890 }
891 bool isPostIdxImm8() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000892 if (Kind != k_Immediate)
Jim Grosbach7ce05792011-08-03 23:50:40 +0000893 return false;
894 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
895 if (!CE) return false;
896 int64_t Val = CE->getValue();
Owen Anderson63553c72011-08-29 17:17:09 +0000897 return (Val > -256 && Val < 256) || (Val == INT32_MIN);
Jim Grosbach7ce05792011-08-03 23:50:40 +0000898 }
Jim Grosbach2bd01182011-10-11 21:55:36 +0000899 bool isPostIdxImm8s4() const {
900 if (Kind != k_Immediate)
901 return false;
902 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
903 if (!CE) return false;
904 int64_t Val = CE->getValue();
905 return ((Val & 3) == 0 && Val >= -1020 && Val <= 1020) ||
906 (Val == INT32_MIN);
907 }
Jim Grosbach7ce05792011-08-03 23:50:40 +0000908
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000909 bool isMSRMask() const { return Kind == k_MSRMask; }
910 bool isProcIFlags() const { return Kind == k_ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000911
Jim Grosbach0e387b22011-10-17 22:26:03 +0000912 // NEON operands.
Jim Grosbach862019c2011-10-18 23:02:30 +0000913 bool isVecListOneD() const {
914 if (Kind != k_VectorList) return false;
915 return VectorList.Count == 1;
916 }
917
Jim Grosbach280dfad2011-10-21 18:54:25 +0000918 bool isVecListTwoD() const {
919 if (Kind != k_VectorList) return false;
920 return VectorList.Count == 2;
921 }
922
Jim Grosbachcdcfa282011-10-21 20:02:19 +0000923 bool isVecListThreeD() const {
924 if (Kind != k_VectorList) return false;
925 return VectorList.Count == 3;
926 }
927
Jim Grosbachb6310312011-10-21 20:35:01 +0000928 bool isVecListFourD() const {
929 if (Kind != k_VectorList) return false;
930 return VectorList.Count == 4;
931 }
932
Jim Grosbach4661d4c2011-10-21 22:21:10 +0000933 bool isVecListTwoQ() const {
934 if (Kind != k_VectorList) return false;
935 //FIXME: We haven't taught the parser to handle by-two register lists
936 // yet, so don't pretend to know one.
937 return VectorList.Count == 2 && false;
938 }
939
Jim Grosbach460a9052011-10-07 23:56:00 +0000940 bool isVectorIndex8() const {
941 if (Kind != k_VectorIndex) return false;
942 return VectorIndex.Val < 8;
943 }
944 bool isVectorIndex16() const {
945 if (Kind != k_VectorIndex) return false;
946 return VectorIndex.Val < 4;
947 }
948 bool isVectorIndex32() const {
949 if (Kind != k_VectorIndex) return false;
950 return VectorIndex.Val < 2;
951 }
952
Jim Grosbach0e387b22011-10-17 22:26:03 +0000953 bool isNEONi8splat() const {
954 if (Kind != k_Immediate)
955 return false;
956 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
957 // Must be a constant.
958 if (!CE) return false;
959 int64_t Value = CE->getValue();
960 // i8 value splatted across 8 bytes. The immediate is just the 8 byte
961 // value.
Jim Grosbach0e387b22011-10-17 22:26:03 +0000962 return Value >= 0 && Value < 256;
963 }
Jim Grosbach460a9052011-10-07 23:56:00 +0000964
Jim Grosbachea461102011-10-17 23:09:09 +0000965 bool isNEONi16splat() const {
966 if (Kind != k_Immediate)
967 return false;
968 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
969 // Must be a constant.
970 if (!CE) return false;
971 int64_t Value = CE->getValue();
972 // i16 value in the range [0,255] or [0x0100, 0xff00]
973 return (Value >= 0 && Value < 256) || (Value >= 0x0100 && Value <= 0xff00);
974 }
975
Jim Grosbach6248a542011-10-18 00:22:00 +0000976 bool isNEONi32splat() const {
977 if (Kind != k_Immediate)
978 return false;
979 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
980 // Must be a constant.
981 if (!CE) return false;
982 int64_t Value = CE->getValue();
983 // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X.
984 return (Value >= 0 && Value < 256) ||
985 (Value >= 0x0100 && Value <= 0xff00) ||
986 (Value >= 0x010000 && Value <= 0xff0000) ||
987 (Value >= 0x01000000 && Value <= 0xff000000);
988 }
989
990 bool isNEONi32vmov() const {
991 if (Kind != k_Immediate)
992 return false;
993 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
994 // Must be a constant.
995 if (!CE) return false;
996 int64_t Value = CE->getValue();
997 // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
998 // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
999 return (Value >= 0 && Value < 256) ||
1000 (Value >= 0x0100 && Value <= 0xff00) ||
1001 (Value >= 0x010000 && Value <= 0xff0000) ||
1002 (Value >= 0x01000000 && Value <= 0xff000000) ||
1003 (Value >= 0x01ff && Value <= 0xffff && (Value & 0xff) == 0xff) ||
1004 (Value >= 0x01ffff && Value <= 0xffffff && (Value & 0xffff) == 0xffff);
1005 }
1006
Jim Grosbachf2f5bc62011-10-18 16:18:11 +00001007 bool isNEONi64splat() const {
1008 if (Kind != k_Immediate)
1009 return false;
1010 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1011 // Must be a constant.
1012 if (!CE) return false;
1013 uint64_t Value = CE->getValue();
1014 // i64 value with each byte being either 0 or 0xff.
1015 for (unsigned i = 0; i < 8; ++i)
1016 if ((Value & 0xff) != 0 && (Value & 0xff) != 0xff) return false;
1017 return true;
1018 }
1019
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001020 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +00001021 // Add as immediates when possible. Null MCExpr = 0.
1022 if (Expr == 0)
1023 Inst.addOperand(MCOperand::CreateImm(0));
1024 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001025 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1026 else
1027 Inst.addOperand(MCOperand::CreateExpr(Expr));
1028 }
1029
Daniel Dunbar8462b302010-08-11 06:36:53 +00001030 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001031 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +00001032 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +00001033 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
1034 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +00001035 }
1036
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001037 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
1038 assert(N == 1 && "Invalid number of operands!");
1039 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
1040 }
1041
Jim Grosbach9b8f2a02011-10-12 17:34:41 +00001042 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
1043 assert(N == 1 && "Invalid number of operands!");
1044 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
1045 }
1046
1047 void addCoprocOptionOperands(MCInst &Inst, unsigned N) const {
1048 assert(N == 1 && "Invalid number of operands!");
1049 Inst.addOperand(MCOperand::CreateImm(CoprocOption.Val));
1050 }
1051
Jim Grosbach89df9962011-08-26 21:43:41 +00001052 void addITMaskOperands(MCInst &Inst, unsigned N) const {
1053 assert(N == 1 && "Invalid number of operands!");
1054 Inst.addOperand(MCOperand::CreateImm(ITMask.Mask));
1055 }
1056
1057 void addITCondCodeOperands(MCInst &Inst, unsigned N) const {
1058 assert(N == 1 && "Invalid number of operands!");
1059 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
1060 }
1061
Jim Grosbachd67641b2010-12-06 18:21:12 +00001062 void addCCOutOperands(MCInst &Inst, unsigned N) const {
1063 assert(N == 1 && "Invalid number of operands!");
1064 Inst.addOperand(MCOperand::CreateReg(getReg()));
1065 }
1066
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001067 void addRegOperands(MCInst &Inst, unsigned N) const {
1068 assert(N == 1 && "Invalid number of operands!");
1069 Inst.addOperand(MCOperand::CreateReg(getReg()));
1070 }
1071
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001072 void addRegShiftedRegOperands(MCInst &Inst, unsigned N) const {
Jim Grosbache8606dc2011-07-13 17:50:29 +00001073 assert(N == 3 && "Invalid number of operands!");
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001074 assert(isRegShiftedReg() && "addRegShiftedRegOperands() on non RegShiftedReg!");
1075 Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.SrcReg));
1076 Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.ShiftReg));
Jim Grosbache8606dc2011-07-13 17:50:29 +00001077 Inst.addOperand(MCOperand::CreateImm(
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001078 ARM_AM::getSORegOpc(RegShiftedReg.ShiftTy, RegShiftedReg.ShiftImm)));
Jim Grosbache8606dc2011-07-13 17:50:29 +00001079 }
1080
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001081 void addRegShiftedImmOperands(MCInst &Inst, unsigned N) const {
Owen Anderson152d4a42011-07-21 23:38:37 +00001082 assert(N == 2 && "Invalid number of operands!");
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001083 assert(isRegShiftedImm() && "addRegShiftedImmOperands() on non RegShiftedImm!");
1084 Inst.addOperand(MCOperand::CreateReg(RegShiftedImm.SrcReg));
Owen Anderson92a20222011-07-21 18:54:16 +00001085 Inst.addOperand(MCOperand::CreateImm(
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001086 ARM_AM::getSORegOpc(RegShiftedImm.ShiftTy, RegShiftedImm.ShiftImm)));
Owen Anderson92a20222011-07-21 18:54:16 +00001087 }
1088
Jim Grosbach580f4a92011-07-25 22:20:28 +00001089 void addShifterImmOperands(MCInst &Inst, unsigned N) const {
Owen Anderson00828302011-03-18 22:50:18 +00001090 assert(N == 1 && "Invalid number of operands!");
Jim Grosbach580f4a92011-07-25 22:20:28 +00001091 Inst.addOperand(MCOperand::CreateImm((ShifterImm.isASR << 5) |
1092 ShifterImm.Imm));
Owen Anderson00828302011-03-18 22:50:18 +00001093 }
1094
Bill Wendling87f4f9a2010-11-08 23:49:57 +00001095 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +00001096 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +00001097 const SmallVectorImpl<unsigned> &RegList = getRegList();
1098 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +00001099 I = RegList.begin(), E = RegList.end(); I != E; ++I)
1100 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +00001101 }
1102
Bill Wendling0f630752010-11-17 04:32:08 +00001103 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
1104 addRegListOperands(Inst, N);
1105 }
1106
1107 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
1108 addRegListOperands(Inst, N);
1109 }
1110
Jim Grosbach7e1547e2011-07-27 20:15:40 +00001111 void addRotImmOperands(MCInst &Inst, unsigned N) const {
1112 assert(N == 1 && "Invalid number of operands!");
1113 // Encoded as val>>3. The printer handles display as 8, 16, 24.
1114 Inst.addOperand(MCOperand::CreateImm(RotImm.Imm >> 3));
1115 }
1116
Jim Grosbach293a2ee2011-07-28 21:34:26 +00001117 void addBitfieldOperands(MCInst &Inst, unsigned N) const {
1118 assert(N == 1 && "Invalid number of operands!");
1119 // Munge the lsb/width into a bitfield mask.
1120 unsigned lsb = Bitfield.LSB;
1121 unsigned width = Bitfield.Width;
1122 // Make a 32-bit mask w/ the referenced bits clear and all other bits set.
1123 uint32_t Mask = ~(((uint32_t)0xffffffff >> lsb) << (32 - width) >>
1124 (32 - (lsb + width)));
1125 Inst.addOperand(MCOperand::CreateImm(Mask));
1126 }
1127
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001128 void addImmOperands(MCInst &Inst, unsigned N) const {
1129 assert(N == 1 && "Invalid number of operands!");
1130 addExpr(Inst, getImm());
1131 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001132
Jim Grosbach9d390362011-10-03 23:38:36 +00001133 void addFPImmOperands(MCInst &Inst, unsigned N) const {
1134 assert(N == 1 && "Invalid number of operands!");
1135 Inst.addOperand(MCOperand::CreateImm(getFPImm()));
1136 }
1137
Jim Grosbacha77295d2011-09-08 22:07:06 +00001138 void addImm8s4Operands(MCInst &Inst, unsigned N) const {
1139 assert(N == 1 && "Invalid number of operands!");
1140 // FIXME: We really want to scale the value here, but the LDRD/STRD
1141 // instruction don't encode operands that way yet.
1142 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1143 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1144 }
1145
Jim Grosbach72f39f82011-08-24 21:22:15 +00001146 void addImm0_1020s4Operands(MCInst &Inst, unsigned N) const {
1147 assert(N == 1 && "Invalid number of operands!");
1148 // The immediate is scaled by four in the encoding and is stored
1149 // in the MCInst as such. Lop off the low two bits here.
1150 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1151 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1152 }
1153
1154 void addImm0_508s4Operands(MCInst &Inst, unsigned N) const {
1155 assert(N == 1 && "Invalid number of operands!");
1156 // The immediate is scaled by four in the encoding and is stored
1157 // in the MCInst as such. Lop off the low two bits here.
1158 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1159 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1160 }
1161
Jim Grosbach6b8f1e32011-06-27 23:54:06 +00001162 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
1163 assert(N == 1 && "Invalid number of operands!");
1164 addExpr(Inst, getImm());
1165 }
1166
Jim Grosbach83ab0702011-07-13 22:01:08 +00001167 void addImm0_7Operands(MCInst &Inst, unsigned N) const {
1168 assert(N == 1 && "Invalid number of operands!");
1169 addExpr(Inst, getImm());
1170 }
1171
1172 void addImm0_15Operands(MCInst &Inst, unsigned N) const {
1173 assert(N == 1 && "Invalid number of operands!");
1174 addExpr(Inst, getImm());
1175 }
1176
Jim Grosbach7c6e42e2011-07-21 23:26:25 +00001177 void addImm0_31Operands(MCInst &Inst, unsigned N) const {
1178 assert(N == 1 && "Invalid number of operands!");
1179 addExpr(Inst, getImm());
1180 }
1181
Jim Grosbachf4943352011-07-25 23:09:14 +00001182 void addImm1_16Operands(MCInst &Inst, unsigned N) const {
1183 assert(N == 1 && "Invalid number of operands!");
1184 // The constant encodes as the immediate-1, and we store in the instruction
1185 // the bits as encoded, so subtract off one here.
1186 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1187 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1188 }
1189
Jim Grosbach4a5ffb32011-07-22 23:16:18 +00001190 void addImm1_32Operands(MCInst &Inst, unsigned N) const {
1191 assert(N == 1 && "Invalid number of operands!");
1192 // The constant encodes as the immediate-1, and we store in the instruction
1193 // the bits as encoded, so subtract off one here.
1194 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1195 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1196 }
1197
Jim Grosbachfff76ee2011-07-13 20:10:10 +00001198 void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
1199 assert(N == 1 && "Invalid number of operands!");
1200 addExpr(Inst, getImm());
1201 }
1202
Jim Grosbachffa32252011-07-19 19:13:28 +00001203 void addImm0_65535ExprOperands(MCInst &Inst, unsigned N) const {
1204 assert(N == 1 && "Invalid number of operands!");
1205 addExpr(Inst, getImm());
1206 }
1207
Jim Grosbached838482011-07-26 16:24:27 +00001208 void addImm24bitOperands(MCInst &Inst, unsigned N) const {
1209 assert(N == 1 && "Invalid number of operands!");
1210 addExpr(Inst, getImm());
1211 }
1212
Jim Grosbach70939ee2011-08-17 21:51:27 +00001213 void addImmThumbSROperands(MCInst &Inst, unsigned N) const {
1214 assert(N == 1 && "Invalid number of operands!");
1215 // The constant encodes as the immediate, except for 32, which encodes as
1216 // zero.
1217 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1218 unsigned Imm = CE->getValue();
1219 Inst.addOperand(MCOperand::CreateImm((Imm == 32 ? 0 : Imm)));
1220 }
1221
Jim Grosbachf6c05252011-07-21 17:23:04 +00001222 void addPKHLSLImmOperands(MCInst &Inst, unsigned N) const {
1223 assert(N == 1 && "Invalid number of operands!");
1224 addExpr(Inst, getImm());
1225 }
1226
1227 void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
1228 assert(N == 1 && "Invalid number of operands!");
1229 // An ASR value of 32 encodes as 0, so that's how we want to add it to
1230 // the instruction as well.
1231 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1232 int Val = CE->getValue();
1233 Inst.addOperand(MCOperand::CreateImm(Val == 32 ? 0 : Val));
1234 }
1235
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +00001236 void addARMSOImmOperands(MCInst &Inst, unsigned N) const {
1237 assert(N == 1 && "Invalid number of operands!");
1238 addExpr(Inst, getImm());
1239 }
1240
Jim Grosbach6b8f1e32011-06-27 23:54:06 +00001241 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
1242 assert(N == 1 && "Invalid number of operands!");
1243 addExpr(Inst, getImm());
1244 }
1245
Jim Grosbachc27d4f92011-07-22 17:44:50 +00001246 void addSetEndImmOperands(MCInst &Inst, unsigned N) const {
1247 assert(N == 1 && "Invalid number of operands!");
1248 addExpr(Inst, getImm());
1249 }
1250
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001251 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
1252 assert(N == 1 && "Invalid number of operands!");
1253 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
1254 }
1255
Jim Grosbach7ce05792011-08-03 23:50:40 +00001256 void addMemNoOffsetOperands(MCInst &Inst, unsigned N) const {
1257 assert(N == 1 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001258 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +00001259 }
1260
Jim Grosbach57dcb852011-10-11 17:29:55 +00001261 void addAlignedMemoryOperands(MCInst &Inst, unsigned N) const {
1262 assert(N == 2 && "Invalid number of operands!");
1263 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1264 Inst.addOperand(MCOperand::CreateImm(Memory.Alignment));
1265 }
1266
Jim Grosbach7ce05792011-08-03 23:50:40 +00001267 void addAddrMode2Operands(MCInst &Inst, unsigned N) const {
1268 assert(N == 3 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001269 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1270 if (!Memory.OffsetRegNum) {
Jim Grosbach7ce05792011-08-03 23:50:40 +00001271 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1272 // Special case for #-0
1273 if (Val == INT32_MIN) Val = 0;
1274 if (Val < 0) Val = -Val;
1275 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1276 } else {
1277 // For register offset, we encode the shift type and negation flag
1278 // here.
Jim Grosbache53c87b2011-10-11 15:59:20 +00001279 Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1280 Memory.ShiftImm, Memory.ShiftType);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001281 }
Jim Grosbache53c87b2011-10-11 15:59:20 +00001282 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1283 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
Jim Grosbach7ce05792011-08-03 23:50:40 +00001284 Inst.addOperand(MCOperand::CreateImm(Val));
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001285 }
1286
Jim Grosbach039c2e12011-08-04 23:01:30 +00001287 void addAM2OffsetImmOperands(MCInst &Inst, unsigned N) const {
1288 assert(N == 2 && "Invalid number of operands!");
1289 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1290 assert(CE && "non-constant AM2OffsetImm operand!");
1291 int32_t Val = CE->getValue();
1292 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1293 // Special case for #-0
1294 if (Val == INT32_MIN) Val = 0;
1295 if (Val < 0) Val = -Val;
1296 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1297 Inst.addOperand(MCOperand::CreateReg(0));
1298 Inst.addOperand(MCOperand::CreateImm(Val));
1299 }
1300
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001301 void addAddrMode3Operands(MCInst &Inst, unsigned N) const {
1302 assert(N == 3 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001303 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1304 if (!Memory.OffsetRegNum) {
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001305 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1306 // Special case for #-0
1307 if (Val == INT32_MIN) Val = 0;
1308 if (Val < 0) Val = -Val;
1309 Val = ARM_AM::getAM3Opc(AddSub, Val);
1310 } else {
1311 // For register offset, we encode the shift type and negation flag
1312 // here.
Jim Grosbache53c87b2011-10-11 15:59:20 +00001313 Val = ARM_AM::getAM3Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add, 0);
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001314 }
Jim Grosbache53c87b2011-10-11 15:59:20 +00001315 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1316 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001317 Inst.addOperand(MCOperand::CreateImm(Val));
1318 }
1319
1320 void addAM3OffsetOperands(MCInst &Inst, unsigned N) const {
1321 assert(N == 2 && "Invalid number of operands!");
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001322 if (Kind == k_PostIndexRegister) {
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001323 int32_t Val =
1324 ARM_AM::getAM3Opc(PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub, 0);
1325 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1326 Inst.addOperand(MCOperand::CreateImm(Val));
Jim Grosbach251bf252011-08-10 21:56:18 +00001327 return;
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001328 }
1329
1330 // Constant offset.
1331 const MCConstantExpr *CE = static_cast<const MCConstantExpr*>(getImm());
1332 int32_t Val = CE->getValue();
1333 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1334 // Special case for #-0
1335 if (Val == INT32_MIN) Val = 0;
1336 if (Val < 0) Val = -Val;
Jim Grosbach251bf252011-08-10 21:56:18 +00001337 Val = ARM_AM::getAM3Opc(AddSub, Val);
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001338 Inst.addOperand(MCOperand::CreateReg(0));
1339 Inst.addOperand(MCOperand::CreateImm(Val));
1340 }
1341
Jim Grosbach7ce05792011-08-03 23:50:40 +00001342 void addAddrMode5Operands(MCInst &Inst, unsigned N) const {
1343 assert(N == 2 && "Invalid number of operands!");
1344 // The lower two bits are always zero and as such are not encoded.
Jim Grosbache53c87b2011-10-11 15:59:20 +00001345 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
Jim Grosbach7ce05792011-08-03 23:50:40 +00001346 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1347 // Special case for #-0
1348 if (Val == INT32_MIN) Val = 0;
1349 if (Val < 0) Val = -Val;
1350 Val = ARM_AM::getAM5Opc(AddSub, Val);
Jim Grosbache53c87b2011-10-11 15:59:20 +00001351 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbach7ce05792011-08-03 23:50:40 +00001352 Inst.addOperand(MCOperand::CreateImm(Val));
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001353 }
1354
Jim Grosbacha77295d2011-09-08 22:07:06 +00001355 void addMemImm8s4OffsetOperands(MCInst &Inst, unsigned N) const {
1356 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001357 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1358 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbacha77295d2011-09-08 22:07:06 +00001359 Inst.addOperand(MCOperand::CreateImm(Val));
1360 }
1361
Jim Grosbachb6aed502011-09-09 18:37:27 +00001362 void addMemImm0_1020s4OffsetOperands(MCInst &Inst, unsigned N) const {
1363 assert(N == 2 && "Invalid number of operands!");
1364 // The lower two bits are always zero and as such are not encoded.
Jim Grosbache53c87b2011-10-11 15:59:20 +00001365 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1366 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbachb6aed502011-09-09 18:37:27 +00001367 Inst.addOperand(MCOperand::CreateImm(Val));
1368 }
1369
Jim Grosbach7ce05792011-08-03 23:50:40 +00001370 void addMemImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1371 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001372 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1373 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbach7ce05792011-08-03 23:50:40 +00001374 Inst.addOperand(MCOperand::CreateImm(Val));
Chris Lattner14b93852010-10-29 00:27:31 +00001375 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001376
Jim Grosbachf0eee6e2011-09-07 23:39:14 +00001377 void addMemPosImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1378 addMemImm8OffsetOperands(Inst, N);
1379 }
1380
Jim Grosbacha8307dd2011-09-07 20:58:57 +00001381 void addMemNegImm8OffsetOperands(MCInst &Inst, unsigned N) const {
Jim Grosbachf0eee6e2011-09-07 23:39:14 +00001382 addMemImm8OffsetOperands(Inst, N);
Jim Grosbacha8307dd2011-09-07 20:58:57 +00001383 }
1384
1385 void addMemUImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1386 assert(N == 2 && "Invalid number of operands!");
1387 // If this is an immediate, it's a label reference.
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001388 if (Kind == k_Immediate) {
Jim Grosbacha8307dd2011-09-07 20:58:57 +00001389 addExpr(Inst, getImm());
1390 Inst.addOperand(MCOperand::CreateImm(0));
1391 return;
1392 }
1393
1394 // Otherwise, it's a normal memory reg+offset.
Jim Grosbache53c87b2011-10-11 15:59:20 +00001395 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1396 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbacha8307dd2011-09-07 20:58:57 +00001397 Inst.addOperand(MCOperand::CreateImm(Val));
1398 }
1399
Jim Grosbach7ce05792011-08-03 23:50:40 +00001400 void addMemImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1401 assert(N == 2 && "Invalid number of operands!");
Jim Grosbach09176e12011-08-08 20:59:31 +00001402 // If this is an immediate, it's a label reference.
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001403 if (Kind == k_Immediate) {
Jim Grosbach09176e12011-08-08 20:59:31 +00001404 addExpr(Inst, getImm());
1405 Inst.addOperand(MCOperand::CreateImm(0));
1406 return;
1407 }
1408
1409 // Otherwise, it's a normal memory reg+offset.
Jim Grosbache53c87b2011-10-11 15:59:20 +00001410 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1411 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbach7ce05792011-08-03 23:50:40 +00001412 Inst.addOperand(MCOperand::CreateImm(Val));
Bill Wendlingf4caf692010-12-14 03:36:38 +00001413 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +00001414
Jim Grosbach7f739be2011-09-19 22:21:13 +00001415 void addMemTBBOperands(MCInst &Inst, unsigned N) const {
1416 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001417 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1418 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
Jim Grosbach7f739be2011-09-19 22:21:13 +00001419 }
1420
1421 void addMemTBHOperands(MCInst &Inst, unsigned N) const {
1422 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001423 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1424 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
Jim Grosbach7f739be2011-09-19 22:21:13 +00001425 }
1426
Jim Grosbach7ce05792011-08-03 23:50:40 +00001427 void addMemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1428 assert(N == 3 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001429 unsigned Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1430 Memory.ShiftImm, Memory.ShiftType);
1431 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1432 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
Jim Grosbach7ce05792011-08-03 23:50:40 +00001433 Inst.addOperand(MCOperand::CreateImm(Val));
1434 }
1435
Jim Grosbachab899c12011-09-07 23:10:15 +00001436 void addT2MemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1437 assert(N == 3 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001438 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1439 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1440 Inst.addOperand(MCOperand::CreateImm(Memory.ShiftImm));
Jim Grosbachab899c12011-09-07 23:10:15 +00001441 }
1442
Jim Grosbach7ce05792011-08-03 23:50:40 +00001443 void addMemThumbRROperands(MCInst &Inst, unsigned N) const {
1444 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001445 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1446 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
Jim Grosbach7ce05792011-08-03 23:50:40 +00001447 }
1448
Jim Grosbach60f91a32011-08-19 17:55:24 +00001449 void addMemThumbRIs4Operands(MCInst &Inst, unsigned N) const {
1450 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001451 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1452 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbach60f91a32011-08-19 17:55:24 +00001453 Inst.addOperand(MCOperand::CreateImm(Val));
1454 }
1455
Jim Grosbach38466302011-08-19 18:55:51 +00001456 void addMemThumbRIs2Operands(MCInst &Inst, unsigned N) const {
1457 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001458 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 2) : 0;
1459 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbach38466302011-08-19 18:55:51 +00001460 Inst.addOperand(MCOperand::CreateImm(Val));
1461 }
1462
Jim Grosbach48ff5ff2011-08-19 18:49:59 +00001463 void addMemThumbRIs1Operands(MCInst &Inst, unsigned N) const {
1464 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001465 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue()) : 0;
1466 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbach48ff5ff2011-08-19 18:49:59 +00001467 Inst.addOperand(MCOperand::CreateImm(Val));
1468 }
1469
Jim Grosbachecd85892011-08-19 18:13:48 +00001470 void addMemThumbSPIOperands(MCInst &Inst, unsigned N) const {
1471 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001472 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1473 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbachecd85892011-08-19 18:13:48 +00001474 Inst.addOperand(MCOperand::CreateImm(Val));
1475 }
1476
Jim Grosbach7ce05792011-08-03 23:50:40 +00001477 void addPostIdxImm8Operands(MCInst &Inst, unsigned N) const {
1478 assert(N == 1 && "Invalid number of operands!");
1479 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1480 assert(CE && "non-constant post-idx-imm8 operand!");
1481 int Imm = CE->getValue();
1482 bool isAdd = Imm >= 0;
Owen Anderson63553c72011-08-29 17:17:09 +00001483 if (Imm == INT32_MIN) Imm = 0;
Jim Grosbach7ce05792011-08-03 23:50:40 +00001484 Imm = (Imm < 0 ? -Imm : Imm) | (int)isAdd << 8;
1485 Inst.addOperand(MCOperand::CreateImm(Imm));
1486 }
1487
Jim Grosbach2bd01182011-10-11 21:55:36 +00001488 void addPostIdxImm8s4Operands(MCInst &Inst, unsigned N) const {
1489 assert(N == 1 && "Invalid number of operands!");
1490 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1491 assert(CE && "non-constant post-idx-imm8s4 operand!");
1492 int Imm = CE->getValue();
1493 bool isAdd = Imm >= 0;
1494 if (Imm == INT32_MIN) Imm = 0;
1495 // Immediate is scaled by 4.
1496 Imm = ((Imm < 0 ? -Imm : Imm) / 4) | (int)isAdd << 8;
1497 Inst.addOperand(MCOperand::CreateImm(Imm));
1498 }
1499
Jim Grosbach7ce05792011-08-03 23:50:40 +00001500 void addPostIdxRegOperands(MCInst &Inst, unsigned N) const {
1501 assert(N == 2 && "Invalid number of operands!");
1502 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00001503 Inst.addOperand(MCOperand::CreateImm(PostIdxReg.isAdd));
1504 }
1505
1506 void addPostIdxRegShiftedOperands(MCInst &Inst, unsigned N) const {
1507 assert(N == 2 && "Invalid number of operands!");
1508 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1509 // The sign, shift type, and shift amount are encoded in a single operand
1510 // using the AM2 encoding helpers.
1511 ARM_AM::AddrOpc opc = PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub;
1512 unsigned Imm = ARM_AM::getAM2Opc(opc, PostIdxReg.ShiftImm,
1513 PostIdxReg.ShiftTy);
1514 Inst.addOperand(MCOperand::CreateImm(Imm));
Bill Wendlingef4a68b2010-11-30 07:44:32 +00001515 }
1516
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001517 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
1518 assert(N == 1 && "Invalid number of operands!");
1519 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
1520 }
1521
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001522 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
1523 assert(N == 1 && "Invalid number of operands!");
1524 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
1525 }
1526
Jim Grosbach862019c2011-10-18 23:02:30 +00001527 void addVecListOneDOperands(MCInst &Inst, unsigned N) const {
1528 assert(N == 1 && "Invalid number of operands!");
1529 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1530 }
1531
Jim Grosbach280dfad2011-10-21 18:54:25 +00001532 void addVecListTwoDOperands(MCInst &Inst, unsigned N) const {
1533 assert(N == 1 && "Invalid number of operands!");
1534 // Only the first register actually goes on the instruction. The rest
1535 // are implied by the opcode.
1536 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1537 }
1538
Jim Grosbachcdcfa282011-10-21 20:02:19 +00001539 void addVecListThreeDOperands(MCInst &Inst, unsigned N) const {
1540 assert(N == 1 && "Invalid number of operands!");
1541 // Only the first register actually goes on the instruction. The rest
1542 // are implied by the opcode.
1543 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1544 }
1545
Jim Grosbachb6310312011-10-21 20:35:01 +00001546 void addVecListFourDOperands(MCInst &Inst, unsigned N) const {
1547 assert(N == 1 && "Invalid number of operands!");
1548 // Only the first register actually goes on the instruction. The rest
1549 // are implied by the opcode.
1550 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1551 }
1552
Jim Grosbach4661d4c2011-10-21 22:21:10 +00001553 void addVecListTwoQOperands(MCInst &Inst, unsigned N) const {
1554 assert(N == 1 && "Invalid number of operands!");
1555 // Only the first register actually goes on the instruction. The rest
1556 // are implied by the opcode.
1557 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1558 }
1559
Jim Grosbach460a9052011-10-07 23:56:00 +00001560 void addVectorIndex8Operands(MCInst &Inst, unsigned N) const {
1561 assert(N == 1 && "Invalid number of operands!");
1562 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1563 }
1564
1565 void addVectorIndex16Operands(MCInst &Inst, unsigned N) const {
1566 assert(N == 1 && "Invalid number of operands!");
1567 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1568 }
1569
1570 void addVectorIndex32Operands(MCInst &Inst, unsigned N) const {
1571 assert(N == 1 && "Invalid number of operands!");
1572 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1573 }
1574
Jim Grosbach0e387b22011-10-17 22:26:03 +00001575 void addNEONi8splatOperands(MCInst &Inst, unsigned N) const {
1576 assert(N == 1 && "Invalid number of operands!");
1577 // The immediate encodes the type of constant as well as the value.
1578 // Mask in that this is an i8 splat.
1579 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1580 Inst.addOperand(MCOperand::CreateImm(CE->getValue() | 0xe00));
1581 }
1582
Jim Grosbachea461102011-10-17 23:09:09 +00001583 void addNEONi16splatOperands(MCInst &Inst, unsigned N) const {
1584 assert(N == 1 && "Invalid number of operands!");
1585 // The immediate encodes the type of constant as well as the value.
1586 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1587 unsigned Value = CE->getValue();
1588 if (Value >= 256)
1589 Value = (Value >> 8) | 0xa00;
1590 else
1591 Value |= 0x800;
1592 Inst.addOperand(MCOperand::CreateImm(Value));
1593 }
1594
Jim Grosbach6248a542011-10-18 00:22:00 +00001595 void addNEONi32splatOperands(MCInst &Inst, unsigned N) const {
1596 assert(N == 1 && "Invalid number of operands!");
1597 // The immediate encodes the type of constant as well as the value.
1598 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1599 unsigned Value = CE->getValue();
1600 if (Value >= 256 && Value <= 0xff00)
1601 Value = (Value >> 8) | 0x200;
1602 else if (Value > 0xffff && Value <= 0xff0000)
1603 Value = (Value >> 16) | 0x400;
1604 else if (Value > 0xffffff)
1605 Value = (Value >> 24) | 0x600;
1606 Inst.addOperand(MCOperand::CreateImm(Value));
1607 }
1608
1609 void addNEONi32vmovOperands(MCInst &Inst, unsigned N) const {
1610 assert(N == 1 && "Invalid number of operands!");
1611 // The immediate encodes the type of constant as well as the value.
1612 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1613 unsigned Value = CE->getValue();
1614 if (Value >= 256 && Value <= 0xffff)
1615 Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
1616 else if (Value > 0xffff && Value <= 0xffffff)
1617 Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
1618 else if (Value > 0xffffff)
1619 Value = (Value >> 24) | 0x600;
1620 Inst.addOperand(MCOperand::CreateImm(Value));
1621 }
1622
Jim Grosbachf2f5bc62011-10-18 16:18:11 +00001623 void addNEONi64splatOperands(MCInst &Inst, unsigned N) const {
1624 assert(N == 1 && "Invalid number of operands!");
1625 // The immediate encodes the type of constant as well as the value.
1626 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1627 uint64_t Value = CE->getValue();
1628 unsigned Imm = 0;
1629 for (unsigned i = 0; i < 8; ++i, Value >>= 8) {
1630 Imm |= (Value & 1) << i;
1631 }
1632 Inst.addOperand(MCOperand::CreateImm(Imm | 0x1e00));
1633 }
1634
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001635 virtual void print(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +00001636
Jim Grosbach89df9962011-08-26 21:43:41 +00001637 static ARMOperand *CreateITMask(unsigned Mask, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001638 ARMOperand *Op = new ARMOperand(k_ITCondMask);
Jim Grosbach89df9962011-08-26 21:43:41 +00001639 Op->ITMask.Mask = Mask;
1640 Op->StartLoc = S;
1641 Op->EndLoc = S;
1642 return Op;
1643 }
1644
Chris Lattner3a697562010-10-28 17:20:03 +00001645 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001646 ARMOperand *Op = new ARMOperand(k_CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001647 Op->CC.Val = CC;
1648 Op->StartLoc = S;
1649 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +00001650 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001651 }
1652
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001653 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001654 ARMOperand *Op = new ARMOperand(k_CoprocNum);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001655 Op->Cop.Val = CopVal;
1656 Op->StartLoc = S;
1657 Op->EndLoc = S;
1658 return Op;
1659 }
1660
1661 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001662 ARMOperand *Op = new ARMOperand(k_CoprocReg);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001663 Op->Cop.Val = CopVal;
1664 Op->StartLoc = S;
1665 Op->EndLoc = S;
1666 return Op;
1667 }
1668
Jim Grosbach9b8f2a02011-10-12 17:34:41 +00001669 static ARMOperand *CreateCoprocOption(unsigned Val, SMLoc S, SMLoc E) {
1670 ARMOperand *Op = new ARMOperand(k_CoprocOption);
1671 Op->Cop.Val = Val;
1672 Op->StartLoc = S;
1673 Op->EndLoc = E;
1674 return Op;
1675 }
1676
Jim Grosbachd67641b2010-12-06 18:21:12 +00001677 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001678 ARMOperand *Op = new ARMOperand(k_CCOut);
Jim Grosbachd67641b2010-12-06 18:21:12 +00001679 Op->Reg.RegNum = RegNum;
1680 Op->StartLoc = S;
1681 Op->EndLoc = S;
1682 return Op;
1683 }
1684
Chris Lattner3a697562010-10-28 17:20:03 +00001685 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001686 ARMOperand *Op = new ARMOperand(k_Token);
Sean Callanan76264762010-04-02 22:27:05 +00001687 Op->Tok.Data = Str.data();
1688 Op->Tok.Length = Str.size();
1689 Op->StartLoc = S;
1690 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +00001691 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001692 }
1693
Bill Wendling50d0f582010-11-18 23:43:05 +00001694 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001695 ARMOperand *Op = new ARMOperand(k_Register);
Sean Callanan76264762010-04-02 22:27:05 +00001696 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +00001697 Op->StartLoc = S;
1698 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +00001699 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001700 }
1701
Jim Grosbache8606dc2011-07-13 17:50:29 +00001702 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
1703 unsigned SrcReg,
1704 unsigned ShiftReg,
1705 unsigned ShiftImm,
1706 SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001707 ARMOperand *Op = new ARMOperand(k_ShiftedRegister);
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001708 Op->RegShiftedReg.ShiftTy = ShTy;
1709 Op->RegShiftedReg.SrcReg = SrcReg;
1710 Op->RegShiftedReg.ShiftReg = ShiftReg;
1711 Op->RegShiftedReg.ShiftImm = ShiftImm;
Jim Grosbache8606dc2011-07-13 17:50:29 +00001712 Op->StartLoc = S;
1713 Op->EndLoc = E;
1714 return Op;
1715 }
1716
Owen Anderson92a20222011-07-21 18:54:16 +00001717 static ARMOperand *CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy,
1718 unsigned SrcReg,
1719 unsigned ShiftImm,
1720 SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001721 ARMOperand *Op = new ARMOperand(k_ShiftedImmediate);
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001722 Op->RegShiftedImm.ShiftTy = ShTy;
1723 Op->RegShiftedImm.SrcReg = SrcReg;
1724 Op->RegShiftedImm.ShiftImm = ShiftImm;
Owen Anderson92a20222011-07-21 18:54:16 +00001725 Op->StartLoc = S;
1726 Op->EndLoc = E;
1727 return Op;
1728 }
1729
Jim Grosbach580f4a92011-07-25 22:20:28 +00001730 static ARMOperand *CreateShifterImm(bool isASR, unsigned Imm,
Owen Anderson00828302011-03-18 22:50:18 +00001731 SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001732 ARMOperand *Op = new ARMOperand(k_ShifterImmediate);
Jim Grosbach580f4a92011-07-25 22:20:28 +00001733 Op->ShifterImm.isASR = isASR;
1734 Op->ShifterImm.Imm = Imm;
Owen Anderson00828302011-03-18 22:50:18 +00001735 Op->StartLoc = S;
1736 Op->EndLoc = E;
1737 return Op;
1738 }
1739
Jim Grosbach7e1547e2011-07-27 20:15:40 +00001740 static ARMOperand *CreateRotImm(unsigned Imm, SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001741 ARMOperand *Op = new ARMOperand(k_RotateImmediate);
Jim Grosbach7e1547e2011-07-27 20:15:40 +00001742 Op->RotImm.Imm = Imm;
1743 Op->StartLoc = S;
1744 Op->EndLoc = E;
1745 return Op;
1746 }
1747
Jim Grosbach293a2ee2011-07-28 21:34:26 +00001748 static ARMOperand *CreateBitfield(unsigned LSB, unsigned Width,
1749 SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001750 ARMOperand *Op = new ARMOperand(k_BitfieldDescriptor);
Jim Grosbach293a2ee2011-07-28 21:34:26 +00001751 Op->Bitfield.LSB = LSB;
1752 Op->Bitfield.Width = Width;
1753 Op->StartLoc = S;
1754 Op->EndLoc = E;
1755 return Op;
1756 }
1757
Bill Wendling7729e062010-11-09 22:44:22 +00001758 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +00001759 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +00001760 SMLoc StartLoc, SMLoc EndLoc) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001761 KindTy Kind = k_RegisterList;
Bill Wendling0f630752010-11-17 04:32:08 +00001762
Jim Grosbachd300b942011-09-13 22:56:44 +00001763 if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Regs.front().first))
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001764 Kind = k_DPRRegisterList;
Jim Grosbachd300b942011-09-13 22:56:44 +00001765 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].
Evan Cheng275944a2011-07-25 21:32:49 +00001766 contains(Regs.front().first))
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001767 Kind = k_SPRRegisterList;
Bill Wendling0f630752010-11-17 04:32:08 +00001768
1769 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +00001770 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +00001771 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +00001772 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +00001773 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +00001774 Op->StartLoc = StartLoc;
1775 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +00001776 return Op;
1777 }
1778
Jim Grosbach862019c2011-10-18 23:02:30 +00001779 static ARMOperand *CreateVectorList(unsigned RegNum, unsigned Count,
1780 SMLoc S, SMLoc E) {
1781 ARMOperand *Op = new ARMOperand(k_VectorList);
1782 Op->VectorList.RegNum = RegNum;
1783 Op->VectorList.Count = Count;
1784 Op->StartLoc = S;
1785 Op->EndLoc = E;
1786 return Op;
1787 }
1788
Jim Grosbach460a9052011-10-07 23:56:00 +00001789 static ARMOperand *CreateVectorIndex(unsigned Idx, SMLoc S, SMLoc E,
1790 MCContext &Ctx) {
1791 ARMOperand *Op = new ARMOperand(k_VectorIndex);
1792 Op->VectorIndex.Val = Idx;
1793 Op->StartLoc = S;
1794 Op->EndLoc = E;
1795 return Op;
1796 }
1797
Chris Lattner3a697562010-10-28 17:20:03 +00001798 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001799 ARMOperand *Op = new ARMOperand(k_Immediate);
Sean Callanan76264762010-04-02 22:27:05 +00001800 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +00001801 Op->StartLoc = S;
1802 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +00001803 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +00001804 }
1805
Jim Grosbach9d390362011-10-03 23:38:36 +00001806 static ARMOperand *CreateFPImm(unsigned Val, SMLoc S, MCContext &Ctx) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001807 ARMOperand *Op = new ARMOperand(k_FPImmediate);
Jim Grosbach9d390362011-10-03 23:38:36 +00001808 Op->FPImm.Val = Val;
1809 Op->StartLoc = S;
1810 Op->EndLoc = S;
1811 return Op;
1812 }
1813
Jim Grosbach7ce05792011-08-03 23:50:40 +00001814 static ARMOperand *CreateMem(unsigned BaseRegNum,
1815 const MCConstantExpr *OffsetImm,
1816 unsigned OffsetRegNum,
1817 ARM_AM::ShiftOpc ShiftType,
Jim Grosbach0d6fac32011-08-05 22:03:36 +00001818 unsigned ShiftImm,
Jim Grosbach57dcb852011-10-11 17:29:55 +00001819 unsigned Alignment,
Jim Grosbach7ce05792011-08-03 23:50:40 +00001820 bool isNegative,
Chris Lattner3a697562010-10-28 17:20:03 +00001821 SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001822 ARMOperand *Op = new ARMOperand(k_Memory);
Jim Grosbache53c87b2011-10-11 15:59:20 +00001823 Op->Memory.BaseRegNum = BaseRegNum;
1824 Op->Memory.OffsetImm = OffsetImm;
1825 Op->Memory.OffsetRegNum = OffsetRegNum;
1826 Op->Memory.ShiftType = ShiftType;
1827 Op->Memory.ShiftImm = ShiftImm;
Jim Grosbach57dcb852011-10-11 17:29:55 +00001828 Op->Memory.Alignment = Alignment;
Jim Grosbache53c87b2011-10-11 15:59:20 +00001829 Op->Memory.isNegative = isNegative;
Jim Grosbach7ce05792011-08-03 23:50:40 +00001830 Op->StartLoc = S;
1831 Op->EndLoc = E;
1832 return Op;
1833 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001834
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00001835 static ARMOperand *CreatePostIdxReg(unsigned RegNum, bool isAdd,
1836 ARM_AM::ShiftOpc ShiftTy,
1837 unsigned ShiftImm,
Jim Grosbach7ce05792011-08-03 23:50:40 +00001838 SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001839 ARMOperand *Op = new ARMOperand(k_PostIndexRegister);
Jim Grosbach7ce05792011-08-03 23:50:40 +00001840 Op->PostIdxReg.RegNum = RegNum;
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00001841 Op->PostIdxReg.isAdd = isAdd;
1842 Op->PostIdxReg.ShiftTy = ShiftTy;
1843 Op->PostIdxReg.ShiftImm = ShiftImm;
Sean Callanan76264762010-04-02 22:27:05 +00001844 Op->StartLoc = S;
1845 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +00001846 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001847 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001848
1849 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001850 ARMOperand *Op = new ARMOperand(k_MemBarrierOpt);
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001851 Op->MBOpt.Val = Opt;
1852 Op->StartLoc = S;
1853 Op->EndLoc = S;
1854 return Op;
1855 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001856
1857 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001858 ARMOperand *Op = new ARMOperand(k_ProcIFlags);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001859 Op->IFlags.Val = IFlags;
1860 Op->StartLoc = S;
1861 Op->EndLoc = S;
1862 return Op;
1863 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001864
1865 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001866 ARMOperand *Op = new ARMOperand(k_MSRMask);
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001867 Op->MMask.Val = MMask;
1868 Op->StartLoc = S;
1869 Op->EndLoc = S;
1870 return Op;
1871 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001872};
1873
1874} // end anonymous namespace.
1875
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001876void ARMOperand::print(raw_ostream &OS) const {
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001877 switch (Kind) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001878 case k_FPImmediate:
Jim Grosbach9d390362011-10-03 23:38:36 +00001879 OS << "<fpimm " << getFPImm() << "(" << ARM_AM::getFPImmFloat(getFPImm())
1880 << ") >";
1881 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001882 case k_CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +00001883 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001884 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001885 case k_CCOut:
Jim Grosbachd67641b2010-12-06 18:21:12 +00001886 OS << "<ccout " << getReg() << ">";
1887 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001888 case k_ITCondMask: {
Jim Grosbach89df9962011-08-26 21:43:41 +00001889 static char MaskStr[][6] = { "()", "(t)", "(e)", "(tt)", "(et)", "(te)",
1890 "(ee)", "(ttt)", "(ett)", "(tet)", "(eet)", "(tte)", "(ete)",
1891 "(tee)", "(eee)" };
1892 assert((ITMask.Mask & 0xf) == ITMask.Mask);
1893 OS << "<it-mask " << MaskStr[ITMask.Mask] << ">";
1894 break;
1895 }
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001896 case k_CoprocNum:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001897 OS << "<coprocessor number: " << getCoproc() << ">";
1898 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001899 case k_CoprocReg:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001900 OS << "<coprocessor register: " << getCoproc() << ">";
1901 break;
Jim Grosbach9b8f2a02011-10-12 17:34:41 +00001902 case k_CoprocOption:
1903 OS << "<coprocessor option: " << CoprocOption.Val << ">";
1904 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001905 case k_MSRMask:
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001906 OS << "<mask: " << getMSRMask() << ">";
1907 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001908 case k_Immediate:
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001909 getImm()->print(OS);
1910 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001911 case k_MemBarrierOpt:
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001912 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
1913 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001914 case k_Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +00001915 OS << "<memory "
Jim Grosbache53c87b2011-10-11 15:59:20 +00001916 << " base:" << Memory.BaseRegNum;
Daniel Dunbar6ec56202011-01-18 05:55:21 +00001917 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001918 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001919 case k_PostIndexRegister:
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00001920 OS << "post-idx register " << (PostIdxReg.isAdd ? "" : "-")
1921 << PostIdxReg.RegNum;
1922 if (PostIdxReg.ShiftTy != ARM_AM::no_shift)
1923 OS << ARM_AM::getShiftOpcStr(PostIdxReg.ShiftTy) << " "
1924 << PostIdxReg.ShiftImm;
1925 OS << ">";
Jim Grosbach7ce05792011-08-03 23:50:40 +00001926 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001927 case k_ProcIFlags: {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001928 OS << "<ARM_PROC::";
1929 unsigned IFlags = getProcIFlags();
1930 for (int i=2; i >= 0; --i)
1931 if (IFlags & (1 << i))
1932 OS << ARM_PROC::IFlagsToString(1 << i);
1933 OS << ">";
1934 break;
1935 }
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001936 case k_Register:
Bill Wendling50d0f582010-11-18 23:43:05 +00001937 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001938 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001939 case k_ShifterImmediate:
Jim Grosbach580f4a92011-07-25 22:20:28 +00001940 OS << "<shift " << (ShifterImm.isASR ? "asr" : "lsl")
1941 << " #" << ShifterImm.Imm << ">";
Jim Grosbache8606dc2011-07-13 17:50:29 +00001942 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001943 case k_ShiftedRegister:
Owen Anderson92a20222011-07-21 18:54:16 +00001944 OS << "<so_reg_reg "
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001945 << RegShiftedReg.SrcReg
1946 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(RegShiftedReg.ShiftImm))
1947 << ", " << RegShiftedReg.ShiftReg << ", "
1948 << ARM_AM::getSORegOffset(RegShiftedReg.ShiftImm)
Jim Grosbache8606dc2011-07-13 17:50:29 +00001949 << ">";
Owen Anderson00828302011-03-18 22:50:18 +00001950 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001951 case k_ShiftedImmediate:
Owen Anderson92a20222011-07-21 18:54:16 +00001952 OS << "<so_reg_imm "
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001953 << RegShiftedImm.SrcReg
1954 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(RegShiftedImm.ShiftImm))
1955 << ", " << ARM_AM::getSORegOffset(RegShiftedImm.ShiftImm)
Owen Anderson92a20222011-07-21 18:54:16 +00001956 << ">";
1957 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001958 case k_RotateImmediate:
Jim Grosbach7e1547e2011-07-27 20:15:40 +00001959 OS << "<ror " << " #" << (RotImm.Imm * 8) << ">";
1960 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001961 case k_BitfieldDescriptor:
Jim Grosbach293a2ee2011-07-28 21:34:26 +00001962 OS << "<bitfield " << "lsb: " << Bitfield.LSB
1963 << ", width: " << Bitfield.Width << ">";
1964 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001965 case k_RegisterList:
1966 case k_DPRRegisterList:
1967 case k_SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +00001968 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001969
Bill Wendling5fa22a12010-11-09 23:28:44 +00001970 const SmallVectorImpl<unsigned> &RegList = getRegList();
1971 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +00001972 I = RegList.begin(), E = RegList.end(); I != E; ) {
1973 OS << *I;
1974 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001975 }
1976
1977 OS << ">";
1978 break;
1979 }
Jim Grosbach862019c2011-10-18 23:02:30 +00001980 case k_VectorList:
1981 OS << "<vector_list " << VectorList.Count << " * "
1982 << VectorList.RegNum << ">";
1983 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001984 case k_Token:
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001985 OS << "'" << getToken() << "'";
1986 break;
Jim Grosbach460a9052011-10-07 23:56:00 +00001987 case k_VectorIndex:
1988 OS << "<vectorindex " << getVectorIndex() << ">";
1989 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001990 }
1991}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001992
1993/// @name Auto-generated Match Functions
1994/// {
1995
1996static unsigned MatchRegisterName(StringRef Name);
1997
1998/// }
1999
Bob Wilson69df7232011-02-03 21:46:10 +00002000bool ARMAsmParser::ParseRegister(unsigned &RegNo,
2001 SMLoc &StartLoc, SMLoc &EndLoc) {
Jim Grosbach1355cf12011-07-26 17:10:22 +00002002 RegNo = tryParseRegister();
Roman Divackybf755322011-01-27 17:14:22 +00002003
2004 return (RegNo == (unsigned)-1);
2005}
2006
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002007/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +00002008/// and if it is a register name the token is eaten and the register number is
2009/// returned. Otherwise return -1.
2010///
Jim Grosbach1355cf12011-07-26 17:10:22 +00002011int ARMAsmParser::tryParseRegister() {
Chris Lattnere5658fa2010-10-30 04:09:10 +00002012 const AsmToken &Tok = Parser.getTok();
Jim Grosbach7ce05792011-08-03 23:50:40 +00002013 if (Tok.isNot(AsmToken::Identifier)) return -1;
Jim Grosbachd4462a52010-11-01 16:44:21 +00002014
Chris Lattnere5658fa2010-10-30 04:09:10 +00002015 // FIXME: Validate register for the current architecture; we have to do
2016 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +00002017 std::string upperCase = Tok.getString().str();
2018 std::string lowerCase = LowercaseString(upperCase);
2019 unsigned RegNum = MatchRegisterName(lowerCase);
2020 if (!RegNum) {
2021 RegNum = StringSwitch<unsigned>(lowerCase)
2022 .Case("r13", ARM::SP)
2023 .Case("r14", ARM::LR)
2024 .Case("r15", ARM::PC)
2025 .Case("ip", ARM::R12)
2026 .Default(0);
2027 }
2028 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +00002029
Chris Lattnere5658fa2010-10-30 04:09:10 +00002030 Parser.Lex(); // Eat identifier token.
Jim Grosbach460a9052011-10-07 23:56:00 +00002031
Chris Lattnere5658fa2010-10-30 04:09:10 +00002032 return RegNum;
2033}
Jim Grosbachd4462a52010-11-01 16:44:21 +00002034
Jim Grosbach19906722011-07-13 18:49:30 +00002035// Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
2036// If a recoverable error occurs, return 1. If an irrecoverable error
2037// occurs, return -1. An irrecoverable error is one where tokens have been
2038// consumed in the process of trying to parse the shifter (i.e., when it is
2039// indeed a shifter operand, but malformed).
Jim Grosbach0d87ec22011-07-26 20:41:24 +00002040int ARMAsmParser::tryParseShiftRegister(
Owen Anderson00828302011-03-18 22:50:18 +00002041 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2042 SMLoc S = Parser.getTok().getLoc();
2043 const AsmToken &Tok = Parser.getTok();
2044 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2045
2046 std::string upperCase = Tok.getString().str();
2047 std::string lowerCase = LowercaseString(upperCase);
2048 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
2049 .Case("lsl", ARM_AM::lsl)
2050 .Case("lsr", ARM_AM::lsr)
2051 .Case("asr", ARM_AM::asr)
2052 .Case("ror", ARM_AM::ror)
2053 .Case("rrx", ARM_AM::rrx)
2054 .Default(ARM_AM::no_shift);
2055
2056 if (ShiftTy == ARM_AM::no_shift)
Jim Grosbach19906722011-07-13 18:49:30 +00002057 return 1;
Owen Anderson00828302011-03-18 22:50:18 +00002058
Jim Grosbache8606dc2011-07-13 17:50:29 +00002059 Parser.Lex(); // Eat the operator.
Owen Anderson00828302011-03-18 22:50:18 +00002060
Jim Grosbache8606dc2011-07-13 17:50:29 +00002061 // The source register for the shift has already been added to the
2062 // operand list, so we need to pop it off and combine it into the shifted
2063 // register operand instead.
Benjamin Kramereac07962011-07-14 18:41:22 +00002064 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
Jim Grosbache8606dc2011-07-13 17:50:29 +00002065 if (!PrevOp->isReg())
2066 return Error(PrevOp->getStartLoc(), "shift must be of a register");
2067 int SrcReg = PrevOp->getReg();
2068 int64_t Imm = 0;
2069 int ShiftReg = 0;
2070 if (ShiftTy == ARM_AM::rrx) {
2071 // RRX Doesn't have an explicit shift amount. The encoder expects
2072 // the shift register to be the same as the source register. Seems odd,
2073 // but OK.
2074 ShiftReg = SrcReg;
2075 } else {
2076 // Figure out if this is shifted by a constant or a register (for non-RRX).
2077 if (Parser.getTok().is(AsmToken::Hash)) {
2078 Parser.Lex(); // Eat hash.
2079 SMLoc ImmLoc = Parser.getTok().getLoc();
2080 const MCExpr *ShiftExpr = 0;
Jim Grosbach19906722011-07-13 18:49:30 +00002081 if (getParser().ParseExpression(ShiftExpr)) {
2082 Error(ImmLoc, "invalid immediate shift value");
2083 return -1;
2084 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00002085 // The expression must be evaluatable as an immediate.
2086 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
Jim Grosbach19906722011-07-13 18:49:30 +00002087 if (!CE) {
2088 Error(ImmLoc, "invalid immediate shift value");
2089 return -1;
2090 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00002091 // Range check the immediate.
2092 // lsl, ror: 0 <= imm <= 31
2093 // lsr, asr: 0 <= imm <= 32
2094 Imm = CE->getValue();
2095 if (Imm < 0 ||
2096 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
2097 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
Jim Grosbach19906722011-07-13 18:49:30 +00002098 Error(ImmLoc, "immediate shift value out of range");
2099 return -1;
Jim Grosbache8606dc2011-07-13 17:50:29 +00002100 }
2101 } else if (Parser.getTok().is(AsmToken::Identifier)) {
Jim Grosbach1355cf12011-07-26 17:10:22 +00002102 ShiftReg = tryParseRegister();
Jim Grosbache8606dc2011-07-13 17:50:29 +00002103 SMLoc L = Parser.getTok().getLoc();
Jim Grosbach19906722011-07-13 18:49:30 +00002104 if (ShiftReg == -1) {
2105 Error (L, "expected immediate or register in shift operand");
2106 return -1;
2107 }
2108 } else {
2109 Error (Parser.getTok().getLoc(),
Jim Grosbache8606dc2011-07-13 17:50:29 +00002110 "expected immediate or register in shift operand");
Jim Grosbach19906722011-07-13 18:49:30 +00002111 return -1;
2112 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00002113 }
2114
Owen Anderson92a20222011-07-21 18:54:16 +00002115 if (ShiftReg && ShiftTy != ARM_AM::rrx)
2116 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
Jim Grosbachaf6981f2011-07-25 20:49:51 +00002117 ShiftReg, Imm,
Owen Anderson00828302011-03-18 22:50:18 +00002118 S, Parser.getTok().getLoc()));
Owen Anderson92a20222011-07-21 18:54:16 +00002119 else
2120 Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
2121 S, Parser.getTok().getLoc()));
Owen Anderson00828302011-03-18 22:50:18 +00002122
Jim Grosbach19906722011-07-13 18:49:30 +00002123 return 0;
Owen Anderson00828302011-03-18 22:50:18 +00002124}
2125
2126
Bill Wendling50d0f582010-11-18 23:43:05 +00002127/// Try to parse a register name. The token must be an Identifier when called.
2128/// If it's a register, an AsmOperand is created. Another AsmOperand is created
2129/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +00002130///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002131/// TODO this is likely to change to allow different register types and or to
2132/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +00002133bool ARMAsmParser::
Jim Grosbach1355cf12011-07-26 17:10:22 +00002134tryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00002135 SMLoc S = Parser.getTok().getLoc();
Jim Grosbach1355cf12011-07-26 17:10:22 +00002136 int RegNo = tryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +00002137 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +00002138 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +00002139
Bill Wendling50d0f582010-11-18 23:43:05 +00002140 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002141
Chris Lattnere5658fa2010-10-30 04:09:10 +00002142 const AsmToken &ExclaimTok = Parser.getTok();
2143 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00002144 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
2145 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +00002146 Parser.Lex(); // Eat exclaim token
Jim Grosbach460a9052011-10-07 23:56:00 +00002147 return false;
2148 }
2149
2150 // Also check for an index operand. This is only legal for vector registers,
2151 // but that'll get caught OK in operand matching, so we don't need to
2152 // explicitly filter everything else out here.
2153 if (Parser.getTok().is(AsmToken::LBrac)) {
2154 SMLoc SIdx = Parser.getTok().getLoc();
2155 Parser.Lex(); // Eat left bracket token.
2156
2157 const MCExpr *ImmVal;
Jim Grosbach460a9052011-10-07 23:56:00 +00002158 if (getParser().ParseExpression(ImmVal))
2159 return MatchOperand_ParseFail;
2160 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
2161 if (!MCE) {
2162 TokError("immediate value expected for vector index");
2163 return MatchOperand_ParseFail;
2164 }
2165
2166 SMLoc E = Parser.getTok().getLoc();
2167 if (Parser.getTok().isNot(AsmToken::RBrac)) {
2168 Error(E, "']' expected");
2169 return MatchOperand_ParseFail;
2170 }
2171
2172 Parser.Lex(); // Eat right bracket token.
2173
2174 Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
2175 SIdx, E,
2176 getContext()));
Kevin Enderby99e6d4e2009-10-07 18:01:35 +00002177 }
2178
Bill Wendling50d0f582010-11-18 23:43:05 +00002179 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002180}
2181
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002182/// MatchCoprocessorOperandName - Try to parse an coprocessor related
2183/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
2184/// "c5", ...
2185static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002186 // Use the same layout as the tablegen'erated register name matcher. Ugly,
2187 // but efficient.
2188 switch (Name.size()) {
2189 default: break;
2190 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002191 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002192 return -1;
2193 switch (Name[1]) {
2194 default: return -1;
2195 case '0': return 0;
2196 case '1': return 1;
2197 case '2': return 2;
2198 case '3': return 3;
2199 case '4': return 4;
2200 case '5': return 5;
2201 case '6': return 6;
2202 case '7': return 7;
2203 case '8': return 8;
2204 case '9': return 9;
2205 }
2206 break;
2207 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002208 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002209 return -1;
2210 switch (Name[2]) {
2211 default: return -1;
2212 case '0': return 10;
2213 case '1': return 11;
2214 case '2': return 12;
2215 case '3': return 13;
2216 case '4': return 14;
2217 case '5': return 15;
2218 }
2219 break;
2220 }
2221
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002222 return -1;
2223}
2224
Jim Grosbach89df9962011-08-26 21:43:41 +00002225/// parseITCondCode - Try to parse a condition code for an IT instruction.
2226ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2227parseITCondCode(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2228 SMLoc S = Parser.getTok().getLoc();
2229 const AsmToken &Tok = Parser.getTok();
2230 if (!Tok.is(AsmToken::Identifier))
2231 return MatchOperand_NoMatch;
2232 unsigned CC = StringSwitch<unsigned>(Tok.getString())
2233 .Case("eq", ARMCC::EQ)
2234 .Case("ne", ARMCC::NE)
2235 .Case("hs", ARMCC::HS)
2236 .Case("cs", ARMCC::HS)
2237 .Case("lo", ARMCC::LO)
2238 .Case("cc", ARMCC::LO)
2239 .Case("mi", ARMCC::MI)
2240 .Case("pl", ARMCC::PL)
2241 .Case("vs", ARMCC::VS)
2242 .Case("vc", ARMCC::VC)
2243 .Case("hi", ARMCC::HI)
2244 .Case("ls", ARMCC::LS)
2245 .Case("ge", ARMCC::GE)
2246 .Case("lt", ARMCC::LT)
2247 .Case("gt", ARMCC::GT)
2248 .Case("le", ARMCC::LE)
2249 .Case("al", ARMCC::AL)
2250 .Default(~0U);
2251 if (CC == ~0U)
2252 return MatchOperand_NoMatch;
2253 Parser.Lex(); // Eat the token.
2254
2255 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), S));
2256
2257 return MatchOperand_Success;
2258}
2259
Jim Grosbach43904292011-07-25 20:14:50 +00002260/// parseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002261/// token must be an Identifier when called, and if it is a coprocessor
2262/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00002263ARMAsmParser::OperandMatchResultTy ARMAsmParser::
Jim Grosbach43904292011-07-25 20:14:50 +00002264parseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002265 SMLoc S = Parser.getTok().getLoc();
2266 const AsmToken &Tok = Parser.getTok();
Jim Grosbachc66e7af2011-10-12 20:54:17 +00002267 if (Tok.isNot(AsmToken::Identifier))
2268 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002269
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002270 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002271 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00002272 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002273
2274 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002275 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00002276 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002277}
2278
Jim Grosbach43904292011-07-25 20:14:50 +00002279/// parseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002280/// token must be an Identifier when called, and if it is a coprocessor
2281/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00002282ARMAsmParser::OperandMatchResultTy ARMAsmParser::
Jim Grosbach43904292011-07-25 20:14:50 +00002283parseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002284 SMLoc S = Parser.getTok().getLoc();
2285 const AsmToken &Tok = Parser.getTok();
Jim Grosbachc66e7af2011-10-12 20:54:17 +00002286 if (Tok.isNot(AsmToken::Identifier))
2287 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002288
2289 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
2290 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00002291 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002292
2293 Parser.Lex(); // Eat identifier token.
2294 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00002295 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002296}
2297
Jim Grosbach9b8f2a02011-10-12 17:34:41 +00002298/// parseCoprocOptionOperand - Try to parse an coprocessor option operand.
2299/// coproc_option : '{' imm0_255 '}'
2300ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2301parseCoprocOptionOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2302 SMLoc S = Parser.getTok().getLoc();
2303
2304 // If this isn't a '{', this isn't a coprocessor immediate operand.
2305 if (Parser.getTok().isNot(AsmToken::LCurly))
2306 return MatchOperand_NoMatch;
2307 Parser.Lex(); // Eat the '{'
2308
2309 const MCExpr *Expr;
2310 SMLoc Loc = Parser.getTok().getLoc();
2311 if (getParser().ParseExpression(Expr)) {
2312 Error(Loc, "illegal expression");
2313 return MatchOperand_ParseFail;
2314 }
2315 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
2316 if (!CE || CE->getValue() < 0 || CE->getValue() > 255) {
2317 Error(Loc, "coprocessor option must be an immediate in range [0, 255]");
2318 return MatchOperand_ParseFail;
2319 }
2320 int Val = CE->getValue();
2321
2322 // Check for and consume the closing '}'
2323 if (Parser.getTok().isNot(AsmToken::RCurly))
2324 return MatchOperand_ParseFail;
2325 SMLoc E = Parser.getTok().getLoc();
2326 Parser.Lex(); // Eat the '}'
2327
2328 Operands.push_back(ARMOperand::CreateCoprocOption(Val, S, E));
2329 return MatchOperand_Success;
2330}
2331
Jim Grosbachd0588e22011-09-14 18:08:35 +00002332// For register list parsing, we need to map from raw GPR register numbering
2333// to the enumeration values. The enumeration values aren't sorted by
2334// register number due to our using "sp", "lr" and "pc" as canonical names.
2335static unsigned getNextRegister(unsigned Reg) {
2336 // If this is a GPR, we need to do it manually, otherwise we can rely
2337 // on the sort ordering of the enumeration since the other reg-classes
2338 // are sane.
2339 if (!ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2340 return Reg + 1;
2341 switch(Reg) {
2342 default: assert(0 && "Invalid GPR number!");
2343 case ARM::R0: return ARM::R1; case ARM::R1: return ARM::R2;
2344 case ARM::R2: return ARM::R3; case ARM::R3: return ARM::R4;
2345 case ARM::R4: return ARM::R5; case ARM::R5: return ARM::R6;
2346 case ARM::R6: return ARM::R7; case ARM::R7: return ARM::R8;
2347 case ARM::R8: return ARM::R9; case ARM::R9: return ARM::R10;
2348 case ARM::R10: return ARM::R11; case ARM::R11: return ARM::R12;
2349 case ARM::R12: return ARM::SP; case ARM::SP: return ARM::LR;
2350 case ARM::LR: return ARM::PC; case ARM::PC: return ARM::R0;
2351 }
2352}
2353
2354/// Parse a register list.
Bill Wendling50d0f582010-11-18 23:43:05 +00002355bool ARMAsmParser::
Jim Grosbach1355cf12011-07-26 17:10:22 +00002356parseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00002357 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00002358 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00002359 SMLoc S = Parser.getTok().getLoc();
Jim Grosbachd0588e22011-09-14 18:08:35 +00002360 Parser.Lex(); // Eat '{' token.
2361 SMLoc RegLoc = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00002362
Jim Grosbachd0588e22011-09-14 18:08:35 +00002363 // Check the first register in the list to see what register class
2364 // this is a list of.
2365 int Reg = tryParseRegister();
2366 if (Reg == -1)
2367 return Error(RegLoc, "register expected");
2368
2369 MCRegisterClass *RC;
2370 if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2371 RC = &ARMMCRegisterClasses[ARM::GPRRegClassID];
2372 else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg))
2373 RC = &ARMMCRegisterClasses[ARM::DPRRegClassID];
2374 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg))
2375 RC = &ARMMCRegisterClasses[ARM::SPRRegClassID];
2376 else
2377 return Error(RegLoc, "invalid register in register list");
2378
2379 // The reglist instructions have at most 16 registers, so reserve
2380 // space for that many.
Jim Grosbachd7a2b3b2011-09-13 20:35:57 +00002381 SmallVector<std::pair<unsigned, SMLoc>, 16> Registers;
Jim Grosbachd0588e22011-09-14 18:08:35 +00002382 // Store the first register.
2383 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
Kevin Enderbyd7894f12009-10-09 21:12:28 +00002384
Jim Grosbachd0588e22011-09-14 18:08:35 +00002385 // This starts immediately after the first register token in the list,
2386 // so we can see either a comma or a minus (range separator) as a legal
2387 // next token.
2388 while (Parser.getTok().is(AsmToken::Comma) ||
2389 Parser.getTok().is(AsmToken::Minus)) {
2390 if (Parser.getTok().is(AsmToken::Minus)) {
2391 Parser.Lex(); // Eat the comma.
2392 SMLoc EndLoc = Parser.getTok().getLoc();
2393 int EndReg = tryParseRegister();
2394 if (EndReg == -1)
2395 return Error(EndLoc, "register expected");
2396 // If the register is the same as the start reg, there's nothing
2397 // more to do.
2398 if (Reg == EndReg)
2399 continue;
2400 // The register must be in the same register class as the first.
2401 if (!RC->contains(EndReg))
2402 return Error(EndLoc, "invalid register in register list");
2403 // Ranges must go from low to high.
2404 if (getARMRegisterNumbering(Reg) > getARMRegisterNumbering(EndReg))
2405 return Error(EndLoc, "bad range in register list");
Kevin Enderbyd7894f12009-10-09 21:12:28 +00002406
Jim Grosbachd0588e22011-09-14 18:08:35 +00002407 // Add all the registers in the range to the register list.
2408 while (Reg != EndReg) {
2409 Reg = getNextRegister(Reg);
2410 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2411 }
2412 continue;
2413 }
2414 Parser.Lex(); // Eat the comma.
2415 RegLoc = Parser.getTok().getLoc();
2416 int OldReg = Reg;
2417 Reg = tryParseRegister();
2418 if (Reg == -1)
Jim Grosbach2d539692011-09-12 23:36:42 +00002419 return Error(RegLoc, "register expected");
Jim Grosbachd0588e22011-09-14 18:08:35 +00002420 // The register must be in the same register class as the first.
2421 if (!RC->contains(Reg))
2422 return Error(RegLoc, "invalid register in register list");
2423 // List must be monotonically increasing.
2424 if (getARMRegisterNumbering(Reg) <= getARMRegisterNumbering(OldReg))
2425 return Error(RegLoc, "register list not in ascending order");
2426 // VFP register lists must also be contiguous.
2427 // It's OK to use the enumeration values directly here rather, as the
2428 // VFP register classes have the enum sorted properly.
2429 if (RC != &ARMMCRegisterClasses[ARM::GPRRegClassID] &&
2430 Reg != OldReg + 1)
2431 return Error(RegLoc, "non-contiguous register range");
2432 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
Bill Wendlinge7176102010-11-06 22:36:58 +00002433 }
2434
Jim Grosbachd0588e22011-09-14 18:08:35 +00002435 SMLoc E = Parser.getTok().getLoc();
2436 if (Parser.getTok().isNot(AsmToken::RCurly))
2437 return Error(E, "'}' expected");
2438 Parser.Lex(); // Eat '}' token.
2439
Bill Wendling50d0f582010-11-18 23:43:05 +00002440 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
2441 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00002442}
2443
Jim Grosbach862019c2011-10-18 23:02:30 +00002444// parse a vector register list
2445ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2446parseVectorList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2447 if(Parser.getTok().isNot(AsmToken::LCurly))
2448 return MatchOperand_NoMatch;
2449
2450 SMLoc S = Parser.getTok().getLoc();
2451 Parser.Lex(); // Eat '{' token.
2452 SMLoc RegLoc = Parser.getTok().getLoc();
2453
2454 int Reg = tryParseRegister();
2455 if (Reg == -1) {
2456 Error(RegLoc, "register expected");
2457 return MatchOperand_ParseFail;
2458 }
2459
2460 unsigned FirstReg = Reg;
2461 unsigned Count = 1;
2462 while (Parser.getTok().is(AsmToken::Comma)) {
2463 Parser.Lex(); // Eat the comma.
2464 RegLoc = Parser.getTok().getLoc();
2465 int OldReg = Reg;
2466 Reg = tryParseRegister();
2467 if (Reg == -1) {
2468 Error(RegLoc, "register expected");
2469 return MatchOperand_ParseFail;
2470 }
2471 // vector register lists must also be contiguous.
2472 // It's OK to use the enumeration values directly here rather, as the
2473 // VFP register classes have the enum sorted properly.
2474 if (Reg != OldReg + 1) {
2475 Error(RegLoc, "non-contiguous register range");
2476 return MatchOperand_ParseFail;
2477 }
2478
2479 ++Count;
2480 }
2481
2482 SMLoc E = Parser.getTok().getLoc();
2483 if (Parser.getTok().isNot(AsmToken::RCurly)) {
2484 Error(E, "'}' expected");
2485 return MatchOperand_ParseFail;
2486 }
2487 Parser.Lex(); // Eat '}' token.
2488
2489 Operands.push_back(ARMOperand::CreateVectorList(FirstReg, Count, S, E));
2490 return MatchOperand_Success;
2491}
2492
Jim Grosbach43904292011-07-25 20:14:50 +00002493/// parseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
Jim Grosbachf922c472011-02-12 01:34:40 +00002494ARMAsmParser::OperandMatchResultTy ARMAsmParser::
Jim Grosbach43904292011-07-25 20:14:50 +00002495parseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002496 SMLoc S = Parser.getTok().getLoc();
2497 const AsmToken &Tok = Parser.getTok();
2498 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2499 StringRef OptStr = Tok.getString();
2500
2501 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
2502 .Case("sy", ARM_MB::SY)
2503 .Case("st", ARM_MB::ST)
Jim Grosbach032434d2011-07-13 23:40:38 +00002504 .Case("sh", ARM_MB::ISH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002505 .Case("ish", ARM_MB::ISH)
Jim Grosbach032434d2011-07-13 23:40:38 +00002506 .Case("shst", ARM_MB::ISHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002507 .Case("ishst", ARM_MB::ISHST)
2508 .Case("nsh", ARM_MB::NSH)
Jim Grosbach032434d2011-07-13 23:40:38 +00002509 .Case("un", ARM_MB::NSH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002510 .Case("nshst", ARM_MB::NSHST)
Jim Grosbach032434d2011-07-13 23:40:38 +00002511 .Case("unst", ARM_MB::NSHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002512 .Case("osh", ARM_MB::OSH)
2513 .Case("oshst", ARM_MB::OSHST)
2514 .Default(~0U);
2515
2516 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00002517 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002518
2519 Parser.Lex(); // Eat identifier token.
2520 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00002521 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002522}
2523
Jim Grosbach43904292011-07-25 20:14:50 +00002524/// parseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002525ARMAsmParser::OperandMatchResultTy ARMAsmParser::
Jim Grosbach43904292011-07-25 20:14:50 +00002526parseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002527 SMLoc S = Parser.getTok().getLoc();
2528 const AsmToken &Tok = Parser.getTok();
2529 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2530 StringRef IFlagsStr = Tok.getString();
2531
Owen Anderson2dbb46a2011-10-05 17:16:40 +00002532 // An iflags string of "none" is interpreted to mean that none of the AIF
2533 // bits are set. Not a terribly useful instruction, but a valid encoding.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002534 unsigned IFlags = 0;
Owen Anderson2dbb46a2011-10-05 17:16:40 +00002535 if (IFlagsStr != "none") {
2536 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
2537 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
2538 .Case("a", ARM_PROC::A)
2539 .Case("i", ARM_PROC::I)
2540 .Case("f", ARM_PROC::F)
2541 .Default(~0U);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002542
Owen Anderson2dbb46a2011-10-05 17:16:40 +00002543 // If some specific iflag is already set, it means that some letter is
2544 // present more than once, this is not acceptable.
2545 if (Flag == ~0U || (IFlags & Flag))
2546 return MatchOperand_NoMatch;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002547
Owen Anderson2dbb46a2011-10-05 17:16:40 +00002548 IFlags |= Flag;
2549 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002550 }
2551
2552 Parser.Lex(); // Eat identifier token.
2553 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
2554 return MatchOperand_Success;
2555}
2556
Jim Grosbach43904292011-07-25 20:14:50 +00002557/// parseMSRMaskOperand - Try to parse mask flags from MSR instruction.
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002558ARMAsmParser::OperandMatchResultTy ARMAsmParser::
Jim Grosbach43904292011-07-25 20:14:50 +00002559parseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002560 SMLoc S = Parser.getTok().getLoc();
2561 const AsmToken &Tok = Parser.getTok();
2562 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2563 StringRef Mask = Tok.getString();
2564
James Molloyacad68d2011-09-28 14:21:38 +00002565 if (isMClass()) {
2566 // See ARMv6-M 10.1.1
2567 unsigned FlagsVal = StringSwitch<unsigned>(Mask)
2568 .Case("apsr", 0)
2569 .Case("iapsr", 1)
2570 .Case("eapsr", 2)
2571 .Case("xpsr", 3)
2572 .Case("ipsr", 5)
2573 .Case("epsr", 6)
2574 .Case("iepsr", 7)
2575 .Case("msp", 8)
2576 .Case("psp", 9)
2577 .Case("primask", 16)
2578 .Case("basepri", 17)
2579 .Case("basepri_max", 18)
2580 .Case("faultmask", 19)
2581 .Case("control", 20)
2582 .Default(~0U);
2583
2584 if (FlagsVal == ~0U)
2585 return MatchOperand_NoMatch;
2586
2587 if (!hasV7Ops() && FlagsVal >= 17 && FlagsVal <= 19)
2588 // basepri, basepri_max and faultmask only valid for V7m.
2589 return MatchOperand_NoMatch;
2590
2591 Parser.Lex(); // Eat identifier token.
2592 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
2593 return MatchOperand_Success;
2594 }
2595
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002596 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
2597 size_t Start = 0, Next = Mask.find('_');
2598 StringRef Flags = "";
Jim Grosbachb29b4dd2011-07-19 22:45:10 +00002599 std::string SpecReg = LowercaseString(Mask.slice(Start, Next));
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002600 if (Next != StringRef::npos)
2601 Flags = Mask.slice(Next+1, Mask.size());
2602
2603 // FlagsVal contains the complete mask:
2604 // 3-0: Mask
2605 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
2606 unsigned FlagsVal = 0;
2607
2608 if (SpecReg == "apsr") {
2609 FlagsVal = StringSwitch<unsigned>(Flags)
Jim Grosbachb29b4dd2011-07-19 22:45:10 +00002610 .Case("nzcvq", 0x8) // same as CPSR_f
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002611 .Case("g", 0x4) // same as CPSR_s
2612 .Case("nzcvqg", 0xc) // same as CPSR_fs
2613 .Default(~0U);
2614
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00002615 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002616 if (!Flags.empty())
2617 return MatchOperand_NoMatch;
2618 else
Jim Grosbachbf841cf2011-09-14 20:03:46 +00002619 FlagsVal = 8; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00002620 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002621 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00002622 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
2623 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002624 for (int i = 0, e = Flags.size(); i != e; ++i) {
2625 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
2626 .Case("c", 1)
2627 .Case("x", 2)
2628 .Case("s", 4)
2629 .Case("f", 8)
2630 .Default(~0U);
2631
2632 // If some specific flag is already set, it means that some letter is
2633 // present more than once, this is not acceptable.
2634 if (FlagsVal == ~0U || (FlagsVal & Flag))
2635 return MatchOperand_NoMatch;
2636 FlagsVal |= Flag;
2637 }
2638 } else // No match for special register.
2639 return MatchOperand_NoMatch;
2640
Owen Anderson7784f1d2011-10-21 18:43:28 +00002641 // Special register without flags is NOT equivalent to "fc" flags.
2642 // NOTE: This is a divergence from gas' behavior. Uncommenting the following
2643 // two lines would enable gas compatibility at the expense of breaking
2644 // round-tripping.
2645 //
2646 // if (!FlagsVal)
2647 // FlagsVal = 0x9;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002648
2649 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
2650 if (SpecReg == "spsr")
2651 FlagsVal |= 16;
2652
2653 Parser.Lex(); // Eat identifier token.
2654 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
2655 return MatchOperand_Success;
2656}
2657
Jim Grosbachf6c05252011-07-21 17:23:04 +00002658ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2659parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands, StringRef Op,
2660 int Low, int High) {
2661 const AsmToken &Tok = Parser.getTok();
2662 if (Tok.isNot(AsmToken::Identifier)) {
2663 Error(Parser.getTok().getLoc(), Op + " operand expected.");
2664 return MatchOperand_ParseFail;
2665 }
2666 StringRef ShiftName = Tok.getString();
2667 std::string LowerOp = LowercaseString(Op);
2668 std::string UpperOp = UppercaseString(Op);
2669 if (ShiftName != LowerOp && ShiftName != UpperOp) {
2670 Error(Parser.getTok().getLoc(), Op + " operand expected.");
2671 return MatchOperand_ParseFail;
2672 }
2673 Parser.Lex(); // Eat shift type token.
2674
2675 // There must be a '#' and a shift amount.
2676 if (Parser.getTok().isNot(AsmToken::Hash)) {
2677 Error(Parser.getTok().getLoc(), "'#' expected");
2678 return MatchOperand_ParseFail;
2679 }
2680 Parser.Lex(); // Eat hash token.
2681
2682 const MCExpr *ShiftAmount;
2683 SMLoc Loc = Parser.getTok().getLoc();
2684 if (getParser().ParseExpression(ShiftAmount)) {
2685 Error(Loc, "illegal expression");
2686 return MatchOperand_ParseFail;
2687 }
2688 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2689 if (!CE) {
2690 Error(Loc, "constant expression expected");
2691 return MatchOperand_ParseFail;
2692 }
2693 int Val = CE->getValue();
2694 if (Val < Low || Val > High) {
2695 Error(Loc, "immediate value out of range");
2696 return MatchOperand_ParseFail;
2697 }
2698
2699 Operands.push_back(ARMOperand::CreateImm(CE, Loc, Parser.getTok().getLoc()));
2700
2701 return MatchOperand_Success;
2702}
2703
Jim Grosbachc27d4f92011-07-22 17:44:50 +00002704ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2705parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2706 const AsmToken &Tok = Parser.getTok();
2707 SMLoc S = Tok.getLoc();
2708 if (Tok.isNot(AsmToken::Identifier)) {
2709 Error(Tok.getLoc(), "'be' or 'le' operand expected");
2710 return MatchOperand_ParseFail;
2711 }
2712 int Val = StringSwitch<int>(Tok.getString())
2713 .Case("be", 1)
2714 .Case("le", 0)
2715 .Default(-1);
2716 Parser.Lex(); // Eat the token.
2717
2718 if (Val == -1) {
2719 Error(Tok.getLoc(), "'be' or 'le' operand expected");
2720 return MatchOperand_ParseFail;
2721 }
2722 Operands.push_back(ARMOperand::CreateImm(MCConstantExpr::Create(Val,
2723 getContext()),
2724 S, Parser.getTok().getLoc()));
2725 return MatchOperand_Success;
2726}
2727
Jim Grosbach580f4a92011-07-25 22:20:28 +00002728/// parseShifterImm - Parse the shifter immediate operand for SSAT/USAT
2729/// instructions. Legal values are:
2730/// lsl #n 'n' in [0,31]
2731/// asr #n 'n' in [1,32]
2732/// n == 32 encoded as n == 0.
2733ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2734parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2735 const AsmToken &Tok = Parser.getTok();
2736 SMLoc S = Tok.getLoc();
2737 if (Tok.isNot(AsmToken::Identifier)) {
2738 Error(S, "shift operator 'asr' or 'lsl' expected");
2739 return MatchOperand_ParseFail;
2740 }
2741 StringRef ShiftName = Tok.getString();
2742 bool isASR;
2743 if (ShiftName == "lsl" || ShiftName == "LSL")
2744 isASR = false;
2745 else if (ShiftName == "asr" || ShiftName == "ASR")
2746 isASR = true;
2747 else {
2748 Error(S, "shift operator 'asr' or 'lsl' expected");
2749 return MatchOperand_ParseFail;
2750 }
2751 Parser.Lex(); // Eat the operator.
2752
2753 // A '#' and a shift amount.
2754 if (Parser.getTok().isNot(AsmToken::Hash)) {
2755 Error(Parser.getTok().getLoc(), "'#' expected");
2756 return MatchOperand_ParseFail;
2757 }
2758 Parser.Lex(); // Eat hash token.
2759
2760 const MCExpr *ShiftAmount;
2761 SMLoc E = Parser.getTok().getLoc();
2762 if (getParser().ParseExpression(ShiftAmount)) {
2763 Error(E, "malformed shift expression");
2764 return MatchOperand_ParseFail;
2765 }
2766 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2767 if (!CE) {
2768 Error(E, "shift amount must be an immediate");
2769 return MatchOperand_ParseFail;
2770 }
2771
2772 int64_t Val = CE->getValue();
2773 if (isASR) {
2774 // Shift amount must be in [1,32]
2775 if (Val < 1 || Val > 32) {
2776 Error(E, "'asr' shift amount must be in range [1,32]");
2777 return MatchOperand_ParseFail;
2778 }
Owen Anderson0afa0092011-09-26 21:06:22 +00002779 // asr #32 encoded as asr #0, but is not allowed in Thumb2 mode.
2780 if (isThumb() && Val == 32) {
2781 Error(E, "'asr #32' shift amount not allowed in Thumb mode");
2782 return MatchOperand_ParseFail;
2783 }
Jim Grosbach580f4a92011-07-25 22:20:28 +00002784 if (Val == 32) Val = 0;
2785 } else {
2786 // Shift amount must be in [1,32]
2787 if (Val < 0 || Val > 31) {
2788 Error(E, "'lsr' shift amount must be in range [0,31]");
2789 return MatchOperand_ParseFail;
2790 }
2791 }
2792
2793 E = Parser.getTok().getLoc();
2794 Operands.push_back(ARMOperand::CreateShifterImm(isASR, Val, S, E));
2795
2796 return MatchOperand_Success;
2797}
2798
Jim Grosbach7e1547e2011-07-27 20:15:40 +00002799/// parseRotImm - Parse the shifter immediate operand for SXTB/UXTB family
2800/// of instructions. Legal values are:
2801/// ror #n 'n' in {0, 8, 16, 24}
2802ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2803parseRotImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2804 const AsmToken &Tok = Parser.getTok();
2805 SMLoc S = Tok.getLoc();
Jim Grosbach326efe52011-09-19 20:29:33 +00002806 if (Tok.isNot(AsmToken::Identifier))
2807 return MatchOperand_NoMatch;
Jim Grosbach7e1547e2011-07-27 20:15:40 +00002808 StringRef ShiftName = Tok.getString();
Jim Grosbach326efe52011-09-19 20:29:33 +00002809 if (ShiftName != "ror" && ShiftName != "ROR")
2810 return MatchOperand_NoMatch;
Jim Grosbach7e1547e2011-07-27 20:15:40 +00002811 Parser.Lex(); // Eat the operator.
2812
2813 // A '#' and a rotate amount.
2814 if (Parser.getTok().isNot(AsmToken::Hash)) {
2815 Error(Parser.getTok().getLoc(), "'#' expected");
2816 return MatchOperand_ParseFail;
2817 }
2818 Parser.Lex(); // Eat hash token.
2819
2820 const MCExpr *ShiftAmount;
2821 SMLoc E = Parser.getTok().getLoc();
2822 if (getParser().ParseExpression(ShiftAmount)) {
2823 Error(E, "malformed rotate expression");
2824 return MatchOperand_ParseFail;
2825 }
2826 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
2827 if (!CE) {
2828 Error(E, "rotate amount must be an immediate");
2829 return MatchOperand_ParseFail;
2830 }
2831
2832 int64_t Val = CE->getValue();
2833 // Shift amount must be in {0, 8, 16, 24} (0 is undocumented extension)
2834 // normally, zero is represented in asm by omitting the rotate operand
2835 // entirely.
2836 if (Val != 8 && Val != 16 && Val != 24 && Val != 0) {
2837 Error(E, "'ror' rotate amount must be 8, 16, or 24");
2838 return MatchOperand_ParseFail;
2839 }
2840
2841 E = Parser.getTok().getLoc();
2842 Operands.push_back(ARMOperand::CreateRotImm(Val, S, E));
2843
2844 return MatchOperand_Success;
2845}
2846
Jim Grosbach293a2ee2011-07-28 21:34:26 +00002847ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2848parseBitfield(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2849 SMLoc S = Parser.getTok().getLoc();
2850 // The bitfield descriptor is really two operands, the LSB and the width.
2851 if (Parser.getTok().isNot(AsmToken::Hash)) {
2852 Error(Parser.getTok().getLoc(), "'#' expected");
2853 return MatchOperand_ParseFail;
2854 }
2855 Parser.Lex(); // Eat hash token.
2856
2857 const MCExpr *LSBExpr;
2858 SMLoc E = Parser.getTok().getLoc();
2859 if (getParser().ParseExpression(LSBExpr)) {
2860 Error(E, "malformed immediate expression");
2861 return MatchOperand_ParseFail;
2862 }
2863 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LSBExpr);
2864 if (!CE) {
2865 Error(E, "'lsb' operand must be an immediate");
2866 return MatchOperand_ParseFail;
2867 }
2868
2869 int64_t LSB = CE->getValue();
2870 // The LSB must be in the range [0,31]
2871 if (LSB < 0 || LSB > 31) {
2872 Error(E, "'lsb' operand must be in the range [0,31]");
2873 return MatchOperand_ParseFail;
2874 }
2875 E = Parser.getTok().getLoc();
2876
2877 // Expect another immediate operand.
2878 if (Parser.getTok().isNot(AsmToken::Comma)) {
2879 Error(Parser.getTok().getLoc(), "too few operands");
2880 return MatchOperand_ParseFail;
2881 }
2882 Parser.Lex(); // Eat hash token.
2883 if (Parser.getTok().isNot(AsmToken::Hash)) {
2884 Error(Parser.getTok().getLoc(), "'#' expected");
2885 return MatchOperand_ParseFail;
2886 }
2887 Parser.Lex(); // Eat hash token.
2888
2889 const MCExpr *WidthExpr;
2890 if (getParser().ParseExpression(WidthExpr)) {
2891 Error(E, "malformed immediate expression");
2892 return MatchOperand_ParseFail;
2893 }
2894 CE = dyn_cast<MCConstantExpr>(WidthExpr);
2895 if (!CE) {
2896 Error(E, "'width' operand must be an immediate");
2897 return MatchOperand_ParseFail;
2898 }
2899
2900 int64_t Width = CE->getValue();
2901 // The LSB must be in the range [1,32-lsb]
2902 if (Width < 1 || Width > 32 - LSB) {
2903 Error(E, "'width' operand must be in the range [1,32-lsb]");
2904 return MatchOperand_ParseFail;
2905 }
2906 E = Parser.getTok().getLoc();
2907
2908 Operands.push_back(ARMOperand::CreateBitfield(LSB, Width, S, E));
2909
2910 return MatchOperand_Success;
2911}
2912
Jim Grosbach7ce05792011-08-03 23:50:40 +00002913ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2914parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2915 // Check for a post-index addressing register operand. Specifically:
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00002916 // postidx_reg := '+' register {, shift}
2917 // | '-' register {, shift}
2918 // | register {, shift}
Jim Grosbach7ce05792011-08-03 23:50:40 +00002919
2920 // This method must return MatchOperand_NoMatch without consuming any tokens
2921 // in the case where there is no match, as other alternatives take other
2922 // parse methods.
2923 AsmToken Tok = Parser.getTok();
2924 SMLoc S = Tok.getLoc();
2925 bool haveEaten = false;
Jim Grosbach16578b52011-08-05 16:11:38 +00002926 bool isAdd = true;
Jim Grosbach7ce05792011-08-03 23:50:40 +00002927 int Reg = -1;
2928 if (Tok.is(AsmToken::Plus)) {
2929 Parser.Lex(); // Eat the '+' token.
2930 haveEaten = true;
2931 } else if (Tok.is(AsmToken::Minus)) {
2932 Parser.Lex(); // Eat the '-' token.
Jim Grosbach16578b52011-08-05 16:11:38 +00002933 isAdd = false;
Jim Grosbach7ce05792011-08-03 23:50:40 +00002934 haveEaten = true;
2935 }
2936 if (Parser.getTok().is(AsmToken::Identifier))
2937 Reg = tryParseRegister();
2938 if (Reg == -1) {
2939 if (!haveEaten)
2940 return MatchOperand_NoMatch;
2941 Error(Parser.getTok().getLoc(), "register expected");
2942 return MatchOperand_ParseFail;
2943 }
2944 SMLoc E = Parser.getTok().getLoc();
2945
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00002946 ARM_AM::ShiftOpc ShiftTy = ARM_AM::no_shift;
2947 unsigned ShiftImm = 0;
Jim Grosbach0d6fac32011-08-05 22:03:36 +00002948 if (Parser.getTok().is(AsmToken::Comma)) {
2949 Parser.Lex(); // Eat the ','.
2950 if (parseMemRegOffsetShift(ShiftTy, ShiftImm))
2951 return MatchOperand_ParseFail;
2952 }
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00002953
2954 Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ShiftTy,
2955 ShiftImm, S, E));
Jim Grosbach7ce05792011-08-03 23:50:40 +00002956
2957 return MatchOperand_Success;
2958}
2959
Jim Grosbach251bf252011-08-10 21:56:18 +00002960ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2961parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2962 // Check for a post-index addressing register operand. Specifically:
2963 // am3offset := '+' register
2964 // | '-' register
2965 // | register
2966 // | # imm
2967 // | # + imm
2968 // | # - imm
2969
2970 // This method must return MatchOperand_NoMatch without consuming any tokens
2971 // in the case where there is no match, as other alternatives take other
2972 // parse methods.
2973 AsmToken Tok = Parser.getTok();
2974 SMLoc S = Tok.getLoc();
2975
2976 // Do immediates first, as we always parse those if we have a '#'.
2977 if (Parser.getTok().is(AsmToken::Hash)) {
2978 Parser.Lex(); // Eat the '#'.
2979 // Explicitly look for a '-', as we need to encode negative zero
2980 // differently.
2981 bool isNegative = Parser.getTok().is(AsmToken::Minus);
2982 const MCExpr *Offset;
2983 if (getParser().ParseExpression(Offset))
2984 return MatchOperand_ParseFail;
2985 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
2986 if (!CE) {
2987 Error(S, "constant expression expected");
2988 return MatchOperand_ParseFail;
2989 }
2990 SMLoc E = Tok.getLoc();
2991 // Negative zero is encoded as the flag value INT32_MIN.
2992 int32_t Val = CE->getValue();
2993 if (isNegative && Val == 0)
2994 Val = INT32_MIN;
2995
2996 Operands.push_back(
2997 ARMOperand::CreateImm(MCConstantExpr::Create(Val, getContext()), S, E));
2998
2999 return MatchOperand_Success;
3000 }
3001
3002
3003 bool haveEaten = false;
3004 bool isAdd = true;
3005 int Reg = -1;
3006 if (Tok.is(AsmToken::Plus)) {
3007 Parser.Lex(); // Eat the '+' token.
3008 haveEaten = true;
3009 } else if (Tok.is(AsmToken::Minus)) {
3010 Parser.Lex(); // Eat the '-' token.
3011 isAdd = false;
3012 haveEaten = true;
3013 }
3014 if (Parser.getTok().is(AsmToken::Identifier))
3015 Reg = tryParseRegister();
3016 if (Reg == -1) {
3017 if (!haveEaten)
3018 return MatchOperand_NoMatch;
3019 Error(Parser.getTok().getLoc(), "register expected");
3020 return MatchOperand_ParseFail;
3021 }
3022 SMLoc E = Parser.getTok().getLoc();
3023
3024 Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ARM_AM::no_shift,
3025 0, S, E));
3026
3027 return MatchOperand_Success;
3028}
3029
Jim Grosbacha77295d2011-09-08 22:07:06 +00003030/// cvtT2LdrdPre - Convert parsed operands to MCInst.
3031/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3032/// when they refer multiple MIOperands inside a single one.
3033bool ARMAsmParser::
3034cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
3035 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3036 // Rt, Rt2
3037 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3038 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3039 // Create a writeback register dummy placeholder.
3040 Inst.addOperand(MCOperand::CreateReg(0));
3041 // addr
3042 ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
3043 // pred
3044 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3045 return true;
3046}
3047
3048/// cvtT2StrdPre - Convert parsed operands to MCInst.
3049/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3050/// when they refer multiple MIOperands inside a single one.
3051bool ARMAsmParser::
3052cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
3053 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3054 // Create a writeback register dummy placeholder.
3055 Inst.addOperand(MCOperand::CreateReg(0));
3056 // Rt, Rt2
3057 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3058 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3059 // addr
3060 ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
3061 // pred
3062 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3063 return true;
3064}
3065
Jim Grosbacheeec0252011-09-08 00:39:19 +00003066/// cvtLdWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
3067/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3068/// when they refer multiple MIOperands inside a single one.
3069bool ARMAsmParser::
3070cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
3071 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3072 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3073
3074 // Create a writeback register dummy placeholder.
3075 Inst.addOperand(MCOperand::CreateImm(0));
3076
3077 ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
3078 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3079 return true;
3080}
3081
Jim Grosbachee2c2a42011-09-16 21:55:56 +00003082/// cvtStWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
3083/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3084/// when they refer multiple MIOperands inside a single one.
3085bool ARMAsmParser::
3086cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
3087 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3088 // Create a writeback register dummy placeholder.
3089 Inst.addOperand(MCOperand::CreateImm(0));
3090 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3091 ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
3092 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3093 return true;
3094}
3095
Jim Grosbach1355cf12011-07-26 17:10:22 +00003096/// cvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003097/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3098/// when they refer multiple MIOperands inside a single one.
3099bool ARMAsmParser::
Jim Grosbach1355cf12011-07-26 17:10:22 +00003100cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003101 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3102 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3103
3104 // Create a writeback register dummy placeholder.
3105 Inst.addOperand(MCOperand::CreateImm(0));
3106
Jim Grosbach7ce05792011-08-03 23:50:40 +00003107 ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003108 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3109 return true;
3110}
3111
Owen Anderson9ab0f252011-08-26 20:43:14 +00003112/// cvtLdWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
3113/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3114/// when they refer multiple MIOperands inside a single one.
3115bool ARMAsmParser::
3116cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
3117 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3118 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3119
3120 // Create a writeback register dummy placeholder.
3121 Inst.addOperand(MCOperand::CreateImm(0));
3122
3123 ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
3124 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3125 return true;
3126}
3127
3128
Jim Grosbach548340c2011-08-11 19:22:40 +00003129/// cvtStWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
3130/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3131/// when they refer multiple MIOperands inside a single one.
3132bool ARMAsmParser::
3133cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
3134 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3135 // Create a writeback register dummy placeholder.
3136 Inst.addOperand(MCOperand::CreateImm(0));
3137 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3138 ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
3139 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3140 return true;
3141}
3142
Jim Grosbach1355cf12011-07-26 17:10:22 +00003143/// cvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003144/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3145/// when they refer multiple MIOperands inside a single one.
3146bool ARMAsmParser::
Jim Grosbach1355cf12011-07-26 17:10:22 +00003147cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003148 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3149 // Create a writeback register dummy placeholder.
3150 Inst.addOperand(MCOperand::CreateImm(0));
Jim Grosbach548340c2011-08-11 19:22:40 +00003151 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3152 ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
3153 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
Jim Grosbach7ce05792011-08-03 23:50:40 +00003154 return true;
3155}
3156
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00003157/// cvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
3158/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3159/// when they refer multiple MIOperands inside a single one.
3160bool ARMAsmParser::
3161cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
3162 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3163 // Create a writeback register dummy placeholder.
3164 Inst.addOperand(MCOperand::CreateImm(0));
3165 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3166 ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
3167 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3168 return true;
3169}
3170
Jim Grosbach7ce05792011-08-03 23:50:40 +00003171/// cvtLdExtTWriteBackImm - Convert parsed operands to MCInst.
3172/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3173/// when they refer multiple MIOperands inside a single one.
3174bool ARMAsmParser::
3175cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
3176 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3177 // Rt
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003178 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
Jim Grosbach7ce05792011-08-03 23:50:40 +00003179 // Create a writeback register dummy placeholder.
3180 Inst.addOperand(MCOperand::CreateImm(0));
3181 // addr
3182 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3183 // offset
3184 ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
3185 // pred
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003186 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3187 return true;
3188}
3189
Jim Grosbach7ce05792011-08-03 23:50:40 +00003190/// cvtLdExtTWriteBackReg - Convert parsed operands to MCInst.
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003191/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3192/// when they refer multiple MIOperands inside a single one.
3193bool ARMAsmParser::
Jim Grosbach7ce05792011-08-03 23:50:40 +00003194cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
3195 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3196 // Rt
Owen Andersonaa3402e2011-07-28 17:18:57 +00003197 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003198 // Create a writeback register dummy placeholder.
3199 Inst.addOperand(MCOperand::CreateImm(0));
Jim Grosbach7ce05792011-08-03 23:50:40 +00003200 // addr
3201 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3202 // offset
3203 ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
3204 // pred
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003205 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3206 return true;
3207}
3208
Jim Grosbach7ce05792011-08-03 23:50:40 +00003209/// cvtStExtTWriteBackImm - Convert parsed operands to MCInst.
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003210/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3211/// when they refer multiple MIOperands inside a single one.
3212bool ARMAsmParser::
Jim Grosbach7ce05792011-08-03 23:50:40 +00003213cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
3214 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003215 // Create a writeback register dummy placeholder.
3216 Inst.addOperand(MCOperand::CreateImm(0));
Jim Grosbach7ce05792011-08-03 23:50:40 +00003217 // Rt
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003218 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
Jim Grosbach7ce05792011-08-03 23:50:40 +00003219 // addr
3220 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3221 // offset
3222 ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
3223 // pred
3224 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3225 return true;
3226}
3227
3228/// cvtStExtTWriteBackReg - Convert parsed operands to MCInst.
3229/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3230/// when they refer multiple MIOperands inside a single one.
3231bool ARMAsmParser::
3232cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
3233 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3234 // Create a writeback register dummy placeholder.
3235 Inst.addOperand(MCOperand::CreateImm(0));
3236 // Rt
3237 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3238 // addr
3239 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3240 // offset
3241 ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
3242 // pred
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003243 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3244 return true;
3245}
3246
Jim Grosbach2fd2b872011-08-10 20:29:19 +00003247/// cvtLdrdPre - Convert parsed operands to MCInst.
3248/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3249/// when they refer multiple MIOperands inside a single one.
3250bool ARMAsmParser::
3251cvtLdrdPre(MCInst &Inst, unsigned Opcode,
3252 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3253 // Rt, Rt2
3254 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3255 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3256 // Create a writeback register dummy placeholder.
3257 Inst.addOperand(MCOperand::CreateImm(0));
3258 // addr
3259 ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
3260 // pred
3261 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3262 return true;
3263}
3264
Jim Grosbach14605d12011-08-11 20:28:23 +00003265/// cvtStrdPre - Convert parsed operands to MCInst.
3266/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3267/// when they refer multiple MIOperands inside a single one.
3268bool ARMAsmParser::
3269cvtStrdPre(MCInst &Inst, unsigned Opcode,
3270 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3271 // Create a writeback register dummy placeholder.
3272 Inst.addOperand(MCOperand::CreateImm(0));
3273 // Rt, Rt2
3274 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3275 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3276 // addr
3277 ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
3278 // pred
3279 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3280 return true;
3281}
3282
Jim Grosbach623a4542011-08-10 22:42:16 +00003283/// cvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
3284/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3285/// when they refer multiple MIOperands inside a single one.
3286bool ARMAsmParser::
3287cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
3288 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3289 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3290 // Create a writeback register dummy placeholder.
3291 Inst.addOperand(MCOperand::CreateImm(0));
3292 ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
3293 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3294 return true;
3295}
3296
Jim Grosbach88ae2bc2011-08-19 22:07:46 +00003297/// cvtThumbMultiple- Convert parsed operands to MCInst.
3298/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3299/// when they refer multiple MIOperands inside a single one.
3300bool ARMAsmParser::
3301cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
3302 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3303 // The second source operand must be the same register as the destination
3304 // operand.
3305 if (Operands.size() == 6 &&
Jim Grosbach7a010692011-08-19 22:30:46 +00003306 (((ARMOperand*)Operands[3])->getReg() !=
3307 ((ARMOperand*)Operands[5])->getReg()) &&
3308 (((ARMOperand*)Operands[3])->getReg() !=
3309 ((ARMOperand*)Operands[4])->getReg())) {
Jim Grosbach88ae2bc2011-08-19 22:07:46 +00003310 Error(Operands[3]->getStartLoc(),
Jim Grosbach7a010692011-08-19 22:30:46 +00003311 "destination register must match source register");
Jim Grosbach88ae2bc2011-08-19 22:07:46 +00003312 return false;
3313 }
3314 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3315 ((ARMOperand*)Operands[1])->addCCOutOperands(Inst, 1);
3316 ((ARMOperand*)Operands[4])->addRegOperands(Inst, 1);
Jim Grosbach7a010692011-08-19 22:30:46 +00003317 // If we have a three-operand form, use that, else the second source operand
3318 // is just the destination operand again.
3319 if (Operands.size() == 6)
3320 ((ARMOperand*)Operands[5])->addRegOperands(Inst, 1);
3321 else
3322 Inst.addOperand(Inst.getOperand(0));
Jim Grosbach88ae2bc2011-08-19 22:07:46 +00003323 ((ARMOperand*)Operands[2])->addCondCodeOperands(Inst, 2);
3324
3325 return true;
3326}
Jim Grosbach623a4542011-08-10 22:42:16 +00003327
Bill Wendlinge7176102010-11-06 22:36:58 +00003328/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003329/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00003330bool ARMAsmParser::
Jim Grosbach7ce05792011-08-03 23:50:40 +00003331parseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +00003332 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00003333 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00003334 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00003335 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00003336 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00003337
Sean Callanan18b83232010-01-19 21:44:56 +00003338 const AsmToken &BaseRegTok = Parser.getTok();
Jim Grosbach1355cf12011-07-26 17:10:22 +00003339 int BaseRegNum = tryParseRegister();
Jim Grosbach7ce05792011-08-03 23:50:40 +00003340 if (BaseRegNum == -1)
3341 return Error(BaseRegTok.getLoc(), "register expected");
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00003342
Daniel Dunbar05710932011-01-18 05:34:17 +00003343 // The next token must either be a comma or a closing bracket.
3344 const AsmToken &Tok = Parser.getTok();
3345 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
Jim Grosbach7ce05792011-08-03 23:50:40 +00003346 return Error(Tok.getLoc(), "malformed memory operand");
Daniel Dunbar05710932011-01-18 05:34:17 +00003347
Jim Grosbach7ce05792011-08-03 23:50:40 +00003348 if (Tok.is(AsmToken::RBrac)) {
Sean Callanan76264762010-04-02 22:27:05 +00003349 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00003350 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00003351
Jim Grosbach7ce05792011-08-03 23:50:40 +00003352 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0, ARM_AM::no_shift,
Jim Grosbach57dcb852011-10-11 17:29:55 +00003353 0, 0, false, S, E));
Jim Grosbach03f44a02010-11-29 23:18:01 +00003354
Jim Grosbachfb12f352011-09-19 18:42:21 +00003355 // If there's a pre-indexing writeback marker, '!', just add it as a token
3356 // operand. It's rather odd, but syntactically valid.
3357 if (Parser.getTok().is(AsmToken::Exclaim)) {
3358 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3359 Parser.Lex(); // Eat the '!'.
3360 }
3361
Jim Grosbach7ce05792011-08-03 23:50:40 +00003362 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00003363 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00003364
Jim Grosbach7ce05792011-08-03 23:50:40 +00003365 assert(Tok.is(AsmToken::Comma) && "Lost comma in memory operand?!");
3366 Parser.Lex(); // Eat the comma.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00003367
Jim Grosbach57dcb852011-10-11 17:29:55 +00003368 // If we have a ':', it's an alignment specifier.
3369 if (Parser.getTok().is(AsmToken::Colon)) {
3370 Parser.Lex(); // Eat the ':'.
3371 E = Parser.getTok().getLoc();
3372
3373 const MCExpr *Expr;
3374 if (getParser().ParseExpression(Expr))
3375 return true;
3376
3377 // The expression has to be a constant. Memory references with relocations
3378 // don't come through here, as they use the <label> forms of the relevant
3379 // instructions.
3380 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3381 if (!CE)
3382 return Error (E, "constant expression expected");
3383
3384 unsigned Align = 0;
3385 switch (CE->getValue()) {
3386 default:
3387 return Error(E, "alignment specifier must be 64, 128, or 256 bits");
3388 case 64: Align = 8; break;
3389 case 128: Align = 16; break;
3390 case 256: Align = 32; break;
3391 }
3392
3393 // Now we should have the closing ']'
3394 E = Parser.getTok().getLoc();
3395 if (Parser.getTok().isNot(AsmToken::RBrac))
3396 return Error(E, "']' expected");
3397 Parser.Lex(); // Eat right bracket token.
3398
3399 // Don't worry about range checking the value here. That's handled by
3400 // the is*() predicates.
3401 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0,
3402 ARM_AM::no_shift, 0, Align,
3403 false, S, E));
3404
3405 // If there's a pre-indexing writeback marker, '!', just add it as a token
3406 // operand.
3407 if (Parser.getTok().is(AsmToken::Exclaim)) {
3408 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3409 Parser.Lex(); // Eat the '!'.
3410 }
3411
3412 return false;
3413 }
3414
3415 // If we have a '#', it's an immediate offset, else assume it's a register
Jim Grosbach7ce05792011-08-03 23:50:40 +00003416 // offset.
3417 if (Parser.getTok().is(AsmToken::Hash)) {
3418 Parser.Lex(); // Eat the '#'.
3419 E = Parser.getTok().getLoc();
Daniel Dunbar05d8b712011-01-18 05:34:24 +00003420
Owen Anderson0da10cf2011-08-29 19:36:44 +00003421 bool isNegative = getParser().getTok().is(AsmToken::Minus);
Jim Grosbach7ce05792011-08-03 23:50:40 +00003422 const MCExpr *Offset;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003423 if (getParser().ParseExpression(Offset))
3424 return true;
Jim Grosbach7ce05792011-08-03 23:50:40 +00003425
3426 // The expression has to be a constant. Memory references with relocations
3427 // don't come through here, as they use the <label> forms of the relevant
3428 // instructions.
3429 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
3430 if (!CE)
3431 return Error (E, "constant expression expected");
3432
Owen Anderson0da10cf2011-08-29 19:36:44 +00003433 // If the constant was #-0, represent it as INT32_MIN.
3434 int32_t Val = CE->getValue();
3435 if (isNegative && Val == 0)
3436 CE = MCConstantExpr::Create(INT32_MIN, getContext());
3437
Jim Grosbach7ce05792011-08-03 23:50:40 +00003438 // Now we should have the closing ']'
3439 E = Parser.getTok().getLoc();
3440 if (Parser.getTok().isNot(AsmToken::RBrac))
3441 return Error(E, "']' expected");
3442 Parser.Lex(); // Eat right bracket token.
3443
3444 // Don't worry about range checking the value here. That's handled by
3445 // the is*() predicates.
3446 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, CE, 0,
Jim Grosbach57dcb852011-10-11 17:29:55 +00003447 ARM_AM::no_shift, 0, 0,
3448 false, S, E));
Jim Grosbach7ce05792011-08-03 23:50:40 +00003449
3450 // If there's a pre-indexing writeback marker, '!', just add it as a token
3451 // operand.
3452 if (Parser.getTok().is(AsmToken::Exclaim)) {
3453 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3454 Parser.Lex(); // Eat the '!'.
3455 }
3456
3457 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00003458 }
Jim Grosbach7ce05792011-08-03 23:50:40 +00003459
3460 // The register offset is optionally preceded by a '+' or '-'
3461 bool isNegative = false;
3462 if (Parser.getTok().is(AsmToken::Minus)) {
3463 isNegative = true;
3464 Parser.Lex(); // Eat the '-'.
3465 } else if (Parser.getTok().is(AsmToken::Plus)) {
3466 // Nothing to do.
3467 Parser.Lex(); // Eat the '+'.
3468 }
3469
3470 E = Parser.getTok().getLoc();
3471 int OffsetRegNum = tryParseRegister();
3472 if (OffsetRegNum == -1)
3473 return Error(E, "register expected");
3474
3475 // If there's a shift operator, handle it.
3476 ARM_AM::ShiftOpc ShiftType = ARM_AM::no_shift;
Jim Grosbach0d6fac32011-08-05 22:03:36 +00003477 unsigned ShiftImm = 0;
Jim Grosbach7ce05792011-08-03 23:50:40 +00003478 if (Parser.getTok().is(AsmToken::Comma)) {
3479 Parser.Lex(); // Eat the ','.
Jim Grosbach0d6fac32011-08-05 22:03:36 +00003480 if (parseMemRegOffsetShift(ShiftType, ShiftImm))
Jim Grosbach7ce05792011-08-03 23:50:40 +00003481 return true;
3482 }
3483
3484 // Now we should have the closing ']'
3485 E = Parser.getTok().getLoc();
3486 if (Parser.getTok().isNot(AsmToken::RBrac))
3487 return Error(E, "']' expected");
3488 Parser.Lex(); // Eat right bracket token.
3489
3490 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, OffsetRegNum,
Jim Grosbach57dcb852011-10-11 17:29:55 +00003491 ShiftType, ShiftImm, 0, isNegative,
Jim Grosbach7ce05792011-08-03 23:50:40 +00003492 S, E));
3493
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00003494 // If there's a pre-indexing writeback marker, '!', just add it as a token
3495 // operand.
3496 if (Parser.getTok().is(AsmToken::Exclaim)) {
3497 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3498 Parser.Lex(); // Eat the '!'.
3499 }
Jim Grosbach7ce05792011-08-03 23:50:40 +00003500
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003501 return false;
3502}
3503
Jim Grosbach7ce05792011-08-03 23:50:40 +00003504/// parseMemRegOffsetShift - one of these two:
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003505/// ( lsl | lsr | asr | ror ) , # shift_amount
3506/// rrx
Jim Grosbach7ce05792011-08-03 23:50:40 +00003507/// return true if it parses a shift otherwise it returns false.
3508bool ARMAsmParser::parseMemRegOffsetShift(ARM_AM::ShiftOpc &St,
3509 unsigned &Amount) {
3510 SMLoc Loc = Parser.getTok().getLoc();
Sean Callanan18b83232010-01-19 21:44:56 +00003511 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003512 if (Tok.isNot(AsmToken::Identifier))
3513 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00003514 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003515 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00003516 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003517 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00003518 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003519 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00003520 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003521 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00003522 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003523 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00003524 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003525 else
Jim Grosbach7ce05792011-08-03 23:50:40 +00003526 return Error(Loc, "illegal shift operator");
Sean Callananb9a25b72010-01-19 20:27:46 +00003527 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003528
Jim Grosbach7ce05792011-08-03 23:50:40 +00003529 // rrx stands alone.
3530 Amount = 0;
3531 if (St != ARM_AM::rrx) {
3532 Loc = Parser.getTok().getLoc();
3533 // A '#' and a shift amount.
3534 const AsmToken &HashTok = Parser.getTok();
3535 if (HashTok.isNot(AsmToken::Hash))
3536 return Error(HashTok.getLoc(), "'#' expected");
3537 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003538
Jim Grosbach7ce05792011-08-03 23:50:40 +00003539 const MCExpr *Expr;
3540 if (getParser().ParseExpression(Expr))
3541 return true;
3542 // Range check the immediate.
3543 // lsl, ror: 0 <= imm <= 31
3544 // lsr, asr: 0 <= imm <= 32
3545 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3546 if (!CE)
3547 return Error(Loc, "shift amount must be an immediate");
3548 int64_t Imm = CE->getValue();
3549 if (Imm < 0 ||
3550 ((St == ARM_AM::lsl || St == ARM_AM::ror) && Imm > 31) ||
3551 ((St == ARM_AM::lsr || St == ARM_AM::asr) && Imm > 32))
3552 return Error(Loc, "immediate shift value out of range");
3553 Amount = Imm;
3554 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00003555
3556 return false;
3557}
3558
Jim Grosbach9d390362011-10-03 23:38:36 +00003559/// parseFPImm - A floating point immediate expression operand.
3560ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3561parseFPImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3562 SMLoc S = Parser.getTok().getLoc();
3563
3564 if (Parser.getTok().isNot(AsmToken::Hash))
3565 return MatchOperand_NoMatch;
Jim Grosbach0e387b22011-10-17 22:26:03 +00003566
3567 // Disambiguate the VMOV forms that can accept an FP immediate.
3568 // vmov.f32 <sreg>, #imm
3569 // vmov.f64 <dreg>, #imm
3570 // vmov.f32 <dreg>, #imm @ vector f32x2
3571 // vmov.f32 <qreg>, #imm @ vector f32x4
3572 //
3573 // There are also the NEON VMOV instructions which expect an
3574 // integer constant. Make sure we don't try to parse an FPImm
3575 // for these:
3576 // vmov.i{8|16|32|64} <dreg|qreg>, #imm
3577 ARMOperand *TyOp = static_cast<ARMOperand*>(Operands[2]);
3578 if (!TyOp->isToken() || (TyOp->getToken() != ".f32" &&
3579 TyOp->getToken() != ".f64"))
3580 return MatchOperand_NoMatch;
3581
Jim Grosbach9d390362011-10-03 23:38:36 +00003582 Parser.Lex(); // Eat the '#'.
3583
3584 // Handle negation, as that still comes through as a separate token.
3585 bool isNegative = false;
3586 if (Parser.getTok().is(AsmToken::Minus)) {
3587 isNegative = true;
3588 Parser.Lex();
3589 }
3590 const AsmToken &Tok = Parser.getTok();
3591 if (Tok.is(AsmToken::Real)) {
3592 APFloat RealVal(APFloat::IEEEdouble, Tok.getString());
3593 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
3594 // If we had a '-' in front, toggle the sign bit.
3595 IntVal ^= (uint64_t)isNegative << 63;
3596 int Val = ARM_AM::getFP64Imm(APInt(64, IntVal));
3597 Parser.Lex(); // Eat the token.
3598 if (Val == -1) {
3599 TokError("floating point value out of range");
3600 return MatchOperand_ParseFail;
3601 }
3602 Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
3603 return MatchOperand_Success;
3604 }
3605 if (Tok.is(AsmToken::Integer)) {
3606 int64_t Val = Tok.getIntVal();
3607 Parser.Lex(); // Eat the token.
3608 if (Val > 255 || Val < 0) {
3609 TokError("encoded floating point value out of range");
3610 return MatchOperand_ParseFail;
3611 }
3612 Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
3613 return MatchOperand_Success;
3614 }
3615
3616 TokError("invalid floating point immediate");
3617 return MatchOperand_ParseFail;
3618}
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003619/// Parse a arm instruction operand. For now this parses the operand regardless
3620/// of the mnemonic.
Jim Grosbach1355cf12011-07-26 17:10:22 +00003621bool ARMAsmParser::parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00003622 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00003623 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00003624
3625 // Check if the current operand has a custom associated parser, if so, try to
3626 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00003627 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
3628 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00003629 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00003630 // If there wasn't a custom match, try the generic matcher below. Otherwise,
3631 // there was a match, but an error occurred, in which case, just return that
3632 // the operand parsing failed.
3633 if (ResTy == MatchOperand_ParseFail)
3634 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00003635
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00003636 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00003637 default:
3638 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00003639 return true;
Jim Grosbach19906722011-07-13 18:49:30 +00003640 case AsmToken::Identifier: {
Jim Grosbach5cd5ac62011-10-03 21:12:43 +00003641 // If this is VMRS, check for the apsr_nzcv operand.
Jim Grosbach1355cf12011-07-26 17:10:22 +00003642 if (!tryParseRegisterWithWriteBack(Operands))
Bill Wendling50d0f582010-11-18 23:43:05 +00003643 return false;
Jim Grosbach0d87ec22011-07-26 20:41:24 +00003644 int Res = tryParseShiftRegister(Operands);
Jim Grosbach19906722011-07-13 18:49:30 +00003645 if (Res == 0) // success
Owen Anderson00828302011-03-18 22:50:18 +00003646 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00003647 else if (Res == -1) // irrecoverable error
3648 return true;
Jim Grosbach5cd5ac62011-10-03 21:12:43 +00003649 if (Mnemonic == "vmrs" && Parser.getTok().getString() == "apsr_nzcv") {
3650 S = Parser.getTok().getLoc();
3651 Parser.Lex();
3652 Operands.push_back(ARMOperand::CreateToken("apsr_nzcv", S));
3653 return false;
3654 }
Owen Andersone4e5e2a2011-01-13 21:46:02 +00003655
3656 // Fall though for the Identifier case that is not a register or a
3657 // special name.
Jim Grosbach19906722011-07-13 18:49:30 +00003658 }
Kevin Enderby67b212e2011-01-13 20:32:36 +00003659 case AsmToken::Integer: // things like 1f and 2b as a branch targets
3660 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00003661 // This was not a register so parse other operands that start with an
3662 // identifier (like labels) as expressions and create them as immediates.
3663 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00003664 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00003665 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00003666 return true;
Sean Callanan76264762010-04-02 22:27:05 +00003667 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00003668 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
3669 return false;
3670 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00003671 case AsmToken::LBrac:
Jim Grosbach1355cf12011-07-26 17:10:22 +00003672 return parseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00003673 case AsmToken::LCurly:
Jim Grosbach1355cf12011-07-26 17:10:22 +00003674 return parseRegisterList(Operands);
Owen Anderson63553c72011-08-29 17:17:09 +00003675 case AsmToken::Hash: {
Kevin Enderby079469f2009-10-13 23:33:38 +00003676 // #42 -> immediate.
3677 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00003678 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00003679 Parser.Lex();
Owen Anderson63553c72011-08-29 17:17:09 +00003680 bool isNegative = Parser.getTok().is(AsmToken::Minus);
Kevin Enderby515d5092009-10-15 20:48:48 +00003681 const MCExpr *ImmVal;
3682 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00003683 return true;
Owen Anderson63553c72011-08-29 17:17:09 +00003684 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmVal);
3685 if (!CE) {
3686 Error(S, "constant expression expected");
3687 return MatchOperand_ParseFail;
3688 }
3689 int32_t Val = CE->getValue();
3690 if (isNegative && Val == 0)
3691 ImmVal = MCConstantExpr::Create(INT32_MIN, getContext());
Sean Callanan76264762010-04-02 22:27:05 +00003692 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00003693 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
3694 return false;
Owen Anderson63553c72011-08-29 17:17:09 +00003695 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00003696 case AsmToken::Colon: {
3697 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00003698 // FIXME: Check it's an expression prefix,
3699 // e.g. (FOO - :lower16:BAR) isn't legal.
3700 ARMMCExpr::VariantKind RefKind;
Jim Grosbach1355cf12011-07-26 17:10:22 +00003701 if (parsePrefix(RefKind))
Jason W Kim9081b4b2011-01-11 23:53:41 +00003702 return true;
3703
Evan Cheng75972122011-01-13 07:58:56 +00003704 const MCExpr *SubExprVal;
3705 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00003706 return true;
3707
Evan Cheng75972122011-01-13 07:58:56 +00003708 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
3709 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00003710 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00003711 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00003712 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00003713 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00003714 }
3715}
3716
Jim Grosbach1355cf12011-07-26 17:10:22 +00003717// parsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
Evan Cheng75972122011-01-13 07:58:56 +00003718// :lower16: and :upper16:.
Jim Grosbach1355cf12011-07-26 17:10:22 +00003719bool ARMAsmParser::parsePrefix(ARMMCExpr::VariantKind &RefKind) {
Evan Cheng75972122011-01-13 07:58:56 +00003720 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00003721
3722 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00003723 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00003724 Parser.Lex(); // Eat ':'
3725
3726 if (getLexer().isNot(AsmToken::Identifier)) {
3727 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
3728 return true;
3729 }
3730
3731 StringRef IDVal = Parser.getTok().getIdentifier();
3732 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00003733 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00003734 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00003735 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00003736 } else {
3737 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
3738 return true;
3739 }
3740 Parser.Lex();
3741
3742 if (getLexer().isNot(AsmToken::Colon)) {
3743 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
3744 return true;
3745 }
3746 Parser.Lex(); // Eat the last ':'
3747 return false;
3748}
3749
Daniel Dunbar352e1482011-01-11 15:59:50 +00003750/// \brief Given a mnemonic, split out possible predication code and carry
3751/// setting letters to form a canonical mnemonic and flags.
3752//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00003753// FIXME: Would be nice to autogen this.
Jim Grosbach89df9962011-08-26 21:43:41 +00003754// FIXME: This is a bit of a maze of special cases.
Jim Grosbach1355cf12011-07-26 17:10:22 +00003755StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic,
Jim Grosbach5f160572011-07-19 20:10:31 +00003756 unsigned &PredicationCode,
3757 bool &CarrySetting,
Jim Grosbach89df9962011-08-26 21:43:41 +00003758 unsigned &ProcessorIMod,
3759 StringRef &ITMask) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00003760 PredicationCode = ARMCC::AL;
3761 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00003762 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00003763
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00003764 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00003765 //
3766 // FIXME: Would be nice to autogen this.
Jim Grosbach5f160572011-07-19 20:10:31 +00003767 if ((Mnemonic == "movs" && isThumb()) ||
3768 Mnemonic == "teq" || Mnemonic == "vceq" || Mnemonic == "svc" ||
3769 Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
3770 Mnemonic == "vmls" || Mnemonic == "vnmls" || Mnemonic == "vacge" ||
3771 Mnemonic == "vcge" || Mnemonic == "vclt" || Mnemonic == "vacgt" ||
3772 Mnemonic == "vcgt" || Mnemonic == "vcle" || Mnemonic == "smlal" ||
3773 Mnemonic == "umaal" || Mnemonic == "umlal" || Mnemonic == "vabal" ||
3774 Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal")
Daniel Dunbar352e1482011-01-11 15:59:50 +00003775 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00003776
Jim Grosbach3f00e312011-07-11 17:09:57 +00003777 // First, split out any predication code. Ignore mnemonics we know aren't
3778 // predicated but do have a carry-set and so weren't caught above.
Jim Grosbachab40f4b2011-07-20 18:20:31 +00003779 if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
Jim Grosbach71725a02011-07-27 21:58:11 +00003780 Mnemonic != "muls" && Mnemonic != "smlals" && Mnemonic != "smulls" &&
Jim Grosbach04d55f12011-08-22 23:55:58 +00003781 Mnemonic != "umlals" && Mnemonic != "umulls" && Mnemonic != "lsls" &&
Jim Grosbach2f25d9b2011-09-01 18:22:13 +00003782 Mnemonic != "sbcs" && Mnemonic != "rscs") {
Jim Grosbach3f00e312011-07-11 17:09:57 +00003783 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
3784 .Case("eq", ARMCC::EQ)
3785 .Case("ne", ARMCC::NE)
3786 .Case("hs", ARMCC::HS)
3787 .Case("cs", ARMCC::HS)
3788 .Case("lo", ARMCC::LO)
3789 .Case("cc", ARMCC::LO)
3790 .Case("mi", ARMCC::MI)
3791 .Case("pl", ARMCC::PL)
3792 .Case("vs", ARMCC::VS)
3793 .Case("vc", ARMCC::VC)
3794 .Case("hi", ARMCC::HI)
3795 .Case("ls", ARMCC::LS)
3796 .Case("ge", ARMCC::GE)
3797 .Case("lt", ARMCC::LT)
3798 .Case("gt", ARMCC::GT)
3799 .Case("le", ARMCC::LE)
3800 .Case("al", ARMCC::AL)
3801 .Default(~0U);
3802 if (CC != ~0U) {
3803 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
3804 PredicationCode = CC;
3805 }
Bill Wendling52925b62010-10-29 23:50:21 +00003806 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00003807
Daniel Dunbar352e1482011-01-11 15:59:50 +00003808 // Next, determine if we have a carry setting bit. We explicitly ignore all
3809 // the instructions we know end in 's'.
3810 if (Mnemonic.endswith("s") &&
Jim Grosbach00f5d982011-08-17 22:49:09 +00003811 !(Mnemonic == "cps" || Mnemonic == "mls" ||
Jim Grosbach5f160572011-07-19 20:10:31 +00003812 Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
3813 Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
3814 Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
Jim Grosbache1cf5902011-07-29 20:26:09 +00003815 Mnemonic == "vrsqrts" || Mnemonic == "srs" ||
3816 (Mnemonic == "movs" && isThumb()))) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00003817 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
3818 CarrySetting = true;
3819 }
3820
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00003821 // The "cps" instruction can have a interrupt mode operand which is glued into
3822 // the mnemonic. Check if this is the case, split it and parse the imod op
3823 if (Mnemonic.startswith("cps")) {
3824 // Split out any imod code.
3825 unsigned IMod =
3826 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
3827 .Case("ie", ARM_PROC::IE)
3828 .Case("id", ARM_PROC::ID)
3829 .Default(~0U);
3830 if (IMod != ~0U) {
3831 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
3832 ProcessorIMod = IMod;
3833 }
3834 }
3835
Jim Grosbach89df9962011-08-26 21:43:41 +00003836 // The "it" instruction has the condition mask on the end of the mnemonic.
3837 if (Mnemonic.startswith("it")) {
3838 ITMask = Mnemonic.slice(2, Mnemonic.size());
3839 Mnemonic = Mnemonic.slice(0, 2);
3840 }
3841
Daniel Dunbar352e1482011-01-11 15:59:50 +00003842 return Mnemonic;
3843}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00003844
3845/// \brief Given a canonical mnemonic, determine if the instruction ever allows
3846/// inclusion of carry set or predication code operands.
3847//
3848// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00003849void ARMAsmParser::
Jim Grosbach1355cf12011-07-26 17:10:22 +00003850getMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00003851 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00003852 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
3853 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
Jim Grosbach3443ed52011-09-16 18:05:48 +00003854 Mnemonic == "add" || Mnemonic == "adc" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00003855 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Jim Grosbachd5d0e812011-09-19 23:31:02 +00003856 Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00003857 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
Jim Grosbachd5d0e812011-09-19 23:31:02 +00003858 Mnemonic == "sbc" || Mnemonic == "eor" || Mnemonic == "neg" ||
Jim Grosbach3443ed52011-09-16 18:05:48 +00003859 (!isThumb() && (Mnemonic == "smull" || Mnemonic == "mov" ||
Jim Grosbachd5d0e812011-09-19 23:31:02 +00003860 Mnemonic == "mla" || Mnemonic == "smlal" ||
3861 Mnemonic == "umlal" || Mnemonic == "umull"))) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00003862 CanAcceptCarrySet = true;
Jim Grosbachfb9cffe2011-09-16 16:39:25 +00003863 } else
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00003864 CanAcceptCarrySet = false;
Daniel Dunbar3771dd02011-01-11 15:59:53 +00003865
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00003866 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
3867 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
3868 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
3869 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Jim Grosbachad2dad92011-09-06 20:27:04 +00003870 Mnemonic == "dsb" || Mnemonic == "isb" || Mnemonic == "setend" ||
3871 (Mnemonic == "clrex" && !isThumb()) ||
Jim Grosbach0780b632011-08-19 23:24:36 +00003872 (Mnemonic == "nop" && isThumbOne()) ||
Jim Grosbach2bd01182011-10-11 21:55:36 +00003873 ((Mnemonic == "pld" || Mnemonic == "pli" || Mnemonic == "pldw" ||
3874 Mnemonic == "ldc2" || Mnemonic == "ldc2l" ||
3875 Mnemonic == "stc2" || Mnemonic == "stc2l") && !isThumb()) ||
Jim Grosbach4af54a42011-08-26 22:21:51 +00003876 ((Mnemonic.startswith("rfe") || Mnemonic.startswith("srs")) &&
3877 !isThumb()) ||
Jim Grosbach1ad60c22011-09-10 00:15:36 +00003878 Mnemonic.startswith("cps") || (Mnemonic == "movs" && isThumbOne())) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00003879 CanAcceptPredicationCode = false;
Jim Grosbachfb9cffe2011-09-16 16:39:25 +00003880 } else
Daniel Dunbar3771dd02011-01-11 15:59:53 +00003881 CanAcceptPredicationCode = true;
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00003882
Jim Grosbachfb9cffe2011-09-16 16:39:25 +00003883 if (isThumb()) {
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00003884 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00003885 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00003886 CanAcceptPredicationCode = false;
Jim Grosbachfb9cffe2011-09-16 16:39:25 +00003887 }
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00003888}
3889
Jim Grosbachd54b4e62011-08-16 21:12:37 +00003890bool ARMAsmParser::shouldOmitCCOutOperand(StringRef Mnemonic,
3891 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Jim Grosbach20ed2e72011-09-01 00:28:52 +00003892 // FIXME: This is all horribly hacky. We really need a better way to deal
3893 // with optional operands like this in the matcher table.
Jim Grosbachd54b4e62011-08-16 21:12:37 +00003894
3895 // The 'mov' mnemonic is special. One variant has a cc_out operand, while
3896 // another does not. Specifically, the MOVW instruction does not. So we
3897 // special case it here and remove the defaulted (non-setting) cc_out
3898 // operand if that's the instruction we're trying to match.
3899 //
3900 // We do this as post-processing of the explicit operands rather than just
3901 // conditionally adding the cc_out in the first place because we need
3902 // to check the type of the parsed immediate operand.
Owen Anderson8adf6202011-09-14 22:46:14 +00003903 if (Mnemonic == "mov" && Operands.size() > 4 && !isThumb() &&
Jim Grosbachd54b4e62011-08-16 21:12:37 +00003904 !static_cast<ARMOperand*>(Operands[4])->isARMSOImm() &&
3905 static_cast<ARMOperand*>(Operands[4])->isImm0_65535Expr() &&
3906 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
3907 return true;
Jim Grosbach3912b732011-08-16 21:34:08 +00003908
3909 // Register-register 'add' for thumb does not have a cc_out operand
3910 // when there are only two register operands.
3911 if (isThumb() && Mnemonic == "add" && Operands.size() == 5 &&
3912 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3913 static_cast<ARMOperand*>(Operands[4])->isReg() &&
3914 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
3915 return true;
Jim Grosbach72f39f82011-08-24 21:22:15 +00003916 // Register-register 'add' for thumb does not have a cc_out operand
Jim Grosbach20ed2e72011-09-01 00:28:52 +00003917 // when it's an ADD Rdm, SP, {Rdm|#imm0_255} instruction. We do
3918 // have to check the immediate range here since Thumb2 has a variant
3919 // that can handle a different range and has a cc_out operand.
Jim Grosbachf67e8552011-09-16 22:58:42 +00003920 if (((isThumb() && Mnemonic == "add") ||
3921 (isThumbTwo() && Mnemonic == "sub")) &&
3922 Operands.size() == 6 &&
Jim Grosbach72f39f82011-08-24 21:22:15 +00003923 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3924 static_cast<ARMOperand*>(Operands[4])->isReg() &&
3925 static_cast<ARMOperand*>(Operands[4])->getReg() == ARM::SP &&
Jim Grosbach20ed2e72011-09-01 00:28:52 +00003926 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
3927 (static_cast<ARMOperand*>(Operands[5])->isReg() ||
3928 static_cast<ARMOperand*>(Operands[5])->isImm0_1020s4()))
Jim Grosbach72f39f82011-08-24 21:22:15 +00003929 return true;
Jim Grosbachf67e8552011-09-16 22:58:42 +00003930 // For Thumb2, add/sub immediate does not have a cc_out operand for the
3931 // imm0_4095 variant. That's the least-preferred variant when
Jim Grosbach20ed2e72011-09-01 00:28:52 +00003932 // selecting via the generic "add" mnemonic, so to know that we
3933 // should remove the cc_out operand, we have to explicitly check that
3934 // it's not one of the other variants. Ugh.
Jim Grosbachf67e8552011-09-16 22:58:42 +00003935 if (isThumbTwo() && (Mnemonic == "add" || Mnemonic == "sub") &&
3936 Operands.size() == 6 &&
Jim Grosbach20ed2e72011-09-01 00:28:52 +00003937 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3938 static_cast<ARMOperand*>(Operands[4])->isReg() &&
3939 static_cast<ARMOperand*>(Operands[5])->isImm()) {
3940 // Nest conditions rather than one big 'if' statement for readability.
3941 //
3942 // If either register is a high reg, it's either one of the SP
3943 // variants (handled above) or a 32-bit encoding, so we just
3944 // check against T3.
3945 if ((!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
3946 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg())) &&
3947 static_cast<ARMOperand*>(Operands[5])->isT2SOImm())
3948 return false;
3949 // If both registers are low, we're in an IT block, and the immediate is
3950 // in range, we should use encoding T1 instead, which has a cc_out.
3951 if (inITBlock() &&
Jim Grosbach64944f42011-09-14 21:00:40 +00003952 isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) &&
Jim Grosbach20ed2e72011-09-01 00:28:52 +00003953 isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) &&
3954 static_cast<ARMOperand*>(Operands[5])->isImm0_7())
3955 return false;
3956
3957 // Otherwise, we use encoding T4, which does not have a cc_out
3958 // operand.
3959 return true;
3960 }
3961
Jim Grosbach64944f42011-09-14 21:00:40 +00003962 // The thumb2 multiply instruction doesn't have a CCOut register, so
3963 // if we have a "mul" mnemonic in Thumb mode, check if we'll be able to
3964 // use the 16-bit encoding or not.
3965 if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 6 &&
3966 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
3967 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3968 static_cast<ARMOperand*>(Operands[4])->isReg() &&
3969 static_cast<ARMOperand*>(Operands[5])->isReg() &&
3970 // If the registers aren't low regs, the destination reg isn't the
3971 // same as one of the source regs, or the cc_out operand is zero
3972 // outside of an IT block, we have to use the 32-bit encoding, so
3973 // remove the cc_out operand.
3974 (!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
3975 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) ||
3976 !inITBlock() ||
3977 (static_cast<ARMOperand*>(Operands[3])->getReg() !=
3978 static_cast<ARMOperand*>(Operands[5])->getReg() &&
3979 static_cast<ARMOperand*>(Operands[3])->getReg() !=
3980 static_cast<ARMOperand*>(Operands[4])->getReg())))
3981 return true;
3982
3983
Jim Grosbach20ed2e72011-09-01 00:28:52 +00003984
Jim Grosbachf69c8042011-08-24 21:42:27 +00003985 // Register-register 'add/sub' for thumb does not have a cc_out operand
3986 // when it's an ADD/SUB SP, #imm. Be lenient on count since there's also
3987 // the "add/sub SP, SP, #imm" version. If the follow-up operands aren't
3988 // right, this will result in better diagnostics (which operand is off)
3989 // anyway.
3990 if (isThumb() && (Mnemonic == "add" || Mnemonic == "sub") &&
3991 (Operands.size() == 5 || Operands.size() == 6) &&
Jim Grosbach72f39f82011-08-24 21:22:15 +00003992 static_cast<ARMOperand*>(Operands[3])->isReg() &&
3993 static_cast<ARMOperand*>(Operands[3])->getReg() == ARM::SP &&
3994 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
3995 return true;
Jim Grosbach3912b732011-08-16 21:34:08 +00003996
Jim Grosbachd54b4e62011-08-16 21:12:37 +00003997 return false;
3998}
3999
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00004000/// Parse an arm instruction mnemonic followed by its operands.
4001bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
4002 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4003 // Create the leading tokens for the mnemonic, split by '.' characters.
4004 size_t Start = 0, Next = Name.find('.');
Jim Grosbachffa32252011-07-19 19:13:28 +00004005 StringRef Mnemonic = Name.slice(Start, Next);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00004006
Daniel Dunbar352e1482011-01-11 15:59:50 +00004007 // Split out the predication code and carry setting flag from the mnemonic.
4008 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00004009 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00004010 bool CarrySetting;
Jim Grosbach89df9962011-08-26 21:43:41 +00004011 StringRef ITMask;
Jim Grosbach1355cf12011-07-26 17:10:22 +00004012 Mnemonic = splitMnemonic(Mnemonic, PredicationCode, CarrySetting,
Jim Grosbach89df9962011-08-26 21:43:41 +00004013 ProcessorIMod, ITMask);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00004014
Jim Grosbach0c49ac02011-08-25 17:23:55 +00004015 // In Thumb1, only the branch (B) instruction can be predicated.
4016 if (isThumbOne() && PredicationCode != ARMCC::AL && Mnemonic != "b") {
4017 Parser.EatToEndOfStatement();
4018 return Error(NameLoc, "conditional execution not supported in Thumb1");
4019 }
4020
Jim Grosbachffa32252011-07-19 19:13:28 +00004021 Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
4022
Jim Grosbach89df9962011-08-26 21:43:41 +00004023 // Handle the IT instruction ITMask. Convert it to a bitmask. This
4024 // is the mask as it will be for the IT encoding if the conditional
4025 // encoding has a '1' as it's bit0 (i.e. 't' ==> '1'). In the case
4026 // where the conditional bit0 is zero, the instruction post-processing
4027 // will adjust the mask accordingly.
4028 if (Mnemonic == "it") {
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004029 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + 2);
4030 if (ITMask.size() > 3) {
4031 Parser.EatToEndOfStatement();
4032 return Error(Loc, "too many conditions on IT instruction");
4033 }
Jim Grosbach89df9962011-08-26 21:43:41 +00004034 unsigned Mask = 8;
4035 for (unsigned i = ITMask.size(); i != 0; --i) {
4036 char pos = ITMask[i - 1];
4037 if (pos != 't' && pos != 'e') {
4038 Parser.EatToEndOfStatement();
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004039 return Error(Loc, "illegal IT block condition mask '" + ITMask + "'");
Jim Grosbach89df9962011-08-26 21:43:41 +00004040 }
4041 Mask >>= 1;
4042 if (ITMask[i - 1] == 't')
4043 Mask |= 8;
4044 }
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004045 Operands.push_back(ARMOperand::CreateITMask(Mask, Loc));
Jim Grosbach89df9962011-08-26 21:43:41 +00004046 }
4047
Jim Grosbachffa32252011-07-19 19:13:28 +00004048 // FIXME: This is all a pretty gross hack. We should automatically handle
4049 // optional operands like this via tblgen.
Bill Wendling9717fa92010-11-21 10:56:05 +00004050
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004051 // Next, add the CCOut and ConditionCode operands, if needed.
4052 //
4053 // For mnemonics which can ever incorporate a carry setting bit or predication
4054 // code, our matching model involves us always generating CCOut and
4055 // ConditionCode operands to match the mnemonic "as written" and then we let
4056 // the matcher deal with finding the right instruction or generating an
4057 // appropriate error.
4058 bool CanAcceptCarrySet, CanAcceptPredicationCode;
Jim Grosbach1355cf12011-07-26 17:10:22 +00004059 getMnemonicAcceptInfo(Mnemonic, CanAcceptCarrySet, CanAcceptPredicationCode);
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004060
Jim Grosbach33c16a22011-07-14 22:04:21 +00004061 // If we had a carry-set on an instruction that can't do that, issue an
4062 // error.
4063 if (!CanAcceptCarrySet && CarrySetting) {
4064 Parser.EatToEndOfStatement();
Jim Grosbachffa32252011-07-19 19:13:28 +00004065 return Error(NameLoc, "instruction '" + Mnemonic +
Jim Grosbach33c16a22011-07-14 22:04:21 +00004066 "' can not set flags, but 's' suffix specified");
4067 }
Jim Grosbachc27d4f92011-07-22 17:44:50 +00004068 // If we had a predication code on an instruction that can't do that, issue an
4069 // error.
4070 if (!CanAcceptPredicationCode && PredicationCode != ARMCC::AL) {
4071 Parser.EatToEndOfStatement();
4072 return Error(NameLoc, "instruction '" + Mnemonic +
4073 "' is not predicable, but condition code specified");
4074 }
Jim Grosbach33c16a22011-07-14 22:04:21 +00004075
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004076 // Add the carry setting operand, if necessary.
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004077 if (CanAcceptCarrySet) {
4078 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size());
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004079 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004080 Loc));
4081 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004082
4083 // Add the predication code operand, if necessary.
4084 if (CanAcceptPredicationCode) {
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004085 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size() +
4086 CarrySetting);
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004087 Operands.push_back(ARMOperand::CreateCondCode(
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004088 ARMCC::CondCodes(PredicationCode), Loc));
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00004089 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00004090
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00004091 // Add the processor imod operand, if necessary.
4092 if (ProcessorIMod) {
4093 Operands.push_back(ARMOperand::CreateImm(
4094 MCConstantExpr::Create(ProcessorIMod, getContext()),
4095 NameLoc, NameLoc));
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00004096 }
4097
Daniel Dunbar345a9a62010-08-11 06:37:20 +00004098 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00004099 while (Next != StringRef::npos) {
4100 Start = Next;
4101 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00004102 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004103
Jim Grosbach4d23e992011-08-24 22:19:48 +00004104 // For now, we're only parsing Thumb1 (for the most part), so
4105 // just ignore ".n" qualifiers. We'll use them to restrict
4106 // matching when we do Thumb2.
Jim Grosbach81d2e392011-09-07 16:06:04 +00004107 if (ExtraToken != ".n") {
4108 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
4109 Operands.push_back(ARMOperand::CreateToken(ExtraToken, Loc));
4110 }
Daniel Dunbar5747b132010-08-11 06:37:16 +00004111 }
4112
4113 // Read the remaining operands.
4114 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004115 // Read the first operand.
Jim Grosbach1355cf12011-07-26 17:10:22 +00004116 if (parseOperand(Operands, Mnemonic)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00004117 Parser.EatToEndOfStatement();
4118 return true;
4119 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004120
4121 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00004122 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004123
4124 // Parse and remember the operand.
Jim Grosbach1355cf12011-07-26 17:10:22 +00004125 if (parseOperand(Operands, Mnemonic)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00004126 Parser.EatToEndOfStatement();
4127 return true;
4128 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004129 }
4130 }
Jim Grosbach16c74252010-10-29 14:46:02 +00004131
Chris Lattnercbf8a982010-09-11 16:18:25 +00004132 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbach186ffac2011-10-07 18:27:04 +00004133 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00004134 Parser.EatToEndOfStatement();
Jim Grosbach186ffac2011-10-07 18:27:04 +00004135 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00004136 }
Bill Wendling146018f2010-11-06 21:42:12 +00004137
Chris Lattner34e53142010-09-08 05:10:46 +00004138 Parser.Lex(); // Consume the EndOfStatement
Jim Grosbachffa32252011-07-19 19:13:28 +00004139
Jim Grosbachd54b4e62011-08-16 21:12:37 +00004140 // Some instructions, mostly Thumb, have forms for the same mnemonic that
4141 // do and don't have a cc_out optional-def operand. With some spot-checks
4142 // of the operand list, we can figure out which variant we're trying to
Jim Grosbach20ed2e72011-09-01 00:28:52 +00004143 // parse and adjust accordingly before actually matching. We shouldn't ever
4144 // try to remove a cc_out operand that was explicitly set on the the
4145 // mnemonic, of course (CarrySetting == true). Reason number #317 the
4146 // table driven matcher doesn't fit well with the ARM instruction set.
4147 if (!CarrySetting && shouldOmitCCOutOperand(Mnemonic, Operands)) {
Jim Grosbachffa32252011-07-19 19:13:28 +00004148 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
4149 Operands.erase(Operands.begin() + 1);
4150 delete Op;
4151 }
4152
Jim Grosbachcf121c32011-07-28 21:57:55 +00004153 // ARM mode 'blx' need special handling, as the register operand version
4154 // is predicable, but the label operand version is not. So, we can't rely
4155 // on the Mnemonic based checking to correctly figure out when to put
Jim Grosbach21ff17c2011-10-07 23:24:09 +00004156 // a k_CondCode operand in the list. If we're trying to match the label
4157 // version, remove the k_CondCode operand here.
Jim Grosbachcf121c32011-07-28 21:57:55 +00004158 if (!isThumb() && Mnemonic == "blx" && Operands.size() == 3 &&
4159 static_cast<ARMOperand*>(Operands[2])->isImm()) {
4160 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
4161 Operands.erase(Operands.begin() + 1);
4162 delete Op;
4163 }
Jim Grosbach857e1a72011-08-11 23:51:13 +00004164
4165 // The vector-compare-to-zero instructions have a literal token "#0" at
4166 // the end that comes to here as an immediate operand. Convert it to a
4167 // token to play nicely with the matcher.
4168 if ((Mnemonic == "vceq" || Mnemonic == "vcge" || Mnemonic == "vcgt" ||
4169 Mnemonic == "vcle" || Mnemonic == "vclt") && Operands.size() == 6 &&
4170 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4171 ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
4172 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4173 if (CE && CE->getValue() == 0) {
4174 Operands.erase(Operands.begin() + 5);
4175 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4176 delete Op;
4177 }
4178 }
Jim Grosbach68259142011-10-03 22:30:24 +00004179 // VCMP{E} does the same thing, but with a different operand count.
4180 if ((Mnemonic == "vcmp" || Mnemonic == "vcmpe") && Operands.size() == 5 &&
4181 static_cast<ARMOperand*>(Operands[4])->isImm()) {
4182 ARMOperand *Op = static_cast<ARMOperand*>(Operands[4]);
4183 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4184 if (CE && CE->getValue() == 0) {
4185 Operands.erase(Operands.begin() + 4);
4186 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4187 delete Op;
4188 }
4189 }
Jim Grosbach934755a2011-08-22 23:47:13 +00004190 // Similarly, the Thumb1 "RSB" instruction has a literal "#0" on the
4191 // end. Convert it to a token here.
4192 if (Mnemonic == "rsb" && isThumb() && Operands.size() == 6 &&
4193 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4194 ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
4195 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4196 if (CE && CE->getValue() == 0) {
4197 Operands.erase(Operands.begin() + 5);
4198 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4199 delete Op;
4200 }
4201 }
4202
Chris Lattner98986712010-01-14 22:21:20 +00004203 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00004204}
4205
Jim Grosbach189610f2011-07-26 18:25:39 +00004206// Validate context-sensitive operand constraints.
Jim Grosbachaa875f82011-08-23 18:13:04 +00004207
4208// return 'true' if register list contains non-low GPR registers,
4209// 'false' otherwise. If Reg is in the register list or is HiReg, set
4210// 'containsReg' to true.
4211static bool checkLowRegisterList(MCInst Inst, unsigned OpNo, unsigned Reg,
4212 unsigned HiReg, bool &containsReg) {
4213 containsReg = false;
4214 for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
4215 unsigned OpReg = Inst.getOperand(i).getReg();
4216 if (OpReg == Reg)
4217 containsReg = true;
4218 // Anything other than a low register isn't legal here.
4219 if (!isARMLowRegister(OpReg) && (!HiReg || OpReg != HiReg))
4220 return true;
4221 }
4222 return false;
4223}
4224
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00004225// Check if the specified regisgter is in the register list of the inst,
4226// starting at the indicated operand number.
4227static bool listContainsReg(MCInst &Inst, unsigned OpNo, unsigned Reg) {
4228 for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
4229 unsigned OpReg = Inst.getOperand(i).getReg();
4230 if (OpReg == Reg)
4231 return true;
4232 }
4233 return false;
4234}
4235
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004236// FIXME: We would really prefer to have MCInstrInfo (the wrapper around
4237// the ARMInsts array) instead. Getting that here requires awkward
4238// API changes, though. Better way?
4239namespace llvm {
4240extern MCInstrDesc ARMInsts[];
4241}
4242static MCInstrDesc &getInstDesc(unsigned Opcode) {
4243 return ARMInsts[Opcode];
4244}
4245
Jim Grosbach189610f2011-07-26 18:25:39 +00004246// FIXME: We would really like to be able to tablegen'erate this.
4247bool ARMAsmParser::
4248validateInstruction(MCInst &Inst,
4249 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004250 MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
4251 SMLoc Loc = Operands[0]->getStartLoc();
4252 // Check the IT block state first.
Owen Andersonb6b7f512011-09-13 17:59:19 +00004253 // NOTE: In Thumb mode, the BKPT instruction has the interesting property of
4254 // being allowed in IT blocks, but not being predicable. It just always
4255 // executes.
4256 if (inITBlock() && Inst.getOpcode() != ARM::tBKPT) {
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004257 unsigned bit = 1;
4258 if (ITState.FirstCond)
4259 ITState.FirstCond = false;
4260 else
Jim Grosbacha1109882011-09-02 23:22:08 +00004261 bit = (ITState.Mask >> (5 - ITState.CurPosition)) & 1;
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004262 // The instruction must be predicable.
4263 if (!MCID.isPredicable())
4264 return Error(Loc, "instructions in IT block must be predicable");
4265 unsigned Cond = Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm();
4266 unsigned ITCond = bit ? ITState.Cond :
4267 ARMCC::getOppositeCondition(ITState.Cond);
4268 if (Cond != ITCond) {
4269 // Find the condition code Operand to get its SMLoc information.
4270 SMLoc CondLoc;
4271 for (unsigned i = 1; i < Operands.size(); ++i)
4272 if (static_cast<ARMOperand*>(Operands[i])->isCondCode())
4273 CondLoc = Operands[i]->getStartLoc();
4274 return Error(CondLoc, "incorrect condition in IT block; got '" +
4275 StringRef(ARMCondCodeToString(ARMCC::CondCodes(Cond))) +
4276 "', but expected '" +
4277 ARMCondCodeToString(ARMCC::CondCodes(ITCond)) + "'");
4278 }
Jim Grosbachc9a9b442011-08-31 18:29:05 +00004279 // Check for non-'al' condition codes outside of the IT block.
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004280 } else if (isThumbTwo() && MCID.isPredicable() &&
4281 Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
Owen Anderson51f6a7a2011-09-09 21:48:23 +00004282 ARMCC::AL && Inst.getOpcode() != ARM::tB &&
4283 Inst.getOpcode() != ARM::t2B)
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004284 return Error(Loc, "predicated instructions must be in IT block");
4285
Jim Grosbach189610f2011-07-26 18:25:39 +00004286 switch (Inst.getOpcode()) {
Jim Grosbach2fd2b872011-08-10 20:29:19 +00004287 case ARM::LDRD:
4288 case ARM::LDRD_PRE:
4289 case ARM::LDRD_POST:
Jim Grosbach189610f2011-07-26 18:25:39 +00004290 case ARM::LDREXD: {
4291 // Rt2 must be Rt + 1.
4292 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
4293 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4294 if (Rt2 != Rt + 1)
4295 return Error(Operands[3]->getStartLoc(),
4296 "destination operands must be sequential");
4297 return false;
4298 }
Jim Grosbach14605d12011-08-11 20:28:23 +00004299 case ARM::STRD: {
4300 // Rt2 must be Rt + 1.
4301 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
4302 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4303 if (Rt2 != Rt + 1)
4304 return Error(Operands[3]->getStartLoc(),
4305 "source operands must be sequential");
4306 return false;
4307 }
Jim Grosbach53642c52011-08-10 20:49:18 +00004308 case ARM::STRD_PRE:
4309 case ARM::STRD_POST:
Jim Grosbach189610f2011-07-26 18:25:39 +00004310 case ARM::STREXD: {
4311 // Rt2 must be Rt + 1.
4312 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4313 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(2).getReg());
4314 if (Rt2 != Rt + 1)
Jim Grosbach14605d12011-08-11 20:28:23 +00004315 return Error(Operands[3]->getStartLoc(),
Jim Grosbach189610f2011-07-26 18:25:39 +00004316 "source operands must be sequential");
4317 return false;
4318 }
Jim Grosbachfb8989e2011-07-27 21:09:25 +00004319 case ARM::SBFX:
4320 case ARM::UBFX: {
4321 // width must be in range [1, 32-lsb]
4322 unsigned lsb = Inst.getOperand(2).getImm();
4323 unsigned widthm1 = Inst.getOperand(3).getImm();
4324 if (widthm1 >= 32 - lsb)
4325 return Error(Operands[5]->getStartLoc(),
4326 "bitfield width must be in range [1,32-lsb]");
Jim Grosbach00c9a512011-08-16 21:42:31 +00004327 return false;
Jim Grosbachfb8989e2011-07-27 21:09:25 +00004328 }
Jim Grosbach93b3eff2011-08-18 21:50:53 +00004329 case ARM::tLDMIA: {
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00004330 // If we're parsing Thumb2, the .w variant is available and handles
4331 // most cases that are normally illegal for a Thumb1 LDM
4332 // instruction. We'll make the transformation in processInstruction()
4333 // if necessary.
4334 //
Jim Grosbach93b3eff2011-08-18 21:50:53 +00004335 // Thumb LDM instructions are writeback iff the base register is not
4336 // in the register list.
4337 unsigned Rn = Inst.getOperand(0).getReg();
Jim Grosbach7260c6a2011-08-22 23:01:07 +00004338 bool hasWritebackToken =
4339 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
4340 static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
Jim Grosbachaa875f82011-08-23 18:13:04 +00004341 bool listContainsBase;
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00004342 if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) && !isThumbTwo())
Jim Grosbachaa875f82011-08-23 18:13:04 +00004343 return Error(Operands[3 + hasWritebackToken]->getStartLoc(),
4344 "registers must be in range r0-r7");
Jim Grosbach93b3eff2011-08-18 21:50:53 +00004345 // If we should have writeback, then there should be a '!' token.
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00004346 if (!listContainsBase && !hasWritebackToken && !isThumbTwo())
Jim Grosbach93b3eff2011-08-18 21:50:53 +00004347 return Error(Operands[2]->getStartLoc(),
4348 "writeback operator '!' expected");
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00004349 // If we should not have writeback, there must not be a '!'. This is
4350 // true even for the 32-bit wide encodings.
Jim Grosbachaa875f82011-08-23 18:13:04 +00004351 if (listContainsBase && hasWritebackToken)
Jim Grosbach7260c6a2011-08-22 23:01:07 +00004352 return Error(Operands[3]->getStartLoc(),
4353 "writeback operator '!' not allowed when base register "
4354 "in register list");
Jim Grosbach93b3eff2011-08-18 21:50:53 +00004355
4356 break;
4357 }
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00004358 case ARM::t2LDMIA_UPD: {
4359 if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
4360 return Error(Operands[4]->getStartLoc(),
4361 "writeback operator '!' not allowed when base register "
4362 "in register list");
4363 break;
4364 }
Jim Grosbach6dcafc02011-08-22 23:17:34 +00004365 case ARM::tPOP: {
Jim Grosbachaa875f82011-08-23 18:13:04 +00004366 bool listContainsBase;
4367 if (checkLowRegisterList(Inst, 3, 0, ARM::PC, listContainsBase))
4368 return Error(Operands[2]->getStartLoc(),
4369 "registers must be in range r0-r7 or pc");
Jim Grosbach6dcafc02011-08-22 23:17:34 +00004370 break;
4371 }
4372 case ARM::tPUSH: {
Jim Grosbachaa875f82011-08-23 18:13:04 +00004373 bool listContainsBase;
4374 if (checkLowRegisterList(Inst, 3, 0, ARM::LR, listContainsBase))
4375 return Error(Operands[2]->getStartLoc(),
4376 "registers must be in range r0-r7 or lr");
Jim Grosbach6dcafc02011-08-22 23:17:34 +00004377 break;
4378 }
Jim Grosbach1e84f192011-08-23 18:15:37 +00004379 case ARM::tSTMIA_UPD: {
4380 bool listContainsBase;
Jim Grosbach8213c962011-09-16 20:50:13 +00004381 if (checkLowRegisterList(Inst, 4, 0, 0, listContainsBase) && !isThumbTwo())
Jim Grosbach1e84f192011-08-23 18:15:37 +00004382 return Error(Operands[4]->getStartLoc(),
4383 "registers must be in range r0-r7");
4384 break;
4385 }
Jim Grosbach189610f2011-07-26 18:25:39 +00004386 }
4387
4388 return false;
4389}
4390
Jim Grosbachf8fce712011-08-11 17:35:48 +00004391void ARMAsmParser::
4392processInstruction(MCInst &Inst,
4393 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4394 switch (Inst.getOpcode()) {
4395 case ARM::LDMIA_UPD:
4396 // If this is a load of a single register via a 'pop', then we should use
4397 // a post-indexed LDR instruction instead, per the ARM ARM.
4398 if (static_cast<ARMOperand*>(Operands[0])->getToken() == "pop" &&
4399 Inst.getNumOperands() == 5) {
4400 MCInst TmpInst;
4401 TmpInst.setOpcode(ARM::LDR_POST_IMM);
4402 TmpInst.addOperand(Inst.getOperand(4)); // Rt
4403 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
4404 TmpInst.addOperand(Inst.getOperand(1)); // Rn
4405 TmpInst.addOperand(MCOperand::CreateReg(0)); // am2offset
4406 TmpInst.addOperand(MCOperand::CreateImm(4));
4407 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
4408 TmpInst.addOperand(Inst.getOperand(3));
4409 Inst = TmpInst;
4410 }
4411 break;
Jim Grosbachf6713912011-08-11 18:07:11 +00004412 case ARM::STMDB_UPD:
4413 // If this is a store of a single register via a 'push', then we should use
4414 // a pre-indexed STR instruction instead, per the ARM ARM.
4415 if (static_cast<ARMOperand*>(Operands[0])->getToken() == "push" &&
4416 Inst.getNumOperands() == 5) {
4417 MCInst TmpInst;
4418 TmpInst.setOpcode(ARM::STR_PRE_IMM);
4419 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
4420 TmpInst.addOperand(Inst.getOperand(4)); // Rt
4421 TmpInst.addOperand(Inst.getOperand(1)); // addrmode_imm12
4422 TmpInst.addOperand(MCOperand::CreateImm(-4));
4423 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
4424 TmpInst.addOperand(Inst.getOperand(3));
4425 Inst = TmpInst;
4426 }
4427 break;
Jim Grosbach89e2aa62011-08-16 23:57:34 +00004428 case ARM::tADDi8:
Jim Grosbach0f3abd82011-08-31 17:07:33 +00004429 // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
4430 // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
4431 // to encoding T2 if <Rd> is specified and encoding T2 is preferred
4432 // to encoding T1 if <Rd> is omitted."
4433 if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6)
Jim Grosbach89e2aa62011-08-16 23:57:34 +00004434 Inst.setOpcode(ARM::tADDi3);
4435 break;
Jim Grosbachf67e8552011-09-16 22:58:42 +00004436 case ARM::tSUBi8:
4437 // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
4438 // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
4439 // to encoding T2 if <Rd> is specified and encoding T2 is preferred
4440 // to encoding T1 if <Rd> is omitted."
4441 if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6)
4442 Inst.setOpcode(ARM::tSUBi3);
4443 break;
Owen Anderson51f6a7a2011-09-09 21:48:23 +00004444 case ARM::tB:
4445 // A Thumb conditional branch outside of an IT block is a tBcc.
4446 if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock())
4447 Inst.setOpcode(ARM::tBcc);
4448 break;
4449 case ARM::t2B:
4450 // A Thumb2 conditional branch outside of an IT block is a t2Bcc.
4451 if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock())
4452 Inst.setOpcode(ARM::t2Bcc);
4453 break;
Jim Grosbachc0755102011-08-31 21:17:31 +00004454 case ARM::t2Bcc:
Jim Grosbacha1109882011-09-02 23:22:08 +00004455 // If the conditional is AL or we're in an IT block, we really want t2B.
4456 if (Inst.getOperand(1).getImm() == ARMCC::AL || inITBlock())
Jim Grosbachc0755102011-08-31 21:17:31 +00004457 Inst.setOpcode(ARM::t2B);
4458 break;
Jim Grosbach395b4532011-08-17 22:57:40 +00004459 case ARM::tBcc:
4460 // If the conditional is AL, we really want tB.
4461 if (Inst.getOperand(1).getImm() == ARMCC::AL)
4462 Inst.setOpcode(ARM::tB);
Jim Grosbach3ce23d32011-08-18 16:08:39 +00004463 break;
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00004464 case ARM::tLDMIA: {
4465 // If the register list contains any high registers, or if the writeback
4466 // doesn't match what tLDMIA can do, we need to use the 32-bit encoding
4467 // instead if we're in Thumb2. Otherwise, this should have generated
4468 // an error in validateInstruction().
4469 unsigned Rn = Inst.getOperand(0).getReg();
4470 bool hasWritebackToken =
4471 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
4472 static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
4473 bool listContainsBase;
4474 if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) ||
4475 (!listContainsBase && !hasWritebackToken) ||
4476 (listContainsBase && hasWritebackToken)) {
4477 // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
4478 assert (isThumbTwo());
4479 Inst.setOpcode(hasWritebackToken ? ARM::t2LDMIA_UPD : ARM::t2LDMIA);
4480 // If we're switching to the updating version, we need to insert
4481 // the writeback tied operand.
4482 if (hasWritebackToken)
4483 Inst.insert(Inst.begin(),
4484 MCOperand::CreateReg(Inst.getOperand(0).getReg()));
4485 }
4486 break;
4487 }
Jim Grosbach8213c962011-09-16 20:50:13 +00004488 case ARM::tSTMIA_UPD: {
4489 // If the register list contains any high registers, we need to use
4490 // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
4491 // should have generated an error in validateInstruction().
4492 unsigned Rn = Inst.getOperand(0).getReg();
4493 bool listContainsBase;
4494 if (checkLowRegisterList(Inst, 4, Rn, 0, listContainsBase)) {
4495 // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
4496 assert (isThumbTwo());
4497 Inst.setOpcode(ARM::t2STMIA_UPD);
4498 }
4499 break;
4500 }
Jim Grosbach1ad60c22011-09-10 00:15:36 +00004501 case ARM::t2MOVi: {
4502 // If we can use the 16-bit encoding and the user didn't explicitly
4503 // request the 32-bit variant, transform it here.
4504 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4505 Inst.getOperand(1).getImm() <= 255 &&
Jim Grosbachc2d31642011-09-14 19:12:11 +00004506 ((!inITBlock() && Inst.getOperand(2).getImm() == ARMCC::AL &&
4507 Inst.getOperand(4).getReg() == ARM::CPSR) ||
4508 (inITBlock() && Inst.getOperand(4).getReg() == 0)) &&
Jim Grosbach1ad60c22011-09-10 00:15:36 +00004509 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4510 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4511 // The operands aren't in the same order for tMOVi8...
4512 MCInst TmpInst;
4513 TmpInst.setOpcode(ARM::tMOVi8);
4514 TmpInst.addOperand(Inst.getOperand(0));
4515 TmpInst.addOperand(Inst.getOperand(4));
4516 TmpInst.addOperand(Inst.getOperand(1));
4517 TmpInst.addOperand(Inst.getOperand(2));
4518 TmpInst.addOperand(Inst.getOperand(3));
4519 Inst = TmpInst;
4520 }
4521 break;
4522 }
4523 case ARM::t2MOVr: {
4524 // If we can use the 16-bit encoding and the user didn't explicitly
4525 // request the 32-bit variant, transform it here.
4526 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4527 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4528 Inst.getOperand(2).getImm() == ARMCC::AL &&
4529 Inst.getOperand(4).getReg() == ARM::CPSR &&
4530 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4531 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
4532 // The operands aren't the same for tMOV[S]r... (no cc_out)
4533 MCInst TmpInst;
4534 TmpInst.setOpcode(Inst.getOperand(4).getReg() ? ARM::tMOVSr : ARM::tMOVr);
4535 TmpInst.addOperand(Inst.getOperand(0));
4536 TmpInst.addOperand(Inst.getOperand(1));
4537 TmpInst.addOperand(Inst.getOperand(2));
4538 TmpInst.addOperand(Inst.getOperand(3));
4539 Inst = TmpInst;
4540 }
4541 break;
4542 }
Jim Grosbach326efe52011-09-19 20:29:33 +00004543 case ARM::t2SXTH:
Jim Grosbach50f1c372011-09-20 00:46:54 +00004544 case ARM::t2SXTB:
4545 case ARM::t2UXTH:
4546 case ARM::t2UXTB: {
Jim Grosbach326efe52011-09-19 20:29:33 +00004547 // If we can use the 16-bit encoding and the user didn't explicitly
4548 // request the 32-bit variant, transform it here.
4549 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
4550 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4551 Inst.getOperand(2).getImm() == 0 &&
4552 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
4553 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
Jim Grosbach50f1c372011-09-20 00:46:54 +00004554 unsigned NewOpc;
4555 switch (Inst.getOpcode()) {
4556 default: llvm_unreachable("Illegal opcode!");
4557 case ARM::t2SXTH: NewOpc = ARM::tSXTH; break;
4558 case ARM::t2SXTB: NewOpc = ARM::tSXTB; break;
4559 case ARM::t2UXTH: NewOpc = ARM::tUXTH; break;
4560 case ARM::t2UXTB: NewOpc = ARM::tUXTB; break;
4561 }
Jim Grosbach326efe52011-09-19 20:29:33 +00004562 // The operands aren't the same for thumb1 (no rotate operand).
4563 MCInst TmpInst;
4564 TmpInst.setOpcode(NewOpc);
4565 TmpInst.addOperand(Inst.getOperand(0));
4566 TmpInst.addOperand(Inst.getOperand(1));
4567 TmpInst.addOperand(Inst.getOperand(3));
4568 TmpInst.addOperand(Inst.getOperand(4));
4569 Inst = TmpInst;
4570 }
4571 break;
4572 }
Jim Grosbach89df9962011-08-26 21:43:41 +00004573 case ARM::t2IT: {
4574 // The mask bits for all but the first condition are represented as
4575 // the low bit of the condition code value implies 't'. We currently
4576 // always have 1 implies 't', so XOR toggle the bits if the low bit
4577 // of the condition code is zero. The encoding also expects the low
4578 // bit of the condition to be encoded as bit 4 of the mask operand,
4579 // so mask that in if needed
4580 MCOperand &MO = Inst.getOperand(1);
4581 unsigned Mask = MO.getImm();
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004582 unsigned OrigMask = Mask;
4583 unsigned TZ = CountTrailingZeros_32(Mask);
Jim Grosbach89df9962011-08-26 21:43:41 +00004584 if ((Inst.getOperand(0).getImm() & 1) == 0) {
Jim Grosbach89df9962011-08-26 21:43:41 +00004585 assert(Mask && TZ <= 3 && "illegal IT mask value!");
4586 for (unsigned i = 3; i != TZ; --i)
4587 Mask ^= 1 << i;
4588 } else
4589 Mask |= 0x10;
4590 MO.setImm(Mask);
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004591
4592 // Set up the IT block state according to the IT instruction we just
4593 // matched.
4594 assert(!inITBlock() && "nested IT blocks?!");
4595 ITState.Cond = ARMCC::CondCodes(Inst.getOperand(0).getImm());
4596 ITState.Mask = OrigMask; // Use the original mask, not the updated one.
4597 ITState.CurPosition = 0;
4598 ITState.FirstCond = true;
Jim Grosbach89df9962011-08-26 21:43:41 +00004599 break;
4600 }
Jim Grosbachf8fce712011-08-11 17:35:48 +00004601 }
4602}
4603
Jim Grosbach47a0d522011-08-16 20:45:50 +00004604unsigned ARMAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
4605 // 16-bit thumb arithmetic instructions either require or preclude the 'S'
4606 // suffix depending on whether they're in an IT block or not.
Jim Grosbach194bd892011-08-16 22:20:01 +00004607 unsigned Opc = Inst.getOpcode();
4608 MCInstrDesc &MCID = getInstDesc(Opc);
Jim Grosbach47a0d522011-08-16 20:45:50 +00004609 if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
4610 assert(MCID.hasOptionalDef() &&
4611 "optionally flag setting instruction missing optional def operand");
4612 assert(MCID.NumOperands == Inst.getNumOperands() &&
4613 "operand count mismatch!");
4614 // Find the optional-def operand (cc_out).
4615 unsigned OpNo;
4616 for (OpNo = 0;
4617 !MCID.OpInfo[OpNo].isOptionalDef() && OpNo < MCID.NumOperands;
4618 ++OpNo)
4619 ;
4620 // If we're parsing Thumb1, reject it completely.
4621 if (isThumbOne() && Inst.getOperand(OpNo).getReg() != ARM::CPSR)
4622 return Match_MnemonicFail;
4623 // If we're parsing Thumb2, which form is legal depends on whether we're
4624 // in an IT block.
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004625 if (isThumbTwo() && Inst.getOperand(OpNo).getReg() != ARM::CPSR &&
4626 !inITBlock())
Jim Grosbach47a0d522011-08-16 20:45:50 +00004627 return Match_RequiresITBlock;
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004628 if (isThumbTwo() && Inst.getOperand(OpNo).getReg() == ARM::CPSR &&
4629 inITBlock())
4630 return Match_RequiresNotITBlock;
Jim Grosbach47a0d522011-08-16 20:45:50 +00004631 }
Jim Grosbach194bd892011-08-16 22:20:01 +00004632 // Some high-register supporting Thumb1 encodings only allow both registers
4633 // to be from r0-r7 when in Thumb2.
4634 else if (Opc == ARM::tADDhirr && isThumbOne() &&
4635 isARMLowRegister(Inst.getOperand(1).getReg()) &&
4636 isARMLowRegister(Inst.getOperand(2).getReg()))
4637 return Match_RequiresThumb2;
4638 // Others only require ARMv6 or later.
Jim Grosbach4ec6e882011-08-19 20:46:54 +00004639 else if (Opc == ARM::tMOVr && isThumbOne() && !hasV6Ops() &&
Jim Grosbach194bd892011-08-16 22:20:01 +00004640 isARMLowRegister(Inst.getOperand(0).getReg()) &&
4641 isARMLowRegister(Inst.getOperand(1).getReg()))
4642 return Match_RequiresV6;
Jim Grosbach47a0d522011-08-16 20:45:50 +00004643 return Match_Success;
4644}
4645
Chris Lattnerfa42fad2010-10-28 21:28:01 +00004646bool ARMAsmParser::
4647MatchAndEmitInstruction(SMLoc IDLoc,
4648 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
4649 MCStreamer &Out) {
4650 MCInst Inst;
4651 unsigned ErrorInfo;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00004652 unsigned MatchResult;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00004653 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00004654 switch (MatchResult) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00004655 default: break;
Chris Lattnere73d4f82010-10-28 21:41:58 +00004656 case Match_Success:
Jim Grosbach189610f2011-07-26 18:25:39 +00004657 // Context sensitive operand constraints aren't handled by the matcher,
4658 // so check them here.
Jim Grosbacha1109882011-09-02 23:22:08 +00004659 if (validateInstruction(Inst, Operands)) {
4660 // Still progress the IT block, otherwise one wrong condition causes
4661 // nasty cascading errors.
4662 forwardITPosition();
Jim Grosbach189610f2011-07-26 18:25:39 +00004663 return true;
Jim Grosbacha1109882011-09-02 23:22:08 +00004664 }
Jim Grosbach189610f2011-07-26 18:25:39 +00004665
Jim Grosbachf8fce712011-08-11 17:35:48 +00004666 // Some instructions need post-processing to, for example, tweak which
4667 // encoding is selected.
4668 processInstruction(Inst, Operands);
4669
Jim Grosbacha1109882011-09-02 23:22:08 +00004670 // Only move forward at the very end so that everything in validate
4671 // and process gets a consistent answer about whether we're in an IT
4672 // block.
4673 forwardITPosition();
4674
Chris Lattnerfa42fad2010-10-28 21:28:01 +00004675 Out.EmitInstruction(Inst);
4676 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00004677 case Match_MissingFeature:
4678 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
4679 return true;
4680 case Match_InvalidOperand: {
4681 SMLoc ErrorLoc = IDLoc;
4682 if (ErrorInfo != ~0U) {
4683 if (ErrorInfo >= Operands.size())
4684 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00004685
Chris Lattnere73d4f82010-10-28 21:41:58 +00004686 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
4687 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
4688 }
Jim Grosbach16c74252010-10-29 14:46:02 +00004689
Chris Lattnere73d4f82010-10-28 21:41:58 +00004690 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00004691 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00004692 case Match_MnemonicFail:
Jim Grosbach47a0d522011-08-16 20:45:50 +00004693 return Error(IDLoc, "invalid instruction");
Daniel Dunbarb4129152011-02-04 17:12:23 +00004694 case Match_ConversionFail:
Jim Grosbach88ae2bc2011-08-19 22:07:46 +00004695 // The converter function will have already emited a diagnostic.
4696 return true;
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004697 case Match_RequiresNotITBlock:
4698 return Error(IDLoc, "flag setting instruction only valid outside IT block");
Jim Grosbach47a0d522011-08-16 20:45:50 +00004699 case Match_RequiresITBlock:
4700 return Error(IDLoc, "instruction only valid inside IT block");
Jim Grosbach194bd892011-08-16 22:20:01 +00004701 case Match_RequiresV6:
4702 return Error(IDLoc, "instruction variant requires ARMv6 or later");
4703 case Match_RequiresThumb2:
4704 return Error(IDLoc, "instruction variant requires Thumb2");
Chris Lattnere73d4f82010-10-28 21:41:58 +00004705 }
Jim Grosbach16c74252010-10-29 14:46:02 +00004706
Eric Christopherc223e2b2010-10-29 09:26:59 +00004707 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00004708 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00004709}
4710
Jim Grosbach1355cf12011-07-26 17:10:22 +00004711/// parseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00004712bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
4713 StringRef IDVal = DirectiveID.getIdentifier();
4714 if (IDVal == ".word")
Jim Grosbach1355cf12011-07-26 17:10:22 +00004715 return parseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00004716 else if (IDVal == ".thumb")
Jim Grosbach1355cf12011-07-26 17:10:22 +00004717 return parseDirectiveThumb(DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00004718 else if (IDVal == ".thumb_func")
Jim Grosbach1355cf12011-07-26 17:10:22 +00004719 return parseDirectiveThumbFunc(DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00004720 else if (IDVal == ".code")
Jim Grosbach1355cf12011-07-26 17:10:22 +00004721 return parseDirectiveCode(DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00004722 else if (IDVal == ".syntax")
Jim Grosbach1355cf12011-07-26 17:10:22 +00004723 return parseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00004724 return true;
4725}
4726
Jim Grosbach1355cf12011-07-26 17:10:22 +00004727/// parseDirectiveWord
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00004728/// ::= .word [ expression (, expression)* ]
Jim Grosbach1355cf12011-07-26 17:10:22 +00004729bool ARMAsmParser::parseDirectiveWord(unsigned Size, SMLoc L) {
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00004730 if (getLexer().isNot(AsmToken::EndOfStatement)) {
4731 for (;;) {
4732 const MCExpr *Value;
4733 if (getParser().ParseExpression(Value))
4734 return true;
4735
Chris Lattneraaec2052010-01-19 19:46:13 +00004736 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00004737
4738 if (getLexer().is(AsmToken::EndOfStatement))
4739 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00004740
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00004741 // FIXME: Improve diagnostic.
4742 if (getLexer().isNot(AsmToken::Comma))
4743 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00004744 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00004745 }
4746 }
4747
Sean Callananb9a25b72010-01-19 20:27:46 +00004748 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00004749 return false;
4750}
4751
Jim Grosbach1355cf12011-07-26 17:10:22 +00004752/// parseDirectiveThumb
Kevin Enderby515d5092009-10-15 20:48:48 +00004753/// ::= .thumb
Jim Grosbach1355cf12011-07-26 17:10:22 +00004754bool ARMAsmParser::parseDirectiveThumb(SMLoc L) {
Kevin Enderby515d5092009-10-15 20:48:48 +00004755 if (getLexer().isNot(AsmToken::EndOfStatement))
4756 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00004757 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00004758
4759 // TODO: set thumb mode
4760 // TODO: tell the MC streamer the mode
4761 // getParser().getStreamer().Emit???();
4762 return false;
4763}
4764
Jim Grosbach1355cf12011-07-26 17:10:22 +00004765/// parseDirectiveThumbFunc
Kevin Enderby515d5092009-10-15 20:48:48 +00004766/// ::= .thumbfunc symbol_name
Jim Grosbach1355cf12011-07-26 17:10:22 +00004767bool ARMAsmParser::parseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00004768 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
4769 bool isMachO = MAI.hasSubsectionsViaSymbols();
4770 StringRef Name;
4771
4772 // Darwin asm has function name after .thumb_func direction
4773 // ELF doesn't
4774 if (isMachO) {
4775 const AsmToken &Tok = Parser.getTok();
4776 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
4777 return Error(L, "unexpected token in .thumb_func directive");
4778 Name = Tok.getString();
4779 Parser.Lex(); // Consume the identifier token.
4780 }
4781
Kevin Enderby515d5092009-10-15 20:48:48 +00004782 if (getLexer().isNot(AsmToken::EndOfStatement))
4783 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00004784 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00004785
Rafael Espindola64695402011-05-16 16:17:21 +00004786 // FIXME: assuming function name will be the line following .thumb_func
4787 if (!isMachO) {
4788 Name = Parser.getTok().getString();
4789 }
4790
Jim Grosbach642fc9c2010-11-05 22:33:53 +00004791 // Mark symbol as a thumb symbol.
4792 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
4793 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00004794 return false;
4795}
4796
Jim Grosbach1355cf12011-07-26 17:10:22 +00004797/// parseDirectiveSyntax
Kevin Enderby515d5092009-10-15 20:48:48 +00004798/// ::= .syntax unified | divided
Jim Grosbach1355cf12011-07-26 17:10:22 +00004799bool ARMAsmParser::parseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00004800 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00004801 if (Tok.isNot(AsmToken::Identifier))
4802 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00004803 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00004804 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00004805 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00004806 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00004807 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00004808 else
4809 return Error(L, "unrecognized syntax mode in .syntax directive");
4810
4811 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00004812 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00004813 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00004814
4815 // TODO tell the MC streamer the mode
4816 // getParser().getStreamer().Emit???();
4817 return false;
4818}
4819
Jim Grosbach1355cf12011-07-26 17:10:22 +00004820/// parseDirectiveCode
Kevin Enderby515d5092009-10-15 20:48:48 +00004821/// ::= .code 16 | 32
Jim Grosbach1355cf12011-07-26 17:10:22 +00004822bool ARMAsmParser::parseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00004823 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00004824 if (Tok.isNot(AsmToken::Integer))
4825 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00004826 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00004827 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00004828 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00004829 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00004830 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00004831 else
4832 return Error(L, "invalid operand to .code directive");
4833
4834 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00004835 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00004836 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00004837
Evan Cheng32869202011-07-08 22:36:29 +00004838 if (Val == 16) {
Jim Grosbach98447da2011-09-06 18:46:23 +00004839 if (!isThumb())
Evan Chengffc0e732011-07-09 05:47:46 +00004840 SwitchMode();
Jim Grosbach98447da2011-09-06 18:46:23 +00004841 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Evan Cheng32869202011-07-08 22:36:29 +00004842 } else {
Jim Grosbach98447da2011-09-06 18:46:23 +00004843 if (isThumb())
Evan Chengffc0e732011-07-09 05:47:46 +00004844 SwitchMode();
Jim Grosbach98447da2011-09-06 18:46:23 +00004845 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Evan Chengeb0caa12011-07-08 22:49:55 +00004846 }
Jim Grosbach2a301702010-11-05 22:40:53 +00004847
Kevin Enderby515d5092009-10-15 20:48:48 +00004848 return false;
4849}
4850
Sean Callanan90b70972010-04-07 20:29:34 +00004851extern "C" void LLVMInitializeARMAsmLexer();
4852
Kevin Enderby9c41fa82009-10-30 22:55:57 +00004853/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00004854extern "C" void LLVMInitializeARMAsmParser() {
Evan Cheng94b95502011-07-26 00:24:13 +00004855 RegisterMCAsmParser<ARMAsmParser> X(TheARMTarget);
4856 RegisterMCAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00004857 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00004858}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00004859
Chris Lattner0692ee62010-09-06 19:11:01 +00004860#define GET_REGISTER_MATCHER
4861#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00004862#include "ARMGenAsmMatcher.inc"