blob: 328ac858402d8df9a2759d1ddfed7ccdbae6d7b7 [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"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000011#include "ARMSubtarget.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000012#include "llvm/MC/MCParser/MCAsmLexer.h"
13#include "llvm/MC/MCParser/MCAsmParser.h"
14#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000015#include "llvm/MC/MCStreamer.h"
16#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCInst.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000018#include "llvm/Target/TargetRegistry.h"
19#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000020#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000022#include "llvm/ADT/SmallVector.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000023#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000025using namespace llvm;
26
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000027// The shift types for register controlled shifts in arm memory addressing
28enum ShiftType {
29 Lsl,
30 Lsr,
31 Asr,
32 Ror,
33 Rrx
34};
35
Chris Lattner3a697562010-10-28 17:20:03 +000036namespace {
37 struct ARMOperand;
38
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000039class ARMAsmParser : public TargetAsmParser {
40 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000041 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000042
43private:
44 MCAsmParser &getParser() const { return Parser; }
45
46 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
47
48 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
49
50 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
51
Chris Lattner3a697562010-10-28 17:20:03 +000052 ARMOperand *MaybeParseRegister(bool ParseWriteBack);
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +000053 ARMOperand *ParseRegisterList();
Chris Lattner550276e2010-10-28 20:52:15 +000054 ARMOperand *ParseMemory();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000055
Kevin Enderby9c41fa82009-10-30 22:55:57 +000056 bool ParseMemoryOffsetReg(bool &Negative,
57 bool &OffsetRegShifted,
58 enum ShiftType &ShiftType,
59 const MCExpr *&ShiftAmount,
60 const MCExpr *&Offset,
61 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000062 int &OffsetRegNum,
63 SMLoc &E);
Kevin Enderby9c41fa82009-10-30 22:55:57 +000064
Sean Callanan76264762010-04-02 22:27:05 +000065 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000066
Chris Lattner550276e2010-10-28 20:52:15 +000067 ARMOperand *ParseOperand();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000068
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000069 bool ParseDirectiveWord(unsigned Size, SMLoc L);
70
Kevin Enderby515d5092009-10-15 20:48:48 +000071 bool ParseDirectiveThumb(SMLoc L);
72
73 bool ParseDirectiveThumbFunc(SMLoc L);
74
75 bool ParseDirectiveCode(SMLoc L);
76
77 bool ParseDirectiveSyntax(SMLoc L);
78
Chris Lattner7036f8b2010-09-29 01:42:58 +000079 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000080 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000081 MCStreamer &Out);
82
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000083 /// @name Auto-generated Match Functions
84 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000085
Chris Lattner0692ee62010-09-06 19:11:01 +000086#define GET_ASSEMBLER_HEADER
87#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000088
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000089 /// }
90
91
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000092public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000093 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
94 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {}
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000095
Benjamin Kramer38e59892010-07-14 22:38:02 +000096 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000097 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000098
99 virtual bool ParseDirective(AsmToken DirectiveID);
100};
Chris Lattner3a697562010-10-28 17:20:03 +0000101} // end anonymous namespace
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000102
Chris Lattner3a697562010-10-28 17:20:03 +0000103namespace {
104
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000105/// ARMOperand - Instances of this class represent a parsed ARM machine
106/// instruction.
Chris Lattner76593892010-01-14 21:21:40 +0000107struct ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000108public:
109 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000110 CondCode,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000111 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000112 Memory,
113 Register,
114 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000115 } Kind;
116
Sean Callanan76264762010-04-02 22:27:05 +0000117 SMLoc StartLoc, EndLoc;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000118
119 union {
120 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000121 ARMCC::CondCodes Val;
122 } CC;
123
124 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000125 const char *Data;
126 unsigned Length;
127 } Tok;
128
129 struct {
130 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000131 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000132 } Reg;
133
Kevin Enderbycfe07242009-10-13 22:19:02 +0000134 struct {
135 const MCExpr *Val;
136 } Imm;
Sean Callanan76264762010-04-02 22:27:05 +0000137
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000138 // This is for all forms of ARM address expressions
139 struct {
140 unsigned BaseRegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000141 unsigned OffsetRegNum; // used when OffsetIsReg is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000142 const MCExpr *Offset; // used when OffsetIsReg is false
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000143 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000144 enum ShiftType ShiftType; // used when OffsetRegShifted is true
145 unsigned
146 OffsetRegShifted : 1, // only used when OffsetIsReg is true
147 Preindexed : 1,
148 Postindexed : 1,
149 OffsetIsReg : 1,
150 Negative : 1, // only used when OffsetIsReg is true
151 Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000152 } Mem;
153
154 };
Sean Callanan76264762010-04-02 22:27:05 +0000155
Sean Callanan76264762010-04-02 22:27:05 +0000156 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
157 Kind = o.Kind;
158 StartLoc = o.StartLoc;
159 EndLoc = o.EndLoc;
160 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000161 case CondCode:
162 CC = o.CC;
163 break;
Sean Callanan76264762010-04-02 22:27:05 +0000164 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000165 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000166 break;
167 case Register:
168 Reg = o.Reg;
169 break;
170 case Immediate:
171 Imm = o.Imm;
172 break;
173 case Memory:
174 Mem = o.Mem;
175 break;
176 }
177 }
178
179 /// getStartLoc - Get the location of the first token of this operand.
180 SMLoc getStartLoc() const { return StartLoc; }
181 /// getEndLoc - Get the location of the last token of this operand.
182 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000183
Daniel Dunbar8462b302010-08-11 06:36:53 +0000184 ARMCC::CondCodes getCondCode() const {
185 assert(Kind == CondCode && "Invalid access!");
186 return CC.Val;
187 }
188
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000189 StringRef getToken() const {
190 assert(Kind == Token && "Invalid access!");
191 return StringRef(Tok.Data, Tok.Length);
192 }
193
194 unsigned getReg() const {
195 assert(Kind == Register && "Invalid access!");
196 return Reg.RegNum;
197 }
198
Kevin Enderbycfe07242009-10-13 22:19:02 +0000199 const MCExpr *getImm() const {
200 assert(Kind == Immediate && "Invalid access!");
201 return Imm.Val;
202 }
203
Daniel Dunbar8462b302010-08-11 06:36:53 +0000204 bool isCondCode() const { return Kind == CondCode; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000205 bool isImm() const { return Kind == Immediate; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000206 bool isReg() const { return Kind == Register; }
Chris Lattner14b93852010-10-29 00:27:31 +0000207 bool isToken() const { return Kind == Token; }
208 bool isMemory() const { return Kind == Memory; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000209
210 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000211 // Add as immediates when possible. Null MCExpr = 0.
212 if (Expr == 0)
213 Inst.addOperand(MCOperand::CreateImm(0));
214 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000215 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
216 else
217 Inst.addOperand(MCOperand::CreateExpr(Expr));
218 }
219
Daniel Dunbar8462b302010-08-11 06:36:53 +0000220 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000221 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000222 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000223 // FIXME: What belongs here?
224 Inst.addOperand(MCOperand::CreateReg(0));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000225 }
226
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000227 void addRegOperands(MCInst &Inst, unsigned N) const {
228 assert(N == 1 && "Invalid number of operands!");
229 Inst.addOperand(MCOperand::CreateReg(getReg()));
230 }
231
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000232 void addImmOperands(MCInst &Inst, unsigned N) const {
233 assert(N == 1 && "Invalid number of operands!");
234 addExpr(Inst, getImm());
235 }
Chris Lattner14b93852010-10-29 00:27:31 +0000236
237
238 bool isMemMode5() const {
239 // FIXME: Is this right? What about postindexed and Writeback?
240 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
241 Mem.Preindexed || Mem.Negative)
242 return false;
243
244 return true;
245 }
246
247 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
248 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
249
250 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
251 assert(!Mem.OffsetIsReg && "invalid mode 5 operand");
252 addExpr(Inst, Mem.Offset);
253 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000254
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000255 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000256
Chris Lattner3a697562010-10-28 17:20:03 +0000257 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
258 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000259 Op->CC.Val = CC;
260 Op->StartLoc = S;
261 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000262 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000263 }
264
Chris Lattner3a697562010-10-28 17:20:03 +0000265 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
266 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000267 Op->Tok.Data = Str.data();
268 Op->Tok.Length = Str.size();
269 Op->StartLoc = S;
270 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000271 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000272 }
273
Chris Lattner3a697562010-10-28 17:20:03 +0000274 static ARMOperand *CreateReg(unsigned RegNum, bool Writeback, SMLoc S,
275 SMLoc E) {
276 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000277 Op->Reg.RegNum = RegNum;
278 Op->Reg.Writeback = Writeback;
Sean Callanan76264762010-04-02 22:27:05 +0000279 Op->StartLoc = S;
280 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000281 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000282 }
283
Chris Lattner3a697562010-10-28 17:20:03 +0000284 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
285 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000286 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000287 Op->StartLoc = S;
288 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000289 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000290 }
291
Chris Lattner3a697562010-10-28 17:20:03 +0000292 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
293 const MCExpr *Offset, unsigned OffsetRegNum,
294 bool OffsetRegShifted, enum ShiftType ShiftType,
295 const MCExpr *ShiftAmount, bool Preindexed,
296 bool Postindexed, bool Negative, bool Writeback,
297 SMLoc S, SMLoc E) {
298 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000299 Op->Mem.BaseRegNum = BaseRegNum;
300 Op->Mem.OffsetIsReg = OffsetIsReg;
301 Op->Mem.Offset = Offset;
302 Op->Mem.OffsetRegNum = OffsetRegNum;
303 Op->Mem.OffsetRegShifted = OffsetRegShifted;
304 Op->Mem.ShiftType = ShiftType;
305 Op->Mem.ShiftAmount = ShiftAmount;
306 Op->Mem.Preindexed = Preindexed;
307 Op->Mem.Postindexed = Postindexed;
308 Op->Mem.Negative = Negative;
309 Op->Mem.Writeback = Writeback;
310
311 Op->StartLoc = S;
312 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000313 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000314 }
Chris Lattner3a697562010-10-28 17:20:03 +0000315
316private:
317 ARMOperand(KindTy K) : Kind(K) {}
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000318};
319
320} // end anonymous namespace.
321
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000322void ARMOperand::dump(raw_ostream &OS) const {
323 switch (Kind) {
324 case CondCode:
325 OS << ARMCondCodeToString(getCondCode());
326 break;
327 case Immediate:
328 getImm()->print(OS);
329 break;
330 case Memory:
331 OS << "<memory>";
332 break;
333 case Register:
334 OS << "<register " << getReg() << ">";
335 break;
336 case Token:
337 OS << "'" << getToken() << "'";
338 break;
339 }
340}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000341
342/// @name Auto-generated Match Functions
343/// {
344
345static unsigned MatchRegisterName(StringRef Name);
346
347/// }
348
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000349/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattner3a697562010-10-28 17:20:03 +0000350/// and if it is a register name the token is eaten and a Reg operand is created
351/// and returned. Otherwise return null.
352///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000353/// TODO this is likely to change to allow different register types and or to
354/// parse for a specific register type.
Chris Lattner3a697562010-10-28 17:20:03 +0000355ARMOperand *ARMAsmParser::MaybeParseRegister(bool ParseWriteBack) {
Sean Callanan76264762010-04-02 22:27:05 +0000356 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000357 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000358 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
359
360 // FIXME: Validate register for the current architecture; we have to do
361 // validation later, so maybe there is no need for this here.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000362 int RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000363
364 RegNum = MatchRegisterName(Tok.getString());
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000365 if (RegNum == -1)
Chris Lattner3a697562010-10-28 17:20:03 +0000366 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000367
368 S = Tok.getLoc();
369
Sean Callananb9a25b72010-01-19 20:27:46 +0000370 Parser.Lex(); // Eat identifier token.
Sean Callanan76264762010-04-02 22:27:05 +0000371
372 E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000373
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000374 bool Writeback = false;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000375 if (ParseWriteBack) {
Sean Callanan18b83232010-01-19 21:44:56 +0000376 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000377 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000378 E = ExclaimTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000379 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000380 Parser.Lex(); // Eat exclaim token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000381 }
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000382 }
383
Chris Lattner3a697562010-10-28 17:20:03 +0000384 return ARMOperand::CreateReg(RegNum, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000385}
386
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000387/// Parse a register list, return it if successful else return null. The first
388/// token must be a '{' when called.
389ARMOperand *ARMAsmParser::ParseRegisterList() {
Sean Callanan76264762010-04-02 22:27:05 +0000390 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000391 assert(Parser.getTok().is(AsmToken::LCurly) &&
Kevin Enderbycfe07242009-10-13 22:19:02 +0000392 "Token is not an Left Curly Brace");
Sean Callanan76264762010-04-02 22:27:05 +0000393 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000394 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000395
Sean Callanan18b83232010-01-19 21:44:56 +0000396 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000397 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000398 if (RegTok.isNot(AsmToken::Identifier)) {
399 Error(RegLoc, "register expected");
400 return 0;
401 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000402 int RegNum = MatchRegisterName(RegTok.getString());
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000403 if (RegNum == -1) {
404 Error(RegLoc, "register expected");
405 return 0;
406 }
407
Sean Callananb9a25b72010-01-19 20:27:46 +0000408 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000409 unsigned RegList = 1 << RegNum;
410
411 int HighRegNum = RegNum;
412 // TODO ranges like "{Rn-Rm}"
Sean Callanan18b83232010-01-19 21:44:56 +0000413 while (Parser.getTok().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000414 Parser.Lex(); // Eat comma token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000415
Sean Callanan18b83232010-01-19 21:44:56 +0000416 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000417 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000418 if (RegTok.isNot(AsmToken::Identifier)) {
419 Error(RegLoc, "register expected");
420 return 0;
421 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000422 int RegNum = MatchRegisterName(RegTok.getString());
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000423 if (RegNum == -1) {
424 Error(RegLoc, "register expected");
425 return 0;
426 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000427
428 if (RegList & (1 << RegNum))
429 Warning(RegLoc, "register duplicated in register list");
430 else if (RegNum <= HighRegNum)
431 Warning(RegLoc, "register not in ascending order in register list");
432 RegList |= 1 << RegNum;
433 HighRegNum = RegNum;
434
Sean Callananb9a25b72010-01-19 20:27:46 +0000435 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000436 }
Sean Callanan18b83232010-01-19 21:44:56 +0000437 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000438 if (RCurlyTok.isNot(AsmToken::RCurly)) {
439 Error(RCurlyTok.getLoc(), "'}' expected");
440 return 0;
441 }
Sean Callanan76264762010-04-02 22:27:05 +0000442 E = RCurlyTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000443 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000444
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000445 // FIXME: Need to return an operand!
446 Error(E, "FIXME: register list parsing not implemented");
447 return 0;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000448}
449
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000450/// Parse an arm memory expression, return false if successful else return true
451/// or an error. The first token must be a '[' when called.
452/// TODO Only preindexing and postindexing addressing are started, unindexed
453/// with option, etc are still to do.
Chris Lattner550276e2010-10-28 20:52:15 +0000454ARMOperand *ARMAsmParser::ParseMemory() {
Sean Callanan76264762010-04-02 22:27:05 +0000455 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000456 assert(Parser.getTok().is(AsmToken::LBrac) &&
Kevin Enderby6bd266e2009-10-12 22:51:49 +0000457 "Token is not an Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000458 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000459 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000460
Sean Callanan18b83232010-01-19 21:44:56 +0000461 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000462 if (BaseRegTok.isNot(AsmToken::Identifier)) {
463 Error(BaseRegTok.getLoc(), "register expected");
464 return 0;
465 }
466 int BaseRegNum = 0;
Benjamin Kramer61a4d562010-10-29 09:43:39 +0000467 if (ARMOperand *Op = MaybeParseRegister(false)) {
Chris Lattner550276e2010-10-28 20:52:15 +0000468 BaseRegNum = Op->getReg();
Benjamin Kramer61a4d562010-10-29 09:43:39 +0000469 delete Op;
470 } else {
Chris Lattner550276e2010-10-28 20:52:15 +0000471 Error(BaseRegTok.getLoc(), "register expected");
472 return 0;
473 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000474
475 bool Preindexed = false;
476 bool Postindexed = false;
477 bool OffsetIsReg = false;
478 bool Negative = false;
479 bool Writeback = false;
480
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000481 // First look for preindexed address forms, that is after the "[Rn" we now
482 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000483 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000484 if (Tok.is(AsmToken::Comma)) {
485 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000486 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000487 int OffsetRegNum;
488 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000489 enum ShiftType ShiftType;
490 const MCExpr *ShiftAmount;
491 const MCExpr *Offset;
Chris Lattner550276e2010-10-28 20:52:15 +0000492 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
493 Offset, OffsetIsReg, OffsetRegNum, E))
494 return 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000495 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000496 if (RBracTok.isNot(AsmToken::RBrac)) {
497 Error(RBracTok.getLoc(), "']' expected");
498 return 0;
499 }
Sean Callanan76264762010-04-02 22:27:05 +0000500 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000501 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000502
Sean Callanan18b83232010-01-19 21:44:56 +0000503 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000504 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000505 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000506 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000507 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000508 }
Chris Lattner550276e2010-10-28 20:52:15 +0000509 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
510 OffsetRegShifted, ShiftType, ShiftAmount,
511 Preindexed, Postindexed, Negative, Writeback,
512 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000513 }
514 // The "[Rn" we have so far was not followed by a comma.
515 else if (Tok.is(AsmToken::RBrac)) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000516 // This is a post indexing addressing forms, that is a ']' follows after
517 // the "[Rn".
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000518 Postindexed = true;
519 Writeback = true;
Sean Callanan76264762010-04-02 22:27:05 +0000520 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000521 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000522
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000523 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000524 bool OffsetRegShifted = false;
525 enum ShiftType ShiftType;
526 const MCExpr *ShiftAmount;
Chris Lattner14b93852010-10-29 00:27:31 +0000527 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000528
Sean Callanan18b83232010-01-19 21:44:56 +0000529 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000530 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Chris Lattner550276e2010-10-28 20:52:15 +0000531 if (NextTok.isNot(AsmToken::Comma)) {
532 Error(NextTok.getLoc(), "',' expected");
533 return 0;
534 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000535 Parser.Lex(); // Eat comma token.
Chris Lattner550276e2010-10-28 20:52:15 +0000536 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
537 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
538 E))
539 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000540 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000541
Chris Lattner550276e2010-10-28 20:52:15 +0000542 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
543 OffsetRegShifted, ShiftType, ShiftAmount,
544 Preindexed, Postindexed, Negative, Writeback,
545 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000546 }
547
Chris Lattner550276e2010-10-28 20:52:15 +0000548 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000549}
550
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000551/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
552/// we will parse the following (were +/- means that a plus or minus is
553/// optional):
554/// +/-Rm
555/// +/-Rm, shift
556/// #offset
557/// we return false on success or an error otherwise.
558bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000559 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000560 enum ShiftType &ShiftType,
561 const MCExpr *&ShiftAmount,
562 const MCExpr *&Offset,
563 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000564 int &OffsetRegNum,
565 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000566 Negative = false;
567 OffsetRegShifted = false;
568 OffsetIsReg = false;
569 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000570 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000571 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000572 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000573 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000574 else if (NextTok.is(AsmToken::Minus)) {
575 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000576 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000577 }
578 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000579 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000580 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattner550276e2010-10-28 20:52:15 +0000581 if (ARMOperand *Op = MaybeParseRegister(false)) {
582 OffsetIsReg = true;
Sean Callanan76264762010-04-02 22:27:05 +0000583 E = Op->getEndLoc();
584 OffsetRegNum = Op->getReg();
Chris Lattner550276e2010-10-28 20:52:15 +0000585 delete Op;
Sean Callanan76264762010-04-02 22:27:05 +0000586 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000587 }
588 // If we parsed a register as the offset then their can be a shift after that
589 if (OffsetRegNum != -1) {
590 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000591 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000592 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000593 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000594
Sean Callanan18b83232010-01-19 21:44:56 +0000595 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000596 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000597 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000598 OffsetRegShifted = true;
599 }
600 }
601 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
602 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000603 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000604 if (HashTok.isNot(AsmToken::Hash))
605 return Error(HashTok.getLoc(), "'#' expected");
Sean Callanan76264762010-04-02 22:27:05 +0000606
Sean Callananb9a25b72010-01-19 20:27:46 +0000607 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000608
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000609 if (getParser().ParseExpression(Offset))
610 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000611 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000612 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000613 return false;
614}
615
616/// ParseShift as one of these two:
617/// ( lsl | lsr | asr | ror ) , # shift_amount
618/// rrx
619/// and returns true if it parses a shift otherwise it returns false.
Chris Lattner3a697562010-10-28 17:20:03 +0000620bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000621 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000622 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000623 if (Tok.isNot(AsmToken::Identifier))
624 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000625 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000626 if (ShiftName == "lsl" || ShiftName == "LSL")
627 St = Lsl;
628 else if (ShiftName == "lsr" || ShiftName == "LSR")
629 St = Lsr;
630 else if (ShiftName == "asr" || ShiftName == "ASR")
631 St = Asr;
632 else if (ShiftName == "ror" || ShiftName == "ROR")
633 St = Ror;
634 else if (ShiftName == "rrx" || ShiftName == "RRX")
635 St = Rrx;
636 else
637 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000638 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000639
640 // Rrx stands alone.
641 if (St == Rrx)
642 return false;
643
644 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000645 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000646 if (HashTok.isNot(AsmToken::Hash))
647 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000648 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000649
650 if (getParser().ParseExpression(ShiftAmount))
651 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000652
653 return false;
654}
655
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000656/// Parse a arm instruction operand. For now this parses the operand regardless
657/// of the mnemonic.
Chris Lattner550276e2010-10-28 20:52:15 +0000658ARMOperand *ARMAsmParser::ParseOperand() {
Sean Callanan76264762010-04-02 22:27:05 +0000659 SMLoc S, E;
660
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000661 switch (getLexer().getKind()) {
662 case AsmToken::Identifier:
Chris Lattner550276e2010-10-28 20:52:15 +0000663 if (ARMOperand *Op = MaybeParseRegister(true))
664 return Op;
665
Kevin Enderby515d5092009-10-15 20:48:48 +0000666 // This was not a register so parse other operands that start with an
667 // identifier (like labels) as expressions and create them as immediates.
668 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000669 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000670 if (getParser().ParseExpression(IdVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000671 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000672 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000673 return ARMOperand::CreateImm(IdVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000674 case AsmToken::LBrac:
Chris Lattner550276e2010-10-28 20:52:15 +0000675 return ParseMemory();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000676 case AsmToken::LCurly:
Chris Lattner550276e2010-10-28 20:52:15 +0000677 return ParseRegisterList();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000678 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000679 // #42 -> immediate.
680 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000681 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000682 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000683 const MCExpr *ImmVal;
684 if (getParser().ParseExpression(ImmVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000685 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000686 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000687 return ARMOperand::CreateImm(ImmVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000688 default:
Chris Lattner550276e2010-10-28 20:52:15 +0000689 Error(Parser.getTok().getLoc(), "unexpected token in operand");
690 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000691 }
692}
693
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000694/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000695bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000696 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000697 // Create the leading tokens for the mnemonic, split by '.' characters.
698 size_t Start = 0, Next = Name.find('.');
699 StringRef Head = Name.slice(Start, Next);
700
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000701 // Determine the predicate, if any.
702 //
703 // FIXME: We need a way to check whether a prefix supports predication,
704 // otherwise we will end up with an ambiguity for instructions that happen to
705 // end with a predicate name.
706 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
707 .Case("eq", ARMCC::EQ)
708 .Case("ne", ARMCC::NE)
709 .Case("hs", ARMCC::HS)
710 .Case("lo", ARMCC::LO)
711 .Case("mi", ARMCC::MI)
712 .Case("pl", ARMCC::PL)
713 .Case("vs", ARMCC::VS)
714 .Case("vc", ARMCC::VC)
715 .Case("hi", ARMCC::HI)
716 .Case("ls", ARMCC::LS)
717 .Case("ge", ARMCC::GE)
718 .Case("lt", ARMCC::LT)
719 .Case("gt", ARMCC::GT)
720 .Case("le", ARMCC::LE)
721 .Case("al", ARMCC::AL)
722 .Default(~0U);
Chris Lattner3a697562010-10-28 17:20:03 +0000723
724 if (CC != ~0U)
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000725 Head = Head.slice(0, Head.size() - 2);
Chris Lattner3a697562010-10-28 17:20:03 +0000726 else
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000727 CC = ARMCC::AL;
728
Chris Lattner3a697562010-10-28 17:20:03 +0000729 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
730 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000731
732 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000733 while (Next != StringRef::npos) {
734 Start = Next;
735 Next = Name.find('.', Start + 1);
736 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000737
Chris Lattner3a697562010-10-28 17:20:03 +0000738 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000739 }
740
741 // Read the remaining operands.
742 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000743 // Read the first operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000744 if (ARMOperand *Op = ParseOperand())
745 Operands.push_back(Op);
746 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000747 Parser.EatToEndOfStatement();
748 return true;
749 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000750
751 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000752 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000753
754 // Parse and remember the operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000755 if (ARMOperand *Op = ParseOperand())
756 Operands.push_back(Op);
757 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000758 Parser.EatToEndOfStatement();
759 return true;
760 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000761 }
762 }
Chris Lattner34e53142010-09-08 05:10:46 +0000763
Chris Lattnercbf8a982010-09-11 16:18:25 +0000764 if (getLexer().isNot(AsmToken::EndOfStatement)) {
765 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000766 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000767 }
Chris Lattner34e53142010-09-08 05:10:46 +0000768 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000769 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000770}
771
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000772bool ARMAsmParser::
773MatchAndEmitInstruction(SMLoc IDLoc,
774 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
775 MCStreamer &Out) {
776 MCInst Inst;
777 unsigned ErrorInfo;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000778 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
779 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000780 Out.EmitInstruction(Inst);
781 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000782
783 case Match_MissingFeature:
784 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
785 return true;
786 case Match_InvalidOperand: {
787 SMLoc ErrorLoc = IDLoc;
788 if (ErrorInfo != ~0U) {
789 if (ErrorInfo >= Operands.size())
790 return Error(IDLoc, "too few operands for instruction");
791
792 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
793 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
794 }
795
796 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000797 }
Chris Lattnere73d4f82010-10-28 21:41:58 +0000798 case Match_MnemonicFail:
799 return Error(IDLoc, "unrecognized instruction mnemonic");
800 }
Eric Christopherc223e2b2010-10-29 09:26:59 +0000801
802 llvm_unreachable("Implement any new match types added!");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000803}
804
805
806
Kevin Enderby515d5092009-10-15 20:48:48 +0000807/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000808bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
809 StringRef IDVal = DirectiveID.getIdentifier();
810 if (IDVal == ".word")
811 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000812 else if (IDVal == ".thumb")
813 return ParseDirectiveThumb(DirectiveID.getLoc());
814 else if (IDVal == ".thumb_func")
815 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
816 else if (IDVal == ".code")
817 return ParseDirectiveCode(DirectiveID.getLoc());
818 else if (IDVal == ".syntax")
819 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000820 return true;
821}
822
823/// ParseDirectiveWord
824/// ::= .word [ expression (, expression)* ]
825bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
826 if (getLexer().isNot(AsmToken::EndOfStatement)) {
827 for (;;) {
828 const MCExpr *Value;
829 if (getParser().ParseExpression(Value))
830 return true;
831
Chris Lattneraaec2052010-01-19 19:46:13 +0000832 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000833
834 if (getLexer().is(AsmToken::EndOfStatement))
835 break;
836
837 // FIXME: Improve diagnostic.
838 if (getLexer().isNot(AsmToken::Comma))
839 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000840 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000841 }
842 }
843
Sean Callananb9a25b72010-01-19 20:27:46 +0000844 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000845 return false;
846}
847
Kevin Enderby515d5092009-10-15 20:48:48 +0000848/// ParseDirectiveThumb
849/// ::= .thumb
850bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
851 if (getLexer().isNot(AsmToken::EndOfStatement))
852 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000853 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000854
855 // TODO: set thumb mode
856 // TODO: tell the MC streamer the mode
857 // getParser().getStreamer().Emit???();
858 return false;
859}
860
861/// ParseDirectiveThumbFunc
862/// ::= .thumbfunc symbol_name
863bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000864 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000865 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
866 return Error(L, "unexpected token in .syntax directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000867 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000868
869 if (getLexer().isNot(AsmToken::EndOfStatement))
870 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000871 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000872
873 // TODO: mark symbol as a thumb symbol
874 // getParser().getStreamer().Emit???();
875 return false;
876}
877
878/// ParseDirectiveSyntax
879/// ::= .syntax unified | divided
880bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000881 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000882 if (Tok.isNot(AsmToken::Identifier))
883 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000884 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000885 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000886 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000887 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000888 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000889 else
890 return Error(L, "unrecognized syntax mode in .syntax directive");
891
892 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000893 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000894 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000895
896 // TODO tell the MC streamer the mode
897 // getParser().getStreamer().Emit???();
898 return false;
899}
900
901/// ParseDirectiveCode
902/// ::= .code 16 | 32
903bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000904 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000905 if (Tok.isNot(AsmToken::Integer))
906 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000907 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +0000908 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +0000909 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000910 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +0000911 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000912 else
913 return Error(L, "invalid operand to .code directive");
914
915 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000916 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000917 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000918
919 // TODO tell the MC streamer the mode
920 // getParser().getStreamer().Emit???();
921 return false;
922}
923
Sean Callanan90b70972010-04-07 20:29:34 +0000924extern "C" void LLVMInitializeARMAsmLexer();
925
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000926/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000927extern "C" void LLVMInitializeARMAsmParser() {
928 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
929 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +0000930 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000931}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000932
Chris Lattner0692ee62010-09-06 19:11:01 +0000933#define GET_REGISTER_MATCHER
934#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000935#include "ARMGenAsmMatcher.inc"