blob: 648b54a0338568374572ca6b6b8d531f800a2e50 [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;
467 if (ARMOperand *Op = MaybeParseRegister(false))
468 BaseRegNum = Op->getReg();
469 else {
470 Error(BaseRegTok.getLoc(), "register expected");
471 return 0;
472 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000473
474 bool Preindexed = false;
475 bool Postindexed = false;
476 bool OffsetIsReg = false;
477 bool Negative = false;
478 bool Writeback = false;
479
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000480 // First look for preindexed address forms, that is after the "[Rn" we now
481 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000482 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000483 if (Tok.is(AsmToken::Comma)) {
484 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000485 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000486 int OffsetRegNum;
487 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000488 enum ShiftType ShiftType;
489 const MCExpr *ShiftAmount;
490 const MCExpr *Offset;
Chris Lattner550276e2010-10-28 20:52:15 +0000491 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
492 Offset, OffsetIsReg, OffsetRegNum, E))
493 return 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000494 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000495 if (RBracTok.isNot(AsmToken::RBrac)) {
496 Error(RBracTok.getLoc(), "']' expected");
497 return 0;
498 }
Sean Callanan76264762010-04-02 22:27:05 +0000499 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000500 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000501
Sean Callanan18b83232010-01-19 21:44:56 +0000502 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000503 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000504 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000505 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000506 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000507 }
Chris Lattner550276e2010-10-28 20:52:15 +0000508 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
509 OffsetRegShifted, ShiftType, ShiftAmount,
510 Preindexed, Postindexed, Negative, Writeback,
511 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000512 }
513 // The "[Rn" we have so far was not followed by a comma.
514 else if (Tok.is(AsmToken::RBrac)) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000515 // This is a post indexing addressing forms, that is a ']' follows after
516 // the "[Rn".
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000517 Postindexed = true;
518 Writeback = true;
Sean Callanan76264762010-04-02 22:27:05 +0000519 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000520 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000521
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000522 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000523 bool OffsetRegShifted = false;
524 enum ShiftType ShiftType;
525 const MCExpr *ShiftAmount;
Chris Lattner14b93852010-10-29 00:27:31 +0000526 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000527
Sean Callanan18b83232010-01-19 21:44:56 +0000528 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000529 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Chris Lattner550276e2010-10-28 20:52:15 +0000530 if (NextTok.isNot(AsmToken::Comma)) {
531 Error(NextTok.getLoc(), "',' expected");
532 return 0;
533 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000534 Parser.Lex(); // Eat comma token.
Chris Lattner550276e2010-10-28 20:52:15 +0000535 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
536 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
537 E))
538 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000539 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000540
Chris Lattner550276e2010-10-28 20:52:15 +0000541 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
542 OffsetRegShifted, ShiftType, ShiftAmount,
543 Preindexed, Postindexed, Negative, Writeback,
544 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000545 }
546
Chris Lattner550276e2010-10-28 20:52:15 +0000547 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000548}
549
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000550/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
551/// we will parse the following (were +/- means that a plus or minus is
552/// optional):
553/// +/-Rm
554/// +/-Rm, shift
555/// #offset
556/// we return false on success or an error otherwise.
557bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000558 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000559 enum ShiftType &ShiftType,
560 const MCExpr *&ShiftAmount,
561 const MCExpr *&Offset,
562 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000563 int &OffsetRegNum,
564 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000565 Negative = false;
566 OffsetRegShifted = false;
567 OffsetIsReg = false;
568 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000569 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000570 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000571 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000572 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000573 else if (NextTok.is(AsmToken::Minus)) {
574 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000575 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000576 }
577 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000578 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000579 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattner550276e2010-10-28 20:52:15 +0000580 if (ARMOperand *Op = MaybeParseRegister(false)) {
581 OffsetIsReg = true;
Sean Callanan76264762010-04-02 22:27:05 +0000582 E = Op->getEndLoc();
583 OffsetRegNum = Op->getReg();
Chris Lattner550276e2010-10-28 20:52:15 +0000584 delete Op;
Sean Callanan76264762010-04-02 22:27:05 +0000585 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000586 }
587 // If we parsed a register as the offset then their can be a shift after that
588 if (OffsetRegNum != -1) {
589 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000590 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000591 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000592 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000593
Sean Callanan18b83232010-01-19 21:44:56 +0000594 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000595 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000596 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000597 OffsetRegShifted = true;
598 }
599 }
600 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
601 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000602 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000603 if (HashTok.isNot(AsmToken::Hash))
604 return Error(HashTok.getLoc(), "'#' expected");
Sean Callanan76264762010-04-02 22:27:05 +0000605
Sean Callananb9a25b72010-01-19 20:27:46 +0000606 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000607
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000608 if (getParser().ParseExpression(Offset))
609 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000610 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000611 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000612 return false;
613}
614
615/// ParseShift as one of these two:
616/// ( lsl | lsr | asr | ror ) , # shift_amount
617/// rrx
618/// and returns true if it parses a shift otherwise it returns false.
Chris Lattner3a697562010-10-28 17:20:03 +0000619bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000620 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000621 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000622 if (Tok.isNot(AsmToken::Identifier))
623 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000624 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000625 if (ShiftName == "lsl" || ShiftName == "LSL")
626 St = Lsl;
627 else if (ShiftName == "lsr" || ShiftName == "LSR")
628 St = Lsr;
629 else if (ShiftName == "asr" || ShiftName == "ASR")
630 St = Asr;
631 else if (ShiftName == "ror" || ShiftName == "ROR")
632 St = Ror;
633 else if (ShiftName == "rrx" || ShiftName == "RRX")
634 St = Rrx;
635 else
636 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000637 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000638
639 // Rrx stands alone.
640 if (St == Rrx)
641 return false;
642
643 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000644 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000645 if (HashTok.isNot(AsmToken::Hash))
646 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000647 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000648
649 if (getParser().ParseExpression(ShiftAmount))
650 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000651
652 return false;
653}
654
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000655/// Parse a arm instruction operand. For now this parses the operand regardless
656/// of the mnemonic.
Chris Lattner550276e2010-10-28 20:52:15 +0000657ARMOperand *ARMAsmParser::ParseOperand() {
Sean Callanan76264762010-04-02 22:27:05 +0000658 SMLoc S, E;
659
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000660 switch (getLexer().getKind()) {
661 case AsmToken::Identifier:
Chris Lattner550276e2010-10-28 20:52:15 +0000662 if (ARMOperand *Op = MaybeParseRegister(true))
663 return Op;
664
Kevin Enderby515d5092009-10-15 20:48:48 +0000665 // This was not a register so parse other operands that start with an
666 // identifier (like labels) as expressions and create them as immediates.
667 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000668 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000669 if (getParser().ParseExpression(IdVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000670 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000671 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000672 return ARMOperand::CreateImm(IdVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000673 case AsmToken::LBrac:
Chris Lattner550276e2010-10-28 20:52:15 +0000674 return ParseMemory();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000675 case AsmToken::LCurly:
Chris Lattner550276e2010-10-28 20:52:15 +0000676 return ParseRegisterList();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000677 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000678 // #42 -> immediate.
679 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000680 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000681 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000682 const MCExpr *ImmVal;
683 if (getParser().ParseExpression(ImmVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000684 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000685 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000686 return ARMOperand::CreateImm(ImmVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000687 default:
Chris Lattner550276e2010-10-28 20:52:15 +0000688 Error(Parser.getTok().getLoc(), "unexpected token in operand");
689 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000690 }
691}
692
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000693/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000694bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000695 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000696 // Create the leading tokens for the mnemonic, split by '.' characters.
697 size_t Start = 0, Next = Name.find('.');
698 StringRef Head = Name.slice(Start, Next);
699
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000700 // Determine the predicate, if any.
701 //
702 // FIXME: We need a way to check whether a prefix supports predication,
703 // otherwise we will end up with an ambiguity for instructions that happen to
704 // end with a predicate name.
705 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
706 .Case("eq", ARMCC::EQ)
707 .Case("ne", ARMCC::NE)
708 .Case("hs", ARMCC::HS)
709 .Case("lo", ARMCC::LO)
710 .Case("mi", ARMCC::MI)
711 .Case("pl", ARMCC::PL)
712 .Case("vs", ARMCC::VS)
713 .Case("vc", ARMCC::VC)
714 .Case("hi", ARMCC::HI)
715 .Case("ls", ARMCC::LS)
716 .Case("ge", ARMCC::GE)
717 .Case("lt", ARMCC::LT)
718 .Case("gt", ARMCC::GT)
719 .Case("le", ARMCC::LE)
720 .Case("al", ARMCC::AL)
721 .Default(~0U);
Chris Lattner3a697562010-10-28 17:20:03 +0000722
723 if (CC != ~0U)
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000724 Head = Head.slice(0, Head.size() - 2);
Chris Lattner3a697562010-10-28 17:20:03 +0000725 else
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000726 CC = ARMCC::AL;
727
Chris Lattner3a697562010-10-28 17:20:03 +0000728 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
729 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000730
731 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000732 while (Next != StringRef::npos) {
733 Start = Next;
734 Next = Name.find('.', Start + 1);
735 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000736
Chris Lattner3a697562010-10-28 17:20:03 +0000737 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000738 }
739
740 // Read the remaining operands.
741 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000742 // Read the first operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000743 if (ARMOperand *Op = ParseOperand())
744 Operands.push_back(Op);
745 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000746 Parser.EatToEndOfStatement();
747 return true;
748 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000749
750 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000751 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000752
753 // Parse and remember the operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000754 if (ARMOperand *Op = ParseOperand())
755 Operands.push_back(Op);
756 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000757 Parser.EatToEndOfStatement();
758 return true;
759 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000760 }
761 }
Chris Lattner34e53142010-09-08 05:10:46 +0000762
Chris Lattnercbf8a982010-09-11 16:18:25 +0000763 if (getLexer().isNot(AsmToken::EndOfStatement)) {
764 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000765 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000766 }
Chris Lattner34e53142010-09-08 05:10:46 +0000767 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000768 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000769}
770
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000771bool ARMAsmParser::
772MatchAndEmitInstruction(SMLoc IDLoc,
773 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
774 MCStreamer &Out) {
775 MCInst Inst;
776 unsigned ErrorInfo;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000777 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
778 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000779 Out.EmitInstruction(Inst);
780 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000781
782 case Match_MissingFeature:
783 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
784 return true;
785 case Match_InvalidOperand: {
786 SMLoc ErrorLoc = IDLoc;
787 if (ErrorInfo != ~0U) {
788 if (ErrorInfo >= Operands.size())
789 return Error(IDLoc, "too few operands for instruction");
790
791 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
792 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
793 }
794
795 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000796 }
Chris Lattnere73d4f82010-10-28 21:41:58 +0000797 case Match_MnemonicFail:
798 return Error(IDLoc, "unrecognized instruction mnemonic");
799 }
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000800}
801
802
803
Kevin Enderby515d5092009-10-15 20:48:48 +0000804/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000805bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
806 StringRef IDVal = DirectiveID.getIdentifier();
807 if (IDVal == ".word")
808 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000809 else if (IDVal == ".thumb")
810 return ParseDirectiveThumb(DirectiveID.getLoc());
811 else if (IDVal == ".thumb_func")
812 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
813 else if (IDVal == ".code")
814 return ParseDirectiveCode(DirectiveID.getLoc());
815 else if (IDVal == ".syntax")
816 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000817 return true;
818}
819
820/// ParseDirectiveWord
821/// ::= .word [ expression (, expression)* ]
822bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
823 if (getLexer().isNot(AsmToken::EndOfStatement)) {
824 for (;;) {
825 const MCExpr *Value;
826 if (getParser().ParseExpression(Value))
827 return true;
828
Chris Lattneraaec2052010-01-19 19:46:13 +0000829 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000830
831 if (getLexer().is(AsmToken::EndOfStatement))
832 break;
833
834 // FIXME: Improve diagnostic.
835 if (getLexer().isNot(AsmToken::Comma))
836 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000837 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000838 }
839 }
840
Sean Callananb9a25b72010-01-19 20:27:46 +0000841 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000842 return false;
843}
844
Kevin Enderby515d5092009-10-15 20:48:48 +0000845/// ParseDirectiveThumb
846/// ::= .thumb
847bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
848 if (getLexer().isNot(AsmToken::EndOfStatement))
849 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000850 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000851
852 // TODO: set thumb mode
853 // TODO: tell the MC streamer the mode
854 // getParser().getStreamer().Emit???();
855 return false;
856}
857
858/// ParseDirectiveThumbFunc
859/// ::= .thumbfunc symbol_name
860bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000861 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000862 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
863 return Error(L, "unexpected token in .syntax directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000864 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000865
866 if (getLexer().isNot(AsmToken::EndOfStatement))
867 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000868 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000869
870 // TODO: mark symbol as a thumb symbol
871 // getParser().getStreamer().Emit???();
872 return false;
873}
874
875/// ParseDirectiveSyntax
876/// ::= .syntax unified | divided
877bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000878 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000879 if (Tok.isNot(AsmToken::Identifier))
880 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000881 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000882 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000883 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000884 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000885 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000886 else
887 return Error(L, "unrecognized syntax mode in .syntax directive");
888
889 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000890 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000891 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000892
893 // TODO tell the MC streamer the mode
894 // getParser().getStreamer().Emit???();
895 return false;
896}
897
898/// ParseDirectiveCode
899/// ::= .code 16 | 32
900bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000901 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000902 if (Tok.isNot(AsmToken::Integer))
903 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000904 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +0000905 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +0000906 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000907 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +0000908 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000909 else
910 return Error(L, "invalid operand to .code directive");
911
912 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000913 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000914 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000915
916 // TODO tell the MC streamer the mode
917 // getParser().getStreamer().Emit???();
918 return false;
919}
920
Sean Callanan90b70972010-04-07 20:29:34 +0000921extern "C" void LLVMInitializeARMAsmLexer();
922
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000923/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000924extern "C" void LLVMInitializeARMAsmParser() {
925 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
926 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +0000927 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000928}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000929
Chris Lattner0692ee62010-09-06 19:11:01 +0000930#define GET_REGISTER_MATCHER
931#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000932#include "ARMGenAsmMatcher.inc"