blob: b6dac44cfce4829fd6de57ba23e3b1fb15997b78 [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 /// {
Chris Lattner98986712010-01-14 22:21:20 +000082 bool MatchInstruction(const SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000083 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
Chris Lattnerf007e852010-01-14 21:32:45 +000098 virtual bool ParseInstruction(const StringRef &Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000099 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000100
101 virtual bool ParseDirective(AsmToken DirectiveID);
102};
103
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000104/// ARMOperand - Instances of this class represent a parsed ARM machine
105/// instruction.
Chris Lattner76593892010-01-14 21:21:40 +0000106struct ARMOperand : public MCParsedAsmOperand {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000107 enum {
108 Token,
109 Register,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000110 Immediate,
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000111 Memory
112 } Kind;
113
114
115 union {
116 struct {
117 const char *Data;
118 unsigned Length;
119 } Tok;
120
121 struct {
122 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000123 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000124 } Reg;
125
Kevin Enderbycfe07242009-10-13 22:19:02 +0000126 struct {
127 const MCExpr *Val;
128 } Imm;
129
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000130 // This is for all forms of ARM address expressions
131 struct {
132 unsigned BaseRegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000133 unsigned OffsetRegNum; // used when OffsetIsReg is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000134 const MCExpr *Offset; // used when OffsetIsReg is false
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000135 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000136 enum ShiftType ShiftType; // used when OffsetRegShifted is true
137 unsigned
138 OffsetRegShifted : 1, // only used when OffsetIsReg is true
139 Preindexed : 1,
140 Postindexed : 1,
141 OffsetIsReg : 1,
142 Negative : 1, // only used when OffsetIsReg is true
143 Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000144 } Mem;
145
146 };
147
148 StringRef getToken() const {
149 assert(Kind == Token && "Invalid access!");
150 return StringRef(Tok.Data, Tok.Length);
151 }
152
153 unsigned getReg() const {
154 assert(Kind == Register && "Invalid access!");
155 return Reg.RegNum;
156 }
157
Kevin Enderbycfe07242009-10-13 22:19:02 +0000158 const MCExpr *getImm() const {
159 assert(Kind == Immediate && "Invalid access!");
160 return Imm.Val;
161 }
162
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000163 bool isToken() const {return Kind == Token; }
164
165 bool isReg() const { return Kind == Register; }
166
167 void addRegOperands(MCInst &Inst, unsigned N) const {
168 assert(N == 1 && "Invalid number of operands!");
169 Inst.addOperand(MCOperand::CreateReg(getReg()));
170 }
171
172 static ARMOperand CreateToken(StringRef Str) {
173 ARMOperand Res;
174 Res.Kind = Token;
175 Res.Tok.Data = Str.data();
176 Res.Tok.Length = Str.size();
177 return Res;
178 }
179
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000180 static ARMOperand CreateReg(unsigned RegNum, bool Writeback) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000181 ARMOperand Res;
182 Res.Kind = Register;
183 Res.Reg.RegNum = RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000184 Res.Reg.Writeback = Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000185 return Res;
186 }
187
Kevin Enderbycfe07242009-10-13 22:19:02 +0000188 static ARMOperand CreateImm(const MCExpr *Val) {
189 ARMOperand Res;
190 Res.Kind = Immediate;
191 Res.Imm.Val = Val;
192 return Res;
193 }
194
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000195 static ARMOperand CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
196 const MCExpr *Offset, unsigned OffsetRegNum,
197 bool OffsetRegShifted, enum ShiftType ShiftType,
198 const MCExpr *ShiftAmount, bool Preindexed,
199 bool Postindexed, bool Negative, bool Writeback) {
200 ARMOperand Res;
201 Res.Kind = Memory;
202 Res.Mem.BaseRegNum = BaseRegNum;
203 Res.Mem.OffsetIsReg = OffsetIsReg;
204 Res.Mem.Offset = Offset;
205 Res.Mem.OffsetRegNum = OffsetRegNum;
206 Res.Mem.OffsetRegShifted = OffsetRegShifted;
207 Res.Mem.ShiftType = ShiftType;
208 Res.Mem.ShiftAmount = ShiftAmount;
209 Res.Mem.Preindexed = Preindexed;
210 Res.Mem.Postindexed = Postindexed;
211 Res.Mem.Negative = Negative;
212 Res.Mem.Writeback = Writeback;
213 return Res;
214 }
215};
216
217} // end anonymous namespace.
218
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000219/// Try to parse a register name. The token must be an Identifier when called,
220/// and if it is a register name a Reg operand is created, the token is eaten
221/// and false is returned. Else true is returned and no token is eaten.
222/// TODO this is likely to change to allow different register types and or to
223/// parse for a specific register type.
224bool ARMAsmParser::MaybeParseRegister(ARMOperand &Op, bool ParseWriteBack) {
Sean Callanan18b83232010-01-19 21:44:56 +0000225 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000226 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
227
228 // FIXME: Validate register for the current architecture; we have to do
229 // validation later, so maybe there is no need for this here.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000230 int RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000231
232 RegNum = MatchRegisterName(Tok.getString());
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000233 if (RegNum == -1)
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000234 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000235 Parser.Lex(); // Eat identifier token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000236
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000237 bool Writeback = false;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000238 if (ParseWriteBack) {
Sean Callanan18b83232010-01-19 21:44:56 +0000239 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000240 if (ExclaimTok.is(AsmToken::Exclaim)) {
241 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000242 Parser.Lex(); // Eat exclaim token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000243 }
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000244 }
245
246 Op = ARMOperand::CreateReg(RegNum, Writeback);
247
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000248 return false;
249}
250
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000251/// Parse a register list, return false if successful else return true or an
252/// error. The first token must be a '{' when called.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000253bool ARMAsmParser::ParseRegisterList(ARMOperand &Op) {
Sean Callanan18b83232010-01-19 21:44:56 +0000254 assert(Parser.getTok().is(AsmToken::LCurly) &&
Kevin Enderbycfe07242009-10-13 22:19:02 +0000255 "Token is not an Left Curly Brace");
Sean Callananb9a25b72010-01-19 20:27:46 +0000256 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000257
Sean Callanan18b83232010-01-19 21:44:56 +0000258 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000259 SMLoc RegLoc = RegTok.getLoc();
260 if (RegTok.isNot(AsmToken::Identifier))
261 return Error(RegLoc, "register expected");
262 int RegNum = MatchRegisterName(RegTok.getString());
263 if (RegNum == -1)
264 return Error(RegLoc, "register expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000265 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000266 unsigned RegList = 1 << RegNum;
267
268 int HighRegNum = RegNum;
269 // TODO ranges like "{Rn-Rm}"
Sean Callanan18b83232010-01-19 21:44:56 +0000270 while (Parser.getTok().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000271 Parser.Lex(); // Eat comma token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000272
Sean Callanan18b83232010-01-19 21:44:56 +0000273 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000274 SMLoc RegLoc = RegTok.getLoc();
275 if (RegTok.isNot(AsmToken::Identifier))
276 return Error(RegLoc, "register expected");
277 int RegNum = MatchRegisterName(RegTok.getString());
278 if (RegNum == -1)
279 return Error(RegLoc, "register expected");
280
281 if (RegList & (1 << RegNum))
282 Warning(RegLoc, "register duplicated in register list");
283 else if (RegNum <= HighRegNum)
284 Warning(RegLoc, "register not in ascending order in register list");
285 RegList |= 1 << RegNum;
286 HighRegNum = RegNum;
287
Sean Callananb9a25b72010-01-19 20:27:46 +0000288 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000289 }
Sean Callanan18b83232010-01-19 21:44:56 +0000290 const AsmToken &RCurlyTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000291 if (RCurlyTok.isNot(AsmToken::RCurly))
292 return Error(RCurlyTok.getLoc(), "'}' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000293 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000294
295 return false;
296}
297
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000298/// Parse an arm memory expression, return false if successful else return true
299/// or an error. The first token must be a '[' when called.
300/// TODO Only preindexing and postindexing addressing are started, unindexed
301/// with option, etc are still to do.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000302bool ARMAsmParser::ParseMemory(ARMOperand &Op) {
Sean Callanan18b83232010-01-19 21:44:56 +0000303 assert(Parser.getTok().is(AsmToken::LBrac) &&
Kevin Enderby6bd266e2009-10-12 22:51:49 +0000304 "Token is not an Left Bracket");
Sean Callananb9a25b72010-01-19 20:27:46 +0000305 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000306
Sean Callanan18b83232010-01-19 21:44:56 +0000307 const AsmToken &BaseRegTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000308 if (BaseRegTok.isNot(AsmToken::Identifier))
309 return Error(BaseRegTok.getLoc(), "register expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000310 if (MaybeParseRegister(Op, false))
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000311 return Error(BaseRegTok.getLoc(), "register expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000312 int BaseRegNum = Op.getReg();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000313
314 bool Preindexed = false;
315 bool Postindexed = false;
316 bool OffsetIsReg = false;
317 bool Negative = false;
318 bool Writeback = false;
319
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000320 // First look for preindexed address forms, that is after the "[Rn" we now
321 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000322 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000323 if (Tok.is(AsmToken::Comma)) {
324 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000325 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000326 int OffsetRegNum;
327 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000328 enum ShiftType ShiftType;
329 const MCExpr *ShiftAmount;
330 const MCExpr *Offset;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000331 if(ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
332 Offset, OffsetIsReg, OffsetRegNum))
333 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000334 const AsmToken &RBracTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000335 if (RBracTok.isNot(AsmToken::RBrac))
336 return Error(RBracTok.getLoc(), "']' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000337 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000338
Sean Callanan18b83232010-01-19 21:44:56 +0000339 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000340 if (ExclaimTok.is(AsmToken::Exclaim)) {
341 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000342 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000343 }
344 Op = ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
345 OffsetRegShifted, ShiftType, ShiftAmount,
346 Preindexed, Postindexed, Negative, Writeback);
347 return false;
348 }
349 // The "[Rn" we have so far was not followed by a comma.
350 else if (Tok.is(AsmToken::RBrac)) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000351 // This is a post indexing addressing forms, that is a ']' follows after
352 // the "[Rn".
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000353 Postindexed = true;
354 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000355 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000356
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000357 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000358 bool OffsetRegShifted = false;
359 enum ShiftType ShiftType;
360 const MCExpr *ShiftAmount;
361 const MCExpr *Offset;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000362
Sean Callanan18b83232010-01-19 21:44:56 +0000363 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000364 if (NextTok.isNot(AsmToken::EndOfStatement)) {
365 if (NextTok.isNot(AsmToken::Comma))
366 return Error(NextTok.getLoc(), "',' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000367 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000368 if(ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
369 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum))
370 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000371 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000372
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000373 Op = ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
374 OffsetRegShifted, ShiftType, ShiftAmount,
375 Preindexed, Postindexed, Negative, Writeback);
376 return false;
377 }
378
379 return true;
380}
381
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000382/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
383/// we will parse the following (were +/- means that a plus or minus is
384/// optional):
385/// +/-Rm
386/// +/-Rm, shift
387/// #offset
388/// we return false on success or an error otherwise.
389bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
390 bool &OffsetRegShifted,
391 enum ShiftType &ShiftType,
392 const MCExpr *&ShiftAmount,
393 const MCExpr *&Offset,
394 bool &OffsetIsReg,
Kevin Enderby60131c02009-11-02 20:14:39 +0000395 int &OffsetRegNum) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000396 ARMOperand Op;
397 Negative = false;
398 OffsetRegShifted = false;
399 OffsetIsReg = false;
400 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000401 const AsmToken &NextTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000402 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000403 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000404 else if (NextTok.is(AsmToken::Minus)) {
405 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000406 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000407 }
408 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000409 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000410 if (OffsetRegTok.is(AsmToken::Identifier)) {
411 OffsetIsReg = !MaybeParseRegister(Op, false);
412 if (OffsetIsReg)
413 OffsetRegNum = Op.getReg();
414 }
415 // If we parsed a register as the offset then their can be a shift after that
416 if (OffsetRegNum != -1) {
417 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000418 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000419 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000420 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000421
Sean Callanan18b83232010-01-19 21:44:56 +0000422 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000423 if (ParseShift(ShiftType, ShiftAmount))
424 return Error(Tok.getLoc(), "shift expected");
425 OffsetRegShifted = true;
426 }
427 }
428 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
429 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000430 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000431 if (HashTok.isNot(AsmToken::Hash))
432 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000433 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000434
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000435 if (getParser().ParseExpression(Offset))
436 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000437 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000438 return false;
439}
440
441/// ParseShift as one of these two:
442/// ( lsl | lsr | asr | ror ) , # shift_amount
443/// rrx
444/// and returns true if it parses a shift otherwise it returns false.
445bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount) {
Sean Callanan18b83232010-01-19 21:44:56 +0000446 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000447 if (Tok.isNot(AsmToken::Identifier))
448 return true;
449 const StringRef &ShiftName = Tok.getString();
450 if (ShiftName == "lsl" || ShiftName == "LSL")
451 St = Lsl;
452 else if (ShiftName == "lsr" || ShiftName == "LSR")
453 St = Lsr;
454 else if (ShiftName == "asr" || ShiftName == "ASR")
455 St = Asr;
456 else if (ShiftName == "ror" || ShiftName == "ROR")
457 St = Ror;
458 else if (ShiftName == "rrx" || ShiftName == "RRX")
459 St = Rrx;
460 else
461 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000462 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000463
464 // Rrx stands alone.
465 if (St == Rrx)
466 return false;
467
468 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000469 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000470 if (HashTok.isNot(AsmToken::Hash))
471 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000472 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000473
474 if (getParser().ParseExpression(ShiftAmount))
475 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000476
477 return false;
478}
479
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000480/// A hack to allow some testing, to be replaced by a real table gen version.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000481int ARMAsmParser::MatchRegisterName(const StringRef &Name) {
482 if (Name == "r0" || Name == "R0")
483 return 0;
484 else if (Name == "r1" || Name == "R1")
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000485 return 1;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000486 else if (Name == "r2" || Name == "R2")
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000487 return 2;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000488 else if (Name == "r3" || Name == "R3")
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000489 return 3;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000490 else if (Name == "r3" || Name == "R3")
491 return 3;
492 else if (Name == "r4" || Name == "R4")
493 return 4;
494 else if (Name == "r5" || Name == "R5")
495 return 5;
496 else if (Name == "r6" || Name == "R6")
497 return 6;
498 else if (Name == "r7" || Name == "R7")
499 return 7;
500 else if (Name == "r8" || Name == "R8")
501 return 8;
502 else if (Name == "r9" || Name == "R9")
503 return 9;
504 else if (Name == "r10" || Name == "R10")
505 return 10;
506 else if (Name == "r11" || Name == "R11" || Name == "fp")
507 return 11;
508 else if (Name == "r12" || Name == "R12" || Name == "ip")
509 return 12;
510 else if (Name == "r13" || Name == "R13" || Name == "sp")
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000511 return 13;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000512 else if (Name == "r14" || Name == "R14" || Name == "lr")
513 return 14;
514 else if (Name == "r15" || Name == "R15" || Name == "pc")
515 return 15;
516 return -1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000517}
518
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000519/// A hack to allow some testing, to be replaced by a real table gen version.
Chris Lattner98986712010-01-14 22:21:20 +0000520bool ARMAsmParser::
521MatchInstruction(const SmallVectorImpl<MCParsedAsmOperand*> &Operands,
522 MCInst &Inst) {
523 ARMOperand &Op0 = *(ARMOperand*)Operands[0];
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000524 assert(Op0.Kind == ARMOperand::Token && "First operand not a Token");
525 const StringRef &Mnemonic = Op0.getToken();
526 if (Mnemonic == "add" ||
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000527 Mnemonic == "stmfd" ||
528 Mnemonic == "str" ||
529 Mnemonic == "ldmfd" ||
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000530 Mnemonic == "ldr" ||
Kevin Enderbycfe07242009-10-13 22:19:02 +0000531 Mnemonic == "mov" ||
Kevin Enderby515d5092009-10-15 20:48:48 +0000532 Mnemonic == "sub" ||
533 Mnemonic == "bl" ||
534 Mnemonic == "push" ||
535 Mnemonic == "blx" ||
Daniel Dunbar2685a292009-10-20 05:15:36 +0000536 Mnemonic == "pop") {
537 // Hard-coded to a valid instruction, till we have a real matcher.
538 Inst = MCInst();
539 Inst.setOpcode(ARM::MOVr);
540 Inst.addOperand(MCOperand::CreateReg(2));
541 Inst.addOperand(MCOperand::CreateReg(2));
542 Inst.addOperand(MCOperand::CreateImm(0));
543 Inst.addOperand(MCOperand::CreateImm(0));
544 Inst.addOperand(MCOperand::CreateReg(0));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000545 return false;
Daniel Dunbar2685a292009-10-20 05:15:36 +0000546 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000547
548 return true;
549}
550
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000551/// Parse a arm instruction operand. For now this parses the operand regardless
552/// of the mnemonic.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000553bool ARMAsmParser::ParseOperand(ARMOperand &Op) {
554 switch (getLexer().getKind()) {
555 case AsmToken::Identifier:
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000556 if (!MaybeParseRegister(Op, true))
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000557 return false;
Kevin Enderby515d5092009-10-15 20:48:48 +0000558 // This was not a register so parse other operands that start with an
559 // identifier (like labels) as expressions and create them as immediates.
560 const MCExpr *IdVal;
561 if (getParser().ParseExpression(IdVal))
562 return true;
563 Op = ARMOperand::CreateImm(IdVal);
564 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000565 case AsmToken::LBrac:
Kevin Enderby515d5092009-10-15 20:48:48 +0000566 return ParseMemory(Op);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000567 case AsmToken::LCurly:
Kevin Enderby515d5092009-10-15 20:48:48 +0000568 return ParseRegisterList(Op);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000569 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000570 // #42 -> immediate.
571 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callananb9a25b72010-01-19 20:27:46 +0000572 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000573 const MCExpr *ImmVal;
574 if (getParser().ParseExpression(ImmVal))
Kevin Enderbycfe07242009-10-13 22:19:02 +0000575 return true;
Kevin Enderby515d5092009-10-15 20:48:48 +0000576 Op = ARMOperand::CreateImm(ImmVal);
Kevin Enderbycfe07242009-10-13 22:19:02 +0000577 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000578 default:
Sean Callanan18b83232010-01-19 21:44:56 +0000579 return Error(Parser.getTok().getLoc(), "unexpected token in operand");
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000580 }
581}
582
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000583/// Parse an arm instruction mnemonic followed by its operands.
Chris Lattnerf007e852010-01-14 21:32:45 +0000584bool ARMAsmParser::ParseInstruction(const StringRef &Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000585 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
586 Operands.push_back(new ARMOperand(ARMOperand::CreateToken(Name)));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000587
Sean Callanan18b83232010-01-19 21:44:56 +0000588 SMLoc Loc = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000589 if (getLexer().isNot(AsmToken::EndOfStatement)) {
590
591 // Read the first operand.
Chris Lattner98986712010-01-14 22:21:20 +0000592 ARMOperand Op;
593 if (ParseOperand(Op)) return true;
594 Operands.push_back(new ARMOperand(Op));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000595
596 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000597 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000598
599 // Parse and remember the operand.
Chris Lattner98986712010-01-14 22:21:20 +0000600 if (ParseOperand(Op)) return true;
601 Operands.push_back(new ARMOperand(Op));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000602 }
603 }
Chris Lattner98986712010-01-14 22:21:20 +0000604 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000605}
606
Kevin Enderby515d5092009-10-15 20:48:48 +0000607/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000608bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
609 StringRef IDVal = DirectiveID.getIdentifier();
610 if (IDVal == ".word")
611 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000612 else if (IDVal == ".thumb")
613 return ParseDirectiveThumb(DirectiveID.getLoc());
614 else if (IDVal == ".thumb_func")
615 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
616 else if (IDVal == ".code")
617 return ParseDirectiveCode(DirectiveID.getLoc());
618 else if (IDVal == ".syntax")
619 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000620 return true;
621}
622
623/// ParseDirectiveWord
624/// ::= .word [ expression (, expression)* ]
625bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
626 if (getLexer().isNot(AsmToken::EndOfStatement)) {
627 for (;;) {
628 const MCExpr *Value;
629 if (getParser().ParseExpression(Value))
630 return true;
631
Chris Lattneraaec2052010-01-19 19:46:13 +0000632 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000633
634 if (getLexer().is(AsmToken::EndOfStatement))
635 break;
636
637 // FIXME: Improve diagnostic.
638 if (getLexer().isNot(AsmToken::Comma))
639 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000640 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000641 }
642 }
643
Sean Callananb9a25b72010-01-19 20:27:46 +0000644 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000645 return false;
646}
647
Kevin Enderby515d5092009-10-15 20:48:48 +0000648/// ParseDirectiveThumb
649/// ::= .thumb
650bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
651 if (getLexer().isNot(AsmToken::EndOfStatement))
652 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000653 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000654
655 // TODO: set thumb mode
656 // TODO: tell the MC streamer the mode
657 // getParser().getStreamer().Emit???();
658 return false;
659}
660
661/// ParseDirectiveThumbFunc
662/// ::= .thumbfunc symbol_name
663bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000664 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000665 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
666 return Error(L, "unexpected token in .syntax directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000667 StringRef ATTRIBUTE_UNUSED SymbolName = Parser.getTok().getIdentifier();
Sean Callananb9a25b72010-01-19 20:27:46 +0000668 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000669
670 if (getLexer().isNot(AsmToken::EndOfStatement))
671 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000672 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000673
674 // TODO: mark symbol as a thumb symbol
675 // getParser().getStreamer().Emit???();
676 return false;
677}
678
679/// ParseDirectiveSyntax
680/// ::= .syntax unified | divided
681bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000682 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000683 if (Tok.isNot(AsmToken::Identifier))
684 return Error(L, "unexpected token in .syntax directive");
685 const StringRef &Mode = Tok.getString();
686 bool unified_syntax;
687 if (Mode == "unified" || Mode == "UNIFIED") {
Sean Callananb9a25b72010-01-19 20:27:46 +0000688 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000689 unified_syntax = true;
690 }
691 else if (Mode == "divided" || Mode == "DIVIDED") {
Sean Callananb9a25b72010-01-19 20:27:46 +0000692 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000693 unified_syntax = false;
694 }
695 else
696 return Error(L, "unrecognized syntax mode in .syntax directive");
697
698 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000699 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000700 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000701
702 // TODO tell the MC streamer the mode
703 // getParser().getStreamer().Emit???();
704 return false;
705}
706
707/// ParseDirectiveCode
708/// ::= .code 16 | 32
709bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000710 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000711 if (Tok.isNot(AsmToken::Integer))
712 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000713 int64_t Val = Parser.getTok().getIntVal();
Kevin Enderby515d5092009-10-15 20:48:48 +0000714 bool thumb_mode;
715 if (Val == 16) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000716 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000717 thumb_mode = true;
718 }
719 else if (Val == 32) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000720 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000721 thumb_mode = false;
722 }
723 else
724 return Error(L, "invalid operand to .code directive");
725
726 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000727 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000728 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000729
730 // TODO tell the MC streamer the mode
731 // getParser().getStreamer().Emit???();
732 return false;
733}
734
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000735/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000736extern "C" void LLVMInitializeARMAsmParser() {
737 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
738 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
739}