blob: 327af20d9934532590cd0cedf68ef38e5d03e4a3 [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"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000022#include "llvm/ADT/SmallVector.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000023#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000025using namespace llvm;
26
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000027// The shift types for register controlled shifts in arm memory addressing
28enum ShiftType {
29 Lsl,
30 Lsr,
31 Asr,
32 Ror,
33 Rrx
34};
35
Chris Lattner3a697562010-10-28 17:20:03 +000036namespace {
37 struct ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000038
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000039class ARMAsmParser : public TargetAsmParser {
40 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000041 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000042
43private:
44 MCAsmParser &getParser() const { return Parser; }
45
46 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
47
48 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
49
50 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
51
Chris Lattner3a697562010-10-28 17:20:03 +000052 ARMOperand *MaybeParseRegister(bool ParseWriteBack);
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +000053 ARMOperand *ParseRegisterList();
Chris Lattner550276e2010-10-28 20:52:15 +000054 ARMOperand *ParseMemory();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000055
Kevin Enderby9c41fa82009-10-30 22:55:57 +000056 bool ParseMemoryOffsetReg(bool &Negative,
57 bool &OffsetRegShifted,
58 enum ShiftType &ShiftType,
59 const MCExpr *&ShiftAmount,
60 const MCExpr *&Offset,
61 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000062 int &OffsetRegNum,
63 SMLoc &E);
Kevin Enderby9c41fa82009-10-30 22:55:57 +000064
Sean Callanan76264762010-04-02 22:27:05 +000065 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000066
Chris Lattner550276e2010-10-28 20:52:15 +000067 ARMOperand *ParseOperand();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000068
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000069 bool ParseDirectiveWord(unsigned Size, SMLoc L);
70
Kevin Enderby515d5092009-10-15 20:48:48 +000071 bool ParseDirectiveThumb(SMLoc L);
72
73 bool ParseDirectiveThumbFunc(SMLoc L);
74
75 bool ParseDirectiveCode(SMLoc L);
76
77 bool ParseDirectiveSyntax(SMLoc L);
78
Chris Lattner7036f8b2010-09-29 01:42:58 +000079 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000080 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000081 MCStreamer &Out);
Jim Grosbach16c74252010-10-29 14:46:02 +000082
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000083 /// @name Auto-generated Match Functions
84 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000085
Chris Lattner0692ee62010-09-06 19:11:01 +000086#define GET_ASSEMBLER_HEADER
87#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000088
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000089 /// }
90
91
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000092public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000093 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
94 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {}
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000095
Benjamin Kramer38e59892010-07-14 22:38:02 +000096 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000097 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000098
99 virtual bool ParseDirective(AsmToken DirectiveID);
100};
Jim Grosbach16c74252010-10-29 14:46:02 +0000101} // end anonymous namespace
102
Chris Lattner3a697562010-10-28 17:20:03 +0000103namespace {
104
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000105/// ARMOperand - Instances of this class represent a parsed ARM machine
106/// instruction.
Chris Lattner76593892010-01-14 21:21:40 +0000107struct ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000108public:
109 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000110 CondCode,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000111 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000112 Memory,
113 Register,
114 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000115 } Kind;
116
Sean Callanan76264762010-04-02 22:27:05 +0000117 SMLoc StartLoc, EndLoc;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000118
119 union {
120 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000121 ARMCC::CondCodes Val;
122 } CC;
123
124 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000125 const char *Data;
126 unsigned Length;
127 } Tok;
128
129 struct {
130 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000131 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000132 } Reg;
133
Kevin Enderbycfe07242009-10-13 22:19:02 +0000134 struct {
135 const MCExpr *Val;
136 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000137
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000138 // This is for all forms of ARM address expressions
139 struct {
140 unsigned BaseRegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000141 unsigned OffsetRegNum; // used when OffsetIsReg is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000142 const MCExpr *Offset; // used when OffsetIsReg is false
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000143 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000144 enum ShiftType ShiftType; // used when OffsetRegShifted is true
145 unsigned
146 OffsetRegShifted : 1, // only used when OffsetIsReg is true
147 Preindexed : 1,
148 Postindexed : 1,
149 OffsetIsReg : 1,
150 Negative : 1, // only used when OffsetIsReg is true
151 Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000152 } Mem;
153
154 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000155
Sean Callanan76264762010-04-02 22:27:05 +0000156 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
157 Kind = o.Kind;
158 StartLoc = o.StartLoc;
159 EndLoc = o.EndLoc;
160 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000161 case CondCode:
162 CC = o.CC;
163 break;
Sean Callanan76264762010-04-02 22:27:05 +0000164 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000165 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000166 break;
167 case Register:
168 Reg = o.Reg;
169 break;
170 case Immediate:
171 Imm = o.Imm;
172 break;
173 case Memory:
174 Mem = o.Mem;
175 break;
176 }
177 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000178
Sean Callanan76264762010-04-02 22:27:05 +0000179 /// getStartLoc - Get the location of the first token of this operand.
180 SMLoc getStartLoc() const { return StartLoc; }
181 /// getEndLoc - Get the location of the last token of this operand.
182 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000183
Daniel Dunbar8462b302010-08-11 06:36:53 +0000184 ARMCC::CondCodes getCondCode() const {
185 assert(Kind == CondCode && "Invalid access!");
186 return CC.Val;
187 }
188
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000189 StringRef getToken() const {
190 assert(Kind == Token && "Invalid access!");
191 return StringRef(Tok.Data, Tok.Length);
192 }
193
194 unsigned getReg() const {
195 assert(Kind == Register && "Invalid access!");
196 return Reg.RegNum;
197 }
198
Kevin Enderbycfe07242009-10-13 22:19:02 +0000199 const MCExpr *getImm() const {
200 assert(Kind == Immediate && "Invalid access!");
201 return Imm.Val;
202 }
203
Daniel Dunbar8462b302010-08-11 06:36:53 +0000204 bool isCondCode() const { return Kind == CondCode; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000205 bool isImm() const { return Kind == Immediate; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000206 bool isReg() const { return Kind == Register; }
Chris Lattner14b93852010-10-29 00:27:31 +0000207 bool isToken() const { return Kind == Token; }
208 bool isMemory() const { return Kind == Memory; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000209
210 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000211 // Add as immediates when possible. Null MCExpr = 0.
212 if (Expr == 0)
213 Inst.addOperand(MCOperand::CreateImm(0));
214 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000215 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
216 else
217 Inst.addOperand(MCOperand::CreateExpr(Expr));
218 }
219
Daniel Dunbar8462b302010-08-11 06:36:53 +0000220 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000221 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000222 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000223 // FIXME: What belongs here?
224 Inst.addOperand(MCOperand::CreateReg(0));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000225 }
226
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000227 void addRegOperands(MCInst &Inst, unsigned N) const {
228 assert(N == 1 && "Invalid number of operands!");
229 Inst.addOperand(MCOperand::CreateReg(getReg()));
230 }
231
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000232 void addImmOperands(MCInst &Inst, unsigned N) const {
233 assert(N == 1 && "Invalid number of operands!");
234 addExpr(Inst, getImm());
235 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000236
237
Chris Lattner14b93852010-10-29 00:27:31 +0000238 bool isMemMode5() const {
Chris Lattner14b93852010-10-29 00:27:31 +0000239 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
Jim Grosbach80eb2332010-10-29 17:41:25 +0000240 Mem.Writeback || Mem.Negative)
Chris Lattner14b93852010-10-29 00:27:31 +0000241 return false;
Jim Grosbach80eb2332010-10-29 17:41:25 +0000242 // If there is an offset expression, make sure it's valid.
243 if (!Mem.Offset)
244 return true;
245 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
246 if (!CE)
247 return false;
248 // The offset must be a multiple of 4 in the range 0-1020.
249 int64_t Value = CE->getValue();
250 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
Chris Lattner14b93852010-10-29 00:27:31 +0000251 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000252
Chris Lattner14b93852010-10-29 00:27:31 +0000253 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
254 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000255
Chris Lattner14b93852010-10-29 00:27:31 +0000256 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
257 assert(!Mem.OffsetIsReg && "invalid mode 5 operand");
Jim Grosbach80eb2332010-10-29 17:41:25 +0000258 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
259 // the difference?
260 if (Mem.Offset) {
261 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
262 assert (CE && "non-constant mode 5 offset operand!");
263 // The MCInst offset operand doesn't include the low two bits (like
264 // the instruction encoding).
265 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
266 } else
267 Inst.addOperand(MCOperand::CreateImm(0));
Chris Lattner14b93852010-10-29 00:27:31 +0000268 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000269
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000270 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000271
Chris Lattner3a697562010-10-28 17:20:03 +0000272 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
273 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000274 Op->CC.Val = CC;
275 Op->StartLoc = S;
276 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000277 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000278 }
279
Chris Lattner3a697562010-10-28 17:20:03 +0000280 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
281 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000282 Op->Tok.Data = Str.data();
283 Op->Tok.Length = Str.size();
284 Op->StartLoc = S;
285 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000286 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000287 }
288
Chris Lattner3a697562010-10-28 17:20:03 +0000289 static ARMOperand *CreateReg(unsigned RegNum, bool Writeback, SMLoc S,
290 SMLoc E) {
291 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000292 Op->Reg.RegNum = RegNum;
293 Op->Reg.Writeback = Writeback;
Sean Callanan76264762010-04-02 22:27:05 +0000294 Op->StartLoc = S;
295 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000296 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000297 }
298
Chris Lattner3a697562010-10-28 17:20:03 +0000299 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
300 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000301 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000302 Op->StartLoc = S;
303 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000304 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000305 }
306
Chris Lattner3a697562010-10-28 17:20:03 +0000307 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
308 const MCExpr *Offset, unsigned OffsetRegNum,
309 bool OffsetRegShifted, enum ShiftType ShiftType,
310 const MCExpr *ShiftAmount, bool Preindexed,
311 bool Postindexed, bool Negative, bool Writeback,
312 SMLoc S, SMLoc E) {
313 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000314 Op->Mem.BaseRegNum = BaseRegNum;
315 Op->Mem.OffsetIsReg = OffsetIsReg;
316 Op->Mem.Offset = Offset;
317 Op->Mem.OffsetRegNum = OffsetRegNum;
318 Op->Mem.OffsetRegShifted = OffsetRegShifted;
319 Op->Mem.ShiftType = ShiftType;
320 Op->Mem.ShiftAmount = ShiftAmount;
321 Op->Mem.Preindexed = Preindexed;
322 Op->Mem.Postindexed = Postindexed;
323 Op->Mem.Negative = Negative;
324 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000325
Sean Callanan76264762010-04-02 22:27:05 +0000326 Op->StartLoc = S;
327 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000328 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000329 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000330
Chris Lattner3a697562010-10-28 17:20:03 +0000331private:
332 ARMOperand(KindTy K) : Kind(K) {}
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000333};
334
335} // end anonymous namespace.
336
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000337void ARMOperand::dump(raw_ostream &OS) const {
338 switch (Kind) {
339 case CondCode:
340 OS << ARMCondCodeToString(getCondCode());
341 break;
342 case Immediate:
343 getImm()->print(OS);
344 break;
345 case Memory:
346 OS << "<memory>";
347 break;
348 case Register:
349 OS << "<register " << getReg() << ">";
350 break;
351 case Token:
352 OS << "'" << getToken() << "'";
353 break;
354 }
355}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000356
357/// @name Auto-generated Match Functions
358/// {
359
360static unsigned MatchRegisterName(StringRef Name);
361
362/// }
363
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000364/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattner3a697562010-10-28 17:20:03 +0000365/// and if it is a register name the token is eaten and a Reg operand is created
366/// and returned. Otherwise return null.
367///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000368/// TODO this is likely to change to allow different register types and or to
369/// parse for a specific register type.
Chris Lattner3a697562010-10-28 17:20:03 +0000370ARMOperand *ARMAsmParser::MaybeParseRegister(bool ParseWriteBack) {
Sean Callanan76264762010-04-02 22:27:05 +0000371 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000372 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000373 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
374
375 // FIXME: Validate register for the current architecture; we have to do
376 // validation later, so maybe there is no need for this here.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000377 int RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000378
379 RegNum = MatchRegisterName(Tok.getString());
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000380 if (RegNum == -1)
Chris Lattner3a697562010-10-28 17:20:03 +0000381 return 0;
Jim Grosbach16c74252010-10-29 14:46:02 +0000382
Sean Callanan76264762010-04-02 22:27:05 +0000383 S = Tok.getLoc();
Jim Grosbach16c74252010-10-29 14:46:02 +0000384
Sean Callananb9a25b72010-01-19 20:27:46 +0000385 Parser.Lex(); // Eat identifier token.
Jim Grosbach16c74252010-10-29 14:46:02 +0000386
Sean Callanan76264762010-04-02 22:27:05 +0000387 E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000388
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000389 bool Writeback = false;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000390 if (ParseWriteBack) {
Sean Callanan18b83232010-01-19 21:44:56 +0000391 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000392 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000393 E = ExclaimTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000394 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000395 Parser.Lex(); // Eat exclaim token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000396 }
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000397 }
398
Chris Lattner3a697562010-10-28 17:20:03 +0000399 return ARMOperand::CreateReg(RegNum, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000400}
401
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000402/// Parse a register list, return it if successful else return null. The first
403/// token must be a '{' when called.
404ARMOperand *ARMAsmParser::ParseRegisterList() {
Sean Callanan76264762010-04-02 22:27:05 +0000405 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000406 assert(Parser.getTok().is(AsmToken::LCurly) &&
Kevin Enderbycfe07242009-10-13 22:19:02 +0000407 "Token is not an Left Curly Brace");
Sean Callanan76264762010-04-02 22:27:05 +0000408 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000409 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000410
Sean Callanan18b83232010-01-19 21:44:56 +0000411 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000412 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000413 if (RegTok.isNot(AsmToken::Identifier)) {
414 Error(RegLoc, "register expected");
415 return 0;
416 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000417 int RegNum = MatchRegisterName(RegTok.getString());
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000418 if (RegNum == -1) {
419 Error(RegLoc, "register expected");
420 return 0;
421 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000422
Sean Callananb9a25b72010-01-19 20:27:46 +0000423 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000424 unsigned RegList = 1 << RegNum;
425
426 int HighRegNum = RegNum;
427 // TODO ranges like "{Rn-Rm}"
Sean Callanan18b83232010-01-19 21:44:56 +0000428 while (Parser.getTok().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000429 Parser.Lex(); // Eat comma token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000430
Sean Callanan18b83232010-01-19 21:44:56 +0000431 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000432 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000433 if (RegTok.isNot(AsmToken::Identifier)) {
434 Error(RegLoc, "register expected");
435 return 0;
436 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000437 int RegNum = MatchRegisterName(RegTok.getString());
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000438 if (RegNum == -1) {
439 Error(RegLoc, "register expected");
440 return 0;
441 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000442
443 if (RegList & (1 << RegNum))
444 Warning(RegLoc, "register duplicated in register list");
445 else if (RegNum <= HighRegNum)
446 Warning(RegLoc, "register not in ascending order in register list");
447 RegList |= 1 << RegNum;
448 HighRegNum = RegNum;
449
Sean Callananb9a25b72010-01-19 20:27:46 +0000450 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000451 }
Sean Callanan18b83232010-01-19 21:44:56 +0000452 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000453 if (RCurlyTok.isNot(AsmToken::RCurly)) {
454 Error(RCurlyTok.getLoc(), "'}' expected");
455 return 0;
456 }
Sean Callanan76264762010-04-02 22:27:05 +0000457 E = RCurlyTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000458 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000459
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000460 // FIXME: Need to return an operand!
461 Error(E, "FIXME: register list parsing not implemented");
462 return 0;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000463}
464
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000465/// Parse an arm memory expression, return false if successful else return true
466/// or an error. The first token must be a '[' when called.
467/// TODO Only preindexing and postindexing addressing are started, unindexed
468/// with option, etc are still to do.
Chris Lattner550276e2010-10-28 20:52:15 +0000469ARMOperand *ARMAsmParser::ParseMemory() {
Sean Callanan76264762010-04-02 22:27:05 +0000470 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000471 assert(Parser.getTok().is(AsmToken::LBrac) &&
Kevin Enderby6bd266e2009-10-12 22:51:49 +0000472 "Token is not an Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000473 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000474 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000475
Sean Callanan18b83232010-01-19 21:44:56 +0000476 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000477 if (BaseRegTok.isNot(AsmToken::Identifier)) {
478 Error(BaseRegTok.getLoc(), "register expected");
479 return 0;
480 }
481 int BaseRegNum = 0;
Benjamin Kramer61a4d562010-10-29 09:43:39 +0000482 if (ARMOperand *Op = MaybeParseRegister(false)) {
Chris Lattner550276e2010-10-28 20:52:15 +0000483 BaseRegNum = Op->getReg();
Benjamin Kramer61a4d562010-10-29 09:43:39 +0000484 delete Op;
485 } else {
Chris Lattner550276e2010-10-28 20:52:15 +0000486 Error(BaseRegTok.getLoc(), "register expected");
487 return 0;
488 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000489
490 bool Preindexed = false;
491 bool Postindexed = false;
492 bool OffsetIsReg = false;
493 bool Negative = false;
494 bool Writeback = false;
495
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000496 // First look for preindexed address forms, that is after the "[Rn" we now
497 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000498 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000499 if (Tok.is(AsmToken::Comma)) {
500 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000501 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000502 int OffsetRegNum;
503 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000504 enum ShiftType ShiftType;
505 const MCExpr *ShiftAmount;
506 const MCExpr *Offset;
Chris Lattner550276e2010-10-28 20:52:15 +0000507 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
508 Offset, OffsetIsReg, OffsetRegNum, E))
509 return 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000510 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000511 if (RBracTok.isNot(AsmToken::RBrac)) {
512 Error(RBracTok.getLoc(), "']' expected");
513 return 0;
514 }
Sean Callanan76264762010-04-02 22:27:05 +0000515 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000516 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000517
Sean Callanan18b83232010-01-19 21:44:56 +0000518 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000519 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000520 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000521 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000522 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000523 }
Chris Lattner550276e2010-10-28 20:52:15 +0000524 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
525 OffsetRegShifted, ShiftType, ShiftAmount,
526 Preindexed, Postindexed, Negative, Writeback,
527 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000528 }
529 // The "[Rn" we have so far was not followed by a comma.
530 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000531 // If there's anything other than the right brace, this is a post indexing
532 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000533 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000534 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000535
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000536 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000537 bool OffsetRegShifted = false;
538 enum ShiftType ShiftType;
539 const MCExpr *ShiftAmount;
Chris Lattner14b93852010-10-29 00:27:31 +0000540 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000541
Sean Callanan18b83232010-01-19 21:44:56 +0000542 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000543 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000544 Postindexed = true;
545 Writeback = true;
Chris Lattner550276e2010-10-28 20:52:15 +0000546 if (NextTok.isNot(AsmToken::Comma)) {
547 Error(NextTok.getLoc(), "',' expected");
548 return 0;
549 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000550 Parser.Lex(); // Eat comma token.
Chris Lattner550276e2010-10-28 20:52:15 +0000551 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000552 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000553 E))
554 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000555 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000556
Chris Lattner550276e2010-10-28 20:52:15 +0000557 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
558 OffsetRegShifted, ShiftType, ShiftAmount,
559 Preindexed, Postindexed, Negative, Writeback,
560 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000561 }
562
Chris Lattner550276e2010-10-28 20:52:15 +0000563 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000564}
565
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000566/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
567/// we will parse the following (were +/- means that a plus or minus is
568/// optional):
569/// +/-Rm
570/// +/-Rm, shift
571/// #offset
572/// we return false on success or an error otherwise.
573bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000574 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000575 enum ShiftType &ShiftType,
576 const MCExpr *&ShiftAmount,
577 const MCExpr *&Offset,
578 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000579 int &OffsetRegNum,
580 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000581 Negative = false;
582 OffsetRegShifted = false;
583 OffsetIsReg = false;
584 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000585 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000586 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000587 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000588 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000589 else if (NextTok.is(AsmToken::Minus)) {
590 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000591 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000592 }
593 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000594 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000595 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattner550276e2010-10-28 20:52:15 +0000596 if (ARMOperand *Op = MaybeParseRegister(false)) {
597 OffsetIsReg = true;
Sean Callanan76264762010-04-02 22:27:05 +0000598 E = Op->getEndLoc();
599 OffsetRegNum = Op->getReg();
Chris Lattner550276e2010-10-28 20:52:15 +0000600 delete Op;
Sean Callanan76264762010-04-02 22:27:05 +0000601 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000602 }
603 // If we parsed a register as the offset then their can be a shift after that
604 if (OffsetRegNum != -1) {
605 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000606 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000607 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000608 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000609
Sean Callanan18b83232010-01-19 21:44:56 +0000610 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000611 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000612 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000613 OffsetRegShifted = true;
614 }
615 }
616 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
617 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000618 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000619 if (HashTok.isNot(AsmToken::Hash))
620 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000621
Sean Callananb9a25b72010-01-19 20:27:46 +0000622 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000623
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000624 if (getParser().ParseExpression(Offset))
625 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000626 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000627 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000628 return false;
629}
630
631/// ParseShift as one of these two:
632/// ( lsl | lsr | asr | ror ) , # shift_amount
633/// rrx
634/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000635bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000636 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000637 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000638 if (Tok.isNot(AsmToken::Identifier))
639 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000640 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000641 if (ShiftName == "lsl" || ShiftName == "LSL")
642 St = Lsl;
643 else if (ShiftName == "lsr" || ShiftName == "LSR")
644 St = Lsr;
645 else if (ShiftName == "asr" || ShiftName == "ASR")
646 St = Asr;
647 else if (ShiftName == "ror" || ShiftName == "ROR")
648 St = Ror;
649 else if (ShiftName == "rrx" || ShiftName == "RRX")
650 St = Rrx;
651 else
652 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000653 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000654
655 // Rrx stands alone.
656 if (St == Rrx)
657 return false;
658
659 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000660 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000661 if (HashTok.isNot(AsmToken::Hash))
662 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000663 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000664
665 if (getParser().ParseExpression(ShiftAmount))
666 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000667
668 return false;
669}
670
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000671/// Parse a arm instruction operand. For now this parses the operand regardless
672/// of the mnemonic.
Chris Lattner550276e2010-10-28 20:52:15 +0000673ARMOperand *ARMAsmParser::ParseOperand() {
Sean Callanan76264762010-04-02 22:27:05 +0000674 SMLoc S, E;
Jim Grosbach16c74252010-10-29 14:46:02 +0000675
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000676 switch (getLexer().getKind()) {
677 case AsmToken::Identifier:
Chris Lattner550276e2010-10-28 20:52:15 +0000678 if (ARMOperand *Op = MaybeParseRegister(true))
679 return Op;
Jim Grosbach16c74252010-10-29 14:46:02 +0000680
Kevin Enderby515d5092009-10-15 20:48:48 +0000681 // This was not a register so parse other operands that start with an
682 // identifier (like labels) as expressions and create them as immediates.
683 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000684 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000685 if (getParser().ParseExpression(IdVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000686 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000687 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000688 return ARMOperand::CreateImm(IdVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000689 case AsmToken::LBrac:
Chris Lattner550276e2010-10-28 20:52:15 +0000690 return ParseMemory();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000691 case AsmToken::LCurly:
Chris Lattner550276e2010-10-28 20:52:15 +0000692 return ParseRegisterList();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000693 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000694 // #42 -> immediate.
695 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000696 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000697 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000698 const MCExpr *ImmVal;
699 if (getParser().ParseExpression(ImmVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000700 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000701 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000702 return ARMOperand::CreateImm(ImmVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000703 default:
Chris Lattner550276e2010-10-28 20:52:15 +0000704 Error(Parser.getTok().getLoc(), "unexpected token in operand");
705 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000706 }
707}
708
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000709/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000710bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000711 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000712 // Create the leading tokens for the mnemonic, split by '.' characters.
713 size_t Start = 0, Next = Name.find('.');
714 StringRef Head = Name.slice(Start, Next);
715
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000716 // Determine the predicate, if any.
717 //
718 // FIXME: We need a way to check whether a prefix supports predication,
719 // otherwise we will end up with an ambiguity for instructions that happen to
720 // end with a predicate name.
Jim Grosbach3df518e2010-10-29 21:56:51 +0000721 // FIXME: Likewise, some arithmetic instructions have an 's' prefix which
722 // indicates to update the condition codes. Those instructions have an
723 // additional immediate operand which encodes the prefix as reg0 or CPSR.
724 // Just checking for a suffix of 's' definitely creates ambiguities; e.g,
725 // the SMMLS instruction.
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000726 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
727 .Case("eq", ARMCC::EQ)
728 .Case("ne", ARMCC::NE)
729 .Case("hs", ARMCC::HS)
730 .Case("lo", ARMCC::LO)
731 .Case("mi", ARMCC::MI)
732 .Case("pl", ARMCC::PL)
733 .Case("vs", ARMCC::VS)
734 .Case("vc", ARMCC::VC)
735 .Case("hi", ARMCC::HI)
736 .Case("ls", ARMCC::LS)
737 .Case("ge", ARMCC::GE)
738 .Case("lt", ARMCC::LT)
739 .Case("gt", ARMCC::GT)
740 .Case("le", ARMCC::LE)
741 .Case("al", ARMCC::AL)
742 .Default(~0U);
Jim Grosbach16c74252010-10-29 14:46:02 +0000743
Bill Wendling52925b62010-10-29 23:50:21 +0000744 if (CC != ~0U) {
745 if (CC == ARMCC::LS &&
746 (Head.compare("vmls") == 0 || Head.compare("vnmls") == 0)) {
747 CC = ARMCC::AL;
748 } else {
749 Head = Head.slice(0, Head.size() - 2);
750 }
751 } else {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000752 CC = ARMCC::AL;
Bill Wendling52925b62010-10-29 23:50:21 +0000753 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000754
Chris Lattner3a697562010-10-28 17:20:03 +0000755 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
756 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000757
758 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000759 while (Next != StringRef::npos) {
760 Start = Next;
761 Next = Name.find('.', Start + 1);
762 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000763
Chris Lattner3a697562010-10-28 17:20:03 +0000764 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000765 }
766
767 // Read the remaining operands.
768 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000769 // Read the first operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000770 if (ARMOperand *Op = ParseOperand())
771 Operands.push_back(Op);
772 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000773 Parser.EatToEndOfStatement();
774 return true;
775 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000776
777 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000778 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000779
780 // Parse and remember the operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000781 if (ARMOperand *Op = ParseOperand())
782 Operands.push_back(Op);
783 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000784 Parser.EatToEndOfStatement();
785 return true;
786 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000787 }
788 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000789
Chris Lattnercbf8a982010-09-11 16:18:25 +0000790 if (getLexer().isNot(AsmToken::EndOfStatement)) {
791 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000792 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000793 }
Chris Lattner34e53142010-09-08 05:10:46 +0000794 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000795 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000796}
797
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000798bool ARMAsmParser::
799MatchAndEmitInstruction(SMLoc IDLoc,
800 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
801 MCStreamer &Out) {
802 MCInst Inst;
803 unsigned ErrorInfo;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000804 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
805 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000806 Out.EmitInstruction(Inst);
807 return false;
Jim Grosbach16c74252010-10-29 14:46:02 +0000808
Chris Lattnere73d4f82010-10-28 21:41:58 +0000809 case Match_MissingFeature:
810 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
811 return true;
812 case Match_InvalidOperand: {
813 SMLoc ErrorLoc = IDLoc;
814 if (ErrorInfo != ~0U) {
815 if (ErrorInfo >= Operands.size())
816 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +0000817
Chris Lattnere73d4f82010-10-28 21:41:58 +0000818 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
819 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
820 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000821
Chris Lattnere73d4f82010-10-28 21:41:58 +0000822 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000823 }
Chris Lattnere73d4f82010-10-28 21:41:58 +0000824 case Match_MnemonicFail:
825 return Error(IDLoc, "unrecognized instruction mnemonic");
826 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000827
Eric Christopherc223e2b2010-10-29 09:26:59 +0000828 llvm_unreachable("Implement any new match types added!");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000829}
830
831
832
Kevin Enderby515d5092009-10-15 20:48:48 +0000833/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000834bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
835 StringRef IDVal = DirectiveID.getIdentifier();
836 if (IDVal == ".word")
837 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000838 else if (IDVal == ".thumb")
839 return ParseDirectiveThumb(DirectiveID.getLoc());
840 else if (IDVal == ".thumb_func")
841 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
842 else if (IDVal == ".code")
843 return ParseDirectiveCode(DirectiveID.getLoc());
844 else if (IDVal == ".syntax")
845 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000846 return true;
847}
848
849/// ParseDirectiveWord
850/// ::= .word [ expression (, expression)* ]
851bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
852 if (getLexer().isNot(AsmToken::EndOfStatement)) {
853 for (;;) {
854 const MCExpr *Value;
855 if (getParser().ParseExpression(Value))
856 return true;
857
Chris Lattneraaec2052010-01-19 19:46:13 +0000858 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000859
860 if (getLexer().is(AsmToken::EndOfStatement))
861 break;
Jim Grosbach16c74252010-10-29 14:46:02 +0000862
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000863 // FIXME: Improve diagnostic.
864 if (getLexer().isNot(AsmToken::Comma))
865 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000866 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000867 }
868 }
869
Sean Callananb9a25b72010-01-19 20:27:46 +0000870 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000871 return false;
872}
873
Kevin Enderby515d5092009-10-15 20:48:48 +0000874/// ParseDirectiveThumb
875/// ::= .thumb
876bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
877 if (getLexer().isNot(AsmToken::EndOfStatement))
878 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000879 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000880
881 // TODO: set thumb mode
882 // TODO: tell the MC streamer the mode
883 // getParser().getStreamer().Emit???();
884 return false;
885}
886
887/// ParseDirectiveThumbFunc
888/// ::= .thumbfunc symbol_name
889bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000890 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000891 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
892 return Error(L, "unexpected token in .syntax directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000893 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000894
895 if (getLexer().isNot(AsmToken::EndOfStatement))
896 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000897 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000898
899 // TODO: mark symbol as a thumb symbol
900 // getParser().getStreamer().Emit???();
901 return false;
902}
903
904/// ParseDirectiveSyntax
905/// ::= .syntax unified | divided
906bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000907 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000908 if (Tok.isNot(AsmToken::Identifier))
909 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000910 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000911 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000912 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000913 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000914 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000915 else
916 return Error(L, "unrecognized syntax mode in .syntax directive");
917
918 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000919 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000920 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000921
922 // TODO tell the MC streamer the mode
923 // getParser().getStreamer().Emit???();
924 return false;
925}
926
927/// ParseDirectiveCode
928/// ::= .code 16 | 32
929bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000930 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000931 if (Tok.isNot(AsmToken::Integer))
932 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000933 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +0000934 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +0000935 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000936 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +0000937 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000938 else
939 return Error(L, "invalid operand to .code directive");
940
941 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000942 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000943 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000944
945 // TODO tell the MC streamer the mode
946 // getParser().getStreamer().Emit???();
947 return false;
948}
949
Sean Callanan90b70972010-04-07 20:29:34 +0000950extern "C" void LLVMInitializeARMAsmLexer();
951
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000952/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000953extern "C" void LLVMInitializeARMAsmParser() {
954 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
955 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +0000956 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000957}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000958
Chris Lattner0692ee62010-09-06 19:11:01 +0000959#define GET_REGISTER_MATCHER
960#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000961#include "ARMGenAsmMatcher.inc"