blob: 86a1bb679a63c5985c58cb2df4c996e15887e9f1 [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 {
39 struct ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000040
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000041class ARMAsmParser : public TargetAsmParser {
42 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000043 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000044
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000045 MCAsmParser &getParser() const { return Parser; }
46
47 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
48
49 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
50
51 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
52
Chris Lattnere5658fa2010-10-30 04:09:10 +000053 int TryParseRegister();
54 ARMOperand *TryParseRegisterWithWriteBack();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +000055 ARMOperand *ParseRegisterList();
Chris Lattner550276e2010-10-28 20:52:15 +000056 ARMOperand *ParseMemory();
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);
Kevin Enderby9c41fa82009-10-30 22:55:57 +000066
Sean Callanan76264762010-04-02 22:27:05 +000067 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000068
Chris Lattner550276e2010-10-28 20:52:15 +000069 ARMOperand *ParseOperand();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000070
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000071 bool ParseDirectiveWord(unsigned Size, SMLoc L);
72
Kevin Enderby515d5092009-10-15 20:48:48 +000073 bool ParseDirectiveThumb(SMLoc L);
74
75 bool ParseDirectiveThumbFunc(SMLoc L);
76
77 bool ParseDirectiveCode(SMLoc L);
78
79 bool ParseDirectiveSyntax(SMLoc L);
80
Chris Lattner7036f8b2010-09-29 01:42:58 +000081 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000082 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000083 MCStreamer &Out);
Jim Grosbach16c74252010-10-29 14:46:02 +000084
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000085 /// @name Auto-generated Match Functions
86 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000087
Chris Lattner0692ee62010-09-06 19:11:01 +000088#define GET_ASSEMBLER_HEADER
89#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000090
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000091 /// }
92
93
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000094public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000095 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +000096 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
97 // Initialize the set of available features.
98 setAvailableFeatures(ComputeAvailableFeatures(
99 &TM.getSubtarget<ARMSubtarget>()));
100 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000101
Benjamin Kramer38e59892010-07-14 22:38:02 +0000102 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000103 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000104
105 virtual bool ParseDirective(AsmToken DirectiveID);
106};
Jim Grosbach16c74252010-10-29 14:46:02 +0000107} // end anonymous namespace
108
Chris Lattner3a697562010-10-28 17:20:03 +0000109namespace {
110
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000111/// ARMOperand - Instances of this class represent a parsed ARM machine
112/// instruction.
Chris Lattner76593892010-01-14 21:21:40 +0000113struct ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000114public:
115 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000116 CondCode,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000117 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000118 Memory,
119 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000120 RegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000121 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000122 } Kind;
123
Sean Callanan76264762010-04-02 22:27:05 +0000124 SMLoc StartLoc, EndLoc;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000125
126 union {
127 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000128 ARMCC::CondCodes Val;
129 } CC;
130
131 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000132 const char *Data;
133 unsigned Length;
134 } Tok;
135
136 struct {
137 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000138 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000139 } Reg;
140
Bill Wendling8d5acb72010-11-06 19:56:04 +0000141 struct {
142 unsigned RegStart;
143 unsigned Number;
144 } RegList;
145
Kevin Enderbycfe07242009-10-13 22:19:02 +0000146 struct {
147 const MCExpr *Val;
148 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000149
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000150 // This is for all forms of ARM address expressions
151 struct {
152 unsigned BaseRegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000153 unsigned OffsetRegNum; // used when OffsetIsReg is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000154 const MCExpr *Offset; // used when OffsetIsReg is false
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000155 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000156 enum ShiftType ShiftType; // used when OffsetRegShifted is true
157 unsigned
158 OffsetRegShifted : 1, // only used when OffsetIsReg is true
159 Preindexed : 1,
160 Postindexed : 1,
161 OffsetIsReg : 1,
162 Negative : 1, // only used when OffsetIsReg is true
163 Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000164 } Mem;
165
166 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000167
Sean Callanan76264762010-04-02 22:27:05 +0000168 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
169 Kind = o.Kind;
170 StartLoc = o.StartLoc;
171 EndLoc = o.EndLoc;
172 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000173 case CondCode:
174 CC = o.CC;
175 break;
Sean Callanan76264762010-04-02 22:27:05 +0000176 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000177 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000178 break;
179 case Register:
180 Reg = o.Reg;
181 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000182 case RegisterList:
183 RegList = o.RegList;
184 break;
Sean Callanan76264762010-04-02 22:27:05 +0000185 case Immediate:
186 Imm = o.Imm;
187 break;
188 case Memory:
189 Mem = o.Mem;
190 break;
191 }
192 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000193
Sean Callanan76264762010-04-02 22:27:05 +0000194 /// getStartLoc - Get the location of the first token of this operand.
195 SMLoc getStartLoc() const { return StartLoc; }
196 /// getEndLoc - Get the location of the last token of this operand.
197 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000198
Daniel Dunbar8462b302010-08-11 06:36:53 +0000199 ARMCC::CondCodes getCondCode() const {
200 assert(Kind == CondCode && "Invalid access!");
201 return CC.Val;
202 }
203
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000204 StringRef getToken() const {
205 assert(Kind == Token && "Invalid access!");
206 return StringRef(Tok.Data, Tok.Length);
207 }
208
209 unsigned getReg() const {
210 assert(Kind == Register && "Invalid access!");
211 return Reg.RegNum;
212 }
213
Bill Wendling8d5acb72010-11-06 19:56:04 +0000214 std::pair<unsigned, unsigned> getRegList() const {
215 assert(Kind == RegisterList && "Invalid access!");
216 return std::make_pair(RegList.RegStart, RegList.Number);
217 }
218
Kevin Enderbycfe07242009-10-13 22:19:02 +0000219 const MCExpr *getImm() const {
220 assert(Kind == Immediate && "Invalid access!");
221 return Imm.Val;
222 }
223
Daniel Dunbar8462b302010-08-11 06:36:53 +0000224 bool isCondCode() const { return Kind == CondCode; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000225 bool isImm() const { return Kind == Immediate; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000226 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000227 bool isRegList() const { return Kind == RegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000228 bool isToken() const { return Kind == Token; }
229 bool isMemory() const { return Kind == Memory; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000230
231 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000232 // Add as immediates when possible. Null MCExpr = 0.
233 if (Expr == 0)
234 Inst.addOperand(MCOperand::CreateImm(0));
235 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000236 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
237 else
238 Inst.addOperand(MCOperand::CreateExpr(Expr));
239 }
240
Daniel Dunbar8462b302010-08-11 06:36:53 +0000241 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000242 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000243 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000244 // FIXME: What belongs here?
245 Inst.addOperand(MCOperand::CreateReg(0));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000246 }
247
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000248 void addRegOperands(MCInst &Inst, unsigned N) const {
249 assert(N == 1 && "Invalid number of operands!");
250 Inst.addOperand(MCOperand::CreateReg(getReg()));
251 }
252
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000253 void addImmOperands(MCInst &Inst, unsigned N) const {
254 assert(N == 1 && "Invalid number of operands!");
255 addExpr(Inst, getImm());
256 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000257
258
Chris Lattner14b93852010-10-29 00:27:31 +0000259 bool isMemMode5() const {
Chris Lattner14b93852010-10-29 00:27:31 +0000260 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
Jim Grosbach80eb2332010-10-29 17:41:25 +0000261 Mem.Writeback || Mem.Negative)
Chris Lattner14b93852010-10-29 00:27:31 +0000262 return false;
Jim Grosbach80eb2332010-10-29 17:41:25 +0000263 // If there is an offset expression, make sure it's valid.
264 if (!Mem.Offset)
265 return true;
266 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
267 if (!CE)
268 return false;
269 // The offset must be a multiple of 4 in the range 0-1020.
270 int64_t Value = CE->getValue();
271 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
Chris Lattner14b93852010-10-29 00:27:31 +0000272 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000273
Chris Lattner14b93852010-10-29 00:27:31 +0000274 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
275 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000276
Chris Lattner14b93852010-10-29 00:27:31 +0000277 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlinga60f1572010-11-06 10:48:18 +0000278 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000279
Jim Grosbach80eb2332010-10-29 17:41:25 +0000280 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
281 // the difference?
282 if (Mem.Offset) {
283 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000284 assert(CE && "Non-constant mode 5 offset operand!");
285
Jim Grosbach80eb2332010-10-29 17:41:25 +0000286 // The MCInst offset operand doesn't include the low two bits (like
287 // the instruction encoding).
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000288 int64_t Offset = CE->getValue() / 4;
289 if (Offset >= 0)
290 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
291 Offset)));
292 else
293 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
294 -Offset)));
295 } else {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000296 Inst.addOperand(MCOperand::CreateImm(0));
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000297 }
Chris Lattner14b93852010-10-29 00:27:31 +0000298 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000299
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000300 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000301
Chris Lattner3a697562010-10-28 17:20:03 +0000302 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
303 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000304 Op->CC.Val = CC;
305 Op->StartLoc = S;
306 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000307 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000308 }
309
Chris Lattner3a697562010-10-28 17:20:03 +0000310 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
311 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000312 Op->Tok.Data = Str.data();
313 Op->Tok.Length = Str.size();
314 Op->StartLoc = S;
315 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000316 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000317 }
318
Chris Lattner3a697562010-10-28 17:20:03 +0000319 static ARMOperand *CreateReg(unsigned RegNum, bool Writeback, SMLoc S,
320 SMLoc E) {
321 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000322 Op->Reg.RegNum = RegNum;
323 Op->Reg.Writeback = Writeback;
Sean Callanan76264762010-04-02 22:27:05 +0000324 Op->StartLoc = S;
325 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000326 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000327 }
328
Bill Wendling8d5acb72010-11-06 19:56:04 +0000329 static ARMOperand *CreateRegList(unsigned RegStart, unsigned Number,
330 SMLoc S, SMLoc E) {
331 ARMOperand *Op = new ARMOperand(RegisterList);
332 Op->RegList.RegStart = RegStart;
333 Op->RegList.Number = Number;
334 Op->StartLoc = S;
335 Op->EndLoc = E;
336 return Op;
337 }
338
Chris Lattner3a697562010-10-28 17:20:03 +0000339 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
340 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000341 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000342 Op->StartLoc = S;
343 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000344 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000345 }
346
Chris Lattner3a697562010-10-28 17:20:03 +0000347 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
348 const MCExpr *Offset, unsigned OffsetRegNum,
349 bool OffsetRegShifted, enum ShiftType ShiftType,
350 const MCExpr *ShiftAmount, bool Preindexed,
351 bool Postindexed, bool Negative, bool Writeback,
352 SMLoc S, SMLoc E) {
353 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000354 Op->Mem.BaseRegNum = BaseRegNum;
355 Op->Mem.OffsetIsReg = OffsetIsReg;
356 Op->Mem.Offset = Offset;
357 Op->Mem.OffsetRegNum = OffsetRegNum;
358 Op->Mem.OffsetRegShifted = OffsetRegShifted;
359 Op->Mem.ShiftType = ShiftType;
360 Op->Mem.ShiftAmount = ShiftAmount;
361 Op->Mem.Preindexed = Preindexed;
362 Op->Mem.Postindexed = Postindexed;
363 Op->Mem.Negative = Negative;
364 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000365
Sean Callanan76264762010-04-02 22:27:05 +0000366 Op->StartLoc = S;
367 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000368 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000369 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000370
Chris Lattner3a697562010-10-28 17:20:03 +0000371private:
372 ARMOperand(KindTy K) : Kind(K) {}
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000373};
374
375} // end anonymous namespace.
376
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000377void ARMOperand::dump(raw_ostream &OS) const {
378 switch (Kind) {
379 case CondCode:
380 OS << ARMCondCodeToString(getCondCode());
381 break;
382 case Immediate:
383 getImm()->print(OS);
384 break;
385 case Memory:
386 OS << "<memory>";
387 break;
388 case Register:
389 OS << "<register " << getReg() << ">";
390 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000391 case RegisterList: {
392 OS << "<register_list ";
393 std::pair<unsigned, unsigned> List = getRegList();
394 unsigned RegEnd = List.first + List.second;
395
396 for (unsigned Idx = List.first; Idx < RegEnd; ) {
397 OS << Idx;
398 if (++Idx < RegEnd) OS << ", ";
399 }
400
401 OS << ">";
402 break;
403 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000404 case Token:
405 OS << "'" << getToken() << "'";
406 break;
407 }
408}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000409
410/// @name Auto-generated Match Functions
411/// {
412
413static unsigned MatchRegisterName(StringRef Name);
414
415/// }
416
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000417/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000418/// and if it is a register name the token is eaten and the register number is
419/// returned. Otherwise return -1.
420///
421int ARMAsmParser::TryParseRegister() {
422 const AsmToken &Tok = Parser.getTok();
423 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000424
Chris Lattnere5658fa2010-10-30 04:09:10 +0000425 // FIXME: Validate register for the current architecture; we have to do
426 // validation later, so maybe there is no need for this here.
Bill Wendlingd68fd9c2010-11-06 10:45:34 +0000427 unsigned RegNum = MatchRegisterName(Tok.getString());
428 if (RegNum == 0)
Chris Lattnere5658fa2010-10-30 04:09:10 +0000429 return -1;
430 Parser.Lex(); // Eat identifier token.
431 return RegNum;
432}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000433
434
Chris Lattnere5658fa2010-10-30 04:09:10 +0000435/// Try to parse a register name. The token must be an Identifier when called,
436/// and if it is a register name the token is eaten and the register number is
437/// returned. Otherwise return -1.
Chris Lattner3a697562010-10-28 17:20:03 +0000438///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000439/// TODO this is likely to change to allow different register types and or to
440/// parse for a specific register type.
Chris Lattnere5658fa2010-10-30 04:09:10 +0000441ARMOperand *ARMAsmParser::TryParseRegisterWithWriteBack() {
442 SMLoc S = Parser.getTok().getLoc();
443 int RegNo = TryParseRegister();
444 if (RegNo == -1) return 0;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000445
Chris Lattnere5658fa2010-10-30 04:09:10 +0000446 SMLoc E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000447
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000448 bool Writeback = false;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000449 const AsmToken &ExclaimTok = Parser.getTok();
450 if (ExclaimTok.is(AsmToken::Exclaim)) {
451 E = ExclaimTok.getLoc();
452 Writeback = true;
453 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000454 }
455
Chris Lattnere5658fa2010-10-30 04:09:10 +0000456 return ARMOperand::CreateReg(RegNo, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000457}
458
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000459/// Parse a register list, return it if successful else return null. The first
460/// token must be a '{' when called.
461ARMOperand *ARMAsmParser::ParseRegisterList() {
Sean Callanan76264762010-04-02 22:27:05 +0000462 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000463 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000464 "Token is not a Left Curly Brace");
Sean Callanan76264762010-04-02 22:27:05 +0000465 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000466 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000467
Sean Callanan18b83232010-01-19 21:44:56 +0000468 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000469 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000470 if (RegTok.isNot(AsmToken::Identifier)) {
471 Error(RegLoc, "register expected");
472 return 0;
473 }
Bill Wendling1d6a2652010-11-06 10:40:24 +0000474 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000475 if (RegNum == -1) {
476 Error(RegLoc, "register expected");
477 return 0;
478 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000479
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000480 unsigned RegList = 1 << RegNum;
481
482 int HighRegNum = RegNum;
483 // TODO ranges like "{Rn-Rm}"
Sean Callanan18b83232010-01-19 21:44:56 +0000484 while (Parser.getTok().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000485 Parser.Lex(); // Eat comma 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 Wendling1d6a2652010-11-06 10:40:24 +0000493 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000494 if (RegNum == -1) {
495 Error(RegLoc, "register expected");
496 return 0;
497 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000498
499 if (RegList & (1 << RegNum))
500 Warning(RegLoc, "register duplicated in register list");
501 else if (RegNum <= HighRegNum)
502 Warning(RegLoc, "register not in ascending order in register list");
503 RegList |= 1 << RegNum;
504 HighRegNum = RegNum;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000505 }
Sean Callanan18b83232010-01-19 21:44:56 +0000506 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000507 if (RCurlyTok.isNot(AsmToken::RCurly)) {
508 Error(RCurlyTok.getLoc(), "'}' expected");
509 return 0;
510 }
Sean Callanan76264762010-04-02 22:27:05 +0000511 E = RCurlyTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000512 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000513
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000514 // FIXME: Need to return an operand!
515 Error(E, "FIXME: register list parsing not implemented");
516 return 0;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000517}
518
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000519/// Parse an arm memory expression, return false if successful else return true
520/// or an error. The first token must be a '[' when called.
521/// TODO Only preindexing and postindexing addressing are started, unindexed
522/// with option, etc are still to do.
Chris Lattner550276e2010-10-28 20:52:15 +0000523ARMOperand *ARMAsmParser::ParseMemory() {
Sean Callanan76264762010-04-02 22:27:05 +0000524 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000525 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000526 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000527 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000528 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000529
Sean Callanan18b83232010-01-19 21:44:56 +0000530 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000531 if (BaseRegTok.isNot(AsmToken::Identifier)) {
532 Error(BaseRegTok.getLoc(), "register expected");
533 return 0;
534 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000535 int BaseRegNum = TryParseRegister();
536 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000537 Error(BaseRegTok.getLoc(), "register expected");
538 return 0;
539 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000540
541 bool Preindexed = false;
542 bool Postindexed = false;
543 bool OffsetIsReg = false;
544 bool Negative = false;
545 bool Writeback = false;
546
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000547 // First look for preindexed address forms, that is after the "[Rn" we now
548 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000549 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000550 if (Tok.is(AsmToken::Comma)) {
551 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000552 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000553 int OffsetRegNum;
554 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000555 enum ShiftType ShiftType;
556 const MCExpr *ShiftAmount;
557 const MCExpr *Offset;
Chris Lattner550276e2010-10-28 20:52:15 +0000558 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
559 Offset, OffsetIsReg, OffsetRegNum, E))
560 return 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000561 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000562 if (RBracTok.isNot(AsmToken::RBrac)) {
563 Error(RBracTok.getLoc(), "']' expected");
564 return 0;
565 }
Sean Callanan76264762010-04-02 22:27:05 +0000566 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000567 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000568
Sean Callanan18b83232010-01-19 21:44:56 +0000569 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000570 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000571 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000572 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000573 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000574 }
Chris Lattner550276e2010-10-28 20:52:15 +0000575 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
576 OffsetRegShifted, ShiftType, ShiftAmount,
577 Preindexed, Postindexed, Negative, Writeback,
578 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000579 }
580 // The "[Rn" we have so far was not followed by a comma.
581 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000582 // If there's anything other than the right brace, this is a post indexing
583 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000584 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000585 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000586
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000587 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000588 bool OffsetRegShifted = false;
589 enum ShiftType ShiftType;
590 const MCExpr *ShiftAmount;
Chris Lattner14b93852010-10-29 00:27:31 +0000591 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000592
Sean Callanan18b83232010-01-19 21:44:56 +0000593 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000594 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000595 Postindexed = true;
596 Writeback = true;
Chris Lattner550276e2010-10-28 20:52:15 +0000597 if (NextTok.isNot(AsmToken::Comma)) {
598 Error(NextTok.getLoc(), "',' expected");
599 return 0;
600 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000601 Parser.Lex(); // Eat comma token.
Chris Lattner550276e2010-10-28 20:52:15 +0000602 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000603 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000604 E))
605 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000606 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000607
Chris Lattner550276e2010-10-28 20:52:15 +0000608 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
609 OffsetRegShifted, ShiftType, ShiftAmount,
610 Preindexed, Postindexed, Negative, Writeback,
611 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000612 }
613
Chris Lattner550276e2010-10-28 20:52:15 +0000614 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000615}
616
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000617/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
618/// we will parse the following (were +/- means that a plus or minus is
619/// optional):
620/// +/-Rm
621/// +/-Rm, shift
622/// #offset
623/// we return false on success or an error otherwise.
624bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000625 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000626 enum ShiftType &ShiftType,
627 const MCExpr *&ShiftAmount,
628 const MCExpr *&Offset,
629 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000630 int &OffsetRegNum,
631 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000632 Negative = false;
633 OffsetRegShifted = false;
634 OffsetIsReg = false;
635 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000636 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000637 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000638 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000639 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000640 else if (NextTok.is(AsmToken::Minus)) {
641 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000642 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000643 }
644 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000645 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000646 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000647 SMLoc CurLoc = OffsetRegTok.getLoc();
648 OffsetRegNum = TryParseRegister();
649 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000650 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000651 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000652 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000653 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000654
Bill Wendling12f40e92010-11-06 10:51:53 +0000655 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000656 if (OffsetRegNum != -1) {
657 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000658 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000659 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000660 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000661
Sean Callanan18b83232010-01-19 21:44:56 +0000662 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000663 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000664 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000665 OffsetRegShifted = true;
666 }
667 }
668 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
669 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000670 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000671 if (HashTok.isNot(AsmToken::Hash))
672 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000673
Sean Callananb9a25b72010-01-19 20:27:46 +0000674 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000675
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000676 if (getParser().ParseExpression(Offset))
677 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000678 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000679 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000680 return false;
681}
682
683/// ParseShift as one of these two:
684/// ( lsl | lsr | asr | ror ) , # shift_amount
685/// rrx
686/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000687bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000688 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000689 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000690 if (Tok.isNot(AsmToken::Identifier))
691 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000692 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000693 if (ShiftName == "lsl" || ShiftName == "LSL")
694 St = Lsl;
695 else if (ShiftName == "lsr" || ShiftName == "LSR")
696 St = Lsr;
697 else if (ShiftName == "asr" || ShiftName == "ASR")
698 St = Asr;
699 else if (ShiftName == "ror" || ShiftName == "ROR")
700 St = Ror;
701 else if (ShiftName == "rrx" || ShiftName == "RRX")
702 St = Rrx;
703 else
704 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000705 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000706
707 // Rrx stands alone.
708 if (St == Rrx)
709 return false;
710
711 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000712 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000713 if (HashTok.isNot(AsmToken::Hash))
714 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000715 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000716
717 if (getParser().ParseExpression(ShiftAmount))
718 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000719
720 return false;
721}
722
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000723/// Parse a arm instruction operand. For now this parses the operand regardless
724/// of the mnemonic.
Chris Lattner550276e2010-10-28 20:52:15 +0000725ARMOperand *ARMAsmParser::ParseOperand() {
Sean Callanan76264762010-04-02 22:27:05 +0000726 SMLoc S, E;
Jim Grosbach16c74252010-10-29 14:46:02 +0000727
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000728 switch (getLexer().getKind()) {
729 case AsmToken::Identifier:
Chris Lattnere5658fa2010-10-30 04:09:10 +0000730 if (ARMOperand *Op = TryParseRegisterWithWriteBack())
Chris Lattner550276e2010-10-28 20:52:15 +0000731 return Op;
Jim Grosbach16c74252010-10-29 14:46:02 +0000732
Kevin Enderby515d5092009-10-15 20:48:48 +0000733 // This was not a register so parse other operands that start with an
734 // identifier (like labels) as expressions and create them as immediates.
735 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000736 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000737 if (getParser().ParseExpression(IdVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000738 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000739 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000740 return ARMOperand::CreateImm(IdVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000741 case AsmToken::LBrac:
Chris Lattner550276e2010-10-28 20:52:15 +0000742 return ParseMemory();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000743 case AsmToken::LCurly:
Chris Lattner550276e2010-10-28 20:52:15 +0000744 return ParseRegisterList();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000745 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000746 // #42 -> immediate.
747 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000748 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000749 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000750 const MCExpr *ImmVal;
751 if (getParser().ParseExpression(ImmVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000752 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000753 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000754 return ARMOperand::CreateImm(ImmVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000755 default:
Chris Lattner550276e2010-10-28 20:52:15 +0000756 Error(Parser.getTok().getLoc(), "unexpected token in operand");
757 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000758 }
759}
760
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000761/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000762bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000763 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000764 // Create the leading tokens for the mnemonic, split by '.' characters.
765 size_t Start = 0, Next = Name.find('.');
766 StringRef Head = Name.slice(Start, Next);
767
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000768 // Determine the predicate, if any.
769 //
770 // FIXME: We need a way to check whether a prefix supports predication,
771 // otherwise we will end up with an ambiguity for instructions that happen to
772 // end with a predicate name.
Jim Grosbach3df518e2010-10-29 21:56:51 +0000773 // FIXME: Likewise, some arithmetic instructions have an 's' prefix which
774 // indicates to update the condition codes. Those instructions have an
775 // additional immediate operand which encodes the prefix as reg0 or CPSR.
776 // Just checking for a suffix of 's' definitely creates ambiguities; e.g,
777 // the SMMLS instruction.
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000778 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
779 .Case("eq", ARMCC::EQ)
780 .Case("ne", ARMCC::NE)
781 .Case("hs", ARMCC::HS)
782 .Case("lo", ARMCC::LO)
783 .Case("mi", ARMCC::MI)
784 .Case("pl", ARMCC::PL)
785 .Case("vs", ARMCC::VS)
786 .Case("vc", ARMCC::VC)
787 .Case("hi", ARMCC::HI)
788 .Case("ls", ARMCC::LS)
789 .Case("ge", ARMCC::GE)
790 .Case("lt", ARMCC::LT)
791 .Case("gt", ARMCC::GT)
792 .Case("le", ARMCC::LE)
793 .Case("al", ARMCC::AL)
794 .Default(~0U);
Jim Grosbach16c74252010-10-29 14:46:02 +0000795
Chris Lattnerdba34d82010-10-30 04:35:59 +0000796 if (CC == ~0U ||
797 (CC == ARMCC::LS && (Head == "vmls" || Head == "vnmls"))) {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000798 CC = ARMCC::AL;
Chris Lattnerdba34d82010-10-30 04:35:59 +0000799 } else {
800 Head = Head.slice(0, Head.size() - 2);
Bill Wendling52925b62010-10-29 23:50:21 +0000801 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000802
Chris Lattner3a697562010-10-28 17:20:03 +0000803 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Jim Grosbach469ebbe2010-11-01 18:11:14 +0000804 // FIXME: Should only add this operand for predicated instructions
Chris Lattner3a697562010-10-28 17:20:03 +0000805 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000806
807 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000808 while (Next != StringRef::npos) {
809 Start = Next;
810 Next = Name.find('.', Start + 1);
811 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000812
Chris Lattner3a697562010-10-28 17:20:03 +0000813 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000814 }
815
816 // Read the remaining operands.
817 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000818 // Read the first operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000819 if (ARMOperand *Op = ParseOperand())
820 Operands.push_back(Op);
821 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000822 Parser.EatToEndOfStatement();
823 return true;
824 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000825
826 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000827 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000828
829 // Parse and remember the operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000830 if (ARMOperand *Op = ParseOperand())
831 Operands.push_back(Op);
832 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000833 Parser.EatToEndOfStatement();
834 return true;
835 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000836 }
837 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000838
Chris Lattnercbf8a982010-09-11 16:18:25 +0000839 if (getLexer().isNot(AsmToken::EndOfStatement)) {
840 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000841 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000842 }
Chris Lattner34e53142010-09-08 05:10:46 +0000843 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000844 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000845}
846
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000847bool ARMAsmParser::
848MatchAndEmitInstruction(SMLoc IDLoc,
849 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
850 MCStreamer &Out) {
851 MCInst Inst;
852 unsigned ErrorInfo;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000853 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
854 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000855 Out.EmitInstruction(Inst);
856 return false;
Jim Grosbach16c74252010-10-29 14:46:02 +0000857
Chris Lattnere73d4f82010-10-28 21:41:58 +0000858 case Match_MissingFeature:
859 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
860 return true;
861 case Match_InvalidOperand: {
862 SMLoc ErrorLoc = IDLoc;
863 if (ErrorInfo != ~0U) {
864 if (ErrorInfo >= Operands.size())
865 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +0000866
Chris Lattnere73d4f82010-10-28 21:41:58 +0000867 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
868 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
869 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000870
Chris Lattnere73d4f82010-10-28 21:41:58 +0000871 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000872 }
Chris Lattnere73d4f82010-10-28 21:41:58 +0000873 case Match_MnemonicFail:
874 return Error(IDLoc, "unrecognized instruction mnemonic");
875 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000876
Eric Christopherc223e2b2010-10-29 09:26:59 +0000877 llvm_unreachable("Implement any new match types added!");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000878}
879
880
881
Kevin Enderby515d5092009-10-15 20:48:48 +0000882/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000883bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
884 StringRef IDVal = DirectiveID.getIdentifier();
885 if (IDVal == ".word")
886 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000887 else if (IDVal == ".thumb")
888 return ParseDirectiveThumb(DirectiveID.getLoc());
889 else if (IDVal == ".thumb_func")
890 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
891 else if (IDVal == ".code")
892 return ParseDirectiveCode(DirectiveID.getLoc());
893 else if (IDVal == ".syntax")
894 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000895 return true;
896}
897
898/// ParseDirectiveWord
899/// ::= .word [ expression (, expression)* ]
900bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
901 if (getLexer().isNot(AsmToken::EndOfStatement)) {
902 for (;;) {
903 const MCExpr *Value;
904 if (getParser().ParseExpression(Value))
905 return true;
906
Chris Lattneraaec2052010-01-19 19:46:13 +0000907 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000908
909 if (getLexer().is(AsmToken::EndOfStatement))
910 break;
Jim Grosbach16c74252010-10-29 14:46:02 +0000911
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000912 // FIXME: Improve diagnostic.
913 if (getLexer().isNot(AsmToken::Comma))
914 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000915 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000916 }
917 }
918
Sean Callananb9a25b72010-01-19 20:27:46 +0000919 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000920 return false;
921}
922
Kevin Enderby515d5092009-10-15 20:48:48 +0000923/// ParseDirectiveThumb
924/// ::= .thumb
925bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
926 if (getLexer().isNot(AsmToken::EndOfStatement))
927 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000928 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000929
930 // TODO: set thumb mode
931 // TODO: tell the MC streamer the mode
932 // getParser().getStreamer().Emit???();
933 return false;
934}
935
936/// ParseDirectiveThumbFunc
937/// ::= .thumbfunc symbol_name
938bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000939 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000940 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +0000941 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000942 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +0000943 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000944 if (getLexer().isNot(AsmToken::EndOfStatement))
945 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000946 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000947
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000948 // Mark symbol as a thumb symbol.
949 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
950 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +0000951 return false;
952}
953
954/// ParseDirectiveSyntax
955/// ::= .syntax unified | divided
956bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000957 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000958 if (Tok.isNot(AsmToken::Identifier))
959 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000960 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000961 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000962 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000963 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000964 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000965 else
966 return Error(L, "unrecognized syntax mode in .syntax directive");
967
968 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000969 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000970 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000971
972 // TODO tell the MC streamer the mode
973 // getParser().getStreamer().Emit???();
974 return false;
975}
976
977/// ParseDirectiveCode
978/// ::= .code 16 | 32
979bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000980 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000981 if (Tok.isNot(AsmToken::Integer))
982 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000983 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +0000984 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +0000985 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000986 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +0000987 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000988 else
989 return Error(L, "invalid operand to .code directive");
990
991 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000992 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000993 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000994
Jim Grosbach2a301702010-11-05 22:40:53 +0000995 if (Val == 16)
996 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
997 else
998 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
999
Kevin Enderby515d5092009-10-15 20:48:48 +00001000 return false;
1001}
1002
Sean Callanan90b70972010-04-07 20:29:34 +00001003extern "C" void LLVMInitializeARMAsmLexer();
1004
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001005/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001006extern "C" void LLVMInitializeARMAsmParser() {
1007 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1008 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001009 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001010}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001011
Chris Lattner0692ee62010-09-06 19:11:01 +00001012#define GET_REGISTER_MATCHER
1013#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001014#include "ARMGenAsmMatcher.inc"