blob: f5dc38bcfed04ac84c2c2a3d72a0b3e971405b8e [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"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000016#include "llvm/MC/MCStreamer.h"
17#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCInst.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000019#include "llvm/Target/TargetRegistry.h"
20#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000021#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000022#include "llvm/Support/raw_ostream.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000023#include "llvm/ADT/SmallVector.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000024#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000025#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000026using namespace llvm;
27
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000028// The shift types for register controlled shifts in arm memory addressing
29enum ShiftType {
30 Lsl,
31 Lsr,
32 Asr,
33 Ror,
34 Rrx
35};
36
Chris Lattner3a697562010-10-28 17:20:03 +000037namespace {
38 struct ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000039
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000040class ARMAsmParser : public TargetAsmParser {
41 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000042 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000043
44private:
45 MCAsmParser &getParser() const { return Parser; }
46
47 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
48
49 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
50
51 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
52
Chris Lattnere5658fa2010-10-30 04:09:10 +000053 int TryParseRegister();
54 ARMOperand *TryParseRegisterWithWriteBack();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +000055 ARMOperand *ParseRegisterList();
Chris Lattner550276e2010-10-28 20:52:15 +000056 ARMOperand *ParseMemory();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000057
Kevin Enderby9c41fa82009-10-30 22:55:57 +000058 bool ParseMemoryOffsetReg(bool &Negative,
59 bool &OffsetRegShifted,
60 enum ShiftType &ShiftType,
61 const MCExpr *&ShiftAmount,
62 const MCExpr *&Offset,
63 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000064 int &OffsetRegNum,
65 SMLoc &E);
Kevin Enderby9c41fa82009-10-30 22:55:57 +000066
Sean Callanan76264762010-04-02 22:27:05 +000067 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000068
Chris Lattner550276e2010-10-28 20:52:15 +000069 ARMOperand *ParseOperand();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000070
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000071 bool ParseDirectiveWord(unsigned Size, SMLoc L);
72
Kevin Enderby515d5092009-10-15 20:48:48 +000073 bool ParseDirectiveThumb(SMLoc L);
74
75 bool ParseDirectiveThumbFunc(SMLoc L);
76
77 bool ParseDirectiveCode(SMLoc L);
78
79 bool ParseDirectiveSyntax(SMLoc L);
80
Chris Lattner7036f8b2010-09-29 01:42:58 +000081 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000082 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000083 MCStreamer &Out);
Jim Grosbach16c74252010-10-29 14:46:02 +000084
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000085 /// @name Auto-generated Match Functions
86 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000087
Chris Lattner0692ee62010-09-06 19:11:01 +000088#define GET_ASSEMBLER_HEADER
89#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000090
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000091 /// }
92
93
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000094public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000095 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +000096 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
97 // Initialize the set of available features.
98 setAvailableFeatures(ComputeAvailableFeatures(
99 &TM.getSubtarget<ARMSubtarget>()));
100 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000101
Benjamin Kramer38e59892010-07-14 22:38:02 +0000102 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000103 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000104
105 virtual bool ParseDirective(AsmToken DirectiveID);
106};
Jim Grosbach16c74252010-10-29 14:46:02 +0000107} // end anonymous namespace
108
Chris Lattner3a697562010-10-28 17:20:03 +0000109namespace {
110
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000111/// ARMOperand - Instances of this class represent a parsed ARM machine
112/// instruction.
Chris Lattner76593892010-01-14 21:21:40 +0000113struct ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000114public:
115 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000116 CondCode,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000117 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000118 Memory,
119 Register,
120 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000121 } Kind;
122
Sean Callanan76264762010-04-02 22:27:05 +0000123 SMLoc StartLoc, EndLoc;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000124
125 union {
126 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000127 ARMCC::CondCodes Val;
128 } CC;
129
130 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000131 const char *Data;
132 unsigned Length;
133 } Tok;
134
135 struct {
136 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000137 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000138 } Reg;
139
Kevin Enderbycfe07242009-10-13 22:19:02 +0000140 struct {
141 const MCExpr *Val;
142 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000143
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000144 // This is for all forms of ARM address expressions
145 struct {
146 unsigned BaseRegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000147 unsigned OffsetRegNum; // used when OffsetIsReg is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000148 const MCExpr *Offset; // used when OffsetIsReg is false
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000149 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000150 enum ShiftType ShiftType; // used when OffsetRegShifted is true
151 unsigned
152 OffsetRegShifted : 1, // only used when OffsetIsReg is true
153 Preindexed : 1,
154 Postindexed : 1,
155 OffsetIsReg : 1,
156 Negative : 1, // only used when OffsetIsReg is true
157 Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000158 } Mem;
159
160 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000161
Sean Callanan76264762010-04-02 22:27:05 +0000162 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
163 Kind = o.Kind;
164 StartLoc = o.StartLoc;
165 EndLoc = o.EndLoc;
166 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000167 case CondCode:
168 CC = o.CC;
169 break;
Sean Callanan76264762010-04-02 22:27:05 +0000170 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000171 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000172 break;
173 case Register:
174 Reg = o.Reg;
175 break;
176 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 {
201 assert(Kind == Register && "Invalid access!");
202 return Reg.RegNum;
203 }
204
Kevin Enderbycfe07242009-10-13 22:19:02 +0000205 const MCExpr *getImm() const {
206 assert(Kind == Immediate && "Invalid access!");
207 return Imm.Val;
208 }
209
Daniel Dunbar8462b302010-08-11 06:36:53 +0000210 bool isCondCode() const { return Kind == CondCode; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000211 bool isImm() const { return Kind == Immediate; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000212 bool isReg() const { return Kind == Register; }
Chris Lattner14b93852010-10-29 00:27:31 +0000213 bool isToken() const { return Kind == Token; }
214 bool isMemory() const { return Kind == Memory; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000215
216 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000217 // Add as immediates when possible. Null MCExpr = 0.
218 if (Expr == 0)
219 Inst.addOperand(MCOperand::CreateImm(0));
220 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000221 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
222 else
223 Inst.addOperand(MCOperand::CreateExpr(Expr));
224 }
225
Daniel Dunbar8462b302010-08-11 06:36:53 +0000226 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000227 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000228 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000229 // FIXME: What belongs here?
230 Inst.addOperand(MCOperand::CreateReg(0));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000231 }
232
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000233 void addRegOperands(MCInst &Inst, unsigned N) const {
234 assert(N == 1 && "Invalid number of operands!");
235 Inst.addOperand(MCOperand::CreateReg(getReg()));
236 }
237
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000238 void addImmOperands(MCInst &Inst, unsigned N) const {
239 assert(N == 1 && "Invalid number of operands!");
240 addExpr(Inst, getImm());
241 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000242
243
Chris Lattner14b93852010-10-29 00:27:31 +0000244 bool isMemMode5() const {
Chris Lattner14b93852010-10-29 00:27:31 +0000245 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
Jim Grosbach80eb2332010-10-29 17:41:25 +0000246 Mem.Writeback || Mem.Negative)
Chris Lattner14b93852010-10-29 00:27:31 +0000247 return false;
Jim Grosbach80eb2332010-10-29 17:41:25 +0000248 // If there is an offset expression, make sure it's valid.
249 if (!Mem.Offset)
250 return true;
251 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
252 if (!CE)
253 return false;
254 // The offset must be a multiple of 4 in the range 0-1020.
255 int64_t Value = CE->getValue();
256 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
Chris Lattner14b93852010-10-29 00:27:31 +0000257 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000258
Chris Lattner14b93852010-10-29 00:27:31 +0000259 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
260 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000261
Chris Lattner14b93852010-10-29 00:27:31 +0000262 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
263 assert(!Mem.OffsetIsReg && "invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000264
Jim Grosbach80eb2332010-10-29 17:41:25 +0000265 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
266 // the difference?
267 if (Mem.Offset) {
268 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000269 assert(CE && "Non-constant mode 5 offset operand!");
270
Jim Grosbach80eb2332010-10-29 17:41:25 +0000271 // The MCInst offset operand doesn't include the low two bits (like
272 // the instruction encoding).
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000273 int64_t Offset = CE->getValue() / 4;
274 if (Offset >= 0)
275 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
276 Offset)));
277 else
278 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
279 -Offset)));
280 } else {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000281 Inst.addOperand(MCOperand::CreateImm(0));
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000282 }
Chris Lattner14b93852010-10-29 00:27:31 +0000283 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000284
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000285 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000286
Chris Lattner3a697562010-10-28 17:20:03 +0000287 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
288 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000289 Op->CC.Val = CC;
290 Op->StartLoc = S;
291 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000292 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000293 }
294
Chris Lattner3a697562010-10-28 17:20:03 +0000295 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
296 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000297 Op->Tok.Data = Str.data();
298 Op->Tok.Length = Str.size();
299 Op->StartLoc = S;
300 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000301 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000302 }
303
Chris Lattner3a697562010-10-28 17:20:03 +0000304 static ARMOperand *CreateReg(unsigned RegNum, bool Writeback, SMLoc S,
305 SMLoc E) {
306 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000307 Op->Reg.RegNum = RegNum;
308 Op->Reg.Writeback = Writeback;
Sean Callanan76264762010-04-02 22:27:05 +0000309 Op->StartLoc = S;
310 Op->EndLoc = E;
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 *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
315 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000316 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000317 Op->StartLoc = S;
318 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000319 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000320 }
321
Chris Lattner3a697562010-10-28 17:20:03 +0000322 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
323 const MCExpr *Offset, unsigned OffsetRegNum,
324 bool OffsetRegShifted, enum ShiftType ShiftType,
325 const MCExpr *ShiftAmount, bool Preindexed,
326 bool Postindexed, bool Negative, bool Writeback,
327 SMLoc S, SMLoc E) {
328 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000329 Op->Mem.BaseRegNum = BaseRegNum;
330 Op->Mem.OffsetIsReg = OffsetIsReg;
331 Op->Mem.Offset = Offset;
332 Op->Mem.OffsetRegNum = OffsetRegNum;
333 Op->Mem.OffsetRegShifted = OffsetRegShifted;
334 Op->Mem.ShiftType = ShiftType;
335 Op->Mem.ShiftAmount = ShiftAmount;
336 Op->Mem.Preindexed = Preindexed;
337 Op->Mem.Postindexed = Postindexed;
338 Op->Mem.Negative = Negative;
339 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000340
Sean Callanan76264762010-04-02 22:27:05 +0000341 Op->StartLoc = S;
342 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000343 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000344 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000345
Chris Lattner3a697562010-10-28 17:20:03 +0000346private:
347 ARMOperand(KindTy K) : Kind(K) {}
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000348};
349
350} // end anonymous namespace.
351
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000352void ARMOperand::dump(raw_ostream &OS) const {
353 switch (Kind) {
354 case CondCode:
355 OS << ARMCondCodeToString(getCondCode());
356 break;
357 case Immediate:
358 getImm()->print(OS);
359 break;
360 case Memory:
361 OS << "<memory>";
362 break;
363 case Register:
364 OS << "<register " << getReg() << ">";
365 break;
366 case Token:
367 OS << "'" << getToken() << "'";
368 break;
369 }
370}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000371
372/// @name Auto-generated Match Functions
373/// {
374
375static unsigned MatchRegisterName(StringRef Name);
376
377/// }
378
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000379/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000380/// and if it is a register name the token is eaten and the register number is
381/// returned. Otherwise return -1.
382///
383int ARMAsmParser::TryParseRegister() {
384 const AsmToken &Tok = Parser.getTok();
385 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000386
Chris Lattnere5658fa2010-10-30 04:09:10 +0000387 // FIXME: Validate register for the current architecture; we have to do
388 // validation later, so maybe there is no need for this here.
389 int RegNum = MatchRegisterName(Tok.getString());
390 if (RegNum == -1)
391 return -1;
392 Parser.Lex(); // Eat identifier token.
393 return RegNum;
394}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000395
396
Chris Lattnere5658fa2010-10-30 04:09:10 +0000397/// Try to parse a register name. The token must be an Identifier when called,
398/// and if it is a register name the token is eaten and the register number is
399/// returned. Otherwise return -1.
Chris Lattner3a697562010-10-28 17:20:03 +0000400///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000401/// TODO this is likely to change to allow different register types and or to
402/// parse for a specific register type.
Chris Lattnere5658fa2010-10-30 04:09:10 +0000403ARMOperand *ARMAsmParser::TryParseRegisterWithWriteBack() {
404 SMLoc S = Parser.getTok().getLoc();
405 int RegNo = TryParseRegister();
406 if (RegNo == -1) return 0;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000407
Chris Lattnere5658fa2010-10-30 04:09:10 +0000408 SMLoc E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000409
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000410 bool Writeback = false;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000411 const AsmToken &ExclaimTok = Parser.getTok();
412 if (ExclaimTok.is(AsmToken::Exclaim)) {
413 E = ExclaimTok.getLoc();
414 Writeback = true;
415 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000416 }
417
Chris Lattnere5658fa2010-10-30 04:09:10 +0000418 return ARMOperand::CreateReg(RegNo, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000419}
420
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000421/// Parse a register list, return it if successful else return null. The first
422/// token must be a '{' when called.
423ARMOperand *ARMAsmParser::ParseRegisterList() {
Sean Callanan76264762010-04-02 22:27:05 +0000424 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000425 assert(Parser.getTok().is(AsmToken::LCurly) &&
Kevin Enderbycfe07242009-10-13 22:19:02 +0000426 "Token is not an Left Curly Brace");
Sean Callanan76264762010-04-02 22:27:05 +0000427 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000428 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000429
Sean Callanan18b83232010-01-19 21:44:56 +0000430 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000431 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000432 if (RegTok.isNot(AsmToken::Identifier)) {
433 Error(RegLoc, "register expected");
434 return 0;
435 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000436 int RegNum = MatchRegisterName(RegTok.getString());
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000437 if (RegNum == -1) {
438 Error(RegLoc, "register expected");
439 return 0;
440 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000441
Sean Callananb9a25b72010-01-19 20:27:46 +0000442 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000443 unsigned RegList = 1 << RegNum;
444
445 int HighRegNum = RegNum;
446 // TODO ranges like "{Rn-Rm}"
Sean Callanan18b83232010-01-19 21:44:56 +0000447 while (Parser.getTok().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000448 Parser.Lex(); // Eat comma token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000449
Sean Callanan18b83232010-01-19 21:44:56 +0000450 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000451 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000452 if (RegTok.isNot(AsmToken::Identifier)) {
453 Error(RegLoc, "register expected");
454 return 0;
455 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000456 int RegNum = MatchRegisterName(RegTok.getString());
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000457 if (RegNum == -1) {
458 Error(RegLoc, "register expected");
459 return 0;
460 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000461
462 if (RegList & (1 << RegNum))
463 Warning(RegLoc, "register duplicated in register list");
464 else if (RegNum <= HighRegNum)
465 Warning(RegLoc, "register not in ascending order in register list");
466 RegList |= 1 << RegNum;
467 HighRegNum = RegNum;
468
Sean Callananb9a25b72010-01-19 20:27:46 +0000469 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000470 }
Sean Callanan18b83232010-01-19 21:44:56 +0000471 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000472 if (RCurlyTok.isNot(AsmToken::RCurly)) {
473 Error(RCurlyTok.getLoc(), "'}' expected");
474 return 0;
475 }
Sean Callanan76264762010-04-02 22:27:05 +0000476 E = RCurlyTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000477 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000478
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000479 // FIXME: Need to return an operand!
480 Error(E, "FIXME: register list parsing not implemented");
481 return 0;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000482}
483
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000484/// Parse an arm memory expression, return false if successful else return true
485/// or an error. The first token must be a '[' when called.
486/// TODO Only preindexing and postindexing addressing are started, unindexed
487/// with option, etc are still to do.
Chris Lattner550276e2010-10-28 20:52:15 +0000488ARMOperand *ARMAsmParser::ParseMemory() {
Sean Callanan76264762010-04-02 22:27:05 +0000489 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000490 assert(Parser.getTok().is(AsmToken::LBrac) &&
Kevin Enderby6bd266e2009-10-12 22:51:49 +0000491 "Token is not an Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000492 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000493 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000494
Sean Callanan18b83232010-01-19 21:44:56 +0000495 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000496 if (BaseRegTok.isNot(AsmToken::Identifier)) {
497 Error(BaseRegTok.getLoc(), "register expected");
498 return 0;
499 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000500 int BaseRegNum = TryParseRegister();
501 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000502 Error(BaseRegTok.getLoc(), "register expected");
503 return 0;
504 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000505
506 bool Preindexed = false;
507 bool Postindexed = false;
508 bool OffsetIsReg = false;
509 bool Negative = false;
510 bool Writeback = false;
511
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000512 // First look for preindexed address forms, that is after the "[Rn" we now
513 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000514 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000515 if (Tok.is(AsmToken::Comma)) {
516 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000517 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000518 int OffsetRegNum;
519 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000520 enum ShiftType ShiftType;
521 const MCExpr *ShiftAmount;
522 const MCExpr *Offset;
Chris Lattner550276e2010-10-28 20:52:15 +0000523 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
524 Offset, OffsetIsReg, OffsetRegNum, E))
525 return 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000526 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000527 if (RBracTok.isNot(AsmToken::RBrac)) {
528 Error(RBracTok.getLoc(), "']' expected");
529 return 0;
530 }
Sean Callanan76264762010-04-02 22:27:05 +0000531 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000532 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000533
Sean Callanan18b83232010-01-19 21:44:56 +0000534 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000535 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000536 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000537 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000538 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000539 }
Chris Lattner550276e2010-10-28 20:52:15 +0000540 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
541 OffsetRegShifted, ShiftType, ShiftAmount,
542 Preindexed, Postindexed, Negative, Writeback,
543 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000544 }
545 // The "[Rn" we have so far was not followed by a comma.
546 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000547 // If there's anything other than the right brace, this is a post indexing
548 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000549 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000550 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000551
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000552 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000553 bool OffsetRegShifted = false;
554 enum ShiftType ShiftType;
555 const MCExpr *ShiftAmount;
Chris Lattner14b93852010-10-29 00:27:31 +0000556 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000557
Sean Callanan18b83232010-01-19 21:44:56 +0000558 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000559 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000560 Postindexed = true;
561 Writeback = true;
Chris Lattner550276e2010-10-28 20:52:15 +0000562 if (NextTok.isNot(AsmToken::Comma)) {
563 Error(NextTok.getLoc(), "',' expected");
564 return 0;
565 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000566 Parser.Lex(); // Eat comma token.
Chris Lattner550276e2010-10-28 20:52:15 +0000567 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000568 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000569 E))
570 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000571 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000572
Chris Lattner550276e2010-10-28 20:52:15 +0000573 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
574 OffsetRegShifted, ShiftType, ShiftAmount,
575 Preindexed, Postindexed, Negative, Writeback,
576 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000577 }
578
Chris Lattner550276e2010-10-28 20:52:15 +0000579 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000580}
581
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000582/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
583/// we will parse the following (were +/- means that a plus or minus is
584/// optional):
585/// +/-Rm
586/// +/-Rm, shift
587/// #offset
588/// we return false on success or an error otherwise.
589bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000590 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000591 enum ShiftType &ShiftType,
592 const MCExpr *&ShiftAmount,
593 const MCExpr *&Offset,
594 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000595 int &OffsetRegNum,
596 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000597 Negative = false;
598 OffsetRegShifted = false;
599 OffsetIsReg = false;
600 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000601 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000602 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000603 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000604 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000605 else if (NextTok.is(AsmToken::Minus)) {
606 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000607 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000608 }
609 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000610 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000611 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000612 SMLoc CurLoc = OffsetRegTok.getLoc();
613 OffsetRegNum = TryParseRegister();
614 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000615 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000616 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000617 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000618 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000619
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000620 // If we parsed a register as the offset then their can be a shift after that
621 if (OffsetRegNum != -1) {
622 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000623 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000624 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000625 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000626
Sean Callanan18b83232010-01-19 21:44:56 +0000627 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000628 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000629 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000630 OffsetRegShifted = true;
631 }
632 }
633 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
634 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000635 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000636 if (HashTok.isNot(AsmToken::Hash))
637 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000638
Sean Callananb9a25b72010-01-19 20:27:46 +0000639 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000640
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000641 if (getParser().ParseExpression(Offset))
642 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000643 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000644 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000645 return false;
646}
647
648/// ParseShift as one of these two:
649/// ( lsl | lsr | asr | ror ) , # shift_amount
650/// rrx
651/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000652bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000653 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000654 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000655 if (Tok.isNot(AsmToken::Identifier))
656 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000657 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000658 if (ShiftName == "lsl" || ShiftName == "LSL")
659 St = Lsl;
660 else if (ShiftName == "lsr" || ShiftName == "LSR")
661 St = Lsr;
662 else if (ShiftName == "asr" || ShiftName == "ASR")
663 St = Asr;
664 else if (ShiftName == "ror" || ShiftName == "ROR")
665 St = Ror;
666 else if (ShiftName == "rrx" || ShiftName == "RRX")
667 St = Rrx;
668 else
669 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000670 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000671
672 // Rrx stands alone.
673 if (St == Rrx)
674 return false;
675
676 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000677 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000678 if (HashTok.isNot(AsmToken::Hash))
679 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000680 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000681
682 if (getParser().ParseExpression(ShiftAmount))
683 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000684
685 return false;
686}
687
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000688/// Parse a arm instruction operand. For now this parses the operand regardless
689/// of the mnemonic.
Chris Lattner550276e2010-10-28 20:52:15 +0000690ARMOperand *ARMAsmParser::ParseOperand() {
Sean Callanan76264762010-04-02 22:27:05 +0000691 SMLoc S, E;
Jim Grosbach16c74252010-10-29 14:46:02 +0000692
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000693 switch (getLexer().getKind()) {
694 case AsmToken::Identifier:
Chris Lattnere5658fa2010-10-30 04:09:10 +0000695 if (ARMOperand *Op = TryParseRegisterWithWriteBack())
Chris Lattner550276e2010-10-28 20:52:15 +0000696 return Op;
Jim Grosbach16c74252010-10-29 14:46:02 +0000697
Kevin Enderby515d5092009-10-15 20:48:48 +0000698 // This was not a register so parse other operands that start with an
699 // identifier (like labels) as expressions and create them as immediates.
700 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000701 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000702 if (getParser().ParseExpression(IdVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000703 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000704 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000705 return ARMOperand::CreateImm(IdVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000706 case AsmToken::LBrac:
Chris Lattner550276e2010-10-28 20:52:15 +0000707 return ParseMemory();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000708 case AsmToken::LCurly:
Chris Lattner550276e2010-10-28 20:52:15 +0000709 return ParseRegisterList();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000710 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000711 // #42 -> immediate.
712 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000713 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000714 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000715 const MCExpr *ImmVal;
716 if (getParser().ParseExpression(ImmVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000717 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000718 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000719 return ARMOperand::CreateImm(ImmVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000720 default:
Chris Lattner550276e2010-10-28 20:52:15 +0000721 Error(Parser.getTok().getLoc(), "unexpected token in operand");
722 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000723 }
724}
725
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000726/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000727bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000728 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000729 // Create the leading tokens for the mnemonic, split by '.' characters.
730 size_t Start = 0, Next = Name.find('.');
731 StringRef Head = Name.slice(Start, Next);
732
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000733 // Determine the predicate, if any.
734 //
735 // FIXME: We need a way to check whether a prefix supports predication,
736 // otherwise we will end up with an ambiguity for instructions that happen to
737 // end with a predicate name.
Jim Grosbach3df518e2010-10-29 21:56:51 +0000738 // FIXME: Likewise, some arithmetic instructions have an 's' prefix which
739 // indicates to update the condition codes. Those instructions have an
740 // additional immediate operand which encodes the prefix as reg0 or CPSR.
741 // Just checking for a suffix of 's' definitely creates ambiguities; e.g,
742 // the SMMLS instruction.
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000743 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
744 .Case("eq", ARMCC::EQ)
745 .Case("ne", ARMCC::NE)
746 .Case("hs", ARMCC::HS)
747 .Case("lo", ARMCC::LO)
748 .Case("mi", ARMCC::MI)
749 .Case("pl", ARMCC::PL)
750 .Case("vs", ARMCC::VS)
751 .Case("vc", ARMCC::VC)
752 .Case("hi", ARMCC::HI)
753 .Case("ls", ARMCC::LS)
754 .Case("ge", ARMCC::GE)
755 .Case("lt", ARMCC::LT)
756 .Case("gt", ARMCC::GT)
757 .Case("le", ARMCC::LE)
758 .Case("al", ARMCC::AL)
759 .Default(~0U);
Jim Grosbach16c74252010-10-29 14:46:02 +0000760
Chris Lattnerdba34d82010-10-30 04:35:59 +0000761 if (CC == ~0U ||
762 (CC == ARMCC::LS && (Head == "vmls" || Head == "vnmls"))) {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000763 CC = ARMCC::AL;
Chris Lattnerdba34d82010-10-30 04:35:59 +0000764 } else {
765 Head = Head.slice(0, Head.size() - 2);
Bill Wendling52925b62010-10-29 23:50:21 +0000766 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000767
Chris Lattner3a697562010-10-28 17:20:03 +0000768 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Jim Grosbach469ebbe2010-11-01 18:11:14 +0000769 // FIXME: Should only add this operand for predicated instructions
Chris Lattner3a697562010-10-28 17:20:03 +0000770 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000771
772 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000773 while (Next != StringRef::npos) {
774 Start = Next;
775 Next = Name.find('.', Start + 1);
776 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000777
Chris Lattner3a697562010-10-28 17:20:03 +0000778 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000779 }
780
781 // Read the remaining operands.
782 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000783 // Read the first operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000784 if (ARMOperand *Op = ParseOperand())
785 Operands.push_back(Op);
786 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000787 Parser.EatToEndOfStatement();
788 return true;
789 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000790
791 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000792 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000793
794 // Parse and remember the operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000795 if (ARMOperand *Op = ParseOperand())
796 Operands.push_back(Op);
797 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000798 Parser.EatToEndOfStatement();
799 return true;
800 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000801 }
802 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000803
Chris Lattnercbf8a982010-09-11 16:18:25 +0000804 if (getLexer().isNot(AsmToken::EndOfStatement)) {
805 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000806 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000807 }
Chris Lattner34e53142010-09-08 05:10:46 +0000808 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000809 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000810}
811
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000812bool ARMAsmParser::
813MatchAndEmitInstruction(SMLoc IDLoc,
814 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
815 MCStreamer &Out) {
816 MCInst Inst;
817 unsigned ErrorInfo;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000818 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
819 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000820 Out.EmitInstruction(Inst);
821 return false;
Jim Grosbach16c74252010-10-29 14:46:02 +0000822
Chris Lattnere73d4f82010-10-28 21:41:58 +0000823 case Match_MissingFeature:
824 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
825 return true;
826 case Match_InvalidOperand: {
827 SMLoc ErrorLoc = IDLoc;
828 if (ErrorInfo != ~0U) {
829 if (ErrorInfo >= Operands.size())
830 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +0000831
Chris Lattnere73d4f82010-10-28 21:41:58 +0000832 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
833 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
834 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000835
Chris Lattnere73d4f82010-10-28 21:41:58 +0000836 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000837 }
Chris Lattnere73d4f82010-10-28 21:41:58 +0000838 case Match_MnemonicFail:
839 return Error(IDLoc, "unrecognized instruction mnemonic");
840 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000841
Eric Christopherc223e2b2010-10-29 09:26:59 +0000842 llvm_unreachable("Implement any new match types added!");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000843}
844
845
846
Kevin Enderby515d5092009-10-15 20:48:48 +0000847/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000848bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
849 StringRef IDVal = DirectiveID.getIdentifier();
850 if (IDVal == ".word")
851 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000852 else if (IDVal == ".thumb")
853 return ParseDirectiveThumb(DirectiveID.getLoc());
854 else if (IDVal == ".thumb_func")
855 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
856 else if (IDVal == ".code")
857 return ParseDirectiveCode(DirectiveID.getLoc());
858 else if (IDVal == ".syntax")
859 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000860 return true;
861}
862
863/// ParseDirectiveWord
864/// ::= .word [ expression (, expression)* ]
865bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
866 if (getLexer().isNot(AsmToken::EndOfStatement)) {
867 for (;;) {
868 const MCExpr *Value;
869 if (getParser().ParseExpression(Value))
870 return true;
871
Chris Lattneraaec2052010-01-19 19:46:13 +0000872 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000873
874 if (getLexer().is(AsmToken::EndOfStatement))
875 break;
Jim Grosbach16c74252010-10-29 14:46:02 +0000876
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000877 // FIXME: Improve diagnostic.
878 if (getLexer().isNot(AsmToken::Comma))
879 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000880 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000881 }
882 }
883
Sean Callananb9a25b72010-01-19 20:27:46 +0000884 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000885 return false;
886}
887
Kevin Enderby515d5092009-10-15 20:48:48 +0000888/// ParseDirectiveThumb
889/// ::= .thumb
890bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
891 if (getLexer().isNot(AsmToken::EndOfStatement))
892 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000893 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000894
895 // TODO: set thumb mode
896 // TODO: tell the MC streamer the mode
897 // getParser().getStreamer().Emit???();
898 return false;
899}
900
901/// ParseDirectiveThumbFunc
902/// ::= .thumbfunc symbol_name
903bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000904 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000905 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
906 return Error(L, "unexpected token in .syntax directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000907 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000908
909 if (getLexer().isNot(AsmToken::EndOfStatement))
910 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000911 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000912
913 // TODO: mark symbol as a thumb symbol
914 // getParser().getStreamer().Emit???();
915 return false;
916}
917
918/// ParseDirectiveSyntax
919/// ::= .syntax unified | divided
920bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000921 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000922 if (Tok.isNot(AsmToken::Identifier))
923 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000924 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000925 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000926 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000927 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000928 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000929 else
930 return Error(L, "unrecognized syntax mode in .syntax directive");
931
932 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000933 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000934 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000935
936 // TODO tell the MC streamer the mode
937 // getParser().getStreamer().Emit???();
938 return false;
939}
940
941/// ParseDirectiveCode
942/// ::= .code 16 | 32
943bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000944 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000945 if (Tok.isNot(AsmToken::Integer))
946 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000947 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +0000948 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +0000949 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000950 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +0000951 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000952 else
953 return Error(L, "invalid operand to .code directive");
954
955 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000956 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000957 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000958
959 // TODO tell the MC streamer the mode
960 // getParser().getStreamer().Emit???();
961 return false;
962}
963
Sean Callanan90b70972010-04-07 20:29:34 +0000964extern "C" void LLVMInitializeARMAsmLexer();
965
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000966/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000967extern "C" void LLVMInitializeARMAsmParser() {
968 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
969 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +0000970 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000971}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000972
Chris Lattner0692ee62010-09-06 19:11:01 +0000973#define GET_REGISTER_MATCHER
974#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000975#include "ARMGenAsmMatcher.inc"