blob: bb7494ba94346fb67c24842f50bf1bcfdf8b6e13 [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 Wendling7729e062010-11-09 22:44:22 +0000131 std::vector<unsigned> *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 Wendling7729e062010-11-09 22:44:22 +0000206 const std::vector<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!");
261 const std::vector<unsigned> &RegList = getRegList();
262 for (std::vector<unsigned>::const_iterator
263 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 *
328 CreateRegList(std::vector<std::pair<unsigned, SMLoc> > &Regs,
329 SMLoc S, SMLoc E) {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000330 ARMOperand *Op = new ARMOperand(RegisterList);
Bill Wendling7729e062010-11-09 22:44:22 +0000331 Op->RegList.Registers = new std::vector<unsigned>();
332 for (std::vector<std::pair<unsigned, SMLoc> >::iterator
333 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 Wendling7729e062010-11-09 22:44:22 +0000393 const std::vector<unsigned> &RegList = getRegList();
394 for (std::vector<unsigned>::const_iterator
395 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 Wendlinge7176102010-11-06 22:36:58 +0000468 std::vector<std::pair<unsigned, SMLoc> > Registers;
469 Registers.reserve(32);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000470
Bill Wendling7729e062010-11-09 22:44:22 +0000471 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000472 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000473 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000474
Sean Callanan18b83232010-01-19 21:44:56 +0000475 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000476 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000477 if (RegTok.isNot(AsmToken::Identifier)) {
478 Error(RegLoc, "register expected");
479 return 0;
480 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000481
Bill Wendling1d6a2652010-11-06 10:40:24 +0000482 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000483 if (RegNum == -1) {
484 Error(RegLoc, "register expected");
485 return 0;
486 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000487
Bill Wendlinge7176102010-11-06 22:36:58 +0000488 if (IsRange) {
489 int Reg = PrevRegNum;
490 do {
491 ++Reg;
492 Registers.push_back(std::make_pair(Reg, RegLoc));
493 } while (Reg != RegNum);
494 } else {
495 Registers.push_back(std::make_pair(RegNum, RegLoc));
496 }
497
498 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000499 } while (Parser.getTok().is(AsmToken::Comma) ||
500 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000501
502 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000503 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000504 if (RCurlyTok.isNot(AsmToken::RCurly)) {
505 Error(RCurlyTok.getLoc(), "'}' expected");
506 return 0;
507 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000508
Bill Wendlinge7176102010-11-06 22:36:58 +0000509 SMLoc E = RCurlyTok.getLoc();
510 Parser.Lex(); // Eat right curly brace token.
511
512 // Verify the register list.
Bill Wendling7729e062010-11-09 22:44:22 +0000513 std::vector<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000514 RI = Registers.begin(), RE = Registers.end();
515
Bill Wendlinge7176102010-11-06 22:36:58 +0000516 unsigned HighRegNum = RI->first;
Bill Wendlinge7176102010-11-06 22:36:58 +0000517 DenseMap<unsigned, bool> RegMap;
518 RegMap[RI->first] = true;
519
520 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000521 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendlinge7176102010-11-06 22:36:58 +0000522
523 if (RegMap[RegInfo.first]) {
524 Error(RegInfo.second, "register duplicated in register list");
525 return 0;
526 }
527
528 if (RegInfo.first < HighRegNum)
529 Warning(RegInfo.second,
530 "register not in ascending order in register list");
531
532 RegMap[RegInfo.first] = true;
533 HighRegNum = std::max(RegInfo.first, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000534 }
535
Bill Wendling7729e062010-11-09 22:44:22 +0000536 return ARMOperand::CreateRegList(Registers, S, E);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000537}
538
Bill Wendlinge7176102010-11-06 22:36:58 +0000539/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000540/// or an error. The first token must be a '[' when called.
541/// TODO Only preindexing and postindexing addressing are started, unindexed
542/// with option, etc are still to do.
Chris Lattner550276e2010-10-28 20:52:15 +0000543ARMOperand *ARMAsmParser::ParseMemory() {
Sean Callanan76264762010-04-02 22:27:05 +0000544 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000545 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000546 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000547 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000548 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000549
Sean Callanan18b83232010-01-19 21:44:56 +0000550 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000551 if (BaseRegTok.isNot(AsmToken::Identifier)) {
552 Error(BaseRegTok.getLoc(), "register expected");
553 return 0;
554 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000555 int BaseRegNum = TryParseRegister();
556 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000557 Error(BaseRegTok.getLoc(), "register expected");
558 return 0;
559 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000560
561 bool Preindexed = false;
562 bool Postindexed = false;
563 bool OffsetIsReg = false;
564 bool Negative = false;
565 bool Writeback = false;
566
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000567 // First look for preindexed address forms, that is after the "[Rn" we now
568 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000569 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000570 if (Tok.is(AsmToken::Comma)) {
571 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000572 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000573 int OffsetRegNum;
574 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000575 enum ShiftType ShiftType;
576 const MCExpr *ShiftAmount;
577 const MCExpr *Offset;
Chris Lattner550276e2010-10-28 20:52:15 +0000578 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
579 Offset, OffsetIsReg, OffsetRegNum, E))
580 return 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000581 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000582 if (RBracTok.isNot(AsmToken::RBrac)) {
583 Error(RBracTok.getLoc(), "']' expected");
584 return 0;
585 }
Sean Callanan76264762010-04-02 22:27:05 +0000586 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000587 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000588
Sean Callanan18b83232010-01-19 21:44:56 +0000589 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000590 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000591 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000592 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000593 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000594 }
Chris Lattner550276e2010-10-28 20:52:15 +0000595 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
596 OffsetRegShifted, ShiftType, ShiftAmount,
597 Preindexed, Postindexed, Negative, Writeback,
598 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000599 }
600 // The "[Rn" we have so far was not followed by a comma.
601 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000602 // If there's anything other than the right brace, this is a post indexing
603 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000604 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000605 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000606
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000607 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000608 bool OffsetRegShifted = false;
609 enum ShiftType ShiftType;
610 const MCExpr *ShiftAmount;
Chris Lattner14b93852010-10-29 00:27:31 +0000611 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000612
Sean Callanan18b83232010-01-19 21:44:56 +0000613 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000614 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000615 Postindexed = true;
616 Writeback = true;
Chris Lattner550276e2010-10-28 20:52:15 +0000617 if (NextTok.isNot(AsmToken::Comma)) {
618 Error(NextTok.getLoc(), "',' expected");
619 return 0;
620 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000621 Parser.Lex(); // Eat comma token.
Chris Lattner550276e2010-10-28 20:52:15 +0000622 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000623 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000624 E))
625 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000626 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000627
Chris Lattner550276e2010-10-28 20:52:15 +0000628 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
629 OffsetRegShifted, ShiftType, ShiftAmount,
630 Preindexed, Postindexed, Negative, Writeback,
631 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000632 }
633
Chris Lattner550276e2010-10-28 20:52:15 +0000634 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000635}
636
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000637/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
638/// we will parse the following (were +/- means that a plus or minus is
639/// optional):
640/// +/-Rm
641/// +/-Rm, shift
642/// #offset
643/// we return false on success or an error otherwise.
644bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000645 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000646 enum ShiftType &ShiftType,
647 const MCExpr *&ShiftAmount,
648 const MCExpr *&Offset,
649 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000650 int &OffsetRegNum,
651 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000652 Negative = false;
653 OffsetRegShifted = false;
654 OffsetIsReg = false;
655 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000656 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000657 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000658 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000659 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000660 else if (NextTok.is(AsmToken::Minus)) {
661 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000662 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000663 }
664 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000665 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000666 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000667 SMLoc CurLoc = OffsetRegTok.getLoc();
668 OffsetRegNum = TryParseRegister();
669 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000670 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000671 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000672 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000673 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000674
Bill Wendling12f40e92010-11-06 10:51:53 +0000675 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000676 if (OffsetRegNum != -1) {
677 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000678 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000679 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000680 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000681
Sean Callanan18b83232010-01-19 21:44:56 +0000682 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000683 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000684 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000685 OffsetRegShifted = true;
686 }
687 }
688 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
689 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000690 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000691 if (HashTok.isNot(AsmToken::Hash))
692 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000693
Sean Callananb9a25b72010-01-19 20:27:46 +0000694 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000695
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000696 if (getParser().ParseExpression(Offset))
697 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000698 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000699 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000700 return false;
701}
702
703/// ParseShift as one of these two:
704/// ( lsl | lsr | asr | ror ) , # shift_amount
705/// rrx
706/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000707bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000708 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000709 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000710 if (Tok.isNot(AsmToken::Identifier))
711 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000712 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000713 if (ShiftName == "lsl" || ShiftName == "LSL")
714 St = Lsl;
715 else if (ShiftName == "lsr" || ShiftName == "LSR")
716 St = Lsr;
717 else if (ShiftName == "asr" || ShiftName == "ASR")
718 St = Asr;
719 else if (ShiftName == "ror" || ShiftName == "ROR")
720 St = Ror;
721 else if (ShiftName == "rrx" || ShiftName == "RRX")
722 St = Rrx;
723 else
724 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000725 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000726
727 // Rrx stands alone.
728 if (St == Rrx)
729 return false;
730
731 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000732 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000733 if (HashTok.isNot(AsmToken::Hash))
734 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000735 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000736
737 if (getParser().ParseExpression(ShiftAmount))
738 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000739
740 return false;
741}
742
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000743/// Parse a arm instruction operand. For now this parses the operand regardless
744/// of the mnemonic.
Chris Lattner550276e2010-10-28 20:52:15 +0000745ARMOperand *ARMAsmParser::ParseOperand() {
Sean Callanan76264762010-04-02 22:27:05 +0000746 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000747 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000748 default:
749 Error(Parser.getTok().getLoc(), "unexpected token in operand");
750 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000751 case AsmToken::Identifier:
Chris Lattnere5658fa2010-10-30 04:09:10 +0000752 if (ARMOperand *Op = TryParseRegisterWithWriteBack())
Chris Lattner550276e2010-10-28 20:52:15 +0000753 return Op;
Jim Grosbach16c74252010-10-29 14:46:02 +0000754
Kevin Enderby515d5092009-10-15 20:48:48 +0000755 // This was not a register so parse other operands that start with an
756 // identifier (like labels) as expressions and create them as immediates.
757 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000758 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000759 if (getParser().ParseExpression(IdVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000760 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000761 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000762 return ARMOperand::CreateImm(IdVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000763 case AsmToken::LBrac:
Chris Lattner550276e2010-10-28 20:52:15 +0000764 return ParseMemory();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000765 case AsmToken::LCurly:
Chris Lattner550276e2010-10-28 20:52:15 +0000766 return ParseRegisterList();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000767 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000768 // #42 -> immediate.
769 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000770 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000771 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000772 const MCExpr *ImmVal;
773 if (getParser().ParseExpression(ImmVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000774 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000775 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000776 return ARMOperand::CreateImm(ImmVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000777 }
778}
779
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000780/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000781bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000782 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000783 // Create the leading tokens for the mnemonic, split by '.' characters.
784 size_t Start = 0, Next = Name.find('.');
785 StringRef Head = Name.slice(Start, Next);
786
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000787 // Determine the predicate, if any.
788 //
789 // FIXME: We need a way to check whether a prefix supports predication,
790 // otherwise we will end up with an ambiguity for instructions that happen to
791 // end with a predicate name.
Jim Grosbach3df518e2010-10-29 21:56:51 +0000792 // FIXME: Likewise, some arithmetic instructions have an 's' prefix which
793 // indicates to update the condition codes. Those instructions have an
794 // additional immediate operand which encodes the prefix as reg0 or CPSR.
795 // Just checking for a suffix of 's' definitely creates ambiguities; e.g,
796 // the SMMLS instruction.
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000797 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
798 .Case("eq", ARMCC::EQ)
799 .Case("ne", ARMCC::NE)
800 .Case("hs", ARMCC::HS)
801 .Case("lo", ARMCC::LO)
802 .Case("mi", ARMCC::MI)
803 .Case("pl", ARMCC::PL)
804 .Case("vs", ARMCC::VS)
805 .Case("vc", ARMCC::VC)
806 .Case("hi", ARMCC::HI)
807 .Case("ls", ARMCC::LS)
808 .Case("ge", ARMCC::GE)
809 .Case("lt", ARMCC::LT)
810 .Case("gt", ARMCC::GT)
811 .Case("le", ARMCC::LE)
812 .Case("al", ARMCC::AL)
813 .Default(~0U);
Jim Grosbach16c74252010-10-29 14:46:02 +0000814
Chris Lattnerdba34d82010-10-30 04:35:59 +0000815 if (CC == ~0U ||
816 (CC == ARMCC::LS && (Head == "vmls" || Head == "vnmls"))) {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000817 CC = ARMCC::AL;
Chris Lattnerdba34d82010-10-30 04:35:59 +0000818 } else {
819 Head = Head.slice(0, Head.size() - 2);
Bill Wendling52925b62010-10-29 23:50:21 +0000820 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000821
Chris Lattner3a697562010-10-28 17:20:03 +0000822 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Jim Grosbach469ebbe2010-11-01 18:11:14 +0000823 // FIXME: Should only add this operand for predicated instructions
Chris Lattner3a697562010-10-28 17:20:03 +0000824 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000825
826 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000827 while (Next != StringRef::npos) {
828 Start = Next;
829 Next = Name.find('.', Start + 1);
830 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000831
Chris Lattner3a697562010-10-28 17:20:03 +0000832 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000833 }
834
835 // Read the remaining operands.
836 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000837 // Read the first operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000838 if (ARMOperand *Op = ParseOperand())
839 Operands.push_back(Op);
840 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000841 Parser.EatToEndOfStatement();
842 return true;
843 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000844
845 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000846 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000847
848 // Parse and remember the operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000849 if (ARMOperand *Op = ParseOperand())
850 Operands.push_back(Op);
851 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000852 Parser.EatToEndOfStatement();
853 return true;
854 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000855 }
856 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000857
Chris Lattnercbf8a982010-09-11 16:18:25 +0000858 if (getLexer().isNot(AsmToken::EndOfStatement)) {
859 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000860 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000861 }
Bill Wendling146018f2010-11-06 21:42:12 +0000862
Chris Lattner34e53142010-09-08 05:10:46 +0000863 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000864 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000865}
866
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000867bool ARMAsmParser::
868MatchAndEmitInstruction(SMLoc IDLoc,
869 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
870 MCStreamer &Out) {
871 MCInst Inst;
872 unsigned ErrorInfo;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000873 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
874 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000875 Out.EmitInstruction(Inst);
876 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000877 case Match_MissingFeature:
878 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
879 return true;
880 case Match_InvalidOperand: {
881 SMLoc ErrorLoc = IDLoc;
882 if (ErrorInfo != ~0U) {
883 if (ErrorInfo >= Operands.size())
884 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +0000885
Chris Lattnere73d4f82010-10-28 21:41:58 +0000886 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
887 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
888 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000889
Chris Lattnere73d4f82010-10-28 21:41:58 +0000890 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000891 }
Chris Lattnere73d4f82010-10-28 21:41:58 +0000892 case Match_MnemonicFail:
893 return Error(IDLoc, "unrecognized instruction mnemonic");
894 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000895
Eric Christopherc223e2b2010-10-29 09:26:59 +0000896 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +0000897 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000898}
899
Kevin Enderby515d5092009-10-15 20:48:48 +0000900/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000901bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
902 StringRef IDVal = DirectiveID.getIdentifier();
903 if (IDVal == ".word")
904 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000905 else if (IDVal == ".thumb")
906 return ParseDirectiveThumb(DirectiveID.getLoc());
907 else if (IDVal == ".thumb_func")
908 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
909 else if (IDVal == ".code")
910 return ParseDirectiveCode(DirectiveID.getLoc());
911 else if (IDVal == ".syntax")
912 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000913 return true;
914}
915
916/// ParseDirectiveWord
917/// ::= .word [ expression (, expression)* ]
918bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
919 if (getLexer().isNot(AsmToken::EndOfStatement)) {
920 for (;;) {
921 const MCExpr *Value;
922 if (getParser().ParseExpression(Value))
923 return true;
924
Chris Lattneraaec2052010-01-19 19:46:13 +0000925 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000926
927 if (getLexer().is(AsmToken::EndOfStatement))
928 break;
Jim Grosbach16c74252010-10-29 14:46:02 +0000929
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000930 // FIXME: Improve diagnostic.
931 if (getLexer().isNot(AsmToken::Comma))
932 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000933 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000934 }
935 }
936
Sean Callananb9a25b72010-01-19 20:27:46 +0000937 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000938 return false;
939}
940
Kevin Enderby515d5092009-10-15 20:48:48 +0000941/// ParseDirectiveThumb
942/// ::= .thumb
943bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
944 if (getLexer().isNot(AsmToken::EndOfStatement))
945 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000946 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000947
948 // TODO: set thumb mode
949 // TODO: tell the MC streamer the mode
950 // getParser().getStreamer().Emit???();
951 return false;
952}
953
954/// ParseDirectiveThumbFunc
955/// ::= .thumbfunc symbol_name
956bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000957 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000958 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +0000959 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000960 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +0000961 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000962 if (getLexer().isNot(AsmToken::EndOfStatement))
963 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000964 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000965
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000966 // Mark symbol as a thumb symbol.
967 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
968 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +0000969 return false;
970}
971
972/// ParseDirectiveSyntax
973/// ::= .syntax unified | divided
974bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000975 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000976 if (Tok.isNot(AsmToken::Identifier))
977 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000978 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000979 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000980 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000981 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000982 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000983 else
984 return Error(L, "unrecognized syntax mode in .syntax directive");
985
986 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000987 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000988 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000989
990 // TODO tell the MC streamer the mode
991 // getParser().getStreamer().Emit???();
992 return false;
993}
994
995/// ParseDirectiveCode
996/// ::= .code 16 | 32
997bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000998 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000999 if (Tok.isNot(AsmToken::Integer))
1000 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001001 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001002 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001003 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001004 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001005 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001006 else
1007 return Error(L, "invalid operand to .code directive");
1008
1009 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001010 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001011 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001012
Jim Grosbach2a301702010-11-05 22:40:53 +00001013 if (Val == 16)
1014 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
1015 else
1016 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1017
Kevin Enderby515d5092009-10-15 20:48:48 +00001018 return false;
1019}
1020
Sean Callanan90b70972010-04-07 20:29:34 +00001021extern "C" void LLVMInitializeARMAsmLexer();
1022
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001023/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001024extern "C" void LLVMInitializeARMAsmParser() {
1025 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1026 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001027 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001028}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001029
Chris Lattner0692ee62010-09-06 19:11:01 +00001030#define GET_REGISTER_MATCHER
1031#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001032#include "ARMGenAsmMatcher.inc"