blob: b2df045a16c61de19ad9bcc9085e252f8d3f5cdb [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
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};
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
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000386/// Parse a register list, return false if successful else return true or an
387/// error. The first token must be a '{' when called.
Sean Callanan76264762010-04-02 22:27:05 +0000388bool ARMAsmParser::ParseRegisterList(OwningPtr<ARMOperand> &Op) {
389 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();
397 if (RegTok.isNot(AsmToken::Identifier))
398 return Error(RegLoc, "register expected");
399 int RegNum = MatchRegisterName(RegTok.getString());
400 if (RegNum == -1)
401 return Error(RegLoc, "register expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000402 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000403 unsigned RegList = 1 << RegNum;
404
405 int HighRegNum = RegNum;
406 // TODO ranges like "{Rn-Rm}"
Sean Callanan18b83232010-01-19 21:44:56 +0000407 while (Parser.getTok().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000408 Parser.Lex(); // Eat comma token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000409
Sean Callanan18b83232010-01-19 21:44:56 +0000410 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000411 SMLoc RegLoc = RegTok.getLoc();
412 if (RegTok.isNot(AsmToken::Identifier))
413 return Error(RegLoc, "register expected");
414 int RegNum = MatchRegisterName(RegTok.getString());
415 if (RegNum == -1)
416 return Error(RegLoc, "register expected");
417
418 if (RegList & (1 << RegNum))
419 Warning(RegLoc, "register duplicated in register list");
420 else if (RegNum <= HighRegNum)
421 Warning(RegLoc, "register not in ascending order in register list");
422 RegList |= 1 << RegNum;
423 HighRegNum = RegNum;
424
Sean Callananb9a25b72010-01-19 20:27:46 +0000425 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000426 }
Sean Callanan18b83232010-01-19 21:44:56 +0000427 const AsmToken &RCurlyTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000428 if (RCurlyTok.isNot(AsmToken::RCurly))
429 return Error(RCurlyTok.getLoc(), "'}' expected");
Sean Callanan76264762010-04-02 22:27:05 +0000430 E = RCurlyTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000431 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000432
433 return false;
434}
435
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000436/// Parse an arm memory expression, return false if successful else return true
437/// or an error. The first token must be a '[' when called.
438/// TODO Only preindexing and postindexing addressing are started, unindexed
439/// with option, etc are still to do.
Sean Callanan76264762010-04-02 22:27:05 +0000440bool ARMAsmParser::ParseMemory(OwningPtr<ARMOperand> &Op) {
441 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000442 assert(Parser.getTok().is(AsmToken::LBrac) &&
Kevin Enderby6bd266e2009-10-12 22:51:49 +0000443 "Token is not an Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000444 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000445 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000446
Sean Callanan18b83232010-01-19 21:44:56 +0000447 const AsmToken &BaseRegTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000448 if (BaseRegTok.isNot(AsmToken::Identifier))
449 return Error(BaseRegTok.getLoc(), "register expected");
Chris Lattner3a697562010-10-28 17:20:03 +0000450 Op.reset(MaybeParseRegister(false));
451 if (Op.get() == 0)
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000452 return Error(BaseRegTok.getLoc(), "register expected");
Sean Callanan76264762010-04-02 22:27:05 +0000453 int BaseRegNum = Op->getReg();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000454
455 bool Preindexed = false;
456 bool Postindexed = false;
457 bool OffsetIsReg = false;
458 bool Negative = false;
459 bool Writeback = false;
460
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000461 // First look for preindexed address forms, that is after the "[Rn" we now
462 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000463 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000464 if (Tok.is(AsmToken::Comma)) {
465 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000466 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000467 int OffsetRegNum;
468 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000469 enum ShiftType ShiftType;
470 const MCExpr *ShiftAmount;
471 const MCExpr *Offset;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000472 if(ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000473 Offset, OffsetIsReg, OffsetRegNum, E))
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000474 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000475 const AsmToken &RBracTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000476 if (RBracTok.isNot(AsmToken::RBrac))
477 return Error(RBracTok.getLoc(), "']' expected");
Sean Callanan76264762010-04-02 22:27:05 +0000478 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000479 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000480
Sean Callanan18b83232010-01-19 21:44:56 +0000481 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000482 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000483 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000484 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000485 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000486 }
Chris Lattner3a697562010-10-28 17:20:03 +0000487 Op.reset(
488 ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
489 OffsetRegShifted, ShiftType, ShiftAmount,
490 Preindexed, Postindexed, Negative, Writeback, S,E));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000491 return false;
492 }
493 // The "[Rn" we have so far was not followed by a comma.
494 else if (Tok.is(AsmToken::RBrac)) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000495 // This is a post indexing addressing forms, that is a ']' follows after
496 // the "[Rn".
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000497 Postindexed = true;
498 Writeback = true;
Sean Callanan76264762010-04-02 22:27:05 +0000499 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000500 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000501
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000502 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000503 bool OffsetRegShifted = false;
504 enum ShiftType ShiftType;
505 const MCExpr *ShiftAmount;
506 const MCExpr *Offset;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000507
Sean Callanan18b83232010-01-19 21:44:56 +0000508 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000509 if (NextTok.isNot(AsmToken::EndOfStatement)) {
510 if (NextTok.isNot(AsmToken::Comma))
Duncan Sands34727662010-07-12 08:16:59 +0000511 return Error(NextTok.getLoc(), "',' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000512 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000513 if(ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Sean Callanan76264762010-04-02 22:27:05 +0000514 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
515 E))
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000516 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000517 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000518
Chris Lattner3a697562010-10-28 17:20:03 +0000519 Op.reset(
520 ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
521 OffsetRegShifted, ShiftType, ShiftAmount,
522 Preindexed, Postindexed, Negative, Writeback, S,E));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000523 return false;
524 }
525
526 return true;
527}
528
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000529/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
530/// we will parse the following (were +/- means that a plus or minus is
531/// optional):
532/// +/-Rm
533/// +/-Rm, shift
534/// #offset
535/// we return false on success or an error otherwise.
536bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000537 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000538 enum ShiftType &ShiftType,
539 const MCExpr *&ShiftAmount,
540 const MCExpr *&Offset,
541 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000542 int &OffsetRegNum,
543 SMLoc &E) {
544 OwningPtr<ARMOperand> Op;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000545 Negative = false;
546 OffsetRegShifted = false;
547 OffsetIsReg = false;
548 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000549 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000550 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000551 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000552 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000553 else if (NextTok.is(AsmToken::Minus)) {
554 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000555 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000556 }
557 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000558 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000559 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattner3a697562010-10-28 17:20:03 +0000560 Op.reset(MaybeParseRegister(false));
561 OffsetIsReg = Op.get() != 0;
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.
Chris Lattner3a697562010-10-28 17:20:03 +0000599bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000600 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000601 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000602 if (Tok.isNot(AsmToken::Identifier))
603 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000604 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000605 if (ShiftName == "lsl" || ShiftName == "LSL")
606 St = Lsl;
607 else if (ShiftName == "lsr" || ShiftName == "LSR")
608 St = Lsr;
609 else if (ShiftName == "asr" || ShiftName == "ASR")
610 St = Asr;
611 else if (ShiftName == "ror" || ShiftName == "ROR")
612 St = Ror;
613 else if (ShiftName == "rrx" || ShiftName == "RRX")
614 St = Rrx;
615 else
616 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000617 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000618
619 // Rrx stands alone.
620 if (St == Rrx)
621 return false;
622
623 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000624 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000625 if (HashTok.isNot(AsmToken::Hash))
626 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000627 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000628
629 if (getParser().ParseExpression(ShiftAmount))
630 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000631
632 return false;
633}
634
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000635/// Parse a arm instruction operand. For now this parses the operand regardless
636/// of the mnemonic.
Sean Callanan76264762010-04-02 22:27:05 +0000637bool ARMAsmParser::ParseOperand(OwningPtr<ARMOperand> &Op) {
638 SMLoc S, E;
639
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000640 switch (getLexer().getKind()) {
641 case AsmToken::Identifier:
Chris Lattner3a697562010-10-28 17:20:03 +0000642 Op.reset(MaybeParseRegister(true));
643 if (Op.get() != 0)
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);
Chris Lattner3a697562010-10-28 17:20:03 +0000652 Op.reset(ARMOperand::CreateImm(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);
Chris Lattner3a697562010-10-28 17:20:03 +0000667 Op.reset(ARMOperand::CreateImm(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) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000677 // Create the leading tokens for the mnemonic, split by '.' characters.
678 size_t Start = 0, Next = Name.find('.');
679 StringRef Head = Name.slice(Start, Next);
680
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000681 // Determine the predicate, if any.
682 //
683 // FIXME: We need a way to check whether a prefix supports predication,
684 // otherwise we will end up with an ambiguity for instructions that happen to
685 // end with a predicate name.
686 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
687 .Case("eq", ARMCC::EQ)
688 .Case("ne", ARMCC::NE)
689 .Case("hs", ARMCC::HS)
690 .Case("lo", ARMCC::LO)
691 .Case("mi", ARMCC::MI)
692 .Case("pl", ARMCC::PL)
693 .Case("vs", ARMCC::VS)
694 .Case("vc", ARMCC::VC)
695 .Case("hi", ARMCC::HI)
696 .Case("ls", ARMCC::LS)
697 .Case("ge", ARMCC::GE)
698 .Case("lt", ARMCC::LT)
699 .Case("gt", ARMCC::GT)
700 .Case("le", ARMCC::LE)
701 .Case("al", ARMCC::AL)
702 .Default(~0U);
Chris Lattner3a697562010-10-28 17:20:03 +0000703
704 if (CC != ~0U)
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000705 Head = Head.slice(0, Head.size() - 2);
Chris Lattner3a697562010-10-28 17:20:03 +0000706 else
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000707 CC = ARMCC::AL;
708
Chris Lattner3a697562010-10-28 17:20:03 +0000709 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
710 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000711
712 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000713 while (Next != StringRef::npos) {
714 Start = Next;
715 Next = Name.find('.', Start + 1);
716 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000717
Chris Lattner3a697562010-10-28 17:20:03 +0000718 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000719 }
720
721 // Read the remaining operands.
722 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000723 // Read the first operand.
Sean Callanan76264762010-04-02 22:27:05 +0000724 OwningPtr<ARMOperand> Op;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000725 if (ParseOperand(Op)) {
726 Parser.EatToEndOfStatement();
727 return true;
728 }
Sean Callanan76264762010-04-02 22:27:05 +0000729 Operands.push_back(Op.take());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000730
731 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000732 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000733
734 // Parse and remember the operand.
Chris Lattnercbf8a982010-09-11 16:18:25 +0000735 if (ParseOperand(Op)) {
736 Parser.EatToEndOfStatement();
737 return true;
738 }
Sean Callanan76264762010-04-02 22:27:05 +0000739 Operands.push_back(Op.take());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000740 }
741 }
Chris Lattner34e53142010-09-08 05:10:46 +0000742
Chris Lattnercbf8a982010-09-11 16:18:25 +0000743 if (getLexer().isNot(AsmToken::EndOfStatement)) {
744 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000745 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000746 }
Chris Lattner34e53142010-09-08 05:10:46 +0000747 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000748 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000749}
750
Kevin Enderby515d5092009-10-15 20:48:48 +0000751/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000752bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
753 StringRef IDVal = DirectiveID.getIdentifier();
754 if (IDVal == ".word")
755 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000756 else if (IDVal == ".thumb")
757 return ParseDirectiveThumb(DirectiveID.getLoc());
758 else if (IDVal == ".thumb_func")
759 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
760 else if (IDVal == ".code")
761 return ParseDirectiveCode(DirectiveID.getLoc());
762 else if (IDVal == ".syntax")
763 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000764 return true;
765}
766
767/// ParseDirectiveWord
768/// ::= .word [ expression (, expression)* ]
769bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
770 if (getLexer().isNot(AsmToken::EndOfStatement)) {
771 for (;;) {
772 const MCExpr *Value;
773 if (getParser().ParseExpression(Value))
774 return true;
775
Chris Lattneraaec2052010-01-19 19:46:13 +0000776 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000777
778 if (getLexer().is(AsmToken::EndOfStatement))
779 break;
780
781 // FIXME: Improve diagnostic.
782 if (getLexer().isNot(AsmToken::Comma))
783 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000784 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000785 }
786 }
787
Sean Callananb9a25b72010-01-19 20:27:46 +0000788 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000789 return false;
790}
791
Kevin Enderby515d5092009-10-15 20:48:48 +0000792/// ParseDirectiveThumb
793/// ::= .thumb
794bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
795 if (getLexer().isNot(AsmToken::EndOfStatement))
796 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000797 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000798
799 // TODO: set thumb mode
800 // TODO: tell the MC streamer the mode
801 // getParser().getStreamer().Emit???();
802 return false;
803}
804
805/// ParseDirectiveThumbFunc
806/// ::= .thumbfunc symbol_name
807bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000808 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000809 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
810 return Error(L, "unexpected token in .syntax directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000811 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000812
813 if (getLexer().isNot(AsmToken::EndOfStatement))
814 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000815 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000816
817 // TODO: mark symbol as a thumb symbol
818 // getParser().getStreamer().Emit???();
819 return false;
820}
821
822/// ParseDirectiveSyntax
823/// ::= .syntax unified | divided
824bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000825 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000826 if (Tok.isNot(AsmToken::Identifier))
827 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000828 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000829 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000830 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000831 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000832 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000833 else
834 return Error(L, "unrecognized syntax mode in .syntax directive");
835
836 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000837 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000838 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000839
840 // TODO tell the MC streamer the mode
841 // getParser().getStreamer().Emit???();
842 return false;
843}
844
845/// ParseDirectiveCode
846/// ::= .code 16 | 32
847bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000848 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000849 if (Tok.isNot(AsmToken::Integer))
850 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000851 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +0000852 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +0000853 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000854 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +0000855 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000856 else
857 return Error(L, "invalid operand to .code directive");
858
859 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000860 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000861 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000862
863 // TODO tell the MC streamer the mode
864 // getParser().getStreamer().Emit???();
865 return false;
866}
867
Sean Callanan90b70972010-04-07 20:29:34 +0000868extern "C" void LLVMInitializeARMAsmLexer();
869
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000870/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000871extern "C" void LLVMInitializeARMAsmParser() {
872 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
873 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +0000874 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000875}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000876
Chris Lattner0692ee62010-09-06 19:11:01 +0000877#define GET_REGISTER_MATCHER
878#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000879#include "ARMGenAsmMatcher.inc"