blob: 65f0ad4bb5033e8779a62a19493718f9914ffbf7 [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,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000329 SMLoc StartLoc, SMLoc EndLoc) {
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());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000336 Op->StartLoc = StartLoc;
337 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000338 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 DenseMap<unsigned, bool> RegMap;
516 RegMap[RI->first] = true;
517
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000518 unsigned HighRegNum = RI->first;
519 bool EmittedWarning = false;
520
Bill Wendlinge7176102010-11-06 22:36:58 +0000521 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000522 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000523 unsigned Reg = RegInfo.first;
Bill Wendlinge7176102010-11-06 22:36:58 +0000524
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000525 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000526 Error(RegInfo.second, "register duplicated in register list");
527 return 0;
528 }
529
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000530 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000531 Warning(RegInfo.second,
532 "register not in ascending order in register list");
533
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000534 RegMap[Reg] = true;
535 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000536 }
537
Bill Wendling7729e062010-11-09 22:44:22 +0000538 return ARMOperand::CreateRegList(Registers, S, E);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000539}
540
Bill Wendlinge7176102010-11-06 22:36:58 +0000541/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000542/// or an error. The first token must be a '[' when called.
543/// TODO Only preindexing and postindexing addressing are started, unindexed
544/// with option, etc are still to do.
Chris Lattner550276e2010-10-28 20:52:15 +0000545ARMOperand *ARMAsmParser::ParseMemory() {
Sean Callanan76264762010-04-02 22:27:05 +0000546 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000547 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000548 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000549 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000550 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000551
Sean Callanan18b83232010-01-19 21:44:56 +0000552 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000553 if (BaseRegTok.isNot(AsmToken::Identifier)) {
554 Error(BaseRegTok.getLoc(), "register expected");
555 return 0;
556 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000557 int BaseRegNum = TryParseRegister();
558 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000559 Error(BaseRegTok.getLoc(), "register expected");
560 return 0;
561 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000562
563 bool Preindexed = false;
564 bool Postindexed = false;
565 bool OffsetIsReg = false;
566 bool Negative = false;
567 bool Writeback = false;
568
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000569 // First look for preindexed address forms, that is after the "[Rn" we now
570 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000571 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000572 if (Tok.is(AsmToken::Comma)) {
573 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000574 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000575 int OffsetRegNum;
576 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000577 enum ShiftType ShiftType;
578 const MCExpr *ShiftAmount;
579 const MCExpr *Offset;
Chris Lattner550276e2010-10-28 20:52:15 +0000580 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
581 Offset, OffsetIsReg, OffsetRegNum, E))
582 return 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000583 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000584 if (RBracTok.isNot(AsmToken::RBrac)) {
585 Error(RBracTok.getLoc(), "']' expected");
586 return 0;
587 }
Sean Callanan76264762010-04-02 22:27:05 +0000588 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000589 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000590
Sean Callanan18b83232010-01-19 21:44:56 +0000591 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000592 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000593 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000594 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000595 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000596 }
Chris Lattner550276e2010-10-28 20:52:15 +0000597 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
598 OffsetRegShifted, ShiftType, ShiftAmount,
599 Preindexed, Postindexed, Negative, Writeback,
600 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000601 }
602 // The "[Rn" we have so far was not followed by a comma.
603 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000604 // If there's anything other than the right brace, this is a post indexing
605 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000606 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000607 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000608
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000609 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000610 bool OffsetRegShifted = false;
611 enum ShiftType ShiftType;
612 const MCExpr *ShiftAmount;
Chris Lattner14b93852010-10-29 00:27:31 +0000613 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000614
Sean Callanan18b83232010-01-19 21:44:56 +0000615 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000616 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000617 Postindexed = true;
618 Writeback = true;
Chris Lattner550276e2010-10-28 20:52:15 +0000619 if (NextTok.isNot(AsmToken::Comma)) {
620 Error(NextTok.getLoc(), "',' expected");
621 return 0;
622 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000623 Parser.Lex(); // Eat comma token.
Chris Lattner550276e2010-10-28 20:52:15 +0000624 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000625 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000626 E))
627 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000628 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000629
Chris Lattner550276e2010-10-28 20:52:15 +0000630 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
631 OffsetRegShifted, ShiftType, ShiftAmount,
632 Preindexed, Postindexed, Negative, Writeback,
633 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000634 }
635
Chris Lattner550276e2010-10-28 20:52:15 +0000636 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000637}
638
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000639/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
640/// we will parse the following (were +/- means that a plus or minus is
641/// optional):
642/// +/-Rm
643/// +/-Rm, shift
644/// #offset
645/// we return false on success or an error otherwise.
646bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000647 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000648 enum ShiftType &ShiftType,
649 const MCExpr *&ShiftAmount,
650 const MCExpr *&Offset,
651 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000652 int &OffsetRegNum,
653 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000654 Negative = false;
655 OffsetRegShifted = false;
656 OffsetIsReg = false;
657 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000658 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000659 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000660 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000661 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000662 else if (NextTok.is(AsmToken::Minus)) {
663 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000664 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000665 }
666 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000667 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000668 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000669 SMLoc CurLoc = OffsetRegTok.getLoc();
670 OffsetRegNum = TryParseRegister();
671 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000672 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000673 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000674 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000675 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000676
Bill Wendling12f40e92010-11-06 10:51:53 +0000677 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000678 if (OffsetRegNum != -1) {
679 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000680 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000681 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000682 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000683
Sean Callanan18b83232010-01-19 21:44:56 +0000684 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000685 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000686 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000687 OffsetRegShifted = true;
688 }
689 }
690 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
691 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000692 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000693 if (HashTok.isNot(AsmToken::Hash))
694 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000695
Sean Callananb9a25b72010-01-19 20:27:46 +0000696 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000697
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000698 if (getParser().ParseExpression(Offset))
699 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000700 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000701 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000702 return false;
703}
704
705/// ParseShift as one of these two:
706/// ( lsl | lsr | asr | ror ) , # shift_amount
707/// rrx
708/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000709bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000710 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000711 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000712 if (Tok.isNot(AsmToken::Identifier))
713 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000714 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000715 if (ShiftName == "lsl" || ShiftName == "LSL")
716 St = Lsl;
717 else if (ShiftName == "lsr" || ShiftName == "LSR")
718 St = Lsr;
719 else if (ShiftName == "asr" || ShiftName == "ASR")
720 St = Asr;
721 else if (ShiftName == "ror" || ShiftName == "ROR")
722 St = Ror;
723 else if (ShiftName == "rrx" || ShiftName == "RRX")
724 St = Rrx;
725 else
726 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000727 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000728
729 // Rrx stands alone.
730 if (St == Rrx)
731 return false;
732
733 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000734 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000735 if (HashTok.isNot(AsmToken::Hash))
736 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000737 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000738
739 if (getParser().ParseExpression(ShiftAmount))
740 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000741
742 return false;
743}
744
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000745/// Parse a arm instruction operand. For now this parses the operand regardless
746/// of the mnemonic.
Chris Lattner550276e2010-10-28 20:52:15 +0000747ARMOperand *ARMAsmParser::ParseOperand() {
Sean Callanan76264762010-04-02 22:27:05 +0000748 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000749 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000750 default:
751 Error(Parser.getTok().getLoc(), "unexpected token in operand");
752 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000753 case AsmToken::Identifier:
Chris Lattnere5658fa2010-10-30 04:09:10 +0000754 if (ARMOperand *Op = TryParseRegisterWithWriteBack())
Chris Lattner550276e2010-10-28 20:52:15 +0000755 return Op;
Jim Grosbach16c74252010-10-29 14:46:02 +0000756
Kevin Enderby515d5092009-10-15 20:48:48 +0000757 // This was not a register so parse other operands that start with an
758 // identifier (like labels) as expressions and create them as immediates.
759 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000760 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000761 if (getParser().ParseExpression(IdVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000762 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000763 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000764 return ARMOperand::CreateImm(IdVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000765 case AsmToken::LBrac:
Chris Lattner550276e2010-10-28 20:52:15 +0000766 return ParseMemory();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000767 case AsmToken::LCurly:
Chris Lattner550276e2010-10-28 20:52:15 +0000768 return ParseRegisterList();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000769 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000770 // #42 -> immediate.
771 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000772 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000773 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000774 const MCExpr *ImmVal;
775 if (getParser().ParseExpression(ImmVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000776 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000777 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000778 return ARMOperand::CreateImm(ImmVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000779 }
780}
781
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000782/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000783bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000784 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000785 // Create the leading tokens for the mnemonic, split by '.' characters.
786 size_t Start = 0, Next = Name.find('.');
787 StringRef Head = Name.slice(Start, Next);
788
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000789 // Determine the predicate, if any.
790 //
791 // FIXME: We need a way to check whether a prefix supports predication,
792 // otherwise we will end up with an ambiguity for instructions that happen to
793 // end with a predicate name.
Jim Grosbach3df518e2010-10-29 21:56:51 +0000794 // FIXME: Likewise, some arithmetic instructions have an 's' prefix which
795 // indicates to update the condition codes. Those instructions have an
796 // additional immediate operand which encodes the prefix as reg0 or CPSR.
797 // Just checking for a suffix of 's' definitely creates ambiguities; e.g,
798 // the SMMLS instruction.
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000799 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
800 .Case("eq", ARMCC::EQ)
801 .Case("ne", ARMCC::NE)
802 .Case("hs", ARMCC::HS)
803 .Case("lo", ARMCC::LO)
804 .Case("mi", ARMCC::MI)
805 .Case("pl", ARMCC::PL)
806 .Case("vs", ARMCC::VS)
807 .Case("vc", ARMCC::VC)
808 .Case("hi", ARMCC::HI)
809 .Case("ls", ARMCC::LS)
810 .Case("ge", ARMCC::GE)
811 .Case("lt", ARMCC::LT)
812 .Case("gt", ARMCC::GT)
813 .Case("le", ARMCC::LE)
814 .Case("al", ARMCC::AL)
815 .Default(~0U);
Jim Grosbach16c74252010-10-29 14:46:02 +0000816
Chris Lattnerdba34d82010-10-30 04:35:59 +0000817 if (CC == ~0U ||
818 (CC == ARMCC::LS && (Head == "vmls" || Head == "vnmls"))) {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000819 CC = ARMCC::AL;
Chris Lattnerdba34d82010-10-30 04:35:59 +0000820 } else {
821 Head = Head.slice(0, Head.size() - 2);
Bill Wendling52925b62010-10-29 23:50:21 +0000822 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000823
Chris Lattner3a697562010-10-28 17:20:03 +0000824 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Jim Grosbach469ebbe2010-11-01 18:11:14 +0000825 // FIXME: Should only add this operand for predicated instructions
Chris Lattner3a697562010-10-28 17:20:03 +0000826 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000827
828 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000829 while (Next != StringRef::npos) {
830 Start = Next;
831 Next = Name.find('.', Start + 1);
832 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000833
Chris Lattner3a697562010-10-28 17:20:03 +0000834 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000835 }
836
837 // Read the remaining operands.
838 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000839 // Read the first operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000840 if (ARMOperand *Op = ParseOperand())
841 Operands.push_back(Op);
842 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000843 Parser.EatToEndOfStatement();
844 return true;
845 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000846
847 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000848 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000849
850 // Parse and remember the operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000851 if (ARMOperand *Op = ParseOperand())
852 Operands.push_back(Op);
853 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000854 Parser.EatToEndOfStatement();
855 return true;
856 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000857 }
858 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000859
Chris Lattnercbf8a982010-09-11 16:18:25 +0000860 if (getLexer().isNot(AsmToken::EndOfStatement)) {
861 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000862 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000863 }
Bill Wendling146018f2010-11-06 21:42:12 +0000864
Chris Lattner34e53142010-09-08 05:10:46 +0000865 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000866 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000867}
868
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000869bool ARMAsmParser::
870MatchAndEmitInstruction(SMLoc IDLoc,
871 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
872 MCStreamer &Out) {
873 MCInst Inst;
874 unsigned ErrorInfo;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000875 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
876 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000877 Out.EmitInstruction(Inst);
878 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000879 case Match_MissingFeature:
880 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
881 return true;
882 case Match_InvalidOperand: {
883 SMLoc ErrorLoc = IDLoc;
884 if (ErrorInfo != ~0U) {
885 if (ErrorInfo >= Operands.size())
886 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +0000887
Chris Lattnere73d4f82010-10-28 21:41:58 +0000888 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
889 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
890 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000891
Chris Lattnere73d4f82010-10-28 21:41:58 +0000892 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000893 }
Chris Lattnere73d4f82010-10-28 21:41:58 +0000894 case Match_MnemonicFail:
895 return Error(IDLoc, "unrecognized instruction mnemonic");
896 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000897
Eric Christopherc223e2b2010-10-29 09:26:59 +0000898 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +0000899 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000900}
901
Kevin Enderby515d5092009-10-15 20:48:48 +0000902/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000903bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
904 StringRef IDVal = DirectiveID.getIdentifier();
905 if (IDVal == ".word")
906 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000907 else if (IDVal == ".thumb")
908 return ParseDirectiveThumb(DirectiveID.getLoc());
909 else if (IDVal == ".thumb_func")
910 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
911 else if (IDVal == ".code")
912 return ParseDirectiveCode(DirectiveID.getLoc());
913 else if (IDVal == ".syntax")
914 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000915 return true;
916}
917
918/// ParseDirectiveWord
919/// ::= .word [ expression (, expression)* ]
920bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
921 if (getLexer().isNot(AsmToken::EndOfStatement)) {
922 for (;;) {
923 const MCExpr *Value;
924 if (getParser().ParseExpression(Value))
925 return true;
926
Chris Lattneraaec2052010-01-19 19:46:13 +0000927 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000928
929 if (getLexer().is(AsmToken::EndOfStatement))
930 break;
Jim Grosbach16c74252010-10-29 14:46:02 +0000931
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000932 // FIXME: Improve diagnostic.
933 if (getLexer().isNot(AsmToken::Comma))
934 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000935 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000936 }
937 }
938
Sean Callananb9a25b72010-01-19 20:27:46 +0000939 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000940 return false;
941}
942
Kevin Enderby515d5092009-10-15 20:48:48 +0000943/// ParseDirectiveThumb
944/// ::= .thumb
945bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
946 if (getLexer().isNot(AsmToken::EndOfStatement))
947 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000948 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000949
950 // TODO: set thumb mode
951 // TODO: tell the MC streamer the mode
952 // getParser().getStreamer().Emit???();
953 return false;
954}
955
956/// ParseDirectiveThumbFunc
957/// ::= .thumbfunc symbol_name
958bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000959 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000960 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +0000961 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000962 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +0000963 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000964 if (getLexer().isNot(AsmToken::EndOfStatement))
965 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000966 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000967
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000968 // Mark symbol as a thumb symbol.
969 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
970 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +0000971 return false;
972}
973
974/// ParseDirectiveSyntax
975/// ::= .syntax unified | divided
976bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000977 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000978 if (Tok.isNot(AsmToken::Identifier))
979 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000980 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000981 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000982 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000983 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000984 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000985 else
986 return Error(L, "unrecognized syntax mode in .syntax directive");
987
988 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000989 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000990 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000991
992 // TODO tell the MC streamer the mode
993 // getParser().getStreamer().Emit???();
994 return false;
995}
996
997/// ParseDirectiveCode
998/// ::= .code 16 | 32
999bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001000 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001001 if (Tok.isNot(AsmToken::Integer))
1002 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001003 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001004 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001005 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001006 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001007 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001008 else
1009 return Error(L, "invalid operand to .code directive");
1010
1011 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001012 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001013 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001014
Jim Grosbach2a301702010-11-05 22:40:53 +00001015 if (Val == 16)
1016 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
1017 else
1018 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1019
Kevin Enderby515d5092009-10-15 20:48:48 +00001020 return false;
1021}
1022
Sean Callanan90b70972010-04-07 20:29:34 +00001023extern "C" void LLVMInitializeARMAsmLexer();
1024
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001025/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001026extern "C" void LLVMInitializeARMAsmParser() {
1027 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1028 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001029 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001030}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001031
Chris Lattner0692ee62010-09-06 19:11:01 +00001032#define GET_REGISTER_MATCHER
1033#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001034#include "ARMGenAsmMatcher.inc"