blob: b07fb6410a0af38cf51e327db9a709906a3c9bf6 [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,
Bill Wendling0f630752010-11-17 04:32:08 +0000110 DPRRegisterList,
111 SPRRegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000112 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000113 } Kind;
114
Sean Callanan76264762010-04-02 22:27:05 +0000115 SMLoc StartLoc, EndLoc;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000116
117 union {
118 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000119 ARMCC::CondCodes Val;
120 } CC;
121
122 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000123 const char *Data;
124 unsigned Length;
125 } Tok;
126
127 struct {
128 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000129 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000130 } Reg;
131
Bill Wendling8155e5b2010-11-06 22:19:43 +0000132 struct {
Bill Wendling5fa22a12010-11-09 23:28:44 +0000133 SmallVector<unsigned, 32> *Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000134 } RegList;
135
Kevin Enderbycfe07242009-10-13 22:19:02 +0000136 struct {
137 const MCExpr *Val;
138 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000139
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000140 // This is for all forms of ARM address expressions
141 struct {
142 unsigned BaseRegNum;
Bill Wendling146018f2010-11-06 21:42:12 +0000143 unsigned OffsetRegNum; // used when OffsetIsReg is true
144 const MCExpr *Offset; // used when OffsetIsReg is false
145 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
146 enum ShiftType ShiftType; // used when OffsetRegShifted is true
147 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
148 unsigned Preindexed : 1;
149 unsigned Postindexed : 1;
150 unsigned OffsetIsReg : 1;
151 unsigned Negative : 1; // only used when OffsetIsReg is true
152 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000153 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000154 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000155
Bill Wendling146018f2010-11-06 21:42:12 +0000156 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
157public:
Sean Callanan76264762010-04-02 22:27:05 +0000158 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
159 Kind = o.Kind;
160 StartLoc = o.StartLoc;
161 EndLoc = o.EndLoc;
162 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000163 case CondCode:
164 CC = o.CC;
165 break;
Sean Callanan76264762010-04-02 22:27:05 +0000166 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000167 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000168 break;
169 case Register:
170 Reg = o.Reg;
171 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000172 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000173 case DPRRegisterList:
174 case SPRRegisterList:
Bill Wendling8d5acb72010-11-06 19:56:04 +0000175 RegList = o.RegList;
176 break;
Sean Callanan76264762010-04-02 22:27:05 +0000177 case Immediate:
178 Imm = o.Imm;
179 break;
180 case Memory:
181 Mem = o.Mem;
182 break;
183 }
184 }
Bill Wendlingc3236752010-11-09 22:51:42 +0000185 ~ARMOperand() {
186 if (isRegList())
187 delete RegList.Registers;
188 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000189
Sean Callanan76264762010-04-02 22:27:05 +0000190 /// getStartLoc - Get the location of the first token of this operand.
191 SMLoc getStartLoc() const { return StartLoc; }
192 /// getEndLoc - Get the location of the last token of this operand.
193 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000194
Daniel Dunbar8462b302010-08-11 06:36:53 +0000195 ARMCC::CondCodes getCondCode() const {
196 assert(Kind == CondCode && "Invalid access!");
197 return CC.Val;
198 }
199
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000200 StringRef getToken() const {
201 assert(Kind == Token && "Invalid access!");
202 return StringRef(Tok.Data, Tok.Length);
203 }
204
205 unsigned getReg() const {
Bill Wendling7729e062010-11-09 22:44:22 +0000206 assert(Kind == Register && "Invalid access!");
207 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000208 }
209
Bill Wendling5fa22a12010-11-09 23:28:44 +0000210 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000211 assert((Kind == RegisterList || Kind == DPRRegisterList ||
212 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000213 return *RegList.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000214 }
215
Kevin Enderbycfe07242009-10-13 22:19:02 +0000216 const MCExpr *getImm() const {
217 assert(Kind == Immediate && "Invalid access!");
218 return Imm.Val;
219 }
220
Daniel Dunbar8462b302010-08-11 06:36:53 +0000221 bool isCondCode() const { return Kind == CondCode; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000222 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000223 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000224 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000225 bool isDPRRegList() const { return Kind == DPRRegisterList; }
226 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000227 bool isToken() const { return Kind == Token; }
228 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000229 bool isMemMode5() const {
230 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
231 Mem.Writeback || Mem.Negative)
232 return false;
233 // If there is an offset expression, make sure it's valid.
234 if (!Mem.Offset)
235 return true;
236 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
237 if (!CE)
238 return false;
239 // The offset must be a multiple of 4 in the range 0-1020.
240 int64_t Value = CE->getValue();
241 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
242 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000243
244 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000245 // Add as immediates when possible. Null MCExpr = 0.
246 if (Expr == 0)
247 Inst.addOperand(MCOperand::CreateImm(0));
248 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000249 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
250 else
251 Inst.addOperand(MCOperand::CreateExpr(Expr));
252 }
253
Daniel Dunbar8462b302010-08-11 06:36:53 +0000254 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000255 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000256 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000257 // FIXME: What belongs here?
258 Inst.addOperand(MCOperand::CreateReg(0));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000259 }
260
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000261 void addRegOperands(MCInst &Inst, unsigned N) const {
262 assert(N == 1 && "Invalid number of operands!");
263 Inst.addOperand(MCOperand::CreateReg(getReg()));
264 }
265
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000266 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000267 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000268 const SmallVectorImpl<unsigned> &RegList = getRegList();
269 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000270 I = RegList.begin(), E = RegList.end(); I != E; ++I)
271 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000272 }
273
Bill Wendling0f630752010-11-17 04:32:08 +0000274 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
275 addRegListOperands(Inst, N);
276 }
277
278 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
279 addRegListOperands(Inst, N);
280 }
281
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000282 void addImmOperands(MCInst &Inst, unsigned N) const {
283 assert(N == 1 && "Invalid number of operands!");
284 addExpr(Inst, getImm());
285 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000286
Chris Lattner14b93852010-10-29 00:27:31 +0000287 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
288 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000289
Chris Lattner14b93852010-10-29 00:27:31 +0000290 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlinga60f1572010-11-06 10:48:18 +0000291 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000292
Jim Grosbach80eb2332010-10-29 17:41:25 +0000293 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
294 // the difference?
295 if (Mem.Offset) {
296 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000297 assert(CE && "Non-constant mode 5 offset operand!");
298
Jim Grosbach80eb2332010-10-29 17:41:25 +0000299 // The MCInst offset operand doesn't include the low two bits (like
300 // the instruction encoding).
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000301 int64_t Offset = CE->getValue() / 4;
302 if (Offset >= 0)
303 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
304 Offset)));
305 else
306 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
307 -Offset)));
308 } else {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000309 Inst.addOperand(MCOperand::CreateImm(0));
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000310 }
Chris Lattner14b93852010-10-29 00:27:31 +0000311 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000312
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000313 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000314
Chris Lattner3a697562010-10-28 17:20:03 +0000315 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
316 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000317 Op->CC.Val = CC;
318 Op->StartLoc = S;
319 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000320 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000321 }
322
Chris Lattner3a697562010-10-28 17:20:03 +0000323 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
324 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000325 Op->Tok.Data = Str.data();
326 Op->Tok.Length = Str.size();
327 Op->StartLoc = S;
328 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000329 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000330 }
331
Chris Lattner3a697562010-10-28 17:20:03 +0000332 static ARMOperand *CreateReg(unsigned RegNum, bool Writeback, SMLoc S,
333 SMLoc E) {
334 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000335 Op->Reg.RegNum = RegNum;
336 Op->Reg.Writeback = Writeback;
Sean Callanan76264762010-04-02 22:27:05 +0000337 Op->StartLoc = S;
338 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000339 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000340 }
341
Bill Wendling7729e062010-11-09 22:44:22 +0000342 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000343 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000344 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000345 KindTy Kind = RegisterList;
346
347 if (ARM::DPRRegClass.contains(Regs.front().first))
348 Kind = DPRRegisterList;
349 else if (ARM::SPRRegClass.contains(Regs.front().first))
350 Kind = SPRRegisterList;
351
352 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000353 Op->RegList.Registers = new SmallVector<unsigned, 32>();
354 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000355 I = Regs.begin(), E = Regs.end(); I != E; ++I)
356 Op->RegList.Registers->push_back(I->first);
357 std::sort(Op->RegList.Registers->begin(), Op->RegList.Registers->end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000358 Op->StartLoc = StartLoc;
359 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000360 return Op;
361 }
362
Chris Lattner3a697562010-10-28 17:20:03 +0000363 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
364 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000365 Op->Imm.Val = Val;
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 Enderbycfe07242009-10-13 22:19:02 +0000369 }
370
Chris Lattner3a697562010-10-28 17:20:03 +0000371 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
372 const MCExpr *Offset, unsigned OffsetRegNum,
373 bool OffsetRegShifted, enum ShiftType ShiftType,
374 const MCExpr *ShiftAmount, bool Preindexed,
375 bool Postindexed, bool Negative, bool Writeback,
376 SMLoc S, SMLoc E) {
377 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000378 Op->Mem.BaseRegNum = BaseRegNum;
379 Op->Mem.OffsetIsReg = OffsetIsReg;
380 Op->Mem.Offset = Offset;
381 Op->Mem.OffsetRegNum = OffsetRegNum;
382 Op->Mem.OffsetRegShifted = OffsetRegShifted;
383 Op->Mem.ShiftType = ShiftType;
384 Op->Mem.ShiftAmount = ShiftAmount;
385 Op->Mem.Preindexed = Preindexed;
386 Op->Mem.Postindexed = Postindexed;
387 Op->Mem.Negative = Negative;
388 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000389
Sean Callanan76264762010-04-02 22:27:05 +0000390 Op->StartLoc = S;
391 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000392 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000393 }
394};
395
396} // end anonymous namespace.
397
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000398void ARMOperand::dump(raw_ostream &OS) const {
399 switch (Kind) {
400 case CondCode:
401 OS << ARMCondCodeToString(getCondCode());
402 break;
403 case Immediate:
404 getImm()->print(OS);
405 break;
406 case Memory:
Bill Wendling8ea97402010-11-10 01:07:54 +0000407 OS << "<memory" << (!Mem.Writeback ? ">" : "!>");
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000408 break;
409 case Register:
Bill Wendling8ea97402010-11-10 01:07:54 +0000410 OS << "<register " << getReg() << (!Reg.Writeback ? ">" : "!>");
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000411 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000412 case RegisterList:
413 case DPRRegisterList:
414 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000415 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000416
Bill Wendling5fa22a12010-11-09 23:28:44 +0000417 const SmallVectorImpl<unsigned> &RegList = getRegList();
418 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000419 I = RegList.begin(), E = RegList.end(); I != E; ) {
420 OS << *I;
421 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000422 }
423
424 OS << ">";
425 break;
426 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000427 case Token:
428 OS << "'" << getToken() << "'";
429 break;
430 }
431}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000432
433/// @name Auto-generated Match Functions
434/// {
435
436static unsigned MatchRegisterName(StringRef Name);
437
438/// }
439
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000440/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000441/// and if it is a register name the token is eaten and the register number is
442/// returned. Otherwise return -1.
443///
444int ARMAsmParser::TryParseRegister() {
445 const AsmToken &Tok = Parser.getTok();
446 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000447
Chris Lattnere5658fa2010-10-30 04:09:10 +0000448 // FIXME: Validate register for the current architecture; we have to do
449 // validation later, so maybe there is no need for this here.
Bill Wendlingd68fd9c2010-11-06 10:45:34 +0000450 unsigned RegNum = MatchRegisterName(Tok.getString());
451 if (RegNum == 0)
Chris Lattnere5658fa2010-10-30 04:09:10 +0000452 return -1;
453 Parser.Lex(); // Eat identifier token.
454 return RegNum;
455}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000456
457
Chris Lattnere5658fa2010-10-30 04:09:10 +0000458/// Try to parse a register name. The token must be an Identifier when called,
459/// and if it is a register name the token is eaten and the register number is
460/// returned. Otherwise return -1.
Chris Lattner3a697562010-10-28 17:20:03 +0000461///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000462/// TODO this is likely to change to allow different register types and or to
463/// parse for a specific register type.
Chris Lattnere5658fa2010-10-30 04:09:10 +0000464ARMOperand *ARMAsmParser::TryParseRegisterWithWriteBack() {
465 SMLoc S = Parser.getTok().getLoc();
466 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000467 if (RegNo == -1)
468 return 0;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000469
Chris Lattnere5658fa2010-10-30 04:09:10 +0000470 SMLoc E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000471
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000472 bool Writeback = false;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000473 const AsmToken &ExclaimTok = Parser.getTok();
474 if (ExclaimTok.is(AsmToken::Exclaim)) {
475 E = ExclaimTok.getLoc();
476 Writeback = true;
477 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000478 }
479
Chris Lattnere5658fa2010-10-30 04:09:10 +0000480 return ARMOperand::CreateReg(RegNo, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000481}
482
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000483/// Parse a register list, return it if successful else return null. The first
484/// token must be a '{' when called.
485ARMOperand *ARMAsmParser::ParseRegisterList() {
Sean Callanan18b83232010-01-19 21:44:56 +0000486 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000487 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000488 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000489
Bill Wendling7729e062010-11-09 22:44:22 +0000490 // Read the rest of the registers in the list.
491 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000492 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000493
Bill Wendling7729e062010-11-09 22:44:22 +0000494 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000495 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000496 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000497
Sean Callanan18b83232010-01-19 21:44:56 +0000498 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000499 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000500 if (RegTok.isNot(AsmToken::Identifier)) {
501 Error(RegLoc, "register expected");
502 return 0;
503 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000504
Bill Wendling1d6a2652010-11-06 10:40:24 +0000505 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000506 if (RegNum == -1) {
507 Error(RegLoc, "register expected");
508 return 0;
509 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000510
Bill Wendlinge7176102010-11-06 22:36:58 +0000511 if (IsRange) {
512 int Reg = PrevRegNum;
513 do {
514 ++Reg;
515 Registers.push_back(std::make_pair(Reg, RegLoc));
516 } while (Reg != RegNum);
517 } else {
518 Registers.push_back(std::make_pair(RegNum, RegLoc));
519 }
520
521 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000522 } while (Parser.getTok().is(AsmToken::Comma) ||
523 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000524
525 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000526 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000527 if (RCurlyTok.isNot(AsmToken::RCurly)) {
528 Error(RCurlyTok.getLoc(), "'}' expected");
529 return 0;
530 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000531
Bill Wendlinge7176102010-11-06 22:36:58 +0000532 SMLoc E = RCurlyTok.getLoc();
533 Parser.Lex(); // Eat right curly brace token.
534
535 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000536 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000537 RI = Registers.begin(), RE = Registers.end();
538
Bill Wendlinge7176102010-11-06 22:36:58 +0000539 DenseMap<unsigned, bool> RegMap;
540 RegMap[RI->first] = true;
541
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000542 unsigned HighRegNum = RI->first;
543 bool EmittedWarning = false;
544
Bill Wendlinge7176102010-11-06 22:36:58 +0000545 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000546 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000547 unsigned Reg = RegInfo.first;
Bill Wendlinge7176102010-11-06 22:36:58 +0000548
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000549 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000550 Error(RegInfo.second, "register duplicated in register list");
551 return 0;
552 }
553
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000554 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000555 Warning(RegInfo.second,
556 "register not in ascending order in register list");
557
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000558 RegMap[Reg] = true;
559 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000560 }
561
Bill Wendling7729e062010-11-09 22:44:22 +0000562 return ARMOperand::CreateRegList(Registers, S, E);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000563}
564
Bill Wendlinge7176102010-11-06 22:36:58 +0000565/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000566/// or an error. The first token must be a '[' when called.
567/// TODO Only preindexing and postindexing addressing are started, unindexed
568/// with option, etc are still to do.
Chris Lattner550276e2010-10-28 20:52:15 +0000569ARMOperand *ARMAsmParser::ParseMemory() {
Sean Callanan76264762010-04-02 22:27:05 +0000570 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000571 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000572 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000573 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000574 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000575
Sean Callanan18b83232010-01-19 21:44:56 +0000576 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000577 if (BaseRegTok.isNot(AsmToken::Identifier)) {
578 Error(BaseRegTok.getLoc(), "register expected");
579 return 0;
580 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000581 int BaseRegNum = TryParseRegister();
582 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000583 Error(BaseRegTok.getLoc(), "register expected");
584 return 0;
585 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000586
587 bool Preindexed = false;
588 bool Postindexed = false;
589 bool OffsetIsReg = false;
590 bool Negative = false;
591 bool Writeback = false;
592
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000593 // First look for preindexed address forms, that is after the "[Rn" we now
594 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000595 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000596 if (Tok.is(AsmToken::Comma)) {
597 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000598 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000599 int OffsetRegNum;
600 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000601 enum ShiftType ShiftType;
602 const MCExpr *ShiftAmount;
603 const MCExpr *Offset;
Chris Lattner550276e2010-10-28 20:52:15 +0000604 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
605 Offset, OffsetIsReg, OffsetRegNum, E))
606 return 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000607 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000608 if (RBracTok.isNot(AsmToken::RBrac)) {
609 Error(RBracTok.getLoc(), "']' expected");
610 return 0;
611 }
Sean Callanan76264762010-04-02 22:27:05 +0000612 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000613 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000614
Sean Callanan18b83232010-01-19 21:44:56 +0000615 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000616 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000617 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000618 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000619 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000620 }
Chris Lattner550276e2010-10-28 20:52:15 +0000621 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
622 OffsetRegShifted, ShiftType, ShiftAmount,
623 Preindexed, Postindexed, Negative, Writeback,
624 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000625 }
626 // The "[Rn" we have so far was not followed by a comma.
627 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000628 // If there's anything other than the right brace, this is a post indexing
629 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000630 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000631 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000632
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000633 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000634 bool OffsetRegShifted = false;
635 enum ShiftType ShiftType;
636 const MCExpr *ShiftAmount;
Chris Lattner14b93852010-10-29 00:27:31 +0000637 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000638
Sean Callanan18b83232010-01-19 21:44:56 +0000639 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000640 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000641 Postindexed = true;
642 Writeback = true;
Chris Lattner550276e2010-10-28 20:52:15 +0000643 if (NextTok.isNot(AsmToken::Comma)) {
644 Error(NextTok.getLoc(), "',' expected");
645 return 0;
646 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000647 Parser.Lex(); // Eat comma token.
Chris Lattner550276e2010-10-28 20:52:15 +0000648 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000649 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000650 E))
651 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000652 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000653
Chris Lattner550276e2010-10-28 20:52:15 +0000654 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
655 OffsetRegShifted, ShiftType, ShiftAmount,
656 Preindexed, Postindexed, Negative, Writeback,
657 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000658 }
659
Chris Lattner550276e2010-10-28 20:52:15 +0000660 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000661}
662
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000663/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
664/// we will parse the following (were +/- means that a plus or minus is
665/// optional):
666/// +/-Rm
667/// +/-Rm, shift
668/// #offset
669/// we return false on success or an error otherwise.
670bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000671 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000672 enum ShiftType &ShiftType,
673 const MCExpr *&ShiftAmount,
674 const MCExpr *&Offset,
675 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000676 int &OffsetRegNum,
677 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000678 Negative = false;
679 OffsetRegShifted = false;
680 OffsetIsReg = false;
681 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000682 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000683 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000684 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000685 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000686 else if (NextTok.is(AsmToken::Minus)) {
687 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000688 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000689 }
690 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000691 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000692 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000693 SMLoc CurLoc = OffsetRegTok.getLoc();
694 OffsetRegNum = TryParseRegister();
695 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000696 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000697 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000698 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000699 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000700
Bill Wendling12f40e92010-11-06 10:51:53 +0000701 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000702 if (OffsetRegNum != -1) {
703 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000704 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000705 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000706 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000707
Sean Callanan18b83232010-01-19 21:44:56 +0000708 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000709 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000710 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000711 OffsetRegShifted = true;
712 }
713 }
714 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
715 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000716 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000717 if (HashTok.isNot(AsmToken::Hash))
718 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000719
Sean Callananb9a25b72010-01-19 20:27:46 +0000720 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000721
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000722 if (getParser().ParseExpression(Offset))
723 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000724 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000725 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000726 return false;
727}
728
729/// ParseShift as one of these two:
730/// ( lsl | lsr | asr | ror ) , # shift_amount
731/// rrx
732/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000733bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000734 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000735 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000736 if (Tok.isNot(AsmToken::Identifier))
737 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000738 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000739 if (ShiftName == "lsl" || ShiftName == "LSL")
740 St = Lsl;
741 else if (ShiftName == "lsr" || ShiftName == "LSR")
742 St = Lsr;
743 else if (ShiftName == "asr" || ShiftName == "ASR")
744 St = Asr;
745 else if (ShiftName == "ror" || ShiftName == "ROR")
746 St = Ror;
747 else if (ShiftName == "rrx" || ShiftName == "RRX")
748 St = Rrx;
749 else
750 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000751 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000752
753 // Rrx stands alone.
754 if (St == Rrx)
755 return false;
756
757 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000758 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000759 if (HashTok.isNot(AsmToken::Hash))
760 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000761 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000762
763 if (getParser().ParseExpression(ShiftAmount))
764 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000765
766 return false;
767}
768
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000769/// Parse a arm instruction operand. For now this parses the operand regardless
770/// of the mnemonic.
Chris Lattner550276e2010-10-28 20:52:15 +0000771ARMOperand *ARMAsmParser::ParseOperand() {
Sean Callanan76264762010-04-02 22:27:05 +0000772 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000773 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000774 default:
775 Error(Parser.getTok().getLoc(), "unexpected token in operand");
776 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000777 case AsmToken::Identifier:
Chris Lattnere5658fa2010-10-30 04:09:10 +0000778 if (ARMOperand *Op = TryParseRegisterWithWriteBack())
Chris Lattner550276e2010-10-28 20:52:15 +0000779 return Op;
Jim Grosbach16c74252010-10-29 14:46:02 +0000780
Kevin Enderby515d5092009-10-15 20:48:48 +0000781 // This was not a register so parse other operands that start with an
782 // identifier (like labels) as expressions and create them as immediates.
783 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000784 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000785 if (getParser().ParseExpression(IdVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000786 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000787 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000788 return ARMOperand::CreateImm(IdVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000789 case AsmToken::LBrac:
Chris Lattner550276e2010-10-28 20:52:15 +0000790 return ParseMemory();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000791 case AsmToken::LCurly:
Chris Lattner550276e2010-10-28 20:52:15 +0000792 return ParseRegisterList();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000793 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000794 // #42 -> immediate.
795 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000796 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000797 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000798 const MCExpr *ImmVal;
799 if (getParser().ParseExpression(ImmVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000800 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000801 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000802 return ARMOperand::CreateImm(ImmVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000803 }
804}
805
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000806/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000807bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000808 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000809 // Create the leading tokens for the mnemonic, split by '.' characters.
810 size_t Start = 0, Next = Name.find('.');
811 StringRef Head = Name.slice(Start, Next);
812
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000813 // Determine the predicate, if any.
814 //
815 // FIXME: We need a way to check whether a prefix supports predication,
816 // otherwise we will end up with an ambiguity for instructions that happen to
817 // end with a predicate name.
Jim Grosbach3df518e2010-10-29 21:56:51 +0000818 // FIXME: Likewise, some arithmetic instructions have an 's' prefix which
819 // indicates to update the condition codes. Those instructions have an
820 // additional immediate operand which encodes the prefix as reg0 or CPSR.
821 // Just checking for a suffix of 's' definitely creates ambiguities; e.g,
822 // the SMMLS instruction.
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000823 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
824 .Case("eq", ARMCC::EQ)
825 .Case("ne", ARMCC::NE)
826 .Case("hs", ARMCC::HS)
827 .Case("lo", ARMCC::LO)
828 .Case("mi", ARMCC::MI)
829 .Case("pl", ARMCC::PL)
830 .Case("vs", ARMCC::VS)
831 .Case("vc", ARMCC::VC)
832 .Case("hi", ARMCC::HI)
833 .Case("ls", ARMCC::LS)
834 .Case("ge", ARMCC::GE)
835 .Case("lt", ARMCC::LT)
836 .Case("gt", ARMCC::GT)
837 .Case("le", ARMCC::LE)
838 .Case("al", ARMCC::AL)
839 .Default(~0U);
Jim Grosbach16c74252010-10-29 14:46:02 +0000840
Chris Lattnerdba34d82010-10-30 04:35:59 +0000841 if (CC == ~0U ||
842 (CC == ARMCC::LS && (Head == "vmls" || Head == "vnmls"))) {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000843 CC = ARMCC::AL;
Chris Lattnerdba34d82010-10-30 04:35:59 +0000844 } else {
845 Head = Head.slice(0, Head.size() - 2);
Bill Wendling52925b62010-10-29 23:50:21 +0000846 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000847
Chris Lattner3a697562010-10-28 17:20:03 +0000848 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Jim Grosbach469ebbe2010-11-01 18:11:14 +0000849 // FIXME: Should only add this operand for predicated instructions
Chris Lattner3a697562010-10-28 17:20:03 +0000850 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000851
852 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000853 while (Next != StringRef::npos) {
854 Start = Next;
855 Next = Name.find('.', Start + 1);
856 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000857
Chris Lattner3a697562010-10-28 17:20:03 +0000858 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000859 }
860
861 // Read the remaining operands.
862 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000863 // Read the first operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000864 if (ARMOperand *Op = ParseOperand())
865 Operands.push_back(Op);
866 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000867 Parser.EatToEndOfStatement();
868 return true;
869 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000870
871 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000872 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000873
874 // Parse and remember the operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000875 if (ARMOperand *Op = ParseOperand())
876 Operands.push_back(Op);
877 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000878 Parser.EatToEndOfStatement();
879 return true;
880 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000881 }
882 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000883
Chris Lattnercbf8a982010-09-11 16:18:25 +0000884 if (getLexer().isNot(AsmToken::EndOfStatement)) {
885 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000886 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000887 }
Bill Wendling146018f2010-11-06 21:42:12 +0000888
Chris Lattner34e53142010-09-08 05:10:46 +0000889 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000890 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000891}
892
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000893bool ARMAsmParser::
894MatchAndEmitInstruction(SMLoc IDLoc,
895 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
896 MCStreamer &Out) {
897 MCInst Inst;
898 unsigned ErrorInfo;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000899 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
900 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000901 Out.EmitInstruction(Inst);
902 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000903 case Match_MissingFeature:
904 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
905 return true;
906 case Match_InvalidOperand: {
907 SMLoc ErrorLoc = IDLoc;
908 if (ErrorInfo != ~0U) {
909 if (ErrorInfo >= Operands.size())
910 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +0000911
Chris Lattnere73d4f82010-10-28 21:41:58 +0000912 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
913 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
914 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000915
Chris Lattnere73d4f82010-10-28 21:41:58 +0000916 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000917 }
Chris Lattnere73d4f82010-10-28 21:41:58 +0000918 case Match_MnemonicFail:
919 return Error(IDLoc, "unrecognized instruction mnemonic");
920 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000921
Eric Christopherc223e2b2010-10-29 09:26:59 +0000922 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +0000923 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000924}
925
Kevin Enderby515d5092009-10-15 20:48:48 +0000926/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000927bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
928 StringRef IDVal = DirectiveID.getIdentifier();
929 if (IDVal == ".word")
930 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000931 else if (IDVal == ".thumb")
932 return ParseDirectiveThumb(DirectiveID.getLoc());
933 else if (IDVal == ".thumb_func")
934 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
935 else if (IDVal == ".code")
936 return ParseDirectiveCode(DirectiveID.getLoc());
937 else if (IDVal == ".syntax")
938 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000939 return true;
940}
941
942/// ParseDirectiveWord
943/// ::= .word [ expression (, expression)* ]
944bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
945 if (getLexer().isNot(AsmToken::EndOfStatement)) {
946 for (;;) {
947 const MCExpr *Value;
948 if (getParser().ParseExpression(Value))
949 return true;
950
Chris Lattneraaec2052010-01-19 19:46:13 +0000951 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000952
953 if (getLexer().is(AsmToken::EndOfStatement))
954 break;
Jim Grosbach16c74252010-10-29 14:46:02 +0000955
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000956 // FIXME: Improve diagnostic.
957 if (getLexer().isNot(AsmToken::Comma))
958 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000959 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000960 }
961 }
962
Sean Callananb9a25b72010-01-19 20:27:46 +0000963 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000964 return false;
965}
966
Kevin Enderby515d5092009-10-15 20:48:48 +0000967/// ParseDirectiveThumb
968/// ::= .thumb
969bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
970 if (getLexer().isNot(AsmToken::EndOfStatement))
971 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000972 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000973
974 // TODO: set thumb mode
975 // TODO: tell the MC streamer the mode
976 // getParser().getStreamer().Emit???();
977 return false;
978}
979
980/// ParseDirectiveThumbFunc
981/// ::= .thumbfunc symbol_name
982bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000983 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000984 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +0000985 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000986 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +0000987 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000988 if (getLexer().isNot(AsmToken::EndOfStatement))
989 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000990 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000991
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000992 // Mark symbol as a thumb symbol.
993 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
994 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +0000995 return false;
996}
997
998/// ParseDirectiveSyntax
999/// ::= .syntax unified | divided
1000bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001001 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001002 if (Tok.isNot(AsmToken::Identifier))
1003 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001004 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001005 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001006 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001007 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001008 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001009 else
1010 return Error(L, "unrecognized syntax mode in .syntax directive");
1011
1012 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001013 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001014 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001015
1016 // TODO tell the MC streamer the mode
1017 // getParser().getStreamer().Emit???();
1018 return false;
1019}
1020
1021/// ParseDirectiveCode
1022/// ::= .code 16 | 32
1023bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001024 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001025 if (Tok.isNot(AsmToken::Integer))
1026 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001027 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001028 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001029 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001030 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001031 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001032 else
1033 return Error(L, "invalid operand to .code directive");
1034
1035 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001036 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001037 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001038
Jim Grosbach2a301702010-11-05 22:40:53 +00001039 if (Val == 16)
1040 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
1041 else
1042 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1043
Kevin Enderby515d5092009-10-15 20:48:48 +00001044 return false;
1045}
1046
Sean Callanan90b70972010-04-07 20:29:34 +00001047extern "C" void LLVMInitializeARMAsmLexer();
1048
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001049/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001050extern "C" void LLVMInitializeARMAsmParser() {
1051 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1052 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001053 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001054}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001055
Chris Lattner0692ee62010-09-06 19:11:01 +00001056#define GET_REGISTER_MATCHER
1057#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001058#include "ARMGenAsmMatcher.inc"