blob: 275fcb98a7367a132fda001d0cb64cdc61f81dd8 [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"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000011#include "ARMSubtarget.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000012#include "llvm/MC/MCParser/MCAsmLexer.h"
13#include "llvm/MC/MCParser/MCAsmParser.h"
14#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000015#include "llvm/MC/MCStreamer.h"
16#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCInst.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000018#include "llvm/Target/TargetRegistry.h"
19#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000020#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000022#include "llvm/ADT/SmallVector.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000023#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000025using namespace llvm;
26
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000027// The shift types for register controlled shifts in arm memory addressing
28enum ShiftType {
29 Lsl,
30 Lsr,
31 Asr,
32 Ror,
33 Rrx
34};
35
Chris Lattner3a697562010-10-28 17:20:03 +000036namespace {
37 struct ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000038
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000039class ARMAsmParser : public TargetAsmParser {
40 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000041 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000042
43private:
44 MCAsmParser &getParser() const { return Parser; }
45
46 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
47
48 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
49
50 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
51
Chris Lattnere5658fa2010-10-30 04:09:10 +000052 int TryParseRegister();
53 ARMOperand *TryParseRegisterWithWriteBack();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +000054 ARMOperand *ParseRegisterList();
Chris Lattner550276e2010-10-28 20:52:15 +000055 ARMOperand *ParseMemory();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000056
Kevin Enderby9c41fa82009-10-30 22:55:57 +000057 bool ParseMemoryOffsetReg(bool &Negative,
58 bool &OffsetRegShifted,
59 enum ShiftType &ShiftType,
60 const MCExpr *&ShiftAmount,
61 const MCExpr *&Offset,
62 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000063 int &OffsetRegNum,
64 SMLoc &E);
Kevin Enderby9c41fa82009-10-30 22:55:57 +000065
Sean Callanan76264762010-04-02 22:27:05 +000066 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000067
Chris Lattner550276e2010-10-28 20:52:15 +000068 ARMOperand *ParseOperand();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000069
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000070 bool ParseDirectiveWord(unsigned Size, SMLoc L);
71
Kevin Enderby515d5092009-10-15 20:48:48 +000072 bool ParseDirectiveThumb(SMLoc L);
73
74 bool ParseDirectiveThumbFunc(SMLoc L);
75
76 bool ParseDirectiveCode(SMLoc L);
77
78 bool ParseDirectiveSyntax(SMLoc L);
79
Chris Lattner7036f8b2010-09-29 01:42:58 +000080 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000081 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000082 MCStreamer &Out);
Jim Grosbach16c74252010-10-29 14:46:02 +000083
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000084 /// @name Auto-generated Match Functions
85 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000086
Chris Lattner0692ee62010-09-06 19:11:01 +000087#define GET_ASSEMBLER_HEADER
88#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000089
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000090 /// }
91
92
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000093public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000094 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
95 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {}
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000096
Benjamin Kramer38e59892010-07-14 22:38:02 +000097 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000098 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000099
100 virtual bool ParseDirective(AsmToken DirectiveID);
101};
Jim Grosbach16c74252010-10-29 14:46:02 +0000102} // end anonymous namespace
103
Chris Lattner3a697562010-10-28 17:20:03 +0000104namespace {
105
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000106/// ARMOperand - Instances of this class represent a parsed ARM machine
107/// instruction.
Chris Lattner76593892010-01-14 21:21:40 +0000108struct ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000109public:
110 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000111 CondCode,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000112 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000113 Memory,
114 Register,
115 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000116 } Kind;
117
Sean Callanan76264762010-04-02 22:27:05 +0000118 SMLoc StartLoc, EndLoc;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000119
120 union {
121 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000122 ARMCC::CondCodes Val;
123 } CC;
124
125 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000126 const char *Data;
127 unsigned Length;
128 } Tok;
129
130 struct {
131 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000132 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000133 } Reg;
134
Kevin Enderbycfe07242009-10-13 22:19:02 +0000135 struct {
136 const MCExpr *Val;
137 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000138
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000139 // This is for all forms of ARM address expressions
140 struct {
141 unsigned BaseRegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000142 unsigned OffsetRegNum; // used when OffsetIsReg is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000143 const MCExpr *Offset; // used when OffsetIsReg is false
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000144 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000145 enum ShiftType ShiftType; // used when OffsetRegShifted is true
146 unsigned
147 OffsetRegShifted : 1, // only used when OffsetIsReg is true
148 Preindexed : 1,
149 Postindexed : 1,
150 OffsetIsReg : 1,
151 Negative : 1, // only used when OffsetIsReg is true
152 Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000153 } Mem;
154
155 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000156
Sean Callanan76264762010-04-02 22:27:05 +0000157 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
158 Kind = o.Kind;
159 StartLoc = o.StartLoc;
160 EndLoc = o.EndLoc;
161 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000162 case CondCode:
163 CC = o.CC;
164 break;
Sean Callanan76264762010-04-02 22:27:05 +0000165 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000166 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000167 break;
168 case Register:
169 Reg = o.Reg;
170 break;
171 case Immediate:
172 Imm = o.Imm;
173 break;
174 case Memory:
175 Mem = o.Mem;
176 break;
177 }
178 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000179
Sean Callanan76264762010-04-02 22:27:05 +0000180 /// getStartLoc - Get the location of the first token of this operand.
181 SMLoc getStartLoc() const { return StartLoc; }
182 /// getEndLoc - Get the location of the last token of this operand.
183 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000184
Daniel Dunbar8462b302010-08-11 06:36:53 +0000185 ARMCC::CondCodes getCondCode() const {
186 assert(Kind == CondCode && "Invalid access!");
187 return CC.Val;
188 }
189
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000190 StringRef getToken() const {
191 assert(Kind == Token && "Invalid access!");
192 return StringRef(Tok.Data, Tok.Length);
193 }
194
195 unsigned getReg() const {
196 assert(Kind == Register && "Invalid access!");
197 return Reg.RegNum;
198 }
199
Kevin Enderbycfe07242009-10-13 22:19:02 +0000200 const MCExpr *getImm() const {
201 assert(Kind == Immediate && "Invalid access!");
202 return Imm.Val;
203 }
204
Daniel Dunbar8462b302010-08-11 06:36:53 +0000205 bool isCondCode() const { return Kind == CondCode; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000206 bool isImm() const { return Kind == Immediate; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000207 bool isReg() const { return Kind == Register; }
Chris Lattner14b93852010-10-29 00:27:31 +0000208 bool isToken() const { return Kind == Token; }
209 bool isMemory() const { return Kind == Memory; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000210
211 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000212 // Add as immediates when possible. Null MCExpr = 0.
213 if (Expr == 0)
214 Inst.addOperand(MCOperand::CreateImm(0));
215 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000216 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
217 else
218 Inst.addOperand(MCOperand::CreateExpr(Expr));
219 }
220
Daniel Dunbar8462b302010-08-11 06:36:53 +0000221 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000222 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000223 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000224 // FIXME: What belongs here?
225 Inst.addOperand(MCOperand::CreateReg(0));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000226 }
227
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000228 void addRegOperands(MCInst &Inst, unsigned N) const {
229 assert(N == 1 && "Invalid number of operands!");
230 Inst.addOperand(MCOperand::CreateReg(getReg()));
231 }
232
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000233 void addImmOperands(MCInst &Inst, unsigned N) const {
234 assert(N == 1 && "Invalid number of operands!");
235 addExpr(Inst, getImm());
236 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000237
238
Chris Lattner14b93852010-10-29 00:27:31 +0000239 bool isMemMode5() const {
Chris Lattner14b93852010-10-29 00:27:31 +0000240 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
Jim Grosbach80eb2332010-10-29 17:41:25 +0000241 Mem.Writeback || Mem.Negative)
Chris Lattner14b93852010-10-29 00:27:31 +0000242 return false;
Jim Grosbach80eb2332010-10-29 17:41:25 +0000243 // If there is an offset expression, make sure it's valid.
244 if (!Mem.Offset)
245 return true;
246 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
247 if (!CE)
248 return false;
249 // The offset must be a multiple of 4 in the range 0-1020.
250 int64_t Value = CE->getValue();
251 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
Chris Lattner14b93852010-10-29 00:27:31 +0000252 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000253
Chris Lattner14b93852010-10-29 00:27:31 +0000254 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
255 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000256
Chris Lattner14b93852010-10-29 00:27:31 +0000257 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
258 assert(!Mem.OffsetIsReg && "invalid mode 5 operand");
Jim Grosbach80eb2332010-10-29 17:41:25 +0000259 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
260 // the difference?
261 if (Mem.Offset) {
262 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
263 assert (CE && "non-constant mode 5 offset operand!");
264 // The MCInst offset operand doesn't include the low two bits (like
265 // the instruction encoding).
266 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
267 } else
268 Inst.addOperand(MCOperand::CreateImm(0));
Chris Lattner14b93852010-10-29 00:27:31 +0000269 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000270
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000271 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000272
Chris Lattner3a697562010-10-28 17:20:03 +0000273 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
274 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000275 Op->CC.Val = CC;
276 Op->StartLoc = S;
277 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000278 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000279 }
280
Chris Lattner3a697562010-10-28 17:20:03 +0000281 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
282 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000283 Op->Tok.Data = Str.data();
284 Op->Tok.Length = Str.size();
285 Op->StartLoc = S;
286 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000287 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000288 }
289
Chris Lattner3a697562010-10-28 17:20:03 +0000290 static ARMOperand *CreateReg(unsigned RegNum, bool Writeback, SMLoc S,
291 SMLoc E) {
292 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000293 Op->Reg.RegNum = RegNum;
294 Op->Reg.Writeback = Writeback;
Sean Callanan76264762010-04-02 22:27:05 +0000295 Op->StartLoc = S;
296 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000297 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000298 }
299
Chris Lattner3a697562010-10-28 17:20:03 +0000300 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
301 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000302 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000303 Op->StartLoc = S;
304 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000305 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000306 }
307
Chris Lattner3a697562010-10-28 17:20:03 +0000308 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
309 const MCExpr *Offset, unsigned OffsetRegNum,
310 bool OffsetRegShifted, enum ShiftType ShiftType,
311 const MCExpr *ShiftAmount, bool Preindexed,
312 bool Postindexed, bool Negative, bool Writeback,
313 SMLoc S, SMLoc E) {
314 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000315 Op->Mem.BaseRegNum = BaseRegNum;
316 Op->Mem.OffsetIsReg = OffsetIsReg;
317 Op->Mem.Offset = Offset;
318 Op->Mem.OffsetRegNum = OffsetRegNum;
319 Op->Mem.OffsetRegShifted = OffsetRegShifted;
320 Op->Mem.ShiftType = ShiftType;
321 Op->Mem.ShiftAmount = ShiftAmount;
322 Op->Mem.Preindexed = Preindexed;
323 Op->Mem.Postindexed = Postindexed;
324 Op->Mem.Negative = Negative;
325 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000326
Sean Callanan76264762010-04-02 22:27:05 +0000327 Op->StartLoc = S;
328 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000329 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000330 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000331
Chris Lattner3a697562010-10-28 17:20:03 +0000332private:
333 ARMOperand(KindTy K) : Kind(K) {}
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000334};
335
336} // end anonymous namespace.
337
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000338void ARMOperand::dump(raw_ostream &OS) const {
339 switch (Kind) {
340 case CondCode:
341 OS << ARMCondCodeToString(getCondCode());
342 break;
343 case Immediate:
344 getImm()->print(OS);
345 break;
346 case Memory:
347 OS << "<memory>";
348 break;
349 case Register:
350 OS << "<register " << getReg() << ">";
351 break;
352 case Token:
353 OS << "'" << getToken() << "'";
354 break;
355 }
356}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000357
358/// @name Auto-generated Match Functions
359/// {
360
361static unsigned MatchRegisterName(StringRef Name);
362
363/// }
364
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000365/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000366/// and if it is a register name the token is eaten and the register number is
367/// returned. Otherwise return -1.
368///
369int ARMAsmParser::TryParseRegister() {
370 const AsmToken &Tok = Parser.getTok();
371 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
372
373 // FIXME: Validate register for the current architecture; we have to do
374 // validation later, so maybe there is no need for this here.
375 int RegNum = MatchRegisterName(Tok.getString());
376 if (RegNum == -1)
377 return -1;
378 Parser.Lex(); // Eat identifier token.
379 return RegNum;
380}
381
382
383/// Try to parse a register name. The token must be an Identifier when called,
384/// and if it is a register name the token is eaten and the register number is
385/// returned. Otherwise return -1.
Chris Lattner3a697562010-10-28 17:20:03 +0000386///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000387/// TODO this is likely to change to allow different register types and or to
388/// parse for a specific register type.
Chris Lattnere5658fa2010-10-30 04:09:10 +0000389ARMOperand *ARMAsmParser::TryParseRegisterWithWriteBack() {
390 SMLoc S = Parser.getTok().getLoc();
391 int RegNo = TryParseRegister();
392 if (RegNo == -1) return 0;
393
394 SMLoc E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000395
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000396 bool Writeback = false;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000397 const AsmToken &ExclaimTok = Parser.getTok();
398 if (ExclaimTok.is(AsmToken::Exclaim)) {
399 E = ExclaimTok.getLoc();
400 Writeback = true;
401 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000402 }
403
Chris Lattnere5658fa2010-10-30 04:09:10 +0000404 return ARMOperand::CreateReg(RegNo, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000405}
406
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000407/// Parse a register list, return it if successful else return null. The first
408/// token must be a '{' when called.
409ARMOperand *ARMAsmParser::ParseRegisterList() {
Sean Callanan76264762010-04-02 22:27:05 +0000410 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000411 assert(Parser.getTok().is(AsmToken::LCurly) &&
Kevin Enderbycfe07242009-10-13 22:19:02 +0000412 "Token is not an Left Curly Brace");
Sean Callanan76264762010-04-02 22:27:05 +0000413 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000414 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000415
Sean Callanan18b83232010-01-19 21:44:56 +0000416 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000417 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000418 if (RegTok.isNot(AsmToken::Identifier)) {
419 Error(RegLoc, "register expected");
420 return 0;
421 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000422 int RegNum = MatchRegisterName(RegTok.getString());
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000423 if (RegNum == -1) {
424 Error(RegLoc, "register expected");
425 return 0;
426 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000427
Sean Callananb9a25b72010-01-19 20:27:46 +0000428 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000429 unsigned RegList = 1 << RegNum;
430
431 int HighRegNum = RegNum;
432 // TODO ranges like "{Rn-Rm}"
Sean Callanan18b83232010-01-19 21:44:56 +0000433 while (Parser.getTok().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000434 Parser.Lex(); // Eat comma token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000435
Sean Callanan18b83232010-01-19 21:44:56 +0000436 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000437 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000438 if (RegTok.isNot(AsmToken::Identifier)) {
439 Error(RegLoc, "register expected");
440 return 0;
441 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000442 int RegNum = MatchRegisterName(RegTok.getString());
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000443 if (RegNum == -1) {
444 Error(RegLoc, "register expected");
445 return 0;
446 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000447
448 if (RegList & (1 << RegNum))
449 Warning(RegLoc, "register duplicated in register list");
450 else if (RegNum <= HighRegNum)
451 Warning(RegLoc, "register not in ascending order in register list");
452 RegList |= 1 << RegNum;
453 HighRegNum = RegNum;
454
Sean Callananb9a25b72010-01-19 20:27:46 +0000455 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000456 }
Sean Callanan18b83232010-01-19 21:44:56 +0000457 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000458 if (RCurlyTok.isNot(AsmToken::RCurly)) {
459 Error(RCurlyTok.getLoc(), "'}' expected");
460 return 0;
461 }
Sean Callanan76264762010-04-02 22:27:05 +0000462 E = RCurlyTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000463 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000464
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000465 // FIXME: Need to return an operand!
466 Error(E, "FIXME: register list parsing not implemented");
467 return 0;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000468}
469
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000470/// Parse an arm memory expression, return false if successful else return true
471/// or an error. The first token must be a '[' when called.
472/// TODO Only preindexing and postindexing addressing are started, unindexed
473/// with option, etc are still to do.
Chris Lattner550276e2010-10-28 20:52:15 +0000474ARMOperand *ARMAsmParser::ParseMemory() {
Sean Callanan76264762010-04-02 22:27:05 +0000475 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000476 assert(Parser.getTok().is(AsmToken::LBrac) &&
Kevin Enderby6bd266e2009-10-12 22:51:49 +0000477 "Token is not an Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000478 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000479 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000480
Sean Callanan18b83232010-01-19 21:44:56 +0000481 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000482 if (BaseRegTok.isNot(AsmToken::Identifier)) {
483 Error(BaseRegTok.getLoc(), "register expected");
484 return 0;
485 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000486 int BaseRegNum = TryParseRegister();
487 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000488 Error(BaseRegTok.getLoc(), "register expected");
489 return 0;
490 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000491
492 bool Preindexed = false;
493 bool Postindexed = false;
494 bool OffsetIsReg = false;
495 bool Negative = false;
496 bool Writeback = false;
497
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000498 // First look for preindexed address forms, that is after the "[Rn" we now
499 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000500 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000501 if (Tok.is(AsmToken::Comma)) {
502 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000503 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000504 int OffsetRegNum;
505 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000506 enum ShiftType ShiftType;
507 const MCExpr *ShiftAmount;
508 const MCExpr *Offset;
Chris Lattner550276e2010-10-28 20:52:15 +0000509 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
510 Offset, OffsetIsReg, OffsetRegNum, E))
511 return 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000512 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000513 if (RBracTok.isNot(AsmToken::RBrac)) {
514 Error(RBracTok.getLoc(), "']' expected");
515 return 0;
516 }
Sean Callanan76264762010-04-02 22:27:05 +0000517 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000518 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000519
Sean Callanan18b83232010-01-19 21:44:56 +0000520 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000521 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000522 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000523 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000524 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000525 }
Chris Lattner550276e2010-10-28 20:52:15 +0000526 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
527 OffsetRegShifted, ShiftType, ShiftAmount,
528 Preindexed, Postindexed, Negative, Writeback,
529 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000530 }
531 // The "[Rn" we have so far was not followed by a comma.
532 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000533 // If there's anything other than the right brace, this is a post indexing
534 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000535 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000536 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000537
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000538 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000539 bool OffsetRegShifted = false;
540 enum ShiftType ShiftType;
541 const MCExpr *ShiftAmount;
Chris Lattner14b93852010-10-29 00:27:31 +0000542 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000543
Sean Callanan18b83232010-01-19 21:44:56 +0000544 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000545 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000546 Postindexed = true;
547 Writeback = true;
Chris Lattner550276e2010-10-28 20:52:15 +0000548 if (NextTok.isNot(AsmToken::Comma)) {
549 Error(NextTok.getLoc(), "',' expected");
550 return 0;
551 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000552 Parser.Lex(); // Eat comma token.
Chris Lattner550276e2010-10-28 20:52:15 +0000553 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000554 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000555 E))
556 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000557 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000558
Chris Lattner550276e2010-10-28 20:52:15 +0000559 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
560 OffsetRegShifted, ShiftType, ShiftAmount,
561 Preindexed, Postindexed, Negative, Writeback,
562 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000563 }
564
Chris Lattner550276e2010-10-28 20:52:15 +0000565 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000566}
567
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000568/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
569/// we will parse the following (were +/- means that a plus or minus is
570/// optional):
571/// +/-Rm
572/// +/-Rm, shift
573/// #offset
574/// we return false on success or an error otherwise.
575bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000576 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000577 enum ShiftType &ShiftType,
578 const MCExpr *&ShiftAmount,
579 const MCExpr *&Offset,
580 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000581 int &OffsetRegNum,
582 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000583 Negative = false;
584 OffsetRegShifted = false;
585 OffsetIsReg = false;
586 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000587 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000588 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000589 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000590 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000591 else if (NextTok.is(AsmToken::Minus)) {
592 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000593 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000594 }
595 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000596 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000597 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000598 SMLoc CurLoc = OffsetRegTok.getLoc();
599 OffsetRegNum = TryParseRegister();
600 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000601 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000602 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000603 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000604 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000605
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000606 // If we parsed a register as the offset then their can be a shift after that
607 if (OffsetRegNum != -1) {
608 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000609 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000610 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000611 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000612
Sean Callanan18b83232010-01-19 21:44:56 +0000613 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000614 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000615 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000616 OffsetRegShifted = true;
617 }
618 }
619 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
620 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000621 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000622 if (HashTok.isNot(AsmToken::Hash))
623 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000624
Sean Callananb9a25b72010-01-19 20:27:46 +0000625 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000626
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000627 if (getParser().ParseExpression(Offset))
628 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000629 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000630 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000631 return false;
632}
633
634/// ParseShift as one of these two:
635/// ( lsl | lsr | asr | ror ) , # shift_amount
636/// rrx
637/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000638bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000639 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000640 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000641 if (Tok.isNot(AsmToken::Identifier))
642 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000643 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000644 if (ShiftName == "lsl" || ShiftName == "LSL")
645 St = Lsl;
646 else if (ShiftName == "lsr" || ShiftName == "LSR")
647 St = Lsr;
648 else if (ShiftName == "asr" || ShiftName == "ASR")
649 St = Asr;
650 else if (ShiftName == "ror" || ShiftName == "ROR")
651 St = Ror;
652 else if (ShiftName == "rrx" || ShiftName == "RRX")
653 St = Rrx;
654 else
655 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000656 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000657
658 // Rrx stands alone.
659 if (St == Rrx)
660 return false;
661
662 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000663 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000664 if (HashTok.isNot(AsmToken::Hash))
665 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000666 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000667
668 if (getParser().ParseExpression(ShiftAmount))
669 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000670
671 return false;
672}
673
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000674/// Parse a arm instruction operand. For now this parses the operand regardless
675/// of the mnemonic.
Chris Lattner550276e2010-10-28 20:52:15 +0000676ARMOperand *ARMAsmParser::ParseOperand() {
Sean Callanan76264762010-04-02 22:27:05 +0000677 SMLoc S, E;
Jim Grosbach16c74252010-10-29 14:46:02 +0000678
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000679 switch (getLexer().getKind()) {
680 case AsmToken::Identifier:
Chris Lattnere5658fa2010-10-30 04:09:10 +0000681 if (ARMOperand *Op = TryParseRegisterWithWriteBack())
Chris Lattner550276e2010-10-28 20:52:15 +0000682 return Op;
Jim Grosbach16c74252010-10-29 14:46:02 +0000683
Kevin Enderby515d5092009-10-15 20:48:48 +0000684 // This was not a register so parse other operands that start with an
685 // identifier (like labels) as expressions and create them as immediates.
686 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000687 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000688 if (getParser().ParseExpression(IdVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000689 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000690 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000691 return ARMOperand::CreateImm(IdVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000692 case AsmToken::LBrac:
Chris Lattner550276e2010-10-28 20:52:15 +0000693 return ParseMemory();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000694 case AsmToken::LCurly:
Chris Lattner550276e2010-10-28 20:52:15 +0000695 return ParseRegisterList();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000696 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000697 // #42 -> immediate.
698 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000699 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000700 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000701 const MCExpr *ImmVal;
702 if (getParser().ParseExpression(ImmVal))
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(ImmVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000706 default:
Chris Lattner550276e2010-10-28 20:52:15 +0000707 Error(Parser.getTok().getLoc(), "unexpected token in operand");
708 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000709 }
710}
711
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000712/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000713bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000714 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000715 // Create the leading tokens for the mnemonic, split by '.' characters.
716 size_t Start = 0, Next = Name.find('.');
717 StringRef Head = Name.slice(Start, Next);
718
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000719 // Determine the predicate, if any.
720 //
721 // FIXME: We need a way to check whether a prefix supports predication,
722 // otherwise we will end up with an ambiguity for instructions that happen to
723 // end with a predicate name.
Jim Grosbach3df518e2010-10-29 21:56:51 +0000724 // FIXME: Likewise, some arithmetic instructions have an 's' prefix which
725 // indicates to update the condition codes. Those instructions have an
726 // additional immediate operand which encodes the prefix as reg0 or CPSR.
727 // Just checking for a suffix of 's' definitely creates ambiguities; e.g,
728 // the SMMLS instruction.
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000729 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
730 .Case("eq", ARMCC::EQ)
731 .Case("ne", ARMCC::NE)
732 .Case("hs", ARMCC::HS)
733 .Case("lo", ARMCC::LO)
734 .Case("mi", ARMCC::MI)
735 .Case("pl", ARMCC::PL)
736 .Case("vs", ARMCC::VS)
737 .Case("vc", ARMCC::VC)
738 .Case("hi", ARMCC::HI)
739 .Case("ls", ARMCC::LS)
740 .Case("ge", ARMCC::GE)
741 .Case("lt", ARMCC::LT)
742 .Case("gt", ARMCC::GT)
743 .Case("le", ARMCC::LE)
744 .Case("al", ARMCC::AL)
745 .Default(~0U);
Jim Grosbach16c74252010-10-29 14:46:02 +0000746
Bill Wendling52925b62010-10-29 23:50:21 +0000747 if (CC != ~0U) {
748 if (CC == ARMCC::LS &&
749 (Head.compare("vmls") == 0 || Head.compare("vnmls") == 0)) {
750 CC = ARMCC::AL;
751 } else {
752 Head = Head.slice(0, Head.size() - 2);
753 }
754 } else {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000755 CC = ARMCC::AL;
Bill Wendling52925b62010-10-29 23:50:21 +0000756 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000757
Chris Lattner3a697562010-10-28 17:20:03 +0000758 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
759 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000760
761 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000762 while (Next != StringRef::npos) {
763 Start = Next;
764 Next = Name.find('.', Start + 1);
765 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000766
Chris Lattner3a697562010-10-28 17:20:03 +0000767 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000768 }
769
770 // Read the remaining operands.
771 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000772 // Read the first operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000773 if (ARMOperand *Op = ParseOperand())
774 Operands.push_back(Op);
775 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000776 Parser.EatToEndOfStatement();
777 return true;
778 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000779
780 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000781 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000782
783 // Parse and remember the 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 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000792
Chris Lattnercbf8a982010-09-11 16:18:25 +0000793 if (getLexer().isNot(AsmToken::EndOfStatement)) {
794 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000795 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000796 }
Chris Lattner34e53142010-09-08 05:10:46 +0000797 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000798 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000799}
800
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000801bool ARMAsmParser::
802MatchAndEmitInstruction(SMLoc IDLoc,
803 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
804 MCStreamer &Out) {
805 MCInst Inst;
806 unsigned ErrorInfo;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000807 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
808 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000809 Out.EmitInstruction(Inst);
810 return false;
Jim Grosbach16c74252010-10-29 14:46:02 +0000811
Chris Lattnere73d4f82010-10-28 21:41:58 +0000812 case Match_MissingFeature:
813 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
814 return true;
815 case Match_InvalidOperand: {
816 SMLoc ErrorLoc = IDLoc;
817 if (ErrorInfo != ~0U) {
818 if (ErrorInfo >= Operands.size())
819 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +0000820
Chris Lattnere73d4f82010-10-28 21:41:58 +0000821 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
822 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
823 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000824
Chris Lattnere73d4f82010-10-28 21:41:58 +0000825 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000826 }
Chris Lattnere73d4f82010-10-28 21:41:58 +0000827 case Match_MnemonicFail:
828 return Error(IDLoc, "unrecognized instruction mnemonic");
829 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000830
Eric Christopherc223e2b2010-10-29 09:26:59 +0000831 llvm_unreachable("Implement any new match types added!");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000832}
833
834
835
Kevin Enderby515d5092009-10-15 20:48:48 +0000836/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000837bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
838 StringRef IDVal = DirectiveID.getIdentifier();
839 if (IDVal == ".word")
840 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000841 else if (IDVal == ".thumb")
842 return ParseDirectiveThumb(DirectiveID.getLoc());
843 else if (IDVal == ".thumb_func")
844 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
845 else if (IDVal == ".code")
846 return ParseDirectiveCode(DirectiveID.getLoc());
847 else if (IDVal == ".syntax")
848 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000849 return true;
850}
851
852/// ParseDirectiveWord
853/// ::= .word [ expression (, expression)* ]
854bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
855 if (getLexer().isNot(AsmToken::EndOfStatement)) {
856 for (;;) {
857 const MCExpr *Value;
858 if (getParser().ParseExpression(Value))
859 return true;
860
Chris Lattneraaec2052010-01-19 19:46:13 +0000861 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000862
863 if (getLexer().is(AsmToken::EndOfStatement))
864 break;
Jim Grosbach16c74252010-10-29 14:46:02 +0000865
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000866 // FIXME: Improve diagnostic.
867 if (getLexer().isNot(AsmToken::Comma))
868 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000869 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000870 }
871 }
872
Sean Callananb9a25b72010-01-19 20:27:46 +0000873 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000874 return false;
875}
876
Kevin Enderby515d5092009-10-15 20:48:48 +0000877/// ParseDirectiveThumb
878/// ::= .thumb
879bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
880 if (getLexer().isNot(AsmToken::EndOfStatement))
881 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000882 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000883
884 // TODO: set thumb mode
885 // TODO: tell the MC streamer the mode
886 // getParser().getStreamer().Emit???();
887 return false;
888}
889
890/// ParseDirectiveThumbFunc
891/// ::= .thumbfunc symbol_name
892bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000893 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000894 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
895 return Error(L, "unexpected token in .syntax directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000896 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000897
898 if (getLexer().isNot(AsmToken::EndOfStatement))
899 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000900 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000901
902 // TODO: mark symbol as a thumb symbol
903 // getParser().getStreamer().Emit???();
904 return false;
905}
906
907/// ParseDirectiveSyntax
908/// ::= .syntax unified | divided
909bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000910 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000911 if (Tok.isNot(AsmToken::Identifier))
912 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000913 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000914 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000915 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000916 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000917 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000918 else
919 return Error(L, "unrecognized syntax mode in .syntax directive");
920
921 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000922 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000923 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000924
925 // TODO tell the MC streamer the mode
926 // getParser().getStreamer().Emit???();
927 return false;
928}
929
930/// ParseDirectiveCode
931/// ::= .code 16 | 32
932bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000933 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000934 if (Tok.isNot(AsmToken::Integer))
935 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000936 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +0000937 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +0000938 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000939 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +0000940 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000941 else
942 return Error(L, "invalid operand to .code directive");
943
944 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000945 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000946 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000947
948 // TODO tell the MC streamer the mode
949 // getParser().getStreamer().Emit???();
950 return false;
951}
952
Sean Callanan90b70972010-04-07 20:29:34 +0000953extern "C" void LLVMInitializeARMAsmLexer();
954
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000955/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000956extern "C" void LLVMInitializeARMAsmParser() {
957 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
958 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +0000959 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000960}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000961
Chris Lattner0692ee62010-09-06 19:11:01 +0000962#define GET_REGISTER_MATCHER
963#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000964#include "ARMGenAsmMatcher.inc"