blob: 499c95440c43b025adcbfd134d0abff178fcc122 [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.
1088 ARMOperand *PrevOp = (ARMOperand*)Operands.pop_back_val();
1089 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)
1346 .Case("ish", ARM_MB::ISH)
1347 .Case("ishst", ARM_MB::ISHST)
1348 .Case("nsh", ARM_MB::NSH)
1349 .Case("nshst", ARM_MB::NSHST)
1350 .Case("osh", ARM_MB::OSH)
1351 .Case("oshst", ARM_MB::OSHST)
1352 .Default(~0U);
1353
1354 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001355 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001356
1357 Parser.Lex(); // Eat identifier token.
1358 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001359 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001360}
1361
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001362/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001363ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1364tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1365 SMLoc S = Parser.getTok().getLoc();
1366 const AsmToken &Tok = Parser.getTok();
1367 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1368 StringRef IFlagsStr = Tok.getString();
1369
1370 unsigned IFlags = 0;
1371 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1372 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1373 .Case("a", ARM_PROC::A)
1374 .Case("i", ARM_PROC::I)
1375 .Case("f", ARM_PROC::F)
1376 .Default(~0U);
1377
1378 // If some specific iflag is already set, it means that some letter is
1379 // present more than once, this is not acceptable.
1380 if (Flag == ~0U || (IFlags & Flag))
1381 return MatchOperand_NoMatch;
1382
1383 IFlags |= Flag;
1384 }
1385
1386 Parser.Lex(); // Eat identifier token.
1387 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1388 return MatchOperand_Success;
1389}
1390
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001391/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1392ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1393tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1394 SMLoc S = Parser.getTok().getLoc();
1395 const AsmToken &Tok = Parser.getTok();
1396 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1397 StringRef Mask = Tok.getString();
1398
1399 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1400 size_t Start = 0, Next = Mask.find('_');
1401 StringRef Flags = "";
1402 StringRef SpecReg = Mask.slice(Start, Next);
1403 if (Next != StringRef::npos)
1404 Flags = Mask.slice(Next+1, Mask.size());
1405
1406 // FlagsVal contains the complete mask:
1407 // 3-0: Mask
1408 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1409 unsigned FlagsVal = 0;
1410
1411 if (SpecReg == "apsr") {
1412 FlagsVal = StringSwitch<unsigned>(Flags)
1413 .Case("nzcvq", 0x8) // same as CPSR_c
1414 .Case("g", 0x4) // same as CPSR_s
1415 .Case("nzcvqg", 0xc) // same as CPSR_fs
1416 .Default(~0U);
1417
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001418 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001419 if (!Flags.empty())
1420 return MatchOperand_NoMatch;
1421 else
1422 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001423 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001424 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
Bruno Cardoso Lopes56926a32011-05-25 00:35:03 +00001425 if (Flags == "all") // cpsr_all is an alias for cpsr_fc
1426 Flags = "fc";
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001427 for (int i = 0, e = Flags.size(); i != e; ++i) {
1428 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1429 .Case("c", 1)
1430 .Case("x", 2)
1431 .Case("s", 4)
1432 .Case("f", 8)
1433 .Default(~0U);
1434
1435 // If some specific flag is already set, it means that some letter is
1436 // present more than once, this is not acceptable.
1437 if (FlagsVal == ~0U || (FlagsVal & Flag))
1438 return MatchOperand_NoMatch;
1439 FlagsVal |= Flag;
1440 }
1441 } else // No match for special register.
1442 return MatchOperand_NoMatch;
1443
1444 // Special register without flags are equivalent to "fc" flags.
1445 if (!FlagsVal)
1446 FlagsVal = 0x9;
1447
1448 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1449 if (SpecReg == "spsr")
1450 FlagsVal |= 16;
1451
1452 Parser.Lex(); // Eat identifier token.
1453 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1454 return MatchOperand_Success;
1455}
1456
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001457/// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1458ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1459tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Matt Beaumont-Gaye3662cc2011-04-01 00:06:01 +00001460 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001461
1462 if (ParseMemory(Operands, ARMII::AddrMode2))
1463 return MatchOperand_NoMatch;
1464
1465 return MatchOperand_Success;
1466}
1467
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001468/// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1469ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1470tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1471 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1472
1473 if (ParseMemory(Operands, ARMII::AddrMode3))
1474 return MatchOperand_NoMatch;
1475
1476 return MatchOperand_Success;
1477}
1478
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001479/// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1480/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1481/// when they refer multiple MIOperands inside a single one.
1482bool ARMAsmParser::
1483CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1484 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1485 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1486
1487 // Create a writeback register dummy placeholder.
1488 Inst.addOperand(MCOperand::CreateImm(0));
1489
1490 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1491 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1492 return true;
1493}
1494
1495/// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1496/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1497/// when they refer multiple MIOperands inside a single one.
1498bool ARMAsmParser::
1499CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1500 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1501 // Create a writeback register dummy placeholder.
1502 Inst.addOperand(MCOperand::CreateImm(0));
1503 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1504 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1505 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1506 return true;
1507}
1508
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001509/// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1510/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1511/// when they refer multiple MIOperands inside a single one.
1512bool ARMAsmParser::
1513CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1514 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1515 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1516
1517 // Create a writeback register dummy placeholder.
1518 Inst.addOperand(MCOperand::CreateImm(0));
1519
1520 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1521 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1522 return true;
1523}
1524
1525/// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1526/// Needed here because the Asm Gen Matcher can't handle properly tied operands
1527/// when they refer multiple MIOperands inside a single one.
1528bool ARMAsmParser::
1529CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1530 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1531 // Create a writeback register dummy placeholder.
1532 Inst.addOperand(MCOperand::CreateImm(0));
1533 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1534 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1535 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1536 return true;
1537}
1538
Bill Wendlinge7176102010-11-06 22:36:58 +00001539/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001540/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001541///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001542/// TODO Only preindexing and postindexing addressing are started, unindexed
1543/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001544bool ARMAsmParser::
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001545ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1546 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
Sean Callanan76264762010-04-02 22:27:05 +00001547 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001548 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001549 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001550 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001551 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001552
Sean Callanan18b83232010-01-19 21:44:56 +00001553 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001554 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1555 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001556 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001557 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001558 int BaseRegNum = TryParseRegister();
1559 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001560 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001561 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001562 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001563
Daniel Dunbar05710932011-01-18 05:34:17 +00001564 // The next token must either be a comma or a closing bracket.
1565 const AsmToken &Tok = Parser.getTok();
1566 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1567 return true;
1568
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001569 bool Preindexed = false;
1570 bool Postindexed = false;
1571 bool OffsetIsReg = false;
1572 bool Negative = false;
1573 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001574 ARMOperand *WBOp = 0;
1575 int OffsetRegNum = -1;
1576 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001577 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001578 const MCExpr *ShiftAmount = 0;
1579 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001580
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001581 // First look for preindexed address forms, that is after the "[Rn" we now
1582 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001583 if (Tok.is(AsmToken::Comma)) {
1584 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001585 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001586
Chris Lattner550276e2010-10-28 20:52:15 +00001587 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1588 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001589 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001590 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001591 if (RBracTok.isNot(AsmToken::RBrac)) {
1592 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001593 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001594 }
Sean Callanan76264762010-04-02 22:27:05 +00001595 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001596 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001597
Sean Callanan18b83232010-01-19 21:44:56 +00001598 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001599 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001600 // None of addrmode3 instruction uses "!"
1601 if (AddrMode == ARMII::AddrMode3)
1602 return true;
1603
Bill Wendling50d0f582010-11-18 23:43:05 +00001604 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1605 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001606 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001607 Parser.Lex(); // Eat exclaim token
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001608 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1609 if (AddrMode == ARMII::AddrMode2)
1610 Preindexed = false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001611 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001612 } else {
1613 // The "[Rn" we have so far was not followed by a comma.
1614
Jim Grosbach80eb2332010-10-29 17:41:25 +00001615 // If there's anything other than the right brace, this is a post indexing
1616 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001617 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001618 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001619
Sean Callanan18b83232010-01-19 21:44:56 +00001620 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001621
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001622 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001623 Postindexed = true;
1624 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001625
Chris Lattner550276e2010-10-28 20:52:15 +00001626 if (NextTok.isNot(AsmToken::Comma)) {
1627 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001628 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001629 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001630
Sean Callananb9a25b72010-01-19 20:27:46 +00001631 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001632
Chris Lattner550276e2010-10-28 20:52:15 +00001633 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001634 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001635 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001636 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001637 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001638 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001639
1640 // Force Offset to exist if used.
1641 if (!OffsetIsReg) {
1642 if (!Offset)
1643 Offset = MCConstantExpr::Create(0, getContext());
Bruno Cardoso Lopesac79e4c2011-04-04 17:18:19 +00001644 } else {
1645 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1646 Error(E, "shift amount not supported");
1647 return true;
1648 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001649 }
1650
Bruno Cardoso Lopesae085542011-03-31 23:26:08 +00001651 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1652 Offset, OffsetRegNum, OffsetRegShifted,
1653 ShiftType, ShiftAmount, Preindexed,
1654 Postindexed, Negative, Writeback, S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001655 if (WBOp)
1656 Operands.push_back(WBOp);
1657
1658 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001659}
1660
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001661/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1662/// we will parse the following (were +/- means that a plus or minus is
1663/// optional):
1664/// +/-Rm
1665/// +/-Rm, shift
1666/// #offset
1667/// we return false on success or an error otherwise.
1668bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001669 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001670 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001671 const MCExpr *&ShiftAmount,
1672 const MCExpr *&Offset,
1673 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001674 int &OffsetRegNum,
1675 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001676 Negative = false;
1677 OffsetRegShifted = false;
1678 OffsetIsReg = false;
1679 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001680 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001681 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001682 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001683 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001684 else if (NextTok.is(AsmToken::Minus)) {
1685 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001686 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001687 }
1688 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001689 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001690 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001691 SMLoc CurLoc = OffsetRegTok.getLoc();
1692 OffsetRegNum = TryParseRegister();
1693 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001694 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001695 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001696 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001697 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001698
Bill Wendling12f40e92010-11-06 10:51:53 +00001699 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001700 if (OffsetRegNum != -1) {
1701 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001702 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001703 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001704 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001705
Sean Callanan18b83232010-01-19 21:44:56 +00001706 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001707 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001708 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001709 OffsetRegShifted = true;
1710 }
1711 }
1712 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1713 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001714 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001715 if (HashTok.isNot(AsmToken::Hash))
1716 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001717
Sean Callananb9a25b72010-01-19 20:27:46 +00001718 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001719
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001720 if (getParser().ParseExpression(Offset))
1721 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001722 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001723 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001724 return false;
1725}
1726
1727/// ParseShift as one of these two:
1728/// ( lsl | lsr | asr | ror ) , # shift_amount
1729/// rrx
1730/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001731bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1732 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001733 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001734 if (Tok.isNot(AsmToken::Identifier))
1735 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001736 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001737 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001738 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001739 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001740 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001741 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001742 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001743 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001744 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001745 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001746 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001747 else
1748 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001749 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001750
1751 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001752 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001753 return false;
1754
1755 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001756 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001757 if (HashTok.isNot(AsmToken::Hash))
1758 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001759 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001760
1761 if (getParser().ParseExpression(ShiftAmount))
1762 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001763
1764 return false;
1765}
1766
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001767/// Parse a arm instruction operand. For now this parses the operand regardless
1768/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001769bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001770 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001771 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001772
1773 // Check if the current operand has a custom associated parser, if so, try to
1774 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001775 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1776 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001777 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001778 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1779 // there was a match, but an error occurred, in which case, just return that
1780 // the operand parsing failed.
1781 if (ResTy == MatchOperand_ParseFail)
1782 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001783
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001784 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001785 default:
1786 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001787 return true;
Jim Grosbach19906722011-07-13 18:49:30 +00001788 case AsmToken::Identifier: {
Bill Wendling50d0f582010-11-18 23:43:05 +00001789 if (!TryParseRegisterWithWriteBack(Operands))
1790 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001791 int Res = TryParseShiftRegister(Operands);
1792 if (Res == 0) // success
Owen Anderson00828302011-03-18 22:50:18 +00001793 return false;
Jim Grosbach19906722011-07-13 18:49:30 +00001794 else if (Res == -1) // irrecoverable error
1795 return true;
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001796
1797 // Fall though for the Identifier case that is not a register or a
1798 // special name.
Jim Grosbach19906722011-07-13 18:49:30 +00001799 }
Kevin Enderby67b212e2011-01-13 20:32:36 +00001800 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1801 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001802 // This was not a register so parse other operands that start with an
1803 // identifier (like labels) as expressions and create them as immediates.
1804 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001805 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001806 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001807 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001808 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001809 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1810 return false;
1811 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001812 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001813 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001814 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001815 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001816 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001817 // #42 -> immediate.
1818 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001819 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001820 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001821 const MCExpr *ImmVal;
1822 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001823 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001824 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001825 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1826 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001827 case AsmToken::Colon: {
1828 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001829 // FIXME: Check it's an expression prefix,
1830 // e.g. (FOO - :lower16:BAR) isn't legal.
1831 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001832 if (ParsePrefix(RefKind))
1833 return true;
1834
Evan Cheng75972122011-01-13 07:58:56 +00001835 const MCExpr *SubExprVal;
1836 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001837 return true;
1838
Evan Cheng75972122011-01-13 07:58:56 +00001839 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1840 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001841 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001842 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001843 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001844 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001845 }
1846}
1847
Evan Cheng75972122011-01-13 07:58:56 +00001848// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1849// :lower16: and :upper16:.
1850bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1851 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001852
1853 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001854 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001855 Parser.Lex(); // Eat ':'
1856
1857 if (getLexer().isNot(AsmToken::Identifier)) {
1858 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1859 return true;
1860 }
1861
1862 StringRef IDVal = Parser.getTok().getIdentifier();
1863 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001864 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001865 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001866 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001867 } else {
1868 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1869 return true;
1870 }
1871 Parser.Lex();
1872
1873 if (getLexer().isNot(AsmToken::Colon)) {
1874 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1875 return true;
1876 }
1877 Parser.Lex(); // Eat the last ':'
1878 return false;
1879}
1880
1881const MCExpr *
1882ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1883 MCSymbolRefExpr::VariantKind Variant) {
1884 // Recurse over the given expression, rebuilding it to apply the given variant
1885 // to the leftmost symbol.
1886 if (Variant == MCSymbolRefExpr::VK_None)
1887 return E;
1888
1889 switch (E->getKind()) {
1890 case MCExpr::Target:
1891 llvm_unreachable("Can't handle target expr yet");
1892 case MCExpr::Constant:
1893 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1894
1895 case MCExpr::SymbolRef: {
1896 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1897
1898 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1899 return 0;
1900
1901 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1902 }
1903
1904 case MCExpr::Unary:
1905 llvm_unreachable("Can't handle unary expressions yet");
1906
1907 case MCExpr::Binary: {
1908 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1909 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1910 const MCExpr *RHS = BE->getRHS();
1911 if (!LHS)
1912 return 0;
1913
1914 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1915 }
1916 }
1917
1918 assert(0 && "Invalid expression kind!");
1919 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001920}
1921
Daniel Dunbar352e1482011-01-11 15:59:50 +00001922/// \brief Given a mnemonic, split out possible predication code and carry
1923/// setting letters to form a canonical mnemonic and flags.
1924//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001925// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001926static StringRef SplitMnemonic(StringRef Mnemonic,
1927 unsigned &PredicationCode,
1928 bool &CarrySetting,
1929 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001930 PredicationCode = ARMCC::AL;
1931 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001932 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001933
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001934 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001935 //
1936 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001937 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1938 Mnemonic == "movs" ||
1939 Mnemonic == "svc" ||
1940 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1941 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1942 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1943 Mnemonic == "vclt" ||
1944 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1945 Mnemonic == "vcle" ||
1946 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1947 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
Jim Grosbachd1f0bbe2011-06-27 20:59:10 +00001948 Mnemonic == "vqdmlal" || Mnemonic == "bics"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001949 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001950
Jim Grosbach3f00e312011-07-11 17:09:57 +00001951 // First, split out any predication code. Ignore mnemonics we know aren't
1952 // predicated but do have a carry-set and so weren't caught above.
1953 if (Mnemonic != "adcs") {
1954 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
1955 .Case("eq", ARMCC::EQ)
1956 .Case("ne", ARMCC::NE)
1957 .Case("hs", ARMCC::HS)
1958 .Case("cs", ARMCC::HS)
1959 .Case("lo", ARMCC::LO)
1960 .Case("cc", ARMCC::LO)
1961 .Case("mi", ARMCC::MI)
1962 .Case("pl", ARMCC::PL)
1963 .Case("vs", ARMCC::VS)
1964 .Case("vc", ARMCC::VC)
1965 .Case("hi", ARMCC::HI)
1966 .Case("ls", ARMCC::LS)
1967 .Case("ge", ARMCC::GE)
1968 .Case("lt", ARMCC::LT)
1969 .Case("gt", ARMCC::GT)
1970 .Case("le", ARMCC::LE)
1971 .Case("al", ARMCC::AL)
1972 .Default(~0U);
1973 if (CC != ~0U) {
1974 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
1975 PredicationCode = CC;
1976 }
Bill Wendling52925b62010-10-29 23:50:21 +00001977 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001978
Daniel Dunbar352e1482011-01-11 15:59:50 +00001979 // Next, determine if we have a carry setting bit. We explicitly ignore all
1980 // the instructions we know end in 's'.
1981 if (Mnemonic.endswith("s") &&
1982 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1983 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1984 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1985 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1986 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1987 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1988 CarrySetting = true;
1989 }
1990
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001991 // The "cps" instruction can have a interrupt mode operand which is glued into
1992 // the mnemonic. Check if this is the case, split it and parse the imod op
1993 if (Mnemonic.startswith("cps")) {
1994 // Split out any imod code.
1995 unsigned IMod =
1996 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1997 .Case("ie", ARM_PROC::IE)
1998 .Case("id", ARM_PROC::ID)
1999 .Default(~0U);
2000 if (IMod != ~0U) {
2001 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
2002 ProcessorIMod = IMod;
2003 }
2004 }
2005
Daniel Dunbar352e1482011-01-11 15:59:50 +00002006 return Mnemonic;
2007}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002008
2009/// \brief Given a canonical mnemonic, determine if the instruction ever allows
2010/// inclusion of carry set or predication code operands.
2011//
2012// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00002013void ARMAsmParser::
2014GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
2015 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002016 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
2017 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
2018 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
2019 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002020 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mvn" ||
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002021 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
2022 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
Bruno Cardoso Lopesbe64b392011-05-27 23:46:09 +00002023 Mnemonic == "eor" || Mnemonic == "smlal" ||
Evan Chengebdeeab2011-07-08 01:53:10 +00002024 (Mnemonic == "mov" && !isThumbOne())) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002025 CanAcceptCarrySet = true;
2026 } else {
2027 CanAcceptCarrySet = false;
2028 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002029
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00002030 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
2031 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
2032 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
2033 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00002034 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002035 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002036 CanAcceptPredicationCode = false;
2037 } else {
2038 CanAcceptPredicationCode = true;
2039 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002040
Evan Chengebdeeab2011-07-08 01:53:10 +00002041 if (isThumb())
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002042 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Jim Grosbach63b46fa2011-06-30 22:10:46 +00002043 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00002044 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002045}
2046
2047/// Parse an arm instruction mnemonic followed by its operands.
2048bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
2049 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
2050 // Create the leading tokens for the mnemonic, split by '.' characters.
2051 size_t Start = 0, Next = Name.find('.');
2052 StringRef Head = Name.slice(Start, Next);
2053
Daniel Dunbar352e1482011-01-11 15:59:50 +00002054 // Split out the predication code and carry setting flag from the mnemonic.
2055 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002056 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00002057 bool CarrySetting;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002058 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
2059 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002060
Chris Lattner3a697562010-10-28 17:20:03 +00002061 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00002062
Daniel Dunbar3771dd02011-01-11 15:59:53 +00002063 // Next, add the CCOut and ConditionCode operands, if needed.
2064 //
2065 // For mnemonics which can ever incorporate a carry setting bit or predication
2066 // code, our matching model involves us always generating CCOut and
2067 // ConditionCode operands to match the mnemonic "as written" and then we let
2068 // the matcher deal with finding the right instruction or generating an
2069 // appropriate error.
2070 bool CanAcceptCarrySet, CanAcceptPredicationCode;
2071 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
2072
2073 // Add the carry setting operand, if necessary.
2074 //
2075 // FIXME: It would be awesome if we could somehow invent a location such that
2076 // match errors on this operand would print a nice diagnostic about how the
2077 // 's' character in the mnemonic resulted in a CCOut operand.
2078 if (CanAcceptCarrySet) {
2079 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
2080 NameLoc));
2081 } else {
2082 // This mnemonic can't ever accept a carry set, but the user wrote one (or
2083 // misspelled another mnemonic).
2084
2085 // FIXME: Issue a nice error.
2086 }
2087
2088 // Add the predication code operand, if necessary.
2089 if (CanAcceptPredicationCode) {
2090 Operands.push_back(ARMOperand::CreateCondCode(
2091 ARMCC::CondCodes(PredicationCode), NameLoc));
2092 } else {
2093 // This mnemonic can't ever accept a predication code, but the user wrote
2094 // one (or misspelled another mnemonic).
2095
2096 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00002097 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002098
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002099 // Add the processor imod operand, if necessary.
2100 if (ProcessorIMod) {
2101 Operands.push_back(ARMOperand::CreateImm(
2102 MCConstantExpr::Create(ProcessorIMod, getContext()),
2103 NameLoc, NameLoc));
2104 } else {
2105 // This mnemonic can't ever accept a imod, but the user wrote
2106 // one (or misspelled another mnemonic).
2107
2108 // FIXME: Issue a nice error.
2109 }
2110
Daniel Dunbar345a9a62010-08-11 06:37:20 +00002111 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00002112 while (Next != StringRef::npos) {
2113 Start = Next;
2114 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002115 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002116
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00002117 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00002118 }
2119
2120 // Read the remaining operands.
2121 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002122 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002123 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002124 Parser.EatToEndOfStatement();
2125 return true;
2126 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002127
2128 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00002129 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002130
2131 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00002132 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00002133 Parser.EatToEndOfStatement();
2134 return true;
2135 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00002136 }
2137 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002138
Chris Lattnercbf8a982010-09-11 16:18:25 +00002139 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2140 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00002141 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00002142 }
Bill Wendling146018f2010-11-06 21:42:12 +00002143
Chris Lattner34e53142010-09-08 05:10:46 +00002144 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00002145 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002146}
2147
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002148bool ARMAsmParser::
2149MatchAndEmitInstruction(SMLoc IDLoc,
2150 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
2151 MCStreamer &Out) {
2152 MCInst Inst;
2153 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002154 MatchResultTy MatchResult, MatchResult2;
2155 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2156 if (MatchResult != Match_Success) {
2157 // If we get a Match_InvalidOperand it might be some arithmetic instruction
2158 // that does not update the condition codes. So try adding a CCOut operand
2159 // with a value of reg0.
2160 if (MatchResult == Match_InvalidOperand) {
2161 Operands.insert(Operands.begin() + 1,
2162 ARMOperand::CreateCCOut(0,
2163 ((ARMOperand*)Operands[0])->getStartLoc()));
2164 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2165 if (MatchResult2 == Match_Success)
2166 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002167 else {
2168 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002169 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002170 delete CCOut;
2171 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002172 }
2173 // If we get a Match_MnemonicFail it might be some arithmetic instruction
2174 // that updates the condition codes if it ends in 's'. So see if the
2175 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
2176 // operand with a value of CPSR.
Evan Chengeb0caa12011-07-08 22:49:55 +00002177 else if (MatchResult == Match_MnemonicFail) {
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002178 // Get the instruction mnemonic, which is the first token.
2179 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
2180 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
2181 // removed the 's' from the mnemonic for matching.
2182 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
2183 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002184 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2185 Operands.erase(Operands.begin());
2186 delete OldMnemonic;
2187 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002188 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
2189 Operands.insert(Operands.begin() + 1,
2190 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
2191 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2192 if (MatchResult2 == Match_Success)
2193 MatchResult = Match_Success;
2194 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002195 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2196 Operands.erase(Operands.begin());
2197 delete OldMnemonic;
2198 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002199 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00002200 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
2201 Operands.erase(Operands.begin() + 1);
2202 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00002203 }
2204 }
2205 }
2206 }
2207 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00002208 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002209 Out.EmitInstruction(Inst);
2210 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00002211 case Match_MissingFeature:
2212 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2213 return true;
2214 case Match_InvalidOperand: {
2215 SMLoc ErrorLoc = IDLoc;
2216 if (ErrorInfo != ~0U) {
2217 if (ErrorInfo >= Operands.size())
2218 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00002219
Chris Lattnere73d4f82010-10-28 21:41:58 +00002220 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2221 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2222 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002223
Chris Lattnere73d4f82010-10-28 21:41:58 +00002224 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002225 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00002226 case Match_MnemonicFail:
2227 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00002228 case Match_ConversionFail:
2229 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00002230 }
Jim Grosbach16c74252010-10-29 14:46:02 +00002231
Eric Christopherc223e2b2010-10-29 09:26:59 +00002232 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00002233 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00002234}
2235
Kevin Enderby515d5092009-10-15 20:48:48 +00002236/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002237bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2238 StringRef IDVal = DirectiveID.getIdentifier();
2239 if (IDVal == ".word")
2240 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00002241 else if (IDVal == ".thumb")
2242 return ParseDirectiveThumb(DirectiveID.getLoc());
2243 else if (IDVal == ".thumb_func")
2244 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2245 else if (IDVal == ".code")
2246 return ParseDirectiveCode(DirectiveID.getLoc());
2247 else if (IDVal == ".syntax")
2248 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002249 return true;
2250}
2251
2252/// ParseDirectiveWord
2253/// ::= .word [ expression (, expression)* ]
2254bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2255 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2256 for (;;) {
2257 const MCExpr *Value;
2258 if (getParser().ParseExpression(Value))
2259 return true;
2260
Chris Lattneraaec2052010-01-19 19:46:13 +00002261 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002262
2263 if (getLexer().is(AsmToken::EndOfStatement))
2264 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00002265
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002266 // FIXME: Improve diagnostic.
2267 if (getLexer().isNot(AsmToken::Comma))
2268 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002269 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002270 }
2271 }
2272
Sean Callananb9a25b72010-01-19 20:27:46 +00002273 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002274 return false;
2275}
2276
Kevin Enderby515d5092009-10-15 20:48:48 +00002277/// ParseDirectiveThumb
2278/// ::= .thumb
2279bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2280 if (getLexer().isNot(AsmToken::EndOfStatement))
2281 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002282 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002283
2284 // TODO: set thumb mode
2285 // TODO: tell the MC streamer the mode
2286 // getParser().getStreamer().Emit???();
2287 return false;
2288}
2289
2290/// ParseDirectiveThumbFunc
2291/// ::= .thumbfunc symbol_name
2292bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Rafael Espindola64695402011-05-16 16:17:21 +00002293 const MCAsmInfo &MAI = getParser().getStreamer().getContext().getAsmInfo();
2294 bool isMachO = MAI.hasSubsectionsViaSymbols();
2295 StringRef Name;
2296
2297 // Darwin asm has function name after .thumb_func direction
2298 // ELF doesn't
2299 if (isMachO) {
2300 const AsmToken &Tok = Parser.getTok();
2301 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2302 return Error(L, "unexpected token in .thumb_func directive");
2303 Name = Tok.getString();
2304 Parser.Lex(); // Consume the identifier token.
2305 }
2306
Kevin Enderby515d5092009-10-15 20:48:48 +00002307 if (getLexer().isNot(AsmToken::EndOfStatement))
2308 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002309 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002310
Rafael Espindola64695402011-05-16 16:17:21 +00002311 // FIXME: assuming function name will be the line following .thumb_func
2312 if (!isMachO) {
2313 Name = Parser.getTok().getString();
2314 }
2315
Jim Grosbach642fc9c2010-11-05 22:33:53 +00002316 // Mark symbol as a thumb symbol.
2317 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2318 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00002319 return false;
2320}
2321
2322/// ParseDirectiveSyntax
2323/// ::= .syntax unified | divided
2324bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002325 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002326 if (Tok.isNot(AsmToken::Identifier))
2327 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00002328 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00002329 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00002330 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002331 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00002332 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00002333 else
2334 return Error(L, "unrecognized syntax mode in .syntax directive");
2335
2336 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002337 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002338 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002339
2340 // TODO tell the MC streamer the mode
2341 // getParser().getStreamer().Emit???();
2342 return false;
2343}
2344
2345/// ParseDirectiveCode
2346/// ::= .code 16 | 32
2347bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00002348 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00002349 if (Tok.isNot(AsmToken::Integer))
2350 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00002351 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00002352 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00002353 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00002354 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00002355 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002356 else
2357 return Error(L, "invalid operand to .code directive");
2358
2359 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00002360 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00002361 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00002362
Evan Cheng32869202011-07-08 22:36:29 +00002363 if (Val == 16) {
Evan Chengffc0e732011-07-09 05:47:46 +00002364 if (!isThumb())
2365 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002366 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Evan Cheng32869202011-07-08 22:36:29 +00002367 } else {
Evan Chengffc0e732011-07-09 05:47:46 +00002368 if (isThumb())
2369 SwitchMode();
Jim Grosbach2a301702010-11-05 22:40:53 +00002370 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Evan Chengeb0caa12011-07-08 22:49:55 +00002371 }
Jim Grosbach2a301702010-11-05 22:40:53 +00002372
Kevin Enderby515d5092009-10-15 20:48:48 +00002373 return false;
2374}
2375
Sean Callanan90b70972010-04-07 20:29:34 +00002376extern "C" void LLVMInitializeARMAsmLexer();
2377
Kevin Enderby9c41fa82009-10-30 22:55:57 +00002378/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002379extern "C" void LLVMInitializeARMAsmParser() {
2380 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2381 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00002382 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00002383}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002384
Chris Lattner0692ee62010-09-06 19:11:01 +00002385#define GET_REGISTER_MATCHER
2386#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00002387#include "ARMGenAsmMatcher.inc"