blob: 1744899a29b1812f6464bf4b33af886875ad4a41 [file] [log] [blame]
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001//===-- ARMAsmParser.cpp - Parse ARM assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ARM.h"
Bill Wendling92b5a2e2010-11-03 01:49:29 +000011#include "ARMAddressingModes.h"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000012#include "ARMSubtarget.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000013#include "llvm/MC/MCParser/MCAsmLexer.h"
14#include "llvm/MC/MCParser/MCAsmParser.h"
15#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Jim Grosbach642fc9c2010-11-05 22:33:53 +000016#include "llvm/MC/MCContext.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000017#include "llvm/MC/MCStreamer.h"
18#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCInst.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000020#include "llvm/Target/TargetRegistry.h"
21#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000022#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000023#include "llvm/Support/raw_ostream.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/ADT/SmallVector.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000025#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000026#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000027using namespace llvm;
28
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000029// The shift types for register controlled shifts in arm memory addressing
30enum ShiftType {
31 Lsl,
32 Lsr,
33 Asr,
34 Ror,
35 Rrx
36};
37
Chris Lattner3a697562010-10-28 17:20:03 +000038namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000039
40class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000041
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000042class ARMAsmParser : public TargetAsmParser {
43 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000044 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000045
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000046 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000047 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
48
49 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000050 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
51
Chris Lattnere5658fa2010-10-30 04:09:10 +000052 int TryParseRegister();
53 ARMOperand *TryParseRegisterWithWriteBack();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +000054 ARMOperand *ParseRegisterList();
Chris Lattner550276e2010-10-28 20:52:15 +000055 ARMOperand *ParseMemory();
Bill Wendling146018f2010-11-06 21:42:12 +000056 ARMOperand *ParseOperand();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000057
Kevin Enderby9c41fa82009-10-30 22:55:57 +000058 bool ParseMemoryOffsetReg(bool &Negative,
59 bool &OffsetRegShifted,
60 enum ShiftType &ShiftType,
61 const MCExpr *&ShiftAmount,
62 const MCExpr *&Offset,
63 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000064 int &OffsetRegNum,
65 SMLoc &E);
Sean Callanan76264762010-04-02 22:27:05 +000066 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000067 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000068 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000069 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000070 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000071 bool ParseDirectiveSyntax(SMLoc L);
72
Chris Lattner7036f8b2010-09-29 01:42:58 +000073 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000074 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000075 MCStreamer &Out);
Jim Grosbach16c74252010-10-29 14:46:02 +000076
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000077 /// @name Auto-generated Match Functions
78 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000079
Chris Lattner0692ee62010-09-06 19:11:01 +000080#define GET_ASSEMBLER_HEADER
81#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000082
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000083 /// }
84
85
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000086public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000087 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +000088 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
89 // Initialize the set of available features.
90 setAvailableFeatures(ComputeAvailableFeatures(
91 &TM.getSubtarget<ARMSubtarget>()));
92 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000093
Benjamin Kramer38e59892010-07-14 22:38:02 +000094 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000095 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000096
97 virtual bool ParseDirective(AsmToken DirectiveID);
98};
Jim Grosbach16c74252010-10-29 14:46:02 +000099} // end anonymous namespace
100
Chris Lattner3a697562010-10-28 17:20:03 +0000101namespace {
102
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000103/// ARMOperand - Instances of this class represent a parsed ARM machine
104/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000105class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000106 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000107 CondCode,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000108 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000109 Memory,
110 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000111 RegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000112 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000113 } Kind;
114
Sean Callanan76264762010-04-02 22:27:05 +0000115 SMLoc StartLoc, EndLoc;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000116
117 union {
118 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000119 ARMCC::CondCodes Val;
120 } CC;
121
122 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000123 const char *Data;
124 unsigned Length;
125 } Tok;
126
127 struct {
128 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000129 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000130 } Reg;
131
Bill Wendling8155e5b2010-11-06 22:19:43 +0000132 struct {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000133 unsigned RegStart;
134 unsigned Number;
135 } RegList;
136
Kevin Enderbycfe07242009-10-13 22:19:02 +0000137 struct {
138 const MCExpr *Val;
139 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000140
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000141 // This is for all forms of ARM address expressions
142 struct {
143 unsigned BaseRegNum;
Bill Wendling146018f2010-11-06 21:42:12 +0000144 unsigned OffsetRegNum; // used when OffsetIsReg is true
145 const MCExpr *Offset; // used when OffsetIsReg is false
146 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
147 enum ShiftType ShiftType; // used when OffsetRegShifted is true
148 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
149 unsigned Preindexed : 1;
150 unsigned Postindexed : 1;
151 unsigned OffsetIsReg : 1;
152 unsigned Negative : 1; // only used when OffsetIsReg is true
153 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000154 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000155 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000156
Bill Wendling146018f2010-11-06 21:42:12 +0000157 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
158public:
Sean Callanan76264762010-04-02 22:27:05 +0000159 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
160 Kind = o.Kind;
161 StartLoc = o.StartLoc;
162 EndLoc = o.EndLoc;
163 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000164 case CondCode:
165 CC = o.CC;
166 break;
Sean Callanan76264762010-04-02 22:27:05 +0000167 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000168 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000169 break;
170 case Register:
171 Reg = o.Reg;
172 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000173 case RegisterList:
174 RegList = o.RegList;
175 break;
Sean Callanan76264762010-04-02 22:27:05 +0000176 case Immediate:
177 Imm = o.Imm;
178 break;
179 case Memory:
180 Mem = o.Mem;
181 break;
182 }
183 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000184
Sean Callanan76264762010-04-02 22:27:05 +0000185 /// getStartLoc - Get the location of the first token of this operand.
186 SMLoc getStartLoc() const { return StartLoc; }
187 /// getEndLoc - Get the location of the last token of this operand.
188 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000189
Daniel Dunbar8462b302010-08-11 06:36:53 +0000190 ARMCC::CondCodes getCondCode() const {
191 assert(Kind == CondCode && "Invalid access!");
192 return CC.Val;
193 }
194
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000195 StringRef getToken() const {
196 assert(Kind == Token && "Invalid access!");
197 return StringRef(Tok.Data, Tok.Length);
198 }
199
200 unsigned getReg() const {
Bill Wendling8155e5b2010-11-06 22:19:43 +0000201 assert((Kind == Register || Kind == RegisterList) && "Invalid access!");
202 unsigned RegNum = 0;
203 if (Kind == Register)
204 RegNum = Reg.RegNum;
205 else
206 RegNum = RegList.RegStart;
207 return RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000208 }
209
Bill Wendling8d5acb72010-11-06 19:56:04 +0000210 std::pair<unsigned, unsigned> getRegList() const {
211 assert(Kind == RegisterList && "Invalid access!");
212 return std::make_pair(RegList.RegStart, RegList.Number);
213 }
214
Kevin Enderbycfe07242009-10-13 22:19:02 +0000215 const MCExpr *getImm() const {
216 assert(Kind == Immediate && "Invalid access!");
217 return Imm.Val;
218 }
219
Daniel Dunbar8462b302010-08-11 06:36:53 +0000220 bool isCondCode() const { return Kind == CondCode; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000221 bool isImm() const { return Kind == Immediate; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000222 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000223 bool isRegList() const { return Kind == RegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000224 bool isToken() const { return Kind == Token; }
225 bool isMemory() const { return Kind == Memory; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000226
227 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000228 // Add as immediates when possible. Null MCExpr = 0.
229 if (Expr == 0)
230 Inst.addOperand(MCOperand::CreateImm(0));
231 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000232 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
233 else
234 Inst.addOperand(MCOperand::CreateExpr(Expr));
235 }
236
Daniel Dunbar8462b302010-08-11 06:36:53 +0000237 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000238 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000239 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000240 // FIXME: What belongs here?
241 Inst.addOperand(MCOperand::CreateReg(0));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000242 }
243
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000244 void addRegOperands(MCInst &Inst, unsigned N) const {
245 assert(N == 1 && "Invalid number of operands!");
246 Inst.addOperand(MCOperand::CreateReg(getReg()));
247 }
248
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000249 void addImmOperands(MCInst &Inst, unsigned N) const {
250 assert(N == 1 && "Invalid number of operands!");
251 addExpr(Inst, getImm());
252 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000253
Chris Lattner14b93852010-10-29 00:27:31 +0000254 bool isMemMode5() const {
Chris Lattner14b93852010-10-29 00:27:31 +0000255 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
Jim Grosbach80eb2332010-10-29 17:41:25 +0000256 Mem.Writeback || Mem.Negative)
Chris Lattner14b93852010-10-29 00:27:31 +0000257 return false;
Jim Grosbach80eb2332010-10-29 17:41:25 +0000258 // If there is an offset expression, make sure it's valid.
259 if (!Mem.Offset)
260 return true;
261 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
262 if (!CE)
263 return false;
264 // The offset must be a multiple of 4 in the range 0-1020.
265 int64_t Value = CE->getValue();
266 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
Chris Lattner14b93852010-10-29 00:27:31 +0000267 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000268
Chris Lattner14b93852010-10-29 00:27:31 +0000269 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
270 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000271
Chris Lattner14b93852010-10-29 00:27:31 +0000272 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlinga60f1572010-11-06 10:48:18 +0000273 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000274
Jim Grosbach80eb2332010-10-29 17:41:25 +0000275 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
276 // the difference?
277 if (Mem.Offset) {
278 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000279 assert(CE && "Non-constant mode 5 offset operand!");
280
Jim Grosbach80eb2332010-10-29 17:41:25 +0000281 // The MCInst offset operand doesn't include the low two bits (like
282 // the instruction encoding).
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000283 int64_t Offset = CE->getValue() / 4;
284 if (Offset >= 0)
285 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
286 Offset)));
287 else
288 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
289 -Offset)));
290 } else {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000291 Inst.addOperand(MCOperand::CreateImm(0));
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000292 }
Chris Lattner14b93852010-10-29 00:27:31 +0000293 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000294
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000295 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000296
Chris Lattner3a697562010-10-28 17:20:03 +0000297 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
298 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000299 Op->CC.Val = CC;
300 Op->StartLoc = S;
301 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000302 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000303 }
304
Chris Lattner3a697562010-10-28 17:20:03 +0000305 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
306 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000307 Op->Tok.Data = Str.data();
308 Op->Tok.Length = Str.size();
309 Op->StartLoc = S;
310 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000311 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000312 }
313
Chris Lattner3a697562010-10-28 17:20:03 +0000314 static ARMOperand *CreateReg(unsigned RegNum, bool Writeback, SMLoc S,
315 SMLoc E) {
316 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000317 Op->Reg.RegNum = RegNum;
318 Op->Reg.Writeback = Writeback;
Sean Callanan76264762010-04-02 22:27:05 +0000319 Op->StartLoc = S;
320 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000321 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000322 }
323
Bill Wendling8d5acb72010-11-06 19:56:04 +0000324 static ARMOperand *CreateRegList(unsigned RegStart, unsigned Number,
325 SMLoc S, SMLoc E) {
326 ARMOperand *Op = new ARMOperand(RegisterList);
327 Op->RegList.RegStart = RegStart;
328 Op->RegList.Number = Number;
329 Op->StartLoc = S;
330 Op->EndLoc = E;
331 return Op;
332 }
333
Chris Lattner3a697562010-10-28 17:20:03 +0000334 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
335 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000336 Op->Imm.Val = Val;
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 Enderbycfe07242009-10-13 22:19:02 +0000340 }
341
Chris Lattner3a697562010-10-28 17:20:03 +0000342 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
343 const MCExpr *Offset, unsigned OffsetRegNum,
344 bool OffsetRegShifted, enum ShiftType ShiftType,
345 const MCExpr *ShiftAmount, bool Preindexed,
346 bool Postindexed, bool Negative, bool Writeback,
347 SMLoc S, SMLoc E) {
348 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000349 Op->Mem.BaseRegNum = BaseRegNum;
350 Op->Mem.OffsetIsReg = OffsetIsReg;
351 Op->Mem.Offset = Offset;
352 Op->Mem.OffsetRegNum = OffsetRegNum;
353 Op->Mem.OffsetRegShifted = OffsetRegShifted;
354 Op->Mem.ShiftType = ShiftType;
355 Op->Mem.ShiftAmount = ShiftAmount;
356 Op->Mem.Preindexed = Preindexed;
357 Op->Mem.Postindexed = Postindexed;
358 Op->Mem.Negative = Negative;
359 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000360
Sean Callanan76264762010-04-02 22:27:05 +0000361 Op->StartLoc = S;
362 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000363 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000364 }
365};
366
367} // end anonymous namespace.
368
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000369void ARMOperand::dump(raw_ostream &OS) const {
370 switch (Kind) {
371 case CondCode:
372 OS << ARMCondCodeToString(getCondCode());
373 break;
374 case Immediate:
375 getImm()->print(OS);
376 break;
377 case Memory:
378 OS << "<memory>";
379 break;
380 case Register:
381 OS << "<register " << getReg() << ">";
382 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000383 case RegisterList: {
384 OS << "<register_list ";
385 std::pair<unsigned, unsigned> List = getRegList();
386 unsigned RegEnd = List.first + List.second;
387
388 for (unsigned Idx = List.first; Idx < RegEnd; ) {
389 OS << Idx;
390 if (++Idx < RegEnd) OS << ", ";
391 }
392
393 OS << ">";
394 break;
395 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000396 case Token:
397 OS << "'" << getToken() << "'";
398 break;
399 }
400}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000401
402/// @name Auto-generated Match Functions
403/// {
404
405static unsigned MatchRegisterName(StringRef Name);
406
407/// }
408
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000409/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000410/// and if it is a register name the token is eaten and the register number is
411/// returned. Otherwise return -1.
412///
413int ARMAsmParser::TryParseRegister() {
414 const AsmToken &Tok = Parser.getTok();
415 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000416
Chris Lattnere5658fa2010-10-30 04:09:10 +0000417 // FIXME: Validate register for the current architecture; we have to do
418 // validation later, so maybe there is no need for this here.
Bill Wendlingd68fd9c2010-11-06 10:45:34 +0000419 unsigned RegNum = MatchRegisterName(Tok.getString());
420 if (RegNum == 0)
Chris Lattnere5658fa2010-10-30 04:09:10 +0000421 return -1;
422 Parser.Lex(); // Eat identifier token.
423 return RegNum;
424}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000425
426
Chris Lattnere5658fa2010-10-30 04:09:10 +0000427/// Try to parse a register name. The token must be an Identifier when called,
428/// and if it is a register name the token is eaten and the register number is
429/// returned. Otherwise return -1.
Chris Lattner3a697562010-10-28 17:20:03 +0000430///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000431/// TODO this is likely to change to allow different register types and or to
432/// parse for a specific register type.
Chris Lattnere5658fa2010-10-30 04:09:10 +0000433ARMOperand *ARMAsmParser::TryParseRegisterWithWriteBack() {
434 SMLoc S = Parser.getTok().getLoc();
435 int RegNo = TryParseRegister();
436 if (RegNo == -1) return 0;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000437
Chris Lattnere5658fa2010-10-30 04:09:10 +0000438 SMLoc E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000439
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000440 bool Writeback = false;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000441 const AsmToken &ExclaimTok = Parser.getTok();
442 if (ExclaimTok.is(AsmToken::Exclaim)) {
443 E = ExclaimTok.getLoc();
444 Writeback = true;
445 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000446 }
447
Chris Lattnere5658fa2010-10-30 04:09:10 +0000448 return ARMOperand::CreateReg(RegNo, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000449}
450
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000451/// Parse a register list, return it if successful else return null. The first
452/// token must be a '{' when called.
453ARMOperand *ARMAsmParser::ParseRegisterList() {
Sean Callanan76264762010-04-02 22:27:05 +0000454 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000455 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000456 "Token is not a Left Curly Brace");
Sean Callanan76264762010-04-02 22:27:05 +0000457 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000458 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000459
Sean Callanan18b83232010-01-19 21:44:56 +0000460 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000461 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000462 if (RegTok.isNot(AsmToken::Identifier)) {
463 Error(RegLoc, "register expected");
464 return 0;
465 }
Bill Wendling1d6a2652010-11-06 10:40:24 +0000466 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000467 if (RegNum == -1) {
468 Error(RegLoc, "register expected");
469 return 0;
470 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000471
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000472 unsigned RegList = 1 << RegNum;
473
474 int HighRegNum = RegNum;
475 // TODO ranges like "{Rn-Rm}"
Sean Callanan18b83232010-01-19 21:44:56 +0000476 while (Parser.getTok().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000477 Parser.Lex(); // Eat comma token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000478
Sean Callanan18b83232010-01-19 21:44:56 +0000479 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000480 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000481 if (RegTok.isNot(AsmToken::Identifier)) {
482 Error(RegLoc, "register expected");
483 return 0;
484 }
Bill Wendling1d6a2652010-11-06 10:40:24 +0000485 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000486 if (RegNum == -1) {
487 Error(RegLoc, "register expected");
488 return 0;
489 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000490
491 if (RegList & (1 << RegNum))
492 Warning(RegLoc, "register duplicated in register list");
493 else if (RegNum <= HighRegNum)
494 Warning(RegLoc, "register not in ascending order in register list");
495 RegList |= 1 << RegNum;
496 HighRegNum = RegNum;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000497 }
Sean Callanan18b83232010-01-19 21:44:56 +0000498 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000499 if (RCurlyTok.isNot(AsmToken::RCurly)) {
500 Error(RCurlyTok.getLoc(), "'}' expected");
501 return 0;
502 }
Sean Callanan76264762010-04-02 22:27:05 +0000503 E = RCurlyTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000504 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000505
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000506 // FIXME: Need to return an operand!
507 Error(E, "FIXME: register list parsing not implemented");
508 return 0;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000509}
510
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000511/// Parse an arm memory expression, return false if successful else return true
512/// or an error. The first token must be a '[' when called.
513/// TODO Only preindexing and postindexing addressing are started, unindexed
514/// with option, etc are still to do.
Chris Lattner550276e2010-10-28 20:52:15 +0000515ARMOperand *ARMAsmParser::ParseMemory() {
Sean Callanan76264762010-04-02 22:27:05 +0000516 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000517 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000518 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000519 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000520 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000521
Sean Callanan18b83232010-01-19 21:44:56 +0000522 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000523 if (BaseRegTok.isNot(AsmToken::Identifier)) {
524 Error(BaseRegTok.getLoc(), "register expected");
525 return 0;
526 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000527 int BaseRegNum = TryParseRegister();
528 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000529 Error(BaseRegTok.getLoc(), "register expected");
530 return 0;
531 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000532
533 bool Preindexed = false;
534 bool Postindexed = false;
535 bool OffsetIsReg = false;
536 bool Negative = false;
537 bool Writeback = false;
538
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000539 // First look for preindexed address forms, that is after the "[Rn" we now
540 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000541 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000542 if (Tok.is(AsmToken::Comma)) {
543 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000544 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000545 int OffsetRegNum;
546 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000547 enum ShiftType ShiftType;
548 const MCExpr *ShiftAmount;
549 const MCExpr *Offset;
Chris Lattner550276e2010-10-28 20:52:15 +0000550 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
551 Offset, OffsetIsReg, OffsetRegNum, E))
552 return 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000553 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000554 if (RBracTok.isNot(AsmToken::RBrac)) {
555 Error(RBracTok.getLoc(), "']' expected");
556 return 0;
557 }
Sean Callanan76264762010-04-02 22:27:05 +0000558 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000559 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000560
Sean Callanan18b83232010-01-19 21:44:56 +0000561 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000562 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000563 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000564 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000565 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000566 }
Chris Lattner550276e2010-10-28 20:52:15 +0000567 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
568 OffsetRegShifted, ShiftType, ShiftAmount,
569 Preindexed, Postindexed, Negative, Writeback,
570 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000571 }
572 // The "[Rn" we have so far was not followed by a comma.
573 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000574 // If there's anything other than the right brace, this is a post indexing
575 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000576 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000577 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000578
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000579 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000580 bool OffsetRegShifted = false;
581 enum ShiftType ShiftType;
582 const MCExpr *ShiftAmount;
Chris Lattner14b93852010-10-29 00:27:31 +0000583 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000584
Sean Callanan18b83232010-01-19 21:44:56 +0000585 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000586 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000587 Postindexed = true;
588 Writeback = true;
Chris Lattner550276e2010-10-28 20:52:15 +0000589 if (NextTok.isNot(AsmToken::Comma)) {
590 Error(NextTok.getLoc(), "',' expected");
591 return 0;
592 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000593 Parser.Lex(); // Eat comma token.
Chris Lattner550276e2010-10-28 20:52:15 +0000594 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000595 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000596 E))
597 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000598 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000599
Chris Lattner550276e2010-10-28 20:52:15 +0000600 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
601 OffsetRegShifted, ShiftType, ShiftAmount,
602 Preindexed, Postindexed, Negative, Writeback,
603 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000604 }
605
Chris Lattner550276e2010-10-28 20:52:15 +0000606 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000607}
608
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000609/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
610/// we will parse the following (were +/- means that a plus or minus is
611/// optional):
612/// +/-Rm
613/// +/-Rm, shift
614/// #offset
615/// we return false on success or an error otherwise.
616bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000617 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000618 enum ShiftType &ShiftType,
619 const MCExpr *&ShiftAmount,
620 const MCExpr *&Offset,
621 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000622 int &OffsetRegNum,
623 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000624 Negative = false;
625 OffsetRegShifted = false;
626 OffsetIsReg = false;
627 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000628 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000629 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000630 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000631 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000632 else if (NextTok.is(AsmToken::Minus)) {
633 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000634 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000635 }
636 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000637 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000638 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000639 SMLoc CurLoc = OffsetRegTok.getLoc();
640 OffsetRegNum = TryParseRegister();
641 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000642 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000643 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000644 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000645 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000646
Bill Wendling12f40e92010-11-06 10:51:53 +0000647 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000648 if (OffsetRegNum != -1) {
649 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000650 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000651 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000652 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000653
Sean Callanan18b83232010-01-19 21:44:56 +0000654 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000655 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000656 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000657 OffsetRegShifted = true;
658 }
659 }
660 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
661 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000662 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000663 if (HashTok.isNot(AsmToken::Hash))
664 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000665
Sean Callananb9a25b72010-01-19 20:27:46 +0000666 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000667
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000668 if (getParser().ParseExpression(Offset))
669 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000670 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000671 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000672 return false;
673}
674
675/// ParseShift as one of these two:
676/// ( lsl | lsr | asr | ror ) , # shift_amount
677/// rrx
678/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000679bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000680 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000681 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000682 if (Tok.isNot(AsmToken::Identifier))
683 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000684 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000685 if (ShiftName == "lsl" || ShiftName == "LSL")
686 St = Lsl;
687 else if (ShiftName == "lsr" || ShiftName == "LSR")
688 St = Lsr;
689 else if (ShiftName == "asr" || ShiftName == "ASR")
690 St = Asr;
691 else if (ShiftName == "ror" || ShiftName == "ROR")
692 St = Ror;
693 else if (ShiftName == "rrx" || ShiftName == "RRX")
694 St = Rrx;
695 else
696 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000697 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000698
699 // Rrx stands alone.
700 if (St == Rrx)
701 return false;
702
703 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000704 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000705 if (HashTok.isNot(AsmToken::Hash))
706 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000707 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000708
709 if (getParser().ParseExpression(ShiftAmount))
710 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000711
712 return false;
713}
714
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000715/// Parse a arm instruction operand. For now this parses the operand regardless
716/// of the mnemonic.
Chris Lattner550276e2010-10-28 20:52:15 +0000717ARMOperand *ARMAsmParser::ParseOperand() {
Sean Callanan76264762010-04-02 22:27:05 +0000718 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000719 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000720 default:
721 Error(Parser.getTok().getLoc(), "unexpected token in operand");
722 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000723 case AsmToken::Identifier:
Chris Lattnere5658fa2010-10-30 04:09:10 +0000724 if (ARMOperand *Op = TryParseRegisterWithWriteBack())
Chris Lattner550276e2010-10-28 20:52:15 +0000725 return Op;
Jim Grosbach16c74252010-10-29 14:46:02 +0000726
Kevin Enderby515d5092009-10-15 20:48:48 +0000727 // This was not a register so parse other operands that start with an
728 // identifier (like labels) as expressions and create them as immediates.
729 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000730 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000731 if (getParser().ParseExpression(IdVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000732 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000733 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000734 return ARMOperand::CreateImm(IdVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000735 case AsmToken::LBrac:
Chris Lattner550276e2010-10-28 20:52:15 +0000736 return ParseMemory();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000737 case AsmToken::LCurly:
Chris Lattner550276e2010-10-28 20:52:15 +0000738 return ParseRegisterList();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000739 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000740 // #42 -> immediate.
741 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000742 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000743 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000744 const MCExpr *ImmVal;
745 if (getParser().ParseExpression(ImmVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000746 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000747 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000748 return ARMOperand::CreateImm(ImmVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000749 }
750}
751
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000752/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000753bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000754 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000755 // Create the leading tokens for the mnemonic, split by '.' characters.
756 size_t Start = 0, Next = Name.find('.');
757 StringRef Head = Name.slice(Start, Next);
758
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000759 // Determine the predicate, if any.
760 //
761 // FIXME: We need a way to check whether a prefix supports predication,
762 // otherwise we will end up with an ambiguity for instructions that happen to
763 // end with a predicate name.
Jim Grosbach3df518e2010-10-29 21:56:51 +0000764 // FIXME: Likewise, some arithmetic instructions have an 's' prefix which
765 // indicates to update the condition codes. Those instructions have an
766 // additional immediate operand which encodes the prefix as reg0 or CPSR.
767 // Just checking for a suffix of 's' definitely creates ambiguities; e.g,
768 // the SMMLS instruction.
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000769 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
770 .Case("eq", ARMCC::EQ)
771 .Case("ne", ARMCC::NE)
772 .Case("hs", ARMCC::HS)
773 .Case("lo", ARMCC::LO)
774 .Case("mi", ARMCC::MI)
775 .Case("pl", ARMCC::PL)
776 .Case("vs", ARMCC::VS)
777 .Case("vc", ARMCC::VC)
778 .Case("hi", ARMCC::HI)
779 .Case("ls", ARMCC::LS)
780 .Case("ge", ARMCC::GE)
781 .Case("lt", ARMCC::LT)
782 .Case("gt", ARMCC::GT)
783 .Case("le", ARMCC::LE)
784 .Case("al", ARMCC::AL)
785 .Default(~0U);
Jim Grosbach16c74252010-10-29 14:46:02 +0000786
Chris Lattnerdba34d82010-10-30 04:35:59 +0000787 if (CC == ~0U ||
788 (CC == ARMCC::LS && (Head == "vmls" || Head == "vnmls"))) {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000789 CC = ARMCC::AL;
Chris Lattnerdba34d82010-10-30 04:35:59 +0000790 } else {
791 Head = Head.slice(0, Head.size() - 2);
Bill Wendling52925b62010-10-29 23:50:21 +0000792 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000793
Chris Lattner3a697562010-10-28 17:20:03 +0000794 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Jim Grosbach469ebbe2010-11-01 18:11:14 +0000795 // FIXME: Should only add this operand for predicated instructions
Chris Lattner3a697562010-10-28 17:20:03 +0000796 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000797
798 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000799 while (Next != StringRef::npos) {
800 Start = Next;
801 Next = Name.find('.', Start + 1);
802 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000803
Chris Lattner3a697562010-10-28 17:20:03 +0000804 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000805 }
806
807 // Read the remaining operands.
808 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000809 // Read the first operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000810 if (ARMOperand *Op = ParseOperand())
811 Operands.push_back(Op);
812 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000813 Parser.EatToEndOfStatement();
814 return true;
815 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000816
817 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000818 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000819
820 // Parse and remember the operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000821 if (ARMOperand *Op = ParseOperand())
822 Operands.push_back(Op);
823 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000824 Parser.EatToEndOfStatement();
825 return true;
826 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000827 }
828 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000829
Chris Lattnercbf8a982010-09-11 16:18:25 +0000830 if (getLexer().isNot(AsmToken::EndOfStatement)) {
831 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000832 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000833 }
Bill Wendling146018f2010-11-06 21:42:12 +0000834
Chris Lattner34e53142010-09-08 05:10:46 +0000835 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000836 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000837}
838
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000839bool ARMAsmParser::
840MatchAndEmitInstruction(SMLoc IDLoc,
841 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
842 MCStreamer &Out) {
843 MCInst Inst;
844 unsigned ErrorInfo;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000845 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
846 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000847 Out.EmitInstruction(Inst);
848 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000849 case Match_MissingFeature:
850 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
851 return true;
852 case Match_InvalidOperand: {
853 SMLoc ErrorLoc = IDLoc;
854 if (ErrorInfo != ~0U) {
855 if (ErrorInfo >= Operands.size())
856 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +0000857
Chris Lattnere73d4f82010-10-28 21:41:58 +0000858 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
859 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
860 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000861
Chris Lattnere73d4f82010-10-28 21:41:58 +0000862 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000863 }
Chris Lattnere73d4f82010-10-28 21:41:58 +0000864 case Match_MnemonicFail:
865 return Error(IDLoc, "unrecognized instruction mnemonic");
866 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000867
Eric Christopherc223e2b2010-10-29 09:26:59 +0000868 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +0000869 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000870}
871
Kevin Enderby515d5092009-10-15 20:48:48 +0000872/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000873bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
874 StringRef IDVal = DirectiveID.getIdentifier();
875 if (IDVal == ".word")
876 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000877 else if (IDVal == ".thumb")
878 return ParseDirectiveThumb(DirectiveID.getLoc());
879 else if (IDVal == ".thumb_func")
880 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
881 else if (IDVal == ".code")
882 return ParseDirectiveCode(DirectiveID.getLoc());
883 else if (IDVal == ".syntax")
884 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000885 return true;
886}
887
888/// ParseDirectiveWord
889/// ::= .word [ expression (, expression)* ]
890bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
891 if (getLexer().isNot(AsmToken::EndOfStatement)) {
892 for (;;) {
893 const MCExpr *Value;
894 if (getParser().ParseExpression(Value))
895 return true;
896
Chris Lattneraaec2052010-01-19 19:46:13 +0000897 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000898
899 if (getLexer().is(AsmToken::EndOfStatement))
900 break;
Jim Grosbach16c74252010-10-29 14:46:02 +0000901
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000902 // FIXME: Improve diagnostic.
903 if (getLexer().isNot(AsmToken::Comma))
904 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000905 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000906 }
907 }
908
Sean Callananb9a25b72010-01-19 20:27:46 +0000909 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000910 return false;
911}
912
Kevin Enderby515d5092009-10-15 20:48:48 +0000913/// ParseDirectiveThumb
914/// ::= .thumb
915bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
916 if (getLexer().isNot(AsmToken::EndOfStatement))
917 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000918 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000919
920 // TODO: set thumb mode
921 // TODO: tell the MC streamer the mode
922 // getParser().getStreamer().Emit???();
923 return false;
924}
925
926/// ParseDirectiveThumbFunc
927/// ::= .thumbfunc symbol_name
928bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000929 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000930 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +0000931 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000932 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +0000933 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000934 if (getLexer().isNot(AsmToken::EndOfStatement))
935 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000936 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000937
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000938 // Mark symbol as a thumb symbol.
939 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
940 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +0000941 return false;
942}
943
944/// ParseDirectiveSyntax
945/// ::= .syntax unified | divided
946bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000947 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000948 if (Tok.isNot(AsmToken::Identifier))
949 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000950 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000951 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000952 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000953 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000954 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000955 else
956 return Error(L, "unrecognized syntax mode in .syntax directive");
957
958 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000959 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000960 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000961
962 // TODO tell the MC streamer the mode
963 // getParser().getStreamer().Emit???();
964 return false;
965}
966
967/// ParseDirectiveCode
968/// ::= .code 16 | 32
969bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000970 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000971 if (Tok.isNot(AsmToken::Integer))
972 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000973 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +0000974 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +0000975 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000976 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +0000977 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000978 else
979 return Error(L, "invalid operand to .code directive");
980
981 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000982 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000983 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000984
Jim Grosbach2a301702010-11-05 22:40:53 +0000985 if (Val == 16)
986 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
987 else
988 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
989
Kevin Enderby515d5092009-10-15 20:48:48 +0000990 return false;
991}
992
Sean Callanan90b70972010-04-07 20:29:34 +0000993extern "C" void LLVMInitializeARMAsmLexer();
994
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000995/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000996extern "C" void LLVMInitializeARMAsmParser() {
997 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
998 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +0000999 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001000}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001001
Chris Lattner0692ee62010-09-06 19:11:01 +00001002#define GET_REGISTER_MATCHER
1003#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001004#include "ARMGenAsmMatcher.inc"