blob: 95b57f3471609f0f4420014344f7b859cfc59915 [file] [log] [blame]
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001//===-- ARMAsmParser.cpp - Parse ARM assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ARM.h"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000011#include "ARMSubtarget.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000012#include "llvm/MC/MCParser/MCAsmLexer.h"
13#include "llvm/MC/MCParser/MCAsmParser.h"
14#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000015#include "llvm/MC/MCStreamer.h"
16#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCInst.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000018#include "llvm/Target/TargetRegistry.h"
19#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000020#include "llvm/Support/Compiler.h"
21#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000022#include "llvm/Support/raw_ostream.h"
Sean Callanan76264762010-04-02 22:27:05 +000023#include "llvm/ADT/OwningPtr.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/ADT/SmallVector.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000025#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000026#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000027using namespace llvm;
28
29namespace {
30struct ARMOperand;
31
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000032// The shift types for register controlled shifts in arm memory addressing
33enum ShiftType {
34 Lsl,
35 Lsr,
36 Asr,
37 Ror,
38 Rrx
39};
40
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000041class ARMAsmParser : public TargetAsmParser {
42 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000043 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000044
45private:
46 MCAsmParser &getParser() const { return Parser; }
47
48 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
49
50 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
51
52 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
53
Sean Callanan76264762010-04-02 22:27:05 +000054 bool MaybeParseRegister(OwningPtr<ARMOperand> &Op, bool ParseWriteBack);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000055
Sean Callanan76264762010-04-02 22:27:05 +000056 bool ParseRegisterList(OwningPtr<ARMOperand> &Op);
Kevin Enderbyd7894f12009-10-09 21:12:28 +000057
Sean Callanan76264762010-04-02 22:27:05 +000058 bool ParseMemory(OwningPtr<ARMOperand> &Op);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000059
Kevin Enderby9c41fa82009-10-30 22:55:57 +000060 bool ParseMemoryOffsetReg(bool &Negative,
61 bool &OffsetRegShifted,
62 enum ShiftType &ShiftType,
63 const MCExpr *&ShiftAmount,
64 const MCExpr *&Offset,
65 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000066 int &OffsetRegNum,
67 SMLoc &E);
Kevin Enderby9c41fa82009-10-30 22:55:57 +000068
Sean Callanan76264762010-04-02 22:27:05 +000069 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000070
Sean Callanan76264762010-04-02 22:27:05 +000071 bool ParseOperand(OwningPtr<ARMOperand> &Op);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000072
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000073 bool ParseDirectiveWord(unsigned Size, SMLoc L);
74
Kevin Enderby515d5092009-10-15 20:48:48 +000075 bool ParseDirectiveThumb(SMLoc L);
76
77 bool ParseDirectiveThumbFunc(SMLoc L);
78
79 bool ParseDirectiveCode(SMLoc L);
80
81 bool ParseDirectiveSyntax(SMLoc L);
82
Chris Lattner7036f8b2010-09-29 01:42:58 +000083 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000084 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
85 MCStreamer &Out) {
Chris Lattner7036f8b2010-09-29 01:42:58 +000086 MCInst Inst;
Chris Lattnerce4a3352010-09-06 22:11:18 +000087 unsigned ErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +000088 if (MatchInstructionImpl(Operands, Inst, ErrorInfo) == Match_Success) {
89 Out.EmitInstruction(Inst);
Daniel Dunbarf1e29d42010-08-12 00:55:38 +000090 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +000091 }
Daniel Dunbarf1e29d42010-08-12 00:55:38 +000092
93 // FIXME: We should give nicer diagnostics about the exact failure.
94 Error(IDLoc, "unrecognized instruction");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +000095 return true;
Daniel Dunbar4f98f832010-08-12 00:55:32 +000096 }
97
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000098 /// @name Auto-generated Match Functions
99 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000100
Chris Lattner0692ee62010-09-06 19:11:01 +0000101#define GET_ASSEMBLER_HEADER
102#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000103
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000104 /// }
105
106
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000107public:
Daniel Dunbard73ada72010-07-19 00:33:49 +0000108 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
109 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {}
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000110
Benjamin Kramer38e59892010-07-14 22:38:02 +0000111 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000112 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000113
114 virtual bool ParseDirective(AsmToken DirectiveID);
115};
116
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000117/// ARMOperand - Instances of this class represent a parsed ARM machine
118/// instruction.
Chris Lattner76593892010-01-14 21:21:40 +0000119struct ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000120private:
121 ARMOperand() {}
122public:
123 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000124 CondCode,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000125 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000126 Memory,
127 Register,
128 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000129 } Kind;
130
Sean Callanan76264762010-04-02 22:27:05 +0000131 SMLoc StartLoc, EndLoc;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000132
133 union {
134 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000135 ARMCC::CondCodes Val;
136 } CC;
137
138 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000139 const char *Data;
140 unsigned Length;
141 } Tok;
142
143 struct {
144 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000145 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000146 } Reg;
147
Kevin Enderbycfe07242009-10-13 22:19:02 +0000148 struct {
149 const MCExpr *Val;
150 } Imm;
Sean Callanan76264762010-04-02 22:27:05 +0000151
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000152 // This is for all forms of ARM address expressions
153 struct {
154 unsigned BaseRegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000155 unsigned OffsetRegNum; // used when OffsetIsReg is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000156 const MCExpr *Offset; // used when OffsetIsReg is false
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000157 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000158 enum ShiftType ShiftType; // used when OffsetRegShifted is true
159 unsigned
160 OffsetRegShifted : 1, // only used when OffsetIsReg is true
161 Preindexed : 1,
162 Postindexed : 1,
163 OffsetIsReg : 1,
164 Negative : 1, // only used when OffsetIsReg is true
165 Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000166 } Mem;
167
168 };
Sean Callanan76264762010-04-02 22:27:05 +0000169
Chris Lattner14ab39e2010-09-01 16:04:34 +0000170 //ARMOperand(KindTy K, SMLoc S, SMLoc E)
171 // : Kind(K), StartLoc(S), EndLoc(E) {}
Sean Callanan76264762010-04-02 22:27:05 +0000172
173 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
174 Kind = o.Kind;
175 StartLoc = o.StartLoc;
176 EndLoc = o.EndLoc;
177 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000178 case CondCode:
179 CC = o.CC;
180 break;
Sean Callanan76264762010-04-02 22:27:05 +0000181 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000182 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000183 break;
184 case Register:
185 Reg = o.Reg;
186 break;
187 case Immediate:
188 Imm = o.Imm;
189 break;
190 case Memory:
191 Mem = o.Mem;
192 break;
193 }
194 }
195
196 /// getStartLoc - Get the location of the first token of this operand.
197 SMLoc getStartLoc() const { return StartLoc; }
198 /// getEndLoc - Get the location of the last token of this operand.
199 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000200
Daniel Dunbar8462b302010-08-11 06:36:53 +0000201 ARMCC::CondCodes getCondCode() const {
202 assert(Kind == CondCode && "Invalid access!");
203 return CC.Val;
204 }
205
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000206 StringRef getToken() const {
207 assert(Kind == Token && "Invalid access!");
208 return StringRef(Tok.Data, Tok.Length);
209 }
210
211 unsigned getReg() const {
212 assert(Kind == Register && "Invalid access!");
213 return Reg.RegNum;
214 }
215
Kevin Enderbycfe07242009-10-13 22:19:02 +0000216 const MCExpr *getImm() const {
217 assert(Kind == Immediate && "Invalid access!");
218 return Imm.Val;
219 }
220
Daniel Dunbar8462b302010-08-11 06:36:53 +0000221 bool isCondCode() const { return Kind == CondCode; }
222
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000223 bool isImm() const { return Kind == Immediate; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000224
225 bool isReg() const { return Kind == Register; }
226
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000227 bool isToken() const {return Kind == Token; }
228
229 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
230 // Add as immediates when possible.
231 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
232 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
233 else
234 Inst.addOperand(MCOperand::CreateExpr(Expr));
235 }
236
Daniel Dunbar8462b302010-08-11 06:36:53 +0000237 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000238 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000239 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000240 // FIXME: What belongs here?
241 Inst.addOperand(MCOperand::CreateReg(0));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000242 }
243
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000244 void addRegOperands(MCInst &Inst, unsigned N) const {
245 assert(N == 1 && "Invalid number of operands!");
246 Inst.addOperand(MCOperand::CreateReg(getReg()));
247 }
248
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000249 void addImmOperands(MCInst &Inst, unsigned N) const {
250 assert(N == 1 && "Invalid number of operands!");
251 addExpr(Inst, getImm());
252 }
253
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000254 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000255
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000256 static void CreateCondCode(OwningPtr<ARMOperand> &Op, ARMCC::CondCodes CC,
257 SMLoc S) {
258 Op.reset(new ARMOperand);
259 Op->Kind = CondCode;
260 Op->CC.Val = CC;
261 Op->StartLoc = S;
262 Op->EndLoc = S;
263 }
264
Sean Callanan76264762010-04-02 22:27:05 +0000265 static void CreateToken(OwningPtr<ARMOperand> &Op, StringRef Str,
266 SMLoc S) {
267 Op.reset(new ARMOperand);
268 Op->Kind = Token;
269 Op->Tok.Data = Str.data();
270 Op->Tok.Length = Str.size();
271 Op->StartLoc = S;
272 Op->EndLoc = S;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000273 }
274
Sean Callanan76264762010-04-02 22:27:05 +0000275 static void CreateReg(OwningPtr<ARMOperand> &Op, unsigned RegNum,
276 bool Writeback, SMLoc S, SMLoc E) {
277 Op.reset(new ARMOperand);
278 Op->Kind = Register;
279 Op->Reg.RegNum = RegNum;
280 Op->Reg.Writeback = Writeback;
281
282 Op->StartLoc = S;
283 Op->EndLoc = E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000284 }
285
Sean Callanan76264762010-04-02 22:27:05 +0000286 static void CreateImm(OwningPtr<ARMOperand> &Op, const MCExpr *Val,
287 SMLoc S, SMLoc E) {
288 Op.reset(new ARMOperand);
289 Op->Kind = Immediate;
290 Op->Imm.Val = Val;
291
292 Op->StartLoc = S;
293 Op->EndLoc = E;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000294 }
295
Sean Callanan76264762010-04-02 22:27:05 +0000296 static void CreateMem(OwningPtr<ARMOperand> &Op,
297 unsigned BaseRegNum, bool OffsetIsReg,
298 const MCExpr *Offset, unsigned OffsetRegNum,
299 bool OffsetRegShifted, enum ShiftType ShiftType,
300 const MCExpr *ShiftAmount, bool Preindexed,
301 bool Postindexed, bool Negative, bool Writeback,
302 SMLoc S, SMLoc E) {
303 Op.reset(new ARMOperand);
304 Op->Kind = Memory;
305 Op->Mem.BaseRegNum = BaseRegNum;
306 Op->Mem.OffsetIsReg = OffsetIsReg;
307 Op->Mem.Offset = Offset;
308 Op->Mem.OffsetRegNum = OffsetRegNum;
309 Op->Mem.OffsetRegShifted = OffsetRegShifted;
310 Op->Mem.ShiftType = ShiftType;
311 Op->Mem.ShiftAmount = ShiftAmount;
312 Op->Mem.Preindexed = Preindexed;
313 Op->Mem.Postindexed = Postindexed;
314 Op->Mem.Negative = Negative;
315 Op->Mem.Writeback = Writeback;
316
317 Op->StartLoc = S;
318 Op->EndLoc = E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000319 }
320};
321
322} // end anonymous namespace.
323
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000324void ARMOperand::dump(raw_ostream &OS) const {
325 switch (Kind) {
326 case CondCode:
327 OS << ARMCondCodeToString(getCondCode());
328 break;
329 case Immediate:
330 getImm()->print(OS);
331 break;
332 case Memory:
333 OS << "<memory>";
334 break;
335 case Register:
336 OS << "<register " << getReg() << ">";
337 break;
338 case Token:
339 OS << "'" << getToken() << "'";
340 break;
341 }
342}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000343
344/// @name Auto-generated Match Functions
345/// {
346
347static unsigned MatchRegisterName(StringRef Name);
348
349/// }
350
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000351/// Try to parse a register name. The token must be an Identifier when called,
352/// and if it is a register name a Reg operand is created, the token is eaten
353/// and false is returned. Else true is returned and no token is eaten.
354/// TODO this is likely to change to allow different register types and or to
355/// parse for a specific register type.
Sean Callanan76264762010-04-02 22:27:05 +0000356bool ARMAsmParser::MaybeParseRegister
357 (OwningPtr<ARMOperand> &Op, bool ParseWriteBack) {
358 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000359 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000360 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
361
362 // FIXME: Validate register for the current architecture; we have to do
363 // validation later, so maybe there is no need for this here.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000364 int RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000365
366 RegNum = MatchRegisterName(Tok.getString());
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000367 if (RegNum == -1)
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000368 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000369
370 S = Tok.getLoc();
371
Sean Callananb9a25b72010-01-19 20:27:46 +0000372 Parser.Lex(); // Eat identifier token.
Sean Callanan76264762010-04-02 22:27:05 +0000373
374 E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000375
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000376 bool Writeback = false;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000377 if (ParseWriteBack) {
Sean Callanan18b83232010-01-19 21:44:56 +0000378 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000379 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000380 E = ExclaimTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000381 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000382 Parser.Lex(); // Eat exclaim token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000383 }
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000384 }
385
Sean Callanan76264762010-04-02 22:27:05 +0000386 ARMOperand::CreateReg(Op, RegNum, Writeback, S, E);
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000387
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000388 return false;
389}
390
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000391/// Parse a register list, return false if successful else return true or an
392/// error. The first token must be a '{' when called.
Sean Callanan76264762010-04-02 22:27:05 +0000393bool ARMAsmParser::ParseRegisterList(OwningPtr<ARMOperand> &Op) {
394 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000395 assert(Parser.getTok().is(AsmToken::LCurly) &&
Kevin Enderbycfe07242009-10-13 22:19:02 +0000396 "Token is not an Left Curly Brace");
Sean Callanan76264762010-04-02 22:27:05 +0000397 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000398 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000399
Sean Callanan18b83232010-01-19 21:44:56 +0000400 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000401 SMLoc RegLoc = RegTok.getLoc();
402 if (RegTok.isNot(AsmToken::Identifier))
403 return Error(RegLoc, "register expected");
404 int RegNum = MatchRegisterName(RegTok.getString());
405 if (RegNum == -1)
406 return Error(RegLoc, "register expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000407 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000408 unsigned RegList = 1 << RegNum;
409
410 int HighRegNum = RegNum;
411 // TODO ranges like "{Rn-Rm}"
Sean Callanan18b83232010-01-19 21:44:56 +0000412 while (Parser.getTok().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000413 Parser.Lex(); // Eat comma token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000414
Sean Callanan18b83232010-01-19 21:44:56 +0000415 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000416 SMLoc RegLoc = RegTok.getLoc();
417 if (RegTok.isNot(AsmToken::Identifier))
418 return Error(RegLoc, "register expected");
419 int RegNum = MatchRegisterName(RegTok.getString());
420 if (RegNum == -1)
421 return Error(RegLoc, "register expected");
422
423 if (RegList & (1 << RegNum))
424 Warning(RegLoc, "register duplicated in register list");
425 else if (RegNum <= HighRegNum)
426 Warning(RegLoc, "register not in ascending order in register list");
427 RegList |= 1 << RegNum;
428 HighRegNum = RegNum;
429
Sean Callananb9a25b72010-01-19 20:27:46 +0000430 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000431 }
Sean Callanan18b83232010-01-19 21:44:56 +0000432 const AsmToken &RCurlyTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000433 if (RCurlyTok.isNot(AsmToken::RCurly))
434 return Error(RCurlyTok.getLoc(), "'}' expected");
Sean Callanan76264762010-04-02 22:27:05 +0000435 E = RCurlyTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000436 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000437
438 return false;
439}
440
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000441/// Parse an arm memory expression, return false if successful else return true
442/// or an error. The first token must be a '[' when called.
443/// TODO Only preindexing and postindexing addressing are started, unindexed
444/// with option, etc are still to do.
Sean Callanan76264762010-04-02 22:27:05 +0000445bool ARMAsmParser::ParseMemory(OwningPtr<ARMOperand> &Op) {
446 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000447 assert(Parser.getTok().is(AsmToken::LBrac) &&
Kevin Enderby6bd266e2009-10-12 22:51:49 +0000448 "Token is not an Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000449 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000450 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000451
Sean Callanan18b83232010-01-19 21:44:56 +0000452 const AsmToken &BaseRegTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000453 if (BaseRegTok.isNot(AsmToken::Identifier))
454 return Error(BaseRegTok.getLoc(), "register expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000455 if (MaybeParseRegister(Op, false))
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000456 return Error(BaseRegTok.getLoc(), "register expected");
Sean Callanan76264762010-04-02 22:27:05 +0000457 int BaseRegNum = Op->getReg();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000458
459 bool Preindexed = false;
460 bool Postindexed = false;
461 bool OffsetIsReg = false;
462 bool Negative = false;
463 bool Writeback = false;
464
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000465 // First look for preindexed address forms, that is after the "[Rn" we now
466 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000467 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000468 if (Tok.is(AsmToken::Comma)) {
469 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000470 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000471 int OffsetRegNum;
472 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000473 enum ShiftType ShiftType;
474 const MCExpr *ShiftAmount;
475 const MCExpr *Offset;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000476 if(ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000477 Offset, OffsetIsReg, OffsetRegNum, E))
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000478 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000479 const AsmToken &RBracTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000480 if (RBracTok.isNot(AsmToken::RBrac))
481 return Error(RBracTok.getLoc(), "']' expected");
Sean Callanan76264762010-04-02 22:27:05 +0000482 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000483 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000484
Sean Callanan18b83232010-01-19 21:44:56 +0000485 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000486 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000487 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000488 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000489 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000490 }
Sean Callanan76264762010-04-02 22:27:05 +0000491 ARMOperand::CreateMem(Op, BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
492 OffsetRegShifted, ShiftType, ShiftAmount,
493 Preindexed, Postindexed, Negative, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000494 return false;
495 }
496 // The "[Rn" we have so far was not followed by a comma.
497 else if (Tok.is(AsmToken::RBrac)) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000498 // This is a post indexing addressing forms, that is a ']' follows after
499 // the "[Rn".
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000500 Postindexed = true;
501 Writeback = true;
Sean Callanan76264762010-04-02 22:27:05 +0000502 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000503 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000504
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000505 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000506 bool OffsetRegShifted = false;
507 enum ShiftType ShiftType;
508 const MCExpr *ShiftAmount;
509 const MCExpr *Offset;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000510
Sean Callanan18b83232010-01-19 21:44:56 +0000511 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000512 if (NextTok.isNot(AsmToken::EndOfStatement)) {
513 if (NextTok.isNot(AsmToken::Comma))
Duncan Sands34727662010-07-12 08:16:59 +0000514 return Error(NextTok.getLoc(), "',' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000515 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000516 if(ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Sean Callanan76264762010-04-02 22:27:05 +0000517 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
518 E))
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000519 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000520 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000521
Sean Callanan76264762010-04-02 22:27:05 +0000522 ARMOperand::CreateMem(Op, BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
523 OffsetRegShifted, ShiftType, ShiftAmount,
524 Preindexed, Postindexed, Negative, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000525 return false;
526 }
527
528 return true;
529}
530
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000531/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
532/// we will parse the following (were +/- means that a plus or minus is
533/// optional):
534/// +/-Rm
535/// +/-Rm, shift
536/// #offset
537/// we return false on success or an error otherwise.
538bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000539 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000540 enum ShiftType &ShiftType,
541 const MCExpr *&ShiftAmount,
542 const MCExpr *&Offset,
543 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000544 int &OffsetRegNum,
545 SMLoc &E) {
546 OwningPtr<ARMOperand> Op;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000547 Negative = false;
548 OffsetRegShifted = false;
549 OffsetIsReg = false;
550 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000551 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000552 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000553 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000554 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000555 else if (NextTok.is(AsmToken::Minus)) {
556 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000557 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000558 }
559 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000560 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000561 if (OffsetRegTok.is(AsmToken::Identifier)) {
562 OffsetIsReg = !MaybeParseRegister(Op, false);
Sean Callanan76264762010-04-02 22:27:05 +0000563 if (OffsetIsReg) {
564 E = Op->getEndLoc();
565 OffsetRegNum = Op->getReg();
566 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000567 }
568 // If we parsed a register as the offset then their can be a shift after that
569 if (OffsetRegNum != -1) {
570 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000571 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000572 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000573 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000574
Sean Callanan18b83232010-01-19 21:44:56 +0000575 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000576 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000577 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000578 OffsetRegShifted = true;
579 }
580 }
581 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
582 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000583 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000584 if (HashTok.isNot(AsmToken::Hash))
585 return Error(HashTok.getLoc(), "'#' expected");
Sean Callanan76264762010-04-02 22:27:05 +0000586
Sean Callananb9a25b72010-01-19 20:27:46 +0000587 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000588
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000589 if (getParser().ParseExpression(Offset))
590 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000591 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000592 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000593 return false;
594}
595
596/// ParseShift as one of these two:
597/// ( lsl | lsr | asr | ror ) , # shift_amount
598/// rrx
599/// and returns true if it parses a shift otherwise it returns false.
Sean Callanan76264762010-04-02 22:27:05 +0000600bool ARMAsmParser::ParseShift(ShiftType &St,
601 const MCExpr *&ShiftAmount,
602 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000603 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000604 if (Tok.isNot(AsmToken::Identifier))
605 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000606 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000607 if (ShiftName == "lsl" || ShiftName == "LSL")
608 St = Lsl;
609 else if (ShiftName == "lsr" || ShiftName == "LSR")
610 St = Lsr;
611 else if (ShiftName == "asr" || ShiftName == "ASR")
612 St = Asr;
613 else if (ShiftName == "ror" || ShiftName == "ROR")
614 St = Ror;
615 else if (ShiftName == "rrx" || ShiftName == "RRX")
616 St = Rrx;
617 else
618 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000619 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000620
621 // Rrx stands alone.
622 if (St == Rrx)
623 return false;
624
625 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000626 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000627 if (HashTok.isNot(AsmToken::Hash))
628 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000629 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000630
631 if (getParser().ParseExpression(ShiftAmount))
632 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000633
634 return false;
635}
636
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000637/// Parse a arm instruction operand. For now this parses the operand regardless
638/// of the mnemonic.
Sean Callanan76264762010-04-02 22:27:05 +0000639bool ARMAsmParser::ParseOperand(OwningPtr<ARMOperand> &Op) {
640 SMLoc S, E;
641
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000642 switch (getLexer().getKind()) {
643 case AsmToken::Identifier:
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000644 if (!MaybeParseRegister(Op, true))
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000645 return false;
Kevin Enderby515d5092009-10-15 20:48:48 +0000646 // This was not a register so parse other operands that start with an
647 // identifier (like labels) as expressions and create them as immediates.
648 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000649 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000650 if (getParser().ParseExpression(IdVal))
651 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000652 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
653 ARMOperand::CreateImm(Op, IdVal, S, E);
Kevin Enderby515d5092009-10-15 20:48:48 +0000654 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000655 case AsmToken::LBrac:
Kevin Enderby515d5092009-10-15 20:48:48 +0000656 return ParseMemory(Op);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000657 case AsmToken::LCurly:
Kevin Enderby515d5092009-10-15 20:48:48 +0000658 return ParseRegisterList(Op);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000659 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000660 // #42 -> immediate.
661 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000662 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000663 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000664 const MCExpr *ImmVal;
665 if (getParser().ParseExpression(ImmVal))
Kevin Enderbycfe07242009-10-13 22:19:02 +0000666 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000667 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
668 ARMOperand::CreateImm(Op, ImmVal, S, E);
Kevin Enderbycfe07242009-10-13 22:19:02 +0000669 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000670 default:
Sean Callanan18b83232010-01-19 21:44:56 +0000671 return Error(Parser.getTok().getLoc(), "unexpected token in operand");
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000672 }
673}
674
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000675/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000676bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000677 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000678 OwningPtr<ARMOperand> Op;
Daniel Dunbar5747b132010-08-11 06:37:16 +0000679
680 // Create the leading tokens for the mnemonic, split by '.' characters.
681 size_t Start = 0, Next = Name.find('.');
682 StringRef Head = Name.slice(Start, Next);
683
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000684 // Determine the predicate, if any.
685 //
686 // FIXME: We need a way to check whether a prefix supports predication,
687 // otherwise we will end up with an ambiguity for instructions that happen to
688 // end with a predicate name.
689 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
690 .Case("eq", ARMCC::EQ)
691 .Case("ne", ARMCC::NE)
692 .Case("hs", ARMCC::HS)
693 .Case("lo", ARMCC::LO)
694 .Case("mi", ARMCC::MI)
695 .Case("pl", ARMCC::PL)
696 .Case("vs", ARMCC::VS)
697 .Case("vc", ARMCC::VC)
698 .Case("hi", ARMCC::HI)
699 .Case("ls", ARMCC::LS)
700 .Case("ge", ARMCC::GE)
701 .Case("lt", ARMCC::LT)
702 .Case("gt", ARMCC::GT)
703 .Case("le", ARMCC::LE)
704 .Case("al", ARMCC::AL)
705 .Default(~0U);
706 if (CC != ~0U) {
707 Head = Head.slice(0, Head.size() - 2);
708 } else
709 CC = ARMCC::AL;
710
Daniel Dunbar5747b132010-08-11 06:37:16 +0000711 ARMOperand::CreateToken(Op, Head, NameLoc);
Sean Callanan76264762010-04-02 22:27:05 +0000712 Operands.push_back(Op.take());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000713
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000714 ARMOperand::CreateCondCode(Op, ARMCC::CondCodes(CC), NameLoc);
715 Operands.push_back(Op.take());
716
717 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000718 while (Next != StringRef::npos) {
719 Start = Next;
720 Next = Name.find('.', Start + 1);
721 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000722
Daniel Dunbar5747b132010-08-11 06:37:16 +0000723 ARMOperand::CreateToken(Op, Head, NameLoc);
724 Operands.push_back(Op.take());
725 }
726
727 // Read the remaining operands.
728 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000729 // Read the first operand.
Sean Callanan76264762010-04-02 22:27:05 +0000730 OwningPtr<ARMOperand> Op;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000731 if (ParseOperand(Op)) {
732 Parser.EatToEndOfStatement();
733 return true;
734 }
Sean Callanan76264762010-04-02 22:27:05 +0000735 Operands.push_back(Op.take());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000736
737 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000738 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000739
740 // Parse and remember the operand.
Chris Lattnercbf8a982010-09-11 16:18:25 +0000741 if (ParseOperand(Op)) {
742 Parser.EatToEndOfStatement();
743 return true;
744 }
Sean Callanan76264762010-04-02 22:27:05 +0000745 Operands.push_back(Op.take());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000746 }
747 }
Chris Lattner34e53142010-09-08 05:10:46 +0000748
Chris Lattnercbf8a982010-09-11 16:18:25 +0000749 if (getLexer().isNot(AsmToken::EndOfStatement)) {
750 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000751 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000752 }
Chris Lattner34e53142010-09-08 05:10:46 +0000753 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000754 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000755}
756
Kevin Enderby515d5092009-10-15 20:48:48 +0000757/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000758bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
759 StringRef IDVal = DirectiveID.getIdentifier();
760 if (IDVal == ".word")
761 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000762 else if (IDVal == ".thumb")
763 return ParseDirectiveThumb(DirectiveID.getLoc());
764 else if (IDVal == ".thumb_func")
765 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
766 else if (IDVal == ".code")
767 return ParseDirectiveCode(DirectiveID.getLoc());
768 else if (IDVal == ".syntax")
769 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000770 return true;
771}
772
773/// ParseDirectiveWord
774/// ::= .word [ expression (, expression)* ]
775bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
776 if (getLexer().isNot(AsmToken::EndOfStatement)) {
777 for (;;) {
778 const MCExpr *Value;
779 if (getParser().ParseExpression(Value))
780 return true;
781
Chris Lattneraaec2052010-01-19 19:46:13 +0000782 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000783
784 if (getLexer().is(AsmToken::EndOfStatement))
785 break;
786
787 // FIXME: Improve diagnostic.
788 if (getLexer().isNot(AsmToken::Comma))
789 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000790 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000791 }
792 }
793
Sean Callananb9a25b72010-01-19 20:27:46 +0000794 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000795 return false;
796}
797
Kevin Enderby515d5092009-10-15 20:48:48 +0000798/// ParseDirectiveThumb
799/// ::= .thumb
800bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
801 if (getLexer().isNot(AsmToken::EndOfStatement))
802 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000803 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000804
805 // TODO: set thumb mode
806 // TODO: tell the MC streamer the mode
807 // getParser().getStreamer().Emit???();
808 return false;
809}
810
811/// ParseDirectiveThumbFunc
812/// ::= .thumbfunc symbol_name
813bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000814 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000815 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
816 return Error(L, "unexpected token in .syntax directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000817 StringRef ATTRIBUTE_UNUSED SymbolName = Parser.getTok().getIdentifier();
Sean Callananb9a25b72010-01-19 20:27:46 +0000818 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000819
820 if (getLexer().isNot(AsmToken::EndOfStatement))
821 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000822 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000823
824 // TODO: mark symbol as a thumb symbol
825 // getParser().getStreamer().Emit???();
826 return false;
827}
828
829/// ParseDirectiveSyntax
830/// ::= .syntax unified | divided
831bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000832 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000833 if (Tok.isNot(AsmToken::Identifier))
834 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000835 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000836 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000837 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000838 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000839 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000840 else
841 return Error(L, "unrecognized syntax mode in .syntax directive");
842
843 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000844 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000845 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000846
847 // TODO tell the MC streamer the mode
848 // getParser().getStreamer().Emit???();
849 return false;
850}
851
852/// ParseDirectiveCode
853/// ::= .code 16 | 32
854bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000855 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000856 if (Tok.isNot(AsmToken::Integer))
857 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000858 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +0000859 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +0000860 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000861 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +0000862 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000863 else
864 return Error(L, "invalid operand to .code directive");
865
866 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000867 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000868 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000869
870 // TODO tell the MC streamer the mode
871 // getParser().getStreamer().Emit???();
872 return false;
873}
874
Sean Callanan90b70972010-04-07 20:29:34 +0000875extern "C" void LLVMInitializeARMAsmLexer();
876
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000877/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000878extern "C" void LLVMInitializeARMAsmParser() {
879 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
880 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +0000881 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000882}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000883
Chris Lattner0692ee62010-09-06 19:11:01 +0000884#define GET_REGISTER_MATCHER
885#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000886#include "ARMGenAsmMatcher.inc"