blob: 2ebea1ac2b4cd6b6ec394c30b7b0fae2d5eaef55 [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 {
39 struct ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000040
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000041class ARMAsmParser : public TargetAsmParser {
42 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000043 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000044
45private:
46 MCAsmParser &getParser() const { return Parser; }
47
48 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
49
50 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
51
52 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
53
Chris Lattnere5658fa2010-10-30 04:09:10 +000054 int TryParseRegister();
55 ARMOperand *TryParseRegisterWithWriteBack();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +000056 ARMOperand *ParseRegisterList();
Chris Lattner550276e2010-10-28 20:52:15 +000057 ARMOperand *ParseMemory();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000058
Kevin Enderby9c41fa82009-10-30 22:55:57 +000059 bool ParseMemoryOffsetReg(bool &Negative,
60 bool &OffsetRegShifted,
61 enum ShiftType &ShiftType,
62 const MCExpr *&ShiftAmount,
63 const MCExpr *&Offset,
64 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000065 int &OffsetRegNum,
66 SMLoc &E);
Kevin Enderby9c41fa82009-10-30 22:55:57 +000067
Sean Callanan76264762010-04-02 22:27:05 +000068 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000069
Chris Lattner550276e2010-10-28 20:52:15 +000070 ARMOperand *ParseOperand();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000071
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000072 bool ParseDirectiveWord(unsigned Size, SMLoc L);
73
Kevin Enderby515d5092009-10-15 20:48:48 +000074 bool ParseDirectiveThumb(SMLoc L);
75
76 bool ParseDirectiveThumbFunc(SMLoc L);
77
78 bool ParseDirectiveCode(SMLoc L);
79
80 bool ParseDirectiveSyntax(SMLoc L);
81
Chris Lattner7036f8b2010-09-29 01:42:58 +000082 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000083 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000084 MCStreamer &Out);
Jim Grosbach16c74252010-10-29 14:46:02 +000085
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000086 /// @name Auto-generated Match Functions
87 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000088
Chris Lattner0692ee62010-09-06 19:11:01 +000089#define GET_ASSEMBLER_HEADER
90#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000091
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000092 /// }
93
94
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000095public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000096 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +000097 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
98 // Initialize the set of available features.
99 setAvailableFeatures(ComputeAvailableFeatures(
100 &TM.getSubtarget<ARMSubtarget>()));
101 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000102
Benjamin Kramer38e59892010-07-14 22:38:02 +0000103 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000104 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000105
106 virtual bool ParseDirective(AsmToken DirectiveID);
107};
Jim Grosbach16c74252010-10-29 14:46:02 +0000108} // end anonymous namespace
109
Chris Lattner3a697562010-10-28 17:20:03 +0000110namespace {
111
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000112/// ARMOperand - Instances of this class represent a parsed ARM machine
113/// instruction.
Chris Lattner76593892010-01-14 21:21:40 +0000114struct ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000115public:
116 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000117 CondCode,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000118 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000119 Memory,
120 Register,
121 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000122 } Kind;
123
Sean Callanan76264762010-04-02 22:27:05 +0000124 SMLoc StartLoc, EndLoc;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000125
126 union {
127 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000128 ARMCC::CondCodes Val;
129 } CC;
130
131 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000132 const char *Data;
133 unsigned Length;
134 } Tok;
135
136 struct {
137 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000138 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000139 } Reg;
140
Kevin Enderbycfe07242009-10-13 22:19:02 +0000141 struct {
142 const MCExpr *Val;
143 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000144
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000145 // This is for all forms of ARM address expressions
146 struct {
147 unsigned BaseRegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000148 unsigned OffsetRegNum; // used when OffsetIsReg is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000149 const MCExpr *Offset; // used when OffsetIsReg is false
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000150 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000151 enum ShiftType ShiftType; // used when OffsetRegShifted is true
152 unsigned
153 OffsetRegShifted : 1, // only used when OffsetIsReg is true
154 Preindexed : 1,
155 Postindexed : 1,
156 OffsetIsReg : 1,
157 Negative : 1, // only used when OffsetIsReg is true
158 Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000159 } Mem;
160
161 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000162
Sean Callanan76264762010-04-02 22:27:05 +0000163 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
164 Kind = o.Kind;
165 StartLoc = o.StartLoc;
166 EndLoc = o.EndLoc;
167 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000168 case CondCode:
169 CC = o.CC;
170 break;
Sean Callanan76264762010-04-02 22:27:05 +0000171 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000172 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000173 break;
174 case Register:
175 Reg = o.Reg;
176 break;
177 case Immediate:
178 Imm = o.Imm;
179 break;
180 case Memory:
181 Mem = o.Mem;
182 break;
183 }
184 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000185
Sean Callanan76264762010-04-02 22:27:05 +0000186 /// getStartLoc - Get the location of the first token of this operand.
187 SMLoc getStartLoc() const { return StartLoc; }
188 /// getEndLoc - Get the location of the last token of this operand.
189 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000190
Daniel Dunbar8462b302010-08-11 06:36:53 +0000191 ARMCC::CondCodes getCondCode() const {
192 assert(Kind == CondCode && "Invalid access!");
193 return CC.Val;
194 }
195
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000196 StringRef getToken() const {
197 assert(Kind == Token && "Invalid access!");
198 return StringRef(Tok.Data, Tok.Length);
199 }
200
201 unsigned getReg() const {
202 assert(Kind == Register && "Invalid access!");
203 return Reg.RegNum;
204 }
205
Kevin Enderbycfe07242009-10-13 22:19:02 +0000206 const MCExpr *getImm() const {
207 assert(Kind == Immediate && "Invalid access!");
208 return Imm.Val;
209 }
210
Daniel Dunbar8462b302010-08-11 06:36:53 +0000211 bool isCondCode() const { return Kind == CondCode; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000212 bool isImm() const { return Kind == Immediate; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000213 bool isReg() const { return Kind == Register; }
Chris Lattner14b93852010-10-29 00:27:31 +0000214 bool isToken() const { return Kind == Token; }
215 bool isMemory() const { return Kind == Memory; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000216
217 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000218 // Add as immediates when possible. Null MCExpr = 0.
219 if (Expr == 0)
220 Inst.addOperand(MCOperand::CreateImm(0));
221 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000222 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
223 else
224 Inst.addOperand(MCOperand::CreateExpr(Expr));
225 }
226
Daniel Dunbar8462b302010-08-11 06:36:53 +0000227 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000228 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000229 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000230 // FIXME: What belongs here?
231 Inst.addOperand(MCOperand::CreateReg(0));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000232 }
233
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000234 void addRegOperands(MCInst &Inst, unsigned N) const {
235 assert(N == 1 && "Invalid number of operands!");
236 Inst.addOperand(MCOperand::CreateReg(getReg()));
237 }
238
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000239 void addImmOperands(MCInst &Inst, unsigned N) const {
240 assert(N == 1 && "Invalid number of operands!");
241 addExpr(Inst, getImm());
242 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000243
244
Chris Lattner14b93852010-10-29 00:27:31 +0000245 bool isMemMode5() const {
Chris Lattner14b93852010-10-29 00:27:31 +0000246 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
Jim Grosbach80eb2332010-10-29 17:41:25 +0000247 Mem.Writeback || Mem.Negative)
Chris Lattner14b93852010-10-29 00:27:31 +0000248 return false;
Jim Grosbach80eb2332010-10-29 17:41:25 +0000249 // If there is an offset expression, make sure it's valid.
250 if (!Mem.Offset)
251 return true;
252 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
253 if (!CE)
254 return false;
255 // The offset must be a multiple of 4 in the range 0-1020.
256 int64_t Value = CE->getValue();
257 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
Chris Lattner14b93852010-10-29 00:27:31 +0000258 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000259
Chris Lattner14b93852010-10-29 00:27:31 +0000260 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
261 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000262
Chris Lattner14b93852010-10-29 00:27:31 +0000263 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
264 assert(!Mem.OffsetIsReg && "invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000265
Jim Grosbach80eb2332010-10-29 17:41:25 +0000266 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
267 // the difference?
268 if (Mem.Offset) {
269 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000270 assert(CE && "Non-constant mode 5 offset operand!");
271
Jim Grosbach80eb2332010-10-29 17:41:25 +0000272 // The MCInst offset operand doesn't include the low two bits (like
273 // the instruction encoding).
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000274 int64_t Offset = CE->getValue() / 4;
275 if (Offset >= 0)
276 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
277 Offset)));
278 else
279 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
280 -Offset)));
281 } else {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000282 Inst.addOperand(MCOperand::CreateImm(0));
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000283 }
Chris Lattner14b93852010-10-29 00:27:31 +0000284 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000285
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000286 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000287
Chris Lattner3a697562010-10-28 17:20:03 +0000288 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
289 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000290 Op->CC.Val = CC;
291 Op->StartLoc = S;
292 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000293 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000294 }
295
Chris Lattner3a697562010-10-28 17:20:03 +0000296 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
297 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000298 Op->Tok.Data = Str.data();
299 Op->Tok.Length = Str.size();
300 Op->StartLoc = S;
301 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000302 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000303 }
304
Chris Lattner3a697562010-10-28 17:20:03 +0000305 static ARMOperand *CreateReg(unsigned RegNum, bool Writeback, SMLoc S,
306 SMLoc E) {
307 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000308 Op->Reg.RegNum = RegNum;
309 Op->Reg.Writeback = Writeback;
Sean Callanan76264762010-04-02 22:27:05 +0000310 Op->StartLoc = S;
311 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000312 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000313 }
314
Chris Lattner3a697562010-10-28 17:20:03 +0000315 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
316 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000317 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000318 Op->StartLoc = S;
319 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000320 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000321 }
322
Chris Lattner3a697562010-10-28 17:20:03 +0000323 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
324 const MCExpr *Offset, unsigned OffsetRegNum,
325 bool OffsetRegShifted, enum ShiftType ShiftType,
326 const MCExpr *ShiftAmount, bool Preindexed,
327 bool Postindexed, bool Negative, bool Writeback,
328 SMLoc S, SMLoc E) {
329 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000330 Op->Mem.BaseRegNum = BaseRegNum;
331 Op->Mem.OffsetIsReg = OffsetIsReg;
332 Op->Mem.Offset = Offset;
333 Op->Mem.OffsetRegNum = OffsetRegNum;
334 Op->Mem.OffsetRegShifted = OffsetRegShifted;
335 Op->Mem.ShiftType = ShiftType;
336 Op->Mem.ShiftAmount = ShiftAmount;
337 Op->Mem.Preindexed = Preindexed;
338 Op->Mem.Postindexed = Postindexed;
339 Op->Mem.Negative = Negative;
340 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000341
Sean Callanan76264762010-04-02 22:27:05 +0000342 Op->StartLoc = S;
343 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000344 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000345 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000346
Chris Lattner3a697562010-10-28 17:20:03 +0000347private:
348 ARMOperand(KindTy K) : Kind(K) {}
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000349};
350
351} // end anonymous namespace.
352
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000353void ARMOperand::dump(raw_ostream &OS) const {
354 switch (Kind) {
355 case CondCode:
356 OS << ARMCondCodeToString(getCondCode());
357 break;
358 case Immediate:
359 getImm()->print(OS);
360 break;
361 case Memory:
362 OS << "<memory>";
363 break;
364 case Register:
365 OS << "<register " << getReg() << ">";
366 break;
367 case Token:
368 OS << "'" << getToken() << "'";
369 break;
370 }
371}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000372
373/// @name Auto-generated Match Functions
374/// {
375
376static unsigned MatchRegisterName(StringRef Name);
377
378/// }
379
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000380/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000381/// and if it is a register name the token is eaten and the register number is
382/// returned. Otherwise return -1.
383///
384int ARMAsmParser::TryParseRegister() {
385 const AsmToken &Tok = Parser.getTok();
386 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000387
Chris Lattnere5658fa2010-10-30 04:09:10 +0000388 // FIXME: Validate register for the current architecture; we have to do
389 // validation later, so maybe there is no need for this here.
390 int RegNum = MatchRegisterName(Tok.getString());
391 if (RegNum == -1)
392 return -1;
393 Parser.Lex(); // Eat identifier token.
394 return RegNum;
395}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000396
397
Chris Lattnere5658fa2010-10-30 04:09:10 +0000398/// Try to parse a register name. The token must be an Identifier when called,
399/// and if it is a register name the token is eaten and the register number is
400/// returned. Otherwise return -1.
Chris Lattner3a697562010-10-28 17:20:03 +0000401///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000402/// TODO this is likely to change to allow different register types and or to
403/// parse for a specific register type.
Chris Lattnere5658fa2010-10-30 04:09:10 +0000404ARMOperand *ARMAsmParser::TryParseRegisterWithWriteBack() {
405 SMLoc S = Parser.getTok().getLoc();
406 int RegNo = TryParseRegister();
407 if (RegNo == -1) return 0;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000408
Chris Lattnere5658fa2010-10-30 04:09:10 +0000409 SMLoc E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000410
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000411 bool Writeback = false;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000412 const AsmToken &ExclaimTok = Parser.getTok();
413 if (ExclaimTok.is(AsmToken::Exclaim)) {
414 E = ExclaimTok.getLoc();
415 Writeback = true;
416 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000417 }
418
Chris Lattnere5658fa2010-10-30 04:09:10 +0000419 return ARMOperand::CreateReg(RegNo, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000420}
421
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000422/// Parse a register list, return it if successful else return null. The first
423/// token must be a '{' when called.
424ARMOperand *ARMAsmParser::ParseRegisterList() {
Sean Callanan76264762010-04-02 22:27:05 +0000425 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000426 assert(Parser.getTok().is(AsmToken::LCurly) &&
Kevin Enderbycfe07242009-10-13 22:19:02 +0000427 "Token is not an Left Curly Brace");
Sean Callanan76264762010-04-02 22:27:05 +0000428 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000429 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000430
Sean Callanan18b83232010-01-19 21:44:56 +0000431 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000432 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000433 if (RegTok.isNot(AsmToken::Identifier)) {
434 Error(RegLoc, "register expected");
435 return 0;
436 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000437 int RegNum = MatchRegisterName(RegTok.getString());
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000438 if (RegNum == -1) {
439 Error(RegLoc, "register expected");
440 return 0;
441 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000442
Sean Callananb9a25b72010-01-19 20:27:46 +0000443 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000444 unsigned RegList = 1 << RegNum;
445
446 int HighRegNum = RegNum;
447 // TODO ranges like "{Rn-Rm}"
Sean Callanan18b83232010-01-19 21:44:56 +0000448 while (Parser.getTok().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000449 Parser.Lex(); // Eat comma token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000450
Sean Callanan18b83232010-01-19 21:44:56 +0000451 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000452 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000453 if (RegTok.isNot(AsmToken::Identifier)) {
454 Error(RegLoc, "register expected");
455 return 0;
456 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000457 int RegNum = MatchRegisterName(RegTok.getString());
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000458 if (RegNum == -1) {
459 Error(RegLoc, "register expected");
460 return 0;
461 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000462
463 if (RegList & (1 << RegNum))
464 Warning(RegLoc, "register duplicated in register list");
465 else if (RegNum <= HighRegNum)
466 Warning(RegLoc, "register not in ascending order in register list");
467 RegList |= 1 << RegNum;
468 HighRegNum = RegNum;
469
Sean Callananb9a25b72010-01-19 20:27:46 +0000470 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000471 }
Sean Callanan18b83232010-01-19 21:44:56 +0000472 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000473 if (RCurlyTok.isNot(AsmToken::RCurly)) {
474 Error(RCurlyTok.getLoc(), "'}' expected");
475 return 0;
476 }
Sean Callanan76264762010-04-02 22:27:05 +0000477 E = RCurlyTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000478 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000479
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000480 // FIXME: Need to return an operand!
481 Error(E, "FIXME: register list parsing not implemented");
482 return 0;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000483}
484
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000485/// Parse an arm memory expression, return false if successful else return true
486/// or an error. The first token must be a '[' when called.
487/// TODO Only preindexing and postindexing addressing are started, unindexed
488/// with option, etc are still to do.
Chris Lattner550276e2010-10-28 20:52:15 +0000489ARMOperand *ARMAsmParser::ParseMemory() {
Sean Callanan76264762010-04-02 22:27:05 +0000490 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000491 assert(Parser.getTok().is(AsmToken::LBrac) &&
Kevin Enderby6bd266e2009-10-12 22:51:49 +0000492 "Token is not an Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000493 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000494 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000495
Sean Callanan18b83232010-01-19 21:44:56 +0000496 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000497 if (BaseRegTok.isNot(AsmToken::Identifier)) {
498 Error(BaseRegTok.getLoc(), "register expected");
499 return 0;
500 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000501 int BaseRegNum = TryParseRegister();
502 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000503 Error(BaseRegTok.getLoc(), "register expected");
504 return 0;
505 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000506
507 bool Preindexed = false;
508 bool Postindexed = false;
509 bool OffsetIsReg = false;
510 bool Negative = false;
511 bool Writeback = false;
512
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000513 // First look for preindexed address forms, that is after the "[Rn" we now
514 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000515 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000516 if (Tok.is(AsmToken::Comma)) {
517 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000518 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000519 int OffsetRegNum;
520 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000521 enum ShiftType ShiftType;
522 const MCExpr *ShiftAmount;
523 const MCExpr *Offset;
Chris Lattner550276e2010-10-28 20:52:15 +0000524 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
525 Offset, OffsetIsReg, OffsetRegNum, E))
526 return 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000527 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000528 if (RBracTok.isNot(AsmToken::RBrac)) {
529 Error(RBracTok.getLoc(), "']' expected");
530 return 0;
531 }
Sean Callanan76264762010-04-02 22:27:05 +0000532 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000533 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000534
Sean Callanan18b83232010-01-19 21:44:56 +0000535 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000536 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000537 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000538 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000539 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000540 }
Chris Lattner550276e2010-10-28 20:52:15 +0000541 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
542 OffsetRegShifted, ShiftType, ShiftAmount,
543 Preindexed, Postindexed, Negative, Writeback,
544 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000545 }
546 // The "[Rn" we have so far was not followed by a comma.
547 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000548 // If there's anything other than the right brace, this is a post indexing
549 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000550 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000551 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000552
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000553 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000554 bool OffsetRegShifted = false;
555 enum ShiftType ShiftType;
556 const MCExpr *ShiftAmount;
Chris Lattner14b93852010-10-29 00:27:31 +0000557 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000558
Sean Callanan18b83232010-01-19 21:44:56 +0000559 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000560 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000561 Postindexed = true;
562 Writeback = true;
Chris Lattner550276e2010-10-28 20:52:15 +0000563 if (NextTok.isNot(AsmToken::Comma)) {
564 Error(NextTok.getLoc(), "',' expected");
565 return 0;
566 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000567 Parser.Lex(); // Eat comma token.
Chris Lattner550276e2010-10-28 20:52:15 +0000568 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000569 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000570 E))
571 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000572 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000573
Chris Lattner550276e2010-10-28 20:52:15 +0000574 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
575 OffsetRegShifted, ShiftType, ShiftAmount,
576 Preindexed, Postindexed, Negative, Writeback,
577 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000578 }
579
Chris Lattner550276e2010-10-28 20:52:15 +0000580 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000581}
582
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000583/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
584/// we will parse the following (were +/- means that a plus or minus is
585/// optional):
586/// +/-Rm
587/// +/-Rm, shift
588/// #offset
589/// we return false on success or an error otherwise.
590bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000591 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000592 enum ShiftType &ShiftType,
593 const MCExpr *&ShiftAmount,
594 const MCExpr *&Offset,
595 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000596 int &OffsetRegNum,
597 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000598 Negative = false;
599 OffsetRegShifted = false;
600 OffsetIsReg = false;
601 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000602 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000603 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000604 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000605 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000606 else if (NextTok.is(AsmToken::Minus)) {
607 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000608 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000609 }
610 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000611 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000612 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000613 SMLoc CurLoc = OffsetRegTok.getLoc();
614 OffsetRegNum = TryParseRegister();
615 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000616 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000617 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000618 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000619 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000620
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000621 // If we parsed a register as the offset then their can be a shift after that
622 if (OffsetRegNum != -1) {
623 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000624 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000625 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000626 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000627
Sean Callanan18b83232010-01-19 21:44:56 +0000628 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000629 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000630 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000631 OffsetRegShifted = true;
632 }
633 }
634 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
635 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000636 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000637 if (HashTok.isNot(AsmToken::Hash))
638 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000639
Sean Callananb9a25b72010-01-19 20:27:46 +0000640 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000641
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000642 if (getParser().ParseExpression(Offset))
643 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000644 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000645 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000646 return false;
647}
648
649/// ParseShift as one of these two:
650/// ( lsl | lsr | asr | ror ) , # shift_amount
651/// rrx
652/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000653bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000654 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000655 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000656 if (Tok.isNot(AsmToken::Identifier))
657 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000658 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000659 if (ShiftName == "lsl" || ShiftName == "LSL")
660 St = Lsl;
661 else if (ShiftName == "lsr" || ShiftName == "LSR")
662 St = Lsr;
663 else if (ShiftName == "asr" || ShiftName == "ASR")
664 St = Asr;
665 else if (ShiftName == "ror" || ShiftName == "ROR")
666 St = Ror;
667 else if (ShiftName == "rrx" || ShiftName == "RRX")
668 St = Rrx;
669 else
670 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000671 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000672
673 // Rrx stands alone.
674 if (St == Rrx)
675 return false;
676
677 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000678 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000679 if (HashTok.isNot(AsmToken::Hash))
680 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000681 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000682
683 if (getParser().ParseExpression(ShiftAmount))
684 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000685
686 return false;
687}
688
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000689/// Parse a arm instruction operand. For now this parses the operand regardless
690/// of the mnemonic.
Chris Lattner550276e2010-10-28 20:52:15 +0000691ARMOperand *ARMAsmParser::ParseOperand() {
Sean Callanan76264762010-04-02 22:27:05 +0000692 SMLoc S, E;
Jim Grosbach16c74252010-10-29 14:46:02 +0000693
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000694 switch (getLexer().getKind()) {
695 case AsmToken::Identifier:
Chris Lattnere5658fa2010-10-30 04:09:10 +0000696 if (ARMOperand *Op = TryParseRegisterWithWriteBack())
Chris Lattner550276e2010-10-28 20:52:15 +0000697 return Op;
Jim Grosbach16c74252010-10-29 14:46:02 +0000698
Kevin Enderby515d5092009-10-15 20:48:48 +0000699 // This was not a register so parse other operands that start with an
700 // identifier (like labels) as expressions and create them as immediates.
701 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000702 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000703 if (getParser().ParseExpression(IdVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000704 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000705 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000706 return ARMOperand::CreateImm(IdVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000707 case AsmToken::LBrac:
Chris Lattner550276e2010-10-28 20:52:15 +0000708 return ParseMemory();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000709 case AsmToken::LCurly:
Chris Lattner550276e2010-10-28 20:52:15 +0000710 return ParseRegisterList();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000711 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000712 // #42 -> immediate.
713 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000714 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000715 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000716 const MCExpr *ImmVal;
717 if (getParser().ParseExpression(ImmVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000718 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000719 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000720 return ARMOperand::CreateImm(ImmVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000721 default:
Chris Lattner550276e2010-10-28 20:52:15 +0000722 Error(Parser.getTok().getLoc(), "unexpected token in operand");
723 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000724 }
725}
726
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000727/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000728bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000729 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000730 // Create the leading tokens for the mnemonic, split by '.' characters.
731 size_t Start = 0, Next = Name.find('.');
732 StringRef Head = Name.slice(Start, Next);
733
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000734 // Determine the predicate, if any.
735 //
736 // FIXME: We need a way to check whether a prefix supports predication,
737 // otherwise we will end up with an ambiguity for instructions that happen to
738 // end with a predicate name.
Jim Grosbach3df518e2010-10-29 21:56:51 +0000739 // FIXME: Likewise, some arithmetic instructions have an 's' prefix which
740 // indicates to update the condition codes. Those instructions have an
741 // additional immediate operand which encodes the prefix as reg0 or CPSR.
742 // Just checking for a suffix of 's' definitely creates ambiguities; e.g,
743 // the SMMLS instruction.
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000744 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
745 .Case("eq", ARMCC::EQ)
746 .Case("ne", ARMCC::NE)
747 .Case("hs", ARMCC::HS)
748 .Case("lo", ARMCC::LO)
749 .Case("mi", ARMCC::MI)
750 .Case("pl", ARMCC::PL)
751 .Case("vs", ARMCC::VS)
752 .Case("vc", ARMCC::VC)
753 .Case("hi", ARMCC::HI)
754 .Case("ls", ARMCC::LS)
755 .Case("ge", ARMCC::GE)
756 .Case("lt", ARMCC::LT)
757 .Case("gt", ARMCC::GT)
758 .Case("le", ARMCC::LE)
759 .Case("al", ARMCC::AL)
760 .Default(~0U);
Jim Grosbach16c74252010-10-29 14:46:02 +0000761
Chris Lattnerdba34d82010-10-30 04:35:59 +0000762 if (CC == ~0U ||
763 (CC == ARMCC::LS && (Head == "vmls" || Head == "vnmls"))) {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000764 CC = ARMCC::AL;
Chris Lattnerdba34d82010-10-30 04:35:59 +0000765 } else {
766 Head = Head.slice(0, Head.size() - 2);
Bill Wendling52925b62010-10-29 23:50:21 +0000767 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000768
Chris Lattner3a697562010-10-28 17:20:03 +0000769 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Jim Grosbach469ebbe2010-11-01 18:11:14 +0000770 // FIXME: Should only add this operand for predicated instructions
Chris Lattner3a697562010-10-28 17:20:03 +0000771 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000772
773 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000774 while (Next != StringRef::npos) {
775 Start = Next;
776 Next = Name.find('.', Start + 1);
777 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000778
Chris Lattner3a697562010-10-28 17:20:03 +0000779 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000780 }
781
782 // Read the remaining operands.
783 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000784 // Read the first operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000785 if (ARMOperand *Op = ParseOperand())
786 Operands.push_back(Op);
787 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000788 Parser.EatToEndOfStatement();
789 return true;
790 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000791
792 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000793 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000794
795 // Parse and remember the operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000796 if (ARMOperand *Op = ParseOperand())
797 Operands.push_back(Op);
798 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000799 Parser.EatToEndOfStatement();
800 return true;
801 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000802 }
803 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000804
Chris Lattnercbf8a982010-09-11 16:18:25 +0000805 if (getLexer().isNot(AsmToken::EndOfStatement)) {
806 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000807 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000808 }
Chris Lattner34e53142010-09-08 05:10:46 +0000809 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000810 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000811}
812
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000813bool ARMAsmParser::
814MatchAndEmitInstruction(SMLoc IDLoc,
815 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
816 MCStreamer &Out) {
817 MCInst Inst;
818 unsigned ErrorInfo;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000819 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
820 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000821 Out.EmitInstruction(Inst);
822 return false;
Jim Grosbach16c74252010-10-29 14:46:02 +0000823
Chris Lattnere73d4f82010-10-28 21:41:58 +0000824 case Match_MissingFeature:
825 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
826 return true;
827 case Match_InvalidOperand: {
828 SMLoc ErrorLoc = IDLoc;
829 if (ErrorInfo != ~0U) {
830 if (ErrorInfo >= Operands.size())
831 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +0000832
Chris Lattnere73d4f82010-10-28 21:41:58 +0000833 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
834 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
835 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000836
Chris Lattnere73d4f82010-10-28 21:41:58 +0000837 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000838 }
Chris Lattnere73d4f82010-10-28 21:41:58 +0000839 case Match_MnemonicFail:
840 return Error(IDLoc, "unrecognized instruction mnemonic");
841 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000842
Eric Christopherc223e2b2010-10-29 09:26:59 +0000843 llvm_unreachable("Implement any new match types added!");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000844}
845
846
847
Kevin Enderby515d5092009-10-15 20:48:48 +0000848/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000849bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
850 StringRef IDVal = DirectiveID.getIdentifier();
851 if (IDVal == ".word")
852 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000853 else if (IDVal == ".thumb")
854 return ParseDirectiveThumb(DirectiveID.getLoc());
855 else if (IDVal == ".thumb_func")
856 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
857 else if (IDVal == ".code")
858 return ParseDirectiveCode(DirectiveID.getLoc());
859 else if (IDVal == ".syntax")
860 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000861 return true;
862}
863
864/// ParseDirectiveWord
865/// ::= .word [ expression (, expression)* ]
866bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
867 if (getLexer().isNot(AsmToken::EndOfStatement)) {
868 for (;;) {
869 const MCExpr *Value;
870 if (getParser().ParseExpression(Value))
871 return true;
872
Chris Lattneraaec2052010-01-19 19:46:13 +0000873 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000874
875 if (getLexer().is(AsmToken::EndOfStatement))
876 break;
Jim Grosbach16c74252010-10-29 14:46:02 +0000877
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000878 // FIXME: Improve diagnostic.
879 if (getLexer().isNot(AsmToken::Comma))
880 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000881 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000882 }
883 }
884
Sean Callananb9a25b72010-01-19 20:27:46 +0000885 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000886 return false;
887}
888
Kevin Enderby515d5092009-10-15 20:48:48 +0000889/// ParseDirectiveThumb
890/// ::= .thumb
891bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
892 if (getLexer().isNot(AsmToken::EndOfStatement))
893 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000894 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000895
896 // TODO: set thumb mode
897 // TODO: tell the MC streamer the mode
898 // getParser().getStreamer().Emit???();
899 return false;
900}
901
902/// ParseDirectiveThumbFunc
903/// ::= .thumbfunc symbol_name
904bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000905 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000906 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +0000907 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000908 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +0000909 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000910 if (getLexer().isNot(AsmToken::EndOfStatement))
911 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000912 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000913
Jim Grosbach642fc9c2010-11-05 22:33:53 +0000914 // Mark symbol as a thumb symbol.
915 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
916 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +0000917 return false;
918}
919
920/// ParseDirectiveSyntax
921/// ::= .syntax unified | divided
922bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000923 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000924 if (Tok.isNot(AsmToken::Identifier))
925 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000926 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000927 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000928 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000929 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000930 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000931 else
932 return Error(L, "unrecognized syntax mode in .syntax directive");
933
934 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000935 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000936 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000937
938 // TODO tell the MC streamer the mode
939 // getParser().getStreamer().Emit???();
940 return false;
941}
942
943/// ParseDirectiveCode
944/// ::= .code 16 | 32
945bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000946 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000947 if (Tok.isNot(AsmToken::Integer))
948 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000949 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +0000950 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +0000951 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000952 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +0000953 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000954 else
955 return Error(L, "invalid operand to .code directive");
956
957 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000958 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000959 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000960
961 // TODO tell the MC streamer the mode
962 // getParser().getStreamer().Emit???();
963 return false;
964}
965
Sean Callanan90b70972010-04-07 20:29:34 +0000966extern "C" void LLVMInitializeARMAsmLexer();
967
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000968/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000969extern "C" void LLVMInitializeARMAsmParser() {
970 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
971 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +0000972 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000973}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000974
Chris Lattner0692ee62010-09-06 19:11:01 +0000975#define GET_REGISTER_MATCHER
976#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000977#include "ARMGenAsmMatcher.inc"