blob: 8ae5e03d261ace29d14f11b1ba9e1f89fc2606e1 [file] [log] [blame]
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001//===-- ARMAsmParser.cpp - Parse ARM assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ARM.h"
Bill Wendling92b5a2e2010-11-03 01:49:29 +000011#include "ARMAddressingModes.h"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000012#include "ARMSubtarget.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000013#include "llvm/MC/MCParser/MCAsmLexer.h"
14#include "llvm/MC/MCParser/MCAsmParser.h"
15#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Jim Grosbach642fc9c2010-11-05 22:33:53 +000016#include "llvm/MC/MCContext.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000017#include "llvm/MC/MCStreamer.h"
18#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCInst.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000020#include "llvm/Target/TargetRegistry.h"
21#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000022#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000023#include "llvm/Support/raw_ostream.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/ADT/SmallVector.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000025#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000026#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000027using namespace llvm;
28
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000029// The shift types for register controlled shifts in arm memory addressing
30enum ShiftType {
31 Lsl,
32 Lsr,
33 Asr,
34 Ror,
35 Rrx
36};
37
Chris Lattner3a697562010-10-28 17:20:03 +000038namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000039
40class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000041
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000042class ARMAsmParser : public TargetAsmParser {
43 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000044 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000045
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000046 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000047 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
48
49 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000050 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
51
Chris Lattnere5658fa2010-10-30 04:09:10 +000052 int TryParseRegister();
53 ARMOperand *TryParseRegisterWithWriteBack();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +000054 ARMOperand *ParseRegisterList();
Chris Lattner550276e2010-10-28 20:52:15 +000055 ARMOperand *ParseMemory();
Bill Wendling146018f2010-11-06 21:42:12 +000056 ARMOperand *ParseOperand();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000057
Kevin Enderby9c41fa82009-10-30 22:55:57 +000058 bool ParseMemoryOffsetReg(bool &Negative,
59 bool &OffsetRegShifted,
60 enum ShiftType &ShiftType,
61 const MCExpr *&ShiftAmount,
62 const MCExpr *&Offset,
63 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000064 int &OffsetRegNum,
65 SMLoc &E);
Sean Callanan76264762010-04-02 22:27:05 +000066 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000067 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000068 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000069 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000070 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000071 bool ParseDirectiveSyntax(SMLoc L);
72
Chris Lattner7036f8b2010-09-29 01:42:58 +000073 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000074 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000075 MCStreamer &Out);
Jim Grosbach16c74252010-10-29 14:46:02 +000076
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000077 /// @name Auto-generated Match Functions
78 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000079
Chris Lattner0692ee62010-09-06 19:11:01 +000080#define GET_ASSEMBLER_HEADER
81#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000082
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000083 /// }
84
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000085public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000086 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +000087 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
88 // Initialize the set of available features.
89 setAvailableFeatures(ComputeAvailableFeatures(
90 &TM.getSubtarget<ARMSubtarget>()));
91 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000092
Benjamin Kramer38e59892010-07-14 22:38:02 +000093 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000094 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000095 virtual bool ParseDirective(AsmToken DirectiveID);
96};
Jim Grosbach16c74252010-10-29 14:46:02 +000097} // end anonymous namespace
98
Chris Lattner3a697562010-10-28 17:20:03 +000099namespace {
100
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000101/// ARMOperand - Instances of this class represent a parsed ARM machine
102/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000103class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000104 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000105 CondCode,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000106 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000107 Memory,
108 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000109 RegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000110 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000111 } Kind;
112
Sean Callanan76264762010-04-02 22:27:05 +0000113 SMLoc StartLoc, EndLoc;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000114
115 union {
116 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000117 ARMCC::CondCodes Val;
118 } CC;
119
120 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000121 const char *Data;
122 unsigned Length;
123 } Tok;
124
125 struct {
126 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000127 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000128 } Reg;
129
Bill Wendling8155e5b2010-11-06 22:19:43 +0000130 struct {
Bill Wendling7729e062010-11-09 22:44:22 +0000131 std::vector<unsigned> *Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000132 } RegList;
133
Kevin Enderbycfe07242009-10-13 22:19:02 +0000134 struct {
135 const MCExpr *Val;
136 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000137
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000138 // This is for all forms of ARM address expressions
139 struct {
140 unsigned BaseRegNum;
Bill Wendling146018f2010-11-06 21:42:12 +0000141 unsigned OffsetRegNum; // used when OffsetIsReg is true
142 const MCExpr *Offset; // used when OffsetIsReg is false
143 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
144 enum ShiftType ShiftType; // used when OffsetRegShifted is true
145 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
146 unsigned Preindexed : 1;
147 unsigned Postindexed : 1;
148 unsigned OffsetIsReg : 1;
149 unsigned Negative : 1; // only used when OffsetIsReg is true
150 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000151 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000152 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000153
Bill Wendling146018f2010-11-06 21:42:12 +0000154 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
155public:
Sean Callanan76264762010-04-02 22:27:05 +0000156 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
157 Kind = o.Kind;
158 StartLoc = o.StartLoc;
159 EndLoc = o.EndLoc;
160 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000161 case CondCode:
162 CC = o.CC;
163 break;
Sean Callanan76264762010-04-02 22:27:05 +0000164 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000165 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000166 break;
167 case Register:
168 Reg = o.Reg;
169 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000170 case RegisterList:
171 RegList = o.RegList;
172 break;
Sean Callanan76264762010-04-02 22:27:05 +0000173 case Immediate:
174 Imm = o.Imm;
175 break;
176 case Memory:
177 Mem = o.Mem;
178 break;
179 }
180 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000181
Sean Callanan76264762010-04-02 22:27:05 +0000182 /// getStartLoc - Get the location of the first token of this operand.
183 SMLoc getStartLoc() const { return StartLoc; }
184 /// getEndLoc - Get the location of the last token of this operand.
185 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000186
Daniel Dunbar8462b302010-08-11 06:36:53 +0000187 ARMCC::CondCodes getCondCode() const {
188 assert(Kind == CondCode && "Invalid access!");
189 return CC.Val;
190 }
191
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000192 StringRef getToken() const {
193 assert(Kind == Token && "Invalid access!");
194 return StringRef(Tok.Data, Tok.Length);
195 }
196
197 unsigned getReg() const {
Bill Wendling7729e062010-11-09 22:44:22 +0000198 assert(Kind == Register && "Invalid access!");
199 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000200 }
201
Bill Wendling7729e062010-11-09 22:44:22 +0000202 const std::vector<unsigned> &getRegList() const {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000203 assert(Kind == RegisterList && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000204 return *RegList.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000205 }
206
Kevin Enderbycfe07242009-10-13 22:19:02 +0000207 const MCExpr *getImm() const {
208 assert(Kind == Immediate && "Invalid access!");
209 return Imm.Val;
210 }
211
Daniel Dunbar8462b302010-08-11 06:36:53 +0000212 bool isCondCode() const { return Kind == CondCode; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000213 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000214 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000215 bool isRegList() const { return Kind == RegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000216 bool isToken() const { return Kind == Token; }
217 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000218 bool isMemMode5() const {
219 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
220 Mem.Writeback || Mem.Negative)
221 return false;
222 // If there is an offset expression, make sure it's valid.
223 if (!Mem.Offset)
224 return true;
225 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
226 if (!CE)
227 return false;
228 // The offset must be a multiple of 4 in the range 0-1020.
229 int64_t Value = CE->getValue();
230 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
231 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000232
233 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000234 // Add as immediates when possible. Null MCExpr = 0.
235 if (Expr == 0)
236 Inst.addOperand(MCOperand::CreateImm(0));
237 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000238 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
239 else
240 Inst.addOperand(MCOperand::CreateExpr(Expr));
241 }
242
Daniel Dunbar8462b302010-08-11 06:36:53 +0000243 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000244 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000245 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000246 // FIXME: What belongs here?
247 Inst.addOperand(MCOperand::CreateReg(0));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000248 }
249
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000250 void addRegOperands(MCInst &Inst, unsigned N) const {
251 assert(N == 1 && "Invalid number of operands!");
252 Inst.addOperand(MCOperand::CreateReg(getReg()));
253 }
254
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000255 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000256 assert(N == 1 && "Invalid number of operands!");
257 const std::vector<unsigned> &RegList = getRegList();
258 for (std::vector<unsigned>::const_iterator
259 I = RegList.begin(), E = RegList.end(); I != E; ++I)
260 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000261 }
262
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000263 void addImmOperands(MCInst &Inst, unsigned N) const {
264 assert(N == 1 && "Invalid number of operands!");
265 addExpr(Inst, getImm());
266 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000267
Chris Lattner14b93852010-10-29 00:27:31 +0000268 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
269 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000270
Chris Lattner14b93852010-10-29 00:27:31 +0000271 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlinga60f1572010-11-06 10:48:18 +0000272 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000273
Jim Grosbach80eb2332010-10-29 17:41:25 +0000274 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
275 // the difference?
276 if (Mem.Offset) {
277 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000278 assert(CE && "Non-constant mode 5 offset operand!");
279
Jim Grosbach80eb2332010-10-29 17:41:25 +0000280 // The MCInst offset operand doesn't include the low two bits (like
281 // the instruction encoding).
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000282 int64_t Offset = CE->getValue() / 4;
283 if (Offset >= 0)
284 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
285 Offset)));
286 else
287 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
288 -Offset)));
289 } else {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000290 Inst.addOperand(MCOperand::CreateImm(0));
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000291 }
Chris Lattner14b93852010-10-29 00:27:31 +0000292 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000293
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000294 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000295
Chris Lattner3a697562010-10-28 17:20:03 +0000296 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
297 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000298 Op->CC.Val = CC;
299 Op->StartLoc = S;
300 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000301 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000302 }
303
Chris Lattner3a697562010-10-28 17:20:03 +0000304 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
305 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000306 Op->Tok.Data = Str.data();
307 Op->Tok.Length = Str.size();
308 Op->StartLoc = S;
309 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000310 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000311 }
312
Chris Lattner3a697562010-10-28 17:20:03 +0000313 static ARMOperand *CreateReg(unsigned RegNum, bool Writeback, SMLoc S,
314 SMLoc E) {
315 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000316 Op->Reg.RegNum = RegNum;
317 Op->Reg.Writeback = Writeback;
Sean Callanan76264762010-04-02 22:27:05 +0000318 Op->StartLoc = S;
319 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000320 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000321 }
322
Bill Wendling7729e062010-11-09 22:44:22 +0000323 static ARMOperand *
324 CreateRegList(std::vector<std::pair<unsigned, SMLoc> > &Regs,
325 SMLoc S, SMLoc E) {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000326 ARMOperand *Op = new ARMOperand(RegisterList);
Bill Wendling7729e062010-11-09 22:44:22 +0000327 Op->RegList.Registers = new std::vector<unsigned>();
328 for (std::vector<std::pair<unsigned, SMLoc> >::iterator
329 I = Regs.begin(), E = Regs.end(); I != E; ++I)
330 Op->RegList.Registers->push_back(I->first);
331 std::sort(Op->RegList.Registers->begin(), Op->RegList.Registers->end());
Bill Wendling8d5acb72010-11-06 19:56:04 +0000332 Op->StartLoc = S;
333 Op->EndLoc = E;
334 return Op;
335 }
336
Chris Lattner3a697562010-10-28 17:20:03 +0000337 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
338 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000339 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000340 Op->StartLoc = S;
341 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000342 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000343 }
344
Chris Lattner3a697562010-10-28 17:20:03 +0000345 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
346 const MCExpr *Offset, unsigned OffsetRegNum,
347 bool OffsetRegShifted, enum ShiftType ShiftType,
348 const MCExpr *ShiftAmount, bool Preindexed,
349 bool Postindexed, bool Negative, bool Writeback,
350 SMLoc S, SMLoc E) {
351 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000352 Op->Mem.BaseRegNum = BaseRegNum;
353 Op->Mem.OffsetIsReg = OffsetIsReg;
354 Op->Mem.Offset = Offset;
355 Op->Mem.OffsetRegNum = OffsetRegNum;
356 Op->Mem.OffsetRegShifted = OffsetRegShifted;
357 Op->Mem.ShiftType = ShiftType;
358 Op->Mem.ShiftAmount = ShiftAmount;
359 Op->Mem.Preindexed = Preindexed;
360 Op->Mem.Postindexed = Postindexed;
361 Op->Mem.Negative = Negative;
362 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000363
Sean Callanan76264762010-04-02 22:27:05 +0000364 Op->StartLoc = S;
365 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000366 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000367 }
368};
369
370} // end anonymous namespace.
371
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000372void ARMOperand::dump(raw_ostream &OS) const {
373 switch (Kind) {
374 case CondCode:
375 OS << ARMCondCodeToString(getCondCode());
376 break;
377 case Immediate:
378 getImm()->print(OS);
379 break;
380 case Memory:
381 OS << "<memory>";
382 break;
383 case Register:
384 OS << "<register " << getReg() << ">";
385 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000386 case RegisterList: {
387 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000388
Bill Wendling7729e062010-11-09 22:44:22 +0000389 const std::vector<unsigned> &RegList = getRegList();
390 for (std::vector<unsigned>::const_iterator
391 I = RegList.begin(), E = RegList.end(); I != E; ) {
392 OS << *I;
393 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000394 }
395
396 OS << ">";
397 break;
398 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000399 case Token:
400 OS << "'" << getToken() << "'";
401 break;
402 }
403}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000404
405/// @name Auto-generated Match Functions
406/// {
407
408static unsigned MatchRegisterName(StringRef Name);
409
410/// }
411
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000412/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000413/// and if it is a register name the token is eaten and the register number is
414/// returned. Otherwise return -1.
415///
416int ARMAsmParser::TryParseRegister() {
417 const AsmToken &Tok = Parser.getTok();
418 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000419
Chris Lattnere5658fa2010-10-30 04:09:10 +0000420 // FIXME: Validate register for the current architecture; we have to do
421 // validation later, so maybe there is no need for this here.
Bill Wendlingd68fd9c2010-11-06 10:45:34 +0000422 unsigned RegNum = MatchRegisterName(Tok.getString());
423 if (RegNum == 0)
Chris Lattnere5658fa2010-10-30 04:09:10 +0000424 return -1;
425 Parser.Lex(); // Eat identifier token.
426 return RegNum;
427}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000428
429
Chris Lattnere5658fa2010-10-30 04:09:10 +0000430/// Try to parse a register name. The token must be an Identifier when called,
431/// and if it is a register name the token is eaten and the register number is
432/// returned. Otherwise return -1.
Chris Lattner3a697562010-10-28 17:20:03 +0000433///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000434/// TODO this is likely to change to allow different register types and or to
435/// parse for a specific register type.
Chris Lattnere5658fa2010-10-30 04:09:10 +0000436ARMOperand *ARMAsmParser::TryParseRegisterWithWriteBack() {
437 SMLoc S = Parser.getTok().getLoc();
438 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000439 if (RegNo == -1)
440 return 0;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000441
Chris Lattnere5658fa2010-10-30 04:09:10 +0000442 SMLoc E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000443
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000444 bool Writeback = false;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000445 const AsmToken &ExclaimTok = Parser.getTok();
446 if (ExclaimTok.is(AsmToken::Exclaim)) {
447 E = ExclaimTok.getLoc();
448 Writeback = true;
449 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000450 }
451
Chris Lattnere5658fa2010-10-30 04:09:10 +0000452 return ARMOperand::CreateReg(RegNo, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000453}
454
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000455/// Parse a register list, return it if successful else return null. The first
456/// token must be a '{' when called.
457ARMOperand *ARMAsmParser::ParseRegisterList() {
Sean Callanan18b83232010-01-19 21:44:56 +0000458 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000459 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000460 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000461
Bill Wendling7729e062010-11-09 22:44:22 +0000462 // Read the rest of the registers in the list.
463 unsigned PrevRegNum = 0;
Bill Wendlinge7176102010-11-06 22:36:58 +0000464 std::vector<std::pair<unsigned, SMLoc> > Registers;
465 Registers.reserve(32);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000466
Bill Wendling7729e062010-11-09 22:44:22 +0000467 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000468 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000469 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000470
Sean Callanan18b83232010-01-19 21:44:56 +0000471 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000472 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000473 if (RegTok.isNot(AsmToken::Identifier)) {
474 Error(RegLoc, "register expected");
475 return 0;
476 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000477
Bill Wendling1d6a2652010-11-06 10:40:24 +0000478 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000479 if (RegNum == -1) {
480 Error(RegLoc, "register expected");
481 return 0;
482 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000483
Bill Wendlinge7176102010-11-06 22:36:58 +0000484 if (IsRange) {
485 int Reg = PrevRegNum;
486 do {
487 ++Reg;
488 Registers.push_back(std::make_pair(Reg, RegLoc));
489 } while (Reg != RegNum);
490 } else {
491 Registers.push_back(std::make_pair(RegNum, RegLoc));
492 }
493
494 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000495 } while (Parser.getTok().is(AsmToken::Comma) ||
496 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000497
498 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000499 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000500 if (RCurlyTok.isNot(AsmToken::RCurly)) {
501 Error(RCurlyTok.getLoc(), "'}' expected");
502 return 0;
503 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000504
Bill Wendlinge7176102010-11-06 22:36:58 +0000505 SMLoc E = RCurlyTok.getLoc();
506 Parser.Lex(); // Eat right curly brace token.
507
508 // Verify the register list.
Bill Wendling7729e062010-11-09 22:44:22 +0000509 std::vector<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000510 RI = Registers.begin(), RE = Registers.end();
511
Bill Wendlinge7176102010-11-06 22:36:58 +0000512 unsigned HighRegNum = RI->first;
Bill Wendlinge7176102010-11-06 22:36:58 +0000513 DenseMap<unsigned, bool> RegMap;
514 RegMap[RI->first] = true;
515
516 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000517 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendlinge7176102010-11-06 22:36:58 +0000518
519 if (RegMap[RegInfo.first]) {
520 Error(RegInfo.second, "register duplicated in register list");
521 return 0;
522 }
523
524 if (RegInfo.first < HighRegNum)
525 Warning(RegInfo.second,
526 "register not in ascending order in register list");
527
528 RegMap[RegInfo.first] = true;
529 HighRegNum = std::max(RegInfo.first, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000530 }
531
Bill Wendling7729e062010-11-09 22:44:22 +0000532 return ARMOperand::CreateRegList(Registers, S, E);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000533}
534
Bill Wendlinge7176102010-11-06 22:36:58 +0000535/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000536/// or an error. The first token must be a '[' when called.
537/// TODO Only preindexing and postindexing addressing are started, unindexed
538/// with option, etc are still to do.
Chris Lattner550276e2010-10-28 20:52:15 +0000539ARMOperand *ARMAsmParser::ParseMemory() {
Sean Callanan76264762010-04-02 22:27:05 +0000540 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000541 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000542 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000543 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000544 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000545
Sean Callanan18b83232010-01-19 21:44:56 +0000546 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000547 if (BaseRegTok.isNot(AsmToken::Identifier)) {
548 Error(BaseRegTok.getLoc(), "register expected");
549 return 0;
550 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000551 int BaseRegNum = TryParseRegister();
552 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000553 Error(BaseRegTok.getLoc(), "register expected");
554 return 0;
555 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000556
557 bool Preindexed = false;
558 bool Postindexed = false;
559 bool OffsetIsReg = false;
560 bool Negative = false;
561 bool Writeback = false;
562
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000563 // First look for preindexed address forms, that is after the "[Rn" we now
564 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000565 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000566 if (Tok.is(AsmToken::Comma)) {
567 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000568 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000569 int OffsetRegNum;
570 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000571 enum ShiftType ShiftType;
572 const MCExpr *ShiftAmount;
573 const MCExpr *Offset;
Chris Lattner550276e2010-10-28 20:52:15 +0000574 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
575 Offset, OffsetIsReg, OffsetRegNum, E))
576 return 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000577 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000578 if (RBracTok.isNot(AsmToken::RBrac)) {
579 Error(RBracTok.getLoc(), "']' expected");
580 return 0;
581 }
Sean Callanan76264762010-04-02 22:27:05 +0000582 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000583 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000584
Sean Callanan18b83232010-01-19 21:44:56 +0000585 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000586 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000587 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000588 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000589 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000590 }
Chris Lattner550276e2010-10-28 20:52:15 +0000591 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
592 OffsetRegShifted, ShiftType, ShiftAmount,
593 Preindexed, Postindexed, Negative, Writeback,
594 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000595 }
596 // The "[Rn" we have so far was not followed by a comma.
597 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000598 // If there's anything other than the right brace, this is a post indexing
599 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000600 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000601 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000602
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000603 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000604 bool OffsetRegShifted = false;
605 enum ShiftType ShiftType;
606 const MCExpr *ShiftAmount;
Chris Lattner14b93852010-10-29 00:27:31 +0000607 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000608
Sean Callanan18b83232010-01-19 21:44:56 +0000609 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000610 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000611 Postindexed = true;
612 Writeback = true;
Chris Lattner550276e2010-10-28 20:52:15 +0000613 if (NextTok.isNot(AsmToken::Comma)) {
614 Error(NextTok.getLoc(), "',' expected");
615 return 0;
616 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000617 Parser.Lex(); // Eat comma token.
Chris Lattner550276e2010-10-28 20:52:15 +0000618 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000619 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000620 E))
621 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000622 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000623
Chris Lattner550276e2010-10-28 20:52:15 +0000624 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
625 OffsetRegShifted, ShiftType, ShiftAmount,
626 Preindexed, Postindexed, Negative, Writeback,
627 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000628 }
629
Chris Lattner550276e2010-10-28 20:52:15 +0000630 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000631}
632
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000633/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
634/// we will parse the following (were +/- means that a plus or minus is
635/// optional):
636/// +/-Rm
637/// +/-Rm, shift
638/// #offset
639/// we return false on success or an error otherwise.
640bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000641 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000642 enum ShiftType &ShiftType,
643 const MCExpr *&ShiftAmount,
644 const MCExpr *&Offset,
645 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000646 int &OffsetRegNum,
647 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000648 Negative = false;
649 OffsetRegShifted = false;
650 OffsetIsReg = false;
651 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000652 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000653 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000654 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000655 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000656 else if (NextTok.is(AsmToken::Minus)) {
657 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000658 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000659 }
660 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000661 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000662 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000663 SMLoc CurLoc = OffsetRegTok.getLoc();
664 OffsetRegNum = TryParseRegister();
665 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000666 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000667 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000668 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000669 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000670
Bill Wendling12f40e92010-11-06 10:51:53 +0000671 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000672 if (OffsetRegNum != -1) {
673 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000674 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000675 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000676 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000677
Sean Callanan18b83232010-01-19 21:44:56 +0000678 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000679 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000680 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000681 OffsetRegShifted = true;
682 }
683 }
684 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
685 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000686 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000687 if (HashTok.isNot(AsmToken::Hash))
688 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000689
Sean Callananb9a25b72010-01-19 20:27:46 +0000690 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000691
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000692 if (getParser().ParseExpression(Offset))
693 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000694 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000695 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000696 return false;
697}
698
699/// ParseShift as one of these two:
700/// ( lsl | lsr | asr | ror ) , # shift_amount
701/// rrx
702/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000703bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000704 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000705 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000706 if (Tok.isNot(AsmToken::Identifier))
707 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000708 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000709 if (ShiftName == "lsl" || ShiftName == "LSL")
710 St = Lsl;
711 else if (ShiftName == "lsr" || ShiftName == "LSR")
712 St = Lsr;
713 else if (ShiftName == "asr" || ShiftName == "ASR")
714 St = Asr;
715 else if (ShiftName == "ror" || ShiftName == "ROR")
716 St = Ror;
717 else if (ShiftName == "rrx" || ShiftName == "RRX")
718 St = Rrx;
719 else
720 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000721 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000722
723 // Rrx stands alone.
724 if (St == Rrx)
725 return false;
726
727 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000728 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000729 if (HashTok.isNot(AsmToken::Hash))
730 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000731 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000732
733 if (getParser().ParseExpression(ShiftAmount))
734 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000735
736 return false;
737}
738
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000739/// Parse a arm instruction operand. For now this parses the operand regardless
740/// of the mnemonic.
Chris Lattner550276e2010-10-28 20:52:15 +0000741ARMOperand *ARMAsmParser::ParseOperand() {
Sean Callanan76264762010-04-02 22:27:05 +0000742 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000743 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000744 default:
745 Error(Parser.getTok().getLoc(), "unexpected token in operand");
746 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000747 case AsmToken::Identifier:
Chris Lattnere5658fa2010-10-30 04:09:10 +0000748 if (ARMOperand *Op = TryParseRegisterWithWriteBack())
Chris Lattner550276e2010-10-28 20:52:15 +0000749 return Op;
Jim Grosbach16c74252010-10-29 14:46:02 +0000750
Kevin Enderby515d5092009-10-15 20:48:48 +0000751 // This was not a register so parse other operands that start with an
752 // identifier (like labels) as expressions and create them as immediates.
753 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000754 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000755 if (getParser().ParseExpression(IdVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000756 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000757 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000758 return ARMOperand::CreateImm(IdVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000759 case AsmToken::LBrac:
Chris Lattner550276e2010-10-28 20:52:15 +0000760 return ParseMemory();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000761 case AsmToken::LCurly:
Chris Lattner550276e2010-10-28 20:52:15 +0000762 return ParseRegisterList();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000763 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000764 // #42 -> immediate.
765 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000766 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000767 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000768 const MCExpr *ImmVal;
769 if (getParser().ParseExpression(ImmVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000770 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000771 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000772 return ARMOperand::CreateImm(ImmVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000773 }
774}
775
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000776/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000777bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000778 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000779 // Create the leading tokens for the mnemonic, split by '.' characters.
780 size_t Start = 0, Next = Name.find('.');
781 StringRef Head = Name.slice(Start, Next);
782
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000783 // Determine the predicate, if any.
784 //
785 // FIXME: We need a way to check whether a prefix supports predication,
786 // otherwise we will end up with an ambiguity for instructions that happen to
787 // end with a predicate name.
Jim Grosbach3df518e2010-10-29 21:56:51 +0000788 // FIXME: Likewise, some arithmetic instructions have an 's' prefix which
789 // indicates to update the condition codes. Those instructions have an
790 // additional immediate operand which encodes the prefix as reg0 or CPSR.
791 // Just checking for a suffix of 's' definitely creates ambiguities; e.g,
792 // the SMMLS instruction.
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000793 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
794 .Case("eq", ARMCC::EQ)
795 .Case("ne", ARMCC::NE)
796 .Case("hs", ARMCC::HS)
797 .Case("lo", ARMCC::LO)
798 .Case("mi", ARMCC::MI)
799 .Case("pl", ARMCC::PL)
800 .Case("vs", ARMCC::VS)
801 .Case("vc", ARMCC::VC)
802 .Case("hi", ARMCC::HI)
803 .Case("ls", ARMCC::LS)
804 .Case("ge", ARMCC::GE)
805 .Case("lt", ARMCC::LT)
806 .Case("gt", ARMCC::GT)
807 .Case("le", ARMCC::LE)
808 .Case("al", ARMCC::AL)
809 .Default(~0U);
Jim Grosbach16c74252010-10-29 14:46:02 +0000810
Chris Lattnerdba34d82010-10-30 04:35:59 +0000811 if (CC == ~0U ||
812 (CC == ARMCC::LS && (Head == "vmls" || Head == "vnmls"))) {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000813 CC = ARMCC::AL;
Chris Lattnerdba34d82010-10-30 04:35:59 +0000814 } else {
815 Head = Head.slice(0, Head.size() - 2);
Bill Wendling52925b62010-10-29 23:50:21 +0000816 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000817
Chris Lattner3a697562010-10-28 17:20:03 +0000818 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Jim Grosbach469ebbe2010-11-01 18:11:14 +0000819 // FIXME: Should only add this operand for predicated instructions
Chris Lattner3a697562010-10-28 17:20:03 +0000820 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000821
822 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000823 while (Next != StringRef::npos) {
824 Start = Next;
825 Next = Name.find('.', Start + 1);
826 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000827
Chris Lattner3a697562010-10-28 17:20:03 +0000828 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000829 }
830
831 // Read the remaining operands.
832 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000833 // Read the first operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000834 if (ARMOperand *Op = ParseOperand())
835 Operands.push_back(Op);
836 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000837 Parser.EatToEndOfStatement();
838 return true;
839 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000840
841 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000842 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000843
844 // Parse and remember the operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000845 if (ARMOperand *Op = ParseOperand())
846 Operands.push_back(Op);
847 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000848 Parser.EatToEndOfStatement();
849 return true;
850 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000851 }
852 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000853
Chris Lattnercbf8a982010-09-11 16:18:25 +0000854 if (getLexer().isNot(AsmToken::EndOfStatement)) {
855 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000856 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000857 }
Bill Wendling146018f2010-11-06 21:42:12 +0000858
Chris Lattner34e53142010-09-08 05:10:46 +0000859 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000860 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000861}
862
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000863bool ARMAsmParser::
864MatchAndEmitInstruction(SMLoc IDLoc,
865 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
866 MCStreamer &Out) {
867 MCInst Inst;
868 unsigned ErrorInfo;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000869 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
870 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000871 Out.EmitInstruction(Inst);
872 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000873 case Match_MissingFeature:
874 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
875 return true;
876 case Match_InvalidOperand: {
877 SMLoc ErrorLoc = IDLoc;
878 if (ErrorInfo != ~0U) {
879 if (ErrorInfo >= Operands.size())
880 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +0000881
Chris Lattnere73d4f82010-10-28 21:41:58 +0000882 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
883 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
884 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000885
Chris Lattnere73d4f82010-10-28 21:41:58 +0000886 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000887 }
Chris Lattnere73d4f82010-10-28 21:41:58 +0000888 case Match_MnemonicFail:
889 return Error(IDLoc, "unrecognized instruction mnemonic");
890 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000891
Eric Christopherc223e2b2010-10-29 09:26:59 +0000892 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +0000893 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000894}
895
Kevin Enderby515d5092009-10-15 20:48:48 +0000896/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000897bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
898 StringRef IDVal = DirectiveID.getIdentifier();
899 if (IDVal == ".word")
900 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000901 else if (IDVal == ".thumb")
902 return ParseDirectiveThumb(DirectiveID.getLoc());
903 else if (IDVal == ".thumb_func")
904 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
905 else if (IDVal == ".code")
906 return ParseDirectiveCode(DirectiveID.getLoc());
907 else if (IDVal == ".syntax")
908 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000909 return true;
910}
911
912/// ParseDirectiveWord
913/// ::= .word [ expression (, expression)* ]
914bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
915 if (getLexer().isNot(AsmToken::EndOfStatement)) {
916 for (;;) {
917 const MCExpr *Value;
918 if (getParser().ParseExpression(Value))
919 return true;
920
Chris Lattneraaec2052010-01-19 19:46:13 +0000921 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000922
923 if (getLexer().is(AsmToken::EndOfStatement))
924 break;
Jim Grosbach16c74252010-10-29 14:46:02 +0000925
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000926 // FIXME: Improve diagnostic.
927 if (getLexer().isNot(AsmToken::Comma))
928 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000929 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000930 }
931 }
932
Sean Callananb9a25b72010-01-19 20:27:46 +0000933 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000934 return false;
935}
936
Kevin Enderby515d5092009-10-15 20:48:48 +0000937/// ParseDirectiveThumb
938/// ::= .thumb
939bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
940 if (getLexer().isNot(AsmToken::EndOfStatement))
941 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000942 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000943
944 // TODO: set thumb mode
945 // TODO: tell the MC streamer the mode
946 // getParser().getStreamer().Emit???();
947 return false;
948}
949
950/// ParseDirectiveThumbFunc
951/// ::= .thumbfunc symbol_name
952bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000953 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000954 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +0000955 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000956 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +0000957 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000958 if (getLexer().isNot(AsmToken::EndOfStatement))
959 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000960 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000961
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000962 // Mark symbol as a thumb symbol.
963 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
964 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +0000965 return false;
966}
967
968/// ParseDirectiveSyntax
969/// ::= .syntax unified | divided
970bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000971 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000972 if (Tok.isNot(AsmToken::Identifier))
973 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000974 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000975 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000976 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000977 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000978 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000979 else
980 return Error(L, "unrecognized syntax mode in .syntax directive");
981
982 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000983 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000984 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000985
986 // TODO tell the MC streamer the mode
987 // getParser().getStreamer().Emit???();
988 return false;
989}
990
991/// ParseDirectiveCode
992/// ::= .code 16 | 32
993bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000994 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000995 if (Tok.isNot(AsmToken::Integer))
996 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000997 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +0000998 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +0000999 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001000 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001001 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001002 else
1003 return Error(L, "invalid operand to .code directive");
1004
1005 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001006 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001007 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001008
Jim Grosbach2a301702010-11-05 22:40:53 +00001009 if (Val == 16)
1010 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
1011 else
1012 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1013
Kevin Enderby515d5092009-10-15 20:48:48 +00001014 return false;
1015}
1016
Sean Callanan90b70972010-04-07 20:29:34 +00001017extern "C" void LLVMInitializeARMAsmLexer();
1018
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001019/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001020extern "C" void LLVMInitializeARMAsmParser() {
1021 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1022 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001023 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001024}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001025
Chris Lattner0692ee62010-09-06 19:11:01 +00001026#define GET_REGISTER_MATCHER
1027#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001028#include "ARMGenAsmMatcher.inc"