blob: 0d2caa862eac9e06d16e2c3fdddddaa05ea3ea75 [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 Wendling8d5acb72010-11-06 19:56:04 +0000131 unsigned RegStart;
132 unsigned Number;
133 } RegList;
134
Kevin Enderbycfe07242009-10-13 22:19:02 +0000135 struct {
136 const MCExpr *Val;
137 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000138
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000139 // This is for all forms of ARM address expressions
140 struct {
141 unsigned BaseRegNum;
Bill Wendling146018f2010-11-06 21:42:12 +0000142 unsigned OffsetRegNum; // used when OffsetIsReg is true
143 const MCExpr *Offset; // used when OffsetIsReg is false
144 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
145 enum ShiftType ShiftType; // used when OffsetRegShifted is true
146 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
147 unsigned Preindexed : 1;
148 unsigned Postindexed : 1;
149 unsigned OffsetIsReg : 1;
150 unsigned Negative : 1; // only used when OffsetIsReg is true
151 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000152 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000153 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000154
Bill Wendling146018f2010-11-06 21:42:12 +0000155 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
156public:
Sean Callanan76264762010-04-02 22:27:05 +0000157 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
158 Kind = o.Kind;
159 StartLoc = o.StartLoc;
160 EndLoc = o.EndLoc;
161 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000162 case CondCode:
163 CC = o.CC;
164 break;
Sean Callanan76264762010-04-02 22:27:05 +0000165 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000166 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000167 break;
168 case Register:
169 Reg = o.Reg;
170 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000171 case RegisterList:
172 RegList = o.RegList;
173 break;
Sean Callanan76264762010-04-02 22:27:05 +0000174 case Immediate:
175 Imm = o.Imm;
176 break;
177 case Memory:
178 Mem = o.Mem;
179 break;
180 }
181 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000182
Sean Callanan76264762010-04-02 22:27:05 +0000183 /// getStartLoc - Get the location of the first token of this operand.
184 SMLoc getStartLoc() const { return StartLoc; }
185 /// getEndLoc - Get the location of the last token of this operand.
186 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000187
Daniel Dunbar8462b302010-08-11 06:36:53 +0000188 ARMCC::CondCodes getCondCode() const {
189 assert(Kind == CondCode && "Invalid access!");
190 return CC.Val;
191 }
192
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000193 StringRef getToken() const {
194 assert(Kind == Token && "Invalid access!");
195 return StringRef(Tok.Data, Tok.Length);
196 }
197
198 unsigned getReg() const {
Bill Wendling8155e5b2010-11-06 22:19:43 +0000199 assert((Kind == Register || Kind == RegisterList) && "Invalid access!");
200 unsigned RegNum = 0;
201 if (Kind == Register)
202 RegNum = Reg.RegNum;
203 else
204 RegNum = RegList.RegStart;
205 return RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000206 }
207
Bill Wendling8d5acb72010-11-06 19:56:04 +0000208 std::pair<unsigned, unsigned> getRegList() const {
209 assert(Kind == RegisterList && "Invalid access!");
210 return std::make_pair(RegList.RegStart, RegList.Number);
211 }
212
Kevin Enderbycfe07242009-10-13 22:19:02 +0000213 const MCExpr *getImm() const {
214 assert(Kind == Immediate && "Invalid access!");
215 return Imm.Val;
216 }
217
Daniel Dunbar8462b302010-08-11 06:36:53 +0000218 bool isCondCode() const { return Kind == CondCode; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000219 bool isImm() const { return Kind == Immediate; }
Bill Wendlinge8399c62010-11-07 13:08:28 +0000220 bool isReg() const { return Kind == Register || Kind == RegisterList; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000221 bool isRegList() const { return Kind == RegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000222 bool isToken() const { return Kind == Token; }
223 bool isMemory() const { return Kind == Memory; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000224
225 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000226 // Add as immediates when possible. Null MCExpr = 0.
227 if (Expr == 0)
228 Inst.addOperand(MCOperand::CreateImm(0));
229 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000230 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
231 else
232 Inst.addOperand(MCOperand::CreateExpr(Expr));
233 }
234
Daniel Dunbar8462b302010-08-11 06:36:53 +0000235 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000236 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000237 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000238 // FIXME: What belongs here?
239 Inst.addOperand(MCOperand::CreateReg(0));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000240 }
241
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000242 void addRegOperands(MCInst &Inst, unsigned N) const {
243 assert(N == 1 && "Invalid number of operands!");
244 Inst.addOperand(MCOperand::CreateReg(getReg()));
245 }
246
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000247 void addImmOperands(MCInst &Inst, unsigned N) const {
248 assert(N == 1 && "Invalid number of operands!");
249 addExpr(Inst, getImm());
250 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000251
Chris Lattner14b93852010-10-29 00:27:31 +0000252 bool isMemMode5() const {
Chris Lattner14b93852010-10-29 00:27:31 +0000253 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
Jim Grosbach80eb2332010-10-29 17:41:25 +0000254 Mem.Writeback || Mem.Negative)
Chris Lattner14b93852010-10-29 00:27:31 +0000255 return false;
Jim Grosbach80eb2332010-10-29 17:41:25 +0000256 // If there is an offset expression, make sure it's valid.
257 if (!Mem.Offset)
258 return true;
259 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
260 if (!CE)
261 return false;
262 // The offset must be a multiple of 4 in the range 0-1020.
263 int64_t Value = CE->getValue();
264 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
Chris Lattner14b93852010-10-29 00:27:31 +0000265 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000266
Chris Lattner14b93852010-10-29 00:27:31 +0000267 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
268 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000269
Chris Lattner14b93852010-10-29 00:27:31 +0000270 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlinga60f1572010-11-06 10:48:18 +0000271 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000272
Jim Grosbach80eb2332010-10-29 17:41:25 +0000273 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
274 // the difference?
275 if (Mem.Offset) {
276 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000277 assert(CE && "Non-constant mode 5 offset operand!");
278
Jim Grosbach80eb2332010-10-29 17:41:25 +0000279 // The MCInst offset operand doesn't include the low two bits (like
280 // the instruction encoding).
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000281 int64_t Offset = CE->getValue() / 4;
282 if (Offset >= 0)
283 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
284 Offset)));
285 else
286 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
287 -Offset)));
288 } else {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000289 Inst.addOperand(MCOperand::CreateImm(0));
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000290 }
Chris Lattner14b93852010-10-29 00:27:31 +0000291 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000292
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000293 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000294
Chris Lattner3a697562010-10-28 17:20:03 +0000295 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
296 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000297 Op->CC.Val = CC;
298 Op->StartLoc = S;
299 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000300 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000301 }
302
Chris Lattner3a697562010-10-28 17:20:03 +0000303 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
304 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000305 Op->Tok.Data = Str.data();
306 Op->Tok.Length = Str.size();
307 Op->StartLoc = S;
308 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000309 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000310 }
311
Chris Lattner3a697562010-10-28 17:20:03 +0000312 static ARMOperand *CreateReg(unsigned RegNum, bool Writeback, SMLoc S,
313 SMLoc E) {
314 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000315 Op->Reg.RegNum = RegNum;
316 Op->Reg.Writeback = Writeback;
Sean Callanan76264762010-04-02 22:27:05 +0000317 Op->StartLoc = S;
318 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000319 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000320 }
321
Bill Wendling8d5acb72010-11-06 19:56:04 +0000322 static ARMOperand *CreateRegList(unsigned RegStart, unsigned Number,
323 SMLoc S, SMLoc E) {
324 ARMOperand *Op = new ARMOperand(RegisterList);
325 Op->RegList.RegStart = RegStart;
326 Op->RegList.Number = Number;
327 Op->StartLoc = S;
328 Op->EndLoc = E;
329 return Op;
330 }
331
Chris Lattner3a697562010-10-28 17:20:03 +0000332 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
333 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000334 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000335 Op->StartLoc = S;
336 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000337 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000338 }
339
Chris Lattner3a697562010-10-28 17:20:03 +0000340 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
341 const MCExpr *Offset, unsigned OffsetRegNum,
342 bool OffsetRegShifted, enum ShiftType ShiftType,
343 const MCExpr *ShiftAmount, bool Preindexed,
344 bool Postindexed, bool Negative, bool Writeback,
345 SMLoc S, SMLoc E) {
346 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000347 Op->Mem.BaseRegNum = BaseRegNum;
348 Op->Mem.OffsetIsReg = OffsetIsReg;
349 Op->Mem.Offset = Offset;
350 Op->Mem.OffsetRegNum = OffsetRegNum;
351 Op->Mem.OffsetRegShifted = OffsetRegShifted;
352 Op->Mem.ShiftType = ShiftType;
353 Op->Mem.ShiftAmount = ShiftAmount;
354 Op->Mem.Preindexed = Preindexed;
355 Op->Mem.Postindexed = Postindexed;
356 Op->Mem.Negative = Negative;
357 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000358
Sean Callanan76264762010-04-02 22:27:05 +0000359 Op->StartLoc = S;
360 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000361 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000362 }
363};
364
365} // end anonymous namespace.
366
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000367void ARMOperand::dump(raw_ostream &OS) const {
368 switch (Kind) {
369 case CondCode:
370 OS << ARMCondCodeToString(getCondCode());
371 break;
372 case Immediate:
373 getImm()->print(OS);
374 break;
375 case Memory:
376 OS << "<memory>";
377 break;
378 case Register:
379 OS << "<register " << getReg() << ">";
380 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000381 case RegisterList: {
382 OS << "<register_list ";
383 std::pair<unsigned, unsigned> List = getRegList();
384 unsigned RegEnd = List.first + List.second;
385
386 for (unsigned Idx = List.first; Idx < RegEnd; ) {
387 OS << Idx;
388 if (++Idx < RegEnd) OS << ", ";
389 }
390
391 OS << ">";
392 break;
393 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000394 case Token:
395 OS << "'" << getToken() << "'";
396 break;
397 }
398}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000399
400/// @name Auto-generated Match Functions
401/// {
402
403static unsigned MatchRegisterName(StringRef Name);
404
405/// }
406
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000407/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000408/// and if it is a register name the token is eaten and the register number is
409/// returned. Otherwise return -1.
410///
411int ARMAsmParser::TryParseRegister() {
412 const AsmToken &Tok = Parser.getTok();
413 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000414
Chris Lattnere5658fa2010-10-30 04:09:10 +0000415 // FIXME: Validate register for the current architecture; we have to do
416 // validation later, so maybe there is no need for this here.
Bill Wendlingd68fd9c2010-11-06 10:45:34 +0000417 unsigned RegNum = MatchRegisterName(Tok.getString());
418 if (RegNum == 0)
Chris Lattnere5658fa2010-10-30 04:09:10 +0000419 return -1;
420 Parser.Lex(); // Eat identifier token.
421 return RegNum;
422}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000423
424
Chris Lattnere5658fa2010-10-30 04:09:10 +0000425/// Try to parse a register name. The token must be an Identifier when called,
426/// and if it is a register name the token is eaten and the register number is
427/// returned. Otherwise return -1.
Chris Lattner3a697562010-10-28 17:20:03 +0000428///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000429/// TODO this is likely to change to allow different register types and or to
430/// parse for a specific register type.
Chris Lattnere5658fa2010-10-30 04:09:10 +0000431ARMOperand *ARMAsmParser::TryParseRegisterWithWriteBack() {
432 SMLoc S = Parser.getTok().getLoc();
433 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000434 if (RegNo == -1)
435 return 0;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000436
Chris Lattnere5658fa2010-10-30 04:09:10 +0000437 SMLoc E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000438
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000439 bool Writeback = false;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000440 const AsmToken &ExclaimTok = Parser.getTok();
441 if (ExclaimTok.is(AsmToken::Exclaim)) {
442 E = ExclaimTok.getLoc();
443 Writeback = true;
444 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000445 }
446
Chris Lattnere5658fa2010-10-30 04:09:10 +0000447 return ARMOperand::CreateReg(RegNo, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000448}
449
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000450/// Parse a register list, return it if successful else return null. The first
451/// token must be a '{' when called.
452ARMOperand *ARMAsmParser::ParseRegisterList() {
Sean Callanan18b83232010-01-19 21:44:56 +0000453 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000454 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000455 SMLoc S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000456 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000457
Sean Callanan18b83232010-01-19 21:44:56 +0000458 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000459 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000460 if (RegTok.isNot(AsmToken::Identifier)) {
461 Error(RegLoc, "register expected");
462 return 0;
463 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000464
Bill Wendling1d6a2652010-11-06 10:40:24 +0000465 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000466 if (RegNum == -1) {
467 Error(RegLoc, "register expected");
468 return 0;
469 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000470
Bill Wendlinge7176102010-11-06 22:36:58 +0000471 unsigned PrevRegNum = RegNum;
472 std::vector<std::pair<unsigned, SMLoc> > Registers;
473 Registers.reserve(32);
474 Registers.push_back(std::make_pair(RegNum, RegLoc));
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000475
Bill Wendlinge7176102010-11-06 22:36:58 +0000476 while (Parser.getTok().is(AsmToken::Comma) ||
477 Parser.getTok().is(AsmToken::Minus)) {
478 bool IsRange = Parser.getTok().is(AsmToken::Minus);
479 Parser.Lex(); // Eat comma or minus token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000480
Sean Callanan18b83232010-01-19 21:44:56 +0000481 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000482 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000483 if (RegTok.isNot(AsmToken::Identifier)) {
484 Error(RegLoc, "register expected");
485 return 0;
486 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000487
Bill Wendling1d6a2652010-11-06 10:40:24 +0000488 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000489 if (RegNum == -1) {
490 Error(RegLoc, "register expected");
491 return 0;
492 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000493
Bill Wendlinge7176102010-11-06 22:36:58 +0000494 if (IsRange) {
495 int Reg = PrevRegNum;
496 do {
497 ++Reg;
498 Registers.push_back(std::make_pair(Reg, RegLoc));
499 } while (Reg != RegNum);
500 } else {
501 Registers.push_back(std::make_pair(RegNum, RegLoc));
502 }
503
504 PrevRegNum = RegNum;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000505 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000506
507 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000508 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000509 if (RCurlyTok.isNot(AsmToken::RCurly)) {
510 Error(RCurlyTok.getLoc(), "'}' expected");
511 return 0;
512 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000513
Bill Wendlinge7176102010-11-06 22:36:58 +0000514 SMLoc E = RCurlyTok.getLoc();
515 Parser.Lex(); // Eat right curly brace token.
516
517 // Verify the register list.
518 std::vector<std::pair<unsigned, SMLoc> >::iterator
519 RI = Registers.begin(), RE = Registers.end();
520
521 unsigned Number = Registers.size();
522 unsigned HighRegNum = RI->first;
523 unsigned RegStart = RI->first;
524
525 DenseMap<unsigned, bool> RegMap;
526 RegMap[RI->first] = true;
527
528 for (++RI; RI != RE; ++RI) {
529 std::pair<unsigned, SMLoc> &RegInfo = *RI;
530
531 if (RegMap[RegInfo.first]) {
532 Error(RegInfo.second, "register duplicated in register list");
533 return 0;
534 }
535
536 if (RegInfo.first < HighRegNum)
537 Warning(RegInfo.second,
538 "register not in ascending order in register list");
539
540 RegMap[RegInfo.first] = true;
541 HighRegNum = std::max(RegInfo.first, HighRegNum);
542 RegStart = std::min(RegInfo.first, RegStart);
543 }
544
545 if (RegStart + Number - 1 != HighRegNum) {
546 Error(RegLoc, "non-contiguous register range");
547 return 0;
548 }
549
550 return ARMOperand::CreateRegList(RegStart, Number, S, E);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000551}
552
Bill Wendlinge7176102010-11-06 22:36:58 +0000553/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000554/// or an error. The first token must be a '[' when called.
555/// TODO Only preindexing and postindexing addressing are started, unindexed
556/// with option, etc are still to do.
Chris Lattner550276e2010-10-28 20:52:15 +0000557ARMOperand *ARMAsmParser::ParseMemory() {
Sean Callanan76264762010-04-02 22:27:05 +0000558 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000559 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000560 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000561 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000562 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000563
Sean Callanan18b83232010-01-19 21:44:56 +0000564 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000565 if (BaseRegTok.isNot(AsmToken::Identifier)) {
566 Error(BaseRegTok.getLoc(), "register expected");
567 return 0;
568 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000569 int BaseRegNum = TryParseRegister();
570 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000571 Error(BaseRegTok.getLoc(), "register expected");
572 return 0;
573 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000574
575 bool Preindexed = false;
576 bool Postindexed = false;
577 bool OffsetIsReg = false;
578 bool Negative = false;
579 bool Writeback = false;
580
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000581 // First look for preindexed address forms, that is after the "[Rn" we now
582 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000583 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000584 if (Tok.is(AsmToken::Comma)) {
585 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000586 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000587 int OffsetRegNum;
588 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000589 enum ShiftType ShiftType;
590 const MCExpr *ShiftAmount;
591 const MCExpr *Offset;
Chris Lattner550276e2010-10-28 20:52:15 +0000592 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
593 Offset, OffsetIsReg, OffsetRegNum, E))
594 return 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000595 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000596 if (RBracTok.isNot(AsmToken::RBrac)) {
597 Error(RBracTok.getLoc(), "']' expected");
598 return 0;
599 }
Sean Callanan76264762010-04-02 22:27:05 +0000600 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000601 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000602
Sean Callanan18b83232010-01-19 21:44:56 +0000603 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000604 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000605 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000606 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000607 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000608 }
Chris Lattner550276e2010-10-28 20:52:15 +0000609 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
610 OffsetRegShifted, ShiftType, ShiftAmount,
611 Preindexed, Postindexed, Negative, Writeback,
612 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000613 }
614 // The "[Rn" we have so far was not followed by a comma.
615 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000616 // If there's anything other than the right brace, this is a post indexing
617 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000618 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000619 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000620
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000621 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000622 bool OffsetRegShifted = false;
623 enum ShiftType ShiftType;
624 const MCExpr *ShiftAmount;
Chris Lattner14b93852010-10-29 00:27:31 +0000625 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000626
Sean Callanan18b83232010-01-19 21:44:56 +0000627 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000628 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000629 Postindexed = true;
630 Writeback = true;
Chris Lattner550276e2010-10-28 20:52:15 +0000631 if (NextTok.isNot(AsmToken::Comma)) {
632 Error(NextTok.getLoc(), "',' expected");
633 return 0;
634 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000635 Parser.Lex(); // Eat comma token.
Chris Lattner550276e2010-10-28 20:52:15 +0000636 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000637 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000638 E))
639 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000640 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000641
Chris Lattner550276e2010-10-28 20:52:15 +0000642 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
643 OffsetRegShifted, ShiftType, ShiftAmount,
644 Preindexed, Postindexed, Negative, Writeback,
645 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000646 }
647
Chris Lattner550276e2010-10-28 20:52:15 +0000648 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000649}
650
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000651/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
652/// we will parse the following (were +/- means that a plus or minus is
653/// optional):
654/// +/-Rm
655/// +/-Rm, shift
656/// #offset
657/// we return false on success or an error otherwise.
658bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000659 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000660 enum ShiftType &ShiftType,
661 const MCExpr *&ShiftAmount,
662 const MCExpr *&Offset,
663 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000664 int &OffsetRegNum,
665 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000666 Negative = false;
667 OffsetRegShifted = false;
668 OffsetIsReg = false;
669 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000670 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000671 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000672 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000673 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000674 else if (NextTok.is(AsmToken::Minus)) {
675 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000676 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000677 }
678 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000679 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000680 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000681 SMLoc CurLoc = OffsetRegTok.getLoc();
682 OffsetRegNum = TryParseRegister();
683 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000684 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000685 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000686 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000687 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000688
Bill Wendling12f40e92010-11-06 10:51:53 +0000689 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000690 if (OffsetRegNum != -1) {
691 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000692 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000693 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000694 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000695
Sean Callanan18b83232010-01-19 21:44:56 +0000696 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000697 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000698 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000699 OffsetRegShifted = true;
700 }
701 }
702 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
703 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000704 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000705 if (HashTok.isNot(AsmToken::Hash))
706 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000707
Sean Callananb9a25b72010-01-19 20:27:46 +0000708 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000709
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000710 if (getParser().ParseExpression(Offset))
711 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000712 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000713 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000714 return false;
715}
716
717/// ParseShift as one of these two:
718/// ( lsl | lsr | asr | ror ) , # shift_amount
719/// rrx
720/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000721bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000722 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000723 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000724 if (Tok.isNot(AsmToken::Identifier))
725 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000726 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000727 if (ShiftName == "lsl" || ShiftName == "LSL")
728 St = Lsl;
729 else if (ShiftName == "lsr" || ShiftName == "LSR")
730 St = Lsr;
731 else if (ShiftName == "asr" || ShiftName == "ASR")
732 St = Asr;
733 else if (ShiftName == "ror" || ShiftName == "ROR")
734 St = Ror;
735 else if (ShiftName == "rrx" || ShiftName == "RRX")
736 St = Rrx;
737 else
738 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000739 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000740
741 // Rrx stands alone.
742 if (St == Rrx)
743 return false;
744
745 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000746 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000747 if (HashTok.isNot(AsmToken::Hash))
748 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000749 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000750
751 if (getParser().ParseExpression(ShiftAmount))
752 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000753
754 return false;
755}
756
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000757/// Parse a arm instruction operand. For now this parses the operand regardless
758/// of the mnemonic.
Chris Lattner550276e2010-10-28 20:52:15 +0000759ARMOperand *ARMAsmParser::ParseOperand() {
Sean Callanan76264762010-04-02 22:27:05 +0000760 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000761 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000762 default:
763 Error(Parser.getTok().getLoc(), "unexpected token in operand");
764 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000765 case AsmToken::Identifier:
Chris Lattnere5658fa2010-10-30 04:09:10 +0000766 if (ARMOperand *Op = TryParseRegisterWithWriteBack())
Chris Lattner550276e2010-10-28 20:52:15 +0000767 return Op;
Jim Grosbach16c74252010-10-29 14:46:02 +0000768
Kevin Enderby515d5092009-10-15 20:48:48 +0000769 // This was not a register so parse other operands that start with an
770 // identifier (like labels) as expressions and create them as immediates.
771 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000772 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000773 if (getParser().ParseExpression(IdVal))
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(IdVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000777 case AsmToken::LBrac:
Chris Lattner550276e2010-10-28 20:52:15 +0000778 return ParseMemory();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000779 case AsmToken::LCurly:
Chris Lattner550276e2010-10-28 20:52:15 +0000780 return ParseRegisterList();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000781 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000782 // #42 -> immediate.
783 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000784 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000785 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000786 const MCExpr *ImmVal;
787 if (getParser().ParseExpression(ImmVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000788 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000789 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000790 return ARMOperand::CreateImm(ImmVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000791 }
792}
793
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000794/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000795bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000796 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000797 // Create the leading tokens for the mnemonic, split by '.' characters.
798 size_t Start = 0, Next = Name.find('.');
799 StringRef Head = Name.slice(Start, Next);
800
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000801 // Determine the predicate, if any.
802 //
803 // FIXME: We need a way to check whether a prefix supports predication,
804 // otherwise we will end up with an ambiguity for instructions that happen to
805 // end with a predicate name.
Jim Grosbach3df518e2010-10-29 21:56:51 +0000806 // FIXME: Likewise, some arithmetic instructions have an 's' prefix which
807 // indicates to update the condition codes. Those instructions have an
808 // additional immediate operand which encodes the prefix as reg0 or CPSR.
809 // Just checking for a suffix of 's' definitely creates ambiguities; e.g,
810 // the SMMLS instruction.
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000811 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
812 .Case("eq", ARMCC::EQ)
813 .Case("ne", ARMCC::NE)
814 .Case("hs", ARMCC::HS)
815 .Case("lo", ARMCC::LO)
816 .Case("mi", ARMCC::MI)
817 .Case("pl", ARMCC::PL)
818 .Case("vs", ARMCC::VS)
819 .Case("vc", ARMCC::VC)
820 .Case("hi", ARMCC::HI)
821 .Case("ls", ARMCC::LS)
822 .Case("ge", ARMCC::GE)
823 .Case("lt", ARMCC::LT)
824 .Case("gt", ARMCC::GT)
825 .Case("le", ARMCC::LE)
826 .Case("al", ARMCC::AL)
827 .Default(~0U);
Jim Grosbach16c74252010-10-29 14:46:02 +0000828
Chris Lattnerdba34d82010-10-30 04:35:59 +0000829 if (CC == ~0U ||
830 (CC == ARMCC::LS && (Head == "vmls" || Head == "vnmls"))) {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000831 CC = ARMCC::AL;
Chris Lattnerdba34d82010-10-30 04:35:59 +0000832 } else {
833 Head = Head.slice(0, Head.size() - 2);
Bill Wendling52925b62010-10-29 23:50:21 +0000834 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000835
Chris Lattner3a697562010-10-28 17:20:03 +0000836 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Jim Grosbach469ebbe2010-11-01 18:11:14 +0000837 // FIXME: Should only add this operand for predicated instructions
Chris Lattner3a697562010-10-28 17:20:03 +0000838 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000839
840 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000841 while (Next != StringRef::npos) {
842 Start = Next;
843 Next = Name.find('.', Start + 1);
844 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000845
Chris Lattner3a697562010-10-28 17:20:03 +0000846 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000847 }
848
849 // Read the remaining operands.
850 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000851 // Read the first operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000852 if (ARMOperand *Op = ParseOperand())
853 Operands.push_back(Op);
854 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000855 Parser.EatToEndOfStatement();
856 return true;
857 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000858
859 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000860 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000861
862 // Parse and remember the operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000863 if (ARMOperand *Op = ParseOperand())
864 Operands.push_back(Op);
865 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000866 Parser.EatToEndOfStatement();
867 return true;
868 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000869 }
870 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000871
Chris Lattnercbf8a982010-09-11 16:18:25 +0000872 if (getLexer().isNot(AsmToken::EndOfStatement)) {
873 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000874 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000875 }
Bill Wendling146018f2010-11-06 21:42:12 +0000876
Chris Lattner34e53142010-09-08 05:10:46 +0000877 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000878 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000879}
880
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000881bool ARMAsmParser::
882MatchAndEmitInstruction(SMLoc IDLoc,
883 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
884 MCStreamer &Out) {
885 MCInst Inst;
886 unsigned ErrorInfo;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000887 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
888 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000889 Out.EmitInstruction(Inst);
890 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000891 case Match_MissingFeature:
892 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
893 return true;
894 case Match_InvalidOperand: {
895 SMLoc ErrorLoc = IDLoc;
896 if (ErrorInfo != ~0U) {
897 if (ErrorInfo >= Operands.size())
898 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +0000899
Chris Lattnere73d4f82010-10-28 21:41:58 +0000900 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
901 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
902 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000903
Chris Lattnere73d4f82010-10-28 21:41:58 +0000904 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000905 }
Chris Lattnere73d4f82010-10-28 21:41:58 +0000906 case Match_MnemonicFail:
907 return Error(IDLoc, "unrecognized instruction mnemonic");
908 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000909
Eric Christopherc223e2b2010-10-29 09:26:59 +0000910 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +0000911 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000912}
913
Kevin Enderby515d5092009-10-15 20:48:48 +0000914/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000915bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
916 StringRef IDVal = DirectiveID.getIdentifier();
917 if (IDVal == ".word")
918 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000919 else if (IDVal == ".thumb")
920 return ParseDirectiveThumb(DirectiveID.getLoc());
921 else if (IDVal == ".thumb_func")
922 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
923 else if (IDVal == ".code")
924 return ParseDirectiveCode(DirectiveID.getLoc());
925 else if (IDVal == ".syntax")
926 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000927 return true;
928}
929
930/// ParseDirectiveWord
931/// ::= .word [ expression (, expression)* ]
932bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
933 if (getLexer().isNot(AsmToken::EndOfStatement)) {
934 for (;;) {
935 const MCExpr *Value;
936 if (getParser().ParseExpression(Value))
937 return true;
938
Chris Lattneraaec2052010-01-19 19:46:13 +0000939 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000940
941 if (getLexer().is(AsmToken::EndOfStatement))
942 break;
Jim Grosbach16c74252010-10-29 14:46:02 +0000943
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000944 // FIXME: Improve diagnostic.
945 if (getLexer().isNot(AsmToken::Comma))
946 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000947 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000948 }
949 }
950
Sean Callananb9a25b72010-01-19 20:27:46 +0000951 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000952 return false;
953}
954
Kevin Enderby515d5092009-10-15 20:48:48 +0000955/// ParseDirectiveThumb
956/// ::= .thumb
957bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
958 if (getLexer().isNot(AsmToken::EndOfStatement))
959 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000960 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000961
962 // TODO: set thumb mode
963 // TODO: tell the MC streamer the mode
964 // getParser().getStreamer().Emit???();
965 return false;
966}
967
968/// ParseDirectiveThumbFunc
969/// ::= .thumbfunc symbol_name
970bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000971 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000972 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +0000973 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000974 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +0000975 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000976 if (getLexer().isNot(AsmToken::EndOfStatement))
977 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000978 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000979
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000980 // Mark symbol as a thumb symbol.
981 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
982 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +0000983 return false;
984}
985
986/// ParseDirectiveSyntax
987/// ::= .syntax unified | divided
988bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000989 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000990 if (Tok.isNot(AsmToken::Identifier))
991 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000992 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000993 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000994 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000995 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000996 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000997 else
998 return Error(L, "unrecognized syntax mode in .syntax directive");
999
1000 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001001 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001002 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001003
1004 // TODO tell the MC streamer the mode
1005 // getParser().getStreamer().Emit???();
1006 return false;
1007}
1008
1009/// ParseDirectiveCode
1010/// ::= .code 16 | 32
1011bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001012 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001013 if (Tok.isNot(AsmToken::Integer))
1014 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001015 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001016 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001017 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001018 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001019 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001020 else
1021 return Error(L, "invalid operand to .code directive");
1022
1023 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001024 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001025 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001026
Jim Grosbach2a301702010-11-05 22:40:53 +00001027 if (Val == 16)
1028 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
1029 else
1030 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1031
Kevin Enderby515d5092009-10-15 20:48:48 +00001032 return false;
1033}
1034
Sean Callanan90b70972010-04-07 20:29:34 +00001035extern "C" void LLVMInitializeARMAsmLexer();
1036
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001037/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001038extern "C" void LLVMInitializeARMAsmParser() {
1039 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1040 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001041 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001042}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001043
Chris Lattner0692ee62010-09-06 19:11:01 +00001044#define GET_REGISTER_MATCHER
1045#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001046#include "ARMGenAsmMatcher.inc"