blob: aa65e747e41f0b0abdb9158ea510ed8e99777a8e [file] [log] [blame]
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001//===-- ARMAsmParser.cpp - Parse ARM assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Evan Cheng94b95502011-07-26 00:24:13 +000010#include "MCTargetDesc/ARMBaseInfo.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000011#include "MCTargetDesc/ARMAddressingModes.h"
12#include "MCTargetDesc/ARMMCExpr.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000013#include "llvm/MC/MCParser/MCAsmLexer.h"
14#include "llvm/MC/MCParser/MCAsmParser.h"
15#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Rafael Espindola64695402011-05-16 16:17:21 +000016#include "llvm/MC/MCAsmInfo.h"
Jim Grosbach642fc9c2010-11-05 22:33:53 +000017#include "llvm/MC/MCContext.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000018#include "llvm/MC/MCStreamer.h"
19#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCInst.h"
Evan Cheng78011362011-08-23 20:15:21 +000021#include "llvm/MC/MCInstrDesc.h"
Evan Cheng94b95502011-07-26 00:24:13 +000022#include "llvm/MC/MCRegisterInfo.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000023#include "llvm/MC/MCSubtargetInfo.h"
Evan Cheng94b95502011-07-26 00:24:13 +000024#include "llvm/MC/MCTargetAsmParser.h"
Jim Grosbach89df9962011-08-26 21:43:41 +000025#include "llvm/Support/MathExtras.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000026#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000027#include "llvm/Support/TargetRegistry.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000028#include "llvm/Support/raw_ostream.h"
Jim Grosbach11e03e72011-08-22 18:50:36 +000029#include "llvm/ADT/BitVector.h"
Benjamin Kramer75ca4b92011-07-08 21:06:23 +000030#include "llvm/ADT/OwningPtr.h"
Evan Cheng94b95502011-07-26 00:24:13 +000031#include "llvm/ADT/STLExtras.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000032#include "llvm/ADT/SmallVector.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000033#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000034#include "llvm/ADT/Twine.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000035
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000036using namespace llvm;
37
Chris Lattner3a697562010-10-28 17:20:03 +000038namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000039
40class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000041
Jim Grosbach7636bf62011-12-02 00:35:16 +000042enum VectorLaneTy { NoLanes, AllLanes, IndexedLane };
Jim Grosbach98b05a52011-11-30 01:09:44 +000043
Evan Cheng94b95502011-07-26 00:24:13 +000044class ARMAsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000045 MCSubtargetInfo &STI;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000046 MCAsmParser &Parser;
47
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +000048 struct {
49 ARMCC::CondCodes Cond; // Condition for IT block.
50 unsigned Mask:4; // Condition mask for instructions.
51 // Starting at first 1 (from lsb).
52 // '1' condition as indicated in IT.
53 // '0' inverse of condition (else).
54 // Count of instructions in IT block is
55 // 4 - trailingzeroes(mask)
56
57 bool FirstCond; // Explicit flag for when we're parsing the
58 // First instruction in the IT block. It's
59 // implied in the mask, so needs special
60 // handling.
61
62 unsigned CurPosition; // Current position in parsing of IT
63 // block. In range [0,3]. Initialized
64 // according to count of instructions in block.
65 // ~0U if no active IT block.
66 } ITState;
67 bool inITBlock() { return ITState.CurPosition != ~0U;}
Jim Grosbacha1109882011-09-02 23:22:08 +000068 void forwardITPosition() {
69 if (!inITBlock()) return;
70 // Move to the next instruction in the IT block, if there is one. If not,
71 // mark the block as done.
72 unsigned TZ = CountTrailingZeros_32(ITState.Mask);
73 if (++ITState.CurPosition == 5 - TZ)
74 ITState.CurPosition = ~0U; // Done with the IT block after this.
75 }
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +000076
77
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000078 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000079 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
80
81 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000082 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
83
Jim Grosbach1355cf12011-07-26 17:10:22 +000084 int tryParseRegister();
85 bool tryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach0d87ec22011-07-26 20:41:24 +000086 int tryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach1355cf12011-07-26 17:10:22 +000087 bool parseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach7ce05792011-08-03 23:50:40 +000088 bool parseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach1355cf12011-07-26 17:10:22 +000089 bool parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
90 bool parsePrefix(ARMMCExpr::VariantKind &RefKind);
Jim Grosbach7ce05792011-08-03 23:50:40 +000091 bool parseMemRegOffsetShift(ARM_AM::ShiftOpc &ShiftType,
92 unsigned &ShiftAmount);
Jim Grosbach1355cf12011-07-26 17:10:22 +000093 bool parseDirectiveWord(unsigned Size, SMLoc L);
94 bool parseDirectiveThumb(SMLoc L);
Jim Grosbach9a70df92011-12-07 18:04:19 +000095 bool parseDirectiveARM(SMLoc L);
Jim Grosbach1355cf12011-07-26 17:10:22 +000096 bool parseDirectiveThumbFunc(SMLoc L);
97 bool parseDirectiveCode(SMLoc L);
98 bool parseDirectiveSyntax(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000099
Jim Grosbach1355cf12011-07-26 17:10:22 +0000100 StringRef splitMnemonic(StringRef Mnemonic, unsigned &PredicationCode,
Jim Grosbach89df9962011-08-26 21:43:41 +0000101 bool &CarrySetting, unsigned &ProcessorIMod,
102 StringRef &ITMask);
Jim Grosbach1355cf12011-07-26 17:10:22 +0000103 void getMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +0000104 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +0000105
Evan Chengebdeeab2011-07-08 01:53:10 +0000106 bool isThumb() const {
107 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +0000108 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +0000109 }
Evan Chengebdeeab2011-07-08 01:53:10 +0000110 bool isThumbOne() const {
Evan Chengffc0e732011-07-09 05:47:46 +0000111 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) == 0;
Evan Chengebdeeab2011-07-08 01:53:10 +0000112 }
Jim Grosbach47a0d522011-08-16 20:45:50 +0000113 bool isThumbTwo() const {
114 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2);
115 }
Jim Grosbach194bd892011-08-16 22:20:01 +0000116 bool hasV6Ops() const {
117 return STI.getFeatureBits() & ARM::HasV6Ops;
118 }
James Molloyacad68d2011-09-28 14:21:38 +0000119 bool hasV7Ops() const {
120 return STI.getFeatureBits() & ARM::HasV7Ops;
121 }
Evan Cheng32869202011-07-08 22:36:29 +0000122 void SwitchMode() {
Evan Chengffc0e732011-07-09 05:47:46 +0000123 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
124 setAvailableFeatures(FB);
Evan Cheng32869202011-07-08 22:36:29 +0000125 }
James Molloyacad68d2011-09-28 14:21:38 +0000126 bool isMClass() const {
127 return STI.getFeatureBits() & ARM::FeatureMClass;
128 }
Evan Chengebdeeab2011-07-08 01:53:10 +0000129
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000130 /// @name Auto-generated Match Functions
131 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000132
Chris Lattner0692ee62010-09-06 19:11:01 +0000133#define GET_ASSEMBLER_HEADER
134#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000135
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000136 /// }
137
Jim Grosbach89df9962011-08-26 21:43:41 +0000138 OperandMatchResultTy parseITCondCode(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach43904292011-07-25 20:14:50 +0000139 OperandMatchResultTy parseCoprocNumOperand(
Jim Grosbachf922c472011-02-12 01:34:40 +0000140 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach43904292011-07-25 20:14:50 +0000141 OperandMatchResultTy parseCoprocRegOperand(
Jim Grosbachf922c472011-02-12 01:34:40 +0000142 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach9b8f2a02011-10-12 17:34:41 +0000143 OperandMatchResultTy parseCoprocOptionOperand(
144 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach43904292011-07-25 20:14:50 +0000145 OperandMatchResultTy parseMemBarrierOptOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000146 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach43904292011-07-25 20:14:50 +0000147 OperandMatchResultTy parseProcIFlagsOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000148 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach43904292011-07-25 20:14:50 +0000149 OperandMatchResultTy parseMSRMaskOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000150 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbachf6c05252011-07-21 17:23:04 +0000151 OperandMatchResultTy parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &O,
152 StringRef Op, int Low, int High);
153 OperandMatchResultTy parsePKHLSLImm(SmallVectorImpl<MCParsedAsmOperand*> &O) {
154 return parsePKHImm(O, "lsl", 0, 31);
155 }
156 OperandMatchResultTy parsePKHASRImm(SmallVectorImpl<MCParsedAsmOperand*> &O) {
157 return parsePKHImm(O, "asr", 1, 32);
158 }
Jim Grosbachc27d4f92011-07-22 17:44:50 +0000159 OperandMatchResultTy parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach580f4a92011-07-25 22:20:28 +0000160 OperandMatchResultTy parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach7e1547e2011-07-27 20:15:40 +0000161 OperandMatchResultTy parseRotImm(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach293a2ee2011-07-28 21:34:26 +0000162 OperandMatchResultTy parseBitfield(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach7ce05792011-08-03 23:50:40 +0000163 OperandMatchResultTy parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach251bf252011-08-10 21:56:18 +0000164 OperandMatchResultTy parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach9d390362011-10-03 23:38:36 +0000165 OperandMatchResultTy parseFPImm(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach862019c2011-10-18 23:02:30 +0000166 OperandMatchResultTy parseVectorList(SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbach7636bf62011-12-02 00:35:16 +0000167 OperandMatchResultTy parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000168
169 // Asm Match Converter Methods
Jim Grosbacha77295d2011-09-08 22:07:06 +0000170 bool cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
171 const SmallVectorImpl<MCParsedAsmOperand*> &);
172 bool cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
173 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbacheeec0252011-09-08 00:39:19 +0000174 bool cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
175 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbachee2c2a42011-09-16 21:55:56 +0000176 bool cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
177 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach1355cf12011-07-26 17:10:22 +0000178 bool cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000179 const SmallVectorImpl<MCParsedAsmOperand*> &);
Owen Anderson9ab0f252011-08-26 20:43:14 +0000180 bool cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
181 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach548340c2011-08-11 19:22:40 +0000182 bool cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
183 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach1355cf12011-07-26 17:10:22 +0000184 bool cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000185 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach7b8f46c2011-08-11 21:17:22 +0000186 bool cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
187 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach7ce05792011-08-03 23:50:40 +0000188 bool cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
189 const SmallVectorImpl<MCParsedAsmOperand*> &);
190 bool cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
191 const SmallVectorImpl<MCParsedAsmOperand*> &);
192 bool cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
193 const SmallVectorImpl<MCParsedAsmOperand*> &);
194 bool cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
195 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000196 bool cvtLdrdPre(MCInst &Inst, unsigned Opcode,
197 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach14605d12011-08-11 20:28:23 +0000198 bool cvtStrdPre(MCInst &Inst, unsigned Opcode,
199 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach623a4542011-08-10 22:42:16 +0000200 bool cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
201 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach88ae2bc2011-08-19 22:07:46 +0000202 bool cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
203 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach12431322011-10-24 22:16:58 +0000204 bool cvtVLDwbFixed(MCInst &Inst, unsigned Opcode,
205 const SmallVectorImpl<MCParsedAsmOperand*> &);
206 bool cvtVLDwbRegister(MCInst &Inst, unsigned Opcode,
207 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach4334e032011-10-31 21:50:31 +0000208 bool cvtVSTwbFixed(MCInst &Inst, unsigned Opcode,
209 const SmallVectorImpl<MCParsedAsmOperand*> &);
210 bool cvtVSTwbRegister(MCInst &Inst, unsigned Opcode,
211 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach189610f2011-07-26 18:25:39 +0000212
213 bool validateInstruction(MCInst &Inst,
214 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
Jim Grosbach83ec8772011-11-10 23:42:14 +0000215 bool processInstruction(MCInst &Inst,
Jim Grosbachf8fce712011-08-11 17:35:48 +0000216 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
Jim Grosbachd54b4e62011-08-16 21:12:37 +0000217 bool shouldOmitCCOutOperand(StringRef Mnemonic,
218 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Jim Grosbach189610f2011-07-26 18:25:39 +0000219
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000220public:
Jim Grosbach47a0d522011-08-16 20:45:50 +0000221 enum ARMMatchResultTy {
Jim Grosbach194bd892011-08-16 22:20:01 +0000222 Match_RequiresITBlock = FIRST_TARGET_MATCH_RESULT_TY,
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +0000223 Match_RequiresNotITBlock,
Jim Grosbach194bd892011-08-16 22:20:01 +0000224 Match_RequiresV6,
225 Match_RequiresThumb2
Jim Grosbach47a0d522011-08-16 20:45:50 +0000226 };
227
Evan Chengffc0e732011-07-09 05:47:46 +0000228 ARMAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
Evan Cheng94b95502011-07-26 00:24:13 +0000229 : MCTargetAsmParser(), STI(_STI), Parser(_Parser) {
Evan Chengebdeeab2011-07-08 01:53:10 +0000230 MCAsmParserExtension::Initialize(_Parser);
Evan Cheng32869202011-07-08 22:36:29 +0000231
Evan Chengebdeeab2011-07-08 01:53:10 +0000232 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000233 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +0000234
235 // Not in an ITBlock to start with.
236 ITState.CurPosition = ~0U;
Evan Chengebdeeab2011-07-08 01:53:10 +0000237 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000238
Jim Grosbach1355cf12011-07-26 17:10:22 +0000239 // Implementation of the MCTargetAsmParser interface:
240 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
241 bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Jim Grosbach189610f2011-07-26 18:25:39 +0000242 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Jim Grosbach1355cf12011-07-26 17:10:22 +0000243 bool ParseDirective(AsmToken DirectiveID);
244
Jim Grosbach47a0d522011-08-16 20:45:50 +0000245 unsigned checkTargetMatchPredicate(MCInst &Inst);
246
Jim Grosbach1355cf12011-07-26 17:10:22 +0000247 bool MatchAndEmitInstruction(SMLoc IDLoc,
248 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
249 MCStreamer &Out);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000250};
Jim Grosbach16c74252010-10-29 14:46:02 +0000251} // end anonymous namespace
252
Chris Lattner3a697562010-10-28 17:20:03 +0000253namespace {
254
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000255/// ARMOperand - Instances of this class represent a parsed ARM machine
256/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000257class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000258 enum KindTy {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000259 k_CondCode,
260 k_CCOut,
261 k_ITCondMask,
262 k_CoprocNum,
263 k_CoprocReg,
Jim Grosbach9b8f2a02011-10-12 17:34:41 +0000264 k_CoprocOption,
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000265 k_Immediate,
266 k_FPImmediate,
267 k_MemBarrierOpt,
268 k_Memory,
269 k_PostIndexRegister,
270 k_MSRMask,
271 k_ProcIFlags,
Jim Grosbach460a9052011-10-07 23:56:00 +0000272 k_VectorIndex,
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000273 k_Register,
274 k_RegisterList,
275 k_DPRRegisterList,
276 k_SPRRegisterList,
Jim Grosbach862019c2011-10-18 23:02:30 +0000277 k_VectorList,
Jim Grosbach98b05a52011-11-30 01:09:44 +0000278 k_VectorListAllLanes,
Jim Grosbach7636bf62011-12-02 00:35:16 +0000279 k_VectorListIndexed,
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000280 k_ShiftedRegister,
281 k_ShiftedImmediate,
282 k_ShifterImmediate,
283 k_RotateImmediate,
284 k_BitfieldDescriptor,
285 k_Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000286 } Kind;
287
Sean Callanan76264762010-04-02 22:27:05 +0000288 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000289 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000290
291 union {
292 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000293 ARMCC::CondCodes Val;
294 } CC;
295
296 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000297 unsigned Val;
298 } Cop;
299
300 struct {
Jim Grosbach9b8f2a02011-10-12 17:34:41 +0000301 unsigned Val;
302 } CoprocOption;
303
304 struct {
Jim Grosbach89df9962011-08-26 21:43:41 +0000305 unsigned Mask:4;
306 } ITMask;
307
308 struct {
309 ARM_MB::MemBOpt Val;
310 } MBOpt;
311
312 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000313 ARM_PROC::IFlags Val;
314 } IFlags;
315
316 struct {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000317 unsigned Val;
318 } MMask;
319
320 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000321 const char *Data;
322 unsigned Length;
323 } Tok;
324
325 struct {
326 unsigned RegNum;
327 } Reg;
328
Jim Grosbach862019c2011-10-18 23:02:30 +0000329 // A vector register list is a sequential list of 1 to 4 registers.
330 struct {
331 unsigned RegNum;
332 unsigned Count;
Jim Grosbach7636bf62011-12-02 00:35:16 +0000333 unsigned LaneIndex;
Jim Grosbach862019c2011-10-18 23:02:30 +0000334 } VectorList;
335
Bill Wendling8155e5b2010-11-06 22:19:43 +0000336 struct {
Jim Grosbach460a9052011-10-07 23:56:00 +0000337 unsigned Val;
338 } VectorIndex;
339
340 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000341 const MCExpr *Val;
342 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000343
Jim Grosbach9d390362011-10-03 23:38:36 +0000344 struct {
345 unsigned Val; // encoded 8-bit representation
346 } FPImm;
347
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000348 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000349 struct {
350 unsigned BaseRegNum;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000351 // Offset is in OffsetReg or OffsetImm. If both are zero, no offset
352 // was specified.
353 const MCConstantExpr *OffsetImm; // Offset immediate value
354 unsigned OffsetRegNum; // Offset register num, when OffsetImm == NULL
355 ARM_AM::ShiftOpc ShiftType; // Shift type for OffsetReg
Jim Grosbach57dcb852011-10-11 17:29:55 +0000356 unsigned ShiftImm; // shift for OffsetReg.
357 unsigned Alignment; // 0 = no alignment specified
358 // n = alignment in bytes (8, 16, or 32)
Jim Grosbach7ce05792011-08-03 23:50:40 +0000359 unsigned isNegative : 1; // Negated OffsetReg? (~'U' bit)
Jim Grosbache53c87b2011-10-11 15:59:20 +0000360 } Memory;
Owen Anderson00828302011-03-18 22:50:18 +0000361
362 struct {
Jim Grosbach7ce05792011-08-03 23:50:40 +0000363 unsigned RegNum;
Jim Grosbachf4fa3d62011-08-05 21:28:30 +0000364 bool isAdd;
365 ARM_AM::ShiftOpc ShiftTy;
366 unsigned ShiftImm;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000367 } PostIdxReg;
368
369 struct {
Jim Grosbach580f4a92011-07-25 22:20:28 +0000370 bool isASR;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000371 unsigned Imm;
Jim Grosbach580f4a92011-07-25 22:20:28 +0000372 } ShifterImm;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000373 struct {
374 ARM_AM::ShiftOpc ShiftTy;
375 unsigned SrcReg;
376 unsigned ShiftReg;
377 unsigned ShiftImm;
Jim Grosbachaf6981f2011-07-25 20:49:51 +0000378 } RegShiftedReg;
Owen Anderson92a20222011-07-21 18:54:16 +0000379 struct {
380 ARM_AM::ShiftOpc ShiftTy;
381 unsigned SrcReg;
382 unsigned ShiftImm;
Jim Grosbachaf6981f2011-07-25 20:49:51 +0000383 } RegShiftedImm;
Jim Grosbach7e1547e2011-07-27 20:15:40 +0000384 struct {
385 unsigned Imm;
386 } RotImm;
Jim Grosbach293a2ee2011-07-28 21:34:26 +0000387 struct {
388 unsigned LSB;
389 unsigned Width;
390 } Bitfield;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000391 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000392
Bill Wendling146018f2010-11-06 21:42:12 +0000393 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
394public:
Sean Callanan76264762010-04-02 22:27:05 +0000395 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
396 Kind = o.Kind;
397 StartLoc = o.StartLoc;
398 EndLoc = o.EndLoc;
399 switch (Kind) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000400 case k_CondCode:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000401 CC = o.CC;
402 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000403 case k_ITCondMask:
Jim Grosbach89df9962011-08-26 21:43:41 +0000404 ITMask = o.ITMask;
405 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000406 case k_Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000407 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000408 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000409 case k_CCOut:
410 case k_Register:
Sean Callanan76264762010-04-02 22:27:05 +0000411 Reg = o.Reg;
412 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000413 case k_RegisterList:
414 case k_DPRRegisterList:
415 case k_SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000416 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000417 break;
Jim Grosbach862019c2011-10-18 23:02:30 +0000418 case k_VectorList:
Jim Grosbach98b05a52011-11-30 01:09:44 +0000419 case k_VectorListAllLanes:
Jim Grosbach7636bf62011-12-02 00:35:16 +0000420 case k_VectorListIndexed:
Jim Grosbach862019c2011-10-18 23:02:30 +0000421 VectorList = o.VectorList;
422 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000423 case k_CoprocNum:
424 case k_CoprocReg:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000425 Cop = o.Cop;
426 break;
Jim Grosbach9b8f2a02011-10-12 17:34:41 +0000427 case k_CoprocOption:
428 CoprocOption = o.CoprocOption;
429 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000430 case k_Immediate:
Sean Callanan76264762010-04-02 22:27:05 +0000431 Imm = o.Imm;
432 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000433 case k_FPImmediate:
Jim Grosbach9d390362011-10-03 23:38:36 +0000434 FPImm = o.FPImm;
435 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000436 case k_MemBarrierOpt:
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000437 MBOpt = o.MBOpt;
438 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000439 case k_Memory:
Jim Grosbache53c87b2011-10-11 15:59:20 +0000440 Memory = o.Memory;
Sean Callanan76264762010-04-02 22:27:05 +0000441 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000442 case k_PostIndexRegister:
Jim Grosbach7ce05792011-08-03 23:50:40 +0000443 PostIdxReg = o.PostIdxReg;
444 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000445 case k_MSRMask:
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000446 MMask = o.MMask;
447 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000448 case k_ProcIFlags:
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000449 IFlags = o.IFlags;
Owen Anderson00828302011-03-18 22:50:18 +0000450 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000451 case k_ShifterImmediate:
Jim Grosbach580f4a92011-07-25 22:20:28 +0000452 ShifterImm = o.ShifterImm;
Owen Anderson00828302011-03-18 22:50:18 +0000453 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000454 case k_ShiftedRegister:
Jim Grosbachaf6981f2011-07-25 20:49:51 +0000455 RegShiftedReg = o.RegShiftedReg;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000456 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000457 case k_ShiftedImmediate:
Jim Grosbachaf6981f2011-07-25 20:49:51 +0000458 RegShiftedImm = o.RegShiftedImm;
Owen Anderson92a20222011-07-21 18:54:16 +0000459 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000460 case k_RotateImmediate:
Jim Grosbach7e1547e2011-07-27 20:15:40 +0000461 RotImm = o.RotImm;
462 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000463 case k_BitfieldDescriptor:
Jim Grosbach293a2ee2011-07-28 21:34:26 +0000464 Bitfield = o.Bitfield;
465 break;
Jim Grosbach460a9052011-10-07 23:56:00 +0000466 case k_VectorIndex:
467 VectorIndex = o.VectorIndex;
468 break;
Sean Callanan76264762010-04-02 22:27:05 +0000469 }
470 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000471
Sean Callanan76264762010-04-02 22:27:05 +0000472 /// getStartLoc - Get the location of the first token of this operand.
473 SMLoc getStartLoc() const { return StartLoc; }
474 /// getEndLoc - Get the location of the last token of this operand.
475 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000476
Daniel Dunbar8462b302010-08-11 06:36:53 +0000477 ARMCC::CondCodes getCondCode() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000478 assert(Kind == k_CondCode && "Invalid access!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000479 return CC.Val;
480 }
481
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000482 unsigned getCoproc() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000483 assert((Kind == k_CoprocNum || Kind == k_CoprocReg) && "Invalid access!");
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000484 return Cop.Val;
485 }
486
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000487 StringRef getToken() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000488 assert(Kind == k_Token && "Invalid access!");
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000489 return StringRef(Tok.Data, Tok.Length);
490 }
491
492 unsigned getReg() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000493 assert((Kind == k_Register || Kind == k_CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000494 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000495 }
496
Bill Wendling5fa22a12010-11-09 23:28:44 +0000497 const SmallVectorImpl<unsigned> &getRegList() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000498 assert((Kind == k_RegisterList || Kind == k_DPRRegisterList ||
499 Kind == k_SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000500 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000501 }
502
Kevin Enderbycfe07242009-10-13 22:19:02 +0000503 const MCExpr *getImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000504 assert(Kind == k_Immediate && "Invalid access!");
Kevin Enderbycfe07242009-10-13 22:19:02 +0000505 return Imm.Val;
506 }
507
Jim Grosbach9d390362011-10-03 23:38:36 +0000508 unsigned getFPImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000509 assert(Kind == k_FPImmediate && "Invalid access!");
Jim Grosbach9d390362011-10-03 23:38:36 +0000510 return FPImm.Val;
511 }
512
Jim Grosbach460a9052011-10-07 23:56:00 +0000513 unsigned getVectorIndex() const {
514 assert(Kind == k_VectorIndex && "Invalid access!");
515 return VectorIndex.Val;
516 }
517
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000518 ARM_MB::MemBOpt getMemBarrierOpt() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000519 assert(Kind == k_MemBarrierOpt && "Invalid access!");
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000520 return MBOpt.Val;
521 }
522
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000523 ARM_PROC::IFlags getProcIFlags() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000524 assert(Kind == k_ProcIFlags && "Invalid access!");
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000525 return IFlags.Val;
526 }
527
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000528 unsigned getMSRMask() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000529 assert(Kind == k_MSRMask && "Invalid access!");
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000530 return MMask.Val;
531 }
532
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000533 bool isCoprocNum() const { return Kind == k_CoprocNum; }
534 bool isCoprocReg() const { return Kind == k_CoprocReg; }
Jim Grosbach9b8f2a02011-10-12 17:34:41 +0000535 bool isCoprocOption() const { return Kind == k_CoprocOption; }
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000536 bool isCondCode() const { return Kind == k_CondCode; }
537 bool isCCOut() const { return Kind == k_CCOut; }
538 bool isITMask() const { return Kind == k_ITCondMask; }
539 bool isITCondCode() const { return Kind == k_CondCode; }
540 bool isImm() const { return Kind == k_Immediate; }
541 bool isFPImm() const { return Kind == k_FPImmediate; }
Jim Grosbacha77295d2011-09-08 22:07:06 +0000542 bool isImm8s4() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000543 if (Kind != k_Immediate)
Jim Grosbacha77295d2011-09-08 22:07:06 +0000544 return false;
545 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
546 if (!CE) return false;
547 int64_t Value = CE->getValue();
548 return ((Value & 3) == 0) && Value >= -1020 && Value <= 1020;
549 }
Jim Grosbach72f39f82011-08-24 21:22:15 +0000550 bool isImm0_1020s4() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000551 if (Kind != k_Immediate)
Jim Grosbach72f39f82011-08-24 21:22:15 +0000552 return false;
553 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
554 if (!CE) return false;
555 int64_t Value = CE->getValue();
556 return ((Value & 3) == 0) && Value >= 0 && Value <= 1020;
557 }
558 bool isImm0_508s4() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000559 if (Kind != k_Immediate)
Jim Grosbach72f39f82011-08-24 21:22:15 +0000560 return false;
561 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
562 if (!CE) return false;
563 int64_t Value = CE->getValue();
564 return ((Value & 3) == 0) && Value >= 0 && Value <= 508;
565 }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000566 bool isImm0_255() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000567 if (Kind != k_Immediate)
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000568 return false;
569 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
570 if (!CE) return false;
571 int64_t Value = CE->getValue();
572 return Value >= 0 && Value < 256;
573 }
Jim Grosbach587f5062011-12-02 23:34:39 +0000574 bool isImm0_1() const {
575 if (Kind != k_Immediate)
576 return false;
577 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
578 if (!CE) return false;
579 int64_t Value = CE->getValue();
580 return Value >= 0 && Value < 2;
581 }
582 bool isImm0_3() const {
583 if (Kind != k_Immediate)
584 return false;
585 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
586 if (!CE) return false;
587 int64_t Value = CE->getValue();
588 return Value >= 0 && Value < 4;
589 }
Jim Grosbach83ab0702011-07-13 22:01:08 +0000590 bool isImm0_7() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000591 if (Kind != k_Immediate)
Jim Grosbach83ab0702011-07-13 22:01:08 +0000592 return false;
593 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
594 if (!CE) return false;
595 int64_t Value = CE->getValue();
596 return Value >= 0 && Value < 8;
597 }
598 bool isImm0_15() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000599 if (Kind != k_Immediate)
Jim Grosbach83ab0702011-07-13 22:01:08 +0000600 return false;
601 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
602 if (!CE) return false;
603 int64_t Value = CE->getValue();
604 return Value >= 0 && Value < 16;
605 }
Jim Grosbach7c6e42e2011-07-21 23:26:25 +0000606 bool isImm0_31() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000607 if (Kind != k_Immediate)
Jim Grosbach7c6e42e2011-07-21 23:26:25 +0000608 return false;
609 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
610 if (!CE) return false;
611 int64_t Value = CE->getValue();
612 return Value >= 0 && Value < 32;
613 }
Jim Grosbach730fe6c2011-12-08 01:30:04 +0000614 bool isImm0_63() const {
615 if (Kind != k_Immediate)
616 return false;
617 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
618 if (!CE) return false;
619 int64_t Value = CE->getValue();
620 return Value >= 0 && Value < 64;
621 }
Jim Grosbach3b8991c2011-12-07 01:07:24 +0000622 bool isImm8() const {
623 if (Kind != k_Immediate)
624 return false;
625 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
626 if (!CE) return false;
627 int64_t Value = CE->getValue();
628 return Value == 8;
629 }
630 bool isImm16() const {
631 if (Kind != k_Immediate)
632 return false;
633 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
634 if (!CE) return false;
635 int64_t Value = CE->getValue();
636 return Value == 16;
637 }
638 bool isImm32() const {
639 if (Kind != k_Immediate)
640 return false;
641 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
642 if (!CE) return false;
643 int64_t Value = CE->getValue();
644 return Value == 32;
645 }
Jim Grosbach6b044c22011-12-08 22:06:06 +0000646 bool isShrImm8() const {
647 if (Kind != k_Immediate)
648 return false;
649 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
650 if (!CE) return false;
651 int64_t Value = CE->getValue();
652 return Value > 0 && Value <= 8;
653 }
654 bool isShrImm16() const {
655 if (Kind != k_Immediate)
656 return false;
657 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
658 if (!CE) return false;
659 int64_t Value = CE->getValue();
660 return Value > 0 && Value <= 16;
661 }
662 bool isShrImm32() const {
663 if (Kind != k_Immediate)
664 return false;
665 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
666 if (!CE) return false;
667 int64_t Value = CE->getValue();
668 return Value > 0 && Value <= 32;
669 }
670 bool isShrImm64() const {
671 if (Kind != k_Immediate)
672 return false;
673 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
674 if (!CE) return false;
675 int64_t Value = CE->getValue();
676 return Value > 0 && Value <= 64;
677 }
Jim Grosbach3b8991c2011-12-07 01:07:24 +0000678 bool isImm1_7() const {
679 if (Kind != k_Immediate)
680 return false;
681 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
682 if (!CE) return false;
683 int64_t Value = CE->getValue();
684 return Value > 0 && Value < 8;
685 }
686 bool isImm1_15() const {
687 if (Kind != k_Immediate)
688 return false;
689 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
690 if (!CE) return false;
691 int64_t Value = CE->getValue();
692 return Value > 0 && Value < 16;
693 }
694 bool isImm1_31() const {
695 if (Kind != k_Immediate)
696 return false;
697 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
698 if (!CE) return false;
699 int64_t Value = CE->getValue();
700 return Value > 0 && Value < 32;
701 }
Jim Grosbachf4943352011-07-25 23:09:14 +0000702 bool isImm1_16() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000703 if (Kind != k_Immediate)
Jim Grosbachf4943352011-07-25 23:09:14 +0000704 return false;
705 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
706 if (!CE) return false;
707 int64_t Value = CE->getValue();
708 return Value > 0 && Value < 17;
709 }
Jim Grosbach4a5ffb32011-07-22 23:16:18 +0000710 bool isImm1_32() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000711 if (Kind != k_Immediate)
Jim Grosbach4a5ffb32011-07-22 23:16:18 +0000712 return false;
713 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
714 if (!CE) return false;
715 int64_t Value = CE->getValue();
716 return Value > 0 && Value < 33;
717 }
Jim Grosbachee10ff82011-11-10 19:18:01 +0000718 bool isImm0_32() const {
719 if (Kind != k_Immediate)
720 return false;
721 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
722 if (!CE) return false;
723 int64_t Value = CE->getValue();
724 return Value >= 0 && Value < 33;
725 }
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000726 bool isImm0_65535() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000727 if (Kind != k_Immediate)
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000728 return false;
729 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
730 if (!CE) return false;
731 int64_t Value = CE->getValue();
732 return Value >= 0 && Value < 65536;
733 }
Jim Grosbachffa32252011-07-19 19:13:28 +0000734 bool isImm0_65535Expr() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000735 if (Kind != k_Immediate)
Jim Grosbachffa32252011-07-19 19:13:28 +0000736 return false;
737 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
738 // If it's not a constant expression, it'll generate a fixup and be
739 // handled later.
740 if (!CE) return true;
741 int64_t Value = CE->getValue();
742 return Value >= 0 && Value < 65536;
743 }
Jim Grosbached838482011-07-26 16:24:27 +0000744 bool isImm24bit() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000745 if (Kind != k_Immediate)
Jim Grosbached838482011-07-26 16:24:27 +0000746 return false;
747 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
748 if (!CE) return false;
749 int64_t Value = CE->getValue();
750 return Value >= 0 && Value <= 0xffffff;
751 }
Jim Grosbach70939ee2011-08-17 21:51:27 +0000752 bool isImmThumbSR() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000753 if (Kind != k_Immediate)
Jim Grosbach70939ee2011-08-17 21:51:27 +0000754 return false;
755 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
756 if (!CE) return false;
757 int64_t Value = CE->getValue();
758 return Value > 0 && Value < 33;
759 }
Jim Grosbachf6c05252011-07-21 17:23:04 +0000760 bool isPKHLSLImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000761 if (Kind != k_Immediate)
Jim Grosbachf6c05252011-07-21 17:23:04 +0000762 return false;
763 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
764 if (!CE) return false;
765 int64_t Value = CE->getValue();
766 return Value >= 0 && Value < 32;
767 }
768 bool isPKHASRImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000769 if (Kind != k_Immediate)
Jim Grosbachf6c05252011-07-21 17:23:04 +0000770 return false;
771 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
772 if (!CE) return false;
773 int64_t Value = CE->getValue();
774 return Value > 0 && Value <= 32;
775 }
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000776 bool isARMSOImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000777 if (Kind != k_Immediate)
Jim Grosbach6bc1dbc2011-07-19 16:50:30 +0000778 return false;
779 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
780 if (!CE) return false;
781 int64_t Value = CE->getValue();
782 return ARM_AM::getSOImmVal(Value) != -1;
783 }
Jim Grosbache70ec842011-10-28 22:50:54 +0000784 bool isARMSOImmNot() const {
785 if (Kind != k_Immediate)
786 return false;
787 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
788 if (!CE) return false;
789 int64_t Value = CE->getValue();
790 return ARM_AM::getSOImmVal(~Value) != -1;
791 }
Jim Grosbach3bc8a3d2011-12-08 00:31:07 +0000792 bool isARMSOImmNeg() const {
793 if (Kind != k_Immediate)
794 return false;
795 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
796 if (!CE) return false;
797 int64_t Value = CE->getValue();
798 return ARM_AM::getSOImmVal(-Value) != -1;
799 }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000800 bool isT2SOImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000801 if (Kind != k_Immediate)
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000802 return false;
803 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
804 if (!CE) return false;
805 int64_t Value = CE->getValue();
806 return ARM_AM::getT2SOImmVal(Value) != -1;
807 }
Jim Grosbach89a63372011-10-28 22:36:30 +0000808 bool isT2SOImmNot() const {
809 if (Kind != k_Immediate)
810 return false;
811 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
812 if (!CE) return false;
813 int64_t Value = CE->getValue();
814 return ARM_AM::getT2SOImmVal(~Value) != -1;
815 }
Jim Grosbach3bc8a3d2011-12-08 00:31:07 +0000816 bool isT2SOImmNeg() const {
817 if (Kind != k_Immediate)
818 return false;
819 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
820 if (!CE) return false;
821 int64_t Value = CE->getValue();
822 return ARM_AM::getT2SOImmVal(-Value) != -1;
823 }
Jim Grosbachc27d4f92011-07-22 17:44:50 +0000824 bool isSetEndImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000825 if (Kind != k_Immediate)
Jim Grosbachc27d4f92011-07-22 17:44:50 +0000826 return false;
827 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
828 if (!CE) return false;
829 int64_t Value = CE->getValue();
830 return Value == 1 || Value == 0;
831 }
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000832 bool isReg() const { return Kind == k_Register; }
833 bool isRegList() const { return Kind == k_RegisterList; }
834 bool isDPRRegList() const { return Kind == k_DPRRegisterList; }
835 bool isSPRRegList() const { return Kind == k_SPRRegisterList; }
836 bool isToken() const { return Kind == k_Token; }
837 bool isMemBarrierOpt() const { return Kind == k_MemBarrierOpt; }
838 bool isMemory() const { return Kind == k_Memory; }
839 bool isShifterImm() const { return Kind == k_ShifterImmediate; }
840 bool isRegShiftedReg() const { return Kind == k_ShiftedRegister; }
841 bool isRegShiftedImm() const { return Kind == k_ShiftedImmediate; }
842 bool isRotImm() const { return Kind == k_RotateImmediate; }
843 bool isBitfield() const { return Kind == k_BitfieldDescriptor; }
844 bool isPostIdxRegShifted() const { return Kind == k_PostIndexRegister; }
Jim Grosbachf4fa3d62011-08-05 21:28:30 +0000845 bool isPostIdxReg() const {
Jim Grosbach430052b2011-11-14 17:52:47 +0000846 return Kind == k_PostIndexRegister && PostIdxReg.ShiftTy ==ARM_AM::no_shift;
Jim Grosbachf4fa3d62011-08-05 21:28:30 +0000847 }
Jim Grosbach57dcb852011-10-11 17:29:55 +0000848 bool isMemNoOffset(bool alignOK = false) const {
Jim Grosbachf6c35c52011-10-10 23:06:42 +0000849 if (!isMemory())
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000850 return false;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000851 // No offset of any kind.
Jim Grosbach57dcb852011-10-11 17:29:55 +0000852 return Memory.OffsetRegNum == 0 && Memory.OffsetImm == 0 &&
853 (alignOK || Memory.Alignment == 0);
854 }
855 bool isAlignedMemory() const {
856 return isMemNoOffset(true);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000857 }
Jim Grosbach7ce05792011-08-03 23:50:40 +0000858 bool isAddrMode2() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000859 if (!isMemory() || Memory.Alignment != 0) return false;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000860 // Check for register offset.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000861 if (Memory.OffsetRegNum) return true;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000862 // Immediate offset in range [-4095, 4095].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000863 if (!Memory.OffsetImm) return true;
864 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbach7ce05792011-08-03 23:50:40 +0000865 return Val > -4096 && Val < 4096;
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000866 }
Jim Grosbach039c2e12011-08-04 23:01:30 +0000867 bool isAM2OffsetImm() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000868 if (Kind != k_Immediate)
Jim Grosbach039c2e12011-08-04 23:01:30 +0000869 return false;
870 // Immediate offset in range [-4095, 4095].
871 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
872 if (!CE) return false;
873 int64_t Val = CE->getValue();
874 return Val > -4096 && Val < 4096;
875 }
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000876 bool isAddrMode3() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000877 if (!isMemory() || Memory.Alignment != 0) return false;
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000878 // No shifts are legal for AM3.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000879 if (Memory.ShiftType != ARM_AM::no_shift) return false;
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000880 // Check for register offset.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000881 if (Memory.OffsetRegNum) return true;
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000882 // Immediate offset in range [-255, 255].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000883 if (!Memory.OffsetImm) return true;
884 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000885 return Val > -256 && Val < 256;
886 }
887 bool isAM3Offset() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000888 if (Kind != k_Immediate && Kind != k_PostIndexRegister)
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000889 return false;
Jim Grosbach21ff17c2011-10-07 23:24:09 +0000890 if (Kind == k_PostIndexRegister)
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000891 return PostIdxReg.ShiftTy == ARM_AM::no_shift;
892 // Immediate offset in range [-255, 255].
893 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
894 if (!CE) return false;
895 int64_t Val = CE->getValue();
Jim Grosbach251bf252011-08-10 21:56:18 +0000896 // Special case, #-0 is INT32_MIN.
897 return (Val > -256 && Val < 256) || Val == INT32_MIN;
Jim Grosbach2fd2b872011-08-10 20:29:19 +0000898 }
Jim Grosbach7ce05792011-08-03 23:50:40 +0000899 bool isAddrMode5() const {
Jim Grosbach681460f2011-11-01 01:24:45 +0000900 // If we have an immediate that's not a constant, treat it as a label
901 // reference needing a fixup. If it is a constant, it's something else
902 // and we reject it.
903 if (Kind == k_Immediate && !isa<MCConstantExpr>(getImm()))
904 return true;
Jim Grosbach57dcb852011-10-11 17:29:55 +0000905 if (!isMemory() || Memory.Alignment != 0) return false;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000906 // Check for register offset.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000907 if (Memory.OffsetRegNum) return false;
Jim Grosbach7ce05792011-08-03 23:50:40 +0000908 // Immediate offset in range [-1020, 1020] and a multiple of 4.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000909 if (!Memory.OffsetImm) return true;
910 int64_t Val = Memory.OffsetImm->getValue();
Owen Anderson0da10cf2011-08-29 19:36:44 +0000911 return (Val >= -1020 && Val <= 1020 && ((Val & 3) == 0)) ||
Jim Grosbach681460f2011-11-01 01:24:45 +0000912 Val == INT32_MIN;
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000913 }
Jim Grosbach7f739be2011-09-19 22:21:13 +0000914 bool isMemTBB() const {
Jim Grosbache53c87b2011-10-11 15:59:20 +0000915 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
Jim Grosbach57dcb852011-10-11 17:29:55 +0000916 Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
Jim Grosbach7f739be2011-09-19 22:21:13 +0000917 return false;
918 return true;
919 }
920 bool isMemTBH() const {
Jim Grosbache53c87b2011-10-11 15:59:20 +0000921 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
Jim Grosbach57dcb852011-10-11 17:29:55 +0000922 Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm != 1 ||
923 Memory.Alignment != 0 )
Jim Grosbach7f739be2011-09-19 22:21:13 +0000924 return false;
925 return true;
926 }
Jim Grosbach7ce05792011-08-03 23:50:40 +0000927 bool isMemRegOffset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000928 if (!isMemory() || !Memory.OffsetRegNum || Memory.Alignment != 0)
Bill Wendlingf4caf692010-12-14 03:36:38 +0000929 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000930 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000931 }
Jim Grosbachab899c12011-09-07 23:10:15 +0000932 bool isT2MemRegOffset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000933 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
934 Memory.Alignment != 0)
Jim Grosbachab899c12011-09-07 23:10:15 +0000935 return false;
936 // Only lsl #{0, 1, 2, 3} allowed.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000937 if (Memory.ShiftType == ARM_AM::no_shift)
Jim Grosbachab899c12011-09-07 23:10:15 +0000938 return true;
Jim Grosbache53c87b2011-10-11 15:59:20 +0000939 if (Memory.ShiftType != ARM_AM::lsl || Memory.ShiftImm > 3)
Jim Grosbachab899c12011-09-07 23:10:15 +0000940 return false;
941 return true;
942 }
Jim Grosbach7ce05792011-08-03 23:50:40 +0000943 bool isMemThumbRR() const {
944 // Thumb reg+reg addressing is simple. Just two registers, a base and
945 // an offset. No shifts, negations or any other complicating factors.
Jim Grosbache53c87b2011-10-11 15:59:20 +0000946 if (!isMemory() || !Memory.OffsetRegNum || Memory.isNegative ||
Jim Grosbach57dcb852011-10-11 17:29:55 +0000947 Memory.ShiftType != ARM_AM::no_shift || Memory.Alignment != 0)
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000948 return false;
Jim Grosbache53c87b2011-10-11 15:59:20 +0000949 return isARMLowRegister(Memory.BaseRegNum) &&
950 (!Memory.OffsetRegNum || isARMLowRegister(Memory.OffsetRegNum));
Jim Grosbach60f91a32011-08-19 17:55:24 +0000951 }
952 bool isMemThumbRIs4() const {
Jim Grosbache53c87b2011-10-11 15:59:20 +0000953 if (!isMemory() || Memory.OffsetRegNum != 0 ||
Jim Grosbach57dcb852011-10-11 17:29:55 +0000954 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
Jim Grosbach60f91a32011-08-19 17:55:24 +0000955 return false;
956 // Immediate offset, multiple of 4 in range [0, 124].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000957 if (!Memory.OffsetImm) return true;
958 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbachecd85892011-08-19 18:13:48 +0000959 return Val >= 0 && Val <= 124 && (Val % 4) == 0;
960 }
Jim Grosbach38466302011-08-19 18:55:51 +0000961 bool isMemThumbRIs2() const {
Jim Grosbache53c87b2011-10-11 15:59:20 +0000962 if (!isMemory() || Memory.OffsetRegNum != 0 ||
Jim Grosbach57dcb852011-10-11 17:29:55 +0000963 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
Jim Grosbach38466302011-08-19 18:55:51 +0000964 return false;
965 // Immediate offset, multiple of 4 in range [0, 62].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000966 if (!Memory.OffsetImm) return true;
967 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbach38466302011-08-19 18:55:51 +0000968 return Val >= 0 && Val <= 62 && (Val % 2) == 0;
969 }
Jim Grosbach48ff5ff2011-08-19 18:49:59 +0000970 bool isMemThumbRIs1() const {
Jim Grosbache53c87b2011-10-11 15:59:20 +0000971 if (!isMemory() || Memory.OffsetRegNum != 0 ||
Jim Grosbach57dcb852011-10-11 17:29:55 +0000972 !isARMLowRegister(Memory.BaseRegNum) || Memory.Alignment != 0)
Jim Grosbach48ff5ff2011-08-19 18:49:59 +0000973 return false;
974 // Immediate offset in range [0, 31].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000975 if (!Memory.OffsetImm) return true;
976 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbach48ff5ff2011-08-19 18:49:59 +0000977 return Val >= 0 && Val <= 31;
978 }
Jim Grosbachecd85892011-08-19 18:13:48 +0000979 bool isMemThumbSPI() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000980 if (!isMemory() || Memory.OffsetRegNum != 0 ||
981 Memory.BaseRegNum != ARM::SP || Memory.Alignment != 0)
Jim Grosbachecd85892011-08-19 18:13:48 +0000982 return false;
983 // Immediate offset, multiple of 4 in range [0, 1020].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000984 if (!Memory.OffsetImm) return true;
985 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbachecd85892011-08-19 18:13:48 +0000986 return Val >= 0 && Val <= 1020 && (Val % 4) == 0;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000987 }
Jim Grosbacha77295d2011-09-08 22:07:06 +0000988 bool isMemImm8s4Offset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000989 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbacha77295d2011-09-08 22:07:06 +0000990 return false;
991 // Immediate offset a multiple of 4 in range [-1020, 1020].
Jim Grosbache53c87b2011-10-11 15:59:20 +0000992 if (!Memory.OffsetImm) return true;
993 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbacha77295d2011-09-08 22:07:06 +0000994 return Val >= -1020 && Val <= 1020 && (Val & 3) == 0;
995 }
Jim Grosbachb6aed502011-09-09 18:37:27 +0000996 bool isMemImm0_1020s4Offset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +0000997 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbachb6aed502011-09-09 18:37:27 +0000998 return false;
999 // Immediate offset a multiple of 4 in range [0, 1020].
Jim Grosbache53c87b2011-10-11 15:59:20 +00001000 if (!Memory.OffsetImm) return true;
1001 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbachb6aed502011-09-09 18:37:27 +00001002 return Val >= 0 && Val <= 1020 && (Val & 3) == 0;
1003 }
Jim Grosbach7ce05792011-08-03 23:50:40 +00001004 bool isMemImm8Offset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +00001005 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbach7ce05792011-08-03 23:50:40 +00001006 return false;
1007 // Immediate offset in range [-255, 255].
Jim Grosbache53c87b2011-10-11 15:59:20 +00001008 if (!Memory.OffsetImm) return true;
1009 int64_t Val = Memory.OffsetImm->getValue();
Owen Anderson4d2a0012011-09-23 22:25:02 +00001010 return (Val == INT32_MIN) || (Val > -256 && Val < 256);
Jim Grosbach7ce05792011-08-03 23:50:40 +00001011 }
Jim Grosbachf0eee6e2011-09-07 23:39:14 +00001012 bool isMemPosImm8Offset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +00001013 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbachf0eee6e2011-09-07 23:39:14 +00001014 return false;
1015 // Immediate offset in range [0, 255].
Jim Grosbache53c87b2011-10-11 15:59:20 +00001016 if (!Memory.OffsetImm) return true;
1017 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbachf0eee6e2011-09-07 23:39:14 +00001018 return Val >= 0 && Val < 256;
1019 }
Jim Grosbacha8307dd2011-09-07 20:58:57 +00001020 bool isMemNegImm8Offset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +00001021 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbacha8307dd2011-09-07 20:58:57 +00001022 return false;
1023 // Immediate offset in range [-255, -1].
Jim Grosbachdf33e0d2011-12-06 04:49:29 +00001024 if (!Memory.OffsetImm) return false;
Jim Grosbache53c87b2011-10-11 15:59:20 +00001025 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbachdf33e0d2011-12-06 04:49:29 +00001026 return (Val == INT32_MIN) || (Val > -256 && Val < 0);
Jim Grosbacha8307dd2011-09-07 20:58:57 +00001027 }
1028 bool isMemUImm12Offset() const {
Jim Grosbach57dcb852011-10-11 17:29:55 +00001029 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbacha8307dd2011-09-07 20:58:57 +00001030 return false;
1031 // Immediate offset in range [0, 4095].
Jim Grosbache53c87b2011-10-11 15:59:20 +00001032 if (!Memory.OffsetImm) return true;
1033 int64_t Val = Memory.OffsetImm->getValue();
Jim Grosbacha8307dd2011-09-07 20:58:57 +00001034 return (Val >= 0 && Val < 4096);
1035 }
Jim Grosbach7ce05792011-08-03 23:50:40 +00001036 bool isMemImm12Offset() const {
Jim Grosbach09176e12011-08-08 20:59:31 +00001037 // If we have an immediate that's not a constant, treat it as a label
1038 // reference needing a fixup. If it is a constant, it's something else
1039 // and we reject it.
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001040 if (Kind == k_Immediate && !isa<MCConstantExpr>(getImm()))
Jim Grosbach09176e12011-08-08 20:59:31 +00001041 return true;
1042
Jim Grosbach57dcb852011-10-11 17:29:55 +00001043 if (!isMemory() || Memory.OffsetRegNum != 0 || Memory.Alignment != 0)
Jim Grosbach7ce05792011-08-03 23:50:40 +00001044 return false;
1045 // Immediate offset in range [-4095, 4095].
Jim Grosbache53c87b2011-10-11 15:59:20 +00001046 if (!Memory.OffsetImm) return true;
1047 int64_t Val = Memory.OffsetImm->getValue();
Owen Anderson0da10cf2011-08-29 19:36:44 +00001048 return (Val > -4096 && Val < 4096) || (Val == INT32_MIN);
Jim Grosbach7ce05792011-08-03 23:50:40 +00001049 }
1050 bool isPostIdxImm8() const {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001051 if (Kind != k_Immediate)
Jim Grosbach7ce05792011-08-03 23:50:40 +00001052 return false;
1053 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1054 if (!CE) return false;
1055 int64_t Val = CE->getValue();
Owen Anderson63553c72011-08-29 17:17:09 +00001056 return (Val > -256 && Val < 256) || (Val == INT32_MIN);
Jim Grosbach7ce05792011-08-03 23:50:40 +00001057 }
Jim Grosbach2bd01182011-10-11 21:55:36 +00001058 bool isPostIdxImm8s4() const {
1059 if (Kind != k_Immediate)
1060 return false;
1061 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1062 if (!CE) return false;
1063 int64_t Val = CE->getValue();
1064 return ((Val & 3) == 0 && Val >= -1020 && Val <= 1020) ||
1065 (Val == INT32_MIN);
1066 }
Jim Grosbach7ce05792011-08-03 23:50:40 +00001067
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001068 bool isMSRMask() const { return Kind == k_MSRMask; }
1069 bool isProcIFlags() const { return Kind == k_ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001070
Jim Grosbach0e387b22011-10-17 22:26:03 +00001071 // NEON operands.
Jim Grosbach862019c2011-10-18 23:02:30 +00001072 bool isVecListOneD() const {
1073 if (Kind != k_VectorList) return false;
1074 return VectorList.Count == 1;
1075 }
1076
Jim Grosbach280dfad2011-10-21 18:54:25 +00001077 bool isVecListTwoD() const {
1078 if (Kind != k_VectorList) return false;
1079 return VectorList.Count == 2;
1080 }
1081
Jim Grosbachcdcfa282011-10-21 20:02:19 +00001082 bool isVecListThreeD() const {
1083 if (Kind != k_VectorList) return false;
1084 return VectorList.Count == 3;
1085 }
1086
Jim Grosbachb6310312011-10-21 20:35:01 +00001087 bool isVecListFourD() const {
1088 if (Kind != k_VectorList) return false;
1089 return VectorList.Count == 4;
1090 }
1091
Jim Grosbach4661d4c2011-10-21 22:21:10 +00001092 bool isVecListTwoQ() const {
1093 if (Kind != k_VectorList) return false;
1094 //FIXME: We haven't taught the parser to handle by-two register lists
1095 // yet, so don't pretend to know one.
1096 return VectorList.Count == 2 && false;
1097 }
1098
Jim Grosbach98b05a52011-11-30 01:09:44 +00001099 bool isVecListOneDAllLanes() const {
1100 if (Kind != k_VectorListAllLanes) return false;
1101 return VectorList.Count == 1;
1102 }
1103
Jim Grosbach13af2222011-11-30 18:21:25 +00001104 bool isVecListTwoDAllLanes() const {
1105 if (Kind != k_VectorListAllLanes) return false;
1106 return VectorList.Count == 2;
1107 }
1108
Jim Grosbach7636bf62011-12-02 00:35:16 +00001109 bool isVecListOneDByteIndexed() const {
1110 if (Kind != k_VectorListIndexed) return false;
1111 return VectorList.Count == 1 && VectorList.LaneIndex <= 7;
1112 }
1113
Jim Grosbach460a9052011-10-07 23:56:00 +00001114 bool isVectorIndex8() const {
1115 if (Kind != k_VectorIndex) return false;
1116 return VectorIndex.Val < 8;
1117 }
1118 bool isVectorIndex16() const {
1119 if (Kind != k_VectorIndex) return false;
1120 return VectorIndex.Val < 4;
1121 }
1122 bool isVectorIndex32() const {
1123 if (Kind != k_VectorIndex) return false;
1124 return VectorIndex.Val < 2;
1125 }
1126
Jim Grosbach0e387b22011-10-17 22:26:03 +00001127 bool isNEONi8splat() const {
1128 if (Kind != k_Immediate)
1129 return false;
1130 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1131 // Must be a constant.
1132 if (!CE) return false;
1133 int64_t Value = CE->getValue();
1134 // i8 value splatted across 8 bytes. The immediate is just the 8 byte
1135 // value.
Jim Grosbach0e387b22011-10-17 22:26:03 +00001136 return Value >= 0 && Value < 256;
1137 }
Jim Grosbach460a9052011-10-07 23:56:00 +00001138
Jim Grosbachea461102011-10-17 23:09:09 +00001139 bool isNEONi16splat() const {
1140 if (Kind != k_Immediate)
1141 return false;
1142 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1143 // Must be a constant.
1144 if (!CE) return false;
1145 int64_t Value = CE->getValue();
1146 // i16 value in the range [0,255] or [0x0100, 0xff00]
1147 return (Value >= 0 && Value < 256) || (Value >= 0x0100 && Value <= 0xff00);
1148 }
1149
Jim Grosbach6248a542011-10-18 00:22:00 +00001150 bool isNEONi32splat() const {
1151 if (Kind != k_Immediate)
1152 return false;
1153 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1154 // Must be a constant.
1155 if (!CE) return false;
1156 int64_t Value = CE->getValue();
1157 // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X.
1158 return (Value >= 0 && Value < 256) ||
1159 (Value >= 0x0100 && Value <= 0xff00) ||
1160 (Value >= 0x010000 && Value <= 0xff0000) ||
1161 (Value >= 0x01000000 && Value <= 0xff000000);
1162 }
1163
1164 bool isNEONi32vmov() const {
1165 if (Kind != k_Immediate)
1166 return false;
1167 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1168 // Must be a constant.
1169 if (!CE) return false;
1170 int64_t Value = CE->getValue();
1171 // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X,
1172 // for VMOV/VMVN only, 00Xf or 0Xff are also accepted.
1173 return (Value >= 0 && Value < 256) ||
1174 (Value >= 0x0100 && Value <= 0xff00) ||
1175 (Value >= 0x010000 && Value <= 0xff0000) ||
1176 (Value >= 0x01000000 && Value <= 0xff000000) ||
1177 (Value >= 0x01ff && Value <= 0xffff && (Value & 0xff) == 0xff) ||
1178 (Value >= 0x01ffff && Value <= 0xffffff && (Value & 0xffff) == 0xffff);
1179 }
1180
Jim Grosbachf2f5bc62011-10-18 16:18:11 +00001181 bool isNEONi64splat() const {
1182 if (Kind != k_Immediate)
1183 return false;
1184 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1185 // Must be a constant.
1186 if (!CE) return false;
1187 uint64_t Value = CE->getValue();
1188 // i64 value with each byte being either 0 or 0xff.
1189 for (unsigned i = 0; i < 8; ++i)
1190 if ((Value & 0xff) != 0 && (Value & 0xff) != 0xff) return false;
1191 return true;
1192 }
1193
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001194 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +00001195 // Add as immediates when possible. Null MCExpr = 0.
1196 if (Expr == 0)
1197 Inst.addOperand(MCOperand::CreateImm(0));
1198 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001199 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1200 else
1201 Inst.addOperand(MCOperand::CreateExpr(Expr));
1202 }
1203
Daniel Dunbar8462b302010-08-11 06:36:53 +00001204 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001205 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +00001206 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +00001207 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
1208 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +00001209 }
1210
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001211 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
1212 assert(N == 1 && "Invalid number of operands!");
1213 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
1214 }
1215
Jim Grosbach9b8f2a02011-10-12 17:34:41 +00001216 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
1217 assert(N == 1 && "Invalid number of operands!");
1218 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
1219 }
1220
1221 void addCoprocOptionOperands(MCInst &Inst, unsigned N) const {
1222 assert(N == 1 && "Invalid number of operands!");
1223 Inst.addOperand(MCOperand::CreateImm(CoprocOption.Val));
1224 }
1225
Jim Grosbach89df9962011-08-26 21:43:41 +00001226 void addITMaskOperands(MCInst &Inst, unsigned N) const {
1227 assert(N == 1 && "Invalid number of operands!");
1228 Inst.addOperand(MCOperand::CreateImm(ITMask.Mask));
1229 }
1230
1231 void addITCondCodeOperands(MCInst &Inst, unsigned N) const {
1232 assert(N == 1 && "Invalid number of operands!");
1233 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
1234 }
1235
Jim Grosbachd67641b2010-12-06 18:21:12 +00001236 void addCCOutOperands(MCInst &Inst, unsigned N) const {
1237 assert(N == 1 && "Invalid number of operands!");
1238 Inst.addOperand(MCOperand::CreateReg(getReg()));
1239 }
1240
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001241 void addRegOperands(MCInst &Inst, unsigned N) const {
1242 assert(N == 1 && "Invalid number of operands!");
1243 Inst.addOperand(MCOperand::CreateReg(getReg()));
1244 }
1245
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001246 void addRegShiftedRegOperands(MCInst &Inst, unsigned N) const {
Jim Grosbache8606dc2011-07-13 17:50:29 +00001247 assert(N == 3 && "Invalid number of operands!");
Jim Grosbach430052b2011-11-14 17:52:47 +00001248 assert(isRegShiftedReg() &&
1249 "addRegShiftedRegOperands() on non RegShiftedReg!");
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001250 Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.SrcReg));
1251 Inst.addOperand(MCOperand::CreateReg(RegShiftedReg.ShiftReg));
Jim Grosbache8606dc2011-07-13 17:50:29 +00001252 Inst.addOperand(MCOperand::CreateImm(
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001253 ARM_AM::getSORegOpc(RegShiftedReg.ShiftTy, RegShiftedReg.ShiftImm)));
Jim Grosbache8606dc2011-07-13 17:50:29 +00001254 }
1255
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001256 void addRegShiftedImmOperands(MCInst &Inst, unsigned N) const {
Owen Anderson152d4a42011-07-21 23:38:37 +00001257 assert(N == 2 && "Invalid number of operands!");
Jim Grosbach430052b2011-11-14 17:52:47 +00001258 assert(isRegShiftedImm() &&
1259 "addRegShiftedImmOperands() on non RegShiftedImm!");
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001260 Inst.addOperand(MCOperand::CreateReg(RegShiftedImm.SrcReg));
Owen Anderson92a20222011-07-21 18:54:16 +00001261 Inst.addOperand(MCOperand::CreateImm(
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001262 ARM_AM::getSORegOpc(RegShiftedImm.ShiftTy, RegShiftedImm.ShiftImm)));
Owen Anderson92a20222011-07-21 18:54:16 +00001263 }
1264
Jim Grosbach580f4a92011-07-25 22:20:28 +00001265 void addShifterImmOperands(MCInst &Inst, unsigned N) const {
Owen Anderson00828302011-03-18 22:50:18 +00001266 assert(N == 1 && "Invalid number of operands!");
Jim Grosbach580f4a92011-07-25 22:20:28 +00001267 Inst.addOperand(MCOperand::CreateImm((ShifterImm.isASR << 5) |
1268 ShifterImm.Imm));
Owen Anderson00828302011-03-18 22:50:18 +00001269 }
1270
Bill Wendling87f4f9a2010-11-08 23:49:57 +00001271 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +00001272 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +00001273 const SmallVectorImpl<unsigned> &RegList = getRegList();
1274 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +00001275 I = RegList.begin(), E = RegList.end(); I != E; ++I)
1276 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +00001277 }
1278
Bill Wendling0f630752010-11-17 04:32:08 +00001279 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
1280 addRegListOperands(Inst, N);
1281 }
1282
1283 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
1284 addRegListOperands(Inst, N);
1285 }
1286
Jim Grosbach7e1547e2011-07-27 20:15:40 +00001287 void addRotImmOperands(MCInst &Inst, unsigned N) const {
1288 assert(N == 1 && "Invalid number of operands!");
1289 // Encoded as val>>3. The printer handles display as 8, 16, 24.
1290 Inst.addOperand(MCOperand::CreateImm(RotImm.Imm >> 3));
1291 }
1292
Jim Grosbach293a2ee2011-07-28 21:34:26 +00001293 void addBitfieldOperands(MCInst &Inst, unsigned N) const {
1294 assert(N == 1 && "Invalid number of operands!");
1295 // Munge the lsb/width into a bitfield mask.
1296 unsigned lsb = Bitfield.LSB;
1297 unsigned width = Bitfield.Width;
1298 // Make a 32-bit mask w/ the referenced bits clear and all other bits set.
1299 uint32_t Mask = ~(((uint32_t)0xffffffff >> lsb) << (32 - width) >>
1300 (32 - (lsb + width)));
1301 Inst.addOperand(MCOperand::CreateImm(Mask));
1302 }
1303
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001304 void addImmOperands(MCInst &Inst, unsigned N) const {
1305 assert(N == 1 && "Invalid number of operands!");
1306 addExpr(Inst, getImm());
1307 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001308
Jim Grosbach9d390362011-10-03 23:38:36 +00001309 void addFPImmOperands(MCInst &Inst, unsigned N) const {
1310 assert(N == 1 && "Invalid number of operands!");
1311 Inst.addOperand(MCOperand::CreateImm(getFPImm()));
1312 }
1313
Jim Grosbacha77295d2011-09-08 22:07:06 +00001314 void addImm8s4Operands(MCInst &Inst, unsigned N) const {
1315 assert(N == 1 && "Invalid number of operands!");
1316 // FIXME: We really want to scale the value here, but the LDRD/STRD
1317 // instruction don't encode operands that way yet.
1318 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1319 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
1320 }
1321
Jim Grosbach72f39f82011-08-24 21:22:15 +00001322 void addImm0_1020s4Operands(MCInst &Inst, unsigned N) const {
1323 assert(N == 1 && "Invalid number of operands!");
1324 // The immediate is scaled by four in the encoding and is stored
1325 // in the MCInst as such. Lop off the low two bits here.
1326 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1327 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1328 }
1329
1330 void addImm0_508s4Operands(MCInst &Inst, unsigned N) const {
1331 assert(N == 1 && "Invalid number of operands!");
1332 // The immediate is scaled by four in the encoding and is stored
1333 // in the MCInst as such. Lop off the low two bits here.
1334 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1335 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
1336 }
1337
Jim Grosbachf4943352011-07-25 23:09:14 +00001338 void addImm1_16Operands(MCInst &Inst, unsigned N) const {
1339 assert(N == 1 && "Invalid number of operands!");
1340 // The constant encodes as the immediate-1, and we store in the instruction
1341 // the bits as encoded, so subtract off one here.
1342 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1343 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1344 }
1345
Jim Grosbach4a5ffb32011-07-22 23:16:18 +00001346 void addImm1_32Operands(MCInst &Inst, unsigned N) const {
1347 assert(N == 1 && "Invalid number of operands!");
1348 // The constant encodes as the immediate-1, and we store in the instruction
1349 // the bits as encoded, so subtract off one here.
1350 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1351 Inst.addOperand(MCOperand::CreateImm(CE->getValue() - 1));
1352 }
1353
Jim Grosbach70939ee2011-08-17 21:51:27 +00001354 void addImmThumbSROperands(MCInst &Inst, unsigned N) const {
1355 assert(N == 1 && "Invalid number of operands!");
1356 // The constant encodes as the immediate, except for 32, which encodes as
1357 // zero.
1358 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1359 unsigned Imm = CE->getValue();
1360 Inst.addOperand(MCOperand::CreateImm((Imm == 32 ? 0 : Imm)));
1361 }
1362
Jim Grosbachf6c05252011-07-21 17:23:04 +00001363 void addPKHASRImmOperands(MCInst &Inst, unsigned N) const {
1364 assert(N == 1 && "Invalid number of operands!");
1365 // An ASR value of 32 encodes as 0, so that's how we want to add it to
1366 // the instruction as well.
1367 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1368 int Val = CE->getValue();
1369 Inst.addOperand(MCOperand::CreateImm(Val == 32 ? 0 : Val));
1370 }
1371
Jim Grosbach89a63372011-10-28 22:36:30 +00001372 void addT2SOImmNotOperands(MCInst &Inst, unsigned N) const {
1373 assert(N == 1 && "Invalid number of operands!");
1374 // The operand is actually a t2_so_imm, but we have its bitwise
1375 // negation in the assembly source, so twiddle it here.
1376 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1377 Inst.addOperand(MCOperand::CreateImm(~CE->getValue()));
1378 }
1379
Jim Grosbach3bc8a3d2011-12-08 00:31:07 +00001380 void addT2SOImmNegOperands(MCInst &Inst, unsigned N) const {
1381 assert(N == 1 && "Invalid number of operands!");
1382 // The operand is actually a t2_so_imm, but we have its
1383 // negation in the assembly source, so twiddle it here.
1384 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1385 Inst.addOperand(MCOperand::CreateImm(-CE->getValue()));
1386 }
1387
Jim Grosbache70ec842011-10-28 22:50:54 +00001388 void addARMSOImmNotOperands(MCInst &Inst, unsigned N) const {
1389 assert(N == 1 && "Invalid number of operands!");
1390 // The operand is actually a so_imm, but we have its bitwise
1391 // negation in the assembly source, so twiddle it here.
1392 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1393 Inst.addOperand(MCOperand::CreateImm(~CE->getValue()));
1394 }
1395
Jim Grosbach3bc8a3d2011-12-08 00:31:07 +00001396 void addARMSOImmNegOperands(MCInst &Inst, unsigned N) const {
1397 assert(N == 1 && "Invalid number of operands!");
1398 // The operand is actually a so_imm, but we have its
1399 // negation in the assembly source, so twiddle it here.
1400 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1401 Inst.addOperand(MCOperand::CreateImm(-CE->getValue()));
1402 }
1403
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001404 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
1405 assert(N == 1 && "Invalid number of operands!");
1406 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
1407 }
1408
Jim Grosbach7ce05792011-08-03 23:50:40 +00001409 void addMemNoOffsetOperands(MCInst &Inst, unsigned N) const {
1410 assert(N == 1 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001411 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +00001412 }
1413
Jim Grosbach57dcb852011-10-11 17:29:55 +00001414 void addAlignedMemoryOperands(MCInst &Inst, unsigned N) const {
1415 assert(N == 2 && "Invalid number of operands!");
1416 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1417 Inst.addOperand(MCOperand::CreateImm(Memory.Alignment));
1418 }
1419
Jim Grosbach7ce05792011-08-03 23:50:40 +00001420 void addAddrMode2Operands(MCInst &Inst, unsigned N) const {
1421 assert(N == 3 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001422 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1423 if (!Memory.OffsetRegNum) {
Jim Grosbach7ce05792011-08-03 23:50:40 +00001424 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1425 // Special case for #-0
1426 if (Val == INT32_MIN) Val = 0;
1427 if (Val < 0) Val = -Val;
1428 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1429 } else {
1430 // For register offset, we encode the shift type and negation flag
1431 // here.
Jim Grosbache53c87b2011-10-11 15:59:20 +00001432 Val = ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1433 Memory.ShiftImm, Memory.ShiftType);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001434 }
Jim Grosbache53c87b2011-10-11 15:59:20 +00001435 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1436 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
Jim Grosbach7ce05792011-08-03 23:50:40 +00001437 Inst.addOperand(MCOperand::CreateImm(Val));
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001438 }
1439
Jim Grosbach039c2e12011-08-04 23:01:30 +00001440 void addAM2OffsetImmOperands(MCInst &Inst, unsigned N) const {
1441 assert(N == 2 && "Invalid number of operands!");
1442 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1443 assert(CE && "non-constant AM2OffsetImm operand!");
1444 int32_t Val = CE->getValue();
1445 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1446 // Special case for #-0
1447 if (Val == INT32_MIN) Val = 0;
1448 if (Val < 0) Val = -Val;
1449 Val = ARM_AM::getAM2Opc(AddSub, Val, ARM_AM::no_shift);
1450 Inst.addOperand(MCOperand::CreateReg(0));
1451 Inst.addOperand(MCOperand::CreateImm(Val));
1452 }
1453
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001454 void addAddrMode3Operands(MCInst &Inst, unsigned N) const {
1455 assert(N == 3 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001456 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1457 if (!Memory.OffsetRegNum) {
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001458 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1459 // Special case for #-0
1460 if (Val == INT32_MIN) Val = 0;
1461 if (Val < 0) Val = -Val;
1462 Val = ARM_AM::getAM3Opc(AddSub, Val);
1463 } else {
1464 // For register offset, we encode the shift type and negation flag
1465 // here.
Jim Grosbache53c87b2011-10-11 15:59:20 +00001466 Val = ARM_AM::getAM3Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add, 0);
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001467 }
Jim Grosbache53c87b2011-10-11 15:59:20 +00001468 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1469 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001470 Inst.addOperand(MCOperand::CreateImm(Val));
1471 }
1472
1473 void addAM3OffsetOperands(MCInst &Inst, unsigned N) const {
1474 assert(N == 2 && "Invalid number of operands!");
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001475 if (Kind == k_PostIndexRegister) {
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001476 int32_t Val =
1477 ARM_AM::getAM3Opc(PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub, 0);
1478 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1479 Inst.addOperand(MCOperand::CreateImm(Val));
Jim Grosbach251bf252011-08-10 21:56:18 +00001480 return;
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001481 }
1482
1483 // Constant offset.
1484 const MCConstantExpr *CE = static_cast<const MCConstantExpr*>(getImm());
1485 int32_t Val = CE->getValue();
1486 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1487 // Special case for #-0
1488 if (Val == INT32_MIN) Val = 0;
1489 if (Val < 0) Val = -Val;
Jim Grosbach251bf252011-08-10 21:56:18 +00001490 Val = ARM_AM::getAM3Opc(AddSub, Val);
Jim Grosbach2fd2b872011-08-10 20:29:19 +00001491 Inst.addOperand(MCOperand::CreateReg(0));
1492 Inst.addOperand(MCOperand::CreateImm(Val));
1493 }
1494
Jim Grosbach7ce05792011-08-03 23:50:40 +00001495 void addAddrMode5Operands(MCInst &Inst, unsigned N) const {
1496 assert(N == 2 && "Invalid number of operands!");
Jim Grosbach681460f2011-11-01 01:24:45 +00001497 // If we have an immediate that's not a constant, treat it as a label
1498 // reference needing a fixup. If it is a constant, it's something else
1499 // and we reject it.
1500 if (isImm()) {
1501 Inst.addOperand(MCOperand::CreateExpr(getImm()));
1502 Inst.addOperand(MCOperand::CreateImm(0));
1503 return;
1504 }
1505
Jim Grosbach7ce05792011-08-03 23:50:40 +00001506 // The lower two bits are always zero and as such are not encoded.
Jim Grosbache53c87b2011-10-11 15:59:20 +00001507 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
Jim Grosbach7ce05792011-08-03 23:50:40 +00001508 ARM_AM::AddrOpc AddSub = Val < 0 ? ARM_AM::sub : ARM_AM::add;
1509 // Special case for #-0
1510 if (Val == INT32_MIN) Val = 0;
1511 if (Val < 0) Val = -Val;
1512 Val = ARM_AM::getAM5Opc(AddSub, Val);
Jim Grosbache53c87b2011-10-11 15:59:20 +00001513 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbach7ce05792011-08-03 23:50:40 +00001514 Inst.addOperand(MCOperand::CreateImm(Val));
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001515 }
1516
Jim Grosbacha77295d2011-09-08 22:07:06 +00001517 void addMemImm8s4OffsetOperands(MCInst &Inst, unsigned N) const {
1518 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001519 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1520 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbacha77295d2011-09-08 22:07:06 +00001521 Inst.addOperand(MCOperand::CreateImm(Val));
1522 }
1523
Jim Grosbachb6aed502011-09-09 18:37:27 +00001524 void addMemImm0_1020s4OffsetOperands(MCInst &Inst, unsigned N) const {
1525 assert(N == 2 && "Invalid number of operands!");
1526 // The lower two bits are always zero and as such are not encoded.
Jim Grosbache53c87b2011-10-11 15:59:20 +00001527 int32_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() / 4 : 0;
1528 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbachb6aed502011-09-09 18:37:27 +00001529 Inst.addOperand(MCOperand::CreateImm(Val));
1530 }
1531
Jim Grosbach7ce05792011-08-03 23:50:40 +00001532 void addMemImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1533 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001534 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1535 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbach7ce05792011-08-03 23:50:40 +00001536 Inst.addOperand(MCOperand::CreateImm(Val));
Chris Lattner14b93852010-10-29 00:27:31 +00001537 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001538
Jim Grosbachf0eee6e2011-09-07 23:39:14 +00001539 void addMemPosImm8OffsetOperands(MCInst &Inst, unsigned N) const {
1540 addMemImm8OffsetOperands(Inst, N);
1541 }
1542
Jim Grosbacha8307dd2011-09-07 20:58:57 +00001543 void addMemNegImm8OffsetOperands(MCInst &Inst, unsigned N) const {
Jim Grosbachf0eee6e2011-09-07 23:39:14 +00001544 addMemImm8OffsetOperands(Inst, N);
Jim Grosbacha8307dd2011-09-07 20:58:57 +00001545 }
1546
1547 void addMemUImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1548 assert(N == 2 && "Invalid number of operands!");
1549 // If this is an immediate, it's a label reference.
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001550 if (Kind == k_Immediate) {
Jim Grosbacha8307dd2011-09-07 20:58:57 +00001551 addExpr(Inst, getImm());
1552 Inst.addOperand(MCOperand::CreateImm(0));
1553 return;
1554 }
1555
1556 // Otherwise, it's a normal memory reg+offset.
Jim Grosbache53c87b2011-10-11 15:59:20 +00001557 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1558 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbacha8307dd2011-09-07 20:58:57 +00001559 Inst.addOperand(MCOperand::CreateImm(Val));
1560 }
1561
Jim Grosbach7ce05792011-08-03 23:50:40 +00001562 void addMemImm12OffsetOperands(MCInst &Inst, unsigned N) const {
1563 assert(N == 2 && "Invalid number of operands!");
Jim Grosbach09176e12011-08-08 20:59:31 +00001564 // If this is an immediate, it's a label reference.
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001565 if (Kind == k_Immediate) {
Jim Grosbach09176e12011-08-08 20:59:31 +00001566 addExpr(Inst, getImm());
1567 Inst.addOperand(MCOperand::CreateImm(0));
1568 return;
1569 }
1570
1571 // Otherwise, it's a normal memory reg+offset.
Jim Grosbache53c87b2011-10-11 15:59:20 +00001572 int64_t Val = Memory.OffsetImm ? Memory.OffsetImm->getValue() : 0;
1573 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbach7ce05792011-08-03 23:50:40 +00001574 Inst.addOperand(MCOperand::CreateImm(Val));
Bill Wendlingf4caf692010-12-14 03:36:38 +00001575 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +00001576
Jim Grosbach7f739be2011-09-19 22:21:13 +00001577 void addMemTBBOperands(MCInst &Inst, unsigned N) const {
1578 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001579 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1580 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
Jim Grosbach7f739be2011-09-19 22:21:13 +00001581 }
1582
1583 void addMemTBHOperands(MCInst &Inst, unsigned N) const {
1584 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001585 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1586 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
Jim Grosbach7f739be2011-09-19 22:21:13 +00001587 }
1588
Jim Grosbach7ce05792011-08-03 23:50:40 +00001589 void addMemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1590 assert(N == 3 && "Invalid number of operands!");
Jim Grosbach430052b2011-11-14 17:52:47 +00001591 unsigned Val =
1592 ARM_AM::getAM2Opc(Memory.isNegative ? ARM_AM::sub : ARM_AM::add,
1593 Memory.ShiftImm, Memory.ShiftType);
Jim Grosbache53c87b2011-10-11 15:59:20 +00001594 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1595 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
Jim Grosbach7ce05792011-08-03 23:50:40 +00001596 Inst.addOperand(MCOperand::CreateImm(Val));
1597 }
1598
Jim Grosbachab899c12011-09-07 23:10:15 +00001599 void addT2MemRegOffsetOperands(MCInst &Inst, unsigned N) const {
1600 assert(N == 3 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001601 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1602 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
1603 Inst.addOperand(MCOperand::CreateImm(Memory.ShiftImm));
Jim Grosbachab899c12011-09-07 23:10:15 +00001604 }
1605
Jim Grosbach7ce05792011-08-03 23:50:40 +00001606 void addMemThumbRROperands(MCInst &Inst, unsigned N) const {
1607 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001608 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
1609 Inst.addOperand(MCOperand::CreateReg(Memory.OffsetRegNum));
Jim Grosbach7ce05792011-08-03 23:50:40 +00001610 }
1611
Jim Grosbach60f91a32011-08-19 17:55:24 +00001612 void addMemThumbRIs4Operands(MCInst &Inst, unsigned N) const {
1613 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001614 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1615 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbach60f91a32011-08-19 17:55:24 +00001616 Inst.addOperand(MCOperand::CreateImm(Val));
1617 }
1618
Jim Grosbach38466302011-08-19 18:55:51 +00001619 void addMemThumbRIs2Operands(MCInst &Inst, unsigned N) const {
1620 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001621 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 2) : 0;
1622 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbach38466302011-08-19 18:55:51 +00001623 Inst.addOperand(MCOperand::CreateImm(Val));
1624 }
1625
Jim Grosbach48ff5ff2011-08-19 18:49:59 +00001626 void addMemThumbRIs1Operands(MCInst &Inst, unsigned N) const {
1627 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001628 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue()) : 0;
1629 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbach48ff5ff2011-08-19 18:49:59 +00001630 Inst.addOperand(MCOperand::CreateImm(Val));
1631 }
1632
Jim Grosbachecd85892011-08-19 18:13:48 +00001633 void addMemThumbSPIOperands(MCInst &Inst, unsigned N) const {
1634 assert(N == 2 && "Invalid number of operands!");
Jim Grosbache53c87b2011-10-11 15:59:20 +00001635 int64_t Val = Memory.OffsetImm ? (Memory.OffsetImm->getValue() / 4) : 0;
1636 Inst.addOperand(MCOperand::CreateReg(Memory.BaseRegNum));
Jim Grosbachecd85892011-08-19 18:13:48 +00001637 Inst.addOperand(MCOperand::CreateImm(Val));
1638 }
1639
Jim Grosbach7ce05792011-08-03 23:50:40 +00001640 void addPostIdxImm8Operands(MCInst &Inst, unsigned N) const {
1641 assert(N == 1 && "Invalid number of operands!");
1642 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1643 assert(CE && "non-constant post-idx-imm8 operand!");
1644 int Imm = CE->getValue();
1645 bool isAdd = Imm >= 0;
Owen Anderson63553c72011-08-29 17:17:09 +00001646 if (Imm == INT32_MIN) Imm = 0;
Jim Grosbach7ce05792011-08-03 23:50:40 +00001647 Imm = (Imm < 0 ? -Imm : Imm) | (int)isAdd << 8;
1648 Inst.addOperand(MCOperand::CreateImm(Imm));
1649 }
1650
Jim Grosbach2bd01182011-10-11 21:55:36 +00001651 void addPostIdxImm8s4Operands(MCInst &Inst, unsigned N) const {
1652 assert(N == 1 && "Invalid number of operands!");
1653 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1654 assert(CE && "non-constant post-idx-imm8s4 operand!");
1655 int Imm = CE->getValue();
1656 bool isAdd = Imm >= 0;
1657 if (Imm == INT32_MIN) Imm = 0;
1658 // Immediate is scaled by 4.
1659 Imm = ((Imm < 0 ? -Imm : Imm) / 4) | (int)isAdd << 8;
1660 Inst.addOperand(MCOperand::CreateImm(Imm));
1661 }
1662
Jim Grosbach7ce05792011-08-03 23:50:40 +00001663 void addPostIdxRegOperands(MCInst &Inst, unsigned N) const {
1664 assert(N == 2 && "Invalid number of operands!");
1665 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00001666 Inst.addOperand(MCOperand::CreateImm(PostIdxReg.isAdd));
1667 }
1668
1669 void addPostIdxRegShiftedOperands(MCInst &Inst, unsigned N) const {
1670 assert(N == 2 && "Invalid number of operands!");
1671 Inst.addOperand(MCOperand::CreateReg(PostIdxReg.RegNum));
1672 // The sign, shift type, and shift amount are encoded in a single operand
1673 // using the AM2 encoding helpers.
1674 ARM_AM::AddrOpc opc = PostIdxReg.isAdd ? ARM_AM::add : ARM_AM::sub;
1675 unsigned Imm = ARM_AM::getAM2Opc(opc, PostIdxReg.ShiftImm,
1676 PostIdxReg.ShiftTy);
1677 Inst.addOperand(MCOperand::CreateImm(Imm));
Bill Wendlingef4a68b2010-11-30 07:44:32 +00001678 }
1679
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001680 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
1681 assert(N == 1 && "Invalid number of operands!");
1682 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
1683 }
1684
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001685 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
1686 assert(N == 1 && "Invalid number of operands!");
1687 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
1688 }
1689
Jim Grosbach6029b6d2011-11-29 23:51:09 +00001690 void addVecListOperands(MCInst &Inst, unsigned N) const {
Jim Grosbach862019c2011-10-18 23:02:30 +00001691 assert(N == 1 && "Invalid number of operands!");
1692 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1693 }
1694
Jim Grosbach7636bf62011-12-02 00:35:16 +00001695 void addVecListIndexedOperands(MCInst &Inst, unsigned N) const {
1696 assert(N == 2 && "Invalid number of operands!");
1697 Inst.addOperand(MCOperand::CreateReg(VectorList.RegNum));
1698 Inst.addOperand(MCOperand::CreateImm(VectorList.LaneIndex));
1699 }
1700
Jim Grosbach460a9052011-10-07 23:56:00 +00001701 void addVectorIndex8Operands(MCInst &Inst, unsigned N) const {
1702 assert(N == 1 && "Invalid number of operands!");
1703 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1704 }
1705
1706 void addVectorIndex16Operands(MCInst &Inst, unsigned N) const {
1707 assert(N == 1 && "Invalid number of operands!");
1708 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1709 }
1710
1711 void addVectorIndex32Operands(MCInst &Inst, unsigned N) const {
1712 assert(N == 1 && "Invalid number of operands!");
1713 Inst.addOperand(MCOperand::CreateImm(getVectorIndex()));
1714 }
1715
Jim Grosbach0e387b22011-10-17 22:26:03 +00001716 void addNEONi8splatOperands(MCInst &Inst, unsigned N) const {
1717 assert(N == 1 && "Invalid number of operands!");
1718 // The immediate encodes the type of constant as well as the value.
1719 // Mask in that this is an i8 splat.
1720 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1721 Inst.addOperand(MCOperand::CreateImm(CE->getValue() | 0xe00));
1722 }
1723
Jim Grosbachea461102011-10-17 23:09:09 +00001724 void addNEONi16splatOperands(MCInst &Inst, unsigned N) const {
1725 assert(N == 1 && "Invalid number of operands!");
1726 // The immediate encodes the type of constant as well as the value.
1727 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1728 unsigned Value = CE->getValue();
1729 if (Value >= 256)
1730 Value = (Value >> 8) | 0xa00;
1731 else
1732 Value |= 0x800;
1733 Inst.addOperand(MCOperand::CreateImm(Value));
1734 }
1735
Jim Grosbach6248a542011-10-18 00:22:00 +00001736 void addNEONi32splatOperands(MCInst &Inst, unsigned N) const {
1737 assert(N == 1 && "Invalid number of operands!");
1738 // The immediate encodes the type of constant as well as the value.
1739 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1740 unsigned Value = CE->getValue();
1741 if (Value >= 256 && Value <= 0xff00)
1742 Value = (Value >> 8) | 0x200;
1743 else if (Value > 0xffff && Value <= 0xff0000)
1744 Value = (Value >> 16) | 0x400;
1745 else if (Value > 0xffffff)
1746 Value = (Value >> 24) | 0x600;
1747 Inst.addOperand(MCOperand::CreateImm(Value));
1748 }
1749
1750 void addNEONi32vmovOperands(MCInst &Inst, unsigned N) const {
1751 assert(N == 1 && "Invalid number of operands!");
1752 // The immediate encodes the type of constant as well as the value.
1753 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1754 unsigned Value = CE->getValue();
1755 if (Value >= 256 && Value <= 0xffff)
1756 Value = (Value >> 8) | ((Value & 0xff) ? 0xc00 : 0x200);
1757 else if (Value > 0xffff && Value <= 0xffffff)
1758 Value = (Value >> 16) | ((Value & 0xff) ? 0xd00 : 0x400);
1759 else if (Value > 0xffffff)
1760 Value = (Value >> 24) | 0x600;
1761 Inst.addOperand(MCOperand::CreateImm(Value));
1762 }
1763
Jim Grosbachf2f5bc62011-10-18 16:18:11 +00001764 void addNEONi64splatOperands(MCInst &Inst, unsigned N) const {
1765 assert(N == 1 && "Invalid number of operands!");
1766 // The immediate encodes the type of constant as well as the value.
1767 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
1768 uint64_t Value = CE->getValue();
1769 unsigned Imm = 0;
1770 for (unsigned i = 0; i < 8; ++i, Value >>= 8) {
1771 Imm |= (Value & 1) << i;
1772 }
1773 Inst.addOperand(MCOperand::CreateImm(Imm | 0x1e00));
1774 }
1775
Jim Grosbachb7f689b2011-07-13 15:34:57 +00001776 virtual void print(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +00001777
Jim Grosbach89df9962011-08-26 21:43:41 +00001778 static ARMOperand *CreateITMask(unsigned Mask, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001779 ARMOperand *Op = new ARMOperand(k_ITCondMask);
Jim Grosbach89df9962011-08-26 21:43:41 +00001780 Op->ITMask.Mask = Mask;
1781 Op->StartLoc = S;
1782 Op->EndLoc = S;
1783 return Op;
1784 }
1785
Chris Lattner3a697562010-10-28 17:20:03 +00001786 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001787 ARMOperand *Op = new ARMOperand(k_CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001788 Op->CC.Val = CC;
1789 Op->StartLoc = S;
1790 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +00001791 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001792 }
1793
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001794 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001795 ARMOperand *Op = new ARMOperand(k_CoprocNum);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001796 Op->Cop.Val = CopVal;
1797 Op->StartLoc = S;
1798 Op->EndLoc = S;
1799 return Op;
1800 }
1801
1802 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001803 ARMOperand *Op = new ARMOperand(k_CoprocReg);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001804 Op->Cop.Val = CopVal;
1805 Op->StartLoc = S;
1806 Op->EndLoc = S;
1807 return Op;
1808 }
1809
Jim Grosbach9b8f2a02011-10-12 17:34:41 +00001810 static ARMOperand *CreateCoprocOption(unsigned Val, SMLoc S, SMLoc E) {
1811 ARMOperand *Op = new ARMOperand(k_CoprocOption);
1812 Op->Cop.Val = Val;
1813 Op->StartLoc = S;
1814 Op->EndLoc = E;
1815 return Op;
1816 }
1817
Jim Grosbachd67641b2010-12-06 18:21:12 +00001818 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001819 ARMOperand *Op = new ARMOperand(k_CCOut);
Jim Grosbachd67641b2010-12-06 18:21:12 +00001820 Op->Reg.RegNum = RegNum;
1821 Op->StartLoc = S;
1822 Op->EndLoc = S;
1823 return Op;
1824 }
1825
Chris Lattner3a697562010-10-28 17:20:03 +00001826 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001827 ARMOperand *Op = new ARMOperand(k_Token);
Sean Callanan76264762010-04-02 22:27:05 +00001828 Op->Tok.Data = Str.data();
1829 Op->Tok.Length = Str.size();
1830 Op->StartLoc = S;
1831 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +00001832 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001833 }
1834
Bill Wendling50d0f582010-11-18 23:43:05 +00001835 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001836 ARMOperand *Op = new ARMOperand(k_Register);
Sean Callanan76264762010-04-02 22:27:05 +00001837 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +00001838 Op->StartLoc = S;
1839 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +00001840 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001841 }
1842
Jim Grosbache8606dc2011-07-13 17:50:29 +00001843 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
1844 unsigned SrcReg,
1845 unsigned ShiftReg,
1846 unsigned ShiftImm,
1847 SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001848 ARMOperand *Op = new ARMOperand(k_ShiftedRegister);
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001849 Op->RegShiftedReg.ShiftTy = ShTy;
1850 Op->RegShiftedReg.SrcReg = SrcReg;
1851 Op->RegShiftedReg.ShiftReg = ShiftReg;
1852 Op->RegShiftedReg.ShiftImm = ShiftImm;
Jim Grosbache8606dc2011-07-13 17:50:29 +00001853 Op->StartLoc = S;
1854 Op->EndLoc = E;
1855 return Op;
1856 }
1857
Owen Anderson92a20222011-07-21 18:54:16 +00001858 static ARMOperand *CreateShiftedImmediate(ARM_AM::ShiftOpc ShTy,
1859 unsigned SrcReg,
1860 unsigned ShiftImm,
1861 SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001862 ARMOperand *Op = new ARMOperand(k_ShiftedImmediate);
Jim Grosbachaf6981f2011-07-25 20:49:51 +00001863 Op->RegShiftedImm.ShiftTy = ShTy;
1864 Op->RegShiftedImm.SrcReg = SrcReg;
1865 Op->RegShiftedImm.ShiftImm = ShiftImm;
Owen Anderson92a20222011-07-21 18:54:16 +00001866 Op->StartLoc = S;
1867 Op->EndLoc = E;
1868 return Op;
1869 }
1870
Jim Grosbach580f4a92011-07-25 22:20:28 +00001871 static ARMOperand *CreateShifterImm(bool isASR, unsigned Imm,
Owen Anderson00828302011-03-18 22:50:18 +00001872 SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001873 ARMOperand *Op = new ARMOperand(k_ShifterImmediate);
Jim Grosbach580f4a92011-07-25 22:20:28 +00001874 Op->ShifterImm.isASR = isASR;
1875 Op->ShifterImm.Imm = Imm;
Owen Anderson00828302011-03-18 22:50:18 +00001876 Op->StartLoc = S;
1877 Op->EndLoc = E;
1878 return Op;
1879 }
1880
Jim Grosbach7e1547e2011-07-27 20:15:40 +00001881 static ARMOperand *CreateRotImm(unsigned Imm, SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001882 ARMOperand *Op = new ARMOperand(k_RotateImmediate);
Jim Grosbach7e1547e2011-07-27 20:15:40 +00001883 Op->RotImm.Imm = Imm;
1884 Op->StartLoc = S;
1885 Op->EndLoc = E;
1886 return Op;
1887 }
1888
Jim Grosbach293a2ee2011-07-28 21:34:26 +00001889 static ARMOperand *CreateBitfield(unsigned LSB, unsigned Width,
1890 SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001891 ARMOperand *Op = new ARMOperand(k_BitfieldDescriptor);
Jim Grosbach293a2ee2011-07-28 21:34:26 +00001892 Op->Bitfield.LSB = LSB;
1893 Op->Bitfield.Width = Width;
1894 Op->StartLoc = S;
1895 Op->EndLoc = E;
1896 return Op;
1897 }
1898
Bill Wendling7729e062010-11-09 22:44:22 +00001899 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +00001900 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +00001901 SMLoc StartLoc, SMLoc EndLoc) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001902 KindTy Kind = k_RegisterList;
Bill Wendling0f630752010-11-17 04:32:08 +00001903
Jim Grosbachd300b942011-09-13 22:56:44 +00001904 if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Regs.front().first))
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001905 Kind = k_DPRRegisterList;
Jim Grosbachd300b942011-09-13 22:56:44 +00001906 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].
Evan Cheng275944a2011-07-25 21:32:49 +00001907 contains(Regs.front().first))
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001908 Kind = k_SPRRegisterList;
Bill Wendling0f630752010-11-17 04:32:08 +00001909
1910 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +00001911 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +00001912 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +00001913 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +00001914 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +00001915 Op->StartLoc = StartLoc;
1916 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +00001917 return Op;
1918 }
1919
Jim Grosbach862019c2011-10-18 23:02:30 +00001920 static ARMOperand *CreateVectorList(unsigned RegNum, unsigned Count,
1921 SMLoc S, SMLoc E) {
1922 ARMOperand *Op = new ARMOperand(k_VectorList);
1923 Op->VectorList.RegNum = RegNum;
1924 Op->VectorList.Count = Count;
1925 Op->StartLoc = S;
1926 Op->EndLoc = E;
1927 return Op;
1928 }
1929
Jim Grosbach98b05a52011-11-30 01:09:44 +00001930 static ARMOperand *CreateVectorListAllLanes(unsigned RegNum, unsigned Count,
1931 SMLoc S, SMLoc E) {
1932 ARMOperand *Op = new ARMOperand(k_VectorListAllLanes);
1933 Op->VectorList.RegNum = RegNum;
1934 Op->VectorList.Count = Count;
1935 Op->StartLoc = S;
1936 Op->EndLoc = E;
1937 return Op;
1938 }
1939
Jim Grosbach7636bf62011-12-02 00:35:16 +00001940 static ARMOperand *CreateVectorListIndexed(unsigned RegNum, unsigned Count,
1941 unsigned Index, SMLoc S, SMLoc E) {
1942 ARMOperand *Op = new ARMOperand(k_VectorListIndexed);
1943 Op->VectorList.RegNum = RegNum;
1944 Op->VectorList.Count = Count;
1945 Op->VectorList.LaneIndex = Index;
1946 Op->StartLoc = S;
1947 Op->EndLoc = E;
1948 return Op;
1949 }
1950
Jim Grosbach460a9052011-10-07 23:56:00 +00001951 static ARMOperand *CreateVectorIndex(unsigned Idx, SMLoc S, SMLoc E,
1952 MCContext &Ctx) {
1953 ARMOperand *Op = new ARMOperand(k_VectorIndex);
1954 Op->VectorIndex.Val = Idx;
1955 Op->StartLoc = S;
1956 Op->EndLoc = E;
1957 return Op;
1958 }
1959
Chris Lattner3a697562010-10-28 17:20:03 +00001960 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001961 ARMOperand *Op = new ARMOperand(k_Immediate);
Sean Callanan76264762010-04-02 22:27:05 +00001962 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +00001963 Op->StartLoc = S;
1964 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +00001965 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +00001966 }
1967
Jim Grosbach9d390362011-10-03 23:38:36 +00001968 static ARMOperand *CreateFPImm(unsigned Val, SMLoc S, MCContext &Ctx) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001969 ARMOperand *Op = new ARMOperand(k_FPImmediate);
Jim Grosbach9d390362011-10-03 23:38:36 +00001970 Op->FPImm.Val = Val;
1971 Op->StartLoc = S;
1972 Op->EndLoc = S;
1973 return Op;
1974 }
1975
Jim Grosbach7ce05792011-08-03 23:50:40 +00001976 static ARMOperand *CreateMem(unsigned BaseRegNum,
1977 const MCConstantExpr *OffsetImm,
1978 unsigned OffsetRegNum,
1979 ARM_AM::ShiftOpc ShiftType,
Jim Grosbach0d6fac32011-08-05 22:03:36 +00001980 unsigned ShiftImm,
Jim Grosbach57dcb852011-10-11 17:29:55 +00001981 unsigned Alignment,
Jim Grosbach7ce05792011-08-03 23:50:40 +00001982 bool isNegative,
Chris Lattner3a697562010-10-28 17:20:03 +00001983 SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00001984 ARMOperand *Op = new ARMOperand(k_Memory);
Jim Grosbache53c87b2011-10-11 15:59:20 +00001985 Op->Memory.BaseRegNum = BaseRegNum;
1986 Op->Memory.OffsetImm = OffsetImm;
1987 Op->Memory.OffsetRegNum = OffsetRegNum;
1988 Op->Memory.ShiftType = ShiftType;
1989 Op->Memory.ShiftImm = ShiftImm;
Jim Grosbach57dcb852011-10-11 17:29:55 +00001990 Op->Memory.Alignment = Alignment;
Jim Grosbache53c87b2011-10-11 15:59:20 +00001991 Op->Memory.isNegative = isNegative;
Jim Grosbach7ce05792011-08-03 23:50:40 +00001992 Op->StartLoc = S;
1993 Op->EndLoc = E;
1994 return Op;
1995 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001996
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00001997 static ARMOperand *CreatePostIdxReg(unsigned RegNum, bool isAdd,
1998 ARM_AM::ShiftOpc ShiftTy,
1999 unsigned ShiftImm,
Jim Grosbach7ce05792011-08-03 23:50:40 +00002000 SMLoc S, SMLoc E) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002001 ARMOperand *Op = new ARMOperand(k_PostIndexRegister);
Jim Grosbach7ce05792011-08-03 23:50:40 +00002002 Op->PostIdxReg.RegNum = RegNum;
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00002003 Op->PostIdxReg.isAdd = isAdd;
2004 Op->PostIdxReg.ShiftTy = ShiftTy;
2005 Op->PostIdxReg.ShiftImm = ShiftImm;
Sean Callanan76264762010-04-02 22:27:05 +00002006 Op->StartLoc = S;
2007 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +00002008 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002009 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002010
2011 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002012 ARMOperand *Op = new ARMOperand(k_MemBarrierOpt);
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002013 Op->MBOpt.Val = Opt;
2014 Op->StartLoc = S;
2015 Op->EndLoc = S;
2016 return Op;
2017 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002018
2019 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002020 ARMOperand *Op = new ARMOperand(k_ProcIFlags);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002021 Op->IFlags.Val = IFlags;
2022 Op->StartLoc = S;
2023 Op->EndLoc = S;
2024 return Op;
2025 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002026
2027 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002028 ARMOperand *Op = new ARMOperand(k_MSRMask);
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002029 Op->MMask.Val = MMask;
2030 Op->StartLoc = S;
2031 Op->EndLoc = S;
2032 return Op;
2033 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002034};
2035
2036} // end anonymous namespace.
2037
Jim Grosbachb7f689b2011-07-13 15:34:57 +00002038void ARMOperand::print(raw_ostream &OS) const {
Daniel Dunbarfa315de2010-08-11 06:37:12 +00002039 switch (Kind) {
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002040 case k_FPImmediate:
Jim Grosbach9d390362011-10-03 23:38:36 +00002041 OS << "<fpimm " << getFPImm() << "(" << ARM_AM::getFPImmFloat(getFPImm())
2042 << ") >";
2043 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002044 case k_CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +00002045 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00002046 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002047 case k_CCOut:
Jim Grosbachd67641b2010-12-06 18:21:12 +00002048 OS << "<ccout " << getReg() << ">";
2049 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002050 case k_ITCondMask: {
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00002051 static const char *MaskStr[] = {
2052 "()", "(t)", "(e)", "(tt)", "(et)", "(te)", "(ee)", "(ttt)", "(ett)",
2053 "(tet)", "(eet)", "(tte)", "(ete)", "(tee)", "(eee)"
2054 };
Jim Grosbach89df9962011-08-26 21:43:41 +00002055 assert((ITMask.Mask & 0xf) == ITMask.Mask);
2056 OS << "<it-mask " << MaskStr[ITMask.Mask] << ">";
2057 break;
2058 }
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002059 case k_CoprocNum:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002060 OS << "<coprocessor number: " << getCoproc() << ">";
2061 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002062 case k_CoprocReg:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002063 OS << "<coprocessor register: " << getCoproc() << ">";
2064 break;
Jim Grosbach9b8f2a02011-10-12 17:34:41 +00002065 case k_CoprocOption:
2066 OS << "<coprocessor option: " << CoprocOption.Val << ">";
2067 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002068 case k_MSRMask:
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002069 OS << "<mask: " << getMSRMask() << ">";
2070 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002071 case k_Immediate:
Daniel Dunbarfa315de2010-08-11 06:37:12 +00002072 getImm()->print(OS);
2073 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002074 case k_MemBarrierOpt:
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002075 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
2076 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002077 case k_Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +00002078 OS << "<memory "
Jim Grosbache53c87b2011-10-11 15:59:20 +00002079 << " base:" << Memory.BaseRegNum;
Daniel Dunbar6ec56202011-01-18 05:55:21 +00002080 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00002081 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002082 case k_PostIndexRegister:
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00002083 OS << "post-idx register " << (PostIdxReg.isAdd ? "" : "-")
2084 << PostIdxReg.RegNum;
2085 if (PostIdxReg.ShiftTy != ARM_AM::no_shift)
2086 OS << ARM_AM::getShiftOpcStr(PostIdxReg.ShiftTy) << " "
2087 << PostIdxReg.ShiftImm;
2088 OS << ">";
Jim Grosbach7ce05792011-08-03 23:50:40 +00002089 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002090 case k_ProcIFlags: {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002091 OS << "<ARM_PROC::";
2092 unsigned IFlags = getProcIFlags();
2093 for (int i=2; i >= 0; --i)
2094 if (IFlags & (1 << i))
2095 OS << ARM_PROC::IFlagsToString(1 << i);
2096 OS << ">";
2097 break;
2098 }
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002099 case k_Register:
Bill Wendling50d0f582010-11-18 23:43:05 +00002100 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +00002101 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002102 case k_ShifterImmediate:
Jim Grosbach580f4a92011-07-25 22:20:28 +00002103 OS << "<shift " << (ShifterImm.isASR ? "asr" : "lsl")
2104 << " #" << ShifterImm.Imm << ">";
Jim Grosbache8606dc2011-07-13 17:50:29 +00002105 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002106 case k_ShiftedRegister:
Owen Anderson92a20222011-07-21 18:54:16 +00002107 OS << "<so_reg_reg "
Jim Grosbachefed3d12011-11-16 21:46:50 +00002108 << RegShiftedReg.SrcReg << " "
2109 << ARM_AM::getShiftOpcStr(RegShiftedReg.ShiftTy)
2110 << " " << RegShiftedReg.ShiftReg << ">";
Owen Anderson00828302011-03-18 22:50:18 +00002111 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002112 case k_ShiftedImmediate:
Owen Anderson92a20222011-07-21 18:54:16 +00002113 OS << "<so_reg_imm "
Jim Grosbachefed3d12011-11-16 21:46:50 +00002114 << RegShiftedImm.SrcReg << " "
2115 << ARM_AM::getShiftOpcStr(RegShiftedImm.ShiftTy)
2116 << " #" << RegShiftedImm.ShiftImm << ">";
Owen Anderson92a20222011-07-21 18:54:16 +00002117 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002118 case k_RotateImmediate:
Jim Grosbach7e1547e2011-07-27 20:15:40 +00002119 OS << "<ror " << " #" << (RotImm.Imm * 8) << ">";
2120 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002121 case k_BitfieldDescriptor:
Jim Grosbach293a2ee2011-07-28 21:34:26 +00002122 OS << "<bitfield " << "lsb: " << Bitfield.LSB
2123 << ", width: " << Bitfield.Width << ">";
2124 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002125 case k_RegisterList:
2126 case k_DPRRegisterList:
2127 case k_SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +00002128 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00002129
Bill Wendling5fa22a12010-11-09 23:28:44 +00002130 const SmallVectorImpl<unsigned> &RegList = getRegList();
2131 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +00002132 I = RegList.begin(), E = RegList.end(); I != E; ) {
2133 OS << *I;
2134 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00002135 }
2136
2137 OS << ">";
2138 break;
2139 }
Jim Grosbach862019c2011-10-18 23:02:30 +00002140 case k_VectorList:
2141 OS << "<vector_list " << VectorList.Count << " * "
2142 << VectorList.RegNum << ">";
2143 break;
Jim Grosbach98b05a52011-11-30 01:09:44 +00002144 case k_VectorListAllLanes:
2145 OS << "<vector_list(all lanes) " << VectorList.Count << " * "
2146 << VectorList.RegNum << ">";
2147 break;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002148 case k_VectorListIndexed:
2149 OS << "<vector_list(lane " << VectorList.LaneIndex << ") "
2150 << VectorList.Count << " * " << VectorList.RegNum << ">";
2151 break;
Jim Grosbach21ff17c2011-10-07 23:24:09 +00002152 case k_Token:
Daniel Dunbarfa315de2010-08-11 06:37:12 +00002153 OS << "'" << getToken() << "'";
2154 break;
Jim Grosbach460a9052011-10-07 23:56:00 +00002155 case k_VectorIndex:
2156 OS << "<vectorindex " << getVectorIndex() << ">";
2157 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +00002158 }
2159}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002160
2161/// @name Auto-generated Match Functions
2162/// {
2163
2164static unsigned MatchRegisterName(StringRef Name);
2165
2166/// }
2167
Bob Wilson69df7232011-02-03 21:46:10 +00002168bool ARMAsmParser::ParseRegister(unsigned &RegNo,
2169 SMLoc &StartLoc, SMLoc &EndLoc) {
Jim Grosbach1355cf12011-07-26 17:10:22 +00002170 RegNo = tryParseRegister();
Roman Divackybf755322011-01-27 17:14:22 +00002171
2172 return (RegNo == (unsigned)-1);
2173}
2174
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002175/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +00002176/// and if it is a register name the token is eaten and the register number is
2177/// returned. Otherwise return -1.
2178///
Jim Grosbach1355cf12011-07-26 17:10:22 +00002179int ARMAsmParser::tryParseRegister() {
Chris Lattnere5658fa2010-10-30 04:09:10 +00002180 const AsmToken &Tok = Parser.getTok();
Jim Grosbach7ce05792011-08-03 23:50:40 +00002181 if (Tok.isNot(AsmToken::Identifier)) return -1;
Jim Grosbachd4462a52010-11-01 16:44:21 +00002182
Benjamin Kramer59085362011-11-06 20:37:06 +00002183 std::string lowerCase = Tok.getString().lower();
Owen Anderson0c9f2502011-01-13 22:50:36 +00002184 unsigned RegNum = MatchRegisterName(lowerCase);
2185 if (!RegNum) {
2186 RegNum = StringSwitch<unsigned>(lowerCase)
2187 .Case("r13", ARM::SP)
2188 .Case("r14", ARM::LR)
2189 .Case("r15", ARM::PC)
2190 .Case("ip", ARM::R12)
Jim Grosbach40e28552011-12-08 19:27:38 +00002191 // Additional register name aliases for 'gas' compatibility.
2192 .Case("a1", ARM::R0)
2193 .Case("a2", ARM::R1)
2194 .Case("a3", ARM::R2)
2195 .Case("a4", ARM::R3)
2196 .Case("v1", ARM::R4)
2197 .Case("v2", ARM::R5)
2198 .Case("v3", ARM::R6)
2199 .Case("v4", ARM::R7)
2200 .Case("v5", ARM::R8)
2201 .Case("v6", ARM::R9)
2202 .Case("v7", ARM::R10)
2203 .Case("v8", ARM::R11)
2204 .Case("sb", ARM::R9)
2205 .Case("sl", ARM::R10)
2206 .Case("fp", ARM::R11)
Owen Anderson0c9f2502011-01-13 22:50:36 +00002207 .Default(0);
2208 }
2209 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +00002210
Chris Lattnere5658fa2010-10-30 04:09:10 +00002211 Parser.Lex(); // Eat identifier token.
Jim Grosbach460a9052011-10-07 23:56:00 +00002212
Chris Lattnere5658fa2010-10-30 04:09:10 +00002213 return RegNum;
2214}
Jim Grosbachd4462a52010-11-01 16:44:21 +00002215
Jim Grosbach19906722011-07-13 18:49:30 +00002216// Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
2217// If a recoverable error occurs, return 1. If an irrecoverable error
2218// occurs, return -1. An irrecoverable error is one where tokens have been
2219// consumed in the process of trying to parse the shifter (i.e., when it is
2220// indeed a shifter operand, but malformed).
Jim Grosbach0d87ec22011-07-26 20:41:24 +00002221int ARMAsmParser::tryParseShiftRegister(
Owen Anderson00828302011-03-18 22:50:18 +00002222 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2223 SMLoc S = Parser.getTok().getLoc();
2224 const AsmToken &Tok = Parser.getTok();
2225 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2226
Benjamin Kramer59085362011-11-06 20:37:06 +00002227 std::string lowerCase = Tok.getString().lower();
Owen Anderson00828302011-03-18 22:50:18 +00002228 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
Jim Grosbachaf4edea2011-12-07 23:40:58 +00002229 .Case("asl", ARM_AM::lsl)
Owen Anderson00828302011-03-18 22:50:18 +00002230 .Case("lsl", ARM_AM::lsl)
2231 .Case("lsr", ARM_AM::lsr)
2232 .Case("asr", ARM_AM::asr)
2233 .Case("ror", ARM_AM::ror)
2234 .Case("rrx", ARM_AM::rrx)
2235 .Default(ARM_AM::no_shift);
2236
2237 if (ShiftTy == ARM_AM::no_shift)
Jim Grosbach19906722011-07-13 18:49:30 +00002238 return 1;
Owen Anderson00828302011-03-18 22:50:18 +00002239
Jim Grosbache8606dc2011-07-13 17:50:29 +00002240 Parser.Lex(); // Eat the operator.
Owen Anderson00828302011-03-18 22:50:18 +00002241
Jim Grosbache8606dc2011-07-13 17:50:29 +00002242 // The source register for the shift has already been added to the
2243 // operand list, so we need to pop it off and combine it into the shifted
2244 // register operand instead.
Benjamin Kramereac07962011-07-14 18:41:22 +00002245 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
Jim Grosbache8606dc2011-07-13 17:50:29 +00002246 if (!PrevOp->isReg())
2247 return Error(PrevOp->getStartLoc(), "shift must be of a register");
2248 int SrcReg = PrevOp->getReg();
2249 int64_t Imm = 0;
2250 int ShiftReg = 0;
2251 if (ShiftTy == ARM_AM::rrx) {
2252 // RRX Doesn't have an explicit shift amount. The encoder expects
2253 // the shift register to be the same as the source register. Seems odd,
2254 // but OK.
2255 ShiftReg = SrcReg;
2256 } else {
2257 // Figure out if this is shifted by a constant or a register (for non-RRX).
2258 if (Parser.getTok().is(AsmToken::Hash)) {
2259 Parser.Lex(); // Eat hash.
2260 SMLoc ImmLoc = Parser.getTok().getLoc();
2261 const MCExpr *ShiftExpr = 0;
Jim Grosbach19906722011-07-13 18:49:30 +00002262 if (getParser().ParseExpression(ShiftExpr)) {
2263 Error(ImmLoc, "invalid immediate shift value");
2264 return -1;
2265 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00002266 // The expression must be evaluatable as an immediate.
2267 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
Jim Grosbach19906722011-07-13 18:49:30 +00002268 if (!CE) {
2269 Error(ImmLoc, "invalid immediate shift value");
2270 return -1;
2271 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00002272 // Range check the immediate.
2273 // lsl, ror: 0 <= imm <= 31
2274 // lsr, asr: 0 <= imm <= 32
2275 Imm = CE->getValue();
2276 if (Imm < 0 ||
2277 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
2278 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
Jim Grosbach19906722011-07-13 18:49:30 +00002279 Error(ImmLoc, "immediate shift value out of range");
2280 return -1;
Jim Grosbache8606dc2011-07-13 17:50:29 +00002281 }
2282 } else if (Parser.getTok().is(AsmToken::Identifier)) {
Jim Grosbach1355cf12011-07-26 17:10:22 +00002283 ShiftReg = tryParseRegister();
Jim Grosbache8606dc2011-07-13 17:50:29 +00002284 SMLoc L = Parser.getTok().getLoc();
Jim Grosbach19906722011-07-13 18:49:30 +00002285 if (ShiftReg == -1) {
2286 Error (L, "expected immediate or register in shift operand");
2287 return -1;
2288 }
2289 } else {
2290 Error (Parser.getTok().getLoc(),
Jim Grosbache8606dc2011-07-13 17:50:29 +00002291 "expected immediate or register in shift operand");
Jim Grosbach19906722011-07-13 18:49:30 +00002292 return -1;
2293 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00002294 }
2295
Owen Anderson92a20222011-07-21 18:54:16 +00002296 if (ShiftReg && ShiftTy != ARM_AM::rrx)
2297 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
Jim Grosbachaf6981f2011-07-25 20:49:51 +00002298 ShiftReg, Imm,
Owen Anderson00828302011-03-18 22:50:18 +00002299 S, Parser.getTok().getLoc()));
Owen Anderson92a20222011-07-21 18:54:16 +00002300 else
2301 Operands.push_back(ARMOperand::CreateShiftedImmediate(ShiftTy, SrcReg, Imm,
2302 S, Parser.getTok().getLoc()));
Owen Anderson00828302011-03-18 22:50:18 +00002303
Jim Grosbach19906722011-07-13 18:49:30 +00002304 return 0;
Owen Anderson00828302011-03-18 22:50:18 +00002305}
2306
2307
Bill Wendling50d0f582010-11-18 23:43:05 +00002308/// Try to parse a register name. The token must be an Identifier when called.
2309/// If it's a register, an AsmOperand is created. Another AsmOperand is created
2310/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +00002311///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002312/// TODO this is likely to change to allow different register types and or to
2313/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +00002314bool ARMAsmParser::
Jim Grosbach1355cf12011-07-26 17:10:22 +00002315tryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00002316 SMLoc S = Parser.getTok().getLoc();
Jim Grosbach1355cf12011-07-26 17:10:22 +00002317 int RegNo = tryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +00002318 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +00002319 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +00002320
Bill Wendling50d0f582010-11-18 23:43:05 +00002321 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002322
Chris Lattnere5658fa2010-10-30 04:09:10 +00002323 const AsmToken &ExclaimTok = Parser.getTok();
2324 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00002325 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
2326 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +00002327 Parser.Lex(); // Eat exclaim token
Jim Grosbach460a9052011-10-07 23:56:00 +00002328 return false;
2329 }
2330
2331 // Also check for an index operand. This is only legal for vector registers,
2332 // but that'll get caught OK in operand matching, so we don't need to
2333 // explicitly filter everything else out here.
2334 if (Parser.getTok().is(AsmToken::LBrac)) {
2335 SMLoc SIdx = Parser.getTok().getLoc();
2336 Parser.Lex(); // Eat left bracket token.
2337
2338 const MCExpr *ImmVal;
Jim Grosbach460a9052011-10-07 23:56:00 +00002339 if (getParser().ParseExpression(ImmVal))
2340 return MatchOperand_ParseFail;
2341 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(ImmVal);
2342 if (!MCE) {
2343 TokError("immediate value expected for vector index");
2344 return MatchOperand_ParseFail;
2345 }
2346
2347 SMLoc E = Parser.getTok().getLoc();
2348 if (Parser.getTok().isNot(AsmToken::RBrac)) {
2349 Error(E, "']' expected");
2350 return MatchOperand_ParseFail;
2351 }
2352
2353 Parser.Lex(); // Eat right bracket token.
2354
2355 Operands.push_back(ARMOperand::CreateVectorIndex(MCE->getValue(),
2356 SIdx, E,
2357 getContext()));
Kevin Enderby99e6d4e2009-10-07 18:01:35 +00002358 }
2359
Bill Wendling50d0f582010-11-18 23:43:05 +00002360 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002361}
2362
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002363/// MatchCoprocessorOperandName - Try to parse an coprocessor related
2364/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
2365/// "c5", ...
2366static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002367 // Use the same layout as the tablegen'erated register name matcher. Ugly,
2368 // but efficient.
2369 switch (Name.size()) {
2370 default: break;
2371 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002372 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002373 return -1;
2374 switch (Name[1]) {
2375 default: return -1;
2376 case '0': return 0;
2377 case '1': return 1;
2378 case '2': return 2;
2379 case '3': return 3;
2380 case '4': return 4;
2381 case '5': return 5;
2382 case '6': return 6;
2383 case '7': return 7;
2384 case '8': return 8;
2385 case '9': return 9;
2386 }
2387 break;
2388 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002389 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002390 return -1;
2391 switch (Name[2]) {
2392 default: return -1;
2393 case '0': return 10;
2394 case '1': return 11;
2395 case '2': return 12;
2396 case '3': return 13;
2397 case '4': return 14;
2398 case '5': return 15;
2399 }
2400 break;
2401 }
2402
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002403 return -1;
2404}
2405
Jim Grosbach89df9962011-08-26 21:43:41 +00002406/// parseITCondCode - Try to parse a condition code for an IT instruction.
2407ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2408parseITCondCode(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2409 SMLoc S = Parser.getTok().getLoc();
2410 const AsmToken &Tok = Parser.getTok();
2411 if (!Tok.is(AsmToken::Identifier))
2412 return MatchOperand_NoMatch;
2413 unsigned CC = StringSwitch<unsigned>(Tok.getString())
2414 .Case("eq", ARMCC::EQ)
2415 .Case("ne", ARMCC::NE)
2416 .Case("hs", ARMCC::HS)
2417 .Case("cs", ARMCC::HS)
2418 .Case("lo", ARMCC::LO)
2419 .Case("cc", ARMCC::LO)
2420 .Case("mi", ARMCC::MI)
2421 .Case("pl", ARMCC::PL)
2422 .Case("vs", ARMCC::VS)
2423 .Case("vc", ARMCC::VC)
2424 .Case("hi", ARMCC::HI)
2425 .Case("ls", ARMCC::LS)
2426 .Case("ge", ARMCC::GE)
2427 .Case("lt", ARMCC::LT)
2428 .Case("gt", ARMCC::GT)
2429 .Case("le", ARMCC::LE)
2430 .Case("al", ARMCC::AL)
2431 .Default(~0U);
2432 if (CC == ~0U)
2433 return MatchOperand_NoMatch;
2434 Parser.Lex(); // Eat the token.
2435
2436 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), S));
2437
2438 return MatchOperand_Success;
2439}
2440
Jim Grosbach43904292011-07-25 20:14:50 +00002441/// parseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002442/// token must be an Identifier when called, and if it is a coprocessor
2443/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00002444ARMAsmParser::OperandMatchResultTy ARMAsmParser::
Jim Grosbach43904292011-07-25 20:14:50 +00002445parseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002446 SMLoc S = Parser.getTok().getLoc();
2447 const AsmToken &Tok = Parser.getTok();
Jim Grosbachc66e7af2011-10-12 20:54:17 +00002448 if (Tok.isNot(AsmToken::Identifier))
2449 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002450
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002451 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002452 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00002453 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002454
2455 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002456 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00002457 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002458}
2459
Jim Grosbach43904292011-07-25 20:14:50 +00002460/// parseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002461/// token must be an Identifier when called, and if it is a coprocessor
2462/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00002463ARMAsmParser::OperandMatchResultTy ARMAsmParser::
Jim Grosbach43904292011-07-25 20:14:50 +00002464parseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002465 SMLoc S = Parser.getTok().getLoc();
2466 const AsmToken &Tok = Parser.getTok();
Jim Grosbachc66e7af2011-10-12 20:54:17 +00002467 if (Tok.isNot(AsmToken::Identifier))
2468 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002469
2470 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
2471 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00002472 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002473
2474 Parser.Lex(); // Eat identifier token.
2475 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00002476 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00002477}
2478
Jim Grosbach9b8f2a02011-10-12 17:34:41 +00002479/// parseCoprocOptionOperand - Try to parse an coprocessor option operand.
2480/// coproc_option : '{' imm0_255 '}'
2481ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2482parseCoprocOptionOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2483 SMLoc S = Parser.getTok().getLoc();
2484
2485 // If this isn't a '{', this isn't a coprocessor immediate operand.
2486 if (Parser.getTok().isNot(AsmToken::LCurly))
2487 return MatchOperand_NoMatch;
2488 Parser.Lex(); // Eat the '{'
2489
2490 const MCExpr *Expr;
2491 SMLoc Loc = Parser.getTok().getLoc();
2492 if (getParser().ParseExpression(Expr)) {
2493 Error(Loc, "illegal expression");
2494 return MatchOperand_ParseFail;
2495 }
2496 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
2497 if (!CE || CE->getValue() < 0 || CE->getValue() > 255) {
2498 Error(Loc, "coprocessor option must be an immediate in range [0, 255]");
2499 return MatchOperand_ParseFail;
2500 }
2501 int Val = CE->getValue();
2502
2503 // Check for and consume the closing '}'
2504 if (Parser.getTok().isNot(AsmToken::RCurly))
2505 return MatchOperand_ParseFail;
2506 SMLoc E = Parser.getTok().getLoc();
2507 Parser.Lex(); // Eat the '}'
2508
2509 Operands.push_back(ARMOperand::CreateCoprocOption(Val, S, E));
2510 return MatchOperand_Success;
2511}
2512
Jim Grosbachd0588e22011-09-14 18:08:35 +00002513// For register list parsing, we need to map from raw GPR register numbering
2514// to the enumeration values. The enumeration values aren't sorted by
2515// register number due to our using "sp", "lr" and "pc" as canonical names.
2516static unsigned getNextRegister(unsigned Reg) {
2517 // If this is a GPR, we need to do it manually, otherwise we can rely
2518 // on the sort ordering of the enumeration since the other reg-classes
2519 // are sane.
2520 if (!ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2521 return Reg + 1;
2522 switch(Reg) {
2523 default: assert(0 && "Invalid GPR number!");
2524 case ARM::R0: return ARM::R1; case ARM::R1: return ARM::R2;
2525 case ARM::R2: return ARM::R3; case ARM::R3: return ARM::R4;
2526 case ARM::R4: return ARM::R5; case ARM::R5: return ARM::R6;
2527 case ARM::R6: return ARM::R7; case ARM::R7: return ARM::R8;
2528 case ARM::R8: return ARM::R9; case ARM::R9: return ARM::R10;
2529 case ARM::R10: return ARM::R11; case ARM::R11: return ARM::R12;
2530 case ARM::R12: return ARM::SP; case ARM::SP: return ARM::LR;
2531 case ARM::LR: return ARM::PC; case ARM::PC: return ARM::R0;
2532 }
2533}
2534
Jim Grosbachce485e72011-11-11 21:27:40 +00002535// Return the low-subreg of a given Q register.
2536static unsigned getDRegFromQReg(unsigned QReg) {
2537 switch (QReg) {
2538 default: llvm_unreachable("expected a Q register!");
2539 case ARM::Q0: return ARM::D0;
2540 case ARM::Q1: return ARM::D2;
2541 case ARM::Q2: return ARM::D4;
2542 case ARM::Q3: return ARM::D6;
2543 case ARM::Q4: return ARM::D8;
2544 case ARM::Q5: return ARM::D10;
2545 case ARM::Q6: return ARM::D12;
2546 case ARM::Q7: return ARM::D14;
2547 case ARM::Q8: return ARM::D16;
Jim Grosbach25e0a872011-11-15 21:01:30 +00002548 case ARM::Q9: return ARM::D18;
Jim Grosbachce485e72011-11-11 21:27:40 +00002549 case ARM::Q10: return ARM::D20;
2550 case ARM::Q11: return ARM::D22;
2551 case ARM::Q12: return ARM::D24;
2552 case ARM::Q13: return ARM::D26;
2553 case ARM::Q14: return ARM::D28;
2554 case ARM::Q15: return ARM::D30;
2555 }
2556}
2557
Jim Grosbachd0588e22011-09-14 18:08:35 +00002558/// Parse a register list.
Bill Wendling50d0f582010-11-18 23:43:05 +00002559bool ARMAsmParser::
Jim Grosbach1355cf12011-07-26 17:10:22 +00002560parseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00002561 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00002562 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00002563 SMLoc S = Parser.getTok().getLoc();
Jim Grosbachd0588e22011-09-14 18:08:35 +00002564 Parser.Lex(); // Eat '{' token.
2565 SMLoc RegLoc = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00002566
Jim Grosbachd0588e22011-09-14 18:08:35 +00002567 // Check the first register in the list to see what register class
2568 // this is a list of.
2569 int Reg = tryParseRegister();
2570 if (Reg == -1)
2571 return Error(RegLoc, "register expected");
2572
Jim Grosbachce485e72011-11-11 21:27:40 +00002573 // The reglist instructions have at most 16 registers, so reserve
2574 // space for that many.
2575 SmallVector<std::pair<unsigned, SMLoc>, 16> Registers;
2576
2577 // Allow Q regs and just interpret them as the two D sub-registers.
2578 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2579 Reg = getDRegFromQReg(Reg);
2580 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2581 ++Reg;
2582 }
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00002583 const MCRegisterClass *RC;
Jim Grosbachd0588e22011-09-14 18:08:35 +00002584 if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
2585 RC = &ARMMCRegisterClasses[ARM::GPRRegClassID];
2586 else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg))
2587 RC = &ARMMCRegisterClasses[ARM::DPRRegClassID];
2588 else if (ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg))
2589 RC = &ARMMCRegisterClasses[ARM::SPRRegClassID];
2590 else
2591 return Error(RegLoc, "invalid register in register list");
2592
Jim Grosbachce485e72011-11-11 21:27:40 +00002593 // Store the register.
Jim Grosbachd0588e22011-09-14 18:08:35 +00002594 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
Kevin Enderbyd7894f12009-10-09 21:12:28 +00002595
Jim Grosbachd0588e22011-09-14 18:08:35 +00002596 // This starts immediately after the first register token in the list,
2597 // so we can see either a comma or a minus (range separator) as a legal
2598 // next token.
2599 while (Parser.getTok().is(AsmToken::Comma) ||
2600 Parser.getTok().is(AsmToken::Minus)) {
2601 if (Parser.getTok().is(AsmToken::Minus)) {
Jim Grosbache43862b2011-11-15 23:19:15 +00002602 Parser.Lex(); // Eat the minus.
Jim Grosbachd0588e22011-09-14 18:08:35 +00002603 SMLoc EndLoc = Parser.getTok().getLoc();
2604 int EndReg = tryParseRegister();
2605 if (EndReg == -1)
2606 return Error(EndLoc, "register expected");
Jim Grosbachce485e72011-11-11 21:27:40 +00002607 // Allow Q regs and just interpret them as the two D sub-registers.
2608 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
2609 EndReg = getDRegFromQReg(EndReg) + 1;
Jim Grosbachd0588e22011-09-14 18:08:35 +00002610 // If the register is the same as the start reg, there's nothing
2611 // more to do.
2612 if (Reg == EndReg)
2613 continue;
2614 // The register must be in the same register class as the first.
2615 if (!RC->contains(EndReg))
2616 return Error(EndLoc, "invalid register in register list");
2617 // Ranges must go from low to high.
2618 if (getARMRegisterNumbering(Reg) > getARMRegisterNumbering(EndReg))
2619 return Error(EndLoc, "bad range in register list");
Kevin Enderbyd7894f12009-10-09 21:12:28 +00002620
Jim Grosbachd0588e22011-09-14 18:08:35 +00002621 // Add all the registers in the range to the register list.
2622 while (Reg != EndReg) {
2623 Reg = getNextRegister(Reg);
2624 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
2625 }
2626 continue;
2627 }
2628 Parser.Lex(); // Eat the comma.
2629 RegLoc = Parser.getTok().getLoc();
2630 int OldReg = Reg;
Jim Grosbacha62d11e2011-12-08 21:34:20 +00002631 const AsmToken RegTok = Parser.getTok();
Jim Grosbachd0588e22011-09-14 18:08:35 +00002632 Reg = tryParseRegister();
2633 if (Reg == -1)
Jim Grosbach2d539692011-09-12 23:36:42 +00002634 return Error(RegLoc, "register expected");
Jim Grosbachce485e72011-11-11 21:27:40 +00002635 // Allow Q regs and just interpret them as the two D sub-registers.
2636 bool isQReg = false;
2637 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2638 Reg = getDRegFromQReg(Reg);
2639 isQReg = true;
2640 }
Jim Grosbachd0588e22011-09-14 18:08:35 +00002641 // The register must be in the same register class as the first.
2642 if (!RC->contains(Reg))
2643 return Error(RegLoc, "invalid register in register list");
2644 // List must be monotonically increasing.
Jim Grosbacha62d11e2011-12-08 21:34:20 +00002645 if (getARMRegisterNumbering(Reg) < getARMRegisterNumbering(OldReg))
Jim Grosbachd0588e22011-09-14 18:08:35 +00002646 return Error(RegLoc, "register list not in ascending order");
Jim Grosbacha62d11e2011-12-08 21:34:20 +00002647 if (getARMRegisterNumbering(Reg) == getARMRegisterNumbering(OldReg)) {
2648 Warning(RegLoc, "duplicated register (" + RegTok.getString() +
2649 ") in register list");
2650 continue;
2651 }
Jim Grosbachd0588e22011-09-14 18:08:35 +00002652 // VFP register lists must also be contiguous.
2653 // It's OK to use the enumeration values directly here rather, as the
2654 // VFP register classes have the enum sorted properly.
2655 if (RC != &ARMMCRegisterClasses[ARM::GPRRegClassID] &&
2656 Reg != OldReg + 1)
2657 return Error(RegLoc, "non-contiguous register range");
2658 Registers.push_back(std::pair<unsigned, SMLoc>(Reg, RegLoc));
Jim Grosbachce485e72011-11-11 21:27:40 +00002659 if (isQReg)
2660 Registers.push_back(std::pair<unsigned, SMLoc>(++Reg, RegLoc));
Bill Wendlinge7176102010-11-06 22:36:58 +00002661 }
2662
Jim Grosbachd0588e22011-09-14 18:08:35 +00002663 SMLoc E = Parser.getTok().getLoc();
2664 if (Parser.getTok().isNot(AsmToken::RCurly))
2665 return Error(E, "'}' expected");
2666 Parser.Lex(); // Eat '}' token.
2667
Bill Wendling50d0f582010-11-18 23:43:05 +00002668 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
2669 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00002670}
2671
Jim Grosbach98b05a52011-11-30 01:09:44 +00002672// Helper function to parse the lane index for vector lists.
2673ARMAsmParser::OperandMatchResultTy ARMAsmParser::
Jim Grosbach7636bf62011-12-02 00:35:16 +00002674parseVectorLane(VectorLaneTy &LaneKind, unsigned &Index) {
2675 Index = 0; // Always return a defined index value.
Jim Grosbach98b05a52011-11-30 01:09:44 +00002676 if (Parser.getTok().is(AsmToken::LBrac)) {
2677 Parser.Lex(); // Eat the '['.
2678 if (Parser.getTok().is(AsmToken::RBrac)) {
2679 // "Dn[]" is the 'all lanes' syntax.
2680 LaneKind = AllLanes;
2681 Parser.Lex(); // Eat the ']'.
2682 return MatchOperand_Success;
2683 }
Jim Grosbach7636bf62011-12-02 00:35:16 +00002684 if (Parser.getTok().is(AsmToken::Integer)) {
2685 int64_t Val = Parser.getTok().getIntVal();
2686 // Make this range check context sensitive for .8, .16, .32.
2687 if (Val < 0 && Val > 7)
2688 Error(Parser.getTok().getLoc(), "lane index out of range");
2689 Index = Val;
2690 LaneKind = IndexedLane;
2691 Parser.Lex(); // Eat the token;
2692 if (Parser.getTok().isNot(AsmToken::RBrac))
2693 Error(Parser.getTok().getLoc(), "']' expected");
2694 Parser.Lex(); // Eat the ']'.
2695 return MatchOperand_Success;
2696 }
2697 Error(Parser.getTok().getLoc(), "lane index must be empty or an integer");
Jim Grosbach98b05a52011-11-30 01:09:44 +00002698 return MatchOperand_ParseFail;
2699 }
2700 LaneKind = NoLanes;
2701 return MatchOperand_Success;
2702}
2703
Jim Grosbach862019c2011-10-18 23:02:30 +00002704// parse a vector register list
2705ARMAsmParser::OperandMatchResultTy ARMAsmParser::
2706parseVectorList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Jim Grosbach98b05a52011-11-30 01:09:44 +00002707 VectorLaneTy LaneKind;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002708 unsigned LaneIndex;
Jim Grosbach5c984e42011-11-15 21:45:55 +00002709 SMLoc S = Parser.getTok().getLoc();
2710 // As an extension (to match gas), support a plain D register or Q register
2711 // (without encosing curly braces) as a single or double entry list,
2712 // respectively.
2713 if (Parser.getTok().is(AsmToken::Identifier)) {
2714 int Reg = tryParseRegister();
2715 if (Reg == -1)
2716 return MatchOperand_NoMatch;
2717 SMLoc E = Parser.getTok().getLoc();
2718 if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg)) {
Jim Grosbach7636bf62011-12-02 00:35:16 +00002719 OperandMatchResultTy Res = parseVectorLane(LaneKind, LaneIndex);
Jim Grosbach98b05a52011-11-30 01:09:44 +00002720 if (Res != MatchOperand_Success)
2721 return Res;
2722 switch (LaneKind) {
2723 default:
2724 assert(0 && "unexpected lane kind!");
2725 case NoLanes:
2726 E = Parser.getTok().getLoc();
2727 Operands.push_back(ARMOperand::CreateVectorList(Reg, 1, S, E));
2728 break;
2729 case AllLanes:
2730 E = Parser.getTok().getLoc();
2731 Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 1, S, E));
2732 break;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002733 case IndexedLane:
2734 Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 1,
2735 LaneIndex, S,E));
2736 break;
Jim Grosbach98b05a52011-11-30 01:09:44 +00002737 }
Jim Grosbach5c984e42011-11-15 21:45:55 +00002738 return MatchOperand_Success;
2739 }
2740 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2741 Reg = getDRegFromQReg(Reg);
Jim Grosbach7636bf62011-12-02 00:35:16 +00002742 OperandMatchResultTy Res = parseVectorLane(LaneKind, LaneIndex);
Jim Grosbach98b05a52011-11-30 01:09:44 +00002743 if (Res != MatchOperand_Success)
2744 return Res;
2745 switch (LaneKind) {
2746 default:
2747 assert(0 && "unexpected lane kind!");
2748 case NoLanes:
2749 E = Parser.getTok().getLoc();
2750 Operands.push_back(ARMOperand::CreateVectorList(Reg, 2, S, E));
2751 break;
2752 case AllLanes:
2753 E = Parser.getTok().getLoc();
2754 Operands.push_back(ARMOperand::CreateVectorListAllLanes(Reg, 2, S, E));
2755 break;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002756 case IndexedLane:
2757 Operands.push_back(ARMOperand::CreateVectorListIndexed(Reg, 2,
2758 LaneIndex, S,E));
2759 break;
Jim Grosbach98b05a52011-11-30 01:09:44 +00002760 }
Jim Grosbach5c984e42011-11-15 21:45:55 +00002761 return MatchOperand_Success;
2762 }
2763 Error(S, "vector register expected");
2764 return MatchOperand_ParseFail;
2765 }
2766
2767 if (Parser.getTok().isNot(AsmToken::LCurly))
Jim Grosbach862019c2011-10-18 23:02:30 +00002768 return MatchOperand_NoMatch;
2769
Jim Grosbach862019c2011-10-18 23:02:30 +00002770 Parser.Lex(); // Eat '{' token.
2771 SMLoc RegLoc = Parser.getTok().getLoc();
2772
2773 int Reg = tryParseRegister();
2774 if (Reg == -1) {
2775 Error(RegLoc, "register expected");
2776 return MatchOperand_ParseFail;
2777 }
Jim Grosbach862019c2011-10-18 23:02:30 +00002778 unsigned Count = 1;
Jim Grosbachc73d73e2011-10-28 00:06:50 +00002779 unsigned FirstReg = Reg;
2780 // The list is of D registers, but we also allow Q regs and just interpret
2781 // them as the two D sub-registers.
2782 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2783 FirstReg = Reg = getDRegFromQReg(Reg);
2784 ++Reg;
2785 ++Count;
2786 }
Jim Grosbach7636bf62011-12-02 00:35:16 +00002787 if (parseVectorLane(LaneKind, LaneIndex) != MatchOperand_Success)
Jim Grosbach98b05a52011-11-30 01:09:44 +00002788 return MatchOperand_ParseFail;
Jim Grosbachc73d73e2011-10-28 00:06:50 +00002789
Jim Grosbache43862b2011-11-15 23:19:15 +00002790 while (Parser.getTok().is(AsmToken::Comma) ||
2791 Parser.getTok().is(AsmToken::Minus)) {
2792 if (Parser.getTok().is(AsmToken::Minus)) {
2793 Parser.Lex(); // Eat the minus.
2794 SMLoc EndLoc = Parser.getTok().getLoc();
2795 int EndReg = tryParseRegister();
2796 if (EndReg == -1) {
2797 Error(EndLoc, "register expected");
2798 return MatchOperand_ParseFail;
2799 }
2800 // Allow Q regs and just interpret them as the two D sub-registers.
2801 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(EndReg))
2802 EndReg = getDRegFromQReg(EndReg) + 1;
2803 // If the register is the same as the start reg, there's nothing
2804 // more to do.
2805 if (Reg == EndReg)
2806 continue;
2807 // The register must be in the same register class as the first.
2808 if (!ARMMCRegisterClasses[ARM::DPRRegClassID].contains(EndReg)) {
2809 Error(EndLoc, "invalid register in register list");
2810 return MatchOperand_ParseFail;
2811 }
2812 // Ranges must go from low to high.
2813 if (Reg > EndReg) {
2814 Error(EndLoc, "bad range in register list");
2815 return MatchOperand_ParseFail;
2816 }
Jim Grosbach98b05a52011-11-30 01:09:44 +00002817 // Parse the lane specifier if present.
2818 VectorLaneTy NextLaneKind;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002819 unsigned NextLaneIndex;
2820 if (parseVectorLane(NextLaneKind, NextLaneIndex) != MatchOperand_Success)
Jim Grosbach98b05a52011-11-30 01:09:44 +00002821 return MatchOperand_ParseFail;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002822 if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
Jim Grosbach98b05a52011-11-30 01:09:44 +00002823 Error(EndLoc, "mismatched lane index in register list");
2824 return MatchOperand_ParseFail;
2825 }
2826 EndLoc = Parser.getTok().getLoc();
Jim Grosbache43862b2011-11-15 23:19:15 +00002827
2828 // Add all the registers in the range to the register list.
2829 Count += EndReg - Reg;
2830 Reg = EndReg;
2831 continue;
2832 }
Jim Grosbach862019c2011-10-18 23:02:30 +00002833 Parser.Lex(); // Eat the comma.
2834 RegLoc = Parser.getTok().getLoc();
2835 int OldReg = Reg;
2836 Reg = tryParseRegister();
2837 if (Reg == -1) {
2838 Error(RegLoc, "register expected");
2839 return MatchOperand_ParseFail;
2840 }
Jim Grosbachc73d73e2011-10-28 00:06:50 +00002841 // vector register lists must be contiguous.
Jim Grosbach862019c2011-10-18 23:02:30 +00002842 // It's OK to use the enumeration values directly here rather, as the
2843 // VFP register classes have the enum sorted properly.
Jim Grosbachc73d73e2011-10-28 00:06:50 +00002844 //
2845 // The list is of D registers, but we also allow Q regs and just interpret
2846 // them as the two D sub-registers.
2847 if (ARMMCRegisterClasses[ARM::QPRRegClassID].contains(Reg)) {
2848 Reg = getDRegFromQReg(Reg);
2849 if (Reg != OldReg + 1) {
2850 Error(RegLoc, "non-contiguous register range");
2851 return MatchOperand_ParseFail;
2852 }
2853 ++Reg;
2854 Count += 2;
Jim Grosbach98b05a52011-11-30 01:09:44 +00002855 // Parse the lane specifier if present.
2856 VectorLaneTy NextLaneKind;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002857 unsigned NextLaneIndex;
Jim Grosbach98b05a52011-11-30 01:09:44 +00002858 SMLoc EndLoc = Parser.getTok().getLoc();
Jim Grosbach7636bf62011-12-02 00:35:16 +00002859 if (parseVectorLane(NextLaneKind, NextLaneIndex) != MatchOperand_Success)
Jim Grosbach98b05a52011-11-30 01:09:44 +00002860 return MatchOperand_ParseFail;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002861 if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
Jim Grosbach98b05a52011-11-30 01:09:44 +00002862 Error(EndLoc, "mismatched lane index in register list");
2863 return MatchOperand_ParseFail;
2864 }
Jim Grosbachc73d73e2011-10-28 00:06:50 +00002865 continue;
2866 }
2867 // Normal D register. Just check that it's contiguous and keep going.
Jim Grosbach862019c2011-10-18 23:02:30 +00002868 if (Reg != OldReg + 1) {
2869 Error(RegLoc, "non-contiguous register range");
2870 return MatchOperand_ParseFail;
2871 }
Jim Grosbach862019c2011-10-18 23:02:30 +00002872 ++Count;
Jim Grosbach98b05a52011-11-30 01:09:44 +00002873 // Parse the lane specifier if present.
2874 VectorLaneTy NextLaneKind;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002875 unsigned NextLaneIndex;
Jim Grosbach98b05a52011-11-30 01:09:44 +00002876 SMLoc EndLoc = Parser.getTok().getLoc();
Jim Grosbach7636bf62011-12-02 00:35:16 +00002877 if (parseVectorLane(NextLaneKind, NextLaneIndex) != MatchOperand_Success)
Jim Grosbach98b05a52011-11-30 01:09:44 +00002878 return MatchOperand_ParseFail;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002879 if (NextLaneKind != LaneKind || LaneIndex != NextLaneIndex) {
Jim Grosbach98b05a52011-11-30 01:09:44 +00002880 Error(EndLoc, "mismatched lane index in register list");
2881 return MatchOperand_ParseFail;
2882 }
Jim Grosbach862019c2011-10-18 23:02:30 +00002883 }
2884
2885 SMLoc E = Parser.getTok().getLoc();
2886 if (Parser.getTok().isNot(AsmToken::RCurly)) {
2887 Error(E, "'}' expected");
2888 return MatchOperand_ParseFail;
2889 }
2890 Parser.Lex(); // Eat '}' token.
2891
Jim Grosbach98b05a52011-11-30 01:09:44 +00002892 switch (LaneKind) {
2893 default:
2894 assert(0 && "unexpected lane kind in register list.");
2895 case NoLanes:
2896 Operands.push_back(ARMOperand::CreateVectorList(FirstReg, Count, S, E));
2897 break;
2898 case AllLanes:
2899 Operands.push_back(ARMOperand::CreateVectorListAllLanes(FirstReg, Count,
2900 S, E));
2901 break;
Jim Grosbach7636bf62011-12-02 00:35:16 +00002902 case IndexedLane:
2903 Operands.push_back(ARMOperand::CreateVectorListIndexed(FirstReg, Count,
2904 LaneIndex, S, E));
2905 break;
Jim Grosbach98b05a52011-11-30 01:09:44 +00002906 }
Jim Grosbach862019c2011-10-18 23:02:30 +00002907 return MatchOperand_Success;
2908}
2909
Jim Grosbach43904292011-07-25 20:14:50 +00002910/// parseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
Jim Grosbachf922c472011-02-12 01:34:40 +00002911ARMAsmParser::OperandMatchResultTy ARMAsmParser::
Jim Grosbach43904292011-07-25 20:14:50 +00002912parseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002913 SMLoc S = Parser.getTok().getLoc();
2914 const AsmToken &Tok = Parser.getTok();
2915 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2916 StringRef OptStr = Tok.getString();
2917
2918 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
2919 .Case("sy", ARM_MB::SY)
2920 .Case("st", ARM_MB::ST)
Jim Grosbach032434d2011-07-13 23:40:38 +00002921 .Case("sh", ARM_MB::ISH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002922 .Case("ish", ARM_MB::ISH)
Jim Grosbach032434d2011-07-13 23:40:38 +00002923 .Case("shst", ARM_MB::ISHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002924 .Case("ishst", ARM_MB::ISHST)
2925 .Case("nsh", ARM_MB::NSH)
Jim Grosbach032434d2011-07-13 23:40:38 +00002926 .Case("un", ARM_MB::NSH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002927 .Case("nshst", ARM_MB::NSHST)
Jim Grosbach032434d2011-07-13 23:40:38 +00002928 .Case("unst", ARM_MB::NSHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002929 .Case("osh", ARM_MB::OSH)
2930 .Case("oshst", ARM_MB::OSHST)
2931 .Default(~0U);
2932
2933 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00002934 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002935
2936 Parser.Lex(); // Eat identifier token.
2937 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00002938 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00002939}
2940
Jim Grosbach43904292011-07-25 20:14:50 +00002941/// parseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002942ARMAsmParser::OperandMatchResultTy ARMAsmParser::
Jim Grosbach43904292011-07-25 20:14:50 +00002943parseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002944 SMLoc S = Parser.getTok().getLoc();
2945 const AsmToken &Tok = Parser.getTok();
2946 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2947 StringRef IFlagsStr = Tok.getString();
2948
Owen Anderson2dbb46a2011-10-05 17:16:40 +00002949 // An iflags string of "none" is interpreted to mean that none of the AIF
2950 // bits are set. Not a terribly useful instruction, but a valid encoding.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002951 unsigned IFlags = 0;
Owen Anderson2dbb46a2011-10-05 17:16:40 +00002952 if (IFlagsStr != "none") {
2953 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
2954 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
2955 .Case("a", ARM_PROC::A)
2956 .Case("i", ARM_PROC::I)
2957 .Case("f", ARM_PROC::F)
2958 .Default(~0U);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002959
Owen Anderson2dbb46a2011-10-05 17:16:40 +00002960 // If some specific iflag is already set, it means that some letter is
2961 // present more than once, this is not acceptable.
2962 if (Flag == ~0U || (IFlags & Flag))
2963 return MatchOperand_NoMatch;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002964
Owen Anderson2dbb46a2011-10-05 17:16:40 +00002965 IFlags |= Flag;
2966 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002967 }
2968
2969 Parser.Lex(); // Eat identifier token.
2970 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
2971 return MatchOperand_Success;
2972}
2973
Jim Grosbach43904292011-07-25 20:14:50 +00002974/// parseMSRMaskOperand - Try to parse mask flags from MSR instruction.
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002975ARMAsmParser::OperandMatchResultTy ARMAsmParser::
Jim Grosbach43904292011-07-25 20:14:50 +00002976parseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00002977 SMLoc S = Parser.getTok().getLoc();
2978 const AsmToken &Tok = Parser.getTok();
2979 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
2980 StringRef Mask = Tok.getString();
2981
James Molloyacad68d2011-09-28 14:21:38 +00002982 if (isMClass()) {
2983 // See ARMv6-M 10.1.1
2984 unsigned FlagsVal = StringSwitch<unsigned>(Mask)
2985 .Case("apsr", 0)
2986 .Case("iapsr", 1)
2987 .Case("eapsr", 2)
2988 .Case("xpsr", 3)
2989 .Case("ipsr", 5)
2990 .Case("epsr", 6)
2991 .Case("iepsr", 7)
2992 .Case("msp", 8)
2993 .Case("psp", 9)
2994 .Case("primask", 16)
2995 .Case("basepri", 17)
2996 .Case("basepri_max", 18)
2997 .Case("faultmask", 19)
2998 .Case("control", 20)
2999 .Default(~0U);
3000
3001 if (FlagsVal == ~0U)
3002 return MatchOperand_NoMatch;
3003
3004 if (!hasV7Ops() && FlagsVal >= 17 && FlagsVal <= 19)
3005 // basepri, basepri_max and faultmask only valid for V7m.
3006 return MatchOperand_NoMatch;
3007
3008 Parser.Lex(); // Eat identifier token.
3009 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
3010 return MatchOperand_Success;
3011 }
3012
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00003013 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
3014 size_t Start = 0, Next = Mask.find('_');
3015 StringRef Flags = "";
Benjamin Kramer59085362011-11-06 20:37:06 +00003016 std::string SpecReg = Mask.slice(Start, Next).lower();
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00003017 if (Next != StringRef::npos)
3018 Flags = Mask.slice(Next+1, Mask.size());
3019
3020 // FlagsVal contains the complete mask:
3021 // 3-0: Mask
3022 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
3023 unsigned FlagsVal = 0;
3024
3025 if (SpecReg == "apsr") {
3026 FlagsVal = StringSwitch<unsigned>(Flags)
Jim Grosbachb29b4dd2011-07-19 22:45:10 +00003027 .Case("nzcvq", 0x8) // same as CPSR_f
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00003028 .Case("g", 0x4) // same as CPSR_s
3029 .Case("nzcvqg", 0xc) // same as CPSR_fs
3030 .Default(~0U);
3031
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00003032 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00003033 if (!Flags.empty())
3034 return MatchOperand_NoMatch;
3035 else
Jim Grosbachbf841cf2011-09-14 20:03:46 +00003036 FlagsVal = 8; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00003037 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00003038 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00003039 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
3040 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00003041 for (int i = 0, e = Flags.size(); i != e; ++i) {
3042 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
3043 .Case("c", 1)
3044 .Case("x", 2)
3045 .Case("s", 4)
3046 .Case("f", 8)
3047 .Default(~0U);
3048
3049 // If some specific flag is already set, it means that some letter is
3050 // present more than once, this is not acceptable.
3051 if (FlagsVal == ~0U || (FlagsVal & Flag))
3052 return MatchOperand_NoMatch;
3053 FlagsVal |= Flag;
3054 }
3055 } else // No match for special register.
3056 return MatchOperand_NoMatch;
3057
Owen Anderson7784f1d2011-10-21 18:43:28 +00003058 // Special register without flags is NOT equivalent to "fc" flags.
3059 // NOTE: This is a divergence from gas' behavior. Uncommenting the following
3060 // two lines would enable gas compatibility at the expense of breaking
3061 // round-tripping.
3062 //
3063 // if (!FlagsVal)
3064 // FlagsVal = 0x9;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00003065
3066 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
3067 if (SpecReg == "spsr")
3068 FlagsVal |= 16;
3069
3070 Parser.Lex(); // Eat identifier token.
3071 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
3072 return MatchOperand_Success;
3073}
3074
Jim Grosbachf6c05252011-07-21 17:23:04 +00003075ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3076parsePKHImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands, StringRef Op,
3077 int Low, int High) {
3078 const AsmToken &Tok = Parser.getTok();
3079 if (Tok.isNot(AsmToken::Identifier)) {
3080 Error(Parser.getTok().getLoc(), Op + " operand expected.");
3081 return MatchOperand_ParseFail;
3082 }
3083 StringRef ShiftName = Tok.getString();
Benjamin Kramer59085362011-11-06 20:37:06 +00003084 std::string LowerOp = Op.lower();
3085 std::string UpperOp = Op.upper();
Jim Grosbachf6c05252011-07-21 17:23:04 +00003086 if (ShiftName != LowerOp && ShiftName != UpperOp) {
3087 Error(Parser.getTok().getLoc(), Op + " operand expected.");
3088 return MatchOperand_ParseFail;
3089 }
3090 Parser.Lex(); // Eat shift type token.
3091
3092 // There must be a '#' and a shift amount.
3093 if (Parser.getTok().isNot(AsmToken::Hash)) {
3094 Error(Parser.getTok().getLoc(), "'#' expected");
3095 return MatchOperand_ParseFail;
3096 }
3097 Parser.Lex(); // Eat hash token.
3098
3099 const MCExpr *ShiftAmount;
3100 SMLoc Loc = Parser.getTok().getLoc();
3101 if (getParser().ParseExpression(ShiftAmount)) {
3102 Error(Loc, "illegal expression");
3103 return MatchOperand_ParseFail;
3104 }
3105 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
3106 if (!CE) {
3107 Error(Loc, "constant expression expected");
3108 return MatchOperand_ParseFail;
3109 }
3110 int Val = CE->getValue();
3111 if (Val < Low || Val > High) {
3112 Error(Loc, "immediate value out of range");
3113 return MatchOperand_ParseFail;
3114 }
3115
3116 Operands.push_back(ARMOperand::CreateImm(CE, Loc, Parser.getTok().getLoc()));
3117
3118 return MatchOperand_Success;
3119}
3120
Jim Grosbachc27d4f92011-07-22 17:44:50 +00003121ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3122parseSetEndImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3123 const AsmToken &Tok = Parser.getTok();
3124 SMLoc S = Tok.getLoc();
3125 if (Tok.isNot(AsmToken::Identifier)) {
3126 Error(Tok.getLoc(), "'be' or 'le' operand expected");
3127 return MatchOperand_ParseFail;
3128 }
3129 int Val = StringSwitch<int>(Tok.getString())
3130 .Case("be", 1)
3131 .Case("le", 0)
3132 .Default(-1);
3133 Parser.Lex(); // Eat the token.
3134
3135 if (Val == -1) {
3136 Error(Tok.getLoc(), "'be' or 'le' operand expected");
3137 return MatchOperand_ParseFail;
3138 }
3139 Operands.push_back(ARMOperand::CreateImm(MCConstantExpr::Create(Val,
3140 getContext()),
3141 S, Parser.getTok().getLoc()));
3142 return MatchOperand_Success;
3143}
3144
Jim Grosbach580f4a92011-07-25 22:20:28 +00003145/// parseShifterImm - Parse the shifter immediate operand for SSAT/USAT
3146/// instructions. Legal values are:
3147/// lsl #n 'n' in [0,31]
3148/// asr #n 'n' in [1,32]
3149/// n == 32 encoded as n == 0.
3150ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3151parseShifterImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3152 const AsmToken &Tok = Parser.getTok();
3153 SMLoc S = Tok.getLoc();
3154 if (Tok.isNot(AsmToken::Identifier)) {
3155 Error(S, "shift operator 'asr' or 'lsl' expected");
3156 return MatchOperand_ParseFail;
3157 }
3158 StringRef ShiftName = Tok.getString();
3159 bool isASR;
3160 if (ShiftName == "lsl" || ShiftName == "LSL")
3161 isASR = false;
3162 else if (ShiftName == "asr" || ShiftName == "ASR")
3163 isASR = true;
3164 else {
3165 Error(S, "shift operator 'asr' or 'lsl' expected");
3166 return MatchOperand_ParseFail;
3167 }
3168 Parser.Lex(); // Eat the operator.
3169
3170 // A '#' and a shift amount.
3171 if (Parser.getTok().isNot(AsmToken::Hash)) {
3172 Error(Parser.getTok().getLoc(), "'#' expected");
3173 return MatchOperand_ParseFail;
3174 }
3175 Parser.Lex(); // Eat hash token.
3176
3177 const MCExpr *ShiftAmount;
3178 SMLoc E = Parser.getTok().getLoc();
3179 if (getParser().ParseExpression(ShiftAmount)) {
3180 Error(E, "malformed shift expression");
3181 return MatchOperand_ParseFail;
3182 }
3183 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
3184 if (!CE) {
3185 Error(E, "shift amount must be an immediate");
3186 return MatchOperand_ParseFail;
3187 }
3188
3189 int64_t Val = CE->getValue();
3190 if (isASR) {
3191 // Shift amount must be in [1,32]
3192 if (Val < 1 || Val > 32) {
3193 Error(E, "'asr' shift amount must be in range [1,32]");
3194 return MatchOperand_ParseFail;
3195 }
Owen Anderson0afa0092011-09-26 21:06:22 +00003196 // asr #32 encoded as asr #0, but is not allowed in Thumb2 mode.
3197 if (isThumb() && Val == 32) {
3198 Error(E, "'asr #32' shift amount not allowed in Thumb mode");
3199 return MatchOperand_ParseFail;
3200 }
Jim Grosbach580f4a92011-07-25 22:20:28 +00003201 if (Val == 32) Val = 0;
3202 } else {
3203 // Shift amount must be in [1,32]
3204 if (Val < 0 || Val > 31) {
3205 Error(E, "'lsr' shift amount must be in range [0,31]");
3206 return MatchOperand_ParseFail;
3207 }
3208 }
3209
3210 E = Parser.getTok().getLoc();
3211 Operands.push_back(ARMOperand::CreateShifterImm(isASR, Val, S, E));
3212
3213 return MatchOperand_Success;
3214}
3215
Jim Grosbach7e1547e2011-07-27 20:15:40 +00003216/// parseRotImm - Parse the shifter immediate operand for SXTB/UXTB family
3217/// of instructions. Legal values are:
3218/// ror #n 'n' in {0, 8, 16, 24}
3219ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3220parseRotImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3221 const AsmToken &Tok = Parser.getTok();
3222 SMLoc S = Tok.getLoc();
Jim Grosbach326efe52011-09-19 20:29:33 +00003223 if (Tok.isNot(AsmToken::Identifier))
3224 return MatchOperand_NoMatch;
Jim Grosbach7e1547e2011-07-27 20:15:40 +00003225 StringRef ShiftName = Tok.getString();
Jim Grosbach326efe52011-09-19 20:29:33 +00003226 if (ShiftName != "ror" && ShiftName != "ROR")
3227 return MatchOperand_NoMatch;
Jim Grosbach7e1547e2011-07-27 20:15:40 +00003228 Parser.Lex(); // Eat the operator.
3229
3230 // A '#' and a rotate amount.
3231 if (Parser.getTok().isNot(AsmToken::Hash)) {
3232 Error(Parser.getTok().getLoc(), "'#' expected");
3233 return MatchOperand_ParseFail;
3234 }
3235 Parser.Lex(); // Eat hash token.
3236
3237 const MCExpr *ShiftAmount;
3238 SMLoc E = Parser.getTok().getLoc();
3239 if (getParser().ParseExpression(ShiftAmount)) {
3240 Error(E, "malformed rotate expression");
3241 return MatchOperand_ParseFail;
3242 }
3243 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftAmount);
3244 if (!CE) {
3245 Error(E, "rotate amount must be an immediate");
3246 return MatchOperand_ParseFail;
3247 }
3248
3249 int64_t Val = CE->getValue();
3250 // Shift amount must be in {0, 8, 16, 24} (0 is undocumented extension)
3251 // normally, zero is represented in asm by omitting the rotate operand
3252 // entirely.
3253 if (Val != 8 && Val != 16 && Val != 24 && Val != 0) {
3254 Error(E, "'ror' rotate amount must be 8, 16, or 24");
3255 return MatchOperand_ParseFail;
3256 }
3257
3258 E = Parser.getTok().getLoc();
3259 Operands.push_back(ARMOperand::CreateRotImm(Val, S, E));
3260
3261 return MatchOperand_Success;
3262}
3263
Jim Grosbach293a2ee2011-07-28 21:34:26 +00003264ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3265parseBitfield(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3266 SMLoc S = Parser.getTok().getLoc();
3267 // The bitfield descriptor is really two operands, the LSB and the width.
3268 if (Parser.getTok().isNot(AsmToken::Hash)) {
3269 Error(Parser.getTok().getLoc(), "'#' expected");
3270 return MatchOperand_ParseFail;
3271 }
3272 Parser.Lex(); // Eat hash token.
3273
3274 const MCExpr *LSBExpr;
3275 SMLoc E = Parser.getTok().getLoc();
3276 if (getParser().ParseExpression(LSBExpr)) {
3277 Error(E, "malformed immediate expression");
3278 return MatchOperand_ParseFail;
3279 }
3280 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(LSBExpr);
3281 if (!CE) {
3282 Error(E, "'lsb' operand must be an immediate");
3283 return MatchOperand_ParseFail;
3284 }
3285
3286 int64_t LSB = CE->getValue();
3287 // The LSB must be in the range [0,31]
3288 if (LSB < 0 || LSB > 31) {
3289 Error(E, "'lsb' operand must be in the range [0,31]");
3290 return MatchOperand_ParseFail;
3291 }
3292 E = Parser.getTok().getLoc();
3293
3294 // Expect another immediate operand.
3295 if (Parser.getTok().isNot(AsmToken::Comma)) {
3296 Error(Parser.getTok().getLoc(), "too few operands");
3297 return MatchOperand_ParseFail;
3298 }
3299 Parser.Lex(); // Eat hash token.
3300 if (Parser.getTok().isNot(AsmToken::Hash)) {
3301 Error(Parser.getTok().getLoc(), "'#' expected");
3302 return MatchOperand_ParseFail;
3303 }
3304 Parser.Lex(); // Eat hash token.
3305
3306 const MCExpr *WidthExpr;
3307 if (getParser().ParseExpression(WidthExpr)) {
3308 Error(E, "malformed immediate expression");
3309 return MatchOperand_ParseFail;
3310 }
3311 CE = dyn_cast<MCConstantExpr>(WidthExpr);
3312 if (!CE) {
3313 Error(E, "'width' operand must be an immediate");
3314 return MatchOperand_ParseFail;
3315 }
3316
3317 int64_t Width = CE->getValue();
3318 // The LSB must be in the range [1,32-lsb]
3319 if (Width < 1 || Width > 32 - LSB) {
3320 Error(E, "'width' operand must be in the range [1,32-lsb]");
3321 return MatchOperand_ParseFail;
3322 }
3323 E = Parser.getTok().getLoc();
3324
3325 Operands.push_back(ARMOperand::CreateBitfield(LSB, Width, S, E));
3326
3327 return MatchOperand_Success;
3328}
3329
Jim Grosbach7ce05792011-08-03 23:50:40 +00003330ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3331parsePostIdxReg(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3332 // Check for a post-index addressing register operand. Specifically:
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00003333 // postidx_reg := '+' register {, shift}
3334 // | '-' register {, shift}
3335 // | register {, shift}
Jim Grosbach7ce05792011-08-03 23:50:40 +00003336
3337 // This method must return MatchOperand_NoMatch without consuming any tokens
3338 // in the case where there is no match, as other alternatives take other
3339 // parse methods.
3340 AsmToken Tok = Parser.getTok();
3341 SMLoc S = Tok.getLoc();
3342 bool haveEaten = false;
Jim Grosbach16578b52011-08-05 16:11:38 +00003343 bool isAdd = true;
Jim Grosbach7ce05792011-08-03 23:50:40 +00003344 int Reg = -1;
3345 if (Tok.is(AsmToken::Plus)) {
3346 Parser.Lex(); // Eat the '+' token.
3347 haveEaten = true;
3348 } else if (Tok.is(AsmToken::Minus)) {
3349 Parser.Lex(); // Eat the '-' token.
Jim Grosbach16578b52011-08-05 16:11:38 +00003350 isAdd = false;
Jim Grosbach7ce05792011-08-03 23:50:40 +00003351 haveEaten = true;
3352 }
3353 if (Parser.getTok().is(AsmToken::Identifier))
3354 Reg = tryParseRegister();
3355 if (Reg == -1) {
3356 if (!haveEaten)
3357 return MatchOperand_NoMatch;
3358 Error(Parser.getTok().getLoc(), "register expected");
3359 return MatchOperand_ParseFail;
3360 }
3361 SMLoc E = Parser.getTok().getLoc();
3362
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00003363 ARM_AM::ShiftOpc ShiftTy = ARM_AM::no_shift;
3364 unsigned ShiftImm = 0;
Jim Grosbach0d6fac32011-08-05 22:03:36 +00003365 if (Parser.getTok().is(AsmToken::Comma)) {
3366 Parser.Lex(); // Eat the ','.
3367 if (parseMemRegOffsetShift(ShiftTy, ShiftImm))
3368 return MatchOperand_ParseFail;
3369 }
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00003370
3371 Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ShiftTy,
3372 ShiftImm, S, E));
Jim Grosbach7ce05792011-08-03 23:50:40 +00003373
3374 return MatchOperand_Success;
3375}
3376
Jim Grosbach251bf252011-08-10 21:56:18 +00003377ARMAsmParser::OperandMatchResultTy ARMAsmParser::
3378parseAM3Offset(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3379 // Check for a post-index addressing register operand. Specifically:
3380 // am3offset := '+' register
3381 // | '-' register
3382 // | register
3383 // | # imm
3384 // | # + imm
3385 // | # - imm
3386
3387 // This method must return MatchOperand_NoMatch without consuming any tokens
3388 // in the case where there is no match, as other alternatives take other
3389 // parse methods.
3390 AsmToken Tok = Parser.getTok();
3391 SMLoc S = Tok.getLoc();
3392
3393 // Do immediates first, as we always parse those if we have a '#'.
3394 if (Parser.getTok().is(AsmToken::Hash)) {
3395 Parser.Lex(); // Eat the '#'.
3396 // Explicitly look for a '-', as we need to encode negative zero
3397 // differently.
3398 bool isNegative = Parser.getTok().is(AsmToken::Minus);
3399 const MCExpr *Offset;
3400 if (getParser().ParseExpression(Offset))
3401 return MatchOperand_ParseFail;
3402 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
3403 if (!CE) {
3404 Error(S, "constant expression expected");
3405 return MatchOperand_ParseFail;
3406 }
3407 SMLoc E = Tok.getLoc();
3408 // Negative zero is encoded as the flag value INT32_MIN.
3409 int32_t Val = CE->getValue();
3410 if (isNegative && Val == 0)
3411 Val = INT32_MIN;
3412
3413 Operands.push_back(
3414 ARMOperand::CreateImm(MCConstantExpr::Create(Val, getContext()), S, E));
3415
3416 return MatchOperand_Success;
3417 }
3418
3419
3420 bool haveEaten = false;
3421 bool isAdd = true;
3422 int Reg = -1;
3423 if (Tok.is(AsmToken::Plus)) {
3424 Parser.Lex(); // Eat the '+' token.
3425 haveEaten = true;
3426 } else if (Tok.is(AsmToken::Minus)) {
3427 Parser.Lex(); // Eat the '-' token.
3428 isAdd = false;
3429 haveEaten = true;
3430 }
3431 if (Parser.getTok().is(AsmToken::Identifier))
3432 Reg = tryParseRegister();
3433 if (Reg == -1) {
3434 if (!haveEaten)
3435 return MatchOperand_NoMatch;
3436 Error(Parser.getTok().getLoc(), "register expected");
3437 return MatchOperand_ParseFail;
3438 }
3439 SMLoc E = Parser.getTok().getLoc();
3440
3441 Operands.push_back(ARMOperand::CreatePostIdxReg(Reg, isAdd, ARM_AM::no_shift,
3442 0, S, E));
3443
3444 return MatchOperand_Success;
3445}
3446
Jim Grosbacha77295d2011-09-08 22:07:06 +00003447/// cvtT2LdrdPre - Convert parsed operands to MCInst.
3448/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3449/// when they refer multiple MIOperands inside a single one.
3450bool ARMAsmParser::
3451cvtT2LdrdPre(MCInst &Inst, unsigned Opcode,
3452 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3453 // Rt, Rt2
3454 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3455 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3456 // Create a writeback register dummy placeholder.
3457 Inst.addOperand(MCOperand::CreateReg(0));
3458 // addr
3459 ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
3460 // pred
3461 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3462 return true;
3463}
3464
3465/// cvtT2StrdPre - Convert parsed operands to MCInst.
3466/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3467/// when they refer multiple MIOperands inside a single one.
3468bool ARMAsmParser::
3469cvtT2StrdPre(MCInst &Inst, unsigned Opcode,
3470 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3471 // Create a writeback register dummy placeholder.
3472 Inst.addOperand(MCOperand::CreateReg(0));
3473 // Rt, Rt2
3474 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3475 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3476 // addr
3477 ((ARMOperand*)Operands[4])->addMemImm8s4OffsetOperands(Inst, 2);
3478 // pred
3479 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3480 return true;
3481}
3482
Jim Grosbacheeec0252011-09-08 00:39:19 +00003483/// cvtLdWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
3484/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3485/// when they refer multiple MIOperands inside a single one.
3486bool ARMAsmParser::
3487cvtLdWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
3488 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3489 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3490
3491 // Create a writeback register dummy placeholder.
3492 Inst.addOperand(MCOperand::CreateImm(0));
3493
3494 ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
3495 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3496 return true;
3497}
3498
Jim Grosbachee2c2a42011-09-16 21:55:56 +00003499/// cvtStWriteBackRegT2AddrModeImm8 - Convert parsed operands to MCInst.
3500/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3501/// when they refer multiple MIOperands inside a single one.
3502bool ARMAsmParser::
3503cvtStWriteBackRegT2AddrModeImm8(MCInst &Inst, unsigned Opcode,
3504 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3505 // Create a writeback register dummy placeholder.
3506 Inst.addOperand(MCOperand::CreateImm(0));
3507 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3508 ((ARMOperand*)Operands[3])->addMemImm8OffsetOperands(Inst, 2);
3509 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3510 return true;
3511}
3512
Jim Grosbach1355cf12011-07-26 17:10:22 +00003513/// cvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003514/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3515/// when they refer multiple MIOperands inside a single one.
3516bool ARMAsmParser::
Jim Grosbach1355cf12011-07-26 17:10:22 +00003517cvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003518 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3519 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3520
3521 // Create a writeback register dummy placeholder.
3522 Inst.addOperand(MCOperand::CreateImm(0));
3523
Jim Grosbach7ce05792011-08-03 23:50:40 +00003524 ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003525 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3526 return true;
3527}
3528
Owen Anderson9ab0f252011-08-26 20:43:14 +00003529/// cvtLdWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
3530/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3531/// when they refer multiple MIOperands inside a single one.
3532bool ARMAsmParser::
3533cvtLdWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
3534 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3535 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3536
3537 // Create a writeback register dummy placeholder.
3538 Inst.addOperand(MCOperand::CreateImm(0));
3539
3540 ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
3541 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3542 return true;
3543}
3544
3545
Jim Grosbach548340c2011-08-11 19:22:40 +00003546/// cvtStWriteBackRegAddrModeImm12 - Convert parsed operands to MCInst.
3547/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3548/// when they refer multiple MIOperands inside a single one.
3549bool ARMAsmParser::
3550cvtStWriteBackRegAddrModeImm12(MCInst &Inst, unsigned Opcode,
3551 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3552 // Create a writeback register dummy placeholder.
3553 Inst.addOperand(MCOperand::CreateImm(0));
3554 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3555 ((ARMOperand*)Operands[3])->addMemImm12OffsetOperands(Inst, 2);
3556 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3557 return true;
3558}
3559
Jim Grosbach1355cf12011-07-26 17:10:22 +00003560/// cvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003561/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3562/// when they refer multiple MIOperands inside a single one.
3563bool ARMAsmParser::
Jim Grosbach1355cf12011-07-26 17:10:22 +00003564cvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003565 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3566 // Create a writeback register dummy placeholder.
3567 Inst.addOperand(MCOperand::CreateImm(0));
Jim Grosbach548340c2011-08-11 19:22:40 +00003568 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3569 ((ARMOperand*)Operands[3])->addAddrMode2Operands(Inst, 3);
3570 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
Jim Grosbach7ce05792011-08-03 23:50:40 +00003571 return true;
3572}
3573
Jim Grosbach7b8f46c2011-08-11 21:17:22 +00003574/// cvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
3575/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3576/// when they refer multiple MIOperands inside a single one.
3577bool ARMAsmParser::
3578cvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
3579 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3580 // Create a writeback register dummy placeholder.
3581 Inst.addOperand(MCOperand::CreateImm(0));
3582 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3583 ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
3584 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3585 return true;
3586}
3587
Jim Grosbach7ce05792011-08-03 23:50:40 +00003588/// cvtLdExtTWriteBackImm - Convert parsed operands to MCInst.
3589/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3590/// when they refer multiple MIOperands inside a single one.
3591bool ARMAsmParser::
3592cvtLdExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
3593 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3594 // Rt
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003595 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
Jim Grosbach7ce05792011-08-03 23:50:40 +00003596 // Create a writeback register dummy placeholder.
3597 Inst.addOperand(MCOperand::CreateImm(0));
3598 // addr
3599 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3600 // offset
3601 ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
3602 // pred
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00003603 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3604 return true;
3605}
3606
Jim Grosbach7ce05792011-08-03 23:50:40 +00003607/// cvtLdExtTWriteBackReg - Convert parsed operands to MCInst.
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003608/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3609/// when they refer multiple MIOperands inside a single one.
3610bool ARMAsmParser::
Jim Grosbach7ce05792011-08-03 23:50:40 +00003611cvtLdExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
3612 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3613 // Rt
Owen Andersonaa3402e2011-07-28 17:18:57 +00003614 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003615 // Create a writeback register dummy placeholder.
3616 Inst.addOperand(MCOperand::CreateImm(0));
Jim Grosbach7ce05792011-08-03 23:50:40 +00003617 // addr
3618 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3619 // offset
3620 ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
3621 // pred
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003622 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3623 return true;
3624}
3625
Jim Grosbach7ce05792011-08-03 23:50:40 +00003626/// cvtStExtTWriteBackImm - Convert parsed operands to MCInst.
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003627/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3628/// when they refer multiple MIOperands inside a single one.
3629bool ARMAsmParser::
Jim Grosbach7ce05792011-08-03 23:50:40 +00003630cvtStExtTWriteBackImm(MCInst &Inst, unsigned Opcode,
3631 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003632 // Create a writeback register dummy placeholder.
3633 Inst.addOperand(MCOperand::CreateImm(0));
Jim Grosbach7ce05792011-08-03 23:50:40 +00003634 // Rt
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003635 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
Jim Grosbach7ce05792011-08-03 23:50:40 +00003636 // addr
3637 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3638 // offset
3639 ((ARMOperand*)Operands[4])->addPostIdxImm8Operands(Inst, 1);
3640 // pred
3641 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3642 return true;
3643}
3644
3645/// cvtStExtTWriteBackReg - Convert parsed operands to MCInst.
3646/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3647/// when they refer multiple MIOperands inside a single one.
3648bool ARMAsmParser::
3649cvtStExtTWriteBackReg(MCInst &Inst, unsigned Opcode,
3650 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3651 // Create a writeback register dummy placeholder.
3652 Inst.addOperand(MCOperand::CreateImm(0));
3653 // Rt
3654 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3655 // addr
3656 ((ARMOperand*)Operands[3])->addMemNoOffsetOperands(Inst, 1);
3657 // offset
3658 ((ARMOperand*)Operands[4])->addPostIdxRegOperands(Inst, 2);
3659 // pred
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00003660 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3661 return true;
3662}
3663
Jim Grosbach2fd2b872011-08-10 20:29:19 +00003664/// cvtLdrdPre - Convert parsed operands to MCInst.
3665/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3666/// when they refer multiple MIOperands inside a single one.
3667bool ARMAsmParser::
3668cvtLdrdPre(MCInst &Inst, unsigned Opcode,
3669 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3670 // Rt, Rt2
3671 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3672 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3673 // Create a writeback register dummy placeholder.
3674 Inst.addOperand(MCOperand::CreateImm(0));
3675 // addr
3676 ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
3677 // pred
3678 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3679 return true;
3680}
3681
Jim Grosbach14605d12011-08-11 20:28:23 +00003682/// cvtStrdPre - Convert parsed operands to MCInst.
3683/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3684/// when they refer multiple MIOperands inside a single one.
3685bool ARMAsmParser::
3686cvtStrdPre(MCInst &Inst, unsigned Opcode,
3687 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3688 // Create a writeback register dummy placeholder.
3689 Inst.addOperand(MCOperand::CreateImm(0));
3690 // Rt, Rt2
3691 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3692 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3693 // addr
3694 ((ARMOperand*)Operands[4])->addAddrMode3Operands(Inst, 3);
3695 // pred
3696 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3697 return true;
3698}
3699
Jim Grosbach623a4542011-08-10 22:42:16 +00003700/// cvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
3701/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3702/// when they refer multiple MIOperands inside a single one.
3703bool ARMAsmParser::
3704cvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
3705 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3706 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
3707 // Create a writeback register dummy placeholder.
3708 Inst.addOperand(MCOperand::CreateImm(0));
3709 ((ARMOperand*)Operands[3])->addAddrMode3Operands(Inst, 3);
3710 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3711 return true;
3712}
3713
Jim Grosbach88ae2bc2011-08-19 22:07:46 +00003714/// cvtThumbMultiple- Convert parsed operands to MCInst.
3715/// Needed here because the Asm Gen Matcher can't handle properly tied operands
3716/// when they refer multiple MIOperands inside a single one.
3717bool ARMAsmParser::
3718cvtThumbMultiply(MCInst &Inst, unsigned Opcode,
3719 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3720 // The second source operand must be the same register as the destination
3721 // operand.
3722 if (Operands.size() == 6 &&
Jim Grosbach7a010692011-08-19 22:30:46 +00003723 (((ARMOperand*)Operands[3])->getReg() !=
3724 ((ARMOperand*)Operands[5])->getReg()) &&
3725 (((ARMOperand*)Operands[3])->getReg() !=
3726 ((ARMOperand*)Operands[4])->getReg())) {
Jim Grosbach88ae2bc2011-08-19 22:07:46 +00003727 Error(Operands[3]->getStartLoc(),
Jim Grosbach7a010692011-08-19 22:30:46 +00003728 "destination register must match source register");
Jim Grosbach88ae2bc2011-08-19 22:07:46 +00003729 return false;
3730 }
3731 ((ARMOperand*)Operands[3])->addRegOperands(Inst, 1);
3732 ((ARMOperand*)Operands[1])->addCCOutOperands(Inst, 1);
Jim Grosbach1b332862011-11-10 22:10:12 +00003733 // If we have a three-operand form, make sure to set Rn to be the operand
3734 // that isn't the same as Rd.
3735 unsigned RegOp = 4;
3736 if (Operands.size() == 6 &&
3737 ((ARMOperand*)Operands[4])->getReg() ==
3738 ((ARMOperand*)Operands[3])->getReg())
3739 RegOp = 5;
3740 ((ARMOperand*)Operands[RegOp])->addRegOperands(Inst, 1);
3741 Inst.addOperand(Inst.getOperand(0));
Jim Grosbach88ae2bc2011-08-19 22:07:46 +00003742 ((ARMOperand*)Operands[2])->addCondCodeOperands(Inst, 2);
3743
3744 return true;
3745}
Jim Grosbach623a4542011-08-10 22:42:16 +00003746
Jim Grosbach12431322011-10-24 22:16:58 +00003747bool ARMAsmParser::
3748cvtVLDwbFixed(MCInst &Inst, unsigned Opcode,
3749 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3750 // Vd
Jim Grosbach6029b6d2011-11-29 23:51:09 +00003751 ((ARMOperand*)Operands[3])->addVecListOperands(Inst, 1);
Jim Grosbach12431322011-10-24 22:16:58 +00003752 // Create a writeback register dummy placeholder.
3753 Inst.addOperand(MCOperand::CreateImm(0));
3754 // Vn
3755 ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3756 // pred
3757 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3758 return true;
3759}
3760
3761bool ARMAsmParser::
3762cvtVLDwbRegister(MCInst &Inst, unsigned Opcode,
3763 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3764 // Vd
Jim Grosbach6029b6d2011-11-29 23:51:09 +00003765 ((ARMOperand*)Operands[3])->addVecListOperands(Inst, 1);
Jim Grosbach12431322011-10-24 22:16:58 +00003766 // Create a writeback register dummy placeholder.
3767 Inst.addOperand(MCOperand::CreateImm(0));
3768 // Vn
3769 ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3770 // Vm
3771 ((ARMOperand*)Operands[5])->addRegOperands(Inst, 1);
3772 // pred
3773 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3774 return true;
3775}
3776
Jim Grosbach4334e032011-10-31 21:50:31 +00003777bool ARMAsmParser::
3778cvtVSTwbFixed(MCInst &Inst, unsigned Opcode,
3779 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3780 // Create a writeback register dummy placeholder.
3781 Inst.addOperand(MCOperand::CreateImm(0));
3782 // Vn
3783 ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3784 // Vt
Jim Grosbach6029b6d2011-11-29 23:51:09 +00003785 ((ARMOperand*)Operands[3])->addVecListOperands(Inst, 1);
Jim Grosbach4334e032011-10-31 21:50:31 +00003786 // pred
3787 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3788 return true;
3789}
3790
3791bool ARMAsmParser::
3792cvtVSTwbRegister(MCInst &Inst, unsigned Opcode,
3793 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
3794 // Create a writeback register dummy placeholder.
3795 Inst.addOperand(MCOperand::CreateImm(0));
3796 // Vn
3797 ((ARMOperand*)Operands[4])->addAlignedMemoryOperands(Inst, 2);
3798 // Vm
3799 ((ARMOperand*)Operands[5])->addRegOperands(Inst, 1);
3800 // Vt
Jim Grosbach6029b6d2011-11-29 23:51:09 +00003801 ((ARMOperand*)Operands[3])->addVecListOperands(Inst, 1);
Jim Grosbach4334e032011-10-31 21:50:31 +00003802 // pred
3803 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
3804 return true;
3805}
3806
Bill Wendlinge7176102010-11-06 22:36:58 +00003807/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003808/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00003809bool ARMAsmParser::
Jim Grosbach7ce05792011-08-03 23:50:40 +00003810parseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +00003811 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00003812 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00003813 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00003814 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00003815 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00003816
Sean Callanan18b83232010-01-19 21:44:56 +00003817 const AsmToken &BaseRegTok = Parser.getTok();
Jim Grosbach1355cf12011-07-26 17:10:22 +00003818 int BaseRegNum = tryParseRegister();
Jim Grosbach7ce05792011-08-03 23:50:40 +00003819 if (BaseRegNum == -1)
3820 return Error(BaseRegTok.getLoc(), "register expected");
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00003821
Daniel Dunbar05710932011-01-18 05:34:17 +00003822 // The next token must either be a comma or a closing bracket.
3823 const AsmToken &Tok = Parser.getTok();
3824 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
Jim Grosbach7ce05792011-08-03 23:50:40 +00003825 return Error(Tok.getLoc(), "malformed memory operand");
Daniel Dunbar05710932011-01-18 05:34:17 +00003826
Jim Grosbach7ce05792011-08-03 23:50:40 +00003827 if (Tok.is(AsmToken::RBrac)) {
Sean Callanan76264762010-04-02 22:27:05 +00003828 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00003829 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00003830
Jim Grosbach7ce05792011-08-03 23:50:40 +00003831 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0, ARM_AM::no_shift,
Jim Grosbach57dcb852011-10-11 17:29:55 +00003832 0, 0, false, S, E));
Jim Grosbach03f44a02010-11-29 23:18:01 +00003833
Jim Grosbachfb12f352011-09-19 18:42:21 +00003834 // If there's a pre-indexing writeback marker, '!', just add it as a token
3835 // operand. It's rather odd, but syntactically valid.
3836 if (Parser.getTok().is(AsmToken::Exclaim)) {
3837 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3838 Parser.Lex(); // Eat the '!'.
3839 }
3840
Jim Grosbach7ce05792011-08-03 23:50:40 +00003841 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00003842 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00003843
Jim Grosbach7ce05792011-08-03 23:50:40 +00003844 assert(Tok.is(AsmToken::Comma) && "Lost comma in memory operand?!");
3845 Parser.Lex(); // Eat the comma.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00003846
Jim Grosbach57dcb852011-10-11 17:29:55 +00003847 // If we have a ':', it's an alignment specifier.
3848 if (Parser.getTok().is(AsmToken::Colon)) {
3849 Parser.Lex(); // Eat the ':'.
3850 E = Parser.getTok().getLoc();
3851
3852 const MCExpr *Expr;
3853 if (getParser().ParseExpression(Expr))
3854 return true;
3855
3856 // The expression has to be a constant. Memory references with relocations
3857 // don't come through here, as they use the <label> forms of the relevant
3858 // instructions.
3859 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
3860 if (!CE)
3861 return Error (E, "constant expression expected");
3862
3863 unsigned Align = 0;
3864 switch (CE->getValue()) {
3865 default:
3866 return Error(E, "alignment specifier must be 64, 128, or 256 bits");
3867 case 64: Align = 8; break;
3868 case 128: Align = 16; break;
3869 case 256: Align = 32; break;
3870 }
3871
3872 // Now we should have the closing ']'
3873 E = Parser.getTok().getLoc();
3874 if (Parser.getTok().isNot(AsmToken::RBrac))
3875 return Error(E, "']' expected");
3876 Parser.Lex(); // Eat right bracket token.
3877
3878 // Don't worry about range checking the value here. That's handled by
3879 // the is*() predicates.
3880 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, 0,
3881 ARM_AM::no_shift, 0, Align,
3882 false, S, E));
3883
3884 // If there's a pre-indexing writeback marker, '!', just add it as a token
3885 // operand.
3886 if (Parser.getTok().is(AsmToken::Exclaim)) {
3887 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3888 Parser.Lex(); // Eat the '!'.
3889 }
3890
3891 return false;
3892 }
3893
3894 // If we have a '#', it's an immediate offset, else assume it's a register
Jim Grosbach6cb4b082011-11-15 22:14:41 +00003895 // offset. Be friendly and also accept a plain integer (without a leading
3896 // hash) for gas compatibility.
3897 if (Parser.getTok().is(AsmToken::Hash) ||
3898 Parser.getTok().is(AsmToken::Integer)) {
3899 if (Parser.getTok().is(AsmToken::Hash))
3900 Parser.Lex(); // Eat the '#'.
Jim Grosbach7ce05792011-08-03 23:50:40 +00003901 E = Parser.getTok().getLoc();
Daniel Dunbar05d8b712011-01-18 05:34:24 +00003902
Owen Anderson0da10cf2011-08-29 19:36:44 +00003903 bool isNegative = getParser().getTok().is(AsmToken::Minus);
Jim Grosbach7ce05792011-08-03 23:50:40 +00003904 const MCExpr *Offset;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003905 if (getParser().ParseExpression(Offset))
3906 return true;
Jim Grosbach7ce05792011-08-03 23:50:40 +00003907
3908 // The expression has to be a constant. Memory references with relocations
3909 // don't come through here, as they use the <label> forms of the relevant
3910 // instructions.
3911 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Offset);
3912 if (!CE)
3913 return Error (E, "constant expression expected");
3914
Owen Anderson0da10cf2011-08-29 19:36:44 +00003915 // If the constant was #-0, represent it as INT32_MIN.
3916 int32_t Val = CE->getValue();
3917 if (isNegative && Val == 0)
3918 CE = MCConstantExpr::Create(INT32_MIN, getContext());
3919
Jim Grosbach7ce05792011-08-03 23:50:40 +00003920 // Now we should have the closing ']'
3921 E = Parser.getTok().getLoc();
3922 if (Parser.getTok().isNot(AsmToken::RBrac))
3923 return Error(E, "']' expected");
3924 Parser.Lex(); // Eat right bracket token.
3925
3926 // Don't worry about range checking the value here. That's handled by
3927 // the is*() predicates.
3928 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, CE, 0,
Jim Grosbach57dcb852011-10-11 17:29:55 +00003929 ARM_AM::no_shift, 0, 0,
3930 false, S, E));
Jim Grosbach7ce05792011-08-03 23:50:40 +00003931
3932 // If there's a pre-indexing writeback marker, '!', just add it as a token
3933 // operand.
3934 if (Parser.getTok().is(AsmToken::Exclaim)) {
3935 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3936 Parser.Lex(); // Eat the '!'.
3937 }
3938
3939 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00003940 }
Jim Grosbach7ce05792011-08-03 23:50:40 +00003941
3942 // The register offset is optionally preceded by a '+' or '-'
3943 bool isNegative = false;
3944 if (Parser.getTok().is(AsmToken::Minus)) {
3945 isNegative = true;
3946 Parser.Lex(); // Eat the '-'.
3947 } else if (Parser.getTok().is(AsmToken::Plus)) {
3948 // Nothing to do.
3949 Parser.Lex(); // Eat the '+'.
3950 }
3951
3952 E = Parser.getTok().getLoc();
3953 int OffsetRegNum = tryParseRegister();
3954 if (OffsetRegNum == -1)
3955 return Error(E, "register expected");
3956
3957 // If there's a shift operator, handle it.
3958 ARM_AM::ShiftOpc ShiftType = ARM_AM::no_shift;
Jim Grosbach0d6fac32011-08-05 22:03:36 +00003959 unsigned ShiftImm = 0;
Jim Grosbach7ce05792011-08-03 23:50:40 +00003960 if (Parser.getTok().is(AsmToken::Comma)) {
3961 Parser.Lex(); // Eat the ','.
Jim Grosbach0d6fac32011-08-05 22:03:36 +00003962 if (parseMemRegOffsetShift(ShiftType, ShiftImm))
Jim Grosbach7ce05792011-08-03 23:50:40 +00003963 return true;
3964 }
3965
3966 // Now we should have the closing ']'
3967 E = Parser.getTok().getLoc();
3968 if (Parser.getTok().isNot(AsmToken::RBrac))
3969 return Error(E, "']' expected");
3970 Parser.Lex(); // Eat right bracket token.
3971
3972 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, 0, OffsetRegNum,
Jim Grosbach57dcb852011-10-11 17:29:55 +00003973 ShiftType, ShiftImm, 0, isNegative,
Jim Grosbach7ce05792011-08-03 23:50:40 +00003974 S, E));
3975
Jim Grosbachf4fa3d62011-08-05 21:28:30 +00003976 // If there's a pre-indexing writeback marker, '!', just add it as a token
3977 // operand.
3978 if (Parser.getTok().is(AsmToken::Exclaim)) {
3979 Operands.push_back(ARMOperand::CreateToken("!",Parser.getTok().getLoc()));
3980 Parser.Lex(); // Eat the '!'.
3981 }
Jim Grosbach7ce05792011-08-03 23:50:40 +00003982
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003983 return false;
3984}
3985
Jim Grosbach7ce05792011-08-03 23:50:40 +00003986/// parseMemRegOffsetShift - one of these two:
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003987/// ( lsl | lsr | asr | ror ) , # shift_amount
3988/// rrx
Jim Grosbach7ce05792011-08-03 23:50:40 +00003989/// return true if it parses a shift otherwise it returns false.
3990bool ARMAsmParser::parseMemRegOffsetShift(ARM_AM::ShiftOpc &St,
3991 unsigned &Amount) {
3992 SMLoc Loc = Parser.getTok().getLoc();
Sean Callanan18b83232010-01-19 21:44:56 +00003993 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00003994 if (Tok.isNot(AsmToken::Identifier))
3995 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00003996 StringRef ShiftName = Tok.getString();
Jim Grosbachaf4edea2011-12-07 23:40:58 +00003997 if (ShiftName == "lsl" || ShiftName == "LSL" ||
3998 ShiftName == "asl" || ShiftName == "ASL")
Owen Anderson00828302011-03-18 22:50:18 +00003999 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00004000 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00004001 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00004002 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00004003 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00004004 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00004005 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00004006 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00004007 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00004008 else
Jim Grosbach7ce05792011-08-03 23:50:40 +00004009 return Error(Loc, "illegal shift operator");
Sean Callananb9a25b72010-01-19 20:27:46 +00004010 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00004011
Jim Grosbach7ce05792011-08-03 23:50:40 +00004012 // rrx stands alone.
4013 Amount = 0;
4014 if (St != ARM_AM::rrx) {
4015 Loc = Parser.getTok().getLoc();
4016 // A '#' and a shift amount.
4017 const AsmToken &HashTok = Parser.getTok();
4018 if (HashTok.isNot(AsmToken::Hash))
4019 return Error(HashTok.getLoc(), "'#' expected");
4020 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00004021
Jim Grosbach7ce05792011-08-03 23:50:40 +00004022 const MCExpr *Expr;
4023 if (getParser().ParseExpression(Expr))
4024 return true;
4025 // Range check the immediate.
4026 // lsl, ror: 0 <= imm <= 31
4027 // lsr, asr: 0 <= imm <= 32
4028 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
4029 if (!CE)
4030 return Error(Loc, "shift amount must be an immediate");
4031 int64_t Imm = CE->getValue();
4032 if (Imm < 0 ||
4033 ((St == ARM_AM::lsl || St == ARM_AM::ror) && Imm > 31) ||
4034 ((St == ARM_AM::lsr || St == ARM_AM::asr) && Imm > 32))
4035 return Error(Loc, "immediate shift value out of range");
4036 Amount = Imm;
4037 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004038
4039 return false;
4040}
4041
Jim Grosbach9d390362011-10-03 23:38:36 +00004042/// parseFPImm - A floating point immediate expression operand.
4043ARMAsmParser::OperandMatchResultTy ARMAsmParser::
4044parseFPImm(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4045 SMLoc S = Parser.getTok().getLoc();
4046
4047 if (Parser.getTok().isNot(AsmToken::Hash))
4048 return MatchOperand_NoMatch;
Jim Grosbach0e387b22011-10-17 22:26:03 +00004049
4050 // Disambiguate the VMOV forms that can accept an FP immediate.
4051 // vmov.f32 <sreg>, #imm
4052 // vmov.f64 <dreg>, #imm
4053 // vmov.f32 <dreg>, #imm @ vector f32x2
4054 // vmov.f32 <qreg>, #imm @ vector f32x4
4055 //
4056 // There are also the NEON VMOV instructions which expect an
4057 // integer constant. Make sure we don't try to parse an FPImm
4058 // for these:
4059 // vmov.i{8|16|32|64} <dreg|qreg>, #imm
4060 ARMOperand *TyOp = static_cast<ARMOperand*>(Operands[2]);
4061 if (!TyOp->isToken() || (TyOp->getToken() != ".f32" &&
4062 TyOp->getToken() != ".f64"))
4063 return MatchOperand_NoMatch;
4064
Jim Grosbach9d390362011-10-03 23:38:36 +00004065 Parser.Lex(); // Eat the '#'.
4066
4067 // Handle negation, as that still comes through as a separate token.
4068 bool isNegative = false;
4069 if (Parser.getTok().is(AsmToken::Minus)) {
4070 isNegative = true;
4071 Parser.Lex();
4072 }
4073 const AsmToken &Tok = Parser.getTok();
4074 if (Tok.is(AsmToken::Real)) {
4075 APFloat RealVal(APFloat::IEEEdouble, Tok.getString());
4076 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
4077 // If we had a '-' in front, toggle the sign bit.
4078 IntVal ^= (uint64_t)isNegative << 63;
4079 int Val = ARM_AM::getFP64Imm(APInt(64, IntVal));
4080 Parser.Lex(); // Eat the token.
4081 if (Val == -1) {
4082 TokError("floating point value out of range");
4083 return MatchOperand_ParseFail;
4084 }
4085 Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
4086 return MatchOperand_Success;
4087 }
4088 if (Tok.is(AsmToken::Integer)) {
4089 int64_t Val = Tok.getIntVal();
4090 Parser.Lex(); // Eat the token.
4091 if (Val > 255 || Val < 0) {
4092 TokError("encoded floating point value out of range");
4093 return MatchOperand_ParseFail;
4094 }
4095 Operands.push_back(ARMOperand::CreateFPImm(Val, S, getContext()));
4096 return MatchOperand_Success;
4097 }
4098
4099 TokError("invalid floating point immediate");
4100 return MatchOperand_ParseFail;
4101}
Kevin Enderby9c41fa82009-10-30 22:55:57 +00004102/// Parse a arm instruction operand. For now this parses the operand regardless
4103/// of the mnemonic.
Jim Grosbach1355cf12011-07-26 17:10:22 +00004104bool ARMAsmParser::parseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00004105 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00004106 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00004107
4108 // Check if the current operand has a custom associated parser, if so, try to
4109 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00004110 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
4111 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00004112 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00004113 // If there wasn't a custom match, try the generic matcher below. Otherwise,
4114 // there was a match, but an error occurred, in which case, just return that
4115 // the operand parsing failed.
4116 if (ResTy == MatchOperand_ParseFail)
4117 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00004118
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004119 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00004120 default:
4121 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00004122 return true;
Jim Grosbach19906722011-07-13 18:49:30 +00004123 case AsmToken::Identifier: {
Jim Grosbach5cd5ac62011-10-03 21:12:43 +00004124 // If this is VMRS, check for the apsr_nzcv operand.
Jim Grosbach1355cf12011-07-26 17:10:22 +00004125 if (!tryParseRegisterWithWriteBack(Operands))
Bill Wendling50d0f582010-11-18 23:43:05 +00004126 return false;
Jim Grosbach0d87ec22011-07-26 20:41:24 +00004127 int Res = tryParseShiftRegister(Operands);
Jim Grosbach19906722011-07-13 18:49:30 +00004128 if (Res == 0) // success
Owen Anderson00828302011-03-18 22:50:18 +00004129 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00004130 else if (Res == -1) // irrecoverable error
4131 return true;
Jim Grosbach5cd5ac62011-10-03 21:12:43 +00004132 if (Mnemonic == "vmrs" && Parser.getTok().getString() == "apsr_nzcv") {
4133 S = Parser.getTok().getLoc();
4134 Parser.Lex();
4135 Operands.push_back(ARMOperand::CreateToken("apsr_nzcv", S));
4136 return false;
4137 }
Owen Andersone4e5e2a2011-01-13 21:46:02 +00004138
4139 // Fall though for the Identifier case that is not a register or a
4140 // special name.
Jim Grosbach19906722011-07-13 18:49:30 +00004141 }
Jim Grosbach758a5192011-10-26 21:14:08 +00004142 case AsmToken::LParen: // parenthesized expressions like (_strcmp-4)
Kevin Enderby67b212e2011-01-13 20:32:36 +00004143 case AsmToken::Integer: // things like 1f and 2b as a branch targets
Jim Grosbach6284afc2011-11-01 22:38:31 +00004144 case AsmToken::String: // quoted label names.
Kevin Enderby67b212e2011-01-13 20:32:36 +00004145 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00004146 // This was not a register so parse other operands that start with an
4147 // identifier (like labels) as expressions and create them as immediates.
4148 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00004149 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00004150 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00004151 return true;
Sean Callanan76264762010-04-02 22:27:05 +00004152 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00004153 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
4154 return false;
4155 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004156 case AsmToken::LBrac:
Jim Grosbach1355cf12011-07-26 17:10:22 +00004157 return parseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00004158 case AsmToken::LCurly:
Jim Grosbach1355cf12011-07-26 17:10:22 +00004159 return parseRegisterList(Operands);
Owen Anderson63553c72011-08-29 17:17:09 +00004160 case AsmToken::Hash: {
Kevin Enderby079469f2009-10-13 23:33:38 +00004161 // #42 -> immediate.
4162 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00004163 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00004164 Parser.Lex();
Owen Anderson63553c72011-08-29 17:17:09 +00004165 bool isNegative = Parser.getTok().is(AsmToken::Minus);
Kevin Enderby515d5092009-10-15 20:48:48 +00004166 const MCExpr *ImmVal;
4167 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00004168 return true;
Owen Anderson63553c72011-08-29 17:17:09 +00004169 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ImmVal);
Jim Grosbached6a0c52011-11-01 22:37:37 +00004170 if (CE) {
4171 int32_t Val = CE->getValue();
4172 if (isNegative && Val == 0)
4173 ImmVal = MCConstantExpr::Create(INT32_MIN, getContext());
Owen Anderson63553c72011-08-29 17:17:09 +00004174 }
Sean Callanan76264762010-04-02 22:27:05 +00004175 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00004176 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
4177 return false;
Owen Anderson63553c72011-08-29 17:17:09 +00004178 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00004179 case AsmToken::Colon: {
4180 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00004181 // FIXME: Check it's an expression prefix,
4182 // e.g. (FOO - :lower16:BAR) isn't legal.
4183 ARMMCExpr::VariantKind RefKind;
Jim Grosbach1355cf12011-07-26 17:10:22 +00004184 if (parsePrefix(RefKind))
Jason W Kim9081b4b2011-01-11 23:53:41 +00004185 return true;
4186
Evan Cheng75972122011-01-13 07:58:56 +00004187 const MCExpr *SubExprVal;
4188 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00004189 return true;
4190
Evan Cheng75972122011-01-13 07:58:56 +00004191 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
4192 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00004193 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00004194 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00004195 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004196 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00004197 }
4198}
4199
Jim Grosbach1355cf12011-07-26 17:10:22 +00004200// parsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
Evan Cheng75972122011-01-13 07:58:56 +00004201// :lower16: and :upper16:.
Jim Grosbach1355cf12011-07-26 17:10:22 +00004202bool ARMAsmParser::parsePrefix(ARMMCExpr::VariantKind &RefKind) {
Evan Cheng75972122011-01-13 07:58:56 +00004203 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00004204
4205 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00004206 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00004207 Parser.Lex(); // Eat ':'
4208
4209 if (getLexer().isNot(AsmToken::Identifier)) {
4210 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
4211 return true;
4212 }
4213
4214 StringRef IDVal = Parser.getTok().getIdentifier();
4215 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00004216 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00004217 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00004218 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00004219 } else {
4220 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
4221 return true;
4222 }
4223 Parser.Lex();
4224
4225 if (getLexer().isNot(AsmToken::Colon)) {
4226 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
4227 return true;
4228 }
4229 Parser.Lex(); // Eat the last ':'
4230 return false;
4231}
4232
Daniel Dunbar352e1482011-01-11 15:59:50 +00004233/// \brief Given a mnemonic, split out possible predication code and carry
4234/// setting letters to form a canonical mnemonic and flags.
4235//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00004236// FIXME: Would be nice to autogen this.
Jim Grosbach89df9962011-08-26 21:43:41 +00004237// FIXME: This is a bit of a maze of special cases.
Jim Grosbach1355cf12011-07-26 17:10:22 +00004238StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic,
Jim Grosbach5f160572011-07-19 20:10:31 +00004239 unsigned &PredicationCode,
4240 bool &CarrySetting,
Jim Grosbach89df9962011-08-26 21:43:41 +00004241 unsigned &ProcessorIMod,
4242 StringRef &ITMask) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00004243 PredicationCode = ARMCC::AL;
4244 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00004245 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00004246
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00004247 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00004248 //
4249 // FIXME: Would be nice to autogen this.
Jim Grosbach5f160572011-07-19 20:10:31 +00004250 if ((Mnemonic == "movs" && isThumb()) ||
4251 Mnemonic == "teq" || Mnemonic == "vceq" || Mnemonic == "svc" ||
4252 Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
4253 Mnemonic == "vmls" || Mnemonic == "vnmls" || Mnemonic == "vacge" ||
4254 Mnemonic == "vcge" || Mnemonic == "vclt" || Mnemonic == "vacgt" ||
4255 Mnemonic == "vcgt" || Mnemonic == "vcle" || Mnemonic == "smlal" ||
4256 Mnemonic == "umaal" || Mnemonic == "umlal" || Mnemonic == "vabal" ||
4257 Mnemonic == "vmlal" || Mnemonic == "vpadal" || Mnemonic == "vqdmlal")
Daniel Dunbar352e1482011-01-11 15:59:50 +00004258 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00004259
Jim Grosbach3f00e312011-07-11 17:09:57 +00004260 // First, split out any predication code. Ignore mnemonics we know aren't
4261 // predicated but do have a carry-set and so weren't caught above.
Jim Grosbachab40f4b2011-07-20 18:20:31 +00004262 if (Mnemonic != "adcs" && Mnemonic != "bics" && Mnemonic != "movs" &&
Jim Grosbach71725a02011-07-27 21:58:11 +00004263 Mnemonic != "muls" && Mnemonic != "smlals" && Mnemonic != "smulls" &&
Jim Grosbach04d55f12011-08-22 23:55:58 +00004264 Mnemonic != "umlals" && Mnemonic != "umulls" && Mnemonic != "lsls" &&
Jim Grosbach2f25d9b2011-09-01 18:22:13 +00004265 Mnemonic != "sbcs" && Mnemonic != "rscs") {
Jim Grosbach3f00e312011-07-11 17:09:57 +00004266 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
4267 .Case("eq", ARMCC::EQ)
4268 .Case("ne", ARMCC::NE)
4269 .Case("hs", ARMCC::HS)
4270 .Case("cs", ARMCC::HS)
4271 .Case("lo", ARMCC::LO)
4272 .Case("cc", ARMCC::LO)
4273 .Case("mi", ARMCC::MI)
4274 .Case("pl", ARMCC::PL)
4275 .Case("vs", ARMCC::VS)
4276 .Case("vc", ARMCC::VC)
4277 .Case("hi", ARMCC::HI)
4278 .Case("ls", ARMCC::LS)
4279 .Case("ge", ARMCC::GE)
4280 .Case("lt", ARMCC::LT)
4281 .Case("gt", ARMCC::GT)
4282 .Case("le", ARMCC::LE)
4283 .Case("al", ARMCC::AL)
4284 .Default(~0U);
4285 if (CC != ~0U) {
4286 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
4287 PredicationCode = CC;
4288 }
Bill Wendling52925b62010-10-29 23:50:21 +00004289 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00004290
Daniel Dunbar352e1482011-01-11 15:59:50 +00004291 // Next, determine if we have a carry setting bit. We explicitly ignore all
4292 // the instructions we know end in 's'.
4293 if (Mnemonic.endswith("s") &&
Jim Grosbach00f5d982011-08-17 22:49:09 +00004294 !(Mnemonic == "cps" || Mnemonic == "mls" ||
Jim Grosbach5f160572011-07-19 20:10:31 +00004295 Mnemonic == "mrs" || Mnemonic == "smmls" || Mnemonic == "vabs" ||
4296 Mnemonic == "vcls" || Mnemonic == "vmls" || Mnemonic == "vmrs" ||
4297 Mnemonic == "vnmls" || Mnemonic == "vqabs" || Mnemonic == "vrecps" ||
Jim Grosbach67ca1ad2011-12-08 00:49:29 +00004298 Mnemonic == "vrsqrts" || Mnemonic == "srs" || Mnemonic == "flds" ||
Jim Grosbach976c0da2011-12-08 22:51:25 +00004299 Mnemonic == "fmrs" || Mnemonic == "fsqrts" ||
Jim Grosbache1cf5902011-07-29 20:26:09 +00004300 (Mnemonic == "movs" && isThumb()))) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00004301 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
4302 CarrySetting = true;
4303 }
4304
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00004305 // The "cps" instruction can have a interrupt mode operand which is glued into
4306 // the mnemonic. Check if this is the case, split it and parse the imod op
4307 if (Mnemonic.startswith("cps")) {
4308 // Split out any imod code.
4309 unsigned IMod =
4310 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
4311 .Case("ie", ARM_PROC::IE)
4312 .Case("id", ARM_PROC::ID)
4313 .Default(~0U);
4314 if (IMod != ~0U) {
4315 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
4316 ProcessorIMod = IMod;
4317 }
4318 }
4319
Jim Grosbach89df9962011-08-26 21:43:41 +00004320 // The "it" instruction has the condition mask on the end of the mnemonic.
4321 if (Mnemonic.startswith("it")) {
4322 ITMask = Mnemonic.slice(2, Mnemonic.size());
4323 Mnemonic = Mnemonic.slice(0, 2);
4324 }
4325
Daniel Dunbar352e1482011-01-11 15:59:50 +00004326 return Mnemonic;
4327}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004328
4329/// \brief Given a canonical mnemonic, determine if the instruction ever allows
4330/// inclusion of carry set or predication code operands.
4331//
4332// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00004333void ARMAsmParser::
Jim Grosbach1355cf12011-07-26 17:10:22 +00004334getMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00004335 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00004336 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
4337 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
Jim Grosbach3443ed52011-09-16 18:05:48 +00004338 Mnemonic == "add" || Mnemonic == "adc" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00004339 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Jim Grosbachd5d0e812011-09-19 23:31:02 +00004340 Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00004341 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
Jim Grosbachd5d0e812011-09-19 23:31:02 +00004342 Mnemonic == "sbc" || Mnemonic == "eor" || Mnemonic == "neg" ||
Jim Grosbach3443ed52011-09-16 18:05:48 +00004343 (!isThumb() && (Mnemonic == "smull" || Mnemonic == "mov" ||
Jim Grosbachd5d0e812011-09-19 23:31:02 +00004344 Mnemonic == "mla" || Mnemonic == "smlal" ||
4345 Mnemonic == "umlal" || Mnemonic == "umull"))) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00004346 CanAcceptCarrySet = true;
Jim Grosbachfb9cffe2011-09-16 16:39:25 +00004347 } else
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00004348 CanAcceptCarrySet = false;
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004349
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00004350 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
4351 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
4352 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
4353 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Jim Grosbachad2dad92011-09-06 20:27:04 +00004354 Mnemonic == "dsb" || Mnemonic == "isb" || Mnemonic == "setend" ||
4355 (Mnemonic == "clrex" && !isThumb()) ||
Jim Grosbach0780b632011-08-19 23:24:36 +00004356 (Mnemonic == "nop" && isThumbOne()) ||
Jim Grosbach2bd01182011-10-11 21:55:36 +00004357 ((Mnemonic == "pld" || Mnemonic == "pli" || Mnemonic == "pldw" ||
4358 Mnemonic == "ldc2" || Mnemonic == "ldc2l" ||
4359 Mnemonic == "stc2" || Mnemonic == "stc2l") && !isThumb()) ||
Jim Grosbach4af54a42011-08-26 22:21:51 +00004360 ((Mnemonic.startswith("rfe") || Mnemonic.startswith("srs")) &&
4361 !isThumb()) ||
Jim Grosbach1ad60c22011-09-10 00:15:36 +00004362 Mnemonic.startswith("cps") || (Mnemonic == "movs" && isThumbOne())) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004363 CanAcceptPredicationCode = false;
Jim Grosbachfb9cffe2011-09-16 16:39:25 +00004364 } else
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004365 CanAcceptPredicationCode = true;
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00004366
Jim Grosbachfb9cffe2011-09-16 16:39:25 +00004367 if (isThumb()) {
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00004368 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00004369 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00004370 CanAcceptPredicationCode = false;
Jim Grosbachfb9cffe2011-09-16 16:39:25 +00004371 }
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00004372}
4373
Jim Grosbachd54b4e62011-08-16 21:12:37 +00004374bool ARMAsmParser::shouldOmitCCOutOperand(StringRef Mnemonic,
4375 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Jim Grosbach20ed2e72011-09-01 00:28:52 +00004376 // FIXME: This is all horribly hacky. We really need a better way to deal
4377 // with optional operands like this in the matcher table.
Jim Grosbachd54b4e62011-08-16 21:12:37 +00004378
4379 // The 'mov' mnemonic is special. One variant has a cc_out operand, while
4380 // another does not. Specifically, the MOVW instruction does not. So we
4381 // special case it here and remove the defaulted (non-setting) cc_out
4382 // operand if that's the instruction we're trying to match.
4383 //
4384 // We do this as post-processing of the explicit operands rather than just
4385 // conditionally adding the cc_out in the first place because we need
4386 // to check the type of the parsed immediate operand.
Owen Anderson8adf6202011-09-14 22:46:14 +00004387 if (Mnemonic == "mov" && Operands.size() > 4 && !isThumb() &&
Jim Grosbachd54b4e62011-08-16 21:12:37 +00004388 !static_cast<ARMOperand*>(Operands[4])->isARMSOImm() &&
4389 static_cast<ARMOperand*>(Operands[4])->isImm0_65535Expr() &&
4390 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
4391 return true;
Jim Grosbach3912b732011-08-16 21:34:08 +00004392
4393 // Register-register 'add' for thumb does not have a cc_out operand
4394 // when there are only two register operands.
4395 if (isThumb() && Mnemonic == "add" && Operands.size() == 5 &&
4396 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4397 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4398 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
4399 return true;
Jim Grosbach72f39f82011-08-24 21:22:15 +00004400 // Register-register 'add' for thumb does not have a cc_out operand
Jim Grosbach20ed2e72011-09-01 00:28:52 +00004401 // when it's an ADD Rdm, SP, {Rdm|#imm0_255} instruction. We do
4402 // have to check the immediate range here since Thumb2 has a variant
4403 // that can handle a different range and has a cc_out operand.
Jim Grosbachf67e8552011-09-16 22:58:42 +00004404 if (((isThumb() && Mnemonic == "add") ||
4405 (isThumbTwo() && Mnemonic == "sub")) &&
4406 Operands.size() == 6 &&
Jim Grosbach72f39f82011-08-24 21:22:15 +00004407 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4408 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4409 static_cast<ARMOperand*>(Operands[4])->getReg() == ARM::SP &&
Jim Grosbach20ed2e72011-09-01 00:28:52 +00004410 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
4411 (static_cast<ARMOperand*>(Operands[5])->isReg() ||
4412 static_cast<ARMOperand*>(Operands[5])->isImm0_1020s4()))
Jim Grosbach72f39f82011-08-24 21:22:15 +00004413 return true;
Jim Grosbachf67e8552011-09-16 22:58:42 +00004414 // For Thumb2, add/sub immediate does not have a cc_out operand for the
4415 // imm0_4095 variant. That's the least-preferred variant when
Jim Grosbach20ed2e72011-09-01 00:28:52 +00004416 // selecting via the generic "add" mnemonic, so to know that we
4417 // should remove the cc_out operand, we have to explicitly check that
4418 // it's not one of the other variants. Ugh.
Jim Grosbachf67e8552011-09-16 22:58:42 +00004419 if (isThumbTwo() && (Mnemonic == "add" || Mnemonic == "sub") &&
4420 Operands.size() == 6 &&
Jim Grosbach20ed2e72011-09-01 00:28:52 +00004421 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4422 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4423 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4424 // Nest conditions rather than one big 'if' statement for readability.
4425 //
4426 // If either register is a high reg, it's either one of the SP
4427 // variants (handled above) or a 32-bit encoding, so we just
4428 // check against T3.
4429 if ((!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
4430 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg())) &&
4431 static_cast<ARMOperand*>(Operands[5])->isT2SOImm())
4432 return false;
4433 // If both registers are low, we're in an IT block, and the immediate is
4434 // in range, we should use encoding T1 instead, which has a cc_out.
4435 if (inITBlock() &&
Jim Grosbach64944f42011-09-14 21:00:40 +00004436 isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) &&
Jim Grosbach20ed2e72011-09-01 00:28:52 +00004437 isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) &&
4438 static_cast<ARMOperand*>(Operands[5])->isImm0_7())
4439 return false;
4440
4441 // Otherwise, we use encoding T4, which does not have a cc_out
4442 // operand.
4443 return true;
4444 }
4445
Jim Grosbach64944f42011-09-14 21:00:40 +00004446 // The thumb2 multiply instruction doesn't have a CCOut register, so
4447 // if we have a "mul" mnemonic in Thumb mode, check if we'll be able to
4448 // use the 16-bit encoding or not.
4449 if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 6 &&
4450 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
4451 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4452 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4453 static_cast<ARMOperand*>(Operands[5])->isReg() &&
4454 // If the registers aren't low regs, the destination reg isn't the
4455 // same as one of the source regs, or the cc_out operand is zero
4456 // outside of an IT block, we have to use the 32-bit encoding, so
4457 // remove the cc_out operand.
4458 (!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
4459 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) ||
Jim Grosbach1de0bd12011-11-15 19:29:45 +00004460 !isARMLowRegister(static_cast<ARMOperand*>(Operands[5])->getReg()) ||
Jim Grosbach64944f42011-09-14 21:00:40 +00004461 !inITBlock() ||
4462 (static_cast<ARMOperand*>(Operands[3])->getReg() !=
4463 static_cast<ARMOperand*>(Operands[5])->getReg() &&
4464 static_cast<ARMOperand*>(Operands[3])->getReg() !=
4465 static_cast<ARMOperand*>(Operands[4])->getReg())))
4466 return true;
4467
Jim Grosbach7f1ec952011-11-15 19:55:16 +00004468 // Also check the 'mul' syntax variant that doesn't specify an explicit
4469 // destination register.
4470 if (isThumbTwo() && Mnemonic == "mul" && Operands.size() == 5 &&
4471 static_cast<ARMOperand*>(Operands[1])->getReg() == 0 &&
4472 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4473 static_cast<ARMOperand*>(Operands[4])->isReg() &&
4474 // If the registers aren't low regs or the cc_out operand is zero
4475 // outside of an IT block, we have to use the 32-bit encoding, so
4476 // remove the cc_out operand.
4477 (!isARMLowRegister(static_cast<ARMOperand*>(Operands[3])->getReg()) ||
4478 !isARMLowRegister(static_cast<ARMOperand*>(Operands[4])->getReg()) ||
4479 !inITBlock()))
4480 return true;
4481
Jim Grosbach64944f42011-09-14 21:00:40 +00004482
Jim Grosbach20ed2e72011-09-01 00:28:52 +00004483
Jim Grosbachf69c8042011-08-24 21:42:27 +00004484 // Register-register 'add/sub' for thumb does not have a cc_out operand
4485 // when it's an ADD/SUB SP, #imm. Be lenient on count since there's also
4486 // the "add/sub SP, SP, #imm" version. If the follow-up operands aren't
4487 // right, this will result in better diagnostics (which operand is off)
4488 // anyway.
4489 if (isThumb() && (Mnemonic == "add" || Mnemonic == "sub") &&
4490 (Operands.size() == 5 || Operands.size() == 6) &&
Jim Grosbach72f39f82011-08-24 21:22:15 +00004491 static_cast<ARMOperand*>(Operands[3])->isReg() &&
4492 static_cast<ARMOperand*>(Operands[3])->getReg() == ARM::SP &&
4493 static_cast<ARMOperand*>(Operands[1])->getReg() == 0)
4494 return true;
Jim Grosbach3912b732011-08-16 21:34:08 +00004495
Jim Grosbachd54b4e62011-08-16 21:12:37 +00004496 return false;
4497}
4498
Jim Grosbach7aef99b2011-11-11 23:08:10 +00004499static bool isDataTypeToken(StringRef Tok) {
4500 return Tok == ".8" || Tok == ".16" || Tok == ".32" || Tok == ".64" ||
4501 Tok == ".i8" || Tok == ".i16" || Tok == ".i32" || Tok == ".i64" ||
4502 Tok == ".u8" || Tok == ".u16" || Tok == ".u32" || Tok == ".u64" ||
4503 Tok == ".s8" || Tok == ".s16" || Tok == ".s32" || Tok == ".s64" ||
4504 Tok == ".p8" || Tok == ".p16" || Tok == ".f32" || Tok == ".f64" ||
4505 Tok == ".f" || Tok == ".d";
4506}
4507
4508// FIXME: This bit should probably be handled via an explicit match class
4509// in the .td files that matches the suffix instead of having it be
4510// a literal string token the way it is now.
4511static bool doesIgnoreDataTypeSuffix(StringRef Mnemonic, StringRef DT) {
4512 return Mnemonic.startswith("vldm") || Mnemonic.startswith("vstm");
4513}
4514
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00004515/// Parse an arm instruction mnemonic followed by its operands.
4516bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
4517 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
4518 // Create the leading tokens for the mnemonic, split by '.' characters.
4519 size_t Start = 0, Next = Name.find('.');
Jim Grosbachffa32252011-07-19 19:13:28 +00004520 StringRef Mnemonic = Name.slice(Start, Next);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00004521
Daniel Dunbar352e1482011-01-11 15:59:50 +00004522 // Split out the predication code and carry setting flag from the mnemonic.
4523 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00004524 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00004525 bool CarrySetting;
Jim Grosbach89df9962011-08-26 21:43:41 +00004526 StringRef ITMask;
Jim Grosbach1355cf12011-07-26 17:10:22 +00004527 Mnemonic = splitMnemonic(Mnemonic, PredicationCode, CarrySetting,
Jim Grosbach89df9962011-08-26 21:43:41 +00004528 ProcessorIMod, ITMask);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00004529
Jim Grosbach0c49ac02011-08-25 17:23:55 +00004530 // In Thumb1, only the branch (B) instruction can be predicated.
4531 if (isThumbOne() && PredicationCode != ARMCC::AL && Mnemonic != "b") {
4532 Parser.EatToEndOfStatement();
4533 return Error(NameLoc, "conditional execution not supported in Thumb1");
4534 }
4535
Jim Grosbachffa32252011-07-19 19:13:28 +00004536 Operands.push_back(ARMOperand::CreateToken(Mnemonic, NameLoc));
4537
Jim Grosbach89df9962011-08-26 21:43:41 +00004538 // Handle the IT instruction ITMask. Convert it to a bitmask. This
4539 // is the mask as it will be for the IT encoding if the conditional
4540 // encoding has a '1' as it's bit0 (i.e. 't' ==> '1'). In the case
4541 // where the conditional bit0 is zero, the instruction post-processing
4542 // will adjust the mask accordingly.
4543 if (Mnemonic == "it") {
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004544 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + 2);
4545 if (ITMask.size() > 3) {
4546 Parser.EatToEndOfStatement();
4547 return Error(Loc, "too many conditions on IT instruction");
4548 }
Jim Grosbach89df9962011-08-26 21:43:41 +00004549 unsigned Mask = 8;
4550 for (unsigned i = ITMask.size(); i != 0; --i) {
4551 char pos = ITMask[i - 1];
4552 if (pos != 't' && pos != 'e') {
4553 Parser.EatToEndOfStatement();
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004554 return Error(Loc, "illegal IT block condition mask '" + ITMask + "'");
Jim Grosbach89df9962011-08-26 21:43:41 +00004555 }
4556 Mask >>= 1;
4557 if (ITMask[i - 1] == 't')
4558 Mask |= 8;
4559 }
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004560 Operands.push_back(ARMOperand::CreateITMask(Mask, Loc));
Jim Grosbach89df9962011-08-26 21:43:41 +00004561 }
4562
Jim Grosbachffa32252011-07-19 19:13:28 +00004563 // FIXME: This is all a pretty gross hack. We should automatically handle
4564 // optional operands like this via tblgen.
Bill Wendling9717fa92010-11-21 10:56:05 +00004565
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004566 // Next, add the CCOut and ConditionCode operands, if needed.
4567 //
4568 // For mnemonics which can ever incorporate a carry setting bit or predication
4569 // code, our matching model involves us always generating CCOut and
4570 // ConditionCode operands to match the mnemonic "as written" and then we let
4571 // the matcher deal with finding the right instruction or generating an
4572 // appropriate error.
4573 bool CanAcceptCarrySet, CanAcceptPredicationCode;
Jim Grosbach1355cf12011-07-26 17:10:22 +00004574 getMnemonicAcceptInfo(Mnemonic, CanAcceptCarrySet, CanAcceptPredicationCode);
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004575
Jim Grosbach33c16a22011-07-14 22:04:21 +00004576 // If we had a carry-set on an instruction that can't do that, issue an
4577 // error.
4578 if (!CanAcceptCarrySet && CarrySetting) {
4579 Parser.EatToEndOfStatement();
Jim Grosbachffa32252011-07-19 19:13:28 +00004580 return Error(NameLoc, "instruction '" + Mnemonic +
Jim Grosbach33c16a22011-07-14 22:04:21 +00004581 "' can not set flags, but 's' suffix specified");
4582 }
Jim Grosbachc27d4f92011-07-22 17:44:50 +00004583 // If we had a predication code on an instruction that can't do that, issue an
4584 // error.
4585 if (!CanAcceptPredicationCode && PredicationCode != ARMCC::AL) {
4586 Parser.EatToEndOfStatement();
4587 return Error(NameLoc, "instruction '" + Mnemonic +
4588 "' is not predicable, but condition code specified");
4589 }
Jim Grosbach33c16a22011-07-14 22:04:21 +00004590
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004591 // Add the carry setting operand, if necessary.
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004592 if (CanAcceptCarrySet) {
4593 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size());
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004594 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004595 Loc));
4596 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004597
4598 // Add the predication code operand, if necessary.
4599 if (CanAcceptPredicationCode) {
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004600 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Mnemonic.size() +
4601 CarrySetting);
Daniel Dunbar3771dd02011-01-11 15:59:53 +00004602 Operands.push_back(ARMOperand::CreateCondCode(
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004603 ARMCC::CondCodes(PredicationCode), Loc));
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00004604 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00004605
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00004606 // Add the processor imod operand, if necessary.
4607 if (ProcessorIMod) {
4608 Operands.push_back(ARMOperand::CreateImm(
4609 MCConstantExpr::Create(ProcessorIMod, getContext()),
4610 NameLoc, NameLoc));
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00004611 }
4612
Daniel Dunbar345a9a62010-08-11 06:37:20 +00004613 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00004614 while (Next != StringRef::npos) {
4615 Start = Next;
4616 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00004617 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004618
Jim Grosbach7aef99b2011-11-11 23:08:10 +00004619 // Some NEON instructions have an optional datatype suffix that is
4620 // completely ignored. Check for that.
4621 if (isDataTypeToken(ExtraToken) &&
4622 doesIgnoreDataTypeSuffix(Mnemonic, ExtraToken))
4623 continue;
4624
Jim Grosbach81d2e392011-09-07 16:06:04 +00004625 if (ExtraToken != ".n") {
4626 SMLoc Loc = SMLoc::getFromPointer(NameLoc.getPointer() + Start);
4627 Operands.push_back(ARMOperand::CreateToken(ExtraToken, Loc));
4628 }
Daniel Dunbar5747b132010-08-11 06:37:16 +00004629 }
4630
4631 // Read the remaining operands.
4632 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004633 // Read the first operand.
Jim Grosbach1355cf12011-07-26 17:10:22 +00004634 if (parseOperand(Operands, Mnemonic)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00004635 Parser.EatToEndOfStatement();
4636 return true;
4637 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004638
4639 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00004640 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004641
4642 // Parse and remember the operand.
Jim Grosbach1355cf12011-07-26 17:10:22 +00004643 if (parseOperand(Operands, Mnemonic)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00004644 Parser.EatToEndOfStatement();
4645 return true;
4646 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00004647 }
4648 }
Jim Grosbach16c74252010-10-29 14:46:02 +00004649
Chris Lattnercbf8a982010-09-11 16:18:25 +00004650 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Jim Grosbach186ffac2011-10-07 18:27:04 +00004651 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00004652 Parser.EatToEndOfStatement();
Jim Grosbach186ffac2011-10-07 18:27:04 +00004653 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00004654 }
Bill Wendling146018f2010-11-06 21:42:12 +00004655
Chris Lattner34e53142010-09-08 05:10:46 +00004656 Parser.Lex(); // Consume the EndOfStatement
Jim Grosbachffa32252011-07-19 19:13:28 +00004657
Jim Grosbachd54b4e62011-08-16 21:12:37 +00004658 // Some instructions, mostly Thumb, have forms for the same mnemonic that
4659 // do and don't have a cc_out optional-def operand. With some spot-checks
4660 // of the operand list, we can figure out which variant we're trying to
Jim Grosbach20ed2e72011-09-01 00:28:52 +00004661 // parse and adjust accordingly before actually matching. We shouldn't ever
4662 // try to remove a cc_out operand that was explicitly set on the the
4663 // mnemonic, of course (CarrySetting == true). Reason number #317 the
4664 // table driven matcher doesn't fit well with the ARM instruction set.
4665 if (!CarrySetting && shouldOmitCCOutOperand(Mnemonic, Operands)) {
Jim Grosbachffa32252011-07-19 19:13:28 +00004666 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
4667 Operands.erase(Operands.begin() + 1);
4668 delete Op;
4669 }
4670
Jim Grosbachcf121c32011-07-28 21:57:55 +00004671 // ARM mode 'blx' need special handling, as the register operand version
4672 // is predicable, but the label operand version is not. So, we can't rely
4673 // on the Mnemonic based checking to correctly figure out when to put
Jim Grosbach21ff17c2011-10-07 23:24:09 +00004674 // a k_CondCode operand in the list. If we're trying to match the label
4675 // version, remove the k_CondCode operand here.
Jim Grosbachcf121c32011-07-28 21:57:55 +00004676 if (!isThumb() && Mnemonic == "blx" && Operands.size() == 3 &&
4677 static_cast<ARMOperand*>(Operands[2])->isImm()) {
4678 ARMOperand *Op = static_cast<ARMOperand*>(Operands[1]);
4679 Operands.erase(Operands.begin() + 1);
4680 delete Op;
4681 }
Jim Grosbach857e1a72011-08-11 23:51:13 +00004682
4683 // The vector-compare-to-zero instructions have a literal token "#0" at
4684 // the end that comes to here as an immediate operand. Convert it to a
4685 // token to play nicely with the matcher.
4686 if ((Mnemonic == "vceq" || Mnemonic == "vcge" || Mnemonic == "vcgt" ||
4687 Mnemonic == "vcle" || Mnemonic == "vclt") && Operands.size() == 6 &&
4688 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4689 ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
4690 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4691 if (CE && CE->getValue() == 0) {
4692 Operands.erase(Operands.begin() + 5);
4693 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4694 delete Op;
4695 }
4696 }
Jim Grosbach68259142011-10-03 22:30:24 +00004697 // VCMP{E} does the same thing, but with a different operand count.
4698 if ((Mnemonic == "vcmp" || Mnemonic == "vcmpe") && Operands.size() == 5 &&
4699 static_cast<ARMOperand*>(Operands[4])->isImm()) {
4700 ARMOperand *Op = static_cast<ARMOperand*>(Operands[4]);
4701 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4702 if (CE && CE->getValue() == 0) {
4703 Operands.erase(Operands.begin() + 4);
4704 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4705 delete Op;
4706 }
4707 }
Jim Grosbach934755a2011-08-22 23:47:13 +00004708 // Similarly, the Thumb1 "RSB" instruction has a literal "#0" on the
4709 // end. Convert it to a token here.
4710 if (Mnemonic == "rsb" && isThumb() && Operands.size() == 6 &&
4711 static_cast<ARMOperand*>(Operands[5])->isImm()) {
4712 ARMOperand *Op = static_cast<ARMOperand*>(Operands[5]);
4713 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Op->getImm());
4714 if (CE && CE->getValue() == 0) {
4715 Operands.erase(Operands.begin() + 5);
4716 Operands.push_back(ARMOperand::CreateToken("#0", Op->getStartLoc()));
4717 delete Op;
4718 }
4719 }
4720
Chris Lattner98986712010-01-14 22:21:20 +00004721 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00004722}
4723
Jim Grosbach189610f2011-07-26 18:25:39 +00004724// Validate context-sensitive operand constraints.
Jim Grosbachaa875f82011-08-23 18:13:04 +00004725
4726// return 'true' if register list contains non-low GPR registers,
4727// 'false' otherwise. If Reg is in the register list or is HiReg, set
4728// 'containsReg' to true.
4729static bool checkLowRegisterList(MCInst Inst, unsigned OpNo, unsigned Reg,
4730 unsigned HiReg, bool &containsReg) {
4731 containsReg = false;
4732 for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
4733 unsigned OpReg = Inst.getOperand(i).getReg();
4734 if (OpReg == Reg)
4735 containsReg = true;
4736 // Anything other than a low register isn't legal here.
4737 if (!isARMLowRegister(OpReg) && (!HiReg || OpReg != HiReg))
4738 return true;
4739 }
4740 return false;
4741}
4742
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00004743// Check if the specified regisgter is in the register list of the inst,
4744// starting at the indicated operand number.
4745static bool listContainsReg(MCInst &Inst, unsigned OpNo, unsigned Reg) {
4746 for (unsigned i = OpNo; i < Inst.getNumOperands(); ++i) {
4747 unsigned OpReg = Inst.getOperand(i).getReg();
4748 if (OpReg == Reg)
4749 return true;
4750 }
4751 return false;
4752}
4753
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004754// FIXME: We would really prefer to have MCInstrInfo (the wrapper around
4755// the ARMInsts array) instead. Getting that here requires awkward
4756// API changes, though. Better way?
4757namespace llvm {
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00004758extern const MCInstrDesc ARMInsts[];
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004759}
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00004760static const MCInstrDesc &getInstDesc(unsigned Opcode) {
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004761 return ARMInsts[Opcode];
4762}
4763
Jim Grosbach189610f2011-07-26 18:25:39 +00004764// FIXME: We would really like to be able to tablegen'erate this.
4765bool ARMAsmParser::
4766validateInstruction(MCInst &Inst,
4767 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00004768 const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004769 SMLoc Loc = Operands[0]->getStartLoc();
4770 // Check the IT block state first.
Owen Andersonb6b7f512011-09-13 17:59:19 +00004771 // NOTE: In Thumb mode, the BKPT instruction has the interesting property of
4772 // being allowed in IT blocks, but not being predicable. It just always
4773 // executes.
4774 if (inITBlock() && Inst.getOpcode() != ARM::tBKPT) {
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004775 unsigned bit = 1;
4776 if (ITState.FirstCond)
4777 ITState.FirstCond = false;
4778 else
Jim Grosbacha1109882011-09-02 23:22:08 +00004779 bit = (ITState.Mask >> (5 - ITState.CurPosition)) & 1;
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004780 // The instruction must be predicable.
4781 if (!MCID.isPredicable())
4782 return Error(Loc, "instructions in IT block must be predicable");
4783 unsigned Cond = Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm();
4784 unsigned ITCond = bit ? ITState.Cond :
4785 ARMCC::getOppositeCondition(ITState.Cond);
4786 if (Cond != ITCond) {
4787 // Find the condition code Operand to get its SMLoc information.
4788 SMLoc CondLoc;
4789 for (unsigned i = 1; i < Operands.size(); ++i)
4790 if (static_cast<ARMOperand*>(Operands[i])->isCondCode())
4791 CondLoc = Operands[i]->getStartLoc();
4792 return Error(CondLoc, "incorrect condition in IT block; got '" +
4793 StringRef(ARMCondCodeToString(ARMCC::CondCodes(Cond))) +
4794 "', but expected '" +
4795 ARMCondCodeToString(ARMCC::CondCodes(ITCond)) + "'");
4796 }
Jim Grosbachc9a9b442011-08-31 18:29:05 +00004797 // Check for non-'al' condition codes outside of the IT block.
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004798 } else if (isThumbTwo() && MCID.isPredicable() &&
4799 Inst.getOperand(MCID.findFirstPredOperandIdx()).getImm() !=
Owen Anderson51f6a7a2011-09-09 21:48:23 +00004800 ARMCC::AL && Inst.getOpcode() != ARM::tB &&
4801 Inst.getOpcode() != ARM::t2B)
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00004802 return Error(Loc, "predicated instructions must be in IT block");
4803
Jim Grosbach189610f2011-07-26 18:25:39 +00004804 switch (Inst.getOpcode()) {
Jim Grosbach2fd2b872011-08-10 20:29:19 +00004805 case ARM::LDRD:
4806 case ARM::LDRD_PRE:
4807 case ARM::LDRD_POST:
Jim Grosbach189610f2011-07-26 18:25:39 +00004808 case ARM::LDREXD: {
4809 // Rt2 must be Rt + 1.
4810 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
4811 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4812 if (Rt2 != Rt + 1)
4813 return Error(Operands[3]->getStartLoc(),
4814 "destination operands must be sequential");
4815 return false;
4816 }
Jim Grosbach14605d12011-08-11 20:28:23 +00004817 case ARM::STRD: {
4818 // Rt2 must be Rt + 1.
4819 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(0).getReg());
4820 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4821 if (Rt2 != Rt + 1)
4822 return Error(Operands[3]->getStartLoc(),
4823 "source operands must be sequential");
4824 return false;
4825 }
Jim Grosbach53642c52011-08-10 20:49:18 +00004826 case ARM::STRD_PRE:
4827 case ARM::STRD_POST:
Jim Grosbach189610f2011-07-26 18:25:39 +00004828 case ARM::STREXD: {
4829 // Rt2 must be Rt + 1.
4830 unsigned Rt = getARMRegisterNumbering(Inst.getOperand(1).getReg());
4831 unsigned Rt2 = getARMRegisterNumbering(Inst.getOperand(2).getReg());
4832 if (Rt2 != Rt + 1)
Jim Grosbach14605d12011-08-11 20:28:23 +00004833 return Error(Operands[3]->getStartLoc(),
Jim Grosbach189610f2011-07-26 18:25:39 +00004834 "source operands must be sequential");
4835 return false;
4836 }
Jim Grosbachfb8989e2011-07-27 21:09:25 +00004837 case ARM::SBFX:
4838 case ARM::UBFX: {
4839 // width must be in range [1, 32-lsb]
4840 unsigned lsb = Inst.getOperand(2).getImm();
4841 unsigned widthm1 = Inst.getOperand(3).getImm();
4842 if (widthm1 >= 32 - lsb)
4843 return Error(Operands[5]->getStartLoc(),
4844 "bitfield width must be in range [1,32-lsb]");
Jim Grosbach00c9a512011-08-16 21:42:31 +00004845 return false;
Jim Grosbachfb8989e2011-07-27 21:09:25 +00004846 }
Jim Grosbach93b3eff2011-08-18 21:50:53 +00004847 case ARM::tLDMIA: {
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00004848 // If we're parsing Thumb2, the .w variant is available and handles
4849 // most cases that are normally illegal for a Thumb1 LDM
4850 // instruction. We'll make the transformation in processInstruction()
4851 // if necessary.
4852 //
Jim Grosbach93b3eff2011-08-18 21:50:53 +00004853 // Thumb LDM instructions are writeback iff the base register is not
4854 // in the register list.
4855 unsigned Rn = Inst.getOperand(0).getReg();
Jim Grosbach7260c6a2011-08-22 23:01:07 +00004856 bool hasWritebackToken =
4857 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
4858 static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
Jim Grosbachaa875f82011-08-23 18:13:04 +00004859 bool listContainsBase;
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00004860 if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) && !isThumbTwo())
Jim Grosbachaa875f82011-08-23 18:13:04 +00004861 return Error(Operands[3 + hasWritebackToken]->getStartLoc(),
4862 "registers must be in range r0-r7");
Jim Grosbach93b3eff2011-08-18 21:50:53 +00004863 // If we should have writeback, then there should be a '!' token.
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00004864 if (!listContainsBase && !hasWritebackToken && !isThumbTwo())
Jim Grosbach93b3eff2011-08-18 21:50:53 +00004865 return Error(Operands[2]->getStartLoc(),
4866 "writeback operator '!' expected");
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00004867 // If we should not have writeback, there must not be a '!'. This is
4868 // true even for the 32-bit wide encodings.
Jim Grosbachaa875f82011-08-23 18:13:04 +00004869 if (listContainsBase && hasWritebackToken)
Jim Grosbach7260c6a2011-08-22 23:01:07 +00004870 return Error(Operands[3]->getStartLoc(),
4871 "writeback operator '!' not allowed when base register "
4872 "in register list");
Jim Grosbach93b3eff2011-08-18 21:50:53 +00004873
4874 break;
4875 }
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00004876 case ARM::t2LDMIA_UPD: {
4877 if (listContainsReg(Inst, 3, Inst.getOperand(0).getReg()))
4878 return Error(Operands[4]->getStartLoc(),
4879 "writeback operator '!' not allowed when base register "
4880 "in register list");
4881 break;
4882 }
Jim Grosbach54026372011-11-10 23:17:11 +00004883 // Like for ldm/stm, push and pop have hi-reg handling version in Thumb2,
4884 // so only issue a diagnostic for thumb1. The instructions will be
4885 // switched to the t2 encodings in processInstruction() if necessary.
Jim Grosbach6dcafc02011-08-22 23:17:34 +00004886 case ARM::tPOP: {
Jim Grosbachaa875f82011-08-23 18:13:04 +00004887 bool listContainsBase;
Jim Grosbach54026372011-11-10 23:17:11 +00004888 if (checkLowRegisterList(Inst, 2, 0, ARM::PC, listContainsBase) &&
4889 !isThumbTwo())
Jim Grosbachaa875f82011-08-23 18:13:04 +00004890 return Error(Operands[2]->getStartLoc(),
4891 "registers must be in range r0-r7 or pc");
Jim Grosbach6dcafc02011-08-22 23:17:34 +00004892 break;
4893 }
4894 case ARM::tPUSH: {
Jim Grosbachaa875f82011-08-23 18:13:04 +00004895 bool listContainsBase;
Jim Grosbach54026372011-11-10 23:17:11 +00004896 if (checkLowRegisterList(Inst, 2, 0, ARM::LR, listContainsBase) &&
4897 !isThumbTwo())
Jim Grosbachaa875f82011-08-23 18:13:04 +00004898 return Error(Operands[2]->getStartLoc(),
4899 "registers must be in range r0-r7 or lr");
Jim Grosbach6dcafc02011-08-22 23:17:34 +00004900 break;
4901 }
Jim Grosbach1e84f192011-08-23 18:15:37 +00004902 case ARM::tSTMIA_UPD: {
4903 bool listContainsBase;
Jim Grosbach8213c962011-09-16 20:50:13 +00004904 if (checkLowRegisterList(Inst, 4, 0, 0, listContainsBase) && !isThumbTwo())
Jim Grosbach1e84f192011-08-23 18:15:37 +00004905 return Error(Operands[4]->getStartLoc(),
4906 "registers must be in range r0-r7");
4907 break;
4908 }
Jim Grosbach189610f2011-07-26 18:25:39 +00004909 }
4910
4911 return false;
4912}
4913
Jim Grosbach84defb52011-12-02 22:34:51 +00004914static unsigned getRealVSTLNOpcode(unsigned Opc) {
4915 switch(Opc) {
4916 default: assert(0 && "unexpected opcode!");
4917 case ARM::VST1LNdWB_fixed_Asm_8: return ARM::VST1LNd8_UPD;
4918 case ARM::VST1LNdWB_fixed_Asm_P8: return ARM::VST1LNd8_UPD;
4919 case ARM::VST1LNdWB_fixed_Asm_I8: return ARM::VST1LNd8_UPD;
4920 case ARM::VST1LNdWB_fixed_Asm_S8: return ARM::VST1LNd8_UPD;
4921 case ARM::VST1LNdWB_fixed_Asm_U8: return ARM::VST1LNd8_UPD;
4922 case ARM::VST1LNdWB_fixed_Asm_16: return ARM::VST1LNd16_UPD;
4923 case ARM::VST1LNdWB_fixed_Asm_P16: return ARM::VST1LNd16_UPD;
4924 case ARM::VST1LNdWB_fixed_Asm_I16: return ARM::VST1LNd16_UPD;
4925 case ARM::VST1LNdWB_fixed_Asm_S16: return ARM::VST1LNd16_UPD;
4926 case ARM::VST1LNdWB_fixed_Asm_U16: return ARM::VST1LNd16_UPD;
4927 case ARM::VST1LNdWB_fixed_Asm_32: return ARM::VST1LNd32_UPD;
4928 case ARM::VST1LNdWB_fixed_Asm_F: return ARM::VST1LNd32_UPD;
4929 case ARM::VST1LNdWB_fixed_Asm_F32: return ARM::VST1LNd32_UPD;
4930 case ARM::VST1LNdWB_fixed_Asm_I32: return ARM::VST1LNd32_UPD;
4931 case ARM::VST1LNdWB_fixed_Asm_S32: return ARM::VST1LNd32_UPD;
4932 case ARM::VST1LNdWB_fixed_Asm_U32: return ARM::VST1LNd32_UPD;
4933 case ARM::VST1LNdWB_register_Asm_8: return ARM::VST1LNd8_UPD;
4934 case ARM::VST1LNdWB_register_Asm_P8: return ARM::VST1LNd8_UPD;
4935 case ARM::VST1LNdWB_register_Asm_I8: return ARM::VST1LNd8_UPD;
4936 case ARM::VST1LNdWB_register_Asm_S8: return ARM::VST1LNd8_UPD;
4937 case ARM::VST1LNdWB_register_Asm_U8: return ARM::VST1LNd8_UPD;
4938 case ARM::VST1LNdWB_register_Asm_16: return ARM::VST1LNd16_UPD;
4939 case ARM::VST1LNdWB_register_Asm_P16: return ARM::VST1LNd16_UPD;
4940 case ARM::VST1LNdWB_register_Asm_I16: return ARM::VST1LNd16_UPD;
4941 case ARM::VST1LNdWB_register_Asm_S16: return ARM::VST1LNd16_UPD;
4942 case ARM::VST1LNdWB_register_Asm_U16: return ARM::VST1LNd16_UPD;
4943 case ARM::VST1LNdWB_register_Asm_32: return ARM::VST1LNd32_UPD;
4944 case ARM::VST1LNdWB_register_Asm_F: return ARM::VST1LNd32_UPD;
4945 case ARM::VST1LNdWB_register_Asm_F32: return ARM::VST1LNd32_UPD;
4946 case ARM::VST1LNdWB_register_Asm_I32: return ARM::VST1LNd32_UPD;
4947 case ARM::VST1LNdWB_register_Asm_S32: return ARM::VST1LNd32_UPD;
4948 case ARM::VST1LNdWB_register_Asm_U32: return ARM::VST1LNd32_UPD;
4949 case ARM::VST1LNdAsm_8: return ARM::VST1LNd8;
4950 case ARM::VST1LNdAsm_P8: return ARM::VST1LNd8;
4951 case ARM::VST1LNdAsm_I8: return ARM::VST1LNd8;
4952 case ARM::VST1LNdAsm_S8: return ARM::VST1LNd8;
4953 case ARM::VST1LNdAsm_U8: return ARM::VST1LNd8;
4954 case ARM::VST1LNdAsm_16: return ARM::VST1LNd16;
4955 case ARM::VST1LNdAsm_P16: return ARM::VST1LNd16;
4956 case ARM::VST1LNdAsm_I16: return ARM::VST1LNd16;
4957 case ARM::VST1LNdAsm_S16: return ARM::VST1LNd16;
4958 case ARM::VST1LNdAsm_U16: return ARM::VST1LNd16;
4959 case ARM::VST1LNdAsm_32: return ARM::VST1LNd32;
4960 case ARM::VST1LNdAsm_F: return ARM::VST1LNd32;
4961 case ARM::VST1LNdAsm_F32: return ARM::VST1LNd32;
4962 case ARM::VST1LNdAsm_I32: return ARM::VST1LNd32;
4963 case ARM::VST1LNdAsm_S32: return ARM::VST1LNd32;
4964 case ARM::VST1LNdAsm_U32: return ARM::VST1LNd32;
4965 }
4966}
4967
4968static unsigned getRealVLDLNOpcode(unsigned Opc) {
Jim Grosbach7636bf62011-12-02 00:35:16 +00004969 switch(Opc) {
4970 default: assert(0 && "unexpected opcode!");
Jim Grosbach872eedb2011-12-02 22:01:52 +00004971 case ARM::VLD1LNdWB_fixed_Asm_8: return ARM::VLD1LNd8_UPD;
4972 case ARM::VLD1LNdWB_fixed_Asm_P8: return ARM::VLD1LNd8_UPD;
4973 case ARM::VLD1LNdWB_fixed_Asm_I8: return ARM::VLD1LNd8_UPD;
4974 case ARM::VLD1LNdWB_fixed_Asm_S8: return ARM::VLD1LNd8_UPD;
4975 case ARM::VLD1LNdWB_fixed_Asm_U8: return ARM::VLD1LNd8_UPD;
4976 case ARM::VLD1LNdWB_fixed_Asm_16: return ARM::VLD1LNd16_UPD;
4977 case ARM::VLD1LNdWB_fixed_Asm_P16: return ARM::VLD1LNd16_UPD;
4978 case ARM::VLD1LNdWB_fixed_Asm_I16: return ARM::VLD1LNd16_UPD;
4979 case ARM::VLD1LNdWB_fixed_Asm_S16: return ARM::VLD1LNd16_UPD;
4980 case ARM::VLD1LNdWB_fixed_Asm_U16: return ARM::VLD1LNd16_UPD;
4981 case ARM::VLD1LNdWB_fixed_Asm_32: return ARM::VLD1LNd32_UPD;
4982 case ARM::VLD1LNdWB_fixed_Asm_F: return ARM::VLD1LNd32_UPD;
4983 case ARM::VLD1LNdWB_fixed_Asm_F32: return ARM::VLD1LNd32_UPD;
4984 case ARM::VLD1LNdWB_fixed_Asm_I32: return ARM::VLD1LNd32_UPD;
4985 case ARM::VLD1LNdWB_fixed_Asm_S32: return ARM::VLD1LNd32_UPD;
4986 case ARM::VLD1LNdWB_fixed_Asm_U32: return ARM::VLD1LNd32_UPD;
4987 case ARM::VLD1LNdWB_register_Asm_8: return ARM::VLD1LNd8_UPD;
4988 case ARM::VLD1LNdWB_register_Asm_P8: return ARM::VLD1LNd8_UPD;
4989 case ARM::VLD1LNdWB_register_Asm_I8: return ARM::VLD1LNd8_UPD;
4990 case ARM::VLD1LNdWB_register_Asm_S8: return ARM::VLD1LNd8_UPD;
4991 case ARM::VLD1LNdWB_register_Asm_U8: return ARM::VLD1LNd8_UPD;
4992 case ARM::VLD1LNdWB_register_Asm_16: return ARM::VLD1LNd16_UPD;
4993 case ARM::VLD1LNdWB_register_Asm_P16: return ARM::VLD1LNd16_UPD;
4994 case ARM::VLD1LNdWB_register_Asm_I16: return ARM::VLD1LNd16_UPD;
4995 case ARM::VLD1LNdWB_register_Asm_S16: return ARM::VLD1LNd16_UPD;
4996 case ARM::VLD1LNdWB_register_Asm_U16: return ARM::VLD1LNd16_UPD;
4997 case ARM::VLD1LNdWB_register_Asm_32: return ARM::VLD1LNd32_UPD;
4998 case ARM::VLD1LNdWB_register_Asm_F: return ARM::VLD1LNd32_UPD;
4999 case ARM::VLD1LNdWB_register_Asm_F32: return ARM::VLD1LNd32_UPD;
5000 case ARM::VLD1LNdWB_register_Asm_I32: return ARM::VLD1LNd32_UPD;
5001 case ARM::VLD1LNdWB_register_Asm_S32: return ARM::VLD1LNd32_UPD;
5002 case ARM::VLD1LNdWB_register_Asm_U32: return ARM::VLD1LNd32_UPD;
Jim Grosbachdad2f8e2011-12-02 18:52:30 +00005003 case ARM::VLD1LNdAsm_8: return ARM::VLD1LNd8;
5004 case ARM::VLD1LNdAsm_P8: return ARM::VLD1LNd8;
5005 case ARM::VLD1LNdAsm_I8: return ARM::VLD1LNd8;
5006 case ARM::VLD1LNdAsm_S8: return ARM::VLD1LNd8;
5007 case ARM::VLD1LNdAsm_U8: return ARM::VLD1LNd8;
Jim Grosbach872eedb2011-12-02 22:01:52 +00005008 case ARM::VLD1LNdAsm_16: return ARM::VLD1LNd16;
5009 case ARM::VLD1LNdAsm_P16: return ARM::VLD1LNd16;
5010 case ARM::VLD1LNdAsm_I16: return ARM::VLD1LNd16;
5011 case ARM::VLD1LNdAsm_S16: return ARM::VLD1LNd16;
5012 case ARM::VLD1LNdAsm_U16: return ARM::VLD1LNd16;
Jim Grosbachdad2f8e2011-12-02 18:52:30 +00005013 case ARM::VLD1LNdAsm_32: return ARM::VLD1LNd32;
5014 case ARM::VLD1LNdAsm_F: return ARM::VLD1LNd32;
5015 case ARM::VLD1LNdAsm_F32: return ARM::VLD1LNd32;
5016 case ARM::VLD1LNdAsm_I32: return ARM::VLD1LNd32;
5017 case ARM::VLD1LNdAsm_S32: return ARM::VLD1LNd32;
5018 case ARM::VLD1LNdAsm_U32: return ARM::VLD1LNd32;
Jim Grosbach7636bf62011-12-02 00:35:16 +00005019 }
5020}
5021
Jim Grosbach83ec8772011-11-10 23:42:14 +00005022bool ARMAsmParser::
Jim Grosbachf8fce712011-08-11 17:35:48 +00005023processInstruction(MCInst &Inst,
5024 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
5025 switch (Inst.getOpcode()) {
Jim Grosbach84defb52011-12-02 22:34:51 +00005026 // Handle NEON VST1 complex aliases.
5027 case ARM::VST1LNdWB_register_Asm_8:
5028 case ARM::VST1LNdWB_register_Asm_P8:
5029 case ARM::VST1LNdWB_register_Asm_I8:
5030 case ARM::VST1LNdWB_register_Asm_S8:
5031 case ARM::VST1LNdWB_register_Asm_U8:
5032 case ARM::VST1LNdWB_register_Asm_16:
5033 case ARM::VST1LNdWB_register_Asm_P16:
5034 case ARM::VST1LNdWB_register_Asm_I16:
5035 case ARM::VST1LNdWB_register_Asm_S16:
5036 case ARM::VST1LNdWB_register_Asm_U16:
5037 case ARM::VST1LNdWB_register_Asm_32:
5038 case ARM::VST1LNdWB_register_Asm_F:
5039 case ARM::VST1LNdWB_register_Asm_F32:
5040 case ARM::VST1LNdWB_register_Asm_I32:
5041 case ARM::VST1LNdWB_register_Asm_S32:
5042 case ARM::VST1LNdWB_register_Asm_U32: {
5043 MCInst TmpInst;
5044 // Shuffle the operands around so the lane index operand is in the
5045 // right place.
5046 TmpInst.setOpcode(getRealVSTLNOpcode(Inst.getOpcode()));
5047 TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5048 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5049 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5050 TmpInst.addOperand(Inst.getOperand(4)); // Rm
5051 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5052 TmpInst.addOperand(Inst.getOperand(1)); // lane
5053 TmpInst.addOperand(Inst.getOperand(5)); // CondCode
5054 TmpInst.addOperand(Inst.getOperand(6));
5055 Inst = TmpInst;
5056 return true;
5057 }
5058 case ARM::VST1LNdWB_fixed_Asm_8:
5059 case ARM::VST1LNdWB_fixed_Asm_P8:
5060 case ARM::VST1LNdWB_fixed_Asm_I8:
5061 case ARM::VST1LNdWB_fixed_Asm_S8:
5062 case ARM::VST1LNdWB_fixed_Asm_U8:
5063 case ARM::VST1LNdWB_fixed_Asm_16:
5064 case ARM::VST1LNdWB_fixed_Asm_P16:
5065 case ARM::VST1LNdWB_fixed_Asm_I16:
5066 case ARM::VST1LNdWB_fixed_Asm_S16:
5067 case ARM::VST1LNdWB_fixed_Asm_U16:
5068 case ARM::VST1LNdWB_fixed_Asm_32:
5069 case ARM::VST1LNdWB_fixed_Asm_F:
5070 case ARM::VST1LNdWB_fixed_Asm_F32:
5071 case ARM::VST1LNdWB_fixed_Asm_I32:
5072 case ARM::VST1LNdWB_fixed_Asm_S32:
5073 case ARM::VST1LNdWB_fixed_Asm_U32: {
5074 MCInst TmpInst;
5075 // Shuffle the operands around so the lane index operand is in the
5076 // right place.
5077 TmpInst.setOpcode(getRealVSTLNOpcode(Inst.getOpcode()));
5078 TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5079 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5080 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5081 TmpInst.addOperand(MCOperand::CreateReg(0)); // Rm
5082 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5083 TmpInst.addOperand(Inst.getOperand(1)); // lane
5084 TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5085 TmpInst.addOperand(Inst.getOperand(5));
5086 Inst = TmpInst;
5087 return true;
5088 }
5089 case ARM::VST1LNdAsm_8:
5090 case ARM::VST1LNdAsm_P8:
5091 case ARM::VST1LNdAsm_I8:
5092 case ARM::VST1LNdAsm_S8:
5093 case ARM::VST1LNdAsm_U8:
5094 case ARM::VST1LNdAsm_16:
5095 case ARM::VST1LNdAsm_P16:
5096 case ARM::VST1LNdAsm_I16:
5097 case ARM::VST1LNdAsm_S16:
5098 case ARM::VST1LNdAsm_U16:
5099 case ARM::VST1LNdAsm_32:
5100 case ARM::VST1LNdAsm_F:
5101 case ARM::VST1LNdAsm_F32:
5102 case ARM::VST1LNdAsm_I32:
5103 case ARM::VST1LNdAsm_S32:
5104 case ARM::VST1LNdAsm_U32: {
5105 MCInst TmpInst;
5106 // Shuffle the operands around so the lane index operand is in the
5107 // right place.
5108 TmpInst.setOpcode(getRealVSTLNOpcode(Inst.getOpcode()));
5109 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5110 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5111 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5112 TmpInst.addOperand(Inst.getOperand(1)); // lane
5113 TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5114 TmpInst.addOperand(Inst.getOperand(5));
5115 Inst = TmpInst;
5116 return true;
5117 }
Jim Grosbach7636bf62011-12-02 00:35:16 +00005118 // Handle NEON VLD1 complex aliases.
Jim Grosbach872eedb2011-12-02 22:01:52 +00005119 case ARM::VLD1LNdWB_register_Asm_8:
5120 case ARM::VLD1LNdWB_register_Asm_P8:
5121 case ARM::VLD1LNdWB_register_Asm_I8:
5122 case ARM::VLD1LNdWB_register_Asm_S8:
5123 case ARM::VLD1LNdWB_register_Asm_U8:
5124 case ARM::VLD1LNdWB_register_Asm_16:
5125 case ARM::VLD1LNdWB_register_Asm_P16:
5126 case ARM::VLD1LNdWB_register_Asm_I16:
5127 case ARM::VLD1LNdWB_register_Asm_S16:
5128 case ARM::VLD1LNdWB_register_Asm_U16:
5129 case ARM::VLD1LNdWB_register_Asm_32:
5130 case ARM::VLD1LNdWB_register_Asm_F:
5131 case ARM::VLD1LNdWB_register_Asm_F32:
5132 case ARM::VLD1LNdWB_register_Asm_I32:
5133 case ARM::VLD1LNdWB_register_Asm_S32:
5134 case ARM::VLD1LNdWB_register_Asm_U32: {
5135 MCInst TmpInst;
5136 // Shuffle the operands around so the lane index operand is in the
5137 // right place.
Jim Grosbach84defb52011-12-02 22:34:51 +00005138 TmpInst.setOpcode(getRealVLDLNOpcode(Inst.getOpcode()));
Jim Grosbach872eedb2011-12-02 22:01:52 +00005139 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5140 TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5141 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5142 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5143 TmpInst.addOperand(Inst.getOperand(4)); // Rm
5144 TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
5145 TmpInst.addOperand(Inst.getOperand(1)); // lane
5146 TmpInst.addOperand(Inst.getOperand(5)); // CondCode
5147 TmpInst.addOperand(Inst.getOperand(6));
5148 Inst = TmpInst;
5149 return true;
5150 }
5151 case ARM::VLD1LNdWB_fixed_Asm_8:
5152 case ARM::VLD1LNdWB_fixed_Asm_P8:
5153 case ARM::VLD1LNdWB_fixed_Asm_I8:
5154 case ARM::VLD1LNdWB_fixed_Asm_S8:
5155 case ARM::VLD1LNdWB_fixed_Asm_U8:
5156 case ARM::VLD1LNdWB_fixed_Asm_16:
5157 case ARM::VLD1LNdWB_fixed_Asm_P16:
5158 case ARM::VLD1LNdWB_fixed_Asm_I16:
5159 case ARM::VLD1LNdWB_fixed_Asm_S16:
5160 case ARM::VLD1LNdWB_fixed_Asm_U16:
5161 case ARM::VLD1LNdWB_fixed_Asm_32:
5162 case ARM::VLD1LNdWB_fixed_Asm_F:
5163 case ARM::VLD1LNdWB_fixed_Asm_F32:
5164 case ARM::VLD1LNdWB_fixed_Asm_I32:
5165 case ARM::VLD1LNdWB_fixed_Asm_S32:
5166 case ARM::VLD1LNdWB_fixed_Asm_U32: {
5167 MCInst TmpInst;
5168 // Shuffle the operands around so the lane index operand is in the
5169 // right place.
Jim Grosbach84defb52011-12-02 22:34:51 +00005170 TmpInst.setOpcode(getRealVLDLNOpcode(Inst.getOpcode()));
Jim Grosbach872eedb2011-12-02 22:01:52 +00005171 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5172 TmpInst.addOperand(Inst.getOperand(2)); // Rn_wb
5173 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5174 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5175 TmpInst.addOperand(MCOperand::CreateReg(0)); // Rm
5176 TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
5177 TmpInst.addOperand(Inst.getOperand(1)); // lane
5178 TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5179 TmpInst.addOperand(Inst.getOperand(5));
5180 Inst = TmpInst;
5181 return true;
5182 }
Jim Grosbachdad2f8e2011-12-02 18:52:30 +00005183 case ARM::VLD1LNdAsm_8:
5184 case ARM::VLD1LNdAsm_P8:
5185 case ARM::VLD1LNdAsm_I8:
5186 case ARM::VLD1LNdAsm_S8:
5187 case ARM::VLD1LNdAsm_U8:
5188 case ARM::VLD1LNdAsm_16:
5189 case ARM::VLD1LNdAsm_P16:
5190 case ARM::VLD1LNdAsm_I16:
5191 case ARM::VLD1LNdAsm_S16:
5192 case ARM::VLD1LNdAsm_U16:
5193 case ARM::VLD1LNdAsm_32:
5194 case ARM::VLD1LNdAsm_F:
5195 case ARM::VLD1LNdAsm_F32:
5196 case ARM::VLD1LNdAsm_I32:
5197 case ARM::VLD1LNdAsm_S32:
5198 case ARM::VLD1LNdAsm_U32: {
Jim Grosbach7636bf62011-12-02 00:35:16 +00005199 MCInst TmpInst;
5200 // Shuffle the operands around so the lane index operand is in the
5201 // right place.
Jim Grosbach84defb52011-12-02 22:34:51 +00005202 TmpInst.setOpcode(getRealVLDLNOpcode(Inst.getOpcode()));
Jim Grosbach7636bf62011-12-02 00:35:16 +00005203 TmpInst.addOperand(Inst.getOperand(0)); // Vd
5204 TmpInst.addOperand(Inst.getOperand(2)); // Rn
5205 TmpInst.addOperand(Inst.getOperand(3)); // alignment
5206 TmpInst.addOperand(Inst.getOperand(0)); // Tied operand src (== Vd)
5207 TmpInst.addOperand(Inst.getOperand(1)); // lane
5208 TmpInst.addOperand(Inst.getOperand(4)); // CondCode
5209 TmpInst.addOperand(Inst.getOperand(5));
5210 Inst = TmpInst;
5211 return true;
5212 }
Jim Grosbach71810ab2011-11-10 16:44:55 +00005213 // Handle the MOV complex aliases.
Jim Grosbach23f22072011-11-16 18:31:45 +00005214 case ARM::ASRr:
5215 case ARM::LSRr:
5216 case ARM::LSLr:
5217 case ARM::RORr: {
5218 ARM_AM::ShiftOpc ShiftTy;
5219 switch(Inst.getOpcode()) {
5220 default: llvm_unreachable("unexpected opcode!");
5221 case ARM::ASRr: ShiftTy = ARM_AM::asr; break;
5222 case ARM::LSRr: ShiftTy = ARM_AM::lsr; break;
5223 case ARM::LSLr: ShiftTy = ARM_AM::lsl; break;
5224 case ARM::RORr: ShiftTy = ARM_AM::ror; break;
5225 }
5226 // A shift by zero is a plain MOVr, not a MOVsi.
5227 unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, 0);
5228 MCInst TmpInst;
5229 TmpInst.setOpcode(ARM::MOVsr);
5230 TmpInst.addOperand(Inst.getOperand(0)); // Rd
5231 TmpInst.addOperand(Inst.getOperand(1)); // Rn
5232 TmpInst.addOperand(Inst.getOperand(2)); // Rm
5233 TmpInst.addOperand(MCOperand::CreateImm(Shifter)); // Shift value and ty
5234 TmpInst.addOperand(Inst.getOperand(3)); // CondCode
5235 TmpInst.addOperand(Inst.getOperand(4));
5236 TmpInst.addOperand(Inst.getOperand(5)); // cc_out
5237 Inst = TmpInst;
5238 return true;
5239 }
Jim Grosbachee10ff82011-11-10 19:18:01 +00005240 case ARM::ASRi:
5241 case ARM::LSRi:
5242 case ARM::LSLi:
5243 case ARM::RORi: {
5244 ARM_AM::ShiftOpc ShiftTy;
Jim Grosbachee10ff82011-11-10 19:18:01 +00005245 switch(Inst.getOpcode()) {
5246 default: llvm_unreachable("unexpected opcode!");
5247 case ARM::ASRi: ShiftTy = ARM_AM::asr; break;
5248 case ARM::LSRi: ShiftTy = ARM_AM::lsr; break;
5249 case ARM::LSLi: ShiftTy = ARM_AM::lsl; break;
5250 case ARM::RORi: ShiftTy = ARM_AM::ror; break;
5251 }
5252 // A shift by zero is a plain MOVr, not a MOVsi.
Jim Grosbach48b368b2011-11-16 19:05:59 +00005253 unsigned Amt = Inst.getOperand(2).getImm();
Jim Grosbachee10ff82011-11-10 19:18:01 +00005254 unsigned Opc = Amt == 0 ? ARM::MOVr : ARM::MOVsi;
5255 unsigned Shifter = ARM_AM::getSORegOpc(ShiftTy, Amt);
Jim Grosbach71810ab2011-11-10 16:44:55 +00005256 MCInst TmpInst;
Jim Grosbachee10ff82011-11-10 19:18:01 +00005257 TmpInst.setOpcode(Opc);
Jim Grosbach71810ab2011-11-10 16:44:55 +00005258 TmpInst.addOperand(Inst.getOperand(0)); // Rd
5259 TmpInst.addOperand(Inst.getOperand(1)); // Rn
Jim Grosbachee10ff82011-11-10 19:18:01 +00005260 if (Opc == ARM::MOVsi)
5261 TmpInst.addOperand(MCOperand::CreateImm(Shifter)); // Shift value and ty
Jim Grosbach71810ab2011-11-10 16:44:55 +00005262 TmpInst.addOperand(Inst.getOperand(3)); // CondCode
5263 TmpInst.addOperand(Inst.getOperand(4));
5264 TmpInst.addOperand(Inst.getOperand(5)); // cc_out
5265 Inst = TmpInst;
Jim Grosbach83ec8772011-11-10 23:42:14 +00005266 return true;
Jim Grosbach71810ab2011-11-10 16:44:55 +00005267 }
Jim Grosbach48b368b2011-11-16 19:05:59 +00005268 case ARM::RRXi: {
5269 unsigned Shifter = ARM_AM::getSORegOpc(ARM_AM::rrx, 0);
5270 MCInst TmpInst;
5271 TmpInst.setOpcode(ARM::MOVsi);
5272 TmpInst.addOperand(Inst.getOperand(0)); // Rd
5273 TmpInst.addOperand(Inst.getOperand(1)); // Rn
5274 TmpInst.addOperand(MCOperand::CreateImm(Shifter)); // Shift value and ty
5275 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
5276 TmpInst.addOperand(Inst.getOperand(3));
5277 TmpInst.addOperand(Inst.getOperand(4)); // cc_out
5278 Inst = TmpInst;
5279 return true;
5280 }
Jim Grosbach0352b462011-11-10 23:58:34 +00005281 case ARM::t2LDMIA_UPD: {
5282 // If this is a load of a single register, then we should use
5283 // a post-indexed LDR instruction instead, per the ARM ARM.
5284 if (Inst.getNumOperands() != 5)
5285 return false;
5286 MCInst TmpInst;
5287 TmpInst.setOpcode(ARM::t2LDR_POST);
5288 TmpInst.addOperand(Inst.getOperand(4)); // Rt
5289 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
5290 TmpInst.addOperand(Inst.getOperand(1)); // Rn
5291 TmpInst.addOperand(MCOperand::CreateImm(4));
5292 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
5293 TmpInst.addOperand(Inst.getOperand(3));
5294 Inst = TmpInst;
5295 return true;
5296 }
5297 case ARM::t2STMDB_UPD: {
5298 // If this is a store of a single register, then we should use
5299 // a pre-indexed STR instruction instead, per the ARM ARM.
5300 if (Inst.getNumOperands() != 5)
5301 return false;
5302 MCInst TmpInst;
5303 TmpInst.setOpcode(ARM::t2STR_PRE);
5304 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
5305 TmpInst.addOperand(Inst.getOperand(4)); // Rt
5306 TmpInst.addOperand(Inst.getOperand(1)); // Rn
5307 TmpInst.addOperand(MCOperand::CreateImm(-4));
5308 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
5309 TmpInst.addOperand(Inst.getOperand(3));
5310 Inst = TmpInst;
5311 return true;
5312 }
Jim Grosbachf8fce712011-08-11 17:35:48 +00005313 case ARM::LDMIA_UPD:
5314 // If this is a load of a single register via a 'pop', then we should use
5315 // a post-indexed LDR instruction instead, per the ARM ARM.
5316 if (static_cast<ARMOperand*>(Operands[0])->getToken() == "pop" &&
5317 Inst.getNumOperands() == 5) {
5318 MCInst TmpInst;
5319 TmpInst.setOpcode(ARM::LDR_POST_IMM);
5320 TmpInst.addOperand(Inst.getOperand(4)); // Rt
5321 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
5322 TmpInst.addOperand(Inst.getOperand(1)); // Rn
5323 TmpInst.addOperand(MCOperand::CreateReg(0)); // am2offset
5324 TmpInst.addOperand(MCOperand::CreateImm(4));
5325 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
5326 TmpInst.addOperand(Inst.getOperand(3));
5327 Inst = TmpInst;
Jim Grosbach83ec8772011-11-10 23:42:14 +00005328 return true;
Jim Grosbachf8fce712011-08-11 17:35:48 +00005329 }
5330 break;
Jim Grosbachf6713912011-08-11 18:07:11 +00005331 case ARM::STMDB_UPD:
5332 // If this is a store of a single register via a 'push', then we should use
5333 // a pre-indexed STR instruction instead, per the ARM ARM.
5334 if (static_cast<ARMOperand*>(Operands[0])->getToken() == "push" &&
5335 Inst.getNumOperands() == 5) {
5336 MCInst TmpInst;
5337 TmpInst.setOpcode(ARM::STR_PRE_IMM);
5338 TmpInst.addOperand(Inst.getOperand(0)); // Rn_wb
5339 TmpInst.addOperand(Inst.getOperand(4)); // Rt
5340 TmpInst.addOperand(Inst.getOperand(1)); // addrmode_imm12
5341 TmpInst.addOperand(MCOperand::CreateImm(-4));
5342 TmpInst.addOperand(Inst.getOperand(2)); // CondCode
5343 TmpInst.addOperand(Inst.getOperand(3));
5344 Inst = TmpInst;
5345 }
5346 break;
Jim Grosbachda847862011-12-05 21:06:26 +00005347 case ARM::t2ADDri12:
5348 // If the immediate fits for encoding T3 (t2ADDri) and the generic "add"
5349 // mnemonic was used (not "addw"), encoding T3 is preferred.
5350 if (static_cast<ARMOperand*>(Operands[0])->getToken() != "add" ||
5351 ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
5352 break;
5353 Inst.setOpcode(ARM::t2ADDri);
5354 Inst.addOperand(MCOperand::CreateReg(0)); // cc_out
5355 break;
5356 case ARM::t2SUBri12:
5357 // If the immediate fits for encoding T3 (t2SUBri) and the generic "sub"
5358 // mnemonic was used (not "subw"), encoding T3 is preferred.
5359 if (static_cast<ARMOperand*>(Operands[0])->getToken() != "sub" ||
5360 ARM_AM::getT2SOImmVal(Inst.getOperand(2).getImm()) == -1)
5361 break;
5362 Inst.setOpcode(ARM::t2SUBri);
5363 Inst.addOperand(MCOperand::CreateReg(0)); // cc_out
5364 break;
Jim Grosbach89e2aa62011-08-16 23:57:34 +00005365 case ARM::tADDi8:
Jim Grosbach0f3abd82011-08-31 17:07:33 +00005366 // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
5367 // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
5368 // to encoding T2 if <Rd> is specified and encoding T2 is preferred
5369 // to encoding T1 if <Rd> is omitted."
Jim Grosbach83ec8772011-11-10 23:42:14 +00005370 if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
Jim Grosbach89e2aa62011-08-16 23:57:34 +00005371 Inst.setOpcode(ARM::tADDi3);
Jim Grosbach83ec8772011-11-10 23:42:14 +00005372 return true;
5373 }
Jim Grosbach89e2aa62011-08-16 23:57:34 +00005374 break;
Jim Grosbachf67e8552011-09-16 22:58:42 +00005375 case ARM::tSUBi8:
5376 // If the immediate is in the range 0-7, we want tADDi3 iff Rd was
5377 // explicitly specified. From the ARM ARM: "Encoding T1 is preferred
5378 // to encoding T2 if <Rd> is specified and encoding T2 is preferred
5379 // to encoding T1 if <Rd> is omitted."
Jim Grosbach83ec8772011-11-10 23:42:14 +00005380 if (Inst.getOperand(3).getImm() < 8 && Operands.size() == 6) {
Jim Grosbachf67e8552011-09-16 22:58:42 +00005381 Inst.setOpcode(ARM::tSUBi3);
Jim Grosbach83ec8772011-11-10 23:42:14 +00005382 return true;
5383 }
Jim Grosbachf67e8552011-09-16 22:58:42 +00005384 break;
Jim Grosbach927b9df2011-12-05 22:16:39 +00005385 case ARM::t2ADDrr: {
5386 // If the destination and first source operand are the same, and
5387 // there's no setting of the flags, use encoding T2 instead of T3.
5388 // Note that this is only for ADD, not SUB. This mirrors the system
5389 // 'as' behaviour. Make sure the wide encoding wasn't explicit.
5390 if (Inst.getOperand(0).getReg() != Inst.getOperand(1).getReg() ||
5391 Inst.getOperand(5).getReg() != 0 ||
Jim Grosbach713c7022011-12-05 22:27:04 +00005392 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
5393 static_cast<ARMOperand*>(Operands[3])->getToken() == ".w"))
Jim Grosbach927b9df2011-12-05 22:16:39 +00005394 break;
5395 MCInst TmpInst;
5396 TmpInst.setOpcode(ARM::tADDhirr);
5397 TmpInst.addOperand(Inst.getOperand(0));
5398 TmpInst.addOperand(Inst.getOperand(0));
5399 TmpInst.addOperand(Inst.getOperand(2));
5400 TmpInst.addOperand(Inst.getOperand(3));
5401 TmpInst.addOperand(Inst.getOperand(4));
5402 Inst = TmpInst;
5403 return true;
5404 }
Owen Anderson51f6a7a2011-09-09 21:48:23 +00005405 case ARM::tB:
5406 // A Thumb conditional branch outside of an IT block is a tBcc.
Jim Grosbach83ec8772011-11-10 23:42:14 +00005407 if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()) {
Owen Anderson51f6a7a2011-09-09 21:48:23 +00005408 Inst.setOpcode(ARM::tBcc);
Jim Grosbach83ec8772011-11-10 23:42:14 +00005409 return true;
5410 }
Owen Anderson51f6a7a2011-09-09 21:48:23 +00005411 break;
5412 case ARM::t2B:
5413 // A Thumb2 conditional branch outside of an IT block is a t2Bcc.
Jim Grosbach83ec8772011-11-10 23:42:14 +00005414 if (Inst.getOperand(1).getImm() != ARMCC::AL && !inITBlock()){
Owen Anderson51f6a7a2011-09-09 21:48:23 +00005415 Inst.setOpcode(ARM::t2Bcc);
Jim Grosbach83ec8772011-11-10 23:42:14 +00005416 return true;
5417 }
Owen Anderson51f6a7a2011-09-09 21:48:23 +00005418 break;
Jim Grosbachc0755102011-08-31 21:17:31 +00005419 case ARM::t2Bcc:
Jim Grosbacha1109882011-09-02 23:22:08 +00005420 // If the conditional is AL or we're in an IT block, we really want t2B.
Jim Grosbach83ec8772011-11-10 23:42:14 +00005421 if (Inst.getOperand(1).getImm() == ARMCC::AL || inITBlock()) {
Jim Grosbachc0755102011-08-31 21:17:31 +00005422 Inst.setOpcode(ARM::t2B);
Jim Grosbach83ec8772011-11-10 23:42:14 +00005423 return true;
5424 }
Jim Grosbachc0755102011-08-31 21:17:31 +00005425 break;
Jim Grosbach395b4532011-08-17 22:57:40 +00005426 case ARM::tBcc:
5427 // If the conditional is AL, we really want tB.
Jim Grosbach83ec8772011-11-10 23:42:14 +00005428 if (Inst.getOperand(1).getImm() == ARMCC::AL) {
Jim Grosbach395b4532011-08-17 22:57:40 +00005429 Inst.setOpcode(ARM::tB);
Jim Grosbach83ec8772011-11-10 23:42:14 +00005430 return true;
5431 }
Jim Grosbach3ce23d32011-08-18 16:08:39 +00005432 break;
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00005433 case ARM::tLDMIA: {
5434 // If the register list contains any high registers, or if the writeback
5435 // doesn't match what tLDMIA can do, we need to use the 32-bit encoding
5436 // instead if we're in Thumb2. Otherwise, this should have generated
5437 // an error in validateInstruction().
5438 unsigned Rn = Inst.getOperand(0).getReg();
5439 bool hasWritebackToken =
5440 (static_cast<ARMOperand*>(Operands[3])->isToken() &&
5441 static_cast<ARMOperand*>(Operands[3])->getToken() == "!");
5442 bool listContainsBase;
5443 if (checkLowRegisterList(Inst, 3, Rn, 0, listContainsBase) ||
5444 (!listContainsBase && !hasWritebackToken) ||
5445 (listContainsBase && hasWritebackToken)) {
5446 // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
5447 assert (isThumbTwo());
5448 Inst.setOpcode(hasWritebackToken ? ARM::t2LDMIA_UPD : ARM::t2LDMIA);
5449 // If we're switching to the updating version, we need to insert
5450 // the writeback tied operand.
5451 if (hasWritebackToken)
5452 Inst.insert(Inst.begin(),
5453 MCOperand::CreateReg(Inst.getOperand(0).getReg()));
Jim Grosbach83ec8772011-11-10 23:42:14 +00005454 return true;
Jim Grosbach76ecc3d2011-09-07 18:05:34 +00005455 }
5456 break;
5457 }
Jim Grosbach8213c962011-09-16 20:50:13 +00005458 case ARM::tSTMIA_UPD: {
5459 // If the register list contains any high registers, we need to use
5460 // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
5461 // should have generated an error in validateInstruction().
5462 unsigned Rn = Inst.getOperand(0).getReg();
5463 bool listContainsBase;
5464 if (checkLowRegisterList(Inst, 4, Rn, 0, listContainsBase)) {
5465 // 16-bit encoding isn't sufficient. Switch to the 32-bit version.
5466 assert (isThumbTwo());
5467 Inst.setOpcode(ARM::t2STMIA_UPD);
Jim Grosbach83ec8772011-11-10 23:42:14 +00005468 return true;
Jim Grosbach8213c962011-09-16 20:50:13 +00005469 }
5470 break;
5471 }
Jim Grosbach54026372011-11-10 23:17:11 +00005472 case ARM::tPOP: {
5473 bool listContainsBase;
5474 // If the register list contains any high registers, we need to use
5475 // the 32-bit encoding instead if we're in Thumb2. Otherwise, this
5476 // should have generated an error in validateInstruction().
5477 if (!checkLowRegisterList(Inst, 2, 0, ARM::PC, listContainsBase))
Jim Grosbach83ec8772011-11-10 23:42:14 +00005478 return false;
Jim Grosbach54026372011-11-10 23:17:11 +00005479 assert (isThumbTwo());
5480 Inst.setOpcode(ARM::t2LDMIA_UPD);
5481 // Add the base register and writeback operands.
5482 Inst.insert(Inst.begin(), MCOperand::CreateReg(ARM::SP));
5483 Inst.insert(Inst.begin(), MCOperand::CreateReg(ARM::SP));
Jim Grosbach83ec8772011-11-10 23:42:14 +00005484 return true;
Jim Grosbach54026372011-11-10 23:17:11 +00005485 }
5486 case ARM::tPUSH: {
5487 bool listContainsBase;
5488 if (!checkLowRegisterList(Inst, 2, 0, ARM::LR, listContainsBase))
Jim Grosbach83ec8772011-11-10 23:42:14 +00005489 return false;
Jim Grosbach54026372011-11-10 23:17:11 +00005490 assert (isThumbTwo());
5491 Inst.setOpcode(ARM::t2STMDB_UPD);
5492 // Add the base register and writeback operands.
5493 Inst.insert(Inst.begin(), MCOperand::CreateReg(ARM::SP));
5494 Inst.insert(Inst.begin(), MCOperand::CreateReg(ARM::SP));
Jim Grosbach83ec8772011-11-10 23:42:14 +00005495 return true;
Jim Grosbach54026372011-11-10 23:17:11 +00005496 }
Jim Grosbach1ad60c22011-09-10 00:15:36 +00005497 case ARM::t2MOVi: {
5498 // If we can use the 16-bit encoding and the user didn't explicitly
5499 // request the 32-bit variant, transform it here.
5500 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
5501 Inst.getOperand(1).getImm() <= 255 &&
Jim Grosbachc2d31642011-09-14 19:12:11 +00005502 ((!inITBlock() && Inst.getOperand(2).getImm() == ARMCC::AL &&
5503 Inst.getOperand(4).getReg() == ARM::CPSR) ||
5504 (inITBlock() && Inst.getOperand(4).getReg() == 0)) &&
Jim Grosbach1ad60c22011-09-10 00:15:36 +00005505 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
5506 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
5507 // The operands aren't in the same order for tMOVi8...
5508 MCInst TmpInst;
5509 TmpInst.setOpcode(ARM::tMOVi8);
5510 TmpInst.addOperand(Inst.getOperand(0));
5511 TmpInst.addOperand(Inst.getOperand(4));
5512 TmpInst.addOperand(Inst.getOperand(1));
5513 TmpInst.addOperand(Inst.getOperand(2));
5514 TmpInst.addOperand(Inst.getOperand(3));
5515 Inst = TmpInst;
Jim Grosbach83ec8772011-11-10 23:42:14 +00005516 return true;
Jim Grosbach1ad60c22011-09-10 00:15:36 +00005517 }
5518 break;
5519 }
5520 case ARM::t2MOVr: {
5521 // If we can use the 16-bit encoding and the user didn't explicitly
5522 // request the 32-bit variant, transform it here.
5523 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
5524 isARMLowRegister(Inst.getOperand(1).getReg()) &&
5525 Inst.getOperand(2).getImm() == ARMCC::AL &&
5526 Inst.getOperand(4).getReg() == ARM::CPSR &&
5527 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
5528 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
5529 // The operands aren't the same for tMOV[S]r... (no cc_out)
5530 MCInst TmpInst;
5531 TmpInst.setOpcode(Inst.getOperand(4).getReg() ? ARM::tMOVSr : ARM::tMOVr);
5532 TmpInst.addOperand(Inst.getOperand(0));
5533 TmpInst.addOperand(Inst.getOperand(1));
5534 TmpInst.addOperand(Inst.getOperand(2));
5535 TmpInst.addOperand(Inst.getOperand(3));
5536 Inst = TmpInst;
Jim Grosbach83ec8772011-11-10 23:42:14 +00005537 return true;
Jim Grosbach1ad60c22011-09-10 00:15:36 +00005538 }
5539 break;
5540 }
Jim Grosbach326efe52011-09-19 20:29:33 +00005541 case ARM::t2SXTH:
Jim Grosbach50f1c372011-09-20 00:46:54 +00005542 case ARM::t2SXTB:
5543 case ARM::t2UXTH:
5544 case ARM::t2UXTB: {
Jim Grosbach326efe52011-09-19 20:29:33 +00005545 // If we can use the 16-bit encoding and the user didn't explicitly
5546 // request the 32-bit variant, transform it here.
5547 if (isARMLowRegister(Inst.getOperand(0).getReg()) &&
5548 isARMLowRegister(Inst.getOperand(1).getReg()) &&
5549 Inst.getOperand(2).getImm() == 0 &&
5550 (!static_cast<ARMOperand*>(Operands[2])->isToken() ||
5551 static_cast<ARMOperand*>(Operands[2])->getToken() != ".w")) {
Jim Grosbach50f1c372011-09-20 00:46:54 +00005552 unsigned NewOpc;
5553 switch (Inst.getOpcode()) {
5554 default: llvm_unreachable("Illegal opcode!");
5555 case ARM::t2SXTH: NewOpc = ARM::tSXTH; break;
5556 case ARM::t2SXTB: NewOpc = ARM::tSXTB; break;
5557 case ARM::t2UXTH: NewOpc = ARM::tUXTH; break;
5558 case ARM::t2UXTB: NewOpc = ARM::tUXTB; break;
5559 }
Jim Grosbach326efe52011-09-19 20:29:33 +00005560 // The operands aren't the same for thumb1 (no rotate operand).
5561 MCInst TmpInst;
5562 TmpInst.setOpcode(NewOpc);
5563 TmpInst.addOperand(Inst.getOperand(0));
5564 TmpInst.addOperand(Inst.getOperand(1));
5565 TmpInst.addOperand(Inst.getOperand(3));
5566 TmpInst.addOperand(Inst.getOperand(4));
5567 Inst = TmpInst;
Jim Grosbach83ec8772011-11-10 23:42:14 +00005568 return true;
Jim Grosbach326efe52011-09-19 20:29:33 +00005569 }
5570 break;
5571 }
Jim Grosbach89df9962011-08-26 21:43:41 +00005572 case ARM::t2IT: {
5573 // The mask bits for all but the first condition are represented as
5574 // the low bit of the condition code value implies 't'. We currently
5575 // always have 1 implies 't', so XOR toggle the bits if the low bit
5576 // of the condition code is zero. The encoding also expects the low
5577 // bit of the condition to be encoded as bit 4 of the mask operand,
5578 // so mask that in if needed
5579 MCOperand &MO = Inst.getOperand(1);
5580 unsigned Mask = MO.getImm();
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00005581 unsigned OrigMask = Mask;
5582 unsigned TZ = CountTrailingZeros_32(Mask);
Jim Grosbach89df9962011-08-26 21:43:41 +00005583 if ((Inst.getOperand(0).getImm() & 1) == 0) {
Jim Grosbach89df9962011-08-26 21:43:41 +00005584 assert(Mask && TZ <= 3 && "illegal IT mask value!");
5585 for (unsigned i = 3; i != TZ; --i)
5586 Mask ^= 1 << i;
5587 } else
5588 Mask |= 0x10;
5589 MO.setImm(Mask);
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00005590
5591 // Set up the IT block state according to the IT instruction we just
5592 // matched.
5593 assert(!inITBlock() && "nested IT blocks?!");
5594 ITState.Cond = ARMCC::CondCodes(Inst.getOperand(0).getImm());
5595 ITState.Mask = OrigMask; // Use the original mask, not the updated one.
5596 ITState.CurPosition = 0;
5597 ITState.FirstCond = true;
Jim Grosbach89df9962011-08-26 21:43:41 +00005598 break;
5599 }
Jim Grosbachf8fce712011-08-11 17:35:48 +00005600 }
Jim Grosbach83ec8772011-11-10 23:42:14 +00005601 return false;
Jim Grosbachf8fce712011-08-11 17:35:48 +00005602}
5603
Jim Grosbach47a0d522011-08-16 20:45:50 +00005604unsigned ARMAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
5605 // 16-bit thumb arithmetic instructions either require or preclude the 'S'
5606 // suffix depending on whether they're in an IT block or not.
Jim Grosbach194bd892011-08-16 22:20:01 +00005607 unsigned Opc = Inst.getOpcode();
Benjamin Kramer1a2f9882011-10-22 16:50:00 +00005608 const MCInstrDesc &MCID = getInstDesc(Opc);
Jim Grosbach47a0d522011-08-16 20:45:50 +00005609 if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
5610 assert(MCID.hasOptionalDef() &&
5611 "optionally flag setting instruction missing optional def operand");
5612 assert(MCID.NumOperands == Inst.getNumOperands() &&
5613 "operand count mismatch!");
5614 // Find the optional-def operand (cc_out).
5615 unsigned OpNo;
5616 for (OpNo = 0;
5617 !MCID.OpInfo[OpNo].isOptionalDef() && OpNo < MCID.NumOperands;
5618 ++OpNo)
5619 ;
5620 // If we're parsing Thumb1, reject it completely.
5621 if (isThumbOne() && Inst.getOperand(OpNo).getReg() != ARM::CPSR)
5622 return Match_MnemonicFail;
5623 // If we're parsing Thumb2, which form is legal depends on whether we're
5624 // in an IT block.
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00005625 if (isThumbTwo() && Inst.getOperand(OpNo).getReg() != ARM::CPSR &&
5626 !inITBlock())
Jim Grosbach47a0d522011-08-16 20:45:50 +00005627 return Match_RequiresITBlock;
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00005628 if (isThumbTwo() && Inst.getOperand(OpNo).getReg() == ARM::CPSR &&
5629 inITBlock())
5630 return Match_RequiresNotITBlock;
Jim Grosbach47a0d522011-08-16 20:45:50 +00005631 }
Jim Grosbach194bd892011-08-16 22:20:01 +00005632 // Some high-register supporting Thumb1 encodings only allow both registers
5633 // to be from r0-r7 when in Thumb2.
5634 else if (Opc == ARM::tADDhirr && isThumbOne() &&
5635 isARMLowRegister(Inst.getOperand(1).getReg()) &&
5636 isARMLowRegister(Inst.getOperand(2).getReg()))
5637 return Match_RequiresThumb2;
5638 // Others only require ARMv6 or later.
Jim Grosbach4ec6e882011-08-19 20:46:54 +00005639 else if (Opc == ARM::tMOVr && isThumbOne() && !hasV6Ops() &&
Jim Grosbach194bd892011-08-16 22:20:01 +00005640 isARMLowRegister(Inst.getOperand(0).getReg()) &&
5641 isARMLowRegister(Inst.getOperand(1).getReg()))
5642 return Match_RequiresV6;
Jim Grosbach47a0d522011-08-16 20:45:50 +00005643 return Match_Success;
5644}
5645
Chris Lattnerfa42fad2010-10-28 21:28:01 +00005646bool ARMAsmParser::
5647MatchAndEmitInstruction(SMLoc IDLoc,
5648 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
5649 MCStreamer &Out) {
5650 MCInst Inst;
5651 unsigned ErrorInfo;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00005652 unsigned MatchResult;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00005653 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00005654 switch (MatchResult) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00005655 default: break;
Chris Lattnere73d4f82010-10-28 21:41:58 +00005656 case Match_Success:
Jim Grosbach189610f2011-07-26 18:25:39 +00005657 // Context sensitive operand constraints aren't handled by the matcher,
5658 // so check them here.
Jim Grosbacha1109882011-09-02 23:22:08 +00005659 if (validateInstruction(Inst, Operands)) {
5660 // Still progress the IT block, otherwise one wrong condition causes
5661 // nasty cascading errors.
5662 forwardITPosition();
Jim Grosbach189610f2011-07-26 18:25:39 +00005663 return true;
Jim Grosbacha1109882011-09-02 23:22:08 +00005664 }
Jim Grosbach189610f2011-07-26 18:25:39 +00005665
Jim Grosbachf8fce712011-08-11 17:35:48 +00005666 // Some instructions need post-processing to, for example, tweak which
Jim Grosbach83ec8772011-11-10 23:42:14 +00005667 // encoding is selected. Loop on it while changes happen so the
5668 // individual transformations can chain off each other. E.g.,
5669 // tPOP(r8)->t2LDMIA_UPD(sp,r8)->t2STR_POST(sp,r8)
5670 while (processInstruction(Inst, Operands))
5671 ;
Jim Grosbachf8fce712011-08-11 17:35:48 +00005672
Jim Grosbacha1109882011-09-02 23:22:08 +00005673 // Only move forward at the very end so that everything in validate
5674 // and process gets a consistent answer about whether we're in an IT
5675 // block.
5676 forwardITPosition();
5677
Chris Lattnerfa42fad2010-10-28 21:28:01 +00005678 Out.EmitInstruction(Inst);
5679 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00005680 case Match_MissingFeature:
5681 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
5682 return true;
5683 case Match_InvalidOperand: {
5684 SMLoc ErrorLoc = IDLoc;
5685 if (ErrorInfo != ~0U) {
5686 if (ErrorInfo >= Operands.size())
5687 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00005688
Chris Lattnere73d4f82010-10-28 21:41:58 +00005689 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
5690 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
5691 }
Jim Grosbach16c74252010-10-29 14:46:02 +00005692
Chris Lattnere73d4f82010-10-28 21:41:58 +00005693 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00005694 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00005695 case Match_MnemonicFail:
Jim Grosbach47a0d522011-08-16 20:45:50 +00005696 return Error(IDLoc, "invalid instruction");
Daniel Dunbarb4129152011-02-04 17:12:23 +00005697 case Match_ConversionFail:
Jim Grosbach88ae2bc2011-08-19 22:07:46 +00005698 // The converter function will have already emited a diagnostic.
5699 return true;
Jim Grosbachf8e1e3e2011-08-29 22:24:09 +00005700 case Match_RequiresNotITBlock:
5701 return Error(IDLoc, "flag setting instruction only valid outside IT block");
Jim Grosbach47a0d522011-08-16 20:45:50 +00005702 case Match_RequiresITBlock:
5703 return Error(IDLoc, "instruction only valid inside IT block");
Jim Grosbach194bd892011-08-16 22:20:01 +00005704 case Match_RequiresV6:
5705 return Error(IDLoc, "instruction variant requires ARMv6 or later");
5706 case Match_RequiresThumb2:
5707 return Error(IDLoc, "instruction variant requires Thumb2");
Chris Lattnere73d4f82010-10-28 21:41:58 +00005708 }
Jim Grosbach16c74252010-10-29 14:46:02 +00005709
Eric Christopherc223e2b2010-10-29 09:26:59 +00005710 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00005711 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00005712}
5713
Jim Grosbach1355cf12011-07-26 17:10:22 +00005714/// parseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00005715bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
5716 StringRef IDVal = DirectiveID.getIdentifier();
5717 if (IDVal == ".word")
Jim Grosbach1355cf12011-07-26 17:10:22 +00005718 return parseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00005719 else if (IDVal == ".thumb")
Jim Grosbach1355cf12011-07-26 17:10:22 +00005720 return parseDirectiveThumb(DirectiveID.getLoc());
Jim Grosbach9a70df92011-12-07 18:04:19 +00005721 else if (IDVal == ".arm")
5722 return parseDirectiveARM(DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00005723 else if (IDVal == ".thumb_func")
Jim Grosbach1355cf12011-07-26 17:10:22 +00005724 return parseDirectiveThumbFunc(DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00005725 else if (IDVal == ".code")
Jim Grosbach1355cf12011-07-26 17:10:22 +00005726 return parseDirectiveCode(DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00005727 else if (IDVal == ".syntax")
Jim Grosbach1355cf12011-07-26 17:10:22 +00005728 return parseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00005729 return true;
5730}
5731
Jim Grosbach1355cf12011-07-26 17:10:22 +00005732/// parseDirectiveWord
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00005733/// ::= .word [ expression (, expression)* ]
Jim Grosbach1355cf12011-07-26 17:10:22 +00005734bool ARMAsmParser::parseDirectiveWord(unsigned Size, SMLoc L) {
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00005735 if (getLexer().isNot(AsmToken::EndOfStatement)) {
5736 for (;;) {
5737 const MCExpr *Value;
5738 if (getParser().ParseExpression(Value))
5739 return true;
5740
Chris Lattneraaec2052010-01-19 19:46:13 +00005741 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00005742
5743 if (getLexer().is(AsmToken::EndOfStatement))
5744 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00005745
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00005746 // FIXME: Improve diagnostic.
5747 if (getLexer().isNot(AsmToken::Comma))
5748 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00005749 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00005750 }
5751 }
5752
Sean Callananb9a25b72010-01-19 20:27:46 +00005753 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00005754 return false;
5755}
5756
Jim Grosbach1355cf12011-07-26 17:10:22 +00005757/// parseDirectiveThumb
Kevin Enderby515d5092009-10-15 20:48:48 +00005758/// ::= .thumb
Jim Grosbach1355cf12011-07-26 17:10:22 +00005759bool ARMAsmParser::parseDirectiveThumb(SMLoc L) {
Kevin Enderby515d5092009-10-15 20:48:48 +00005760 if (getLexer().isNot(AsmToken::EndOfStatement))
5761 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00005762 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00005763
Jim Grosbach9a70df92011-12-07 18:04:19 +00005764 if (!isThumb())
5765 SwitchMode();
5766 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
5767 return false;
5768}
5769
5770/// parseDirectiveARM
5771/// ::= .arm
5772bool ARMAsmParser::parseDirectiveARM(SMLoc L) {
5773 if (getLexer().isNot(AsmToken::EndOfStatement))
5774 return Error(L, "unexpected token in directive");
5775 Parser.Lex();
5776
5777 if (isThumb())
5778 SwitchMode();
5779 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderby515d5092009-10-15 20:48:48 +00005780 return false;
5781}
5782
Jim Grosbach1355cf12011-07-26 17:10:22 +00005783/// parseDirectiveThumbFunc
Kevin Enderby515d5092009-10-15 20:48:48 +00005784/// ::= .thumbfunc symbol_name
Jim Grosbach1355cf12011-07-26 17:10:22 +00005785bool ARMAsmParser::parseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00005786 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
5787 bool isMachO = MAI.hasSubsectionsViaSymbols();
5788 StringRef Name;
5789
5790 // Darwin asm has function name after .thumb_func direction
5791 // ELF doesn't
5792 if (isMachO) {
5793 const AsmToken &Tok = Parser.getTok();
5794 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
5795 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbachd475f862011-11-10 20:48:53 +00005796 Name = Tok.getIdentifier();
Rafael Espindola64695402011-05-16 16:17:21 +00005797 Parser.Lex(); // Consume the identifier token.
5798 }
5799
Jim Grosbachd475f862011-11-10 20:48:53 +00005800 if (getLexer().isNot(AsmToken::EndOfStatement))
Kevin Enderby515d5092009-10-15 20:48:48 +00005801 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00005802 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00005803
Rafael Espindola64695402011-05-16 16:17:21 +00005804 // FIXME: assuming function name will be the line following .thumb_func
5805 if (!isMachO) {
Jim Grosbachd475f862011-11-10 20:48:53 +00005806 Name = Parser.getTok().getIdentifier();
Rafael Espindola64695402011-05-16 16:17:21 +00005807 }
5808
Jim Grosbach642fc9c2010-11-05 22:33:53 +00005809 // Mark symbol as a thumb symbol.
5810 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
5811 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00005812 return false;
5813}
5814
Jim Grosbach1355cf12011-07-26 17:10:22 +00005815/// parseDirectiveSyntax
Kevin Enderby515d5092009-10-15 20:48:48 +00005816/// ::= .syntax unified | divided
Jim Grosbach1355cf12011-07-26 17:10:22 +00005817bool ARMAsmParser::parseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00005818 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00005819 if (Tok.isNot(AsmToken::Identifier))
5820 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00005821 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00005822 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00005823 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00005824 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00005825 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00005826 else
5827 return Error(L, "unrecognized syntax mode in .syntax directive");
5828
5829 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00005830 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00005831 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00005832
5833 // TODO tell the MC streamer the mode
5834 // getParser().getStreamer().Emit???();
5835 return false;
5836}
5837
Jim Grosbach1355cf12011-07-26 17:10:22 +00005838/// parseDirectiveCode
Kevin Enderby515d5092009-10-15 20:48:48 +00005839/// ::= .code 16 | 32
Jim Grosbach1355cf12011-07-26 17:10:22 +00005840bool ARMAsmParser::parseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00005841 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00005842 if (Tok.isNot(AsmToken::Integer))
5843 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00005844 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00005845 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00005846 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00005847 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00005848 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00005849 else
5850 return Error(L, "invalid operand to .code directive");
5851
5852 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00005853 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00005854 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00005855
Evan Cheng32869202011-07-08 22:36:29 +00005856 if (Val == 16) {
Jim Grosbach98447da2011-09-06 18:46:23 +00005857 if (!isThumb())
Evan Chengffc0e732011-07-09 05:47:46 +00005858 SwitchMode();
Jim Grosbach98447da2011-09-06 18:46:23 +00005859 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Evan Cheng32869202011-07-08 22:36:29 +00005860 } else {
Jim Grosbach98447da2011-09-06 18:46:23 +00005861 if (isThumb())
Evan Chengffc0e732011-07-09 05:47:46 +00005862 SwitchMode();
Jim Grosbach98447da2011-09-06 18:46:23 +00005863 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Evan Chengeb0caa12011-07-08 22:49:55 +00005864 }
Jim Grosbach2a301702010-11-05 22:40:53 +00005865
Kevin Enderby515d5092009-10-15 20:48:48 +00005866 return false;
5867}
5868
Sean Callanan90b70972010-04-07 20:29:34 +00005869extern "C" void LLVMInitializeARMAsmLexer();
5870
Kevin Enderby9c41fa82009-10-30 22:55:57 +00005871/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00005872extern "C" void LLVMInitializeARMAsmParser() {
Evan Cheng94b95502011-07-26 00:24:13 +00005873 RegisterMCAsmParser<ARMAsmParser> X(TheARMTarget);
5874 RegisterMCAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00005875 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00005876}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00005877
Chris Lattner0692ee62010-09-06 19:11:01 +00005878#define GET_REGISTER_MATCHER
5879#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00005880#include "ARMGenAsmMatcher.inc"