blob: 9438ee177576e0b60478d222dc97088951988578 [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
10#include "ARM.h"
Bill Wendling92b5a2e2010-11-03 01:49:29 +000011#include "ARMAddressingModes.h"
Evan Cheng75972122011-01-13 07:58:56 +000012#include "ARMMCExpr.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000013#include "ARMBaseRegisterInfo.h"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000014#include "ARMSubtarget.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000015#include "llvm/MC/MCParser/MCAsmLexer.h"
16#include "llvm/MC/MCParser/MCAsmParser.h"
17#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Rafael Espindola64695402011-05-16 16:17:21 +000018#include "llvm/MC/MCAsmInfo.h"
Jim Grosbach642fc9c2010-11-05 22:33:53 +000019#include "llvm/MC/MCContext.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000020#include "llvm/MC/MCStreamer.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000023#include "llvm/MC/MCSubtargetInfo.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000024#include "llvm/Target/TargetRegistry.h"
25#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000026#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000027#include "llvm/Support/raw_ostream.h"
Benjamin Kramer75ca4b92011-07-08 21:06:23 +000028#include "llvm/ADT/OwningPtr.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000029#include "llvm/ADT/SmallVector.h"
Owen Anderson0c9f2502011-01-13 22:50:36 +000030#include "llvm/ADT/StringExtras.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000031#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000032#include "llvm/ADT/Twine.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000033
34#define GET_SUBTARGETINFO_ENUM
35#include "ARMGenSubtargetInfo.inc"
36
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000037using namespace llvm;
38
Chris Lattner3a697562010-10-28 17:20:03 +000039namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000040
41class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000042
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000043class ARMAsmParser : public TargetAsmParser {
44 MCAsmParser &Parser;
Benjamin Kramer75ca4b92011-07-08 21:06:23 +000045 OwningPtr<const MCSubtargetInfo> STI;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000046
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000047 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000048 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
49
50 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000051 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
52
Chris Lattnere5658fa2010-10-30 04:09:10 +000053 int TryParseRegister();
Roman Divackybf755322011-01-27 17:14:22 +000054 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Bill Wendling50d0f582010-11-18 23:43:05 +000055 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
Owen Anderson00828302011-03-18 22:50:18 +000056 bool TryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
Bill Wendling50d0f582010-11-18 23:43:05 +000057 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +000058 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &,
59 ARMII::AddrMode AddrMode);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +000060 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
Evan Cheng75972122011-01-13 07:58:56 +000061 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000062 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
63 MCSymbolRefExpr::VariantKind Variant);
64
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000065
Kevin Enderby9c41fa82009-10-30 22:55:57 +000066 bool ParseMemoryOffsetReg(bool &Negative,
67 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +000068 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +000069 const MCExpr *&ShiftAmount,
70 const MCExpr *&Offset,
71 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000072 int &OffsetRegNum,
73 SMLoc &E);
Owen Anderson00828302011-03-18 22:50:18 +000074 bool ParseShift(enum ARM_AM::ShiftOpc &St,
75 const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000076 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000077 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000078 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000079 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000080 bool ParseDirectiveSyntax(SMLoc L);
81
Chris Lattner7036f8b2010-09-29 01:42:58 +000082 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000083 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000084 MCStreamer &Out);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000085 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
86 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000087
Evan Chengebdeeab2011-07-08 01:53:10 +000088 bool isThumb() const {
89 // FIXME: Can tablegen auto-generate this?
90 return (STI->getFeatureBits() & ARM::ModeThumb) != 0;
91 }
92
93 bool isThumbOne() const {
94 return isThumb() && (STI->getFeatureBits() & ARM::FeatureThumb2) == 0;
95 }
96
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000097 /// @name Auto-generated Match Functions
98 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000099
Chris Lattner0692ee62010-09-06 19:11:01 +0000100#define GET_ASSEMBLER_HEADER
101#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000102
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000103 /// }
104
Jim Grosbachf922c472011-02-12 01:34:40 +0000105 OperandMatchResultTy tryParseCoprocNumOperand(
106 SmallVectorImpl<MCParsedAsmOperand*>&);
107 OperandMatchResultTy tryParseCoprocRegOperand(
108 SmallVectorImpl<MCParsedAsmOperand*>&);
109 OperandMatchResultTy tryParseMemBarrierOptOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000110 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000111 OperandMatchResultTy tryParseProcIFlagsOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000112 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000113 OperandMatchResultTy tryParseMSRMaskOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000114 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000115 OperandMatchResultTy tryParseMemMode2Operand(
116 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000117 OperandMatchResultTy tryParseMemMode3Operand(
118 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000119
120 // Asm Match Converter Methods
121 bool CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
122 const SmallVectorImpl<MCParsedAsmOperand*> &);
123 bool CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
124 const SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000125 bool CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
126 const SmallVectorImpl<MCParsedAsmOperand*> &);
127 bool CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
128 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbachf922c472011-02-12 01:34:40 +0000129
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000130public:
Evan Cheng480cee52011-07-08 19:33:14 +0000131 ARMAsmParser(StringRef TT, StringRef CPU, StringRef FS, MCAsmParser &_Parser)
Benjamin Kramer75ca4b92011-07-08 21:06:23 +0000132 : TargetAsmParser(), Parser(_Parser),
133 STI(ARM_MC::createARMMCSubtargetInfo(TT, CPU, FS)) {
Evan Chengebdeeab2011-07-08 01:53:10 +0000134
135 MCAsmParserExtension::Initialize(_Parser);
136 // Initialize the set of available features.
137 setAvailableFeatures(ComputeAvailableFeatures(STI->getFeatureBits()));
138 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000139
Benjamin Kramer38e59892010-07-14 22:38:02 +0000140 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000141 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000142 virtual bool ParseDirective(AsmToken DirectiveID);
143};
Jim Grosbach16c74252010-10-29 14:46:02 +0000144} // end anonymous namespace
145
Chris Lattner3a697562010-10-28 17:20:03 +0000146namespace {
147
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000148/// ARMOperand - Instances of this class represent a parsed ARM machine
149/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000150class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000151 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000152 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000153 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000154 CoprocNum,
155 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000156 Immediate,
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000157 MemBarrierOpt,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000158 Memory,
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000159 MSRMask,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000160 ProcIFlags,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000161 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000162 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000163 DPRRegisterList,
164 SPRRegisterList,
Owen Anderson00828302011-03-18 22:50:18 +0000165 Shifter,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000166 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000167 } Kind;
168
Sean Callanan76264762010-04-02 22:27:05 +0000169 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000170 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000171
172 union {
173 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000174 ARMCC::CondCodes Val;
175 } CC;
176
177 struct {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000178 ARM_MB::MemBOpt Val;
179 } MBOpt;
180
181 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000182 unsigned Val;
183 } Cop;
184
185 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000186 ARM_PROC::IFlags Val;
187 } IFlags;
188
189 struct {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000190 unsigned Val;
191 } MMask;
192
193 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000194 const char *Data;
195 unsigned Length;
196 } Tok;
197
198 struct {
199 unsigned RegNum;
200 } Reg;
201
Bill Wendling8155e5b2010-11-06 22:19:43 +0000202 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000203 const MCExpr *Val;
204 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000205
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000206 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000207 struct {
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000208 ARMII::AddrMode AddrMode;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000209 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000210 union {
211 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
212 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
213 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000214 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Owen Anderson00828302011-03-18 22:50:18 +0000215 enum ARM_AM::ShiftOpc ShiftType; // used when OffsetRegShifted is true
Bill Wendling146018f2010-11-06 21:42:12 +0000216 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000217 unsigned Preindexed : 1;
218 unsigned Postindexed : 1;
219 unsigned OffsetIsReg : 1;
220 unsigned Negative : 1; // only used when OffsetIsReg is true
221 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000222 } Mem;
Owen Anderson00828302011-03-18 22:50:18 +0000223
224 struct {
225 ARM_AM::ShiftOpc ShiftTy;
226 unsigned RegNum;
227 } Shift;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000228 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000229
Bill Wendling146018f2010-11-06 21:42:12 +0000230 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
231public:
Sean Callanan76264762010-04-02 22:27:05 +0000232 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
233 Kind = o.Kind;
234 StartLoc = o.StartLoc;
235 EndLoc = o.EndLoc;
236 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000237 case CondCode:
238 CC = o.CC;
239 break;
Sean Callanan76264762010-04-02 22:27:05 +0000240 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000241 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000242 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000243 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000244 case Register:
245 Reg = o.Reg;
246 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000247 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000248 case DPRRegisterList:
249 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000250 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000251 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000252 case CoprocNum:
253 case CoprocReg:
254 Cop = o.Cop;
255 break;
Sean Callanan76264762010-04-02 22:27:05 +0000256 case Immediate:
257 Imm = o.Imm;
258 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000259 case MemBarrierOpt:
260 MBOpt = o.MBOpt;
261 break;
Sean Callanan76264762010-04-02 22:27:05 +0000262 case Memory:
263 Mem = o.Mem;
264 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000265 case MSRMask:
266 MMask = o.MMask;
267 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000268 case ProcIFlags:
269 IFlags = o.IFlags;
Owen Anderson00828302011-03-18 22:50:18 +0000270 break;
271 case Shifter:
272 Shift = o.Shift;
273 break;
Sean Callanan76264762010-04-02 22:27:05 +0000274 }
275 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000276
Sean Callanan76264762010-04-02 22:27:05 +0000277 /// getStartLoc - Get the location of the first token of this operand.
278 SMLoc getStartLoc() const { return StartLoc; }
279 /// getEndLoc - Get the location of the last token of this operand.
280 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000281
Daniel Dunbar8462b302010-08-11 06:36:53 +0000282 ARMCC::CondCodes getCondCode() const {
283 assert(Kind == CondCode && "Invalid access!");
284 return CC.Val;
285 }
286
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000287 unsigned getCoproc() const {
288 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
289 return Cop.Val;
290 }
291
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000292 StringRef getToken() const {
293 assert(Kind == Token && "Invalid access!");
294 return StringRef(Tok.Data, Tok.Length);
295 }
296
297 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000298 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000299 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000300 }
301
Bill Wendling5fa22a12010-11-09 23:28:44 +0000302 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000303 assert((Kind == RegisterList || Kind == DPRRegisterList ||
304 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000305 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000306 }
307
Kevin Enderbycfe07242009-10-13 22:19:02 +0000308 const MCExpr *getImm() const {
309 assert(Kind == Immediate && "Invalid access!");
310 return Imm.Val;
311 }
312
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000313 ARM_MB::MemBOpt getMemBarrierOpt() const {
314 assert(Kind == MemBarrierOpt && "Invalid access!");
315 return MBOpt.Val;
316 }
317
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000318 ARM_PROC::IFlags getProcIFlags() const {
319 assert(Kind == ProcIFlags && "Invalid access!");
320 return IFlags.Val;
321 }
322
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000323 unsigned getMSRMask() const {
324 assert(Kind == MSRMask && "Invalid access!");
325 return MMask.Val;
326 }
327
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000328 /// @name Memory Operand Accessors
329 /// @{
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000330 ARMII::AddrMode getMemAddrMode() const {
331 return Mem.AddrMode;
332 }
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000333 unsigned getMemBaseRegNum() const {
334 return Mem.BaseRegNum;
335 }
336 unsigned getMemOffsetRegNum() const {
337 assert(Mem.OffsetIsReg && "Invalid access!");
338 return Mem.Offset.RegNum;
339 }
340 const MCExpr *getMemOffset() const {
341 assert(!Mem.OffsetIsReg && "Invalid access!");
342 return Mem.Offset.Value;
343 }
344 unsigned getMemOffsetRegShifted() const {
345 assert(Mem.OffsetIsReg && "Invalid access!");
346 return Mem.OffsetRegShifted;
347 }
348 const MCExpr *getMemShiftAmount() const {
349 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
350 return Mem.ShiftAmount;
351 }
Owen Anderson00828302011-03-18 22:50:18 +0000352 enum ARM_AM::ShiftOpc getMemShiftType() const {
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000353 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
354 return Mem.ShiftType;
355 }
356 bool getMemPreindexed() const { return Mem.Preindexed; }
357 bool getMemPostindexed() const { return Mem.Postindexed; }
358 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
359 bool getMemNegative() const { return Mem.Negative; }
360 bool getMemWriteback() const { return Mem.Writeback; }
361
362 /// @}
363
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000364 bool isCoprocNum() const { return Kind == CoprocNum; }
365 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000366 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000367 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000368 bool isImm() const { return Kind == Immediate; }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000369 bool isImm0_255() const {
370 if (Kind != Immediate)
371 return false;
372 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
373 if (!CE) return false;
374 int64_t Value = CE->getValue();
375 return Value >= 0 && Value < 256;
376 }
377 bool isT2SOImm() const {
378 if (Kind != Immediate)
379 return false;
380 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
381 if (!CE) return false;
382 int64_t Value = CE->getValue();
383 return ARM_AM::getT2SOImmVal(Value) != -1;
384 }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000385 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000386 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000387 bool isDPRRegList() const { return Kind == DPRRegisterList; }
388 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000389 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000390 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000391 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000392 bool isShifter() const { return Kind == Shifter; }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000393 bool isMemMode2() const {
394 if (getMemAddrMode() != ARMII::AddrMode2)
395 return false;
396
397 if (getMemOffsetIsReg())
398 return true;
399
400 if (getMemNegative() &&
401 !(getMemPostindexed() || getMemPreindexed()))
402 return false;
403
404 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
405 if (!CE) return false;
406 int64_t Value = CE->getValue();
407
408 // The offset must be in the range 0-4095 (imm12).
409 if (Value > 4095 || Value < -4095)
410 return false;
411
412 return true;
413 }
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000414 bool isMemMode3() const {
415 if (getMemAddrMode() != ARMII::AddrMode3)
416 return false;
417
418 if (getMemOffsetIsReg()) {
419 if (getMemOffsetRegShifted())
420 return false; // No shift with offset reg allowed
421 return true;
422 }
423
424 if (getMemNegative() &&
425 !(getMemPostindexed() || getMemPreindexed()))
426 return false;
427
428 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
429 if (!CE) return false;
430 int64_t Value = CE->getValue();
431
432 // The offset must be in the range 0-255 (imm8).
433 if (Value > 255 || Value < -255)
434 return false;
435
436 return true;
437 }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000438 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000439 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
440 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000441 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000442
Daniel Dunbar4b462672011-01-18 05:55:27 +0000443 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000444 if (!CE) return false;
445
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000446 // The offset must be a multiple of 4 in the range 0-1020.
447 int64_t Value = CE->getValue();
448 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
449 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000450 bool isMemMode7() const {
451 if (!isMemory() ||
452 getMemPreindexed() ||
453 getMemPostindexed() ||
454 getMemOffsetIsReg() ||
455 getMemNegative() ||
456 getMemWriteback())
457 return false;
458
459 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
460 if (!CE) return false;
461
462 if (CE->getValue())
463 return false;
464
465 return true;
466 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000467 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000468 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000469 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000470 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000471 }
472 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000473 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000474 return false;
475
Daniel Dunbar4b462672011-01-18 05:55:27 +0000476 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000477 if (!CE) return false;
478
479 // The offset must be a multiple of 4 in the range 0-124.
480 uint64_t Value = CE->getValue();
481 return ((Value & 0x3) == 0 && Value <= 124);
482 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000483 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000484 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000485
486 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000487 // Add as immediates when possible. Null MCExpr = 0.
488 if (Expr == 0)
489 Inst.addOperand(MCOperand::CreateImm(0));
490 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000491 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
492 else
493 Inst.addOperand(MCOperand::CreateExpr(Expr));
494 }
495
Daniel Dunbar8462b302010-08-11 06:36:53 +0000496 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000497 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000498 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000499 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
500 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000501 }
502
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000503 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
504 assert(N == 1 && "Invalid number of operands!");
505 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
506 }
507
508 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
509 assert(N == 1 && "Invalid number of operands!");
510 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
511 }
512
Jim Grosbachd67641b2010-12-06 18:21:12 +0000513 void addCCOutOperands(MCInst &Inst, unsigned N) const {
514 assert(N == 1 && "Invalid number of operands!");
515 Inst.addOperand(MCOperand::CreateReg(getReg()));
516 }
517
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000518 void addRegOperands(MCInst &Inst, unsigned N) const {
519 assert(N == 1 && "Invalid number of operands!");
520 Inst.addOperand(MCOperand::CreateReg(getReg()));
521 }
522
Owen Anderson00828302011-03-18 22:50:18 +0000523 void addShifterOperands(MCInst &Inst, unsigned N) const {
524 assert(N == 1 && "Invalid number of operands!");
525 Inst.addOperand(MCOperand::CreateImm(
526 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
527 }
528
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000529 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000530 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000531 const SmallVectorImpl<unsigned> &RegList = getRegList();
532 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000533 I = RegList.begin(), E = RegList.end(); I != E; ++I)
534 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000535 }
536
Bill Wendling0f630752010-11-17 04:32:08 +0000537 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
538 addRegListOperands(Inst, N);
539 }
540
541 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
542 addRegListOperands(Inst, N);
543 }
544
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000545 void addImmOperands(MCInst &Inst, unsigned N) const {
546 assert(N == 1 && "Invalid number of operands!");
547 addExpr(Inst, getImm());
548 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000549
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000550 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
551 assert(N == 1 && "Invalid number of operands!");
552 addExpr(Inst, getImm());
553 }
554
555 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
556 assert(N == 1 && "Invalid number of operands!");
557 addExpr(Inst, getImm());
558 }
559
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000560 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
561 assert(N == 1 && "Invalid number of operands!");
562 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
563 }
564
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000565 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
566 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
567 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
568
569 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000570 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000571 assert((CE || CE->getValue() == 0) &&
572 "No offset operand support in mode 7");
573 }
574
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000575 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
576 assert(isMemMode2() && "Invalid mode or number of operands!");
577 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
578 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
579
580 if (getMemOffsetIsReg()) {
581 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
582
583 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
584 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
585 int64_t ShiftAmount = 0;
586
587 if (getMemOffsetRegShifted()) {
588 ShOpc = getMemShiftType();
589 const MCConstantExpr *CE =
590 dyn_cast<MCConstantExpr>(getMemShiftAmount());
591 ShiftAmount = CE->getValue();
592 }
593
594 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
595 ShOpc, IdxMode)));
596 return;
597 }
598
599 // Create a operand placeholder to always yield the same number of operands.
600 Inst.addOperand(MCOperand::CreateReg(0));
601
602 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
603 // the difference?
604 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
605 assert(CE && "Non-constant mode 2 offset operand!");
606 int64_t Offset = CE->getValue();
607
608 if (Offset >= 0)
609 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
610 Offset, ARM_AM::no_shift, IdxMode)));
611 else
612 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
613 -Offset, ARM_AM::no_shift, IdxMode)));
614 }
615
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000616 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
617 assert(isMemMode3() && "Invalid mode or number of operands!");
618 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
619 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
620
621 if (getMemOffsetIsReg()) {
622 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
623
624 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
625 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
626 IdxMode)));
627 return;
628 }
629
630 // Create a operand placeholder to always yield the same number of operands.
631 Inst.addOperand(MCOperand::CreateReg(0));
632
633 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
634 // the difference?
635 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
636 assert(CE && "Non-constant mode 3 offset operand!");
637 int64_t Offset = CE->getValue();
638
639 if (Offset >= 0)
640 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
641 Offset, IdxMode)));
642 else
643 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
644 -Offset, IdxMode)));
645 }
646
Chris Lattner14b93852010-10-29 00:27:31 +0000647 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
648 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000649
Daniel Dunbar4b462672011-01-18 05:55:27 +0000650 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
651 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000652
Jim Grosbach80eb2332010-10-29 17:41:25 +0000653 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
654 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000655 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000656 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000657
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000658 // The MCInst offset operand doesn't include the low two bits (like
659 // the instruction encoding).
660 int64_t Offset = CE->getValue() / 4;
661 if (Offset >= 0)
662 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
663 Offset)));
664 else
665 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
666 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000667 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000668
Bill Wendlingf4caf692010-12-14 03:36:38 +0000669 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
670 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000671 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
672 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000673 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000674
Bill Wendlingf4caf692010-12-14 03:36:38 +0000675 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
676 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000677 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
678 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000679 assert(CE && "Non-constant mode offset operand!");
680 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000681 }
682
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000683 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
684 assert(N == 1 && "Invalid number of operands!");
685 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
686 }
687
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000688 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
689 assert(N == 1 && "Invalid number of operands!");
690 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
691 }
692
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000693 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000694
Chris Lattner3a697562010-10-28 17:20:03 +0000695 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
696 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000697 Op->CC.Val = CC;
698 Op->StartLoc = S;
699 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000700 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000701 }
702
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000703 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
704 ARMOperand *Op = new ARMOperand(CoprocNum);
705 Op->Cop.Val = CopVal;
706 Op->StartLoc = S;
707 Op->EndLoc = S;
708 return Op;
709 }
710
711 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
712 ARMOperand *Op = new ARMOperand(CoprocReg);
713 Op->Cop.Val = CopVal;
714 Op->StartLoc = S;
715 Op->EndLoc = S;
716 return Op;
717 }
718
Jim Grosbachd67641b2010-12-06 18:21:12 +0000719 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
720 ARMOperand *Op = new ARMOperand(CCOut);
721 Op->Reg.RegNum = RegNum;
722 Op->StartLoc = S;
723 Op->EndLoc = S;
724 return Op;
725 }
726
Chris Lattner3a697562010-10-28 17:20:03 +0000727 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
728 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000729 Op->Tok.Data = Str.data();
730 Op->Tok.Length = Str.size();
731 Op->StartLoc = S;
732 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000733 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000734 }
735
Bill Wendling50d0f582010-11-18 23:43:05 +0000736 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000737 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000738 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000739 Op->StartLoc = S;
740 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000741 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000742 }
743
Owen Anderson00828302011-03-18 22:50:18 +0000744 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
745 SMLoc S, SMLoc E) {
746 ARMOperand *Op = new ARMOperand(Shifter);
747 Op->Shift.ShiftTy = ShTy;
748 Op->StartLoc = S;
749 Op->EndLoc = E;
750 return Op;
751 }
752
Bill Wendling7729e062010-11-09 22:44:22 +0000753 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000754 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000755 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000756 KindTy Kind = RegisterList;
757
758 if (ARM::DPRRegClass.contains(Regs.front().first))
759 Kind = DPRRegisterList;
760 else if (ARM::SPRRegClass.contains(Regs.front().first))
761 Kind = SPRRegisterList;
762
763 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000764 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000765 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000766 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000767 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000768 Op->StartLoc = StartLoc;
769 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000770 return Op;
771 }
772
Chris Lattner3a697562010-10-28 17:20:03 +0000773 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
774 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000775 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000776 Op->StartLoc = S;
777 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000778 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000779 }
780
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000781 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
782 bool OffsetIsReg, const MCExpr *Offset,
783 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000784 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000785 const MCExpr *ShiftAmount, bool Preindexed,
786 bool Postindexed, bool Negative, bool Writeback,
787 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000788 assert((OffsetRegNum == -1 || OffsetIsReg) &&
789 "OffsetRegNum must imply OffsetIsReg!");
790 assert((!OffsetRegShifted || OffsetIsReg) &&
791 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000792 assert((Offset || OffsetIsReg) &&
793 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000794 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
795 "Cannot have shift amount without shifted register offset!");
796 assert((!Offset || !OffsetIsReg) &&
797 "Cannot have expression offset and register offset!");
798
Chris Lattner3a697562010-10-28 17:20:03 +0000799 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000800 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +0000801 Op->Mem.BaseRegNum = BaseRegNum;
802 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000803 if (OffsetIsReg)
804 Op->Mem.Offset.RegNum = OffsetRegNum;
805 else
806 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000807 Op->Mem.OffsetRegShifted = OffsetRegShifted;
808 Op->Mem.ShiftType = ShiftType;
809 Op->Mem.ShiftAmount = ShiftAmount;
810 Op->Mem.Preindexed = Preindexed;
811 Op->Mem.Postindexed = Postindexed;
812 Op->Mem.Negative = Negative;
813 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000814
Sean Callanan76264762010-04-02 22:27:05 +0000815 Op->StartLoc = S;
816 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000817 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000818 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000819
820 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
821 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
822 Op->MBOpt.Val = Opt;
823 Op->StartLoc = S;
824 Op->EndLoc = S;
825 return Op;
826 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000827
828 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
829 ARMOperand *Op = new ARMOperand(ProcIFlags);
830 Op->IFlags.Val = IFlags;
831 Op->StartLoc = S;
832 Op->EndLoc = S;
833 return Op;
834 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000835
836 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
837 ARMOperand *Op = new ARMOperand(MSRMask);
838 Op->MMask.Val = MMask;
839 Op->StartLoc = S;
840 Op->EndLoc = S;
841 return Op;
842 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000843};
844
845} // end anonymous namespace.
846
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000847void ARMOperand::dump(raw_ostream &OS) const {
848 switch (Kind) {
849 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000850 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000851 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000852 case CCOut:
853 OS << "<ccout " << getReg() << ">";
854 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000855 case CoprocNum:
856 OS << "<coprocessor number: " << getCoproc() << ">";
857 break;
858 case CoprocReg:
859 OS << "<coprocessor register: " << getCoproc() << ">";
860 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000861 case MSRMask:
862 OS << "<mask: " << getMSRMask() << ">";
863 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000864 case Immediate:
865 getImm()->print(OS);
866 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000867 case MemBarrierOpt:
868 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
869 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000870 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000871 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000872 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
873 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000874 if (getMemOffsetIsReg()) {
875 OS << " offset:<register " << getMemOffsetRegNum();
876 if (getMemOffsetRegShifted()) {
877 OS << " offset-shift-type:" << getMemShiftType();
878 OS << " offset-shift-amount:" << *getMemShiftAmount();
879 }
880 } else {
881 OS << " offset:" << *getMemOffset();
882 }
883 if (getMemOffsetIsReg())
884 OS << " (offset-is-reg)";
885 if (getMemPreindexed())
886 OS << " (pre-indexed)";
887 if (getMemPostindexed())
888 OS << " (post-indexed)";
889 if (getMemNegative())
890 OS << " (negative)";
891 if (getMemWriteback())
892 OS << " (writeback)";
893 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000894 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000895 case ProcIFlags: {
896 OS << "<ARM_PROC::";
897 unsigned IFlags = getProcIFlags();
898 for (int i=2; i >= 0; --i)
899 if (IFlags & (1 << i))
900 OS << ARM_PROC::IFlagsToString(1 << i);
901 OS << ">";
902 break;
903 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000904 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000905 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000906 break;
Owen Anderson00828302011-03-18 22:50:18 +0000907 case Shifter:
908 OS << "<shifter " << getShiftOpcStr(Shift.ShiftTy) << ">";
909 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000910 case RegisterList:
911 case DPRRegisterList:
912 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000913 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000914
Bill Wendling5fa22a12010-11-09 23:28:44 +0000915 const SmallVectorImpl<unsigned> &RegList = getRegList();
916 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000917 I = RegList.begin(), E = RegList.end(); I != E; ) {
918 OS << *I;
919 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000920 }
921
922 OS << ">";
923 break;
924 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000925 case Token:
926 OS << "'" << getToken() << "'";
927 break;
928 }
929}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000930
931/// @name Auto-generated Match Functions
932/// {
933
934static unsigned MatchRegisterName(StringRef Name);
935
936/// }
937
Bob Wilson69df7232011-02-03 21:46:10 +0000938bool ARMAsmParser::ParseRegister(unsigned &RegNo,
939 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +0000940 RegNo = TryParseRegister();
941
942 return (RegNo == (unsigned)-1);
943}
944
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000945/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000946/// and if it is a register name the token is eaten and the register number is
947/// returned. Otherwise return -1.
948///
949int ARMAsmParser::TryParseRegister() {
950 const AsmToken &Tok = Parser.getTok();
951 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000952
Chris Lattnere5658fa2010-10-30 04:09:10 +0000953 // FIXME: Validate register for the current architecture; we have to do
954 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000955 std::string upperCase = Tok.getString().str();
956 std::string lowerCase = LowercaseString(upperCase);
957 unsigned RegNum = MatchRegisterName(lowerCase);
958 if (!RegNum) {
959 RegNum = StringSwitch<unsigned>(lowerCase)
960 .Case("r13", ARM::SP)
961 .Case("r14", ARM::LR)
962 .Case("r15", ARM::PC)
963 .Case("ip", ARM::R12)
964 .Default(0);
965 }
966 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +0000967
Chris Lattnere5658fa2010-10-30 04:09:10 +0000968 Parser.Lex(); // Eat identifier token.
969 return RegNum;
970}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000971
Owen Anderson00828302011-03-18 22:50:18 +0000972/// Try to parse a register name. The token must be an Identifier when called,
973/// and if it is a register name the token is eaten and the register number is
974/// returned. Otherwise return -1.
975///
976bool ARMAsmParser::TryParseShiftRegister(
977 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
978 SMLoc S = Parser.getTok().getLoc();
979 const AsmToken &Tok = Parser.getTok();
980 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
981
982 std::string upperCase = Tok.getString().str();
983 std::string lowerCase = LowercaseString(upperCase);
984 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
985 .Case("lsl", ARM_AM::lsl)
986 .Case("lsr", ARM_AM::lsr)
987 .Case("asr", ARM_AM::asr)
988 .Case("ror", ARM_AM::ror)
989 .Case("rrx", ARM_AM::rrx)
990 .Default(ARM_AM::no_shift);
991
992 if (ShiftTy == ARM_AM::no_shift)
993 return true;
994
995 Parser.Lex(); // Eat shift-type operand;
996 int RegNum = TryParseRegister();
997 if (RegNum == -1)
998 return Error(Parser.getTok().getLoc(), "register expected");
999
1000 Operands.push_back(ARMOperand::CreateReg(RegNum,S, Parser.getTok().getLoc()));
1001 Operands.push_back(ARMOperand::CreateShifter(ShiftTy,
1002 S, Parser.getTok().getLoc()));
1003
1004 return false;
1005}
1006
1007
Bill Wendling50d0f582010-11-18 23:43:05 +00001008/// Try to parse a register name. The token must be an Identifier when called.
1009/// If it's a register, an AsmOperand is created. Another AsmOperand is created
1010/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +00001011///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001012/// TODO this is likely to change to allow different register types and or to
1013/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +00001014bool ARMAsmParser::
1015TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001016 SMLoc S = Parser.getTok().getLoc();
1017 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +00001018 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +00001019 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +00001020
Bill Wendling50d0f582010-11-18 23:43:05 +00001021 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001022
Chris Lattnere5658fa2010-10-30 04:09:10 +00001023 const AsmToken &ExclaimTok = Parser.getTok();
1024 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001025 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1026 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +00001027 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +00001028 }
1029
Bill Wendling50d0f582010-11-18 23:43:05 +00001030 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001031}
1032
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001033/// MatchCoprocessorOperandName - Try to parse an coprocessor related
1034/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
1035/// "c5", ...
1036static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001037 // Use the same layout as the tablegen'erated register name matcher. Ugly,
1038 // but efficient.
1039 switch (Name.size()) {
1040 default: break;
1041 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001042 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001043 return -1;
1044 switch (Name[1]) {
1045 default: return -1;
1046 case '0': return 0;
1047 case '1': return 1;
1048 case '2': return 2;
1049 case '3': return 3;
1050 case '4': return 4;
1051 case '5': return 5;
1052 case '6': return 6;
1053 case '7': return 7;
1054 case '8': return 8;
1055 case '9': return 9;
1056 }
1057 break;
1058 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001059 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001060 return -1;
1061 switch (Name[2]) {
1062 default: return -1;
1063 case '0': return 10;
1064 case '1': return 11;
1065 case '2': return 12;
1066 case '3': return 13;
1067 case '4': return 14;
1068 case '5': return 15;
1069 }
1070 break;
1071 }
1072
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001073 return -1;
1074}
1075
Jim Grosbachf922c472011-02-12 01:34:40 +00001076/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001077/// token must be an Identifier when called, and if it is a coprocessor
1078/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001079ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1080tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001081 SMLoc S = Parser.getTok().getLoc();
1082 const AsmToken &Tok = Parser.getTok();
1083 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1084
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001085 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001086 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001087 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001088
1089 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001090 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001091 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001092}
1093
Jim Grosbachf922c472011-02-12 01:34:40 +00001094/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001095/// token must be an Identifier when called, and if it is a coprocessor
1096/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001097ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1098tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001099 SMLoc S = Parser.getTok().getLoc();
1100 const AsmToken &Tok = Parser.getTok();
1101 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1102
1103 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1104 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001105 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001106
1107 Parser.Lex(); // Eat identifier token.
1108 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001109 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001110}
1111
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001112/// Parse a register list, return it if successful else return null. The first
1113/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001114bool ARMAsmParser::
1115ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001116 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001117 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001118 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001119
Bill Wendling7729e062010-11-09 22:44:22 +00001120 // Read the rest of the registers in the list.
1121 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001122 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001123
Bill Wendling7729e062010-11-09 22:44:22 +00001124 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001125 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001126 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001127
Sean Callanan18b83232010-01-19 21:44:56 +00001128 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001129 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001130 if (RegTok.isNot(AsmToken::Identifier)) {
1131 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001132 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001133 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001134
Bill Wendling1d6a2652010-11-06 10:40:24 +00001135 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001136 if (RegNum == -1) {
1137 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001138 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001139 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001140
Bill Wendlinge7176102010-11-06 22:36:58 +00001141 if (IsRange) {
1142 int Reg = PrevRegNum;
1143 do {
1144 ++Reg;
1145 Registers.push_back(std::make_pair(Reg, RegLoc));
1146 } while (Reg != RegNum);
1147 } else {
1148 Registers.push_back(std::make_pair(RegNum, RegLoc));
1149 }
1150
1151 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001152 } while (Parser.getTok().is(AsmToken::Comma) ||
1153 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001154
1155 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001156 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001157 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1158 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001159 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001160 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001161
Bill Wendlinge7176102010-11-06 22:36:58 +00001162 SMLoc E = RCurlyTok.getLoc();
1163 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001164
Bill Wendlinge7176102010-11-06 22:36:58 +00001165 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001166 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001167 RI = Registers.begin(), RE = Registers.end();
1168
Bill Wendling7caebff2011-01-12 21:20:59 +00001169 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001170 bool EmittedWarning = false;
1171
Bill Wendling7caebff2011-01-12 21:20:59 +00001172 DenseMap<unsigned, bool> RegMap;
1173 RegMap[HighRegNum] = true;
1174
Bill Wendlinge7176102010-11-06 22:36:58 +00001175 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001176 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001177 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001178
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001179 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001180 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001181 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001182 }
1183
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001184 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001185 Warning(RegInfo.second,
1186 "register not in ascending order in register list");
1187
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001188 RegMap[Reg] = true;
1189 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001190 }
1191
Bill Wendling50d0f582010-11-18 23:43:05 +00001192 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1193 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001194}
1195
Jim Grosbachf922c472011-02-12 01:34:40 +00001196/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1197ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1198tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001199 SMLoc S = Parser.getTok().getLoc();
1200 const AsmToken &Tok = Parser.getTok();
1201 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1202 StringRef OptStr = Tok.getString();
1203
1204 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1205 .Case("sy", ARM_MB::SY)
1206 .Case("st", ARM_MB::ST)
1207 .Case("ish", ARM_MB::ISH)
1208 .Case("ishst", ARM_MB::ISHST)
1209 .Case("nsh", ARM_MB::NSH)
1210 .Case("nshst", ARM_MB::NSHST)
1211 .Case("osh", ARM_MB::OSH)
1212 .Case("oshst", ARM_MB::OSHST)
1213 .Default(~0U);
1214
1215 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001216 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001217
1218 Parser.Lex(); // Eat identifier token.
1219 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001220 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001221}
1222
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001223/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001224ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1225tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1226 SMLoc S = Parser.getTok().getLoc();
1227 const AsmToken &Tok = Parser.getTok();
1228 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1229 StringRef IFlagsStr = Tok.getString();
1230
1231 unsigned IFlags = 0;
1232 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1233 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1234 .Case("a", ARM_PROC::A)
1235 .Case("i", ARM_PROC::I)
1236 .Case("f", ARM_PROC::F)
1237 .Default(~0U);
1238
1239 // If some specific iflag is already set, it means that some letter is
1240 // present more than once, this is not acceptable.
1241 if (Flag == ~0U || (IFlags & Flag))
1242 return MatchOperand_NoMatch;
1243
1244 IFlags |= Flag;
1245 }
1246
1247 Parser.Lex(); // Eat identifier token.
1248 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1249 return MatchOperand_Success;
1250}
1251
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001252/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1253ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1254tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1255 SMLoc S = Parser.getTok().getLoc();
1256 const AsmToken &Tok = Parser.getTok();
1257 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1258 StringRef Mask = Tok.getString();
1259
1260 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1261 size_t Start = 0, Next = Mask.find('_');
1262 StringRef Flags = "";
1263 StringRef SpecReg = Mask.slice(Start, Next);
1264 if (Next != StringRef::npos)
1265 Flags = Mask.slice(Next+1, Mask.size());
1266
1267 // FlagsVal contains the complete mask:
1268 // 3-0: Mask
1269 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1270 unsigned FlagsVal = 0;
1271
1272 if (SpecReg == "apsr") {
1273 FlagsVal = StringSwitch<unsigned>(Flags)
1274 .Case("nzcvq", 0x8) // same as CPSR_c
1275 .Case("g", 0x4) // same as CPSR_s
1276 .Case("nzcvqg", 0xc) // same as CPSR_fs
1277 .Default(~0U);
1278
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001279 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001280 if (!Flags.empty())
1281 return MatchOperand_NoMatch;
1282 else
1283 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001284 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001285 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00001286 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
1287 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001288 for (int i = 0, e = Flags.size(); i != e; ++i) {
1289 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1290 .Case("c", 1)
1291 .Case("x", 2)
1292 .Case("s", 4)
1293 .Case("f", 8)
1294 .Default(~0U);
1295
1296 // If some specific flag is already set, it means that some letter is
1297 // present more than once, this is not acceptable.
1298 if (FlagsVal == ~0U || (FlagsVal & Flag))
1299 return MatchOperand_NoMatch;
1300 FlagsVal |= Flag;
1301 }
1302 } else // No match for special register.
1303 return MatchOperand_NoMatch;
1304
1305 // Special register without flags are equivalent to "fc" flags.
1306 if (!FlagsVal)
1307 FlagsVal = 0x9;
1308
1309 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1310 if (SpecReg == "spsr")
1311 FlagsVal |= 16;
1312
1313 Parser.Lex(); // Eat identifier token.
1314 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1315 return MatchOperand_Success;
1316}
1317
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001318/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1319ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1320tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001321 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001322
1323 if (ParseMemory(Operands, ARMII::AddrMode2))
1324 return MatchOperand_NoMatch;
1325
1326 return MatchOperand_Success;
1327}
1328
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001329/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1330ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1331tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1332 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1333
1334 if (ParseMemory(Operands, ARMII::AddrMode3))
1335 return MatchOperand_NoMatch;
1336
1337 return MatchOperand_Success;
1338}
1339
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001340/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1341/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1342/// when they refer multiple MIOperands inside a single one.
1343bool ARMAsmParser::
1344CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1345 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1346 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1347
1348 // Create a writeback register dummy placeholder.
1349 Inst.addOperand(MCOperand::CreateImm(0));
1350
1351 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1352 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1353 return true;
1354}
1355
1356/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1357/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1358/// when they refer multiple MIOperands inside a single one.
1359bool ARMAsmParser::
1360CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1361 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1362 // Create a writeback register dummy placeholder.
1363 Inst.addOperand(MCOperand::CreateImm(0));
1364 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1365 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1366 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1367 return true;
1368}
1369
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001370/// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1371/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1372/// when they refer multiple MIOperands inside a single one.
1373bool ARMAsmParser::
1374CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1375 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1376 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1377
1378 // Create a writeback register dummy placeholder.
1379 Inst.addOperand(MCOperand::CreateImm(0));
1380
1381 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1382 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1383 return true;
1384}
1385
1386/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1387/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1388/// when they refer multiple MIOperands inside a single one.
1389bool ARMAsmParser::
1390CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1391 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1392 // Create a writeback register dummy placeholder.
1393 Inst.addOperand(MCOperand::CreateImm(0));
1394 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1395 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1396 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1397 return true;
1398}
1399
Bill Wendlinge7176102010-11-06 22:36:58 +00001400/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001401/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001402///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001403/// TODO Only preindexing and postindexing addressing are started, unindexed
1404/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001405bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001406ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1407 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001408 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001409 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001410 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001411 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001412 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001413
Sean Callanan18b83232010-01-19 21:44:56 +00001414 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001415 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1416 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001417 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001418 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001419 int BaseRegNum = TryParseRegister();
1420 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001421 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001422 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001423 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001424
Daniel Dunbar05710932011-01-18 05:34:17 +00001425 // The next token must either be a comma or a closing bracket.
1426 const AsmToken &Tok = Parser.getTok();
1427 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1428 return true;
1429
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001430 bool Preindexed = false;
1431 bool Postindexed = false;
1432 bool OffsetIsReg = false;
1433 bool Negative = false;
1434 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001435 ARMOperand *WBOp = 0;
1436 int OffsetRegNum = -1;
1437 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001438 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001439 const MCExpr *ShiftAmount = 0;
1440 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001441
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001442 // First look for preindexed address forms, that is after the "[Rn" we now
1443 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001444 if (Tok.is(AsmToken::Comma)) {
1445 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001446 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001447
Chris Lattner550276e2010-10-28 20:52:15 +00001448 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1449 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001450 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001451 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001452 if (RBracTok.isNot(AsmToken::RBrac)) {
1453 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001454 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001455 }
Sean Callanan76264762010-04-02 22:27:05 +00001456 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001457 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001458
Sean Callanan18b83232010-01-19 21:44:56 +00001459 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001460 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001461 // None of addrmode3 instruction uses "!"
1462 if (AddrMode == ARMII::AddrMode3)
1463 return true;
1464
Bill Wendling50d0f582010-11-18 23:43:05 +00001465 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1466 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001467 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001468 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001469 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1470 if (AddrMode == ARMII::AddrMode2)
1471 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001472 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001473 } else {
1474 // The "[Rn" we have so far was not followed by a comma.
1475
Jim Grosbach80eb2332010-10-29 17:41:25 +00001476 // If there's anything other than the right brace, this is a post indexing
1477 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001478 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001479 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001480
Sean Callanan18b83232010-01-19 21:44:56 +00001481 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001482
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001483 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001484 Postindexed = true;
1485 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001486
Chris Lattner550276e2010-10-28 20:52:15 +00001487 if (NextTok.isNot(AsmToken::Comma)) {
1488 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001489 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001490 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001491
Sean Callananb9a25b72010-01-19 20:27:46 +00001492 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001493
Chris Lattner550276e2010-10-28 20:52:15 +00001494 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001495 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001496 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001497 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001498 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001499 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001500
1501 // Force Offset to exist if used.
1502 if (!OffsetIsReg) {
1503 if (!Offset)
1504 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001505 } else {
1506 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1507 Error(E, "shift amount not supported");
1508 return true;
1509 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001510 }
1511
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001512 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1513 Offset, OffsetRegNum, OffsetRegShifted,
1514 ShiftType, ShiftAmount, Preindexed,
1515 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001516 if (WBOp)
1517 Operands.push_back(WBOp);
1518
1519 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001520}
1521
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001522/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1523/// we will parse the following (were +/- means that a plus or minus is
1524/// optional):
1525/// +/-Rm
1526/// +/-Rm, shift
1527/// #offset
1528/// we return false on success or an error otherwise.
1529bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001530 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001531 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001532 const MCExpr *&ShiftAmount,
1533 const MCExpr *&Offset,
1534 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001535 int &OffsetRegNum,
1536 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001537 Negative = false;
1538 OffsetRegShifted = false;
1539 OffsetIsReg = false;
1540 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001541 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001542 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001543 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001544 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001545 else if (NextTok.is(AsmToken::Minus)) {
1546 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001547 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001548 }
1549 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001550 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001551 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001552 SMLoc CurLoc = OffsetRegTok.getLoc();
1553 OffsetRegNum = TryParseRegister();
1554 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001555 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001556 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001557 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001558 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001559
Bill Wendling12f40e92010-11-06 10:51:53 +00001560 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001561 if (OffsetRegNum != -1) {
1562 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001563 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001564 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001565 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001566
Sean Callanan18b83232010-01-19 21:44:56 +00001567 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001568 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001569 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001570 OffsetRegShifted = true;
1571 }
1572 }
1573 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1574 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001575 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001576 if (HashTok.isNot(AsmToken::Hash))
1577 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001578
Sean Callananb9a25b72010-01-19 20:27:46 +00001579 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001580
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001581 if (getParser().ParseExpression(Offset))
1582 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001583 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001584 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001585 return false;
1586}
1587
1588/// ParseShift as one of these two:
1589/// ( lsl | lsr | asr | ror ) , # shift_amount
1590/// rrx
1591/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001592bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1593 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001594 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001595 if (Tok.isNot(AsmToken::Identifier))
1596 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001597 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001598 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001599 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001600 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001601 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001602 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001603 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001604 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001605 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001606 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001607 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001608 else
1609 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001610 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001611
1612 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001613 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001614 return false;
1615
1616 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001617 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001618 if (HashTok.isNot(AsmToken::Hash))
1619 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001620 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001621
1622 if (getParser().ParseExpression(ShiftAmount))
1623 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001624
1625 return false;
1626}
1627
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001628/// Parse a arm instruction operand. For now this parses the operand regardless
1629/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001630bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001631 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001632 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001633
1634 // Check if the current operand has a custom associated parser, if so, try to
1635 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001636 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1637 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001638 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001639 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1640 // there was a match, but an error occurred, in which case, just return that
1641 // the operand parsing failed.
1642 if (ResTy == MatchOperand_ParseFail)
1643 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001644
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001645 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001646 default:
1647 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001648 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +00001649 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +00001650 if (!TryParseRegisterWithWriteBack(Operands))
1651 return false;
Owen Anderson00828302011-03-18 22:50:18 +00001652 if (!TryParseShiftRegister(Operands))
1653 return false;
1654
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001655
1656 // Fall though for the Identifier case that is not a register or a
1657 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +00001658 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1659 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001660 // This was not a register so parse other operands that start with an
1661 // identifier (like labels) as expressions and create them as immediates.
1662 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001663 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001664 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001665 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001666 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001667 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1668 return false;
1669 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001670 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001671 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001672 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001673 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001674 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001675 // #42 -> immediate.
1676 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001677 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001678 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001679 const MCExpr *ImmVal;
1680 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001681 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001682 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001683 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1684 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001685 case AsmToken::Colon: {
1686 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001687 // FIXME: Check it's an expression prefix,
1688 // e.g. (FOO - :lower16:BAR) isn't legal.
1689 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001690 if (ParsePrefix(RefKind))
1691 return true;
1692
Evan Cheng75972122011-01-13 07:58:56 +00001693 const MCExpr *SubExprVal;
1694 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001695 return true;
1696
Evan Cheng75972122011-01-13 07:58:56 +00001697 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1698 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001699 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001700 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001701 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001702 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001703 }
1704}
1705
Evan Cheng75972122011-01-13 07:58:56 +00001706// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1707// :lower16: and :upper16:.
1708bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1709 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001710
1711 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001712 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001713 Parser.Lex(); // Eat ':'
1714
1715 if (getLexer().isNot(AsmToken::Identifier)) {
1716 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1717 return true;
1718 }
1719
1720 StringRef IDVal = Parser.getTok().getIdentifier();
1721 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001722 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001723 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001724 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001725 } else {
1726 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1727 return true;
1728 }
1729 Parser.Lex();
1730
1731 if (getLexer().isNot(AsmToken::Colon)) {
1732 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1733 return true;
1734 }
1735 Parser.Lex(); // Eat the last ':'
1736 return false;
1737}
1738
1739const MCExpr *
1740ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1741 MCSymbolRefExpr::VariantKind Variant) {
1742 // Recurse over the given expression, rebuilding it to apply the given variant
1743 // to the leftmost symbol.
1744 if (Variant == MCSymbolRefExpr::VK_None)
1745 return E;
1746
1747 switch (E->getKind()) {
1748 case MCExpr::Target:
1749 llvm_unreachable("Can't handle target expr yet");
1750 case MCExpr::Constant:
1751 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1752
1753 case MCExpr::SymbolRef: {
1754 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1755
1756 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1757 return 0;
1758
1759 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1760 }
1761
1762 case MCExpr::Unary:
1763 llvm_unreachable("Can't handle unary expressions yet");
1764
1765 case MCExpr::Binary: {
1766 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1767 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1768 const MCExpr *RHS = BE->getRHS();
1769 if (!LHS)
1770 return 0;
1771
1772 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1773 }
1774 }
1775
1776 assert(0 && "Invalid expression kind!");
1777 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001778}
1779
Daniel Dunbar352e1482011-01-11 15:59:50 +00001780/// \brief Given a mnemonic, split out possible predication code and carry
1781/// setting letters to form a canonical mnemonic and flags.
1782//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001783// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001784static StringRef SplitMnemonic(StringRef Mnemonic,
1785 unsigned &PredicationCode,
1786 bool &CarrySetting,
1787 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001788 PredicationCode = ARMCC::AL;
1789 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001790 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001791
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001792 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001793 //
1794 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001795 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1796 Mnemonic == "movs" ||
1797 Mnemonic == "svc" ||
1798 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1799 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1800 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1801 Mnemonic == "vclt" ||
1802 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1803 Mnemonic == "vcle" ||
1804 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1805 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
Jim Grosbachd1f0bbe2011-06-27 20:59:10 +00001806 Mnemonic == "vqdmlal" || Mnemonic == "bics"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001807 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001808
Daniel Dunbar352e1482011-01-11 15:59:50 +00001809 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001810 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001811 .Case("eq", ARMCC::EQ)
1812 .Case("ne", ARMCC::NE)
1813 .Case("hs", ARMCC::HS)
Jim Grosbach660a9ec2011-06-27 20:40:29 +00001814 .Case("cs", ARMCC::HS)
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001815 .Case("lo", ARMCC::LO)
Jim Grosbach660a9ec2011-06-27 20:40:29 +00001816 .Case("cc", ARMCC::LO)
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001817 .Case("mi", ARMCC::MI)
1818 .Case("pl", ARMCC::PL)
1819 .Case("vs", ARMCC::VS)
1820 .Case("vc", ARMCC::VC)
1821 .Case("hi", ARMCC::HI)
1822 .Case("ls", ARMCC::LS)
1823 .Case("ge", ARMCC::GE)
1824 .Case("lt", ARMCC::LT)
1825 .Case("gt", ARMCC::GT)
1826 .Case("le", ARMCC::LE)
1827 .Case("al", ARMCC::AL)
1828 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001829 if (CC != ~0U) {
1830 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001831 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001832 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001833
Daniel Dunbar352e1482011-01-11 15:59:50 +00001834 // Next, determine if we have a carry setting bit. We explicitly ignore all
1835 // the instructions we know end in 's'.
1836 if (Mnemonic.endswith("s") &&
1837 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1838 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1839 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1840 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1841 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1842 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1843 CarrySetting = true;
1844 }
1845
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001846 // The "cps" instruction can have a interrupt mode operand which is glued into
1847 // the mnemonic. Check if this is the case, split it and parse the imod op
1848 if (Mnemonic.startswith("cps")) {
1849 // Split out any imod code.
1850 unsigned IMod =
1851 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1852 .Case("ie", ARM_PROC::IE)
1853 .Case("id", ARM_PROC::ID)
1854 .Default(~0U);
1855 if (IMod != ~0U) {
1856 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
1857 ProcessorIMod = IMod;
1858 }
1859 }
1860
Daniel Dunbar352e1482011-01-11 15:59:50 +00001861 return Mnemonic;
1862}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001863
1864/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1865/// inclusion of carry set or predication code operands.
1866//
1867// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001868void ARMAsmParser::
1869GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1870 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001871 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1872 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1873 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1874 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00001875 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001876 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1877 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00001878 Mnemonic == "eor" || Mnemonic == "smlal" ||
Evan Chengebdeeab2011-07-08 01:53:10 +00001879 (Mnemonic == "mov" && !isThumbOne())) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001880 CanAcceptCarrySet = true;
1881 } else {
1882 CanAcceptCarrySet = false;
1883 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001884
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001885 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1886 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1887 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1888 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00001889 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001890 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001891 CanAcceptPredicationCode = false;
1892 } else {
1893 CanAcceptPredicationCode = true;
1894 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001895
Evan Chengebdeeab2011-07-08 01:53:10 +00001896 if (isThumb())
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001897 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00001898 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001899 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001900}
1901
1902/// Parse an arm instruction mnemonic followed by its operands.
1903bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1904 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1905 // Create the leading tokens for the mnemonic, split by '.' characters.
1906 size_t Start = 0, Next = Name.find('.');
1907 StringRef Head = Name.slice(Start, Next);
1908
Daniel Dunbar352e1482011-01-11 15:59:50 +00001909 // Split out the predication code and carry setting flag from the mnemonic.
1910 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001911 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001912 bool CarrySetting;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001913 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
1914 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001915
Chris Lattner3a697562010-10-28 17:20:03 +00001916 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001917
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001918 // Next, add the CCOut and ConditionCode operands, if needed.
1919 //
1920 // For mnemonics which can ever incorporate a carry setting bit or predication
1921 // code, our matching model involves us always generating CCOut and
1922 // ConditionCode operands to match the mnemonic "as written" and then we let
1923 // the matcher deal with finding the right instruction or generating an
1924 // appropriate error.
1925 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1926 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1927
1928 // Add the carry setting operand, if necessary.
1929 //
1930 // FIXME: It would be awesome if we could somehow invent a location such that
1931 // match errors on this operand would print a nice diagnostic about how the
1932 // 's' character in the mnemonic resulted in a CCOut operand.
1933 if (CanAcceptCarrySet) {
1934 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1935 NameLoc));
1936 } else {
1937 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1938 // misspelled another mnemonic).
1939
1940 // FIXME: Issue a nice error.
1941 }
1942
1943 // Add the predication code operand, if necessary.
1944 if (CanAcceptPredicationCode) {
1945 Operands.push_back(ARMOperand::CreateCondCode(
1946 ARMCC::CondCodes(PredicationCode), NameLoc));
1947 } else {
1948 // This mnemonic can't ever accept a predication code, but the user wrote
1949 // one (or misspelled another mnemonic).
1950
1951 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001952 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001953
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001954 // Add the processor imod operand, if necessary.
1955 if (ProcessorIMod) {
1956 Operands.push_back(ARMOperand::CreateImm(
1957 MCConstantExpr::Create(ProcessorIMod, getContext()),
1958 NameLoc, NameLoc));
1959 } else {
1960 // This mnemonic can't ever accept a imod, but the user wrote
1961 // one (or misspelled another mnemonic).
1962
1963 // FIXME: Issue a nice error.
1964 }
1965
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001966 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001967 while (Next != StringRef::npos) {
1968 Start = Next;
1969 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001970 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001971
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001972 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001973 }
1974
1975 // Read the remaining operands.
1976 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001977 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001978 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001979 Parser.EatToEndOfStatement();
1980 return true;
1981 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001982
1983 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001984 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001985
1986 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001987 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001988 Parser.EatToEndOfStatement();
1989 return true;
1990 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001991 }
1992 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001993
Chris Lattnercbf8a982010-09-11 16:18:25 +00001994 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1995 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001996 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001997 }
Bill Wendling146018f2010-11-06 21:42:12 +00001998
Chris Lattner34e53142010-09-08 05:10:46 +00001999 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00002000 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002001}
2002
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002003bool ARMAsmParser::
2004MatchAndEmitInstruction(SMLoc IDLoc,
2005 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2006 MCStreamer &Out) {
2007 MCInst Inst;
2008 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002009 MatchResultTy MatchResult, MatchResult2;
2010 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2011 if (MatchResult != Match_Success) {
2012 // If we get a Match_InvalidOperand it might be some arithmetic instruction
2013 // that does not update the condition codes. So try adding a CCOut operand
2014 // with a value of reg0.
2015 if (MatchResult == Match_InvalidOperand) {
2016 Operands.insert(Operands.begin() + 1,
2017 ARMOperand::CreateCCOut(0,
2018 ((ARMOperand*)Operands[0])->getStartLoc()));
2019 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2020 if (MatchResult2 == Match_Success)
2021 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002022 else {
2023 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002024 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002025 delete CCOut;
2026 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002027 }
2028 // If we get a Match_MnemonicFail it might be some arithmetic instruction
2029 // that updates the condition codes if it ends in 's'. So see if the
2030 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
2031 // operand with a value of CPSR.
2032 else if(MatchResult == Match_MnemonicFail) {
2033 // Get the instruction mnemonic, which is the first token.
2034 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
2035 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
2036 // removed the 's' from the mnemonic for matching.
2037 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
2038 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002039 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2040 Operands.erase(Operands.begin());
2041 delete OldMnemonic;
2042 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002043 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
2044 Operands.insert(Operands.begin() + 1,
2045 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
2046 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2047 if (MatchResult2 == Match_Success)
2048 MatchResult = Match_Success;
2049 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002050 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2051 Operands.erase(Operands.begin());
2052 delete OldMnemonic;
2053 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002054 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002055 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
2056 Operands.erase(Operands.begin() + 1);
2057 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002058 }
2059 }
2060 }
2061 }
2062 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002063 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002064 Out.EmitInstruction(Inst);
2065 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002066 case Match_MissingFeature:
2067 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2068 return true;
2069 case Match_InvalidOperand: {
2070 SMLoc ErrorLoc = IDLoc;
2071 if (ErrorInfo != ~0U) {
2072 if (ErrorInfo >= Operands.size())
2073 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002074
Chris Lattnere73d4f82010-10-28 21:41:58 +00002075 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2076 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2077 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002078
Chris Lattnere73d4f82010-10-28 21:41:58 +00002079 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002080 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002081 case Match_MnemonicFail:
2082 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002083 case Match_ConversionFail:
2084 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002085 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002086
Eric Christopherc223e2b2010-10-29 09:26:59 +00002087 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002088 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002089}
2090
Kevin Enderby515d5092009-10-15 20:48:48 +00002091/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002092bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2093 StringRef IDVal = DirectiveID.getIdentifier();
2094 if (IDVal == ".word")
2095 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002096 else if (IDVal == ".thumb")
2097 return ParseDirectiveThumb(DirectiveID.getLoc());
2098 else if (IDVal == ".thumb_func")
2099 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2100 else if (IDVal == ".code")
2101 return ParseDirectiveCode(DirectiveID.getLoc());
2102 else if (IDVal == ".syntax")
2103 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002104 return true;
2105}
2106
2107/// ParseDirectiveWord
2108/// ::= .word [ expression (, expression)* ]
2109bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2110 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2111 for (;;) {
2112 const MCExpr *Value;
2113 if (getParser().ParseExpression(Value))
2114 return true;
2115
Chris Lattneraaec2052010-01-19 19:46:13 +00002116 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002117
2118 if (getLexer().is(AsmToken::EndOfStatement))
2119 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002120
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002121 // FIXME: Improve diagnostic.
2122 if (getLexer().isNot(AsmToken::Comma))
2123 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002124 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002125 }
2126 }
2127
Sean Callananb9a25b72010-01-19 20:27:46 +00002128 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002129 return false;
2130}
2131
Kevin Enderby515d5092009-10-15 20:48:48 +00002132/// ParseDirectiveThumb
2133/// ::= .thumb
2134bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2135 if (getLexer().isNot(AsmToken::EndOfStatement))
2136 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002137 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002138
2139 // TODO: set thumb mode
2140 // TODO: tell the MC streamer the mode
2141 // getParser().getStreamer().Emit???();
2142 return false;
2143}
2144
2145/// ParseDirectiveThumbFunc
2146/// ::= .thumbfunc symbol_name
2147bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00002148 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
2149 bool isMachO = MAI.hasSubsectionsViaSymbols();
2150 StringRef Name;
2151
2152 // Darwin asm has function name after .thumb_func direction
2153 // ELF doesn't
2154 if (isMachO) {
2155 const AsmToken &Tok = Parser.getTok();
2156 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2157 return Error(L, "unexpected token in .thumb_func directive");
2158 Name = Tok.getString();
2159 Parser.Lex(); // Consume the identifier token.
2160 }
2161
Kevin Enderby515d5092009-10-15 20:48:48 +00002162 if (getLexer().isNot(AsmToken::EndOfStatement))
2163 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002164 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002165
Rafael Espindola64695402011-05-16 16:17:21 +00002166 // FIXME: assuming function name will be the line following .thumb_func
2167 if (!isMachO) {
2168 Name = Parser.getTok().getString();
2169 }
2170
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002171 // Mark symbol as a thumb symbol.
2172 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2173 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002174 return false;
2175}
2176
2177/// ParseDirectiveSyntax
2178/// ::= .syntax unified | divided
2179bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002180 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002181 if (Tok.isNot(AsmToken::Identifier))
2182 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002183 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002184 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002185 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002186 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002187 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002188 else
2189 return Error(L, "unrecognized syntax mode in .syntax directive");
2190
2191 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002192 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002193 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002194
2195 // TODO tell the MC streamer the mode
2196 // getParser().getStreamer().Emit???();
2197 return false;
2198}
2199
2200/// ParseDirectiveCode
2201/// ::= .code 16 | 32
2202bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002203 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002204 if (Tok.isNot(AsmToken::Integer))
2205 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002206 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002207 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002208 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002209 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002210 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002211 else
2212 return Error(L, "invalid operand to .code directive");
2213
2214 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002215 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002216 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002217
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00002218 // FIXME: We need to be able switch subtargets at this point so that
2219 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
2220 // includes Feature_IsThumb or not to match the right instructions. This is
2221 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
2222 if (Val == 16){
Evan Chengebdeeab2011-07-08 01:53:10 +00002223 assert(isThumb() &&
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00002224 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00002225 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00002226 }
2227 else{
Evan Chengebdeeab2011-07-08 01:53:10 +00002228 assert(!isThumb() &&
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00002229 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00002230 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00002231 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002232
Kevin Enderby515d5092009-10-15 20:48:48 +00002233 return false;
2234}
2235
Sean Callanan90b70972010-04-07 20:29:34 +00002236extern "C" void LLVMInitializeARMAsmLexer();
2237
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002238/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002239extern "C" void LLVMInitializeARMAsmParser() {
2240 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2241 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002242 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002243}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002244
Chris Lattner0692ee62010-09-06 19:11:01 +00002245#define GET_REGISTER_MATCHER
2246#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002247#include "ARMGenAsmMatcher.inc"