blob: 401894e75e1cc47a07ba6118189c03bde5a01d7f [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"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000023#include "llvm/Target/TargetRegistry.h"
24#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000025#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000026#include "llvm/Support/raw_ostream.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000027#include "llvm/ADT/SmallVector.h"
Owen Anderson0c9f2502011-01-13 22:50:36 +000028#include "llvm/ADT/StringExtras.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000029#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000030#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000031using namespace llvm;
32
Chris Lattner3a697562010-10-28 17:20:03 +000033namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000034
35class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000036
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000037class ARMAsmParser : public TargetAsmParser {
38 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000039 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000040
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000041 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000042 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
43
44 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000045 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
46
Chris Lattnere5658fa2010-10-30 04:09:10 +000047 int TryParseRegister();
Roman Divackybf755322011-01-27 17:14:22 +000048 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Bill Wendling50d0f582010-11-18 23:43:05 +000049 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
Owen Anderson00828302011-03-18 22:50:18 +000050 bool TryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
Bill Wendling50d0f582010-11-18 23:43:05 +000051 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +000052 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &,
53 ARMII::AddrMode AddrMode);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +000054 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
Evan Cheng75972122011-01-13 07:58:56 +000055 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000056 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
57 MCSymbolRefExpr::VariantKind Variant);
58
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000059
Kevin Enderby9c41fa82009-10-30 22:55:57 +000060 bool ParseMemoryOffsetReg(bool &Negative,
61 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +000062 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +000063 const MCExpr *&ShiftAmount,
64 const MCExpr *&Offset,
65 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000066 int &OffsetRegNum,
67 SMLoc &E);
Owen Anderson00828302011-03-18 22:50:18 +000068 bool ParseShift(enum ARM_AM::ShiftOpc &St,
69 const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000070 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000071 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000072 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000073 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000074 bool ParseDirectiveSyntax(SMLoc L);
75
Chris Lattner7036f8b2010-09-29 01:42:58 +000076 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000077 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000078 MCStreamer &Out);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000079 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
80 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000081
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000082 /// @name Auto-generated Match Functions
83 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000084
Chris Lattner0692ee62010-09-06 19:11:01 +000085#define GET_ASSEMBLER_HEADER
86#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000087
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000088 /// }
89
Jim Grosbachf922c472011-02-12 01:34:40 +000090 OperandMatchResultTy tryParseCoprocNumOperand(
91 SmallVectorImpl<MCParsedAsmOperand*>&);
92 OperandMatchResultTy tryParseCoprocRegOperand(
93 SmallVectorImpl<MCParsedAsmOperand*>&);
94 OperandMatchResultTy tryParseMemBarrierOptOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +000095 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +000096 OperandMatchResultTy tryParseProcIFlagsOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +000097 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +000098 OperandMatchResultTy tryParseMSRMaskOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +000099 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000100 OperandMatchResultTy tryParseMemMode2Operand(
101 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000102 OperandMatchResultTy tryParseMemMode3Operand(
103 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000104
105 // Asm Match Converter Methods
106 bool CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
107 const SmallVectorImpl<MCParsedAsmOperand*> &);
108 bool CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
109 const SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000110 bool CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
111 const SmallVectorImpl<MCParsedAsmOperand*> &);
112 bool CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
113 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbachf922c472011-02-12 01:34:40 +0000114
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000115public:
Daniel Dunbard73ada72010-07-19 00:33:49 +0000116 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +0000117 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
Sean Callananf6d91092011-04-18 20:20:44 +0000118 MCAsmParserExtension::Initialize(_Parser);
Jim Grosbach833c93c2010-11-01 16:59:54 +0000119 // Initialize the set of available features.
120 setAvailableFeatures(ComputeAvailableFeatures(
121 &TM.getSubtarget<ARMSubtarget>()));
122 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000123
Benjamin Kramer38e59892010-07-14 22:38:02 +0000124 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000125 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000126 virtual bool ParseDirective(AsmToken DirectiveID);
127};
Jim Grosbach16c74252010-10-29 14:46:02 +0000128} // end anonymous namespace
129
Chris Lattner3a697562010-10-28 17:20:03 +0000130namespace {
131
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000132/// ARMOperand - Instances of this class represent a parsed ARM machine
133/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000134class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000135 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000136 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000137 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000138 CoprocNum,
139 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000140 Immediate,
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000141 MemBarrierOpt,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000142 Memory,
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000143 MSRMask,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000144 ProcIFlags,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000145 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000146 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000147 DPRRegisterList,
148 SPRRegisterList,
Owen Anderson00828302011-03-18 22:50:18 +0000149 Shifter,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000150 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000151 } Kind;
152
Sean Callanan76264762010-04-02 22:27:05 +0000153 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000154 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000155
156 union {
157 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000158 ARMCC::CondCodes Val;
159 } CC;
160
161 struct {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000162 ARM_MB::MemBOpt Val;
163 } MBOpt;
164
165 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000166 unsigned Val;
167 } Cop;
168
169 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000170 ARM_PROC::IFlags Val;
171 } IFlags;
172
173 struct {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000174 unsigned Val;
175 } MMask;
176
177 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000178 const char *Data;
179 unsigned Length;
180 } Tok;
181
182 struct {
183 unsigned RegNum;
184 } Reg;
185
Bill Wendling8155e5b2010-11-06 22:19:43 +0000186 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000187 const MCExpr *Val;
188 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000189
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000190 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000191 struct {
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000192 ARMII::AddrMode AddrMode;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000193 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000194 union {
195 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
196 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
197 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000198 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Owen Anderson00828302011-03-18 22:50:18 +0000199 enum ARM_AM::ShiftOpc ShiftType; // used when OffsetRegShifted is true
Bill Wendling146018f2010-11-06 21:42:12 +0000200 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000201 unsigned Preindexed : 1;
202 unsigned Postindexed : 1;
203 unsigned OffsetIsReg : 1;
204 unsigned Negative : 1; // only used when OffsetIsReg is true
205 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000206 } Mem;
Owen Anderson00828302011-03-18 22:50:18 +0000207
208 struct {
209 ARM_AM::ShiftOpc ShiftTy;
210 unsigned RegNum;
211 } Shift;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000212 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000213
Bill Wendling146018f2010-11-06 21:42:12 +0000214 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
215public:
Sean Callanan76264762010-04-02 22:27:05 +0000216 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
217 Kind = o.Kind;
218 StartLoc = o.StartLoc;
219 EndLoc = o.EndLoc;
220 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000221 case CondCode:
222 CC = o.CC;
223 break;
Sean Callanan76264762010-04-02 22:27:05 +0000224 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000225 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000226 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000227 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000228 case Register:
229 Reg = o.Reg;
230 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000231 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000232 case DPRRegisterList:
233 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000234 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000235 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000236 case CoprocNum:
237 case CoprocReg:
238 Cop = o.Cop;
239 break;
Sean Callanan76264762010-04-02 22:27:05 +0000240 case Immediate:
241 Imm = o.Imm;
242 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000243 case MemBarrierOpt:
244 MBOpt = o.MBOpt;
245 break;
Sean Callanan76264762010-04-02 22:27:05 +0000246 case Memory:
247 Mem = o.Mem;
248 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000249 case MSRMask:
250 MMask = o.MMask;
251 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000252 case ProcIFlags:
253 IFlags = o.IFlags;
Owen Anderson00828302011-03-18 22:50:18 +0000254 break;
255 case Shifter:
256 Shift = o.Shift;
257 break;
Sean Callanan76264762010-04-02 22:27:05 +0000258 }
259 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000260
Sean Callanan76264762010-04-02 22:27:05 +0000261 /// getStartLoc - Get the location of the first token of this operand.
262 SMLoc getStartLoc() const { return StartLoc; }
263 /// getEndLoc - Get the location of the last token of this operand.
264 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000265
Daniel Dunbar8462b302010-08-11 06:36:53 +0000266 ARMCC::CondCodes getCondCode() const {
267 assert(Kind == CondCode && "Invalid access!");
268 return CC.Val;
269 }
270
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000271 unsigned getCoproc() const {
272 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
273 return Cop.Val;
274 }
275
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000276 StringRef getToken() const {
277 assert(Kind == Token && "Invalid access!");
278 return StringRef(Tok.Data, Tok.Length);
279 }
280
281 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000282 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000283 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000284 }
285
Bill Wendling5fa22a12010-11-09 23:28:44 +0000286 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000287 assert((Kind == RegisterList || Kind == DPRRegisterList ||
288 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000289 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000290 }
291
Kevin Enderbycfe07242009-10-13 22:19:02 +0000292 const MCExpr *getImm() const {
293 assert(Kind == Immediate && "Invalid access!");
294 return Imm.Val;
295 }
296
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000297 ARM_MB::MemBOpt getMemBarrierOpt() const {
298 assert(Kind == MemBarrierOpt && "Invalid access!");
299 return MBOpt.Val;
300 }
301
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000302 ARM_PROC::IFlags getProcIFlags() const {
303 assert(Kind == ProcIFlags && "Invalid access!");
304 return IFlags.Val;
305 }
306
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000307 unsigned getMSRMask() const {
308 assert(Kind == MSRMask && "Invalid access!");
309 return MMask.Val;
310 }
311
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000312 /// @name Memory Operand Accessors
313 /// @{
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000314 ARMII::AddrMode getMemAddrMode() const {
315 return Mem.AddrMode;
316 }
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000317 unsigned getMemBaseRegNum() const {
318 return Mem.BaseRegNum;
319 }
320 unsigned getMemOffsetRegNum() const {
321 assert(Mem.OffsetIsReg && "Invalid access!");
322 return Mem.Offset.RegNum;
323 }
324 const MCExpr *getMemOffset() const {
325 assert(!Mem.OffsetIsReg && "Invalid access!");
326 return Mem.Offset.Value;
327 }
328 unsigned getMemOffsetRegShifted() const {
329 assert(Mem.OffsetIsReg && "Invalid access!");
330 return Mem.OffsetRegShifted;
331 }
332 const MCExpr *getMemShiftAmount() const {
333 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
334 return Mem.ShiftAmount;
335 }
Owen Anderson00828302011-03-18 22:50:18 +0000336 enum ARM_AM::ShiftOpc getMemShiftType() const {
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000337 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
338 return Mem.ShiftType;
339 }
340 bool getMemPreindexed() const { return Mem.Preindexed; }
341 bool getMemPostindexed() const { return Mem.Postindexed; }
342 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
343 bool getMemNegative() const { return Mem.Negative; }
344 bool getMemWriteback() const { return Mem.Writeback; }
345
346 /// @}
347
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000348 bool isCoprocNum() const { return Kind == CoprocNum; }
349 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000350 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000351 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000352 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000353 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000354 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000355 bool isDPRRegList() const { return Kind == DPRRegisterList; }
356 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000357 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000358 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000359 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000360 bool isShifter() const { return Kind == Shifter; }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000361 bool isMemMode2() const {
362 if (getMemAddrMode() != ARMII::AddrMode2)
363 return false;
364
365 if (getMemOffsetIsReg())
366 return true;
367
368 if (getMemNegative() &&
369 !(getMemPostindexed() || getMemPreindexed()))
370 return false;
371
372 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
373 if (!CE) return false;
374 int64_t Value = CE->getValue();
375
376 // The offset must be in the range 0-4095 (imm12).
377 if (Value > 4095 || Value < -4095)
378 return false;
379
380 return true;
381 }
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000382 bool isMemMode3() const {
383 if (getMemAddrMode() != ARMII::AddrMode3)
384 return false;
385
386 if (getMemOffsetIsReg()) {
387 if (getMemOffsetRegShifted())
388 return false; // No shift with offset reg allowed
389 return true;
390 }
391
392 if (getMemNegative() &&
393 !(getMemPostindexed() || getMemPreindexed()))
394 return false;
395
396 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
397 if (!CE) return false;
398 int64_t Value = CE->getValue();
399
400 // The offset must be in the range 0-255 (imm8).
401 if (Value > 255 || Value < -255)
402 return false;
403
404 return true;
405 }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000406 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000407 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
408 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000409 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000410
Daniel Dunbar4b462672011-01-18 05:55:27 +0000411 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000412 if (!CE) return false;
413
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000414 // The offset must be a multiple of 4 in the range 0-1020.
415 int64_t Value = CE->getValue();
416 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
417 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000418 bool isMemMode7() const {
419 if (!isMemory() ||
420 getMemPreindexed() ||
421 getMemPostindexed() ||
422 getMemOffsetIsReg() ||
423 getMemNegative() ||
424 getMemWriteback())
425 return false;
426
427 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
428 if (!CE) return false;
429
430 if (CE->getValue())
431 return false;
432
433 return true;
434 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000435 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000436 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000437 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000438 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000439 }
440 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000441 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000442 return false;
443
Daniel Dunbar4b462672011-01-18 05:55:27 +0000444 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000445 if (!CE) return false;
446
447 // The offset must be a multiple of 4 in the range 0-124.
448 uint64_t Value = CE->getValue();
449 return ((Value & 0x3) == 0 && Value <= 124);
450 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000451 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000452 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000453
454 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000455 // Add as immediates when possible. Null MCExpr = 0.
456 if (Expr == 0)
457 Inst.addOperand(MCOperand::CreateImm(0));
458 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000459 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
460 else
461 Inst.addOperand(MCOperand::CreateExpr(Expr));
462 }
463
Daniel Dunbar8462b302010-08-11 06:36:53 +0000464 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000465 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000466 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000467 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
468 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000469 }
470
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000471 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
472 assert(N == 1 && "Invalid number of operands!");
473 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
474 }
475
476 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
477 assert(N == 1 && "Invalid number of operands!");
478 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
479 }
480
Jim Grosbachd67641b2010-12-06 18:21:12 +0000481 void addCCOutOperands(MCInst &Inst, unsigned N) const {
482 assert(N == 1 && "Invalid number of operands!");
483 Inst.addOperand(MCOperand::CreateReg(getReg()));
484 }
485
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000486 void addRegOperands(MCInst &Inst, unsigned N) const {
487 assert(N == 1 && "Invalid number of operands!");
488 Inst.addOperand(MCOperand::CreateReg(getReg()));
489 }
490
Owen Anderson00828302011-03-18 22:50:18 +0000491 void addShifterOperands(MCInst &Inst, unsigned N) const {
492 assert(N == 1 && "Invalid number of operands!");
493 Inst.addOperand(MCOperand::CreateImm(
494 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
495 }
496
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000497 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000498 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000499 const SmallVectorImpl<unsigned> &RegList = getRegList();
500 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000501 I = RegList.begin(), E = RegList.end(); I != E; ++I)
502 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000503 }
504
Bill Wendling0f630752010-11-17 04:32:08 +0000505 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
506 addRegListOperands(Inst, N);
507 }
508
509 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
510 addRegListOperands(Inst, N);
511 }
512
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000513 void addImmOperands(MCInst &Inst, unsigned N) const {
514 assert(N == 1 && "Invalid number of operands!");
515 addExpr(Inst, getImm());
516 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000517
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000518 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
519 assert(N == 1 && "Invalid number of operands!");
520 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
521 }
522
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000523 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
524 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
525 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
526
527 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000528 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000529 assert((CE || CE->getValue() == 0) &&
530 "No offset operand support in mode 7");
531 }
532
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000533 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
534 assert(isMemMode2() && "Invalid mode or number of operands!");
535 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
536 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
537
538 if (getMemOffsetIsReg()) {
539 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
540
541 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
542 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
543 int64_t ShiftAmount = 0;
544
545 if (getMemOffsetRegShifted()) {
546 ShOpc = getMemShiftType();
547 const MCConstantExpr *CE =
548 dyn_cast<MCConstantExpr>(getMemShiftAmount());
549 ShiftAmount = CE->getValue();
550 }
551
552 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
553 ShOpc, IdxMode)));
554 return;
555 }
556
557 // Create a operand placeholder to always yield the same number of operands.
558 Inst.addOperand(MCOperand::CreateReg(0));
559
560 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
561 // the difference?
562 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
563 assert(CE && "Non-constant mode 2 offset operand!");
564 int64_t Offset = CE->getValue();
565
566 if (Offset >= 0)
567 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
568 Offset, ARM_AM::no_shift, IdxMode)));
569 else
570 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
571 -Offset, ARM_AM::no_shift, IdxMode)));
572 }
573
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000574 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
575 assert(isMemMode3() && "Invalid mode or number of operands!");
576 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
577 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
578
579 if (getMemOffsetIsReg()) {
580 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
581
582 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
583 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
584 IdxMode)));
585 return;
586 }
587
588 // Create a operand placeholder to always yield the same number of operands.
589 Inst.addOperand(MCOperand::CreateReg(0));
590
591 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
592 // the difference?
593 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
594 assert(CE && "Non-constant mode 3 offset operand!");
595 int64_t Offset = CE->getValue();
596
597 if (Offset >= 0)
598 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
599 Offset, IdxMode)));
600 else
601 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
602 -Offset, IdxMode)));
603 }
604
Chris Lattner14b93852010-10-29 00:27:31 +0000605 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
606 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000607
Daniel Dunbar4b462672011-01-18 05:55:27 +0000608 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
609 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000610
Jim Grosbach80eb2332010-10-29 17:41:25 +0000611 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
612 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000613 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000614 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000615
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000616 // The MCInst offset operand doesn't include the low two bits (like
617 // the instruction encoding).
618 int64_t Offset = CE->getValue() / 4;
619 if (Offset >= 0)
620 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
621 Offset)));
622 else
623 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
624 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000625 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000626
Bill Wendlingf4caf692010-12-14 03:36:38 +0000627 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
628 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000629 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
630 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000631 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000632
Bill Wendlingf4caf692010-12-14 03:36:38 +0000633 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
634 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000635 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
636 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000637 assert(CE && "Non-constant mode offset operand!");
638 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000639 }
640
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000641 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
642 assert(N == 1 && "Invalid number of operands!");
643 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
644 }
645
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000646 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
647 assert(N == 1 && "Invalid number of operands!");
648 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
649 }
650
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000651 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000652
Chris Lattner3a697562010-10-28 17:20:03 +0000653 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
654 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000655 Op->CC.Val = CC;
656 Op->StartLoc = S;
657 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000658 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000659 }
660
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000661 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
662 ARMOperand *Op = new ARMOperand(CoprocNum);
663 Op->Cop.Val = CopVal;
664 Op->StartLoc = S;
665 Op->EndLoc = S;
666 return Op;
667 }
668
669 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
670 ARMOperand *Op = new ARMOperand(CoprocReg);
671 Op->Cop.Val = CopVal;
672 Op->StartLoc = S;
673 Op->EndLoc = S;
674 return Op;
675 }
676
Jim Grosbachd67641b2010-12-06 18:21:12 +0000677 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
678 ARMOperand *Op = new ARMOperand(CCOut);
679 Op->Reg.RegNum = RegNum;
680 Op->StartLoc = S;
681 Op->EndLoc = S;
682 return Op;
683 }
684
Chris Lattner3a697562010-10-28 17:20:03 +0000685 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
686 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000687 Op->Tok.Data = Str.data();
688 Op->Tok.Length = Str.size();
689 Op->StartLoc = S;
690 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000691 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000692 }
693
Bill Wendling50d0f582010-11-18 23:43:05 +0000694 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000695 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000696 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000697 Op->StartLoc = S;
698 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000699 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000700 }
701
Owen Anderson00828302011-03-18 22:50:18 +0000702 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
703 SMLoc S, SMLoc E) {
704 ARMOperand *Op = new ARMOperand(Shifter);
705 Op->Shift.ShiftTy = ShTy;
706 Op->StartLoc = S;
707 Op->EndLoc = E;
708 return Op;
709 }
710
Bill Wendling7729e062010-11-09 22:44:22 +0000711 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000712 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000713 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000714 KindTy Kind = RegisterList;
715
716 if (ARM::DPRRegClass.contains(Regs.front().first))
717 Kind = DPRRegisterList;
718 else if (ARM::SPRRegClass.contains(Regs.front().first))
719 Kind = SPRRegisterList;
720
721 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000722 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000723 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000724 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000725 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000726 Op->StartLoc = StartLoc;
727 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000728 return Op;
729 }
730
Chris Lattner3a697562010-10-28 17:20:03 +0000731 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
732 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000733 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000734 Op->StartLoc = S;
735 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000736 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000737 }
738
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000739 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
740 bool OffsetIsReg, const MCExpr *Offset,
741 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000742 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000743 const MCExpr *ShiftAmount, bool Preindexed,
744 bool Postindexed, bool Negative, bool Writeback,
745 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000746 assert((OffsetRegNum == -1 || OffsetIsReg) &&
747 "OffsetRegNum must imply OffsetIsReg!");
748 assert((!OffsetRegShifted || OffsetIsReg) &&
749 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000750 assert((Offset || OffsetIsReg) &&
751 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000752 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
753 "Cannot have shift amount without shifted register offset!");
754 assert((!Offset || !OffsetIsReg) &&
755 "Cannot have expression offset and register offset!");
756
Chris Lattner3a697562010-10-28 17:20:03 +0000757 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000758 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +0000759 Op->Mem.BaseRegNum = BaseRegNum;
760 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000761 if (OffsetIsReg)
762 Op->Mem.Offset.RegNum = OffsetRegNum;
763 else
764 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000765 Op->Mem.OffsetRegShifted = OffsetRegShifted;
766 Op->Mem.ShiftType = ShiftType;
767 Op->Mem.ShiftAmount = ShiftAmount;
768 Op->Mem.Preindexed = Preindexed;
769 Op->Mem.Postindexed = Postindexed;
770 Op->Mem.Negative = Negative;
771 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000772
Sean Callanan76264762010-04-02 22:27:05 +0000773 Op->StartLoc = S;
774 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000775 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000776 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000777
778 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
779 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
780 Op->MBOpt.Val = Opt;
781 Op->StartLoc = S;
782 Op->EndLoc = S;
783 return Op;
784 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000785
786 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
787 ARMOperand *Op = new ARMOperand(ProcIFlags);
788 Op->IFlags.Val = IFlags;
789 Op->StartLoc = S;
790 Op->EndLoc = S;
791 return Op;
792 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000793
794 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
795 ARMOperand *Op = new ARMOperand(MSRMask);
796 Op->MMask.Val = MMask;
797 Op->StartLoc = S;
798 Op->EndLoc = S;
799 return Op;
800 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000801};
802
803} // end anonymous namespace.
804
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000805void ARMOperand::dump(raw_ostream &OS) const {
806 switch (Kind) {
807 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000808 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000809 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000810 case CCOut:
811 OS << "<ccout " << getReg() << ">";
812 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000813 case CoprocNum:
814 OS << "<coprocessor number: " << getCoproc() << ">";
815 break;
816 case CoprocReg:
817 OS << "<coprocessor register: " << getCoproc() << ">";
818 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000819 case MSRMask:
820 OS << "<mask: " << getMSRMask() << ">";
821 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000822 case Immediate:
823 getImm()->print(OS);
824 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000825 case MemBarrierOpt:
826 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
827 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000828 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000829 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000830 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
831 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000832 if (getMemOffsetIsReg()) {
833 OS << " offset:<register " << getMemOffsetRegNum();
834 if (getMemOffsetRegShifted()) {
835 OS << " offset-shift-type:" << getMemShiftType();
836 OS << " offset-shift-amount:" << *getMemShiftAmount();
837 }
838 } else {
839 OS << " offset:" << *getMemOffset();
840 }
841 if (getMemOffsetIsReg())
842 OS << " (offset-is-reg)";
843 if (getMemPreindexed())
844 OS << " (pre-indexed)";
845 if (getMemPostindexed())
846 OS << " (post-indexed)";
847 if (getMemNegative())
848 OS << " (negative)";
849 if (getMemWriteback())
850 OS << " (writeback)";
851 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000852 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000853 case ProcIFlags: {
854 OS << "<ARM_PROC::";
855 unsigned IFlags = getProcIFlags();
856 for (int i=2; i >= 0; --i)
857 if (IFlags & (1 << i))
858 OS << ARM_PROC::IFlagsToString(1 << i);
859 OS << ">";
860 break;
861 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000862 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000863 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000864 break;
Owen Anderson00828302011-03-18 22:50:18 +0000865 case Shifter:
866 OS << "<shifter " << getShiftOpcStr(Shift.ShiftTy) << ">";
867 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000868 case RegisterList:
869 case DPRRegisterList:
870 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000871 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000872
Bill Wendling5fa22a12010-11-09 23:28:44 +0000873 const SmallVectorImpl<unsigned> &RegList = getRegList();
874 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000875 I = RegList.begin(), E = RegList.end(); I != E; ) {
876 OS << *I;
877 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000878 }
879
880 OS << ">";
881 break;
882 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000883 case Token:
884 OS << "'" << getToken() << "'";
885 break;
886 }
887}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000888
889/// @name Auto-generated Match Functions
890/// {
891
892static unsigned MatchRegisterName(StringRef Name);
893
894/// }
895
Bob Wilson69df7232011-02-03 21:46:10 +0000896bool ARMAsmParser::ParseRegister(unsigned &RegNo,
897 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +0000898 RegNo = TryParseRegister();
899
900 return (RegNo == (unsigned)-1);
901}
902
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000903/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000904/// and if it is a register name the token is eaten and the register number is
905/// returned. Otherwise return -1.
906///
907int ARMAsmParser::TryParseRegister() {
908 const AsmToken &Tok = Parser.getTok();
909 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000910
Chris Lattnere5658fa2010-10-30 04:09:10 +0000911 // FIXME: Validate register for the current architecture; we have to do
912 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000913 std::string upperCase = Tok.getString().str();
914 std::string lowerCase = LowercaseString(upperCase);
915 unsigned RegNum = MatchRegisterName(lowerCase);
916 if (!RegNum) {
917 RegNum = StringSwitch<unsigned>(lowerCase)
918 .Case("r13", ARM::SP)
919 .Case("r14", ARM::LR)
920 .Case("r15", ARM::PC)
921 .Case("ip", ARM::R12)
922 .Default(0);
923 }
924 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +0000925
Chris Lattnere5658fa2010-10-30 04:09:10 +0000926 Parser.Lex(); // Eat identifier token.
927 return RegNum;
928}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000929
Owen Anderson00828302011-03-18 22:50:18 +0000930/// Try to parse a register name. The token must be an Identifier when called,
931/// and if it is a register name the token is eaten and the register number is
932/// returned. Otherwise return -1.
933///
934bool ARMAsmParser::TryParseShiftRegister(
935 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
936 SMLoc S = Parser.getTok().getLoc();
937 const AsmToken &Tok = Parser.getTok();
938 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
939
940 std::string upperCase = Tok.getString().str();
941 std::string lowerCase = LowercaseString(upperCase);
942 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
943 .Case("lsl", ARM_AM::lsl)
944 .Case("lsr", ARM_AM::lsr)
945 .Case("asr", ARM_AM::asr)
946 .Case("ror", ARM_AM::ror)
947 .Case("rrx", ARM_AM::rrx)
948 .Default(ARM_AM::no_shift);
949
950 if (ShiftTy == ARM_AM::no_shift)
951 return true;
952
953 Parser.Lex(); // Eat shift-type operand;
954 int RegNum = TryParseRegister();
955 if (RegNum == -1)
956 return Error(Parser.getTok().getLoc(), "register expected");
957
958 Operands.push_back(ARMOperand::CreateReg(RegNum,S, Parser.getTok().getLoc()));
959 Operands.push_back(ARMOperand::CreateShifter(ShiftTy,
960 S, Parser.getTok().getLoc()));
961
962 return false;
963}
964
965
Bill Wendling50d0f582010-11-18 23:43:05 +0000966/// Try to parse a register name. The token must be an Identifier when called.
967/// If it's a register, an AsmOperand is created. Another AsmOperand is created
968/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000969///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000970/// TODO this is likely to change to allow different register types and or to
971/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000972bool ARMAsmParser::
973TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000974 SMLoc S = Parser.getTok().getLoc();
975 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000976 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000977 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000978
Bill Wendling50d0f582010-11-18 23:43:05 +0000979 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000980
Chris Lattnere5658fa2010-10-30 04:09:10 +0000981 const AsmToken &ExclaimTok = Parser.getTok();
982 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000983 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
984 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000985 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000986 }
987
Bill Wendling50d0f582010-11-18 23:43:05 +0000988 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000989}
990
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000991/// MatchCoprocessorOperandName - Try to parse an coprocessor related
992/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
993/// "c5", ...
994static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000995 // Use the same layout as the tablegen'erated register name matcher. Ugly,
996 // but efficient.
997 switch (Name.size()) {
998 default: break;
999 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001000 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001001 return -1;
1002 switch (Name[1]) {
1003 default: return -1;
1004 case '0': return 0;
1005 case '1': return 1;
1006 case '2': return 2;
1007 case '3': return 3;
1008 case '4': return 4;
1009 case '5': return 5;
1010 case '6': return 6;
1011 case '7': return 7;
1012 case '8': return 8;
1013 case '9': return 9;
1014 }
1015 break;
1016 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001017 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001018 return -1;
1019 switch (Name[2]) {
1020 default: return -1;
1021 case '0': return 10;
1022 case '1': return 11;
1023 case '2': return 12;
1024 case '3': return 13;
1025 case '4': return 14;
1026 case '5': return 15;
1027 }
1028 break;
1029 }
1030
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001031 return -1;
1032}
1033
Jim Grosbachf922c472011-02-12 01:34:40 +00001034/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001035/// token must be an Identifier when called, and if it is a coprocessor
1036/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001037ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1038tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001039 SMLoc S = Parser.getTok().getLoc();
1040 const AsmToken &Tok = Parser.getTok();
1041 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1042
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001043 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001044 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001045 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001046
1047 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001048 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001049 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001050}
1051
Jim Grosbachf922c472011-02-12 01:34:40 +00001052/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001053/// token must be an Identifier when called, and if it is a coprocessor
1054/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001055ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1056tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001057 SMLoc S = Parser.getTok().getLoc();
1058 const AsmToken &Tok = Parser.getTok();
1059 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1060
1061 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1062 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001063 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001064
1065 Parser.Lex(); // Eat identifier token.
1066 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001067 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001068}
1069
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001070/// Parse a register list, return it if successful else return null. The first
1071/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001072bool ARMAsmParser::
1073ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001074 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001075 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001076 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001077
Bill Wendling7729e062010-11-09 22:44:22 +00001078 // Read the rest of the registers in the list.
1079 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001080 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001081
Bill Wendling7729e062010-11-09 22:44:22 +00001082 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001083 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001084 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001085
Sean Callanan18b83232010-01-19 21:44:56 +00001086 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001087 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001088 if (RegTok.isNot(AsmToken::Identifier)) {
1089 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001090 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001091 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001092
Bill Wendling1d6a2652010-11-06 10:40:24 +00001093 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001094 if (RegNum == -1) {
1095 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001096 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001097 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001098
Bill Wendlinge7176102010-11-06 22:36:58 +00001099 if (IsRange) {
1100 int Reg = PrevRegNum;
1101 do {
1102 ++Reg;
1103 Registers.push_back(std::make_pair(Reg, RegLoc));
1104 } while (Reg != RegNum);
1105 } else {
1106 Registers.push_back(std::make_pair(RegNum, RegLoc));
1107 }
1108
1109 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001110 } while (Parser.getTok().is(AsmToken::Comma) ||
1111 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001112
1113 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001114 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001115 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1116 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001117 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001118 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001119
Bill Wendlinge7176102010-11-06 22:36:58 +00001120 SMLoc E = RCurlyTok.getLoc();
1121 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001122
Bill Wendlinge7176102010-11-06 22:36:58 +00001123 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001124 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001125 RI = Registers.begin(), RE = Registers.end();
1126
Bill Wendling7caebff2011-01-12 21:20:59 +00001127 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001128 bool EmittedWarning = false;
1129
Bill Wendling7caebff2011-01-12 21:20:59 +00001130 DenseMap<unsigned, bool> RegMap;
1131 RegMap[HighRegNum] = true;
1132
Bill Wendlinge7176102010-11-06 22:36:58 +00001133 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001134 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001135 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001136
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001137 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001138 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001139 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001140 }
1141
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001142 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001143 Warning(RegInfo.second,
1144 "register not in ascending order in register list");
1145
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001146 RegMap[Reg] = true;
1147 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001148 }
1149
Bill Wendling50d0f582010-11-18 23:43:05 +00001150 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1151 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001152}
1153
Jim Grosbachf922c472011-02-12 01:34:40 +00001154/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1155ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1156tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001157 SMLoc S = Parser.getTok().getLoc();
1158 const AsmToken &Tok = Parser.getTok();
1159 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1160 StringRef OptStr = Tok.getString();
1161
1162 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1163 .Case("sy", ARM_MB::SY)
1164 .Case("st", ARM_MB::ST)
1165 .Case("ish", ARM_MB::ISH)
1166 .Case("ishst", ARM_MB::ISHST)
1167 .Case("nsh", ARM_MB::NSH)
1168 .Case("nshst", ARM_MB::NSHST)
1169 .Case("osh", ARM_MB::OSH)
1170 .Case("oshst", ARM_MB::OSHST)
1171 .Default(~0U);
1172
1173 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001174 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001175
1176 Parser.Lex(); // Eat identifier token.
1177 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001178 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001179}
1180
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001181/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001182ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1183tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1184 SMLoc S = Parser.getTok().getLoc();
1185 const AsmToken &Tok = Parser.getTok();
1186 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1187 StringRef IFlagsStr = Tok.getString();
1188
1189 unsigned IFlags = 0;
1190 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1191 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1192 .Case("a", ARM_PROC::A)
1193 .Case("i", ARM_PROC::I)
1194 .Case("f", ARM_PROC::F)
1195 .Default(~0U);
1196
1197 // If some specific iflag is already set, it means that some letter is
1198 // present more than once, this is not acceptable.
1199 if (Flag == ~0U || (IFlags & Flag))
1200 return MatchOperand_NoMatch;
1201
1202 IFlags |= Flag;
1203 }
1204
1205 Parser.Lex(); // Eat identifier token.
1206 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1207 return MatchOperand_Success;
1208}
1209
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001210/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1211ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1212tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1213 SMLoc S = Parser.getTok().getLoc();
1214 const AsmToken &Tok = Parser.getTok();
1215 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1216 StringRef Mask = Tok.getString();
1217
1218 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1219 size_t Start = 0, Next = Mask.find('_');
1220 StringRef Flags = "";
1221 StringRef SpecReg = Mask.slice(Start, Next);
1222 if (Next != StringRef::npos)
1223 Flags = Mask.slice(Next+1, Mask.size());
1224
1225 // FlagsVal contains the complete mask:
1226 // 3-0: Mask
1227 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1228 unsigned FlagsVal = 0;
1229
1230 if (SpecReg == "apsr") {
1231 FlagsVal = StringSwitch<unsigned>(Flags)
1232 .Case("nzcvq", 0x8) // same as CPSR_c
1233 .Case("g", 0x4) // same as CPSR_s
1234 .Case("nzcvqg", 0xc) // same as CPSR_fs
1235 .Default(~0U);
1236
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001237 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001238 if (!Flags.empty())
1239 return MatchOperand_NoMatch;
1240 else
1241 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001242 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001243 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
1244 for (int i = 0, e = Flags.size(); i != e; ++i) {
1245 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1246 .Case("c", 1)
1247 .Case("x", 2)
1248 .Case("s", 4)
1249 .Case("f", 8)
1250 .Default(~0U);
1251
1252 // If some specific flag is already set, it means that some letter is
1253 // present more than once, this is not acceptable.
1254 if (FlagsVal == ~0U || (FlagsVal & Flag))
1255 return MatchOperand_NoMatch;
1256 FlagsVal |= Flag;
1257 }
1258 } else // No match for special register.
1259 return MatchOperand_NoMatch;
1260
1261 // Special register without flags are equivalent to "fc" flags.
1262 if (!FlagsVal)
1263 FlagsVal = 0x9;
1264
1265 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1266 if (SpecReg == "spsr")
1267 FlagsVal |= 16;
1268
1269 Parser.Lex(); // Eat identifier token.
1270 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1271 return MatchOperand_Success;
1272}
1273
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001274/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1275ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1276tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001277 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001278
1279 if (ParseMemory(Operands, ARMII::AddrMode2))
1280 return MatchOperand_NoMatch;
1281
1282 return MatchOperand_Success;
1283}
1284
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001285/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1286ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1287tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1288 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1289
1290 if (ParseMemory(Operands, ARMII::AddrMode3))
1291 return MatchOperand_NoMatch;
1292
1293 return MatchOperand_Success;
1294}
1295
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001296/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1297/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1298/// when they refer multiple MIOperands inside a single one.
1299bool ARMAsmParser::
1300CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1301 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1302 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1303
1304 // Create a writeback register dummy placeholder.
1305 Inst.addOperand(MCOperand::CreateImm(0));
1306
1307 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1308 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1309 return true;
1310}
1311
1312/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1313/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1314/// when they refer multiple MIOperands inside a single one.
1315bool ARMAsmParser::
1316CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1317 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1318 // Create a writeback register dummy placeholder.
1319 Inst.addOperand(MCOperand::CreateImm(0));
1320 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1321 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1322 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1323 return true;
1324}
1325
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001326/// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1327/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1328/// when they refer multiple MIOperands inside a single one.
1329bool ARMAsmParser::
1330CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1331 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1332 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1333
1334 // Create a writeback register dummy placeholder.
1335 Inst.addOperand(MCOperand::CreateImm(0));
1336
1337 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1338 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1339 return true;
1340}
1341
1342/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1343/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1344/// when they refer multiple MIOperands inside a single one.
1345bool ARMAsmParser::
1346CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1347 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1348 // Create a writeback register dummy placeholder.
1349 Inst.addOperand(MCOperand::CreateImm(0));
1350 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1351 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1352 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1353 return true;
1354}
1355
Bill Wendlinge7176102010-11-06 22:36:58 +00001356/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001357/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001358///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001359/// TODO Only preindexing and postindexing addressing are started, unindexed
1360/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001361bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001362ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1363 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001364 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001365 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001366 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001367 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001368 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001369
Sean Callanan18b83232010-01-19 21:44:56 +00001370 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001371 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1372 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001373 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001374 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001375 int BaseRegNum = TryParseRegister();
1376 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001377 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001378 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001379 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001380
Daniel Dunbar05710932011-01-18 05:34:17 +00001381 // The next token must either be a comma or a closing bracket.
1382 const AsmToken &Tok = Parser.getTok();
1383 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1384 return true;
1385
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001386 bool Preindexed = false;
1387 bool Postindexed = false;
1388 bool OffsetIsReg = false;
1389 bool Negative = false;
1390 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001391 ARMOperand *WBOp = 0;
1392 int OffsetRegNum = -1;
1393 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001394 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001395 const MCExpr *ShiftAmount = 0;
1396 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001397
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001398 // First look for preindexed address forms, that is after the "[Rn" we now
1399 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001400 if (Tok.is(AsmToken::Comma)) {
1401 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001402 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001403
Chris Lattner550276e2010-10-28 20:52:15 +00001404 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1405 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001406 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001407 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001408 if (RBracTok.isNot(AsmToken::RBrac)) {
1409 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001410 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001411 }
Sean Callanan76264762010-04-02 22:27:05 +00001412 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001413 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001414
Sean Callanan18b83232010-01-19 21:44:56 +00001415 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001416 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001417 // None of addrmode3 instruction uses "!"
1418 if (AddrMode == ARMII::AddrMode3)
1419 return true;
1420
Bill Wendling50d0f582010-11-18 23:43:05 +00001421 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1422 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001423 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001424 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001425 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1426 if (AddrMode == ARMII::AddrMode2)
1427 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001428 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001429 } else {
1430 // The "[Rn" we have so far was not followed by a comma.
1431
Jim Grosbach80eb2332010-10-29 17:41:25 +00001432 // If there's anything other than the right brace, this is a post indexing
1433 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001434 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001435 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001436
Sean Callanan18b83232010-01-19 21:44:56 +00001437 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001438
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001439 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001440 Postindexed = true;
1441 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001442
Chris Lattner550276e2010-10-28 20:52:15 +00001443 if (NextTok.isNot(AsmToken::Comma)) {
1444 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001445 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001446 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001447
Sean Callananb9a25b72010-01-19 20:27:46 +00001448 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001449
Chris Lattner550276e2010-10-28 20:52:15 +00001450 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001451 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001452 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001453 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001454 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001455 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001456
1457 // Force Offset to exist if used.
1458 if (!OffsetIsReg) {
1459 if (!Offset)
1460 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001461 } else {
1462 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1463 Error(E, "shift amount not supported");
1464 return true;
1465 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001466 }
1467
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001468 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1469 Offset, OffsetRegNum, OffsetRegShifted,
1470 ShiftType, ShiftAmount, Preindexed,
1471 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001472 if (WBOp)
1473 Operands.push_back(WBOp);
1474
1475 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001476}
1477
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001478/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1479/// we will parse the following (were +/- means that a plus or minus is
1480/// optional):
1481/// +/-Rm
1482/// +/-Rm, shift
1483/// #offset
1484/// we return false on success or an error otherwise.
1485bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001486 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001487 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001488 const MCExpr *&ShiftAmount,
1489 const MCExpr *&Offset,
1490 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001491 int &OffsetRegNum,
1492 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001493 Negative = false;
1494 OffsetRegShifted = false;
1495 OffsetIsReg = false;
1496 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001497 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001498 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001499 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001500 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001501 else if (NextTok.is(AsmToken::Minus)) {
1502 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001503 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001504 }
1505 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001506 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001507 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001508 SMLoc CurLoc = OffsetRegTok.getLoc();
1509 OffsetRegNum = TryParseRegister();
1510 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001511 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001512 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001513 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001514 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001515
Bill Wendling12f40e92010-11-06 10:51:53 +00001516 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001517 if (OffsetRegNum != -1) {
1518 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001519 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001520 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001521 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001522
Sean Callanan18b83232010-01-19 21:44:56 +00001523 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001524 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001525 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001526 OffsetRegShifted = true;
1527 }
1528 }
1529 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1530 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001531 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001532 if (HashTok.isNot(AsmToken::Hash))
1533 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001534
Sean Callananb9a25b72010-01-19 20:27:46 +00001535 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001536
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001537 if (getParser().ParseExpression(Offset))
1538 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001539 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001540 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001541 return false;
1542}
1543
1544/// ParseShift as one of these two:
1545/// ( lsl | lsr | asr | ror ) , # shift_amount
1546/// rrx
1547/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001548bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1549 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001550 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001551 if (Tok.isNot(AsmToken::Identifier))
1552 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001553 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001554 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001555 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001556 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001557 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001558 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001559 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001560 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001561 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001562 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001563 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001564 else
1565 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001566 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001567
1568 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001569 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001570 return false;
1571
1572 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001573 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001574 if (HashTok.isNot(AsmToken::Hash))
1575 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001576 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001577
1578 if (getParser().ParseExpression(ShiftAmount))
1579 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001580
1581 return false;
1582}
1583
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001584/// Parse a arm instruction operand. For now this parses the operand regardless
1585/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001586bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001587 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001588 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001589
1590 // Check if the current operand has a custom associated parser, if so, try to
1591 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001592 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1593 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001594 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001595 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1596 // there was a match, but an error occurred, in which case, just return that
1597 // the operand parsing failed.
1598 if (ResTy == MatchOperand_ParseFail)
1599 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001600
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001601 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001602 default:
1603 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001604 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +00001605 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +00001606 if (!TryParseRegisterWithWriteBack(Operands))
1607 return false;
Owen Anderson00828302011-03-18 22:50:18 +00001608 if (!TryParseShiftRegister(Operands))
1609 return false;
1610
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001611
1612 // Fall though for the Identifier case that is not a register or a
1613 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +00001614 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1615 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001616 // This was not a register so parse other operands that start with an
1617 // identifier (like labels) as expressions and create them as immediates.
1618 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001619 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001620 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001621 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001622 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001623 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1624 return false;
1625 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001626 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001627 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001628 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001629 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001630 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001631 // #42 -> immediate.
1632 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001633 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001634 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001635 const MCExpr *ImmVal;
1636 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001637 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001638 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001639 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1640 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001641 case AsmToken::Colon: {
1642 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001643 // FIXME: Check it's an expression prefix,
1644 // e.g. (FOO - :lower16:BAR) isn't legal.
1645 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001646 if (ParsePrefix(RefKind))
1647 return true;
1648
Evan Cheng75972122011-01-13 07:58:56 +00001649 const MCExpr *SubExprVal;
1650 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001651 return true;
1652
Evan Cheng75972122011-01-13 07:58:56 +00001653 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1654 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001655 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001656 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001657 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001658 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001659 }
1660}
1661
Evan Cheng75972122011-01-13 07:58:56 +00001662// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1663// :lower16: and :upper16:.
1664bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1665 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001666
1667 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001668 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001669 Parser.Lex(); // Eat ':'
1670
1671 if (getLexer().isNot(AsmToken::Identifier)) {
1672 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1673 return true;
1674 }
1675
1676 StringRef IDVal = Parser.getTok().getIdentifier();
1677 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001678 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001679 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001680 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001681 } else {
1682 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1683 return true;
1684 }
1685 Parser.Lex();
1686
1687 if (getLexer().isNot(AsmToken::Colon)) {
1688 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1689 return true;
1690 }
1691 Parser.Lex(); // Eat the last ':'
1692 return false;
1693}
1694
1695const MCExpr *
1696ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1697 MCSymbolRefExpr::VariantKind Variant) {
1698 // Recurse over the given expression, rebuilding it to apply the given variant
1699 // to the leftmost symbol.
1700 if (Variant == MCSymbolRefExpr::VK_None)
1701 return E;
1702
1703 switch (E->getKind()) {
1704 case MCExpr::Target:
1705 llvm_unreachable("Can't handle target expr yet");
1706 case MCExpr::Constant:
1707 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1708
1709 case MCExpr::SymbolRef: {
1710 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1711
1712 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1713 return 0;
1714
1715 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1716 }
1717
1718 case MCExpr::Unary:
1719 llvm_unreachable("Can't handle unary expressions yet");
1720
1721 case MCExpr::Binary: {
1722 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1723 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1724 const MCExpr *RHS = BE->getRHS();
1725 if (!LHS)
1726 return 0;
1727
1728 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1729 }
1730 }
1731
1732 assert(0 && "Invalid expression kind!");
1733 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001734}
1735
Daniel Dunbar352e1482011-01-11 15:59:50 +00001736/// \brief Given a mnemonic, split out possible predication code and carry
1737/// setting letters to form a canonical mnemonic and flags.
1738//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001739// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001740static StringRef SplitMnemonic(StringRef Mnemonic,
1741 unsigned &PredicationCode,
1742 bool &CarrySetting,
1743 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001744 PredicationCode = ARMCC::AL;
1745 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001746 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001747
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001748 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001749 //
1750 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001751 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1752 Mnemonic == "movs" ||
1753 Mnemonic == "svc" ||
1754 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1755 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1756 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1757 Mnemonic == "vclt" ||
1758 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1759 Mnemonic == "vcle" ||
1760 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1761 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1762 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001763 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001764
Daniel Dunbar352e1482011-01-11 15:59:50 +00001765 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001766 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001767 .Case("eq", ARMCC::EQ)
1768 .Case("ne", ARMCC::NE)
1769 .Case("hs", ARMCC::HS)
1770 .Case("lo", ARMCC::LO)
1771 .Case("mi", ARMCC::MI)
1772 .Case("pl", ARMCC::PL)
1773 .Case("vs", ARMCC::VS)
1774 .Case("vc", ARMCC::VC)
1775 .Case("hi", ARMCC::HI)
1776 .Case("ls", ARMCC::LS)
1777 .Case("ge", ARMCC::GE)
1778 .Case("lt", ARMCC::LT)
1779 .Case("gt", ARMCC::GT)
1780 .Case("le", ARMCC::LE)
1781 .Case("al", ARMCC::AL)
1782 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001783 if (CC != ~0U) {
1784 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001785 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001786 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001787
Daniel Dunbar352e1482011-01-11 15:59:50 +00001788 // Next, determine if we have a carry setting bit. We explicitly ignore all
1789 // the instructions we know end in 's'.
1790 if (Mnemonic.endswith("s") &&
1791 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1792 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1793 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1794 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1795 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1796 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1797 CarrySetting = true;
1798 }
1799
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001800 // The "cps" instruction can have a interrupt mode operand which is glued into
1801 // the mnemonic. Check if this is the case, split it and parse the imod op
1802 if (Mnemonic.startswith("cps")) {
1803 // Split out any imod code.
1804 unsigned IMod =
1805 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1806 .Case("ie", ARM_PROC::IE)
1807 .Case("id", ARM_PROC::ID)
1808 .Default(~0U);
1809 if (IMod != ~0U) {
1810 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
1811 ProcessorIMod = IMod;
1812 }
1813 }
1814
Daniel Dunbar352e1482011-01-11 15:59:50 +00001815 return Mnemonic;
1816}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001817
1818/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1819/// inclusion of carry set or predication code operands.
1820//
1821// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001822void ARMAsmParser::
1823GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1824 bool &CanAcceptPredicationCode) {
1825 bool isThumb = TM.getSubtarget<ARMSubtarget>().isThumb();
1826
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001827 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1828 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1829 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1830 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1831 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1832 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1833 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1834 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1835 CanAcceptCarrySet = true;
1836 } else {
1837 CanAcceptCarrySet = false;
1838 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001839
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001840 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1841 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1842 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1843 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00001844 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001845 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001846 CanAcceptPredicationCode = false;
1847 } else {
1848 CanAcceptPredicationCode = true;
1849 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001850
1851 if (isThumb)
1852 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Bruno Cardoso Lopes8dd37f72011-01-20 18:32:09 +00001853 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001854 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001855}
1856
1857/// Parse an arm instruction mnemonic followed by its operands.
1858bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1859 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1860 // Create the leading tokens for the mnemonic, split by '.' characters.
1861 size_t Start = 0, Next = Name.find('.');
1862 StringRef Head = Name.slice(Start, Next);
1863
Daniel Dunbar352e1482011-01-11 15:59:50 +00001864 // Split out the predication code and carry setting flag from the mnemonic.
1865 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001866 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001867 bool CarrySetting;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001868 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
1869 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001870
Chris Lattner3a697562010-10-28 17:20:03 +00001871 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001872
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001873 // Next, add the CCOut and ConditionCode operands, if needed.
1874 //
1875 // For mnemonics which can ever incorporate a carry setting bit or predication
1876 // code, our matching model involves us always generating CCOut and
1877 // ConditionCode operands to match the mnemonic "as written" and then we let
1878 // the matcher deal with finding the right instruction or generating an
1879 // appropriate error.
1880 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1881 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1882
1883 // Add the carry setting operand, if necessary.
1884 //
1885 // FIXME: It would be awesome if we could somehow invent a location such that
1886 // match errors on this operand would print a nice diagnostic about how the
1887 // 's' character in the mnemonic resulted in a CCOut operand.
1888 if (CanAcceptCarrySet) {
1889 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1890 NameLoc));
1891 } else {
1892 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1893 // misspelled another mnemonic).
1894
1895 // FIXME: Issue a nice error.
1896 }
1897
1898 // Add the predication code operand, if necessary.
1899 if (CanAcceptPredicationCode) {
1900 Operands.push_back(ARMOperand::CreateCondCode(
1901 ARMCC::CondCodes(PredicationCode), NameLoc));
1902 } else {
1903 // This mnemonic can't ever accept a predication code, but the user wrote
1904 // one (or misspelled another mnemonic).
1905
1906 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001907 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001908
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001909 // Add the processor imod operand, if necessary.
1910 if (ProcessorIMod) {
1911 Operands.push_back(ARMOperand::CreateImm(
1912 MCConstantExpr::Create(ProcessorIMod, getContext()),
1913 NameLoc, NameLoc));
1914 } else {
1915 // This mnemonic can't ever accept a imod, but the user wrote
1916 // one (or misspelled another mnemonic).
1917
1918 // FIXME: Issue a nice error.
1919 }
1920
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001921 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001922 while (Next != StringRef::npos) {
1923 Start = Next;
1924 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001925 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001926
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001927 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001928 }
1929
1930 // Read the remaining operands.
1931 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001932 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001933 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001934 Parser.EatToEndOfStatement();
1935 return true;
1936 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001937
1938 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001939 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001940
1941 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001942 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001943 Parser.EatToEndOfStatement();
1944 return true;
1945 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001946 }
1947 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001948
Chris Lattnercbf8a982010-09-11 16:18:25 +00001949 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1950 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001951 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001952 }
Bill Wendling146018f2010-11-06 21:42:12 +00001953
Chris Lattner34e53142010-09-08 05:10:46 +00001954 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001955 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001956}
1957
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001958bool ARMAsmParser::
1959MatchAndEmitInstruction(SMLoc IDLoc,
1960 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1961 MCStreamer &Out) {
1962 MCInst Inst;
1963 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001964 MatchResultTy MatchResult, MatchResult2;
1965 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1966 if (MatchResult != Match_Success) {
1967 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1968 // that does not update the condition codes. So try adding a CCOut operand
1969 // with a value of reg0.
1970 if (MatchResult == Match_InvalidOperand) {
1971 Operands.insert(Operands.begin() + 1,
1972 ARMOperand::CreateCCOut(0,
1973 ((ARMOperand*)Operands[0])->getStartLoc()));
1974 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1975 if (MatchResult2 == Match_Success)
1976 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001977 else {
1978 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001979 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001980 delete CCOut;
1981 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001982 }
1983 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1984 // that updates the condition codes if it ends in 's'. So see if the
1985 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1986 // operand with a value of CPSR.
1987 else if(MatchResult == Match_MnemonicFail) {
1988 // Get the instruction mnemonic, which is the first token.
1989 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1990 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1991 // removed the 's' from the mnemonic for matching.
1992 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1993 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001994 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1995 Operands.erase(Operands.begin());
1996 delete OldMnemonic;
1997 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001998 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1999 Operands.insert(Operands.begin() + 1,
2000 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
2001 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2002 if (MatchResult2 == Match_Success)
2003 MatchResult = Match_Success;
2004 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002005 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2006 Operands.erase(Operands.begin());
2007 delete OldMnemonic;
2008 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002009 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002010 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
2011 Operands.erase(Operands.begin() + 1);
2012 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002013 }
2014 }
2015 }
2016 }
2017 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002018 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002019 Out.EmitInstruction(Inst);
2020 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002021 case Match_MissingFeature:
2022 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2023 return true;
2024 case Match_InvalidOperand: {
2025 SMLoc ErrorLoc = IDLoc;
2026 if (ErrorInfo != ~0U) {
2027 if (ErrorInfo >= Operands.size())
2028 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002029
Chris Lattnere73d4f82010-10-28 21:41:58 +00002030 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2031 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2032 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002033
Chris Lattnere73d4f82010-10-28 21:41:58 +00002034 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002035 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002036 case Match_MnemonicFail:
2037 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002038 case Match_ConversionFail:
2039 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002040 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002041
Eric Christopherc223e2b2010-10-29 09:26:59 +00002042 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002043 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002044}
2045
Kevin Enderby515d5092009-10-15 20:48:48 +00002046/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002047bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2048 StringRef IDVal = DirectiveID.getIdentifier();
2049 if (IDVal == ".word")
2050 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002051 else if (IDVal == ".thumb")
2052 return ParseDirectiveThumb(DirectiveID.getLoc());
2053 else if (IDVal == ".thumb_func")
2054 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2055 else if (IDVal == ".code")
2056 return ParseDirectiveCode(DirectiveID.getLoc());
2057 else if (IDVal == ".syntax")
2058 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002059 return true;
2060}
2061
2062/// ParseDirectiveWord
2063/// ::= .word [ expression (, expression)* ]
2064bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2065 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2066 for (;;) {
2067 const MCExpr *Value;
2068 if (getParser().ParseExpression(Value))
2069 return true;
2070
Chris Lattneraaec2052010-01-19 19:46:13 +00002071 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002072
2073 if (getLexer().is(AsmToken::EndOfStatement))
2074 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002075
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002076 // FIXME: Improve diagnostic.
2077 if (getLexer().isNot(AsmToken::Comma))
2078 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002079 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002080 }
2081 }
2082
Sean Callananb9a25b72010-01-19 20:27:46 +00002083 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002084 return false;
2085}
2086
Kevin Enderby515d5092009-10-15 20:48:48 +00002087/// ParseDirectiveThumb
2088/// ::= .thumb
2089bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2090 if (getLexer().isNot(AsmToken::EndOfStatement))
2091 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002092 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002093
2094 // TODO: set thumb mode
2095 // TODO: tell the MC streamer the mode
2096 // getParser().getStreamer().Emit???();
2097 return false;
2098}
2099
2100/// ParseDirectiveThumbFunc
2101/// ::= .thumbfunc symbol_name
2102bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00002103 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
2104 bool isMachO = MAI.hasSubsectionsViaSymbols();
2105 StringRef Name;
2106
2107 // Darwin asm has function name after .thumb_func direction
2108 // ELF doesn't
2109 if (isMachO) {
2110 const AsmToken &Tok = Parser.getTok();
2111 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2112 return Error(L, "unexpected token in .thumb_func directive");
2113 Name = Tok.getString();
2114 Parser.Lex(); // Consume the identifier token.
2115 }
2116
Kevin Enderby515d5092009-10-15 20:48:48 +00002117 if (getLexer().isNot(AsmToken::EndOfStatement))
2118 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002119 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002120
Rafael Espindola64695402011-05-16 16:17:21 +00002121 // FIXME: assuming function name will be the line following .thumb_func
2122 if (!isMachO) {
2123 Name = Parser.getTok().getString();
2124 }
2125
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002126 // Mark symbol as a thumb symbol.
2127 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2128 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002129 return false;
2130}
2131
2132/// ParseDirectiveSyntax
2133/// ::= .syntax unified | divided
2134bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002135 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002136 if (Tok.isNot(AsmToken::Identifier))
2137 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002138 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002139 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002140 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002141 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002142 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002143 else
2144 return Error(L, "unrecognized syntax mode in .syntax directive");
2145
2146 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002147 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002148 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002149
2150 // TODO tell the MC streamer the mode
2151 // getParser().getStreamer().Emit???();
2152 return false;
2153}
2154
2155/// ParseDirectiveCode
2156/// ::= .code 16 | 32
2157bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002158 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002159 if (Tok.isNot(AsmToken::Integer))
2160 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002161 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002162 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002163 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002164 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002165 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002166 else
2167 return Error(L, "invalid operand to .code directive");
2168
2169 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002170 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002171 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002172
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00002173 // FIXME: We need to be able switch subtargets at this point so that
2174 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
2175 // includes Feature_IsThumb or not to match the right instructions. This is
2176 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
2177 if (Val == 16){
2178 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
2179 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00002180 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00002181 }
2182 else{
2183 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
2184 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00002185 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00002186 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002187
Kevin Enderby515d5092009-10-15 20:48:48 +00002188 return false;
2189}
2190
Sean Callanan90b70972010-04-07 20:29:34 +00002191extern "C" void LLVMInitializeARMAsmLexer();
2192
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002193/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002194extern "C" void LLVMInitializeARMAsmParser() {
2195 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2196 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002197 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002198}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002199
Chris Lattner0692ee62010-09-06 19:11:01 +00002200#define GET_REGISTER_MATCHER
2201#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002202#include "ARMGenAsmMatcher.inc"