blob: e0d7762ed5cbcf4b7db6e6a3181425dd5af9e0b1 [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
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
Chris Lattner3a697562010-10-28 17:20:03 +000037namespace {
38 struct ARMOperand;
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
Chris Lattner3a697562010-10-28 17:20:03 +000053 ARMOperand *MaybeParseRegister(bool ParseWriteBack);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000054
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +000055 ARMOperand *ParseRegisterList();
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};
Chris Lattner3a697562010-10-28 17:20:03 +0000115} // end anonymous namespace
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000116
Chris Lattner3a697562010-10-28 17:20:03 +0000117namespace {
118
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000119/// ARMOperand - Instances of this class represent a parsed ARM machine
120/// instruction.
Chris Lattner76593892010-01-14 21:21:40 +0000121struct ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000122public:
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
Chris Lattner3a697562010-10-28 17:20:03 +0000256 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
257 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000258 Op->CC.Val = CC;
259 Op->StartLoc = S;
260 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000261 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000262 }
263
Chris Lattner3a697562010-10-28 17:20:03 +0000264 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
265 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000266 Op->Tok.Data = Str.data();
267 Op->Tok.Length = Str.size();
268 Op->StartLoc = S;
269 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000270 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000271 }
272
Chris Lattner3a697562010-10-28 17:20:03 +0000273 static ARMOperand *CreateReg(unsigned RegNum, bool Writeback, SMLoc S,
274 SMLoc E) {
275 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000276 Op->Reg.RegNum = RegNum;
277 Op->Reg.Writeback = Writeback;
Sean Callanan76264762010-04-02 22:27:05 +0000278 Op->StartLoc = S;
279 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000280 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000281 }
282
Chris Lattner3a697562010-10-28 17:20:03 +0000283 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
284 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000285 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000286 Op->StartLoc = S;
287 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000288 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000289 }
290
Chris Lattner3a697562010-10-28 17:20:03 +0000291 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
292 const MCExpr *Offset, unsigned OffsetRegNum,
293 bool OffsetRegShifted, enum ShiftType ShiftType,
294 const MCExpr *ShiftAmount, bool Preindexed,
295 bool Postindexed, bool Negative, bool Writeback,
296 SMLoc S, SMLoc E) {
297 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000298 Op->Mem.BaseRegNum = BaseRegNum;
299 Op->Mem.OffsetIsReg = OffsetIsReg;
300 Op->Mem.Offset = Offset;
301 Op->Mem.OffsetRegNum = OffsetRegNum;
302 Op->Mem.OffsetRegShifted = OffsetRegShifted;
303 Op->Mem.ShiftType = ShiftType;
304 Op->Mem.ShiftAmount = ShiftAmount;
305 Op->Mem.Preindexed = Preindexed;
306 Op->Mem.Postindexed = Postindexed;
307 Op->Mem.Negative = Negative;
308 Op->Mem.Writeback = Writeback;
309
310 Op->StartLoc = S;
311 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000312 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000313 }
Chris Lattner3a697562010-10-28 17:20:03 +0000314
315private:
316 ARMOperand(KindTy K) : Kind(K) {}
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000317};
318
319} // end anonymous namespace.
320
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000321void ARMOperand::dump(raw_ostream &OS) const {
322 switch (Kind) {
323 case CondCode:
324 OS << ARMCondCodeToString(getCondCode());
325 break;
326 case Immediate:
327 getImm()->print(OS);
328 break;
329 case Memory:
330 OS << "<memory>";
331 break;
332 case Register:
333 OS << "<register " << getReg() << ">";
334 break;
335 case Token:
336 OS << "'" << getToken() << "'";
337 break;
338 }
339}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000340
341/// @name Auto-generated Match Functions
342/// {
343
344static unsigned MatchRegisterName(StringRef Name);
345
346/// }
347
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000348/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattner3a697562010-10-28 17:20:03 +0000349/// and if it is a register name the token is eaten and a Reg operand is created
350/// and returned. Otherwise return null.
351///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000352/// TODO this is likely to change to allow different register types and or to
353/// parse for a specific register type.
Chris Lattner3a697562010-10-28 17:20:03 +0000354ARMOperand *ARMAsmParser::MaybeParseRegister(bool ParseWriteBack) {
Sean Callanan76264762010-04-02 22:27:05 +0000355 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000356 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000357 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
358
359 // FIXME: Validate register for the current architecture; we have to do
360 // validation later, so maybe there is no need for this here.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000361 int RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000362
363 RegNum = MatchRegisterName(Tok.getString());
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000364 if (RegNum == -1)
Chris Lattner3a697562010-10-28 17:20:03 +0000365 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000366
367 S = Tok.getLoc();
368
Sean Callananb9a25b72010-01-19 20:27:46 +0000369 Parser.Lex(); // Eat identifier token.
Sean Callanan76264762010-04-02 22:27:05 +0000370
371 E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000372
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000373 bool Writeback = false;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000374 if (ParseWriteBack) {
Sean Callanan18b83232010-01-19 21:44:56 +0000375 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000376 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000377 E = ExclaimTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000378 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000379 Parser.Lex(); // Eat exclaim token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000380 }
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000381 }
382
Chris Lattner3a697562010-10-28 17:20:03 +0000383 return ARMOperand::CreateReg(RegNum, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000384}
385
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000386/// Parse a register list, return it if successful else return null. The first
387/// token must be a '{' when called.
388ARMOperand *ARMAsmParser::ParseRegisterList() {
Sean Callanan76264762010-04-02 22:27:05 +0000389 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000390 assert(Parser.getTok().is(AsmToken::LCurly) &&
Kevin Enderbycfe07242009-10-13 22:19:02 +0000391 "Token is not an Left Curly Brace");
Sean Callanan76264762010-04-02 22:27:05 +0000392 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000393 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000394
Sean Callanan18b83232010-01-19 21:44:56 +0000395 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000396 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000397 if (RegTok.isNot(AsmToken::Identifier)) {
398 Error(RegLoc, "register expected");
399 return 0;
400 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000401 int RegNum = MatchRegisterName(RegTok.getString());
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000402 if (RegNum == -1) {
403 Error(RegLoc, "register expected");
404 return 0;
405 }
406
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();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000417 if (RegTok.isNot(AsmToken::Identifier)) {
418 Error(RegLoc, "register expected");
419 return 0;
420 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000421 int RegNum = MatchRegisterName(RegTok.getString());
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000422 if (RegNum == -1) {
423 Error(RegLoc, "register expected");
424 return 0;
425 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000426
427 if (RegList & (1 << RegNum))
428 Warning(RegLoc, "register duplicated in register list");
429 else if (RegNum <= HighRegNum)
430 Warning(RegLoc, "register not in ascending order in register list");
431 RegList |= 1 << RegNum;
432 HighRegNum = RegNum;
433
Sean Callananb9a25b72010-01-19 20:27:46 +0000434 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000435 }
Sean Callanan18b83232010-01-19 21:44:56 +0000436 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000437 if (RCurlyTok.isNot(AsmToken::RCurly)) {
438 Error(RCurlyTok.getLoc(), "'}' expected");
439 return 0;
440 }
Sean Callanan76264762010-04-02 22:27:05 +0000441 E = RCurlyTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000442 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000443
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000444 // FIXME: Need to return an operand!
445 Error(E, "FIXME: register list parsing not implemented");
446 return 0;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000447}
448
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000449/// Parse an arm memory expression, return false if successful else return true
450/// or an error. The first token must be a '[' when called.
451/// TODO Only preindexing and postindexing addressing are started, unindexed
452/// with option, etc are still to do.
Sean Callanan76264762010-04-02 22:27:05 +0000453bool ARMAsmParser::ParseMemory(OwningPtr<ARMOperand> &Op) {
454 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000455 assert(Parser.getTok().is(AsmToken::LBrac) &&
Kevin Enderby6bd266e2009-10-12 22:51:49 +0000456 "Token is not an Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000457 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000458 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000459
Sean Callanan18b83232010-01-19 21:44:56 +0000460 const AsmToken &BaseRegTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000461 if (BaseRegTok.isNot(AsmToken::Identifier))
462 return Error(BaseRegTok.getLoc(), "register expected");
Chris Lattner3a697562010-10-28 17:20:03 +0000463 Op.reset(MaybeParseRegister(false));
464 if (Op.get() == 0)
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000465 return Error(BaseRegTok.getLoc(), "register expected");
Sean Callanan76264762010-04-02 22:27:05 +0000466 int BaseRegNum = Op->getReg();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000467
468 bool Preindexed = false;
469 bool Postindexed = false;
470 bool OffsetIsReg = false;
471 bool Negative = false;
472 bool Writeback = false;
473
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000474 // First look for preindexed address forms, that is after the "[Rn" we now
475 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000476 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000477 if (Tok.is(AsmToken::Comma)) {
478 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000479 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000480 int OffsetRegNum;
481 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000482 enum ShiftType ShiftType;
483 const MCExpr *ShiftAmount;
484 const MCExpr *Offset;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000485 if(ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000486 Offset, OffsetIsReg, OffsetRegNum, E))
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000487 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000488 const AsmToken &RBracTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000489 if (RBracTok.isNot(AsmToken::RBrac))
490 return Error(RBracTok.getLoc(), "']' expected");
Sean Callanan76264762010-04-02 22:27:05 +0000491 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000492 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000493
Sean Callanan18b83232010-01-19 21:44:56 +0000494 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000495 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000496 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000497 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000498 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000499 }
Chris Lattner3a697562010-10-28 17:20:03 +0000500 Op.reset(
501 ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
502 OffsetRegShifted, ShiftType, ShiftAmount,
503 Preindexed, Postindexed, Negative, Writeback, S,E));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000504 return false;
505 }
506 // The "[Rn" we have so far was not followed by a comma.
507 else if (Tok.is(AsmToken::RBrac)) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000508 // This is a post indexing addressing forms, that is a ']' follows after
509 // the "[Rn".
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000510 Postindexed = true;
511 Writeback = true;
Sean Callanan76264762010-04-02 22:27:05 +0000512 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000513 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000514
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000515 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000516 bool OffsetRegShifted = false;
517 enum ShiftType ShiftType;
518 const MCExpr *ShiftAmount;
519 const MCExpr *Offset;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000520
Sean Callanan18b83232010-01-19 21:44:56 +0000521 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000522 if (NextTok.isNot(AsmToken::EndOfStatement)) {
523 if (NextTok.isNot(AsmToken::Comma))
Duncan Sands34727662010-07-12 08:16:59 +0000524 return Error(NextTok.getLoc(), "',' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000525 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000526 if(ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Sean Callanan76264762010-04-02 22:27:05 +0000527 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
528 E))
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000529 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000530 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000531
Chris Lattner3a697562010-10-28 17:20:03 +0000532 Op.reset(
533 ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
534 OffsetRegShifted, ShiftType, ShiftAmount,
535 Preindexed, Postindexed, Negative, Writeback, S,E));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000536 return false;
537 }
538
539 return true;
540}
541
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000542/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
543/// we will parse the following (were +/- means that a plus or minus is
544/// optional):
545/// +/-Rm
546/// +/-Rm, shift
547/// #offset
548/// we return false on success or an error otherwise.
549bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000550 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000551 enum ShiftType &ShiftType,
552 const MCExpr *&ShiftAmount,
553 const MCExpr *&Offset,
554 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000555 int &OffsetRegNum,
556 SMLoc &E) {
557 OwningPtr<ARMOperand> Op;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000558 Negative = false;
559 OffsetRegShifted = false;
560 OffsetIsReg = false;
561 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000562 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000563 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000564 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000565 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000566 else if (NextTok.is(AsmToken::Minus)) {
567 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000568 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000569 }
570 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000571 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000572 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattner3a697562010-10-28 17:20:03 +0000573 Op.reset(MaybeParseRegister(false));
574 OffsetIsReg = Op.get() != 0;
Sean Callanan76264762010-04-02 22:27:05 +0000575 if (OffsetIsReg) {
576 E = Op->getEndLoc();
577 OffsetRegNum = Op->getReg();
578 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000579 }
580 // If we parsed a register as the offset then their can be a shift after that
581 if (OffsetRegNum != -1) {
582 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000583 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000584 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000585 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000586
Sean Callanan18b83232010-01-19 21:44:56 +0000587 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000588 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000589 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000590 OffsetRegShifted = true;
591 }
592 }
593 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
594 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000595 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000596 if (HashTok.isNot(AsmToken::Hash))
597 return Error(HashTok.getLoc(), "'#' expected");
Sean Callanan76264762010-04-02 22:27:05 +0000598
Sean Callananb9a25b72010-01-19 20:27:46 +0000599 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000600
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000601 if (getParser().ParseExpression(Offset))
602 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000603 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000604 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000605 return false;
606}
607
608/// ParseShift as one of these two:
609/// ( lsl | lsr | asr | ror ) , # shift_amount
610/// rrx
611/// and returns true if it parses a shift otherwise it returns false.
Chris Lattner3a697562010-10-28 17:20:03 +0000612bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000613 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000614 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000615 if (Tok.isNot(AsmToken::Identifier))
616 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000617 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000618 if (ShiftName == "lsl" || ShiftName == "LSL")
619 St = Lsl;
620 else if (ShiftName == "lsr" || ShiftName == "LSR")
621 St = Lsr;
622 else if (ShiftName == "asr" || ShiftName == "ASR")
623 St = Asr;
624 else if (ShiftName == "ror" || ShiftName == "ROR")
625 St = Ror;
626 else if (ShiftName == "rrx" || ShiftName == "RRX")
627 St = Rrx;
628 else
629 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000630 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000631
632 // Rrx stands alone.
633 if (St == Rrx)
634 return false;
635
636 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000637 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000638 if (HashTok.isNot(AsmToken::Hash))
639 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000640 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000641
642 if (getParser().ParseExpression(ShiftAmount))
643 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000644
645 return false;
646}
647
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000648/// Parse a arm instruction operand. For now this parses the operand regardless
649/// of the mnemonic.
Sean Callanan76264762010-04-02 22:27:05 +0000650bool ARMAsmParser::ParseOperand(OwningPtr<ARMOperand> &Op) {
651 SMLoc S, E;
652
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000653 switch (getLexer().getKind()) {
654 case AsmToken::Identifier:
Chris Lattner3a697562010-10-28 17:20:03 +0000655 Op.reset(MaybeParseRegister(true));
656 if (Op.get() != 0)
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000657 return false;
Kevin Enderby515d5092009-10-15 20:48:48 +0000658 // This was not a register so parse other operands that start with an
659 // identifier (like labels) as expressions and create them as immediates.
660 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000661 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000662 if (getParser().ParseExpression(IdVal))
663 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000664 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner3a697562010-10-28 17:20:03 +0000665 Op.reset(ARMOperand::CreateImm(IdVal, S, E));
Kevin Enderby515d5092009-10-15 20:48:48 +0000666 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000667 case AsmToken::LBrac:
Kevin Enderby515d5092009-10-15 20:48:48 +0000668 return ParseMemory(Op);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000669 case AsmToken::LCurly:
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000670 Op.reset(ParseRegisterList());
671 return Op.get() == 0;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000672 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000673 // #42 -> immediate.
674 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000675 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000676 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000677 const MCExpr *ImmVal;
678 if (getParser().ParseExpression(ImmVal))
Kevin Enderbycfe07242009-10-13 22:19:02 +0000679 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000680 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner3a697562010-10-28 17:20:03 +0000681 Op.reset(ARMOperand::CreateImm(ImmVal, S, E));
Kevin Enderbycfe07242009-10-13 22:19:02 +0000682 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000683 default:
Sean Callanan18b83232010-01-19 21:44:56 +0000684 return Error(Parser.getTok().getLoc(), "unexpected token in operand");
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000685 }
686}
687
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000688/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000689bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000690 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000691 // Create the leading tokens for the mnemonic, split by '.' characters.
692 size_t Start = 0, Next = Name.find('.');
693 StringRef Head = Name.slice(Start, Next);
694
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000695 // Determine the predicate, if any.
696 //
697 // FIXME: We need a way to check whether a prefix supports predication,
698 // otherwise we will end up with an ambiguity for instructions that happen to
699 // end with a predicate name.
700 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
701 .Case("eq", ARMCC::EQ)
702 .Case("ne", ARMCC::NE)
703 .Case("hs", ARMCC::HS)
704 .Case("lo", ARMCC::LO)
705 .Case("mi", ARMCC::MI)
706 .Case("pl", ARMCC::PL)
707 .Case("vs", ARMCC::VS)
708 .Case("vc", ARMCC::VC)
709 .Case("hi", ARMCC::HI)
710 .Case("ls", ARMCC::LS)
711 .Case("ge", ARMCC::GE)
712 .Case("lt", ARMCC::LT)
713 .Case("gt", ARMCC::GT)
714 .Case("le", ARMCC::LE)
715 .Case("al", ARMCC::AL)
716 .Default(~0U);
Chris Lattner3a697562010-10-28 17:20:03 +0000717
718 if (CC != ~0U)
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000719 Head = Head.slice(0, Head.size() - 2);
Chris Lattner3a697562010-10-28 17:20:03 +0000720 else
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000721 CC = ARMCC::AL;
722
Chris Lattner3a697562010-10-28 17:20:03 +0000723 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
724 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000725
726 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000727 while (Next != StringRef::npos) {
728 Start = Next;
729 Next = Name.find('.', Start + 1);
730 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000731
Chris Lattner3a697562010-10-28 17:20:03 +0000732 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000733 }
734
735 // Read the remaining operands.
736 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000737 // Read the first operand.
Sean Callanan76264762010-04-02 22:27:05 +0000738 OwningPtr<ARMOperand> Op;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000739 if (ParseOperand(Op)) {
740 Parser.EatToEndOfStatement();
741 return true;
742 }
Sean Callanan76264762010-04-02 22:27:05 +0000743 Operands.push_back(Op.take());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000744
745 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000746 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000747
748 // Parse and remember the operand.
Chris Lattnercbf8a982010-09-11 16:18:25 +0000749 if (ParseOperand(Op)) {
750 Parser.EatToEndOfStatement();
751 return true;
752 }
Sean Callanan76264762010-04-02 22:27:05 +0000753 Operands.push_back(Op.take());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000754 }
755 }
Chris Lattner34e53142010-09-08 05:10:46 +0000756
Chris Lattnercbf8a982010-09-11 16:18:25 +0000757 if (getLexer().isNot(AsmToken::EndOfStatement)) {
758 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000759 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000760 }
Chris Lattner34e53142010-09-08 05:10:46 +0000761 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000762 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000763}
764
Kevin Enderby515d5092009-10-15 20:48:48 +0000765/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000766bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
767 StringRef IDVal = DirectiveID.getIdentifier();
768 if (IDVal == ".word")
769 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000770 else if (IDVal == ".thumb")
771 return ParseDirectiveThumb(DirectiveID.getLoc());
772 else if (IDVal == ".thumb_func")
773 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
774 else if (IDVal == ".code")
775 return ParseDirectiveCode(DirectiveID.getLoc());
776 else if (IDVal == ".syntax")
777 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000778 return true;
779}
780
781/// ParseDirectiveWord
782/// ::= .word [ expression (, expression)* ]
783bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
784 if (getLexer().isNot(AsmToken::EndOfStatement)) {
785 for (;;) {
786 const MCExpr *Value;
787 if (getParser().ParseExpression(Value))
788 return true;
789
Chris Lattneraaec2052010-01-19 19:46:13 +0000790 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000791
792 if (getLexer().is(AsmToken::EndOfStatement))
793 break;
794
795 // FIXME: Improve diagnostic.
796 if (getLexer().isNot(AsmToken::Comma))
797 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000798 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000799 }
800 }
801
Sean Callananb9a25b72010-01-19 20:27:46 +0000802 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000803 return false;
804}
805
Kevin Enderby515d5092009-10-15 20:48:48 +0000806/// ParseDirectiveThumb
807/// ::= .thumb
808bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
809 if (getLexer().isNot(AsmToken::EndOfStatement))
810 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000811 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000812
813 // TODO: set thumb mode
814 // TODO: tell the MC streamer the mode
815 // getParser().getStreamer().Emit???();
816 return false;
817}
818
819/// ParseDirectiveThumbFunc
820/// ::= .thumbfunc symbol_name
821bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000822 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000823 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
824 return Error(L, "unexpected token in .syntax directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000825 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000826
827 if (getLexer().isNot(AsmToken::EndOfStatement))
828 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000829 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000830
831 // TODO: mark symbol as a thumb symbol
832 // getParser().getStreamer().Emit???();
833 return false;
834}
835
836/// ParseDirectiveSyntax
837/// ::= .syntax unified | divided
838bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000839 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000840 if (Tok.isNot(AsmToken::Identifier))
841 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000842 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000843 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000844 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000845 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000846 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000847 else
848 return Error(L, "unrecognized syntax mode in .syntax directive");
849
850 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000851 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000852 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000853
854 // TODO tell the MC streamer the mode
855 // getParser().getStreamer().Emit???();
856 return false;
857}
858
859/// ParseDirectiveCode
860/// ::= .code 16 | 32
861bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000862 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000863 if (Tok.isNot(AsmToken::Integer))
864 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000865 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +0000866 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +0000867 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000868 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +0000869 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000870 else
871 return Error(L, "invalid operand to .code directive");
872
873 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000874 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000875 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000876
877 // TODO tell the MC streamer the mode
878 // getParser().getStreamer().Emit???();
879 return false;
880}
881
Sean Callanan90b70972010-04-07 20:29:34 +0000882extern "C" void LLVMInitializeARMAsmLexer();
883
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000884/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000885extern "C" void LLVMInitializeARMAsmParser() {
886 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
887 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +0000888 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000889}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000890
Chris Lattner0692ee62010-09-06 19:11:01 +0000891#define GET_REGISTER_MATCHER
892#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000893#include "ARMGenAsmMatcher.inc"