blob: 71fa27cb46ff6f5eb2f49b5269f4eec0237a3ab7 [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 Wendlingb32e7842010-11-08 00:32:40 +0000220 bool isReg() const { return Kind == Register; }
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; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000224 bool isMemMode5() const {
225 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
226 Mem.Writeback || Mem.Negative)
227 return false;
228 // If there is an offset expression, make sure it's valid.
229 if (!Mem.Offset)
230 return true;
231 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
232 if (!CE)
233 return false;
234 // The offset must be a multiple of 4 in the range 0-1020.
235 int64_t Value = CE->getValue();
236 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
237 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000238
239 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000240 // Add as immediates when possible. Null MCExpr = 0.
241 if (Expr == 0)
242 Inst.addOperand(MCOperand::CreateImm(0));
243 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000244 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
245 else
246 Inst.addOperand(MCOperand::CreateExpr(Expr));
247 }
248
Daniel Dunbar8462b302010-08-11 06:36:53 +0000249 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000250 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000251 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000252 // FIXME: What belongs here?
253 Inst.addOperand(MCOperand::CreateReg(0));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000254 }
255
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000256 void addRegOperands(MCInst &Inst, unsigned N) const {
257 assert(N == 1 && "Invalid number of operands!");
258 Inst.addOperand(MCOperand::CreateReg(getReg()));
259 }
260
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000261 void addRegListOperands(MCInst &Inst, unsigned N) const {
262 assert(N == 2 && "Invalid number of operands!");
263 std::pair<unsigned, unsigned> RegList = getRegList();
264 Inst.addOperand(MCOperand::CreateReg(RegList.first));
265 Inst.addOperand(MCOperand::CreateImm(RegList.second));
266 }
267
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000268 void addImmOperands(MCInst &Inst, unsigned N) const {
269 assert(N == 1 && "Invalid number of operands!");
270 addExpr(Inst, getImm());
271 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000272
Chris Lattner14b93852010-10-29 00:27:31 +0000273 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
274 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000275
Chris Lattner14b93852010-10-29 00:27:31 +0000276 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlinga60f1572010-11-06 10:48:18 +0000277 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000278
Jim Grosbach80eb2332010-10-29 17:41:25 +0000279 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
280 // the difference?
281 if (Mem.Offset) {
282 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000283 assert(CE && "Non-constant mode 5 offset operand!");
284
Jim Grosbach80eb2332010-10-29 17:41:25 +0000285 // The MCInst offset operand doesn't include the low two bits (like
286 // the instruction encoding).
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000287 int64_t Offset = CE->getValue() / 4;
288 if (Offset >= 0)
289 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
290 Offset)));
291 else
292 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
293 -Offset)));
294 } else {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000295 Inst.addOperand(MCOperand::CreateImm(0));
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000296 }
Chris Lattner14b93852010-10-29 00:27:31 +0000297 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000298
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000299 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000300
Chris Lattner3a697562010-10-28 17:20:03 +0000301 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
302 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000303 Op->CC.Val = CC;
304 Op->StartLoc = S;
305 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000306 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000307 }
308
Chris Lattner3a697562010-10-28 17:20:03 +0000309 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
310 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000311 Op->Tok.Data = Str.data();
312 Op->Tok.Length = Str.size();
313 Op->StartLoc = S;
314 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000315 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000316 }
317
Chris Lattner3a697562010-10-28 17:20:03 +0000318 static ARMOperand *CreateReg(unsigned RegNum, bool Writeback, SMLoc S,
319 SMLoc E) {
320 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000321 Op->Reg.RegNum = RegNum;
322 Op->Reg.Writeback = Writeback;
Sean Callanan76264762010-04-02 22:27:05 +0000323 Op->StartLoc = S;
324 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000325 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000326 }
327
Bill Wendling8d5acb72010-11-06 19:56:04 +0000328 static ARMOperand *CreateRegList(unsigned RegStart, unsigned Number,
329 SMLoc S, SMLoc E) {
330 ARMOperand *Op = new ARMOperand(RegisterList);
331 Op->RegList.RegStart = RegStart;
332 Op->RegList.Number = Number;
333 Op->StartLoc = S;
334 Op->EndLoc = E;
335 return Op;
336 }
337
Chris Lattner3a697562010-10-28 17:20:03 +0000338 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
339 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000340 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000341 Op->StartLoc = S;
342 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000343 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000344 }
345
Chris Lattner3a697562010-10-28 17:20:03 +0000346 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
347 const MCExpr *Offset, unsigned OffsetRegNum,
348 bool OffsetRegShifted, enum ShiftType ShiftType,
349 const MCExpr *ShiftAmount, bool Preindexed,
350 bool Postindexed, bool Negative, bool Writeback,
351 SMLoc S, SMLoc E) {
352 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000353 Op->Mem.BaseRegNum = BaseRegNum;
354 Op->Mem.OffsetIsReg = OffsetIsReg;
355 Op->Mem.Offset = Offset;
356 Op->Mem.OffsetRegNum = OffsetRegNum;
357 Op->Mem.OffsetRegShifted = OffsetRegShifted;
358 Op->Mem.ShiftType = ShiftType;
359 Op->Mem.ShiftAmount = ShiftAmount;
360 Op->Mem.Preindexed = Preindexed;
361 Op->Mem.Postindexed = Postindexed;
362 Op->Mem.Negative = Negative;
363 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000364
Sean Callanan76264762010-04-02 22:27:05 +0000365 Op->StartLoc = S;
366 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000367 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000368 }
369};
370
371} // end anonymous namespace.
372
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000373void ARMOperand::dump(raw_ostream &OS) const {
374 switch (Kind) {
375 case CondCode:
376 OS << ARMCondCodeToString(getCondCode());
377 break;
378 case Immediate:
379 getImm()->print(OS);
380 break;
381 case Memory:
382 OS << "<memory>";
383 break;
384 case Register:
385 OS << "<register " << getReg() << ">";
386 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000387 case RegisterList: {
388 OS << "<register_list ";
389 std::pair<unsigned, unsigned> List = getRegList();
390 unsigned RegEnd = List.first + List.second;
391
392 for (unsigned Idx = List.first; Idx < RegEnd; ) {
393 OS << Idx;
394 if (++Idx < RegEnd) OS << ", ";
395 }
396
397 OS << ">";
398 break;
399 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000400 case Token:
401 OS << "'" << getToken() << "'";
402 break;
403 }
404}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000405
406/// @name Auto-generated Match Functions
407/// {
408
409static unsigned MatchRegisterName(StringRef Name);
410
411/// }
412
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000413/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000414/// and if it is a register name the token is eaten and the register number is
415/// returned. Otherwise return -1.
416///
417int ARMAsmParser::TryParseRegister() {
418 const AsmToken &Tok = Parser.getTok();
419 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000420
Chris Lattnere5658fa2010-10-30 04:09:10 +0000421 // FIXME: Validate register for the current architecture; we have to do
422 // validation later, so maybe there is no need for this here.
Bill Wendlingd68fd9c2010-11-06 10:45:34 +0000423 unsigned RegNum = MatchRegisterName(Tok.getString());
424 if (RegNum == 0)
Chris Lattnere5658fa2010-10-30 04:09:10 +0000425 return -1;
426 Parser.Lex(); // Eat identifier token.
427 return RegNum;
428}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000429
430
Chris Lattnere5658fa2010-10-30 04:09:10 +0000431/// Try to parse a register name. The token must be an Identifier when called,
432/// and if it is a register name the token is eaten and the register number is
433/// returned. Otherwise return -1.
Chris Lattner3a697562010-10-28 17:20:03 +0000434///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000435/// TODO this is likely to change to allow different register types and or to
436/// parse for a specific register type.
Chris Lattnere5658fa2010-10-30 04:09:10 +0000437ARMOperand *ARMAsmParser::TryParseRegisterWithWriteBack() {
438 SMLoc S = Parser.getTok().getLoc();
439 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000440 if (RegNo == -1)
441 return 0;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000442
Chris Lattnere5658fa2010-10-30 04:09:10 +0000443 SMLoc E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000444
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000445 bool Writeback = false;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000446 const AsmToken &ExclaimTok = Parser.getTok();
447 if (ExclaimTok.is(AsmToken::Exclaim)) {
448 E = ExclaimTok.getLoc();
449 Writeback = true;
450 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000451 }
452
Chris Lattnere5658fa2010-10-30 04:09:10 +0000453 return ARMOperand::CreateReg(RegNo, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000454}
455
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000456/// Parse a register list, return it if successful else return null. The first
457/// token must be a '{' when called.
458ARMOperand *ARMAsmParser::ParseRegisterList() {
Sean Callanan18b83232010-01-19 21:44:56 +0000459 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000460 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000461 SMLoc S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000462 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000463
Sean Callanan18b83232010-01-19 21:44:56 +0000464 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000465 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000466 if (RegTok.isNot(AsmToken::Identifier)) {
467 Error(RegLoc, "register expected");
468 return 0;
469 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000470
Bill Wendling1d6a2652010-11-06 10:40:24 +0000471 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000472 if (RegNum == -1) {
473 Error(RegLoc, "register expected");
474 return 0;
475 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000476
Bill Wendlinge7176102010-11-06 22:36:58 +0000477 unsigned PrevRegNum = RegNum;
478 std::vector<std::pair<unsigned, SMLoc> > Registers;
479 Registers.reserve(32);
480 Registers.push_back(std::make_pair(RegNum, RegLoc));
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000481
Bill Wendlinge7176102010-11-06 22:36:58 +0000482 while (Parser.getTok().is(AsmToken::Comma) ||
483 Parser.getTok().is(AsmToken::Minus)) {
484 bool IsRange = Parser.getTok().is(AsmToken::Minus);
485 Parser.Lex(); // Eat comma or minus token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000486
Sean Callanan18b83232010-01-19 21:44:56 +0000487 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000488 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000489 if (RegTok.isNot(AsmToken::Identifier)) {
490 Error(RegLoc, "register expected");
491 return 0;
492 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000493
Bill Wendling1d6a2652010-11-06 10:40:24 +0000494 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000495 if (RegNum == -1) {
496 Error(RegLoc, "register expected");
497 return 0;
498 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000499
Bill Wendlinge7176102010-11-06 22:36:58 +0000500 if (IsRange) {
501 int Reg = PrevRegNum;
502 do {
503 ++Reg;
504 Registers.push_back(std::make_pair(Reg, RegLoc));
505 } while (Reg != RegNum);
506 } else {
507 Registers.push_back(std::make_pair(RegNum, RegLoc));
508 }
509
510 PrevRegNum = RegNum;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000511 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000512
513 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000514 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000515 if (RCurlyTok.isNot(AsmToken::RCurly)) {
516 Error(RCurlyTok.getLoc(), "'}' expected");
517 return 0;
518 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000519
Bill Wendlinge7176102010-11-06 22:36:58 +0000520 SMLoc E = RCurlyTok.getLoc();
521 Parser.Lex(); // Eat right curly brace token.
522
523 // Verify the register list.
524 std::vector<std::pair<unsigned, SMLoc> >::iterator
525 RI = Registers.begin(), RE = Registers.end();
526
527 unsigned Number = Registers.size();
528 unsigned HighRegNum = RI->first;
529 unsigned RegStart = RI->first;
530
531 DenseMap<unsigned, bool> RegMap;
532 RegMap[RI->first] = true;
533
534 for (++RI; RI != RE; ++RI) {
535 std::pair<unsigned, SMLoc> &RegInfo = *RI;
536
537 if (RegMap[RegInfo.first]) {
538 Error(RegInfo.second, "register duplicated in register list");
539 return 0;
540 }
541
542 if (RegInfo.first < HighRegNum)
543 Warning(RegInfo.second,
544 "register not in ascending order in register list");
545
546 RegMap[RegInfo.first] = true;
547 HighRegNum = std::max(RegInfo.first, HighRegNum);
548 RegStart = std::min(RegInfo.first, RegStart);
549 }
550
551 if (RegStart + Number - 1 != HighRegNum) {
552 Error(RegLoc, "non-contiguous register range");
553 return 0;
554 }
555
556 return ARMOperand::CreateRegList(RegStart, Number, S, E);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000557}
558
Bill Wendlinge7176102010-11-06 22:36:58 +0000559/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000560/// or an error. The first token must be a '[' when called.
561/// TODO Only preindexing and postindexing addressing are started, unindexed
562/// with option, etc are still to do.
Chris Lattner550276e2010-10-28 20:52:15 +0000563ARMOperand *ARMAsmParser::ParseMemory() {
Sean Callanan76264762010-04-02 22:27:05 +0000564 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000565 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000566 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000567 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000568 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000569
Sean Callanan18b83232010-01-19 21:44:56 +0000570 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000571 if (BaseRegTok.isNot(AsmToken::Identifier)) {
572 Error(BaseRegTok.getLoc(), "register expected");
573 return 0;
574 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000575 int BaseRegNum = TryParseRegister();
576 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000577 Error(BaseRegTok.getLoc(), "register expected");
578 return 0;
579 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000580
581 bool Preindexed = false;
582 bool Postindexed = false;
583 bool OffsetIsReg = false;
584 bool Negative = false;
585 bool Writeback = false;
586
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000587 // First look for preindexed address forms, that is after the "[Rn" we now
588 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000589 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000590 if (Tok.is(AsmToken::Comma)) {
591 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000592 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000593 int OffsetRegNum;
594 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000595 enum ShiftType ShiftType;
596 const MCExpr *ShiftAmount;
597 const MCExpr *Offset;
Chris Lattner550276e2010-10-28 20:52:15 +0000598 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
599 Offset, OffsetIsReg, OffsetRegNum, E))
600 return 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000601 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000602 if (RBracTok.isNot(AsmToken::RBrac)) {
603 Error(RBracTok.getLoc(), "']' expected");
604 return 0;
605 }
Sean Callanan76264762010-04-02 22:27:05 +0000606 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000607 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000608
Sean Callanan18b83232010-01-19 21:44:56 +0000609 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000610 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000611 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000612 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000613 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000614 }
Chris Lattner550276e2010-10-28 20:52:15 +0000615 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
616 OffsetRegShifted, ShiftType, ShiftAmount,
617 Preindexed, Postindexed, Negative, Writeback,
618 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000619 }
620 // The "[Rn" we have so far was not followed by a comma.
621 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000622 // If there's anything other than the right brace, this is a post indexing
623 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000624 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000625 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000626
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000627 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000628 bool OffsetRegShifted = false;
629 enum ShiftType ShiftType;
630 const MCExpr *ShiftAmount;
Chris Lattner14b93852010-10-29 00:27:31 +0000631 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000632
Sean Callanan18b83232010-01-19 21:44:56 +0000633 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000634 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000635 Postindexed = true;
636 Writeback = true;
Chris Lattner550276e2010-10-28 20:52:15 +0000637 if (NextTok.isNot(AsmToken::Comma)) {
638 Error(NextTok.getLoc(), "',' expected");
639 return 0;
640 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000641 Parser.Lex(); // Eat comma token.
Chris Lattner550276e2010-10-28 20:52:15 +0000642 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000643 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000644 E))
645 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000646 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000647
Chris Lattner550276e2010-10-28 20:52:15 +0000648 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
649 OffsetRegShifted, ShiftType, ShiftAmount,
650 Preindexed, Postindexed, Negative, Writeback,
651 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000652 }
653
Chris Lattner550276e2010-10-28 20:52:15 +0000654 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000655}
656
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000657/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
658/// we will parse the following (were +/- means that a plus or minus is
659/// optional):
660/// +/-Rm
661/// +/-Rm, shift
662/// #offset
663/// we return false on success or an error otherwise.
664bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000665 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000666 enum ShiftType &ShiftType,
667 const MCExpr *&ShiftAmount,
668 const MCExpr *&Offset,
669 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000670 int &OffsetRegNum,
671 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000672 Negative = false;
673 OffsetRegShifted = false;
674 OffsetIsReg = false;
675 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000676 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000677 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000678 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000679 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000680 else if (NextTok.is(AsmToken::Minus)) {
681 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000682 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000683 }
684 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000685 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000686 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000687 SMLoc CurLoc = OffsetRegTok.getLoc();
688 OffsetRegNum = TryParseRegister();
689 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000690 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000691 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000692 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000693 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000694
Bill Wendling12f40e92010-11-06 10:51:53 +0000695 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000696 if (OffsetRegNum != -1) {
697 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000698 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000699 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000700 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000701
Sean Callanan18b83232010-01-19 21:44:56 +0000702 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000703 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000704 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000705 OffsetRegShifted = true;
706 }
707 }
708 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
709 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000710 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000711 if (HashTok.isNot(AsmToken::Hash))
712 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000713
Sean Callananb9a25b72010-01-19 20:27:46 +0000714 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000715
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000716 if (getParser().ParseExpression(Offset))
717 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000718 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000719 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000720 return false;
721}
722
723/// ParseShift as one of these two:
724/// ( lsl | lsr | asr | ror ) , # shift_amount
725/// rrx
726/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000727bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000728 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000729 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000730 if (Tok.isNot(AsmToken::Identifier))
731 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000732 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000733 if (ShiftName == "lsl" || ShiftName == "LSL")
734 St = Lsl;
735 else if (ShiftName == "lsr" || ShiftName == "LSR")
736 St = Lsr;
737 else if (ShiftName == "asr" || ShiftName == "ASR")
738 St = Asr;
739 else if (ShiftName == "ror" || ShiftName == "ROR")
740 St = Ror;
741 else if (ShiftName == "rrx" || ShiftName == "RRX")
742 St = Rrx;
743 else
744 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000745 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000746
747 // Rrx stands alone.
748 if (St == Rrx)
749 return false;
750
751 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000752 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000753 if (HashTok.isNot(AsmToken::Hash))
754 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000755 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000756
757 if (getParser().ParseExpression(ShiftAmount))
758 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000759
760 return false;
761}
762
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000763/// Parse a arm instruction operand. For now this parses the operand regardless
764/// of the mnemonic.
Chris Lattner550276e2010-10-28 20:52:15 +0000765ARMOperand *ARMAsmParser::ParseOperand() {
Sean Callanan76264762010-04-02 22:27:05 +0000766 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000767 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000768 default:
769 Error(Parser.getTok().getLoc(), "unexpected token in operand");
770 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000771 case AsmToken::Identifier:
Chris Lattnere5658fa2010-10-30 04:09:10 +0000772 if (ARMOperand *Op = TryParseRegisterWithWriteBack())
Chris Lattner550276e2010-10-28 20:52:15 +0000773 return Op;
Jim Grosbach16c74252010-10-29 14:46:02 +0000774
Kevin Enderby515d5092009-10-15 20:48:48 +0000775 // This was not a register so parse other operands that start with an
776 // identifier (like labels) as expressions and create them as immediates.
777 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000778 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000779 if (getParser().ParseExpression(IdVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000780 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000781 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000782 return ARMOperand::CreateImm(IdVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000783 case AsmToken::LBrac:
Chris Lattner550276e2010-10-28 20:52:15 +0000784 return ParseMemory();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000785 case AsmToken::LCurly:
Chris Lattner550276e2010-10-28 20:52:15 +0000786 return ParseRegisterList();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000787 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000788 // #42 -> immediate.
789 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000790 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000791 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000792 const MCExpr *ImmVal;
793 if (getParser().ParseExpression(ImmVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000794 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000795 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000796 return ARMOperand::CreateImm(ImmVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000797 }
798}
799
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000800/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000801bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000802 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000803 // Create the leading tokens for the mnemonic, split by '.' characters.
804 size_t Start = 0, Next = Name.find('.');
805 StringRef Head = Name.slice(Start, Next);
806
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000807 // Determine the predicate, if any.
808 //
809 // FIXME: We need a way to check whether a prefix supports predication,
810 // otherwise we will end up with an ambiguity for instructions that happen to
811 // end with a predicate name.
Jim Grosbach3df518e2010-10-29 21:56:51 +0000812 // FIXME: Likewise, some arithmetic instructions have an 's' prefix which
813 // indicates to update the condition codes. Those instructions have an
814 // additional immediate operand which encodes the prefix as reg0 or CPSR.
815 // Just checking for a suffix of 's' definitely creates ambiguities; e.g,
816 // the SMMLS instruction.
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000817 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
818 .Case("eq", ARMCC::EQ)
819 .Case("ne", ARMCC::NE)
820 .Case("hs", ARMCC::HS)
821 .Case("lo", ARMCC::LO)
822 .Case("mi", ARMCC::MI)
823 .Case("pl", ARMCC::PL)
824 .Case("vs", ARMCC::VS)
825 .Case("vc", ARMCC::VC)
826 .Case("hi", ARMCC::HI)
827 .Case("ls", ARMCC::LS)
828 .Case("ge", ARMCC::GE)
829 .Case("lt", ARMCC::LT)
830 .Case("gt", ARMCC::GT)
831 .Case("le", ARMCC::LE)
832 .Case("al", ARMCC::AL)
833 .Default(~0U);
Jim Grosbach16c74252010-10-29 14:46:02 +0000834
Chris Lattnerdba34d82010-10-30 04:35:59 +0000835 if (CC == ~0U ||
836 (CC == ARMCC::LS && (Head == "vmls" || Head == "vnmls"))) {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000837 CC = ARMCC::AL;
Chris Lattnerdba34d82010-10-30 04:35:59 +0000838 } else {
839 Head = Head.slice(0, Head.size() - 2);
Bill Wendling52925b62010-10-29 23:50:21 +0000840 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000841
Chris Lattner3a697562010-10-28 17:20:03 +0000842 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Jim Grosbach469ebbe2010-11-01 18:11:14 +0000843 // FIXME: Should only add this operand for predicated instructions
Chris Lattner3a697562010-10-28 17:20:03 +0000844 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000845
846 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000847 while (Next != StringRef::npos) {
848 Start = Next;
849 Next = Name.find('.', Start + 1);
850 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000851
Chris Lattner3a697562010-10-28 17:20:03 +0000852 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000853 }
854
855 // Read the remaining operands.
856 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000857 // Read the first operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000858 if (ARMOperand *Op = ParseOperand())
859 Operands.push_back(Op);
860 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000861 Parser.EatToEndOfStatement();
862 return true;
863 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000864
865 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000866 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000867
868 // Parse and remember the operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000869 if (ARMOperand *Op = ParseOperand())
870 Operands.push_back(Op);
871 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000872 Parser.EatToEndOfStatement();
873 return true;
874 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000875 }
876 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000877
Chris Lattnercbf8a982010-09-11 16:18:25 +0000878 if (getLexer().isNot(AsmToken::EndOfStatement)) {
879 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000880 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000881 }
Bill Wendling146018f2010-11-06 21:42:12 +0000882
Chris Lattner34e53142010-09-08 05:10:46 +0000883 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000884 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000885}
886
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000887bool ARMAsmParser::
888MatchAndEmitInstruction(SMLoc IDLoc,
889 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
890 MCStreamer &Out) {
891 MCInst Inst;
892 unsigned ErrorInfo;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000893 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
894 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000895 Out.EmitInstruction(Inst);
896 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000897 case Match_MissingFeature:
898 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
899 return true;
900 case Match_InvalidOperand: {
901 SMLoc ErrorLoc = IDLoc;
902 if (ErrorInfo != ~0U) {
903 if (ErrorInfo >= Operands.size())
904 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +0000905
Chris Lattnere73d4f82010-10-28 21:41:58 +0000906 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
907 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
908 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000909
Chris Lattnere73d4f82010-10-28 21:41:58 +0000910 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000911 }
Chris Lattnere73d4f82010-10-28 21:41:58 +0000912 case Match_MnemonicFail:
913 return Error(IDLoc, "unrecognized instruction mnemonic");
914 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000915
Eric Christopherc223e2b2010-10-29 09:26:59 +0000916 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +0000917 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000918}
919
Kevin Enderby515d5092009-10-15 20:48:48 +0000920/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000921bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
922 StringRef IDVal = DirectiveID.getIdentifier();
923 if (IDVal == ".word")
924 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000925 else if (IDVal == ".thumb")
926 return ParseDirectiveThumb(DirectiveID.getLoc());
927 else if (IDVal == ".thumb_func")
928 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
929 else if (IDVal == ".code")
930 return ParseDirectiveCode(DirectiveID.getLoc());
931 else if (IDVal == ".syntax")
932 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000933 return true;
934}
935
936/// ParseDirectiveWord
937/// ::= .word [ expression (, expression)* ]
938bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
939 if (getLexer().isNot(AsmToken::EndOfStatement)) {
940 for (;;) {
941 const MCExpr *Value;
942 if (getParser().ParseExpression(Value))
943 return true;
944
Chris Lattneraaec2052010-01-19 19:46:13 +0000945 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000946
947 if (getLexer().is(AsmToken::EndOfStatement))
948 break;
Jim Grosbach16c74252010-10-29 14:46:02 +0000949
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000950 // FIXME: Improve diagnostic.
951 if (getLexer().isNot(AsmToken::Comma))
952 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000953 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000954 }
955 }
956
Sean Callananb9a25b72010-01-19 20:27:46 +0000957 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000958 return false;
959}
960
Kevin Enderby515d5092009-10-15 20:48:48 +0000961/// ParseDirectiveThumb
962/// ::= .thumb
963bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
964 if (getLexer().isNot(AsmToken::EndOfStatement))
965 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000966 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000967
968 // TODO: set thumb mode
969 // TODO: tell the MC streamer the mode
970 // getParser().getStreamer().Emit???();
971 return false;
972}
973
974/// ParseDirectiveThumbFunc
975/// ::= .thumbfunc symbol_name
976bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000977 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000978 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +0000979 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000980 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +0000981 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000982 if (getLexer().isNot(AsmToken::EndOfStatement))
983 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000984 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000985
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000986 // Mark symbol as a thumb symbol.
987 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
988 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +0000989 return false;
990}
991
992/// ParseDirectiveSyntax
993/// ::= .syntax unified | divided
994bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000995 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000996 if (Tok.isNot(AsmToken::Identifier))
997 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000998 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000999 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001000 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001001 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001002 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001003 else
1004 return Error(L, "unrecognized syntax mode in .syntax directive");
1005
1006 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001007 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001008 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001009
1010 // TODO tell the MC streamer the mode
1011 // getParser().getStreamer().Emit???();
1012 return false;
1013}
1014
1015/// ParseDirectiveCode
1016/// ::= .code 16 | 32
1017bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001018 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001019 if (Tok.isNot(AsmToken::Integer))
1020 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001021 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001022 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001023 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001024 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001025 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001026 else
1027 return Error(L, "invalid operand to .code directive");
1028
1029 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001030 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001031 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001032
Jim Grosbach2a301702010-11-05 22:40:53 +00001033 if (Val == 16)
1034 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
1035 else
1036 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1037
Kevin Enderby515d5092009-10-15 20:48:48 +00001038 return false;
1039}
1040
Sean Callanan90b70972010-04-07 20:29:34 +00001041extern "C" void LLVMInitializeARMAsmLexer();
1042
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001043/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001044extern "C" void LLVMInitializeARMAsmParser() {
1045 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1046 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001047 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001048}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001049
Chris Lattner0692ee62010-09-06 19:11:01 +00001050#define GET_REGISTER_MATCHER
1051#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001052#include "ARMGenAsmMatcher.inc"