blob: c9597fb5e4007433ac0fa35445ff1eb1269bf840 [file] [log] [blame]
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001//===-- ARMAsmParser.cpp - Parse ARM assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ARM.h"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000011#include "ARMSubtarget.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000012#include "llvm/MC/MCParser/MCAsmLexer.h"
13#include "llvm/MC/MCParser/MCAsmParser.h"
14#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000015#include "llvm/MC/MCStreamer.h"
16#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCInst.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000018#include "llvm/Target/TargetRegistry.h"
19#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000020#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000021#include "llvm/Support/raw_ostream.h"
Sean Callanan76264762010-04-02 22:27:05 +000022#include "llvm/ADT/OwningPtr.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000023#include "llvm/ADT/SmallVector.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000024#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000025#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000026using namespace llvm;
27
28namespace {
29struct ARMOperand;
30
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000031// The shift types for register controlled shifts in arm memory addressing
32enum ShiftType {
33 Lsl,
34 Lsr,
35 Asr,
36 Ror,
37 Rrx
38};
39
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000040class ARMAsmParser : public TargetAsmParser {
41 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000042 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000043
44private:
45 MCAsmParser &getParser() const { return Parser; }
46
47 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
48
49 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
50
51 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
52
Sean Callanan76264762010-04-02 22:27:05 +000053 bool MaybeParseRegister(OwningPtr<ARMOperand> &Op, bool ParseWriteBack);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000054
Sean Callanan76264762010-04-02 22:27:05 +000055 bool ParseRegisterList(OwningPtr<ARMOperand> &Op);
Kevin Enderbyd7894f12009-10-09 21:12:28 +000056
Sean Callanan76264762010-04-02 22:27:05 +000057 bool ParseMemory(OwningPtr<ARMOperand> &Op);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000058
Kevin Enderby9c41fa82009-10-30 22:55:57 +000059 bool ParseMemoryOffsetReg(bool &Negative,
60 bool &OffsetRegShifted,
61 enum ShiftType &ShiftType,
62 const MCExpr *&ShiftAmount,
63 const MCExpr *&Offset,
64 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000065 int &OffsetRegNum,
66 SMLoc &E);
Kevin Enderby9c41fa82009-10-30 22:55:57 +000067
Sean Callanan76264762010-04-02 22:27:05 +000068 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000069
Sean Callanan76264762010-04-02 22:27:05 +000070 bool ParseOperand(OwningPtr<ARMOperand> &Op);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000071
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000072 bool ParseDirectiveWord(unsigned Size, SMLoc L);
73
Kevin Enderby515d5092009-10-15 20:48:48 +000074 bool ParseDirectiveThumb(SMLoc L);
75
76 bool ParseDirectiveThumbFunc(SMLoc L);
77
78 bool ParseDirectiveCode(SMLoc L);
79
80 bool ParseDirectiveSyntax(SMLoc L);
81
Chris Lattner7036f8b2010-09-29 01:42:58 +000082 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000083 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
84 MCStreamer &Out) {
Chris Lattner7036f8b2010-09-29 01:42:58 +000085 MCInst Inst;
Chris Lattnerce4a3352010-09-06 22:11:18 +000086 unsigned ErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +000087 if (MatchInstructionImpl(Operands, Inst, ErrorInfo) == Match_Success) {
88 Out.EmitInstruction(Inst);
Daniel Dunbarf1e29d42010-08-12 00:55:38 +000089 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +000090 }
Daniel Dunbarf1e29d42010-08-12 00:55:38 +000091
92 // FIXME: We should give nicer diagnostics about the exact failure.
93 Error(IDLoc, "unrecognized instruction");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +000094 return true;
Daniel Dunbar4f98f832010-08-12 00:55:32 +000095 }
96
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000097 /// @name Auto-generated Match Functions
98 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000099
Chris Lattner0692ee62010-09-06 19:11:01 +0000100#define GET_ASSEMBLER_HEADER
101#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000102
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000103 /// }
104
105
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000106public:
Daniel Dunbard73ada72010-07-19 00:33:49 +0000107 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
108 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {}
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000109
Benjamin Kramer38e59892010-07-14 22:38:02 +0000110 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000111 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000112
113 virtual bool ParseDirective(AsmToken DirectiveID);
114};
115
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000116/// ARMOperand - Instances of this class represent a parsed ARM machine
117/// instruction.
Chris Lattner76593892010-01-14 21:21:40 +0000118struct ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000119private:
120 ARMOperand() {}
121public:
122 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000123 CondCode,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000124 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000125 Memory,
126 Register,
127 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000128 } Kind;
129
Sean Callanan76264762010-04-02 22:27:05 +0000130 SMLoc StartLoc, EndLoc;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000131
132 union {
133 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000134 ARMCC::CondCodes Val;
135 } CC;
136
137 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000138 const char *Data;
139 unsigned Length;
140 } Tok;
141
142 struct {
143 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000144 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000145 } Reg;
146
Kevin Enderbycfe07242009-10-13 22:19:02 +0000147 struct {
148 const MCExpr *Val;
149 } Imm;
Sean Callanan76264762010-04-02 22:27:05 +0000150
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000151 // This is for all forms of ARM address expressions
152 struct {
153 unsigned BaseRegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000154 unsigned OffsetRegNum; // used when OffsetIsReg is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000155 const MCExpr *Offset; // used when OffsetIsReg is false
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000156 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000157 enum ShiftType ShiftType; // used when OffsetRegShifted is true
158 unsigned
159 OffsetRegShifted : 1, // only used when OffsetIsReg is true
160 Preindexed : 1,
161 Postindexed : 1,
162 OffsetIsReg : 1,
163 Negative : 1, // only used when OffsetIsReg is true
164 Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000165 } Mem;
166
167 };
Sean Callanan76264762010-04-02 22:27:05 +0000168
Chris Lattner14ab39e2010-09-01 16:04:34 +0000169 //ARMOperand(KindTy K, SMLoc S, SMLoc E)
170 // : Kind(K), StartLoc(S), EndLoc(E) {}
Sean Callanan76264762010-04-02 22:27:05 +0000171
172 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
173 Kind = o.Kind;
174 StartLoc = o.StartLoc;
175 EndLoc = o.EndLoc;
176 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000177 case CondCode:
178 CC = o.CC;
179 break;
Sean Callanan76264762010-04-02 22:27:05 +0000180 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000181 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000182 break;
183 case Register:
184 Reg = o.Reg;
185 break;
186 case Immediate:
187 Imm = o.Imm;
188 break;
189 case Memory:
190 Mem = o.Mem;
191 break;
192 }
193 }
194
195 /// getStartLoc - Get the location of the first token of this operand.
196 SMLoc getStartLoc() const { return StartLoc; }
197 /// getEndLoc - Get the location of the last token of this operand.
198 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000199
Daniel Dunbar8462b302010-08-11 06:36:53 +0000200 ARMCC::CondCodes getCondCode() const {
201 assert(Kind == CondCode && "Invalid access!");
202 return CC.Val;
203 }
204
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000205 StringRef getToken() const {
206 assert(Kind == Token && "Invalid access!");
207 return StringRef(Tok.Data, Tok.Length);
208 }
209
210 unsigned getReg() const {
211 assert(Kind == Register && "Invalid access!");
212 return Reg.RegNum;
213 }
214
Kevin Enderbycfe07242009-10-13 22:19:02 +0000215 const MCExpr *getImm() const {
216 assert(Kind == Immediate && "Invalid access!");
217 return Imm.Val;
218 }
219
Daniel Dunbar8462b302010-08-11 06:36:53 +0000220 bool isCondCode() const { return Kind == CondCode; }
221
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000222 bool isImm() const { return Kind == Immediate; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000223
224 bool isReg() const { return Kind == Register; }
225
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000226 bool isToken() const {return Kind == Token; }
227
228 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
229 // Add as immediates when possible.
230 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
231 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
232 else
233 Inst.addOperand(MCOperand::CreateExpr(Expr));
234 }
235
Daniel Dunbar8462b302010-08-11 06:36:53 +0000236 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000237 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000238 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000239 // FIXME: What belongs here?
240 Inst.addOperand(MCOperand::CreateReg(0));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000241 }
242
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000243 void addRegOperands(MCInst &Inst, unsigned N) const {
244 assert(N == 1 && "Invalid number of operands!");
245 Inst.addOperand(MCOperand::CreateReg(getReg()));
246 }
247
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000248 void addImmOperands(MCInst &Inst, unsigned N) const {
249 assert(N == 1 && "Invalid number of operands!");
250 addExpr(Inst, getImm());
251 }
252
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000253 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000254
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000255 static void CreateCondCode(OwningPtr<ARMOperand> &Op, ARMCC::CondCodes CC,
256 SMLoc S) {
257 Op.reset(new ARMOperand);
258 Op->Kind = CondCode;
259 Op->CC.Val = CC;
260 Op->StartLoc = S;
261 Op->EndLoc = S;
262 }
263
Sean Callanan76264762010-04-02 22:27:05 +0000264 static void CreateToken(OwningPtr<ARMOperand> &Op, StringRef Str,
265 SMLoc S) {
266 Op.reset(new ARMOperand);
267 Op->Kind = Token;
268 Op->Tok.Data = Str.data();
269 Op->Tok.Length = Str.size();
270 Op->StartLoc = S;
271 Op->EndLoc = S;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000272 }
273
Sean Callanan76264762010-04-02 22:27:05 +0000274 static void CreateReg(OwningPtr<ARMOperand> &Op, unsigned RegNum,
275 bool Writeback, SMLoc S, SMLoc E) {
276 Op.reset(new ARMOperand);
277 Op->Kind = Register;
278 Op->Reg.RegNum = RegNum;
279 Op->Reg.Writeback = Writeback;
280
281 Op->StartLoc = S;
282 Op->EndLoc = E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000283 }
284
Sean Callanan76264762010-04-02 22:27:05 +0000285 static void CreateImm(OwningPtr<ARMOperand> &Op, const MCExpr *Val,
286 SMLoc S, SMLoc E) {
287 Op.reset(new ARMOperand);
288 Op->Kind = Immediate;
289 Op->Imm.Val = Val;
290
291 Op->StartLoc = S;
292 Op->EndLoc = E;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000293 }
294
Sean Callanan76264762010-04-02 22:27:05 +0000295 static void CreateMem(OwningPtr<ARMOperand> &Op,
296 unsigned BaseRegNum, bool OffsetIsReg,
297 const MCExpr *Offset, unsigned OffsetRegNum,
298 bool OffsetRegShifted, enum ShiftType ShiftType,
299 const MCExpr *ShiftAmount, bool Preindexed,
300 bool Postindexed, bool Negative, bool Writeback,
301 SMLoc S, SMLoc E) {
302 Op.reset(new ARMOperand);
303 Op->Kind = Memory;
304 Op->Mem.BaseRegNum = BaseRegNum;
305 Op->Mem.OffsetIsReg = OffsetIsReg;
306 Op->Mem.Offset = Offset;
307 Op->Mem.OffsetRegNum = OffsetRegNum;
308 Op->Mem.OffsetRegShifted = OffsetRegShifted;
309 Op->Mem.ShiftType = ShiftType;
310 Op->Mem.ShiftAmount = ShiftAmount;
311 Op->Mem.Preindexed = Preindexed;
312 Op->Mem.Postindexed = Postindexed;
313 Op->Mem.Negative = Negative;
314 Op->Mem.Writeback = Writeback;
315
316 Op->StartLoc = S;
317 Op->EndLoc = E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000318 }
319};
320
321} // end anonymous namespace.
322
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000323void ARMOperand::dump(raw_ostream &OS) const {
324 switch (Kind) {
325 case CondCode:
326 OS << ARMCondCodeToString(getCondCode());
327 break;
328 case Immediate:
329 getImm()->print(OS);
330 break;
331 case Memory:
332 OS << "<memory>";
333 break;
334 case Register:
335 OS << "<register " << getReg() << ">";
336 break;
337 case Token:
338 OS << "'" << getToken() << "'";
339 break;
340 }
341}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000342
343/// @name Auto-generated Match Functions
344/// {
345
346static unsigned MatchRegisterName(StringRef Name);
347
348/// }
349
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000350/// Try to parse a register name. The token must be an Identifier when called,
351/// and if it is a register name a Reg operand is created, the token is eaten
352/// and false is returned. Else true is returned and no token is eaten.
353/// TODO this is likely to change to allow different register types and or to
354/// parse for a specific register type.
Sean Callanan76264762010-04-02 22:27:05 +0000355bool ARMAsmParser::MaybeParseRegister
356 (OwningPtr<ARMOperand> &Op, bool ParseWriteBack) {
357 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000358 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000359 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
360
361 // FIXME: Validate register for the current architecture; we have to do
362 // validation later, so maybe there is no need for this here.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000363 int RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000364
365 RegNum = MatchRegisterName(Tok.getString());
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000366 if (RegNum == -1)
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000367 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000368
369 S = Tok.getLoc();
370
Sean Callananb9a25b72010-01-19 20:27:46 +0000371 Parser.Lex(); // Eat identifier token.
Sean Callanan76264762010-04-02 22:27:05 +0000372
373 E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000374
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000375 bool Writeback = false;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000376 if (ParseWriteBack) {
Sean Callanan18b83232010-01-19 21:44:56 +0000377 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000378 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000379 E = ExclaimTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000380 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000381 Parser.Lex(); // Eat exclaim token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000382 }
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000383 }
384
Sean Callanan76264762010-04-02 22:27:05 +0000385 ARMOperand::CreateReg(Op, RegNum, Writeback, S, E);
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000386
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000387 return false;
388}
389
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000390/// Parse a register list, return false if successful else return true or an
391/// error. The first token must be a '{' when called.
Sean Callanan76264762010-04-02 22:27:05 +0000392bool ARMAsmParser::ParseRegisterList(OwningPtr<ARMOperand> &Op) {
393 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000394 assert(Parser.getTok().is(AsmToken::LCurly) &&
Kevin Enderbycfe07242009-10-13 22:19:02 +0000395 "Token is not an Left Curly Brace");
Sean Callanan76264762010-04-02 22:27:05 +0000396 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000397 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000398
Sean Callanan18b83232010-01-19 21:44:56 +0000399 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000400 SMLoc RegLoc = RegTok.getLoc();
401 if (RegTok.isNot(AsmToken::Identifier))
402 return Error(RegLoc, "register expected");
403 int RegNum = MatchRegisterName(RegTok.getString());
404 if (RegNum == -1)
405 return Error(RegLoc, "register expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000406 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000407 unsigned RegList = 1 << RegNum;
408
409 int HighRegNum = RegNum;
410 // TODO ranges like "{Rn-Rm}"
Sean Callanan18b83232010-01-19 21:44:56 +0000411 while (Parser.getTok().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000412 Parser.Lex(); // Eat comma token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000413
Sean Callanan18b83232010-01-19 21:44:56 +0000414 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000415 SMLoc RegLoc = RegTok.getLoc();
416 if (RegTok.isNot(AsmToken::Identifier))
417 return Error(RegLoc, "register expected");
418 int RegNum = MatchRegisterName(RegTok.getString());
419 if (RegNum == -1)
420 return Error(RegLoc, "register expected");
421
422 if (RegList & (1 << RegNum))
423 Warning(RegLoc, "register duplicated in register list");
424 else if (RegNum <= HighRegNum)
425 Warning(RegLoc, "register not in ascending order in register list");
426 RegList |= 1 << RegNum;
427 HighRegNum = RegNum;
428
Sean Callananb9a25b72010-01-19 20:27:46 +0000429 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000430 }
Sean Callanan18b83232010-01-19 21:44:56 +0000431 const AsmToken &RCurlyTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000432 if (RCurlyTok.isNot(AsmToken::RCurly))
433 return Error(RCurlyTok.getLoc(), "'}' expected");
Sean Callanan76264762010-04-02 22:27:05 +0000434 E = RCurlyTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000435 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000436
437 return false;
438}
439
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000440/// Parse an arm memory expression, return false if successful else return true
441/// or an error. The first token must be a '[' when called.
442/// TODO Only preindexing and postindexing addressing are started, unindexed
443/// with option, etc are still to do.
Sean Callanan76264762010-04-02 22:27:05 +0000444bool ARMAsmParser::ParseMemory(OwningPtr<ARMOperand> &Op) {
445 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000446 assert(Parser.getTok().is(AsmToken::LBrac) &&
Kevin Enderby6bd266e2009-10-12 22:51:49 +0000447 "Token is not an Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000448 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000449 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000450
Sean Callanan18b83232010-01-19 21:44:56 +0000451 const AsmToken &BaseRegTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000452 if (BaseRegTok.isNot(AsmToken::Identifier))
453 return Error(BaseRegTok.getLoc(), "register expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000454 if (MaybeParseRegister(Op, false))
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000455 return Error(BaseRegTok.getLoc(), "register expected");
Sean Callanan76264762010-04-02 22:27:05 +0000456 int BaseRegNum = Op->getReg();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000457
458 bool Preindexed = false;
459 bool Postindexed = false;
460 bool OffsetIsReg = false;
461 bool Negative = false;
462 bool Writeback = false;
463
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000464 // First look for preindexed address forms, that is after the "[Rn" we now
465 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000466 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000467 if (Tok.is(AsmToken::Comma)) {
468 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000469 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000470 int OffsetRegNum;
471 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000472 enum ShiftType ShiftType;
473 const MCExpr *ShiftAmount;
474 const MCExpr *Offset;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000475 if(ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000476 Offset, OffsetIsReg, OffsetRegNum, E))
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000477 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000478 const AsmToken &RBracTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000479 if (RBracTok.isNot(AsmToken::RBrac))
480 return Error(RBracTok.getLoc(), "']' expected");
Sean Callanan76264762010-04-02 22:27:05 +0000481 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000482 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000483
Sean Callanan18b83232010-01-19 21:44:56 +0000484 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000485 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000486 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000487 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000488 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000489 }
Sean Callanan76264762010-04-02 22:27:05 +0000490 ARMOperand::CreateMem(Op, BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
491 OffsetRegShifted, ShiftType, ShiftAmount,
492 Preindexed, Postindexed, Negative, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000493 return false;
494 }
495 // The "[Rn" we have so far was not followed by a comma.
496 else if (Tok.is(AsmToken::RBrac)) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000497 // This is a post indexing addressing forms, that is a ']' follows after
498 // the "[Rn".
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000499 Postindexed = true;
500 Writeback = true;
Sean Callanan76264762010-04-02 22:27:05 +0000501 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000502 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000503
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000504 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000505 bool OffsetRegShifted = false;
506 enum ShiftType ShiftType;
507 const MCExpr *ShiftAmount;
508 const MCExpr *Offset;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000509
Sean Callanan18b83232010-01-19 21:44:56 +0000510 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000511 if (NextTok.isNot(AsmToken::EndOfStatement)) {
512 if (NextTok.isNot(AsmToken::Comma))
Duncan Sands34727662010-07-12 08:16:59 +0000513 return Error(NextTok.getLoc(), "',' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000514 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000515 if(ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Sean Callanan76264762010-04-02 22:27:05 +0000516 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
517 E))
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000518 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000519 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000520
Sean Callanan76264762010-04-02 22:27:05 +0000521 ARMOperand::CreateMem(Op, BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
522 OffsetRegShifted, ShiftType, ShiftAmount,
523 Preindexed, Postindexed, Negative, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000524 return false;
525 }
526
527 return true;
528}
529
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000530/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
531/// we will parse the following (were +/- means that a plus or minus is
532/// optional):
533/// +/-Rm
534/// +/-Rm, shift
535/// #offset
536/// we return false on success or an error otherwise.
537bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000538 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000539 enum ShiftType &ShiftType,
540 const MCExpr *&ShiftAmount,
541 const MCExpr *&Offset,
542 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000543 int &OffsetRegNum,
544 SMLoc &E) {
545 OwningPtr<ARMOperand> Op;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000546 Negative = false;
547 OffsetRegShifted = false;
548 OffsetIsReg = false;
549 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000550 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000551 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000552 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000553 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000554 else if (NextTok.is(AsmToken::Minus)) {
555 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000556 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000557 }
558 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000559 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000560 if (OffsetRegTok.is(AsmToken::Identifier)) {
561 OffsetIsReg = !MaybeParseRegister(Op, false);
Sean Callanan76264762010-04-02 22:27:05 +0000562 if (OffsetIsReg) {
563 E = Op->getEndLoc();
564 OffsetRegNum = Op->getReg();
565 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000566 }
567 // If we parsed a register as the offset then their can be a shift after that
568 if (OffsetRegNum != -1) {
569 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000570 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000571 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000572 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000573
Sean Callanan18b83232010-01-19 21:44:56 +0000574 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000575 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000576 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000577 OffsetRegShifted = true;
578 }
579 }
580 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
581 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000582 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000583 if (HashTok.isNot(AsmToken::Hash))
584 return Error(HashTok.getLoc(), "'#' expected");
Sean Callanan76264762010-04-02 22:27:05 +0000585
Sean Callananb9a25b72010-01-19 20:27:46 +0000586 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000587
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000588 if (getParser().ParseExpression(Offset))
589 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000590 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000591 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000592 return false;
593}
594
595/// ParseShift as one of these two:
596/// ( lsl | lsr | asr | ror ) , # shift_amount
597/// rrx
598/// and returns true if it parses a shift otherwise it returns false.
Sean Callanan76264762010-04-02 22:27:05 +0000599bool ARMAsmParser::ParseShift(ShiftType &St,
600 const MCExpr *&ShiftAmount,
601 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000602 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000603 if (Tok.isNot(AsmToken::Identifier))
604 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000605 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000606 if (ShiftName == "lsl" || ShiftName == "LSL")
607 St = Lsl;
608 else if (ShiftName == "lsr" || ShiftName == "LSR")
609 St = Lsr;
610 else if (ShiftName == "asr" || ShiftName == "ASR")
611 St = Asr;
612 else if (ShiftName == "ror" || ShiftName == "ROR")
613 St = Ror;
614 else if (ShiftName == "rrx" || ShiftName == "RRX")
615 St = Rrx;
616 else
617 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000618 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000619
620 // Rrx stands alone.
621 if (St == Rrx)
622 return false;
623
624 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000625 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000626 if (HashTok.isNot(AsmToken::Hash))
627 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000628 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000629
630 if (getParser().ParseExpression(ShiftAmount))
631 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000632
633 return false;
634}
635
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000636/// Parse a arm instruction operand. For now this parses the operand regardless
637/// of the mnemonic.
Sean Callanan76264762010-04-02 22:27:05 +0000638bool ARMAsmParser::ParseOperand(OwningPtr<ARMOperand> &Op) {
639 SMLoc S, E;
640
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000641 switch (getLexer().getKind()) {
642 case AsmToken::Identifier:
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000643 if (!MaybeParseRegister(Op, true))
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000644 return false;
Kevin Enderby515d5092009-10-15 20:48:48 +0000645 // This was not a register so parse other operands that start with an
646 // identifier (like labels) as expressions and create them as immediates.
647 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000648 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000649 if (getParser().ParseExpression(IdVal))
650 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000651 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
652 ARMOperand::CreateImm(Op, IdVal, S, E);
Kevin Enderby515d5092009-10-15 20:48:48 +0000653 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000654 case AsmToken::LBrac:
Kevin Enderby515d5092009-10-15 20:48:48 +0000655 return ParseMemory(Op);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000656 case AsmToken::LCurly:
Kevin Enderby515d5092009-10-15 20:48:48 +0000657 return ParseRegisterList(Op);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000658 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000659 // #42 -> immediate.
660 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000661 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000662 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000663 const MCExpr *ImmVal;
664 if (getParser().ParseExpression(ImmVal))
Kevin Enderbycfe07242009-10-13 22:19:02 +0000665 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000666 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
667 ARMOperand::CreateImm(Op, ImmVal, S, E);
Kevin Enderbycfe07242009-10-13 22:19:02 +0000668 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000669 default:
Sean Callanan18b83232010-01-19 21:44:56 +0000670 return Error(Parser.getTok().getLoc(), "unexpected token in operand");
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000671 }
672}
673
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000674/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000675bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000676 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000677 OwningPtr<ARMOperand> Op;
Daniel Dunbar5747b132010-08-11 06:37:16 +0000678
679 // Create the leading tokens for the mnemonic, split by '.' characters.
680 size_t Start = 0, Next = Name.find('.');
681 StringRef Head = Name.slice(Start, Next);
682
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000683 // Determine the predicate, if any.
684 //
685 // FIXME: We need a way to check whether a prefix supports predication,
686 // otherwise we will end up with an ambiguity for instructions that happen to
687 // end with a predicate name.
688 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
689 .Case("eq", ARMCC::EQ)
690 .Case("ne", ARMCC::NE)
691 .Case("hs", ARMCC::HS)
692 .Case("lo", ARMCC::LO)
693 .Case("mi", ARMCC::MI)
694 .Case("pl", ARMCC::PL)
695 .Case("vs", ARMCC::VS)
696 .Case("vc", ARMCC::VC)
697 .Case("hi", ARMCC::HI)
698 .Case("ls", ARMCC::LS)
699 .Case("ge", ARMCC::GE)
700 .Case("lt", ARMCC::LT)
701 .Case("gt", ARMCC::GT)
702 .Case("le", ARMCC::LE)
703 .Case("al", ARMCC::AL)
704 .Default(~0U);
705 if (CC != ~0U) {
706 Head = Head.slice(0, Head.size() - 2);
707 } else
708 CC = ARMCC::AL;
709
Daniel Dunbar5747b132010-08-11 06:37:16 +0000710 ARMOperand::CreateToken(Op, Head, NameLoc);
Sean Callanan76264762010-04-02 22:27:05 +0000711 Operands.push_back(Op.take());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000712
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000713 ARMOperand::CreateCondCode(Op, ARMCC::CondCodes(CC), NameLoc);
714 Operands.push_back(Op.take());
715
716 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000717 while (Next != StringRef::npos) {
718 Start = Next;
719 Next = Name.find('.', Start + 1);
720 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000721
Daniel Dunbar5747b132010-08-11 06:37:16 +0000722 ARMOperand::CreateToken(Op, Head, NameLoc);
723 Operands.push_back(Op.take());
724 }
725
726 // Read the remaining operands.
727 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000728 // Read the first operand.
Sean Callanan76264762010-04-02 22:27:05 +0000729 OwningPtr<ARMOperand> Op;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000730 if (ParseOperand(Op)) {
731 Parser.EatToEndOfStatement();
732 return true;
733 }
Sean Callanan76264762010-04-02 22:27:05 +0000734 Operands.push_back(Op.take());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000735
736 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000737 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000738
739 // Parse and remember the operand.
Chris Lattnercbf8a982010-09-11 16:18:25 +0000740 if (ParseOperand(Op)) {
741 Parser.EatToEndOfStatement();
742 return true;
743 }
Sean Callanan76264762010-04-02 22:27:05 +0000744 Operands.push_back(Op.take());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000745 }
746 }
Chris Lattner34e53142010-09-08 05:10:46 +0000747
Chris Lattnercbf8a982010-09-11 16:18:25 +0000748 if (getLexer().isNot(AsmToken::EndOfStatement)) {
749 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000750 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000751 }
Chris Lattner34e53142010-09-08 05:10:46 +0000752 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000753 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000754}
755
Kevin Enderby515d5092009-10-15 20:48:48 +0000756/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000757bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
758 StringRef IDVal = DirectiveID.getIdentifier();
759 if (IDVal == ".word")
760 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000761 else if (IDVal == ".thumb")
762 return ParseDirectiveThumb(DirectiveID.getLoc());
763 else if (IDVal == ".thumb_func")
764 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
765 else if (IDVal == ".code")
766 return ParseDirectiveCode(DirectiveID.getLoc());
767 else if (IDVal == ".syntax")
768 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000769 return true;
770}
771
772/// ParseDirectiveWord
773/// ::= .word [ expression (, expression)* ]
774bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
775 if (getLexer().isNot(AsmToken::EndOfStatement)) {
776 for (;;) {
777 const MCExpr *Value;
778 if (getParser().ParseExpression(Value))
779 return true;
780
Chris Lattneraaec2052010-01-19 19:46:13 +0000781 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000782
783 if (getLexer().is(AsmToken::EndOfStatement))
784 break;
785
786 // FIXME: Improve diagnostic.
787 if (getLexer().isNot(AsmToken::Comma))
788 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000789 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000790 }
791 }
792
Sean Callananb9a25b72010-01-19 20:27:46 +0000793 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000794 return false;
795}
796
Kevin Enderby515d5092009-10-15 20:48:48 +0000797/// ParseDirectiveThumb
798/// ::= .thumb
799bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
800 if (getLexer().isNot(AsmToken::EndOfStatement))
801 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000802 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000803
804 // TODO: set thumb mode
805 // TODO: tell the MC streamer the mode
806 // getParser().getStreamer().Emit???();
807 return false;
808}
809
810/// ParseDirectiveThumbFunc
811/// ::= .thumbfunc symbol_name
812bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000813 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000814 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
815 return Error(L, "unexpected token in .syntax directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000816 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000817
818 if (getLexer().isNot(AsmToken::EndOfStatement))
819 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000820 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000821
822 // TODO: mark symbol as a thumb symbol
823 // getParser().getStreamer().Emit???();
824 return false;
825}
826
827/// ParseDirectiveSyntax
828/// ::= .syntax unified | divided
829bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000830 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000831 if (Tok.isNot(AsmToken::Identifier))
832 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000833 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000834 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000835 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000836 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000837 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000838 else
839 return Error(L, "unrecognized syntax mode in .syntax directive");
840
841 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000842 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000843 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000844
845 // TODO tell the MC streamer the mode
846 // getParser().getStreamer().Emit???();
847 return false;
848}
849
850/// ParseDirectiveCode
851/// ::= .code 16 | 32
852bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000853 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000854 if (Tok.isNot(AsmToken::Integer))
855 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000856 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +0000857 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +0000858 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000859 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +0000860 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000861 else
862 return Error(L, "invalid operand to .code directive");
863
864 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000865 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000866 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000867
868 // TODO tell the MC streamer the mode
869 // getParser().getStreamer().Emit???();
870 return false;
871}
872
Sean Callanan90b70972010-04-07 20:29:34 +0000873extern "C" void LLVMInitializeARMAsmLexer();
874
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000875/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000876extern "C" void LLVMInitializeARMAsmParser() {
877 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
878 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +0000879 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000880}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000881
Chris Lattner0692ee62010-09-06 19:11:01 +0000882#define GET_REGISTER_MATCHER
883#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000884#include "ARMGenAsmMatcher.inc"