blob: 3cb93a317a75d6297626ce11f9958196496af253 [file] [log] [blame]
Kevin Enderbyccab3172009-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 Wendlinge84eb992010-11-03 01:49:29 +000011#include "ARMAddressingModes.h"
Daniel Dunbar5cd4d0f2010-08-11 05:24:50 +000012#include "ARMSubtarget.h"
Chris Lattner00646cf2010-01-22 01:44:57 +000013#include "llvm/MC/MCParser/MCAsmLexer.h"
14#include "llvm/MC/MCParser/MCAsmParser.h"
15#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Jim Grosbachc6db8ce2010-11-05 22:33:53 +000016#include "llvm/MC/MCContext.h"
Kevin Enderbyccab3172009-09-15 00:27:25 +000017#include "llvm/MC/MCStreamer.h"
18#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCInst.h"
Kevin Enderbyccab3172009-09-15 00:27:25 +000020#include "llvm/Target/TargetRegistry.h"
21#include "llvm/Target/TargetAsmParser.h"
Chris Lattner00646cf2010-01-22 01:44:57 +000022#include "llvm/Support/SourceMgr.h"
Daniel Dunbar4a863e62010-08-11 06:37:12 +000023#include "llvm/Support/raw_ostream.h"
Chris Lattner00646cf2010-01-22 01:44:57 +000024#include "llvm/ADT/SmallVector.h"
Daniel Dunbar188b47b2010-08-11 06:37:20 +000025#include "llvm/ADT/StringSwitch.h"
Chris Lattner00646cf2010-01-22 01:44:57 +000026#include "llvm/ADT/Twine.h"
Kevin Enderbyccab3172009-09-15 00:27:25 +000027using namespace llvm;
28
Kevin Enderbyfebe39b2009-10-06 22:26:42 +000029// The shift types for register controlled shifts in arm memory addressing
30enum ShiftType {
31 Lsl,
32 Lsr,
33 Asr,
34 Ror,
35 Rrx
36};
37
Chris Lattnerbd7c9fa2010-10-28 17:20:03 +000038namespace {
39 struct ARMOperand;
Jim Grosbach624bcc72010-10-29 14:46:02 +000040
Kevin Enderbyccab3172009-09-15 00:27:25 +000041class ARMAsmParser : public TargetAsmParser {
42 MCAsmParser &Parser;
Daniel Dunbar419197c2010-07-19 00:33:49 +000043 TargetMachine &TM;
Kevin Enderbyccab3172009-09-15 00:27:25 +000044
45private:
46 MCAsmParser &getParser() const { return Parser; }
47
48 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
49
50 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
51
52 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
53
Chris Lattner44e5981c2010-10-30 04:09:10 +000054 int TryParseRegister();
55 ARMOperand *TryParseRegisterWithWriteBack();
Chris Lattnerb24ba7be2010-10-28 17:23:41 +000056 ARMOperand *ParseRegisterList();
Chris Lattner9f9f4eb2010-10-28 20:52:15 +000057 ARMOperand *ParseMemory();
Kevin Enderbyfebe39b2009-10-06 22:26:42 +000058
Kevin Enderby8be42bd2009-10-30 22:55:57 +000059 bool ParseMemoryOffsetReg(bool &Negative,
60 bool &OffsetRegShifted,
61 enum ShiftType &ShiftType,
62 const MCExpr *&ShiftAmount,
63 const MCExpr *&Offset,
64 bool &OffsetIsReg,
Sean Callanan7ad0ad02010-04-02 22:27:05 +000065 int &OffsetRegNum,
66 SMLoc &E);
Kevin Enderby8be42bd2009-10-30 22:55:57 +000067
Sean Callanan7ad0ad02010-04-02 22:27:05 +000068 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyfebe39b2009-10-06 22:26:42 +000069
Chris Lattner9f9f4eb2010-10-28 20:52:15 +000070 ARMOperand *ParseOperand();
Kevin Enderbyfebe39b2009-10-06 22:26:42 +000071
Kevin Enderbyccab3172009-09-15 00:27:25 +000072 bool ParseDirectiveWord(unsigned Size, SMLoc L);
73
Kevin Enderby146dcf22009-10-15 20:48:48 +000074 bool ParseDirectiveThumb(SMLoc L);
75
76 bool ParseDirectiveThumbFunc(SMLoc L);
77
78 bool ParseDirectiveCode(SMLoc L);
79
80 bool ParseDirectiveSyntax(SMLoc L);
81
Chris Lattnerb44fd242010-09-29 01:42:58 +000082 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattnera63292a2010-09-29 01:50:45 +000083 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner9487de62010-10-28 21:28:01 +000084 MCStreamer &Out);
Jim Grosbach624bcc72010-10-29 14:46:02 +000085
Kevin Enderbyfebe39b2009-10-06 22:26:42 +000086 /// @name Auto-generated Match Functions
87 /// {
Daniel Dunbar5cd4d0f2010-08-11 05:24:50 +000088
Chris Lattner3e4582a2010-09-06 19:11:01 +000089#define GET_ASSEMBLER_HEADER
90#include "ARMGenAsmMatcher.inc"
Kevin Enderbyfebe39b2009-10-06 22:26:42 +000091
Kevin Enderbyfebe39b2009-10-06 22:26:42 +000092 /// }
93
94
Kevin Enderbyccab3172009-09-15 00:27:25 +000095public:
Daniel Dunbar419197c2010-07-19 00:33:49 +000096 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach0190a642010-11-01 16:59:54 +000097 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
98 // Initialize the set of available features.
99 setAvailableFeatures(ComputeAvailableFeatures(
100 &TM.getSubtarget<ARMSubtarget>()));
101 }
Kevin Enderbyccab3172009-09-15 00:27:25 +0000102
Benjamin Kramer92d89982010-07-14 22:38:02 +0000103 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +0000104 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyccab3172009-09-15 00:27:25 +0000105
106 virtual bool ParseDirective(AsmToken DirectiveID);
107};
Jim Grosbach624bcc72010-10-29 14:46:02 +0000108} // end anonymous namespace
109
Chris Lattnerbd7c9fa2010-10-28 17:20:03 +0000110namespace {
111
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000112/// ARMOperand - Instances of this class represent a parsed ARM machine
113/// instruction.
Chris Lattnerecc8eec2010-01-14 21:21:40 +0000114struct ARMOperand : public MCParsedAsmOperand {
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000115public:
116 enum KindTy {
Daniel Dunbard8042b72010-08-11 06:36:53 +0000117 CondCode,
Kevin Enderbyf5079942009-10-13 22:19:02 +0000118 Immediate,
Daniel Dunbard8042b72010-08-11 06:36:53 +0000119 Memory,
120 Register,
121 Token
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000122 } Kind;
123
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000124 SMLoc StartLoc, EndLoc;
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000125
126 union {
127 struct {
Daniel Dunbard8042b72010-08-11 06:36:53 +0000128 ARMCC::CondCodes Val;
129 } CC;
130
131 struct {
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000132 const char *Data;
133 unsigned Length;
134 } Tok;
135
136 struct {
137 unsigned RegNum;
Kevin Enderby2207e5f2009-10-07 18:01:35 +0000138 bool Writeback;
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000139 } Reg;
140
Kevin Enderbyf5079942009-10-13 22:19:02 +0000141 struct {
142 const MCExpr *Val;
143 } Imm;
Jim Grosbach624bcc72010-10-29 14:46:02 +0000144
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000145 // This is for all forms of ARM address expressions
146 struct {
147 unsigned BaseRegNum;
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000148 unsigned OffsetRegNum; // used when OffsetIsReg is true
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000149 const MCExpr *Offset; // used when OffsetIsReg is false
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000150 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000151 enum ShiftType ShiftType; // used when OffsetRegShifted is true
152 unsigned
153 OffsetRegShifted : 1, // only used when OffsetIsReg is true
154 Preindexed : 1,
155 Postindexed : 1,
156 OffsetIsReg : 1,
157 Negative : 1, // only used when OffsetIsReg is true
158 Writeback : 1;
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000159 } Mem;
160
161 };
Jim Grosbach624bcc72010-10-29 14:46:02 +0000162
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000163 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
164 Kind = o.Kind;
165 StartLoc = o.StartLoc;
166 EndLoc = o.EndLoc;
167 switch (Kind) {
Daniel Dunbard8042b72010-08-11 06:36:53 +0000168 case CondCode:
169 CC = o.CC;
170 break;
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000171 case Token:
Daniel Dunbard8042b72010-08-11 06:36:53 +0000172 Tok = o.Tok;
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000173 break;
174 case Register:
175 Reg = o.Reg;
176 break;
177 case Immediate:
178 Imm = o.Imm;
179 break;
180 case Memory:
181 Mem = o.Mem;
182 break;
183 }
184 }
Jim Grosbach624bcc72010-10-29 14:46:02 +0000185
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000186 /// getStartLoc - Get the location of the first token of this operand.
187 SMLoc getStartLoc() const { return StartLoc; }
188 /// getEndLoc - Get the location of the last token of this operand.
189 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000190
Daniel Dunbard8042b72010-08-11 06:36:53 +0000191 ARMCC::CondCodes getCondCode() const {
192 assert(Kind == CondCode && "Invalid access!");
193 return CC.Val;
194 }
195
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000196 StringRef getToken() const {
197 assert(Kind == Token && "Invalid access!");
198 return StringRef(Tok.Data, Tok.Length);
199 }
200
201 unsigned getReg() const {
202 assert(Kind == Register && "Invalid access!");
203 return Reg.RegNum;
204 }
205
Kevin Enderbyf5079942009-10-13 22:19:02 +0000206 const MCExpr *getImm() const {
207 assert(Kind == Immediate && "Invalid access!");
208 return Imm.Val;
209 }
210
Daniel Dunbard8042b72010-08-11 06:36:53 +0000211 bool isCondCode() const { return Kind == CondCode; }
Daniel Dunbar5cd4d0f2010-08-11 05:24:50 +0000212 bool isImm() const { return Kind == Immediate; }
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000213 bool isReg() const { return Kind == Register; }
Chris Lattner5d6f6a02010-10-29 00:27:31 +0000214 bool isToken() const { return Kind == Token; }
215 bool isMemory() const { return Kind == Memory; }
Daniel Dunbar5cd4d0f2010-08-11 05:24:50 +0000216
217 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner5d6f6a02010-10-29 00:27:31 +0000218 // Add as immediates when possible. Null MCExpr = 0.
219 if (Expr == 0)
220 Inst.addOperand(MCOperand::CreateImm(0));
221 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar5cd4d0f2010-08-11 05:24:50 +0000222 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
223 else
224 Inst.addOperand(MCOperand::CreateExpr(Expr));
225 }
226
Daniel Dunbard8042b72010-08-11 06:36:53 +0000227 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar188b47b2010-08-11 06:37:20 +0000228 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbard8042b72010-08-11 06:36:53 +0000229 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Daniel Dunbar188b47b2010-08-11 06:37:20 +0000230 // FIXME: What belongs here?
231 Inst.addOperand(MCOperand::CreateReg(0));
Daniel Dunbard8042b72010-08-11 06:36:53 +0000232 }
233
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000234 void addRegOperands(MCInst &Inst, unsigned N) const {
235 assert(N == 1 && "Invalid number of operands!");
236 Inst.addOperand(MCOperand::CreateReg(getReg()));
237 }
238
Daniel Dunbar5cd4d0f2010-08-11 05:24:50 +0000239 void addImmOperands(MCInst &Inst, unsigned N) const {
240 assert(N == 1 && "Invalid number of operands!");
241 addExpr(Inst, getImm());
242 }
Jim Grosbach624bcc72010-10-29 14:46:02 +0000243
244
Chris Lattner5d6f6a02010-10-29 00:27:31 +0000245 bool isMemMode5() const {
Chris Lattner5d6f6a02010-10-29 00:27:31 +0000246 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
Jim Grosbach16bd9f12010-10-29 17:41:25 +0000247 Mem.Writeback || Mem.Negative)
Chris Lattner5d6f6a02010-10-29 00:27:31 +0000248 return false;
Jim Grosbach16bd9f12010-10-29 17:41:25 +0000249 // If there is an offset expression, make sure it's valid.
250 if (!Mem.Offset)
251 return true;
252 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
253 if (!CE)
254 return false;
255 // The offset must be a multiple of 4 in the range 0-1020.
256 int64_t Value = CE->getValue();
257 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
Chris Lattner5d6f6a02010-10-29 00:27:31 +0000258 }
Jim Grosbach624bcc72010-10-29 14:46:02 +0000259
Chris Lattner5d6f6a02010-10-29 00:27:31 +0000260 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
261 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach624bcc72010-10-29 14:46:02 +0000262
Chris Lattner5d6f6a02010-10-29 00:27:31 +0000263 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
264 assert(!Mem.OffsetIsReg && "invalid mode 5 operand");
Bill Wendlinge84eb992010-11-03 01:49:29 +0000265
Jim Grosbach16bd9f12010-10-29 17:41:25 +0000266 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
267 // the difference?
268 if (Mem.Offset) {
269 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendlinge84eb992010-11-03 01:49:29 +0000270 assert(CE && "Non-constant mode 5 offset operand!");
271
Jim Grosbach16bd9f12010-10-29 17:41:25 +0000272 // The MCInst offset operand doesn't include the low two bits (like
273 // the instruction encoding).
Bill Wendlinge84eb992010-11-03 01:49:29 +0000274 int64_t Offset = CE->getValue() / 4;
275 if (Offset >= 0)
276 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
277 Offset)));
278 else
279 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
280 -Offset)));
281 } else {
Jim Grosbach16bd9f12010-10-29 17:41:25 +0000282 Inst.addOperand(MCOperand::CreateImm(0));
Bill Wendlinge84eb992010-11-03 01:49:29 +0000283 }
Chris Lattner5d6f6a02010-10-29 00:27:31 +0000284 }
Daniel Dunbar5cd4d0f2010-08-11 05:24:50 +0000285
Daniel Dunbar4a863e62010-08-11 06:37:12 +0000286 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarebace222010-08-11 06:37:04 +0000287
Chris Lattnerbd7c9fa2010-10-28 17:20:03 +0000288 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
289 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar188b47b2010-08-11 06:37:20 +0000290 Op->CC.Val = CC;
291 Op->StartLoc = S;
292 Op->EndLoc = S;
Chris Lattnerbd7c9fa2010-10-28 17:20:03 +0000293 return Op;
Daniel Dunbar188b47b2010-08-11 06:37:20 +0000294 }
295
Chris Lattnerbd7c9fa2010-10-28 17:20:03 +0000296 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
297 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000298 Op->Tok.Data = Str.data();
299 Op->Tok.Length = Str.size();
300 Op->StartLoc = S;
301 Op->EndLoc = S;
Chris Lattnerbd7c9fa2010-10-28 17:20:03 +0000302 return Op;
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000303 }
304
Chris Lattnerbd7c9fa2010-10-28 17:20:03 +0000305 static ARMOperand *CreateReg(unsigned RegNum, bool Writeback, SMLoc S,
306 SMLoc E) {
307 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000308 Op->Reg.RegNum = RegNum;
309 Op->Reg.Writeback = Writeback;
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000310 Op->StartLoc = S;
311 Op->EndLoc = E;
Chris Lattnerbd7c9fa2010-10-28 17:20:03 +0000312 return Op;
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000313 }
314
Chris Lattnerbd7c9fa2010-10-28 17:20:03 +0000315 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
316 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000317 Op->Imm.Val = Val;
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000318 Op->StartLoc = S;
319 Op->EndLoc = E;
Chris Lattnerbd7c9fa2010-10-28 17:20:03 +0000320 return Op;
Kevin Enderbyf5079942009-10-13 22:19:02 +0000321 }
322
Chris Lattnerbd7c9fa2010-10-28 17:20:03 +0000323 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
324 const MCExpr *Offset, unsigned OffsetRegNum,
325 bool OffsetRegShifted, enum ShiftType ShiftType,
326 const MCExpr *ShiftAmount, bool Preindexed,
327 bool Postindexed, bool Negative, bool Writeback,
328 SMLoc S, SMLoc E) {
329 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000330 Op->Mem.BaseRegNum = BaseRegNum;
331 Op->Mem.OffsetIsReg = OffsetIsReg;
332 Op->Mem.Offset = Offset;
333 Op->Mem.OffsetRegNum = OffsetRegNum;
334 Op->Mem.OffsetRegShifted = OffsetRegShifted;
335 Op->Mem.ShiftType = ShiftType;
336 Op->Mem.ShiftAmount = ShiftAmount;
337 Op->Mem.Preindexed = Preindexed;
338 Op->Mem.Postindexed = Postindexed;
339 Op->Mem.Negative = Negative;
340 Op->Mem.Writeback = Writeback;
Jim Grosbach624bcc72010-10-29 14:46:02 +0000341
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000342 Op->StartLoc = S;
343 Op->EndLoc = E;
Chris Lattnerbd7c9fa2010-10-28 17:20:03 +0000344 return Op;
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000345 }
Jim Grosbach624bcc72010-10-29 14:46:02 +0000346
Chris Lattnerbd7c9fa2010-10-28 17:20:03 +0000347private:
348 ARMOperand(KindTy K) : Kind(K) {}
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000349};
350
351} // end anonymous namespace.
352
Daniel Dunbar4a863e62010-08-11 06:37:12 +0000353void ARMOperand::dump(raw_ostream &OS) const {
354 switch (Kind) {
355 case CondCode:
356 OS << ARMCondCodeToString(getCondCode());
357 break;
358 case Immediate:
359 getImm()->print(OS);
360 break;
361 case Memory:
362 OS << "<memory>";
363 break;
364 case Register:
365 OS << "<register " << getReg() << ">";
366 break;
367 case Token:
368 OS << "'" << getToken() << "'";
369 break;
370 }
371}
Daniel Dunbar5cd4d0f2010-08-11 05:24:50 +0000372
373/// @name Auto-generated Match Functions
374/// {
375
376static unsigned MatchRegisterName(StringRef Name);
377
378/// }
379
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000380/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattner44e5981c2010-10-30 04:09:10 +0000381/// and if it is a register name the token is eaten and the register number is
382/// returned. Otherwise return -1.
383///
384int ARMAsmParser::TryParseRegister() {
385 const AsmToken &Tok = Parser.getTok();
386 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbach99710a82010-11-01 16:44:21 +0000387
Chris Lattner44e5981c2010-10-30 04:09:10 +0000388 // FIXME: Validate register for the current architecture; we have to do
389 // validation later, so maybe there is no need for this here.
390 int RegNum = MatchRegisterName(Tok.getString());
391 if (RegNum == -1)
392 return -1;
393 Parser.Lex(); // Eat identifier token.
394 return RegNum;
395}
Jim Grosbach99710a82010-11-01 16:44:21 +0000396
397
Chris Lattner44e5981c2010-10-30 04:09:10 +0000398/// Try to parse a register name. The token must be an Identifier when called,
399/// and if it is a register name the token is eaten and the register number is
400/// returned. Otherwise return -1.
Chris Lattnerbd7c9fa2010-10-28 17:20:03 +0000401///
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000402/// TODO this is likely to change to allow different register types and or to
403/// parse for a specific register type.
Chris Lattner44e5981c2010-10-30 04:09:10 +0000404ARMOperand *ARMAsmParser::TryParseRegisterWithWriteBack() {
405 SMLoc S = Parser.getTok().getLoc();
406 int RegNo = TryParseRegister();
407 if (RegNo == -1) return 0;
Jim Grosbach99710a82010-11-01 16:44:21 +0000408
Chris Lattner44e5981c2010-10-30 04:09:10 +0000409 SMLoc E = Parser.getTok().getLoc();
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000410
Kevin Enderby2207e5f2009-10-07 18:01:35 +0000411 bool Writeback = false;
Chris Lattner44e5981c2010-10-30 04:09:10 +0000412 const AsmToken &ExclaimTok = Parser.getTok();
413 if (ExclaimTok.is(AsmToken::Exclaim)) {
414 E = ExclaimTok.getLoc();
415 Writeback = true;
416 Parser.Lex(); // Eat exclaim token
Kevin Enderby2207e5f2009-10-07 18:01:35 +0000417 }
418
Chris Lattner44e5981c2010-10-30 04:09:10 +0000419 return ARMOperand::CreateReg(RegNo, Writeback, S, E);
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000420}
421
Chris Lattnerb24ba7be2010-10-28 17:23:41 +0000422/// Parse a register list, return it if successful else return null. The first
423/// token must be a '{' when called.
424ARMOperand *ARMAsmParser::ParseRegisterList() {
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000425 SMLoc S, E;
Sean Callanan936b0d32010-01-19 21:44:56 +0000426 assert(Parser.getTok().is(AsmToken::LCurly) &&
Kevin Enderbyf5079942009-10-13 22:19:02 +0000427 "Token is not an Left Curly Brace");
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000428 S = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +0000429 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbya2b99102009-10-09 21:12:28 +0000430
Sean Callanan936b0d32010-01-19 21:44:56 +0000431 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbya2b99102009-10-09 21:12:28 +0000432 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerb24ba7be2010-10-28 17:23:41 +0000433 if (RegTok.isNot(AsmToken::Identifier)) {
434 Error(RegLoc, "register expected");
435 return 0;
436 }
Bill Wendling6d2eb732010-11-06 10:40:24 +0000437 int RegNum = TryParseRegister();
Chris Lattnerb24ba7be2010-10-28 17:23:41 +0000438 if (RegNum == -1) {
439 Error(RegLoc, "register expected");
440 return 0;
441 }
Jim Grosbach624bcc72010-10-29 14:46:02 +0000442
Kevin Enderbya2b99102009-10-09 21:12:28 +0000443 unsigned RegList = 1 << RegNum;
444
445 int HighRegNum = RegNum;
446 // TODO ranges like "{Rn-Rm}"
Sean Callanan936b0d32010-01-19 21:44:56 +0000447 while (Parser.getTok().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +0000448 Parser.Lex(); // Eat comma token.
Kevin Enderbya2b99102009-10-09 21:12:28 +0000449
Sean Callanan936b0d32010-01-19 21:44:56 +0000450 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbya2b99102009-10-09 21:12:28 +0000451 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerb24ba7be2010-10-28 17:23:41 +0000452 if (RegTok.isNot(AsmToken::Identifier)) {
453 Error(RegLoc, "register expected");
454 return 0;
455 }
Bill Wendling6d2eb732010-11-06 10:40:24 +0000456 int RegNum = TryParseRegister();
Chris Lattnerb24ba7be2010-10-28 17:23:41 +0000457 if (RegNum == -1) {
458 Error(RegLoc, "register expected");
459 return 0;
460 }
Kevin Enderbya2b99102009-10-09 21:12:28 +0000461
462 if (RegList & (1 << RegNum))
463 Warning(RegLoc, "register duplicated in register list");
464 else if (RegNum <= HighRegNum)
465 Warning(RegLoc, "register not in ascending order in register list");
466 RegList |= 1 << RegNum;
467 HighRegNum = RegNum;
Kevin Enderbya2b99102009-10-09 21:12:28 +0000468 }
Sean Callanan936b0d32010-01-19 21:44:56 +0000469 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerb24ba7be2010-10-28 17:23:41 +0000470 if (RCurlyTok.isNot(AsmToken::RCurly)) {
471 Error(RCurlyTok.getLoc(), "'}' expected");
472 return 0;
473 }
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000474 E = RCurlyTok.getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +0000475 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbya2b99102009-10-09 21:12:28 +0000476
Chris Lattnerb24ba7be2010-10-28 17:23:41 +0000477 // FIXME: Need to return an operand!
478 Error(E, "FIXME: register list parsing not implemented");
479 return 0;
Kevin Enderbya2b99102009-10-09 21:12:28 +0000480}
481
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000482/// Parse an arm memory expression, return false if successful else return true
483/// or an error. The first token must be a '[' when called.
484/// TODO Only preindexing and postindexing addressing are started, unindexed
485/// with option, etc are still to do.
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000486ARMOperand *ARMAsmParser::ParseMemory() {
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000487 SMLoc S, E;
Sean Callanan936b0d32010-01-19 21:44:56 +0000488 assert(Parser.getTok().is(AsmToken::LBrac) &&
Kevin Enderby11b32382009-10-12 22:51:49 +0000489 "Token is not an Left Bracket");
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000490 S = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +0000491 Parser.Lex(); // Eat left bracket token.
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000492
Sean Callanan936b0d32010-01-19 21:44:56 +0000493 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000494 if (BaseRegTok.isNot(AsmToken::Identifier)) {
495 Error(BaseRegTok.getLoc(), "register expected");
496 return 0;
497 }
Chris Lattner44e5981c2010-10-30 04:09:10 +0000498 int BaseRegNum = TryParseRegister();
499 if (BaseRegNum == -1) {
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000500 Error(BaseRegTok.getLoc(), "register expected");
501 return 0;
502 }
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000503
504 bool Preindexed = false;
505 bool Postindexed = false;
506 bool OffsetIsReg = false;
507 bool Negative = false;
508 bool Writeback = false;
509
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000510 // First look for preindexed address forms, that is after the "[Rn" we now
511 // have to see if the next token is a comma.
Sean Callanan936b0d32010-01-19 21:44:56 +0000512 const AsmToken &Tok = Parser.getTok();
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000513 if (Tok.is(AsmToken::Comma)) {
514 Preindexed = true;
Sean Callanana83fd7d2010-01-19 20:27:46 +0000515 Parser.Lex(); // Eat comma token.
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000516 int OffsetRegNum;
517 bool OffsetRegShifted;
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000518 enum ShiftType ShiftType;
519 const MCExpr *ShiftAmount;
520 const MCExpr *Offset;
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000521 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
522 Offset, OffsetIsReg, OffsetRegNum, E))
523 return 0;
Sean Callanan936b0d32010-01-19 21:44:56 +0000524 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000525 if (RBracTok.isNot(AsmToken::RBrac)) {
526 Error(RBracTok.getLoc(), "']' expected");
527 return 0;
528 }
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000529 E = RBracTok.getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +0000530 Parser.Lex(); // Eat right bracket token.
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000531
Sean Callanan936b0d32010-01-19 21:44:56 +0000532 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000533 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000534 E = ExclaimTok.getLoc();
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000535 Writeback = true;
Sean Callanana83fd7d2010-01-19 20:27:46 +0000536 Parser.Lex(); // Eat exclaim token
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000537 }
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000538 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
539 OffsetRegShifted, ShiftType, ShiftAmount,
540 Preindexed, Postindexed, Negative, Writeback,
541 S, E);
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000542 }
543 // The "[Rn" we have so far was not followed by a comma.
544 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach16bd9f12010-10-29 17:41:25 +0000545 // If there's anything other than the right brace, this is a post indexing
546 // addressing form.
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000547 E = Tok.getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +0000548 Parser.Lex(); // Eat right bracket token.
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000549
Kevin Enderby644de272009-10-15 21:42:45 +0000550 int OffsetRegNum = 0;
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000551 bool OffsetRegShifted = false;
552 enum ShiftType ShiftType;
553 const MCExpr *ShiftAmount;
Chris Lattner5d6f6a02010-10-29 00:27:31 +0000554 const MCExpr *Offset = 0;
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000555
Sean Callanan936b0d32010-01-19 21:44:56 +0000556 const AsmToken &NextTok = Parser.getTok();
Kevin Enderby644de272009-10-15 21:42:45 +0000557 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach16bd9f12010-10-29 17:41:25 +0000558 Postindexed = true;
559 Writeback = true;
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000560 if (NextTok.isNot(AsmToken::Comma)) {
561 Error(NextTok.getLoc(), "',' expected");
562 return 0;
563 }
Sean Callanana83fd7d2010-01-19 20:27:46 +0000564 Parser.Lex(); // Eat comma token.
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000565 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach624bcc72010-10-29 14:46:02 +0000566 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000567 E))
568 return 0;
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000569 }
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000570
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000571 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
572 OffsetRegShifted, ShiftType, ShiftAmount,
573 Preindexed, Postindexed, Negative, Writeback,
574 S, E);
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000575 }
576
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000577 return 0;
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000578}
579
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000580/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
581/// we will parse the following (were +/- means that a plus or minus is
582/// optional):
583/// +/-Rm
584/// +/-Rm, shift
585/// #offset
586/// we return false on success or an error otherwise.
587bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000588 bool &OffsetRegShifted,
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000589 enum ShiftType &ShiftType,
590 const MCExpr *&ShiftAmount,
591 const MCExpr *&Offset,
592 bool &OffsetIsReg,
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000593 int &OffsetRegNum,
594 SMLoc &E) {
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000595 Negative = false;
596 OffsetRegShifted = false;
597 OffsetIsReg = false;
598 OffsetRegNum = -1;
Sean Callanan936b0d32010-01-19 21:44:56 +0000599 const AsmToken &NextTok = Parser.getTok();
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000600 E = NextTok.getLoc();
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000601 if (NextTok.is(AsmToken::Plus))
Sean Callanana83fd7d2010-01-19 20:27:46 +0000602 Parser.Lex(); // Eat plus token.
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000603 else if (NextTok.is(AsmToken::Minus)) {
604 Negative = true;
Sean Callanana83fd7d2010-01-19 20:27:46 +0000605 Parser.Lex(); // Eat minus token
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000606 }
607 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan936b0d32010-01-19 21:44:56 +0000608 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000609 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattner44e5981c2010-10-30 04:09:10 +0000610 SMLoc CurLoc = OffsetRegTok.getLoc();
611 OffsetRegNum = TryParseRegister();
612 if (OffsetRegNum != -1) {
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000613 OffsetIsReg = true;
Chris Lattner44e5981c2010-10-30 04:09:10 +0000614 E = CurLoc;
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000615 }
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000616 }
Jim Grosbach99710a82010-11-01 16:44:21 +0000617
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000618 // If we parsed a register as the offset then their can be a shift after that
619 if (OffsetRegNum != -1) {
620 // Look for a comma then a shift
Sean Callanan936b0d32010-01-19 21:44:56 +0000621 const AsmToken &Tok = Parser.getTok();
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000622 if (Tok.is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +0000623 Parser.Lex(); // Eat comma token.
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000624
Sean Callanan936b0d32010-01-19 21:44:56 +0000625 const AsmToken &Tok = Parser.getTok();
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000626 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000627 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000628 OffsetRegShifted = true;
629 }
630 }
631 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
632 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan936b0d32010-01-19 21:44:56 +0000633 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000634 if (HashTok.isNot(AsmToken::Hash))
635 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach624bcc72010-10-29 14:46:02 +0000636
Sean Callanana83fd7d2010-01-19 20:27:46 +0000637 Parser.Lex(); // Eat hash token.
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000638
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000639 if (getParser().ParseExpression(Offset))
640 return true;
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000641 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000642 }
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000643 return false;
644}
645
646/// ParseShift as one of these two:
647/// ( lsl | lsr | asr | ror ) , # shift_amount
648/// rrx
649/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach624bcc72010-10-29 14:46:02 +0000650bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000651 SMLoc &E) {
Sean Callanan936b0d32010-01-19 21:44:56 +0000652 const AsmToken &Tok = Parser.getTok();
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000653 if (Tok.isNot(AsmToken::Identifier))
654 return true;
Benjamin Kramer92d89982010-07-14 22:38:02 +0000655 StringRef ShiftName = Tok.getString();
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000656 if (ShiftName == "lsl" || ShiftName == "LSL")
657 St = Lsl;
658 else if (ShiftName == "lsr" || ShiftName == "LSR")
659 St = Lsr;
660 else if (ShiftName == "asr" || ShiftName == "ASR")
661 St = Asr;
662 else if (ShiftName == "ror" || ShiftName == "ROR")
663 St = Ror;
664 else if (ShiftName == "rrx" || ShiftName == "RRX")
665 St = Rrx;
666 else
667 return true;
Sean Callanana83fd7d2010-01-19 20:27:46 +0000668 Parser.Lex(); // Eat shift type token.
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000669
670 // Rrx stands alone.
671 if (St == Rrx)
672 return false;
673
674 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan936b0d32010-01-19 21:44:56 +0000675 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000676 if (HashTok.isNot(AsmToken::Hash))
677 return Error(HashTok.getLoc(), "'#' expected");
Sean Callanana83fd7d2010-01-19 20:27:46 +0000678 Parser.Lex(); // Eat hash token.
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000679
680 if (getParser().ParseExpression(ShiftAmount))
681 return true;
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000682
683 return false;
684}
685
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000686/// Parse a arm instruction operand. For now this parses the operand regardless
687/// of the mnemonic.
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000688ARMOperand *ARMAsmParser::ParseOperand() {
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000689 SMLoc S, E;
Jim Grosbach624bcc72010-10-29 14:46:02 +0000690
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000691 switch (getLexer().getKind()) {
692 case AsmToken::Identifier:
Chris Lattner44e5981c2010-10-30 04:09:10 +0000693 if (ARMOperand *Op = TryParseRegisterWithWriteBack())
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000694 return Op;
Jim Grosbach624bcc72010-10-29 14:46:02 +0000695
Kevin Enderby146dcf22009-10-15 20:48:48 +0000696 // This was not a register so parse other operands that start with an
697 // identifier (like labels) as expressions and create them as immediates.
698 const MCExpr *IdVal;
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000699 S = Parser.getTok().getLoc();
Kevin Enderby146dcf22009-10-15 20:48:48 +0000700 if (getParser().ParseExpression(IdVal))
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000701 return 0;
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000702 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000703 return ARMOperand::CreateImm(IdVal, S, E);
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000704 case AsmToken::LBrac:
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000705 return ParseMemory();
Kevin Enderbya2b99102009-10-09 21:12:28 +0000706 case AsmToken::LCurly:
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000707 return ParseRegisterList();
Kevin Enderbya2b99102009-10-09 21:12:28 +0000708 case AsmToken::Hash:
Kevin Enderby3a80dac2009-10-13 23:33:38 +0000709 // #42 -> immediate.
710 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000711 S = Parser.getTok().getLoc();
Sean Callanana83fd7d2010-01-19 20:27:46 +0000712 Parser.Lex();
Kevin Enderby146dcf22009-10-15 20:48:48 +0000713 const MCExpr *ImmVal;
714 if (getParser().ParseExpression(ImmVal))
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000715 return 0;
Sean Callanan7ad0ad02010-04-02 22:27:05 +0000716 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000717 return ARMOperand::CreateImm(ImmVal, S, E);
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000718 default:
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000719 Error(Parser.getTok().getLoc(), "unexpected token in operand");
720 return 0;
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000721 }
722}
723
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000724/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer92d89982010-07-14 22:38:02 +0000725bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattnerf29c0b62010-01-14 22:21:20 +0000726 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar75d26be2010-08-11 06:37:16 +0000727 // Create the leading tokens for the mnemonic, split by '.' characters.
728 size_t Start = 0, Next = Name.find('.');
729 StringRef Head = Name.slice(Start, Next);
730
Daniel Dunbar188b47b2010-08-11 06:37:20 +0000731 // Determine the predicate, if any.
732 //
733 // FIXME: We need a way to check whether a prefix supports predication,
734 // otherwise we will end up with an ambiguity for instructions that happen to
735 // end with a predicate name.
Jim Grosbach31f23b42010-10-29 21:56:51 +0000736 // FIXME: Likewise, some arithmetic instructions have an 's' prefix which
737 // indicates to update the condition codes. Those instructions have an
738 // additional immediate operand which encodes the prefix as reg0 or CPSR.
739 // Just checking for a suffix of 's' definitely creates ambiguities; e.g,
740 // the SMMLS instruction.
Daniel Dunbar188b47b2010-08-11 06:37:20 +0000741 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
742 .Case("eq", ARMCC::EQ)
743 .Case("ne", ARMCC::NE)
744 .Case("hs", ARMCC::HS)
745 .Case("lo", ARMCC::LO)
746 .Case("mi", ARMCC::MI)
747 .Case("pl", ARMCC::PL)
748 .Case("vs", ARMCC::VS)
749 .Case("vc", ARMCC::VC)
750 .Case("hi", ARMCC::HI)
751 .Case("ls", ARMCC::LS)
752 .Case("ge", ARMCC::GE)
753 .Case("lt", ARMCC::LT)
754 .Case("gt", ARMCC::GT)
755 .Case("le", ARMCC::LE)
756 .Case("al", ARMCC::AL)
757 .Default(~0U);
Jim Grosbach624bcc72010-10-29 14:46:02 +0000758
Chris Lattner549a31c2010-10-30 04:35:59 +0000759 if (CC == ~0U ||
760 (CC == ARMCC::LS && (Head == "vmls" || Head == "vnmls"))) {
Daniel Dunbar188b47b2010-08-11 06:37:20 +0000761 CC = ARMCC::AL;
Chris Lattner549a31c2010-10-30 04:35:59 +0000762 } else {
763 Head = Head.slice(0, Head.size() - 2);
Bill Wendling193961b2010-10-29 23:50:21 +0000764 }
Daniel Dunbar188b47b2010-08-11 06:37:20 +0000765
Chris Lattnerbd7c9fa2010-10-28 17:20:03 +0000766 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Jim Grosbach7d45c102010-11-01 18:11:14 +0000767 // FIXME: Should only add this operand for predicated instructions
Chris Lattnerbd7c9fa2010-10-28 17:20:03 +0000768 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar188b47b2010-08-11 06:37:20 +0000769
770 // Add the remaining tokens in the mnemonic.
Daniel Dunbar75d26be2010-08-11 06:37:16 +0000771 while (Next != StringRef::npos) {
772 Start = Next;
773 Next = Name.find('.', Start + 1);
774 Head = Name.slice(Start, Next);
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000775
Chris Lattnerbd7c9fa2010-10-28 17:20:03 +0000776 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar75d26be2010-08-11 06:37:16 +0000777 }
778
779 // Read the remaining operands.
780 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000781 // Read the first operand.
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000782 if (ARMOperand *Op = ParseOperand())
783 Operands.push_back(Op);
784 else {
Chris Lattnera2a9d162010-09-11 16:18:25 +0000785 Parser.EatToEndOfStatement();
786 return true;
787 }
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000788
789 while (getLexer().is(AsmToken::Comma)) {
Sean Callanana83fd7d2010-01-19 20:27:46 +0000790 Parser.Lex(); // Eat the comma.
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000791
792 // Parse and remember the operand.
Chris Lattner9f9f4eb2010-10-28 20:52:15 +0000793 if (ARMOperand *Op = ParseOperand())
794 Operands.push_back(Op);
795 else {
Chris Lattnera2a9d162010-09-11 16:18:25 +0000796 Parser.EatToEndOfStatement();
797 return true;
798 }
Kevin Enderbyfebe39b2009-10-06 22:26:42 +0000799 }
800 }
Jim Grosbach624bcc72010-10-29 14:46:02 +0000801
Chris Lattnera2a9d162010-09-11 16:18:25 +0000802 if (getLexer().isNot(AsmToken::EndOfStatement)) {
803 Parser.EatToEndOfStatement();
Chris Lattner91689c12010-09-08 05:10:46 +0000804 return TokError("unexpected token in argument list");
Chris Lattnera2a9d162010-09-11 16:18:25 +0000805 }
Chris Lattner91689c12010-09-08 05:10:46 +0000806 Parser.Lex(); // Consume the EndOfStatement
Chris Lattnerf29c0b62010-01-14 22:21:20 +0000807 return false;
Kevin Enderbyccab3172009-09-15 00:27:25 +0000808}
809
Chris Lattner9487de62010-10-28 21:28:01 +0000810bool ARMAsmParser::
811MatchAndEmitInstruction(SMLoc IDLoc,
812 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
813 MCStreamer &Out) {
814 MCInst Inst;
815 unsigned ErrorInfo;
Chris Lattnerd27b05e2010-10-28 21:41:58 +0000816 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
817 case Match_Success:
Chris Lattner9487de62010-10-28 21:28:01 +0000818 Out.EmitInstruction(Inst);
819 return false;
Jim Grosbach624bcc72010-10-29 14:46:02 +0000820
Chris Lattnerd27b05e2010-10-28 21:41:58 +0000821 case Match_MissingFeature:
822 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
823 return true;
824 case Match_InvalidOperand: {
825 SMLoc ErrorLoc = IDLoc;
826 if (ErrorInfo != ~0U) {
827 if (ErrorInfo >= Operands.size())
828 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach624bcc72010-10-29 14:46:02 +0000829
Chris Lattnerd27b05e2010-10-28 21:41:58 +0000830 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
831 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
832 }
Jim Grosbach624bcc72010-10-29 14:46:02 +0000833
Chris Lattnerd27b05e2010-10-28 21:41:58 +0000834 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattner9487de62010-10-28 21:28:01 +0000835 }
Chris Lattnerd27b05e2010-10-28 21:41:58 +0000836 case Match_MnemonicFail:
837 return Error(IDLoc, "unrecognized instruction mnemonic");
838 }
Jim Grosbach624bcc72010-10-29 14:46:02 +0000839
Eric Christopher91d7b902010-10-29 09:26:59 +0000840 llvm_unreachable("Implement any new match types added!");
Chris Lattner9487de62010-10-28 21:28:01 +0000841}
842
843
844
Kevin Enderby146dcf22009-10-15 20:48:48 +0000845/// ParseDirective parses the arm specific directives
Kevin Enderbyccab3172009-09-15 00:27:25 +0000846bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
847 StringRef IDVal = DirectiveID.getIdentifier();
848 if (IDVal == ".word")
849 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby146dcf22009-10-15 20:48:48 +0000850 else if (IDVal == ".thumb")
851 return ParseDirectiveThumb(DirectiveID.getLoc());
852 else if (IDVal == ".thumb_func")
853 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
854 else if (IDVal == ".code")
855 return ParseDirectiveCode(DirectiveID.getLoc());
856 else if (IDVal == ".syntax")
857 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyccab3172009-09-15 00:27:25 +0000858 return true;
859}
860
861/// ParseDirectiveWord
862/// ::= .word [ expression (, expression)* ]
863bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
864 if (getLexer().isNot(AsmToken::EndOfStatement)) {
865 for (;;) {
866 const MCExpr *Value;
867 if (getParser().ParseExpression(Value))
868 return true;
869
Chris Lattnerc35681b2010-01-19 19:46:13 +0000870 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyccab3172009-09-15 00:27:25 +0000871
872 if (getLexer().is(AsmToken::EndOfStatement))
873 break;
Jim Grosbach624bcc72010-10-29 14:46:02 +0000874
Kevin Enderbyccab3172009-09-15 00:27:25 +0000875 // FIXME: Improve diagnostic.
876 if (getLexer().isNot(AsmToken::Comma))
877 return Error(L, "unexpected token in directive");
Sean Callanana83fd7d2010-01-19 20:27:46 +0000878 Parser.Lex();
Kevin Enderbyccab3172009-09-15 00:27:25 +0000879 }
880 }
881
Sean Callanana83fd7d2010-01-19 20:27:46 +0000882 Parser.Lex();
Kevin Enderbyccab3172009-09-15 00:27:25 +0000883 return false;
884}
885
Kevin Enderby146dcf22009-10-15 20:48:48 +0000886/// ParseDirectiveThumb
887/// ::= .thumb
888bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
889 if (getLexer().isNot(AsmToken::EndOfStatement))
890 return Error(L, "unexpected token in directive");
Sean Callanana83fd7d2010-01-19 20:27:46 +0000891 Parser.Lex();
Kevin Enderby146dcf22009-10-15 20:48:48 +0000892
893 // TODO: set thumb mode
894 // TODO: tell the MC streamer the mode
895 // getParser().getStreamer().Emit???();
896 return false;
897}
898
899/// ParseDirectiveThumbFunc
900/// ::= .thumbfunc symbol_name
901bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan936b0d32010-01-19 21:44:56 +0000902 const AsmToken &Tok = Parser.getTok();
Kevin Enderby146dcf22009-10-15 20:48:48 +0000903 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach0fe92e32010-11-05 22:11:33 +0000904 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbachc6db8ce2010-11-05 22:33:53 +0000905 StringRef Name = Tok.getString();
Sean Callanana83fd7d2010-01-19 20:27:46 +0000906 Parser.Lex(); // Consume the identifier token.
Kevin Enderby146dcf22009-10-15 20:48:48 +0000907 if (getLexer().isNot(AsmToken::EndOfStatement))
908 return Error(L, "unexpected token in directive");
Sean Callanana83fd7d2010-01-19 20:27:46 +0000909 Parser.Lex();
Kevin Enderby146dcf22009-10-15 20:48:48 +0000910
Jim Grosbachc6db8ce2010-11-05 22:33:53 +0000911 // Mark symbol as a thumb symbol.
912 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
913 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby146dcf22009-10-15 20:48:48 +0000914 return false;
915}
916
917/// ParseDirectiveSyntax
918/// ::= .syntax unified | divided
919bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan936b0d32010-01-19 21:44:56 +0000920 const AsmToken &Tok = Parser.getTok();
Kevin Enderby146dcf22009-10-15 20:48:48 +0000921 if (Tok.isNot(AsmToken::Identifier))
922 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer92d89982010-07-14 22:38:02 +0000923 StringRef Mode = Tok.getString();
Duncan Sands257eba42010-06-29 13:04:35 +0000924 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callanana83fd7d2010-01-19 20:27:46 +0000925 Parser.Lex();
Duncan Sands257eba42010-06-29 13:04:35 +0000926 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callanana83fd7d2010-01-19 20:27:46 +0000927 Parser.Lex();
Kevin Enderby146dcf22009-10-15 20:48:48 +0000928 else
929 return Error(L, "unrecognized syntax mode in .syntax directive");
930
931 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan936b0d32010-01-19 21:44:56 +0000932 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callanana83fd7d2010-01-19 20:27:46 +0000933 Parser.Lex();
Kevin Enderby146dcf22009-10-15 20:48:48 +0000934
935 // TODO tell the MC streamer the mode
936 // getParser().getStreamer().Emit???();
937 return false;
938}
939
940/// ParseDirectiveCode
941/// ::= .code 16 | 32
942bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan936b0d32010-01-19 21:44:56 +0000943 const AsmToken &Tok = Parser.getTok();
Kevin Enderby146dcf22009-10-15 20:48:48 +0000944 if (Tok.isNot(AsmToken::Integer))
945 return Error(L, "unexpected token in .code directive");
Sean Callanan936b0d32010-01-19 21:44:56 +0000946 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands257eba42010-06-29 13:04:35 +0000947 if (Val == 16)
Sean Callanana83fd7d2010-01-19 20:27:46 +0000948 Parser.Lex();
Duncan Sands257eba42010-06-29 13:04:35 +0000949 else if (Val == 32)
Sean Callanana83fd7d2010-01-19 20:27:46 +0000950 Parser.Lex();
Kevin Enderby146dcf22009-10-15 20:48:48 +0000951 else
952 return Error(L, "invalid operand to .code directive");
953
954 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan936b0d32010-01-19 21:44:56 +0000955 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callanana83fd7d2010-01-19 20:27:46 +0000956 Parser.Lex();
Kevin Enderby146dcf22009-10-15 20:48:48 +0000957
Jim Grosbach2db0ea02010-11-05 22:40:53 +0000958 if (Val == 16)
959 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
960 else
961 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
962
Kevin Enderby146dcf22009-10-15 20:48:48 +0000963 return false;
964}
965
Sean Callanan643a5572010-04-07 20:29:34 +0000966extern "C" void LLVMInitializeARMAsmLexer();
967
Kevin Enderby8be42bd2009-10-30 22:55:57 +0000968/// Force static initialization.
Kevin Enderbyccab3172009-09-15 00:27:25 +0000969extern "C" void LLVMInitializeARMAsmParser() {
970 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
971 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan643a5572010-04-07 20:29:34 +0000972 LLVMInitializeARMAsmLexer();
Kevin Enderbyccab3172009-09-15 00:27:25 +0000973}
Daniel Dunbar5cd4d0f2010-08-11 05:24:50 +0000974
Chris Lattner3e4582a2010-09-06 19:11:01 +0000975#define GET_REGISTER_MATCHER
976#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar5cd4d0f2010-08-11 05:24:50 +0000977#include "ARMGenAsmMatcher.inc"