blob: 23d612505d3010739f94495ce33c34897e81d966 [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
Chris Lattner3a697562010-10-28 17:20:03 +000032namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000033
34class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000035
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000036class ARMAsmParser : public TargetAsmParser {
37 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000038 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000039
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000040 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000041 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
42
43 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000044 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
45
Chris Lattnere5658fa2010-10-30 04:09:10 +000046 int TryParseRegister();
Roman Divackybf755322011-01-27 17:14:22 +000047 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Bill Wendling50d0f582010-11-18 23:43:05 +000048 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
Owen Anderson00828302011-03-18 22:50:18 +000049 bool TryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
Bill Wendling50d0f582010-11-18 23:43:05 +000050 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +000051 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &,
52 ARMII::AddrMode AddrMode);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +000053 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
Evan Cheng75972122011-01-13 07:58:56 +000054 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000055 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
56 MCSymbolRefExpr::VariantKind Variant);
57
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000058
Kevin Enderby9c41fa82009-10-30 22:55:57 +000059 bool ParseMemoryOffsetReg(bool &Negative,
60 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +000061 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +000062 const MCExpr *&ShiftAmount,
63 const MCExpr *&Offset,
64 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000065 int &OffsetRegNum,
66 SMLoc &E);
Owen Anderson00828302011-03-18 22:50:18 +000067 bool ParseShift(enum ARM_AM::ShiftOpc &St,
68 const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000069 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000070 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000071 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000072 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000073 bool ParseDirectiveSyntax(SMLoc L);
74
Chris Lattner7036f8b2010-09-29 01:42:58 +000075 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000076 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000077 MCStreamer &Out);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000078 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
79 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000080
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000081 /// @name Auto-generated Match Functions
82 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000083
Chris Lattner0692ee62010-09-06 19:11:01 +000084#define GET_ASSEMBLER_HEADER
85#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000086
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000087 /// }
88
Jim Grosbachf922c472011-02-12 01:34:40 +000089 OperandMatchResultTy tryParseCoprocNumOperand(
90 SmallVectorImpl<MCParsedAsmOperand*>&);
91 OperandMatchResultTy tryParseCoprocRegOperand(
92 SmallVectorImpl<MCParsedAsmOperand*>&);
93 OperandMatchResultTy tryParseMemBarrierOptOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +000094 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +000095 OperandMatchResultTy tryParseProcIFlagsOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +000096 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +000097 OperandMatchResultTy tryParseMSRMaskOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +000098 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +000099 OperandMatchResultTy tryParseMemMode2Operand(
100 SmallVectorImpl<MCParsedAsmOperand*>&);
101
102 // Asm Match Converter Methods
103 bool CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
104 const SmallVectorImpl<MCParsedAsmOperand*> &);
105 bool CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
106 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbachf922c472011-02-12 01:34:40 +0000107
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000108public:
Daniel Dunbard73ada72010-07-19 00:33:49 +0000109 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +0000110 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
111 // Initialize the set of available features.
112 setAvailableFeatures(ComputeAvailableFeatures(
113 &TM.getSubtarget<ARMSubtarget>()));
114 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000115
Benjamin Kramer38e59892010-07-14 22:38:02 +0000116 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000117 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000118 virtual bool ParseDirective(AsmToken DirectiveID);
119};
Jim Grosbach16c74252010-10-29 14:46:02 +0000120} // end anonymous namespace
121
Chris Lattner3a697562010-10-28 17:20:03 +0000122namespace {
123
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000124/// ARMOperand - Instances of this class represent a parsed ARM machine
125/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000126class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000127 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000128 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000129 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000130 CoprocNum,
131 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000132 Immediate,
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000133 MemBarrierOpt,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000134 Memory,
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000135 MSRMask,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000136 ProcIFlags,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000137 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000138 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000139 DPRRegisterList,
140 SPRRegisterList,
Owen Anderson00828302011-03-18 22:50:18 +0000141 Shifter,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000142 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000143 } Kind;
144
Sean Callanan76264762010-04-02 22:27:05 +0000145 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000146 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000147
148 union {
149 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000150 ARMCC::CondCodes Val;
151 } CC;
152
153 struct {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000154 ARM_MB::MemBOpt Val;
155 } MBOpt;
156
157 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000158 unsigned Val;
159 } Cop;
160
161 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000162 ARM_PROC::IFlags Val;
163 } IFlags;
164
165 struct {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000166 unsigned Val;
167 } MMask;
168
169 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000170 const char *Data;
171 unsigned Length;
172 } Tok;
173
174 struct {
175 unsigned RegNum;
176 } Reg;
177
Bill Wendling8155e5b2010-11-06 22:19:43 +0000178 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000179 const MCExpr *Val;
180 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000181
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000182 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000183 struct {
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000184 ARMII::AddrMode AddrMode;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000185 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000186 union {
187 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
188 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
189 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000190 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Owen Anderson00828302011-03-18 22:50:18 +0000191 enum ARM_AM::ShiftOpc ShiftType; // used when OffsetRegShifted is true
Bill Wendling146018f2010-11-06 21:42:12 +0000192 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000193 unsigned Preindexed : 1;
194 unsigned Postindexed : 1;
195 unsigned OffsetIsReg : 1;
196 unsigned Negative : 1; // only used when OffsetIsReg is true
197 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000198 } Mem;
Owen Anderson00828302011-03-18 22:50:18 +0000199
200 struct {
201 ARM_AM::ShiftOpc ShiftTy;
202 unsigned RegNum;
203 } Shift;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000204 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000205
Bill Wendling146018f2010-11-06 21:42:12 +0000206 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
207public:
Sean Callanan76264762010-04-02 22:27:05 +0000208 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
209 Kind = o.Kind;
210 StartLoc = o.StartLoc;
211 EndLoc = o.EndLoc;
212 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000213 case CondCode:
214 CC = o.CC;
215 break;
Sean Callanan76264762010-04-02 22:27:05 +0000216 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000217 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000218 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000219 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000220 case Register:
221 Reg = o.Reg;
222 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000223 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000224 case DPRRegisterList:
225 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000226 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000227 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000228 case CoprocNum:
229 case CoprocReg:
230 Cop = o.Cop;
231 break;
Sean Callanan76264762010-04-02 22:27:05 +0000232 case Immediate:
233 Imm = o.Imm;
234 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000235 case MemBarrierOpt:
236 MBOpt = o.MBOpt;
237 break;
Sean Callanan76264762010-04-02 22:27:05 +0000238 case Memory:
239 Mem = o.Mem;
240 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000241 case MSRMask:
242 MMask = o.MMask;
243 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000244 case ProcIFlags:
245 IFlags = o.IFlags;
Owen Anderson00828302011-03-18 22:50:18 +0000246 break;
247 case Shifter:
248 Shift = o.Shift;
249 break;
Sean Callanan76264762010-04-02 22:27:05 +0000250 }
251 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000252
Sean Callanan76264762010-04-02 22:27:05 +0000253 /// getStartLoc - Get the location of the first token of this operand.
254 SMLoc getStartLoc() const { return StartLoc; }
255 /// getEndLoc - Get the location of the last token of this operand.
256 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000257
Daniel Dunbar8462b302010-08-11 06:36:53 +0000258 ARMCC::CondCodes getCondCode() const {
259 assert(Kind == CondCode && "Invalid access!");
260 return CC.Val;
261 }
262
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000263 unsigned getCoproc() const {
264 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
265 return Cop.Val;
266 }
267
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000268 StringRef getToken() const {
269 assert(Kind == Token && "Invalid access!");
270 return StringRef(Tok.Data, Tok.Length);
271 }
272
273 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000274 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000275 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000276 }
277
Bill Wendling5fa22a12010-11-09 23:28:44 +0000278 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000279 assert((Kind == RegisterList || Kind == DPRRegisterList ||
280 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000281 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000282 }
283
Kevin Enderbycfe07242009-10-13 22:19:02 +0000284 const MCExpr *getImm() const {
285 assert(Kind == Immediate && "Invalid access!");
286 return Imm.Val;
287 }
288
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000289 ARM_MB::MemBOpt getMemBarrierOpt() const {
290 assert(Kind == MemBarrierOpt && "Invalid access!");
291 return MBOpt.Val;
292 }
293
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000294 ARM_PROC::IFlags getProcIFlags() const {
295 assert(Kind == ProcIFlags && "Invalid access!");
296 return IFlags.Val;
297 }
298
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000299 unsigned getMSRMask() const {
300 assert(Kind == MSRMask && "Invalid access!");
301 return MMask.Val;
302 }
303
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000304 /// @name Memory Operand Accessors
305 /// @{
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000306 ARMII::AddrMode getMemAddrMode() const {
307 return Mem.AddrMode;
308 }
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000309 unsigned getMemBaseRegNum() const {
310 return Mem.BaseRegNum;
311 }
312 unsigned getMemOffsetRegNum() const {
313 assert(Mem.OffsetIsReg && "Invalid access!");
314 return Mem.Offset.RegNum;
315 }
316 const MCExpr *getMemOffset() const {
317 assert(!Mem.OffsetIsReg && "Invalid access!");
318 return Mem.Offset.Value;
319 }
320 unsigned getMemOffsetRegShifted() const {
321 assert(Mem.OffsetIsReg && "Invalid access!");
322 return Mem.OffsetRegShifted;
323 }
324 const MCExpr *getMemShiftAmount() const {
325 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
326 return Mem.ShiftAmount;
327 }
Owen Anderson00828302011-03-18 22:50:18 +0000328 enum ARM_AM::ShiftOpc getMemShiftType() const {
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000329 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
330 return Mem.ShiftType;
331 }
332 bool getMemPreindexed() const { return Mem.Preindexed; }
333 bool getMemPostindexed() const { return Mem.Postindexed; }
334 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
335 bool getMemNegative() const { return Mem.Negative; }
336 bool getMemWriteback() const { return Mem.Writeback; }
337
338 /// @}
339
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000340 bool isCoprocNum() const { return Kind == CoprocNum; }
341 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000342 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000343 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000344 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000345 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000346 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000347 bool isDPRRegList() const { return Kind == DPRRegisterList; }
348 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000349 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000350 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000351 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000352 bool isShifter() const { return Kind == Shifter; }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000353 bool isMemMode2() const {
354 if (getMemAddrMode() != ARMII::AddrMode2)
355 return false;
356
357 if (getMemOffsetIsReg())
358 return true;
359
360 if (getMemNegative() &&
361 !(getMemPostindexed() || getMemPreindexed()))
362 return false;
363
364 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
365 if (!CE) return false;
366 int64_t Value = CE->getValue();
367
368 // The offset must be in the range 0-4095 (imm12).
369 if (Value > 4095 || Value < -4095)
370 return false;
371
372 return true;
373 }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000374 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000375 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
376 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000377 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000378
Daniel Dunbar4b462672011-01-18 05:55:27 +0000379 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000380 if (!CE) return false;
381
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000382 // The offset must be a multiple of 4 in the range 0-1020.
383 int64_t Value = CE->getValue();
384 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
385 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000386 bool isMemMode7() const {
387 if (!isMemory() ||
388 getMemPreindexed() ||
389 getMemPostindexed() ||
390 getMemOffsetIsReg() ||
391 getMemNegative() ||
392 getMemWriteback())
393 return false;
394
395 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
396 if (!CE) return false;
397
398 if (CE->getValue())
399 return false;
400
401 return true;
402 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000403 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000404 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000405 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000406 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000407 }
408 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000409 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000410 return false;
411
Daniel Dunbar4b462672011-01-18 05:55:27 +0000412 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000413 if (!CE) return false;
414
415 // The offset must be a multiple of 4 in the range 0-124.
416 uint64_t Value = CE->getValue();
417 return ((Value & 0x3) == 0 && Value <= 124);
418 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000419 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000420 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000421
422 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000423 // Add as immediates when possible. Null MCExpr = 0.
424 if (Expr == 0)
425 Inst.addOperand(MCOperand::CreateImm(0));
426 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000427 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
428 else
429 Inst.addOperand(MCOperand::CreateExpr(Expr));
430 }
431
Daniel Dunbar8462b302010-08-11 06:36:53 +0000432 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000433 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000434 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000435 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
436 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000437 }
438
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000439 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
440 assert(N == 1 && "Invalid number of operands!");
441 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
442 }
443
444 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
445 assert(N == 1 && "Invalid number of operands!");
446 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
447 }
448
Jim Grosbachd67641b2010-12-06 18:21:12 +0000449 void addCCOutOperands(MCInst &Inst, unsigned N) const {
450 assert(N == 1 && "Invalid number of operands!");
451 Inst.addOperand(MCOperand::CreateReg(getReg()));
452 }
453
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000454 void addRegOperands(MCInst &Inst, unsigned N) const {
455 assert(N == 1 && "Invalid number of operands!");
456 Inst.addOperand(MCOperand::CreateReg(getReg()));
457 }
458
Owen Anderson00828302011-03-18 22:50:18 +0000459 void addShifterOperands(MCInst &Inst, unsigned N) const {
460 assert(N == 1 && "Invalid number of operands!");
461 Inst.addOperand(MCOperand::CreateImm(
462 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
463 }
464
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000465 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000466 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000467 const SmallVectorImpl<unsigned> &RegList = getRegList();
468 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000469 I = RegList.begin(), E = RegList.end(); I != E; ++I)
470 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000471 }
472
Bill Wendling0f630752010-11-17 04:32:08 +0000473 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
474 addRegListOperands(Inst, N);
475 }
476
477 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
478 addRegListOperands(Inst, N);
479 }
480
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000481 void addImmOperands(MCInst &Inst, unsigned N) const {
482 assert(N == 1 && "Invalid number of operands!");
483 addExpr(Inst, getImm());
484 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000485
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000486 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
487 assert(N == 1 && "Invalid number of operands!");
488 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
489 }
490
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000491 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
492 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
493 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
494
495 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000496 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000497 assert((CE || CE->getValue() == 0) &&
498 "No offset operand support in mode 7");
499 }
500
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000501 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
502 assert(isMemMode2() && "Invalid mode or number of operands!");
503 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
504 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
505
506 if (getMemOffsetIsReg()) {
507 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
508
509 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
510 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
511 int64_t ShiftAmount = 0;
512
513 if (getMemOffsetRegShifted()) {
514 ShOpc = getMemShiftType();
515 const MCConstantExpr *CE =
516 dyn_cast<MCConstantExpr>(getMemShiftAmount());
517 ShiftAmount = CE->getValue();
518 }
519
520 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
521 ShOpc, IdxMode)));
522 return;
523 }
524
525 // Create a operand placeholder to always yield the same number of operands.
526 Inst.addOperand(MCOperand::CreateReg(0));
527
528 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
529 // the difference?
530 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
531 assert(CE && "Non-constant mode 2 offset operand!");
532 int64_t Offset = CE->getValue();
533
534 if (Offset >= 0)
535 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
536 Offset, ARM_AM::no_shift, IdxMode)));
537 else
538 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
539 -Offset, ARM_AM::no_shift, IdxMode)));
540 }
541
Chris Lattner14b93852010-10-29 00:27:31 +0000542 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
543 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000544
Daniel Dunbar4b462672011-01-18 05:55:27 +0000545 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
546 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000547
Jim Grosbach80eb2332010-10-29 17:41:25 +0000548 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
549 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000550 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000551 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000552
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000553 // The MCInst offset operand doesn't include the low two bits (like
554 // the instruction encoding).
555 int64_t Offset = CE->getValue() / 4;
556 if (Offset >= 0)
557 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
558 Offset)));
559 else
560 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
561 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000562 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000563
Bill Wendlingf4caf692010-12-14 03:36:38 +0000564 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
565 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000566 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
567 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000568 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000569
Bill Wendlingf4caf692010-12-14 03:36:38 +0000570 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
571 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000572 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
573 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000574 assert(CE && "Non-constant mode offset operand!");
575 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000576 }
577
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000578 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
579 assert(N == 1 && "Invalid number of operands!");
580 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
581 }
582
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000583 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
584 assert(N == 1 && "Invalid number of operands!");
585 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
586 }
587
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000588 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000589
Chris Lattner3a697562010-10-28 17:20:03 +0000590 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
591 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000592 Op->CC.Val = CC;
593 Op->StartLoc = S;
594 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000595 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000596 }
597
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000598 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
599 ARMOperand *Op = new ARMOperand(CoprocNum);
600 Op->Cop.Val = CopVal;
601 Op->StartLoc = S;
602 Op->EndLoc = S;
603 return Op;
604 }
605
606 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
607 ARMOperand *Op = new ARMOperand(CoprocReg);
608 Op->Cop.Val = CopVal;
609 Op->StartLoc = S;
610 Op->EndLoc = S;
611 return Op;
612 }
613
Jim Grosbachd67641b2010-12-06 18:21:12 +0000614 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
615 ARMOperand *Op = new ARMOperand(CCOut);
616 Op->Reg.RegNum = RegNum;
617 Op->StartLoc = S;
618 Op->EndLoc = S;
619 return Op;
620 }
621
Chris Lattner3a697562010-10-28 17:20:03 +0000622 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
623 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000624 Op->Tok.Data = Str.data();
625 Op->Tok.Length = Str.size();
626 Op->StartLoc = S;
627 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000628 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000629 }
630
Bill Wendling50d0f582010-11-18 23:43:05 +0000631 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000632 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000633 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000634 Op->StartLoc = S;
635 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000636 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000637 }
638
Owen Anderson00828302011-03-18 22:50:18 +0000639 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
640 SMLoc S, SMLoc E) {
641 ARMOperand *Op = new ARMOperand(Shifter);
642 Op->Shift.ShiftTy = ShTy;
643 Op->StartLoc = S;
644 Op->EndLoc = E;
645 return Op;
646 }
647
Bill Wendling7729e062010-11-09 22:44:22 +0000648 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000649 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000650 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000651 KindTy Kind = RegisterList;
652
653 if (ARM::DPRRegClass.contains(Regs.front().first))
654 Kind = DPRRegisterList;
655 else if (ARM::SPRRegClass.contains(Regs.front().first))
656 Kind = SPRRegisterList;
657
658 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000659 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000660 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000661 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000662 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000663 Op->StartLoc = StartLoc;
664 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000665 return Op;
666 }
667
Chris Lattner3a697562010-10-28 17:20:03 +0000668 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
669 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000670 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000671 Op->StartLoc = S;
672 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000673 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000674 }
675
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000676 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
677 bool OffsetIsReg, const MCExpr *Offset,
678 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000679 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000680 const MCExpr *ShiftAmount, bool Preindexed,
681 bool Postindexed, bool Negative, bool Writeback,
682 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000683 assert((OffsetRegNum == -1 || OffsetIsReg) &&
684 "OffsetRegNum must imply OffsetIsReg!");
685 assert((!OffsetRegShifted || OffsetIsReg) &&
686 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000687 assert((Offset || OffsetIsReg) &&
688 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000689 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
690 "Cannot have shift amount without shifted register offset!");
691 assert((!Offset || !OffsetIsReg) &&
692 "Cannot have expression offset and register offset!");
693
Chris Lattner3a697562010-10-28 17:20:03 +0000694 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000695 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +0000696 Op->Mem.BaseRegNum = BaseRegNum;
697 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000698 if (OffsetIsReg)
699 Op->Mem.Offset.RegNum = OffsetRegNum;
700 else
701 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000702 Op->Mem.OffsetRegShifted = OffsetRegShifted;
703 Op->Mem.ShiftType = ShiftType;
704 Op->Mem.ShiftAmount = ShiftAmount;
705 Op->Mem.Preindexed = Preindexed;
706 Op->Mem.Postindexed = Postindexed;
707 Op->Mem.Negative = Negative;
708 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000709
Sean Callanan76264762010-04-02 22:27:05 +0000710 Op->StartLoc = S;
711 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000712 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000713 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000714
715 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
716 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
717 Op->MBOpt.Val = Opt;
718 Op->StartLoc = S;
719 Op->EndLoc = S;
720 return Op;
721 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000722
723 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
724 ARMOperand *Op = new ARMOperand(ProcIFlags);
725 Op->IFlags.Val = IFlags;
726 Op->StartLoc = S;
727 Op->EndLoc = S;
728 return Op;
729 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000730
731 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
732 ARMOperand *Op = new ARMOperand(MSRMask);
733 Op->MMask.Val = MMask;
734 Op->StartLoc = S;
735 Op->EndLoc = S;
736 return Op;
737 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000738};
739
740} // end anonymous namespace.
741
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000742void ARMOperand::dump(raw_ostream &OS) const {
743 switch (Kind) {
744 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000745 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000746 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000747 case CCOut:
748 OS << "<ccout " << getReg() << ">";
749 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000750 case CoprocNum:
751 OS << "<coprocessor number: " << getCoproc() << ">";
752 break;
753 case CoprocReg:
754 OS << "<coprocessor register: " << getCoproc() << ">";
755 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000756 case MSRMask:
757 OS << "<mask: " << getMSRMask() << ">";
758 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000759 case Immediate:
760 getImm()->print(OS);
761 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000762 case MemBarrierOpt:
763 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
764 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000765 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000766 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000767 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
768 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000769 if (getMemOffsetIsReg()) {
770 OS << " offset:<register " << getMemOffsetRegNum();
771 if (getMemOffsetRegShifted()) {
772 OS << " offset-shift-type:" << getMemShiftType();
773 OS << " offset-shift-amount:" << *getMemShiftAmount();
774 }
775 } else {
776 OS << " offset:" << *getMemOffset();
777 }
778 if (getMemOffsetIsReg())
779 OS << " (offset-is-reg)";
780 if (getMemPreindexed())
781 OS << " (pre-indexed)";
782 if (getMemPostindexed())
783 OS << " (post-indexed)";
784 if (getMemNegative())
785 OS << " (negative)";
786 if (getMemWriteback())
787 OS << " (writeback)";
788 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000789 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000790 case ProcIFlags: {
791 OS << "<ARM_PROC::";
792 unsigned IFlags = getProcIFlags();
793 for (int i=2; i >= 0; --i)
794 if (IFlags & (1 << i))
795 OS << ARM_PROC::IFlagsToString(1 << i);
796 OS << ">";
797 break;
798 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000799 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000800 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000801 break;
Owen Anderson00828302011-03-18 22:50:18 +0000802 case Shifter:
803 OS << "<shifter " << getShiftOpcStr(Shift.ShiftTy) << ">";
804 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000805 case RegisterList:
806 case DPRRegisterList:
807 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000808 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000809
Bill Wendling5fa22a12010-11-09 23:28:44 +0000810 const SmallVectorImpl<unsigned> &RegList = getRegList();
811 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000812 I = RegList.begin(), E = RegList.end(); I != E; ) {
813 OS << *I;
814 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000815 }
816
817 OS << ">";
818 break;
819 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000820 case Token:
821 OS << "'" << getToken() << "'";
822 break;
823 }
824}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000825
826/// @name Auto-generated Match Functions
827/// {
828
829static unsigned MatchRegisterName(StringRef Name);
830
831/// }
832
Bob Wilson69df7232011-02-03 21:46:10 +0000833bool ARMAsmParser::ParseRegister(unsigned &RegNo,
834 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +0000835 RegNo = TryParseRegister();
836
837 return (RegNo == (unsigned)-1);
838}
839
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000840/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000841/// and if it is a register name the token is eaten and the register number is
842/// returned. Otherwise return -1.
843///
844int ARMAsmParser::TryParseRegister() {
845 const AsmToken &Tok = Parser.getTok();
846 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000847
Chris Lattnere5658fa2010-10-30 04:09:10 +0000848 // FIXME: Validate register for the current architecture; we have to do
849 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000850 std::string upperCase = Tok.getString().str();
851 std::string lowerCase = LowercaseString(upperCase);
852 unsigned RegNum = MatchRegisterName(lowerCase);
853 if (!RegNum) {
854 RegNum = StringSwitch<unsigned>(lowerCase)
855 .Case("r13", ARM::SP)
856 .Case("r14", ARM::LR)
857 .Case("r15", ARM::PC)
858 .Case("ip", ARM::R12)
859 .Default(0);
860 }
861 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +0000862
Chris Lattnere5658fa2010-10-30 04:09:10 +0000863 Parser.Lex(); // Eat identifier token.
864 return RegNum;
865}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000866
Owen Anderson00828302011-03-18 22:50:18 +0000867/// Try to parse a register name. The token must be an Identifier when called,
868/// and if it is a register name the token is eaten and the register number is
869/// returned. Otherwise return -1.
870///
871bool ARMAsmParser::TryParseShiftRegister(
872 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
873 SMLoc S = Parser.getTok().getLoc();
874 const AsmToken &Tok = Parser.getTok();
875 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
876
877 std::string upperCase = Tok.getString().str();
878 std::string lowerCase = LowercaseString(upperCase);
879 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
880 .Case("lsl", ARM_AM::lsl)
881 .Case("lsr", ARM_AM::lsr)
882 .Case("asr", ARM_AM::asr)
883 .Case("ror", ARM_AM::ror)
884 .Case("rrx", ARM_AM::rrx)
885 .Default(ARM_AM::no_shift);
886
887 if (ShiftTy == ARM_AM::no_shift)
888 return true;
889
890 Parser.Lex(); // Eat shift-type operand;
891 int RegNum = TryParseRegister();
892 if (RegNum == -1)
893 return Error(Parser.getTok().getLoc(), "register expected");
894
895 Operands.push_back(ARMOperand::CreateReg(RegNum,S, Parser.getTok().getLoc()));
896 Operands.push_back(ARMOperand::CreateShifter(ShiftTy,
897 S, Parser.getTok().getLoc()));
898
899 return false;
900}
901
902
Bill Wendling50d0f582010-11-18 23:43:05 +0000903/// Try to parse a register name. The token must be an Identifier when called.
904/// If it's a register, an AsmOperand is created. Another AsmOperand is created
905/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000906///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000907/// TODO this is likely to change to allow different register types and or to
908/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000909bool ARMAsmParser::
910TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000911 SMLoc S = Parser.getTok().getLoc();
912 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000913 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000914 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000915
Bill Wendling50d0f582010-11-18 23:43:05 +0000916 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000917
Chris Lattnere5658fa2010-10-30 04:09:10 +0000918 const AsmToken &ExclaimTok = Parser.getTok();
919 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000920 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
921 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000922 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000923 }
924
Bill Wendling50d0f582010-11-18 23:43:05 +0000925 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000926}
927
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000928/// MatchCoprocessorOperandName - Try to parse an coprocessor related
929/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
930/// "c5", ...
931static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000932 // Use the same layout as the tablegen'erated register name matcher. Ugly,
933 // but efficient.
934 switch (Name.size()) {
935 default: break;
936 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000937 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000938 return -1;
939 switch (Name[1]) {
940 default: return -1;
941 case '0': return 0;
942 case '1': return 1;
943 case '2': return 2;
944 case '3': return 3;
945 case '4': return 4;
946 case '5': return 5;
947 case '6': return 6;
948 case '7': return 7;
949 case '8': return 8;
950 case '9': return 9;
951 }
952 break;
953 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000954 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000955 return -1;
956 switch (Name[2]) {
957 default: return -1;
958 case '0': return 10;
959 case '1': return 11;
960 case '2': return 12;
961 case '3': return 13;
962 case '4': return 14;
963 case '5': return 15;
964 }
965 break;
966 }
967
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000968 return -1;
969}
970
Jim Grosbachf922c472011-02-12 01:34:40 +0000971/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000972/// token must be an Identifier when called, and if it is a coprocessor
973/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +0000974ARMAsmParser::OperandMatchResultTy ARMAsmParser::
975tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000976 SMLoc S = Parser.getTok().getLoc();
977 const AsmToken &Tok = Parser.getTok();
978 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
979
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000980 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000981 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +0000982 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000983
984 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000985 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +0000986 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000987}
988
Jim Grosbachf922c472011-02-12 01:34:40 +0000989/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000990/// token must be an Identifier when called, and if it is a coprocessor
991/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +0000992ARMAsmParser::OperandMatchResultTy ARMAsmParser::
993tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000994 SMLoc S = Parser.getTok().getLoc();
995 const AsmToken &Tok = Parser.getTok();
996 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
997
998 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
999 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001000 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001001
1002 Parser.Lex(); // Eat identifier token.
1003 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001004 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001005}
1006
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001007/// Parse a register list, return it if successful else return null. The first
1008/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001009bool ARMAsmParser::
1010ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001011 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001012 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001013 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001014
Bill Wendling7729e062010-11-09 22:44:22 +00001015 // Read the rest of the registers in the list.
1016 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001017 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001018
Bill Wendling7729e062010-11-09 22:44:22 +00001019 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001020 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001021 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001022
Sean Callanan18b83232010-01-19 21:44:56 +00001023 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001024 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001025 if (RegTok.isNot(AsmToken::Identifier)) {
1026 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001027 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001028 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001029
Bill Wendling1d6a2652010-11-06 10:40:24 +00001030 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001031 if (RegNum == -1) {
1032 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001033 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001034 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001035
Bill Wendlinge7176102010-11-06 22:36:58 +00001036 if (IsRange) {
1037 int Reg = PrevRegNum;
1038 do {
1039 ++Reg;
1040 Registers.push_back(std::make_pair(Reg, RegLoc));
1041 } while (Reg != RegNum);
1042 } else {
1043 Registers.push_back(std::make_pair(RegNum, RegLoc));
1044 }
1045
1046 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001047 } while (Parser.getTok().is(AsmToken::Comma) ||
1048 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001049
1050 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001051 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001052 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1053 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001054 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001055 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001056
Bill Wendlinge7176102010-11-06 22:36:58 +00001057 SMLoc E = RCurlyTok.getLoc();
1058 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001059
Bill Wendlinge7176102010-11-06 22:36:58 +00001060 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001061 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001062 RI = Registers.begin(), RE = Registers.end();
1063
Bill Wendling7caebff2011-01-12 21:20:59 +00001064 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001065 bool EmittedWarning = false;
1066
Bill Wendling7caebff2011-01-12 21:20:59 +00001067 DenseMap<unsigned, bool> RegMap;
1068 RegMap[HighRegNum] = true;
1069
Bill Wendlinge7176102010-11-06 22:36:58 +00001070 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001071 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001072 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001073
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001074 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001075 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001076 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001077 }
1078
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001079 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001080 Warning(RegInfo.second,
1081 "register not in ascending order in register list");
1082
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001083 RegMap[Reg] = true;
1084 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001085 }
1086
Bill Wendling50d0f582010-11-18 23:43:05 +00001087 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1088 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001089}
1090
Jim Grosbachf922c472011-02-12 01:34:40 +00001091/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1092ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1093tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001094 SMLoc S = Parser.getTok().getLoc();
1095 const AsmToken &Tok = Parser.getTok();
1096 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1097 StringRef OptStr = Tok.getString();
1098
1099 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1100 .Case("sy", ARM_MB::SY)
1101 .Case("st", ARM_MB::ST)
1102 .Case("ish", ARM_MB::ISH)
1103 .Case("ishst", ARM_MB::ISHST)
1104 .Case("nsh", ARM_MB::NSH)
1105 .Case("nshst", ARM_MB::NSHST)
1106 .Case("osh", ARM_MB::OSH)
1107 .Case("oshst", ARM_MB::OSHST)
1108 .Default(~0U);
1109
1110 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001111 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001112
1113 Parser.Lex(); // Eat identifier token.
1114 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001115 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001116}
1117
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001118/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001119ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1120tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1121 SMLoc S = Parser.getTok().getLoc();
1122 const AsmToken &Tok = Parser.getTok();
1123 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1124 StringRef IFlagsStr = Tok.getString();
1125
1126 unsigned IFlags = 0;
1127 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1128 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1129 .Case("a", ARM_PROC::A)
1130 .Case("i", ARM_PROC::I)
1131 .Case("f", ARM_PROC::F)
1132 .Default(~0U);
1133
1134 // If some specific iflag is already set, it means that some letter is
1135 // present more than once, this is not acceptable.
1136 if (Flag == ~0U || (IFlags & Flag))
1137 return MatchOperand_NoMatch;
1138
1139 IFlags |= Flag;
1140 }
1141
1142 Parser.Lex(); // Eat identifier token.
1143 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1144 return MatchOperand_Success;
1145}
1146
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001147/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1148ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1149tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1150 SMLoc S = Parser.getTok().getLoc();
1151 const AsmToken &Tok = Parser.getTok();
1152 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1153 StringRef Mask = Tok.getString();
1154
1155 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1156 size_t Start = 0, Next = Mask.find('_');
1157 StringRef Flags = "";
1158 StringRef SpecReg = Mask.slice(Start, Next);
1159 if (Next != StringRef::npos)
1160 Flags = Mask.slice(Next+1, Mask.size());
1161
1162 // FlagsVal contains the complete mask:
1163 // 3-0: Mask
1164 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1165 unsigned FlagsVal = 0;
1166
1167 if (SpecReg == "apsr") {
1168 FlagsVal = StringSwitch<unsigned>(Flags)
1169 .Case("nzcvq", 0x8) // same as CPSR_c
1170 .Case("g", 0x4) // same as CPSR_s
1171 .Case("nzcvqg", 0xc) // same as CPSR_fs
1172 .Default(~0U);
1173
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001174 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001175 if (!Flags.empty())
1176 return MatchOperand_NoMatch;
1177 else
1178 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001179 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001180 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
1181 for (int i = 0, e = Flags.size(); i != e; ++i) {
1182 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1183 .Case("c", 1)
1184 .Case("x", 2)
1185 .Case("s", 4)
1186 .Case("f", 8)
1187 .Default(~0U);
1188
1189 // If some specific flag is already set, it means that some letter is
1190 // present more than once, this is not acceptable.
1191 if (FlagsVal == ~0U || (FlagsVal & Flag))
1192 return MatchOperand_NoMatch;
1193 FlagsVal |= Flag;
1194 }
1195 } else // No match for special register.
1196 return MatchOperand_NoMatch;
1197
1198 // Special register without flags are equivalent to "fc" flags.
1199 if (!FlagsVal)
1200 FlagsVal = 0x9;
1201
1202 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1203 if (SpecReg == "spsr")
1204 FlagsVal |= 16;
1205
1206 Parser.Lex(); // Eat identifier token.
1207 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1208 return MatchOperand_Success;
1209}
1210
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001211/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1212ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1213tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001214 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001215
1216 if (ParseMemory(Operands, ARMII::AddrMode2))
1217 return MatchOperand_NoMatch;
1218
1219 return MatchOperand_Success;
1220}
1221
1222/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1223/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1224/// when they refer multiple MIOperands inside a single one.
1225bool ARMAsmParser::
1226CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1227 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1228 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1229
1230 // Create a writeback register dummy placeholder.
1231 Inst.addOperand(MCOperand::CreateImm(0));
1232
1233 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1234 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1235 return true;
1236}
1237
1238/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1239/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1240/// when they refer multiple MIOperands inside a single one.
1241bool ARMAsmParser::
1242CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1243 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1244 // Create a writeback register dummy placeholder.
1245 Inst.addOperand(MCOperand::CreateImm(0));
1246 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1247 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1248 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1249 return true;
1250}
1251
Bill Wendlinge7176102010-11-06 22:36:58 +00001252/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001253/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001254///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001255/// TODO Only preindexing and postindexing addressing are started, unindexed
1256/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001257bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001258ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1259 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001260 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001261 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001262 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001263 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001264 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001265
Sean Callanan18b83232010-01-19 21:44:56 +00001266 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001267 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1268 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001269 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001270 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001271 int BaseRegNum = TryParseRegister();
1272 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001273 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001274 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001275 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001276
Daniel Dunbar05710932011-01-18 05:34:17 +00001277 // The next token must either be a comma or a closing bracket.
1278 const AsmToken &Tok = Parser.getTok();
1279 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1280 return true;
1281
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001282 bool Preindexed = false;
1283 bool Postindexed = false;
1284 bool OffsetIsReg = false;
1285 bool Negative = false;
1286 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001287 ARMOperand *WBOp = 0;
1288 int OffsetRegNum = -1;
1289 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001290 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001291 const MCExpr *ShiftAmount = 0;
1292 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001293
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001294 // First look for preindexed address forms, that is after the "[Rn" we now
1295 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001296 if (Tok.is(AsmToken::Comma)) {
1297 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001298 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001299
Chris Lattner550276e2010-10-28 20:52:15 +00001300 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1301 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001302 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001303 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001304 if (RBracTok.isNot(AsmToken::RBrac)) {
1305 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001306 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001307 }
Sean Callanan76264762010-04-02 22:27:05 +00001308 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001309 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001310
Sean Callanan18b83232010-01-19 21:44:56 +00001311 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001312 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001313 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1314 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001315 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001316 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001317 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1318 if (AddrMode == ARMII::AddrMode2)
1319 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001320 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001321 } else {
1322 // The "[Rn" we have so far was not followed by a comma.
1323
Jim Grosbach80eb2332010-10-29 17:41:25 +00001324 // If there's anything other than the right brace, this is a post indexing
1325 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001326 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001327 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001328
Sean Callanan18b83232010-01-19 21:44:56 +00001329 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001330
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001331 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001332 Postindexed = true;
1333 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001334
Chris Lattner550276e2010-10-28 20:52:15 +00001335 if (NextTok.isNot(AsmToken::Comma)) {
1336 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001337 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001338 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001339
Sean Callananb9a25b72010-01-19 20:27:46 +00001340 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001341
Chris Lattner550276e2010-10-28 20:52:15 +00001342 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001343 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001344 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001345 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001346 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001347 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001348
1349 // Force Offset to exist if used.
1350 if (!OffsetIsReg) {
1351 if (!Offset)
1352 Offset = MCConstantExpr::Create(0, getContext());
1353 }
1354
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001355 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1356 Offset, OffsetRegNum, OffsetRegShifted,
1357 ShiftType, ShiftAmount, Preindexed,
1358 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001359 if (WBOp)
1360 Operands.push_back(WBOp);
1361
1362 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001363}
1364
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001365/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1366/// we will parse the following (were +/- means that a plus or minus is
1367/// optional):
1368/// +/-Rm
1369/// +/-Rm, shift
1370/// #offset
1371/// we return false on success or an error otherwise.
1372bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001373 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001374 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001375 const MCExpr *&ShiftAmount,
1376 const MCExpr *&Offset,
1377 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001378 int &OffsetRegNum,
1379 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001380 Negative = false;
1381 OffsetRegShifted = false;
1382 OffsetIsReg = false;
1383 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001384 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001385 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001386 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001387 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001388 else if (NextTok.is(AsmToken::Minus)) {
1389 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001390 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001391 }
1392 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001393 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001394 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001395 SMLoc CurLoc = OffsetRegTok.getLoc();
1396 OffsetRegNum = TryParseRegister();
1397 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001398 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001399 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001400 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001401 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001402
Bill Wendling12f40e92010-11-06 10:51:53 +00001403 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001404 if (OffsetRegNum != -1) {
1405 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001406 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001407 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001408 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001409
Sean Callanan18b83232010-01-19 21:44:56 +00001410 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001411 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001412 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001413 OffsetRegShifted = true;
1414 }
1415 }
1416 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1417 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001418 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001419 if (HashTok.isNot(AsmToken::Hash))
1420 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001421
Sean Callananb9a25b72010-01-19 20:27:46 +00001422 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001423
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001424 if (getParser().ParseExpression(Offset))
1425 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001426 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001427 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001428 return false;
1429}
1430
1431/// ParseShift as one of these two:
1432/// ( lsl | lsr | asr | ror ) , # shift_amount
1433/// rrx
1434/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001435bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1436 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001437 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001438 if (Tok.isNot(AsmToken::Identifier))
1439 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001440 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001441 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001442 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001443 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001444 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001445 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001446 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001447 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001448 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001449 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001450 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001451 else
1452 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001453 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001454
1455 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001456 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001457 return false;
1458
1459 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001460 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001461 if (HashTok.isNot(AsmToken::Hash))
1462 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001463 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001464
1465 if (getParser().ParseExpression(ShiftAmount))
1466 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001467
1468 return false;
1469}
1470
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001471/// Parse a arm instruction operand. For now this parses the operand regardless
1472/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001473bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001474 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001475 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001476
1477 // Check if the current operand has a custom associated parser, if so, try to
1478 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001479 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1480 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001481 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001482 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1483 // there was a match, but an error occurred, in which case, just return that
1484 // the operand parsing failed.
1485 if (ResTy == MatchOperand_ParseFail)
1486 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001487
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001488 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001489 default:
1490 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001491 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +00001492 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +00001493 if (!TryParseRegisterWithWriteBack(Operands))
1494 return false;
Owen Anderson00828302011-03-18 22:50:18 +00001495 if (!TryParseShiftRegister(Operands))
1496 return false;
1497
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001498
1499 // Fall though for the Identifier case that is not a register or a
1500 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +00001501 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1502 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001503 // This was not a register so parse other operands that start with an
1504 // identifier (like labels) as expressions and create them as immediates.
1505 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001506 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001507 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001508 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001509 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001510 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1511 return false;
1512 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001513 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001514 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001515 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001516 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001517 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001518 // #42 -> immediate.
1519 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001520 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001521 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001522 const MCExpr *ImmVal;
1523 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001524 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001525 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001526 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1527 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001528 case AsmToken::Colon: {
1529 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001530 // FIXME: Check it's an expression prefix,
1531 // e.g. (FOO - :lower16:BAR) isn't legal.
1532 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001533 if (ParsePrefix(RefKind))
1534 return true;
1535
Evan Cheng75972122011-01-13 07:58:56 +00001536 const MCExpr *SubExprVal;
1537 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001538 return true;
1539
Evan Cheng75972122011-01-13 07:58:56 +00001540 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1541 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001542 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001543 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001544 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001545 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001546 }
1547}
1548
Evan Cheng75972122011-01-13 07:58:56 +00001549// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1550// :lower16: and :upper16:.
1551bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1552 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001553
1554 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001555 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001556 Parser.Lex(); // Eat ':'
1557
1558 if (getLexer().isNot(AsmToken::Identifier)) {
1559 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1560 return true;
1561 }
1562
1563 StringRef IDVal = Parser.getTok().getIdentifier();
1564 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001565 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001566 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001567 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001568 } else {
1569 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1570 return true;
1571 }
1572 Parser.Lex();
1573
1574 if (getLexer().isNot(AsmToken::Colon)) {
1575 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1576 return true;
1577 }
1578 Parser.Lex(); // Eat the last ':'
1579 return false;
1580}
1581
1582const MCExpr *
1583ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1584 MCSymbolRefExpr::VariantKind Variant) {
1585 // Recurse over the given expression, rebuilding it to apply the given variant
1586 // to the leftmost symbol.
1587 if (Variant == MCSymbolRefExpr::VK_None)
1588 return E;
1589
1590 switch (E->getKind()) {
1591 case MCExpr::Target:
1592 llvm_unreachable("Can't handle target expr yet");
1593 case MCExpr::Constant:
1594 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1595
1596 case MCExpr::SymbolRef: {
1597 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1598
1599 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1600 return 0;
1601
1602 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1603 }
1604
1605 case MCExpr::Unary:
1606 llvm_unreachable("Can't handle unary expressions yet");
1607
1608 case MCExpr::Binary: {
1609 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1610 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1611 const MCExpr *RHS = BE->getRHS();
1612 if (!LHS)
1613 return 0;
1614
1615 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1616 }
1617 }
1618
1619 assert(0 && "Invalid expression kind!");
1620 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001621}
1622
Daniel Dunbar352e1482011-01-11 15:59:50 +00001623/// \brief Given a mnemonic, split out possible predication code and carry
1624/// setting letters to form a canonical mnemonic and flags.
1625//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001626// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001627static StringRef SplitMnemonic(StringRef Mnemonic,
1628 unsigned &PredicationCode,
1629 bool &CarrySetting,
1630 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001631 PredicationCode = ARMCC::AL;
1632 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001633 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001634
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001635 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001636 //
1637 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001638 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1639 Mnemonic == "movs" ||
1640 Mnemonic == "svc" ||
1641 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1642 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1643 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1644 Mnemonic == "vclt" ||
1645 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1646 Mnemonic == "vcle" ||
1647 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1648 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1649 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001650 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001651
Daniel Dunbar352e1482011-01-11 15:59:50 +00001652 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001653 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001654 .Case("eq", ARMCC::EQ)
1655 .Case("ne", ARMCC::NE)
1656 .Case("hs", ARMCC::HS)
1657 .Case("lo", ARMCC::LO)
1658 .Case("mi", ARMCC::MI)
1659 .Case("pl", ARMCC::PL)
1660 .Case("vs", ARMCC::VS)
1661 .Case("vc", ARMCC::VC)
1662 .Case("hi", ARMCC::HI)
1663 .Case("ls", ARMCC::LS)
1664 .Case("ge", ARMCC::GE)
1665 .Case("lt", ARMCC::LT)
1666 .Case("gt", ARMCC::GT)
1667 .Case("le", ARMCC::LE)
1668 .Case("al", ARMCC::AL)
1669 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001670 if (CC != ~0U) {
1671 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001672 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001673 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001674
Daniel Dunbar352e1482011-01-11 15:59:50 +00001675 // Next, determine if we have a carry setting bit. We explicitly ignore all
1676 // the instructions we know end in 's'.
1677 if (Mnemonic.endswith("s") &&
1678 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1679 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1680 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1681 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1682 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1683 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1684 CarrySetting = true;
1685 }
1686
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001687 // The "cps" instruction can have a interrupt mode operand which is glued into
1688 // the mnemonic. Check if this is the case, split it and parse the imod op
1689 if (Mnemonic.startswith("cps")) {
1690 // Split out any imod code.
1691 unsigned IMod =
1692 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1693 .Case("ie", ARM_PROC::IE)
1694 .Case("id", ARM_PROC::ID)
1695 .Default(~0U);
1696 if (IMod != ~0U) {
1697 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
1698 ProcessorIMod = IMod;
1699 }
1700 }
1701
Daniel Dunbar352e1482011-01-11 15:59:50 +00001702 return Mnemonic;
1703}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001704
1705/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1706/// inclusion of carry set or predication code operands.
1707//
1708// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001709void ARMAsmParser::
1710GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1711 bool &CanAcceptPredicationCode) {
1712 bool isThumb = TM.getSubtarget<ARMSubtarget>().isThumb();
1713
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001714 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1715 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1716 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1717 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1718 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1719 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1720 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1721 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1722 CanAcceptCarrySet = true;
1723 } else {
1724 CanAcceptCarrySet = false;
1725 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001726
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001727 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1728 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1729 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1730 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00001731 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001732 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001733 CanAcceptPredicationCode = false;
1734 } else {
1735 CanAcceptPredicationCode = true;
1736 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001737
1738 if (isThumb)
1739 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Bruno Cardoso Lopes8dd37f72011-01-20 18:32:09 +00001740 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001741 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001742}
1743
1744/// Parse an arm instruction mnemonic followed by its operands.
1745bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1746 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1747 // Create the leading tokens for the mnemonic, split by '.' characters.
1748 size_t Start = 0, Next = Name.find('.');
1749 StringRef Head = Name.slice(Start, Next);
1750
Daniel Dunbar352e1482011-01-11 15:59:50 +00001751 // Split out the predication code and carry setting flag from the mnemonic.
1752 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001753 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001754 bool CarrySetting;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001755 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
1756 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001757
Chris Lattner3a697562010-10-28 17:20:03 +00001758 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001759
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001760 // Next, add the CCOut and ConditionCode operands, if needed.
1761 //
1762 // For mnemonics which can ever incorporate a carry setting bit or predication
1763 // code, our matching model involves us always generating CCOut and
1764 // ConditionCode operands to match the mnemonic "as written" and then we let
1765 // the matcher deal with finding the right instruction or generating an
1766 // appropriate error.
1767 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1768 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1769
1770 // Add the carry setting operand, if necessary.
1771 //
1772 // FIXME: It would be awesome if we could somehow invent a location such that
1773 // match errors on this operand would print a nice diagnostic about how the
1774 // 's' character in the mnemonic resulted in a CCOut operand.
1775 if (CanAcceptCarrySet) {
1776 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1777 NameLoc));
1778 } else {
1779 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1780 // misspelled another mnemonic).
1781
1782 // FIXME: Issue a nice error.
1783 }
1784
1785 // Add the predication code operand, if necessary.
1786 if (CanAcceptPredicationCode) {
1787 Operands.push_back(ARMOperand::CreateCondCode(
1788 ARMCC::CondCodes(PredicationCode), NameLoc));
1789 } else {
1790 // This mnemonic can't ever accept a predication code, but the user wrote
1791 // one (or misspelled another mnemonic).
1792
1793 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001794 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001795
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001796 // Add the processor imod operand, if necessary.
1797 if (ProcessorIMod) {
1798 Operands.push_back(ARMOperand::CreateImm(
1799 MCConstantExpr::Create(ProcessorIMod, getContext()),
1800 NameLoc, NameLoc));
1801 } else {
1802 // This mnemonic can't ever accept a imod, but the user wrote
1803 // one (or misspelled another mnemonic).
1804
1805 // FIXME: Issue a nice error.
1806 }
1807
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001808 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001809 while (Next != StringRef::npos) {
1810 Start = Next;
1811 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001812 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001813
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001814 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001815 }
1816
1817 // Read the remaining operands.
1818 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001819 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001820 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001821 Parser.EatToEndOfStatement();
1822 return true;
1823 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001824
1825 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001826 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001827
1828 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001829 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001830 Parser.EatToEndOfStatement();
1831 return true;
1832 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001833 }
1834 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001835
Chris Lattnercbf8a982010-09-11 16:18:25 +00001836 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1837 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001838 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001839 }
Bill Wendling146018f2010-11-06 21:42:12 +00001840
Chris Lattner34e53142010-09-08 05:10:46 +00001841 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001842 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001843}
1844
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001845bool ARMAsmParser::
1846MatchAndEmitInstruction(SMLoc IDLoc,
1847 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1848 MCStreamer &Out) {
1849 MCInst Inst;
1850 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001851 MatchResultTy MatchResult, MatchResult2;
1852 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1853 if (MatchResult != Match_Success) {
1854 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1855 // that does not update the condition codes. So try adding a CCOut operand
1856 // with a value of reg0.
1857 if (MatchResult == Match_InvalidOperand) {
1858 Operands.insert(Operands.begin() + 1,
1859 ARMOperand::CreateCCOut(0,
1860 ((ARMOperand*)Operands[0])->getStartLoc()));
1861 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1862 if (MatchResult2 == Match_Success)
1863 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001864 else {
1865 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001866 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001867 delete CCOut;
1868 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001869 }
1870 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1871 // that updates the condition codes if it ends in 's'. So see if the
1872 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1873 // operand with a value of CPSR.
1874 else if(MatchResult == Match_MnemonicFail) {
1875 // Get the instruction mnemonic, which is the first token.
1876 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1877 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1878 // removed the 's' from the mnemonic for matching.
1879 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1880 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001881 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1882 Operands.erase(Operands.begin());
1883 delete OldMnemonic;
1884 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001885 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1886 Operands.insert(Operands.begin() + 1,
1887 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1888 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1889 if (MatchResult2 == Match_Success)
1890 MatchResult = Match_Success;
1891 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001892 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1893 Operands.erase(Operands.begin());
1894 delete OldMnemonic;
1895 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001896 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001897 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1898 Operands.erase(Operands.begin() + 1);
1899 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001900 }
1901 }
1902 }
1903 }
1904 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001905 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001906 Out.EmitInstruction(Inst);
1907 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001908 case Match_MissingFeature:
1909 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1910 return true;
1911 case Match_InvalidOperand: {
1912 SMLoc ErrorLoc = IDLoc;
1913 if (ErrorInfo != ~0U) {
1914 if (ErrorInfo >= Operands.size())
1915 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001916
Chris Lattnere73d4f82010-10-28 21:41:58 +00001917 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1918 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1919 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001920
Chris Lattnere73d4f82010-10-28 21:41:58 +00001921 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001922 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001923 case Match_MnemonicFail:
1924 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00001925 case Match_ConversionFail:
1926 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00001927 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001928
Eric Christopherc223e2b2010-10-29 09:26:59 +00001929 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001930 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001931}
1932
Kevin Enderby515d5092009-10-15 20:48:48 +00001933/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001934bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1935 StringRef IDVal = DirectiveID.getIdentifier();
1936 if (IDVal == ".word")
1937 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001938 else if (IDVal == ".thumb")
1939 return ParseDirectiveThumb(DirectiveID.getLoc());
1940 else if (IDVal == ".thumb_func")
1941 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1942 else if (IDVal == ".code")
1943 return ParseDirectiveCode(DirectiveID.getLoc());
1944 else if (IDVal == ".syntax")
1945 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001946 return true;
1947}
1948
1949/// ParseDirectiveWord
1950/// ::= .word [ expression (, expression)* ]
1951bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1952 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1953 for (;;) {
1954 const MCExpr *Value;
1955 if (getParser().ParseExpression(Value))
1956 return true;
1957
Chris Lattneraaec2052010-01-19 19:46:13 +00001958 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001959
1960 if (getLexer().is(AsmToken::EndOfStatement))
1961 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001962
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001963 // FIXME: Improve diagnostic.
1964 if (getLexer().isNot(AsmToken::Comma))
1965 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001966 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001967 }
1968 }
1969
Sean Callananb9a25b72010-01-19 20:27:46 +00001970 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001971 return false;
1972}
1973
Kevin Enderby515d5092009-10-15 20:48:48 +00001974/// ParseDirectiveThumb
1975/// ::= .thumb
1976bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1977 if (getLexer().isNot(AsmToken::EndOfStatement))
1978 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001979 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001980
1981 // TODO: set thumb mode
1982 // TODO: tell the MC streamer the mode
1983 // getParser().getStreamer().Emit???();
1984 return false;
1985}
1986
1987/// ParseDirectiveThumbFunc
1988/// ::= .thumbfunc symbol_name
1989bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001990 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001991 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001992 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001993 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001994 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001995 if (getLexer().isNot(AsmToken::EndOfStatement))
1996 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001997 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001998
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001999 // Mark symbol as a thumb symbol.
2000 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2001 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002002 return false;
2003}
2004
2005/// ParseDirectiveSyntax
2006/// ::= .syntax unified | divided
2007bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002008 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002009 if (Tok.isNot(AsmToken::Identifier))
2010 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002011 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002012 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002013 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002014 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002015 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002016 else
2017 return Error(L, "unrecognized syntax mode in .syntax directive");
2018
2019 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002020 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002021 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002022
2023 // TODO tell the MC streamer the mode
2024 // getParser().getStreamer().Emit???();
2025 return false;
2026}
2027
2028/// ParseDirectiveCode
2029/// ::= .code 16 | 32
2030bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002031 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002032 if (Tok.isNot(AsmToken::Integer))
2033 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002034 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002035 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002036 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002037 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002038 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002039 else
2040 return Error(L, "invalid operand to .code directive");
2041
2042 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002043 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002044 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002045
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00002046 // FIXME: We need to be able switch subtargets at this point so that
2047 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
2048 // includes Feature_IsThumb or not to match the right instructions. This is
2049 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
2050 if (Val == 16){
2051 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
2052 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00002053 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00002054 }
2055 else{
2056 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
2057 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00002058 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00002059 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002060
Kevin Enderby515d5092009-10-15 20:48:48 +00002061 return false;
2062}
2063
Sean Callanan90b70972010-04-07 20:29:34 +00002064extern "C" void LLVMInitializeARMAsmLexer();
2065
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002066/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002067extern "C" void LLVMInitializeARMAsmParser() {
2068 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2069 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002070 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002071}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002072
Chris Lattner0692ee62010-09-06 19:11:01 +00002073#define GET_REGISTER_MATCHER
2074#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002075#include "ARMGenAsmMatcher.inc"