blob: 6783bc3da025f5fc24578a5e92a61aaebac3ce34 [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"
Rafael Espindola64695402011-05-16 16:17:21 +000018#include "llvm/MC/MCAsmInfo.h"
Jim Grosbach642fc9c2010-11-05 22:33:53 +000019#include "llvm/MC/MCContext.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000020#include "llvm/MC/MCStreamer.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000023#include "llvm/MC/MCSubtargetInfo.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000024#include "llvm/Target/TargetRegistry.h"
25#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000026#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000027#include "llvm/Support/raw_ostream.h"
Benjamin Kramer75ca4b92011-07-08 21:06:23 +000028#include "llvm/ADT/OwningPtr.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000029#include "llvm/ADT/SmallVector.h"
Owen Anderson0c9f2502011-01-13 22:50:36 +000030#include "llvm/ADT/StringExtras.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000031#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000032#include "llvm/ADT/Twine.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000033
34#define GET_SUBTARGETINFO_ENUM
35#include "ARMGenSubtargetInfo.inc"
36
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000037using namespace llvm;
38
Chris Lattner3a697562010-10-28 17:20:03 +000039namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000040
41class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000042
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000043class ARMAsmParser : public TargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000044 MCSubtargetInfo &STI;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000045 MCAsmParser &Parser;
46
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000047 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000048 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
49
50 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000051 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
52
Chris Lattnere5658fa2010-10-30 04:09:10 +000053 int TryParseRegister();
Roman Divackybf755322011-01-27 17:14:22 +000054 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Bill Wendling50d0f582010-11-18 23:43:05 +000055 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbach19906722011-07-13 18:49:30 +000056 int TryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
Bill Wendling50d0f582010-11-18 23:43:05 +000057 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +000058 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &,
59 ARMII::AddrMode AddrMode);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +000060 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
Evan Cheng75972122011-01-13 07:58:56 +000061 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000062 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
63 MCSymbolRefExpr::VariantKind Variant);
64
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000065
Kevin Enderby9c41fa82009-10-30 22:55:57 +000066 bool ParseMemoryOffsetReg(bool &Negative,
67 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +000068 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +000069 const MCExpr *&ShiftAmount,
70 const MCExpr *&Offset,
71 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000072 int &OffsetRegNum,
73 SMLoc &E);
Owen Anderson00828302011-03-18 22:50:18 +000074 bool ParseShift(enum ARM_AM::ShiftOpc &St,
75 const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000076 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000077 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000078 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000079 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000080 bool ParseDirectiveSyntax(SMLoc L);
81
Chris Lattner7036f8b2010-09-29 01:42:58 +000082 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000083 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000084 MCStreamer &Out);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000085 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
86 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000087
Evan Chengebdeeab2011-07-08 01:53:10 +000088 bool isThumb() const {
89 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000090 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000091 }
Evan Chengebdeeab2011-07-08 01:53:10 +000092 bool isThumbOne() const {
Evan Chengffc0e732011-07-09 05:47:46 +000093 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) == 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000094 }
Evan Cheng32869202011-07-08 22:36:29 +000095 void SwitchMode() {
Evan Chengffc0e732011-07-09 05:47:46 +000096 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(ARM::ModeThumb));
97 setAvailableFeatures(FB);
Evan Cheng32869202011-07-08 22:36:29 +000098 }
Evan Chengebdeeab2011-07-08 01:53:10 +000099
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000100 /// @name Auto-generated Match Functions
101 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000102
Chris Lattner0692ee62010-09-06 19:11:01 +0000103#define GET_ASSEMBLER_HEADER
104#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000105
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000106 /// }
107
Jim Grosbachf922c472011-02-12 01:34:40 +0000108 OperandMatchResultTy tryParseCoprocNumOperand(
109 SmallVectorImpl<MCParsedAsmOperand*>&);
110 OperandMatchResultTy tryParseCoprocRegOperand(
111 SmallVectorImpl<MCParsedAsmOperand*>&);
112 OperandMatchResultTy tryParseMemBarrierOptOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000113 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000114 OperandMatchResultTy tryParseProcIFlagsOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000115 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000116 OperandMatchResultTy tryParseMSRMaskOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +0000117 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000118 OperandMatchResultTy tryParseMemMode2Operand(
119 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000120 OperandMatchResultTy tryParseMemMode3Operand(
121 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000122
123 // Asm Match Converter Methods
124 bool CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
125 const SmallVectorImpl<MCParsedAsmOperand*> &);
126 bool CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
127 const SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000128 bool CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
129 const SmallVectorImpl<MCParsedAsmOperand*> &);
130 bool CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
131 const SmallVectorImpl<MCParsedAsmOperand*> &);
Jim Grosbachf922c472011-02-12 01:34:40 +0000132
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000133public:
Evan Chengffc0e732011-07-09 05:47:46 +0000134 ARMAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser)
135 : TargetAsmParser(), STI(_STI), Parser(_Parser) {
Evan Chengebdeeab2011-07-08 01:53:10 +0000136 MCAsmParserExtension::Initialize(_Parser);
Evan Cheng32869202011-07-08 22:36:29 +0000137
Evan Chengebdeeab2011-07-08 01:53:10 +0000138 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000139 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Evan Chengebdeeab2011-07-08 01:53:10 +0000140 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000141
Benjamin Kramer38e59892010-07-14 22:38:02 +0000142 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000143 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000144 virtual bool ParseDirective(AsmToken DirectiveID);
145};
Jim Grosbach16c74252010-10-29 14:46:02 +0000146} // end anonymous namespace
147
Chris Lattner3a697562010-10-28 17:20:03 +0000148namespace {
149
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000150/// ARMOperand - Instances of this class represent a parsed ARM machine
151/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000152class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000153 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000154 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000155 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000156 CoprocNum,
157 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000158 Immediate,
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000159 MemBarrierOpt,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000160 Memory,
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000161 MSRMask,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000162 ProcIFlags,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000163 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000164 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000165 DPRRegisterList,
166 SPRRegisterList,
Jim Grosbache8606dc2011-07-13 17:50:29 +0000167 ShiftedRegister,
Owen Anderson00828302011-03-18 22:50:18 +0000168 Shifter,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000169 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000170 } Kind;
171
Sean Callanan76264762010-04-02 22:27:05 +0000172 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000173 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000174
175 union {
176 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000177 ARMCC::CondCodes Val;
178 } CC;
179
180 struct {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000181 ARM_MB::MemBOpt Val;
182 } MBOpt;
183
184 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000185 unsigned Val;
186 } Cop;
187
188 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000189 ARM_PROC::IFlags Val;
190 } IFlags;
191
192 struct {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000193 unsigned Val;
194 } MMask;
195
196 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000197 const char *Data;
198 unsigned Length;
199 } Tok;
200
201 struct {
202 unsigned RegNum;
203 } Reg;
204
Bill Wendling8155e5b2010-11-06 22:19:43 +0000205 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000206 const MCExpr *Val;
207 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000208
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000209 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000210 struct {
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000211 ARMII::AddrMode AddrMode;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000212 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000213 union {
214 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
215 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
216 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000217 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Owen Anderson00828302011-03-18 22:50:18 +0000218 enum ARM_AM::ShiftOpc ShiftType; // used when OffsetRegShifted is true
Bill Wendling146018f2010-11-06 21:42:12 +0000219 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000220 unsigned Preindexed : 1;
221 unsigned Postindexed : 1;
222 unsigned OffsetIsReg : 1;
223 unsigned Negative : 1; // only used when OffsetIsReg is true
224 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000225 } Mem;
Owen Anderson00828302011-03-18 22:50:18 +0000226
227 struct {
228 ARM_AM::ShiftOpc ShiftTy;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000229 unsigned Imm;
Owen Anderson00828302011-03-18 22:50:18 +0000230 } Shift;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000231 struct {
232 ARM_AM::ShiftOpc ShiftTy;
233 unsigned SrcReg;
234 unsigned ShiftReg;
235 unsigned ShiftImm;
236 } ShiftedReg;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000237 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000238
Bill Wendling146018f2010-11-06 21:42:12 +0000239 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
240public:
Sean Callanan76264762010-04-02 22:27:05 +0000241 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
242 Kind = o.Kind;
243 StartLoc = o.StartLoc;
244 EndLoc = o.EndLoc;
245 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000246 case CondCode:
247 CC = o.CC;
248 break;
Sean Callanan76264762010-04-02 22:27:05 +0000249 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000250 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000251 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000252 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000253 case Register:
254 Reg = o.Reg;
255 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000256 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000257 case DPRRegisterList:
258 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000259 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000260 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000261 case CoprocNum:
262 case CoprocReg:
263 Cop = o.Cop;
264 break;
Sean Callanan76264762010-04-02 22:27:05 +0000265 case Immediate:
266 Imm = o.Imm;
267 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000268 case MemBarrierOpt:
269 MBOpt = o.MBOpt;
270 break;
Sean Callanan76264762010-04-02 22:27:05 +0000271 case Memory:
272 Mem = o.Mem;
273 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000274 case MSRMask:
275 MMask = o.MMask;
276 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000277 case ProcIFlags:
278 IFlags = o.IFlags;
Owen Anderson00828302011-03-18 22:50:18 +0000279 break;
280 case Shifter:
281 Shift = o.Shift;
282 break;
Jim Grosbache8606dc2011-07-13 17:50:29 +0000283 case ShiftedRegister:
284 ShiftedReg = o.ShiftedReg;
285 break;
Sean Callanan76264762010-04-02 22:27:05 +0000286 }
287 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000288
Sean Callanan76264762010-04-02 22:27:05 +0000289 /// getStartLoc - Get the location of the first token of this operand.
290 SMLoc getStartLoc() const { return StartLoc; }
291 /// getEndLoc - Get the location of the last token of this operand.
292 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000293
Daniel Dunbar8462b302010-08-11 06:36:53 +0000294 ARMCC::CondCodes getCondCode() const {
295 assert(Kind == CondCode && "Invalid access!");
296 return CC.Val;
297 }
298
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000299 unsigned getCoproc() const {
300 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
301 return Cop.Val;
302 }
303
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000304 StringRef getToken() const {
305 assert(Kind == Token && "Invalid access!");
306 return StringRef(Tok.Data, Tok.Length);
307 }
308
309 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000310 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000311 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000312 }
313
Bill Wendling5fa22a12010-11-09 23:28:44 +0000314 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000315 assert((Kind == RegisterList || Kind == DPRRegisterList ||
316 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000317 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000318 }
319
Kevin Enderbycfe07242009-10-13 22:19:02 +0000320 const MCExpr *getImm() const {
321 assert(Kind == Immediate && "Invalid access!");
322 return Imm.Val;
323 }
324
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000325 ARM_MB::MemBOpt getMemBarrierOpt() const {
326 assert(Kind == MemBarrierOpt && "Invalid access!");
327 return MBOpt.Val;
328 }
329
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000330 ARM_PROC::IFlags getProcIFlags() const {
331 assert(Kind == ProcIFlags && "Invalid access!");
332 return IFlags.Val;
333 }
334
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000335 unsigned getMSRMask() const {
336 assert(Kind == MSRMask && "Invalid access!");
337 return MMask.Val;
338 }
339
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000340 /// @name Memory Operand Accessors
341 /// @{
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000342 ARMII::AddrMode getMemAddrMode() const {
343 return Mem.AddrMode;
344 }
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000345 unsigned getMemBaseRegNum() const {
346 return Mem.BaseRegNum;
347 }
348 unsigned getMemOffsetRegNum() const {
349 assert(Mem.OffsetIsReg && "Invalid access!");
350 return Mem.Offset.RegNum;
351 }
352 const MCExpr *getMemOffset() const {
353 assert(!Mem.OffsetIsReg && "Invalid access!");
354 return Mem.Offset.Value;
355 }
356 unsigned getMemOffsetRegShifted() const {
357 assert(Mem.OffsetIsReg && "Invalid access!");
358 return Mem.OffsetRegShifted;
359 }
360 const MCExpr *getMemShiftAmount() const {
361 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
362 return Mem.ShiftAmount;
363 }
Owen Anderson00828302011-03-18 22:50:18 +0000364 enum ARM_AM::ShiftOpc getMemShiftType() const {
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000365 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
366 return Mem.ShiftType;
367 }
368 bool getMemPreindexed() const { return Mem.Preindexed; }
369 bool getMemPostindexed() const { return Mem.Postindexed; }
370 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
371 bool getMemNegative() const { return Mem.Negative; }
372 bool getMemWriteback() const { return Mem.Writeback; }
373
374 /// @}
375
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000376 bool isCoprocNum() const { return Kind == CoprocNum; }
377 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000378 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000379 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000380 bool isImm() const { return Kind == Immediate; }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000381 bool isImm0_255() const {
382 if (Kind != Immediate)
383 return false;
384 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
385 if (!CE) return false;
386 int64_t Value = CE->getValue();
387 return Value >= 0 && Value < 256;
388 }
Jim Grosbach83ab0702011-07-13 22:01:08 +0000389 bool isImm0_7() const {
390 if (Kind != Immediate)
391 return false;
392 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
393 if (!CE) return false;
394 int64_t Value = CE->getValue();
395 return Value >= 0 && Value < 8;
396 }
397 bool isImm0_15() const {
398 if (Kind != Immediate)
399 return false;
400 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
401 if (!CE) return false;
402 int64_t Value = CE->getValue();
403 return Value >= 0 && Value < 16;
404 }
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000405 bool isImm0_65535() const {
406 if (Kind != Immediate)
407 return false;
408 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
409 if (!CE) return false;
410 int64_t Value = CE->getValue();
411 return Value >= 0 && Value < 65536;
412 }
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000413 bool isT2SOImm() const {
414 if (Kind != Immediate)
415 return false;
416 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
417 if (!CE) return false;
418 int64_t Value = CE->getValue();
419 return ARM_AM::getT2SOImmVal(Value) != -1;
420 }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000421 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000422 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000423 bool isDPRRegList() const { return Kind == DPRRegisterList; }
424 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000425 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000426 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000427 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000428 bool isShifter() const { return Kind == Shifter; }
Jim Grosbache8606dc2011-07-13 17:50:29 +0000429 bool isShiftedReg() const { return Kind == ShiftedRegister; }
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000430 bool isMemMode2() const {
431 if (getMemAddrMode() != ARMII::AddrMode2)
432 return false;
433
434 if (getMemOffsetIsReg())
435 return true;
436
437 if (getMemNegative() &&
438 !(getMemPostindexed() || getMemPreindexed()))
439 return false;
440
441 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
442 if (!CE) return false;
443 int64_t Value = CE->getValue();
444
445 // The offset must be in the range 0-4095 (imm12).
446 if (Value > 4095 || Value < -4095)
447 return false;
448
449 return true;
450 }
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000451 bool isMemMode3() const {
452 if (getMemAddrMode() != ARMII::AddrMode3)
453 return false;
454
455 if (getMemOffsetIsReg()) {
456 if (getMemOffsetRegShifted())
457 return false; // No shift with offset reg allowed
458 return true;
459 }
460
461 if (getMemNegative() &&
462 !(getMemPostindexed() || getMemPreindexed()))
463 return false;
464
465 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
466 if (!CE) return false;
467 int64_t Value = CE->getValue();
468
469 // The offset must be in the range 0-255 (imm8).
470 if (Value > 255 || Value < -255)
471 return false;
472
473 return true;
474 }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000475 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000476 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
477 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000478 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000479
Daniel Dunbar4b462672011-01-18 05:55:27 +0000480 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000481 if (!CE) return false;
482
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000483 // The offset must be a multiple of 4 in the range 0-1020.
484 int64_t Value = CE->getValue();
485 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
486 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000487 bool isMemMode7() const {
488 if (!isMemory() ||
489 getMemPreindexed() ||
490 getMemPostindexed() ||
491 getMemOffsetIsReg() ||
492 getMemNegative() ||
493 getMemWriteback())
494 return false;
495
496 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
497 if (!CE) return false;
498
499 if (CE->getValue())
500 return false;
501
502 return true;
503 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000504 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000505 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000506 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000507 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000508 }
509 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000510 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000511 return false;
512
Daniel Dunbar4b462672011-01-18 05:55:27 +0000513 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000514 if (!CE) return false;
515
516 // The offset must be a multiple of 4 in the range 0-124.
517 uint64_t Value = CE->getValue();
518 return ((Value & 0x3) == 0 && Value <= 124);
519 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000520 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000521 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000522
523 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000524 // Add as immediates when possible. Null MCExpr = 0.
525 if (Expr == 0)
526 Inst.addOperand(MCOperand::CreateImm(0));
527 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000528 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
529 else
530 Inst.addOperand(MCOperand::CreateExpr(Expr));
531 }
532
Daniel Dunbar8462b302010-08-11 06:36:53 +0000533 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000534 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000535 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000536 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
537 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000538 }
539
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000540 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
541 assert(N == 1 && "Invalid number of operands!");
542 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
543 }
544
545 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
546 assert(N == 1 && "Invalid number of operands!");
547 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
548 }
549
Jim Grosbachd67641b2010-12-06 18:21:12 +0000550 void addCCOutOperands(MCInst &Inst, unsigned N) const {
551 assert(N == 1 && "Invalid number of operands!");
552 Inst.addOperand(MCOperand::CreateReg(getReg()));
553 }
554
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000555 void addRegOperands(MCInst &Inst, unsigned N) const {
556 assert(N == 1 && "Invalid number of operands!");
557 Inst.addOperand(MCOperand::CreateReg(getReg()));
558 }
559
Jim Grosbache8606dc2011-07-13 17:50:29 +0000560 void addShiftedRegOperands(MCInst &Inst, unsigned N) const {
561 assert(N == 3 && "Invalid number of operands!");
562 assert(isShiftedReg() && "addShiftedRegOperands() on non ShiftedReg!");
563 assert((ShiftedReg.ShiftReg == 0 ||
564 ARM_AM::getSORegOffset(ShiftedReg.ShiftImm) == 0) &&
565 "Invalid shifted register operand!");
566 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.SrcReg));
567 Inst.addOperand(MCOperand::CreateReg(ShiftedReg.ShiftReg));
568 Inst.addOperand(MCOperand::CreateImm(
569 ARM_AM::getSORegOpc(ShiftedReg.ShiftTy, ShiftedReg.ShiftImm)));
570 }
571
Owen Anderson00828302011-03-18 22:50:18 +0000572 void addShifterOperands(MCInst &Inst, unsigned N) const {
573 assert(N == 1 && "Invalid number of operands!");
574 Inst.addOperand(MCOperand::CreateImm(
575 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
576 }
577
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000578 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000579 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000580 const SmallVectorImpl<unsigned> &RegList = getRegList();
581 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000582 I = RegList.begin(), E = RegList.end(); I != E; ++I)
583 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000584 }
585
Bill Wendling0f630752010-11-17 04:32:08 +0000586 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
587 addRegListOperands(Inst, N);
588 }
589
590 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
591 addRegListOperands(Inst, N);
592 }
593
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000594 void addImmOperands(MCInst &Inst, unsigned N) const {
595 assert(N == 1 && "Invalid number of operands!");
596 addExpr(Inst, getImm());
597 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000598
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000599 void addImm0_255Operands(MCInst &Inst, unsigned N) const {
600 assert(N == 1 && "Invalid number of operands!");
601 addExpr(Inst, getImm());
602 }
603
Jim Grosbach83ab0702011-07-13 22:01:08 +0000604 void addImm0_7Operands(MCInst &Inst, unsigned N) const {
605 assert(N == 1 && "Invalid number of operands!");
606 addExpr(Inst, getImm());
607 }
608
609 void addImm0_15Operands(MCInst &Inst, unsigned N) const {
610 assert(N == 1 && "Invalid number of operands!");
611 addExpr(Inst, getImm());
612 }
613
Jim Grosbachfff76ee2011-07-13 20:10:10 +0000614 void addImm0_65535Operands(MCInst &Inst, unsigned N) const {
615 assert(N == 1 && "Invalid number of operands!");
616 addExpr(Inst, getImm());
617 }
618
Jim Grosbach6b8f1e32011-06-27 23:54:06 +0000619 void addT2SOImmOperands(MCInst &Inst, unsigned N) const {
620 assert(N == 1 && "Invalid number of operands!");
621 addExpr(Inst, getImm());
622 }
623
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000624 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
625 assert(N == 1 && "Invalid number of operands!");
626 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
627 }
628
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000629 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
630 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
631 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
632
633 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000634 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000635 assert((CE || CE->getValue() == 0) &&
636 "No offset operand support in mode 7");
637 }
638
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000639 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
640 assert(isMemMode2() && "Invalid mode or number of operands!");
641 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
642 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
643
644 if (getMemOffsetIsReg()) {
645 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
646
647 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
648 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
649 int64_t ShiftAmount = 0;
650
651 if (getMemOffsetRegShifted()) {
652 ShOpc = getMemShiftType();
653 const MCConstantExpr *CE =
654 dyn_cast<MCConstantExpr>(getMemShiftAmount());
655 ShiftAmount = CE->getValue();
656 }
657
658 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
659 ShOpc, IdxMode)));
660 return;
661 }
662
663 // Create a operand placeholder to always yield the same number of operands.
664 Inst.addOperand(MCOperand::CreateReg(0));
665
666 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
667 // the difference?
668 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
669 assert(CE && "Non-constant mode 2 offset operand!");
670 int64_t Offset = CE->getValue();
671
672 if (Offset >= 0)
673 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
674 Offset, ARM_AM::no_shift, IdxMode)));
675 else
676 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
677 -Offset, ARM_AM::no_shift, IdxMode)));
678 }
679
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +0000680 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
681 assert(isMemMode3() && "Invalid mode or number of operands!");
682 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
683 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
684
685 if (getMemOffsetIsReg()) {
686 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
687
688 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
689 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
690 IdxMode)));
691 return;
692 }
693
694 // Create a operand placeholder to always yield the same number of operands.
695 Inst.addOperand(MCOperand::CreateReg(0));
696
697 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
698 // the difference?
699 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
700 assert(CE && "Non-constant mode 3 offset operand!");
701 int64_t Offset = CE->getValue();
702
703 if (Offset >= 0)
704 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
705 Offset, IdxMode)));
706 else
707 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
708 -Offset, IdxMode)));
709 }
710
Chris Lattner14b93852010-10-29 00:27:31 +0000711 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
712 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000713
Daniel Dunbar4b462672011-01-18 05:55:27 +0000714 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
715 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000716
Jim Grosbach80eb2332010-10-29 17:41:25 +0000717 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
718 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000719 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000720 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000721
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000722 // The MCInst offset operand doesn't include the low two bits (like
723 // the instruction encoding).
724 int64_t Offset = CE->getValue() / 4;
725 if (Offset >= 0)
726 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
727 Offset)));
728 else
729 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
730 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000731 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000732
Bill Wendlingf4caf692010-12-14 03:36:38 +0000733 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
734 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000735 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
736 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000737 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000738
Bill Wendlingf4caf692010-12-14 03:36:38 +0000739 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
740 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000741 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
742 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000743 assert(CE && "Non-constant mode offset operand!");
744 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000745 }
746
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000747 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
748 assert(N == 1 && "Invalid number of operands!");
749 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
750 }
751
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000752 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
753 assert(N == 1 && "Invalid number of operands!");
754 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
755 }
756
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000757 virtual void print(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000758
Chris Lattner3a697562010-10-28 17:20:03 +0000759 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
760 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000761 Op->CC.Val = CC;
762 Op->StartLoc = S;
763 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000764 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000765 }
766
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000767 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
768 ARMOperand *Op = new ARMOperand(CoprocNum);
769 Op->Cop.Val = CopVal;
770 Op->StartLoc = S;
771 Op->EndLoc = S;
772 return Op;
773 }
774
775 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
776 ARMOperand *Op = new ARMOperand(CoprocReg);
777 Op->Cop.Val = CopVal;
778 Op->StartLoc = S;
779 Op->EndLoc = S;
780 return Op;
781 }
782
Jim Grosbachd67641b2010-12-06 18:21:12 +0000783 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
784 ARMOperand *Op = new ARMOperand(CCOut);
785 Op->Reg.RegNum = RegNum;
786 Op->StartLoc = S;
787 Op->EndLoc = S;
788 return Op;
789 }
790
Chris Lattner3a697562010-10-28 17:20:03 +0000791 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
792 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000793 Op->Tok.Data = Str.data();
794 Op->Tok.Length = Str.size();
795 Op->StartLoc = S;
796 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000797 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000798 }
799
Bill Wendling50d0f582010-11-18 23:43:05 +0000800 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000801 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000802 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000803 Op->StartLoc = S;
804 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000805 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000806 }
807
Jim Grosbache8606dc2011-07-13 17:50:29 +0000808 static ARMOperand *CreateShiftedRegister(ARM_AM::ShiftOpc ShTy,
809 unsigned SrcReg,
810 unsigned ShiftReg,
811 unsigned ShiftImm,
812 SMLoc S, SMLoc E) {
813 ARMOperand *Op = new ARMOperand(ShiftedRegister);
814 Op->ShiftedReg.ShiftTy = ShTy;
815 Op->ShiftedReg.SrcReg = SrcReg;
816 Op->ShiftedReg.ShiftReg = ShiftReg;
817 Op->ShiftedReg.ShiftImm = ShiftImm;
818 Op->StartLoc = S;
819 Op->EndLoc = E;
820 return Op;
821 }
822
Owen Anderson00828302011-03-18 22:50:18 +0000823 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
824 SMLoc S, SMLoc E) {
825 ARMOperand *Op = new ARMOperand(Shifter);
826 Op->Shift.ShiftTy = ShTy;
827 Op->StartLoc = S;
828 Op->EndLoc = E;
829 return Op;
830 }
831
Bill Wendling7729e062010-11-09 22:44:22 +0000832 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000833 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000834 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000835 KindTy Kind = RegisterList;
836
837 if (ARM::DPRRegClass.contains(Regs.front().first))
838 Kind = DPRRegisterList;
839 else if (ARM::SPRRegClass.contains(Regs.front().first))
840 Kind = SPRRegisterList;
841
842 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000843 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000844 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000845 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000846 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000847 Op->StartLoc = StartLoc;
848 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000849 return Op;
850 }
851
Chris Lattner3a697562010-10-28 17:20:03 +0000852 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
853 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000854 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000855 Op->StartLoc = S;
856 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000857 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000858 }
859
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000860 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
861 bool OffsetIsReg, const MCExpr *Offset,
862 int OffsetRegNum, bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000863 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000864 const MCExpr *ShiftAmount, bool Preindexed,
865 bool Postindexed, bool Negative, bool Writeback,
866 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000867 assert((OffsetRegNum == -1 || OffsetIsReg) &&
868 "OffsetRegNum must imply OffsetIsReg!");
869 assert((!OffsetRegShifted || OffsetIsReg) &&
870 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000871 assert((Offset || OffsetIsReg) &&
872 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000873 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
874 "Cannot have shift amount without shifted register offset!");
875 assert((!Offset || !OffsetIsReg) &&
876 "Cannot have expression offset and register offset!");
877
Chris Lattner3a697562010-10-28 17:20:03 +0000878 ARMOperand *Op = new ARMOperand(Memory);
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000879 Op->Mem.AddrMode = AddrMode;
Sean Callanan76264762010-04-02 22:27:05 +0000880 Op->Mem.BaseRegNum = BaseRegNum;
881 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000882 if (OffsetIsReg)
883 Op->Mem.Offset.RegNum = OffsetRegNum;
884 else
885 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000886 Op->Mem.OffsetRegShifted = OffsetRegShifted;
887 Op->Mem.ShiftType = ShiftType;
888 Op->Mem.ShiftAmount = ShiftAmount;
889 Op->Mem.Preindexed = Preindexed;
890 Op->Mem.Postindexed = Postindexed;
891 Op->Mem.Negative = Negative;
892 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000893
Sean Callanan76264762010-04-02 22:27:05 +0000894 Op->StartLoc = S;
895 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000896 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000897 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000898
899 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
900 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
901 Op->MBOpt.Val = Opt;
902 Op->StartLoc = S;
903 Op->EndLoc = S;
904 return Op;
905 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000906
907 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
908 ARMOperand *Op = new ARMOperand(ProcIFlags);
909 Op->IFlags.Val = IFlags;
910 Op->StartLoc = S;
911 Op->EndLoc = S;
912 return Op;
913 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000914
915 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
916 ARMOperand *Op = new ARMOperand(MSRMask);
917 Op->MMask.Val = MMask;
918 Op->StartLoc = S;
919 Op->EndLoc = S;
920 return Op;
921 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000922};
923
924} // end anonymous namespace.
925
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000926void ARMOperand::print(raw_ostream &OS) const {
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000927 switch (Kind) {
928 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000929 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000930 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000931 case CCOut:
932 OS << "<ccout " << getReg() << ">";
933 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000934 case CoprocNum:
935 OS << "<coprocessor number: " << getCoproc() << ">";
936 break;
937 case CoprocReg:
938 OS << "<coprocessor register: " << getCoproc() << ">";
939 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000940 case MSRMask:
941 OS << "<mask: " << getMSRMask() << ">";
942 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000943 case Immediate:
944 getImm()->print(OS);
945 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000946 case MemBarrierOpt:
947 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
948 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000949 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000950 OS << "<memory "
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +0000951 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
952 << " base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000953 if (getMemOffsetIsReg()) {
954 OS << " offset:<register " << getMemOffsetRegNum();
955 if (getMemOffsetRegShifted()) {
956 OS << " offset-shift-type:" << getMemShiftType();
957 OS << " offset-shift-amount:" << *getMemShiftAmount();
958 }
959 } else {
960 OS << " offset:" << *getMemOffset();
961 }
962 if (getMemOffsetIsReg())
963 OS << " (offset-is-reg)";
964 if (getMemPreindexed())
965 OS << " (pre-indexed)";
966 if (getMemPostindexed())
967 OS << " (post-indexed)";
968 if (getMemNegative())
969 OS << " (negative)";
970 if (getMemWriteback())
971 OS << " (writeback)";
972 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000973 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000974 case ProcIFlags: {
975 OS << "<ARM_PROC::";
976 unsigned IFlags = getProcIFlags();
977 for (int i=2; i >= 0; --i)
978 if (IFlags & (1 << i))
979 OS << ARM_PROC::IFlagsToString(1 << i);
980 OS << ">";
981 break;
982 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000983 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000984 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000985 break;
Owen Anderson00828302011-03-18 22:50:18 +0000986 case Shifter:
Jim Grosbache8606dc2011-07-13 17:50:29 +0000987 OS << "<shifter " << ARM_AM::getShiftOpcStr(Shift.ShiftTy) << ">";
988 break;
989 case ShiftedRegister:
990 OS << "<so_reg"
991 << ShiftedReg.SrcReg
992 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(ShiftedReg.ShiftImm))
993 << ", " << ShiftedReg.ShiftReg << ", "
994 << ARM_AM::getSORegOffset(ShiftedReg.ShiftImm)
995 << ">";
Owen Anderson00828302011-03-18 22:50:18 +0000996 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000997 case RegisterList:
998 case DPRRegisterList:
999 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +00001000 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001001
Bill Wendling5fa22a12010-11-09 23:28:44 +00001002 const SmallVectorImpl<unsigned> &RegList = getRegList();
1003 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +00001004 I = RegList.begin(), E = RegList.end(); I != E; ) {
1005 OS << *I;
1006 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +00001007 }
1008
1009 OS << ">";
1010 break;
1011 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +00001012 case Token:
1013 OS << "'" << getToken() << "'";
1014 break;
1015 }
1016}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001017
1018/// @name Auto-generated Match Functions
1019/// {
1020
1021static unsigned MatchRegisterName(StringRef Name);
1022
1023/// }
1024
Bob Wilson69df7232011-02-03 21:46:10 +00001025bool ARMAsmParser::ParseRegister(unsigned &RegNo,
1026 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +00001027 RegNo = TryParseRegister();
1028
1029 return (RegNo == (unsigned)-1);
1030}
1031
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001032/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +00001033/// and if it is a register name the token is eaten and the register number is
1034/// returned. Otherwise return -1.
1035///
1036int ARMAsmParser::TryParseRegister() {
1037 const AsmToken &Tok = Parser.getTok();
1038 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +00001039
Chris Lattnere5658fa2010-10-30 04:09:10 +00001040 // FIXME: Validate register for the current architecture; we have to do
1041 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +00001042 std::string upperCase = Tok.getString().str();
1043 std::string lowerCase = LowercaseString(upperCase);
1044 unsigned RegNum = MatchRegisterName(lowerCase);
1045 if (!RegNum) {
1046 RegNum = StringSwitch<unsigned>(lowerCase)
1047 .Case("r13", ARM::SP)
1048 .Case("r14", ARM::LR)
1049 .Case("r15", ARM::PC)
1050 .Case("ip", ARM::R12)
1051 .Default(0);
1052 }
1053 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +00001054
Chris Lattnere5658fa2010-10-30 04:09:10 +00001055 Parser.Lex(); // Eat identifier token.
1056 return RegNum;
1057}
Jim Grosbachd4462a52010-11-01 16:44:21 +00001058
Jim Grosbach19906722011-07-13 18:49:30 +00001059// Try to parse a shifter (e.g., "lsl <amt>"). On success, return 0.
1060// If a recoverable error occurs, return 1. If an irrecoverable error
1061// occurs, return -1. An irrecoverable error is one where tokens have been
1062// consumed in the process of trying to parse the shifter (i.e., when it is
1063// indeed a shifter operand, but malformed).
1064int ARMAsmParser::TryParseShiftRegister(
Owen Anderson00828302011-03-18 22:50:18 +00001065 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1066 SMLoc S = Parser.getTok().getLoc();
1067 const AsmToken &Tok = Parser.getTok();
1068 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1069
1070 std::string upperCase = Tok.getString().str();
1071 std::string lowerCase = LowercaseString(upperCase);
1072 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
1073 .Case("lsl", ARM_AM::lsl)
1074 .Case("lsr", ARM_AM::lsr)
1075 .Case("asr", ARM_AM::asr)
1076 .Case("ror", ARM_AM::ror)
1077 .Case("rrx", ARM_AM::rrx)
1078 .Default(ARM_AM::no_shift);
1079
1080 if (ShiftTy == ARM_AM::no_shift)
Jim Grosbach19906722011-07-13 18:49:30 +00001081 return 1;
Owen Anderson00828302011-03-18 22:50:18 +00001082
Jim Grosbache8606dc2011-07-13 17:50:29 +00001083 Parser.Lex(); // Eat the operator.
Owen Anderson00828302011-03-18 22:50:18 +00001084
Jim Grosbache8606dc2011-07-13 17:50:29 +00001085 // The source register for the shift has already been added to the
1086 // operand list, so we need to pop it off and combine it into the shifted
1087 // register operand instead.
Benjamin Kramereac07962011-07-14 18:41:22 +00001088 OwningPtr<ARMOperand> PrevOp((ARMOperand*)Operands.pop_back_val());
Jim Grosbache8606dc2011-07-13 17:50:29 +00001089 if (!PrevOp->isReg())
1090 return Error(PrevOp->getStartLoc(), "shift must be of a register");
1091 int SrcReg = PrevOp->getReg();
1092 int64_t Imm = 0;
1093 int ShiftReg = 0;
1094 if (ShiftTy == ARM_AM::rrx) {
1095 // RRX Doesn't have an explicit shift amount. The encoder expects
1096 // the shift register to be the same as the source register. Seems odd,
1097 // but OK.
1098 ShiftReg = SrcReg;
1099 } else {
1100 // Figure out if this is shifted by a constant or a register (for non-RRX).
1101 if (Parser.getTok().is(AsmToken::Hash)) {
1102 Parser.Lex(); // Eat hash.
1103 SMLoc ImmLoc = Parser.getTok().getLoc();
1104 const MCExpr *ShiftExpr = 0;
Jim Grosbach19906722011-07-13 18:49:30 +00001105 if (getParser().ParseExpression(ShiftExpr)) {
1106 Error(ImmLoc, "invalid immediate shift value");
1107 return -1;
1108 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001109 // The expression must be evaluatable as an immediate.
1110 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(ShiftExpr);
Jim Grosbach19906722011-07-13 18:49:30 +00001111 if (!CE) {
1112 Error(ImmLoc, "invalid immediate shift value");
1113 return -1;
1114 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001115 // Range check the immediate.
1116 // lsl, ror: 0 <= imm <= 31
1117 // lsr, asr: 0 <= imm <= 32
1118 Imm = CE->getValue();
1119 if (Imm < 0 ||
1120 ((ShiftTy == ARM_AM::lsl || ShiftTy == ARM_AM::ror) && Imm > 31) ||
1121 ((ShiftTy == ARM_AM::lsr || ShiftTy == ARM_AM::asr) && Imm > 32)) {
Jim Grosbach19906722011-07-13 18:49:30 +00001122 Error(ImmLoc, "immediate shift value out of range");
1123 return -1;
Jim Grosbache8606dc2011-07-13 17:50:29 +00001124 }
1125 } else if (Parser.getTok().is(AsmToken::Identifier)) {
1126 ShiftReg = TryParseRegister();
1127 SMLoc L = Parser.getTok().getLoc();
Jim Grosbach19906722011-07-13 18:49:30 +00001128 if (ShiftReg == -1) {
1129 Error (L, "expected immediate or register in shift operand");
1130 return -1;
1131 }
1132 } else {
1133 Error (Parser.getTok().getLoc(),
Jim Grosbache8606dc2011-07-13 17:50:29 +00001134 "expected immediate or register in shift operand");
Jim Grosbach19906722011-07-13 18:49:30 +00001135 return -1;
1136 }
Jim Grosbache8606dc2011-07-13 17:50:29 +00001137 }
1138
Jim Grosbache8606dc2011-07-13 17:50:29 +00001139 Operands.push_back(ARMOperand::CreateShiftedRegister(ShiftTy, SrcReg,
1140 ShiftReg, Imm,
Owen Anderson00828302011-03-18 22:50:18 +00001141 S, Parser.getTok().getLoc()));
1142
Jim Grosbach19906722011-07-13 18:49:30 +00001143 return 0;
Owen Anderson00828302011-03-18 22:50:18 +00001144}
1145
1146
Bill Wendling50d0f582010-11-18 23:43:05 +00001147/// Try to parse a register name. The token must be an Identifier when called.
1148/// If it's a register, an AsmOperand is created. Another AsmOperand is created
1149/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +00001150///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001151/// TODO this is likely to change to allow different register types and or to
1152/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +00001153bool ARMAsmParser::
1154TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001155 SMLoc S = Parser.getTok().getLoc();
1156 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +00001157 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +00001158 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +00001159
Bill Wendling50d0f582010-11-18 23:43:05 +00001160 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001161
Chris Lattnere5658fa2010-10-30 04:09:10 +00001162 const AsmToken &ExclaimTok = Parser.getTok();
1163 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001164 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
1165 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +00001166 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +00001167 }
1168
Bill Wendling50d0f582010-11-18 23:43:05 +00001169 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001170}
1171
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001172/// MatchCoprocessorOperandName - Try to parse an coprocessor related
1173/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
1174/// "c5", ...
1175static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001176 // Use the same layout as the tablegen'erated register name matcher. Ugly,
1177 // but efficient.
1178 switch (Name.size()) {
1179 default: break;
1180 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001181 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001182 return -1;
1183 switch (Name[1]) {
1184 default: return -1;
1185 case '0': return 0;
1186 case '1': return 1;
1187 case '2': return 2;
1188 case '3': return 3;
1189 case '4': return 4;
1190 case '5': return 5;
1191 case '6': return 6;
1192 case '7': return 7;
1193 case '8': return 8;
1194 case '9': return 9;
1195 }
1196 break;
1197 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001198 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001199 return -1;
1200 switch (Name[2]) {
1201 default: return -1;
1202 case '0': return 10;
1203 case '1': return 11;
1204 case '2': return 12;
1205 case '3': return 13;
1206 case '4': return 14;
1207 case '5': return 15;
1208 }
1209 break;
1210 }
1211
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001212 return -1;
1213}
1214
Jim Grosbachf922c472011-02-12 01:34:40 +00001215/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001216/// token must be an Identifier when called, and if it is a coprocessor
1217/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001218ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1219tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001220 SMLoc S = Parser.getTok().getLoc();
1221 const AsmToken &Tok = Parser.getTok();
1222 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1223
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001224 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001225 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001226 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001227
1228 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001229 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001230 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001231}
1232
Jim Grosbachf922c472011-02-12 01:34:40 +00001233/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001234/// token must be an Identifier when called, and if it is a coprocessor
1235/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +00001236ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1237tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001238 SMLoc S = Parser.getTok().getLoc();
1239 const AsmToken &Tok = Parser.getTok();
1240 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1241
1242 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1243 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +00001244 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001245
1246 Parser.Lex(); // Eat identifier token.
1247 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001248 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001249}
1250
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001251/// Parse a register list, return it if successful else return null. The first
1252/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001253bool ARMAsmParser::
1254ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +00001255 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001256 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +00001257 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001258
Bill Wendling7729e062010-11-09 22:44:22 +00001259 // Read the rest of the registers in the list.
1260 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +00001261 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001262
Bill Wendling7729e062010-11-09 22:44:22 +00001263 do {
Bill Wendlinge7176102010-11-06 22:36:58 +00001264 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +00001265 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001266
Sean Callanan18b83232010-01-19 21:44:56 +00001267 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001268 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001269 if (RegTok.isNot(AsmToken::Identifier)) {
1270 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001271 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001272 }
Bill Wendlinge7176102010-11-06 22:36:58 +00001273
Bill Wendling1d6a2652010-11-06 10:40:24 +00001274 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001275 if (RegNum == -1) {
1276 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001277 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001278 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001279
Bill Wendlinge7176102010-11-06 22:36:58 +00001280 if (IsRange) {
1281 int Reg = PrevRegNum;
1282 do {
1283 ++Reg;
1284 Registers.push_back(std::make_pair(Reg, RegLoc));
1285 } while (Reg != RegNum);
1286 } else {
1287 Registers.push_back(std::make_pair(RegNum, RegLoc));
1288 }
1289
1290 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +00001291 } while (Parser.getTok().is(AsmToken::Comma) ||
1292 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +00001293
1294 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +00001295 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001296 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1297 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001298 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +00001299 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001300
Bill Wendlinge7176102010-11-06 22:36:58 +00001301 SMLoc E = RCurlyTok.getLoc();
1302 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +00001303
Bill Wendlinge7176102010-11-06 22:36:58 +00001304 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +00001305 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +00001306 RI = Registers.begin(), RE = Registers.end();
1307
Bill Wendling7caebff2011-01-12 21:20:59 +00001308 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001309 bool EmittedWarning = false;
1310
Bill Wendling7caebff2011-01-12 21:20:59 +00001311 DenseMap<unsigned, bool> RegMap;
1312 RegMap[HighRegNum] = true;
1313
Bill Wendlinge7176102010-11-06 22:36:58 +00001314 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +00001315 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +00001316 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +00001317
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001318 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +00001319 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001320 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001321 }
1322
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001323 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001324 Warning(RegInfo.second,
1325 "register not in ascending order in register list");
1326
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001327 RegMap[Reg] = true;
1328 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001329 }
1330
Bill Wendling50d0f582010-11-18 23:43:05 +00001331 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1332 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001333}
1334
Jim Grosbachf922c472011-02-12 01:34:40 +00001335/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1336ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1337tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001338 SMLoc S = Parser.getTok().getLoc();
1339 const AsmToken &Tok = Parser.getTok();
1340 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1341 StringRef OptStr = Tok.getString();
1342
1343 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1344 .Case("sy", ARM_MB::SY)
1345 .Case("st", ARM_MB::ST)
Jim Grosbach032434d2011-07-13 23:40:38 +00001346 .Case("sh", ARM_MB::ISH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001347 .Case("ish", ARM_MB::ISH)
Jim Grosbach032434d2011-07-13 23:40:38 +00001348 .Case("shst", ARM_MB::ISHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001349 .Case("ishst", ARM_MB::ISHST)
1350 .Case("nsh", ARM_MB::NSH)
Jim Grosbach032434d2011-07-13 23:40:38 +00001351 .Case("un", ARM_MB::NSH)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001352 .Case("nshst", ARM_MB::NSHST)
Jim Grosbach032434d2011-07-13 23:40:38 +00001353 .Case("unst", ARM_MB::NSHST)
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001354 .Case("osh", ARM_MB::OSH)
1355 .Case("oshst", ARM_MB::OSHST)
1356 .Default(~0U);
1357
1358 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001359 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001360
1361 Parser.Lex(); // Eat identifier token.
1362 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001363 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001364}
1365
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001366/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001367ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1368tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1369 SMLoc S = Parser.getTok().getLoc();
1370 const AsmToken &Tok = Parser.getTok();
1371 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1372 StringRef IFlagsStr = Tok.getString();
1373
1374 unsigned IFlags = 0;
1375 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1376 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1377 .Case("a", ARM_PROC::A)
1378 .Case("i", ARM_PROC::I)
1379 .Case("f", ARM_PROC::F)
1380 .Default(~0U);
1381
1382 // If some specific iflag is already set, it means that some letter is
1383 // present more than once, this is not acceptable.
1384 if (Flag == ~0U || (IFlags & Flag))
1385 return MatchOperand_NoMatch;
1386
1387 IFlags |= Flag;
1388 }
1389
1390 Parser.Lex(); // Eat identifier token.
1391 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1392 return MatchOperand_Success;
1393}
1394
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001395/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1396ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1397tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1398 SMLoc S = Parser.getTok().getLoc();
1399 const AsmToken &Tok = Parser.getTok();
1400 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1401 StringRef Mask = Tok.getString();
1402
1403 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1404 size_t Start = 0, Next = Mask.find('_');
1405 StringRef Flags = "";
1406 StringRef SpecReg = Mask.slice(Start, Next);
1407 if (Next != StringRef::npos)
1408 Flags = Mask.slice(Next+1, Mask.size());
1409
1410 // FlagsVal contains the complete mask:
1411 // 3-0: Mask
1412 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1413 unsigned FlagsVal = 0;
1414
1415 if (SpecReg == "apsr") {
1416 FlagsVal = StringSwitch<unsigned>(Flags)
1417 .Case("nzcvq", 0x8) // same as CPSR_c
1418 .Case("g", 0x4) // same as CPSR_s
1419 .Case("nzcvqg", 0xc) // same as CPSR_fs
1420 .Default(~0U);
1421
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001422 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001423 if (!Flags.empty())
1424 return MatchOperand_NoMatch;
1425 else
1426 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001427 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001428 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00001429 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
1430 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001431 for (int i = 0, e = Flags.size(); i != e; ++i) {
1432 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1433 .Case("c", 1)
1434 .Case("x", 2)
1435 .Case("s", 4)
1436 .Case("f", 8)
1437 .Default(~0U);
1438
1439 // If some specific flag is already set, it means that some letter is
1440 // present more than once, this is not acceptable.
1441 if (FlagsVal == ~0U || (FlagsVal & Flag))
1442 return MatchOperand_NoMatch;
1443 FlagsVal |= Flag;
1444 }
1445 } else // No match for special register.
1446 return MatchOperand_NoMatch;
1447
1448 // Special register without flags are equivalent to "fc" flags.
1449 if (!FlagsVal)
1450 FlagsVal = 0x9;
1451
1452 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1453 if (SpecReg == "spsr")
1454 FlagsVal |= 16;
1455
1456 Parser.Lex(); // Eat identifier token.
1457 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1458 return MatchOperand_Success;
1459}
1460
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001461/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1462ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1463tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001464 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001465
1466 if (ParseMemory(Operands, ARMII::AddrMode2))
1467 return MatchOperand_NoMatch;
1468
1469 return MatchOperand_Success;
1470}
1471
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001472/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1473ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1474tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1475 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1476
1477 if (ParseMemory(Operands, ARMII::AddrMode3))
1478 return MatchOperand_NoMatch;
1479
1480 return MatchOperand_Success;
1481}
1482
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001483/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1484/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1485/// when they refer multiple MIOperands inside a single one.
1486bool ARMAsmParser::
1487CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1488 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1489 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1490
1491 // Create a writeback register dummy placeholder.
1492 Inst.addOperand(MCOperand::CreateImm(0));
1493
1494 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1495 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1496 return true;
1497}
1498
1499/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1500/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1501/// when they refer multiple MIOperands inside a single one.
1502bool ARMAsmParser::
1503CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1504 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1505 // Create a writeback register dummy placeholder.
1506 Inst.addOperand(MCOperand::CreateImm(0));
1507 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1508 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1509 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1510 return true;
1511}
1512
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001513/// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1514/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1515/// when they refer multiple MIOperands inside a single one.
1516bool ARMAsmParser::
1517CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1518 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1519 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1520
1521 // Create a writeback register dummy placeholder.
1522 Inst.addOperand(MCOperand::CreateImm(0));
1523
1524 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1525 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1526 return true;
1527}
1528
1529/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1530/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1531/// when they refer multiple MIOperands inside a single one.
1532bool ARMAsmParser::
1533CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1534 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1535 // Create a writeback register dummy placeholder.
1536 Inst.addOperand(MCOperand::CreateImm(0));
1537 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1538 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1539 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1540 return true;
1541}
1542
Bill Wendlinge7176102010-11-06 22:36:58 +00001543/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001544/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001545///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001546/// TODO Only preindexing and postindexing addressing are started, unindexed
1547/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001548bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001549ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1550 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001551 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001552 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001553 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001554 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001555 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001556
Sean Callanan18b83232010-01-19 21:44:56 +00001557 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001558 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1559 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001560 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001561 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001562 int BaseRegNum = TryParseRegister();
1563 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001564 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001565 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001566 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001567
Daniel Dunbar05710932011-01-18 05:34:17 +00001568 // The next token must either be a comma or a closing bracket.
1569 const AsmToken &Tok = Parser.getTok();
1570 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1571 return true;
1572
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001573 bool Preindexed = false;
1574 bool Postindexed = false;
1575 bool OffsetIsReg = false;
1576 bool Negative = false;
1577 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001578 ARMOperand *WBOp = 0;
1579 int OffsetRegNum = -1;
1580 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001581 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001582 const MCExpr *ShiftAmount = 0;
1583 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001584
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001585 // First look for preindexed address forms, that is after the "[Rn" we now
1586 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001587 if (Tok.is(AsmToken::Comma)) {
1588 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001589 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001590
Chris Lattner550276e2010-10-28 20:52:15 +00001591 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1592 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001593 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001594 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001595 if (RBracTok.isNot(AsmToken::RBrac)) {
1596 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001597 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001598 }
Sean Callanan76264762010-04-02 22:27:05 +00001599 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001600 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001601
Sean Callanan18b83232010-01-19 21:44:56 +00001602 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001603 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001604 // None of addrmode3 instruction uses "!"
1605 if (AddrMode == ARMII::AddrMode3)
1606 return true;
1607
Bill Wendling50d0f582010-11-18 23:43:05 +00001608 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1609 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001610 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001611 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001612 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1613 if (AddrMode == ARMII::AddrMode2)
1614 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001615 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001616 } else {
1617 // The "[Rn" we have so far was not followed by a comma.
1618
Jim Grosbach80eb2332010-10-29 17:41:25 +00001619 // If there's anything other than the right brace, this is a post indexing
1620 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001621 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001622 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001623
Sean Callanan18b83232010-01-19 21:44:56 +00001624 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001625
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001626 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001627 Postindexed = true;
1628 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001629
Chris Lattner550276e2010-10-28 20:52:15 +00001630 if (NextTok.isNot(AsmToken::Comma)) {
1631 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001632 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001633 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001634
Sean Callananb9a25b72010-01-19 20:27:46 +00001635 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001636
Chris Lattner550276e2010-10-28 20:52:15 +00001637 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001638 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001639 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001640 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001641 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001642 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001643
1644 // Force Offset to exist if used.
1645 if (!OffsetIsReg) {
1646 if (!Offset)
1647 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001648 } else {
1649 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1650 Error(E, "shift amount not supported");
1651 return true;
1652 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001653 }
1654
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001655 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1656 Offset, OffsetRegNum, OffsetRegShifted,
1657 ShiftType, ShiftAmount, Preindexed,
1658 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001659 if (WBOp)
1660 Operands.push_back(WBOp);
1661
1662 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001663}
1664
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001665/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1666/// we will parse the following (were +/- means that a plus or minus is
1667/// optional):
1668/// +/-Rm
1669/// +/-Rm, shift
1670/// #offset
1671/// we return false on success or an error otherwise.
1672bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001673 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001674 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001675 const MCExpr *&ShiftAmount,
1676 const MCExpr *&Offset,
1677 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001678 int &OffsetRegNum,
1679 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001680 Negative = false;
1681 OffsetRegShifted = false;
1682 OffsetIsReg = false;
1683 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001684 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001685 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001686 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001687 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001688 else if (NextTok.is(AsmToken::Minus)) {
1689 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001690 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001691 }
1692 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001693 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001694 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001695 SMLoc CurLoc = OffsetRegTok.getLoc();
1696 OffsetRegNum = TryParseRegister();
1697 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001698 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001699 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001700 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001701 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001702
Bill Wendling12f40e92010-11-06 10:51:53 +00001703 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001704 if (OffsetRegNum != -1) {
1705 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001706 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001707 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001708 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001709
Sean Callanan18b83232010-01-19 21:44:56 +00001710 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001711 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001712 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001713 OffsetRegShifted = true;
1714 }
1715 }
1716 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1717 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001718 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001719 if (HashTok.isNot(AsmToken::Hash))
1720 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001721
Sean Callananb9a25b72010-01-19 20:27:46 +00001722 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001723
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001724 if (getParser().ParseExpression(Offset))
1725 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001726 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001727 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001728 return false;
1729}
1730
1731/// ParseShift as one of these two:
1732/// ( lsl | lsr | asr | ror ) , # shift_amount
1733/// rrx
1734/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001735bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1736 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001737 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001738 if (Tok.isNot(AsmToken::Identifier))
1739 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001740 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001741 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001742 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001743 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001744 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001745 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001746 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001747 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001748 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001749 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001750 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001751 else
1752 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001753 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001754
1755 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001756 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001757 return false;
1758
1759 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001760 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001761 if (HashTok.isNot(AsmToken::Hash))
1762 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001763 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001764
1765 if (getParser().ParseExpression(ShiftAmount))
1766 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001767
1768 return false;
1769}
1770
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001771/// Parse a arm instruction operand. For now this parses the operand regardless
1772/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001773bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001774 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001775 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001776
1777 // Check if the current operand has a custom associated parser, if so, try to
1778 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001779 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1780 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001781 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001782 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1783 // there was a match, but an error occurred, in which case, just return that
1784 // the operand parsing failed.
1785 if (ResTy == MatchOperand_ParseFail)
1786 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001787
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001788 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001789 default:
1790 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001791 return true;
Jim Grosbach19906722011-07-13 18:49:30 +00001792 case AsmToken::Identifier: {
Bill Wendling50d0f582010-11-18 23:43:05 +00001793 if (!TryParseRegisterWithWriteBack(Operands))
1794 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001795 int Res = TryParseShiftRegister(Operands);
1796 if (Res == 0) // success
Owen Anderson00828302011-03-18 22:50:18 +00001797 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001798 else if (Res == -1) // irrecoverable error
1799 return true;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001800
1801 // Fall though for the Identifier case that is not a register or a
1802 // special name.
Jim Grosbach19906722011-07-13 18:49:30 +00001803 }
Kevin Enderby67b212e2011-01-13 20:32:36 +00001804 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1805 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001806 // This was not a register so parse other operands that start with an
1807 // identifier (like labels) as expressions and create them as immediates.
1808 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001809 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001810 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001811 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001812 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001813 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1814 return false;
1815 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001816 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001817 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001818 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001819 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001820 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001821 // #42 -> immediate.
1822 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001823 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001824 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001825 const MCExpr *ImmVal;
1826 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001827 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001828 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001829 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1830 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001831 case AsmToken::Colon: {
1832 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001833 // FIXME: Check it's an expression prefix,
1834 // e.g. (FOO - :lower16:BAR) isn't legal.
1835 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001836 if (ParsePrefix(RefKind))
1837 return true;
1838
Evan Cheng75972122011-01-13 07:58:56 +00001839 const MCExpr *SubExprVal;
1840 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001841 return true;
1842
Evan Cheng75972122011-01-13 07:58:56 +00001843 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1844 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001845 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001846 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001847 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001848 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001849 }
1850}
1851
Evan Cheng75972122011-01-13 07:58:56 +00001852// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1853// :lower16: and :upper16:.
1854bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1855 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001856
1857 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001858 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001859 Parser.Lex(); // Eat ':'
1860
1861 if (getLexer().isNot(AsmToken::Identifier)) {
1862 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1863 return true;
1864 }
1865
1866 StringRef IDVal = Parser.getTok().getIdentifier();
1867 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001868 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001869 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001870 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001871 } else {
1872 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1873 return true;
1874 }
1875 Parser.Lex();
1876
1877 if (getLexer().isNot(AsmToken::Colon)) {
1878 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1879 return true;
1880 }
1881 Parser.Lex(); // Eat the last ':'
1882 return false;
1883}
1884
1885const MCExpr *
1886ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1887 MCSymbolRefExpr::VariantKind Variant) {
1888 // Recurse over the given expression, rebuilding it to apply the given variant
1889 // to the leftmost symbol.
1890 if (Variant == MCSymbolRefExpr::VK_None)
1891 return E;
1892
1893 switch (E->getKind()) {
1894 case MCExpr::Target:
1895 llvm_unreachable("Can't handle target expr yet");
1896 case MCExpr::Constant:
1897 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1898
1899 case MCExpr::SymbolRef: {
1900 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1901
1902 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1903 return 0;
1904
1905 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1906 }
1907
1908 case MCExpr::Unary:
1909 llvm_unreachable("Can't handle unary expressions yet");
1910
1911 case MCExpr::Binary: {
1912 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1913 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1914 const MCExpr *RHS = BE->getRHS();
1915 if (!LHS)
1916 return 0;
1917
1918 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1919 }
1920 }
1921
1922 assert(0 && "Invalid expression kind!");
1923 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001924}
1925
Daniel Dunbar352e1482011-01-11 15:59:50 +00001926/// \brief Given a mnemonic, split out possible predication code and carry
1927/// setting letters to form a canonical mnemonic and flags.
1928//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001929// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001930static StringRef SplitMnemonic(StringRef Mnemonic,
1931 unsigned &PredicationCode,
1932 bool &CarrySetting,
1933 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001934 PredicationCode = ARMCC::AL;
1935 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001936 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001937
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001938 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001939 //
1940 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001941 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1942 Mnemonic == "movs" ||
1943 Mnemonic == "svc" ||
1944 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1945 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1946 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1947 Mnemonic == "vclt" ||
1948 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1949 Mnemonic == "vcle" ||
1950 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1951 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
Jim Grosbachd1f0bbe2011-06-27 20:59:10 +00001952 Mnemonic == "vqdmlal" || Mnemonic == "bics"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001953 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001954
Jim Grosbach3f00e312011-07-11 17:09:57 +00001955 // First, split out any predication code. Ignore mnemonics we know aren't
1956 // predicated but do have a carry-set and so weren't caught above.
1957 if (Mnemonic != "adcs") {
1958 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
1959 .Case("eq", ARMCC::EQ)
1960 .Case("ne", ARMCC::NE)
1961 .Case("hs", ARMCC::HS)
1962 .Case("cs", ARMCC::HS)
1963 .Case("lo", ARMCC::LO)
1964 .Case("cc", ARMCC::LO)
1965 .Case("mi", ARMCC::MI)
1966 .Case("pl", ARMCC::PL)
1967 .Case("vs", ARMCC::VS)
1968 .Case("vc", ARMCC::VC)
1969 .Case("hi", ARMCC::HI)
1970 .Case("ls", ARMCC::LS)
1971 .Case("ge", ARMCC::GE)
1972 .Case("lt", ARMCC::LT)
1973 .Case("gt", ARMCC::GT)
1974 .Case("le", ARMCC::LE)
1975 .Case("al", ARMCC::AL)
1976 .Default(~0U);
1977 if (CC != ~0U) {
1978 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
1979 PredicationCode = CC;
1980 }
Bill Wendling52925b62010-10-29 23:50:21 +00001981 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001982
Daniel Dunbar352e1482011-01-11 15:59:50 +00001983 // Next, determine if we have a carry setting bit. We explicitly ignore all
1984 // the instructions we know end in 's'.
1985 if (Mnemonic.endswith("s") &&
1986 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1987 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1988 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1989 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1990 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1991 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1992 CarrySetting = true;
1993 }
1994
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001995 // The "cps" instruction can have a interrupt mode operand which is glued into
1996 // the mnemonic. Check if this is the case, split it and parse the imod op
1997 if (Mnemonic.startswith("cps")) {
1998 // Split out any imod code.
1999 unsigned IMod =
2000 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
2001 .Case("ie", ARM_PROC::IE)
2002 .Case("id", ARM_PROC::ID)
2003 .Default(~0U);
2004 if (IMod != ~0U) {
2005 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
2006 ProcessorIMod = IMod;
2007 }
2008 }
2009
Daniel Dunbar352e1482011-01-11 15:59:50 +00002010 return Mnemonic;
2011}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002012
2013/// \brief Given a canonical mnemonic, determine if the instruction ever allows
2014/// inclusion of carry set or predication code operands.
2015//
2016// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00002017void ARMAsmParser::
2018GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
2019 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002020 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
2021 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
2022 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
2023 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002024 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002025 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
2026 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002027 Mnemonic == "eor" || Mnemonic == "smlal" ||
Evan Chengebdeeab2011-07-08 01:53:10 +00002028 (Mnemonic == "mov" && !isThumbOne())) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002029 CanAcceptCarrySet = true;
2030 } else {
2031 CanAcceptCarrySet = false;
2032 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002033
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002034 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
2035 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
2036 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
2037 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00002038 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002039 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002040 CanAcceptPredicationCode = false;
2041 } else {
2042 CanAcceptPredicationCode = true;
2043 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002044
Evan Chengebdeeab2011-07-08 01:53:10 +00002045 if (isThumb())
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002046 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00002047 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002048 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002049}
2050
2051/// Parse an arm instruction mnemonic followed by its operands.
2052bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
2053 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2054 // Create the leading tokens for the mnemonic, split by '.' characters.
2055 size_t Start = 0, Next = Name.find('.');
2056 StringRef Head = Name.slice(Start, Next);
2057
Daniel Dunbar352e1482011-01-11 15:59:50 +00002058 // Split out the predication code and carry setting flag from the mnemonic.
2059 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002060 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00002061 bool CarrySetting;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002062 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
2063 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002064
Chris Lattner3a697562010-10-28 17:20:03 +00002065 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00002066
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002067 // Next, add the CCOut and ConditionCode operands, if needed.
2068 //
2069 // For mnemonics which can ever incorporate a carry setting bit or predication
2070 // code, our matching model involves us always generating CCOut and
2071 // ConditionCode operands to match the mnemonic "as written" and then we let
2072 // the matcher deal with finding the right instruction or generating an
2073 // appropriate error.
2074 bool CanAcceptCarrySet, CanAcceptPredicationCode;
2075 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
2076
2077 // Add the carry setting operand, if necessary.
2078 //
2079 // FIXME: It would be awesome if we could somehow invent a location such that
2080 // match errors on this operand would print a nice diagnostic about how the
2081 // 's' character in the mnemonic resulted in a CCOut operand.
2082 if (CanAcceptCarrySet) {
2083 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
2084 NameLoc));
2085 } else {
2086 // This mnemonic can't ever accept a carry set, but the user wrote one (or
2087 // misspelled another mnemonic).
2088
2089 // FIXME: Issue a nice error.
2090 }
2091
2092 // Add the predication code operand, if necessary.
2093 if (CanAcceptPredicationCode) {
2094 Operands.push_back(ARMOperand::CreateCondCode(
2095 ARMCC::CondCodes(PredicationCode), NameLoc));
2096 } else {
2097 // This mnemonic can't ever accept a predication code, but the user wrote
2098 // one (or misspelled another mnemonic).
2099
2100 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002101 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002102
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002103 // Add the processor imod operand, if necessary.
2104 if (ProcessorIMod) {
2105 Operands.push_back(ARMOperand::CreateImm(
2106 MCConstantExpr::Create(ProcessorIMod, getContext()),
2107 NameLoc, NameLoc));
2108 } else {
2109 // This mnemonic can't ever accept a imod, but the user wrote
2110 // one (or misspelled another mnemonic).
2111
2112 // FIXME: Issue a nice error.
2113 }
2114
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002115 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00002116 while (Next != StringRef::npos) {
2117 Start = Next;
2118 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002119 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002120
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002121 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00002122 }
2123
2124 // Read the remaining operands.
2125 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002126 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002127 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002128 Parser.EatToEndOfStatement();
2129 return true;
2130 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002131
2132 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00002133 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002134
2135 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002136 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002137 Parser.EatToEndOfStatement();
2138 return true;
2139 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002140 }
2141 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002142
Chris Lattnercbf8a982010-09-11 16:18:25 +00002143 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2144 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00002145 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00002146 }
Bill Wendling146018f2010-11-06 21:42:12 +00002147
Chris Lattner34e53142010-09-08 05:10:46 +00002148 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00002149 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002150}
2151
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002152bool ARMAsmParser::
2153MatchAndEmitInstruction(SMLoc IDLoc,
2154 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2155 MCStreamer &Out) {
2156 MCInst Inst;
2157 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002158 MatchResultTy MatchResult, MatchResult2;
2159 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2160 if (MatchResult != Match_Success) {
2161 // If we get a Match_InvalidOperand it might be some arithmetic instruction
2162 // that does not update the condition codes. So try adding a CCOut operand
2163 // with a value of reg0.
2164 if (MatchResult == Match_InvalidOperand) {
2165 Operands.insert(Operands.begin() + 1,
2166 ARMOperand::CreateCCOut(0,
2167 ((ARMOperand*)Operands[0])->getStartLoc()));
2168 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2169 if (MatchResult2 == Match_Success)
2170 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002171 else {
2172 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002173 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002174 delete CCOut;
2175 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002176 }
2177 // If we get a Match_MnemonicFail it might be some arithmetic instruction
2178 // that updates the condition codes if it ends in 's'. So see if the
2179 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
2180 // operand with a value of CPSR.
Evan Chengeb0caa12011-07-08 22:49:55 +00002181 else if (MatchResult == Match_MnemonicFail) {
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002182 // Get the instruction mnemonic, which is the first token.
2183 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
2184 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
2185 // removed the 's' from the mnemonic for matching.
2186 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
2187 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002188 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2189 Operands.erase(Operands.begin());
2190 delete OldMnemonic;
2191 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002192 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
2193 Operands.insert(Operands.begin() + 1,
2194 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
2195 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2196 if (MatchResult2 == Match_Success)
2197 MatchResult = Match_Success;
2198 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002199 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2200 Operands.erase(Operands.begin());
2201 delete OldMnemonic;
2202 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002203 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002204 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
2205 Operands.erase(Operands.begin() + 1);
2206 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002207 }
2208 }
2209 }
2210 }
2211 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002212 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002213 Out.EmitInstruction(Inst);
2214 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002215 case Match_MissingFeature:
2216 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2217 return true;
2218 case Match_InvalidOperand: {
2219 SMLoc ErrorLoc = IDLoc;
2220 if (ErrorInfo != ~0U) {
2221 if (ErrorInfo >= Operands.size())
2222 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002223
Chris Lattnere73d4f82010-10-28 21:41:58 +00002224 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2225 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2226 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002227
Chris Lattnere73d4f82010-10-28 21:41:58 +00002228 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002229 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002230 case Match_MnemonicFail:
2231 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002232 case Match_ConversionFail:
2233 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002234 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002235
Eric Christopherc223e2b2010-10-29 09:26:59 +00002236 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002237 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002238}
2239
Kevin Enderby515d5092009-10-15 20:48:48 +00002240/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002241bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2242 StringRef IDVal = DirectiveID.getIdentifier();
2243 if (IDVal == ".word")
2244 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002245 else if (IDVal == ".thumb")
2246 return ParseDirectiveThumb(DirectiveID.getLoc());
2247 else if (IDVal == ".thumb_func")
2248 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2249 else if (IDVal == ".code")
2250 return ParseDirectiveCode(DirectiveID.getLoc());
2251 else if (IDVal == ".syntax")
2252 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002253 return true;
2254}
2255
2256/// ParseDirectiveWord
2257/// ::= .word [ expression (, expression)* ]
2258bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2259 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2260 for (;;) {
2261 const MCExpr *Value;
2262 if (getParser().ParseExpression(Value))
2263 return true;
2264
Chris Lattneraaec2052010-01-19 19:46:13 +00002265 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002266
2267 if (getLexer().is(AsmToken::EndOfStatement))
2268 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002269
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002270 // FIXME: Improve diagnostic.
2271 if (getLexer().isNot(AsmToken::Comma))
2272 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002273 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002274 }
2275 }
2276
Sean Callananb9a25b72010-01-19 20:27:46 +00002277 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002278 return false;
2279}
2280
Kevin Enderby515d5092009-10-15 20:48:48 +00002281/// ParseDirectiveThumb
2282/// ::= .thumb
2283bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2284 if (getLexer().isNot(AsmToken::EndOfStatement))
2285 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002286 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002287
2288 // TODO: set thumb mode
2289 // TODO: tell the MC streamer the mode
2290 // getParser().getStreamer().Emit???();
2291 return false;
2292}
2293
2294/// ParseDirectiveThumbFunc
2295/// ::= .thumbfunc symbol_name
2296bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00002297 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
2298 bool isMachO = MAI.hasSubsectionsViaSymbols();
2299 StringRef Name;
2300
2301 // Darwin asm has function name after .thumb_func direction
2302 // ELF doesn't
2303 if (isMachO) {
2304 const AsmToken &Tok = Parser.getTok();
2305 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2306 return Error(L, "unexpected token in .thumb_func directive");
2307 Name = Tok.getString();
2308 Parser.Lex(); // Consume the identifier token.
2309 }
2310
Kevin Enderby515d5092009-10-15 20:48:48 +00002311 if (getLexer().isNot(AsmToken::EndOfStatement))
2312 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002313 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002314
Rafael Espindola64695402011-05-16 16:17:21 +00002315 // FIXME: assuming function name will be the line following .thumb_func
2316 if (!isMachO) {
2317 Name = Parser.getTok().getString();
2318 }
2319
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002320 // Mark symbol as a thumb symbol.
2321 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2322 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002323 return false;
2324}
2325
2326/// ParseDirectiveSyntax
2327/// ::= .syntax unified | divided
2328bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002329 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002330 if (Tok.isNot(AsmToken::Identifier))
2331 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002332 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002333 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002334 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002335 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002336 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002337 else
2338 return Error(L, "unrecognized syntax mode in .syntax directive");
2339
2340 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002341 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002342 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002343
2344 // TODO tell the MC streamer the mode
2345 // getParser().getStreamer().Emit???();
2346 return false;
2347}
2348
2349/// ParseDirectiveCode
2350/// ::= .code 16 | 32
2351bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002352 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002353 if (Tok.isNot(AsmToken::Integer))
2354 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002355 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002356 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002357 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002358 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002359 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002360 else
2361 return Error(L, "invalid operand to .code directive");
2362
2363 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002364 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002365 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002366
Evan Cheng32869202011-07-08 22:36:29 +00002367 if (Val == 16) {
Evan Chengffc0e732011-07-09 05:47:46 +00002368 if (!isThumb())
2369 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002370 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Evan Cheng32869202011-07-08 22:36:29 +00002371 } else {
Evan Chengffc0e732011-07-09 05:47:46 +00002372 if (isThumb())
2373 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002374 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Evan Chengeb0caa12011-07-08 22:49:55 +00002375 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002376
Kevin Enderby515d5092009-10-15 20:48:48 +00002377 return false;
2378}
2379
Sean Callanan90b70972010-04-07 20:29:34 +00002380extern "C" void LLVMInitializeARMAsmLexer();
2381
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002382/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002383extern "C" void LLVMInitializeARMAsmParser() {
2384 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2385 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002386 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002387}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002388
Chris Lattner0692ee62010-09-06 19:11:01 +00002389#define GET_REGISTER_MATCHER
2390#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002391#include "ARMGenAsmMatcher.inc"