blob: e9a507088a615c192b2645e8ce4e6fb6e3d98f55 [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"
Jim Grosbach642fc9c2010-11-05 22:33:53 +000018#include "llvm/MC/MCContext.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000019#include "llvm/MC/MCStreamer.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCInst.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000022#include "llvm/Target/TargetRegistry.h"
23#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000026#include "llvm/ADT/SmallVector.h"
Owen Anderson0c9f2502011-01-13 22:50:36 +000027#include "llvm/ADT/StringExtras.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000028#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000029#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000030using namespace llvm;
31
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +000032/// Shift types used for register controlled shifts in ARM memory addressing.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000033enum ShiftType {
34 Lsl,
35 Lsr,
36 Asr,
37 Ror,
38 Rrx
39};
40
Chris Lattner3a697562010-10-28 17:20:03 +000041namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000042
43class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000044
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000045class ARMAsmParser : public TargetAsmParser {
46 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000047 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000048
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000049 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000050 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
51
52 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000053 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
54
Chris Lattnere5658fa2010-10-30 04:09:10 +000055 int TryParseRegister();
Roman Divackybf755322011-01-27 17:14:22 +000056 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Bill Wendling50d0f582010-11-18 23:43:05 +000057 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
58 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
59 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +000060 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
Evan Cheng75972122011-01-13 07:58:56 +000061 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000062 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
63 MCSymbolRefExpr::VariantKind Variant);
64
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000065
Kevin Enderby9c41fa82009-10-30 22:55:57 +000066 bool ParseMemoryOffsetReg(bool &Negative,
67 bool &OffsetRegShifted,
68 enum ShiftType &ShiftType,
69 const MCExpr *&ShiftAmount,
70 const MCExpr *&Offset,
71 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000072 int &OffsetRegNum,
73 SMLoc &E);
Sean Callanan76264762010-04-02 22:27:05 +000074 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000075 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000076 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000077 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000078 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000079 bool ParseDirectiveSyntax(SMLoc L);
80
Chris Lattner7036f8b2010-09-29 01:42:58 +000081 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000082 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000083 MCStreamer &Out);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000084 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
85 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000086
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000087 /// @name Auto-generated Match Functions
88 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000089
Chris Lattner0692ee62010-09-06 19:11:01 +000090#define GET_ASSEMBLER_HEADER
91#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000092
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000093 /// }
94
Jim Grosbachf922c472011-02-12 01:34:40 +000095 OperandMatchResultTy tryParseCoprocNumOperand(
96 SmallVectorImpl<MCParsedAsmOperand*>&);
97 OperandMatchResultTy tryParseCoprocRegOperand(
98 SmallVectorImpl<MCParsedAsmOperand*>&);
99 OperandMatchResultTy tryParseMemBarrierOptOperand(
100 SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000101 OperandMatchResultTy tryParseProcIFlagsOperand(
102 SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbachf922c472011-02-12 01:34:40 +0000103
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000104public:
Daniel Dunbard73ada72010-07-19 00:33:49 +0000105 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +0000106 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
107 // Initialize the set of available features.
108 setAvailableFeatures(ComputeAvailableFeatures(
109 &TM.getSubtarget<ARMSubtarget>()));
110 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000111
Benjamin Kramer38e59892010-07-14 22:38:02 +0000112 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000113 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000114 virtual bool ParseDirective(AsmToken DirectiveID);
115};
Jim Grosbach16c74252010-10-29 14:46:02 +0000116} // end anonymous namespace
117
Chris Lattner3a697562010-10-28 17:20:03 +0000118namespace {
119
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000120/// ARMOperand - Instances of this class represent a parsed ARM machine
121/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000122class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000123 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000124 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000125 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000126 CoprocNum,
127 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000128 Immediate,
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000129 MemBarrierOpt,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000130 Memory,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000131 ProcIFlags,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000132 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000133 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000134 DPRRegisterList,
135 SPRRegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000136 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000137 } Kind;
138
Sean Callanan76264762010-04-02 22:27:05 +0000139 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000140 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000141
142 union {
143 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000144 ARMCC::CondCodes Val;
145 } CC;
146
147 struct {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000148 ARM_MB::MemBOpt Val;
149 } MBOpt;
150
151 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000152 unsigned Val;
153 } Cop;
154
155 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000156 ARM_PROC::IFlags Val;
157 } IFlags;
158
159 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000160 const char *Data;
161 unsigned Length;
162 } Tok;
163
164 struct {
165 unsigned RegNum;
166 } Reg;
167
Bill Wendling8155e5b2010-11-06 22:19:43 +0000168 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000169 const MCExpr *Val;
170 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000171
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000172 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000173 struct {
174 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000175 union {
176 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
177 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
178 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000179 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
180 enum ShiftType ShiftType; // used when OffsetRegShifted is true
181 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000182 unsigned Preindexed : 1;
183 unsigned Postindexed : 1;
184 unsigned OffsetIsReg : 1;
185 unsigned Negative : 1; // only used when OffsetIsReg is true
186 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000187 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000188 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000189
Bill Wendling146018f2010-11-06 21:42:12 +0000190 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
191public:
Sean Callanan76264762010-04-02 22:27:05 +0000192 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
193 Kind = o.Kind;
194 StartLoc = o.StartLoc;
195 EndLoc = o.EndLoc;
196 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000197 case CondCode:
198 CC = o.CC;
199 break;
Sean Callanan76264762010-04-02 22:27:05 +0000200 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000201 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000202 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000203 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000204 case Register:
205 Reg = o.Reg;
206 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000207 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000208 case DPRRegisterList:
209 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000210 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000211 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000212 case CoprocNum:
213 case CoprocReg:
214 Cop = o.Cop;
215 break;
Sean Callanan76264762010-04-02 22:27:05 +0000216 case Immediate:
217 Imm = o.Imm;
218 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000219 case MemBarrierOpt:
220 MBOpt = o.MBOpt;
221 break;
Sean Callanan76264762010-04-02 22:27:05 +0000222 case Memory:
223 Mem = o.Mem;
224 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000225 case ProcIFlags:
226 IFlags = o.IFlags;
Sean Callanan76264762010-04-02 22:27:05 +0000227 }
228 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000229
Sean Callanan76264762010-04-02 22:27:05 +0000230 /// getStartLoc - Get the location of the first token of this operand.
231 SMLoc getStartLoc() const { return StartLoc; }
232 /// getEndLoc - Get the location of the last token of this operand.
233 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000234
Daniel Dunbar8462b302010-08-11 06:36:53 +0000235 ARMCC::CondCodes getCondCode() const {
236 assert(Kind == CondCode && "Invalid access!");
237 return CC.Val;
238 }
239
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000240 unsigned getCoproc() const {
241 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
242 return Cop.Val;
243 }
244
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000245 StringRef getToken() const {
246 assert(Kind == Token && "Invalid access!");
247 return StringRef(Tok.Data, Tok.Length);
248 }
249
250 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000251 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000252 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000253 }
254
Bill Wendling5fa22a12010-11-09 23:28:44 +0000255 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000256 assert((Kind == RegisterList || Kind == DPRRegisterList ||
257 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000258 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000259 }
260
Kevin Enderbycfe07242009-10-13 22:19:02 +0000261 const MCExpr *getImm() const {
262 assert(Kind == Immediate && "Invalid access!");
263 return Imm.Val;
264 }
265
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000266 ARM_MB::MemBOpt getMemBarrierOpt() const {
267 assert(Kind == MemBarrierOpt && "Invalid access!");
268 return MBOpt.Val;
269 }
270
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000271 ARM_PROC::IFlags getProcIFlags() const {
272 assert(Kind == ProcIFlags && "Invalid access!");
273 return IFlags.Val;
274 }
275
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000276 /// @name Memory Operand Accessors
277 /// @{
278
279 unsigned getMemBaseRegNum() const {
280 return Mem.BaseRegNum;
281 }
282 unsigned getMemOffsetRegNum() const {
283 assert(Mem.OffsetIsReg && "Invalid access!");
284 return Mem.Offset.RegNum;
285 }
286 const MCExpr *getMemOffset() const {
287 assert(!Mem.OffsetIsReg && "Invalid access!");
288 return Mem.Offset.Value;
289 }
290 unsigned getMemOffsetRegShifted() const {
291 assert(Mem.OffsetIsReg && "Invalid access!");
292 return Mem.OffsetRegShifted;
293 }
294 const MCExpr *getMemShiftAmount() const {
295 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
296 return Mem.ShiftAmount;
297 }
298 enum ShiftType getMemShiftType() const {
299 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
300 return Mem.ShiftType;
301 }
302 bool getMemPreindexed() const { return Mem.Preindexed; }
303 bool getMemPostindexed() const { return Mem.Postindexed; }
304 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
305 bool getMemNegative() const { return Mem.Negative; }
306 bool getMemWriteback() const { return Mem.Writeback; }
307
308 /// @}
309
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000310 bool isCoprocNum() const { return Kind == CoprocNum; }
311 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000312 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000313 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000314 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000315 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000316 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000317 bool isDPRRegList() const { return Kind == DPRRegisterList; }
318 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000319 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000320 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000321 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000322 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000323 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
324 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000325 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000326
Daniel Dunbar4b462672011-01-18 05:55:27 +0000327 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000328 if (!CE) return false;
329
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000330 // The offset must be a multiple of 4 in the range 0-1020.
331 int64_t Value = CE->getValue();
332 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
333 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000334 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000335 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000336 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000337 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000338 }
339 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000340 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000341 return false;
342
Daniel Dunbar4b462672011-01-18 05:55:27 +0000343 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000344 if (!CE) return false;
345
346 // The offset must be a multiple of 4 in the range 0-124.
347 uint64_t Value = CE->getValue();
348 return ((Value & 0x3) == 0 && Value <= 124);
349 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000350 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000351
352 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000353 // Add as immediates when possible. Null MCExpr = 0.
354 if (Expr == 0)
355 Inst.addOperand(MCOperand::CreateImm(0));
356 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000357 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
358 else
359 Inst.addOperand(MCOperand::CreateExpr(Expr));
360 }
361
Daniel Dunbar8462b302010-08-11 06:36:53 +0000362 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000363 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000364 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000365 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
366 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000367 }
368
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000369 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
370 assert(N == 1 && "Invalid number of operands!");
371 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
372 }
373
374 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
375 assert(N == 1 && "Invalid number of operands!");
376 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
377 }
378
Jim Grosbachd67641b2010-12-06 18:21:12 +0000379 void addCCOutOperands(MCInst &Inst, unsigned N) const {
380 assert(N == 1 && "Invalid number of operands!");
381 Inst.addOperand(MCOperand::CreateReg(getReg()));
382 }
383
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000384 void addRegOperands(MCInst &Inst, unsigned N) const {
385 assert(N == 1 && "Invalid number of operands!");
386 Inst.addOperand(MCOperand::CreateReg(getReg()));
387 }
388
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000389 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000390 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000391 const SmallVectorImpl<unsigned> &RegList = getRegList();
392 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000393 I = RegList.begin(), E = RegList.end(); I != E; ++I)
394 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000395 }
396
Bill Wendling0f630752010-11-17 04:32:08 +0000397 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
398 addRegListOperands(Inst, N);
399 }
400
401 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
402 addRegListOperands(Inst, N);
403 }
404
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000405 void addImmOperands(MCInst &Inst, unsigned N) const {
406 assert(N == 1 && "Invalid number of operands!");
407 addExpr(Inst, getImm());
408 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000409
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000410 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
411 assert(N == 1 && "Invalid number of operands!");
412 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
413 }
414
Chris Lattner14b93852010-10-29 00:27:31 +0000415 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
416 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000417
Daniel Dunbar4b462672011-01-18 05:55:27 +0000418 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
419 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000420
Jim Grosbach80eb2332010-10-29 17:41:25 +0000421 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
422 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000423 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000424 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000425
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000426 // The MCInst offset operand doesn't include the low two bits (like
427 // the instruction encoding).
428 int64_t Offset = CE->getValue() / 4;
429 if (Offset >= 0)
430 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
431 Offset)));
432 else
433 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
434 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000435 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000436
Bill Wendlingf4caf692010-12-14 03:36:38 +0000437 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
438 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000439 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
440 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000441 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000442
Bill Wendlingf4caf692010-12-14 03:36:38 +0000443 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
444 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000445 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
446 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000447 assert(CE && "Non-constant mode offset operand!");
448 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000449 }
450
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000451 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
452 assert(N == 1 && "Invalid number of operands!");
453 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
454 }
455
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000456 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000457
Chris Lattner3a697562010-10-28 17:20:03 +0000458 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
459 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000460 Op->CC.Val = CC;
461 Op->StartLoc = S;
462 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000463 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000464 }
465
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000466 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
467 ARMOperand *Op = new ARMOperand(CoprocNum);
468 Op->Cop.Val = CopVal;
469 Op->StartLoc = S;
470 Op->EndLoc = S;
471 return Op;
472 }
473
474 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
475 ARMOperand *Op = new ARMOperand(CoprocReg);
476 Op->Cop.Val = CopVal;
477 Op->StartLoc = S;
478 Op->EndLoc = S;
479 return Op;
480 }
481
Jim Grosbachd67641b2010-12-06 18:21:12 +0000482 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
483 ARMOperand *Op = new ARMOperand(CCOut);
484 Op->Reg.RegNum = RegNum;
485 Op->StartLoc = S;
486 Op->EndLoc = S;
487 return Op;
488 }
489
Chris Lattner3a697562010-10-28 17:20:03 +0000490 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
491 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000492 Op->Tok.Data = Str.data();
493 Op->Tok.Length = Str.size();
494 Op->StartLoc = S;
495 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000496 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000497 }
498
Bill Wendling50d0f582010-11-18 23:43:05 +0000499 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000500 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000501 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000502 Op->StartLoc = S;
503 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000504 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000505 }
506
Bill Wendling7729e062010-11-09 22:44:22 +0000507 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000508 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000509 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000510 KindTy Kind = RegisterList;
511
512 if (ARM::DPRRegClass.contains(Regs.front().first))
513 Kind = DPRRegisterList;
514 else if (ARM::SPRRegClass.contains(Regs.front().first))
515 Kind = SPRRegisterList;
516
517 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000518 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000519 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000520 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000521 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000522 Op->StartLoc = StartLoc;
523 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000524 return Op;
525 }
526
Chris Lattner3a697562010-10-28 17:20:03 +0000527 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
528 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000529 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000530 Op->StartLoc = S;
531 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000532 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000533 }
534
Chris Lattner3a697562010-10-28 17:20:03 +0000535 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
Daniel Dunbar023835d2011-01-18 05:34:05 +0000536 const MCExpr *Offset, int OffsetRegNum,
Chris Lattner3a697562010-10-28 17:20:03 +0000537 bool OffsetRegShifted, enum ShiftType ShiftType,
538 const MCExpr *ShiftAmount, bool Preindexed,
539 bool Postindexed, bool Negative, bool Writeback,
540 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000541 assert((OffsetRegNum == -1 || OffsetIsReg) &&
542 "OffsetRegNum must imply OffsetIsReg!");
543 assert((!OffsetRegShifted || OffsetIsReg) &&
544 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000545 assert((Offset || OffsetIsReg) &&
546 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000547 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
548 "Cannot have shift amount without shifted register offset!");
549 assert((!Offset || !OffsetIsReg) &&
550 "Cannot have expression offset and register offset!");
551
Chris Lattner3a697562010-10-28 17:20:03 +0000552 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000553 Op->Mem.BaseRegNum = BaseRegNum;
554 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000555 if (OffsetIsReg)
556 Op->Mem.Offset.RegNum = OffsetRegNum;
557 else
558 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000559 Op->Mem.OffsetRegShifted = OffsetRegShifted;
560 Op->Mem.ShiftType = ShiftType;
561 Op->Mem.ShiftAmount = ShiftAmount;
562 Op->Mem.Preindexed = Preindexed;
563 Op->Mem.Postindexed = Postindexed;
564 Op->Mem.Negative = Negative;
565 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000566
Sean Callanan76264762010-04-02 22:27:05 +0000567 Op->StartLoc = S;
568 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000569 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000570 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000571
572 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
573 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
574 Op->MBOpt.Val = Opt;
575 Op->StartLoc = S;
576 Op->EndLoc = S;
577 return Op;
578 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000579
580 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
581 ARMOperand *Op = new ARMOperand(ProcIFlags);
582 Op->IFlags.Val = IFlags;
583 Op->StartLoc = S;
584 Op->EndLoc = S;
585 return Op;
586 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000587};
588
589} // end anonymous namespace.
590
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000591void ARMOperand::dump(raw_ostream &OS) const {
592 switch (Kind) {
593 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000594 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000595 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000596 case CCOut:
597 OS << "<ccout " << getReg() << ">";
598 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000599 case CoprocNum:
600 OS << "<coprocessor number: " << getCoproc() << ">";
601 break;
602 case CoprocReg:
603 OS << "<coprocessor register: " << getCoproc() << ">";
604 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000605 case Immediate:
606 getImm()->print(OS);
607 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000608 case MemBarrierOpt:
609 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
610 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000611 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000612 OS << "<memory "
613 << "base:" << getMemBaseRegNum();
614 if (getMemOffsetIsReg()) {
615 OS << " offset:<register " << getMemOffsetRegNum();
616 if (getMemOffsetRegShifted()) {
617 OS << " offset-shift-type:" << getMemShiftType();
618 OS << " offset-shift-amount:" << *getMemShiftAmount();
619 }
620 } else {
621 OS << " offset:" << *getMemOffset();
622 }
623 if (getMemOffsetIsReg())
624 OS << " (offset-is-reg)";
625 if (getMemPreindexed())
626 OS << " (pre-indexed)";
627 if (getMemPostindexed())
628 OS << " (post-indexed)";
629 if (getMemNegative())
630 OS << " (negative)";
631 if (getMemWriteback())
632 OS << " (writeback)";
633 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000634 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000635 case ProcIFlags: {
636 OS << "<ARM_PROC::";
637 unsigned IFlags = getProcIFlags();
638 for (int i=2; i >= 0; --i)
639 if (IFlags & (1 << i))
640 OS << ARM_PROC::IFlagsToString(1 << i);
641 OS << ">";
642 break;
643 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000644 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000645 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000646 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000647 case RegisterList:
648 case DPRRegisterList:
649 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000650 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000651
Bill Wendling5fa22a12010-11-09 23:28:44 +0000652 const SmallVectorImpl<unsigned> &RegList = getRegList();
653 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000654 I = RegList.begin(), E = RegList.end(); I != E; ) {
655 OS << *I;
656 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000657 }
658
659 OS << ">";
660 break;
661 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000662 case Token:
663 OS << "'" << getToken() << "'";
664 break;
665 }
666}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000667
668/// @name Auto-generated Match Functions
669/// {
670
671static unsigned MatchRegisterName(StringRef Name);
672
673/// }
674
Bob Wilson69df7232011-02-03 21:46:10 +0000675bool ARMAsmParser::ParseRegister(unsigned &RegNo,
676 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +0000677 RegNo = TryParseRegister();
678
679 return (RegNo == (unsigned)-1);
680}
681
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000682/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000683/// and if it is a register name the token is eaten and the register number is
684/// returned. Otherwise return -1.
685///
686int ARMAsmParser::TryParseRegister() {
687 const AsmToken &Tok = Parser.getTok();
688 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000689
Chris Lattnere5658fa2010-10-30 04:09:10 +0000690 // FIXME: Validate register for the current architecture; we have to do
691 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000692 std::string upperCase = Tok.getString().str();
693 std::string lowerCase = LowercaseString(upperCase);
694 unsigned RegNum = MatchRegisterName(lowerCase);
695 if (!RegNum) {
696 RegNum = StringSwitch<unsigned>(lowerCase)
697 .Case("r13", ARM::SP)
698 .Case("r14", ARM::LR)
699 .Case("r15", ARM::PC)
700 .Case("ip", ARM::R12)
701 .Default(0);
702 }
703 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +0000704
Chris Lattnere5658fa2010-10-30 04:09:10 +0000705 Parser.Lex(); // Eat identifier token.
706 return RegNum;
707}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000708
Bill Wendling50d0f582010-11-18 23:43:05 +0000709/// Try to parse a register name. The token must be an Identifier when called.
710/// If it's a register, an AsmOperand is created. Another AsmOperand is created
711/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000712///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000713/// TODO this is likely to change to allow different register types and or to
714/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000715bool ARMAsmParser::
716TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000717 SMLoc S = Parser.getTok().getLoc();
718 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000719 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000720 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000721
Bill Wendling50d0f582010-11-18 23:43:05 +0000722 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000723
Chris Lattnere5658fa2010-10-30 04:09:10 +0000724 const AsmToken &ExclaimTok = Parser.getTok();
725 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000726 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
727 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000728 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000729 }
730
Bill Wendling50d0f582010-11-18 23:43:05 +0000731 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000732}
733
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000734/// MatchCoprocessorOperandName - Try to parse an coprocessor related
735/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
736/// "c5", ...
737static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000738 // Use the same layout as the tablegen'erated register name matcher. Ugly,
739 // but efficient.
740 switch (Name.size()) {
741 default: break;
742 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000743 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000744 return -1;
745 switch (Name[1]) {
746 default: return -1;
747 case '0': return 0;
748 case '1': return 1;
749 case '2': return 2;
750 case '3': return 3;
751 case '4': return 4;
752 case '5': return 5;
753 case '6': return 6;
754 case '7': return 7;
755 case '8': return 8;
756 case '9': return 9;
757 }
758 break;
759 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000760 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000761 return -1;
762 switch (Name[2]) {
763 default: return -1;
764 case '0': return 10;
765 case '1': return 11;
766 case '2': return 12;
767 case '3': return 13;
768 case '4': return 14;
769 case '5': return 15;
770 }
771 break;
772 }
773
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000774 return -1;
775}
776
Jim Grosbachf922c472011-02-12 01:34:40 +0000777/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000778/// token must be an Identifier when called, and if it is a coprocessor
779/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +0000780ARMAsmParser::OperandMatchResultTy ARMAsmParser::
781tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000782 SMLoc S = Parser.getTok().getLoc();
783 const AsmToken &Tok = Parser.getTok();
784 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
785
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000786 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000787 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +0000788 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000789
790 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000791 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +0000792 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000793}
794
Jim Grosbachf922c472011-02-12 01:34:40 +0000795/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000796/// token must be an Identifier when called, and if it is a coprocessor
797/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +0000798ARMAsmParser::OperandMatchResultTy ARMAsmParser::
799tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000800 SMLoc S = Parser.getTok().getLoc();
801 const AsmToken &Tok = Parser.getTok();
802 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
803
804 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
805 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +0000806 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000807
808 Parser.Lex(); // Eat identifier token.
809 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +0000810 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000811}
812
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000813/// Parse a register list, return it if successful else return null. The first
814/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000815bool ARMAsmParser::
816ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +0000817 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000818 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000819 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000820
Bill Wendling7729e062010-11-09 22:44:22 +0000821 // Read the rest of the registers in the list.
822 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000823 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000824
Bill Wendling7729e062010-11-09 22:44:22 +0000825 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000826 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000827 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000828
Sean Callanan18b83232010-01-19 21:44:56 +0000829 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000830 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000831 if (RegTok.isNot(AsmToken::Identifier)) {
832 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000833 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000834 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000835
Bill Wendling1d6a2652010-11-06 10:40:24 +0000836 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000837 if (RegNum == -1) {
838 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000839 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000840 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000841
Bill Wendlinge7176102010-11-06 22:36:58 +0000842 if (IsRange) {
843 int Reg = PrevRegNum;
844 do {
845 ++Reg;
846 Registers.push_back(std::make_pair(Reg, RegLoc));
847 } while (Reg != RegNum);
848 } else {
849 Registers.push_back(std::make_pair(RegNum, RegLoc));
850 }
851
852 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000853 } while (Parser.getTok().is(AsmToken::Comma) ||
854 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000855
856 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000857 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000858 if (RCurlyTok.isNot(AsmToken::RCurly)) {
859 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000860 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000861 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000862
Bill Wendlinge7176102010-11-06 22:36:58 +0000863 SMLoc E = RCurlyTok.getLoc();
864 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +0000865
Bill Wendlinge7176102010-11-06 22:36:58 +0000866 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000867 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000868 RI = Registers.begin(), RE = Registers.end();
869
Bill Wendling7caebff2011-01-12 21:20:59 +0000870 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000871 bool EmittedWarning = false;
872
Bill Wendling7caebff2011-01-12 21:20:59 +0000873 DenseMap<unsigned, bool> RegMap;
874 RegMap[HighRegNum] = true;
875
Bill Wendlinge7176102010-11-06 22:36:58 +0000876 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000877 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +0000878 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +0000879
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000880 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000881 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +0000882 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +0000883 }
884
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000885 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000886 Warning(RegInfo.second,
887 "register not in ascending order in register list");
888
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000889 RegMap[Reg] = true;
890 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000891 }
892
Bill Wendling50d0f582010-11-18 23:43:05 +0000893 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
894 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000895}
896
Jim Grosbachf922c472011-02-12 01:34:40 +0000897/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
898ARMAsmParser::OperandMatchResultTy ARMAsmParser::
899tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000900 SMLoc S = Parser.getTok().getLoc();
901 const AsmToken &Tok = Parser.getTok();
902 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
903 StringRef OptStr = Tok.getString();
904
905 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
906 .Case("sy", ARM_MB::SY)
907 .Case("st", ARM_MB::ST)
908 .Case("ish", ARM_MB::ISH)
909 .Case("ishst", ARM_MB::ISHST)
910 .Case("nsh", ARM_MB::NSH)
911 .Case("nshst", ARM_MB::NSHST)
912 .Case("osh", ARM_MB::OSH)
913 .Case("oshst", ARM_MB::OSHST)
914 .Default(~0U);
915
916 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +0000917 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000918
919 Parser.Lex(); // Eat identifier token.
920 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +0000921 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000922}
923
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000924/// ParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
925ARMAsmParser::OperandMatchResultTy ARMAsmParser::
926tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
927 SMLoc S = Parser.getTok().getLoc();
928 const AsmToken &Tok = Parser.getTok();
929 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
930 StringRef IFlagsStr = Tok.getString();
931
932 unsigned IFlags = 0;
933 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
934 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
935 .Case("a", ARM_PROC::A)
936 .Case("i", ARM_PROC::I)
937 .Case("f", ARM_PROC::F)
938 .Default(~0U);
939
940 // If some specific iflag is already set, it means that some letter is
941 // present more than once, this is not acceptable.
942 if (Flag == ~0U || (IFlags & Flag))
943 return MatchOperand_NoMatch;
944
945 IFlags |= Flag;
946 }
947
948 Parser.Lex(); // Eat identifier token.
949 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
950 return MatchOperand_Success;
951}
952
Bill Wendlinge7176102010-11-06 22:36:58 +0000953/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000954/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000955///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000956/// TODO Only preindexing and postindexing addressing are started, unindexed
957/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +0000958bool ARMAsmParser::
959ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000960 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000961 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000962 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000963 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000964 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000965
Sean Callanan18b83232010-01-19 21:44:56 +0000966 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000967 if (BaseRegTok.isNot(AsmToken::Identifier)) {
968 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000969 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000970 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000971 int BaseRegNum = TryParseRegister();
972 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000973 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000974 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000975 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000976
Daniel Dunbar05710932011-01-18 05:34:17 +0000977 // The next token must either be a comma or a closing bracket.
978 const AsmToken &Tok = Parser.getTok();
979 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
980 return true;
981
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000982 bool Preindexed = false;
983 bool Postindexed = false;
984 bool OffsetIsReg = false;
985 bool Negative = false;
986 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000987 ARMOperand *WBOp = 0;
988 int OffsetRegNum = -1;
989 bool OffsetRegShifted = false;
990 enum ShiftType ShiftType = Lsl;
991 const MCExpr *ShiftAmount = 0;
992 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000993
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000994 // First look for preindexed address forms, that is after the "[Rn" we now
995 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000996 if (Tok.is(AsmToken::Comma)) {
997 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000998 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000999
Chris Lattner550276e2010-10-28 20:52:15 +00001000 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1001 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001002 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001003 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001004 if (RBracTok.isNot(AsmToken::RBrac)) {
1005 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001006 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001007 }
Sean Callanan76264762010-04-02 22:27:05 +00001008 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001009 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001010
Sean Callanan18b83232010-01-19 21:44:56 +00001011 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001012 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001013 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1014 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001015 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001016 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001017 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001018 } else {
1019 // The "[Rn" we have so far was not followed by a comma.
1020
Jim Grosbach80eb2332010-10-29 17:41:25 +00001021 // If there's anything other than the right brace, this is a post indexing
1022 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001023 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001024 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001025
Sean Callanan18b83232010-01-19 21:44:56 +00001026 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001027
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001028 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001029 Postindexed = true;
1030 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001031
Chris Lattner550276e2010-10-28 20:52:15 +00001032 if (NextTok.isNot(AsmToken::Comma)) {
1033 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001034 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001035 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001036
Sean Callananb9a25b72010-01-19 20:27:46 +00001037 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001038
Chris Lattner550276e2010-10-28 20:52:15 +00001039 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001040 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001041 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001042 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001043 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001044 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001045
1046 // Force Offset to exist if used.
1047 if (!OffsetIsReg) {
1048 if (!Offset)
1049 Offset = MCConstantExpr::Create(0, getContext());
1050 }
1051
1052 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
1053 OffsetRegNum, OffsetRegShifted,
1054 ShiftType, ShiftAmount, Preindexed,
1055 Postindexed, Negative, Writeback,
1056 S, E));
1057 if (WBOp)
1058 Operands.push_back(WBOp);
1059
1060 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001061}
1062
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001063/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1064/// we will parse the following (were +/- means that a plus or minus is
1065/// optional):
1066/// +/-Rm
1067/// +/-Rm, shift
1068/// #offset
1069/// we return false on success or an error otherwise.
1070bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001071 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001072 enum ShiftType &ShiftType,
1073 const MCExpr *&ShiftAmount,
1074 const MCExpr *&Offset,
1075 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001076 int &OffsetRegNum,
1077 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001078 Negative = false;
1079 OffsetRegShifted = false;
1080 OffsetIsReg = false;
1081 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001082 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001083 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001084 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001085 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001086 else if (NextTok.is(AsmToken::Minus)) {
1087 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001088 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001089 }
1090 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001091 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001092 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001093 SMLoc CurLoc = OffsetRegTok.getLoc();
1094 OffsetRegNum = TryParseRegister();
1095 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001096 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001097 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001098 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001099 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001100
Bill Wendling12f40e92010-11-06 10:51:53 +00001101 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001102 if (OffsetRegNum != -1) {
1103 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001104 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001105 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001106 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001107
Sean Callanan18b83232010-01-19 21:44:56 +00001108 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001109 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001110 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001111 OffsetRegShifted = true;
1112 }
1113 }
1114 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1115 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001116 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001117 if (HashTok.isNot(AsmToken::Hash))
1118 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001119
Sean Callananb9a25b72010-01-19 20:27:46 +00001120 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001121
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001122 if (getParser().ParseExpression(Offset))
1123 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001124 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001125 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001126 return false;
1127}
1128
1129/// ParseShift as one of these two:
1130/// ( lsl | lsr | asr | ror ) , # shift_amount
1131/// rrx
1132/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +00001133bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +00001134 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001135 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001136 if (Tok.isNot(AsmToken::Identifier))
1137 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001138 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001139 if (ShiftName == "lsl" || ShiftName == "LSL")
1140 St = Lsl;
1141 else if (ShiftName == "lsr" || ShiftName == "LSR")
1142 St = Lsr;
1143 else if (ShiftName == "asr" || ShiftName == "ASR")
1144 St = Asr;
1145 else if (ShiftName == "ror" || ShiftName == "ROR")
1146 St = Ror;
1147 else if (ShiftName == "rrx" || ShiftName == "RRX")
1148 St = Rrx;
1149 else
1150 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001151 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001152
1153 // Rrx stands alone.
1154 if (St == Rrx)
1155 return false;
1156
1157 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001158 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001159 if (HashTok.isNot(AsmToken::Hash))
1160 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001161 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001162
1163 if (getParser().ParseExpression(ShiftAmount))
1164 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001165
1166 return false;
1167}
1168
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001169/// Parse a arm instruction operand. For now this parses the operand regardless
1170/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001171bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001172 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001173 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001174
1175 // Check if the current operand has a custom associated parser, if so, try to
1176 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001177 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1178 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001179 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001180 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1181 // there was a match, but an error occurred, in which case, just return that
1182 // the operand parsing failed.
1183 if (ResTy == MatchOperand_ParseFail)
1184 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001185
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001186 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001187 default:
1188 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001189 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +00001190 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +00001191 if (!TryParseRegisterWithWriteBack(Operands))
1192 return false;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001193
1194 // Fall though for the Identifier case that is not a register or a
1195 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +00001196 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1197 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001198 // This was not a register so parse other operands that start with an
1199 // identifier (like labels) as expressions and create them as immediates.
1200 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001201 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001202 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001203 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001204 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001205 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1206 return false;
1207 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001208 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001209 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001210 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001211 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001212 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001213 // #42 -> immediate.
1214 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001215 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001216 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001217 const MCExpr *ImmVal;
1218 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001219 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001220 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001221 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1222 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001223 case AsmToken::Colon: {
1224 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001225 // FIXME: Check it's an expression prefix,
1226 // e.g. (FOO - :lower16:BAR) isn't legal.
1227 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001228 if (ParsePrefix(RefKind))
1229 return true;
1230
Evan Cheng75972122011-01-13 07:58:56 +00001231 const MCExpr *SubExprVal;
1232 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001233 return true;
1234
Evan Cheng75972122011-01-13 07:58:56 +00001235 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1236 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001237 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001238 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001239 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001240 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001241 }
1242}
1243
Evan Cheng75972122011-01-13 07:58:56 +00001244// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1245// :lower16: and :upper16:.
1246bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1247 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001248
1249 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001250 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001251 Parser.Lex(); // Eat ':'
1252
1253 if (getLexer().isNot(AsmToken::Identifier)) {
1254 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1255 return true;
1256 }
1257
1258 StringRef IDVal = Parser.getTok().getIdentifier();
1259 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001260 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001261 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001262 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001263 } else {
1264 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1265 return true;
1266 }
1267 Parser.Lex();
1268
1269 if (getLexer().isNot(AsmToken::Colon)) {
1270 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1271 return true;
1272 }
1273 Parser.Lex(); // Eat the last ':'
1274 return false;
1275}
1276
1277const MCExpr *
1278ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1279 MCSymbolRefExpr::VariantKind Variant) {
1280 // Recurse over the given expression, rebuilding it to apply the given variant
1281 // to the leftmost symbol.
1282 if (Variant == MCSymbolRefExpr::VK_None)
1283 return E;
1284
1285 switch (E->getKind()) {
1286 case MCExpr::Target:
1287 llvm_unreachable("Can't handle target expr yet");
1288 case MCExpr::Constant:
1289 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1290
1291 case MCExpr::SymbolRef: {
1292 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1293
1294 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1295 return 0;
1296
1297 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1298 }
1299
1300 case MCExpr::Unary:
1301 llvm_unreachable("Can't handle unary expressions yet");
1302
1303 case MCExpr::Binary: {
1304 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1305 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1306 const MCExpr *RHS = BE->getRHS();
1307 if (!LHS)
1308 return 0;
1309
1310 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1311 }
1312 }
1313
1314 assert(0 && "Invalid expression kind!");
1315 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001316}
1317
Daniel Dunbar352e1482011-01-11 15:59:50 +00001318/// \brief Given a mnemonic, split out possible predication code and carry
1319/// setting letters to form a canonical mnemonic and flags.
1320//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001321// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001322static StringRef SplitMnemonic(StringRef Mnemonic,
1323 unsigned &PredicationCode,
1324 bool &CarrySetting,
1325 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001326 PredicationCode = ARMCC::AL;
1327 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001328 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001329
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001330 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001331 //
1332 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001333 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1334 Mnemonic == "movs" ||
1335 Mnemonic == "svc" ||
1336 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1337 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1338 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1339 Mnemonic == "vclt" ||
1340 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1341 Mnemonic == "vcle" ||
1342 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1343 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1344 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001345 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001346
Daniel Dunbar352e1482011-01-11 15:59:50 +00001347 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001348 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001349 .Case("eq", ARMCC::EQ)
1350 .Case("ne", ARMCC::NE)
1351 .Case("hs", ARMCC::HS)
1352 .Case("lo", ARMCC::LO)
1353 .Case("mi", ARMCC::MI)
1354 .Case("pl", ARMCC::PL)
1355 .Case("vs", ARMCC::VS)
1356 .Case("vc", ARMCC::VC)
1357 .Case("hi", ARMCC::HI)
1358 .Case("ls", ARMCC::LS)
1359 .Case("ge", ARMCC::GE)
1360 .Case("lt", ARMCC::LT)
1361 .Case("gt", ARMCC::GT)
1362 .Case("le", ARMCC::LE)
1363 .Case("al", ARMCC::AL)
1364 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001365 if (CC != ~0U) {
1366 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001367 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001368 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001369
Daniel Dunbar352e1482011-01-11 15:59:50 +00001370 // Next, determine if we have a carry setting bit. We explicitly ignore all
1371 // the instructions we know end in 's'.
1372 if (Mnemonic.endswith("s") &&
1373 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1374 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1375 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1376 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1377 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1378 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1379 CarrySetting = true;
1380 }
1381
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001382 // The "cps" instruction can have a interrupt mode operand which is glued into
1383 // the mnemonic. Check if this is the case, split it and parse the imod op
1384 if (Mnemonic.startswith("cps")) {
1385 // Split out any imod code.
1386 unsigned IMod =
1387 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1388 .Case("ie", ARM_PROC::IE)
1389 .Case("id", ARM_PROC::ID)
1390 .Default(~0U);
1391 if (IMod != ~0U) {
1392 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
1393 ProcessorIMod = IMod;
1394 }
1395 }
1396
Daniel Dunbar352e1482011-01-11 15:59:50 +00001397 return Mnemonic;
1398}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001399
1400/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1401/// inclusion of carry set or predication code operands.
1402//
1403// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001404void ARMAsmParser::
1405GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1406 bool &CanAcceptPredicationCode) {
1407 bool isThumb = TM.getSubtarget<ARMSubtarget>().isThumb();
1408
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001409 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1410 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1411 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1412 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1413 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1414 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1415 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1416 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1417 CanAcceptCarrySet = true;
1418 } else {
1419 CanAcceptCarrySet = false;
1420 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001421
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001422 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1423 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1424 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1425 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00001426 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001427 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001428 CanAcceptPredicationCode = false;
1429 } else {
1430 CanAcceptPredicationCode = true;
1431 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001432
1433 if (isThumb)
1434 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Bruno Cardoso Lopes8dd37f72011-01-20 18:32:09 +00001435 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001436 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001437}
1438
1439/// Parse an arm instruction mnemonic followed by its operands.
1440bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1441 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1442 // Create the leading tokens for the mnemonic, split by '.' characters.
1443 size_t Start = 0, Next = Name.find('.');
1444 StringRef Head = Name.slice(Start, Next);
1445
Daniel Dunbar352e1482011-01-11 15:59:50 +00001446 // Split out the predication code and carry setting flag from the mnemonic.
1447 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001448 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001449 bool CarrySetting;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001450 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
1451 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001452
Chris Lattner3a697562010-10-28 17:20:03 +00001453 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001454
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001455 // Next, add the CCOut and ConditionCode operands, if needed.
1456 //
1457 // For mnemonics which can ever incorporate a carry setting bit or predication
1458 // code, our matching model involves us always generating CCOut and
1459 // ConditionCode operands to match the mnemonic "as written" and then we let
1460 // the matcher deal with finding the right instruction or generating an
1461 // appropriate error.
1462 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1463 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1464
1465 // Add the carry setting operand, if necessary.
1466 //
1467 // FIXME: It would be awesome if we could somehow invent a location such that
1468 // match errors on this operand would print a nice diagnostic about how the
1469 // 's' character in the mnemonic resulted in a CCOut operand.
1470 if (CanAcceptCarrySet) {
1471 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1472 NameLoc));
1473 } else {
1474 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1475 // misspelled another mnemonic).
1476
1477 // FIXME: Issue a nice error.
1478 }
1479
1480 // Add the predication code operand, if necessary.
1481 if (CanAcceptPredicationCode) {
1482 Operands.push_back(ARMOperand::CreateCondCode(
1483 ARMCC::CondCodes(PredicationCode), NameLoc));
1484 } else {
1485 // This mnemonic can't ever accept a predication code, but the user wrote
1486 // one (or misspelled another mnemonic).
1487
1488 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001489 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001490
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001491 // Add the processor imod operand, if necessary.
1492 if (ProcessorIMod) {
1493 Operands.push_back(ARMOperand::CreateImm(
1494 MCConstantExpr::Create(ProcessorIMod, getContext()),
1495 NameLoc, NameLoc));
1496 } else {
1497 // This mnemonic can't ever accept a imod, but the user wrote
1498 // one (or misspelled another mnemonic).
1499
1500 // FIXME: Issue a nice error.
1501 }
1502
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001503 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001504 while (Next != StringRef::npos) {
1505 Start = Next;
1506 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001507 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001508
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001509 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001510 }
1511
1512 // Read the remaining operands.
1513 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001514 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001515 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001516 Parser.EatToEndOfStatement();
1517 return true;
1518 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001519
1520 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001521 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001522
1523 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001524 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001525 Parser.EatToEndOfStatement();
1526 return true;
1527 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001528 }
1529 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001530
Chris Lattnercbf8a982010-09-11 16:18:25 +00001531 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1532 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001533 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001534 }
Bill Wendling146018f2010-11-06 21:42:12 +00001535
Chris Lattner34e53142010-09-08 05:10:46 +00001536 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001537 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001538}
1539
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001540bool ARMAsmParser::
1541MatchAndEmitInstruction(SMLoc IDLoc,
1542 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1543 MCStreamer &Out) {
1544 MCInst Inst;
1545 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001546 MatchResultTy MatchResult, MatchResult2;
1547 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1548 if (MatchResult != Match_Success) {
1549 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1550 // that does not update the condition codes. So try adding a CCOut operand
1551 // with a value of reg0.
1552 if (MatchResult == Match_InvalidOperand) {
1553 Operands.insert(Operands.begin() + 1,
1554 ARMOperand::CreateCCOut(0,
1555 ((ARMOperand*)Operands[0])->getStartLoc()));
1556 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1557 if (MatchResult2 == Match_Success)
1558 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001559 else {
1560 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001561 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001562 delete CCOut;
1563 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001564 }
1565 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1566 // that updates the condition codes if it ends in 's'. So see if the
1567 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1568 // operand with a value of CPSR.
1569 else if(MatchResult == Match_MnemonicFail) {
1570 // Get the instruction mnemonic, which is the first token.
1571 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1572 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1573 // removed the 's' from the mnemonic for matching.
1574 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1575 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001576 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1577 Operands.erase(Operands.begin());
1578 delete OldMnemonic;
1579 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001580 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1581 Operands.insert(Operands.begin() + 1,
1582 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1583 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1584 if (MatchResult2 == Match_Success)
1585 MatchResult = Match_Success;
1586 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001587 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1588 Operands.erase(Operands.begin());
1589 delete OldMnemonic;
1590 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001591 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001592 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1593 Operands.erase(Operands.begin() + 1);
1594 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001595 }
1596 }
1597 }
1598 }
1599 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001600 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001601 Out.EmitInstruction(Inst);
1602 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001603 case Match_MissingFeature:
1604 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1605 return true;
1606 case Match_InvalidOperand: {
1607 SMLoc ErrorLoc = IDLoc;
1608 if (ErrorInfo != ~0U) {
1609 if (ErrorInfo >= Operands.size())
1610 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001611
Chris Lattnere73d4f82010-10-28 21:41:58 +00001612 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1613 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1614 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001615
Chris Lattnere73d4f82010-10-28 21:41:58 +00001616 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001617 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001618 case Match_MnemonicFail:
1619 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00001620 case Match_ConversionFail:
1621 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00001622 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001623
Eric Christopherc223e2b2010-10-29 09:26:59 +00001624 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001625 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001626}
1627
Kevin Enderby515d5092009-10-15 20:48:48 +00001628/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001629bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1630 StringRef IDVal = DirectiveID.getIdentifier();
1631 if (IDVal == ".word")
1632 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001633 else if (IDVal == ".thumb")
1634 return ParseDirectiveThumb(DirectiveID.getLoc());
1635 else if (IDVal == ".thumb_func")
1636 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1637 else if (IDVal == ".code")
1638 return ParseDirectiveCode(DirectiveID.getLoc());
1639 else if (IDVal == ".syntax")
1640 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001641 return true;
1642}
1643
1644/// ParseDirectiveWord
1645/// ::= .word [ expression (, expression)* ]
1646bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1647 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1648 for (;;) {
1649 const MCExpr *Value;
1650 if (getParser().ParseExpression(Value))
1651 return true;
1652
Chris Lattneraaec2052010-01-19 19:46:13 +00001653 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001654
1655 if (getLexer().is(AsmToken::EndOfStatement))
1656 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001657
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001658 // FIXME: Improve diagnostic.
1659 if (getLexer().isNot(AsmToken::Comma))
1660 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001661 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001662 }
1663 }
1664
Sean Callananb9a25b72010-01-19 20:27:46 +00001665 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001666 return false;
1667}
1668
Kevin Enderby515d5092009-10-15 20:48:48 +00001669/// ParseDirectiveThumb
1670/// ::= .thumb
1671bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1672 if (getLexer().isNot(AsmToken::EndOfStatement))
1673 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001674 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001675
1676 // TODO: set thumb mode
1677 // TODO: tell the MC streamer the mode
1678 // getParser().getStreamer().Emit???();
1679 return false;
1680}
1681
1682/// ParseDirectiveThumbFunc
1683/// ::= .thumbfunc symbol_name
1684bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001685 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001686 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001687 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001688 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001689 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001690 if (getLexer().isNot(AsmToken::EndOfStatement))
1691 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001692 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001693
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001694 // Mark symbol as a thumb symbol.
1695 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1696 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001697 return false;
1698}
1699
1700/// ParseDirectiveSyntax
1701/// ::= .syntax unified | divided
1702bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001703 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001704 if (Tok.isNot(AsmToken::Identifier))
1705 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001706 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001707 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001708 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001709 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00001710 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00001711 else
1712 return Error(L, "unrecognized syntax mode in .syntax directive");
1713
1714 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001715 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001716 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001717
1718 // TODO tell the MC streamer the mode
1719 // getParser().getStreamer().Emit???();
1720 return false;
1721}
1722
1723/// ParseDirectiveCode
1724/// ::= .code 16 | 32
1725bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001726 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001727 if (Tok.isNot(AsmToken::Integer))
1728 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001729 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001730 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001731 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001732 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001733 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001734 else
1735 return Error(L, "invalid operand to .code directive");
1736
1737 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001738 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001739 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001740
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001741 // FIXME: We need to be able switch subtargets at this point so that
1742 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
1743 // includes Feature_IsThumb or not to match the right instructions. This is
1744 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
1745 if (Val == 16){
1746 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
1747 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001748 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001749 }
1750 else{
1751 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
1752 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001753 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001754 }
Jim Grosbach2a301702010-11-05 22:40:53 +00001755
Kevin Enderby515d5092009-10-15 20:48:48 +00001756 return false;
1757}
1758
Sean Callanan90b70972010-04-07 20:29:34 +00001759extern "C" void LLVMInitializeARMAsmLexer();
1760
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001761/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001762extern "C" void LLVMInitializeARMAsmParser() {
1763 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1764 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001765 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001766}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001767
Chris Lattner0692ee62010-09-06 19:11:01 +00001768#define GET_REGISTER_MATCHER
1769#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001770#include "ARMGenAsmMatcher.inc"