blob: cf2d0eb9721badf261d01918d12d6a66195b1d88 [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,
81 MCStreamer &Out) {
Chris Lattner7036f8b2010-09-29 01:42:58 +000082 MCInst Inst;
Chris Lattnerce4a3352010-09-06 22:11:18 +000083 unsigned ErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +000084 if (MatchInstructionImpl(Operands, Inst, ErrorInfo) == Match_Success) {
85 Out.EmitInstruction(Inst);
Daniel Dunbarf1e29d42010-08-12 00:55:38 +000086 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +000087 }
Daniel Dunbarf1e29d42010-08-12 00:55:38 +000088
89 // FIXME: We should give nicer diagnostics about the exact failure.
90 Error(IDLoc, "unrecognized instruction");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +000091 return true;
Daniel Dunbar4f98f832010-08-12 00:55:32 +000092 }
93
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000094 /// @name Auto-generated Match Functions
95 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000096
Chris Lattner0692ee62010-09-06 19:11:01 +000097#define GET_ASSEMBLER_HEADER
98#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000099
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000100 /// }
101
102
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000103public:
Daniel Dunbard73ada72010-07-19 00:33:49 +0000104 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
105 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {}
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000106
Benjamin Kramer38e59892010-07-14 22:38:02 +0000107 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000108 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000109
110 virtual bool ParseDirective(AsmToken DirectiveID);
111};
Chris Lattner3a697562010-10-28 17:20:03 +0000112} // end anonymous namespace
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000113
Chris Lattner3a697562010-10-28 17:20:03 +0000114namespace {
115
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000116/// ARMOperand - Instances of this class represent a parsed ARM machine
117/// instruction.
Chris Lattner76593892010-01-14 21:21:40 +0000118struct ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000119public:
120 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000121 CondCode,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000122 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000123 Memory,
124 Register,
125 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000126 } Kind;
127
Sean Callanan76264762010-04-02 22:27:05 +0000128 SMLoc StartLoc, EndLoc;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000129
130 union {
131 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000132 ARMCC::CondCodes Val;
133 } CC;
134
135 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000136 const char *Data;
137 unsigned Length;
138 } Tok;
139
140 struct {
141 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000142 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000143 } Reg;
144
Kevin Enderbycfe07242009-10-13 22:19:02 +0000145 struct {
146 const MCExpr *Val;
147 } Imm;
Sean Callanan76264762010-04-02 22:27:05 +0000148
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000149 // This is for all forms of ARM address expressions
150 struct {
151 unsigned BaseRegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000152 unsigned OffsetRegNum; // used when OffsetIsReg is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000153 const MCExpr *Offset; // used when OffsetIsReg is false
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000154 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000155 enum ShiftType ShiftType; // used when OffsetRegShifted is true
156 unsigned
157 OffsetRegShifted : 1, // only used when OffsetIsReg is true
158 Preindexed : 1,
159 Postindexed : 1,
160 OffsetIsReg : 1,
161 Negative : 1, // only used when OffsetIsReg is true
162 Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000163 } Mem;
164
165 };
Sean Callanan76264762010-04-02 22:27:05 +0000166
Chris Lattner14ab39e2010-09-01 16:04:34 +0000167 //ARMOperand(KindTy K, SMLoc S, SMLoc E)
168 // : Kind(K), StartLoc(S), EndLoc(E) {}
Sean Callanan76264762010-04-02 22:27:05 +0000169
170 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
171 Kind = o.Kind;
172 StartLoc = o.StartLoc;
173 EndLoc = o.EndLoc;
174 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000175 case CondCode:
176 CC = o.CC;
177 break;
Sean Callanan76264762010-04-02 22:27:05 +0000178 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000179 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000180 break;
181 case Register:
182 Reg = o.Reg;
183 break;
184 case Immediate:
185 Imm = o.Imm;
186 break;
187 case Memory:
188 Mem = o.Mem;
189 break;
190 }
191 }
192
193 /// getStartLoc - Get the location of the first token of this operand.
194 SMLoc getStartLoc() const { return StartLoc; }
195 /// getEndLoc - Get the location of the last token of this operand.
196 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000197
Daniel Dunbar8462b302010-08-11 06:36:53 +0000198 ARMCC::CondCodes getCondCode() const {
199 assert(Kind == CondCode && "Invalid access!");
200 return CC.Val;
201 }
202
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000203 StringRef getToken() const {
204 assert(Kind == Token && "Invalid access!");
205 return StringRef(Tok.Data, Tok.Length);
206 }
207
208 unsigned getReg() const {
209 assert(Kind == Register && "Invalid access!");
210 return Reg.RegNum;
211 }
212
Kevin Enderbycfe07242009-10-13 22:19:02 +0000213 const MCExpr *getImm() const {
214 assert(Kind == Immediate && "Invalid access!");
215 return Imm.Val;
216 }
217
Daniel Dunbar8462b302010-08-11 06:36:53 +0000218 bool isCondCode() const { return Kind == CondCode; }
219
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000220 bool isImm() const { return Kind == Immediate; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000221
222 bool isReg() const { return Kind == Register; }
223
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000224 bool isToken() const {return Kind == Token; }
225
226 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
227 // Add as immediates when possible.
228 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
229 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
230 else
231 Inst.addOperand(MCOperand::CreateExpr(Expr));
232 }
233
Daniel Dunbar8462b302010-08-11 06:36:53 +0000234 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000235 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000236 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000237 // FIXME: What belongs here?
238 Inst.addOperand(MCOperand::CreateReg(0));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000239 }
240
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000241 void addRegOperands(MCInst &Inst, unsigned N) const {
242 assert(N == 1 && "Invalid number of operands!");
243 Inst.addOperand(MCOperand::CreateReg(getReg()));
244 }
245
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000246 void addImmOperands(MCInst &Inst, unsigned N) const {
247 assert(N == 1 && "Invalid number of operands!");
248 addExpr(Inst, getImm());
249 }
250
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000251 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000252
Chris Lattner3a697562010-10-28 17:20:03 +0000253 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
254 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000255 Op->CC.Val = CC;
256 Op->StartLoc = S;
257 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000258 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000259 }
260
Chris Lattner3a697562010-10-28 17:20:03 +0000261 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
262 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000263 Op->Tok.Data = Str.data();
264 Op->Tok.Length = Str.size();
265 Op->StartLoc = S;
266 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000267 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000268 }
269
Chris Lattner3a697562010-10-28 17:20:03 +0000270 static ARMOperand *CreateReg(unsigned RegNum, bool Writeback, SMLoc S,
271 SMLoc E) {
272 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000273 Op->Reg.RegNum = RegNum;
274 Op->Reg.Writeback = Writeback;
Sean Callanan76264762010-04-02 22:27:05 +0000275 Op->StartLoc = S;
276 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000277 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000278 }
279
Chris Lattner3a697562010-10-28 17:20:03 +0000280 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
281 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000282 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000283 Op->StartLoc = S;
284 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000285 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000286 }
287
Chris Lattner3a697562010-10-28 17:20:03 +0000288 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
289 const MCExpr *Offset, unsigned OffsetRegNum,
290 bool OffsetRegShifted, enum ShiftType ShiftType,
291 const MCExpr *ShiftAmount, bool Preindexed,
292 bool Postindexed, bool Negative, bool Writeback,
293 SMLoc S, SMLoc E) {
294 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000295 Op->Mem.BaseRegNum = BaseRegNum;
296 Op->Mem.OffsetIsReg = OffsetIsReg;
297 Op->Mem.Offset = Offset;
298 Op->Mem.OffsetRegNum = OffsetRegNum;
299 Op->Mem.OffsetRegShifted = OffsetRegShifted;
300 Op->Mem.ShiftType = ShiftType;
301 Op->Mem.ShiftAmount = ShiftAmount;
302 Op->Mem.Preindexed = Preindexed;
303 Op->Mem.Postindexed = Postindexed;
304 Op->Mem.Negative = Negative;
305 Op->Mem.Writeback = Writeback;
306
307 Op->StartLoc = S;
308 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000309 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000310 }
Chris Lattner3a697562010-10-28 17:20:03 +0000311
312private:
313 ARMOperand(KindTy K) : Kind(K) {}
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000314};
315
316} // end anonymous namespace.
317
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000318void ARMOperand::dump(raw_ostream &OS) const {
319 switch (Kind) {
320 case CondCode:
321 OS << ARMCondCodeToString(getCondCode());
322 break;
323 case Immediate:
324 getImm()->print(OS);
325 break;
326 case Memory:
327 OS << "<memory>";
328 break;
329 case Register:
330 OS << "<register " << getReg() << ">";
331 break;
332 case Token:
333 OS << "'" << getToken() << "'";
334 break;
335 }
336}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000337
338/// @name Auto-generated Match Functions
339/// {
340
341static unsigned MatchRegisterName(StringRef Name);
342
343/// }
344
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000345/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattner3a697562010-10-28 17:20:03 +0000346/// and if it is a register name the token is eaten and a Reg operand is created
347/// and returned. Otherwise return null.
348///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000349/// TODO this is likely to change to allow different register types and or to
350/// parse for a specific register type.
Chris Lattner3a697562010-10-28 17:20:03 +0000351ARMOperand *ARMAsmParser::MaybeParseRegister(bool ParseWriteBack) {
Sean Callanan76264762010-04-02 22:27:05 +0000352 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000353 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000354 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
355
356 // FIXME: Validate register for the current architecture; we have to do
357 // validation later, so maybe there is no need for this here.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000358 int RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000359
360 RegNum = MatchRegisterName(Tok.getString());
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000361 if (RegNum == -1)
Chris Lattner3a697562010-10-28 17:20:03 +0000362 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000363
364 S = Tok.getLoc();
365
Sean Callananb9a25b72010-01-19 20:27:46 +0000366 Parser.Lex(); // Eat identifier token.
Sean Callanan76264762010-04-02 22:27:05 +0000367
368 E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000369
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000370 bool Writeback = false;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000371 if (ParseWriteBack) {
Sean Callanan18b83232010-01-19 21:44:56 +0000372 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000373 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000374 E = ExclaimTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000375 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000376 Parser.Lex(); // Eat exclaim token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000377 }
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000378 }
379
Chris Lattner3a697562010-10-28 17:20:03 +0000380 return ARMOperand::CreateReg(RegNum, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000381}
382
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000383/// Parse a register list, return it if successful else return null. The first
384/// token must be a '{' when called.
385ARMOperand *ARMAsmParser::ParseRegisterList() {
Sean Callanan76264762010-04-02 22:27:05 +0000386 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000387 assert(Parser.getTok().is(AsmToken::LCurly) &&
Kevin Enderbycfe07242009-10-13 22:19:02 +0000388 "Token is not an Left Curly Brace");
Sean Callanan76264762010-04-02 22:27:05 +0000389 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000390 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000391
Sean Callanan18b83232010-01-19 21:44:56 +0000392 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000393 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000394 if (RegTok.isNot(AsmToken::Identifier)) {
395 Error(RegLoc, "register expected");
396 return 0;
397 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000398 int RegNum = MatchRegisterName(RegTok.getString());
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000399 if (RegNum == -1) {
400 Error(RegLoc, "register expected");
401 return 0;
402 }
403
Sean Callananb9a25b72010-01-19 20:27:46 +0000404 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000405 unsigned RegList = 1 << RegNum;
406
407 int HighRegNum = RegNum;
408 // TODO ranges like "{Rn-Rm}"
Sean Callanan18b83232010-01-19 21:44:56 +0000409 while (Parser.getTok().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000410 Parser.Lex(); // Eat comma token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000411
Sean Callanan18b83232010-01-19 21:44:56 +0000412 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000413 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000414 if (RegTok.isNot(AsmToken::Identifier)) {
415 Error(RegLoc, "register expected");
416 return 0;
417 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000418 int RegNum = MatchRegisterName(RegTok.getString());
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000419 if (RegNum == -1) {
420 Error(RegLoc, "register expected");
421 return 0;
422 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000423
424 if (RegList & (1 << RegNum))
425 Warning(RegLoc, "register duplicated in register list");
426 else if (RegNum <= HighRegNum)
427 Warning(RegLoc, "register not in ascending order in register list");
428 RegList |= 1 << RegNum;
429 HighRegNum = RegNum;
430
Sean Callananb9a25b72010-01-19 20:27:46 +0000431 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000432 }
Sean Callanan18b83232010-01-19 21:44:56 +0000433 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000434 if (RCurlyTok.isNot(AsmToken::RCurly)) {
435 Error(RCurlyTok.getLoc(), "'}' expected");
436 return 0;
437 }
Sean Callanan76264762010-04-02 22:27:05 +0000438 E = RCurlyTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000439 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000440
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000441 // FIXME: Need to return an operand!
442 Error(E, "FIXME: register list parsing not implemented");
443 return 0;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000444}
445
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000446/// Parse an arm memory expression, return false if successful else return true
447/// or an error. The first token must be a '[' when called.
448/// TODO Only preindexing and postindexing addressing are started, unindexed
449/// with option, etc are still to do.
Chris Lattner550276e2010-10-28 20:52:15 +0000450ARMOperand *ARMAsmParser::ParseMemory() {
Sean Callanan76264762010-04-02 22:27:05 +0000451 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000452 assert(Parser.getTok().is(AsmToken::LBrac) &&
Kevin Enderby6bd266e2009-10-12 22:51:49 +0000453 "Token is not an Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000454 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000455 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000456
Sean Callanan18b83232010-01-19 21:44:56 +0000457 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000458 if (BaseRegTok.isNot(AsmToken::Identifier)) {
459 Error(BaseRegTok.getLoc(), "register expected");
460 return 0;
461 }
462 int BaseRegNum = 0;
463 if (ARMOperand *Op = MaybeParseRegister(false))
464 BaseRegNum = Op->getReg();
465 else {
466 Error(BaseRegTok.getLoc(), "register expected");
467 return 0;
468 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000469
470 bool Preindexed = false;
471 bool Postindexed = false;
472 bool OffsetIsReg = false;
473 bool Negative = false;
474 bool Writeback = false;
475
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000476 // First look for preindexed address forms, that is after the "[Rn" we now
477 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000478 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000479 if (Tok.is(AsmToken::Comma)) {
480 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000481 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000482 int OffsetRegNum;
483 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000484 enum ShiftType ShiftType;
485 const MCExpr *ShiftAmount;
486 const MCExpr *Offset;
Chris Lattner550276e2010-10-28 20:52:15 +0000487 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
488 Offset, OffsetIsReg, OffsetRegNum, E))
489 return 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000490 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000491 if (RBracTok.isNot(AsmToken::RBrac)) {
492 Error(RBracTok.getLoc(), "']' expected");
493 return 0;
494 }
Sean Callanan76264762010-04-02 22:27:05 +0000495 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000496 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000497
Sean Callanan18b83232010-01-19 21:44:56 +0000498 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000499 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000500 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000501 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000502 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000503 }
Chris Lattner550276e2010-10-28 20:52:15 +0000504 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
505 OffsetRegShifted, ShiftType, ShiftAmount,
506 Preindexed, Postindexed, Negative, Writeback,
507 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000508 }
509 // The "[Rn" we have so far was not followed by a comma.
510 else if (Tok.is(AsmToken::RBrac)) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000511 // This is a post indexing addressing forms, that is a ']' follows after
512 // the "[Rn".
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000513 Postindexed = true;
514 Writeback = true;
Sean Callanan76264762010-04-02 22:27:05 +0000515 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000516 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000517
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000518 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000519 bool OffsetRegShifted = false;
520 enum ShiftType ShiftType;
521 const MCExpr *ShiftAmount;
522 const MCExpr *Offset;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000523
Sean Callanan18b83232010-01-19 21:44:56 +0000524 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000525 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Chris Lattner550276e2010-10-28 20:52:15 +0000526 if (NextTok.isNot(AsmToken::Comma)) {
527 Error(NextTok.getLoc(), "',' expected");
528 return 0;
529 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000530 Parser.Lex(); // Eat comma token.
Chris Lattner550276e2010-10-28 20:52:15 +0000531 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
532 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
533 E))
534 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000535 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000536
Chris Lattner550276e2010-10-28 20:52:15 +0000537 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
538 OffsetRegShifted, ShiftType, ShiftAmount,
539 Preindexed, Postindexed, Negative, Writeback,
540 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000541 }
542
Chris Lattner550276e2010-10-28 20:52:15 +0000543 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000544}
545
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000546/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
547/// we will parse the following (were +/- means that a plus or minus is
548/// optional):
549/// +/-Rm
550/// +/-Rm, shift
551/// #offset
552/// we return false on success or an error otherwise.
553bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000554 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000555 enum ShiftType &ShiftType,
556 const MCExpr *&ShiftAmount,
557 const MCExpr *&Offset,
558 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000559 int &OffsetRegNum,
560 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000561 Negative = false;
562 OffsetRegShifted = false;
563 OffsetIsReg = false;
564 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000565 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000566 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000567 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000568 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000569 else if (NextTok.is(AsmToken::Minus)) {
570 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000571 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000572 }
573 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000574 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000575 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattner550276e2010-10-28 20:52:15 +0000576 if (ARMOperand *Op = MaybeParseRegister(false)) {
577 OffsetIsReg = true;
Sean Callanan76264762010-04-02 22:27:05 +0000578 E = Op->getEndLoc();
579 OffsetRegNum = Op->getReg();
Chris Lattner550276e2010-10-28 20:52:15 +0000580 delete Op;
Sean Callanan76264762010-04-02 22:27:05 +0000581 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000582 }
583 // If we parsed a register as the offset then their can be a shift after that
584 if (OffsetRegNum != -1) {
585 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000586 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000587 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000588 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000589
Sean Callanan18b83232010-01-19 21:44:56 +0000590 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000591 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000592 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000593 OffsetRegShifted = true;
594 }
595 }
596 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
597 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000598 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000599 if (HashTok.isNot(AsmToken::Hash))
600 return Error(HashTok.getLoc(), "'#' expected");
Sean Callanan76264762010-04-02 22:27:05 +0000601
Sean Callananb9a25b72010-01-19 20:27:46 +0000602 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000603
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000604 if (getParser().ParseExpression(Offset))
605 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000606 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000607 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000608 return false;
609}
610
611/// ParseShift as one of these two:
612/// ( lsl | lsr | asr | ror ) , # shift_amount
613/// rrx
614/// and returns true if it parses a shift otherwise it returns false.
Chris Lattner3a697562010-10-28 17:20:03 +0000615bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000616 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000617 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000618 if (Tok.isNot(AsmToken::Identifier))
619 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000620 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000621 if (ShiftName == "lsl" || ShiftName == "LSL")
622 St = Lsl;
623 else if (ShiftName == "lsr" || ShiftName == "LSR")
624 St = Lsr;
625 else if (ShiftName == "asr" || ShiftName == "ASR")
626 St = Asr;
627 else if (ShiftName == "ror" || ShiftName == "ROR")
628 St = Ror;
629 else if (ShiftName == "rrx" || ShiftName == "RRX")
630 St = Rrx;
631 else
632 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000633 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000634
635 // Rrx stands alone.
636 if (St == Rrx)
637 return false;
638
639 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000640 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000641 if (HashTok.isNot(AsmToken::Hash))
642 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000643 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000644
645 if (getParser().ParseExpression(ShiftAmount))
646 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000647
648 return false;
649}
650
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000651/// Parse a arm instruction operand. For now this parses the operand regardless
652/// of the mnemonic.
Chris Lattner550276e2010-10-28 20:52:15 +0000653ARMOperand *ARMAsmParser::ParseOperand() {
Sean Callanan76264762010-04-02 22:27:05 +0000654 SMLoc S, E;
655
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000656 switch (getLexer().getKind()) {
657 case AsmToken::Identifier:
Chris Lattner550276e2010-10-28 20:52:15 +0000658 if (ARMOperand *Op = MaybeParseRegister(true))
659 return Op;
660
Kevin Enderby515d5092009-10-15 20:48:48 +0000661 // This was not a register so parse other operands that start with an
662 // identifier (like labels) as expressions and create them as immediates.
663 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000664 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000665 if (getParser().ParseExpression(IdVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000666 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000667 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000668 return ARMOperand::CreateImm(IdVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000669 case AsmToken::LBrac:
Chris Lattner550276e2010-10-28 20:52:15 +0000670 return ParseMemory();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000671 case AsmToken::LCurly:
Chris Lattner550276e2010-10-28 20:52:15 +0000672 return ParseRegisterList();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000673 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000674 // #42 -> immediate.
675 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000676 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000677 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000678 const MCExpr *ImmVal;
679 if (getParser().ParseExpression(ImmVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000680 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000681 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000682 return ARMOperand::CreateImm(ImmVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000683 default:
Chris Lattner550276e2010-10-28 20:52:15 +0000684 Error(Parser.getTok().getLoc(), "unexpected token in operand");
685 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000686 }
687}
688
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000689/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000690bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000691 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000692 // Create the leading tokens for the mnemonic, split by '.' characters.
693 size_t Start = 0, Next = Name.find('.');
694 StringRef Head = Name.slice(Start, Next);
695
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000696 // Determine the predicate, if any.
697 //
698 // FIXME: We need a way to check whether a prefix supports predication,
699 // otherwise we will end up with an ambiguity for instructions that happen to
700 // end with a predicate name.
701 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
702 .Case("eq", ARMCC::EQ)
703 .Case("ne", ARMCC::NE)
704 .Case("hs", ARMCC::HS)
705 .Case("lo", ARMCC::LO)
706 .Case("mi", ARMCC::MI)
707 .Case("pl", ARMCC::PL)
708 .Case("vs", ARMCC::VS)
709 .Case("vc", ARMCC::VC)
710 .Case("hi", ARMCC::HI)
711 .Case("ls", ARMCC::LS)
712 .Case("ge", ARMCC::GE)
713 .Case("lt", ARMCC::LT)
714 .Case("gt", ARMCC::GT)
715 .Case("le", ARMCC::LE)
716 .Case("al", ARMCC::AL)
717 .Default(~0U);
Chris Lattner3a697562010-10-28 17:20:03 +0000718
719 if (CC != ~0U)
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000720 Head = Head.slice(0, Head.size() - 2);
Chris Lattner3a697562010-10-28 17:20:03 +0000721 else
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000722 CC = ARMCC::AL;
723
Chris Lattner3a697562010-10-28 17:20:03 +0000724 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
725 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000726
727 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000728 while (Next != StringRef::npos) {
729 Start = Next;
730 Next = Name.find('.', Start + 1);
731 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000732
Chris Lattner3a697562010-10-28 17:20:03 +0000733 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000734 }
735
736 // Read the remaining operands.
737 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000738 // Read the first operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000739 if (ARMOperand *Op = ParseOperand())
740 Operands.push_back(Op);
741 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000742 Parser.EatToEndOfStatement();
743 return true;
744 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000745
746 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000747 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000748
749 // Parse and remember the operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000750 if (ARMOperand *Op = ParseOperand())
751 Operands.push_back(Op);
752 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000753 Parser.EatToEndOfStatement();
754 return true;
755 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000756 }
757 }
Chris Lattner34e53142010-09-08 05:10:46 +0000758
Chris Lattnercbf8a982010-09-11 16:18:25 +0000759 if (getLexer().isNot(AsmToken::EndOfStatement)) {
760 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000761 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000762 }
Chris Lattner34e53142010-09-08 05:10:46 +0000763 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000764 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000765}
766
Kevin Enderby515d5092009-10-15 20:48:48 +0000767/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000768bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
769 StringRef IDVal = DirectiveID.getIdentifier();
770 if (IDVal == ".word")
771 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000772 else if (IDVal == ".thumb")
773 return ParseDirectiveThumb(DirectiveID.getLoc());
774 else if (IDVal == ".thumb_func")
775 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
776 else if (IDVal == ".code")
777 return ParseDirectiveCode(DirectiveID.getLoc());
778 else if (IDVal == ".syntax")
779 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000780 return true;
781}
782
783/// ParseDirectiveWord
784/// ::= .word [ expression (, expression)* ]
785bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
786 if (getLexer().isNot(AsmToken::EndOfStatement)) {
787 for (;;) {
788 const MCExpr *Value;
789 if (getParser().ParseExpression(Value))
790 return true;
791
Chris Lattneraaec2052010-01-19 19:46:13 +0000792 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000793
794 if (getLexer().is(AsmToken::EndOfStatement))
795 break;
796
797 // FIXME: Improve diagnostic.
798 if (getLexer().isNot(AsmToken::Comma))
799 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000800 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000801 }
802 }
803
Sean Callananb9a25b72010-01-19 20:27:46 +0000804 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000805 return false;
806}
807
Kevin Enderby515d5092009-10-15 20:48:48 +0000808/// ParseDirectiveThumb
809/// ::= .thumb
810bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
811 if (getLexer().isNot(AsmToken::EndOfStatement))
812 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000813 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000814
815 // TODO: set thumb mode
816 // TODO: tell the MC streamer the mode
817 // getParser().getStreamer().Emit???();
818 return false;
819}
820
821/// ParseDirectiveThumbFunc
822/// ::= .thumbfunc symbol_name
823bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000824 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000825 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
826 return Error(L, "unexpected token in .syntax directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000827 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000828
829 if (getLexer().isNot(AsmToken::EndOfStatement))
830 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000831 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000832
833 // TODO: mark symbol as a thumb symbol
834 // getParser().getStreamer().Emit???();
835 return false;
836}
837
838/// ParseDirectiveSyntax
839/// ::= .syntax unified | divided
840bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000841 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000842 if (Tok.isNot(AsmToken::Identifier))
843 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000844 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000845 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000846 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000847 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000848 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000849 else
850 return Error(L, "unrecognized syntax mode in .syntax directive");
851
852 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000853 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000854 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000855
856 // TODO tell the MC streamer the mode
857 // getParser().getStreamer().Emit???();
858 return false;
859}
860
861/// ParseDirectiveCode
862/// ::= .code 16 | 32
863bool ARMAsmParser::ParseDirectiveCode(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::Integer))
866 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000867 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +0000868 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +0000869 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000870 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +0000871 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000872 else
873 return Error(L, "invalid operand to .code directive");
874
875 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000876 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000877 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000878
879 // TODO tell the MC streamer the mode
880 // getParser().getStreamer().Emit???();
881 return false;
882}
883
Sean Callanan90b70972010-04-07 20:29:34 +0000884extern "C" void LLVMInitializeARMAsmLexer();
885
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000886/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000887extern "C" void LLVMInitializeARMAsmParser() {
888 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
889 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +0000890 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000891}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000892
Chris Lattner0692ee62010-09-06 19:11:01 +0000893#define GET_REGISTER_MATCHER
894#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000895#include "ARMGenAsmMatcher.inc"