blob: ed4667ba18556094ddcdcd6ce3ad1c721a5b98a0 [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"
11#include "llvm/ADT/SmallVector.h"
12#include "llvm/ADT/Twine.h"
13#include "llvm/MC/MCAsmLexer.h"
14#include "llvm/MC/MCAsmParser.h"
15#include "llvm/MC/MCStreamer.h"
16#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCInst.h"
Bill Wendling079b6f52009-12-28 01:20:29 +000018#include "llvm/Support/Compiler.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000019#include "llvm/Support/SourceMgr.h"
20#include "llvm/Target/TargetRegistry.h"
21#include "llvm/Target/TargetAsmParser.h"
22using namespace llvm;
23
24namespace {
25struct ARMOperand;
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
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000036class ARMAsmParser : public TargetAsmParser {
37 MCAsmParser &Parser;
38
39private:
40 MCAsmParser &getParser() const { return Parser; }
41
42 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
43
44 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
45
46 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
47
Kevin Enderby9c41fa82009-10-30 22:55:57 +000048 bool MaybeParseRegister(ARMOperand &Op, bool ParseWriteBack);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000049
Kevin Enderbyd7894f12009-10-09 21:12:28 +000050 bool ParseRegisterList(ARMOperand &Op);
51
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000052 bool ParseMemory(ARMOperand &Op);
53
Kevin Enderby9c41fa82009-10-30 22:55:57 +000054 bool ParseMemoryOffsetReg(bool &Negative,
55 bool &OffsetRegShifted,
56 enum ShiftType &ShiftType,
57 const MCExpr *&ShiftAmount,
58 const MCExpr *&Offset,
59 bool &OffsetIsReg,
Kevin Enderby60131c02009-11-02 20:14:39 +000060 int &OffsetRegNum);
Kevin Enderby9c41fa82009-10-30 22:55:57 +000061
62 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000063
64 bool ParseOperand(ARMOperand &Op);
65
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000066 bool ParseDirectiveWord(unsigned Size, SMLoc L);
67
Kevin Enderby515d5092009-10-15 20:48:48 +000068 bool ParseDirectiveThumb(SMLoc L);
69
70 bool ParseDirectiveThumbFunc(SMLoc L);
71
72 bool ParseDirectiveCode(SMLoc L);
73
74 bool ParseDirectiveSyntax(SMLoc L);
75
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000076 // TODO - For now hacked versions of the next two are in here in this file to
77 // allow some parser testing until the table gen versions are implemented.
78
79 /// @name Auto-generated Match Functions
80 /// {
81 bool MatchInstruction(SmallVectorImpl<ARMOperand> &Operands,
82 MCInst &Inst);
83
Kevin Enderbyd7894f12009-10-09 21:12:28 +000084 /// MatchRegisterName - Match the given string to a register name and return
85 /// its register number, or -1 if there is no match. To allow return values
86 /// to be used directly in register lists, arm registers have values between
87 /// 0 and 15.
88 int MatchRegisterName(const StringRef &Name);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000089
90 /// }
91
92
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000093public:
94 ARMAsmParser(const Target &T, MCAsmParser &_Parser)
95 : TargetAsmParser(T), Parser(_Parser) {}
96
97 virtual bool ParseInstruction(const StringRef &Name, MCInst &Inst);
98
99 virtual bool ParseDirective(AsmToken DirectiveID);
100};
101
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000102/// ARMOperand - Instances of this class represent a parsed ARM machine
103/// instruction.
104struct ARMOperand {
105 enum {
106 Token,
107 Register,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000108 Immediate,
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000109 Memory
110 } Kind;
111
112
113 union {
114 struct {
115 const char *Data;
116 unsigned Length;
117 } Tok;
118
119 struct {
120 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000121 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000122 } Reg;
123
Kevin Enderbycfe07242009-10-13 22:19:02 +0000124 struct {
125 const MCExpr *Val;
126 } Imm;
127
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000128 // This is for all forms of ARM address expressions
129 struct {
130 unsigned BaseRegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000131 unsigned OffsetRegNum; // used when OffsetIsReg is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000132 const MCExpr *Offset; // used when OffsetIsReg is false
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000133 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000134 enum ShiftType ShiftType; // used when OffsetRegShifted is true
135 unsigned
136 OffsetRegShifted : 1, // only used when OffsetIsReg is true
137 Preindexed : 1,
138 Postindexed : 1,
139 OffsetIsReg : 1,
140 Negative : 1, // only used when OffsetIsReg is true
141 Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000142 } Mem;
143
144 };
145
146 StringRef getToken() const {
147 assert(Kind == Token && "Invalid access!");
148 return StringRef(Tok.Data, Tok.Length);
149 }
150
151 unsigned getReg() const {
152 assert(Kind == Register && "Invalid access!");
153 return Reg.RegNum;
154 }
155
Kevin Enderbycfe07242009-10-13 22:19:02 +0000156 const MCExpr *getImm() const {
157 assert(Kind == Immediate && "Invalid access!");
158 return Imm.Val;
159 }
160
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000161 bool isToken() const {return Kind == Token; }
162
163 bool isReg() const { return Kind == Register; }
164
165 void addRegOperands(MCInst &Inst, unsigned N) const {
166 assert(N == 1 && "Invalid number of operands!");
167 Inst.addOperand(MCOperand::CreateReg(getReg()));
168 }
169
170 static ARMOperand CreateToken(StringRef Str) {
171 ARMOperand Res;
172 Res.Kind = Token;
173 Res.Tok.Data = Str.data();
174 Res.Tok.Length = Str.size();
175 return Res;
176 }
177
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000178 static ARMOperand CreateReg(unsigned RegNum, bool Writeback) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000179 ARMOperand Res;
180 Res.Kind = Register;
181 Res.Reg.RegNum = RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000182 Res.Reg.Writeback = Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000183 return Res;
184 }
185
Kevin Enderbycfe07242009-10-13 22:19:02 +0000186 static ARMOperand CreateImm(const MCExpr *Val) {
187 ARMOperand Res;
188 Res.Kind = Immediate;
189 Res.Imm.Val = Val;
190 return Res;
191 }
192
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000193 static ARMOperand CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
194 const MCExpr *Offset, unsigned OffsetRegNum,
195 bool OffsetRegShifted, enum ShiftType ShiftType,
196 const MCExpr *ShiftAmount, bool Preindexed,
197 bool Postindexed, bool Negative, bool Writeback) {
198 ARMOperand Res;
199 Res.Kind = Memory;
200 Res.Mem.BaseRegNum = BaseRegNum;
201 Res.Mem.OffsetIsReg = OffsetIsReg;
202 Res.Mem.Offset = Offset;
203 Res.Mem.OffsetRegNum = OffsetRegNum;
204 Res.Mem.OffsetRegShifted = OffsetRegShifted;
205 Res.Mem.ShiftType = ShiftType;
206 Res.Mem.ShiftAmount = ShiftAmount;
207 Res.Mem.Preindexed = Preindexed;
208 Res.Mem.Postindexed = Postindexed;
209 Res.Mem.Negative = Negative;
210 Res.Mem.Writeback = Writeback;
211 return Res;
212 }
213};
214
215} // end anonymous namespace.
216
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000217/// Try to parse a register name. The token must be an Identifier when called,
218/// and if it is a register name a Reg operand is created, the token is eaten
219/// and false is returned. Else true is returned and no token is eaten.
220/// TODO this is likely to change to allow different register types and or to
221/// parse for a specific register type.
222bool ARMAsmParser::MaybeParseRegister(ARMOperand &Op, bool ParseWriteBack) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000223 const AsmToken &Tok = getLexer().getTok();
224 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
225
226 // FIXME: Validate register for the current architecture; we have to do
227 // validation later, so maybe there is no need for this here.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000228 int RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000229
230 RegNum = MatchRegisterName(Tok.getString());
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000231 if (RegNum == -1)
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000232 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000233 getLexer().Lex(); // Eat identifier token.
234
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000235 bool Writeback = false;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000236 if (ParseWriteBack) {
237 const AsmToken &ExclaimTok = getLexer().getTok();
238 if (ExclaimTok.is(AsmToken::Exclaim)) {
239 Writeback = true;
240 getLexer().Lex(); // Eat exclaim token
241 }
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000242 }
243
244 Op = ARMOperand::CreateReg(RegNum, Writeback);
245
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000246 return false;
247}
248
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000249/// Parse a register list, return false if successful else return true or an
250/// error. The first token must be a '{' when called.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000251bool ARMAsmParser::ParseRegisterList(ARMOperand &Op) {
Kevin Enderby6bd266e2009-10-12 22:51:49 +0000252 assert(getLexer().getTok().is(AsmToken::LCurly) &&
Kevin Enderbycfe07242009-10-13 22:19:02 +0000253 "Token is not an Left Curly Brace");
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000254 getLexer().Lex(); // Eat left curly brace token.
255
256 const AsmToken &RegTok = getLexer().getTok();
257 SMLoc RegLoc = RegTok.getLoc();
258 if (RegTok.isNot(AsmToken::Identifier))
259 return Error(RegLoc, "register expected");
260 int RegNum = MatchRegisterName(RegTok.getString());
261 if (RegNum == -1)
262 return Error(RegLoc, "register expected");
263 getLexer().Lex(); // Eat identifier token.
264 unsigned RegList = 1 << RegNum;
265
266 int HighRegNum = RegNum;
267 // TODO ranges like "{Rn-Rm}"
268 while (getLexer().getTok().is(AsmToken::Comma)) {
269 getLexer().Lex(); // Eat comma token.
270
271 const AsmToken &RegTok = getLexer().getTok();
272 SMLoc RegLoc = RegTok.getLoc();
273 if (RegTok.isNot(AsmToken::Identifier))
274 return Error(RegLoc, "register expected");
275 int RegNum = MatchRegisterName(RegTok.getString());
276 if (RegNum == -1)
277 return Error(RegLoc, "register expected");
278
279 if (RegList & (1 << RegNum))
280 Warning(RegLoc, "register duplicated in register list");
281 else if (RegNum <= HighRegNum)
282 Warning(RegLoc, "register not in ascending order in register list");
283 RegList |= 1 << RegNum;
284 HighRegNum = RegNum;
285
286 getLexer().Lex(); // Eat identifier token.
287 }
288 const AsmToken &RCurlyTok = getLexer().getTok();
289 if (RCurlyTok.isNot(AsmToken::RCurly))
290 return Error(RCurlyTok.getLoc(), "'}' expected");
291 getLexer().Lex(); // Eat left curly brace token.
292
293 return false;
294}
295
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000296/// Parse an arm memory expression, return false if successful else return true
297/// or an error. The first token must be a '[' when called.
298/// TODO Only preindexing and postindexing addressing are started, unindexed
299/// with option, etc are still to do.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000300bool ARMAsmParser::ParseMemory(ARMOperand &Op) {
Kevin Enderby6bd266e2009-10-12 22:51:49 +0000301 assert(getLexer().getTok().is(AsmToken::LBrac) &&
302 "Token is not an Left Bracket");
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000303 getLexer().Lex(); // Eat left bracket token.
304
305 const AsmToken &BaseRegTok = getLexer().getTok();
306 if (BaseRegTok.isNot(AsmToken::Identifier))
307 return Error(BaseRegTok.getLoc(), "register expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000308 if (MaybeParseRegister(Op, false))
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000309 return Error(BaseRegTok.getLoc(), "register expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000310 int BaseRegNum = Op.getReg();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000311
312 bool Preindexed = false;
313 bool Postindexed = false;
314 bool OffsetIsReg = false;
315 bool Negative = false;
316 bool Writeback = false;
317
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000318 // First look for preindexed address forms, that is after the "[Rn" we now
319 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000320 const AsmToken &Tok = getLexer().getTok();
321 if (Tok.is(AsmToken::Comma)) {
322 Preindexed = true;
323 getLexer().Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000324 int OffsetRegNum;
325 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000326 enum ShiftType ShiftType;
327 const MCExpr *ShiftAmount;
328 const MCExpr *Offset;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000329 if(ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
330 Offset, OffsetIsReg, OffsetRegNum))
331 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000332 const AsmToken &RBracTok = getLexer().getTok();
333 if (RBracTok.isNot(AsmToken::RBrac))
334 return Error(RBracTok.getLoc(), "']' expected");
335 getLexer().Lex(); // Eat right bracket token.
336
337 const AsmToken &ExclaimTok = getLexer().getTok();
338 if (ExclaimTok.is(AsmToken::Exclaim)) {
339 Writeback = true;
340 getLexer().Lex(); // Eat exclaim token
341 }
342 Op = ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
343 OffsetRegShifted, ShiftType, ShiftAmount,
344 Preindexed, Postindexed, Negative, Writeback);
345 return false;
346 }
347 // The "[Rn" we have so far was not followed by a comma.
348 else if (Tok.is(AsmToken::RBrac)) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000349 // This is a post indexing addressing forms, that is a ']' follows after
350 // the "[Rn".
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000351 Postindexed = true;
352 Writeback = true;
353 getLexer().Lex(); // Eat right bracket token.
354
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000355 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000356 bool OffsetRegShifted = false;
357 enum ShiftType ShiftType;
358 const MCExpr *ShiftAmount;
359 const MCExpr *Offset;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000360
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000361 const AsmToken &NextTok = getLexer().getTok();
362 if (NextTok.isNot(AsmToken::EndOfStatement)) {
363 if (NextTok.isNot(AsmToken::Comma))
364 return Error(NextTok.getLoc(), "',' expected");
365 getLexer().Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000366 if(ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
367 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum))
368 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000369 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000370
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000371 Op = ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
372 OffsetRegShifted, ShiftType, ShiftAmount,
373 Preindexed, Postindexed, Negative, Writeback);
374 return false;
375 }
376
377 return true;
378}
379
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000380/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
381/// we will parse the following (were +/- means that a plus or minus is
382/// optional):
383/// +/-Rm
384/// +/-Rm, shift
385/// #offset
386/// we return false on success or an error otherwise.
387bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
388 bool &OffsetRegShifted,
389 enum ShiftType &ShiftType,
390 const MCExpr *&ShiftAmount,
391 const MCExpr *&Offset,
392 bool &OffsetIsReg,
Kevin Enderby60131c02009-11-02 20:14:39 +0000393 int &OffsetRegNum) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000394 ARMOperand Op;
395 Negative = false;
396 OffsetRegShifted = false;
397 OffsetIsReg = false;
398 OffsetRegNum = -1;
399 const AsmToken &NextTok = getLexer().getTok();
400 if (NextTok.is(AsmToken::Plus))
401 getLexer().Lex(); // Eat plus token.
402 else if (NextTok.is(AsmToken::Minus)) {
403 Negative = true;
404 getLexer().Lex(); // Eat minus token
405 }
406 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
407 const AsmToken &OffsetRegTok = getLexer().getTok();
408 if (OffsetRegTok.is(AsmToken::Identifier)) {
409 OffsetIsReg = !MaybeParseRegister(Op, false);
410 if (OffsetIsReg)
411 OffsetRegNum = Op.getReg();
412 }
413 // If we parsed a register as the offset then their can be a shift after that
414 if (OffsetRegNum != -1) {
415 // Look for a comma then a shift
416 const AsmToken &Tok = getLexer().getTok();
417 if (Tok.is(AsmToken::Comma)) {
418 getLexer().Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000419
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000420 const AsmToken &Tok = getLexer().getTok();
421 if (ParseShift(ShiftType, ShiftAmount))
422 return Error(Tok.getLoc(), "shift expected");
423 OffsetRegShifted = true;
424 }
425 }
426 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
427 // Look for #offset following the "[Rn," or "[Rn],"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000428 const AsmToken &HashTok = getLexer().getTok();
429 if (HashTok.isNot(AsmToken::Hash))
430 return Error(HashTok.getLoc(), "'#' expected");
431 getLexer().Lex(); // Eat hash token.
432
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000433 if (getParser().ParseExpression(Offset))
434 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000435 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000436 return false;
437}
438
439/// ParseShift as one of these two:
440/// ( lsl | lsr | asr | ror ) , # shift_amount
441/// rrx
442/// and returns true if it parses a shift otherwise it returns false.
443bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount) {
444 const AsmToken &Tok = getLexer().getTok();
445 if (Tok.isNot(AsmToken::Identifier))
446 return true;
447 const StringRef &ShiftName = Tok.getString();
448 if (ShiftName == "lsl" || ShiftName == "LSL")
449 St = Lsl;
450 else if (ShiftName == "lsr" || ShiftName == "LSR")
451 St = Lsr;
452 else if (ShiftName == "asr" || ShiftName == "ASR")
453 St = Asr;
454 else if (ShiftName == "ror" || ShiftName == "ROR")
455 St = Ror;
456 else if (ShiftName == "rrx" || ShiftName == "RRX")
457 St = Rrx;
458 else
459 return true;
460 getLexer().Lex(); // Eat shift type token.
461
462 // Rrx stands alone.
463 if (St == Rrx)
464 return false;
465
466 // Otherwise, there must be a '#' and a shift amount.
467 const AsmToken &HashTok = getLexer().getTok();
468 if (HashTok.isNot(AsmToken::Hash))
469 return Error(HashTok.getLoc(), "'#' expected");
470 getLexer().Lex(); // Eat hash token.
471
472 if (getParser().ParseExpression(ShiftAmount))
473 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000474
475 return false;
476}
477
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000478/// A hack to allow some testing, to be replaced by a real table gen version.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000479int ARMAsmParser::MatchRegisterName(const StringRef &Name) {
480 if (Name == "r0" || Name == "R0")
481 return 0;
482 else if (Name == "r1" || Name == "R1")
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000483 return 1;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000484 else if (Name == "r2" || Name == "R2")
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000485 return 2;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000486 else if (Name == "r3" || Name == "R3")
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000487 return 3;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000488 else if (Name == "r3" || Name == "R3")
489 return 3;
490 else if (Name == "r4" || Name == "R4")
491 return 4;
492 else if (Name == "r5" || Name == "R5")
493 return 5;
494 else if (Name == "r6" || Name == "R6")
495 return 6;
496 else if (Name == "r7" || Name == "R7")
497 return 7;
498 else if (Name == "r8" || Name == "R8")
499 return 8;
500 else if (Name == "r9" || Name == "R9")
501 return 9;
502 else if (Name == "r10" || Name == "R10")
503 return 10;
504 else if (Name == "r11" || Name == "R11" || Name == "fp")
505 return 11;
506 else if (Name == "r12" || Name == "R12" || Name == "ip")
507 return 12;
508 else if (Name == "r13" || Name == "R13" || Name == "sp")
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000509 return 13;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000510 else if (Name == "r14" || Name == "R14" || Name == "lr")
511 return 14;
512 else if (Name == "r15" || Name == "R15" || Name == "pc")
513 return 15;
514 return -1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000515}
516
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000517/// A hack to allow some testing, to be replaced by a real table gen version.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000518bool ARMAsmParser::MatchInstruction(SmallVectorImpl<ARMOperand> &Operands,
519 MCInst &Inst) {
520 struct ARMOperand Op0 = Operands[0];
521 assert(Op0.Kind == ARMOperand::Token && "First operand not a Token");
522 const StringRef &Mnemonic = Op0.getToken();
523 if (Mnemonic == "add" ||
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000524 Mnemonic == "stmfd" ||
525 Mnemonic == "str" ||
526 Mnemonic == "ldmfd" ||
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000527 Mnemonic == "ldr" ||
Kevin Enderbycfe07242009-10-13 22:19:02 +0000528 Mnemonic == "mov" ||
Kevin Enderby515d5092009-10-15 20:48:48 +0000529 Mnemonic == "sub" ||
530 Mnemonic == "bl" ||
531 Mnemonic == "push" ||
532 Mnemonic == "blx" ||
Daniel Dunbar2685a292009-10-20 05:15:36 +0000533 Mnemonic == "pop") {
534 // Hard-coded to a valid instruction, till we have a real matcher.
535 Inst = MCInst();
536 Inst.setOpcode(ARM::MOVr);
537 Inst.addOperand(MCOperand::CreateReg(2));
538 Inst.addOperand(MCOperand::CreateReg(2));
539 Inst.addOperand(MCOperand::CreateImm(0));
540 Inst.addOperand(MCOperand::CreateImm(0));
541 Inst.addOperand(MCOperand::CreateReg(0));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000542 return false;
Daniel Dunbar2685a292009-10-20 05:15:36 +0000543 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000544
545 return true;
546}
547
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000548/// Parse a arm instruction operand. For now this parses the operand regardless
549/// of the mnemonic.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000550bool ARMAsmParser::ParseOperand(ARMOperand &Op) {
551 switch (getLexer().getKind()) {
552 case AsmToken::Identifier:
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000553 if (!MaybeParseRegister(Op, true))
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000554 return false;
Kevin Enderby515d5092009-10-15 20:48:48 +0000555 // This was not a register so parse other operands that start with an
556 // identifier (like labels) as expressions and create them as immediates.
557 const MCExpr *IdVal;
558 if (getParser().ParseExpression(IdVal))
559 return true;
560 Op = ARMOperand::CreateImm(IdVal);
561 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000562 case AsmToken::LBrac:
Kevin Enderby515d5092009-10-15 20:48:48 +0000563 return ParseMemory(Op);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000564 case AsmToken::LCurly:
Kevin Enderby515d5092009-10-15 20:48:48 +0000565 return ParseRegisterList(Op);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000566 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000567 // #42 -> immediate.
568 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Kevin Enderbycfe07242009-10-13 22:19:02 +0000569 getLexer().Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000570 const MCExpr *ImmVal;
571 if (getParser().ParseExpression(ImmVal))
Kevin Enderbycfe07242009-10-13 22:19:02 +0000572 return true;
Kevin Enderby515d5092009-10-15 20:48:48 +0000573 Op = ARMOperand::CreateImm(ImmVal);
Kevin Enderbycfe07242009-10-13 22:19:02 +0000574 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000575 default:
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000576 return Error(getLexer().getTok().getLoc(), "unexpected token in operand");
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000577 }
578}
579
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000580/// Parse an arm instruction mnemonic followed by its operands.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000581bool ARMAsmParser::ParseInstruction(const StringRef &Name, MCInst &Inst) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000582 SmallVector<ARMOperand, 7> Operands;
583
584 Operands.push_back(ARMOperand::CreateToken(Name));
585
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000586 SMLoc Loc = getLexer().getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000587 if (getLexer().isNot(AsmToken::EndOfStatement)) {
588
589 // Read the first operand.
590 Operands.push_back(ARMOperand());
591 if (ParseOperand(Operands.back()))
592 return true;
593
594 while (getLexer().is(AsmToken::Comma)) {
595 getLexer().Lex(); // Eat the comma.
596
597 // Parse and remember the operand.
598 Operands.push_back(ARMOperand());
599 if (ParseOperand(Operands.back()))
600 return true;
601 }
602 }
603 if (!MatchInstruction(Operands, Inst))
604 return false;
605
606 Error(Loc, "ARMAsmParser::ParseInstruction only partly implemented");
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000607 return true;
608}
609
Kevin Enderby515d5092009-10-15 20:48:48 +0000610/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000611bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
612 StringRef IDVal = DirectiveID.getIdentifier();
613 if (IDVal == ".word")
614 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000615 else if (IDVal == ".thumb")
616 return ParseDirectiveThumb(DirectiveID.getLoc());
617 else if (IDVal == ".thumb_func")
618 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
619 else if (IDVal == ".code")
620 return ParseDirectiveCode(DirectiveID.getLoc());
621 else if (IDVal == ".syntax")
622 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000623 return true;
624}
625
626/// ParseDirectiveWord
627/// ::= .word [ expression (, expression)* ]
628bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
629 if (getLexer().isNot(AsmToken::EndOfStatement)) {
630 for (;;) {
631 const MCExpr *Value;
632 if (getParser().ParseExpression(Value))
633 return true;
634
635 getParser().getStreamer().EmitValue(Value, Size);
636
637 if (getLexer().is(AsmToken::EndOfStatement))
638 break;
639
640 // FIXME: Improve diagnostic.
641 if (getLexer().isNot(AsmToken::Comma))
642 return Error(L, "unexpected token in directive");
643 getLexer().Lex();
644 }
645 }
646
647 getLexer().Lex();
648 return false;
649}
650
Kevin Enderby515d5092009-10-15 20:48:48 +0000651/// ParseDirectiveThumb
652/// ::= .thumb
653bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
654 if (getLexer().isNot(AsmToken::EndOfStatement))
655 return Error(L, "unexpected token in directive");
656 getLexer().Lex();
657
658 // TODO: set thumb mode
659 // TODO: tell the MC streamer the mode
660 // getParser().getStreamer().Emit???();
661 return false;
662}
663
664/// ParseDirectiveThumbFunc
665/// ::= .thumbfunc symbol_name
666bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
667 const AsmToken &Tok = getLexer().getTok();
668 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
669 return Error(L, "unexpected token in .syntax directive");
Bill Wendling079b6f52009-12-28 01:20:29 +0000670 StringRef ATTRIBUTE_UNUSED SymbolName = getLexer().getTok().getIdentifier();
Kevin Enderby515d5092009-10-15 20:48:48 +0000671 getLexer().Lex(); // Consume the identifier token.
672
673 if (getLexer().isNot(AsmToken::EndOfStatement))
674 return Error(L, "unexpected token in directive");
675 getLexer().Lex();
676
677 // TODO: mark symbol as a thumb symbol
678 // getParser().getStreamer().Emit???();
679 return false;
680}
681
682/// ParseDirectiveSyntax
683/// ::= .syntax unified | divided
684bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
685 const AsmToken &Tok = getLexer().getTok();
686 if (Tok.isNot(AsmToken::Identifier))
687 return Error(L, "unexpected token in .syntax directive");
688 const StringRef &Mode = Tok.getString();
689 bool unified_syntax;
690 if (Mode == "unified" || Mode == "UNIFIED") {
691 getLexer().Lex();
692 unified_syntax = true;
693 }
694 else if (Mode == "divided" || Mode == "DIVIDED") {
695 getLexer().Lex();
696 unified_syntax = false;
697 }
698 else
699 return Error(L, "unrecognized syntax mode in .syntax directive");
700
701 if (getLexer().isNot(AsmToken::EndOfStatement))
702 return Error(getLexer().getTok().getLoc(), "unexpected token in directive");
703 getLexer().Lex();
704
705 // TODO tell the MC streamer the mode
706 // getParser().getStreamer().Emit???();
707 return false;
708}
709
710/// ParseDirectiveCode
711/// ::= .code 16 | 32
712bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
713 const AsmToken &Tok = getLexer().getTok();
714 if (Tok.isNot(AsmToken::Integer))
715 return Error(L, "unexpected token in .code directive");
716 int64_t Val = getLexer().getTok().getIntVal();
717 bool thumb_mode;
718 if (Val == 16) {
719 getLexer().Lex();
720 thumb_mode = true;
721 }
722 else if (Val == 32) {
723 getLexer().Lex();
724 thumb_mode = false;
725 }
726 else
727 return Error(L, "invalid operand to .code directive");
728
729 if (getLexer().isNot(AsmToken::EndOfStatement))
730 return Error(getLexer().getTok().getLoc(), "unexpected token in directive");
731 getLexer().Lex();
732
733 // TODO tell the MC streamer the mode
734 // getParser().getStreamer().Emit???();
735 return false;
736}
737
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000738/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000739extern "C" void LLVMInitializeARMAsmParser() {
740 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
741 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
742}