blob: 12225b00ed0556c03541b6c73900be24f6f1253e [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"
Bill Wendling92b5a2e2010-11-03 01:49:29 +000011#include "ARMAddressingModes.h"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000012#include "ARMSubtarget.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000013#include "llvm/MC/MCParser/MCAsmLexer.h"
14#include "llvm/MC/MCParser/MCAsmParser.h"
15#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Jim Grosbach642fc9c2010-11-05 22:33:53 +000016#include "llvm/MC/MCContext.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000017#include "llvm/MC/MCStreamer.h"
18#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCInst.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000020#include "llvm/Target/TargetRegistry.h"
21#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000022#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000023#include "llvm/Support/raw_ostream.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/ADT/SmallVector.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000025#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000026#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000027using namespace llvm;
28
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000029// The shift types for register controlled shifts in arm memory addressing
30enum ShiftType {
31 Lsl,
32 Lsr,
33 Asr,
34 Ror,
35 Rrx
36};
37
Chris Lattner3a697562010-10-28 17:20:03 +000038namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000039
40class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000041
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000042class ARMAsmParser : public TargetAsmParser {
43 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000044 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000045
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000046 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000047 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
48
49 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000050 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
51
Chris Lattnere5658fa2010-10-30 04:09:10 +000052 int TryParseRegister();
53 ARMOperand *TryParseRegisterWithWriteBack();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +000054 ARMOperand *ParseRegisterList();
Chris Lattner550276e2010-10-28 20:52:15 +000055 ARMOperand *ParseMemory();
Bill Wendling146018f2010-11-06 21:42:12 +000056 ARMOperand *ParseOperand();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000057
Kevin Enderby9c41fa82009-10-30 22:55:57 +000058 bool ParseMemoryOffsetReg(bool &Negative,
59 bool &OffsetRegShifted,
60 enum ShiftType &ShiftType,
61 const MCExpr *&ShiftAmount,
62 const MCExpr *&Offset,
63 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000064 int &OffsetRegNum,
65 SMLoc &E);
Sean Callanan76264762010-04-02 22:27:05 +000066 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000067 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000068 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000069 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000070 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000071 bool ParseDirectiveSyntax(SMLoc L);
72
Chris Lattner7036f8b2010-09-29 01:42:58 +000073 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000074 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000075 MCStreamer &Out);
Jim Grosbach16c74252010-10-29 14:46:02 +000076
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000077 /// @name Auto-generated Match Functions
78 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000079
Chris Lattner0692ee62010-09-06 19:11:01 +000080#define GET_ASSEMBLER_HEADER
81#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000082
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000083 /// }
84
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000085public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000086 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +000087 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
88 // Initialize the set of available features.
89 setAvailableFeatures(ComputeAvailableFeatures(
90 &TM.getSubtarget<ARMSubtarget>()));
91 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000092
Benjamin Kramer38e59892010-07-14 22:38:02 +000093 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000094 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000095 virtual bool ParseDirective(AsmToken DirectiveID);
96};
Jim Grosbach16c74252010-10-29 14:46:02 +000097} // end anonymous namespace
98
Chris Lattner3a697562010-10-28 17:20:03 +000099namespace {
100
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000101/// ARMOperand - Instances of this class represent a parsed ARM machine
102/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000103class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000104 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000105 CondCode,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000106 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000107 Memory,
108 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000109 RegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000110 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000111 } Kind;
112
Sean Callanan76264762010-04-02 22:27:05 +0000113 SMLoc StartLoc, EndLoc;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000114
115 union {
116 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000117 ARMCC::CondCodes Val;
118 } CC;
119
120 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000121 const char *Data;
122 unsigned Length;
123 } Tok;
124
125 struct {
126 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000127 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000128 } Reg;
129
Bill Wendling8155e5b2010-11-06 22:19:43 +0000130 struct {
Bill Wendling5fa22a12010-11-09 23:28:44 +0000131 SmallVector<unsigned, 32> *Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000132 } RegList;
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;
Bill Wendling146018f2010-11-06 21:42:12 +0000141 unsigned OffsetRegNum; // used when OffsetIsReg is true
142 const MCExpr *Offset; // used when OffsetIsReg is false
143 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
144 enum ShiftType ShiftType; // used when OffsetRegShifted is true
145 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
146 unsigned Preindexed : 1;
147 unsigned Postindexed : 1;
148 unsigned OffsetIsReg : 1;
149 unsigned Negative : 1; // only used when OffsetIsReg is true
150 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000151 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000152 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000153
Bill Wendling146018f2010-11-06 21:42:12 +0000154 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
155public:
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;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000170 case RegisterList:
171 RegList = o.RegList;
172 break;
Sean Callanan76264762010-04-02 22:27:05 +0000173 case Immediate:
174 Imm = o.Imm;
175 break;
176 case Memory:
177 Mem = o.Mem;
178 break;
179 }
180 }
Bill Wendlingc3236752010-11-09 22:51:42 +0000181 ~ARMOperand() {
182 if (isRegList())
183 delete RegList.Registers;
184 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000185
Sean Callanan76264762010-04-02 22:27:05 +0000186 /// getStartLoc - Get the location of the first token of this operand.
187 SMLoc getStartLoc() const { return StartLoc; }
188 /// getEndLoc - Get the location of the last token of this operand.
189 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000190
Daniel Dunbar8462b302010-08-11 06:36:53 +0000191 ARMCC::CondCodes getCondCode() const {
192 assert(Kind == CondCode && "Invalid access!");
193 return CC.Val;
194 }
195
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000196 StringRef getToken() const {
197 assert(Kind == Token && "Invalid access!");
198 return StringRef(Tok.Data, Tok.Length);
199 }
200
201 unsigned getReg() const {
Bill Wendling7729e062010-11-09 22:44:22 +0000202 assert(Kind == Register && "Invalid access!");
203 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000204 }
205
Bill Wendling5fa22a12010-11-09 23:28:44 +0000206 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000207 assert(Kind == RegisterList && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000208 return *RegList.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000209 }
210
Kevin Enderbycfe07242009-10-13 22:19:02 +0000211 const MCExpr *getImm() const {
212 assert(Kind == Immediate && "Invalid access!");
213 return Imm.Val;
214 }
215
Daniel Dunbar8462b302010-08-11 06:36:53 +0000216 bool isCondCode() const { return Kind == CondCode; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000217 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000218 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000219 bool isRegList() const { return Kind == RegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000220 bool isToken() const { return Kind == Token; }
221 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000222 bool isMemMode5() const {
223 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
224 Mem.Writeback || Mem.Negative)
225 return false;
226 // If there is an offset expression, make sure it's valid.
227 if (!Mem.Offset)
228 return true;
229 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
230 if (!CE)
231 return false;
232 // The offset must be a multiple of 4 in the range 0-1020.
233 int64_t Value = CE->getValue();
234 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
235 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000236
237 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000238 // Add as immediates when possible. Null MCExpr = 0.
239 if (Expr == 0)
240 Inst.addOperand(MCOperand::CreateImm(0));
241 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000242 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
243 else
244 Inst.addOperand(MCOperand::CreateExpr(Expr));
245 }
246
Daniel Dunbar8462b302010-08-11 06:36:53 +0000247 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000248 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000249 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000250 // FIXME: What belongs here?
251 Inst.addOperand(MCOperand::CreateReg(0));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000252 }
253
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000254 void addRegOperands(MCInst &Inst, unsigned N) const {
255 assert(N == 1 && "Invalid number of operands!");
256 Inst.addOperand(MCOperand::CreateReg(getReg()));
257 }
258
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000259 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000260 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000261 const SmallVectorImpl<unsigned> &RegList = getRegList();
262 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000263 I = RegList.begin(), E = RegList.end(); I != E; ++I)
264 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000265 }
266
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000267 void addImmOperands(MCInst &Inst, unsigned N) const {
268 assert(N == 1 && "Invalid number of operands!");
269 addExpr(Inst, getImm());
270 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000271
Chris Lattner14b93852010-10-29 00:27:31 +0000272 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
273 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000274
Chris Lattner14b93852010-10-29 00:27:31 +0000275 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlinga60f1572010-11-06 10:48:18 +0000276 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000277
Jim Grosbach80eb2332010-10-29 17:41:25 +0000278 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
279 // the difference?
280 if (Mem.Offset) {
281 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000282 assert(CE && "Non-constant mode 5 offset operand!");
283
Jim Grosbach80eb2332010-10-29 17:41:25 +0000284 // The MCInst offset operand doesn't include the low two bits (like
285 // the instruction encoding).
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000286 int64_t Offset = CE->getValue() / 4;
287 if (Offset >= 0)
288 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
289 Offset)));
290 else
291 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
292 -Offset)));
293 } else {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000294 Inst.addOperand(MCOperand::CreateImm(0));
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000295 }
Chris Lattner14b93852010-10-29 00:27:31 +0000296 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000297
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000298 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000299
Chris Lattner3a697562010-10-28 17:20:03 +0000300 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
301 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000302 Op->CC.Val = CC;
303 Op->StartLoc = S;
304 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000305 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000306 }
307
Chris Lattner3a697562010-10-28 17:20:03 +0000308 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
309 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000310 Op->Tok.Data = Str.data();
311 Op->Tok.Length = Str.size();
312 Op->StartLoc = S;
313 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000314 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000315 }
316
Chris Lattner3a697562010-10-28 17:20:03 +0000317 static ARMOperand *CreateReg(unsigned RegNum, bool Writeback, SMLoc S,
318 SMLoc E) {
319 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000320 Op->Reg.RegNum = RegNum;
321 Op->Reg.Writeback = Writeback;
Sean Callanan76264762010-04-02 22:27:05 +0000322 Op->StartLoc = S;
323 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000324 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000325 }
326
Bill Wendling7729e062010-11-09 22:44:22 +0000327 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000328 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Bill Wendling7729e062010-11-09 22:44:22 +0000329 SMLoc S, SMLoc E) {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000330 ARMOperand *Op = new ARMOperand(RegisterList);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000331 Op->RegList.Registers = new SmallVector<unsigned, 32>();
332 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000333 I = Regs.begin(), E = Regs.end(); I != E; ++I)
334 Op->RegList.Registers->push_back(I->first);
335 std::sort(Op->RegList.Registers->begin(), Op->RegList.Registers->end());
Bill Wendling8d5acb72010-11-06 19:56:04 +0000336 Op->StartLoc = S;
337 Op->EndLoc = E;
338 return Op;
339 }
340
Chris Lattner3a697562010-10-28 17:20:03 +0000341 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
342 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000343 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000344 Op->StartLoc = S;
345 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000346 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000347 }
348
Chris Lattner3a697562010-10-28 17:20:03 +0000349 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
350 const MCExpr *Offset, unsigned OffsetRegNum,
351 bool OffsetRegShifted, enum ShiftType ShiftType,
352 const MCExpr *ShiftAmount, bool Preindexed,
353 bool Postindexed, bool Negative, bool Writeback,
354 SMLoc S, SMLoc E) {
355 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000356 Op->Mem.BaseRegNum = BaseRegNum;
357 Op->Mem.OffsetIsReg = OffsetIsReg;
358 Op->Mem.Offset = Offset;
359 Op->Mem.OffsetRegNum = OffsetRegNum;
360 Op->Mem.OffsetRegShifted = OffsetRegShifted;
361 Op->Mem.ShiftType = ShiftType;
362 Op->Mem.ShiftAmount = ShiftAmount;
363 Op->Mem.Preindexed = Preindexed;
364 Op->Mem.Postindexed = Postindexed;
365 Op->Mem.Negative = Negative;
366 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000367
Sean Callanan76264762010-04-02 22:27:05 +0000368 Op->StartLoc = S;
369 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000370 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000371 }
372};
373
374} // end anonymous namespace.
375
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000376void ARMOperand::dump(raw_ostream &OS) const {
377 switch (Kind) {
378 case CondCode:
379 OS << ARMCondCodeToString(getCondCode());
380 break;
381 case Immediate:
382 getImm()->print(OS);
383 break;
384 case Memory:
385 OS << "<memory>";
386 break;
387 case Register:
388 OS << "<register " << getReg() << ">";
389 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000390 case RegisterList: {
391 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000392
Bill Wendling5fa22a12010-11-09 23:28:44 +0000393 const SmallVectorImpl<unsigned> &RegList = getRegList();
394 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000395 I = RegList.begin(), E = RegList.end(); I != E; ) {
396 OS << *I;
397 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000398 }
399
400 OS << ">";
401 break;
402 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000403 case Token:
404 OS << "'" << getToken() << "'";
405 break;
406 }
407}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000408
409/// @name Auto-generated Match Functions
410/// {
411
412static unsigned MatchRegisterName(StringRef Name);
413
414/// }
415
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000416/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000417/// and if it is a register name the token is eaten and the register number is
418/// returned. Otherwise return -1.
419///
420int ARMAsmParser::TryParseRegister() {
421 const AsmToken &Tok = Parser.getTok();
422 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000423
Chris Lattnere5658fa2010-10-30 04:09:10 +0000424 // FIXME: Validate register for the current architecture; we have to do
425 // validation later, so maybe there is no need for this here.
Bill Wendlingd68fd9c2010-11-06 10:45:34 +0000426 unsigned RegNum = MatchRegisterName(Tok.getString());
427 if (RegNum == 0)
Chris Lattnere5658fa2010-10-30 04:09:10 +0000428 return -1;
429 Parser.Lex(); // Eat identifier token.
430 return RegNum;
431}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000432
433
Chris Lattnere5658fa2010-10-30 04:09:10 +0000434/// Try to parse a register name. The token must be an Identifier when called,
435/// and if it is a register name the token is eaten and the register number is
436/// returned. Otherwise return -1.
Chris Lattner3a697562010-10-28 17:20:03 +0000437///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000438/// TODO this is likely to change to allow different register types and or to
439/// parse for a specific register type.
Chris Lattnere5658fa2010-10-30 04:09:10 +0000440ARMOperand *ARMAsmParser::TryParseRegisterWithWriteBack() {
441 SMLoc S = Parser.getTok().getLoc();
442 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000443 if (RegNo == -1)
444 return 0;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000445
Chris Lattnere5658fa2010-10-30 04:09:10 +0000446 SMLoc E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000447
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000448 bool Writeback = false;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000449 const AsmToken &ExclaimTok = Parser.getTok();
450 if (ExclaimTok.is(AsmToken::Exclaim)) {
451 E = ExclaimTok.getLoc();
452 Writeback = true;
453 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000454 }
455
Chris Lattnere5658fa2010-10-30 04:09:10 +0000456 return ARMOperand::CreateReg(RegNo, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000457}
458
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000459/// Parse a register list, return it if successful else return null. The first
460/// token must be a '{' when called.
461ARMOperand *ARMAsmParser::ParseRegisterList() {
Sean Callanan18b83232010-01-19 21:44:56 +0000462 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000463 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000464 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000465
Bill Wendling7729e062010-11-09 22:44:22 +0000466 // Read the rest of the registers in the list.
467 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000468 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000469
Bill Wendling7729e062010-11-09 22:44:22 +0000470 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000471 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000472 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000473
Sean Callanan18b83232010-01-19 21:44:56 +0000474 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000475 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000476 if (RegTok.isNot(AsmToken::Identifier)) {
477 Error(RegLoc, "register expected");
478 return 0;
479 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000480
Bill Wendling1d6a2652010-11-06 10:40:24 +0000481 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000482 if (RegNum == -1) {
483 Error(RegLoc, "register expected");
484 return 0;
485 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000486
Bill Wendlinge7176102010-11-06 22:36:58 +0000487 if (IsRange) {
488 int Reg = PrevRegNum;
489 do {
490 ++Reg;
491 Registers.push_back(std::make_pair(Reg, RegLoc));
492 } while (Reg != RegNum);
493 } else {
494 Registers.push_back(std::make_pair(RegNum, RegLoc));
495 }
496
497 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000498 } while (Parser.getTok().is(AsmToken::Comma) ||
499 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000500
501 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000502 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000503 if (RCurlyTok.isNot(AsmToken::RCurly)) {
504 Error(RCurlyTok.getLoc(), "'}' expected");
505 return 0;
506 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000507
Bill Wendlinge7176102010-11-06 22:36:58 +0000508 SMLoc E = RCurlyTok.getLoc();
509 Parser.Lex(); // Eat right curly brace token.
510
511 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000512 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000513 RI = Registers.begin(), RE = Registers.end();
514
Bill Wendlinge7176102010-11-06 22:36:58 +0000515 unsigned HighRegNum = RI->first;
Bill Wendlinge7176102010-11-06 22:36:58 +0000516 DenseMap<unsigned, bool> RegMap;
517 RegMap[RI->first] = true;
518
519 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000520 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendlinge7176102010-11-06 22:36:58 +0000521
522 if (RegMap[RegInfo.first]) {
523 Error(RegInfo.second, "register duplicated in register list");
524 return 0;
525 }
526
527 if (RegInfo.first < HighRegNum)
528 Warning(RegInfo.second,
529 "register not in ascending order in register list");
530
531 RegMap[RegInfo.first] = true;
532 HighRegNum = std::max(RegInfo.first, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000533 }
534
Bill Wendling7729e062010-11-09 22:44:22 +0000535 return ARMOperand::CreateRegList(Registers, S, E);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000536}
537
Bill Wendlinge7176102010-11-06 22:36:58 +0000538/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000539/// or an error. The first token must be a '[' when called.
540/// TODO Only preindexing and postindexing addressing are started, unindexed
541/// with option, etc are still to do.
Chris Lattner550276e2010-10-28 20:52:15 +0000542ARMOperand *ARMAsmParser::ParseMemory() {
Sean Callanan76264762010-04-02 22:27:05 +0000543 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000544 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000545 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000546 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000547 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000548
Sean Callanan18b83232010-01-19 21:44:56 +0000549 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000550 if (BaseRegTok.isNot(AsmToken::Identifier)) {
551 Error(BaseRegTok.getLoc(), "register expected");
552 return 0;
553 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000554 int BaseRegNum = TryParseRegister();
555 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000556 Error(BaseRegTok.getLoc(), "register expected");
557 return 0;
558 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000559
560 bool Preindexed = false;
561 bool Postindexed = false;
562 bool OffsetIsReg = false;
563 bool Negative = false;
564 bool Writeback = false;
565
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000566 // First look for preindexed address forms, that is after the "[Rn" we now
567 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000568 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000569 if (Tok.is(AsmToken::Comma)) {
570 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000571 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000572 int OffsetRegNum;
573 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000574 enum ShiftType ShiftType;
575 const MCExpr *ShiftAmount;
576 const MCExpr *Offset;
Chris Lattner550276e2010-10-28 20:52:15 +0000577 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
578 Offset, OffsetIsReg, OffsetRegNum, E))
579 return 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000580 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000581 if (RBracTok.isNot(AsmToken::RBrac)) {
582 Error(RBracTok.getLoc(), "']' expected");
583 return 0;
584 }
Sean Callanan76264762010-04-02 22:27:05 +0000585 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000586 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000587
Sean Callanan18b83232010-01-19 21:44:56 +0000588 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000589 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000590 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000591 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000592 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000593 }
Chris Lattner550276e2010-10-28 20:52:15 +0000594 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
595 OffsetRegShifted, ShiftType, ShiftAmount,
596 Preindexed, Postindexed, Negative, Writeback,
597 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000598 }
599 // The "[Rn" we have so far was not followed by a comma.
600 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000601 // If there's anything other than the right brace, this is a post indexing
602 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000603 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000604 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000605
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000606 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000607 bool OffsetRegShifted = false;
608 enum ShiftType ShiftType;
609 const MCExpr *ShiftAmount;
Chris Lattner14b93852010-10-29 00:27:31 +0000610 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000611
Sean Callanan18b83232010-01-19 21:44:56 +0000612 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000613 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000614 Postindexed = true;
615 Writeback = true;
Chris Lattner550276e2010-10-28 20:52:15 +0000616 if (NextTok.isNot(AsmToken::Comma)) {
617 Error(NextTok.getLoc(), "',' expected");
618 return 0;
619 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000620 Parser.Lex(); // Eat comma token.
Chris Lattner550276e2010-10-28 20:52:15 +0000621 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000622 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000623 E))
624 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000625 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000626
Chris Lattner550276e2010-10-28 20:52:15 +0000627 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
628 OffsetRegShifted, ShiftType, ShiftAmount,
629 Preindexed, Postindexed, Negative, Writeback,
630 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000631 }
632
Chris Lattner550276e2010-10-28 20:52:15 +0000633 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000634}
635
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000636/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
637/// we will parse the following (were +/- means that a plus or minus is
638/// optional):
639/// +/-Rm
640/// +/-Rm, shift
641/// #offset
642/// we return false on success or an error otherwise.
643bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000644 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000645 enum ShiftType &ShiftType,
646 const MCExpr *&ShiftAmount,
647 const MCExpr *&Offset,
648 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000649 int &OffsetRegNum,
650 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000651 Negative = false;
652 OffsetRegShifted = false;
653 OffsetIsReg = false;
654 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000655 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000656 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000657 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000658 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000659 else if (NextTok.is(AsmToken::Minus)) {
660 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000661 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000662 }
663 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000664 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000665 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000666 SMLoc CurLoc = OffsetRegTok.getLoc();
667 OffsetRegNum = TryParseRegister();
668 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000669 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000670 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000671 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000672 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000673
Bill Wendling12f40e92010-11-06 10:51:53 +0000674 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000675 if (OffsetRegNum != -1) {
676 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000677 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000678 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000679 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000680
Sean Callanan18b83232010-01-19 21:44:56 +0000681 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000682 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000683 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000684 OffsetRegShifted = true;
685 }
686 }
687 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
688 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000689 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000690 if (HashTok.isNot(AsmToken::Hash))
691 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000692
Sean Callananb9a25b72010-01-19 20:27:46 +0000693 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000694
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000695 if (getParser().ParseExpression(Offset))
696 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000697 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000698 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000699 return false;
700}
701
702/// ParseShift as one of these two:
703/// ( lsl | lsr | asr | ror ) , # shift_amount
704/// rrx
705/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000706bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000707 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000708 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000709 if (Tok.isNot(AsmToken::Identifier))
710 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000711 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000712 if (ShiftName == "lsl" || ShiftName == "LSL")
713 St = Lsl;
714 else if (ShiftName == "lsr" || ShiftName == "LSR")
715 St = Lsr;
716 else if (ShiftName == "asr" || ShiftName == "ASR")
717 St = Asr;
718 else if (ShiftName == "ror" || ShiftName == "ROR")
719 St = Ror;
720 else if (ShiftName == "rrx" || ShiftName == "RRX")
721 St = Rrx;
722 else
723 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000724 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000725
726 // Rrx stands alone.
727 if (St == Rrx)
728 return false;
729
730 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000731 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000732 if (HashTok.isNot(AsmToken::Hash))
733 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000734 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000735
736 if (getParser().ParseExpression(ShiftAmount))
737 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000738
739 return false;
740}
741
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000742/// Parse a arm instruction operand. For now this parses the operand regardless
743/// of the mnemonic.
Chris Lattner550276e2010-10-28 20:52:15 +0000744ARMOperand *ARMAsmParser::ParseOperand() {
Sean Callanan76264762010-04-02 22:27:05 +0000745 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000746 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000747 default:
748 Error(Parser.getTok().getLoc(), "unexpected token in operand");
749 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000750 case AsmToken::Identifier:
Chris Lattnere5658fa2010-10-30 04:09:10 +0000751 if (ARMOperand *Op = TryParseRegisterWithWriteBack())
Chris Lattner550276e2010-10-28 20:52:15 +0000752 return Op;
Jim Grosbach16c74252010-10-29 14:46:02 +0000753
Kevin Enderby515d5092009-10-15 20:48:48 +0000754 // This was not a register so parse other operands that start with an
755 // identifier (like labels) as expressions and create them as immediates.
756 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000757 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000758 if (getParser().ParseExpression(IdVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000759 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000760 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000761 return ARMOperand::CreateImm(IdVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000762 case AsmToken::LBrac:
Chris Lattner550276e2010-10-28 20:52:15 +0000763 return ParseMemory();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000764 case AsmToken::LCurly:
Chris Lattner550276e2010-10-28 20:52:15 +0000765 return ParseRegisterList();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000766 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000767 // #42 -> immediate.
768 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000769 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000770 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000771 const MCExpr *ImmVal;
772 if (getParser().ParseExpression(ImmVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000773 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000774 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000775 return ARMOperand::CreateImm(ImmVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000776 }
777}
778
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000779/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000780bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000781 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000782 // Create the leading tokens for the mnemonic, split by '.' characters.
783 size_t Start = 0, Next = Name.find('.');
784 StringRef Head = Name.slice(Start, Next);
785
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000786 // Determine the predicate, if any.
787 //
788 // FIXME: We need a way to check whether a prefix supports predication,
789 // otherwise we will end up with an ambiguity for instructions that happen to
790 // end with a predicate name.
Jim Grosbach3df518e2010-10-29 21:56:51 +0000791 // FIXME: Likewise, some arithmetic instructions have an 's' prefix which
792 // indicates to update the condition codes. Those instructions have an
793 // additional immediate operand which encodes the prefix as reg0 or CPSR.
794 // Just checking for a suffix of 's' definitely creates ambiguities; e.g,
795 // the SMMLS instruction.
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000796 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
797 .Case("eq", ARMCC::EQ)
798 .Case("ne", ARMCC::NE)
799 .Case("hs", ARMCC::HS)
800 .Case("lo", ARMCC::LO)
801 .Case("mi", ARMCC::MI)
802 .Case("pl", ARMCC::PL)
803 .Case("vs", ARMCC::VS)
804 .Case("vc", ARMCC::VC)
805 .Case("hi", ARMCC::HI)
806 .Case("ls", ARMCC::LS)
807 .Case("ge", ARMCC::GE)
808 .Case("lt", ARMCC::LT)
809 .Case("gt", ARMCC::GT)
810 .Case("le", ARMCC::LE)
811 .Case("al", ARMCC::AL)
812 .Default(~0U);
Jim Grosbach16c74252010-10-29 14:46:02 +0000813
Chris Lattnerdba34d82010-10-30 04:35:59 +0000814 if (CC == ~0U ||
815 (CC == ARMCC::LS && (Head == "vmls" || Head == "vnmls"))) {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000816 CC = ARMCC::AL;
Chris Lattnerdba34d82010-10-30 04:35:59 +0000817 } else {
818 Head = Head.slice(0, Head.size() - 2);
Bill Wendling52925b62010-10-29 23:50:21 +0000819 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000820
Chris Lattner3a697562010-10-28 17:20:03 +0000821 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Jim Grosbach469ebbe2010-11-01 18:11:14 +0000822 // FIXME: Should only add this operand for predicated instructions
Chris Lattner3a697562010-10-28 17:20:03 +0000823 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000824
825 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000826 while (Next != StringRef::npos) {
827 Start = Next;
828 Next = Name.find('.', Start + 1);
829 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000830
Chris Lattner3a697562010-10-28 17:20:03 +0000831 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000832 }
833
834 // Read the remaining operands.
835 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000836 // Read the first operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000837 if (ARMOperand *Op = ParseOperand())
838 Operands.push_back(Op);
839 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000840 Parser.EatToEndOfStatement();
841 return true;
842 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000843
844 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000845 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000846
847 // Parse and remember the operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000848 if (ARMOperand *Op = ParseOperand())
849 Operands.push_back(Op);
850 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000851 Parser.EatToEndOfStatement();
852 return true;
853 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000854 }
855 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000856
Chris Lattnercbf8a982010-09-11 16:18:25 +0000857 if (getLexer().isNot(AsmToken::EndOfStatement)) {
858 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000859 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000860 }
Bill Wendling146018f2010-11-06 21:42:12 +0000861
Chris Lattner34e53142010-09-08 05:10:46 +0000862 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000863 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000864}
865
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000866bool ARMAsmParser::
867MatchAndEmitInstruction(SMLoc IDLoc,
868 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
869 MCStreamer &Out) {
870 MCInst Inst;
871 unsigned ErrorInfo;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000872 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
873 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000874 Out.EmitInstruction(Inst);
875 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000876 case Match_MissingFeature:
877 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
878 return true;
879 case Match_InvalidOperand: {
880 SMLoc ErrorLoc = IDLoc;
881 if (ErrorInfo != ~0U) {
882 if (ErrorInfo >= Operands.size())
883 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +0000884
Chris Lattnere73d4f82010-10-28 21:41:58 +0000885 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
886 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
887 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000888
Chris Lattnere73d4f82010-10-28 21:41:58 +0000889 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000890 }
Chris Lattnere73d4f82010-10-28 21:41:58 +0000891 case Match_MnemonicFail:
892 return Error(IDLoc, "unrecognized instruction mnemonic");
893 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000894
Eric Christopherc223e2b2010-10-29 09:26:59 +0000895 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +0000896 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000897}
898
Kevin Enderby515d5092009-10-15 20:48:48 +0000899/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000900bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
901 StringRef IDVal = DirectiveID.getIdentifier();
902 if (IDVal == ".word")
903 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000904 else if (IDVal == ".thumb")
905 return ParseDirectiveThumb(DirectiveID.getLoc());
906 else if (IDVal == ".thumb_func")
907 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
908 else if (IDVal == ".code")
909 return ParseDirectiveCode(DirectiveID.getLoc());
910 else if (IDVal == ".syntax")
911 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000912 return true;
913}
914
915/// ParseDirectiveWord
916/// ::= .word [ expression (, expression)* ]
917bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
918 if (getLexer().isNot(AsmToken::EndOfStatement)) {
919 for (;;) {
920 const MCExpr *Value;
921 if (getParser().ParseExpression(Value))
922 return true;
923
Chris Lattneraaec2052010-01-19 19:46:13 +0000924 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000925
926 if (getLexer().is(AsmToken::EndOfStatement))
927 break;
Jim Grosbach16c74252010-10-29 14:46:02 +0000928
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000929 // FIXME: Improve diagnostic.
930 if (getLexer().isNot(AsmToken::Comma))
931 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000932 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000933 }
934 }
935
Sean Callananb9a25b72010-01-19 20:27:46 +0000936 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000937 return false;
938}
939
Kevin Enderby515d5092009-10-15 20:48:48 +0000940/// ParseDirectiveThumb
941/// ::= .thumb
942bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
943 if (getLexer().isNot(AsmToken::EndOfStatement))
944 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000945 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000946
947 // TODO: set thumb mode
948 // TODO: tell the MC streamer the mode
949 // getParser().getStreamer().Emit???();
950 return false;
951}
952
953/// ParseDirectiveThumbFunc
954/// ::= .thumbfunc symbol_name
955bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000956 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000957 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +0000958 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000959 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +0000960 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000961 if (getLexer().isNot(AsmToken::EndOfStatement))
962 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000963 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000964
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000965 // Mark symbol as a thumb symbol.
966 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
967 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +0000968 return false;
969}
970
971/// ParseDirectiveSyntax
972/// ::= .syntax unified | divided
973bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000974 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000975 if (Tok.isNot(AsmToken::Identifier))
976 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000977 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000978 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000979 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000980 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000981 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000982 else
983 return Error(L, "unrecognized syntax mode in .syntax directive");
984
985 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000986 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000987 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000988
989 // TODO tell the MC streamer the mode
990 // getParser().getStreamer().Emit???();
991 return false;
992}
993
994/// ParseDirectiveCode
995/// ::= .code 16 | 32
996bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000997 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000998 if (Tok.isNot(AsmToken::Integer))
999 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001000 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001001 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001002 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001003 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001004 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001005 else
1006 return Error(L, "invalid operand to .code directive");
1007
1008 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001009 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001010 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001011
Jim Grosbach2a301702010-11-05 22:40:53 +00001012 if (Val == 16)
1013 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
1014 else
1015 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1016
Kevin Enderby515d5092009-10-15 20:48:48 +00001017 return false;
1018}
1019
Sean Callanan90b70972010-04-07 20:29:34 +00001020extern "C" void LLVMInitializeARMAsmLexer();
1021
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001022/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001023extern "C" void LLVMInitializeARMAsmParser() {
1024 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1025 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001026 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001027}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001028
Chris Lattner0692ee62010-09-06 19:11:01 +00001029#define GET_REGISTER_MATCHER
1030#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001031#include "ARMGenAsmMatcher.inc"