blob: 87f77f25ef12eea315733da5219f448bd5ec737e [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"
Evan Cheng75972122011-01-13 07:58:56 +000012#include "ARMMCExpr.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000013#include "ARMBaseRegisterInfo.h"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000014#include "ARMSubtarget.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000015#include "llvm/MC/MCParser/MCAsmLexer.h"
16#include "llvm/MC/MCParser/MCAsmParser.h"
17#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Jim Grosbach642fc9c2010-11-05 22:33:53 +000018#include "llvm/MC/MCContext.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000019#include "llvm/MC/MCStreamer.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCInst.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000022#include "llvm/Target/TargetRegistry.h"
23#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000026#include "llvm/ADT/SmallVector.h"
Owen Anderson0c9f2502011-01-13 22:50:36 +000027#include "llvm/ADT/StringExtras.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000028#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000029#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000030using namespace llvm;
31
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +000032/// Shift types used for register controlled shifts in ARM memory addressing.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000033enum ShiftType {
34 Lsl,
35 Lsr,
36 Asr,
37 Ror,
38 Rrx
39};
40
Chris Lattner3a697562010-10-28 17:20:03 +000041namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000042
43class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000044
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000045class ARMAsmParser : public TargetAsmParser {
46 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000047 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000048
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000049 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000050 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
51
52 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000053 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
54
Chris Lattnere5658fa2010-10-30 04:09:10 +000055 int TryParseRegister();
Roman Divackybf755322011-01-27 17:14:22 +000056 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Bill Wendling50d0f582010-11-18 23:43:05 +000057 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +000058 bool ParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*>&);
59 bool ParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*>&);
Bill Wendling50d0f582010-11-18 23:43:05 +000060 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +000061 bool ParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &);
Bill Wendling50d0f582010-11-18 23:43:05 +000062 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +000063 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
Evan Cheng75972122011-01-13 07:58:56 +000064 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000065 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
66 MCSymbolRefExpr::VariantKind Variant);
67
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000068
Kevin Enderby9c41fa82009-10-30 22:55:57 +000069 bool ParseMemoryOffsetReg(bool &Negative,
70 bool &OffsetRegShifted,
71 enum ShiftType &ShiftType,
72 const MCExpr *&ShiftAmount,
73 const MCExpr *&Offset,
74 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000075 int &OffsetRegNum,
76 SMLoc &E);
Sean Callanan76264762010-04-02 22:27:05 +000077 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000078 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000079 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000080 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000081 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000082 bool ParseDirectiveSyntax(SMLoc L);
83
Chris Lattner7036f8b2010-09-29 01:42:58 +000084 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000085 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000086 MCStreamer &Out);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000087 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
88 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000089
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000090 /// @name Auto-generated Match Functions
91 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000092
Chris Lattner0692ee62010-09-06 19:11:01 +000093#define GET_ASSEMBLER_HEADER
94#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000095
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000096 /// }
97
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000098public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000099 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +0000100 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
101 // Initialize the set of available features.
102 setAvailableFeatures(ComputeAvailableFeatures(
103 &TM.getSubtarget<ARMSubtarget>()));
104 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000105
Benjamin Kramer38e59892010-07-14 22:38:02 +0000106 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000107 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000108 virtual bool ParseDirective(AsmToken DirectiveID);
109};
Jim Grosbach16c74252010-10-29 14:46:02 +0000110} // end anonymous namespace
111
Chris Lattner3a697562010-10-28 17:20:03 +0000112namespace {
113
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000114/// ARMOperand - Instances of this class represent a parsed ARM machine
115/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000116class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000117 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000118 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000119 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000120 CoprocNum,
121 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000122 Immediate,
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000123 MemBarrierOpt,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000124 Memory,
125 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000126 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000127 DPRRegisterList,
128 SPRRegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000129 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000130 } Kind;
131
Sean Callanan76264762010-04-02 22:27:05 +0000132 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000133 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000134
135 union {
136 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000137 ARMCC::CondCodes Val;
138 } CC;
139
140 struct {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000141 ARM_MB::MemBOpt Val;
142 } MBOpt;
143
144 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000145 unsigned Val;
146 } Cop;
147
148 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000149 const char *Data;
150 unsigned Length;
151 } Tok;
152
153 struct {
154 unsigned RegNum;
155 } Reg;
156
Bill Wendling8155e5b2010-11-06 22:19:43 +0000157 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000158 const MCExpr *Val;
159 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000160
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000161 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000162 struct {
163 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000164 union {
165 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
166 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
167 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000168 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
169 enum ShiftType ShiftType; // used when OffsetRegShifted is true
170 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000171 unsigned Preindexed : 1;
172 unsigned Postindexed : 1;
173 unsigned OffsetIsReg : 1;
174 unsigned Negative : 1; // only used when OffsetIsReg is true
175 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000176 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000177 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000178
Bill Wendling146018f2010-11-06 21:42:12 +0000179 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
180public:
Sean Callanan76264762010-04-02 22:27:05 +0000181 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
182 Kind = o.Kind;
183 StartLoc = o.StartLoc;
184 EndLoc = o.EndLoc;
185 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000186 case CondCode:
187 CC = o.CC;
188 break;
Sean Callanan76264762010-04-02 22:27:05 +0000189 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000190 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000191 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000192 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000193 case Register:
194 Reg = o.Reg;
195 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000196 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000197 case DPRRegisterList:
198 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000199 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000200 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000201 case CoprocNum:
202 case CoprocReg:
203 Cop = o.Cop;
204 break;
Sean Callanan76264762010-04-02 22:27:05 +0000205 case Immediate:
206 Imm = o.Imm;
207 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000208 case MemBarrierOpt:
209 MBOpt = o.MBOpt;
210 break;
Sean Callanan76264762010-04-02 22:27:05 +0000211 case Memory:
212 Mem = o.Mem;
213 break;
214 }
215 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000216
Sean Callanan76264762010-04-02 22:27:05 +0000217 /// getStartLoc - Get the location of the first token of this operand.
218 SMLoc getStartLoc() const { return StartLoc; }
219 /// getEndLoc - Get the location of the last token of this operand.
220 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000221
Daniel Dunbar8462b302010-08-11 06:36:53 +0000222 ARMCC::CondCodes getCondCode() const {
223 assert(Kind == CondCode && "Invalid access!");
224 return CC.Val;
225 }
226
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000227 unsigned getCoproc() const {
228 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
229 return Cop.Val;
230 }
231
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000232 StringRef getToken() const {
233 assert(Kind == Token && "Invalid access!");
234 return StringRef(Tok.Data, Tok.Length);
235 }
236
237 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000238 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000239 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000240 }
241
Bill Wendling5fa22a12010-11-09 23:28:44 +0000242 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000243 assert((Kind == RegisterList || Kind == DPRRegisterList ||
244 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000245 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000246 }
247
Kevin Enderbycfe07242009-10-13 22:19:02 +0000248 const MCExpr *getImm() const {
249 assert(Kind == Immediate && "Invalid access!");
250 return Imm.Val;
251 }
252
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000253 ARM_MB::MemBOpt getMemBarrierOpt() const {
254 assert(Kind == MemBarrierOpt && "Invalid access!");
255 return MBOpt.Val;
256 }
257
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000258 /// @name Memory Operand Accessors
259 /// @{
260
261 unsigned getMemBaseRegNum() const {
262 return Mem.BaseRegNum;
263 }
264 unsigned getMemOffsetRegNum() const {
265 assert(Mem.OffsetIsReg && "Invalid access!");
266 return Mem.Offset.RegNum;
267 }
268 const MCExpr *getMemOffset() const {
269 assert(!Mem.OffsetIsReg && "Invalid access!");
270 return Mem.Offset.Value;
271 }
272 unsigned getMemOffsetRegShifted() const {
273 assert(Mem.OffsetIsReg && "Invalid access!");
274 return Mem.OffsetRegShifted;
275 }
276 const MCExpr *getMemShiftAmount() const {
277 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
278 return Mem.ShiftAmount;
279 }
280 enum ShiftType getMemShiftType() const {
281 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
282 return Mem.ShiftType;
283 }
284 bool getMemPreindexed() const { return Mem.Preindexed; }
285 bool getMemPostindexed() const { return Mem.Postindexed; }
286 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
287 bool getMemNegative() const { return Mem.Negative; }
288 bool getMemWriteback() const { return Mem.Writeback; }
289
290 /// @}
291
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000292 bool isCoprocNum() const { return Kind == CoprocNum; }
293 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000294 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000295 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000296 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000297 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000298 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000299 bool isDPRRegList() const { return Kind == DPRRegisterList; }
300 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000301 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000302 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000303 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000304 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000305 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
306 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000307 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000308
Daniel Dunbar4b462672011-01-18 05:55:27 +0000309 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000310 if (!CE) return false;
311
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000312 // The offset must be a multiple of 4 in the range 0-1020.
313 int64_t Value = CE->getValue();
314 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
315 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000316 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000317 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000318 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000319 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000320 }
321 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000322 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000323 return false;
324
Daniel Dunbar4b462672011-01-18 05:55:27 +0000325 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000326 if (!CE) return false;
327
328 // The offset must be a multiple of 4 in the range 0-124.
329 uint64_t Value = CE->getValue();
330 return ((Value & 0x3) == 0 && Value <= 124);
331 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000332
333 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000334 // Add as immediates when possible. Null MCExpr = 0.
335 if (Expr == 0)
336 Inst.addOperand(MCOperand::CreateImm(0));
337 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000338 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
339 else
340 Inst.addOperand(MCOperand::CreateExpr(Expr));
341 }
342
Daniel Dunbar8462b302010-08-11 06:36:53 +0000343 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000344 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000345 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000346 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
347 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000348 }
349
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000350 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
351 assert(N == 1 && "Invalid number of operands!");
352 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
353 }
354
355 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
356 assert(N == 1 && "Invalid number of operands!");
357 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
358 }
359
Jim Grosbachd67641b2010-12-06 18:21:12 +0000360 void addCCOutOperands(MCInst &Inst, unsigned N) const {
361 assert(N == 1 && "Invalid number of operands!");
362 Inst.addOperand(MCOperand::CreateReg(getReg()));
363 }
364
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000365 void addRegOperands(MCInst &Inst, unsigned N) const {
366 assert(N == 1 && "Invalid number of operands!");
367 Inst.addOperand(MCOperand::CreateReg(getReg()));
368 }
369
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000370 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000371 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000372 const SmallVectorImpl<unsigned> &RegList = getRegList();
373 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000374 I = RegList.begin(), E = RegList.end(); I != E; ++I)
375 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000376 }
377
Bill Wendling0f630752010-11-17 04:32:08 +0000378 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
379 addRegListOperands(Inst, N);
380 }
381
382 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
383 addRegListOperands(Inst, N);
384 }
385
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000386 void addImmOperands(MCInst &Inst, unsigned N) const {
387 assert(N == 1 && "Invalid number of operands!");
388 addExpr(Inst, getImm());
389 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000390
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000391 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
392 assert(N == 1 && "Invalid number of operands!");
393 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
394 }
395
Chris Lattner14b93852010-10-29 00:27:31 +0000396 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
397 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000398
Daniel Dunbar4b462672011-01-18 05:55:27 +0000399 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
400 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000401
Jim Grosbach80eb2332010-10-29 17:41:25 +0000402 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
403 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000404 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000405 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000406
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000407 // The MCInst offset operand doesn't include the low two bits (like
408 // the instruction encoding).
409 int64_t Offset = CE->getValue() / 4;
410 if (Offset >= 0)
411 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
412 Offset)));
413 else
414 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
415 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000416 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000417
Bill Wendlingf4caf692010-12-14 03:36:38 +0000418 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
419 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000420 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
421 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000422 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000423
Bill Wendlingf4caf692010-12-14 03:36:38 +0000424 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
425 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000426 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
427 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000428 assert(CE && "Non-constant mode offset operand!");
429 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000430 }
431
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000432 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000433
Chris Lattner3a697562010-10-28 17:20:03 +0000434 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
435 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000436 Op->CC.Val = CC;
437 Op->StartLoc = S;
438 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000439 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000440 }
441
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000442 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
443 ARMOperand *Op = new ARMOperand(CoprocNum);
444 Op->Cop.Val = CopVal;
445 Op->StartLoc = S;
446 Op->EndLoc = S;
447 return Op;
448 }
449
450 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
451 ARMOperand *Op = new ARMOperand(CoprocReg);
452 Op->Cop.Val = CopVal;
453 Op->StartLoc = S;
454 Op->EndLoc = S;
455 return Op;
456 }
457
Jim Grosbachd67641b2010-12-06 18:21:12 +0000458 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
459 ARMOperand *Op = new ARMOperand(CCOut);
460 Op->Reg.RegNum = RegNum;
461 Op->StartLoc = S;
462 Op->EndLoc = S;
463 return Op;
464 }
465
Chris Lattner3a697562010-10-28 17:20:03 +0000466 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
467 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000468 Op->Tok.Data = Str.data();
469 Op->Tok.Length = Str.size();
470 Op->StartLoc = S;
471 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000472 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000473 }
474
Bill Wendling50d0f582010-11-18 23:43:05 +0000475 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000476 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000477 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000478 Op->StartLoc = S;
479 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000480 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000481 }
482
Bill Wendling7729e062010-11-09 22:44:22 +0000483 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000484 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000485 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000486 KindTy Kind = RegisterList;
487
488 if (ARM::DPRRegClass.contains(Regs.front().first))
489 Kind = DPRRegisterList;
490 else if (ARM::SPRRegClass.contains(Regs.front().first))
491 Kind = SPRRegisterList;
492
493 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000494 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000495 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000496 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000497 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000498 Op->StartLoc = StartLoc;
499 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000500 return Op;
501 }
502
Chris Lattner3a697562010-10-28 17:20:03 +0000503 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
504 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000505 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000506 Op->StartLoc = S;
507 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000508 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000509 }
510
Chris Lattner3a697562010-10-28 17:20:03 +0000511 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
Daniel Dunbar023835d2011-01-18 05:34:05 +0000512 const MCExpr *Offset, int OffsetRegNum,
Chris Lattner3a697562010-10-28 17:20:03 +0000513 bool OffsetRegShifted, enum ShiftType ShiftType,
514 const MCExpr *ShiftAmount, bool Preindexed,
515 bool Postindexed, bool Negative, bool Writeback,
516 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000517 assert((OffsetRegNum == -1 || OffsetIsReg) &&
518 "OffsetRegNum must imply OffsetIsReg!");
519 assert((!OffsetRegShifted || OffsetIsReg) &&
520 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000521 assert((Offset || OffsetIsReg) &&
522 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000523 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
524 "Cannot have shift amount without shifted register offset!");
525 assert((!Offset || !OffsetIsReg) &&
526 "Cannot have expression offset and register offset!");
527
Chris Lattner3a697562010-10-28 17:20:03 +0000528 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000529 Op->Mem.BaseRegNum = BaseRegNum;
530 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000531 if (OffsetIsReg)
532 Op->Mem.Offset.RegNum = OffsetRegNum;
533 else
534 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000535 Op->Mem.OffsetRegShifted = OffsetRegShifted;
536 Op->Mem.ShiftType = ShiftType;
537 Op->Mem.ShiftAmount = ShiftAmount;
538 Op->Mem.Preindexed = Preindexed;
539 Op->Mem.Postindexed = Postindexed;
540 Op->Mem.Negative = Negative;
541 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000542
Sean Callanan76264762010-04-02 22:27:05 +0000543 Op->StartLoc = S;
544 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000545 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000546 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000547
548 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
549 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
550 Op->MBOpt.Val = Opt;
551 Op->StartLoc = S;
552 Op->EndLoc = S;
553 return Op;
554 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000555};
556
557} // end anonymous namespace.
558
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000559void ARMOperand::dump(raw_ostream &OS) const {
560 switch (Kind) {
561 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000562 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000563 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000564 case CCOut:
565 OS << "<ccout " << getReg() << ">";
566 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000567 case CoprocNum:
568 OS << "<coprocessor number: " << getCoproc() << ">";
569 break;
570 case CoprocReg:
571 OS << "<coprocessor register: " << getCoproc() << ">";
572 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000573 case Immediate:
574 getImm()->print(OS);
575 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000576 case MemBarrierOpt:
577 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
578 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000579 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000580 OS << "<memory "
581 << "base:" << getMemBaseRegNum();
582 if (getMemOffsetIsReg()) {
583 OS << " offset:<register " << getMemOffsetRegNum();
584 if (getMemOffsetRegShifted()) {
585 OS << " offset-shift-type:" << getMemShiftType();
586 OS << " offset-shift-amount:" << *getMemShiftAmount();
587 }
588 } else {
589 OS << " offset:" << *getMemOffset();
590 }
591 if (getMemOffsetIsReg())
592 OS << " (offset-is-reg)";
593 if (getMemPreindexed())
594 OS << " (pre-indexed)";
595 if (getMemPostindexed())
596 OS << " (post-indexed)";
597 if (getMemNegative())
598 OS << " (negative)";
599 if (getMemWriteback())
600 OS << " (writeback)";
601 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000602 break;
603 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000604 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000605 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000606 case RegisterList:
607 case DPRRegisterList:
608 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000609 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000610
Bill Wendling5fa22a12010-11-09 23:28:44 +0000611 const SmallVectorImpl<unsigned> &RegList = getRegList();
612 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000613 I = RegList.begin(), E = RegList.end(); I != E; ) {
614 OS << *I;
615 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000616 }
617
618 OS << ">";
619 break;
620 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000621 case Token:
622 OS << "'" << getToken() << "'";
623 break;
624 }
625}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000626
627/// @name Auto-generated Match Functions
628/// {
629
630static unsigned MatchRegisterName(StringRef Name);
631
632/// }
633
Bob Wilson69df7232011-02-03 21:46:10 +0000634bool ARMAsmParser::ParseRegister(unsigned &RegNo,
635 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +0000636 RegNo = TryParseRegister();
637
638 return (RegNo == (unsigned)-1);
639}
640
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000641/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000642/// and if it is a register name the token is eaten and the register number is
643/// returned. Otherwise return -1.
644///
645int ARMAsmParser::TryParseRegister() {
646 const AsmToken &Tok = Parser.getTok();
647 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000648
Chris Lattnere5658fa2010-10-30 04:09:10 +0000649 // FIXME: Validate register for the current architecture; we have to do
650 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000651 std::string upperCase = Tok.getString().str();
652 std::string lowerCase = LowercaseString(upperCase);
653 unsigned RegNum = MatchRegisterName(lowerCase);
654 if (!RegNum) {
655 RegNum = StringSwitch<unsigned>(lowerCase)
656 .Case("r13", ARM::SP)
657 .Case("r14", ARM::LR)
658 .Case("r15", ARM::PC)
659 .Case("ip", ARM::R12)
660 .Default(0);
661 }
662 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +0000663
Chris Lattnere5658fa2010-10-30 04:09:10 +0000664 Parser.Lex(); // Eat identifier token.
665 return RegNum;
666}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000667
Bill Wendling50d0f582010-11-18 23:43:05 +0000668/// Try to parse a register name. The token must be an Identifier when called.
669/// If it's a register, an AsmOperand is created. Another AsmOperand is created
670/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000671///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000672/// TODO this is likely to change to allow different register types and or to
673/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000674bool ARMAsmParser::
675TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000676 SMLoc S = Parser.getTok().getLoc();
677 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000678 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000679 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000680
Bill Wendling50d0f582010-11-18 23:43:05 +0000681 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000682
Chris Lattnere5658fa2010-10-30 04:09:10 +0000683 const AsmToken &ExclaimTok = Parser.getTok();
684 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000685 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
686 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000687 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000688 }
689
Bill Wendling50d0f582010-11-18 23:43:05 +0000690 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000691}
692
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000693/// MatchCoprocessorOperandName - Try to parse an coprocessor related
694/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
695/// "c5", ...
696static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000697 // Use the same layout as the tablegen'erated register name matcher. Ugly,
698 // but efficient.
699 switch (Name.size()) {
700 default: break;
701 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000702 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000703 return -1;
704 switch (Name[1]) {
705 default: return -1;
706 case '0': return 0;
707 case '1': return 1;
708 case '2': return 2;
709 case '3': return 3;
710 case '4': return 4;
711 case '5': return 5;
712 case '6': return 6;
713 case '7': return 7;
714 case '8': return 8;
715 case '9': return 9;
716 }
717 break;
718 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000719 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000720 return -1;
721 switch (Name[2]) {
722 default: return -1;
723 case '0': return 10;
724 case '1': return 11;
725 case '2': return 12;
726 case '3': return 13;
727 case '4': return 14;
728 case '5': return 15;
729 }
730 break;
731 }
732
733 llvm_unreachable("Unhandled coprocessor operand string!");
734 return -1;
735}
736
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000737/// ParseCoprocNumOperand - Try to parse an coprocessor number operand. The
738/// token must be an Identifier when called, and if it is a coprocessor
739/// number, the token is eaten and the operand is added to the operand list.
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000740bool ARMAsmParser::
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000741ParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000742 SMLoc S = Parser.getTok().getLoc();
743 const AsmToken &Tok = Parser.getTok();
744 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
745
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000746 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000747 if (Num == -1)
748 return true;
749
750 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000751 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
752 return false;
753}
754
755/// ParseCoprocRegOperand - Try to parse an coprocessor register operand. The
756/// token must be an Identifier when called, and if it is a coprocessor
757/// number, the token is eaten and the operand is added to the operand list.
758bool ARMAsmParser::
759ParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
760 SMLoc S = Parser.getTok().getLoc();
761 const AsmToken &Tok = Parser.getTok();
762 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
763
764 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
765 if (Reg == -1)
766 return true;
767
768 Parser.Lex(); // Eat identifier token.
769 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000770 return false;
771}
772
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000773/// Parse a register list, return it if successful else return null. The first
774/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000775bool ARMAsmParser::
776ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +0000777 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000778 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000779 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000780
Bill Wendling7729e062010-11-09 22:44:22 +0000781 // Read the rest of the registers in the list.
782 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000783 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000784
Bill Wendling7729e062010-11-09 22:44:22 +0000785 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000786 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000787 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000788
Sean Callanan18b83232010-01-19 21:44:56 +0000789 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000790 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000791 if (RegTok.isNot(AsmToken::Identifier)) {
792 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000793 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000794 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000795
Bill Wendling1d6a2652010-11-06 10:40:24 +0000796 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000797 if (RegNum == -1) {
798 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000799 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000800 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000801
Bill Wendlinge7176102010-11-06 22:36:58 +0000802 if (IsRange) {
803 int Reg = PrevRegNum;
804 do {
805 ++Reg;
806 Registers.push_back(std::make_pair(Reg, RegLoc));
807 } while (Reg != RegNum);
808 } else {
809 Registers.push_back(std::make_pair(RegNum, RegLoc));
810 }
811
812 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000813 } while (Parser.getTok().is(AsmToken::Comma) ||
814 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000815
816 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000817 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000818 if (RCurlyTok.isNot(AsmToken::RCurly)) {
819 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000820 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000821 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000822
Bill Wendlinge7176102010-11-06 22:36:58 +0000823 SMLoc E = RCurlyTok.getLoc();
824 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +0000825
Bill Wendlinge7176102010-11-06 22:36:58 +0000826 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000827 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000828 RI = Registers.begin(), RE = Registers.end();
829
Bill Wendling7caebff2011-01-12 21:20:59 +0000830 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000831 bool EmittedWarning = false;
832
Bill Wendling7caebff2011-01-12 21:20:59 +0000833 DenseMap<unsigned, bool> RegMap;
834 RegMap[HighRegNum] = true;
835
Bill Wendlinge7176102010-11-06 22:36:58 +0000836 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000837 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +0000838 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +0000839
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000840 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000841 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +0000842 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +0000843 }
844
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000845 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000846 Warning(RegInfo.second,
847 "register not in ascending order in register list");
848
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000849 RegMap[Reg] = true;
850 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000851 }
852
Bill Wendling50d0f582010-11-18 23:43:05 +0000853 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
854 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000855}
856
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000857/// ParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
858bool ARMAsmParser::
859ParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
860 SMLoc S = Parser.getTok().getLoc();
861 const AsmToken &Tok = Parser.getTok();
862 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
863 StringRef OptStr = Tok.getString();
864
865 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
866 .Case("sy", ARM_MB::SY)
867 .Case("st", ARM_MB::ST)
868 .Case("ish", ARM_MB::ISH)
869 .Case("ishst", ARM_MB::ISHST)
870 .Case("nsh", ARM_MB::NSH)
871 .Case("nshst", ARM_MB::NSHST)
872 .Case("osh", ARM_MB::OSH)
873 .Case("oshst", ARM_MB::OSHST)
874 .Default(~0U);
875
876 if (Opt == ~0U)
877 return true;
878
879 Parser.Lex(); // Eat identifier token.
880 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
881 return false;
882}
883
Bill Wendlinge7176102010-11-06 22:36:58 +0000884/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000885/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000886///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000887/// TODO Only preindexing and postindexing addressing are started, unindexed
888/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +0000889bool ARMAsmParser::
890ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000891 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000892 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000893 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000894 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000895 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000896
Sean Callanan18b83232010-01-19 21:44:56 +0000897 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000898 if (BaseRegTok.isNot(AsmToken::Identifier)) {
899 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000900 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000901 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000902 int BaseRegNum = TryParseRegister();
903 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000904 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000905 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000906 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000907
Daniel Dunbar05710932011-01-18 05:34:17 +0000908 // The next token must either be a comma or a closing bracket.
909 const AsmToken &Tok = Parser.getTok();
910 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
911 return true;
912
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000913 bool Preindexed = false;
914 bool Postindexed = false;
915 bool OffsetIsReg = false;
916 bool Negative = false;
917 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000918 ARMOperand *WBOp = 0;
919 int OffsetRegNum = -1;
920 bool OffsetRegShifted = false;
921 enum ShiftType ShiftType = Lsl;
922 const MCExpr *ShiftAmount = 0;
923 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000924
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000925 // First look for preindexed address forms, that is after the "[Rn" we now
926 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000927 if (Tok.is(AsmToken::Comma)) {
928 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000929 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000930
Chris Lattner550276e2010-10-28 20:52:15 +0000931 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
932 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000933 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000934 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000935 if (RBracTok.isNot(AsmToken::RBrac)) {
936 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000937 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000938 }
Sean Callanan76264762010-04-02 22:27:05 +0000939 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000940 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000941
Sean Callanan18b83232010-01-19 21:44:56 +0000942 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000943 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000944 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
945 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000946 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000947 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000948 }
Daniel Dunbar05710932011-01-18 05:34:17 +0000949 } else {
950 // The "[Rn" we have so far was not followed by a comma.
951
Jim Grosbach80eb2332010-10-29 17:41:25 +0000952 // If there's anything other than the right brace, this is a post indexing
953 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000954 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000955 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000956
Sean Callanan18b83232010-01-19 21:44:56 +0000957 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +0000958
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000959 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000960 Postindexed = true;
961 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +0000962
Chris Lattner550276e2010-10-28 20:52:15 +0000963 if (NextTok.isNot(AsmToken::Comma)) {
964 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000965 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000966 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000967
Sean Callananb9a25b72010-01-19 20:27:46 +0000968 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +0000969
Chris Lattner550276e2010-10-28 20:52:15 +0000970 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000971 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000972 E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000973 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000974 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000975 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000976
977 // Force Offset to exist if used.
978 if (!OffsetIsReg) {
979 if (!Offset)
980 Offset = MCConstantExpr::Create(0, getContext());
981 }
982
983 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
984 OffsetRegNum, OffsetRegShifted,
985 ShiftType, ShiftAmount, Preindexed,
986 Postindexed, Negative, Writeback,
987 S, E));
988 if (WBOp)
989 Operands.push_back(WBOp);
990
991 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000992}
993
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000994/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
995/// we will parse the following (were +/- means that a plus or minus is
996/// optional):
997/// +/-Rm
998/// +/-Rm, shift
999/// #offset
1000/// we return false on success or an error otherwise.
1001bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001002 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001003 enum ShiftType &ShiftType,
1004 const MCExpr *&ShiftAmount,
1005 const MCExpr *&Offset,
1006 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001007 int &OffsetRegNum,
1008 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001009 Negative = false;
1010 OffsetRegShifted = false;
1011 OffsetIsReg = false;
1012 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001013 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001014 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001015 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001016 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001017 else if (NextTok.is(AsmToken::Minus)) {
1018 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001019 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001020 }
1021 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001022 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001023 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001024 SMLoc CurLoc = OffsetRegTok.getLoc();
1025 OffsetRegNum = TryParseRegister();
1026 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001027 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001028 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001029 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001030 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001031
Bill Wendling12f40e92010-11-06 10:51:53 +00001032 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001033 if (OffsetRegNum != -1) {
1034 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001035 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001036 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001037 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001038
Sean Callanan18b83232010-01-19 21:44:56 +00001039 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001040 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001041 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001042 OffsetRegShifted = true;
1043 }
1044 }
1045 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1046 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001047 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001048 if (HashTok.isNot(AsmToken::Hash))
1049 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001050
Sean Callananb9a25b72010-01-19 20:27:46 +00001051 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001052
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001053 if (getParser().ParseExpression(Offset))
1054 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001055 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001056 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001057 return false;
1058}
1059
1060/// ParseShift as one of these two:
1061/// ( lsl | lsr | asr | ror ) , # shift_amount
1062/// rrx
1063/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +00001064bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +00001065 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001066 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001067 if (Tok.isNot(AsmToken::Identifier))
1068 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001069 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001070 if (ShiftName == "lsl" || ShiftName == "LSL")
1071 St = Lsl;
1072 else if (ShiftName == "lsr" || ShiftName == "LSR")
1073 St = Lsr;
1074 else if (ShiftName == "asr" || ShiftName == "ASR")
1075 St = Asr;
1076 else if (ShiftName == "ror" || ShiftName == "ROR")
1077 St = Ror;
1078 else if (ShiftName == "rrx" || ShiftName == "RRX")
1079 St = Rrx;
1080 else
1081 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001082 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001083
1084 // Rrx stands alone.
1085 if (St == Rrx)
1086 return false;
1087
1088 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001089 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001090 if (HashTok.isNot(AsmToken::Hash))
1091 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001092 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001093
1094 if (getParser().ParseExpression(ShiftAmount))
1095 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001096
1097 return false;
1098}
1099
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001100/// Parse a arm instruction operand. For now this parses the operand regardless
1101/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001102bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001103 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001104 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001105
1106 // Check if the current operand has a custom associated parser, if so, try to
1107 // custom parse the operand, or fallback to the general approach.
1108 MatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1109 if (ResTy == Match_Success)
1110 return false;
1111
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001112 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001113 default:
1114 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001115 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +00001116 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +00001117 if (!TryParseRegisterWithWriteBack(Operands))
1118 return false;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001119
1120 // Fall though for the Identifier case that is not a register or a
1121 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +00001122 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1123 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001124 // This was not a register so parse other operands that start with an
1125 // identifier (like labels) as expressions and create them as immediates.
1126 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001127 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001128 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001129 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001130 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001131 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1132 return false;
1133 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001134 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001135 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001136 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001137 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001138 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001139 // #42 -> immediate.
1140 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001141 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001142 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001143 const MCExpr *ImmVal;
1144 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001145 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001146 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001147 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1148 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001149 case AsmToken::Colon: {
1150 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001151 // FIXME: Check it's an expression prefix,
1152 // e.g. (FOO - :lower16:BAR) isn't legal.
1153 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001154 if (ParsePrefix(RefKind))
1155 return true;
1156
Evan Cheng75972122011-01-13 07:58:56 +00001157 const MCExpr *SubExprVal;
1158 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001159 return true;
1160
Evan Cheng75972122011-01-13 07:58:56 +00001161 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1162 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001163 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001164 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001165 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001166 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001167 }
1168}
1169
Evan Cheng75972122011-01-13 07:58:56 +00001170// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1171// :lower16: and :upper16:.
1172bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1173 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001174
1175 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001176 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001177 Parser.Lex(); // Eat ':'
1178
1179 if (getLexer().isNot(AsmToken::Identifier)) {
1180 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1181 return true;
1182 }
1183
1184 StringRef IDVal = Parser.getTok().getIdentifier();
1185 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001186 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001187 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001188 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001189 } else {
1190 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1191 return true;
1192 }
1193 Parser.Lex();
1194
1195 if (getLexer().isNot(AsmToken::Colon)) {
1196 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1197 return true;
1198 }
1199 Parser.Lex(); // Eat the last ':'
1200 return false;
1201}
1202
1203const MCExpr *
1204ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1205 MCSymbolRefExpr::VariantKind Variant) {
1206 // Recurse over the given expression, rebuilding it to apply the given variant
1207 // to the leftmost symbol.
1208 if (Variant == MCSymbolRefExpr::VK_None)
1209 return E;
1210
1211 switch (E->getKind()) {
1212 case MCExpr::Target:
1213 llvm_unreachable("Can't handle target expr yet");
1214 case MCExpr::Constant:
1215 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1216
1217 case MCExpr::SymbolRef: {
1218 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1219
1220 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1221 return 0;
1222
1223 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1224 }
1225
1226 case MCExpr::Unary:
1227 llvm_unreachable("Can't handle unary expressions yet");
1228
1229 case MCExpr::Binary: {
1230 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1231 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1232 const MCExpr *RHS = BE->getRHS();
1233 if (!LHS)
1234 return 0;
1235
1236 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1237 }
1238 }
1239
1240 assert(0 && "Invalid expression kind!");
1241 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001242}
1243
Daniel Dunbar352e1482011-01-11 15:59:50 +00001244/// \brief Given a mnemonic, split out possible predication code and carry
1245/// setting letters to form a canonical mnemonic and flags.
1246//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001247// FIXME: Would be nice to autogen this.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001248static StringRef SplitMnemonicAndCC(StringRef Mnemonic,
1249 unsigned &PredicationCode,
1250 bool &CarrySetting) {
1251 PredicationCode = ARMCC::AL;
1252 CarrySetting = false;
1253
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001254 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001255 //
1256 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001257 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1258 Mnemonic == "movs" ||
1259 Mnemonic == "svc" ||
1260 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1261 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1262 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1263 Mnemonic == "vclt" ||
1264 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1265 Mnemonic == "vcle" ||
1266 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1267 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1268 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001269 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001270
Daniel Dunbar352e1482011-01-11 15:59:50 +00001271 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001272 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001273 .Case("eq", ARMCC::EQ)
1274 .Case("ne", ARMCC::NE)
1275 .Case("hs", ARMCC::HS)
1276 .Case("lo", ARMCC::LO)
1277 .Case("mi", ARMCC::MI)
1278 .Case("pl", ARMCC::PL)
1279 .Case("vs", ARMCC::VS)
1280 .Case("vc", ARMCC::VC)
1281 .Case("hi", ARMCC::HI)
1282 .Case("ls", ARMCC::LS)
1283 .Case("ge", ARMCC::GE)
1284 .Case("lt", ARMCC::LT)
1285 .Case("gt", ARMCC::GT)
1286 .Case("le", ARMCC::LE)
1287 .Case("al", ARMCC::AL)
1288 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001289 if (CC != ~0U) {
1290 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001291 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001292 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001293
Daniel Dunbar352e1482011-01-11 15:59:50 +00001294 // Next, determine if we have a carry setting bit. We explicitly ignore all
1295 // the instructions we know end in 's'.
1296 if (Mnemonic.endswith("s") &&
1297 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1298 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1299 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1300 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1301 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1302 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1303 CarrySetting = true;
1304 }
1305
1306 return Mnemonic;
1307}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001308
1309/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1310/// inclusion of carry set or predication code operands.
1311//
1312// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001313void ARMAsmParser::
1314GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1315 bool &CanAcceptPredicationCode) {
1316 bool isThumb = TM.getSubtarget<ARMSubtarget>().isThumb();
1317
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001318 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1319 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1320 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1321 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1322 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1323 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1324 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1325 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1326 CanAcceptCarrySet = true;
1327 } else {
1328 CanAcceptCarrySet = false;
1329 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001330
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001331 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1332 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1333 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1334 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00001335 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
1336 Mnemonic == "clrex") {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001337 CanAcceptPredicationCode = false;
1338 } else {
1339 CanAcceptPredicationCode = true;
1340 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001341
1342 if (isThumb)
1343 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Bruno Cardoso Lopes8dd37f72011-01-20 18:32:09 +00001344 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001345 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001346}
1347
1348/// Parse an arm instruction mnemonic followed by its operands.
1349bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1350 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1351 // Create the leading tokens for the mnemonic, split by '.' characters.
1352 size_t Start = 0, Next = Name.find('.');
1353 StringRef Head = Name.slice(Start, Next);
1354
Daniel Dunbar352e1482011-01-11 15:59:50 +00001355 // Split out the predication code and carry setting flag from the mnemonic.
1356 unsigned PredicationCode;
1357 bool CarrySetting;
1358 Head = SplitMnemonicAndCC(Head, PredicationCode, CarrySetting);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001359
Chris Lattner3a697562010-10-28 17:20:03 +00001360 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001361
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001362 // Next, add the CCOut and ConditionCode operands, if needed.
1363 //
1364 // For mnemonics which can ever incorporate a carry setting bit or predication
1365 // code, our matching model involves us always generating CCOut and
1366 // ConditionCode operands to match the mnemonic "as written" and then we let
1367 // the matcher deal with finding the right instruction or generating an
1368 // appropriate error.
1369 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1370 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1371
1372 // Add the carry setting operand, if necessary.
1373 //
1374 // FIXME: It would be awesome if we could somehow invent a location such that
1375 // match errors on this operand would print a nice diagnostic about how the
1376 // 's' character in the mnemonic resulted in a CCOut operand.
1377 if (CanAcceptCarrySet) {
1378 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1379 NameLoc));
1380 } else {
1381 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1382 // misspelled another mnemonic).
1383
1384 // FIXME: Issue a nice error.
1385 }
1386
1387 // Add the predication code operand, if necessary.
1388 if (CanAcceptPredicationCode) {
1389 Operands.push_back(ARMOperand::CreateCondCode(
1390 ARMCC::CondCodes(PredicationCode), NameLoc));
1391 } else {
1392 // This mnemonic can't ever accept a predication code, but the user wrote
1393 // one (or misspelled another mnemonic).
1394
1395 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001396 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001397
1398 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001399 while (Next != StringRef::npos) {
1400 Start = Next;
1401 Next = Name.find('.', Start + 1);
1402 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001403
Chris Lattner3a697562010-10-28 17:20:03 +00001404 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001405 }
1406
1407 // Read the remaining operands.
1408 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001409 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001410 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001411 Parser.EatToEndOfStatement();
1412 return true;
1413 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001414
1415 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001416 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001417
1418 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001419 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001420 Parser.EatToEndOfStatement();
1421 return true;
1422 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001423 }
1424 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001425
Chris Lattnercbf8a982010-09-11 16:18:25 +00001426 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1427 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001428 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001429 }
Bill Wendling146018f2010-11-06 21:42:12 +00001430
Chris Lattner34e53142010-09-08 05:10:46 +00001431 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001432 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001433}
1434
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001435bool ARMAsmParser::
1436MatchAndEmitInstruction(SMLoc IDLoc,
1437 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1438 MCStreamer &Out) {
1439 MCInst Inst;
1440 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001441 MatchResultTy MatchResult, MatchResult2;
1442 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1443 if (MatchResult != Match_Success) {
1444 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1445 // that does not update the condition codes. So try adding a CCOut operand
1446 // with a value of reg0.
1447 if (MatchResult == Match_InvalidOperand) {
1448 Operands.insert(Operands.begin() + 1,
1449 ARMOperand::CreateCCOut(0,
1450 ((ARMOperand*)Operands[0])->getStartLoc()));
1451 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1452 if (MatchResult2 == Match_Success)
1453 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001454 else {
1455 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001456 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001457 delete CCOut;
1458 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001459 }
1460 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1461 // that updates the condition codes if it ends in 's'. So see if the
1462 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1463 // operand with a value of CPSR.
1464 else if(MatchResult == Match_MnemonicFail) {
1465 // Get the instruction mnemonic, which is the first token.
1466 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1467 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1468 // removed the 's' from the mnemonic for matching.
1469 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1470 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001471 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1472 Operands.erase(Operands.begin());
1473 delete OldMnemonic;
1474 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001475 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1476 Operands.insert(Operands.begin() + 1,
1477 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1478 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1479 if (MatchResult2 == Match_Success)
1480 MatchResult = Match_Success;
1481 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001482 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1483 Operands.erase(Operands.begin());
1484 delete OldMnemonic;
1485 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001486 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001487 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1488 Operands.erase(Operands.begin() + 1);
1489 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001490 }
1491 }
1492 }
1493 }
1494 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001495 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001496 Out.EmitInstruction(Inst);
1497 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001498 case Match_MissingFeature:
1499 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1500 return true;
1501 case Match_InvalidOperand: {
1502 SMLoc ErrorLoc = IDLoc;
1503 if (ErrorInfo != ~0U) {
1504 if (ErrorInfo >= Operands.size())
1505 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001506
Chris Lattnere73d4f82010-10-28 21:41:58 +00001507 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1508 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1509 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001510
Chris Lattnere73d4f82010-10-28 21:41:58 +00001511 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001512 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001513 case Match_MnemonicFail:
1514 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00001515 case Match_ConversionFail:
1516 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00001517 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001518
Eric Christopherc223e2b2010-10-29 09:26:59 +00001519 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001520 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001521}
1522
Kevin Enderby515d5092009-10-15 20:48:48 +00001523/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001524bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1525 StringRef IDVal = DirectiveID.getIdentifier();
1526 if (IDVal == ".word")
1527 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001528 else if (IDVal == ".thumb")
1529 return ParseDirectiveThumb(DirectiveID.getLoc());
1530 else if (IDVal == ".thumb_func")
1531 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1532 else if (IDVal == ".code")
1533 return ParseDirectiveCode(DirectiveID.getLoc());
1534 else if (IDVal == ".syntax")
1535 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001536 return true;
1537}
1538
1539/// ParseDirectiveWord
1540/// ::= .word [ expression (, expression)* ]
1541bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1542 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1543 for (;;) {
1544 const MCExpr *Value;
1545 if (getParser().ParseExpression(Value))
1546 return true;
1547
Chris Lattneraaec2052010-01-19 19:46:13 +00001548 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001549
1550 if (getLexer().is(AsmToken::EndOfStatement))
1551 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001552
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001553 // FIXME: Improve diagnostic.
1554 if (getLexer().isNot(AsmToken::Comma))
1555 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001556 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001557 }
1558 }
1559
Sean Callananb9a25b72010-01-19 20:27:46 +00001560 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001561 return false;
1562}
1563
Kevin Enderby515d5092009-10-15 20:48:48 +00001564/// ParseDirectiveThumb
1565/// ::= .thumb
1566bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1567 if (getLexer().isNot(AsmToken::EndOfStatement))
1568 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001569 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001570
1571 // TODO: set thumb mode
1572 // TODO: tell the MC streamer the mode
1573 // getParser().getStreamer().Emit???();
1574 return false;
1575}
1576
1577/// ParseDirectiveThumbFunc
1578/// ::= .thumbfunc symbol_name
1579bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001580 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001581 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001582 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001583 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001584 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001585 if (getLexer().isNot(AsmToken::EndOfStatement))
1586 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001587 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001588
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001589 // Mark symbol as a thumb symbol.
1590 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1591 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001592 return false;
1593}
1594
1595/// ParseDirectiveSyntax
1596/// ::= .syntax unified | divided
1597bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001598 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001599 if (Tok.isNot(AsmToken::Identifier))
1600 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001601 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001602 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001603 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001604 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00001605 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00001606 else
1607 return Error(L, "unrecognized syntax mode in .syntax directive");
1608
1609 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001610 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001611 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001612
1613 // TODO tell the MC streamer the mode
1614 // getParser().getStreamer().Emit???();
1615 return false;
1616}
1617
1618/// ParseDirectiveCode
1619/// ::= .code 16 | 32
1620bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001621 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001622 if (Tok.isNot(AsmToken::Integer))
1623 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001624 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001625 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001626 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001627 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001628 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001629 else
1630 return Error(L, "invalid operand to .code directive");
1631
1632 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001633 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001634 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001635
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001636 // FIXME: We need to be able switch subtargets at this point so that
1637 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
1638 // includes Feature_IsThumb or not to match the right instructions. This is
1639 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
1640 if (Val == 16){
1641 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
1642 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001643 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001644 }
1645 else{
1646 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
1647 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001648 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001649 }
Jim Grosbach2a301702010-11-05 22:40:53 +00001650
Kevin Enderby515d5092009-10-15 20:48:48 +00001651 return false;
1652}
1653
Sean Callanan90b70972010-04-07 20:29:34 +00001654extern "C" void LLVMInitializeARMAsmLexer();
1655
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001656/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001657extern "C" void LLVMInitializeARMAsmParser() {
1658 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1659 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001660 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001661}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001662
Chris Lattner0692ee62010-09-06 19:11:01 +00001663#define GET_REGISTER_MATCHER
1664#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001665#include "ARMGenAsmMatcher.inc"