blob: 71191f7cf413dbcb2da8a349f7994895dbc9644a [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
85
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000086public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000087 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +000088 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
89 // Initialize the set of available features.
90 setAvailableFeatures(ComputeAvailableFeatures(
91 &TM.getSubtarget<ARMSubtarget>()));
92 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000093
Benjamin Kramer38e59892010-07-14 22:38:02 +000094 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000095 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000096
97 virtual bool ParseDirective(AsmToken DirectiveID);
98};
Jim Grosbach16c74252010-10-29 14:46:02 +000099} // end anonymous namespace
100
Chris Lattner3a697562010-10-28 17:20:03 +0000101namespace {
102
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000103/// ARMOperand - Instances of this class represent a parsed ARM machine
104/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000105class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000106 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000107 CondCode,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000108 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000109 Memory,
110 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000111 RegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000112 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000113 } Kind;
114
Sean Callanan76264762010-04-02 22:27:05 +0000115 SMLoc StartLoc, EndLoc;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000116
117 union {
118 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000119 ARMCC::CondCodes Val;
120 } CC;
121
122 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000123 const char *Data;
124 unsigned Length;
125 } Tok;
126
127 struct {
128 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000129 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000130 } Reg;
131
Bill Wendling8d5acb72010-11-06 19:56:04 +0000132 struct {
133 unsigned RegStart;
134 unsigned Number;
135 } RegList;
136
Kevin Enderbycfe07242009-10-13 22:19:02 +0000137 struct {
138 const MCExpr *Val;
139 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000140
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000141 // This is for all forms of ARM address expressions
142 struct {
143 unsigned BaseRegNum;
Bill Wendling146018f2010-11-06 21:42:12 +0000144 unsigned OffsetRegNum; // used when OffsetIsReg is true
145 const MCExpr *Offset; // used when OffsetIsReg is false
146 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
147 enum ShiftType ShiftType; // used when OffsetRegShifted is true
148 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
149 unsigned Preindexed : 1;
150 unsigned Postindexed : 1;
151 unsigned OffsetIsReg : 1;
152 unsigned Negative : 1; // only used when OffsetIsReg is true
153 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000154 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000155 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000156
Bill Wendling146018f2010-11-06 21:42:12 +0000157 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
158public:
Sean Callanan76264762010-04-02 22:27:05 +0000159 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
160 Kind = o.Kind;
161 StartLoc = o.StartLoc;
162 EndLoc = o.EndLoc;
163 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000164 case CondCode:
165 CC = o.CC;
166 break;
Sean Callanan76264762010-04-02 22:27:05 +0000167 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000168 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000169 break;
170 case Register:
171 Reg = o.Reg;
172 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000173 case RegisterList:
174 RegList = o.RegList;
175 break;
Sean Callanan76264762010-04-02 22:27:05 +0000176 case Immediate:
177 Imm = o.Imm;
178 break;
179 case Memory:
180 Mem = o.Mem;
181 break;
182 }
183 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000184
Sean Callanan76264762010-04-02 22:27:05 +0000185 /// getStartLoc - Get the location of the first token of this operand.
186 SMLoc getStartLoc() const { return StartLoc; }
187 /// getEndLoc - Get the location of the last token of this operand.
188 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000189
Daniel Dunbar8462b302010-08-11 06:36:53 +0000190 ARMCC::CondCodes getCondCode() const {
191 assert(Kind == CondCode && "Invalid access!");
192 return CC.Val;
193 }
194
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000195 StringRef getToken() const {
196 assert(Kind == Token && "Invalid access!");
197 return StringRef(Tok.Data, Tok.Length);
198 }
199
200 unsigned getReg() const {
201 assert(Kind == Register && "Invalid access!");
202 return Reg.RegNum;
203 }
204
Bill Wendling8d5acb72010-11-06 19:56:04 +0000205 std::pair<unsigned, unsigned> getRegList() const {
206 assert(Kind == RegisterList && "Invalid access!");
207 return std::make_pair(RegList.RegStart, RegList.Number);
208 }
209
Kevin Enderbycfe07242009-10-13 22:19:02 +0000210 const MCExpr *getImm() const {
211 assert(Kind == Immediate && "Invalid access!");
212 return Imm.Val;
213 }
214
Daniel Dunbar8462b302010-08-11 06:36:53 +0000215 bool isCondCode() const { return Kind == CondCode; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000216 bool isImm() const { return Kind == Immediate; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000217 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000218 bool isRegList() const { return Kind == RegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000219 bool isToken() const { return Kind == Token; }
220 bool isMemory() const { return Kind == Memory; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000221
222 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000223 // Add as immediates when possible. Null MCExpr = 0.
224 if (Expr == 0)
225 Inst.addOperand(MCOperand::CreateImm(0));
226 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000227 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
228 else
229 Inst.addOperand(MCOperand::CreateExpr(Expr));
230 }
231
Daniel Dunbar8462b302010-08-11 06:36:53 +0000232 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000233 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000234 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000235 // FIXME: What belongs here?
236 Inst.addOperand(MCOperand::CreateReg(0));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000237 }
238
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000239 void addRegOperands(MCInst &Inst, unsigned N) const {
240 assert(N == 1 && "Invalid number of operands!");
241 Inst.addOperand(MCOperand::CreateReg(getReg()));
242 }
243
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000244 void addImmOperands(MCInst &Inst, unsigned N) const {
245 assert(N == 1 && "Invalid number of operands!");
246 addExpr(Inst, getImm());
247 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000248
Chris Lattner14b93852010-10-29 00:27:31 +0000249 bool isMemMode5() const {
Chris Lattner14b93852010-10-29 00:27:31 +0000250 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
Jim Grosbach80eb2332010-10-29 17:41:25 +0000251 Mem.Writeback || Mem.Negative)
Chris Lattner14b93852010-10-29 00:27:31 +0000252 return false;
Jim Grosbach80eb2332010-10-29 17:41:25 +0000253 // If there is an offset expression, make sure it's valid.
254 if (!Mem.Offset)
255 return true;
256 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
257 if (!CE)
258 return false;
259 // The offset must be a multiple of 4 in the range 0-1020.
260 int64_t Value = CE->getValue();
261 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
Chris Lattner14b93852010-10-29 00:27:31 +0000262 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000263
Chris Lattner14b93852010-10-29 00:27:31 +0000264 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
265 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000266
Chris Lattner14b93852010-10-29 00:27:31 +0000267 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlinga60f1572010-11-06 10:48:18 +0000268 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000269
Jim Grosbach80eb2332010-10-29 17:41:25 +0000270 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
271 // the difference?
272 if (Mem.Offset) {
273 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000274 assert(CE && "Non-constant mode 5 offset operand!");
275
Jim Grosbach80eb2332010-10-29 17:41:25 +0000276 // The MCInst offset operand doesn't include the low two bits (like
277 // the instruction encoding).
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000278 int64_t Offset = CE->getValue() / 4;
279 if (Offset >= 0)
280 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
281 Offset)));
282 else
283 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
284 -Offset)));
285 } else {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000286 Inst.addOperand(MCOperand::CreateImm(0));
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000287 }
Chris Lattner14b93852010-10-29 00:27:31 +0000288 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000289
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000290 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000291
Chris Lattner3a697562010-10-28 17:20:03 +0000292 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
293 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000294 Op->CC.Val = CC;
295 Op->StartLoc = S;
296 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000297 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000298 }
299
Chris Lattner3a697562010-10-28 17:20:03 +0000300 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
301 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000302 Op->Tok.Data = Str.data();
303 Op->Tok.Length = Str.size();
304 Op->StartLoc = S;
305 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000306 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000307 }
308
Chris Lattner3a697562010-10-28 17:20:03 +0000309 static ARMOperand *CreateReg(unsigned RegNum, bool Writeback, SMLoc S,
310 SMLoc E) {
311 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000312 Op->Reg.RegNum = RegNum;
313 Op->Reg.Writeback = Writeback;
Sean Callanan76264762010-04-02 22:27:05 +0000314 Op->StartLoc = S;
315 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000316 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000317 }
318
Bill Wendling8d5acb72010-11-06 19:56:04 +0000319 static ARMOperand *CreateRegList(unsigned RegStart, unsigned Number,
320 SMLoc S, SMLoc E) {
321 ARMOperand *Op = new ARMOperand(RegisterList);
322 Op->RegList.RegStart = RegStart;
323 Op->RegList.Number = Number;
324 Op->StartLoc = S;
325 Op->EndLoc = E;
326 return Op;
327 }
328
Chris Lattner3a697562010-10-28 17:20:03 +0000329 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
330 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000331 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000332 Op->StartLoc = S;
333 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000334 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000335 }
336
Chris Lattner3a697562010-10-28 17:20:03 +0000337 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
338 const MCExpr *Offset, unsigned OffsetRegNum,
339 bool OffsetRegShifted, enum ShiftType ShiftType,
340 const MCExpr *ShiftAmount, bool Preindexed,
341 bool Postindexed, bool Negative, bool Writeback,
342 SMLoc S, SMLoc E) {
343 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000344 Op->Mem.BaseRegNum = BaseRegNum;
345 Op->Mem.OffsetIsReg = OffsetIsReg;
346 Op->Mem.Offset = Offset;
347 Op->Mem.OffsetRegNum = OffsetRegNum;
348 Op->Mem.OffsetRegShifted = OffsetRegShifted;
349 Op->Mem.ShiftType = ShiftType;
350 Op->Mem.ShiftAmount = ShiftAmount;
351 Op->Mem.Preindexed = Preindexed;
352 Op->Mem.Postindexed = Postindexed;
353 Op->Mem.Negative = Negative;
354 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000355
Sean Callanan76264762010-04-02 22:27:05 +0000356 Op->StartLoc = S;
357 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000358 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000359 }
360};
361
362} // end anonymous namespace.
363
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000364void ARMOperand::dump(raw_ostream &OS) const {
365 switch (Kind) {
366 case CondCode:
367 OS << ARMCondCodeToString(getCondCode());
368 break;
369 case Immediate:
370 getImm()->print(OS);
371 break;
372 case Memory:
373 OS << "<memory>";
374 break;
375 case Register:
376 OS << "<register " << getReg() << ">";
377 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000378 case RegisterList: {
379 OS << "<register_list ";
380 std::pair<unsigned, unsigned> List = getRegList();
381 unsigned RegEnd = List.first + List.second;
382
383 for (unsigned Idx = List.first; Idx < RegEnd; ) {
384 OS << Idx;
385 if (++Idx < RegEnd) OS << ", ";
386 }
387
388 OS << ">";
389 break;
390 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000391 case Token:
392 OS << "'" << getToken() << "'";
393 break;
394 }
395}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000396
397/// @name Auto-generated Match Functions
398/// {
399
400static unsigned MatchRegisterName(StringRef Name);
401
402/// }
403
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000404/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000405/// and if it is a register name the token is eaten and the register number is
406/// returned. Otherwise return -1.
407///
408int ARMAsmParser::TryParseRegister() {
409 const AsmToken &Tok = Parser.getTok();
410 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000411
Chris Lattnere5658fa2010-10-30 04:09:10 +0000412 // FIXME: Validate register for the current architecture; we have to do
413 // validation later, so maybe there is no need for this here.
Bill Wendlingd68fd9c2010-11-06 10:45:34 +0000414 unsigned RegNum = MatchRegisterName(Tok.getString());
415 if (RegNum == 0)
Chris Lattnere5658fa2010-10-30 04:09:10 +0000416 return -1;
417 Parser.Lex(); // Eat identifier token.
418 return RegNum;
419}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000420
421
Chris Lattnere5658fa2010-10-30 04:09:10 +0000422/// Try to parse a register name. The token must be an Identifier when called,
423/// and if it is a register name the token is eaten and the register number is
424/// returned. Otherwise return -1.
Chris Lattner3a697562010-10-28 17:20:03 +0000425///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000426/// TODO this is likely to change to allow different register types and or to
427/// parse for a specific register type.
Chris Lattnere5658fa2010-10-30 04:09:10 +0000428ARMOperand *ARMAsmParser::TryParseRegisterWithWriteBack() {
429 SMLoc S = Parser.getTok().getLoc();
430 int RegNo = TryParseRegister();
431 if (RegNo == -1) return 0;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000432
Chris Lattnere5658fa2010-10-30 04:09:10 +0000433 SMLoc E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000434
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000435 bool Writeback = false;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000436 const AsmToken &ExclaimTok = Parser.getTok();
437 if (ExclaimTok.is(AsmToken::Exclaim)) {
438 E = ExclaimTok.getLoc();
439 Writeback = true;
440 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000441 }
442
Chris Lattnere5658fa2010-10-30 04:09:10 +0000443 return ARMOperand::CreateReg(RegNo, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000444}
445
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000446/// Parse a register list, return it if successful else return null. The first
447/// token must be a '{' when called.
448ARMOperand *ARMAsmParser::ParseRegisterList() {
Sean Callanan76264762010-04-02 22:27:05 +0000449 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000450 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000451 "Token is not a Left Curly Brace");
Sean Callanan76264762010-04-02 22:27:05 +0000452 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000453 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000454
Sean Callanan18b83232010-01-19 21:44:56 +0000455 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000456 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000457 if (RegTok.isNot(AsmToken::Identifier)) {
458 Error(RegLoc, "register expected");
459 return 0;
460 }
Bill Wendling1d6a2652010-11-06 10:40:24 +0000461 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000462 if (RegNum == -1) {
463 Error(RegLoc, "register expected");
464 return 0;
465 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000466
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000467 unsigned RegList = 1 << RegNum;
468
469 int HighRegNum = RegNum;
470 // TODO ranges like "{Rn-Rm}"
Sean Callanan18b83232010-01-19 21:44:56 +0000471 while (Parser.getTok().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000472 Parser.Lex(); // Eat comma token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000473
Sean Callanan18b83232010-01-19 21:44:56 +0000474 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000475 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000476 if (RegTok.isNot(AsmToken::Identifier)) {
477 Error(RegLoc, "register expected");
478 return 0;
479 }
Bill Wendling1d6a2652010-11-06 10:40:24 +0000480 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000481 if (RegNum == -1) {
482 Error(RegLoc, "register expected");
483 return 0;
484 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000485
486 if (RegList & (1 << RegNum))
487 Warning(RegLoc, "register duplicated in register list");
488 else if (RegNum <= HighRegNum)
489 Warning(RegLoc, "register not in ascending order in register list");
490 RegList |= 1 << RegNum;
491 HighRegNum = RegNum;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000492 }
Sean Callanan18b83232010-01-19 21:44:56 +0000493 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000494 if (RCurlyTok.isNot(AsmToken::RCurly)) {
495 Error(RCurlyTok.getLoc(), "'}' expected");
496 return 0;
497 }
Sean Callanan76264762010-04-02 22:27:05 +0000498 E = RCurlyTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000499 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000500
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000501 // FIXME: Need to return an operand!
502 Error(E, "FIXME: register list parsing not implemented");
503 return 0;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000504}
505
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000506/// Parse an arm memory expression, return false if successful else return true
507/// or an error. The first token must be a '[' when called.
508/// TODO Only preindexing and postindexing addressing are started, unindexed
509/// with option, etc are still to do.
Chris Lattner550276e2010-10-28 20:52:15 +0000510ARMOperand *ARMAsmParser::ParseMemory() {
Sean Callanan76264762010-04-02 22:27:05 +0000511 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000512 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000513 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000514 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000515 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000516
Sean Callanan18b83232010-01-19 21:44:56 +0000517 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000518 if (BaseRegTok.isNot(AsmToken::Identifier)) {
519 Error(BaseRegTok.getLoc(), "register expected");
520 return 0;
521 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000522 int BaseRegNum = TryParseRegister();
523 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000524 Error(BaseRegTok.getLoc(), "register expected");
525 return 0;
526 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000527
528 bool Preindexed = false;
529 bool Postindexed = false;
530 bool OffsetIsReg = false;
531 bool Negative = false;
532 bool Writeback = false;
533
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000534 // First look for preindexed address forms, that is after the "[Rn" we now
535 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000536 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000537 if (Tok.is(AsmToken::Comma)) {
538 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000539 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000540 int OffsetRegNum;
541 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000542 enum ShiftType ShiftType;
543 const MCExpr *ShiftAmount;
544 const MCExpr *Offset;
Chris Lattner550276e2010-10-28 20:52:15 +0000545 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
546 Offset, OffsetIsReg, OffsetRegNum, E))
547 return 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000548 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000549 if (RBracTok.isNot(AsmToken::RBrac)) {
550 Error(RBracTok.getLoc(), "']' expected");
551 return 0;
552 }
Sean Callanan76264762010-04-02 22:27:05 +0000553 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000554 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000555
Sean Callanan18b83232010-01-19 21:44:56 +0000556 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000557 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000558 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000559 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000560 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000561 }
Chris Lattner550276e2010-10-28 20:52:15 +0000562 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
563 OffsetRegShifted, ShiftType, ShiftAmount,
564 Preindexed, Postindexed, Negative, Writeback,
565 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000566 }
567 // The "[Rn" we have so far was not followed by a comma.
568 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000569 // If there's anything other than the right brace, this is a post indexing
570 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000571 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000572 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000573
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000574 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000575 bool OffsetRegShifted = false;
576 enum ShiftType ShiftType;
577 const MCExpr *ShiftAmount;
Chris Lattner14b93852010-10-29 00:27:31 +0000578 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000579
Sean Callanan18b83232010-01-19 21:44:56 +0000580 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000581 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000582 Postindexed = true;
583 Writeback = true;
Chris Lattner550276e2010-10-28 20:52:15 +0000584 if (NextTok.isNot(AsmToken::Comma)) {
585 Error(NextTok.getLoc(), "',' expected");
586 return 0;
587 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000588 Parser.Lex(); // Eat comma token.
Chris Lattner550276e2010-10-28 20:52:15 +0000589 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000590 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000591 E))
592 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000593 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000594
Chris Lattner550276e2010-10-28 20:52:15 +0000595 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
596 OffsetRegShifted, ShiftType, ShiftAmount,
597 Preindexed, Postindexed, Negative, Writeback,
598 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000599 }
600
Chris Lattner550276e2010-10-28 20:52:15 +0000601 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000602}
603
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000604/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
605/// we will parse the following (were +/- means that a plus or minus is
606/// optional):
607/// +/-Rm
608/// +/-Rm, shift
609/// #offset
610/// we return false on success or an error otherwise.
611bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000612 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000613 enum ShiftType &ShiftType,
614 const MCExpr *&ShiftAmount,
615 const MCExpr *&Offset,
616 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000617 int &OffsetRegNum,
618 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000619 Negative = false;
620 OffsetRegShifted = false;
621 OffsetIsReg = false;
622 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000623 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000624 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000625 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000626 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000627 else if (NextTok.is(AsmToken::Minus)) {
628 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000629 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000630 }
631 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000632 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000633 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000634 SMLoc CurLoc = OffsetRegTok.getLoc();
635 OffsetRegNum = TryParseRegister();
636 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000637 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000638 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000639 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000640 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000641
Bill Wendling12f40e92010-11-06 10:51:53 +0000642 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000643 if (OffsetRegNum != -1) {
644 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000645 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000646 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000647 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000648
Sean Callanan18b83232010-01-19 21:44:56 +0000649 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000650 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000651 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000652 OffsetRegShifted = true;
653 }
654 }
655 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
656 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000657 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000658 if (HashTok.isNot(AsmToken::Hash))
659 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000660
Sean Callananb9a25b72010-01-19 20:27:46 +0000661 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000662
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000663 if (getParser().ParseExpression(Offset))
664 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000665 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000666 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000667 return false;
668}
669
670/// ParseShift as one of these two:
671/// ( lsl | lsr | asr | ror ) , # shift_amount
672/// rrx
673/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000674bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000675 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000676 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000677 if (Tok.isNot(AsmToken::Identifier))
678 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000679 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000680 if (ShiftName == "lsl" || ShiftName == "LSL")
681 St = Lsl;
682 else if (ShiftName == "lsr" || ShiftName == "LSR")
683 St = Lsr;
684 else if (ShiftName == "asr" || ShiftName == "ASR")
685 St = Asr;
686 else if (ShiftName == "ror" || ShiftName == "ROR")
687 St = Ror;
688 else if (ShiftName == "rrx" || ShiftName == "RRX")
689 St = Rrx;
690 else
691 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000692 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000693
694 // Rrx stands alone.
695 if (St == Rrx)
696 return false;
697
698 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000699 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000700 if (HashTok.isNot(AsmToken::Hash))
701 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000702 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000703
704 if (getParser().ParseExpression(ShiftAmount))
705 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000706
707 return false;
708}
709
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000710/// Parse a arm instruction operand. For now this parses the operand regardless
711/// of the mnemonic.
Chris Lattner550276e2010-10-28 20:52:15 +0000712ARMOperand *ARMAsmParser::ParseOperand() {
Sean Callanan76264762010-04-02 22:27:05 +0000713 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000714 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000715 default:
716 Error(Parser.getTok().getLoc(), "unexpected token in operand");
717 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000718 case AsmToken::Identifier:
Chris Lattnere5658fa2010-10-30 04:09:10 +0000719 if (ARMOperand *Op = TryParseRegisterWithWriteBack())
Chris Lattner550276e2010-10-28 20:52:15 +0000720 return Op;
Jim Grosbach16c74252010-10-29 14:46:02 +0000721
Kevin Enderby515d5092009-10-15 20:48:48 +0000722 // This was not a register so parse other operands that start with an
723 // identifier (like labels) as expressions and create them as immediates.
724 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000725 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000726 if (getParser().ParseExpression(IdVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000727 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000728 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000729 return ARMOperand::CreateImm(IdVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000730 case AsmToken::LBrac:
Chris Lattner550276e2010-10-28 20:52:15 +0000731 return ParseMemory();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000732 case AsmToken::LCurly:
Chris Lattner550276e2010-10-28 20:52:15 +0000733 return ParseRegisterList();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000734 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000735 // #42 -> immediate.
736 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000737 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000738 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000739 const MCExpr *ImmVal;
740 if (getParser().ParseExpression(ImmVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000741 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000742 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000743 return ARMOperand::CreateImm(ImmVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000744 }
745}
746
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000747/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000748bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000749 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000750 // Create the leading tokens for the mnemonic, split by '.' characters.
751 size_t Start = 0, Next = Name.find('.');
752 StringRef Head = Name.slice(Start, Next);
753
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000754 // Determine the predicate, if any.
755 //
756 // FIXME: We need a way to check whether a prefix supports predication,
757 // otherwise we will end up with an ambiguity for instructions that happen to
758 // end with a predicate name.
Jim Grosbach3df518e2010-10-29 21:56:51 +0000759 // FIXME: Likewise, some arithmetic instructions have an 's' prefix which
760 // indicates to update the condition codes. Those instructions have an
761 // additional immediate operand which encodes the prefix as reg0 or CPSR.
762 // Just checking for a suffix of 's' definitely creates ambiguities; e.g,
763 // the SMMLS instruction.
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000764 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
765 .Case("eq", ARMCC::EQ)
766 .Case("ne", ARMCC::NE)
767 .Case("hs", ARMCC::HS)
768 .Case("lo", ARMCC::LO)
769 .Case("mi", ARMCC::MI)
770 .Case("pl", ARMCC::PL)
771 .Case("vs", ARMCC::VS)
772 .Case("vc", ARMCC::VC)
773 .Case("hi", ARMCC::HI)
774 .Case("ls", ARMCC::LS)
775 .Case("ge", ARMCC::GE)
776 .Case("lt", ARMCC::LT)
777 .Case("gt", ARMCC::GT)
778 .Case("le", ARMCC::LE)
779 .Case("al", ARMCC::AL)
780 .Default(~0U);
Jim Grosbach16c74252010-10-29 14:46:02 +0000781
Chris Lattnerdba34d82010-10-30 04:35:59 +0000782 if (CC == ~0U ||
783 (CC == ARMCC::LS && (Head == "vmls" || Head == "vnmls"))) {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000784 CC = ARMCC::AL;
Chris Lattnerdba34d82010-10-30 04:35:59 +0000785 } else {
786 Head = Head.slice(0, Head.size() - 2);
Bill Wendling52925b62010-10-29 23:50:21 +0000787 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000788
Chris Lattner3a697562010-10-28 17:20:03 +0000789 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Jim Grosbach469ebbe2010-11-01 18:11:14 +0000790 // FIXME: Should only add this operand for predicated instructions
Chris Lattner3a697562010-10-28 17:20:03 +0000791 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000792
793 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000794 while (Next != StringRef::npos) {
795 Start = Next;
796 Next = Name.find('.', Start + 1);
797 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000798
Chris Lattner3a697562010-10-28 17:20:03 +0000799 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000800 }
801
802 // Read the remaining operands.
803 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000804 // Read the first operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000805 if (ARMOperand *Op = ParseOperand())
806 Operands.push_back(Op);
807 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000808 Parser.EatToEndOfStatement();
809 return true;
810 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000811
812 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000813 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000814
815 // Parse and remember the operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000816 if (ARMOperand *Op = ParseOperand())
817 Operands.push_back(Op);
818 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000819 Parser.EatToEndOfStatement();
820 return true;
821 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000822 }
823 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000824
Chris Lattnercbf8a982010-09-11 16:18:25 +0000825 if (getLexer().isNot(AsmToken::EndOfStatement)) {
826 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000827 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000828 }
Bill Wendling146018f2010-11-06 21:42:12 +0000829
Chris Lattner34e53142010-09-08 05:10:46 +0000830 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000831 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000832}
833
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000834bool ARMAsmParser::
835MatchAndEmitInstruction(SMLoc IDLoc,
836 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
837 MCStreamer &Out) {
838 MCInst Inst;
839 unsigned ErrorInfo;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000840 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
841 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000842 Out.EmitInstruction(Inst);
843 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000844 case Match_MissingFeature:
845 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
846 return true;
847 case Match_InvalidOperand: {
848 SMLoc ErrorLoc = IDLoc;
849 if (ErrorInfo != ~0U) {
850 if (ErrorInfo >= Operands.size())
851 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +0000852
Chris Lattnere73d4f82010-10-28 21:41:58 +0000853 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
854 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
855 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000856
Chris Lattnere73d4f82010-10-28 21:41:58 +0000857 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000858 }
Chris Lattnere73d4f82010-10-28 21:41:58 +0000859 case Match_MnemonicFail:
860 return Error(IDLoc, "unrecognized instruction mnemonic");
861 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000862
Eric Christopherc223e2b2010-10-29 09:26:59 +0000863 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +0000864 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000865}
866
Kevin Enderby515d5092009-10-15 20:48:48 +0000867/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000868bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
869 StringRef IDVal = DirectiveID.getIdentifier();
870 if (IDVal == ".word")
871 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000872 else if (IDVal == ".thumb")
873 return ParseDirectiveThumb(DirectiveID.getLoc());
874 else if (IDVal == ".thumb_func")
875 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
876 else if (IDVal == ".code")
877 return ParseDirectiveCode(DirectiveID.getLoc());
878 else if (IDVal == ".syntax")
879 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000880 return true;
881}
882
883/// ParseDirectiveWord
884/// ::= .word [ expression (, expression)* ]
885bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
886 if (getLexer().isNot(AsmToken::EndOfStatement)) {
887 for (;;) {
888 const MCExpr *Value;
889 if (getParser().ParseExpression(Value))
890 return true;
891
Chris Lattneraaec2052010-01-19 19:46:13 +0000892 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000893
894 if (getLexer().is(AsmToken::EndOfStatement))
895 break;
Jim Grosbach16c74252010-10-29 14:46:02 +0000896
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000897 // FIXME: Improve diagnostic.
898 if (getLexer().isNot(AsmToken::Comma))
899 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000900 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000901 }
902 }
903
Sean Callananb9a25b72010-01-19 20:27:46 +0000904 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000905 return false;
906}
907
Kevin Enderby515d5092009-10-15 20:48:48 +0000908/// ParseDirectiveThumb
909/// ::= .thumb
910bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
911 if (getLexer().isNot(AsmToken::EndOfStatement))
912 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000913 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000914
915 // TODO: set thumb mode
916 // TODO: tell the MC streamer the mode
917 // getParser().getStreamer().Emit???();
918 return false;
919}
920
921/// ParseDirectiveThumbFunc
922/// ::= .thumbfunc symbol_name
923bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000924 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000925 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +0000926 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000927 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +0000928 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000929 if (getLexer().isNot(AsmToken::EndOfStatement))
930 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000931 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000932
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000933 // Mark symbol as a thumb symbol.
934 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
935 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +0000936 return false;
937}
938
939/// ParseDirectiveSyntax
940/// ::= .syntax unified | divided
941bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000942 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000943 if (Tok.isNot(AsmToken::Identifier))
944 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000945 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000946 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000947 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000948 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000949 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000950 else
951 return Error(L, "unrecognized syntax mode in .syntax directive");
952
953 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000954 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000955 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000956
957 // TODO tell the MC streamer the mode
958 // getParser().getStreamer().Emit???();
959 return false;
960}
961
962/// ParseDirectiveCode
963/// ::= .code 16 | 32
964bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000965 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000966 if (Tok.isNot(AsmToken::Integer))
967 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000968 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +0000969 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +0000970 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000971 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +0000972 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000973 else
974 return Error(L, "invalid operand to .code directive");
975
976 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000977 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000978 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000979
Jim Grosbach2a301702010-11-05 22:40:53 +0000980 if (Val == 16)
981 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
982 else
983 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
984
Kevin Enderby515d5092009-10-15 20:48:48 +0000985 return false;
986}
987
Sean Callanan90b70972010-04-07 20:29:34 +0000988extern "C" void LLVMInitializeARMAsmLexer();
989
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000990/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000991extern "C" void LLVMInitializeARMAsmParser() {
992 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
993 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +0000994 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000995}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000996
Chris Lattner0692ee62010-09-06 19:11:01 +0000997#define GET_REGISTER_MATCHER
998#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000999#include "ARMGenAsmMatcher.inc"