blob: b5f2e12e1c5d2a87a94b69c58174c6f67e532644 [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;
Bill Wendling24d22d22010-11-18 21:50:54 +0000116 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000117
118 union {
119 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000120 ARMCC::CondCodes Val;
121 } CC;
122
123 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000124 const char *Data;
125 unsigned Length;
126 } Tok;
127
128 struct {
129 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000130 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000131 } Reg;
132
Bill Wendling8155e5b2010-11-06 22:19:43 +0000133 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000134 const MCExpr *Val;
135 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000136
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000137 // This is for all forms of ARM address expressions
138 struct {
139 unsigned BaseRegNum;
Bill Wendling146018f2010-11-06 21:42:12 +0000140 unsigned OffsetRegNum; // used when OffsetIsReg is true
141 const MCExpr *Offset; // used when OffsetIsReg is false
142 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
143 enum ShiftType ShiftType; // used when OffsetRegShifted is true
144 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
145 unsigned Preindexed : 1;
146 unsigned Postindexed : 1;
147 unsigned OffsetIsReg : 1;
148 unsigned Negative : 1; // only used when OffsetIsReg is true
149 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000150 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000151 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000152
Bill Wendling146018f2010-11-06 21:42:12 +0000153 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
154public:
Sean Callanan76264762010-04-02 22:27:05 +0000155 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
156 Kind = o.Kind;
157 StartLoc = o.StartLoc;
158 EndLoc = o.EndLoc;
159 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000160 case CondCode:
161 CC = o.CC;
162 break;
Sean Callanan76264762010-04-02 22:27:05 +0000163 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000164 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000165 break;
166 case Register:
167 Reg = o.Reg;
168 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000169 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000170 case DPRRegisterList:
171 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000172 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000173 break;
Sean Callanan76264762010-04-02 22:27:05 +0000174 case Immediate:
175 Imm = o.Imm;
176 break;
177 case Memory:
178 Mem = o.Mem;
179 break;
180 }
181 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000182
Sean Callanan76264762010-04-02 22:27:05 +0000183 /// getStartLoc - Get the location of the first token of this operand.
184 SMLoc getStartLoc() const { return StartLoc; }
185 /// getEndLoc - Get the location of the last token of this operand.
186 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000187
Daniel Dunbar8462b302010-08-11 06:36:53 +0000188 ARMCC::CondCodes getCondCode() const {
189 assert(Kind == CondCode && "Invalid access!");
190 return CC.Val;
191 }
192
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000193 StringRef getToken() const {
194 assert(Kind == Token && "Invalid access!");
195 return StringRef(Tok.Data, Tok.Length);
196 }
197
198 unsigned getReg() const {
Bill Wendling7729e062010-11-09 22:44:22 +0000199 assert(Kind == Register && "Invalid access!");
200 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000201 }
202
Bill Wendling5fa22a12010-11-09 23:28:44 +0000203 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000204 assert((Kind == RegisterList || Kind == DPRRegisterList ||
205 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000206 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000207 }
208
Kevin Enderbycfe07242009-10-13 22:19:02 +0000209 const MCExpr *getImm() const {
210 assert(Kind == Immediate && "Invalid access!");
211 return Imm.Val;
212 }
213
Daniel Dunbar8462b302010-08-11 06:36:53 +0000214 bool isCondCode() const { return Kind == CondCode; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000215 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000216 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000217 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000218 bool isDPRRegList() const { return Kind == DPRRegisterList; }
219 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000220 bool isToken() const { return Kind == Token; }
221 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000222 bool isMemMode5() const {
223 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
224 Mem.Writeback || Mem.Negative)
225 return false;
226 // If there is an offset expression, make sure it's valid.
227 if (!Mem.Offset)
228 return true;
229 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
230 if (!CE)
231 return false;
232 // The offset must be a multiple of 4 in the range 0-1020.
233 int64_t Value = CE->getValue();
234 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
235 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000236
237 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000238 // Add as immediates when possible. Null MCExpr = 0.
239 if (Expr == 0)
240 Inst.addOperand(MCOperand::CreateImm(0));
241 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000242 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
243 else
244 Inst.addOperand(MCOperand::CreateExpr(Expr));
245 }
246
Daniel Dunbar8462b302010-08-11 06:36:53 +0000247 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000248 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000249 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000250 // FIXME: What belongs here?
251 Inst.addOperand(MCOperand::CreateReg(0));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000252 }
253
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000254 void addRegOperands(MCInst &Inst, unsigned N) const {
255 assert(N == 1 && "Invalid number of operands!");
256 Inst.addOperand(MCOperand::CreateReg(getReg()));
257 }
258
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000259 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000260 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000261 const SmallVectorImpl<unsigned> &RegList = getRegList();
262 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000263 I = RegList.begin(), E = RegList.end(); I != E; ++I)
264 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000265 }
266
Bill Wendling0f630752010-11-17 04:32:08 +0000267 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
268 addRegListOperands(Inst, N);
269 }
270
271 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
272 addRegListOperands(Inst, N);
273 }
274
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000275 void addImmOperands(MCInst &Inst, unsigned N) const {
276 assert(N == 1 && "Invalid number of operands!");
277 addExpr(Inst, getImm());
278 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000279
Chris Lattner14b93852010-10-29 00:27:31 +0000280 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
281 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000282
Chris Lattner14b93852010-10-29 00:27:31 +0000283 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlinga60f1572010-11-06 10:48:18 +0000284 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000285
Jim Grosbach80eb2332010-10-29 17:41:25 +0000286 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
287 // the difference?
288 if (Mem.Offset) {
289 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000290 assert(CE && "Non-constant mode 5 offset operand!");
291
Jim Grosbach80eb2332010-10-29 17:41:25 +0000292 // The MCInst offset operand doesn't include the low two bits (like
293 // the instruction encoding).
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000294 int64_t Offset = CE->getValue() / 4;
295 if (Offset >= 0)
296 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
297 Offset)));
298 else
299 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
300 -Offset)));
301 } else {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000302 Inst.addOperand(MCOperand::CreateImm(0));
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000303 }
Chris Lattner14b93852010-10-29 00:27:31 +0000304 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000305
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000306 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000307
Chris Lattner3a697562010-10-28 17:20:03 +0000308 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
309 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000310 Op->CC.Val = CC;
311 Op->StartLoc = S;
312 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000313 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000314 }
315
Chris Lattner3a697562010-10-28 17:20:03 +0000316 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
317 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000318 Op->Tok.Data = Str.data();
319 Op->Tok.Length = Str.size();
320 Op->StartLoc = S;
321 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000322 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000323 }
324
Chris Lattner3a697562010-10-28 17:20:03 +0000325 static ARMOperand *CreateReg(unsigned RegNum, bool Writeback, SMLoc S,
326 SMLoc E) {
327 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000328 Op->Reg.RegNum = RegNum;
329 Op->Reg.Writeback = Writeback;
Sean Callanan76264762010-04-02 22:27:05 +0000330 Op->StartLoc = S;
331 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000332 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000333 }
334
Bill Wendling7729e062010-11-09 22:44:22 +0000335 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000336 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000337 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000338 KindTy Kind = RegisterList;
339
340 if (ARM::DPRRegClass.contains(Regs.front().first))
341 Kind = DPRRegisterList;
342 else if (ARM::SPRRegClass.contains(Regs.front().first))
343 Kind = SPRRegisterList;
344
345 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000346 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000347 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000348 Op->Registers.push_back(I->first);
349 std::sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000350 Op->StartLoc = StartLoc;
351 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000352 return Op;
353 }
354
Chris Lattner3a697562010-10-28 17:20:03 +0000355 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
356 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000357 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000358 Op->StartLoc = S;
359 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000360 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000361 }
362
Chris Lattner3a697562010-10-28 17:20:03 +0000363 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
364 const MCExpr *Offset, unsigned OffsetRegNum,
365 bool OffsetRegShifted, enum ShiftType ShiftType,
366 const MCExpr *ShiftAmount, bool Preindexed,
367 bool Postindexed, bool Negative, bool Writeback,
368 SMLoc S, SMLoc E) {
369 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000370 Op->Mem.BaseRegNum = BaseRegNum;
371 Op->Mem.OffsetIsReg = OffsetIsReg;
372 Op->Mem.Offset = Offset;
373 Op->Mem.OffsetRegNum = OffsetRegNum;
374 Op->Mem.OffsetRegShifted = OffsetRegShifted;
375 Op->Mem.ShiftType = ShiftType;
376 Op->Mem.ShiftAmount = ShiftAmount;
377 Op->Mem.Preindexed = Preindexed;
378 Op->Mem.Postindexed = Postindexed;
379 Op->Mem.Negative = Negative;
380 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000381
Sean Callanan76264762010-04-02 22:27:05 +0000382 Op->StartLoc = S;
383 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000384 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000385 }
386};
387
388} // end anonymous namespace.
389
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000390void ARMOperand::dump(raw_ostream &OS) const {
391 switch (Kind) {
392 case CondCode:
393 OS << ARMCondCodeToString(getCondCode());
394 break;
395 case Immediate:
396 getImm()->print(OS);
397 break;
398 case Memory:
Bill Wendling8ea97402010-11-10 01:07:54 +0000399 OS << "<memory" << (!Mem.Writeback ? ">" : "!>");
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000400 break;
401 case Register:
Bill Wendling8ea97402010-11-10 01:07:54 +0000402 OS << "<register " << getReg() << (!Reg.Writeback ? ">" : "!>");
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000403 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000404 case RegisterList:
405 case DPRRegisterList:
406 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000407 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000408
Bill Wendling5fa22a12010-11-09 23:28:44 +0000409 const SmallVectorImpl<unsigned> &RegList = getRegList();
410 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000411 I = RegList.begin(), E = RegList.end(); I != E; ) {
412 OS << *I;
413 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000414 }
415
416 OS << ">";
417 break;
418 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000419 case Token:
420 OS << "'" << getToken() << "'";
421 break;
422 }
423}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000424
425/// @name Auto-generated Match Functions
426/// {
427
428static unsigned MatchRegisterName(StringRef Name);
429
430/// }
431
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000432/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000433/// and if it is a register name the token is eaten and the register number is
434/// returned. Otherwise return -1.
435///
436int ARMAsmParser::TryParseRegister() {
437 const AsmToken &Tok = Parser.getTok();
438 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000439
Chris Lattnere5658fa2010-10-30 04:09:10 +0000440 // FIXME: Validate register for the current architecture; we have to do
441 // validation later, so maybe there is no need for this here.
Bill Wendlingd68fd9c2010-11-06 10:45:34 +0000442 unsigned RegNum = MatchRegisterName(Tok.getString());
443 if (RegNum == 0)
Chris Lattnere5658fa2010-10-30 04:09:10 +0000444 return -1;
445 Parser.Lex(); // Eat identifier token.
446 return RegNum;
447}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000448
449
Chris Lattnere5658fa2010-10-30 04:09:10 +0000450/// Try to parse a register name. The token must be an Identifier when called,
451/// and if it is a register name the token is eaten and the register number is
452/// returned. Otherwise return -1.
Chris Lattner3a697562010-10-28 17:20:03 +0000453///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000454/// TODO this is likely to change to allow different register types and or to
455/// parse for a specific register type.
Chris Lattnere5658fa2010-10-30 04:09:10 +0000456ARMOperand *ARMAsmParser::TryParseRegisterWithWriteBack() {
457 SMLoc S = Parser.getTok().getLoc();
458 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000459 if (RegNo == -1)
460 return 0;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000461
Chris Lattnere5658fa2010-10-30 04:09:10 +0000462 SMLoc E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000463
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000464 bool Writeback = false;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000465 const AsmToken &ExclaimTok = Parser.getTok();
466 if (ExclaimTok.is(AsmToken::Exclaim)) {
467 E = ExclaimTok.getLoc();
468 Writeback = true;
469 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000470 }
471
Chris Lattnere5658fa2010-10-30 04:09:10 +0000472 return ARMOperand::CreateReg(RegNo, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000473}
474
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000475/// Parse a register list, return it if successful else return null. The first
476/// token must be a '{' when called.
477ARMOperand *ARMAsmParser::ParseRegisterList() {
Sean Callanan18b83232010-01-19 21:44:56 +0000478 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000479 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000480 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000481
Bill Wendling7729e062010-11-09 22:44:22 +0000482 // Read the rest of the registers in the list.
483 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000484 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000485
Bill Wendling7729e062010-11-09 22:44:22 +0000486 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000487 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000488 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000489
Sean Callanan18b83232010-01-19 21:44:56 +0000490 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000491 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000492 if (RegTok.isNot(AsmToken::Identifier)) {
493 Error(RegLoc, "register expected");
494 return 0;
495 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000496
Bill Wendling1d6a2652010-11-06 10:40:24 +0000497 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000498 if (RegNum == -1) {
499 Error(RegLoc, "register expected");
500 return 0;
501 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000502
Bill Wendlinge7176102010-11-06 22:36:58 +0000503 if (IsRange) {
504 int Reg = PrevRegNum;
505 do {
506 ++Reg;
507 Registers.push_back(std::make_pair(Reg, RegLoc));
508 } while (Reg != RegNum);
509 } else {
510 Registers.push_back(std::make_pair(RegNum, RegLoc));
511 }
512
513 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000514 } while (Parser.getTok().is(AsmToken::Comma) ||
515 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000516
517 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000518 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000519 if (RCurlyTok.isNot(AsmToken::RCurly)) {
520 Error(RCurlyTok.getLoc(), "'}' expected");
521 return 0;
522 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000523
Bill Wendlinge7176102010-11-06 22:36:58 +0000524 SMLoc E = RCurlyTok.getLoc();
525 Parser.Lex(); // Eat right curly brace token.
526
527 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000528 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000529 RI = Registers.begin(), RE = Registers.end();
530
Bill Wendlinge7176102010-11-06 22:36:58 +0000531 DenseMap<unsigned, bool> RegMap;
532 RegMap[RI->first] = true;
533
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000534 unsigned HighRegNum = RI->first;
535 bool EmittedWarning = false;
536
Bill Wendlinge7176102010-11-06 22:36:58 +0000537 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000538 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000539 unsigned Reg = RegInfo.first;
Bill Wendlinge7176102010-11-06 22:36:58 +0000540
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000541 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000542 Error(RegInfo.second, "register duplicated in register list");
543 return 0;
544 }
545
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000546 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000547 Warning(RegInfo.second,
548 "register not in ascending order in register list");
549
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000550 RegMap[Reg] = true;
551 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000552 }
553
Bill Wendling7729e062010-11-09 22:44:22 +0000554 return ARMOperand::CreateRegList(Registers, S, E);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000555}
556
Bill Wendlinge7176102010-11-06 22:36:58 +0000557/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000558/// or an error. The first token must be a '[' when called.
559/// TODO Only preindexing and postindexing addressing are started, unindexed
560/// with option, etc are still to do.
Chris Lattner550276e2010-10-28 20:52:15 +0000561ARMOperand *ARMAsmParser::ParseMemory() {
Sean Callanan76264762010-04-02 22:27:05 +0000562 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000563 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000564 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000565 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000566 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000567
Sean Callanan18b83232010-01-19 21:44:56 +0000568 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000569 if (BaseRegTok.isNot(AsmToken::Identifier)) {
570 Error(BaseRegTok.getLoc(), "register expected");
571 return 0;
572 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000573 int BaseRegNum = TryParseRegister();
574 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000575 Error(BaseRegTok.getLoc(), "register expected");
576 return 0;
577 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000578
579 bool Preindexed = false;
580 bool Postindexed = false;
581 bool OffsetIsReg = false;
582 bool Negative = false;
583 bool Writeback = false;
584
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000585 // First look for preindexed address forms, that is after the "[Rn" we now
586 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000587 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000588 if (Tok.is(AsmToken::Comma)) {
589 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000590 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000591 int OffsetRegNum;
592 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000593 enum ShiftType ShiftType;
594 const MCExpr *ShiftAmount;
595 const MCExpr *Offset;
Chris Lattner550276e2010-10-28 20:52:15 +0000596 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
597 Offset, OffsetIsReg, OffsetRegNum, E))
598 return 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000599 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000600 if (RBracTok.isNot(AsmToken::RBrac)) {
601 Error(RBracTok.getLoc(), "']' expected");
602 return 0;
603 }
Sean Callanan76264762010-04-02 22:27:05 +0000604 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000605 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000606
Sean Callanan18b83232010-01-19 21:44:56 +0000607 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000608 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000609 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000610 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000611 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000612 }
Chris Lattner550276e2010-10-28 20:52:15 +0000613 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
614 OffsetRegShifted, ShiftType, ShiftAmount,
615 Preindexed, Postindexed, Negative, Writeback,
616 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000617 }
618 // The "[Rn" we have so far was not followed by a comma.
619 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000620 // If there's anything other than the right brace, this is a post indexing
621 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000622 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000623 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000624
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000625 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000626 bool OffsetRegShifted = false;
627 enum ShiftType ShiftType;
628 const MCExpr *ShiftAmount;
Chris Lattner14b93852010-10-29 00:27:31 +0000629 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000630
Sean Callanan18b83232010-01-19 21:44:56 +0000631 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000632 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000633 Postindexed = true;
634 Writeback = true;
Chris Lattner550276e2010-10-28 20:52:15 +0000635 if (NextTok.isNot(AsmToken::Comma)) {
636 Error(NextTok.getLoc(), "',' expected");
637 return 0;
638 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000639 Parser.Lex(); // Eat comma token.
Chris Lattner550276e2010-10-28 20:52:15 +0000640 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000641 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000642 E))
643 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000644 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000645
Chris Lattner550276e2010-10-28 20:52:15 +0000646 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
647 OffsetRegShifted, ShiftType, ShiftAmount,
648 Preindexed, Postindexed, Negative, Writeback,
649 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000650 }
651
Chris Lattner550276e2010-10-28 20:52:15 +0000652 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000653}
654
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000655/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
656/// we will parse the following (were +/- means that a plus or minus is
657/// optional):
658/// +/-Rm
659/// +/-Rm, shift
660/// #offset
661/// we return false on success or an error otherwise.
662bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000663 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000664 enum ShiftType &ShiftType,
665 const MCExpr *&ShiftAmount,
666 const MCExpr *&Offset,
667 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000668 int &OffsetRegNum,
669 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000670 Negative = false;
671 OffsetRegShifted = false;
672 OffsetIsReg = false;
673 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000674 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000675 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000676 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000677 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000678 else if (NextTok.is(AsmToken::Minus)) {
679 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000680 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000681 }
682 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000683 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000684 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000685 SMLoc CurLoc = OffsetRegTok.getLoc();
686 OffsetRegNum = TryParseRegister();
687 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000688 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000689 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000690 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000691 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000692
Bill Wendling12f40e92010-11-06 10:51:53 +0000693 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000694 if (OffsetRegNum != -1) {
695 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000696 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000697 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000698 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000699
Sean Callanan18b83232010-01-19 21:44:56 +0000700 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000701 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000702 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000703 OffsetRegShifted = true;
704 }
705 }
706 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
707 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000708 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000709 if (HashTok.isNot(AsmToken::Hash))
710 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000711
Sean Callananb9a25b72010-01-19 20:27:46 +0000712 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000713
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000714 if (getParser().ParseExpression(Offset))
715 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000716 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000717 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000718 return false;
719}
720
721/// ParseShift as one of these two:
722/// ( lsl | lsr | asr | ror ) , # shift_amount
723/// rrx
724/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000725bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000726 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000727 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000728 if (Tok.isNot(AsmToken::Identifier))
729 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000730 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000731 if (ShiftName == "lsl" || ShiftName == "LSL")
732 St = Lsl;
733 else if (ShiftName == "lsr" || ShiftName == "LSR")
734 St = Lsr;
735 else if (ShiftName == "asr" || ShiftName == "ASR")
736 St = Asr;
737 else if (ShiftName == "ror" || ShiftName == "ROR")
738 St = Ror;
739 else if (ShiftName == "rrx" || ShiftName == "RRX")
740 St = Rrx;
741 else
742 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000743 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000744
745 // Rrx stands alone.
746 if (St == Rrx)
747 return false;
748
749 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000750 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000751 if (HashTok.isNot(AsmToken::Hash))
752 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000753 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000754
755 if (getParser().ParseExpression(ShiftAmount))
756 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000757
758 return false;
759}
760
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000761/// Parse a arm instruction operand. For now this parses the operand regardless
762/// of the mnemonic.
Chris Lattner550276e2010-10-28 20:52:15 +0000763ARMOperand *ARMAsmParser::ParseOperand() {
Sean Callanan76264762010-04-02 22:27:05 +0000764 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000765 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000766 default:
767 Error(Parser.getTok().getLoc(), "unexpected token in operand");
768 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000769 case AsmToken::Identifier:
Chris Lattnere5658fa2010-10-30 04:09:10 +0000770 if (ARMOperand *Op = TryParseRegisterWithWriteBack())
Chris Lattner550276e2010-10-28 20:52:15 +0000771 return Op;
Jim Grosbach16c74252010-10-29 14:46:02 +0000772
Kevin Enderby515d5092009-10-15 20:48:48 +0000773 // This was not a register so parse other operands that start with an
774 // identifier (like labels) as expressions and create them as immediates.
775 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000776 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000777 if (getParser().ParseExpression(IdVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000778 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000779 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000780 return ARMOperand::CreateImm(IdVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000781 case AsmToken::LBrac:
Chris Lattner550276e2010-10-28 20:52:15 +0000782 return ParseMemory();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000783 case AsmToken::LCurly:
Chris Lattner550276e2010-10-28 20:52:15 +0000784 return ParseRegisterList();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000785 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000786 // #42 -> immediate.
787 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000788 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000789 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000790 const MCExpr *ImmVal;
791 if (getParser().ParseExpression(ImmVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000792 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000793 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000794 return ARMOperand::CreateImm(ImmVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000795 }
796}
797
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000798/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000799bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000800 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000801 // Create the leading tokens for the mnemonic, split by '.' characters.
802 size_t Start = 0, Next = Name.find('.');
803 StringRef Head = Name.slice(Start, Next);
804
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000805 // Determine the predicate, if any.
806 //
807 // FIXME: We need a way to check whether a prefix supports predication,
808 // otherwise we will end up with an ambiguity for instructions that happen to
809 // end with a predicate name.
Jim Grosbach3df518e2010-10-29 21:56:51 +0000810 // FIXME: Likewise, some arithmetic instructions have an 's' prefix which
811 // indicates to update the condition codes. Those instructions have an
812 // additional immediate operand which encodes the prefix as reg0 or CPSR.
813 // Just checking for a suffix of 's' definitely creates ambiguities; e.g,
814 // the SMMLS instruction.
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000815 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
816 .Case("eq", ARMCC::EQ)
817 .Case("ne", ARMCC::NE)
818 .Case("hs", ARMCC::HS)
819 .Case("lo", ARMCC::LO)
820 .Case("mi", ARMCC::MI)
821 .Case("pl", ARMCC::PL)
822 .Case("vs", ARMCC::VS)
823 .Case("vc", ARMCC::VC)
824 .Case("hi", ARMCC::HI)
825 .Case("ls", ARMCC::LS)
826 .Case("ge", ARMCC::GE)
827 .Case("lt", ARMCC::LT)
828 .Case("gt", ARMCC::GT)
829 .Case("le", ARMCC::LE)
830 .Case("al", ARMCC::AL)
831 .Default(~0U);
Jim Grosbach16c74252010-10-29 14:46:02 +0000832
Chris Lattnerdba34d82010-10-30 04:35:59 +0000833 if (CC == ~0U ||
834 (CC == ARMCC::LS && (Head == "vmls" || Head == "vnmls"))) {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000835 CC = ARMCC::AL;
Chris Lattnerdba34d82010-10-30 04:35:59 +0000836 } else {
837 Head = Head.slice(0, Head.size() - 2);
Bill Wendling52925b62010-10-29 23:50:21 +0000838 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000839
Chris Lattner3a697562010-10-28 17:20:03 +0000840 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Jim Grosbach469ebbe2010-11-01 18:11:14 +0000841 // FIXME: Should only add this operand for predicated instructions
Chris Lattner3a697562010-10-28 17:20:03 +0000842 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000843
844 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000845 while (Next != StringRef::npos) {
846 Start = Next;
847 Next = Name.find('.', Start + 1);
848 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000849
Chris Lattner3a697562010-10-28 17:20:03 +0000850 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000851 }
852
853 // Read the remaining operands.
854 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000855 // Read the first operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000856 if (ARMOperand *Op = ParseOperand())
857 Operands.push_back(Op);
858 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000859 Parser.EatToEndOfStatement();
860 return true;
861 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000862
863 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000864 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000865
866 // Parse and remember the operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000867 if (ARMOperand *Op = ParseOperand())
868 Operands.push_back(Op);
869 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000870 Parser.EatToEndOfStatement();
871 return true;
872 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000873 }
874 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000875
Chris Lattnercbf8a982010-09-11 16:18:25 +0000876 if (getLexer().isNot(AsmToken::EndOfStatement)) {
877 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000878 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000879 }
Bill Wendling146018f2010-11-06 21:42:12 +0000880
Chris Lattner34e53142010-09-08 05:10:46 +0000881 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000882 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000883}
884
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000885bool ARMAsmParser::
886MatchAndEmitInstruction(SMLoc IDLoc,
887 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
888 MCStreamer &Out) {
889 MCInst Inst;
890 unsigned ErrorInfo;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000891 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
892 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000893 Out.EmitInstruction(Inst);
894 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000895 case Match_MissingFeature:
896 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
897 return true;
898 case Match_InvalidOperand: {
899 SMLoc ErrorLoc = IDLoc;
900 if (ErrorInfo != ~0U) {
901 if (ErrorInfo >= Operands.size())
902 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +0000903
Chris Lattnere73d4f82010-10-28 21:41:58 +0000904 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
905 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
906 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000907
Chris Lattnere73d4f82010-10-28 21:41:58 +0000908 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000909 }
Chris Lattnere73d4f82010-10-28 21:41:58 +0000910 case Match_MnemonicFail:
911 return Error(IDLoc, "unrecognized instruction mnemonic");
912 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000913
Eric Christopherc223e2b2010-10-29 09:26:59 +0000914 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +0000915 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000916}
917
Kevin Enderby515d5092009-10-15 20:48:48 +0000918/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000919bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
920 StringRef IDVal = DirectiveID.getIdentifier();
921 if (IDVal == ".word")
922 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000923 else if (IDVal == ".thumb")
924 return ParseDirectiveThumb(DirectiveID.getLoc());
925 else if (IDVal == ".thumb_func")
926 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
927 else if (IDVal == ".code")
928 return ParseDirectiveCode(DirectiveID.getLoc());
929 else if (IDVal == ".syntax")
930 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000931 return true;
932}
933
934/// ParseDirectiveWord
935/// ::= .word [ expression (, expression)* ]
936bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
937 if (getLexer().isNot(AsmToken::EndOfStatement)) {
938 for (;;) {
939 const MCExpr *Value;
940 if (getParser().ParseExpression(Value))
941 return true;
942
Chris Lattneraaec2052010-01-19 19:46:13 +0000943 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000944
945 if (getLexer().is(AsmToken::EndOfStatement))
946 break;
Jim Grosbach16c74252010-10-29 14:46:02 +0000947
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000948 // FIXME: Improve diagnostic.
949 if (getLexer().isNot(AsmToken::Comma))
950 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000951 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000952 }
953 }
954
Sean Callananb9a25b72010-01-19 20:27:46 +0000955 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000956 return false;
957}
958
Kevin Enderby515d5092009-10-15 20:48:48 +0000959/// ParseDirectiveThumb
960/// ::= .thumb
961bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
962 if (getLexer().isNot(AsmToken::EndOfStatement))
963 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000964 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000965
966 // TODO: set thumb mode
967 // TODO: tell the MC streamer the mode
968 // getParser().getStreamer().Emit???();
969 return false;
970}
971
972/// ParseDirectiveThumbFunc
973/// ::= .thumbfunc symbol_name
974bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000975 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000976 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +0000977 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000978 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +0000979 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000980 if (getLexer().isNot(AsmToken::EndOfStatement))
981 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000982 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000983
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000984 // Mark symbol as a thumb symbol.
985 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
986 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +0000987 return false;
988}
989
990/// ParseDirectiveSyntax
991/// ::= .syntax unified | divided
992bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000993 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000994 if (Tok.isNot(AsmToken::Identifier))
995 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000996 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000997 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000998 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000999 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001000 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001001 else
1002 return Error(L, "unrecognized syntax mode in .syntax directive");
1003
1004 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001005 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001006 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001007
1008 // TODO tell the MC streamer the mode
1009 // getParser().getStreamer().Emit???();
1010 return false;
1011}
1012
1013/// ParseDirectiveCode
1014/// ::= .code 16 | 32
1015bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001016 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001017 if (Tok.isNot(AsmToken::Integer))
1018 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001019 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001020 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001021 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001022 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001023 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001024 else
1025 return Error(L, "invalid operand to .code directive");
1026
1027 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001028 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001029 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001030
Jim Grosbach2a301702010-11-05 22:40:53 +00001031 if (Val == 16)
1032 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
1033 else
1034 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1035
Kevin Enderby515d5092009-10-15 20:48:48 +00001036 return false;
1037}
1038
Sean Callanan90b70972010-04-07 20:29:34 +00001039extern "C" void LLVMInitializeARMAsmLexer();
1040
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001041/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001042extern "C" void LLVMInitializeARMAsmParser() {
1043 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1044 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001045 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001046}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001047
Chris Lattner0692ee62010-09-06 19:11:01 +00001048#define GET_REGISTER_MATCHER
1049#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001050#include "ARMGenAsmMatcher.inc"