blob: db776850464043524323d84ee96ac00fe0161d7d [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"
Chris Lattner76593892010-01-14 21:21:40 +000015#include "llvm/MC/MCParsedAsmOperand.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000016#include "llvm/MC/MCStreamer.h"
17#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCInst.h"
Bill Wendling079b6f52009-12-28 01:20:29 +000019#include "llvm/Support/Compiler.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000020#include "llvm/Support/SourceMgr.h"
21#include "llvm/Target/TargetRegistry.h"
22#include "llvm/Target/TargetAsmParser.h"
23using namespace llvm;
24
25namespace {
26struct ARMOperand;
27
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000028// The shift types for register controlled shifts in arm memory addressing
29enum ShiftType {
30 Lsl,
31 Lsr,
32 Asr,
33 Ror,
34 Rrx
35};
36
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000037class ARMAsmParser : public TargetAsmParser {
38 MCAsmParser &Parser;
39
40private:
41 MCAsmParser &getParser() const { return Parser; }
42
43 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
44
45 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
46
47 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
48
Kevin Enderby9c41fa82009-10-30 22:55:57 +000049 bool MaybeParseRegister(ARMOperand &Op, bool ParseWriteBack);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000050
Kevin Enderbyd7894f12009-10-09 21:12:28 +000051 bool ParseRegisterList(ARMOperand &Op);
52
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000053 bool ParseMemory(ARMOperand &Op);
54
Kevin Enderby9c41fa82009-10-30 22:55:57 +000055 bool ParseMemoryOffsetReg(bool &Negative,
56 bool &OffsetRegShifted,
57 enum ShiftType &ShiftType,
58 const MCExpr *&ShiftAmount,
59 const MCExpr *&Offset,
60 bool &OffsetIsReg,
Kevin Enderby60131c02009-11-02 20:14:39 +000061 int &OffsetRegNum);
Kevin Enderby9c41fa82009-10-30 22:55:57 +000062
63 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000064
65 bool ParseOperand(ARMOperand &Op);
66
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000067 bool ParseDirectiveWord(unsigned Size, SMLoc L);
68
Kevin Enderby515d5092009-10-15 20:48:48 +000069 bool ParseDirectiveThumb(SMLoc L);
70
71 bool ParseDirectiveThumbFunc(SMLoc L);
72
73 bool ParseDirectiveCode(SMLoc L);
74
75 bool ParseDirectiveSyntax(SMLoc L);
76
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000077 // TODO - For now hacked versions of the next two are in here in this file to
78 // allow some parser testing until the table gen versions are implemented.
79
80 /// @name Auto-generated Match Functions
81 /// {
82 bool MatchInstruction(SmallVectorImpl<ARMOperand> &Operands,
83 MCInst &Inst);
84
Kevin Enderbyd7894f12009-10-09 21:12:28 +000085 /// MatchRegisterName - Match the given string to a register name and return
86 /// its register number, or -1 if there is no match. To allow return values
87 /// to be used directly in register lists, arm registers have values between
88 /// 0 and 15.
89 int MatchRegisterName(const StringRef &Name);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000090
91 /// }
92
93
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000094public:
95 ARMAsmParser(const Target &T, MCAsmParser &_Parser)
96 : TargetAsmParser(T), Parser(_Parser) {}
97
98 virtual bool ParseInstruction(const StringRef &Name, MCInst &Inst);
99
100 virtual bool ParseDirective(AsmToken DirectiveID);
101};
102
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000103/// ARMOperand - Instances of this class represent a parsed ARM machine
104/// instruction.
Chris Lattner76593892010-01-14 21:21:40 +0000105struct ARMOperand : public MCParsedAsmOperand {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000106 enum {
107 Token,
108 Register,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000109 Immediate,
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000110 Memory
111 } Kind;
112
113
114 union {
115 struct {
116 const char *Data;
117 unsigned Length;
118 } Tok;
119
120 struct {
121 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000122 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000123 } Reg;
124
Kevin Enderbycfe07242009-10-13 22:19:02 +0000125 struct {
126 const MCExpr *Val;
127 } Imm;
128
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000129 // This is for all forms of ARM address expressions
130 struct {
131 unsigned BaseRegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000132 unsigned OffsetRegNum; // used when OffsetIsReg is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000133 const MCExpr *Offset; // used when OffsetIsReg is false
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000134 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000135 enum ShiftType ShiftType; // used when OffsetRegShifted is true
136 unsigned
137 OffsetRegShifted : 1, // only used when OffsetIsReg is true
138 Preindexed : 1,
139 Postindexed : 1,
140 OffsetIsReg : 1,
141 Negative : 1, // only used when OffsetIsReg is true
142 Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000143 } Mem;
144
145 };
146
147 StringRef getToken() const {
148 assert(Kind == Token && "Invalid access!");
149 return StringRef(Tok.Data, Tok.Length);
150 }
151
152 unsigned getReg() const {
153 assert(Kind == Register && "Invalid access!");
154 return Reg.RegNum;
155 }
156
Kevin Enderbycfe07242009-10-13 22:19:02 +0000157 const MCExpr *getImm() const {
158 assert(Kind == Immediate && "Invalid access!");
159 return Imm.Val;
160 }
161
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000162 bool isToken() const {return Kind == Token; }
163
164 bool isReg() const { return Kind == Register; }
165
166 void addRegOperands(MCInst &Inst, unsigned N) const {
167 assert(N == 1 && "Invalid number of operands!");
168 Inst.addOperand(MCOperand::CreateReg(getReg()));
169 }
170
171 static ARMOperand CreateToken(StringRef Str) {
172 ARMOperand Res;
173 Res.Kind = Token;
174 Res.Tok.Data = Str.data();
175 Res.Tok.Length = Str.size();
176 return Res;
177 }
178
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000179 static ARMOperand CreateReg(unsigned RegNum, bool Writeback) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000180 ARMOperand Res;
181 Res.Kind = Register;
182 Res.Reg.RegNum = RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000183 Res.Reg.Writeback = Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000184 return Res;
185 }
186
Kevin Enderbycfe07242009-10-13 22:19:02 +0000187 static ARMOperand CreateImm(const MCExpr *Val) {
188 ARMOperand Res;
189 Res.Kind = Immediate;
190 Res.Imm.Val = Val;
191 return Res;
192 }
193
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000194 static ARMOperand CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
195 const MCExpr *Offset, unsigned OffsetRegNum,
196 bool OffsetRegShifted, enum ShiftType ShiftType,
197 const MCExpr *ShiftAmount, bool Preindexed,
198 bool Postindexed, bool Negative, bool Writeback) {
199 ARMOperand Res;
200 Res.Kind = Memory;
201 Res.Mem.BaseRegNum = BaseRegNum;
202 Res.Mem.OffsetIsReg = OffsetIsReg;
203 Res.Mem.Offset = Offset;
204 Res.Mem.OffsetRegNum = OffsetRegNum;
205 Res.Mem.OffsetRegShifted = OffsetRegShifted;
206 Res.Mem.ShiftType = ShiftType;
207 Res.Mem.ShiftAmount = ShiftAmount;
208 Res.Mem.Preindexed = Preindexed;
209 Res.Mem.Postindexed = Postindexed;
210 Res.Mem.Negative = Negative;
211 Res.Mem.Writeback = Writeback;
212 return Res;
213 }
214};
215
216} // end anonymous namespace.
217
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000218/// Try to parse a register name. The token must be an Identifier when called,
219/// and if it is a register name a Reg operand is created, the token is eaten
220/// and false is returned. Else true is returned and no token is eaten.
221/// TODO this is likely to change to allow different register types and or to
222/// parse for a specific register type.
223bool ARMAsmParser::MaybeParseRegister(ARMOperand &Op, bool ParseWriteBack) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000224 const AsmToken &Tok = getLexer().getTok();
225 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
226
227 // FIXME: Validate register for the current architecture; we have to do
228 // validation later, so maybe there is no need for this here.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000229 int RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000230
231 RegNum = MatchRegisterName(Tok.getString());
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000232 if (RegNum == -1)
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000233 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000234 getLexer().Lex(); // Eat identifier token.
235
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000236 bool Writeback = false;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000237 if (ParseWriteBack) {
238 const AsmToken &ExclaimTok = getLexer().getTok();
239 if (ExclaimTok.is(AsmToken::Exclaim)) {
240 Writeback = true;
241 getLexer().Lex(); // Eat exclaim token
242 }
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000243 }
244
245 Op = ARMOperand::CreateReg(RegNum, Writeback);
246
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000247 return false;
248}
249
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000250/// Parse a register list, return false if successful else return true or an
251/// error. The first token must be a '{' when called.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000252bool ARMAsmParser::ParseRegisterList(ARMOperand &Op) {
Kevin Enderby6bd266e2009-10-12 22:51:49 +0000253 assert(getLexer().getTok().is(AsmToken::LCurly) &&
Kevin Enderbycfe07242009-10-13 22:19:02 +0000254 "Token is not an Left Curly Brace");
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000255 getLexer().Lex(); // Eat left curly brace token.
256
257 const AsmToken &RegTok = getLexer().getTok();
258 SMLoc RegLoc = RegTok.getLoc();
259 if (RegTok.isNot(AsmToken::Identifier))
260 return Error(RegLoc, "register expected");
261 int RegNum = MatchRegisterName(RegTok.getString());
262 if (RegNum == -1)
263 return Error(RegLoc, "register expected");
264 getLexer().Lex(); // Eat identifier token.
265 unsigned RegList = 1 << RegNum;
266
267 int HighRegNum = RegNum;
268 // TODO ranges like "{Rn-Rm}"
269 while (getLexer().getTok().is(AsmToken::Comma)) {
270 getLexer().Lex(); // Eat comma token.
271
272 const AsmToken &RegTok = getLexer().getTok();
273 SMLoc RegLoc = RegTok.getLoc();
274 if (RegTok.isNot(AsmToken::Identifier))
275 return Error(RegLoc, "register expected");
276 int RegNum = MatchRegisterName(RegTok.getString());
277 if (RegNum == -1)
278 return Error(RegLoc, "register expected");
279
280 if (RegList & (1 << RegNum))
281 Warning(RegLoc, "register duplicated in register list");
282 else if (RegNum <= HighRegNum)
283 Warning(RegLoc, "register not in ascending order in register list");
284 RegList |= 1 << RegNum;
285 HighRegNum = RegNum;
286
287 getLexer().Lex(); // Eat identifier token.
288 }
289 const AsmToken &RCurlyTok = getLexer().getTok();
290 if (RCurlyTok.isNot(AsmToken::RCurly))
291 return Error(RCurlyTok.getLoc(), "'}' expected");
292 getLexer().Lex(); // Eat left curly brace token.
293
294 return false;
295}
296
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000297/// Parse an arm memory expression, return false if successful else return true
298/// or an error. The first token must be a '[' when called.
299/// TODO Only preindexing and postindexing addressing are started, unindexed
300/// with option, etc are still to do.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000301bool ARMAsmParser::ParseMemory(ARMOperand &Op) {
Kevin Enderby6bd266e2009-10-12 22:51:49 +0000302 assert(getLexer().getTok().is(AsmToken::LBrac) &&
303 "Token is not an Left Bracket");
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000304 getLexer().Lex(); // Eat left bracket token.
305
306 const AsmToken &BaseRegTok = getLexer().getTok();
307 if (BaseRegTok.isNot(AsmToken::Identifier))
308 return Error(BaseRegTok.getLoc(), "register expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000309 if (MaybeParseRegister(Op, false))
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000310 return Error(BaseRegTok.getLoc(), "register expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000311 int BaseRegNum = Op.getReg();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000312
313 bool Preindexed = false;
314 bool Postindexed = false;
315 bool OffsetIsReg = false;
316 bool Negative = false;
317 bool Writeback = false;
318
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000319 // First look for preindexed address forms, that is after the "[Rn" we now
320 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000321 const AsmToken &Tok = getLexer().getTok();
322 if (Tok.is(AsmToken::Comma)) {
323 Preindexed = true;
324 getLexer().Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000325 int OffsetRegNum;
326 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000327 enum ShiftType ShiftType;
328 const MCExpr *ShiftAmount;
329 const MCExpr *Offset;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000330 if(ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
331 Offset, OffsetIsReg, OffsetRegNum))
332 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000333 const AsmToken &RBracTok = getLexer().getTok();
334 if (RBracTok.isNot(AsmToken::RBrac))
335 return Error(RBracTok.getLoc(), "']' expected");
336 getLexer().Lex(); // Eat right bracket token.
337
338 const AsmToken &ExclaimTok = getLexer().getTok();
339 if (ExclaimTok.is(AsmToken::Exclaim)) {
340 Writeback = true;
341 getLexer().Lex(); // Eat exclaim token
342 }
343 Op = ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
344 OffsetRegShifted, ShiftType, ShiftAmount,
345 Preindexed, Postindexed, Negative, Writeback);
346 return false;
347 }
348 // The "[Rn" we have so far was not followed by a comma.
349 else if (Tok.is(AsmToken::RBrac)) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000350 // This is a post indexing addressing forms, that is a ']' follows after
351 // the "[Rn".
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000352 Postindexed = true;
353 Writeback = true;
354 getLexer().Lex(); // Eat right bracket token.
355
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000356 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000357 bool OffsetRegShifted = false;
358 enum ShiftType ShiftType;
359 const MCExpr *ShiftAmount;
360 const MCExpr *Offset;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000361
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000362 const AsmToken &NextTok = getLexer().getTok();
363 if (NextTok.isNot(AsmToken::EndOfStatement)) {
364 if (NextTok.isNot(AsmToken::Comma))
365 return Error(NextTok.getLoc(), "',' expected");
366 getLexer().Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000367 if(ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
368 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum))
369 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000370 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000371
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000372 Op = ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
373 OffsetRegShifted, ShiftType, ShiftAmount,
374 Preindexed, Postindexed, Negative, Writeback);
375 return false;
376 }
377
378 return true;
379}
380
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000381/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
382/// we will parse the following (were +/- means that a plus or minus is
383/// optional):
384/// +/-Rm
385/// +/-Rm, shift
386/// #offset
387/// we return false on success or an error otherwise.
388bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
389 bool &OffsetRegShifted,
390 enum ShiftType &ShiftType,
391 const MCExpr *&ShiftAmount,
392 const MCExpr *&Offset,
393 bool &OffsetIsReg,
Kevin Enderby60131c02009-11-02 20:14:39 +0000394 int &OffsetRegNum) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000395 ARMOperand Op;
396 Negative = false;
397 OffsetRegShifted = false;
398 OffsetIsReg = false;
399 OffsetRegNum = -1;
400 const AsmToken &NextTok = getLexer().getTok();
401 if (NextTok.is(AsmToken::Plus))
402 getLexer().Lex(); // Eat plus token.
403 else if (NextTok.is(AsmToken::Minus)) {
404 Negative = true;
405 getLexer().Lex(); // Eat minus token
406 }
407 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
408 const AsmToken &OffsetRegTok = getLexer().getTok();
409 if (OffsetRegTok.is(AsmToken::Identifier)) {
410 OffsetIsReg = !MaybeParseRegister(Op, false);
411 if (OffsetIsReg)
412 OffsetRegNum = Op.getReg();
413 }
414 // If we parsed a register as the offset then their can be a shift after that
415 if (OffsetRegNum != -1) {
416 // Look for a comma then a shift
417 const AsmToken &Tok = getLexer().getTok();
418 if (Tok.is(AsmToken::Comma)) {
419 getLexer().Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000420
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000421 const AsmToken &Tok = getLexer().getTok();
422 if (ParseShift(ShiftType, ShiftAmount))
423 return Error(Tok.getLoc(), "shift expected");
424 OffsetRegShifted = true;
425 }
426 }
427 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
428 // Look for #offset following the "[Rn," or "[Rn],"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000429 const AsmToken &HashTok = getLexer().getTok();
430 if (HashTok.isNot(AsmToken::Hash))
431 return Error(HashTok.getLoc(), "'#' expected");
432 getLexer().Lex(); // Eat hash token.
433
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000434 if (getParser().ParseExpression(Offset))
435 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000436 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000437 return false;
438}
439
440/// ParseShift as one of these two:
441/// ( lsl | lsr | asr | ror ) , # shift_amount
442/// rrx
443/// and returns true if it parses a shift otherwise it returns false.
444bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount) {
445 const AsmToken &Tok = getLexer().getTok();
446 if (Tok.isNot(AsmToken::Identifier))
447 return true;
448 const StringRef &ShiftName = Tok.getString();
449 if (ShiftName == "lsl" || ShiftName == "LSL")
450 St = Lsl;
451 else if (ShiftName == "lsr" || ShiftName == "LSR")
452 St = Lsr;
453 else if (ShiftName == "asr" || ShiftName == "ASR")
454 St = Asr;
455 else if (ShiftName == "ror" || ShiftName == "ROR")
456 St = Ror;
457 else if (ShiftName == "rrx" || ShiftName == "RRX")
458 St = Rrx;
459 else
460 return true;
461 getLexer().Lex(); // Eat shift type token.
462
463 // Rrx stands alone.
464 if (St == Rrx)
465 return false;
466
467 // Otherwise, there must be a '#' and a shift amount.
468 const AsmToken &HashTok = getLexer().getTok();
469 if (HashTok.isNot(AsmToken::Hash))
470 return Error(HashTok.getLoc(), "'#' expected");
471 getLexer().Lex(); // Eat hash token.
472
473 if (getParser().ParseExpression(ShiftAmount))
474 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000475
476 return false;
477}
478
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000479/// A hack to allow some testing, to be replaced by a real table gen version.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000480int ARMAsmParser::MatchRegisterName(const StringRef &Name) {
481 if (Name == "r0" || Name == "R0")
482 return 0;
483 else if (Name == "r1" || Name == "R1")
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000484 return 1;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000485 else if (Name == "r2" || Name == "R2")
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000486 return 2;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000487 else if (Name == "r3" || Name == "R3")
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000488 return 3;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000489 else if (Name == "r3" || Name == "R3")
490 return 3;
491 else if (Name == "r4" || Name == "R4")
492 return 4;
493 else if (Name == "r5" || Name == "R5")
494 return 5;
495 else if (Name == "r6" || Name == "R6")
496 return 6;
497 else if (Name == "r7" || Name == "R7")
498 return 7;
499 else if (Name == "r8" || Name == "R8")
500 return 8;
501 else if (Name == "r9" || Name == "R9")
502 return 9;
503 else if (Name == "r10" || Name == "R10")
504 return 10;
505 else if (Name == "r11" || Name == "R11" || Name == "fp")
506 return 11;
507 else if (Name == "r12" || Name == "R12" || Name == "ip")
508 return 12;
509 else if (Name == "r13" || Name == "R13" || Name == "sp")
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000510 return 13;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000511 else if (Name == "r14" || Name == "R14" || Name == "lr")
512 return 14;
513 else if (Name == "r15" || Name == "R15" || Name == "pc")
514 return 15;
515 return -1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000516}
517
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000518/// A hack to allow some testing, to be replaced by a real table gen version.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000519bool ARMAsmParser::MatchInstruction(SmallVectorImpl<ARMOperand> &Operands,
520 MCInst &Inst) {
521 struct ARMOperand Op0 = Operands[0];
522 assert(Op0.Kind == ARMOperand::Token && "First operand not a Token");
523 const StringRef &Mnemonic = Op0.getToken();
524 if (Mnemonic == "add" ||
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000525 Mnemonic == "stmfd" ||
526 Mnemonic == "str" ||
527 Mnemonic == "ldmfd" ||
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000528 Mnemonic == "ldr" ||
Kevin Enderbycfe07242009-10-13 22:19:02 +0000529 Mnemonic == "mov" ||
Kevin Enderby515d5092009-10-15 20:48:48 +0000530 Mnemonic == "sub" ||
531 Mnemonic == "bl" ||
532 Mnemonic == "push" ||
533 Mnemonic == "blx" ||
Daniel Dunbar2685a292009-10-20 05:15:36 +0000534 Mnemonic == "pop") {
535 // Hard-coded to a valid instruction, till we have a real matcher.
536 Inst = MCInst();
537 Inst.setOpcode(ARM::MOVr);
538 Inst.addOperand(MCOperand::CreateReg(2));
539 Inst.addOperand(MCOperand::CreateReg(2));
540 Inst.addOperand(MCOperand::CreateImm(0));
541 Inst.addOperand(MCOperand::CreateImm(0));
542 Inst.addOperand(MCOperand::CreateReg(0));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000543 return false;
Daniel Dunbar2685a292009-10-20 05:15:36 +0000544 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000545
546 return true;
547}
548
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000549/// Parse a arm instruction operand. For now this parses the operand regardless
550/// of the mnemonic.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000551bool ARMAsmParser::ParseOperand(ARMOperand &Op) {
552 switch (getLexer().getKind()) {
553 case AsmToken::Identifier:
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000554 if (!MaybeParseRegister(Op, true))
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000555 return false;
Kevin Enderby515d5092009-10-15 20:48:48 +0000556 // This was not a register so parse other operands that start with an
557 // identifier (like labels) as expressions and create them as immediates.
558 const MCExpr *IdVal;
559 if (getParser().ParseExpression(IdVal))
560 return true;
561 Op = ARMOperand::CreateImm(IdVal);
562 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000563 case AsmToken::LBrac:
Kevin Enderby515d5092009-10-15 20:48:48 +0000564 return ParseMemory(Op);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000565 case AsmToken::LCurly:
Kevin Enderby515d5092009-10-15 20:48:48 +0000566 return ParseRegisterList(Op);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000567 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000568 // #42 -> immediate.
569 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Kevin Enderbycfe07242009-10-13 22:19:02 +0000570 getLexer().Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000571 const MCExpr *ImmVal;
572 if (getParser().ParseExpression(ImmVal))
Kevin Enderbycfe07242009-10-13 22:19:02 +0000573 return true;
Kevin Enderby515d5092009-10-15 20:48:48 +0000574 Op = ARMOperand::CreateImm(ImmVal);
Kevin Enderbycfe07242009-10-13 22:19:02 +0000575 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000576 default:
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000577 return Error(getLexer().getTok().getLoc(), "unexpected token in operand");
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000578 }
579}
580
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000581/// Parse an arm instruction mnemonic followed by its operands.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000582bool ARMAsmParser::ParseInstruction(const StringRef &Name, MCInst &Inst) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000583 SmallVector<ARMOperand, 7> Operands;
584
585 Operands.push_back(ARMOperand::CreateToken(Name));
586
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000587 SMLoc Loc = getLexer().getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000588 if (getLexer().isNot(AsmToken::EndOfStatement)) {
589
590 // Read the first operand.
591 Operands.push_back(ARMOperand());
592 if (ParseOperand(Operands.back()))
593 return true;
594
595 while (getLexer().is(AsmToken::Comma)) {
596 getLexer().Lex(); // Eat the comma.
597
598 // Parse and remember the operand.
599 Operands.push_back(ARMOperand());
600 if (ParseOperand(Operands.back()))
601 return true;
602 }
603 }
604 if (!MatchInstruction(Operands, Inst))
605 return false;
606
607 Error(Loc, "ARMAsmParser::ParseInstruction only partly implemented");
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000608 return true;
609}
610
Kevin Enderby515d5092009-10-15 20:48:48 +0000611/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000612bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
613 StringRef IDVal = DirectiveID.getIdentifier();
614 if (IDVal == ".word")
615 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000616 else if (IDVal == ".thumb")
617 return ParseDirectiveThumb(DirectiveID.getLoc());
618 else if (IDVal == ".thumb_func")
619 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
620 else if (IDVal == ".code")
621 return ParseDirectiveCode(DirectiveID.getLoc());
622 else if (IDVal == ".syntax")
623 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000624 return true;
625}
626
627/// ParseDirectiveWord
628/// ::= .word [ expression (, expression)* ]
629bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
630 if (getLexer().isNot(AsmToken::EndOfStatement)) {
631 for (;;) {
632 const MCExpr *Value;
633 if (getParser().ParseExpression(Value))
634 return true;
635
636 getParser().getStreamer().EmitValue(Value, Size);
637
638 if (getLexer().is(AsmToken::EndOfStatement))
639 break;
640
641 // FIXME: Improve diagnostic.
642 if (getLexer().isNot(AsmToken::Comma))
643 return Error(L, "unexpected token in directive");
644 getLexer().Lex();
645 }
646 }
647
648 getLexer().Lex();
649 return false;
650}
651
Kevin Enderby515d5092009-10-15 20:48:48 +0000652/// ParseDirectiveThumb
653/// ::= .thumb
654bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
655 if (getLexer().isNot(AsmToken::EndOfStatement))
656 return Error(L, "unexpected token in directive");
657 getLexer().Lex();
658
659 // TODO: set thumb mode
660 // TODO: tell the MC streamer the mode
661 // getParser().getStreamer().Emit???();
662 return false;
663}
664
665/// ParseDirectiveThumbFunc
666/// ::= .thumbfunc symbol_name
667bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
668 const AsmToken &Tok = getLexer().getTok();
669 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
670 return Error(L, "unexpected token in .syntax directive");
Bill Wendling079b6f52009-12-28 01:20:29 +0000671 StringRef ATTRIBUTE_UNUSED SymbolName = getLexer().getTok().getIdentifier();
Kevin Enderby515d5092009-10-15 20:48:48 +0000672 getLexer().Lex(); // Consume the identifier token.
673
674 if (getLexer().isNot(AsmToken::EndOfStatement))
675 return Error(L, "unexpected token in directive");
676 getLexer().Lex();
677
678 // TODO: mark symbol as a thumb symbol
679 // getParser().getStreamer().Emit???();
680 return false;
681}
682
683/// ParseDirectiveSyntax
684/// ::= .syntax unified | divided
685bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
686 const AsmToken &Tok = getLexer().getTok();
687 if (Tok.isNot(AsmToken::Identifier))
688 return Error(L, "unexpected token in .syntax directive");
689 const StringRef &Mode = Tok.getString();
690 bool unified_syntax;
691 if (Mode == "unified" || Mode == "UNIFIED") {
692 getLexer().Lex();
693 unified_syntax = true;
694 }
695 else if (Mode == "divided" || Mode == "DIVIDED") {
696 getLexer().Lex();
697 unified_syntax = false;
698 }
699 else
700 return Error(L, "unrecognized syntax mode in .syntax directive");
701
702 if (getLexer().isNot(AsmToken::EndOfStatement))
703 return Error(getLexer().getTok().getLoc(), "unexpected token in directive");
704 getLexer().Lex();
705
706 // TODO tell the MC streamer the mode
707 // getParser().getStreamer().Emit???();
708 return false;
709}
710
711/// ParseDirectiveCode
712/// ::= .code 16 | 32
713bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
714 const AsmToken &Tok = getLexer().getTok();
715 if (Tok.isNot(AsmToken::Integer))
716 return Error(L, "unexpected token in .code directive");
717 int64_t Val = getLexer().getTok().getIntVal();
718 bool thumb_mode;
719 if (Val == 16) {
720 getLexer().Lex();
721 thumb_mode = true;
722 }
723 else if (Val == 32) {
724 getLexer().Lex();
725 thumb_mode = false;
726 }
727 else
728 return Error(L, "invalid operand to .code directive");
729
730 if (getLexer().isNot(AsmToken::EndOfStatement))
731 return Error(getLexer().getTok().getLoc(), "unexpected token in directive");
732 getLexer().Lex();
733
734 // TODO tell the MC streamer the mode
735 // getParser().getStreamer().Emit???();
736 return false;
737}
738
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000739/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000740extern "C" void LLVMInitializeARMAsmParser() {
741 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
742 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
743}